@fungi.computer/darkroom 0.11.11 → 0.11.12
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 +7 -1
- 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,98 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import {
|
|
3
|
+
decodeMediaProviderDiagnostic,
|
|
4
|
+
type MediaProviderDiagnostic,
|
|
5
|
+
} from "@fungi.computer/agar/errors";
|
|
6
|
+
|
|
7
|
+
/** Public diagnostic labels only; never provider messages or request data. */
|
|
8
|
+
export const mediaFailureStageSchema = z.enum({
|
|
9
|
+
/** Host admission before operation creation. */
|
|
10
|
+
admission: "admission",
|
|
11
|
+
/** Host authority check before dispatch. */
|
|
12
|
+
"generation-authorization": "generation-authorization",
|
|
13
|
+
/** Generation settings validation. */
|
|
14
|
+
settings: "settings",
|
|
15
|
+
/** Reference preparation and cost estimation. */
|
|
16
|
+
estimate: "estimate",
|
|
17
|
+
/** Host price admission or settlement. */
|
|
18
|
+
billing: "billing",
|
|
19
|
+
/** Durable operation transition. */
|
|
20
|
+
operation: "operation",
|
|
21
|
+
/** Provider dispatch. */
|
|
22
|
+
provider: "provider",
|
|
23
|
+
/** Provider output decoding. */
|
|
24
|
+
normalization: "normalization",
|
|
25
|
+
/** Retained output storage. */
|
|
26
|
+
artifact: "artifact",
|
|
27
|
+
/** Host permission check before saving. */
|
|
28
|
+
"destination-authorization": "destination-authorization",
|
|
29
|
+
/** Host destination write. */
|
|
30
|
+
"destination-write": "destination-write",
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
/** Execution stage at which a generation failed. */
|
|
34
|
+
export type MediaFailureStage = Exclude<
|
|
35
|
+
z.output<typeof mediaFailureStageSchema>,
|
|
36
|
+
"admission" | "settings"
|
|
37
|
+
>;
|
|
38
|
+
|
|
39
|
+
/** Private cause plus an independently decoded, safe provider diagnostic. */
|
|
40
|
+
export type MediaExecutionErrorOptions = ErrorOptions & {
|
|
41
|
+
/** Safe provider facts, when the failure came from provider transport. */
|
|
42
|
+
readonly provider?: MediaProviderDiagnostic | undefined;
|
|
43
|
+
/** Confirmed zero-unit customer settlement, never an upstream billing claim. */
|
|
44
|
+
readonly customerCharge?: "none" | undefined;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** A bounded stage marker for failures that cross the Media HTTP boundary. */
|
|
48
|
+
export class MediaExecutionError extends Error {
|
|
49
|
+
/** Safe provider facts only; the original cause remains private. */
|
|
50
|
+
readonly provider: MediaProviderDiagnostic | undefined;
|
|
51
|
+
/** Unknown unless the execution owner confirmed zero-unit settlement. */
|
|
52
|
+
readonly customerCharge: "none" | "unknown";
|
|
53
|
+
|
|
54
|
+
constructor(
|
|
55
|
+
/** Execution stage where the failure occurred. */
|
|
56
|
+
readonly stage: MediaFailureStage,
|
|
57
|
+
options?: MediaExecutionErrorOptions,
|
|
58
|
+
) {
|
|
59
|
+
super(`Media ${stage} failed`, options);
|
|
60
|
+
this.name = "MediaExecutionError";
|
|
61
|
+
this.customerCharge = options?.customerCharge ?? "unknown";
|
|
62
|
+
this.provider =
|
|
63
|
+
options?.provider === undefined
|
|
64
|
+
? undefined
|
|
65
|
+
: decodeMediaProviderDiagnostic(options.provider);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** A provider safety filter refused generation; no output was delivered. */
|
|
70
|
+
export class MediaSafetyFilterRefusal extends MediaExecutionError {
|
|
71
|
+
constructor(options?: MediaExecutionErrorOptions) {
|
|
72
|
+
super("provider", options);
|
|
73
|
+
this.name = "MediaSafetyFilterRefusal";
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Why the host's billing owner refused to admit a new generation. */
|
|
78
|
+
export type MediaBillingRefusalCode =
|
|
79
|
+
"insufficient_credits" | "denied" | "daily_limit_reached";
|
|
80
|
+
|
|
81
|
+
/** A definitive billing refusal before dispatch; nothing was generated or charged. */
|
|
82
|
+
export class MediaBillingRefusal extends Error {
|
|
83
|
+
constructor(
|
|
84
|
+
/** Bounded refusal or failure reason. */
|
|
85
|
+
readonly code: MediaBillingRefusalCode,
|
|
86
|
+
) {
|
|
87
|
+
super(`Media billing refused: ${code}`);
|
|
88
|
+
this.name = "MediaBillingRefusal";
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** The durable format pin prevents a mismatched file extension or retained output. */
|
|
93
|
+
export class MediaOutputFormatMismatch extends Error {
|
|
94
|
+
constructor() {
|
|
95
|
+
super("Media provider did not return the requested image format");
|
|
96
|
+
this.name = "MediaOutputFormatMismatch";
|
|
97
|
+
}
|
|
98
|
+
}
|
package/src/failure.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as Redacted from "effect/Redacted";
|
|
2
|
+
|
|
3
|
+
/** Retain the original failure without printing provider or customer data. */
|
|
4
|
+
export function reportCatchFailure(operation: string, cause: unknown): void {
|
|
5
|
+
console.error(
|
|
6
|
+
"Media service operation failed",
|
|
7
|
+
{
|
|
8
|
+
operation,
|
|
9
|
+
errorKind: cause instanceof Error ? "error" : "non-error",
|
|
10
|
+
},
|
|
11
|
+
Redacted.make(cause),
|
|
12
|
+
);
|
|
13
|
+
}
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getGatewayModel,
|
|
3
|
+
getMediaModel,
|
|
4
|
+
mediaModels,
|
|
5
|
+
MediaSettingsError,
|
|
6
|
+
transformMilliunits,
|
|
7
|
+
type MediaCatalogue,
|
|
8
|
+
} from "./model-catalog.js";
|
|
9
|
+
import {
|
|
10
|
+
validateMediaGenerationSettings,
|
|
11
|
+
mediaSettingsInput,
|
|
12
|
+
estimateVideoMilliunits,
|
|
13
|
+
type MediaAudioReferenceBytes,
|
|
14
|
+
type MediaGenerationSettings,
|
|
15
|
+
type MediaReferenceBytes,
|
|
16
|
+
} from "./index.js";
|
|
17
|
+
import type {
|
|
18
|
+
MediaAudioModelId,
|
|
19
|
+
MediaBillingUnit,
|
|
20
|
+
MediaVideoModelId,
|
|
21
|
+
} from "./model-catalog.js";
|
|
22
|
+
import { mediaBillingUnit } from "./model-catalog.js";
|
|
23
|
+
import { AUDIO_CHARACTER_BLOCK } from "./model-catalog.js";
|
|
24
|
+
import {
|
|
25
|
+
buildElevenLabsInput,
|
|
26
|
+
estimateElevenLabsMilliunits,
|
|
27
|
+
} from "./audio-providers/elevenlabs.js";
|
|
28
|
+
import { imageDataUri } from "./video-providers/reference-encoding.js";
|
|
29
|
+
import {
|
|
30
|
+
videoProviderAdapters,
|
|
31
|
+
type CheckedVideoSettings,
|
|
32
|
+
} from "./video-providers/index.js";
|
|
33
|
+
|
|
34
|
+
/** Gateway images and the verified catalog's video and audio families share Unified billing. */
|
|
35
|
+
export type GatewayModelId =
|
|
36
|
+
| "openai/gpt-image-2"
|
|
37
|
+
| "bytedance/seedream-5-pro"
|
|
38
|
+
| "bria/remove-background"
|
|
39
|
+
| "pruna/p-image-upscale"
|
|
40
|
+
| MediaVideoModelId
|
|
41
|
+
| MediaAudioModelId;
|
|
42
|
+
export const isGatewayModel = (id: string): id is GatewayModelId => {
|
|
43
|
+
if (
|
|
44
|
+
id === "openai/gpt-image-2" ||
|
|
45
|
+
id === "bytedance/seedream-5-pro" ||
|
|
46
|
+
id === "bria/remove-background" ||
|
|
47
|
+
id === "pruna/p-image-upscale"
|
|
48
|
+
)
|
|
49
|
+
return true;
|
|
50
|
+
const kind = getGatewayModel(id)?.kind;
|
|
51
|
+
return kind === "video" || kind === "audio";
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** What one generation bills, in the unit its catalog entry names. */
|
|
55
|
+
export type MediaUnits = Readonly<{
|
|
56
|
+
/** The model's `billingUnit`. */
|
|
57
|
+
unit: MediaBillingUnit;
|
|
58
|
+
/** Number of delivered or admitted units. */
|
|
59
|
+
units: number;
|
|
60
|
+
}>;
|
|
61
|
+
|
|
62
|
+
/** Re-run the settings grammar over already-normalized settings. */
|
|
63
|
+
const revalidate = (
|
|
64
|
+
settings: MediaGenerationSettings,
|
|
65
|
+
catalogue: MediaCatalogue = mediaModels,
|
|
66
|
+
) => validateMediaGenerationSettings(mediaSettingsInput(settings), catalogue);
|
|
67
|
+
|
|
68
|
+
/** Billing units for validated settings; video bills its requested length. */
|
|
69
|
+
export function mediaUnits(
|
|
70
|
+
settings: MediaGenerationSettings,
|
|
71
|
+
catalogue: MediaCatalogue = mediaModels,
|
|
72
|
+
): MediaUnits {
|
|
73
|
+
const model = getMediaModel(settings.model, catalogue);
|
|
74
|
+
if (model === undefined)
|
|
75
|
+
throw new MediaSettingsError("billing units require a known Media model");
|
|
76
|
+
const unit = mediaBillingUnit(model);
|
|
77
|
+
if (unit === "image") return { unit, units: 1 };
|
|
78
|
+
const checked = revalidate(settings, catalogue);
|
|
79
|
+
if (unit === "audio_character_block")
|
|
80
|
+
return {
|
|
81
|
+
unit,
|
|
82
|
+
units: Math.ceil(checked.prompt.length / AUDIO_CHARACTER_BLOCK),
|
|
83
|
+
};
|
|
84
|
+
if (checked.durationSeconds === undefined)
|
|
85
|
+
throw new MediaSettingsError(
|
|
86
|
+
unit === "video_second"
|
|
87
|
+
? "video settings need a duration"
|
|
88
|
+
: "music settings need a length",
|
|
89
|
+
);
|
|
90
|
+
return { unit, units: checked.durationSeconds };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* GPT Image 2 output tokens per image at `medium` quality. OpenAI publishes
|
|
95
|
+
* this table for its gpt-image models; PRICE DECISION PENDING (Levi): confirm
|
|
96
|
+
* against the first live gateway logs, which carry the real token counts.
|
|
97
|
+
* Any other size is billed in proportion to its pixels at the 1024x1024 rate
|
|
98
|
+
* (1,056 tokens per 1,048,576 pixels, which reproduces the table's 1024x1536
|
|
99
|
+
* row exactly and bounds its 1536x1024 row), rounded up; OpenAI publishes no
|
|
100
|
+
* table for the larger sizes.
|
|
101
|
+
*/
|
|
102
|
+
const gptImageOutputTokens = new Map([
|
|
103
|
+
["1024x1024", 1_056],
|
|
104
|
+
["1024x1536", 1_584],
|
|
105
|
+
["1536x1024", 1_568],
|
|
106
|
+
]);
|
|
107
|
+
const gptImageTokensPerMegapixel = 1_056 / 1_048_576;
|
|
108
|
+
function gptImageOutputTokenCount(settings: MediaGenerationSettings): number {
|
|
109
|
+
const { width, height } = settings;
|
|
110
|
+
if (width === undefined || height === undefined)
|
|
111
|
+
throw new MediaSettingsError("an image needs a width and a height");
|
|
112
|
+
const known = gptImageOutputTokens.get(sizeOf(settings));
|
|
113
|
+
return known ?? Math.ceil(width * height * gptImageTokensPerMegapixel);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* USD per million tokens: text input, image input and image output, as
|
|
117
|
+
* Cloudflare publishes them for this exact model.
|
|
118
|
+
* https://developers.cloudflare.com/ai/models/openai/gpt-image-2/
|
|
119
|
+
*/
|
|
120
|
+
const gptImageRates = { input: 5, inputImage: 8, outputImage: 30 } as const;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Upper bound on the image input tokens one reference adds. OpenAI publishes
|
|
124
|
+
* no input-token rule for gpt-image-2, which always reads inputs at high
|
|
125
|
+
* fidelity, so this applies the GPT Image high-fidelity rule OpenAI does
|
|
126
|
+
* publish (images-vision guide, GPT Image 1): fit within 2048 px, cap the
|
|
127
|
+
* short side at 512 px, 65 base + 129 per 512 px tile, plus 4,160 extra
|
|
128
|
+
* tokens for a square image or 6,240 for any other aspect.
|
|
129
|
+
* https://developers.openai.com/api/docs/guides/images-vision
|
|
130
|
+
* Community measurements put gpt-image-2 at most 1,536 image tokens per
|
|
131
|
+
* input, so this bound over-bills rather than under-bills; lower it only
|
|
132
|
+
* against real gateway input-token counts.
|
|
133
|
+
*/
|
|
134
|
+
function gptImageReferenceTokens(
|
|
135
|
+
reference: Readonly<{ width: number; height: number }>,
|
|
136
|
+
): number {
|
|
137
|
+
const fit = Math.min(1, 2048 / Math.max(reference.width, reference.height));
|
|
138
|
+
const short = Math.min(reference.width, reference.height) * fit;
|
|
139
|
+
const scale = fit * Math.min(1, 512 / short);
|
|
140
|
+
const tiles =
|
|
141
|
+
Math.ceil((reference.width * scale) / 512) *
|
|
142
|
+
Math.ceil((reference.height * scale) / 512);
|
|
143
|
+
const aspect = reference.width === reference.height ? 4_160 : 6_240;
|
|
144
|
+
return 65 + 129 * tiles + aspect;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const sizeOf = (settings: MediaGenerationSettings) =>
|
|
148
|
+
`${String(settings.width)}x${String(settings.height)}`;
|
|
149
|
+
|
|
150
|
+
/** Milliunits (1 cent = 1,000) of provider cost, rounded up. */
|
|
151
|
+
export function estimateGatewayMilliunits(
|
|
152
|
+
settings: MediaGenerationSettings,
|
|
153
|
+
references: readonly Readonly<{ width: number; height: number }>[],
|
|
154
|
+
): number {
|
|
155
|
+
if (settings.model === "openai/gpt-image-2") {
|
|
156
|
+
const output = gptImageOutputTokenCount(settings);
|
|
157
|
+
if (references.length !== (settings.references?.length ?? 0))
|
|
158
|
+
throw new MediaSettingsError(
|
|
159
|
+
"resolved references must match every admitted reference",
|
|
160
|
+
);
|
|
161
|
+
// A bound on prompt tokens: at most one token per character, plus one
|
|
162
|
+
// text token OpenAI is observed to add per input image.
|
|
163
|
+
const input = settings.prompt.length + references.length;
|
|
164
|
+
const inputImage = references.reduce(
|
|
165
|
+
(sum, reference) => sum + gptImageReferenceTokens(reference),
|
|
166
|
+
0,
|
|
167
|
+
);
|
|
168
|
+
// tokens * USD/M * 100,000 milliunits/USD / 1,000,000 = tokens * rate / 10
|
|
169
|
+
return Math.ceil(
|
|
170
|
+
(output * gptImageRates.outputImage +
|
|
171
|
+
input * gptImageRates.input +
|
|
172
|
+
inputImage * gptImageRates.inputImage) /
|
|
173
|
+
10,
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
// Cloudflare prices Seedream 5 Pro at $0.045 per image, and $0.03 per input
|
|
177
|
+
// image (none are sent); it states no price by size.
|
|
178
|
+
// https://developers.cloudflare.com/ai/models/bytedance/seedream-5-pro/
|
|
179
|
+
if (settings.model === "bytedance/seedream-5-pro") return 4_500;
|
|
180
|
+
const model = getGatewayModel(settings.model);
|
|
181
|
+
if (model?.transform != null) {
|
|
182
|
+
// Published per-image prices, held in the catalog's transform facts.
|
|
183
|
+
const price = transformMilliunits(
|
|
184
|
+
model.transform,
|
|
185
|
+
settings.targetMegapixels,
|
|
186
|
+
);
|
|
187
|
+
if (price === undefined)
|
|
188
|
+
throw new MediaSettingsError(
|
|
189
|
+
`${model.id} has no price for this output size`,
|
|
190
|
+
);
|
|
191
|
+
return price;
|
|
192
|
+
}
|
|
193
|
+
if (model?.kind === "video") return estimateVideoMilliunits(settings);
|
|
194
|
+
if (model?.kind === "audio")
|
|
195
|
+
return estimateElevenLabsMilliunits(revalidate(settings), model);
|
|
196
|
+
throw new MediaSettingsError(`${settings.model} is not a gateway model`);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* One `images[]` entry for `openai/gpt-image-2`: a base64 data URI of a png,
|
|
201
|
+
* jpeg or webp image (Cloudflare's published input schema).
|
|
202
|
+
* https://developers.cloudflare.com/ai/models/openai/gpt-image-2/
|
|
203
|
+
*/
|
|
204
|
+
function gptImageReference(reference: MediaReferenceBytes): string {
|
|
205
|
+
if (reference.format === "gif")
|
|
206
|
+
throw new MediaSettingsError(
|
|
207
|
+
"openai/gpt-image-2 accepts png, jpeg or webp reference images, not gif",
|
|
208
|
+
);
|
|
209
|
+
return imageDataUri(reference);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** The provider JSON body for one validated gateway generation. */
|
|
213
|
+
export function gatewayInput(
|
|
214
|
+
settings: MediaGenerationSettings,
|
|
215
|
+
references: readonly MediaReferenceBytes[] = [],
|
|
216
|
+
outputFormat: "png" | "jpeg" | "webp" = "png",
|
|
217
|
+
audioReferences: readonly MediaAudioReferenceBytes[] = [],
|
|
218
|
+
) {
|
|
219
|
+
if (settings.model === "openai/gpt-image-2") {
|
|
220
|
+
const body = {
|
|
221
|
+
prompt: settings.prompt,
|
|
222
|
+
size: sizeOf(settings),
|
|
223
|
+
quality: "medium",
|
|
224
|
+
output_format: outputFormat,
|
|
225
|
+
};
|
|
226
|
+
if (references.length !== (settings.references?.length ?? 0))
|
|
227
|
+
throw new MediaSettingsError(
|
|
228
|
+
"resolved references must match every admitted reference",
|
|
229
|
+
);
|
|
230
|
+
if (references.length === 0) return body;
|
|
231
|
+
return { ...body, images: references.map(gptImageReference) };
|
|
232
|
+
}
|
|
233
|
+
if (settings.model === "bytedance/seedream-5-pro") {
|
|
234
|
+
if (references.length > 0)
|
|
235
|
+
throw new MediaSettingsError(
|
|
236
|
+
"bytedance/seedream-5-pro takes no reference images here",
|
|
237
|
+
);
|
|
238
|
+
// `watermark` is a required field; false adds none.
|
|
239
|
+
return {
|
|
240
|
+
prompt: settings.prompt,
|
|
241
|
+
size: sizeOf(settings),
|
|
242
|
+
watermark: false,
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
if (
|
|
246
|
+
settings.model === "bria/remove-background" ||
|
|
247
|
+
settings.model === "pruna/p-image-upscale"
|
|
248
|
+
) {
|
|
249
|
+
const [source, ...extra] = references;
|
|
250
|
+
if (source === undefined || extra.length > 0)
|
|
251
|
+
throw new MediaSettingsError(`${settings.model} takes one source image`);
|
|
252
|
+
if (source.format !== "png" && source.format !== "jpeg")
|
|
253
|
+
throw new MediaSettingsError(
|
|
254
|
+
`${settings.model} accepts png or jpeg source images`,
|
|
255
|
+
);
|
|
256
|
+
// Always the bytes as a data URI, never a URL to our storage.
|
|
257
|
+
// https://developers.cloudflare.com/ai/models/bria/remove-background/
|
|
258
|
+
const image = imageDataUri(source);
|
|
259
|
+
if (settings.model === "bria/remove-background")
|
|
260
|
+
return { image, preserve_alpha: true };
|
|
261
|
+
// Every field is marked required on the model page; the values are its
|
|
262
|
+
// defaults except the output encoding (see the catalog entry).
|
|
263
|
+
// https://developers.cloudflare.com/ai/models/pruna/p-image-upscale/
|
|
264
|
+
return {
|
|
265
|
+
image,
|
|
266
|
+
target: settings.targetMegapixels ?? 4,
|
|
267
|
+
output_format: "webp",
|
|
268
|
+
output_quality: 90,
|
|
269
|
+
enhance_details: false,
|
|
270
|
+
enhance_realism: false,
|
|
271
|
+
disable_safety_checker: false,
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
const checked = revalidate(settings);
|
|
275
|
+
const model = getGatewayModel(checked.model);
|
|
276
|
+
if (model?.audio != null) return buildElevenLabsInput(checked, model);
|
|
277
|
+
if (
|
|
278
|
+
model?.video == null ||
|
|
279
|
+
checked.resolution === undefined ||
|
|
280
|
+
checked.aspectRatio === undefined ||
|
|
281
|
+
checked.durationSeconds === undefined
|
|
282
|
+
)
|
|
283
|
+
throw new MediaSettingsError("not a video gateway model");
|
|
284
|
+
if (references.length !== (checked.references?.length ?? 0))
|
|
285
|
+
throw new MediaSettingsError(
|
|
286
|
+
"resolved video references must match every admitted reference",
|
|
287
|
+
);
|
|
288
|
+
if (audioReferences.length !== (checked.audioReferences?.length ?? 0))
|
|
289
|
+
throw new MediaSettingsError(
|
|
290
|
+
"resolved audio references must match every admitted audio reference",
|
|
291
|
+
);
|
|
292
|
+
const videoSettings: CheckedVideoSettings = {
|
|
293
|
+
...checked,
|
|
294
|
+
resolution: checked.resolution,
|
|
295
|
+
aspectRatio: checked.aspectRatio,
|
|
296
|
+
durationSeconds: checked.durationSeconds,
|
|
297
|
+
};
|
|
298
|
+
return videoProviderAdapters[model.video.provider].buildInput(
|
|
299
|
+
videoSettings,
|
|
300
|
+
references,
|
|
301
|
+
model,
|
|
302
|
+
audioReferences,
|
|
303
|
+
);
|
|
304
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
/** A result URL every gateway provider must return as plain HTTPS. */
|
|
4
|
+
export const resultUrl = z.lazy(() =>
|
|
5
|
+
z
|
|
6
|
+
.url()
|
|
7
|
+
.refine(
|
|
8
|
+
(value) => value.startsWith("https://"),
|
|
9
|
+
"result URLs must be HTTPS",
|
|
10
|
+
),
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
/** The Unified AI gateway's normalized answer: a completed state and one
|
|
14
|
+
* result URL. Seedance and Veo answer this way; MiniMax does not. */
|
|
15
|
+
export const gatewayOutputSchema = z.lazy(() =>
|
|
16
|
+
z.looseObject({
|
|
17
|
+
state: z.literal("Completed").optional(),
|
|
18
|
+
result: z.looseObject({
|
|
19
|
+
image: resultUrl.optional(),
|
|
20
|
+
/** Seedream answers with a list of result URLs. */
|
|
21
|
+
images: z.array(resultUrl).min(1).optional(),
|
|
22
|
+
video: resultUrl.optional(),
|
|
23
|
+
}),
|
|
24
|
+
}),
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
/** Byte ceilings for fetched results; video stays within Worker memory. */
|
|
28
|
+
export const GATEWAY_VIDEO_MAX_BYTES = 64 * 1024 * 1024;
|
|
29
|
+
|
|
30
|
+
/** An MP4 carries an `ftyp` box at byte 4. */
|
|
31
|
+
export const isMp4 = (bytes: Uint8Array): boolean =>
|
|
32
|
+
bytes.byteLength > 12 &&
|
|
33
|
+
bytes[4] === 0x66 &&
|
|
34
|
+
bytes[5] === 0x74 &&
|
|
35
|
+
bytes[6] === 0x79 &&
|
|
36
|
+
bytes[7] === 0x70;
|