fix: encode published image editing contracts correctly

This commit is contained in:
WuYongJie 2026-11-16 12:19:53 +08:00
parent c655bbd81e
commit 5701e95bb4
3 changed files with 63 additions and 5 deletions

View File

@ -22,6 +22,7 @@ import { getDefaultStorageConfig, getSystemSetting } from "@/lib/db/queries";
import { import {
bindVip98kImages, bindVip98kImages,
bindVip98kExpansionParams, bindVip98kExpansionParams,
encodeVip98kTaskBody,
getVip98kImageBindingKeys, getVip98kImageBindingKeys,
hasVerifiedVip98kOpenAiImageArrayContract, hasVerifiedVip98kOpenAiImageArrayContract,
normalizeVip98kInvocationHeaders, normalizeVip98kInvocationHeaders,
@ -1152,6 +1153,7 @@ async function submitTask(params: {
if (!path.startsWith("/")) throw new Error("VIP98K invocation path is invalid"); if (!path.startsWith("/")) throw new Error("VIP98K invocation path is invalid");
const stagedReferenceKeys = [ const stagedReferenceKeys = [
"file_urls", "file_urls",
"image[]",
"images", "images",
"image", "image",
"imageUrls", "imageUrls",
@ -1197,15 +1199,16 @@ async function submitTask(params: {
: params.body; : params.body;
let response: Response; let response: Response;
try { try {
const encoded = encodeVip98kTaskBody(requestBody, params.contentType);
response = await fetch(`${baseURL}${path}`, { response = await fetch(`${baseURL}${path}`, {
method: String(params.method || "POST").toUpperCase(), method: String(params.method || "POST").toUpperCase(),
headers: { headers: {
...normalizeVip98kInvocationHeaders(params.headers), ...normalizeVip98kInvocationHeaders(params.headers),
Authorization: `Bearer ${params.apiKey}`, Authorization: `Bearer ${params.apiKey}`,
"Content-Type": params.contentType || "application/json", ...encoded.headers,
"Idempotency-Key": idempotencyKey, "Idempotency-Key": idempotencyKey,
}, },
body: JSON.stringify(requestBody), body: encoded.body,
signal: AbortSignal.timeout(params.submissionTimeoutMs), signal: AbortSignal.timeout(params.submissionTimeoutMs),
}); });
} catch (error) { } catch (error) {
@ -1694,7 +1697,7 @@ export function buildTaskBody(params: Vip98kTaskParams, invocation: Vip98kInvoca
delete body.language; delete body.language;
delete body.size; delete body.size;
} }
const imageBindingKeys = bindVip98kImages(body, params.imageUrls); const imageBindingKeys = bindVip98kImages(body, params.imageUrls, invocation?.inputSchema);
const imageProperties = toRecord(invocation?.inputSchema?.properties) || {}; const imageProperties = toRecord(invocation?.inputSchema?.properties) || {};
for (const key of imageBindingKeys) { for (const key of imageBindingKeys) {
const limit = toRecord(imageProperties[key])?.maxItems; const limit = toRecord(imageProperties[key])?.maxItems;

View File

@ -122,6 +122,7 @@ export function resolveVip98kImageTaskSize(params: {
const IMAGE_ARRAY_INPUT_KEYS = [ const IMAGE_ARRAY_INPUT_KEYS = [
"file_urls", "file_urls",
"image[]",
"images", "images",
"imageUrls", "imageUrls",
"referenceImages", "referenceImages",
@ -238,6 +239,7 @@ export function invocationSupportsVip98kImages(
export function bindVip98kImages( export function bindVip98kImages(
body: Record<string, unknown>, body: Record<string, unknown>,
imageUrls: string[], imageUrls: string[],
inputSchema?: Record<string, unknown> | null,
) { ) {
const bindingKeys: string[] = []; const bindingKeys: string[] = [];
if (imageUrls.length === 0) { if (imageUrls.length === 0) {
@ -247,7 +249,12 @@ export function bindVip98kImages(
for (const key of IMAGE_ARRAY_INPUT_KEYS) { for (const key of IMAGE_ARRAY_INPUT_KEYS) {
if (!(key in body)) continue; if (!(key in body)) continue;
body[key] = imageUrls; const example = Array.isArray(body[key]) ? body[key][0] : undefined;
const properties = inputSchema?.properties as Record<string, { items?: { properties?: Record<string, unknown> } }> | undefined;
const objectReference = example && typeof example === "object" && "image_url" in example;
body[key] = objectReference || properties?.[key]?.items?.properties?.image_url
? imageUrls.map(image_url => ({ image_url }))
: imageUrls;
bindingKeys.push(key); bindingKeys.push(key);
} }
for (const key of IMAGE_SINGLE_INPUT_KEYS) { for (const key of IMAGE_SINGLE_INPUT_KEYS) {
@ -305,9 +312,26 @@ function scoreReferenceInvocation(invocation: Vip98kTaskInvocation) {
if (IMAGE_SINGLE_INPUT_KEYS.some((key) => key in body)) score += 15; if (IMAGE_SINGLE_INPUT_KEYS.some((key) => key in body)) score += 15;
if (invocation.kind === "task") score += 10; if (invocation.kind === "task") score += 10;
if (invocation.path === "/extensions/v1/tasks") score += 10; if (invocation.path === "/extensions/v1/tasks") score += 10;
if (invocation.contentType === "application/json") score += 5;
return score; return score;
} }
export function encodeVip98kTaskBody(body: Record<string, unknown>, contentType?: string | null): { body: string | FormData; headers: Record<string, string> } {
if (contentType?.split(";", 1)[0].trim() !== "multipart/form-data") {
return { body: JSON.stringify(body), headers: { "Content-Type": contentType || "application/json" } };
}
const form = new FormData();
for (const [key, value] of Object.entries(body)) {
if (value === undefined || value === null) continue;
if (key === "image[]" && Array.isArray(value)) {
for (const image of value) form.append(key, String(image));
} else {
form.append(key, typeof value === "object" ? JSON.stringify(value) : String(value));
}
}
return { body: form, headers: {} };
}
export function selectVip98kTaskInvocation(params: { export function selectVip98kTaskInvocation(params: {
modelId: string; modelId: string;
kind?: string; kind?: string;

View File

@ -2,7 +2,7 @@ import { test, expect } from "@playwright/test";
import { createVip98kChatModel } from "@/lib/ai/providers/vip98k-chat"; import { createVip98kChatModel } from "@/lib/ai/providers/vip98k-chat";
import { buildVip98kModelPresets } from "@/lib/ai/vip98k-models"; import { buildVip98kModelPresets } from "@/lib/ai/vip98k-models";
import { buildTaskBody } from "@/lib/ai/providers/vip98k"; import { buildTaskBody } from "@/lib/ai/providers/vip98k";
import { getVip98kImageBindingKeys } from "@/lib/ai/vip98k-task-invocation"; import { encodeVip98kTaskBody, getVip98kImageBindingKeys, selectVip98kTaskInvocation } from "@/lib/ai/vip98k-task-invocation";
import { getAudioCheckpoint } from "@/lib/video-canvas/task-recovery"; import { getAudioCheckpoint } from "@/lib/video-canvas/task-recovery";
import { audioRequestSchema } from "@/lib/video-canvas/audio-contract"; import { audioRequestSchema } from "@/lib/video-canvas/audio-contract";
import { buildVip98kAudioBody, vip98kAudioContracts } from "@/lib/video-canvas/vip98k-audio-contract"; import { buildVip98kAudioBody, vip98kAudioContracts } from "@/lib/video-canvas/vip98k-audio-contract";
@ -63,6 +63,36 @@ test("image reference limits reject excess inputs before submission", () => {
expect(() => buildTaskBody({...params,imageUrls:["https://example.com/1.png","https://example.com/2.png"]},invocation({image:{type:"array",maxItems:1}},{image:[]}))).toThrow("最多支持 1 张"); expect(() => buildTaskBody({...params,imageUrls:["https://example.com/1.png","https://example.com/2.png"]},invocation({image:{type:"array",maxItems:1}},{image:[]}))).toThrow("最多支持 1 张");
}); });
test("standard JSON edits bind reference objects and take precedence over multipart examples", () => {
const json = { ...invocation({ images: { type: "array", items: { properties: { image_url: { type: "string" } } } } }, {
images: [{ image_url: "https://example.com/catalog.png" }],
}), path: "/v1/images/edits" };
const multipart = { ...json, contentType: "multipart/form-data", requestExample: { model: "image", prompt: "example", "image[]": "@./input.png" } };
expect(selectVip98kTaskInvocation({ modelId: "image", kind: "image", configuredInvocation: multipart, configuredInvocations: [multipart, json], imageCount: 2 })).toEqual(json);
const urls = ["https://example.com/one.png", "https://example.com/two.png"];
const { body } = buildTaskBody({ ...params, imageUrls: urls }, json);
expect(body.images).toEqual(urls.map(image_url => ({ image_url })));
expect(body).not.toHaveProperty("file_urls");
expect(body).not.toHaveProperty("image[]");
const encoded = encodeVip98kTaskBody(body, json.contentType);
expect(encoded.headers).toEqual({ "Content-Type": "application/json" });
expect(JSON.parse(String(encoded.body)).images).toEqual(body.images);
});
test("multipart edits replace file placeholders and let fetch provide the boundary", async () => {
const contract = { ...invocation({ "image[]": {} }, { "image[]": "@./input.png", stream: false }), contentType: "multipart/form-data", path: "/v1/images/edits" };
const urls = ["https://example.com/one.png", "https://example.com/two.png"];
const { body } = buildTaskBody({ ...params, imageUrls: urls }, contract);
const encoded = encodeVip98kTaskBody(body, contract.contentType);
const request = new Request("https://relay.example/v1/images/edits", { method: "POST", ...encoded });
expect(request.headers.get("content-type")).toContain("multipart/form-data; boundary=");
const form = await request.formData();
expect(form.getAll("image[]")).toEqual(urls);
expect(form.get("stream")).toBe("false");
expect(form.get("prompt")).toContain("a blue cup");
expect(form.has("file_urls")).toBe(false);
});
test("Seedance content binds prompts and reference roles", () => { test("Seedance content binds prompts and reference roles", () => {
const i=invocation({content:{items:{oneOf:[{properties:{type:{const:"text"}}},{properties:{type:{const:"image_url"}}}]}},duration:{anyOf:[{const:-1},{minimum:4,maximum:30}]},resolution:{enum:["480P","720P"]},ratio:{enum:["16:9","9:16"]}}); const i=invocation({content:{items:{oneOf:[{properties:{type:{const:"text"}}},{properties:{type:{const:"image_url"}}}]}},duration:{anyOf:[{const:-1},{minimum:4,maximum:30}]},resolution:{enum:["480P","720P"]},ratio:{enum:["16:9","9:16"]}});
const result=buildTaskBody({...params,kind:"video",imageUrls:["https://example.com/one.png","https://example.com/two.png"],videoParams:{mode:"first-last-frame",resolution:"480p",duration:4,ratio:"16:9"}},i); const result=buildTaskBody({...params,kind:"video",imageUrls:["https://example.com/one.png","https://example.com/two.png"],videoParams:{mode:"first-last-frame",resolution:"480p",duration:4,ratio:"16:9"}},i);
@ -82,6 +112,7 @@ test("published first-frame ratio constraints reach the canvas and submission",
}, },
allOf: [{ allOf: [{ allOf: [{ allOf: [{
if: { properties: { content: { contains: { properties: { type: { const: "image_url" }, role: { const: "first_frame" } }, required: ["type"] } } } }, if: { properties: { content: { contains: { properties: { type: { const: "image_url" }, role: { const: "first_frame" } }, required: ["type"] } } } },
// biome-ignore lint/suspicious/noThenProperty: JSON Schema uses then for conditional constraints.
then: { properties: { ratio: { const: "adaptive" } } }, then: { properties: { ratio: { const: "adaptive" } } },
}] }], }] }],
}; };