diff --git a/packages/grok/README.md b/packages/grok/README.md index 3a41ebe..c358f88 100644 --- a/packages/grok/README.md +++ b/packages/grok/README.md @@ -66,6 +66,10 @@ leaves these out: - a `tool_choice` naming a dropped type - a reasoning item's `"content": null` +Plain Codex collaboration items (`agent_message`) reach Grok as user +messages, with their sender, recipient and content preserved. Sealed or +unrecognized content stays unchanged; the plugin does not decrypt tasks. + ## Models Grok 4.7 until signed in. Signed in, the models are the ones the account diff --git a/packages/grok/agent-message.test.mjs b/packages/grok/agent-message.test.mjs new file mode 100644 index 0000000..8c35eec --- /dev/null +++ b/packages/grok/agent-message.test.mjs @@ -0,0 +1,48 @@ +import { expect, test } from "bun:test" +import { _internal } from "./index.mjs" + +test("a plain Codex task reaches Grok as a user message without requiring tools", () => { + const task = { + type: "agent_message", author: "/root", recipient: "/root/research", + content: [{ type: "input_text", text: "Message Type: NEW_TASK\nPayload:\nresearch the failure" }], + } + const sent = JSON.parse(_internal.rewrite(JSON.stringify({ input: [task] }))) + expect(sent.input).toEqual([{ + type: "message", role: "user", + content: [{ type: "input_text", text: "From /root to /root/research\n\n" }, ...task.content], + }]) +}) + +test("a subagent's reply keeps its sender, recipient, position and multimodal parts", () => { + const before = { type: "function_call", call_id: "call_1", name: "send_message", arguments: "{}" } + const after = { type: "function_call_output", call_id: "call_1", output: "sent" } + const reply = { type: "agent_message", author: "/root/research", recipient: "/root", content: [ + { type: "input_text", text: "Message Type: FINAL_ANSWER\nPayload:\nfound the cause" }, + { type: "input_image", image_url: "data:image/png;base64,fixture" }, + ] } + const sent = JSON.parse(_internal.rewrite(JSON.stringify({ tools: [], input: [before, reply, after] }))) + expect(sent.input).toEqual([before, { + type: "message", role: "user", + content: [{ type: "input_text", text: "From /root/research to /root\n\n" }, ...reply.content], + }, after]) +}) + +test("a sealed or malformed agent message is never converted or dropped", () => { + for (const item of [ + { type: "agent_message", content: [{ type: "encrypted_content", encrypted_content: "sealed fixture" }] }, + { type: "agent_message", content: [{ type: "input_text", text: "header" }, { type: "encrypted_content", encrypted_content: "sealed fixture" }] }, + { type: "agent_message", content: [{ type: "input_text", encrypted_content: "sealed fixture" }] }, + { type: "agent_message", encrypted_content: "sealed fixture", content: [{ type: "input_text", text: "header" }] }, + { type: "agent_message", content: [{ type: "future_content", payload: "keep me" }] }, + { type: "agent_message", content: [] }, + { type: "agent_message", content: null }, + ]) { + const body = JSON.stringify({ input: [item] }) + expect(_internal.rewrite(body)).toBe(body) + } +}) + +test("ordinary Responses histories stay byte-for-byte unchanged", () => { + const body = '{ "input": [{"type":"message","role":"user","content":"hello"}] }' + expect(_internal.rewrite(body)).toBe(body) +}) diff --git a/packages/grok/index.mjs b/packages/grok/index.mjs index a87947d..99e794f 100644 --- a/packages/grok/index.mjs +++ b/packages/grok/index.mjs @@ -288,7 +288,7 @@ const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v) // which Grok turns away otherwise: "tool parameter root must be an object // type" (magpie#1271). function rewrite(body) { - if (typeof body !== "string" || (!body.includes('"tools"') && !body.includes('"reasoning"'))) return body + if (typeof body !== "string" || (!body.includes('"tools"') && !body.includes('"reasoning"') && !body.includes('"agent_message"'))) return body let m try { m = JSON.parse(body) @@ -323,6 +323,23 @@ function rewrite(body) { dirty = true } } + if (Array.isArray(m.input)) { + m.input = m.input.map((it) => { + if (!isObj(it) || it.type !== "agent_message" || Object.hasOwn(it, "encrypted_content") || + !Array.isArray(it.content) || !it.content.length || + !it.content.every((part) => isObj(part) && !Object.hasOwn(part, "encrypted_content") && + (part.type === "input_text" && typeof part.text === "string" || + part.type === "input_image" && typeof part.image_url === "string"))) return it + // Grok takes ordinary messages, not Codex's collaboration item. + // Sealed tasks remain untouched for the gateway's existing guard. + const from = typeof it.author === "string" ? it.author : "" + const to = typeof it.recipient === "string" ? it.recipient : "" + const header = from && to ? `From ${from} to ${to}` : from ? `From ${from}` : to ? `To ${to}` : "" + dirty = true + return { type: "message", role: "user", content: header + ? [{ type: "input_text", text: `${header}\n\n` }, ...it.content] : it.content } + }) + } for (const it of Array.isArray(m.input) ? m.input : []) { if (it && it.type === "reasoning" && "content" in it && it.content === null) { delete it.content