Repository navigation
[Blazor] Add Components.AI streaming chat - #68323
Conversation
Add Microsoft.AspNetCore.Components.AI, a Blazor library that turns an IChatClient response stream into content blocks the UI can render while the model is still producing them. The engine is protocol- and provider-neutral: it consumes Microsoft.Extensions.AI and nothing else, so any chat client can drive it. - Blocks: ContentBlock carries id, role, and lifecycle state, and notifies subscribers through a disposable subscription; RichContentBlock accumulates the text fragments of one model message. - Pipeline: BlockMappingPipeline gives blocks that are still active first refusal on each update, so a streaming message keeps accumulating instead of emitting a block per token, and offers whatever is left to inactive handlers. ContentBlockHandler<TState> lets consumers claim content before the built-in text fallback. - Engine: UIAgent streams a message and keeps the history for the next turn. AgentContext owns the conversation: turns, status, error contract, retry, and cancellation, plus the notifications the components subscribe to. - Components: AgentBoundary cascades the conversation, MessageList renders turns and re-renders as blocks change, MessageInput sends messages and disables itself while streaming, ChatPage composes them, and BlockRenderer<TBlock> overrides the markup for a block type. MessageInput registers its status subscription once instead of on every parameter set, which otherwise accumulates handlers and retains the component on each parent re-render. Microsoft.Extensions.AI is not mirrored to the dnceng feeds yet, so a subtree-scoped NuGet.config adds nuget.org for src/Components/AI only. It can be removed once the package is available from the shared feeds. Behavior: changed — Components.AI streams plain-text chat into blocks. State: builds; the dojo applications use it in the next commit and the tests follow in the one after. Review hint: read the block lifecycle and the two-phase pipeline first; the components are thin renderers over the notifications AgentContext raises. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 323b0ef8-7905-4041-8388-a08586c0bd34
Add the two dojo test assets that prove Components.AI works over the real AG-UI wire, kept as separate processes so the boundary under test is a genuine one. - AGUIDojoApi is an ASP.NET Core API modelled on the AGUIDojoServer sample from ag-ui-protocol/ag-ui#1963. MapDojoEndpoint takes a RunAgentInput, turns it into a chat request with ToChatRequestContext, streams the model response, and writes it back with AsAGUIEventStreamAsync over Server-Sent Events. Only the /agentic_chat endpoint exists so far; the remaining dojo scenarios arrive with the product behavior they need. - DojoClient is a Blazor application whose IChatClient is AGUI.Client's AGUIChatClient pointed at that API, so the Agentic Chat page drives Components.AI through HTTP POST plus SSE rather than an in-process client. The API resolves its model client from DI so browser tests can replace it, and it only builds a live OpenAI-compatible client when an endpoint or key is configured. Otherwise it answers from a local scripted client, which keeps running the dojo from reaching a paid service or picking up ambient credentials. AGUI.Client, AGUI.Server, and the AG-UI packages they compile against are pinned to 0.0.5. The UI takes AGUI.Client alone and gets AGUI.Abstractions and AGUI.Formatting transitively. Behavior: changed — a dedicated dojo UI crosses the real AG-UI HTTP/SSE boundary. State: builds; the browser tests land in the next commit. Review hint: compare AGUIEndpoint with the #1963 sample, then check that nothing in DojoClient talks to a model directly. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 323b0ef8-7905-4041-8388-a08586c0bd34
Add the product unit tests and the browser tests for the Agentic Chat scenario. Unit tests cover the block subscription contract, paragraph splitting, the two-phase pipeline (accumulation, completion, custom handler precedence), the agent's history across turns, and the conversation status transitions including the error, retry, and cancellation paths. Component tests render the message list, the input, and the chat page against the test renderer. DojoClient.E2E.Tests launches both dojo applications through the Components. Testing harness: the API first, then the UI pointed at it, so the browser only ever talks to DojoClient. The API's model client is replaced with a recorded one; the UI keeps its AGUIChatClient because the AG-UI serialization, transport, and SSE parsing are part of the behavior under test. The recorded client stops at named checkpoints and waits on a test-controlled gate, which is what makes "the text is still streaming" assertable rather than timing dependent. Gates are released over HTTP against the API, because DojoClient calls it server to server and no browser session cookie reaches it, and the keys are namespaced by the message a test types so runs stay isolated. AgenticChatScenarioTests covers incremental streaming, completion, and a second turn. Behavior: preserved. State: incomplete — the recordings these tests replay land in the next commit. Review hint: check that no override touches DojoClient's IChatClient. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 323b0ef8-7905-4041-8388-a08586c0bd34
Add the baselines the new tests replay. - TextStreaming_SingleTurn and TextStreaming_MultiTurn are captured provider responses, so the engine is exercised against the update shapes a live model produces rather than hand-written ones. - AgenticChat is the dojo script the API replays in the browser tests. It is split into the checkpoints those tests stop at, so the streamed and the completed states are both assertable. No credentials, endpoints, or user data appear in any of them. Behavior: preserved. State: complete — the Agentic Chat unit and browser tests now run end to end. Review hint: generated evidence; check scenario identity and the absence of sensitive data rather than every serialized token. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 323b0ef8-7905-4041-8388-a08586c0bd34
The shared dotnet-public feed already serves the Components.AI and AG-UI packages, so the subtree-specific nuget.org source is unnecessary and violates repository dependency policy. Behavior: preserved State: complete Review hint: deletion only; restore succeeds against the repository NuGet.config Co-authored-by: Copilot App <[email protected]> Copilot-Session: 323b0ef8-7905-4041-8388-a08586c0bd34
There was a problem hiding this comment.
Pull request overview
Adds a new Components.AI slice to the Blazor Components area: a protocol-neutral streaming chat engine plus Blazor UI primitives built on Microsoft.Extensions.AI (IChatClient), along with dedicated dojo API/UI test assets and deterministic E2E coverage over real HTTP/SSE.
Changes:
- Introduces
Microsoft.AspNetCore.Components.AI(engine, block-mapping pipeline, and UI components) plus packaged CSS and PublicAPI entries. - Adds AG-UI dojo test assets (
AGUIDojoApi,DojoClient) to validate the canonical streaming scenario over HTTP + SSE. - Adds unit tests and Playwright E2E tests with recorded/replayed model outputs for deterministic validation.
Reviewed changes
Copilot reviewed 96 out of 96 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Components/Components.slnf | Adds Components.AI projects + dojo test assets to the Components solution filter. |
| src/Components/AI/testassets/DojoClient/wwwroot/app.css | Adds basic dojo UI styles for the test asset client. |
| src/Components/AI/testassets/DojoClient/Properties/launchSettings.json | Adds local launch profile + API URL env var for the dojo UI. |
| src/Components/AI/testassets/DojoClient/Program.cs | Configures the dojo UI to call the dojo API over HTTP/SSE via IChatClient. |
| src/Components/AI/testassets/DojoClient/DojoScenarios.cs | Defines scenario endpoint constants for dojo API routes. |
| src/Components/AI/testassets/DojoClient/DojoClient.csproj | Adds the dojo UI project referencing AG-UI client + Components.AI. |
| src/Components/AI/testassets/DojoClient/Components/Scenarios/AgenticChat/AgenticChatScenario.razor | Implements the Agentic Chat scenario page in the dojo UI. |
| src/Components/AI/testassets/DojoClient/Components/Routes.razor | Adds router setup for the dojo UI. |
| src/Components/AI/testassets/DojoClient/Components/Pages/Home.razor | Adds dojo landing page linking to scenarios. |
| src/Components/AI/testassets/DojoClient/Components/Layout/MainLayout.razor | Adds a minimal layout wrapper for the dojo UI. |
| src/Components/AI/testassets/DojoClient/Components/App.razor | Adds the dojo HTML host page wiring CSS/assets and blazor.web.js. |
| src/Components/AI/testassets/DojoClient/Components/_Imports.razor | Adds namespace imports for dojo UI components and AI primitives. |
| src/Components/AI/testassets/DojoClient/appsettings.json | Adds basic logging defaults for dojo UI. |
| src/Components/AI/testassets/DojoClient.E2E.Tests/Tests/AgenticChatScenarioTests.cs | Adds Playwright E2E tests validating incremental streaming UI behavior. |
| src/Components/AI/testassets/DojoClient.E2E.Tests/ServiceOverrides/RecordedScript.cs | Loads recorded model scripts used for deterministic replay. |
| src/Components/AI/testassets/DojoClient.E2E.Tests/ServiceOverrides/RecordedChatClient.cs | Implements an IChatClient that replays recorded streaming frames with gates. |
| src/Components/AI/testassets/DojoClient.E2E.Tests/ServiceOverrides/DojoModelOverrides.cs | Registers model overrides in the API host to use recorded replay clients. |
| src/Components/AI/testassets/DojoClient.E2E.Tests/ServiceOverrides/ApiCheckpointClient.cs | Adds helper to release API-side streaming gates from tests. |
| src/Components/AI/testassets/DojoClient.E2E.Tests/Fixtures/TestRoot.cs | Adds per-assembly server factory initialization for E2E tests. |
| src/Components/AI/testassets/DojoClient.E2E.Tests/Fixtures/E2ETestAssembly.cs | Adds marker type for E2E infra to locate assembly/manifest. |
| src/Components/AI/testassets/DojoClient.E2E.Tests/DojoClient.E2E.Tests.csproj | Adds the E2E test project referencing both dojo apps and components testing infra. |
| src/Components/AI/testassets/DojoClient.E2E.Tests/Baselines/AgenticChat.recording.json | Adds recorded deterministic model response frames for E2E. |
| src/Components/AI/testassets/Directory.Build.targets | Adds testasset build customizations (e.g., removing xUnit using). |
| src/Components/AI/testassets/Directory.Build.props | Adds testasset build customizations and Components.Testing integration properties. |
| src/Components/AI/testassets/AGUIDojoApi/ScriptedChatClient.cs | Adds local scripted model fallback for running dojo without credentials. |
| src/Components/AI/testassets/AGUIDojoApi/Properties/launchSettings.json | Adds local launch profile for dojo API host. |
| src/Components/AI/testassets/AGUIDojoApi/Program.cs | Configures AG-UI hosting + JSON options and maps dojo endpoints. |
| src/Components/AI/testassets/AGUIDojoApi/ChatClientAgentFactory.cs | Selects scripted vs OpenAI-compatible live model based on configuration. |
| src/Components/AI/testassets/AGUIDojoApi/appsettings.json | Adds basic logging defaults for dojo API host. |
| src/Components/AI/testassets/AGUIDojoApi/AGUIEventStreamResult.cs | Implements SSE result streaming AG-UI events. |
| src/Components/AI/testassets/AGUIDojoApi/AGUIEndpoint.cs | Maps AG-UI endpoints bridging RunAgent input to streaming chat updates. |
| src/Components/AI/testassets/AGUIDojoApi/AGUIDojoApiAssembly.cs | Adds marker type for E2E infra targeting the API host. |
| src/Components/AI/testassets/AGUIDojoApi/AGUIDojoApi.csproj | Adds dojo API test asset project referencing AG-UI server/formatting + AI packages. |
| src/Components/AI/test/TestHelpers/ResponseEmitters.cs | Adds helpers to emit streaming updates for unit tests. |
| src/Components/AI/test/TestHelpers/RecordingLoader.cs | Adds helpers to load and replay recorded update sequences in unit tests. |
| src/Components/AI/test/TestHelpers/DelegatingStreamingChatClient.cs | Adds delegating IChatClient for test-controlled streaming. |
| src/Components/AI/test/TestFramework/TestServiceProvider.cs | Adds minimal DI container for component tests. |
| src/Components/AI/test/TestFramework/TestRenderer.cs | Adds a lightweight renderer capturing batches/tree for component tests. |
| src/Components/AI/test/TestFramework/RenderedComponent.cs | Adds test wrapper for rendered components and tree querying. |
| src/Components/AI/test/TestFramework/NullJSRuntime.cs | Adds JS runtime stub for component tests. |
| src/Components/AI/test/TestFramework/HtmlContentWriter.cs | Adds HTML snapshot writer for render tree frames. |
| src/Components/AI/test/TestFramework/ComponentTree.cs | Adds component tree tracking and relationship rebuilding for tests. |
| src/Components/AI/test/TestFramework/ComponentRender.cs | Adds per-batch HTML snapshot record type for tests. |
| src/Components/AI/test/TestFramework/ComponentNode.cs | Adds component node model for rendered tree inspection. |
| src/Components/AI/test/TestFramework/CapturedBatch.cs | Adds captured batch model for renderer test inspection. |
| src/Components/AI/test/Pipeline/TextBlockHandlerParagraphTests.cs | Adds tests for paragraph splitting behavior in text block handler. |
| src/Components/AI/test/Pipeline/BlockMappingPipelineTests.cs | Adds tests for the block mapping pipeline behavior and handler precedence. |
| src/Components/AI/test/Microsoft.AspNetCore.Components.AI.Tests.csproj | Adds unit test project for Components.AI. |
| src/Components/AI/test/Engine/UIAgentTests.cs | Adds unit tests for UIAgent streaming and history behaviors. |
| src/Components/AI/test/Engine/UIAgentBaselineTests.cs | Adds baseline replay tests for real-world update shapes. |
| src/Components/AI/test/Engine/AgentContextTests.cs | Adds tests for conversation state transitions, retry/cancel, and notifications. |
| src/Components/AI/test/Components/MessageListTests.cs | Adds component tests validating streaming UI rendering and error UI. |
| src/Components/AI/test/Components/MessageInputTests.cs | Adds component tests validating input placeholder/disabled/registration behavior. |
| src/Components/AI/test/Components/ChatPageTests.cs | Adds component tests validating page shell structure and attribute flow. |
| src/Components/AI/test/Blocks/ContentBlockTests.cs | Adds unit tests for content block subscription and text accumulation. |
| src/Components/AI/test/Baselines/TextStreaming_SingleTurn.recording.json | Adds recorded update sequence baseline for single-turn streaming tests. |
| src/Components/AI/test/Baselines/TextStreaming_MultiTurn.recording.json | Adds recorded update sequence baseline for multi-turn streaming tests. |
| src/Components/AI/src/wwwroot/ai-chat.css | Adds default design tokens and styles for the Components.AI chat UI. |
| src/Components/AI/src/PublicAPI.Unshipped.txt | Declares new public APIs introduced by Components.AI. |
| src/Components/AI/src/PublicAPI.Shipped.txt | Initializes shipped API list for the new package. |
| src/Components/AI/src/Pipeline/UIAgentOptions.cs | Adds configuration surface for registering custom block handlers and chat options. |
| src/Components/AI/src/Pipeline/TextBlockHandler.cs | Adds fallback text handler that accumulates TextContent into rich blocks. |
| src/Components/AI/src/Pipeline/IHandlerEntry.cs | Adds internal pipeline handler entry abstraction. |
| src/Components/AI/src/Pipeline/IActiveEntry.cs | Adds internal active handler entry abstraction. |
| src/Components/AI/src/Pipeline/HandleResult.cs | Adds internal handler invocation result type for the pipeline. |
| src/Components/AI/src/Pipeline/HandlerEntry.cs | Adds handler entry and active-entry implementation. |
| src/Components/AI/src/Pipeline/ContentBlockHandler.cs | Adds public base class for mapping updates into blocks. |
| src/Components/AI/src/Pipeline/BlockMappingResult.cs | Adds public result type for block handler invocations. |
| src/Components/AI/src/Pipeline/BlockMappingPipelineLog.cs | Adds structured logging helpers for pipeline processing. |
| src/Components/AI/src/Pipeline/BlockMappingPipeline.cs | Adds pipeline that routes updates to active/inactive handlers and emits blocks. |
| src/Components/AI/src/Pipeline/BlockMappingContext.cs | Adds public context tracking handled contents within an update. |
| src/Components/AI/src/Microsoft.AspNetCore.Components.AI.csproj | Adds new shipping package project definition for Components.AI. |
| src/Components/AI/src/Engine/UIAgentLog.cs | Adds structured logging helpers for UIAgent. |
| src/Components/AI/src/Engine/UIAgent.cs | Adds UIAgent that streams blocks and maintains conversation history. |
| src/Components/AI/src/Engine/ConversationTurn.cs | Adds conversation turn model (request/response blocks). |
| src/Components/AI/src/Engine/ConversationStatus.cs | Adds conversation status enum. |
| src/Components/AI/src/Engine/AgentContext.cs | Adds conversation state owner + notifications + retry/cancel. |
| src/Components/AI/src/Components/MessageListContext.cs | Adds rendering context for blocks with custom renderer registration support. |
| src/Components/AI/src/Components/MessageList.cs | Adds message list component that renders turns and updates on streaming changes. |
| src/Components/AI/src/Components/MessageInput.cs | Adds message input component that sends messages and disables while streaming. |
| src/Components/AI/src/Components/ConversationTurnRenderer.cs | Adds internal renderer for a single conversation turn. |
| src/Components/AI/src/Components/ChatPage.cs | Adds full-page chat shell composed of boundary/list/input. |
| src/Components/AI/src/Components/BlockRendererRegistration.cs | Adds internal registration model for block renderers. |
| src/Components/AI/src/Components/BlockRenderer.cs | Adds block renderer registration component for custom markup. |
| src/Components/AI/src/Components/BlockContainer.cs | Adds container that re-renders list when a block signals changes. |
| src/Components/AI/src/Components/AgentBoundary.cs | Adds boundary that creates/cascades AgentContext and resets on agent changes. |
| src/Components/AI/src/Blocks/RichContentBlock.cs | Adds rich text block that accumulates streaming segments and exposes paragraphs. |
| src/Components/AI/src/Blocks/ContentBlockChangedSubscription.cs | Adds subscription handle type for block change notifications. |
| src/Components/AI/src/Blocks/ContentBlock.cs | Adds base content block with lifecycle + change notifications. |
| src/Components/AI/src/Blocks/BlockLifecycleState.cs | Adds lifecycle state enum for streaming blocks. |
| eng/Versions.props | Introduces version properties for Microsoft.Extensions.AI and AG-UI dependencies. |
| eng/ShippingAssemblies.props | Adds Microsoft.AspNetCore.Components.AI to shipping assemblies list. |
| eng/ProjectReferences.props | Adds Components.AI project reference provider mapping. |
| eng/Dependencies.props | Adds dependency declarations for Microsoft.Extensions.AI* and AGUI.* packages. |
| eng/Build.props | Excludes the new DojoClient E2E test project from certain build scans. |
| AspNetCore.slnx | Adds Components.AI folder + projects to the main solution definition. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: Copilot App <[email protected]> Copilot-Session: 323b0ef8-7905-4041-8388-a08586c0bd34
Align custom renderer precedence with its documented contract, validate required child content, keep textarea DOM state synchronized after submit, and prevent retry buttons from submitting surrounding forms. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 323b0ef8-7905-4041-8388-a08586c0bd34
| /// <typeparam name="TState">The state the handler keeps across updates.</typeparam> | ||
| public readonly struct BlockMappingResult<TState> | ||
| { | ||
| internal enum ResultKind { Pass, Emit, Update, Complete } |
There was a problem hiding this comment.
NIT: The naming made me confused, I was fixated on Pass == "block handler succeeded", not "this handler ignores and we pass it to the next handler". Can we rename to NotHandled or similar?
ilonatommy
left a comment
There was a problem hiding this comment.
Finished, I think we're good to merge. Great job!
kotlarmilos
left a comment
There was a problem hiding this comment.
LGTM, thanks! Follow-up work tracked in #68227 (comment)
Overview
Introduces the foundational
Microsoft.AspNetCore.Components.AIstreaming-chat layer and the first dojo scenario. The product library remains provider- and protocol-neutral by depending only onMicrosoft.Extensions.AI.IChatClient; AG-UI 0.0.5 is confined to separate API/client test assets that prove the feature across a real HTTP/SSE boundary. This is PR 1 of 9 in native stack #68340 and targetsmain.Design
The public contract separates model streaming, observable UI state, and rendering:
AgentContextowns the conversation contract—ordered turns, status, error, retry, and cancellation—whileAgentBoundary,MessageList,MessageInput, andChatPageare thin Blazor projections over its notifications. Custom content is an extension point rather than a transport concern:UIAgentOptions.AddBlockHandler<TState>registers handlers ahead of the built-in plain-text fallback, andBlockMappingResult<TState>explicitly choosesPass,Emit,Update, orComplete.The alternative of coupling the shipping assembly directly to AG-UI was rejected: doing so would make a wire protocol part of the component model. Instead, AG-UI is used only by the dojo applications, so any
IChatClientcan drive the same blocks and components.Implementation
UIAgentappends the request to persistent history, maps the user message, streamsChatResponseUpdatevalues through the two-phase block pipeline, finalizes active blocks, and records the completed assistant response for the next turn.AgentContextplaces those blocks into a turn and transitions through streaming, idle, cancellation, and error/retry states; disposable callbacks let components rerender without retaining stale subscriptions.The dojo follows the same boundary as the AG-UI reference server:
The deterministic E2E seam replaces only the API-side model client. The UI-side
AGUIChatClient, request serialization, POST, SSE stream, event parsing, Components.AI pipeline, and browser rendering all remain real:Repository wiring adds the shipping/test projects, pins AG-UI packages to 0.0.5 through the shared
dotnet-publicfeed, and includes all new package references in Dependabot discovery. Test source and generated recordings remain separate commits so reviewers can assess behavior without baseline noise.Outcome
Review in this order: public surface in
PublicAPI.Unshipped.txt; block lifecycle and two-phase pipeline;AgentContextstatus/error semantics; the API/client process boundary; then verify thatDojoModelOverridesreplaces only the API model and that recordings contain deterministic provider output with no credentials or user data.