@devries-cameron20 / t3code Mirror · read-only / commits / 7fcec368cb
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

fix(server): Claude text generation retries without images when the model rejects them (#39684)

Julius Marminge committed Nov 20, 2026 at 04:01 UTC 7fcec368cb13ce14cad8d7d4f4aca9b3f9a041e0
3 files changed +83 -15
apps/server/src/textGeneration/ClaudeTextGeneration.test.ts
+43
@@ -65,6 +65,7 @@ function withFakeClaudeEnv<A, E, R>(
65 expectedTools?: string;
66 argsMustNotContain?: string;
67 stdinMustContain?: string;
68 + rejectsImages?: boolean;
69 configDirMustBe?: string;
70 cwdMustNotBe?: string;
71 claudeConfig?: Partial<ClaudeSettings>;
@@ -88,6 +89,7 @@ function withFakeClaudeEnv<A, E, R>(
89 T3_FAKE_CLAUDE_ARGS_MUST_CONTAIN: input.argsMustContain,
90 T3_FAKE_CLAUDE_ARGS_MUST_NOT_CONTAIN: input.argsMustNotContain,
91 T3_FAKE_CLAUDE_STDIN_MUST_CONTAIN: input.stdinMustContain,
92 + T3_FAKE_CLAUDE_REJECTS_IMAGES: input.rejectsImages ? "1" : undefined,
93 T3_FAKE_CLAUDE_CWD_MUST_NOT_BE: input.cwdMustNotBe,
94 T3_FAKE_CLAUDE_CONFIG_DIR_MUST_BE: input.configDirMustBe,
95 };
@@ -268,6 +270,47 @@ it.layer(layerClaudeTextGenerationTest)("ClaudeTextGeneration", (it) => {
270 ),
271 );
272
273 + it.effect("still generates text when a text-only model rejects image attachments", () =>
274 + withFakeClaudeEnv(
275 + {
276 + output: JSON.stringify({ structured_output: { branch: "fix/login-button" } }),
277 + rejectsImages: true,
278 + },
279 + (textGeneration) =>
280 + Effect.gen(function* () {
281 + const fs = yield* FileSystem.FileSystem;
282 + const path = yield* Path.Path;
283 + const { attachmentsDir } = yield* ServerConfig.ServerConfig;
284 + const attachmentId = "thread-1-claude-text-only-attachment";
285 + const imagePath = path.join(attachmentsDir, `${attachmentId}.png`);
286 + yield* fs.makeDirectory(attachmentsDir, { recursive: true });
287 + yield* fs.writeFile(imagePath, Buffer.from("hello"));
288 +
289 + const result = yield* textGeneration
290 + .generateBranchName({
291 + cwd: process.cwd(),
292 + message: "fix this",
293 + attachments: [
294 + {
295 + type: "image",
296 + id: attachmentId,
297 + name: "screen.png",
298 + mimeType: "image/png",
299 + sizeBytes: 5,
300 + },
301 + ],
302 + modelSelection: {
303 + instanceId: ProviderInstanceId.make("claudeAgent"),
304 + model: SYNTHETIC_CLAUDE_STANDARD_MODEL,
305 + },
306 + })
307 + .pipe(Effect.ensuring(fs.remove(imagePath).pipe(Effect.ignore)));
308 +
309 + expect(result.branch).toBe("fix/login-button");
310 + }),
311 + ),
312 + );
313 +
314 it.effect("runs Claude text generation with the configured CLAUDE_CONFIG_DIR", () =>
315 Effect.gen(function* () {
316 const path = yield* Path.Path;
apps/server/src/textGeneration/ClaudeTextGeneration.ts
+27 -15
@@ -262,21 +262,24 @@ export const makeClaudeTextGeneration = Effect.fn("makeClaudeTextGeneration")(fu
262
263 // `claude -p` accepts images only as content blocks of a stream-json user message.
264 const imageBlocks = yield* readImageBlocks(attachments);
265 - const streamJson = imageBlocks.length > 0;
266 - const stdin = streamJson
267 - ? `${yield* encodeJsonForOperation(
268 - operation,
269 - {
270 - type: "user",
271 - message: { role: "user", content: [{ type: "text", text: prompt }, ...imageBlocks] },
272 - },
273 - "Failed to encode Claude CLI input.",
274 - )}\n`
275 - : prompt;
265 + const imageStdin =
266 + imageBlocks.length > 0
267 + ? `${yield* encodeJsonForOperation(
268 + operation,
269 + {
270 + type: "user",
271 + message: { role: "user", content: [{ type: "text", text: prompt }, ...imageBlocks] },
272 + },
273 + "Failed to encode Claude CLI input.",
274 + )}\n`
275 + : undefined;
276
277 const runClaudeCommand = Effect.fn("runClaudeJson.runClaudeCommand")(function* (
278 noSessionPersistence: boolean,
279 + withImages: boolean,
280 ) {
281 + const streamJson = withImages && imageStdin !== undefined;
282 + const stdin = streamJson ? imageStdin : prompt;
283 const managedMcpConfig = yield* hasClaudeManagedMcpConfig();
284 const spawnCommand = yield* resolveSpawnCommand(
285 claudeSettings.binaryPath || "claude",
@@ -351,11 +354,20 @@ export const makeClaudeTextGeneration = Effect.fn("makeClaudeTextGeneration")(fu
354 };
355 });
356
354 - const rawStdout = yield* runClaudeCommand(true).pipe(
357 + const runClaudeAttempt = (withImages: boolean) =>
358 + runClaudeCommand(true, withImages).pipe(
359 + Effect.filterOrElse(
360 + (result) =>
361 + result.exitCode === 0 || !result.stderr.includes(NO_SESSION_PERSISTENCE_REJECTED),
362 + () => runClaudeCommand(false, withImages),
363 + ),
364 + );
365 +
366 + const rawStdout = yield* runClaudeAttempt(true).pipe(
367 + // Text-only models (custom models behind Anthropic-compatible endpoints) reject image blocks.
368 Effect.filterOrElse(
356 - (result) =>
357 - result.exitCode === 0 || !result.stderr.includes(NO_SESSION_PERSISTENCE_REJECTED),
358 - () => runClaudeCommand(false),
369 + (result) => result.exitCode === 0 || imageStdin === undefined,
370 + () => runClaudeAttempt(false),
371 ),
372 Effect.flatMap(({ stdout, stderr, exitCode }) => {
373 if (exitCode === 0) return Effect.succeed(stdout);
apps/server/src/textGeneration/testing/ClaudeTextGeneration.fixture.mjs
+13
@@ -68,6 +68,19 @@ if (stdinMustContain && !stdinContent.includes(stdinMustContain)) {
68 fail("stdin missing expected content", 4);
69 }
70
71 +if (process.env.T3_FAKE_CLAUDE_REJECTS_IMAGES === "1" && stdinContent.includes('"type":"image"')) {
72 + process.stdout.write(
73 + JSON.stringify({
74 + type: "result",
75 + subtype: "success",
76 + is_error: true,
77 + api_error_status: 400,
78 + result: "API Error: 400 Image inputs are not supported by this model",
79 + }) + "\n",
80 + );
81 + process.exit(1);
82 +}
83 +
84 const configDirMustBe = process.env.T3_FAKE_CLAUDE_CONFIG_DIR_MUST_BE;
85 if (configDirMustBe && process.env.CLAUDE_CONFIG_DIR !== configDirMustBe) {
86 fail("CLAUDE_CONFIG_DIR was " + (process.env.CLAUDE_CONFIG_DIR ?? ""), 5);