@fungi.computer/darkroom 0.9.2 → 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 +79 -3
- 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 +1 -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/model-catalog.js
CHANGED
|
@@ -285,6 +285,102 @@ export const AUDIO_CHARACTER_BLOCK = 100;
|
|
|
285
285
|
export const MAX_MEDIA_PROMPT_LENGTH = 32_000;
|
|
286
286
|
/** Prompt cap kept for models whose provider documents no prompt limit. */
|
|
287
287
|
const UNDOCUMENTED_PROMPT_LENGTH = 2_048;
|
|
288
|
+
/**
|
|
289
|
+
* What a source-image operation does to its one reference image. Both write a
|
|
290
|
+
* new image and never touch the source.
|
|
291
|
+
*/
|
|
292
|
+
export const mediaTransformOperationSchema = z.enum({
|
|
293
|
+
/** Cut the foreground out onto a transparent PNG. */
|
|
294
|
+
remove_background: "remove_background",
|
|
295
|
+
/** Enlarge the image to a chosen output size in megapixels. */
|
|
296
|
+
upscale: "upscale",
|
|
297
|
+
});
|
|
298
|
+
/**
|
|
299
|
+
* Facts of a catalog entry that edits one source image rather than generating
|
|
300
|
+
* from a prompt. The source limits are Botanical's own conservative bounds, not
|
|
301
|
+
* Cloudflare's: neither model page publishes an input size limit.
|
|
302
|
+
*/
|
|
303
|
+
export const mediaTransformSchema = z.lazy(() => z
|
|
304
|
+
.strictObject({
|
|
305
|
+
/** What the operation does. */
|
|
306
|
+
operation: mediaTransformOperationSchema,
|
|
307
|
+
/** Botanical's limits on the one source image, checked before any paid call. */
|
|
308
|
+
source: z
|
|
309
|
+
.strictObject({
|
|
310
|
+
/** Accepted source encodings. */
|
|
311
|
+
formats: z.array(mediaRequestedImageFormatSchema).readonly(),
|
|
312
|
+
/** Largest source, in bytes (the hosts' existing reference read limit). */
|
|
313
|
+
maxBytes: z.number().int().positive(),
|
|
314
|
+
/** Largest source, in pixels. */
|
|
315
|
+
maxPixels: z.number().int().positive(),
|
|
316
|
+
})
|
|
317
|
+
.readonly(),
|
|
318
|
+
/** Output size chosen when none is given, or null when the operation has no size. */
|
|
319
|
+
defaultMegapixels: z.number().int().positive().nullable(),
|
|
320
|
+
/**
|
|
321
|
+
* Provider milliunits for one image when the price does not depend on
|
|
322
|
+
* the output size, else null (1 USD = 100,000 milliunits).
|
|
323
|
+
*/
|
|
324
|
+
milliunits: z.number().int().positive().nullable(),
|
|
325
|
+
/** Output sizes offered, each with its provider milliunits; empty for a flat price. */
|
|
326
|
+
sizes: z
|
|
327
|
+
.array(z
|
|
328
|
+
.strictObject({
|
|
329
|
+
/** Output size in megapixels. */
|
|
330
|
+
megapixels: z.number().int().positive(),
|
|
331
|
+
/** Provider milliunits for one image at this size. */
|
|
332
|
+
milliunits: z.number().int().positive(),
|
|
333
|
+
})
|
|
334
|
+
.readonly())
|
|
335
|
+
.readonly(),
|
|
336
|
+
})
|
|
337
|
+
.readonly());
|
|
338
|
+
/**
|
|
339
|
+
* Pruna P-Image-Upscale's published price per image by output size, copied
|
|
340
|
+
* exactly: [largest output megapixels in the tier, USD].
|
|
341
|
+
* https://developers.cloudflare.com/ai/models/pruna/p-image-upscale/
|
|
342
|
+
*/
|
|
343
|
+
const UPSCALE_PRICE_TIERS = [
|
|
344
|
+
[4, 0.005],
|
|
345
|
+
[8, 0.01],
|
|
346
|
+
[16, 0.02],
|
|
347
|
+
[32, 0.04],
|
|
348
|
+
[64, 0.06],
|
|
349
|
+
[128, 0.12],
|
|
350
|
+
];
|
|
351
|
+
/** The output sizes Botanical offers; the provider accepts 1 to 128. */
|
|
352
|
+
const UPSCALE_TARGET_MEGAPIXELS = [4, 8, 16];
|
|
353
|
+
const USD_TO_MILLIUNITS = 100_000;
|
|
354
|
+
/** Bria RMBG 2.0's published price per image: https://developers.cloudflare.com/ai/models/bria/remove-background/ */
|
|
355
|
+
const REMOVE_BACKGROUND_USD = 0.018;
|
|
356
|
+
/** Botanical's own source bounds for both operations (see {@link mediaTransformSchema}). */
|
|
357
|
+
const TRANSFORM_SOURCE_MAX_BYTES = 8 * 1024 * 1024;
|
|
358
|
+
const TRANSFORM_SOURCE_MAX_PIXELS = 25_000_000;
|
|
359
|
+
/**
|
|
360
|
+
* What one generation bills: whole images, whole seconds of video or music, or
|
|
361
|
+
* blocks of {@link AUDIO_CHARACTER_BLOCK} speech characters (so a per-character
|
|
362
|
+
* provider rate is marked up once per block, not rounded up per character).
|
|
363
|
+
*/
|
|
364
|
+
export const mediaBillingUnitSchema = z.enum({
|
|
365
|
+
/** One image. */
|
|
366
|
+
image: "image",
|
|
367
|
+
/** One whole second of video. */
|
|
368
|
+
video_second: "video_second",
|
|
369
|
+
/** One whole second of music. */
|
|
370
|
+
audio_second: "audio_second",
|
|
371
|
+
/** One block of speech characters. */
|
|
372
|
+
audio_character_block: "audio_character_block",
|
|
373
|
+
});
|
|
374
|
+
/** The one rule mapping a model's kind (and speech or music mode) to its billing unit. */
|
|
375
|
+
export const mediaBillingUnit = (model) => {
|
|
376
|
+
if (model.kind === "video")
|
|
377
|
+
return "video_second";
|
|
378
|
+
if (model.kind === "audio")
|
|
379
|
+
return model.audio?.mode === "speech"
|
|
380
|
+
? "audio_character_block"
|
|
381
|
+
: "audio_second";
|
|
382
|
+
return "image";
|
|
383
|
+
};
|
|
288
384
|
/** Public wire grammar for a catalog entry returned by the Media owner. */
|
|
289
385
|
export const mediaModelSchema = z
|
|
290
386
|
.strictObject({
|
|
@@ -324,17 +420,33 @@ export const mediaModelSchema = z
|
|
|
324
420
|
video: mediaVideoSchema.nullable(),
|
|
325
421
|
/** Speech or music constraints, or null for an image or video model. */
|
|
326
422
|
audio: mediaAudioSchema.nullable(),
|
|
423
|
+
/**
|
|
424
|
+
* Price basis: the unit one generation bills in. The cost of those units
|
|
425
|
+
* depends on the settings (size, steps, resolution, sound, references), so
|
|
426
|
+
* an App shows `priceEstimate` where its host supplies one and otherwise
|
|
427
|
+
* asks its host for a quote of the exact settings.
|
|
428
|
+
*/
|
|
429
|
+
billingUnit: mediaBillingUnitSchema,
|
|
430
|
+
/** Source-image operation facts, or null for a model that generates from a prompt. */
|
|
431
|
+
transform: mediaTransformSchema.nullable(),
|
|
327
432
|
})
|
|
328
433
|
.readonly();
|
|
329
|
-
const freezeModel = (model) =>
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
434
|
+
const freezeModel = (model) => {
|
|
435
|
+
const entry = {
|
|
436
|
+
kind: "image",
|
|
437
|
+
sizes: null,
|
|
438
|
+
video: null,
|
|
439
|
+
audio: null,
|
|
440
|
+
transform: null,
|
|
441
|
+
outputFormats: Object.freeze([]),
|
|
442
|
+
...model,
|
|
443
|
+
};
|
|
444
|
+
return Object.freeze({
|
|
445
|
+
...entry,
|
|
446
|
+
billingUnit: mediaBillingUnit(entry),
|
|
447
|
+
input: Object.freeze(model.input),
|
|
448
|
+
});
|
|
449
|
+
};
|
|
338
450
|
/**
|
|
339
451
|
* GPT Image 2's `size` is `auto` or any WIDTHxHEIGHT with each edge at most
|
|
340
452
|
* 3840, both edges multiples of 16, an aspect ratio between 1:3 and 3:1 and a
|
|
@@ -704,6 +816,58 @@ function audioModel(id, label, tier, maxPromptLength, audio) {
|
|
|
704
816
|
},
|
|
705
817
|
});
|
|
706
818
|
}
|
|
819
|
+
/** The published tier a chosen output size falls in; every offered size has one. */
|
|
820
|
+
function upscaleTierUsd(megapixels) {
|
|
821
|
+
const tier = UPSCALE_PRICE_TIERS.find(([largest]) => megapixels <= largest);
|
|
822
|
+
if (tier === undefined)
|
|
823
|
+
throw new Error(`no published upscale price for ${String(megapixels)} MP`);
|
|
824
|
+
return tier[1];
|
|
825
|
+
}
|
|
826
|
+
/** A source-image operation model: one reference in, one new image out, no prompt text sent. */
|
|
827
|
+
function transformModel(input) {
|
|
828
|
+
return freezeModel({
|
|
829
|
+
id: input.id,
|
|
830
|
+
label: input.label,
|
|
831
|
+
tier: input.tier,
|
|
832
|
+
outputFormats: Object.freeze([input.outputFormat]),
|
|
833
|
+
// The prompt only labels the saved output ("Remove background"); the
|
|
834
|
+
// provider never reads it.
|
|
835
|
+
maxPromptLength: 256,
|
|
836
|
+
transform: Object.freeze({
|
|
837
|
+
operation: input.operation,
|
|
838
|
+
source: Object.freeze({
|
|
839
|
+
formats: Object.freeze(["png", "jpeg"]),
|
|
840
|
+
maxBytes: TRANSFORM_SOURCE_MAX_BYTES,
|
|
841
|
+
maxPixels: TRANSFORM_SOURCE_MAX_PIXELS,
|
|
842
|
+
}),
|
|
843
|
+
defaultMegapixels: input.defaultMegapixels,
|
|
844
|
+
milliunits: input.flatUsd === null
|
|
845
|
+
? null
|
|
846
|
+
: Math.round(input.flatUsd * USD_TO_MILLIUNITS),
|
|
847
|
+
sizes: Object.freeze(input.sizes.map(({ megapixels, usd }) => Object.freeze({
|
|
848
|
+
megapixels,
|
|
849
|
+
milliunits: Math.round(usd * USD_TO_MILLIUNITS),
|
|
850
|
+
}))),
|
|
851
|
+
}),
|
|
852
|
+
input: {
|
|
853
|
+
transport: "json",
|
|
854
|
+
dimensions: "none",
|
|
855
|
+
maxDimension: null,
|
|
856
|
+
maxDimensionDocumented: false,
|
|
857
|
+
defaultWidth: 1024,
|
|
858
|
+
defaultHeight: 1024,
|
|
859
|
+
steps: "fixed",
|
|
860
|
+
maxSteps: null,
|
|
861
|
+
defaultSteps: null,
|
|
862
|
+
referenceField: "image",
|
|
863
|
+
maxReferences: 1,
|
|
864
|
+
// The hosts' reference fitter only shrinks; this keeps any source within
|
|
865
|
+
// the pixel bound at its original size.
|
|
866
|
+
referenceDimension: 8_192,
|
|
867
|
+
referenceFormat: "png",
|
|
868
|
+
},
|
|
869
|
+
});
|
|
870
|
+
}
|
|
707
871
|
const models = Object.freeze([
|
|
708
872
|
freezeModel({
|
|
709
873
|
id: "@cf/black-forest-labs/flux-1-schnell",
|
|
@@ -913,6 +1077,31 @@ const models = Object.freeze([
|
|
|
913
1077
|
referenceFormat: "png",
|
|
914
1078
|
},
|
|
915
1079
|
}),
|
|
1080
|
+
transformModel({
|
|
1081
|
+
id: "bria/remove-background",
|
|
1082
|
+
label: "Remove background — Bria RMBG 2.0",
|
|
1083
|
+
tier: "draft",
|
|
1084
|
+
operation: "remove_background",
|
|
1085
|
+
outputFormat: "png",
|
|
1086
|
+
flatUsd: REMOVE_BACKGROUND_USD,
|
|
1087
|
+
sizes: [],
|
|
1088
|
+
defaultMegapixels: null,
|
|
1089
|
+
}),
|
|
1090
|
+
transformModel({
|
|
1091
|
+
id: "pruna/p-image-upscale",
|
|
1092
|
+
label: "Upscale — Pruna P-Image-Upscale",
|
|
1093
|
+
tier: "draft",
|
|
1094
|
+
operation: "upscale",
|
|
1095
|
+
// Lossless output would swell past the output byte bound at 16 megapixels;
|
|
1096
|
+
// WebP keeps transparency.
|
|
1097
|
+
outputFormat: "webp",
|
|
1098
|
+
flatUsd: null,
|
|
1099
|
+
sizes: UPSCALE_TARGET_MEGAPIXELS.map((megapixels) => ({
|
|
1100
|
+
megapixels,
|
|
1101
|
+
usd: upscaleTierUsd(megapixels),
|
|
1102
|
+
})),
|
|
1103
|
+
defaultMegapixels: 4,
|
|
1104
|
+
}),
|
|
916
1105
|
videoModel(mediaVideoModelIdSchema.enum.seedance25, "Seedance 2.5", "balanced", "seedance", [
|
|
917
1106
|
["480p", 10_280, 10_280],
|
|
918
1107
|
["720p", 23_120, 23_120],
|
|
@@ -955,8 +1144,12 @@ export const mediaModels = models;
|
|
|
955
1144
|
export const mediaCatalogueModelSchema = mediaModelSchema
|
|
956
1145
|
.unwrap()
|
|
957
1146
|
.extend({
|
|
1147
|
+
/** Optional on a host's entry because Darkroom derives it; a stated value must match. */
|
|
1148
|
+
billingUnit: mediaBillingUnitSchema.optional(),
|
|
958
1149
|
/** Speech or music constraints; a host's catalogue omits it unless it lists audio models. */
|
|
959
1150
|
audio: mediaAudioSchema.unwrap().nullable().default(null),
|
|
1151
|
+
/** Source-image operation facts; a host's catalogue omits it unless it lists such models. */
|
|
1152
|
+
transform: mediaTransformSchema.unwrap().nullable().default(null),
|
|
960
1153
|
/** Video length constraints, or null for a still-image model. */
|
|
961
1154
|
video: mediaVideoSchema
|
|
962
1155
|
.unwrap()
|
|
@@ -1008,6 +1201,9 @@ export const mediaCatalogueSchema = z
|
|
|
1008
1201
|
report(`${model.id} must carry video bounds exactly when kind is video`);
|
|
1009
1202
|
if ((model.kind === "audio") !== (model.audio !== null))
|
|
1010
1203
|
report(`${model.id} must carry audio bounds exactly when kind is audio`);
|
|
1204
|
+
if (model.billingUnit !== undefined &&
|
|
1205
|
+
model.billingUnit !== mediaBillingUnit(model))
|
|
1206
|
+
report(`${model.id} billingUnit must be ${mediaBillingUnit(model)} for ${model.kind === "image" ? "an" : "a"} ${model.kind} model`);
|
|
1011
1207
|
const video = model.video;
|
|
1012
1208
|
if (video === null)
|
|
1013
1209
|
return;
|
|
@@ -1028,6 +1224,21 @@ export const parseMediaCatalogue = (raw) => {
|
|
|
1028
1224
|
throw new MediaSettingsError(`media catalogue is invalid: ${parsed.error.issues[0]?.message ?? "unreadable"}`);
|
|
1029
1225
|
return parsed.data;
|
|
1030
1226
|
};
|
|
1227
|
+
/** A catalogue entry as the public `/models` list returns it: the entry plus its derived billing unit. */
|
|
1228
|
+
export const withBillingUnit = (model) => ({
|
|
1229
|
+
...model,
|
|
1230
|
+
billingUnit: mediaBillingUnit(model),
|
|
1231
|
+
});
|
|
1232
|
+
/**
|
|
1233
|
+
* Provider milliunits for one image of a source-image operation at `megapixels`
|
|
1234
|
+
* (omitted: the model's default), or undefined when that output size is not offered.
|
|
1235
|
+
*/
|
|
1236
|
+
export const transformMilliunits = (transform, megapixels) => {
|
|
1237
|
+
if (transform.milliunits !== null)
|
|
1238
|
+
return megapixels === undefined ? transform.milliunits : undefined;
|
|
1239
|
+
const size = megapixels ?? transform.defaultMegapixels;
|
|
1240
|
+
return transform.sizes.find((price) => price.megapixels === size)?.milliunits;
|
|
1241
|
+
};
|
|
1031
1242
|
/** List the gateway provider's models in catalog order. */
|
|
1032
1243
|
export const listMediaModels = () => mediaModels;
|
|
1033
1244
|
/** Find a model in the Fungi gateway's own catalogue; gateway-internal code never sees a custom provider's models. */
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type MediaReference } from "./index.js";
|
|
2
|
+
/** One stored reference: its size is known before its bytes are loaded. */
|
|
3
|
+
export type HostReferenceSource = Readonly<{
|
|
4
|
+
/** Stored byte length, checked before the bytes are read. */
|
|
5
|
+
size: number;
|
|
6
|
+
/** Load the exact stored bytes. */
|
|
7
|
+
bytes: () => Promise<Uint8Array>;
|
|
8
|
+
}>;
|
|
9
|
+
/**
|
|
10
|
+
* The one owner of a host reference read's refusals. The reference must name
|
|
11
|
+
* the host's admitted destination, exist, be non-empty and fit the byte limit
|
|
12
|
+
* for its kind; anything else is a {@link MediaSettingsError} raised before
|
|
13
|
+
* any provider dispatch.
|
|
14
|
+
*/
|
|
15
|
+
export declare function readHostReference(input: Readonly<{
|
|
16
|
+
reference: MediaReference;
|
|
17
|
+
/** The host's admitted destination identity. */
|
|
18
|
+
destinationId: string;
|
|
19
|
+
kind: "image" | "audio";
|
|
20
|
+
/** Open the reference path in the host's store, or null when missing. */
|
|
21
|
+
open: (path: string) => Promise<HostReferenceSource | null>;
|
|
22
|
+
}>): Promise<Uint8Array>;
|
|
23
|
+
/** The filename sent with a multipart reference: the last path segment. */
|
|
24
|
+
export declare function referenceFilename(reference: MediaReference): string;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { MAX_AUDIO_REFERENCE_BYTES } from "./audio-reference-types.js";
|
|
2
|
+
import { MediaSettingsError } from "./model-catalog.js";
|
|
3
|
+
import { MAX_REFERENCE_BYTES } from "./index.js";
|
|
4
|
+
/**
|
|
5
|
+
* The one owner of a host reference read's refusals. The reference must name
|
|
6
|
+
* the host's admitted destination, exist, be non-empty and fit the byte limit
|
|
7
|
+
* for its kind; anything else is a {@link MediaSettingsError} raised before
|
|
8
|
+
* any provider dispatch.
|
|
9
|
+
*/
|
|
10
|
+
export async function readHostReference(input) {
|
|
11
|
+
const { reference, kind } = input;
|
|
12
|
+
if (reference.computerId !== input.destinationId)
|
|
13
|
+
throw new MediaSettingsError("reference Computer is not selected");
|
|
14
|
+
const limit = kind === "image" ? MAX_REFERENCE_BYTES : MAX_AUDIO_REFERENCE_BYTES;
|
|
15
|
+
const source = await input.open(reference.path);
|
|
16
|
+
if (source === null || source.size === 0 || source.size > limit)
|
|
17
|
+
throw new MediaSettingsError(`${kind} reference is unavailable or exceeds the byte limit`);
|
|
18
|
+
const bytes = await source.bytes();
|
|
19
|
+
if (bytes.byteLength === 0 || bytes.byteLength > limit)
|
|
20
|
+
throw new MediaSettingsError(`${kind} reference is unavailable or exceeds the byte limit`);
|
|
21
|
+
return bytes;
|
|
22
|
+
}
|
|
23
|
+
/** The filename sent with a multipart reference: the last path segment. */
|
|
24
|
+
export function referenceFilename(reference) {
|
|
25
|
+
return reference.path.split("/").at(-1) ?? "reference";
|
|
26
|
+
}
|
package/dist/server.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { type MediaExecution, type MediaMetering } from "./media-operation-contr
|
|
|
7
7
|
import { MediaExecutionError, type MediaBillingRefusalCode } from "./execution-error.js";
|
|
8
8
|
export { createDailyMediaMetering, DailyMediaMeter, type DailyMediaMeterPort, type DailyMediaMeterStorage, } from "./daily-media-meter.js";
|
|
9
9
|
export { MediaBillingRefusal, MediaExecutionError, type MediaBillingRefusalCode, type MediaFailureStage, type MediaExecutionErrorOptions, } from "./execution-error.js";
|
|
10
|
+
export { readHostReference, referenceFilename, type HostReferenceSource, } from "./reference-read.js";
|
|
10
11
|
export { mediaUnits, type MediaUnits } from "./gateway-models.js";
|
|
11
12
|
export { quoteMediaGeneration, type MediaQuote } from "./media-quote.js";
|
|
12
13
|
export type { MediaMetering, MediaOperation, } from "./media-operation-contract.js";
|
|
@@ -23,6 +24,7 @@ export declare const mediaRequestSchema: z.ZodObject<{
|
|
|
23
24
|
height: z.ZodOptional<z.ZodNumber>;
|
|
24
25
|
steps: z.ZodOptional<z.ZodNumber>;
|
|
25
26
|
durationSeconds: z.ZodOptional<z.ZodNumber>;
|
|
27
|
+
targetMegapixels: z.ZodOptional<z.ZodNumber>;
|
|
26
28
|
voiceId: z.ZodOptional<z.ZodString>;
|
|
27
29
|
resolution: z.ZodOptional<z.ZodEnum<{
|
|
28
30
|
readonly p480: "480p";
|
package/dist/server.js
CHANGED
|
@@ -2,10 +2,12 @@ import { mediaPendingSchema } from "./media-binding.js";
|
|
|
2
2
|
export { estimateMediaModel } from "./cloudflare/provider.js";
|
|
3
3
|
export { mediaPendingSchema, mediaBindingCallSchema, mediaBindingGenerationSchema, mediaImageBindingInputSchema, mediaVideoBindingInputSchema, parseMediaBindingCall, mediaBindingRequest, readMediaBindingReply, } from "./media-binding.js";
|
|
4
4
|
import { Hono } from "hono";
|
|
5
|
+
import { withBillingUnit } from "./model-catalog.js";
|
|
5
6
|
import { z } from "zod";
|
|
6
7
|
import { AppError, describeAppError } from "@fungi.computer/agar/errors";
|
|
7
8
|
import * as Effect from "effect/Effect";
|
|
8
9
|
import * as Result from "effect/Result";
|
|
10
|
+
import { mediaPathSchema } from "./media-path.js";
|
|
9
11
|
import { runMediaGeneration } from "./media-generation.js";
|
|
10
12
|
import { quoteMediaGeneration } from "./media-quote.js";
|
|
11
13
|
import { validateMediaGenerationSettings, listMediaModels, mediaGenerationSettingsSchema, mediaRequestedImageFormatSchema, MediaSettingsError, } from "./index.js";
|
|
@@ -13,6 +15,7 @@ import { mediaResultSchema, } from "./media-operation-contract.js";
|
|
|
13
15
|
import { MediaOutputFormatMismatch, MediaBillingRefusal, MediaExecutionError, MediaSafetyFilterRefusal, } from "./execution-error.js";
|
|
14
16
|
export { createDailyMediaMetering, DailyMediaMeter, } from "./daily-media-meter.js";
|
|
15
17
|
export { MediaBillingRefusal, MediaExecutionError, } from "./execution-error.js";
|
|
18
|
+
export { readHostReference, referenceFilename, } from "./reference-read.js";
|
|
16
19
|
export { mediaUnits } from "./gateway-models.js";
|
|
17
20
|
export { quoteMediaGeneration } from "./media-quote.js";
|
|
18
21
|
export { MediaOperationOwner, mediaOperationDestinationSchema, mediaOperationCoordinatesSchema, mediaOperationStateSchema, mediaImageSchema, mediaOperationResultSchema, } from "./media-operation-owner.js";
|
|
@@ -20,11 +23,7 @@ const identifier = z.lazy(() => z.string().min(1).max(256));
|
|
|
20
23
|
/** Decode relative service generation input. Identity comes from the host. */
|
|
21
24
|
export const mediaRequestSchema = z.strictObject({
|
|
22
25
|
/** Fixed destination or reference file path. */
|
|
23
|
-
path:
|
|
24
|
-
.string()
|
|
25
|
-
.min(1)
|
|
26
|
-
.max(1_024)
|
|
27
|
-
.refine((path) => !path.includes("\0")),
|
|
26
|
+
path: mediaPathSchema,
|
|
28
27
|
/** Requested model, dimensions, prompt and references. */
|
|
29
28
|
settings: mediaGenerationSettingsSchema,
|
|
30
29
|
/** Optional verified image encoding pin. */
|
|
@@ -203,6 +202,7 @@ function outcomeResponse(outcome) {
|
|
|
203
202
|
export function createMediaService(host) {
|
|
204
203
|
const app = new Hono();
|
|
205
204
|
app.get("/models", () => Response.json((host.catalogue ?? listMediaModels())
|
|
205
|
+
.map(withBillingUnit)
|
|
206
206
|
.map((model) => host.destination.readReference !== undefined ||
|
|
207
207
|
model.input.maxReferences === 0
|
|
208
208
|
? model
|
|
@@ -27,6 +27,7 @@ const mediaModelPriceEstimateSchema: ZodObject<{
|
|
|
27
27
|
height: ZodOptional<ZodNumber>;
|
|
28
28
|
steps: ZodOptional<ZodNumber>;
|
|
29
29
|
durationSeconds: ZodOptional<ZodNumber>;
|
|
30
|
+
targetMegapixels: ZodOptional<ZodNumber>;
|
|
30
31
|
voiceId: ZodOptional<ZodString>;
|
|
31
32
|
resolution: ZodOptional<ZodEnum<{
|
|
32
33
|
p480: "480p";
|
|
@@ -84,6 +85,7 @@ const mediaCatalogModelSchema: ZodReadonly<ZodObject<{
|
|
|
84
85
|
height: ZodOptional<ZodNumber>;
|
|
85
86
|
steps: ZodOptional<ZodNumber>;
|
|
86
87
|
durationSeconds: ZodOptional<ZodNumber>;
|
|
88
|
+
targetMegapixels: ZodOptional<ZodNumber>;
|
|
87
89
|
voiceId: ZodOptional<ZodString>;
|
|
88
90
|
resolution: ZodOptional<ZodEnum<{
|
|
89
91
|
p480: ...;
|
|
@@ -200,6 +202,26 @@ const mediaCatalogModelSchema: ZodReadonly<ZodObject<{
|
|
|
200
202
|
seconds: ZodNullable<ZodReadonly<ZodObject<..., ...>>>;
|
|
201
203
|
milliunitsPerUnit: ZodNumber;
|
|
202
204
|
}, $strict>>>>;
|
|
205
|
+
billingUnit: ZodEnum<{
|
|
206
|
+
image: "image";
|
|
207
|
+
video_second: "video_second";
|
|
208
|
+
audio_second: "audio_second";
|
|
209
|
+
audio_character_block: "audio_character_block";
|
|
210
|
+
}>;
|
|
211
|
+
transform: ZodNullable<ZodLazy<ZodReadonly<ZodObject<{
|
|
212
|
+
operation: ZodEnum<{
|
|
213
|
+
remove_background: "remove_background";
|
|
214
|
+
upscale: "upscale";
|
|
215
|
+
}>;
|
|
216
|
+
source: ZodReadonly<ZodObject<{
|
|
217
|
+
formats: ...;
|
|
218
|
+
maxBytes: ...;
|
|
219
|
+
maxPixels: ...;
|
|
220
|
+
}, $strict>>;
|
|
221
|
+
defaultMegapixels: ZodNullable<ZodNumber>;
|
|
222
|
+
milliunits: ZodNullable<ZodNumber>;
|
|
223
|
+
sizes: ZodReadonly<ZodArray<ZodReadonly<...>>>;
|
|
224
|
+
}, $strict>>>>;
|
|
203
225
|
}, $strict>>;
|
|
204
226
|
```
|
|
205
227
|
|
|
@@ -225,6 +247,7 @@ const mediaModelsSchema: ZodLazy<ZodArray<ZodReadonly<ZodObject<{
|
|
|
225
247
|
height: ZodOptional<...>;
|
|
226
248
|
steps: ZodOptional<...>;
|
|
227
249
|
durationSeconds: ZodOptional<...>;
|
|
250
|
+
targetMegapixels: ZodOptional<...>;
|
|
228
251
|
voiceId: ZodOptional<...>;
|
|
229
252
|
resolution: ZodOptional<...>;
|
|
230
253
|
aspectRatio: ZodOptional<...>;
|
|
@@ -306,6 +329,19 @@ const mediaModelsSchema: ZodLazy<ZodArray<ZodReadonly<ZodObject<{
|
|
|
306
329
|
seconds: ZodNullable<...>;
|
|
307
330
|
milliunitsPerUnit: ZodNumber;
|
|
308
331
|
}, $strict>>>>;
|
|
332
|
+
billingUnit: ZodEnum<{
|
|
333
|
+
image: "image";
|
|
334
|
+
video_second: "video_second";
|
|
335
|
+
audio_second: "audio_second";
|
|
336
|
+
audio_character_block: "audio_character_block";
|
|
337
|
+
}>;
|
|
338
|
+
transform: ZodNullable<ZodLazy<ZodReadonly<ZodObject<{
|
|
339
|
+
operation: ZodEnum<...>;
|
|
340
|
+
source: ZodReadonly<...>;
|
|
341
|
+
defaultMegapixels: ZodNullable<...>;
|
|
342
|
+
milliunits: ZodNullable<...>;
|
|
343
|
+
sizes: ZodReadonly<...>;
|
|
344
|
+
}, $strict>>>>;
|
|
309
345
|
}, $strict>>>>;
|
|
310
346
|
```
|
|
311
347
|
|
|
@@ -615,6 +651,13 @@ const appMediaModelsOutcomeSchema: ZodUnion<readonly [ZodLazy<ZodArray<ZodReadon
|
|
|
615
651
|
sizes: ZodNullable<ZodReadonly<ZodArray<ZodString>>>;
|
|
616
652
|
video: ZodNullable<ZodLazy<ZodReadonly<ZodObject<..., ...>>>>;
|
|
617
653
|
audio: ZodNullable<ZodLazy<ZodReadonly<ZodObject<..., ...>>>>;
|
|
654
|
+
billingUnit: ZodEnum<{
|
|
655
|
+
image: "image";
|
|
656
|
+
video_second: "video_second";
|
|
657
|
+
audio_second: "audio_second";
|
|
658
|
+
audio_character_block: "audio_character_block";
|
|
659
|
+
}>;
|
|
660
|
+
transform: ZodNullable<ZodLazy<ZodReadonly<ZodObject<..., ...>>>>;
|
|
618
661
|
}, $strict>>>>, ZodUnion<readonly [ZodObject<{
|
|
619
662
|
error: ZodEnum<{
|
|
620
663
|
revoked: "revoked";
|
package/docs/api/browser.md
CHANGED
|
@@ -78,6 +78,17 @@ const mediaFailureSchema: ZodObject<{
|
|
|
78
78
|
Sanitized HTTP refusal emitted by a Media host and decoded at the browser
|
|
79
79
|
boundary.
|
|
80
80
|
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
### backgroundProviderTimeoutMs
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
const backgroundProviderTimeoutMs: number;
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
The longest a background provider call runs before it is cut off, safely inside
|
|
90
|
+
the 15-minute alarm wall-clock limit that would kill the call.
|
|
91
|
+
|
|
81
92
|
## Functions
|
|
82
93
|
|
|
83
94
|
### readMediaFailure()
|
|
@@ -90,12 +101,12 @@ function readMediaFailure(
|
|
|
90
101
|
error: string;
|
|
91
102
|
stage:
|
|
92
103
|
| "provider"
|
|
104
|
+
| "operation"
|
|
93
105
|
| "admission"
|
|
94
106
|
| "generation-authorization"
|
|
95
107
|
| "settings"
|
|
96
108
|
| "estimate"
|
|
97
109
|
| "billing"
|
|
98
|
-
| "operation"
|
|
99
110
|
| "normalization"
|
|
100
111
|
| "artifact"
|
|
101
112
|
| "destination-authorization"
|
|
@@ -125,9 +136,9 @@ Read and decode one Media host failure at the browser HTTP boundary.
|
|
|
125
136
|
|
|
126
137
|
#### Returns
|
|
127
138
|
|
|
128
|
-
`Promise`\< \| \{ `error`: `string`; `stage`: \| `"provider"` \| `"
|
|
129
|
-
\| `"
|
|
130
|
-
\| `"
|
|
139
|
+
`Promise`\< \| \{ `error`: `string`; `stage`: \| `"provider"` \| `"operation"`
|
|
140
|
+
\| `"admission"` \| `"generation-authorization"` \| `"settings"` \| `"estimate"`
|
|
141
|
+
\| `"billing"` \| `"normalization"` \| `"artifact"` \|
|
|
131
142
|
`"destination-authorization"` \| `"destination-write"`; `code`: \|
|
|
132
143
|
`"CAPABILITY_DENIED"` \| `"DAILY_LIMIT_REACHED"` \| `"INSUFFICIENT_CREDIT"` \|
|
|
133
144
|
`"HOST_FAILURE"` \| `"INVALID_ARGUMENT"` \| `"MEDIA_PROVIDER_FAILURE"` \|
|
package/docs/api/cloudflare.md
CHANGED
|
@@ -600,6 +600,94 @@ Dispatch to the native Workers AI binding.
|
|
|
600
600
|
|
|
601
601
|
---
|
|
602
602
|
|
|
603
|
+
### MediaR2Bucket
|
|
604
|
+
|
|
605
|
+
```ts
|
|
606
|
+
type MediaR2Bucket = object;
|
|
607
|
+
```
|
|
608
|
+
|
|
609
|
+
The R2 bucket operations a Media destination needs; a native `R2Bucket`
|
|
610
|
+
satisfies it.
|
|
611
|
+
|
|
612
|
+
#### Methods
|
|
613
|
+
|
|
614
|
+
##### get()
|
|
615
|
+
|
|
616
|
+
```ts
|
|
617
|
+
get(key): Promise<
|
|
618
|
+
| Readonly<{
|
|
619
|
+
size: number;
|
|
620
|
+
}> & object
|
|
621
|
+
| null>;
|
|
622
|
+
```
|
|
623
|
+
|
|
624
|
+
Read one object, or null when the key is absent.
|
|
625
|
+
|
|
626
|
+
###### Parameters
|
|
627
|
+
|
|
628
|
+
| Parameter | Type |
|
|
629
|
+
| --------- | -------- |
|
|
630
|
+
| `key` | `string` |
|
|
631
|
+
|
|
632
|
+
###### Returns
|
|
633
|
+
|
|
634
|
+
`Promise`\< \| `Readonly`\<\{ `size`: `number`; \}\> & `object` \| `null`\>
|
|
635
|
+
|
|
636
|
+
##### put()
|
|
637
|
+
|
|
638
|
+
```ts
|
|
639
|
+
put(
|
|
640
|
+
key,
|
|
641
|
+
bytes,
|
|
642
|
+
options
|
|
643
|
+
): Promise<object | null>;
|
|
644
|
+
```
|
|
645
|
+
|
|
646
|
+
Store exact bytes with their MIME type.
|
|
647
|
+
|
|
648
|
+
###### Parameters
|
|
649
|
+
|
|
650
|
+
| Parameter | Type |
|
|
651
|
+
| --------- | --------------------------------------------------------------------------------- |
|
|
652
|
+
| `key` | `string` |
|
|
653
|
+
| `bytes` | `Uint8Array` |
|
|
654
|
+
| `options` | `Readonly`\<\{ `httpMetadata`: `Readonly`\<\{ `contentType`: `string`; \}\>; \}\> |
|
|
655
|
+
|
|
656
|
+
###### Returns
|
|
657
|
+
|
|
658
|
+
`Promise`\<`object` \| `null`\>
|
|
659
|
+
|
|
660
|
+
---
|
|
661
|
+
|
|
662
|
+
### R2MediaDestinationOptions
|
|
663
|
+
|
|
664
|
+
```ts
|
|
665
|
+
type R2MediaDestinationOptions = Readonly<{
|
|
666
|
+
bucket: MediaR2Bucket;
|
|
667
|
+
destinationId: string;
|
|
668
|
+
fit: ReferenceImageFitter;
|
|
669
|
+
authorize?: (key) => Promise<void> | void;
|
|
670
|
+
}>;
|
|
671
|
+
```
|
|
672
|
+
|
|
673
|
+
Host-owned R2 destination configuration.
|
|
674
|
+
|
|
675
|
+
---
|
|
676
|
+
|
|
677
|
+
### R2MediaDestination
|
|
678
|
+
|
|
679
|
+
```ts
|
|
680
|
+
type R2MediaDestination = MediaServiceHost["destination"] &
|
|
681
|
+
Readonly<{
|
|
682
|
+
readReference: (reference, model) => Promise<MediaReferenceBytes>;
|
|
683
|
+
readAudioReference: (reference) => Promise<Uint8Array>;
|
|
684
|
+
}>;
|
|
685
|
+
```
|
|
686
|
+
|
|
687
|
+
The host destination over an R2 bucket, with both reference readers present.
|
|
688
|
+
|
|
689
|
+
---
|
|
690
|
+
|
|
603
691
|
### MediaImagePort
|
|
604
692
|
|
|
605
693
|
```ts
|
|
@@ -708,6 +796,27 @@ AI Gateway route.
|
|
|
708
796
|
|
|
709
797
|
---
|
|
710
798
|
|
|
799
|
+
### createR2MediaDestination()
|
|
800
|
+
|
|
801
|
+
```ts
|
|
802
|
+
function createR2MediaDestination(options): R2MediaDestination;
|
|
803
|
+
```
|
|
804
|
+
|
|
805
|
+
A Media destination over one R2 bucket: object keys are the paths, validated
|
|
806
|
+
with Darkroom's path schema. Never accepts URLs.
|
|
807
|
+
|
|
808
|
+
#### Parameters
|
|
809
|
+
|
|
810
|
+
| Parameter | Type |
|
|
811
|
+
| --------- | --------------------------------------------------------- |
|
|
812
|
+
| `options` | [`R2MediaDestinationOptions`](#r2mediadestinationoptions) |
|
|
813
|
+
|
|
814
|
+
#### Returns
|
|
815
|
+
|
|
816
|
+
[`R2MediaDestination`](#r2mediadestination)
|
|
817
|
+
|
|
818
|
+
---
|
|
819
|
+
|
|
711
820
|
### createImagesReferenceFitter()
|
|
712
821
|
|
|
713
822
|
```ts
|