-
Notifications
You must be signed in to change notification settings - Fork 1.3k
.NET: Fix AG-UI multimodal user message handling #4761
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
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 |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| 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 AGUIBinaryInputContent : AGUIInputContent | ||
| { | ||
| public AGUIBinaryInputContent() | ||
| { | ||
| this.Type = "binary"; | ||
| } | ||
|
|
||
| [JsonPropertyName("mimeType")] | ||
| [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] | ||
| public string? MimeType { get; set; } | ||
|
|
||
| [JsonPropertyName("id")] | ||
| public string? Id { get; set; } | ||
|
|
||
| [JsonPropertyName("url")] | ||
| public string? Url { get; set; } | ||
|
|
||
| [JsonPropertyName("data")] | ||
| public string? Data { get; set; } | ||
|
|
||
| [JsonPropertyName("filename")] | ||
| public string? Filename { get; set; } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System.Text.Json.Serialization; | ||
|
|
||
| #if ASPNETCORE | ||
| namespace Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.Shared; | ||
| #else | ||
| namespace Microsoft.Agents.AI.AGUI.Shared; | ||
| #endif | ||
|
|
||
| [JsonConverter(typeof(AGUIInputContentJsonConverter))] | ||
| internal abstract class AGUIInputContent | ||
| { | ||
| [JsonPropertyName("type")] | ||
| public string Type { get; set; } = string.Empty; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System; | ||
| 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 AGUIInputContentJsonConverter : JsonConverter<AGUIInputContent> | ||
| { | ||
| private const string TypeDiscriminatorPropertyName = "type"; | ||
|
|
||
| public override bool CanConvert(Type typeToConvert) => | ||
| typeof(AGUIInputContent).IsAssignableFrom(typeToConvert); | ||
|
|
||
| public override AGUIInputContent Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| { | ||
| var jsonElementTypeInfo = options.GetTypeInfo(typeof(JsonElement)); | ||
| JsonElement jsonElement = (JsonElement)JsonSerializer.Deserialize(ref reader, jsonElementTypeInfo)!; | ||
|
|
||
| if (!jsonElement.TryGetProperty(TypeDiscriminatorPropertyName, out JsonElement discriminatorElement)) | ||
| { | ||
| throw new JsonException($"Missing required property '{TypeDiscriminatorPropertyName}' for AGUIInputContent deserialization"); | ||
| } | ||
|
|
||
| string? discriminator = discriminatorElement.GetString(); | ||
|
|
||
| AGUIInputContent? result = discriminator switch | ||
| { | ||
| "text" => jsonElement.Deserialize(options.GetTypeInfo(typeof(AGUITextInputContent))) as AGUITextInputContent, | ||
| "binary" => DeserializeBinaryInputContent(jsonElement, options), | ||
| _ => throw new JsonException($"Unknown AGUIInputContent type discriminator: '{discriminator}'") | ||
| }; | ||
|
Comment on lines
+30
to
+37
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leaving this one open for now. I agree case-insensitive discriminator matching would improve tolerance for non-canonical client payloads, but I treated it as a compatibility enhancement rather than a correctness fix for #3729. The current implementation emits canonical lowercase |
||
|
|
||
| if (result is null) | ||
| { | ||
| throw new JsonException($"Failed to deserialize AGUIInputContent with type discriminator: '{discriminator}'"); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| public override void Write(Utf8JsonWriter writer, AGUIInputContent value, JsonSerializerOptions options) | ||
| { | ||
| switch (value) | ||
| { | ||
| case AGUITextInputContent text: | ||
| JsonSerializer.Serialize(writer, text, options.GetTypeInfo(typeof(AGUITextInputContent))); | ||
| break; | ||
| case AGUIBinaryInputContent binary: | ||
| JsonSerializer.Serialize(writer, binary, options.GetTypeInfo(typeof(AGUIBinaryInputContent))); | ||
| break; | ||
| default: | ||
| throw new JsonException($"Unknown AGUIInputContent type: {value.GetType().Name}"); | ||
| } | ||
| } | ||
|
|
||
| private static AGUIBinaryInputContent? DeserializeBinaryInputContent(JsonElement jsonElement, JsonSerializerOptions options) | ||
| { | ||
| AGUIBinaryInputContent? binaryContent = jsonElement.Deserialize(options.GetTypeInfo(typeof(AGUIBinaryInputContent))) as AGUIBinaryInputContent; | ||
| if (binaryContent is null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| if (string.IsNullOrEmpty(binaryContent.Id) && | ||
| string.IsNullOrEmpty(binaryContent.Url) && | ||
| string.IsNullOrEmpty(binaryContent.Data)) | ||
| { | ||
| throw new JsonException("Binary input content must provide at least one of 'id', 'url', or 'data'."); | ||
| } | ||
|
Comment on lines
+70
to
+75
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leaving this one open for now as well. I agree that validating exactly one of |
||
|
|
||
| return binaryContent; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| 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 AGUITextInputContent : AGUIInputContent | ||
| { | ||
| public AGUITextInputContent() | ||
| { | ||
| this.Type = "text"; | ||
| } | ||
|
|
||
| [JsonPropertyName("text")] | ||
| public string Text { get; set; } = string.Empty; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.