@fungi.computer/darkroom 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +178 -0
- package/dist/app-media-contracts.d.ts +107 -12
- package/dist/audio-providers/elevenlabs.d.ts +40 -0
- package/dist/audio-providers/elevenlabs.js +69 -0
- package/dist/audio-reference-types.d.ts +28 -0
- package/dist/audio-reference-types.js +16 -0
- package/dist/audio-references.d.ts +14 -0
- package/dist/audio-references.js +69 -0
- package/dist/browser-failure.d.ts +2 -0
- package/dist/browser-failure.js +2 -0
- package/dist/cloudflare/provider.d.ts +6 -4
- package/dist/cloudflare/provider.js +39 -21
- package/dist/cloudflare/reference-images.d.ts +6 -0
- package/dist/cloudflare/reference-images.js +25 -7
- package/dist/cost-estimate.js +2 -2
- package/dist/daily-media-meter.d.ts +55 -0
- package/dist/daily-media-meter.js +81 -0
- package/dist/execution-error.d.ts +1 -1
- package/dist/gateway-models.d.ts +19 -9
- package/dist/gateway-models.js +57 -19
- package/dist/gateway-wire.d.ts +1 -0
- package/dist/gateway-wire.js +2 -0
- package/dist/index.d.ts +116 -24
- package/dist/index.js +147 -26
- package/dist/media-binding.d.ts +60 -7
- package/dist/media-binding.js +43 -21
- package/dist/media-generation.js +20 -7
- package/dist/media-operation-contract.d.ts +9 -3
- package/dist/media-operation-contract.js +6 -4
- package/dist/media-operation-do.d.ts +13 -2
- package/dist/media-operation-do.js +26 -1
- package/dist/media-operation-owner.d.ts +10 -2
- package/dist/media-operation-owner.js +56 -11
- package/dist/media-operation.js +5 -3
- package/dist/media-quote.d.ts +14 -0
- package/dist/media-quote.js +17 -0
- package/dist/model-catalog.d.ts +602 -6
- package/dist/model-catalog.js +309 -9
- package/dist/reference-image-size.d.ts +4 -3
- package/dist/reference-image-size.js +9 -9
- package/dist/server.d.ts +31 -8
- package/dist/server.js +51 -8
- package/dist/video-providers/reference-encoding.d.ts +7 -2
- package/dist/video-providers/reference-encoding.js +14 -3
- package/dist/video-providers/types.d.ts +2 -2
- package/dist/video-providers/unified.d.ts +1 -0
- package/dist/video-providers/unified.js +4 -2
- package/docs/api/app-media-contracts.md +50 -10
- package/docs/api/browser.md +4 -2
- package/docs/api/cloudflare.md +56 -3
- package/docs/api/index.md +1042 -121
- package/docs/api/server.md +306 -50
- package/package.json +3 -2
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as z from "zod";
|
|
2
2
|
import type { MediaOperationOwner, MediaOperationCoordinates, MediaOperationRequest } from "./media-operation-owner.js";
|
|
3
3
|
import type { MediaUnits } from "./gateway-models.js";
|
|
4
|
-
import type { MediaReferenceBytes } from "./index.js";
|
|
4
|
+
import type { MediaAudioReferenceBytes, MediaCatalogue, MediaReferenceBytes } from "./index.js";
|
|
5
5
|
/**
|
|
6
6
|
* The host's billing owner for Media. Every call is keyed by the durable
|
|
7
7
|
* operation id and must be idempotent: a lost acknowledgement is retried with
|
|
@@ -52,6 +52,8 @@ export type MediaExecution = Readonly<{
|
|
|
52
52
|
requestId: string;
|
|
53
53
|
/** Captured generation and fixed destination. */
|
|
54
54
|
request: MediaOperationRequest;
|
|
55
|
+
/** The active provider's model list; omitted means the Fungi gateway's. */
|
|
56
|
+
catalogue?: MediaCatalogue | undefined;
|
|
55
57
|
/** The billing owner, or `unmetered` when the host pays its own provider. */
|
|
56
58
|
metering: MediaMetering | "unmetered";
|
|
57
59
|
/** Optional cancellation signal. */
|
|
@@ -71,6 +73,8 @@ export type MediaExecution = Readonly<{
|
|
|
71
73
|
costMilliunits: number;
|
|
72
74
|
/** Host-authorized reference images. */
|
|
73
75
|
references: readonly MediaReferenceBytes[];
|
|
76
|
+
/** Host-authorized reference audio clips, parsed and within the model's budget. */
|
|
77
|
+
audioReferences: readonly MediaAudioReferenceBytes[];
|
|
74
78
|
/** Dispatch one provider call with the prepared inputs. */
|
|
75
79
|
run: (signal?: AbortSignal) => Promise<Readonly<{
|
|
76
80
|
/** Generated output bytes. */
|
|
@@ -97,8 +101,10 @@ export declare const mediaResultSchema: z.ZodObject<{
|
|
|
97
101
|
readonly webp: "webp";
|
|
98
102
|
/** MP4 video encoding. */
|
|
99
103
|
readonly mp4: "mp4";
|
|
104
|
+
/** MP3 audio encoding. */
|
|
105
|
+
readonly mp3: "mp3";
|
|
100
106
|
}>;
|
|
101
|
-
height: z.ZodNumber
|
|
107
|
+
height: z.ZodOptional<z.ZodNumber>;
|
|
102
108
|
model: z.ZodString;
|
|
103
|
-
width: z.ZodNumber
|
|
109
|
+
width: z.ZodOptional<z.ZodNumber>;
|
|
104
110
|
}, z.core.$strict>;
|
|
@@ -19,11 +19,13 @@ export const mediaResultSchema = z.strictObject({
|
|
|
19
19
|
webp: "webp",
|
|
20
20
|
/** MP4 video encoding. */
|
|
21
21
|
mp4: "mp4",
|
|
22
|
+
/** MP3 audio encoding. */
|
|
23
|
+
mp3: "mp3",
|
|
22
24
|
}),
|
|
23
|
-
/** Requested image height or nominal video selector, not decoded MP4 dimensions. */
|
|
24
|
-
height: z.number().int().positive(),
|
|
25
|
+
/** Requested image height or nominal video selector, not decoded MP4 dimensions; absent for audio. */
|
|
26
|
+
height: z.number().int().positive().optional(),
|
|
25
27
|
/** Provider catalog model identifier. */
|
|
26
28
|
model: z.string().min(1).max(256),
|
|
27
|
-
/** Requested image width or nominal video selector, not decoded MP4 dimensions. */
|
|
28
|
-
width: z.number().int().positive(),
|
|
29
|
+
/** Requested image width or nominal video selector, not decoded MP4 dimensions; absent for audio. */
|
|
30
|
+
width: z.number().int().positive().optional(),
|
|
29
31
|
});
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { DurableObject } from "cloudflare:workers";
|
|
2
2
|
import { type MediaAiPort } from "./cloudflare/provider.js";
|
|
3
|
-
import type
|
|
3
|
+
import { type MediaCatalogue, type MediaProvider } from "./index.js";
|
|
4
4
|
import { MediaOperationOwner, type MediaOperationEnvironment } from "./media-operation-owner.js";
|
|
5
|
+
import { DailyMediaMeter, type DailyMediaMeterPort } from "./daily-media-meter.js";
|
|
5
6
|
export type { MediaOperationRequest, MediaOperationCoordinates, MediaOperationEnvironment, } from "./media-operation-owner.js";
|
|
6
7
|
/** Retention storage and native background provider bindings for the operation DO. */
|
|
7
8
|
export type MediaOperationCloudflareEnvironment = MediaOperationEnvironment & Readonly<{
|
|
@@ -11,13 +12,23 @@ export type MediaOperationCloudflareEnvironment = MediaOperationEnvironment & Re
|
|
|
11
12
|
AI_GATEWAY_ID?: string;
|
|
12
13
|
}>;
|
|
13
14
|
/** Cloudflare lifecycle and RPC surface around the durable media owner. */
|
|
14
|
-
export declare class MediaOperationDO extends DurableObject<MediaOperationCloudflareEnvironment> {
|
|
15
|
+
export declare class MediaOperationDO extends DurableObject<MediaOperationCloudflareEnvironment> implements DailyMediaMeterPort {
|
|
15
16
|
protected readonly owner: MediaOperationOwner;
|
|
17
|
+
private readonly dailyMeter;
|
|
16
18
|
constructor(ctx: DurableObjectState, env: MediaOperationCloudflareEnvironment);
|
|
17
19
|
/** A host may pin the namespace through its own local lifecycle facts. */
|
|
18
20
|
protected retainedNamespace(): string | null;
|
|
21
|
+
/**
|
|
22
|
+
* A host that overrides `mediaProvider` also overrides this with that
|
|
23
|
+
* provider's catalogue; the default is the Fungi gateway's.
|
|
24
|
+
*/
|
|
25
|
+
protected mediaCatalogue(): MediaCatalogue;
|
|
19
26
|
/** Hosts may supply their own provider for background generation. */
|
|
20
27
|
protected mediaProvider(): MediaProvider | undefined;
|
|
28
|
+
/** Admit one generation against today's hard stop; false means the daily limit is reached. */
|
|
29
|
+
dailyAdmit(...input: Parameters<DailyMediaMeter["admit"]>): boolean;
|
|
30
|
+
/** Settle an admitted generation once its outcome is known. */
|
|
31
|
+
dailySettle(...input: Parameters<DailyMediaMeter["settle"]>): void;
|
|
21
32
|
/** {@inheritDoc MediaOperationOwner.read} */
|
|
22
33
|
read(...input: Parameters<MediaOperationOwner["read"]>): ReturnType<MediaOperationOwner["read"]>;
|
|
23
34
|
/** {@inheritDoc MediaOperationOwner.readShare} */
|
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
import { DurableObject } from "cloudflare:workers";
|
|
2
2
|
import { createWorkersAiMediaProvider, } from "./cloudflare/provider.js";
|
|
3
|
+
import { mediaModels, } from "./index.js";
|
|
3
4
|
import { MediaOperationOwner, } from "./media-operation-owner.js";
|
|
5
|
+
import { DailyMediaMeter, } from "./daily-media-meter.js";
|
|
4
6
|
/** Cloudflare lifecycle and RPC surface around the durable media owner. */
|
|
5
7
|
export class MediaOperationDO extends DurableObject {
|
|
6
8
|
owner;
|
|
9
|
+
dailyMeter;
|
|
7
10
|
constructor(ctx, env) {
|
|
8
11
|
super(ctx, env);
|
|
12
|
+
this.dailyMeter = new DailyMediaMeter({
|
|
13
|
+
sql: ctx.storage.sql,
|
|
14
|
+
transactionSync: (body) => ctx.storage.transactionSync(body),
|
|
15
|
+
});
|
|
9
16
|
this.owner = new MediaOperationOwner({
|
|
10
17
|
id: { name: ctx.id.name },
|
|
11
18
|
blockConcurrencyWhile: (body) => ctx.blockConcurrencyWhile(body),
|
|
@@ -20,12 +27,22 @@ export class MediaOperationDO extends DurableObject {
|
|
|
20
27
|
getAlarm: () => ctx.storage.getAlarm(),
|
|
21
28
|
setAlarm: (timestamp) => ctx.storage.setAlarm(timestamp),
|
|
22
29
|
},
|
|
23
|
-
}, env, () => this.mediaProvider(), {
|
|
30
|
+
}, env, () => this.mediaProvider(), {
|
|
31
|
+
namespace: () => this.retainedNamespace(),
|
|
32
|
+
catalogue: () => this.mediaCatalogue(),
|
|
33
|
+
});
|
|
24
34
|
}
|
|
25
35
|
/** A host may pin the namespace through its own local lifecycle facts. */
|
|
26
36
|
retainedNamespace() {
|
|
27
37
|
return null;
|
|
28
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* A host that overrides `mediaProvider` also overrides this with that
|
|
41
|
+
* provider's catalogue; the default is the Fungi gateway's.
|
|
42
|
+
*/
|
|
43
|
+
mediaCatalogue() {
|
|
44
|
+
return mediaModels;
|
|
45
|
+
}
|
|
29
46
|
/** Hosts may supply their own provider for background generation. */
|
|
30
47
|
mediaProvider() {
|
|
31
48
|
if (this.env.AI === undefined)
|
|
@@ -34,6 +51,14 @@ export class MediaOperationDO extends DurableObject {
|
|
|
34
51
|
? undefined
|
|
35
52
|
: { gateway: { id: this.env.AI_GATEWAY_ID } });
|
|
36
53
|
}
|
|
54
|
+
/** Admit one generation against today's hard stop; false means the daily limit is reached. */
|
|
55
|
+
dailyAdmit(...input) {
|
|
56
|
+
return this.dailyMeter.admit(...input);
|
|
57
|
+
}
|
|
58
|
+
/** Settle an admitted generation once its outcome is known. */
|
|
59
|
+
dailySettle(...input) {
|
|
60
|
+
this.dailyMeter.settle(...input);
|
|
61
|
+
}
|
|
37
62
|
/** {@inheritDoc MediaOperationOwner.read} */
|
|
38
63
|
read(...input) {
|
|
39
64
|
return this.owner.read(...input);
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { type MediaCatalogue } from "./model-catalog.js";
|
|
1
2
|
import { type MediaGenerationSettings } from "./index.js";
|
|
2
3
|
import { z } from "zod";
|
|
3
4
|
import type { MediaOperationContext, MediaOperationBucket } from "./media-operation-storage.js";
|
|
4
|
-
import type { MediaProvider, MediaReferenceBytes } from "./index.js";
|
|
5
|
+
import type { MediaAudioReferenceBytes, MediaProvider, MediaReferenceBytes } from "./index.js";
|
|
5
6
|
import { type MediaFailureStage } from "./execution-error.js";
|
|
6
7
|
import { type MediaProviderDiagnostic } from "@fungi.computer/agar/errors";
|
|
7
8
|
/** Fixed generation destination captured before provider dispatch. */
|
|
@@ -153,6 +154,12 @@ export type MediaRetentionResult = MediaOperationProjection | Readonly<{
|
|
|
153
154
|
export type MediaOperationHooks = Readonly<{
|
|
154
155
|
/** Read the namespace already owned by host-local facts, or null when unbound. */
|
|
155
156
|
namespace?: () => string | null;
|
|
157
|
+
/**
|
|
158
|
+
* The model list of the provider this owner dispatches to. Omitted means the
|
|
159
|
+
* Fungi gateway's; a host that injects its own provider supplies that
|
|
160
|
+
* provider's catalogue so stored requests and alarms validate against it.
|
|
161
|
+
*/
|
|
162
|
+
catalogue?: () => MediaCatalogue;
|
|
156
163
|
}>;
|
|
157
164
|
/** Retained-output storage supplied by the host. */
|
|
158
165
|
export type MediaOperationEnvironment = Readonly<{
|
|
@@ -177,6 +184,7 @@ export declare class MediaOperationOwner {
|
|
|
177
184
|
* The host supplies the provider for background alarm generations.
|
|
178
185
|
*/
|
|
179
186
|
protected mediaProvider(): MediaProvider | undefined;
|
|
187
|
+
private catalogue;
|
|
180
188
|
constructor(ctx: MediaOperationContext, env: MediaOperationEnvironment, provider?: (() => MediaProvider | undefined) | undefined, hooks?: MediaOperationHooks);
|
|
181
189
|
private ensureSchema;
|
|
182
190
|
private decodeJson;
|
|
@@ -248,7 +256,7 @@ export declare class MediaOperationOwner {
|
|
|
248
256
|
* returns at once; the alarm dispatches the provider exactly once, retains
|
|
249
257
|
* the output, and releases the lease so a later retry saves and charges it.
|
|
250
258
|
*/
|
|
251
|
-
dispatchInBackground(leaseId: string, references: readonly MediaReferenceBytes[]): Promise<MediaOperationProjection>;
|
|
259
|
+
dispatchInBackground(leaseId: string, references: readonly MediaReferenceBytes[], audioReferences: readonly MediaAudioReferenceBytes[]): Promise<MediaOperationProjection>;
|
|
252
260
|
/** Run a background generation once; a retried alarm never redispatches. */
|
|
253
261
|
alarm(): Promise<void>;
|
|
254
262
|
/** An aborted in-flight provider call is terminal for this request ID. */
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { mediaRequestedImageFormatSchema } from "./model-catalog.js";
|
|
1
|
+
import { mediaModels, mediaRequestedImageFormatSchema, } from "./model-catalog.js";
|
|
2
2
|
import { defineIdFamily } from "@fungi.computer/cairn/id";
|
|
3
3
|
import { mediaGenerationSettingsSchema, validateMediaGenerationSettings, } from "./index.js";
|
|
4
4
|
import { z } from "zod";
|
|
@@ -23,6 +23,11 @@ const referenceMetadataSchema = z.lazy(() => z.object({
|
|
|
23
23
|
height: z.coerce.number().int().positive(),
|
|
24
24
|
}));
|
|
25
25
|
const referenceKey = (operationId, index) => `media-reference/v1/${operationId}/${String(index)}`;
|
|
26
|
+
const audioReferenceMetadataSchema = z.lazy(() => z.object({
|
|
27
|
+
mimeType: z.enum(["audio/mpeg", "audio/mp4", "audio/wav", "audio/ogg"]),
|
|
28
|
+
durationSeconds: z.coerce.number().positive(),
|
|
29
|
+
}));
|
|
30
|
+
const audioReferenceKey = (operationId, index) => `media-audio-reference/v1/${operationId}/${String(index)}`;
|
|
26
31
|
/** Fixed generation destination captured before provider dispatch. */
|
|
27
32
|
export const mediaOperationDestinationSchema = z.lazy(() => z.strictObject({
|
|
28
33
|
/** Host-admitted destination or reference identity. */
|
|
@@ -66,13 +71,21 @@ export const mediaImageSchema = z.lazy(() => z.strictObject({
|
|
|
66
71
|
format: text,
|
|
67
72
|
}));
|
|
68
73
|
const retainedFormatSchema = z.lazy(() => z
|
|
69
|
-
.enum([
|
|
74
|
+
.enum([
|
|
75
|
+
"image/png",
|
|
76
|
+
"image/jpeg",
|
|
77
|
+
"image/gif",
|
|
78
|
+
"image/webp",
|
|
79
|
+
"video/mp4",
|
|
80
|
+
"audio/mpeg",
|
|
81
|
+
])
|
|
70
82
|
.transform((mime) => ({
|
|
71
83
|
"image/png": "png",
|
|
72
84
|
"image/jpeg": "jpeg",
|
|
73
85
|
"image/gif": "gif",
|
|
74
86
|
"image/webp": "webp",
|
|
75
87
|
"video/mp4": "mp4",
|
|
88
|
+
"audio/mpeg": "mp3",
|
|
76
89
|
})[mime]));
|
|
77
90
|
const outputSchema = z.lazy(() => z.strictObject({
|
|
78
91
|
key: text,
|
|
@@ -102,7 +115,11 @@ class MediaOperationError extends Error {
|
|
|
102
115
|
}
|
|
103
116
|
}
|
|
104
117
|
/** Stored media type for an output format. */
|
|
105
|
-
const outputMime = (format) => format === "mp4"
|
|
118
|
+
const outputMime = (format) => format === "mp4"
|
|
119
|
+
? "video/mp4"
|
|
120
|
+
: format === "mp3"
|
|
121
|
+
? "audio/mpeg"
|
|
122
|
+
: `image/${format}`;
|
|
106
123
|
const operationRowSchema = z.lazy(() => z.object({
|
|
107
124
|
operation_id: z.string(),
|
|
108
125
|
request_id: z.string(),
|
|
@@ -154,7 +171,7 @@ function fingerprint(input) {
|
|
|
154
171
|
return JSON.stringify(input);
|
|
155
172
|
}
|
|
156
173
|
const objectSchema = z.lazy(() => z.record(z.string(), z.unknown()));
|
|
157
|
-
function decodeStoredRequest(serialized) {
|
|
174
|
+
function decodeStoredRequest(serialized, catalogue) {
|
|
158
175
|
let raw;
|
|
159
176
|
try {
|
|
160
177
|
raw = JSON.parse(serialized);
|
|
@@ -181,7 +198,7 @@ function decodeStoredRequest(serialized) {
|
|
|
181
198
|
throw new MediaOperationError("partial_init");
|
|
182
199
|
let settings;
|
|
183
200
|
try {
|
|
184
|
-
settings = validateMediaGenerationSettings(parsedGeneration.data);
|
|
201
|
+
settings = validateMediaGenerationSettings(parsedGeneration.data, catalogue);
|
|
185
202
|
}
|
|
186
203
|
catch (cause) {
|
|
187
204
|
throw new MediaOperationError("partial_init", { cause });
|
|
@@ -213,6 +230,9 @@ export class MediaOperationOwner {
|
|
|
213
230
|
mediaProvider() {
|
|
214
231
|
return this.provider?.();
|
|
215
232
|
}
|
|
233
|
+
catalogue() {
|
|
234
|
+
return this.hooks.catalogue?.() ?? mediaModels;
|
|
235
|
+
}
|
|
216
236
|
constructor(ctx, env, provider, hooks = {}) {
|
|
217
237
|
this.ctx = ctx;
|
|
218
238
|
this.env = env;
|
|
@@ -278,7 +298,7 @@ export class MediaOperationOwner {
|
|
|
278
298
|
namespace: coordinates.data.namespace,
|
|
279
299
|
owner: coordinates.data.owner,
|
|
280
300
|
fingerprint: row.fingerprint,
|
|
281
|
-
input: decodeStoredRequest(row.input),
|
|
301
|
+
input: decodeStoredRequest(row.input, this.catalogue()),
|
|
282
302
|
state: row.state,
|
|
283
303
|
leaseId: row.lease_id,
|
|
284
304
|
leaseExpiresAt: row.lease_expires_at,
|
|
@@ -358,7 +378,11 @@ export class MediaOperationOwner {
|
|
|
358
378
|
const record = this.readRecord();
|
|
359
379
|
if (record?.namespace !== namespace ||
|
|
360
380
|
record.state !== "succeeded" ||
|
|
361
|
-
record.output === null
|
|
381
|
+
record.output === null ||
|
|
382
|
+
// Public sharing covers pictures and video; audio has no dimensions to
|
|
383
|
+
// show and no public page yet.
|
|
384
|
+
record.input.width === undefined ||
|
|
385
|
+
record.input.height === undefined)
|
|
362
386
|
return null;
|
|
363
387
|
return Object.freeze({
|
|
364
388
|
requestId: record.requestId,
|
|
@@ -649,7 +673,7 @@ export class MediaOperationOwner {
|
|
|
649
673
|
* returns at once; the alarm dispatches the provider exactly once, retains
|
|
650
674
|
* the output, and releases the lease so a later retry saves and charges it.
|
|
651
675
|
*/
|
|
652
|
-
async dispatchInBackground(leaseId, references) {
|
|
676
|
+
async dispatchInBackground(leaseId, references, audioReferences) {
|
|
653
677
|
const record = this.requireRecord();
|
|
654
678
|
if (record.state !== "generating" || record.leaseId !== leaseId)
|
|
655
679
|
throw new MediaOperationError("lease_lost");
|
|
@@ -662,6 +686,14 @@ export class MediaOperationOwner {
|
|
|
662
686
|
},
|
|
663
687
|
});
|
|
664
688
|
}
|
|
689
|
+
for (const [index, clip] of audioReferences.entries()) {
|
|
690
|
+
await this.env.APP_ARTIFACTS.put(audioReferenceKey(record.operationId, index), clip.bytes, {
|
|
691
|
+
customMetadata: {
|
|
692
|
+
mimeType: clip.mimeType,
|
|
693
|
+
durationSeconds: String(clip.durationSeconds),
|
|
694
|
+
},
|
|
695
|
+
});
|
|
696
|
+
}
|
|
665
697
|
const timestamp = Date.now();
|
|
666
698
|
this.ctx.storage.sql.exec("UPDATE hub_media_operation SET background=1, lease_expires_at=?, updated_at=? WHERE singleton=1 AND state='generating' AND lease_id=?", timestamp + this.backgroundLeaseMs, timestamp, leaseId);
|
|
667
699
|
await this.ctx.storage.setAlarm(timestamp);
|
|
@@ -708,9 +740,19 @@ export class MediaOperationOwner {
|
|
|
708
740
|
bytes: new Uint8Array(await object.arrayBuffer()),
|
|
709
741
|
};
|
|
710
742
|
}));
|
|
743
|
+
const audioReferences = await Promise.all((record.input.audioReferences ?? []).map(async (_reference, index) => {
|
|
744
|
+
const object = await this.env.APP_ARTIFACTS.get(audioReferenceKey(record.operationId, index));
|
|
745
|
+
if (object === null)
|
|
746
|
+
throw new Error("Media audio reference is unavailable");
|
|
747
|
+
const metadata = audioReferenceMetadataSchema.parse(object.customMetadata);
|
|
748
|
+
return {
|
|
749
|
+
...metadata,
|
|
750
|
+
bytes: new Uint8Array(await object.arrayBuffer()),
|
|
751
|
+
};
|
|
752
|
+
}));
|
|
711
753
|
// The captured request includes destination authority; providers receive settings only.
|
|
712
|
-
const settings = validateMediaGenerationSettings(mediaGenerationSettingsSchema.strip().parse(record.input));
|
|
713
|
-
output = await provider(settings, references, undefined, record.input.outputFormat);
|
|
754
|
+
const settings = validateMediaGenerationSettings(mediaGenerationSettingsSchema.strip().parse(record.input), this.catalogue());
|
|
755
|
+
output = await provider(settings, references, undefined, record.input.outputFormat, audioReferences);
|
|
714
756
|
}
|
|
715
757
|
catch (cause) {
|
|
716
758
|
reportCatchFailure("media-operation-provider", cause);
|
|
@@ -733,7 +775,10 @@ export class MediaOperationOwner {
|
|
|
733
775
|
return;
|
|
734
776
|
}
|
|
735
777
|
await this.recordGenerated(leaseId, output.bytes, output.format);
|
|
736
|
-
await Promise.all(
|
|
778
|
+
await Promise.all([
|
|
779
|
+
...(record.input.references ?? []).map((_reference, index) => this.env.APP_ARTIFACTS.delete(referenceKey(record.operationId, index))),
|
|
780
|
+
...(record.input.audioReferences ?? []).map((_reference, index) => this.env.APP_ARTIFACTS.delete(audioReferenceKey(record.operationId, index))),
|
|
781
|
+
]).catch((cause) => {
|
|
737
782
|
reportCatchFailure("media-reference-cleanup", cause);
|
|
738
783
|
});
|
|
739
784
|
// Release the lease: the next retry of this request claims the save.
|
package/dist/media-operation.js
CHANGED
|
@@ -58,8 +58,9 @@ export function executeMediaOperation(input) {
|
|
|
58
58
|
yield* call(input.destination.authorize, "destination-authorization");
|
|
59
59
|
const metering = input.metering === "unmetered" ? null : input.metering;
|
|
60
60
|
const operationId = admission.operation.operationId;
|
|
61
|
-
const billed = mediaUnits(validateMediaGenerationSettings(mediaGenerationSettingsSchema.strip().parse(input.request)));
|
|
62
|
-
|
|
61
|
+
const billed = mediaUnits(validateMediaGenerationSettings(mediaGenerationSettingsSchema.strip().parse(input.request), input.catalogue), input.catalogue);
|
|
62
|
+
// Video and audio run through the durable alarm; only an image is inline.
|
|
63
|
+
const background = getMediaModel(input.request.model, input.catalogue)?.kind !== "image";
|
|
63
64
|
/** Best-effort release; true proves customer settlement, not supplier billing. */
|
|
64
65
|
const settleNothing = () => metering === null
|
|
65
66
|
? Effect.succeed(false)
|
|
@@ -138,7 +139,7 @@ export function executeMediaOperation(input) {
|
|
|
138
139
|
if (background) {
|
|
139
140
|
// Video outlives one request: the owner's alarm generates, and a
|
|
140
141
|
// retry of this request id saves and charges the retained output.
|
|
141
|
-
yield* call(() => input.operation.dispatchInBackground(leaseId, prepared.references), "operation").pipe(Effect.tapError(() => call(() => input.operation.interruptGeneration(leaseId), "operation").pipe(Effect.andThen(settleNothing()), Effect.ignore)));
|
|
142
|
+
yield* call(() => input.operation.dispatchInBackground(leaseId, prepared.references, prepared.audioReferences), "operation").pipe(Effect.tapError(() => call(() => input.operation.interruptGeneration(leaseId), "operation").pipe(Effect.andThen(settleNothing()), Effect.ignore)));
|
|
142
143
|
return { kind: "accepted" };
|
|
143
144
|
}
|
|
144
145
|
const generation = yield* call(async () => {
|
|
@@ -204,6 +205,7 @@ export function executeMediaOperation(input) {
|
|
|
204
205
|
// retry writes byte-identical output. A charged generation is never
|
|
205
206
|
// failed over metadata, so a malformed or truncated provider image is
|
|
206
207
|
// written as-is; embedProvenance never throws.
|
|
208
|
+
// MP3 is not listed: audio carries no embedded provenance, like MP4.
|
|
207
209
|
const format = z
|
|
208
210
|
.enum(["png", "jpeg", "gif", "webp", "mp4"])
|
|
209
211
|
.safeParse(output.format);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type MediaUnits } from "./gateway-models.js";
|
|
2
|
+
import { type MediaGenerationSettingsInput, type MediaReferenceReader } from "./index.js";
|
|
3
|
+
/** The provider cost of one exact generation request, before any billing policy. */
|
|
4
|
+
export type MediaQuote = Readonly<MediaUnits & {
|
|
5
|
+
/** Provider milliunits of a cent, as admission would estimate them. */
|
|
6
|
+
providerCostMilliunits: number;
|
|
7
|
+
}>;
|
|
8
|
+
/**
|
|
9
|
+
* Quote one exact request: the same settings validation, reference reads,
|
|
10
|
+
* estimator and billing units a generation uses at admission, run without
|
|
11
|
+
* dispatching anything. A host applies its billing policy to this per unit,
|
|
12
|
+
* the way its metering does, so the number matches the charge.
|
|
13
|
+
*/
|
|
14
|
+
export declare function quoteMediaGeneration(raw: MediaGenerationSettingsInput, references: MediaReferenceReader): Promise<MediaQuote>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { estimate } from "./cloudflare/provider.js";
|
|
2
|
+
import { mediaUnits } from "./gateway-models.js";
|
|
3
|
+
import { resolveMediaReferences, validateMediaGenerationSettings, } from "./index.js";
|
|
4
|
+
/**
|
|
5
|
+
* Quote one exact request: the same settings validation, reference reads,
|
|
6
|
+
* estimator and billing units a generation uses at admission, run without
|
|
7
|
+
* dispatching anything. A host applies its billing policy to this per unit,
|
|
8
|
+
* the way its metering does, so the number matches the charge.
|
|
9
|
+
*/
|
|
10
|
+
export async function quoteMediaGeneration(raw, references) {
|
|
11
|
+
const settings = validateMediaGenerationSettings(raw);
|
|
12
|
+
const resolved = await resolveMediaReferences(settings, references);
|
|
13
|
+
return {
|
|
14
|
+
...mediaUnits(settings),
|
|
15
|
+
providerCostMilliunits: estimate(settings, resolved),
|
|
16
|
+
};
|
|
17
|
+
}
|