Files
plugins/packages/agent-console/web/src/components/Menu.tsx
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

78 lines
2.7 KiB
TypeScript

// Generic popup menu: anchored positioning, close on outside click/Esc.
import { useEffect, useLayoutEffect, useRef, useState, type ReactNode } from "react";
import { createPortal } from "react-dom";
export type MenuItem =
| { kind: "item"; label: string; danger?: boolean; disabled?: boolean; checked?: boolean; onClick: () => void }
| { kind: "sep" }
| { kind: "label"; text: string };
export function Menu({
x, y, items, onClose,
}: {
x: number; y: number; items: MenuItem[]; onClose: () => void;
}) {
const ref = useRef<HTMLDivElement>(null);
const [pos, setPos] = useState({ left: x, top: y });
useLayoutEffect(() => {
const el = ref.current;
if (!el) return;
const r = el.getBoundingClientRect();
let { left, top } = pos;
if (left + r.width > window.innerWidth - 8) left = Math.max(8, window.innerWidth - r.width - 8);
if (top + r.height > window.innerHeight - 8) top = Math.max(8, window.innerHeight - r.height - 8);
if (left !== pos.left || top !== pos.top) setPos({ left, top });
}, [x, y]); // eslint-disable-line react-hooks/exhaustive-deps
useEffect(() => {
const onDown = (e: PointerEvent) => {
if (ref.current && !ref.current.contains(e.target as Node)) onClose();
};
const onKey = (e: KeyboardEvent) => {
if (e.key === "Escape") { e.stopPropagation(); onClose(); }
};
window.addEventListener("pointerdown", onDown, true);
window.addEventListener("keydown", onKey, true);
return () => {
window.removeEventListener("pointerdown", onDown, true);
window.removeEventListener("keydown", onKey, true);
};
}, [onClose]);
return createPortal(
<div ref={ref} className="menu" style={pos} role="menu">
{items.map((it, i) =>
it.kind === "sep" ? (
<div key={i} className="msep" />
) : it.kind === "label" ? (
<div key={i} className="mlabel">{it.text}</div>
) : (
<button
key={i}
className={`mi${it.danger ? " danger" : ""}`}
disabled={it.disabled}
role="menuitem"
onClick={() => { onClose(); it.onClick(); }}
>
<span>{it.label}</span>
{it.checked && <span className="mi-check">✓</span>}
</button>
),
)}
</div>,
document.body,
);
}
// Trigger helper: turns onContextMenu/onClick into {x,y} state.
export function useMenu(): [{ x: number; y: number } | null, (e: React.MouseEvent) => void, () => void] {
const [pos, setPos] = useState<{ x: number; y: number } | null>(null);
const open = (e: React.MouseEvent) => {
e.preventDefault();
e.stopPropagation();
setPos({ x: e.clientX, y: e.clientY });
};
return [pos, open, () => setPos(null)];
}