@octanejs/tanstack-ai 0.0.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/LICENSE +21 -0
- package/README.md +123 -0
- package/package.json +54 -0
- package/src/index.ts +62 -0
- package/src/realtime-types.ts +146 -0
- package/src/types.ts +213 -0
- package/src/use-audio-recorder.tsrx +112 -0
- package/src/use-audio-recorder.tsrx.d.ts +44 -0
- package/src/use-chat.tsrx +363 -0
- package/src/use-chat.tsrx.d.ts +5 -0
- package/src/use-generate-audio.tsrx +125 -0
- package/src/use-generate-audio.tsrx.d.ts +88 -0
- package/src/use-generate-image.tsrx +127 -0
- package/src/use-generate-image.tsrx.d.ts +90 -0
- package/src/use-generate-speech.tsrx +121 -0
- package/src/use-generate-speech.tsrx.d.ts +84 -0
- package/src/use-generate-video.tsrx +245 -0
- package/src/use-generate-video.tsrx.d.ts +95 -0
- package/src/use-generation.tsrx +216 -0
- package/src/use-generation.tsrx.d.ts +81 -0
- package/src/use-mcp-app-bridge.tsrx +60 -0
- package/src/use-mcp-app-bridge.tsrx.d.ts +26 -0
- package/src/use-realtime-chat.tsrx +307 -0
- package/src/use-realtime-chat.tsrx.d.ts +43 -0
- package/src/use-summarize.tsrx +124 -0
- package/src/use-summarize.tsrx.d.ts +87 -0
- package/src/use-transcription.tsrx +131 -0
- package/src/use-transcription.tsrx.d.ts +93 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// Declaration companion generated from use-generate-audio.tsrx.
|
|
2
|
+
import type { AudioGenerationResult, StreamChunk } from '@tanstack/ai';
|
|
3
|
+
import type { AIDevtoolsDisplayOptions, AudioGenerateInput, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher, InferGenerationOutputFromReturn } from '@tanstack/ai-client';
|
|
4
|
+
/**
|
|
5
|
+
* Options for the useGenerateAudio hook.
|
|
6
|
+
*
|
|
7
|
+
* @template TOutput - The output type after optional transform (defaults to AudioGenerationResult)
|
|
8
|
+
*/
|
|
9
|
+
export interface UseGenerateAudioOptions<TOutput = AudioGenerationResult> {
|
|
10
|
+
/** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
11
|
+
connection?: ConnectConnectionAdapter;
|
|
12
|
+
/** Direct async function for audio generation */
|
|
13
|
+
fetcher?: GenerationFetcher<AudioGenerateInput, AudioGenerationResult>;
|
|
14
|
+
/** Unique identifier for this generation instance */
|
|
15
|
+
id?: string;
|
|
16
|
+
/** Additional body parameters to send with connect-based adapter requests */
|
|
17
|
+
body?: Record<string, any>;
|
|
18
|
+
/** Display options for TanStack AI Devtools. */
|
|
19
|
+
devtools?: AIDevtoolsDisplayOptions;
|
|
20
|
+
/**
|
|
21
|
+
* Callback when audio is generated. Can optionally return a transformed value.
|
|
22
|
+
*
|
|
23
|
+
* - Return a non-null value to transform and store it as the result
|
|
24
|
+
* - Return `null` to keep the previous result unchanged
|
|
25
|
+
* - Return nothing (`void`) to store the raw result as-is
|
|
26
|
+
*/
|
|
27
|
+
onResult?: (result: AudioGenerationResult) => TOutput | null | void;
|
|
28
|
+
/** Callback when an error occurs */
|
|
29
|
+
onError?: (error: Error) => void;
|
|
30
|
+
/** Callback when progress is reported (0-100) */
|
|
31
|
+
onProgress?: (progress: number, message?: string) => void;
|
|
32
|
+
/** Callback for each stream chunk (connect-based adapter mode only) */
|
|
33
|
+
onChunk?: (chunk: StreamChunk) => void;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Return type for the useGenerateAudio hook.
|
|
37
|
+
*
|
|
38
|
+
* @template TOutput - The output type (after optional transform)
|
|
39
|
+
*/
|
|
40
|
+
export interface UseGenerateAudioReturn<TOutput = AudioGenerationResult> {
|
|
41
|
+
/** Trigger audio generation */
|
|
42
|
+
generate: (input: AudioGenerateInput) => Promise<void>;
|
|
43
|
+
/** The generation result containing audio, or null */
|
|
44
|
+
result: TOutput | null;
|
|
45
|
+
/** Whether generation is in progress */
|
|
46
|
+
isLoading: boolean;
|
|
47
|
+
/** Current error, if any */
|
|
48
|
+
error: Error | undefined;
|
|
49
|
+
/** Current state of the generation */
|
|
50
|
+
status: GenerationClientState;
|
|
51
|
+
/** Abort the current generation */
|
|
52
|
+
stop: () => void;
|
|
53
|
+
/** Clear result, error, and return to idle */
|
|
54
|
+
reset: () => void;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Octane hook for generating audio (music, sound effects) using AI models.
|
|
58
|
+
*
|
|
59
|
+
* Supports two transport modes:
|
|
60
|
+
* - **ConnectConnectionAdapter** — Streaming transport (SSE, HTTP stream, custom)
|
|
61
|
+
* - **Fetcher** — Direct async function call
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```tsx
|
|
65
|
+
* import { useGenerateAudio } from '@octanejs/tanstack-ai'
|
|
66
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
67
|
+
*
|
|
68
|
+
* function AudioGenerator() {
|
|
69
|
+
* const { generate, result, isLoading, error, reset } = useGenerateAudio({
|
|
70
|
+
* connection: fetchServerSentEvents('/api/generate/audio'),
|
|
71
|
+
* })
|
|
72
|
+
*
|
|
73
|
+
* return (
|
|
74
|
+
* <div>
|
|
75
|
+
* <button onClick={() => generate({ prompt: 'An upbeat electronic track', duration: 10 })}>
|
|
76
|
+
* Generate
|
|
77
|
+
* </button>
|
|
78
|
+
* {isLoading && <p>Generating...</p>}
|
|
79
|
+
* {error && <p>Error: {error.message}</p>}
|
|
80
|
+
* {result?.audio.url && <audio src={result.audio.url} controls />}
|
|
81
|
+
* </div>
|
|
82
|
+
* )
|
|
83
|
+
* }
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export declare function useGenerateAudio<TTransformed = void>(options: Omit<UseGenerateAudioOptions, 'onResult'> & {
|
|
87
|
+
onResult?: (result: AudioGenerationResult) => TTransformed;
|
|
88
|
+
}): UseGenerateAudioReturn<InferGenerationOutputFromReturn<AudioGenerationResult, TTransformed>>;
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { useGeneration } from './use-generation.tsrx'
|
|
2
|
+
import type { ImageGenerationResult, StreamChunk } from '@tanstack/ai'
|
|
3
|
+
import type {
|
|
4
|
+
AIDevtoolsDisplayOptions,
|
|
5
|
+
ConnectConnectionAdapter,
|
|
6
|
+
GenerationClientState,
|
|
7
|
+
GenerationFetcher,
|
|
8
|
+
ImageGenerateInput,
|
|
9
|
+
InferGenerationOutputFromReturn,
|
|
10
|
+
} from '@tanstack/ai-client'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Options for the useGenerateImage hook.
|
|
14
|
+
*
|
|
15
|
+
* @template TOutput - The output type after optional transform (defaults to ImageGenerationResult)
|
|
16
|
+
*/
|
|
17
|
+
export interface UseGenerateImageOptions<TOutput = ImageGenerationResult> {
|
|
18
|
+
/** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
19
|
+
connection?: ConnectConnectionAdapter
|
|
20
|
+
/** Direct async function for image generation */
|
|
21
|
+
fetcher?: GenerationFetcher<ImageGenerateInput, ImageGenerationResult>
|
|
22
|
+
/** Unique identifier for this generation instance */
|
|
23
|
+
id?: string
|
|
24
|
+
/** Additional body parameters to send with connect-based adapter requests */
|
|
25
|
+
body?: Record<string, any>
|
|
26
|
+
/** Display options for TanStack AI Devtools. */
|
|
27
|
+
devtools?: AIDevtoolsDisplayOptions
|
|
28
|
+
/**
|
|
29
|
+
* Callback when images are generated. Can optionally return a transformed value.
|
|
30
|
+
*
|
|
31
|
+
* - Return a non-null value to transform and store it as the result
|
|
32
|
+
* - Return `null` to keep the previous result unchanged
|
|
33
|
+
* - Return nothing (`void`) to store the raw result as-is
|
|
34
|
+
*/
|
|
35
|
+
onResult?: (result: ImageGenerationResult) => TOutput | null | void
|
|
36
|
+
/** Callback when an error occurs */
|
|
37
|
+
onError?: (error: Error) => void
|
|
38
|
+
/** Callback when progress is reported (0-100) */
|
|
39
|
+
onProgress?: (progress: number, message?: string) => void
|
|
40
|
+
/** Callback for each stream chunk (connect-based adapter mode only) */
|
|
41
|
+
onChunk?: (chunk: StreamChunk) => void
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Return type for the useGenerateImage hook.
|
|
46
|
+
*
|
|
47
|
+
* @template TOutput - The output type (after optional transform)
|
|
48
|
+
*/
|
|
49
|
+
export interface UseGenerateImageReturn<TOutput = ImageGenerationResult> {
|
|
50
|
+
/** Trigger image generation */
|
|
51
|
+
generate: (input: ImageGenerateInput) => Promise<void>
|
|
52
|
+
/** The generation result containing images, or null */
|
|
53
|
+
result: TOutput | null
|
|
54
|
+
/** Whether generation is in progress */
|
|
55
|
+
isLoading: boolean
|
|
56
|
+
/** Current error, if any */
|
|
57
|
+
error: Error | undefined
|
|
58
|
+
/** Current state of the generation */
|
|
59
|
+
status: GenerationClientState
|
|
60
|
+
/** Abort the current generation */
|
|
61
|
+
stop: () => void
|
|
62
|
+
/** Clear result, error, and return to idle */
|
|
63
|
+
reset: () => void
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Octane hook for generating images using AI models.
|
|
68
|
+
*
|
|
69
|
+
* Supports two transport modes:
|
|
70
|
+
* - **ConnectConnectionAdapter** — Streaming transport (SSE, HTTP stream, custom)
|
|
71
|
+
* - **Fetcher** — Direct async function call
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```tsx
|
|
75
|
+
* import { useGenerateImage } from '@octanejs/tanstack-ai'
|
|
76
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
77
|
+
*
|
|
78
|
+
* function ImageGenerator() {
|
|
79
|
+
* const { generate, result, isLoading, error, reset } = useGenerateImage({
|
|
80
|
+
* connection: fetchServerSentEvents('/api/generate/image'),
|
|
81
|
+
* })
|
|
82
|
+
*
|
|
83
|
+
* return (
|
|
84
|
+
* <div>
|
|
85
|
+
* <button onClick={() => generate({ prompt: 'A sunset over mountains' })}>
|
|
86
|
+
* Generate
|
|
87
|
+
* </button>
|
|
88
|
+
* {isLoading && <p>Generating...</p>}
|
|
89
|
+
* {error && <p>Error: {error.message}</p>}
|
|
90
|
+
* {result?.images.map((img, i) => (
|
|
91
|
+
* <img key={i} src={img.url || `data:image/png;base64,${img.b64Json}`} />
|
|
92
|
+
* ))}
|
|
93
|
+
* </div>
|
|
94
|
+
* )
|
|
95
|
+
* }
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
export function useGenerateImage<TTransformed = void>(
|
|
99
|
+
options: Omit<UseGenerateImageOptions, 'onResult'> & {
|
|
100
|
+
onResult?: (result: ImageGenerationResult) => TTransformed
|
|
101
|
+
},
|
|
102
|
+
): UseGenerateImageReturn<
|
|
103
|
+
InferGenerationOutputFromReturn<ImageGenerationResult, TTransformed>
|
|
104
|
+
> {
|
|
105
|
+
const devtools = {
|
|
106
|
+
...options.devtools,
|
|
107
|
+
// OCTANE DIVERGENCE: identify this binding as Octane (not 'react') in TanStack AI Devtools.
|
|
108
|
+
framework: 'octane',
|
|
109
|
+
hookName: 'useGenerateImage',
|
|
110
|
+
outputKind: 'image' as const,
|
|
111
|
+
}
|
|
112
|
+
const { generate, result, isLoading, error, status, stop, reset } =
|
|
113
|
+
useGeneration<ImageGenerateInput, ImageGenerationResult, TTransformed>({
|
|
114
|
+
...options,
|
|
115
|
+
devtools,
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
generate: generate as (input: ImageGenerateInput) => Promise<void>,
|
|
120
|
+
result,
|
|
121
|
+
isLoading,
|
|
122
|
+
error,
|
|
123
|
+
status,
|
|
124
|
+
stop,
|
|
125
|
+
reset,
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// Declaration companion generated from use-generate-image.tsrx.
|
|
2
|
+
import type { ImageGenerationResult, StreamChunk } from '@tanstack/ai';
|
|
3
|
+
import type { AIDevtoolsDisplayOptions, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher, ImageGenerateInput, InferGenerationOutputFromReturn } from '@tanstack/ai-client';
|
|
4
|
+
/**
|
|
5
|
+
* Options for the useGenerateImage hook.
|
|
6
|
+
*
|
|
7
|
+
* @template TOutput - The output type after optional transform (defaults to ImageGenerationResult)
|
|
8
|
+
*/
|
|
9
|
+
export interface UseGenerateImageOptions<TOutput = ImageGenerationResult> {
|
|
10
|
+
/** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
11
|
+
connection?: ConnectConnectionAdapter;
|
|
12
|
+
/** Direct async function for image generation */
|
|
13
|
+
fetcher?: GenerationFetcher<ImageGenerateInput, ImageGenerationResult>;
|
|
14
|
+
/** Unique identifier for this generation instance */
|
|
15
|
+
id?: string;
|
|
16
|
+
/** Additional body parameters to send with connect-based adapter requests */
|
|
17
|
+
body?: Record<string, any>;
|
|
18
|
+
/** Display options for TanStack AI Devtools. */
|
|
19
|
+
devtools?: AIDevtoolsDisplayOptions;
|
|
20
|
+
/**
|
|
21
|
+
* Callback when images are generated. Can optionally return a transformed value.
|
|
22
|
+
*
|
|
23
|
+
* - Return a non-null value to transform and store it as the result
|
|
24
|
+
* - Return `null` to keep the previous result unchanged
|
|
25
|
+
* - Return nothing (`void`) to store the raw result as-is
|
|
26
|
+
*/
|
|
27
|
+
onResult?: (result: ImageGenerationResult) => TOutput | null | void;
|
|
28
|
+
/** Callback when an error occurs */
|
|
29
|
+
onError?: (error: Error) => void;
|
|
30
|
+
/** Callback when progress is reported (0-100) */
|
|
31
|
+
onProgress?: (progress: number, message?: string) => void;
|
|
32
|
+
/** Callback for each stream chunk (connect-based adapter mode only) */
|
|
33
|
+
onChunk?: (chunk: StreamChunk) => void;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Return type for the useGenerateImage hook.
|
|
37
|
+
*
|
|
38
|
+
* @template TOutput - The output type (after optional transform)
|
|
39
|
+
*/
|
|
40
|
+
export interface UseGenerateImageReturn<TOutput = ImageGenerationResult> {
|
|
41
|
+
/** Trigger image generation */
|
|
42
|
+
generate: (input: ImageGenerateInput) => Promise<void>;
|
|
43
|
+
/** The generation result containing images, or null */
|
|
44
|
+
result: TOutput | null;
|
|
45
|
+
/** Whether generation is in progress */
|
|
46
|
+
isLoading: boolean;
|
|
47
|
+
/** Current error, if any */
|
|
48
|
+
error: Error | undefined;
|
|
49
|
+
/** Current state of the generation */
|
|
50
|
+
status: GenerationClientState;
|
|
51
|
+
/** Abort the current generation */
|
|
52
|
+
stop: () => void;
|
|
53
|
+
/** Clear result, error, and return to idle */
|
|
54
|
+
reset: () => void;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Octane hook for generating images using AI models.
|
|
58
|
+
*
|
|
59
|
+
* Supports two transport modes:
|
|
60
|
+
* - **ConnectConnectionAdapter** — Streaming transport (SSE, HTTP stream, custom)
|
|
61
|
+
* - **Fetcher** — Direct async function call
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```tsx
|
|
65
|
+
* import { useGenerateImage } from '@octanejs/tanstack-ai'
|
|
66
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
67
|
+
*
|
|
68
|
+
* function ImageGenerator() {
|
|
69
|
+
* const { generate, result, isLoading, error, reset } = useGenerateImage({
|
|
70
|
+
* connection: fetchServerSentEvents('/api/generate/image'),
|
|
71
|
+
* })
|
|
72
|
+
*
|
|
73
|
+
* return (
|
|
74
|
+
* <div>
|
|
75
|
+
* <button onClick={() => generate({ prompt: 'A sunset over mountains' })}>
|
|
76
|
+
* Generate
|
|
77
|
+
* </button>
|
|
78
|
+
* {isLoading && <p>Generating...</p>}
|
|
79
|
+
* {error && <p>Error: {error.message}</p>}
|
|
80
|
+
* {result?.images.map((img, i) => (
|
|
81
|
+
* <img key={i} src={img.url || `data:image/png;base64,${img.b64Json}`} />
|
|
82
|
+
* ))}
|
|
83
|
+
* </div>
|
|
84
|
+
* )
|
|
85
|
+
* }
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
export declare function useGenerateImage<TTransformed = void>(options: Omit<UseGenerateImageOptions, 'onResult'> & {
|
|
89
|
+
onResult?: (result: ImageGenerationResult) => TTransformed;
|
|
90
|
+
}): UseGenerateImageReturn<InferGenerationOutputFromReturn<ImageGenerationResult, TTransformed>>;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { useGeneration } from './use-generation.tsrx'
|
|
2
|
+
import type { StreamChunk, TTSResult } from '@tanstack/ai'
|
|
3
|
+
import type {
|
|
4
|
+
AIDevtoolsDisplayOptions,
|
|
5
|
+
ConnectConnectionAdapter,
|
|
6
|
+
GenerationClientState,
|
|
7
|
+
GenerationFetcher,
|
|
8
|
+
InferGenerationOutputFromReturn,
|
|
9
|
+
SpeechGenerateInput,
|
|
10
|
+
} from '@tanstack/ai-client'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Options for the useGenerateSpeech hook.
|
|
14
|
+
*
|
|
15
|
+
* @template TOutput - The output type after optional transform (defaults to TTSResult)
|
|
16
|
+
*/
|
|
17
|
+
export interface UseGenerateSpeechOptions<TOutput = TTSResult> {
|
|
18
|
+
/** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
19
|
+
connection?: ConnectConnectionAdapter
|
|
20
|
+
/** Direct async function for speech generation */
|
|
21
|
+
fetcher?: GenerationFetcher<SpeechGenerateInput, TTSResult>
|
|
22
|
+
/** Unique identifier for this generation instance */
|
|
23
|
+
id?: string
|
|
24
|
+
/** Additional body parameters to send with connect-based adapter requests */
|
|
25
|
+
body?: Record<string, any>
|
|
26
|
+
/** Display options for TanStack AI Devtools. */
|
|
27
|
+
devtools?: AIDevtoolsDisplayOptions
|
|
28
|
+
/**
|
|
29
|
+
* Callback when speech is generated. Can optionally return a transformed value.
|
|
30
|
+
*
|
|
31
|
+
* - Return a non-null value to transform and store it as the result
|
|
32
|
+
* - Return `null` to keep the previous result unchanged
|
|
33
|
+
* - Return nothing (`void`) to store the raw result as-is
|
|
34
|
+
*/
|
|
35
|
+
onResult?: (result: TTSResult) => TOutput | null | void
|
|
36
|
+
/** Callback when an error occurs */
|
|
37
|
+
onError?: (error: Error) => void
|
|
38
|
+
/** Callback when progress is reported (0-100) */
|
|
39
|
+
onProgress?: (progress: number, message?: string) => void
|
|
40
|
+
/** Callback for each stream chunk (connect-based adapter mode only) */
|
|
41
|
+
onChunk?: (chunk: StreamChunk) => void
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Return type for the useGenerateSpeech hook.
|
|
46
|
+
*
|
|
47
|
+
* @template TOutput - The output type (after optional transform)
|
|
48
|
+
*/
|
|
49
|
+
export interface UseGenerateSpeechReturn<TOutput = TTSResult> {
|
|
50
|
+
/** Trigger speech generation */
|
|
51
|
+
generate: (input: SpeechGenerateInput) => Promise<void>
|
|
52
|
+
/** The TTS result containing audio data, or null */
|
|
53
|
+
result: TOutput | null
|
|
54
|
+
/** Whether generation is in progress */
|
|
55
|
+
isLoading: boolean
|
|
56
|
+
/** Current error, if any */
|
|
57
|
+
error: Error | undefined
|
|
58
|
+
/** Current state of the generation */
|
|
59
|
+
status: GenerationClientState
|
|
60
|
+
/** Abort the current generation */
|
|
61
|
+
stop: () => void
|
|
62
|
+
/** Clear result, error, and return to idle */
|
|
63
|
+
reset: () => void
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Octane hook for generating speech (text-to-speech) using AI models.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```tsx
|
|
71
|
+
* import { useGenerateSpeech } from '@octanejs/tanstack-ai'
|
|
72
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
73
|
+
*
|
|
74
|
+
* function SpeechGenerator() {
|
|
75
|
+
* const { generate, result, isLoading } = useGenerateSpeech({
|
|
76
|
+
* connection: fetchServerSentEvents('/api/generate/speech'),
|
|
77
|
+
* })
|
|
78
|
+
*
|
|
79
|
+
* return (
|
|
80
|
+
* <div>
|
|
81
|
+
* <button onClick={() => generate({ text: 'Hello world', voice: 'alloy' })}>
|
|
82
|
+
* Generate Speech
|
|
83
|
+
* </button>
|
|
84
|
+
* {result && (
|
|
85
|
+
* <audio src={`data:audio/${result.format};base64,${result.audio}`} controls />
|
|
86
|
+
* )}
|
|
87
|
+
* </div>
|
|
88
|
+
* )
|
|
89
|
+
* }
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
export function useGenerateSpeech<TTransformed = void>(
|
|
93
|
+
options: Omit<UseGenerateSpeechOptions, 'onResult'> & {
|
|
94
|
+
onResult?: (result: TTSResult) => TTransformed
|
|
95
|
+
},
|
|
96
|
+
): UseGenerateSpeechReturn<
|
|
97
|
+
InferGenerationOutputFromReturn<TTSResult, TTransformed>
|
|
98
|
+
> {
|
|
99
|
+
const devtools = {
|
|
100
|
+
...options.devtools,
|
|
101
|
+
// OCTANE DIVERGENCE: identify this binding as Octane (not 'react') in TanStack AI Devtools.
|
|
102
|
+
framework: 'octane',
|
|
103
|
+
hookName: 'useGenerateSpeech',
|
|
104
|
+
outputKind: 'audio' as const,
|
|
105
|
+
}
|
|
106
|
+
const { generate, result, isLoading, error, status, stop, reset } =
|
|
107
|
+
useGeneration<SpeechGenerateInput, TTSResult, TTransformed>({
|
|
108
|
+
...options,
|
|
109
|
+
devtools,
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
generate: generate as (input: SpeechGenerateInput) => Promise<void>,
|
|
114
|
+
result,
|
|
115
|
+
isLoading,
|
|
116
|
+
error,
|
|
117
|
+
status,
|
|
118
|
+
stop,
|
|
119
|
+
reset,
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Declaration companion generated from use-generate-speech.tsrx.
|
|
2
|
+
import type { StreamChunk, TTSResult } from '@tanstack/ai';
|
|
3
|
+
import type { AIDevtoolsDisplayOptions, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher, InferGenerationOutputFromReturn, SpeechGenerateInput } from '@tanstack/ai-client';
|
|
4
|
+
/**
|
|
5
|
+
* Options for the useGenerateSpeech hook.
|
|
6
|
+
*
|
|
7
|
+
* @template TOutput - The output type after optional transform (defaults to TTSResult)
|
|
8
|
+
*/
|
|
9
|
+
export interface UseGenerateSpeechOptions<TOutput = TTSResult> {
|
|
10
|
+
/** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
11
|
+
connection?: ConnectConnectionAdapter;
|
|
12
|
+
/** Direct async function for speech generation */
|
|
13
|
+
fetcher?: GenerationFetcher<SpeechGenerateInput, TTSResult>;
|
|
14
|
+
/** Unique identifier for this generation instance */
|
|
15
|
+
id?: string;
|
|
16
|
+
/** Additional body parameters to send with connect-based adapter requests */
|
|
17
|
+
body?: Record<string, any>;
|
|
18
|
+
/** Display options for TanStack AI Devtools. */
|
|
19
|
+
devtools?: AIDevtoolsDisplayOptions;
|
|
20
|
+
/**
|
|
21
|
+
* Callback when speech is generated. Can optionally return a transformed value.
|
|
22
|
+
*
|
|
23
|
+
* - Return a non-null value to transform and store it as the result
|
|
24
|
+
* - Return `null` to keep the previous result unchanged
|
|
25
|
+
* - Return nothing (`void`) to store the raw result as-is
|
|
26
|
+
*/
|
|
27
|
+
onResult?: (result: TTSResult) => TOutput | null | void;
|
|
28
|
+
/** Callback when an error occurs */
|
|
29
|
+
onError?: (error: Error) => void;
|
|
30
|
+
/** Callback when progress is reported (0-100) */
|
|
31
|
+
onProgress?: (progress: number, message?: string) => void;
|
|
32
|
+
/** Callback for each stream chunk (connect-based adapter mode only) */
|
|
33
|
+
onChunk?: (chunk: StreamChunk) => void;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Return type for the useGenerateSpeech hook.
|
|
37
|
+
*
|
|
38
|
+
* @template TOutput - The output type (after optional transform)
|
|
39
|
+
*/
|
|
40
|
+
export interface UseGenerateSpeechReturn<TOutput = TTSResult> {
|
|
41
|
+
/** Trigger speech generation */
|
|
42
|
+
generate: (input: SpeechGenerateInput) => Promise<void>;
|
|
43
|
+
/** The TTS result containing audio data, or null */
|
|
44
|
+
result: TOutput | null;
|
|
45
|
+
/** Whether generation is in progress */
|
|
46
|
+
isLoading: boolean;
|
|
47
|
+
/** Current error, if any */
|
|
48
|
+
error: Error | undefined;
|
|
49
|
+
/** Current state of the generation */
|
|
50
|
+
status: GenerationClientState;
|
|
51
|
+
/** Abort the current generation */
|
|
52
|
+
stop: () => void;
|
|
53
|
+
/** Clear result, error, and return to idle */
|
|
54
|
+
reset: () => void;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Octane hook for generating speech (text-to-speech) using AI models.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```tsx
|
|
61
|
+
* import { useGenerateSpeech } from '@octanejs/tanstack-ai'
|
|
62
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
63
|
+
*
|
|
64
|
+
* function SpeechGenerator() {
|
|
65
|
+
* const { generate, result, isLoading } = useGenerateSpeech({
|
|
66
|
+
* connection: fetchServerSentEvents('/api/generate/speech'),
|
|
67
|
+
* })
|
|
68
|
+
*
|
|
69
|
+
* return (
|
|
70
|
+
* <div>
|
|
71
|
+
* <button onClick={() => generate({ text: 'Hello world', voice: 'alloy' })}>
|
|
72
|
+
* Generate Speech
|
|
73
|
+
* </button>
|
|
74
|
+
* {result && (
|
|
75
|
+
* <audio src={`data:audio/${result.format};base64,${result.audio}`} controls />
|
|
76
|
+
* )}
|
|
77
|
+
* </div>
|
|
78
|
+
* )
|
|
79
|
+
* }
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export declare function useGenerateSpeech<TTransformed = void>(options: Omit<UseGenerateSpeechOptions, 'onResult'> & {
|
|
83
|
+
onResult?: (result: TTSResult) => TTransformed;
|
|
84
|
+
}): UseGenerateSpeechReturn<InferGenerationOutputFromReturn<TTSResult, TTransformed>>;
|