kiro 0.1.9: a tool's images reach the model, as the built-in sends them (yetone/magpie#1861)

What: buildKiro read a tool_result's content for its text alone, so an
image a tool returned (Codex's view_image, a computer-use screenshot,
Claude Code's Read of a PNG) never reached Kiro: the model got
"(no output)" and said it saw nothing. Its image blocks (base64) now go
with the message that answers the call, beside any image the user sent,
as magpie's built-in has sent them since yetone/magpie#369. Kiro's tool
results hold text alone. The "only the latest images" rule is unchanged;
an image named only by a URL still has no bytes to send and is left out.

Why: simonsmh reported Codex's view_image unreadable through Kiro on
magpie v0.1.1215 (#1861). A moved Kiro is served by this plugin, which
never got #369's fix.

Run: HOME=<scratch> TMPDIR=<scratch> bun test (1152 pass, 0 fail) and
bun scripts/check.mjs. tool-images.test.mjs's fixture is the Anthropic body
magpie's gateway hands the plugin for Codex 0.162's view_image output
([{"type":"input_image","image_url":"data:...","detail":"high"}]), dumped
from magpie's buildAnthropic; 3 of its 4 tests fail on 0.1.8. Not tried
against a real Kiro account (none signed in here); #369 checked the same
body shape on the built-in against a real account.
This commit is contained in:
yetone
2026-10-13 03:40:07 +08:00
parent caf3908eff
commit 66facacf14
4 changed files with 80 additions and 4 deletions
+2
View File
@@ -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'
+11 -3
View File
@@ -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")
+1 -1
View File
@@ -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",
+66
View File
@@ -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()
})