Merge pull request '参考图编辑按公开合同发送 JSON 和表单请求' (#91) from codex/98k-reference-edits-20261116 into main

This commit is contained in:
WuYongJie 2026-11-16 12:20:28 +08:00
commit 2d0238aca6
3 changed files with 63 additions and 5 deletions

View File

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

View File

@ -122,6 +122,7 @@ export function resolveVip98kImageTaskSize(params: {
const IMAGE_ARRAY_INPUT_KEYS = [
"file_urls",
"image[]",
"images",
"imageUrls",
"referenceImages",
@ -238,6 +239,7 @@ export function invocationSupportsVip98kImages(
export function bindVip98kImages(
body: Record<string, unknown>,
imageUrls: string[],
inputSchema?: Record<string, unknown> | null,
) {
const bindingKeys: string[] = [];
if (imageUrls.length === 0) {
@ -247,7 +249,12 @@ export function bindVip98kImages(
for (const key of IMAGE_ARRAY_INPUT_KEYS) {
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);
}
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 (invocation.kind === "task") score += 10;
if (invocation.path === "/extensions/v1/tasks") score += 10;
if (invocation.contentType === "application/json") score += 5;
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: {
modelId: string;
kind?: string;

View File

@ -2,7 +2,7 @@ import { test, expect } from "@playwright/test";
import { createVip98kChatModel } from "@/lib/ai/providers/vip98k-chat";
import { buildVip98kModelPresets } from "@/lib/ai/vip98k-models";
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 { audioRequestSchema } from "@/lib/video-canvas/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 张");
});
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", () => {
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);
@ -82,6 +112,7 @@ test("published first-frame ratio constraints reach the canvas and submission",
},
allOf: [{ allOf: [{
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" } } },
}] }],
};