@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.
Files changed (53) hide show
  1. package/README.md +178 -0
  2. package/dist/app-media-contracts.d.ts +107 -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/browser-failure.d.ts +2 -0
  10. package/dist/browser-failure.js +2 -0
  11. package/dist/cloudflare/provider.d.ts +6 -4
  12. package/dist/cloudflare/provider.js +39 -21
  13. package/dist/cloudflare/reference-images.d.ts +6 -0
  14. package/dist/cloudflare/reference-images.js +25 -7
  15. package/dist/cost-estimate.js +2 -2
  16. package/dist/daily-media-meter.d.ts +55 -0
  17. package/dist/daily-media-meter.js +81 -0
  18. package/dist/execution-error.d.ts +1 -1
  19. package/dist/gateway-models.d.ts +19 -9
  20. package/dist/gateway-models.js +57 -19
  21. package/dist/gateway-wire.d.ts +1 -0
  22. package/dist/gateway-wire.js +2 -0
  23. package/dist/index.d.ts +116 -24
  24. package/dist/index.js +147 -26
  25. package/dist/media-binding.d.ts +60 -7
  26. package/dist/media-binding.js +43 -21
  27. package/dist/media-generation.js +20 -7
  28. package/dist/media-operation-contract.d.ts +9 -3
  29. package/dist/media-operation-contract.js +6 -4
  30. package/dist/media-operation-do.d.ts +13 -2
  31. package/dist/media-operation-do.js +26 -1
  32. package/dist/media-operation-owner.d.ts +10 -2
  33. package/dist/media-operation-owner.js +56 -11
  34. package/dist/media-operation.js +5 -3
  35. package/dist/media-quote.d.ts +14 -0
  36. package/dist/media-quote.js +17 -0
  37. package/dist/model-catalog.d.ts +602 -6
  38. package/dist/model-catalog.js +309 -9
  39. package/dist/reference-image-size.d.ts +4 -3
  40. package/dist/reference-image-size.js +9 -9
  41. package/dist/server.d.ts +31 -8
  42. package/dist/server.js +51 -8
  43. package/dist/video-providers/reference-encoding.d.ts +7 -2
  44. package/dist/video-providers/reference-encoding.js +14 -3
  45. package/dist/video-providers/types.d.ts +2 -2
  46. package/dist/video-providers/unified.d.ts +1 -0
  47. package/dist/video-providers/unified.js +4 -2
  48. package/docs/api/app-media-contracts.md +50 -10
  49. package/docs/api/browser.md +4 -2
  50. package/docs/api/cloudflare.md +56 -3
  51. package/docs/api/index.md +1042 -121
  52. package/docs/api/server.md +306 -50
  53. package/package.json +3 -2
package/dist/index.js CHANGED
@@ -1,9 +1,10 @@
1
1
  import * as z from "zod";
2
2
  import { reportCatchFailure } from "./failure.js";
3
3
  import { referenceImageSizeIsValid } from "./reference-image-size.js";
4
- import { getMediaModel, MAX_REFERENCE_COUNT, MediaSettingsError, mediaVideoModelIdSchema, mediaVideoResolutionSchema, mediaVideoAspectRatioSchema, mediaVideoReferenceRoleSchema, MAX_MEDIA_PROMPT_LENGTH, } from "./model-catalog.js";
4
+ import { getGatewayModel, getMediaModel, mediaAudioModelIdSchema, MAX_REFERENCE_COUNT, MediaSettingsError, mediaModels, mediaVideoModelIdSchema, mediaVideoResolutionSchema, mediaVideoAspectRatioSchema, mediaVideoReferenceRoleSchema, MAX_MEDIA_PROMPT_LENGTH, } from "./model-catalog.js";
5
5
  export { mediaFailureStageSchema } from "./execution-error.js";
6
- export { MAX_MEDIA_PROMPT_LENGTH, getMediaModel, MAX_REFERENCE_COUNT, listMediaModels, mediaModelInputSchema, mediaModelSchema, mediaRequestedImageFormatSchema, mediaModels, mediaModelTierSchema, MediaSettingsError, mediaVideoModelIdSchema, mediaVideoResolutionSchema, mediaVideoAspectRatioSchema, mediaVideoReferenceRoleSchema, mediaVideoFormatSchema, mediaVideoSchema, } from "./model-catalog.js";
6
+ export { MAX_AUDIO_REFERENCE_BODY_BYTES, MAX_AUDIO_REFERENCE_BYTES, } from "./audio-reference-types.js";
7
+ export { MAX_MEDIA_PROMPT_LENGTH, getMediaModel, getGatewayModel, MAX_REFERENCE_COUNT, listMediaModels, listPictureMediaModels, mediaCatalogueSchema, mediaCatalogueModelSchema, parseMediaCatalogue, mediaModelInputSchema, mediaModelSchema, mediaRequestedImageFormatSchema, mediaModels, mediaModelTierSchema, MediaSettingsError, mediaVideoModelIdSchema, mediaVideoResolutionSchema, mediaVideoAspectRatioSchema, mediaVideoReferenceRoleSchema, mediaVideoFormatSchema, mediaVideoSchema, mediaAudioModelIdSchema, mediaAudioSchema, mediaAudioVoiceSchema, AUDIO_CHARACTER_BLOCK, } from "./model-catalog.js";
7
8
  /** The long-edge bounds a viewer may ask for when fitting an image to look at. */
8
9
  export const MIN_VIEW_DIMENSION = 256;
9
10
  /** Maximum long-edge size accepted by an image viewer, in pixels. */
@@ -43,6 +44,8 @@ const normalizeReference = (value) => {
43
44
  return reference;
44
45
  return { ...reference, role: value.role };
45
46
  };
47
+ /** Schema ceiling for audio reference lists; each model's own `audioReferences.max` is enforced after parsing. */
48
+ export const MAX_AUDIO_REFERENCE_COUNT = 10;
46
49
  /** Structural wire grammar for one Media generation request. */
47
50
  export const mediaGenerationSettingsSchema = z
48
51
  .object({
@@ -50,14 +53,16 @@ export const mediaGenerationSettingsSchema = z
50
53
  model: z.string().min(1).max(256),
51
54
  /** Generation prompt; each model's `maxPromptLength` is enforced on top of this ceiling. */
52
55
  prompt: z.string().min(1).max(MAX_MEDIA_PROMPT_LENGTH),
53
- /** Requested image width or nominal video selector, not decoded MP4 dimensions. */
54
- width: z.number().int().min(1).max(5_040),
55
- /** Requested image height or nominal video selector, not decoded MP4 dimensions. */
56
- height: z.number().int().min(1).max(5_040),
56
+ /** Requested image width or nominal video selector; absent for audio. */
57
+ width: z.number().int().min(1).max(5_040).optional(),
58
+ /** Requested image height or nominal video selector; absent for audio. */
59
+ height: z.number().int().min(1).max(5_040).optional(),
57
60
  /** Model-specific inference step count. */
58
61
  steps: z.number().int().optional(),
59
- /** Requested video length in whole seconds. */
62
+ /** Requested video or music length in whole seconds. */
60
63
  durationSeconds: z.number().int().optional(),
64
+ /** Speech models only: a premade voice from the catalog; the default when omitted. */
65
+ voiceId: z.string().min(1).max(64).optional(),
61
66
  /** Video resolution; must match the nominal width/height selector. */
62
67
  resolution: mediaVideoResolutionSchema.optional(),
63
68
  /** Video aspect; must match the nominal width/height selector. */
@@ -78,6 +83,18 @@ export const mediaGenerationSettingsSchema = z
78
83
  .strict())
79
84
  .max(MAX_REFERENCE_COUNT)
80
85
  .optional(),
86
+ /** Host-authorized reference audio clips; only a model declaring `audioReferences` takes them. */
87
+ audioReferences: z
88
+ .array(z
89
+ .object({
90
+ /** Host-admitted destination or reference identity. */
91
+ computerId: z.string().min(1).max(256),
92
+ /** Fixed reference file path. */
93
+ path: z.string().min(1).max(4_096),
94
+ })
95
+ .strict())
96
+ .max(MAX_AUDIO_REFERENCE_COUNT)
97
+ .optional(),
81
98
  })
82
99
  .strict();
83
100
  /** A reference may select another Computer; identity never grants access. */
@@ -96,16 +113,39 @@ export const mediaGenerationSelectionSchema = mediaGenerationSettingsSchema.exte
96
113
  .strict())
97
114
  .max(MAX_REFERENCE_COUNT)
98
115
  .optional(),
116
+ /** Host-authorized reference audio clips. */
117
+ audioReferences: z
118
+ .array(z
119
+ .object({
120
+ /** Omit for the selected Computer; the host authorizes every named Computer. */
121
+ computerId: z.string().min(1).max(256).optional(),
122
+ /** Fixed reference file path. */
123
+ path: z.string().min(1).max(4_096),
124
+ })
125
+ .strict())
126
+ .max(MAX_AUDIO_REFERENCE_COUNT)
127
+ .optional(),
128
+ });
129
+ /** Mutable caller input for already-normalized settings, so they can be validated again. */
130
+ export const mediaSettingsInput = (settings) => ({
131
+ ...settings,
132
+ references: settings.references?.map((reference) => ({ ...reference })),
133
+ audioReferences: settings.audioReferences?.map((reference) => ({
134
+ ...reference,
135
+ })),
99
136
  });
100
- /** Validate caller settings and apply retained model defaults. */
101
- export const validateMediaGenerationSettings = (raw) => {
137
+ /**
138
+ * Validate caller settings against `catalogue`, the active provider's model
139
+ * list (the Fungi gateway's by default), and apply that model's defaults.
140
+ */
141
+ export const validateMediaGenerationSettings = (raw, catalogue = mediaModels) => {
102
142
  const parsed = mediaGenerationSettingsSchema.safeParse(raw);
103
143
  if (!parsed.success)
104
144
  throw new MediaSettingsError("generation settings are invalid");
105
- return normalizeGenerationSettings(parsed.data, normalizeReference);
145
+ return normalizeGenerationSettings(parsed.data, normalizeReference, catalogue);
106
146
  };
107
147
  /** Validate selections without granting Computer authority. */
108
- export const validateMediaGenerationSelection = (raw) => {
148
+ export const validateMediaGenerationSelection = (raw, catalogue = mediaModels) => {
109
149
  const parsed = mediaGenerationSelectionSchema.safeParse(raw);
110
150
  if (!parsed.success)
111
151
  throw new MediaSettingsError("generation settings are invalid");
@@ -117,16 +157,32 @@ export const validateMediaGenerationSelection = (raw) => {
117
157
  if (reference.role === undefined)
118
158
  return selection;
119
159
  return { ...selection, role: reference.role };
120
- });
160
+ }, catalogue);
121
161
  };
122
- function normalizeGenerationSettings(input, normalize) {
162
+ function normalizeGenerationSettings(input, normalize, catalogue) {
123
163
  const modelId = requiredString(input.model, "model");
124
- const model = getMediaModel(modelId);
164
+ const model = getMediaModel(modelId, catalogue);
125
165
  if (model === undefined)
126
166
  throw new MediaSettingsError(`unknown Media model "${modelId}"`);
127
167
  const prompt = requiredString(input.prompt, "prompt");
128
168
  if (prompt.length > model.maxPromptLength)
129
169
  throw new MediaSettingsError(`${model.id} prompt is ${String(prompt.length)} characters; its limit is ${String(model.maxPromptLength)}`);
170
+ if (model.audio !== null) {
171
+ if (input.width !== undefined ||
172
+ input.height !== undefined ||
173
+ input.steps !== undefined ||
174
+ input.resolution !== undefined ||
175
+ input.aspectRatio !== undefined ||
176
+ input.generateAudio !== undefined ||
177
+ input.references !== undefined ||
178
+ input.audioReferences !== undefined)
179
+ throw new MediaSettingsError(`${model.id} takes only a prompt${model.audio.mode === "speech" ? " and a voice" : " and a length"}`);
180
+ return normalizeAudioSettings(model, model.audio, prompt, input);
181
+ }
182
+ if (input.voiceId !== undefined)
183
+ throw new MediaSettingsError(`${model.id} does not take a voice`);
184
+ if (input.width === undefined || input.height === undefined)
185
+ throw new MediaSettingsError(`${model.id} needs a width and a height`);
130
186
  const maximumDimension = model.video === null ? 4_096 : 5_040;
131
187
  const width = boundedInteger(input.width, "width", 1, maximumDimension);
132
188
  const height = boundedInteger(input.height, "height", 1, maximumDimension);
@@ -228,6 +284,36 @@ function normalizeGenerationSettings(input, normalize) {
228
284
  }
229
285
  if (references !== undefined)
230
286
  normalized.references = Object.freeze(references);
287
+ if (input.audioReferences !== undefined && input.audioReferences.length > 0) {
288
+ const limits = model.video?.audioReferences ?? null;
289
+ if (limits === null)
290
+ throw new MediaSettingsError(`${model.label} does not take audio references`);
291
+ if (input.audioReferences.length > limits.max)
292
+ throw new MediaSettingsError(`${model.label} takes up to ${String(limits.max)} audio references`);
293
+ normalized.audioReferences = Object.freeze(input.audioReferences.map(normalize));
294
+ }
295
+ return Object.freeze(normalized);
296
+ }
297
+ /** Speech takes a catalog voice; music takes a bounded length. Neither takes references. */
298
+ function normalizeAudioSettings(model, audio, prompt, input) {
299
+ const normalized = {
300
+ model: model.id,
301
+ prompt,
302
+ };
303
+ if (audio.mode === "speech") {
304
+ if (input.durationSeconds !== undefined)
305
+ throw new MediaSettingsError(`${model.id} does not take a duration`);
306
+ const voiceId = input.voiceId ?? audio.defaultVoiceId;
307
+ if (voiceId === null || !audio.voices.some((voice) => voice.id === voiceId))
308
+ throw new MediaSettingsError(`${model.id} accepts only these voices: ${audio.voices.map((voice) => voice.id).join(", ")}`);
309
+ normalized.voiceId = voiceId;
310
+ return Object.freeze(normalized);
311
+ }
312
+ if (input.voiceId !== undefined)
313
+ throw new MediaSettingsError(`${model.id} does not take a voice`);
314
+ if (audio.seconds === null)
315
+ throw new MediaSettingsError(`${model.id} is missing its length bounds`);
316
+ normalized.durationSeconds = boundedInteger(input.durationSeconds ?? audio.seconds.default, "durationSeconds", audio.seconds.min, audio.seconds.max);
231
317
  return Object.freeze(normalized);
232
318
  }
233
319
  /** Path-only video reference; the host supplies authority and reads its bytes. */
@@ -237,6 +323,11 @@ export const mediaVideoReferenceSchema = z.lazy(() => z.strictObject({
237
323
  /** Explicit first frame, last frame, or reference-image role. */
238
324
  role: mediaVideoReferenceRoleSchema,
239
325
  }));
326
+ /** Path-only audio reference; the host supplies authority and reads its bytes. */
327
+ export const mediaAudioReferenceSelectionSchema = z.lazy(() => z.strictObject({
328
+ /** Selected audio file path, trimmed and nonblank after validation. */
329
+ path: z.string().min(1).max(4_096),
330
+ }));
240
331
  /** Neutral video selection; model-specific combinations are checked by validateMediaVideoSelection. */
241
332
  export const mediaVideoSelectionSchema = z.lazy(() => z.strictObject({
242
333
  /** Verified catalog model identifier. */
@@ -257,14 +348,26 @@ export const mediaVideoSelectionSchema = z.lazy(() => z.strictObject({
257
348
  .array(mediaVideoReferenceSchema)
258
349
  .max(MAX_VIDEO_REFERENCE_COUNT)
259
350
  .optional(),
351
+ /** Reference audio clips, for a model declaring `audioReferences` (Seedance 2.5: 10 clips, 30 seconds). */
352
+ audioReferences: z
353
+ .array(mediaAudioReferenceSelectionSchema)
354
+ .max(MAX_AUDIO_REFERENCE_COUNT)
355
+ .optional(),
260
356
  }));
357
+ /**
358
+ * The same selection for a host's own catalogue: the model is any id that
359
+ * catalogue lists, checked by `validateMediaVideoSelection`.
360
+ */
361
+ const catalogueVideoSelectionSchema = mediaVideoSelectionSchema
362
+ .unwrap()
363
+ .extend({ model: z.string().min(1).max(256) });
261
364
  /** Validate supported model/size/duration/reference combinations before authority or billing admission. */
262
- export function validateMediaVideoSelection(raw) {
263
- const parsed = mediaVideoSelectionSchema.safeParse(raw);
365
+ export function validateMediaVideoSelection(raw, catalogue = mediaModels) {
366
+ const parsed = catalogueVideoSelectionSchema.safeParse(raw);
264
367
  if (!parsed.success)
265
368
  throw new MediaSettingsError("video selection is invalid");
266
369
  const input = parsed.data;
267
- const model = getMediaModel(input.model);
370
+ const model = getMediaModel(input.model, catalogue);
268
371
  if (model?.video == null)
269
372
  throw new MediaSettingsError("unknown video model");
270
373
  const resolution = input.resolution ?? model.video.defaultResolution;
@@ -277,7 +380,7 @@ export function validateMediaVideoSelection(raw) {
277
380
  ...input,
278
381
  width: format.width,
279
382
  height: format.height,
280
- });
383
+ }, catalogue);
281
384
  const selection = {
282
385
  ...format,
283
386
  model: input.model,
@@ -288,12 +391,25 @@ export function validateMediaVideoSelection(raw) {
288
391
  selection.generateAudio = normalized.generateAudio;
289
392
  if (normalized.references !== undefined)
290
393
  selection.references = Object.freeze(normalized.references.map((reference) => Object.freeze(mediaVideoReferenceSchema.parse(reference))));
394
+ if (normalized.audioReferences !== undefined)
395
+ selection.audioReferences = Object.freeze(normalized.audioReferences.map((reference) => Object.freeze(mediaAudioReferenceSelectionSchema.parse(reference))));
291
396
  return Object.freeze(selection);
292
397
  }
398
+ /** Neutral audio selection: speech takes a voice, music a length; model-specific limits are checked by validateMediaGenerationSettings. */
399
+ export const mediaAudioSelectionSchema = z.lazy(() => z.strictObject({
400
+ /** Verified catalog audio model identifier. */
401
+ model: mediaAudioModelIdSchema,
402
+ /** Text to speak, or a description of the music; the model's `maxPromptLength` is enforced on top of this ceiling. */
403
+ prompt: z.string().min(1).max(MAX_MEDIA_PROMPT_LENGTH),
404
+ /** Speech only: a premade catalog voice; the model default when omitted. */
405
+ voiceId: z.string().min(1).max(64).optional(),
406
+ /** Music only: whole seconds, 3 through 600; 30 when omitted. */
407
+ durationSeconds: z.number().int().min(3).max(600).optional(),
408
+ }));
293
409
  const imageFormats = new Set(["png", "jpeg", "gif", "webp"]);
294
410
  /** Resolve only provider-accepted references through the caller's reader. */
295
- export const resolveMediaReferences = async (settings, reader) => {
296
- const model = getMediaModel(settings.model);
411
+ export const resolveMediaReferences = async (settings, reader, catalogue = mediaModels) => {
412
+ const model = getMediaModel(settings.model, catalogue);
297
413
  if (model === undefined)
298
414
  throw new MediaSettingsError("unknown Media model");
299
415
  const references = settings.references ?? [];
@@ -308,7 +424,7 @@ export const resolveMediaReferences = async (settings, reader) => {
308
424
  !Number.isSafeInteger(image.height) ||
309
425
  image.width < 1 ||
310
426
  image.height < 1 ||
311
- !referenceImageSizeIsValid(settings.model, image.width, image.height) ||
427
+ !referenceImageSizeIsValid(settings.model, image.width, image.height, catalogue) ||
312
428
  !(image.bytes instanceof Uint8Array) ||
313
429
  image.bytes.byteLength === 0 ||
314
430
  image.bytes.byteLength > MAX_REFERENCE_BYTES)
@@ -323,7 +439,7 @@ export const resolveMediaReferences = async (settings, reader) => {
323
439
  const mimeForFormat = (format) => `image/${format}`;
324
440
  /** Build the exact JSON or multipart payload required by a retained model. */
325
441
  export const buildProviderInput = (settings, references = []) => {
326
- const model = getMediaModel(settings.model);
442
+ const model = getGatewayModel(settings.model);
327
443
  if (model === undefined)
328
444
  throw new MediaSettingsError("unknown Media model");
329
445
  if (model.sizes !== null)
@@ -337,6 +453,8 @@ export const buildProviderInput = (settings, references = []) => {
337
453
  prompt: settings.prompt,
338
454
  };
339
455
  if (model.input.dimensions === "own") {
456
+ if (settings.width === undefined || settings.height === undefined)
457
+ throw new MediaSettingsError(`${model.id} needs a width and a height`);
340
458
  body.width = settings.width;
341
459
  body.height = settings.height;
342
460
  }
@@ -604,16 +722,19 @@ export const normalizeMediaProviderOutput = async (value, options = {}) => {
604
722
  };
605
723
  export { estimateImageMilliunits, estimateImageNeurons, } from "./cost-estimate.js";
606
724
  export { neuronsToMilliunits, USD_PER_1K_NEURONS } from "./neurons.js";
607
- /** Provider video cost from the sole catalog, in milliunits (1 cent = 1,000). Video-input references are unsupported. */
608
- export function estimateVideoMilliunits(settings) {
725
+ /** Provider video cost from `catalogue`, in milliunits (1 cent = 1,000). Video-input references are unsupported. */
726
+ export function estimateVideoMilliunits(settings, catalogue = mediaModels) {
609
727
  const checked = validateMediaGenerationSelection({
610
728
  ...settings,
611
729
  references: settings.references?.map((reference) => ({
612
730
  path: reference.path,
613
731
  role: reference.role,
614
732
  })),
615
- });
616
- const model = getMediaModel(checked.model);
733
+ audioReferences: settings.audioReferences?.map((reference) => ({
734
+ path: reference.path,
735
+ })),
736
+ }, catalogue);
737
+ const model = getMediaModel(checked.model, catalogue);
617
738
  if (model?.video == null || checked.durationSeconds === undefined)
618
739
  throw new MediaSettingsError("video cost requires a verified video model");
619
740
  const price = model.video.prices.find((candidate) => candidate.resolution === checked.resolution);
@@ -23,8 +23,6 @@ export declare const mediaImageBindingInputSchema: z.ZodObject<{
23
23
  readonly ultrawide: "21:9";
24
24
  readonly ultratall: "9:21";
25
25
  }>>;
26
- width: z.ZodNumber;
27
- height: z.ZodNumber;
28
26
  references: z.ZodOptional<z.ZodArray<z.ZodObject<{
29
27
  computerId: z.ZodOptional<z.ZodString>;
30
28
  path: z.ZodString;
@@ -37,6 +35,8 @@ export declare const mediaImageBindingInputSchema: z.ZodObject<{
37
35
  model: z.ZodString;
38
36
  prompt: z.ZodString;
39
37
  generateAudio: z.ZodOptional<z.ZodBoolean>;
38
+ width: z.ZodNumber;
39
+ height: z.ZodNumber;
40
40
  steps: z.ZodOptional<z.ZodNumber>;
41
41
  }, z.core.$strict>;
42
42
  outputFormat: z.ZodOptional<z.ZodEnum<{
@@ -87,6 +87,25 @@ export declare const mediaVideoBindingInputSchema: z.ZodObject<{
87
87
  readonly reference: "reference";
88
88
  }>;
89
89
  }, z.core.$strict>>>>;
90
+ audioReferences: z.ZodOptional<z.ZodArray<z.ZodLazy<z.ZodObject<{
91
+ path: z.ZodString;
92
+ }, z.core.$strict>>>>;
93
+ }, z.core.$strict>>;
94
+ }, z.core.$strict>;
95
+ /** Speech or music settings crossing the same scoped binding; audio takes no references. */
96
+ export declare const mediaAudioBindingInputSchema: z.ZodObject<{
97
+ kind: z.ZodLiteral<"audio">;
98
+ requestId: z.ZodLazy<z.ZodString>;
99
+ libraryPath: z.ZodString & z.ZodType<string, string, z.core.$ZodTypeInternals<string, string>>;
100
+ settings: z.ZodLazy<z.ZodObject<{
101
+ model: z.ZodEnum<{
102
+ readonly multilingual: "elevenlabs/eleven-multilingual-v2";
103
+ readonly v3: "elevenlabs/eleven-v3";
104
+ readonly music: "elevenlabs/music-v2";
105
+ }>;
106
+ prompt: z.ZodString;
107
+ voiceId: z.ZodOptional<z.ZodString>;
108
+ durationSeconds: z.ZodOptional<z.ZodNumber>;
90
109
  }, z.core.$strict>>;
91
110
  }, z.core.$strict>;
92
111
  /** One generation identity, including the App-owned folder captured for replay. */
@@ -111,8 +130,6 @@ export declare const mediaBindingGenerationSchema: z.ZodDiscriminatedUnion<[z.Zo
111
130
  readonly ultrawide: "21:9";
112
131
  readonly ultratall: "9:21";
113
132
  }>>;
114
- width: z.ZodNumber;
115
- height: z.ZodNumber;
116
133
  references: z.ZodOptional<z.ZodArray<z.ZodObject<{
117
134
  computerId: z.ZodOptional<z.ZodString>;
118
135
  path: z.ZodString;
@@ -125,6 +142,8 @@ export declare const mediaBindingGenerationSchema: z.ZodDiscriminatedUnion<[z.Zo
125
142
  model: z.ZodString;
126
143
  prompt: z.ZodString;
127
144
  generateAudio: z.ZodOptional<z.ZodBoolean>;
145
+ width: z.ZodNumber;
146
+ height: z.ZodNumber;
128
147
  steps: z.ZodOptional<z.ZodNumber>;
129
148
  }, z.core.$strict>;
130
149
  outputFormat: z.ZodOptional<z.ZodEnum<{
@@ -173,6 +192,23 @@ export declare const mediaBindingGenerationSchema: z.ZodDiscriminatedUnion<[z.Zo
173
192
  readonly reference: "reference";
174
193
  }>;
175
194
  }, z.core.$strict>>>>;
195
+ audioReferences: z.ZodOptional<z.ZodArray<z.ZodLazy<z.ZodObject<{
196
+ path: z.ZodString;
197
+ }, z.core.$strict>>>>;
198
+ }, z.core.$strict>>;
199
+ }, z.core.$strict>, z.ZodObject<{
200
+ kind: z.ZodLiteral<"audio">;
201
+ requestId: z.ZodLazy<z.ZodString>;
202
+ libraryPath: z.ZodString & z.ZodType<string, string, z.core.$ZodTypeInternals<string, string>>;
203
+ settings: z.ZodLazy<z.ZodObject<{
204
+ model: z.ZodEnum<{
205
+ readonly multilingual: "elevenlabs/eleven-multilingual-v2";
206
+ readonly v3: "elevenlabs/eleven-v3";
207
+ readonly music: "elevenlabs/music-v2";
208
+ }>;
209
+ prompt: z.ZodString;
210
+ voiceId: z.ZodOptional<z.ZodString>;
211
+ durationSeconds: z.ZodOptional<z.ZodNumber>;
176
212
  }, z.core.$strict>>;
177
213
  }, z.core.$strict>], "kind">;
178
214
  /** Parsed generation command consumed by either host. */
@@ -204,8 +240,6 @@ export declare const mediaBindingCallSchema: z.ZodDiscriminatedUnion<[z.ZodObjec
204
240
  readonly ultrawide: "21:9";
205
241
  readonly ultratall: "9:21";
206
242
  }>>;
207
- width: z.ZodNumber;
208
- height: z.ZodNumber;
209
243
  references: z.ZodOptional<z.ZodArray<z.ZodObject<{
210
244
  computerId: z.ZodOptional<z.ZodString>;
211
245
  path: z.ZodString;
@@ -218,6 +252,8 @@ export declare const mediaBindingCallSchema: z.ZodDiscriminatedUnion<[z.ZodObjec
218
252
  model: z.ZodString;
219
253
  prompt: z.ZodString;
220
254
  generateAudio: z.ZodOptional<z.ZodBoolean>;
255
+ width: z.ZodNumber;
256
+ height: z.ZodNumber;
221
257
  steps: z.ZodOptional<z.ZodNumber>;
222
258
  }, z.core.$strict>;
223
259
  outputFormat: z.ZodOptional<z.ZodEnum<{
@@ -266,6 +302,23 @@ export declare const mediaBindingCallSchema: z.ZodDiscriminatedUnion<[z.ZodObjec
266
302
  readonly reference: "reference";
267
303
  }>;
268
304
  }, z.core.$strict>>>>;
305
+ audioReferences: z.ZodOptional<z.ZodArray<z.ZodLazy<z.ZodObject<{
306
+ path: z.ZodString;
307
+ }, z.core.$strict>>>>;
308
+ }, z.core.$strict>>;
309
+ }, z.core.$strict>, z.ZodObject<{
310
+ kind: z.ZodLiteral<"audio">;
311
+ requestId: z.ZodLazy<z.ZodString>;
312
+ libraryPath: z.ZodString & z.ZodType<string, string, z.core.$ZodTypeInternals<string, string>>;
313
+ settings: z.ZodLazy<z.ZodObject<{
314
+ model: z.ZodEnum<{
315
+ readonly multilingual: "elevenlabs/eleven-multilingual-v2";
316
+ readonly v3: "elevenlabs/eleven-v3";
317
+ readonly music: "elevenlabs/music-v2";
318
+ }>;
319
+ prompt: z.ZodString;
320
+ voiceId: z.ZodOptional<z.ZodString>;
321
+ durationSeconds: z.ZodOptional<z.ZodNumber>;
269
322
  }, z.core.$strict>>;
270
323
  }, z.core.$strict>], "kind">;
271
324
  }, z.core.$strict>], "operation">;
@@ -294,7 +347,7 @@ export type MediaBindingRequest = Readonly<{
294
347
  path: string;
295
348
  /** Validated references with explicit Computer identities, authorized by the host. */
296
349
  settings: MediaGenerationSettings;
297
- /** Optional image encoding; video output remains MP4. */
350
+ /** Optional image encoding; video output remains MP4 and audio MP3. */
298
351
  outputFormat?: z.output<typeof mediaRequestedImageFormatSchema>;
299
352
  }>;
300
353
  /** Choose the same fixed filename on both hosts without a recursive App lookup. */
@@ -3,7 +3,7 @@ import * as Schema from "effect/Schema";
3
3
  import { pending } from "@fungi.computer/agar/backend-definition";
4
4
  import { mediaRequestIdSchema } from "@fungi.computer/agar/media-request-id";
5
5
  import { mediaLibraryPathSchema } from "@fungi.computer/agar/media-library";
6
- import { getMediaModel, mediaGenerationSelectionSchema, mediaRequestedImageFormatSchema, mediaVideoSelectionSchema, validateMediaGenerationSelection, validateMediaVideoSelection, MediaSettingsError, readResponseBytes, } from "./index.js";
6
+ import { getGatewayModel, mediaAudioSelectionSchema, mediaGenerationSelectionSchema, mediaRequestedImageFormatSchema, mediaVideoSelectionSchema, validateMediaGenerationSelection, validateMediaGenerationSettings, validateMediaVideoSelection, MediaSettingsError, readResponseBytes, } from "./index.js";
7
7
  export { mediaResultSchema } from "./media-operation-contract.js";
8
8
  import { mediaFailureSchema } from "./browser-failure.js";
9
9
  import { appMediaBrokerFailureSchema, appMediaGenerationResultSchema, mediaModelsSchema, } from "./app-media-contracts.js";
@@ -21,8 +21,12 @@ export const mediaImageBindingInputSchema = z.strictObject({
21
21
  libraryPath: mediaBindingLibraryPathSchema,
22
22
  /** Image model selection, references and inference settings. */
23
23
  settings: mediaGenerationSelectionSchema
24
- .omit({ durationSeconds: true })
24
+ .omit({ durationSeconds: true, voiceId: true, audioReferences: true })
25
25
  .extend({
26
+ /** Output width in pixels; an image always names its size. */
27
+ width: z.number().int().min(1).max(5_040),
28
+ /** Output height in pixels; an image always names its size. */
29
+ height: z.number().int().min(1).max(5_040),
26
30
  /** Optional inference-step count for models that support it. */
27
31
  steps: z
28
32
  .number()
@@ -31,7 +35,7 @@ export const mediaImageBindingInputSchema = z.strictObject({
31
35
  .max(Number.MAX_SAFE_INTEGER)
32
36
  .optional(),
33
37
  })
34
- .refine((settings) => getMediaModel(settings.model)?.kind === "image", "Select an image model from listModels"),
38
+ .refine((settings) => getGatewayModel(settings.model)?.kind === "image", "Select an image model from listModels"),
35
39
  /** Requested image encoding, validated against the selected model. */
36
40
  outputFormat: mediaRequestedImageFormatSchema.optional(),
37
41
  });
@@ -46,10 +50,22 @@ export const mediaVideoBindingInputSchema = z.strictObject({
46
50
  /** Catalog-backed video model, size, duration and references. */
47
51
  settings: mediaVideoSelectionSchema,
48
52
  });
53
+ /** Speech or music settings crossing the same scoped binding; audio takes no references. */
54
+ export const mediaAudioBindingInputSchema = z.strictObject({
55
+ /** Select speech or music generation. */
56
+ kind: z.literal("audio"),
57
+ /** Attempt identity retained through polling and replay. */
58
+ requestId: mediaRequestIdSchema,
59
+ /** App-owned absolute folder; the host supplies Computer authority. */
60
+ libraryPath: mediaBindingLibraryPathSchema,
61
+ /** Catalog-backed audio model, prompt, voice and length. */
62
+ settings: mediaAudioSelectionSchema,
63
+ });
49
64
  /** One generation identity, including the App-owned folder captured for replay. */
50
65
  export const mediaBindingGenerationSchema = z.discriminatedUnion("kind", [
51
66
  mediaImageBindingInputSchema,
52
67
  mediaVideoBindingInputSchema,
68
+ mediaAudioBindingInputSchema,
53
69
  ]);
54
70
  /** Maintained MEDIA grammar; reference selectors carry no Computer authority. */
55
71
  export const mediaBindingCallSchema = z.discriminatedUnion("operation", [
@@ -84,6 +100,8 @@ export function parseMediaBindingCall(raw) {
84
100
  try {
85
101
  if (parsed.data.input.kind === "image")
86
102
  validateMediaGenerationSelection(parsed.data.input.settings);
103
+ else if (parsed.data.input.kind === "audio")
104
+ validateMediaGenerationSettings(parsed.data.input.settings);
87
105
  else
88
106
  validateMediaVideoSelection(parsed.data.input.settings);
89
107
  }
@@ -107,26 +125,28 @@ export function parseMediaBindingCall(raw) {
107
125
  export function mediaBindingRequest(input, computerId) {
108
126
  const selected = input.kind === "image"
109
127
  ? validateMediaGenerationSelection(input.settings)
110
- : validateMediaVideoSelection(input.settings);
111
- const { references, ...settings } = selected;
112
- const model = getMediaModel(input.settings.model);
128
+ : input.kind === "audio"
129
+ ? validateMediaGenerationSettings(input.settings)
130
+ : validateMediaVideoSelection(input.settings);
131
+ const { references, audioReferences, ...settings } = selected;
132
+ const model = getGatewayModel(input.settings.model);
113
133
  const format = input.kind === "video"
114
134
  ? "mp4"
115
- : (input.outputFormat ??
116
- (model?.outputFormats.includes("png")
117
- ? "png"
118
- : (model?.outputFormats[0] ?? "png")));
135
+ : input.kind === "audio"
136
+ ? "mp3"
137
+ : (input.outputFormat ??
138
+ (model?.outputFormats.includes("png")
139
+ ? "png"
140
+ : (model?.outputFormats[0] ?? "png")));
141
+ const attach = (selection) => selection.map((reference) => ({
142
+ ...reference,
143
+ computerId: reference.computerId ?? computerId,
144
+ }));
119
145
  let attached = settings;
120
146
  if (references !== undefined)
121
- attached = {
122
- ...settings,
123
- references: references.map((reference) => ({
124
- ...reference,
125
- computerId: "computerId" in reference
126
- ? (reference.computerId ?? computerId)
127
- : computerId,
128
- })),
129
- };
147
+ attached = { ...attached, references: attach(references) };
148
+ if (audioReferences !== undefined)
149
+ attached = { ...attached, audioReferences: attach(audioReferences) };
130
150
  const request = {
131
151
  path: `${input.libraryPath}/prints-${input.requestId}.${format}`,
132
152
  settings: attached,
@@ -166,8 +186,10 @@ export async function readMediaBindingReply(input, computerId, response) {
166
186
  if (result.destination.computerId !== computerId ||
167
187
  (input.input.kind === "video"
168
188
  ? result.format !== "mp4"
169
- : input.input.outputFormat !== undefined &&
170
- result.format !== input.input.outputFormat))
189
+ : input.input.kind === "audio"
190
+ ? result.format !== "mp3"
191
+ : input.input.outputFormat !== undefined &&
192
+ result.format !== input.input.outputFormat))
171
193
  return { error: "unavailable" };
172
194
  return z.json().parse(result);
173
195
  }
@@ -1,6 +1,8 @@
1
1
  import * as Effect from "effect/Effect";
2
2
  import { getMediaModel, validateMediaGenerationSettings, resolveMediaReferences, MediaSettingsError, } from "./index.js";
3
+ import { resolveMediaAudioReferences } from "./audio-references.js";
3
4
  import { estimate } from "./cloudflare/provider.js";
5
+ import { mediaModels } from "./model-catalog.js";
4
6
  import { executeMediaOperation } from "./media-operation.js";
5
7
  import { MediaExecutionError } from "./execution-error.js";
6
8
  import { mediaResultSchema } from "./media-operation-contract.js";
@@ -10,13 +12,15 @@ import { mediaResultSchema } from "./media-operation-contract.js";
10
12
  * same admission, billing and destination-write sequence.
11
13
  */
12
14
  export function runMediaGeneration(host, request, signal) {
15
+ const catalogue = host.catalogue ?? mediaModels;
16
+ const estimator = host.estimate ?? estimate;
13
17
  return Effect.gen(function* () {
14
18
  const settings = yield* Effect.try({
15
- try: () => validateMediaGenerationSettings(request.settings),
19
+ try: () => validateMediaGenerationSettings(request.settings, catalogue),
16
20
  catch: () => new MediaSettingsError("invalid generation settings"),
17
21
  });
18
22
  if (request.outputFormat !== undefined &&
19
- !getMediaModel(settings.model)?.outputFormats.includes(request.outputFormat))
23
+ !getMediaModel(settings.model, catalogue)?.outputFormats.includes(request.outputFormat))
20
24
  return yield* Effect.fail(new MediaSettingsError("model cannot produce the requested image format"));
21
25
  const path = request.path;
22
26
  const beforeAdmit = host.beforeAdmit;
@@ -33,6 +37,7 @@ export function runMediaGeneration(host, request, signal) {
33
37
  owner: host.scope.owner,
34
38
  },
35
39
  requestId: request.requestId,
40
+ catalogue,
36
41
  request: {
37
42
  ...settings,
38
43
  computerId: host.scope.destinationId,
@@ -52,8 +57,15 @@ export function runMediaGeneration(host, request, signal) {
52
57
  throw new MediaSettingsError("reference images are unavailable");
53
58
  return host.destination.readReference(reference, model);
54
59
  },
55
- });
56
- const costMilliunits = estimate(settings, references);
60
+ }, catalogue);
61
+ const audioReferences = await resolveMediaAudioReferences(settings, {
62
+ read: (reference) => {
63
+ if (host.destination.readAudioReference === undefined)
64
+ throw new MediaSettingsError("audio references are unavailable");
65
+ return host.destination.readAudioReference(reference);
66
+ },
67
+ }, catalogue);
68
+ const costMilliunits = estimator(settings, references);
57
69
  // Reference preparation may suspend. This final authority check is
58
70
  // the new-generation admission cutoff, before durable dispatch.
59
71
  try {
@@ -67,7 +79,8 @@ export function runMediaGeneration(host, request, signal) {
67
79
  return {
68
80
  costMilliunits,
69
81
  references,
70
- run: (runSignal) => host.provider(settings, references, runSignal, request.outputFormat),
82
+ audioReferences,
83
+ run: (runSignal) => host.provider(settings, references, runSignal, request.outputFormat, audioReferences),
71
84
  };
72
85
  },
73
86
  },
@@ -79,7 +92,7 @@ export function runMediaGeneration(host, request, signal) {
79
92
  return { kind: "conflict" };
80
93
  if (outcome.kind === "accepted" ||
81
94
  (outcome.kind === "pending" &&
82
- getMediaModel(settings.model)?.kind === "video"))
95
+ getMediaModel(settings.model, catalogue)?.kind !== "image"))
83
96
  // A video generation runs in the background; retry this request id.
84
97
  return {
85
98
  kind: "pending",
@@ -102,8 +115,8 @@ export function runMediaGeneration(host, request, signal) {
102
115
  costMilliunits: outcome.costMilliunits,
103
116
  path,
104
117
  format: mediaResultSchema.shape.format.parse(outcome.format),
105
- height: settings.height,
106
118
  model: settings.model,
119
+ height: settings.height,
107
120
  width: settings.width,
108
121
  };
109
122
  });