mirror of
https://github.com/JimLiu/baoyu-skills.git
synced 2026-07-12 05:51:44 +08:00
fix(baoyu-image-gen): broaden OpenRouter model detection and aspect ratio validation
This commit is contained in:
@@ -12,7 +12,9 @@ import {
|
|||||||
} from "./openrouter.ts";
|
} from "./openrouter.ts";
|
||||||
|
|
||||||
const GEMINI_MODEL = "google/gemini-3.1-flash-image-preview";
|
const GEMINI_MODEL = "google/gemini-3.1-flash-image-preview";
|
||||||
const GEMINI_25_MODEL = "google/gemini-2.5-flash-image-preview";
|
const GEMINI_25_MODEL = "google/gemini-2.5-flash-image";
|
||||||
|
const GPT_5_IMAGE_MODEL = "openai/gpt-5-image";
|
||||||
|
const OPENROUTER_AUTO_MODEL = "openrouter/auto";
|
||||||
const FLUX_MODEL = "black-forest-labs/flux.2-pro";
|
const FLUX_MODEL = "black-forest-labs/flux.2-pro";
|
||||||
|
|
||||||
function makeArgs(overrides: Partial<CliArgs> = {}): CliArgs {
|
function makeArgs(overrides: Partial<CliArgs> = {}): CliArgs {
|
||||||
@@ -52,7 +54,36 @@ test("OpenRouter request body uses image_config and string content for text-only
|
|||||||
assert.equal(body.messages[0].content, "hello");
|
assert.equal(body.messages[0].content, "hello");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("OpenRouter request body omits image_config and provider guard when no image options are requested", () => {
|
test("OpenRouter request body keeps text+image modalities for current text+image models", () => {
|
||||||
|
for (const model of [GEMINI_MODEL, GEMINI_25_MODEL, GPT_5_IMAGE_MODEL, OPENROUTER_AUTO_MODEL]) {
|
||||||
|
const body = buildRequestBody("hello", model, makeArgs({ quality: "2k" }), []);
|
||||||
|
|
||||||
|
assert.deepEqual(body.image_config, {
|
||||||
|
image_size: "2K",
|
||||||
|
});
|
||||||
|
assert.deepEqual(body.provider, {
|
||||||
|
require_parameters: true,
|
||||||
|
});
|
||||||
|
assert.deepEqual(body.modalities, ["image", "text"]);
|
||||||
|
assert.equal(body.messages[0].content, "hello");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test("OpenRouter request body uses image-only modalities for image-only models under CLI defaults", () => {
|
||||||
|
const body = buildRequestBody("hello", FLUX_MODEL, makeArgs({ quality: "2k" }), []);
|
||||||
|
|
||||||
|
assert.deepEqual(body.image_config, {
|
||||||
|
image_size: "2K",
|
||||||
|
});
|
||||||
|
assert.deepEqual(body.provider, {
|
||||||
|
require_parameters: true,
|
||||||
|
});
|
||||||
|
assert.deepEqual(body.modalities, ["image"]);
|
||||||
|
assert.equal(body.stream, false);
|
||||||
|
assert.equal(body.messages[0].content, "hello");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("OpenRouter helper omits image_config when no size or quality is passed", () => {
|
||||||
const body = buildRequestBody("hello", FLUX_MODEL, makeArgs(), []);
|
const body = buildRequestBody("hello", FLUX_MODEL, makeArgs(), []);
|
||||||
|
|
||||||
assert.equal(body.image_config, undefined);
|
assert.equal(body.image_config, undefined);
|
||||||
@@ -72,21 +103,23 @@ test("OpenRouter request body keeps multimodal array content when references are
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test("OpenRouter size and aspect helpers infer expected defaults", () => {
|
test("OpenRouter size and aspect helpers infer supported values", () => {
|
||||||
assert.equal(getImageSize(makeArgs()), null);
|
assert.equal(getImageSize(makeArgs()), null);
|
||||||
assert.equal(getImageSize(makeArgs({ quality: "normal" })), "1K");
|
assert.equal(getImageSize(makeArgs({ quality: "normal" })), "1K");
|
||||||
assert.equal(getImageSize(makeArgs({ size: "2048x1024" })), "2K");
|
assert.equal(getImageSize(makeArgs({ size: "2048x1024" })), "2K");
|
||||||
assert.equal(getAspectRatio(GEMINI_MODEL, makeArgs({ size: "2048x1024" })), null);
|
|
||||||
assert.equal(getAspectRatio(GEMINI_MODEL, makeArgs({ size: "1600x900" })), "16:9");
|
assert.equal(getAspectRatio(GEMINI_MODEL, makeArgs({ size: "1600x900" })), "16:9");
|
||||||
assert.equal(getAspectRatio(GEMINI_MODEL, makeArgs({ size: "1024x4096" })), "1:4");
|
assert.equal(getAspectRatio(GEMINI_MODEL, makeArgs({ size: "1024x4096" })), "1:4");
|
||||||
assert.equal(getAspectRatio(GEMINI_25_MODEL, makeArgs({ size: "1024x4096" })), null);
|
assert.equal(getAspectRatio(GEMINI_25_MODEL, makeArgs({ size: "1600x900" })), "16:9");
|
||||||
assert.equal(getAspectRatio(FLUX_MODEL, makeArgs({ size: "1024x4096" })), null);
|
assert.equal(getAspectRatio(FLUX_MODEL, makeArgs({ size: "1024x4096" })), null);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("OpenRouter validates explicit aspect ratios against model support", () => {
|
test("OpenRouter validates explicit aspect ratios and inferred size ratios against model support", () => {
|
||||||
assert.doesNotThrow(() =>
|
assert.doesNotThrow(() =>
|
||||||
validateArgs(GEMINI_MODEL, makeArgs({ aspectRatio: "1:4" })),
|
validateArgs(GEMINI_MODEL, makeArgs({ aspectRatio: "1:4" })),
|
||||||
);
|
);
|
||||||
|
assert.doesNotThrow(() =>
|
||||||
|
validateArgs(GEMINI_MODEL, makeArgs({ size: "1024x4096" })),
|
||||||
|
);
|
||||||
assert.throws(
|
assert.throws(
|
||||||
() => validateArgs(GEMINI_25_MODEL, makeArgs({ aspectRatio: "1:4" })),
|
() => validateArgs(GEMINI_25_MODEL, makeArgs({ aspectRatio: "1:4" })),
|
||||||
/does not support aspect ratio 1:4/,
|
/does not support aspect ratio 1:4/,
|
||||||
@@ -95,6 +128,10 @@ test("OpenRouter validates explicit aspect ratios against model support", () =>
|
|||||||
() => validateArgs(FLUX_MODEL, makeArgs({ aspectRatio: "1:4" })),
|
() => validateArgs(FLUX_MODEL, makeArgs({ aspectRatio: "1:4" })),
|
||||||
/does not support aspect ratio 1:4/,
|
/does not support aspect ratio 1:4/,
|
||||||
);
|
);
|
||||||
|
assert.throws(
|
||||||
|
() => validateArgs(GEMINI_MODEL, makeArgs({ size: "2048x1024" })),
|
||||||
|
/does not support size 2048x1024 \(aspect ratio 2:1\)/,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("OpenRouter response extraction supports inline image data and finish_reason errors", async () => {
|
test("OpenRouter response extraction supports inline image data and finish_reason errors", async () => {
|
||||||
|
|||||||
@@ -48,9 +48,21 @@ function normalizeModelId(model: string): string {
|
|||||||
return model.trim().toLowerCase().split(":")[0]!;
|
return model.trim().toLowerCase().split(":")[0]!;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isGeminiImageModel(model: string): boolean {
|
function isTextAndImageModel(model: string): boolean {
|
||||||
const normalized = normalizeModelId(model);
|
const normalized = normalizeModelId(model);
|
||||||
return normalized.startsWith("google/gemini-") && normalized.includes("image-preview");
|
if (normalized === "openrouter/auto") {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (normalized.startsWith("google/gemini-") && normalized.includes("image")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (normalized.startsWith("openai/gpt-") && normalized.includes("image")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSupportedAspectRatios(model: string): Set<string> {
|
function getSupportedAspectRatios(model: string): Set<string> {
|
||||||
@@ -159,21 +171,26 @@ export function getAspectRatio(model: string, args: CliArgs): string | null {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getModalities(model: string): string[] {
|
function getModalities(model: string): string[] {
|
||||||
return isGeminiImageModel(model) ? ["image", "text"] : ["image"];
|
return isTextAndImageModel(model) ? ["image", "text"] : ["image"];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function validateArgs(model: string, args: CliArgs): void {
|
export function validateArgs(model: string, args: CliArgs): void {
|
||||||
if (!args.aspectRatio) {
|
const requestedAspectRatio = args.aspectRatio || inferAspectRatio(args.size);
|
||||||
|
if (!requestedAspectRatio) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const supported = getSupportedAspectRatios(model);
|
const supported = getSupportedAspectRatios(model);
|
||||||
if (supported.has(args.aspectRatio)) {
|
if (supported.has(requestedAspectRatio)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const requestedValue = args.aspectRatio
|
||||||
|
? `aspect ratio ${requestedAspectRatio}`
|
||||||
|
: `size ${args.size} (aspect ratio ${requestedAspectRatio})`;
|
||||||
|
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`OpenRouter model ${model} does not support aspect ratio ${args.aspectRatio}. Supported values: ${Array.from(supported).join(", ")}`
|
`OpenRouter model ${model} does not support ${requestedValue}. Supported values: ${Array.from(supported).join(", ")}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -276,6 +293,8 @@ export function buildRequestBody(
|
|||||||
args: CliArgs,
|
args: CliArgs,
|
||||||
referenceImages: string[],
|
referenceImages: string[],
|
||||||
): Record<string, unknown> {
|
): Record<string, unknown> {
|
||||||
|
validateArgs(model, args);
|
||||||
|
|
||||||
const imageConfig: Record<string, string> = {};
|
const imageConfig: Record<string, string> = {};
|
||||||
|
|
||||||
const imageSize = getImageSize(args);
|
const imageSize = getImageSize(args);
|
||||||
|
|||||||
Reference in New Issue
Block a user