Define an activity type with defineActivityType, then call emitActivity from a server tool or a middleware. The client gets a typed row in messages, and chat persistence keeps that row after a reload.
For a high-volume stream, such as log lines, use custom events.
Put the definition in a module that the server and the client both import:
// activities.ts
import { defineActivityType } from '@tanstack/ai'
import { z } from 'zod'
export const deployProgress = defineActivityType({
activityType: 'deploy.progress',
contentSchema: z.object({ step: z.number(), total: z.number() }),
})Call context?.emitActivity?.(definition, id, content) in the tool. The id names the row. Each call with the same id replaces the content of that row:
// tools.ts
import { toolDefinition } from '@tanstack/ai'
import { z } from 'zod'
import { deployProgress } from './activities'
// Stands in for one deploy step, such as a build or an upload.
const runStep = () => new Promise((resolve) => setTimeout(resolve, 2000))
export const deploy = toolDefinition({
name: 'deploy',
description: 'Deploy an app to production',
inputSchema: z.object({ app: z.string() }),
}).server(async ({ app }, context) => {
const id = `deploy-${context?.toolCallId}`
const total = 3
for (let step = 0; step < total; step++) {
context?.emitActivity?.(deployProgress, id, { step, total })
await runStep()
}
context?.emitActivity?.(deployProgress, id, { step: total, total })
return { deployed: app }
})Here step counts the finished steps. The last call sets step to total.
Give the tool to chat():
// api/chat.ts
import {
chat,
chatParamsFromRequest,
toServerSentEventsResponse,
} from '@tanstack/ai'
import { openaiText } from '@tanstack/ai-openai'
import { deploy } from './tools'
export async function POST(request: Request) {
const params = await chatParamsFromRequest(request)
const stream = chat({
adapter: openaiText('gpt-6.1-sol'),
messages: params.messages,
threadId: params.threadId,
runId: params.runId,
tools: [deploy],
})
return toServerSentEventsResponse(stream)
}Each row is a message with role: 'activity' and one part with type: 'activity'. Pass the definition to useChat({ activities }). Then part.activityType === 'deploy.progress' gives part.content its type:
// chat.tsx
import { fetchServerSentEvents, useChat } from '@tanstack/ai-react'
import { deployProgress } from './activities'
export function Chat() {
const { messages, sendMessage, isLoading } = useChat({
connection: fetchServerSentEvents('/api/chat'),
threadId: 'deploy-chat',
activities: [deployProgress],
})
return (
<>
<button onClick={() => sendMessage('Deploy the shop app')}>Deploy</button>
{messages.map((message) => (
<div key={message.id}>
{message.parts.map((part, index) => {
if (part.type === 'text') {
return <p key={index}>{part.content}</p>
}
if (part.type !== 'activity' || part.activityType !== 'deploy.progress') {
return null
}
const { step, total } = part.content
if (step === total) {
return <p key={index}>Deployed.</p>
}
// A row that is not done is live only while the run streams.
return isLoading ? (
<p key={index}>
Deploying: {step} of {total} steps done...
</p>
) : null
})}
</div>
))}
</>
)
}activities only gives the types. The client does not check the content that it gets.
Do you render with createChatUI or createChatHook? Give each activity type its own component with activitiesComponents. See React Chat UI.
Click Deploy. The model calls the deploy tool. A row shows "Deploying: 0 of 3 steps done..." and counts up every two seconds. At the end, it says "Deployed."
Add chat persistence. withPersistence saves the rows, and reconstructChat gives them back.
// api/chat.ts
import {
chat,
chatParamsFromRequest,
toServerSentEventsResponse,
} from '@tanstack/ai'
import { openaiText } from '@tanstack/ai-openai'
import {
memoryPersistence,
reconstructChat,
withPersistence,
} from '@tanstack/ai-persistence'
import { deploy } from './tools'
// Use a durable backend in production.
const persistence = memoryPersistence()
export async function POST(request: Request) {
const params = await chatParamsFromRequest(request)
const stream = chat({
adapter: openaiText('gpt-6.1-sol'),
messages: params.messages,
threadId: params.threadId,
runId: params.runId,
tools: [deploy],
middleware: [withPersistence(persistence)],
})
return toServerSentEventsResponse(stream)
}
export function GET(request: Request): Promise<Response> {
// Add `authorize` in a multi-user app. See Client persistence.
return reconstructChat(persistence, request)
}The "Deployed." row comes back from the store.
For a run that still streams:
For your own backend, add an activities store. See AG-UI activity rows on reload.
A middleware can send a row too, for example while it loads data before the model starts. Middleware always has ctx.emitActivity, so you call it without ?..
// activities.ts
import { defineActivityType } from '@tanstack/ai'
import { z } from 'zod'
export const deployProgress = defineActivityType({
activityType: 'deploy.progress',
contentSchema: z.object({ step: z.number(), total: z.number() }),
})
export const filesStatus = defineActivityType({
activityType: 'files.status',
contentSchema: z.object({ status: z.enum(['loading', 'ready']) }),
})// load-files.ts
import type { ChatMiddleware } from '@tanstack/ai'
import { filesStatus } from './activities'
// Stands in for your own slow work, such as a database read.
const loadProjectFiles = () => new Promise((resolve) => setTimeout(resolve, 2000))
export const loadFiles: ChatMiddleware = {
name: 'load-files',
async onStart(ctx) {
const id = `files-${ctx.runId}`
ctx.emitActivity(filesStatus, id, { status: 'loading' })
await loadProjectFiles()
ctx.emitActivity(filesStatus, id, { status: 'ready' })
},
}Each run now starts with a files.status row that goes from loading to ready. The engine sends each update immediately, also while the hook still waits.
useChat types two built-in types. They need no entry in activities:
| activityType | Sent by | Guide |
|---|---|---|
| sandbox.status | withSandbox, while the sandbox starts | Show a pending state while the sandbox starts |
| sandbox.approval | The Claude Code, OpenCode, and ACP adapters, when an action waits for a decision | Show an approval prompt |
Their definitions are sandboxStatusActivity and sandboxApprovalActivity, from @tanstack/ai.
To answer a pending sandbox.approval row, call respondToSandboxApproval(approvalId, granted) from useChat. See Show an approval prompt.
=== accepts only the built-in names and the names in activities, so a typo is a compile error. For any other type, call the is() of its definition. is() takes any value, so a card can check the part itself:
// review-card.tsx
import { defineActivityType } from '@tanstack/ai'
import { z } from 'zod'
export const reviewStatus = defineActivityType({
activityType: 'review.status',
contentSchema: z.object({ reviewer: z.string() }),
})
export function ReviewCard({ part }: { part: unknown }) {
if (!reviewStatus.is(part)) {
return null
}
return <p>Waiting for a review from {part.content.reviewer}.</p>
}Render <ReviewCard part={part} /> for each part. The card shows only the review.status rows, with typed content. It works in any chat, also in a chat with no activities.
A type that no check selects still gets to the last else, or to the default branch of a switch. There, content is Record<string, unknown>.