@phren/agent 0.1.15 → 0.3.5
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/README.md +86 -0
- package/dist/agent-loop/index.js +239 -65
- package/dist/agent-loop/stream.js +212 -29
- package/dist/agent-loop/types.js +15 -2
- package/dist/checkpoint.js +103 -22
- package/dist/commands/info.js +30 -15
- package/dist/commands/model.js +6 -2
- package/dist/commands/review.js +183 -0
- package/dist/commands/session.js +70 -7
- package/dist/commands.js +168 -12
- package/dist/config.js +87 -10
- package/dist/context/compactor.js +239 -0
- package/dist/context/pruner.js +39 -13
- package/dist/context/token-counter.js +33 -1
- package/dist/custom-commands.js +69 -0
- package/dist/guards/repeat-tool-reminder.js +75 -0
- package/dist/headless.js +115 -0
- package/dist/herdr-hooks.js +71 -0
- package/dist/index.js +351 -48
- package/dist/mcp-client.js +434 -73
- package/dist/mcp-oauth.js +121 -0
- package/dist/memory/anti-patterns.js +7 -3
- package/dist/memory/auto-capture.js +7 -3
- package/dist/memory/context.js +92 -73
- package/dist/memory/project-context.js +24 -5
- package/dist/memory/provenance.js +15 -0
- package/dist/memory/review-triage.js +166 -0
- package/dist/memory/session.js +36 -1
- package/dist/models.js +152 -19
- package/dist/multi/child-entry.js +65 -10
- package/dist/multi/diff-renderer.js +3 -3
- package/dist/multi/markdown.js +125 -27
- package/dist/multi/provider-manager.js +8 -0
- package/dist/multi/publish.js +72 -0
- package/dist/multi/spawner.js +45 -4
- package/dist/multi/syntax-highlight.js +25 -4
- package/dist/multi/types.js +1 -1
- package/dist/permissions/allowlist.js +86 -13
- package/dist/permissions/checker.js +42 -7
- package/dist/permissions/kernel-sandbox.js +197 -0
- package/dist/permissions/prompt.js +10 -1
- package/dist/permissions/sandbox.js +40 -17
- package/dist/providers/anthropic.js +154 -7
- package/dist/providers/codex-auth.js +28 -0
- package/dist/providers/codex.js +134 -23
- package/dist/providers/history.js +35 -0
- package/dist/providers/ollama.js +37 -6
- package/dist/providers/openai-compat.js +171 -74
- package/dist/providers/openrouter.js +47 -17
- package/dist/providers/replay.js +62 -0
- package/dist/providers/resolve.js +41 -7
- package/dist/providers/retry.js +38 -5
- package/dist/providers/types.js +9 -1
- package/dist/repl.js +47 -5
- package/dist/session/log.js +217 -0
- package/dist/session/persist.js +190 -0
- package/dist/settings.js +42 -26
- package/dist/system-prompt.js +18 -2
- package/dist/tools/apply-patch.js +355 -0
- package/dist/tools/edit-engine.js +287 -0
- package/dist/tools/edit-file.js +86 -27
- package/dist/tools/grep.js +25 -10
- package/dist/tools/lint-test.js +0 -22
- package/dist/tools/mcp-resources.js +51 -6
- package/dist/tools/phren-finding.js +12 -4
- package/dist/tools/phren-search.js +7 -2
- package/dist/tools/read-file.js +37 -9
- package/dist/tools/read-image.js +76 -0
- package/dist/tools/registry.js +27 -5
- package/dist/tools/shell.js +134 -71
- package/dist/tools/skill.js +20 -20
- package/dist/tools/spawn-agent.js +77 -13
- package/dist/tools/update-plan.js +48 -0
- package/dist/tools/web-fetch.js +110 -12
- package/dist/tools/web-search.js +4 -4
- package/dist/tui/components/App.js +98 -26
- package/dist/tui/components/ApprovalPanel.js +8 -0
- package/dist/tui/components/InputArea.js +5 -5
- package/dist/tui/components/ModelPicker.js +43 -0
- package/dist/tui/components/PhrenInput.js +10 -5
- package/dist/tui/components/PlanReview.js +7 -0
- package/dist/tui/components/ShortcutHelp.js +5 -0
- package/dist/tui/components/StatusBar.js +46 -20
- package/dist/tui/components/SteerQueue.js +1 -1
- package/dist/tui/components/ThinkingIndicator.js +6 -4
- package/dist/tui/components/ToolCall.js +10 -4
- package/dist/tui/components/ToolDetail.js +21 -0
- package/dist/tui/help.js +6 -0
- package/dist/tui/hooks/useKeyboardShortcuts.js +15 -1
- package/dist/tui/index.js +0 -1
- package/dist/tui/ink-entry.js +317 -33
- package/dist/tui/tool-render.js +1 -1
- package/dist/user-hooks.js +171 -0
- package/package.json +14 -8
- package/dist/multi/multi-commands.js +0 -170
- package/dist/multi/multi-events.js +0 -98
- package/dist/multi/multi-render.js +0 -147
- package/dist/multi/pane.js +0 -28
- package/dist/multi/tui-multi.js +0 -200
- package/dist/permissions/privacy.js +0 -248
- package/dist/tui/components/AgentMessage.js +0 -5
- package/dist/tui/components/ChatMessage.js +0 -23
- package/dist/tui/components/CodeBlock.js +0 -8
- package/dist/tui/components/Separator.js +0 -7
- package/dist/tui/components/StreamingText.js +0 -5
- package/dist/tui/components/UserMessage.js +0 -5
- package/dist/tui/menu-mode.js +0 -86
- package/dist/tui/mouse.js +0 -71
- package/dist/tui/terminal-control.js +0 -62
- package/dist/tui.js +0 -781
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* /review — triage the phren review queue without leaving the session.
|
|
3
|
+
*
|
|
4
|
+
* /review list pending items
|
|
5
|
+
* /review go per-item triage: [y]approve [n]reject [e]edit [s]skip [q]uit
|
|
6
|
+
* /review auto the model proposes verdicts, you confirm in one keypress
|
|
7
|
+
* /review expire N drop unreviewed items older than N days (default env/14,
|
|
8
|
+
* 0 = never). Dequeues only — findings are never deleted.
|
|
9
|
+
*/
|
|
10
|
+
import * as readline from "node:readline";
|
|
11
|
+
import { listQueueItems, proposeTriage, expireStaleItems, formatExpiryNotice, resolveExpireDays, } from "../memory/review-triage.js";
|
|
12
|
+
import { approveQueueItem, rejectQueueItem, editQueueItem } from "@phren/cli/data/access";
|
|
13
|
+
const DIM = "\x1b[2m";
|
|
14
|
+
const BOLD = "\x1b[1m";
|
|
15
|
+
const GREEN = "\x1b[32m";
|
|
16
|
+
const RED = "\x1b[31m";
|
|
17
|
+
const YELLOW = "\x1b[33m";
|
|
18
|
+
const RESET = "\x1b[0m";
|
|
19
|
+
function ask(question) {
|
|
20
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stderr });
|
|
21
|
+
return new Promise((resolve) => {
|
|
22
|
+
rl.question(question, (answer) => {
|
|
23
|
+
rl.close();
|
|
24
|
+
resolve(answer.trim());
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
function formatItem(item, index) {
|
|
29
|
+
const age = item.ageDays !== null ? `${item.ageDays}d` : "undated";
|
|
30
|
+
const text = item.text.length > 100 ? `${item.text.slice(0, 100)}…` : item.text;
|
|
31
|
+
return ` ${BOLD}${index + 1}.${RESET} ${DIM}[${age}]${RESET} ${text}`;
|
|
32
|
+
}
|
|
33
|
+
function listItems(items) {
|
|
34
|
+
process.stderr.write(`${BOLD}Review queue (${items.length} pending):${RESET}\n`);
|
|
35
|
+
items.forEach((item, i) => process.stderr.write(formatItem(item, i) + "\n"));
|
|
36
|
+
process.stderr.write(`${DIM}Use /review go (manual), /review auto (model-assisted), /review expire [days].${RESET}\n`);
|
|
37
|
+
}
|
|
38
|
+
async function triageLoop(ctx, items, defaults) {
|
|
39
|
+
const counts = { approved: 0, rejected: 0, edited: 0, skipped: 0 };
|
|
40
|
+
const phrenCtx = ctx.phrenCtx;
|
|
41
|
+
const project = phrenCtx.project;
|
|
42
|
+
for (let i = 0; i < items.length; i++) {
|
|
43
|
+
const item = items[i];
|
|
44
|
+
process.stderr.write("\n" + formatItem(item, i) + "\n");
|
|
45
|
+
const proposal = defaults?.get(item.line);
|
|
46
|
+
let promptSuffix = "[y]approve [n]reject [e]dit [s]kip [q]uit";
|
|
47
|
+
if (proposal) {
|
|
48
|
+
const color = proposal.verdict === "approve" ? GREEN : RED;
|
|
49
|
+
process.stderr.write(` ${DIM}model suggests:${RESET} ${color}${proposal.verdict}${RESET}${proposal.reason ? ` ${DIM}— ${proposal.reason}${RESET}` : ""}\n`);
|
|
50
|
+
promptSuffix += ` (enter = ${proposal.verdict})`;
|
|
51
|
+
}
|
|
52
|
+
const answer = (await ask(` ${promptSuffix}: `)).toLowerCase();
|
|
53
|
+
const effective = answer === "" && proposal ? (proposal.verdict === "approve" ? "y" : "n") : answer;
|
|
54
|
+
if (effective === "q")
|
|
55
|
+
break;
|
|
56
|
+
if (effective === "y") {
|
|
57
|
+
const result = approveQueueItem(phrenCtx.phrenPath, project, item.line);
|
|
58
|
+
if (result.ok) {
|
|
59
|
+
counts.approved++;
|
|
60
|
+
process.stderr.write(` ${GREEN}✓ approved${RESET}\n`);
|
|
61
|
+
}
|
|
62
|
+
else
|
|
63
|
+
process.stderr.write(` ${RED}${result.error}${RESET}\n`);
|
|
64
|
+
}
|
|
65
|
+
else if (effective === "n") {
|
|
66
|
+
const result = rejectQueueItem(phrenCtx.phrenPath, project, item.line);
|
|
67
|
+
if (result.ok) {
|
|
68
|
+
counts.rejected++;
|
|
69
|
+
process.stderr.write(` ${RED}✗ rejected${RESET}\n`);
|
|
70
|
+
}
|
|
71
|
+
else
|
|
72
|
+
process.stderr.write(` ${RED}${result.error}${RESET}\n`);
|
|
73
|
+
}
|
|
74
|
+
else if (effective === "e") {
|
|
75
|
+
const newText = await ask(" new text: ");
|
|
76
|
+
if (newText) {
|
|
77
|
+
const result = editQueueItem(phrenCtx.phrenPath, project, item.line, newText);
|
|
78
|
+
if (result.ok) {
|
|
79
|
+
counts.edited++;
|
|
80
|
+
process.stderr.write(` ${YELLOW}✎ edited (still queued)${RESET}\n`);
|
|
81
|
+
}
|
|
82
|
+
else
|
|
83
|
+
process.stderr.write(` ${RED}${result.error}${RESET}\n`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
counts.skipped++;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return counts;
|
|
91
|
+
}
|
|
92
|
+
function summarize(counts) {
|
|
93
|
+
process.stderr.write(`\n${DIM}Triage done: ${GREEN}${counts.approved} approved${RESET}${DIM}, ${RED}${counts.rejected} rejected${RESET}${DIM}, ${counts.edited} edited, ${counts.skipped} skipped.${RESET}\n`);
|
|
94
|
+
}
|
|
95
|
+
export async function reviewCommand(parts, ctx) {
|
|
96
|
+
const sub = parts[1]?.toLowerCase();
|
|
97
|
+
if (!ctx.phrenCtx?.project) {
|
|
98
|
+
process.stderr.write(`${DIM}No phren project context — nothing to review.${RESET}\n`);
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
if (sub === "expire") {
|
|
102
|
+
// Explicit parse, not `|| resolveExpireDays()`: `expire 0` means "never expire"
|
|
103
|
+
// and must not fall through the falsy-OR into the 14-day default.
|
|
104
|
+
const arg = parts[2]?.trim();
|
|
105
|
+
let days;
|
|
106
|
+
if (arg === undefined || arg === "") {
|
|
107
|
+
days = resolveExpireDays();
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
const parsed = Number.parseInt(arg, 10);
|
|
111
|
+
if (!Number.isFinite(parsed) || parsed < 0 || !/^\d+$/.test(arg)) {
|
|
112
|
+
process.stderr.write(`${RED}Not a day count: "${arg}". Usage: /review expire [days] (0 = never).${RESET}\n`);
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
days = parsed;
|
|
116
|
+
}
|
|
117
|
+
if (days === 0) {
|
|
118
|
+
process.stderr.write(`${DIM}Expiry disabled (0 days) — nothing removed.${RESET}\n`);
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
const result = expireStaleItems(ctx.phrenCtx, days);
|
|
122
|
+
process.stderr.write(`${DIM}${formatExpiryNotice(result) ?? `No review items older than ${days} days.`}${RESET}\n`);
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
const items = listQueueItems(ctx.phrenCtx);
|
|
126
|
+
if (items.length === 0) {
|
|
127
|
+
process.stderr.write(`${DIM}Review queue is empty. Nothing to triage.${RESET}\n`);
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
if (sub === undefined) {
|
|
131
|
+
listItems(items);
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
if (sub === "go") {
|
|
135
|
+
summarize(await triageLoop(ctx, items));
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
if (sub === "auto") {
|
|
139
|
+
if (!ctx.provider) {
|
|
140
|
+
process.stderr.write(`${DIM}No provider available for auto-triage. Use /review go.${RESET}\n`);
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
process.stderr.write(`${DIM}Asking the model to triage ${items.length} item(s)…${RESET}\n`);
|
|
144
|
+
const proposals = await proposeTriage(ctx.provider, items);
|
|
145
|
+
if (proposals.length === 0) {
|
|
146
|
+
process.stderr.write(`${DIM}Could not get usable triage proposals. Use /review go.${RESET}\n`);
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
const approvals = proposals.filter((p) => p.verdict === "approve");
|
|
150
|
+
const rejections = proposals.filter((p) => p.verdict === "reject");
|
|
151
|
+
for (const p of proposals) {
|
|
152
|
+
const color = p.verdict === "approve" ? GREEN : RED;
|
|
153
|
+
const text = p.text.length > 80 ? `${p.text.slice(0, 80)}…` : p.text;
|
|
154
|
+
process.stderr.write(` ${color}${p.verdict.padEnd(7)}${RESET} ${text}${p.reason ? ` ${DIM}— ${p.reason}${RESET}` : ""}\n`);
|
|
155
|
+
}
|
|
156
|
+
const answer = (await ask(`\nApply ${approvals.length} approval(s) and ${rejections.length} rejection(s)? [y/N/i(nteractive)] `)).toLowerCase();
|
|
157
|
+
if (answer === "y" || answer === "yes") {
|
|
158
|
+
const counts = { approved: 0, rejected: 0, edited: 0, skipped: 0 };
|
|
159
|
+
for (const p of proposals) {
|
|
160
|
+
const apply = p.verdict === "approve" ? approveQueueItem : rejectQueueItem;
|
|
161
|
+
const result = apply(ctx.phrenCtx.phrenPath, ctx.phrenCtx.project, p.line);
|
|
162
|
+
if (result.ok) {
|
|
163
|
+
if (p.verdict === "approve")
|
|
164
|
+
counts.approved++;
|
|
165
|
+
else
|
|
166
|
+
counts.rejected++;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
counts.skipped = items.length - counts.approved - counts.rejected;
|
|
170
|
+
summarize(counts);
|
|
171
|
+
}
|
|
172
|
+
else if (answer === "i") {
|
|
173
|
+
const defaults = new Map(proposals.map((p) => [p.line, p]));
|
|
174
|
+
summarize(await triageLoop(ctx, items, defaults));
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
process.stderr.write(`${DIM}Nothing applied.${RESET}\n`);
|
|
178
|
+
}
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
process.stderr.write(`${DIM}Usage: /review [go|auto|expire [days]]${RESET}\n`);
|
|
182
|
+
return true;
|
|
183
|
+
}
|
package/dist/commands/session.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
+
import { toolResultText } from "../providers/types.js";
|
|
2
|
+
import { seedFromMessages } from "../session/log.js";
|
|
1
3
|
import { estimateMessageTokens } from "../context/token-counter.js";
|
|
2
|
-
import {
|
|
4
|
+
import { planPrune } from "../context/pruner.js";
|
|
5
|
+
import { compactWithLlm } from "../context/compactor.js";
|
|
3
6
|
import { renderMarkdown } from "../multi/markdown.js";
|
|
4
7
|
import { saveSessionMessages, loadLastSessionSnapshot } from "../memory/session.js";
|
|
8
|
+
import { listCheckpoints, restoreCheckpoint } from "../checkpoint.js";
|
|
5
9
|
import { execSync } from "node:child_process";
|
|
6
10
|
import * as fs from "node:fs";
|
|
7
11
|
import * as path from "node:path";
|
|
@@ -107,7 +111,7 @@ export function historyCommand(parts, ctx) {
|
|
|
107
111
|
for (const block of msg.content) {
|
|
108
112
|
if (block.type === "tool_result") {
|
|
109
113
|
const icon = block.is_error ? `${RED}\u2717${RESET}` : `${GREEN}\u2713${RESET}`;
|
|
110
|
-
const preview = (block
|
|
114
|
+
const preview = toolResultText(block).slice(0, 80).replace(/\n/g, " ");
|
|
111
115
|
process.stderr.write(`${DIM} ${icon} tool_result ${preview}${preview.length >= 80 ? "..." : ""}${RESET}\n`);
|
|
112
116
|
}
|
|
113
117
|
else if (block.type === "text") {
|
|
@@ -140,16 +144,47 @@ export function historyCommand(parts, ctx) {
|
|
|
140
144
|
process.stderr.write(`${DIM}-- end --${RESET}\n`);
|
|
141
145
|
return true;
|
|
142
146
|
}
|
|
143
|
-
export function compactCommand(_parts, ctx) {
|
|
147
|
+
export async function compactCommand(_parts, ctx) {
|
|
144
148
|
const beforeCount = ctx.session.messages.length;
|
|
145
149
|
const beforeTokens = estimateMessageTokens(ctx.session.messages);
|
|
146
|
-
|
|
150
|
+
// LLM checkpoint when a provider is wired up; regex fallback otherwise.
|
|
151
|
+
let applied = false;
|
|
152
|
+
let mode = "regex";
|
|
153
|
+
let routed = "";
|
|
154
|
+
if (ctx.provider && ctx.systemPrompt) {
|
|
155
|
+
const result = await compactWithLlm(ctx.provider, ctx.systemPrompt, ctx.session.messages, {
|
|
156
|
+
phrenCtx: ctx.phrenCtx,
|
|
157
|
+
sessionId: ctx.sessionId,
|
|
158
|
+
pruneConfig: { contextLimit: ctx.contextLimit, keepRecentTurns: 4 },
|
|
159
|
+
// Manual /compact should always try the LLM, even for small ranges.
|
|
160
|
+
config: { minPrunedTokens: 0 },
|
|
161
|
+
});
|
|
162
|
+
if (result) {
|
|
163
|
+
ctx.session.log.replaceMessageRange(result.plan.startIndex, result.plan.endIndex, result.plan.summaryMessage);
|
|
164
|
+
applied = true;
|
|
165
|
+
mode = result.usedLlm ? "llm" : "regex";
|
|
166
|
+
if (result.promoted + result.queued > 0) {
|
|
167
|
+
routed = `, ${result.promoted} finding(s) promoted, ${result.queued} queued for review`;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
const plan = planPrune(ctx.session.messages, { contextLimit: ctx.contextLimit, keepRecentTurns: 4 });
|
|
173
|
+
if (plan) {
|
|
174
|
+
ctx.session.log.replaceMessageRange(plan.startIndex, plan.endIndex, plan.summaryMessage);
|
|
175
|
+
applied = true;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (!applied) {
|
|
179
|
+
process.stderr.write(`${DIM}Nothing to compact.${RESET}\n`);
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
147
182
|
const afterCount = ctx.session.messages.length;
|
|
148
183
|
const afterTokens = estimateMessageTokens(ctx.session.messages);
|
|
149
184
|
const reduction = beforeTokens > 0 ? ((1 - afterTokens / beforeTokens) * 100).toFixed(0) : "0";
|
|
150
185
|
const fmtBefore = beforeTokens >= 1000 ? `${(beforeTokens / 1000).toFixed(1)}k` : String(beforeTokens);
|
|
151
186
|
const fmtAfter = afterTokens >= 1000 ? `${(afterTokens / 1000).toFixed(1)}k` : String(afterTokens);
|
|
152
|
-
process.stderr.write(`${DIM}Compacted: ${beforeCount} -> ${afterCount} messages (~${fmtBefore} -> ~${fmtAfter} tokens, ${reduction}% reduction)${RESET}\n`);
|
|
187
|
+
process.stderr.write(`${DIM}Compacted (${mode}): ${beforeCount} -> ${afterCount} messages (~${fmtBefore} -> ~${fmtAfter} tokens, ${reduction}% reduction${routed})${RESET}\n`);
|
|
153
188
|
return true;
|
|
154
189
|
}
|
|
155
190
|
export function diffCommand(parts, _ctx) {
|
|
@@ -192,9 +227,13 @@ export function resumeCommand(_parts, ctx) {
|
|
|
192
227
|
process.stderr.write(`${DIM}No previous session to resume.${RESET}\n`);
|
|
193
228
|
return true;
|
|
194
229
|
}
|
|
195
|
-
//
|
|
230
|
+
// Seed the current session's log from the serialized snapshot
|
|
196
231
|
const priorCount = snapshot.messages.length;
|
|
197
|
-
ctx.session.messages
|
|
232
|
+
if (ctx.session.messages.length > 0) {
|
|
233
|
+
process.stderr.write(`${DIM}Session already has history; /resume only works on a fresh session.${RESET}\n`);
|
|
234
|
+
return true;
|
|
235
|
+
}
|
|
236
|
+
seedFromMessages(ctx.session.log, snapshot.messages);
|
|
198
237
|
process.stderr.write(`${GREEN}-> Resumed ${priorCount} messages from session ${snapshot.sessionId}${RESET}\n`);
|
|
199
238
|
if (snapshot.project) {
|
|
200
239
|
process.stderr.write(`${DIM} Project: ${snapshot.project}${RESET}\n`);
|
|
@@ -203,6 +242,30 @@ export function resumeCommand(_parts, ctx) {
|
|
|
203
242
|
process.stderr.write(`${DIM} Use /history to review. The LLM will see the full prior conversation on your next message.${RESET}\n`);
|
|
204
243
|
return true;
|
|
205
244
|
}
|
|
245
|
+
export function rewindCommand(parts, _ctx) {
|
|
246
|
+
const arg = parts[1];
|
|
247
|
+
const list = listCheckpoints();
|
|
248
|
+
if (!arg) {
|
|
249
|
+
if (list.length === 0) {
|
|
250
|
+
process.stderr.write(`${DIM}No checkpoints.${RESET}\n`);
|
|
251
|
+
return true;
|
|
252
|
+
}
|
|
253
|
+
const lines = list.map((c, i) => ` ${i + 1}. ${c.label} ${c.createdAt.slice(0, 19).replace("T", " ")}`).join("\n");
|
|
254
|
+
process.stderr.write(`${DIM}Checkpoints (newest first):\n${lines}\n /rewind <n> to restore${RESET}\n`);
|
|
255
|
+
return true;
|
|
256
|
+
}
|
|
257
|
+
const index = Number(arg);
|
|
258
|
+
const target = Number.isInteger(index) && index >= 1 && index <= list.length
|
|
259
|
+
? list[index - 1]
|
|
260
|
+
: list.find((c) => c.ref === arg || c.label === arg);
|
|
261
|
+
if (!target) {
|
|
262
|
+
process.stderr.write(`${RED}No checkpoint "${arg}".${RESET}\n`);
|
|
263
|
+
return true;
|
|
264
|
+
}
|
|
265
|
+
const result = restoreCheckpoint(process.cwd(), target.ref);
|
|
266
|
+
process.stderr.write(`${result.ok ? GREEN : RED}${result.message}${RESET}\n`);
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
206
269
|
export function gitCommand(parts, _ctx) {
|
|
207
270
|
const sub = parts.slice(1).join(" ").trim();
|
|
208
271
|
if (!sub) {
|
package/dist/commands.js
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
// Sub-module handlers
|
|
2
2
|
import { helpCommand, turnsCommand, clearCommand, cwdCommand, filesCommand, costCommand, planCommand, undoCommand, contextCommand } from "./commands/info.js";
|
|
3
|
-
import { sessionCommand, historyCommand, compactCommand, diffCommand, gitCommand, resumeCommand } from "./commands/session.js";
|
|
3
|
+
import { sessionCommand, historyCommand, compactCommand, diffCommand, gitCommand, resumeCommand, rewindCommand } from "./commands/session.js";
|
|
4
4
|
import { memCommand, askCommand } from "./commands/memory.js";
|
|
5
|
+
import { reviewCommand } from "./commands/review.js";
|
|
6
|
+
import { findSkill, getScopedSkills } from "@phren/cli/skill/registry";
|
|
5
7
|
import { modelCommand, providerCommand, presetCommand } from "./commands/model.js";
|
|
6
8
|
import { configCommand } from "./commands/config.js";
|
|
7
9
|
import { loadInputMode, saveInputMode, savePermissionMode } from "./settings.js";
|
|
10
|
+
import { addAllow } from "./permissions/allowlist.js";
|
|
11
|
+
import { loadCustomCommands, expandCustomCommand, customCommandInfos } from "./custom-commands.js";
|
|
8
12
|
const DIM = "\x1b[2m";
|
|
9
13
|
const RESET = "\x1b[0m";
|
|
10
14
|
export function createCommandContext(session, contextLimit) {
|
|
@@ -14,17 +18,91 @@ export function createCommandContext(session, contextLimit) {
|
|
|
14
18
|
undoStack: [],
|
|
15
19
|
};
|
|
16
20
|
}
|
|
17
|
-
|
|
18
|
-
* All slash commands available in the agent, including TUI-only commands.
|
|
19
|
-
* Used for tab completion in the TUI.
|
|
20
|
-
*/
|
|
21
|
-
export const COMMAND_NAMES = [
|
|
21
|
+
const BUILTIN_COMMAND_NAMES = [
|
|
22
22
|
"/help", "/turns", "/clear", "/cwd", "/files", "/cost", "/plan", "/undo",
|
|
23
23
|
"/context", "/model", "/provider", "/preset", "/session", "/history",
|
|
24
|
-
"/compact", "/diff", "/git", "/mem", "/ask", "/resume", "/config", "/spawn", "/agents",
|
|
25
|
-
"/
|
|
24
|
+
"/compact", "/diff", "/git", "/mem", "/ask", "/resume", "/review", "/config", "/spawn", "/agents",
|
|
25
|
+
"/allow",
|
|
26
|
+
"/mode", "/permissions", "/verbose", "/theme", "/agent", "/rewind", "/fork",
|
|
26
27
|
"/exit", "/quit", "/q",
|
|
27
28
|
];
|
|
29
|
+
/**
|
|
30
|
+
* All slash commands available in the agent, including TUI-only commands.
|
|
31
|
+
* Used for tab completion in the TUI.
|
|
32
|
+
*/
|
|
33
|
+
export const COMMAND_NAMES = [...BUILTIN_COMMAND_NAMES];
|
|
34
|
+
const BUILTIN_BARE_NAMES = new Set(BUILTIN_COMMAND_NAMES.map((name) => name.slice(1)));
|
|
35
|
+
let customCommands = [];
|
|
36
|
+
export function setCustomCommands(commands) {
|
|
37
|
+
customCommands = commands.filter((command) => !BUILTIN_BARE_NAMES.has(command.name));
|
|
38
|
+
COMMAND_NAMES.length = 0;
|
|
39
|
+
COMMAND_NAMES.push(...BUILTIN_COMMAND_NAMES, ...customCommands.map((command) => `/${command.name}`));
|
|
40
|
+
}
|
|
41
|
+
export function loadAndRegisterCustomCommands(cwd = process.cwd()) {
|
|
42
|
+
const commands = loadCustomCommands(cwd);
|
|
43
|
+
setCustomCommands(commands);
|
|
44
|
+
return commands;
|
|
45
|
+
}
|
|
46
|
+
export function getCustomCommands() {
|
|
47
|
+
return customCommands;
|
|
48
|
+
}
|
|
49
|
+
export function getCustomCommandInfos() {
|
|
50
|
+
return customCommandInfos(customCommands);
|
|
51
|
+
}
|
|
52
|
+
export function resolveCustomCommand(input) {
|
|
53
|
+
if (!input.startsWith("/"))
|
|
54
|
+
return null;
|
|
55
|
+
const trimmed = input.trim();
|
|
56
|
+
const parts = trimmed.split(/\s+/);
|
|
57
|
+
const cmd = parts[0];
|
|
58
|
+
if (BUILTIN_BARE_NAMES.has(cmd.slice(1)))
|
|
59
|
+
return null;
|
|
60
|
+
const bare = cmd.slice(1);
|
|
61
|
+
if (!bare)
|
|
62
|
+
return null;
|
|
63
|
+
const match = customCommands.find((command) => command.name === bare);
|
|
64
|
+
if (!match)
|
|
65
|
+
return null;
|
|
66
|
+
const args = trimmed.slice(cmd.length).trim();
|
|
67
|
+
const expanded = expandCustomCommand(match, args);
|
|
68
|
+
return expanded.length > 0 ? expanded : null;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* /skill-name gesture: an unknown slash input matching a phren skill (by name,
|
|
72
|
+
* frontmatter command, or alias) becomes a model task that invokes run_skill.
|
|
73
|
+
* Returns the rewritten task, or null when the input is not a skill gesture.
|
|
74
|
+
* Built-in commands always win. Never throws.
|
|
75
|
+
*/
|
|
76
|
+
export function resolveSkillGesture(input, phrenCtx) {
|
|
77
|
+
if (!phrenCtx || !input.startsWith("/"))
|
|
78
|
+
return null;
|
|
79
|
+
const parts = input.trim().split(/\s+/);
|
|
80
|
+
const cmd = parts[0];
|
|
81
|
+
if (COMMAND_NAMES.includes(cmd))
|
|
82
|
+
return null;
|
|
83
|
+
const args = parts.slice(1).join(" ");
|
|
84
|
+
const bare = cmd.slice(1);
|
|
85
|
+
if (!bare)
|
|
86
|
+
return null;
|
|
87
|
+
try {
|
|
88
|
+
let name = null;
|
|
89
|
+
const byName = findSkill(phrenCtx.phrenPath, phrenCtx.profile, phrenCtx.project ?? undefined, bare);
|
|
90
|
+
if (byName && !("error" in byName) && byName.enabled)
|
|
91
|
+
name = byName.name;
|
|
92
|
+
if (!name) {
|
|
93
|
+
const all = getScopedSkills(phrenCtx.phrenPath, phrenCtx.profile, phrenCtx.project ?? undefined);
|
|
94
|
+
const match = all.find((s) => s.enabled && (s.command === cmd || s.aliases.includes(cmd)));
|
|
95
|
+
if (match)
|
|
96
|
+
name = match.name;
|
|
97
|
+
}
|
|
98
|
+
if (!name)
|
|
99
|
+
return null;
|
|
100
|
+
return `[user invoked ${cmd}] Call run_skill with name="${name}"${args ? ` and args="${args}"` : ""}, then follow the skill's instructions.`;
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
28
106
|
/**
|
|
29
107
|
* Try to handle a slash command. Returns true if the input was a command.
|
|
30
108
|
* Returns a Promise<boolean> for async commands like /ask.
|
|
@@ -53,7 +131,18 @@ export function handleCommand(input, ctx) {
|
|
|
53
131
|
case "/mem": return memCommand(parts, ctx);
|
|
54
132
|
case "/ask": return askCommand(parts, ctx);
|
|
55
133
|
case "/resume": return resumeCommand(parts, ctx);
|
|
134
|
+
case "/review": return reviewCommand(parts, ctx);
|
|
56
135
|
case "/config": return configCommand(parts, ctx);
|
|
136
|
+
case "/rewind": return rewindCommand(parts, ctx);
|
|
137
|
+
case "/fork": {
|
|
138
|
+
const result = ctx.forkSession?.();
|
|
139
|
+
if (!result) {
|
|
140
|
+
process.stderr.write(`${DIM}Fork is not available here.${RESET}\n`);
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
process.stderr.write(`${result.ok ? "\x1b[32m" : "\x1b[31m"}${result.message}${RESET}\n`);
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
57
146
|
case "/mode": {
|
|
58
147
|
const current = loadInputMode();
|
|
59
148
|
const newMode = current === "steering" ? "queue" : "steering";
|
|
@@ -76,18 +165,85 @@ export function handleCommand(input, ctx) {
|
|
|
76
165
|
}
|
|
77
166
|
return true;
|
|
78
167
|
}
|
|
168
|
+
case "/allow": {
|
|
169
|
+
const toolName = parts[1];
|
|
170
|
+
if (!toolName) {
|
|
171
|
+
process.stderr.write(`${DIM}Usage: /allow <tool> [pattern] [--global]${RESET}\n`);
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
const global = parts.includes("--global");
|
|
175
|
+
const pattern = parts.slice(2).find((p) => p !== "--global");
|
|
176
|
+
if (toolName === "shell" && !pattern) {
|
|
177
|
+
process.stderr.write(`${DIM}shell rules need a command/prefix, e.g. /allow shell git${RESET}\n`);
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
const input = {};
|
|
181
|
+
if (pattern) {
|
|
182
|
+
if (toolName === "shell")
|
|
183
|
+
input.command = pattern;
|
|
184
|
+
else
|
|
185
|
+
input.path = pattern;
|
|
186
|
+
}
|
|
187
|
+
const scope = global ? "global" : "project";
|
|
188
|
+
addAllow(toolName, input, scope, process.cwd());
|
|
189
|
+
process.stderr.write(`${DIM}Allowed ${toolName}${pattern ? ` (${pattern})` : ""} [${scope}]${RESET}\n`);
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
79
192
|
case "/spawn": {
|
|
80
193
|
if (!ctx.spawner) {
|
|
81
194
|
process.stderr.write(`${DIM}Spawner not available. Start with --multi or --team to enable.${RESET}\n`);
|
|
82
195
|
return true;
|
|
83
196
|
}
|
|
84
|
-
const
|
|
85
|
-
const
|
|
197
|
+
const rest = parts.slice(1);
|
|
198
|
+
const spawnName = rest[0];
|
|
199
|
+
let isolation;
|
|
200
|
+
let agentType;
|
|
201
|
+
let provider;
|
|
202
|
+
let model;
|
|
203
|
+
let permissions;
|
|
204
|
+
const taskParts = [];
|
|
205
|
+
const VALID_MODES = ["suggest", "auto-confirm", "plan", "full-auto"];
|
|
206
|
+
for (let i = 1; i < rest.length; i++) {
|
|
207
|
+
const token = rest[i];
|
|
208
|
+
if (token === "--worktree") {
|
|
209
|
+
isolation = "worktree";
|
|
210
|
+
}
|
|
211
|
+
else if (token === "--agent" && rest[i + 1]) {
|
|
212
|
+
agentType = rest[++i];
|
|
213
|
+
}
|
|
214
|
+
else if (token === "--provider" && rest[i + 1]) {
|
|
215
|
+
provider = rest[++i];
|
|
216
|
+
}
|
|
217
|
+
else if (token === "--model" && rest[i + 1]) {
|
|
218
|
+
model = rest[++i];
|
|
219
|
+
}
|
|
220
|
+
else if (token === "--permissions" && rest[i + 1]) {
|
|
221
|
+
const mode = rest[++i];
|
|
222
|
+
if (VALID_MODES.includes(mode))
|
|
223
|
+
permissions = mode;
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
taskParts.push(token);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
const spawnTask = taskParts.join(" ");
|
|
86
230
|
if (!spawnName || !spawnTask) {
|
|
87
|
-
process.stderr.write(`${DIM}Usage: /spawn <name> <task
|
|
231
|
+
process.stderr.write(`${DIM}Usage: /spawn <name> <task> [--worktree] [--agent <type>] [--provider <p>] [--model <m>] [--permissions <mode>]${RESET}\n`);
|
|
88
232
|
return true;
|
|
89
233
|
}
|
|
90
|
-
const
|
|
234
|
+
const perms = ctx.registry?.permissionConfig;
|
|
235
|
+
const agentId = ctx.spawner.spawn({
|
|
236
|
+
task: spawnTask,
|
|
237
|
+
displayName: spawnName,
|
|
238
|
+
cwd: perms?.projectRoot ?? process.cwd(),
|
|
239
|
+
isolation,
|
|
240
|
+
agentType,
|
|
241
|
+
provider,
|
|
242
|
+
model,
|
|
243
|
+
permissions: permissions ?? perms?.mode,
|
|
244
|
+
sandboxMode: perms?.sandboxMode,
|
|
245
|
+
allowedPaths: perms?.allowedPaths,
|
|
246
|
+
});
|
|
91
247
|
process.stderr.write(`${DIM}Spawned agent "${spawnName}" (${agentId}): ${spawnTask}${RESET}\n`);
|
|
92
248
|
return true;
|
|
93
249
|
}
|