@phren/agent 0.1.8 → 0.1.10
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/commands/info.js +1 -0
- package/dist/commands/model.js +5 -4
- package/dist/commands/session.js +24 -2
- package/dist/commands.js +36 -1
- package/dist/config.js +27 -11
- package/dist/cost.js +11 -74
- package/dist/index.js +92 -35
- package/dist/mcp-client.js +4 -2
- package/dist/memory/context.js +56 -24
- package/dist/memory/session.js +21 -88
- package/dist/models.js +390 -0
- package/dist/multi/agent-types.js +170 -0
- package/dist/multi/child-entry.js +248 -78
- package/dist/multi/coordinator.js +69 -1
- package/dist/multi/diff-renderer.js +193 -17
- package/dist/multi/markdown.js +49 -23
- package/dist/multi/model-picker.js +12 -21
- package/dist/multi/multi-events.js +17 -0
- package/dist/multi/multi-render.js +1 -0
- package/dist/multi/provider-manager.js +23 -13
- package/dist/multi/spawner.js +84 -7
- package/dist/multi/syntax-highlight.js +64 -48
- package/dist/multi/worktree.js +43 -0
- package/dist/package-metadata.js +11 -0
- package/dist/permissions/checker.js +21 -1
- package/dist/providers/codex-auth.js +48 -34
- package/dist/providers/codex.js +110 -40
- package/dist/providers/ollama.js +5 -4
- package/dist/providers/openrouter.js +12 -5
- package/dist/providers/resolve.js +50 -32
- package/dist/providers/types.js +0 -1
- package/dist/repl.js +19 -34
- package/dist/tools/grep.js +111 -23
- package/dist/tools/mcp-resources.js +32 -0
- package/dist/tools/registry.js +13 -2
- package/dist/tools/shell.js +101 -3
- package/dist/tools/skill.js +44 -0
- package/dist/tools/spawn-agent.js +158 -0
- package/dist/tui/ansi.js +15 -11
- package/dist/tui/components/App.js +180 -20
- package/dist/tui/components/Banner.js +7 -4
- package/dist/tui/components/InputArea.js +54 -12
- package/dist/tui/components/PhrenInput.js +329 -0
- package/dist/tui/components/StatusBar.js +3 -1
- package/dist/tui/components/SteerQueue.js +4 -2
- package/dist/tui/components/ThinkingIndicator.js +12 -11
- package/dist/tui/components/ToolCall.js +17 -11
- package/dist/tui/components/ToolSpinner.js +13 -0
- package/dist/tui/hooks/useKeyboardShortcuts.js +65 -24
- package/dist/tui/hooks/useSearch.js +47 -0
- package/dist/tui/hooks/useSlashCommands.js +16 -19
- package/dist/tui/ink-entry.js +317 -18
- package/dist/tui/mouse.js +71 -0
- package/dist/tui/terminal-control.js +62 -0
- package/dist/tui/themes.js +398 -0
- package/dist/tui/tool-render.js +12 -1
- package/dist/tui.js +2 -1
- package/package.json +1 -3
package/dist/commands/info.js
CHANGED
|
@@ -26,6 +26,7 @@ export function helpCommand(_parts, _ctx) {
|
|
|
26
26
|
/context Show context window usage and provider info
|
|
27
27
|
/mode Toggle input mode (steering ↔ queue)
|
|
28
28
|
/verbose Toggle verbose tool output (show first 5 lines)
|
|
29
|
+
/theme Toggle theme (dark ↔ light)
|
|
29
30
|
/spawn <name> <task> Spawn a background agent
|
|
30
31
|
/agents List running agents
|
|
31
32
|
/session Show session info (id, duration, stats)
|
package/dist/commands/model.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { listPresets, loadPreset, savePreset, deletePreset, formatPreset } from "../multi/presets.js";
|
|
2
2
|
import { showModelPicker } from "../multi/model-picker.js";
|
|
3
3
|
import { formatProviderList, formatModelAddHelp, addCustomModel, removeCustomModel } from "../multi/provider-manager.js";
|
|
4
|
+
import { normalizeReasoningEffort } from "../models.js";
|
|
4
5
|
const DIM = "\x1b[2m";
|
|
5
6
|
const GREEN = "\x1b[32m";
|
|
6
7
|
const RESET = "\x1b[0m";
|
|
@@ -24,10 +25,10 @@ export function modelCommand(parts, ctx) {
|
|
|
24
25
|
else if (k === "context")
|
|
25
26
|
contextWindow = parseInt(v, 10) || 128_000;
|
|
26
27
|
else if (k === "reasoning") {
|
|
27
|
-
reasoning = v;
|
|
28
|
+
reasoning = normalizeReasoningEffort(v) ?? null;
|
|
28
29
|
reasoningRange.push("low", "medium", "high");
|
|
29
|
-
if (
|
|
30
|
-
reasoningRange.push("
|
|
30
|
+
if (reasoning === "xhigh")
|
|
31
|
+
reasoningRange.push("xhigh");
|
|
31
32
|
}
|
|
32
33
|
}
|
|
33
34
|
addCustomModel(modelId, provider, { contextWindow, reasoning, reasoningRange });
|
|
@@ -50,7 +51,7 @@ export function modelCommand(parts, ctx) {
|
|
|
50
51
|
process.stderr.write(`${DIM}Provider not configured. Start with --provider to set one.${RESET}\n`);
|
|
51
52
|
return true;
|
|
52
53
|
}
|
|
53
|
-
showModelPicker(ctx.providerName, ctx.currentModel, process.stdout).then((result) => {
|
|
54
|
+
showModelPicker(ctx.providerName, ctx.currentModel, ctx.currentReasoning, process.stdout).then((result) => {
|
|
54
55
|
if (result && ctx.onModelChange) {
|
|
55
56
|
ctx.onModelChange(result);
|
|
56
57
|
const reasoningLabel = result.reasoning ? ` (reasoning: ${result.reasoning})` : "";
|
package/dist/commands/session.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { estimateMessageTokens } from "../context/token-counter.js";
|
|
2
2
|
import { pruneMessages } from "../context/pruner.js";
|
|
3
3
|
import { renderMarkdown } from "../multi/markdown.js";
|
|
4
|
-
import { saveSessionMessages } from "../memory/session.js";
|
|
4
|
+
import { saveSessionMessages, loadLastSessionSnapshot } from "../memory/session.js";
|
|
5
5
|
import { execSync } from "node:child_process";
|
|
6
6
|
import * as fs from "node:fs";
|
|
7
7
|
import * as path from "node:path";
|
|
@@ -41,7 +41,7 @@ export function sessionCommand(parts, ctx) {
|
|
|
41
41
|
return true;
|
|
42
42
|
}
|
|
43
43
|
try {
|
|
44
|
-
saveSessionMessages(ctx.phrenPath, ctx.sessionId, ctx.session.messages);
|
|
44
|
+
saveSessionMessages(ctx.phrenPath, ctx.sessionId, ctx.session.messages, ctx.phrenCtx?.project ?? undefined);
|
|
45
45
|
process.stderr.write(`${GREEN}-> Checkpoint saved (${ctx.session.messages.length} messages)${RESET}\n`);
|
|
46
46
|
}
|
|
47
47
|
catch (err) {
|
|
@@ -181,6 +181,28 @@ export function diffCommand(parts, _ctx) {
|
|
|
181
181
|
}
|
|
182
182
|
return true;
|
|
183
183
|
}
|
|
184
|
+
export function resumeCommand(_parts, ctx) {
|
|
185
|
+
if (!ctx.phrenPath) {
|
|
186
|
+
process.stderr.write(`${DIM}No phren context — cannot resume.${RESET}\n`);
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
const project = ctx.phrenCtx?.project;
|
|
190
|
+
const snapshot = loadLastSessionSnapshot(ctx.phrenPath, project ?? undefined);
|
|
191
|
+
if (!snapshot) {
|
|
192
|
+
process.stderr.write(`${DIM}No previous session to resume.${RESET}\n`);
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
// Load prior messages into current session (cast from serialized format)
|
|
196
|
+
const priorCount = snapshot.messages.length;
|
|
197
|
+
ctx.session.messages = snapshot.messages;
|
|
198
|
+
process.stderr.write(`${GREEN}-> Resumed ${priorCount} messages from session ${snapshot.sessionId}${RESET}\n`);
|
|
199
|
+
if (snapshot.project) {
|
|
200
|
+
process.stderr.write(`${DIM} Project: ${snapshot.project}${RESET}\n`);
|
|
201
|
+
}
|
|
202
|
+
process.stderr.write(`${DIM} Saved: ${snapshot.savedAt}${RESET}\n`);
|
|
203
|
+
process.stderr.write(`${DIM} Use /history to review. The LLM will see the full prior conversation on your next message.${RESET}\n`);
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
184
206
|
export function gitCommand(parts, _ctx) {
|
|
185
207
|
const sub = parts.slice(1).join(" ").trim();
|
|
186
208
|
if (!sub) {
|
package/dist/commands.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
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 } from "./commands/session.js";
|
|
3
|
+
import { sessionCommand, historyCommand, compactCommand, diffCommand, gitCommand, resumeCommand } from "./commands/session.js";
|
|
4
4
|
import { memCommand, askCommand } from "./commands/memory.js";
|
|
5
5
|
import { modelCommand, providerCommand, presetCommand } from "./commands/model.js";
|
|
6
|
+
import { loadInputMode, saveInputMode, savePermissionMode } from "./settings.js";
|
|
6
7
|
const DIM = "\x1b[2m";
|
|
7
8
|
const RESET = "\x1b[0m";
|
|
8
9
|
export function createCommandContext(session, contextLimit) {
|
|
@@ -12,6 +13,17 @@ export function createCommandContext(session, contextLimit) {
|
|
|
12
13
|
undoStack: [],
|
|
13
14
|
};
|
|
14
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* All slash commands available in the agent, including TUI-only commands.
|
|
18
|
+
* Used for tab completion in the TUI.
|
|
19
|
+
*/
|
|
20
|
+
export const COMMAND_NAMES = [
|
|
21
|
+
"/help", "/turns", "/clear", "/cwd", "/files", "/cost", "/plan", "/undo",
|
|
22
|
+
"/context", "/model", "/provider", "/preset", "/session", "/history",
|
|
23
|
+
"/compact", "/diff", "/git", "/mem", "/ask", "/resume", "/spawn", "/agents",
|
|
24
|
+
"/mode", "/permissions", "/verbose", "/theme", "/agent",
|
|
25
|
+
"/exit", "/quit", "/q",
|
|
26
|
+
];
|
|
15
27
|
/**
|
|
16
28
|
* Try to handle a slash command. Returns true if the input was a command.
|
|
17
29
|
* Returns a Promise<boolean> for async commands like /ask.
|
|
@@ -39,6 +51,29 @@ export function handleCommand(input, ctx) {
|
|
|
39
51
|
case "/git": return gitCommand(parts, ctx);
|
|
40
52
|
case "/mem": return memCommand(parts, ctx);
|
|
41
53
|
case "/ask": return askCommand(parts, ctx);
|
|
54
|
+
case "/resume": return resumeCommand(parts, ctx);
|
|
55
|
+
case "/mode": {
|
|
56
|
+
const current = loadInputMode();
|
|
57
|
+
const newMode = current === "steering" ? "queue" : "steering";
|
|
58
|
+
saveInputMode(newMode);
|
|
59
|
+
process.stderr.write(`${DIM}Input mode: ${newMode}${RESET}\n`);
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
case "/permissions": {
|
|
63
|
+
const VALID_MODES = ["suggest", "auto-confirm", "full-auto"];
|
|
64
|
+
const arg = parts[1];
|
|
65
|
+
if (!ctx.registry || !arg || !VALID_MODES.includes(arg)) {
|
|
66
|
+
const current = ctx.registry?.permissionConfig.mode ?? "unknown";
|
|
67
|
+
process.stderr.write(`${DIM}Permission mode: ${current}${RESET}\n`);
|
|
68
|
+
process.stderr.write(`${DIM}Usage: /permissions <suggest|auto-confirm|full-auto>${RESET}\n`);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
ctx.registry.setPermissions({ ...ctx.registry.permissionConfig, mode: arg });
|
|
72
|
+
savePermissionMode(arg);
|
|
73
|
+
process.stderr.write(`${DIM}Permission mode: ${arg}${RESET}\n`);
|
|
74
|
+
}
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
42
77
|
case "/spawn": {
|
|
43
78
|
if (!ctx.spawner) {
|
|
44
79
|
process.stderr.write(`${DIM}Spawner not available. Start with --multi or --team to enable.${RESET}\n`);
|
package/dist/config.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
const HELP = `
|
|
2
|
-
phren
|
|
2
|
+
phren — coding agent with persistent memory
|
|
3
3
|
|
|
4
|
-
Usage: phren
|
|
4
|
+
Usage: phren [options] <task>
|
|
5
5
|
|
|
6
6
|
Options:
|
|
7
|
-
--provider <name> Force provider: openrouter, anthropic, openai, codex, ollama
|
|
7
|
+
--provider <name> Force provider: openrouter, anthropic, openai, openai-codex, ollama
|
|
8
8
|
--model <model> Override LLM model
|
|
9
|
+
--reasoning <level> Reasoning effort: low, medium, high, xhigh
|
|
9
10
|
--project <name> Force phren project context
|
|
10
11
|
--max-turns <n> Max tool-use turns (default: 50)
|
|
11
12
|
--max-output <n> Max output tokens per response (default: auto per model)
|
|
@@ -27,22 +28,25 @@ Options:
|
|
|
27
28
|
--help Show this help
|
|
28
29
|
|
|
29
30
|
Providers (auto-detected from env, or use --provider):
|
|
30
|
-
|
|
31
|
-
anthropic ANTHROPIC_API_KEY — Claude direct
|
|
32
|
-
openai OPENAI_API_KEY — OpenAI direct
|
|
33
|
-
codex Uses your ChatGPT/Codex subscription directly
|
|
31
|
+
openai-codex Uses your ChatGPT/Codex subscription directly (preferred default)
|
|
34
32
|
(no API key needed, flat rate via your subscription)
|
|
35
|
-
Setup: phren
|
|
33
|
+
Setup: phren auth login
|
|
34
|
+
Legacy alias: codex
|
|
35
|
+
openai OPENAI_API_KEY — OpenAI direct (defaults to gpt-5.4)
|
|
36
|
+
openrouter OPENROUTER_API_KEY — routes to any model
|
|
37
|
+
anthropic ANTHROPIC_API_KEY — Claude direct
|
|
36
38
|
ollama PHREN_OLLAMA_URL — local models (default: localhost:11434)
|
|
37
39
|
|
|
38
40
|
Environment:
|
|
39
41
|
PHREN_AGENT_PROVIDER Force provider via env
|
|
40
42
|
PHREN_AGENT_MODEL Override model via env
|
|
43
|
+
PHREN_AGENT_REASONING Override reasoning effort via env
|
|
41
44
|
|
|
42
45
|
Examples:
|
|
43
|
-
phren
|
|
44
|
-
phren
|
|
45
|
-
phren
|
|
46
|
+
phren "fix the login bug"
|
|
47
|
+
phren --provider openai-codex "add input validation"
|
|
48
|
+
phren --model openai-codex/gpt-5.4 --reasoning high "add input validation"
|
|
49
|
+
phren --provider anthropic --verbose "refactor the database layer"
|
|
46
50
|
`.trim();
|
|
47
51
|
export function parseArgs(argv) {
|
|
48
52
|
const args = {
|
|
@@ -109,6 +113,12 @@ export function parseArgs(argv) {
|
|
|
109
113
|
else if (arg === "--model" && argv[i + 1]) {
|
|
110
114
|
args.model = argv[++i];
|
|
111
115
|
}
|
|
116
|
+
else if (arg === "--reasoning" && argv[i + 1]) {
|
|
117
|
+
const value = argv[++i]?.toLowerCase();
|
|
118
|
+
if (value === "low" || value === "medium" || value === "high" || value === "xhigh" || value === "max") {
|
|
119
|
+
args.reasoning = value === "max" ? "xhigh" : value;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
112
122
|
else if (arg === "--project" && argv[i + 1]) {
|
|
113
123
|
args.project = argv[++i];
|
|
114
124
|
}
|
|
@@ -140,6 +150,12 @@ export function parseArgs(argv) {
|
|
|
140
150
|
if (!args.model && process.env.PHREN_AGENT_MODEL) {
|
|
141
151
|
args.model = process.env.PHREN_AGENT_MODEL;
|
|
142
152
|
}
|
|
153
|
+
if (!args.reasoning && process.env.PHREN_AGENT_REASONING) {
|
|
154
|
+
const value = process.env.PHREN_AGENT_REASONING.toLowerCase();
|
|
155
|
+
if (value === "low" || value === "medium" || value === "high" || value === "xhigh" || value === "max") {
|
|
156
|
+
args.reasoning = value === "max" ? "xhigh" : value;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
143
159
|
return args;
|
|
144
160
|
}
|
|
145
161
|
export function printHelp() {
|
package/dist/cost.js
CHANGED
|
@@ -1,81 +1,13 @@
|
|
|
1
1
|
/** Cost tracking for LLM API usage. */
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
["claude-opus-4", { inputPer1M: 15, outputPer1M: 75 }],
|
|
6
|
-
["claude-sonnet-4", { inputPer1M: 3, outputPer1M: 15 }],
|
|
7
|
-
["claude-haiku-4", { inputPer1M: 0.80, outputPer1M: 4 }],
|
|
8
|
-
["claude-3-5-sonnet", { inputPer1M: 3, outputPer1M: 15 }],
|
|
9
|
-
["claude-3-5-haiku", { inputPer1M: 0.80, outputPer1M: 4 }],
|
|
10
|
-
["claude-3-opus", { inputPer1M: 15, outputPer1M: 75 }],
|
|
11
|
-
// OpenAI
|
|
12
|
-
["gpt-5", { inputPer1M: 2.50, outputPer1M: 10 }],
|
|
13
|
-
["gpt-4.1", { inputPer1M: 2, outputPer1M: 8 }],
|
|
14
|
-
["gpt-4o", { inputPer1M: 2.50, outputPer1M: 10 }],
|
|
15
|
-
["gpt-4-turbo", { inputPer1M: 10, outputPer1M: 30 }],
|
|
16
|
-
["gpt-4", { inputPer1M: 30, outputPer1M: 60 }],
|
|
17
|
-
["o3", { inputPer1M: 2, outputPer1M: 8 }],
|
|
18
|
-
["o4-mini", { inputPer1M: 1.10, outputPer1M: 4.40 }],
|
|
19
|
-
// OpenRouter prefixed
|
|
20
|
-
["anthropic/claude-opus-4", { inputPer1M: 15, outputPer1M: 75 }],
|
|
21
|
-
["anthropic/claude-sonnet-4", { inputPer1M: 3, outputPer1M: 15 }],
|
|
22
|
-
["anthropic/claude-haiku-4", { inputPer1M: 0.80, outputPer1M: 4 }],
|
|
23
|
-
["openai/gpt-4o", { inputPer1M: 2.50, outputPer1M: 10 }],
|
|
24
|
-
// Local (free)
|
|
25
|
-
["ollama", { inputPer1M: 0, outputPer1M: 0 }],
|
|
26
|
-
];
|
|
27
|
-
PRICING.sort((a, b) => b[0].length - a[0].length); // longest prefix first
|
|
28
|
-
// Max output token limits per model — prefix match (most specific wins)
|
|
29
|
-
const OUTPUT_LIMITS = [
|
|
30
|
-
// Anthropic
|
|
31
|
-
["claude-opus-4", 32768],
|
|
32
|
-
["claude-sonnet-4", 16384],
|
|
33
|
-
["claude-haiku-4", 8192],
|
|
34
|
-
["claude-3-5-sonnet", 8192],
|
|
35
|
-
["claude-3-5-haiku", 8192],
|
|
36
|
-
["claude-3-opus", 4096],
|
|
37
|
-
// OpenAI
|
|
38
|
-
["gpt-5", 16384],
|
|
39
|
-
["gpt-4.1", 32768],
|
|
40
|
-
["gpt-4o", 16384],
|
|
41
|
-
["gpt-4-turbo", 4096],
|
|
42
|
-
["o3", 100000],
|
|
43
|
-
["o4-mini", 100000],
|
|
44
|
-
// OpenRouter prefixed
|
|
45
|
-
["anthropic/claude-opus-4", 32768],
|
|
46
|
-
["anthropic/claude-sonnet-4", 16384],
|
|
47
|
-
["anthropic/claude-haiku-4", 8192],
|
|
48
|
-
["openai/gpt-4o", 16384],
|
|
49
|
-
// Codex
|
|
50
|
-
["gpt-5.2-codex", 16384],
|
|
51
|
-
// Local
|
|
52
|
-
["qwen", 8192],
|
|
53
|
-
];
|
|
54
|
-
OUTPUT_LIMITS.sort((a, b) => b[0].length - a[0].length); // longest prefix first
|
|
55
|
-
export function lookupMaxOutputTokens(model) {
|
|
56
|
-
const lower = model.toLowerCase();
|
|
57
|
-
for (const [prefix, limit] of OUTPUT_LIMITS) {
|
|
58
|
-
if (lower.startsWith(prefix))
|
|
59
|
-
return limit;
|
|
60
|
-
}
|
|
61
|
-
return 8192; // default
|
|
62
|
-
}
|
|
63
|
-
function lookupPricing(model) {
|
|
64
|
-
const lower = model.toLowerCase();
|
|
65
|
-
for (const [prefix, pricing] of PRICING) {
|
|
66
|
-
if (lower.startsWith(prefix))
|
|
67
|
-
return pricing;
|
|
68
|
-
}
|
|
69
|
-
// Default fallback — assume mid-tier pricing
|
|
70
|
-
return { inputPer1M: 3, outputPer1M: 15 };
|
|
71
|
-
}
|
|
72
|
-
export function createCostTracker(model, budget = null) {
|
|
73
|
-
const pricing = lookupPricing(model);
|
|
2
|
+
import { lookupPricing } from "./models.js";
|
|
3
|
+
export function createCostTracker(model, budget = null, provider) {
|
|
4
|
+
const { pricing, metered } = lookupPricing(model, provider);
|
|
74
5
|
const tracker = {
|
|
75
6
|
totalInputTokens: 0,
|
|
76
7
|
totalOutputTokens: 0,
|
|
77
8
|
totalCost: 0,
|
|
78
9
|
budget,
|
|
10
|
+
metered,
|
|
79
11
|
recordUsage(inputTokens, outputTokens) {
|
|
80
12
|
tracker.totalInputTokens += inputTokens;
|
|
81
13
|
tracker.totalOutputTokens += outputTokens;
|
|
@@ -84,17 +16,22 @@ export function createCostTracker(model, budget = null) {
|
|
|
84
16
|
(outputTokens / 1_000_000) * pricing.outputPer1M;
|
|
85
17
|
},
|
|
86
18
|
isOverBudget() {
|
|
87
|
-
return budget !== null && tracker.totalCost >= budget;
|
|
19
|
+
return tracker.metered && budget !== null && tracker.totalCost >= budget;
|
|
88
20
|
},
|
|
89
21
|
formatCost() {
|
|
22
|
+
const tokens = `${tracker.totalInputTokens + tracker.totalOutputTokens} tokens`;
|
|
23
|
+
if (!tracker.metered) {
|
|
24
|
+
return `included (${tokens})`;
|
|
25
|
+
}
|
|
90
26
|
const cost = tracker.totalCost < 0.01
|
|
91
27
|
? `$${tracker.totalCost.toFixed(4)}`
|
|
92
28
|
: `$${tracker.totalCost.toFixed(2)}`;
|
|
93
|
-
const tokens = `${tracker.totalInputTokens + tracker.totalOutputTokens} tokens`;
|
|
94
29
|
const budgetStr = budget !== null ? ` / $${budget.toFixed(2)} budget` : "";
|
|
95
30
|
return `${cost} (${tokens}${budgetStr})`;
|
|
96
31
|
},
|
|
97
32
|
formatTurnCost(inputTokens, outputTokens) {
|
|
33
|
+
if (!tracker.metered)
|
|
34
|
+
return "included";
|
|
98
35
|
const turnCost = (inputTokens / 1_000_000) * pricing.inputPer1M +
|
|
99
36
|
(outputTokens / 1_000_000) * pricing.outputPer1M;
|
|
100
37
|
return turnCost < 0.01
|
package/dist/index.js
CHANGED
|
@@ -5,18 +5,20 @@ import { ToolRegistry } from "./tools/registry.js";
|
|
|
5
5
|
import { readFileTool } from "./tools/read-file.js";
|
|
6
6
|
import { writeFileTool } from "./tools/write-file.js";
|
|
7
7
|
import { editFileTool } from "./tools/edit-file.js";
|
|
8
|
-
import { shellTool } from "./tools/shell.js";
|
|
8
|
+
import { shellTool, taskOutputTool, taskStopTool } from "./tools/shell.js";
|
|
9
9
|
import { globTool } from "./tools/glob.js";
|
|
10
10
|
import { grepTool } from "./tools/grep.js";
|
|
11
11
|
import { createWebFetchTool } from "./tools/web-fetch.js";
|
|
12
12
|
import { createWebSearchTool } from "./tools/web-search.js";
|
|
13
13
|
import { createPhrenAddTaskTool } from "./tools/phren-add-task.js";
|
|
14
|
+
import { createSkillTool } from "./tools/skill.js";
|
|
14
15
|
import { createPhrenSearchTool } from "./tools/phren-search.js";
|
|
15
16
|
import { createPhrenFindingTool } from "./tools/phren-finding.js";
|
|
16
17
|
import { createPhrenGetTasksTool, createPhrenCompleteTaskTool } from "./tools/phren-tasks.js";
|
|
17
18
|
import { gitStatusTool, gitDiffTool, gitCommitTool } from "./tools/git.js";
|
|
19
|
+
import { listMcpResourcesTool, readMcpResourceTool } from "./tools/mcp-resources.js";
|
|
18
20
|
import { buildPhrenContext, buildContextSnippet } from "./memory/context.js";
|
|
19
|
-
import { startSession, endSession, getPriorSummary, saveSessionMessages,
|
|
21
|
+
import { startSession, endSession, getPriorSummary, saveSessionMessages, loadLastSessionSnapshot } from "./memory/session.js";
|
|
20
22
|
import { loadProjectContext, evolveProjectContext } from "./memory/project-context.js";
|
|
21
23
|
import { buildSystemPrompt } from "./system-prompt.js";
|
|
22
24
|
import { runAgent, createSession, runTurn } from "./agent-loop.js";
|
|
@@ -25,7 +27,35 @@ import { codexLogin, codexLogout } from "./providers/codex-auth.js";
|
|
|
25
27
|
import { createCheckpoint } from "./checkpoint.js";
|
|
26
28
|
import { detectLintCommand, detectTestCommand } from "./tools/lint-test.js";
|
|
27
29
|
import { connectMcpServers, loadMcpConfig, parseMcpInline } from "./mcp-client.js";
|
|
28
|
-
|
|
30
|
+
import { VERSION } from "./package-metadata.js";
|
|
31
|
+
import { authProfilesPath, getAuthStatusEntries, removeApiKeyProfile, upsertApiKeyProfile, } from "@phren/cli/auth/profiles";
|
|
32
|
+
function parseApiKeyProvider(raw) {
|
|
33
|
+
if (raw === "openai" || raw === "openrouter" || raw === "anthropic")
|
|
34
|
+
return raw;
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
function envVarForApiProvider(provider) {
|
|
38
|
+
switch (provider) {
|
|
39
|
+
case "openai":
|
|
40
|
+
return "OPENAI_API_KEY";
|
|
41
|
+
case "openrouter":
|
|
42
|
+
return "OPENROUTER_API_KEY";
|
|
43
|
+
case "anthropic":
|
|
44
|
+
return "ANTHROPIC_API_KEY";
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function printAuthStatus() {
|
|
48
|
+
const entries = getAuthStatusEntries();
|
|
49
|
+
console.log("phren auth");
|
|
50
|
+
console.log(`store: ${authProfilesPath()}`);
|
|
51
|
+
console.log("");
|
|
52
|
+
for (const entry of entries) {
|
|
53
|
+
const status = entry.configured ? "configured" : "not configured";
|
|
54
|
+
const source = entry.source === "none" ? "" : ` via ${entry.source}`;
|
|
55
|
+
const account = entry.accountId ? ` account=${entry.accountId}` : "";
|
|
56
|
+
console.log(`- ${entry.provider}: ${status}${source}${account}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
29
59
|
/**
|
|
30
60
|
* Run the agent CLI with the given argv tokens.
|
|
31
61
|
* Called from `phren agent ...` or directly via `phren-agent ...`.
|
|
@@ -41,7 +71,36 @@ export async function runAgentCli(raw) {
|
|
|
41
71
|
codexLogout();
|
|
42
72
|
process.exit(0);
|
|
43
73
|
}
|
|
44
|
-
|
|
74
|
+
if (raw[1] === "status") {
|
|
75
|
+
printAuthStatus();
|
|
76
|
+
process.exit(0);
|
|
77
|
+
}
|
|
78
|
+
if (raw[1] === "set-key") {
|
|
79
|
+
const provider = parseApiKeyProvider(raw[2]);
|
|
80
|
+
if (!provider) {
|
|
81
|
+
console.error("Usage: phren auth set-key <openai|openrouter|anthropic> [key]");
|
|
82
|
+
process.exit(1);
|
|
83
|
+
}
|
|
84
|
+
const key = raw[3] || process.env[envVarForApiProvider(provider)];
|
|
85
|
+
if (!key) {
|
|
86
|
+
console.error(`No API key provided. Pass it as an argument or set ${envVarForApiProvider(provider)}.`);
|
|
87
|
+
process.exit(1);
|
|
88
|
+
}
|
|
89
|
+
upsertApiKeyProfile(provider, key);
|
|
90
|
+
console.log(`Saved ${provider} API key profile to ${authProfilesPath()}`);
|
|
91
|
+
process.exit(0);
|
|
92
|
+
}
|
|
93
|
+
if (raw[1] === "clear-key") {
|
|
94
|
+
const provider = parseApiKeyProvider(raw[2]);
|
|
95
|
+
if (!provider) {
|
|
96
|
+
console.error("Usage: phren auth clear-key <openai|openrouter|anthropic>");
|
|
97
|
+
process.exit(1);
|
|
98
|
+
}
|
|
99
|
+
const removed = removeApiKeyProfile(provider);
|
|
100
|
+
console.log(removed ? `Removed ${provider} API key profile.` : `No ${provider} API key profile was set.`);
|
|
101
|
+
process.exit(0);
|
|
102
|
+
}
|
|
103
|
+
console.error("Usage: phren auth <login|logout|status|set-key|clear-key>");
|
|
45
104
|
process.exit(1);
|
|
46
105
|
}
|
|
47
106
|
const args = parseArgs(raw);
|
|
@@ -60,7 +119,7 @@ export async function runAgentCli(raw) {
|
|
|
60
119
|
// Resolve LLM provider
|
|
61
120
|
let provider;
|
|
62
121
|
try {
|
|
63
|
-
provider = resolveProvider(args.provider, args.model, args.maxOutput);
|
|
122
|
+
provider = resolveProvider(args.provider, args.model, args.maxOutput, args.reasoning);
|
|
64
123
|
}
|
|
65
124
|
catch (err) {
|
|
66
125
|
console.error(err instanceof Error ? err.message : String(err));
|
|
@@ -110,6 +169,8 @@ export async function runAgentCli(raw) {
|
|
|
110
169
|
registry.register(writeFileTool);
|
|
111
170
|
registry.register(editFileTool);
|
|
112
171
|
registry.register(shellTool);
|
|
172
|
+
registry.register(taskOutputTool);
|
|
173
|
+
registry.register(taskStopTool);
|
|
113
174
|
registry.register(globTool);
|
|
114
175
|
registry.register(grepTool);
|
|
115
176
|
if (phrenCtx) {
|
|
@@ -118,6 +179,7 @@ export async function runAgentCli(raw) {
|
|
|
118
179
|
registry.register(createPhrenGetTasksTool(phrenCtx));
|
|
119
180
|
registry.register(createPhrenCompleteTaskTool(phrenCtx, sessionId));
|
|
120
181
|
registry.register(createPhrenAddTaskTool(phrenCtx, sessionId));
|
|
182
|
+
registry.register(createSkillTool(phrenCtx));
|
|
121
183
|
}
|
|
122
184
|
// Web tools
|
|
123
185
|
registry.register(createWebFetchTool());
|
|
@@ -125,6 +187,8 @@ export async function runAgentCli(raw) {
|
|
|
125
187
|
registry.register(gitStatusTool);
|
|
126
188
|
registry.register(gitDiffTool);
|
|
127
189
|
registry.register(gitCommitTool);
|
|
190
|
+
registry.register(listMcpResourcesTool);
|
|
191
|
+
registry.register(readMcpResourceTool);
|
|
128
192
|
// MCP server connections
|
|
129
193
|
let mcpCleanup;
|
|
130
194
|
const mcpServers = {};
|
|
@@ -142,8 +206,8 @@ export async function runAgentCli(raw) {
|
|
|
142
206
|
registry.register(tool);
|
|
143
207
|
}
|
|
144
208
|
// Build cost tracker from model info
|
|
145
|
-
const modelName = args.model ?? provider.name;
|
|
146
|
-
const costTracker = createCostTracker(modelName, args.budget);
|
|
209
|
+
const modelName = provider.model ?? args.model ?? provider.name;
|
|
210
|
+
const costTracker = createCostTracker(modelName, args.budget, provider.name);
|
|
147
211
|
// Build lint/test config from CLI flags or auto-detect
|
|
148
212
|
const cwd = process.cwd();
|
|
149
213
|
const lintCmd = args.lintCmd ?? detectLintCommand(cwd);
|
|
@@ -167,33 +231,23 @@ export async function runAgentCli(raw) {
|
|
|
167
231
|
lintTestConfig,
|
|
168
232
|
sessionId,
|
|
169
233
|
};
|
|
170
|
-
//
|
|
171
|
-
if (args.multi || args.team) {
|
|
172
|
-
const { AgentSpawner } = await import("./multi/spawner.js");
|
|
173
|
-
const { startMultiTui } = await import("./multi/tui-multi.js");
|
|
174
|
-
const spawner = new AgentSpawner();
|
|
175
|
-
process.on("SIGINT", async () => {
|
|
176
|
-
await spawner.shutdown();
|
|
177
|
-
process.exit(130);
|
|
178
|
-
});
|
|
179
|
-
await startMultiTui(spawner, agentConfig);
|
|
180
|
-
await spawner.shutdown();
|
|
181
|
-
if (phrenCtx && sessionId) {
|
|
182
|
-
endSession(phrenCtx, sessionId, "Multi-agent session ended");
|
|
183
|
-
}
|
|
184
|
-
mcpCleanup?.();
|
|
185
|
-
return;
|
|
186
|
-
}
|
|
187
|
-
// Interactive mode — Ink TUI if available, legacy TUI fallback, REPL if not TTY
|
|
188
|
-
if (args.interactive) {
|
|
234
|
+
// Interactive mode — Ink TUI with built-in spawner (--multi and --team also route here)
|
|
235
|
+
if (args.interactive || args.multi || args.team) {
|
|
189
236
|
const isTTY = process.stdout.isTTY && process.stdin.isTTY;
|
|
190
237
|
let session;
|
|
191
238
|
if (!isTTY) {
|
|
192
239
|
session = await (await import("./repl.js")).startRepl(agentConfig);
|
|
193
240
|
}
|
|
194
241
|
else {
|
|
195
|
-
// Ink TUI
|
|
196
|
-
|
|
242
|
+
// Ink TUI with spawner — LLM can spawn agents via spawn_agent tool
|
|
243
|
+
const { AgentSpawner } = await import("./multi/spawner.js");
|
|
244
|
+
const { createSpawnAgentTool, createSendMessageTool, createListAgentsTool } = await import("./tools/spawn-agent.js");
|
|
245
|
+
const spawner = new AgentSpawner();
|
|
246
|
+
registry.register(createSpawnAgentTool(spawner));
|
|
247
|
+
registry.register(createSendMessageTool(spawner));
|
|
248
|
+
registry.register(createListAgentsTool(spawner));
|
|
249
|
+
session = await (await import("./tui/ink-entry.js")).startInkTui(agentConfig, spawner);
|
|
250
|
+
await spawner.shutdown();
|
|
197
251
|
}
|
|
198
252
|
// Flush anti-patterns at session end
|
|
199
253
|
if (phrenCtx) {
|
|
@@ -209,6 +263,7 @@ export async function runAgentCli(raw) {
|
|
|
209
263
|
if (phrenCtx && sessionId) {
|
|
210
264
|
const lastText = session.messages.length > 0 ? "Interactive session ended" : "Empty session";
|
|
211
265
|
endSession(phrenCtx, sessionId, lastText);
|
|
266
|
+
saveSessionMessages(phrenCtx.phrenPath, sessionId, session.messages, phrenCtx.project ?? undefined);
|
|
212
267
|
}
|
|
213
268
|
mcpCleanup?.();
|
|
214
269
|
return;
|
|
@@ -238,16 +293,18 @@ export async function runAgentCli(raw) {
|
|
|
238
293
|
let result;
|
|
239
294
|
if (args.resume && phrenCtx) {
|
|
240
295
|
// Resume: load previous messages and continue
|
|
241
|
-
const
|
|
242
|
-
if (
|
|
243
|
-
if (args.verbose)
|
|
244
|
-
|
|
296
|
+
const priorSnapshot = loadLastSessionSnapshot(phrenCtx.phrenPath, phrenCtx.project ?? undefined);
|
|
297
|
+
if (priorSnapshot && priorSnapshot.messages.length > 0) {
|
|
298
|
+
if (args.verbose) {
|
|
299
|
+
const projectLabel = priorSnapshot.project ?? "global";
|
|
300
|
+
process.stderr.write(`Resuming session ${priorSnapshot.sessionId.slice(0, 8)} (${projectLabel}) with ${priorSnapshot.messages.length} messages\n`);
|
|
301
|
+
}
|
|
245
302
|
const contextLimit = provider.contextWindow ?? 200_000;
|
|
246
303
|
const session = createSession(contextLimit);
|
|
247
|
-
session.messages =
|
|
304
|
+
session.messages = priorSnapshot.messages;
|
|
248
305
|
const turnResult = await runTurn("Continuing where we left off. Please review the conversation and continue with the task.", session, agentConfig);
|
|
249
306
|
// Save messages for future resume
|
|
250
|
-
saveSessionMessages(phrenCtx.phrenPath, sessionId, session.messages);
|
|
307
|
+
saveSessionMessages(phrenCtx.phrenPath, sessionId, session.messages, phrenCtx.project ?? undefined);
|
|
251
308
|
result = {
|
|
252
309
|
finalText: turnResult.text,
|
|
253
310
|
turns: turnResult.turns,
|
|
@@ -274,7 +331,7 @@ export async function runAgentCli(raw) {
|
|
|
274
331
|
const summary = result.finalText.slice(0, 500);
|
|
275
332
|
endSession(phrenCtx, sessionId, summary);
|
|
276
333
|
// Save messages for resume
|
|
277
|
-
saveSessionMessages(phrenCtx.phrenPath, sessionId, result.messages);
|
|
334
|
+
saveSessionMessages(phrenCtx.phrenPath, sessionId, result.messages, phrenCtx.project ?? undefined);
|
|
278
335
|
// Evolve project context via lightweight LLM reflection
|
|
279
336
|
try {
|
|
280
337
|
await evolveProjectContext(phrenCtx, provider, result.messages);
|
package/dist/mcp-client.js
CHANGED
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
import { spawn } from "child_process";
|
|
6
6
|
import * as fs from "fs";
|
|
7
7
|
import * as readline from "readline";
|
|
8
|
+
import { VERSION } from "./package-metadata.js";
|
|
9
|
+
import { scrubEnv } from "./permissions/shell-safety.js";
|
|
8
10
|
/** Active MCP server connection. */
|
|
9
11
|
class McpConnection {
|
|
10
12
|
proc;
|
|
@@ -14,7 +16,7 @@ class McpConnection {
|
|
|
14
16
|
name;
|
|
15
17
|
constructor(name, config) {
|
|
16
18
|
this.name = name;
|
|
17
|
-
const env = { ...
|
|
19
|
+
const env = { ...scrubEnv(), ...config.env };
|
|
18
20
|
this.proc = spawn(config.command, config.args ?? [], {
|
|
19
21
|
stdio: ["pipe", "pipe", "pipe"],
|
|
20
22
|
env,
|
|
@@ -61,7 +63,7 @@ class McpConnection {
|
|
|
61
63
|
await this.send("initialize", {
|
|
62
64
|
protocolVersion: "2024-11-05",
|
|
63
65
|
capabilities: {},
|
|
64
|
-
clientInfo: { name: "phren-agent", version:
|
|
66
|
+
clientInfo: { name: "phren-agent", version: VERSION },
|
|
65
67
|
});
|
|
66
68
|
// Send initialized notification (no id)
|
|
67
69
|
this.proc.stdin.write(JSON.stringify({ jsonrpc: "2.0", method: "notifications/initialized" }) + "\n");
|