import { useGeneration } from './use-generation' import { reconstructSpeechResult } from '@tanstack/ai-client' import type { UseGenerationOptions, UseGenerationReturn, } from './use-generation' import type { StreamChunk, TTSResult } from '@tanstack/ai' import type { AIDevtoolsDisplayOptions, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher, GenerationPersistenceOptions, InferGenerationOutputFromReturn, SpeechGenerateInput, } from '@tanstack/ai-client' import type { Accessor } from 'solid-js' /** * Options for the useGenerateSpeech hook. * * @template TOutput - The transformed output type (defaults to TTSResult) */ export interface UseGenerateSpeechOptions extends Pick< UseGenerationOptions, | 'persistence' | 'threadId' | 'hydrateGeneration' | 'joinRun' | 'byok' | 'byokProvider' > { /** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */ connection?: ConnectConnectionAdapter /** Direct async function for speech generation */ fetcher?: GenerationFetcher /** Additional body parameters to send with connect-based adapter requests */ body?: Record /** Display options for TanStack AI Devtools. */ devtools?: AIDevtoolsDisplayOptions /** * Callback when speech is generated. Can optionally return a transformed value. * * - Return a non-null value to transform and store it as the result * - Return `null` to keep the previous result unchanged * - Return nothing (`void`) to store the raw result as-is */ onResult?: (result: TTSResult) => TOutput | null | void /** Callback when an error occurs */ onError?: (error: Error) => void /** Callback when progress is reported (0-100) */ onProgress?: (progress: number, message?: string) => void /** Callback for each stream chunk (connect-based adapter mode only) */ onChunk?: (chunk: StreamChunk) => void } /** * Return type for the useGenerateSpeech hook. * * @template TOutput - The transformed output type (defaults to TTSResult) */ export interface UseGenerateSpeechReturn extends Omit< UseGenerationReturn, 'generate' > { /** Trigger speech generation */ generate: (input: SpeechGenerateInput) => Promise /** The TTS result containing audio data, or null */ result: Accessor /** Whether generation is in progress */ isLoading: Accessor /** Current error, if any */ error: Accessor /** Current state of the generation */ status: Accessor } /** * Solid hook for generating speech (text-to-speech) using AI models. * * @example * ```tsx * import { useGenerateSpeech } from '@tanstack/ai-solid' * import { fetchServerSentEvents } from '@tanstack/ai-client' * * function SpeechGenerator() { * const { generate, result, isLoading } = useGenerateSpeech({ * connection: fetchServerSentEvents('/api/generate/speech'), * }) * * return ( *
* * {result() && ( *
* ) * } * ``` */ export function useGenerateSpeech( options: Omit< UseGenerateSpeechOptions, 'onResult' | 'persistence' | 'threadId' > & { onResult?: (result: TTSResult) => TTransformed } & GenerationPersistenceOptions, ): UseGenerateSpeechReturn< InferGenerationOutputFromReturn > { const devtools = { ...options.devtools, framework: 'solid', hookName: 'useGenerateSpeech', outputKind: 'audio' as const, } const generation = useGeneration< SpeechGenerateInput, TTSResult, TTransformed >({ ...options, devtools, reconstructResult: reconstructSpeechResult, }) return generation }