@fungi.computer/darkroom 0.6.0 → 0.8.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.
- package/README.md +178 -0
- package/dist/app-media-contracts.d.ts +107 -12
- package/dist/audio-providers/elevenlabs.d.ts +40 -0
- package/dist/audio-providers/elevenlabs.js +69 -0
- package/dist/audio-reference-types.d.ts +28 -0
- package/dist/audio-reference-types.js +16 -0
- package/dist/audio-references.d.ts +14 -0
- package/dist/audio-references.js +69 -0
- package/dist/browser-failure.d.ts +2 -0
- package/dist/browser-failure.js +2 -0
- package/dist/cloudflare/provider.d.ts +6 -4
- package/dist/cloudflare/provider.js +39 -21
- package/dist/cloudflare/reference-images.d.ts +6 -0
- package/dist/cloudflare/reference-images.js +25 -7
- package/dist/cost-estimate.js +2 -2
- package/dist/daily-media-meter.d.ts +55 -0
- package/dist/daily-media-meter.js +81 -0
- package/dist/execution-error.d.ts +1 -1
- package/dist/gateway-models.d.ts +19 -9
- package/dist/gateway-models.js +57 -19
- package/dist/gateway-wire.d.ts +1 -0
- package/dist/gateway-wire.js +2 -0
- package/dist/index.d.ts +116 -24
- package/dist/index.js +147 -26
- package/dist/media-binding.d.ts +60 -7
- package/dist/media-binding.js +43 -21
- package/dist/media-generation.js +20 -7
- package/dist/media-operation-contract.d.ts +9 -3
- package/dist/media-operation-contract.js +6 -4
- package/dist/media-operation-do.d.ts +13 -2
- package/dist/media-operation-do.js +26 -1
- package/dist/media-operation-owner.d.ts +10 -2
- package/dist/media-operation-owner.js +56 -11
- package/dist/media-operation.js +5 -3
- package/dist/media-quote.d.ts +14 -0
- package/dist/media-quote.js +17 -0
- package/dist/model-catalog.d.ts +602 -6
- package/dist/model-catalog.js +309 -9
- package/dist/reference-image-size.d.ts +4 -3
- package/dist/reference-image-size.js +9 -9
- package/dist/server.d.ts +31 -8
- package/dist/server.js +51 -8
- package/dist/video-providers/reference-encoding.d.ts +7 -2
- package/dist/video-providers/reference-encoding.js +14 -3
- package/dist/video-providers/types.d.ts +2 -2
- package/dist/video-providers/unified.d.ts +1 -0
- package/dist/video-providers/unified.js +4 -2
- package/docs/api/app-media-contracts.md +50 -10
- package/docs/api/browser.md +4 -2
- package/docs/api/cloudflare.md +56 -3
- package/docs/api/index.md +1042 -121
- package/docs/api/server.md +306 -50
- package/package.json +3 -2
package/dist/model-catalog.js
CHANGED
|
@@ -23,7 +23,7 @@ export const mediaModelTierSchema = z.enum({
|
|
|
23
23
|
/** Most expensive model selection. */
|
|
24
24
|
max: "max",
|
|
25
25
|
});
|
|
26
|
-
/** Media's provider catalog: Workers AI images plus gateway image and
|
|
26
|
+
/** Media's provider catalog: Workers AI images plus gateway image, video and audio models. */
|
|
27
27
|
export const mediaModelInputSchema = z.lazy(() => z
|
|
28
28
|
.strictObject({
|
|
29
29
|
/**
|
|
@@ -190,6 +190,20 @@ export const mediaVideoSchema = z.lazy(() => z
|
|
|
190
190
|
maxReferenceImages: z.number().int().nonnegative(),
|
|
191
191
|
/** Resolutions supporting reference-image mode. */
|
|
192
192
|
referenceResolutions: z.array(mediaVideoResolutionSchema).readonly(),
|
|
193
|
+
/**
|
|
194
|
+
* Audio clips accepted as `reference_audios`, or null when the model has
|
|
195
|
+
* no audio references. Declared by the provider's input schema, never
|
|
196
|
+
* inferred from the family.
|
|
197
|
+
*/
|
|
198
|
+
audioReferences: z
|
|
199
|
+
.strictObject({
|
|
200
|
+
/** Most clips in one request. */
|
|
201
|
+
max: z.number().int().positive(),
|
|
202
|
+
/** Longest total duration of all clips, in seconds. */
|
|
203
|
+
totalSeconds: z.number().int().positive(),
|
|
204
|
+
})
|
|
205
|
+
.readonly()
|
|
206
|
+
.nullable(),
|
|
193
207
|
/** Non-video-input provider cost; 1 USD = 100,000 milliunits. */
|
|
194
208
|
prices: z
|
|
195
209
|
.array(z
|
|
@@ -205,6 +219,65 @@ export const mediaVideoSchema = z.lazy(() => z
|
|
|
205
219
|
.readonly(),
|
|
206
220
|
})
|
|
207
221
|
.readonly());
|
|
222
|
+
/**
|
|
223
|
+
* Verified ElevenLabs model identifiers on Cloudflare's Unified Billing, from
|
|
224
|
+
* the model pages under https://developers.cloudflare.com/ai/models/elevenlabs/
|
|
225
|
+
* (checked 2026-11-13). Cloudflare offers no sound-effects model. Eleven Flash
|
|
226
|
+
* v2.5 and Turbo v2.5 are listed there but a live Unified Billing call to Flash
|
|
227
|
+
* answered 403 code 2047 ("not available via unified billing. Please use
|
|
228
|
+
* BYOK"), so they are not offered. Multilingual v2 is the default voice model.
|
|
229
|
+
*/
|
|
230
|
+
export const mediaAudioModelIdSchema = z.enum({
|
|
231
|
+
/** Eleven Multilingual v2 text to speech. */
|
|
232
|
+
multilingual: "elevenlabs/eleven-multilingual-v2",
|
|
233
|
+
/** Eleven v3 expressive text to speech. */
|
|
234
|
+
v3: "elevenlabs/eleven-v3",
|
|
235
|
+
/** Eleven Music v2. */
|
|
236
|
+
music: "elevenlabs/music-v2",
|
|
237
|
+
});
|
|
238
|
+
/** One premade voice a speech model may speak with. */
|
|
239
|
+
export const mediaAudioVoiceSchema = z.lazy(() => z
|
|
240
|
+
.strictObject({
|
|
241
|
+
/** ElevenLabs voice identifier, exactly as Cloudflare's docs list it. */
|
|
242
|
+
id: z.string().min(1).max(64),
|
|
243
|
+
/** Customer-visible voice label. */
|
|
244
|
+
label: z.string().min(1).max(128),
|
|
245
|
+
})
|
|
246
|
+
.readonly());
|
|
247
|
+
/** Verified audio parameters and provider cost, checked 2026-11-13. */
|
|
248
|
+
export const mediaAudioSchema = z.lazy(() => z
|
|
249
|
+
.strictObject({
|
|
250
|
+
/** Provider translation family. */
|
|
251
|
+
provider: z.literal("elevenlabs"),
|
|
252
|
+
/** Speech reads the prompt aloud; music composes from it. */
|
|
253
|
+
mode: z.enum({
|
|
254
|
+
/** Text to speech: the prompt is the spoken text. */
|
|
255
|
+
speech: "speech",
|
|
256
|
+
/** Music: the prompt describes the track. */
|
|
257
|
+
music: "music",
|
|
258
|
+
}),
|
|
259
|
+
/** Premade voices; empty for music. */
|
|
260
|
+
voices: z.array(mediaAudioVoiceSchema).readonly(),
|
|
261
|
+
/** Voice used when none is chosen; null for music. */
|
|
262
|
+
defaultVoiceId: z.string().min(1).max(64).nullable(),
|
|
263
|
+
/** Music length bounds in whole seconds; null for speech. */
|
|
264
|
+
seconds: z
|
|
265
|
+
.strictObject({
|
|
266
|
+
/** Shortest track. */
|
|
267
|
+
min: z.number().int().positive(),
|
|
268
|
+
/** Longest track. */
|
|
269
|
+
max: z.number().int().positive(),
|
|
270
|
+
/** Length when omitted. */
|
|
271
|
+
default: z.number().int().positive(),
|
|
272
|
+
})
|
|
273
|
+
.readonly()
|
|
274
|
+
.nullable(),
|
|
275
|
+
/** Provider milliunits per billed unit; 1 USD = 100,000 milliunits. */
|
|
276
|
+
milliunitsPerUnit: z.number().int().positive(),
|
|
277
|
+
})
|
|
278
|
+
.readonly());
|
|
279
|
+
/** Characters one speech billing block covers. */
|
|
280
|
+
export const AUDIO_CHARACTER_BLOCK = 100;
|
|
208
281
|
/**
|
|
209
282
|
* Structural ceiling for any prompt on the wire. Each model's own
|
|
210
283
|
* `maxPromptLength` is the real bound; no model may exceed this ceiling.
|
|
@@ -225,6 +298,8 @@ export const mediaModelSchema = z
|
|
|
225
298
|
image: "image",
|
|
226
299
|
/** Video generation. */
|
|
227
300
|
video: "video",
|
|
301
|
+
/** Speech or music generation. */
|
|
302
|
+
audio: "audio",
|
|
228
303
|
}),
|
|
229
304
|
/** Model selection tier, independent of price. */
|
|
230
305
|
tier: mediaModelTierSchema,
|
|
@@ -247,12 +322,15 @@ export const mediaModelSchema = z
|
|
|
247
322
|
.nullable(),
|
|
248
323
|
/** Video length constraints, or null for a still-image model. */
|
|
249
324
|
video: mediaVideoSchema.nullable(),
|
|
325
|
+
/** Speech or music constraints, or null for an image or video model. */
|
|
326
|
+
audio: mediaAudioSchema.nullable(),
|
|
250
327
|
})
|
|
251
328
|
.readonly();
|
|
252
329
|
const freezeModel = (model) => Object.freeze({
|
|
253
330
|
kind: "image",
|
|
254
331
|
sizes: null,
|
|
255
332
|
video: null,
|
|
333
|
+
audio: null,
|
|
256
334
|
outputFormats: Object.freeze([]),
|
|
257
335
|
...model,
|
|
258
336
|
input: Object.freeze(model.input),
|
|
@@ -270,7 +348,7 @@ const freezeModel = (model) => Object.freeze({
|
|
|
270
348
|
const GPT_IMAGE_MAX_EDGE = 3_840;
|
|
271
349
|
/** Largest pixel count the documented size grammar admits. */
|
|
272
350
|
const GPT_IMAGE_MAX_PIXELS = 8_294_400;
|
|
273
|
-
const
|
|
351
|
+
const COMMON_IMAGE_RATIOS = [
|
|
274
352
|
[1, 1],
|
|
275
353
|
[4, 3],
|
|
276
354
|
[3, 4],
|
|
@@ -284,7 +362,7 @@ const gptImageSizes = (() => {
|
|
|
284
362
|
const keep = new Set(["1024x1024", "1024x1536", "1536x1024"]);
|
|
285
363
|
const standard = [];
|
|
286
364
|
const largest = [];
|
|
287
|
-
for (const [across, down] of
|
|
365
|
+
for (const [across, down] of COMMON_IMAGE_RATIOS) {
|
|
288
366
|
const divisor = ((a, b) => {
|
|
289
367
|
let x = a;
|
|
290
368
|
let y = b;
|
|
@@ -306,6 +384,26 @@ const gptImageSizes = (() => {
|
|
|
306
384
|
}
|
|
307
385
|
return Object.freeze([...standard, ...largest]);
|
|
308
386
|
})();
|
|
387
|
+
/** The common ratios as exact 16-aligned sizes of about `area` pixels. */
|
|
388
|
+
const commonRatioSizes = (area) => Object.freeze(COMMON_IMAGE_RATIOS.map(([across, down]) => {
|
|
389
|
+
let x = across;
|
|
390
|
+
let y = down;
|
|
391
|
+
while (y !== 0)
|
|
392
|
+
[x, y] = [y, x % y];
|
|
393
|
+
const unitW = (16 * across) / x;
|
|
394
|
+
const unitH = (16 * down) / x;
|
|
395
|
+
const steps = Math.round(Math.sqrt(area / (unitW * unitH)));
|
|
396
|
+
return `${String(unitW * steps)}x${String(unitH * steps)}`;
|
|
397
|
+
}));
|
|
398
|
+
/**
|
|
399
|
+
* Seedream 5 Pro takes `size` as a string: "1K", "2K" or an explicit pixel
|
|
400
|
+
* size (the page's examples are "1K" and "1536x864"). It documents no bounds,
|
|
401
|
+
* so each common ratio is listed as an exact explicit size at the scale of its
|
|
402
|
+
* own 1536x864 example (about 1.3 megapixels), where 16:9 is that example.
|
|
403
|
+
* https://developers.cloudflare.com/ai/models/bytedance/seedream-5-pro/
|
|
404
|
+
* https://developers.cloudflare.com/ai/models/bytedance/seedream-5-pro/schema-input.json
|
|
405
|
+
*/
|
|
406
|
+
const seedreamSizes = commonRatioSizes(1536 * 864);
|
|
309
407
|
const videoShortEdges = {
|
|
310
408
|
"480p": 480,
|
|
311
409
|
"720p": 720,
|
|
@@ -329,10 +427,13 @@ const videoAspects = {
|
|
|
329
427
|
* Prices are Cloudflare dashboard prices, supplied by the owner 2026-11-13;
|
|
330
428
|
* the public model page lists none. Only the non-video-input rates apply, as
|
|
331
429
|
* video-input references are unsupported.
|
|
430
|
+
* `reference_audios` takes at most 10 clips totalling 30 seconds
|
|
431
|
+
* (https://developers.cloudflare.com/ai/models/bytedance/seedance-2.5/schema-input.json).
|
|
332
432
|
*/
|
|
333
433
|
const seedance25 = {
|
|
334
434
|
maxSeconds: 30,
|
|
335
435
|
maxReferenceImages: 30,
|
|
436
|
+
audioReferences: { max: 10, totalSeconds: 30 },
|
|
336
437
|
maxPromptLength: 25_000,
|
|
337
438
|
resolutions: ["480p", "720p"],
|
|
338
439
|
aspects: mediaVideoAspectRatioSchema.options.filter((aspectRatio) => aspectRatio !== mediaVideoAspectRatioSchema.enum.ultratall),
|
|
@@ -387,6 +488,9 @@ function videoModel(id, label, tier, provider, rates, bounds) {
|
|
|
387
488
|
? mediaVideoReferenceRoleSchema.options
|
|
388
489
|
: [mediaVideoReferenceRoleSchema.enum.first]),
|
|
389
490
|
maxReferenceImages,
|
|
491
|
+
audioReferences: bounds === undefined
|
|
492
|
+
? null
|
|
493
|
+
: Object.freeze({ ...bounds.audioReferences }),
|
|
390
494
|
referenceResolutions: Object.freeze(seedance
|
|
391
495
|
? resolutions.filter((resolution) => resolution !== "1080p")
|
|
392
496
|
: []),
|
|
@@ -485,6 +589,7 @@ function minimaxVideoModel() {
|
|
|
485
589
|
// The vendor page documents up to nine; this stays at the conservative
|
|
486
590
|
// verified value of four until the gateway schema is confirmed.
|
|
487
591
|
maxReferenceImages: 4,
|
|
592
|
+
audioReferences: null,
|
|
488
593
|
referenceResolutions: Object.freeze(resolutions),
|
|
489
594
|
prices: Object.freeze([
|
|
490
595
|
{
|
|
@@ -528,6 +633,77 @@ function minimaxVideoModel() {
|
|
|
528
633
|
},
|
|
529
634
|
});
|
|
530
635
|
}
|
|
636
|
+
/**
|
|
637
|
+
* The one voice Cloudflare's ElevenLabs pages list: every `voice_id` example
|
|
638
|
+
* and request sample on the TTS model pages uses it, and there is no voice
|
|
639
|
+
* listing endpoint on Cloudflare. More voices are added only from a page that
|
|
640
|
+
* lists them.
|
|
641
|
+
* https://developers.cloudflare.com/ai/models/elevenlabs/eleven-v3/
|
|
642
|
+
*/
|
|
643
|
+
const premadeVoices = Object.freeze([
|
|
644
|
+
Object.freeze({ id: "JBFqnCBsd6RMkjVDRZzb", label: "Default voice" }),
|
|
645
|
+
]);
|
|
646
|
+
/**
|
|
647
|
+
* An ElevenLabs speech model. Text is at most 10,000 characters and a
|
|
648
|
+
* `voice_id` is required (each model's `schema-input.json`); the rate is the
|
|
649
|
+
* per-character price on its Cloudflare page, 1 USD = 100,000 milliunits.
|
|
650
|
+
* https://developers.cloudflare.com/ai/models/elevenlabs/eleven-v3/
|
|
651
|
+
* https://developers.cloudflare.com/ai/models/elevenlabs/eleven-multilingual-v2/
|
|
652
|
+
*/
|
|
653
|
+
function speechModel(id, label, tier, milliunitsPerCharacter) {
|
|
654
|
+
return audioModel(id, label, tier, 10_000, {
|
|
655
|
+
provider: "elevenlabs",
|
|
656
|
+
mode: "speech",
|
|
657
|
+
voices: premadeVoices,
|
|
658
|
+
defaultVoiceId: premadeVoices[0]?.id ?? null,
|
|
659
|
+
seconds: null,
|
|
660
|
+
milliunitsPerUnit: milliunitsPerCharacter,
|
|
661
|
+
});
|
|
662
|
+
}
|
|
663
|
+
/**
|
|
664
|
+
* Eleven Music v2: `music_length_ms` runs 3,000 through 600,000 and the page
|
|
665
|
+
* lists $0.0025 per output second (250 milliunits). The prompt has no
|
|
666
|
+
* documented limit, so the retained 2,048 host cap applies. The model is run
|
|
667
|
+
* with a length always set, so the quote is the length asked for.
|
|
668
|
+
* https://developers.cloudflare.com/ai/models/elevenlabs/music-v2/
|
|
669
|
+
*/
|
|
670
|
+
function musicModel() {
|
|
671
|
+
return audioModel(mediaAudioModelIdSchema.enum.music, "Eleven Music v2", "premium", UNDOCUMENTED_PROMPT_LENGTH, {
|
|
672
|
+
provider: "elevenlabs",
|
|
673
|
+
mode: "music",
|
|
674
|
+
voices: Object.freeze([]),
|
|
675
|
+
defaultVoiceId: null,
|
|
676
|
+
seconds: Object.freeze({ min: 3, max: 600, default: 30 }),
|
|
677
|
+
milliunitsPerUnit: 250,
|
|
678
|
+
});
|
|
679
|
+
}
|
|
680
|
+
function audioModel(id, label, tier, maxPromptLength, audio) {
|
|
681
|
+
return freezeModel({
|
|
682
|
+
id,
|
|
683
|
+
label,
|
|
684
|
+
tier,
|
|
685
|
+
maxPromptLength,
|
|
686
|
+
kind: "audio",
|
|
687
|
+
audio: Object.freeze(audio),
|
|
688
|
+
input: {
|
|
689
|
+
transport: "json",
|
|
690
|
+
dimensions: "none",
|
|
691
|
+
maxDimension: null,
|
|
692
|
+
maxDimensionDocumented: false,
|
|
693
|
+
// Audio has no dimensions; the shared wire grammar still requires a
|
|
694
|
+
// positive default, and nothing reads it for an audio model.
|
|
695
|
+
defaultWidth: 1,
|
|
696
|
+
defaultHeight: 1,
|
|
697
|
+
steps: "fixed",
|
|
698
|
+
maxSteps: null,
|
|
699
|
+
defaultSteps: null,
|
|
700
|
+
referenceField: null,
|
|
701
|
+
maxReferences: 0,
|
|
702
|
+
referenceDimension: null,
|
|
703
|
+
referenceFormat: "png",
|
|
704
|
+
},
|
|
705
|
+
});
|
|
706
|
+
}
|
|
531
707
|
const models = Object.freeze([
|
|
532
708
|
freezeModel({
|
|
533
709
|
id: "@cf/black-forest-labs/flux-1-schnell",
|
|
@@ -707,6 +883,36 @@ const models = Object.freeze([
|
|
|
707
883
|
referenceFormat: "webp",
|
|
708
884
|
},
|
|
709
885
|
}),
|
|
886
|
+
freezeModel({
|
|
887
|
+
id: "bytedance/seedream-5-pro",
|
|
888
|
+
// The page documents no prompt maxLength (schema-input.json has none):
|
|
889
|
+
// keep the 2,048 host cap.
|
|
890
|
+
maxPromptLength: UNDOCUMENTED_PROMPT_LENGTH,
|
|
891
|
+
// The page's example results are .jpeg URLs; the schema has no format field.
|
|
892
|
+
outputFormats: Object.freeze(["jpeg"]),
|
|
893
|
+
tier: "premium",
|
|
894
|
+
label: "Seedream 5 Pro — ByteDance, high-quality images",
|
|
895
|
+
sizes: seedreamSizes,
|
|
896
|
+
input: {
|
|
897
|
+
transport: "json",
|
|
898
|
+
dimensions: "own",
|
|
899
|
+
// The longest listed edge: the page documents no maximum.
|
|
900
|
+
maxDimension: 1792,
|
|
901
|
+
maxDimensionDocumented: false,
|
|
902
|
+
defaultWidth: 1536,
|
|
903
|
+
defaultHeight: 864,
|
|
904
|
+
steps: "fixed",
|
|
905
|
+
maxSteps: null,
|
|
906
|
+
defaultSteps: null,
|
|
907
|
+
// The page says the model takes up to 10 reference images (`image`, a
|
|
908
|
+
// string or 1-10 strings) but shows only URL inputs and states no
|
|
909
|
+
// base64 or data-URI form, so Prints sends none until it does.
|
|
910
|
+
referenceField: null,
|
|
911
|
+
maxReferences: 0,
|
|
912
|
+
referenceDimension: 512,
|
|
913
|
+
referenceFormat: "png",
|
|
914
|
+
},
|
|
915
|
+
}),
|
|
710
916
|
videoModel(mediaVideoModelIdSchema.enum.seedance25, "Seedance 2.5", "balanced", "seedance", [
|
|
711
917
|
["480p", 10_280, 10_280],
|
|
712
918
|
["720p", 23_120, 23_120],
|
|
@@ -734,17 +940,111 @@ const models = Object.freeze([
|
|
|
734
940
|
["1080p", 10_000, 12_000],
|
|
735
941
|
]),
|
|
736
942
|
minimaxVideoModel(),
|
|
943
|
+
// ElevenLabs on Unified Billing: URL results, like the video models.
|
|
944
|
+
speechModel(mediaAudioModelIdSchema.enum.multilingual, "Eleven Multilingual v2: natural speech", "balanced", 10),
|
|
945
|
+
speechModel(mediaAudioModelIdSchema.enum.v3, "Eleven v3: expressive speech", "premium", 10),
|
|
946
|
+
musicModel(),
|
|
737
947
|
]);
|
|
738
|
-
/** The immutable image and
|
|
948
|
+
/** The Fungi gateway provider's own immutable image, video and audio model catalog. */
|
|
739
949
|
export const mediaModels = models;
|
|
740
|
-
/**
|
|
950
|
+
/**
|
|
951
|
+
* One catalogue entry. Same grammar as the gateway's wire entry, except that a
|
|
952
|
+
* video model may name `host` as its provider family: the injected provider
|
|
953
|
+
* owns that model's wire grammar and Darkroom builds no gateway request for it.
|
|
954
|
+
*/
|
|
955
|
+
export const mediaCatalogueModelSchema = mediaModelSchema
|
|
956
|
+
.unwrap()
|
|
957
|
+
.extend({
|
|
958
|
+
/** Speech or music constraints; a host's catalogue omits it unless it lists audio models. */
|
|
959
|
+
audio: mediaAudioSchema.unwrap().nullable().default(null),
|
|
960
|
+
/** Video length constraints, or null for a still-image model. */
|
|
961
|
+
video: mediaVideoSchema
|
|
962
|
+
.unwrap()
|
|
963
|
+
.unwrap()
|
|
964
|
+
.extend({
|
|
965
|
+
/** Audio reference limits; a host's catalogue omits it unless its model takes audio references. */
|
|
966
|
+
audioReferences: mediaVideoSchema
|
|
967
|
+
.unwrap()
|
|
968
|
+
.unwrap()
|
|
969
|
+
.shape.audioReferences.default(null),
|
|
970
|
+
/** Provider translation family, or `host` for a host-injected provider. */
|
|
971
|
+
provider: z.enum({
|
|
972
|
+
/** Seedance integer duration and data-URI image fields. */
|
|
973
|
+
seedance: "seedance",
|
|
974
|
+
/** Veo duration string and base64 image_input. */
|
|
975
|
+
veo: "veo",
|
|
976
|
+
/** MiniMax multimodal `content[]` array and uppercase resolution. */
|
|
977
|
+
minimax: "minimax",
|
|
978
|
+
/** The injected provider owns the wire grammar. */
|
|
979
|
+
host: "host",
|
|
980
|
+
}),
|
|
981
|
+
})
|
|
982
|
+
.readonly()
|
|
983
|
+
.nullable(),
|
|
984
|
+
})
|
|
985
|
+
.readonly();
|
|
986
|
+
/** Upper bound on one provider's model list. */
|
|
987
|
+
const MAX_CATALOGUE_MODELS = 256;
|
|
988
|
+
/**
|
|
989
|
+
* Decode a host-supplied provider catalogue. Each entry is the same typed wire
|
|
990
|
+
* grammar the built-in catalogue uses; this adds the cross-field facts the
|
|
991
|
+
* validator relies on (unique ids, video metadata iff `kind` is video, defaults
|
|
992
|
+
* inside their own bounds).
|
|
993
|
+
*/
|
|
994
|
+
export const mediaCatalogueSchema = z
|
|
995
|
+
.array(mediaCatalogueModelSchema)
|
|
996
|
+
.min(1)
|
|
997
|
+
.max(MAX_CATALOGUE_MODELS)
|
|
998
|
+
.superRefine((entries, context) => {
|
|
999
|
+
const seen = new Set();
|
|
1000
|
+
entries.forEach((model, index) => {
|
|
1001
|
+
const report = (message) => {
|
|
1002
|
+
context.addIssue({ code: "custom", message, path: [index] });
|
|
1003
|
+
};
|
|
1004
|
+
if (seen.has(model.id))
|
|
1005
|
+
report(`duplicate model id "${model.id}"`);
|
|
1006
|
+
seen.add(model.id);
|
|
1007
|
+
if ((model.kind === "video") !== (model.video !== null))
|
|
1008
|
+
report(`${model.id} must carry video bounds exactly when kind is video`);
|
|
1009
|
+
if ((model.kind === "audio") !== (model.audio !== null))
|
|
1010
|
+
report(`${model.id} must carry audio bounds exactly when kind is audio`);
|
|
1011
|
+
const video = model.video;
|
|
1012
|
+
if (video === null)
|
|
1013
|
+
return;
|
|
1014
|
+
if (video.minSeconds > video.maxSeconds ||
|
|
1015
|
+
video.durations.some((seconds) => seconds < video.minSeconds || seconds > video.maxSeconds) ||
|
|
1016
|
+
!video.durations.includes(video.defaultSeconds))
|
|
1017
|
+
report(`${model.id} durations must sit inside min/max and include the default`);
|
|
1018
|
+
if (!video.formats.some(({ resolution, aspectRatio }) => resolution === video.defaultResolution &&
|
|
1019
|
+
aspectRatio === video.defaultAspectRatio))
|
|
1020
|
+
report(`${model.id} formats must include the default resolution and aspect`);
|
|
1021
|
+
});
|
|
1022
|
+
})
|
|
1023
|
+
.transform((entries) => Object.freeze(entries.map((model) => Object.freeze({ ...model, input: Object.freeze(model.input) }))));
|
|
1024
|
+
/** Decode untrusted catalogue data, throwing a MediaSettingsError that names the first problem. */
|
|
1025
|
+
export const parseMediaCatalogue = (raw) => {
|
|
1026
|
+
const parsed = mediaCatalogueSchema.safeParse(raw);
|
|
1027
|
+
if (!parsed.success)
|
|
1028
|
+
throw new MediaSettingsError(`media catalogue is invalid: ${parsed.error.issues[0]?.message ?? "unreadable"}`);
|
|
1029
|
+
return parsed.data;
|
|
1030
|
+
};
|
|
1031
|
+
/** List the gateway provider's models in catalog order. */
|
|
741
1032
|
export const listMediaModels = () => mediaModels;
|
|
742
|
-
/**
|
|
743
|
-
|
|
1033
|
+
/**
|
|
1034
|
+
* The gateway models Apps and browsers list and pick from: pictures and video.
|
|
1035
|
+
* The Agar and Prints catalog decoders know no `audio` kind yet, and one unknown
|
|
1036
|
+
* row fails the whole list, so audio models are generated by id until the
|
|
1037
|
+
* Prints audio UI lands and lists them.
|
|
1038
|
+
*/
|
|
1039
|
+
export const listPictureMediaModels = () => mediaModels.filter((model) => model.kind !== "audio");
|
|
1040
|
+
/** Find a model in the Fungi gateway's own catalogue; gateway-internal code never sees a custom provider's models. */
|
|
1041
|
+
export const getGatewayModel = (id) => mediaModels.find((model) => model.id === id);
|
|
1042
|
+
/** Find a model in `catalogue` (the gateway's by default), or undefined when absent. */
|
|
1043
|
+
export const getMediaModel = (id, catalogue = mediaModels) => catalogue.find((model) => model.id === id);
|
|
744
1044
|
/** Invalid generation settings or unsupported model parameters. */
|
|
745
1045
|
export class MediaSettingsError extends Error {
|
|
746
|
-
constructor(message) {
|
|
747
|
-
super(message);
|
|
1046
|
+
constructor(message, options) {
|
|
1047
|
+
super(message, options);
|
|
748
1048
|
this.name = "MediaSettingsError";
|
|
749
1049
|
}
|
|
750
1050
|
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import { type MediaCatalogue } from "./model-catalog.js";
|
|
1
2
|
/** Aspect-preserving native resize; only video references may be enlarged. */
|
|
2
|
-
export declare function referenceImageTransform(modelId: string, width: number, height: number): {
|
|
3
|
+
export declare function referenceImageTransform(modelId: string, width: number, height: number, catalogue?: MediaCatalogue): {
|
|
3
4
|
width: number;
|
|
4
5
|
height: number;
|
|
5
6
|
fit: "contain" | "scale-down";
|
|
6
7
|
};
|
|
7
8
|
/** Validate actual decoded output, rather than trusting requested resize dimensions. */
|
|
8
|
-
export declare function referenceImageSizeIsValid(modelId: string, width: number, height: number): boolean;
|
|
9
|
+
export declare function referenceImageSizeIsValid(modelId: string, width: number, height: number, catalogue?: MediaCatalogue): boolean;
|
|
9
10
|
/** The encoding the selected model's fitted references are sent in. */
|
|
10
|
-
export declare function referenceImageFormat(modelId: string): "image/webp" | "image/png";
|
|
11
|
+
export declare function referenceImageFormat(modelId: string, catalogue?: MediaCatalogue): "image/webp" | "image/png";
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { getMediaModel, MediaSettingsError } from "./model-catalog.js";
|
|
1
|
+
import { getMediaModel, mediaModels, MediaSettingsError, } from "./model-catalog.js";
|
|
2
2
|
const MIN_VIDEO_REFERENCE_DIMENSION = 320;
|
|
3
3
|
/** One owner for both native fitting and the resolved-reference boundary. */
|
|
4
|
-
function sizing(modelId) {
|
|
5
|
-
const model = getMediaModel(modelId);
|
|
4
|
+
function sizing(modelId, catalogue) {
|
|
5
|
+
const model = getMediaModel(modelId, catalogue);
|
|
6
6
|
if (model === undefined)
|
|
7
7
|
throw new MediaSettingsError("unknown Media model");
|
|
8
8
|
return model.video === null && model.input.referenceDimension !== null
|
|
@@ -10,8 +10,8 @@ function sizing(modelId) {
|
|
|
10
10
|
: { edge: "short", dimension: MIN_VIDEO_REFERENCE_DIMENSION };
|
|
11
11
|
}
|
|
12
12
|
/** Aspect-preserving native resize; only video references may be enlarged. */
|
|
13
|
-
export function referenceImageTransform(modelId, width, height) {
|
|
14
|
-
const rule = sizing(modelId);
|
|
13
|
+
export function referenceImageTransform(modelId, width, height, catalogue = mediaModels) {
|
|
14
|
+
const rule = sizing(modelId, catalogue);
|
|
15
15
|
const edge = rule.edge === "short" ? Math.min(width, height) : Math.max(width, height);
|
|
16
16
|
const ratio = rule.edge === "short"
|
|
17
17
|
? rule.dimension / edge
|
|
@@ -27,15 +27,15 @@ export function referenceImageTransform(modelId, width, height) {
|
|
|
27
27
|
};
|
|
28
28
|
}
|
|
29
29
|
/** Validate actual decoded output, rather than trusting requested resize dimensions. */
|
|
30
|
-
export function referenceImageSizeIsValid(modelId, width, height) {
|
|
31
|
-
const rule = sizing(modelId);
|
|
30
|
+
export function referenceImageSizeIsValid(modelId, width, height, catalogue = mediaModels) {
|
|
31
|
+
const rule = sizing(modelId, catalogue);
|
|
32
32
|
return rule.edge === "short"
|
|
33
33
|
? Math.min(width, height) >= rule.dimension
|
|
34
34
|
: Math.max(width, height) <= rule.dimension;
|
|
35
35
|
}
|
|
36
36
|
/** The encoding the selected model's fitted references are sent in. */
|
|
37
|
-
export function referenceImageFormat(modelId) {
|
|
38
|
-
const model = getMediaModel(modelId);
|
|
37
|
+
export function referenceImageFormat(modelId, catalogue = mediaModels) {
|
|
38
|
+
const model = getMediaModel(modelId, catalogue);
|
|
39
39
|
if (model === undefined)
|
|
40
40
|
throw new MediaSettingsError("unknown Media model");
|
|
41
41
|
return model.input.referenceFormat === "webp"
|
package/dist/server.d.ts
CHANGED
|
@@ -2,11 +2,13 @@ export { estimateMediaModel } from "./cloudflare/provider.js";
|
|
|
2
2
|
export { mediaPendingSchema, mediaBindingCallSchema, mediaBindingGenerationSchema, mediaImageBindingInputSchema, mediaVideoBindingInputSchema, parseMediaBindingCall, mediaBindingRequest, readMediaBindingReply, type MediaBindingCall, type MediaBindingGeneration, type MediaBindingRequest, } from "./media-binding.js";
|
|
3
3
|
import { Hono } from "hono";
|
|
4
4
|
import { z } from "zod";
|
|
5
|
-
import { validateMediaGenerationSettings, type MediaProvider, type MediaReference, type MediaReferenceBytes } from "./index.js";
|
|
5
|
+
import { validateMediaGenerationSettings, type MediaCatalogue, type MediaProvider, type MediaReference, type MediaReferenceBytes } from "./index.js";
|
|
6
6
|
import { type MediaExecution, type MediaMetering } from "./media-operation-contract.js";
|
|
7
7
|
import { MediaExecutionError, type MediaBillingRefusalCode } from "./execution-error.js";
|
|
8
|
+
export { createDailyMediaMetering, DailyMediaMeter, type DailyMediaMeterPort, type DailyMediaMeterStorage, } from "./daily-media-meter.js";
|
|
8
9
|
export { MediaBillingRefusal, MediaExecutionError, type MediaBillingRefusalCode, type MediaFailureStage, type MediaExecutionErrorOptions, } from "./execution-error.js";
|
|
9
10
|
export { mediaUnits, type MediaUnits } from "./gateway-models.js";
|
|
11
|
+
export { quoteMediaGeneration, type MediaQuote } from "./media-quote.js";
|
|
10
12
|
export type { MediaMetering, MediaOperation, } from "./media-operation-contract.js";
|
|
11
13
|
export { MediaOperationOwner, mediaOperationDestinationSchema, mediaOperationCoordinatesSchema, mediaOperationStateSchema, mediaImageSchema, mediaOperationResultSchema, } from "./media-operation-owner.js";
|
|
12
14
|
export type { MediaOperationCoordinates, MediaOperationRequest, MediaOperationProjection, MediaOperationDecision, MediaOperationState, MediaOperationResult, MediaOperationEnvironment, MediaOperationHooks, MediaImage, MediaErasurePreflight, MediaErasureReadback, MediaRetentionResult, } from "./media-operation-owner.js";
|
|
@@ -17,10 +19,11 @@ export declare const mediaRequestSchema: z.ZodObject<{
|
|
|
17
19
|
settings: z.ZodObject<{
|
|
18
20
|
model: z.ZodString;
|
|
19
21
|
prompt: z.ZodString;
|
|
20
|
-
width: z.ZodNumber
|
|
21
|
-
height: z.ZodNumber
|
|
22
|
+
width: z.ZodOptional<z.ZodNumber>;
|
|
23
|
+
height: z.ZodOptional<z.ZodNumber>;
|
|
22
24
|
steps: z.ZodOptional<z.ZodNumber>;
|
|
23
25
|
durationSeconds: z.ZodOptional<z.ZodNumber>;
|
|
26
|
+
voiceId: z.ZodOptional<z.ZodString>;
|
|
24
27
|
resolution: z.ZodOptional<z.ZodEnum<{
|
|
25
28
|
readonly p480: "480p";
|
|
26
29
|
readonly p720: "720p";
|
|
@@ -47,6 +50,10 @@ export declare const mediaRequestSchema: z.ZodObject<{
|
|
|
47
50
|
readonly reference: "reference";
|
|
48
51
|
}>>;
|
|
49
52
|
}, z.core.$strict>>>;
|
|
53
|
+
audioReferences: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
54
|
+
computerId: z.ZodString;
|
|
55
|
+
path: z.ZodString;
|
|
56
|
+
}, z.core.$strict>>>;
|
|
50
57
|
}, z.core.$strict>;
|
|
51
58
|
outputFormat: z.ZodOptional<z.ZodEnum<{
|
|
52
59
|
readonly png: "png";
|
|
@@ -93,10 +100,26 @@ export type MediaServiceHost = Readonly<{
|
|
|
93
100
|
write: (path: string, bytes: Uint8Array) => Promise<void>;
|
|
94
101
|
/** Authorize and fit one selected reference image for the catalog model. */
|
|
95
102
|
readReference?: (reference: MediaReference, model: string) => Promise<MediaReferenceBytes>;
|
|
103
|
+
/** Authorize and read the raw bytes of one reference audio clip. */
|
|
104
|
+
readAudioReference?: (reference: MediaReference) => Promise<Uint8Array>;
|
|
96
105
|
}>;
|
|
97
106
|
/** Translate host authority failures; undefined uses the shared Media failure. */
|
|
98
107
|
onFailure?: (error: MediaExecutionError) => Response | undefined;
|
|
99
108
|
}> & (Readonly<{
|
|
109
|
+
/** Omitted: the Fungi gateway provider's own catalogue and quote. */
|
|
110
|
+
catalogue?: undefined;
|
|
111
|
+
/** Omitted with the catalogue: the gateway's own quote. */
|
|
112
|
+
estimate?: undefined;
|
|
113
|
+
}> | Readonly<{
|
|
114
|
+
/**
|
|
115
|
+
* The injected `provider`'s own model list, decoded with
|
|
116
|
+
* `parseMediaCatalogue`. Requests are validated against it, never
|
|
117
|
+
* against the Fungi gateway's.
|
|
118
|
+
*/
|
|
119
|
+
catalogue: MediaCatalogue;
|
|
120
|
+
/** Provider cost of validated settings in milliunits; owned by the custom provider. */
|
|
121
|
+
estimate: (settings: Parameters<MediaProvider>[0], references: readonly MediaReferenceBytes[]) => number;
|
|
122
|
+
}>) & (Readonly<{
|
|
100
123
|
/** The host pays its own provider directly. */
|
|
101
124
|
metering: "unmetered";
|
|
102
125
|
/** Unmetered hosts do not have billing refusal policy. */
|
|
@@ -129,13 +152,13 @@ export type MediaGenerationOutcome = Readonly<{
|
|
|
129
152
|
/** Admitted price in milliunits, where 1 cent is 1,000 milliunits. */
|
|
130
153
|
costMilliunits: number;
|
|
131
154
|
/** Actual output encoding. */
|
|
132
|
-
format: "png" | "jpeg" | "gif" | "webp" | "mp4";
|
|
133
|
-
/** Requested image height or nominal video selector
|
|
134
|
-
height
|
|
155
|
+
format: "png" | "jpeg" | "gif" | "webp" | "mp4" | "mp3";
|
|
156
|
+
/** Requested image height or nominal video selector; absent for audio. */
|
|
157
|
+
height?: number | undefined;
|
|
135
158
|
/** Provider catalog model identifier. */
|
|
136
159
|
model: string;
|
|
137
|
-
/** Requested image width or nominal video selector
|
|
138
|
-
width
|
|
160
|
+
/** Requested image width or nominal video selector; absent for audio. */
|
|
161
|
+
width?: number | undefined;
|
|
139
162
|
}> | Readonly<{
|
|
140
163
|
/** Outcome or model-kind discriminant. */
|
|
141
164
|
kind: "pending";
|