@tanstack/ai-react 0.15.14 → 0.15.15

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.
@@ -0,0 +1,80 @@
1
+ import { StreamChunk } from '@tanstack/ai';
2
+ import { AIDevtoolsDisplayOptions, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher, InferGenerationOutput } from '@tanstack/ai-client';
3
+ /**
4
+ * Options for the useGeneration hook.
5
+ *
6
+ * Accepts either a `connection` (streaming transport) or a `fetcher` (direct async call).
7
+ *
8
+ * @template TInput - The input type for the generation request
9
+ * @template TResult - The result type returned by the generation
10
+ * @template TOutput - The output type after optional transform (defaults to TResult)
11
+ */
12
+ export interface UseGenerationOptions<TInput, TResult, TOutput = TResult> {
13
+ /** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */
14
+ connection?: ConnectConnectionAdapter;
15
+ /** Direct async function for one-shot generation (no streaming protocol needed) */
16
+ fetcher?: GenerationFetcher<TInput, TResult>;
17
+ /** Unique identifier for this generation instance */
18
+ id?: string;
19
+ /** Additional body parameters to send with connect-based adapter requests */
20
+ body?: Record<string, any>;
21
+ /** Display options for TanStack AI Devtools. */
22
+ devtools?: AIDevtoolsDisplayOptions;
23
+ /**
24
+ * Callback when a result is received. Can optionally return a transformed value.
25
+ *
26
+ * - Return a non-null value to transform and store it as the result
27
+ * - Return `null` to keep the previous result unchanged
28
+ * - Return nothing (`void`) to store the raw result as-is
29
+ */
30
+ onResult?: (result: TResult) => TOutput | null | void;
31
+ /** Callback when an error occurs */
32
+ onError?: (error: Error) => void;
33
+ /** Callback when progress is reported (0-100) */
34
+ onProgress?: (progress: number, message?: string) => void;
35
+ /** Callback for each stream chunk (connect-based adapter mode only) */
36
+ onChunk?: (chunk: StreamChunk) => void;
37
+ }
38
+ /**
39
+ * Return type for the useGeneration hook.
40
+ *
41
+ * @template TOutput - The output type (after optional transform)
42
+ */
43
+ export interface UseGenerationReturn<TOutput> {
44
+ /** Trigger a generation request */
45
+ generate: (input: Record<string, any>) => Promise<void>;
46
+ /** The generation result, or null if not yet generated */
47
+ result: TOutput | null;
48
+ /** Whether a generation is currently in progress */
49
+ isLoading: boolean;
50
+ /** Current error, if any */
51
+ error: Error | undefined;
52
+ /** Current state of the generation client */
53
+ status: GenerationClientState;
54
+ /** Abort the current generation */
55
+ stop: () => void;
56
+ /** Clear result, error, and return to idle */
57
+ reset: () => void;
58
+ }
59
+ /**
60
+ * Generic React hook for one-shot generation tasks.
61
+ *
62
+ * This is the base hook used by `useGenerateImage`, `useGenerateSpeech`,
63
+ * `useTranscription`, and `useSummarize`. You can also use it directly
64
+ * for custom generation types.
65
+ *
66
+ * @template TInput - The input type for the generation request
67
+ * @template TResult - The result type returned by the generation
68
+ *
69
+ * @example
70
+ * ```tsx
71
+ * const { generate, result, isLoading } = useGeneration<MyInput, MyResult>({
72
+ * connection: fetchServerSentEvents('/api/generate/custom'),
73
+ * })
74
+ *
75
+ * await generate({ prompt: 'Hello' })
76
+ * ```
77
+ */
78
+ export declare function useGeneration<TInput extends Record<string, any>, TResult, TOnResult extends ((result: TResult) => any) | undefined = undefined>(options: Omit<UseGenerationOptions<TInput, TResult>, 'onResult'> & {
79
+ onResult?: TOnResult;
80
+ }): UseGenerationReturn<InferGenerationOutput<TResult, TOnResult>>;
@@ -0,0 +1,91 @@
1
+ import { GenerationClient } from "@tanstack/ai-client";
2
+ import { createGenerationDevtoolsBridge } from "@tanstack/ai-client/devtools";
3
+ import { useId, useState, useRef, useMemo, useEffect, useCallback } from "react";
4
+ function useGeneration(options) {
5
+ const hookId = useId();
6
+ const clientId = options.id || hookId;
7
+ const [result, setResult] = useState(null);
8
+ const [isLoading, setIsLoading] = useState(false);
9
+ const [error, setError] = useState(void 0);
10
+ const [status, setStatus] = useState("idle");
11
+ const optionsRef = useRef(options);
12
+ optionsRef.current = options;
13
+ const client = useMemo(() => {
14
+ const opts = optionsRef.current;
15
+ const clientOptions = {
16
+ id: clientId,
17
+ body: opts.body,
18
+ devtoolsBridgeFactory: createGenerationDevtoolsBridge,
19
+ devtools: {
20
+ hookName: "useGeneration",
21
+ framework: "react",
22
+ ...opts.devtools
23
+ },
24
+ onResult: (r) => optionsRef.current.onResult?.(r),
25
+ onError: (e) => {
26
+ optionsRef.current.onError?.(e);
27
+ },
28
+ onProgress: (p, m) => {
29
+ optionsRef.current.onProgress?.(p, m);
30
+ },
31
+ onChunk: (c) => {
32
+ optionsRef.current.onChunk?.(c);
33
+ },
34
+ onResultChange: setResult,
35
+ onLoadingChange: setIsLoading,
36
+ onErrorChange: setError,
37
+ onStatusChange: setStatus
38
+ };
39
+ if (opts.connection) {
40
+ return new GenerationClient({
41
+ ...clientOptions,
42
+ connection: opts.connection
43
+ });
44
+ }
45
+ if (opts.fetcher) {
46
+ return new GenerationClient({
47
+ ...clientOptions,
48
+ fetcher: opts.fetcher
49
+ });
50
+ }
51
+ throw new Error(
52
+ "useGeneration requires either a connection or fetcher option"
53
+ );
54
+ }, [clientId]);
55
+ useEffect(() => {
56
+ client.updateOptions({
57
+ ...options.body !== void 0 && { body: options.body }
58
+ });
59
+ }, [client, options.body]);
60
+ useEffect(() => {
61
+ client.mountDevtools();
62
+ return () => {
63
+ client.dispose();
64
+ };
65
+ }, [client]);
66
+ const generate = useCallback(
67
+ async (input) => {
68
+ await client.generate(input);
69
+ },
70
+ [client]
71
+ );
72
+ const stop = useCallback(() => {
73
+ client.stop();
74
+ }, [client]);
75
+ const reset = useCallback(() => {
76
+ client.reset();
77
+ }, [client]);
78
+ return {
79
+ generate,
80
+ result,
81
+ isLoading,
82
+ error,
83
+ status,
84
+ stop,
85
+ reset
86
+ };
87
+ }
88
+ export {
89
+ useGeneration
90
+ };
91
+ //# sourceMappingURL=use-generation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-generation.js","sources":["../../src/use-generation.ts"],"sourcesContent":["import { GenerationClient } from '@tanstack/ai-client'\nimport { createGenerationDevtoolsBridge } from '@tanstack/ai-client/devtools'\nimport { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react'\nimport type { StreamChunk } from '@tanstack/ai'\nimport type {\n AIDevtoolsDisplayOptions,\n ConnectConnectionAdapter,\n GenerationClientOptions,\n GenerationClientState,\n GenerationFetcher,\n InferGenerationOutput,\n} from '@tanstack/ai-client'\n\n/**\n * Options for the useGeneration hook.\n *\n * Accepts either a `connection` (streaming transport) or a `fetcher` (direct async call).\n *\n * @template TInput - The input type for the generation request\n * @template TResult - The result type returned by the generation\n * @template TOutput - The output type after optional transform (defaults to TResult)\n */\nexport interface UseGenerationOptions<TInput, TResult, TOutput = TResult> {\n /** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */\n connection?: ConnectConnectionAdapter\n /** Direct async function for one-shot generation (no streaming protocol needed) */\n fetcher?: GenerationFetcher<TInput, TResult>\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 a result is received. 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: TResult) => 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 useGeneration hook.\n *\n * @template TOutput - The output type (after optional transform)\n */\nexport interface UseGenerationReturn<TOutput> {\n /** Trigger a generation request */\n generate: (input: Record<string, any>) => Promise<void>\n /** The generation result, or null if not yet generated */\n result: TOutput | null\n /** Whether a generation is currently in progress */\n isLoading: boolean\n /** Current error, if any */\n error: Error | undefined\n /** Current state of the generation client */\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 * Generic React hook for one-shot generation tasks.\n *\n * This is the base hook used by `useGenerateImage`, `useGenerateSpeech`,\n * `useTranscription`, and `useSummarize`. You can also use it directly\n * for custom generation types.\n *\n * @template TInput - The input type for the generation request\n * @template TResult - The result type returned by the generation\n *\n * @example\n * ```tsx\n * const { generate, result, isLoading } = useGeneration<MyInput, MyResult>({\n * connection: fetchServerSentEvents('/api/generate/custom'),\n * })\n *\n * await generate({ prompt: 'Hello' })\n * ```\n */\nexport function useGeneration<\n TInput extends Record<string, any>,\n TResult,\n TOnResult extends ((result: TResult) => any) | undefined = undefined,\n>(\n options: Omit<UseGenerationOptions<TInput, TResult>, 'onResult'> & {\n onResult?: TOnResult\n },\n): UseGenerationReturn<InferGenerationOutput<TResult, TOnResult>> {\n type TOutput = InferGenerationOutput<TResult, TOnResult>\n const hookId = useId()\n const clientId = options.id || hookId\n\n const [result, setResult] = useState<TOutput | null>(null)\n const [isLoading, setIsLoading] = useState(false)\n const [error, setError] = useState<Error | undefined>(undefined)\n const [status, setStatus] = useState<GenerationClientState>('idle')\n\n const optionsRef = useRef(options)\n optionsRef.current = options\n\n const client = useMemo(() => {\n const opts = optionsRef.current\n\n // Conditional spread for `body` (strict-optional in target;\n // local source is `Record<string, any> | undefined`). Callbacks\n // wrap optional ones in non-returning bodies so `?.()`'s\n // implicit `undefined` doesn't pollute the function return type.\n const clientOptions: GenerationClientOptions<TInput, TResult, TOutput> = {\n id: clientId,\n body: opts.body,\n devtoolsBridgeFactory: createGenerationDevtoolsBridge,\n devtools: {\n hookName: 'useGeneration',\n framework: 'react',\n ...opts.devtools,\n },\n onResult: (r: TResult) => optionsRef.current.onResult?.(r),\n onError: (e: Error) => {\n optionsRef.current.onError?.(e)\n },\n onProgress: (p: number, m?: string) => {\n optionsRef.current.onProgress?.(p, m)\n },\n onChunk: (c: StreamChunk) => {\n optionsRef.current.onChunk?.(c)\n },\n onResultChange: setResult,\n onLoadingChange: setIsLoading,\n onErrorChange: setError,\n onStatusChange: setStatus,\n }\n\n if (opts.connection) {\n return new GenerationClient<TInput, TResult, TOutput>({\n ...clientOptions,\n connection: opts.connection,\n })\n }\n\n if (opts.fetcher) {\n return new GenerationClient<TInput, TResult, TOutput>({\n ...clientOptions,\n fetcher: opts.fetcher,\n })\n }\n\n throw new Error(\n 'useGeneration requires either a connection or fetcher option',\n )\n }, [clientId])\n\n // Sync body changes without recreating client\n useEffect(() => {\n // Conditional spread: target uses strict-optional `body?: T`.\n client.updateOptions({\n ...(options.body !== undefined && { body: options.body }),\n })\n }, [client, options.body])\n\n // Cleanup on unmount\n useEffect(() => {\n client.mountDevtools()\n\n return () => {\n client.dispose()\n }\n }, [client])\n\n const generate = useCallback(\n async (input: TInput) => {\n await client.generate(input)\n },\n [client],\n )\n\n const stop = useCallback(() => {\n client.stop()\n }, [client])\n\n const reset = useCallback(() => {\n client.reset()\n }, [client])\n\n return {\n generate: generate as (input: Record<string, any>) => Promise<void>,\n result,\n isLoading,\n error,\n status,\n stop,\n reset,\n }\n}\n"],"names":[],"mappings":";;;AA0FO,SAAS,cAKd,SAGgE;AAEhE,QAAM,SAAS,MAAA;AACf,QAAM,WAAW,QAAQ,MAAM;AAE/B,QAAM,CAAC,QAAQ,SAAS,IAAI,SAAyB,IAAI;AACzD,QAAM,CAAC,WAAW,YAAY,IAAI,SAAS,KAAK;AAChD,QAAM,CAAC,OAAO,QAAQ,IAAI,SAA4B,MAAS;AAC/D,QAAM,CAAC,QAAQ,SAAS,IAAI,SAAgC,MAAM;AAElE,QAAM,aAAa,OAAO,OAAO;AACjC,aAAW,UAAU;AAErB,QAAM,SAAS,QAAQ,MAAM;AAC3B,UAAM,OAAO,WAAW;AAMxB,UAAM,gBAAmE;AAAA,MACvE,IAAI;AAAA,MACJ,MAAM,KAAK;AAAA,MACX,uBAAuB;AAAA,MACvB,UAAU;AAAA,QACR,UAAU;AAAA,QACV,WAAW;AAAA,QACX,GAAG,KAAK;AAAA,MAAA;AAAA,MAEV,UAAU,CAAC,MAAe,WA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M,CAAC;AAEX,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEJ;"}
@@ -0,0 +1,42 @@
1
+ import { UseRealtimeChatOptions, UseRealtimeChatReturn } from './realtime-types.js';
2
+ /**
3
+ * React hook for realtime voice conversations.
4
+ *
5
+ * Provides a simple interface for voice-to-voice AI interactions
6
+ * with support for multiple providers (OpenAI, ElevenLabs, etc.).
7
+ *
8
+ * @param options - Configuration options including adapter and callbacks
9
+ * @returns Hook return value with state and control methods
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * import { useRealtimeChat } from '@tanstack/ai-react'
14
+ * import { openaiRealtime } from '@tanstack/ai-openai'
15
+ *
16
+ * function VoiceChat() {
17
+ * const {
18
+ * status,
19
+ * mode,
20
+ * messages,
21
+ * connect,
22
+ * disconnect,
23
+ * inputLevel,
24
+ * outputLevel,
25
+ * } = useRealtimeChat({
26
+ * getToken: () => fetch('/api/realtime-token').then(r => r.json()),
27
+ * adapter: openaiRealtime(),
28
+ * })
29
+ *
30
+ * return (
31
+ * <div>
32
+ * <p>Status: {status}</p>
33
+ * <p>Mode: {mode}</p>
34
+ * <button onClick={status === 'idle' ? connect : disconnect}>
35
+ * {status === 'idle' ? 'Start' : 'Stop'}
36
+ * </button>
37
+ * </div>
38
+ * )
39
+ * }
40
+ * ```
41
+ */
42
+ export declare function useRealtimeChat(options: UseRealtimeChatOptions): UseRealtimeChatReturn;
@@ -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":";;AA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AA,KAA2B;AAAA,EAEzD,GAAG,CAAA,CAAE;AAEL,QAAM,yBAAyB,YAAY,MAAM;AAC/C,WACE,UAAU,SAAS,OAAO,uBAAA,KAA4B;AAAA,EAE1D,GAAG,CAAA,CAAE;AAEL,QAAM,yBAAyB,YAAY,MAAM;AAC/C,WACE,UAAU,SAAS,OAAO,uBAAA,KAA4B;AAAA,EAE1D,GAAG,CAAA,CAAE;AAEL,QAAM,0BAA0B,YAAY,MAAM;AAChD,WACE,UAAU,SAAS,OAAO,wBAAA,KAA6B;AAAA,EAE3D,GAAG,CAAA,CAAE;AAGL,QAAM,aAAa;AAAA,IACjB,CAAC,YAA8C;AAC7C,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, InferGenerationOutput, 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<TOnResult extends ((result: SummarizationResult) => any) | undefined = undefined>(options: Omit<UseSummarizeOptions, 'onResult'> & {
85
+ onResult?: TOnResult;
86
+ }): UseSummarizeReturn<InferGenerationOutput<SummarizationResult, TOnResult>>;
@@ -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 InferGenerationOutput,\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<\n TOnResult extends ((result: SummarizationResult) => any) | undefined =\n undefined,\n>(\n options: Omit<UseSummarizeOptions, 'onResult'> & {\n onResult?: TOnResult\n },\n): UseSummarizeReturn<InferGenerationOutput<SummarizationResult, TOnResult>> {\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, TOnResult>({\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,aAId,SAG2E;AAC3E,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,cAAsE;AAAA,IACpE,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, InferGenerationOutput, 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<TOnResult extends ((result: TranscriptionResult) => any) | undefined = undefined>(options: Omit<UseTranscriptionOptions, 'onResult'> & {
90
+ onResult?: TOnResult;
91
+ }): UseTranscriptionReturn<InferGenerationOutput<TranscriptionResult, TOnResult>>;