@fungi.computer/darkroom 0.9.1 → 0.10.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 +111 -31
- package/dist/app-media-contracts.d.ts +85 -0
- package/dist/browser.d.ts +1 -0
- package/dist/browser.js +1 -0
- package/dist/cloudflare/provider.d.ts +4 -4
- package/dist/cloudflare/provider.js +20 -9
- package/dist/cloudflare/r2-destination.d.ts +48 -0
- package/dist/cloudflare/r2-destination.js +46 -0
- package/dist/cloudflare.d.ts +1 -0
- package/dist/cloudflare.js +1 -0
- package/dist/gateway-models.d.ts +44 -9
- package/dist/gateway-models.js +52 -20
- package/dist/index.d.ts +6 -2
- package/dist/index.js +64 -38
- package/dist/media-binding.d.ts +3 -0
- package/dist/media-content-type.d.ts +5 -0
- package/dist/media-content-type.js +49 -0
- package/dist/media-operation-owner.js +35 -2
- package/dist/media-path.d.ts +3 -0
- package/dist/media-path.js +7 -0
- package/dist/media-run-lifetime.d.ts +5 -0
- package/dist/media-run-lifetime.js +5 -0
- package/dist/model-catalog.d.ts +201 -0
- package/dist/model-catalog.js +220 -9
- package/dist/reference-read.d.ts +24 -0
- package/dist/reference-read.js +26 -0
- package/dist/server.d.ts +2 -0
- package/dist/server.js +5 -5
- package/docs/api/app-media-contracts.md +43 -0
- package/docs/api/browser.md +15 -4
- package/docs/api/cloudflare.md +109 -0
- package/docs/api/index.md +292 -56
- package/docs/api/server.md +123 -52
- package/package.json +2 -2
package/dist/gateway-models.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
import { getGatewayModel, getMediaModel, mediaModels, MediaSettingsError, } from "./model-catalog.js";
|
|
1
|
+
import { getGatewayModel, getMediaModel, mediaModels, MediaSettingsError, transformMilliunits, } from "./model-catalog.js";
|
|
2
2
|
import { validateMediaGenerationSettings, mediaSettingsInput, estimateVideoMilliunits, } from "./index.js";
|
|
3
|
+
import { mediaBillingUnit } from "./model-catalog.js";
|
|
3
4
|
import { AUDIO_CHARACTER_BLOCK } from "./model-catalog.js";
|
|
4
5
|
import { buildElevenLabsInput, estimateElevenLabsMilliunits, } from "./audio-providers/elevenlabs.js";
|
|
5
6
|
import { imageDataUri } from "./video-providers/reference-encoding.js";
|
|
6
7
|
import { videoProviderAdapters, } from "./video-providers/index.js";
|
|
7
8
|
export const isGatewayModel = (id) => {
|
|
8
|
-
if (id === "openai/gpt-image-2" ||
|
|
9
|
+
if (id === "openai/gpt-image-2" ||
|
|
10
|
+
id === "bytedance/seedream-5-pro" ||
|
|
11
|
+
id === "bria/remove-background" ||
|
|
12
|
+
id === "pruna/p-image-upscale")
|
|
9
13
|
return true;
|
|
10
14
|
const kind = getGatewayModel(id)?.kind;
|
|
11
15
|
return kind === "video" || kind === "audio";
|
|
@@ -17,24 +21,20 @@ export function mediaUnits(settings, catalogue = mediaModels) {
|
|
|
17
21
|
const model = getMediaModel(settings.model, catalogue);
|
|
18
22
|
if (model === undefined)
|
|
19
23
|
throw new MediaSettingsError("billing units require a known Media model");
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
throw new MediaSettingsError("music settings need a length");
|
|
35
|
-
return { unit: "audio_second", units: checked.durationSeconds };
|
|
36
|
-
}
|
|
37
|
-
return { unit: "image", units: 1 };
|
|
24
|
+
const unit = mediaBillingUnit(model);
|
|
25
|
+
if (unit === "image")
|
|
26
|
+
return { unit, units: 1 };
|
|
27
|
+
const checked = revalidate(settings, catalogue);
|
|
28
|
+
if (unit === "audio_character_block")
|
|
29
|
+
return {
|
|
30
|
+
unit,
|
|
31
|
+
units: Math.ceil(checked.prompt.length / AUDIO_CHARACTER_BLOCK),
|
|
32
|
+
};
|
|
33
|
+
if (checked.durationSeconds === undefined)
|
|
34
|
+
throw new MediaSettingsError(unit === "video_second"
|
|
35
|
+
? "video settings need a duration"
|
|
36
|
+
: "music settings need a length");
|
|
37
|
+
return { unit, units: checked.durationSeconds };
|
|
38
38
|
}
|
|
39
39
|
/**
|
|
40
40
|
* GPT Image 2 output tokens per image at `medium` quality. OpenAI publishes
|
|
@@ -108,6 +108,13 @@ export function estimateGatewayMilliunits(settings, references) {
|
|
|
108
108
|
if (settings.model === "bytedance/seedream-5-pro")
|
|
109
109
|
return 4_500;
|
|
110
110
|
const model = getGatewayModel(settings.model);
|
|
111
|
+
if (model?.transform != null) {
|
|
112
|
+
// Published per-image prices, held in the catalog's transform facts.
|
|
113
|
+
const price = transformMilliunits(model.transform, settings.targetMegapixels);
|
|
114
|
+
if (price === undefined)
|
|
115
|
+
throw new MediaSettingsError(`${model.id} has no price for this output size`);
|
|
116
|
+
return price;
|
|
117
|
+
}
|
|
111
118
|
if (model?.kind === "video")
|
|
112
119
|
return estimateVideoMilliunits(settings);
|
|
113
120
|
if (model?.kind === "audio")
|
|
@@ -149,6 +156,31 @@ export function gatewayInput(settings, references = [], outputFormat = "png", au
|
|
|
149
156
|
watermark: false,
|
|
150
157
|
};
|
|
151
158
|
}
|
|
159
|
+
if (settings.model === "bria/remove-background" ||
|
|
160
|
+
settings.model === "pruna/p-image-upscale") {
|
|
161
|
+
const [source, ...extra] = references;
|
|
162
|
+
if (source === undefined || extra.length > 0)
|
|
163
|
+
throw new MediaSettingsError(`${settings.model} takes one source image`);
|
|
164
|
+
if (source.format !== "png" && source.format !== "jpeg")
|
|
165
|
+
throw new MediaSettingsError(`${settings.model} accepts png or jpeg source images`);
|
|
166
|
+
// Always the bytes as a data URI, never a URL to our storage.
|
|
167
|
+
// https://developers.cloudflare.com/ai/models/bria/remove-background/
|
|
168
|
+
const image = imageDataUri(source);
|
|
169
|
+
if (settings.model === "bria/remove-background")
|
|
170
|
+
return { image, preserve_alpha: true };
|
|
171
|
+
// Every field is marked required on the model page; the values are its
|
|
172
|
+
// defaults except the output encoding (see the catalog entry).
|
|
173
|
+
// https://developers.cloudflare.com/ai/models/pruna/p-image-upscale/
|
|
174
|
+
return {
|
|
175
|
+
image,
|
|
176
|
+
target: settings.targetMegapixels ?? 4,
|
|
177
|
+
output_format: "webp",
|
|
178
|
+
output_quality: 90,
|
|
179
|
+
enhance_details: false,
|
|
180
|
+
enhance_realism: false,
|
|
181
|
+
disable_safety_checker: false,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
152
184
|
const checked = revalidate(settings);
|
|
153
185
|
const model = getGatewayModel(checked.model);
|
|
154
186
|
if (model?.audio != null)
|
package/dist/index.d.ts
CHANGED
|
@@ -4,8 +4,8 @@ import { type MediaVideoReferenceRole, type MediaCatalogue, type MediaVideoResol
|
|
|
4
4
|
export { mediaFailureStageSchema } from "./execution-error.js";
|
|
5
5
|
export { MAX_AUDIO_REFERENCE_BODY_BYTES, MAX_AUDIO_REFERENCE_BYTES, } from "./audio-reference-types.js";
|
|
6
6
|
export type { AudioReferenceMimeType, MediaAudioReferenceBytes, } from "./audio-reference-types.js";
|
|
7
|
-
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, mediaAudioModelIdSchema, mediaAudioSchema, mediaAudioVoiceSchema, AUDIO_CHARACTER_BLOCK, } from "./model-catalog.js";
|
|
8
|
-
export type { MediaCatalogue, MediaCatalogueModel, MediaModel, MediaModelInput, MediaModelTier, MediaVideoModelId, MediaVideoResolution, MediaVideoAspectRatio, MediaVideoReferenceRole, MediaVideoModel, MediaVideoFormat, MediaAudioModelId, MediaAudioModel, MediaAudioVoice, } from "./model-catalog.js";
|
|
7
|
+
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, mediaAudioModelIdSchema, mediaAudioSchema, mediaAudioVoiceSchema, mediaBillingUnitSchema, AUDIO_CHARACTER_BLOCK, mediaTransformSchema, mediaTransformOperationSchema, } from "./model-catalog.js";
|
|
8
|
+
export type { MediaTransform, MediaTransformOperation, MediaCatalogue, MediaBillingUnit, MediaCatalogueModel, MediaModel, MediaModelInput, MediaModelTier, MediaVideoModelId, MediaVideoResolution, MediaVideoAspectRatio, MediaVideoReferenceRole, MediaVideoModel, MediaVideoFormat, MediaAudioModelId, MediaAudioModel, MediaAudioVoice, } from "./model-catalog.js";
|
|
9
9
|
/** A reference file selected by the host, with its destination identity and path. */
|
|
10
10
|
export type MediaReference = Readonly<{
|
|
11
11
|
/** Host-admitted destination or reference identity. */
|
|
@@ -48,6 +48,8 @@ export type MediaGenerationSettings = Readonly<{
|
|
|
48
48
|
steps?: number;
|
|
49
49
|
/** Video and music models only: output length in whole seconds. */
|
|
50
50
|
durationSeconds?: number;
|
|
51
|
+
/** Upscale only: the output size in megapixels. */
|
|
52
|
+
targetMegapixels?: number;
|
|
51
53
|
/** Speech models only: the catalog voice that reads the prompt. */
|
|
52
54
|
voiceId?: string;
|
|
53
55
|
/** Video resolution selector, normalized from the catalog. */
|
|
@@ -108,6 +110,7 @@ export declare const mediaGenerationSettingsSchema: z.ZodObject<{
|
|
|
108
110
|
height: z.ZodOptional<z.ZodNumber>;
|
|
109
111
|
steps: z.ZodOptional<z.ZodNumber>;
|
|
110
112
|
durationSeconds: z.ZodOptional<z.ZodNumber>;
|
|
113
|
+
targetMegapixels: z.ZodOptional<z.ZodNumber>;
|
|
111
114
|
voiceId: z.ZodOptional<z.ZodString>;
|
|
112
115
|
resolution: z.ZodOptional<z.ZodEnum<{
|
|
113
116
|
readonly p480: "480p";
|
|
@@ -150,6 +153,7 @@ export declare const mediaGenerationSelectionSchema: z.ZodObject<{
|
|
|
150
153
|
height: z.ZodOptional<z.ZodNumber>;
|
|
151
154
|
steps: z.ZodOptional<z.ZodNumber>;
|
|
152
155
|
durationSeconds: z.ZodOptional<z.ZodNumber>;
|
|
156
|
+
targetMegapixels: z.ZodOptional<z.ZodNumber>;
|
|
153
157
|
voiceId: z.ZodOptional<z.ZodString>;
|
|
154
158
|
resolution: z.ZodOptional<z.ZodEnum<{
|
|
155
159
|
readonly p480: "480p";
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import * as z from "zod";
|
|
2
2
|
import { reportCatchFailure } from "./failure.js";
|
|
3
|
+
import { sniffImageFormat } from "./media-content-type.js";
|
|
3
4
|
import { referenceImageSizeIsValid } from "./reference-image-size.js";
|
|
4
|
-
import { getGatewayModel, getMediaModel, mediaAudioModelIdSchema, MAX_REFERENCE_COUNT, MediaSettingsError, mediaModels, mediaVideoModelIdSchema, mediaVideoResolutionSchema, mediaVideoAspectRatioSchema, mediaVideoReferenceRoleSchema, MAX_MEDIA_PROMPT_LENGTH, } from "./model-catalog.js";
|
|
5
|
+
import { getGatewayModel, getMediaModel, mediaAudioModelIdSchema, MAX_REFERENCE_COUNT, MediaSettingsError, mediaModels, mediaVideoModelIdSchema, mediaVideoResolutionSchema, mediaVideoAspectRatioSchema, mediaVideoReferenceRoleSchema, MAX_MEDIA_PROMPT_LENGTH, transformMilliunits, } from "./model-catalog.js";
|
|
5
6
|
export { mediaFailureStageSchema } from "./execution-error.js";
|
|
6
7
|
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, 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";
|
|
8
|
+
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, mediaAudioModelIdSchema, mediaAudioSchema, mediaAudioVoiceSchema, mediaBillingUnitSchema, AUDIO_CHARACTER_BLOCK, mediaTransformSchema, mediaTransformOperationSchema, } from "./model-catalog.js";
|
|
8
9
|
/** The long-edge bounds a viewer may ask for when fitting an image to look at. */
|
|
9
10
|
export const MIN_VIEW_DIMENSION = 256;
|
|
10
11
|
/** Maximum long-edge size accepted by an image viewer, in pixels. */
|
|
@@ -61,6 +62,8 @@ export const mediaGenerationSettingsSchema = z
|
|
|
61
62
|
steps: z.number().int().optional(),
|
|
62
63
|
/** Requested video or music length in whole seconds. */
|
|
63
64
|
durationSeconds: z.number().int().optional(),
|
|
65
|
+
/** Upscale only: the output size in megapixels, one of the model's offered sizes. */
|
|
66
|
+
targetMegapixels: z.number().int().optional(),
|
|
64
67
|
/** Speech models only: a premade voice from the catalog; the default when omitted. */
|
|
65
68
|
voiceId: z.string().min(1).max(64).optional(),
|
|
66
69
|
/** Video resolution; must match the nominal width/height selector. */
|
|
@@ -181,6 +184,10 @@ function normalizeGenerationSettings(input, normalize, catalogue) {
|
|
|
181
184
|
}
|
|
182
185
|
if (input.voiceId !== undefined)
|
|
183
186
|
throw new MediaSettingsError(`${model.id} does not take a voice`);
|
|
187
|
+
if (model.transform !== null)
|
|
188
|
+
return normalizeTransformSettings(model, model.transform, prompt, input, normalize);
|
|
189
|
+
if (input.targetMegapixels !== undefined)
|
|
190
|
+
throw new MediaSettingsError(`${model.id} does not take an output size`);
|
|
184
191
|
if (input.width === undefined || input.height === undefined)
|
|
185
192
|
throw new MediaSettingsError(`${model.id} needs a width and a height`);
|
|
186
193
|
const maximumDimension = model.video === null ? 4_096 : 5_040;
|
|
@@ -395,6 +402,40 @@ export function validateMediaVideoSelection(raw, catalogue = mediaModels) {
|
|
|
395
402
|
selection.audioReferences = Object.freeze(normalized.audioReferences.map((reference) => Object.freeze(mediaAudioReferenceSelectionSchema.parse(reference))));
|
|
396
403
|
return Object.freeze(selection);
|
|
397
404
|
}
|
|
405
|
+
/**
|
|
406
|
+
* A source-image operation takes exactly one reference and, for an upscale, one
|
|
407
|
+
* of the model's offered output sizes. Its output size is the provider's, so it
|
|
408
|
+
* takes no width, height, steps or video settings.
|
|
409
|
+
*/
|
|
410
|
+
function normalizeTransformSettings(model, transform, prompt, input, normalize) {
|
|
411
|
+
if (input.width !== undefined ||
|
|
412
|
+
input.height !== undefined ||
|
|
413
|
+
input.steps !== undefined ||
|
|
414
|
+
input.durationSeconds !== undefined ||
|
|
415
|
+
input.resolution !== undefined ||
|
|
416
|
+
input.aspectRatio !== undefined ||
|
|
417
|
+
input.generateAudio !== undefined ||
|
|
418
|
+
input.audioReferences !== undefined)
|
|
419
|
+
throw new MediaSettingsError(`${model.id} takes one source image${transform.defaultMegapixels === null ? "" : " and an output size"}`);
|
|
420
|
+
const references = input.references;
|
|
421
|
+
if (references?.length !== 1 || references[0]?.role !== undefined)
|
|
422
|
+
throw new MediaSettingsError(`${model.id} needs exactly one source image`);
|
|
423
|
+
const settings = {
|
|
424
|
+
model: model.id,
|
|
425
|
+
prompt,
|
|
426
|
+
references: Object.freeze(references.map(normalize)),
|
|
427
|
+
};
|
|
428
|
+
if (transform.defaultMegapixels === null) {
|
|
429
|
+
if (input.targetMegapixels !== undefined)
|
|
430
|
+
throw new MediaSettingsError(`${model.id} does not take an output size`);
|
|
431
|
+
return Object.freeze(settings);
|
|
432
|
+
}
|
|
433
|
+
const target = input.targetMegapixels ?? transform.defaultMegapixels;
|
|
434
|
+
if (transformMilliunits(transform, target) === undefined)
|
|
435
|
+
throw new MediaSettingsError(`${model.id} accepts output sizes of ${transform.sizes.map((price) => String(price.megapixels)).join(", ")} megapixels`);
|
|
436
|
+
settings.targetMegapixels = target;
|
|
437
|
+
return Object.freeze(settings);
|
|
438
|
+
}
|
|
398
439
|
/** Neutral audio selection: speech takes a voice, music a length; model-specific limits are checked by validateMediaGenerationSettings. */
|
|
399
440
|
export const mediaAudioSelectionSchema = z.lazy(() => z.strictObject({
|
|
400
441
|
/** Verified catalog audio model identifier. */
|
|
@@ -407,6 +448,25 @@ export const mediaAudioSelectionSchema = z.lazy(() => z.strictObject({
|
|
|
407
448
|
durationSeconds: z.number().int().min(3).max(600).optional(),
|
|
408
449
|
}));
|
|
409
450
|
const imageFormats = new Set(["png", "jpeg", "gif", "webp"]);
|
|
451
|
+
/**
|
|
452
|
+
* Refuse a source image the operation cannot take, with plain copy, before any
|
|
453
|
+
* paid call: an unsupported encoding, a source over Botanical's size bounds,
|
|
454
|
+
* or an upscale whose source is already as large as the chosen output.
|
|
455
|
+
*/
|
|
456
|
+
function checkTransformSource(model, transform, settings, image) {
|
|
457
|
+
const { source } = transform;
|
|
458
|
+
const named = (format) => (format === "jpeg" ? "JPEG" : "PNG");
|
|
459
|
+
if (!source.formats.some((format) => format === image.format))
|
|
460
|
+
throw new MediaSettingsError(`${model.label} takes ${source.formats.map(named).join(" or ")} images, not ${image.format.toUpperCase()}.`);
|
|
461
|
+
if (image.bytes.byteLength > source.maxBytes)
|
|
462
|
+
throw new MediaSettingsError(`That image is over ${String(source.maxBytes / (1024 * 1024))} MB, the most ${model.label} takes.`);
|
|
463
|
+
const pixels = image.width * image.height;
|
|
464
|
+
if (pixels > source.maxPixels)
|
|
465
|
+
throw new MediaSettingsError(`That image is over ${String(source.maxPixels / 1_000_000)} megapixels, the most ${model.label} takes.`);
|
|
466
|
+
const target = settings.targetMegapixels;
|
|
467
|
+
if (target !== undefined && pixels >= target * 1_000_000)
|
|
468
|
+
throw new MediaSettingsError(`That image is already ${(pixels / 1_000_000).toFixed(1)} megapixels. Choose a larger output than ${String(target)} megapixels.`);
|
|
469
|
+
}
|
|
410
470
|
/** Resolve only provider-accepted references through the caller's reader. */
|
|
411
471
|
export const resolveMediaReferences = async (settings, reader, catalogue = mediaModels) => {
|
|
412
472
|
const model = getMediaModel(settings.model, catalogue);
|
|
@@ -429,6 +489,8 @@ export const resolveMediaReferences = async (settings, reader, catalogue = media
|
|
|
429
489
|
image.bytes.byteLength === 0 ||
|
|
430
490
|
image.bytes.byteLength > MAX_REFERENCE_BYTES)
|
|
431
491
|
throw new MediaSettingsError(`reference ${reference.computerId}:${reference.path} exceeds the bounded image contract`);
|
|
492
|
+
if (model.transform !== null)
|
|
493
|
+
checkTransformSource(model, model.transform, settings, image);
|
|
432
494
|
totalBytes += image.bytes.byteLength;
|
|
433
495
|
if (totalBytes > MAX_REFERENCE_BODY_BYTES)
|
|
434
496
|
throw new MediaSettingsError("reference images exceed the total request size limit");
|
|
@@ -552,42 +614,6 @@ export const parseMediaProviderOutput = (value) => {
|
|
|
552
614
|
entries: parsed.images,
|
|
553
615
|
};
|
|
554
616
|
};
|
|
555
|
-
const sniffImageFormat = (bytes) => {
|
|
556
|
-
if (bytes.length >= 8 &&
|
|
557
|
-
bytes[0] === 0x89 &&
|
|
558
|
-
bytes[1] === 0x50 &&
|
|
559
|
-
bytes[2] === 0x4e &&
|
|
560
|
-
bytes[3] === 0x47 &&
|
|
561
|
-
bytes[4] === 0x0d &&
|
|
562
|
-
bytes[5] === 0x0a &&
|
|
563
|
-
bytes[6] === 0x1a &&
|
|
564
|
-
bytes[7] === 0x0a)
|
|
565
|
-
return "png";
|
|
566
|
-
if (bytes.length >= 3 &&
|
|
567
|
-
bytes[0] === 0xff &&
|
|
568
|
-
bytes[1] === 0xd8 &&
|
|
569
|
-
bytes[2] === 0xff)
|
|
570
|
-
return "jpeg";
|
|
571
|
-
if (bytes.length >= 6 &&
|
|
572
|
-
bytes[0] === 0x47 &&
|
|
573
|
-
bytes[1] === 0x49 &&
|
|
574
|
-
bytes[2] === 0x46 &&
|
|
575
|
-
bytes[3] === 0x38 &&
|
|
576
|
-
(bytes[4] === 0x37 || bytes[4] === 0x39) &&
|
|
577
|
-
bytes[5] === 0x61)
|
|
578
|
-
return "gif";
|
|
579
|
-
if (bytes.length >= 12 &&
|
|
580
|
-
bytes[0] === 0x52 &&
|
|
581
|
-
bytes[1] === 0x49 &&
|
|
582
|
-
bytes[2] === 0x46 &&
|
|
583
|
-
bytes[3] === 0x46 &&
|
|
584
|
-
bytes[8] === 0x57 &&
|
|
585
|
-
bytes[9] === 0x45 &&
|
|
586
|
-
bytes[10] === 0x42 &&
|
|
587
|
-
bytes[11] === 0x50)
|
|
588
|
-
return "webp";
|
|
589
|
-
return undefined;
|
|
590
|
-
};
|
|
591
617
|
const decodeEncoded = (value, maximumBytes) => {
|
|
592
618
|
const comma = value.startsWith("data:") ? value.indexOf(",") : -1;
|
|
593
619
|
const payload = comma >= 0 ? value.slice(comma + 1) : value;
|
package/dist/media-binding.d.ts
CHANGED
|
@@ -34,6 +34,7 @@ export declare const mediaImageBindingInputSchema: z.ZodObject<{
|
|
|
34
34
|
}, z.core.$strict>>>;
|
|
35
35
|
model: z.ZodString;
|
|
36
36
|
prompt: z.ZodString;
|
|
37
|
+
targetMegapixels: z.ZodOptional<z.ZodNumber>;
|
|
37
38
|
generateAudio: z.ZodOptional<z.ZodBoolean>;
|
|
38
39
|
width: z.ZodNumber;
|
|
39
40
|
height: z.ZodNumber;
|
|
@@ -141,6 +142,7 @@ export declare const mediaBindingGenerationSchema: z.ZodDiscriminatedUnion<[z.Zo
|
|
|
141
142
|
}, z.core.$strict>>>;
|
|
142
143
|
model: z.ZodString;
|
|
143
144
|
prompt: z.ZodString;
|
|
145
|
+
targetMegapixels: z.ZodOptional<z.ZodNumber>;
|
|
144
146
|
generateAudio: z.ZodOptional<z.ZodBoolean>;
|
|
145
147
|
width: z.ZodNumber;
|
|
146
148
|
height: z.ZodNumber;
|
|
@@ -251,6 +253,7 @@ export declare const mediaBindingCallSchema: z.ZodDiscriminatedUnion<[z.ZodObjec
|
|
|
251
253
|
}, z.core.$strict>>>;
|
|
252
254
|
model: z.ZodString;
|
|
253
255
|
prompt: z.ZodString;
|
|
256
|
+
targetMegapixels: z.ZodOptional<z.ZodNumber>;
|
|
254
257
|
generateAudio: z.ZodOptional<z.ZodBoolean>;
|
|
255
258
|
width: z.ZodNumber;
|
|
256
259
|
height: z.ZodNumber;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ImageFormat } from "./index.js";
|
|
2
|
+
/** The image encoding named by the file's magic bytes, if any. */
|
|
3
|
+
export declare const sniffImageFormat: (bytes: Uint8Array) => ImageFormat | undefined;
|
|
4
|
+
/** The MIME type for generated bytes, from their magic numbers. */
|
|
5
|
+
export declare const mediaContentType: (bytes: Uint8Array) => string;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { isMp4 } from "./gateway-wire.js";
|
|
2
|
+
/** The image encoding named by the file's magic bytes, if any. */
|
|
3
|
+
export const sniffImageFormat = (bytes) => {
|
|
4
|
+
if (bytes.length >= 8 &&
|
|
5
|
+
bytes[0] === 0x89 &&
|
|
6
|
+
bytes[1] === 0x50 &&
|
|
7
|
+
bytes[2] === 0x4e &&
|
|
8
|
+
bytes[3] === 0x47 &&
|
|
9
|
+
bytes[4] === 0x0d &&
|
|
10
|
+
bytes[5] === 0x0a &&
|
|
11
|
+
bytes[6] === 0x1a &&
|
|
12
|
+
bytes[7] === 0x0a)
|
|
13
|
+
return "png";
|
|
14
|
+
if (bytes.length >= 3 &&
|
|
15
|
+
bytes[0] === 0xff &&
|
|
16
|
+
bytes[1] === 0xd8 &&
|
|
17
|
+
bytes[2] === 0xff)
|
|
18
|
+
return "jpeg";
|
|
19
|
+
if (bytes.length >= 6 &&
|
|
20
|
+
bytes[0] === 0x47 &&
|
|
21
|
+
bytes[1] === 0x49 &&
|
|
22
|
+
bytes[2] === 0x46 &&
|
|
23
|
+
bytes[3] === 0x38 &&
|
|
24
|
+
(bytes[4] === 0x37 || bytes[4] === 0x39) &&
|
|
25
|
+
bytes[5] === 0x61)
|
|
26
|
+
return "gif";
|
|
27
|
+
if (bytes.length >= 12 &&
|
|
28
|
+
bytes[0] === 0x52 &&
|
|
29
|
+
bytes[1] === 0x49 &&
|
|
30
|
+
bytes[2] === 0x46 &&
|
|
31
|
+
bytes[3] === 0x46 &&
|
|
32
|
+
bytes[8] === 0x57 &&
|
|
33
|
+
bytes[9] === 0x45 &&
|
|
34
|
+
bytes[10] === 0x42 &&
|
|
35
|
+
bytes[11] === 0x50)
|
|
36
|
+
return "webp";
|
|
37
|
+
return undefined;
|
|
38
|
+
};
|
|
39
|
+
/** The MIME type for generated bytes, from their magic numbers. */
|
|
40
|
+
export const mediaContentType = (bytes) => {
|
|
41
|
+
const image = sniffImageFormat(bytes);
|
|
42
|
+
if (image !== undefined)
|
|
43
|
+
return `image/${image}`;
|
|
44
|
+
if (isMp4(bytes))
|
|
45
|
+
return "video/mp4";
|
|
46
|
+
const id3 = bytes[0] === 0x49 && bytes[1] === 0x44 && bytes[2] === 0x33;
|
|
47
|
+
const frame = bytes[0] === 0xff && ((bytes[1] ?? 0) & 0xe0) === 0xe0;
|
|
48
|
+
return id3 || frame ? "audio/mpeg" : "application/octet-stream";
|
|
49
|
+
};
|
|
@@ -4,6 +4,7 @@ import { mediaGenerationSettingsSchema, validateMediaGenerationSettings, } from
|
|
|
4
4
|
import { z } from "zod";
|
|
5
5
|
import { reportCatchFailure } from "./failure.js";
|
|
6
6
|
import { MediaExecutionError, MediaSafetyFilterRefusal, mediaFailureStageSchema, } from "./execution-error.js";
|
|
7
|
+
import { backgroundProviderTimeoutMs } from "./media-run-lifetime.js";
|
|
7
8
|
import { mediaProviderDiagnosticWireSchema, } from "@fungi.computer/agar/errors";
|
|
8
9
|
const providerFailureSchema = z.strictObject({
|
|
9
10
|
stage: mediaFailureStageSchema.extract(["provider", "normalization"]),
|
|
@@ -721,7 +722,20 @@ export class MediaOperationOwner {
|
|
|
721
722
|
return;
|
|
722
723
|
if (row.dispatched_at !== null) {
|
|
723
724
|
// A previous alarm dispatched and then died: never call the provider
|
|
724
|
-
// again.
|
|
725
|
+
// again. No output was retained (the state is still generating), so end
|
|
726
|
+
// the operation now rather than leaving every poll pending until the
|
|
727
|
+
// lease lapses.
|
|
728
|
+
reportCatchFailure("media-operation-alarm-retry", new Error("Background media alarm died after dispatch"));
|
|
729
|
+
try {
|
|
730
|
+
this.interruptGeneration(leaseId, {
|
|
731
|
+
stage: "provider",
|
|
732
|
+
safetyRefused: false,
|
|
733
|
+
});
|
|
734
|
+
}
|
|
735
|
+
catch (cause) {
|
|
736
|
+
reportCatchFailure("media-operation-interrupt", cause);
|
|
737
|
+
/* A recovery claim already ended this lease. */
|
|
738
|
+
}
|
|
725
739
|
return;
|
|
726
740
|
}
|
|
727
741
|
this.ctx.storage.sql.exec("UPDATE hub_media_operation SET dispatched_at=? WHERE singleton=1", Date.now());
|
|
@@ -752,7 +766,26 @@ export class MediaOperationOwner {
|
|
|
752
766
|
}));
|
|
753
767
|
// The captured request includes destination authority; providers receive settings only.
|
|
754
768
|
const settings = validateMediaGenerationSettings(mediaGenerationSettingsSchema.strip().parse(record.input), this.catalogue());
|
|
755
|
-
|
|
769
|
+
const controller = new AbortController();
|
|
770
|
+
let timer;
|
|
771
|
+
const timedOut = new Promise((_resolve, reject) => {
|
|
772
|
+
timer = setTimeout(() => {
|
|
773
|
+
const timeout = new MediaExecutionError("provider", {
|
|
774
|
+
cause: new Error("Background media provider timed out"),
|
|
775
|
+
});
|
|
776
|
+
controller.abort(timeout);
|
|
777
|
+
reject(timeout);
|
|
778
|
+
}, backgroundProviderTimeoutMs);
|
|
779
|
+
});
|
|
780
|
+
try {
|
|
781
|
+
output = await Promise.race([
|
|
782
|
+
provider(settings, references, controller.signal, record.input.outputFormat, audioReferences),
|
|
783
|
+
timedOut,
|
|
784
|
+
]);
|
|
785
|
+
}
|
|
786
|
+
finally {
|
|
787
|
+
clearTimeout(timer);
|
|
788
|
+
}
|
|
756
789
|
}
|
|
757
790
|
catch (cause) {
|
|
758
791
|
reportCatchFailure("media-operation-provider", cause);
|