Open History to see the last commit
1import { injectGeneration } from './inject-generation'
2import { reconstructAudioResult } from '@tanstack/ai-client'
3import type { AudioGenerationResult, StreamChunk } from '@tanstack/ai'
4import type {
5 AIDevtoolsDisplayOptions,
6 AudioGenerateInput,
7 ConnectConnectionAdapter,
8 GenerationClientState,
9 GenerationFetcher,
10 GenerationPersistenceOptions,
11 InferGenerationOutputFromReturn,
12} from '@tanstack/ai-client'
13import type { Signal } from '@angular/core'
14import type { ReactiveOption } from './internal/to-reactive'
15import type {
16 InjectGenerationOptions,
17 InjectGenerationResult,
18} from './inject-generation'
19
20/**
21 * Options for the injectGenerateAudio injectable.
22 *
23 * @template TOutput - The output type after optional transform (defaults to AudioGenerationResult)
24 */
25export interface InjectGenerateAudioOptions<
26 TOutput = AudioGenerationResult,
27> extends Pick<
28 InjectGenerationOptions<AudioGenerateInput, AudioGenerationResult, TOutput>,
29 | 'persistence'
30 | 'threadId'
31 | 'hydrateGeneration'
32 | 'joinRun'
33 | 'byok'
34 | 'byokProvider'
35> {
36 /** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */
37 connection?: ConnectConnectionAdapter
38 /** Direct async function for audio generation */
39 fetcher?: GenerationFetcher<AudioGenerateInput, AudioGenerationResult>
40 /** Additional body parameters to send with connect-based adapter requests. Reactive. */
41 body?: ReactiveOption<Record<string, any>>
42 /** Display options for TanStack AI Devtools. */
43 devtools?: AIDevtoolsDisplayOptions
44 /**
45 * Callback when audio is generated. Can optionally return a transformed value.
46 *
47 * - Return a non-null value to transform and store it as the result
48 * - Return `null` to keep the previous result unchanged
49 * - Return nothing (`void`) to store the raw result as-is
50 */
51 onResult?: (result: AudioGenerationResult) => TOutput | null | void
52 /** Callback when an error occurs */
53 onError?: (error: Error) => void
54 /** Callback when progress is reported (0-100) */
55 onProgress?: (progress: number, message?: string) => void
56 /** Callback for each stream chunk (connect-based adapter mode only) */
57 onChunk?: (chunk: StreamChunk) => void
58}
59
60/**
61 * Return type for the injectGenerateAudio injectable.
62 *
63 * @template TOutput - The output type (after optional transform)
64 */
65export interface InjectGenerateAudioResult<
66 TOutput = AudioGenerationResult,
67> extends Omit<InjectGenerationResult<TOutput>, 'generate'> {
68 /** Trigger audio generation */
69 generate: (input: AudioGenerateInput) => Promise<void>
70 /** The generation result containing audio, or null */
71 result: Signal<TOutput | null>
72 /** Whether generation is in progress */
73 isLoading: Signal<boolean>
74 /** Current error, if any */
75 error: Signal<Error | undefined>
76 /** Current state of the generation */
77 status: Signal<GenerationClientState>
78}
79
80/**
81 * Angular injectable for generating audio (music, sound effects) using AI models.
82 *
83 * @example
84 * ```typescript
85 * import { Component } from '@angular/core'
86 * import { injectGenerateAudio } from '@tanstack/ai-angular'
87 * import { fetchServerSentEvents } from '@tanstack/ai-client'
88 *
89 * @Component({
90 * selector: 'app-audio',
91 * template: `
92 * <button (click)="generate({ prompt: 'An upbeat electronic track', duration: 10 })">
93 * Generate
94 * </button>
95 * @if (result()) {
96 * <audio [src]="result()!.audio.url" controls></audio>
97 * }
98 * `,
99 * })
100 * export class AudioComponent {
101 * private gen = injectGenerateAudio({
102 * connection: fetchServerSentEvents('/api/generate/audio'),
103 * })
104 *
105 * generate = this.gen.generate
106 * result = this.gen.result
107 * isLoading = this.gen.isLoading
108 * }
109 * ```
110 */
111export function injectGenerateAudio<TTransformed = void>(
112 options: Omit<
113 InjectGenerateAudioOptions,
114 'onResult' | 'persistence' | 'threadId'
115 > & {
116 onResult?: (result: AudioGenerationResult) => TTransformed
117 } & GenerationPersistenceOptions,
118): InjectGenerateAudioResult<
119 InferGenerationOutputFromReturn<AudioGenerationResult, TTransformed>
120> {
121 const devtools = {
122 ...options.devtools,
123 framework: 'angular',
124 hookName: 'injectGenerateAudio',
125 outputKind: 'audio' as const,
126 }
127 const generation = injectGeneration<
128 AudioGenerateInput,
129 AudioGenerationResult,
130 TTransformed
131 >({
132 ...options,
133 devtools,
134 reconstructResult: reconstructAudioResult,
135 })
136
137 return generation
138}