@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.
- package/dist/esm/index.d.ts +15 -1
- package/dist/esm/index.js +15 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/realtime-types.d.ts +109 -0
- package/dist/esm/use-generate-image.d.ts +87 -0
- package/dist/esm/use-generate-image.js +17 -0
- package/dist/esm/use-generate-image.js.map +1 -0
- package/dist/esm/use-generate-speech.d.ts +81 -0
- package/dist/esm/use-generate-speech.js +17 -0
- package/dist/esm/use-generate-speech.js.map +1 -0
- package/dist/esm/use-generate-video.d.ts +92 -0
- package/dist/esm/use-generate-video.js +83 -0
- package/dist/esm/use-generate-video.js.map +1 -0
- package/dist/esm/use-generation.d.ts +78 -0
- package/dist/esm/use-generation.js +75 -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 +174 -0
- package/dist/esm/use-realtime-chat.js.map +1 -0
- package/dist/esm/use-summarize.d.ts +84 -0
- package/dist/esm/use-summarize.js +19 -0
- package/dist/esm/use-summarize.js.map +1 -0
- package/dist/esm/use-transcription.d.ts +89 -0
- package/dist/esm/use-transcription.js +19 -0
- package/dist/esm/use-transcription.js.map +1 -0
- package/package.json +4 -4
- package/src/index.ts +47 -0
- package/src/realtime-types.ts +143 -0
- package/src/use-generate-image.ts +117 -0
- package/src/use-generate-speech.ts +108 -0
- package/src/use-generate-video.ts +206 -0
- package/src/use-generation.ts +180 -0
- package/src/use-realtime-chat.ts +277 -0
- package/src/use-summarize.ts +114 -0
- package/src/use-transcription.ts +121 -0
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
1
|
export { useChat } from './use-chat.js';
|
|
2
|
+
export { useRealtimeChat } from './use-realtime-chat.js';
|
|
2
3
|
export type { UseChatOptions, UseChatReturn, UIMessage, ChatRequestBody, } from './types.js';
|
|
3
|
-
export
|
|
4
|
+
export type { UseRealtimeChatOptions, UseRealtimeChatReturn, } from './realtime-types.js';
|
|
5
|
+
export { useGeneration } from './use-generation.js';
|
|
6
|
+
export type { UseGenerationOptions, UseGenerationReturn, } from './use-generation.js';
|
|
7
|
+
export { useGenerateImage } from './use-generate-image.js';
|
|
8
|
+
export type { UseGenerateImageOptions, UseGenerateImageReturn, } from './use-generate-image.js';
|
|
9
|
+
export { useGenerateSpeech } from './use-generate-speech.js';
|
|
10
|
+
export type { UseGenerateSpeechOptions, UseGenerateSpeechReturn, } from './use-generate-speech.js';
|
|
11
|
+
export { useTranscription } from './use-transcription.js';
|
|
12
|
+
export type { UseTranscriptionOptions, UseTranscriptionReturn, } from './use-transcription.js';
|
|
13
|
+
export { useSummarize } from './use-summarize.js';
|
|
14
|
+
export type { UseSummarizeOptions, UseSummarizeReturn } from './use-summarize.js';
|
|
15
|
+
export { useGenerateVideo } from './use-generate-video.js';
|
|
16
|
+
export type { UseGenerateVideoOptions, UseGenerateVideoReturn, } from './use-generate-video.js';
|
|
17
|
+
export { fetchServerSentEvents, fetchHttpStream, stream, createChatClientOptions, type ConnectionAdapter, type FetchConnectionOptions, type InferChatMessages, type GenerationClientState, type ImageGenerateInput, type SpeechGenerateInput, type TranscriptionGenerateInput, type SummarizeGenerateInput, type VideoGenerateInput, type VideoGenerateResult, type VideoStatusInfo, } from '@tanstack/ai-client';
|
package/dist/esm/index.js
CHANGED
|
@@ -1,10 +1,24 @@
|
|
|
1
1
|
import { useChat } from "./use-chat.js";
|
|
2
|
+
import { useRealtimeChat } from "./use-realtime-chat.js";
|
|
3
|
+
import { useGeneration } from "./use-generation.js";
|
|
4
|
+
import { useGenerateImage } from "./use-generate-image.js";
|
|
5
|
+
import { useGenerateSpeech } from "./use-generate-speech.js";
|
|
6
|
+
import { useTranscription } from "./use-transcription.js";
|
|
7
|
+
import { useSummarize } from "./use-summarize.js";
|
|
8
|
+
import { useGenerateVideo } from "./use-generate-video.js";
|
|
2
9
|
import { createChatClientOptions, fetchHttpStream, fetchServerSentEvents, stream } from "@tanstack/ai-client";
|
|
3
10
|
export {
|
|
4
11
|
createChatClientOptions,
|
|
5
12
|
fetchHttpStream,
|
|
6
13
|
fetchServerSentEvents,
|
|
7
14
|
stream,
|
|
8
|
-
useChat
|
|
15
|
+
useChat,
|
|
16
|
+
useGenerateImage,
|
|
17
|
+
useGenerateSpeech,
|
|
18
|
+
useGenerateVideo,
|
|
19
|
+
useGeneration,
|
|
20
|
+
useRealtimeChat,
|
|
21
|
+
useSummarize,
|
|
22
|
+
useTranscription
|
|
9
23
|
};
|
|
10
24
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { AnyClientTool, RealtimeMessage, RealtimeMode, RealtimeStatus, RealtimeToken } from '@tanstack/ai';
|
|
2
|
+
import { RealtimeAdapter } from '@tanstack/ai-client';
|
|
3
|
+
/**
|
|
4
|
+
* Options for the useRealtimeChat hook.
|
|
5
|
+
*/
|
|
6
|
+
export interface UseRealtimeChatOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Function to fetch a realtime token from the server.
|
|
9
|
+
* Called on connect and when token needs refresh.
|
|
10
|
+
*/
|
|
11
|
+
getToken: () => Promise<RealtimeToken>;
|
|
12
|
+
/**
|
|
13
|
+
* The realtime adapter to use (e.g., openaiRealtime())
|
|
14
|
+
*/
|
|
15
|
+
adapter: RealtimeAdapter;
|
|
16
|
+
/**
|
|
17
|
+
* Client-side tools with execution logic
|
|
18
|
+
*/
|
|
19
|
+
tools?: ReadonlyArray<AnyClientTool>;
|
|
20
|
+
/**
|
|
21
|
+
* Auto-play assistant audio (default: true)
|
|
22
|
+
*/
|
|
23
|
+
autoPlayback?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Request microphone access on connect (default: true)
|
|
26
|
+
*/
|
|
27
|
+
autoCapture?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* System instructions for the assistant
|
|
30
|
+
*/
|
|
31
|
+
instructions?: string;
|
|
32
|
+
/**
|
|
33
|
+
* Voice to use for audio output
|
|
34
|
+
*/
|
|
35
|
+
voice?: string;
|
|
36
|
+
/**
|
|
37
|
+
* Voice activity detection mode (default: 'server')
|
|
38
|
+
*/
|
|
39
|
+
vadMode?: 'server' | 'semantic' | 'manual';
|
|
40
|
+
/**
|
|
41
|
+
* Output modalities for responses (e.g., ['audio', 'text'])
|
|
42
|
+
*/
|
|
43
|
+
outputModalities?: Array<'audio' | 'text'>;
|
|
44
|
+
/**
|
|
45
|
+
* Temperature for generation (provider-specific range)
|
|
46
|
+
*/
|
|
47
|
+
temperature?: number;
|
|
48
|
+
/**
|
|
49
|
+
* Maximum number of tokens in a response
|
|
50
|
+
*/
|
|
51
|
+
maxOutputTokens?: number | 'inf';
|
|
52
|
+
/**
|
|
53
|
+
* Eagerness level for semantic VAD ('low', 'medium', 'high')
|
|
54
|
+
*/
|
|
55
|
+
semanticEagerness?: 'low' | 'medium' | 'high';
|
|
56
|
+
onConnect?: () => void;
|
|
57
|
+
onDisconnect?: () => void;
|
|
58
|
+
onError?: (error: Error) => void;
|
|
59
|
+
onMessage?: (message: RealtimeMessage) => void;
|
|
60
|
+
onModeChange?: (mode: RealtimeMode) => void;
|
|
61
|
+
onInterrupted?: () => void;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Return type for the useRealtimeChat hook.
|
|
65
|
+
*/
|
|
66
|
+
export interface UseRealtimeChatReturn {
|
|
67
|
+
/** Current connection status */
|
|
68
|
+
status: RealtimeStatus;
|
|
69
|
+
/** Current error, if any */
|
|
70
|
+
error: Error | null;
|
|
71
|
+
/** Connect to the realtime session */
|
|
72
|
+
connect: () => Promise<void>;
|
|
73
|
+
/** Disconnect from the realtime session */
|
|
74
|
+
disconnect: () => Promise<void>;
|
|
75
|
+
/** Current mode (idle, listening, thinking, speaking) */
|
|
76
|
+
mode: RealtimeMode;
|
|
77
|
+
/** Conversation messages */
|
|
78
|
+
messages: Array<RealtimeMessage>;
|
|
79
|
+
/** User transcript while speaking (before finalized) */
|
|
80
|
+
pendingUserTranscript: string | null;
|
|
81
|
+
/** Assistant transcript while speaking (before finalized) */
|
|
82
|
+
pendingAssistantTranscript: string | null;
|
|
83
|
+
/** Start listening for voice input (manual VAD mode) */
|
|
84
|
+
startListening: () => void;
|
|
85
|
+
/** Stop listening for voice input (manual VAD mode) */
|
|
86
|
+
stopListening: () => void;
|
|
87
|
+
/** Interrupt the current assistant response */
|
|
88
|
+
interrupt: () => void;
|
|
89
|
+
/** Send a text message instead of voice */
|
|
90
|
+
sendText: (text: string) => void;
|
|
91
|
+
/** Send an image to the conversation */
|
|
92
|
+
sendImage: (imageData: string, mimeType: string) => void;
|
|
93
|
+
/** Current input (microphone) volume level */
|
|
94
|
+
inputLevel: number;
|
|
95
|
+
/** Current output (speaker) volume level */
|
|
96
|
+
outputLevel: number;
|
|
97
|
+
/** Get frequency data for input audio visualization */
|
|
98
|
+
getInputFrequencyData: () => Uint8Array;
|
|
99
|
+
/** Get frequency data for output audio visualization */
|
|
100
|
+
getOutputFrequencyData: () => Uint8Array;
|
|
101
|
+
/** Get time domain data for input waveform */
|
|
102
|
+
getInputTimeDomainData: () => Uint8Array;
|
|
103
|
+
/** Get time domain data for output waveform */
|
|
104
|
+
getOutputTimeDomainData: () => Uint8Array;
|
|
105
|
+
/** Current VAD mode */
|
|
106
|
+
vadMode: 'server' | 'semantic' | 'manual';
|
|
107
|
+
/** Change VAD mode at runtime */
|
|
108
|
+
setVADMode: (mode: 'server' | 'semantic' | 'manual') => void;
|
|
109
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { ImageGenerationResult, StreamChunk } from '@tanstack/ai';
|
|
2
|
+
import { ConnectionAdapter, 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
|
+
/** Connection adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
10
|
+
connection?: ConnectionAdapter;
|
|
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 ConnectionAdapter requests */
|
|
16
|
+
body?: Record<string, any>;
|
|
17
|
+
/**
|
|
18
|
+
* Callback when images are generated. 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: ImageGenerationResult) => 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 useGenerateImage hook.
|
|
34
|
+
*
|
|
35
|
+
* @template TOutput - The output type (after optional transform)
|
|
36
|
+
*/
|
|
37
|
+
export interface UseGenerateImageReturn<TOutput = ImageGenerationResult> {
|
|
38
|
+
/** Trigger image generation */
|
|
39
|
+
generate: (input: ImageGenerateInput) => Promise<void>;
|
|
40
|
+
/** The generation result containing images, or null */
|
|
41
|
+
result: TOutput | null;
|
|
42
|
+
/** Whether generation 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 generation */
|
|
49
|
+
stop: () => void;
|
|
50
|
+
/** Clear result, error, and return to idle */
|
|
51
|
+
reset: () => void;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* React hook for generating images using AI models.
|
|
55
|
+
*
|
|
56
|
+
* Supports two transport modes:
|
|
57
|
+
* - **ConnectionAdapter** — Streaming transport (SSE, HTTP stream, custom)
|
|
58
|
+
* - **Fetcher** — Direct async function call
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```tsx
|
|
62
|
+
* import { useGenerateImage } from '@tanstack/ai-react'
|
|
63
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
64
|
+
*
|
|
65
|
+
* function ImageGenerator() {
|
|
66
|
+
* const { generate, result, isLoading, error, reset } = useGenerateImage({
|
|
67
|
+
* connection: fetchServerSentEvents('/api/generate/image'),
|
|
68
|
+
* })
|
|
69
|
+
*
|
|
70
|
+
* return (
|
|
71
|
+
* <div>
|
|
72
|
+
* <button onClick={() => generate({ prompt: 'A sunset over mountains' })}>
|
|
73
|
+
* Generate
|
|
74
|
+
* </button>
|
|
75
|
+
* {isLoading && <p>Generating...</p>}
|
|
76
|
+
* {error && <p>Error: {error.message}</p>}
|
|
77
|
+
* {result?.images.map((img, i) => (
|
|
78
|
+
* <img key={i} src={img.url || `data:image/png;base64,${img.b64Json}`} />
|
|
79
|
+
* ))}
|
|
80
|
+
* </div>
|
|
81
|
+
* )
|
|
82
|
+
* }
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export declare function useGenerateImage<TOnResult extends ((result: ImageGenerationResult) => any) | undefined = undefined>(options: Omit<UseGenerateImageOptions, 'onResult'> & {
|
|
86
|
+
onResult?: TOnResult;
|
|
87
|
+
}): UseGenerateImageReturn<InferGenerationOutput<ImageGenerationResult, TOnResult>>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useGeneration } from "./use-generation.js";
|
|
2
|
+
function useGenerateImage(options) {
|
|
3
|
+
const { generate, result, isLoading, error, status, stop, reset } = useGeneration(options);
|
|
4
|
+
return {
|
|
5
|
+
generate,
|
|
6
|
+
result,
|
|
7
|
+
isLoading,
|
|
8
|
+
error,
|
|
9
|
+
status,
|
|
10
|
+
stop,
|
|
11
|
+
reset
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export {
|
|
15
|
+
useGenerateImage
|
|
16
|
+
};
|
|
17
|
+
//# 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 ConnectionAdapter,\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 /** Connection adapter for streaming transport (SSE, HTTP stream, custom) */\n connection?: ConnectionAdapter\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 ConnectionAdapter requests */\n body?: Record<string, any>\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 (ConnectionAdapter 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 * - **ConnectionAdapter** — 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 { generate, result, isLoading, error, status, stop, reset } =\n useGeneration<ImageGenerateInput, ImageGenerationResult, TOnResult>(options)\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":";AA8FO,SAAS,iBAId,SAKA;AACA,QAAM,EAAE,UAAU,QAAQ,WAAW,OAAO,QAAQ,MAAM,MAAA,IACxD,cAAoE,OAAO;AAE7E,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEJ;"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { StreamChunk, TTSResult } from '@tanstack/ai';
|
|
2
|
+
import { ConnectionAdapter, 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
|
+
/** Connection adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
10
|
+
connection?: ConnectionAdapter;
|
|
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 ConnectionAdapter requests */
|
|
16
|
+
body?: Record<string, any>;
|
|
17
|
+
/**
|
|
18
|
+
* Callback when speech is generated. 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: TTSResult) => 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 useGenerateSpeech hook.
|
|
34
|
+
*
|
|
35
|
+
* @template TOutput - The output type (after optional transform)
|
|
36
|
+
*/
|
|
37
|
+
export interface UseGenerateSpeechReturn<TOutput = TTSResult> {
|
|
38
|
+
/** Trigger speech generation */
|
|
39
|
+
generate: (input: SpeechGenerateInput) => Promise<void>;
|
|
40
|
+
/** The TTS result containing audio data, or null */
|
|
41
|
+
result: TOutput | null;
|
|
42
|
+
/** Whether generation 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 generation */
|
|
49
|
+
stop: () => void;
|
|
50
|
+
/** Clear result, error, and return to idle */
|
|
51
|
+
reset: () => void;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* React hook for generating speech (text-to-speech) using AI models.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```tsx
|
|
58
|
+
* import { useGenerateSpeech } from '@tanstack/ai-react'
|
|
59
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
60
|
+
*
|
|
61
|
+
* function SpeechGenerator() {
|
|
62
|
+
* const { generate, result, isLoading } = useGenerateSpeech({
|
|
63
|
+
* connection: fetchServerSentEvents('/api/generate/speech'),
|
|
64
|
+
* })
|
|
65
|
+
*
|
|
66
|
+
* return (
|
|
67
|
+
* <div>
|
|
68
|
+
* <button onClick={() => generate({ text: 'Hello world', voice: 'alloy' })}>
|
|
69
|
+
* Generate Speech
|
|
70
|
+
* </button>
|
|
71
|
+
* {result && (
|
|
72
|
+
* <audio src={`data:audio/${result.format};base64,${result.audio}`} controls />
|
|
73
|
+
* )}
|
|
74
|
+
* </div>
|
|
75
|
+
* )
|
|
76
|
+
* }
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export declare function useGenerateSpeech<TOnResult extends ((result: TTSResult) => any) | undefined = undefined>(options: Omit<UseGenerateSpeechOptions, 'onResult'> & {
|
|
80
|
+
onResult?: TOnResult;
|
|
81
|
+
}): UseGenerateSpeechReturn<InferGenerationOutput<TTSResult, TOnResult>>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useGeneration } from "./use-generation.js";
|
|
2
|
+
function useGenerateSpeech(options) {
|
|
3
|
+
const { generate, result, isLoading, error, status, stop, reset } = useGeneration(options);
|
|
4
|
+
return {
|
|
5
|
+
generate,
|
|
6
|
+
result,
|
|
7
|
+
isLoading,
|
|
8
|
+
error,
|
|
9
|
+
status,
|
|
10
|
+
stop,
|
|
11
|
+
reset
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export {
|
|
15
|
+
useGenerateSpeech
|
|
16
|
+
};
|
|
17
|
+
//# 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 ConnectionAdapter,\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 /** Connection adapter for streaming transport (SSE, HTTP stream, custom) */\n connection?: ConnectionAdapter\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 ConnectionAdapter requests */\n body?: Record<string, any>\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 (ConnectionAdapter 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 { generate, result, isLoading, error, status, stop, reset } =\n useGeneration<SpeechGenerateInput, TTSResult, TOnResult>(options)\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":";AAwFO,SAAS,kBAGd,SAGsE;AACtE,QAAM,EAAE,UAAU,QAAQ,WAAW,OAAO,QAAQ,MAAM,MAAA,IACxD,cAAyD,OAAO;AAElE,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEJ;"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { StreamChunk } from '@tanstack/ai';
|
|
2
|
+
import { ConnectionAdapter, 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
|
+
/** Connection adapter for streaming transport (server handles polling) */
|
|
8
|
+
connection?: ConnectionAdapter;
|
|
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 ConnectionAdapter requests */
|
|
14
|
+
body?: Record<string, any>;
|
|
15
|
+
/**
|
|
16
|
+
* Callback when video generation completes. Can optionally return a transformed value.
|
|
17
|
+
*
|
|
18
|
+
* - Return a non-null value to transform and store it as the result
|
|
19
|
+
* - Return `null` to keep the previous result unchanged
|
|
20
|
+
* - Return nothing (`void`) to store the raw result as-is
|
|
21
|
+
*/
|
|
22
|
+
onResult?: (result: VideoGenerateResult) => TOutput | null | void;
|
|
23
|
+
/** Callback when an error occurs */
|
|
24
|
+
onError?: (error: Error) => void;
|
|
25
|
+
/** Callback when progress is reported (0-100) */
|
|
26
|
+
onProgress?: (progress: number, message?: string) => void;
|
|
27
|
+
/** Callback when a video job is created */
|
|
28
|
+
onJobCreated?: (jobId: string) => void;
|
|
29
|
+
/** Callback on each status update */
|
|
30
|
+
onStatusUpdate?: (status: VideoStatusInfo) => void;
|
|
31
|
+
/** Callback for each stream chunk (ConnectionAdapter mode only) */
|
|
32
|
+
onChunk?: (chunk: StreamChunk) => void;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Return type for the useGenerateVideo hook.
|
|
36
|
+
*
|
|
37
|
+
* @template TOutput - The output type (after optional transform)
|
|
38
|
+
*/
|
|
39
|
+
export interface UseGenerateVideoReturn<TOutput = VideoGenerateResult> {
|
|
40
|
+
/** Trigger video generation */
|
|
41
|
+
generate: (input: VideoGenerateInput) => Promise<void>;
|
|
42
|
+
/** The final video result (with URL), or null */
|
|
43
|
+
result: TOutput | null;
|
|
44
|
+
/** The current job ID, or null */
|
|
45
|
+
jobId: string | null;
|
|
46
|
+
/** Current video generation status info, or null */
|
|
47
|
+
videoStatus: VideoStatusInfo | null;
|
|
48
|
+
/** Whether generation/polling is in progress */
|
|
49
|
+
isLoading: boolean;
|
|
50
|
+
/** Current error, if any */
|
|
51
|
+
error: Error | undefined;
|
|
52
|
+
/** Current state of the generation */
|
|
53
|
+
status: GenerationClientState;
|
|
54
|
+
/** Abort the current generation/polling */
|
|
55
|
+
stop: () => void;
|
|
56
|
+
/** Clear all state and return to idle */
|
|
57
|
+
reset: () => void;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* React hook for generating videos using AI models.
|
|
61
|
+
*
|
|
62
|
+
* Video generation is asynchronous: a job is created, then polled for status
|
|
63
|
+
* until completion. This hook handles the full lifecycle.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```tsx
|
|
67
|
+
* import { useGenerateVideo } from '@tanstack/ai-react'
|
|
68
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
69
|
+
*
|
|
70
|
+
* function VideoGenerator() {
|
|
71
|
+
* const { generate, result, videoStatus, isLoading } = useGenerateVideo({
|
|
72
|
+
* connection: fetchServerSentEvents('/api/generate/video'),
|
|
73
|
+
* onStatusUpdate: (status) => console.log(`Progress: ${status.progress}%`),
|
|
74
|
+
* })
|
|
75
|
+
*
|
|
76
|
+
* return (
|
|
77
|
+
* <div>
|
|
78
|
+
* <button onClick={() => generate({ prompt: 'A flying car over a city' })}>
|
|
79
|
+
* Generate Video
|
|
80
|
+
* </button>
|
|
81
|
+
* {isLoading && videoStatus && (
|
|
82
|
+
* <p>Status: {videoStatus.status} ({videoStatus.progress}%)</p>
|
|
83
|
+
* )}
|
|
84
|
+
* {result && <video src={result.url} controls />}
|
|
85
|
+
* </div>
|
|
86
|
+
* )
|
|
87
|
+
* }
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export declare function useGenerateVideo<TOnResult extends ((result: VideoGenerateResult) => any) | undefined = undefined>(options: Omit<UseGenerateVideoOptions, 'onResult'> & {
|
|
91
|
+
onResult?: TOnResult;
|
|
92
|
+
}): UseGenerateVideoReturn<InferGenerationOutput<VideoGenerateResult, TOnResult>>;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { VideoGenerationClient } from "@tanstack/ai-client";
|
|
2
|
+
import { useId, useState, useRef, useMemo, useEffect, useCallback } from "react";
|
|
3
|
+
function useGenerateVideo(options) {
|
|
4
|
+
const hookId = useId();
|
|
5
|
+
const clientId = options.id || hookId;
|
|
6
|
+
const [result, setResult] = useState(null);
|
|
7
|
+
const [jobId, setJobId] = useState(null);
|
|
8
|
+
const [videoStatus, setVideoStatus] = useState(null);
|
|
9
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
10
|
+
const [error, setError] = useState(void 0);
|
|
11
|
+
const [status, setStatus] = useState("idle");
|
|
12
|
+
const optionsRef = useRef(options);
|
|
13
|
+
optionsRef.current = options;
|
|
14
|
+
const client = useMemo(() => {
|
|
15
|
+
const opts = optionsRef.current;
|
|
16
|
+
const baseOptions = {
|
|
17
|
+
id: clientId,
|
|
18
|
+
body: opts.body,
|
|
19
|
+
onResult: (r) => optionsRef.current.onResult?.(r),
|
|
20
|
+
onError: (e) => optionsRef.current.onError?.(e),
|
|
21
|
+
onProgress: (p, m) => optionsRef.current.onProgress?.(p, m),
|
|
22
|
+
onChunk: (c) => optionsRef.current.onChunk?.(c),
|
|
23
|
+
onJobCreated: (id) => optionsRef.current.onJobCreated?.(id),
|
|
24
|
+
onStatusUpdate: (s) => optionsRef.current.onStatusUpdate?.(s),
|
|
25
|
+
onResultChange: setResult,
|
|
26
|
+
onLoadingChange: setIsLoading,
|
|
27
|
+
onErrorChange: setError,
|
|
28
|
+
onStatusChange: setStatus,
|
|
29
|
+
onJobIdChange: setJobId,
|
|
30
|
+
onVideoStatusChange: setVideoStatus
|
|
31
|
+
};
|
|
32
|
+
if (opts.connection) {
|
|
33
|
+
return new VideoGenerationClient({
|
|
34
|
+
...baseOptions,
|
|
35
|
+
connection: opts.connection
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
if (opts.fetcher) {
|
|
39
|
+
return new VideoGenerationClient({
|
|
40
|
+
...baseOptions,
|
|
41
|
+
fetcher: opts.fetcher
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
throw new Error(
|
|
45
|
+
"useGenerateVideo requires either a connection or fetcher option"
|
|
46
|
+
);
|
|
47
|
+
}, [clientId]);
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
client.updateOptions({ body: options.body });
|
|
50
|
+
}, [client, options.body]);
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
return () => {
|
|
53
|
+
client.stop();
|
|
54
|
+
};
|
|
55
|
+
}, [client]);
|
|
56
|
+
const generate = useCallback(
|
|
57
|
+
async (input) => {
|
|
58
|
+
await client.generate(input);
|
|
59
|
+
},
|
|
60
|
+
[client]
|
|
61
|
+
);
|
|
62
|
+
const stop = useCallback(() => {
|
|
63
|
+
client.stop();
|
|
64
|
+
}, [client]);
|
|
65
|
+
const reset = useCallback(() => {
|
|
66
|
+
client.reset();
|
|
67
|
+
}, [client]);
|
|
68
|
+
return {
|
|
69
|
+
generate,
|
|
70
|
+
result,
|
|
71
|
+
jobId,
|
|
72
|
+
videoStatus,
|
|
73
|
+
isLoading,
|
|
74
|
+
error,
|
|
75
|
+
status,
|
|
76
|
+
stop,
|
|
77
|
+
reset
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
export {
|
|
81
|
+
useGenerateVideo
|
|
82
|
+
};
|
|
83
|
+
//# 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 { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react'\nimport type { StreamChunk } from '@tanstack/ai'\nimport type {\n ConnectionAdapter,\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 /** Connection adapter for streaming transport (server handles polling) */\n connection?: ConnectionAdapter\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 ConnectionAdapter requests */\n body?: Record<string, any>\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 (ConnectionAdapter 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 const baseOptions = {\n id: clientId,\n body: opts.body,\n onResult: (r: VideoGenerateResult) => 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 onJobCreated: (id: string) => optionsRef.current.onJobCreated?.(id),\n onStatusUpdate: (s: VideoStatusInfo) =>\n optionsRef.current.onStatusUpdate?.(s),\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 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: 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":";;AAsGO,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;AAExB,UAAM,cAAc;AAAA,MAClB,IAAI;AAAA,MACJ,MAAM,KAAK;AAAA,MACX,UAAU,CAAC,MAA2B,WAAW,QAAQ,WAAW,CAAC;AAAA,MACrE,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,cAAc,CAAC,OAAe,WAAW,QAAQ,eAAe,EAAE;AAAA,MAClE,gBAAgB,CAAC,MACf,WAAW,QAAQ,iBAAiB,CAAC;AAAA,MACvC,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;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,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;"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { StreamChunk } from '@tanstack/ai';
|
|
2
|
+
import { ConnectionAdapter, GenerationClientState, GenerationFetcher, InferGenerationOutput } from '@tanstack/ai-client';
|
|
3
|
+
/**
|
|
4
|
+
* Options for the useGeneration hook.
|
|
5
|
+
*
|
|
6
|
+
* Accepts either a `connection` (streaming transport) or a `fetcher` (direct async call).
|
|
7
|
+
*
|
|
8
|
+
* @template TInput - The input type for the generation request
|
|
9
|
+
* @template TResult - The result type returned by the generation
|
|
10
|
+
* @template TOutput - The output type after optional transform (defaults to TResult)
|
|
11
|
+
*/
|
|
12
|
+
export interface UseGenerationOptions<TInput, TResult, TOutput = TResult> {
|
|
13
|
+
/** Connection adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
14
|
+
connection?: ConnectionAdapter;
|
|
15
|
+
/** Direct async function for one-shot generation (no streaming protocol needed) */
|
|
16
|
+
fetcher?: GenerationFetcher<TInput, TResult>;
|
|
17
|
+
/** Unique identifier for this generation instance */
|
|
18
|
+
id?: string;
|
|
19
|
+
/** Additional body parameters to send with ConnectionAdapter requests */
|
|
20
|
+
body?: Record<string, any>;
|
|
21
|
+
/**
|
|
22
|
+
* Callback when a result is received. Can optionally return a transformed value.
|
|
23
|
+
*
|
|
24
|
+
* - Return a non-null value to transform and store it as the result
|
|
25
|
+
* - Return `null` to keep the previous result unchanged
|
|
26
|
+
* - Return nothing (`void`) to store the raw result as-is
|
|
27
|
+
*/
|
|
28
|
+
onResult?: (result: TResult) => TOutput | null | void;
|
|
29
|
+
/** Callback when an error occurs */
|
|
30
|
+
onError?: (error: Error) => void;
|
|
31
|
+
/** Callback when progress is reported (0-100) */
|
|
32
|
+
onProgress?: (progress: number, message?: string) => void;
|
|
33
|
+
/** Callback for each stream chunk (ConnectionAdapter mode only) */
|
|
34
|
+
onChunk?: (chunk: StreamChunk) => void;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Return type for the useGeneration hook.
|
|
38
|
+
*
|
|
39
|
+
* @template TOutput - The output type (after optional transform)
|
|
40
|
+
*/
|
|
41
|
+
export interface UseGenerationReturn<TOutput> {
|
|
42
|
+
/** Trigger a generation request */
|
|
43
|
+
generate: (input: Record<string, any>) => Promise<void>;
|
|
44
|
+
/** The generation result, or null if not yet generated */
|
|
45
|
+
result: TOutput | null;
|
|
46
|
+
/** Whether a generation is currently in progress */
|
|
47
|
+
isLoading: boolean;
|
|
48
|
+
/** Current error, if any */
|
|
49
|
+
error: Error | undefined;
|
|
50
|
+
/** Current state of the generation client */
|
|
51
|
+
status: GenerationClientState;
|
|
52
|
+
/** Abort the current generation */
|
|
53
|
+
stop: () => void;
|
|
54
|
+
/** Clear result, error, and return to idle */
|
|
55
|
+
reset: () => void;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Generic React hook for one-shot generation tasks.
|
|
59
|
+
*
|
|
60
|
+
* This is the base hook used by `useGenerateImage`, `useGenerateSpeech`,
|
|
61
|
+
* `useTranscription`, and `useSummarize`. You can also use it directly
|
|
62
|
+
* for custom generation types.
|
|
63
|
+
*
|
|
64
|
+
* @template TInput - The input type for the generation request
|
|
65
|
+
* @template TResult - The result type returned by the generation
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```tsx
|
|
69
|
+
* const { generate, result, isLoading } = useGeneration<MyInput, MyResult>({
|
|
70
|
+
* connection: fetchServerSentEvents('/api/generate/custom'),
|
|
71
|
+
* })
|
|
72
|
+
*
|
|
73
|
+
* await generate({ prompt: 'Hello' })
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export declare function useGeneration<TInput extends Record<string, any>, TResult, TOnResult extends ((result: TResult) => any) | undefined = undefined>(options: Omit<UseGenerationOptions<TInput, TResult>, 'onResult'> & {
|
|
77
|
+
onResult?: TOnResult;
|
|
78
|
+
}): UseGenerationReturn<InferGenerationOutput<TResult, TOnResult>>;
|