@fungi.computer/darkroom 0.6.0 → 0.7.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 +129 -0
- package/dist/app-media-contracts.d.ts +4 -0
- package/dist/browser-failure.d.ts +2 -0
- package/dist/browser-failure.js +2 -0
- package/dist/cloudflare/provider.js +3 -3
- 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 +8 -2
- package/dist/gateway-models.js +24 -7
- package/dist/gateway-wire.d.ts +1 -0
- package/dist/gateway-wire.js +2 -0
- package/dist/index.d.ts +49 -12
- package/dist/index.js +31 -21
- package/dist/media-binding.js +3 -3
- package/dist/media-generation.js +9 -5
- package/dist/media-operation-contract.d.ts +3 -1
- package/dist/media-operation-do.d.ts +13 -2
- package/dist/media-operation-do.js +26 -1
- package/dist/media-operation-owner.d.ts +8 -0
- package/dist/media-operation-owner.js +8 -5
- package/dist/media-operation.js +2 -2
- package/dist/media-quote.d.ts +14 -0
- package/dist/media-quote.js +17 -0
- package/dist/model-catalog.d.ts +435 -4
- package/dist/model-catalog.js +130 -6
- package/dist/reference-image-size.d.ts +4 -3
- package/dist/reference-image-size.js +9 -9
- package/dist/server.d.ts +17 -1
- package/dist/server.js +44 -6
- package/docs/api/app-media-contracts.md +4 -0
- package/docs/api/browser.md +4 -2
- package/docs/api/cloudflare.md +53 -0
- package/docs/api/index.md +461 -54
- package/docs/api/server.md +194 -5
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as z from "zod";
|
|
2
|
-
import { type MediaVideoReferenceRole, type
|
|
2
|
+
import { type MediaVideoReferenceRole, type MediaCatalogue, type MediaVideoResolution, type MediaVideoAspectRatio } from "./model-catalog.js";
|
|
3
3
|
export { mediaFailureStageSchema } from "./execution-error.js";
|
|
4
|
-
export { MAX_MEDIA_PROMPT_LENGTH, getMediaModel, MAX_REFERENCE_COUNT, listMediaModels, mediaModelInputSchema, mediaModelSchema, mediaRequestedImageFormatSchema, mediaModels, mediaModelTierSchema, MediaSettingsError, mediaVideoModelIdSchema, mediaVideoResolutionSchema, mediaVideoAspectRatioSchema, mediaVideoReferenceRoleSchema, mediaVideoFormatSchema, mediaVideoSchema, } from "./model-catalog.js";
|
|
5
|
-
export type { MediaModel, MediaModelInput, MediaModelTier, MediaVideoModelId, MediaVideoResolution, MediaVideoAspectRatio, MediaVideoReferenceRole, MediaVideoModel, MediaVideoFormat, } from "./model-catalog.js";
|
|
4
|
+
export { MAX_MEDIA_PROMPT_LENGTH, getMediaModel, getGatewayModel, MAX_REFERENCE_COUNT, listMediaModels, mediaCatalogueSchema, mediaCatalogueModelSchema, parseMediaCatalogue, mediaModelInputSchema, mediaModelSchema, mediaRequestedImageFormatSchema, mediaModels, mediaModelTierSchema, MediaSettingsError, mediaVideoModelIdSchema, mediaVideoResolutionSchema, mediaVideoAspectRatioSchema, mediaVideoReferenceRoleSchema, mediaVideoFormatSchema, mediaVideoSchema, } from "./model-catalog.js";
|
|
5
|
+
export type { MediaCatalogue, MediaCatalogueModel, MediaModel, MediaModelInput, MediaModelTier, MediaVideoModelId, MediaVideoResolution, MediaVideoAspectRatio, MediaVideoReferenceRole, MediaVideoModel, MediaVideoFormat, } from "./model-catalog.js";
|
|
6
6
|
/** A reference file selected by the host, with its destination identity and path. */
|
|
7
7
|
export type MediaReference = Readonly<{
|
|
8
8
|
/** Host-admitted destination or reference identity. */
|
|
@@ -165,10 +165,13 @@ export declare const mediaGenerationSelectionSchema: z.ZodObject<{
|
|
|
165
165
|
}, z.core.$strict>;
|
|
166
166
|
/** Generation settings with reference paths resolved by an admitting host. */
|
|
167
167
|
export type MediaGenerationSelection = z.output<typeof mediaGenerationSelectionSchema>;
|
|
168
|
-
/**
|
|
169
|
-
|
|
168
|
+
/**
|
|
169
|
+
* Validate caller settings against `catalogue`, the active provider's model
|
|
170
|
+
* list (the Fungi gateway's by default), and apply that model's defaults.
|
|
171
|
+
*/
|
|
172
|
+
export declare const validateMediaGenerationSettings: (raw: MediaGenerationSettingsInput, catalogue?: MediaCatalogue) => MediaGenerationSettings;
|
|
170
173
|
/** Validate selections without granting Computer authority. */
|
|
171
|
-
export declare const validateMediaGenerationSelection: (raw: z.input<typeof mediaGenerationSelectionSchema
|
|
174
|
+
export declare const validateMediaGenerationSelection: (raw: z.input<typeof mediaGenerationSelectionSchema>, catalogue?: MediaCatalogue) => Readonly<Omit<MediaGenerationSettings, "references"> & {
|
|
172
175
|
/** Host-authorized reference images. */
|
|
173
176
|
references?: readonly Readonly<{
|
|
174
177
|
/** Fixed destination or reference file path. */
|
|
@@ -229,12 +232,46 @@ export declare const mediaVideoSelectionSchema: z.ZodLazy<z.ZodObject<{
|
|
|
229
232
|
}>;
|
|
230
233
|
}, z.core.$strict>>>>;
|
|
231
234
|
}, z.core.$strict>>;
|
|
235
|
+
/**
|
|
236
|
+
* The same selection for a host's own catalogue: the model is any id that
|
|
237
|
+
* catalogue lists, checked by `validateMediaVideoSelection`.
|
|
238
|
+
*/
|
|
239
|
+
declare const catalogueVideoSelectionSchema: z.ZodObject<{
|
|
240
|
+
prompt: z.ZodString;
|
|
241
|
+
resolution: z.ZodOptional<z.ZodEnum<{
|
|
242
|
+
readonly p480: "480p";
|
|
243
|
+
readonly p720: "720p";
|
|
244
|
+
readonly p768: "768p";
|
|
245
|
+
readonly p1080: "1080p";
|
|
246
|
+
readonly p4k: "4k";
|
|
247
|
+
}>>;
|
|
248
|
+
aspectRatio: z.ZodOptional<z.ZodEnum<{
|
|
249
|
+
readonly wide: "16:9";
|
|
250
|
+
readonly landscape: "4:3";
|
|
251
|
+
readonly square: "1:1";
|
|
252
|
+
readonly portrait: "3:4";
|
|
253
|
+
readonly tall: "9:16";
|
|
254
|
+
readonly ultrawide: "21:9";
|
|
255
|
+
readonly ultratall: "9:21";
|
|
256
|
+
}>>;
|
|
257
|
+
durationSeconds: z.ZodOptional<z.ZodNumber>;
|
|
258
|
+
generateAudio: z.ZodOptional<z.ZodBoolean>;
|
|
259
|
+
references: z.ZodOptional<z.ZodArray<z.ZodLazy<z.ZodObject<{
|
|
260
|
+
path: z.ZodString;
|
|
261
|
+
role: z.ZodEnum<{
|
|
262
|
+
readonly first: "first-frame";
|
|
263
|
+
readonly last: "last-frame";
|
|
264
|
+
readonly reference: "reference";
|
|
265
|
+
}>;
|
|
266
|
+
}, z.core.$strict>>>>;
|
|
267
|
+
model: z.ZodString;
|
|
268
|
+
}, z.core.$strict>;
|
|
232
269
|
/** Caller video selection before defaults and model-specific refinements. */
|
|
233
270
|
export type MediaVideoSelectionInput = z.input<typeof mediaVideoSelectionSchema>;
|
|
234
271
|
/** Normalized path-only video settings, ready for host-authorized reference hydration. */
|
|
235
272
|
export type MediaVideoSelection = Readonly<{
|
|
236
|
-
/**
|
|
237
|
-
model:
|
|
273
|
+
/** Catalogue video identifier. */
|
|
274
|
+
model: string;
|
|
238
275
|
/** Trimmed, bounded prompt. */
|
|
239
276
|
prompt: string;
|
|
240
277
|
/** Nominal width selecting resolution/aspect, not decoded MP4 dimensions. */
|
|
@@ -253,14 +290,14 @@ export type MediaVideoSelection = Readonly<{
|
|
|
253
290
|
references?: readonly Readonly<MediaVideoReference>[];
|
|
254
291
|
}>;
|
|
255
292
|
/** Validate supported model/size/duration/reference combinations before authority or billing admission. */
|
|
256
|
-
export declare function validateMediaVideoSelection(raw:
|
|
293
|
+
export declare function validateMediaVideoSelection(raw: z.input<typeof catalogueVideoSelectionSchema>, catalogue?: MediaCatalogue): MediaVideoSelection;
|
|
257
294
|
/** Host-authorized reader for decoded reference images. */
|
|
258
295
|
export type MediaReferenceReader = Readonly<{
|
|
259
296
|
/** Authorize and fit one reference for the selected catalog model. */
|
|
260
297
|
read: (reference: MediaReference, model: string) => Promise<MediaReferenceBytes>;
|
|
261
298
|
}>;
|
|
262
299
|
/** Resolve only provider-accepted references through the caller's reader. */
|
|
263
|
-
export declare const resolveMediaReferences: (settings: MediaGenerationSettings, reader: MediaReferenceReader) => Promise<readonly MediaReferenceBytes[]>;
|
|
300
|
+
export declare const resolveMediaReferences: (settings: MediaGenerationSettings, reader: MediaReferenceReader, catalogue?: MediaCatalogue) => Promise<readonly MediaReferenceBytes[]>;
|
|
264
301
|
/** Provider JSON image input for models without multipart references. */
|
|
265
302
|
export type MediaJsonBody = Readonly<{
|
|
266
303
|
/** Generation prompt. */
|
|
@@ -364,5 +401,5 @@ export { neuronsToMilliunits, USD_PER_1K_NEURONS } from "./neurons.js";
|
|
|
364
401
|
export type { ImageParams, ImageReference } from "./cost-estimate.js";
|
|
365
402
|
/** Authority-free video quote settings; hydrated host settings are structurally compatible. */
|
|
366
403
|
export type MediaVideoQuoteSettings = ReturnType<typeof validateMediaGenerationSelection>;
|
|
367
|
-
/** Provider video cost from
|
|
368
|
-
export declare function estimateVideoMilliunits(settings: MediaVideoQuoteSettings): number;
|
|
404
|
+
/** Provider video cost from `catalogue`, in milliunits (1 cent = 1,000). Video-input references are unsupported. */
|
|
405
|
+
export declare function estimateVideoMilliunits(settings: MediaVideoQuoteSettings, catalogue?: MediaCatalogue): number;
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import * as z from "zod";
|
|
2
2
|
import { reportCatchFailure } from "./failure.js";
|
|
3
3
|
import { referenceImageSizeIsValid } from "./reference-image-size.js";
|
|
4
|
-
import { getMediaModel, MAX_REFERENCE_COUNT, MediaSettingsError, mediaVideoModelIdSchema, mediaVideoResolutionSchema, mediaVideoAspectRatioSchema, mediaVideoReferenceRoleSchema, MAX_MEDIA_PROMPT_LENGTH, } from "./model-catalog.js";
|
|
4
|
+
import { getGatewayModel, getMediaModel, MAX_REFERENCE_COUNT, MediaSettingsError, mediaModels, mediaVideoModelIdSchema, mediaVideoResolutionSchema, mediaVideoAspectRatioSchema, mediaVideoReferenceRoleSchema, MAX_MEDIA_PROMPT_LENGTH, } from "./model-catalog.js";
|
|
5
5
|
export { mediaFailureStageSchema } from "./execution-error.js";
|
|
6
|
-
export { MAX_MEDIA_PROMPT_LENGTH, getMediaModel, MAX_REFERENCE_COUNT, listMediaModels, mediaModelInputSchema, mediaModelSchema, mediaRequestedImageFormatSchema, mediaModels, mediaModelTierSchema, MediaSettingsError, mediaVideoModelIdSchema, mediaVideoResolutionSchema, mediaVideoAspectRatioSchema, mediaVideoReferenceRoleSchema, mediaVideoFormatSchema, mediaVideoSchema, } from "./model-catalog.js";
|
|
6
|
+
export { MAX_MEDIA_PROMPT_LENGTH, getMediaModel, getGatewayModel, MAX_REFERENCE_COUNT, listMediaModels, mediaCatalogueSchema, mediaCatalogueModelSchema, parseMediaCatalogue, mediaModelInputSchema, mediaModelSchema, mediaRequestedImageFormatSchema, mediaModels, mediaModelTierSchema, MediaSettingsError, mediaVideoModelIdSchema, mediaVideoResolutionSchema, mediaVideoAspectRatioSchema, mediaVideoReferenceRoleSchema, mediaVideoFormatSchema, mediaVideoSchema, } from "./model-catalog.js";
|
|
7
7
|
/** The long-edge bounds a viewer may ask for when fitting an image to look at. */
|
|
8
8
|
export const MIN_VIEW_DIMENSION = 256;
|
|
9
9
|
/** Maximum long-edge size accepted by an image viewer, in pixels. */
|
|
@@ -97,15 +97,18 @@ export const mediaGenerationSelectionSchema = mediaGenerationSettingsSchema.exte
|
|
|
97
97
|
.max(MAX_REFERENCE_COUNT)
|
|
98
98
|
.optional(),
|
|
99
99
|
});
|
|
100
|
-
/**
|
|
101
|
-
|
|
100
|
+
/**
|
|
101
|
+
* Validate caller settings against `catalogue`, the active provider's model
|
|
102
|
+
* list (the Fungi gateway's by default), and apply that model's defaults.
|
|
103
|
+
*/
|
|
104
|
+
export const validateMediaGenerationSettings = (raw, catalogue = mediaModels) => {
|
|
102
105
|
const parsed = mediaGenerationSettingsSchema.safeParse(raw);
|
|
103
106
|
if (!parsed.success)
|
|
104
107
|
throw new MediaSettingsError("generation settings are invalid");
|
|
105
|
-
return normalizeGenerationSettings(parsed.data, normalizeReference);
|
|
108
|
+
return normalizeGenerationSettings(parsed.data, normalizeReference, catalogue);
|
|
106
109
|
};
|
|
107
110
|
/** Validate selections without granting Computer authority. */
|
|
108
|
-
export const validateMediaGenerationSelection = (raw) => {
|
|
111
|
+
export const validateMediaGenerationSelection = (raw, catalogue = mediaModels) => {
|
|
109
112
|
const parsed = mediaGenerationSelectionSchema.safeParse(raw);
|
|
110
113
|
if (!parsed.success)
|
|
111
114
|
throw new MediaSettingsError("generation settings are invalid");
|
|
@@ -117,11 +120,11 @@ export const validateMediaGenerationSelection = (raw) => {
|
|
|
117
120
|
if (reference.role === undefined)
|
|
118
121
|
return selection;
|
|
119
122
|
return { ...selection, role: reference.role };
|
|
120
|
-
});
|
|
123
|
+
}, catalogue);
|
|
121
124
|
};
|
|
122
|
-
function normalizeGenerationSettings(input, normalize) {
|
|
125
|
+
function normalizeGenerationSettings(input, normalize, catalogue) {
|
|
123
126
|
const modelId = requiredString(input.model, "model");
|
|
124
|
-
const model = getMediaModel(modelId);
|
|
127
|
+
const model = getMediaModel(modelId, catalogue);
|
|
125
128
|
if (model === undefined)
|
|
126
129
|
throw new MediaSettingsError(`unknown Media model "${modelId}"`);
|
|
127
130
|
const prompt = requiredString(input.prompt, "prompt");
|
|
@@ -258,13 +261,20 @@ export const mediaVideoSelectionSchema = z.lazy(() => z.strictObject({
|
|
|
258
261
|
.max(MAX_VIDEO_REFERENCE_COUNT)
|
|
259
262
|
.optional(),
|
|
260
263
|
}));
|
|
264
|
+
/**
|
|
265
|
+
* The same selection for a host's own catalogue: the model is any id that
|
|
266
|
+
* catalogue lists, checked by `validateMediaVideoSelection`.
|
|
267
|
+
*/
|
|
268
|
+
const catalogueVideoSelectionSchema = mediaVideoSelectionSchema
|
|
269
|
+
.unwrap()
|
|
270
|
+
.extend({ model: z.string().min(1).max(256) });
|
|
261
271
|
/** Validate supported model/size/duration/reference combinations before authority or billing admission. */
|
|
262
|
-
export function validateMediaVideoSelection(raw) {
|
|
263
|
-
const parsed =
|
|
272
|
+
export function validateMediaVideoSelection(raw, catalogue = mediaModels) {
|
|
273
|
+
const parsed = catalogueVideoSelectionSchema.safeParse(raw);
|
|
264
274
|
if (!parsed.success)
|
|
265
275
|
throw new MediaSettingsError("video selection is invalid");
|
|
266
276
|
const input = parsed.data;
|
|
267
|
-
const model = getMediaModel(input.model);
|
|
277
|
+
const model = getMediaModel(input.model, catalogue);
|
|
268
278
|
if (model?.video == null)
|
|
269
279
|
throw new MediaSettingsError("unknown video model");
|
|
270
280
|
const resolution = input.resolution ?? model.video.defaultResolution;
|
|
@@ -277,7 +287,7 @@ export function validateMediaVideoSelection(raw) {
|
|
|
277
287
|
...input,
|
|
278
288
|
width: format.width,
|
|
279
289
|
height: format.height,
|
|
280
|
-
});
|
|
290
|
+
}, catalogue);
|
|
281
291
|
const selection = {
|
|
282
292
|
...format,
|
|
283
293
|
model: input.model,
|
|
@@ -292,8 +302,8 @@ export function validateMediaVideoSelection(raw) {
|
|
|
292
302
|
}
|
|
293
303
|
const imageFormats = new Set(["png", "jpeg", "gif", "webp"]);
|
|
294
304
|
/** Resolve only provider-accepted references through the caller's reader. */
|
|
295
|
-
export const resolveMediaReferences = async (settings, reader) => {
|
|
296
|
-
const model = getMediaModel(settings.model);
|
|
305
|
+
export const resolveMediaReferences = async (settings, reader, catalogue = mediaModels) => {
|
|
306
|
+
const model = getMediaModel(settings.model, catalogue);
|
|
297
307
|
if (model === undefined)
|
|
298
308
|
throw new MediaSettingsError("unknown Media model");
|
|
299
309
|
const references = settings.references ?? [];
|
|
@@ -308,7 +318,7 @@ export const resolveMediaReferences = async (settings, reader) => {
|
|
|
308
318
|
!Number.isSafeInteger(image.height) ||
|
|
309
319
|
image.width < 1 ||
|
|
310
320
|
image.height < 1 ||
|
|
311
|
-
!referenceImageSizeIsValid(settings.model, image.width, image.height) ||
|
|
321
|
+
!referenceImageSizeIsValid(settings.model, image.width, image.height, catalogue) ||
|
|
312
322
|
!(image.bytes instanceof Uint8Array) ||
|
|
313
323
|
image.bytes.byteLength === 0 ||
|
|
314
324
|
image.bytes.byteLength > MAX_REFERENCE_BYTES)
|
|
@@ -323,7 +333,7 @@ export const resolveMediaReferences = async (settings, reader) => {
|
|
|
323
333
|
const mimeForFormat = (format) => `image/${format}`;
|
|
324
334
|
/** Build the exact JSON or multipart payload required by a retained model. */
|
|
325
335
|
export const buildProviderInput = (settings, references = []) => {
|
|
326
|
-
const model =
|
|
336
|
+
const model = getGatewayModel(settings.model);
|
|
327
337
|
if (model === undefined)
|
|
328
338
|
throw new MediaSettingsError("unknown Media model");
|
|
329
339
|
if (model.sizes !== null)
|
|
@@ -604,16 +614,16 @@ export const normalizeMediaProviderOutput = async (value, options = {}) => {
|
|
|
604
614
|
};
|
|
605
615
|
export { estimateImageMilliunits, estimateImageNeurons, } from "./cost-estimate.js";
|
|
606
616
|
export { neuronsToMilliunits, USD_PER_1K_NEURONS } from "./neurons.js";
|
|
607
|
-
/** Provider video cost from
|
|
608
|
-
export function estimateVideoMilliunits(settings) {
|
|
617
|
+
/** Provider video cost from `catalogue`, in milliunits (1 cent = 1,000). Video-input references are unsupported. */
|
|
618
|
+
export function estimateVideoMilliunits(settings, catalogue = mediaModels) {
|
|
609
619
|
const checked = validateMediaGenerationSelection({
|
|
610
620
|
...settings,
|
|
611
621
|
references: settings.references?.map((reference) => ({
|
|
612
622
|
path: reference.path,
|
|
613
623
|
role: reference.role,
|
|
614
624
|
})),
|
|
615
|
-
});
|
|
616
|
-
const model = getMediaModel(checked.model);
|
|
625
|
+
}, catalogue);
|
|
626
|
+
const model = getMediaModel(checked.model, catalogue);
|
|
617
627
|
if (model?.video == null || checked.durationSeconds === undefined)
|
|
618
628
|
throw new MediaSettingsError("video cost requires a verified video model");
|
|
619
629
|
const price = model.video.prices.find((candidate) => candidate.resolution === checked.resolution);
|
package/dist/media-binding.js
CHANGED
|
@@ -3,7 +3,7 @@ import * as Schema from "effect/Schema";
|
|
|
3
3
|
import { pending } from "@fungi.computer/agar/backend-definition";
|
|
4
4
|
import { mediaRequestIdSchema } from "@fungi.computer/agar/media-request-id";
|
|
5
5
|
import { mediaLibraryPathSchema } from "@fungi.computer/agar/media-library";
|
|
6
|
-
import {
|
|
6
|
+
import { getGatewayModel, mediaGenerationSelectionSchema, mediaRequestedImageFormatSchema, mediaVideoSelectionSchema, validateMediaGenerationSelection, validateMediaVideoSelection, MediaSettingsError, readResponseBytes, } from "./index.js";
|
|
7
7
|
export { mediaResultSchema } from "./media-operation-contract.js";
|
|
8
8
|
import { mediaFailureSchema } from "./browser-failure.js";
|
|
9
9
|
import { appMediaBrokerFailureSchema, appMediaGenerationResultSchema, mediaModelsSchema, } from "./app-media-contracts.js";
|
|
@@ -31,7 +31,7 @@ export const mediaImageBindingInputSchema = z.strictObject({
|
|
|
31
31
|
.max(Number.MAX_SAFE_INTEGER)
|
|
32
32
|
.optional(),
|
|
33
33
|
})
|
|
34
|
-
.refine((settings) =>
|
|
34
|
+
.refine((settings) => getGatewayModel(settings.model)?.kind === "image", "Select an image model from listModels"),
|
|
35
35
|
/** Requested image encoding, validated against the selected model. */
|
|
36
36
|
outputFormat: mediaRequestedImageFormatSchema.optional(),
|
|
37
37
|
});
|
|
@@ -109,7 +109,7 @@ export function mediaBindingRequest(input, computerId) {
|
|
|
109
109
|
? validateMediaGenerationSelection(input.settings)
|
|
110
110
|
: validateMediaVideoSelection(input.settings);
|
|
111
111
|
const { references, ...settings } = selected;
|
|
112
|
-
const model =
|
|
112
|
+
const model = getGatewayModel(input.settings.model);
|
|
113
113
|
const format = input.kind === "video"
|
|
114
114
|
? "mp4"
|
|
115
115
|
: (input.outputFormat ??
|
package/dist/media-generation.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as Effect from "effect/Effect";
|
|
2
2
|
import { getMediaModel, validateMediaGenerationSettings, resolveMediaReferences, MediaSettingsError, } from "./index.js";
|
|
3
3
|
import { estimate } from "./cloudflare/provider.js";
|
|
4
|
+
import { mediaModels } from "./model-catalog.js";
|
|
4
5
|
import { executeMediaOperation } from "./media-operation.js";
|
|
5
6
|
import { MediaExecutionError } from "./execution-error.js";
|
|
6
7
|
import { mediaResultSchema } from "./media-operation-contract.js";
|
|
@@ -10,13 +11,15 @@ import { mediaResultSchema } from "./media-operation-contract.js";
|
|
|
10
11
|
* same admission, billing and destination-write sequence.
|
|
11
12
|
*/
|
|
12
13
|
export function runMediaGeneration(host, request, signal) {
|
|
14
|
+
const catalogue = host.catalogue ?? mediaModels;
|
|
15
|
+
const estimator = host.estimate ?? estimate;
|
|
13
16
|
return Effect.gen(function* () {
|
|
14
17
|
const settings = yield* Effect.try({
|
|
15
|
-
try: () => validateMediaGenerationSettings(request.settings),
|
|
18
|
+
try: () => validateMediaGenerationSettings(request.settings, catalogue),
|
|
16
19
|
catch: () => new MediaSettingsError("invalid generation settings"),
|
|
17
20
|
});
|
|
18
21
|
if (request.outputFormat !== undefined &&
|
|
19
|
-
!getMediaModel(settings.model)?.outputFormats.includes(request.outputFormat))
|
|
22
|
+
!getMediaModel(settings.model, catalogue)?.outputFormats.includes(request.outputFormat))
|
|
20
23
|
return yield* Effect.fail(new MediaSettingsError("model cannot produce the requested image format"));
|
|
21
24
|
const path = request.path;
|
|
22
25
|
const beforeAdmit = host.beforeAdmit;
|
|
@@ -33,6 +36,7 @@ export function runMediaGeneration(host, request, signal) {
|
|
|
33
36
|
owner: host.scope.owner,
|
|
34
37
|
},
|
|
35
38
|
requestId: request.requestId,
|
|
39
|
+
catalogue,
|
|
36
40
|
request: {
|
|
37
41
|
...settings,
|
|
38
42
|
computerId: host.scope.destinationId,
|
|
@@ -52,8 +56,8 @@ export function runMediaGeneration(host, request, signal) {
|
|
|
52
56
|
throw new MediaSettingsError("reference images are unavailable");
|
|
53
57
|
return host.destination.readReference(reference, model);
|
|
54
58
|
},
|
|
55
|
-
});
|
|
56
|
-
const costMilliunits =
|
|
59
|
+
}, catalogue);
|
|
60
|
+
const costMilliunits = estimator(settings, references);
|
|
57
61
|
// Reference preparation may suspend. This final authority check is
|
|
58
62
|
// the new-generation admission cutoff, before durable dispatch.
|
|
59
63
|
try {
|
|
@@ -79,7 +83,7 @@ export function runMediaGeneration(host, request, signal) {
|
|
|
79
83
|
return { kind: "conflict" };
|
|
80
84
|
if (outcome.kind === "accepted" ||
|
|
81
85
|
(outcome.kind === "pending" &&
|
|
82
|
-
getMediaModel(settings.model)?.kind === "video"))
|
|
86
|
+
getMediaModel(settings.model, catalogue)?.kind === "video"))
|
|
83
87
|
// A video generation runs in the background; retry this request id.
|
|
84
88
|
return {
|
|
85
89
|
kind: "pending",
|
|
@@ -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 { 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. */
|
|
@@ -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,3 +1,4 @@
|
|
|
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";
|
|
@@ -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;
|
|
@@ -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";
|
|
@@ -154,7 +154,7 @@ function fingerprint(input) {
|
|
|
154
154
|
return JSON.stringify(input);
|
|
155
155
|
}
|
|
156
156
|
const objectSchema = z.lazy(() => z.record(z.string(), z.unknown()));
|
|
157
|
-
function decodeStoredRequest(serialized) {
|
|
157
|
+
function decodeStoredRequest(serialized, catalogue) {
|
|
158
158
|
let raw;
|
|
159
159
|
try {
|
|
160
160
|
raw = JSON.parse(serialized);
|
|
@@ -181,7 +181,7 @@ function decodeStoredRequest(serialized) {
|
|
|
181
181
|
throw new MediaOperationError("partial_init");
|
|
182
182
|
let settings;
|
|
183
183
|
try {
|
|
184
|
-
settings = validateMediaGenerationSettings(parsedGeneration.data);
|
|
184
|
+
settings = validateMediaGenerationSettings(parsedGeneration.data, catalogue);
|
|
185
185
|
}
|
|
186
186
|
catch (cause) {
|
|
187
187
|
throw new MediaOperationError("partial_init", { cause });
|
|
@@ -213,6 +213,9 @@ export class MediaOperationOwner {
|
|
|
213
213
|
mediaProvider() {
|
|
214
214
|
return this.provider?.();
|
|
215
215
|
}
|
|
216
|
+
catalogue() {
|
|
217
|
+
return this.hooks.catalogue?.() ?? mediaModels;
|
|
218
|
+
}
|
|
216
219
|
constructor(ctx, env, provider, hooks = {}) {
|
|
217
220
|
this.ctx = ctx;
|
|
218
221
|
this.env = env;
|
|
@@ -278,7 +281,7 @@ export class MediaOperationOwner {
|
|
|
278
281
|
namespace: coordinates.data.namespace,
|
|
279
282
|
owner: coordinates.data.owner,
|
|
280
283
|
fingerprint: row.fingerprint,
|
|
281
|
-
input: decodeStoredRequest(row.input),
|
|
284
|
+
input: decodeStoredRequest(row.input, this.catalogue()),
|
|
282
285
|
state: row.state,
|
|
283
286
|
leaseId: row.lease_id,
|
|
284
287
|
leaseExpiresAt: row.lease_expires_at,
|
|
@@ -709,7 +712,7 @@ export class MediaOperationOwner {
|
|
|
709
712
|
};
|
|
710
713
|
}));
|
|
711
714
|
// The captured request includes destination authority; providers receive settings only.
|
|
712
|
-
const settings = validateMediaGenerationSettings(mediaGenerationSettingsSchema.strip().parse(record.input));
|
|
715
|
+
const settings = validateMediaGenerationSettings(mediaGenerationSettingsSchema.strip().parse(record.input), this.catalogue());
|
|
713
716
|
output = await provider(settings, references, undefined, record.input.outputFormat);
|
|
714
717
|
}
|
|
715
718
|
catch (cause) {
|
package/dist/media-operation.js
CHANGED
|
@@ -58,8 +58,8 @@ 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
|
-
const background = getMediaModel(input.request.model)?.kind === "video";
|
|
61
|
+
const billed = mediaUnits(validateMediaGenerationSettings(mediaGenerationSettingsSchema.strip().parse(input.request), input.catalogue), input.catalogue);
|
|
62
|
+
const background = getMediaModel(input.request.model, input.catalogue)?.kind === "video";
|
|
63
63
|
/** Best-effort release; true proves customer settlement, not supplier billing. */
|
|
64
64
|
const settleNothing = () => metering === null
|
|
65
65
|
? Effect.succeed(false)
|
|
@@ -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
|
+
}
|