agy-cli 0.1.45: a tool result's image goes to agy as a prompt image, not an offloaded file (distinceeeee on X)

Report: on agy-cli 0.1.43 with agy 1.5.8, when OpenCode's read tool
returns a screenshot, agy writes the image to
.system_generated/steps/N/media_0.jpg. The model never sees the image and
keeps re-reading files.

Cause: the bridge answered agy's MCP tools/call with the image as MCP
image content. agy offloads MCP images to a file, and its model only gets
"Image read successfully [Resource offloaded to file://…]". There are no
image tokens, so it loops trying to get the image back.

Fix: when agy supports prompt images and a tool result carries an image,
the result takes the steer-in-session path. The MCP call is answered with
its text plus a line saying the image follows. The image goes in the next
session/prompt as an ACP image block, in the same session, so it reaches
the model as an image. withoutAnswered keeps an answered result's images
in the prompt and drops the rest. If steering fails, the existing replay
fallback already sends tool_result images as prompt images. An agy
without image support keeps the old MCP path.

Run:
- Real agy 1.5.8 (macOS), sandbox HOME and sandbox keychain,
  gemini-3.8-flash-high, through a harness that drives the plugin's fetch
  with an OpenCode-style read tool:
  - origin/main: shot.jpg and term.png each looped through 8 reads
    without an answer.
  - This change: 1 read each. shot.jpg was answered correctly
    ("PLUM-7342 ORBIT", red circle, blue square, green triangle).
    term.png was answered correctly twice (all five terminal lines,
    including "TOKEN: ZEBRA-5519", and the yellow rectangle).
- WSL magpie-desktop (Ubuntu 24.04), bun 1.4.2, temp HOME:
  - packages/agy-cli `bun test`: 21 pass, 0 fail.
  - `node test/smoke.mjs`: ALL OK (347 checks).
  - `bun scripts/check.mjs agy-cli`: ok.
- Broken by hand: smoke.mjs with origin/main's index.mjs fails exactly
  the two image checks this change adds or rewrites (2 FAILURES of 347).

Not tried:
- Gemini 4 Carbon: not offered to the test account.
- Real agy on Linux, and a real OpenCode front end.
- stream:true against real agy (the harness is non-streaming; smoke
  covers SSE).
This commit is contained in:
yetone
2026-11-14 18:20:54 +08:00
parent 1dbbc1fb48
commit 6e0cecb2a0
5 changed files with 71 additions and 15 deletions
+3 -1
View File
@@ -75,7 +75,9 @@ MCP server. The control port listens on loopback only, with a token per process.
- **Streaming with thought summaries**: summaries come back as `thinking` blocks.
Set `AGY_BRIDGE_THINKING=0` to turn them off.
- **Image input**: inline base64 PNG, JPEG, GIF and WebP. This needs agy 1.5.0 or
newer, because the ACP server has to advertise image support.
newer, because the ACP server has to advertise image support. An image a tool
returns (a screenshot from `read`) goes to agy as a prompt image too: over MCP,
agy would save it to a file the model can't open.
- **Tool calls**: parallel and consecutive calls both work. A tool that runs past
150 seconds first gets a "still running" placeholder; once the real result
arrives, the same turn continues, and the client executes each call exactly once.
+2 -1
View File
@@ -55,7 +55,8 @@ agy 连接 `--ide-mcp-port` 时会运行 `nc 127.0.0.1 <端口>`。插件在 agy
历史保留。
- **流式输出与思考摘要**:思考摘要以 `thinking` 块返回,设置 `AGY_BRIDGE_THINKING=0` 可关闭。
- **图片输入**:支持内联 base64 的 PNG、JPEG、GIF、WebP。需要 agy 1.5.0 或更新版本,
因为图片能力要由 ACP 服务端声明。
因为图片能力要由 ACP 服务端声明。工具返回的图片(如 `read` 读到的截图)也作为提示里的
图片交给 agy:走 MCP 的话,agy 会把它存成文件,模型看不到。
- **工具调用**:支持并行和连续调用。运行超过 150 秒的工具会先收到 "still running" 的
占位响应,真正的结果送达后继续同一轮对话,每个调用只在客户端执行一次。
- **会话续接**:每轮结束后记录会话。重启 Magpie 或插件后,只要历史和指令的哈希一致,
+33 -5
View File
@@ -1963,8 +1963,10 @@ class Proc {
async steerInSession(turn, results, history) {
if (this.dead || this.stopping || this.retiring || this.turn !== turn || turn.steering || turn.done ||
turn.finalized || !Array.isArray(history)) return false
// An image in a result is replayed in the prompt, as before.
if (results.some((item) => item.result.content.some((part) => part.type !== "text"))) return false
// agy offloads an image in an MCP result to a file the model can't open,
// so the call is answered with its text and the image goes in the
// steer's prompt (withoutAnswered keeps it there).
results = results.map((item) => ({ ...item, result: textOnlyResult(item.result) }))
const deliveringLiveMcp = results.some(({ call }) =>
(!call.deferred && !call.mcpResponded && call.ready && !turn.done) ||
(call.deferred && !call.result && turn.awaitCalls.some((waiter) => waiter.target === call)))
@@ -2436,6 +2438,22 @@ function resultText(result) {
return (result?.content || []).map((part) => part?.type === "text" ? part.text : "[image returned by tool]").join("\n\n")
}
export const IMAGE_FOLLOWS = "[The image this tool returned is attached to the next message.]"
function resultHasImage(result) {
return (result?.content || []).some((part) => part?.type === "image")
}
// A tool result as agy gets it over MCP: agy writes an MCP image to a file
// ("[Resource offloaded to file://…/media_0.jpg]") and the model never sees
// it, so each image is left out with a line saying where it went.
function textOnlyResult(result) {
if (!resultHasImage(result)) return result
const content = result.content.filter((part) => part?.type !== "image")
content.push({ type: "text", text: IMAGE_FOLLOWS })
return { ...result, content }
}
function renderLateResults(calls) {
const parts = ["Client tool calls that were still running have finished. Their real results:"]
for (const call of calls) {
@@ -5610,10 +5628,16 @@ function streamResponse(proc, turn, body, signal) {
// Tool results a kept session already holds (answered to its live MCP calls
// when the user steered) are not sent again in the steer's prompt.
// An answered result's images are the exception: the call got its text only
// (textOnlyResult), so its images go in the prompt, under the result's id.
function withoutAnswered(messages, answered) {
if (!answered?.size) return messages
return messages.map((message) => message.role === "user" && Array.isArray(message.content)
? { ...message, content: message.content.filter((block) => !(block?.type === "tool_result" && answered.has(block.tool_use_id))) }
? { ...message, content: message.content.flatMap((block) => {
if (!(block?.type === "tool_result" && answered.has(block.tool_use_id))) return [block]
const images = Array.isArray(block.content) ? block.content.filter((part) => part?.type === "image") : []
return images.length ? [{ ...block, content: images }] : []
}) }
: message)
}
@@ -5828,6 +5852,10 @@ export const AntigravityBridgePlugin = async () => {
}
}
if (!interrupted) proc.validateResults(results)
// An image over MCP reaches agy's model as a path it can't
// read, so a result with one takes the steer's way: the call
// gets the text, the next prompt the image.
const imageResult = proc.supportsImages && results.some((item) => resultHasImage(item.result))
const directIdsMatch = matches.every((item) => item.match?.call?.id === item.block.tool_use_id)
const historyMatches = directIdsMatch
? proc.matchesPendingTurnHistory(turn, trailingMessages(body).history)
@@ -5835,7 +5863,7 @@ export const AntigravityBridgePlugin = async () => {
// A steer keeps the live session, so agy's prompt cache holds: the
// calls are answered with their real results, the step is
// cancelled, and the steer goes in as the next prompt.
if (interrupted && historyMatches && !proc.toolsGrew(tools) && proc.workDir === workDir &&
if ((interrupted || imageResult) && historyMatches && !proc.toolsGrew(tools) && proc.workDir === workDir &&
sameInstructions(proc.instructions, instructions) &&
await proc.steerInSession(turn, rawResults, trailingMessages(body).history)) {
keptSession = true
@@ -5844,7 +5872,7 @@ export const AntigravityBridgePlugin = async () => {
.filter((item) => !item.call.deferred || item.call.lateDelivered)
.map((item) => item.block.tool_use_id),
)
} else if (interrupted || !historyMatches || proc.toolsGrew(tools) ||
} else if (interrupted || imageResult || !historyMatches || proc.toolsGrew(tools) ||
proc.workDir !== workDir || !sameInstructions(proc.instructions, instructions)) {
// A changed context, a tool catalog that grew, or a
// user-interrupted tool turn needs a new session. Replay the
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-agy-cli-auth",
"version": "0.1.44",
"version": "0.1.45",
"description": "Magpie/OpenCode model provider that connects your local agy CLI through the Agent Client Protocol (agy acp), so Antigravity subscription models run through the real CLI",
"type": "module",
"main": "./opencode.mjs",
+32 -7
View File
@@ -549,7 +549,7 @@ if (process.platform === "win32") {
fs.chmodSync(wrapper, 0o755)
process.env.AGY_PATH = wrapper
const { default: pluginDefault } = await import("../index.mjs")
const { default: pluginDefault, IMAGE_FOLLOWS } = await import("../index.mjs")
const plugin = await pluginDefault.server({ client: null, directory: os.tmpdir() }, {})
const loader = await plugin.auth.loader(async () => ({ key: "agy" }))
let failures = 0
@@ -1956,13 +1956,38 @@ for (const location of ["inline", "system", "developer", "user"]) {
const final = (await readSSE(response)).events
const delivered = logs().filter(event => event.type === "mcpResult" && event.convId === convId).at(-1)
const content = delivered?.result?.content || []
const texts = content.filter(part => part.type === "text").map(part => part.text).join("\n")
check("image dimension companion, teammate-message, and loop notice continue live turn",
const steer = lastOf("user")
// agy offloads an MCP image to a file its model can't open (distinceeeee
// on X): the call gets the text, and the image goes in the next prompt
// of the same session, as an ACP image.
check("a tool result's image goes to agy as a prompt image in the same session, not over MCP",
response.status === 200 && stopOf(final) === "end_turn" && inits().length === before &&
content[0]?.type === "image" &&
texts.includes("Multiply coordinates by 1.71") &&
texts.includes("worker-1") &&
texts.includes("prior /loop wakeups"), JSON.stringify(content))
!content.some(part => part.type === "image") &&
content.some(part => part.text === IMAGE_FOLLOWS) &&
steer.convId === convId &&
steer.content.some(part => part.type === "image" && part.data === "AAAA") &&
steer.text.includes("Multiply coordinates by 1.71") &&
steer.text.includes("worker-1"), JSON.stringify({ content, steer: steer.content }))
// OpenCode's read tool: the image sits inside the tool_result, beside its text.
const readFirst = { model, messages: [{ role: "user", content: "TOOL opencode read image" }], tools: [echoTool] }
const readEvents = (await readSSE(await callFetch(readFirst))).events
const readConv = inits().at(-1).convId
const readBefore = inits().length
const readSecond = continuation(readFirst, readEvents, [
{ type: "text", text: "Image read successfully" },
{ type: "image", source: { type: "base64", media_type: "image/jpeg", data: "cmVhZGltYWdl" } },
])
const readResponse = await callFetch(readSecond)
const readFinal = (await readSSE(readResponse)).events
const readDelivered = logs().filter(event => event.type === "mcpResult" && event.convId === readConv).at(-1)?.result?.content || []
const readSteer = lastOf("user")
check("an image inside a tool_result goes to agy as a prompt image, its text over MCP",
readResponse.status === 200 && stopOf(readFinal) === "end_turn" && inits().length === readBefore &&
JSON.stringify(readDelivered) === JSON.stringify([{ type: "text", text: "Image read successfully" }, { type: "text", text: IMAGE_FOLLOWS }]) &&
readSteer.convId === readConv && readSteer.images === 1 &&
readSteer.content.some(part => part.type === "image" && part.data === "cmVhZGltYWdl" && part.mimeType === "image/jpeg") &&
!readSteer.text.includes("Image read successfully"), JSON.stringify({ readDelivered, steer: readSteer.content }))
const searchTool = { name: "ToolSearch", description: "Search deferred tools", input_schema: { type: "object" } }
const searchFirst = { model, messages: [{ role: "user", content: "TOOLSEARCHCALL load WebFetch" }], tools: [searchTool] }