@fungi.computer/darkroom 0.9.2 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/README.md +130 -6
  2. package/dist/app-media-contracts.d.ts +104 -0
  3. package/dist/browser.d.ts +1 -0
  4. package/dist/browser.js +1 -0
  5. package/dist/cloudflare/provider.d.ts +5 -4
  6. package/dist/cloudflare/provider.js +20 -9
  7. package/dist/cloudflare/r2-destination.d.ts +50 -0
  8. package/dist/cloudflare/r2-destination.js +59 -0
  9. package/dist/cloudflare.d.ts +3 -2
  10. package/dist/cloudflare.js +1 -0
  11. package/dist/gateway-models.d.ts +44 -9
  12. package/dist/gateway-models.js +52 -20
  13. package/dist/index.d.ts +12 -2
  14. package/dist/index.js +81 -38
  15. package/dist/media-binding.d.ts +6 -0
  16. package/dist/media-content-type.d.ts +5 -0
  17. package/dist/media-content-type.js +49 -0
  18. package/dist/media-generation.js +2 -0
  19. package/dist/media-operation-contract.d.ts +13 -1
  20. package/dist/media-operation-do.d.ts +6 -0
  21. package/dist/media-operation-do.js +37 -2
  22. package/dist/media-operation-owner.d.ts +93 -5
  23. package/dist/media-operation-owner.js +250 -46
  24. package/dist/media-operation-storage.d.ts +18 -4
  25. package/dist/media-operation.js +30 -4
  26. package/dist/media-path.d.ts +3 -0
  27. package/dist/media-path.js +7 -0
  28. package/dist/media-run-lifetime.d.ts +5 -0
  29. package/dist/media-run-lifetime.js +5 -0
  30. package/dist/media-written.d.ts +22 -0
  31. package/dist/media-written.js +20 -0
  32. package/dist/model-catalog.d.ts +226 -0
  33. package/dist/model-catalog.js +254 -9
  34. package/dist/provenance.d.ts +5 -1
  35. package/dist/provenance.js +2 -1
  36. package/dist/reference-read.d.ts +24 -0
  37. package/dist/reference-read.js +26 -0
  38. package/dist/server.d.ts +58 -2
  39. package/dist/server.js +49 -5
  40. package/docs/api/app-media-contracts.md +49 -0
  41. package/docs/api/browser.md +15 -4
  42. package/docs/api/cloudflare.md +254 -5
  43. package/docs/api/index.md +346 -62
  44. package/docs/api/server.md +524 -66
  45. package/package.json +2 -2
@@ -0,0 +1,22 @@
1
+ /** What a destination did when asked to delete a file this operation wrote. */
2
+ export type MediaRemoval =
3
+ /** The file matched what the operation wrote and was deleted. */
4
+ "removed"
5
+ /** Nothing was at the path. */
6
+ | "absent"
7
+ /** The path holds different bytes now; it is not this operation's file and was left alone. */
8
+ | "not-ours"
9
+ /** The destination cannot delete files; the host must remove the matching file itself. */
10
+ | "kept";
11
+ /** Lowercase hex SHA-256 of the exact bytes written to a destination. */
12
+ export declare function mediaWrittenDigest(bytes: Uint8Array): Promise<string>;
13
+ /**
14
+ * Delete the file at a destination path only while it still holds the exact
15
+ * bytes the operation wrote, so a path reused for something else is never lost.
16
+ */
17
+ export declare function removeMediaIfWritten(file: Readonly<{
18
+ /** Read the current bytes at the path, or null when absent. */
19
+ read: () => Promise<Uint8Array | null>;
20
+ /** Delete the file at the path. */
21
+ delete: () => Promise<void>;
22
+ }>, sha256: string): Promise<MediaRemoval>;
@@ -0,0 +1,20 @@
1
+ /** Lowercase hex SHA-256 of the exact bytes written to a destination. */
2
+ export async function mediaWrittenDigest(bytes) {
3
+ const digest = await crypto.subtle.digest("SHA-256", bytes.slice());
4
+ return [...new Uint8Array(digest)]
5
+ .map((byte) => byte.toString(16).padStart(2, "0"))
6
+ .join("");
7
+ }
8
+ /**
9
+ * Delete the file at a destination path only while it still holds the exact
10
+ * bytes the operation wrote, so a path reused for something else is never lost.
11
+ */
12
+ export async function removeMediaIfWritten(file, sha256) {
13
+ const current = await file.read();
14
+ if (current === null)
15
+ return "absent";
16
+ if ((await mediaWrittenDigest(current)) !== sha256)
17
+ return "not-ours";
18
+ await file.delete();
19
+ return "removed";
20
+ }
@@ -53,6 +53,11 @@ export declare const mediaModelInputSchema: z.ZodLazy<z.ZodReadonly<z.ZodObject<
53
53
  }>;
54
54
  maxSteps: z.ZodNullable<z.ZodNumber>;
55
55
  defaultSteps: z.ZodNullable<z.ZodNumber>;
56
+ guidance: z.ZodDefault<z.ZodNullable<z.ZodReadonly<z.ZodObject<{
57
+ min: z.ZodNumber;
58
+ max: z.ZodNumber;
59
+ default: z.ZodNumber;
60
+ }, z.core.$strict>>>>;
56
61
  referenceField: z.ZodNullable<z.ZodString>;
57
62
  maxReferences: z.ZodNumber;
58
63
  referenceDimension: z.ZodNullable<z.ZodNumber>;
@@ -342,6 +347,75 @@ export declare const AUDIO_CHARACTER_BLOCK = 100;
342
347
  * `maxPromptLength` is the real bound; no model may exceed this ceiling.
343
348
  */
344
349
  export declare const MAX_MEDIA_PROMPT_LENGTH = 32000;
350
+ /**
351
+ * What a source-image operation does to its one reference image. Both write a
352
+ * new image and never touch the source.
353
+ */
354
+ export declare const mediaTransformOperationSchema: z.ZodEnum<{
355
+ /** Cut the foreground out onto a transparent PNG. */
356
+ readonly remove_background: "remove_background";
357
+ /** Enlarge the image to a chosen output size in megapixels. */
358
+ readonly upscale: "upscale";
359
+ }>;
360
+ /** A source-image operation. */
361
+ export type MediaTransformOperation = z.output<typeof mediaTransformOperationSchema>;
362
+ /**
363
+ * Facts of a catalog entry that edits one source image rather than generating
364
+ * from a prompt. The source limits are Botanical's own conservative bounds, not
365
+ * Cloudflare's: neither model page publishes an input size limit.
366
+ */
367
+ export declare const mediaTransformSchema: z.ZodLazy<z.ZodReadonly<z.ZodObject<{
368
+ operation: z.ZodEnum<{
369
+ /** Cut the foreground out onto a transparent PNG. */
370
+ readonly remove_background: "remove_background";
371
+ /** Enlarge the image to a chosen output size in megapixels. */
372
+ readonly upscale: "upscale";
373
+ }>;
374
+ source: z.ZodReadonly<z.ZodObject<{
375
+ formats: z.ZodReadonly<z.ZodArray<z.ZodEnum<{
376
+ /** PNG image encoding. */
377
+ readonly png: "png";
378
+ /** JPEG image encoding. */
379
+ readonly jpeg: "jpeg";
380
+ /** WebP image encoding. */
381
+ readonly webp: "webp";
382
+ }>>>;
383
+ maxBytes: z.ZodNumber;
384
+ maxPixels: z.ZodNumber;
385
+ }, z.core.$strict>>;
386
+ defaultMegapixels: z.ZodNullable<z.ZodNumber>;
387
+ milliunits: z.ZodNullable<z.ZodNumber>;
388
+ sizes: z.ZodReadonly<z.ZodArray<z.ZodReadonly<z.ZodObject<{
389
+ megapixels: z.ZodNumber;
390
+ milliunits: z.ZodNumber;
391
+ }, z.core.$strict>>>>;
392
+ }, z.core.$strict>>>;
393
+ /** Immutable source-image operation facts. */
394
+ export type MediaTransform = z.output<typeof mediaTransformSchema>;
395
+ /**
396
+ * What one generation bills: whole images, whole seconds of video or music, or
397
+ * blocks of {@link AUDIO_CHARACTER_BLOCK} speech characters (so a per-character
398
+ * provider rate is marked up once per block, not rounded up per character).
399
+ */
400
+ export declare const mediaBillingUnitSchema: z.ZodEnum<{
401
+ /** One image. */
402
+ readonly image: "image";
403
+ /** One whole second of video. */
404
+ readonly video_second: "video_second";
405
+ /** One whole second of music. */
406
+ readonly audio_second: "audio_second";
407
+ /** One block of speech characters. */
408
+ readonly audio_character_block: "audio_character_block";
409
+ }>;
410
+ /** The unit a model's generations are billed in. */
411
+ export type MediaBillingUnit = z.output<typeof mediaBillingUnitSchema>;
412
+ /** The one rule mapping a model's kind (and speech or music mode) to its billing unit. */
413
+ export declare const mediaBillingUnit: (model: Readonly<{
414
+ kind: "image" | "video" | "audio";
415
+ audio: Readonly<{
416
+ mode: "speech" | "music";
417
+ }> | null;
418
+ }>) => MediaBillingUnit;
345
419
  /** Public wire grammar for a catalog entry returned by the Media owner. */
346
420
  export declare const mediaModelSchema: z.ZodReadonly<z.ZodObject<{
347
421
  id: z.ZodString;
@@ -400,6 +474,11 @@ export declare const mediaModelSchema: z.ZodReadonly<z.ZodObject<{
400
474
  }>;
401
475
  maxSteps: z.ZodNullable<z.ZodNumber>;
402
476
  defaultSteps: z.ZodNullable<z.ZodNumber>;
477
+ guidance: z.ZodDefault<z.ZodNullable<z.ZodReadonly<z.ZodObject<{
478
+ min: z.ZodNumber;
479
+ max: z.ZodNumber;
480
+ default: z.ZodNumber;
481
+ }, z.core.$strict>>>>;
403
482
  referenceField: z.ZodNullable<z.ZodString>;
404
483
  maxReferences: z.ZodNumber;
405
484
  referenceDimension: z.ZodNullable<z.ZodNumber>;
@@ -548,6 +627,42 @@ export declare const mediaModelSchema: z.ZodReadonly<z.ZodObject<{
548
627
  }, z.core.$strict>>>;
549
628
  milliunitsPerUnit: z.ZodNumber;
550
629
  }, z.core.$strict>>>>;
630
+ billingUnit: z.ZodEnum<{
631
+ /** One image. */
632
+ readonly image: "image";
633
+ /** One whole second of video. */
634
+ readonly video_second: "video_second";
635
+ /** One whole second of music. */
636
+ readonly audio_second: "audio_second";
637
+ /** One block of speech characters. */
638
+ readonly audio_character_block: "audio_character_block";
639
+ }>;
640
+ transform: z.ZodNullable<z.ZodLazy<z.ZodReadonly<z.ZodObject<{
641
+ operation: z.ZodEnum<{
642
+ /** Cut the foreground out onto a transparent PNG. */
643
+ readonly remove_background: "remove_background";
644
+ /** Enlarge the image to a chosen output size in megapixels. */
645
+ readonly upscale: "upscale";
646
+ }>;
647
+ source: z.ZodReadonly<z.ZodObject<{
648
+ formats: z.ZodReadonly<z.ZodArray<z.ZodEnum<{
649
+ /** PNG image encoding. */
650
+ readonly png: "png";
651
+ /** JPEG image encoding. */
652
+ readonly jpeg: "jpeg";
653
+ /** WebP image encoding. */
654
+ readonly webp: "webp";
655
+ }>>>;
656
+ maxBytes: z.ZodNumber;
657
+ maxPixels: z.ZodNumber;
658
+ }, z.core.$strict>>;
659
+ defaultMegapixels: z.ZodNullable<z.ZodNumber>;
660
+ milliunits: z.ZodNullable<z.ZodNumber>;
661
+ sizes: z.ZodReadonly<z.ZodArray<z.ZodReadonly<z.ZodObject<{
662
+ megapixels: z.ZodNumber;
663
+ milliunits: z.ZodNumber;
664
+ }, z.core.$strict>>>>;
665
+ }, z.core.$strict>>>>;
551
666
  }, z.core.$strict>>;
552
667
  /** Model-specific dimensions, steps, transport and reference limits. */
553
668
  export type MediaModelInput = z.output<typeof mediaModelInputSchema>;
@@ -617,6 +732,11 @@ export declare const mediaCatalogueModelSchema: z.ZodReadonly<z.ZodObject<{
617
732
  }>;
618
733
  maxSteps: z.ZodNullable<z.ZodNumber>;
619
734
  defaultSteps: z.ZodNullable<z.ZodNumber>;
735
+ guidance: z.ZodDefault<z.ZodNullable<z.ZodReadonly<z.ZodObject<{
736
+ min: z.ZodNumber;
737
+ max: z.ZodNumber;
738
+ default: z.ZodNumber;
739
+ }, z.core.$strict>>>>;
620
740
  referenceField: z.ZodNullable<z.ZodString>;
621
741
  maxReferences: z.ZodNumber;
622
742
  referenceDimension: z.ZodNullable<z.ZodNumber>;
@@ -628,6 +748,16 @@ export declare const mediaCatalogueModelSchema: z.ZodReadonly<z.ZodObject<{
628
748
  }>;
629
749
  }, z.core.$strict>>>;
630
750
  sizes: z.ZodNullable<z.ZodReadonly<z.ZodArray<z.ZodString>>>;
751
+ billingUnit: z.ZodOptional<z.ZodEnum<{
752
+ /** One image. */
753
+ readonly image: "image";
754
+ /** One whole second of video. */
755
+ readonly video_second: "video_second";
756
+ /** One whole second of music. */
757
+ readonly audio_second: "audio_second";
758
+ /** One block of speech characters. */
759
+ readonly audio_character_block: "audio_character_block";
760
+ }>>;
631
761
  audio: z.ZodDefault<z.ZodNullable<z.ZodReadonly<z.ZodObject<{
632
762
  provider: z.ZodLiteral<"elevenlabs">;
633
763
  mode: z.ZodEnum<{
@@ -648,6 +778,32 @@ export declare const mediaCatalogueModelSchema: z.ZodReadonly<z.ZodObject<{
648
778
  }, z.core.$strict>>>;
649
779
  milliunitsPerUnit: z.ZodNumber;
650
780
  }, z.core.$strict>>>>;
781
+ transform: z.ZodDefault<z.ZodNullable<z.ZodReadonly<z.ZodObject<{
782
+ operation: z.ZodEnum<{
783
+ /** Cut the foreground out onto a transparent PNG. */
784
+ readonly remove_background: "remove_background";
785
+ /** Enlarge the image to a chosen output size in megapixels. */
786
+ readonly upscale: "upscale";
787
+ }>;
788
+ source: z.ZodReadonly<z.ZodObject<{
789
+ formats: z.ZodReadonly<z.ZodArray<z.ZodEnum<{
790
+ /** PNG image encoding. */
791
+ readonly png: "png";
792
+ /** JPEG image encoding. */
793
+ readonly jpeg: "jpeg";
794
+ /** WebP image encoding. */
795
+ readonly webp: "webp";
796
+ }>>>;
797
+ maxBytes: z.ZodNumber;
798
+ maxPixels: z.ZodNumber;
799
+ }, z.core.$strict>>;
800
+ defaultMegapixels: z.ZodNullable<z.ZodNumber>;
801
+ milliunits: z.ZodNullable<z.ZodNumber>;
802
+ sizes: z.ZodReadonly<z.ZodArray<z.ZodReadonly<z.ZodObject<{
803
+ megapixels: z.ZodNumber;
804
+ milliunits: z.ZodNumber;
805
+ }, z.core.$strict>>>>;
806
+ }, z.core.$strict>>>>;
651
807
  video: z.ZodNullable<z.ZodReadonly<z.ZodObject<{
652
808
  minSeconds: z.ZodNumber;
653
809
  maxSeconds: z.ZodNumber;
@@ -835,6 +991,11 @@ export declare const mediaCatalogueSchema: z.ZodPipe<z.ZodArray<z.ZodReadonly<z.
835
991
  }>;
836
992
  maxSteps: z.ZodNullable<z.ZodNumber>;
837
993
  defaultSteps: z.ZodNullable<z.ZodNumber>;
994
+ guidance: z.ZodDefault<z.ZodNullable<z.ZodReadonly<z.ZodObject<{
995
+ min: z.ZodNumber;
996
+ max: z.ZodNumber;
997
+ default: z.ZodNumber;
998
+ }, z.core.$strict>>>>;
838
999
  referenceField: z.ZodNullable<z.ZodString>;
839
1000
  maxReferences: z.ZodNumber;
840
1001
  referenceDimension: z.ZodNullable<z.ZodNumber>;
@@ -846,6 +1007,16 @@ export declare const mediaCatalogueSchema: z.ZodPipe<z.ZodArray<z.ZodReadonly<z.
846
1007
  }>;
847
1008
  }, z.core.$strict>>>;
848
1009
  sizes: z.ZodNullable<z.ZodReadonly<z.ZodArray<z.ZodString>>>;
1010
+ billingUnit: z.ZodOptional<z.ZodEnum<{
1011
+ /** One image. */
1012
+ readonly image: "image";
1013
+ /** One whole second of video. */
1014
+ readonly video_second: "video_second";
1015
+ /** One whole second of music. */
1016
+ readonly audio_second: "audio_second";
1017
+ /** One block of speech characters. */
1018
+ readonly audio_character_block: "audio_character_block";
1019
+ }>>;
849
1020
  audio: z.ZodDefault<z.ZodNullable<z.ZodReadonly<z.ZodObject<{
850
1021
  provider: z.ZodLiteral<"elevenlabs">;
851
1022
  mode: z.ZodEnum<{
@@ -866,6 +1037,32 @@ export declare const mediaCatalogueSchema: z.ZodPipe<z.ZodArray<z.ZodReadonly<z.
866
1037
  }, z.core.$strict>>>;
867
1038
  milliunitsPerUnit: z.ZodNumber;
868
1039
  }, z.core.$strict>>>>;
1040
+ transform: z.ZodDefault<z.ZodNullable<z.ZodReadonly<z.ZodObject<{
1041
+ operation: z.ZodEnum<{
1042
+ /** Cut the foreground out onto a transparent PNG. */
1043
+ readonly remove_background: "remove_background";
1044
+ /** Enlarge the image to a chosen output size in megapixels. */
1045
+ readonly upscale: "upscale";
1046
+ }>;
1047
+ source: z.ZodReadonly<z.ZodObject<{
1048
+ formats: z.ZodReadonly<z.ZodArray<z.ZodEnum<{
1049
+ /** PNG image encoding. */
1050
+ readonly png: "png";
1051
+ /** JPEG image encoding. */
1052
+ readonly jpeg: "jpeg";
1053
+ /** WebP image encoding. */
1054
+ readonly webp: "webp";
1055
+ }>>>;
1056
+ maxBytes: z.ZodNumber;
1057
+ maxPixels: z.ZodNumber;
1058
+ }, z.core.$strict>>;
1059
+ defaultMegapixels: z.ZodNullable<z.ZodNumber>;
1060
+ milliunits: z.ZodNullable<z.ZodNumber>;
1061
+ sizes: z.ZodReadonly<z.ZodArray<z.ZodReadonly<z.ZodObject<{
1062
+ megapixels: z.ZodNumber;
1063
+ milliunits: z.ZodNumber;
1064
+ }, z.core.$strict>>>>;
1065
+ }, z.core.$strict>>>>;
869
1066
  video: z.ZodNullable<z.ZodReadonly<z.ZodObject<{
870
1067
  minSeconds: z.ZodNumber;
871
1068
  maxSeconds: z.ZodNumber;
@@ -1002,6 +1199,11 @@ export declare const mediaCatalogueSchema: z.ZodPipe<z.ZodArray<z.ZodReadonly<z.
1002
1199
  steps: "steps" | "num_steps" | "fixed";
1003
1200
  maxSteps: number | null;
1004
1201
  defaultSteps: number | null;
1202
+ guidance: Readonly<{
1203
+ min: number;
1204
+ max: number;
1205
+ default: number;
1206
+ }> | null;
1005
1207
  referenceField: string | null;
1006
1208
  maxReferences: number;
1007
1209
  referenceDimension: number | null;
@@ -1023,6 +1225,20 @@ export declare const mediaCatalogueSchema: z.ZodPipe<z.ZodArray<z.ZodReadonly<z.
1023
1225
  }> | null;
1024
1226
  milliunitsPerUnit: number;
1025
1227
  }> | null;
1228
+ transform: Readonly<{
1229
+ operation: "remove_background" | "upscale";
1230
+ source: Readonly<{
1231
+ formats: readonly ("png" | "jpeg" | "webp")[];
1232
+ maxBytes: number;
1233
+ maxPixels: number;
1234
+ }>;
1235
+ defaultMegapixels: number | null;
1236
+ milliunits: number | null;
1237
+ sizes: readonly Readonly<{
1238
+ megapixels: number;
1239
+ milliunits: number;
1240
+ }>[];
1241
+ }> | null;
1026
1242
  video: Readonly<{
1027
1243
  minSeconds: number;
1028
1244
  maxSeconds: number;
@@ -1052,9 +1268,19 @@ export declare const mediaCatalogueSchema: z.ZodPipe<z.ZodArray<z.ZodReadonly<z.
1052
1268
  }> | null;
1053
1269
  provider: "seedance" | "veo" | "minimax" | "host";
1054
1270
  }> | null;
1271
+ billingUnit?: "image" | "video_second" | "audio_second" | "audio_character_block" | undefined;
1055
1272
  }>[]>>;
1056
1273
  /** Decode untrusted catalogue data, throwing a MediaSettingsError that names the first problem. */
1057
1274
  export declare const parseMediaCatalogue: (raw: unknown) => MediaCatalogue;
1275
+ /** A catalogue entry as the public `/models` list returns it: the entry plus its derived billing unit. */
1276
+ export declare const withBillingUnit: (model: MediaCatalogueModel) => MediaCatalogueModel & Readonly<{
1277
+ billingUnit: MediaBillingUnit;
1278
+ }>;
1279
+ /**
1280
+ * Provider milliunits for one image of a source-image operation at `megapixels`
1281
+ * (omitted: the model's default), or undefined when that output size is not offered.
1282
+ */
1283
+ export declare const transformMilliunits: (transform: MediaTransform, megapixels: number | undefined) => number | undefined;
1058
1284
  /** List the gateway provider's models in catalog order. */
1059
1285
  export declare const listMediaModels: () => readonly MediaModel[];
1060
1286
  /** Find a model in the Fungi gateway's own catalogue; gateway-internal code never sees a custom provider's models. */