@fungi.computer/darkroom 0.7.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 (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 +169 -4
  24. package/dist/model-catalog.js +180 -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 +598 -84
  35. package/docs/api/server.md +129 -62
  36. 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 { getGatewayModel, getMediaModel, MAX_REFERENCE_COUNT, MediaSettingsError, mediaModels, 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, getGatewayModel, MAX_REFERENCE_COUNT, listMediaModels, mediaCatalogueSchema, mediaCatalogueModelSchema, parseMediaCatalogue, 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,6 +113,26 @@ 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
137
  /**
101
138
  * Validate caller settings against `catalogue`, the active provider's model
@@ -130,6 +167,22 @@ function normalizeGenerationSettings(input, normalize, catalogue) {
130
167
  const prompt = requiredString(input.prompt, "prompt");
131
168
  if (prompt.length > model.maxPromptLength)
132
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`);
133
186
  const maximumDimension = model.video === null ? 4_096 : 5_040;
134
187
  const width = boundedInteger(input.width, "width", 1, maximumDimension);
135
188
  const height = boundedInteger(input.height, "height", 1, maximumDimension);
@@ -231,6 +284,36 @@ function normalizeGenerationSettings(input, normalize, catalogue) {
231
284
  }
232
285
  if (references !== undefined)
233
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);
234
317
  return Object.freeze(normalized);
235
318
  }
236
319
  /** Path-only video reference; the host supplies authority and reads its bytes. */
@@ -240,6 +323,11 @@ export const mediaVideoReferenceSchema = z.lazy(() => z.strictObject({
240
323
  /** Explicit first frame, last frame, or reference-image role. */
241
324
  role: mediaVideoReferenceRoleSchema,
242
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
+ }));
243
331
  /** Neutral video selection; model-specific combinations are checked by validateMediaVideoSelection. */
244
332
  export const mediaVideoSelectionSchema = z.lazy(() => z.strictObject({
245
333
  /** Verified catalog model identifier. */
@@ -260,6 +348,11 @@ export const mediaVideoSelectionSchema = z.lazy(() => z.strictObject({
260
348
  .array(mediaVideoReferenceSchema)
261
349
  .max(MAX_VIDEO_REFERENCE_COUNT)
262
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(),
263
356
  }));
264
357
  /**
265
358
  * The same selection for a host's own catalogue: the model is any id that
@@ -298,8 +391,21 @@ export function validateMediaVideoSelection(raw, catalogue = mediaModels) {
298
391
  selection.generateAudio = normalized.generateAudio;
299
392
  if (normalized.references !== undefined)
300
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))));
301
396
  return Object.freeze(selection);
302
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
+ }));
303
409
  const imageFormats = new Set(["png", "jpeg", "gif", "webp"]);
304
410
  /** Resolve only provider-accepted references through the caller's reader. */
305
411
  export const resolveMediaReferences = async (settings, reader, catalogue = mediaModels) => {
@@ -347,6 +453,8 @@ export const buildProviderInput = (settings, references = []) => {
347
453
  prompt: settings.prompt,
348
454
  };
349
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`);
350
458
  body.width = settings.width;
351
459
  body.height = settings.height;
352
460
  }
@@ -622,6 +730,9 @@ export function estimateVideoMilliunits(settings, catalogue = mediaModels) {
622
730
  path: reference.path,
623
731
  role: reference.role,
624
732
  })),
733
+ audioReferences: settings.audioReferences?.map((reference) => ({
734
+ path: reference.path,
735
+ })),
625
736
  }, catalogue);
626
737
  const model = getMediaModel(checked.model, catalogue);
627
738
  if (model?.video == null || checked.durationSeconds === undefined)
@@ -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 { getGatewayModel, 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()
@@ -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;
128
+ : input.kind === "audio"
129
+ ? validateMediaGenerationSettings(input.settings)
130
+ : validateMediaVideoSelection(input.settings);
131
+ const { references, audioReferences, ...settings } = selected;
112
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,5 +1,6 @@
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";
4
5
  import { mediaModels } from "./model-catalog.js";
5
6
  import { executeMediaOperation } from "./media-operation.js";
@@ -57,6 +58,13 @@ export function runMediaGeneration(host, request, signal) {
57
58
  return host.destination.readReference(reference, model);
58
59
  },
59
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);
60
68
  const costMilliunits = estimator(settings, references);
61
69
  // Reference preparation may suspend. This final authority check is
62
70
  // the new-generation admission cutoff, before durable dispatch.
@@ -71,7 +79,8 @@ export function runMediaGeneration(host, request, signal) {
71
79
  return {
72
80
  costMilliunits,
73
81
  references,
74
- run: (runSignal) => host.provider(settings, references, runSignal, request.outputFormat),
82
+ audioReferences,
83
+ run: (runSignal) => host.provider(settings, references, runSignal, request.outputFormat, audioReferences),
75
84
  };
76
85
  },
77
86
  },
@@ -83,7 +92,7 @@ export function runMediaGeneration(host, request, signal) {
83
92
  return { kind: "conflict" };
84
93
  if (outcome.kind === "accepted" ||
85
94
  (outcome.kind === "pending" &&
86
- getMediaModel(settings.model, catalogue)?.kind === "video"))
95
+ getMediaModel(settings.model, catalogue)?.kind !== "image"))
87
96
  // A video generation runs in the background; retry this request id.
88
97
  return {
89
98
  kind: "pending",
@@ -106,8 +115,8 @@ export function runMediaGeneration(host, request, signal) {
106
115
  costMilliunits: outcome.costMilliunits,
107
116
  path,
108
117
  format: mediaResultSchema.shape.format.parse(outcome.format),
109
- height: settings.height,
110
118
  model: settings.model,
119
+ height: settings.height,
111
120
  width: settings.width,
112
121
  };
113
122
  });
@@ -1,7 +1,7 @@
1
1
  import * as z from "zod";
2
2
  import type { MediaOperationOwner, MediaOperationCoordinates, MediaOperationRequest } from "./media-operation-owner.js";
3
3
  import type { MediaUnits } from "./gateway-models.js";
4
- import type { MediaCatalogue, MediaReferenceBytes } from "./index.js";
4
+ import type { MediaAudioReferenceBytes, MediaCatalogue, MediaReferenceBytes } from "./index.js";
5
5
  /**
6
6
  * The host's billing owner for Media. Every call is keyed by the durable
7
7
  * operation id and must be idempotent: a lost acknowledgement is retried with
@@ -73,6 +73,8 @@ export type MediaExecution = Readonly<{
73
73
  costMilliunits: number;
74
74
  /** Host-authorized reference images. */
75
75
  references: readonly MediaReferenceBytes[];
76
+ /** Host-authorized reference audio clips, parsed and within the model's budget. */
77
+ audioReferences: readonly MediaAudioReferenceBytes[];
76
78
  /** Dispatch one provider call with the prepared inputs. */
77
79
  run: (signal?: AbortSignal) => Promise<Readonly<{
78
80
  /** Generated output bytes. */
@@ -99,8 +101,10 @@ export declare const mediaResultSchema: z.ZodObject<{
99
101
  readonly webp: "webp";
100
102
  /** MP4 video encoding. */
101
103
  readonly mp4: "mp4";
104
+ /** MP3 audio encoding. */
105
+ readonly mp3: "mp3";
102
106
  }>;
103
- height: z.ZodNumber;
107
+ height: z.ZodOptional<z.ZodNumber>;
104
108
  model: z.ZodString;
105
- width: z.ZodNumber;
109
+ width: z.ZodOptional<z.ZodNumber>;
106
110
  }, z.core.$strict>;
@@ -19,11 +19,13 @@ export const mediaResultSchema = z.strictObject({
19
19
  webp: "webp",
20
20
  /** MP4 video encoding. */
21
21
  mp4: "mp4",
22
+ /** MP3 audio encoding. */
23
+ mp3: "mp3",
22
24
  }),
23
- /** Requested image height or nominal video selector, not decoded MP4 dimensions. */
24
- height: z.number().int().positive(),
25
+ /** Requested image height or nominal video selector, not decoded MP4 dimensions; absent for audio. */
26
+ height: z.number().int().positive().optional(),
25
27
  /** Provider catalog model identifier. */
26
28
  model: z.string().min(1).max(256),
27
- /** Requested image width or nominal video selector, not decoded MP4 dimensions. */
28
- width: z.number().int().positive(),
29
+ /** Requested image width or nominal video selector, not decoded MP4 dimensions; absent for audio. */
30
+ width: z.number().int().positive().optional(),
29
31
  });
@@ -2,7 +2,7 @@ import { type MediaCatalogue } from "./model-catalog.js";
2
2
  import { type MediaGenerationSettings } from "./index.js";
3
3
  import { z } from "zod";
4
4
  import type { MediaOperationContext, MediaOperationBucket } from "./media-operation-storage.js";
5
- import type { MediaProvider, MediaReferenceBytes } from "./index.js";
5
+ import type { MediaAudioReferenceBytes, MediaProvider, MediaReferenceBytes } from "./index.js";
6
6
  import { type MediaFailureStage } from "./execution-error.js";
7
7
  import { type MediaProviderDiagnostic } from "@fungi.computer/agar/errors";
8
8
  /** Fixed generation destination captured before provider dispatch. */
@@ -256,7 +256,7 @@ export declare class MediaOperationOwner {
256
256
  * returns at once; the alarm dispatches the provider exactly once, retains
257
257
  * the output, and releases the lease so a later retry saves and charges it.
258
258
  */
259
- dispatchInBackground(leaseId: string, references: readonly MediaReferenceBytes[]): Promise<MediaOperationProjection>;
259
+ dispatchInBackground(leaseId: string, references: readonly MediaReferenceBytes[], audioReferences: readonly MediaAudioReferenceBytes[]): Promise<MediaOperationProjection>;
260
260
  /** Run a background generation once; a retried alarm never redispatches. */
261
261
  alarm(): Promise<void>;
262
262
  /** An aborted in-flight provider call is terminal for this request ID. */