Open History to see the last commit
1import { useGeneration } from './use-generation'
2import { reconstructSpeechResult } from '@tanstack/ai-client'
3import type {
4 UseGenerationOptions,
5 UseGenerationReturn,
6} from './use-generation'
7import type { StreamChunk, TTSResult } from '@tanstack/ai'
8import type {
9 AIDevtoolsDisplayOptions,
10 ConnectConnectionAdapter,
11 GenerationClientState,
12 GenerationFetcher,
13 GenerationPersistenceOptions,
14 InferGenerationOutputFromReturn,
15 SpeechGenerateInput,
16} from '@tanstack/ai-client'
17import type { Accessor } from 'solid-js'
18
19/**
20 * Options for the useGenerateSpeech hook.
21 *
22 * @template TOutput - The transformed output type (defaults to TTSResult)
23 */
24export interface UseGenerateSpeechOptions<TOutput = TTSResult> extends Pick<
25 UseGenerationOptions<SpeechGenerateInput, TTSResult, TOutput>,
26 | 'persistence'
27 | 'threadId'
28 | 'hydrateGeneration'
29 | 'joinRun'
30 | 'byok'
31 | 'byokProvider'
32> {
33 /** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */
34 connection?: ConnectConnectionAdapter
35 /** Direct async function for speech generation */
36 fetcher?: GenerationFetcher<SpeechGenerateInput, TTSResult>
37 /** Additional body parameters to send with connect-based adapter requests */
38 body?: Record<string, any>
39 /** Display options for TanStack AI Devtools. */
40 devtools?: AIDevtoolsDisplayOptions
41 /**
42 * Callback when speech is generated. Can optionally return a transformed value.
43 *
44 * - Return a non-null value to transform and store it as the result
45 * - Return `null` to keep the previous result unchanged
46 * - Return nothing (`void`) to store the raw result as-is
47 */
48 onResult?: (result: TTSResult) => TOutput | null | void
49 /** Callback when an error occurs */
50 onError?: (error: Error) => void
51 /** Callback when progress is reported (0-100) */
52 onProgress?: (progress: number, message?: string) => void
53 /** Callback for each stream chunk (connect-based adapter mode only) */
54 onChunk?: (chunk: StreamChunk) => void
55}
56
57/**
58 * Return type for the useGenerateSpeech hook.
59 *
60 * @template TOutput - The transformed output type (defaults to TTSResult)
61 */
62export interface UseGenerateSpeechReturn<TOutput = TTSResult> extends Omit<
63 UseGenerationReturn<TOutput>,
64 'generate'
65> {
66 /** Trigger speech generation */
67 generate: (input: SpeechGenerateInput) => Promise<void>
68 /** The TTS result containing audio data, or null */
69 result: Accessor<TOutput | null>
70 /** Whether generation is in progress */
71 isLoading: Accessor<boolean>
72 /** Current error, if any */
73 error: Accessor<Error | undefined>
74 /** Current state of the generation */
75 status: Accessor<GenerationClientState>
76}
77
78/**
79 * Solid hook for generating speech (text-to-speech) using AI models.
80 *
81 * @example
82 * ```tsx
83 * import { useGenerateSpeech } from '@tanstack/ai-solid'
84 * import { fetchServerSentEvents } from '@tanstack/ai-client'
85 *
86 * function SpeechGenerator() {
87 * const { generate, result, isLoading } = useGenerateSpeech({
88 * connection: fetchServerSentEvents('/api/generate/speech'),
89 * })
90 *
91 * return (
92 * <div>
93 * <button onClick={() => generate({ text: 'Hello world', voice: 'alloy' })}>
94 * Generate Speech
95 * </button>
96 * {result() && (
97 * <audio src={`data:audio/${result()!.format};base64,${result()!.audio}`} controls />
98 * )}
99 * </div>
100 * )
101 * }
102 * ```
103 */
104export function useGenerateSpeech<TTransformed = void>(
105 options: Omit<
106 UseGenerateSpeechOptions,
107 'onResult' | 'persistence' | 'threadId'
108 > & {
109 onResult?: (result: TTSResult) => TTransformed
110 } & GenerationPersistenceOptions,
111): UseGenerateSpeechReturn<
112 InferGenerationOutputFromReturn<TTSResult, TTransformed>
113> {
114 const devtools = {
115 ...options.devtools,
116 framework: 'solid',
117 hookName: 'useGenerateSpeech',
118 outputKind: 'audio' as const,
119 }
120 const generation = useGeneration<
121 SpeechGenerateInput,
122 TTSResult,
123 TTransformed
124 >({
125 ...options,
126 devtools,
127 reconstructResult: reconstructSpeechResult,
128 })
129
130 return generation
131}