mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 17:01:37 +03:00
feat(ai): add Alibaba image generation (#58383)
This commit is contained in:
committed by
GitHub
Unverified
parent
194f853f0b
commit
861a2cf479
@@ -274,6 +274,14 @@ Package entrypoints are `@opencode/ai/providers/alibaba`, `alibaba/chat`, `aliba
|
||||
and `alibaba/responses`. Live recordings cover all three APIs in Singapore; regional URL construction
|
||||
is unit-tested for all six regions.
|
||||
|
||||
### Images
|
||||
|
||||
`alibaba.image(...)` uses the native multimodal generation API (`/api/v1/services/aigc/multimodal-generation/generation`)
|
||||
for Qwen-Image (`qwen-image-2.0`, `qwen-image-2.0-pro`) and Wan (`wan2.7-image`, `wan2.7-image-pro`). `size` lowers to
|
||||
`W*H`, and `images` are sent as URLs or data URLs for editing. Native `parameters` include `negativePrompt`,
|
||||
`promptExtend`, `watermark`, `thinkingMode`, and `enableSequential`; raw keys such as `bbox_list` or `size: "2K"`
|
||||
pass through. Results are URLs that Model Studio keeps for 24 hours. A complete `baseURL` for images includes `/api/v1`.
|
||||
|
||||
## Z.AI
|
||||
|
||||
`ZAI` uses the standard API. Chat Completions is the default language-model API;
|
||||
@@ -551,6 +559,7 @@ call rather than being dropped, so check this table before swapping only the `mo
|
||||
| Google (Gemini) | 1 | ✗ | ✓ | ✓ | ✗ | ✓ (no public URLs) | ✗ |
|
||||
| xAI | ✓ | ✗ | ✓ | ✗ | ✗ | ✓ | ✗ |
|
||||
| Z.ai | ✗ | ✓ | ✗ | ✗ | ✗ | ✗ | ✗ |
|
||||
| Alibaba | ✓ | ✓ | ✗ | ✓ | ✗ | ✓ | ✗ |
|
||||
| Meta | ✓ | ✓ (hint) | ✗ | ✗ | ✓ | ✓ | ✗ |
|
||||
| Black Forest Labs | 1 | per model | per model | ✓ | ✓ | per model (1–8) | `flux-pro-1.0-fill` |
|
||||
| fal | ✓ | per model | per model | ✓ | ✓ | 1 (several on `/edit`, `/multi`) | ✓ |
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
import { Duration, Effect, Schema } from "effect"
|
||||
import type { HttpClientResponse } from "effect/http"
|
||||
import { ImageModel, ImageResponse, type ImageRequestFor } from "../image.js"
|
||||
import { MediaProtocol } from "../route/media-protocol.js"
|
||||
import { MediaRoute } from "../route/media.js"
|
||||
import { mergeJsonRecords } from "../schema/index.js"
|
||||
import { JsonObject, ProviderShared, lenient } from "./shared.js"
|
||||
|
||||
const route = MediaProtocol.identity({ id: "alibaba-images", name: "Alibaba Images", provider: "alibaba" })
|
||||
export const PATH = "/services/aigc/multimodal-generation/generation"
|
||||
const OUTPUT_RETENTION = Duration.hours(24)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 1. Public model input
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** Provider-native `parameters`. Common fields (`n`, `size`, `seed`, `images`) live on the request. */
|
||||
export type AlibabaImageOptions = {
|
||||
readonly negativePrompt?: string
|
||||
readonly promptExtend?: boolean
|
||||
readonly watermark?: boolean
|
||||
readonly thinkingMode?: boolean
|
||||
readonly enableSequential?: boolean
|
||||
} & Record<string, unknown>
|
||||
|
||||
export type Request = ImageRequestFor<AlibabaImageOptions>
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 2. Response schema
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const AlibabaImageResponse = Schema.Struct({
|
||||
output: Schema.Struct({
|
||||
choices: Schema.Array(
|
||||
Schema.Struct({
|
||||
message: Schema.Struct({
|
||||
content: Schema.Array(Schema.Struct({ image: Schema.optional(Schema.String) })),
|
||||
}),
|
||||
}),
|
||||
),
|
||||
}),
|
||||
usage: lenient(JsonObject),
|
||||
request_id: Schema.optional(Schema.String),
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 5. Request body construction
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const nativeOptions = (options: AlibabaImageOptions | undefined) => {
|
||||
if (!options) return undefined
|
||||
const { negativePrompt, promptExtend, thinkingMode, enableSequential, ...native } = options
|
||||
return {
|
||||
negative_prompt: negativePrompt,
|
||||
prompt_extend: promptExtend,
|
||||
thinking_mode: thinkingMode,
|
||||
enable_sequential: enableSequential,
|
||||
...native,
|
||||
}
|
||||
}
|
||||
|
||||
const fromRequest = Effect.fn("AlibabaImages.fromRequest")(function* (request: Request) {
|
||||
const images = yield* Effect.forEach(request.images ?? [], (image) =>
|
||||
ProviderShared.mediaReference(image, undefined, `${route.name} input`).pipe(
|
||||
Effect.map((item) => ({ image: item.value })),
|
||||
),
|
||||
)
|
||||
return MediaProtocol.json(
|
||||
mergeJsonRecords(
|
||||
{
|
||||
model: request.model.id,
|
||||
input: { messages: [{ role: "user", content: [...images, { text: request.prompt }] }] },
|
||||
parameters: mergeJsonRecords(
|
||||
{ size: request.size?.replace("x", "*"), n: request.n, seed: request.seed },
|
||||
nativeOptions(request.providerOptions),
|
||||
),
|
||||
},
|
||||
request.http?.body,
|
||||
) ?? {},
|
||||
)
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 6. Response decoding
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const decodeDocument = route.decodeJson(AlibabaImageResponse)
|
||||
|
||||
const decodeResponse = Effect.fn("AlibabaImages.decodeResponse")(function* (
|
||||
response: HttpClientResponse.HttpClientResponse,
|
||||
) {
|
||||
const output = yield* decodeDocument(response)
|
||||
const decoded = output.value
|
||||
// Qwen-Image returns every image in one choice; Wan returns one choice per image.
|
||||
const urls = decoded.output.choices.flatMap((choice) =>
|
||||
choice.message.content.flatMap((item) => (item.image === undefined ? [] : [item.image])),
|
||||
)
|
||||
if (urls.length === 0) return yield* output.invalid(`${route.name} returned no images`)
|
||||
// Model Studio bills per image; its token counts are informational, so the raw record stays in provider metadata.
|
||||
return new ImageResponse({
|
||||
images: yield* Effect.forEach(urls, (url) => MediaProtocol.expiringUrl(url, OUTPUT_RETENTION)),
|
||||
providerMetadata: { alibaba: { requestID: decoded.request_id, usage: decoded.usage } },
|
||||
})
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 7. Protocol and route
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export const protocol = MediaProtocol.inline<Request, ImageResponse>(route, {
|
||||
unsupported: ["mask", "aspectRatio", "format"],
|
||||
body: { from: fromRequest },
|
||||
response: { decode: decodeResponse },
|
||||
})
|
||||
|
||||
export const model = (input: MediaRoute.ModelInput) =>
|
||||
ImageModel.fromRoute<AlibabaImageOptions>({ protocol, path: PATH }, input)
|
||||
|
||||
export const AlibabaImages = {
|
||||
protocol,
|
||||
model,
|
||||
} as const
|
||||
@@ -153,7 +153,8 @@ const INVALID_REQUEST_CODES = new Set([
|
||||
])
|
||||
// Azure OpenAI reports `content_filter` with `innererror.code` ResponsibleAIPolicyViolation.
|
||||
// OpenRouter tags provider failures with a typed `error_type`; its Responses skin also
|
||||
// emits `image_content_policy_violation` as the native code.
|
||||
// emits `image_content_policy_violation` as the native code. Alibaba Model Studio reports
|
||||
// moderation rejections as `DataInspectionFailed`.
|
||||
const CONTENT_POLICY_CODES = new Set([
|
||||
"content_filter",
|
||||
"content_moderation",
|
||||
@@ -161,6 +162,7 @@ const CONTENT_POLICY_CODES = new Set([
|
||||
"content_policy_violation",
|
||||
"image_content_policy_violation",
|
||||
"refusal",
|
||||
"datainspectionfailed",
|
||||
])
|
||||
// OpenCode Zen replaces upstream codes outside its allow-list but keeps the original
|
||||
// as a `[code]` label at the start of the rewritten message.
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
import { Struct } from "effect"
|
||||
import type { ProviderPackage } from "../provider-package.js"
|
||||
import { AlibabaChat } from "../protocols/alibaba-chat.js"
|
||||
import { AlibabaImages } from "../protocols/alibaba-images.js"
|
||||
import { AlibabaMessages } from "../protocols/alibaba-messages.js"
|
||||
import { AlibabaResponses } from "../protocols/alibaba-responses.js"
|
||||
import { AuthOptions, type AtLeastOne, type ProviderAuthOption } from "../route/auth-options.js"
|
||||
import { Route, type RouteDefaultsInput } from "../route/client.js"
|
||||
import { Endpoint } from "../route/endpoint.js"
|
||||
import { Framing } from "../route/framing.js"
|
||||
import { MediaRoute } from "../route/media.js"
|
||||
import { ProviderConfigurationError, ProviderID, ToolDefinition, type ModelID } from "../schema/index.js"
|
||||
|
||||
export const id = ProviderID.make("alibaba")
|
||||
@@ -22,6 +24,7 @@ export type Region =
|
||||
export type ChatOptionsInput = AlibabaChat.OptionsInput
|
||||
export type MessagesOptionsInput = AlibabaMessages.OptionsInput
|
||||
export type ResponsesOptionsInput = AlibabaResponses.OptionsInput
|
||||
export type { AlibabaImageOptions } from "../protocols/alibaba-images.js"
|
||||
|
||||
type Location = AtLeastOne<{
|
||||
readonly region: Region
|
||||
@@ -103,7 +106,9 @@ export const configure = (input: Config) => {
|
||||
})
|
||||
.model<MessagesOptionsInput>({ id, compatibility: { requireSignature: false } })
|
||||
const responses = (id: string | ModelID) => responsesRoute.with(common).model<ResponsesOptionsInput>({ id })
|
||||
return { id, model: chat, chat, messages, responses, configure }
|
||||
const media = MediaRoute.deployment({ ...input, baseURL: baseURL ?? `https://${host}/api/v1` }, opts.auth)
|
||||
const image = (id: string | ModelID) => AlibabaImages.model({ ...media, id })
|
||||
return { id, model: chat, chat, messages, responses, image, configure }
|
||||
}
|
||||
|
||||
export const provider = { id, configure }
|
||||
|
||||
@@ -21,6 +21,7 @@ import {
|
||||
import { Route, Protocol, RequestExecutor, WebSocketTransport } from "@opencode/ai/route"
|
||||
import { Provider as ProviderSubpath } from "@opencode/ai/provider"
|
||||
import {
|
||||
Alibaba,
|
||||
AssemblyAI,
|
||||
Baseten,
|
||||
BlackForestLabs,
|
||||
@@ -179,6 +180,9 @@ describe("public exports", () => {
|
||||
expect(Meta.provider.image).toBe(Meta.image)
|
||||
expect(ZAI.configure({ apiKey: "fixture" }).image("glm-image").route.id).toBe("zai-images")
|
||||
expect(ZAI.provider.image).toBe(ZAI.image)
|
||||
expect(Alibaba.configure({ region: "ap-southeast-1", apiKey: "fixture" }).image("qwen-image-2.0").route.id).toBe(
|
||||
"alibaba-images",
|
||||
)
|
||||
expect(XAI.configure({ apiKey: "fixture" }).video("grok-imagine-video-1.5").route.id).toBe("xai-video")
|
||||
expect(XAI.provider.video).toBe(XAI.video)
|
||||
expect(Google.configure({ apiKey: "fixture" }).video("veo-3.1-generate-preview").route.id).toBe("google-video")
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
{
|
||||
"version": 1,
|
||||
"metadata": {
|
||||
"tags": [
|
||||
"prefix:alibaba-images",
|
||||
"provider:alibaba",
|
||||
"protocol:alibaba-images",
|
||||
"region:ap-southeast-1"
|
||||
],
|
||||
"name": "alibaba-images/edits-an-image",
|
||||
"recordedAt": "2026-10-23T07:48:20.523Z"
|
||||
},
|
||||
"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\":\"Keep the simple shape and change it from black to bright purple.\"}]}]},\"parameters\":{\"size\":\"512*512\"}}"
|
||||
},
|
||||
"response": {
|
||||
"status": 200,
|
||||
"headers": {
|
||||
"content-type": "application/json"
|
||||
},
|
||||
"body": "{\"output\":{\"choices\":[{\"finish_reason\":\"stop\",\"message\":{\"content\":[{\"image\":\"https://dashscope-f4de.oss-accelerate.aliyuncs.com/7d/1b/20261023/885e41f3/36ed2398-cde9-467b-bb52-29a2bf5afafd.png?Expires=1793347499&OSSAccessKeyId=LTAI5tRcsWJEymQaTsKbKqGf&Signature=wu0WexYqYBN15UXBRy8Qq2SyNiM%3D\"}],\"role\":\"assistant\"}}]},\"usage\":{\"height\":512,\"image_count\":1,\"width\":512},\"request_id\":\"7124c099-ccf7-96fa-ab5d-099477b7666e\"}"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
{
|
||||
"version": 1,
|
||||
"metadata": {
|
||||
"tags": [
|
||||
"prefix:alibaba-images",
|
||||
"provider:alibaba",
|
||||
"protocol:alibaba-images",
|
||||
"region:ap-southeast-1"
|
||||
],
|
||||
"name": "alibaba-images/generates-qwen-image-images",
|
||||
"recordedAt": "2026-10-23T07:48:06.823Z"
|
||||
},
|
||||
"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\":[{\"text\":\"A simple flat red circle centered on a plain white background.\"}]}]},\"parameters\":{\"size\":\"512*512\",\"n\":2,\"seed\":7,\"prompt_extend\":false,\"watermark\":false}}"
|
||||
},
|
||||
"response": {
|
||||
"status": 200,
|
||||
"headers": {
|
||||
"content-type": "application/json"
|
||||
},
|
||||
"body": "{\"output\":{\"choices\":[{\"finish_reason\":\"stop\",\"message\":{\"content\":[{\"image\":\"https://dashscope-f4de.oss-accelerate.aliyuncs.com/7d/16/20261023/7693227b/425cfd51-077d-45fd-885b-da54b6300703.png?Expires=1793347486&OSSAccessKeyId=LTAI5tRcsWJEymQaTsKbKqGf&Signature=JFfz0Ki%2BDixXKTLhetlkKb1DEbY%3D\"},{\"image\":\"https://dashscope-f4de.oss-accelerate.aliyuncs.com/7d/d7/20261023/7693227b/1cc2b97c-9343-4512-a270-f230d35b18d7.png?Expires=1793347486&OSSAccessKeyId=LTAI5tRcsWJEymQaTsKbKqGf&Signature=1fB%2BMFNOgld2xQ1uXnjiPOAPaMI%3D\"}],\"role\":\"assistant\"}}]},\"usage\":{\"height\":512,\"image_count\":2,\"width\":512},\"request_id\":\"296a0628-da97-99f4-909d-2c0b2a618af8\"}"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
{
|
||||
"version": 1,
|
||||
"metadata": {
|
||||
"tags": [
|
||||
"prefix:alibaba-images",
|
||||
"provider:alibaba",
|
||||
"protocol:alibaba-images",
|
||||
"region:ap-southeast-1"
|
||||
],
|
||||
"name": "alibaba-images/generates-wan-images",
|
||||
"recordedAt": "2026-10-23T07:48:10.467Z"
|
||||
},
|
||||
"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\":[{\"text\":\"A simple flat blue square centered on a plain white background.\"}]}]},\"parameters\":{\"size\":\"768*768\",\"n\":2,\"thinking_mode\":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/1e/20261023/10739b93/1792741689_f8dbf733.png?Expires=1792828089&OSSAccessKeyId=LTAI5tRcsWJEymQaTsKbKqGf&Signature=l%2Bc3BSgj1M5I8qiliG9nzzklHJM%3D\",\"type\":\"image\"}],\"role\":\"assistant\"}},{\"finish_reason\":\"stop\",\"message\":{\"content\":[{\"image\":\"https://dashscope-463f.oss-accelerate.aliyuncs.com/1d/81/20261023/10739b93/1792741689_0ebcd768.png?Expires=1792828089&OSSAccessKeyId=LTAI5tRcsWJEymQaTsKbKqGf&Signature=gEdo6BJLstqpBJiAUImL7glnY1I%3D\",\"type\":\"image\"}],\"role\":\"assistant\"}}],\"finished\":true},\"usage\":{\"image_count\":2,\"input_tokens\":62,\"output_tokens\":4,\"size\":\"768*768\",\"total_tokens\":66},\"request_id\":\"0a347130-6308-9af1-80e8-99481c454c16\"}"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -206,6 +206,18 @@ describe("provider error classification", () => {
|
||||
).toBe("ContentPolicy")
|
||||
})
|
||||
|
||||
test("classifies Alibaba Model Studio data inspection failures", () => {
|
||||
const alibaba = {
|
||||
request_id: "1537ecc9-6873-93f7-9865-6fe4eb1d7851",
|
||||
code: "DataInspectionFailed",
|
||||
message: "Green net check failed for text (input): Input data may contain inappropriate content.",
|
||||
}
|
||||
|
||||
expect(
|
||||
classifyProviderFailure({ message: alibaba.message, status: 400, rawBody: JSON.stringify(alibaba) })._tag,
|
||||
).toBe("ContentPolicy")
|
||||
})
|
||||
|
||||
test("recovers policy codes that OpenCode Zen preserves only in its message label", () => {
|
||||
// Zen drops upstream codes outside its allow-list and replaces the type, but
|
||||
// `Protocol.errorMessage` keeps the original code as a `[code]` prefix.
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
import { describe, expect } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { Image, Media } from "../../src/index.js"
|
||||
import { Alibaba } from "../../src/providers.js"
|
||||
import { recordedTests } from "../recorded-test.js"
|
||||
|
||||
const alibaba = Alibaba.configure({ region: "ap-southeast-1", apiKey: process.env.ALIBABA_API_KEY ?? "fixture" })
|
||||
|
||||
const recorded = recordedTests({
|
||||
prefix: "alibaba-images",
|
||||
provider: "alibaba",
|
||||
protocol: "alibaba-images",
|
||||
requires: ["ALIBABA_API_KEY"],
|
||||
tags: ["region:ap-southeast-1"],
|
||||
})
|
||||
|
||||
describe("Alibaba Images recorded", () => {
|
||||
recorded.effect("generates Qwen-Image images", () =>
|
||||
Effect.gen(function* () {
|
||||
const response = yield* Image.generate({
|
||||
model: alibaba.image("qwen-image-2.0"),
|
||||
prompt: "A simple flat red circle centered on a plain white background.",
|
||||
n: 2,
|
||||
size: "512x512",
|
||||
seed: 7,
|
||||
providerOptions: { promptExtend: false, watermark: false },
|
||||
})
|
||||
|
||||
expect(response.images).toHaveLength(2)
|
||||
expect(response.image.source.type === "url" && response.image.source.url).toStartWith("https://")
|
||||
expect(response.providerMetadata?.alibaba).toMatchObject({ usage: { image_count: 2, width: 512, height: 512 } })
|
||||
}),
|
||||
)
|
||||
|
||||
recorded.effect("generates Wan images", () =>
|
||||
Effect.gen(function* () {
|
||||
const response = yield* Image.generate({
|
||||
model: alibaba.image("wan2.7-image"),
|
||||
prompt: "A simple flat blue square centered on a plain white background.",
|
||||
n: 2,
|
||||
size: "768x768",
|
||||
providerOptions: { thinkingMode: false },
|
||||
})
|
||||
|
||||
expect(response.images).toHaveLength(2)
|
||||
expect(response.providerMetadata?.alibaba).toMatchObject({ usage: { image_count: 2, size: "768*768" } })
|
||||
}),
|
||||
)
|
||||
|
||||
recorded.effect(
|
||||
"edits an image",
|
||||
() =>
|
||||
Effect.gen(function* () {
|
||||
const response = yield* Image.generate({
|
||||
model: alibaba.image("qwen-image-2.0"),
|
||||
prompt: "Keep the simple shape and change it from black to bright purple.",
|
||||
images: [
|
||||
Media.bytes(
|
||||
yield* Effect.promise(() => Bun.file("test/fixtures/images/edit-source.jpg").bytes()),
|
||||
"image/jpeg",
|
||||
),
|
||||
],
|
||||
size: "512x512",
|
||||
})
|
||||
|
||||
expect(response.images).toHaveLength(1)
|
||||
expect(response.image.source.type).toBe("url")
|
||||
}),
|
||||
{ timeout: 60_000 },
|
||||
)
|
||||
})
|
||||
@@ -0,0 +1,196 @@
|
||||
import { describe, expect } from "bun:test"
|
||||
import { Effect, Layer } from "effect"
|
||||
import { HttpClientRequest } from "effect/http"
|
||||
import { Image, ImageClient, Media } from "../../src/index.js"
|
||||
import { Alibaba } from "../../src/providers.js"
|
||||
import { it } from "../lib/effect.js"
|
||||
import { dynamicResponse, fixedResponse } from "../lib/http.js"
|
||||
|
||||
const json = { headers: { "content-type": "application/json" } }
|
||||
const result = JSON.stringify({
|
||||
output: { choices: [{ message: { content: [{ image: "https://oss.example/generated.png" }] } }] },
|
||||
})
|
||||
|
||||
describe("Alibaba Images", () => {
|
||||
it.effect("generates through the Model Studio multimodal generation API", () =>
|
||||
Effect.gen(function* () {
|
||||
const response = yield* Image.generate({
|
||||
model: Alibaba.configure({
|
||||
region: "cn-beijing",
|
||||
workspaceID: "llm-fixture",
|
||||
apiKey: "test",
|
||||
headers: { "x-default": "yes" },
|
||||
}).image("wan2.7-image-pro"),
|
||||
prompt: "Put the clock from image 1 into image 2",
|
||||
images: [Media.url("https://example.test/clock.png"), Media.bytes(new Uint8Array([1, 2, 3]), "image/png")],
|
||||
n: 2,
|
||||
size: "1024x768",
|
||||
seed: 7,
|
||||
providerOptions: {
|
||||
negativePrompt: "blurry",
|
||||
watermark: false,
|
||||
thinkingMode: true,
|
||||
bbox_list: [[], [[1, 2, 3, 4]]],
|
||||
},
|
||||
http: { body: { parameters: { watermark: true } } },
|
||||
})
|
||||
|
||||
expect(response.images.map((image) => image.source)).toEqual([
|
||||
{ type: "url", url: "https://oss.example/first.png", expiresAt: 24 * 60 * 60 * 1000 },
|
||||
{ type: "url", url: "https://oss.example/second.png", expiresAt: 24 * 60 * 60 * 1000 },
|
||||
])
|
||||
expect(response.providerMetadata).toEqual({
|
||||
alibaba: { requestID: "request-1", usage: { image_count: 2, size: "1024*768" } },
|
||||
})
|
||||
}).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://llm-fixture.cn-beijing.maas.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation",
|
||||
)
|
||||
expect(request.headers.get("authorization")).toBe("Bearer test")
|
||||
expect(request.headers.get("x-default")).toBe("yes")
|
||||
expect(JSON.parse(input.text)).toEqual({
|
||||
model: "wan2.7-image-pro",
|
||||
input: {
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: [
|
||||
{ image: "https://example.test/clock.png" },
|
||||
{ image: "data:image/png;base64,AQID" },
|
||||
{ text: "Put the clock from image 1 into image 2" },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
parameters: {
|
||||
size: "1024*768",
|
||||
n: 2,
|
||||
seed: 7,
|
||||
negative_prompt: "blurry",
|
||||
watermark: true,
|
||||
thinking_mode: true,
|
||||
bbox_list: [[], [[1, 2, 3, 4]]],
|
||||
},
|
||||
})
|
||||
return input.respond(
|
||||
JSON.stringify({
|
||||
output: {
|
||||
choices: [
|
||||
{ message: { content: [{ image: "https://oss.example/first.png", type: "image" }] } },
|
||||
{ message: { content: [{ image: "https://oss.example/second.png", type: "image" }] } },
|
||||
],
|
||||
finished: true,
|
||||
},
|
||||
usage: { image_count: 2, size: "1024*768" },
|
||||
request_id: "request-1",
|
||||
}),
|
||||
json,
|
||||
)
|
||||
}),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
it.effect("uses the regional shared host and complete base URL overrides", () =>
|
||||
Effect.gen(function* () {
|
||||
const urls: Array<string> = []
|
||||
const layer = ImageClient.layer.pipe(
|
||||
Layer.provide(
|
||||
dynamicResponse((input) =>
|
||||
Effect.sync(() => {
|
||||
urls.push(input.request.url)
|
||||
return input.respond(result, json)
|
||||
}),
|
||||
),
|
||||
),
|
||||
)
|
||||
for (const config of [
|
||||
{ region: "ap-southeast-1" },
|
||||
{ region: "us-east-1" },
|
||||
{ baseURL: "https://token-plan.example/api/v1" },
|
||||
])
|
||||
yield* Image.generate({
|
||||
model: Alibaba.configure({ ...config, apiKey: "test" }).image("qwen-image-2.0"),
|
||||
prompt: "test",
|
||||
}).pipe(Effect.provide(layer))
|
||||
|
||||
expect(urls).toEqual([
|
||||
"https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation",
|
||||
"https://dashscope-us.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation",
|
||||
"https://token-plan.example/api/v1/services/aigc/multimodal-generation/generation",
|
||||
])
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("rejects provider refs and unsupported common fields before sending", () =>
|
||||
Effect.gen(function* () {
|
||||
const model = Alibaba.configure({ region: "ap-southeast-1", apiKey: "test" }).image("qwen-image-2.0")
|
||||
const layer = ImageClient.layer.pipe(Layer.provide(fixedResponse(result, json)))
|
||||
const ref = yield* Image.generate({ model, prompt: "test", images: [Media.ref("alibaba", "file-1")] }).pipe(
|
||||
Effect.provide(layer),
|
||||
Effect.flip,
|
||||
)
|
||||
const mask = yield* Image.generate({
|
||||
model,
|
||||
prompt: "test",
|
||||
images: [Media.url("https://example.test/a.png")],
|
||||
mask: Media.url("https://example.test/mask.png"),
|
||||
}).pipe(Effect.provide(layer), Effect.flip)
|
||||
|
||||
expect(ref.reason._tag).toBe("InvalidRequest")
|
||||
expect(mask.reason._tag).toBe("UnsupportedOperation")
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("rejects invalid response structures", () =>
|
||||
Effect.gen(function* () {
|
||||
const model = Alibaba.configure({ region: "ap-southeast-1", apiKey: "test" }).image("qwen-image-2.0")
|
||||
const payloads = [
|
||||
{},
|
||||
{ output: { choices: [] } },
|
||||
{ output: { choices: [{ message: { content: [{ text: "no image" }] } }] } },
|
||||
{ output: { choices: [{ message: { content: [{ image: 1 }] } }] } },
|
||||
]
|
||||
|
||||
yield* Effect.forEach(payloads, (payload) =>
|
||||
Image.generate({ model, prompt: "test" }).pipe(
|
||||
Effect.provide(ImageClient.layer.pipe(Layer.provide(fixedResponse(JSON.stringify(payload), json)))),
|
||||
Effect.flip,
|
||||
Effect.tap((error) => Effect.sync(() => expect(error.reason._tag).toBe("InvalidProviderOutput"))),
|
||||
),
|
||||
)
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("surfaces Model Studio error messages", () =>
|
||||
Effect.gen(function* () {
|
||||
const error = yield* Image.generate({
|
||||
model: Alibaba.configure({ region: "ap-southeast-1", apiKey: "test" }).image("qwen-image-2.0"),
|
||||
prompt: "test",
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
ImageClient.layer.pipe(
|
||||
Layer.provide(
|
||||
fixedResponse(JSON.stringify({ code: "InvalidParameter", message: "Model not exist.", request_id: "r" }), {
|
||||
status: 400,
|
||||
...json,
|
||||
}),
|
||||
),
|
||||
),
|
||||
),
|
||||
Effect.flip,
|
||||
)
|
||||
|
||||
expect([error.reason._tag, error.reason.message]).toEqual(["InvalidRequest", "Model not exist."])
|
||||
}),
|
||||
)
|
||||
})
|
||||
Reference in New Issue
Block a user