Mark the last-used model in the launcher menu, grow the loading quips

AI Assisted
This commit is contained in:
jparkerweb committed 2026-11-03 03:21:20 -08:00
1 parent 4704922286
commit 1eef797dc0
3 files changed
+846 -72

No files matched your search

+351 -5
View File
@@ -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 <bin>/<cli>.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 <home>/.plan2code/bin/ beside a version.json saying X.Y.Z.
* models.json, update-check.mjs, agents.json and agent-lookup.mjs in <home>/.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/<token>/ 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);
});
+444 -62
View File
@@ -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): "<Label> (last used) was not found, using <Label>". Null when the pick stands or
* nothing can run.
*/
function lastUsedNotice(installed, all, lastCli) {
if (!lastCli || !installed.length || installed.some((cli) => cli.id === lastCli)) return null;
const gone = all.find((cli) => cli.id === lastCli);
return `${gone ? gone.label : lastCli} (last used) was not found, using ${installed[0].label}`;
}
// ---------------------------------------------------------------------------------------------
// First-run agent setup. When no agent can run, the console's agent-less `setup` workflow takes
// the place of the install steps: the command opens its page, waits on the session the way an
// agent would, and a Start there names the agent to look up again and run.
/** process.env without the app-window variables (see APP_ENV_VARS). */
function scrubbedEnv() {
const env = { ...process.env };
for (const name of APP_ENV_VARS) delete env[name];
return env;
}
/** The one JSON object a console command prints on its last line, or null. */
function parseJsonLine(text) {
const trimmed = String(text || '').trim();
if (!trimmed) return null;
try {
return JSON.parse(trimmed);
} catch {
try {
return JSON.parse(trimmed.split('\n').pop());
} catch {
return null;
}
}
}
/**
* The web console's console.mjs, in the order the desktop app's find_console_dir uses:
* PLAN2CODE_CONSOLE_DIR, the console-dir pointer under the console home (it holds the
* console's folder), then the global skill install. Null when none of them exists.
*/
function findConsoleMjs() {
const candidates = [];
if (process.env.PLAN2CODE_CONSOLE_DIR) candidates.push(path.join(process.env.PLAN2CODE_CONSOLE_DIR, 'console.mjs'));
try {
const dir = fs.readFileSync(path.join(CONSOLE_HOME, 'console-dir'), 'utf8').trim();
if (dir) candidates.push(path.join(dir, 'console.mjs'));
} catch {}
candidates.push(path.join(os.homedir(), '.agents', 'skills', 'plan2code', 'references', 'web-console', 'console.mjs'));
return candidates.find((file) => fs.existsSync(file)) || null;
}
/**
* The installed entry for the agent id a Start sent, or null: the lookup runs again because the
* point of the page was to change what can run (an install, a saved template). One retry, since
* a just-finished install can take a moment to be visible; then the caller gives up.
*/
function findStartedCli(agentId, clis, find) {
for (let attempt = 0; attempt < 2; attempt++) {
// Warnings were already printed once by chooseCli: the re-read stays quiet.
const { installed } = runnableClis(customClis(USER_AGENTS_FILE, false), clis, find);
const cli = installed.find((entry) => entry.id === agentId);
if (cli) return cli;
// A just-finished install can take a moment to be visible: the pause is
// the point of the second look.
if (attempt === 0) Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 1500);
}
return null;
}
/**
* The browser setup page: opens `console.mjs` on the agent-less setup workflow (`--for-cli`
* when one agent was asked for, so it is listed first), then waits on the session until a send
* carries `__start`, the wait ends, or the cap passes.
*
* Returns `{ cli, setup }` on a Start, where `setup` is the hand-over the caller puts in the
* agent's environment (its first `open` then moves the setup tab onto its own page rather than
* opening a browser). Null when the flow cannot run at all (no console, a failed open); the
* caller prints its usual guidance. `{ code: 127 }` when the page ran but ended without a
* Start (canceled, closed or gone, or the named agent never appeared).
*/
function runSetupFlow(forCli, clis, find) {
const consoleMjs = findConsoleMjs();
if (!consoleMjs) return null;
const env = scrubbedEnv();
const opened = spawnSync(
process.execPath,
[consoleMjs, 'open', '--workflow', 'setup', ...(forCli ? ['--for-cli', forCli] : [])],
{ encoding: 'utf8', env }
);
const out = parseJsonLine(opened && opened.stdout);
const sid = out && typeof out.sid === 'string' ? out.sid : null;
// The address the agent's first `open` hands its page to: the setup server's base URL with
// the hand-over token. Newer consoles print them as setup.url / setup.token; an older one
// leaves both derivable from the session URL's /s/<token>/ shape.
const url = out && typeof out.url === 'string' ? out.url : '';
const fromUrl = /^(https?:\/\/[^/]+)\/s\/([^/]+)\/?/.exec(url);
const announce = (out && out.setup && typeof out.setup.url === 'string' && out.setup.url) || (fromUrl && fromUrl[1]);
const token = (out && out.setup && typeof out.setup.token === 'string' && out.setup.token) || (fromUrl && fromUrl[2]);
if (!sid || !announce || !token) return null;
console.log(`Waiting for the setup page… ${c.dim(`(if no page opened, go to ${url})`)}`);
const deadline = Date.now() + SETUP_WAIT_CAP_MS;
for (;;) {
const waited = spawnSync(process.execPath, [consoleMjs, 'wait', '--session', sid, '--seconds', '240'], {
encoding: 'utf8',
env,
});
const status = waited && waited.status;
if (status === 10) {
// Still waiting: wait is meant to be re-run for the next slice.
} else if (status === 0) {
const send = parseJsonLine(waited.stdout);
// The wait prints the send's `__` actions whole; a Start is the only one this page leads to.
const start =
send &&
Array.isArray(send.session) &&
send.session.find((action) => action && action.i === '__start' && typeof action.agent === 'string');
if (start) {
const cli = findStartedCli(start.agent, clis, find);
return cli ? { cli, setup: { announce, token } } : { code: 127 };
}
// Any other send (a Quick question, say) is not an ending: keep waiting.
} else {
// 30 is the person's Cancel, 20 the page or server gone, and anything else a failure.
return { code: 127 };
}
if (Date.now() >= deadline) return { code: 127 };
}
}
/**
* Choose the CLI from those installed: the `--cli` pick, the only one installed, or a numbered
* menu defaulting to the last pick. Returns `{ cli }` with its resolved `path`, or `{ code }` (the
* exit code) after printing why none can be used.
*/
async function chooseCli(requested) {
async function chooseCli(requested, { fromShortcut } = {}) {
addSavedWindowsPath();
const customs = customClis();
const all = [...CLIS, ...customs];
const { installed, missing } = runnableClis(customs);
const lookup = await loadAgentLookup();
const list = lookup ? lookup.loadAgentList({ dir: __dirname }) : { builtins: [], templates: [], warnings: ['agent-lookup.mjs is missing or could not be loaded'] };
const listed = list.builtins.map(builtinCli);
// The agent list drives the menu; the hard-coded CLIS are the fallback when it cannot be read.
const clis = listed.length ? listed : CLIS;
if (process.stderr.isTTY) {
const suffix = listed.length ? '' : '; the built-in list is used';
for (const warning of list.warnings) console.error(`${ce.yellow('plan2code:')} ${warning}${suffix}`);
}
const all = [...clis, ...customs];
const find = lookup && listed.length ? lookup.findAgent : null;
const { installed, missing } = runnableClis(customs, clis, find);
if (requested !== undefined) {
const known = all.find((cli) => cli.id === requested);
@@ -736,6 +1013,13 @@ async function chooseCli(requested) {
return { code: 2 };
}
const found = installed.find((cli) => cli.id === requested);
// A missing built-in goes to the browser setup page when a person can see it: it lists that
// agent first and offers Start for it alone (no app redirect here, the page was asked for
// this agent). A refusal falls through to the printed steps below, never a second message.
if (!found && !known.custom && hasDisplay() && (process.stdin.isTTY || fromShortcut)) {
const settled = runSetupFlow(requested, clis, find);
if (settled && settled.cli) return settled;
}
if (!found && known.custom) {
console.error(`${ce.red('plan2code:')} Custom agent ${known.label}: ${known.entry.command} not found`);
} else if (!found) {
@@ -751,17 +1035,20 @@ async function chooseCli(requested) {
}
if (installed.length === 0) {
printNoAgentGuidance(missing);
return { code: 127 };
// Nothing printed here: launch() decides between the app's own setup, the browser setup
// page and the install steps, and prints them itself only when it declines the first two.
return { code: 127, noAgent: true, missing, clis, find };
}
const { lastCli } = readState();
// The last-used agent is gone but another works: one line, then carry on (FR-13).
const notice = lastUsedNotice(installed, all, lastCli);
if (notice && process.stdout.isTTY) console.log(c.dim(notice));
if (installed.length === 1 || !process.stdin.isTTY) {
const { lastCli } = readState();
const cli = installed.find((cli) => cli.id === lastCli) || installed[0];
if (installed.length === 1) announceCli(cli);
return { cli };
}
const { lastCli } = readState();
const defaultIndex = Math.max(0, installed.findIndex((cli) => cli.id === lastCli));
console.log(c.bright('Plan2Code opens inside an AI coding agent. Pick which one to use:'));
console.log('');
@@ -769,6 +1056,7 @@ async function chooseCli(requested) {
console.log(` ${c.cyan(`${i + 1})`)} ${cli.label}${cli.id === lastCli ? ` ${c.dim('(last used)')}` : ''}`)
);
console.log(c.dim('You can also skip this launcher and run `/plan2code` (`$plan2code` in Codex) straight from your agent.'));
console.log(c.dim('That runs outside the desktop app, without its session management or Implementation Loops.'));
for (;;) {
const answer = (await ask(`Choice ${c.green(`[${defaultIndex + 1}]`)}: `)).trim().toLowerCase();
if (!answer) return { cli: installed[defaultIndex] };
@@ -843,8 +1131,9 @@ function listModels(cli) {
}
/**
* Ask which model to run: a numbered list with Enter keeping the model the CLI last used (a
* custom agent: its own default).
* Ask which model to run: a numbered list with Enter running the model the launcher last
* passed this agent (marked and numbered like the agent menu's `(last used)`), or keeping
* the agent's own last-used model when none is recorded (a custom agent: its own default).
* A pick appends `--model <id>` to `extraArgs`. Without an interactive terminal there is
* nobody to ask, so it just says which model will be used, as it always has.
*/
@@ -864,6 +1153,13 @@ async function chooseModel(cli, extraArgs) {
const models = listModels(cli);
if (!models.length) return;
// The model the launcher last passed this agent, marked and numbered like the agent
// menu's own (last used); Enter then runs it for real. A custom agent's Enter is its
// own default model, not its last pick, so nothing is marked there.
const lastModel = cli.custom ? null : (readState().lastModels || {})[cli.id];
const lastIndex = models.findIndex((model) => model.id === lastModel);
const tag = (model) => (model.id === lastModel ? ' (last used)' : '');
console.log('');
console.log(c.bright(`Which model should ${cli.label} use?`));
if (models.length > MODELS_MENU_COMPACT_AT) {
@@ -872,16 +1168,29 @@ async function chooseModel(cli, extraArgs) {
const half = Math.ceil(models.length / 2);
for (let i = 0; i < half; i++) {
// Padded before coloring, so the escape codes never count toward the column width.
const left = ` ${i + 1}) ${models[i].id}`.padEnd(48);
const right = models[i + half] ? ` ${c.cyan(`${i + half + 1})`)} ${models[i + half].id}` : '';
console.log(left.replace(`${i + 1})`, c.cyan(`${i + 1})`)) + right);
const left = ` ${i + 1}) ${models[i].id}${tag(models[i])}`.padEnd(48);
const right = models[i + half]
? ` ${c.cyan(`${i + half + 1})`)} ${models[i + half].id}${c.dim(tag(models[i + half]))}`
: '';
console.log(left.replace(`${i + 1})`, c.cyan(`${i + 1})`)).replace(' (last used)', c.dim(' (last used)')) + right);
}
} else {
models.forEach((model, i) => console.log(!model.label ? ` ${c.cyan(`${i + 1})`)} ${model.id}` : ` ${c.cyan(`${i + 1})`)} ${model.id} — ${model.label}`));
models.forEach((model, i) =>
console.log(
(!model.label ? ` ${c.cyan(`${i + 1})`)} ${model.id}` : ` ${c.cyan(`${i + 1})`)} ${model.id} — ${model.label}`) +
c.dim(tag(model))
)
);
}
const prompt = cli.custom ? '[default]' : lastIndex >= 0 ? `[${lastIndex + 1}]` : '[keep last used]';
for (;;) {
const answer = (await ask(`Choice ${c.green(cli.custom ? '[default]' : '[keep last used]')}: `)).trim().toLowerCase();
if (!answer) return;
const answer = (await ask(`Choice ${c.green(prompt)}: `)).trim().toLowerCase();
if (!answer) {
// The marked number is the one that runs: pass it rather than trusting the
// agent's own memory of its last model.
if (lastIndex >= 0) extraArgs.push('--model', models[lastIndex].id);
return;
}
const byNumber = models[Number(answer) - 1];
const byName = models.find((model) => model.id === answer || model.label.toLowerCase() === answer);
const model = (/^\d+$/.test(answer) && byNumber) || byName;
@@ -889,7 +1198,11 @@ async function chooseModel(cli, extraArgs) {
extraArgs.push('--model', model.id);
return;
}
console.log(c.yellow(`Type a number from 1 to ${models.length}, a model name, or press Enter to keep ${keep}.`));
console.log(
c.yellow(
`Type a number from 1 to ${models.length}, a model name, or press Enter ${lastIndex >= 0 ? `for ${models[lastIndex].id}` : `to keep ${keep}`}.`
)
);
}
}
@@ -919,17 +1232,20 @@ function modelFrom(args, cliId) {
if (arg === '--model' || (cliId === 'codex' && arg === '-m')) return args[i + 1] || null;
if (arg.startsWith('--model=')) return arg.slice('--model='.length) || null;
if (cliId === 'codex' && arg.startsWith('-m=')) return arg.slice(3) || null;
if (cliId === 'codex' && arg.startsWith('-m') && arg.length > 2) return arg.slice(2);
}
return null;
}
/**
* Start the desktop app on the folder, agent and model and let it run on its own. Resolves true
* once it has started, false (after one line saying why) when it could not, so the caller can
* carry on with today's terminal flow.
* Start the desktop app on the folder, agent (when there is one) and model, and let it run on
* its own. Resolves true once it has started, false (after one line saying why) when it could
* not, so the caller can carry on with today's terminal flow.
*/
function startApp(app, { cwd, cliId, model }) {
const args = ['--project', cwd, '--agent', cliId, ...(model ? ['--model', model] : [])];
// No cliId (a run with no agent at all) opens the app on its launch card, where its own
// Set up an agent button takes over from there.
const args = ['--project', cwd, ...(cliId ? ['--agent', cliId] : []), ...(model ? ['--model', model] : [])];
return new Promise((resolve) => {
let child;
try {
@@ -1080,6 +1396,28 @@ async function pickFolder() {
return path.resolve(answer.replace(/^~(?=$|[\\/])/, os.homedir()));
}
/**
* The folder to open in, confirmed as a project: process.cwd(), or the shortcut's folder pick
* plus the same check. `{ cwd }` to carry on; `{ exit }` with the code to leave on when the
* person canceled, picked a non-folder, or declined the project check.
*/
async function projectCwd(fromShortcut) {
let cwd = process.cwd();
if (fromShortcut) {
const folder = await pickFolder();
if (!folder) return { exit: 0 };
if (!isDirectory(folder)) {
console.error(`${ce.red('plan2code:')} ${folder} is not a folder.`);
return { exit: 1 };
}
saveState({ lastFolder: folder });
cwd = folder;
console.log(`Opening Plan2Code in ${c.cyan(folder)}`);
}
if (!(await confirmProject(cwd))) return { exit: 0 };
return { cwd };
}
// Exit codes from Ctrl+C (130 on POSIX, STATUS_CONTROL_C_EXIT on Windows) are the person
// leaving on purpose, not a failure worth holding the window open for.
const INTERRUPT_EXIT_CODES = new Set([130, 0xc000013a]);
@@ -1215,32 +1553,70 @@ async function launch() {
printIntro();
await releaseNotice();
const { cli, code } = await chooseCli(requestedCli);
if (!cli) return exitWith(code, fromShortcut);
const chosen = await chooseCli(requestedCli, { fromShortcut });
let cli = chosen.cli;
// Set only for an agent the setup page produced: the hand-over its first `open` uses to move
// the waiting setup tab onto its own page (carried into the spawn's environment below).
let setup = chosen.setup;
let cwd = process.cwd();
// Whether the folder pick and project check already ran (the no-agent path runs them early:
// the app and the setup page both need the folder settled first).
let cwdSettled = false;
if (!cli) {
if (!chosen.noAgent) return exitWith(chosen.code, fromShortcut);
// No agent can run. With a window to open something in, the folder and project checks come
// first, then the app on its Set up an agent card when it is installed (preferred over the
// browser setup page), then the setup page for a person who can see it. With neither, this
// is exactly the old path: the install steps and 127.
const app = !classic && hasDisplay() ? findApp() : null;
const person = process.stdin.isTTY || fromShortcut;
if (hasDisplay() && (app || person)) {
const picked = await projectCwd(fromShortcut);
if (picked.exit !== undefined) return exitWith(picked.exit, fromShortcut);
cwd = picked.cwd;
cwdSettled = true;
if (app && (await startApp(app, { cwd }))) {
console.log(`Opened the Plan2Code app in ${c.cyan(cwd)} ${c.dim('(run `plan2code --classic` to stay in this terminal)')}`);
return process.exit(0);
}
if (person) {
const settled = runSetupFlow(undefined, chosen.clis, chosen.find);
if (settled && settled.cli) {
cli = settled.cli;
setup = settled.setup;
}
}
}
if (!cli) {
printNoAgentGuidance(chosen.missing, chosen.clis);
return exitWith(127, fromShortcut);
}
}
saveState({ lastCli: cli.id });
// The app has its own permission tick on its launch card, so the question is only for the
// terminal flow (asked late if the app fails to start).
const app = !classic && hasDisplay() ? findApp() : null;
// terminal flow (asked late if the app fails to start). An agent the setup page started runs
// in this terminal even with the app installed: the waiting setup tab is owed the hand-over.
const app = !setup && !classic && hasDisplay() ? findApp() : null;
let bypass = app ? null : await chooseBypass(cli, keepPermissions);
let cwd = process.cwd();
if (fromShortcut) {
const folder = await pickFolder();
if (!folder) return process.exit(0);
if (!isDirectory(folder)) {
console.error(`${ce.red('plan2code:')} ${folder} is not a folder.`);
return exitWith(1, fromShortcut);
}
saveState({ lastFolder: folder });
cwd = folder;
console.log(`Opening Plan2Code in ${c.cyan(folder)}`);
if (!cwdSettled) {
const picked = await projectCwd(fromShortcut);
if (picked.exit !== undefined) return exitWith(picked.exit, fromShortcut);
cwd = picked.cwd;
}
if (!(await confirmProject(cwd))) return process.exit(0);
if (!hasModelFlag(extraArgs, cli.id)) await chooseModel(cli, extraArgs);
// Remember the model this run passes, per agent, so the next menu can mark
// and number it like the agent menu marks its last used.
const launchedModel = modelFrom(extraArgs, cli.id);
if (launchedModel) {
const { lastModels } = readState();
saveState({ lastModels: { ...(lastModels || {}), [cli.id]: launchedModel } });
}
// The app takes over from here when it is installed, a window can open and --classic is absent.
if (app && (await startApp(app, { cwd, cliId: cli.id, model: modelFrom(extraArgs, cli.id) }))) {
console.log(`Opened the Plan2Code app in ${c.cyan(cwd)} ${c.dim('(run `plan2code --classic` to stay in this terminal)')}`);
@@ -1250,6 +1626,12 @@ async function launch() {
const args = cli.args(extraArgs, { bypass });
const options = { cwd, stdio: 'inherit', ...(cli.fullPath ? { fromFullPath: true } : {}) };
// An agent the setup page started tells the waiting setup session where its own page is
// instead of opening a browser: its `open` posts the URL to the announce address under the
// hand-over token, and the tab moves there (FR-20).
if (setup) {
options.env = { ...process.env, PLAN2CODE_SETUP_ANNOUNCE: setup.announce, PLAN2CODE_SETUP_TOKEN: setup.token };
}
const child = spawnCommand(cli.path, args, options);
@@ -1277,4 +1659,4 @@ async function launch() {
// Run only when invoked directly; requiring the file (the tests do) is load-only.
if (require.main === module) launch();
module.exports = { normalizeAgents, buildArgs, parseRegQueryValue, expandPercentVars, addSavedWindowsPath, customClis, resolveCustomCommand, runnableClis, listModels, mergeModels, modelEntries, renderBanner, paint, hasModelFlag, compareVersions, modelFrom, hasDisplay, looksLikeProject };
module.exports = { normalizeAgents, buildArgs, parseRegQueryValue, expandPercentVars, addSavedWindowsPath, customClis, resolveCustomCommand, runnableClis, listModels, mergeModels, modelEntries, renderBanner, paint, hasModelFlag, compareVersions, modelFrom, hasDisplay, looksLikeProject, lastUsedNotice, leadInstall, installLine, findConsoleMjs, runSetupFlow, projectCwd };
+51 -5
View File
@@ -17,6 +17,30 @@ const LAUNCH_QUIPS = [
"Consulting the map…",
"Putting the kettle on…",
"Rolling out the checklist…",
"Tying Planny's laces…",
"Winding up the star…",
"Sharpening the pencils…",
"Untangling the fairy lights…",
"Bribing the progress bar…",
"Warming up the ink…",
"Aligning the stars…",
"Blowing the dust off the plans…",
"Locating the missing semicolon…",
"Feeding the hamster…",
"Buttering the toast…",
"Spinning up the idea mill…",
"Plumping the cushions…",
"Turning the whimsy dial…",
"Oiling the gears…",
"Counting the paperclips…",
"Practicing the victory wave…",
"Rolling a fresh D20…",
"Peeling the shrink-wrap…",
"Summoning the right incantation…",
"Rounding up the ideas…",
"Stretching before the sprint…",
"Packing a tiny lunch…",
"Fluffing the checklist…",
];
let launchTicker = null;
@@ -121,6 +145,26 @@ const START_QUIPS = [
"Polishing the star…",
"Laying out the good pens…",
"Consulting the map…",
"Skimming the table of contents…",
"Following the paper trail…",
"Reading the footnotes first…",
"Taking attendance of the files…",
"Decoding the ancient scrolls…",
"Listening for plot holes…",
"Sharpening the highlighters…",
"Counting the moving parts…",
"Making friends with the codebase…",
"Checking under the hood…",
"Peeking behind the curtain…",
"Dusting the shelves…",
"Cross-referencing the references…",
"Interviewing the git history…",
"Warming up the magnifying glass…",
"Lacing up the walking boots…",
"Consulting the elder scrolls…",
"Unrolling the blueprints…",
"Sniffing out the good bits…",
"Asking the comments what they meant…",
];
function startingElapsed() {
@@ -217,12 +261,14 @@ export function renderWaiting(main) {
pane.appendChild(el("p", null, ask));
return;
}
// "Task" and the tasks tab's own name both open that tab, so the pointer is
// one click from what it points at. A build posts it as `phase` ("Phase N
// tasks"); a quick task ticks the steps of its `plan` in place (an older
// agent may still post a separate `tasks` tab). A skill with no task list
// (Document, Plan) has nothing live to follow, so it gets no pointer at all.
// "Task" and the tab's own name both open that tab, so the pointer is one
// click from what it points at. A build posts `phasefile` ("phase-N.md"):
// the whole phase file, task list included. An older agent may still post
// a separate `phase` or `tasks` list, and a quick task ticks the steps of
// its `plan` in place. A skill with no task list (Document, Plan) has
// nothing live to follow, so it gets no pointer at all.
const list =
docs().find((d) => d.id === "phasefile") ||
docs().find((d) => d.id === "phase") ||
docs().find((d) => d.id === "tasks") ||
(page.S.workflow === "quick-task" && docs().find((d) => d.id === "plan")) ||