fix(qoder): count an image nested in a Claude tool_result as image input

hasQoderImageParts only read top-level content parts, so a Claude request whose
only image sat inside a tool_result (and that declared no tools) did not require
the HTTP transport, and selection could pick a PAT connection.
This commit is contained in:
serenity
2026-11-18 23:54:49 -03:00
committed by diegosouzapw
parent 31d443937b
commit 1e7c336c7b
2 changed files with 40 additions and 7 deletions

View File

@@ -20,6 +20,17 @@ export function qoderSupportsCallerTools(credentials: QoderCredentials): boolean
/** Image parts as the Chat Completions, Responses and Claude-style content arrays name them. */
const QODER_IMAGE_PART_TYPES = new Set(["image_url", "input_image", "image"]);
/** A Claude `tool_result` can carry image blocks in its nested `content` array. */
function isQoderImagePart(part: unknown): boolean {
const record = part as Record<string, unknown> | null;
if (QODER_IMAGE_PART_TYPES.has(String(record?.type))) return true;
return (
record?.type === "tool_result" &&
Array.isArray(record.content) &&
record.content.some(isQoderImagePart)
);
}
/** The qodercli prompt is text only, so an image part cannot reach the model on a PAT connection. */
export function hasQoderImageParts(body: unknown): boolean {
if (!body || typeof body !== "object") return false;
@@ -29,12 +40,7 @@ export function hasQoderImageParts(body: unknown): boolean {
Array.isArray(turns) &&
turns.some((turn) => {
const content = (turn as Record<string, unknown> | null)?.content;
return (
Array.isArray(content) &&
content.some((part) =>
QODER_IMAGE_PART_TYPES.has(String((part as Record<string, unknown> | null)?.type))
)
);
return Array.isArray(content) && content.some(isQoderImagePart);
})
);
}

View File

@@ -15,7 +15,8 @@ const core = await import("../../src/lib/db/core.ts");
const { createProviderConnection, deleteProviderConnectionsByProvider } =
await import("../../src/lib/db/providers.ts");
const { QoderExecutor } = await import("../../open-sse/executors/qoder.ts");
const { hasQoderImageParts } = await import("../../open-sse/services/qoderCapabilities.ts");
const { hasQoderImageParts, requiresQoderHttpTransport } =
await import("../../open-sse/services/qoderCapabilities.ts");
// Built from parts: a literal token-shaped string would trip secret scanners.
const PAT = ["pt", "image", "fixture"].join("-");
@@ -63,6 +64,32 @@ test("image parts are found in messages and in Responses-style input, plain text
assert.equal(hasQoderImageParts({ messages: [null, "x", { content: null }] }), false);
});
test("an image nested in a Claude tool_result needs the HTTP transport even without tools", () => {
const body = {
messages: [
{
role: "user",
content: [
{
type: "tool_result",
tool_use_id: "toolu_1",
content: [
{ type: "text", text: "screenshot" },
{ type: "image", source: { type: "base64", media_type: "image/png", data: "AAAA" } },
],
},
],
},
],
};
assert.equal(hasQoderImageParts(body), true);
// chat.ts passes this predicate as requireToolCalling, which drops PAT connections.
assert.equal(requiresQoderHttpTransport(body), true);
const textOnly = structuredClone(body);
textOnly.messages[0].content[0].content = [{ type: "text", text: "screenshot" }];
assert.equal(requiresQoderHttpTransport(textOnly), false);
});
test("PAT executor rejects image input with a 400 before it launches the local CLI", async () => {
await withStubCli(async (marker) => {
const { response } = await new QoderExecutor().execute({