Files
agent-framework/dotnet/tests/Microsoft.Agents.AI.AGUI.UnitTests/AGUIChatClientTests.cs
Dmytro Struk 361c47f30f Python: [Feature Branch] Merge from main to Azure AI branch (#2111)
* Do not build DevUI assets during .NET project build (#2010)

* .NET: Add unit tests for declarative executor SetMultipleVariables (#2016)

* Add unit tests for create conversation executor

* Update indentation and comment typo.

* Added unit tests for declarative executor SetMultipleVariablesExecutor

* Updated comments and syntactic sugar

* Python: DevUI: Use metadata.entity_id instead of model field (#1984)

* DevUI: Use metadata.entity_id for agent/workflow name instead of model field

* OpenAI Responses: add explicit request validation

* Review feedback

* .NET: DevUI - Do not automatically add/map OpenAI services/endpoints (#2014)

* Don't add OpenAIResponses as part of Dev UI

You should be able to add and remove Dev UI without impacting your other production endpoints.

* Remove `AddDevUI()` and do not map OpenAI endpoints from `MapDevUI()`

* Fix comment wording

* Revise documentation

---------

Co-authored-by: Daniel Roth <daroth@microsoft.com>

* Python: DevUI: Add OpenAI Responses API proxy support  + HIL for Workflows (#1737)

* DevUI: Add OpenAI Responses API proxy support with enhanced UI features

This commit adds support for proxying requests to OpenAI's Responses API,
allowing DevUI to route conversations to OpenAI models when configured to enable testing.

Backend changes:
- Add OpenAI proxy executor with conversation routing logic
- Enhance event mapper to support OpenAI Responses API format
- Extend server endpoints to handle OpenAI proxy mode
- Update models with OpenAI-specific response types
- Remove emojis from logging and CLI output for cleaner text

Frontend changes:
- Add settings modal with OpenAI proxy configuration UI
- Enhance agent and workflow views with improved state management
- Add new UI components (separator, switch) for settings
- Update debug panel with better event filtering
- Improve message renderers for OpenAI content types
- Update types and API client for OpenAI integration

* update ui, settings modal and workflow input form, add register cleanup hooks.

* add workflow HIL support, user mode, other fixes

* feat(devui): add human-in-the-loop (HIL) support with dynamic response schemas

Implement  HIL workflow support allowing workflows to pause for user input
with dynamically generated JSON schemas based on response handler type hints.

Key Features:
- Automatic response schema extraction from @response_handler decorators
- Dynamic form generation in UI based on Pydantic/dataclass response types
- Checkpoint-based conversation storage for HIL requests/responses
- Resume workflow execution after user provides HIL response

Backend Changes:
- Add extract_response_type_from_executor() to introspect response handlers
- Enrich RequestInfoEvent with response_schema via _enrich_request_info_event_with_response_schema()
- Map RequestInfoEvent to response.input.requested OpenAI event format
- Store HIL responses in conversation history and restore checkpoints

Frontend Changes:
- Add HILInputModal component with SchemaFormRenderer for dynamic forms
- Support Pydantic BaseModel and dataclass response types
- Render enum fields as dropdowns, strings as text/textarea, numbers, booleans, arrays, objects
- Display original request context alongside response form

Testing:
- Add  tests for checkpoint storage (test_checkpoints.py)
- Add schema generation tests for all input types (test_schema_generation.py)
- Validate end-to-end HIL flow with spam workflow sample

This enables workflows to seamlessly pause execution and request structured user input
with type-safe, validated forms generated automatically from response type annotations.

* improve HIL support, improve workflow execution view

* ui updates

* ui updates

* improve HIL for workflows, add auth and view modes

* update workflow

* security improvements , ui fixes

* fix mypy error

* update loading spinner in ui

---------

Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>

* .NET: Remove launchSettings.json from .gitignore in dotnet/samples (#2006)

* Remove launchSettings.json from .gitignore in dotnet/samples

* Update dotnet/samples/GettingStarted/DevUI/DevUI_Step01_BasicUsage/Properties/launchSettings.json

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update dotnet/samples/AGUIClientServer/AGUIServer/Properties/launchSettings.json

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* DevUI: Serialize workflow input as string to maintain conformance with OpenAI Responses format (#2021)

Co-authored-by: Victor Dibia <chuvidi2003@gmail.com>

* Add Microsoft Agent Framework logo to assets (#2007)

* Updated package versions (#2027)

* DevUI: Prevent line breaks within words in the agent view (#2024)

Co-authored-by: Victor Dibia <chuvidi2003@gmail.com>

* .NET [AG-UI]: Adds support for shared state. (#1996)

* Product changes

* Tests

* Dojo project

* Cleanups

* Python: Fix underlying tool choice bug and all for return to previous Handoff subagent (#2037)

* Fix tool_choice override bug and add enable_return_to_previous support

* Add unit test for handoff checkpointing

* Handle tools when we have them

* added missing chatAgent params (#2044)

* .NET: fix ChatCompletions Tools serialization (#2043)

* fix serialization in chat completions on tools

* nit

* .NET: assign AgentCard's URL to mapped-endpoint if not defined explicitly (#2047)

* fix serialization in chat completions on tools

* nit

* write e2e test for agent card resolve + adjust behavior

* nit

* Version 1.0.0-preview.251110.1 (#2048)

* .NET: Remove moved OpenAPI sample and point to SK one. (#1997)

* Remove moved OpenAPI sample and point to SK one.

* Update dotnet/samples/GettingStarted/Agents/README.md

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Bump AWSSDK.Extensions.Bedrock.MEAI from 4.0.4.2 to 4.0.4.6 (#2031)

---
updated-dependencies:
- dependency-name: AWSSDK.Extensions.Bedrock.MEAI
  dependency-version: 4.0.4.6
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* .NET: Separate all memory and rag samples into their own folders (#2000)

* Separate all memory and rag samples into their own folders

* Fix broken link.

* Python: .Net: Dotnet devui compatibility fixes (#2026)

* DevUI: Add OpenAI Responses API proxy support with enhanced UI features

This commit adds support for proxying requests to OpenAI's Responses API,
allowing DevUI to route conversations to OpenAI models when configured to enable testing.

Backend changes:
- Add OpenAI proxy executor with conversation routing logic
- Enhance event mapper to support OpenAI Responses API format
- Extend server endpoints to handle OpenAI proxy mode
- Update models with OpenAI-specific response types
- Remove emojis from logging and CLI output for cleaner text

Frontend changes:
- Add settings modal with OpenAI proxy configuration UI
- Enhance agent and workflow views with improved state management
- Add new UI components (separator, switch) for settings
- Update debug panel with better event filtering
- Improve message renderers for OpenAI content types
- Update types and API client for OpenAI integration

* update ui, settings modal and workflow input form, add register cleanup hooks.

* add workflow HIL support, user mode, other fixes

* feat(devui): add human-in-the-loop (HIL) support with dynamic response schemas

Implement  HIL workflow support allowing workflows to pause for user input
with dynamically generated JSON schemas based on response handler type hints.

Key Features:
- Automatic response schema extraction from @response_handler decorators
- Dynamic form generation in UI based on Pydantic/dataclass response types
- Checkpoint-based conversation storage for HIL requests/responses
- Resume workflow execution after user provides HIL response

Backend Changes:
- Add extract_response_type_from_executor() to introspect response handlers
- Enrich RequestInfoEvent with response_schema via _enrich_request_info_event_with_response_schema()
- Map RequestInfoEvent to response.input.requested OpenAI event format
- Store HIL responses in conversation history and restore checkpoints

Frontend Changes:
- Add HILInputModal component with SchemaFormRenderer for dynamic forms
- Support Pydantic BaseModel and dataclass response types
- Render enum fields as dropdowns, strings as text/textarea, numbers, booleans, arrays, objects
- Display original request context alongside response form

Testing:
- Add  tests for checkpoint storage (test_checkpoints.py)
- Add schema generation tests for all input types (test_schema_generation.py)
- Validate end-to-end HIL flow with spam workflow sample

This enables workflows to seamlessly pause execution and request structured user input
with type-safe, validated forms generated automatically from response type annotations.

* improve HIL support, improve workflow execution view

* ui updates

* ui updates

* improve HIL for workflows, add auth and view modes

* update workflow

* security improvements , ui fixes

* fix mypy error

* update loading spinner in ui

* DevUI: Serialize workflow input as string to maintain conformance with OpenAI Responses format

* Phase 1: Add /meta endpoint and fix workflow event naming for .NET DevUI compatibility

* additional fixes for .NET DevUI workflow visualization item ID tracking

**Problem:**
.NET DevUI was generating different item IDs for ExecutorInvokedEvent and
ExecutorCompletedEvent, causing only the first executor to highlight in the
workflow graph. Long executor names and error messages also broke UI layout.

**Changes:**
- Add ExecutorActionItemResource to match Python DevUI implementation
- Track item IDs per executor using dictionary in AgentRunResponseUpdateExtensions
- Reuse same item ID across invoked/completed/failed events for proper pairing
- Add truncateText() utility to workflow-utils.ts
- Truncate executor names to 35 chars in execution timeline
- Truncate error messages to 150 chars in workflow graph nodes

** Details:**
- ExecutorActionItemResource registered with JSON source generation context
- Dictionary cleaned up after executor completion/failure to prevent memory leaks
- Frontend item tracking by unique item.id supports multiple executor runs
- All changes follow existing codebase patterns and conventions

Tested with review-workflow showing correct executor highlighting and state
transitions for sequential and concurrent executors.

* format fixes, remove cors tests

* remove unecessary attributes

---------

Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>
Co-authored-by: Reuben Bond <reuben.bond@gmail.com>

* DevUI: support having both an agent and a workflow with the same id in discovery (#2023)

* Python: Fix Model ID attribute not showing up in `invoke_agent` span (#2061)

* Best effort to surface the model id to invoke agent span

* Fix tests

* Fix tests

* Version 1.0.0-preview.251107.2 (#2065)

* Version 1.0.0-preview.251110.2 (#2067)

* Update README.md to change Grafana links to Azure portal links for dashboard access (#1983)

* .NET - Enable build & test on branch `feature-foundry-agents` (#2068)

* Tests good, mkay

* Update .github/workflows/dotnet-build-and-test.yml

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Enable feature build pipelines

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com>

* Python: Add concrete AGUIChatClient (#2072)

* Add concrete AGUIChatClient

* Update logging docstrings and conventions

* PR feedback

* Updates to support client-side tool calls

* .NET: Move catalog samples to the HostedAgents folder (#2090)

* move catalog samples to the HostedAgents folder

* move the catalog samples' projects to the HostedAgents folder

* Bump OpenTelemetry.Instrumentation.Runtime from 1.12.0 to 1.13.0 (#1856)

---
updated-dependencies:
- dependency-name: OpenTelemetry.Instrumentation.Runtime
  dependency-version: 1.13.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* .NET: Bump Microsoft.SemanticKernel.Agents.Abstractions from 1.66.0 to 1.67.0 (#1962)

* Bump Microsoft.SemanticKernel.Agents.Abstractions from 1.66.0 to 1.67.0

---
updated-dependencies:
- dependency-name: Microsoft.SemanticKernel.Agents.Abstractions
  dependency-version: 1.67.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* .NET: Bump all Microsoft.SemanticKernel packages from 1.66.* to 1.67.* (#1969)

* Initial plan

* Update all Microsoft.SemanticKernel packages to 1.67.*

Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>

* Remove unrelated changes to package-lock.json and yarn.lock

Co-authored-by: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>
Co-authored-by: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com>

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>
Co-authored-by: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com>

* .NET: fix: WorkflowAsAgent Sample (#1787)

* fix: WorkflowAsAgent Sample

* Also makes ChatForwardingExecutor public

* feat: Expand ChatForwardingExecutor handled types

Make ChatForwardingExecutor match the input types of ChatProtocolExecutor.

* fix: Update for the new AgentRunResponseUpdate merge logic

AIAgent always sends out List<ChatMessage> now.

* Updated (#2076)

* Bump vite in /python/samples/demos/chatkit-integration/frontend (#1918)

Bumps [vite](https://github.com/vitejs/vite/tree/HEAD/packages/vite) from 7.1.9 to 7.1.12.
- [Release notes](https://github.com/vitejs/vite/releases)
- [Changelog](https://github.com/vitejs/vite/blob/v7.1.12/packages/vite/CHANGELOG.md)
- [Commits](https://github.com/vitejs/vite/commits/v7.1.12/packages/vite)

---
updated-dependencies:
- dependency-name: vite
  dependency-version: 7.1.12
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump Roslynator.Analyzers from 4.14.0 to 4.14.1 (#1857)

---
updated-dependencies:
- dependency-name: Roslynator.Analyzers
  dependency-version: 4.14.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump MishaKav/pytest-coverage-comment from 1.1.57 to 1.1.59 (#2034)

Bumps [MishaKav/pytest-coverage-comment](https://github.com/mishakav/pytest-coverage-comment) from 1.1.57 to 1.1.59.
- [Release notes](https://github.com/mishakav/pytest-coverage-comment/releases)
- [Changelog](https://github.com/MishaKav/pytest-coverage-comment/blob/main/CHANGELOG.md)
- [Commits](https://github.com/mishakav/pytest-coverage-comment/compare/v1.1.57...v1.1.59)

---
updated-dependencies:
- dependency-name: MishaKav/pytest-coverage-comment
  dependency-version: 1.1.59
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>

* Python: Handle agent user input request in AgentExecutor (#2022)

* Handle agent user input request in AgentExecutor

* fix test

* Address comments

* Fix tests

* Fix tests

* Address comments

* Address comments

* Python: OpenAI Responses Image Generation Stream Support, Sample and Unit Tests (#1853)

* support for image gen streaming

* small fixes

* fixes

* added comment

* Python: Fix MCP Tool Parameter Descriptions Not Propagated to LLMs (#1978)

* mcp tool description fix

* small fix

* .NET: Allow extending agent run options via additional properties (#1872)

* Allow extending agent run options via additional properties

This mirrors the M.E.AI model in ChatOptions.AdditionalProperties which is very useful when building functionality pipelines.

Fixes https://github.com/microsoft/agent-framework/issues/1815

* Expand XML documentation

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Add AdditionalProperties tests to AgentRunOptions

Co-authored-by: kzu <169707+kzu@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: kzu <169707+kzu@users.noreply.github.com>

* Python: Use the last entry in the task history to avoid empty responses (#2101)

* Use the last entry in the task history to avoid empty responses

* History only contains Messages

* Updated package versions (#2104)

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Reuben Bond <203839+ReubenBond@users.noreply.github.com>
Co-authored-by: Peter Ibekwe <109177538+peibekwe@users.noreply.github.com>
Co-authored-by: Jeff Handley <jeffhandley@users.noreply.github.com>
Co-authored-by: Daniel Roth <daroth@microsoft.com>
Co-authored-by: Victor Dibia <chuvidi2003@gmail.com>
Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Shawn Henry <sphenry@gmail.com>
Co-authored-by: Javier Calvarro Nelson <jacalvar@microsoft.com>
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Co-authored-by: Eduard van Valkenburg <eavanvalkenburg@users.noreply.github.com>
Co-authored-by: Korolev Dmitry <deagle.gross@gmail.com>
Co-authored-by: westey <164392973+westey-m@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Reuben Bond <reuben.bond@gmail.com>
Co-authored-by: Tao Chen <taochen@microsoft.com>
Co-authored-by: wuweng <wuweng@microsoft.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>
Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com>
Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Jacob Alber <jaalber@microsoft.com>
Co-authored-by: Giles Odigwe <79032838+giles17@users.noreply.github.com>
Co-authored-by: Daniel Cazzulino <daniel@cazzulino.com>
Co-authored-by: kzu <169707+kzu@users.noreply.github.com>
2025-11-11 23:12:09 -08:00

1740 lines
77 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Agents.AI.AGUI.Shared;
using Microsoft.Extensions.AI;
namespace Microsoft.Agents.AI.AGUI.UnitTests;
public sealed class AGUIAgentTests
{
[Fact]
public async Task RunAsync_AggregatesStreamingUpdates_ReturnsCompleteMessagesAsync()
{
// Arrange
using HttpClient httpClient = this.CreateMockHttpClient(new BaseEvent[]
{
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new TextMessageStartEvent { MessageId = "msg1", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg1", Delta = "Hello" },
new TextMessageContentEvent { MessageId = "msg1", Delta = " World" },
new TextMessageEndEvent { MessageId = "msg1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
});
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
AIAgent agent = chatClient.CreateAIAgent(instructions: null, name: "agent1", description: "Test agent", tools: []);
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act
AgentRunResponse response = await agent.RunAsync(messages);
// Assert
Assert.NotNull(response);
Assert.NotEmpty(response.Messages);
ChatMessage message = response.Messages.First();
Assert.Equal(ChatRole.Assistant, message.Role);
Assert.Equal("Hello World", message.Text);
}
[Fact]
public async Task RunAsync_WithEmptyUpdateStream_ContainsOnlyMetadataMessagesAsync()
{
// Arrange
using HttpClient httpClient = this.CreateMockHttpClient(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
AIAgent agent = chatClient.CreateAIAgent(instructions: null, name: "agent1", description: "Test agent", tools: []);
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act
AgentRunResponse response = await agent.RunAsync(messages);
// Assert
Assert.NotNull(response);
// RunStarted and RunFinished events are aggregated into messages by ToChatResponse()
Assert.NotEmpty(response.Messages);
Assert.All(response.Messages, m => Assert.Equal(ChatRole.Assistant, m.Role));
}
[Fact]
public async Task RunAsync_WithNullMessages_ThrowsArgumentNullExceptionAsync()
{
// Arrange
using HttpClient httpClient = new();
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
AIAgent agent = chatClient.CreateAIAgent(instructions: "Test agent", name: "agent1");
// Act & Assert
await Assert.ThrowsAsync<ArgumentNullException>(() => agent.RunAsync(messages: null!));
}
[Fact]
public async Task RunAsync_WithNullThread_CreatesNewThreadAsync()
{
// Arrange
using HttpClient httpClient = this.CreateMockHttpClient(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
AIAgent agent = chatClient.CreateAIAgent(instructions: "Test agent", name: "agent1");
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act
AgentRunResponse response = await agent.RunAsync(messages, thread: null);
// Assert
Assert.NotNull(response);
}
[Fact]
public async Task RunStreamingAsync_YieldsAllEvents_FromServerStreamAsync()
{
// Arrange
using HttpClient httpClient = this.CreateMockHttpClient(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new TextMessageStartEvent { MessageId = "msg1", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg1", Delta = "Hello" },
new TextMessageEndEvent { MessageId = "msg1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
AIAgent agent = chatClient.CreateAIAgent(instructions: "Test agent", name: "agent1");
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act
List<AgentRunResponseUpdate> updates = [];
await foreach (AgentRunResponseUpdate update in agent.RunStreamingAsync(messages))
{
// Consume the stream
updates.Add(update);
}
// Assert
Assert.NotEmpty(updates);
Assert.Contains(updates, u => u.ResponseId != null); // RunStarted sets ResponseId
Assert.Contains(updates, u => u.Contents.Any(c => c is TextContent));
Assert.Contains(updates, u => u.Contents.Count == 0 && u.ResponseId != null); // RunFinished has no text content
}
[Fact]
public async Task RunStreamingAsync_WithNullMessages_ThrowsArgumentNullExceptionAsync()
{
// Arrange
using HttpClient httpClient = new();
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
AIAgent agent = chatClient.CreateAIAgent(instructions: "Test agent", name: "agent1");
// Act & Assert
await Assert.ThrowsAsync<ArgumentNullException>(async () =>
{
await foreach (var _ in agent.RunStreamingAsync(messages: null!))
{
// Intentionally empty - consuming stream to trigger exception
}
});
}
[Fact]
public async Task RunStreamingAsync_WithNullThread_CreatesNewThreadAsync()
{
// Arrange
using HttpClient httpClient = this.CreateMockHttpClient(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
AIAgent agent = chatClient.CreateAIAgent(instructions: "Test agent", name: "agent1");
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act
List<AgentRunResponseUpdate> updates = [];
await foreach (AgentRunResponseUpdate update in agent.RunStreamingAsync(messages, thread: null))
{
// Consume the stream
updates.Add(update);
}
// Assert
Assert.NotEmpty(updates);
}
[Fact]
public async Task RunStreamingAsync_GeneratesUniqueRunId_ForEachInvocationAsync()
{
// Arrange
var handler = new TestDelegatingHandler();
handler.AddResponseWithCapture(new BaseEvent[]
{
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
});
handler.AddResponseWithCapture(new BaseEvent[]
{
new RunStartedEvent { ThreadId = "thread1", RunId = "run2" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run2" }
});
using HttpClient httpClient = new(handler);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
AIAgent agent = chatClient.CreateAIAgent(instructions: null, name: "agent1", description: "Test agent", tools: []);
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act
await foreach (var _ in agent.RunStreamingAsync(messages))
{
// Consume the stream
}
await foreach (var _ in agent.RunStreamingAsync(messages))
{
// Consume the stream
}
// Assert
Assert.Equal(2, handler.CapturedRunIds.Count);
Assert.NotEqual(handler.CapturedRunIds[0], handler.CapturedRunIds[1]);
}
[Fact]
public async Task RunStreamingAsync_ReturnsStreamingUpdates_AfterCompletionAsync()
{
// Arrange
using HttpClient httpClient = this.CreateMockHttpClient(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new TextMessageStartEvent { MessageId = "msg1", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg1", Delta = "Hello" },
new TextMessageEndEvent { MessageId = "msg1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
AIAgent agent = chatClient.CreateAIAgent(instructions: null, name: "agent1", description: "Test agent", tools: []);
AgentThread thread = agent.GetNewThread();
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Hello")];
// Act
List<AgentRunResponseUpdate> updates = [];
await foreach (var update in agent.RunStreamingAsync(messages, thread))
{
updates.Add(update);
}
// Assert - Verify streaming updates were received
Assert.NotEmpty(updates);
Assert.Contains(updates, u => u.Text == "Hello");
}
[Fact]
public void DeserializeThread_WithValidState_ReturnsChatClientAgentThread()
{
// Arrange
using var httpClient = new HttpClient();
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
AIAgent agent = chatClient.CreateAIAgent(instructions: null, name: "agent1", description: "Test agent", tools: []);
AgentThread originalThread = agent.GetNewThread();
JsonElement serialized = originalThread.Serialize();
// Act
AgentThread deserialized = agent.DeserializeThread(serialized);
// Assert
Assert.NotNull(deserialized);
Assert.IsType<ChatClientAgentThread>(deserialized);
}
private HttpClient CreateMockHttpClient(BaseEvent[] events)
{
var handler = new TestDelegatingHandler();
handler.AddResponse(events);
return new HttpClient(handler);
}
[Fact]
public async Task RunStreamingAsync_InvokesTools_WhenFunctionCallsReturnedAsync()
{
// Arrange
bool toolInvoked = false;
AIFunction testTool = AIFunctionFactory.Create(
(string location) =>
{
toolInvoked = true;
return $"Weather in {location}: Sunny, 72°F";
},
"GetWeather",
"Gets the current weather for a location");
using HttpClient httpClient = this.CreateMockHttpClientForToolCalls(
firstResponse:
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new ToolCallStartEvent { ToolCallId = "call_1", ToolCallName = "GetWeather", ParentMessageId = "msg1" },
new ToolCallArgsEvent { ToolCallId = "call_1", Delta = "{\"location\":\"Seattle\"}" },
new ToolCallEndEvent { ToolCallId = "call_1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
],
secondResponse:
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run2" },
new TextMessageStartEvent { MessageId = "msg2", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg2", Delta = "The weather is nice!" },
new TextMessageEndEvent { MessageId = "msg2" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run2" }
]);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
AIAgent agent = chatClient.CreateAIAgent(instructions: null, name: "agent1", description: "Test agent", tools: [testTool]);
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "What's the weather?")];
// Act
List<AgentRunResponseUpdate> allUpdates = [];
await foreach (AgentRunResponseUpdate update in agent.RunStreamingAsync(messages))
{
allUpdates.Add(update);
}
// Assert
Assert.True(toolInvoked, "Tool should have been invoked");
Assert.NotEmpty(allUpdates);
// Should have updates from both the tool call and the final response
Assert.Contains(allUpdates, u => u.Contents.Any(c => c is FunctionCallContent));
Assert.Contains(allUpdates, u => u.Contents.Any(c => c is TextContent));
}
[Fact]
public async Task RunStreamingAsync_DoesNotInvokeTools_WhenSomeToolsNotAvailableAsync()
{
// Arrange
bool tool1Invoked = false;
AIFunction tool1 = AIFunctionFactory.Create(
() => { tool1Invoked = true; return "Result1"; },
"Tool1");
// FunctionInvokingChatClient makes two calls: first gets tool calls, second returns final response
// When not all tools are available, it invokes the ones that ARE available
var handler = new TestDelegatingHandler();
handler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new ToolCallStartEvent { ToolCallId = "call_1", ToolCallName = "Tool1", ParentMessageId = "msg1" },
new ToolCallArgsEvent { ToolCallId = "call_1", Delta = "{}" },
new ToolCallEndEvent { ToolCallId = "call_1" },
new ToolCallStartEvent { ToolCallId = "call_2", ToolCallName = "Tool2", ParentMessageId = "msg1" },
new ToolCallArgsEvent { ToolCallId = "call_2", Delta = "{}" },
new ToolCallEndEvent { ToolCallId = "call_2" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
handler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run2" },
new TextMessageStartEvent { MessageId = "msg2", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg2", Delta = "Response" },
new TextMessageEndEvent { MessageId = "msg2" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run2" }
]);
using HttpClient httpClient = new(handler);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
AIAgent agent = chatClient.CreateAIAgent(instructions: null, name: "agent1", description: "Test agent", tools: [tool1]); // Only tool1, not tool2
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act
List<AgentRunResponseUpdate> allUpdates = [];
await foreach (AgentRunResponseUpdate update in agent.RunStreamingAsync(messages))
{
allUpdates.Add(update);
}
// Assert
// FunctionInvokingChatClient invokes Tool1 since it's available, even though Tool2 is not
Assert.True(tool1Invoked, "Tool1 should be invoked even though Tool2 is not available");
// Should have tool call results for Tool1 and an error result for Tool2
Assert.Contains(allUpdates, u => u.Contents.Any(c => c is FunctionResultContent frc && frc.CallId == "call_1"));
}
[Fact]
public async Task RunStreamingAsync_HandlesToolInvocationErrors_GracefullyAsync()
{
// Arrange
AIFunction faultyTool = AIFunctionFactory.Create(
() =>
{
throw new InvalidOperationException("Tool failed!");
#pragma warning disable CS0162 // Unreachable code detected
return string.Empty;
#pragma warning restore CS0162 // Unreachable code detected
},
"FaultyTool");
using HttpClient httpClient = this.CreateMockHttpClientForToolCalls(
firstResponse:
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new ToolCallStartEvent { ToolCallId = "call_1", ToolCallName = "FaultyTool", ParentMessageId = "msg1" },
new ToolCallArgsEvent { ToolCallId = "call_1", Delta = "{}" },
new ToolCallEndEvent { ToolCallId = "call_1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
],
secondResponse:
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run2" },
new TextMessageStartEvent { MessageId = "msg2", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg2", Delta = "I encountered an error." },
new TextMessageEndEvent { MessageId = "msg2" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run2" }
]);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
AIAgent agent = chatClient.CreateAIAgent(instructions: null, name: "agent1", description: "Test agent", tools: [faultyTool]);
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act
List<AgentRunResponseUpdate> allUpdates = [];
await foreach (AgentRunResponseUpdate update in agent.RunStreamingAsync(messages))
{
allUpdates.Add(update);
}
// Assert - should complete without throwing
Assert.NotEmpty(allUpdates);
}
[Fact]
public async Task RunStreamingAsync_InvokesMultipleTools_InSingleTurnAsync()
{
// Arrange
int tool1CallCount = 0;
int tool2CallCount = 0;
AIFunction tool1 = AIFunctionFactory.Create(() => { tool1CallCount++; return "Result1"; }, "Tool1");
AIFunction tool2 = AIFunctionFactory.Create(() => { tool2CallCount++; return "Result2"; }, "Tool2");
using HttpClient httpClient = this.CreateMockHttpClientForToolCalls(
firstResponse:
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new ToolCallStartEvent { ToolCallId = "call_1", ToolCallName = "Tool1", ParentMessageId = "msg1" },
new ToolCallArgsEvent { ToolCallId = "call_1", Delta = "{}" },
new ToolCallEndEvent { ToolCallId = "call_1" },
new ToolCallStartEvent { ToolCallId = "call_2", ToolCallName = "Tool2", ParentMessageId = "msg1" },
new ToolCallArgsEvent { ToolCallId = "call_2", Delta = "{}" },
new ToolCallEndEvent { ToolCallId = "call_2" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
],
secondResponse:
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run2" },
new TextMessageStartEvent { MessageId = "msg2", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg2", Delta = "Done" },
new TextMessageEndEvent { MessageId = "msg2" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run2" }
]);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
AIAgent agent = chatClient.CreateAIAgent(instructions: null, name: "agent1", description: "Test agent", tools: [tool1, tool2]);
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act
await foreach (var _ in agent.RunStreamingAsync(messages))
{
}
// Assert
Assert.Equal(1, tool1CallCount);
Assert.Equal(1, tool2CallCount);
}
[Fact]
public async Task RunStreamingAsync_UpdatesThreadWithToolMessages_AfterCompletionAsync()
{
// Arrange
AIFunction testTool = AIFunctionFactory.Create(() => "Result", "TestTool");
using HttpClient httpClient = this.CreateMockHttpClientForToolCalls(
firstResponse:
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new ToolCallStartEvent { ToolCallId = "call_1", ToolCallName = "TestTool", ParentMessageId = "msg1" },
new ToolCallArgsEvent { ToolCallId = "call_1", Delta = "{}" },
new ToolCallEndEvent { ToolCallId = "call_1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
],
secondResponse:
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run2" },
new TextMessageStartEvent { MessageId = "msg2", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg2", Delta = "Complete" },
new TextMessageEndEvent { MessageId = "msg2" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run2" }
]);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
AIAgent agent = chatClient.CreateAIAgent(instructions: null, name: "agent1", description: "Test agent", tools: [testTool]);
AgentThread thread = agent.GetNewThread();
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act
List<AgentRunResponseUpdate> updates = [];
await foreach (var update in agent.RunStreamingAsync(messages, thread))
{
updates.Add(update);
}
// Assert - Verify we received updates including tool calls
Assert.NotEmpty(updates);
Assert.Contains(updates, u => u.Contents.Any(c => c is FunctionCallContent));
Assert.Contains(updates, u => u.Contents.Any(c => c is FunctionResultContent));
Assert.Contains(updates, u => u.Text == "Complete");
}
private HttpClient CreateMockHttpClientForToolCalls(BaseEvent[] firstResponse, BaseEvent[] secondResponse)
{
var handler = new TestDelegatingHandler();
handler.AddResponse(firstResponse);
handler.AddResponse(secondResponse);
return new HttpClient(handler);
}
[Fact]
public async Task GetStreamingResponseAsync_WrapsServerFunctionCalls_InServerFunctionCallContentAsync()
{
// Arrange - Server returns a function call for a tool not in the client tool set
using HttpClient httpClient = this.CreateMockHttpClient(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new ToolCallStartEvent { ToolCallId = "call_1", ToolCallName = "ServerTool", ParentMessageId = "msg1" },
new ToolCallArgsEvent { ToolCallId = "call_1", Delta = "{\"arg\":\"value\"}" },
new ToolCallEndEvent { ToolCallId = "call_1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
// No tools provided - any function call from server is a "server function"
var options = new ChatOptions();
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act
List<ChatResponseUpdate> updates = [];
await foreach (var update in chatClient.GetStreamingResponseAsync(messages, options))
{
updates.Add(update);
}
// Assert - Server function call should be presented as FunctionCallContent (unwrapped)
Assert.Contains(updates, u => u.Contents.Any(c => c is FunctionCallContent fcc && fcc.Name == "ServerTool"));
// Should NOT contain ServerFunctionCallContent (it's internal and unwrapped before yielding)
Assert.DoesNotContain(updates, u => u.Contents.Any(c => c.GetType().Name == "ServerFunctionCallContent"));
}
[Fact]
public async Task GetStreamingResponseAsync_DoesNotWrapClientFunctionCalls_WhenToolInClientSetAsync()
{
// Arrange
AIFunction clientTool = AIFunctionFactory.Create(() => "Result", "ClientTool");
var handler = new TestDelegatingHandler();
handler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new ToolCallStartEvent { ToolCallId = "call_1", ToolCallName = "ClientTool", ParentMessageId = "msg1" },
new ToolCallArgsEvent { ToolCallId = "call_1", Delta = "{}" },
new ToolCallEndEvent { ToolCallId = "call_1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
handler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run2" },
new TextMessageStartEvent { MessageId = "msg2", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg2", Delta = "Done" },
new TextMessageEndEvent { MessageId = "msg2" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run2" }
]);
using HttpClient httpClient = new(handler);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
var options = new ChatOptions { Tools = [clientTool] };
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act
List<ChatResponseUpdate> updates = [];
await foreach (var update in chatClient.GetStreamingResponseAsync(messages, options))
{
updates.Add(update);
}
// Assert - Should have function call and result (FunctionInvokingChatClient processed it)
Assert.Contains(updates, u => u.Contents.Any(c => c is FunctionCallContent fcc && fcc.Name == "ClientTool"));
Assert.Contains(updates, u => u.Contents.Any(c => c is FunctionResultContent frc && frc.CallId == "call_1"));
}
[Fact]
public async Task GetStreamingResponseAsync_HandlesMixedClientAndServerFunctions_InSameResponseAsync()
{
// Arrange
AIFunction clientTool = AIFunctionFactory.Create(() => "ClientResult", "ClientTool");
var handler = new TestDelegatingHandler();
handler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new ToolCallStartEvent { ToolCallId = "call_1", ToolCallName = "ClientTool", ParentMessageId = "msg1" },
new ToolCallArgsEvent { ToolCallId = "call_1", Delta = "{}" },
new ToolCallEndEvent { ToolCallId = "call_1" },
new ToolCallStartEvent { ToolCallId = "call_2", ToolCallName = "ServerTool", ParentMessageId = "msg1" },
new ToolCallArgsEvent { ToolCallId = "call_2", Delta = "{}" },
new ToolCallEndEvent { ToolCallId = "call_2" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
handler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run2" },
new TextMessageStartEvent { MessageId = "msg2", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg2", Delta = "Done" },
new TextMessageEndEvent { MessageId = "msg2" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run2" }
]);
using HttpClient httpClient = new(handler);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
var options = new ChatOptions { Tools = [clientTool] };
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act
List<ChatResponseUpdate> updates = [];
await foreach (var update in chatClient.GetStreamingResponseAsync(messages, options))
{
updates.Add(update);
}
// Assert - Should have both client and server function calls
Assert.Contains(updates, u => u.Contents.Any(c => c is FunctionCallContent fcc && fcc.Name == "ClientTool"));
Assert.Contains(updates, u => u.Contents.Any(c => c is FunctionCallContent fcc && fcc.Name == "ServerTool"));
// Client tool should have result
Assert.Contains(updates, u => u.Contents.Any(c => c is FunctionResultContent frc && frc.CallId == "call_1"));
}
[Fact]
public async Task GetStreamingResponseAsync_PreservesConversationId_AcrossMultipleTurnsAsync()
{
// Arrange
var handler = new TestDelegatingHandler();
handler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new TextMessageStartEvent { MessageId = "msg1", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg1", Delta = "First" },
new TextMessageEndEvent { MessageId = "msg1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
handler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run2" },
new TextMessageStartEvent { MessageId = "msg2", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg2", Delta = "Second" },
new TextMessageEndEvent { MessageId = "msg2" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run2" }
]);
using HttpClient httpClient = new(handler);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
var options = new ChatOptions { ConversationId = "my-conversation-123" };
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act - First turn
List<ChatResponseUpdate> updates1 = [];
await foreach (var update in chatClient.GetStreamingResponseAsync(messages, options))
{
updates1.Add(update);
}
// Second turn with same conversation ID
List<ChatResponseUpdate> updates2 = [];
await foreach (var update in chatClient.GetStreamingResponseAsync(messages, options))
{
updates2.Add(update);
}
// Assert - Both turns should preserve the conversation ID
Assert.All(updates1, u => Assert.Equal("my-conversation-123", u.ConversationId));
Assert.All(updates2, u => Assert.Equal("my-conversation-123", u.ConversationId));
}
[Fact]
public async Task GetStreamingResponseAsync_ExtractsThreadId_FromServerResponseAsync()
{
// Arrange
using HttpClient httpClient = this.CreateMockHttpClient(
[
new RunStartedEvent { ThreadId = "server-thread-456", RunId = "run1" },
new TextMessageStartEvent { MessageId = "msg1", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg1", Delta = "Hello" },
new TextMessageEndEvent { MessageId = "msg1" },
new RunFinishedEvent { ThreadId = "server-thread-456", RunId = "run1" }
]);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
// No conversation ID provided
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act
List<ChatResponseUpdate> updates = [];
await foreach (var update in chatClient.GetStreamingResponseAsync(messages, null))
{
updates.Add(update);
}
// Assert - Should use thread ID from server
Assert.All(updates, u => Assert.Equal("server-thread-456", u.ConversationId));
}
[Fact]
public async Task GetStreamingResponseAsync_GeneratesThreadId_WhenNoneProvidedAsync()
{
// Arrange
using HttpClient httpClient = this.CreateMockHttpClient(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new TextMessageStartEvent { MessageId = "msg1", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg1", Delta = "Hello" },
new TextMessageEndEvent { MessageId = "msg1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act
List<ChatResponseUpdate> updates = [];
await foreach (var update in chatClient.GetStreamingResponseAsync(messages, null))
{
updates.Add(update);
}
// Assert - Should have a conversation ID (either from server or generated)
Assert.All(updates, u => Assert.NotNull(u.ConversationId));
Assert.All(updates, u => Assert.NotEmpty(u.ConversationId!));
}
[Fact]
public async Task GetStreamingResponseAsync_RemovesThreadIdFromFunctionCallProperties_BeforeYieldingAsync()
{
// Arrange
AIFunction clientTool = AIFunctionFactory.Create(() => "Result", "ClientTool");
var handler = new TestDelegatingHandler();
handler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new ToolCallStartEvent { ToolCallId = "call_1", ToolCallName = "ClientTool", ParentMessageId = "msg1" },
new ToolCallArgsEvent { ToolCallId = "call_1", Delta = "{}" },
new ToolCallEndEvent { ToolCallId = "call_1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
handler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run2" },
new TextMessageStartEvent { MessageId = "msg2", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg2", Delta = "Done" },
new TextMessageEndEvent { MessageId = "msg2" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run2" }
]);
using HttpClient httpClient = new(handler);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
var options = new ChatOptions { Tools = [clientTool] };
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act
List<ChatResponseUpdate> updates = [];
await foreach (var update in chatClient.GetStreamingResponseAsync(messages, options))
{
updates.Add(update);
}
// Assert - Function call content should not have agui_thread_id in additional properties
var functionCallUpdate = updates.FirstOrDefault(u => u.Contents.Any(c => c is FunctionCallContent));
Assert.NotNull(functionCallUpdate);
var fcc = functionCallUpdate.Contents.OfType<FunctionCallContent>().First();
Assert.True(fcc.AdditionalProperties?.ContainsKey("agui_thread_id") != true);
}
[Fact]
public async Task GetResponseAsync_PreservesConversationId_ThroughStreamingPathAsync()
{
// Arrange
using HttpClient httpClient = this.CreateMockHttpClient(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new TextMessageStartEvent { MessageId = "msg1", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg1", Delta = "Hello" },
new TextMessageEndEvent { MessageId = "msg1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
var options = new ChatOptions { ConversationId = "my-conversation-456" };
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act
ChatResponse response = await chatClient.GetResponseAsync(messages, options);
// Assert
Assert.Equal("my-conversation-456", response.ConversationId);
}
[Fact]
public async Task GetStreamingResponseAsync_UsesServerThreadId_WhenDifferentFromClientAsync()
{
// Arrange - Server returns different thread ID
using HttpClient httpClient = this.CreateMockHttpClient(
[
new RunStartedEvent { ThreadId = "server-generated-thread", RunId = "run1" },
new TextMessageStartEvent { MessageId = "msg1", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg1", Delta = "Hello" },
new TextMessageEndEvent { MessageId = "msg1" },
new RunFinishedEvent { ThreadId = "server-generated-thread", RunId = "run1" }
]);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
var options = new ChatOptions { ConversationId = "client-thread-123" };
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act
List<ChatResponseUpdate> updates = [];
await foreach (var update in chatClient.GetStreamingResponseAsync(messages, options))
{
updates.Add(update);
}
// Assert - Should use client's conversation ID (we provided it explicitly)
Assert.All(updates, u => Assert.Equal("client-thread-123", u.ConversationId));
}
[Fact]
public async Task GetStreamingResponseAsync_FullConversationFlow_WithMixedFunctionsAsync()
{
// Arrange
AIFunction clientTool = AIFunctionFactory.Create(() => "ClientResult", "ClientTool");
var handler = new TestDelegatingHandler();
// First response: client function call (FunctionInvokingChatClient will handle this)
handler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new ToolCallStartEvent { ToolCallId = "call_client", ToolCallName = "ClientTool", ParentMessageId = "msg1" },
new ToolCallArgsEvent { ToolCallId = "call_client", Delta = "{}" },
new ToolCallEndEvent { ToolCallId = "call_client" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
// Second response: after client function execution, return final text
handler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run2" },
new TextMessageStartEvent { MessageId = "msg2", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg2", Delta = "Complete" },
new TextMessageEndEvent { MessageId = "msg2" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run2" }
]);
using HttpClient httpClient = new(handler);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
var options = new ChatOptions { Tools = [clientTool] };
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act
List<ChatResponseUpdate> updates = [];
string? conversationId = null;
await foreach (var update in chatClient.GetStreamingResponseAsync(messages, options))
{
updates.Add(update);
conversationId ??= update.ConversationId;
}
// Assert
// Should have client function call and result
Assert.Contains(updates, u => u.Contents.Any(c => c is FunctionCallContent fcc && fcc.Name == "ClientTool"));
Assert.Contains(updates, u => u.Contents.Any(c => c is FunctionResultContent frc && frc.CallId == "call_client"));
// Should have final text response
Assert.Contains(updates, u => u.Contents.Any(c => c is TextContent));
// All updates should have consistent conversation ID
Assert.NotNull(conversationId);
Assert.All(updates, u => Assert.Equal(conversationId, u.ConversationId));
}
[Fact]
public async Task GetStreamingResponseAsync_ExtractsThreadIdFromFunctionCall_OnSubsequentTurnsAsync()
{
// Arrange
AIFunction clientTool = AIFunctionFactory.Create(() => "Result", "ClientTool");
var handler = new TestDelegatingHandler();
// First turn: client function call
handler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new ToolCallStartEvent { ToolCallId = "call_1", ToolCallName = "ClientTool", ParentMessageId = "msg1" },
new ToolCallArgsEvent { ToolCallId = "call_1", Delta = "{}" },
new ToolCallEndEvent { ToolCallId = "call_1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
// FunctionInvokingChatClient automatically calls again after function execution
handler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run2" },
new TextMessageStartEvent { MessageId = "msg2", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg2", Delta = "First done" },
new TextMessageEndEvent { MessageId = "msg2" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run2" }
]);
// Third turn: user makes another request with conversation history
handler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run3" },
new TextMessageStartEvent { MessageId = "msg3", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg3", Delta = "Second done" },
new TextMessageEndEvent { MessageId = "msg3" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run3" }
]);
using HttpClient httpClient = new(handler);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
var options = new ChatOptions { Tools = [clientTool] };
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act - First turn
List<ChatMessage> conversation = new(messages);
string? conversationId = null;
await foreach (var update in chatClient.GetStreamingResponseAsync(conversation, options))
{
conversationId ??= update.ConversationId;
// Collect all updates to build the conversation history
foreach (var content in update.Contents)
{
if (content is FunctionCallContent fcc)
{
conversation.Add(new ChatMessage(ChatRole.Assistant, [fcc]));
}
else if (content is FunctionResultContent frc)
{
conversation.Add(new ChatMessage(ChatRole.Tool, [frc]));
}
else if (content is TextContent tc)
{
var existingAssistant = conversation.LastOrDefault(m => m.Role == ChatRole.Assistant && m.Contents.Any(c => c is TextContent));
if (existingAssistant == null)
{
conversation.Add(new ChatMessage(ChatRole.Assistant, [tc]));
}
}
}
}
// Act - Second turn with conversation history including function call
// The thread ID should be extracted from the function call in the conversation history
options.ConversationId = conversationId;
List<ChatResponseUpdate> secondTurnUpdates = [];
await foreach (var update in chatClient.GetStreamingResponseAsync(conversation, options))
{
secondTurnUpdates.Add(update);
}
// Assert - Second turn should maintain the same conversation ID
Assert.NotNull(conversationId);
Assert.All(secondTurnUpdates, u => Assert.Equal(conversationId, u.ConversationId));
Assert.Contains(secondTurnUpdates, u => u.Contents.Any(c => c is TextContent));
}
[Fact]
public async Task GetStreamingResponseAsync_MaintainsConsistentThreadId_AcrossMultipleTurnsAsync()
{
// Arrange
var handler = new TestDelegatingHandler();
// Turn 1
handler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new TextMessageStartEvent { MessageId = "msg1", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg1", Delta = "Response 1" },
new TextMessageEndEvent { MessageId = "msg1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
// Turn 2
handler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run2" },
new TextMessageStartEvent { MessageId = "msg2", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg2", Delta = "Response 2" },
new TextMessageEndEvent { MessageId = "msg2" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run2" }
]);
// Turn 3
handler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run3" },
new TextMessageStartEvent { MessageId = "msg3", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg3", Delta = "Response 3" },
new TextMessageEndEvent { MessageId = "msg3" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run3" }
]);
using HttpClient httpClient = new(handler);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
var options = new ChatOptions { ConversationId = "my-conversation" };
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act - Execute 3 turns
string? conversationId = null;
for (int i = 0; i < 3; i++)
{
await foreach (var update in chatClient.GetStreamingResponseAsync(messages, options))
{
conversationId ??= update.ConversationId;
Assert.Equal("my-conversation", update.ConversationId);
}
}
// Assert
Assert.Equal("my-conversation", conversationId);
}
[Fact]
public async Task GetStreamingResponseAsync_HandlesEmptyThreadId_GracefullyAsync()
{
// Arrange - Server returns empty thread ID
using HttpClient httpClient = this.CreateMockHttpClient(
[
new RunStartedEvent { ThreadId = string.Empty, RunId = "run1" },
new TextMessageStartEvent { MessageId = "msg1", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg1", Delta = "Hello" },
new TextMessageEndEvent { MessageId = "msg1" },
new RunFinishedEvent { ThreadId = string.Empty, RunId = "run1" }
]);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act
List<ChatResponseUpdate> updates = [];
await foreach (var update in chatClient.GetStreamingResponseAsync(messages, null))
{
updates.Add(update);
}
// Assert - Should generate a conversation ID even with empty server thread ID
Assert.NotEmpty(updates);
Assert.All(updates, u => Assert.NotNull(u.ConversationId));
Assert.All(updates, u => Assert.NotEmpty(u.ConversationId!));
}
[Fact]
public async Task GetStreamingResponseAsync_AdaptsToServerThreadIdChange_MidConversationAsync()
{
// Arrange
var handler = new TestDelegatingHandler();
// First turn: server returns thread-A
handler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread-A", RunId = "run1" },
new TextMessageStartEvent { MessageId = "msg1", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg1", Delta = "First" },
new TextMessageEndEvent { MessageId = "msg1" },
new RunFinishedEvent { ThreadId = "thread-A", RunId = "run1" }
]);
// Second turn: provide thread-A but server returns thread-B
handler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread-B", RunId = "run2" },
new TextMessageStartEvent { MessageId = "msg2", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg2", Delta = "Second" },
new TextMessageEndEvent { MessageId = "msg2" },
new RunFinishedEvent { ThreadId = "thread-B", RunId = "run2" }
]);
using HttpClient httpClient = new(handler);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act - First turn
string? firstConversationId = null;
await foreach (var update in chatClient.GetStreamingResponseAsync(messages, null))
{
firstConversationId ??= update.ConversationId;
}
// Second turn - provide the conversation ID from first turn
var options = new ChatOptions { ConversationId = firstConversationId };
string? secondConversationId = null;
await foreach (var update in chatClient.GetStreamingResponseAsync(messages, options))
{
secondConversationId ??= update.ConversationId;
}
// Assert - Should use client-provided conversation ID, not server's changed ID
Assert.Equal("thread-A", firstConversationId);
Assert.Equal("thread-A", secondConversationId); // Client overrides server's thread-B
}
[Fact]
public async Task GetStreamingResponseAsync_PresentsServerFunctionResults_AsRegularFunctionResultsAsync()
{
// Arrange - Server function (not in client tool set)
using HttpClient httpClient = this.CreateMockHttpClient(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new ToolCallStartEvent { ToolCallId = "call_1", ToolCallName = "ServerTool", ParentMessageId = "msg1" },
new ToolCallArgsEvent { ToolCallId = "call_1", Delta = "{\"arg\":\"value\"}" },
new ToolCallEndEvent { ToolCallId = "call_1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act
List<ChatResponseUpdate> updates = [];
await foreach (var update in chatClient.GetStreamingResponseAsync(messages, null))
{
updates.Add(update);
}
// Assert - Server function should be presented as FunctionCallContent (unwrapped from ServerFunctionCallContent)
Assert.Contains(updates, u => u.Contents.Any(c => c is FunctionCallContent fcc && fcc.Name == "ServerTool"));
// Verify it's NOT a ServerFunctionCallContent (internal type should be unwrapped)
Assert.All(updates, u => Assert.DoesNotContain(u.Contents, c => c.GetType().Name == "ServerFunctionCallContent"));
}
[Fact]
public async Task GetStreamingResponseAsync_HandlesMultipleServerFunctions_InSequenceAsync()
{
// Arrange
var handler = new TestDelegatingHandler();
// Turn 1: Server function 1
handler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new ToolCallStartEvent { ToolCallId = "call_1", ToolCallName = "ServerTool1", ParentMessageId = "msg1" },
new ToolCallArgsEvent { ToolCallId = "call_1", Delta = "{}" },
new ToolCallEndEvent { ToolCallId = "call_1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
// Turn 2: Server function 2
handler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run2" },
new ToolCallStartEvent { ToolCallId = "call_2", ToolCallName = "ServerTool2", ParentMessageId = "msg2" },
new ToolCallArgsEvent { ToolCallId = "call_2", Delta = "{}" },
new ToolCallEndEvent { ToolCallId = "call_2" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run2" }
]);
// Turn 3: Final response
handler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run3" },
new TextMessageStartEvent { MessageId = "msg3", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg3", Delta = "Complete" },
new TextMessageEndEvent { MessageId = "msg3" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run3" }
]);
using HttpClient httpClient = new(handler);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
var options = new ChatOptions { ConversationId = "conv1" };
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act - Execute all 3 turns
List<ChatResponseUpdate> allUpdates = [];
for (int i = 0; i < 3; i++)
{
await foreach (var update in chatClient.GetStreamingResponseAsync(messages, options))
{
allUpdates.Add(update);
}
}
// Assert
Assert.Contains(allUpdates, u => u.Contents.Any(c => c is FunctionCallContent fcc && fcc.Name == "ServerTool1"));
Assert.Contains(allUpdates, u => u.Contents.Any(c => c is FunctionCallContent fcc && fcc.Name == "ServerTool2"));
Assert.Contains(allUpdates, u => u.Contents.Any(c => c is TextContent));
Assert.All(allUpdates, u => Assert.Equal("conv1", u.ConversationId));
}
[Fact]
public async Task GetStreamingResponseAsync_MaintainsThreadIdConsistency_WithOnlyServerFunctionsAsync()
{
// Arrange - Full conversation with only server functions
var handler = new TestDelegatingHandler();
// Turn 1: Server function
handler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new ToolCallStartEvent { ToolCallId = "call_1", ToolCallName = "ServerTool", ParentMessageId = "msg1" },
new ToolCallArgsEvent { ToolCallId = "call_1", Delta = "{}" },
new ToolCallEndEvent { ToolCallId = "call_1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
// Turn 2: Final response
handler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run2" },
new TextMessageStartEvent { MessageId = "msg2", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg2", Delta = "Done" },
new TextMessageEndEvent { MessageId = "msg2" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run2" }
]);
using HttpClient httpClient = new(handler);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act
string? conversationId = null;
List<ChatResponseUpdate> allUpdates = [];
for (int i = 0; i < 2; i++)
{
await foreach (var update in chatClient.GetStreamingResponseAsync(messages, null))
{
conversationId ??= update.ConversationId;
allUpdates.Add(update);
}
}
// Assert - Thread ID should be consistent without client function invocations
Assert.NotNull(conversationId);
Assert.All(allUpdates, u => Assert.Equal(conversationId, u.ConversationId));
Assert.Contains(allUpdates, u => u.Contents.Any(c => c is FunctionCallContent));
Assert.Contains(allUpdates, u => u.Contents.Any(c => c is TextContent));
}
[Fact]
public async Task GetStreamingResponseAsync_StoresConversationIdInAdditionalProperties_WithoutMutatingOptionsAsync()
{
// Arrange
using HttpClient httpClient = this.CreateMockHttpClient(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new TextMessageStartEvent { MessageId = "msg1", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg1", Delta = "Hello" },
new TextMessageEndEvent { MessageId = "msg1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
var options = new ChatOptions { ConversationId = "my-conversation-123" };
var originalConversationId = options.ConversationId;
var originalAdditionalProperties = options.AdditionalProperties;
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act
await foreach (var update in chatClient.GetStreamingResponseAsync(messages, options))
{
// Just consume the stream
}
// Assert - Original options should not be mutated
Assert.Equal(originalConversationId, options.ConversationId);
Assert.Equal(originalAdditionalProperties, options.AdditionalProperties);
}
[Fact]
public async Task GetStreamingResponseAsync_EnsuresConversationIdIsNull_ForInnerClientAsync()
{
// Arrange - Use a custom handler to capture what's sent to the inner layer
var captureHandler = new CapturingTestDelegatingHandler();
captureHandler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new TextMessageStartEvent { MessageId = "msg1", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg1", Delta = "Hello" },
new TextMessageEndEvent { MessageId = "msg1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
using HttpClient httpClient = new(captureHandler);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
var options = new ChatOptions { ConversationId = "my-conversation-123" };
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act
await foreach (var _ in chatClient.GetStreamingResponseAsync(messages, options))
{
// Just consume the stream
}
// Assert - The inner handler should see the full message history being sent
// This is implicitly tested by the fact that all messages are sent in the request
// AG-UI requirement: full history on every turn (which happens when ConversationId is null for FunctionInvokingChatClient)
Assert.True(captureHandler.RequestWasMade);
}
[Fact]
public async Task GetStreamingResponseAsync_ExtractsStateFromDataContent_AndRemovesStateMessageAsync()
{
// Arrange
var stateData = new { counter = 42, status = "active" };
string stateJson = JsonSerializer.Serialize(stateData);
byte[] stateBytes = System.Text.Encoding.UTF8.GetBytes(stateJson);
var dataContent = new DataContent(stateBytes, "application/json");
var captureHandler = new StateCapturingTestDelegatingHandler();
captureHandler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new TextMessageStartEvent { MessageId = "msg1", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg1", Delta = "Response" },
new TextMessageEndEvent { MessageId = "msg1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
using HttpClient httpClient = new(captureHandler);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
List<ChatMessage> messages =
[
new ChatMessage(ChatRole.User, "Hello"),
new ChatMessage(ChatRole.System, [dataContent])
];
// Act
await foreach (var _ in chatClient.GetStreamingResponseAsync(messages, null))
{
// Just consume the stream
}
// Assert
Assert.True(captureHandler.RequestWasMade);
Assert.NotNull(captureHandler.CapturedState);
Assert.Equal(42, captureHandler.CapturedState.Value.GetProperty("counter").GetInt32());
Assert.Equal("active", captureHandler.CapturedState.Value.GetProperty("status").GetString());
// Verify state message was removed - only user message should be in the request
Assert.Equal(1, captureHandler.CapturedMessageCount);
}
[Fact]
public async Task GetStreamingResponseAsync_WithNoStateDataContent_SendsEmptyStateAsync()
{
// Arrange
var captureHandler = new StateCapturingTestDelegatingHandler();
captureHandler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new TextMessageStartEvent { MessageId = "msg1", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg1", Delta = "Response" },
new TextMessageEndEvent { MessageId = "msg1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
using HttpClient httpClient = new(captureHandler);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Hello")];
// Act
await foreach (var _ in chatClient.GetStreamingResponseAsync(messages, null))
{
// Just consume the stream
}
// Assert
Assert.True(captureHandler.RequestWasMade);
Assert.Null(captureHandler.CapturedState);
}
[Fact]
public async Task GetStreamingResponseAsync_WithMalformedStateJson_ThrowsInvalidOperationExceptionAsync()
{
// Arrange
byte[] invalidJson = System.Text.Encoding.UTF8.GetBytes("{invalid json");
var dataContent = new DataContent(invalidJson, "application/json");
using HttpClient httpClient = this.CreateMockHttpClient([]);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
List<ChatMessage> messages =
[
new ChatMessage(ChatRole.User, "Hello"),
new ChatMessage(ChatRole.System, [dataContent])
];
// Act & Assert
InvalidOperationException ex = await Assert.ThrowsAsync<InvalidOperationException>(async () =>
{
await foreach (var _ in chatClient.GetStreamingResponseAsync(messages, null))
{
// Just consume the stream
}
});
Assert.Contains("Failed to deserialize state JSON", ex.Message);
}
[Fact]
public async Task GetStreamingResponseAsync_WithEmptyStateObject_SendsEmptyObjectAsync()
{
// Arrange
var emptyState = new { };
string stateJson = JsonSerializer.Serialize(emptyState);
byte[] stateBytes = System.Text.Encoding.UTF8.GetBytes(stateJson);
var dataContent = new DataContent(stateBytes, "application/json");
var captureHandler = new StateCapturingTestDelegatingHandler();
captureHandler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
using HttpClient httpClient = new(captureHandler);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
List<ChatMessage> messages =
[
new ChatMessage(ChatRole.User, "Hello"),
new ChatMessage(ChatRole.System, [dataContent])
];
// Act
await foreach (var _ in chatClient.GetStreamingResponseAsync(messages, null))
{
// Just consume the stream
}
// Assert
Assert.True(captureHandler.RequestWasMade);
Assert.NotNull(captureHandler.CapturedState);
Assert.Equal(JsonValueKind.Object, captureHandler.CapturedState.Value.ValueKind);
}
[Fact]
public async Task GetStreamingResponseAsync_OnlyProcessesDataContentFromLastMessage_IgnoresEarlierOnesAsync()
{
// Arrange
var oldState = new { counter = 10 };
string oldStateJson = JsonSerializer.Serialize(oldState);
byte[] oldStateBytes = System.Text.Encoding.UTF8.GetBytes(oldStateJson);
var oldDataContent = new DataContent(oldStateBytes, "application/json");
var newState = new { counter = 20 };
string newStateJson = JsonSerializer.Serialize(newState);
byte[] newStateBytes = System.Text.Encoding.UTF8.GetBytes(newStateJson);
var newDataContent = new DataContent(newStateBytes, "application/json");
var captureHandler = new StateCapturingTestDelegatingHandler();
captureHandler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
using HttpClient httpClient = new(captureHandler);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
List<ChatMessage> messages =
[
new ChatMessage(ChatRole.User, "First message"),
new ChatMessage(ChatRole.System, [oldDataContent]),
new ChatMessage(ChatRole.User, "Second message"),
new ChatMessage(ChatRole.System, [newDataContent])
];
// Act
await foreach (var _ in chatClient.GetStreamingResponseAsync(messages, null))
{
// Just consume the stream
}
// Assert
Assert.True(captureHandler.RequestWasMade);
Assert.NotNull(captureHandler.CapturedState);
// Should use the new state from the last message
Assert.Equal(20, captureHandler.CapturedState.Value.GetProperty("counter").GetInt32());
// Should have removed only the last state message
Assert.Equal(3, captureHandler.CapturedMessageCount);
}
[Fact]
public async Task GetStreamingResponseAsync_WithNonJsonMediaType_IgnoresDataContentAsync()
{
// Arrange
byte[] imageData = System.Text.Encoding.UTF8.GetBytes("fake image data");
var dataContent = new DataContent(imageData, "image/png");
var captureHandler = new StateCapturingTestDelegatingHandler();
captureHandler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
using HttpClient httpClient = new(captureHandler);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
List<ChatMessage> messages =
[
new ChatMessage(ChatRole.User, [new TextContent("Hello"), dataContent])
];
// Act
await foreach (var _ in chatClient.GetStreamingResponseAsync(messages, null))
{
// Just consume the stream
}
// Assert
Assert.True(captureHandler.RequestWasMade);
Assert.Null(captureHandler.CapturedState);
// Message should not be removed since it's not state
Assert.Equal(1, captureHandler.CapturedMessageCount);
}
[Fact]
public async Task GetStreamingResponseAsync_RoundTripState_PreservesJsonStructureAsync()
{
// Arrange - Server returns state snapshot
var returnedState = new { counter = 100, nested = new { value = "test" } };
JsonElement stateSnapshot = JsonSerializer.SerializeToElement(returnedState);
var captureHandler = new StateCapturingTestDelegatingHandler();
captureHandler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new StateSnapshotEvent { Snapshot = stateSnapshot },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
captureHandler.AddResponse(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run2" },
new TextMessageStartEvent { MessageId = "msg1", Role = AGUIRoles.Assistant },
new TextMessageContentEvent { MessageId = "msg1", Delta = "Done" },
new TextMessageEndEvent { MessageId = "msg1" },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run2" }
]);
using HttpClient httpClient = new(captureHandler);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Hello")];
// Act - First turn: receive state
DataContent? receivedStateContent = null;
await foreach (var update in chatClient.GetStreamingResponseAsync(messages, null))
{
if (update.Contents.Any(c => c is DataContent dc && dc.MediaType == "application/json"))
{
receivedStateContent = (DataContent)update.Contents.First(c => c is DataContent);
}
}
// Second turn: send the received state back
Assert.NotNull(receivedStateContent);
messages.Add(new ChatMessage(ChatRole.System, [receivedStateContent]));
await foreach (var _ in chatClient.GetStreamingResponseAsync(messages, null))
{
// Just consume the stream
}
// Assert - Verify the round-tripped state
Assert.NotNull(captureHandler.CapturedState);
Assert.Equal(100, captureHandler.CapturedState.Value.GetProperty("counter").GetInt32());
Assert.Equal("test", captureHandler.CapturedState.Value.GetProperty("nested").GetProperty("value").GetString());
}
[Fact]
public async Task GetStreamingResponseAsync_ReceivesStateSnapshot_AsDataContentWithAdditionalPropertiesAsync()
{
// Arrange
var state = new { sessionId = "abc123", step = 5 };
JsonElement stateSnapshot = JsonSerializer.SerializeToElement(state);
using HttpClient httpClient = this.CreateMockHttpClient(
[
new RunStartedEvent { ThreadId = "thread1", RunId = "run1" },
new StateSnapshotEvent { Snapshot = stateSnapshot },
new RunFinishedEvent { ThreadId = "thread1", RunId = "run1" }
]);
var chatClient = new AGUIChatClient(httpClient, "http://localhost/agent", null, AGUIJsonSerializerContext.Default.Options);
List<ChatMessage> messages = [new ChatMessage(ChatRole.User, "Test")];
// Act
List<ChatResponseUpdate> updates = [];
await foreach (var update in chatClient.GetStreamingResponseAsync(messages, null))
{
updates.Add(update);
}
// Assert
ChatResponseUpdate stateUpdate = updates.First(u => u.Contents.Any(c => c is DataContent));
Assert.NotNull(stateUpdate.AdditionalProperties);
Assert.True((bool)stateUpdate.AdditionalProperties!["is_state_snapshot"]!);
DataContent dataContent = (DataContent)stateUpdate.Contents[0];
Assert.Equal("application/json", dataContent.MediaType);
string jsonText = System.Text.Encoding.UTF8.GetString(dataContent.Data.ToArray());
JsonElement deserializedState = JsonSerializer.Deserialize<JsonElement>(jsonText);
Assert.Equal("abc123", deserializedState.GetProperty("sessionId").GetString());
Assert.Equal(5, deserializedState.GetProperty("step").GetInt32());
}
}
internal sealed class TestDelegatingHandler : DelegatingHandler
{
private readonly Queue<Func<HttpRequestMessage, Task<HttpResponseMessage>>> _responseFactories = new();
private readonly List<string> _capturedRunIds = new();
public IReadOnlyList<string> CapturedRunIds => this._capturedRunIds;
public void AddResponse(BaseEvent[] events)
{
this._responseFactories.Enqueue(_ => Task.FromResult(CreateResponse(events)));
}
public void AddResponseWithCapture(BaseEvent[] events)
{
this._responseFactories.Enqueue(async request =>
{
await this.CaptureRunIdAsync(request);
return CreateResponse(events);
});
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (this._responseFactories.Count == 0)
{
// Log request count for debugging
throw new InvalidOperationException($"No more responses configured for TestDelegatingHandler. Total requests made: {this._capturedRunIds.Count}");
}
var factory = this._responseFactories.Dequeue();
return await factory(request);
}
private static HttpResponseMessage CreateResponse(BaseEvent[] events)
{
string sseContent = string.Join("", events.Select(e =>
$"data: {JsonSerializer.Serialize(e, AGUIJsonSerializerContext.Default.BaseEvent)}\n\n"));
return new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(sseContent)
};
}
private async Task CaptureRunIdAsync(HttpRequestMessage request)
{
string requestBody = await request.Content!.ReadAsStringAsync().ConfigureAwait(false);
RunAgentInput? input = JsonSerializer.Deserialize(requestBody, AGUIJsonSerializerContext.Default.RunAgentInput);
if (input != null)
{
this._capturedRunIds.Add(input.RunId);
}
}
}
internal sealed class CapturingTestDelegatingHandler : DelegatingHandler
{
private readonly Queue<Func<HttpRequestMessage, Task<HttpResponseMessage>>> _responseFactories = new();
public bool RequestWasMade { get; private set; }
public void AddResponse(BaseEvent[] events)
{
this._responseFactories.Enqueue(_ => Task.FromResult(CreateResponse(events)));
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
this.RequestWasMade = true;
if (this._responseFactories.Count == 0)
{
throw new InvalidOperationException("No more responses configured for CapturingTestDelegatingHandler.");
}
var factory = this._responseFactories.Dequeue();
return await factory(request);
}
private static HttpResponseMessage CreateResponse(BaseEvent[] events)
{
string sseContent = string.Join("", events.Select(e =>
$"data: {JsonSerializer.Serialize(e, AGUIJsonSerializerContext.Default.BaseEvent)}\n\n"));
return new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(sseContent)
};
}
}
internal sealed class StateCapturingTestDelegatingHandler : DelegatingHandler
{
private readonly Queue<Func<HttpRequestMessage, Task<HttpResponseMessage>>> _responseFactories = new();
public bool RequestWasMade { get; private set; }
public JsonElement? CapturedState { get; private set; }
public int CapturedMessageCount { get; private set; }
public void AddResponse(BaseEvent[] events)
{
this._responseFactories.Enqueue(_ => Task.FromResult(CreateResponse(events)));
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
this.RequestWasMade = true;
// Capture the state and message count from the request
#if NET472 || NETSTANDARD2_0
string requestBody = await request.Content!.ReadAsStringAsync().ConfigureAwait(false);
#else
string requestBody = await request.Content!.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
#endif
RunAgentInput? input = JsonSerializer.Deserialize(requestBody, AGUIJsonSerializerContext.Default.RunAgentInput);
if (input != null)
{
if (input.State.ValueKind != JsonValueKind.Undefined && input.State.ValueKind != JsonValueKind.Null)
{
this.CapturedState = input.State;
}
this.CapturedMessageCount = input.Messages.Count();
}
if (this._responseFactories.Count == 0)
{
throw new InvalidOperationException("No more responses configured for StateCapturingTestDelegatingHandler.");
}
var factory = this._responseFactories.Dequeue();
return await factory(request);
}
private static HttpResponseMessage CreateResponse(BaseEvent[] events)
{
string sseContent = string.Join("", events.Select(e =>
$"data: {JsonSerializer.Serialize(e, AGUIJsonSerializerContext.Default.BaseEvent)}\n\n"));
return new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(sseContent)
};
}
}