feat(ai): align Alibaba image API

This commit is contained in:
Aiden Cline 2026-07-22 05:33:05 +00:00
commit bee140e63b
12 changed files with 321 additions and 416 deletions

View file

@ -252,8 +252,6 @@ GitHubCopilot.configure({ baseURL: "https://copilot.test", apiKey: "copilot-key"
// @ts-expect-error GitHub Copilot model selectors only accept model ids.
GitHubCopilot.configure({ baseURL: "https://copilot.test", apiKey: "copilot-key" }).model("gpt-4.1", {})
Alibaba.configure({ image: { providerOptions: { qwen: { promptExtend: true } } } }).image("qwen-image-2.0")
// @ts-expect-error Alibaba Qwen options are closed and reject unimplemented parameters.
Alibaba.configure({ image: { providerOptions: { qwen: { style: "photorealistic" } } } })
// @ts-expect-error Alibaba Wan options are closed and reject unimplemented parameters.
Alibaba.configure({ image: { providerOptions: { wan: { parameters: { watermark: true } } } } })
Alibaba.configure({ apiKey: "dashscope-key" }).image("any-model-id")
// @ts-expect-error Alibaba image options are request-scoped, not provider configuration.
Alibaba.configure({ image: { options: { promptExtend: true } } })

View file

@ -3,6 +3,7 @@ import { ImageInput, LLM, LLMClient, Provider } from "@opencode-ai/ai"
import { Route, Protocol } from "@opencode-ai/ai/route"
import { Provider as ProviderSubpath } from "@opencode-ai/ai/provider"
import {
Alibaba,
CloudflareAIGateway,
CloudflareWorkersAI,
OpenAI,
@ -11,7 +12,13 @@ import {
XAI,
} from "@opencode-ai/ai/providers"
import * as GitHubCopilot from "@opencode-ai/ai/providers/github-copilot"
import { OpenAIChat, OpenAICompatibleChat, OpenAICompatibleResponses, OpenAIResponses } from "@opencode-ai/ai/protocols"
import {
AlibabaImages,
OpenAIChat,
OpenAICompatibleChat,
OpenAICompatibleResponses,
OpenAIResponses,
} from "@opencode-ai/ai/protocols"
import * as AnthropicMessages from "@opencode-ai/ai/protocols/anthropic-messages"
describe("public exports", () => {
@ -32,6 +39,7 @@ describe("public exports", () => {
test("provider barrels expose user-facing facades", async () => {
const { OpenAICompatibleResponses } = await import("@opencode-ai/ai/providers")
expect(Alibaba.configure({ apiKey: "fixture" }).image("any-model-id").provider).toBe("alibaba")
expect(OpenAI.model).toBeFunction()
expect(OpenAI.provider.responses).toBe(OpenAI.responses)
expect(OpenAI.provider.responsesWebSocket).toBe(OpenAI.responsesWebSocket)
@ -72,6 +80,7 @@ describe("public exports", () => {
})
test("protocol barrels expose supported low-level routes", () => {
expect(AlibabaImages.DEFAULT_BASE_URL).toBe("https://dashscope-intl.aliyuncs.com/api/v1")
expect(OpenAIChat.route.id).toBe("openai-chat")
expect(OpenAICompatibleChat.route.id).toBe("openai-compatible-chat")
expect(OpenAICompatibleResponses.route.id).toBe("openai-compatible-responses")

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

View file

@ -0,0 +1,32 @@
{
"version": 1,
"metadata": {
"tags": [
"prefix:alibaba-images",
"provider:alibaba",
"protocol:alibaba-images"
],
"name": "alibaba-images/edits-with-qwen-image-2-0",
"recordedAt": "2026-07-22T05:30:23.625Z"
},
"interactions": [
{
"transport": "http",
"request": {
"method": "POST",
"url": "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation",
"headers": {
"content-type": "application/json"
},
"body": "{\"model\":\"qwen-image-2.0\",\"input\":{\"messages\":[{\"role\":\"user\",\"content\":[{\"image\":\"data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAAQABAAD//gAQTGF2YzYwLjMxLjEwMgD/2wBDAAgQEBMQExYWFhYWFhoYGhsbGxoaGhobGxsdHR0iIiIdHR0bGx0dICAiIiUmJSMjIiMmJigoKDAwLi44ODpFRVP/xABrAAEBAQEBAQAAAAAAAAAAAAAABwgGBAUBAQAAAAAAAAAAAAAAAAAAAAAQAAICAQEFBwMEAwEAAAAAAAABAwIEEQYFEiFhwXGBUUExE6GRU0LwciLh8bEjEQEAAAAAAAAAAAAAAAAAAAAA/8AAEQgAgACAAwEiAAIRAAMRAP/aAAwDAQACEQMRAD8Av4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB458iLFjtLNesdK+9rPRf76e4yMiPFivNLbhpHV2s+i7X6LzMhb33xPvabitrWKrfxxa8qrzfnZ+r8EBTc/bPm64USa/LLrz61omvq/AnMu0W9ZXq8q9elFSi+ldTigB2se0W9Ynqsq9ul1S6+tSiYG2b1Vc2Jafli15dXRt/R+BBgBuvHyIcqOssN6yUt7Wq9V/h9HzPYY03RvefdM3HTW0dn/AOkWvKy815WXo/ua+x8iPKhpNFbipJVWq/36r2a8wPaAAAAAAAAAAIDtnnvWLCq+WnyydfSlX9X9iDHZ7QyuXeuW3+m6ou6lUjjAAAAAAAXbYzeDVpcKz5NfLH0ftdLv5P7kJOw2flcW9cRr1k4H3XTr2gbHAAAAAAAAAAGO9oYnFvXLT/VdXXdeqZxhdts8Bq0WZVcmvik6NaujffzX2ISAAAAAADr9wRuXeuGl6ScfhRO3YcgXHYzAdpZcyy/rRfFH1s+dmu5aLxA0MAAAAAAAAAAPDk40WXDJDLXipJXha7V1Xuupj7eu6p91TuORO1Hr8cmnK67LL1Rs8+fk4sOZFaKeOslLe6f/AFP3T8muYGGAXHP2Nkq3bDlV6/jl5WXRXXJ+KROpdxb0ielsOZ/wXGvvRsDkwdXHuLekr0WHMv5V4F97tFDwNjZrtWzJVHX8cb4rvo7acK8NQJtuvdc+9Z1FGtKrT5JNP60r2vyXqbBxMWLCgjgiXDSNaLzfm31b5sYuJBhRKKCOsdF6L1fm37t9WfRAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/9k=\"},{\"text\":\"Turn the black source shape into a bright orange sun icon on a pale blue background.\"}]}]},\"parameters\":{\"prompt_extend\":false,\"watermark\":false}}"
},
"response": {
"status": 200,
"headers": {
"content-type": "application/json"
},
"body": "{\"output\":{\"choices\":[{\"finish_reason\":\"stop\",\"message\":{\"content\":[{\"image\":\"https://dashscope-463f.oss-accelerate.aliyuncs.com/7d/89/20260722/74716928/0e082b32-9ff8-4e81-aa78-bde6b41d33ae.png?Expires=1785304023&OSSAccessKeyId=%5BREDACTED%5D&Signature=%5BREDACTED%5D\"}],\"role\":\"assistant\"}}]},\"usage\":{\"height\":1024,\"image_count\":1,\"width\":1024},\"request_id\":\"13d119d7-dbf1-98a7-9fae-d4ad6441f8a7\"}"
}
}
]
}

View file

@ -0,0 +1,32 @@
{
"version": 1,
"metadata": {
"tags": [
"prefix:alibaba-images",
"provider:alibaba",
"protocol:alibaba-images"
],
"name": "alibaba-images/edits-with-wan-2-7",
"recordedAt": "2026-07-22T05:30:36.133Z"
},
"interactions": [
{
"transport": "http",
"request": {
"method": "POST",
"url": "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation",
"headers": {
"content-type": "application/json"
},
"body": "{\"model\":\"wan2.7-image\",\"input\":{\"messages\":[{\"role\":\"user\",\"content\":[{\"image\":\"data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAAQABAAD//gAQTGF2YzYwLjMxLjEwMgD/2wBDAAgICAkICQsLCwsLCw0MDQ0NDQ0NDQ0NDQ0ODg4REREODg4NDQ4OEBARERITEhERERETExQUFBgYFxccHB0iIin/xAB+AAEAAwADAQEAAAAAAAAAAAAABgcIAgMBBQQBAQAAAAAAAAAAAAAAAAAAAAAQAQABAgIGBgYHBwUBAQAAAAABAgMEERIhMUEFBoFCB1ETUhRhoXEyIpHBglMjQzMkFcSx4XNidKJy8BY0khEBAAAAAAAAAAAAAAAAAAAAAP/AABEIAQABAAMBIgACEQADEQD/2gAMAwEAAhEDEQA/AL/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABx264+l7u2urLOrPf5f6opzNzhw3lqn8erO75Mrv+HWps3KdlcSCWzOXzTOWW7ahnGOf+AcImabmK0rkfl+FiY8vWjD1RsqzZ15g594xzBq0vCtb7eVivydbwLdW2jNCtUa6Z192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DmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD//Z\"},{\"text\":\"Turn the black source shape into a bright orange sun icon on a pale blue background.\"}]}]},\"parameters\":{\"size\":\"1K\",\"thinking_mode\":false,\"watermark\":false}}"
},
"response": {
"status": 200,
"headers": {
"content-type": "application/json"
},
"body": "{\"output\":{\"choices\":[{\"finish_reason\":\"stop\",\"message\":{\"content\":[{\"image\":\"https://dashscope-463f.oss-accelerate.aliyuncs.com/1d/d0/20260722/10739b93/1784698235_08b3b146.png?Expires=1784784635&OSSAccessKeyId=%5BREDACTED%5D&Signature=%5BREDACTED%5D\",\"type\":\"image\"}],\"role\":\"assistant\"}}],\"finished\":true},\"usage\":{\"image_count\":1,\"input_tokens\":9430,\"output_tokens\":2,\"size\":\"1024*1024\",\"total_tokens\":9432},\"request_id\":\"e55e017f-f02a-92f3-8405-354906e1159a\"}"
}
}
]
}

View file

@ -7,7 +7,7 @@ import {
type ImageRequestFor,
type ImageRoute,
} from "../src"
import { Google, OpenAI, XAI, ZAI } from "../src/providers"
import { Alibaba, Google, OpenAI, XAI, ZAI } from "../src/providers"
type GoogleLikeOptions = {
readonly aspectRatio?: "1:1" | "16:9"
@ -58,6 +58,26 @@ Image.generate({ model: googleProvider, prompt: "A lighthouse", options: { seed:
// @ts-expect-error Known Google boolean options retain their value kind.
Image.generate({ model: googleProvider, prompt: "A lighthouse", options: { includeThoughts: "yes" } })
const alibaba = Alibaba.configure({ apiKey: "test" }).image("any-model-id")
Image.generate({
model: alibaba,
prompt: "A lighthouse",
images: [ImageInput.url("https://example.com/source.png")],
options: {
resolution: "2K",
negativePrompt: "fog",
promptExtend: true,
thinkingMode: false,
colorPalette: [{ hex: "#112233", ratio: "100.00%" }],
watermark: false,
future_parameter: true,
},
})
// @ts-expect-error Known Alibaba boolean options retain their value kind.
Image.generate({ model: alibaba, prompt: "A lighthouse", options: { promptExtend: "yes" } })
// @ts-expect-error Known Alibaba numeric options retain their value kind.
Image.generate({ model: alibaba, prompt: "A lighthouse", options: { seed: "42" } })
const openai = OpenAI.image("gpt-image-2")
// @ts-expect-error Image generation options are request-scoped, not provider configuration.
OpenAI.configure({ image: { options: { quality: "medium" } } })

View file

@ -1,7 +1,8 @@
import { describe, expect, test } from "bun:test"
import { Effect, Option, Schema } from "effect"
import { Image } from "../../src"
import { Image, ImageInput } from "../../src"
import { Alibaba } from "../../src/providers"
import { dimensions } from "../lib/image"
import { recordedTests } from "../recorded-test"
const alibaba = Alibaba.configure({
@ -58,6 +59,15 @@ const recorded = recordedTests({
options: { redact: { body: redactSignedUrls } },
})
const inspectLive = async (value: string | Uint8Array | undefined) => {
if (process.env.RECORD !== "true" || typeof value !== "string") return
const response = await fetch(value)
const bytes = new Uint8Array(await response.arrayBuffer())
expect(response.headers.get("content-type")).toMatch(/^image\//)
expect(dimensions(bytes).width).toBeGreaterThan(0)
expect(dimensions(bytes).height).toBeGreaterThan(0)
}
test("recorder redacts signed URL credentials in nested JSON strings repeatably", () => {
const body = JSON.stringify({
output: {
@ -87,8 +97,7 @@ describe("Alibaba Images recorded", () => {
const response = yield* Image.generate({
model: alibaba.image("qwen-image-2.0"),
prompt: "A simple flat black circle centered on a plain white background.",
size: { width: 512, height: 512 },
providerOptions: { alibaba: { qwen: { promptExtend: false, watermark: false } } },
options: { size: "512*512", promptExtend: false, watermark: false },
})
expect(response.images).toHaveLength(1)
@ -102,7 +111,7 @@ describe("Alibaba Images recorded", () => {
const response = yield* Image.generate({
model: alibaba.image("wan2.7-image"),
prompt: "A simple flat black square centered on a plain white background.",
providerOptions: { alibaba: { wan: { resolution: "1K", thinkingMode: false, watermark: false } } },
options: { resolution: "1K", thinkingMode: false, watermark: false },
})
expect(response.images).toHaveLength(1)
@ -110,4 +119,46 @@ describe("Alibaba Images recorded", () => {
expect(response.image?.data).toStartWith("https://")
}),
)
recorded.effect("edits with Qwen Image 2.0", () =>
Effect.gen(function* () {
const response = yield* Image.generate({
model: alibaba.image("qwen-image-2.0"),
prompt: "Turn the black source shape into a bright orange sun icon on a pale blue background.",
images: [
ImageInput.bytes(
yield* Effect.promise(() => Bun.file("test/fixtures/images/edit-source.jpg").bytes()),
"image/jpeg",
),
],
options: { promptExtend: false, watermark: false },
})
expect(response.images).toHaveLength(1)
expect(response.image?.mediaType).toBe("image/png")
expect(response.image?.data).toStartWith("https://")
yield* Effect.promise(() => inspectLive(response.image?.data))
}),
)
recorded.effect("edits with Wan 2.7", () =>
Effect.gen(function* () {
const response = yield* Image.generate({
model: alibaba.image("wan2.7-image"),
prompt: "Turn the black source shape into a bright orange sun icon on a pale blue background.",
images: [
ImageInput.bytes(
yield* Effect.promise(() => Bun.file("test/fixtures/images/edit-source-256.jpg").bytes()),
"image/jpeg",
),
],
options: { resolution: "1K", thinkingMode: false, watermark: false },
})
expect(response.images).toHaveLength(1)
expect(response.image?.mediaType).toBe("image/png")
expect(response.image?.data).toStartWith("https://")
yield* Effect.promise(() => inspectLive(response.image?.data))
}),
)
})

View file

@ -1,247 +1,109 @@
import { describe, expect } from "bun:test"
import { Effect, Layer } from "effect"
import { HttpClientRequest } from "effect/unstable/http"
import { Image, ImageClient } from "../../src"
import { Image, ImageClient, ImageInput } from "../../src"
import { Alibaba } from "../../src/providers"
import { it } from "../lib/effect"
import { dynamicResponse } from "../lib/http"
const response = (family: "qwen" | "wan") => ({
output: {
choices: [
{
finish_reason: "stop",
message: {
role: "assistant",
content: [
{
image: "https://dashscope-result-intl.oss-cn-singapore.aliyuncs.com/result.png?Expires=1893456000",
...(family === "wan" ? { type: "image" } : {}),
},
],
},
},
],
...(family === "wan" ? { finished: true } : {}),
},
usage:
family === "wan"
? { image_count: 1, input_tokens: 10, output_tokens: 2, total_tokens: 12, size: "2048*2048" }
: { image_count: 1, width: 1024, height: 768 },
request_id: `request-${family}`,
const payload = (url = "https://example.com/result.png?Expires=1893456000") => ({
output: { choices: [{ message: { content: [{ image: url }] } }] },
usage: { total_tokens: 12 },
request_id: "request-alibaba",
})
describe("Alibaba Images", () => {
it.effect("generates Qwen Image 2.0 through the international synchronous route", () =>
Effect.gen(function* () {
const result = yield* Image.generate({
model: Alibaba.configure({
apiKey: "test",
baseURL: "https://dashscope-intl.test/api/v1",
image: { providerOptions: { qwen: { promptExtend: true, watermark: false } } },
http: { headers: { "x-default": "yes" } },
}).image("qwen-image-2.0-pro"),
prompt: "A robot tending a rooftop garden",
count: 2,
size: { width: 1024, height: 768 },
seed: 42,
providerOptions: { alibaba: { qwen: { negativePrompt: "blurry" } } },
http: { headers: { "x-request": "yes" }, query: { trace: "1" }, body: { metadata: "test" } },
})
expect(result.image?.data).toContain("dashscope-result-intl")
expect(result.image?.expiresAt).toBe("2030-01-01T00:00:00.000Z")
expect(result.image?.providerMetadata).toEqual({
alibaba: {
modelId: "qwen-image-2.0-pro",
family: "qwen",
expiresAt: "2030-01-01T00:00:00.000Z",
},
})
expect(result.providerMetadata).toEqual({
alibaba: { requestId: "request-qwen", modelId: "qwen-image-2.0-pro", family: "qwen" },
})
}).pipe(
Effect.provide(
ImageClient.layer.pipe(
Layer.provide(
dynamicResponse((input) =>
Effect.gen(function* () {
const request = yield* HttpClientRequest.toWeb(input.request).pipe(Effect.orDie)
expect(request.url).toBe(
"https://dashscope-intl.test/api/v1/services/aigc/multimodal-generation/generation?trace=1",
)
expect(request.headers.get("authorization")).toBe("Bearer test")
expect(request.headers.get("x-default")).toBe("yes")
expect(request.headers.get("x-request")).toBe("yes")
expect(JSON.parse(input.text)).toEqual({
model: "qwen-image-2.0-pro",
input: {
messages: [{ role: "user", content: [{ text: "A robot tending a rooftop garden" }] }],
},
parameters: {
size: "1024*768",
n: 2,
negative_prompt: "blurry",
prompt_extend: true,
watermark: false,
seed: 42,
},
metadata: "test",
})
return input.respond(JSON.stringify(response("qwen")), {
headers: { "content-type": "application/json" },
})
}),
),
),
),
),
const layer = (inspect: (body: Record<string, unknown>) => void, url?: string) =>
ImageClient.layer.pipe(
Layer.provide(
dynamicResponse((input) => {
inspect(JSON.parse(input.text))
return Effect.succeed(
input.respond(JSON.stringify(payload(url)), { headers: { "content-type": "application/json" } }),
)
}),
),
)
it.effect("generates Wan 2.7 through the international synchronous route", () =>
describe("Alibaba Images", () => {
it.effect("supports open options and arbitrary models", () =>
Effect.gen(function* () {
const result = yield* Image.generate({
model: Alibaba.configure({ apiKey: "test", baseURL: "https://dashscope-intl.test/api/v1" }).image(
"wan2.7-image-pro",
),
prompt: "A flower shop with a wooden door",
count: 1,
seed: 7,
providerOptions: {
alibaba: {
wan: {
resolution: "2K",
thinkingMode: true,
watermark: false,
colorPalette: [
{ hex: "#112233", ratio: "60.00%" },
{ hex: "#445566", ratio: "25.00%" },
{ hex: "#778899", ratio: "15.00%" },
],
},
model: Alibaba.configure({ apiKey: "test" }).image("future-model-id"),
prompt: "A robot",
options: {
resolution: "2K",
negativePrompt: "blurry",
negative_prompt: "native wins",
future_parameter: true,
},
http: {
body: {
model: "ignored-model",
input: { messages: [] },
parameters: { seed: 7 },
},
},
})
expect(result.images).toHaveLength(1)
expect(result.image?.mediaType).toBe("image/png")
expect(result.usage?.totalTokens).toBe(12)
expect(result.usage?.providerMetadata).toEqual({ alibaba: response("wan").usage })
expect(result.image?.expiresAt).toBe("2030-01-01T00:00:00.000Z")
}).pipe(
Effect.provide(
ImageClient.layer.pipe(
Layer.provide(
dynamicResponse((input) =>
Effect.gen(function* () {
const request = yield* HttpClientRequest.toWeb(input.request).pipe(Effect.orDie)
expect(request.url).toBe(
"https://dashscope-intl.test/api/v1/services/aigc/multimodal-generation/generation",
)
expect(JSON.parse(input.text)).toEqual({
model: "wan2.7-image-pro",
input: { messages: [{ role: "user", content: [{ text: "A flower shop with a wooden door" }] }] },
parameters: {
size: "2K",
n: 1,
thinking_mode: true,
color_palette: [
{ hex: "#112233", ratio: "60.00%" },
{ hex: "#445566", ratio: "25.00%" },
{ hex: "#778899", ratio: "15.00%" },
],
watermark: false,
seed: 7,
},
})
return input.respond(JSON.stringify(response("wan")), {
headers: { "content-type": "application/json" },
})
}),
),
),
),
),
),
)
it.effect("surfaces Alibaba error envelopes as typed provider errors", () =>
Image.generate({
model: Alibaba.configure({ apiKey: "test" }).image("qwen-image-2.0"),
prompt: "A robot",
}).pipe(
Effect.flip,
Effect.tap((error) =>
Effect.sync(() => {
expect(error.reason._tag).toBe("UnknownProvider")
expect(error.message).toContain("InvalidParameter: invalid prompt")
layer((body) => {
expect(body.model).toBe("future-model-id")
expect(body.input).toEqual({ messages: [{ role: "user", content: [{ text: "A robot" }] }] })
expect(body.parameters).toEqual({
size: "2K",
negative_prompt: "native wins",
future_parameter: true,
seed: 7,
})
}),
),
Effect.provide(
ImageClient.layer.pipe(
Layer.provide(
dynamicResponse((input) =>
Effect.succeed(
input.respond(
JSON.stringify({ request_id: "request-error", code: "InvalidParameter", message: "invalid prompt" }),
{ headers: { "content-type": "application/json" } },
),
),
),
),
),
),
),
)
it.effect("rejects parameters overlays owned by the protocol", () =>
it.effect("lowers ordered image inputs before text", () =>
Image.generate({
model: Alibaba.configure({ apiKey: "test" }).image("qwen-image-2.0"),
prompt: "A robot",
http: { body: { parameters: { watermark: true } } },
prompt: "Combine these",
images: [
ImageInput.url("https://example.com/first.png"),
ImageInput.bytes(Uint8Array.from([1, 2, 3]), "image/png"),
],
}).pipe(
Effect.flip,
Effect.tap((error) =>
Effect.sync(() => {
expect(error.reason._tag).toBe("InvalidRequest")
expect(error.message).toContain("http.body cannot overlay protocol-owned field(s): parameters")
}),
),
Effect.provide(
ImageClient.layer.pipe(
Layer.provide(
dynamicResponse(() => Effect.die("reserved body validation must happen before the request is sent")),
),
),
layer((body) => {
const input = body.input as { messages: Array<{ content: unknown }> }
expect(input.messages[0].content).toEqual([
{ image: "https://example.com/first.png" },
{ image: "data:image/png;base64,AQID" },
{ text: "Combine these" },
])
}),
),
),
)
it.effect("ignores finite expiration values outside the Date range", () =>
it.effect("rejects provider file inputs", () =>
Image.generate({
model: Alibaba.configure({ apiKey: "test" }).image("any-model"),
prompt: "A robot",
images: [ImageInput.file("file_123")],
}).pipe(
Effect.flip,
Effect.tap((error) => Effect.sync(() => expect(error.reason._tag).toBe("InvalidRequest"))),
Effect.provide(
ImageClient.layer.pipe(Layer.provide(dynamicResponse(() => Effect.die("must fail before network I/O")))),
),
),
)
it.effect("ignores expiration values outside the Date range", () =>
Effect.gen(function* () {
const result = yield* Image.generate({
model: Alibaba.configure({ apiKey: "test" }).image("qwen-image-2.0"),
model: Alibaba.configure({ apiKey: "test" }).image("any-model"),
prompt: "A robot",
})
expect(result.image?.expiresAt).toBeUndefined()
expect(result.image?.providerMetadata?.alibaba?.expiresAt).toBeUndefined()
}).pipe(
Effect.provide(
ImageClient.layer.pipe(
Layer.provide(
dynamicResponse((input) => {
const payload = response("qwen")
payload.output.choices[0].message.content[0].image =
"https://dashscope-result-intl.oss-cn-singapore.aliyuncs.com/result.png?Expires=9007199254740991"
return Effect.succeed(
input.respond(JSON.stringify(payload), { headers: { "content-type": "application/json" } }),
)
}),
),
),
),
),
expect(result.usage?.totalTokens).toBe(12)
}).pipe(Effect.provide(layer(() => {}, "https://example.com/result.png?Expires=9007199254740991"))),
)
})