-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathgenerateMusic.ts
More file actions
463 lines (420 loc) · 16.3 KB
/
Copy pathgenerateMusic.ts
File metadata and controls
463 lines (420 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/**
* @file generateMusic.ts
* Provider-agnostic music generation for the AgentOS high-level API.
*
* Resolves a music generation provider from explicit `opts.provider`, or by
* probing environment variables in priority order:
* `SUNO_API_KEY` -> `UDIO_API_KEY` -> `STABILITY_API_KEY` -> `REPLICATE_API_TOKEN` ->
* `FAL_API_KEY` -> `MINIMAX_API_KEY` -> (local MusicGen — no key required).
*
* When multiple music-capable providers are configured (via env vars), the
* primary provider is wrapped in a {@link FallbackAudioProxy} so that a
* transient failure automatically retries on the next available provider.
*
* Supports an optional {@link MediaProviderPreference} to reorder or filter
* the fallback chain at the caller's discretion.
*/
import { EventEmitter } from 'events';
import { createAudioProvider, hasAudioProviderFactory } from '../io/media/audio/index.js';
import { FallbackAudioProxy } from '../io/media/audio/FallbackAudioProxy.js';
import type { IAudioGenerator } from '../io/media/audio/IAudioGenerator.js';
import type {
AudioResult,
AudioOutputFormat,
AudioProgressEvent,
} from '../io/media/audio/types.js';
import {
resolveProviderChain,
resolveProviderOrder,
type MediaProviderPreference,
} from '../io/media/ProviderPreferences.js';
import { attachUsageAttributes, toTurnMetricUsage } from './observability.js';
import { recordAgentOSUsage, type AgentOSUsageLedgerOptions } from './runtime/usageLedger.js';
import { recordAgentOSTurnMetrics, withAgentOSSpan } from '../safety/evaluation/observability/otel.js';
// ---------------------------------------------------------------------------
// Music provider fallback chain builder
// ---------------------------------------------------------------------------
/**
* Env-var to provider-id mapping used to detect which music providers have
* credentials configured in the current environment. Order determines
* fallback priority (first = highest priority).
*
* The final entry (`musicgen-local`) has no env key requirement — it runs
* on the local machine via HuggingFace Transformers.js.
*/
const MUSIC_PROVIDER_ENV_MAP: Array<{ envKey: string | null; providerId: string }> = [
{ envKey: 'SUNO_API_KEY', providerId: 'suno' },
{ envKey: 'UDIO_API_KEY', providerId: 'udio' },
{ envKey: 'STABILITY_API_KEY', providerId: 'stable-audio' },
{ envKey: 'REPLICATE_API_TOKEN', providerId: 'replicate-audio' },
{ envKey: 'FAL_API_KEY', providerId: 'fal-audio' },
{ envKey: 'MINIMAX_API_KEY', providerId: 'minimax-music' },
{ envKey: null, providerId: 'musicgen-local' },
];
/** Shared emitter for music fallback events (singleton per process). */
const musicFallbackEmitter = new EventEmitter();
function emitAudioProgress(
callback: ((event: AudioProgressEvent) => void) | undefined,
status: AudioProgressEvent['status'],
progress: number,
message: string,
): void {
if (!callback) return;
try {
callback({ status, progress, message });
} catch {
// Progress callbacks are best-effort and must not break generation.
}
}
/**
* Detects the first available music provider from environment variables.
*
* Scans the {@link MUSIC_PROVIDER_ENV_MAP} in priority order and returns
* the first provider whose API key env var is set (or whose factory is
* registered with no key requirement, e.g. `musicgen-local`).
*
* @returns The provider ID and API key, or `undefined` when no music
* provider is available.
*/
function autoDetectMusicProvider(): { providerId: string; apiKey: string } | undefined {
const providerId = detectAvailableMusicProviders()[0];
if (!providerId) return undefined;
const entry = MUSIC_PROVIDER_ENV_MAP.find((candidate) => candidate.providerId === providerId);
return {
providerId,
apiKey: entry?.envKey ? (process.env[entry.envKey] ?? '') : '',
};
}
/**
* Detects all music providers available in the current environment.
*
* @returns Provider IDs in priority order.
*/
function detectAvailableMusicProviders(): string[] {
const available: string[] = [];
for (const { envKey, providerId } of MUSIC_PROVIDER_ENV_MAP) {
if (!hasAudioProviderFactory(providerId)) continue;
if (envKey !== null && !process.env[envKey]) continue;
available.push(providerId);
}
return available;
}
/**
* Detects all music providers with valid credentials in the environment
* and returns their provider IDs in priority order, excluding the primary.
*
* @param primaryProviderId - The provider that was explicitly selected; it
* is excluded from the fallback list since it is already first in line.
* @returns An array of provider IDs suitable for fallback, in priority order.
*/
function detectFallbackMusicProviders(primaryProviderId: string): string[] {
const fallbacks: string[] = [];
for (const { envKey, providerId } of MUSIC_PROVIDER_ENV_MAP) {
if (providerId === primaryProviderId) continue;
if (!hasAudioProviderFactory(providerId)) continue;
if (envKey !== null && !process.env[envKey]) continue;
fallbacks.push(providerId);
}
return fallbacks;
}
/**
* Resolves the API key environment variable name for a known music provider.
*
* @param providerId - The provider identifier.
* @returns The environment variable name, or a generic fallback.
*/
function envKeyForMusicProvider(providerId: string): string {
const entry = MUSIC_PROVIDER_ENV_MAP.find((e) => e.providerId === providerId);
if (entry?.envKey) return entry.envKey;
return `${providerId.toUpperCase().replace(/-/g, '_')}_API_KEY`;
}
/**
* Creates an {@link IAudioGenerator} for a resolved music provider chain,
* optionally wrapped in a {@link FallbackAudioProxy} when multiple
* music-capable providers are available.
*
* @param providerChain - Ordered provider IDs with the primary first and
* optional fallbacks after it.
* @param apiKey - API key for the primary provider.
* @param modelId - Optional model identifier override.
* @returns An initialised audio provider (possibly a fallback proxy).
*/
async function createMusicProviderWithFallback(
providerChain: string[],
apiKey: string,
modelId?: string,
timeoutMs?: number,
): Promise<IAudioGenerator> {
if (providerChain.length === 0) {
throw new Error('No music providers available in the resolved provider chain.');
}
const [providerId, ...fallbackIds] = providerChain;
const primary = createAudioProvider(providerId);
await primary.initialize({
apiKey,
...(modelId ? { defaultModelId: modelId } : {}),
...(timeoutMs ? { timeoutMs } : {}),
});
if (fallbackIds.length === 0) {
return primary;
}
// Build and initialise fallback providers. Failures during init are
// silently skipped — the provider simply won't be part of the chain.
const chain: IAudioGenerator[] = [primary];
for (const fbId of fallbackIds) {
try {
const entry = MUSIC_PROVIDER_ENV_MAP.find((e) => e.providerId === fbId);
const fbKey = entry?.envKey ? (process.env[entry.envKey] ?? '') : '';
const fb = createAudioProvider(fbId);
await fb.initialize({
apiKey: fbKey,
...(timeoutMs ? { timeoutMs } : {}),
});
chain.push(fb);
} catch {
// Skip providers that fail to initialise (missing creds, etc.).
}
}
if (chain.length <= 1) {
return primary;
}
return new FallbackAudioProxy(chain, musicFallbackEmitter);
}
// ---------------------------------------------------------------------------
// Public options / result types
// ---------------------------------------------------------------------------
/**
* Options for a {@link generateMusic} call.
*
* At minimum, a `prompt` is required. The provider is resolved from
* `opts.provider`, `opts.apiKey`, or the first music-capable env var found
* (`SUNO_API_KEY` -> `UDIO_API_KEY` -> `STABILITY_API_KEY` -> `REPLICATE_API_TOKEN` ->
* `FAL_API_KEY` -> `MINIMAX_API_KEY` -> local MusicGen).
*/
export interface GenerateMusicOptions {
/** Text prompt describing the desired musical composition. */
prompt: string;
/**
* Explicit provider identifier (e.g. `"suno"`, `"stable-audio"`, `"musicgen-local"`).
* When omitted, auto-detection from environment variables is used.
*/
provider?: string;
/**
* Model identifier within the provider (e.g. `"suno-v3.5"`,
* `"stable-audio-open-1.0"`). When omitted, the provider's default
* model is used.
*/
model?: string;
/** Desired output duration in seconds. Provider limits vary. */
durationSec?: number;
/** Negative prompt describing musical elements to avoid. */
negativePrompt?: string;
/** Output audio format (e.g. `"mp3"`, `"wav"`). Defaults to provider default. */
outputFormat?: AudioOutputFormat;
/** Random seed for reproducible generation (provider-dependent). */
seed?: number;
/**
* Maximum time in milliseconds to wait for generation to complete.
* Provider-dependent — polling providers enforce this directly.
*/
timeoutMs?: number;
/** Number of audio clips to generate. Defaults to 1. */
n?: number;
/**
* Optional progress callback invoked during long-running generation.
* Called with an {@link AudioProgressEvent} at each status transition.
*/
onProgress?: (event: AudioProgressEvent) => void;
/** Override the provider API key instead of reading from env vars. */
apiKey?: string;
/** Optional user identifier forwarded to the provider for billing. */
userId?: string;
/** Arbitrary provider-specific options. */
providerOptions?: Record<string, unknown>;
/**
* Provider preferences for reordering or filtering the fallback chain.
* When supplied, the available providers are reordered according to
* `preferred` and filtered by `blocked` before building the chain.
*/
providerPreferences?: MediaProviderPreference;
/** Optional durable usage ledger configuration for accounting. */
usageLedger?: AgentOSUsageLedgerOptions;
}
/**
* The result returned by {@link generateMusic}.
*
* Wraps the core {@link AudioResult} with a simpler, AI-SDK-style shape.
*/
export interface GenerateMusicResult {
/** Model identifier reported by the provider. */
model: string;
/** Provider identifier (e.g. `"suno"`, `"stable-audio"`). */
provider: string;
/** Unix timestamp (seconds) when the audio was created. */
created: number;
/** Array of generated audio objects containing URLs or base64 data. */
audio: AudioResult['audio'];
/** Usage / billing information, if available. */
usage?: AudioResult['usage'];
}
// ---------------------------------------------------------------------------
// Main API function
// ---------------------------------------------------------------------------
/**
* Generates music using a provider-agnostic interface.
*
* Resolves provider credentials via explicit options or environment variable
* auto-detection, initialises the matching audio provider (optionally wrapped
* in a fallback chain), and returns a normalised {@link GenerateMusicResult}.
*
* @param opts - Music generation options.
* @returns A promise resolving to the generation result with audio data and metadata.
*
* @example
* ```ts
* const result = await generateMusic({
* prompt: 'Upbeat lo-fi hip hop beat with vinyl crackle and mellow piano',
* durationSec: 60,
* });
* console.log(result.audio[0].url);
* ```
*/
export async function generateMusic(opts: GenerateMusicOptions): Promise<GenerateMusicResult> {
const startedAt = Date.now();
let metricStatus: 'ok' | 'error' = 'ok';
let metricUsage: AudioResult['usage'];
let metricProviderId: string | undefined;
let metricModelId: string | undefined;
try {
return await withAgentOSSpan('agentos.api.generate_music', async (span) => {
// --- Resolve provider ---
let providerId: string;
let apiKey: string;
let providerChain: string[];
if (opts.provider) {
providerId = opts.provider;
apiKey =
opts.apiKey ??
process.env[envKeyForMusicProvider(providerId)] ??
'';
// Local providers don't need keys
if (!apiKey && providerId !== 'musicgen-local') {
throw new Error(
`No API key for music provider "${providerId}". Set ${envKeyForMusicProvider(providerId)} or pass apiKey.`,
);
}
let fallbackIds = detectFallbackMusicProviders(providerId);
if (opts.providerPreferences) {
const ordered = resolveProviderOrder([providerId, ...fallbackIds], opts.providerPreferences);
fallbackIds = ordered.filter((id) => id !== providerId);
}
providerChain = [providerId, ...fallbackIds];
} else if (opts.apiKey) {
// Caller supplied a key but no provider — try auto-detect anyway
const detected = autoDetectMusicProvider();
providerId = detected?.providerId ?? 'suno';
apiKey = opts.apiKey;
let fallbackIds = detectFallbackMusicProviders(providerId);
if (opts.providerPreferences) {
const ordered = resolveProviderOrder([providerId, ...fallbackIds], opts.providerPreferences);
fallbackIds = ordered.filter((id) => id !== providerId);
}
providerChain = [providerId, ...fallbackIds];
} else {
providerChain = resolveProviderChain(
detectAvailableMusicProviders(),
opts.providerPreferences,
);
if (providerChain.length === 0) {
throw new Error(
'No music provider configured. Set SUNO_API_KEY, UDIO_API_KEY, STABILITY_API_KEY, REPLICATE_API_TOKEN, FAL_API_KEY, or MINIMAX_API_KEY.',
);
}
providerId = providerChain[0];
apiKey = process.env[envKeyForMusicProvider(providerId)] ?? '';
}
metricProviderId = providerId;
metricModelId = opts.model;
emitAudioProgress(opts.onProgress, 'queued', 0, `Queued music generation with ${providerId}.`);
span?.setAttribute('llm.provider', providerId);
if (opts.model) span?.setAttribute('llm.model', opts.model);
// --- Create provider (with fallback chain) ---
const provider = await createMusicProviderWithFallback(
providerChain,
apiKey,
opts.model,
opts.timeoutMs,
);
if (!provider.supports('music')) {
throw new Error(`Provider "${providerId}" does not support music generation.`);
}
emitAudioProgress(opts.onProgress, 'processing', 25, `Generating music with ${providerId}.`);
// --- Dispatch to generateMusic ---
const result = await provider.generateMusic({
prompt: opts.prompt,
modelId:
provider instanceof FallbackAudioProxy
? undefined
: (opts.model ?? provider.defaultModelId),
durationSec: opts.durationSec,
negativePrompt: opts.negativePrompt,
outputFormat: opts.outputFormat,
seed: opts.seed,
n: opts.n,
userId: opts.userId,
providerOptions: opts.providerOptions,
});
metricUsage = result.usage;
metricModelId = result.modelId;
span?.setAttribute('agentos.api.audio_clips_count', result.audio.length);
if (result.usage?.totalCostUSD !== undefined) {
attachUsageAttributes(span, { totalCostUSD: result.usage.totalCostUSD });
}
emitAudioProgress(opts.onProgress, 'complete', 100, 'Music generation complete.');
return {
model: result.modelId,
provider: result.providerId,
created: result.created,
audio: result.audio,
usage: result.usage,
};
});
} catch (error) {
metricStatus = 'error';
emitAudioProgress(
opts.onProgress,
'failed',
100,
error instanceof Error ? error.message : String(error),
);
throw error;
} finally {
try {
await recordAgentOSUsage({
providerId: metricProviderId,
modelId: metricModelId,
usage: metricUsage
? {
totalTokens: metricUsage.totalAudioClips,
costUSD: metricUsage.totalCostUSD,
}
: undefined,
options: {
...opts.usageLedger,
source: opts.usageLedger?.source ?? 'generateMusic',
},
});
} catch {
// Usage persistence is best-effort and should not break generation.
}
recordAgentOSTurnMetrics({
durationMs: Date.now() - startedAt,
status: metricStatus,
usage: toTurnMetricUsage(
metricUsage
? { totalCostUSD: metricUsage.totalCostUSD }
: undefined,
),
});
}
}