@opencode/ai 0.0.0-dev-21741 → 0.0.0-dev-21745
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 +50 -43
- package/dist/image.d.ts +6 -41
- package/dist/image.js +3 -8
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/protocols/alibaba-images.d.ts +4 -1
- package/dist/protocols/alibaba-images.js +3 -2
- package/dist/protocols/bfl-images.d.ts +9 -0
- package/dist/protocols/bfl-images.js +17 -15
- package/dist/protocols/cloudflare-workers-ai-images.d.ts +7 -1
- package/dist/protocols/cloudflare-workers-ai-images.js +3 -3
- package/dist/protocols/fal-images.d.ts +10 -1
- package/dist/protocols/fal-images.js +15 -16
- package/dist/protocols/fal-video.js +1 -1
- package/dist/protocols/google-images.d.ts +2 -2
- package/dist/protocols/google-images.js +2 -4
- package/dist/protocols/google-video.d.ts +1 -1
- package/dist/protocols/google-video.js +1 -1
- package/dist/protocols/meta-images.d.ts +7 -1
- package/dist/protocols/meta-images.js +15 -4
- package/dist/protocols/openai-images.d.ts +6 -1
- package/dist/protocols/openai-images.js +45 -5
- package/dist/protocols/replicate-images.js +1 -1
- package/dist/protocols/runway-video.d.ts +1 -1
- package/dist/protocols/runway-video.js +1 -1
- package/dist/protocols/stability-images.d.ts +5 -0
- package/dist/protocols/stability-images.js +4 -6
- package/dist/protocols/utils/image-sizing.d.ts +34 -0
- package/dist/protocols/utils/image-sizing.js +79 -0
- package/dist/protocols/utils/media-input.d.ts +0 -4
- package/dist/protocols/utils/media-input.js +0 -4
- package/dist/protocols/xai-images.d.ts +1 -2
- package/dist/protocols/xai-images.js +15 -3
- package/dist/protocols/xai-video.d.ts +1 -1
- package/dist/protocols/xai-video.js +1 -1
- package/dist/protocols/zai-images.d.ts +3 -1
- package/dist/protocols/zai-images.js +12 -2
- package/dist/video.d.ts +1 -1
- package/dist/video.js +2 -2
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -520,13 +520,13 @@ const edit = Image.generate({
|
|
|
520
520
|
model: meta.image("muse-image-1.0"),
|
|
521
521
|
prompt: "Make the square purple.",
|
|
522
522
|
images: [Media.bytes(imageBytes, "image/webp")],
|
|
523
|
-
|
|
524
|
-
providerOptions: { reasoningStrength: "low" },
|
|
523
|
+
providerOptions: { reasoningStrength: "low", outputFormat: "png" },
|
|
525
524
|
})
|
|
526
525
|
```
|
|
527
526
|
|
|
528
|
-
The default image format is WEBP; `
|
|
529
|
-
returns a signed URL. `
|
|
527
|
+
The default image format is WEBP; `outputFormat` also accepts PNG/JPEG and `responseFormat: "url"`
|
|
528
|
+
returns a signed URL. `aspect` lowers to Meta's `size`, which is only an aspect-ratio hint: Meta renders about 2.5
|
|
529
|
+
megapixels whatever you ask, so `resolution` is ignored with a notice. For conversational images, select
|
|
530
530
|
`meta.responses("muse-image-1.0")` with `tools: [Meta.imageGeneration({ reasoningStrength: "low" })]`.
|
|
531
531
|
Generated images are provider-executed tool results with file content. Retain `response.message` to
|
|
532
532
|
replay the signed image handle on the next request. Muse Image accepts only the `image_generation` tool.
|
|
@@ -538,8 +538,9 @@ Meta Responses is explicitly HTTP/SSE-only and does not use WebSockets, even whe
|
|
|
538
538
|
|
|
539
539
|
Use `Image.generate` with an image model for direct asset generation. `Image.request` mirrors `LLM.request`: the
|
|
540
540
|
model comes from the facade's `.image(...)` selector (mirroring `.responses(...)`), common fields
|
|
541
|
-
(`images`, `
|
|
542
|
-
`
|
|
541
|
+
(`images`, `n`, `aspect`, `resolution`) lower natively or fail typed, and `providerOptions` is inferred from the
|
|
542
|
+
selected model. `aspect` is a ratio such as `"16:9"`; `resolution` is a size tier (`"1K"` is about 1024×1024 pixels
|
|
543
|
+
at that ratio, `"2K"` about 2048×2048). Exact sizes, seeds, masks, and output formats are provider options:
|
|
543
544
|
|
|
544
545
|
```ts
|
|
545
546
|
import { Image, Media } from "@opencode/ai"
|
|
@@ -552,9 +553,10 @@ const program = Effect.gen(function* () {
|
|
|
552
553
|
model: openai.image("gpt-image-2"),
|
|
553
554
|
prompt: "A robot tending a rooftop garden",
|
|
554
555
|
n: 2,
|
|
555
|
-
|
|
556
|
-
|
|
556
|
+
aspect: "16:9",
|
|
557
|
+
resolution: "2K",
|
|
557
558
|
providerOptions: {
|
|
559
|
+
outputFormat: "webp",
|
|
558
560
|
quality: "high", // inferred from the OpenAI image model
|
|
559
561
|
future_option: true, // unknown native options pass through unchanged
|
|
560
562
|
},
|
|
@@ -564,29 +566,34 @@ const program = Effect.gen(function* () {
|
|
|
564
566
|
})
|
|
565
567
|
```
|
|
566
568
|
|
|
567
|
-
Common fields are portable in shape, not in support.
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
|
572
|
-
|
|
|
573
|
-
|
|
|
574
|
-
|
|
|
575
|
-
|
|
|
576
|
-
|
|
|
577
|
-
|
|
|
578
|
-
|
|
|
579
|
-
|
|
|
580
|
-
|
|
|
581
|
-
|
|
|
582
|
-
| Stability `
|
|
583
|
-
|
|
|
584
|
-
|
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
569
|
+
Common fields are portable in shape, not in support. A route that has to approximate `aspect` or `resolution`
|
|
570
|
+
returns what it used in `response.notices`; one that cannot honor them fails with a typed `AIError` before any network
|
|
571
|
+
call rather than dropping them, so check this table before swapping only the `model`:
|
|
572
|
+
|
|
573
|
+
| Provider | `n` | `aspect` | `resolution` | `images` | Moved to `providerOptions` |
|
|
574
|
+
| --------------------- | --- | -------------------- | --------------------- | -------------------------------- | -------------------------------------------------- |
|
|
575
|
+
| OpenAI | ✓¹ | nearest size, notice | nearest size, notice | ✓ | `size`, `outputFormat`, `mask` |
|
|
576
|
+
| Google (Gemini) | 1 | ✓ | ✓ (`imageSize`) | ✓ (no public URLs) | `seed` |
|
|
577
|
+
| xAI | ✓ | ✓ | `1k`/`2k`, notice | ✓ | none |
|
|
578
|
+
| Meta | ✓ | ✓ (hint) | ignored, notice | ✓ | `size`, `outputFormat` |
|
|
579
|
+
| Z.ai | ✗ | width × height | width × height | ✗ | `size` |
|
|
580
|
+
| Alibaba | ✓ | width × height | width × height | ✓ | `size`, `seed` |
|
|
581
|
+
| Black Forest Labs | 1 | per model | width × height models | per model (1–8) | `width`, `height`, `seed`, `output_format`, `mask` |
|
|
582
|
+
| fal | ✓ | per model | per model | 1 (several on `/edit`, `/multi`) | `image_size`, `seed`, `output_format`, `mask` |
|
|
583
|
+
| Replicate | ✗ | ✗ | ✗ | ✗ (use `providerOptions`) | everything |
|
|
584
|
+
| Stability `image` | 1 | ✓ | ✗ | 1 (not on `core`) | `seed`, `output_format` |
|
|
585
|
+
| Stability `upscale()` | ✗ | ✗ | ✗ | exactly 1 (required) | `seed`, `output_format` |
|
|
586
|
+
| Vercel AI Gateway | ✓ | as OpenAI³ | as OpenAI³ | per model | as OpenAI |
|
|
587
|
+
| Cloudflare Workers AI | 1 | width × height² | width × height² | ✗ | `width`, `height`, `seed` |
|
|
588
|
+
|
|
589
|
+
✓ lowers natively; ✗ fails whenever the field is set (including `n: 1`); `1` means `n > 1` fails. "width × height"
|
|
590
|
+
computes a size the provider accepts and fails when the request falls outside its limits. ¹ `Image.stream` on OpenAI
|
|
591
|
+
generates one image. On OpenAI, `gpt-image-2` and later get a computed size; older models get the nearest of their
|
|
592
|
+
fixed sizes. `providerOptions.size` replaces `aspect` and `resolution`, so setting both fails. Vercel AI Gateway
|
|
593
|
+
forwards `outputFormat` but does not apply it, so the returned bytes keep the upstream model's own format. ² Workers AI
|
|
594
|
+
`@cf/black-forest-labs/flux-1-schnell` takes no size, and the provider rejects it. ³ Non-OpenAI gateway models
|
|
595
|
+
(`bfl/flux-2-pro`) get a computed width × height, which the gateway passes through; models with a fixed list, such as
|
|
596
|
+
Recraft, reject sizes outside it.
|
|
590
597
|
|
|
591
598
|
`Media.Asset` is the one asset type shared by image requests, image responses, LLM messages, and tool results.
|
|
592
599
|
`asset.source` is the serializable `Media.Source` (`bytes`, `base64`, `url`, or `ref`); `asset.bytes()`,
|
|
@@ -618,15 +625,15 @@ only forward refs that belong to their own provider (OpenAI, xAI, and Gemini ima
|
|
|
618
625
|
returns a ref yet, and `asset.bytes()` / `materialize()` on a ref fail by design. Raw strings are not accepted as
|
|
619
626
|
image inputs, avoiding ambiguity between base64, URLs, and provider IDs. Empty or omitted `images` uses text-to-image generation; a
|
|
620
627
|
non-empty array selects the provider's edit behavior (see the table above for routes that limit the count). OpenAI
|
|
621
|
-
uses multipart for byte/data-URL edits and its JSON reference body for URL or file-ID edits.
|
|
622
|
-
|
|
628
|
+
uses multipart for byte/data-URL edits and its JSON reference body for URL or file-ID edits. Inpainting is a
|
|
629
|
+
provider option on the routes that support it (OpenAI, fal, and BFL `flux-pro-1.0-fill`):
|
|
623
630
|
|
|
624
631
|
```ts
|
|
625
632
|
const inpainted = Image.generate({
|
|
626
633
|
model: openai.image("gpt-image-2"),
|
|
627
634
|
prompt,
|
|
628
635
|
images: [Media.bytes(sourceBytes, "image/png")],
|
|
629
|
-
mask: Media.bytes(maskBytes, "image/png"),
|
|
636
|
+
providerOptions: { mask: Media.bytes(maskBytes, "image/png") },
|
|
630
637
|
})
|
|
631
638
|
```
|
|
632
639
|
|
|
@@ -655,9 +662,9 @@ const xai = Image.generate({
|
|
|
655
662
|
model: XAI.configure({ apiKey }).image("any-model-id"),
|
|
656
663
|
prompt,
|
|
657
664
|
n: 2,
|
|
658
|
-
|
|
665
|
+
aspect: "16:9",
|
|
666
|
+
resolution: "1K",
|
|
659
667
|
providerOptions: {
|
|
660
|
-
resolution: "1k",
|
|
661
668
|
responseFormat: "b64_json",
|
|
662
669
|
future_option: true,
|
|
663
670
|
},
|
|
@@ -674,10 +681,10 @@ const googleProgram = Effect.gen(function* () {
|
|
|
674
681
|
const response = yield* Image.generate({
|
|
675
682
|
model: Google.configure({ apiKey }).image("any-model-id"),
|
|
676
683
|
prompt: "A robot tending a rooftop garden",
|
|
677
|
-
|
|
678
|
-
|
|
684
|
+
aspect: "16:9",
|
|
685
|
+
resolution: "2K",
|
|
679
686
|
providerOptions: {
|
|
680
|
-
|
|
687
|
+
seed: 42,
|
|
681
688
|
thinkingLevel: "HIGH",
|
|
682
689
|
includeThoughts: true,
|
|
683
690
|
futureOption: true,
|
|
@@ -745,7 +752,7 @@ import { BlackForestLabs, Stability } from "@opencode/ai/providers"
|
|
|
745
752
|
const bfl = BlackForestLabs.configure({ apiKey: process.env.BFL_API_KEY })
|
|
746
753
|
|
|
747
754
|
const submit = Effect.gen(function* () {
|
|
748
|
-
const generation = yield* Image.start({ model: bfl.image("flux-2-pro"), prompt,
|
|
755
|
+
const generation = yield* Image.start({ model: bfl.image("flux-2-pro"), prompt, aspect: "4:3" })
|
|
749
756
|
persist({ provider: "black-forest-labs", modelID: "flux-2-pro", token: generation.token })
|
|
750
757
|
})
|
|
751
758
|
|
|
@@ -801,7 +808,7 @@ The hosted result is represented as a provider-executed tool call and a tool res
|
|
|
801
808
|
|
|
802
809
|
Video mirrors `Image` with one difference: every provider is asynchronous, so the route is a submit-then-poll
|
|
803
810
|
`Generation`. Models come from `.video(...)` selectors on the `Google` (Veo), `XAI`, `Fal`, and `Runway` facades.
|
|
804
|
-
Common fields (`frames`, `references`, `video`, `durationSeconds`, `
|
|
811
|
+
Common fields (`frames`, `references`, `video`, `durationSeconds`, `aspect`, `resolution`, `audio`, `n`, `seed`,
|
|
805
812
|
`negativePrompt`) lower natively or fail with a typed `AIError` before any network call; provider-native controls live
|
|
806
813
|
under `providerOptions`, inferred from the selected model.
|
|
807
814
|
|
|
@@ -817,7 +824,7 @@ const program = Effect.gen(function* () {
|
|
|
817
824
|
{
|
|
818
825
|
model: google.video("veo-3.1-generate-preview"),
|
|
819
826
|
prompt: "Panning wide shot of a calico kitten sleeping in the sunshine",
|
|
820
|
-
|
|
827
|
+
aspect: "16:9",
|
|
821
828
|
resolution: "1080p",
|
|
822
829
|
durationSeconds: 8,
|
|
823
830
|
providerOptions: { personGeneration: "allow_adult" },
|
|
@@ -867,7 +874,7 @@ Provider notes:
|
|
|
867
874
|
- **fal** endpoints are model-specific: `durationSeconds`, `references`, and `frames.last` fail typed and belong in
|
|
868
875
|
`providerOptions` under the model's own names (`duration: "8s"`, `end_image_url`, …). Auth is
|
|
869
876
|
`Authorization: Key <FAL_KEY>`.
|
|
870
|
-
- **Runway** expects pixel ratios in `
|
|
877
|
+
- **Runway** expects pixel ratios in `aspect` for most models (`"1280:720"`), pins `X-Runway-Version`, reports
|
|
871
878
|
`usage: { type: "credits" }`, and its output URLs expire after 24–48 hours.
|
|
872
879
|
|
|
873
880
|
The promise client exposes the same surface: `ai.video.start(...)` resolves to a handle with `await`, `events`,
|
package/dist/image.d.ts
CHANGED
|
@@ -19,11 +19,10 @@ export declare const ImageModelSchema: Schema.declare<ImageModel<{
|
|
|
19
19
|
}>, ImageModel<{
|
|
20
20
|
readonly [x: string]: unknown;
|
|
21
21
|
}>>;
|
|
22
|
-
export type ImageSize = `${number}x${number}`;
|
|
23
|
-
export declare const ImageSize: Schema.declare<`${number}x${number}`, `${number}x${number}`>;
|
|
24
22
|
export type ImageAspectRatio = Media.AspectRatio;
|
|
25
23
|
export declare const ImageAspectRatio: Schema.declare<`${number}:${number}`, `${number}:${number}`>;
|
|
26
|
-
|
|
24
|
+
/** Size tier: `1K` is about as many pixels as 1024×1024 at the requested aspect, `2K` as 2048×2048. */
|
|
25
|
+
export type ImageResolution = OpenString<"1K" | "2K" | "4K">;
|
|
27
26
|
declare const ImageRequest_base: Schema.Class<ImageRequest, Schema.Struct<{
|
|
28
27
|
readonly model: Schema.declare<ImageModel<{
|
|
29
28
|
readonly [x: string]: unknown;
|
|
@@ -63,43 +62,9 @@ declare const ImageRequest_base: Schema.Class<ImageRequest, Schema.Struct<{
|
|
|
63
62
|
}>>;
|
|
64
63
|
readonly providerMetadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
|
|
65
64
|
}>, never, never>>>;
|
|
66
|
-
/** Inpainting mask; routes that cannot honor it fail with `UnsupportedOperation`. */
|
|
67
|
-
readonly mask: Schema.optional<Schema.decodeTo<Schema.declare<Media.Asset, Media.Asset>, Schema.Struct<{
|
|
68
|
-
readonly source: Schema.Union<readonly [Schema.Struct<{
|
|
69
|
-
readonly type: Schema.Literal<"bytes">;
|
|
70
|
-
readonly data: Schema.Uint8Array;
|
|
71
|
-
readonly mediaType: Schema.String;
|
|
72
|
-
}>, Schema.Struct<{
|
|
73
|
-
readonly type: Schema.Literal<"base64">;
|
|
74
|
-
readonly data: Schema.String;
|
|
75
|
-
readonly mediaType: Schema.String;
|
|
76
|
-
}>, Schema.Struct<{
|
|
77
|
-
readonly type: Schema.Literal<"url">;
|
|
78
|
-
readonly url: Schema.String;
|
|
79
|
-
readonly mediaType: Schema.optional<Schema.String>;
|
|
80
|
-
readonly expiresAt: Schema.optional<Schema.Number>;
|
|
81
|
-
}>, Schema.Struct<{
|
|
82
|
-
readonly type: Schema.Literal<"ref">;
|
|
83
|
-
readonly provider: Schema.brand<Schema.String, "AI.ProviderID">;
|
|
84
|
-
readonly id: Schema.String;
|
|
85
|
-
readonly mediaType: Schema.optional<Schema.String>;
|
|
86
|
-
}>]>;
|
|
87
|
-
readonly info: Schema.optional<Schema.Struct<{
|
|
88
|
-
readonly width: Schema.optional<Schema.Number>;
|
|
89
|
-
readonly height: Schema.optional<Schema.Number>;
|
|
90
|
-
readonly durationSeconds: Schema.optional<Schema.Number>;
|
|
91
|
-
readonly sampleRate: Schema.optional<Schema.Number>;
|
|
92
|
-
readonly channels: Schema.optional<Schema.Number>;
|
|
93
|
-
readonly encoding: Schema.optional<Schema.String>;
|
|
94
|
-
readonly format: Schema.optional<Schema.String>;
|
|
95
|
-
}>>;
|
|
96
|
-
readonly providerMetadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
|
|
97
|
-
}>, never, never>>;
|
|
98
65
|
readonly n: Schema.optional<Schema.Int>;
|
|
99
|
-
readonly
|
|
100
|
-
readonly
|
|
101
|
-
readonly seed: Schema.optional<Schema.Number>;
|
|
102
|
-
readonly format: Schema.optional<Schema.String>;
|
|
66
|
+
readonly aspect: Schema.optional<Schema.declare<`${number}:${number}`, `${number}:${number}`>>;
|
|
67
|
+
readonly resolution: Schema.optional<Schema.String>;
|
|
103
68
|
readonly providerOptions: Schema.optional<Schema.$Record<Schema.String, Schema.Unknown>>;
|
|
104
69
|
readonly http: Schema.optional<typeof HttpOptions>;
|
|
105
70
|
}>, {}>;
|
|
@@ -111,9 +76,9 @@ export type ImageRequestFor<Options extends ImageOptions = ImageOptions> = Omit<
|
|
|
111
76
|
readonly providerOptions?: Options;
|
|
112
77
|
};
|
|
113
78
|
export type ImageModelOptions<Model> = Model extends ImageModel<infer Options> ? Options : never;
|
|
114
|
-
export type ImageRequestInput<Model extends ImageModel = ImageModel> = Omit<ConstructorParameters<typeof ImageRequest>[0], "model" | "providerOptions" | "http"> & {
|
|
79
|
+
export type ImageRequestInput<Model extends ImageModel = ImageModel> = Omit<ConstructorParameters<typeof ImageRequest>[0], "model" | "providerOptions" | "http" | "resolution"> & {
|
|
115
80
|
readonly model: Model;
|
|
116
|
-
readonly
|
|
81
|
+
readonly resolution?: ImageResolution;
|
|
117
82
|
readonly providerOptions?: NoInfer<ImageModelOptions<Model>>;
|
|
118
83
|
readonly http?: HttpOptions.Input;
|
|
119
84
|
};
|
package/dist/image.js
CHANGED
|
@@ -3,7 +3,7 @@ import { Generation, ProgressEvent, QueuedEvent } from "./generation.js";
|
|
|
3
3
|
import { Media } from "./media.js";
|
|
4
4
|
import { MediaModel, composeRoute, tryRequest } from "./media-model.js";
|
|
5
5
|
import { MediaRoute } from "./route/media.js";
|
|
6
|
-
import { AIError, HttpOptions, MediaUsage, ProviderMetadata, ProviderOptions
|
|
6
|
+
import { AIError, HttpOptions, MediaUsage, ProviderMetadata, ProviderOptions } from "./schema/index.js";
|
|
7
7
|
import { ImageClient, Service } from "./image-client.js";
|
|
8
8
|
export class ImageModel extends MediaModel {
|
|
9
9
|
static fromRoute(route, input) {
|
|
@@ -18,20 +18,15 @@ export class ImageModel extends MediaModel {
|
|
|
18
18
|
export const ImageModelSchema = Schema.declare((value) => value instanceof ImageModel, {
|
|
19
19
|
expected: "Image.Model",
|
|
20
20
|
});
|
|
21
|
-
export const ImageSize = Schema.declare((value) => typeof value === "string" && /^\d+x\d+$/.test(value), { title: "ImageSize" });
|
|
22
21
|
export const ImageAspectRatio = Media.AspectRatio;
|
|
23
22
|
export class ImageRequest extends Schema.Class("Image.Request")({
|
|
24
23
|
model: ImageModelSchema,
|
|
25
24
|
prompt: Schema.String,
|
|
26
25
|
/** Edit sources or style/subject references, in order. */
|
|
27
26
|
images: Schema.optional(Schema.Array(Media.AssetSchema)),
|
|
28
|
-
/** Inpainting mask; routes that cannot honor it fail with `UnsupportedOperation`. */
|
|
29
|
-
mask: Schema.optional(Media.AssetSchema),
|
|
30
27
|
n: Schema.optional(Schema.Int),
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
seed: Schema.optional(Schema.Number),
|
|
34
|
-
format: Schema.optional(Schema.String),
|
|
28
|
+
aspect: Schema.optional(ImageAspectRatio),
|
|
29
|
+
resolution: Schema.optional(Schema.String),
|
|
35
30
|
providerOptions: Schema.optional(ProviderOptions),
|
|
36
31
|
http: Schema.optional(HttpOptions),
|
|
37
32
|
}) {
|
package/dist/index.d.ts
CHANGED
|
@@ -7,8 +7,8 @@ export { ProviderPackage } from "./provider-package.js";
|
|
|
7
7
|
export { isContextOverflow, isContextOverflowFailure, isRetryable } from "./provider-error.js";
|
|
8
8
|
export type { RouteLanguageModelInput, RouteRoutedLanguageModelInput, Interface as LLMClientShape, LLMClientService, } from "./route/client.js";
|
|
9
9
|
export * from "./schema/index.js";
|
|
10
|
-
export { ImageAspectRatio, ImageEvent, ImageModel, ImageModelSchema, ImageRequest, ImageResponse
|
|
11
|
-
export type {
|
|
10
|
+
export { ImageAspectRatio, ImageEvent, ImageModel, ImageModelSchema, ImageRequest, ImageResponse } from "./image.js";
|
|
11
|
+
export type { ImageModelOptions, ImageOptions, ImageRequestFor, ImageRequestInput, ImageResolution, ImageRoute, } from "./image.js";
|
|
12
12
|
export { Image } from "./image.js";
|
|
13
13
|
export { VideoClient } from "./video-client.js";
|
|
14
14
|
export { VideoAspectRatio, VideoEvent, VideoFrames, VideoModel, VideoModelSchema, VideoRequest, VideoResponse, } from "./video.js";
|
package/dist/index.js
CHANGED
|
@@ -6,7 +6,7 @@ export { Provider } from "./provider.js";
|
|
|
6
6
|
export { ProviderPackage } from "./provider-package.js";
|
|
7
7
|
export { isContextOverflow, isContextOverflowFailure, isRetryable } from "./provider-error.js";
|
|
8
8
|
export * from "./schema/index.js";
|
|
9
|
-
export { ImageAspectRatio, ImageEvent, ImageModel, ImageModelSchema, ImageRequest, ImageResponse
|
|
9
|
+
export { ImageAspectRatio, ImageEvent, ImageModel, ImageModelSchema, ImageRequest, ImageResponse } from "./image.js";
|
|
10
10
|
export { Image } from "./image.js";
|
|
11
11
|
export { VideoClient } from "./video-client.js";
|
|
12
12
|
export { VideoAspectRatio, VideoEvent, VideoFrames, VideoModel, VideoModelSchema, VideoRequest, VideoResponse, } from "./video.js";
|
|
@@ -2,8 +2,11 @@ import { ImageModel, ImageResponse, type ImageRequestFor } from "../image.js";
|
|
|
2
2
|
import { MediaProtocol } from "../route/media-protocol.js";
|
|
3
3
|
import { MediaRoute } from "../route/media.js";
|
|
4
4
|
export declare const PATH = "/services/aigc/multimodal-generation/generation";
|
|
5
|
-
/** Provider-native `parameters`. Common fields (`n`, `
|
|
5
|
+
/** Provider-native `parameters`. Common fields (`n`, `aspect`, `resolution`, `images`) live on the request. */
|
|
6
6
|
export type AlibabaImageOptions = {
|
|
7
|
+
/** Exact `WIDTH*HEIGHT`; it wins over `aspect` and `resolution`. */
|
|
8
|
+
readonly size?: string;
|
|
9
|
+
readonly seed?: number;
|
|
7
10
|
readonly negativePrompt?: string;
|
|
8
11
|
readonly promptExtend?: boolean;
|
|
9
12
|
readonly watermark?: boolean;
|
|
@@ -4,6 +4,7 @@ import { MediaProtocol } from "../route/media-protocol.js";
|
|
|
4
4
|
import { MediaRoute } from "../route/media.js";
|
|
5
5
|
import { mergeJsonRecords } from "../schema/index.js";
|
|
6
6
|
import { JsonObject, ProviderShared, lenient } from "./shared.js";
|
|
7
|
+
import { ImageSizing } from "./utils/image-sizing.js";
|
|
7
8
|
const route = MediaProtocol.identity({ id: "alibaba-images", name: "Alibaba Images", provider: "alibaba" });
|
|
8
9
|
export const PATH = "/services/aigc/multimodal-generation/generation";
|
|
9
10
|
const OUTPUT_RETENTION = Duration.hours(24);
|
|
@@ -38,10 +39,11 @@ const nativeOptions = (options) => {
|
|
|
38
39
|
};
|
|
39
40
|
const fromRequest = Effect.fn("AlibabaImages.fromRequest")(function* (request) {
|
|
40
41
|
const images = yield* Effect.forEach(request.images ?? [], (image) => ProviderShared.mediaReference(image, undefined, `${route.name} input`).pipe(Effect.map((item) => ({ image: item.value }))));
|
|
42
|
+
const size = yield* ImageSizing.exact(route, request, { multiple: 16, minPixels: 512 * 512, maxPixels: 2048 * 2048 });
|
|
41
43
|
return MediaProtocol.json(mergeJsonRecords({
|
|
42
44
|
model: request.model.id,
|
|
43
45
|
input: { messages: [{ role: "user", content: [...images, { text: request.prompt }] }] },
|
|
44
|
-
parameters: mergeJsonRecords({ size:
|
|
46
|
+
parameters: mergeJsonRecords({ size: size === undefined ? undefined : `${size.width}*${size.height}`, n: request.n }, nativeOptions(request.providerOptions)),
|
|
45
47
|
}, request.http?.body) ?? {});
|
|
46
48
|
});
|
|
47
49
|
// ---------------------------------------------------------------------------
|
|
@@ -65,7 +67,6 @@ const decodeResponse = Effect.fn("AlibabaImages.decodeResponse")(function* (resp
|
|
|
65
67
|
// 7. Protocol and route
|
|
66
68
|
// ---------------------------------------------------------------------------
|
|
67
69
|
export const protocol = MediaProtocol.inline(route, {
|
|
68
|
-
unsupported: ["mask", "aspectRatio", "format"],
|
|
69
70
|
body: { from: fromRequest },
|
|
70
71
|
response: { decode: decodeResponse },
|
|
71
72
|
});
|
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
import { Schema } from "effect";
|
|
2
2
|
import { ImageModel, ImageResponse, type ImageRequestFor } from "../image.js";
|
|
3
|
+
import { Media } from "../media.js";
|
|
3
4
|
import { MediaProtocol } from "../route/media-protocol.js";
|
|
4
5
|
import { MediaRoute } from "../route/media.js";
|
|
6
|
+
import { type OpenString } from "../schema/index.js";
|
|
5
7
|
export declare const DEFAULT_BASE_URL = "https://api.bfl.ai";
|
|
8
|
+
/** Native body fields; `aspect` and `resolution` lower to `width`/`height` or `aspect_ratio` depending on the model. */
|
|
6
9
|
export type BlackForestLabsImageOptions = {
|
|
10
|
+
readonly width?: number;
|
|
11
|
+
readonly height?: number;
|
|
12
|
+
readonly seed?: number;
|
|
13
|
+
readonly output_format?: OpenString<"jpeg" | "png" | "webp">;
|
|
14
|
+
/** Inpainting mask for `flux-pro-1.0-fill`. */
|
|
15
|
+
readonly mask?: Media.Asset;
|
|
7
16
|
readonly safety_tolerance?: number;
|
|
8
17
|
readonly prompt_upsampling?: boolean;
|
|
9
18
|
readonly disable_pup?: boolean;
|
|
@@ -5,7 +5,7 @@ import { MediaProtocol } from "../route/media-protocol.js";
|
|
|
5
5
|
import { MediaRoute } from "../route/media.js";
|
|
6
6
|
import { mergeJsonRecords } from "../schema/index.js";
|
|
7
7
|
import { ProviderShared, optionalNull } from "./shared.js";
|
|
8
|
-
import {
|
|
8
|
+
import { ImageSizing } from "./utils/image-sizing.js";
|
|
9
9
|
const route = MediaProtocol.identity({ id: "bfl-images", name: "Black Forest Labs", provider: "black-forest-labs" });
|
|
10
10
|
export const DEFAULT_BASE_URL = "https://api.bfl.ai";
|
|
11
11
|
// ---------------------------------------------------------------------------
|
|
@@ -43,6 +43,8 @@ const STATUS = {
|
|
|
43
43
|
"Task not found": "expired",
|
|
44
44
|
};
|
|
45
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 };
|
|
46
48
|
const capabilities = (model) => {
|
|
47
49
|
if (model.startsWith("flux-pro-1.0-fill"))
|
|
48
50
|
return { sizing: "none", imageField: "image", maxImages: 1, mask: true };
|
|
@@ -53,23 +55,23 @@ const capabilities = (model) => {
|
|
|
53
55
|
if (model.startsWith("flux-pro-1.1-ultra"))
|
|
54
56
|
return { sizing: "aspectRatio", imageField: "image_prompt", maxImages: 1, mask: false };
|
|
55
57
|
if (model.startsWith("flux-pro-1.1") || model.startsWith("flux-dev"))
|
|
56
|
-
return { sizing:
|
|
58
|
+
return { sizing: LEGACY_LIMITS, imageField: "image_prompt", maxImages: 1, mask: false };
|
|
57
59
|
if (model.startsWith("flux-2-klein"))
|
|
58
|
-
return { sizing:
|
|
59
|
-
return { sizing:
|
|
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 };
|
|
60
62
|
};
|
|
61
63
|
const validate = (request, model) => {
|
|
62
64
|
const id = request.model.id;
|
|
63
65
|
const images = request.images?.length ?? 0;
|
|
64
66
|
if (request.n !== undefined && request.n > 1)
|
|
65
67
|
return Effect.fail(route.unsupported("media.n", `${id} generates one image per request; call it once per image`));
|
|
66
|
-
if (request.
|
|
67
|
-
return Effect.fail(route.unsupported("media.
|
|
68
|
-
if (request.
|
|
69
|
-
return Effect.fail(route.unsupported("media.
|
|
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`));
|
|
70
72
|
if (images > model.maxImages)
|
|
71
73
|
return Effect.fail(route.unsupported("media.images", `${id} takes at most ${model.maxImages} images`));
|
|
72
|
-
if (request.mask !== undefined && !model.mask)
|
|
74
|
+
if (request.providerOptions?.mask !== undefined && !model.mask)
|
|
73
75
|
return Effect.fail(route.unsupported("media.mask", `${id} does not inpaint; use flux-pro-1.0-fill`));
|
|
74
76
|
return Effect.void;
|
|
75
77
|
};
|
|
@@ -87,15 +89,15 @@ const fromRequest = Effect.fn("BlackForestLabsImages.fromRequest")(function* (re
|
|
|
87
89
|
index === 0 ? model.imageField : `${model.imageField}_${index + 1}`,
|
|
88
90
|
image,
|
|
89
91
|
]);
|
|
92
|
+
const { mask, ...native } = request.providerOptions ?? {};
|
|
93
|
+
const size = typeof model.sizing === "string" ? undefined : yield* ImageSizing.exact(route, request, model.sizing);
|
|
90
94
|
return MediaProtocol.json(mergeJsonRecords({
|
|
91
95
|
prompt: request.prompt,
|
|
92
|
-
...
|
|
93
|
-
aspect_ratio: request.
|
|
94
|
-
|
|
95
|
-
output_format: request.format,
|
|
96
|
-
mask: request.mask === undefined ? undefined : yield* imageInput(request.mask),
|
|
96
|
+
...size,
|
|
97
|
+
aspect_ratio: model.sizing === "aspectRatio" ? request.aspect : undefined,
|
|
98
|
+
mask: mask === undefined ? undefined : yield* imageInput(mask),
|
|
97
99
|
...Object.fromEntries(fields),
|
|
98
|
-
},
|
|
100
|
+
}, native, request.http?.body) ?? {});
|
|
99
101
|
});
|
|
100
102
|
// ---------------------------------------------------------------------------
|
|
101
103
|
// 6. Response decoding
|
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import { ImageModel, ImageResponse, type ImageRequestFor } from "../image.js";
|
|
2
2
|
import { MediaProtocol } from "../route/media-protocol.js";
|
|
3
3
|
import type { MediaRoute } from "../route/media.js";
|
|
4
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* Native Run API inputs. Step counts are `steps` on FLUX and Lucid Origin and `num_steps` on the other models;
|
|
6
|
+
* `aspect` and `resolution` lower to `width` and `height`.
|
|
7
|
+
*/
|
|
5
8
|
export type CloudflareWorkersAIImageOptions = {
|
|
9
|
+
readonly width?: number;
|
|
10
|
+
readonly height?: number;
|
|
11
|
+
readonly seed?: number;
|
|
6
12
|
readonly steps?: number;
|
|
7
13
|
readonly num_steps?: number;
|
|
8
14
|
readonly guidance?: number;
|
|
@@ -3,6 +3,7 @@ import { ImageModel, ImageResponse } from "../image.js";
|
|
|
3
3
|
import { Media } from "../media.js";
|
|
4
4
|
import { MediaProtocol } from "../route/media-protocol.js";
|
|
5
5
|
import { mergeJsonRecords } from "../schema/index.js";
|
|
6
|
+
import { ImageSizing } from "./utils/image-sizing.js";
|
|
6
7
|
import { MediaInput } from "./utils/media-input.js";
|
|
7
8
|
const route = MediaProtocol.identity({
|
|
8
9
|
id: "cloudflare-workers-ai-images",
|
|
@@ -29,8 +30,7 @@ const fromRequest = Effect.fn("CloudflareWorkersAIImages.fromRequest")(function*
|
|
|
29
30
|
return yield* route.unsupported("media.n", `${route.name} generates one image per request; call it once per image`);
|
|
30
31
|
const fields = {
|
|
31
32
|
prompt: request.prompt,
|
|
32
|
-
...(
|
|
33
|
-
seed: request.seed,
|
|
33
|
+
...(yield* ImageSizing.exact(route, request, { multiple: 16, minEdge: 256, maxEdge: 2048 })),
|
|
34
34
|
};
|
|
35
35
|
const native = mergeJsonRecords(request.providerOptions, request.http?.body);
|
|
36
36
|
if (!request.model.id.startsWith(MULTIPART_MODEL_PREFIX))
|
|
@@ -61,7 +61,7 @@ const decodeResponse = Effect.fn("CloudflareWorkersAIImages.decodeResponse")(fun
|
|
|
61
61
|
// 7. Protocol and route
|
|
62
62
|
// ---------------------------------------------------------------------------
|
|
63
63
|
export const protocol = MediaProtocol.inline(route, {
|
|
64
|
-
unsupported: ["images"
|
|
64
|
+
unsupported: ["images"],
|
|
65
65
|
body: { from: fromRequest },
|
|
66
66
|
response: { decode: decodeResponse },
|
|
67
67
|
});
|
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
import { ImageModel, ImageResponse, type ImageRequestFor } from "../image.js";
|
|
2
|
+
import { Media } from "../media.js";
|
|
2
3
|
import { MediaProtocol } from "../route/media-protocol.js";
|
|
3
4
|
import { MediaRoute } from "../route/media.js";
|
|
4
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. */
|
|
5
7
|
export type FalImageOptions = {
|
|
6
|
-
readonly image_size?: OpenString<"square_hd" | "square" | "portrait_4_3" | "portrait_16_9" | "landscape_4_3" | "landscape_16_9"
|
|
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;
|
|
7
16
|
readonly enable_safety_checker?: boolean;
|
|
8
17
|
} & Record<string, unknown>;
|
|
9
18
|
export type Request = ImageRequestFor<FalImageOptions>;
|
|
@@ -4,9 +4,9 @@ import { Media } from "../media.js";
|
|
|
4
4
|
import { MediaProtocol } from "../route/media-protocol.js";
|
|
5
5
|
import { MediaRoute } from "../route/media.js";
|
|
6
6
|
import { mergeJsonRecords } from "../schema/index.js";
|
|
7
|
-
import {
|
|
7
|
+
import { optionalNull } from "./shared.js";
|
|
8
8
|
import { FalQueue } from "./utils/fal-queue.js";
|
|
9
|
-
import {
|
|
9
|
+
import { ImageSizing } from "./utils/image-sizing.js";
|
|
10
10
|
const route = MediaProtocol.identity({ id: "fal-images", name: "fal Images", provider: "fal" });
|
|
11
11
|
// ---------------------------------------------------------------------------
|
|
12
12
|
// 2. Response schema
|
|
@@ -24,8 +24,11 @@ const QueueResult = Schema.StructWithRest(Schema.Struct({
|
|
|
24
24
|
// ---------------------------------------------------------------------------
|
|
25
25
|
// 5. Request body construction
|
|
26
26
|
// ---------------------------------------------------------------------------
|
|
27
|
+
/** Nano Banana takes `aspect_ratio` and `resolution` tiers, FLUX Ultra and Kontext only `aspect_ratio`, FLUX `image_size`. */
|
|
27
28
|
const sizing = (model) => {
|
|
28
|
-
if (
|
|
29
|
+
if (model.startsWith("fal-ai/nano-banana"))
|
|
30
|
+
return "tiers";
|
|
31
|
+
if (/^fal-ai\/flux-pro\/(v1\.1-ultra|kontext)/.test(model))
|
|
29
32
|
return "aspect_ratio";
|
|
30
33
|
if (model.startsWith("fal-ai/flux"))
|
|
31
34
|
return "image_size";
|
|
@@ -33,13 +36,8 @@ const sizing = (model) => {
|
|
|
33
36
|
};
|
|
34
37
|
const validate = (request) => {
|
|
35
38
|
const id = request.model.id;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return Effect.fail(ProviderShared.invalidRequest(`${route.name} accepts either size or aspectRatio, not both`));
|
|
39
|
-
if (request.size !== undefined && field === "aspect_ratio")
|
|
40
|
-
return Effect.fail(route.unsupported("media.size", `${id} sizes by aspectRatio`));
|
|
41
|
-
if (request.aspectRatio !== undefined && field === "image_size")
|
|
42
|
-
return Effect.fail(route.unsupported("media.aspectRatio", `${id} sizes by size (image_size)`));
|
|
39
|
+
if (request.resolution !== undefined && sizing(id) === "aspect_ratio")
|
|
40
|
+
return Effect.fail(route.unsupported("media.resolution", `${id} sizes by aspect only`));
|
|
43
41
|
if ((request.images?.length ?? 0) > 1 && !takesImageList(id))
|
|
44
42
|
return Effect.fail(route.unsupported("media.images", `${id} takes one image_url; use an /edit or /multi endpoint for several images`));
|
|
45
43
|
return Effect.void;
|
|
@@ -51,17 +49,18 @@ const fromRequest = Effect.fn("FalImages.fromRequest")(function* (request) {
|
|
|
51
49
|
yield* validate(request);
|
|
52
50
|
const images = yield* Effect.forEach(request.images ?? [], (image) => FalQueue.mediaUrl(image, route.name));
|
|
53
51
|
const list = takesImageList(request.model.id);
|
|
52
|
+
const { mask, ...native } = request.providerOptions ?? {};
|
|
53
|
+
const field = sizing(request.model.id);
|
|
54
54
|
return MediaProtocol.json(mergeJsonRecords({
|
|
55
55
|
prompt: request.prompt,
|
|
56
56
|
num_images: request.n,
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
output_format: request.format,
|
|
57
|
+
...(field === "image_size"
|
|
58
|
+
? { image_size: yield* ImageSizing.exact(route, request, { multiple: 16 }) }
|
|
59
|
+
: { aspect_ratio: request.aspect, resolution: request.resolution }),
|
|
61
60
|
image_urls: list && images.length > 0 ? images : undefined,
|
|
62
61
|
image_url: list ? undefined : images[0],
|
|
63
|
-
mask_url:
|
|
64
|
-
},
|
|
62
|
+
mask_url: mask === undefined ? undefined : yield* FalQueue.mediaUrl(mask, route.name),
|
|
63
|
+
}, native, request.http?.body) ?? {});
|
|
65
64
|
});
|
|
66
65
|
// ---------------------------------------------------------------------------
|
|
67
66
|
// 6. Response decoding
|
|
@@ -31,7 +31,7 @@ const fromRequest = Effect.fn("FalVideo.fromRequest")(function* (request) {
|
|
|
31
31
|
prompt: request.prompt,
|
|
32
32
|
negative_prompt: request.negativePrompt,
|
|
33
33
|
seed: request.seed,
|
|
34
|
-
aspect_ratio: request.
|
|
34
|
+
aspect_ratio: request.aspect,
|
|
35
35
|
resolution: request.resolution,
|
|
36
36
|
generate_audio: request.audio,
|
|
37
37
|
image_url: imageUrl,
|
|
@@ -3,9 +3,9 @@ import { MediaProtocol } from "../route/media-protocol.js";
|
|
|
3
3
|
import { MediaRoute } from "../route/media.js";
|
|
4
4
|
import { type OpenString } from "../schema/index.js";
|
|
5
5
|
export declare const DEFAULT_BASE_URL = "https://generativelanguage.googleapis.com/v1beta";
|
|
6
|
-
/** Provider-native
|
|
6
|
+
/** Provider-native `generationConfig` fields. Common fields (`aspect`, `resolution`, `images`) live on the request. */
|
|
7
7
|
export type GoogleImageOptions = {
|
|
8
|
-
readonly
|
|
8
|
+
readonly seed?: number;
|
|
9
9
|
readonly thinkingLevel?: OpenString<"MINIMAL" | "LOW" | "MEDIUM" | "HIGH">;
|
|
10
10
|
readonly includeThoughts?: boolean;
|
|
11
11
|
} & Record<string, unknown>;
|