Adds Agent Console (@magpie-community/page-agent-console 1.0.0), a community page plugin that drives the Agent CLIs on this computer (Claude Code, Codex, ACP agents) through a local daemon on 127.0.0.1, with its source in packages/agent-console and a registry entry. Claude Code is the user's own `claude` CLI driven over stream-json (daemon/src/providers/claude/cli.ts); no Anthropic SDK is installed, bundled or in the branch's history. Cold install ~45 MB (node-pty ~26 MB), stated in the README and registry summary in en and zh. By qingnian108 (#535). What was run at 8e8f910 (mote, macOS, throwaway HOME, no real accounts): - packages/agent-console: `npm ci --ignore-scripts --workspaces=false`; `node scripts/verify-build.mjs`: all five bundles byte-identical (daemon/dist/main.js 08c95aa3…, app/app.js 9a6911f3…); `vitest run`: 45 files, 254 passed, 17 skipped; `npm run check` clean. - Real Claude Code 2.1.316 (npm @anthropic-ai/claude-code, sandbox HOME/CLAUDE_CONFIG_DIR, local fake Messages API): `AC_REAL_CLAUDE=… vitest run --config vitest.real.config.ts test/real/claude-cli.real.test.ts`: 4 of 4 (initialize lists 51 slash commands, streaming turn, Bash permission approved and denied, interrupt, --resume with set_model). - Probe: the driver's exact argv with `--permission-mode bypassPermissions --allow-dangerously-skip-permissions`: the tool runs with no can_use_tool request. - Repo root: `HOME=$(mktemp -d) bun test`: 4006 tests, 0 fail; `bun scripts/check.mjs` exit 0. - Install as magpie does: `npm pack --ignore-scripts` (552 KB) then `bun add --ignore-scripts`: 45 MB node_modules, no @anthropic-ai/*, index.mjs imports. - Read every changed line since 41c7a94: argv values limited to SAFE_ARG tokens (no cmd.exe metacharacters reach the shim path), no new network hosts, no install scripts, no eval; no commit in main..8e8f910 contains claude-agent-sdk. Not tried: magpie's desktop window loading the page; a Windows .cmd/.ps1 claude shim end to end.
111 lines
5.4 KiB
TypeScript
111 lines
5.4 KiB
TypeScript
// How each Agent gets an attachment: only PNG/JPEG/GIF/WebP are images; text files are text; the rest go by path.
|
|
// Claude Code used to send every attachment (a .txt included) as an image block, and Codex as localImage.
|
|
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
import { mkdtempSync, rmSync, truncateSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { attachmentKind, baseMime } from "../../daemon/src/util/attachmentKinds.js";
|
|
import { buildClaudeContent, CLAUDE_MAX_IMAGE_BYTES } from "../../daemon/src/providers/claude/content.js";
|
|
import { buildCodexInput } from "../../daemon/src/providers/codex/input.js";
|
|
import { buildAcpPrompt } from "../../daemon/src/providers/acp/prompt.js";
|
|
|
|
const PNG_B64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==";
|
|
let dir: string, png: string, txt: string, svg: string, bin: string;
|
|
// what the attachment store gives text uploads
|
|
const TEXT = "text/plain; charset=utf-8";
|
|
|
|
beforeAll(() => {
|
|
dir = mkdtempSync(join(tmpdir(), "att-content-"));
|
|
png = join(dir, "shot.png");
|
|
writeFileSync(png, Buffer.from(PNG_B64, "base64"));
|
|
txt = join(dir, "notes.txt");
|
|
writeFileSync(txt, "第一行 notes\n");
|
|
svg = join(dir, "logo.svg");
|
|
writeFileSync(svg, "<svg xmlns='http://www.w3.org/2000/svg'/>");
|
|
bin = join(dir, "blob.bin");
|
|
writeFileSync(bin, Buffer.from([0, 1, 2, 3]));
|
|
});
|
|
afterAll(() => rmSync(dir, { recursive: true, force: true }));
|
|
|
|
describe("attachmentKind", () => {
|
|
it("knows the four model image types, text, and the rest", () => {
|
|
for (const m of ["image/png", "image/jpeg", "image/gif", "image/webp", "IMAGE/PNG"]) expect(attachmentKind(m)).toBe("image");
|
|
for (const m of [TEXT, "text/markdown", "application/json"]) expect(attachmentKind(m)).toBe("text");
|
|
for (const m of ["image/svg+xml", "image/bmp", "application/pdf", "application/octet-stream", "", undefined]) expect(attachmentKind(m)).toBe("other");
|
|
expect(baseMime(TEXT)).toBe("text/plain");
|
|
});
|
|
});
|
|
|
|
describe("Claude Code content", () => {
|
|
it("an image is an image block, a text file a plain-text document block, the text last", async () => {
|
|
const r = await buildClaudeContent("look", [{ path: png, mime: "image/png" }, { path: txt, mime: TEXT }]);
|
|
expect(r.blocks).toEqual([
|
|
{ type: "image", source: { type: "base64", media_type: "image/png", data: PNG_B64 } },
|
|
{ type: "document", source: { type: "text", media_type: "text/plain", data: "第一行 notes\n" }, title: "notes.txt", context: txt },
|
|
{ type: "text", text: "look" },
|
|
]);
|
|
expect(r.warnings).toEqual([]);
|
|
});
|
|
|
|
it("a text file is never an image block (the old bug)", async () => {
|
|
const r = await buildClaudeContent("", [{ path: txt, mime: TEXT }]);
|
|
expect(r.blocks.some((b) => b.type === "image")).toBe(false);
|
|
});
|
|
|
|
it("svg, binaries and images over 5 MB go by path, with a warning", async () => {
|
|
const big = join(dir, "big.png");
|
|
writeFileSync(big, "");
|
|
truncateSync(big, CLAUDE_MAX_IMAGE_BYTES + 1);
|
|
const r = await buildClaudeContent("hi", [
|
|
{ path: svg, mime: "image/svg+xml" },
|
|
{ path: bin, mime: "application/octet-stream" },
|
|
{ path: big, mime: "image/png" },
|
|
]);
|
|
expect(r.blocks).toEqual([{ type: "text", text: `hi\n\nAttached file: ${svg}\nAttached file: ${bin}\nAttached image: ${big}` }]);
|
|
expect(r.warnings).toHaveLength(3);
|
|
expect(r.warnings[0]).toContain("image/svg+xml");
|
|
expect(r.warnings[2]).toContain("5 MB");
|
|
});
|
|
|
|
it("a missing attachment is a warning, not a failed message", async () => {
|
|
const r = await buildClaudeContent("hi", [{ path: join(dir, "gone.png"), mime: "image/png" }]);
|
|
expect(r.blocks).toEqual([{ type: "text", text: "hi" }]);
|
|
expect(r.warnings).toHaveLength(1);
|
|
});
|
|
});
|
|
|
|
describe("Codex input", () => {
|
|
it("only images are localImage; text files and others are paths in the text", async () => {
|
|
const r = await buildCodexInput("look", [
|
|
{ path: png, mime: "image/jpeg" },
|
|
{ path: txt, mime: TEXT },
|
|
{ path: bin, mime: "application/octet-stream" },
|
|
]);
|
|
expect(r.items).toEqual([
|
|
{ type: "text", text: `look\n\nAttached file: ${txt}\nAttached file: ${bin}` },
|
|
{ type: "localImage", path: png },
|
|
]);
|
|
expect(r.warnings).toHaveLength(1);
|
|
expect(r.warnings[0]).toContain("blob.bin");
|
|
});
|
|
|
|
it("a text-only attachment never becomes localImage (the old bug)", async () => {
|
|
const r = await buildCodexInput("", [{ path: txt, mime: TEXT }]);
|
|
expect(r.items).toEqual([{ type: "text", text: `Attached file: ${txt}` }]);
|
|
expect(r.warnings).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("ACP prompt", () => {
|
|
it("an svg is a file by path even for an Agent that takes images", async () => {
|
|
const r = await buildAcpPrompt("", [{ path: svg, mime: "image/svg+xml" }], { image: true, audio: false, embeddedContext: false });
|
|
expect(r.blocks.some((b) => b.type === "image")).toBe(false);
|
|
expect(r.blocks[0]).toEqual({ type: "text", text: `Attached file: ${svg}` });
|
|
});
|
|
|
|
it("a text upload (text/plain; charset=utf-8) is embedded with embeddedContext", async () => {
|
|
const r = await buildAcpPrompt("", [{ path: txt, mime: TEXT }], { image: true, audio: false, embeddedContext: true });
|
|
expect(r.blocks).toEqual([{ type: "resource", resource: { uri: expect.stringMatching(/^file:/), mimeType: TEXT, text: "第一行 notes\n" } }]);
|
|
});
|
|
});
|