Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(image): retry a Custom image request once with size auto when the exact size fails
AdityaUmalebcf1010480fa
diff --git a/server/image-generation.test.ts b/server/image-generation.test.tsindex 29535da05c..1390857f5c 100644--- a/server/image-generation.test.ts+++ b/server/image-generation.test.ts@@ -217,3 +217,47 @@ describe("what the person learns when a provider fails", () => {expect(imageSlots({ openrouter: { key: "or-key" }, imageGen: { primary: { provider: "openrouter", model: "or-key" } } }).primary.model).toBe(IMAGE_DEFAULT_MODELS.openrouter);});});++describe("a Custom gateway that accepts only size auto", () => {+ const CUSTOM = { imageGen: { customUrl: "http://127.0.0.1:4711/v1", customApiKey: "custom-secret", customModel: "vendor/image-model", primary: { provider: "custom" as const } } };+ const sizes = (fetchMock: ReturnType<typeof vi.fn<typeof fetch>>) => fetchMock.mock.calls.map((call) => JSON.parse(String(call[1]?.body)).size);++ it("retries once with size auto when the exact size fails, and makes the image", async () => {+ const fetchMock = vi.fn<typeof fetch>(async (_input, init) => JSON.parse(String(init?.body)).size === "auto" ? imageResponse() : new Response("{}", { status: 502 }));+ await expect(generateImage(CUSTOM, { prompt: "owl", aspect: "16:9" }, { fetchImpl: fetchMock })).resolves.toMatchObject({ provider: "custom", bytes: PNG });+ expect(sizes(fetchMock)).toEqual(["1536x1024", "auto"]);+ });++ it("tries auto only once and reports the exact size's failure", async () => {+ const fetchMock = vi.fn<typeof fetch>(async () => new Response("{}", { status: 400 }));+ await expect(generateImage(CUSTOM, { prompt: "owl" }, { fetchImpl: fetchMock })).rejects.toThrow("Custom image generation failed (HTTP 400, model vendor/image-model)");+ expect(sizes(fetchMock)).toEqual(["1024x1024", "auto"]);+ });++ it("keeps the exact size's error when the auto retry cannot connect", async () => {+ const fetchMock = vi.fn<typeof fetch>(async (_input, init) => {+ if (JSON.parse(String(init?.body)).size === "auto") throw Object.assign(new TypeError("fetch failed"), { cause: { code: "ECONNREFUSED" } });+ return new Response("{}", { status: 502 });+ });+ await expect(generateImage(CUSTOM, { prompt: "owl" }, { fetchImpl: fetchMock })).rejects.toThrow("Custom image generation failed (HTTP 502, model vendor/image-model)");+ expect(fetchMock).toHaveBeenCalledTimes(2);+ });++ it.each([401, 402, 403, 429])("does not retry when the gateway refuses the key or quota (HTTP %i)", async (status) => {+ const fetchMock = vi.fn<typeof fetch>(async () => new Response("{}", { status }));+ await expect(generateImage(CUSTOM, { prompt: "owl" }, { fetchImpl: fetchMock })).rejects.toThrow(`HTTP ${status}`);+ expect(fetchMock).toHaveBeenCalledOnce();+ });++ it("keeps the exact size when it works", async () => {+ const fetchMock = vi.fn<typeof fetch>(async () => imageResponse());+ await generateImage(CUSTOM, { prompt: "owl", aspect: "9:16" }, { fetchImpl: fetchMock });+ expect(sizes(fetchMock)).toEqual(["1024x1536"]);+ });++ it("never retries other providers with size auto", async () => {+ const fetchMock = vi.fn<typeof fetch>(async () => new Response("{}", { status: 502 }));+ await expect(generateImage({ imageGen: { key: "openai-secret" } }, { prompt: "owl" }, { fetchImpl: fetchMock })).rejects.toThrow("HTTP 502");+ expect(fetchMock).toHaveBeenCalledOnce();+ });+});diff --git a/server/image-generation.ts b/server/image-generation.tsindex b71073f331..ef36fd362a 100644--- a/server/image-generation.ts+++ b/server/image-generation.ts@@ -212,20 +212,34 @@ export async function generateImageWith(const timeoutSignal = AbortSignal.timeout(timeoutMs);const cancelled = () => cancel?.aborted === true && !timeoutSignal.aborted;- let response: Response;- try {- response = await fetchImpl(provider.url(cfg), {- method: "POST",- headers,- body: JSON.stringify(body),- redirect: "error",- signal: cancel ? AbortSignal.any([timeoutSignal, cancel]) : timeoutSignal,+ const send = async (payload: Record<string, unknown>): Promise<Response> => {+ try {+ return await fetchImpl(provider.url(cfg), {+ method: "POST",+ headers,+ body: JSON.stringify(payload),+ redirect: "error",+ signal: cancel ? AbortSignal.any([timeoutSignal, cancel]) : timeoutSignal,+ });+ } catch (error) {+ if (cancelled()) fail("Image generation was cancelled", 409);+ const timedOut = timeoutSignal.aborted || (error instanceof Error && error.name === "TimeoutError");+ if (timedOut) fail("Image generation timed out", 502);+ fail(`Could not reach ${provider.name} image generation`, 502, unreached(error));+ }+ };+ let response = await send(body);++ // Some Custom gateways accept only size "auto" and reject every exact size, so a failed exact size gets one retry with "auto".+ if (!response.ok && slot.provider === "custom" && !NOT_STARTED_STATUSES.has(response.status)) {+ await response.body?.cancel().catch(() => undefined);+ // A retry that cannot connect keeps the exact size's error; only cancel and timeout end the call there.+ const retry = await send({ ...body, size: "auto" }).catch((error: unknown) => {+ if (cancel?.aborted || timeoutSignal.aborted) throw error;+ return undefined;});- } catch (error) {- if (cancelled()) fail("Image generation was cancelled", 409);- const timedOut = timeoutSignal.aborted || (error instanceof Error && error.name === "TimeoutError");- if (timedOut) fail("Image generation timed out", 502);- fail(`Could not reach ${provider.name} image generation`, 502, unreached(error));+ if (retry?.ok) response = retry;+ else await retry?.body?.cancel().catch(() => undefined);}if (!response.ok) {