@fungi.computer/darkroom 0.11.11 → 0.11.13
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 +13 -0
- package/examples/standalone/README.md +52 -0
- package/examples/standalone/worker.ts +87 -0
- package/examples/standalone/wrangler.jsonc +26 -0
- package/package.json +8 -2
- package/src/app-media-contracts.ts +142 -0
- package/src/audio-providers/elevenlabs.ts +108 -0
- package/src/audio-reference-types.ts +47 -0
- package/src/audio-references.ts +107 -0
- package/src/browser-failure.ts +73 -0
- package/src/browser.ts +7 -0
- package/src/cloudflare/provider-reason.ts +89 -0
- package/src/cloudflare/provider.ts +405 -0
- package/src/cloudflare/r2-destination.ts +130 -0
- package/src/cloudflare/reference-images.ts +193 -0
- package/src/cloudflare.ts +38 -0
- package/src/cost-estimate.ts +129 -0
- package/src/daily-media-meter.ts +163 -0
- package/src/effect.ts +2 -0
- package/src/execution-error.ts +98 -0
- package/src/failure.ts +13 -0
- package/src/gateway-models.ts +304 -0
- package/src/gateway-wire.ts +36 -0
- package/src/index.ts +1454 -0
- package/src/media-binding.ts +260 -0
- package/src/media-content-type.ts +60 -0
- package/src/media-generation.ts +171 -0
- package/src/media-operation-contract.ts +173 -0
- package/src/media-operation-do.ts +257 -0
- package/src/media-operation-owner.ts +1786 -0
- package/src/media-operation-storage.ts +110 -0
- package/src/media-operation.ts +454 -0
- package/src/media-path.ts +8 -0
- package/src/media-quote.ts +34 -0
- package/src/media-run-lifetime.ts +5 -0
- package/src/media-written.ts +38 -0
- package/src/model-catalog.ts +1574 -0
- package/src/neurons.ts +6 -0
- package/src/node-webpmux.d.ts +12 -0
- package/src/provenance-bytes.ts +22 -0
- package/src/provenance-export.ts +126 -0
- package/src/provenance-jpeg.ts +83 -0
- package/src/provenance-png.ts +26 -0
- package/src/provenance-signed.ts +7 -0
- package/src/provenance-xmp.ts +57 -0
- package/src/provenance.ts +87 -0
- package/src/reference-image-size.ts +67 -0
- package/src/reference-read.ts +50 -0
- package/src/server.ts +711 -0
- package/src/video-providers/index.ts +18 -0
- package/src/video-providers/minimax.ts +96 -0
- package/src/video-providers/reference-encoding.ts +32 -0
- package/src/video-providers/types.ts +47 -0
- package/src/video-providers/unified.ts +68 -0
- package/test/audio-models.test.ts +428 -0
- package/test/audio-references.test.ts +428 -0
- package/test/custom-catalogue.test.ts +257 -0
- package/test/daily-media-meter.test.ts +287 -0
- package/test/failure.test.ts +190 -0
- package/test/fixtures/cloudflare-workers.fixture.ts +10 -0
- package/test/fixtures/media-operation.fixture.ts +134 -0
- package/test/fixtures/media-team-prompts.ts +7 -0
- package/test/fixtures/native-sqlite.fixture.ts +40 -0
- package/test/gateway-models.test.ts +712 -0
- package/test/media-operation-do.test.ts +1565 -0
- package/test/media-quote.test.ts +81 -0
- package/test/media-retention.test.ts +597 -0
- package/test/media-service.test.ts +549 -0
- package/test/media-team-prompts.test.ts +92 -0
- package/test/model-facts.test.ts +126 -0
- package/test/prompt-limit.test.ts +53 -0
- package/test/provenance-export.test.ts +144 -0
- package/test/provenance-images.fixture.mjs +17 -0
- package/test/provenance.test.ts +179 -0
- package/test/provider-gateway.test.ts +114 -0
- package/test/provider-reasons.test.ts +133 -0
- package/test/r2-destination.test.ts +166 -0
- package/test/reference-image-size.test.ts +73 -0
- package/test/reference-images-billing.test.ts +40 -0
- package/test/reference-images.test.ts +399 -0
- package/test/standalone.cloudflare.test.mjs +298 -0
- package/test/standalone.worker.mjs +70 -0
- package/test/transform-models.test.ts +400 -0
- package/test/video-capabilities.test.ts +160 -0
- package/test/video-contract.test.ts +533 -0
- package/test/video-failure-lifecycle.test.ts +177 -0
- package/test/video-provider.test.ts +136 -0
- package/test/video-wire.test.ts +251 -0
- package/vitest.cloudflare.config.ts +22 -0
- package/vitest.node.config.ts +14 -0
|
@@ -0,0 +1,1574 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
|
|
3
|
+
/** An explicitly requested image encoding; video is outside this contract. */
|
|
4
|
+
export const mediaRequestedImageFormatSchema = z.enum({
|
|
5
|
+
/** PNG image encoding. */
|
|
6
|
+
png: "png",
|
|
7
|
+
/** JPEG image encoding. */
|
|
8
|
+
jpeg: "jpeg",
|
|
9
|
+
/** WebP image encoding. */
|
|
10
|
+
webp: "webp",
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Agent-facing selection tier: cheap/fast draft work, the balanced default,
|
|
15
|
+
* premium for art the user will see, and max for the most expensive models.
|
|
16
|
+
* One value per model; never a price.
|
|
17
|
+
*/
|
|
18
|
+
export const mediaModelTierSchema = z.enum({
|
|
19
|
+
/** Cheap, fast draft model. */
|
|
20
|
+
draft: "draft",
|
|
21
|
+
/** Balanced model selection. */
|
|
22
|
+
balanced: "balanced",
|
|
23
|
+
/** Premium model selection. */
|
|
24
|
+
premium: "premium",
|
|
25
|
+
/** Most expensive model selection. */
|
|
26
|
+
max: "max",
|
|
27
|
+
});
|
|
28
|
+
/** A model selection tier, independent of billing price. */
|
|
29
|
+
export type MediaModelTier = z.output<typeof mediaModelTierSchema>;
|
|
30
|
+
|
|
31
|
+
/** Media's provider catalog: Workers AI images plus gateway image, video and audio models. */
|
|
32
|
+
export const mediaModelInputSchema = z.lazy(() =>
|
|
33
|
+
z
|
|
34
|
+
.strictObject({
|
|
35
|
+
/**
|
|
36
|
+
* Provider input transport. Multipart is the binding's documented
|
|
37
|
+
* ReadableStream body, which AI Gateway cannot carry, so multipart models
|
|
38
|
+
* run without the gateway option (see cloudflare/provider.ts).
|
|
39
|
+
*/
|
|
40
|
+
transport: z.enum({
|
|
41
|
+
/** JSON provider input. */
|
|
42
|
+
json: "json",
|
|
43
|
+
/** Multipart provider input. */
|
|
44
|
+
multipart: "multipart",
|
|
45
|
+
}),
|
|
46
|
+
/** Whether this model accepts explicit output dimensions. */
|
|
47
|
+
dimensions: z.enum({
|
|
48
|
+
/** Provider chooses its own dimensions. */
|
|
49
|
+
none: "none",
|
|
50
|
+
/** Provider accepts the requested dimensions. */
|
|
51
|
+
own: "own",
|
|
52
|
+
}),
|
|
53
|
+
/** Maximum accepted dimension in pixels, or null when unbounded by this model. */
|
|
54
|
+
maxDimension: z.number().int().positive().nullable(),
|
|
55
|
+
/** Whether the provider's own page documents `maxDimension`; false for host caps. */
|
|
56
|
+
maxDimensionDocumented: z.boolean(),
|
|
57
|
+
/** Default output width in pixels. */
|
|
58
|
+
defaultWidth: z.number().int().positive(),
|
|
59
|
+
/** Default output height in pixels. */
|
|
60
|
+
defaultHeight: z.number().int().positive(),
|
|
61
|
+
/** Model-specific inference step count. */
|
|
62
|
+
steps: z.enum({
|
|
63
|
+
/** Provider reads the steps field. */
|
|
64
|
+
steps: "steps",
|
|
65
|
+
/** Provider reads the num_steps field. */
|
|
66
|
+
num_steps: "num_steps",
|
|
67
|
+
/** Provider uses fixed inference steps. */
|
|
68
|
+
fixed: "fixed",
|
|
69
|
+
}),
|
|
70
|
+
/** Maximum model steps, or null when the model has no step ceiling. */
|
|
71
|
+
maxSteps: z.number().int().positive().nullable(),
|
|
72
|
+
/** Default inference step count. */
|
|
73
|
+
defaultSteps: z.number().int().nonnegative().nullable(),
|
|
74
|
+
/**
|
|
75
|
+
* Prompt-guidance (`guidance`) bounds and default sent to the provider, or
|
|
76
|
+
* null when the model takes no guidance field. A host's own catalogue may
|
|
77
|
+
* omit the key; absent means null.
|
|
78
|
+
*/
|
|
79
|
+
guidance: z
|
|
80
|
+
.strictObject({
|
|
81
|
+
/** Lowest value the provider accepts. */
|
|
82
|
+
min: z.number().nonnegative(),
|
|
83
|
+
/** Highest value the provider accepts. */
|
|
84
|
+
max: z.number().positive(),
|
|
85
|
+
/** Value sent when the caller names none. */
|
|
86
|
+
default: z.number().nonnegative(),
|
|
87
|
+
})
|
|
88
|
+
.readonly()
|
|
89
|
+
.refine(
|
|
90
|
+
(g) => g.min <= g.default && g.default <= g.max,
|
|
91
|
+
"guidance default must lie within its bounds",
|
|
92
|
+
)
|
|
93
|
+
.nullable()
|
|
94
|
+
.default(null),
|
|
95
|
+
/** Provider multipart reference-field prefix, or null when unsupported. */
|
|
96
|
+
referenceField: z.string().min(1).max(256).nullable(),
|
|
97
|
+
/** Maximum references accepted by this model. */
|
|
98
|
+
maxReferences: z.number().int().nonnegative(),
|
|
99
|
+
/** Long edge, in pixels, an image model's references are fitted to; null for video models. */
|
|
100
|
+
referenceDimension: z.number().int().positive().nullable(),
|
|
101
|
+
/** Encoding of a fitted reference: PNG is lossless, WebP bounds the request body. */
|
|
102
|
+
referenceFormat: z.enum({
|
|
103
|
+
/** Lossless PNG. */
|
|
104
|
+
png: "png",
|
|
105
|
+
/** Lossy WebP. */
|
|
106
|
+
webp: "webp",
|
|
107
|
+
}),
|
|
108
|
+
})
|
|
109
|
+
.readonly(),
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Schema ceiling for reference lists: the largest per-model limit. Each model's
|
|
114
|
+
* own `maxReferences` / `maxReferenceImages` is enforced after parsing.
|
|
115
|
+
*/
|
|
116
|
+
export const MAX_REFERENCE_COUNT = 30;
|
|
117
|
+
|
|
118
|
+
/** Verified Unified AI video identifiers; no inferred family aliases. */
|
|
119
|
+
export const mediaVideoModelIdSchema = z.enum({
|
|
120
|
+
/** Seedance 2.5. */
|
|
121
|
+
seedance25: "bytedance/seedance-2.5",
|
|
122
|
+
/** Seedance 2.0. */
|
|
123
|
+
seedance: "bytedance/seedance-2.0",
|
|
124
|
+
/** Seedance 2.0 Fast. */
|
|
125
|
+
seedanceFast: "bytedance/seedance-2.0-fast",
|
|
126
|
+
/** Seedance 2.0 Mini. */
|
|
127
|
+
seedanceMini: "bytedance/seedance-2.0-mini",
|
|
128
|
+
/** Veo 3.1. */
|
|
129
|
+
veo: "google/veo-3.1",
|
|
130
|
+
/** Veo 3.1 Fast. */
|
|
131
|
+
veoFast: "google/veo-3.1-fast",
|
|
132
|
+
/** MiniMax H3 Max. */
|
|
133
|
+
minimaxH3Max: "minimax/h3-max",
|
|
134
|
+
});
|
|
135
|
+
/** Verified video model identifier. */
|
|
136
|
+
export type MediaVideoModelId = z.output<typeof mediaVideoModelIdSchema>;
|
|
137
|
+
/** Provider resolution selectors, refined against the selected catalog entry. */
|
|
138
|
+
export const mediaVideoResolutionSchema = z.enum({
|
|
139
|
+
/** 480p. */
|
|
140
|
+
p480: "480p",
|
|
141
|
+
/** 720p. */
|
|
142
|
+
p720: "720p",
|
|
143
|
+
/** 768p, admitted only by MiniMax H3 Max. */
|
|
144
|
+
p768: "768p",
|
|
145
|
+
/** 1080p. */
|
|
146
|
+
p1080: "1080p",
|
|
147
|
+
/** 4k, admitted only by Seedance 2.0. */
|
|
148
|
+
p4k: "4k",
|
|
149
|
+
});
|
|
150
|
+
/** A video resolution selector. */
|
|
151
|
+
export type MediaVideoResolution = z.output<typeof mediaVideoResolutionSchema>;
|
|
152
|
+
/** Documented video aspect selectors, refined per model. */
|
|
153
|
+
export const mediaVideoAspectRatioSchema = z.enum({
|
|
154
|
+
/** Landscape widescreen. */
|
|
155
|
+
wide: "16:9",
|
|
156
|
+
/** Landscape standard. */
|
|
157
|
+
landscape: "4:3",
|
|
158
|
+
/** Square. */
|
|
159
|
+
square: "1:1",
|
|
160
|
+
/** Portrait standard. */
|
|
161
|
+
portrait: "3:4",
|
|
162
|
+
/** Portrait widescreen. */
|
|
163
|
+
tall: "9:16",
|
|
164
|
+
/** Landscape ultrawide. */
|
|
165
|
+
ultrawide: "21:9",
|
|
166
|
+
/** Portrait ultrawide. */
|
|
167
|
+
ultratall: "9:21",
|
|
168
|
+
});
|
|
169
|
+
/** A video aspect selector. */
|
|
170
|
+
export type MediaVideoAspectRatio = z.output<
|
|
171
|
+
typeof mediaVideoAspectRatioSchema
|
|
172
|
+
>;
|
|
173
|
+
/** Explicit reference meaning; frame mode and reference mode are exclusive. */
|
|
174
|
+
export const mediaVideoReferenceRoleSchema = z.enum({
|
|
175
|
+
/** First frame; required when a last frame is supplied. */
|
|
176
|
+
first: "first-frame",
|
|
177
|
+
/** Last frame, supported by Seedance only (`last_frame_image`). */
|
|
178
|
+
last: "last-frame",
|
|
179
|
+
/** One image in Seedance reference-image mode. */
|
|
180
|
+
reference: "reference",
|
|
181
|
+
});
|
|
182
|
+
/** The role of a selected reference image in a video request. */
|
|
183
|
+
export type MediaVideoReferenceRole = z.output<
|
|
184
|
+
typeof mediaVideoReferenceRoleSchema
|
|
185
|
+
>;
|
|
186
|
+
|
|
187
|
+
/** One nominal size used to select provider resolution and aspect. */
|
|
188
|
+
export const mediaVideoFormatSchema = z.lazy(() =>
|
|
189
|
+
z
|
|
190
|
+
.strictObject({
|
|
191
|
+
/** Provider resolution. */
|
|
192
|
+
resolution: mediaVideoResolutionSchema,
|
|
193
|
+
/** Requested aspect; Seedance may derive actual aspect from its first image. */
|
|
194
|
+
aspectRatio: mediaVideoAspectRatioSchema,
|
|
195
|
+
/** Nominal width; actual MP4 dimensions are not decoded. */
|
|
196
|
+
width: z.number().int().positive(),
|
|
197
|
+
/** Nominal height; actual MP4 dimensions are not decoded. */
|
|
198
|
+
height: z.number().int().positive(),
|
|
199
|
+
})
|
|
200
|
+
.readonly(),
|
|
201
|
+
);
|
|
202
|
+
/** One immutable nominal video format. */
|
|
203
|
+
export type MediaVideoFormat = z.output<typeof mediaVideoFormatSchema>;
|
|
204
|
+
|
|
205
|
+
/** Verified video parameters and provider costs, checked 2026-10-11. */
|
|
206
|
+
export const mediaVideoSchema = z.lazy(() =>
|
|
207
|
+
z
|
|
208
|
+
.strictObject({
|
|
209
|
+
/** Minimum duration, for callers displaying bounds. */
|
|
210
|
+
minSeconds: z.number().int().positive(),
|
|
211
|
+
/** Maximum duration; durations still owns membership. */
|
|
212
|
+
maxSeconds: z.number().int().positive(),
|
|
213
|
+
/** Default duration. */
|
|
214
|
+
defaultSeconds: z.number().int().positive(),
|
|
215
|
+
/** Exact accepted whole-second durations. */
|
|
216
|
+
durations: z.array(z.number().int().positive()).readonly(),
|
|
217
|
+
/** Verified provider input grammar. */
|
|
218
|
+
provider: z.enum({
|
|
219
|
+
/** Seedance integer duration and data-URI image fields. */
|
|
220
|
+
seedance: "seedance",
|
|
221
|
+
/** Veo duration string and base64 image_input. */
|
|
222
|
+
veo: "veo",
|
|
223
|
+
/** MiniMax multimodal `content[]` array and uppercase resolution. */
|
|
224
|
+
minimax: "minimax",
|
|
225
|
+
}),
|
|
226
|
+
/** Supported resolution/aspect combinations and their nominal selectors. */
|
|
227
|
+
formats: z.array(mediaVideoFormatSchema).readonly(),
|
|
228
|
+
/** Resolution selected when omitted. */
|
|
229
|
+
defaultResolution: mediaVideoResolutionSchema,
|
|
230
|
+
/** Aspect selected when omitted. */
|
|
231
|
+
defaultAspectRatio: mediaVideoAspectRatioSchema,
|
|
232
|
+
/** Whether the model documents a `generate_audio` choice. */
|
|
233
|
+
supportsAudio: z.boolean(),
|
|
234
|
+
/** Audio default, or null when not documented and left to the provider. */
|
|
235
|
+
defaultGenerateAudio: z.boolean().nullable(),
|
|
236
|
+
/** Frame or reference roles this provider accepts. */
|
|
237
|
+
referenceRoles: z.array(mediaVideoReferenceRoleSchema).readonly(),
|
|
238
|
+
/** Reference-image mode count; first/last frame mode has its own grammar. */
|
|
239
|
+
maxReferenceImages: z.number().int().nonnegative(),
|
|
240
|
+
/** Resolutions supporting reference-image mode. */
|
|
241
|
+
referenceResolutions: z.array(mediaVideoResolutionSchema).readonly(),
|
|
242
|
+
/**
|
|
243
|
+
* Audio clips accepted as `reference_audios`, or null when the model has
|
|
244
|
+
* no audio references. Declared by the provider's input schema, never
|
|
245
|
+
* inferred from the family.
|
|
246
|
+
*/
|
|
247
|
+
audioReferences: z
|
|
248
|
+
.strictObject({
|
|
249
|
+
/** Most clips in one request. */
|
|
250
|
+
max: z.number().int().positive(),
|
|
251
|
+
/** Longest total duration of all clips, in seconds. */
|
|
252
|
+
totalSeconds: z.number().int().positive(),
|
|
253
|
+
})
|
|
254
|
+
.readonly()
|
|
255
|
+
.nullable(),
|
|
256
|
+
/** Non-video-input provider cost; 1 USD = 100,000 milliunits. */
|
|
257
|
+
prices: z
|
|
258
|
+
.array(
|
|
259
|
+
z
|
|
260
|
+
.strictObject({
|
|
261
|
+
/** Billed output resolution. */
|
|
262
|
+
resolution: mediaVideoResolutionSchema,
|
|
263
|
+
/** Provider milliunits per output second without audio. */
|
|
264
|
+
silentMilliunitsPerSecond: z.number().int().positive(),
|
|
265
|
+
/** Provider milliunits per output second with audio. */
|
|
266
|
+
audioMilliunitsPerSecond: z.number().int().positive(),
|
|
267
|
+
})
|
|
268
|
+
.readonly(),
|
|
269
|
+
)
|
|
270
|
+
.readonly(),
|
|
271
|
+
})
|
|
272
|
+
.readonly(),
|
|
273
|
+
);
|
|
274
|
+
/** Immutable video capability and price metadata. */
|
|
275
|
+
export type MediaVideoModel = z.output<typeof mediaVideoSchema>;
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Verified ElevenLabs model identifiers on Cloudflare's Unified Billing, from
|
|
279
|
+
* the model pages under https://developers.cloudflare.com/ai/models/elevenlabs/
|
|
280
|
+
* (checked 2026-11-13). Cloudflare offers no sound-effects model. Eleven Flash
|
|
281
|
+
* v2.5 and Turbo v2.5 are listed there but a live Unified Billing call to Flash
|
|
282
|
+
* answered 403 code 2047 ("not available via unified billing. Please use
|
|
283
|
+
* BYOK"), so they are not offered. Multilingual v2 is the default voice model.
|
|
284
|
+
*/
|
|
285
|
+
export const mediaAudioModelIdSchema = z.enum({
|
|
286
|
+
/** Eleven Multilingual v2 text to speech. */
|
|
287
|
+
multilingual: "elevenlabs/eleven-multilingual-v2",
|
|
288
|
+
/** Eleven v3 expressive text to speech. */
|
|
289
|
+
v3: "elevenlabs/eleven-v3",
|
|
290
|
+
/** Eleven Music v2. */
|
|
291
|
+
music: "elevenlabs/music-v2",
|
|
292
|
+
});
|
|
293
|
+
/** Verified audio model identifier. */
|
|
294
|
+
export type MediaAudioModelId = z.output<typeof mediaAudioModelIdSchema>;
|
|
295
|
+
|
|
296
|
+
/** One premade voice a speech model may speak with. */
|
|
297
|
+
export const mediaAudioVoiceSchema = z.lazy(() =>
|
|
298
|
+
z
|
|
299
|
+
.strictObject({
|
|
300
|
+
/** ElevenLabs voice identifier, exactly as Cloudflare's docs list it. */
|
|
301
|
+
id: z.string().min(1).max(64),
|
|
302
|
+
/** Customer-visible voice label. */
|
|
303
|
+
label: z.string().min(1).max(128),
|
|
304
|
+
})
|
|
305
|
+
.readonly(),
|
|
306
|
+
);
|
|
307
|
+
/** One premade speech voice. */
|
|
308
|
+
export type MediaAudioVoice = z.output<typeof mediaAudioVoiceSchema>;
|
|
309
|
+
|
|
310
|
+
/** Verified audio parameters and provider cost, checked 2026-11-13. */
|
|
311
|
+
export const mediaAudioSchema = z.lazy(() =>
|
|
312
|
+
z
|
|
313
|
+
.strictObject({
|
|
314
|
+
/** Provider translation family. */
|
|
315
|
+
provider: z.literal("elevenlabs"),
|
|
316
|
+
/** Speech reads the prompt aloud; music composes from it. */
|
|
317
|
+
mode: z.enum({
|
|
318
|
+
/** Text to speech: the prompt is the spoken text. */
|
|
319
|
+
speech: "speech",
|
|
320
|
+
/** Music: the prompt describes the track. */
|
|
321
|
+
music: "music",
|
|
322
|
+
}),
|
|
323
|
+
/** Premade voices; empty for music. */
|
|
324
|
+
voices: z.array(mediaAudioVoiceSchema).readonly(),
|
|
325
|
+
/** Voice used when none is chosen; null for music. */
|
|
326
|
+
defaultVoiceId: z.string().min(1).max(64).nullable(),
|
|
327
|
+
/** Music length bounds in whole seconds; null for speech. */
|
|
328
|
+
seconds: z
|
|
329
|
+
.strictObject({
|
|
330
|
+
/** Shortest track. */
|
|
331
|
+
min: z.number().int().positive(),
|
|
332
|
+
/** Longest track. */
|
|
333
|
+
max: z.number().int().positive(),
|
|
334
|
+
/** Length when omitted. */
|
|
335
|
+
default: z.number().int().positive(),
|
|
336
|
+
})
|
|
337
|
+
.readonly()
|
|
338
|
+
.nullable(),
|
|
339
|
+
/** Provider milliunits per billed unit; 1 USD = 100,000 milliunits. */
|
|
340
|
+
milliunitsPerUnit: z.number().int().positive(),
|
|
341
|
+
})
|
|
342
|
+
.readonly(),
|
|
343
|
+
);
|
|
344
|
+
/** Immutable audio capability and price metadata. */
|
|
345
|
+
export type MediaAudioModel = z.output<typeof mediaAudioSchema>;
|
|
346
|
+
|
|
347
|
+
/** Characters one speech billing block covers. */
|
|
348
|
+
export const AUDIO_CHARACTER_BLOCK = 100;
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Structural ceiling for any prompt on the wire. Each model's own
|
|
352
|
+
* `maxPromptLength` is the real bound; no model may exceed this ceiling.
|
|
353
|
+
*/
|
|
354
|
+
export const MAX_MEDIA_PROMPT_LENGTH = 32_000;
|
|
355
|
+
/** Prompt cap kept for models whose provider documents no prompt limit. */
|
|
356
|
+
const UNDOCUMENTED_PROMPT_LENGTH = 2_048;
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* What a source-image operation does to its one reference image. Both write a
|
|
360
|
+
* new image and never touch the source.
|
|
361
|
+
*/
|
|
362
|
+
export const mediaTransformOperationSchema = z.enum({
|
|
363
|
+
/** Cut the foreground out onto a transparent PNG. */
|
|
364
|
+
remove_background: "remove_background",
|
|
365
|
+
/** Enlarge the image to a chosen output size in megapixels. */
|
|
366
|
+
upscale: "upscale",
|
|
367
|
+
});
|
|
368
|
+
/** A source-image operation. */
|
|
369
|
+
export type MediaTransformOperation = z.output<
|
|
370
|
+
typeof mediaTransformOperationSchema
|
|
371
|
+
>;
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Facts of a catalog entry that edits one source image rather than generating
|
|
375
|
+
* from a prompt. The source limits are Botanical's own conservative bounds, not
|
|
376
|
+
* Cloudflare's: neither model page publishes an input size limit.
|
|
377
|
+
*/
|
|
378
|
+
export const mediaTransformSchema = z.lazy(() =>
|
|
379
|
+
z
|
|
380
|
+
.strictObject({
|
|
381
|
+
/** What the operation does. */
|
|
382
|
+
operation: mediaTransformOperationSchema,
|
|
383
|
+
/** Botanical's limits on the one source image, checked before any paid call. */
|
|
384
|
+
source: z
|
|
385
|
+
.strictObject({
|
|
386
|
+
/** Accepted source encodings. */
|
|
387
|
+
formats: z.array(mediaRequestedImageFormatSchema).readonly(),
|
|
388
|
+
/** Largest source, in bytes (the hosts' existing reference read limit). */
|
|
389
|
+
maxBytes: z.number().int().positive(),
|
|
390
|
+
/** Largest source, in pixels. */
|
|
391
|
+
maxPixels: z.number().int().positive(),
|
|
392
|
+
})
|
|
393
|
+
.readonly(),
|
|
394
|
+
/** Output size chosen when none is given, or null when the operation has no size. */
|
|
395
|
+
defaultMegapixels: z.number().int().positive().nullable(),
|
|
396
|
+
/**
|
|
397
|
+
* Provider milliunits for one image when the price does not depend on
|
|
398
|
+
* the output size, else null (1 USD = 100,000 milliunits).
|
|
399
|
+
*/
|
|
400
|
+
milliunits: z.number().int().positive().nullable(),
|
|
401
|
+
/** Output sizes offered, each with its provider milliunits; empty for a flat price. */
|
|
402
|
+
sizes: z
|
|
403
|
+
.array(
|
|
404
|
+
z
|
|
405
|
+
.strictObject({
|
|
406
|
+
/** Output size in megapixels. */
|
|
407
|
+
megapixels: z.number().int().positive(),
|
|
408
|
+
/** Provider milliunits for one image at this size. */
|
|
409
|
+
milliunits: z.number().int().positive(),
|
|
410
|
+
})
|
|
411
|
+
.readonly(),
|
|
412
|
+
)
|
|
413
|
+
.readonly(),
|
|
414
|
+
})
|
|
415
|
+
.readonly(),
|
|
416
|
+
);
|
|
417
|
+
/** Immutable source-image operation facts. */
|
|
418
|
+
export type MediaTransform = z.output<typeof mediaTransformSchema>;
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Pruna P-Image-Upscale's published price per image by output size, copied
|
|
422
|
+
* exactly: [largest output megapixels in the tier, USD].
|
|
423
|
+
* https://developers.cloudflare.com/ai/models/pruna/p-image-upscale/
|
|
424
|
+
*/
|
|
425
|
+
const UPSCALE_PRICE_TIERS: readonly (readonly [number, number])[] = [
|
|
426
|
+
[4, 0.005],
|
|
427
|
+
[8, 0.01],
|
|
428
|
+
[16, 0.02],
|
|
429
|
+
[32, 0.04],
|
|
430
|
+
[64, 0.06],
|
|
431
|
+
[128, 0.12],
|
|
432
|
+
];
|
|
433
|
+
/** The output sizes Botanical offers; the provider accepts 1 to 128. */
|
|
434
|
+
const UPSCALE_TARGET_MEGAPIXELS = [4, 8, 16] as const;
|
|
435
|
+
const USD_TO_MILLIUNITS = 100_000;
|
|
436
|
+
/** Bria RMBG 2.0's published price per image: https://developers.cloudflare.com/ai/models/bria/remove-background/ */
|
|
437
|
+
const REMOVE_BACKGROUND_USD = 0.018;
|
|
438
|
+
/** Botanical's own source bounds for both operations (see {@link mediaTransformSchema}). */
|
|
439
|
+
const TRANSFORM_SOURCE_MAX_BYTES = 8 * 1024 * 1024;
|
|
440
|
+
const TRANSFORM_SOURCE_MAX_PIXELS = 25_000_000;
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* What one generation bills: whole images, whole seconds of video or music, or
|
|
444
|
+
* blocks of {@link AUDIO_CHARACTER_BLOCK} speech characters (so a per-character
|
|
445
|
+
* provider rate is marked up once per block, not rounded up per character).
|
|
446
|
+
*/
|
|
447
|
+
export const mediaBillingUnitSchema = z.enum({
|
|
448
|
+
/** One image. */
|
|
449
|
+
image: "image",
|
|
450
|
+
/** One whole second of video. */
|
|
451
|
+
video_second: "video_second",
|
|
452
|
+
/** One whole second of music. */
|
|
453
|
+
audio_second: "audio_second",
|
|
454
|
+
/** One block of speech characters. */
|
|
455
|
+
audio_character_block: "audio_character_block",
|
|
456
|
+
});
|
|
457
|
+
/** The unit a model's generations are billed in. */
|
|
458
|
+
export type MediaBillingUnit = z.output<typeof mediaBillingUnitSchema>;
|
|
459
|
+
|
|
460
|
+
/** The one rule mapping a model's kind (and speech or music mode) to its billing unit. */
|
|
461
|
+
export const mediaBillingUnit = (
|
|
462
|
+
model: Readonly<{
|
|
463
|
+
kind: "image" | "video" | "audio";
|
|
464
|
+
audio: Readonly<{ mode: "speech" | "music" }> | null;
|
|
465
|
+
}>,
|
|
466
|
+
): MediaBillingUnit => {
|
|
467
|
+
if (model.kind === "video") return "video_second";
|
|
468
|
+
if (model.kind === "audio")
|
|
469
|
+
return model.audio?.mode === "speech"
|
|
470
|
+
? "audio_character_block"
|
|
471
|
+
: "audio_second";
|
|
472
|
+
return "image";
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
/** Public wire grammar for a catalog entry returned by the Media owner. */
|
|
476
|
+
export const mediaModelSchema = z
|
|
477
|
+
.strictObject({
|
|
478
|
+
/** Provider model identifier. */
|
|
479
|
+
id: z.string().min(1).max(256),
|
|
480
|
+
/** Customer-visible model label. */
|
|
481
|
+
label: z.string().min(1).max(512),
|
|
482
|
+
/** Outcome or model-kind discriminant. */
|
|
483
|
+
kind: z.enum({
|
|
484
|
+
/** Still-image generation. */
|
|
485
|
+
image: "image",
|
|
486
|
+
/** Video generation. */
|
|
487
|
+
video: "video",
|
|
488
|
+
/** Speech or music generation. */
|
|
489
|
+
audio: "audio",
|
|
490
|
+
}),
|
|
491
|
+
/** Model selection tier, independent of price. */
|
|
492
|
+
tier: mediaModelTierSchema,
|
|
493
|
+
/** Verified provider encodings; an empty list refuses pinned formats. */
|
|
494
|
+
outputFormats: z.array(mediaRequestedImageFormatSchema).max(3).readonly(),
|
|
495
|
+
/**
|
|
496
|
+
* Maximum prompt length in characters for this model: the provider's
|
|
497
|
+
* documented limit, or the retained 2,048 host cap where none is documented.
|
|
498
|
+
*/
|
|
499
|
+
maxPromptLength: z.number().int().positive().max(MAX_MEDIA_PROMPT_LENGTH),
|
|
500
|
+
/** Model-native input options and reference limits. */
|
|
501
|
+
input: mediaModelInputSchema,
|
|
502
|
+
/**
|
|
503
|
+
* Image output sizes or nominal video selectors, as `WIDTHxHEIGHT`; null means
|
|
504
|
+
* any size within the input bounds.
|
|
505
|
+
*/
|
|
506
|
+
sizes: z
|
|
507
|
+
.array(z.string().regex(/^\d+x\d+$/u))
|
|
508
|
+
.readonly()
|
|
509
|
+
.nullable(),
|
|
510
|
+
/** Video length constraints, or null for a still-image model. */
|
|
511
|
+
video: mediaVideoSchema.nullable(),
|
|
512
|
+
/** Speech or music constraints, or null for an image or video model. */
|
|
513
|
+
audio: mediaAudioSchema.nullable(),
|
|
514
|
+
/**
|
|
515
|
+
* Price basis: the unit one generation bills in. The cost of those units
|
|
516
|
+
* depends on the settings (size, steps, resolution, sound, references), so
|
|
517
|
+
* an App shows `priceEstimate` where its host supplies one and otherwise
|
|
518
|
+
* asks its host for a quote of the exact settings.
|
|
519
|
+
*/
|
|
520
|
+
billingUnit: mediaBillingUnitSchema,
|
|
521
|
+
/** Source-image operation facts, or null for a model that generates from a prompt. */
|
|
522
|
+
transform: mediaTransformSchema.nullable(),
|
|
523
|
+
})
|
|
524
|
+
.readonly();
|
|
525
|
+
|
|
526
|
+
/** Model-specific dimensions, steps, transport and reference limits. */
|
|
527
|
+
export type MediaModelInput = z.output<typeof mediaModelInputSchema>;
|
|
528
|
+
/** One immutable provider catalog entry. */
|
|
529
|
+
export type MediaModel = z.output<typeof mediaModelSchema>;
|
|
530
|
+
|
|
531
|
+
const freezeModel = (
|
|
532
|
+
model: Omit<
|
|
533
|
+
MediaModel,
|
|
534
|
+
| "kind"
|
|
535
|
+
| "sizes"
|
|
536
|
+
| "video"
|
|
537
|
+
| "audio"
|
|
538
|
+
| "outputFormats"
|
|
539
|
+
| "billingUnit"
|
|
540
|
+
| "transform"
|
|
541
|
+
> &
|
|
542
|
+
Partial<
|
|
543
|
+
Pick<
|
|
544
|
+
MediaModel,
|
|
545
|
+
"kind" | "sizes" | "video" | "audio" | "outputFormats" | "transform"
|
|
546
|
+
>
|
|
547
|
+
>,
|
|
548
|
+
): MediaModel => {
|
|
549
|
+
const entry = {
|
|
550
|
+
kind: "image" as const,
|
|
551
|
+
sizes: null,
|
|
552
|
+
video: null,
|
|
553
|
+
audio: null,
|
|
554
|
+
transform: null,
|
|
555
|
+
outputFormats: Object.freeze([]),
|
|
556
|
+
...model,
|
|
557
|
+
};
|
|
558
|
+
return Object.freeze({
|
|
559
|
+
...entry,
|
|
560
|
+
billingUnit: mediaBillingUnit(entry),
|
|
561
|
+
input: Object.freeze(model.input),
|
|
562
|
+
});
|
|
563
|
+
};
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* GPT Image 2's `size` is `auto` or any WIDTHxHEIGHT with each edge at most
|
|
567
|
+
* 3840, both edges multiples of 16, an aspect ratio between 1:3 and 3:1 and a
|
|
568
|
+
* total of 655,360 to 8,294,400 pixels (no enum).
|
|
569
|
+
* https://developers.cloudflare.com/ai/models/openai/gpt-image-2/
|
|
570
|
+
* https://developers.cloudflare.com/ai/models/openai/gpt-image-2/schema-input.json
|
|
571
|
+
* The catalog lists each common ratio at a standard size (about one megapixel;
|
|
572
|
+
* the three sizes Prints always offered stay as they were) and at the largest
|
|
573
|
+
* size the limits allow, so every entry is exact in ratio and in bounds.
|
|
574
|
+
*/
|
|
575
|
+
const GPT_IMAGE_MAX_EDGE = 3_840;
|
|
576
|
+
/** Largest pixel count the documented size grammar admits. */
|
|
577
|
+
const GPT_IMAGE_MAX_PIXELS = 8_294_400;
|
|
578
|
+
const COMMON_IMAGE_RATIOS = [
|
|
579
|
+
[1, 1],
|
|
580
|
+
[4, 3],
|
|
581
|
+
[3, 4],
|
|
582
|
+
[3, 2],
|
|
583
|
+
[2, 3],
|
|
584
|
+
[16, 9],
|
|
585
|
+
[9, 16],
|
|
586
|
+
[21, 9],
|
|
587
|
+
] as const;
|
|
588
|
+
const gptImageSizes: readonly string[] = (() => {
|
|
589
|
+
const keep = new Set(["1024x1024", "1024x1536", "1536x1024"]);
|
|
590
|
+
const standard: string[] = [];
|
|
591
|
+
const largest: string[] = [];
|
|
592
|
+
for (const [across, down] of COMMON_IMAGE_RATIOS) {
|
|
593
|
+
const divisor = ((a: number, b: number): number => {
|
|
594
|
+
let x = a;
|
|
595
|
+
let y = b;
|
|
596
|
+
while (y !== 0) [x, y] = [y, x % y];
|
|
597
|
+
return x;
|
|
598
|
+
})(across, down);
|
|
599
|
+
const unitW = (16 * across) / divisor;
|
|
600
|
+
const unitH = (16 * down) / divisor;
|
|
601
|
+
const unitArea = unitW * unitH;
|
|
602
|
+
const at = (k: number) => `${String(unitW * k)}x${String(unitH * k)}`;
|
|
603
|
+
const fit = Math.round(Math.sqrt(1_048_576 / unitArea));
|
|
604
|
+
const known = [...keep].find((size) => {
|
|
605
|
+
const [w, h] = size.split("x").map(Number);
|
|
606
|
+
return (w ?? 0) * down === (h ?? 0) * across;
|
|
607
|
+
});
|
|
608
|
+
standard.push(known ?? at(fit));
|
|
609
|
+
largest.push(
|
|
610
|
+
at(
|
|
611
|
+
Math.floor(
|
|
612
|
+
Math.min(
|
|
613
|
+
GPT_IMAGE_MAX_EDGE / Math.max(unitW, unitH),
|
|
614
|
+
Math.sqrt(GPT_IMAGE_MAX_PIXELS / unitArea),
|
|
615
|
+
),
|
|
616
|
+
),
|
|
617
|
+
),
|
|
618
|
+
);
|
|
619
|
+
}
|
|
620
|
+
return Object.freeze([...standard, ...largest]);
|
|
621
|
+
})();
|
|
622
|
+
|
|
623
|
+
/** The common ratios as exact 16-aligned sizes of about `area` pixels. */
|
|
624
|
+
const commonRatioSizes = (area: number): readonly string[] =>
|
|
625
|
+
Object.freeze(
|
|
626
|
+
COMMON_IMAGE_RATIOS.map(([across, down]) => {
|
|
627
|
+
let x: number = across;
|
|
628
|
+
let y: number = down;
|
|
629
|
+
while (y !== 0) [x, y] = [y, x % y];
|
|
630
|
+
const unitW = (16 * across) / x;
|
|
631
|
+
const unitH = (16 * down) / x;
|
|
632
|
+
const steps = Math.round(Math.sqrt(area / (unitW * unitH)));
|
|
633
|
+
return `${String(unitW * steps)}x${String(unitH * steps)}`;
|
|
634
|
+
}),
|
|
635
|
+
);
|
|
636
|
+
|
|
637
|
+
/**
|
|
638
|
+
* Seedream 5 Pro takes `size` as a string: "1K", "2K" or an explicit pixel
|
|
639
|
+
* size (the page's examples are "1K" and "1536x864"). It documents no bounds,
|
|
640
|
+
* so each common ratio is listed as an exact explicit size at the scale of its
|
|
641
|
+
* own 1536x864 example (about 1.3 megapixels), where 16:9 is that example.
|
|
642
|
+
* https://developers.cloudflare.com/ai/models/bytedance/seedream-5-pro/
|
|
643
|
+
* https://developers.cloudflare.com/ai/models/bytedance/seedream-5-pro/schema-input.json
|
|
644
|
+
*/
|
|
645
|
+
const seedreamSizes = commonRatioSizes(1536 * 864);
|
|
646
|
+
|
|
647
|
+
const videoShortEdges = {
|
|
648
|
+
"480p": 480,
|
|
649
|
+
"720p": 720,
|
|
650
|
+
"768p": 768,
|
|
651
|
+
"1080p": 1080,
|
|
652
|
+
"4k": 2160,
|
|
653
|
+
} satisfies Record<MediaVideoResolution, number>;
|
|
654
|
+
const videoAspects = {
|
|
655
|
+
"16:9": [16, 9],
|
|
656
|
+
"4:3": [4, 3],
|
|
657
|
+
"1:1": [1, 1],
|
|
658
|
+
"3:4": [3, 4],
|
|
659
|
+
"9:16": [9, 16],
|
|
660
|
+
"21:9": [21, 9],
|
|
661
|
+
"9:21": [9, 21],
|
|
662
|
+
} satisfies Record<MediaVideoAspectRatio, readonly [number, number]>;
|
|
663
|
+
|
|
664
|
+
/**
|
|
665
|
+
* Seedance 2.5's own bounds, from its Cloudflare input schema (duration 4-30,
|
|
666
|
+
* 480p/720p, `reference_images` up to 30, a 25,000-character prompt, no 9:21).
|
|
667
|
+
* https://developers.cloudflare.com/ai/models/bytedance/seedance-2.5/
|
|
668
|
+
* Prices are Cloudflare dashboard prices, supplied by the owner 2026-11-13;
|
|
669
|
+
* the public model page lists none. Only the non-video-input rates apply, as
|
|
670
|
+
* video-input references are unsupported.
|
|
671
|
+
* `reference_audios` takes at most 10 clips totalling 30 seconds
|
|
672
|
+
* (https://developers.cloudflare.com/ai/models/bytedance/seedance-2.5/schema-input.json).
|
|
673
|
+
*/
|
|
674
|
+
const seedance25 = {
|
|
675
|
+
maxSeconds: 30,
|
|
676
|
+
maxReferenceImages: 30,
|
|
677
|
+
audioReferences: { max: 10, totalSeconds: 30 },
|
|
678
|
+
maxPromptLength: 25_000,
|
|
679
|
+
resolutions: ["480p", "720p"],
|
|
680
|
+
aspects: mediaVideoAspectRatioSchema.options.filter(
|
|
681
|
+
(aspectRatio) => aspectRatio !== mediaVideoAspectRatioSchema.enum.ultratall,
|
|
682
|
+
),
|
|
683
|
+
} as const;
|
|
684
|
+
|
|
685
|
+
function videoModel(
|
|
686
|
+
id: MediaVideoModelId,
|
|
687
|
+
label: string,
|
|
688
|
+
tier: MediaModelTier,
|
|
689
|
+
provider: MediaVideoModel["provider"],
|
|
690
|
+
rates: readonly (readonly [MediaVideoResolution, number, number])[],
|
|
691
|
+
bounds?: typeof seedance25,
|
|
692
|
+
): MediaModel {
|
|
693
|
+
const seedance = provider === "seedance";
|
|
694
|
+
const resolutions: readonly MediaVideoResolution[] =
|
|
695
|
+
bounds?.resolutions ?? rates.map(([resolution]) => resolution);
|
|
696
|
+
const maxSeconds = bounds?.maxSeconds ?? (seedance ? 12 : 8);
|
|
697
|
+
const maxReferenceImages = bounds?.maxReferenceImages ?? (seedance ? 4 : 0);
|
|
698
|
+
const aspects = bounds
|
|
699
|
+
? bounds.aspects
|
|
700
|
+
: seedance
|
|
701
|
+
? mediaVideoAspectRatioSchema.options
|
|
702
|
+
: [
|
|
703
|
+
mediaVideoAspectRatioSchema.enum.wide,
|
|
704
|
+
mediaVideoAspectRatioSchema.enum.tall,
|
|
705
|
+
mediaVideoAspectRatioSchema.enum.square,
|
|
706
|
+
];
|
|
707
|
+
// Nominal dimensions select resolution/aspect; upstream does not accept pixels.
|
|
708
|
+
// Round the long edge to even pixels, retaining the existing 854x480 selector.
|
|
709
|
+
const formats = Object.freeze(
|
|
710
|
+
resolutions.flatMap((resolution) =>
|
|
711
|
+
aspects.map((aspectRatio): MediaVideoFormat => {
|
|
712
|
+
const [w, h] = videoAspects[aspectRatio];
|
|
713
|
+
const edge = videoShortEdges[resolution];
|
|
714
|
+
const shorter = Math.min(w, h);
|
|
715
|
+
return Object.freeze({
|
|
716
|
+
resolution,
|
|
717
|
+
aspectRatio,
|
|
718
|
+
width: Math.ceil((edge * w) / shorter / 2) * 2,
|
|
719
|
+
height: Math.ceil((edge * h) / shorter / 2) * 2,
|
|
720
|
+
});
|
|
721
|
+
}),
|
|
722
|
+
),
|
|
723
|
+
);
|
|
724
|
+
const durations = Object.freeze(
|
|
725
|
+
seedance
|
|
726
|
+
? Array.from({ length: maxSeconds - 3 }, (_, index) => index + 4)
|
|
727
|
+
: [4, 6, 8],
|
|
728
|
+
);
|
|
729
|
+
const video: MediaVideoModel = Object.freeze({
|
|
730
|
+
minSeconds: 4,
|
|
731
|
+
maxSeconds,
|
|
732
|
+
defaultSeconds: seedance ? 5 : 6,
|
|
733
|
+
durations,
|
|
734
|
+
provider,
|
|
735
|
+
formats,
|
|
736
|
+
defaultResolution: "720p",
|
|
737
|
+
defaultAspectRatio: "16:9",
|
|
738
|
+
// Veo documents `generate_audio` with default true. Seedance documents no
|
|
739
|
+
// default; Botanical chooses On (priced the same as silent) so a prompt's
|
|
740
|
+
// AUDIO and dialogue lines take effect without touching the control.
|
|
741
|
+
// https://developers.cloudflare.com/ai/models/bytedance/seedance-2.0/
|
|
742
|
+
// https://developers.cloudflare.com/ai/models/google/veo-3.1/
|
|
743
|
+
supportsAudio: true,
|
|
744
|
+
defaultGenerateAudio: true,
|
|
745
|
+
referenceRoles: Object.freeze(
|
|
746
|
+
seedance
|
|
747
|
+
? mediaVideoReferenceRoleSchema.options
|
|
748
|
+
: [mediaVideoReferenceRoleSchema.enum.first],
|
|
749
|
+
),
|
|
750
|
+
maxReferenceImages,
|
|
751
|
+
audioReferences:
|
|
752
|
+
bounds === undefined
|
|
753
|
+
? null
|
|
754
|
+
: Object.freeze({ ...bounds.audioReferences }),
|
|
755
|
+
referenceResolutions: Object.freeze(
|
|
756
|
+
seedance
|
|
757
|
+
? resolutions.filter((resolution) => resolution !== "1080p")
|
|
758
|
+
: [],
|
|
759
|
+
),
|
|
760
|
+
prices: Object.freeze(
|
|
761
|
+
rates.map(
|
|
762
|
+
([resolution, silentMilliunitsPerSecond, audioMilliunitsPerSecond]) =>
|
|
763
|
+
Object.freeze({
|
|
764
|
+
resolution,
|
|
765
|
+
silentMilliunitsPerSecond,
|
|
766
|
+
audioMilliunitsPerSecond,
|
|
767
|
+
}),
|
|
768
|
+
),
|
|
769
|
+
),
|
|
770
|
+
});
|
|
771
|
+
return freezeModel({
|
|
772
|
+
id,
|
|
773
|
+
label,
|
|
774
|
+
tier,
|
|
775
|
+
// Seedance 2.0, Fast and Mini: 10,000 characters, from each Cloudflare
|
|
776
|
+
// input schema (the earlier 2,000 rejected real ~2,500-character
|
|
777
|
+
// production prompts). https://developers.cloudflare.com/ai/models/bytedance/seedance-2.0/
|
|
778
|
+
// Veo documents no character limit; keep the retained 2,048 host cap.
|
|
779
|
+
maxPromptLength:
|
|
780
|
+
bounds?.maxPromptLength ??
|
|
781
|
+
(seedance ? 10_000 : UNDOCUMENTED_PROMPT_LENGTH),
|
|
782
|
+
kind: "video",
|
|
783
|
+
video,
|
|
784
|
+
sizes: Object.freeze(
|
|
785
|
+
formats.map(({ width, height }) => `${String(width)}x${String(height)}`),
|
|
786
|
+
),
|
|
787
|
+
input: {
|
|
788
|
+
transport: "json",
|
|
789
|
+
dimensions: "own",
|
|
790
|
+
maxDimension: Math.max(
|
|
791
|
+
...formats.map(({ width, height }) => Math.max(width, height)),
|
|
792
|
+
),
|
|
793
|
+
maxDimensionDocumented: false,
|
|
794
|
+
defaultWidth: 1280,
|
|
795
|
+
defaultHeight: 720,
|
|
796
|
+
steps: "fixed",
|
|
797
|
+
maxSteps: null,
|
|
798
|
+
defaultSteps: null,
|
|
799
|
+
guidance: null,
|
|
800
|
+
referenceField: seedance ? "image" : "image_input",
|
|
801
|
+
maxReferences: seedance ? maxReferenceImages : 1,
|
|
802
|
+
referenceDimension: null,
|
|
803
|
+
referenceFormat: "png",
|
|
804
|
+
},
|
|
805
|
+
});
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
// MiniMax H3 Max diverges too far from `videoModel()`'s shared seedance/veo
|
|
809
|
+
// shape to reuse it: its wire resolution literals are uppercase ("480P"),
|
|
810
|
+
// its ratio grammar admits no "9:21" selector, and its request body is a
|
|
811
|
+
// multimodal `content[]` array dispatched by `media-generation.ts`, not a
|
|
812
|
+
// flat prompt/image body. https://developers.cloudflare.com/ai/models/minimax/h3-max/
|
|
813
|
+
function minimaxVideoModel(): MediaModel {
|
|
814
|
+
const resolutions = [
|
|
815
|
+
"480p",
|
|
816
|
+
"768p",
|
|
817
|
+
] as const satisfies readonly MediaVideoResolution[];
|
|
818
|
+
const aspects = [
|
|
819
|
+
mediaVideoAspectRatioSchema.enum.wide,
|
|
820
|
+
mediaVideoAspectRatioSchema.enum.landscape,
|
|
821
|
+
mediaVideoAspectRatioSchema.enum.square,
|
|
822
|
+
mediaVideoAspectRatioSchema.enum.portrait,
|
|
823
|
+
mediaVideoAspectRatioSchema.enum.tall,
|
|
824
|
+
mediaVideoAspectRatioSchema.enum.ultrawide,
|
|
825
|
+
];
|
|
826
|
+
const formats = Object.freeze(
|
|
827
|
+
resolutions.flatMap((resolution) =>
|
|
828
|
+
aspects.map((aspectRatio): MediaVideoFormat => {
|
|
829
|
+
const [w, h] = videoAspects[aspectRatio];
|
|
830
|
+
const edge = videoShortEdges[resolution];
|
|
831
|
+
const shorter = Math.min(w, h);
|
|
832
|
+
return Object.freeze({
|
|
833
|
+
resolution,
|
|
834
|
+
aspectRatio,
|
|
835
|
+
width: Math.ceil((edge * w) / shorter / 2) * 2,
|
|
836
|
+
height: Math.ceil((edge * h) / shorter / 2) * 2,
|
|
837
|
+
});
|
|
838
|
+
}),
|
|
839
|
+
),
|
|
840
|
+
);
|
|
841
|
+
const defaultResolution = mediaVideoResolutionSchema.enum.p768;
|
|
842
|
+
const defaultAspectRatio = mediaVideoAspectRatioSchema.enum.wide;
|
|
843
|
+
const defaultFormat = formats.find(
|
|
844
|
+
(format) =>
|
|
845
|
+
format.resolution === defaultResolution &&
|
|
846
|
+
format.aspectRatio === defaultAspectRatio,
|
|
847
|
+
);
|
|
848
|
+
if (defaultFormat === undefined)
|
|
849
|
+
throw new Error("minimax/h3-max is missing its default format");
|
|
850
|
+
// Integers 5 through 15 inclusive; the documented bound has no gaps.
|
|
851
|
+
const durations = Object.freeze(
|
|
852
|
+
Array.from({ length: 11 }, (_, index) => index + 5),
|
|
853
|
+
);
|
|
854
|
+
const video: MediaVideoModel = Object.freeze({
|
|
855
|
+
minSeconds: 5,
|
|
856
|
+
maxSeconds: 15,
|
|
857
|
+
defaultSeconds: 5,
|
|
858
|
+
durations,
|
|
859
|
+
provider: "minimax",
|
|
860
|
+
formats,
|
|
861
|
+
defaultResolution,
|
|
862
|
+
defaultAspectRatio,
|
|
863
|
+
// Not merely undocumented: the schema has no audio field at all, so the
|
|
864
|
+
// dispatcher refuses an explicit generateAudio choice outright.
|
|
865
|
+
defaultGenerateAudio: null,
|
|
866
|
+
supportsAudio: false,
|
|
867
|
+
// The vendor page documents `first_frame` and `reference_image` roles only;
|
|
868
|
+
// it names no last frame.
|
|
869
|
+
referenceRoles: Object.freeze([
|
|
870
|
+
mediaVideoReferenceRoleSchema.enum.first,
|
|
871
|
+
mediaVideoReferenceRoleSchema.enum.reference,
|
|
872
|
+
]),
|
|
873
|
+
// The vendor page documents up to nine; this stays at the conservative
|
|
874
|
+
// verified value of four until the gateway schema is confirmed.
|
|
875
|
+
maxReferenceImages: 4,
|
|
876
|
+
audioReferences: null,
|
|
877
|
+
referenceResolutions: Object.freeze(resolutions),
|
|
878
|
+
prices: Object.freeze([
|
|
879
|
+
{
|
|
880
|
+
resolution: mediaVideoResolutionSchema.enum.p480,
|
|
881
|
+
// No documented audio surcharge; one rate serves both selectors.
|
|
882
|
+
silentMilliunitsPerSecond: 5_000,
|
|
883
|
+
audioMilliunitsPerSecond: 5_000,
|
|
884
|
+
},
|
|
885
|
+
{
|
|
886
|
+
resolution: mediaVideoResolutionSchema.enum.p768,
|
|
887
|
+
silentMilliunitsPerSecond: 8_000,
|
|
888
|
+
audioMilliunitsPerSecond: 8_000,
|
|
889
|
+
},
|
|
890
|
+
]),
|
|
891
|
+
});
|
|
892
|
+
return freezeModel({
|
|
893
|
+
id: mediaVideoModelIdSchema.enum.minimaxH3Max,
|
|
894
|
+
label: "MiniMax H3 Max",
|
|
895
|
+
tier: "balanced",
|
|
896
|
+
// 7,000 characters per the vendor page cited above.
|
|
897
|
+
maxPromptLength: 7_000,
|
|
898
|
+
kind: "video",
|
|
899
|
+
video,
|
|
900
|
+
sizes: Object.freeze(
|
|
901
|
+
formats.map(({ width, height }) => `${String(width)}x${String(height)}`),
|
|
902
|
+
),
|
|
903
|
+
input: {
|
|
904
|
+
transport: "json",
|
|
905
|
+
dimensions: "own",
|
|
906
|
+
maxDimension: Math.max(
|
|
907
|
+
...formats.map(({ width, height }) => Math.max(width, height)),
|
|
908
|
+
),
|
|
909
|
+
maxDimensionDocumented: false,
|
|
910
|
+
defaultWidth: defaultFormat.width,
|
|
911
|
+
defaultHeight: defaultFormat.height,
|
|
912
|
+
steps: "fixed",
|
|
913
|
+
maxSteps: null,
|
|
914
|
+
defaultSteps: null,
|
|
915
|
+
guidance: null,
|
|
916
|
+
// Inert for this gateway model (media-generation.ts builds its body
|
|
917
|
+
// directly); recorded for the same documentation parity as Seedance/Veo.
|
|
918
|
+
referenceField: "image_url",
|
|
919
|
+
maxReferences: 4,
|
|
920
|
+
referenceDimension: null,
|
|
921
|
+
referenceFormat: "png",
|
|
922
|
+
},
|
|
923
|
+
});
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
/**
|
|
927
|
+
* The one voice Cloudflare's ElevenLabs pages list: every `voice_id` example
|
|
928
|
+
* and request sample on the TTS model pages uses it, and there is no voice
|
|
929
|
+
* listing endpoint on Cloudflare. More voices are added only from a page that
|
|
930
|
+
* lists them.
|
|
931
|
+
* https://developers.cloudflare.com/ai/models/elevenlabs/eleven-v3/
|
|
932
|
+
*/
|
|
933
|
+
const premadeVoices: readonly MediaAudioVoice[] = Object.freeze([
|
|
934
|
+
Object.freeze({ id: "JBFqnCBsd6RMkjVDRZzb", label: "Default voice" }),
|
|
935
|
+
]);
|
|
936
|
+
|
|
937
|
+
/**
|
|
938
|
+
* An ElevenLabs speech model. Text is at most 10,000 characters and a
|
|
939
|
+
* `voice_id` is required (each model's `schema-input.json`); the rate is the
|
|
940
|
+
* per-character price on its Cloudflare page, 1 USD = 100,000 milliunits.
|
|
941
|
+
* https://developers.cloudflare.com/ai/models/elevenlabs/eleven-v3/
|
|
942
|
+
* https://developers.cloudflare.com/ai/models/elevenlabs/eleven-multilingual-v2/
|
|
943
|
+
*/
|
|
944
|
+
function speechModel(
|
|
945
|
+
id: MediaAudioModelId,
|
|
946
|
+
label: string,
|
|
947
|
+
tier: MediaModelTier,
|
|
948
|
+
milliunitsPerCharacter: number,
|
|
949
|
+
): MediaModel {
|
|
950
|
+
return audioModel(id, label, tier, 10_000, {
|
|
951
|
+
provider: "elevenlabs",
|
|
952
|
+
mode: "speech",
|
|
953
|
+
voices: premadeVoices,
|
|
954
|
+
defaultVoiceId: premadeVoices[0]?.id ?? null,
|
|
955
|
+
seconds: null,
|
|
956
|
+
milliunitsPerUnit: milliunitsPerCharacter,
|
|
957
|
+
});
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
/**
|
|
961
|
+
* Eleven Music v2: `music_length_ms` runs 3,000 through 600,000 and the page
|
|
962
|
+
* lists $0.0025 per output second (250 milliunits). The prompt has no
|
|
963
|
+
* documented limit, so the retained 2,048 host cap applies. The model is run
|
|
964
|
+
* with a length always set, so the quote is the length asked for.
|
|
965
|
+
* https://developers.cloudflare.com/ai/models/elevenlabs/music-v2/
|
|
966
|
+
*/
|
|
967
|
+
function musicModel(): MediaModel {
|
|
968
|
+
return audioModel(
|
|
969
|
+
mediaAudioModelIdSchema.enum.music,
|
|
970
|
+
"Eleven Music v2",
|
|
971
|
+
"premium",
|
|
972
|
+
UNDOCUMENTED_PROMPT_LENGTH,
|
|
973
|
+
{
|
|
974
|
+
provider: "elevenlabs",
|
|
975
|
+
mode: "music",
|
|
976
|
+
voices: Object.freeze([]),
|
|
977
|
+
defaultVoiceId: null,
|
|
978
|
+
seconds: Object.freeze({ min: 3, max: 600, default: 30 }),
|
|
979
|
+
milliunitsPerUnit: 250,
|
|
980
|
+
},
|
|
981
|
+
);
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
function audioModel(
|
|
985
|
+
id: MediaAudioModelId,
|
|
986
|
+
label: string,
|
|
987
|
+
tier: MediaModelTier,
|
|
988
|
+
maxPromptLength: number,
|
|
989
|
+
audio: MediaAudioModel,
|
|
990
|
+
): MediaModel {
|
|
991
|
+
return freezeModel({
|
|
992
|
+
id,
|
|
993
|
+
label,
|
|
994
|
+
tier,
|
|
995
|
+
maxPromptLength,
|
|
996
|
+
kind: "audio",
|
|
997
|
+
audio: Object.freeze(audio),
|
|
998
|
+
input: {
|
|
999
|
+
transport: "json",
|
|
1000
|
+
dimensions: "none",
|
|
1001
|
+
maxDimension: null,
|
|
1002
|
+
maxDimensionDocumented: false,
|
|
1003
|
+
// Audio has no dimensions; the shared wire grammar still requires a
|
|
1004
|
+
// positive default, and nothing reads it for an audio model.
|
|
1005
|
+
defaultWidth: 1,
|
|
1006
|
+
defaultHeight: 1,
|
|
1007
|
+
steps: "fixed",
|
|
1008
|
+
maxSteps: null,
|
|
1009
|
+
defaultSteps: null,
|
|
1010
|
+
guidance: null,
|
|
1011
|
+
referenceField: null,
|
|
1012
|
+
maxReferences: 0,
|
|
1013
|
+
referenceDimension: null,
|
|
1014
|
+
referenceFormat: "png",
|
|
1015
|
+
},
|
|
1016
|
+
});
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
/** The published tier a chosen output size falls in; every offered size has one. */
|
|
1020
|
+
function upscaleTierUsd(megapixels: number): number {
|
|
1021
|
+
const tier = UPSCALE_PRICE_TIERS.find(([largest]) => megapixels <= largest);
|
|
1022
|
+
if (tier === undefined)
|
|
1023
|
+
throw new Error(`no published upscale price for ${String(megapixels)} MP`);
|
|
1024
|
+
return tier[1];
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
/** A source-image operation model: one reference in, one new image out, no prompt text sent. */
|
|
1028
|
+
function transformModel(
|
|
1029
|
+
input: Readonly<{
|
|
1030
|
+
id: string;
|
|
1031
|
+
label: string;
|
|
1032
|
+
tier: MediaModelTier;
|
|
1033
|
+
operation: MediaTransformOperation;
|
|
1034
|
+
outputFormat: "png" | "webp";
|
|
1035
|
+
defaultMegapixels: number | null;
|
|
1036
|
+
flatUsd: number | null;
|
|
1037
|
+
sizes: readonly Readonly<{ megapixels: number; usd: number }>[];
|
|
1038
|
+
}>,
|
|
1039
|
+
): MediaModel {
|
|
1040
|
+
return freezeModel({
|
|
1041
|
+
id: input.id,
|
|
1042
|
+
label: input.label,
|
|
1043
|
+
tier: input.tier,
|
|
1044
|
+
outputFormats: Object.freeze([input.outputFormat]),
|
|
1045
|
+
// The prompt only labels the saved output ("Remove background"); the
|
|
1046
|
+
// provider never reads it.
|
|
1047
|
+
maxPromptLength: 256,
|
|
1048
|
+
transform: Object.freeze({
|
|
1049
|
+
operation: input.operation,
|
|
1050
|
+
source: Object.freeze({
|
|
1051
|
+
formats: Object.freeze(["png", "jpeg"] as const),
|
|
1052
|
+
maxBytes: TRANSFORM_SOURCE_MAX_BYTES,
|
|
1053
|
+
maxPixels: TRANSFORM_SOURCE_MAX_PIXELS,
|
|
1054
|
+
}),
|
|
1055
|
+
defaultMegapixels: input.defaultMegapixels,
|
|
1056
|
+
milliunits:
|
|
1057
|
+
input.flatUsd === null
|
|
1058
|
+
? null
|
|
1059
|
+
: Math.round(input.flatUsd * USD_TO_MILLIUNITS),
|
|
1060
|
+
sizes: Object.freeze(
|
|
1061
|
+
input.sizes.map(({ megapixels, usd }) =>
|
|
1062
|
+
Object.freeze({
|
|
1063
|
+
megapixels,
|
|
1064
|
+
milliunits: Math.round(usd * USD_TO_MILLIUNITS),
|
|
1065
|
+
}),
|
|
1066
|
+
),
|
|
1067
|
+
),
|
|
1068
|
+
}),
|
|
1069
|
+
input: {
|
|
1070
|
+
transport: "json",
|
|
1071
|
+
dimensions: "none",
|
|
1072
|
+
maxDimension: null,
|
|
1073
|
+
maxDimensionDocumented: false,
|
|
1074
|
+
defaultWidth: 1024,
|
|
1075
|
+
defaultHeight: 1024,
|
|
1076
|
+
steps: "fixed",
|
|
1077
|
+
maxSteps: null,
|
|
1078
|
+
defaultSteps: null,
|
|
1079
|
+
guidance: null,
|
|
1080
|
+
referenceField: "image",
|
|
1081
|
+
maxReferences: 1,
|
|
1082
|
+
// The hosts' reference fitter only shrinks; this keeps any source within
|
|
1083
|
+
// the pixel bound at its original size.
|
|
1084
|
+
referenceDimension: 8_192,
|
|
1085
|
+
referenceFormat: "png",
|
|
1086
|
+
},
|
|
1087
|
+
});
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
const models: readonly MediaModel[] = Object.freeze([
|
|
1091
|
+
freezeModel({
|
|
1092
|
+
id: "@cf/black-forest-labs/flux-1-schnell",
|
|
1093
|
+
// 2,048 characters: input schema maxLength at developers.cloudflare.com/workers-ai/models/flux-1-schnell/.
|
|
1094
|
+
maxPromptLength: 2_048,
|
|
1095
|
+
outputFormats: Object.freeze(["jpeg"]),
|
|
1096
|
+
tier: "draft",
|
|
1097
|
+
label: "Flux 1 Schnell — cheap/fast sketches",
|
|
1098
|
+
input: {
|
|
1099
|
+
transport: "json",
|
|
1100
|
+
dimensions: "none",
|
|
1101
|
+
maxDimension: null,
|
|
1102
|
+
maxDimensionDocumented: false,
|
|
1103
|
+
defaultWidth: 1024,
|
|
1104
|
+
defaultHeight: 768,
|
|
1105
|
+
steps: "steps",
|
|
1106
|
+
maxSteps: 8,
|
|
1107
|
+
defaultSteps: 4,
|
|
1108
|
+
guidance: null,
|
|
1109
|
+
referenceField: null,
|
|
1110
|
+
maxReferences: 0,
|
|
1111
|
+
// No documented reference size for this model; the retained 512 px fit.
|
|
1112
|
+
referenceDimension: 512,
|
|
1113
|
+
referenceFormat: "png",
|
|
1114
|
+
},
|
|
1115
|
+
}),
|
|
1116
|
+
freezeModel({
|
|
1117
|
+
id: "@cf/black-forest-labs/flux-2-klein-4b",
|
|
1118
|
+
// Workers AI documents no prompt maxLength for this model; keep the 2,048 host cap.
|
|
1119
|
+
maxPromptLength: UNDOCUMENTED_PROMPT_LENGTH,
|
|
1120
|
+
tier: "balanced",
|
|
1121
|
+
label: "Flux 2 Klein 4b — balanced default",
|
|
1122
|
+
input: {
|
|
1123
|
+
transport: "multipart",
|
|
1124
|
+
dimensions: "own",
|
|
1125
|
+
maxDimension: 1920,
|
|
1126
|
+
maxDimensionDocumented: false,
|
|
1127
|
+
defaultWidth: 1024,
|
|
1128
|
+
defaultHeight: 768,
|
|
1129
|
+
steps: "fixed",
|
|
1130
|
+
maxSteps: null,
|
|
1131
|
+
defaultSteps: 4,
|
|
1132
|
+
guidance: null,
|
|
1133
|
+
referenceField: "input_image_",
|
|
1134
|
+
maxReferences: 4,
|
|
1135
|
+
// No documented reference size for this model; the retained 512 px fit.
|
|
1136
|
+
referenceDimension: 512,
|
|
1137
|
+
referenceFormat: "png",
|
|
1138
|
+
},
|
|
1139
|
+
}),
|
|
1140
|
+
freezeModel({
|
|
1141
|
+
id: "@cf/black-forest-labs/flux-2-klein-9b",
|
|
1142
|
+
// Workers AI documents no prompt maxLength for this model; keep the 2,048 host cap.
|
|
1143
|
+
maxPromptLength: UNDOCUMENTED_PROMPT_LENGTH,
|
|
1144
|
+
tier: "premium",
|
|
1145
|
+
label: "Flux 2 Klein 9b — higher quality flux-2",
|
|
1146
|
+
input: {
|
|
1147
|
+
transport: "multipart",
|
|
1148
|
+
dimensions: "own",
|
|
1149
|
+
maxDimension: 1920,
|
|
1150
|
+
maxDimensionDocumented: false,
|
|
1151
|
+
defaultWidth: 1024,
|
|
1152
|
+
defaultHeight: 768,
|
|
1153
|
+
steps: "fixed",
|
|
1154
|
+
maxSteps: null,
|
|
1155
|
+
defaultSteps: 4,
|
|
1156
|
+
guidance: null,
|
|
1157
|
+
referenceField: "input_image_",
|
|
1158
|
+
maxReferences: 4,
|
|
1159
|
+
// No documented reference size for this model; the retained 512 px fit.
|
|
1160
|
+
referenceDimension: 512,
|
|
1161
|
+
referenceFormat: "png",
|
|
1162
|
+
},
|
|
1163
|
+
}),
|
|
1164
|
+
freezeModel({
|
|
1165
|
+
id: "@cf/black-forest-labs/flux-2-dev",
|
|
1166
|
+
// Workers AI documents no prompt maxLength for this model; keep the 2,048 host cap.
|
|
1167
|
+
maxPromptLength: UNDOCUMENTED_PROMPT_LENGTH,
|
|
1168
|
+
tier: "max",
|
|
1169
|
+
label: "Flux 2 Dev — best detailed quality, expensive",
|
|
1170
|
+
input: {
|
|
1171
|
+
transport: "multipart",
|
|
1172
|
+
dimensions: "own",
|
|
1173
|
+
maxDimension: 1920,
|
|
1174
|
+
maxDimensionDocumented: false,
|
|
1175
|
+
defaultWidth: 1024,
|
|
1176
|
+
defaultHeight: 768,
|
|
1177
|
+
steps: "steps",
|
|
1178
|
+
maxSteps: null,
|
|
1179
|
+
defaultSteps: 20,
|
|
1180
|
+
guidance: null,
|
|
1181
|
+
referenceField: "input_image_",
|
|
1182
|
+
maxReferences: 4,
|
|
1183
|
+
// No documented reference size for this model; the retained 512 px fit.
|
|
1184
|
+
referenceDimension: 512,
|
|
1185
|
+
referenceFormat: "png",
|
|
1186
|
+
},
|
|
1187
|
+
}),
|
|
1188
|
+
freezeModel({
|
|
1189
|
+
id: "@cf/leonardo/lucid-origin",
|
|
1190
|
+
// Workers AI documents no prompt maxLength for this model; keep the 2,048 host cap.
|
|
1191
|
+
maxPromptLength: UNDOCUMENTED_PROMPT_LENGTH,
|
|
1192
|
+
tier: "premium",
|
|
1193
|
+
label: "Leonardo Lucid Origin — premium prompt adherence",
|
|
1194
|
+
input: {
|
|
1195
|
+
transport: "json",
|
|
1196
|
+
dimensions: "own",
|
|
1197
|
+
// https://developers.cloudflare.com/workers-ai/models/lucid-origin/ (width/height maximum)
|
|
1198
|
+
maxDimension: 2500,
|
|
1199
|
+
maxDimensionDocumented: true,
|
|
1200
|
+
defaultWidth: 1120,
|
|
1201
|
+
defaultHeight: 1120,
|
|
1202
|
+
steps: "num_steps",
|
|
1203
|
+
maxSteps: 40,
|
|
1204
|
+
defaultSteps: null,
|
|
1205
|
+
// https://developers.cloudflare.com/workers-ai/models/lucid-origin/ (guidance range); the
|
|
1206
|
+
// default is the value Loftwah's studio tuned for this model.
|
|
1207
|
+
guidance: { min: 0, max: 10, default: 5 },
|
|
1208
|
+
referenceField: null,
|
|
1209
|
+
maxReferences: 0,
|
|
1210
|
+
// No documented reference size for this model; the retained 512 px fit.
|
|
1211
|
+
referenceDimension: 512,
|
|
1212
|
+
referenceFormat: "png",
|
|
1213
|
+
},
|
|
1214
|
+
}),
|
|
1215
|
+
freezeModel({
|
|
1216
|
+
id: "@cf/leonardo/phoenix-1.0",
|
|
1217
|
+
// Workers AI documents no prompt maxLength for this model; keep the 2,048 host cap.
|
|
1218
|
+
maxPromptLength: UNDOCUMENTED_PROMPT_LENGTH,
|
|
1219
|
+
tier: "balanced",
|
|
1220
|
+
label: "Leonardo Phoenix 1.0 — best prompt adherence",
|
|
1221
|
+
input: {
|
|
1222
|
+
transport: "json",
|
|
1223
|
+
dimensions: "own",
|
|
1224
|
+
// https://developers.cloudflare.com/workers-ai/models/phoenix-1.0/ (width/height range)
|
|
1225
|
+
maxDimension: 2048,
|
|
1226
|
+
maxDimensionDocumented: true,
|
|
1227
|
+
defaultWidth: 1024,
|
|
1228
|
+
defaultHeight: 1024,
|
|
1229
|
+
steps: "num_steps",
|
|
1230
|
+
maxSteps: 50,
|
|
1231
|
+
defaultSteps: 25,
|
|
1232
|
+
// https://developers.cloudflare.com/workers-ai/models/phoenix-1.0/ (guidance range); the
|
|
1233
|
+
// default is the value Loftwah's studio tuned for this model.
|
|
1234
|
+
guidance: { min: 2, max: 10, default: 3 },
|
|
1235
|
+
referenceField: null,
|
|
1236
|
+
maxReferences: 0,
|
|
1237
|
+
// No documented reference size for this model; the retained 512 px fit.
|
|
1238
|
+
referenceDimension: 512,
|
|
1239
|
+
referenceFormat: "png",
|
|
1240
|
+
},
|
|
1241
|
+
}),
|
|
1242
|
+
// Third-party models run through the AI Gateway (Unified Billing): OpenAI's
|
|
1243
|
+
// image model and ByteDance's video model return a result URL, not bytes.
|
|
1244
|
+
freezeModel({
|
|
1245
|
+
id: "openai/gpt-image-2",
|
|
1246
|
+
// 32,000 characters: OpenAI's Images API reference gives GPT image models a 32000-character prompt limit.
|
|
1247
|
+
maxPromptLength: 32_000,
|
|
1248
|
+
outputFormats: Object.freeze(["png", "jpeg", "webp"]),
|
|
1249
|
+
tier: "premium",
|
|
1250
|
+
label: "GPT Image 2 — strong text and layout, premium",
|
|
1251
|
+
sizes: gptImageSizes,
|
|
1252
|
+
input: {
|
|
1253
|
+
transport: "json",
|
|
1254
|
+
dimensions: "own",
|
|
1255
|
+
maxDimension: GPT_IMAGE_MAX_EDGE,
|
|
1256
|
+
maxDimensionDocumented: true,
|
|
1257
|
+
defaultWidth: 1024,
|
|
1258
|
+
defaultHeight: 1024,
|
|
1259
|
+
steps: "fixed",
|
|
1260
|
+
maxSteps: null,
|
|
1261
|
+
defaultSteps: null,
|
|
1262
|
+
guidance: null,
|
|
1263
|
+
// Cloudflare documents `images[]` (base64 strings or data URIs, 1-16
|
|
1264
|
+
// entries, routed to OpenAI's /v1/images/edits) for this exact model:
|
|
1265
|
+
// https://developers.cloudflare.com/ai/models/openai/gpt-image-2/
|
|
1266
|
+
// OpenAI's own page shows multipart `image[]` without stating a cap, so
|
|
1267
|
+
// 16 is Cloudflare's published schema limit, the route Prints calls.
|
|
1268
|
+
referenceField: "images",
|
|
1269
|
+
maxReferences: 16,
|
|
1270
|
+
// Neither OpenAI nor Cloudflare documents an input size for this model;
|
|
1271
|
+
// it reads every input at high fidelity and is reported to upscale
|
|
1272
|
+
// small inputs toward 1024 px, so 512 px references were upscaled blur.
|
|
1273
|
+
// 1024 px is also the edge of its documented 1024x1024 output.
|
|
1274
|
+
referenceDimension: 1024,
|
|
1275
|
+
// Its `images[]` data URIs accept webp (Cloudflare schema); WebP keeps
|
|
1276
|
+
// 16 references well under the request body bound that PNG would break.
|
|
1277
|
+
referenceFormat: "webp",
|
|
1278
|
+
},
|
|
1279
|
+
}),
|
|
1280
|
+
freezeModel({
|
|
1281
|
+
id: "bytedance/seedream-5-pro",
|
|
1282
|
+
// The page documents no prompt maxLength (schema-input.json has none):
|
|
1283
|
+
// keep the 2,048 host cap.
|
|
1284
|
+
maxPromptLength: UNDOCUMENTED_PROMPT_LENGTH,
|
|
1285
|
+
// The page's example results are .jpeg URLs; the schema has no format field.
|
|
1286
|
+
outputFormats: Object.freeze(["jpeg"]),
|
|
1287
|
+
tier: "premium",
|
|
1288
|
+
label: "Seedream 5 Pro — ByteDance, high-quality images",
|
|
1289
|
+
sizes: seedreamSizes,
|
|
1290
|
+
input: {
|
|
1291
|
+
transport: "json",
|
|
1292
|
+
dimensions: "own",
|
|
1293
|
+
// The longest listed edge: the page documents no maximum.
|
|
1294
|
+
maxDimension: 1792,
|
|
1295
|
+
maxDimensionDocumented: false,
|
|
1296
|
+
defaultWidth: 1536,
|
|
1297
|
+
defaultHeight: 864,
|
|
1298
|
+
steps: "fixed",
|
|
1299
|
+
maxSteps: null,
|
|
1300
|
+
defaultSteps: null,
|
|
1301
|
+
guidance: null,
|
|
1302
|
+
// The page says the model takes up to 10 reference images (`image`, a
|
|
1303
|
+
// string or 1-10 strings) but shows only URL inputs and states no
|
|
1304
|
+
// base64 or data-URI form, so Prints sends none until it does.
|
|
1305
|
+
referenceField: null,
|
|
1306
|
+
maxReferences: 0,
|
|
1307
|
+
referenceDimension: 512,
|
|
1308
|
+
referenceFormat: "png",
|
|
1309
|
+
},
|
|
1310
|
+
}),
|
|
1311
|
+
transformModel({
|
|
1312
|
+
id: "bria/remove-background",
|
|
1313
|
+
label: "Remove background — Bria RMBG 2.0",
|
|
1314
|
+
tier: "draft",
|
|
1315
|
+
operation: "remove_background",
|
|
1316
|
+
outputFormat: "png",
|
|
1317
|
+
flatUsd: REMOVE_BACKGROUND_USD,
|
|
1318
|
+
sizes: [],
|
|
1319
|
+
defaultMegapixels: null,
|
|
1320
|
+
}),
|
|
1321
|
+
transformModel({
|
|
1322
|
+
id: "pruna/p-image-upscale",
|
|
1323
|
+
label: "Upscale — Pruna P-Image-Upscale",
|
|
1324
|
+
tier: "draft",
|
|
1325
|
+
operation: "upscale",
|
|
1326
|
+
// Lossless output would swell past the output byte bound at 16 megapixels;
|
|
1327
|
+
// WebP keeps transparency.
|
|
1328
|
+
outputFormat: "webp",
|
|
1329
|
+
flatUsd: null,
|
|
1330
|
+
sizes: UPSCALE_TARGET_MEGAPIXELS.map((megapixels) => ({
|
|
1331
|
+
megapixels,
|
|
1332
|
+
usd: upscaleTierUsd(megapixels),
|
|
1333
|
+
})),
|
|
1334
|
+
defaultMegapixels: 4,
|
|
1335
|
+
}),
|
|
1336
|
+
videoModel(
|
|
1337
|
+
mediaVideoModelIdSchema.enum.seedance25,
|
|
1338
|
+
"Seedance 2.5",
|
|
1339
|
+
"balanced",
|
|
1340
|
+
"seedance",
|
|
1341
|
+
[
|
|
1342
|
+
["480p", 10_280, 10_280],
|
|
1343
|
+
["720p", 23_120, 23_120],
|
|
1344
|
+
],
|
|
1345
|
+
seedance25,
|
|
1346
|
+
),
|
|
1347
|
+
videoModel(
|
|
1348
|
+
mediaVideoModelIdSchema.enum.seedance,
|
|
1349
|
+
"Seedance 2.0",
|
|
1350
|
+
"balanced",
|
|
1351
|
+
"seedance",
|
|
1352
|
+
[
|
|
1353
|
+
["480p", 7_000, 7_000],
|
|
1354
|
+
["720p", 15_000, 15_000],
|
|
1355
|
+
["1080p", 37_000, 37_000],
|
|
1356
|
+
["4k", 78_000, 78_000],
|
|
1357
|
+
],
|
|
1358
|
+
),
|
|
1359
|
+
videoModel(
|
|
1360
|
+
mediaVideoModelIdSchema.enum.seedanceFast,
|
|
1361
|
+
"Seedance 2.0 Fast",
|
|
1362
|
+
"draft",
|
|
1363
|
+
"seedance",
|
|
1364
|
+
[
|
|
1365
|
+
["480p", 6_000, 6_000],
|
|
1366
|
+
["720p", 12_000, 12_000],
|
|
1367
|
+
],
|
|
1368
|
+
),
|
|
1369
|
+
videoModel(
|
|
1370
|
+
mediaVideoModelIdSchema.enum.seedanceMini,
|
|
1371
|
+
"Seedance 2.0 Mini",
|
|
1372
|
+
"draft",
|
|
1373
|
+
"seedance",
|
|
1374
|
+
[
|
|
1375
|
+
["480p", 4_000, 4_000],
|
|
1376
|
+
["720p", 9_000, 9_000],
|
|
1377
|
+
],
|
|
1378
|
+
),
|
|
1379
|
+
videoModel(mediaVideoModelIdSchema.enum.veo, "Veo 3.1", "premium", "veo", [
|
|
1380
|
+
["720p", 20_000, 40_000],
|
|
1381
|
+
["1080p", 20_000, 40_000],
|
|
1382
|
+
]),
|
|
1383
|
+
videoModel(
|
|
1384
|
+
mediaVideoModelIdSchema.enum.veoFast,
|
|
1385
|
+
"Veo 3.1 Fast",
|
|
1386
|
+
"balanced",
|
|
1387
|
+
"veo",
|
|
1388
|
+
[
|
|
1389
|
+
["720p", 8_000, 10_000],
|
|
1390
|
+
["1080p", 10_000, 12_000],
|
|
1391
|
+
],
|
|
1392
|
+
),
|
|
1393
|
+
minimaxVideoModel(),
|
|
1394
|
+
// ElevenLabs on Unified Billing: URL results, like the video models.
|
|
1395
|
+
speechModel(
|
|
1396
|
+
mediaAudioModelIdSchema.enum.multilingual,
|
|
1397
|
+
"Eleven Multilingual v2 — natural speech",
|
|
1398
|
+
"balanced",
|
|
1399
|
+
10,
|
|
1400
|
+
),
|
|
1401
|
+
speechModel(
|
|
1402
|
+
mediaAudioModelIdSchema.enum.v3,
|
|
1403
|
+
"Eleven v3 — expressive speech",
|
|
1404
|
+
"premium",
|
|
1405
|
+
10,
|
|
1406
|
+
),
|
|
1407
|
+
musicModel(),
|
|
1408
|
+
]);
|
|
1409
|
+
|
|
1410
|
+
/** The Fungi gateway provider's own immutable image, video and audio model catalog. */
|
|
1411
|
+
export const mediaModels: readonly MediaModel[] = models;
|
|
1412
|
+
/**
|
|
1413
|
+
* One catalogue entry. Same grammar as the gateway's wire entry, except that a
|
|
1414
|
+
* video model may name `host` as its provider family: the injected provider
|
|
1415
|
+
* owns that model's wire grammar and Darkroom builds no gateway request for it.
|
|
1416
|
+
*/
|
|
1417
|
+
export const mediaCatalogueModelSchema = mediaModelSchema
|
|
1418
|
+
.unwrap()
|
|
1419
|
+
.extend({
|
|
1420
|
+
/** Optional on a host's entry because Darkroom derives it; a stated value must match. */
|
|
1421
|
+
billingUnit: mediaBillingUnitSchema.optional(),
|
|
1422
|
+
/** Speech or music constraints; a host's catalogue omits it unless it lists audio models. */
|
|
1423
|
+
audio: mediaAudioSchema.unwrap().nullable().default(null),
|
|
1424
|
+
/** Source-image operation facts; a host's catalogue omits it unless it lists such models. */
|
|
1425
|
+
transform: mediaTransformSchema.unwrap().nullable().default(null),
|
|
1426
|
+
/** Video length constraints, or null for a still-image model. */
|
|
1427
|
+
video: mediaVideoSchema
|
|
1428
|
+
.unwrap()
|
|
1429
|
+
.unwrap()
|
|
1430
|
+
.extend({
|
|
1431
|
+
/** Audio reference limits; a host's catalogue omits it unless its model takes audio references. */
|
|
1432
|
+
audioReferences: mediaVideoSchema
|
|
1433
|
+
.unwrap()
|
|
1434
|
+
.unwrap()
|
|
1435
|
+
.shape.audioReferences.default(null),
|
|
1436
|
+
/** Provider translation family, or `host` for a host-injected provider. */
|
|
1437
|
+
provider: z.enum({
|
|
1438
|
+
/** Seedance integer duration and data-URI image fields. */
|
|
1439
|
+
seedance: "seedance",
|
|
1440
|
+
/** Veo duration string and base64 image_input. */
|
|
1441
|
+
veo: "veo",
|
|
1442
|
+
/** MiniMax multimodal `content[]` array and uppercase resolution. */
|
|
1443
|
+
minimax: "minimax",
|
|
1444
|
+
/** The injected provider owns the wire grammar. */
|
|
1445
|
+
host: "host",
|
|
1446
|
+
}),
|
|
1447
|
+
})
|
|
1448
|
+
.readonly()
|
|
1449
|
+
.nullable(),
|
|
1450
|
+
})
|
|
1451
|
+
.readonly();
|
|
1452
|
+
/** One provider's catalogue entry. */
|
|
1453
|
+
export type MediaCatalogueModel = z.output<typeof mediaCatalogueModelSchema>;
|
|
1454
|
+
/** One provider's model list: the typed bounds every request is checked against. */
|
|
1455
|
+
export type MediaCatalogue = readonly MediaCatalogueModel[];
|
|
1456
|
+
/** Upper bound on one provider's model list. */
|
|
1457
|
+
const MAX_CATALOGUE_MODELS = 256;
|
|
1458
|
+
|
|
1459
|
+
/**
|
|
1460
|
+
* Decode a host-supplied provider catalogue. Each entry is the same typed wire
|
|
1461
|
+
* grammar the built-in catalogue uses; this adds the cross-field facts the
|
|
1462
|
+
* validator relies on (unique ids, video metadata iff `kind` is video, defaults
|
|
1463
|
+
* inside their own bounds).
|
|
1464
|
+
*/
|
|
1465
|
+
export const mediaCatalogueSchema = z
|
|
1466
|
+
.array(mediaCatalogueModelSchema)
|
|
1467
|
+
.min(1)
|
|
1468
|
+
.max(MAX_CATALOGUE_MODELS)
|
|
1469
|
+
.superRefine((entries, context) => {
|
|
1470
|
+
const seen = new Set<string>();
|
|
1471
|
+
entries.forEach((model, index) => {
|
|
1472
|
+
const report = (message: string) => {
|
|
1473
|
+
context.addIssue({ code: "custom", message, path: [index] });
|
|
1474
|
+
};
|
|
1475
|
+
if (seen.has(model.id)) report(`duplicate model id "${model.id}"`);
|
|
1476
|
+
seen.add(model.id);
|
|
1477
|
+
if ((model.kind === "video") !== (model.video !== null))
|
|
1478
|
+
report(
|
|
1479
|
+
`${model.id} must carry video bounds exactly when kind is video`,
|
|
1480
|
+
);
|
|
1481
|
+
if ((model.kind === "audio") !== (model.audio !== null))
|
|
1482
|
+
report(
|
|
1483
|
+
`${model.id} must carry audio bounds exactly when kind is audio`,
|
|
1484
|
+
);
|
|
1485
|
+
if (
|
|
1486
|
+
model.billingUnit !== undefined &&
|
|
1487
|
+
model.billingUnit !== mediaBillingUnit(model)
|
|
1488
|
+
)
|
|
1489
|
+
report(
|
|
1490
|
+
`${model.id} billingUnit must be ${mediaBillingUnit(model)} for ${model.kind === "image" ? "an" : "a"} ${model.kind} model`,
|
|
1491
|
+
);
|
|
1492
|
+
const video = model.video;
|
|
1493
|
+
if (video === null) return;
|
|
1494
|
+
if (
|
|
1495
|
+
video.minSeconds > video.maxSeconds ||
|
|
1496
|
+
video.durations.some(
|
|
1497
|
+
(seconds) => seconds < video.minSeconds || seconds > video.maxSeconds,
|
|
1498
|
+
) ||
|
|
1499
|
+
!video.durations.includes(video.defaultSeconds)
|
|
1500
|
+
)
|
|
1501
|
+
report(
|
|
1502
|
+
`${model.id} durations must sit inside min/max and include the default`,
|
|
1503
|
+
);
|
|
1504
|
+
if (
|
|
1505
|
+
!video.formats.some(
|
|
1506
|
+
({ resolution, aspectRatio }) =>
|
|
1507
|
+
resolution === video.defaultResolution &&
|
|
1508
|
+
aspectRatio === video.defaultAspectRatio,
|
|
1509
|
+
)
|
|
1510
|
+
)
|
|
1511
|
+
report(
|
|
1512
|
+
`${model.id} formats must include the default resolution and aspect`,
|
|
1513
|
+
);
|
|
1514
|
+
});
|
|
1515
|
+
})
|
|
1516
|
+
.transform((entries): MediaCatalogue =>
|
|
1517
|
+
Object.freeze(
|
|
1518
|
+
entries.map((model) =>
|
|
1519
|
+
Object.freeze({ ...model, input: Object.freeze(model.input) }),
|
|
1520
|
+
),
|
|
1521
|
+
),
|
|
1522
|
+
);
|
|
1523
|
+
|
|
1524
|
+
/** Decode untrusted catalogue data, throwing a MediaSettingsError that names the first problem. */
|
|
1525
|
+
export const parseMediaCatalogue = (raw: unknown): MediaCatalogue => {
|
|
1526
|
+
const parsed = mediaCatalogueSchema.safeParse(raw);
|
|
1527
|
+
if (!parsed.success)
|
|
1528
|
+
throw new MediaSettingsError(
|
|
1529
|
+
`media catalogue is invalid: ${parsed.error.issues[0]?.message ?? "unreadable"}`,
|
|
1530
|
+
);
|
|
1531
|
+
return parsed.data;
|
|
1532
|
+
};
|
|
1533
|
+
|
|
1534
|
+
/** A catalogue entry as the public `/models` list returns it: the entry plus its derived billing unit. */
|
|
1535
|
+
export const withBillingUnit = (
|
|
1536
|
+
model: MediaCatalogueModel,
|
|
1537
|
+
): MediaCatalogueModel & Readonly<{ billingUnit: MediaBillingUnit }> => ({
|
|
1538
|
+
...model,
|
|
1539
|
+
billingUnit: mediaBillingUnit(model),
|
|
1540
|
+
});
|
|
1541
|
+
|
|
1542
|
+
/**
|
|
1543
|
+
* Provider milliunits for one image of a source-image operation at `megapixels`
|
|
1544
|
+
* (omitted: the model's default), or undefined when that output size is not offered.
|
|
1545
|
+
*/
|
|
1546
|
+
export const transformMilliunits = (
|
|
1547
|
+
transform: MediaTransform,
|
|
1548
|
+
megapixels: number | undefined,
|
|
1549
|
+
): number | undefined => {
|
|
1550
|
+
if (transform.milliunits !== null)
|
|
1551
|
+
return megapixels === undefined ? transform.milliunits : undefined;
|
|
1552
|
+
const size = megapixels ?? transform.defaultMegapixels;
|
|
1553
|
+
return transform.sizes.find((price) => price.megapixels === size)?.milliunits;
|
|
1554
|
+
};
|
|
1555
|
+
|
|
1556
|
+
/** List the gateway provider's models in catalog order. */
|
|
1557
|
+
export const listMediaModels = (): readonly MediaModel[] => mediaModels;
|
|
1558
|
+
/** Find a model in the Fungi gateway's own catalogue; gateway-internal code never sees a custom provider's models. */
|
|
1559
|
+
export const getGatewayModel = (id: string): MediaModel | undefined =>
|
|
1560
|
+
mediaModels.find((model) => model.id === id);
|
|
1561
|
+
/** Find a model in `catalogue` (the gateway's by default), or undefined when absent. */
|
|
1562
|
+
export const getMediaModel = (
|
|
1563
|
+
id: string,
|
|
1564
|
+
catalogue: MediaCatalogue = mediaModels,
|
|
1565
|
+
): MediaCatalogueModel | undefined =>
|
|
1566
|
+
catalogue.find((model) => model.id === id);
|
|
1567
|
+
|
|
1568
|
+
/** Invalid generation settings or unsupported model parameters. */
|
|
1569
|
+
export class MediaSettingsError extends Error {
|
|
1570
|
+
constructor(message: string, options?: ErrorOptions) {
|
|
1571
|
+
super(message, options);
|
|
1572
|
+
this.name = "MediaSettingsError";
|
|
1573
|
+
}
|
|
1574
|
+
}
|