Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
6 changes: 6 additions & 0 deletions dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/IdGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public IdGenerator(string? responseId, string? conversationId, int? randomSeed =
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;
}

Expand All @@ -59,6 +60,11 @@ public static IdGenerator From(CreateResponse request)
/// </summary>
public string ConversationId { get; }

/// <summary>
/// Gets a value indicating whether this is a new conversation.
/// </summary>
public bool IsNewConversation { get; }

/// <summary>
/// Generates a new ID.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ internal sealed class AgentInvocationContext(IdGenerator idGenerator, JsonSerial
/// </summary>
public string ConversationId => this.IdGenerator.ConversationId;

/// <summary>
/// Returns true, if conversation is new.
/// </summary>
public bool IsNewConversation => this.IdGenerator.IsNewConversation;

/// <summary>
/// Gets the JSON serializer options.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,13 @@ public async IAsyncEnumerable<StreamingResponseEvent> ExecuteAsync(
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
string agentName = GetAgentName(request)!;
AIAgent agent = this._serviceProvider.GetRequiredKeyedService<AIAgent>(agentName);
string conversationId = context.ConversationId;

var agent = this._serviceProvider.GetRequiredKeyedService<AIAgent>(agentName);
var threadStore = this._serviceProvider.GetKeyedService<AgentThreadStore>(agent.Name);
Comment on lines +86 to +87
Copy link

Copilot AI Nov 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable declaration uses var instead of an explicit type. According to the C# Sample Code Guidelines (CodingGuidelineID: 1000000), you should "Prefer defining variables using types rather than var, to help users understand the types involved."

Consider changing to:

AIAgent agent = this._serviceProvider.GetRequiredKeyedService<AIAgent>(agentName);
AgentThreadStore? threadStore = this._serviceProvider.GetKeyedService<AgentThreadStore>(agent.Name);

Copilot generated this review using guidance from repository custom instructions.

var chatOptions = new ChatOptions
{
ConversationId = request.Conversation?.Id,
Temperature = (float?)request.Temperature,
TopP = (float?)request.TopP,
MaxOutputTokens = request.MaxOutputTokens,
Expand All @@ -90,16 +93,25 @@ public async IAsyncEnumerable<StreamingResponseEvent> ExecuteAsync(
var options = new ChatClientAgentRunOptions(chatOptions);
var messages = new List<ChatMessage>();

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())
{
messages.Add(inputMessage.ToChatMessage());
}

await foreach (var streamingEvent in agent.RunStreamingAsync(messages, options: options, cancellationToken: cancellationToken)
await foreach (var streamingEvent in agent.RunStreamingAsync(messages, thread, options: options, cancellationToken: cancellationToken)
.ToStreamingResponseAsync(request, context, cancellationToken).ConfigureAwait(false))
{
yield return streamingEvent;
}

if (threadStore is not null && thread is not null)
{
await threadStore.SaveThreadAsync(agent, conversationId, thread, cancellationToken).ConfigureAwait(false);
}
}

/// <summary>
Expand Down
Loading