@fungi.computer/darkroom 0.7.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/README.md +49 -0
  2. package/dist/app-media-contracts.d.ts +103 -12
  3. package/dist/audio-providers/elevenlabs.d.ts +40 -0
  4. package/dist/audio-providers/elevenlabs.js +69 -0
  5. package/dist/audio-reference-types.d.ts +28 -0
  6. package/dist/audio-reference-types.js +16 -0
  7. package/dist/audio-references.d.ts +14 -0
  8. package/dist/audio-references.js +69 -0
  9. package/dist/cloudflare/provider.d.ts +6 -4
  10. package/dist/cloudflare/provider.js +37 -19
  11. package/dist/gateway-models.d.ts +12 -8
  12. package/dist/gateway-models.js +38 -17
  13. package/dist/index.d.ts +70 -15
  14. package/dist/index.js +118 -7
  15. package/dist/media-binding.d.ts +60 -7
  16. package/dist/media-binding.js +41 -19
  17. package/dist/media-generation.js +12 -3
  18. package/dist/media-operation-contract.d.ts +7 -3
  19. package/dist/media-operation-contract.js +6 -4
  20. package/dist/media-operation-owner.d.ts +2 -2
  21. package/dist/media-operation-owner.js +48 -6
  22. package/dist/media-operation.js +4 -2
  23. package/dist/model-catalog.d.ts +155 -4
  24. package/dist/model-catalog.js +173 -4
  25. package/dist/server.d.ts +14 -7
  26. package/dist/server.js +9 -4
  27. package/dist/video-providers/reference-encoding.d.ts +7 -2
  28. package/dist/video-providers/reference-encoding.js +14 -3
  29. package/dist/video-providers/types.d.ts +2 -2
  30. package/dist/video-providers/unified.d.ts +1 -0
  31. package/dist/video-providers/unified.js +4 -2
  32. package/docs/api/app-media-contracts.md +46 -10
  33. package/docs/api/cloudflare.md +3 -3
  34. package/docs/api/index.md +564 -84
  35. package/docs/api/server.md +129 -62
  36. package/package.json +3 -2
package/README.md CHANGED
@@ -458,6 +458,55 @@ destination and settings to recover the retained result. The host chooses the
458
458
  output path, such as `prints-<requestId>.mp4`; Darkroom adds no output-naming
459
459
  rule or human-review gate.
460
460
 
461
+ ### Audio
462
+
463
+ Speech and music run through Cloudflare's Unified Billing, so Teams pay Team
464
+ credit at provider cost plus the normal markup and no customer key is involved.
465
+ The catalog carries `elevenlabs/eleven-multilingual-v2` (the default voice
466
+ model), `elevenlabs/eleven-v3` and `elevenlabs/music-v2`, each with
467
+ `kind: "audio"` and an `audio` block; every bound and rate comes from the
468
+ model's page under `https://developers.cloudflare.com/ai/models/elevenlabs/`.
469
+ Cloudflare offers no sound-effects model and no voice listing, so the only voice
470
+ is the one its pages list (`JBFqnCBsd6RMkjVDRZzb`), and sound effects, custom
471
+ voices and cloned voices are not offered.
472
+
473
+ Speech takes `prompt` (the spoken text, up to 10,000 characters) and an optional
474
+ catalog `voiceId`; music takes `prompt` and `durationSeconds` from 3 through 600
475
+ (30 when omitted). Neither takes dimensions, steps, `generateAudio` or
476
+ references; `validateMediaGenerationSettings` refuses them rather than dropping
477
+ them. Speech is quoted per character (5 or 10 milliunits) and music per second
478
+ (250 milliunits). `mediaUnits` bills speech as
479
+ `{unit: "audio_character_block", units: ceil(characters / 100)}` and music as
480
+ `{unit: "audio_second", units: durationSeconds}`, so a per-character rate is
481
+ marked up once per block. Requests ask ElevenLabs for `mp3_44100_128`; the
482
+ provider fetches the result URL and returns `{bytes, format: "mp3"}`. Audio runs
483
+ through the same durable alarm as video: HTTP returns 202 until the retained MP3
484
+ is ready, and the output has no embedded provenance. Hub public sharing does not
485
+ cover audio. The MEDIA binding accepts `kind: "audio"` with `prompt`, `voiceId`
486
+ or `durationSeconds`, and saves `<libraryPath>/prints-<requestId>.mp3`. The
487
+ `/models` listing still carries pictures and video only, because the Agar and
488
+ Prints catalog decoders have no audio kind yet; audio models are generated by
489
+ id.
490
+
491
+ Video models declare `video.audioReferences` (`{max, totalSeconds}`, or null).
492
+ Only Seedance 2.5 sets it, at 10 clips and 30 seconds, from its Cloudflare input
493
+ schema's `reference_audios`. A video selection may carry `audioReferences`
494
+ (`[{path}]`); every other model refuses them before admission, and a clip is
495
+ never dropped or trimmed. The host supplies authority through
496
+ `destination.readAudioReference(reference)`, which returns the raw bytes;
497
+ Darkroom parses each clip with music-metadata (mp3, m4a, wav, ogg or opus),
498
+ refuses unreadable bytes, and refuses a set whose count exceeds the model's
499
+ `max` or whose durations total more than `totalSeconds`, all before billing.
500
+ Clips reach the provider as `data:audio/...;base64,...` entries in
501
+ `reference_audios`, are retained in the artifacts bucket for the background
502
+ alarm like reference images, and are deleted after dispatch. A host that gives
503
+ no `readAudioReference` lists `audioReferences: null`. Standalone Oyster reads
504
+ clips from the Computer workspace; the Hub Agent host does not read audio
505
+ references yet. Parsing pulls in music-metadata, which reads Node built-ins, so
506
+ a Worker that runs the Media service needs the `nodejs_compat` flag (the Hub and
507
+ Oyster deploy configs set it). The hub-edge provider path only forwards clips
508
+ and MP3 output, and does not load the parser.
509
+
461
510
  ### App MEDIA binding
462
511
 
463
512
  The shared `@fungi.computer/darkroom/server` binding grammar and reply decoder
@@ -12,10 +12,11 @@ export declare const mediaModelPriceEstimateSchema: z.ZodObject<{
12
12
  settings: z.ZodObject<{
13
13
  model: z.ZodString;
14
14
  prompt: z.ZodString;
15
- width: z.ZodNumber;
16
- height: z.ZodNumber;
15
+ width: z.ZodOptional<z.ZodNumber>;
16
+ height: z.ZodOptional<z.ZodNumber>;
17
17
  steps: z.ZodOptional<z.ZodNumber>;
18
18
  durationSeconds: z.ZodOptional<z.ZodNumber>;
19
+ voiceId: z.ZodOptional<z.ZodString>;
19
20
  resolution: z.ZodOptional<z.ZodEnum<{
20
21
  readonly p480: "480p";
21
22
  readonly p720: "720p";
@@ -42,6 +43,10 @@ export declare const mediaModelPriceEstimateSchema: z.ZodObject<{
42
43
  readonly reference: "reference";
43
44
  }>>;
44
45
  }, z.core.$strict>>>;
46
+ audioReferences: z.ZodOptional<z.ZodArray<z.ZodObject<{
47
+ computerId: z.ZodString;
48
+ path: z.ZodString;
49
+ }, z.core.$strict>>>;
45
50
  }, z.core.$strict>;
46
51
  }, z.core.$strict>;
47
52
  /** Host catalog projection; provider metadata remains owned by the sole catalog. */
@@ -51,6 +56,7 @@ export declare const mediaCatalogModelSchema: z.ZodReadonly<z.ZodObject<{
51
56
  kind: z.ZodEnum<{
52
57
  readonly image: "image";
53
58
  readonly video: "video";
59
+ readonly audio: "audio";
54
60
  }>;
55
61
  tier: z.ZodEnum<{
56
62
  readonly draft: "draft";
@@ -154,6 +160,10 @@ export declare const mediaCatalogModelSchema: z.ZodReadonly<z.ZodObject<{
154
160
  readonly p1080: "1080p";
155
161
  readonly p4k: "4k";
156
162
  }>>>;
163
+ audioReferences: z.ZodNullable<z.ZodReadonly<z.ZodObject<{
164
+ max: z.ZodNumber;
165
+ totalSeconds: z.ZodNumber;
166
+ }, z.core.$strict>>>;
157
167
  prices: z.ZodReadonly<z.ZodArray<z.ZodReadonly<z.ZodObject<{
158
168
  resolution: z.ZodEnum<{
159
169
  readonly p480: "480p";
@@ -166,6 +176,24 @@ export declare const mediaCatalogModelSchema: z.ZodReadonly<z.ZodObject<{
166
176
  audioMilliunitsPerSecond: z.ZodNumber;
167
177
  }, z.core.$strict>>>>;
168
178
  }, z.core.$strict>>>>;
179
+ audio: z.ZodNullable<z.ZodLazy<z.ZodReadonly<z.ZodObject<{
180
+ provider: z.ZodLiteral<"elevenlabs">;
181
+ mode: z.ZodEnum<{
182
+ readonly speech: "speech";
183
+ readonly music: "music";
184
+ }>;
185
+ voices: z.ZodReadonly<z.ZodArray<z.ZodLazy<z.ZodReadonly<z.ZodObject<{
186
+ id: z.ZodString;
187
+ label: z.ZodString;
188
+ }, z.core.$strict>>>>>;
189
+ defaultVoiceId: z.ZodNullable<z.ZodString>;
190
+ seconds: z.ZodNullable<z.ZodReadonly<z.ZodObject<{
191
+ min: z.ZodNumber;
192
+ max: z.ZodNumber;
193
+ default: z.ZodNumber;
194
+ }, z.core.$strict>>>;
195
+ milliunitsPerUnit: z.ZodNumber;
196
+ }, z.core.$strict>>>>;
169
197
  priceEstimate: z.ZodOptional<z.ZodObject<{
170
198
  kind: z.ZodLiteral<"estimate">;
171
199
  basis: z.ZodEnum<{
@@ -178,10 +206,11 @@ export declare const mediaCatalogModelSchema: z.ZodReadonly<z.ZodObject<{
178
206
  settings: z.ZodObject<{
179
207
  model: z.ZodString;
180
208
  prompt: z.ZodString;
181
- width: z.ZodNumber;
182
- height: z.ZodNumber;
209
+ width: z.ZodOptional<z.ZodNumber>;
210
+ height: z.ZodOptional<z.ZodNumber>;
183
211
  steps: z.ZodOptional<z.ZodNumber>;
184
212
  durationSeconds: z.ZodOptional<z.ZodNumber>;
213
+ voiceId: z.ZodOptional<z.ZodString>;
185
214
  resolution: z.ZodOptional<z.ZodEnum<{
186
215
  readonly p480: "480p";
187
216
  readonly p720: "720p";
@@ -208,6 +237,10 @@ export declare const mediaCatalogModelSchema: z.ZodReadonly<z.ZodObject<{
208
237
  readonly reference: "reference";
209
238
  }>>;
210
239
  }, z.core.$strict>>>;
240
+ audioReferences: z.ZodOptional<z.ZodArray<z.ZodObject<{
241
+ computerId: z.ZodString;
242
+ path: z.ZodString;
243
+ }, z.core.$strict>>>;
211
244
  }, z.core.$strict>;
212
245
  }, z.core.$strict>>;
213
246
  }, z.core.$strict>>;
@@ -218,6 +251,7 @@ export declare const mediaModelsSchema: z.ZodLazy<z.ZodArray<z.ZodReadonly<z.Zod
218
251
  kind: z.ZodEnum<{
219
252
  readonly image: "image";
220
253
  readonly video: "video";
254
+ readonly audio: "audio";
221
255
  }>;
222
256
  tier: z.ZodEnum<{
223
257
  readonly draft: "draft";
@@ -321,6 +355,10 @@ export declare const mediaModelsSchema: z.ZodLazy<z.ZodArray<z.ZodReadonly<z.Zod
321
355
  readonly p1080: "1080p";
322
356
  readonly p4k: "4k";
323
357
  }>>>;
358
+ audioReferences: z.ZodNullable<z.ZodReadonly<z.ZodObject<{
359
+ max: z.ZodNumber;
360
+ totalSeconds: z.ZodNumber;
361
+ }, z.core.$strict>>>;
324
362
  prices: z.ZodReadonly<z.ZodArray<z.ZodReadonly<z.ZodObject<{
325
363
  resolution: z.ZodEnum<{
326
364
  readonly p480: "480p";
@@ -333,6 +371,24 @@ export declare const mediaModelsSchema: z.ZodLazy<z.ZodArray<z.ZodReadonly<z.Zod
333
371
  audioMilliunitsPerSecond: z.ZodNumber;
334
372
  }, z.core.$strict>>>>;
335
373
  }, z.core.$strict>>>>;
374
+ audio: z.ZodNullable<z.ZodLazy<z.ZodReadonly<z.ZodObject<{
375
+ provider: z.ZodLiteral<"elevenlabs">;
376
+ mode: z.ZodEnum<{
377
+ readonly speech: "speech";
378
+ readonly music: "music";
379
+ }>;
380
+ voices: z.ZodReadonly<z.ZodArray<z.ZodLazy<z.ZodReadonly<z.ZodObject<{
381
+ id: z.ZodString;
382
+ label: z.ZodString;
383
+ }, z.core.$strict>>>>>;
384
+ defaultVoiceId: z.ZodNullable<z.ZodString>;
385
+ seconds: z.ZodNullable<z.ZodReadonly<z.ZodObject<{
386
+ min: z.ZodNumber;
387
+ max: z.ZodNumber;
388
+ default: z.ZodNumber;
389
+ }, z.core.$strict>>>;
390
+ milliunitsPerUnit: z.ZodNumber;
391
+ }, z.core.$strict>>>>;
336
392
  priceEstimate: z.ZodOptional<z.ZodObject<{
337
393
  kind: z.ZodLiteral<"estimate">;
338
394
  basis: z.ZodEnum<{
@@ -345,10 +401,11 @@ export declare const mediaModelsSchema: z.ZodLazy<z.ZodArray<z.ZodReadonly<z.Zod
345
401
  settings: z.ZodObject<{
346
402
  model: z.ZodString;
347
403
  prompt: z.ZodString;
348
- width: z.ZodNumber;
349
- height: z.ZodNumber;
404
+ width: z.ZodOptional<z.ZodNumber>;
405
+ height: z.ZodOptional<z.ZodNumber>;
350
406
  steps: z.ZodOptional<z.ZodNumber>;
351
407
  durationSeconds: z.ZodOptional<z.ZodNumber>;
408
+ voiceId: z.ZodOptional<z.ZodString>;
352
409
  resolution: z.ZodOptional<z.ZodEnum<{
353
410
  readonly p480: "480p";
354
411
  readonly p720: "720p";
@@ -375,6 +432,10 @@ export declare const mediaModelsSchema: z.ZodLazy<z.ZodArray<z.ZodReadonly<z.Zod
375
432
  readonly reference: "reference";
376
433
  }>>;
377
434
  }, z.core.$strict>>>;
435
+ audioReferences: z.ZodOptional<z.ZodArray<z.ZodObject<{
436
+ computerId: z.ZodString;
437
+ path: z.ZodString;
438
+ }, z.core.$strict>>>;
378
439
  }, z.core.$strict>;
379
440
  }, z.core.$strict>>;
380
441
  }, z.core.$strict>>>>;
@@ -391,9 +452,10 @@ export declare const appMediaGenerationResultSchema: z.ZodObject<{
391
452
  readonly gif: "gif";
392
453
  readonly webp: "webp";
393
454
  readonly mp4: "mp4";
455
+ readonly mp3: "mp3";
394
456
  }>;
395
- width: z.ZodNumber;
396
- height: z.ZodNumber;
457
+ width: z.ZodOptional<z.ZodNumber>;
458
+ height: z.ZodOptional<z.ZodNumber>;
397
459
  bytes: z.ZodNumber;
398
460
  model: z.ZodString;
399
461
  costMilliunits: z.ZodNumber;
@@ -547,6 +609,7 @@ export declare const appMediaModelsOutcomeSchema: z.ZodUnion<readonly [z.ZodLazy
547
609
  kind: z.ZodEnum<{
548
610
  readonly image: "image";
549
611
  readonly video: "video";
612
+ readonly audio: "audio";
550
613
  }>;
551
614
  tier: z.ZodEnum<{
552
615
  readonly draft: "draft";
@@ -650,6 +713,10 @@ export declare const appMediaModelsOutcomeSchema: z.ZodUnion<readonly [z.ZodLazy
650
713
  readonly p1080: "1080p";
651
714
  readonly p4k: "4k";
652
715
  }>>>;
716
+ audioReferences: z.ZodNullable<z.ZodReadonly<z.ZodObject<{
717
+ max: z.ZodNumber;
718
+ totalSeconds: z.ZodNumber;
719
+ }, z.core.$strict>>>;
653
720
  prices: z.ZodReadonly<z.ZodArray<z.ZodReadonly<z.ZodObject<{
654
721
  resolution: z.ZodEnum<{
655
722
  readonly p480: "480p";
@@ -662,6 +729,24 @@ export declare const appMediaModelsOutcomeSchema: z.ZodUnion<readonly [z.ZodLazy
662
729
  audioMilliunitsPerSecond: z.ZodNumber;
663
730
  }, z.core.$strict>>>>;
664
731
  }, z.core.$strict>>>>;
732
+ audio: z.ZodNullable<z.ZodLazy<z.ZodReadonly<z.ZodObject<{
733
+ provider: z.ZodLiteral<"elevenlabs">;
734
+ mode: z.ZodEnum<{
735
+ readonly speech: "speech";
736
+ readonly music: "music";
737
+ }>;
738
+ voices: z.ZodReadonly<z.ZodArray<z.ZodLazy<z.ZodReadonly<z.ZodObject<{
739
+ id: z.ZodString;
740
+ label: z.ZodString;
741
+ }, z.core.$strict>>>>>;
742
+ defaultVoiceId: z.ZodNullable<z.ZodString>;
743
+ seconds: z.ZodNullable<z.ZodReadonly<z.ZodObject<{
744
+ min: z.ZodNumber;
745
+ max: z.ZodNumber;
746
+ default: z.ZodNumber;
747
+ }, z.core.$strict>>>;
748
+ milliunitsPerUnit: z.ZodNumber;
749
+ }, z.core.$strict>>>>;
665
750
  priceEstimate: z.ZodOptional<z.ZodObject<{
666
751
  kind: z.ZodLiteral<"estimate">;
667
752
  basis: z.ZodEnum<{
@@ -674,10 +759,11 @@ export declare const appMediaModelsOutcomeSchema: z.ZodUnion<readonly [z.ZodLazy
674
759
  settings: z.ZodObject<{
675
760
  model: z.ZodString;
676
761
  prompt: z.ZodString;
677
- width: z.ZodNumber;
678
- height: z.ZodNumber;
762
+ width: z.ZodOptional<z.ZodNumber>;
763
+ height: z.ZodOptional<z.ZodNumber>;
679
764
  steps: z.ZodOptional<z.ZodNumber>;
680
765
  durationSeconds: z.ZodOptional<z.ZodNumber>;
766
+ voiceId: z.ZodOptional<z.ZodString>;
681
767
  resolution: z.ZodOptional<z.ZodEnum<{
682
768
  readonly p480: "480p";
683
769
  readonly p720: "720p";
@@ -704,6 +790,10 @@ export declare const appMediaModelsOutcomeSchema: z.ZodUnion<readonly [z.ZodLazy
704
790
  readonly reference: "reference";
705
791
  }>>;
706
792
  }, z.core.$strict>>>;
793
+ audioReferences: z.ZodOptional<z.ZodArray<z.ZodObject<{
794
+ computerId: z.ZodString;
795
+ path: z.ZodString;
796
+ }, z.core.$strict>>>;
707
797
  }, z.core.$strict>;
708
798
  }, z.core.$strict>>;
709
799
  }, z.core.$strict>>>>, z.ZodUnion<readonly [z.ZodObject<{
@@ -764,9 +854,10 @@ export declare const appMediaGenerationOutcomeSchema: z.ZodUnion<readonly [z.Zod
764
854
  readonly gif: "gif";
765
855
  readonly webp: "webp";
766
856
  readonly mp4: "mp4";
857
+ readonly mp3: "mp3";
767
858
  }>;
768
- width: z.ZodNumber;
769
- height: z.ZodNumber;
859
+ width: z.ZodOptional<z.ZodNumber>;
860
+ height: z.ZodOptional<z.ZodNumber>;
770
861
  bytes: z.ZodNumber;
771
862
  model: z.ZodString;
772
863
  costMilliunits: z.ZodNumber;
@@ -0,0 +1,40 @@
1
+ import { type MediaModel } from "../model-catalog.js";
2
+ import type { MediaAiResult, MediaGenerationSettings } from "../index.js";
3
+ /**
4
+ * ElevenLabs on Cloudflare's Unified Billing. Every shape below is the model
5
+ * page's own `schema-input.json` / `schema-output.json`, checked 2026-11-13:
6
+ * https://developers.cloudflare.com/ai/models/elevenlabs/eleven-v3/
7
+ * https://developers.cloudflare.com/ai/models/elevenlabs/eleven-multilingual-v2/
8
+ * https://developers.cloudflare.com/ai/models/elevenlabs/music-v2/
9
+ */
10
+ /**
11
+ * `mp3_44100_128` is in the `output_format` enum of every ElevenLabs model
12
+ * page above. It is requested explicitly so the saved file is always an MP3.
13
+ */
14
+ declare const ELEVENLABS_OUTPUT_FORMAT = "mp3_44100_128";
15
+ /** A text-to-speech request: `text` (1-10,000 characters) and a required `voice_id`. */
16
+ export type ElevenLabsSpeechInput = {
17
+ text: string;
18
+ voice_id: string;
19
+ output_format: typeof ELEVENLABS_OUTPUT_FORMAT;
20
+ };
21
+ /** A music request: a `prompt` and `music_length_ms` of 3,000 through 600,000. */
22
+ export type ElevenLabsMusicInput = {
23
+ prompt: string;
24
+ music_length_ms: number;
25
+ output_format: typeof ELEVENLABS_OUTPUT_FORMAT;
26
+ };
27
+ /** Every ElevenLabs request body this catalog can produce. */
28
+ export type ElevenLabsInput = ElevenLabsSpeechInput | ElevenLabsMusicInput;
29
+ /** Results are fetched whole; 600 seconds of 128 kbps MP3 is about 10 MB. */
30
+ export declare const ELEVENLABS_MAX_BYTES: number;
31
+ /** The exact provider JSON body for one validated speech or music generation. */
32
+ export declare function buildElevenLabsInput(settings: MediaGenerationSettings, model: MediaModel): ElevenLabsInput;
33
+ /** Extract and validate the HTTPS result URL from the raw provider answer. */
34
+ export declare function decodeElevenLabsResultUrl(providerValue: MediaAiResult): string;
35
+ /**
36
+ * Provider cost in milliunits: speech is per character of text, music per
37
+ * second of output length asked for (1 USD = 100,000 milliunits).
38
+ */
39
+ export declare function estimateElevenLabsMilliunits(settings: MediaGenerationSettings, model: MediaModel): number;
40
+ export {};
@@ -0,0 +1,69 @@
1
+ import { z } from "zod";
2
+ import { MediaSettingsError, } from "../model-catalog.js";
3
+ import { resultUrl } from "../gateway-wire.js";
4
+ /**
5
+ * ElevenLabs on Cloudflare's Unified Billing. Every shape below is the model
6
+ * page's own `schema-input.json` / `schema-output.json`, checked 2026-11-13:
7
+ * https://developers.cloudflare.com/ai/models/elevenlabs/eleven-v3/
8
+ * https://developers.cloudflare.com/ai/models/elevenlabs/eleven-multilingual-v2/
9
+ * https://developers.cloudflare.com/ai/models/elevenlabs/music-v2/
10
+ */
11
+ /**
12
+ * `mp3_44100_128` is in the `output_format` enum of every ElevenLabs model
13
+ * page above. It is requested explicitly so the saved file is always an MP3.
14
+ */
15
+ const ELEVENLABS_OUTPUT_FORMAT = "mp3_44100_128";
16
+ /**
17
+ * The one shape a live Unified Billing call returned for Multilingual v2, v3
18
+ * and Music v2 (2026-11-13, REST envelope `{result: ...}` removed by the
19
+ * binding): `{state: "Completed", result: {audio: <HTTPS URL>}}`, the same
20
+ * wrapper Seedance and Veo use. The audio is a URL to fetch, not bytes. The
21
+ * model pages' bare `{audio}` schema-output is not what the gateway answers
22
+ * and is refused.
23
+ */
24
+ const elevenLabsOutputSchema = z.lazy(() => z.looseObject({
25
+ state: z.literal("Completed"),
26
+ result: z.looseObject({ audio: resultUrl }),
27
+ }));
28
+ /** Results are fetched whole; 600 seconds of 128 kbps MP3 is about 10 MB. */
29
+ export const ELEVENLABS_MAX_BYTES = 32 * 1024 * 1024;
30
+ function audioOf(model) {
31
+ if (model.audio === null)
32
+ throw new MediaSettingsError(`${model.id} is not an audio model`);
33
+ return model.audio;
34
+ }
35
+ /** The exact provider JSON body for one validated speech or music generation. */
36
+ export function buildElevenLabsInput(settings, model) {
37
+ const audio = audioOf(model);
38
+ if (audio.mode === "speech") {
39
+ if (settings.voiceId === undefined)
40
+ throw new MediaSettingsError(`${model.id} needs a voice`);
41
+ return {
42
+ text: settings.prompt,
43
+ voice_id: settings.voiceId,
44
+ output_format: ELEVENLABS_OUTPUT_FORMAT,
45
+ };
46
+ }
47
+ if (settings.durationSeconds === undefined)
48
+ throw new MediaSettingsError(`${model.id} needs a length`);
49
+ return {
50
+ prompt: settings.prompt,
51
+ music_length_ms: settings.durationSeconds * 1_000,
52
+ output_format: ELEVENLABS_OUTPUT_FORMAT,
53
+ };
54
+ }
55
+ /** Extract and validate the HTTPS result URL from the raw provider answer. */
56
+ export function decodeElevenLabsResultUrl(providerValue) {
57
+ return elevenLabsOutputSchema.parse(providerValue).result.audio;
58
+ }
59
+ /**
60
+ * Provider cost in milliunits: speech is per character of text, music per
61
+ * second of output length asked for (1 USD = 100,000 milliunits).
62
+ */
63
+ export function estimateElevenLabsMilliunits(settings, model) {
64
+ const audio = audioOf(model);
65
+ const quantity = audio.mode === "speech" ? settings.prompt.length : settings.durationSeconds;
66
+ if (quantity === undefined)
67
+ throw new MediaSettingsError(`${model.id} needs a length`);
68
+ return quantity * audio.milliunitsPerUnit;
69
+ }
@@ -0,0 +1,28 @@
1
+ import { type MediaVideoModel } from "./model-catalog.js";
2
+ /** Decoded bytes of one reference clip: well past 30 seconds of any admitted container. */
3
+ export declare const MAX_AUDIO_REFERENCE_BYTES: number;
4
+ /** Decoded bytes across every clip of one request; base64 adds a third. */
5
+ export declare const MAX_AUDIO_REFERENCE_BODY_BYTES: number;
6
+ /** One reference clip with the facts the provider request and the 30-second budget need. */
7
+ export type MediaAudioReferenceBytes = Readonly<{
8
+ /** Exact clip bytes as the Computer holds them. */
9
+ bytes: Uint8Array;
10
+ /** Media type of the clip's container, from its parsed header. */
11
+ mimeType: AudioReferenceMimeType;
12
+ /** Whole-clip duration in seconds, from the parsed header or decoded stream. */
13
+ durationSeconds: number;
14
+ /** Optional filename for diagnostics. */
15
+ filename?: string;
16
+ }>;
17
+ /**
18
+ * Containers a clip may arrive in: `mp3`, `m4a`, `wav`, `ogg` and `opus`
19
+ * files. Seedance 2.5 states only that clips are "base64 data:audio/... URIs"
20
+ * and does not list formats, so Darkroom admits what the gallery plays.
21
+ * https://developers.cloudflare.com/ai/models/bytedance/seedance-2.5/schema-input.json
22
+ */
23
+ export type AudioReferenceMimeType = "audio/mpeg" | "audio/mp4" | "audio/wav" | "audio/ogg";
24
+ /**
25
+ * Enforce the model's declared clip count and total duration. Neither is
26
+ * clamped: a clip over budget refuses the request instead of being dropped.
27
+ */
28
+ export declare function checkAudioReferenceBudget(label: string, limits: NonNullable<MediaVideoModel["audioReferences"]>, clips: readonly Pick<MediaAudioReferenceBytes, "durationSeconds">[]): void;
@@ -0,0 +1,16 @@
1
+ import { MediaSettingsError } from "./model-catalog.js";
2
+ /** Decoded bytes of one reference clip: well past 30 seconds of any admitted container. */
3
+ export const MAX_AUDIO_REFERENCE_BYTES = 16 * 1024 * 1024;
4
+ /** Decoded bytes across every clip of one request; base64 adds a third. */
5
+ export const MAX_AUDIO_REFERENCE_BODY_BYTES = 24 * 1024 * 1024;
6
+ /**
7
+ * Enforce the model's declared clip count and total duration. Neither is
8
+ * clamped: a clip over budget refuses the request instead of being dropped.
9
+ */
10
+ export function checkAudioReferenceBudget(label, limits, clips) {
11
+ if (clips.length > limits.max)
12
+ throw new MediaSettingsError(`${label} takes up to ${String(limits.max)} audio references`);
13
+ const total = clips.reduce((sum, clip) => sum + clip.durationSeconds, 0);
14
+ if (total > limits.totalSeconds)
15
+ throw new MediaSettingsError(`${label} audio references total ${total.toFixed(1)} seconds; the limit is ${String(limits.totalSeconds)}`);
16
+ }
@@ -0,0 +1,14 @@
1
+ import { type MediaCatalogue } from "./model-catalog.js";
2
+ import type { MediaGenerationSettings, MediaReference } from "./index.js";
3
+ import { type MediaAudioReferenceBytes } from "./audio-reference-types.js";
4
+ /** Host-authorized reader for the raw bytes of reference audio clips. */
5
+ export type MediaAudioReferenceReader = Readonly<{
6
+ /** Authorize and read one clip; Darkroom parses it, the host never does. */
7
+ read: (reference: MediaReference) => Promise<Uint8Array>;
8
+ }>;
9
+ /**
10
+ * Read every reference clip through the caller's reader and parse each with
11
+ * music-metadata. The model's declared clip count and total duration are
12
+ * enforced here, before billing admission; nothing is clamped or dropped.
13
+ */
14
+ export declare const resolveMediaAudioReferences: (settings: MediaGenerationSettings, reader: MediaAudioReferenceReader, catalogue?: MediaCatalogue) => Promise<readonly MediaAudioReferenceBytes[]>;
@@ -0,0 +1,69 @@
1
+ import { parseBuffer } from "music-metadata";
2
+ import { getMediaModel, MediaSettingsError, mediaModels, } from "./model-catalog.js";
3
+ import { MAX_AUDIO_REFERENCE_BODY_BYTES, MAX_AUDIO_REFERENCE_BYTES, checkAudioReferenceBudget, } from "./audio-reference-types.js";
4
+ /** music-metadata's container names, matched case-insensitively. */
5
+ function mimeTypeOf(container) {
6
+ const name = (container ?? "").toLowerCase();
7
+ if (name === "mpeg")
8
+ return "audio/mpeg";
9
+ if (name === "wave")
10
+ return "audio/wav";
11
+ if (name === "ogg")
12
+ return "audio/ogg";
13
+ if (name === "m4a" || name === "mp4" || name.startsWith("iso"))
14
+ return "audio/mp4";
15
+ throw new MediaSettingsError("audio reference is not an mp3, m4a, wav, ogg or opus file");
16
+ }
17
+ /**
18
+ * Read one clip's duration and container with music-metadata, the maintained
19
+ * parser. `duration: true` makes it scan a stream whose header has no length.
20
+ */
21
+ async function readAudioReference(bytes, filename) {
22
+ if (bytes.byteLength === 0 || bytes.byteLength > MAX_AUDIO_REFERENCE_BYTES)
23
+ throw new MediaSettingsError("audio reference is unavailable or exceeds the byte limit");
24
+ let metadata;
25
+ try {
26
+ metadata = await parseBuffer(bytes, undefined, { duration: true });
27
+ }
28
+ catch (cause) {
29
+ throw new MediaSettingsError("audio reference could not be read as audio", {
30
+ cause,
31
+ });
32
+ }
33
+ const durationSeconds = metadata.format.duration;
34
+ if (durationSeconds === undefined ||
35
+ !Number.isFinite(durationSeconds) ||
36
+ durationSeconds <= 0)
37
+ throw new MediaSettingsError("audio reference has no readable duration");
38
+ const clip = {
39
+ bytes,
40
+ mimeType: mimeTypeOf(metadata.format.container),
41
+ durationSeconds,
42
+ };
43
+ return Object.freeze(filename === undefined ? clip : { ...clip, filename });
44
+ }
45
+ /**
46
+ * Read every reference clip through the caller's reader and parse each with
47
+ * music-metadata. The model's declared clip count and total duration are
48
+ * enforced here, before billing admission; nothing is clamped or dropped.
49
+ */
50
+ export const resolveMediaAudioReferences = async (settings, reader, catalogue = mediaModels) => {
51
+ const references = settings.audioReferences ?? [];
52
+ if (references.length === 0)
53
+ return Object.freeze([]);
54
+ const model = getMediaModel(settings.model, catalogue);
55
+ const limits = model?.video?.audioReferences ?? null;
56
+ if (model === undefined || limits === null)
57
+ throw new MediaSettingsError(`${model?.label ?? settings.model} does not take audio references`);
58
+ const resolved = [];
59
+ let totalBytes = 0;
60
+ for (const reference of references) {
61
+ const bytes = await reader.read(reference);
62
+ totalBytes += bytes.byteLength;
63
+ if (totalBytes > MAX_AUDIO_REFERENCE_BODY_BYTES)
64
+ throw new MediaSettingsError("audio references exceed the total request size limit");
65
+ resolved.push(await readAudioReference(bytes, reference.path.split("/").at(-1) ?? "reference"));
66
+ }
67
+ checkAudioReferenceBudget(model.label, limits, resolved);
68
+ return Object.freeze(resolved);
69
+ };
@@ -1,4 +1,4 @@
1
- import { type MediaModel, type MediaAiResult, type MediaGenerationSettings, type MediaProvider, type MediaReferenceBytes } from "../index.js";
1
+ import { type MediaModel, type MediaAiResult, type MediaAudioReferenceBytes, type MediaGenerationSettings, type MediaProvider, type MediaReferenceBytes } from "../index.js";
2
2
  /** Quote the catalog defaults through the exact estimator used before dispatch. */
3
3
  export declare function estimateMediaModel(model: MediaModel): {
4
4
  /** An indicative quote, not an admitted generation receipt. */
@@ -9,14 +9,16 @@ export declare function estimateMediaModel(model: MediaModel): {
9
9
  settings: Readonly<{
10
10
  model: string;
11
11
  prompt: string;
12
- width: number;
13
- height: number;
12
+ width?: number;
13
+ height?: number;
14
14
  steps?: number;
15
15
  durationSeconds?: number;
16
+ voiceId?: string;
16
17
  resolution?: import("../model-catalog.js").MediaVideoResolution;
17
18
  aspectRatio?: import("../model-catalog.js").MediaVideoAspectRatio;
18
19
  generateAudio?: boolean;
19
20
  references?: readonly import("../index.js").MediaReference[];
21
+ audioReferences?: readonly import("../index.js").MediaReference[];
20
22
  }>;
21
23
  /** Estimated provider milliunits of a cent for these exact settings. */
22
24
  costMilliunits: number;
@@ -27,7 +29,7 @@ export type MediaAiPort = {
27
29
  run(model: string, input: Parameters<Ai["run"]>[1], options?: AiOptions): Promise<MediaAiResult>;
28
30
  };
29
31
  export declare function estimate(settings: MediaGenerationSettings, references: readonly MediaReferenceBytes[]): number;
30
- export declare function generate(ai: MediaAiPort, settings: MediaGenerationSettings, references: readonly MediaReferenceBytes[], signal?: AbortSignal, options?: AiOptions, fetchResult?: typeof fetch, outputFormat?: "png" | "jpeg" | "webp"): Promise<{
32
+ export declare function generate(ai: MediaAiPort, settings: MediaGenerationSettings, references: readonly MediaReferenceBytes[], signal?: AbortSignal, options?: AiOptions, fetchResult?: typeof fetch, outputFormat?: "png" | "jpeg" | "webp", audioReferences?: readonly MediaAudioReferenceBytes[]): Promise<{
31
33
  bytes: Uint8Array;
32
34
  format: string;
33
35
  }>;