@tanstack/ai-react 0.6.3 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/dist/esm/index.d.ts +15 -1
  2. package/dist/esm/index.js +15 -1
  3. package/dist/esm/index.js.map +1 -1
  4. package/dist/esm/realtime-types.d.ts +109 -0
  5. package/dist/esm/use-generate-image.d.ts +87 -0
  6. package/dist/esm/use-generate-image.js +17 -0
  7. package/dist/esm/use-generate-image.js.map +1 -0
  8. package/dist/esm/use-generate-speech.d.ts +81 -0
  9. package/dist/esm/use-generate-speech.js +17 -0
  10. package/dist/esm/use-generate-speech.js.map +1 -0
  11. package/dist/esm/use-generate-video.d.ts +92 -0
  12. package/dist/esm/use-generate-video.js +83 -0
  13. package/dist/esm/use-generate-video.js.map +1 -0
  14. package/dist/esm/use-generation.d.ts +78 -0
  15. package/dist/esm/use-generation.js +75 -0
  16. package/dist/esm/use-generation.js.map +1 -0
  17. package/dist/esm/use-realtime-chat.d.ts +42 -0
  18. package/dist/esm/use-realtime-chat.js +174 -0
  19. package/dist/esm/use-realtime-chat.js.map +1 -0
  20. package/dist/esm/use-summarize.d.ts +84 -0
  21. package/dist/esm/use-summarize.js +19 -0
  22. package/dist/esm/use-summarize.js.map +1 -0
  23. package/dist/esm/use-transcription.d.ts +89 -0
  24. package/dist/esm/use-transcription.js +19 -0
  25. package/dist/esm/use-transcription.js.map +1 -0
  26. package/package.json +4 -4
  27. package/src/index.ts +47 -0
  28. package/src/realtime-types.ts +143 -0
  29. package/src/use-generate-image.ts +117 -0
  30. package/src/use-generate-speech.ts +108 -0
  31. package/src/use-generate-video.ts +206 -0
  32. package/src/use-generation.ts +180 -0
  33. package/src/use-realtime-chat.ts +277 -0
  34. package/src/use-summarize.ts +114 -0
  35. package/src/use-transcription.ts +121 -0
@@ -0,0 +1,75 @@
1
+ import { GenerationClient } from "@tanstack/ai-client";
2
+ import { useId, useState, useRef, useMemo, useEffect, useCallback } from "react";
3
+ function useGeneration(options) {
4
+ const hookId = useId();
5
+ const clientId = options.id || hookId;
6
+ const [result, setResult] = useState(null);
7
+ const [isLoading, setIsLoading] = useState(false);
8
+ const [error, setError] = useState(void 0);
9
+ const [status, setStatus] = useState("idle");
10
+ const optionsRef = useRef(options);
11
+ optionsRef.current = options;
12
+ const client = useMemo(() => {
13
+ const opts = optionsRef.current;
14
+ const clientOptions = {
15
+ id: clientId,
16
+ body: opts.body,
17
+ onResult: (r) => optionsRef.current.onResult?.(r),
18
+ onError: (e) => optionsRef.current.onError?.(e),
19
+ onProgress: (p, m) => optionsRef.current.onProgress?.(p, m),
20
+ onChunk: (c) => optionsRef.current.onChunk?.(c),
21
+ onResultChange: setResult,
22
+ onLoadingChange: setIsLoading,
23
+ onErrorChange: setError,
24
+ onStatusChange: setStatus
25
+ };
26
+ if (opts.connection) {
27
+ return new GenerationClient({
28
+ ...clientOptions,
29
+ connection: opts.connection
30
+ });
31
+ }
32
+ if (opts.fetcher) {
33
+ return new GenerationClient({
34
+ ...clientOptions,
35
+ fetcher: opts.fetcher
36
+ });
37
+ }
38
+ throw new Error(
39
+ "useGeneration requires either a connection or fetcher option"
40
+ );
41
+ }, [clientId]);
42
+ useEffect(() => {
43
+ client.updateOptions({ body: options.body });
44
+ }, [client, options.body]);
45
+ useEffect(() => {
46
+ return () => {
47
+ client.stop();
48
+ };
49
+ }, [client]);
50
+ const generate = useCallback(
51
+ async (input) => {
52
+ await client.generate(input);
53
+ },
54
+ [client]
55
+ );
56
+ const stop = useCallback(() => {
57
+ client.stop();
58
+ }, [client]);
59
+ const reset = useCallback(() => {
60
+ client.reset();
61
+ }, [client]);
62
+ return {
63
+ generate,
64
+ result,
65
+ isLoading,
66
+ error,
67
+ status,
68
+ stop,
69
+ reset
70
+ };
71
+ }
72
+ export {
73
+ useGeneration
74
+ };
75
+ //# 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 { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react'\nimport type { StreamChunk } from '@tanstack/ai'\nimport type {\n ConnectionAdapter,\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 /** Connection adapter for streaming transport (SSE, HTTP stream, custom) */\n connection?: ConnectionAdapter\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 ConnectionAdapter requests */\n body?: Record<string, any>\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 (ConnectionAdapter 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 const clientOptions: GenerationClientOptions<TInput, TResult, TOutput> = {\n id: clientId,\n body: opts.body,\n onResult: (r: TResult) => optionsRef.current.onResult?.(r),\n onError: (e: Error) => optionsRef.current.onError?.(e),\n onProgress: (p: number, m?: string) =>\n optionsRef.current.onProgress?.(p, m),\n onChunk: (c: StreamChunk) => optionsRef.current.onChunk?.(c),\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 client.updateOptions({ body: options.body })\n }, [client, options.body])\n\n // Cleanup on unmount\n useEffect(() => {\n return () => {\n client.stop()\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":";;AAsFO,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;AAExB,UAAM,gBAAmE;AAAA,MACvE,IAAI;AAAA,MACJ,MAAM,KAAK;AAAA,MACX,UAAU,CAAC,MAAe,WAAW,QAAQ,WAAW,CAAC;AAAA,MACzD,SAAS,CAAC,MAAa,WAAW,QAAQ,UAAU,CAAC;AAAA,MACrD,YAAY,CAAC,GAAW,MACtB,WAAW,QAAQ,aAAa,GAAG,CAAC;AAAA,MACtC,SAAS,CAAC,MAAmB,WAAW,QAAQ,UAAU,CAAC;AAAA,MAC3D,gBAAgB;AAAA,MAChB,iBAAiB;AAAA,MACjB,eAAe;AAAA,MACf,gBAAgB;AAAA,IAAA;AAGlB,QAAI,KAAK,YAAY;AACnB,aAAO,IAAI,iBAA2C;AAAA,QACpD,GAAG;AAAA,QACH,YAAY,KAAK;AAAA,MAAA,CAClB;AAAA,IACH;AAEA,QAAI,KAAK,SAAS;AAChB,aAAO,IAAI,iBAA2C;AAAA,QACpD,GAAG;AAAA,QACH,SAAS,KAAK;AAAA,MAAA,CACf;AAAA,IACH;AAEA,UAAM,IAAI;AAAA,MACR;AAAA,IAAA;AAAA,EAEJ,GAAG,CAAC,QAAQ,CAAC;AAGb,YAAU,MAAM;AACd,WAAO,cAAc,EAAE,MAAM,QAAQ,MAAM;AAAA,EAC7C,GAAG,CAAC,QAAQ,QAAQ,IAAI,CAAC;AAGzB,YAAU,MAAM;AACd,WAAO,MAAM;AACX,aAAO,KAAA;AAAA,IACT;AAAA,EACF,GAAG,CAAC,MAAM,CAAC;AAEX,QAAM,WAAW;AAAA,IACf,OAAO,UAAkB;AACvB,YAAM,OAAO,SAAS,KAAK;AAAA,IAC7B;AAAA,IACA,CAAC,MAAM;AAAA,EAAA;AAGT,QAAM,OAAO,YAAY,MAAM;AAC7B,WAAO,KAAA;AAAA,EACT,GAAG,CAAC,MAAM,CAAC;AAEX,QAAM,QAAQ,YAAY,MAAM;AAC9B,WAAO,MAAA;AAAA,EACT,GAAG,CAAC,MAAM,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,174 @@
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
+ clientRef.current = new RealtimeClient({
23
+ getToken: optionsRef.current.getToken,
24
+ adapter: optionsRef.current.adapter,
25
+ tools: optionsRef.current.tools,
26
+ instructions: optionsRef.current.instructions,
27
+ voice: optionsRef.current.voice,
28
+ autoPlayback: optionsRef.current.autoPlayback,
29
+ autoCapture: optionsRef.current.autoCapture,
30
+ vadMode: optionsRef.current.vadMode,
31
+ outputModalities: optionsRef.current.outputModalities,
32
+ temperature: optionsRef.current.temperature,
33
+ maxOutputTokens: optionsRef.current.maxOutputTokens,
34
+ semanticEagerness: optionsRef.current.semanticEagerness,
35
+ onStatusChange: (newStatus) => {
36
+ setStatus(newStatus);
37
+ },
38
+ onModeChange: (newMode) => {
39
+ setMode(newMode);
40
+ optionsRef.current.onModeChange?.(newMode);
41
+ },
42
+ onMessage: (message) => {
43
+ setMessages((prev) => [...prev, message]);
44
+ optionsRef.current.onMessage?.(message);
45
+ },
46
+ onError: (err) => {
47
+ setError(err);
48
+ optionsRef.current.onError?.(err);
49
+ },
50
+ onConnect: () => {
51
+ setError(null);
52
+ optionsRef.current.onConnect?.();
53
+ },
54
+ onDisconnect: () => {
55
+ optionsRef.current.onDisconnect?.();
56
+ },
57
+ onInterrupted: () => {
58
+ setPendingAssistantTranscript(null);
59
+ optionsRef.current.onInterrupted?.();
60
+ }
61
+ });
62
+ clientRef.current.onStateChange((state) => {
63
+ setPendingUserTranscript(state.pendingUserTranscript);
64
+ setPendingAssistantTranscript(state.pendingAssistantTranscript);
65
+ });
66
+ }
67
+ const client = clientRef.current;
68
+ useEffect(() => {
69
+ function updateLevels() {
70
+ if (clientRef.current?.audio) {
71
+ setInputLevel(clientRef.current.audio.inputLevel);
72
+ setOutputLevel(clientRef.current.audio.outputLevel);
73
+ }
74
+ animationFrameRef.current = requestAnimationFrame(updateLevels);
75
+ }
76
+ if (status === "connected") {
77
+ updateLevels();
78
+ }
79
+ return () => {
80
+ if (animationFrameRef.current) {
81
+ cancelAnimationFrame(animationFrameRef.current);
82
+ animationFrameRef.current = null;
83
+ }
84
+ };
85
+ }, [status]);
86
+ useEffect(() => {
87
+ return () => {
88
+ clientRef.current?.destroy();
89
+ };
90
+ }, []);
91
+ const connect = useCallback(async () => {
92
+ setError(null);
93
+ setMessages([]);
94
+ setPendingUserTranscript(null);
95
+ setPendingAssistantTranscript(null);
96
+ await client.connect();
97
+ }, [client]);
98
+ const disconnect = useCallback(async () => {
99
+ await client.disconnect();
100
+ }, [client]);
101
+ const startListening = useCallback(() => {
102
+ client.startListening();
103
+ }, [client]);
104
+ const stopListening = useCallback(() => {
105
+ client.stopListening();
106
+ }, [client]);
107
+ const interrupt = useCallback(() => {
108
+ client.interrupt();
109
+ }, [client]);
110
+ const sendText = useCallback(
111
+ (text) => {
112
+ client.sendText(text);
113
+ },
114
+ [client]
115
+ );
116
+ const sendImage = useCallback(
117
+ (imageData, mimeType) => {
118
+ client.sendImage(imageData, mimeType);
119
+ },
120
+ [client]
121
+ );
122
+ const getInputFrequencyData = useCallback(() => {
123
+ return clientRef.current?.audio?.getInputFrequencyData() ?? emptyFrequencyData;
124
+ }, []);
125
+ const getOutputFrequencyData = useCallback(() => {
126
+ return clientRef.current?.audio?.getOutputFrequencyData() ?? emptyFrequencyData;
127
+ }, []);
128
+ const getInputTimeDomainData = useCallback(() => {
129
+ return clientRef.current?.audio?.getInputTimeDomainData() ?? emptyTimeDomainData;
130
+ }, []);
131
+ const getOutputTimeDomainData = useCallback(() => {
132
+ return clientRef.current?.audio?.getOutputTimeDomainData() ?? emptyTimeDomainData;
133
+ }, []);
134
+ const setVADMode = useCallback(
135
+ (newMode) => {
136
+ setVADModeState(newMode);
137
+ },
138
+ []
139
+ );
140
+ return {
141
+ // Connection state
142
+ status,
143
+ error,
144
+ connect,
145
+ disconnect,
146
+ // Conversation state
147
+ mode,
148
+ messages,
149
+ pendingUserTranscript,
150
+ pendingAssistantTranscript,
151
+ // Voice control
152
+ startListening,
153
+ stopListening,
154
+ interrupt,
155
+ // Text input
156
+ sendText,
157
+ // Image input
158
+ sendImage,
159
+ // Audio visualization
160
+ inputLevel,
161
+ outputLevel,
162
+ getInputFrequencyData,
163
+ getOutputFrequencyData,
164
+ getInputTimeDomainData,
165
+ getOutputTimeDomainData,
166
+ // VAD control
167
+ vadMode,
168
+ setVADMode
169
+ };
170
+ }
171
+ export {
172
+ useRealtimeChat
173
+ };
174
+ //# 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 clientRef.current = new RealtimeClient({\n getToken: optionsRef.current.getToken,\n adapter: optionsRef.current.adapter,\n tools: optionsRef.current.tools,\n instructions: optionsRef.current.instructions,\n voice: optionsRef.current.voice,\n autoPlayback: optionsRef.current.autoPlayback,\n autoCapture: optionsRef.current.autoCapture,\n vadMode: optionsRef.current.vadMode,\n outputModalities: optionsRef.current.outputModalities,\n temperature: optionsRef.current.temperature,\n maxOutputTokens: optionsRef.current.maxOutputTokens,\n semanticEagerness: optionsRef.current.semanticEagerness,\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 out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U,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,84 @@
1
+ import { StreamChunk, SummarizationResult } from '@tanstack/ai';
2
+ import { ConnectionAdapter, 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
+ /** Connection adapter for streaming transport (SSE, HTTP stream, custom) */
10
+ connection?: ConnectionAdapter;
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 ConnectionAdapter requests */
16
+ body?: Record<string, any>;
17
+ /**
18
+ * Callback when summarization is complete. Can optionally return a transformed value.
19
+ *
20
+ * - Return a non-null value to transform and store it as the result
21
+ * - Return `null` to keep the previous result unchanged
22
+ * - Return nothing (`void`) to store the raw result as-is
23
+ */
24
+ onResult?: (result: SummarizationResult) => TOutput | null | void;
25
+ /** Callback when an error occurs */
26
+ onError?: (error: Error) => void;
27
+ /** Callback when progress is reported (0-100) */
28
+ onProgress?: (progress: number, message?: string) => void;
29
+ /** Callback for each stream chunk (ConnectionAdapter mode only) */
30
+ onChunk?: (chunk: StreamChunk) => void;
31
+ }
32
+ /**
33
+ * Return type for the useSummarize hook.
34
+ *
35
+ * @template TOutput - The output type (after optional transform)
36
+ */
37
+ export interface UseSummarizeReturn<TOutput = SummarizationResult> {
38
+ /** Trigger summarization */
39
+ generate: (input: SummarizeGenerateInput) => Promise<void>;
40
+ /** The summarization result, or null */
41
+ result: TOutput | null;
42
+ /** Whether summarization is in progress */
43
+ isLoading: boolean;
44
+ /** Current error, if any */
45
+ error: Error | undefined;
46
+ /** Current state of the generation */
47
+ status: GenerationClientState;
48
+ /** Abort the current summarization */
49
+ stop: () => void;
50
+ /** Clear result, error, and return to idle */
51
+ reset: () => void;
52
+ }
53
+ /**
54
+ * React hook for summarizing text using AI models.
55
+ *
56
+ * @example
57
+ * ```tsx
58
+ * import { useSummarize } from '@tanstack/ai-react'
59
+ * import { fetchServerSentEvents } from '@tanstack/ai-client'
60
+ *
61
+ * function Summarizer() {
62
+ * const { generate, result, isLoading } = useSummarize({
63
+ * connection: fetchServerSentEvents('/api/summarize'),
64
+ * })
65
+ *
66
+ * return (
67
+ * <div>
68
+ * <button onClick={() => generate({
69
+ * text: 'Long article text...',
70
+ * style: 'bullet-points',
71
+ * maxLength: 200,
72
+ * })}>
73
+ * Summarize
74
+ * </button>
75
+ * {isLoading && <p>Summarizing...</p>}
76
+ * {result && <p>{result.summary}</p>}
77
+ * </div>
78
+ * )
79
+ * }
80
+ * ```
81
+ */
82
+ export declare function useSummarize<TOnResult extends ((result: SummarizationResult) => any) | undefined = undefined>(options: Omit<UseSummarizeOptions, 'onResult'> & {
83
+ onResult?: TOnResult;
84
+ }): UseSummarizeReturn<InferGenerationOutput<SummarizationResult, TOnResult>>;
@@ -0,0 +1,19 @@
1
+ import { useGeneration } from "./use-generation.js";
2
+ function useSummarize(options) {
3
+ const { generate, result, isLoading, error, status, stop, reset } = useGeneration(
4
+ options
5
+ );
6
+ return {
7
+ generate,
8
+ result,
9
+ isLoading,
10
+ error,
11
+ status,
12
+ stop,
13
+ reset
14
+ };
15
+ }
16
+ export {
17
+ useSummarize
18
+ };
19
+ //# 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 ConnectionAdapter,\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 /** Connection adapter for streaming transport (SSE, HTTP stream, custom) */\n connection?: ConnectionAdapter\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 ConnectionAdapter requests */\n body?: Record<string, any>\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 (ConnectionAdapter 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 { generate, result, isLoading, error, status, stop, reset } =\n useGeneration<SummarizeGenerateInput, SummarizationResult, TOnResult>(\n options,\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":";AA2FO,SAAS,aAId,SAG2E;AAC3E,QAAM,EAAE,UAAU,QAAQ,WAAW,OAAO,QAAQ,MAAM,UACxD;AAAA,IACE;AAAA,EAAA;AAGJ,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEJ;"}
@@ -0,0 +1,89 @@
1
+ import { StreamChunk, TranscriptionResult } from '@tanstack/ai';
2
+ import { ConnectionAdapter, 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
+ /** Connection adapter for streaming transport (SSE, HTTP stream, custom) */
10
+ connection?: ConnectionAdapter;
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 ConnectionAdapter requests */
16
+ body?: Record<string, any>;
17
+ /**
18
+ * Callback when transcription is complete. Can optionally return a transformed value.
19
+ *
20
+ * - Return a non-null value to transform and store it as the result
21
+ * - Return `null` to keep the previous result unchanged
22
+ * - Return nothing (`void`) to store the raw result as-is
23
+ */
24
+ onResult?: (result: TranscriptionResult) => TOutput | null | void;
25
+ /** Callback when an error occurs */
26
+ onError?: (error: Error) => void;
27
+ /** Callback when progress is reported (0-100) */
28
+ onProgress?: (progress: number, message?: string) => void;
29
+ /** Callback for each stream chunk (ConnectionAdapter mode only) */
30
+ onChunk?: (chunk: StreamChunk) => void;
31
+ }
32
+ /**
33
+ * Return type for the useTranscription hook.
34
+ *
35
+ * @template TOutput - The output type (after optional transform)
36
+ */
37
+ export interface UseTranscriptionReturn<TOutput = TranscriptionResult> {
38
+ /** Trigger transcription */
39
+ generate: (input: TranscriptionGenerateInput) => Promise<void>;
40
+ /** The transcription result, or null */
41
+ result: TOutput | null;
42
+ /** Whether transcription is in progress */
43
+ isLoading: boolean;
44
+ /** Current error, if any */
45
+ error: Error | undefined;
46
+ /** Current state of the generation */
47
+ status: GenerationClientState;
48
+ /** Abort the current transcription */
49
+ stop: () => void;
50
+ /** Clear result, error, and return to idle */
51
+ reset: () => void;
52
+ }
53
+ /**
54
+ * React hook for transcribing audio to text using AI models.
55
+ *
56
+ * @example
57
+ * ```tsx
58
+ * import { useTranscription } from '@tanstack/ai-react'
59
+ * import { fetchServerSentEvents } from '@tanstack/ai-client'
60
+ *
61
+ * function Transcriber() {
62
+ * const { generate, result, isLoading } = useTranscription({
63
+ * connection: fetchServerSentEvents('/api/transcribe'),
64
+ * })
65
+ *
66
+ * const handleFile = (e: React.ChangeEvent<HTMLInputElement>) => {
67
+ * const file = e.target.files?.[0]
68
+ * if (file) {
69
+ * const reader = new FileReader()
70
+ * reader.onload = () => {
71
+ * generate({ audio: reader.result as string, language: 'en' })
72
+ * }
73
+ * reader.readAsDataURL(file)
74
+ * }
75
+ * }
76
+ *
77
+ * return (
78
+ * <div>
79
+ * <input type="file" accept="audio/*" onChange={handleFile} />
80
+ * {isLoading && <p>Transcribing...</p>}
81
+ * {result && <p>{result.text}</p>}
82
+ * </div>
83
+ * )
84
+ * }
85
+ * ```
86
+ */
87
+ export declare function useTranscription<TOnResult extends ((result: TranscriptionResult) => any) | undefined = undefined>(options: Omit<UseTranscriptionOptions, 'onResult'> & {
88
+ onResult?: TOnResult;
89
+ }): UseTranscriptionReturn<InferGenerationOutput<TranscriptionResult, TOnResult>>;
@@ -0,0 +1,19 @@
1
+ import { useGeneration } from "./use-generation.js";
2
+ function useTranscription(options) {
3
+ const { generate, result, isLoading, error, status, stop, reset } = useGeneration(
4
+ options
5
+ );
6
+ return {
7
+ generate,
8
+ result,
9
+ isLoading,
10
+ error,
11
+ status,
12
+ stop,
13
+ reset
14
+ };
15
+ }
16
+ export {
17
+ useTranscription
18
+ };
19
+ //# 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 ConnectionAdapter,\n GenerationClientState,\n GenerationFetcher,\n InferGenerationOutput,\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 /** Connection adapter for streaming transport (SSE, HTTP stream, custom) */\n connection?: ConnectionAdapter\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 ConnectionAdapter requests */\n body?: Record<string, any>\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 (ConnectionAdapter 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<\n TOnResult extends ((result: TranscriptionResult) => any) | undefined =\n undefined,\n>(\n options: Omit<UseTranscriptionOptions, 'onResult'> & {\n onResult?: TOnResult\n },\n): UseTranscriptionReturn<\n InferGenerationOutput<TranscriptionResult, TOnResult>\n> {\n const { generate, result, isLoading, error, status, stop, reset } =\n useGeneration<TranscriptionGenerateInput, TranscriptionResult, TOnResult>(\n options,\n )\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":";AAgGO,SAAS,iBAId,SAKA;AACA,QAAM,EAAE,UAAU,QAAQ,WAAW,OAAO,QAAQ,MAAM,UACxD;AAAA,IACE;AAAA,EAAA;AAGJ,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.6.3",
3
+ "version": "0.7.1",
4
4
  "description": "React hooks for TanStack AI",
5
5
  "author": "",
6
6
  "license": "MIT",
@@ -31,12 +31,12 @@
31
31
  "streaming"
32
32
  ],
33
33
  "dependencies": {
34
- "@tanstack/ai-client": "0.5.3"
34
+ "@tanstack/ai-client": "0.7.1"
35
35
  },
36
36
  "peerDependencies": {
37
37
  "@types/react": ">=18.0.0",
38
38
  "react": ">=18.0.0",
39
- "@tanstack/ai": "^0.6.2"
39
+ "@tanstack/ai": "^0.8.0"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@testing-library/react": "^16.3.0",
@@ -45,7 +45,7 @@
45
45
  "jsdom": "^27.2.0",
46
46
  "react": "^19.2.3",
47
47
  "vite": "^7.2.7",
48
- "@tanstack/ai": "0.6.2"
48
+ "@tanstack/ai": "0.8.0"
49
49
  },
50
50
  "scripts": {
51
51
  "clean": "premove ./build ./dist",
package/src/index.ts CHANGED
@@ -1,10 +1,49 @@
1
1
  export { useChat } from './use-chat'
2
+ export { useRealtimeChat } from './use-realtime-chat'
2
3
  export type {
3
4
  UseChatOptions,
4
5
  UseChatReturn,
5
6
  UIMessage,
6
7
  ChatRequestBody,
7
8
  } from './types'
9
+ export type {
10
+ UseRealtimeChatOptions,
11
+ UseRealtimeChatReturn,
12
+ } from './realtime-types'
13
+
14
+ // Generation hooks
15
+ export { useGeneration } from './use-generation'
16
+ export type {
17
+ UseGenerationOptions,
18
+ UseGenerationReturn,
19
+ } from './use-generation'
20
+
21
+ export { useGenerateImage } from './use-generate-image'
22
+ export type {
23
+ UseGenerateImageOptions,
24
+ UseGenerateImageReturn,
25
+ } from './use-generate-image'
26
+
27
+ export { useGenerateSpeech } from './use-generate-speech'
28
+ export type {
29
+ UseGenerateSpeechOptions,
30
+ UseGenerateSpeechReturn,
31
+ } from './use-generate-speech'
32
+
33
+ export { useTranscription } from './use-transcription'
34
+ export type {
35
+ UseTranscriptionOptions,
36
+ UseTranscriptionReturn,
37
+ } from './use-transcription'
38
+
39
+ export { useSummarize } from './use-summarize'
40
+ export type { UseSummarizeOptions, UseSummarizeReturn } from './use-summarize'
41
+
42
+ export { useGenerateVideo } from './use-generate-video'
43
+ export type {
44
+ UseGenerateVideoOptions,
45
+ UseGenerateVideoReturn,
46
+ } from './use-generate-video'
8
47
 
9
48
  // Re-export from ai-client for convenience
10
49
  export {
@@ -15,4 +54,12 @@ export {
15
54
  type ConnectionAdapter,
16
55
  type FetchConnectionOptions,
17
56
  type InferChatMessages,
57
+ type GenerationClientState,
58
+ type ImageGenerateInput,
59
+ type SpeechGenerateInput,
60
+ type TranscriptionGenerateInput,
61
+ type SummarizeGenerateInput,
62
+ type VideoGenerateInput,
63
+ type VideoGenerateResult,
64
+ type VideoStatusInfo,
18
65
  } from '@tanstack/ai-client'