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.
This commit is contained in:
qingnian108
2026-10-31 20:11:22 +08:00
committed by GitHub
parent 62ba6d7267
commit a431798fe1
527 changed files with 180426 additions and 0 deletions
@@ -0,0 +1,5 @@
// The daemon token lives in the fragment so it is not sent in the HTTP Referer.
export function getToken(): string {
const fragment = location.hash.startsWith("#") ? location.hash.slice(1) : location.hash;
return new URLSearchParams(fragment).get("t") ?? "";
}
+174
View File
@@ -0,0 +1,174 @@
// WS client: hello, request/response, subscriptions, auto-reconnect, binary frames.
import { t } from "../i18n/index.js";
export type ConnState = "connecting" | "open" | "reconnecting" | "down";
export type EvtHandler = (ch: string, data: unknown) => void;
export type BinHandler = (op: number, index: number, payload: Uint8Array) => void;
type Pending = { res: (v: unknown) => void; rej: (e: Error) => void; timer: ReturnType<typeof setTimeout> };
export class WsClient {
private ws: WebSocket | null = null;
private seq = 0;
private pending = new Map<number, Pending>();
private subs = new Map<string, { handler: EvtHandler; afterSeq?: () => number | undefined }>();
private binHandlers: BinHandler[] = [];
private stateListeners = new Set<(s: ConnState) => void>();
private state: ConnState = "connecting";
private retry = 0;
private closed = false;
private helloOk: Record<string, unknown> | null = null;
private helloWaiters: ((v: Record<string, unknown>) => void)[] = [];
constructor(private token: string, private lang = "zh") {}
onState(fn: (s: ConnState) => void): () => void {
this.stateListeners.add(fn);
fn(this.state);
return () => this.stateListeners.delete(fn);
}
getState(): ConnState {
return this.state;
}
private setState(s: ConnState): void {
this.state = s;
for (const f of this.stateListeners) f(s);
}
connect(): void {
if (this.closed) return;
this.setState(this.retry === 0 ? "connecting" : "reconnecting");
const ws = new WebSocket(`ws://${location.host}/ws`);
ws.binaryType = "arraybuffer";
this.ws = ws;
ws.onopen = () => {
ws.send(JSON.stringify({ t: "hello", token: this.token, v: 1, lang: this.lang }));
};
ws.onmessage = (ev) => {
if (typeof ev.data !== "string") {
this.onBinary(new Uint8Array(ev.data as ArrayBuffer));
return;
}
let msg: Record<string, unknown>;
try {
msg = JSON.parse(ev.data);
} catch {
return;
}
if (msg.t === "hello.ok") {
this.retry = 0;
this.helloOk = msg;
this.setState("open");
const w = this.helloWaiters.splice(0);
for (const fn of w) fn(msg);
// Restore subscriptions after reconnecting
for (const [ch, s] of this.subs) {
void this.call("sub.add", { ch, afterSeq: s.afterSeq?.() }).catch(() => {});
}
return;
}
if (msg.t === "res") {
const id = msg.id as number;
const p = this.pending.get(id);
if (p) {
this.pending.delete(id);
clearTimeout(p.timer);
if (msg.ok) p.res(msg.r);
else {
const e = msg.e as { code?: string; message?: string } | undefined;
const err = new Error(e?.message ?? t("api.requestFailed"));
(err as { code?: string }).code = e?.code;
p.rej(err);
}
}
return;
}
if (msg.t === "evt") {
const sub = this.subs.get(msg.ch as string);
sub?.handler(msg.ch as string, msg.d);
return;
}
};
ws.onclose = () => {
this.ws = null;
// Reject all pending requests
for (const [id, p] of this.pending) {
clearTimeout(p.timer);
p.rej(new Error("连接中断"));
this.pending.delete(id);
}
if (this.closed) return;
this.retry++;
const delay = Math.min(1000 * Math.pow(2, this.retry - 1), 10000);
this.setState(this.retry > 20 ? "down" : "reconnecting");
setTimeout(() => this.connect(), delay);
};
ws.onerror = () => {};
}
waitHello(): Promise<Record<string, unknown>> {
if (this.helloOk) return Promise.resolve(this.helloOk);
return new Promise((res) => this.helloWaiters.push(res));
}
call(method: string, params?: unknown, timeoutMs = 60000): Promise<unknown> {
return new Promise((res, rej) => {
const ws = this.ws;
if (!ws || ws.readyState !== WebSocket.OPEN) {
rej(new Error("连接未建立"));
return;
}
const id = ++this.seq;
const timer = setTimeout(() => {
this.pending.delete(id);
rej(new Error("请求超时"));
}, timeoutMs);
this.pending.set(id, { res, rej, timer });
ws.send(JSON.stringify({ t: "req", id, m: method, p: params ?? {} }));
});
}
subscribe(ch: string, handler: EvtHandler, afterSeq?: () => number | undefined): () => void {
this.subs.set(ch, { handler, afterSeq });
if (this.state === "open") {
void this.call("sub.add", { ch, afterSeq: afterSeq?.() }).catch(() => {});
}
return () => {
this.subs.delete(ch);
if (this.state === "open") void this.call("sub.remove", { ch }).catch(() => {});
};
}
onBinaryFrame(fn: BinHandler): () => void {
this.binHandlers.push(fn);
return () => {
this.binHandlers = this.binHandlers.filter((f) => f !== fn);
};
}
private onBinary(data: Uint8Array): void {
if (data.length < 3) return;
const op = data[0];
const index = (data[1] << 8) | data[2];
for (const f of this.binHandlers) f(op, index, data.subarray(3));
}
sendBinary(op: number, index: number, payload: Uint8Array): void {
const ws = this.ws;
if (!ws || ws.readyState !== WebSocket.OPEN) return;
const frame = new Uint8Array(3 + payload.length);
frame[0] = op;
frame[1] = (index >> 8) & 0xff;
frame[2] = index & 0xff;
frame.set(payload, 3);
ws.send(frame);
}
ping(): void {
try {
this.ws?.send(JSON.stringify({ t: "ping" }));
} catch {}
}
close(): void {
this.closed = true;
this.ws?.close();
}
}