@tanstack/ai-react 0.6.3 → 0.6.4
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 +13 -1
- package/dist/esm/index.js +13 -1
- package/dist/esm/index.js.map +1 -1
- 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-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 +42 -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-summarize.ts +114 -0
- package/src/use-transcription.ts +121 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { StreamChunk, SummarizationResult } from '@tanstack/ai';
|
|
2
|
+
import { ConnectionAdapter, GenerationClientState, GenerationFetcher, InferGenerationOutput, SummarizeGenerateInput } from '@tanstack/ai-client';
|
|
3
|
+
/**
|
|
4
|
+
* Options for the useSummarize hook.
|
|
5
|
+
*
|
|
6
|
+
* @template TOutput - The output type after optional transform (defaults to SummarizationResult)
|
|
7
|
+
*/
|
|
8
|
+
export interface UseSummarizeOptions<TOutput = SummarizationResult> {
|
|
9
|
+
/** Connection adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
10
|
+
connection?: ConnectionAdapter;
|
|
11
|
+
/** Direct async function for summarization */
|
|
12
|
+
fetcher?: GenerationFetcher<SummarizeGenerateInput, SummarizationResult>;
|
|
13
|
+
/** Unique identifier for this generation instance */
|
|
14
|
+
id?: string;
|
|
15
|
+
/** Additional body parameters to send with ConnectionAdapter requests */
|
|
16
|
+
body?: Record<string, any>;
|
|
17
|
+
/**
|
|
18
|
+
* Callback when summarization is complete. Can optionally return a transformed value.
|
|
19
|
+
*
|
|
20
|
+
* - Return a non-null value to transform and store it as the result
|
|
21
|
+
* - Return `null` to keep the previous result unchanged
|
|
22
|
+
* - Return nothing (`void`) to store the raw result as-is
|
|
23
|
+
*/
|
|
24
|
+
onResult?: (result: SummarizationResult) => TOutput | null | void;
|
|
25
|
+
/** Callback when an error occurs */
|
|
26
|
+
onError?: (error: Error) => void;
|
|
27
|
+
/** Callback when progress is reported (0-100) */
|
|
28
|
+
onProgress?: (progress: number, message?: string) => void;
|
|
29
|
+
/** Callback for each stream chunk (ConnectionAdapter mode only) */
|
|
30
|
+
onChunk?: (chunk: StreamChunk) => void;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Return type for the useSummarize hook.
|
|
34
|
+
*
|
|
35
|
+
* @template TOutput - The output type (after optional transform)
|
|
36
|
+
*/
|
|
37
|
+
export interface UseSummarizeReturn<TOutput = SummarizationResult> {
|
|
38
|
+
/** Trigger summarization */
|
|
39
|
+
generate: (input: SummarizeGenerateInput) => Promise<void>;
|
|
40
|
+
/** The summarization result, or null */
|
|
41
|
+
result: TOutput | null;
|
|
42
|
+
/** Whether summarization is in progress */
|
|
43
|
+
isLoading: boolean;
|
|
44
|
+
/** Current error, if any */
|
|
45
|
+
error: Error | undefined;
|
|
46
|
+
/** Current state of the generation */
|
|
47
|
+
status: GenerationClientState;
|
|
48
|
+
/** Abort the current summarization */
|
|
49
|
+
stop: () => void;
|
|
50
|
+
/** Clear result, error, and return to idle */
|
|
51
|
+
reset: () => void;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* React hook for summarizing text using AI models.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```tsx
|
|
58
|
+
* import { useSummarize } from '@tanstack/ai-react'
|
|
59
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
60
|
+
*
|
|
61
|
+
* function Summarizer() {
|
|
62
|
+
* const { generate, result, isLoading } = useSummarize({
|
|
63
|
+
* connection: fetchServerSentEvents('/api/summarize'),
|
|
64
|
+
* })
|
|
65
|
+
*
|
|
66
|
+
* return (
|
|
67
|
+
* <div>
|
|
68
|
+
* <button onClick={() => generate({
|
|
69
|
+
* text: 'Long article text...',
|
|
70
|
+
* style: 'bullet-points',
|
|
71
|
+
* maxLength: 200,
|
|
72
|
+
* })}>
|
|
73
|
+
* Summarize
|
|
74
|
+
* </button>
|
|
75
|
+
* {isLoading && <p>Summarizing...</p>}
|
|
76
|
+
* {result && <p>{result.summary}</p>}
|
|
77
|
+
* </div>
|
|
78
|
+
* )
|
|
79
|
+
* }
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export declare function useSummarize<TOnResult extends ((result: SummarizationResult) => any) | undefined = undefined>(options: Omit<UseSummarizeOptions, 'onResult'> & {
|
|
83
|
+
onResult?: TOnResult;
|
|
84
|
+
}): UseSummarizeReturn<InferGenerationOutput<SummarizationResult, TOnResult>>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { useGeneration } from "./use-generation.js";
|
|
2
|
+
function useSummarize(options) {
|
|
3
|
+
const { generate, result, isLoading, error, status, stop, reset } = useGeneration(
|
|
4
|
+
options
|
|
5
|
+
);
|
|
6
|
+
return {
|
|
7
|
+
generate,
|
|
8
|
+
result,
|
|
9
|
+
isLoading,
|
|
10
|
+
error,
|
|
11
|
+
status,
|
|
12
|
+
stop,
|
|
13
|
+
reset
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export {
|
|
17
|
+
useSummarize
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=use-summarize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-summarize.js","sources":["../../src/use-summarize.ts"],"sourcesContent":["import { useGeneration } from './use-generation'\nimport type { StreamChunk, SummarizationResult } from '@tanstack/ai'\nimport type {\n ConnectionAdapter,\n GenerationClientState,\n GenerationFetcher,\n InferGenerationOutput,\n SummarizeGenerateInput,\n} from '@tanstack/ai-client'\n\n/**\n * Options for the useSummarize hook.\n *\n * @template TOutput - The output type after optional transform (defaults to SummarizationResult)\n */\nexport interface UseSummarizeOptions<TOutput = SummarizationResult> {\n /** Connection adapter for streaming transport (SSE, HTTP stream, custom) */\n connection?: ConnectionAdapter\n /** Direct async function for summarization */\n fetcher?: GenerationFetcher<SummarizeGenerateInput, SummarizationResult>\n /** Unique identifier for this generation instance */\n id?: string\n /** Additional body parameters to send with ConnectionAdapter requests */\n body?: Record<string, any>\n /**\n * Callback when summarization is complete. Can optionally return a transformed value.\n *\n * - Return a non-null value to transform and store it as the result\n * - Return `null` to keep the previous result unchanged\n * - Return nothing (`void`) to store the raw result as-is\n */\n onResult?: (result: SummarizationResult) => TOutput | null | void\n /** Callback when an error occurs */\n onError?: (error: Error) => void\n /** Callback when progress is reported (0-100) */\n onProgress?: (progress: number, message?: string) => void\n /** Callback for each stream chunk (ConnectionAdapter mode only) */\n onChunk?: (chunk: StreamChunk) => void\n}\n\n/**\n * Return type for the useSummarize hook.\n *\n * @template TOutput - The output type (after optional transform)\n */\nexport interface UseSummarizeReturn<TOutput = SummarizationResult> {\n /** Trigger summarization */\n generate: (input: SummarizeGenerateInput) => Promise<void>\n /** The summarization result, or null */\n result: TOutput | null\n /** Whether summarization is in progress */\n isLoading: boolean\n /** Current error, if any */\n error: Error | undefined\n /** Current state of the generation */\n status: GenerationClientState\n /** Abort the current summarization */\n stop: () => void\n /** Clear result, error, and return to idle */\n reset: () => void\n}\n\n/**\n * React hook for summarizing text using AI models.\n *\n * @example\n * ```tsx\n * import { useSummarize } from '@tanstack/ai-react'\n * import { fetchServerSentEvents } from '@tanstack/ai-client'\n *\n * function Summarizer() {\n * const { generate, result, isLoading } = useSummarize({\n * connection: fetchServerSentEvents('/api/summarize'),\n * })\n *\n * return (\n * <div>\n * <button onClick={() => generate({\n * text: 'Long article text...',\n * style: 'bullet-points',\n * maxLength: 200,\n * })}>\n * Summarize\n * </button>\n * {isLoading && <p>Summarizing...</p>}\n * {result && <p>{result.summary}</p>}\n * </div>\n * )\n * }\n * ```\n */\nexport function useSummarize<\n TOnResult extends ((result: SummarizationResult) => any) | undefined =\n undefined,\n>(\n options: Omit<UseSummarizeOptions, 'onResult'> & {\n onResult?: TOnResult\n },\n): UseSummarizeReturn<InferGenerationOutput<SummarizationResult, TOnResult>> {\n const { generate, result, isLoading, error, status, stop, reset } =\n useGeneration<SummarizeGenerateInput, SummarizationResult, TOnResult>(\n options,\n )\n\n return {\n generate: generate as (input: SummarizeGenerateInput) => Promise<void>,\n result,\n isLoading,\n error,\n status,\n stop,\n reset,\n }\n}\n"],"names":[],"mappings":";AA2FO,SAAS,aAId,SAG2E;AAC3E,QAAM,EAAE,UAAU,QAAQ,WAAW,OAAO,QAAQ,MAAM,UACxD;AAAA,IACE;AAAA,EAAA;AAGJ,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEJ;"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { StreamChunk, TranscriptionResult } from '@tanstack/ai';
|
|
2
|
+
import { ConnectionAdapter, GenerationClientState, GenerationFetcher, InferGenerationOutput, TranscriptionGenerateInput } from '@tanstack/ai-client';
|
|
3
|
+
/**
|
|
4
|
+
* Options for the useTranscription hook.
|
|
5
|
+
*
|
|
6
|
+
* @template TOutput - The output type after optional transform (defaults to TranscriptionResult)
|
|
7
|
+
*/
|
|
8
|
+
export interface UseTranscriptionOptions<TOutput = TranscriptionResult> {
|
|
9
|
+
/** Connection adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
10
|
+
connection?: ConnectionAdapter;
|
|
11
|
+
/** Direct async function for transcription */
|
|
12
|
+
fetcher?: GenerationFetcher<TranscriptionGenerateInput, TranscriptionResult>;
|
|
13
|
+
/** Unique identifier for this generation instance */
|
|
14
|
+
id?: string;
|
|
15
|
+
/** Additional body parameters to send with ConnectionAdapter requests */
|
|
16
|
+
body?: Record<string, any>;
|
|
17
|
+
/**
|
|
18
|
+
* Callback when transcription is complete. Can optionally return a transformed value.
|
|
19
|
+
*
|
|
20
|
+
* - Return a non-null value to transform and store it as the result
|
|
21
|
+
* - Return `null` to keep the previous result unchanged
|
|
22
|
+
* - Return nothing (`void`) to store the raw result as-is
|
|
23
|
+
*/
|
|
24
|
+
onResult?: (result: TranscriptionResult) => TOutput | null | void;
|
|
25
|
+
/** Callback when an error occurs */
|
|
26
|
+
onError?: (error: Error) => void;
|
|
27
|
+
/** Callback when progress is reported (0-100) */
|
|
28
|
+
onProgress?: (progress: number, message?: string) => void;
|
|
29
|
+
/** Callback for each stream chunk (ConnectionAdapter mode only) */
|
|
30
|
+
onChunk?: (chunk: StreamChunk) => void;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Return type for the useTranscription hook.
|
|
34
|
+
*
|
|
35
|
+
* @template TOutput - The output type (after optional transform)
|
|
36
|
+
*/
|
|
37
|
+
export interface UseTranscriptionReturn<TOutput = TranscriptionResult> {
|
|
38
|
+
/** Trigger transcription */
|
|
39
|
+
generate: (input: TranscriptionGenerateInput) => Promise<void>;
|
|
40
|
+
/** The transcription result, or null */
|
|
41
|
+
result: TOutput | null;
|
|
42
|
+
/** Whether transcription is in progress */
|
|
43
|
+
isLoading: boolean;
|
|
44
|
+
/** Current error, if any */
|
|
45
|
+
error: Error | undefined;
|
|
46
|
+
/** Current state of the generation */
|
|
47
|
+
status: GenerationClientState;
|
|
48
|
+
/** Abort the current transcription */
|
|
49
|
+
stop: () => void;
|
|
50
|
+
/** Clear result, error, and return to idle */
|
|
51
|
+
reset: () => void;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* React hook for transcribing audio to text using AI models.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```tsx
|
|
58
|
+
* import { useTranscription } from '@tanstack/ai-react'
|
|
59
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
60
|
+
*
|
|
61
|
+
* function Transcriber() {
|
|
62
|
+
* const { generate, result, isLoading } = useTranscription({
|
|
63
|
+
* connection: fetchServerSentEvents('/api/transcribe'),
|
|
64
|
+
* })
|
|
65
|
+
*
|
|
66
|
+
* const handleFile = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
67
|
+
* const file = e.target.files?.[0]
|
|
68
|
+
* if (file) {
|
|
69
|
+
* const reader = new FileReader()
|
|
70
|
+
* reader.onload = () => {
|
|
71
|
+
* generate({ audio: reader.result as string, language: 'en' })
|
|
72
|
+
* }
|
|
73
|
+
* reader.readAsDataURL(file)
|
|
74
|
+
* }
|
|
75
|
+
* }
|
|
76
|
+
*
|
|
77
|
+
* return (
|
|
78
|
+
* <div>
|
|
79
|
+
* <input type="file" accept="audio/*" onChange={handleFile} />
|
|
80
|
+
* {isLoading && <p>Transcribing...</p>}
|
|
81
|
+
* {result && <p>{result.text}</p>}
|
|
82
|
+
* </div>
|
|
83
|
+
* )
|
|
84
|
+
* }
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export declare function useTranscription<TOnResult extends ((result: TranscriptionResult) => any) | undefined = undefined>(options: Omit<UseTranscriptionOptions, 'onResult'> & {
|
|
88
|
+
onResult?: TOnResult;
|
|
89
|
+
}): UseTranscriptionReturn<InferGenerationOutput<TranscriptionResult, TOnResult>>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { useGeneration } from "./use-generation.js";
|
|
2
|
+
function useTranscription(options) {
|
|
3
|
+
const { generate, result, isLoading, error, status, stop, reset } = useGeneration(
|
|
4
|
+
options
|
|
5
|
+
);
|
|
6
|
+
return {
|
|
7
|
+
generate,
|
|
8
|
+
result,
|
|
9
|
+
isLoading,
|
|
10
|
+
error,
|
|
11
|
+
status,
|
|
12
|
+
stop,
|
|
13
|
+
reset
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export {
|
|
17
|
+
useTranscription
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=use-transcription.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-transcription.js","sources":["../../src/use-transcription.ts"],"sourcesContent":["import { useGeneration } from './use-generation'\nimport type { StreamChunk, TranscriptionResult } from '@tanstack/ai'\nimport type {\n ConnectionAdapter,\n GenerationClientState,\n GenerationFetcher,\n InferGenerationOutput,\n TranscriptionGenerateInput,\n} from '@tanstack/ai-client'\n\n/**\n * Options for the useTranscription hook.\n *\n * @template TOutput - The output type after optional transform (defaults to TranscriptionResult)\n */\nexport interface UseTranscriptionOptions<TOutput = TranscriptionResult> {\n /** Connection adapter for streaming transport (SSE, HTTP stream, custom) */\n connection?: ConnectionAdapter\n /** Direct async function for transcription */\n fetcher?: GenerationFetcher<TranscriptionGenerateInput, TranscriptionResult>\n /** Unique identifier for this generation instance */\n id?: string\n /** Additional body parameters to send with ConnectionAdapter requests */\n body?: Record<string, any>\n /**\n * Callback when transcription is complete. Can optionally return a transformed value.\n *\n * - Return a non-null value to transform and store it as the result\n * - Return `null` to keep the previous result unchanged\n * - Return nothing (`void`) to store the raw result as-is\n */\n onResult?: (result: TranscriptionResult) => TOutput | null | void\n /** Callback when an error occurs */\n onError?: (error: Error) => void\n /** Callback when progress is reported (0-100) */\n onProgress?: (progress: number, message?: string) => void\n /** Callback for each stream chunk (ConnectionAdapter mode only) */\n onChunk?: (chunk: StreamChunk) => void\n}\n\n/**\n * Return type for the useTranscription hook.\n *\n * @template TOutput - The output type (after optional transform)\n */\nexport interface UseTranscriptionReturn<TOutput = TranscriptionResult> {\n /** Trigger transcription */\n generate: (input: TranscriptionGenerateInput) => Promise<void>\n /** The transcription result, or null */\n result: TOutput | null\n /** Whether transcription is in progress */\n isLoading: boolean\n /** Current error, if any */\n error: Error | undefined\n /** Current state of the generation */\n status: GenerationClientState\n /** Abort the current transcription */\n stop: () => void\n /** Clear result, error, and return to idle */\n reset: () => void\n}\n\n/**\n * React hook for transcribing audio to text using AI models.\n *\n * @example\n * ```tsx\n * import { useTranscription } from '@tanstack/ai-react'\n * import { fetchServerSentEvents } from '@tanstack/ai-client'\n *\n * function Transcriber() {\n * const { generate, result, isLoading } = useTranscription({\n * connection: fetchServerSentEvents('/api/transcribe'),\n * })\n *\n * const handleFile = (e: React.ChangeEvent<HTMLInputElement>) => {\n * const file = e.target.files?.[0]\n * if (file) {\n * const reader = new FileReader()\n * reader.onload = () => {\n * generate({ audio: reader.result as string, language: 'en' })\n * }\n * reader.readAsDataURL(file)\n * }\n * }\n *\n * return (\n * <div>\n * <input type=\"file\" accept=\"audio/*\" onChange={handleFile} />\n * {isLoading && <p>Transcribing...</p>}\n * {result && <p>{result.text}</p>}\n * </div>\n * )\n * }\n * ```\n */\nexport function useTranscription<\n TOnResult extends ((result: TranscriptionResult) => any) | undefined =\n undefined,\n>(\n options: Omit<UseTranscriptionOptions, 'onResult'> & {\n onResult?: TOnResult\n },\n): UseTranscriptionReturn<\n InferGenerationOutput<TranscriptionResult, TOnResult>\n> {\n const { generate, result, isLoading, error, status, stop, reset } =\n useGeneration<TranscriptionGenerateInput, TranscriptionResult, TOnResult>(\n options,\n )\n\n return {\n generate: generate as (input: TranscriptionGenerateInput) => Promise<void>,\n result,\n isLoading,\n error,\n status,\n stop,\n reset,\n }\n}\n"],"names":[],"mappings":";AAgGO,SAAS,iBAId,SAKA;AACA,QAAM,EAAE,UAAU,QAAQ,WAAW,OAAO,QAAQ,MAAM,UACxD;AAAA,IACE;AAAA,EAAA;AAGJ,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEJ;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-react",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.4",
|
|
4
4
|
"description": "React hooks for TanStack AI",
|
|
5
5
|
"author": "",
|
|
6
6
|
"license": "MIT",
|
|
@@ -31,12 +31,12 @@
|
|
|
31
31
|
"streaming"
|
|
32
32
|
],
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@tanstack/ai-client": "0.
|
|
34
|
+
"@tanstack/ai-client": "0.6.0"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
37
|
"@types/react": ">=18.0.0",
|
|
38
38
|
"react": ">=18.0.0",
|
|
39
|
-
"@tanstack/ai": "^0.6.
|
|
39
|
+
"@tanstack/ai": "^0.6.3"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@testing-library/react": "^16.3.0",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"jsdom": "^27.2.0",
|
|
46
46
|
"react": "^19.2.3",
|
|
47
47
|
"vite": "^7.2.7",
|
|
48
|
-
"@tanstack/ai": "0.6.
|
|
48
|
+
"@tanstack/ai": "0.6.3"
|
|
49
49
|
},
|
|
50
50
|
"scripts": {
|
|
51
51
|
"clean": "premove ./build ./dist",
|
package/src/index.ts
CHANGED
|
@@ -6,6 +6,40 @@ export type {
|
|
|
6
6
|
ChatRequestBody,
|
|
7
7
|
} from './types'
|
|
8
8
|
|
|
9
|
+
// Generation hooks
|
|
10
|
+
export { useGeneration } from './use-generation'
|
|
11
|
+
export type {
|
|
12
|
+
UseGenerationOptions,
|
|
13
|
+
UseGenerationReturn,
|
|
14
|
+
} from './use-generation'
|
|
15
|
+
|
|
16
|
+
export { useGenerateImage } from './use-generate-image'
|
|
17
|
+
export type {
|
|
18
|
+
UseGenerateImageOptions,
|
|
19
|
+
UseGenerateImageReturn,
|
|
20
|
+
} from './use-generate-image'
|
|
21
|
+
|
|
22
|
+
export { useGenerateSpeech } from './use-generate-speech'
|
|
23
|
+
export type {
|
|
24
|
+
UseGenerateSpeechOptions,
|
|
25
|
+
UseGenerateSpeechReturn,
|
|
26
|
+
} from './use-generate-speech'
|
|
27
|
+
|
|
28
|
+
export { useTranscription } from './use-transcription'
|
|
29
|
+
export type {
|
|
30
|
+
UseTranscriptionOptions,
|
|
31
|
+
UseTranscriptionReturn,
|
|
32
|
+
} from './use-transcription'
|
|
33
|
+
|
|
34
|
+
export { useSummarize } from './use-summarize'
|
|
35
|
+
export type { UseSummarizeOptions, UseSummarizeReturn } from './use-summarize'
|
|
36
|
+
|
|
37
|
+
export { useGenerateVideo } from './use-generate-video'
|
|
38
|
+
export type {
|
|
39
|
+
UseGenerateVideoOptions,
|
|
40
|
+
UseGenerateVideoReturn,
|
|
41
|
+
} from './use-generate-video'
|
|
42
|
+
|
|
9
43
|
// Re-export from ai-client for convenience
|
|
10
44
|
export {
|
|
11
45
|
fetchServerSentEvents,
|
|
@@ -15,4 +49,12 @@ export {
|
|
|
15
49
|
type ConnectionAdapter,
|
|
16
50
|
type FetchConnectionOptions,
|
|
17
51
|
type InferChatMessages,
|
|
52
|
+
type GenerationClientState,
|
|
53
|
+
type ImageGenerateInput,
|
|
54
|
+
type SpeechGenerateInput,
|
|
55
|
+
type TranscriptionGenerateInput,
|
|
56
|
+
type SummarizeGenerateInput,
|
|
57
|
+
type VideoGenerateInput,
|
|
58
|
+
type VideoGenerateResult,
|
|
59
|
+
type VideoStatusInfo,
|
|
18
60
|
} from '@tanstack/ai-client'
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { useGeneration } from './use-generation'
|
|
2
|
+
import type { ImageGenerationResult, StreamChunk } from '@tanstack/ai'
|
|
3
|
+
import type {
|
|
4
|
+
ConnectionAdapter,
|
|
5
|
+
GenerationClientState,
|
|
6
|
+
GenerationFetcher,
|
|
7
|
+
ImageGenerateInput,
|
|
8
|
+
InferGenerationOutput,
|
|
9
|
+
} from '@tanstack/ai-client'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Options for the useGenerateImage hook.
|
|
13
|
+
*
|
|
14
|
+
* @template TOutput - The output type after optional transform (defaults to ImageGenerationResult)
|
|
15
|
+
*/
|
|
16
|
+
export interface UseGenerateImageOptions<TOutput = ImageGenerationResult> {
|
|
17
|
+
/** Connection adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
18
|
+
connection?: ConnectionAdapter
|
|
19
|
+
/** Direct async function for image generation */
|
|
20
|
+
fetcher?: GenerationFetcher<ImageGenerateInput, ImageGenerationResult>
|
|
21
|
+
/** Unique identifier for this generation instance */
|
|
22
|
+
id?: string
|
|
23
|
+
/** Additional body parameters to send with ConnectionAdapter requests */
|
|
24
|
+
body?: Record<string, any>
|
|
25
|
+
/**
|
|
26
|
+
* Callback when images are generated. Can optionally return a transformed value.
|
|
27
|
+
*
|
|
28
|
+
* - Return a non-null value to transform and store it as the result
|
|
29
|
+
* - Return `null` to keep the previous result unchanged
|
|
30
|
+
* - Return nothing (`void`) to store the raw result as-is
|
|
31
|
+
*/
|
|
32
|
+
onResult?: (result: ImageGenerationResult) => TOutput | null | void
|
|
33
|
+
/** Callback when an error occurs */
|
|
34
|
+
onError?: (error: Error) => void
|
|
35
|
+
/** Callback when progress is reported (0-100) */
|
|
36
|
+
onProgress?: (progress: number, message?: string) => void
|
|
37
|
+
/** Callback for each stream chunk (ConnectionAdapter mode only) */
|
|
38
|
+
onChunk?: (chunk: StreamChunk) => void
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Return type for the useGenerateImage hook.
|
|
43
|
+
*
|
|
44
|
+
* @template TOutput - The output type (after optional transform)
|
|
45
|
+
*/
|
|
46
|
+
export interface UseGenerateImageReturn<TOutput = ImageGenerationResult> {
|
|
47
|
+
/** Trigger image generation */
|
|
48
|
+
generate: (input: ImageGenerateInput) => Promise<void>
|
|
49
|
+
/** The generation result containing images, or null */
|
|
50
|
+
result: TOutput | null
|
|
51
|
+
/** Whether generation is in progress */
|
|
52
|
+
isLoading: boolean
|
|
53
|
+
/** Current error, if any */
|
|
54
|
+
error: Error | undefined
|
|
55
|
+
/** Current state of the generation */
|
|
56
|
+
status: GenerationClientState
|
|
57
|
+
/** Abort the current generation */
|
|
58
|
+
stop: () => void
|
|
59
|
+
/** Clear result, error, and return to idle */
|
|
60
|
+
reset: () => void
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* React hook for generating images using AI models.
|
|
65
|
+
*
|
|
66
|
+
* Supports two transport modes:
|
|
67
|
+
* - **ConnectionAdapter** — Streaming transport (SSE, HTTP stream, custom)
|
|
68
|
+
* - **Fetcher** — Direct async function call
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```tsx
|
|
72
|
+
* import { useGenerateImage } from '@tanstack/ai-react'
|
|
73
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
74
|
+
*
|
|
75
|
+
* function ImageGenerator() {
|
|
76
|
+
* const { generate, result, isLoading, error, reset } = useGenerateImage({
|
|
77
|
+
* connection: fetchServerSentEvents('/api/generate/image'),
|
|
78
|
+
* })
|
|
79
|
+
*
|
|
80
|
+
* return (
|
|
81
|
+
* <div>
|
|
82
|
+
* <button onClick={() => generate({ prompt: 'A sunset over mountains' })}>
|
|
83
|
+
* Generate
|
|
84
|
+
* </button>
|
|
85
|
+
* {isLoading && <p>Generating...</p>}
|
|
86
|
+
* {error && <p>Error: {error.message}</p>}
|
|
87
|
+
* {result?.images.map((img, i) => (
|
|
88
|
+
* <img key={i} src={img.url || `data:image/png;base64,${img.b64Json}`} />
|
|
89
|
+
* ))}
|
|
90
|
+
* </div>
|
|
91
|
+
* )
|
|
92
|
+
* }
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
export function useGenerateImage<
|
|
96
|
+
TOnResult extends ((result: ImageGenerationResult) => any) | undefined =
|
|
97
|
+
undefined,
|
|
98
|
+
>(
|
|
99
|
+
options: Omit<UseGenerateImageOptions, 'onResult'> & {
|
|
100
|
+
onResult?: TOnResult
|
|
101
|
+
},
|
|
102
|
+
): UseGenerateImageReturn<
|
|
103
|
+
InferGenerationOutput<ImageGenerationResult, TOnResult>
|
|
104
|
+
> {
|
|
105
|
+
const { generate, result, isLoading, error, status, stop, reset } =
|
|
106
|
+
useGeneration<ImageGenerateInput, ImageGenerationResult, TOnResult>(options)
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
generate: generate as (input: ImageGenerateInput) => Promise<void>,
|
|
110
|
+
result,
|
|
111
|
+
isLoading,
|
|
112
|
+
error,
|
|
113
|
+
status,
|
|
114
|
+
stop,
|
|
115
|
+
reset,
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { useGeneration } from './use-generation'
|
|
2
|
+
import type { StreamChunk, TTSResult } from '@tanstack/ai'
|
|
3
|
+
import type {
|
|
4
|
+
ConnectionAdapter,
|
|
5
|
+
GenerationClientState,
|
|
6
|
+
GenerationFetcher,
|
|
7
|
+
InferGenerationOutput,
|
|
8
|
+
SpeechGenerateInput,
|
|
9
|
+
} from '@tanstack/ai-client'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Options for the useGenerateSpeech hook.
|
|
13
|
+
*
|
|
14
|
+
* @template TOutput - The output type after optional transform (defaults to TTSResult)
|
|
15
|
+
*/
|
|
16
|
+
export interface UseGenerateSpeechOptions<TOutput = TTSResult> {
|
|
17
|
+
/** Connection adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
18
|
+
connection?: ConnectionAdapter
|
|
19
|
+
/** Direct async function for speech generation */
|
|
20
|
+
fetcher?: GenerationFetcher<SpeechGenerateInput, TTSResult>
|
|
21
|
+
/** Unique identifier for this generation instance */
|
|
22
|
+
id?: string
|
|
23
|
+
/** Additional body parameters to send with ConnectionAdapter requests */
|
|
24
|
+
body?: Record<string, any>
|
|
25
|
+
/**
|
|
26
|
+
* Callback when speech is generated. Can optionally return a transformed value.
|
|
27
|
+
*
|
|
28
|
+
* - Return a non-null value to transform and store it as the result
|
|
29
|
+
* - Return `null` to keep the previous result unchanged
|
|
30
|
+
* - Return nothing (`void`) to store the raw result as-is
|
|
31
|
+
*/
|
|
32
|
+
onResult?: (result: TTSResult) => TOutput | null | void
|
|
33
|
+
/** Callback when an error occurs */
|
|
34
|
+
onError?: (error: Error) => void
|
|
35
|
+
/** Callback when progress is reported (0-100) */
|
|
36
|
+
onProgress?: (progress: number, message?: string) => void
|
|
37
|
+
/** Callback for each stream chunk (ConnectionAdapter mode only) */
|
|
38
|
+
onChunk?: (chunk: StreamChunk) => void
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Return type for the useGenerateSpeech hook.
|
|
43
|
+
*
|
|
44
|
+
* @template TOutput - The output type (after optional transform)
|
|
45
|
+
*/
|
|
46
|
+
export interface UseGenerateSpeechReturn<TOutput = TTSResult> {
|
|
47
|
+
/** Trigger speech generation */
|
|
48
|
+
generate: (input: SpeechGenerateInput) => Promise<void>
|
|
49
|
+
/** The TTS result containing audio data, or null */
|
|
50
|
+
result: TOutput | null
|
|
51
|
+
/** Whether generation is in progress */
|
|
52
|
+
isLoading: boolean
|
|
53
|
+
/** Current error, if any */
|
|
54
|
+
error: Error | undefined
|
|
55
|
+
/** Current state of the generation */
|
|
56
|
+
status: GenerationClientState
|
|
57
|
+
/** Abort the current generation */
|
|
58
|
+
stop: () => void
|
|
59
|
+
/** Clear result, error, and return to idle */
|
|
60
|
+
reset: () => void
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* React hook for generating speech (text-to-speech) using AI models.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```tsx
|
|
68
|
+
* import { useGenerateSpeech } from '@tanstack/ai-react'
|
|
69
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
70
|
+
*
|
|
71
|
+
* function SpeechGenerator() {
|
|
72
|
+
* const { generate, result, isLoading } = useGenerateSpeech({
|
|
73
|
+
* connection: fetchServerSentEvents('/api/generate/speech'),
|
|
74
|
+
* })
|
|
75
|
+
*
|
|
76
|
+
* return (
|
|
77
|
+
* <div>
|
|
78
|
+
* <button onClick={() => generate({ text: 'Hello world', voice: 'alloy' })}>
|
|
79
|
+
* Generate Speech
|
|
80
|
+
* </button>
|
|
81
|
+
* {result && (
|
|
82
|
+
* <audio src={`data:audio/${result.format};base64,${result.audio}`} controls />
|
|
83
|
+
* )}
|
|
84
|
+
* </div>
|
|
85
|
+
* )
|
|
86
|
+
* }
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export function useGenerateSpeech<
|
|
90
|
+
TOnResult extends ((result: TTSResult) => any) | undefined = undefined,
|
|
91
|
+
>(
|
|
92
|
+
options: Omit<UseGenerateSpeechOptions, 'onResult'> & {
|
|
93
|
+
onResult?: TOnResult
|
|
94
|
+
},
|
|
95
|
+
): UseGenerateSpeechReturn<InferGenerationOutput<TTSResult, TOnResult>> {
|
|
96
|
+
const { generate, result, isLoading, error, status, stop, reset } =
|
|
97
|
+
useGeneration<SpeechGenerateInput, TTSResult, TOnResult>(options)
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
generate: generate as (input: SpeechGenerateInput) => Promise<void>,
|
|
101
|
+
result,
|
|
102
|
+
isLoading,
|
|
103
|
+
error,
|
|
104
|
+
status,
|
|
105
|
+
stop,
|
|
106
|
+
reset,
|
|
107
|
+
}
|
|
108
|
+
}
|