Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ public override async IAsyncEnumerable<AgentRunResponseUpdate> RunStreamingAsync
(ChatClientAgentThread safeThread, ChatOptions? chatOptions, List<ChatMessage> inputMessagesForChatClient, IList<ChatMessage>? aiContextProviderMessages) =
await this.PrepareThreadAndMessagesAsync(thread, inputMessages, options, cancellationToken).ConfigureAwait(false);

ValidateStreamResumptionAllowed(chatOptions?.ContinuationToken, safeThread);

var chatClient = this.ChatClient;

chatClient = ApplyRunOptionsTransformations(options, chatClient);
Expand Down Expand Up @@ -621,6 +623,12 @@ await thread.AIContextProvider.InvokedAsync(new(inputMessages, aiContextProvider
{
throw new InvalidOperationException("Input messages are not allowed when continuing a background response using a continuation token.");
}

if (chatOptions?.ContinuationToken is not null && typedThread.ConversationId is null && typedThread.MessageStore is null)
{
throw new InvalidOperationException("Continuation tokens are not allowed to be used for initial runs.");
}

List<ChatMessage> inputMessagesForChatClient = [];
IList<ChatMessage>? aiContextProviderMessages = null;

Expand Down Expand Up @@ -731,6 +739,28 @@ private static Task NotifyMessageStoreOfNewMessagesAsync(ChatClientAgentThread t
return Task.CompletedTask;
}

private static void ValidateStreamResumptionAllowed(ResponseContinuationToken? continuationToken, ChatClientAgentThread safeThread)
{
if (continuationToken is null)
{
return;
}

// Streaming resumption is only supported with chat history managed by the agent service because, currently, there's no good solution
// to collect updates received in failed runs and pass them to the last successful run so it can store them to the message store.
if (safeThread.ConversationId is null)
{
throw new NotSupportedException("Streaming resumption is only supported when chat history is stored and managed by the underlying AI service.");
}

// Similarly, streaming resumption is not supported when a context provider is used because, currently, there's no good solution
// to collect updates received in failed runs and pass them to the last successful run so it can notify the context provider of the updates.
if (safeThread.AIContextProvider is not null)
{
throw new NotSupportedException("Using context provider with streaming resumption is not supported.");
}
}

private string GetLoggingAgentName() => this.Name ?? "UnnamedAgent";
#endregion
}
Loading
Loading