@tanstack/ai-react 0.15.14 → 0.16.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/dist/esm/index.d.ts +23 -0
- package/dist/esm/index.js +34 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/mcp-apps.d.ts +20 -0
- package/dist/esm/mcp-apps.js +52 -0
- package/dist/esm/mcp-apps.js.map +1 -0
- package/dist/esm/realtime-types.d.ts +109 -0
- package/dist/esm/types.d.ts +150 -0
- package/dist/esm/use-audio-recorder.d.ts +43 -0
- package/dist/esm/use-audio-recorder.js +49 -0
- package/dist/esm/use-audio-recorder.js.map +1 -0
- package/dist/esm/use-chat.d.ts +4 -0
- package/dist/esm/use-chat.js +258 -0
- package/dist/esm/use-chat.js.map +1 -0
- package/dist/esm/use-generate-audio.d.ts +87 -0
- package/dist/esm/use-generate-audio.js +26 -0
- package/dist/esm/use-generate-audio.js.map +1 -0
- package/dist/esm/use-generate-image.d.ts +89 -0
- package/dist/esm/use-generate-image.js +26 -0
- package/dist/esm/use-generate-image.js.map +1 -0
- package/dist/esm/use-generate-speech.d.ts +83 -0
- package/dist/esm/use-generate-speech.js +26 -0
- package/dist/esm/use-generate-speech.js.map +1 -0
- package/dist/esm/use-generate-video.d.ts +94 -0
- package/dist/esm/use-generate-video.js +107 -0
- package/dist/esm/use-generate-video.js.map +1 -0
- package/dist/esm/use-generation.d.ts +80 -0
- package/dist/esm/use-generation.js +94 -0
- package/dist/esm/use-generation.js.map +1 -0
- package/dist/esm/use-mcp-app-bridge.d.ts +25 -0
- package/dist/esm/use-mcp-app-bridge.js +26 -0
- package/dist/esm/use-mcp-app-bridge.js.map +1 -0
- package/dist/esm/use-realtime-chat.d.ts +42 -0
- package/dist/esm/use-realtime-chat.js +189 -0
- package/dist/esm/use-realtime-chat.js.map +1 -0
- package/dist/esm/use-summarize.d.ts +86 -0
- package/dist/esm/use-summarize.js +26 -0
- package/dist/esm/use-summarize.js.map +1 -0
- package/dist/esm/use-transcription.d.ts +91 -0
- package/dist/esm/use-transcription.js +23 -0
- package/dist/esm/use-transcription.js.map +1 -0
- package/package.json +15 -4
- package/src/index.ts +11 -0
- package/src/mcp-apps.tsx +88 -0
- package/src/use-audio-recorder.ts +112 -0
- package/src/use-generate-audio.ts +5 -8
- package/src/use-generate-image.ts +5 -8
- package/src/use-generate-speech.ts +7 -7
- package/src/use-generate-video.ts +18 -9
- package/src/use-generation.ts +17 -6
- package/src/use-mcp-app-bridge.ts +60 -0
- package/src/use-summarize.ts +7 -8
- package/src/use-transcription.ts +9 -11
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { ChatClient } from "@tanstack/ai-client";
|
|
2
|
+
import { createChatDevtoolsBridge } from "@tanstack/ai-client/devtools";
|
|
3
|
+
import { useId, useState, useRef, useMemo, useEffect, useCallback } from "react";
|
|
4
|
+
function useChat(options) {
|
|
5
|
+
const hookId = useId();
|
|
6
|
+
const clientId = options.id || hookId;
|
|
7
|
+
const [messages, setMessages] = useState(
|
|
8
|
+
options.initialMessages || []
|
|
9
|
+
);
|
|
10
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
11
|
+
const [error, setError] = useState(void 0);
|
|
12
|
+
const [status, setStatus] = useState("ready");
|
|
13
|
+
const [isSubscribed, setIsSubscribed] = useState(false);
|
|
14
|
+
const [connectionStatus, setConnectionStatus] = useState("disconnected");
|
|
15
|
+
const [sessionGenerating, setSessionGenerating] = useState(false);
|
|
16
|
+
const messagesRef = useRef(
|
|
17
|
+
options.initialMessages || []
|
|
18
|
+
);
|
|
19
|
+
const isFirstMountRef = useRef(true);
|
|
20
|
+
const activeClientRef = useRef(null);
|
|
21
|
+
const cleanupInvalidationRef = useRef(
|
|
22
|
+
null
|
|
23
|
+
);
|
|
24
|
+
messagesRef.current = messages;
|
|
25
|
+
const optionsRef = useRef(options);
|
|
26
|
+
optionsRef.current = options;
|
|
27
|
+
const client = useMemo(() => {
|
|
28
|
+
const messagesToUse = options.initialMessages || [];
|
|
29
|
+
isFirstMountRef.current = false;
|
|
30
|
+
const initialOptions = optionsRef.current;
|
|
31
|
+
const transport = initialOptions.connection ? { connection: initialOptions.connection } : { fetcher: initialOptions.fetcher };
|
|
32
|
+
const instance = new ChatClient({
|
|
33
|
+
devtoolsBridgeFactory: createChatDevtoolsBridge,
|
|
34
|
+
...transport,
|
|
35
|
+
id: clientId,
|
|
36
|
+
initialMessages: messagesToUse,
|
|
37
|
+
...initialOptions.body !== void 0 && { body: initialOptions.body },
|
|
38
|
+
...initialOptions.threadId !== void 0 && {
|
|
39
|
+
threadId: initialOptions.threadId
|
|
40
|
+
},
|
|
41
|
+
...initialOptions.forwardedProps !== void 0 && {
|
|
42
|
+
forwardedProps: initialOptions.forwardedProps
|
|
43
|
+
},
|
|
44
|
+
...initialOptions.persistence !== void 0 && {
|
|
45
|
+
persistence: initialOptions.persistence
|
|
46
|
+
},
|
|
47
|
+
...initialOptions.context !== void 0 && {
|
|
48
|
+
context: initialOptions.context
|
|
49
|
+
},
|
|
50
|
+
devtools: {
|
|
51
|
+
...initialOptions.devtools,
|
|
52
|
+
framework: "react",
|
|
53
|
+
hookName: "useChat",
|
|
54
|
+
outputKind: initialOptions.outputSchema ? "structured" : "chat"
|
|
55
|
+
},
|
|
56
|
+
onResponse: (response) => {
|
|
57
|
+
if (activeClientRef.current !== instance) return;
|
|
58
|
+
void optionsRef.current.onResponse?.(response);
|
|
59
|
+
},
|
|
60
|
+
onChunk: (chunk) => {
|
|
61
|
+
if (activeClientRef.current !== instance) return;
|
|
62
|
+
optionsRef.current.onChunk?.(chunk);
|
|
63
|
+
},
|
|
64
|
+
onFinish: (message) => {
|
|
65
|
+
if (activeClientRef.current !== instance) return;
|
|
66
|
+
optionsRef.current.onFinish?.(message);
|
|
67
|
+
},
|
|
68
|
+
onError: (error2) => {
|
|
69
|
+
if (activeClientRef.current !== instance) return;
|
|
70
|
+
optionsRef.current.onError?.(error2);
|
|
71
|
+
},
|
|
72
|
+
...initialOptions.tools !== void 0 && {
|
|
73
|
+
tools: initialOptions.tools
|
|
74
|
+
},
|
|
75
|
+
onCustomEvent: (eventType, data, context) => {
|
|
76
|
+
if (activeClientRef.current !== instance) return;
|
|
77
|
+
optionsRef.current.onCustomEvent?.(eventType, data, context);
|
|
78
|
+
},
|
|
79
|
+
...options.streamProcessor !== void 0 && {
|
|
80
|
+
streamProcessor: options.streamProcessor
|
|
81
|
+
},
|
|
82
|
+
onMessagesChange: (newMessages) => {
|
|
83
|
+
if (activeClientRef.current !== instance) return;
|
|
84
|
+
setMessages(newMessages);
|
|
85
|
+
},
|
|
86
|
+
onLoadingChange: (newIsLoading) => {
|
|
87
|
+
if (activeClientRef.current !== instance) return;
|
|
88
|
+
setIsLoading(newIsLoading);
|
|
89
|
+
},
|
|
90
|
+
onErrorChange: (newError) => {
|
|
91
|
+
if (activeClientRef.current !== instance) return;
|
|
92
|
+
setError(newError);
|
|
93
|
+
},
|
|
94
|
+
onStatusChange: (status2) => {
|
|
95
|
+
if (activeClientRef.current !== instance) return;
|
|
96
|
+
setStatus(status2);
|
|
97
|
+
},
|
|
98
|
+
onSubscriptionChange: (nextIsSubscribed) => {
|
|
99
|
+
if (activeClientRef.current !== instance) return;
|
|
100
|
+
setIsSubscribed(nextIsSubscribed);
|
|
101
|
+
},
|
|
102
|
+
onConnectionStatusChange: (nextStatus) => {
|
|
103
|
+
if (activeClientRef.current !== instance) return;
|
|
104
|
+
setConnectionStatus(nextStatus);
|
|
105
|
+
},
|
|
106
|
+
onSessionGeneratingChange: (isGenerating) => {
|
|
107
|
+
if (activeClientRef.current !== instance) return;
|
|
108
|
+
setSessionGenerating(isGenerating);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
activeClientRef.current = instance;
|
|
112
|
+
return instance;
|
|
113
|
+
}, [clientId]);
|
|
114
|
+
useEffect(() => {
|
|
115
|
+
const clientMessages = client.getMessages();
|
|
116
|
+
if (clientMessages !== messagesRef.current) {
|
|
117
|
+
setMessages(clientMessages);
|
|
118
|
+
}
|
|
119
|
+
}, [client]);
|
|
120
|
+
useEffect(() => {
|
|
121
|
+
client.updateOptions({ body: options.body });
|
|
122
|
+
}, [client, options.body]);
|
|
123
|
+
useEffect(() => {
|
|
124
|
+
if (options.forwardedProps !== void 0) {
|
|
125
|
+
client.updateOptions({ forwardedProps: options.forwardedProps });
|
|
126
|
+
}
|
|
127
|
+
}, [client, options.forwardedProps]);
|
|
128
|
+
useEffect(() => {
|
|
129
|
+
if (options.tools !== void 0) {
|
|
130
|
+
client.updateOptions({ tools: options.tools });
|
|
131
|
+
}
|
|
132
|
+
}, [client, options.tools]);
|
|
133
|
+
useEffect(() => {
|
|
134
|
+
client.updateOptions({ context: options.context });
|
|
135
|
+
}, [client, options.context]);
|
|
136
|
+
useEffect(() => {
|
|
137
|
+
if (options.live) {
|
|
138
|
+
client.subscribe();
|
|
139
|
+
} else {
|
|
140
|
+
client.unsubscribe();
|
|
141
|
+
}
|
|
142
|
+
}, [client, options.live]);
|
|
143
|
+
useEffect(() => {
|
|
144
|
+
if (cleanupInvalidationRef.current) {
|
|
145
|
+
clearTimeout(cleanupInvalidationRef.current);
|
|
146
|
+
cleanupInvalidationRef.current = null;
|
|
147
|
+
}
|
|
148
|
+
activeClientRef.current = client;
|
|
149
|
+
client.mountDevtools();
|
|
150
|
+
return () => {
|
|
151
|
+
cleanupInvalidationRef.current = setTimeout(() => {
|
|
152
|
+
if (activeClientRef.current === client) {
|
|
153
|
+
activeClientRef.current = null;
|
|
154
|
+
}
|
|
155
|
+
cleanupInvalidationRef.current = null;
|
|
156
|
+
}, 0);
|
|
157
|
+
if (optionsRef.current.live) {
|
|
158
|
+
client.unsubscribe();
|
|
159
|
+
} else {
|
|
160
|
+
client.stop();
|
|
161
|
+
}
|
|
162
|
+
client.dispose();
|
|
163
|
+
};
|
|
164
|
+
}, [client]);
|
|
165
|
+
const sendMessage = useCallback(
|
|
166
|
+
async (content) => {
|
|
167
|
+
await client.sendMessage(content);
|
|
168
|
+
},
|
|
169
|
+
[client]
|
|
170
|
+
);
|
|
171
|
+
const append = useCallback(
|
|
172
|
+
async (message) => {
|
|
173
|
+
await client.append(message);
|
|
174
|
+
},
|
|
175
|
+
[client]
|
|
176
|
+
);
|
|
177
|
+
const reload = useCallback(async () => {
|
|
178
|
+
await client.reload();
|
|
179
|
+
}, [client]);
|
|
180
|
+
const stop = useCallback(() => {
|
|
181
|
+
client.stop();
|
|
182
|
+
}, [client]);
|
|
183
|
+
const clear = useCallback(() => {
|
|
184
|
+
client.clear();
|
|
185
|
+
}, [client]);
|
|
186
|
+
const setMessagesManually = useCallback(
|
|
187
|
+
(newMessages) => {
|
|
188
|
+
client.setMessagesManually(newMessages);
|
|
189
|
+
},
|
|
190
|
+
[client]
|
|
191
|
+
);
|
|
192
|
+
const addToolResult = useCallback(
|
|
193
|
+
async (result) => {
|
|
194
|
+
await client.addToolResult(result);
|
|
195
|
+
},
|
|
196
|
+
[client]
|
|
197
|
+
);
|
|
198
|
+
const addToolApprovalResponse = useCallback(
|
|
199
|
+
async (response) => {
|
|
200
|
+
await client.addToolApprovalResponse(response);
|
|
201
|
+
},
|
|
202
|
+
[client]
|
|
203
|
+
);
|
|
204
|
+
const renderedMessages = client.getMessages();
|
|
205
|
+
const activeStructuredPart = useMemo(() => {
|
|
206
|
+
let lastUserIndex = -1;
|
|
207
|
+
for (let i = renderedMessages.length - 1; i >= 0; i--) {
|
|
208
|
+
if (renderedMessages[i]?.role === "user") {
|
|
209
|
+
lastUserIndex = i;
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (lastUserIndex === -1) return null;
|
|
214
|
+
for (let i = renderedMessages.length - 1; i > lastUserIndex; i--) {
|
|
215
|
+
const m = renderedMessages[i];
|
|
216
|
+
if (m?.role !== "assistant") continue;
|
|
217
|
+
const part = m.parts.find(
|
|
218
|
+
(p) => p.type === "structured-output"
|
|
219
|
+
);
|
|
220
|
+
if (part) return part;
|
|
221
|
+
}
|
|
222
|
+
return null;
|
|
223
|
+
}, [renderedMessages]);
|
|
224
|
+
const partial = useMemo(() => {
|
|
225
|
+
if (!activeStructuredPart) return {};
|
|
226
|
+
const v = activeStructuredPart.partial ?? activeStructuredPart.data;
|
|
227
|
+
return v ?? {};
|
|
228
|
+
}, [activeStructuredPart]);
|
|
229
|
+
const final = useMemo(() => {
|
|
230
|
+
if (!activeStructuredPart || activeStructuredPart.status !== "complete") {
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
return activeStructuredPart.data;
|
|
234
|
+
}, [activeStructuredPart]);
|
|
235
|
+
return {
|
|
236
|
+
messages: renderedMessages,
|
|
237
|
+
sendMessage,
|
|
238
|
+
append,
|
|
239
|
+
reload,
|
|
240
|
+
stop,
|
|
241
|
+
isLoading,
|
|
242
|
+
error,
|
|
243
|
+
status,
|
|
244
|
+
isSubscribed,
|
|
245
|
+
connectionStatus,
|
|
246
|
+
sessionGenerating,
|
|
247
|
+
setMessages: setMessagesManually,
|
|
248
|
+
clear,
|
|
249
|
+
addToolResult,
|
|
250
|
+
addToolApprovalResponse,
|
|
251
|
+
partial,
|
|
252
|
+
final
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
export {
|
|
256
|
+
useChat
|
|
257
|
+
};
|
|
258
|
+
//# sourceMappingURL=use-chat.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-chat.js","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 SchemaInput,\n StreamChunk,\n} from '@tanstack/ai/client'\nimport type {\n ChatClientState,\n ConnectionStatus,\n InferredClientContext,\n StructuredOutputPart,\n} from '@tanstack/ai-client'\n\nimport type {\n DeepPartial,\n MultimodalContent,\n UIMessage,\n UseChatOptions,\n UseChatReturn,\n} from './types'\n\nexport function useChat<\n 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 const hookId = useId()\n const clientId = options.id || 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\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 activeClientRef = useRef<ChatClient | null>(null)\n const cleanupInvalidationRef = useRef<ReturnType<typeof setTimeout> | null>(\n null,\n )\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 // 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 instance = new ChatClient<TTools, TContext>({\n devtoolsBridgeFactory: createChatDevtoolsBridge,\n ...transport,\n id: clientId,\n initialMessages: messagesToUse,\n ...(initialOptions.body !== undefined && { body: initialOptions.body }),\n ...(initialOptions.threadId !== undefined && {\n threadId: initialOptions.threadId,\n }),\n ...(initialOptions.forwardedProps !== undefined && {\n forwardedProps: initialOptions.forwardedProps,\n }),\n ...(initialOptions.persistence !== undefined && {\n persistence: initialOptions.persistence,\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 (activeClientRef.current !== instance) return\n void optionsRef.current.onResponse?.(response)\n },\n onChunk: (chunk: StreamChunk) => {\n if (activeClientRef.current !== instance) return\n optionsRef.current.onChunk?.(chunk)\n },\n onFinish: (message: UIMessage<TTools>) => {\n if (activeClientRef.current !== instance) return\n optionsRef.current.onFinish?.(message)\n },\n onError: (error: Error) => {\n if (activeClientRef.current !== instance) return\n optionsRef.current.onError?.(error)\n },\n ...(initialOptions.tools !== undefined && {\n tools: initialOptions.tools,\n }),\n onCustomEvent: (eventType, data, context) => {\n if (activeClientRef.current !== instance) 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 (activeClientRef.current !== instance) return\n setMessages(newMessages)\n },\n onLoadingChange: (newIsLoading: boolean) => {\n if (activeClientRef.current !== instance) return\n setIsLoading(newIsLoading)\n },\n onErrorChange: (newError: Error | undefined) => {\n if (activeClientRef.current !== instance) return\n setError(newError)\n },\n onStatusChange: (status: ChatClientState) => {\n if (activeClientRef.current !== instance) return\n setStatus(status)\n },\n onSubscriptionChange: (nextIsSubscribed: boolean) => {\n if (activeClientRef.current !== instance) return\n setIsSubscribed(nextIsSubscribed)\n },\n onConnectionStatusChange: (nextStatus: ConnectionStatus) => {\n if (activeClientRef.current !== instance) return\n setConnectionStatus(nextStatus)\n },\n onSessionGeneratingChange: (isGenerating: boolean) => {\n if (activeClientRef.current !== instance) return\n setSessionGenerating(isGenerating)\n },\n })\n activeClientRef.current = instance\n return instance\n }, [clientId])\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.live) {\n client.subscribe()\n } else {\n client.unsubscribe()\n }\n }, [client, options.live])\n\n useEffect(() => {\n if (cleanupInvalidationRef.current) {\n clearTimeout(cleanupInvalidationRef.current)\n cleanupInvalidationRef.current = null\n }\n activeClientRef.current = client\n client.mountDevtools()\n\n return () => {\n cleanupInvalidationRef.current = setTimeout(() => {\n if (activeClientRef.current === client) {\n activeClientRef.current = null\n }\n cleanupInvalidationRef.current = null\n }, 0)\n // Subscribe/unsubscribe on `options.live` is owned by the dedicated\n // effect above. This cleanup only fires on unmount or client swap,\n // so read `live` through the ref to avoid disposing the client every\n // time `live` toggles.\n if (optionsRef.current.live) {\n client.unsubscribe()\n } else {\n client.stop()\n }\n client.dispose()\n }\n }, [client])\n\n const sendMessage = useCallback(\n async (content: string | MultimodalContent) => {\n await client.sendMessage(content)\n },\n [client],\n )\n\n const append = useCallback(\n async (message: ModelMessage | UIMessage) => {\n await client.append(message)\n },\n [client],\n )\n\n const reload = useCallback(async () => {\n await client.reload()\n }, [client])\n\n const stop = useCallback(() => {\n client.stop()\n }, [client])\n\n const clear = useCallback(() => {\n client.clear()\n }, [client])\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 },\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 // eslint-disable-next-line 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 partial,\n final,\n } as unknown as UseChatReturn<TTools, TSchema>\n}\n"],"names":["error","status"],"mappings":";;;AAyBO,SAAS,QAKd,SACgC;AAChC,QAAM,SAAS,MAAA;AACf,QAAM,WAAW,QAAQ,MAAM;AAE/B,QAAM,CAAC,UAAU,WAAW,IAAI;AAAA,IAC9B,QAAQ,mBAAmB,CAAA;AAAA,EAAC;AAE9B,QAAM,CAAC,WAAW,YAAY,IAAI,SAAS,KAAK;AAChD,QAAM,CAAC,OAAO,QAAQ,IAAI,SAA4B,MAAS;AAC/D,QAAM,CAAC,QAAQ,SAAS,IAAI,SAA0B,OAAO;AAC7D,QAAM,CAAC,cAAc,eAAe,IAAI,SAAS,KAAK;AACtD,QAAM,CAAC,kBAAkB,mBAAmB,IAC1C,SAA2B,cAAc;AAC3C,QAAM,CAAC,mBAAmB,oBAAoB,IAAI,SAAS,KAAK;AAMhE,QAAM,cAAc;AAAA,IAClB,QAAQ,mBAAmB,CAAA;AAAA,EAAC;AAE9B,QAAM,kBAAkB,OAAO,IAAI;AACnC,QAAM,kBAAkB,OAA0B,IAAI;AACtD,QAAM,yBAAyB;AAAA,IAC7B;AAAA,EAAA;AAIF,cAAY,UAAU;AAGtB,QAAM,aAAa,OAAkD,OAAO;AAC5E,aAAW,UAAU;AAGrB,QAAM,SAAS,QAAQ,MAAM;AAC3B,UAAM,gBAAgB,QAAQ,mBAAmB,CAAA;AACjD,oBAAgB,UAAU;AAM1B,UAAM,iBAAiB,WAAW;AAClC,UAAM,YAAY,eAAe,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iB,MAAM;AACrC,QAAI,CAAC,qBAAsB,QAAO,CAAA;AAClC,UAAM,IAAI,qBAAqB,WAAW,qBAAqB;AAC/D,WAAQ,KAAK,CAAA;AAAA,EACf,GAAG,CAAC,oBAAoB,CAAC;AAEzB,QAAM,QAAQ,QAAsB,MAAM;AACxC,QAAI,CAAC,wBAAwB,qBAAqB,WAAW,YAAY;AACvE,aAAO;AAAA,IACT;AACA,WAAO,qBAAqB;AAAA,EAC9B,GAAG,CAAC,oBAAoB,CAAC;AAMzB,SAAO;AAAA,IACL,UAAU;AAAA,IACV;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,aAAa;AAAA,IACb;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEJ;"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { AudioGenerationResult, StreamChunk } from '@tanstack/ai';
|
|
2
|
+
import { AIDevtoolsDisplayOptions, AudioGenerateInput, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher, InferGenerationOutputFromReturn } from '@tanstack/ai-client';
|
|
3
|
+
/**
|
|
4
|
+
* Options for the useGenerateAudio hook.
|
|
5
|
+
*
|
|
6
|
+
* @template TOutput - The output type after optional transform (defaults to AudioGenerationResult)
|
|
7
|
+
*/
|
|
8
|
+
export interface UseGenerateAudioOptions<TOutput = AudioGenerationResult> {
|
|
9
|
+
/** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
10
|
+
connection?: ConnectConnectionAdapter;
|
|
11
|
+
/** Direct async function for audio generation */
|
|
12
|
+
fetcher?: GenerationFetcher<AudioGenerateInput, AudioGenerationResult>;
|
|
13
|
+
/** Unique identifier for this generation instance */
|
|
14
|
+
id?: string;
|
|
15
|
+
/** Additional body parameters to send with connect-based adapter requests */
|
|
16
|
+
body?: Record<string, any>;
|
|
17
|
+
/** Display options for TanStack AI Devtools. */
|
|
18
|
+
devtools?: AIDevtoolsDisplayOptions;
|
|
19
|
+
/**
|
|
20
|
+
* Callback when audio is generated. Can optionally return a transformed value.
|
|
21
|
+
*
|
|
22
|
+
* - Return a non-null value to transform and store it as the result
|
|
23
|
+
* - Return `null` to keep the previous result unchanged
|
|
24
|
+
* - Return nothing (`void`) to store the raw result as-is
|
|
25
|
+
*/
|
|
26
|
+
onResult?: (result: AudioGenerationResult) => TOutput | null | void;
|
|
27
|
+
/** Callback when an error occurs */
|
|
28
|
+
onError?: (error: Error) => void;
|
|
29
|
+
/** Callback when progress is reported (0-100) */
|
|
30
|
+
onProgress?: (progress: number, message?: string) => void;
|
|
31
|
+
/** Callback for each stream chunk (connect-based adapter mode only) */
|
|
32
|
+
onChunk?: (chunk: StreamChunk) => void;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Return type for the useGenerateAudio hook.
|
|
36
|
+
*
|
|
37
|
+
* @template TOutput - The output type (after optional transform)
|
|
38
|
+
*/
|
|
39
|
+
export interface UseGenerateAudioReturn<TOutput = AudioGenerationResult> {
|
|
40
|
+
/** Trigger audio generation */
|
|
41
|
+
generate: (input: AudioGenerateInput) => Promise<void>;
|
|
42
|
+
/** The generation result containing audio, or null */
|
|
43
|
+
result: TOutput | null;
|
|
44
|
+
/** Whether generation is in progress */
|
|
45
|
+
isLoading: boolean;
|
|
46
|
+
/** Current error, if any */
|
|
47
|
+
error: Error | undefined;
|
|
48
|
+
/** Current state of the generation */
|
|
49
|
+
status: GenerationClientState;
|
|
50
|
+
/** Abort the current generation */
|
|
51
|
+
stop: () => void;
|
|
52
|
+
/** Clear result, error, and return to idle */
|
|
53
|
+
reset: () => void;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* React hook for generating audio (music, sound effects) using AI models.
|
|
57
|
+
*
|
|
58
|
+
* Supports two transport modes:
|
|
59
|
+
* - **ConnectConnectionAdapter** — Streaming transport (SSE, HTTP stream, custom)
|
|
60
|
+
* - **Fetcher** — Direct async function call
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```tsx
|
|
64
|
+
* import { useGenerateAudio } from '@tanstack/ai-react'
|
|
65
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
66
|
+
*
|
|
67
|
+
* function AudioGenerator() {
|
|
68
|
+
* const { generate, result, isLoading, error, reset } = useGenerateAudio({
|
|
69
|
+
* connection: fetchServerSentEvents('/api/generate/audio'),
|
|
70
|
+
* })
|
|
71
|
+
*
|
|
72
|
+
* return (
|
|
73
|
+
* <div>
|
|
74
|
+
* <button onClick={() => generate({ prompt: 'An upbeat electronic track', duration: 10 })}>
|
|
75
|
+
* Generate
|
|
76
|
+
* </button>
|
|
77
|
+
* {isLoading && <p>Generating...</p>}
|
|
78
|
+
* {error && <p>Error: {error.message}</p>}
|
|
79
|
+
* {result?.audio.url && <audio src={result.audio.url} controls />}
|
|
80
|
+
* </div>
|
|
81
|
+
* )
|
|
82
|
+
* }
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export declare function useGenerateAudio<TTransformed = void>(options: Omit<UseGenerateAudioOptions, 'onResult'> & {
|
|
86
|
+
onResult?: (result: AudioGenerationResult) => TTransformed;
|
|
87
|
+
}): UseGenerateAudioReturn<InferGenerationOutputFromReturn<AudioGenerationResult, TTransformed>>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useGeneration } from "./use-generation.js";
|
|
2
|
+
function useGenerateAudio(options) {
|
|
3
|
+
const devtools = {
|
|
4
|
+
...options.devtools,
|
|
5
|
+
framework: "react",
|
|
6
|
+
hookName: "useGenerateAudio",
|
|
7
|
+
outputKind: "audio"
|
|
8
|
+
};
|
|
9
|
+
const { generate, result, isLoading, error, status, stop, reset } = useGeneration({
|
|
10
|
+
...options,
|
|
11
|
+
devtools
|
|
12
|
+
});
|
|
13
|
+
return {
|
|
14
|
+
generate,
|
|
15
|
+
result,
|
|
16
|
+
isLoading,
|
|
17
|
+
error,
|
|
18
|
+
status,
|
|
19
|
+
stop,
|
|
20
|
+
reset
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export {
|
|
24
|
+
useGenerateAudio
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=use-generate-audio.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-generate-audio.js","sources":["../../src/use-generate-audio.ts"],"sourcesContent":["import { useGeneration } from './use-generation'\nimport type { AudioGenerationResult, StreamChunk } from '@tanstack/ai'\nimport type {\n AIDevtoolsDisplayOptions,\n AudioGenerateInput,\n ConnectConnectionAdapter,\n GenerationClientState,\n GenerationFetcher,\n InferGenerationOutputFromReturn,\n} from '@tanstack/ai-client'\n\n/**\n * Options for the useGenerateAudio hook.\n *\n * @template TOutput - The output type after optional transform (defaults to AudioGenerationResult)\n */\nexport interface UseGenerateAudioOptions<TOutput = AudioGenerationResult> {\n /** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */\n connection?: ConnectConnectionAdapter\n /** Direct async function for audio generation */\n fetcher?: GenerationFetcher<AudioGenerateInput, AudioGenerationResult>\n /** Unique identifier for this generation instance */\n id?: string\n /** Additional body parameters to send with connect-based adapter requests */\n body?: Record<string, any>\n /** Display options for TanStack AI Devtools. */\n devtools?: AIDevtoolsDisplayOptions\n /**\n * Callback when audio is generated. Can optionally return a transformed value.\n *\n * - Return a non-null value to transform and store it as the result\n * - Return `null` to keep the previous result unchanged\n * - Return nothing (`void`) to store the raw result as-is\n */\n onResult?: (result: AudioGenerationResult) => TOutput | null | void\n /** Callback when an error occurs */\n onError?: (error: Error) => void\n /** Callback when progress is reported (0-100) */\n onProgress?: (progress: number, message?: string) => void\n /** Callback for each stream chunk (connect-based adapter mode only) */\n onChunk?: (chunk: StreamChunk) => void\n}\n\n/**\n * Return type for the useGenerateAudio hook.\n *\n * @template TOutput - The output type (after optional transform)\n */\nexport interface UseGenerateAudioReturn<TOutput = AudioGenerationResult> {\n /** Trigger audio generation */\n generate: (input: AudioGenerateInput) => Promise<void>\n /** The generation result containing audio, or null */\n result: TOutput | null\n /** Whether generation is in progress */\n isLoading: boolean\n /** Current error, if any */\n error: Error | undefined\n /** Current state of the generation */\n status: GenerationClientState\n /** Abort the current generation */\n stop: () => void\n /** Clear result, error, and return to idle */\n reset: () => void\n}\n\n/**\n * React hook for generating audio (music, sound effects) using AI models.\n *\n * Supports two transport modes:\n * - **ConnectConnectionAdapter** — Streaming transport (SSE, HTTP stream, custom)\n * - **Fetcher** — Direct async function call\n *\n * @example\n * ```tsx\n * import { useGenerateAudio } from '@tanstack/ai-react'\n * import { fetchServerSentEvents } from '@tanstack/ai-client'\n *\n * function AudioGenerator() {\n * const { generate, result, isLoading, error, reset } = useGenerateAudio({\n * connection: fetchServerSentEvents('/api/generate/audio'),\n * })\n *\n * return (\n * <div>\n * <button onClick={() => generate({ prompt: 'An upbeat electronic track', duration: 10 })}>\n * Generate\n * </button>\n * {isLoading && <p>Generating...</p>}\n * {error && <p>Error: {error.message}</p>}\n * {result?.audio.url && <audio src={result.audio.url} controls />}\n * </div>\n * )\n * }\n * ```\n */\nexport function useGenerateAudio<TTransformed = void>(\n options: Omit<UseGenerateAudioOptions, 'onResult'> & {\n onResult?: (result: AudioGenerationResult) => TTransformed\n },\n): UseGenerateAudioReturn<\n InferGenerationOutputFromReturn<AudioGenerationResult, TTransformed>\n> {\n const devtools = {\n ...options.devtools,\n framework: 'react',\n hookName: 'useGenerateAudio',\n outputKind: 'audio' as const,\n }\n const { generate, result, isLoading, error, status, stop, reset } =\n useGeneration<AudioGenerateInput, AudioGenerationResult, TTransformed>({\n ...options,\n devtools,\n })\n\n return {\n generate: generate as (input: AudioGenerateInput) => Promise<void>,\n result,\n isLoading,\n error,\n status,\n stop,\n reset,\n }\n}\n"],"names":[],"mappings":";AA+FO,SAAS,iBACd,SAKA;AACA,QAAM,WAAW;AAAA,IACf,GAAG,QAAQ;AAAA,IACX,WAAW;AAAA,IACX,UAAU;AAAA,IACV,YAAY;AAAA,EAAA;AAEd,QAAM,EAAE,UAAU,QAAQ,WAAW,OAAO,QAAQ,MAAM,MAAA,IACxD,cAAuE;AAAA,IACrE,GAAG;AAAA,IACH;AAAA,EAAA,CACD;AAEH,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEJ;"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { ImageGenerationResult, StreamChunk } from '@tanstack/ai';
|
|
2
|
+
import { AIDevtoolsDisplayOptions, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher, ImageGenerateInput, InferGenerationOutputFromReturn } from '@tanstack/ai-client';
|
|
3
|
+
/**
|
|
4
|
+
* Options for the useGenerateImage hook.
|
|
5
|
+
*
|
|
6
|
+
* @template TOutput - The output type after optional transform (defaults to ImageGenerationResult)
|
|
7
|
+
*/
|
|
8
|
+
export interface UseGenerateImageOptions<TOutput = ImageGenerationResult> {
|
|
9
|
+
/** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
10
|
+
connection?: ConnectConnectionAdapter;
|
|
11
|
+
/** Direct async function for image generation */
|
|
12
|
+
fetcher?: GenerationFetcher<ImageGenerateInput, ImageGenerationResult>;
|
|
13
|
+
/** Unique identifier for this generation instance */
|
|
14
|
+
id?: string;
|
|
15
|
+
/** Additional body parameters to send with connect-based adapter requests */
|
|
16
|
+
body?: Record<string, any>;
|
|
17
|
+
/** Display options for TanStack AI Devtools. */
|
|
18
|
+
devtools?: AIDevtoolsDisplayOptions;
|
|
19
|
+
/**
|
|
20
|
+
* Callback when images are generated. Can optionally return a transformed value.
|
|
21
|
+
*
|
|
22
|
+
* - Return a non-null value to transform and store it as the result
|
|
23
|
+
* - Return `null` to keep the previous result unchanged
|
|
24
|
+
* - Return nothing (`void`) to store the raw result as-is
|
|
25
|
+
*/
|
|
26
|
+
onResult?: (result: ImageGenerationResult) => TOutput | null | void;
|
|
27
|
+
/** Callback when an error occurs */
|
|
28
|
+
onError?: (error: Error) => void;
|
|
29
|
+
/** Callback when progress is reported (0-100) */
|
|
30
|
+
onProgress?: (progress: number, message?: string) => void;
|
|
31
|
+
/** Callback for each stream chunk (connect-based adapter mode only) */
|
|
32
|
+
onChunk?: (chunk: StreamChunk) => void;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Return type for the useGenerateImage hook.
|
|
36
|
+
*
|
|
37
|
+
* @template TOutput - The output type (after optional transform)
|
|
38
|
+
*/
|
|
39
|
+
export interface UseGenerateImageReturn<TOutput = ImageGenerationResult> {
|
|
40
|
+
/** Trigger image generation */
|
|
41
|
+
generate: (input: ImageGenerateInput) => Promise<void>;
|
|
42
|
+
/** The generation result containing images, or null */
|
|
43
|
+
result: TOutput | null;
|
|
44
|
+
/** Whether generation is in progress */
|
|
45
|
+
isLoading: boolean;
|
|
46
|
+
/** Current error, if any */
|
|
47
|
+
error: Error | undefined;
|
|
48
|
+
/** Current state of the generation */
|
|
49
|
+
status: GenerationClientState;
|
|
50
|
+
/** Abort the current generation */
|
|
51
|
+
stop: () => void;
|
|
52
|
+
/** Clear result, error, and return to idle */
|
|
53
|
+
reset: () => void;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* React hook for generating images using AI models.
|
|
57
|
+
*
|
|
58
|
+
* Supports two transport modes:
|
|
59
|
+
* - **ConnectConnectionAdapter** — Streaming transport (SSE, HTTP stream, custom)
|
|
60
|
+
* - **Fetcher** — Direct async function call
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```tsx
|
|
64
|
+
* import { useGenerateImage } from '@tanstack/ai-react'
|
|
65
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
66
|
+
*
|
|
67
|
+
* function ImageGenerator() {
|
|
68
|
+
* const { generate, result, isLoading, error, reset } = useGenerateImage({
|
|
69
|
+
* connection: fetchServerSentEvents('/api/generate/image'),
|
|
70
|
+
* })
|
|
71
|
+
*
|
|
72
|
+
* return (
|
|
73
|
+
* <div>
|
|
74
|
+
* <button onClick={() => generate({ prompt: 'A sunset over mountains' })}>
|
|
75
|
+
* Generate
|
|
76
|
+
* </button>
|
|
77
|
+
* {isLoading && <p>Generating...</p>}
|
|
78
|
+
* {error && <p>Error: {error.message}</p>}
|
|
79
|
+
* {result?.images.map((img, i) => (
|
|
80
|
+
* <img key={i} src={img.url || `data:image/png;base64,${img.b64Json}`} />
|
|
81
|
+
* ))}
|
|
82
|
+
* </div>
|
|
83
|
+
* )
|
|
84
|
+
* }
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export declare function useGenerateImage<TTransformed = void>(options: Omit<UseGenerateImageOptions, 'onResult'> & {
|
|
88
|
+
onResult?: (result: ImageGenerationResult) => TTransformed;
|
|
89
|
+
}): UseGenerateImageReturn<InferGenerationOutputFromReturn<ImageGenerationResult, TTransformed>>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useGeneration } from "./use-generation.js";
|
|
2
|
+
function useGenerateImage(options) {
|
|
3
|
+
const devtools = {
|
|
4
|
+
...options.devtools,
|
|
5
|
+
framework: "react",
|
|
6
|
+
hookName: "useGenerateImage",
|
|
7
|
+
outputKind: "image"
|
|
8
|
+
};
|
|
9
|
+
const { generate, result, isLoading, error, status, stop, reset } = useGeneration({
|
|
10
|
+
...options,
|
|
11
|
+
devtools
|
|
12
|
+
});
|
|
13
|
+
return {
|
|
14
|
+
generate,
|
|
15
|
+
result,
|
|
16
|
+
isLoading,
|
|
17
|
+
error,
|
|
18
|
+
status,
|
|
19
|
+
stop,
|
|
20
|
+
reset
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export {
|
|
24
|
+
useGenerateImage
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=use-generate-image.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-generate-image.js","sources":["../../src/use-generate-image.ts"],"sourcesContent":["import { useGeneration } from './use-generation'\nimport type { ImageGenerationResult, StreamChunk } from '@tanstack/ai'\nimport type {\n AIDevtoolsDisplayOptions,\n ConnectConnectionAdapter,\n GenerationClientState,\n GenerationFetcher,\n ImageGenerateInput,\n InferGenerationOutputFromReturn,\n} from '@tanstack/ai-client'\n\n/**\n * Options for the useGenerateImage hook.\n *\n * @template TOutput - The output type after optional transform (defaults to ImageGenerationResult)\n */\nexport interface UseGenerateImageOptions<TOutput = ImageGenerationResult> {\n /** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */\n connection?: ConnectConnectionAdapter\n /** Direct async function for image generation */\n fetcher?: GenerationFetcher<ImageGenerateInput, ImageGenerationResult>\n /** Unique identifier for this generation instance */\n id?: string\n /** Additional body parameters to send with connect-based adapter requests */\n body?: Record<string, any>\n /** Display options for TanStack AI Devtools. */\n devtools?: AIDevtoolsDisplayOptions\n /**\n * Callback when images are generated. Can optionally return a transformed value.\n *\n * - Return a non-null value to transform and store it as the result\n * - Return `null` to keep the previous result unchanged\n * - Return nothing (`void`) to store the raw result as-is\n */\n onResult?: (result: ImageGenerationResult) => TOutput | null | void\n /** Callback when an error occurs */\n onError?: (error: Error) => void\n /** Callback when progress is reported (0-100) */\n onProgress?: (progress: number, message?: string) => void\n /** Callback for each stream chunk (connect-based adapter mode only) */\n onChunk?: (chunk: StreamChunk) => void\n}\n\n/**\n * Return type for the useGenerateImage hook.\n *\n * @template TOutput - The output type (after optional transform)\n */\nexport interface UseGenerateImageReturn<TOutput = ImageGenerationResult> {\n /** Trigger image generation */\n generate: (input: ImageGenerateInput) => Promise<void>\n /** The generation result containing images, or null */\n result: TOutput | null\n /** Whether generation is in progress */\n isLoading: boolean\n /** Current error, if any */\n error: Error | undefined\n /** Current state of the generation */\n status: GenerationClientState\n /** Abort the current generation */\n stop: () => void\n /** Clear result, error, and return to idle */\n reset: () => void\n}\n\n/**\n * React hook for generating images using AI models.\n *\n * Supports two transport modes:\n * - **ConnectConnectionAdapter** — Streaming transport (SSE, HTTP stream, custom)\n * - **Fetcher** — Direct async function call\n *\n * @example\n * ```tsx\n * import { useGenerateImage } from '@tanstack/ai-react'\n * import { fetchServerSentEvents } from '@tanstack/ai-client'\n *\n * function ImageGenerator() {\n * const { generate, result, isLoading, error, reset } = useGenerateImage({\n * connection: fetchServerSentEvents('/api/generate/image'),\n * })\n *\n * return (\n * <div>\n * <button onClick={() => generate({ prompt: 'A sunset over mountains' })}>\n * Generate\n * </button>\n * {isLoading && <p>Generating...</p>}\n * {error && <p>Error: {error.message}</p>}\n * {result?.images.map((img, i) => (\n * <img key={i} src={img.url || `data:image/png;base64,${img.b64Json}`} />\n * ))}\n * </div>\n * )\n * }\n * ```\n */\nexport function useGenerateImage<TTransformed = void>(\n options: Omit<UseGenerateImageOptions, 'onResult'> & {\n onResult?: (result: ImageGenerationResult) => TTransformed\n },\n): UseGenerateImageReturn<\n InferGenerationOutputFromReturn<ImageGenerationResult, TTransformed>\n> {\n const devtools = {\n ...options.devtools,\n framework: 'react',\n hookName: 'useGenerateImage',\n outputKind: 'image' as const,\n }\n const { generate, result, isLoading, error, status, stop, reset } =\n useGeneration<ImageGenerateInput, ImageGenerationResult, TTransformed>({\n ...options,\n devtools,\n })\n\n return {\n generate: generate as (input: ImageGenerateInput) => Promise<void>,\n result,\n isLoading,\n error,\n status,\n stop,\n reset,\n }\n}\n"],"names":[],"mappings":";AAiGO,SAAS,iBACd,SAKA;AACA,QAAM,WAAW;AAAA,IACf,GAAG,QAAQ;AAAA,IACX,WAAW;AAAA,IACX,UAAU;AAAA,IACV,YAAY;AAAA,EAAA;AAEd,QAAM,EAAE,UAAU,QAAQ,WAAW,OAAO,QAAQ,MAAM,MAAA,IACxD,cAAuE;AAAA,IACrE,GAAG;AAAA,IACH;AAAA,EAAA,CACD;AAEH,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEJ;"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { StreamChunk, TTSResult } from '@tanstack/ai';
|
|
2
|
+
import { AIDevtoolsDisplayOptions, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher, InferGenerationOutputFromReturn, SpeechGenerateInput } from '@tanstack/ai-client';
|
|
3
|
+
/**
|
|
4
|
+
* Options for the useGenerateSpeech hook.
|
|
5
|
+
*
|
|
6
|
+
* @template TOutput - The output type after optional transform (defaults to TTSResult)
|
|
7
|
+
*/
|
|
8
|
+
export interface UseGenerateSpeechOptions<TOutput = TTSResult> {
|
|
9
|
+
/** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
10
|
+
connection?: ConnectConnectionAdapter;
|
|
11
|
+
/** Direct async function for speech generation */
|
|
12
|
+
fetcher?: GenerationFetcher<SpeechGenerateInput, TTSResult>;
|
|
13
|
+
/** Unique identifier for this generation instance */
|
|
14
|
+
id?: string;
|
|
15
|
+
/** Additional body parameters to send with connect-based adapter requests */
|
|
16
|
+
body?: Record<string, any>;
|
|
17
|
+
/** Display options for TanStack AI Devtools. */
|
|
18
|
+
devtools?: AIDevtoolsDisplayOptions;
|
|
19
|
+
/**
|
|
20
|
+
* Callback when speech is generated. Can optionally return a transformed value.
|
|
21
|
+
*
|
|
22
|
+
* - Return a non-null value to transform and store it as the result
|
|
23
|
+
* - Return `null` to keep the previous result unchanged
|
|
24
|
+
* - Return nothing (`void`) to store the raw result as-is
|
|
25
|
+
*/
|
|
26
|
+
onResult?: (result: TTSResult) => TOutput | null | void;
|
|
27
|
+
/** Callback when an error occurs */
|
|
28
|
+
onError?: (error: Error) => void;
|
|
29
|
+
/** Callback when progress is reported (0-100) */
|
|
30
|
+
onProgress?: (progress: number, message?: string) => void;
|
|
31
|
+
/** Callback for each stream chunk (connect-based adapter mode only) */
|
|
32
|
+
onChunk?: (chunk: StreamChunk) => void;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Return type for the useGenerateSpeech hook.
|
|
36
|
+
*
|
|
37
|
+
* @template TOutput - The output type (after optional transform)
|
|
38
|
+
*/
|
|
39
|
+
export interface UseGenerateSpeechReturn<TOutput = TTSResult> {
|
|
40
|
+
/** Trigger speech generation */
|
|
41
|
+
generate: (input: SpeechGenerateInput) => Promise<void>;
|
|
42
|
+
/** The TTS result containing audio data, or null */
|
|
43
|
+
result: TOutput | null;
|
|
44
|
+
/** Whether generation is in progress */
|
|
45
|
+
isLoading: boolean;
|
|
46
|
+
/** Current error, if any */
|
|
47
|
+
error: Error | undefined;
|
|
48
|
+
/** Current state of the generation */
|
|
49
|
+
status: GenerationClientState;
|
|
50
|
+
/** Abort the current generation */
|
|
51
|
+
stop: () => void;
|
|
52
|
+
/** Clear result, error, and return to idle */
|
|
53
|
+
reset: () => void;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* React hook for generating speech (text-to-speech) using AI models.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```tsx
|
|
60
|
+
* import { useGenerateSpeech } from '@tanstack/ai-react'
|
|
61
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
62
|
+
*
|
|
63
|
+
* function SpeechGenerator() {
|
|
64
|
+
* const { generate, result, isLoading } = useGenerateSpeech({
|
|
65
|
+
* connection: fetchServerSentEvents('/api/generate/speech'),
|
|
66
|
+
* })
|
|
67
|
+
*
|
|
68
|
+
* return (
|
|
69
|
+
* <div>
|
|
70
|
+
* <button onClick={() => generate({ text: 'Hello world', voice: 'alloy' })}>
|
|
71
|
+
* Generate Speech
|
|
72
|
+
* </button>
|
|
73
|
+
* {result && (
|
|
74
|
+
* <audio src={`data:audio/${result.format};base64,${result.audio}`} controls />
|
|
75
|
+
* )}
|
|
76
|
+
* </div>
|
|
77
|
+
* )
|
|
78
|
+
* }
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export declare function useGenerateSpeech<TTransformed = void>(options: Omit<UseGenerateSpeechOptions, 'onResult'> & {
|
|
82
|
+
onResult?: (result: TTSResult) => TTransformed;
|
|
83
|
+
}): UseGenerateSpeechReturn<InferGenerationOutputFromReturn<TTSResult, TTransformed>>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useGeneration } from "./use-generation.js";
|
|
2
|
+
function useGenerateSpeech(options) {
|
|
3
|
+
const devtools = {
|
|
4
|
+
...options.devtools,
|
|
5
|
+
framework: "react",
|
|
6
|
+
hookName: "useGenerateSpeech",
|
|
7
|
+
outputKind: "audio"
|
|
8
|
+
};
|
|
9
|
+
const { generate, result, isLoading, error, status, stop, reset } = useGeneration({
|
|
10
|
+
...options,
|
|
11
|
+
devtools
|
|
12
|
+
});
|
|
13
|
+
return {
|
|
14
|
+
generate,
|
|
15
|
+
result,
|
|
16
|
+
isLoading,
|
|
17
|
+
error,
|
|
18
|
+
status,
|
|
19
|
+
stop,
|
|
20
|
+
reset
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export {
|
|
24
|
+
useGenerateSpeech
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=use-generate-speech.js.map
|