Skip to content

[Blazor] Add tool-based generative UI dojo scenario - #68330

Merged
ilonatommy merged 6 commits into
mainfrom
javiercn-components-ai-06-tool-generative-ui
Aug 25, 2026
Merged

ilonatommy merged 6 commits into
mainfrom
javiercn-components-ai-06-tool-generative-ui

Conversation

@javiercn

@javiercn javiercn commented Aug 10, 2026 •

Copy link
Copy Markdown
Member

Overview

Adds the canonical Tool-Based Generative UI dojo scenario as position 6 in the native Components.AI stack tracked by #68340, stacked on #68329. This three-commit layer changes only dojo/test assets: it composes the existing client-action APIs to paint a streamed generate_haiku call as rich UI, without adding a shipping product API. The governing constraint is preserved end to end: the browser still uses DojoClient's AGUIChatClient over real HTTP/SSE to AGUIDojoApi; deterministic replay replaces only the API-side model.

Design

The scenario introduces one internal protocol contract: /tool_based_generative_ui advertises generate_haiku, whose arguments contain exactly three Japanese lines, three English lines, an image name, and a CSS gradient. The UI accepts only the demonstrated two-color linear-gradient format and falls back to the placeholder gradient for other values. The API prompt owns the model-facing shape, while the UI registers the same route as a keyed IChatClient so scenario selection cannot silently fall back to another endpoint.

// src/Components/AI/testassets/AGUIDojoApi/Program.cs
// The API endpoint injects the scenario prompt but otherwise uses the shared AG-UI request/SSE pipeline.
app.MapDojoEndpoint(
    "/tool_based_generative_ui",
    systemPrompt: ChatClientAgentFactory.ToolBasedGenerativeUISystemPrompt);
// src/Components/AI/testassets/DojoClient/Program.cs
// A keyed client keeps this scenario on the real DojoClient -> AGUI.Client -> HTTP/SSE boundary.
builder.Services.AddKeyedScoped<IChatClient>(
    DojoScenarios.ToolBasedGenerativeUIEndpoint,
    (sp, _) => CreateChatClient(sp, DojoScenarios.ToolBasedGenerativeUIEndpoint));

static IChatClient CreateChatClient(IServiceProvider services, string endpoint)
{
    var httpClient = services.GetRequiredService<IHttpClientFactory>()
        .CreateClient(DojoScenarios.ApiHttpClientName);
    var aguiClient = new AGUIChatClient(new AGUIChatClientOptions(httpClient, endpoint));

    return new FormattedChatClient(aguiClient);
}

The deliberate alternative not taken is an in-process or directly mocked UI client: that would make the card test easier but would stop testing AG-UI tool serialization and SSE parsing. The remaining contract additions are internal scenario composition, not new framework surface: UIActionBlock selects generate_haiku, and HaikuData feeds the card/carousel renderers.

Implementation

The user-facing entry point registers the existing client-action mechanism and maps the completed tool arguments into observable component state. Appending rather than replacing preserves the placeholder and prior generated cards; selecting the newest index makes progressive generation visible immediately.

// src/Components/AI/testassets/DojoClient/Components/Scenarios/ToolBasedGenerativeUI/ToolBasedGenerativeUIScenario.razor
// Existing UIAgent infrastructure advertises the browser-side tool; no product handler is added.
_agent = new UIAgent(ChatClient, options =>
{
    options.RegisterUIAction(AIFunctionFactory.Create(
        GenerateHaiku,
        name: "generate_haiku",
        description: "Generate a haiku with Japanese text, English translation, an image, and a gradient background."));
}, LoggerFactory);

private string GenerateHaiku(
    List<string> japanese,
    List<string> english,
    string image_name,
    string gradient)
{
    _haikus.Add(new HaikuData
    {
        Japanese = japanese,
        English = english,
        ImageName = image_name,
        Gradient = GetSafeGradient(gradient),
    });
    _currentIndex = _haikus.Count - 1;
    _ = InvokeAsync(StateHasChanged);

    return "Haiku displayed to user.";
}

The repeated files fall into three equivalence classes. Scenario glue (Home.razor, _Imports.razor, DojoScenarios.cs, and both Program.cs files) only makes the route discoverable and keeps endpoint selection aligned. Presentation (HaikuData, GenerateHaikuAction, HaikuCard, HaikuCarousel, and their CSS) has one lifecycle rule: invoke the action once, then render the latest card while previous/next buttons clamp to valid indices. Deterministic coverage (DojoModelOverrides, the scenario test, and the generated recording) uses one representative nature haiku with two summary checkpoints; the scripted live fallback mirrors the same call/result shape for credential-free manual use.

The browser test proves the lifecycle rather than only checking final markup: the card exists while assistant text still has the streaming class, navigation works before completion, and only then does the API-side checkpoint release the continuation.

// src/Components/AI/testassets/DojoClient.E2E.Tests/Tests/ToolBasedGenerativeUIScenarioTests.cs
await AssertGeneratedHaikuAsync(carousel);
await AssertGeneratedCarouselControlsAsync(carousel);
await Expect(assistant).ToHaveClassAsync(
    "sc-ai-message__content sc-ai-message__content--streaming");
await Expect(_page.Locator("button.sc-ai-input__send")).ToBeDisabledAsync();

await previous.ClickAsync();
await AssertPlaceholderHaikuAsync(carousel);
await Expect(previous).ToBeDisabledAsync();
await Expect(next).ToBeEnabledAsync();

await next.ClickAsync();
await AssertGeneratedHaikuAsync(carousel);
await _checkpoints.ReleaseAsync(prompt, "haiku-summary-start");

await Expect(assistant).ToHaveTextAsync(
    "Your nature haiku is ready\u2014a quiet pond awakened by a frog.");
await Expect(_page.Locator("button.sc-ai-input__send")).ToBeEnabledAsync();

Outcome

Equivalence class Reviewer acceptance criterion Evidence
AG-UI transport Tool declaration, arguments, result, and continuation cross DojoClient -> AGUIDojoApi rather than an in-process replacement. Dual-host test starts both applications and overrides only the API model.
Generative UI lifecycle The generated card paints before the response completes, then returns one tool result and finishes the assistant turn. Streaming class and disabled input are asserted before checkpoint release; completed text and enabled input are asserted after it.
Carousel presentation Placeholder and generated cards remain navigable while the stream is active, with boundary buttons disabled correctly. Test moves 2/2 -> 1/2 -> 2/2 before releasing the continuation.

Review guidance: read the endpoint/client pairing, ToolBasedGenerativeUIScenario.razor, GenerateHaikuAction.razor, and the checkpointed E2E flow closely. The card/carousel CSS and recorded Japanese/English fixture values are equivalent presentation data and can be spot-checked. Acceptance is met when the card appears from tool arguments during streaming, carousel navigation remains interactive, and the same turn completes over the real dual-host transport.

Validation: dependency-aware E2E build passed with 0 warnings and 0 errors; runtime build passed with 0 warnings and 0 errors; AgentContextUIActionTests passed 4/4; ToolBasedGenerativeUIScenarioTests passed 1/1.

@javiercn
javiercn requested a review from a team as a code owner August 10, 2026 19:56
@javiercn
javiercn force-pushed the javiercn-components-ai-06-tool-generative-ui branch from c9dc674 to 7ad9a88 Compare August 11, 2026 07:10
@javiercn
javiercn force-pushed the javiercn-components-ai-06-tool-generative-ui branch from 7ad9a88 to 169074a Compare August 12, 2026 13:10
Copilot AI lite review requested due to automatic review settings August 12, 2026 16:47
@kotlarmilos
kotlarmilos force-pushed the javiercn-components-ai-06-tool-generative-ui branch from 169074a to fe640b1 Compare August 12, 2026 16:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new “Tool-Based Generative UI” dojo scenario (position 6 in the native Components.AI stack) that exercises AG-UI tool-call serialization and SSE streaming over the real DojoClient → AGUIDojoApi HTTP boundary. The scenario registers a generate_haiku UI action, maps tool arguments into component state to render a haiku carousel during streaming, and includes deterministic E2E coverage via recorded API-side model replay.

Changes:

  • Register and expose the new /tool_based_generative_ui scenario in DojoClient (routing, DI keyed IChatClient, navigation entry).
  • Add new UI components (scenario page, action renderer, haiku card/carousel + CSS) driven by generate_haiku tool arguments.
  • Add a checkpointed Playwright E2E test plus recorded model baseline and scripted live fallback in AGUIDojoApi.
Show a summary per file
File Description
src/Components/AI/testassets/DojoClient/Program.cs Registers a keyed IChatClient for the new tool-based generative UI endpoint.
src/Components/AI/testassets/DojoClient/DojoScenarios.cs Adds the ToolBasedGenerativeUIEndpoint scenario constant.
src/Components/AI/testassets/DojoClient/Components/Scenarios/ToolBasedGenerativeUI/ToolBasedGenerativeUIScenario.razor.css Layout styling for split display/chat view and responsive behavior.
src/Components/AI/testassets/DojoClient/Components/Scenarios/ToolBasedGenerativeUI/ToolBasedGenerativeUIScenario.razor New scenario page wiring UIAgent + generate_haiku UI action and carousel state updates.
src/Components/AI/testassets/DojoClient/Components/Scenarios/ToolBasedGenerativeUI/HaikuData.cs Adds the scenario data model for haiku content and presentation fields.
src/Components/AI/testassets/DojoClient/Components/Scenarios/ToolBasedGenerativeUI/HaikuCarousel.razor.css Styling for carousel navigation controls and counter.
src/Components/AI/testassets/DojoClient/Components/Scenarios/ToolBasedGenerativeUI/HaikuCarousel.razor Carousel component that renders a single card and navigates between haikus.
src/Components/AI/testassets/DojoClient/Components/Scenarios/ToolBasedGenerativeUI/HaikuCard.razor.css Styling for the haiku “card” presentation.
src/Components/AI/testassets/DojoClient/Components/Scenarios/ToolBasedGenerativeUI/HaikuCard.razor Card component rendering Japanese + English lines with gradient background.
src/Components/AI/testassets/DojoClient/Components/Scenarios/ToolBasedGenerativeUI/GenerateHaikuAction.razor.css Styling for the action status line.
src/Components/AI/testassets/DojoClient/Components/Scenarios/ToolBasedGenerativeUI/GenerateHaikuAction.razor Auto-invokes the UI action block and displays completion status.
src/Components/AI/testassets/DojoClient/Components/Pages/Home.razor Adds the new scenario link to the home page list.
src/Components/AI/testassets/DojoClient/Components/_Imports.razor Imports the new scenario namespace for component usage.
src/Components/AI/testassets/DojoClient.E2E.Tests/Tests/ToolBasedGenerativeUIScenarioTests.cs Adds Playwright E2E coverage validating render-during-streaming + carousel navigation.
src/Components/AI/testassets/DojoClient.E2E.Tests/ServiceOverrides/DojoModelOverrides.cs Hooks the new scenario to a recorded model baseline for determinism.
src/Components/AI/testassets/DojoClient.E2E.Tests/Baselines/ToolBasedGenerativeUI.recording.json Recorded model frames/tool call+result for the scenario’s deterministic replay.
src/Components/AI/testassets/AGUIDojoApi/ScriptedChatClient.cs Adds a live scripted fallback for generate_haiku and its continuation summary.
src/Components/AI/testassets/AGUIDojoApi/Program.cs Maps the new dojo endpoint to the corresponding system prompt.
src/Components/AI/testassets/AGUIDojoApi/ChatClientAgentFactory.cs Adds the ToolBasedGenerativeUISystemPrompt for the new scenario contract.

Review details

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

  • Files reviewed: 19/19 changed files
  • Comments generated: 2
  • Review effort level: Lite

@javiercn
javiercn force-pushed the javiercn-components-ai-06-tool-generative-ui branch from fe640b1 to cf1356d Compare August 13, 2026 09:55
@ilonatommy
ilonatommy force-pushed the javiercn-components-ai-06-tool-generative-ui branch from cf1356d to e335258 Compare August 14, 2026 12:37
@ilonatommy
ilonatommy force-pushed the javiercn-components-ai-06-tool-generative-ui branch from e335258 to 37849fc Compare August 20, 2026 06:55
@ilonatommy
ilonatommy force-pushed the javiercn-components-ai-06-tool-generative-ui branch from cf1d845 to dae3619 Compare August 21, 2026 14:22
Base automatically changed from javiercn-components-ai-05-human-in-loop to main August 25, 2026 08:11
javiercn and others added 4 commits August 25, 2026 10:11
Rationale: demonstrate canonical Tool-Based Generative UI across the real DojoClient to AGUIDojoApi HTTP/SSE boundary by painting rich browser UI from a streamed client-tool call.

Implementation: add the /tool_based_generative_ui endpoint and keyed AGUIChatClient, declare generate_haiku as a browser UI action, and render generated Japanese and English verse in an interactive gradient-card carousel.

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: 323b0ef8-7905-4041-8388-a08586c0bd34
Rationale: lock down streamed rich client-tool rendering and carousel interactivity without replacing DojoClient's real AG-UI transport.

Implementation: add API-side model replay registration and focused dual-host browser assertions for the placeholder card, generated haiku, in-progress assistant stream, and bidirectional carousel navigation.

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: 323b0ef8-7905-4041-8388-a08586c0bd34
Rationale: keep deterministic Tool-Based Generative UI browser coverage independent of live model credentials while exercising the real AG-UI HTTP/SSE boundary.

Implementation: record the generate_haiku call, exact Japanese and English arguments, gradient card data, tool result continuation, and two streamed summary checkpoints.

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: 323b0ef8-7905-4041-8388-a08586c0bd34
@ilonatommy
ilonatommy force-pushed the javiercn-components-ai-06-tool-generative-ui branch from dae3619 to c6b49da Compare August 25, 2026 08:11
@ilonatommy

ilonatommy commented Aug 25, 2026 •

Copy link
Copy Markdown
Member

Current flow is:

  • User: write me haiku
  • Model: calls genrate_haiku
  • Browser: execute UI action and render
  • Model: sends the acknowledgment message, like: "haiku done"

In CopilotKit developer can disable the last follow-up message to save costs. We could have similar API that skips the last step in some scenarios.

#68780

@ilonatommy
ilonatommy merged commit 31b2046 into main Aug 25, 2026
29 checks passed
@ilonatommy
ilonatommy deleted the javiercn-components-ai-06-tool-generative-ui branch August 25, 2026 14:21
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Aug 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants