# AG-UI

Show messages and shared agent state from an AG-UI endpoint.

## Connect an AG-UI endpoint

[Install Ghost UI and its view dependencies](/libraries/ai#installation), then add the AG-UI client.

```bash
npm install @ag-ui/client@1.0.5
```

Point one client and runtime per conversation at an endpoint that speaks AG-UI. Supply the endpoint credentials and thread ID in your app.

```tsx
import { useState } from "react";
import { HttpAgent } from "@ag-ui/client";
import { createAgUiRuntime } from "@collectivecreation/ghost-ui/chat-adapter-ag-ui";
import { useChatRuntimeStore } from "@collectivecreation/ghost-ui/chat-runtime-react";
import { Button } from "@collectivecreation/ghost-ui/components/button";
import { Message, MessageBody } from "@collectivecreation/ghost-ui/components/message";
import { MessageParts } from "@collectivecreation/ghost-ui/components/message-parts";
export function AgUiConversation({ url, threadId }: { url: string; threadId: string }) {
  const [owner] = useState(() => {
    const client = new HttpAgent({ url, threadId });
    return { client, store: createAgUiRuntime(client, { identity: { threadId, revision: 0 } }) };
  });
  const runtime = useChatRuntimeStore(owner.store);
  const [error, setError] = useState<unknown>();
  return <>
    {runtime.messages.map(message => <Message key={message.id} actor={message.actor}><MessageBody>
      <MessageParts parts={message.parts} />
    </MessageBody></Message>)}
    <Button disabled={runtime.capabilities.send.support !== "supported" || !runtime.capabilities.send.available}
      onClick={() => { setError(undefined); void Promise.resolve().then(() => runtime.controller.send({ text: "Review the brief", attachments: [] })).catch(setError); }}>Review</Button>
    {runtime.capabilities.stop.support === "supported" && runtime.capabilities.stop.available &&
      <Button onClick={() => { void Promise.resolve().then(() => runtime.controller.stop?.()).catch(setError); }}>Stop</Button>}
    {(error || runtime.controller.error) ? <p role="alert">The request could not complete.</p> : null}
  </>;
}

```

Mount with key={threadId + ":" + revision} when replacing its owner. useState intentionally creates the client once; changing props does not retarget it. Use a caller-created HttpAgent with headers or custom fetch when authentication needs application context.

## Shared state and advanced runs

The client assembles messages and updates client.state. Ghost shares a native subscription across its views and releases it when the last subscriber leaves. To show a plan or another state panel, read and validate owner.client.state alongside the messages.

createAgUiRuntime(client, { identity, run: native => native.runAgent({ forwardedProps, context, tools }) }) can customize a run using the actual client type. Schema validation, tool execution and access policy stay with the application. Use native pendingInterrupts and runAgent({ resume }) for protocol-specific resumption; an interrupt is not automatically a boolean approval.

## Boundaries and failures

Text send adds a native user message before running; if the run fails, that message remains native history for deliberate retry/reconciliation. Stop calls abortRun; it is a cancellation request. Ghost's generic retry/edit/tool approval are not guessed from AG-UI events. Use native commands or a deliberate application adapter when equivalent semantics exist.

For protocol schemas, state events and tools see the [AG-UI client reference](https://docs.ag-ui.com/sdk/js/client). [Pydantic AI](/libraries/ai-runtime-pydantic), [Agno](/libraries/ai-runtime-agno) and [CrewAI](/libraries/ai-runtime-crewai) can expose this same protocol.
