Files
plugins/packages/agent-console/daemon/src/providers/acp/mapUpdate.ts
T
qingnian108 a431798fe1 agent-console 1.0.0: a page plugin that drives the local Agent CLIs (#535)
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.
2026-10-31 20:11:22 +08:00

120 lines
3.8 KiB
TypeScript

import type * as schema from "@agentclientprotocol/sdk";
import type { ProviderEvent } from "../../timeline/types.js";
import type { ToolKind } from "../../timeline/types.js";
const rid = () => Math.random().toString(36).slice(2, 8);
function mapToolKind(k: schema.ToolKind | null | undefined): ToolKind {
switch (k) {
case "read":
case "edit":
case "search":
return k;
case "execute":
return "bash";
case "fetch":
return "web";
default:
return "other";
}
}
function mapToolStatus(s: schema.ToolCallStatus | null | undefined): "running" | "ok" | "failed" {
if (s === "completed") return "ok";
if (s === "failed") return "failed";
return "running";
}
function textFromContent(content: schema.ContentBlock): string {
if (content.type === "text") return content.text;
return "";
}
export type MapCtx = { itemPrefix: string; messageIds: Map<string, string> };
export function newMapCtx(): MapCtx {
return { itemPrefix: "a" + rid() + "_", messageIds: new Map() };
}
export function mapSessionUpdate(update: schema.SessionUpdate, ctx: MapCtx): ProviderEvent[] {
const out: ProviderEvent[] = [];
const mid = "messageId" in update && update.messageId ? String(update.messageId) : null;
switch (update.sessionUpdate) {
case "agent_message_chunk": {
const text = textFromContent(update.content);
if (!text) break;
const itemId = mid ? ctx.messageIds.get(mid) ?? ctx.itemPrefix + mid : ctx.itemPrefix + "msg";
if (mid) ctx.messageIds.set(mid, itemId);
out.push({ type: "assistant_delta", itemId, text });
break;
}
case "agent_thought_chunk": {
const text = textFromContent(update.content);
if (!text) break;
const itemId = mid ? ctx.itemPrefix + "t_" + mid : ctx.itemPrefix + "think";
out.push({ type: "reasoning_delta", itemId, text });
break;
}
case "tool_call":
out.push({
type: "tool_call",
itemId: ctx.itemPrefix + update.toolCallId,
kind: mapToolKind(update.kind),
title: update.title,
status: mapToolStatus(update.status),
input: update.rawInput,
});
break;
case "tool_call_update":
out.push({
type: "tool_call",
itemId: ctx.itemPrefix + update.toolCallId,
kind: mapToolKind(update.kind),
title: update.title ?? update.name ?? "tool",
status: mapToolStatus(update.status),
input: update.rawInput,
output: typeof update.rawOutput === "string" ? update.rawOutput : undefined,
});
break;
case "plan":
out.push({
type: "plan",
itemId: ctx.itemPrefix + "plan",
markdown: update.entries.map((e) => `- ${e.content}`).join("\n"),
entries: update.entries.map((e) => ({ step: e.content, status: e.status ?? "" })),
});
break;
case "notice":
out.push({
type: "notice",
level: update.severity === "error" ? "error" : update.severity === "warning" ? "warn" : "info",
message: update.description ? `${update.title}: ${update.description}` : update.title,
});
break;
case "usage_update":
out.push({
type: "usage",
inputTokens: update.used,
outputTokens: 0,
cacheReadTokens: 0,
costUsd: typeof update.cost?.amount === "number" ? update.cost.amount : 0,
contextWindow: update.size,
});
break;
default:
break;
}
return out;
}
export function stopToTurnEnd(stopReason: schema.StopReason, turnId: string, err?: string): ProviderEvent {
if (stopReason === "cancelled") {
return { type: "turn_ended", turnId, status: "interrupted" };
}
if (stopReason === "refusal" || stopReason === "max_tokens") {
return { type: "turn_ended", turnId, status: "failed", error: err ?? stopReason };
}
return { type: "turn_ended", turnId, status: "completed" };
}