-
Notifications
You must be signed in to change notification settings - Fork 1.2k
.NET: Fix Usage and Annotations not present in AG-UI events (#3752) #4114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
b8381cb
cd00892
a6e82cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -375,6 +375,12 @@ chatResponse.Contents[0] is TextContent && | |
| MessageId = chatResponse.MessageId!, | ||
| Delta = textContent.Text | ||
| }; | ||
|
|
||
| // Emit annotations if present on the text content | ||
| if (textContent.Annotations is { Count: > 0 }) | ||
| { | ||
| yield return CreateAnnotationsCustomEvent(textContent.Annotations, jsonSerializerOptions); | ||
| } | ||
| } | ||
|
|
||
| // Emit tool call events and tool result events | ||
|
|
@@ -463,6 +469,11 @@ chatResponse.Contents[0] is TextContent && | |
| }; | ||
| } | ||
| } | ||
| else if (content is UsageContent usageContent) | ||
| { | ||
| // Emit usage data as a custom event | ||
| yield return CreateUsageCustomEvent(usageContent, jsonSerializerOptions); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -493,4 +504,77 @@ chatResponse.Contents[0] is TextContent && | |
| _ => JsonSerializer.Serialize(functionResultContent.Result, options.GetTypeInfo(functionResultContent.Result.GetType())), | ||
| }; | ||
| } | ||
|
|
||
| private static CustomEvent CreateUsageCustomEvent(UsageContent usageContent, JsonSerializerOptions jsonSerializerOptions) | ||
| { | ||
| using var buffer = new System.IO.MemoryStream(); | ||
| using (var writer = new Utf8JsonWriter(buffer)) | ||
| { | ||
| writer.WriteStartObject(); | ||
| writer.WriteNumber("inputTokenCount", usageContent.Details.InputTokenCount ?? 0); | ||
| writer.WriteNumber("outputTokenCount", usageContent.Details.OutputTokenCount ?? 0); | ||
| writer.WriteNumber("totalTokenCount", usageContent.Details.TotalTokenCount ?? 0); | ||
| writer.WriteEndObject(); | ||
| } | ||
|
|
||
| return new CustomEvent | ||
| { | ||
| Name = "usage", | ||
| Value = JsonSerializer.Deserialize(buffer.ToArray(), jsonSerializerOptions.GetTypeInfo(typeof(JsonElement))) as JsonElement? | ||
| }; | ||
| } | ||
|
|
||
| private static CustomEvent CreateAnnotationsCustomEvent( | ||
| IList<AIAnnotation> annotations, | ||
| JsonSerializerOptions jsonSerializerOptions) | ||
| { | ||
| using var buffer = new System.IO.MemoryStream(); | ||
| using (var writer = new Utf8JsonWriter(buffer)) | ||
| { | ||
| writer.WriteStartArray(); | ||
| foreach (var annotation in annotations) | ||
| { | ||
| writer.WriteStartObject(); | ||
| writer.WriteString("type", annotation.GetType().Name); | ||
|
|
||
| if (annotation is CitationAnnotation citation) | ||
| { | ||
| if (citation.Title is not null) | ||
| { | ||
| writer.WriteString("title", citation.Title); | ||
| } | ||
|
|
||
| if (citation.Url is not null) | ||
| { | ||
| writer.WriteString("url", citation.Url.ToString()); | ||
| } | ||
|
|
||
| if (citation.FileId is not null) | ||
| { | ||
| writer.WriteString("fileId", citation.FileId); | ||
| } | ||
|
|
||
| if (citation.ToolName is not null) | ||
| { | ||
| writer.WriteString("toolName", citation.ToolName); | ||
| } | ||
|
|
||
| if (citation.Snippet is not null) | ||
| { | ||
| writer.WriteString("snippet", citation.Snippet); | ||
| } | ||
| } | ||
|
|
||
| writer.WriteEndObject(); | ||
| } | ||
|
|
||
| writer.WriteEndArray(); | ||
| } | ||
|
|
||
| return new CustomEvent | ||
| { | ||
| Name = "annotations", | ||
| Value = JsonSerializer.Deserialize(buffer.ToArray(), jsonSerializerOptions.GetTypeInfo(typeof(JsonElement))) as JsonElement? | ||
| }; | ||
|
Comment on lines
+574
to
+578
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| #if ASPNETCORE | ||
| namespace Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.Shared; | ||
| #else | ||
| namespace Microsoft.Agents.AI.AGUI.Shared; | ||
| #endif | ||
|
|
||
| internal sealed class CustomEvent : BaseEvent | ||
| { | ||
| public CustomEvent() | ||
| { | ||
| this.Type = AGUIEventTypes.Custom; | ||
| } | ||
|
|
||
| [JsonPropertyName("name")] | ||
| public string Name { get; set; } = string.Empty; | ||
|
|
||
| [JsonPropertyName("value")] | ||
| public JsonElement? Value { get; set; } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
JsonSerializer.Deserialize(... ) as JsonElement?pattern is hard to read and inconsistent with the rest of this file (which uses explicit casts toJsonElement?). Consider usingJsonSerializer.Deserialize<JsonElement>(...)(or an explicit(JsonElement?)cast) to avoid the nullable-asunboxing trick and make it clearer that deserialization is expected to succeed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot open a new pull request to apply changes based on this feedback
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot open a new pull request to apply changes based on this feedback