Files
agent-framework/dotnet/src/Microsoft.Agents.AI.AGUI/AGUIHttpService.cs
T
Javier Calvarro NelsonandGitHub b03a4fb95e .NET: AG-UI support for .NET (#1776)
* Initial plan

* Infrastructure setup

* Plan for minimal client

* Plan update

* Basic agentic chat

* cleanup

* Cleanups

* More cleanups

* Cleanups

* More cleanups

* Test plan

* Sample

* Fix streaming and error handling

* Fix notifications

* Cleanups

* cleanup sample

* Additional tests

* Additional tests

* Run dotnet format

* Remove unnecessary files

* Mark packages as non packable

* Fix build

* Address feedback

* Fix build

* Fix remaining warnings

* Feedback

* Feedback and cleanup

* Cleanup

* Cleanups

* Cleanups

* Cleanups

* Retrieve existing messages from the store to send them along the way and update the sample client

* Run dotnet format

* Add ADR for AG-UI

* Switch to use the SG and use a convention for run ids

* Cleanup MapAGUI API

* Fix formatting

* Fix solution

* Fix solution
2025-11-05 15:51:37 +00:00

53 lines
1.8 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Json;
using System.Net.ServerSentEvents;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Agents.AI.AGUI.Shared;
namespace Microsoft.Agents.AI.AGUI;
internal sealed class AGUIHttpService(HttpClient client, string endpoint)
{
public async IAsyncEnumerable<BaseEvent> PostRunAsync(
RunAgentInput input,
[EnumeratorCancellation] CancellationToken cancellationToken)
{
using HttpRequestMessage request = new(HttpMethod.Post, endpoint)
{
Content = JsonContent.Create(input, AGUIJsonSerializerContext.Default.RunAgentInput)
};
using HttpResponseMessage response = await client.SendAsync(
request,
HttpCompletionOption.ResponseHeadersRead,
cancellationToken).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
#if NET
Stream responseStream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
#else
Stream responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
#endif
var items = SseParser.Create(responseStream, ItemParser).EnumerateAsync(cancellationToken);
await foreach (var sseItem in items.ConfigureAwait(false))
{
yield return sseItem.Data;
}
}
private static BaseEvent ItemParser(string type, ReadOnlySpan<byte> data)
{
return JsonSerializer.Deserialize(data, AGUIJsonSerializerContext.Default.BaseEvent) ??
throw new InvalidOperationException("Failed to deserialize SSE item.");
}
}