@assistant-ui/mcp-docs-server 0.1.39 → 0.2.0
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/waterfall.md +7 -7
- package/.docs/organized/code-examples/with-a2a.md +7 -7
- package/.docs/organized/code-examples/with-ag-ui.md +8 -8
- package/.docs/organized/code-examples/with-ai-sdk-v7.md +9 -9
- package/.docs/organized/code-examples/with-artifacts.md +463 -138
- package/.docs/organized/code-examples/with-assistant-transport.md +7 -7
- package/.docs/organized/code-examples/with-browser-extension.md +6 -6
- package/.docs/organized/code-examples/with-chain-of-thought.md +11 -11
- package/.docs/organized/code-examples/with-cloud-standalone.md +9 -9
- package/.docs/organized/code-examples/with-cloud.md +9 -9
- package/.docs/organized/code-examples/with-custom-thread-list.md +9 -9
- package/.docs/organized/code-examples/with-elevenlabs-conversational.md +11 -11
- package/.docs/organized/code-examples/with-elevenlabs-scribe.md +11 -11
- package/.docs/organized/code-examples/with-eve.md +8 -8
- package/.docs/organized/code-examples/with-expo.md +26 -46
- package/.docs/organized/code-examples/with-external-store.md +7 -7
- package/.docs/organized/code-examples/with-ffmpeg.md +9 -9
- package/.docs/organized/code-examples/with-generative-ui.md +10 -10
- package/.docs/organized/code-examples/with-google-adk.md +8 -8
- package/.docs/organized/code-examples/with-heat-graph.md +6 -6
- package/.docs/organized/code-examples/with-image-generation.md +9 -9
- package/.docs/organized/code-examples/with-interactables.md +9 -9
- package/.docs/organized/code-examples/with-langchain.md +9 -9
- package/.docs/organized/code-examples/with-langgraph.md +9 -9
- package/.docs/organized/code-examples/with-livekit.md +11 -11
- package/.docs/organized/code-examples/with-mcp.md +28 -23
- package/.docs/organized/code-examples/with-opencode.md +8 -11
- package/.docs/organized/code-examples/with-pi.md +14 -14
- package/.docs/organized/code-examples/with-react-hook-form.md +10 -10
- package/.docs/organized/code-examples/with-react-ink-web.md +5 -5
- package/.docs/organized/code-examples/with-react-ink.md +2 -2
- package/.docs/organized/code-examples/with-react-router.md +11 -11
- package/.docs/organized/code-examples/with-resumable-stream.md +10 -10
- package/.docs/organized/code-examples/with-store.md +6 -6
- package/.docs/organized/code-examples/with-tanstack.md +9 -9
- package/.docs/organized/code-examples/with-tap-runtime.md +7 -7
- package/.docs/organized/code-examples/with-virtualized-thread.md +8 -8
- package/.docs/raw/docs/(docs)/cli.mdx +2 -0
- package/.docs/raw/docs/(docs)/devtools.mdx +7 -2
- package/.docs/raw/docs/(reference)/api-reference/generative-ui/a2ui.mdx +40 -0
- package/.docs/raw/docs/(reference)/api-reference/generative-ui/index.mdx +3 -0
- package/.docs/raw/docs/(reference)/api-reference/hooks/primitives.mdx +19 -420
- package/.docs/raw/docs/(reference)/api-reference/hooks/state.mdx +4 -1
- package/.docs/raw/docs/(reference)/api-reference/integrations/react-ai-sdk.mdx +21 -0
- package/.docs/raw/docs/(reference)/api-reference/model-context/context.mdx +1 -9
- package/.docs/raw/docs/(reference)/api-reference/primitives/assistant-if.mdx +1 -18
- package/.docs/raw/docs/cloud/langgraph.mdx +1 -1
- package/.docs/raw/docs/copilots/model-context.mdx +1 -1
- package/.docs/raw/docs/copilots/motivation.mdx +1 -1
- package/.docs/raw/docs/guides/attachments.mdx +3 -3
- package/.docs/raw/docs/guides/branching.mdx +2 -2
- package/.docs/raw/docs/guides/context-api.mdx +89 -111
- package/.docs/raw/docs/guides/editing.mdx +5 -5
- package/.docs/raw/docs/guides/electron.mdx +369 -0
- package/.docs/raw/docs/guides/index.mdx +10 -0
- package/.docs/raw/docs/guides/quoting.mdx +3 -3
- package/.docs/raw/docs/guides/resumable-stream-deployment.mdx +2 -2
- package/.docs/raw/docs/ink/hooks.mdx +3 -3
- package/.docs/raw/docs/ink/primitives.mdx +14 -7
- package/.docs/raw/docs/integrations/auth/better-auth.mdx +2 -2
- package/.docs/raw/docs/integrations/auth/clerk.mdx +2 -2
- package/.docs/raw/docs/integrations/auth/next-auth.mdx +3 -3
- package/.docs/raw/docs/integrations/observability/langsmith.mdx +1 -1
- package/.docs/raw/docs/integrations/persistence/custom-adapter.mdx +36 -9
- package/.docs/raw/docs/migrations/v0-15.mdx +156 -0
- package/.docs/raw/docs/primitives/composer.mdx +1 -1
- package/.docs/raw/docs/primitives/thread-list.mdx +2 -2
- package/.docs/raw/docs/react-native/hooks.mdx +3 -3
- package/.docs/raw/docs/react-native/index.mdx +2 -2
- package/.docs/raw/docs/react-native/primitives.mdx +39 -5
- package/.docs/raw/docs/runtimes/ai-sdk/overview.mdx +3 -3
- package/.docs/raw/docs/runtimes/ai-sdk/v4-legacy.mdx +4 -4
- package/.docs/raw/docs/runtimes/ai-sdk/v5-legacy.mdx +4 -4
- package/.docs/raw/docs/runtimes/ai-sdk/v6-legacy.mdx +4 -4
- package/.docs/raw/docs/runtimes/ai-sdk/v7.mdx +2 -2
- package/.docs/raw/docs/runtimes/concepts/threads.mdx +10 -10
- package/.docs/raw/docs/runtimes/custom/assistant-transport.mdx +2 -2
- package/.docs/raw/docs/runtimes/custom/external-store.mdx +1 -1
- package/.docs/raw/docs/runtimes/custom/local-runtime.mdx +3 -3
- package/.docs/raw/docs/runtimes/langchain.mdx +2 -2
- package/.docs/raw/docs/runtimes/langgraph/overview.mdx +1 -1
- package/.docs/raw/docs/runtimes/opencode/hooks.mdx +3 -1
- package/.docs/raw/docs/tools/a2ui.mdx +107 -0
- package/.docs/raw/docs/tools/interactables-legacy.mdx +4 -4
- package/.docs/raw/docs/tools/interactables.mdx +3 -3
- package/.docs/raw/docs/tools/mcp-apps.mdx +61 -2
- package/.docs/raw/docs/tools/user-managed-mcp.mdx +52 -7
- package/.docs/raw/docs/ui/follow-up-suggestions.mdx +2 -0
- package/.docs/raw/docs/ui/model-selector.mdx +1 -1
- package/.docs/raw/docs/ui/part-grouping.mdx +0 -4
- package/.docs/raw/docs/ui/reasoning.mdx +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/prepare-docs/code-examples.js.map +1 -1
- package/dist/prepare-docs/prepare.d.ts +1 -1
- package/dist/stdio.d.ts +1 -1
- package/dist/tools/docs.d.ts +6 -10
- package/dist/tools/docs.d.ts.map +1 -1
- package/dist/tools/docs.js +2 -2
- package/dist/tools/docs.js.map +1 -1
- package/dist/tools/examples.d.ts +4 -8
- package/dist/tools/examples.d.ts.map +1 -1
- package/dist/tools/examples.js +2 -2
- package/dist/tools/examples.js.map +1 -1
- package/dist/tools/resources.d.ts +1 -1
- package/dist/tools/resources.d.ts.map +1 -1
- package/dist/tools/resources.js +1 -1
- package/dist/tools/resources.js.map +1 -1
- package/dist/tools/search.d.ts +4 -10
- package/dist/tools/search.d.ts.map +1 -1
- package/dist/tools/search.js +2 -2
- package/dist/tools/search.js.map +1 -1
- package/dist/tools/tests/mcp-test-client.d.ts +15 -0
- package/dist/tools/tests/mcp-test-client.d.ts.map +1 -0
- package/dist/tools/tests/mcp-test-client.js +68 -0
- package/dist/tools/tests/mcp-test-client.js.map +1 -0
- package/dist/tools/tests/test-setup.d.ts.map +1 -1
- package/dist/tools/xulux-templates.d.ts +9 -23
- package/dist/tools/xulux-templates.d.ts.map +1 -1
- package/dist/tools/xulux-templates.js +4 -4
- package/dist/tools/xulux-templates.js.map +1 -1
- package/dist/utils/logger.d.ts.map +1 -1
- package/dist/utils/security.js.map +1 -1
- package/package.json +4 -3
- package/src/index.ts +2 -2
- package/src/tools/docs.ts +2 -2
- package/src/tools/examples.ts +2 -2
- package/src/tools/resources.ts +1 -4
- package/src/tools/search.ts +2 -2
- package/src/tools/tests/completions.test.ts +40 -26
- package/src/tools/tests/integration.test.ts +3 -4
- package/src/tools/tests/mcp-protocol.test.ts +160 -175
- package/src/tools/tests/mcp-test-client.ts +111 -0
- package/src/tools/tests/resources.test.ts +97 -66
- package/src/tools/xulux-templates.ts +4 -4
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Electron
|
|
3
|
+
description: Run assistant-ui in an Electron renderer with a hosted backend or a secure, streaming preload and IPC bridge.
|
|
4
|
+
platforms: ["react"]
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
assistant-ui works in Electron through `@assistant-ui/react`. The renderer is a React DOM environment, so there is no separate `@assistant-ui/electron` package to install. The Electron-specific decision is where model requests run and how the renderer reaches them.
|
|
8
|
+
|
|
9
|
+
<Callout type="warn">
|
|
10
|
+
Never put a provider API key in renderer code, a `VITE_*` variable, or a preload script. Bundled values are readable by anyone with the app. Keep remote credentials on your backend, or keep local credentials in the main process and provision them with an OS-backed secret store.
|
|
11
|
+
</Callout>
|
|
12
|
+
|
|
13
|
+
## Choose a connection pattern
|
|
14
|
+
|
|
15
|
+
| Pattern | Use it when | Runtime |
|
|
16
|
+
| --- | --- | --- |
|
|
17
|
+
| Hosted backend | You already have an AI SDK endpoint, need server auth or persistence, or ship the app to other people | `useChatRuntime` with an absolute HTTPS URL |
|
|
18
|
+
| Local main process | The desktop app owns the provider or agent process and must work without your web backend | `useLocalRuntime` with a narrow preload/IPC bridge |
|
|
19
|
+
|
|
20
|
+
```mermaid
|
|
21
|
+
flowchart LR
|
|
22
|
+
Renderer["Electron renderer<br/>assistant-ui React"] -->|"absolute HTTPS"| Backend["Hosted chat backend"]
|
|
23
|
+
Renderer -->|"window.assistantAI"| Preload["context-isolated preload"]
|
|
24
|
+
Preload -->|"transferred MessagePort"| Main["Electron main process"]
|
|
25
|
+
Backend --> Provider["Model or agent"]
|
|
26
|
+
Main --> Provider
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Do not send an SDK client, assistant-ui runtime, callback, `AbortSignal`, or `File` through IPC. Electron IPC uses structured clone semantics; define a small data-only protocol instead.
|
|
30
|
+
|
|
31
|
+
## Pattern 1: hosted backend
|
|
32
|
+
|
|
33
|
+
This is the smallest integration. Keep your existing AI SDK chat route and point `AssistantChatTransport` at its public URL.
|
|
34
|
+
|
|
35
|
+
```tsx title="renderer/assistant-runtime.tsx"
|
|
36
|
+
import type { ReactNode } from "react";
|
|
37
|
+
import { AssistantRuntimeProvider } from "@assistant-ui/react";
|
|
38
|
+
import {
|
|
39
|
+
AssistantChatTransport,
|
|
40
|
+
useChatRuntime,
|
|
41
|
+
} from "@assistant-ui/react-ai-sdk";
|
|
42
|
+
|
|
43
|
+
const transport = new AssistantChatTransport({
|
|
44
|
+
api: "https://api.example.com/chat",
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
export function ElectronRuntimeProvider({ children }: { children: ReactNode }) {
|
|
48
|
+
const runtime = useChatRuntime({ transport });
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<AssistantRuntimeProvider runtime={runtime}>
|
|
52
|
+
{children}
|
|
53
|
+
</AssistantRuntimeProvider>
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
The URL must be absolute in a packaged app. A relative value such as `/api/chat` targets the renderer's `file://` or custom-protocol origin, not your deployed backend. Configure the backend's CORS policy for the packaged origin, allow the endpoint in `connect-src`, and authenticate requests as you would from any desktop client.
|
|
59
|
+
|
|
60
|
+
The endpoint must return the AI SDK UI message stream consumed by `AssistantChatTransport`. See the [AI SDK runtime guide](/docs/runtimes/ai-sdk/v7) for the backend route and tool-calling setup.
|
|
61
|
+
|
|
62
|
+
## Pattern 2: local main process
|
|
63
|
+
|
|
64
|
+
Use this pattern when the Electron main process calls the model or runs a local agent. Keep `contextIsolation: true`, `sandbox: true`, and `nodeIntegration: false` on the `BrowserWindow`. Register `registerAssistantIpc(mainWindow)` after creating the trusted window.
|
|
65
|
+
|
|
66
|
+
The following example is intentionally text-only. It streams one request over a dedicated `MessagePort`; closing that port propagates assistant-ui's Stop action to an `AbortController` in the main process.
|
|
67
|
+
|
|
68
|
+
### 1. Define a data-only protocol
|
|
69
|
+
|
|
70
|
+
Place the shared types somewhere all three Electron bundles can import.
|
|
71
|
+
|
|
72
|
+
```ts title="shared/assistant-ipc.ts"
|
|
73
|
+
export const ASSISTANT_STREAM_CHANNEL = "assistant:stream";
|
|
74
|
+
|
|
75
|
+
export type ChatMessage =
|
|
76
|
+
| { role: "user"; content: string }
|
|
77
|
+
| { role: "assistant"; content: string };
|
|
78
|
+
|
|
79
|
+
export type ChatRequest = {
|
|
80
|
+
system?: string;
|
|
81
|
+
messages: ChatMessage[];
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export type ChatEvent =
|
|
85
|
+
| { type: "delta"; text: string }
|
|
86
|
+
| { type: "done" }
|
|
87
|
+
| { type: "error"; message: string };
|
|
88
|
+
|
|
89
|
+
export type AssistantAI = {
|
|
90
|
+
streamChat(
|
|
91
|
+
request: ChatRequest,
|
|
92
|
+
onEvent: (event: ChatEvent) => void,
|
|
93
|
+
): () => void;
|
|
94
|
+
};
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### 2. Expose one preload capability
|
|
98
|
+
|
|
99
|
+
Expose the smallest API the renderer needs, not `ipcRenderer` itself. The callback receives only validated event data, never Electron's privileged IPC event object.
|
|
100
|
+
|
|
101
|
+
```ts title="preload/assistant.ts"
|
|
102
|
+
import { contextBridge, ipcRenderer } from "electron";
|
|
103
|
+
import {
|
|
104
|
+
ASSISTANT_STREAM_CHANNEL,
|
|
105
|
+
type AssistantAI,
|
|
106
|
+
type ChatEvent,
|
|
107
|
+
} from "./shared";
|
|
108
|
+
|
|
109
|
+
const assistantAI: AssistantAI = {
|
|
110
|
+
streamChat(request, onEvent) {
|
|
111
|
+
const { port1, port2 } = new MessageChannel();
|
|
112
|
+
const onMessage = (event: MessageEvent<ChatEvent>) => onEvent(event.data);
|
|
113
|
+
|
|
114
|
+
port1.addEventListener("message", onMessage);
|
|
115
|
+
port1.start();
|
|
116
|
+
ipcRenderer.postMessage(ASSISTANT_STREAM_CHANNEL, request, [port2]);
|
|
117
|
+
|
|
118
|
+
let stopped = false;
|
|
119
|
+
return () => {
|
|
120
|
+
if (stopped) return;
|
|
121
|
+
stopped = true;
|
|
122
|
+
port1.removeEventListener("message", onMessage);
|
|
123
|
+
port1.close();
|
|
124
|
+
};
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
contextBridge.exposeInMainWorld("assistantAI", assistantAI);
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Declare the context-bridge API for renderer TypeScript:
|
|
132
|
+
|
|
133
|
+
```ts title="renderer/electron.d.ts"
|
|
134
|
+
import type { AssistantAI } from "./shared";
|
|
135
|
+
|
|
136
|
+
declare global {
|
|
137
|
+
interface Window {
|
|
138
|
+
assistantAI: AssistantAI;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export {};
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### 3. Stream from the main process
|
|
146
|
+
|
|
147
|
+
Install the provider packages in the Electron main-process bundle:
|
|
148
|
+
|
|
149
|
+
```sh
|
|
150
|
+
pnpm add ai @ai-sdk/openai
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
The handler checks both the sending `WebContents` and its main frame, validates the untrusted payload and bounds its size before calling the model. Set `OPENAI_API_KEY` only in the main-process environment.
|
|
154
|
+
|
|
155
|
+
```ts title="main/assistant-ipc.ts"
|
|
156
|
+
import { openai } from "@ai-sdk/openai";
|
|
157
|
+
import { streamText } from "ai";
|
|
158
|
+
import { ipcMain, type BrowserWindow, type IpcMainEvent } from "electron";
|
|
159
|
+
import {
|
|
160
|
+
ASSISTANT_STREAM_CHANNEL,
|
|
161
|
+
type ChatEvent,
|
|
162
|
+
type ChatRequest,
|
|
163
|
+
} from "./shared";
|
|
164
|
+
|
|
165
|
+
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
166
|
+
typeof value === "object" && value !== null;
|
|
167
|
+
|
|
168
|
+
const isChatRequest = (value: unknown): value is ChatRequest => {
|
|
169
|
+
if (!isRecord(value) || !Array.isArray(value.messages)) return false;
|
|
170
|
+
if (value.system !== undefined && typeof value.system !== "string") {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
if (value.messages.length > 200) return false;
|
|
174
|
+
|
|
175
|
+
let totalLength = typeof value.system === "string" ? value.system.length : 0;
|
|
176
|
+
for (const message of value.messages) {
|
|
177
|
+
if (!isRecord(message)) return false;
|
|
178
|
+
if (message.role !== "user" && message.role !== "assistant") return false;
|
|
179
|
+
if (typeof message.content !== "string") return false;
|
|
180
|
+
totalLength += message.content.length;
|
|
181
|
+
if (totalLength > 1_000_000) return false;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return true;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
export function registerAssistantIpc(mainWindow: BrowserWindow) {
|
|
188
|
+
const handleStream = (event: IpcMainEvent, request: unknown) => {
|
|
189
|
+
const [port] = event.ports;
|
|
190
|
+
if (!port) return;
|
|
191
|
+
|
|
192
|
+
if (
|
|
193
|
+
event.sender !== mainWindow.webContents ||
|
|
194
|
+
event.senderFrame !== mainWindow.webContents.mainFrame
|
|
195
|
+
) {
|
|
196
|
+
port.close();
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
port.start();
|
|
201
|
+
if (!isChatRequest(request)) {
|
|
202
|
+
port.postMessage({
|
|
203
|
+
type: "error",
|
|
204
|
+
message: "Invalid chat request.",
|
|
205
|
+
} satisfies ChatEvent);
|
|
206
|
+
port.close();
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const abortController = new AbortController();
|
|
211
|
+
port.once("close", () => abortController.abort());
|
|
212
|
+
const send = (message: ChatEvent) => port.postMessage(message);
|
|
213
|
+
|
|
214
|
+
void (async () => {
|
|
215
|
+
try {
|
|
216
|
+
const result = streamText({
|
|
217
|
+
model: openai("gpt-5.4-mini"),
|
|
218
|
+
messages: request.messages,
|
|
219
|
+
...(request.system ? { system: request.system } : {}),
|
|
220
|
+
abortSignal: abortController.signal,
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
for await (const text of result.textStream) {
|
|
224
|
+
send({ type: "delta", text });
|
|
225
|
+
}
|
|
226
|
+
send({ type: "done" });
|
|
227
|
+
} catch (error) {
|
|
228
|
+
if (abortController.signal.aborted) return;
|
|
229
|
+
console.error("Assistant stream failed", error);
|
|
230
|
+
send({ type: "error", message: "The model request failed." });
|
|
231
|
+
}
|
|
232
|
+
})();
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
ipcMain.on(ASSISTANT_STREAM_CHANNEL, handleStream);
|
|
236
|
+
mainWindow.once("closed", () => {
|
|
237
|
+
ipcMain.removeListener(ASSISTANT_STREAM_CHANNEL, handleStream);
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
If your app can open more than one trusted assistant window, register a unique channel per window or route requests through one application-level registry. A global `ipcMain.on` listener should not be re-registered under the same channel for every window.
|
|
243
|
+
|
|
244
|
+
### 4. Adapt IPC to assistant-ui
|
|
245
|
+
|
|
246
|
+
`ChatModelAdapter` yields complete snapshots, so the renderer accumulates each IPC delta before yielding it. Its cleanup function closes the port on Stop, unmount, or completion.
|
|
247
|
+
|
|
248
|
+
```tsx title="renderer/assistant-runtime.tsx"
|
|
249
|
+
import type { ReactNode } from "react";
|
|
250
|
+
import {
|
|
251
|
+
AssistantRuntimeProvider,
|
|
252
|
+
useLocalRuntime,
|
|
253
|
+
type ChatModelAdapter,
|
|
254
|
+
} from "@assistant-ui/react";
|
|
255
|
+
import type { ChatMessage } from "./shared";
|
|
256
|
+
|
|
257
|
+
const ipcChatModel: ChatModelAdapter = {
|
|
258
|
+
async *run({ messages, context, abortSignal }) {
|
|
259
|
+
abortSignal.throwIfAborted();
|
|
260
|
+
|
|
261
|
+
const system = [context.system];
|
|
262
|
+
const serializedMessages: ChatMessage[] = [];
|
|
263
|
+
|
|
264
|
+
for (const message of messages) {
|
|
265
|
+
const text = message.content
|
|
266
|
+
.flatMap((part) => (part.type === "text" ? [part.text] : []))
|
|
267
|
+
.join("\n");
|
|
268
|
+
if (!text) continue;
|
|
269
|
+
|
|
270
|
+
if (message.role === "system") system.push(text);
|
|
271
|
+
if (message.role === "user") {
|
|
272
|
+
serializedMessages.push({ role: "user", content: text });
|
|
273
|
+
}
|
|
274
|
+
if (message.role === "assistant") {
|
|
275
|
+
serializedMessages.push({ role: "assistant", content: text });
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const systemText = system.filter(Boolean).join("\n\n");
|
|
280
|
+
let stop: (() => void) | undefined;
|
|
281
|
+
let removeAbortListener: (() => void) | undefined;
|
|
282
|
+
const deltas = new ReadableStream<string>({
|
|
283
|
+
start(controller) {
|
|
284
|
+
let settled = false;
|
|
285
|
+
const close = () => {
|
|
286
|
+
if (settled) return;
|
|
287
|
+
settled = true;
|
|
288
|
+
controller.close();
|
|
289
|
+
};
|
|
290
|
+
const fail = (error: unknown) => {
|
|
291
|
+
if (settled) return;
|
|
292
|
+
settled = true;
|
|
293
|
+
controller.error(error);
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
stop = window.assistantAI.streamChat(
|
|
297
|
+
{
|
|
298
|
+
...(systemText ? { system: systemText } : {}),
|
|
299
|
+
messages: serializedMessages,
|
|
300
|
+
},
|
|
301
|
+
(event) => {
|
|
302
|
+
if (event.type === "delta") controller.enqueue(event.text);
|
|
303
|
+
if (event.type === "done") close();
|
|
304
|
+
if (event.type === "error") fail(new Error(event.message));
|
|
305
|
+
},
|
|
306
|
+
);
|
|
307
|
+
|
|
308
|
+
const onAbort = () => {
|
|
309
|
+
stop?.();
|
|
310
|
+
fail(abortSignal.reason);
|
|
311
|
+
};
|
|
312
|
+
abortSignal.addEventListener("abort", onAbort, { once: true });
|
|
313
|
+
removeAbortListener = () =>
|
|
314
|
+
abortSignal.removeEventListener("abort", onAbort);
|
|
315
|
+
if (abortSignal.aborted) onAbort();
|
|
316
|
+
},
|
|
317
|
+
cancel() {
|
|
318
|
+
stop?.();
|
|
319
|
+
},
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
const reader = deltas.getReader();
|
|
323
|
+
let fullText = "";
|
|
324
|
+
try {
|
|
325
|
+
while (true) {
|
|
326
|
+
const { done, value } = await reader.read();
|
|
327
|
+
if (done) return;
|
|
328
|
+
fullText += value;
|
|
329
|
+
yield { content: [{ type: "text", text: fullText }] };
|
|
330
|
+
}
|
|
331
|
+
} finally {
|
|
332
|
+
removeAbortListener?.();
|
|
333
|
+
stop?.();
|
|
334
|
+
reader.releaseLock();
|
|
335
|
+
}
|
|
336
|
+
},
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
export function ElectronRuntimeProvider({ children }: { children: ReactNode }) {
|
|
340
|
+
const runtime = useLocalRuntime(ipcChatModel);
|
|
341
|
+
return (
|
|
342
|
+
<AssistantRuntimeProvider runtime={runtime}>
|
|
343
|
+
{children}
|
|
344
|
+
</AssistantRuntimeProvider>
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
Wrap your existing assistant-ui thread with `ElectronRuntimeProvider`. Primitives and installed UI components work exactly as they do in a browser React app.
|
|
350
|
+
|
|
351
|
+
## Extend the protocol deliberately
|
|
352
|
+
|
|
353
|
+
The local example drops every non-text content part. Add explicit structured-clone-safe fields before claiming support for more features:
|
|
354
|
+
|
|
355
|
+
- Attachments: pass bounded `ArrayBuffer` data or an app-owned file reference after validating type, size, and path in the main process. Do not pass browser `File` objects.
|
|
356
|
+
- Tools: define serializable tool-call and tool-result events, and validate every tool invocation in the privileged process. Never expose a generic shell or filesystem IPC method.
|
|
357
|
+
- Reasoning and metadata: add distinct event variants and map them to the corresponding assistant-ui content parts.
|
|
358
|
+
- Thread persistence: store serializable thread data in the main process or use a remote thread-list adapter; do not try to transfer a runtime object.
|
|
359
|
+
|
|
360
|
+
## Packaged-app checklist
|
|
361
|
+
|
|
362
|
+
- Use an absolute HTTPS endpoint, a privileged custom protocol, or the preload bridge. Relative `/api/*` routes that worked against a development server will not follow your backend after packaging.
|
|
363
|
+
- Serve local content through a custom protocol instead of `file://` when possible, and define a restrictive Content Security Policy. Do not disable `webSecurity` to work around origin errors.
|
|
364
|
+
- Validate every IPC sender and every payload in the main process. Treat renderer data as untrusted even when the renderer is local.
|
|
365
|
+
- Intercept new windows and external links with `webContents.setWindowOpenHandler`; do not let model-generated links create unrestricted Electron windows.
|
|
366
|
+
- If you render remote HTML with `SafeContentFrame`, allow its documented host in `frame-src`. Ordinary text and Markdown rendering need no Electron-specific change.
|
|
367
|
+
- Test the packaged build, not only the Vite or Webpack development server. The scheme, origin, preload path, CSP, and environment loading can all differ.
|
|
368
|
+
|
|
369
|
+
For the underlying platform constraints, see Electron's official guides to [IPC and MessagePorts](https://www.electronjs.org/docs/latest/tutorial/message-ports), [context isolation](https://www.electronjs.org/docs/latest/tutorial/context-isolation), and the [security checklist](https://www.electronjs.org/docs/latest/tutorial/security).
|
|
@@ -105,3 +105,13 @@ Alternative ways to connect a model.
|
|
|
105
105
|
Run your app locally on a ChatGPT Plus or Pro plan via Codex OAuth, no API key required.
|
|
106
106
|
</Card>
|
|
107
107
|
</Cards>
|
|
108
|
+
|
|
109
|
+
## Environments
|
|
110
|
+
|
|
111
|
+
Run assistant-ui outside a conventional browser deployment.
|
|
112
|
+
|
|
113
|
+
<Cards>
|
|
114
|
+
<Card title="Electron" href="/docs/guides/electron">
|
|
115
|
+
Connect an Electron renderer to a hosted backend or a secure, streaming preload/IPC bridge.
|
|
116
|
+
</Card>
|
|
117
|
+
</Cards>
|
|
@@ -127,7 +127,7 @@ function CustomQuoteDisplay() {
|
|
|
127
127
|
|
|
128
128
|
## Programmatic API
|
|
129
129
|
|
|
130
|
-
Set or clear quotes via `useAui` from `@assistant-ui/react`. Call `aui.thread
|
|
130
|
+
Set or clear quotes via `useAui` from `@assistant-ui/react`. Call `aui.thread.composer().setQuote()` when your component is rendered outside of a specific thread context, or `aui.composer.setQuote()` when it is rendered inside a thread:
|
|
131
131
|
|
|
132
132
|
```tsx
|
|
133
133
|
import { useAui } from "@assistant-ui/react";
|
|
@@ -136,14 +136,14 @@ function MyComponent() {
|
|
|
136
136
|
const aui = useAui();
|
|
137
137
|
|
|
138
138
|
const quoteText = () => {
|
|
139
|
-
aui.thread
|
|
139
|
+
aui.thread.composer().setQuote({
|
|
140
140
|
text: "The text to quote",
|
|
141
141
|
messageId: "msg-123",
|
|
142
142
|
});
|
|
143
143
|
};
|
|
144
144
|
|
|
145
145
|
const clearQuote = () => {
|
|
146
|
-
aui.thread
|
|
146
|
+
aui.thread.composer().setQuote(undefined);
|
|
147
147
|
};
|
|
148
148
|
|
|
149
149
|
return (
|
|
@@ -143,7 +143,7 @@ Per-tenant prefixes also make incident response cheaper. A `SCAN MATCH aui:app:t
|
|
|
143
143
|
|
|
144
144
|
## Observability hooks
|
|
145
145
|
|
|
146
|
-
`ResumableStreamContextOptions` exposes lifecycle hooks for structured logging, metrics, and tracing.
|
|
146
|
+
`ResumableStreamContextOptions` exposes lifecycle hooks for structured logging, metrics, and tracing. Hook exceptions and rejected promises are reported through `console.error` without affecting stream output or stored lifecycle status.
|
|
147
147
|
|
|
148
148
|
```ts title="/lib/resumable-context.ts"
|
|
149
149
|
import { createResumableStreamContext } from "assistant-stream/resumable";
|
|
@@ -170,7 +170,7 @@ export const resumableContext = createResumableStreamContext({
|
|
|
170
170
|
});
|
|
171
171
|
```
|
|
172
172
|
|
|
173
|
-
Keep hook
|
|
173
|
+
Keep synchronous hook work cheap. `onAcquire` runs on the `run()` request path for producers and consumers alike, while the remaining hooks run on the producer's streaming hot path. Returned promises are observed for failures but are not awaited.
|
|
174
174
|
|
|
175
175
|
## Resource limits
|
|
176
176
|
|
|
@@ -41,11 +41,11 @@ import { useAui } from "@assistant-ui/react-ink";
|
|
|
41
41
|
const aui = useAui();
|
|
42
42
|
|
|
43
43
|
// Composer actions
|
|
44
|
-
aui.composer
|
|
45
|
-
aui.composer
|
|
44
|
+
aui.composer.setText("Hello");
|
|
45
|
+
aui.composer.send();
|
|
46
46
|
|
|
47
47
|
// Thread actions
|
|
48
|
-
aui.thread
|
|
48
|
+
aui.thread.cancelRun();
|
|
49
49
|
```
|
|
50
50
|
|
|
51
51
|
### useAuiEvent
|
|
@@ -396,7 +396,7 @@ Renders the quoted text from `s.composer.quote?.text`. Pass `children` to overri
|
|
|
396
396
|
|
|
397
397
|
### QuoteDismiss
|
|
398
398
|
|
|
399
|
-
Pressable that clears the active quote by calling `aui.composer
|
|
399
|
+
Pressable that clears the active quote by calling `aui.composer.setQuote(undefined)`.
|
|
400
400
|
|
|
401
401
|
```tsx
|
|
402
402
|
<ComposerPrimitive.QuoteDismiss>
|
|
@@ -683,7 +683,7 @@ Container `Box` for an attachment. Forwards all `Box` props.
|
|
|
683
683
|
|
|
684
684
|
### Remove
|
|
685
685
|
|
|
686
|
-
`Pressable` that calls `aui.attachment
|
|
686
|
+
`Pressable` that calls `aui.attachment.remove()` when activated (Enter while focused). Disabled when not focused or when the `disabled` prop is set.
|
|
687
687
|
|
|
688
688
|
| Prop | Type | Description |
|
|
689
689
|
|------|------|-------------|
|
|
@@ -725,7 +725,7 @@ Renders the queue item's prompt with Ink `<Text>`. Pass `children` to override t
|
|
|
725
725
|
|
|
726
726
|
### Remove
|
|
727
727
|
|
|
728
|
-
Pressable that removes the queue item by calling `aui.queueItem
|
|
728
|
+
Pressable that removes the queue item by calling `aui.queueItem.remove()`.
|
|
729
729
|
|
|
730
730
|
```tsx
|
|
731
731
|
<QueueItemPrimitive.Remove>
|
|
@@ -742,7 +742,7 @@ Remaining props are forwarded to the underlying `Pressable` (Ink `Box` props).
|
|
|
742
742
|
|
|
743
743
|
### Steer
|
|
744
744
|
|
|
745
|
-
Pressable that promotes the queue item to run next by calling `aui.queueItem
|
|
745
|
+
Pressable that promotes the queue item to run next by calling `aui.queueItem.steer()`.
|
|
746
746
|
|
|
747
747
|
```tsx
|
|
748
748
|
<QueueItemPrimitive.Steer>
|
|
@@ -765,10 +765,17 @@ import { ActionBarPrimitive } from "@assistant-ui/react-ink";
|
|
|
765
765
|
|
|
766
766
|
### Copy
|
|
767
767
|
|
|
768
|
-
Pressable that copies the message content. Supports function-as-children for copy state feedback.
|
|
768
|
+
Pressable that copies the message content. Pass a platform clipboard writer through `copyToClipboard`. Supports function-as-children for copy state feedback.
|
|
769
|
+
|
|
770
|
+
<InstallCommand npm={["clipboardy"]} />
|
|
769
771
|
|
|
770
772
|
```tsx
|
|
771
|
-
|
|
773
|
+
import clipboard from "clipboardy";
|
|
774
|
+
|
|
775
|
+
<ActionBarPrimitive.Copy
|
|
776
|
+
copiedDuration={3000}
|
|
777
|
+
copyToClipboard={clipboard.write}
|
|
778
|
+
>
|
|
772
779
|
{({ isCopied }) => <Text>{isCopied ? "[Copied!]" : "[Copy]"}</Text>}
|
|
773
780
|
</ActionBarPrimitive.Copy>
|
|
774
781
|
```
|
|
@@ -776,7 +783,7 @@ Pressable that copies the message content. Supports function-as-children for cop
|
|
|
776
783
|
| Prop | Type | Description |
|
|
777
784
|
|------|------|-------------|
|
|
778
785
|
| `copiedDuration` | `number` | Duration in ms to show "copied" state (default: 3000) |
|
|
779
|
-
| `copyToClipboard` | `(text: string) => void
|
|
786
|
+
| `copyToClipboard` | `(text: string) => void \| Promise<void>` | Platform clipboard writer |
|
|
780
787
|
|
|
781
788
|
### Edit
|
|
782
789
|
|
|
@@ -22,7 +22,7 @@ Three integration points:
|
|
|
22
22
|
|
|
23
23
|
1. **Auth handlers** mounted at `/api/auth/[...all]/route.ts` via `toNextJsHandler(auth)`.
|
|
24
24
|
2. **API routes** read the session server-side with `auth.api.getSession({ headers: await headers() })`.
|
|
25
|
-
3. **Reload-on-auth** uses better-auth's React client to detect sign-in and trigger `aui.threads
|
|
25
|
+
3. **Reload-on-auth** uses better-auth's React client to detect sign-in and trigger `aui.threads.reload()`.
|
|
26
26
|
|
|
27
27
|
`session.user.id` is exposed on the session object directly, so route handlers can scope queries against it.
|
|
28
28
|
|
|
@@ -136,7 +136,7 @@ export function ReloadOnAuth() {
|
|
|
136
136
|
const aui = useAui();
|
|
137
137
|
const { data: session, isPending } = authClient.useSession();
|
|
138
138
|
useEffect(() => {
|
|
139
|
-
if (!isPending && session) aui.threads
|
|
139
|
+
if (!isPending && session) aui.threads.reload();
|
|
140
140
|
}, [isPending, session?.user?.id]);
|
|
141
141
|
return null;
|
|
142
142
|
}
|
|
@@ -102,7 +102,7 @@ const rows = await db
|
|
|
102
102
|
|
|
103
103
|
### Reload threads after async auth
|
|
104
104
|
|
|
105
|
-
The first render of `<MyProvider>` may run before Clerk resolves the user on the client. Drop a small effect inside `<AssistantRuntimeProvider>` that calls `aui.threads
|
|
105
|
+
The first render of `<MyProvider>` may run before Clerk resolves the user on the client. Drop a small effect inside `<AssistantRuntimeProvider>` that calls `aui.threads.reload()` once the user is loaded:
|
|
106
106
|
|
|
107
107
|
```tsx title="app/components/ReloadOnAuth.tsx"
|
|
108
108
|
"use client";
|
|
@@ -115,7 +115,7 @@ export function ReloadOnAuth() {
|
|
|
115
115
|
const aui = useAui();
|
|
116
116
|
const { isLoaded, isSignedIn, user } = useUser();
|
|
117
117
|
useEffect(() => {
|
|
118
|
-
if (isLoaded && isSignedIn) aui.threads
|
|
118
|
+
if (isLoaded && isSignedIn) aui.threads.reload();
|
|
119
119
|
}, [isLoaded, isSignedIn, user?.id]);
|
|
120
120
|
return null;
|
|
121
121
|
}
|
|
@@ -18,7 +18,7 @@ Three places auth touches the integration:
|
|
|
18
18
|
|
|
19
19
|
1. **API routes** (`/api/chat`, `/api/threads/*`) call `auth()` server-side, return 401 if no session, then scope DB queries by `session.user.id`.
|
|
20
20
|
2. **`RemoteThreadListAdapter`** does nothing auth-specific; the cookie travels with the `fetch` calls automatically (same origin).
|
|
21
|
-
3. **Reload-on-auth**: if the session resolves after the initial render, call `aui.threads
|
|
21
|
+
3. **Reload-on-auth**: if the session resolves after the initial render, call `aui.threads.reload()` so the list re-fetches with the new identity.
|
|
22
22
|
|
|
23
23
|
## Setup
|
|
24
24
|
|
|
@@ -113,7 +113,7 @@ Apply the same pattern to `POST`, `PATCH`, and `DELETE` handlers. Always verify
|
|
|
113
113
|
|
|
114
114
|
### Reload threads after async auth
|
|
115
115
|
|
|
116
|
-
The first render of `<MyProvider>` may run before `auth()` resolves on the client. The thread list will be empty until the user refreshes. Drop a small effect into the layout to call `aui.threads
|
|
116
|
+
The first render of `<MyProvider>` may run before `auth()` resolves on the client. The thread list will be empty until the user refreshes. Drop a small effect into the layout to call `aui.threads.reload()` once the session resolves.
|
|
117
117
|
|
|
118
118
|
`useSession` requires `<SessionProvider>` higher in the tree. Wrap your root layout's client subtree once:
|
|
119
119
|
|
|
@@ -140,7 +140,7 @@ export function ReloadOnAuth() {
|
|
|
140
140
|
const aui = useAui();
|
|
141
141
|
const { status, data } = useSession();
|
|
142
142
|
useEffect(() => {
|
|
143
|
-
if (status === "authenticated") aui.threads
|
|
143
|
+
if (status === "authenticated") aui.threads.reload();
|
|
144
144
|
}, [status, data?.user?.id]);
|
|
145
145
|
return null;
|
|
146
146
|
}
|
|
@@ -35,7 +35,7 @@ LANGSMITH_API_KEY=lsv2_pt_...
|
|
|
35
35
|
LANGSMITH_PROJECT=assistant-ui
|
|
36
36
|
```
|
|
37
37
|
|
|
38
|
-
`LANGSMITH_PROJECT` controls which project receives traces; the default project applies if you omit it. See LangSmith's [
|
|
38
|
+
`LANGSMITH_PROJECT` controls which project receives traces; the default project applies if you omit it. See LangSmith's [project configuration guide](https://docs.langchain.com/langsmith/log-traces-to-project) for details.
|
|
39
39
|
|
|
40
40
|
</Step>
|
|
41
41
|
<Step>
|