修复 GPT Image 2.6 比例与像素尺寸的映射
This commit is contained in:
commit
121ca3014b
@ -1607,22 +1607,15 @@ export function buildTaskBody(params: Vip98kTaskParams, invocation: Vip98kInvoca
|
||||
const negativePrompt = String(videoParams.negativePrompt || imageParams.negativePrompt || "").trim();
|
||||
const quality = String(imageParams.quality || "").trim().toLowerCase();
|
||||
const outputConstraintFallbacks: string[] = [];
|
||||
let hasNativeAspectRatio = false;
|
||||
const aspectRatioFields = ["ratio", "aspectRatio", "aspect_ratio"].filter((key) =>
|
||||
invocationSupportsBodyField(invocation, key),
|
||||
);
|
||||
const hasSeparateAspectRatio = Boolean(aspectRatio && aspectRatioFields.length);
|
||||
let hasNativeAspectRatio = hasSeparateAspectRatio;
|
||||
let hasNativeResolution = false;
|
||||
|
||||
if (aspectRatio) {
|
||||
if (invocationSupportsBodyField(invocation, "ratio")) {
|
||||
body.ratio = aspectRatio;
|
||||
hasNativeAspectRatio = true;
|
||||
}
|
||||
if (invocationSupportsBodyField(invocation, "aspectRatio")) {
|
||||
body.aspectRatio = aspectRatio;
|
||||
hasNativeAspectRatio = true;
|
||||
}
|
||||
if (invocationSupportsBodyField(invocation, "aspect_ratio")) {
|
||||
body.aspect_ratio = aspectRatio;
|
||||
hasNativeAspectRatio = true;
|
||||
}
|
||||
for (const key of aspectRatioFields) body[key] = aspectRatio;
|
||||
}
|
||||
if (duration && invocationSupportsBodyField(invocation, "duration")) body.duration = duration;
|
||||
if (duration && invocationSupportsBodyField(invocation, "duration_seconds")) body.duration_seconds = duration;
|
||||
@ -1798,11 +1791,16 @@ export function buildTaskBody(params: Vip98kTaskParams, invocation: Vip98kInvoca
|
||||
if (!aspectRatio && automaticRatioField && hasNativeResolution && sizes?.includes(resolution)) {
|
||||
body[automaticRatioField] = "auto";
|
||||
body.size = resolution;
|
||||
} else if (ratios.includes(selectedRatio)) body.size = selectedRatio;
|
||||
else if (sizes?.includes(resolution) && hasNativeAspectRatio) body.size = resolution;
|
||||
} else if (ratios.includes(selectedRatio) && !(hasSeparateAspectRatio && pixels.length)) {
|
||||
// Some routes express the ratio through size alone. Routes with a
|
||||
// separate ratio and pixel presets can require both fields.
|
||||
body.size = selectedRatio;
|
||||
} else if (sizes?.includes(resolution) && hasNativeAspectRatio) body.size = resolution;
|
||||
else if (pixels.length && (resolution || aspectRatio)) {
|
||||
const [rw, rh] = selectedRatio.split(":").map(Number);
|
||||
const matching = pixels.filter(size => Math.abs(size.width / size.height - rw / rh) < 0.025);
|
||||
const matching = pixels.filter(
|
||||
({ width, height }) => Math.abs((width * rh) / (height * rw) - 1) < 0.02,
|
||||
);
|
||||
if (!matching.length && sizes?.includes("auto") && hasNativeAspectRatio && hasNativeResolution) {
|
||||
body.size = "auto";
|
||||
} else {
|
||||
|
||||
@ -571,6 +571,11 @@ function getSupportedImageSizes(model: Vip98kCatalogModel) {
|
||||
const primary = toRecord(toRecord(model.invocation?.input_schema)?.properties) || {};
|
||||
const resolutionEnum = parseStringArray(toRecord(primary.resolution)?.enum);
|
||||
if (resolutionEnum.length) return [...new Set(resolutionEnum.map(normalizeImageSize).filter((size): size is string => Boolean(size)))];
|
||||
// The published tier can include portrait presets with a longer pixel edge.
|
||||
const declaredSizes = getSupportedResolutions(model)
|
||||
.map(normalizeImageSize)
|
||||
.filter((size): size is string => Boolean(size));
|
||||
if (declaredSizes.length) return [...new Set(declaredSizes)];
|
||||
const schemaSizes = getInvocationCandidates(model).flatMap((invocation) => {
|
||||
const properties =
|
||||
toRecord(toRecord(invocation.input_schema)?.properties) || {};
|
||||
@ -578,7 +583,7 @@ function getSupportedImageSizes(model: Vip98kCatalogModel) {
|
||||
(key) => parseStringArray(toRecord(properties[key])?.enum),
|
||||
);
|
||||
});
|
||||
const sizes = [...getSupportedResolutions(model), ...schemaSizes]
|
||||
const sizes = schemaSizes
|
||||
.map(normalizeImageSize)
|
||||
.filter((value): value is string => Boolean(value));
|
||||
return sizes.length ? Array.from(new Set(sizes)) : [DEFAULT_IMAGE_SIZE];
|
||||
|
||||
@ -47,6 +47,57 @@ test("ratio-sized image contracts bind size and resolution without stale square
|
||||
const result = buildTaskBody(params,invocation({size:{enum:["1:1","16:9"]},resolution:{enum:["1K","2K"]}},{size:"1:1",resolution:"1K"}));
|
||||
expect(result.body).toMatchObject({size:"16:9",resolution:"2K",prompt:"a blue cup"});
|
||||
});
|
||||
|
||||
test("mixed size enums retain ratio aliases when size is the ratio field", () => {
|
||||
const { body } = buildTaskBody(params, invocation({
|
||||
size: { enum: ["16:9", "2048x1152"] },
|
||||
resolution: { enum: ["1K", "2K"] },
|
||||
}));
|
||||
expect(body).toMatchObject({ size: "16:9", resolution: "2K" });
|
||||
});
|
||||
|
||||
test("GPT Image 2.6 uses the published pixel preset with the requested ratio", () => {
|
||||
const presets = [
|
||||
["9:16", "941x1672"],
|
||||
["21:9", "1920x832"],
|
||||
["9:21", "832x1920"],
|
||||
["1:1", "1024x1024"],
|
||||
];
|
||||
const ratios = presets.map(([ratio]) => ratio);
|
||||
const contract = invocation({
|
||||
size: { enum: [...ratios, ...presets.map(([, size]) => size)] },
|
||||
aspect_ratio: { enum: ratios },
|
||||
image: { type: "array", items: { type: "string" } },
|
||||
quality: { enum: ["auto"] },
|
||||
}, { size: "1024x1024", image: [], quality: "auto" });
|
||||
const references = ["https://example.com/one.png"];
|
||||
|
||||
for (const [aspectRatio, size] of presets) {
|
||||
const { body } = buildTaskBody({
|
||||
...params, modelId: "gpt-image-2.6", imageUrls: references,
|
||||
imageParams: { aspectRatio, imageSize: "1K" },
|
||||
}, contract);
|
||||
expect(body).toMatchObject({ size, aspect_ratio: aspectRatio, image: references, quality: "auto" });
|
||||
}
|
||||
});
|
||||
|
||||
test("fixed-tier catalog models keep their declared tier for tall pixel presets", () => {
|
||||
const [preset] = buildVip98kModelPresets([{
|
||||
id: "gpt-image-2.6",
|
||||
type: "image",
|
||||
supported_resolutions: ["1K"],
|
||||
invocation: {
|
||||
kind: "tool", path: "/v1/images/generations",
|
||||
input_schema: { properties: { size: { enum: ["1024x1024", "941x1672"] } } },
|
||||
request_example: { model: "gpt-image-2.6", prompt: "", size: "1024x1024" },
|
||||
},
|
||||
}]);
|
||||
expect(preset.defaultParams.imageSize).toBe("1K");
|
||||
expect(preset.imageGenConfig).toMatchObject({
|
||||
supports1K: true, supports2K: false, supports4K: false, supportedImageSizes: ["1K"],
|
||||
});
|
||||
});
|
||||
|
||||
test("pixel-only image contracts use a supported size in the requested tier", () => {
|
||||
const result = buildTaskBody(params,invocation({size:{enum:["1024x1024","1280x720","2048x1152","3840x2160"]}},{size:"1024x1024"}));
|
||||
expect(result.body.size).toBe("2048x1152");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user