diff --git a/packages/agy-cli/README.en.md b/packages/agy-cli/README.en.md index c5cf438..033a90b 100644 --- a/packages/agy-cli/README.en.md +++ b/packages/agy-cli/README.en.md @@ -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. diff --git a/packages/agy-cli/README.md b/packages/agy-cli/README.md index 6651920..0921b58 100644 --- a/packages/agy-cli/README.md +++ b/packages/agy-cli/README.md @@ -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 或插件后,只要历史和指令的哈希一致, diff --git a/packages/agy-cli/index.mjs b/packages/agy-cli/index.mjs index 6c530fc..3d18490 100644 --- a/packages/agy-cli/index.mjs +++ b/packages/agy-cli/index.mjs @@ -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 diff --git a/packages/agy-cli/package.json b/packages/agy-cli/package.json index ea3dd23..ad9c2c9 100644 --- a/packages/agy-cli/package.json +++ b/packages/agy-cli/package.json @@ -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", diff --git a/packages/agy-cli/test/smoke.mjs b/packages/agy-cli/test/smoke.mjs index 0e66f04..641548a 100644 --- a/packages/agy-cli/test/smoke.mjs +++ b/packages/agy-cli/test/smoke.mjs @@ -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] }