--- name: crestapps-core-ag-ui description: Skill for running the CrestApps.Core 2.0 AI chat as an AG-UI agent. Covers the CrestApps.Core.AI.AGUI package (AGUIRunEventWriter, AGUIRunRequest, AGUIJson), AIChatHubCore.RunAgent over SignalR and MapAGUIChat with Server-Sent Events, the RunAgentInput members the chat reads and the events it streams, tool approvals with RequireApproval, ApprovalRequiredAIFunction, interrupts and resume, page context with crestAppsAGUI.registerContextProvider, shared state with registerStateProvider and update_shared_state, frontend tools, stored tool calls (ChatTurnToolActivity), runs that continue on the next page, AGUIChatOptions.DisconnectedRunTimeout, the protocol and sendPageContext chat options, and the AICompletionContextKeys and ChatResponseHandlerContext members. Use it when showing tool calls in the chat, asking a person before a tool runs, sharing page state with the agent, connecting CopilotKit or another AG-UI client, or troubleshooting approvals and continued runs. license: Apache-2.0 metadata: author: CrestApps Team version: "1.0" --- # CrestApps.Core AG-UI [AG-UI](https://docs.ag-ui.com) is an event protocol between an agent and the app a person uses it in. A client starts a *run* with a `RunAgentInput` (thread, newest message, the tools it runs itself, shared state, page context) and the agent streams events back: `RUN_STARTED`, text, reasoning, tool calls and results, state changes, then `RUN_FINISHED` or `RUN_ERROR`. CrestApps runs the whole AI chat pipeline (profiles, orchestration, retrieval, tools, guardrails, session history) behind it. | Transport | Used by | |---|---| | The chat hub's `RunAgent(JsonElement input, CancellationToken)` method (SignalR, returns a `ChannelReader` of events) | The CrestApps chat UI and widget (`ai-chat.js`), by default | | `MapAGUIChat()` (HTTP `POST`, Server-Sent Events) | Any AG-UI client: CopilotKit, the AG-UI client SDKs, your own app | Both go through the same hub, so the hub's profile and session authorization, rate limits and guardrails apply to both. ## Guidelines - AG-UI has no feature to enable. Every hub that derives from `AIChatHubCore` has `RunAgent`, and `AddCoreAIOrchestration()` (called by `AddAISuite()`) registers the shared state tool, the frontend tool provider and the page context handler. - Map `MapAGUIChat` only when other AG-UI clients should reach your profiles over HTTP, and add authorization and rate limiting to the builder it returns. - Mark tools that change data with `.RequireApproval()`. They only run after a person approves the call, and are never offered where nobody can answer. - Page context and shared state are data the browser sends. The model receives them as data, never as instructions; do not put secrets in them. - Chat Interactions (`ChatInteractionHubBase`) and realtime voice conversations do not speak AG-UI yet; they keep the text-only stream. ## Packages | Package | What it has | |---|---| | `CrestApps.Core.AI.AGUI` | `AGUIRunEventWriter` (chat updates to AG-UI events), `AGUIRunRequest` (what a run input asks for: prompt, `Resume`, `ClientToolResults`, `IsResume`, `TryGetApproval`) and `AGUIJson` (`SerializerOptions`, `SerializeEvent`, `DeserializeInput`), on the MIT-licensed AG-UI .NET SDK (`AGUI.Abstractions`, `AGUI.Formatting` 1.0.1) | | `CrestApps.Core.AI.Chat` | `AIChatHubCore.RunAgent`, resumable runs, `AGUIChatOptions` and `MapAGUIChat` (namespace `CrestApps.Core.AI.Chat.AGUI`) | | `CrestApps.AI.Resources` (`@crestapps/ai-chat-ui`) | The chat UI's AG-UI client: tool cards, reasoning, approvals, page context, shared state (`agui-activity.css` styles) | ## Setup ```csharp using CrestApps.Core.AI.Chat.AGUI; builder.Services.AddCrestAppsCore(crestApps => crestApps .AddAISuite(ai => ai .AddOpenAI() ) ); // Optional: how long a run keeps going after its connection drops (two minutes by default; TimeSpan.Zero ends it with the connection). builder.Services.Configure(options => options.DisconnectedRunTimeout = TimeSpan.FromMinutes(1)); var app = builder.Build(); app.MapHub("/hubs/ai-chat"); // Optional: POST api/ai/agui (AGUIEndpointRouteBuilderExtensions.DefaultPattern) with a RunAgentInput. app.MapAGUIChat() .RequireAuthorization(); ``` - Without the builder: `services.AddCoreServices().AddCoreAIServices().AddCoreAIOrchestration();`. - A hub whose client contract extends `IAIChatHubClient` uses `MapAGUIChat(pattern)`. - The endpoint reads only `Content-Type: application/json` (a cross-site page cannot send that without a CORS preflight), so it calls `DisableAntiforgery()` and needs no token. - The chat UI uses `RunAgent` by default (`protocol: 'agui'`). Set `protocol: 'legacy'` in the chat configuration to keep the text-only `SendMessage` stream; a hub without `RunAgent` makes the UI fall back by itself. | Want | Add | |---|---| | Tool calls, reasoning, citations and shared state in the chat | Nothing: the chat UI and widget use AG-UI by default | | Other AG-UI clients over HTTP | `app.MapAGUIChat()` | | A person approves a tool before it runs | `.RequireApproval()` on the tool | | The agent works on the page and keeps going on pages it opens | WebMCP page tools (`crestapps-core-webmcp`) | | No page context sent to the model | `sendPageContext: false` in the chat configuration | ## The Run Input | `RunAgentInput` member | What the chat does | |---|---| | `threadId` | The chat session id. Empty starts a new session; its id comes back in `RUN_STARTED` | | `runId` | Echoed in `RUN_STARTED` and `RUN_FINISHED` | | `messages` | Only the newest user message is read (the server keeps the history), plus `role: "tool"` results of calls that waited for the client | | `forwardedProps.profileId` | The AI profile (required for a new message). `forwardedProps.sessionProfileId` names a session-level prompt profile | | `context` | Page context entries | | `state` | The shared state | | `tools` | Frontend tools the client runs itself | | `resume` | Answers to the previous run's interrupts | ## The Events | Event | When | |---|---| | `RUN_STARTED` / `RUN_FINISHED` / `RUN_ERROR` | Every run. `RUN_FINISHED` has a `success` outcome (with `pendingToolCallIds` when frontend tools wait) or an `interrupt` outcome. Hub errors (unknown profile, refused session, rate limit) end with `RUN_ERROR`; a refused session start has the code `session_start_rejected` | | `TEXT_MESSAGE_START` / `CONTENT` / `END` | The answer. Its message id is the stored assistant message id; text written again after a tool call is a new message whose id ends with `-1`, `-2`, ... | | `REASONING_START` ... `REASONING_END` | Visible reasoning, when the model streams it | | `TOOL_CALL_START` / `ARGS` / `END` / `RESULT` | Every tool call and result (profile, MCP, agent, page and shared state tools). Results are cut to 4,000 characters | | `STATE_SNAPSHOT` / `STATE_DELTA` | The shared state and each change, as JSON Patch | | `CUSTOM` | `crestapps.assistant_message`, `crestapps.references`, `crestapps.message_replaced` (output checks replaced what streamed), `crestapps.tool_calls_resolved`, `crestapps.continues_on_next_page` | Tool calls are stored with the assistant message as a `ChatTurnToolActivity` (`ToolCalls` of `ChatTurnToolCall`: `CallId`, `Name`, `Arguments`, `Result`, `Status`, `ApprovalRequestId`; `ContinuesOnNextPage`; `HasPendingCalls`). `ChatTurnToolCallStatus` is `Requested`, `Completed`, `AwaitingApproval`, `Approved`, `Rejected` or `AwaitingClient`. The session payload returns them (`AIChatResponseMessageDetailed.ToolCalls`) and the history replays calls and results to the model in later turns. ## Tool Approvals ```csharp using CrestApps.Core.AI.Tooling; builder.Services .AddCoreAITool(DeleteArticleTool.TheName) .WithDescription("Deletes an article.") .Selectable() .RequireApproval(); ``` 1. In an AG-UI run, `AIChatResponseHandler` sets `AICompletionContextKeys.ToolApprovalSupported`, `ToolMaterializationOptions.SupportsToolApproval` is `true`, and the tool is wrapped in `ApprovalRequiredAIFunction`. 2. When the model calls it, the tool does not run: the run ends with an `interrupt` outcome naming each call and asking for `{ "approved": true | false }`, and the paused message is stored with the calls `AwaitingApproval`. When the model calls several tools at once and one needs approval, the whole batch waits. 3. The chat UI shows **Allow** and **Deny**. It accepts the click only from the `