@opencode/ai 0.0.0-dev-176 → 0.0.0-dev-187
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 -58
- package/dist/image-client.d.ts +45 -0
- package/dist/index.d.ts +36 -19
- package/dist/index.js +13 -2
- package/dist/media-client.d.ts +1 -0
- package/dist/media-client.js +1 -0
- package/dist/provider-error.js +0 -2
- package/dist/providers/digitalocean/responses.d.ts +4 -0
- package/dist/providers/digitalocean/responses.js +1 -0
- package/dist/providers/digitalocean.d.ts +206 -4
- package/dist/providers/digitalocean.js +17 -6
- package/dist/providers/index.d.ts +0 -4
- package/dist/providers/index.js +0 -4
- package/dist/providers/openrouter/responses.d.ts +4 -0
- package/dist/providers/openrouter/responses.js +1 -0
- package/dist/providers/openrouter.d.ts +207 -5
- package/dist/providers/openrouter.js +20 -11
- package/dist/providers/venice/responses.d.ts +4 -0
- package/dist/providers/venice/responses.js +1 -0
- package/dist/providers/venice.d.ts +206 -4
- package/dist/providers/venice.js +33 -17
- package/dist/route/auth.d.ts +1 -1
- package/dist/route/media-protocol.d.ts +2 -4
- package/dist/route/media.d.ts +1 -1
- package/dist/route/media.js +2 -4
- package/dist/speech-client.d.ts +48 -0
- package/dist/testing/decision.d.ts +16 -0
- package/dist/testing/decision.js +20 -0
- package/dist/testing/image.d.ts +14 -0
- package/dist/testing/image.js +12 -0
- package/dist/testing/media.d.ts +18 -0
- package/dist/testing/media.js +36 -0
- package/dist/testing/script.d.ts +28 -0
- package/dist/testing/script.js +60 -0
- package/dist/testing/speech.d.ts +14 -0
- package/dist/testing/speech.js +12 -0
- package/dist/testing/transcription.d.ts +14 -0
- package/dist/testing/transcription.js +12 -0
- package/dist/testing/video.d.ts +14 -0
- package/dist/testing/video.js +12 -0
- package/dist/testing.d.ts +10 -21
- package/dist/testing.js +11 -54
- package/dist/transcription-client.d.ts +64 -0
- package/dist/video-client.d.ts +41 -0
- package/package.json +14 -18
- package/dist/protocols/bfl-images.d.ts +0 -47
- package/dist/protocols/bfl-images.js +0 -155
- package/dist/protocols/fal-images.d.ts +0 -36
- package/dist/protocols/fal-images.js +0 -96
- package/dist/protocols/fal-video.d.ts +0 -29
- package/dist/protocols/fal-video.js +0 -73
- package/dist/protocols/replicate-images.d.ts +0 -28
- package/dist/protocols/replicate-images.js +0 -127
- package/dist/protocols/stability-images.d.ts +0 -44
- package/dist/protocols/stability-images.js +0 -131
- package/dist/protocols/utils/fal-queue.d.ts +0 -26
- package/dist/protocols/utils/fal-queue.js +0 -67
- package/dist/providers/black-forest-labs.d.ts +0 -21
- package/dist/providers/black-forest-labs.js +0 -23
- package/dist/providers/fal.d.ts +0 -24
- package/dist/providers/fal.js +0 -26
- package/dist/providers/replicate.d.ts +0 -21
- package/dist/providers/replicate.js +0 -17
- package/dist/providers/stability.d.ts +0 -23
- package/dist/providers/stability.js +0 -18
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
import { Effect, Schema } from "effect";
|
|
2
|
-
import { ImageModel, ImageResponse } from "../image.js";
|
|
3
|
-
import { Media } from "../media.js";
|
|
4
|
-
import { MediaProtocol } from "../route/media-protocol.js";
|
|
5
|
-
import { MediaRoute } from "../route/media.js";
|
|
6
|
-
import { mergeJsonRecords } from "../schema/index.js";
|
|
7
|
-
import { ProviderShared, optionalNull } from "./shared.js";
|
|
8
|
-
import { ImageSizing } from "./utils/image-sizing.js";
|
|
9
|
-
const route = MediaProtocol.identity({ id: "bfl-images", name: "Black Forest Labs", provider: "black-forest-labs" });
|
|
10
|
-
export const DEFAULT_BASE_URL = "https://api.bfl.ai";
|
|
11
|
-
// ---------------------------------------------------------------------------
|
|
12
|
-
// 2. Token and response schemas
|
|
13
|
-
// ---------------------------------------------------------------------------
|
|
14
|
-
/**
|
|
15
|
-
* Regional clusters answer on different hosts, so the returned `polling_url` is followed verbatim. BFL reports the
|
|
16
|
-
* credit cost on submit, so it rides on the token; it is optional so tokens persisted before it existed still decode.
|
|
17
|
-
*/
|
|
18
|
-
export const Token = Schema.Struct({
|
|
19
|
-
id: Schema.String,
|
|
20
|
-
pollingURL: Schema.String,
|
|
21
|
-
cost: Schema.optionalKey(Schema.Number),
|
|
22
|
-
});
|
|
23
|
-
const StartResponse = Schema.Struct({
|
|
24
|
-
id: Schema.String,
|
|
25
|
-
polling_url: Schema.String,
|
|
26
|
-
cost: optionalNull(Schema.Number),
|
|
27
|
-
});
|
|
28
|
-
const Result = Schema.Struct({
|
|
29
|
-
id: Schema.String,
|
|
30
|
-
status: Schema.String,
|
|
31
|
-
result: optionalNull(Schema.StructWithRest(Schema.Struct({ sample: Schema.String, seed: optionalNull(Schema.Number), prompt: optionalNull(Schema.String) }), [Schema.Record(Schema.String, Schema.Unknown)])),
|
|
32
|
-
cost: optionalNull(Schema.Number),
|
|
33
|
-
});
|
|
34
|
-
const STATUS = {
|
|
35
|
-
Pending: "running",
|
|
36
|
-
Reasoning: "running",
|
|
37
|
-
Generating: "running",
|
|
38
|
-
Ready: "completed",
|
|
39
|
-
Error: "failed",
|
|
40
|
-
// Moderation is terminal; `decodeResult` reports it as a content-policy failure.
|
|
41
|
-
"Content Moderated": "failed",
|
|
42
|
-
"Request Moderated": "failed",
|
|
43
|
-
"Task not found": "expired",
|
|
44
|
-
};
|
|
45
|
-
const isModerated = (status) => status === "Content Moderated" || status === "Request Moderated";
|
|
46
|
-
const LEGACY_LIMITS = { multiple: 32, minEdge: 256, maxEdge: 1440 };
|
|
47
|
-
const FLUX_2_LIMITS = { multiple: 16, minEdge: 64, maxPixels: 4_194_304 };
|
|
48
|
-
const capabilities = (model) => {
|
|
49
|
-
if (model.startsWith("flux-pro-1.0-fill"))
|
|
50
|
-
return { sizing: "none", imageField: "image", maxImages: 1, mask: true };
|
|
51
|
-
if (model.startsWith("flux-pro-1.0-expand"))
|
|
52
|
-
return { sizing: "none", imageField: "image", maxImages: 1, mask: false };
|
|
53
|
-
if (model.startsWith("flux-kontext"))
|
|
54
|
-
return { sizing: "aspectRatio", imageField: "input_image", maxImages: 4, mask: false };
|
|
55
|
-
if (model.startsWith("flux-pro-1.1-ultra"))
|
|
56
|
-
return { sizing: "aspectRatio", imageField: "image_prompt", maxImages: 1, mask: false };
|
|
57
|
-
if (model.startsWith("flux-pro-1.1") || model.startsWith("flux-dev"))
|
|
58
|
-
return { sizing: LEGACY_LIMITS, imageField: "image_prompt", maxImages: 1, mask: false };
|
|
59
|
-
if (model.startsWith("flux-2-klein"))
|
|
60
|
-
return { sizing: FLUX_2_LIMITS, imageField: "input_image", maxImages: 4, mask: false };
|
|
61
|
-
return { sizing: FLUX_2_LIMITS, imageField: "input_image", maxImages: 8, mask: false };
|
|
62
|
-
};
|
|
63
|
-
const validate = (request, model) => {
|
|
64
|
-
const id = request.model.id;
|
|
65
|
-
const images = request.images?.length ?? 0;
|
|
66
|
-
if (request.n !== undefined && request.n > 1)
|
|
67
|
-
return Effect.fail(route.unsupported("media.n", `${id} generates one image per request; call it once per image`));
|
|
68
|
-
if (request.aspect !== undefined && model.sizing === "none")
|
|
69
|
-
return Effect.fail(route.unsupported("media.aspect", `${id} keeps the source image's size`));
|
|
70
|
-
if (request.resolution !== undefined && typeof model.sizing === "string")
|
|
71
|
-
return Effect.fail(route.unsupported("media.resolution", `${id} does not take resolution`));
|
|
72
|
-
if (images > model.maxImages)
|
|
73
|
-
return Effect.fail(route.unsupported("media.images", `${id} takes at most ${model.maxImages} images`));
|
|
74
|
-
if (request.providerOptions?.mask !== undefined && !model.mask)
|
|
75
|
-
return Effect.fail(route.unsupported("media.mask", `${id} does not inpaint; use flux-pro-1.0-fill`));
|
|
76
|
-
return Effect.void;
|
|
77
|
-
};
|
|
78
|
-
const imageInput = (asset) => {
|
|
79
|
-
const value = asset.inline()?.base64 ?? ProviderShared.mediaUrl(asset);
|
|
80
|
-
if (value === undefined)
|
|
81
|
-
return Effect.fail(ProviderShared.invalidRequest(`${route.name} accepts inline images or https URLs`));
|
|
82
|
-
return Effect.succeed(value);
|
|
83
|
-
};
|
|
84
|
-
const fromRequest = Effect.fn("BlackForestLabsImages.fromRequest")(function* (request) {
|
|
85
|
-
const model = capabilities(request.model.id);
|
|
86
|
-
yield* validate(request, model);
|
|
87
|
-
const images = yield* Effect.forEach(request.images ?? [], imageInput);
|
|
88
|
-
const fields = images.map((image, index) => [
|
|
89
|
-
index === 0 ? model.imageField : `${model.imageField}_${index + 1}`,
|
|
90
|
-
image,
|
|
91
|
-
]);
|
|
92
|
-
const { mask, ...native } = request.providerOptions ?? {};
|
|
93
|
-
const size = typeof model.sizing === "string" ? undefined : yield* ImageSizing.exact(route, request, model.sizing);
|
|
94
|
-
return MediaProtocol.json(mergeJsonRecords({
|
|
95
|
-
prompt: request.prompt,
|
|
96
|
-
...size,
|
|
97
|
-
aspect_ratio: model.sizing === "aspectRatio" ? request.aspect : undefined,
|
|
98
|
-
mask: mask === undefined ? undefined : yield* imageInput(mask),
|
|
99
|
-
...Object.fromEntries(fields),
|
|
100
|
-
}, native, request.http?.body) ?? {});
|
|
101
|
-
});
|
|
102
|
-
// ---------------------------------------------------------------------------
|
|
103
|
-
// 6. Response decoding
|
|
104
|
-
// ---------------------------------------------------------------------------
|
|
105
|
-
const decodeStart = route.decodeStarted(StartResponse, (value) => ({
|
|
106
|
-
token: {
|
|
107
|
-
id: value.id,
|
|
108
|
-
pollingURL: value.polling_url,
|
|
109
|
-
...(value.cost === undefined || value.cost === null ? {} : { cost: value.cost }),
|
|
110
|
-
},
|
|
111
|
-
snapshot: { id: value.id, status: "queued" },
|
|
112
|
-
}));
|
|
113
|
-
const decodeDocument = route.decodeJson(Result);
|
|
114
|
-
const decodeStatus = Effect.fn("BlackForestLabsImages.decodeStatus")(function* (response, context) {
|
|
115
|
-
const output = yield* decodeDocument(response);
|
|
116
|
-
return { id: context.token.id, status: yield* MediaProtocol.status(STATUS, output.value.status, output) };
|
|
117
|
-
});
|
|
118
|
-
const decodeResult = Effect.fn("BlackForestLabsImages.decodeResult")(function* (response, context) {
|
|
119
|
-
const output = yield* decodeDocument(response);
|
|
120
|
-
const document = output.value;
|
|
121
|
-
const status = yield* MediaProtocol.status(STATUS, document.status, output);
|
|
122
|
-
if (isModerated(document.status))
|
|
123
|
-
return yield* output.contentPolicy(`${route.name} moderated the generation`);
|
|
124
|
-
if (status === "failed" || status === "expired")
|
|
125
|
-
return yield* output.ended(status, `${route.name} generation ${context.token.id} ended with ${document.status}`);
|
|
126
|
-
if (status !== "completed")
|
|
127
|
-
return yield* output.pending(context.token.id);
|
|
128
|
-
if (document.result === undefined || document.result === null)
|
|
129
|
-
return yield* output.invalid(`${route.name} generation ${context.token.id} has no result`);
|
|
130
|
-
const { sample, seed, prompt, ...rest } = document.result;
|
|
131
|
-
// A settled `cost` on the result supersedes the submit-time cost carried on the token.
|
|
132
|
-
const cost = document.cost ?? context.token.cost;
|
|
133
|
-
return new ImageResponse({
|
|
134
|
-
// `sample` is a signed URL that expires 10 minutes after the result is ready, so it is downloaded now.
|
|
135
|
-
images: [yield* context.materialize(Media.url(sample))],
|
|
136
|
-
usage: cost === undefined ? undefined : { type: "credits", credits: cost },
|
|
137
|
-
providerMetadata: {
|
|
138
|
-
bfl: { id: context.token.id, seed: seed ?? undefined, prompt: prompt ?? undefined, ...rest },
|
|
139
|
-
},
|
|
140
|
-
});
|
|
141
|
-
});
|
|
142
|
-
// ---------------------------------------------------------------------------
|
|
143
|
-
// 7. Protocol and route
|
|
144
|
-
// ---------------------------------------------------------------------------
|
|
145
|
-
export const protocol = MediaProtocol.queued(route, {
|
|
146
|
-
token: Token,
|
|
147
|
-
start: { body: { from: fromRequest }, decode: decodeStart },
|
|
148
|
-
status: { path: (token) => token.pollingURL, decode: decodeStatus },
|
|
149
|
-
result: { path: (token) => token.pollingURL, decode: decodeResult },
|
|
150
|
-
});
|
|
151
|
-
export const model = (input) => ImageModel.fromRoute({ protocol, baseURL: DEFAULT_BASE_URL, path: ({ request }) => `/v1/${request.model.id}` }, input);
|
|
152
|
-
export const BlackForestLabsImages = {
|
|
153
|
-
protocol,
|
|
154
|
-
model,
|
|
155
|
-
};
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { ImageModel, ImageResponse, type ImageRequestFor } from "../image.js";
|
|
2
|
-
import { Media } from "../media.js";
|
|
3
|
-
import { MediaProtocol } from "../route/media-protocol.js";
|
|
4
|
-
import { MediaRoute } from "../route/media.js";
|
|
5
|
-
import { type OpenString } from "../schema/index.js";
|
|
6
|
-
/** Model-defined `input` fields; `aspect` and `resolution` lower to `image_size` or `aspect_ratio` per model. */
|
|
7
|
-
export type FalImageOptions = {
|
|
8
|
-
readonly image_size?: OpenString<"square_hd" | "square" | "portrait_4_3" | "portrait_16_9" | "landscape_4_3" | "landscape_16_9"> | {
|
|
9
|
-
readonly width: number;
|
|
10
|
-
readonly height: number;
|
|
11
|
-
};
|
|
12
|
-
readonly seed?: number;
|
|
13
|
-
readonly output_format?: OpenString<"jpeg" | "png" | "webp">;
|
|
14
|
-
/** Inpainting mask, sent as `mask_url`. */
|
|
15
|
-
readonly mask?: Media.Asset;
|
|
16
|
-
readonly enable_safety_checker?: boolean;
|
|
17
|
-
/** Defaults to `true`, which returns images inline as data URIs; `false` returns hosted URLs. */
|
|
18
|
-
readonly sync_mode?: boolean;
|
|
19
|
-
} & Record<string, unknown>;
|
|
20
|
-
export type Request = ImageRequestFor<FalImageOptions>;
|
|
21
|
-
export declare const protocol: MediaProtocol.Queued<Request, ImageResponse, {
|
|
22
|
-
readonly requestID: string;
|
|
23
|
-
readonly statusURL: string;
|
|
24
|
-
readonly responseURL: string;
|
|
25
|
-
readonly cancelURL: string;
|
|
26
|
-
}>;
|
|
27
|
-
export declare const model: (input: MediaRoute.ModelInput) => ImageModel<FalImageOptions>;
|
|
28
|
-
export declare const FalImages: {
|
|
29
|
-
readonly protocol: MediaProtocol.Queued<Request, ImageResponse, {
|
|
30
|
-
readonly requestID: string;
|
|
31
|
-
readonly statusURL: string;
|
|
32
|
-
readonly responseURL: string;
|
|
33
|
-
readonly cancelURL: string;
|
|
34
|
-
}>;
|
|
35
|
-
readonly model: typeof model;
|
|
36
|
-
};
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { Effect, Schema } from "effect";
|
|
2
|
-
import { ImageModel, ImageResponse } from "../image.js";
|
|
3
|
-
import { Media } from "../media.js";
|
|
4
|
-
import { MediaProtocol } from "../route/media-protocol.js";
|
|
5
|
-
import { MediaRoute } from "../route/media.js";
|
|
6
|
-
import { mergeJsonRecords } from "../schema/index.js";
|
|
7
|
-
import { optionalNull } from "./shared.js";
|
|
8
|
-
import { FalQueue } from "./utils/fal-queue.js";
|
|
9
|
-
import { ImageSizing } from "./utils/image-sizing.js";
|
|
10
|
-
const route = MediaProtocol.identity({ id: "fal-images", name: "fal Images", provider: "fal" });
|
|
11
|
-
// ---------------------------------------------------------------------------
|
|
12
|
-
// 2. Response schema
|
|
13
|
-
// ---------------------------------------------------------------------------
|
|
14
|
-
const QueueResult = Schema.StructWithRest(Schema.Struct({
|
|
15
|
-
images: Schema.Array(Schema.Struct({
|
|
16
|
-
url: Schema.String,
|
|
17
|
-
width: optionalNull(Schema.Number),
|
|
18
|
-
height: optionalNull(Schema.Number),
|
|
19
|
-
content_type: optionalNull(Schema.String),
|
|
20
|
-
})),
|
|
21
|
-
seed: optionalNull(Schema.Number),
|
|
22
|
-
has_nsfw_concepts: optionalNull(Schema.Array(Schema.Boolean)),
|
|
23
|
-
}), [Schema.Record(Schema.String, Schema.Unknown)]);
|
|
24
|
-
// ---------------------------------------------------------------------------
|
|
25
|
-
// 5. Request body construction
|
|
26
|
-
// ---------------------------------------------------------------------------
|
|
27
|
-
/** Nano Banana takes `aspect_ratio` and `resolution` tiers, FLUX Ultra and Kontext only `aspect_ratio`, FLUX `image_size`. */
|
|
28
|
-
const sizing = (model) => {
|
|
29
|
-
if (model.startsWith("fal-ai/nano-banana"))
|
|
30
|
-
return "tiers";
|
|
31
|
-
if (/^fal-ai\/flux-pro\/(v1\.1-ultra|kontext)/.test(model))
|
|
32
|
-
return "aspect_ratio";
|
|
33
|
-
if (model.startsWith("fal-ai/flux"))
|
|
34
|
-
return "image_size";
|
|
35
|
-
return undefined;
|
|
36
|
-
};
|
|
37
|
-
const validate = (request) => {
|
|
38
|
-
const id = request.model.id;
|
|
39
|
-
if (request.resolution !== undefined && sizing(id) === "aspect_ratio")
|
|
40
|
-
return Effect.fail(route.unsupported("media.resolution", `${id} sizes by aspect only`));
|
|
41
|
-
if ((request.images?.length ?? 0) > 1 && !takesImageList(id))
|
|
42
|
-
return Effect.fail(route.unsupported("media.images", `${id} takes one image_url; use an /edit or /multi endpoint for several images`));
|
|
43
|
-
return Effect.void;
|
|
44
|
-
};
|
|
45
|
-
// `/edit` and `/multi` (Kontext) endpoints take an `image_urls` list; image-to-image, fill, and Ultra take one
|
|
46
|
-
// `image_url` (beside `mask_url`).
|
|
47
|
-
const takesImageList = (model) => model.endsWith("/edit") || model.endsWith("/multi");
|
|
48
|
-
const fromRequest = Effect.fn("FalImages.fromRequest")(function* (request) {
|
|
49
|
-
yield* validate(request);
|
|
50
|
-
const images = yield* Effect.forEach(request.images ?? [], (image) => FalQueue.mediaUrl(image, route.name));
|
|
51
|
-
const list = takesImageList(request.model.id);
|
|
52
|
-
const { mask, ...native } = request.providerOptions ?? {};
|
|
53
|
-
const field = sizing(request.model.id);
|
|
54
|
-
return MediaProtocol.json(mergeJsonRecords({
|
|
55
|
-
prompt: request.prompt,
|
|
56
|
-
num_images: request.n,
|
|
57
|
-
...(field === "image_size"
|
|
58
|
-
? { image_size: yield* ImageSizing.exact(route, request, { multiple: 16 }) }
|
|
59
|
-
: { aspect_ratio: request.aspect, resolution: request.resolution }),
|
|
60
|
-
image_urls: list && images.length > 0 ? images : undefined,
|
|
61
|
-
image_url: list ? undefined : images[0],
|
|
62
|
-
mask_url: mask === undefined ? undefined : yield* FalQueue.mediaUrl(mask, route.name),
|
|
63
|
-
sync_mode: true,
|
|
64
|
-
}, native, request.http?.body) ?? {});
|
|
65
|
-
});
|
|
66
|
-
// ---------------------------------------------------------------------------
|
|
67
|
-
// 6. Response decoding
|
|
68
|
-
// ---------------------------------------------------------------------------
|
|
69
|
-
const decodeQueueResult = route.decodeJson(QueueResult);
|
|
70
|
-
const decodeResult = Effect.fn("FalImages.decodeResult")(function* (response, context) {
|
|
71
|
-
const output = yield* decodeQueueResult(response);
|
|
72
|
-
const { images, seed, ...rest } = output.value;
|
|
73
|
-
if (images.length === 0)
|
|
74
|
-
return yield* output.invalid(`${route.name} returned no images`);
|
|
75
|
-
return new ImageResponse({
|
|
76
|
-
images: images.map((image) => {
|
|
77
|
-
const info = { width: image.width ?? undefined, height: image.height ?? undefined };
|
|
78
|
-
// `sync_mode: true` returns data URIs instead of hosted URLs.
|
|
79
|
-
return (Media.parseDataUrl(image.url, { info }) ??
|
|
80
|
-
Media.url(image.url, { mediaType: image.content_type ?? undefined, info }));
|
|
81
|
-
}),
|
|
82
|
-
providerMetadata: { fal: { requestId: context.token.requestID, seed: seed ?? undefined, ...rest } },
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
// ---------------------------------------------------------------------------
|
|
86
|
-
// 7. Protocol and route
|
|
87
|
-
// ---------------------------------------------------------------------------
|
|
88
|
-
export const protocol = FalQueue.protocol(route, {
|
|
89
|
-
from: fromRequest,
|
|
90
|
-
decodeResult,
|
|
91
|
-
});
|
|
92
|
-
export const model = (input) => ImageModel.fromRoute({ protocol, baseURL: FalQueue.DEFAULT_BASE_URL, path: ({ request }) => `/${request.model.id}` }, input);
|
|
93
|
-
export const FalImages = {
|
|
94
|
-
protocol,
|
|
95
|
-
model,
|
|
96
|
-
};
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { MediaProtocol } from "../route/media-protocol.js";
|
|
2
|
-
import { MediaRoute } from "../route/media.js";
|
|
3
|
-
import { type OpenString } from "../schema/index.js";
|
|
4
|
-
import { VideoModel, VideoResponse, type VideoRequestFor } from "../video.js";
|
|
5
|
-
/**
|
|
6
|
-
* Provider-native input. fal video endpoints are model-specific: `duration` is a string enum whose values differ per
|
|
7
|
-
* model (`"8s"` for Veo, `"5"` for Kling), and last-frame fields are named per model (`end_image_url`,
|
|
8
|
-
* `last_frame_url`, `tail_image_url`), so those pass through here instead of lowering from common fields.
|
|
9
|
-
*/
|
|
10
|
-
export type FalVideoOptions = {
|
|
11
|
-
readonly duration?: OpenString<"4s" | "6s" | "8s" | "5" | "10">;
|
|
12
|
-
} & Record<string, unknown>;
|
|
13
|
-
export type Request = VideoRequestFor<FalVideoOptions>;
|
|
14
|
-
export declare const protocol: MediaProtocol.Queued<Request, VideoResponse, {
|
|
15
|
-
readonly requestID: string;
|
|
16
|
-
readonly statusURL: string;
|
|
17
|
-
readonly responseURL: string;
|
|
18
|
-
readonly cancelURL: string;
|
|
19
|
-
}>;
|
|
20
|
-
export declare const model: (input: MediaRoute.ModelInput) => VideoModel<FalVideoOptions>;
|
|
21
|
-
export declare const FalVideo: {
|
|
22
|
-
readonly protocol: MediaProtocol.Queued<Request, VideoResponse, {
|
|
23
|
-
readonly requestID: string;
|
|
24
|
-
readonly statusURL: string;
|
|
25
|
-
readonly responseURL: string;
|
|
26
|
-
readonly cancelURL: string;
|
|
27
|
-
}>;
|
|
28
|
-
readonly model: typeof model;
|
|
29
|
-
};
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { Effect, Schema } from "effect";
|
|
2
|
-
import { Media } from "../media.js";
|
|
3
|
-
import { MediaProtocol } from "../route/media-protocol.js";
|
|
4
|
-
import { MediaRoute } from "../route/media.js";
|
|
5
|
-
import { mergeJsonRecords } from "../schema/index.js";
|
|
6
|
-
import { VideoModel, VideoResponse } from "../video.js";
|
|
7
|
-
import { optionalNull } from "./shared.js";
|
|
8
|
-
import { FalQueue } from "./utils/fal-queue.js";
|
|
9
|
-
const route = MediaProtocol.identity({ id: "fal-video", name: "fal Video", provider: "fal" });
|
|
10
|
-
// ---------------------------------------------------------------------------
|
|
11
|
-
// 2. Response schema
|
|
12
|
-
// ---------------------------------------------------------------------------
|
|
13
|
-
const QueueResult = Schema.StructWithRest(Schema.Struct({
|
|
14
|
-
video: Schema.Struct({
|
|
15
|
-
url: Schema.String,
|
|
16
|
-
content_type: optionalNull(Schema.String),
|
|
17
|
-
file_name: optionalNull(Schema.String),
|
|
18
|
-
file_size: optionalNull(Schema.Number),
|
|
19
|
-
}),
|
|
20
|
-
seed: optionalNull(Schema.Number),
|
|
21
|
-
}), [Schema.Record(Schema.String, Schema.Unknown)]);
|
|
22
|
-
// ---------------------------------------------------------------------------
|
|
23
|
-
// 5. Request body construction
|
|
24
|
-
// ---------------------------------------------------------------------------
|
|
25
|
-
const fromRequest = Effect.fn("FalVideo.fromRequest")(function* (request) {
|
|
26
|
-
if (request.frames?.last !== undefined)
|
|
27
|
-
return yield* route.unsupported("video.frames.last", `${route.name} names the last frame per model; pass it through providerOptions (e.g. end_image_url) instead of frames.last`);
|
|
28
|
-
const imageUrl = request.frames?.first === undefined ? undefined : yield* FalQueue.mediaUrl(request.frames.first, route.name);
|
|
29
|
-
const videoUrl = request.video === undefined ? undefined : yield* FalQueue.mediaUrl(request.video, route.name);
|
|
30
|
-
return MediaProtocol.json(mergeJsonRecords({
|
|
31
|
-
prompt: request.prompt,
|
|
32
|
-
negative_prompt: request.negativePrompt,
|
|
33
|
-
seed: request.seed,
|
|
34
|
-
aspect_ratio: request.aspect,
|
|
35
|
-
resolution: request.resolution,
|
|
36
|
-
generate_audio: request.audio,
|
|
37
|
-
image_url: imageUrl,
|
|
38
|
-
video_url: videoUrl,
|
|
39
|
-
}, request.providerOptions, request.http?.body) ?? {});
|
|
40
|
-
});
|
|
41
|
-
// ---------------------------------------------------------------------------
|
|
42
|
-
// 6. Response decoding
|
|
43
|
-
// ---------------------------------------------------------------------------
|
|
44
|
-
const decodeQueueResult = route.decodeJson(QueueResult);
|
|
45
|
-
const decodeResult = Effect.fn("FalVideo.decodeResult")(function* (response, context) {
|
|
46
|
-
const output = yield* decodeQueueResult(response);
|
|
47
|
-
const { video, seed, ...rest } = output.value;
|
|
48
|
-
return new VideoResponse({
|
|
49
|
-
videos: [Media.url(video.url, { mediaType: video.content_type ?? "video/mp4" })],
|
|
50
|
-
providerMetadata: {
|
|
51
|
-
fal: {
|
|
52
|
-
requestId: context.token.requestID,
|
|
53
|
-
seed: seed ?? undefined,
|
|
54
|
-
fileName: video.file_name ?? undefined,
|
|
55
|
-
fileSize: video.file_size ?? undefined,
|
|
56
|
-
...rest,
|
|
57
|
-
},
|
|
58
|
-
},
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
// ---------------------------------------------------------------------------
|
|
62
|
-
// 7. Protocol and route
|
|
63
|
-
// ---------------------------------------------------------------------------
|
|
64
|
-
export const protocol = FalQueue.protocol(route, {
|
|
65
|
-
unsupported: ["n", "durationSeconds", "references"],
|
|
66
|
-
from: fromRequest,
|
|
67
|
-
decodeResult,
|
|
68
|
-
});
|
|
69
|
-
export const model = (input) => VideoModel.fromRoute({ protocol, baseURL: FalQueue.DEFAULT_BASE_URL, path: ({ request }) => `/${request.model.id}` }, input);
|
|
70
|
-
export const FalVideo = {
|
|
71
|
-
protocol,
|
|
72
|
-
model,
|
|
73
|
-
};
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { Schema } from "effect";
|
|
2
|
-
import { ImageModel, ImageResponse, type ImageRequestFor } from "../image.js";
|
|
3
|
-
import { MediaProtocol } from "../route/media-protocol.js";
|
|
4
|
-
import { MediaRoute } from "../route/media.js";
|
|
5
|
-
export declare const DEFAULT_BASE_URL = "https://api.replicate.com";
|
|
6
|
-
/** Model-defined `input` fields under the model's own names; `Media.Asset` values lower to URLs or data URLs. */
|
|
7
|
-
export type ReplicateImageOptions = Record<string, unknown>;
|
|
8
|
-
export type Request = ImageRequestFor<ReplicateImageOptions>;
|
|
9
|
-
export declare const Token: Schema.Struct<{
|
|
10
|
-
readonly id: Schema.String;
|
|
11
|
-
readonly getURL: Schema.String;
|
|
12
|
-
readonly cancelURL: Schema.String;
|
|
13
|
-
}>;
|
|
14
|
-
export type Token = Schema.Schema.Type<typeof Token>;
|
|
15
|
-
export declare const protocol: MediaProtocol.Queued<Request, ImageResponse, {
|
|
16
|
-
readonly id: string;
|
|
17
|
-
readonly getURL: string;
|
|
18
|
-
readonly cancelURL: string;
|
|
19
|
-
}>;
|
|
20
|
-
export declare const model: (input: MediaRoute.ModelInput) => ImageModel<ReplicateImageOptions>;
|
|
21
|
-
export declare const ReplicateImages: {
|
|
22
|
-
readonly protocol: MediaProtocol.Queued<Request, ImageResponse, {
|
|
23
|
-
readonly id: string;
|
|
24
|
-
readonly getURL: string;
|
|
25
|
-
readonly cancelURL: string;
|
|
26
|
-
}>;
|
|
27
|
-
readonly model: typeof model;
|
|
28
|
-
};
|
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import { Duration, Effect, Schema } from "effect";
|
|
2
|
-
import { ImageModel, ImageResponse } from "../image.js";
|
|
3
|
-
import { Media } from "../media.js";
|
|
4
|
-
import { MediaProtocol } from "../route/media-protocol.js";
|
|
5
|
-
import { MediaRoute } from "../route/media.js";
|
|
6
|
-
import { mergeJsonRecords } from "../schema/index.js";
|
|
7
|
-
import { ProviderShared, optionalNull } from "./shared.js";
|
|
8
|
-
const route = MediaProtocol.identity({ id: "replicate-images", name: "Replicate", provider: "replicate" });
|
|
9
|
-
export const DEFAULT_BASE_URL = "https://api.replicate.com";
|
|
10
|
-
const OUTPUT_RETENTION = Duration.hours(1);
|
|
11
|
-
const MAX_DATA_URL_BYTES = 256 * 1024;
|
|
12
|
-
// ---------------------------------------------------------------------------
|
|
13
|
-
// 2. Token and response schemas
|
|
14
|
-
// ---------------------------------------------------------------------------
|
|
15
|
-
export const Token = Schema.Struct({ id: Schema.String, getURL: Schema.String, cancelURL: Schema.String });
|
|
16
|
-
const Prediction = Schema.Struct({
|
|
17
|
-
id: Schema.String,
|
|
18
|
-
status: Schema.String,
|
|
19
|
-
output: optionalNull(Schema.Unknown),
|
|
20
|
-
error: optionalNull(Schema.Unknown),
|
|
21
|
-
data_removed: optionalNull(Schema.Boolean),
|
|
22
|
-
completed_at: optionalNull(Schema.String),
|
|
23
|
-
metrics: optionalNull(Schema.Struct({ predict_time: optionalNull(Schema.Number) })),
|
|
24
|
-
urls: Schema.Struct({ get: Schema.String, cancel: Schema.String }),
|
|
25
|
-
});
|
|
26
|
-
const Output = Schema.Union([Schema.String, Schema.Array(Schema.String)]);
|
|
27
|
-
const isOutput = Schema.is(Output);
|
|
28
|
-
const STATUS = {
|
|
29
|
-
starting: "queued",
|
|
30
|
-
processing: "running",
|
|
31
|
-
succeeded: "completed",
|
|
32
|
-
failed: "failed",
|
|
33
|
-
canceled: "cancelled",
|
|
34
|
-
// The prediction hit its `Cancel-After` deadline before it started running.
|
|
35
|
-
aborted: "expired",
|
|
36
|
-
};
|
|
37
|
-
// ---------------------------------------------------------------------------
|
|
38
|
-
// 5. Request body construction
|
|
39
|
-
// ---------------------------------------------------------------------------
|
|
40
|
-
// Official models run by `owner/name`; anything else (`owner/name:version` or a bare version id) is a pinned version.
|
|
41
|
-
const isOfficial = (model) => /^[^/:]+\/[^/:]+$/.test(model);
|
|
42
|
-
const inlineSize = (source) => {
|
|
43
|
-
if (source.type === "bytes")
|
|
44
|
-
return source.data.byteLength;
|
|
45
|
-
if (source.type === "base64")
|
|
46
|
-
return source.data.length * 0.75;
|
|
47
|
-
return 0;
|
|
48
|
-
};
|
|
49
|
-
const fileInput = (asset) => {
|
|
50
|
-
if (inlineSize(asset.source) > MAX_DATA_URL_BYTES)
|
|
51
|
-
return Effect.fail(ProviderShared.invalidRequest(`${route.name} data URL inputs are limited to 256 KB; pass a larger file by https URL`));
|
|
52
|
-
return ProviderShared.mediaReference(asset, undefined, route.name).pipe(Effect.map((reference) => reference.value));
|
|
53
|
-
};
|
|
54
|
-
const inputValue = (value) => {
|
|
55
|
-
if (value instanceof Media.Asset)
|
|
56
|
-
return fileInput(value);
|
|
57
|
-
if (Array.isArray(value) && value.some((item) => item instanceof Media.Asset))
|
|
58
|
-
return Effect.forEach(value, (item) => (item instanceof Media.Asset ? fileInput(item) : Effect.succeed(item)));
|
|
59
|
-
return Effect.succeed(value);
|
|
60
|
-
};
|
|
61
|
-
const fromRequest = Effect.fn("ReplicateImages.fromRequest")(function* (request) {
|
|
62
|
-
const native = yield* Effect.forEach(Object.entries(request.providerOptions ?? {}), ([key, value]) => inputValue(value).pipe(Effect.map((lowered) => [key, lowered])));
|
|
63
|
-
const input = mergeJsonRecords({ prompt: request.prompt }, Object.fromEntries(native));
|
|
64
|
-
const model = request.model.id;
|
|
65
|
-
return MediaProtocol.json(mergeJsonRecords(isOfficial(model) ? { input } : { version: model, input }, request.http?.body) ?? {});
|
|
66
|
-
});
|
|
67
|
-
// ---------------------------------------------------------------------------
|
|
68
|
-
// 6. Response decoding
|
|
69
|
-
// ---------------------------------------------------------------------------
|
|
70
|
-
const decodePrediction = route.decodeJson(Prediction);
|
|
71
|
-
const decodeStart = Effect.fn("ReplicateImages.decodeStart")(function* (response) {
|
|
72
|
-
const output = yield* decodePrediction(response);
|
|
73
|
-
const prediction = output.value;
|
|
74
|
-
return {
|
|
75
|
-
token: { id: prediction.id, getURL: prediction.urls.get, cancelURL: prediction.urls.cancel },
|
|
76
|
-
// `Prefer: wait` can return an already-finished prediction, so `await` skips straight to the result.
|
|
77
|
-
snapshot: { id: prediction.id, status: yield* MediaProtocol.status(STATUS, prediction.status, output) },
|
|
78
|
-
};
|
|
79
|
-
});
|
|
80
|
-
const decodeStatus = Effect.fn("ReplicateImages.decodeStatus")(function* (response, context) {
|
|
81
|
-
const output = yield* decodePrediction(response);
|
|
82
|
-
return { id: context.token.id, status: yield* MediaProtocol.status(STATUS, output.value.status, output) };
|
|
83
|
-
});
|
|
84
|
-
const decodeResult = Effect.fn("ReplicateImages.decodeResult")(function* (response, context) {
|
|
85
|
-
const output = yield* decodePrediction(response);
|
|
86
|
-
const prediction = output.value;
|
|
87
|
-
const status = yield* MediaProtocol.status(STATUS, prediction.status, output);
|
|
88
|
-
if (status === "failed" || status === "cancelled" || status === "expired")
|
|
89
|
-
return yield* output.ended(status, `${route.name} prediction ${context.token.id} ${prediction.status}${typeof prediction.error === "string" ? `: ${prediction.error}` : ""}`);
|
|
90
|
-
if (status !== "completed")
|
|
91
|
-
return yield* output.pending(context.token.id);
|
|
92
|
-
if (prediction.data_removed === true)
|
|
93
|
-
return yield* output.ended("expired", `${route.name} removed the output of prediction ${context.token.id}`);
|
|
94
|
-
if (!isOutput(prediction.output))
|
|
95
|
-
return yield* output.invalid(`${route.name} prediction ${context.token.id} returned output that is not image URLs`);
|
|
96
|
-
const urls = typeof prediction.output === "string" ? [prediction.output] : prediction.output;
|
|
97
|
-
if (urls.length === 0)
|
|
98
|
-
return yield* output.invalid(`${route.name} prediction ${context.token.id} returned no images`);
|
|
99
|
-
const predictTime = prediction.metrics?.predict_time ?? undefined;
|
|
100
|
-
const completedAt = prediction.completed_at ?? undefined;
|
|
101
|
-
const expiresAt = completedAt === undefined ? undefined : Date.parse(completedAt) + Duration.toMillis(OUTPUT_RETENTION);
|
|
102
|
-
return new ImageResponse({
|
|
103
|
-
images: urls.map((url) => Media.url(url, { expiresAt })),
|
|
104
|
-
usage: predictTime === undefined ? undefined : { type: "compute", seconds: predictTime },
|
|
105
|
-
providerMetadata: { replicate: { predictionId: prediction.id } },
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
// ---------------------------------------------------------------------------
|
|
109
|
-
// 7. Protocol and route
|
|
110
|
-
// ---------------------------------------------------------------------------
|
|
111
|
-
export const protocol = MediaProtocol.queued(route, {
|
|
112
|
-
token: Token,
|
|
113
|
-
unsupported: ["images", "n", "aspect", "resolution"],
|
|
114
|
-
start: { body: { from: fromRequest }, decode: decodeStart },
|
|
115
|
-
status: { path: (token) => token.getURL, decode: decodeStatus },
|
|
116
|
-
result: { path: (token) => token.getURL, decode: decodeResult },
|
|
117
|
-
cancel: { method: "POST", path: (token) => token.cancelURL },
|
|
118
|
-
});
|
|
119
|
-
export const model = (input) => ImageModel.fromRoute({
|
|
120
|
-
protocol,
|
|
121
|
-
baseURL: DEFAULT_BASE_URL,
|
|
122
|
-
path: ({ request }) => isOfficial(request.model.id) ? `/v1/models/${request.model.id}/predictions` : "/v1/predictions",
|
|
123
|
-
}, input);
|
|
124
|
-
export const ReplicateImages = {
|
|
125
|
-
protocol,
|
|
126
|
-
model,
|
|
127
|
-
};
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { Schema } from "effect";
|
|
2
|
-
import { ImageModel, ImageResponse, type ImageRequestFor } from "../image.js";
|
|
3
|
-
import { MediaProtocol } from "../route/media-protocol.js";
|
|
4
|
-
import { MediaRoute } from "../route/media.js";
|
|
5
|
-
import { type OpenString } from "../schema/index.js";
|
|
6
|
-
export declare const DEFAULT_BASE_URL = "https://api.stability.ai";
|
|
7
|
-
export type StabilityStylePreset = OpenString<"enhance" | "anime" | "photographic" | "digital-art" | "comic-book" | "fantasy-art" | "line-art" | "analog-film" | "neon-punk" | "isometric" | "low-poly" | "origami" | "modeling-compound" | "cinematic" | "3d-model" | "pixel-art" | "tile-texture">;
|
|
8
|
-
/** Native form fields; `aspect` lowers to `aspect_ratio`. */
|
|
9
|
-
export type StabilityImageOptions = {
|
|
10
|
-
readonly seed?: number;
|
|
11
|
-
readonly output_format?: OpenString<"jpeg" | "png" | "webp">;
|
|
12
|
-
readonly negative_prompt?: string;
|
|
13
|
-
readonly style_preset?: StabilityStylePreset;
|
|
14
|
-
readonly strength?: number;
|
|
15
|
-
readonly cfg_scale?: number;
|
|
16
|
-
} & Record<string, unknown>;
|
|
17
|
-
/** Creative upscale options; the one `images` source is the image to upscale. */
|
|
18
|
-
export type StabilityUpscaleOptions = {
|
|
19
|
-
readonly seed?: number;
|
|
20
|
-
readonly output_format?: OpenString<"jpeg" | "png" | "webp">;
|
|
21
|
-
readonly negative_prompt?: string;
|
|
22
|
-
readonly style_preset?: StabilityStylePreset;
|
|
23
|
-
readonly creativity?: number;
|
|
24
|
-
} & Record<string, unknown>;
|
|
25
|
-
export type Request = ImageRequestFor<StabilityImageOptions>;
|
|
26
|
-
export type UpscaleRequest = ImageRequestFor<StabilityUpscaleOptions>;
|
|
27
|
-
export declare const Token: Schema.Struct<{
|
|
28
|
-
readonly id: Schema.String;
|
|
29
|
-
}>;
|
|
30
|
-
export type Token = Schema.Schema.Type<typeof Token>;
|
|
31
|
-
export declare const protocol: MediaProtocol.Inline<Request, ImageResponse>;
|
|
32
|
-
export declare const upscaleProtocol: MediaProtocol.Queued<UpscaleRequest, ImageResponse, {
|
|
33
|
-
readonly id: string;
|
|
34
|
-
}>;
|
|
35
|
-
export declare const model: (input: MediaRoute.ModelInput) => ImageModel<StabilityImageOptions>;
|
|
36
|
-
export declare const upscaleModel: (input: Omit<MediaRoute.ModelInput, "id">) => ImageModel<StabilityUpscaleOptions>;
|
|
37
|
-
export declare const StabilityImages: {
|
|
38
|
-
readonly protocol: MediaProtocol.Inline<Request, ImageResponse>;
|
|
39
|
-
readonly upscaleProtocol: MediaProtocol.Queued<UpscaleRequest, ImageResponse, {
|
|
40
|
-
readonly id: string;
|
|
41
|
-
}>;
|
|
42
|
-
readonly model: typeof model;
|
|
43
|
-
readonly upscaleModel: typeof upscaleModel;
|
|
44
|
-
};
|