@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,189 @@
|
|
|
1
|
+
import { useState, useRef, useEffect, useCallback } from "react";
|
|
2
|
+
import { RealtimeClient } from "@tanstack/ai-client";
|
|
3
|
+
const emptyFrequencyData = new Uint8Array(128);
|
|
4
|
+
const emptyTimeDomainData = new Uint8Array(128).fill(128);
|
|
5
|
+
function useRealtimeChat(options) {
|
|
6
|
+
const [status, setStatus] = useState("idle");
|
|
7
|
+
const [mode, setMode] = useState("idle");
|
|
8
|
+
const [messages, setMessages] = useState([]);
|
|
9
|
+
const [pendingUserTranscript, setPendingUserTranscript] = useState(null);
|
|
10
|
+
const [pendingAssistantTranscript, setPendingAssistantTranscript] = useState(null);
|
|
11
|
+
const [error, setError] = useState(null);
|
|
12
|
+
const [inputLevel, setInputLevel] = useState(0);
|
|
13
|
+
const [outputLevel, setOutputLevel] = useState(0);
|
|
14
|
+
const [vadMode, setVADModeState] = useState(
|
|
15
|
+
options.vadMode ?? "server"
|
|
16
|
+
);
|
|
17
|
+
const clientRef = useRef(null);
|
|
18
|
+
const optionsRef = useRef(options);
|
|
19
|
+
optionsRef.current = options;
|
|
20
|
+
const animationFrameRef = useRef(null);
|
|
21
|
+
if (!clientRef.current) {
|
|
22
|
+
const initial = optionsRef.current;
|
|
23
|
+
clientRef.current = new RealtimeClient({
|
|
24
|
+
getToken: initial.getToken,
|
|
25
|
+
adapter: initial.adapter,
|
|
26
|
+
...initial.tools !== void 0 && { tools: initial.tools },
|
|
27
|
+
...initial.instructions !== void 0 && {
|
|
28
|
+
instructions: initial.instructions
|
|
29
|
+
},
|
|
30
|
+
...initial.voice !== void 0 && { voice: initial.voice },
|
|
31
|
+
...initial.autoPlayback !== void 0 && {
|
|
32
|
+
autoPlayback: initial.autoPlayback
|
|
33
|
+
},
|
|
34
|
+
...initial.autoCapture !== void 0 && {
|
|
35
|
+
autoCapture: initial.autoCapture
|
|
36
|
+
},
|
|
37
|
+
...initial.vadMode !== void 0 && { vadMode: initial.vadMode },
|
|
38
|
+
...initial.outputModalities !== void 0 && {
|
|
39
|
+
outputModalities: initial.outputModalities
|
|
40
|
+
},
|
|
41
|
+
...initial.temperature !== void 0 && {
|
|
42
|
+
temperature: initial.temperature
|
|
43
|
+
},
|
|
44
|
+
...initial.maxOutputTokens !== void 0 && {
|
|
45
|
+
maxOutputTokens: initial.maxOutputTokens
|
|
46
|
+
},
|
|
47
|
+
...initial.semanticEagerness !== void 0 && {
|
|
48
|
+
semanticEagerness: initial.semanticEagerness
|
|
49
|
+
},
|
|
50
|
+
onStatusChange: (newStatus) => {
|
|
51
|
+
setStatus(newStatus);
|
|
52
|
+
},
|
|
53
|
+
onModeChange: (newMode) => {
|
|
54
|
+
setMode(newMode);
|
|
55
|
+
optionsRef.current.onModeChange?.(newMode);
|
|
56
|
+
},
|
|
57
|
+
onMessage: (message) => {
|
|
58
|
+
setMessages((prev) => [...prev, message]);
|
|
59
|
+
optionsRef.current.onMessage?.(message);
|
|
60
|
+
},
|
|
61
|
+
onError: (err) => {
|
|
62
|
+
setError(err);
|
|
63
|
+
optionsRef.current.onError?.(err);
|
|
64
|
+
},
|
|
65
|
+
onConnect: () => {
|
|
66
|
+
setError(null);
|
|
67
|
+
optionsRef.current.onConnect?.();
|
|
68
|
+
},
|
|
69
|
+
onDisconnect: () => {
|
|
70
|
+
optionsRef.current.onDisconnect?.();
|
|
71
|
+
},
|
|
72
|
+
onInterrupted: () => {
|
|
73
|
+
setPendingAssistantTranscript(null);
|
|
74
|
+
optionsRef.current.onInterrupted?.();
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
clientRef.current.onStateChange((state) => {
|
|
78
|
+
setPendingUserTranscript(state.pendingUserTranscript);
|
|
79
|
+
setPendingAssistantTranscript(state.pendingAssistantTranscript);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
const client = clientRef.current;
|
|
83
|
+
useEffect(() => {
|
|
84
|
+
function updateLevels() {
|
|
85
|
+
if (clientRef.current?.audio) {
|
|
86
|
+
setInputLevel(clientRef.current.audio.inputLevel);
|
|
87
|
+
setOutputLevel(clientRef.current.audio.outputLevel);
|
|
88
|
+
}
|
|
89
|
+
animationFrameRef.current = requestAnimationFrame(updateLevels);
|
|
90
|
+
}
|
|
91
|
+
if (status === "connected") {
|
|
92
|
+
updateLevels();
|
|
93
|
+
}
|
|
94
|
+
return () => {
|
|
95
|
+
if (animationFrameRef.current) {
|
|
96
|
+
cancelAnimationFrame(animationFrameRef.current);
|
|
97
|
+
animationFrameRef.current = null;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}, [status]);
|
|
101
|
+
useEffect(() => {
|
|
102
|
+
return () => {
|
|
103
|
+
clientRef.current?.destroy();
|
|
104
|
+
};
|
|
105
|
+
}, []);
|
|
106
|
+
const connect = useCallback(async () => {
|
|
107
|
+
setError(null);
|
|
108
|
+
setMessages([]);
|
|
109
|
+
setPendingUserTranscript(null);
|
|
110
|
+
setPendingAssistantTranscript(null);
|
|
111
|
+
await client.connect();
|
|
112
|
+
}, [client]);
|
|
113
|
+
const disconnect = useCallback(async () => {
|
|
114
|
+
await client.disconnect();
|
|
115
|
+
}, [client]);
|
|
116
|
+
const startListening = useCallback(() => {
|
|
117
|
+
client.startListening();
|
|
118
|
+
}, [client]);
|
|
119
|
+
const stopListening = useCallback(() => {
|
|
120
|
+
client.stopListening();
|
|
121
|
+
}, [client]);
|
|
122
|
+
const interrupt = useCallback(() => {
|
|
123
|
+
client.interrupt();
|
|
124
|
+
}, [client]);
|
|
125
|
+
const sendText = useCallback(
|
|
126
|
+
(text) => {
|
|
127
|
+
client.sendText(text);
|
|
128
|
+
},
|
|
129
|
+
[client]
|
|
130
|
+
);
|
|
131
|
+
const sendImage = useCallback(
|
|
132
|
+
(imageData, mimeType) => {
|
|
133
|
+
client.sendImage(imageData, mimeType);
|
|
134
|
+
},
|
|
135
|
+
[client]
|
|
136
|
+
);
|
|
137
|
+
const getInputFrequencyData = useCallback(() => {
|
|
138
|
+
return clientRef.current?.audio?.getInputFrequencyData() ?? emptyFrequencyData;
|
|
139
|
+
}, []);
|
|
140
|
+
const getOutputFrequencyData = useCallback(() => {
|
|
141
|
+
return clientRef.current?.audio?.getOutputFrequencyData() ?? emptyFrequencyData;
|
|
142
|
+
}, []);
|
|
143
|
+
const getInputTimeDomainData = useCallback(() => {
|
|
144
|
+
return clientRef.current?.audio?.getInputTimeDomainData() ?? emptyTimeDomainData;
|
|
145
|
+
}, []);
|
|
146
|
+
const getOutputTimeDomainData = useCallback(() => {
|
|
147
|
+
return clientRef.current?.audio?.getOutputTimeDomainData() ?? emptyTimeDomainData;
|
|
148
|
+
}, []);
|
|
149
|
+
const setVADMode = useCallback(
|
|
150
|
+
(newMode) => {
|
|
151
|
+
setVADModeState(newMode);
|
|
152
|
+
},
|
|
153
|
+
[]
|
|
154
|
+
);
|
|
155
|
+
return {
|
|
156
|
+
// Connection state
|
|
157
|
+
status,
|
|
158
|
+
error,
|
|
159
|
+
connect,
|
|
160
|
+
disconnect,
|
|
161
|
+
// Conversation state
|
|
162
|
+
mode,
|
|
163
|
+
messages,
|
|
164
|
+
pendingUserTranscript,
|
|
165
|
+
pendingAssistantTranscript,
|
|
166
|
+
// Voice control
|
|
167
|
+
startListening,
|
|
168
|
+
stopListening,
|
|
169
|
+
interrupt,
|
|
170
|
+
// Text input
|
|
171
|
+
sendText,
|
|
172
|
+
// Image input
|
|
173
|
+
sendImage,
|
|
174
|
+
// Audio visualization
|
|
175
|
+
inputLevel,
|
|
176
|
+
outputLevel,
|
|
177
|
+
getInputFrequencyData,
|
|
178
|
+
getOutputFrequencyData,
|
|
179
|
+
getInputTimeDomainData,
|
|
180
|
+
getOutputTimeDomainData,
|
|
181
|
+
// VAD control
|
|
182
|
+
vadMode,
|
|
183
|
+
setVADMode
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
export {
|
|
187
|
+
useRealtimeChat
|
|
188
|
+
};
|
|
189
|
+
//# sourceMappingURL=use-realtime-chat.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-realtime-chat.js","sources":["../../src/use-realtime-chat.ts"],"sourcesContent":["import { useCallback, useEffect, useRef, useState } from 'react'\nimport { RealtimeClient } from '@tanstack/ai-client'\nimport type {\n RealtimeMessage,\n RealtimeMode,\n RealtimeStatus,\n} from '@tanstack/ai'\nimport type {\n UseRealtimeChatOptions,\n UseRealtimeChatReturn,\n} from './realtime-types'\n\n// Empty frequency data for when client is not connected\nconst emptyFrequencyData = new Uint8Array(128)\nconst emptyTimeDomainData = new Uint8Array(128).fill(128)\n\n/**\n * React hook for realtime voice conversations.\n *\n * Provides a simple interface for voice-to-voice AI interactions\n * with support for multiple providers (OpenAI, ElevenLabs, etc.).\n *\n * @param options - Configuration options including adapter and callbacks\n * @returns Hook return value with state and control methods\n *\n * @example\n * ```typescript\n * import { useRealtimeChat } from '@tanstack/ai-react'\n * import { openaiRealtime } from '@tanstack/ai-openai'\n *\n * function VoiceChat() {\n * const {\n * status,\n * mode,\n * messages,\n * connect,\n * disconnect,\n * inputLevel,\n * outputLevel,\n * } = useRealtimeChat({\n * getToken: () => fetch('/api/realtime-token').then(r => r.json()),\n * adapter: openaiRealtime(),\n * })\n *\n * return (\n * <div>\n * <p>Status: {status}</p>\n * <p>Mode: {mode}</p>\n * <button onClick={status === 'idle' ? connect : disconnect}>\n * {status === 'idle' ? 'Start' : 'Stop'}\n * </button>\n * </div>\n * )\n * }\n * ```\n */\nexport function useRealtimeChat(\n options: UseRealtimeChatOptions,\n): UseRealtimeChatReturn {\n // State\n const [status, setStatus] = useState<RealtimeStatus>('idle')\n const [mode, setMode] = useState<RealtimeMode>('idle')\n const [messages, setMessages] = useState<Array<RealtimeMessage>>([])\n const [pendingUserTranscript, setPendingUserTranscript] = useState<\n string | null\n >(null)\n const [pendingAssistantTranscript, setPendingAssistantTranscript] = useState<\n string | null\n >(null)\n const [error, setError] = useState<Error | null>(null)\n const [inputLevel, setInputLevel] = useState(0)\n const [outputLevel, setOutputLevel] = useState(0)\n const [vadMode, setVADModeState] = useState<'server' | 'semantic' | 'manual'>(\n options.vadMode ?? 'server',\n )\n\n // Refs\n const clientRef = useRef<RealtimeClient | null>(null)\n const optionsRef = useRef(options)\n optionsRef.current = options\n const animationFrameRef = useRef<number | null>(null)\n\n // Create client instance - use ref to ensure we reuse the same instance\n // This handles React StrictMode double-rendering\n if (!clientRef.current) {\n // Each optional source field is spread conditionally because the\n // `RealtimeClientOptions` target declares strict optionals\n // (`field?: T`) and `exactOptionalPropertyTypes` rejects passing\n // `undefined` for absent values.\n const initial = optionsRef.current\n clientRef.current = new RealtimeClient({\n getToken: initial.getToken,\n adapter: initial.adapter,\n ...(initial.tools !== undefined && { tools: initial.tools }),\n ...(initial.instructions !== undefined && {\n instructions: initial.instructions,\n }),\n ...(initial.voice !== undefined && { voice: initial.voice }),\n ...(initial.autoPlayback !== undefined && {\n autoPlayback: initial.autoPlayback,\n }),\n ...(initial.autoCapture !== undefined && {\n autoCapture: initial.autoCapture,\n }),\n ...(initial.vadMode !== undefined && { vadMode: initial.vadMode }),\n ...(initial.outputModalities !== undefined && {\n outputModalities: initial.outputModalities,\n }),\n ...(initial.temperature !== undefined && {\n temperature: initial.temperature,\n }),\n ...(initial.maxOutputTokens !== undefined && {\n maxOutputTokens: initial.maxOutputTokens,\n }),\n ...(initial.semanticEagerness !== undefined && {\n semanticEagerness: initial.semanticEagerness,\n }),\n onStatusChange: (newStatus) => {\n setStatus(newStatus)\n },\n onModeChange: (newMode) => {\n setMode(newMode)\n optionsRef.current.onModeChange?.(newMode)\n },\n onMessage: (message) => {\n setMessages((prev) => [...prev, message])\n optionsRef.current.onMessage?.(message)\n },\n onError: (err) => {\n setError(err)\n optionsRef.current.onError?.(err)\n },\n onConnect: () => {\n setError(null)\n optionsRef.current.onConnect?.()\n },\n onDisconnect: () => {\n optionsRef.current.onDisconnect?.()\n },\n onInterrupted: () => {\n setPendingAssistantTranscript(null)\n optionsRef.current.onInterrupted?.()\n },\n })\n\n // Subscribe to state changes for transcripts\n clientRef.current.onStateChange((state) => {\n setPendingUserTranscript(state.pendingUserTranscript)\n setPendingAssistantTranscript(state.pendingAssistantTranscript)\n })\n }\n\n const client = clientRef.current\n\n // Audio level animation loop\n useEffect(() => {\n function updateLevels() {\n if (clientRef.current?.audio) {\n setInputLevel(clientRef.current.audio.inputLevel)\n setOutputLevel(clientRef.current.audio.outputLevel)\n }\n animationFrameRef.current = requestAnimationFrame(updateLevels)\n }\n\n if (status === 'connected') {\n updateLevels()\n }\n\n return () => {\n if (animationFrameRef.current) {\n cancelAnimationFrame(animationFrameRef.current)\n animationFrameRef.current = null\n }\n }\n }, [status])\n\n // Cleanup on unmount\n useEffect(() => {\n return () => {\n clientRef.current?.destroy()\n }\n }, [])\n\n // Connection methods\n const connect = useCallback(async () => {\n setError(null)\n setMessages([])\n setPendingUserTranscript(null)\n setPendingAssistantTranscript(null)\n await client.connect()\n }, [client])\n\n const disconnect = useCallback(async () => {\n await client.disconnect()\n }, [client])\n\n // Voice control methods\n const startListening = useCallback(() => {\n client.startListening()\n }, [client])\n\n const stopListening = useCallback(() => {\n client.stopListening()\n }, [client])\n\n const interrupt = useCallback(() => {\n client.interrupt()\n }, [client])\n\n // Text input\n const sendText = useCallback(\n (text: string) => {\n client.sendText(text)\n },\n [client],\n )\n\n // Image input\n const sendImage = useCallback(\n (imageData: string, mimeType: string) => {\n client.sendImage(imageData, mimeType)\n },\n [client],\n )\n\n // Audio visualization\n const getInputFrequencyData = useCallback(() => {\n return (\n clientRef.current?.audio?.getInputFrequencyData() ?? emptyFrequencyData\n )\n }, [])\n\n const getOutputFrequencyData = useCallback(() => {\n return (\n clientRef.current?.audio?.getOutputFrequencyData() ?? emptyFrequencyData\n )\n }, [])\n\n const getInputTimeDomainData = useCallback(() => {\n return (\n clientRef.current?.audio?.getInputTimeDomainData() ?? emptyTimeDomainData\n )\n }, [])\n\n const getOutputTimeDomainData = useCallback(() => {\n return (\n clientRef.current?.audio?.getOutputTimeDomainData() ?? emptyTimeDomainData\n )\n }, [])\n\n // VAD mode control\n const setVADMode = useCallback(\n (newMode: 'server' | 'semantic' | 'manual') => {\n setVADModeState(newMode)\n // TODO: Update session config if connected\n },\n [],\n )\n\n return {\n // Connection state\n status,\n error,\n connect,\n disconnect,\n\n // Conversation state\n mode,\n messages,\n pendingUserTranscript,\n pendingAssistantTranscript,\n\n // Voice control\n startListening,\n stopListening,\n interrupt,\n\n // Text input\n sendText,\n\n // Image input\n sendImage,\n\n // Audio visualization\n inputLevel,\n outputLevel,\n getInputFrequencyData,\n getOutputFrequencyData,\n getInputTimeDomainData,\n getOutputTimeDomainData,\n\n // VAD control\n vadMode,\n setVADMode,\n }\n}\n"],"names":[],"mappings":";;AAaA,MAAM,qBAAqB,IAAI,WAAW,GAAG;AAC7C,MAAM,sBAAsB,IAAI,WAAW,GAAG,EAAE,KAAK,GAAG;AA0CjD,SAAS,gBACd,SACuB;AAEvB,QAAM,CAAC,QAAQ,SAAS,IAAI,SAAyB,MAAM;AAC3D,QAAM,CAAC,MAAM,OAAO,IAAI,SAAuB,MAAM;AACrD,QAAM,CAAC,UAAU,WAAW,IAAI,SAAiC,CAAA,CAAE;AACnE,QAAM,CAAC,uBAAuB,wBAAwB,IAAI,SAExD,IAAI;AACN,QAAM,CAAC,4BAA4B,6BAA6B,IAAI,SAElE,IAAI;AACN,QAAM,CAAC,OAAO,QAAQ,IA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,sBAAgB,OAAO;AAAA,IAEzB;AAAA,IACA,CAAA;AAAA,EAAC;AAGH,SAAO;AAAA;AAAA,IAEL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA;AAAA,IAGA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA;AAAA,IAGA;AAAA,IACA;AAAA,IACA;AAAA;AAAA,IAGA;AAAA;AAAA,IAGA;AAAA;AAAA,IAGA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA;AAAA,IAGA;AAAA,IACA;AAAA,EAAA;AAEJ;"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { StreamChunk, SummarizationResult } from '@tanstack/ai';
|
|
2
|
+
import { AIDevtoolsDisplayOptions, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher, InferGenerationOutputFromReturn, SummarizeGenerateInput } from '@tanstack/ai-client';
|
|
3
|
+
/**
|
|
4
|
+
* Options for the useSummarize hook.
|
|
5
|
+
*
|
|
6
|
+
* @template TOutput - The output type after optional transform (defaults to SummarizationResult)
|
|
7
|
+
*/
|
|
8
|
+
export interface UseSummarizeOptions<TOutput = SummarizationResult> {
|
|
9
|
+
/** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
10
|
+
connection?: ConnectConnectionAdapter;
|
|
11
|
+
/** Direct async function for summarization */
|
|
12
|
+
fetcher?: GenerationFetcher<SummarizeGenerateInput, SummarizationResult>;
|
|
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 summarization is complete. 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: SummarizationResult) => 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 useSummarize hook.
|
|
36
|
+
*
|
|
37
|
+
* @template TOutput - The output type (after optional transform)
|
|
38
|
+
*/
|
|
39
|
+
export interface UseSummarizeReturn<TOutput = SummarizationResult> {
|
|
40
|
+
/** Trigger summarization */
|
|
41
|
+
generate: (input: SummarizeGenerateInput) => Promise<void>;
|
|
42
|
+
/** The summarization result, or null */
|
|
43
|
+
result: TOutput | null;
|
|
44
|
+
/** Whether summarization 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 summarization */
|
|
51
|
+
stop: () => void;
|
|
52
|
+
/** Clear result, error, and return to idle */
|
|
53
|
+
reset: () => void;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* React hook for summarizing text using AI models.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```tsx
|
|
60
|
+
* import { useSummarize } from '@tanstack/ai-react'
|
|
61
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
62
|
+
*
|
|
63
|
+
* function Summarizer() {
|
|
64
|
+
* const { generate, result, isLoading } = useSummarize({
|
|
65
|
+
* connection: fetchServerSentEvents('/api/summarize'),
|
|
66
|
+
* })
|
|
67
|
+
*
|
|
68
|
+
* return (
|
|
69
|
+
* <div>
|
|
70
|
+
* <button onClick={() => generate({
|
|
71
|
+
* text: 'Long article text...',
|
|
72
|
+
* style: 'bullet-points',
|
|
73
|
+
* maxLength: 200,
|
|
74
|
+
* })}>
|
|
75
|
+
* Summarize
|
|
76
|
+
* </button>
|
|
77
|
+
* {isLoading && <p>Summarizing...</p>}
|
|
78
|
+
* {result && <p>{result.summary}</p>}
|
|
79
|
+
* </div>
|
|
80
|
+
* )
|
|
81
|
+
* }
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export declare function useSummarize<TTransformed = void>(options: Omit<UseSummarizeOptions, 'onResult'> & {
|
|
85
|
+
onResult?: (result: SummarizationResult) => TTransformed;
|
|
86
|
+
}): UseSummarizeReturn<InferGenerationOutputFromReturn<SummarizationResult, TTransformed>>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useGeneration } from "./use-generation.js";
|
|
2
|
+
function useSummarize(options) {
|
|
3
|
+
const devtools = {
|
|
4
|
+
...options.devtools,
|
|
5
|
+
framework: "react",
|
|
6
|
+
hookName: "useSummarize",
|
|
7
|
+
outputKind: "text"
|
|
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
|
+
useSummarize
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=use-summarize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-summarize.js","sources":["../../src/use-summarize.ts"],"sourcesContent":["import { useGeneration } from './use-generation'\nimport type { StreamChunk, SummarizationResult } from '@tanstack/ai'\nimport type {\n AIDevtoolsDisplayOptions,\n ConnectConnectionAdapter,\n GenerationClientState,\n GenerationFetcher,\n InferGenerationOutputFromReturn,\n SummarizeGenerateInput,\n} from '@tanstack/ai-client'\n\n/**\n * Options for the useSummarize hook.\n *\n * @template TOutput - The output type after optional transform (defaults to SummarizationResult)\n */\nexport interface UseSummarizeOptions<TOutput = SummarizationResult> {\n /** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */\n connection?: ConnectConnectionAdapter\n /** Direct async function for summarization */\n fetcher?: GenerationFetcher<SummarizeGenerateInput, SummarizationResult>\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 summarization is complete. 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: SummarizationResult) => 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 useSummarize hook.\n *\n * @template TOutput - The output type (after optional transform)\n */\nexport interface UseSummarizeReturn<TOutput = SummarizationResult> {\n /** Trigger summarization */\n generate: (input: SummarizeGenerateInput) => Promise<void>\n /** The summarization result, or null */\n result: TOutput | null\n /** Whether summarization 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 summarization */\n stop: () => void\n /** Clear result, error, and return to idle */\n reset: () => void\n}\n\n/**\n * React hook for summarizing text using AI models.\n *\n * @example\n * ```tsx\n * import { useSummarize } from '@tanstack/ai-react'\n * import { fetchServerSentEvents } from '@tanstack/ai-client'\n *\n * function Summarizer() {\n * const { generate, result, isLoading } = useSummarize({\n * connection: fetchServerSentEvents('/api/summarize'),\n * })\n *\n * return (\n * <div>\n * <button onClick={() => generate({\n * text: 'Long article text...',\n * style: 'bullet-points',\n * maxLength: 200,\n * })}>\n * Summarize\n * </button>\n * {isLoading && <p>Summarizing...</p>}\n * {result && <p>{result.summary}</p>}\n * </div>\n * )\n * }\n * ```\n */\nexport function useSummarize<TTransformed = void>(\n options: Omit<UseSummarizeOptions, 'onResult'> & {\n onResult?: (result: SummarizationResult) => TTransformed\n },\n): UseSummarizeReturn<\n InferGenerationOutputFromReturn<SummarizationResult, TTransformed>\n> {\n const devtools = {\n ...options.devtools,\n framework: 'react',\n hookName: 'useSummarize',\n outputKind: 'text' as const,\n }\n const { generate, result, isLoading, error, status, stop, reset } =\n useGeneration<SummarizeGenerateInput, SummarizationResult, TTransformed>({\n ...options,\n devtools,\n })\n\n return {\n generate: generate as (input: SummarizeGenerateInput) => Promise<void>,\n result,\n isLoading,\n error,\n status,\n stop,\n reset,\n }\n}\n"],"names":[],"mappings":";AA8FO,SAAS,aACd,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,cAAyE;AAAA,IACvE,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,91 @@
|
|
|
1
|
+
import { StreamChunk, TranscriptionResult } from '@tanstack/ai';
|
|
2
|
+
import { AIDevtoolsDisplayOptions, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher, InferGenerationOutputFromReturn, TranscriptionGenerateInput } from '@tanstack/ai-client';
|
|
3
|
+
/**
|
|
4
|
+
* Options for the useTranscription hook.
|
|
5
|
+
*
|
|
6
|
+
* @template TOutput - The output type after optional transform (defaults to TranscriptionResult)
|
|
7
|
+
*/
|
|
8
|
+
export interface UseTranscriptionOptions<TOutput = TranscriptionResult> {
|
|
9
|
+
/** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
10
|
+
connection?: ConnectConnectionAdapter;
|
|
11
|
+
/** Direct async function for transcription */
|
|
12
|
+
fetcher?: GenerationFetcher<TranscriptionGenerateInput, TranscriptionResult>;
|
|
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 transcription is complete. 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: TranscriptionResult) => 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 useTranscription hook.
|
|
36
|
+
*
|
|
37
|
+
* @template TOutput - The output type (after optional transform)
|
|
38
|
+
*/
|
|
39
|
+
export interface UseTranscriptionReturn<TOutput = TranscriptionResult> {
|
|
40
|
+
/** Trigger transcription */
|
|
41
|
+
generate: (input: TranscriptionGenerateInput) => Promise<void>;
|
|
42
|
+
/** The transcription result, or null */
|
|
43
|
+
result: TOutput | null;
|
|
44
|
+
/** Whether transcription 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 transcription */
|
|
51
|
+
stop: () => void;
|
|
52
|
+
/** Clear result, error, and return to idle */
|
|
53
|
+
reset: () => void;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* React hook for transcribing audio to text using AI models.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```tsx
|
|
60
|
+
* import { useTranscription } from '@tanstack/ai-react'
|
|
61
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
62
|
+
*
|
|
63
|
+
* function Transcriber() {
|
|
64
|
+
* const { generate, result, isLoading } = useTranscription({
|
|
65
|
+
* connection: fetchServerSentEvents('/api/transcribe'),
|
|
66
|
+
* })
|
|
67
|
+
*
|
|
68
|
+
* const handleFile = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
69
|
+
* const file = e.target.files?.[0]
|
|
70
|
+
* if (file) {
|
|
71
|
+
* const reader = new FileReader()
|
|
72
|
+
* reader.onload = () => {
|
|
73
|
+
* generate({ audio: reader.result as string, language: 'en' })
|
|
74
|
+
* }
|
|
75
|
+
* reader.readAsDataURL(file)
|
|
76
|
+
* }
|
|
77
|
+
* }
|
|
78
|
+
*
|
|
79
|
+
* return (
|
|
80
|
+
* <div>
|
|
81
|
+
* <input type="file" accept="audio/*" onChange={handleFile} />
|
|
82
|
+
* {isLoading && <p>Transcribing...</p>}
|
|
83
|
+
* {result && <p>{result.text}</p>}
|
|
84
|
+
* </div>
|
|
85
|
+
* )
|
|
86
|
+
* }
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export declare function useTranscription<TTransformed = void>(options: Omit<UseTranscriptionOptions, 'onResult'> & {
|
|
90
|
+
onResult?: (result: TranscriptionResult) => TTransformed;
|
|
91
|
+
}): UseTranscriptionReturn<InferGenerationOutputFromReturn<TranscriptionResult, TTransformed>>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useGeneration } from "./use-generation.js";
|
|
2
|
+
function useTranscription(options) {
|
|
3
|
+
const devtools = {
|
|
4
|
+
...options.devtools,
|
|
5
|
+
framework: "react",
|
|
6
|
+
hookName: "useTranscription",
|
|
7
|
+
outputKind: "text"
|
|
8
|
+
};
|
|
9
|
+
const { generate, result, isLoading, error, status, stop, reset } = useGeneration({ ...options, devtools });
|
|
10
|
+
return {
|
|
11
|
+
generate,
|
|
12
|
+
result,
|
|
13
|
+
isLoading,
|
|
14
|
+
error,
|
|
15
|
+
status,
|
|
16
|
+
stop,
|
|
17
|
+
reset
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export {
|
|
21
|
+
useTranscription
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=use-transcription.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-transcription.js","sources":["../../src/use-transcription.ts"],"sourcesContent":["import { useGeneration } from './use-generation'\nimport type { StreamChunk, TranscriptionResult } from '@tanstack/ai'\nimport type {\n AIDevtoolsDisplayOptions,\n ConnectConnectionAdapter,\n GenerationClientState,\n GenerationFetcher,\n InferGenerationOutputFromReturn,\n TranscriptionGenerateInput,\n} from '@tanstack/ai-client'\n\n/**\n * Options for the useTranscription hook.\n *\n * @template TOutput - The output type after optional transform (defaults to TranscriptionResult)\n */\nexport interface UseTranscriptionOptions<TOutput = TranscriptionResult> {\n /** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */\n connection?: ConnectConnectionAdapter\n /** Direct async function for transcription */\n fetcher?: GenerationFetcher<TranscriptionGenerateInput, TranscriptionResult>\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 transcription is complete. 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: TranscriptionResult) => 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 useTranscription hook.\n *\n * @template TOutput - The output type (after optional transform)\n */\nexport interface UseTranscriptionReturn<TOutput = TranscriptionResult> {\n /** Trigger transcription */\n generate: (input: TranscriptionGenerateInput) => Promise<void>\n /** The transcription result, or null */\n result: TOutput | null\n /** Whether transcription 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 transcription */\n stop: () => void\n /** Clear result, error, and return to idle */\n reset: () => void\n}\n\n/**\n * React hook for transcribing audio to text using AI models.\n *\n * @example\n * ```tsx\n * import { useTranscription } from '@tanstack/ai-react'\n * import { fetchServerSentEvents } from '@tanstack/ai-client'\n *\n * function Transcriber() {\n * const { generate, result, isLoading } = useTranscription({\n * connection: fetchServerSentEvents('/api/transcribe'),\n * })\n *\n * const handleFile = (e: React.ChangeEvent<HTMLInputElement>) => {\n * const file = e.target.files?.[0]\n * if (file) {\n * const reader = new FileReader()\n * reader.onload = () => {\n * generate({ audio: reader.result as string, language: 'en' })\n * }\n * reader.readAsDataURL(file)\n * }\n * }\n *\n * return (\n * <div>\n * <input type=\"file\" accept=\"audio/*\" onChange={handleFile} />\n * {isLoading && <p>Transcribing...</p>}\n * {result && <p>{result.text}</p>}\n * </div>\n * )\n * }\n * ```\n */\nexport function useTranscription<TTransformed = void>(\n options: Omit<UseTranscriptionOptions, 'onResult'> & {\n onResult?: (result: TranscriptionResult) => TTransformed\n },\n): UseTranscriptionReturn<\n InferGenerationOutputFromReturn<TranscriptionResult, TTransformed>\n> {\n const devtools = {\n ...options.devtools,\n framework: 'react',\n hookName: 'useTranscription',\n outputKind: 'text' as const,\n }\n const { generate, result, isLoading, error, status, stop, reset } =\n useGeneration<\n TranscriptionGenerateInput,\n TranscriptionResult,\n TTransformed\n >({ ...options, devtools })\n\n return {\n generate: generate as (input: TranscriptionGenerateInput) => Promise<void>,\n result,\n isLoading,\n error,\n status,\n stop,\n reset,\n }\n}\n"],"names":[],"mappings":";AAmGO,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,cAIE,EAAE,GAAG,SAAS,UAAU;AAE5B,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEJ;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
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",
|
|
@@ -24,6 +24,10 @@
|
|
|
24
24
|
".": {
|
|
25
25
|
"types": "./dist/esm/index.d.ts",
|
|
26
26
|
"import": "./dist/esm/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./mcp-apps": {
|
|
29
|
+
"types": "./dist/esm/mcp-apps.d.ts",
|
|
30
|
+
"import": "./dist/esm/mcp-apps.js"
|
|
27
31
|
}
|
|
28
32
|
},
|
|
29
33
|
"files": [
|
|
@@ -45,14 +49,21 @@
|
|
|
45
49
|
"media-generation"
|
|
46
50
|
],
|
|
47
51
|
"dependencies": {
|
|
48
|
-
"@tanstack/ai-client": "0.
|
|
52
|
+
"@tanstack/ai-client": "0.19.0"
|
|
49
53
|
},
|
|
50
54
|
"peerDependencies": {
|
|
55
|
+
"@mcp-ui/client": "^7",
|
|
51
56
|
"@types/react": ">=18.0.0",
|
|
52
57
|
"react": ">=18.0.0",
|
|
53
|
-
"@tanstack/ai": "^0.
|
|
58
|
+
"@tanstack/ai": "^0.38.0"
|
|
59
|
+
},
|
|
60
|
+
"peerDependenciesMeta": {
|
|
61
|
+
"@mcp-ui/client": {
|
|
62
|
+
"optional": true
|
|
63
|
+
}
|
|
54
64
|
},
|
|
55
65
|
"devDependencies": {
|
|
66
|
+
"@mcp-ui/client": "^7.1.1",
|
|
56
67
|
"@standard-schema/spec": "^1.1.0",
|
|
57
68
|
"@testing-library/react": "^16.3.0",
|
|
58
69
|
"@types/react": "^19.2.7",
|
|
@@ -60,7 +71,7 @@
|
|
|
60
71
|
"jsdom": "^27.2.0",
|
|
61
72
|
"react": "^19.2.3",
|
|
62
73
|
"vite": "^7.3.3",
|
|
63
|
-
"@tanstack/ai": "0.
|
|
74
|
+
"@tanstack/ai": "0.38.0"
|
|
64
75
|
},
|
|
65
76
|
"scripts": {
|
|
66
77
|
"clean": "premove ./build ./dist",
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export { useChat } from './use-chat'
|
|
2
2
|
export { useRealtimeChat } from './use-realtime-chat'
|
|
3
|
+
export { useMcpAppBridge } from './use-mcp-app-bridge'
|
|
4
|
+
export type { UseMcpAppBridgeOptions } from './use-mcp-app-bridge'
|
|
3
5
|
export type {
|
|
4
6
|
DeepPartial,
|
|
5
7
|
UseChatOptions,
|
|
@@ -52,6 +54,12 @@ export type {
|
|
|
52
54
|
UseGenerateVideoReturn,
|
|
53
55
|
} from './use-generate-video'
|
|
54
56
|
|
|
57
|
+
export { useAudioRecorder } from './use-audio-recorder'
|
|
58
|
+
export type {
|
|
59
|
+
UseAudioRecorderOptions,
|
|
60
|
+
UseAudioRecorderReturn,
|
|
61
|
+
} from './use-audio-recorder'
|
|
62
|
+
|
|
55
63
|
// Re-export from ai-client for convenience
|
|
56
64
|
export {
|
|
57
65
|
fetchServerSentEvents,
|
|
@@ -61,6 +69,9 @@ export {
|
|
|
61
69
|
stream,
|
|
62
70
|
rpcStream,
|
|
63
71
|
createChatClientOptions,
|
|
72
|
+
createMcpAppBridge,
|
|
73
|
+
type McpAppBridge,
|
|
74
|
+
type CreateMcpAppBridgeOptions,
|
|
64
75
|
type ChatFetcher,
|
|
65
76
|
type ChatFetcherInput,
|
|
66
77
|
type ChatFetcherOptions,
|
package/src/mcp-apps.tsx
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { AppRenderer } from '@mcp-ui/client'
|
|
2
|
+
import type { UIResourcePart } from '@tanstack/ai'
|
|
3
|
+
import type { McpAppBridge } from '@tanstack/ai-client'
|
|
4
|
+
import type { JSX } from 'react'
|
|
5
|
+
|
|
6
|
+
export interface MCPAppResourceProps {
|
|
7
|
+
/** The ui-resource part from a UIMessage assistant part. */
|
|
8
|
+
part: UIResourcePart
|
|
9
|
+
/**
|
|
10
|
+
* Framework-agnostic bridge for tool calls, prompt sending, and link opening.
|
|
11
|
+
* Omit it to render the widget in display-only mode — iframe interactions
|
|
12
|
+
* that would trigger tool calls or prompts are ignored.
|
|
13
|
+
*/
|
|
14
|
+
bridge?: McpAppBridge
|
|
15
|
+
/** Sandbox iframe configuration — must include the proxy page URL. */
|
|
16
|
+
sandbox: { url: URL }
|
|
17
|
+
/** Optional structured arguments forwarded to the guest UI once it's ready. */
|
|
18
|
+
toolInput?: Record<string, unknown>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
22
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Coalesce an arbitrary bridge result into the `string` required by the
|
|
27
|
+
* `CallToolResult` text content block. `JSON.stringify` is typed to return
|
|
28
|
+
* `string` but actually returns `undefined` for inputs like `undefined` or a
|
|
29
|
+
* function, so we narrow explicitly to avoid `text: undefined`.
|
|
30
|
+
*/
|
|
31
|
+
function resultToText(result: unknown): string {
|
|
32
|
+
if (typeof result === 'string') return result
|
|
33
|
+
// `JSON.stringify`'s lib signature claims `string`, but it returns
|
|
34
|
+
// `undefined` for `undefined`/function inputs; type the call honestly.
|
|
35
|
+
const stringify: (value: unknown) => string | undefined = JSON.stringify
|
|
36
|
+
return stringify(result) ?? 'null'
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function MCPAppResource(props: MCPAppResourceProps): JSX.Element {
|
|
40
|
+
const { bridge } = props
|
|
41
|
+
return (
|
|
42
|
+
<AppRenderer
|
|
43
|
+
toolName={props.part.toolName}
|
|
44
|
+
sandbox={props.sandbox}
|
|
45
|
+
html={props.part.resource.text}
|
|
46
|
+
toolResourceUri={props.part.resource.uri}
|
|
47
|
+
toolInput={props.toolInput}
|
|
48
|
+
onCallTool={
|
|
49
|
+
bridge
|
|
50
|
+
? async ({ name, arguments: args }) => {
|
|
51
|
+
const result = await bridge.callTool({
|
|
52
|
+
serverId: props.part.serverId,
|
|
53
|
+
toolName: name,
|
|
54
|
+
args,
|
|
55
|
+
})
|
|
56
|
+
const structuredContent = isRecord(result) ? result : undefined
|
|
57
|
+
return {
|
|
58
|
+
content: [
|
|
59
|
+
{
|
|
60
|
+
type: 'text' as const,
|
|
61
|
+
text: resultToText(result),
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
structuredContent,
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
: undefined
|
|
68
|
+
}
|
|
69
|
+
onMessage={
|
|
70
|
+
bridge
|
|
71
|
+
? async ({ content }) => {
|
|
72
|
+
const text = content
|
|
73
|
+
.filter(
|
|
74
|
+
(c): c is { type: 'text'; text: string } => c.type === 'text',
|
|
75
|
+
)
|
|
76
|
+
.map((c) => c.text)
|
|
77
|
+
.join('')
|
|
78
|
+
if (text) await bridge.sendPrompt(text)
|
|
79
|
+
return {}
|
|
80
|
+
}
|
|
81
|
+
: undefined
|
|
82
|
+
}
|
|
83
|
+
onOpenLink={
|
|
84
|
+
bridge ? ({ url }) => Promise.resolve(bridge.openLink(url)) : undefined
|
|
85
|
+
}
|
|
86
|
+
/>
|
|
87
|
+
)
|
|
88
|
+
}
|