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.
119 lines
3.7 KiB
TypeScript
119 lines
3.7 KiB
TypeScript
import { readdir, readFile, stat } from "node:fs/promises";
|
|
import { homedir } from "node:os";
|
|
import { basename, join } from "node:path";
|
|
import type { SlashCommand } from "../slash.js";
|
|
import { parseSlashInput, sortSlashCommands } from "../slash.js";
|
|
|
|
export type CodexSlashEntry = SlashCommand & { body: string };
|
|
|
|
function parseFrontMatter(raw: string): { meta: Record<string, string>; body: string } {
|
|
if (!raw.startsWith("---\n")) return { meta: {}, body: raw };
|
|
const end = raw.indexOf("\n---\n", 4);
|
|
if (end === -1) return { meta: {}, body: raw };
|
|
const fm = raw.slice(4, end);
|
|
const meta: Record<string, string> = {};
|
|
for (const line of fm.split(/\r?\n/)) {
|
|
const m = line.match(/^([a-zA-Z0-9_-]+):\s*(.+)$/);
|
|
if (m) meta[m[1]!] = m[2]!.trim().replace(/^["']|["']$/g, "");
|
|
}
|
|
return { meta, body: raw.slice(end + 5) };
|
|
}
|
|
|
|
function firstLine(body: string): string {
|
|
for (const line of body.split(/\r?\n/)) {
|
|
const t = line.trim();
|
|
if (t && !t.startsWith("#")) return t.replace(/^#+\s*/, "").slice(0, 120);
|
|
}
|
|
return "";
|
|
}
|
|
|
|
async function readPromptFiles(dir: string): Promise<CodexSlashEntry[]> {
|
|
const out: CodexSlashEntry[] = [];
|
|
let names: string[] = [];
|
|
try {
|
|
names = await readdir(dir);
|
|
} catch {
|
|
return out;
|
|
}
|
|
for (const f of names) {
|
|
if (!f.endsWith(".md")) continue;
|
|
const path = join(dir, f);
|
|
try {
|
|
const st = await stat(path);
|
|
if (!st.isFile()) continue;
|
|
const raw = await readFile(path, "utf8");
|
|
const { meta, body } = parseFrontMatter(raw);
|
|
const name = basename(f, ".md");
|
|
out.push({
|
|
name,
|
|
description: meta.description || firstLine(body) || name,
|
|
argumentHint: meta.argumentHint || meta["argument-hint"],
|
|
kind: "command",
|
|
body,
|
|
});
|
|
} catch {
|
|
/* skip */
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
async function readSkillDirs(root: string): Promise<CodexSlashEntry[]> {
|
|
const out: CodexSlashEntry[] = [];
|
|
let dirs: string[] = [];
|
|
try {
|
|
dirs = await readdir(root);
|
|
} catch {
|
|
return out;
|
|
}
|
|
for (const d of dirs) {
|
|
const skillPath = join(root, d, "SKILL.md");
|
|
try {
|
|
const st = await stat(skillPath);
|
|
if (!st.isFile()) continue;
|
|
const raw = await readFile(skillPath, "utf8");
|
|
const { meta, body } = parseFrontMatter(raw);
|
|
const name = meta.name || d;
|
|
out.push({
|
|
name,
|
|
description: meta.description || firstLine(body) || name,
|
|
argumentHint: meta.argumentHint,
|
|
kind: "skill",
|
|
body,
|
|
});
|
|
} catch {
|
|
/* skip */
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
export async function loadCodexSlashEntries(cwd: string, opts?: { skipHome?: boolean }): Promise<CodexSlashEntry[]> {
|
|
const home = homedir();
|
|
const parts = await Promise.all([
|
|
opts?.skipHome ? Promise.resolve([]) : readPromptFiles(join(home, ".codex", "prompts")),
|
|
readPromptFiles(join(cwd, ".codex", "prompts")),
|
|
opts?.skipHome ? Promise.resolve([]) : readSkillDirs(join(home, ".codex", "skills")),
|
|
readSkillDirs(join(cwd, ".codex", "skills")),
|
|
]);
|
|
const byName = new Map<string, CodexSlashEntry>();
|
|
for (const list of parts) {
|
|
for (const e of list) {
|
|
if (!byName.has(e.name)) byName.set(e.name, e);
|
|
}
|
|
}
|
|
return sortSlashCommands([...byName.values()]) as CodexSlashEntry[];
|
|
}
|
|
|
|
export function expandCodexSlash(text: string, entries: CodexSlashEntry[]): string {
|
|
const parsed = parseSlashInput(text);
|
|
if (!parsed) return text;
|
|
const hit = entries.find((e) => e.name === parsed.name);
|
|
if (!hit) return text;
|
|
return hit.body.replace(/\$ARGUMENTS/g, parsed.args);
|
|
}
|
|
|
|
export function codexSlashCommandsFromEntries(entries: CodexSlashEntry[]): SlashCommand[] {
|
|
return entries.map(({ body: _b, ...rest }) => rest);
|
|
}
|