diff --git a/scripts/test-launcher.mjs b/scripts/test-launcher.mjs index d552427..0578f17 100644 --- a/scripts/test-launcher.mjs +++ b/scripts/test-launcher.mjs @@ -23,9 +23,11 @@ const NOTICE = "will use the model you last used in it"; /** * A temp home and a bin dir holding stand-ins for `clis`; each writes its args to /.json. * `installed: "X.Y.Z"` runs an installed copy instead, as install.js lays it out: the launcher, - * models.json and update-check.mjs in /.plan2code/bin/ beside a version.json saying X.Y.Z. + * models.json, update-check.mjs, agents.json and agent-lookup.mjs in /.plan2code/bin/ + * beside a version.json saying X.Y.Z (`lookup: false` leaves agent-lookup.mjs out, `agents` + * replaces agents.json's contents). */ -function sandbox(clis, { installed, project = true } = {}) { +function sandbox(clis, { installed, project = true, lookup = true, agents } = {}) { const dir = fs.mkdtempSync(path.join(os.tmpdir(), "p2c-launcher-")); // `dir` is the launcher's cwd below; mark it as a project so the project-confirmation gate // does not fire in every other test here. The gate's own tests pass `project: false`. @@ -43,6 +45,10 @@ function sandbox(clis, { installed, project = true } = {}) { fs.copyFileSync(LAUNCHER, launcher); fs.copyFileSync(path.join(path.dirname(LAUNCHER), "models.json"), path.join(binCopy, "models.json")); fs.copyFileSync(path.join(ROOT, "src", "web-console", "update-check.mjs"), path.join(binCopy, "update-check.mjs")); + const agentsFile = agents || path.join(path.dirname(LAUNCHER), "agents.json"); + if (typeof agentsFile === "string" && fs.existsSync(agentsFile)) fs.copyFileSync(agentsFile, path.join(binCopy, "agents.json")); + else if (agentsFile) fs.writeFileSync(path.join(binCopy, "agents.json"), JSON.stringify(agentsFile)); + if (lookup) fs.copyFileSync(path.join(ROOT, "src", "web-console", "agent-lookup.mjs"), path.join(binCopy, "agent-lookup.mjs")); fs.writeFileSync(path.join(binCopy, "version.json"), JSON.stringify({ name: "Plan2Code", version: installed })); } const recorder = path.join(bin, "record.js"); @@ -239,6 +245,22 @@ test("launcher: a forwarded --model skips the model notice", (t) => { } }); +test("launcher: a launched model is remembered per agent in launcher.json", (t) => { + const box = sandbox(["claude", "codex"]); + t.after(box.cleanup); + fs.mkdirSync(path.dirname(box.stateFile), { recursive: true }); + fs.writeFileSync(box.stateFile, JSON.stringify({ lastFolder: box.home })); + + assert.equal(box.run(["--cli", "claude", "--model", "opus"]).status, 0); + assert.equal(box.run(["--cli", "codex", "-m", "x"]).status, 0); + assert.equal(box.run(["--cli", "codex", "-my"]).status, 0); + assert.deepEqual(JSON.parse(fs.readFileSync(box.stateFile, "utf8")), { + lastFolder: box.home, + lastCli: "codex", + lastModels: { claude: "opus", codex: "y" }, + }); +}); + test("launcher: --cli picks the CLI and is never forwarded; the pick is merged into launcher.json", (t) => { const box = sandbox(["claude", "devin"]); t.after(box.cleanup); @@ -287,7 +309,7 @@ test("launcher: an unknown --cli exits 2, a missing CLI 127, and nothing is star assert.equal(missing.status, 127); assert.match(missing.stderr, /the `devin` CLI was not found/); assert.match(missing.stderr, /\n\nTo use Devin:\n/); - assert.ok(missing.stderr.includes("1. Install it: https://devin.ai")); + assert.ok(missing.stderr.includes("1. Install it: https://devin.ai (https://docs.devin.ai/work-with-devin/devin-cli)")); assert.ok(missing.stderr.includes("start Devin and type /plan2code.")); // Only the steps for the requested CLI: no "install one of" list that contradicts it. assert.doesNotMatch(missing.stderr, /Install one:/); @@ -339,7 +361,8 @@ test("launcher: with no CLI installed it exits 127 and explains how to get one a ]) { assert.ok(result.stderr.includes(part), part); } - assert.ok(result.stderr.includes("Add it in User Preferences (web console) under Agents, then run `plan2code` again.")); + assert.ok(result.stderr.includes("Add it by editing `~/.plan2code/agents.json`, then run `plan2code` again.")); + assert.doesNotMatch(result.stderr, /User Preferences/); assert.ok(result.stderr.includes("type /plan2code ($plan2code in Codex)")); assert.match(result.stderr, /cd into the project/); }); @@ -564,6 +587,8 @@ test("launcher app: modelFrom reads --model, --model= and Codex's -m", () => { assert.equal(modelFrom(["--model=x"], "claude"), "x"); assert.equal(modelFrom(["-m", "x"], "codex"), "x"); assert.equal(modelFrom(["-m=x"], "codex"), "x"); + assert.equal(modelFrom(["-mx"], "codex"), "x"); + assert.equal(modelFrom(["-m"], "codex"), null); assert.equal(modelFrom(["-m", "x"], "claude"), null); assert.equal(modelFrom(["--verbose"], "claude"), null); }); @@ -810,7 +835,7 @@ test("launcher agents: a custom agent whose command is not found is left out and const result = none.run([]); assert.equal(result.status, 127); assert.match(result.stderr, /Custom agent Ghost: ghost-cmd not found/); - assert.ok(result.stderr.includes("User Preferences")); + assert.ok(result.stderr.includes("agents.json")); }); test("launcher agents: a malformed agents.json warns on a terminal, never crashes, and gives no agents", (t) => { @@ -848,6 +873,79 @@ test("launcher agents: the app hand-over passes a custom id and its model unchan assert.equal(JSON.parse(fs.readFileSync(box.stateFile, "utf8")).lastCli, "goose"); }); +/* ------------------------- the shipped agent list (installed layout) ------------------------- */ + +const OS_KEY = IS_WINDOWS ? "win" : process.platform === "darwin" ? "mac" : "linux"; +const SHIPPED_AGENTS = JSON.parse(fs.readFileSync(path.join(path.dirname(LAUNCHER), "agents.json"), "utf8")); +const builtin = (id) => SHIPPED_AGENTS.agents.find((a) => a.id === id); +const leadRun = (id) => builtin(id).install[OS_KEY][0].run; + +test("launcher list: the menu and launch come from the sandboxed agents.json + agent-lookup.mjs", (t) => { + const box = sandbox(["claude", "devin", "codex"], { installed: "0.0.0" }); + t.after(box.cleanup); + const result = box.run(["--cli", "devin", "--verbose"]); + assert.equal(result.status, 0, result.stderr); + assert.deepEqual(box.argsOf("devin"), ["--permission-mode", "bypass", "--verbose", "--", "/plan2code"]); + // The menu's order and shape come from the list: --cli errors list the same ids. + const bad = box.run(["--cli", "bogus"]); + assert.equal(bad.status, 2); + assert.match(bad.stderr, /--cli takes claude, devin or codex, not "bogus"/); +}); + +test("launcher list: a forwarded --model fills {model} in place; Codex's -m forwards untouched", (t) => { + const box = sandbox(["claude", "codex"], { installed: "0.0.0" }); + t.after(box.cleanup); + assert.equal(box.run(["--cli", "claude", "--model", "opus"]).status, 0); + assert.deepEqual(box.argsOf("claude"), ["/plan2code", "--permission-mode", "bypassPermissions", "--model", "opus"]); + assert.equal(box.run(["--cli", "codex", "-m", "x"]).status, 0); + assert.deepEqual(box.argsOf("codex"), ["--dangerously-bypass-approvals-and-sandbox", "-m", "x", "$plan2code"]); +}); + +test("launcher list: the last-used notice names the gone agent and the pick, on a terminal only", () => { + const { lastUsedNotice } = require(LAUNCHER); + const all = [ + { id: "claude", label: "Claude Code" }, + { id: "devin", label: "Devin" }, + ]; + const installed = [all[0]]; + assert.equal(lastUsedNotice(installed, all, "devin"), "Devin (last used) was not found, using Claude Code"); + assert.equal(lastUsedNotice(installed, all, "claude"), null, "still runnable"); + assert.equal(lastUsedNotice(installed, all, "ghost"), "ghost (last used) was not found, using Claude Code"); + assert.equal(lastUsedNotice([], all, "devin"), null, "nothing to use"); + assert.equal(lastUsedNotice(installed, all, undefined), null); +}); + +test("launcher list: a missing agent-lookup.mjs falls back to the built-in list", (t) => { + const box = sandbox(["claude"], { installed: "0.0.0", lookup: false }); + t.after(box.cleanup); + const result = box.run([]); + assert.equal(result.status, 0, result.stderr); + assert.deepEqual(box.argsOf("claude"), ["/plan2code", "--permission-mode", "bypassPermissions"]); +}); + +test("launcher list: with no agent and no terminal it prints this OS's install steps and exits 127", (t) => { + const box = sandbox([], { installed: "0.0.0" }); + t.after(box.cleanup); + const result = box.run([]); + assert.equal(result.status, 127); + for (const id of ["claude", "devin", "codex"]) { + assert.ok(result.stderr.includes(leadRun(id)), `${id}: ${leadRun(id)}`); + assert.ok(result.stderr.includes(builtin(id).installUrl), id); + } + assert.ok(result.stderr.includes("add another agent by editing `~/.plan2code/agents.json`, then run `plan2code` again") || result.stderr.includes("Add it by editing `~/.plan2code/agents.json`, then run `plan2code` again.")); + assert.doesNotMatch(result.stderr, /User Preferences/); +}); + +test("launcher list: --cli for a missing built-in prints its lead install line and exits 127", (t) => { + const box = sandbox(["claude"], { installed: "0.0.0" }); + t.after(box.cleanup); + const result = box.run(["--cli", "devin"]); + assert.equal(result.status, 127); + assert.match(result.stderr, /the `devin` CLI was not found/); + assert.ok(result.stderr.includes(leadRun("devin")), leadRun("devin")); + assert.ok(result.stderr.includes(builtin("devin").installUrl)); +}); + test("launcher agents: a full-path command starts, and on Windows a .cmd in a folder with a space too", (t) => { const box = sandbox([]); t.after(box.cleanup); @@ -872,3 +970,251 @@ test("launcher agents: a full-path command starts, and on Windows a .cmd in a fo assert.deepEqual(box.argsOf("spaced"), ["/plan2code", "--note", "a b"]); assert.equal(box.argsOf("decoy"), null); }); + +/* ------------------------- first-run setup (the browser setup page) ------------------------- */ + +// When no agent can run and a person can see a page, the launcher opens the console's +// agent-less `setup` workflow (`open --workflow setup`, plus `--for-cli` when one agent +// was asked for), then waits on the session until a send carries `__start`. These drive +// runSetupFlow() directly against a stand-in console.mjs that records each call and +// answers `wait` from a step list in P2C_FAKE_STEPS. + +// The app-window variables scrubbedEnv() must strip from the console child (NFR-2). +const APP_VARS = [ + "PLAN2CODE_IN_APP", + "PLAN2CODE_APP_TOKEN", + "PLAN2CODE_APP_ANNOUNCE", + "PLAN2CODE_EMBED_ORIGIN", + "PLAN2CODE_CONSOLE_SID", + "PLAN2CODE_APP_VERSION", +]; + +// The stand-in console: every call is recorded (argv plus which app vars leaked through), +// `open` prints a setup session's JSON, `wait` plays back the numbered step. A step the +// test forgot to write ends as a Cancel rather than hanging the four-hour cap. +const FAKE_CONSOLE = [ + 'import fs from "node:fs";', + 'import path from "node:path";', + 'const dir = path.dirname(process.argv[1]);', + `const APP = ${JSON.stringify(APP_VARS)};`, + 'fs.appendFileSync(path.join(dir, "calls.jsonl"), JSON.stringify({ argv: process.argv.slice(2), appVars: APP.filter((k) => k in process.env) }) + "\\n");', + 'if (process.argv[2] === "open") {', + ' console.log(JSON.stringify({ sid: "setup-sid", url: "http://127.0.0.1:9/s/signtok/", setup: { url: "http://127.0.0.1:9/s/signtok/", token: "handover-tok" } }));', + '} else if (process.argv[2] === "wait") {', + ' const file = path.join(dir, "waits");', + ' const n = fs.existsSync(file) ? Number(fs.readFileSync(file, "utf8")) : 0;', + ' fs.writeFileSync(file, String(n + 1));', + ' const steps = JSON.parse(process.env.P2C_FAKE_STEPS || "[]");', + ' const step = steps[n] || { exit: 30 };', + ' if (step.print) console.log(JSON.stringify(step.print));', + ' process.exit(step.exit === undefined ? 0 : step.exit);', + '}', +].join("\n"); + +// A folder holding the stand-in console.mjs (the wait counter lives beside it); calls() +// replays what it was asked, oldest first. +function setupConsoleDir() { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "p2c-setup-console-")); + fs.writeFileSync(path.join(dir, "console.mjs"), FAKE_CONSOLE); + return { + dir, + calls: () => + fs.existsSync(path.join(dir, "calls.jsonl")) + ? fs.readFileSync(path.join(dir, "calls.jsonl"), "utf8").trim().split("\n").map(JSON.parse) + : [], + cleanup: () => fs.rmSync(dir, { recursive: true, force: true }), + }; +} + +// Point the console search at the stand-in and hand it the wait steps; restores +// PLAN2CODE_CONSOLE_DIR, P2C_FAKE_STEPS and any app vars `extra` set afterwards. +function setupEnv(t, dir, steps, extra = {}) { + const keys = ["PLAN2CODE_CONSOLE_DIR", "P2C_FAKE_STEPS", ...APP_VARS]; + const saved = Object.fromEntries(keys.map((k) => [k, process.env[k]])); + process.env.PLAN2CODE_CONSOLE_DIR = dir; + process.env.P2C_FAKE_STEPS = JSON.stringify(steps); + for (const [k, v] of Object.entries(extra)) process.env[k] = v; + t.after(() => { + for (const k of keys) { + if (saved[k] === undefined) delete process.env[k]; + else process.env[k] = saved[k]; + } + }); +} + +// A builtin-shaped list for runSetupFlow's re-lookup, and the send that starts claude. +const SETUP_CLIS = [{ id: "claude", label: "Claude Code", entry: { command: "claude" } }]; +const START_CLAUDE = { print: { session: [{ i: "__start", agent: "claude" }] }, exit: 0 }; +const foundAt = (file) => () => ({ state: "found", path: file }); + +test("launcher setup: a Start re-finds the agent and hands back the announce address and token", (t) => { + const box = setupConsoleDir(); + t.after(box.cleanup); + setupEnv(t, box.dir, [START_CLAUDE]); + const { runSetupFlow } = require(LAUNCHER); + const agentPath = path.join(box.dir, "claude.exe"); + const out = runSetupFlow("claude", SETUP_CLIS, foundAt(agentPath)); + assert.equal(out.cli.id, "claude"); + assert.equal(out.cli.path, agentPath, "the named agent is looked up again after the Start"); + assert.deepEqual(out.setup, { announce: "http://127.0.0.1:9/s/signtok/", token: "handover-tok" }); + assert.deepEqual( + box.calls().map((c) => c.argv), + [ + ["open", "--workflow", "setup", "--for-cli", "claude"], + ["wait", "--session", "setup-sid", "--seconds", "240"], + ] + ); +}); + +test("launcher setup: the console is spawned without the six app-window variables", (t) => { + const box = setupConsoleDir(); + t.after(box.cleanup); + // Every one set on this process: none may reach the console child. + setupEnv(t, box.dir, [START_CLAUDE], Object.fromEntries(APP_VARS.map((k) => [k, "live"]))); + const { runSetupFlow } = require(LAUNCHER); + runSetupFlow(undefined, SETUP_CLIS, foundAt(path.join(box.dir, "claude.exe"))); + const calls = box.calls(); + assert.deepEqual(calls[0].argv, ["open", "--workflow", "setup"], "no --for-cli without a named agent"); + for (const call of calls) assert.deepEqual(call.appVars, [], `app vars leaked into the console's ${call.argv[0]}`); +}); + +test("launcher setup: a send that is not a Start and a still-waiting slice keep the wait going", (t) => { + const box = setupConsoleDir(); + t.after(box.cleanup); + setupEnv(t, box.dir, [ + { print: { session: [{ i: "q1", type: "answer", kind: "choice", k: "A" }] }, exit: 0 }, + { exit: 10 }, + START_CLAUDE, + ]); + const { runSetupFlow } = require(LAUNCHER); + const out = runSetupFlow(undefined, SETUP_CLIS, foundAt(path.join(box.dir, "claude.exe"))); + assert.equal(out.cli.id, "claude"); + assert.equal(box.calls().filter((c) => c.argv[0] === "wait").length, 3, "exit 10 re-runs the wait, exit 0 without a Start keeps it"); +}); + +test("launcher setup: cancel, a gone server and a Start for an agent that still cannot run all end as 127", (t) => { + const { runSetupFlow } = require(LAUNCHER); + const cases = [ + ["cancel", [{ exit: 30 }]], + ["server gone", [{ exit: 20 }]], + ["Start for an agent the re-lookup misses", [{ print: { session: [{ i: "__start", agent: "ghost-cli" }] }, exit: 0 }]], + ]; + for (const [name, steps] of cases) { + const box = setupConsoleDir(); + t.after(box.cleanup); + setupEnv(t, box.dir, steps); + const out = runSetupFlow(undefined, SETUP_CLIS, foundAt(path.join(box.dir, "claude.exe"))); + assert.deepEqual(out, { code: 127 }, name); + } +}); + +test("launcher setup: an older console's open line still yields the announce address and token from the url", (t) => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "p2c-setup-console-")); + t.after(() => fs.rmSync(dir, { recursive: true, force: true })); + // No setup block in the open output: sid and the /s// url alone must do. + fs.writeFileSync( + path.join(dir, "console.mjs"), + [ + 'if (process.argv[2] === "open") {', + ' console.log(JSON.stringify({ sid: "setup-sid", url: "http://127.0.0.1:9/s/signtok/" }));', + '} else if (process.argv[2] === "wait") {', + ' console.log(JSON.stringify({ session: [{ i: "__start", agent: "claude" }] }));', + '}', + ].join("\n") + ); + setupEnv(t, dir, []); + const out = require(LAUNCHER).runSetupFlow(undefined, SETUP_CLIS, foundAt("x")); + assert.equal(out.cli.id, "claude"); + assert.deepEqual(out.setup, { announce: "http://127.0.0.1:9", token: "signtok" }); +}); + +test("launcher setup: with no console.mjs anywhere, runSetupFlow returns null rather than guessing", (t) => { + const launcherPath = require.resolve(LAUNCHER); + const home = fs.mkdtempSync(path.join(os.tmpdir(), "p2c-no-console-")); + t.after(() => fs.rmSync(home, { recursive: true, force: true })); + // Every place findConsoleMjs() looks is derived from the home or + // PLAN2CODE_CONSOLE_DIR, so a fresh module under an empty home finds nothing. + const keys = ["HOME", "USERPROFILE", "PLAN2CODE_CONSOLE_HOME", "PLAN2CODE_CONSOLE_DIR"]; + const saved = Object.fromEntries(keys.map((k) => [k, process.env[k]])); + delete require.cache[launcherPath]; + try { + process.env.HOME = home; + process.env.USERPROFILE = home; + delete process.env.PLAN2CODE_CONSOLE_HOME; + delete process.env.PLAN2CODE_CONSOLE_DIR; + assert.equal(require(LAUNCHER).runSetupFlow("claude", SETUP_CLIS, foundAt("x")), null); + } finally { + for (const k of keys) { + if (saved[k] === undefined) delete process.env[k]; + else process.env[k] = saved[k]; + } + // Re-require with the real home so later tests see the host's own module. + delete require.cache[launcherPath]; + require(LAUNCHER); + } +}); + +test("launcher setup: a Start can name a custom agent; the launcher re-reads ~/.plan2code/agents.json", (t) => { + const launcherPath = require.resolve(LAUNCHER); + const home = fs.mkdtempSync(path.join(os.tmpdir(), "p2c-setup-home-")); + const box = setupConsoleDir(); + t.after(() => { + fs.rmSync(home, { recursive: true, force: true }); + box.cleanup(); + }); + // USER_AGENTS_FILE is fixed at require time from the home, so the module needs + // a fresh instance whose home is the scratch one. + const keys = ["HOME", "USERPROFILE", "PLAN2CODE_CONSOLE_HOME"]; + const saved = Object.fromEntries(keys.map((k) => [k, process.env[k]])); + delete require.cache[launcherPath]; + try { + process.env.HOME = home; + process.env.USERPROFILE = home; + process.env.PLAN2CODE_CONSOLE_HOME = path.join(home, ".plan2code", "console"); + const { runSetupFlow } = require(LAUNCHER); + const agent = path.join(box.dir, "myagent.cmd"); + fs.writeFileSync(agent, "@rem stand-in\r\n"); + fs.mkdirSync(path.join(home, ".plan2code"), { recursive: true }); + fs.writeFileSync( + path.join(home, ".plan2code", "agents.json"), + JSON.stringify({ schema: 1, agents: [{ id: "myagent", name: "My Agent", command: agent, args: ["{prompt}"] }] }) + ); + setupEnv(t, box.dir, [{ print: { session: [{ i: "__start", agent: "myagent" }] }, exit: 0 }]); + const out = runSetupFlow(undefined, [], null); + assert.equal(out.cli.id, "myagent"); + assert.equal(out.cli.custom, true); + assert.equal(out.cli.path, agent); + assert.equal(out.cli.fullPath, true); + } finally { + for (const k of keys) { + if (saved[k] === undefined) delete process.env[k]; + else process.env[k] = saved[k]; + } + delete require.cache[launcherPath]; + require(LAUNCHER); + } +}); + +test("launcher setup: with the app installed and no agent runnable, the app opens on the folder alone", (t) => { + const box = sandbox([], { installed: "0.0.0" }); + t.after(box.cleanup); + // npm's real global folder could hold a real agent; an empty prefix keeps "none runnable" certain. + const display = process.platform === "linux" ? { DISPLAY: ":0" } : {}; + const result = box.run([], { PLAN2CODE_APP: box.appFile, npm_config_prefix: path.join(box.dir, "no-npm"), ...display }); + assert.equal(result.status, 0, result.stderr); + const appArgs = box.appArgs(); + assert.ok(appArgs, "the stand-in app was never started"); + assert.equal(appArgs[0], "--project"); + assert.equal(fs.realpathSync(appArgs[1]), fs.realpathSync(box.dir)); + assert.deepEqual(appArgs.slice(2), [], "no --agent and no --model when none can run"); + assert.match(result.stdout, /Opened the Plan2Code app/); +}); + +test("launcher setup: no agent and no display keeps the old 127 and install steps", { skip: process.platform !== "linux" }, (t) => { + const box = sandbox([], { installed: "0.0.0" }); + t.after(box.cleanup); + const result = box.run([], { PLAN2CODE_APP: box.appFile, DISPLAY: "", WAYLAND_DISPLAY: "", npm_config_prefix: path.join(box.dir, "no-npm") }); + assert.equal(result.status, 127); + assert.match(result.stderr, /none was found/); + assert.equal(box.appExists(), false); +}); diff --git a/src/launcher/plan2code.js b/src/launcher/plan2code.js index 00f1940..afe1e8a 100644 --- a/src/launcher/plan2code.js +++ b/src/launcher/plan2code.js @@ -14,12 +14,16 @@ * it asks which one, with the last pick as the default; with only one installed it uses that. * `--cli claude` / `--cli devin` / `--cli codex` skips the question. Before starting it asks * which model to run, from the curated list in models.json (shipped with every install) plus - * any models the user added in User Preferences; Enter keeps the model the CLI last used, and - * `--model` (or Codex's `-m`) skips the question. Right after the agent is chosen, when it has a + * any models the user added in User Preferences; Enter runs the model the launcher last passed + * that agent (marked `(last used)`), or keeps the agent's own last-used model when none is + * recorded, and `--model` (or Codex's `-m`) skips the question. Right after the agent is chosen, when it has a * flag for skipping permission prompts (every built-in does; a custom agent only with bypassArgs), * it asks whether to turn them off, Yes by default; `--keep-permissions` answers no, and with no * terminal to ask on they are off. Any other arguments are forwarded to the CLI. - * Self-contained, Node built-ins only: install.js copies this file to ~/.plan2code/bin/ and + * Self-contained, Node built-ins only, with one exception: the shared agent list and lookup + * rule (agents.json and agent-lookup.mjs, installed beside this file in ~/.plan2code/bin/) are + * loaded by dynamic import inside chooseCli() so the command, the web console and the desktop + * app follow one list and one PATH rule. install.js copies this file to ~/.plan2code/bin/ and * points the global shims at it. * * Custom agents: the person's own agent CLIs in ~/.plan2code/agents.json (written by User @@ -33,6 +37,11 @@ * the app on the chosen folder, agent and model instead of running the agent in this terminal. * `--classic` (never forwarded to the CLI), no app, and no display on Linux keep the flow above. * + * When no agent can run at all, a person with a window gets the console's agent-less setup + * page instead of only the printed install steps: the app on its Set up an agent card when it + * is installed, the browser page otherwise. A Start there names the agent, the lookup runs + * again, and the run carries on as if that agent had been found all along. + * * `plan2code update` starts no agent: it re-runs the npx install with `--update`, which * reinstalls whatever ~/.plan2code/install.json lists (the desktop app included). Before an agent starts, one line says * when a newer release is out (releaseNotice), read from the web console's cached release check @@ -67,8 +76,9 @@ const USER_AGENTS_FILE = path.join(os.homedir(), '.plan2code', 'agents.json'); const MODELS_MENU_COMPACT_AT = 30; const PICKER_PROMPT = 'Choose the project folder to open Plan2Code in'; const UPDATE_COMMAND = 'update'; -// The same URL src/web-console/update-check.mjs exports as REPO_URL. This file may require -// nothing but Node built-ins (and update-check.mjs is ESM), so it keeps its own copy. +// The same URL src/web-console/update-check.mjs exports as REPO_URL. Apart from the +// agent-lookup.mjs import noted in the header, this file may require nothing but Node +// built-ins (and update-check.mjs is ESM), so it keeps its own copy. const REPO_URL = 'https://github.com/jparkerweb/plan2code.git'; // The web console's cached release check, and how long it counts as fresh (update-check.mjs // CACHE_MS). PLAN2CODE_CONSOLE_HOME moves the console's files, as in files.mjs. @@ -76,6 +86,20 @@ const CONSOLE_HOME = process.env.PLAN2CODE_CONSOLE_HOME || path.join(os.homedir( const UPDATE_CACHE_FILE = path.join(CONSOLE_HOME, 'update-check.json'); const UPDATE_CACHE_MS = 60 * 60 * 1000; +// The variables the setup console must never carry: with any of them set its `open` would +// announce its page into a live app window the run is not part of (NFR-2). +const APP_ENV_VARS = [ + 'PLAN2CODE_IN_APP', + 'PLAN2CODE_APP_TOKEN', + 'PLAN2CODE_APP_ANNOUNCE', + 'PLAN2CODE_EMBED_ORIGIN', + 'PLAN2CODE_CONSOLE_SID', + 'PLAN2CODE_APP_VERSION', +]; +// An agent install is minutes, not seconds: the setup page's `wait` is re-run in 240-second +// slices (the way an agent waits), capped around four hours all told. +const SETUP_WAIT_CAP_MS = 4 * 60 * 60 * 1000; + // The escape values install.js's COLORS uses, copied: this file is installed alone and may // require nothing but Node built-ins. const ANSI = { @@ -178,23 +202,38 @@ function cliIdList(quote = '`', clis = CLIS) { return `${ids.slice(0, -1).join(', ')} or ${ids[ids.length - 1]}`; } +const OS_KEY = IS_WINDOWS ? 'win' : IS_MAC ? 'mac' : 'linux'; + +/** This OS's lead install command from an agent-list entry (or a fallback CLIS hint). */ +function leadInstall(cli) { + const lines = cli && cli.install; + if (lines && typeof lines === 'object' && !Array.isArray(lines)) { + const first = Array.isArray(lines[OS_KEY]) ? lines[OS_KEY][0] : null; + return first && first.run; + } + return typeof lines === 'string' ? lines : null; +} + /** An install command with its official page after it, unless the command is the page. */ function installLine(cli) { - return cli.installUrl && cli.installUrl !== cli.install ? `${cli.install} (${cli.installUrl})` : cli.install; + const run = leadInstall(cli); + if (!run) return cli.installUrl || ''; + return cli.installUrl && cli.installUrl !== run ? `${run} (${cli.installUrl})` : run; } /** * What to do when no agent can run: what an agent CLI is, how to get each built-in, where to add * another, and how to run the skill from an agent. `missing` names custom agents whose command - * was not found. Printed to stderr; the caller exits 127. + * was not found; `builtins` is the list the menu used (the agent list, or the CLIS fallback). + * Printed to stderr; the caller exits 127. */ -function printNoAgentGuidance(missing = []) { +function printNoAgentGuidance(missing = [], builtins = CLIS) { console.error(`${ce.red('plan2code:')} Plan2Code runs inside an AI coding agent CLI, and none was found on your PATH.`); console.error(''); console.error(ce.bright('Install one:')); - for (const cli of CLIS) console.error(` ${ce.cyan(cli.label)}: ${installLine(cli)}`); + for (const cli of builtins) console.error(` ${ce.cyan(cli.label)}: ${installLine(cli)}`); console.error(''); - console.error('Already use another agent CLI? Add it in User Preferences (web console) under Agents, then run `plan2code` again.'); + console.error('Already use another agent CLI? Add it by editing `~/.plan2code/agents.json`, then run `plan2code` again.'); for (const cli of missing) console.error(ce.yellow(`Custom agent ${cli.label}: ${cli.entry.command} not found`)); console.error(''); console.error('After installing, open a new terminal, cd into the project you want to work on and run `plan2code`.'); @@ -309,28 +348,53 @@ function checkAgentEntry(entry, ids, names) { /** * The arguments that start an entry, in order: `args` with {prompt} filled (the prompt appended * when no argument holds it), then `modelArgs` when a model is chosen, then `bypassArgs` when - * checks are off. `prompt` defaults to the entry's own; a blank one drops an argument that is - * exactly {prompt}. Plain text in, plain text out: no quoting for any shell. + * checks are off. An argument that is exactly `{bypass}` or `{model}` expands to the entry's + * `bypassArgs` / `modelArgs` in place (nothing when off / unset) and that flag is no longer + * appended at the end. `prompt` defaults to the entry's own; a blank one drops an argument that + * is exactly {prompt}. Plain text in, plain text out: no quoting for any shell. */ function buildArgs(entry, { prompt, model, bypass } = {}) { const text = prompt === undefined ? (typeof entry.prompt === 'string' ? entry.prompt : DEFAULT_PROMPT) : String(prompt ?? ''); const blank = !text.trim(); + const modelArgs = entry.modelArgs || DEFAULT_MODEL_ARGS; const out = []; let placed = false; + let hasBypass = false; + let hasModel = false; for (const arg of entry.args || []) { - if (!arg.includes('{prompt}')) { + if (arg === '{bypass}') { + hasBypass = true; + if (bypass) out.push(...(entry.bypassArgs || [])); + continue; + } + if (arg === '{model}') { + hasModel = true; + if (model) for (const m of modelArgs) out.push(m.split('{model}').join(model)); + continue; + } + if (arg.includes('{bypass}')) hasBypass = true; + if (arg.includes('{model}')) hasModel = true; + if (!arg.includes('{prompt}') && !arg.includes('{model}')) { out.push(arg); continue; } - placed = true; - if (arg === '{prompt}' && blank) continue; - out.push(arg.split('{prompt}').join(text)); + if (arg === '{prompt}' && blank) { + placed = true; + continue; + } + let next = arg; + if (next.includes('{prompt}')) { + placed = true; + next = next.split('{prompt}').join(text); + } + if (next.includes('{model}')) next = next.split('{model}').join(model || ''); + out.push(next); } if (!placed && !blank) out.push(text); - if (model) { - for (const arg of entry.modelArgs || DEFAULT_MODEL_ARGS) out.push(arg.split('{model}').join(model)); + if (model && !hasModel) { + for (const arg of modelArgs) out.push(arg.split('{model}').join(model)); } - if (bypass) out.push(...(entry.bypassArgs || [])); + if (bypass && !hasBypass) out.push(...(entry.bypassArgs || [])); return out; } @@ -639,7 +703,7 @@ function isDirectory(dir) { } } -/** `~/.plan2code/launcher.json`: `{ lastFolder, lastCli }`, either key possibly missing. */ +/** `~/.plan2code/launcher.json`: `{ lastFolder, lastCli, lastModels }`, any key possibly missing. */ function readState() { try { const state = JSON.parse(fs.readFileSync(STATE_FILE, 'utf8')); @@ -703,11 +767,80 @@ function announceCli(cli) { } /** - * The CLIs that can run, in menu order (the built-ins found on PATH, then the custom agents whose - * command resolves), and the custom agents left out because theirs does not. + * The full argv for a built-in entry of the agent list: buildArgs fills {prompt}, {bypass} and + * {model} from the entry, and the forwarded arguments are inserted ahead of the tail of + * positional arguments ("--" and the prompt), where the hard-coded CLIS placed them. A model is + * read from the `--model` forms only: Codex's `-m` forwards untouched, as it does today. */ -function runnableClis(customs) { - const installed = CLIS.map((cli) => ({ ...cli, path: findOnPath(cli.id) })).filter((cli) => cli.path); +function builtinArgs(entry, extra, { bypass } = {}) { + const model = modelFrom(extra, ''); + const forwarded = withoutModelFlag(extra); + // Each slice expands on its own: a slice without {prompt} gets a blank one so buildArgs + // appends nothing, and the tail slice takes no flags so nothing is appended there either. + const expand = (list, withFlags) => + buildArgs( + { ...entry, args: list, ...(withFlags ? {} : { bypassArgs: [], modelArgs: [] }) }, + { + prompt: list.some((arg) => arg.includes('{prompt}')) ? undefined : '', + model: withFlags ? model : undefined, + bypass: withFlags ? bypass : false, + } + ); + const raw = entry.args || []; + let tail = raw.length; + while (tail > 0 && (raw[tail - 1] === '--' || raw[tail - 1].includes('{prompt}'))) tail--; + // No {prompt} anywhere: the entry's own prompt is appended, as buildArgs does. + const promptArg = raw.some((arg) => arg.includes('{prompt}')) ? [] : [entry.prompt || DEFAULT_PROMPT].filter((text) => text && text.trim()); + return [...expand(raw.slice(0, tail), true), ...forwarded, ...expand(raw.slice(tail), false), ...promptArg]; +} + +/** An agent-list built-in entry as a CLIS-shaped entry for the menu and the launch. */ +function builtinCli(entry) { + return { + id: entry.id, + label: entry.name, + install: entry.install, + installUrl: entry.installUrl, + prompt: entry.prompt, + bypassArgs: entry.bypassArgs || [], + entry, + args: (extra, { bypass } = {}) => builtinArgs(entry, extra, { bypass }), + }; +} + +let agentLookup; // undefined: not tried yet; null: could not be loaded; the module otherwise. + +/** + * The shared lookup module, dynamically imported from beside this file (~/.plan2code/bin/) so + * the command, the console and the app follow one agent list and one PATH rule. Null when the + * file cannot be loaded; the caller falls back to the hard-coded CLIS. + */ +async function loadAgentLookup() { + if (agentLookup !== undefined) return agentLookup; + try { + agentLookup = await import(require('url').pathToFileURL(path.join(__dirname, 'agent-lookup.mjs')).href); + } catch { + agentLookup = null; + } + return agentLookup; +} + +/** + * The CLIs that can run, in menu order (the built-ins found on PATH, then the custom agents whose + * command resolves), and the custom agents left out because theirs does not. `find` is the + * shared lookup's findAgent() when the module loaded, else a plain PATH check. + */ +function runnableClis(customs, builtins = CLIS, find = null) { + const findBuiltin = + find || + ((entry) => { + const found = findOnPath(entry.command || entry.id); + return { state: found ? 'found' : 'missing', path: found }; + }); + const installed = builtins + .map((cli) => ({ ...cli, hit: findBuiltin(cli.entry || { command: cli.id }) })) + .filter((cli) => cli.hit.state === 'found' && cli.hit.path) + .map(({ hit, ...cli }) => ({ ...cli, path: hit.path })); // A custom agent whose command does not resolve is left out of the menu and named when nothing can run. const missing = []; for (const cli of customs) { @@ -718,16 +851,160 @@ function runnableClis(customs) { return { installed, missing }; } +/** + * The one-line notice for when the last-used agent is no longer runnable but another is + * (FR-13): "