agent-framework icon indicating copy to clipboard operation
agent-framework copied to clipboard

.NET: valkey streaming support dotnet

Open MatthiasHowellYopp opened this issue 7 months ago • 1 comments

Motivation and Context

The Microsoft.Agents.AI.Valkey package provides ValkeyChatHistoryProvider and ValkeyContextProvider for persistent conversation history and memory context. However, the framework currently has no storage-backed resumable streaming component — if a client disconnects during a streamed agent response (RunStreamingAsync), partial output is lost and must be regenerated. This is the final piece of the Valkey integration story, delivering parity with the Python agent-framework-valkey package.

Valkey Streams (XADD/XRANGE) are a natural fit: each entry gets an auto-generated ID that serves as a continuation token, entries are append-only and durable, and XRANGE with an exclusive start ID gives exact resume-from-here semantics — all with core commands, no modules required.

Fixes issue https://github.com/microsoft/agent-framework/issues/5544

Description

Adds ValkeyStreamBuffer to the Microsoft.Agents.AI.Valkey package — a resumable streaming buffer backed by Valkey Streams.

The component writes serialized AgentResponseUpdate chunks to a Valkey Stream via XADD and replays missed entries via XRANGE from a caller-provided entry ID. The stream entry ID returned by each AppendAsync call serves as the continuation token for resumption.

Public API:

  • AppendAsync(responseId, update) — writes a chunk, returns the entry ID
  • ReadAsync(responseId, afterEntryId) — replays entries after a given ID as IAsyncEnumerable
  • GetEntryCountAsync(responseId) — returns stream length
  • DeleteStreamAsync(responseId) — removes a stream
  • MaxLength property — caps stream size via approximate XTRIM after each XADD

Follows the same patterns as the existing Valkey providers: dual constructors (connection string + IConnectionMultiplexer for DI), IAsyncDisposable with disposed-state guard, CancellationToken forwarding, malformed JSON resilience, trimming/AOT attributes, and ILogger integration at appropriate levels (Debug for per-request, Warning for skipped entries).

Includes a self-contained sample (AgentWithMemory_Step06_ResumableStreamingWithValkey) that demonstrates the full workflow — stream, disconnect, resume, full replay — against a live Valkey instance with no LLM or cloud credentials required.

64 unit tests passing, covering constructor validation, append/read round-trips, entry ID incrementing, MaxLength pass-through, malformed entry handling, cancellation, disposal guards, and empty stream edge cases.

Contribution Checklist

  • [x] The code builds clean without any errors or warnings
  • [x] The PR follows the Contribution Guidelines
  • [x] All unit tests pass, and I have added new tests where possible
  • [ ] Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

MatthiasHowellYopp avatar Apr 29 '26 19:04 MatthiasHowellYopp

@westey-m Hoping I can get a review on this this week, tks

MatthiasHowellYopp avatar May 20 '26 15:05 MatthiasHowellYopp

@westey-m Hoping I can get a review on this this week, tks

MatthiasHowellYopp avatar Jul 06 '26 13:07 MatthiasHowellYopp

@westey-m Hoping I can get a review this week on this, thanks.

MatthiasHowellYopp avatar Sep 15 '26 14:09 MatthiasHowellYopp