@assistant-ui/mcp-docs-server 0.1.19 → 0.1.21
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/with-ag-ui.md +172 -1633
- package/.docs/organized/code-examples/with-ai-sdk-v6.md +42 -1640
- package/.docs/organized/code-examples/with-assistant-transport.md +40 -1743
- package/.docs/organized/code-examples/with-cloud.md +71 -1745
- package/.docs/organized/code-examples/with-custom-thread-list.md +87 -1723
- package/.docs/organized/code-examples/with-elevenlabs-scribe.md +70 -1637
- package/.docs/organized/code-examples/with-external-store.md +67 -1624
- package/.docs/organized/code-examples/with-ffmpeg.md +71 -1629
- package/.docs/organized/code-examples/with-langgraph.md +95 -1893
- package/.docs/organized/code-examples/with-parent-id-grouping.md +57 -1654
- package/.docs/organized/code-examples/with-react-hook-form.md +220 -2163
- package/.docs/organized/code-examples/with-react-router.md +66 -1318
- package/.docs/organized/code-examples/with-store.md +31 -31
- package/.docs/organized/code-examples/with-tanstack.md +77 -861
- package/.docs/organized/code-examples/with-tap-runtime.md +812 -0
- package/.docs/raw/docs/(docs)/cli.mdx +66 -0
- package/.docs/raw/docs/(docs)/copilots/make-assistant-tool-ui.mdx +0 -1
- package/.docs/raw/docs/(docs)/copilots/make-assistant-tool.mdx +0 -1
- package/.docs/raw/docs/(docs)/copilots/model-context.mdx +4 -4
- package/.docs/raw/docs/(docs)/copilots/motivation.mdx +3 -3
- package/.docs/raw/docs/(docs)/devtools.mdx +0 -1
- package/.docs/raw/docs/(docs)/guides/attachments.mdx +2 -3
- package/.docs/raw/docs/(docs)/guides/context-api.mdx +117 -117
- package/.docs/raw/docs/(docs)/guides/suggestions.mdx +296 -0
- package/.docs/raw/docs/(docs)/guides/tools.mdx +336 -513
- package/.docs/raw/docs/(docs)/index.mdx +33 -410
- package/.docs/raw/docs/(docs)/installation.mdx +450 -0
- package/.docs/raw/docs/(docs)/llm.mdx +209 -0
- package/.docs/raw/docs/(reference)/api-reference/context-providers/assistant-runtime-provider.mdx +0 -1
- package/.docs/raw/docs/(reference)/api-reference/context-providers/text-message-part-provider.mdx +0 -1
- package/.docs/raw/docs/(reference)/api-reference/integrations/react-data-stream.mdx +48 -3
- package/.docs/raw/docs/(reference)/api-reference/integrations/react-hook-form.mdx +0 -1
- package/.docs/raw/docs/(reference)/api-reference/integrations/vercel-ai-sdk.mdx +0 -1
- package/.docs/raw/docs/(reference)/api-reference/overview.mdx +9 -3
- package/.docs/raw/docs/(reference)/api-reference/primitives/action-bar-more.mdx +20 -52
- package/.docs/raw/docs/(reference)/api-reference/primitives/action-bar.mdx +16 -39
- package/.docs/raw/docs/(reference)/api-reference/primitives/assistant-if.mdx +49 -50
- package/.docs/raw/docs/(reference)/api-reference/primitives/assistant-modal.mdx +3 -11
- package/.docs/raw/docs/(reference)/api-reference/primitives/attachment.mdx +0 -3
- package/.docs/raw/docs/(reference)/api-reference/primitives/branch-picker.mdx +0 -1
- package/.docs/raw/docs/(reference)/api-reference/primitives/composer.mdx +5 -16
- package/.docs/raw/docs/(reference)/api-reference/primitives/composition.mdx +0 -1
- package/.docs/raw/docs/(reference)/api-reference/primitives/error.mdx +0 -1
- package/.docs/raw/docs/(reference)/api-reference/primitives/message-part.mdx +1 -2
- package/.docs/raw/docs/(reference)/api-reference/primitives/message.mdx +0 -1
- package/.docs/raw/docs/(reference)/api-reference/primitives/suggestion.mdx +152 -0
- package/.docs/raw/docs/(reference)/api-reference/primitives/thread-list-item-more.mdx +0 -1
- package/.docs/raw/docs/(reference)/api-reference/primitives/thread-list-item.mdx +1 -2
- package/.docs/raw/docs/(reference)/api-reference/primitives/thread-list.mdx +1 -2
- package/.docs/raw/docs/(reference)/api-reference/primitives/thread.mdx +28 -40
- package/.docs/raw/docs/(reference)/api-reference/runtimes/assistant-runtime.mdx +0 -1
- package/.docs/raw/docs/(reference)/api-reference/runtimes/attachment-runtime.mdx +1 -2
- package/.docs/raw/docs/(reference)/api-reference/runtimes/composer-runtime.mdx +2 -3
- package/.docs/raw/docs/(reference)/api-reference/runtimes/message-part-runtime.mdx +1 -2
- package/.docs/raw/docs/(reference)/api-reference/runtimes/message-runtime.mdx +1 -2
- package/.docs/raw/docs/(reference)/api-reference/runtimes/thread-list-item-runtime.mdx +0 -1
- package/.docs/raw/docs/(reference)/api-reference/runtimes/thread-list-runtime.mdx +0 -1
- package/.docs/raw/docs/(reference)/api-reference/runtimes/thread-runtime.mdx +1 -2
- package/.docs/raw/docs/(reference)/legacy/styled/assistant-modal.mdx +0 -1
- package/.docs/raw/docs/(reference)/legacy/styled/decomposition.mdx +5 -5
- package/.docs/raw/docs/(reference)/legacy/styled/markdown.mdx +0 -1
- package/.docs/raw/docs/(reference)/legacy/styled/thread.mdx +0 -1
- package/.docs/raw/docs/(reference)/migrations/v0-12.mdx +207 -33
- package/.docs/raw/docs/(reference)/react-compatibility.mdx +0 -1
- package/.docs/raw/docs/cloud/persistence/ai-sdk.mdx +0 -1
- package/.docs/raw/docs/cloud/persistence/langgraph.mdx +0 -1
- package/.docs/raw/docs/runtimes/ai-sdk/v4-legacy.mdx +0 -1
- package/.docs/raw/docs/runtimes/ai-sdk/v5-legacy.mdx +118 -0
- package/.docs/raw/docs/runtimes/ai-sdk/v6.mdx +198 -0
- package/.docs/raw/docs/runtimes/assistant-transport.mdx +3 -3
- package/.docs/raw/docs/runtimes/custom/custom-thread-list.mdx +5 -6
- package/.docs/raw/docs/runtimes/custom/external-store.mdx +9 -11
- package/.docs/raw/docs/runtimes/custom/local.mdx +43 -36
- package/.docs/raw/docs/runtimes/data-stream.mdx +35 -3
- package/.docs/raw/docs/runtimes/langgraph/index.mdx +1 -2
- package/.docs/raw/docs/runtimes/langgraph/tutorial/part-3.mdx +0 -1
- package/.docs/raw/docs/runtimes/langserve.mdx +0 -1
- package/.docs/raw/docs/runtimes/mastra/full-stack-integration.mdx +0 -1
- package/.docs/raw/docs/runtimes/mastra/separate-server-integration.mdx +0 -1
- package/.docs/raw/docs/ui/accordion.mdx +259 -0
- package/.docs/raw/docs/ui/assistant-modal.mdx +1 -3
- package/.docs/raw/docs/ui/assistant-sidebar.mdx +1 -3
- package/.docs/raw/docs/ui/attachment.mdx +0 -2
- package/.docs/raw/docs/ui/badge.mdx +138 -0
- package/.docs/raw/docs/ui/diff-viewer.mdx +279 -0
- package/.docs/raw/docs/ui/file.mdx +152 -0
- package/.docs/raw/docs/ui/image.mdx +100 -0
- package/.docs/raw/docs/ui/markdown.mdx +0 -1
- package/.docs/raw/docs/ui/mermaid.mdx +0 -1
- package/.docs/raw/docs/ui/model-selector.mdx +224 -0
- package/.docs/raw/docs/ui/part-grouping.mdx +4 -5
- package/.docs/raw/docs/ui/reasoning.mdx +6 -5
- package/.docs/raw/docs/ui/scrollbar.mdx +26 -9
- package/.docs/raw/docs/ui/select.mdx +245 -0
- package/.docs/raw/docs/ui/sources.mdx +6 -5
- package/.docs/raw/docs/ui/streamdown.mdx +348 -0
- package/.docs/raw/docs/ui/syntax-highlighting.mdx +8 -63
- package/.docs/raw/docs/ui/tabs.mdx +259 -0
- package/.docs/raw/docs/ui/thread-list.mdx +98 -16
- package/.docs/raw/docs/ui/thread.mdx +57 -73
- package/.docs/raw/docs/ui/tool-fallback.mdx +0 -1
- package/.docs/raw/docs/ui/tool-group.mdx +1 -3
- package/README.md +3 -3
- package/package.json +4 -4
- package/src/tools/tests/examples.test.ts +1 -1
- package/.docs/raw/docs/(docs)/about-assistantui.mdx +0 -54
- package/.docs/raw/docs/(docs)/mcp-docs-server.mdx +0 -321
- package/.docs/raw/docs/runtimes/ai-sdk/use-chat.mdx +0 -219
|
@@ -50,10 +50,10 @@ assistant-ui organizes state into **scopes** - logical boundaries that provide a
|
|
|
50
50
|
// Inside a message component
|
|
51
51
|
function MessageButton() {
|
|
52
52
|
// ✅ Available: message scope (current message)
|
|
53
|
-
const role =
|
|
53
|
+
const role = useAuiState(({ message }) => message.role);
|
|
54
54
|
|
|
55
55
|
// ✅ Available: thread scope (parent)
|
|
56
|
-
const isRunning =
|
|
56
|
+
const isRunning = useAuiState(({ thread }) => thread.isRunning);
|
|
57
57
|
}
|
|
58
58
|
```
|
|
59
59
|
|
|
@@ -68,22 +68,22 @@ The Context API follows a predictable state management pattern:
|
|
|
68
68
|
|
|
69
69
|
## Essential Hooks
|
|
70
70
|
|
|
71
|
-
###
|
|
71
|
+
### useAuiState
|
|
72
72
|
|
|
73
73
|
Read state reactively with automatic re-renders when values change. This hook works like Zustand's selector pattern - you provide a function that extracts the specific data you need, and your component only re-renders when that data changes.
|
|
74
74
|
|
|
75
75
|
```tsx
|
|
76
|
-
import {
|
|
76
|
+
import { useAuiState } from "@assistant-ui/react";
|
|
77
77
|
|
|
78
78
|
// Basic usage - extract a single property
|
|
79
|
-
const role =
|
|
80
|
-
const isRunning =
|
|
79
|
+
const role = useAuiState(({ message }) => message.role); // "user" | "assistant"
|
|
80
|
+
const isRunning = useAuiState(({ thread }) => thread.isRunning); // boolean
|
|
81
81
|
|
|
82
82
|
// Access nested data
|
|
83
|
-
const attachmentCount =
|
|
83
|
+
const attachmentCount = useAuiState(
|
|
84
84
|
({ composer }) => composer.attachments.length,
|
|
85
85
|
);
|
|
86
|
-
const lastMessage =
|
|
86
|
+
const lastMessage = useAuiState(({ thread }) => thread.messages.at(-1));
|
|
87
87
|
```
|
|
88
88
|
|
|
89
89
|
The selector function receives all available scopes for your component's location and should return a specific value. The component re-renders only when that returned value changes.
|
|
@@ -92,54 +92,54 @@ The selector function receives all available scopes for your component's locatio
|
|
|
92
92
|
|
|
93
93
|
```tsx
|
|
94
94
|
// Access multiple scopes
|
|
95
|
-
const canSend =
|
|
95
|
+
const canSend = useAuiState(
|
|
96
96
|
({ thread, composer }) => !thread.isRunning && composer.text.length > 0,
|
|
97
97
|
);
|
|
98
98
|
|
|
99
99
|
// Compute derived state
|
|
100
|
-
const messageCount =
|
|
100
|
+
const messageCount = useAuiState(({ thread }) => thread.messages.length);
|
|
101
101
|
```
|
|
102
102
|
|
|
103
103
|
**Important:** Never create new objects in selectors. Return primitive values or stable references to avoid infinite re-renders.
|
|
104
104
|
|
|
105
105
|
```tsx
|
|
106
106
|
// ❌ Bad - creates new object every time
|
|
107
|
-
const data =
|
|
107
|
+
const data = useAuiState(({ message }) => ({
|
|
108
108
|
role: message.role,
|
|
109
109
|
content: message.content,
|
|
110
110
|
}));
|
|
111
111
|
|
|
112
112
|
// ✅ Good - returns stable values
|
|
113
|
-
const role =
|
|
114
|
-
const content =
|
|
113
|
+
const role = useAuiState(({ message }) => message.role);
|
|
114
|
+
const content = useAuiState(({ message }) => message.content);
|
|
115
115
|
```
|
|
116
116
|
|
|
117
|
-
###
|
|
117
|
+
### useAui
|
|
118
118
|
|
|
119
|
-
Access the API instance for imperative operations and actions. Unlike `
|
|
119
|
+
Access the API instance for imperative operations and actions. Unlike `useAuiState`, this hook returns a stable object that never changes, making it perfect for event handlers and imperative operations.
|
|
120
120
|
|
|
121
121
|
```tsx
|
|
122
|
-
import {
|
|
122
|
+
import { useAui } from "@assistant-ui/react";
|
|
123
123
|
|
|
124
124
|
function CustomMessageActions() {
|
|
125
|
-
const
|
|
125
|
+
const aui = useAui();
|
|
126
126
|
|
|
127
127
|
// Perform actions in event handlers
|
|
128
128
|
const handleSend = () => {
|
|
129
|
-
|
|
129
|
+
aui.composer().send();
|
|
130
130
|
};
|
|
131
131
|
|
|
132
132
|
const handleReload = () => {
|
|
133
|
-
|
|
133
|
+
aui.message().reload();
|
|
134
134
|
};
|
|
135
135
|
|
|
136
136
|
// Read state imperatively when needed
|
|
137
137
|
const handleConditionalAction = () => {
|
|
138
|
-
const { isRunning } =
|
|
139
|
-
const { text } =
|
|
138
|
+
const { isRunning } = aui.thread().getState();
|
|
139
|
+
const { text } = aui.composer().getState();
|
|
140
140
|
|
|
141
141
|
if (!isRunning && text.length > 0) {
|
|
142
|
-
|
|
142
|
+
aui.composer().send();
|
|
143
143
|
}
|
|
144
144
|
};
|
|
145
145
|
|
|
@@ -164,86 +164,86 @@ The API object is stable and doesn't cause re-renders. Use it for:
|
|
|
164
164
|
|
|
165
165
|
```tsx
|
|
166
166
|
// Thread actions
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
167
|
+
aui.thread().append(message);
|
|
168
|
+
aui.thread().startRun(config);
|
|
169
|
+
aui.thread().cancelRun();
|
|
170
|
+
aui.thread().switchToNewThread();
|
|
171
|
+
aui.thread().switchToThread(threadId);
|
|
172
|
+
aui.thread().getState();
|
|
173
|
+
aui.thread().message(idOrIndex);
|
|
174
|
+
aui.thread().composer;
|
|
175
175
|
|
|
176
176
|
// Message actions
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
177
|
+
aui.message().reload();
|
|
178
|
+
aui.message().speak();
|
|
179
|
+
aui.message().stopSpeaking();
|
|
180
|
+
aui.message().submitFeedback({ type: "positive" | "negative" });
|
|
181
|
+
aui.message().switchToBranch({ position, branchId });
|
|
182
|
+
aui.message().getState();
|
|
183
|
+
aui.message().part(indexOrToolCallId);
|
|
184
|
+
aui.message().composer;
|
|
185
185
|
|
|
186
186
|
// Part actions
|
|
187
187
|
api.part().addResult(result);
|
|
188
188
|
api.part().getState();
|
|
189
189
|
|
|
190
190
|
// Composer actions
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
191
|
+
aui.composer().send();
|
|
192
|
+
aui.composer().setText(text);
|
|
193
|
+
aui.composer().setRole(role);
|
|
194
|
+
aui.composer().addAttachment(file);
|
|
195
|
+
aui.composer().clearAttachments();
|
|
196
|
+
aui.composer().reset();
|
|
197
|
+
aui.composer().getState();
|
|
198
198
|
|
|
199
199
|
// Attachment actions
|
|
200
|
-
|
|
201
|
-
|
|
200
|
+
aui.attachment().remove();
|
|
201
|
+
aui.attachment().getState();
|
|
202
202
|
|
|
203
203
|
// ThreadList actions
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
204
|
+
aui.threads().switchToNewThread();
|
|
205
|
+
aui.threads().switchToThread(threadId);
|
|
206
|
+
aui.threads().getState();
|
|
207
207
|
|
|
208
208
|
// ThreadListItem actions
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
209
|
+
aui.threadListItem().switchTo();
|
|
210
|
+
aui.threadListItem().rename(title);
|
|
211
|
+
aui.threadListItem().archive();
|
|
212
|
+
aui.threadListItem().unarchive();
|
|
213
|
+
aui.threadListItem().delete();
|
|
214
|
+
aui.threads().getState();
|
|
215
215
|
|
|
216
216
|
// Tools actions
|
|
217
|
-
|
|
218
|
-
|
|
217
|
+
aui.tools().setToolUI(toolName, render);
|
|
218
|
+
aui.tools().getState();
|
|
219
219
|
```
|
|
220
220
|
|
|
221
|
-
###
|
|
221
|
+
### useAuiEvent
|
|
222
222
|
|
|
223
223
|
Subscribe to events with automatic cleanup on unmount. This hook is perfect for reacting to user interactions, system events, or integrating with external analytics.
|
|
224
224
|
|
|
225
225
|
```tsx
|
|
226
|
-
import {
|
|
226
|
+
import { useAuiEvent } from "@assistant-ui/react";
|
|
227
227
|
|
|
228
228
|
// Listen to current scope events (most common)
|
|
229
|
-
|
|
229
|
+
useAuiEvent("composer.send", (event) => {
|
|
230
230
|
console.log("Current composer sent message:", event.message);
|
|
231
231
|
});
|
|
232
232
|
|
|
233
233
|
// Listen to all events of a type across all scopes
|
|
234
|
-
|
|
234
|
+
useAuiEvent({ event: "composer.send", scope: "*" }, (event) => {
|
|
235
235
|
console.log("Any composer sent a message:", event);
|
|
236
236
|
});
|
|
237
237
|
|
|
238
238
|
// Listen to ALL events (useful for debugging or analytics)
|
|
239
|
-
|
|
239
|
+
useAuiEvent("*", (event) => {
|
|
240
240
|
console.log("Event occurred:", event.type, "from:", event.source);
|
|
241
241
|
// Send to analytics, logging, etc.
|
|
242
242
|
});
|
|
243
243
|
|
|
244
244
|
// Practical example: Track user interactions
|
|
245
245
|
function AnalyticsTracker() {
|
|
246
|
-
|
|
246
|
+
useAuiEvent("composer.send", (event) => {
|
|
247
247
|
analytics.track("message_sent", {
|
|
248
248
|
messageLength: event.message.content.length,
|
|
249
249
|
hasAttachments: event.message.attachments.length > 0,
|
|
@@ -256,7 +256,7 @@ function AnalyticsTracker() {
|
|
|
256
256
|
|
|
257
257
|
**Event name patterns:**
|
|
258
258
|
|
|
259
|
-
- Event names follow `source.action` format (e.g., `composer.send`, `thread.
|
|
259
|
+
- Event names follow `source.action` format in camelCase (e.g., `composer.send`, `thread.runStart`)
|
|
260
260
|
- Use `"*"` as the event name to listen to all events
|
|
261
261
|
- The `scope` parameter controls which instances trigger the event
|
|
262
262
|
|
|
@@ -281,11 +281,11 @@ The Context API automatically resolves the current scope based on component loca
|
|
|
281
281
|
|
|
282
282
|
```tsx
|
|
283
283
|
function MessageButton() {
|
|
284
|
-
const
|
|
284
|
+
const aui = useAui();
|
|
285
285
|
|
|
286
286
|
// Automatically uses the current message scope
|
|
287
287
|
const handleReload = () => {
|
|
288
|
-
|
|
288
|
+
aui.message().reload();
|
|
289
289
|
};
|
|
290
290
|
|
|
291
291
|
return <button onClick={handleReload}>Reload</button>;
|
|
@@ -297,12 +297,12 @@ function MessageButton() {
|
|
|
297
297
|
Before accessing a scope, check if it's available:
|
|
298
298
|
|
|
299
299
|
```tsx
|
|
300
|
-
const
|
|
300
|
+
const aui = useAui();
|
|
301
301
|
|
|
302
302
|
// Check if message scope exists
|
|
303
303
|
if (api.message.source) {
|
|
304
304
|
// Safe to use message scope
|
|
305
|
-
const { role } =
|
|
305
|
+
const { role } = aui.message().getState();
|
|
306
306
|
}
|
|
307
307
|
```
|
|
308
308
|
|
|
@@ -311,22 +311,22 @@ if (api.message.source) {
|
|
|
311
311
|
Navigate through the scope hierarchy programmatically:
|
|
312
312
|
|
|
313
313
|
```tsx
|
|
314
|
-
const
|
|
314
|
+
const aui = useAui();
|
|
315
315
|
|
|
316
316
|
// Access specific message by ID or index
|
|
317
|
-
const messageById =
|
|
318
|
-
const messageByIndex =
|
|
317
|
+
const messageById = aui.thread().message({ id: "msg_123" });
|
|
318
|
+
const messageByIndex = aui.thread().message({ index: 0 });
|
|
319
319
|
|
|
320
320
|
// Access part by index or tool call ID
|
|
321
|
-
const partByIndex =
|
|
322
|
-
const partByToolCall =
|
|
321
|
+
const partByIndex = aui.message().part({ index: 0 });
|
|
322
|
+
const partByToolCall = aui.message().part({ toolCallId: "call_123" });
|
|
323
323
|
|
|
324
324
|
// Access attachment by index
|
|
325
|
-
const attachment =
|
|
325
|
+
const attachment = aui.composer().attachment({ index: 0 }).getState();
|
|
326
326
|
|
|
327
327
|
// Access thread from thread list
|
|
328
|
-
const thread =
|
|
329
|
-
const threadItem =
|
|
328
|
+
const thread = aui.threads().thread("main");
|
|
329
|
+
const threadItem = aui.threads().item({ id: "thread_123" });
|
|
330
330
|
```
|
|
331
331
|
|
|
332
332
|
## Common Patterns
|
|
@@ -335,7 +335,7 @@ const threadItem = api.threads().item({ id: "thread_123" });
|
|
|
335
335
|
|
|
336
336
|
```tsx
|
|
337
337
|
function RunIndicator() {
|
|
338
|
-
const isRunning =
|
|
338
|
+
const isRunning = useAuiState(({ thread }) => thread.isRunning);
|
|
339
339
|
|
|
340
340
|
if (!isRunning) return null;
|
|
341
341
|
return <div>Assistant is thinking...</div>;
|
|
@@ -346,10 +346,10 @@ function RunIndicator() {
|
|
|
346
346
|
|
|
347
347
|
```tsx
|
|
348
348
|
function CopyButton() {
|
|
349
|
-
const
|
|
349
|
+
const aui = useAui();
|
|
350
350
|
|
|
351
351
|
const handleCopy = () => {
|
|
352
|
-
navigator.clipboard.writeText(
|
|
352
|
+
navigator.clipboard.writeText(aui.message().getCopyText());
|
|
353
353
|
};
|
|
354
354
|
|
|
355
355
|
return <button onClick={handleCopy}>Copy</button>;
|
|
@@ -360,9 +360,9 @@ function CopyButton() {
|
|
|
360
360
|
|
|
361
361
|
```tsx
|
|
362
362
|
function SmartComposer() {
|
|
363
|
-
const
|
|
364
|
-
const isRunning =
|
|
365
|
-
const text =
|
|
363
|
+
const aui = useAui();
|
|
364
|
+
const isRunning = useAuiState(({ thread }) => thread.isRunning);
|
|
365
|
+
const text = useAuiState(({ composer }) => composer.text);
|
|
366
366
|
|
|
367
367
|
const canSend = !isRunning && text.length > 0;
|
|
368
368
|
|
|
@@ -370,10 +370,10 @@ function SmartComposer() {
|
|
|
370
370
|
<div>
|
|
371
371
|
<textarea
|
|
372
372
|
value={text}
|
|
373
|
-
onChange={(e) =>
|
|
373
|
+
onChange={(e) => aui.composer().setText(e.target.value)}
|
|
374
374
|
disabled={isRunning}
|
|
375
375
|
/>
|
|
376
|
-
<button onClick={() =>
|
|
376
|
+
<button onClick={() => aui.composer().send()} disabled={!canSend}>
|
|
377
377
|
Send
|
|
378
378
|
</button>
|
|
379
379
|
</div>
|
|
@@ -387,7 +387,7 @@ function SmartComposer() {
|
|
|
387
387
|
function MessageCounter() {
|
|
388
388
|
const [sendCount, setSendCount] = useState(0);
|
|
389
389
|
|
|
390
|
-
|
|
390
|
+
useAuiEvent("composer.send", () => {
|
|
391
391
|
setSendCount((c) => c + 1);
|
|
392
392
|
});
|
|
393
393
|
|
|
@@ -402,16 +402,16 @@ function MessageCounter() {
|
|
|
402
402
|
When you call `api.scope()`, the API resolves the current scope at that moment. This resolution happens each time you call the function, which matters when dealing with changing contexts:
|
|
403
403
|
|
|
404
404
|
```tsx
|
|
405
|
-
const
|
|
405
|
+
const aui = useAui();
|
|
406
406
|
|
|
407
407
|
// Get current thread
|
|
408
|
-
const thread1 =
|
|
408
|
+
const thread1 = aui.thread();
|
|
409
409
|
thread1.append({ role: "user", content: "Hello" });
|
|
410
410
|
|
|
411
411
|
// User might switch threads here
|
|
412
412
|
|
|
413
413
|
// This could be a different thread
|
|
414
|
-
const thread2 =
|
|
414
|
+
const thread2 = aui.thread();
|
|
415
415
|
thread2.cancelRun(); // Cancels the current thread's run, not necessarily thread1's
|
|
416
416
|
```
|
|
417
417
|
|
|
@@ -423,12 +423,12 @@ For most use cases, this behavior is intuitive. In advanced scenarios where you
|
|
|
423
423
|
|
|
424
424
|
```tsx
|
|
425
425
|
// ❌ Expensive computation in selector (runs on every store update)
|
|
426
|
-
const result =
|
|
426
|
+
const result = useAuiState(
|
|
427
427
|
({ thread }) => thread.messages.filter((m) => m.role === "user").length,
|
|
428
428
|
);
|
|
429
429
|
|
|
430
430
|
// ✅ Memoize expensive computations
|
|
431
|
-
const messages =
|
|
431
|
+
const messages = useAuiState(({ thread }) => thread.messages);
|
|
432
432
|
const userCount = useMemo(
|
|
433
433
|
() => messages.filter((m) => m.role === "user").length,
|
|
434
434
|
[messages],
|
|
@@ -439,10 +439,10 @@ const userCount = useMemo(
|
|
|
439
439
|
|
|
440
440
|
```tsx
|
|
441
441
|
// ❌ Subscribes to entire thread state
|
|
442
|
-
const thread =
|
|
442
|
+
const thread = useAuiState(({ thread }) => thread);
|
|
443
443
|
|
|
444
444
|
// ✅ Subscribe only to needed values
|
|
445
|
-
const isRunning =
|
|
445
|
+
const isRunning = useAuiState(({ thread }) => thread.isRunning);
|
|
446
446
|
```
|
|
447
447
|
|
|
448
448
|
## API Reference
|
|
@@ -451,9 +451,9 @@ const isRunning = useAssistantState(({ thread }) => thread.isRunning);
|
|
|
451
451
|
|
|
452
452
|
| Hook | Purpose | Returns |
|
|
453
453
|
| ----------------------------------- | -------------------------- | -------------- |
|
|
454
|
-
| `
|
|
455
|
-
| `
|
|
456
|
-
| `
|
|
454
|
+
| `useAuiState(selector)` | Subscribe to state changes | Selected value |
|
|
455
|
+
| `useAui()` | Get API instance | API object |
|
|
456
|
+
| `useAuiEvent(event, handler)` | Subscribe to events | void |
|
|
457
457
|
|
|
458
458
|
### Scope States
|
|
459
459
|
|
|
@@ -483,14 +483,14 @@ const isRunning = useAssistantState(({ thread }) => thread.isRunning);
|
|
|
483
483
|
|
|
484
484
|
| Event | Description |
|
|
485
485
|
| -------------------------------- | ----------------------------- |
|
|
486
|
-
| `thread.
|
|
487
|
-
| `thread.
|
|
486
|
+
| `thread.runStart` | Assistant starts generating |
|
|
487
|
+
| `thread.runEnd` | Assistant finishes generating |
|
|
488
488
|
| `thread.initialize` | Thread is initialized |
|
|
489
|
-
| `thread.
|
|
489
|
+
| `thread.modelContextUpdate` | Model context is updated |
|
|
490
490
|
| `composer.send` | Message is sent |
|
|
491
|
-
| `composer.
|
|
492
|
-
| `
|
|
493
|
-
| `
|
|
491
|
+
| `composer.attachmentAdd` | Attachment added to composer |
|
|
492
|
+
| `threadListItem.switchedTo` | Switched to a thread |
|
|
493
|
+
| `threadListItem.switchedAway` | Switched away from a thread |
|
|
494
494
|
|
|
495
495
|
## Troubleshooting
|
|
496
496
|
|
|
@@ -500,13 +500,13 @@ const isRunning = useAssistantState(({ thread }) => thread.isRunning);
|
|
|
500
500
|
|
|
501
501
|
```tsx
|
|
502
502
|
// ❌ This will throw if not inside a message component
|
|
503
|
-
const role =
|
|
503
|
+
const role = useAuiState(({ message }) => message.role);
|
|
504
504
|
|
|
505
505
|
// ✅ Check scope availability first
|
|
506
506
|
function SafeMessageButton() {
|
|
507
|
-
const
|
|
507
|
+
const aui = useAui();
|
|
508
508
|
|
|
509
|
-
const role =
|
|
509
|
+
const role = useAuiState(({ message }) =>
|
|
510
510
|
api.message.source !== undefined ? message.role : "none",
|
|
511
511
|
);
|
|
512
512
|
|
|
@@ -518,22 +518,22 @@ function SafeMessageButton() {
|
|
|
518
518
|
|
|
519
519
|
```tsx
|
|
520
520
|
// ❌ Creating new objects in selectors causes infinite re-renders
|
|
521
|
-
const data =
|
|
521
|
+
const data = useAuiState(({ message }) => ({
|
|
522
522
|
role: message.role,
|
|
523
523
|
content: message.content, // New object every time!
|
|
524
524
|
}));
|
|
525
525
|
|
|
526
526
|
// ✅ Return primitive values or use separate selectors
|
|
527
|
-
const role =
|
|
528
|
-
const content =
|
|
527
|
+
const role = useAuiState(({ message }) => message.role);
|
|
528
|
+
const content = useAuiState(({ message }) => message.content);
|
|
529
529
|
```
|
|
530
530
|
|
|
531
531
|
**"Scope resolution failed" / Stale scope references**
|
|
532
532
|
|
|
533
533
|
```tsx
|
|
534
534
|
// ❌ Storing scope references can lead to stale data
|
|
535
|
-
const
|
|
536
|
-
const thread =
|
|
535
|
+
const aui = useAui();
|
|
536
|
+
const thread = aui.thread(); // This reference might become stale
|
|
537
537
|
|
|
538
538
|
useEffect(() => {
|
|
539
539
|
// This might reference the wrong thread if user switched
|
|
@@ -541,26 +541,26 @@ useEffect(() => {
|
|
|
541
541
|
}, [thread]);
|
|
542
542
|
|
|
543
543
|
// ✅ Resolve scopes fresh each time
|
|
544
|
-
const
|
|
544
|
+
const aui = useAui();
|
|
545
545
|
|
|
546
546
|
useEffect(() => {
|
|
547
547
|
// Always gets the current thread
|
|
548
|
-
|
|
549
|
-
}, [
|
|
548
|
+
aui.thread().cancelRun();
|
|
549
|
+
}, [aui]);
|
|
550
550
|
```
|
|
551
551
|
|
|
552
552
|
## Quick Reference
|
|
553
553
|
|
|
554
554
|
```tsx
|
|
555
555
|
// Read state
|
|
556
|
-
const value =
|
|
556
|
+
const value = useAuiState(({ scope }) => scope.property);
|
|
557
557
|
|
|
558
558
|
// Perform action
|
|
559
|
-
const
|
|
559
|
+
const aui = useAui();
|
|
560
560
|
api.scope().action();
|
|
561
561
|
|
|
562
562
|
// Listen to events
|
|
563
|
-
|
|
563
|
+
useAuiEvent("source.event", (e) => {});
|
|
564
564
|
|
|
565
565
|
// Check scope availability
|
|
566
566
|
if (api.scope.source) {
|
|
@@ -571,5 +571,5 @@ if (api.scope.source) {
|
|
|
571
571
|
const state = api.scope().getState();
|
|
572
572
|
|
|
573
573
|
// Navigate scopes
|
|
574
|
-
|
|
574
|
+
aui.thread().message({ id: "..." }).getState();
|
|
575
575
|
```
|