@assistant-ui/mcp-docs-server 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.docs/organized/code-examples/local-ollama.md +1135 -0
- package/.docs/organized/code-examples/search-agent-for-e-commerce.md +1721 -0
- package/.docs/organized/code-examples/with-ai-sdk.md +1081 -0
- package/.docs/organized/code-examples/with-cloud.md +1164 -0
- package/.docs/organized/code-examples/with-external-store.md +1064 -0
- package/.docs/organized/code-examples/with-ffmpeg.md +1305 -0
- package/.docs/organized/code-examples/with-langgraph.md +1819 -0
- package/.docs/organized/code-examples/with-openai-assistants.md +1175 -0
- package/.docs/organized/code-examples/with-react-hook-form.md +1727 -0
- package/.docs/organized/code-examples/with-vercel-ai-rsc.md +1157 -0
- package/.docs/raw/blog/2024-07-29-hello/index.mdx +65 -0
- package/.docs/raw/blog/2024-09-11/index.mdx +10 -0
- package/.docs/raw/blog/2024-12-15/index.mdx +10 -0
- package/.docs/raw/blog/2025-01-31-changelog/index.mdx +129 -0
- package/.docs/raw/docs/about-assistantui.mdx +44 -0
- package/.docs/raw/docs/api-reference/context-providers/AssistantRuntimeProvider.mdx +30 -0
- package/.docs/raw/docs/api-reference/context-providers/TextContentPartProvider.mdx +26 -0
- package/.docs/raw/docs/api-reference/integrations/react-hook-form.mdx +103 -0
- package/.docs/raw/docs/api-reference/integrations/vercel-ai-sdk.mdx +145 -0
- package/.docs/raw/docs/api-reference/overview.mdx +583 -0
- package/.docs/raw/docs/api-reference/primitives/ActionBar.mdx +264 -0
- package/.docs/raw/docs/api-reference/primitives/AssistantModal.mdx +129 -0
- package/.docs/raw/docs/api-reference/primitives/Attachment.mdx +96 -0
- package/.docs/raw/docs/api-reference/primitives/BranchPicker.mdx +87 -0
- package/.docs/raw/docs/api-reference/primitives/Composer.mdx +204 -0
- package/.docs/raw/docs/api-reference/primitives/ContentPart.mdx +173 -0
- package/.docs/raw/docs/api-reference/primitives/Error.mdx +70 -0
- package/.docs/raw/docs/api-reference/primitives/Message.mdx +181 -0
- package/.docs/raw/docs/api-reference/primitives/Thread.mdx +197 -0
- package/.docs/raw/docs/api-reference/primitives/composition.mdx +21 -0
- package/.docs/raw/docs/api-reference/runtimes/AssistantRuntime.mdx +33 -0
- package/.docs/raw/docs/api-reference/runtimes/AttachmentRuntime.mdx +46 -0
- package/.docs/raw/docs/api-reference/runtimes/ComposerRuntime.mdx +69 -0
- package/.docs/raw/docs/api-reference/runtimes/ContentPartRuntime.mdx +22 -0
- package/.docs/raw/docs/api-reference/runtimes/MessageRuntime.mdx +49 -0
- package/.docs/raw/docs/api-reference/runtimes/ThreadListItemRuntime.mdx +32 -0
- package/.docs/raw/docs/api-reference/runtimes/ThreadListRuntime.mdx +31 -0
- package/.docs/raw/docs/api-reference/runtimes/ThreadRuntime.mdx +48 -0
- package/.docs/raw/docs/architecture.mdx +92 -0
- package/.docs/raw/docs/cloud/authorization.mdx +152 -0
- package/.docs/raw/docs/cloud/overview.mdx +55 -0
- package/.docs/raw/docs/cloud/persistence/ai-sdk.mdx +54 -0
- package/.docs/raw/docs/cloud/persistence/langgraph.mdx +123 -0
- package/.docs/raw/docs/concepts/architecture.mdx +19 -0
- package/.docs/raw/docs/concepts/runtime-layer.mdx +163 -0
- package/.docs/raw/docs/concepts/why.mdx +9 -0
- package/.docs/raw/docs/copilots/make-assistant-readable.mdx +71 -0
- package/.docs/raw/docs/copilots/make-assistant-tool-ui.mdx +76 -0
- package/.docs/raw/docs/copilots/make-assistant-tool.mdx +117 -0
- package/.docs/raw/docs/copilots/model-context.mdx +135 -0
- package/.docs/raw/docs/copilots/motivation.mdx +191 -0
- package/.docs/raw/docs/copilots/use-assistant-instructions.mdx +62 -0
- package/.docs/raw/docs/getting-started.mdx +1133 -0
- package/.docs/raw/docs/guides/Attachments.mdx +640 -0
- package/.docs/raw/docs/guides/Branching.mdx +59 -0
- package/.docs/raw/docs/guides/Editing.mdx +56 -0
- package/.docs/raw/docs/guides/Speech.mdx +43 -0
- package/.docs/raw/docs/guides/ToolUI.mdx +663 -0
- package/.docs/raw/docs/guides/Tools.mdx +496 -0
- package/.docs/raw/docs/index.mdx +7 -0
- package/.docs/raw/docs/legacy/styled/AssistantModal.mdx +85 -0
- package/.docs/raw/docs/legacy/styled/Decomposition.mdx +633 -0
- package/.docs/raw/docs/legacy/styled/Markdown.mdx +86 -0
- package/.docs/raw/docs/legacy/styled/Scrollbar.mdx +71 -0
- package/.docs/raw/docs/legacy/styled/Thread.mdx +84 -0
- package/.docs/raw/docs/legacy/styled/ThreadWidth.mdx +21 -0
- package/.docs/raw/docs/mcp-docs-server.mdx +324 -0
- package/.docs/raw/docs/migrations/deprecation-policy.mdx +41 -0
- package/.docs/raw/docs/migrations/v0-7.mdx +188 -0
- package/.docs/raw/docs/migrations/v0-8.mdx +160 -0
- package/.docs/raw/docs/migrations/v0-9.mdx +75 -0
- package/.docs/raw/docs/react-compatibility.mdx +208 -0
- package/.docs/raw/docs/runtimes/ai-sdk/rsc.mdx +226 -0
- package/.docs/raw/docs/runtimes/ai-sdk/use-assistant-hook.mdx +195 -0
- package/.docs/raw/docs/runtimes/ai-sdk/use-chat-hook.mdx +138 -0
- package/.docs/raw/docs/runtimes/ai-sdk/use-chat.mdx +136 -0
- package/.docs/raw/docs/runtimes/custom/external-store.mdx +1624 -0
- package/.docs/raw/docs/runtimes/custom/local.mdx +1185 -0
- package/.docs/raw/docs/runtimes/helicone.mdx +60 -0
- package/.docs/raw/docs/runtimes/langgraph/index.mdx +320 -0
- package/.docs/raw/docs/runtimes/langgraph/tutorial/index.mdx +11 -0
- package/.docs/raw/docs/runtimes/langgraph/tutorial/introduction.mdx +28 -0
- package/.docs/raw/docs/runtimes/langgraph/tutorial/part-1.mdx +120 -0
- package/.docs/raw/docs/runtimes/langgraph/tutorial/part-2.mdx +336 -0
- package/.docs/raw/docs/runtimes/langgraph/tutorial/part-3.mdx +385 -0
- package/.docs/raw/docs/runtimes/langserve.mdx +126 -0
- package/.docs/raw/docs/runtimes/mastra/full-stack-integration.mdx +218 -0
- package/.docs/raw/docs/runtimes/mastra/overview.mdx +17 -0
- package/.docs/raw/docs/runtimes/mastra/separate-server-integration.mdx +196 -0
- package/.docs/raw/docs/runtimes/pick-a-runtime.mdx +222 -0
- package/.docs/raw/docs/ui/AssistantModal.mdx +46 -0
- package/.docs/raw/docs/ui/AssistantSidebar.mdx +42 -0
- package/.docs/raw/docs/ui/Attachment.mdx +82 -0
- package/.docs/raw/docs/ui/Markdown.mdx +72 -0
- package/.docs/raw/docs/ui/Mermaid.mdx +79 -0
- package/.docs/raw/docs/ui/Scrollbar.mdx +59 -0
- package/.docs/raw/docs/ui/SyntaxHighlighting.mdx +253 -0
- package/.docs/raw/docs/ui/Thread.mdx +47 -0
- package/.docs/raw/docs/ui/ThreadList.mdx +49 -0
- package/.docs/raw/docs/ui/ToolFallback.mdx +64 -0
- package/.docs/raw/docs/ui/primitives/Thread.mdx +197 -0
- package/LICENSE +21 -0
- package/README.md +128 -0
- package/dist/chunk-C7O7EFKU.js +38 -0
- package/dist/chunk-CZCDQ3YH.js +420 -0
- package/dist/index.js +1 -0
- package/dist/prepare-docs/prepare.js +199 -0
- package/dist/stdio.js +8 -0
- package/package.json +43 -0
|
@@ -0,0 +1,663 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Generative UI
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
import { ToolUISample } from "../../../components/samples/tool-ui-sample";
|
|
6
|
+
import { Steps, Step } from "fumadocs-ui/components/steps";
|
|
7
|
+
import { Callout } from "fumadocs-ui/components/callout";
|
|
8
|
+
|
|
9
|
+
Create custom UI components for AI tool calls, providing visual feedback and interactive experiences when tools are executed.
|
|
10
|
+
|
|
11
|
+
<ToolUISample />
|
|
12
|
+
|
|
13
|
+
## Overview
|
|
14
|
+
|
|
15
|
+
Tool UIs in assistant-ui allow you to create custom interfaces that appear when AI tools are called. These generative UI components enhance the user experience by:
|
|
16
|
+
|
|
17
|
+
- **Visualizing tool execution** with loading states and progress indicators
|
|
18
|
+
- **Displaying results** in rich, formatted layouts
|
|
19
|
+
- **Enabling user interaction** through forms and controls
|
|
20
|
+
- **Providing error feedback** with helpful recovery options
|
|
21
|
+
|
|
22
|
+
This guide demonstrates building tool UIs with the **Vercel AI SDK**.
|
|
23
|
+
|
|
24
|
+
## Creating Tool UIs
|
|
25
|
+
|
|
26
|
+
There are two main approaches to creating tool UIs in assistant-ui:
|
|
27
|
+
|
|
28
|
+
### 1. Client-Defined Tools (`makeAssistantTool`)
|
|
29
|
+
|
|
30
|
+
If you're creating tools on the client side, use `makeAssistantTool` to register them with the assistant context. Then create a UI component with `makeAssistantToolUI`:
|
|
31
|
+
|
|
32
|
+
```tsx
|
|
33
|
+
import { makeAssistantTool, tool } from "@assistant-ui/react";
|
|
34
|
+
import { z } from "zod";
|
|
35
|
+
|
|
36
|
+
// Define the tool
|
|
37
|
+
const weatherTool = tool({
|
|
38
|
+
description: "Get current weather for a location",
|
|
39
|
+
parameters: z.object({
|
|
40
|
+
location: z.string(),
|
|
41
|
+
unit: z.enum(["celsius", "fahrenheit"]),
|
|
42
|
+
}),
|
|
43
|
+
execute: async ({ location, unit }) => {
|
|
44
|
+
const weather = await fetchWeatherAPI(location, unit);
|
|
45
|
+
return weather;
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Register the tool
|
|
50
|
+
const WeatherTool = makeAssistantTool(weatherTool);
|
|
51
|
+
|
|
52
|
+
// Create the UI
|
|
53
|
+
const WeatherToolUI = makeAssistantToolUI<
|
|
54
|
+
{ location: string; unit: "celsius" | "fahrenheit" },
|
|
55
|
+
{ temperature: number; description: string }
|
|
56
|
+
>({
|
|
57
|
+
toolName: "getWeather",
|
|
58
|
+
render: ({ args, result, status }) => {
|
|
59
|
+
if (status.type === "running") {
|
|
60
|
+
return <div>Checking weather in {args.location}...</div>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
<div className="weather-card">
|
|
65
|
+
<h3>{args.location}</h3>
|
|
66
|
+
<p>
|
|
67
|
+
{result.temperature}°{args.unit === "celsius" ? "C" : "F"}
|
|
68
|
+
</p>
|
|
69
|
+
<p>{result.description}</p>
|
|
70
|
+
</div>
|
|
71
|
+
);
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
<Callout type="tip">
|
|
77
|
+
Tools defined with `makeAssistantTool` can be passed to your backend using the `frontendTools` utility
|
|
78
|
+
</Callout>
|
|
79
|
+
|
|
80
|
+
Learn more about creating tools in the [Tools Guide](/docs/guides/Tools).
|
|
81
|
+
|
|
82
|
+
### 2. UI-Only for Existing Tools (`makeAssistantToolUI`)
|
|
83
|
+
|
|
84
|
+
If your tool is defined elsewhere (e.g., in your backend API, MCP server, or LangGraph), use `makeAssistantToolUI` to create just the UI component:
|
|
85
|
+
|
|
86
|
+
```tsx
|
|
87
|
+
import { makeAssistantToolUI } from "@assistant-ui/react";
|
|
88
|
+
|
|
89
|
+
const WeatherToolUI = makeAssistantToolUI<
|
|
90
|
+
{ location: string; unit: "celsius" | "fahrenheit" },
|
|
91
|
+
{ temperature: number; description: string }
|
|
92
|
+
>({
|
|
93
|
+
toolName: "getWeather", // Must match the backend tool name
|
|
94
|
+
render: ({ args, result, status }) => {
|
|
95
|
+
// UI rendering logic only
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Quick Start Example
|
|
101
|
+
|
|
102
|
+
This example shows how to implement the UI-only approach using `makeAssistantToolUI`:
|
|
103
|
+
|
|
104
|
+
<Steps>
|
|
105
|
+
<Step>
|
|
106
|
+
|
|
107
|
+
### Create a Tool UI Component
|
|
108
|
+
|
|
109
|
+
```tsx
|
|
110
|
+
import { makeAssistantToolUI } from "@assistant-ui/react";
|
|
111
|
+
import { z } from "zod";
|
|
112
|
+
|
|
113
|
+
type WeatherArgs = {
|
|
114
|
+
location: string;
|
|
115
|
+
unit: "celsius" | "fahrenheit";
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
type WeatherResult = {
|
|
119
|
+
temperature: number;
|
|
120
|
+
description: string;
|
|
121
|
+
humidity: number;
|
|
122
|
+
windSpeed: number;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const WeatherToolUI = makeAssistantToolUI<WeatherArgs, WeatherResult>({
|
|
126
|
+
toolName: "getWeather",
|
|
127
|
+
render: ({ args, status, result }) => {
|
|
128
|
+
if (status.type === "running") {
|
|
129
|
+
return (
|
|
130
|
+
<div className="flex items-center gap-2">
|
|
131
|
+
<Spinner />
|
|
132
|
+
<span>Checking weather in {args.location}...</span>
|
|
133
|
+
</div>
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (status.type === "incomplete" && status.reason === "error") {
|
|
138
|
+
return (
|
|
139
|
+
<div className="text-red-500">
|
|
140
|
+
Failed to get weather for {args.location}
|
|
141
|
+
</div>
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return (
|
|
146
|
+
<div className="weather-card rounded-lg bg-blue-50 p-4">
|
|
147
|
+
<h3 className="text-lg font-bold">{args.location}</h3>
|
|
148
|
+
<div className="mt-2 grid grid-cols-2 gap-4">
|
|
149
|
+
<div>
|
|
150
|
+
<p className="text-2xl">
|
|
151
|
+
{result.temperature}°{args.unit === "celsius" ? "C" : "F"}
|
|
152
|
+
</p>
|
|
153
|
+
<p className="text-gray-600">{result.description}</p>
|
|
154
|
+
</div>
|
|
155
|
+
<div className="text-sm">
|
|
156
|
+
<p>Humidity: {result.humidity}%</p>
|
|
157
|
+
<p>Wind: {result.windSpeed} km/h</p>
|
|
158
|
+
</div>
|
|
159
|
+
</div>
|
|
160
|
+
</div>
|
|
161
|
+
);
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
</Step>
|
|
167
|
+
<Step>
|
|
168
|
+
|
|
169
|
+
### Register the Tool UI
|
|
170
|
+
|
|
171
|
+
Place the component inside your `AssistantRuntimeProvider`:
|
|
172
|
+
|
|
173
|
+
```tsx
|
|
174
|
+
function App() {
|
|
175
|
+
return (
|
|
176
|
+
<AssistantRuntimeProvider runtime={runtime}>
|
|
177
|
+
<Thread />
|
|
178
|
+
<WeatherToolUI />
|
|
179
|
+
</AssistantRuntimeProvider>
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
</Step>
|
|
185
|
+
<Step>
|
|
186
|
+
|
|
187
|
+
### Define the Backend Tool (Vercel AI SDK)
|
|
188
|
+
|
|
189
|
+
When using the Vercel AI SDK, define the corresponding tool in your API route:
|
|
190
|
+
|
|
191
|
+
```tsx title="/app/api/chat/route.ts"
|
|
192
|
+
import { streamText, tool } from "ai";
|
|
193
|
+
import { z } from "zod";
|
|
194
|
+
|
|
195
|
+
export async function POST(req: Request) {
|
|
196
|
+
const { messages } = await req.json();
|
|
197
|
+
|
|
198
|
+
const result = streamText({
|
|
199
|
+
model: openai("gpt-4o"),
|
|
200
|
+
messages,
|
|
201
|
+
tools: {
|
|
202
|
+
getWeather: tool({
|
|
203
|
+
description: "Get current weather for a location",
|
|
204
|
+
parameters: z.object({
|
|
205
|
+
location: z.string(),
|
|
206
|
+
unit: z.enum(["celsius", "fahrenheit"]),
|
|
207
|
+
}),
|
|
208
|
+
execute: async ({ location, unit }) => {
|
|
209
|
+
const weather = await fetchWeatherAPI(location);
|
|
210
|
+
return {
|
|
211
|
+
temperature: weather.temp,
|
|
212
|
+
description: weather.condition,
|
|
213
|
+
humidity: weather.humidity,
|
|
214
|
+
windSpeed: weather.wind,
|
|
215
|
+
};
|
|
216
|
+
},
|
|
217
|
+
}),
|
|
218
|
+
},
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
return result.toDataStreamResponse();
|
|
222
|
+
}
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
</Step>
|
|
226
|
+
</Steps>
|
|
227
|
+
|
|
228
|
+
## Tool UI Patterns
|
|
229
|
+
|
|
230
|
+
### Component Pattern
|
|
231
|
+
|
|
232
|
+
Create standalone tool UI components:
|
|
233
|
+
|
|
234
|
+
```tsx
|
|
235
|
+
export const WebSearchToolUI = makeAssistantToolUI<
|
|
236
|
+
{ query: string },
|
|
237
|
+
{ results: SearchResult[] }
|
|
238
|
+
>({
|
|
239
|
+
toolName: "webSearch",
|
|
240
|
+
render: ({ args, status, result }) => {
|
|
241
|
+
return (
|
|
242
|
+
<div className="search-container">
|
|
243
|
+
<div className="mb-3 flex items-center gap-2">
|
|
244
|
+
<SearchIcon />
|
|
245
|
+
<span>Search results for: "{args.query}"</span>
|
|
246
|
+
</div>
|
|
247
|
+
|
|
248
|
+
{status.type === "running" && <LoadingSpinner />}
|
|
249
|
+
|
|
250
|
+
{result && (
|
|
251
|
+
<div className="space-y-2">
|
|
252
|
+
{result.results.map((item, index) => (
|
|
253
|
+
<div key={index} className="rounded border p-3">
|
|
254
|
+
<a href={item.url} className="font-medium text-blue-600">
|
|
255
|
+
{item.title}
|
|
256
|
+
</a>
|
|
257
|
+
<p className="text-sm text-gray-600">{item.snippet}</p>
|
|
258
|
+
</div>
|
|
259
|
+
))}
|
|
260
|
+
</div>
|
|
261
|
+
)}
|
|
262
|
+
</div>
|
|
263
|
+
);
|
|
264
|
+
},
|
|
265
|
+
});
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### Hook Pattern
|
|
269
|
+
|
|
270
|
+
Use hooks for dynamic tool UI registration:
|
|
271
|
+
|
|
272
|
+
<Callout type="tip">
|
|
273
|
+
When you assign your `makeAssistantToolUI({...})` call to a constant starting with `use…`, you can call it directly as a hook inside your component. This pattern lets you access local props or state when rendering the tool UI.
|
|
274
|
+
</Callout>
|
|
275
|
+
|
|
276
|
+
```tsx
|
|
277
|
+
import { useAssistantToolUI } from "@assistant-ui/react";
|
|
278
|
+
|
|
279
|
+
function DynamicToolUI() {
|
|
280
|
+
const [theme, setTheme] = useState("light");
|
|
281
|
+
|
|
282
|
+
useAssistantToolUI({
|
|
283
|
+
toolName: "analyzeData",
|
|
284
|
+
render: ({ args, result, status }) => {
|
|
285
|
+
// Hook allows access to component state
|
|
286
|
+
return (
|
|
287
|
+
<DataVisualization
|
|
288
|
+
data={result}
|
|
289
|
+
theme={theme}
|
|
290
|
+
loading={status.type === "running"}
|
|
291
|
+
/>
|
|
292
|
+
);
|
|
293
|
+
},
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
### Inline Pattern
|
|
301
|
+
|
|
302
|
+
For tools that need access to parent component props:
|
|
303
|
+
|
|
304
|
+
<Callout type="tip">
|
|
305
|
+
**Why `useInlineRender`?**
|
|
306
|
+
By default, a tool UI's `render` function is static. Use `useInlineRender` when your UI needs access to dynamic component props (for example, to pass in an `id` or other contextual data).
|
|
307
|
+
</Callout>
|
|
308
|
+
|
|
309
|
+
```tsx
|
|
310
|
+
import { useAssistantToolUI, useInlineRender } from "@assistant-ui/react";
|
|
311
|
+
|
|
312
|
+
function ProductPage({ productId, productName }) {
|
|
313
|
+
useAssistantToolUI({
|
|
314
|
+
toolName: "checkInventory",
|
|
315
|
+
render: useInlineRender(({ args, result }) => {
|
|
316
|
+
// Access parent component props
|
|
317
|
+
return (
|
|
318
|
+
<div className="inventory-status">
|
|
319
|
+
<h4>{productName} Inventory</h4>
|
|
320
|
+
<p>
|
|
321
|
+
Stock for {productId}: {result.quantity} units
|
|
322
|
+
</p>
|
|
323
|
+
<p>Location: {result.warehouse}</p>
|
|
324
|
+
</div>
|
|
325
|
+
);
|
|
326
|
+
}),
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
return <div>Product details...</div>;
|
|
330
|
+
}
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
## Interactive Tool UIs
|
|
334
|
+
|
|
335
|
+
### User Input Collection
|
|
336
|
+
|
|
337
|
+
Create tools that collect user input during execution:
|
|
338
|
+
|
|
339
|
+
<Callout type="tip">
|
|
340
|
+
**Pro tip:** Call `addResult(...)` exactly once to complete the tool call. After it's invoked, the assistant will resume the conversation with your provided data.
|
|
341
|
+
</Callout>
|
|
342
|
+
|
|
343
|
+
```tsx
|
|
344
|
+
const DatePickerToolUI = makeAssistantToolUI<
|
|
345
|
+
{ prompt: string },
|
|
346
|
+
{ date: string }
|
|
347
|
+
>({
|
|
348
|
+
toolName: "selectDate",
|
|
349
|
+
render: ({ args, result, addResult }) => {
|
|
350
|
+
if (result) {
|
|
351
|
+
return (
|
|
352
|
+
<div className="rounded bg-green-50 p-3">
|
|
353
|
+
✅ Selected date: {new Date(result.date).toLocaleDateString()}
|
|
354
|
+
</div>
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
return (
|
|
359
|
+
<div className="rounded border p-4">
|
|
360
|
+
<p className="mb-3">{args.prompt}</p>
|
|
361
|
+
<DatePicker
|
|
362
|
+
onChange={(date) => {
|
|
363
|
+
addResult({ date: date.toISOString() });
|
|
364
|
+
}}
|
|
365
|
+
/>
|
|
366
|
+
</div>
|
|
367
|
+
);
|
|
368
|
+
},
|
|
369
|
+
});
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
### Multi-Step Interactions
|
|
373
|
+
|
|
374
|
+
Build complex workflows with multiple user interactions:
|
|
375
|
+
|
|
376
|
+
```tsx
|
|
377
|
+
const ApprovalToolUI = makeAssistantToolUI<
|
|
378
|
+
{ action: string; details: any },
|
|
379
|
+
{ approved: boolean; reason?: string }
|
|
380
|
+
>({
|
|
381
|
+
toolName: "requestApproval",
|
|
382
|
+
render: ({ args, result, addResult }) => {
|
|
383
|
+
const [reason, setReason] = useState("");
|
|
384
|
+
|
|
385
|
+
if (result) {
|
|
386
|
+
return (
|
|
387
|
+
<div className={result.approved ? "text-green-600" : "text-red-600"}>
|
|
388
|
+
{result.approved ? "✅ Approved" : `❌ Rejected: ${result.reason}`}
|
|
389
|
+
</div>
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
return (
|
|
394
|
+
<div className="rounded border-2 border-yellow-400 p-4">
|
|
395
|
+
<h4 className="font-bold">Approval Required</h4>
|
|
396
|
+
<p className="my-2">{args.action}</p>
|
|
397
|
+
<pre className="rounded bg-gray-100 p-2 text-sm">
|
|
398
|
+
{JSON.stringify(args.details, null, 2)}
|
|
399
|
+
</pre>
|
|
400
|
+
|
|
401
|
+
<div className="mt-4 flex gap-2">
|
|
402
|
+
<button
|
|
403
|
+
onClick={() => addResult({ approved: true })}
|
|
404
|
+
className="rounded bg-green-500 px-4 py-2 text-white"
|
|
405
|
+
>
|
|
406
|
+
Approve
|
|
407
|
+
</button>
|
|
408
|
+
<button
|
|
409
|
+
onClick={() => addResult({ approved: false, reason })}
|
|
410
|
+
className="rounded bg-red-500 px-4 py-2 text-white"
|
|
411
|
+
>
|
|
412
|
+
Reject
|
|
413
|
+
</button>
|
|
414
|
+
<input
|
|
415
|
+
type="text"
|
|
416
|
+
placeholder="Rejection reason..."
|
|
417
|
+
value={reason}
|
|
418
|
+
onChange={(e) => setReason(e.target.value)}
|
|
419
|
+
className="flex-1 rounded border px-2"
|
|
420
|
+
/>
|
|
421
|
+
</div>
|
|
422
|
+
</div>
|
|
423
|
+
);
|
|
424
|
+
},
|
|
425
|
+
});
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
## Advanced Features
|
|
429
|
+
|
|
430
|
+
### Tool Status Handling
|
|
431
|
+
|
|
432
|
+
The `status` prop provides detailed execution state:
|
|
433
|
+
|
|
434
|
+
```tsx
|
|
435
|
+
render: ({ status, args }) => {
|
|
436
|
+
switch (status.type) {
|
|
437
|
+
case "running":
|
|
438
|
+
return <LoadingState />;
|
|
439
|
+
|
|
440
|
+
case "requires-action":
|
|
441
|
+
return <UserInputRequired reason={status.reason} />;
|
|
442
|
+
|
|
443
|
+
case "incomplete":
|
|
444
|
+
if (status.reason === "cancelled") {
|
|
445
|
+
return <div>Operation cancelled</div>;
|
|
446
|
+
}
|
|
447
|
+
if (status.reason === "error") {
|
|
448
|
+
return <ErrorDisplay error={status.error} />;
|
|
449
|
+
}
|
|
450
|
+
return <div>Failed: {status.reason}</div>;
|
|
451
|
+
|
|
452
|
+
case "complete":
|
|
453
|
+
return <SuccessDisplay />;
|
|
454
|
+
}
|
|
455
|
+
};
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
### Field-Level Validation
|
|
459
|
+
|
|
460
|
+
Use `useToolArgsFieldStatus` to show validation states:
|
|
461
|
+
|
|
462
|
+
```tsx
|
|
463
|
+
import { useToolArgsFieldStatus } from "@assistant-ui/react";
|
|
464
|
+
|
|
465
|
+
const FormToolUI = makeAssistantToolUI({
|
|
466
|
+
toolName: "submitForm",
|
|
467
|
+
render: ({ args }) => {
|
|
468
|
+
const emailStatus = useToolArgsFieldStatus("email");
|
|
469
|
+
const phoneStatus = useToolArgsFieldStatus("phone");
|
|
470
|
+
|
|
471
|
+
return (
|
|
472
|
+
<form className="space-y-4">
|
|
473
|
+
<div>
|
|
474
|
+
<input
|
|
475
|
+
type="email"
|
|
476
|
+
value={args.email}
|
|
477
|
+
className={emailStatus.type === "running" ? "loading" : ""}
|
|
478
|
+
disabled
|
|
479
|
+
/>
|
|
480
|
+
{emailStatus.type === "incomplete" && (
|
|
481
|
+
<span className="text-red-500">Invalid email</span>
|
|
482
|
+
)}
|
|
483
|
+
</div>
|
|
484
|
+
|
|
485
|
+
<div>
|
|
486
|
+
<input
|
|
487
|
+
type="tel"
|
|
488
|
+
value={args.phone}
|
|
489
|
+
className={phoneStatus.type === "running" ? "loading" : ""}
|
|
490
|
+
disabled
|
|
491
|
+
/>
|
|
492
|
+
</div>
|
|
493
|
+
</form>
|
|
494
|
+
);
|
|
495
|
+
},
|
|
496
|
+
});
|
|
497
|
+
```
|
|
498
|
+
|
|
499
|
+
### Partial Results & Streaming
|
|
500
|
+
|
|
501
|
+
Display results as they stream in:
|
|
502
|
+
|
|
503
|
+
```tsx
|
|
504
|
+
const AnalysisToolUI = makeAssistantToolUI<
|
|
505
|
+
{ data: string },
|
|
506
|
+
{ progress: number; insights: string[] }
|
|
507
|
+
>({
|
|
508
|
+
toolName: "analyzeData",
|
|
509
|
+
render: ({ result, status }) => {
|
|
510
|
+
const progress = result?.progress || 0;
|
|
511
|
+
const insights = result?.insights || [];
|
|
512
|
+
|
|
513
|
+
return (
|
|
514
|
+
<div className="analysis-container">
|
|
515
|
+
{status.type === "running" && (
|
|
516
|
+
<div className="mb-4">
|
|
517
|
+
<div className="mb-1 flex justify-between">
|
|
518
|
+
<span>Analyzing...</span>
|
|
519
|
+
<span>{progress}%</span>
|
|
520
|
+
</div>
|
|
521
|
+
<div className="w-full rounded bg-gray-200">
|
|
522
|
+
<div
|
|
523
|
+
className="h-2 rounded bg-blue-500"
|
|
524
|
+
style={{ width: `${progress}%` }}
|
|
525
|
+
/>
|
|
526
|
+
</div>
|
|
527
|
+
</div>
|
|
528
|
+
)}
|
|
529
|
+
|
|
530
|
+
<div className="space-y-2">
|
|
531
|
+
{insights.map((insight, i) => (
|
|
532
|
+
<div key={i} className="rounded bg-gray-50 p-2">
|
|
533
|
+
{insight}
|
|
534
|
+
</div>
|
|
535
|
+
))}
|
|
536
|
+
</div>
|
|
537
|
+
</div>
|
|
538
|
+
);
|
|
539
|
+
},
|
|
540
|
+
});
|
|
541
|
+
```
|
|
542
|
+
|
|
543
|
+
### Custom Tool Fallback
|
|
544
|
+
|
|
545
|
+
Provide a custom UI for tools without specific UIs:
|
|
546
|
+
|
|
547
|
+
```tsx
|
|
548
|
+
<Thread
|
|
549
|
+
components={{
|
|
550
|
+
ToolFallback: ({ toolName, args, result }) => (
|
|
551
|
+
<div className="tool-fallback rounded bg-gray-100 p-3">
|
|
552
|
+
<code className="text-sm">
|
|
553
|
+
{toolName}({JSON.stringify(args)})
|
|
554
|
+
</code>
|
|
555
|
+
{result && (
|
|
556
|
+
<pre className="mt-2 text-xs">{JSON.stringify(result, null, 2)}</pre>
|
|
557
|
+
)}
|
|
558
|
+
</div>
|
|
559
|
+
),
|
|
560
|
+
}}
|
|
561
|
+
/>
|
|
562
|
+
```
|
|
563
|
+
|
|
564
|
+
## Execution Context
|
|
565
|
+
|
|
566
|
+
Generative UI components have access to execution context through props:
|
|
567
|
+
|
|
568
|
+
```tsx
|
|
569
|
+
type ToolUIRenderProps<TArgs, TResult> = {
|
|
570
|
+
// Tool arguments
|
|
571
|
+
args: TArgs;
|
|
572
|
+
argsText: string; // JSON stringified args
|
|
573
|
+
|
|
574
|
+
// Execution status
|
|
575
|
+
status: ToolCallContentPartStatus;
|
|
576
|
+
isError?: boolean;
|
|
577
|
+
|
|
578
|
+
// Tool result (may be partial during streaming)
|
|
579
|
+
result?: TResult;
|
|
580
|
+
|
|
581
|
+
// Tool metadata
|
|
582
|
+
toolName: string;
|
|
583
|
+
toolCallId: string;
|
|
584
|
+
|
|
585
|
+
// Interactive callback
|
|
586
|
+
addResult: (result: TResult) => void;
|
|
587
|
+
|
|
588
|
+
// Optional artifact data
|
|
589
|
+
artifact?: unknown;
|
|
590
|
+
};
|
|
591
|
+
```
|
|
592
|
+
|
|
593
|
+
## Best Practices
|
|
594
|
+
|
|
595
|
+
### 1. Handle All Status States
|
|
596
|
+
|
|
597
|
+
Always handle loading, error, and success states:
|
|
598
|
+
|
|
599
|
+
```tsx
|
|
600
|
+
render: ({ status, result, args }) => {
|
|
601
|
+
if (status.type === "running") return <Skeleton />;
|
|
602
|
+
if (status.type === "incomplete") return <ErrorState />;
|
|
603
|
+
if (!result) return null;
|
|
604
|
+
return <ResultDisplay result={result} />;
|
|
605
|
+
};
|
|
606
|
+
```
|
|
607
|
+
|
|
608
|
+
### 2. Provide Visual Feedback
|
|
609
|
+
|
|
610
|
+
Use animations and transitions for better UX:
|
|
611
|
+
|
|
612
|
+
```tsx
|
|
613
|
+
<div
|
|
614
|
+
className={cn(
|
|
615
|
+
"transition-all duration-300",
|
|
616
|
+
status.type === "running" && "opacity-50",
|
|
617
|
+
status.type === "complete" && "opacity-100",
|
|
618
|
+
)}
|
|
619
|
+
>
|
|
620
|
+
{/* Tool UI content */}
|
|
621
|
+
</div>
|
|
622
|
+
```
|
|
623
|
+
|
|
624
|
+
### 3. Make UIs Accessible
|
|
625
|
+
|
|
626
|
+
Ensure keyboard navigation and screen reader support:
|
|
627
|
+
|
|
628
|
+
```tsx
|
|
629
|
+
<button
|
|
630
|
+
onClick={() => addResult(value)}
|
|
631
|
+
aria-label="Confirm selection"
|
|
632
|
+
className="focus:outline-none focus:ring-2"
|
|
633
|
+
>
|
|
634
|
+
Confirm
|
|
635
|
+
</button>
|
|
636
|
+
```
|
|
637
|
+
|
|
638
|
+
### 4. Optimize Performance
|
|
639
|
+
|
|
640
|
+
Use `useInlineRender` to prevent unnecessary re-renders:
|
|
641
|
+
|
|
642
|
+
```tsx
|
|
643
|
+
useAssistantToolUI({
|
|
644
|
+
toolName: "heavyComputation",
|
|
645
|
+
render: useInlineRender(({ result }) => {
|
|
646
|
+
// Expensive rendering logic
|
|
647
|
+
return <ComplexVisualization data={result} />;
|
|
648
|
+
}),
|
|
649
|
+
});
|
|
650
|
+
```
|
|
651
|
+
|
|
652
|
+
<Callout>
|
|
653
|
+
Generative UI components are only displayed in the chat interface. The actual
|
|
654
|
+
tool execution happens on the backend. This separation allows you to create
|
|
655
|
+
rich, interactive experiences while keeping sensitive logic secure on the
|
|
656
|
+
server.
|
|
657
|
+
</Callout>
|
|
658
|
+
|
|
659
|
+
## Related Guides
|
|
660
|
+
|
|
661
|
+
- [Tools Guide](/docs/guides/Tools) - Learn how to create and use tools with AI models
|
|
662
|
+
- [Tool Fallback](/docs/ui/ToolFallback) - Default UI for tools without custom components
|
|
663
|
+
- [API Reference](/docs/api-reference/primitives/ContentPart) - Detailed type definitions and component APIs
|