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