diff --git a/packages/kiro/README.md b/packages/kiro/README.md index c8130b6..6b3f1a0 100644 --- a/packages/kiro/README.md +++ b/packages/kiro/README.md @@ -76,6 +76,8 @@ request: - Tool ids Kiro wouldn't take are rewritten. A tool name over 64 characters (an MCP plugin's, in Claude Code) is sent shortened with a hash and its calls come back under the full name. + - A tool's images (a screenshot, Codex's view_image) go with the + message that answers the call, as Kiro's tool results hold text alone. - Only the latest images are sent. - Tools the history used but the request doesn't offer are declared. - It turns the AWS event stream Kiro answers with back into Messages' diff --git a/packages/kiro/index.mjs b/packages/kiro/index.mjs index bfc964f..ff74659 100644 --- a/packages/kiro/index.mjs +++ b/packages/kiro/index.mjs @@ -614,6 +614,12 @@ function imageFormat(mediaType) { return f } +// addImage puts an Anthropic image block in images as Kiro takes it: the +// bytes inline. One named only by a URL has no bytes to send and is left out. +function addImage(images, p) { + if (p.source?.type === "base64" && p.source.data) images.push({ format: imageFormat(p.source.media_type), source: { bytes: p.source.data } }) +} + const textOf = (c) => typeof c === "string" ? c : Array.isArray(c) ? c.filter((p) => p?.type === "text").map((p) => p.text ?? "").join("\n\n") : "" const joinNonEmpty = (a, b) => (a && b ? a + "\n\n" + b : a + b) @@ -669,12 +675,14 @@ function buildKiro(req, model, profile, budget) { if (p?.type === "text") { if (p.text) texts.push(p.text) } else if (p?.type === "document") texts.push(documentText(p)) - else if (p?.type === "image") { - if (p.source?.type === "base64" && p.source.data) u.images.push({ format: imageFormat(p.source.media_type), source: { bytes: p.source.data } }) - } else if (p?.type === "tool_result") { + else if (p?.type === "image") addImage(u.images, p) + else if (p?.type === "tool_result") { let out = textOf(p.content) if (out.length > RESULT_LIMIT) out = out.slice(0, RESULT_LIMIT) + "\n… (cut)" results.push({ toolUseId: toolID(p.tool_use_id), status: p.is_error ? "error" : "success", content: [{ text: out || "(no output)" }] }) + // a tool's images (a screenshot, Codex's view_image) go with the + // message, as Kiro's tool results carry text alone (magpie #369, #1861) + if (Array.isArray(p.content)) for (const c of p.content) if (c?.type === "image") addImage(u.images, c) } } u.content = texts.join("\n\n") diff --git a/packages/kiro/package.json b/packages/kiro/package.json index bde29f1..efd3878 100644 --- a/packages/kiro/package.json +++ b/packages/kiro/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-kiro-auth", - "version": "0.1.8", + "version": "0.1.9", "description": "Kiro subscriptions (Free, Pro, Pro+, Power) in OpenCode and magpie", "type": "module", "main": "./index.mjs", diff --git a/packages/kiro/tool-images.test.mjs b/packages/kiro/tool-images.test.mjs new file mode 100644 index 0000000..91b5310 --- /dev/null +++ b/packages/kiro/tool-images.test.mjs @@ -0,0 +1,66 @@ +// A tool's images reach the model (yetone/magpie#1861). Codex's view_image +// answers with a function_call_output whose output is +// [{"type":"input_image","image_url":"data:image/png;base64,…","detail":"high"}] +// (codex-rs 0.162 protocol/src/models.rs, FunctionCallOutputContentItem), +// which magpie hands the plugin as an Anthropic tool_result holding an image +// block. Kiro's tool results carry text alone, so the image goes with the +// message that answers the call, as magpie's built-in sends it (#369). +import { expect, test } from "bun:test" +import { _internal } from "./index.mjs" + +const { buildKiro } = _internal + +// a 1x1 PNG +const PNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==" +const JPG = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/yQALCAABAAEBAREA/8wABgAQEAX/2gAIAQEAAD8A0s8g/9k=" + +// the body magpie's gateway writes for a Codex turn whose view_image call +// came back with an image and nothing else (its tool_result has no text block) +const viewImage = (content) => ({ + model: "claude-sonnet-4.5", + system: [{ type: "text", text: "You are Codex." }], + messages: [ + { role: "user", content: [{ type: "text", text: "使用computer use截图当前屏幕看下" }] }, + { role: "assistant", content: [{ type: "tool_use", id: "call_view1", name: "view_image", input: { path: "/tmp/magpie_icon.png" } }] }, + { role: "user", content: [{ type: "tool_result", tool_use_id: "call_view1", content, cache_control: { type: "ephemeral" } }] }, + ], + tools: [{ name: "view_image", description: "View a local image", input_schema: { type: "object", properties: { path: { type: "string" } } } }], +}) + +const sent = (r) => JSON.parse(buildKiro(r, "claude-sonnet-4.5", "", 0)).conversationState + +test("a tool's image goes with the message that answers its call", () => { + const s = sent(viewImage([{ type: "image", source: { type: "base64", media_type: "image/png", data: PNG } }])) + const u = s.currentMessage.userInputMessage + expect(u.images).toEqual([{ format: "png", source: { bytes: PNG } }]) + expect(u.userInputMessageContext.toolResults).toEqual([{ toolUseId: "call_view1", status: "success", content: [{ text: "(no output)" }] }]) +}) + +test("a tool's text and images both go, every image in order", () => { + const s = sent( + viewImage([ + { type: "text", text: "Screenshot of Finder" }, + { type: "image", source: { type: "base64", media_type: "image/png", data: PNG } }, + { type: "image", source: { type: "base64", media_type: "image/jpeg", data: JPG } }, + ]), + ) + const u = s.currentMessage.userInputMessage + expect(u.userInputMessageContext.toolResults[0].content).toEqual([{ text: "Screenshot of Finder" }]) + expect(u.images.map((i) => i.format)).toEqual(["png", "jpeg"]) +}) + +test("an image named only by a URL has no bytes to send and is left out", () => { + const s = sent(viewImage([{ type: "image", source: { type: "url", url: "https://example.invalid/a.png" } }])) + expect(s.currentMessage.userInputMessage.images).toBeUndefined() +}) + +test("only the latest tool image is sent again, as Kiro's own agent sends only the latest", () => { + const r = viewImage([{ type: "image", source: { type: "base64", media_type: "image/png", data: PNG } }]) + r.messages.push( + { role: "assistant", content: [{ type: "tool_use", id: "call_view2", name: "view_image", input: { path: "/tmp/b.jpg" } }] }, + { role: "user", content: [{ type: "tool_result", tool_use_id: "call_view2", content: [{ type: "image", source: { type: "base64", media_type: "image/jpeg", data: JPG } }] }] }, + ) + const s = sent(r) + expect(s.currentMessage.userInputMessage.images).toEqual([{ format: "jpeg", source: { bytes: JPG } }]) + for (const e of s.history) expect(e.userInputMessage?.images).toBeUndefined() +})