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