From 56f0cbc15c36adf7942e160a97939bff39b6b3cb Mon Sep 17 00:00:00 2001 From: Michael Livshits Date: Mon, 23 Nov 2026 19:01:33 +0200 Subject: [PATCH 1/4] fix: bundled together and openai image plugins use current models, and images are told by their bytes (SHFT-2983) --- .../codemode-plugins/_shared/mediaFile.ts | 60 ++++++---------- packages/codemode-plugins/fal/src/index.ts | 6 +- .../codemode-plugins/googleImage/src/index.ts | 5 +- packages/codemode-plugins/openai/src/index.ts | 53 ++++++-------- .../codemode-plugins/replicate/src/index.ts | 7 +- .../together/src/credentials.ts | 2 +- .../codemode-plugins/together/src/index.ts | 35 ++++++---- packages/codemode-plugins/xai/src/index.ts | 8 ++- .../src/tests/credential-fixtures/together.ts | 2 +- .../src/tests/image-edit-plugins.test.ts | 70 +++++++++++++------ .../codemode/src/tests/media-file.test.ts | 16 +++++ .../src/tests/plugin-input-helpers.test.ts | 23 ++++-- 12 files changed, 162 insertions(+), 125 deletions(-) diff --git a/packages/codemode-plugins/_shared/mediaFile.ts b/packages/codemode-plugins/_shared/mediaFile.ts index efedbee5..cf15377b 100644 --- a/packages/codemode-plugins/_shared/mediaFile.ts +++ b/packages/codemode-plugins/_shared/mediaFile.ts @@ -8,8 +8,12 @@ * send_file/attachment tool. Plugins load in-process via jiti, so node:fs is * available (same pattern as googleDrive/googleSlides). */ -import { basename, extname } from "node:path"; -import { readLocalFile, writeOutputFile } from "@shiftlabs/codemode"; +import { + imageFacts, + type LocalImage, + readLocalImage, + writeOutputFile, +} from "@shiftlabs/codemode"; const MIME_EXT: Record = { "image/png": "png", @@ -83,8 +87,8 @@ export function writeMediaFile(opts: { } /** - * Decode base64 media bytes and write them, for the providers that answer - * with base64 rather than a URL to download. + * Decode an image a provider answers in base64 and write it. What the image + * is, its bytes say: `mimeType` is used only when they tell no type. */ export function writeImageFile(opts: { base64: string; @@ -94,60 +98,38 @@ export function writeImageFile(opts: { saveDir?: string; stamp?: number; }): SavedMedia { - const { base64, ...rest } = opts; + const { base64, mimeType, ...rest } = opts; + const bytes = Buffer.from(base64 ?? "", "base64"); return writeMediaFile({ ...rest, - bytes: Buffer.from(base64 ?? "", "base64"), + bytes, + mimeType: imageFacts(bytes)?.mimeType ?? mimeType, }); } export const SEND_FILE_NOTE = "File(s) written to disk. Deliver each to the user with send_file using its `path`."; -const EXT_MIME: Record = { - png: "image/png", - jpg: "image/jpeg", - jpeg: "image/jpeg", - webp: "image/webp", - gif: "image/gif", -}; - -export interface LoadedImage { - bytes: Buffer; - base64: string; - mimeType: string; - dataUri: string; - fileName: string; -} - -/** Larger than any provider accepts inline; a data URI past it only fails remotely. */ +/** Larger than any provider accepts inline; an image past it only fails remotely. */ const MAX_IMAGE_BYTES = 50 * 1024 * 1024; +/** The types of image an `image.edit` call takes. */ +const IMAGE_TYPES = ["image/png", "image/jpeg", "image/webp", "image/gif"]; /** - * Read a local image for an `image.edit` call: bytes + base64 + mime - * (from the extension) + a `data:` URI for JSON APIs. Errors carry the - * plugin's name so they point at the right tool. + * Read a local image for an `image.edit` call. What it is, its bytes say, + * never its name. Errors carry the plugin's name so they point at the right + * tool. */ export async function readImageInput( imagePath: string, pluginName: string, -): Promise { +): Promise { if (typeof imagePath !== "string" || imagePath.trim().length === 0) { throw new Error(`${pluginName}: imagePath is required`); } - const bytes = await readLocalFile(imagePath, { + return readLocalImage(imagePath, { subject: `${pluginName}: imagePath`, maxBytes: MAX_IMAGE_BYTES, - oversize: `${pluginName}: imagePath exceeds ${MAX_IMAGE_BYTES / (1024 * 1024)} MiB`, + types: IMAGE_TYPES, }); - const ext = extname(imagePath).slice(1).toLowerCase(); - const mimeType = EXT_MIME[ext] ?? "image/png"; - const base64 = bytes.toString("base64"); - return { - bytes, - base64, - mimeType, - dataUri: `data:${mimeType};base64,${base64}`, - fileName: basename(imagePath), - }; } diff --git a/packages/codemode-plugins/fal/src/index.ts b/packages/codemode-plugins/fal/src/index.ts index 32b58e1e..032e2c58 100644 --- a/packages/codemode-plugins/fal/src/index.ts +++ b/packages/codemode-plugins/fal/src/index.ts @@ -1,4 +1,4 @@ -import type { CodemodePluginAPI } from "@shiftlabs/codemode"; +import { type CodemodePluginAPI, imageDataUrl } from "@shiftlabs/codemode"; import * as t from "typebox"; import { mediaDir, readImageInput } from "../../_shared/mediaFile.js"; import { parseSize } from "../../_shared/parseSize.js"; @@ -183,7 +183,9 @@ export default function fal(rl: CodemodePluginAPI): void { p.model ?? EDIT_MODEL, { prompt: p.prompt, - [key]: key.endsWith("_urls") ? [image.dataUri] : image.dataUri, + [key]: key.endsWith("_urls") + ? [imageDataUrl(image)] + : imageDataUrl(image), }, "images", ); diff --git a/packages/codemode-plugins/googleImage/src/index.ts b/packages/codemode-plugins/googleImage/src/index.ts index 529febce..25e3d996 100644 --- a/packages/codemode-plugins/googleImage/src/index.ts +++ b/packages/codemode-plugins/googleImage/src/index.ts @@ -22,6 +22,7 @@ import { type ActionContext, type CodemodePluginAPI, credentialJson, + imageBase64, pathSegment, } from "@shiftlabs/codemode"; import * as t from "typebox"; @@ -165,7 +166,9 @@ export default function googleImage(rl: CodemodePluginAPI) { { parts: [ { text: p.prompt }, - { inlineData: { mimeType: img.mimeType, data: img.base64 } }, + { + inlineData: { mimeType: img.mimeType, data: imageBase64(img) }, + }, ], }, ], diff --git a/packages/codemode-plugins/openai/src/index.ts b/packages/codemode-plugins/openai/src/index.ts index 2197b606..cda7758c 100644 --- a/packages/codemode-plugins/openai/src/index.ts +++ b/packages/codemode-plugins/openai/src/index.ts @@ -1,7 +1,7 @@ /** * OpenAI image generation for codemode. * - * Wraps the GPT Image / DALL-E line: `image.create` at + * Wraps the GPT Image line: `image.create` at * /v1/images/generations, `image.edit` at /v1/images/edits. Generated * images are written to disk and the action returns their file `path`s * (plus the optional revised prompt) — never raw base64, which bloats the @@ -27,15 +27,17 @@ import { import { openaiCredential } from "./credentials.js"; /** - * Newest GPT Image model; override per call or via the connection. + * The default model; override per call or via the connection. * - * The GPT Image 2.5 line ships as two models: `gpt-image-2.5-flare` - * (OpenAI's own default — gpt-image-2 quality at up to half the latency) - * and `gpt-image-2.5-sunburst` (precision editing). Both widen the - * quality range and take custom sizes; see QUALITY_RULE and SIZE_RULE. - * Pin `gpt-image-2` via `defaultModel` to stay on the older line. + * Each GPT Image line from 2.5 ships as two models: Flare, the faster one, + * and Sunburst, for detailed work and precise edits. Pin an earlier model + * via `defaultModel` to stay on it. */ -const DEFAULT_MODEL = "gpt-image-2.5-flare"; +const DEFAULT_MODEL = "gpt-image-2.6-flare"; + +/** The models the inputs name. The API takes any GPT Image model the key reaches. */ +const MODELS = + "gpt-image-2.6-flare | gpt-image-2.6-sunburst | gpt-image-2.5-flare | gpt-image-2.5-sunburst | gpt-image-2"; /** * The size contract, shared by create and edit so the two cannot drift apart. @@ -44,13 +46,13 @@ const DEFAULT_MODEL = "gpt-image-2.5-flare"; * surprises, because 512x512 satisfies every other rule and still 400s. */ const SIZE_RULE = - "gpt-image-2 and gpt-image-2.5-* take any WIDTHxHEIGHT with both sides multiples of 16, aspect between 1:3 and 3:1, " + + "Any WIDTHxHEIGHT with both sides multiples of 16, aspect between 1:3 and 3:1, " + "no edge over 3840, and a total between 655,360 and 8,294,400 pixels — so 512x512 is too small and 1536x864 is fine; " + - "above 2560x1440 is experimental. gpt-image-1* and dall-e-* take 1024x1024 | 1536x1024 | 1024x1536."; + "above 2560x1440 is experimental. 1024x1024 | 1536x1024 | 1024x1536 work on every model."; -/** Quality tiers, likewise shared. `auto` is what the 2.5 models pick by default. */ +/** Quality tiers, likewise shared. `auto` is what the Flare and Sunburst models pick by default. */ const QUALITY_RULE = - "low | medium | high | auto, plus xhigh | max on gpt-image-2.5-*"; + "low | medium | high | auto, plus xhigh | max on the Flare and Sunburst models"; /** * Model precedence, shared by create and edit: the call wins, then the @@ -71,7 +73,6 @@ interface CreateInput { model?: string; size?: string; quality?: string; - style?: string; n?: number; saveDir?: string; } @@ -108,7 +109,7 @@ export default function openai(rl: CodemodePluginAPI) { type: "string", required: false, description: - "Default image model when a call omits `model` — e.g. gpt-image-2 to pin the older line. Defaults to gpt-image-2.5-flare.", + `Default image model when a call omits \`model\` — e.g. gpt-image-2 to pin an earlier line. Defaults to ${DEFAULT_MODEL}.`, env: "OPENAI_IMAGE_MODEL", }, }); @@ -116,7 +117,7 @@ export default function openai(rl: CodemodePluginAPI) { rl.registerAction("image.create", { access: "write", description: - "Generate an image with OpenAI (GPT Image / DALL-E). Writes the PNG(s) to disk and returns their file `path`s (plus any revised prompt) — not base64. Deliver each image to the user with send_file using its `path`.", + "Generate an image with OpenAI (GPT Image). Writes the PNG(s) to disk and returns their file `path`s (plus any revised prompt) — not base64. Deliver each image to the user with send_file using its `path`.", inputSchema: { prompt: { type: "string", @@ -131,8 +132,7 @@ export default function openai(rl: CodemodePluginAPI) { model: { type: "string", required: false, - description: - "gpt-image-2.5-flare | gpt-image-2.5-sunburst | gpt-image-2 | gpt-image-1 | gpt-image-1-mini | dall-e-3 | dall-e-2. Omit to use the connection default.", + description: `${MODELS}. Omit to use the connection default.`, }, size: { type: "string", @@ -142,12 +142,7 @@ export default function openai(rl: CodemodePluginAPI) { quality: { type: "string", required: false, - description: `${QUALITY_RULE}; standard | hd (dall-e-3)`, - }, - style: { - type: "string", - required: false, - description: "vivid | natural — DALL-E 3 only", + description: QUALITY_RULE, }, n: { type: "number", @@ -168,16 +163,9 @@ export default function openai(rl: CodemodePluginAPI) { prompt: p.prompt, n: Math.min(p.n ?? 1, 4), size: p.size ?? "1024x1024", + output_format: "png", }; - // gpt-image-* uses output_format; dall-e-* uses response_format. - // Sending the wrong key 400s, so the model name decides. - if (model.startsWith("dall-e")) { - body.response_format = "b64_json"; - } else { - body.output_format = "png"; - } if (p.quality) body.quality = p.quality; - if (p.style) body.style = p.style; const data = (await credentialJson(ctx, openaiCredential, "openai", { target: "api", @@ -231,8 +219,7 @@ export default function openai(rl: CodemodePluginAPI) { model: { type: "string", required: false, - description: - "gpt-image-2.5-flare | gpt-image-2.5-sunburst | gpt-image-2 | gpt-image-1 | gpt-image-1-mini. Omit to use the connection default.", + description: `${MODELS}. Omit to use the connection default.`, }, size: { type: "string", diff --git a/packages/codemode-plugins/replicate/src/index.ts b/packages/codemode-plugins/replicate/src/index.ts index 150a97a1..fd123307 100644 --- a/packages/codemode-plugins/replicate/src/index.ts +++ b/packages/codemode-plugins/replicate/src/index.ts @@ -21,10 +21,11 @@ import { Buffer } from "node:buffer"; import { type ActionContext, + type CodemodePluginAPI, credentialJson, + imageDataUrl, pathSegments, pathWithin, - type CodemodePluginAPI, } from "@shiftlabs/codemode"; import { mediaDir, @@ -323,7 +324,9 @@ export default function replicate(rl: CodemodePluginAPI) { model, input: { prompt: p.prompt, - [imageKey]: imageKey.endsWith("_input") ? [img.dataUri] : img.dataUri, + [imageKey]: imageKey.endsWith("_input") + ? [imageDataUrl(img)] + : imageDataUrl(img), }, timeoutMs: p.timeoutMs ?? DEFAULT_TIMEOUT_MS, saveDir: mediaDir(p.saveDir, ctx), diff --git a/packages/codemode-plugins/together/src/credentials.ts b/packages/codemode-plugins/together/src/credentials.ts index 75af0176..27a9665d 100644 --- a/packages/codemode-plugins/together/src/credentials.ts +++ b/packages/codemode-plugins/together/src/credentials.ts @@ -10,7 +10,7 @@ export const togetherCredential = staticCredential({ local: { secret: "apiKey" }, targets: { api: { - baseUrl: "https://api.together.xyz/v1/", + baseUrl: "https://api.together.ai/v1/", methods: ["POST"], }, }, diff --git a/packages/codemode-plugins/together/src/index.ts b/packages/codemode-plugins/together/src/index.ts index 43d9737d..f9d8845b 100644 --- a/packages/codemode-plugins/together/src/index.ts +++ b/packages/codemode-plugins/together/src/index.ts @@ -4,19 +4,20 @@ * await together.image.create({ prompt: "a cyberpunk skyline at dusk" }) * await together.image.create({ * prompt: "studio product shot, white background", - * model: "black-forest-labs/FLUX.1-dev", + * model: "black-forest-labs/FLUX.2-flex", * steps: 28, * }) * - * Default model is FLUX.1-schnell — fastest, 4 steps. For better - * fidelity switch to FLUX.1-dev / Ideogram / Qwen-Image and bump - * `steps` accordingly (20–30 is typical for non-schnell models). + * The default model is FLUX.2 [dev]. `steps` is sent only when a call sets + * it: FLUX.2 [dev] and [flex] and the Kontext line take it, FLUX.2 [pro] and + * FLUX1.1 [pro] do not, and each model has a default of its own. */ import { type ActionContext, - credentialJson, type CodemodePluginAPI, + credentialJson, + imageDataUrl, } from "@shiftlabs/codemode"; import { mediaDir, @@ -28,6 +29,11 @@ import { import { parseSize } from "../../_shared/parseSize.js"; import { togetherCredential } from "./credentials.js"; +const CREATE_MODEL = "black-forest-labs/FLUX.2-dev"; +const EDIT_MODEL = "black-forest-labs/FLUX.1-kontext-pro"; +const STEPS_DESCRIPTION = + "Inference steps, for a model that takes them (FLUX.2 dev and flex, FLUX.1 Kontext). The default is the model's"; + function generate( ctx: ActionContext, body: Record, @@ -95,7 +101,7 @@ export default function together(rl: CodemodePluginAPI) { type: "string", required: false, description: - "Together model id, e.g. black-forest-labs/FLUX.1-schnell (default), black-forest-labs/FLUX.1-dev, ideogram/ideogram-3.0, Qwen/Qwen-Image", + `Together model id, e.g. ${CREATE_MODEL} (default), black-forest-labs/FLUX.2-pro, ideogram/ideogram-4.0, Qwen/Qwen-Image`, }, size: { type: "string", @@ -105,8 +111,7 @@ export default function together(rl: CodemodePluginAPI) { steps: { type: "number", required: false, - description: - "Inference steps (default: 4 for FLUX.1-schnell). Use 20–30 for FLUX.1-dev or Ideogram.", + description: STEPS_DESCRIPTION, }, n: { type: "number", @@ -120,7 +125,7 @@ export default function together(rl: CodemodePluginAPI) { throw new Error("together: prompt is required"); } - const model = p.model ?? "black-forest-labs/FLUX.1-schnell"; + const model = p.model ?? CREATE_MODEL; const { width, height } = parseSize(p.size, "together"); const data = await generate(ctx, { @@ -128,7 +133,7 @@ export default function together(rl: CodemodePluginAPI) { prompt: p.prompt, width, height, - steps: p.steps ?? 4, + ...(p.steps === undefined ? {} : { steps: p.steps }), n: Math.min(p.n ?? 1, 4), response_format: "base64", }); @@ -171,12 +176,12 @@ export default function together(rl: CodemodePluginAPI) { type: "string", required: false, description: - "Edit-capable model, e.g. black-forest-labs/FLUX.1-kontext-pro (default), black-forest-labs/FLUX.1-kontext-max", + `Edit-capable model, e.g. ${EDIT_MODEL} (default), black-forest-labs/FLUX.1-kontext-max`, }, steps: { type: "number", required: false, - description: "Inference steps (default: 28)", + description: STEPS_DESCRIPTION, }, n: { type: "number", @@ -191,13 +196,13 @@ export default function together(rl: CodemodePluginAPI) { } const img = await readImageInput(p.imagePath, "together"); - const model = p.model ?? "black-forest-labs/FLUX.1-kontext-pro"; + const model = p.model ?? EDIT_MODEL; const data = await generate(ctx, { model, prompt: p.prompt, - image_url: img.dataUri, - steps: p.steps ?? 28, + image_url: imageDataUrl(img), + ...(p.steps === undefined ? {} : { steps: p.steps }), n: Math.min(p.n ?? 1, 4), response_format: "base64", }); diff --git a/packages/codemode-plugins/xai/src/index.ts b/packages/codemode-plugins/xai/src/index.ts index a6438298..9d7cd3f9 100644 --- a/packages/codemode-plugins/xai/src/index.ts +++ b/packages/codemode-plugins/xai/src/index.ts @@ -9,7 +9,11 @@ * the API does the math. */ -import { credentialJson, type CodemodePluginAPI } from "@shiftlabs/codemode"; +import { + type CodemodePluginAPI, + credentialJson, + imageDataUrl, +} from "@shiftlabs/codemode"; import { mediaDir, readImageInput, @@ -167,7 +171,7 @@ export default function xai(rl: CodemodePluginAPI) { json: { model, prompt: p.prompt, - image: { url: img.dataUri }, + image: { url: imageDataUrl(img) }, n: Math.min(p.n ?? 1, 10), response_format: "b64_json", }, diff --git a/packages/codemode/src/tests/credential-fixtures/together.ts b/packages/codemode/src/tests/credential-fixtures/together.ts index a9d4c9d2..86eb9297 100644 --- a/packages/codemode/src/tests/credential-fixtures/together.ts +++ b/packages/codemode/src/tests/credential-fixtures/together.ts @@ -11,7 +11,7 @@ export default { response: { data: [] }, target: "api", wire: { - url: "https://api.together.xyz/v1/images/generations", + url: "https://api.together.ai/v1/images/generations", header: ["authorization", "Bearer tg_secret"], }, } satisfies CredentialFixture; diff --git a/packages/codemode/src/tests/image-edit-plugins.test.ts b/packages/codemode/src/tests/image-edit-plugins.test.ts index 6a9e2c34..7bdea33a 100644 --- a/packages/codemode/src/tests/image-edit-plugins.test.ts +++ b/packages/codemode/src/tests/image-edit-plugins.test.ts @@ -19,7 +19,7 @@ afterEach(() => { globalThis.fetch = originalFetch; }); -// A real 1x1 transparent PNG so readImageInput has bytes to load. +// A real 1x1 transparent PNG, which readImageInput reads as one from its bytes. const PNG_1X1 = Buffer.from( "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==", "base64", @@ -101,7 +101,7 @@ describe("openai image.edit", () => { assert.equal(seen.url, "https://api.openai.com/v1/images/edits"); const form = await multipartForm(seen.init); assert.equal(form.get("prompt"), "make the sky red"); - assert.equal(form.get("model"), "gpt-image-2.5-flare"); + assert.equal(form.get("model"), "gpt-image-2.6-flare"); const file = form.get("image[]"); assert.ok(file instanceof Blob, "image[] should be a file part"); assert.equal(result.images.length, 1); @@ -195,7 +195,7 @@ describe("openai image.create", () => { return bodies; } - it("defaults to gpt-image-2.5-flare", async () => { + it("defaults to gpt-image-2.6-flare", async () => { const action = getAction(makePlugin("openai", openai), "image.create"); const bodies = captureBodies(); @@ -204,8 +204,8 @@ describe("openai image.create", () => { ctx({ apiKey: "sk-test" }), )) as { model: string }; - assert.equal(bodies[0]?.model, "gpt-image-2.5-flare"); - assert.equal(result.model, "gpt-image-2.5-flare"); + assert.equal(bodies[0]?.model, "gpt-image-2.6-flare"); + assert.equal(result.model, "gpt-image-2.6-flare"); }); it("prefers the per-call model, then the connection default", async () => { @@ -227,11 +227,11 @@ describe("openai image.create", () => { ); }); - it("treats both gpt-image-2.5 models as the gpt-image line (output_format, not response_format)", async () => { + it("asks every model for a PNG, and sends no input of the retired DALL-E line", async () => { const action = getAction(makePlugin("openai", openai), "image.create"); const bodies = captureBodies(); - for (const model of ["gpt-image-2.5-flare", "gpt-image-2.5-sunburst"]) { + for (const model of ["gpt-image-2.6-flare", "gpt-image-2"]) { await action.execute( { prompt: "x", model, saveDir: dir }, ctx({ apiKey: "sk-test" }), @@ -241,6 +241,7 @@ describe("openai image.create", () => { for (const body of bodies) { assert.equal(body.output_format, "png"); assert.equal(body.response_format, undefined); + assert.equal(body.style, undefined); } }); @@ -269,32 +270,30 @@ describe("openai image.create", () => { assert.equal(bodies[1]?.size, "auto"); }); - it("still speaks response_format to the dall-e line", async () => { - const action = getAction(makePlugin("openai", openai), "image.create"); - const bodies = captureBodies(); - - await action.execute( - { prompt: "x", model: "dall-e-3", saveDir: dir }, - ctx({ apiKey: "sk-test" }), - ); - - assert.equal(bodies[0]?.response_format, "b64_json"); - assert.equal(bodies[0]?.output_format, undefined); - }); - - it("documents both gpt-image-2.5 models on the model inputs", () => { + it("documents the current models on the model inputs, and none that are shut down", () => { const plugin = makePlugin("openai", openai); for (const name of ["image.create", "image.edit"]) { const schema = getAction(plugin, name).inputSchema as Record< string, { description?: string } >; - for (const model of ["gpt-image-2.5-flare", "gpt-image-2.5-sunburst"]) { + for (const model of [ + "gpt-image-2.6-flare", + "gpt-image-2.6-sunburst", + "gpt-image-2.5-flare", + "gpt-image-2.5-sunburst", + "| gpt-image-2.", + ]) { assert.ok( schema.model?.description?.includes(model), `${name}.model should mention ${model}`, ); } + assert.doesNotMatch( + JSON.stringify(schema), + /dall-e|gpt-image-1\b/i, + `${name} should name no model that is shut down`, + ); assert.ok(schema.quality?.description?.includes("xhigh")); assert.ok(schema.quality?.description?.includes("max")); // Three of the four size rules let 512x512 through; only the total-pixel @@ -441,6 +440,31 @@ describe("recraft image.edit", () => { }); }); +describe("together image.create", () => { + it("asks its current address for the default model, with steps only when a call sets them", async () => { + const action = getAction(makePlugin("together", together), "image.create"); + const sent: Array<{ url: string; body: Record }> = []; + stubFetch(async (input: RequestInfo | URL, init?: RequestInit) => { + sent.push({ url: String(input), body: JSON.parse(String(init?.body)) }); + return Response.json({ data: [{ b64_json: B64_RESULT }] }); + }); + + await action.execute( + { prompt: "a fox", saveDir: dir }, + ctx({ apiKey: "t-test" }), + ); + await action.execute( + { prompt: "a fox", steps: 12, saveDir: dir }, + ctx({ apiKey: "t-test" }), + ); + + assert.equal(sent[0].url, "https://api.together.ai/v1/images/generations"); + assert.equal(sent[0].body.model, "black-forest-labs/FLUX.2-dev"); + assert.ok(!("steps" in sent[0].body)); + assert.equal(sent[1].body.steps, 12); + }); +}); + describe("together image.edit", () => { it("sends image_url data URI to the generations endpoint with a Kontext default", async () => { const action = getAction(makePlugin("together", together), "image.edit"); @@ -463,7 +487,7 @@ describe("together image.edit", () => { assert.equal(body.model, "black-forest-labs/FLUX.1-kontext-pro"); assert.ok(String(body.image_url).startsWith("data:image/png;base64,")); - assert.equal(body.steps, 28); + assert.ok(!("steps" in body)); }); }); diff --git a/packages/codemode/src/tests/media-file.test.ts b/packages/codemode/src/tests/media-file.test.ts index cb5c96dd..323da74f 100644 --- a/packages/codemode/src/tests/media-file.test.ts +++ b/packages/codemode/src/tests/media-file.test.ts @@ -46,6 +46,22 @@ describe("shared media writer", () => { assert.ok(out.path.endsWith(".png")); }); + it("names an image for what its bytes are, not what it was called", () => { + const jpeg = Buffer.from([ + 0xff, 0xd8, 0xff, 0xe0, 0x00, 0x04, 0x00, 0x00, 0xff, 0xc0, 0x00, 0x11, + 0x08, 0x01, 0xe0, 0x02, 0x80, 0x03, 0x00, 0x00, + ]); + const out = writeImageFile({ + base64: jpeg.toString("base64"), + mimeType: "image/png", + provider: "together", + index: 0, + saveDir: dir, + }); + assert.equal(out.mimeType, "image/jpeg"); + assert.ok(out.path.endsWith(".jpg")); + }); + it("maps MIME types without allowing paths or prototype keys into extensions", () => { for (const [mime, ext] of [ ["image/jpeg", "jpg"], diff --git a/packages/codemode/src/tests/plugin-input-helpers.test.ts b/packages/codemode/src/tests/plugin-input-helpers.test.ts index f7fd2712..6dc3a1b6 100644 --- a/packages/codemode/src/tests/plugin-input-helpers.test.ts +++ b/packages/codemode/src/tests/plugin-input-helpers.test.ts @@ -18,6 +18,7 @@ import { responseFailed } from "../credentials/plugin.js"; import { type CodemodePluginAPI, createPluginAPI } from "../plugin/api.js"; import type { ActionContext } from "../plugin/types.js"; import { readLocalFile } from "../utils/local-file.js"; +import { png } from "./helpers/image.js"; import { getAction, stubFetch } from "./helpers/plugin.js"; const dir = mkdtempSync(join(tmpdir(), "plugin-input-test-")); @@ -133,20 +134,30 @@ describe("readLocalFile", () => { }); describe("readImageInput", () => { - it("loads an image as bytes, base64 and a data URI", async () => { - const file = join(dir, "pixel.png"); - writeFileSync(file, Buffer.from("png")); + it("loads an image with the type its bytes say, whatever its name", async () => { + const file = join(dir, "pixel.jpg"); + writeFileSync(file, png(640, 480)); const image = await readImageInput(file, "fal"); assert.equal(image.mimeType, "image/png"); - assert.equal(image.dataUri, `data:image/png;base64,${image.base64}`); - assert.equal(image.fileName, "pixel.png"); + assert.equal(image.fileName, "pixel.jpg"); + assert.deepEqual(image.bytes, png(640, 480)); + }); + it("refuses a file that is no image, and a missing path", async () => { + const text = join(dir, "notes.png"); + writeFileSync(text, "not an image at all"); + await assert.rejects(() => readImageInput(text, "fal"), { + message: "fal: imagePath is not a PNG, JPEG, WebP or GIF image.", + }); + await assert.rejects(() => readImageInput(" ", "fal"), { + message: "fal: imagePath is required", + }); }); it("bounds the read at 50 MiB", async () => { const big = join(dir, "huge.png"); writeFileSync(big, ""); truncateSync(big, 50 * 1024 * 1024 + 1); await assert.rejects(() => readImageInput(big, "xai"), { - message: "xai: imagePath exceeds 50 MiB", + message: "xai: imagePath is larger than 50 MiB", }); }); }); From be1ef5975beb82fb647c8e1fa7d570919c4ab0e7 Mon Sep 17 00:00:00 2001 From: Michael Livshits Date: Mon, 23 Nov 2026 19:36:11 +0200 Subject: [PATCH 2/4] fix: one bytes rule in the shared media writer, Recraft keeps SVG input, descriptions state only what the docs do (SHFT-2983) --- .../codemode-plugins/_shared/mediaFile.ts | 36 +++++++++++-------- packages/codemode-plugins/openai/src/index.ts | 10 +++--- .../codemode-plugins/recraft/src/index.ts | 8 +++-- .../codemode-plugins/together/src/index.ts | 7 ++-- .../src/tests/image-edit-plugins.test.ts | 31 ++++++++++++++-- .../codemode/src/tests/media-file.test.ts | 31 +++++++++++----- 6 files changed, 83 insertions(+), 40 deletions(-) diff --git a/packages/codemode-plugins/_shared/mediaFile.ts b/packages/codemode-plugins/_shared/mediaFile.ts index cf15377b..8a289779 100644 --- a/packages/codemode-plugins/_shared/mediaFile.ts +++ b/packages/codemode-plugins/_shared/mediaFile.ts @@ -63,7 +63,9 @@ export function mediaDir( /** * Write media bytes to an output file of their own in `saveDir`, which an - * action takes from `mediaDir()`. + * action takes from `mediaDir()`. What an image is, its bytes say: + * `mimeType` names the type of other media, and of an image whose bytes + * tell none. */ export function writeMediaFile(opts: { bytes: Uint8Array; @@ -73,7 +75,10 @@ export function writeMediaFile(opts: { saveDir?: string; stamp?: number; }): SavedMedia { - const mimeType = opts.mimeType ?? "image/png"; + const declared = opts.mimeType ?? "image/png"; + const mimeType = /^image\//i.test(declared) + ? (imageFacts(opts.bytes)?.mimeType ?? declared) + : declared; const stamp = opts.stamp ?? Date.now(); if (!/^[\w-]+$/.test(opts.provider)) throw new Error("invalid media provider name"); @@ -86,10 +91,7 @@ export function writeMediaFile(opts: { return { path, mimeType, byteLength: opts.bytes.byteLength }; } -/** - * Decode an image a provider answers in base64 and write it. What the image - * is, its bytes say: `mimeType` is used only when they tell no type. - */ +/** Decode an image a provider answers in base64 and write it. */ export function writeImageFile(opts: { base64: string; mimeType?: string; @@ -98,12 +100,10 @@ export function writeImageFile(opts: { saveDir?: string; stamp?: number; }): SavedMedia { - const { base64, mimeType, ...rest } = opts; - const bytes = Buffer.from(base64 ?? "", "base64"); + const { base64, ...rest } = opts; return writeMediaFile({ ...rest, - bytes, - mimeType: imageFacts(bytes)?.mimeType ?? mimeType, + bytes: Buffer.from(base64 ?? "", "base64"), }); } @@ -112,17 +112,23 @@ export const SEND_FILE_NOTE = /** Larger than any provider accepts inline; an image past it only fails remotely. */ const MAX_IMAGE_BYTES = 50 * 1024 * 1024; -/** The types of image an `image.edit` call takes. */ -const IMAGE_TYPES = ["image/png", "image/jpeg", "image/webp", "image/gif"]; +/** The types of image an `image.edit` call takes, unless its plugin names its own. */ +export const IMAGE_TYPES: readonly string[] = [ + "image/png", + "image/jpeg", + "image/webp", + "image/gif", +]; /** * Read a local image for an `image.edit` call. What it is, its bytes say, - * never its name. Errors carry the plugin's name so they point at the right - * tool. + * never its name, and one that is none of `types` is refused. Errors carry + * the plugin's name so they point at the right tool. */ export async function readImageInput( imagePath: string, pluginName: string, + types: readonly string[] = IMAGE_TYPES, ): Promise { if (typeof imagePath !== "string" || imagePath.trim().length === 0) { throw new Error(`${pluginName}: imagePath is required`); @@ -130,6 +136,6 @@ export async function readImageInput( return readLocalImage(imagePath, { subject: `${pluginName}: imagePath`, maxBytes: MAX_IMAGE_BYTES, - types: IMAGE_TYPES, + types, }); } diff --git a/packages/codemode-plugins/openai/src/index.ts b/packages/codemode-plugins/openai/src/index.ts index cda7758c..2c89609d 100644 --- a/packages/codemode-plugins/openai/src/index.ts +++ b/packages/codemode-plugins/openai/src/index.ts @@ -50,14 +50,14 @@ const SIZE_RULE = "no edge over 3840, and a total between 655,360 and 8,294,400 pixels — so 512x512 is too small and 1536x864 is fine; " + "above 2560x1440 is experimental. 1024x1024 | 1536x1024 | 1024x1536 work on every model."; -/** Quality tiers, likewise shared. `auto` is what the Flare and Sunburst models pick by default. */ +/** Quality tiers, likewise shared. `auto` is the API's default. */ const QUALITY_RULE = - "low | medium | high | auto, plus xhigh | max on the Flare and Sunburst models"; + "low | medium | high | auto, plus xhigh | max on gpt-image-2.5-*"; /** * Model precedence, shared by create and edit: the call wins, then the - * connection's `defaultModel` (or OPENAI_IMAGE_MODEL), then the newest - * model. + * connection's `defaultModel` (or OPENAI_IMAGE_MODEL), then + * `DEFAULT_MODEL`. */ function resolveModel( explicit: string | undefined, @@ -177,7 +177,6 @@ export default function openai(rl: CodemodePluginAPI) { const images = (data.data ?? []).map((d, i) => ({ ...writeImageFile({ base64: d.b64_json, - mimeType: "image/png", provider: "openai-image", index: i, saveDir: mediaDir(p.saveDir, ctx), @@ -281,7 +280,6 @@ export default function openai(rl: CodemodePluginAPI) { const images = (data.data ?? []).map((d, i) => ({ ...writeImageFile({ base64: d.b64_json, - mimeType: "image/png", provider: "openai-image", index: i, saveDir: mediaDir(p.saveDir, ctx), diff --git a/packages/codemode-plugins/recraft/src/index.ts b/packages/codemode-plugins/recraft/src/index.ts index 82684cec..34ae0c08 100644 --- a/packages/codemode-plugins/recraft/src/index.ts +++ b/packages/codemode-plugins/recraft/src/index.ts @@ -16,6 +16,7 @@ import { credentialJson, multipartBody, type CodemodePluginAPI } from "@shiftlabs/codemode"; import { + IMAGE_TYPES, mediaDir, readImageInput, SAVE_DIR_DESCRIPTION, @@ -24,6 +25,9 @@ import { } from "../../_shared/mediaFile.js"; import { recraftCredential } from "./credentials.js"; +/** Recraft edits a vector image as well as a raster one. */ +const EDIT_TYPES = [...IMAGE_TYPES, "image/svg+xml"]; + interface CreateInput { prompt: string; model?: string; @@ -134,7 +138,6 @@ export default function recraft(rl: CodemodePluginAPI) { const images = (data.data ?? []).map((d, i) => writeImageFile({ base64: d.b64_json, - mimeType: "image/png", provider: "recraft", index: i, saveDir: mediaDir(p.saveDir, ctx), @@ -200,7 +203,7 @@ export default function recraft(rl: CodemodePluginAPI) { if (typeof p.prompt !== "string" || p.prompt.length === 0) { throw new Error("recraft: prompt is required"); } - const img = await readImageInput(p.imagePath, "recraft"); + const img = await readImageInput(p.imagePath, "recraft", EDIT_TYPES); const form = new FormData(); form.append( @@ -228,7 +231,6 @@ export default function recraft(rl: CodemodePluginAPI) { const images = (data.data ?? []).map((d, i) => writeImageFile({ base64: d.b64_json, - mimeType: "image/png", provider: "recraft", index: i, saveDir: mediaDir(p.saveDir, ctx), diff --git a/packages/codemode-plugins/together/src/index.ts b/packages/codemode-plugins/together/src/index.ts index f9d8845b..b0aac6d4 100644 --- a/packages/codemode-plugins/together/src/index.ts +++ b/packages/codemode-plugins/together/src/index.ts @@ -9,8 +9,7 @@ * }) * * The default model is FLUX.2 [dev]. `steps` is sent only when a call sets - * it: FLUX.2 [dev] and [flex] and the Kontext line take it, FLUX.2 [pro] and - * FLUX1.1 [pro] do not, and each model has a default of its own. + * it, because each model has a default of its own. */ import { @@ -32,7 +31,7 @@ import { togetherCredential } from "./credentials.js"; const CREATE_MODEL = "black-forest-labs/FLUX.2-dev"; const EDIT_MODEL = "black-forest-labs/FLUX.1-kontext-pro"; const STEPS_DESCRIPTION = - "Inference steps, for a model that takes them (FLUX.2 dev and flex, FLUX.1 Kontext). The default is the model's"; + "Number of generation steps: more is slower and usually better. Omit for the model's own default."; function generate( ctx: ActionContext, @@ -141,7 +140,6 @@ export default function together(rl: CodemodePluginAPI) { const images = (data.data ?? []).map((d, i) => writeImageFile({ base64: d.b64_json, - mimeType: "image/png", provider: "together", index: i, saveDir: mediaDir(p.saveDir, ctx), @@ -210,7 +208,6 @@ export default function together(rl: CodemodePluginAPI) { const images = (data.data ?? []).map((d, i) => writeImageFile({ base64: d.b64_json, - mimeType: "image/png", provider: "together", index: i, saveDir: mediaDir(p.saveDir, ctx), diff --git a/packages/codemode/src/tests/image-edit-plugins.test.ts b/packages/codemode/src/tests/image-edit-plugins.test.ts index 7bdea33a..617ee7b3 100644 --- a/packages/codemode/src/tests/image-edit-plugins.test.ts +++ b/packages/codemode/src/tests/image-edit-plugins.test.ts @@ -137,7 +137,7 @@ describe("openai image.edit", () => { assert.deepEqual(models, ["gpt-image-2.5-sunburst", "gpt-image-2"]); }); - it("forwards the 2.5-only quality and size values untouched", async () => { + it("forwards the gpt-image-2.5 quality tiers and a custom size untouched", async () => { const action = getAction(makePlugin("openai", openai), "image.edit"); let captured: RequestInit | undefined; stubFetch(async (_input: RequestInfo | URL, init?: RequestInit) => { @@ -245,7 +245,7 @@ describe("openai image.create", () => { } }); - it("forwards the 2.5-only quality and size values untouched", async () => { + it("forwards the gpt-image-2.5 quality tiers and a custom size untouched", async () => { const action = getAction(makePlugin("openai", openai), "image.create"); const bodies = captureBodies(); @@ -440,6 +440,33 @@ describe("recraft image.edit", () => { }); }); +describe("recraft image.edit source types", () => { + it("takes an SVG, which the other plugins refuse", async () => { + const svgPath = join(dir, "logo.svg"); + writeFileSync(svgPath, ''); + let sent: FormData | undefined; + stubFetch(async (_input: RequestInfo | URL, init?: RequestInit) => { + sent = await multipartForm(init); + return Response.json({ data: [{ b64_json: B64_RESULT }] }); + }); + + await getAction(makePlugin("recraft", recraft), "image.edit").execute( + { prompt: "thicker lines", imagePath: svgPath, saveDir: dir }, + ctx({ apiKey: "r-test" }), + ); + assert.equal((sent?.get("image") as Blob).type, "image/svg+xml"); + + await assert.rejects( + () => + getAction(makePlugin("xai", xai), "image.edit").execute( + { prompt: "thicker lines", imagePath: svgPath, saveDir: dir }, + ctx({ apiKey: "x-test" }), + ), + { message: "xai: imagePath is not a PNG, JPEG, WebP or GIF image." }, + ); + }); +}); + describe("together image.create", () => { it("asks its current address for the default model, with steps only when a call sets them", async () => { const action = getAction(makePlugin("together", together), "image.create"); diff --git a/packages/codemode/src/tests/media-file.test.ts b/packages/codemode/src/tests/media-file.test.ts index 323da74f..602c5882 100644 --- a/packages/codemode/src/tests/media-file.test.ts +++ b/packages/codemode/src/tests/media-file.test.ts @@ -46,20 +46,33 @@ describe("shared media writer", () => { assert.ok(out.path.endsWith(".png")); }); - it("names an image for what its bytes are, not what it was called", () => { + it("names an image for what its bytes are, and other media for what it is declared", () => { const jpeg = Buffer.from([ 0xff, 0xd8, 0xff, 0xe0, 0x00, 0x04, 0x00, 0x00, 0xff, 0xc0, 0x00, 0x11, 0x08, 0x01, 0xe0, 0x02, 0x80, 0x03, 0x00, 0x00, ]); - const out = writeImageFile({ - base64: jpeg.toString("base64"), - mimeType: "image/png", - provider: "together", - index: 0, - saveDir: dir, + const options = { provider: "together", index: 0, saveDir: dir }; + for (const out of [ + writeImageFile({ ...options, base64: jpeg.toString("base64") }), + writeMediaFile({ ...options, bytes: jpeg, mimeType: "image/webp" }), + ]) { + assert.equal(out.mimeType, "image/jpeg"); + assert.ok(out.path.endsWith(".jpg")); + } + const svg = writeMediaFile({ + ...options, + bytes: Buffer.from(''), + }); + assert.equal(svg.mimeType, "image/svg+xml"); + assert.ok(svg.path.endsWith(".svg")); + // Raw audio samples can open with the bytes a JPEG does. + const audio = writeMediaFile({ + ...options, + bytes: jpeg, + mimeType: "audio/x-pcm", }); - assert.equal(out.mimeType, "image/jpeg"); - assert.ok(out.path.endsWith(".jpg")); + assert.equal(audio.mimeType, "audio/x-pcm"); + assert.ok(audio.path.endsWith(".pcm")); }); it("maps MIME types without allowing paths or prototype keys into extensions", () => { From 11d3ebaee8a7ccbc5aeee135a5697bdd7b6dc7a5 Mon Sep 17 00:00:00 2001 From: Michael Livshits Date: Mon, 23 Nov 2026 20:32:27 +0200 Subject: [PATCH 3/4] fix: xai, recraft and google image defaults follow the current models, Recraft descriptions follow its docs, image plugins are formatted (SHFT-2983) --- biome.json | 5 -- .../codemode-imagegen/src/providers/google.ts | 4 +- .../codemode-imagegen/tests/imagegen.test.ts | 2 +- .../codemode-plugins/googleImage/src/index.ts | 14 ++--- packages/codemode-plugins/openai/src/index.ts | 9 ++-- .../codemode-plugins/recraft/src/index.ts | 46 ++++++++-------- .../codemode-plugins/together/src/index.ts | 6 +-- packages/codemode-plugins/xai/src/index.ts | 9 ++-- .../tests/credential-fixtures/googleImage.ts | 2 +- .../src/tests/image-edit-plugins.test.ts | 52 +++++++++++++++++++ 10 files changed, 100 insertions(+), 49 deletions(-) diff --git a/biome.json b/biome.json index 13ec75de..253759c4 100644 --- a/biome.json +++ b/biome.json @@ -129,7 +129,6 @@ "!packages/codemode-plugins/oneSimpleApi", "!packages/codemode-plugins/onfleet", "!packages/codemode-plugins/openThesaurus", - "!packages/codemode-plugins/openai", "!packages/codemode-plugins/openweathermap", "!packages/codemode-plugins/oura", "!packages/codemode-plugins/paddle", @@ -150,9 +149,7 @@ "!packages/codemode-plugins/quickbooks", "!packages/codemode-plugins/quickchart", "!packages/codemode-plugins/raindrop", - "!packages/codemode-plugins/recraft", "!packages/codemode-plugins/reddit", - "!packages/codemode-plugins/replicate", "!packages/codemode-plugins/rocketchat", "!packages/codemode-plugins/rundeck", "!packages/codemode-plugins/salesmate", @@ -180,7 +177,6 @@ "!packages/codemode-plugins/telegram", "!packages/codemode-plugins/thehive", "!packages/codemode-plugins/thehiveProject", - "!packages/codemode-plugins/together", "!packages/codemode-plugins/travisci", "!packages/codemode-plugins/trello", "!packages/codemode-plugins/twilio", @@ -197,7 +193,6 @@ "!packages/codemode-plugins/wekan", "!packages/codemode-plugins/woocommerce", "!packages/codemode-plugins/wordpress", - "!packages/codemode-plugins/xai", "!packages/codemode-plugins/xero", "!packages/codemode-plugins/yourls", "!packages/codemode-plugins/zammad", diff --git a/packages/codemode-imagegen/src/providers/google.ts b/packages/codemode-imagegen/src/providers/google.ts index ac752fc3..bea64cc7 100644 --- a/packages/codemode-imagegen/src/providers/google.ts +++ b/packages/codemode-imagegen/src/providers/google.ts @@ -49,8 +49,8 @@ export const google: ImageProvider = { }, }, defaults: { - create: "gemini-2.5-flash-image", - edit: "gemini-2.5-flash-image", + create: "gemini-nano-banana-2.1", + edit: "gemini-nano-banana-2.1", }, models: [ model("gemini-2.5-flash-image", "Nano Banana"), diff --git a/packages/codemode-imagegen/tests/imagegen.test.ts b/packages/codemode-imagegen/tests/imagegen.test.ts index 8e89a36a..8a7bad7f 100644 --- a/packages/codemode-imagegen/tests/imagegen.test.ts +++ b/packages/codemode-imagegen/tests/imagegen.test.ts @@ -294,7 +294,7 @@ describe("imagegen.create through each provider that answers at once", () => { assert.equal(requests.length, 2); assert.equal( requests[0].url, - "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-image:generateContent?key=KEY-google", + "https://generativelanguage.googleapis.com/v1beta/models/gemini-nano-banana-2.1:generateContent?key=KEY-google", ); assert.deepEqual(requests[0].body, { contents: [{ parts: [{ text: "a fox" }] }], diff --git a/packages/codemode-plugins/googleImage/src/index.ts b/packages/codemode-plugins/googleImage/src/index.ts index 25e3d996..73f7aeee 100644 --- a/packages/codemode-plugins/googleImage/src/index.ts +++ b/packages/codemode-plugins/googleImage/src/index.ts @@ -37,6 +37,10 @@ import { import { STRICT } from "../../_shared/schemas.js"; import { googleImageCredential } from "./credentials.js"; +/** Nano Banana 2.1, the model Google recommends for new work. */ +const MODEL = "gemini-nano-banana-2.1"; +const MODEL_DESCRIPTION = `Gemini image model ID, e.g. gemini-3.1-flash-lite-image (fastest and cheapest) or gemini-3-pro-image. Defaults to ${MODEL}.`; + /** The model as one path segment before the fixed :generateContent verb. */ function generateContent( ctx: ActionContext, @@ -113,8 +117,7 @@ export default function googleImage(rl: CodemodePluginAPI) { model: t.Optional( t.String({ minLength: 1, - description: - "Gemini image model ID. Defaults to gemini-2.5-flash-image.", + description: MODEL_DESCRIPTION, }), ), saveDir: t.Optional(t.String({ description: SAVE_DIR_DESCRIPTION })), @@ -122,7 +125,7 @@ export default function googleImage(rl: CodemodePluginAPI) { STRICT, ), async execute(p, ctx) { - const model = p.model ?? "gemini-2.5-flash-image"; + const model = p.model ?? MODEL; const data = await generateContent(ctx, model, { contents: [{ parts: [{ text: p.prompt }] }], generationConfig: { responseModalities: ["IMAGE", "TEXT"] }, @@ -149,8 +152,7 @@ export default function googleImage(rl: CodemodePluginAPI) { model: t.Optional( t.String({ minLength: 1, - description: - "Gemini image model ID. Defaults to gemini-2.5-flash-image.", + description: MODEL_DESCRIPTION, }), ), saveDir: t.Optional(t.String({ description: SAVE_DIR_DESCRIPTION })), @@ -160,7 +162,7 @@ export default function googleImage(rl: CodemodePluginAPI) { async execute(p, ctx) { const img = await readImageInput(p.imagePath, "googleImage"); - const model = p.model ?? "gemini-2.5-flash-image"; + const model = p.model ?? MODEL; const data = await generateContent(ctx, model, { contents: [ { diff --git a/packages/codemode-plugins/openai/src/index.ts b/packages/codemode-plugins/openai/src/index.ts index 2c89609d..257088bb 100644 --- a/packages/codemode-plugins/openai/src/index.ts +++ b/packages/codemode-plugins/openai/src/index.ts @@ -16,7 +16,11 @@ * // images[0].path -> "/tmp/openai-image-….png" */ -import { credentialJson, multipartBody, type CodemodePluginAPI } from "@shiftlabs/codemode"; +import { + type CodemodePluginAPI, + credentialJson, + multipartBody, +} from "@shiftlabs/codemode"; import { mediaDir, readImageInput, @@ -108,8 +112,7 @@ export default function openai(rl: CodemodePluginAPI) { defaultModel: { type: "string", required: false, - description: - `Default image model when a call omits \`model\` — e.g. gpt-image-2 to pin an earlier line. Defaults to ${DEFAULT_MODEL}.`, + description: `Default image model when a call omits \`model\` — e.g. gpt-image-2 to pin an earlier line. Defaults to ${DEFAULT_MODEL}.`, env: "OPENAI_IMAGE_MODEL", }, }); diff --git a/packages/codemode-plugins/recraft/src/index.ts b/packages/codemode-plugins/recraft/src/index.ts index 34ae0c08..647124b8 100644 --- a/packages/codemode-plugins/recraft/src/index.ts +++ b/packages/codemode-plugins/recraft/src/index.ts @@ -9,12 +9,16 @@ * }) * * Recraft is the design-oriented provider: vector output, brand- - * consistent style libraries, typography-aware. V4 models drop the - * legacy `style` knob — pass `styleId` against your own custom - * style if you've set one up. + * consistent style libraries, typography-aware. The default model is + * Recraft V4.1. Only V2 and V3 models take a curated `style`; a V4 or V4.1 + * model takes a `styleId` of your own instead. */ -import { credentialJson, multipartBody, type CodemodePluginAPI } from "@shiftlabs/codemode"; +import { + type CodemodePluginAPI, + credentialJson, + multipartBody, +} from "@shiftlabs/codemode"; import { IMAGE_TYPES, mediaDir, @@ -25,6 +29,9 @@ import { } from "../../_shared/mediaFile.js"; import { recraftCredential } from "./credentials.js"; +/** The model Recraft's API itself defaults to, for creating and for editing. */ +const MODEL = "recraftv4_1"; + /** Recraft edits a vector image as well as a raster one. */ const EDIT_TYPES = [...IMAGE_TYPES, "image/svg+xml"]; @@ -85,14 +92,13 @@ export default function recraft(rl: CodemodePluginAPI) { model: { type: "string", required: false, - description: - "recraftv3 (default) | recraftv3_vector | recraftv4 | recraftv4_pro | recraftv4_vector | recraftv4_pro_vector", + description: `${MODEL} (default) | recraftv4_1_pro | recraftv4_1_vector | recraftv4_1_pro_vector | recraftv4_1_flash_v2 | recraftv4 | recraftv4_pro | recraftv4_vector | recraftv4_pro_vector | recraftv3 | recraftv3_vector. A _vector model writes SVG.`, }, style: { type: "string", required: false, description: - "V2/V3 only — Photorealism | Illustration | Vector art | Hand-drawn | Icon | Recraft V3 Raw", + "V2/V3 models only — Photorealism | Illustration | Vector art | Hand-drawn | Icon | Recraft V3 Raw", }, styleId: { type: "string", @@ -102,7 +108,8 @@ export default function recraft(rl: CodemodePluginAPI) { size: { type: "string", required: false, - description: "WxH (default: 1024x1024). E.g. 1280x1024, 1024x1280", + description: + "Aspect ratio (1:1 | 16:9 | 9:16 | 4:3 | 3:4 | 3:2 | 2:3 | 2:1 | 1:2) or an exact WxH the model lists; 1024x1024 works on every model. Omit for the model's default.", }, n: { type: "number", @@ -116,7 +123,7 @@ export default function recraft(rl: CodemodePluginAPI) { throw new Error("recraft: prompt is required"); } - const model = p.model ?? "recraftv3"; + const model = p.model ?? MODEL; const body: Record = { prompt: p.prompt, @@ -162,7 +169,7 @@ export default function recraft(rl: CodemodePluginAPI) { type: "string", required: true, description: - "Path to the source image file (PNG, JPG, WEBP, or SVG; max 10 MB)", + "Path to the source image file (PNG, JPG, WEBP, or SVG; under 20 MB, up to 16 MP, sides 256–4096 px)", }, strength: { type: "number", @@ -178,19 +185,19 @@ export default function recraft(rl: CodemodePluginAPI) { model: { type: "string", required: false, - description: - "Any Recraft V3 / V4 / V4.1 model, raster or vector (e.g. recraftv3, recraftv4_pro, recraftv4_1_vector). API default: recraftv4_1.", + description: `Any Recraft V3 / V4 / V4.1 model, raster or vector (e.g. recraftv3, recraftv4_pro, recraftv4_1_vector). Default: ${MODEL}.`, }, style: { type: "string", required: false, description: - "Photorealism | Illustration | Vector art | Hand-drawn | Icon | Recraft V3 Raw", + "V3 models only — Photorealism | Illustration | Vector art | Hand-drawn | Recraft V3 Raw", }, styleId: { type: "string", required: false, - description: "ID of a custom style created in your Recraft account", + description: + "V3 models only — ID of a custom style created in your Recraft account", }, n: { type: "number", @@ -215,7 +222,8 @@ export default function recraft(rl: CodemodePluginAPI) { form.append("strength", String(p.strength ?? 0.2)); form.append("response_format", "b64_json"); form.append("n", String(Math.min(p.n ?? 1, 6))); - if (p.model) form.append("model", p.model); + const model = p.model ?? MODEL; + form.append("model", model); if (p.style) form.append("style", p.style); if (p.styleId) form.append("style_id", p.styleId); @@ -237,13 +245,7 @@ export default function recraft(rl: CodemodePluginAPI) { stamp, }), ); - // The imageToImage endpoint's own default model is recraftv4_1. - return { - provider: "recraft", - model: p.model ?? "recraftv4_1", - images, - note: SEND_FILE_NOTE, - }; + return { provider: "recraft", model, images, note: SEND_FILE_NOTE }; }, }); } diff --git a/packages/codemode-plugins/together/src/index.ts b/packages/codemode-plugins/together/src/index.ts index b0aac6d4..79c31ead 100644 --- a/packages/codemode-plugins/together/src/index.ts +++ b/packages/codemode-plugins/together/src/index.ts @@ -99,8 +99,7 @@ export default function together(rl: CodemodePluginAPI) { model: { type: "string", required: false, - description: - `Together model id, e.g. ${CREATE_MODEL} (default), black-forest-labs/FLUX.2-pro, ideogram/ideogram-4.0, Qwen/Qwen-Image`, + description: `Together model id, e.g. ${CREATE_MODEL} (default), black-forest-labs/FLUX.2-pro, ideogram/ideogram-4.0, Qwen/Qwen-Image`, }, size: { type: "string", @@ -173,8 +172,7 @@ export default function together(rl: CodemodePluginAPI) { model: { type: "string", required: false, - description: - `Edit-capable model, e.g. ${EDIT_MODEL} (default), black-forest-labs/FLUX.1-kontext-max`, + description: `Edit-capable model, e.g. ${EDIT_MODEL} (default), black-forest-labs/FLUX.1-kontext-max`, }, steps: { type: "number", diff --git a/packages/codemode-plugins/xai/src/index.ts b/packages/codemode-plugins/xai/src/index.ts index 9d7cd3f9..6bc98699 100644 --- a/packages/codemode-plugins/xai/src/index.ts +++ b/packages/codemode-plugins/xai/src/index.ts @@ -23,8 +23,7 @@ import { } from "../../_shared/mediaFile.js"; import { xaiCredential } from "./credentials.js"; -const MODEL = "grok-imagine-image"; -const EDIT_MODEL = "grok-imagine-image-2.0"; +const MODEL = "grok-imagine-image-2.0"; interface CreateInput { prompt: string; @@ -80,7 +79,7 @@ export default function xai(rl: CodemodePluginAPI) { type: "string", required: false, description: - "1:1 | 16:9 | 9:16 | 4:3 | 3:4 | 3:2 | 2:3 | auto (default: auto)", + "1:1 | 16:9 | 9:16 | 4:3 | 3:4 | 3:2 | 2:3 | 2:1 | 1:2 | 21:9 | 20:9 | 9:20 | 19.5:9 | 9:19.5 | 5:2 | auto (default: auto)", }, n: { type: "number", @@ -147,7 +146,7 @@ export default function xai(rl: CodemodePluginAPI) { model: { type: "string", required: false, - description: "Edit-capable model (default: grok-imagine-image-2.0)", + description: `Edit-capable model (default: ${MODEL})`, }, n: { type: "number", @@ -162,7 +161,7 @@ export default function xai(rl: CodemodePluginAPI) { } const img = await readImageInput(p.imagePath, "xai"); - const model = p.model ?? EDIT_MODEL; + const model = p.model ?? MODEL; const data = (await credentialJson(ctx, xaiCredential, "xai", { target: "api", diff --git a/packages/codemode/src/tests/credential-fixtures/googleImage.ts b/packages/codemode/src/tests/credential-fixtures/googleImage.ts index cfb11d73..575a67b7 100644 --- a/packages/codemode/src/tests/credential-fixtures/googleImage.ts +++ b/packages/codemode/src/tests/credential-fixtures/googleImage.ts @@ -11,6 +11,6 @@ export default { response: { candidates: [] }, target: "api", wire: { - url: "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-image:generateContent?key=g_secret", + url: "https://generativelanguage.googleapis.com/v1beta/models/gemini-nano-banana-2.1:generateContent?key=g_secret", }, } satisfies CredentialFixture; diff --git a/packages/codemode/src/tests/image-edit-plugins.test.ts b/packages/codemode/src/tests/image-edit-plugins.test.ts index 617ee7b3..63cebab5 100644 --- a/packages/codemode/src/tests/image-edit-plugins.test.ts +++ b/packages/codemode/src/tests/image-edit-plugins.test.ts @@ -78,6 +78,58 @@ describe("image.edit is registered on every image plugin", () => { } }); +describe("default models", () => { + // Each plugin answers in its own shape; one reply carries them all. + const reply = { + data: [{ b64_json: B64_RESULT }], + candidates: [ + { content: { parts: [{ inlineData: { data: B64_RESULT } }] } }, + ], + }; + const cases: Array< + [string, (api: CodemodePluginAPI) => void, string, string, string] + > = [ + ["xai", xai, "image.create", "grok-imagine-image-2.0", "body"], + ["xai", xai, "image.edit", "grok-imagine-image-2.0", "body"], + ["recraft", recraft, "image.create", "recraftv4_1", "body"], + ["recraft", recraft, "image.edit", "recraftv4_1", "form"], + [ + "googleImage", + googleImage, + "image.create", + "gemini-nano-banana-2.1", + "url", + ], + ["googleImage", googleImage, "image.edit", "gemini-nano-banana-2.1", "url"], + ]; + for (const [name, fn, actionName, model, where] of cases) { + it(`${name} ${actionName} asks for ${model} and reports it`, async () => { + let sent = ""; + stubFetch(async (input: RequestInfo | URL, init?: RequestInit) => { + if (where === "url") sent = String(input); + else if (where === "form") + sent = String((await multipartForm(init)).get("model")); + else sent = JSON.parse(String(init?.body)).model; + return Response.json(reply); + }); + + const result = (await getAction(makePlugin(name, fn), actionName).execute( + { + prompt: "a fox", + saveDir: dir, + ...(actionName === "image.edit" ? { imagePath: sourcePath } : {}), + }, + ctx({ apiKey: "test" }), + )) as { model: string }; + + assert.equal(result.model, model); + if (where === "url") + assert.ok(sent.includes(`/models/${model}:generateContent`), sent); + else assert.equal(sent, model); + }); + } +}); + describe("openai image.edit", () => { it("POSTs multipart form to /v1/images/edits with image file and prompt", async () => { const action = getAction(makePlugin("openai", openai), "image.edit"); From c010ca5817c577450dbcfdfa04075e1f122e4ff9 Mon Sep 17 00:00:00 2001 From: Michael Livshits Date: Mon, 23 Nov 2026 22:47:09 +0200 Subject: [PATCH 4/4] fix: imagegen drops the gpt-image-1 line, which shuts down on 2026-12-01 (SHFT-2983) --- packages/codemode-imagegen/README.md | 2 +- .../codemode-imagegen/src/providers/openai.ts | 25 ++++++------------- .../codemode-imagegen/tests/imagegen.test.ts | 18 ++++++++++--- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/packages/codemode-imagegen/README.md b/packages/codemode-imagegen/README.md index 71d1eec0..6f7586a9 100644 --- a/packages/codemode-imagegen/README.md +++ b/packages/codemode-imagegen/README.md @@ -86,7 +86,7 @@ The list is the models this package knows of with the provider's own list laid o | Provider | What a size becomes | |---|---| -| `openai` | The same pixels, with sides rounded to 16 and the longer side at most three times the shorter, for `gpt-image-2` and later. The nearest of three sizes for the `gpt-image-1` line | +| `openai` | The same pixels, with sides rounded to 16 and the longer side at most three times the shorter | | `google` | The nearest aspect ratio | | `xai` | The nearest aspect ratio, and a resolution of 1k, 1.5k, or 2k | | `recraft` | The nearest aspect ratio | diff --git a/packages/codemode-imagegen/src/providers/openai.ts b/packages/codemode-imagegen/src/providers/openai.ts index e8c0c285..9ddf2258 100644 --- a/packages/codemode-imagegen/src/providers/openai.ts +++ b/packages/codemode-imagegen/src/providers/openai.ts @@ -1,16 +1,10 @@ import { multipartBody } from "@shiftlabs/codemode"; -import { fitSize, nearestSize, type Size } from "../size.js"; +import { fitSize, type Size } from "../size.js"; import type { ImageModel, ImageProvider } from "../types.js"; import { type ImagesAnswer, imagesOf } from "./answer.js"; -/** The sizes the GPT Image 1 line takes. */ -const FIXED_SIZES: Size[] = [ - { width: 1024, height: 1024 }, - { width: 1536, height: 1024 }, - { width: 1024, height: 1536 }, -]; -/** What gpt-image-2 and later take: any size within these bounds. */ -const FREE_SIZES = { +/** A GPT Image model takes any size within these bounds. */ +const SIZES = { multiple: 16, maxSide: 3840, minPixels: 655_360, @@ -18,16 +12,12 @@ const FREE_SIZES = { maxRatio: 3, }; -const takesFixedSizes = (id: string) => id.startsWith("gpt-image-1"); - function model(id: string): ImageModel { return { id, create: true, edit: true }; } -function sizeFor(id: string, size: Size): string { - const { width, height } = takesFixedSizes(id) - ? nearestSize(size, FIXED_SIZES) - : fitSize(size, FREE_SIZES); +function sizeFor(size: Size): string { + const { width, height } = fitSize(size, SIZES); return `${width}x${height}`; } @@ -47,12 +37,13 @@ export const openai: ImageProvider = { "gpt-image-2", ].map(model), // The list is of every model the key reaches, of any kind, so the image ones are told by their names. + // The gpt-image-1 line takes other sizes and shuts down on 2026-12-01, so it is left out. async listModels(api) { const { data } = await api.request<{ data?: Array<{ id?: unknown }> }>({ path: "models", }); return (data ?? []).flatMap(({ id }) => - typeof id === "string" && /^(gpt-image-|chatgpt-image-)/.test(id) + typeof id === "string" && /^(gpt-image-(?!1)|chatgpt-image-)/.test(id) ? [model(id)] : [], ); @@ -62,7 +53,7 @@ export const openai: ImageProvider = { model: id, prompt, n: numImages, - ...(size ? { size: sizeFor(id, size) } : {}), + ...(size ? { size: sizeFor(size) } : {}), }; let answer: ImagesAnswer; if (images.length) { diff --git a/packages/codemode-imagegen/tests/imagegen.test.ts b/packages/codemode-imagegen/tests/imagegen.test.ts index 8a7bad7f..9097c38c 100644 --- a/packages/codemode-imagegen/tests/imagegen.test.ts +++ b/packages/codemode-imagegen/tests/imagegen.test.ts @@ -927,7 +927,13 @@ describe("the model a request uses", () => { ["generations", () => b64Answer], [ "/models", - () => ({ data: [{ id: "gpt-image-2" }, { id: "gpt-image-1.5" }] }), + () => ({ + data: [ + { id: "gpt-image-2" }, + { id: "chatgpt-image-latest" }, + { id: "gpt-image-1.5" }, + ], + }), ], ]); const config = { ...through("openai"), createModel: "gpt-image-2" }; @@ -937,15 +943,19 @@ describe("the model a request uses", () => { ) => (await call("create", { prompt: "p", ...input }, c)).model; assert.deepEqual( [ - await models({ model: "gpt-image-1.5" }), + await models({ model: "chatgpt-image-latest" }), await models({}), await models({}, through("openai")), ], - ["gpt-image-1.5", "gpt-image-2", "gpt-image-2.6-flare"], + ["chatgpt-image-latest", "gpt-image-2", "gpt-image-2.6-flare"], ); assert.equal( (made(sent)[0].body as { model: string }).model, - "gpt-image-1.5", + "chatgpt-image-latest", + ); + assert.match( + await refused("create", { prompt: "p", model: "gpt-image-1.5" }, config), + /gpt-image-1\.5/, ); });