diff --git a/dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/IdGenerator.cs b/dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/IdGenerator.cs index bd35fa8308..cc03d52887 100644 --- a/dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/IdGenerator.cs +++ b/dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/IdGenerator.cs @@ -34,6 +34,7 @@ internal sealed partial class IdGenerator this._random = randomSeed.HasValue ? new Random(randomSeed.Value) : null; this.ResponseId = responseId ?? NewId("resp", random: this._random); this.ConversationId = conversationId ?? NewId("conv", random: this._random); + this.IsNewConversation = conversationId is null; this._partitionId = GetPartitionIdOrDefault(this.ConversationId) ?? string.Empty; } @@ -59,6 +60,11 @@ internal sealed partial class IdGenerator /// public string ConversationId { get; } + /// + /// + /// + public bool IsNewConversation { get; } + /// /// Generates a new ID. /// diff --git a/dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/Responses/AgentInvocationContext.cs b/dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/Responses/AgentInvocationContext.cs index f21c2e84e9..f90450f514 100644 --- a/dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/Responses/AgentInvocationContext.cs +++ b/dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/Responses/AgentInvocationContext.cs @@ -26,6 +26,8 @@ internal sealed class AgentInvocationContext(IdGenerator idGenerator, JsonSerial /// public string ConversationId => this.IdGenerator.ConversationId; + public bool IsNewConversation => this.IdGenerator.IsNewConversation; + /// /// Gets the JSON serializer options. /// diff --git a/dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/Responses/HostedAgentResponseExecutor.cs b/dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/Responses/HostedAgentResponseExecutor.cs index e777bb6e16..78fa89c9ab 100644 --- a/dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/Responses/HostedAgentResponseExecutor.cs +++ b/dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/Responses/HostedAgentResponseExecutor.cs @@ -77,14 +77,13 @@ internal sealed class HostedAgentResponseExecutor : IResponseExecutor [EnumeratorCancellation] CancellationToken cancellationToken = default) { string agentName = GetAgentName(request)!; - var conversationId = request.Conversation?.Id; + string conversationId = context.ConversationId; var agent = this._serviceProvider.GetRequiredKeyedService(agentName); var threadStore = this._serviceProvider.GetKeyedService(agent.Name); var chatOptions = new ChatOptions { - ConversationId = conversationId, Temperature = (float?)request.Temperature, TopP = (float?)request.TopP, MaxOutputTokens = request.MaxOutputTokens, @@ -94,11 +93,9 @@ internal sealed class HostedAgentResponseExecutor : IResponseExecutor var options = new ChatClientAgentRunOptions(chatOptions); var messages = new List(); - AgentThread? thread = default; - if (conversationId is not null && threadStore is not null) - { - thread = await threadStore.GetThreadAsync(agent, conversationId, cancellationToken).ConfigureAwait(false); - } + AgentThread thread = !context.IsNewConversation && threadStore is not null + ? await threadStore.GetThreadAsync(agent, conversationId, cancellationToken).ConfigureAwait(false) + : agent.GetNewThread(); foreach (var inputMessage in request.Input.GetInputMessages()) { @@ -111,7 +108,7 @@ internal sealed class HostedAgentResponseExecutor : IResponseExecutor yield return streamingEvent; } - if (conversationId is not null && threadStore is not null && thread is not null) + if (threadStore is not null && thread is not null) { await threadStore.SaveThreadAsync(agent, conversationId, thread, cancellationToken).ConfigureAwait(false); }