@phren/cli 0.3.4 → 0.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bridge/agent-hooks.d.ts +6 -0
- package/dist/bridge/agent-hooks.js +63 -23
- package/dist/bridge/canary.d.ts +4 -0
- package/dist/bridge/canary.js +17 -4
- package/dist/bridge/changes.d.ts +7 -0
- package/dist/bridge/changes.js +6 -1
- package/dist/bridge/client.js +2 -1
- package/dist/bridge/command.js +7 -5
- package/dist/bridge/conductor-group.d.ts +1 -0
- package/dist/bridge/conductor-group.js +4 -3
- package/dist/bridge/dispatch-command.js +2 -2
- package/dist/bridge/fanouts.js +6 -2
- package/dist/bridge/hand-off.d.ts +1 -0
- package/dist/bridge/hand-off.js +3 -2
- package/dist/bridge/harness-status.d.ts +12 -0
- package/dist/bridge/harness-status.js +153 -0
- package/dist/bridge/health.d.ts +18 -0
- package/dist/bridge/health.js +30 -4
- package/dist/bridge/herdr.d.ts +6 -0
- package/dist/bridge/herdr.js +62 -8
- package/dist/bridge/install.js +4 -0
- package/dist/bridge/model-switch.js +5 -4
- package/dist/bridge/models.js +3 -2
- package/dist/bridge/pane-status.d.ts +37 -0
- package/dist/bridge/pane-status.js +139 -0
- package/dist/bridge/pane-text.d.ts +3 -11
- package/dist/bridge/pane-text.js +9 -16
- package/dist/bridge/peers.js +9 -6
- package/dist/bridge/prompt-once.d.ts +26 -0
- package/dist/bridge/prompt-once.js +69 -0
- package/dist/bridge/protocol.d.ts +10 -0
- package/dist/bridge/protocol.js +10 -0
- package/dist/bridge/schedule-format.d.ts +2 -0
- package/dist/bridge/schedule-launch.d.ts +10 -1
- package/dist/bridge/schedule-launch.js +29 -3
- package/dist/bridge/schedule-watch.js +1 -1
- package/dist/bridge/schedules.d.ts +10 -0
- package/dist/bridge/schedules.js +40 -0
- package/dist/bridge/server-launch.js +12 -10
- package/dist/bridge/server-pane-routes.js +23 -9
- package/dist/bridge/server-routes.d.ts +1 -0
- package/dist/bridge/server-routes.js +1 -1
- package/dist/bridge/session-servers.js +5 -6
- package/dist/bridge/side-questions.js +6 -5
- package/dist/bridge/terminal-choice.d.ts +11 -0
- package/dist/bridge/terminal-choice.js +37 -0
- package/dist/bridge/terminal-herdr.d.ts +5 -0
- package/dist/bridge/terminal-herdr.js +57 -0
- package/dist/bridge/terminal-tmux.d.ts +111 -0
- package/dist/bridge/terminal-tmux.js +549 -0
- package/dist/bridge/terminal.d.ts +119 -0
- package/dist/bridge/terminal.js +66 -0
- package/dist/bridge/transcript-claude.d.ts +6 -0
- package/dist/bridge/transcript-claude.js +20 -0
- package/dist/bridge/transcript-opencode.js +1 -1
- package/dist/bridge/transcript-preview.js +1 -1
- package/dist/bridge/transport.js +10 -4
- package/dist/bridge-hook.mjs +4800 -3397
- package/dist/cli-registry.js +7 -0
- package/dist/entrypoint.d.ts +2 -0
- package/dist/entrypoint.js +5 -1
- package/dist/index.js +8 -2
- package/dist/init/init.js +4 -0
- package/dist/link/doctor.js +33 -30
- package/dist/mcp/plugin-mode.d.ts +16 -0
- package/dist/mcp/plugin-mode.js +106 -0
- package/dist/modules/agent-package.d.ts +35 -0
- package/dist/modules/agent-package.js +133 -0
- package/dist/modules/registry.js +2 -2
- package/dist/session/utils.js +1 -1
- package/dist/tools/dispatch.js +2 -2
- package/package.json +1 -1
- package/plugins/opencode/phren-transcript.js +97 -6
- package/starter/global/skills/phren-consolidate/SKILL.md +1 -1
- package/starter/global/skills/phren-discover/SKILL.md +1 -1
- package/starter/global/skills/phren-init/SKILL.md +6 -8
- package/starter/global/skills/phren-profiles/SKILL.md +1 -1
- package/starter/global/skills/phren-summarize/SKILL.md +1 -1
- package/starter/global/skills/phren-sync/SKILL.md +1 -1
package/dist/cli-registry.js
CHANGED
|
@@ -318,6 +318,13 @@ export const REGISTRY = [
|
|
|
318
318
|
],
|
|
319
319
|
run: async (args, ctx) => (await (await import("./modules/code-package.js")).requireCodePackage(ctx.phrenPath())).runCodeCommand(args, ctx),
|
|
320
320
|
},
|
|
321
|
+
{
|
|
322
|
+
name: "agent",
|
|
323
|
+
topic: "core",
|
|
324
|
+
usage: "phren agent [options] [task]",
|
|
325
|
+
summary: "Run phren's coding agent (needs @phren/agent); phren agent --help lists its options",
|
|
326
|
+
run: async (args) => (await import("./modules/agent-package.js")).runAgentCommand(args),
|
|
327
|
+
},
|
|
321
328
|
{
|
|
322
329
|
name: "shell",
|
|
323
330
|
topic: "core",
|
package/dist/entrypoint.d.ts
CHANGED
package/dist/entrypoint.js
CHANGED
|
@@ -30,6 +30,9 @@ export function resolveTopLevelInvocation(argv) {
|
|
|
30
30
|
if (looksLikePhrenRootArg(positional)) {
|
|
31
31
|
return { kind: "mcp", phrenArg: positional };
|
|
32
32
|
}
|
|
33
|
+
if (argvCommand === "mcp" && argv.length === 1) {
|
|
34
|
+
return { kind: "mcp-serve" };
|
|
35
|
+
}
|
|
33
36
|
if (argvCommand === "--help" || argvCommand === "-h") {
|
|
34
37
|
// `phren --help all` reads like `phren help all`, so it does the same.
|
|
35
38
|
return argv.length > 1 ? { kind: "manage", argv } : { kind: "help" };
|
|
@@ -174,7 +177,8 @@ export async function runTopLevelCommand(argv, opts = {}) {
|
|
|
174
177
|
ensureFtsCacheRootPrivate();
|
|
175
178
|
// Intercept before any handler runs - in particular before namespace
|
|
176
179
|
// handlers that used to print their own per-subcommand help text.
|
|
177
|
-
|
|
180
|
+
// `phren agent` hands every argument to the agent, which prints its own help.
|
|
181
|
+
if (cmd.name !== "agent" && (argv.includes("--help") || argv.includes("-h")) || (argv.length === 1 && ["maintain", "config", "bridge"].includes(cmd.name))) {
|
|
178
182
|
const out = formatCommand(cmd.name, snapshot);
|
|
179
183
|
if (out)
|
|
180
184
|
console.log(out);
|
package/dist/index.js
CHANGED
|
@@ -6,6 +6,7 @@ import { log as structuredLog, logger } from "./logger.js";
|
|
|
6
6
|
import { errorMessage } from "./utils.js";
|
|
7
7
|
import { nonInteractiveGitEnv } from "./utils-helpers.js";
|
|
8
8
|
import { printIntegratedHelp, printIntegratedVersion, resolveTopLevelInvocation, runTopLevelCommand, } from "./entrypoint.js";
|
|
9
|
+
import { pluginSetupReason, runPluginSetupServer } from "./mcp/plugin-mode.js";
|
|
9
10
|
// NOTE: the MCP-server-only module graph (MCP SDK, FTS indexer, tool registries,
|
|
10
11
|
// startup-embedding, custom-hook engine) is intentionally NOT imported here. It is
|
|
11
12
|
// dynamically imported inside main() so that top-level commands — hook-prompt,
|
|
@@ -30,7 +31,12 @@ if (invocation.kind === "manage") {
|
|
|
30
31
|
}
|
|
31
32
|
process.exit(process.exitCode ?? 0);
|
|
32
33
|
}
|
|
33
|
-
|
|
34
|
+
// `phren mcp` (the Claude Code plugin) with no store yet, or with `phren init`'s
|
|
35
|
+
// own server already registered, serves a small setup / empty server instead.
|
|
36
|
+
const pluginSetup = invocation.kind === "mcp-serve" ? pluginSetupReason() : null;
|
|
37
|
+
const phrenPath = pluginSetup
|
|
38
|
+
? ""
|
|
39
|
+
: findPhrenPathWithArg(invocation.kind === "mcp" ? invocation.phrenArg : undefined);
|
|
34
40
|
const STALE_LOCK_MS = 120_000; // 2 min — slightly above EXEC_TIMEOUT_MS (30s) to avoid blocking healthy writers
|
|
35
41
|
function cleanStaleLocks(phrenPath) {
|
|
36
42
|
const dir = runtimeDir(phrenPath);
|
|
@@ -342,7 +348,7 @@ async function main() {
|
|
|
342
348
|
process.on("SIGTERM", () => void shutdown("SIGTERM"));
|
|
343
349
|
process.on("SIGINT", () => void shutdown("SIGINT"));
|
|
344
350
|
}
|
|
345
|
-
main().catch((err) => {
|
|
351
|
+
(pluginSetup ? runPluginSetupServer(pluginSetup) : main()).catch((err) => {
|
|
346
352
|
console.error("Failed to start phren-mcp:", err);
|
|
347
353
|
process.exit(1);
|
|
348
354
|
});
|
package/dist/init/init.js
CHANGED
|
@@ -703,6 +703,10 @@ export async function runInit(opts = {}) {
|
|
|
703
703
|
log(`\nNext:`);
|
|
704
704
|
let step = 1;
|
|
705
705
|
log(` ${step++}. Start a new agent session (Claude, Codex, Copilot…) in a project. Phren loads its memory automatically.`);
|
|
706
|
+
const { agentInstalled } = await import("../modules/agent-package.js");
|
|
707
|
+
log(agentInstalled()
|
|
708
|
+
? ` Or use phren's own coding agent: phren agent -i`
|
|
709
|
+
: ` phren's own coding agent is optional: npm install -g @phren/agent, then phren agent -i`);
|
|
706
710
|
if (!paired)
|
|
707
711
|
log(` ${step++}. Connect your phone any time: phren pair`);
|
|
708
712
|
if (!synced) {
|
package/dist/link/doctor.js
CHANGED
|
@@ -670,6 +670,38 @@ export async function runDoctor(phrenPath, fix = false, checkData = false, confi
|
|
|
670
670
|
detail: `index build/query failed: ${errorMessage(err)}`,
|
|
671
671
|
});
|
|
672
672
|
}
|
|
673
|
+
// Repair before the hook and wrapper checks below, so they report what
|
|
674
|
+
// --fix left behind: the relink is what writes codex.json, the Copilot and
|
|
675
|
+
// Cursor hook files and the wrappers.
|
|
676
|
+
if (fix) {
|
|
677
|
+
const repaired = repairPreexistingInstall(phrenPath);
|
|
678
|
+
const details = [];
|
|
679
|
+
if (repaired.removedLegacyProjects > 0)
|
|
680
|
+
details.push(`removed ${repaired.removedLegacyProjects} legacy sample profile entries`);
|
|
681
|
+
if (repaired.createdContextFile)
|
|
682
|
+
details.push("recreated ~/.phren-context.md");
|
|
683
|
+
if (repaired.createdRootMemory)
|
|
684
|
+
details.push("recreated generated MEMORY.md");
|
|
685
|
+
if (details.length === 0)
|
|
686
|
+
details.push("baseline repair complete");
|
|
687
|
+
checks.push({ name: "baseline-repair", ok: true, detail: details.join("; ") });
|
|
688
|
+
}
|
|
689
|
+
if (fix && invalidProjectDirs.length > 0) {
|
|
690
|
+
const migration = migrateInvalidProjectNames(phrenPath);
|
|
691
|
+
const unresolved = migration.outcomes.filter((o) => o.action !== "renamed");
|
|
692
|
+
checks.push({
|
|
693
|
+
name: "project-names-migrate",
|
|
694
|
+
ok: unresolved.length === 0,
|
|
695
|
+
detail: formatMigrationSummary(migration),
|
|
696
|
+
});
|
|
697
|
+
}
|
|
698
|
+
if (fix && profile && profileFile) {
|
|
699
|
+
await runLink(phrenPath, { machine, profile });
|
|
700
|
+
checks.push({ name: "self-heal", ok: true, detail: "relinked hooks, symlinks, context, memory pointers" });
|
|
701
|
+
}
|
|
702
|
+
else if (fix) {
|
|
703
|
+
checks.push({ name: "self-heal", ok: false, detail: "relink blocked: machine/profile not fully configured" });
|
|
704
|
+
}
|
|
673
705
|
const detected = detectInstalledTools();
|
|
674
706
|
if (detected.has("copilot")) {
|
|
675
707
|
const copilotHooks = hookConfigPath("copilot", phrenPath);
|
|
@@ -721,36 +753,7 @@ export async function runDoctor(phrenPath, fix = false, checkData = false, confi
|
|
|
721
753
|
checks.push(wrapperCheck(tool, wrapperState(tool)));
|
|
722
754
|
}
|
|
723
755
|
checks.push(wrapperCheck("phren", wrapperState("phren")));
|
|
724
|
-
if (fix) {
|
|
725
|
-
const repaired = repairPreexistingInstall(phrenPath);
|
|
726
|
-
const details = [];
|
|
727
|
-
if (repaired.removedLegacyProjects > 0)
|
|
728
|
-
details.push(`removed ${repaired.removedLegacyProjects} legacy sample profile entries`);
|
|
729
|
-
if (repaired.createdContextFile)
|
|
730
|
-
details.push("recreated ~/.phren-context.md");
|
|
731
|
-
if (repaired.createdRootMemory)
|
|
732
|
-
details.push("recreated generated MEMORY.md");
|
|
733
|
-
if (details.length === 0)
|
|
734
|
-
details.push("baseline repair complete");
|
|
735
|
-
checks.push({ name: "baseline-repair", ok: true, detail: details.join("; ") });
|
|
736
|
-
}
|
|
737
|
-
if (fix && invalidProjectDirs.length > 0) {
|
|
738
|
-
const migration = migrateInvalidProjectNames(phrenPath);
|
|
739
|
-
const unresolved = migration.outcomes.filter((o) => o.action !== "renamed");
|
|
740
|
-
checks.push({
|
|
741
|
-
name: "project-names-migrate",
|
|
742
|
-
ok: unresolved.length === 0,
|
|
743
|
-
detail: formatMigrationSummary(migration),
|
|
744
|
-
});
|
|
745
|
-
}
|
|
746
|
-
if (fix && profile && profileFile) {
|
|
747
|
-
await runLink(phrenPath, { machine, profile });
|
|
748
|
-
checks.push({ name: "self-heal", ok: true, detail: "relinked hooks, symlinks, context, memory pointers" });
|
|
749
|
-
}
|
|
750
|
-
else if (fix) {
|
|
751
|
-
checks.push({ name: "self-heal", ok: false, detail: "relink blocked: machine/profile not fully configured" });
|
|
752
|
-
}
|
|
753
|
-
else {
|
|
756
|
+
if (!fix) {
|
|
754
757
|
// Read-only mode: just check if hook configs exist, don't write anything
|
|
755
758
|
const detectedTools = detected;
|
|
756
759
|
const hookChecks = [];
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type PluginSetupReason = "no-store" | "stand-down";
|
|
2
|
+
/**
|
|
3
|
+
* True when Claude Code will also load a user-level `phren` server that
|
|
4
|
+
* `phren init` registered. Claude Code reads user MCP servers from
|
|
5
|
+
* `.claude.json` (inside CLAUDE_CONFIG_DIR when set), not from settings.json,
|
|
6
|
+
* so an entry only in settings.json does not count.
|
|
7
|
+
*/
|
|
8
|
+
export declare function userLevelPhrenMcpConfigured(env?: NodeJS.ProcessEnv): boolean;
|
|
9
|
+
/** Why `phren mcp` should not start the full server, or null when it should. */
|
|
10
|
+
export declare function pluginSetupReason(env?: NodeJS.ProcessEnv): PluginSetupReason | null;
|
|
11
|
+
export declare function setupMessage(): string;
|
|
12
|
+
export declare function runInitForPlugin(): {
|
|
13
|
+
ok: boolean;
|
|
14
|
+
output: string;
|
|
15
|
+
};
|
|
16
|
+
export declare function runPluginSetupServer(reason: PluginSetupReason): Promise<void>;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `phren mcp`: the MCP server as the Claude Code plugin starts it.
|
|
3
|
+
*
|
|
4
|
+
* The plugin cannot pass a store path (it does not know one), so `phren mcp`
|
|
5
|
+
* finds the store itself. Two cases then need something other than the full
|
|
6
|
+
* server:
|
|
7
|
+
*
|
|
8
|
+
* - no store yet (a plugin-only user on first run): a small server whose one
|
|
9
|
+
* tool explains setup and, once the user agrees, runs `phren init --yes`;
|
|
10
|
+
* - `phren init` already registered its own `phren` server for Claude Code and
|
|
11
|
+
* the plugin launched this one (PHREN_MCP_OWNER=plugin): an empty server, so
|
|
12
|
+
* the user does not see every phren tool twice.
|
|
13
|
+
*/
|
|
14
|
+
import * as fs from "fs";
|
|
15
|
+
import * as path from "path";
|
|
16
|
+
import { spawnSync } from "child_process";
|
|
17
|
+
import { findPhrenPath, homePath } from "../phren-paths.js";
|
|
18
|
+
function hasPhrenServer(filePath) {
|
|
19
|
+
try {
|
|
20
|
+
const data = JSON.parse(fs.readFileSync(filePath, "utf8"));
|
|
21
|
+
return Boolean(data?.mcpServers && typeof data.mcpServers === "object" && data.mcpServers.phren);
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* True when Claude Code will also load a user-level `phren` server that
|
|
29
|
+
* `phren init` registered. Claude Code reads user MCP servers from
|
|
30
|
+
* `.claude.json` (inside CLAUDE_CONFIG_DIR when set), not from settings.json,
|
|
31
|
+
* so an entry only in settings.json does not count.
|
|
32
|
+
*/
|
|
33
|
+
export function userLevelPhrenMcpConfigured(env = process.env) {
|
|
34
|
+
const configDir = env.CLAUDE_CONFIG_DIR?.trim();
|
|
35
|
+
return hasPhrenServer(configDir ? path.join(configDir, ".claude.json") : homePath(".claude.json"));
|
|
36
|
+
}
|
|
37
|
+
/** Why `phren mcp` should not start the full server, or null when it should. */
|
|
38
|
+
export function pluginSetupReason(env = process.env) {
|
|
39
|
+
if (env.PHREN_MCP_OWNER === "plugin" && userLevelPhrenMcpConfigured(env))
|
|
40
|
+
return "stand-down";
|
|
41
|
+
return findPhrenPath() ? null : "no-store";
|
|
42
|
+
}
|
|
43
|
+
const NO_STORE_INSTRUCTIONS = "phren gives this agent memory across sessions (findings, tasks, project context), but it has no store on this machine yet. " +
|
|
44
|
+
"When the user asks about phren or memory, call phren_setup to explain setup, and only call it with confirm=true after they agree.";
|
|
45
|
+
const STAND_DOWN_INSTRUCTIONS = "phren is already registered as its own MCP server by `phren init`; use that server's tools. This plugin copy stays empty so nothing appears twice.";
|
|
46
|
+
export function setupMessage() {
|
|
47
|
+
return [
|
|
48
|
+
"phren has no memory store on this machine yet.",
|
|
49
|
+
"",
|
|
50
|
+
"Setup runs `phren init --yes`, which:",
|
|
51
|
+
"- creates the store at ~/.phren (a local git repo; nothing leaves the machine unless you add a remote)",
|
|
52
|
+
"- registers phren's MCP server and memory hooks in ~/.claude/settings.json (the plugin's own copies then stand down)",
|
|
53
|
+
"- wires any other agents it detects (Codex, Copilot CLI, Cursor, VS Code)",
|
|
54
|
+
"",
|
|
55
|
+
"Ask the user before running it. Afterwards, Claude Code needs a restart to load the memory tools.",
|
|
56
|
+
].join("\n");
|
|
57
|
+
}
|
|
58
|
+
export function runInitForPlugin() {
|
|
59
|
+
const entry = process.argv[1];
|
|
60
|
+
const result = spawnSync(process.execPath, [entry, "init", "--yes"], {
|
|
61
|
+
encoding: "utf8",
|
|
62
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
63
|
+
timeout: 180_000,
|
|
64
|
+
env: { ...process.env, PHREN_MCP_OWNER: "" },
|
|
65
|
+
});
|
|
66
|
+
const output = `${result.stdout ?? ""}${result.stderr ?? ""}`.trim();
|
|
67
|
+
const tail = output.split("\n").slice(-25).join("\n");
|
|
68
|
+
return { ok: result.status === 0, output: tail || (result.error ? String(result.error) : "") };
|
|
69
|
+
}
|
|
70
|
+
export async function runPluginSetupServer(reason) {
|
|
71
|
+
const [{ McpServer }, { StdioServerTransport }, { z }, { VERSION }] = await Promise.all([
|
|
72
|
+
import("@modelcontextprotocol/sdk/server/mcp.js"),
|
|
73
|
+
import("@modelcontextprotocol/sdk/server/stdio.js"),
|
|
74
|
+
import("zod"),
|
|
75
|
+
import("../package-metadata.js"),
|
|
76
|
+
]);
|
|
77
|
+
const server = new McpServer({ name: "phren-mcp", version: VERSION }, { instructions: reason === "no-store" ? NO_STORE_INSTRUCTIONS : STAND_DOWN_INSTRUCTIONS });
|
|
78
|
+
if (reason === "no-store") {
|
|
79
|
+
server.registerTool("phren_setup", {
|
|
80
|
+
title: "◆ phren · set up memory",
|
|
81
|
+
description: "phren has no memory store on this machine yet. Call without arguments to get what setup does; " +
|
|
82
|
+
"call with confirm=true, only after the user agrees, to run `phren init --yes` (creates ~/.phren and wires Claude Code). Restart Claude Code afterwards.",
|
|
83
|
+
inputSchema: z.object({
|
|
84
|
+
confirm: z.boolean().optional().describe("true runs setup now. Only after the user agreed."),
|
|
85
|
+
}),
|
|
86
|
+
}, async ({ confirm }) => {
|
|
87
|
+
if (!confirm)
|
|
88
|
+
return { content: [{ type: "text", text: setupMessage() }] };
|
|
89
|
+
const result = runInitForPlugin();
|
|
90
|
+
const next = result.ok
|
|
91
|
+
? "\n\nphren is set up. Restart Claude Code so its memory tools and hooks load."
|
|
92
|
+
: "\n\nSetup failed. The user can run `npx -y @phren/cli init` in a terminal instead.";
|
|
93
|
+
return { content: [{ type: "text", text: `${result.output}${next}` }], isError: !result.ok };
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
// An empty but well-formed tool list, so clients that list tools
|
|
98
|
+
// regardless of capabilities get [] rather than "method not found".
|
|
99
|
+
const { ListToolsRequestSchema } = await import("@modelcontextprotocol/sdk/types.js");
|
|
100
|
+
server.server.registerCapabilities({ tools: {} });
|
|
101
|
+
server.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [] }));
|
|
102
|
+
}
|
|
103
|
+
const transport = new StdioServerTransport();
|
|
104
|
+
await server.connect(transport);
|
|
105
|
+
console.error(`phren-mcp running in plugin ${reason} mode`);
|
|
106
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `phren agent` launches phren's coding agent, which ships separately as
|
|
3
|
+
* `@phren/agent` (it brings ink and React, which the CLI must not carry). Like
|
|
4
|
+
* `@phren/code` it is optional: nothing here is imported until the command
|
|
5
|
+
* runs, and a missing package is a one-line hint, not a stack trace.
|
|
6
|
+
*/
|
|
7
|
+
export declare const AGENT_PACKAGE_HINT = "phren agent needs @phren/agent: run npm install -g @phren/agent";
|
|
8
|
+
export interface AgentPackage {
|
|
9
|
+
directory: string;
|
|
10
|
+
bin: string;
|
|
11
|
+
version?: string;
|
|
12
|
+
source: string;
|
|
13
|
+
}
|
|
14
|
+
/** The installed package in `directory`, if it is @phren/agent and its binary is built. */
|
|
15
|
+
export declare function agentPackageAt(directory: string, source: string): AgentPackage | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* Resolution order, first hit wins: an explicit directory
|
|
18
|
+
* (`PHREN_AGENT_PACKAGE`), a package Node resolves next to this CLI, this
|
|
19
|
+
* repository's own packages/agent in a workspace checkout, then npm's global
|
|
20
|
+
* root. `global: false` skips the npm call for quick probes such as init's.
|
|
21
|
+
*/
|
|
22
|
+
export declare function findAgentPackage(options?: {
|
|
23
|
+
global?: boolean;
|
|
24
|
+
}): AgentPackage | undefined;
|
|
25
|
+
/** Whether the agent can start here, from a local package or a `phren-agent`
|
|
26
|
+
* on PATH. Cheap enough for init's summary: no npm call. */
|
|
27
|
+
export declare function agentInstalled(): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Run the agent in a child process with this terminal. The child's argv[0] is
|
|
30
|
+
* `phren-agent`, so the tmux provider and anything else that reads the process
|
|
31
|
+
* table sees the same agent whichever entry point started it; the environment
|
|
32
|
+
* (HERDR_PANE_ID, PHREN_PATH) passes through unchanged, which is what binds a
|
|
33
|
+
* Herdr pane to the session's event log.
|
|
34
|
+
*/
|
|
35
|
+
export declare function runAgentCommand(args: string[]): Promise<number>;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { execFileSync, spawn } from "node:child_process";
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
/**
|
|
7
|
+
* `phren agent` launches phren's coding agent, which ships separately as
|
|
8
|
+
* `@phren/agent` (it brings ink and React, which the CLI must not carry). Like
|
|
9
|
+
* `@phren/code` it is optional: nothing here is imported until the command
|
|
10
|
+
* runs, and a missing package is a one-line hint, not a stack trace.
|
|
11
|
+
*/
|
|
12
|
+
export const AGENT_PACKAGE_HINT = "phren agent needs @phren/agent: run npm install -g @phren/agent";
|
|
13
|
+
function readManifest(directory) {
|
|
14
|
+
try {
|
|
15
|
+
return JSON.parse(fs.readFileSync(path.join(directory, "package.json"), "utf8"));
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/** The installed package in `directory`, if it is @phren/agent and its binary is built. */
|
|
22
|
+
export function agentPackageAt(directory, source) {
|
|
23
|
+
const manifest = readManifest(directory);
|
|
24
|
+
if (manifest?.name !== "@phren/agent")
|
|
25
|
+
return undefined;
|
|
26
|
+
const relative = typeof manifest.bin === "string" ? manifest.bin : manifest.bin?.["phren-agent"];
|
|
27
|
+
if (!relative)
|
|
28
|
+
return undefined;
|
|
29
|
+
const bin = path.join(directory, relative);
|
|
30
|
+
return fs.existsSync(bin) ? { directory, bin, version: manifest.version, source } : undefined;
|
|
31
|
+
}
|
|
32
|
+
/** This repository checked out as a workspace, if its packages/agent is present. */
|
|
33
|
+
function workspaceDirectory() {
|
|
34
|
+
let directory = path.dirname(fileURLToPath(import.meta.url));
|
|
35
|
+
for (let depth = 0; depth < 8; depth++) {
|
|
36
|
+
const candidate = path.join(directory, "packages", "agent");
|
|
37
|
+
if (fs.existsSync(path.join(candidate, "package.json")))
|
|
38
|
+
return candidate;
|
|
39
|
+
const parent = path.dirname(directory);
|
|
40
|
+
if (parent === directory)
|
|
41
|
+
break;
|
|
42
|
+
directory = parent;
|
|
43
|
+
}
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
function resolvedDirectory() {
|
|
47
|
+
try {
|
|
48
|
+
return path.dirname(createRequire(import.meta.url).resolve("@phren/agent/package.json"));
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function globalDirectory() {
|
|
55
|
+
try {
|
|
56
|
+
const options = { encoding: "utf8", timeout: 10_000, stdio: ["ignore", "pipe", "ignore"] };
|
|
57
|
+
const root = (process.platform === "win32"
|
|
58
|
+
? execFileSync("npm.cmd", ["root", "-g"], { ...options, shell: true })
|
|
59
|
+
: execFileSync("npm", ["root", "-g"], options)).trim();
|
|
60
|
+
return root ? path.join(root, "@phren", "agent") : undefined;
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Resolution order, first hit wins: an explicit directory
|
|
68
|
+
* (`PHREN_AGENT_PACKAGE`), a package Node resolves next to this CLI, this
|
|
69
|
+
* repository's own packages/agent in a workspace checkout, then npm's global
|
|
70
|
+
* root. `global: false` skips the npm call for quick probes such as init's.
|
|
71
|
+
*/
|
|
72
|
+
export function findAgentPackage(options = {}) {
|
|
73
|
+
const candidates = [
|
|
74
|
+
[() => process.env.PHREN_AGENT_PACKAGE, "PHREN_AGENT_PACKAGE"],
|
|
75
|
+
[resolvedDirectory, "@phren/agent"],
|
|
76
|
+
[workspaceDirectory, "workspace"],
|
|
77
|
+
];
|
|
78
|
+
if (options.global !== false)
|
|
79
|
+
candidates.push([globalDirectory, "npm global"]);
|
|
80
|
+
for (const [locate, source] of candidates) {
|
|
81
|
+
const directory = locate();
|
|
82
|
+
const found = directory ? agentPackageAt(directory, source) : undefined;
|
|
83
|
+
if (found)
|
|
84
|
+
return found;
|
|
85
|
+
}
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
/** Whether the agent can start here, from a local package or a `phren-agent`
|
|
89
|
+
* on PATH. Cheap enough for init's summary: no npm call. */
|
|
90
|
+
export function agentInstalled() {
|
|
91
|
+
if (findAgentPackage({ global: false }))
|
|
92
|
+
return true;
|
|
93
|
+
try {
|
|
94
|
+
execFileSync(process.platform === "win32" ? "where.exe" : "which", ["phren-agent"], { stdio: "ignore", timeout: 5_000 });
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Run the agent in a child process with this terminal. The child's argv[0] is
|
|
103
|
+
* `phren-agent`, so the tmux provider and anything else that reads the process
|
|
104
|
+
* table sees the same agent whichever entry point started it; the environment
|
|
105
|
+
* (HERDR_PANE_ID, PHREN_PATH) passes through unchanged, which is what binds a
|
|
106
|
+
* Herdr pane to the session's event log.
|
|
107
|
+
*/
|
|
108
|
+
export async function runAgentCommand(args) {
|
|
109
|
+
const agent = findAgentPackage();
|
|
110
|
+
if (!agent) {
|
|
111
|
+
console.error(AGENT_PACKAGE_HINT);
|
|
112
|
+
return 1;
|
|
113
|
+
}
|
|
114
|
+
const child = spawn(process.execPath, [agent.bin, ...args], { stdio: "inherit", argv0: "phren-agent", env: process.env });
|
|
115
|
+
// The terminal delivers Ctrl-C to the whole foreground group; the child
|
|
116
|
+
// decides what it means. This process only waits and relays its exit.
|
|
117
|
+
const forward = (signal) => () => { if (!child.killed)
|
|
118
|
+
child.kill(signal); };
|
|
119
|
+
const onTerm = forward("SIGTERM"), onHup = forward("SIGHUP"), onInt = () => { };
|
|
120
|
+
process.on("SIGINT", onInt).on("SIGTERM", onTerm).on("SIGHUP", onHup);
|
|
121
|
+
try {
|
|
122
|
+
return await new Promise((resolve) => {
|
|
123
|
+
child.on("error", (error) => { console.error(`phren agent: ${error.message}`); resolve(1); });
|
|
124
|
+
child.on("exit", (code, signal) => resolve(code ?? (signal ? 128 + (signalNumber(signal) ?? 1) : 1)));
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
finally {
|
|
128
|
+
process.off("SIGINT", onInt).off("SIGTERM", onTerm).off("SIGHUP", onHup);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function signalNumber(signal) {
|
|
132
|
+
return { SIGHUP: 1, SIGINT: 2, SIGKILL: 9, SIGTERM: 15 }[signal];
|
|
133
|
+
}
|
package/dist/modules/registry.js
CHANGED
|
@@ -25,7 +25,7 @@ export const BUILTIN_MODULES = [
|
|
|
25
25
|
]),
|
|
26
26
|
],
|
|
27
27
|
cliCommands: [
|
|
28
|
-
"init", "quickstart", "add", "projects", "search", "status", "doctor", "web-ui", "graph", "shell",
|
|
28
|
+
"init", "quickstart", "add", "agent", "projects", "search", "status", "doctor", "web-ui", "graph", "shell",
|
|
29
29
|
"add-finding", "pin", "review", "session-context", "sessions", "finding", "note", "notes",
|
|
30
30
|
"search-fragments", "related-docs", "truths", "promote", "skills", "detect-skills", "hooks", "config",
|
|
31
31
|
"maintain", "consolidation-status", "quality-feedback", "mcp-mode", "hooks-mode", "preset", "snippet",
|
|
@@ -96,7 +96,7 @@ export const BUILTIN_MODULES = [
|
|
|
96
96
|
capabilities: [
|
|
97
97
|
"hook", "transcript", "progress", "images", "prompt", "stop", "terminal", "shell", "herdr",
|
|
98
98
|
"webServers", "webPreview", "activity", "approvals", "questions", "accountUsage", "providers",
|
|
99
|
-
"files", "repositoryFiles", "subagents", "sideQuestions", "approvalPush", "simulators", "overviewStream", "speech", "speechTimestamps", "transcribe",
|
|
99
|
+
"files", "repositoryFiles", "subagents", "sideQuestions", "approvalPush", "simulators", "overviewStream", "speech", "speechTimestamps", "transcribe", "promptOnce",
|
|
100
100
|
],
|
|
101
101
|
storeFiles: [".runtime/sessions/opencode-*.events.jsonl", ".runtime/approvals/opencode-*.json"],
|
|
102
102
|
localFiles: ["<bridge>/installed.json", "<bridge>/versions/**", "<bridge>/current", "<bridge>/dispatch", "<bridge>/computer-id", "<bridge>/canary.json", "<bridge>/canary-daily"],
|
package/dist/session/utils.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as fs from "fs";
|
|
2
2
|
import * as path from "path";
|
|
3
3
|
import { errorMessage } from "../utils.js";
|
|
4
|
-
// The
|
|
4
|
+
// The coding agent (@phren/agent) imports the sessions directory from here.
|
|
5
5
|
export { sessionsDir } from "../phren-paths.js";
|
|
6
6
|
import { atomicWriteText } from "../phren-paths.js";
|
|
7
7
|
/**
|
package/dist/tools/dispatch.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { hookRequest } from "../bridge/client.js";
|
|
2
2
|
import { dispatchSchema } from "../bridge/dispatch.js";
|
|
3
3
|
import { handOff, handOffSchema, listLiveSessions } from "../bridge/hand-off.js";
|
|
4
|
-
import {
|
|
4
|
+
import { terminalPaneFromEnv } from "../bridge/terminal.js";
|
|
5
5
|
import { mcpResponse } from "./types.js";
|
|
6
6
|
export function register(server) {
|
|
7
7
|
server.registerTool("dispatch", {
|
|
@@ -11,7 +11,7 @@ export function register(server) {
|
|
|
11
11
|
}, async (input) => {
|
|
12
12
|
try {
|
|
13
13
|
// The pane this agent runs in receives the one-line return notices.
|
|
14
|
-
const origin =
|
|
14
|
+
const origin = await terminalPaneFromEnv();
|
|
15
15
|
const result = await hookRequest("/v1/dispatch", { ...input, ...(origin ? { origin } : {}) }, undefined, 180_000);
|
|
16
16
|
return mcpResponse({ ok: result.ok === true, data: result, message: `${result.computer}: ${result.state}.` });
|
|
17
17
|
}
|