@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.
- package/dist/esm/index.d.ts +19 -0
- package/dist/esm/index.js +29 -0
- package/dist/esm/index.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-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 +104 -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 +91 -0
- package/dist/esm/use-generation.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 +26 -0
- package/dist/esm/use-transcription.js.map +1 -0
- package/package.json +4 -4
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { AudioGenerationResult, StreamChunk } from '@tanstack/ai';
|
|
2
|
+
import { AIDevtoolsDisplayOptions, AudioGenerateInput, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher, InferGenerationOutput } from '@tanstack/ai-client';
|
|
3
|
+
/**
|
|
4
|
+
* Options for the useGenerateAudio hook.
|
|
5
|
+
*
|
|
6
|
+
* @template TOutput - The output type after optional transform (defaults to AudioGenerationResult)
|
|
7
|
+
*/
|
|
8
|
+
export interface UseGenerateAudioOptions<TOutput = AudioGenerationResult> {
|
|
9
|
+
/** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
10
|
+
connection?: ConnectConnectionAdapter;
|
|
11
|
+
/** Direct async function for audio generation */
|
|
12
|
+
fetcher?: GenerationFetcher<AudioGenerateInput, AudioGenerationResult>;
|
|
13
|
+
/** Unique identifier for this generation instance */
|
|
14
|
+
id?: string;
|
|
15
|
+
/** Additional body parameters to send with connect-based adapter requests */
|
|
16
|
+
body?: Record<string, any>;
|
|
17
|
+
/** Display options for TanStack AI Devtools. */
|
|
18
|
+
devtools?: AIDevtoolsDisplayOptions;
|
|
19
|
+
/**
|
|
20
|
+
* Callback when audio is generated. Can optionally return a transformed value.
|
|
21
|
+
*
|
|
22
|
+
* - Return a non-null value to transform and store it as the result
|
|
23
|
+
* - Return `null` to keep the previous result unchanged
|
|
24
|
+
* - Return nothing (`void`) to store the raw result as-is
|
|
25
|
+
*/
|
|
26
|
+
onResult?: (result: AudioGenerationResult) => TOutput | null | void;
|
|
27
|
+
/** Callback when an error occurs */
|
|
28
|
+
onError?: (error: Error) => void;
|
|
29
|
+
/** Callback when progress is reported (0-100) */
|
|
30
|
+
onProgress?: (progress: number, message?: string) => void;
|
|
31
|
+
/** Callback for each stream chunk (connect-based adapter mode only) */
|
|
32
|
+
onChunk?: (chunk: StreamChunk) => void;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Return type for the useGenerateAudio hook.
|
|
36
|
+
*
|
|
37
|
+
* @template TOutput - The output type (after optional transform)
|
|
38
|
+
*/
|
|
39
|
+
export interface UseGenerateAudioReturn<TOutput = AudioGenerationResult> {
|
|
40
|
+
/** Trigger audio generation */
|
|
41
|
+
generate: (input: AudioGenerateInput) => Promise<void>;
|
|
42
|
+
/** The generation result containing audio, or null */
|
|
43
|
+
result: TOutput | null;
|
|
44
|
+
/** Whether generation is in progress */
|
|
45
|
+
isLoading: boolean;
|
|
46
|
+
/** Current error, if any */
|
|
47
|
+
error: Error | undefined;
|
|
48
|
+
/** Current state of the generation */
|
|
49
|
+
status: GenerationClientState;
|
|
50
|
+
/** Abort the current generation */
|
|
51
|
+
stop: () => void;
|
|
52
|
+
/** Clear result, error, and return to idle */
|
|
53
|
+
reset: () => void;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* React hook for generating audio (music, sound effects) using AI models.
|
|
57
|
+
*
|
|
58
|
+
* Supports two transport modes:
|
|
59
|
+
* - **ConnectConnectionAdapter** — Streaming transport (SSE, HTTP stream, custom)
|
|
60
|
+
* - **Fetcher** — Direct async function call
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```tsx
|
|
64
|
+
* import { useGenerateAudio } from '@tanstack/ai-react'
|
|
65
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
66
|
+
*
|
|
67
|
+
* function AudioGenerator() {
|
|
68
|
+
* const { generate, result, isLoading, error, reset } = useGenerateAudio({
|
|
69
|
+
* connection: fetchServerSentEvents('/api/generate/audio'),
|
|
70
|
+
* })
|
|
71
|
+
*
|
|
72
|
+
* return (
|
|
73
|
+
* <div>
|
|
74
|
+
* <button onClick={() => generate({ prompt: 'An upbeat electronic track', duration: 10 })}>
|
|
75
|
+
* Generate
|
|
76
|
+
* </button>
|
|
77
|
+
* {isLoading && <p>Generating...</p>}
|
|
78
|
+
* {error && <p>Error: {error.message}</p>}
|
|
79
|
+
* {result?.audio.url && <audio src={result.audio.url} controls />}
|
|
80
|
+
* </div>
|
|
81
|
+
* )
|
|
82
|
+
* }
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export declare function useGenerateAudio<TOnResult extends ((result: AudioGenerationResult) => any) | undefined = undefined>(options: Omit<UseGenerateAudioOptions, 'onResult'> & {
|
|
86
|
+
onResult?: TOnResult;
|
|
87
|
+
}): UseGenerateAudioReturn<InferGenerationOutput<AudioGenerationResult, TOnResult>>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useGeneration } from "./use-generation.js";
|
|
2
|
+
function useGenerateAudio(options) {
|
|
3
|
+
const devtools = {
|
|
4
|
+
...options.devtools,
|
|
5
|
+
framework: "react",
|
|
6
|
+
hookName: "useGenerateAudio",
|
|
7
|
+
outputKind: "audio"
|
|
8
|
+
};
|
|
9
|
+
const { generate, result, isLoading, error, status, stop, reset } = useGeneration({
|
|
10
|
+
...options,
|
|
11
|
+
devtools
|
|
12
|
+
});
|
|
13
|
+
return {
|
|
14
|
+
generate,
|
|
15
|
+
result,
|
|
16
|
+
isLoading,
|
|
17
|
+
error,
|
|
18
|
+
status,
|
|
19
|
+
stop,
|
|
20
|
+
reset
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export {
|
|
24
|
+
useGenerateAudio
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=use-generate-audio.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-generate-audio.js","sources":["../../src/use-generate-audio.ts"],"sourcesContent":["import { useGeneration } from './use-generation'\nimport type { AudioGenerationResult, StreamChunk } from '@tanstack/ai'\nimport type {\n AIDevtoolsDisplayOptions,\n AudioGenerateInput,\n ConnectConnectionAdapter,\n GenerationClientState,\n GenerationFetcher,\n InferGenerationOutput,\n} from '@tanstack/ai-client'\n\n/**\n * Options for the useGenerateAudio hook.\n *\n * @template TOutput - The output type after optional transform (defaults to AudioGenerationResult)\n */\nexport interface UseGenerateAudioOptions<TOutput = AudioGenerationResult> {\n /** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */\n connection?: ConnectConnectionAdapter\n /** Direct async function for audio generation */\n fetcher?: GenerationFetcher<AudioGenerateInput, AudioGenerationResult>\n /** Unique identifier for this generation instance */\n id?: string\n /** Additional body parameters to send with connect-based adapter requests */\n body?: Record<string, any>\n /** Display options for TanStack AI Devtools. */\n devtools?: AIDevtoolsDisplayOptions\n /**\n * Callback when audio is generated. Can optionally return a transformed value.\n *\n * - Return a non-null value to transform and store it as the result\n * - Return `null` to keep the previous result unchanged\n * - Return nothing (`void`) to store the raw result as-is\n */\n onResult?: (result: AudioGenerationResult) => TOutput | null | void\n /** Callback when an error occurs */\n onError?: (error: Error) => void\n /** Callback when progress is reported (0-100) */\n onProgress?: (progress: number, message?: string) => void\n /** Callback for each stream chunk (connect-based adapter mode only) */\n onChunk?: (chunk: StreamChunk) => void\n}\n\n/**\n * Return type for the useGenerateAudio hook.\n *\n * @template TOutput - The output type (after optional transform)\n */\nexport interface UseGenerateAudioReturn<TOutput = AudioGenerationResult> {\n /** Trigger audio generation */\n generate: (input: AudioGenerateInput) => Promise<void>\n /** The generation result containing audio, or null */\n result: TOutput | null\n /** Whether generation is in progress */\n isLoading: boolean\n /** Current error, if any */\n error: Error | undefined\n /** Current state of the generation */\n status: GenerationClientState\n /** Abort the current generation */\n stop: () => void\n /** Clear result, error, and return to idle */\n reset: () => void\n}\n\n/**\n * React hook for generating audio (music, sound effects) using AI models.\n *\n * Supports two transport modes:\n * - **ConnectConnectionAdapter** — Streaming transport (SSE, HTTP stream, custom)\n * - **Fetcher** — Direct async function call\n *\n * @example\n * ```tsx\n * import { useGenerateAudio } from '@tanstack/ai-react'\n * import { fetchServerSentEvents } from '@tanstack/ai-client'\n *\n * function AudioGenerator() {\n * const { generate, result, isLoading, error, reset } = useGenerateAudio({\n * connection: fetchServerSentEvents('/api/generate/audio'),\n * })\n *\n * return (\n * <div>\n * <button onClick={() => generate({ prompt: 'An upbeat electronic track', duration: 10 })}>\n * Generate\n * </button>\n * {isLoading && <p>Generating...</p>}\n * {error && <p>Error: {error.message}</p>}\n * {result?.audio.url && <audio src={result.audio.url} controls />}\n * </div>\n * )\n * }\n * ```\n */\nexport function useGenerateAudio<\n TOnResult extends ((result: AudioGenerationResult) => any) | undefined =\n undefined,\n>(\n options: Omit<UseGenerateAudioOptions, 'onResult'> & {\n onResult?: TOnResult\n },\n): UseGenerateAudioReturn<\n InferGenerationOutput<AudioGenerationResult, TOnResult>\n> {\n const devtools = {\n ...options.devtools,\n framework: 'react',\n hookName: 'useGenerateAudio',\n outputKind: 'audio' as const,\n }\n const { generate, result, isLoading, error, status, stop, reset } =\n useGeneration<AudioGenerateInput, AudioGenerationResult, TOnResult>({\n ...options,\n devtools,\n })\n\n return {\n generate: generate as (input: AudioGenerateInput) => Promise<void>,\n result,\n isLoading,\n error,\n status,\n stop,\n reset,\n }\n}\n"],"names":[],"mappings":";AA+FO,SAAS,iBAId,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,cAAoE;AAAA,IAClE,GAAG;AAAA,IACH;AAAA,EAAA,CACD;AAEH,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEJ;"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { ImageGenerationResult, StreamChunk } from '@tanstack/ai';
|
|
2
|
+
import { AIDevtoolsDisplayOptions, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher, ImageGenerateInput, InferGenerationOutput } from '@tanstack/ai-client';
|
|
3
|
+
/**
|
|
4
|
+
* Options for the useGenerateImage hook.
|
|
5
|
+
*
|
|
6
|
+
* @template TOutput - The output type after optional transform (defaults to ImageGenerationResult)
|
|
7
|
+
*/
|
|
8
|
+
export interface UseGenerateImageOptions<TOutput = ImageGenerationResult> {
|
|
9
|
+
/** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
10
|
+
connection?: ConnectConnectionAdapter;
|
|
11
|
+
/** Direct async function for image generation */
|
|
12
|
+
fetcher?: GenerationFetcher<ImageGenerateInput, ImageGenerationResult>;
|
|
13
|
+
/** Unique identifier for this generation instance */
|
|
14
|
+
id?: string;
|
|
15
|
+
/** Additional body parameters to send with connect-based adapter requests */
|
|
16
|
+
body?: Record<string, any>;
|
|
17
|
+
/** Display options for TanStack AI Devtools. */
|
|
18
|
+
devtools?: AIDevtoolsDisplayOptions;
|
|
19
|
+
/**
|
|
20
|
+
* Callback when images are generated. Can optionally return a transformed value.
|
|
21
|
+
*
|
|
22
|
+
* - Return a non-null value to transform and store it as the result
|
|
23
|
+
* - Return `null` to keep the previous result unchanged
|
|
24
|
+
* - Return nothing (`void`) to store the raw result as-is
|
|
25
|
+
*/
|
|
26
|
+
onResult?: (result: ImageGenerationResult) => TOutput | null | void;
|
|
27
|
+
/** Callback when an error occurs */
|
|
28
|
+
onError?: (error: Error) => void;
|
|
29
|
+
/** Callback when progress is reported (0-100) */
|
|
30
|
+
onProgress?: (progress: number, message?: string) => void;
|
|
31
|
+
/** Callback for each stream chunk (connect-based adapter mode only) */
|
|
32
|
+
onChunk?: (chunk: StreamChunk) => void;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Return type for the useGenerateImage hook.
|
|
36
|
+
*
|
|
37
|
+
* @template TOutput - The output type (after optional transform)
|
|
38
|
+
*/
|
|
39
|
+
export interface UseGenerateImageReturn<TOutput = ImageGenerationResult> {
|
|
40
|
+
/** Trigger image generation */
|
|
41
|
+
generate: (input: ImageGenerateInput) => Promise<void>;
|
|
42
|
+
/** The generation result containing images, or null */
|
|
43
|
+
result: TOutput | null;
|
|
44
|
+
/** Whether generation is in progress */
|
|
45
|
+
isLoading: boolean;
|
|
46
|
+
/** Current error, if any */
|
|
47
|
+
error: Error | undefined;
|
|
48
|
+
/** Current state of the generation */
|
|
49
|
+
status: GenerationClientState;
|
|
50
|
+
/** Abort the current generation */
|
|
51
|
+
stop: () => void;
|
|
52
|
+
/** Clear result, error, and return to idle */
|
|
53
|
+
reset: () => void;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* React hook for generating images using AI models.
|
|
57
|
+
*
|
|
58
|
+
* Supports two transport modes:
|
|
59
|
+
* - **ConnectConnectionAdapter** — Streaming transport (SSE, HTTP stream, custom)
|
|
60
|
+
* - **Fetcher** — Direct async function call
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```tsx
|
|
64
|
+
* import { useGenerateImage } from '@tanstack/ai-react'
|
|
65
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
66
|
+
*
|
|
67
|
+
* function ImageGenerator() {
|
|
68
|
+
* const { generate, result, isLoading, error, reset } = useGenerateImage({
|
|
69
|
+
* connection: fetchServerSentEvents('/api/generate/image'),
|
|
70
|
+
* })
|
|
71
|
+
*
|
|
72
|
+
* return (
|
|
73
|
+
* <div>
|
|
74
|
+
* <button onClick={() => generate({ prompt: 'A sunset over mountains' })}>
|
|
75
|
+
* Generate
|
|
76
|
+
* </button>
|
|
77
|
+
* {isLoading && <p>Generating...</p>}
|
|
78
|
+
* {error && <p>Error: {error.message}</p>}
|
|
79
|
+
* {result?.images.map((img, i) => (
|
|
80
|
+
* <img key={i} src={img.url || `data:image/png;base64,${img.b64Json}`} />
|
|
81
|
+
* ))}
|
|
82
|
+
* </div>
|
|
83
|
+
* )
|
|
84
|
+
* }
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export declare function useGenerateImage<TOnResult extends ((result: ImageGenerationResult) => any) | undefined = undefined>(options: Omit<UseGenerateImageOptions, 'onResult'> & {
|
|
88
|
+
onResult?: TOnResult;
|
|
89
|
+
}): UseGenerateImageReturn<InferGenerationOutput<ImageGenerationResult, TOnResult>>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useGeneration } from "./use-generation.js";
|
|
2
|
+
function useGenerateImage(options) {
|
|
3
|
+
const devtools = {
|
|
4
|
+
...options.devtools,
|
|
5
|
+
framework: "react",
|
|
6
|
+
hookName: "useGenerateImage",
|
|
7
|
+
outputKind: "image"
|
|
8
|
+
};
|
|
9
|
+
const { generate, result, isLoading, error, status, stop, reset } = useGeneration({
|
|
10
|
+
...options,
|
|
11
|
+
devtools
|
|
12
|
+
});
|
|
13
|
+
return {
|
|
14
|
+
generate,
|
|
15
|
+
result,
|
|
16
|
+
isLoading,
|
|
17
|
+
error,
|
|
18
|
+
status,
|
|
19
|
+
stop,
|
|
20
|
+
reset
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export {
|
|
24
|
+
useGenerateImage
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=use-generate-image.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-generate-image.js","sources":["../../src/use-generate-image.ts"],"sourcesContent":["import { useGeneration } from './use-generation'\nimport type { ImageGenerationResult, StreamChunk } from '@tanstack/ai'\nimport type {\n AIDevtoolsDisplayOptions,\n ConnectConnectionAdapter,\n GenerationClientState,\n GenerationFetcher,\n ImageGenerateInput,\n InferGenerationOutput,\n} from '@tanstack/ai-client'\n\n/**\n * Options for the useGenerateImage hook.\n *\n * @template TOutput - The output type after optional transform (defaults to ImageGenerationResult)\n */\nexport interface UseGenerateImageOptions<TOutput = ImageGenerationResult> {\n /** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */\n connection?: ConnectConnectionAdapter\n /** Direct async function for image generation */\n fetcher?: GenerationFetcher<ImageGenerateInput, ImageGenerationResult>\n /** Unique identifier for this generation instance */\n id?: string\n /** Additional body parameters to send with connect-based adapter requests */\n body?: Record<string, any>\n /** Display options for TanStack AI Devtools. */\n devtools?: AIDevtoolsDisplayOptions\n /**\n * Callback when images are generated. Can optionally return a transformed value.\n *\n * - Return a non-null value to transform and store it as the result\n * - Return `null` to keep the previous result unchanged\n * - Return nothing (`void`) to store the raw result as-is\n */\n onResult?: (result: ImageGenerationResult) => TOutput | null | void\n /** Callback when an error occurs */\n onError?: (error: Error) => void\n /** Callback when progress is reported (0-100) */\n onProgress?: (progress: number, message?: string) => void\n /** Callback for each stream chunk (connect-based adapter mode only) */\n onChunk?: (chunk: StreamChunk) => void\n}\n\n/**\n * Return type for the useGenerateImage hook.\n *\n * @template TOutput - The output type (after optional transform)\n */\nexport interface UseGenerateImageReturn<TOutput = ImageGenerationResult> {\n /** Trigger image generation */\n generate: (input: ImageGenerateInput) => Promise<void>\n /** The generation result containing images, or null */\n result: TOutput | null\n /** Whether generation is in progress */\n isLoading: boolean\n /** Current error, if any */\n error: Error | undefined\n /** Current state of the generation */\n status: GenerationClientState\n /** Abort the current generation */\n stop: () => void\n /** Clear result, error, and return to idle */\n reset: () => void\n}\n\n/**\n * React hook for generating images using AI models.\n *\n * Supports two transport modes:\n * - **ConnectConnectionAdapter** — Streaming transport (SSE, HTTP stream, custom)\n * - **Fetcher** — Direct async function call\n *\n * @example\n * ```tsx\n * import { useGenerateImage } from '@tanstack/ai-react'\n * import { fetchServerSentEvents } from '@tanstack/ai-client'\n *\n * function ImageGenerator() {\n * const { generate, result, isLoading, error, reset } = useGenerateImage({\n * connection: fetchServerSentEvents('/api/generate/image'),\n * })\n *\n * return (\n * <div>\n * <button onClick={() => generate({ prompt: 'A sunset over mountains' })}>\n * Generate\n * </button>\n * {isLoading && <p>Generating...</p>}\n * {error && <p>Error: {error.message}</p>}\n * {result?.images.map((img, i) => (\n * <img key={i} src={img.url || `data:image/png;base64,${img.b64Json}`} />\n * ))}\n * </div>\n * )\n * }\n * ```\n */\nexport function useGenerateImage<\n TOnResult extends ((result: ImageGenerationResult) => any) | undefined =\n undefined,\n>(\n options: Omit<UseGenerateImageOptions, 'onResult'> & {\n onResult?: TOnResult\n },\n): UseGenerateImageReturn<\n InferGenerationOutput<ImageGenerationResult, TOnResult>\n> {\n const devtools = {\n ...options.devtools,\n framework: 'react',\n hookName: 'useGenerateImage',\n outputKind: 'image' as const,\n }\n const { generate, result, isLoading, error, status, stop, reset } =\n useGeneration<ImageGenerateInput, ImageGenerationResult, TOnResult>({\n ...options,\n devtools,\n })\n\n return {\n generate: generate as (input: ImageGenerateInput) => Promise<void>,\n result,\n isLoading,\n error,\n status,\n stop,\n reset,\n }\n}\n"],"names":[],"mappings":";AAiGO,SAAS,iBAId,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,cAAoE;AAAA,IAClE,GAAG;AAAA,IACH;AAAA,EAAA,CACD;AAEH,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEJ;"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { StreamChunk, TTSResult } from '@tanstack/ai';
|
|
2
|
+
import { AIDevtoolsDisplayOptions, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher, InferGenerationOutput, SpeechGenerateInput } from '@tanstack/ai-client';
|
|
3
|
+
/**
|
|
4
|
+
* Options for the useGenerateSpeech hook.
|
|
5
|
+
*
|
|
6
|
+
* @template TOutput - The output type after optional transform (defaults to TTSResult)
|
|
7
|
+
*/
|
|
8
|
+
export interface UseGenerateSpeechOptions<TOutput = TTSResult> {
|
|
9
|
+
/** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
10
|
+
connection?: ConnectConnectionAdapter;
|
|
11
|
+
/** Direct async function for speech generation */
|
|
12
|
+
fetcher?: GenerationFetcher<SpeechGenerateInput, TTSResult>;
|
|
13
|
+
/** Unique identifier for this generation instance */
|
|
14
|
+
id?: string;
|
|
15
|
+
/** Additional body parameters to send with connect-based adapter requests */
|
|
16
|
+
body?: Record<string, any>;
|
|
17
|
+
/** Display options for TanStack AI Devtools. */
|
|
18
|
+
devtools?: AIDevtoolsDisplayOptions;
|
|
19
|
+
/**
|
|
20
|
+
* Callback when speech is generated. Can optionally return a transformed value.
|
|
21
|
+
*
|
|
22
|
+
* - Return a non-null value to transform and store it as the result
|
|
23
|
+
* - Return `null` to keep the previous result unchanged
|
|
24
|
+
* - Return nothing (`void`) to store the raw result as-is
|
|
25
|
+
*/
|
|
26
|
+
onResult?: (result: TTSResult) => TOutput | null | void;
|
|
27
|
+
/** Callback when an error occurs */
|
|
28
|
+
onError?: (error: Error) => void;
|
|
29
|
+
/** Callback when progress is reported (0-100) */
|
|
30
|
+
onProgress?: (progress: number, message?: string) => void;
|
|
31
|
+
/** Callback for each stream chunk (connect-based adapter mode only) */
|
|
32
|
+
onChunk?: (chunk: StreamChunk) => void;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Return type for the useGenerateSpeech hook.
|
|
36
|
+
*
|
|
37
|
+
* @template TOutput - The output type (after optional transform)
|
|
38
|
+
*/
|
|
39
|
+
export interface UseGenerateSpeechReturn<TOutput = TTSResult> {
|
|
40
|
+
/** Trigger speech generation */
|
|
41
|
+
generate: (input: SpeechGenerateInput) => Promise<void>;
|
|
42
|
+
/** The TTS result containing audio data, or null */
|
|
43
|
+
result: TOutput | null;
|
|
44
|
+
/** Whether generation is in progress */
|
|
45
|
+
isLoading: boolean;
|
|
46
|
+
/** Current error, if any */
|
|
47
|
+
error: Error | undefined;
|
|
48
|
+
/** Current state of the generation */
|
|
49
|
+
status: GenerationClientState;
|
|
50
|
+
/** Abort the current generation */
|
|
51
|
+
stop: () => void;
|
|
52
|
+
/** Clear result, error, and return to idle */
|
|
53
|
+
reset: () => void;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* React hook for generating speech (text-to-speech) using AI models.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```tsx
|
|
60
|
+
* import { useGenerateSpeech } from '@tanstack/ai-react'
|
|
61
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
62
|
+
*
|
|
63
|
+
* function SpeechGenerator() {
|
|
64
|
+
* const { generate, result, isLoading } = useGenerateSpeech({
|
|
65
|
+
* connection: fetchServerSentEvents('/api/generate/speech'),
|
|
66
|
+
* })
|
|
67
|
+
*
|
|
68
|
+
* return (
|
|
69
|
+
* <div>
|
|
70
|
+
* <button onClick={() => generate({ text: 'Hello world', voice: 'alloy' })}>
|
|
71
|
+
* Generate Speech
|
|
72
|
+
* </button>
|
|
73
|
+
* {result && (
|
|
74
|
+
* <audio src={`data:audio/${result.format};base64,${result.audio}`} controls />
|
|
75
|
+
* )}
|
|
76
|
+
* </div>
|
|
77
|
+
* )
|
|
78
|
+
* }
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export declare function useGenerateSpeech<TOnResult extends ((result: TTSResult) => any) | undefined = undefined>(options: Omit<UseGenerateSpeechOptions, 'onResult'> & {
|
|
82
|
+
onResult?: TOnResult;
|
|
83
|
+
}): UseGenerateSpeechReturn<InferGenerationOutput<TTSResult, TOnResult>>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useGeneration } from "./use-generation.js";
|
|
2
|
+
function useGenerateSpeech(options) {
|
|
3
|
+
const devtools = {
|
|
4
|
+
...options.devtools,
|
|
5
|
+
framework: "react",
|
|
6
|
+
hookName: "useGenerateSpeech",
|
|
7
|
+
outputKind: "audio"
|
|
8
|
+
};
|
|
9
|
+
const { generate, result, isLoading, error, status, stop, reset } = useGeneration({
|
|
10
|
+
...options,
|
|
11
|
+
devtools
|
|
12
|
+
});
|
|
13
|
+
return {
|
|
14
|
+
generate,
|
|
15
|
+
result,
|
|
16
|
+
isLoading,
|
|
17
|
+
error,
|
|
18
|
+
status,
|
|
19
|
+
stop,
|
|
20
|
+
reset
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export {
|
|
24
|
+
useGenerateSpeech
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=use-generate-speech.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-generate-speech.js","sources":["../../src/use-generate-speech.ts"],"sourcesContent":["import { useGeneration } from './use-generation'\nimport type { StreamChunk, TTSResult } from '@tanstack/ai'\nimport type {\n AIDevtoolsDisplayOptions,\n ConnectConnectionAdapter,\n GenerationClientState,\n GenerationFetcher,\n InferGenerationOutput,\n SpeechGenerateInput,\n} from '@tanstack/ai-client'\n\n/**\n * Options for the useGenerateSpeech hook.\n *\n * @template TOutput - The output type after optional transform (defaults to TTSResult)\n */\nexport interface UseGenerateSpeechOptions<TOutput = TTSResult> {\n /** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */\n connection?: ConnectConnectionAdapter\n /** Direct async function for speech generation */\n fetcher?: GenerationFetcher<SpeechGenerateInput, TTSResult>\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 speech is generated. Can optionally return a transformed value.\n *\n * - Return a non-null value to transform and store it as the result\n * - Return `null` to keep the previous result unchanged\n * - Return nothing (`void`) to store the raw result as-is\n */\n onResult?: (result: TTSResult) => 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 useGenerateSpeech hook.\n *\n * @template TOutput - The output type (after optional transform)\n */\nexport interface UseGenerateSpeechReturn<TOutput = TTSResult> {\n /** Trigger speech generation */\n generate: (input: SpeechGenerateInput) => Promise<void>\n /** The TTS result containing audio data, or null */\n result: TOutput | null\n /** Whether generation is in progress */\n isLoading: boolean\n /** Current error, if any */\n error: Error | undefined\n /** Current state of the generation */\n status: GenerationClientState\n /** Abort the current generation */\n stop: () => void\n /** Clear result, error, and return to idle */\n reset: () => void\n}\n\n/**\n * React hook for generating speech (text-to-speech) using AI models.\n *\n * @example\n * ```tsx\n * import { useGenerateSpeech } from '@tanstack/ai-react'\n * import { fetchServerSentEvents } from '@tanstack/ai-client'\n *\n * function SpeechGenerator() {\n * const { generate, result, isLoading } = useGenerateSpeech({\n * connection: fetchServerSentEvents('/api/generate/speech'),\n * })\n *\n * return (\n * <div>\n * <button onClick={() => generate({ text: 'Hello world', voice: 'alloy' })}>\n * Generate Speech\n * </button>\n * {result && (\n * <audio src={`data:audio/${result.format};base64,${result.audio}`} controls />\n * )}\n * </div>\n * )\n * }\n * ```\n */\nexport function useGenerateSpeech<\n TOnResult extends ((result: TTSResult) => any) | undefined = undefined,\n>(\n options: Omit<UseGenerateSpeechOptions, 'onResult'> & {\n onResult?: TOnResult\n },\n): UseGenerateSpeechReturn<InferGenerationOutput<TTSResult, TOnResult>> {\n const devtools = {\n ...options.devtools,\n framework: 'react',\n hookName: 'useGenerateSpeech',\n outputKind: 'audio' as const,\n }\n const { generate, result, isLoading, error, status, stop, reset } =\n useGeneration<SpeechGenerateInput, TTSResult, TOnResult>({\n ...options,\n devtools,\n })\n\n return {\n generate: generate as (input: SpeechGenerateInput) => Promise<void>,\n result,\n isLoading,\n error,\n status,\n stop,\n reset,\n }\n}\n"],"names":[],"mappings":";AA2FO,SAAS,kBAGd,SAGsE;AACtE,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,cAAyD;AAAA,IACvD,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,94 @@
|
|
|
1
|
+
import { StreamChunk } from '@tanstack/ai';
|
|
2
|
+
import { AIDevtoolsDisplayOptions, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher, InferGenerationOutput, VideoGenerateInput, VideoGenerateResult, VideoStatusInfo } from '@tanstack/ai-client';
|
|
3
|
+
/**
|
|
4
|
+
* Options for the useGenerateVideo hook.
|
|
5
|
+
*/
|
|
6
|
+
export interface UseGenerateVideoOptions<TOutput = VideoGenerateResult> {
|
|
7
|
+
/** Connect-based adapter for streaming transport (server handles polling) */
|
|
8
|
+
connection?: ConnectConnectionAdapter;
|
|
9
|
+
/** Direct async function that returns a completed video result */
|
|
10
|
+
fetcher?: GenerationFetcher<VideoGenerateInput, VideoGenerateResult>;
|
|
11
|
+
/** Unique identifier for this generation instance */
|
|
12
|
+
id?: string;
|
|
13
|
+
/** Additional body parameters to send with connect-based adapter requests */
|
|
14
|
+
body?: Record<string, any>;
|
|
15
|
+
/** Display options for TanStack AI Devtools. */
|
|
16
|
+
devtools?: AIDevtoolsDisplayOptions;
|
|
17
|
+
/**
|
|
18
|
+
* Callback when video generation completes. 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: VideoGenerateResult) => 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 when a video job is created */
|
|
30
|
+
onJobCreated?: (jobId: string) => void;
|
|
31
|
+
/** Callback on each status update */
|
|
32
|
+
onStatusUpdate?: (status: VideoStatusInfo) => void;
|
|
33
|
+
/** Callback for each stream chunk (connect-based adapter mode only) */
|
|
34
|
+
onChunk?: (chunk: StreamChunk) => void;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Return type for the useGenerateVideo hook.
|
|
38
|
+
*
|
|
39
|
+
* @template TOutput - The output type (after optional transform)
|
|
40
|
+
*/
|
|
41
|
+
export interface UseGenerateVideoReturn<TOutput = VideoGenerateResult> {
|
|
42
|
+
/** Trigger video generation */
|
|
43
|
+
generate: (input: VideoGenerateInput) => Promise<void>;
|
|
44
|
+
/** The final video result (with URL), or null */
|
|
45
|
+
result: TOutput | null;
|
|
46
|
+
/** The current job ID, or null */
|
|
47
|
+
jobId: string | null;
|
|
48
|
+
/** Current video generation status info, or null */
|
|
49
|
+
videoStatus: VideoStatusInfo | null;
|
|
50
|
+
/** Whether generation/polling is in progress */
|
|
51
|
+
isLoading: boolean;
|
|
52
|
+
/** Current error, if any */
|
|
53
|
+
error: Error | undefined;
|
|
54
|
+
/** Current state of the generation */
|
|
55
|
+
status: GenerationClientState;
|
|
56
|
+
/** Abort the current generation/polling */
|
|
57
|
+
stop: () => void;
|
|
58
|
+
/** Clear all state and return to idle */
|
|
59
|
+
reset: () => void;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* React hook for generating videos using AI models.
|
|
63
|
+
*
|
|
64
|
+
* Video generation is asynchronous: a job is created, then polled for status
|
|
65
|
+
* until completion. This hook handles the full lifecycle.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```tsx
|
|
69
|
+
* import { useGenerateVideo } from '@tanstack/ai-react'
|
|
70
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
71
|
+
*
|
|
72
|
+
* function VideoGenerator() {
|
|
73
|
+
* const { generate, result, videoStatus, isLoading } = useGenerateVideo({
|
|
74
|
+
* connection: fetchServerSentEvents('/api/generate/video'),
|
|
75
|
+
* onStatusUpdate: (status) => console.log(`Progress: ${status.progress}%`),
|
|
76
|
+
* })
|
|
77
|
+
*
|
|
78
|
+
* return (
|
|
79
|
+
* <div>
|
|
80
|
+
* <button onClick={() => generate({ prompt: 'A flying car over a city' })}>
|
|
81
|
+
* Generate Video
|
|
82
|
+
* </button>
|
|
83
|
+
* {isLoading && videoStatus && (
|
|
84
|
+
* <p>Status: {videoStatus.status} ({videoStatus.progress}%)</p>
|
|
85
|
+
* )}
|
|
86
|
+
* {result && <video src={result.url} controls />}
|
|
87
|
+
* </div>
|
|
88
|
+
* )
|
|
89
|
+
* }
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
export declare function useGenerateVideo<TOnResult extends ((result: VideoGenerateResult) => any) | undefined = undefined>(options: Omit<UseGenerateVideoOptions, 'onResult'> & {
|
|
93
|
+
onResult?: TOnResult;
|
|
94
|
+
}): UseGenerateVideoReturn<InferGenerationOutput<VideoGenerateResult, TOnResult>>;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { VideoGenerationClient } from "@tanstack/ai-client";
|
|
2
|
+
import { createVideoDevtoolsBridge } from "@tanstack/ai-client/devtools";
|
|
3
|
+
import { useId, useState, useRef, useMemo, useEffect, useCallback } from "react";
|
|
4
|
+
function useGenerateVideo(options) {
|
|
5
|
+
const hookId = useId();
|
|
6
|
+
const clientId = options.id || hookId;
|
|
7
|
+
const [result, setResult] = useState(null);
|
|
8
|
+
const [jobId, setJobId] = useState(null);
|
|
9
|
+
const [videoStatus, setVideoStatus] = useState(null);
|
|
10
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
11
|
+
const [error, setError] = useState(void 0);
|
|
12
|
+
const [status, setStatus] = useState("idle");
|
|
13
|
+
const optionsRef = useRef(options);
|
|
14
|
+
optionsRef.current = options;
|
|
15
|
+
const client = useMemo(() => {
|
|
16
|
+
const opts = optionsRef.current;
|
|
17
|
+
const baseOptions = {
|
|
18
|
+
id: clientId,
|
|
19
|
+
body: opts.body,
|
|
20
|
+
devtoolsBridgeFactory: createVideoDevtoolsBridge,
|
|
21
|
+
devtools: {
|
|
22
|
+
...opts.devtools,
|
|
23
|
+
framework: "react",
|
|
24
|
+
hookName: "useGenerateVideo",
|
|
25
|
+
outputKind: "video"
|
|
26
|
+
},
|
|
27
|
+
onResult: (r) => optionsRef.current.onResult?.(r),
|
|
28
|
+
onError: (e) => {
|
|
29
|
+
optionsRef.current.onError?.(e);
|
|
30
|
+
},
|
|
31
|
+
onProgress: (p, m) => {
|
|
32
|
+
optionsRef.current.onProgress?.(p, m);
|
|
33
|
+
},
|
|
34
|
+
onChunk: (c) => {
|
|
35
|
+
optionsRef.current.onChunk?.(c);
|
|
36
|
+
},
|
|
37
|
+
onJobCreated: (id) => {
|
|
38
|
+
optionsRef.current.onJobCreated?.(id);
|
|
39
|
+
},
|
|
40
|
+
onStatusUpdate: (s) => {
|
|
41
|
+
optionsRef.current.onStatusUpdate?.(s);
|
|
42
|
+
},
|
|
43
|
+
onResultChange: setResult,
|
|
44
|
+
onLoadingChange: setIsLoading,
|
|
45
|
+
onErrorChange: setError,
|
|
46
|
+
onStatusChange: setStatus,
|
|
47
|
+
onJobIdChange: setJobId,
|
|
48
|
+
onVideoStatusChange: setVideoStatus
|
|
49
|
+
};
|
|
50
|
+
if (opts.connection) {
|
|
51
|
+
return new VideoGenerationClient({
|
|
52
|
+
...baseOptions,
|
|
53
|
+
connection: opts.connection
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
if (opts.fetcher) {
|
|
57
|
+
return new VideoGenerationClient({
|
|
58
|
+
...baseOptions,
|
|
59
|
+
fetcher: opts.fetcher
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
throw new Error(
|
|
63
|
+
"useGenerateVideo requires either a connection or fetcher option"
|
|
64
|
+
);
|
|
65
|
+
}, [clientId]);
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
client.updateOptions({
|
|
68
|
+
...options.body !== void 0 && { body: options.body }
|
|
69
|
+
});
|
|
70
|
+
}, [client, options.body]);
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
client.mountDevtools();
|
|
73
|
+
return () => {
|
|
74
|
+
client.dispose();
|
|
75
|
+
};
|
|
76
|
+
}, [client]);
|
|
77
|
+
const generate = useCallback(
|
|
78
|
+
async (input) => {
|
|
79
|
+
await client.generate(input);
|
|
80
|
+
},
|
|
81
|
+
[client]
|
|
82
|
+
);
|
|
83
|
+
const stop = useCallback(() => {
|
|
84
|
+
client.stop();
|
|
85
|
+
}, [client]);
|
|
86
|
+
const reset = useCallback(() => {
|
|
87
|
+
client.reset();
|
|
88
|
+
}, [client]);
|
|
89
|
+
return {
|
|
90
|
+
generate,
|
|
91
|
+
result,
|
|
92
|
+
jobId,
|
|
93
|
+
videoStatus,
|
|
94
|
+
isLoading,
|
|
95
|
+
error,
|
|
96
|
+
status,
|
|
97
|
+
stop,
|
|
98
|
+
reset
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
export {
|
|
102
|
+
useGenerateVideo
|
|
103
|
+
};
|
|
104
|
+
//# sourceMappingURL=use-generate-video.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-generate-video.js","sources":["../../src/use-generate-video.ts"],"sourcesContent":["import { VideoGenerationClient } from '@tanstack/ai-client'\nimport { createVideoDevtoolsBridge } 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 GenerationClientState,\n GenerationFetcher,\n InferGenerationOutput,\n VideoGenerateInput,\n VideoGenerateResult,\n VideoStatusInfo,\n} from '@tanstack/ai-client'\n\n/**\n * Options for the useGenerateVideo hook.\n */\nexport interface UseGenerateVideoOptions<TOutput = VideoGenerateResult> {\n /** Connect-based adapter for streaming transport (server handles polling) */\n connection?: ConnectConnectionAdapter\n /** Direct async function that returns a completed video result */\n fetcher?: GenerationFetcher<VideoGenerateInput, VideoGenerateResult>\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 video generation completes. 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: VideoGenerateResult) => 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 when a video job is created */\n onJobCreated?: (jobId: string) => void\n /** Callback on each status update */\n onStatusUpdate?: (status: VideoStatusInfo) => 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 useGenerateVideo hook.\n *\n * @template TOutput - The output type (after optional transform)\n */\nexport interface UseGenerateVideoReturn<TOutput = VideoGenerateResult> {\n /** Trigger video generation */\n generate: (input: VideoGenerateInput) => Promise<void>\n /** The final video result (with URL), or null */\n result: TOutput | null\n /** The current job ID, or null */\n jobId: string | null\n /** Current video generation status info, or null */\n videoStatus: VideoStatusInfo | null\n /** Whether generation/polling is in progress */\n isLoading: boolean\n /** Current error, if any */\n error: Error | undefined\n /** Current state of the generation */\n status: GenerationClientState\n /** Abort the current generation/polling */\n stop: () => void\n /** Clear all state and return to idle */\n reset: () => void\n}\n\n/**\n * React hook for generating videos using AI models.\n *\n * Video generation is asynchronous: a job is created, then polled for status\n * until completion. This hook handles the full lifecycle.\n *\n * @example\n * ```tsx\n * import { useGenerateVideo } from '@tanstack/ai-react'\n * import { fetchServerSentEvents } from '@tanstack/ai-client'\n *\n * function VideoGenerator() {\n * const { generate, result, videoStatus, isLoading } = useGenerateVideo({\n * connection: fetchServerSentEvents('/api/generate/video'),\n * onStatusUpdate: (status) => console.log(`Progress: ${status.progress}%`),\n * })\n *\n * return (\n * <div>\n * <button onClick={() => generate({ prompt: 'A flying car over a city' })}>\n * Generate Video\n * </button>\n * {isLoading && videoStatus && (\n * <p>Status: {videoStatus.status} ({videoStatus.progress}%)</p>\n * )}\n * {result && <video src={result.url} controls />}\n * </div>\n * )\n * }\n * ```\n */\nexport function useGenerateVideo<\n TOnResult extends ((result: VideoGenerateResult) => any) | undefined =\n undefined,\n>(\n options: Omit<UseGenerateVideoOptions, 'onResult'> & {\n onResult?: TOnResult\n },\n): UseGenerateVideoReturn<\n InferGenerationOutput<VideoGenerateResult, TOnResult>\n> {\n type TOutput = InferGenerationOutput<VideoGenerateResult, TOnResult>\n const hookId = useId()\n const clientId = options.id || hookId\n\n const [result, setResult] = useState<TOutput | null>(null)\n const [jobId, setJobId] = useState<string | null>(null)\n const [videoStatus, setVideoStatus] = useState<VideoStatusInfo | 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 // Optional callbacks are wrapped in non-returning bodies so\n // `?.()`'s implicit `undefined` doesn't widen the function\n // return type (which `exactOptionalPropertyTypes` rejects\n // against the strict-optional target).\n const baseOptions = {\n id: clientId,\n body: opts.body,\n devtoolsBridgeFactory: createVideoDevtoolsBridge,\n devtools: {\n ...opts.devtools,\n framework: 'react',\n hookName: 'useGenerateVideo',\n outputKind: 'video' as const,\n },\n onResult: (r: VideoGenerateResult) => 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 onJobCreated: (id: string) => {\n optionsRef.current.onJobCreated?.(id)\n },\n onStatusUpdate: (s: VideoStatusInfo) => {\n optionsRef.current.onStatusUpdate?.(s)\n },\n onResultChange: setResult,\n onLoadingChange: setIsLoading,\n onErrorChange: setError,\n onStatusChange: setStatus,\n onJobIdChange: setJobId,\n onVideoStatusChange: setVideoStatus,\n }\n\n if (opts.connection) {\n return new VideoGenerationClient<TOutput>({\n ...baseOptions,\n connection: opts.connection,\n })\n }\n\n if (opts.fetcher) {\n return new VideoGenerationClient<TOutput>({\n ...baseOptions,\n fetcher: opts.fetcher,\n })\n }\n\n throw new Error(\n 'useGenerateVideo 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: VideoGenerateInput) => {\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,\n result,\n jobId,\n videoStatus,\n isLoading,\n error,\n status,\n stop,\n reset,\n }\n}\n"],"names":[],"mappings":";;;AA0GO,SAAS,iBAId,SAKA;AAEA,QAAM,SAAS,MAAA;AACf,QAAM,WAAW,QAAQ,MAAM;AAE/B,QAAM,CAAC,QAAQ,SAAS,IAAI,SAAyB,IAAI;AACzD,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAwB,IAAI;AACtD,QAAM,CAAC,aAAa,cAAc,IAAI,SAAiC,IAAI;AAC3E,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;AAOxB,UAAM,cAAc;AAAA,MAClB,IAAI;AAAA,MACJ,MAAM,KAAK;AAAA,MACX,uBAAuB;AAAA,MACvB,UAAU;AAAA,QACR,GAAG,KAAK;AAAA,QACR,WAAW;AAAA,QACX,UAAU;AAAA,QACV,YAAY;AAAA,MAAA;AAAA,MAEd,UAAU,CAAC,MAA2B,WAAW,QAAQ,WAAW,CAAC;AAAA,MACrE,SAAS,CAAC,MAAa;AACrB,mBAAW,QAAQ,UAAU,CAAC;AAAA,MAChC;AAAA,MACA,YAAY,CAAC,GAAW,MAAe;AACrC,mBAAW,QAAQ,aAAa,GAAG,CAAC;AAAA,MACtC;AAAA,MACA,SAAS,CAAC,MAAmB;AAC3B,mBAAW,QAAQ,UAAU,CAAC;AAAA,MAChC;AAAA,MACA,cAAc,CAAC,OAAe;AAC5B,mBAAW,QAAQ,eAAe,EAAE;AAAA,MACtC;AAAA,MACA,gBAAgB,CAAC,MAAuB;AACtC,mBAAW,QAAQ,iBAAiB,CAAC;AAAA,MACvC;AAAA,MACA,gBAAgB;AAAA,MAChB,iBAAiB;AAAA,MACjB,eAAe;AAAA,MACf,gBAAgB;AAAA,MAChB,eAAe;AAAA,MACf,qBAAqB;AAAA,IAAA;AAGvB,QAAI,KAAK,YAAY;AACnB,aAAO,IAAI,sBAA+B;AAAA,QACxC,GAAG;AAAA,QACH,YAAY,KAAK;AAAA,MAAA,CAClB;AAAA,IACH;AAEA,QAAI,KAAK,SAAS;AAChB,aAAO,IAAI,sBAA+B;AAAA,QACxC,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;AAEd,WAAO,cAAc;AAAA,MACnB,GAAI,QAAQ,SAAS,UAAa,EAAE,MAAM,QAAQ,KAAA;AAAA,IAAK,CACxD;AAAA,EACH,GAAG,CAAC,QAAQ,QAAQ,IAAI,CAAC;AAGzB,YAAU,MAAM;AACd,WAAO,cAAA;AAEP,WAAO,MAAM;AACX,aAAO,QAAA;AAAA,IACT;AAAA,EACF,GAAG,CAAC,MAAM,CAAC;AAEX,QAAM,WAAW;AAAA,IACf,OAAO,UAA8B;AACnC,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,IACA;AAAA,IACA;AAAA,EAAA;AAEJ;"}
|