@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/model-catalog.d.ts
CHANGED
|
@@ -342,6 +342,75 @@ export declare const AUDIO_CHARACTER_BLOCK = 100;
|
|
|
342
342
|
* `maxPromptLength` is the real bound; no model may exceed this ceiling.
|
|
343
343
|
*/
|
|
344
344
|
export declare const MAX_MEDIA_PROMPT_LENGTH = 32000;
|
|
345
|
+
/**
|
|
346
|
+
* What a source-image operation does to its one reference image. Both write a
|
|
347
|
+
* new image and never touch the source.
|
|
348
|
+
*/
|
|
349
|
+
export declare const mediaTransformOperationSchema: z.ZodEnum<{
|
|
350
|
+
/** Cut the foreground out onto a transparent PNG. */
|
|
351
|
+
readonly remove_background: "remove_background";
|
|
352
|
+
/** Enlarge the image to a chosen output size in megapixels. */
|
|
353
|
+
readonly upscale: "upscale";
|
|
354
|
+
}>;
|
|
355
|
+
/** A source-image operation. */
|
|
356
|
+
export type MediaTransformOperation = z.output<typeof mediaTransformOperationSchema>;
|
|
357
|
+
/**
|
|
358
|
+
* Facts of a catalog entry that edits one source image rather than generating
|
|
359
|
+
* from a prompt. The source limits are Botanical's own conservative bounds, not
|
|
360
|
+
* Cloudflare's: neither model page publishes an input size limit.
|
|
361
|
+
*/
|
|
362
|
+
export declare const mediaTransformSchema: z.ZodLazy<z.ZodReadonly<z.ZodObject<{
|
|
363
|
+
operation: z.ZodEnum<{
|
|
364
|
+
/** Cut the foreground out onto a transparent PNG. */
|
|
365
|
+
readonly remove_background: "remove_background";
|
|
366
|
+
/** Enlarge the image to a chosen output size in megapixels. */
|
|
367
|
+
readonly upscale: "upscale";
|
|
368
|
+
}>;
|
|
369
|
+
source: z.ZodReadonly<z.ZodObject<{
|
|
370
|
+
formats: z.ZodReadonly<z.ZodArray<z.ZodEnum<{
|
|
371
|
+
/** PNG image encoding. */
|
|
372
|
+
readonly png: "png";
|
|
373
|
+
/** JPEG image encoding. */
|
|
374
|
+
readonly jpeg: "jpeg";
|
|
375
|
+
/** WebP image encoding. */
|
|
376
|
+
readonly webp: "webp";
|
|
377
|
+
}>>>;
|
|
378
|
+
maxBytes: z.ZodNumber;
|
|
379
|
+
maxPixels: z.ZodNumber;
|
|
380
|
+
}, z.core.$strict>>;
|
|
381
|
+
defaultMegapixels: z.ZodNullable<z.ZodNumber>;
|
|
382
|
+
milliunits: z.ZodNullable<z.ZodNumber>;
|
|
383
|
+
sizes: z.ZodReadonly<z.ZodArray<z.ZodReadonly<z.ZodObject<{
|
|
384
|
+
megapixels: z.ZodNumber;
|
|
385
|
+
milliunits: z.ZodNumber;
|
|
386
|
+
}, z.core.$strict>>>>;
|
|
387
|
+
}, z.core.$strict>>>;
|
|
388
|
+
/** Immutable source-image operation facts. */
|
|
389
|
+
export type MediaTransform = z.output<typeof mediaTransformSchema>;
|
|
390
|
+
/**
|
|
391
|
+
* What one generation bills: whole images, whole seconds of video or music, or
|
|
392
|
+
* blocks of {@link AUDIO_CHARACTER_BLOCK} speech characters (so a per-character
|
|
393
|
+
* provider rate is marked up once per block, not rounded up per character).
|
|
394
|
+
*/
|
|
395
|
+
export declare const mediaBillingUnitSchema: z.ZodEnum<{
|
|
396
|
+
/** One image. */
|
|
397
|
+
readonly image: "image";
|
|
398
|
+
/** One whole second of video. */
|
|
399
|
+
readonly video_second: "video_second";
|
|
400
|
+
/** One whole second of music. */
|
|
401
|
+
readonly audio_second: "audio_second";
|
|
402
|
+
/** One block of speech characters. */
|
|
403
|
+
readonly audio_character_block: "audio_character_block";
|
|
404
|
+
}>;
|
|
405
|
+
/** The unit a model's generations are billed in. */
|
|
406
|
+
export type MediaBillingUnit = z.output<typeof mediaBillingUnitSchema>;
|
|
407
|
+
/** The one rule mapping a model's kind (and speech or music mode) to its billing unit. */
|
|
408
|
+
export declare const mediaBillingUnit: (model: Readonly<{
|
|
409
|
+
kind: "image" | "video" | "audio";
|
|
410
|
+
audio: Readonly<{
|
|
411
|
+
mode: "speech" | "music";
|
|
412
|
+
}> | null;
|
|
413
|
+
}>) => MediaBillingUnit;
|
|
345
414
|
/** Public wire grammar for a catalog entry returned by the Media owner. */
|
|
346
415
|
export declare const mediaModelSchema: z.ZodReadonly<z.ZodObject<{
|
|
347
416
|
id: z.ZodString;
|
|
@@ -548,6 +617,42 @@ export declare const mediaModelSchema: z.ZodReadonly<z.ZodObject<{
|
|
|
548
617
|
}, z.core.$strict>>>;
|
|
549
618
|
milliunitsPerUnit: z.ZodNumber;
|
|
550
619
|
}, z.core.$strict>>>>;
|
|
620
|
+
billingUnit: z.ZodEnum<{
|
|
621
|
+
/** One image. */
|
|
622
|
+
readonly image: "image";
|
|
623
|
+
/** One whole second of video. */
|
|
624
|
+
readonly video_second: "video_second";
|
|
625
|
+
/** One whole second of music. */
|
|
626
|
+
readonly audio_second: "audio_second";
|
|
627
|
+
/** One block of speech characters. */
|
|
628
|
+
readonly audio_character_block: "audio_character_block";
|
|
629
|
+
}>;
|
|
630
|
+
transform: z.ZodNullable<z.ZodLazy<z.ZodReadonly<z.ZodObject<{
|
|
631
|
+
operation: z.ZodEnum<{
|
|
632
|
+
/** Cut the foreground out onto a transparent PNG. */
|
|
633
|
+
readonly remove_background: "remove_background";
|
|
634
|
+
/** Enlarge the image to a chosen output size in megapixels. */
|
|
635
|
+
readonly upscale: "upscale";
|
|
636
|
+
}>;
|
|
637
|
+
source: z.ZodReadonly<z.ZodObject<{
|
|
638
|
+
formats: z.ZodReadonly<z.ZodArray<z.ZodEnum<{
|
|
639
|
+
/** PNG image encoding. */
|
|
640
|
+
readonly png: "png";
|
|
641
|
+
/** JPEG image encoding. */
|
|
642
|
+
readonly jpeg: "jpeg";
|
|
643
|
+
/** WebP image encoding. */
|
|
644
|
+
readonly webp: "webp";
|
|
645
|
+
}>>>;
|
|
646
|
+
maxBytes: z.ZodNumber;
|
|
647
|
+
maxPixels: z.ZodNumber;
|
|
648
|
+
}, z.core.$strict>>;
|
|
649
|
+
defaultMegapixels: z.ZodNullable<z.ZodNumber>;
|
|
650
|
+
milliunits: z.ZodNullable<z.ZodNumber>;
|
|
651
|
+
sizes: z.ZodReadonly<z.ZodArray<z.ZodReadonly<z.ZodObject<{
|
|
652
|
+
megapixels: z.ZodNumber;
|
|
653
|
+
milliunits: z.ZodNumber;
|
|
654
|
+
}, z.core.$strict>>>>;
|
|
655
|
+
}, z.core.$strict>>>>;
|
|
551
656
|
}, z.core.$strict>>;
|
|
552
657
|
/** Model-specific dimensions, steps, transport and reference limits. */
|
|
553
658
|
export type MediaModelInput = z.output<typeof mediaModelInputSchema>;
|
|
@@ -628,6 +733,16 @@ export declare const mediaCatalogueModelSchema: z.ZodReadonly<z.ZodObject<{
|
|
|
628
733
|
}>;
|
|
629
734
|
}, z.core.$strict>>>;
|
|
630
735
|
sizes: z.ZodNullable<z.ZodReadonly<z.ZodArray<z.ZodString>>>;
|
|
736
|
+
billingUnit: z.ZodOptional<z.ZodEnum<{
|
|
737
|
+
/** One image. */
|
|
738
|
+
readonly image: "image";
|
|
739
|
+
/** One whole second of video. */
|
|
740
|
+
readonly video_second: "video_second";
|
|
741
|
+
/** One whole second of music. */
|
|
742
|
+
readonly audio_second: "audio_second";
|
|
743
|
+
/** One block of speech characters. */
|
|
744
|
+
readonly audio_character_block: "audio_character_block";
|
|
745
|
+
}>>;
|
|
631
746
|
audio: z.ZodDefault<z.ZodNullable<z.ZodReadonly<z.ZodObject<{
|
|
632
747
|
provider: z.ZodLiteral<"elevenlabs">;
|
|
633
748
|
mode: z.ZodEnum<{
|
|
@@ -648,6 +763,32 @@ export declare const mediaCatalogueModelSchema: z.ZodReadonly<z.ZodObject<{
|
|
|
648
763
|
}, z.core.$strict>>>;
|
|
649
764
|
milliunitsPerUnit: z.ZodNumber;
|
|
650
765
|
}, z.core.$strict>>>>;
|
|
766
|
+
transform: z.ZodDefault<z.ZodNullable<z.ZodReadonly<z.ZodObject<{
|
|
767
|
+
operation: z.ZodEnum<{
|
|
768
|
+
/** Cut the foreground out onto a transparent PNG. */
|
|
769
|
+
readonly remove_background: "remove_background";
|
|
770
|
+
/** Enlarge the image to a chosen output size in megapixels. */
|
|
771
|
+
readonly upscale: "upscale";
|
|
772
|
+
}>;
|
|
773
|
+
source: z.ZodReadonly<z.ZodObject<{
|
|
774
|
+
formats: z.ZodReadonly<z.ZodArray<z.ZodEnum<{
|
|
775
|
+
/** PNG image encoding. */
|
|
776
|
+
readonly png: "png";
|
|
777
|
+
/** JPEG image encoding. */
|
|
778
|
+
readonly jpeg: "jpeg";
|
|
779
|
+
/** WebP image encoding. */
|
|
780
|
+
readonly webp: "webp";
|
|
781
|
+
}>>>;
|
|
782
|
+
maxBytes: z.ZodNumber;
|
|
783
|
+
maxPixels: z.ZodNumber;
|
|
784
|
+
}, z.core.$strict>>;
|
|
785
|
+
defaultMegapixels: z.ZodNullable<z.ZodNumber>;
|
|
786
|
+
milliunits: z.ZodNullable<z.ZodNumber>;
|
|
787
|
+
sizes: z.ZodReadonly<z.ZodArray<z.ZodReadonly<z.ZodObject<{
|
|
788
|
+
megapixels: z.ZodNumber;
|
|
789
|
+
milliunits: z.ZodNumber;
|
|
790
|
+
}, z.core.$strict>>>>;
|
|
791
|
+
}, z.core.$strict>>>>;
|
|
651
792
|
video: z.ZodNullable<z.ZodReadonly<z.ZodObject<{
|
|
652
793
|
minSeconds: z.ZodNumber;
|
|
653
794
|
maxSeconds: z.ZodNumber;
|
|
@@ -846,6 +987,16 @@ export declare const mediaCatalogueSchema: z.ZodPipe<z.ZodArray<z.ZodReadonly<z.
|
|
|
846
987
|
}>;
|
|
847
988
|
}, z.core.$strict>>>;
|
|
848
989
|
sizes: z.ZodNullable<z.ZodReadonly<z.ZodArray<z.ZodString>>>;
|
|
990
|
+
billingUnit: z.ZodOptional<z.ZodEnum<{
|
|
991
|
+
/** One image. */
|
|
992
|
+
readonly image: "image";
|
|
993
|
+
/** One whole second of video. */
|
|
994
|
+
readonly video_second: "video_second";
|
|
995
|
+
/** One whole second of music. */
|
|
996
|
+
readonly audio_second: "audio_second";
|
|
997
|
+
/** One block of speech characters. */
|
|
998
|
+
readonly audio_character_block: "audio_character_block";
|
|
999
|
+
}>>;
|
|
849
1000
|
audio: z.ZodDefault<z.ZodNullable<z.ZodReadonly<z.ZodObject<{
|
|
850
1001
|
provider: z.ZodLiteral<"elevenlabs">;
|
|
851
1002
|
mode: z.ZodEnum<{
|
|
@@ -866,6 +1017,32 @@ export declare const mediaCatalogueSchema: z.ZodPipe<z.ZodArray<z.ZodReadonly<z.
|
|
|
866
1017
|
}, z.core.$strict>>>;
|
|
867
1018
|
milliunitsPerUnit: z.ZodNumber;
|
|
868
1019
|
}, z.core.$strict>>>>;
|
|
1020
|
+
transform: z.ZodDefault<z.ZodNullable<z.ZodReadonly<z.ZodObject<{
|
|
1021
|
+
operation: z.ZodEnum<{
|
|
1022
|
+
/** Cut the foreground out onto a transparent PNG. */
|
|
1023
|
+
readonly remove_background: "remove_background";
|
|
1024
|
+
/** Enlarge the image to a chosen output size in megapixels. */
|
|
1025
|
+
readonly upscale: "upscale";
|
|
1026
|
+
}>;
|
|
1027
|
+
source: z.ZodReadonly<z.ZodObject<{
|
|
1028
|
+
formats: z.ZodReadonly<z.ZodArray<z.ZodEnum<{
|
|
1029
|
+
/** PNG image encoding. */
|
|
1030
|
+
readonly png: "png";
|
|
1031
|
+
/** JPEG image encoding. */
|
|
1032
|
+
readonly jpeg: "jpeg";
|
|
1033
|
+
/** WebP image encoding. */
|
|
1034
|
+
readonly webp: "webp";
|
|
1035
|
+
}>>>;
|
|
1036
|
+
maxBytes: z.ZodNumber;
|
|
1037
|
+
maxPixels: z.ZodNumber;
|
|
1038
|
+
}, z.core.$strict>>;
|
|
1039
|
+
defaultMegapixels: z.ZodNullable<z.ZodNumber>;
|
|
1040
|
+
milliunits: z.ZodNullable<z.ZodNumber>;
|
|
1041
|
+
sizes: z.ZodReadonly<z.ZodArray<z.ZodReadonly<z.ZodObject<{
|
|
1042
|
+
megapixels: z.ZodNumber;
|
|
1043
|
+
milliunits: z.ZodNumber;
|
|
1044
|
+
}, z.core.$strict>>>>;
|
|
1045
|
+
}, z.core.$strict>>>>;
|
|
869
1046
|
video: z.ZodNullable<z.ZodReadonly<z.ZodObject<{
|
|
870
1047
|
minSeconds: z.ZodNumber;
|
|
871
1048
|
maxSeconds: z.ZodNumber;
|
|
@@ -1023,6 +1200,20 @@ export declare const mediaCatalogueSchema: z.ZodPipe<z.ZodArray<z.ZodReadonly<z.
|
|
|
1023
1200
|
}> | null;
|
|
1024
1201
|
milliunitsPerUnit: number;
|
|
1025
1202
|
}> | null;
|
|
1203
|
+
transform: Readonly<{
|
|
1204
|
+
operation: "remove_background" | "upscale";
|
|
1205
|
+
source: Readonly<{
|
|
1206
|
+
formats: readonly ("png" | "jpeg" | "webp")[];
|
|
1207
|
+
maxBytes: number;
|
|
1208
|
+
maxPixels: number;
|
|
1209
|
+
}>;
|
|
1210
|
+
defaultMegapixels: number | null;
|
|
1211
|
+
milliunits: number | null;
|
|
1212
|
+
sizes: readonly Readonly<{
|
|
1213
|
+
megapixels: number;
|
|
1214
|
+
milliunits: number;
|
|
1215
|
+
}>[];
|
|
1216
|
+
}> | null;
|
|
1026
1217
|
video: Readonly<{
|
|
1027
1218
|
minSeconds: number;
|
|
1028
1219
|
maxSeconds: number;
|
|
@@ -1052,9 +1243,19 @@ export declare const mediaCatalogueSchema: z.ZodPipe<z.ZodArray<z.ZodReadonly<z.
|
|
|
1052
1243
|
}> | null;
|
|
1053
1244
|
provider: "seedance" | "veo" | "minimax" | "host";
|
|
1054
1245
|
}> | null;
|
|
1246
|
+
billingUnit?: "image" | "video_second" | "audio_second" | "audio_character_block" | undefined;
|
|
1055
1247
|
}>[]>>;
|
|
1056
1248
|
/** Decode untrusted catalogue data, throwing a MediaSettingsError that names the first problem. */
|
|
1057
1249
|
export declare const parseMediaCatalogue: (raw: unknown) => MediaCatalogue;
|
|
1250
|
+
/** A catalogue entry as the public `/models` list returns it: the entry plus its derived billing unit. */
|
|
1251
|
+
export declare const withBillingUnit: (model: MediaCatalogueModel) => MediaCatalogueModel & Readonly<{
|
|
1252
|
+
billingUnit: MediaBillingUnit;
|
|
1253
|
+
}>;
|
|
1254
|
+
/**
|
|
1255
|
+
* Provider milliunits for one image of a source-image operation at `megapixels`
|
|
1256
|
+
* (omitted: the model's default), or undefined when that output size is not offered.
|
|
1257
|
+
*/
|
|
1258
|
+
export declare const transformMilliunits: (transform: MediaTransform, megapixels: number | undefined) => number | undefined;
|
|
1058
1259
|
/** List the gateway provider's models in catalog order. */
|
|
1059
1260
|
export declare const listMediaModels: () => readonly MediaModel[];
|
|
1060
1261
|
/** Find a model in the Fungi gateway's own catalogue; gateway-internal code never sees a custom provider's models. */
|
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
|