@tanstack/ai-react 0.20.0 → 0.21.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/dist/esm/index.d.ts +1 -1
- package/dist/esm/types.d.ts +9 -9
- package/dist/esm/use-chat.d.ts +2 -2
- package/dist/esm/use-chat.js +88 -53
- package/dist/esm/use-chat.js.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +2 -0
- package/src/types.ts +19 -7
- package/src/use-chat.ts +118 -62
package/dist/esm/index.d.ts
CHANGED
|
@@ -20,4 +20,4 @@ export { useGenerateVideo } from './use-generate-video.js';
|
|
|
20
20
|
export type { UseGenerateVideoOptions, UseGenerateVideoReturn, } from './use-generate-video.js';
|
|
21
21
|
export { useAudioRecorder } from './use-audio-recorder.js';
|
|
22
22
|
export type { UseAudioRecorderOptions, UseAudioRecorderReturn, } from './use-audio-recorder.js';
|
|
23
|
-
export { fetchServerSentEvents, localStoragePersistence, sessionStoragePersistence, indexedDBPersistence, StorageUnavailableError, type ChatClientPersistence, type ChatPersistedState, type ChatPersistenceOption, type ChatStorageAdapter, type WebStoragePersistenceOptions, type IndexedDBPersistenceOptions, fetchHttpStream, xhrServerSentEvents, xhrHttpStream, stream, rpcStream, webSocket, createChatClientOptions, createMcpAppBridge, type McpAppBridge, type CreateMcpAppBridgeOptions, type ChatFetcher, type ChatFetcherInput, type ChatFetcherOptions, type ConnectionAdapter, type ConnectConnectionAdapter, type SubscribeConnectionAdapter, type RunAgentInputContext, type FetchConnectionOptions, type XhrConnectionOptions, type WebSocketConnectionOptions, type InferChatMessages, type GenerationClientState, type ImageGenerateInput, type AudioGenerateInput, type SpeechGenerateInput, type TranscriptionGenerateInput, type SummarizeGenerateInput, type VideoGenerateInput, type VideoGenerateResult, type VideoStatusInfo, } from '@tanstack/ai-client';
|
|
23
|
+
export { fetchServerSentEvents, localStoragePersistence, sessionStoragePersistence, indexedDBPersistence, StorageUnavailableError, type ChatClientPersistence, type ChatPersistedState, type ChatPersistenceOption, type ChatStorageAdapter, type WebStoragePersistenceOptions, type IndexedDBPersistenceOptions, fetchHttpStream, xhrServerSentEvents, xhrHttpStream, stream, rpcStream, webSocket, createChatClientOptions, createMcpAppBridge, type McpAppBridge, type CreateMcpAppBridgeOptions, type ChatFetcher, type ChatFetcherInput, type ChatFetcherOptions, type ConnectionAdapter, type ConnectConnectionAdapter, type SubscribeConnectionAdapter, type RunAgentInputContext, type FetchConnectionOptions, type XhrConnectionOptions, type WebSocketConnectionOptions, type InferChatMessages, type GenericInterrupt, type RegisteredGenericInterrupt, type GenerationClientState, type ImageGenerateInput, type AudioGenerateInput, type SpeechGenerateInput, type TranscriptionGenerateInput, type SummarizeGenerateInput, type VideoGenerateInput, type VideoGenerateResult, type VideoStatusInfo, } from '@tanstack/ai-client';
|
package/dist/esm/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { AnyClientTool, InferSchemaType, ModelMessage, RunAgentResumeItem, SchemaInput } from '@tanstack/ai/client';
|
|
2
|
-
import { AIDevtoolsDisplayOptions, BoundInterrupts, ChatClientOptions, ChatClientState,
|
|
1
|
+
import { AnyClientTool, InterruptDefinition, InferSchemaType, ModelMessage, RunAgentResumeItem, SchemaInput } from '@tanstack/ai/client';
|
|
2
|
+
import { AIDevtoolsDisplayOptions, BoundInterrupts, ChatClientOptions, ChatClientState, ResolvableChatInterrupt, ChatInterruptState, ChatRequestBody, ChatResumeState, ClientContextOptionFromTools, ConnectionStatus, DistributedOmit, InferredClientContext, MultimodalContent, QueueConfig, QueueOption, QueueStrategy, QueuedMessage, SendMessageOptions, UIMessage, WhenBusy } from '@tanstack/ai-client';
|
|
3
3
|
export type { ChatRequestBody, MultimodalContent, QueueConfig, QueuedMessage, QueueOption, QueueStrategy, SendMessageOptions, UIMessage, WhenBusy, };
|
|
4
4
|
/**
|
|
5
5
|
* Recursive partial — every property and every nested array element is optional.
|
|
@@ -35,7 +35,7 @@ export type DeepPartial<T> = T extends ReadonlyArray<infer U> ? Array<DeepPartia
|
|
|
35
35
|
* Note: Connection and body changes will recreate the ChatClient instance.
|
|
36
36
|
* To update these options, remount the component or use a key prop.
|
|
37
37
|
*/
|
|
38
|
-
export type UseChatOptions<TTools extends ReadonlyArray<AnyClientTool> = any, TSchema extends SchemaInput | undefined = undefined, TContext = InferredClientContext<TTools>> = DistributedOmit<ChatClientOptions<TTools, TContext>, 'onMessagesChange' | 'onLoadingChange' | 'onErrorChange' | 'onStatusChange' | 'onSubscriptionChange' | 'onConnectionStatusChange' | 'onSessionGeneratingChange' | 'onQueueChange' | 'onResumeStateChange' | 'onRunIdChange' | 'context' | 'devtools'> & {
|
|
38
|
+
export type UseChatOptions<TTools extends ReadonlyArray<AnyClientTool> = any, TSchema extends SchemaInput | undefined = undefined, TContext = InferredClientContext<TTools>, TInterrupts extends ReadonlyArray<InterruptDefinition<any, any, any, any>> = readonly []> = DistributedOmit<ChatClientOptions<TTools, TContext, TInterrupts>, 'onMessagesChange' | 'onLoadingChange' | 'onErrorChange' | 'onStatusChange' | 'onSubscriptionChange' | 'onConnectionStatusChange' | 'onSessionGeneratingChange' | 'onQueueChange' | 'onResumeStateChange' | 'onRunIdChange' | 'context' | 'devtools'> & {
|
|
39
39
|
/** Display options for TanStack AI Devtools. */
|
|
40
40
|
devtools?: AIDevtoolsDisplayOptions;
|
|
41
41
|
/**
|
|
@@ -56,7 +56,7 @@ export type UseChatOptions<TTools extends ReadonlyArray<AnyClientTool> = any, TS
|
|
|
56
56
|
* typed `partial` / `final` fields; when it is omitted (default), the return
|
|
57
57
|
* is unchanged.
|
|
58
58
|
*/
|
|
59
|
-
export type UseChatReturn<TTools extends ReadonlyArray<AnyClientTool> = any, TSchema extends SchemaInput | undefined = undefined> = BaseUseChatReturn<TTools, TSchema extends SchemaInput ? InferSchemaType<TSchema> : unknown> & (TSchema extends SchemaInput ? {
|
|
59
|
+
export type UseChatReturn<TTools extends ReadonlyArray<AnyClientTool> = any, TSchema extends SchemaInput | undefined = undefined, TInterrupts extends ReadonlyArray<InterruptDefinition<any, any, any, any>> = readonly []> = BaseUseChatReturn<TTools, TSchema extends SchemaInput ? InferSchemaType<TSchema> : unknown, TInterrupts> & (TSchema extends SchemaInput ? {
|
|
60
60
|
/**
|
|
61
61
|
* Live, progressively-parsed structured output. Updated from
|
|
62
62
|
* `TEXT_MESSAGE_CONTENT` deltas via `parsePartialJSON` while the stream
|
|
@@ -71,7 +71,7 @@ export type UseChatReturn<TTools extends ReadonlyArray<AnyClientTool> = any, TSc
|
|
|
71
71
|
*/
|
|
72
72
|
final: InferSchemaType<TSchema> | null;
|
|
73
73
|
} : Record<never, never>);
|
|
74
|
-
interface BaseUseChatReturn<TTools extends ReadonlyArray<AnyClientTool> = any, TData = unknown> {
|
|
74
|
+
interface BaseUseChatReturn<TTools extends ReadonlyArray<AnyClientTool> = any, TData = unknown, TInterrupts extends ReadonlyArray<InterruptDefinition<any, any, any, any>> = readonly []> {
|
|
75
75
|
/**
|
|
76
76
|
* Current messages in the conversation. When `outputSchema` is supplied,
|
|
77
77
|
* `messages[i].parts.find(p => p.type === 'structured-output')` is typed
|
|
@@ -128,14 +128,14 @@ interface BaseUseChatReturn<TTools extends ReadonlyArray<AnyClientTool> = any, T
|
|
|
128
128
|
* it, correlate a log line).
|
|
129
129
|
*/
|
|
130
130
|
runId: string | null;
|
|
131
|
-
interrupts: BoundInterrupts<TTools>;
|
|
131
|
+
interrupts: BoundInterrupts<TTools, TInterrupts>;
|
|
132
132
|
/** @deprecated Use `interrupts`. */
|
|
133
|
-
pendingInterrupts: BoundInterrupts<TTools>;
|
|
134
|
-
interruptErrors: ChatInterruptState<TTools>['interruptErrors'];
|
|
133
|
+
pendingInterrupts: BoundInterrupts<TTools, TInterrupts>;
|
|
134
|
+
interruptErrors: ChatInterruptState<TTools, TInterrupts>['interruptErrors'];
|
|
135
135
|
resuming: boolean;
|
|
136
136
|
resolveInterrupts: {
|
|
137
137
|
(approved: boolean): void;
|
|
138
|
-
(resolver: (interrupt:
|
|
138
|
+
(resolver: (interrupt: ResolvableChatInterrupt<TTools, TInterrupts>) => undefined): void;
|
|
139
139
|
};
|
|
140
140
|
cancelInterrupts: () => void;
|
|
141
141
|
retryInterrupts: () => void;
|
package/dist/esm/use-chat.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AnyClientTool, SchemaInput } from '@tanstack/ai/client';
|
|
1
|
+
import { AnyClientTool, InterruptDefinition, SchemaInput } from '@tanstack/ai/client';
|
|
2
2
|
import { InferredClientContext } from '@tanstack/ai-client';
|
|
3
3
|
import { UseChatOptions, UseChatReturn } from './types.js';
|
|
4
|
-
export declare function useChat<const TTools extends ReadonlyArray<AnyClientTool> = any, TSchema extends SchemaInput | undefined = undefined, TContext = InferredClientContext<TTools>>(options: UseChatOptions<TTools, TSchema, TContext>): UseChatReturn<TTools, TSchema>;
|
|
4
|
+
export declare function useChat<const TTools extends ReadonlyArray<AnyClientTool> = any, TSchema extends SchemaInput | undefined = undefined, TContext = InferredClientContext<TTools>, const TInterrupts extends ReadonlyArray<InterruptDefinition<any, any, any, any>> = readonly []>(options: UseChatOptions<TTools, TSchema, TContext, TInterrupts>): UseChatReturn<TTools, TSchema, TInterrupts>;
|
package/dist/esm/use-chat.js
CHANGED
|
@@ -36,7 +36,7 @@ function useChat(options) {
|
|
|
36
36
|
setRunId(target.getCurrentRunId());
|
|
37
37
|
setInterruptState(target.getInterruptState());
|
|
38
38
|
}, []);
|
|
39
|
-
const client = useMemo(() => {
|
|
39
|
+
const { client, initialization } = useMemo(() => {
|
|
40
40
|
const messagesToUse = options.initialMessages || [];
|
|
41
41
|
isFirstMountRef.current = false;
|
|
42
42
|
const initialOptions = optionsRef.current;
|
|
@@ -47,7 +47,19 @@ function useChat(options) {
|
|
|
47
47
|
if (!currentInstance || activeClientRef.current !== currentInstance) return;
|
|
48
48
|
return currentInstance;
|
|
49
49
|
};
|
|
50
|
-
const
|
|
50
|
+
const initializationState = {
|
|
51
|
+
ready: false,
|
|
52
|
+
callbacks: []
|
|
53
|
+
};
|
|
54
|
+
const runOrQueueForActiveInstance = (callback) => {
|
|
55
|
+
if (!initializationState.ready) {
|
|
56
|
+
initializationState.callbacks.push(callback);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const currentInstance = instanceHolder.current;
|
|
60
|
+
if (!currentInstance || activeClientRef.current !== currentInstance) return;
|
|
61
|
+
callback();
|
|
62
|
+
};
|
|
51
63
|
const instance = new ChatClient({
|
|
52
64
|
devtoolsBridgeFactory: createChatDevtoolsBridge,
|
|
53
65
|
...transport,
|
|
@@ -68,92 +80,116 @@ function useChat(options) {
|
|
|
68
80
|
},
|
|
69
81
|
onResponse: (response) => {
|
|
70
82
|
if (!getActiveInstance()) return;
|
|
71
|
-
optionsRef.current.onResponse?.(response);
|
|
83
|
+
return optionsRef.current.onResponse?.(response);
|
|
72
84
|
},
|
|
73
85
|
onChunk: (chunk) => {
|
|
74
|
-
|
|
75
|
-
|
|
86
|
+
runOrQueueForActiveInstance(() => {
|
|
87
|
+
optionsRef.current.onChunk?.(chunk);
|
|
88
|
+
});
|
|
76
89
|
},
|
|
77
90
|
onFinish: (message) => {
|
|
78
|
-
|
|
79
|
-
|
|
91
|
+
runOrQueueForActiveInstance(() => {
|
|
92
|
+
optionsRef.current.onFinish?.(message);
|
|
93
|
+
});
|
|
80
94
|
},
|
|
81
95
|
onError: (error) => {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
if (activeClientRef.current !== currentInstance) return;
|
|
88
|
-
optionsRef.current.onError?.(error);
|
|
96
|
+
runOrQueueForActiveInstance(() => {
|
|
97
|
+
optionsRef.current.onError?.(error);
|
|
98
|
+
});
|
|
89
99
|
},
|
|
90
100
|
...initialOptions.tools !== void 0 && { tools: initialOptions.tools },
|
|
101
|
+
...initialOptions.interrupts !== void 0 && { interrupts: initialOptions.interrupts },
|
|
91
102
|
onCustomEvent: (eventType, data, context) => {
|
|
92
|
-
|
|
93
|
-
|
|
103
|
+
runOrQueueForActiveInstance(() => {
|
|
104
|
+
optionsRef.current.onCustomEvent?.(eventType, data, context);
|
|
105
|
+
});
|
|
94
106
|
},
|
|
95
107
|
...options.streamProcessor !== void 0 && { streamProcessor: options.streamProcessor },
|
|
96
108
|
onMessagesChange: (newMessages) => {
|
|
97
|
-
|
|
98
|
-
|
|
109
|
+
runOrQueueForActiveInstance(() => {
|
|
110
|
+
setMessages(newMessages);
|
|
111
|
+
});
|
|
99
112
|
},
|
|
100
113
|
onLoadingChange: (newIsLoading) => {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
114
|
+
runOrQueueForActiveInstance(() => {
|
|
115
|
+
const currentInstance = getActiveInstance();
|
|
116
|
+
if (!currentInstance) return;
|
|
117
|
+
setIsLoading(newIsLoading);
|
|
118
|
+
syncResumeState(currentInstance);
|
|
119
|
+
});
|
|
105
120
|
},
|
|
106
121
|
onErrorChange: (newError) => {
|
|
107
|
-
|
|
108
|
-
|
|
122
|
+
runOrQueueForActiveInstance(() => {
|
|
123
|
+
setError(newError);
|
|
124
|
+
});
|
|
109
125
|
},
|
|
110
126
|
onStatusChange: (status) => {
|
|
111
|
-
|
|
112
|
-
|
|
127
|
+
runOrQueueForActiveInstance(() => {
|
|
128
|
+
setStatus(status);
|
|
129
|
+
});
|
|
113
130
|
},
|
|
114
131
|
onSubscriptionChange: (nextIsSubscribed) => {
|
|
115
|
-
|
|
116
|
-
|
|
132
|
+
runOrQueueForActiveInstance(() => {
|
|
133
|
+
setIsSubscribed(nextIsSubscribed);
|
|
134
|
+
});
|
|
117
135
|
},
|
|
118
136
|
onConnectionStatusChange: (nextStatus) => {
|
|
119
|
-
|
|
120
|
-
|
|
137
|
+
runOrQueueForActiveInstance(() => {
|
|
138
|
+
setConnectionStatus(nextStatus);
|
|
139
|
+
});
|
|
121
140
|
},
|
|
122
141
|
onSessionGeneratingChange: (isGenerating) => {
|
|
123
|
-
|
|
124
|
-
|
|
142
|
+
runOrQueueForActiveInstance(() => {
|
|
143
|
+
setSessionGenerating(isGenerating);
|
|
144
|
+
});
|
|
125
145
|
},
|
|
126
146
|
...optionsRef.current.queue !== void 0 && { queue: optionsRef.current.queue },
|
|
127
147
|
onQueueChange: (nextQueue) => {
|
|
128
|
-
|
|
129
|
-
|
|
148
|
+
runOrQueueForActiveInstance(() => {
|
|
149
|
+
setQueue(nextQueue);
|
|
150
|
+
});
|
|
130
151
|
},
|
|
131
152
|
onRunIdChange: (nextRunId) => {
|
|
132
|
-
|
|
133
|
-
|
|
153
|
+
runOrQueueForActiveInstance(() => {
|
|
154
|
+
setRunId(nextRunId);
|
|
155
|
+
});
|
|
134
156
|
},
|
|
135
157
|
onResumeStateChange: (_nextResumeState, nextPendingInterrupts) => {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
158
|
+
runOrQueueForActiveInstance(() => {
|
|
159
|
+
setInterruptState((current) => ({
|
|
160
|
+
...current,
|
|
161
|
+
interrupts: nextPendingInterrupts,
|
|
162
|
+
pendingInterrupts: nextPendingInterrupts
|
|
163
|
+
}));
|
|
164
|
+
});
|
|
142
165
|
},
|
|
143
|
-
onInterruptStateChange: (nextInterruptState) => {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
166
|
+
onInterruptStateChange: (nextInterruptState, context) => {
|
|
167
|
+
runOrQueueForActiveInstance(() => {
|
|
168
|
+
setInterruptState(nextInterruptState);
|
|
169
|
+
optionsRef.current.onInterruptStateChange?.(nextInterruptState, context);
|
|
170
|
+
});
|
|
147
171
|
}
|
|
148
172
|
});
|
|
149
173
|
instanceHolder.current = instance;
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
}
|
|
155
|
-
return instance;
|
|
174
|
+
return {
|
|
175
|
+
client: instance,
|
|
176
|
+
initialization: initializationState
|
|
177
|
+
};
|
|
156
178
|
}, [clientId, syncResumeState]);
|
|
179
|
+
useEffect(() => {
|
|
180
|
+
activeClientRef.current = client;
|
|
181
|
+
try {
|
|
182
|
+
while (initialization.callbacks.length > 0) {
|
|
183
|
+
if (activeClientRef.current !== client) {
|
|
184
|
+
initialization.callbacks.length = 0;
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
initialization.callbacks.shift()?.();
|
|
188
|
+
}
|
|
189
|
+
} finally {
|
|
190
|
+
initialization.ready = true;
|
|
191
|
+
}
|
|
192
|
+
}, [client, initialization]);
|
|
157
193
|
useEffect(() => {
|
|
158
194
|
const clientMessages = client.getMessages();
|
|
159
195
|
if (clientMessages !== messagesRef.current) setMessages(clientMessages);
|
|
@@ -197,7 +233,6 @@ function useChat(options) {
|
|
|
197
233
|
clearTimeout(cleanupInvalidationRef.current);
|
|
198
234
|
cleanupInvalidationRef.current = null;
|
|
199
235
|
}
|
|
200
|
-
activeClientRef.current = client;
|
|
201
236
|
client.mountDevtools();
|
|
202
237
|
syncResumeState(client);
|
|
203
238
|
return () => {
|
package/dist/esm/use-chat.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-chat.js","names":[],"sources":["../../src/use-chat.ts"],"sourcesContent":["import { ChatClient } from '@tanstack/ai-client'\nimport { createChatDevtoolsBridge } from '@tanstack/ai-client/devtools'\nimport { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react'\nimport type {\n AnyClientTool,\n InferSchemaType,\n ModelMessage,\n RunAgentResumeItem,\n SchemaInput,\n StreamChunk,\n} from '@tanstack/ai/client'\nimport type {\n ChatClientState,\n ChatInterrupt,\n ChatInterruptState,\n ChatResumeState,\n ConnectionStatus,\n InferredClientContext,\n QueuedMessage,\n SendMessageOptions,\n StructuredOutputPart,\n} from '@tanstack/ai-client'\n\nimport type {\n DeepPartial,\n MultimodalContent,\n UIMessage,\n UseChatOptions,\n UseChatReturn,\n} from './types'\n\nconst EMPTY_INTERRUPTS = Object.freeze([])\nconst EMPTY_INTERRUPT_ERRORS = Object.freeze([])\n\nexport function useChat<\n const TTools extends ReadonlyArray<AnyClientTool> = any,\n TSchema extends SchemaInput | undefined = undefined,\n TContext = InferredClientContext<TTools>,\n>(\n options: UseChatOptions<TTools, TSchema, TContext>,\n): UseChatReturn<TTools, TSchema> {\n // The hook's identity is its `threadId`. Reload with the same `threadId`\n // restores the same conversation. `hookId` is only a React recreation key\n // when no `threadId` is given. It is never sent on the wire.\n const hookId = useId()\n const clientId = options.threadId ?? hookId\n\n const [messages, setMessages] = useState<Array<UIMessage<TTools>>>(\n options.initialMessages || [],\n )\n const [isLoading, setIsLoading] = useState(false)\n const [error, setError] = useState<Error | undefined>(undefined)\n const [status, setStatus] = useState<ChatClientState>('ready')\n const [isSubscribed, setIsSubscribed] = useState(false)\n const [connectionStatus, setConnectionStatus] =\n useState<ConnectionStatus>('disconnected')\n const [sessionGenerating, setSessionGenerating] = useState(false)\n const [queue, setQueue] = useState<Array<QueuedMessage>>([])\n const [runId, setRunId] = useState<string | null>(null)\n const [interruptState, setInterruptState] = useState<\n ChatInterruptState<TTools>\n >(() => ({\n interrupts: EMPTY_INTERRUPTS,\n pendingInterrupts: EMPTY_INTERRUPTS,\n interruptErrors: EMPTY_INTERRUPT_ERRORS,\n resuming: false,\n }))\n\n type Partial = DeepPartial<InferSchemaType<NonNullable<TSchema>>>\n type Final = InferSchemaType<NonNullable<TSchema>>\n\n // Track current messages in a ref to preserve them when client is recreated\n const messagesRef = useRef<Array<UIMessage<TTools>>>(\n options.initialMessages || [],\n )\n const isFirstMountRef = useRef(true)\n const subscribedRef = useRef(false)\n const activeClientRef = useRef<ChatClient | null>(null)\n const cleanupInvalidationRef = useRef<ReturnType<typeof setTimeout> | null>(\n null,\n )\n const cleanupDisposalRef = useRef<{\n client: ChatClient\n timeout: ReturnType<typeof setTimeout>\n } | null>(null)\n\n // Update ref synchronously during render so it's always current when useMemo runs.\n messagesRef.current = messages\n\n // Track current options in a ref to avoid recreating client when options change\n const optionsRef = useRef<UseChatOptions<TTools, TSchema, TContext>>(options)\n optionsRef.current = options\n\n const syncResumeState = useCallback((target: ChatClient | null) => {\n if (!target) return\n setRunId(target.getCurrentRunId())\n setInterruptState(target.getInterruptState())\n }, [])\n\n // Create ChatClient instance with callbacks to sync state\n const client = useMemo(() => {\n const messagesToUse = options.initialMessages || []\n isFirstMountRef.current = false\n\n // Build options with conditional spreads for fields whose source\n // type is `T | undefined` but the ChatClient target uses a strict\n // optional (`field?: T`) — `exactOptionalPropertyTypes` rejects\n // assigning `undefined` to those, so we omit the key when absent.\n const initialOptions = optionsRef.current\n const transport = initialOptions.connection\n ? { connection: initialOptions.connection }\n : { fetcher: initialOptions.fetcher }\n\n const instanceHolder: {\n current: ChatClient<TTools, TContext> | undefined\n } = { current: undefined }\n const getActiveInstance = () => {\n const currentInstance = instanceHolder.current\n if (!currentInstance || activeClientRef.current !== currentInstance) {\n return undefined\n }\n return currentInstance\n }\n const pendingInitializationErrors: Array<Error> = []\n const instance = new ChatClient<TTools, TContext>({\n devtoolsBridgeFactory: createChatDevtoolsBridge,\n ...transport,\n initialMessages: messagesToUse,\n ...(typeof initialOptions.threadId === 'string' &&\n initialOptions.persistence\n ? {\n persistence: initialOptions.persistence,\n threadId: initialOptions.threadId,\n }\n : {\n ...(initialOptions.threadId !== undefined && {\n threadId: initialOptions.threadId,\n }),\n }),\n ...(initialOptions.body !== undefined && { body: initialOptions.body }),\n ...(initialOptions.forwardedProps !== undefined && {\n forwardedProps: initialOptions.forwardedProps,\n }),\n ...(initialOptions.initialResumeSnapshot !== undefined && {\n initialResumeSnapshot: initialOptions.initialResumeSnapshot,\n }),\n ...(initialOptions.context !== undefined && {\n context: initialOptions.context,\n }),\n devtools: {\n ...initialOptions.devtools,\n framework: 'react',\n hookName: 'useChat',\n outputKind: initialOptions.outputSchema ? 'structured' : 'chat',\n },\n onResponse: (response) => {\n if (!getActiveInstance()) return\n void optionsRef.current.onResponse?.(response)\n },\n onChunk: (chunk: StreamChunk) => {\n if (!getActiveInstance()) return\n optionsRef.current.onChunk?.(chunk)\n },\n onFinish: (message: UIMessage<TTools>) => {\n if (!getActiveInstance()) return\n optionsRef.current.onFinish?.(message)\n },\n onError: (error: Error) => {\n const currentInstance = instanceHolder.current\n if (!currentInstance) {\n pendingInitializationErrors.push(error)\n return\n }\n if (activeClientRef.current !== currentInstance) return\n optionsRef.current.onError?.(error)\n },\n ...(initialOptions.tools !== undefined && {\n tools: initialOptions.tools,\n }),\n onCustomEvent: (eventType, data, context) => {\n if (!getActiveInstance()) return\n optionsRef.current.onCustomEvent?.(eventType, data, context)\n },\n ...(options.streamProcessor !== undefined && {\n streamProcessor: options.streamProcessor,\n }),\n onMessagesChange: (newMessages: Array<UIMessage<TTools>>) => {\n if (!getActiveInstance()) return\n setMessages(newMessages)\n },\n onLoadingChange: (newIsLoading: boolean) => {\n const currentInstance = getActiveInstance()\n if (!currentInstance) return\n setIsLoading(newIsLoading)\n syncResumeState(currentInstance)\n },\n onErrorChange: (newError: Error | undefined) => {\n if (!getActiveInstance()) return\n setError(newError)\n },\n onStatusChange: (status: ChatClientState) => {\n if (!getActiveInstance()) return\n setStatus(status)\n },\n onSubscriptionChange: (nextIsSubscribed: boolean) => {\n if (!getActiveInstance()) return\n setIsSubscribed(nextIsSubscribed)\n },\n onConnectionStatusChange: (nextStatus: ConnectionStatus) => {\n if (!getActiveInstance()) return\n setConnectionStatus(nextStatus)\n },\n onSessionGeneratingChange: (isGenerating: boolean) => {\n if (!getActiveInstance()) return\n setSessionGenerating(isGenerating)\n },\n ...(optionsRef.current.queue !== undefined && {\n queue: optionsRef.current.queue,\n }),\n onQueueChange: (nextQueue: Array<QueuedMessage>) => {\n if (activeClientRef.current !== instance) return\n setQueue(nextQueue)\n },\n onRunIdChange: (nextRunId) => {\n if (!getActiveInstance()) return\n setRunId(nextRunId)\n },\n onResumeStateChange: (_nextResumeState, nextPendingInterrupts) => {\n if (!getActiveInstance()) return\n setInterruptState((current) => ({\n ...current,\n interrupts: nextPendingInterrupts,\n pendingInterrupts: nextPendingInterrupts,\n }))\n },\n onInterruptStateChange: (nextInterruptState) => {\n if (!getActiveInstance()) return\n setInterruptState(nextInterruptState)\n optionsRef.current.onInterruptStateChange?.(nextInterruptState)\n },\n })\n instanceHolder.current = instance\n activeClientRef.current = instance\n for (const error of pendingInitializationErrors) {\n if (activeClientRef.current !== instance) break\n optionsRef.current.onError?.(error)\n }\n return instance\n }, [clientId, syncResumeState])\n\n useEffect(() => {\n const clientMessages = client.getMessages()\n if (clientMessages !== messagesRef.current) {\n setMessages(clientMessages)\n }\n }, [client])\n\n // Sync each wire-payload slot in its own effect so an unrelated option\n // changing doesn't re-run the others. `updateOptions` declares strict-optional\n // fields and rejects explicit `undefined` under EOPT, so guard the optional\n // slots before passing them.\n useEffect(() => {\n client.updateOptions({ body: options.body })\n }, [client, options.body])\n\n useEffect(() => {\n if (options.forwardedProps !== undefined) {\n client.updateOptions({ forwardedProps: options.forwardedProps })\n }\n }, [client, options.forwardedProps])\n\n useEffect(() => {\n if (options.tools !== undefined) {\n client.updateOptions({ tools: options.tools })\n }\n }, [client, options.tools])\n\n useEffect(() => {\n client.updateOptions({ context: options.context })\n }, [client, options.context])\n\n useEffect(() => {\n if (options.queue !== undefined) {\n client.updateOptions({ queue: options.queue })\n }\n }, [client, options.queue])\n\n useEffect(() => {\n if (options.live) {\n client.subscribe()\n subscribedRef.current = true\n } else if (subscribedRef.current) {\n // Only tear down a subscription we actually started. Calling\n // `unsubscribe()` on initial mount (when `live` was never enabled) would\n // abort an in-flight delivery resume — `resumeInFlightRun` is kicked off\n // in the client constructor, and `unsubscribe()` cancels the shared\n // in-flight stream — so a mid-stream reload would drop its rejoin before\n // it delivers a single chunk. This is exactly why a reload froze instead\n // of continuing.\n client.unsubscribe()\n subscribedRef.current = false\n }\n }, [client, options.live])\n\n // ONLY THE VIEW ON SCREEN HOLDS A STREAM.\n //\n // A page can own many chats — 40 sandboxes, 40 conversations — and a browser\n // allows only ~6 connections per origin. One long-lived stream per chat reaches\n // that ceiling after a handful of views, and every request after it QUEUES:\n // measured, an in-page fetch took over two minutes while the same request from\n // outside the browser took 17ms. So the connection follows the view.\n //\n // Immediate, not deferred: the deferred teardown below can be skipped when the\n // same client remounts, which is right for disposal but useless for a\n // connection. `attach` is idempotent and `detach` keeps the transcript and the\n // resume pointer, so a Strict Mode remount is just detach-then-attach and the\n // run is picked straight back up from the durable log.\n useEffect(() => {\n client.attach()\n return () => {\n client.detach()\n }\n }, [client])\n\n useEffect(() => {\n if (cleanupDisposalRef.current?.client === client) {\n clearTimeout(cleanupDisposalRef.current.timeout)\n cleanupDisposalRef.current = null\n }\n if (cleanupInvalidationRef.current) {\n clearTimeout(cleanupInvalidationRef.current)\n cleanupInvalidationRef.current = null\n }\n activeClientRef.current = client\n client.mountDevtools()\n // Delivery-durability resume is transparent: the resumable SSE connection\n // adapter re-attaches via the browser's native Last-Event-ID on reconnect.\n // We only seed interrupt (state) resume from the client here.\n syncResumeState(client)\n\n return () => {\n cleanupInvalidationRef.current = setTimeout(() => {\n if (activeClientRef.current === client) {\n activeClientRef.current = null\n }\n cleanupInvalidationRef.current = null\n }, 0)\n // Soft cleanup only: do NOT stop/unsubscribe here. React Strict Mode\n // remounts fire this cleanup then re-attach the same client one tick\n // later; calling `stop()` would abort a constructor rejoin\n // (`resumeInFlightRun`) and can wipe the durable resume pointer before\n // the first chunk. Real teardown lives in the deferred dispose path\n // below, which only runs when the client is not remounted.\n // Subscribe/unsubscribe on `options.live` is still owned by the\n // dedicated effect above for live toggles.\n const disposal = {\n client,\n timeout: setTimeout(() => {\n if (optionsRef.current.live) {\n client.unsubscribe()\n } else {\n client.stop()\n }\n client.dispose()\n if (cleanupDisposalRef.current === disposal) {\n cleanupDisposalRef.current = null\n }\n }, 0),\n }\n cleanupDisposalRef.current = disposal\n }\n }, [client, syncResumeState])\n\n const sendMessage = useCallback(\n async (\n content: string | MultimodalContent,\n sendOptions?: SendMessageOptions,\n ) => {\n try {\n await client.sendMessage(content, undefined, sendOptions)\n } finally {\n syncResumeState(client)\n }\n },\n [client, syncResumeState],\n )\n\n const cancelQueued = useCallback(\n (id: string) => {\n client.cancelQueued(id)\n },\n [client, syncResumeState],\n )\n\n const append = useCallback(\n async (message: ModelMessage | UIMessage) => {\n try {\n await client.append(message)\n } finally {\n syncResumeState(client)\n }\n },\n [client, syncResumeState],\n )\n\n const reload = useCallback(async () => {\n try {\n await client.reload()\n } finally {\n syncResumeState(client)\n }\n }, [client, syncResumeState])\n\n const stop = useCallback(() => {\n client.stop()\n }, [client])\n\n const clear = useCallback(() => {\n client.clear()\n syncResumeState(client)\n }, [client, syncResumeState])\n\n const setMessagesManually = useCallback(\n (newMessages: Array<UIMessage<TTools>>) => {\n client.setMessagesManually(newMessages)\n },\n [client],\n )\n\n const addToolResult = useCallback(\n async (result: {\n toolCallId: string\n tool: string\n output: any\n state?: 'output-available' | 'output-error'\n errorText?: string\n }) => {\n await client.addToolResult(result)\n },\n [client],\n )\n\n const addToolApprovalResponse = useCallback(\n async (response: { id: string; approved: boolean }) => {\n await client.addToolApprovalResponse(response)\n syncResumeState(client)\n },\n [client, syncResumeState],\n )\n\n const resumeInterrupts = useCallback(\n async (resumeItems: Array<RunAgentResumeItem>, state?: ChatResumeState) => {\n const result = await client.resumeInterrupts(\n resumeItems,\n state ?? undefined,\n )\n syncResumeState(client)\n return result\n },\n [client, syncResumeState],\n )\n\n const resolveInterrupts = useCallback(\n (\n resolution: boolean | ((interrupt: ChatInterrupt<TTools>) => undefined),\n ) => {\n if (typeof resolution === 'boolean') {\n client.resolveInterrupts(resolution)\n } else {\n client.resolveInterrupts(resolution)\n }\n },\n [client],\n )\n\n const cancelInterrupts = useCallback(() => {\n client.cancelInterrupts()\n }, [client])\n\n const retryInterrupts = useCallback(() => {\n client.retryInterrupts()\n }, [client])\n\n const resumeInterruptsUnsafe = useCallback(\n (resumeItems: Array<RunAgentResumeItem>, state?: ChatResumeState) =>\n client.resumeInterruptsUnsafe(resumeItems, state),\n [client],\n )\n\n // The \"active\" structured-output part is the one on the assistant message\n // that follows the latest user message. No such message exists between\n // sendMessage() and the first chunk, so partial/final naturally read as\n // cleared. Historical parts on earlier assistant messages remain available\n // via `messages` directly.\n //\n // When there is NO user message yet (e.g. `initialMessages` contains only\n // a stale assistant turn or a system prompt) we deliberately return null\n // rather than scanning historical assistants — otherwise a `final` from a\n // previous session would leak into the hook value on first render.\n const renderedMessages = client.getMessages()\n\n const activeStructuredPart = useMemo<StructuredOutputPart | null>(() => {\n let lastUserIndex = -1\n for (let i = renderedMessages.length - 1; i >= 0; i--) {\n if (renderedMessages[i]?.role === 'user') {\n lastUserIndex = i\n break\n }\n }\n if (lastUserIndex === -1) return null\n for (let i = renderedMessages.length - 1; i > lastUserIndex; i--) {\n const m = renderedMessages[i]\n if (m?.role !== 'assistant') continue\n const part = m.parts.find(\n (p): p is StructuredOutputPart => p.type === 'structured-output',\n )\n if (part) return part\n }\n return null\n }, [renderedMessages])\n\n const partial = useMemo<Partial>(() => {\n if (!activeStructuredPart) return {} as Partial\n const v = activeStructuredPart.partial ?? activeStructuredPart.data\n return (v ?? {}) as Partial\n }, [activeStructuredPart])\n\n const final = useMemo<Final | null>(() => {\n if (!activeStructuredPart || activeStructuredPart.status !== 'complete') {\n return null\n }\n return activeStructuredPart.data as Final\n }, [activeStructuredPart])\n\n // The runtime shape unconditionally exposes partial/final; the public\n // return type hides them when no outputSchema was supplied. TS can't\n // structurally narrow across that conditional, so the `as` is the seam.\n // oxlint-disable-next-line eslint-js/no-restricted-syntax -- hook return shape diverges from generic UseChatReturn<TTools, TSchema> due to conditional type on TSchema; TS can't structurally narrow\n return {\n messages: renderedMessages,\n sendMessage,\n append,\n reload,\n stop,\n isLoading,\n error,\n status,\n isSubscribed,\n connectionStatus,\n sessionGenerating,\n setMessages: setMessagesManually,\n clear,\n addToolResult,\n addToolApprovalResponse,\n queue,\n cancelQueued,\n runId,\n interrupts: interruptState.interrupts,\n pendingInterrupts: interruptState.pendingInterrupts,\n interruptErrors: interruptState.interruptErrors,\n resuming: interruptState.resuming,\n resolveInterrupts,\n cancelInterrupts,\n retryInterrupts,\n resumeInterruptsUnsafe,\n resumeInterrupts,\n partial,\n final,\n } as unknown as UseChatReturn<TTools, TSchema>\n}\n"],"mappings":";;;;AA+BA,IAAM,mBAAmB,OAAO,OAAO,CAAC,CAAC;AACzC,IAAM,yBAAyB,OAAO,OAAO,CAAC,CAAC;AAE/C,SAAgB,QAKd,SACgC;CAIhC,MAAM,SAAS,MAAM;CACrB,MAAM,WAAW,QAAQ,YAAY;CAErC,MAAM,CAAC,UAAU,eAAe,SAC9B,QAAQ,mBAAmB,CAAC,CAC9B;CACA,MAAM,CAAC,WAAW,gBAAgB,SAAS,KAAK;CAChD,MAAM,CAAC,OAAO,YAAY,SAA4B,KAAA,CAAS;CAC/D,MAAM,CAAC,QAAQ,aAAa,SAA0B,OAAO;CAC7D,MAAM,CAAC,cAAc,mBAAmB,SAAS,KAAK;CACtD,MAAM,CAAC,kBAAkB,uBACvB,SAA2B,cAAc;CAC3C,MAAM,CAAC,mBAAmB,wBAAwB,SAAS,KAAK;CAChE,MAAM,CAAC,OAAO,YAAY,SAA+B,CAAC,CAAC;CAC3D,MAAM,CAAC,OAAO,YAAY,SAAwB,IAAI;CACtD,MAAM,CAAC,gBAAgB,qBAAqB,gBAEnC;EACP,YAAY;EACZ,mBAAmB;EACnB,iBAAiB;EACjB,UAAU;CACZ,EAAE;CAMF,MAAM,cAAc,OAClB,QAAQ,mBAAmB,CAAC,CAC9B;CACA,MAAM,kBAAkB,OAAO,IAAI;CACnC,MAAM,gBAAgB,OAAO,KAAK;CAClC,MAAM,kBAAkB,OAA0B,IAAI;CACtD,MAAM,yBAAyB,OAC7B,IACF;CACA,MAAM,qBAAqB,OAGjB,IAAI;CAGd,YAAY,UAAU;CAGtB,MAAM,aAAa,OAAkD,OAAO;CAC5E,WAAW,UAAU;CAErB,MAAM,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A;EACA;EACA;EACA;EACA;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"use-chat.js","names":[],"sources":["../../src/use-chat.ts"],"sourcesContent":["import { ChatClient } from '@tanstack/ai-client'\nimport { createChatDevtoolsBridge } from '@tanstack/ai-client/devtools'\nimport { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react'\nimport type {\n AnyClientTool,\n InferSchemaType,\n InterruptDefinition,\n ModelMessage,\n RunAgentResumeItem,\n SchemaInput,\n StreamChunk,\n} from '@tanstack/ai/client'\nimport type {\n ChatClientState,\n ResolvableChatInterrupt,\n ChatInterruptState,\n ChatResumeState,\n ConnectionStatus,\n InferredClientContext,\n QueuedMessage,\n SendMessageOptions,\n StructuredOutputPart,\n} from '@tanstack/ai-client'\n\nimport type {\n DeepPartial,\n MultimodalContent,\n UIMessage,\n UseChatOptions,\n UseChatReturn,\n} from './types'\n\nconst EMPTY_INTERRUPTS = Object.freeze([])\nconst EMPTY_INTERRUPT_ERRORS = Object.freeze([])\n\nexport function useChat<\n const TTools extends ReadonlyArray<AnyClientTool> = any,\n TSchema extends SchemaInput | undefined = undefined,\n TContext = InferredClientContext<TTools>,\n const TInterrupts extends ReadonlyArray<\n InterruptDefinition<any, any, any, any>\n > = readonly [],\n>(\n options: UseChatOptions<TTools, TSchema, TContext, TInterrupts>,\n): UseChatReturn<TTools, TSchema, TInterrupts> {\n // The hook's identity is its `threadId`. Reload with the same `threadId`\n // restores the same conversation. `hookId` is only a React recreation key\n // when no `threadId` is given. It is never sent on the wire.\n const hookId = useId()\n const clientId = options.threadId ?? hookId\n\n const [messages, setMessages] = useState<Array<UIMessage<TTools>>>(\n options.initialMessages || [],\n )\n const [isLoading, setIsLoading] = useState(false)\n const [error, setError] = useState<Error | undefined>(undefined)\n const [status, setStatus] = useState<ChatClientState>('ready')\n const [isSubscribed, setIsSubscribed] = useState(false)\n const [connectionStatus, setConnectionStatus] =\n useState<ConnectionStatus>('disconnected')\n const [sessionGenerating, setSessionGenerating] = useState(false)\n const [queue, setQueue] = useState<Array<QueuedMessage>>([])\n const [runId, setRunId] = useState<string | null>(null)\n const [interruptState, setInterruptState] = useState<\n ChatInterruptState<TTools, TInterrupts>\n >(() => ({\n interrupts: EMPTY_INTERRUPTS,\n pendingInterrupts: EMPTY_INTERRUPTS,\n interruptErrors: EMPTY_INTERRUPT_ERRORS,\n resuming: false,\n }))\n\n type Partial = DeepPartial<InferSchemaType<NonNullable<TSchema>>>\n type Final = InferSchemaType<NonNullable<TSchema>>\n\n // Track current messages in a ref to preserve them when client is recreated\n const messagesRef = useRef<Array<UIMessage<TTools>>>(\n options.initialMessages || [],\n )\n const isFirstMountRef = useRef(true)\n const subscribedRef = useRef(false)\n const activeClientRef = useRef<ChatClient | null>(null)\n const cleanupInvalidationRef = useRef<ReturnType<typeof setTimeout> | null>(\n null,\n )\n const cleanupDisposalRef = useRef<{\n client: ChatClient\n timeout: ReturnType<typeof setTimeout>\n } | null>(null)\n\n // Update ref synchronously during render so it's always current when useMemo runs.\n messagesRef.current = messages\n\n // Track current options in a ref to avoid recreating client when options change\n const optionsRef =\n useRef<UseChatOptions<TTools, TSchema, TContext, TInterrupts>>(options)\n optionsRef.current = options\n\n const syncResumeState = useCallback((target: ChatClient | null) => {\n if (!target) return\n setRunId(target.getCurrentRunId())\n setInterruptState(target.getInterruptState())\n }, [])\n\n // Create ChatClient instance with callbacks to sync state\n const { client, initialization } = useMemo(() => {\n const messagesToUse = options.initialMessages || []\n isFirstMountRef.current = false\n\n // Build options with conditional spreads for fields whose source\n // type is `T | undefined` but the ChatClient target uses a strict\n // optional (`field?: T`) — `exactOptionalPropertyTypes` rejects\n // assigning `undefined` to those, so we omit the key when absent.\n const initialOptions = optionsRef.current\n const transport = initialOptions.connection\n ? { connection: initialOptions.connection }\n : { fetcher: initialOptions.fetcher }\n\n const instanceHolder: {\n current: ChatClient<TTools, TContext, TInterrupts> | undefined\n } = { current: undefined }\n const getActiveInstance = () => {\n const currentInstance = instanceHolder.current\n if (!currentInstance || activeClientRef.current !== currentInstance) {\n return undefined\n }\n return currentInstance\n }\n // ChatClient may publish while its constructor is running or while async\n // persistence resolves before commit. Preserve those exact notifications\n // until this render commits; invoking them here would run state setters and\n // user callbacks for a client React may abandon.\n const initializationState = {\n ready: false,\n callbacks: [] as Array<() => void>,\n }\n const runOrQueueForActiveInstance = (callback: () => void) => {\n if (!initializationState.ready) {\n initializationState.callbacks.push(callback)\n return\n }\n const currentInstance = instanceHolder.current\n if (!currentInstance || activeClientRef.current !== currentInstance)\n return\n callback()\n }\n const instance = new ChatClient<TTools, TContext, TInterrupts>({\n devtoolsBridgeFactory: createChatDevtoolsBridge,\n ...transport,\n initialMessages: messagesToUse,\n ...(typeof initialOptions.threadId === 'string' &&\n initialOptions.persistence\n ? {\n persistence: initialOptions.persistence,\n threadId: initialOptions.threadId,\n }\n : {\n ...(initialOptions.threadId !== undefined && {\n threadId: initialOptions.threadId,\n }),\n }),\n ...(initialOptions.body !== undefined && { body: initialOptions.body }),\n ...(initialOptions.forwardedProps !== undefined && {\n forwardedProps: initialOptions.forwardedProps,\n }),\n ...(initialOptions.initialResumeSnapshot !== undefined && {\n initialResumeSnapshot: initialOptions.initialResumeSnapshot,\n }),\n ...(initialOptions.context !== undefined && {\n context: initialOptions.context,\n }),\n devtools: {\n ...initialOptions.devtools,\n framework: 'react',\n hookName: 'useChat',\n outputKind: initialOptions.outputSchema ? 'structured' : 'chat',\n },\n onResponse: (response) => {\n // ChatClient awaits this return value. Queuing would drop the promise.\n if (!getActiveInstance()) return\n return optionsRef.current.onResponse?.(response)\n },\n onChunk: (chunk: StreamChunk) => {\n runOrQueueForActiveInstance(() => {\n optionsRef.current.onChunk?.(chunk)\n })\n },\n onFinish: (message: UIMessage<TTools>) => {\n runOrQueueForActiveInstance(() => {\n optionsRef.current.onFinish?.(message)\n })\n },\n onError: (error: Error) => {\n runOrQueueForActiveInstance(() => {\n optionsRef.current.onError?.(error)\n })\n },\n ...(initialOptions.tools !== undefined && {\n tools: initialOptions.tools,\n }),\n ...(initialOptions.interrupts !== undefined && {\n interrupts: initialOptions.interrupts,\n }),\n onCustomEvent: (eventType, data, context) => {\n runOrQueueForActiveInstance(() => {\n optionsRef.current.onCustomEvent?.(eventType, data, context)\n })\n },\n ...(options.streamProcessor !== undefined && {\n streamProcessor: options.streamProcessor,\n }),\n onMessagesChange: (newMessages: Array<UIMessage<TTools>>) => {\n runOrQueueForActiveInstance(() => {\n setMessages(newMessages)\n })\n },\n onLoadingChange: (newIsLoading: boolean) => {\n runOrQueueForActiveInstance(() => {\n const currentInstance = getActiveInstance()\n if (!currentInstance) return\n setIsLoading(newIsLoading)\n syncResumeState(currentInstance)\n })\n },\n onErrorChange: (newError: Error | undefined) => {\n runOrQueueForActiveInstance(() => {\n setError(newError)\n })\n },\n onStatusChange: (status: ChatClientState) => {\n runOrQueueForActiveInstance(() => {\n setStatus(status)\n })\n },\n onSubscriptionChange: (nextIsSubscribed: boolean) => {\n runOrQueueForActiveInstance(() => {\n setIsSubscribed(nextIsSubscribed)\n })\n },\n onConnectionStatusChange: (nextStatus: ConnectionStatus) => {\n runOrQueueForActiveInstance(() => {\n setConnectionStatus(nextStatus)\n })\n },\n onSessionGeneratingChange: (isGenerating: boolean) => {\n runOrQueueForActiveInstance(() => {\n setSessionGenerating(isGenerating)\n })\n },\n ...(optionsRef.current.queue !== undefined && {\n queue: optionsRef.current.queue,\n }),\n onQueueChange: (nextQueue: Array<QueuedMessage>) => {\n runOrQueueForActiveInstance(() => {\n setQueue(nextQueue)\n })\n },\n onRunIdChange: (nextRunId) => {\n runOrQueueForActiveInstance(() => {\n setRunId(nextRunId)\n })\n },\n onResumeStateChange: (_nextResumeState, nextPendingInterrupts) => {\n runOrQueueForActiveInstance(() => {\n setInterruptState((current) => ({\n ...current,\n interrupts: nextPendingInterrupts,\n pendingInterrupts: nextPendingInterrupts,\n }))\n })\n },\n onInterruptStateChange: (nextInterruptState, context) => {\n runOrQueueForActiveInstance(() => {\n setInterruptState(nextInterruptState)\n optionsRef.current.onInterruptStateChange?.(\n nextInterruptState,\n context,\n )\n })\n },\n })\n instanceHolder.current = instance\n return { client: instance, initialization: initializationState }\n }, [clientId, syncResumeState])\n\n useEffect(() => {\n activeClientRef.current = client\n try {\n // Keep initialization closed while draining so callbacks published by a\n // queued callback are appended and delivered in the same commit.\n while (initialization.callbacks.length > 0) {\n if (activeClientRef.current !== client) {\n initialization.callbacks.length = 0\n break\n }\n initialization.callbacks.shift()?.()\n }\n } finally {\n // A throw from a queued user callback must not leave the queue closed.\n initialization.ready = true\n }\n }, [client, initialization])\n\n useEffect(() => {\n const clientMessages = client.getMessages()\n if (clientMessages !== messagesRef.current) {\n setMessages(clientMessages)\n }\n }, [client])\n\n // Sync each wire-payload slot in its own effect so an unrelated option\n // changing doesn't re-run the others. `updateOptions` declares strict-optional\n // fields and rejects explicit `undefined` under EOPT, so guard the optional\n // slots before passing them.\n useEffect(() => {\n client.updateOptions({ body: options.body })\n }, [client, options.body])\n\n useEffect(() => {\n if (options.forwardedProps !== undefined) {\n client.updateOptions({ forwardedProps: options.forwardedProps })\n }\n }, [client, options.forwardedProps])\n\n useEffect(() => {\n if (options.tools !== undefined) {\n client.updateOptions({ tools: options.tools })\n }\n }, [client, options.tools])\n\n useEffect(() => {\n client.updateOptions({ context: options.context })\n }, [client, options.context])\n\n useEffect(() => {\n if (options.queue !== undefined) {\n client.updateOptions({ queue: options.queue })\n }\n }, [client, options.queue])\n\n useEffect(() => {\n if (options.live) {\n client.subscribe()\n subscribedRef.current = true\n } else if (subscribedRef.current) {\n // Only tear down a subscription we actually started. Calling\n // `unsubscribe()` on initial mount (when `live` was never enabled) would\n // abort an in-flight delivery resume — `resumeInFlightRun` is kicked off\n // in the client constructor, and `unsubscribe()` cancels the shared\n // in-flight stream — so a mid-stream reload would drop its rejoin before\n // it delivers a single chunk. This is exactly why a reload froze instead\n // of continuing.\n client.unsubscribe()\n subscribedRef.current = false\n }\n }, [client, options.live])\n\n // ONLY THE VIEW ON SCREEN HOLDS A STREAM.\n //\n // A page can own many chats — 40 sandboxes, 40 conversations — and a browser\n // allows only ~6 connections per origin. One long-lived stream per chat reaches\n // that ceiling after a handful of views, and every request after it QUEUES:\n // measured, an in-page fetch took over two minutes while the same request from\n // outside the browser took 17ms. So the connection follows the view.\n //\n // Immediate, not deferred: the deferred teardown below can be skipped when the\n // same client remounts, which is right for disposal but useless for a\n // connection. `attach` is idempotent and `detach` keeps the transcript and the\n // resume pointer, so a Strict Mode remount is just detach-then-attach and the\n // run is picked straight back up from the durable log.\n useEffect(() => {\n client.attach()\n return () => {\n client.detach()\n }\n }, [client])\n\n useEffect(() => {\n if (cleanupDisposalRef.current?.client === client) {\n clearTimeout(cleanupDisposalRef.current.timeout)\n cleanupDisposalRef.current = null\n }\n if (cleanupInvalidationRef.current) {\n clearTimeout(cleanupInvalidationRef.current)\n cleanupInvalidationRef.current = null\n }\n client.mountDevtools()\n // Delivery-durability resume is transparent: the resumable SSE connection\n // adapter re-attaches via the browser's native Last-Event-ID on reconnect.\n // We only seed interrupt (state) resume from the client here.\n syncResumeState(client)\n\n return () => {\n cleanupInvalidationRef.current = setTimeout(() => {\n if (activeClientRef.current === client) {\n activeClientRef.current = null\n }\n cleanupInvalidationRef.current = null\n }, 0)\n // Soft cleanup only: do NOT stop/unsubscribe here. React Strict Mode\n // remounts fire this cleanup then re-attach the same client one tick\n // later; calling `stop()` would abort a constructor rejoin\n // (`resumeInFlightRun`) and can wipe the durable resume pointer before\n // the first chunk. Real teardown lives in the deferred dispose path\n // below, which only runs when the client is not remounted.\n // Subscribe/unsubscribe on `options.live` is still owned by the\n // dedicated effect above for live toggles.\n const disposal = {\n client,\n timeout: setTimeout(() => {\n if (optionsRef.current.live) {\n client.unsubscribe()\n } else {\n client.stop()\n }\n client.dispose()\n if (cleanupDisposalRef.current === disposal) {\n cleanupDisposalRef.current = null\n }\n }, 0),\n }\n cleanupDisposalRef.current = disposal\n }\n }, [client, syncResumeState])\n\n const sendMessage = useCallback(\n async (\n content: string | MultimodalContent,\n sendOptions?: SendMessageOptions,\n ) => {\n try {\n await client.sendMessage(content, undefined, sendOptions)\n } finally {\n syncResumeState(client)\n }\n },\n [client, syncResumeState],\n )\n\n const cancelQueued = useCallback(\n (id: string) => {\n client.cancelQueued(id)\n },\n [client, syncResumeState],\n )\n\n const append = useCallback(\n async (message: ModelMessage | UIMessage) => {\n try {\n await client.append(message)\n } finally {\n syncResumeState(client)\n }\n },\n [client, syncResumeState],\n )\n\n const reload = useCallback(async () => {\n try {\n await client.reload()\n } finally {\n syncResumeState(client)\n }\n }, [client, syncResumeState])\n\n const stop = useCallback(() => {\n client.stop()\n }, [client])\n\n const clear = useCallback(() => {\n client.clear()\n syncResumeState(client)\n }, [client, syncResumeState])\n\n const setMessagesManually = useCallback(\n (newMessages: Array<UIMessage<TTools>>) => {\n client.setMessagesManually(newMessages)\n },\n [client],\n )\n\n const addToolResult = useCallback(\n async (result: {\n toolCallId: string\n tool: string\n output: any\n state?: 'output-available' | 'output-error'\n errorText?: string\n }) => {\n await client.addToolResult(result)\n },\n [client],\n )\n\n const addToolApprovalResponse = useCallback(\n async (response: { id: string; approved: boolean }) => {\n await client.addToolApprovalResponse(response)\n syncResumeState(client)\n },\n [client, syncResumeState],\n )\n\n const resumeInterrupts = useCallback(\n async (resumeItems: Array<RunAgentResumeItem>, state?: ChatResumeState) => {\n const result = await client.resumeInterrupts(\n resumeItems,\n state ?? undefined,\n )\n syncResumeState(client)\n return result\n },\n [client, syncResumeState],\n )\n\n const resolveInterrupts = useCallback(\n (\n resolution:\n | boolean\n | ((\n interrupt: ResolvableChatInterrupt<TTools, TInterrupts>,\n ) => undefined),\n ) => {\n if (typeof resolution === 'boolean') {\n client.resolveInterrupts(resolution)\n } else {\n client.resolveInterrupts(resolution)\n }\n },\n [client],\n )\n\n const cancelInterrupts = useCallback(() => {\n client.cancelInterrupts()\n }, [client])\n\n const retryInterrupts = useCallback(() => {\n client.retryInterrupts()\n }, [client])\n\n const resumeInterruptsUnsafe = useCallback(\n (resumeItems: Array<RunAgentResumeItem>, state?: ChatResumeState) =>\n client.resumeInterruptsUnsafe(resumeItems, state),\n [client],\n )\n\n // The \"active\" structured-output part is the one on the assistant message\n // that follows the latest user message. No such message exists between\n // sendMessage() and the first chunk, so partial/final naturally read as\n // cleared. Historical parts on earlier assistant messages remain available\n // via `messages` directly.\n //\n // When there is NO user message yet (e.g. `initialMessages` contains only\n // a stale assistant turn or a system prompt) we deliberately return null\n // rather than scanning historical assistants — otherwise a `final` from a\n // previous session would leak into the hook value on first render.\n const renderedMessages = client.getMessages()\n\n const activeStructuredPart = useMemo<StructuredOutputPart | null>(() => {\n let lastUserIndex = -1\n for (let i = renderedMessages.length - 1; i >= 0; i--) {\n if (renderedMessages[i]?.role === 'user') {\n lastUserIndex = i\n break\n }\n }\n if (lastUserIndex === -1) return null\n for (let i = renderedMessages.length - 1; i > lastUserIndex; i--) {\n const m = renderedMessages[i]\n if (m?.role !== 'assistant') continue\n const part = m.parts.find(\n (p): p is StructuredOutputPart => p.type === 'structured-output',\n )\n if (part) return part\n }\n return null\n }, [renderedMessages])\n\n const partial = useMemo<Partial>(() => {\n if (!activeStructuredPart) return {} as Partial\n const v = activeStructuredPart.partial ?? activeStructuredPart.data\n return (v ?? {}) as Partial\n }, [activeStructuredPart])\n\n const final = useMemo<Final | null>(() => {\n if (!activeStructuredPart || activeStructuredPart.status !== 'complete') {\n return null\n }\n return activeStructuredPart.data as Final\n }, [activeStructuredPart])\n\n // The runtime shape unconditionally exposes partial/final; the public\n // return type hides them when no outputSchema was supplied. TS can't\n // structurally narrow across that conditional, so the `as` is the seam.\n // oxlint-disable-next-line eslint-js/no-restricted-syntax -- hook return shape diverges from generic UseChatReturn<TTools, TSchema> due to conditional type on TSchema; TS can't structurally narrow\n return {\n messages: renderedMessages,\n sendMessage,\n append,\n reload,\n stop,\n isLoading,\n error,\n status,\n isSubscribed,\n connectionStatus,\n sessionGenerating,\n setMessages: setMessagesManually,\n clear,\n addToolResult,\n addToolApprovalResponse,\n queue,\n cancelQueued,\n runId,\n interrupts: interruptState.interrupts,\n pendingInterrupts: interruptState.pendingInterrupts,\n interruptErrors: interruptState.interruptErrors,\n resuming: interruptState.resuming,\n resolveInterrupts,\n cancelInterrupts,\n retryInterrupts,\n resumeInterruptsUnsafe,\n resumeInterrupts,\n partial,\n final,\n } as unknown as UseChatReturn<TTools, TSchema, TInterrupts>\n}\n"],"mappings":";;;;AAgCA,IAAM,mBAAmB,OAAO,OAAO,CAAC,CAAC;AACzC,IAAM,yBAAyB,OAAO,OAAO,CAAC,CAAC;AAE/C,SAAgB,QAQd,SAC6C;CAI7C,MAAM,SAAS,MAAM;CACrB,MAAM,WAAW,QAAQ,YAAY;CAErC,MAAM,CAAC,UAAU,eAAe,SAC9B,QAAQ,mBAAmB,CAAC,CAC9B;CACA,MAAM,CAAC,WAAW,gBAAgB,SAAS,KAAK;CAChD,MAAM,CAAC,OAAO,YAAY,SAA4B,KAAA,CAAS;CAC/D,MAAM,CAAC,QAAQ,aAAa,SAA0B,OAAO;CAC7D,MAAM,CAAC,cAAc,mBAAmB,SAAS,KAAK;CACtD,MAAM,CAAC,kBAAkB,uBACvB,SAA2B,cAAc;CAC3C,MAAM,CAAC,mBAAmB,wBAAwB,SAAS,KAAK;CAChE,MAAM,CAAC,OAAO,YAAY,SAA+B,CAAC,CAAC;CAC3D,MAAM,CAAC,OAAO,YAAY,SAAwB,IAAI;CACtD,MAAM,CAAC,gBAAgB,qBAAqB,gBAEnC;EACP,YAAY;EACZ,mBAAmB;EACnB,iBAAiB;EACjB,UAAU;CACZ,EAAE;CAMF,MAAM,cAAc,OAClB,QAAQ,mBAAmB,CAAC,CAC9B;CACA,MAAM,kBAAkB,OAAO,IAAI;CACnC,MAAM,gBAAgB,OAAO,KAAK;CAClC,MAAM,kBAAkB,OAA0B,IAAI;CACtD,MAAM,yBAAyB,OAC7B,IACF;CACA,MAAM,qBAAqB,OAGjB,IAAI;CAGd,YAAY,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C,oBAAoB,CAAC;CAEzB,MAAM,QAAQ,cAA4B;EACxC,IAAI,CAAC,wBAAwB,qBAAqB,WAAW,YAC3D,OAAO;EAET,OAAO,qBAAqB;CAC9B,GAAG,CAAC,oBAAoB,CAAC;CAMzB,OAAO;EACL,UAAU;EACV;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,aAAa;EACb;EACA;EACA;EACA;EACA;EACA;EACA,YAAY,eAAe;EAC3B,mBAAmB,eAAe;EAClC,iBAAiB,eAAe;EAChC,UAAU,eAAe;EACzB;EACA;EACA;EACA;EACA;EACA;EACA;CACF;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.1",
|
|
4
4
|
"description": "React hooks for TanStack AI streaming chat, realtime voice, structured outputs, and media generation.",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -49,13 +49,13 @@
|
|
|
49
49
|
"media-generation"
|
|
50
50
|
],
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@tanstack/ai-client": "^0.
|
|
52
|
+
"@tanstack/ai-client": "^0.25.1"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
55
|
"@mcp-ui/client": "^7",
|
|
56
56
|
"@types/react": ">=18.0.0",
|
|
57
57
|
"react": ">=18.0.0",
|
|
58
|
-
"@tanstack/ai": "^0.
|
|
58
|
+
"@tanstack/ai": "^0.47.1"
|
|
59
59
|
},
|
|
60
60
|
"peerDependenciesMeta": {
|
|
61
61
|
"@mcp-ui/client": {
|
|
@@ -71,7 +71,7 @@
|
|
|
71
71
|
"jsdom": "^27.4.0",
|
|
72
72
|
"react": "^19.2.3",
|
|
73
73
|
"vite": "^8.2.1",
|
|
74
|
-
"@tanstack/ai": "0.
|
|
74
|
+
"@tanstack/ai": "0.47.1"
|
|
75
75
|
},
|
|
76
76
|
"scripts": {
|
|
77
77
|
"clean": "premove ./build ./dist",
|
package/src/index.ts
CHANGED
|
@@ -100,6 +100,8 @@ export {
|
|
|
100
100
|
type XhrConnectionOptions,
|
|
101
101
|
type WebSocketConnectionOptions,
|
|
102
102
|
type InferChatMessages,
|
|
103
|
+
type GenericInterrupt,
|
|
104
|
+
type RegisteredGenericInterrupt,
|
|
103
105
|
type GenerationClientState,
|
|
104
106
|
type ImageGenerateInput,
|
|
105
107
|
type AudioGenerateInput,
|
package/src/types.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
AnyClientTool,
|
|
3
|
+
InterruptDefinition,
|
|
3
4
|
InferSchemaType,
|
|
4
5
|
ModelMessage,
|
|
5
6
|
RunAgentResumeItem,
|
|
@@ -10,7 +11,7 @@ import type {
|
|
|
10
11
|
BoundInterrupts,
|
|
11
12
|
ChatClientOptions,
|
|
12
13
|
ChatClientState,
|
|
13
|
-
|
|
14
|
+
ResolvableChatInterrupt,
|
|
14
15
|
ChatInterruptState,
|
|
15
16
|
ChatRequestBody,
|
|
16
17
|
ChatResumeState,
|
|
@@ -83,8 +84,10 @@ export type UseChatOptions<
|
|
|
83
84
|
TTools extends ReadonlyArray<AnyClientTool> = any,
|
|
84
85
|
TSchema extends SchemaInput | undefined = undefined,
|
|
85
86
|
TContext = InferredClientContext<TTools>,
|
|
87
|
+
TInterrupts extends ReadonlyArray<InterruptDefinition<any, any, any, any>> =
|
|
88
|
+
readonly [],
|
|
86
89
|
> = DistributedOmit<
|
|
87
|
-
ChatClientOptions<TTools, TContext>,
|
|
90
|
+
ChatClientOptions<TTools, TContext, TInterrupts>,
|
|
88
91
|
| 'onMessagesChange'
|
|
89
92
|
| 'onLoadingChange'
|
|
90
93
|
| 'onErrorChange'
|
|
@@ -122,9 +125,12 @@ export type UseChatOptions<
|
|
|
122
125
|
export type UseChatReturn<
|
|
123
126
|
TTools extends ReadonlyArray<AnyClientTool> = any,
|
|
124
127
|
TSchema extends SchemaInput | undefined = undefined,
|
|
128
|
+
TInterrupts extends ReadonlyArray<InterruptDefinition<any, any, any, any>> =
|
|
129
|
+
readonly [],
|
|
125
130
|
> = BaseUseChatReturn<
|
|
126
131
|
TTools,
|
|
127
|
-
TSchema extends SchemaInput ? InferSchemaType<TSchema> : unknown
|
|
132
|
+
TSchema extends SchemaInput ? InferSchemaType<TSchema> : unknown,
|
|
133
|
+
TInterrupts
|
|
128
134
|
> &
|
|
129
135
|
(TSchema extends SchemaInput
|
|
130
136
|
? {
|
|
@@ -147,6 +153,8 @@ export type UseChatReturn<
|
|
|
147
153
|
interface BaseUseChatReturn<
|
|
148
154
|
TTools extends ReadonlyArray<AnyClientTool> = any,
|
|
149
155
|
TData = unknown,
|
|
156
|
+
TInterrupts extends ReadonlyArray<InterruptDefinition<any, any, any, any>> =
|
|
157
|
+
readonly [],
|
|
150
158
|
> {
|
|
151
159
|
/**
|
|
152
160
|
* Current messages in the conversation. When `outputSchema` is supplied,
|
|
@@ -214,14 +222,18 @@ interface BaseUseChatReturn<
|
|
|
214
222
|
* it, correlate a log line).
|
|
215
223
|
*/
|
|
216
224
|
runId: string | null
|
|
217
|
-
interrupts: BoundInterrupts<TTools>
|
|
225
|
+
interrupts: BoundInterrupts<TTools, TInterrupts>
|
|
218
226
|
/** @deprecated Use `interrupts`. */
|
|
219
|
-
pendingInterrupts: BoundInterrupts<TTools>
|
|
220
|
-
interruptErrors: ChatInterruptState<TTools>['interruptErrors']
|
|
227
|
+
pendingInterrupts: BoundInterrupts<TTools, TInterrupts>
|
|
228
|
+
interruptErrors: ChatInterruptState<TTools, TInterrupts>['interruptErrors']
|
|
221
229
|
resuming: boolean
|
|
222
230
|
resolveInterrupts: {
|
|
223
231
|
(approved: boolean): void
|
|
224
|
-
(
|
|
232
|
+
(
|
|
233
|
+
resolver: (
|
|
234
|
+
interrupt: ResolvableChatInterrupt<TTools, TInterrupts>,
|
|
235
|
+
) => undefined,
|
|
236
|
+
): void
|
|
225
237
|
}
|
|
226
238
|
cancelInterrupts: () => void
|
|
227
239
|
retryInterrupts: () => void
|
package/src/use-chat.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react'
|
|
|
4
4
|
import type {
|
|
5
5
|
AnyClientTool,
|
|
6
6
|
InferSchemaType,
|
|
7
|
+
InterruptDefinition,
|
|
7
8
|
ModelMessage,
|
|
8
9
|
RunAgentResumeItem,
|
|
9
10
|
SchemaInput,
|
|
@@ -11,7 +12,7 @@ import type {
|
|
|
11
12
|
} from '@tanstack/ai/client'
|
|
12
13
|
import type {
|
|
13
14
|
ChatClientState,
|
|
14
|
-
|
|
15
|
+
ResolvableChatInterrupt,
|
|
15
16
|
ChatInterruptState,
|
|
16
17
|
ChatResumeState,
|
|
17
18
|
ConnectionStatus,
|
|
@@ -36,9 +37,12 @@ export function useChat<
|
|
|
36
37
|
const TTools extends ReadonlyArray<AnyClientTool> = any,
|
|
37
38
|
TSchema extends SchemaInput | undefined = undefined,
|
|
38
39
|
TContext = InferredClientContext<TTools>,
|
|
40
|
+
const TInterrupts extends ReadonlyArray<
|
|
41
|
+
InterruptDefinition<any, any, any, any>
|
|
42
|
+
> = readonly [],
|
|
39
43
|
>(
|
|
40
|
-
options: UseChatOptions<TTools, TSchema, TContext>,
|
|
41
|
-
): UseChatReturn<TTools, TSchema> {
|
|
44
|
+
options: UseChatOptions<TTools, TSchema, TContext, TInterrupts>,
|
|
45
|
+
): UseChatReturn<TTools, TSchema, TInterrupts> {
|
|
42
46
|
// The hook's identity is its `threadId`. Reload with the same `threadId`
|
|
43
47
|
// restores the same conversation. `hookId` is only a React recreation key
|
|
44
48
|
// when no `threadId` is given. It is never sent on the wire.
|
|
@@ -58,7 +62,7 @@ export function useChat<
|
|
|
58
62
|
const [queue, setQueue] = useState<Array<QueuedMessage>>([])
|
|
59
63
|
const [runId, setRunId] = useState<string | null>(null)
|
|
60
64
|
const [interruptState, setInterruptState] = useState<
|
|
61
|
-
ChatInterruptState<TTools>
|
|
65
|
+
ChatInterruptState<TTools, TInterrupts>
|
|
62
66
|
>(() => ({
|
|
63
67
|
interrupts: EMPTY_INTERRUPTS,
|
|
64
68
|
pendingInterrupts: EMPTY_INTERRUPTS,
|
|
@@ -88,7 +92,8 @@ export function useChat<
|
|
|
88
92
|
messagesRef.current = messages
|
|
89
93
|
|
|
90
94
|
// Track current options in a ref to avoid recreating client when options change
|
|
91
|
-
const optionsRef =
|
|
95
|
+
const optionsRef =
|
|
96
|
+
useRef<UseChatOptions<TTools, TSchema, TContext, TInterrupts>>(options)
|
|
92
97
|
optionsRef.current = options
|
|
93
98
|
|
|
94
99
|
const syncResumeState = useCallback((target: ChatClient | null) => {
|
|
@@ -98,7 +103,7 @@ export function useChat<
|
|
|
98
103
|
}, [])
|
|
99
104
|
|
|
100
105
|
// Create ChatClient instance with callbacks to sync state
|
|
101
|
-
const client = useMemo(() => {
|
|
106
|
+
const { client, initialization } = useMemo(() => {
|
|
102
107
|
const messagesToUse = options.initialMessages || []
|
|
103
108
|
isFirstMountRef.current = false
|
|
104
109
|
|
|
@@ -112,7 +117,7 @@ export function useChat<
|
|
|
112
117
|
: { fetcher: initialOptions.fetcher }
|
|
113
118
|
|
|
114
119
|
const instanceHolder: {
|
|
115
|
-
current: ChatClient<TTools, TContext> | undefined
|
|
120
|
+
current: ChatClient<TTools, TContext, TInterrupts> | undefined
|
|
116
121
|
} = { current: undefined }
|
|
117
122
|
const getActiveInstance = () => {
|
|
118
123
|
const currentInstance = instanceHolder.current
|
|
@@ -121,8 +126,25 @@ export function useChat<
|
|
|
121
126
|
}
|
|
122
127
|
return currentInstance
|
|
123
128
|
}
|
|
124
|
-
|
|
125
|
-
|
|
129
|
+
// ChatClient may publish while its constructor is running or while async
|
|
130
|
+
// persistence resolves before commit. Preserve those exact notifications
|
|
131
|
+
// until this render commits; invoking them here would run state setters and
|
|
132
|
+
// user callbacks for a client React may abandon.
|
|
133
|
+
const initializationState = {
|
|
134
|
+
ready: false,
|
|
135
|
+
callbacks: [] as Array<() => void>,
|
|
136
|
+
}
|
|
137
|
+
const runOrQueueForActiveInstance = (callback: () => void) => {
|
|
138
|
+
if (!initializationState.ready) {
|
|
139
|
+
initializationState.callbacks.push(callback)
|
|
140
|
+
return
|
|
141
|
+
}
|
|
142
|
+
const currentInstance = instanceHolder.current
|
|
143
|
+
if (!currentInstance || activeClientRef.current !== currentInstance)
|
|
144
|
+
return
|
|
145
|
+
callback()
|
|
146
|
+
}
|
|
147
|
+
const instance = new ChatClient<TTools, TContext, TInterrupts>({
|
|
126
148
|
devtoolsBridgeFactory: createChatDevtoolsBridge,
|
|
127
149
|
...transport,
|
|
128
150
|
initialMessages: messagesToUse,
|
|
@@ -154,100 +176,131 @@ export function useChat<
|
|
|
154
176
|
outputKind: initialOptions.outputSchema ? 'structured' : 'chat',
|
|
155
177
|
},
|
|
156
178
|
onResponse: (response) => {
|
|
179
|
+
// ChatClient awaits this return value. Queuing would drop the promise.
|
|
157
180
|
if (!getActiveInstance()) return
|
|
158
|
-
|
|
181
|
+
return optionsRef.current.onResponse?.(response)
|
|
159
182
|
},
|
|
160
183
|
onChunk: (chunk: StreamChunk) => {
|
|
161
|
-
|
|
162
|
-
|
|
184
|
+
runOrQueueForActiveInstance(() => {
|
|
185
|
+
optionsRef.current.onChunk?.(chunk)
|
|
186
|
+
})
|
|
163
187
|
},
|
|
164
188
|
onFinish: (message: UIMessage<TTools>) => {
|
|
165
|
-
|
|
166
|
-
|
|
189
|
+
runOrQueueForActiveInstance(() => {
|
|
190
|
+
optionsRef.current.onFinish?.(message)
|
|
191
|
+
})
|
|
167
192
|
},
|
|
168
193
|
onError: (error: Error) => {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
return
|
|
173
|
-
}
|
|
174
|
-
if (activeClientRef.current !== currentInstance) return
|
|
175
|
-
optionsRef.current.onError?.(error)
|
|
194
|
+
runOrQueueForActiveInstance(() => {
|
|
195
|
+
optionsRef.current.onError?.(error)
|
|
196
|
+
})
|
|
176
197
|
},
|
|
177
198
|
...(initialOptions.tools !== undefined && {
|
|
178
199
|
tools: initialOptions.tools,
|
|
179
200
|
}),
|
|
201
|
+
...(initialOptions.interrupts !== undefined && {
|
|
202
|
+
interrupts: initialOptions.interrupts,
|
|
203
|
+
}),
|
|
180
204
|
onCustomEvent: (eventType, data, context) => {
|
|
181
|
-
|
|
182
|
-
|
|
205
|
+
runOrQueueForActiveInstance(() => {
|
|
206
|
+
optionsRef.current.onCustomEvent?.(eventType, data, context)
|
|
207
|
+
})
|
|
183
208
|
},
|
|
184
209
|
...(options.streamProcessor !== undefined && {
|
|
185
210
|
streamProcessor: options.streamProcessor,
|
|
186
211
|
}),
|
|
187
212
|
onMessagesChange: (newMessages: Array<UIMessage<TTools>>) => {
|
|
188
|
-
|
|
189
|
-
|
|
213
|
+
runOrQueueForActiveInstance(() => {
|
|
214
|
+
setMessages(newMessages)
|
|
215
|
+
})
|
|
190
216
|
},
|
|
191
217
|
onLoadingChange: (newIsLoading: boolean) => {
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
218
|
+
runOrQueueForActiveInstance(() => {
|
|
219
|
+
const currentInstance = getActiveInstance()
|
|
220
|
+
if (!currentInstance) return
|
|
221
|
+
setIsLoading(newIsLoading)
|
|
222
|
+
syncResumeState(currentInstance)
|
|
223
|
+
})
|
|
196
224
|
},
|
|
197
225
|
onErrorChange: (newError: Error | undefined) => {
|
|
198
|
-
|
|
199
|
-
|
|
226
|
+
runOrQueueForActiveInstance(() => {
|
|
227
|
+
setError(newError)
|
|
228
|
+
})
|
|
200
229
|
},
|
|
201
230
|
onStatusChange: (status: ChatClientState) => {
|
|
202
|
-
|
|
203
|
-
|
|
231
|
+
runOrQueueForActiveInstance(() => {
|
|
232
|
+
setStatus(status)
|
|
233
|
+
})
|
|
204
234
|
},
|
|
205
235
|
onSubscriptionChange: (nextIsSubscribed: boolean) => {
|
|
206
|
-
|
|
207
|
-
|
|
236
|
+
runOrQueueForActiveInstance(() => {
|
|
237
|
+
setIsSubscribed(nextIsSubscribed)
|
|
238
|
+
})
|
|
208
239
|
},
|
|
209
240
|
onConnectionStatusChange: (nextStatus: ConnectionStatus) => {
|
|
210
|
-
|
|
211
|
-
|
|
241
|
+
runOrQueueForActiveInstance(() => {
|
|
242
|
+
setConnectionStatus(nextStatus)
|
|
243
|
+
})
|
|
212
244
|
},
|
|
213
245
|
onSessionGeneratingChange: (isGenerating: boolean) => {
|
|
214
|
-
|
|
215
|
-
|
|
246
|
+
runOrQueueForActiveInstance(() => {
|
|
247
|
+
setSessionGenerating(isGenerating)
|
|
248
|
+
})
|
|
216
249
|
},
|
|
217
250
|
...(optionsRef.current.queue !== undefined && {
|
|
218
251
|
queue: optionsRef.current.queue,
|
|
219
252
|
}),
|
|
220
253
|
onQueueChange: (nextQueue: Array<QueuedMessage>) => {
|
|
221
|
-
|
|
222
|
-
|
|
254
|
+
runOrQueueForActiveInstance(() => {
|
|
255
|
+
setQueue(nextQueue)
|
|
256
|
+
})
|
|
223
257
|
},
|
|
224
258
|
onRunIdChange: (nextRunId) => {
|
|
225
|
-
|
|
226
|
-
|
|
259
|
+
runOrQueueForActiveInstance(() => {
|
|
260
|
+
setRunId(nextRunId)
|
|
261
|
+
})
|
|
227
262
|
},
|
|
228
263
|
onResumeStateChange: (_nextResumeState, nextPendingInterrupts) => {
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
264
|
+
runOrQueueForActiveInstance(() => {
|
|
265
|
+
setInterruptState((current) => ({
|
|
266
|
+
...current,
|
|
267
|
+
interrupts: nextPendingInterrupts,
|
|
268
|
+
pendingInterrupts: nextPendingInterrupts,
|
|
269
|
+
}))
|
|
270
|
+
})
|
|
235
271
|
},
|
|
236
|
-
onInterruptStateChange: (nextInterruptState) => {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
272
|
+
onInterruptStateChange: (nextInterruptState, context) => {
|
|
273
|
+
runOrQueueForActiveInstance(() => {
|
|
274
|
+
setInterruptState(nextInterruptState)
|
|
275
|
+
optionsRef.current.onInterruptStateChange?.(
|
|
276
|
+
nextInterruptState,
|
|
277
|
+
context,
|
|
278
|
+
)
|
|
279
|
+
})
|
|
240
280
|
},
|
|
241
281
|
})
|
|
242
282
|
instanceHolder.current = instance
|
|
243
|
-
|
|
244
|
-
for (const error of pendingInitializationErrors) {
|
|
245
|
-
if (activeClientRef.current !== instance) break
|
|
246
|
-
optionsRef.current.onError?.(error)
|
|
247
|
-
}
|
|
248
|
-
return instance
|
|
283
|
+
return { client: instance, initialization: initializationState }
|
|
249
284
|
}, [clientId, syncResumeState])
|
|
250
285
|
|
|
286
|
+
useEffect(() => {
|
|
287
|
+
activeClientRef.current = client
|
|
288
|
+
try {
|
|
289
|
+
// Keep initialization closed while draining so callbacks published by a
|
|
290
|
+
// queued callback are appended and delivered in the same commit.
|
|
291
|
+
while (initialization.callbacks.length > 0) {
|
|
292
|
+
if (activeClientRef.current !== client) {
|
|
293
|
+
initialization.callbacks.length = 0
|
|
294
|
+
break
|
|
295
|
+
}
|
|
296
|
+
initialization.callbacks.shift()?.()
|
|
297
|
+
}
|
|
298
|
+
} finally {
|
|
299
|
+
// A throw from a queued user callback must not leave the queue closed.
|
|
300
|
+
initialization.ready = true
|
|
301
|
+
}
|
|
302
|
+
}, [client, initialization])
|
|
303
|
+
|
|
251
304
|
useEffect(() => {
|
|
252
305
|
const clientMessages = client.getMessages()
|
|
253
306
|
if (clientMessages !== messagesRef.current) {
|
|
@@ -331,7 +384,6 @@ export function useChat<
|
|
|
331
384
|
clearTimeout(cleanupInvalidationRef.current)
|
|
332
385
|
cleanupInvalidationRef.current = null
|
|
333
386
|
}
|
|
334
|
-
activeClientRef.current = client
|
|
335
387
|
client.mountDevtools()
|
|
336
388
|
// Delivery-durability resume is transparent: the resumable SSE connection
|
|
337
389
|
// adapter re-attaches via the browser's native Last-Event-ID on reconnect.
|
|
@@ -462,7 +514,11 @@ export function useChat<
|
|
|
462
514
|
|
|
463
515
|
const resolveInterrupts = useCallback(
|
|
464
516
|
(
|
|
465
|
-
resolution:
|
|
517
|
+
resolution:
|
|
518
|
+
| boolean
|
|
519
|
+
| ((
|
|
520
|
+
interrupt: ResolvableChatInterrupt<TTools, TInterrupts>,
|
|
521
|
+
) => undefined),
|
|
466
522
|
) => {
|
|
467
523
|
if (typeof resolution === 'boolean') {
|
|
468
524
|
client.resolveInterrupts(resolution)
|
|
@@ -566,5 +622,5 @@ export function useChat<
|
|
|
566
622
|
resumeInterrupts,
|
|
567
623
|
partial,
|
|
568
624
|
final,
|
|
569
|
-
} as unknown as UseChatReturn<TTools, TSchema>
|
|
625
|
+
} as unknown as UseChatReturn<TTools, TSchema, TInterrupts>
|
|
570
626
|
}
|