@phren/agent 0.3.20 → 0.3.21
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/agent-loop/index.js +113 -19
- package/dist/agent-loop/stream.js +3 -1
- package/dist/attach-images.js +57 -0
- package/dist/commands/info.js +6 -2
- package/dist/commands/model.js +55 -13
- package/dist/commands/session.js +79 -18
- package/dist/commands.js +9 -2
- package/dist/config.js +45 -0
- package/dist/context/clear-tool-results.js +43 -0
- package/dist/context/compactor.js +8 -2
- package/dist/context/usage.js +26 -0
- package/dist/cost.js +10 -3
- package/dist/headless.js +41 -0
- package/dist/index.js +180 -56
- package/dist/mcp-client.js +38 -1
- package/dist/mcp-prompts.js +94 -0
- package/dist/multi/child-entry.js +3 -1
- package/dist/multi/spawner.js +27 -15
- package/dist/permissions/allowlist.js +19 -4
- package/dist/permissions/checker.js +37 -20
- package/dist/permissions/kernel-sandbox.js +32 -10
- package/dist/permissions/rules.js +173 -0
- package/dist/permissions/shell-classify.js +173 -0
- package/dist/plan.js +6 -2
- package/dist/repl.js +16 -1
- package/dist/session/persist.js +9 -4
- package/dist/settings.js +14 -0
- package/dist/structured-output.js +85 -0
- package/dist/system-prompt.js +117 -14
- package/dist/tools/apply-patch.js +15 -1
- package/dist/tools/edit-file.js +8 -1
- package/dist/tools/file-state.js +88 -0
- package/dist/tools/glob.js +53 -35
- package/dist/tools/grep.js +139 -53
- package/dist/tools/lint-test.js +24 -0
- package/dist/tools/read-file.js +2 -0
- package/dist/tools/registry.js +8 -1
- package/dist/tools/search-support.js +141 -0
- package/dist/tools/shell.js +3 -1
- package/dist/tools/spawn-agent.js +15 -3
- package/dist/tools/syntax-check.js +116 -0
- package/dist/tools/write-file.js +11 -3
- package/dist/tui/components/App.js +5 -4
- package/dist/tui/components/Banner.js +1 -1
- package/dist/tui/components/ListPicker.js +32 -0
- package/dist/tui/ink-entry.js +57 -16
- package/dist/user-hooks.js +26 -4
- package/package.json +3 -2
package/dist/agent-loop/index.js
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import { createSpinner, formatTurnHeader, formatToolCall } from "../spinner.js";
|
|
2
|
-
import {
|
|
2
|
+
import { planToolResultClearing } from "../context/clear-tool-results.js";
|
|
3
|
+
import { contextTokens, reportedContext } from "../context/usage.js";
|
|
3
4
|
import { compactWithLlm } from "../context/compactor.js";
|
|
4
5
|
import { estimateMessageTokens } from "../context/token-counter.js";
|
|
5
6
|
import { isContextOverflowError, withRetry } from "../providers/retry.js";
|
|
6
7
|
import { injectPlanPrompt, requestPlanApproval } from "../plan.js";
|
|
8
|
+
import { READ_ONLY_TOOLS } from "../permissions/checker.js";
|
|
9
|
+
import { attachImages } from "../attach-images.js";
|
|
10
|
+
import { modelSupportsVision } from "../models.js";
|
|
11
|
+
import * as path from "path";
|
|
7
12
|
import { detectLintCommand, detectTestCommand } from "../tools/lint-test.js";
|
|
8
13
|
import { createCheckpoint } from "../checkpoint.js";
|
|
9
14
|
import { resetRepeatChain } from "../guards/repeat-tool-reminder.js";
|
|
@@ -16,6 +21,20 @@ export { createSession };
|
|
|
16
21
|
* If the history ends with an assistant message whose tool calls have no
|
|
17
22
|
* results, append a cancelled result for each. Returns how many were closed.
|
|
18
23
|
*/
|
|
24
|
+
/**
|
|
25
|
+
* Up to `max` chars of a check's output: the start (where compilers put the
|
|
26
|
+
* first error) and the end (where test runners put the summary).
|
|
27
|
+
*/
|
|
28
|
+
/** Files a type checker looks at (matched anywhere in a path or patch text). */
|
|
29
|
+
const TYPED_SOURCE = /\.(?:ts|tsx|mts|cts|js|jsx|mjs|cjs|py|pyi)\b/;
|
|
30
|
+
function headAndTail(output, max) {
|
|
31
|
+
if (output.length <= max)
|
|
32
|
+
return output;
|
|
33
|
+
const head = Math.floor(max * 0.4);
|
|
34
|
+
return `${output.slice(0, head)}\n… [${output.length - max} chars cut] …\n${output.slice(output.length - (max - head))}`;
|
|
35
|
+
}
|
|
36
|
+
/** How many times one turn's Stop hooks may send the model back. */
|
|
37
|
+
const MAX_STOP_BLOCKS = 5;
|
|
19
38
|
export function closeDanglingToolUses(session, reason = "Cancelled by user.") {
|
|
20
39
|
const messages = session.messages;
|
|
21
40
|
const last = messages[messages.length - 1];
|
|
@@ -37,6 +56,8 @@ export async function runTurn(userInput, session, config, hooks) {
|
|
|
37
56
|
const { provider, registry, maxTurns, verbose, costTracker } = config;
|
|
38
57
|
let systemPrompt = config.systemPrompt;
|
|
39
58
|
const toolDefs = registry.getDefinitions();
|
|
59
|
+
// While a plan is pending the model may look but not touch.
|
|
60
|
+
const planToolDefs = toolDefs.filter((d) => READ_ONLY_TOOLS.has(d.name));
|
|
40
61
|
const spinner = createSpinner();
|
|
41
62
|
const useStream = typeof provider.chatStream === "function";
|
|
42
63
|
const status = hooks?.onStatus ?? ((msg) => process.stderr.write(msg));
|
|
@@ -52,9 +73,27 @@ export async function runTurn(userInput, session, config, hooks) {
|
|
|
52
73
|
closeDanglingToolUses(session);
|
|
53
74
|
// Direct user input resets the repeat-call chain (repetition across it is not a loop)
|
|
54
75
|
resetRepeatChain(session.repeatChain);
|
|
76
|
+
// UserPromptSubmit hooks may block the prompt (exit 2) or add context to it.
|
|
77
|
+
const hookConfig = config.hookConfig ?? null;
|
|
78
|
+
const promptHooks = hookConfig ? await runLifecycleHooks(hookConfig, "UserPromptSubmit", { prompt: userInput }) : null;
|
|
79
|
+
if (promptHooks?.blocked) {
|
|
80
|
+
(hooks?.onStatus ?? ((msg) => process.stderr.write(msg)))(`\x1b[33m[prompt blocked by a UserPromptSubmit hook: ${promptHooks.reason}]\x1b[0m\n`);
|
|
81
|
+
return { text: "", turns: 0, toolCalls: 0, stopReason: "hook_blocked" };
|
|
82
|
+
}
|
|
83
|
+
const promptText = promptHooks?.context ? `${userInput}\n\n<user-prompt-submit-hook>\n${promptHooks.context}\n</user-prompt-submit-hook>` : userInput;
|
|
84
|
+
// Image paths in the prompt (typed or dropped onto the terminal) go along as images.
|
|
85
|
+
let promptContent = promptText;
|
|
86
|
+
if (modelSupportsVision(provider.name, provider.model ?? "")) {
|
|
87
|
+
const withImages = attachImages(promptText, process.cwd());
|
|
88
|
+
promptContent = withImages.content;
|
|
89
|
+
if (withImages.attached.length > 0)
|
|
90
|
+
status(`\x1b[2m[attached ${withImages.attached.map((f) => path.basename(f)).join(", ")}]\x1b[0m\n`);
|
|
91
|
+
if (withImages.skipped.length > 0)
|
|
92
|
+
status(`\x1b[33m[not attached: ${withImages.skipped.join(", ")}]\x1b[0m\n`);
|
|
93
|
+
}
|
|
55
94
|
// Append user message to the durable log
|
|
56
95
|
const prompted = session.log.append("user/message", {
|
|
57
|
-
message: { role: "user", content:
|
|
96
|
+
message: { role: "user", content: promptContent },
|
|
58
97
|
source: "user",
|
|
59
98
|
turn: session.turns,
|
|
60
99
|
});
|
|
@@ -68,10 +107,9 @@ export async function runTurn(userInput, session, config, hooks) {
|
|
|
68
107
|
// One overflow-driven compaction per model call; reset after a success.
|
|
69
108
|
let overflowRecovered = false;
|
|
70
109
|
const signal = hooks?.signal;
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
110
|
+
// Times a Stop hook sent the model back to work in this turn.
|
|
111
|
+
let stopBlocks = 0;
|
|
112
|
+
let stopHooksRan = false;
|
|
75
113
|
// Why the loop ended; stays "max_turns" if the turn cap runs out.
|
|
76
114
|
let endReason = "max_turns";
|
|
77
115
|
while (session.turns - turnStart < maxTurns) {
|
|
@@ -96,6 +134,8 @@ export async function runTurn(userInput, session, config, hooks) {
|
|
|
96
134
|
// knowledge to phren out of band.)
|
|
97
135
|
const contextLimit = provider.contextWindow ?? 200_000;
|
|
98
136
|
const compactHistory = async (keepRecentTurns, trigger) => {
|
|
137
|
+
if (hookConfig)
|
|
138
|
+
await runLifecycleHooks(hookConfig, "PreCompact", { trigger: "auto" });
|
|
99
139
|
const preCount = session.messages.length;
|
|
100
140
|
const preTokens = estimateMessageTokens(session.messages);
|
|
101
141
|
const result = await compactWithLlm(provider, systemPrompt, session.messages, {
|
|
@@ -106,6 +146,7 @@ export async function runTurn(userInput, session, config, hooks) {
|
|
|
106
146
|
pruneConfig: { contextLimit, keepRecentTurns },
|
|
107
147
|
signal,
|
|
108
148
|
verbose,
|
|
149
|
+
tools: toolDefs,
|
|
109
150
|
});
|
|
110
151
|
if (!result)
|
|
111
152
|
return false;
|
|
@@ -122,11 +163,23 @@ export async function runTurn(userInput, session, config, hooks) {
|
|
|
122
163
|
status(`\x1b[2m[context compacted (${mode}${trigger}): ${preCount} → ${postCount} messages, ~${fmtPre} → ~${fmtPost} tokens, ${reduction}% reduction${routed}]\x1b[0m\n`);
|
|
123
164
|
return true;
|
|
124
165
|
};
|
|
125
|
-
|
|
126
|
-
|
|
166
|
+
// Past 75% of the window (by the provider's own count when there is
|
|
167
|
+
// one), first clear old tool output; compact only if that frees too little.
|
|
168
|
+
const usedTokens = () => contextTokens(systemPrompt, session.messages, session.log, session.reportedContext);
|
|
169
|
+
if (usedTokens() > contextLimit * 0.75) {
|
|
170
|
+
const before = usedTokens();
|
|
171
|
+
const cleared = planToolResultClearing(session.messages);
|
|
172
|
+
for (const { index, message } of cleared)
|
|
173
|
+
session.log.replaceMessageRange(index, index, message);
|
|
174
|
+
const after = usedTokens();
|
|
175
|
+
if (cleared.length > 0) {
|
|
176
|
+
status(`\x1b[2m[cleared ${cleared.length === 1 ? "old tool output in 1 message" : `old tool output in ${cleared.length} messages`}: ~${Math.round(before / 1000)}k → ~${Math.round(after / 1000)}k tokens]\x1b[0m\n`);
|
|
177
|
+
}
|
|
178
|
+
if (after > contextLimit * 0.6)
|
|
179
|
+
await compactHistory(6, "");
|
|
127
180
|
}
|
|
128
181
|
// For plan mode first turn, pass empty tools so LLM can't call any
|
|
129
|
-
const turnTools = planPending ?
|
|
182
|
+
const turnTools = planPending ? planToolDefs : toolDefs;
|
|
130
183
|
// Model-visible means logged: everything the provider is about to see
|
|
131
184
|
// must be reconstructable from the event log. Cheap relative to a model
|
|
132
185
|
// call; opt out with PHREN_AGENT_NO_INVARIANT=1 if it ever matters.
|
|
@@ -136,6 +189,7 @@ export async function runTurn(userInput, session, config, hooks) {
|
|
|
136
189
|
let assistantContent;
|
|
137
190
|
let stopReason;
|
|
138
191
|
let invalidToolCalls = [];
|
|
192
|
+
let usage;
|
|
139
193
|
try {
|
|
140
194
|
if (useStream) {
|
|
141
195
|
// Streaming path — the whole request is retried, not just opening it:
|
|
@@ -175,6 +229,7 @@ export async function runTurn(userInput, session, config, hooks) {
|
|
|
175
229
|
assistantContent = result.content;
|
|
176
230
|
stopReason = result.stop_reason;
|
|
177
231
|
invalidToolCalls = result.invalidToolCalls;
|
|
232
|
+
usage = result.usage;
|
|
178
233
|
}
|
|
179
234
|
else {
|
|
180
235
|
// Batch path
|
|
@@ -184,6 +239,7 @@ export async function runTurn(userInput, session, config, hooks) {
|
|
|
184
239
|
assistantContent = response.content;
|
|
185
240
|
stopReason = response.stop_reason;
|
|
186
241
|
invalidToolCalls = response.invalidToolCalls ?? [];
|
|
242
|
+
usage = response.usage;
|
|
187
243
|
// Track cost from batch response
|
|
188
244
|
if (costTracker && response.usage) {
|
|
189
245
|
recordTokenUsage(costTracker, response.usage);
|
|
@@ -235,6 +291,7 @@ export async function runTurn(userInput, session, config, hooks) {
|
|
|
235
291
|
stop_reason: stopReason,
|
|
236
292
|
turn: session.turns,
|
|
237
293
|
});
|
|
294
|
+
session.reportedContext = reportedContext(usage, session.log) ?? session.reportedContext;
|
|
238
295
|
// Only after the message is in the log, so a reader never sees neither.
|
|
239
296
|
preview?.clear();
|
|
240
297
|
session.turns++;
|
|
@@ -251,8 +308,9 @@ export async function runTurn(userInput, session, config, hooks) {
|
|
|
251
308
|
if (verbose && costTracker) {
|
|
252
309
|
status(`\x1b[2m cost: ${costTracker.formatCost()}\x1b[0m\n`);
|
|
253
310
|
}
|
|
254
|
-
// Plan mode gate:
|
|
255
|
-
|
|
311
|
+
// Plan mode gate: once the model presents its plan (a reply without tool
|
|
312
|
+
// calls), ask for approval. Read-only calls before that just run.
|
|
313
|
+
if (planPending && stopReason === "end_turn") {
|
|
256
314
|
const approve = hooks?.onPlanApproval ?? requestPlanApproval;
|
|
257
315
|
const { approved, feedback } = await approve();
|
|
258
316
|
if (signal?.aborted) {
|
|
@@ -264,7 +322,7 @@ export async function runTurn(userInput, session, config, hooks) {
|
|
|
264
322
|
? `The user rejected the plan with feedback: ${feedback}\nPlease revise your plan.`
|
|
265
323
|
: "The user rejected the plan. Task aborted.";
|
|
266
324
|
if (feedback) {
|
|
267
|
-
// Revisions remain in plan mode,
|
|
325
|
+
// Revisions remain in plan mode, read-only, until approved.
|
|
268
326
|
session.log.append("user/message", {
|
|
269
327
|
message: { role: "user", content: msg },
|
|
270
328
|
source: "user",
|
|
@@ -301,6 +359,22 @@ export async function runTurn(userInput, session, config, hooks) {
|
|
|
301
359
|
}
|
|
302
360
|
// If no tool use, we're done
|
|
303
361
|
if (stopReason !== "tool_use") {
|
|
362
|
+
// A Stop hook that exits 2 sends the model back with its reason (tests
|
|
363
|
+
// still failing, say); stop_hook_active tells it it already did once.
|
|
364
|
+
if (hookConfig && stopBlocks < MAX_STOP_BLOCKS && !signal?.aborted) {
|
|
365
|
+
const stop = await runLifecycleHooks(hookConfig, "Stop", { stop_hook_active: stopBlocks > 0 });
|
|
366
|
+
if (stop.blocked) {
|
|
367
|
+
stopBlocks++;
|
|
368
|
+
status(`\x1b[2m[Stop hook: ${stop.reason}]\x1b[0m\n`);
|
|
369
|
+
session.log.append("user/message", {
|
|
370
|
+
message: { role: "user", content: `A Stop hook asked you to keep going: ${stop.reason}` },
|
|
371
|
+
source: "system",
|
|
372
|
+
turn: session.turns,
|
|
373
|
+
});
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
stopHooksRan = true;
|
|
377
|
+
}
|
|
304
378
|
endReason = "end_turn";
|
|
305
379
|
break;
|
|
306
380
|
}
|
|
@@ -308,7 +382,9 @@ export async function runTurn(userInput, session, config, hooks) {
|
|
|
308
382
|
// valid JSON are answered with an error instead of being run.
|
|
309
383
|
const toolUseBlocks = assistantContent.filter((b) => b.type === "tool_use");
|
|
310
384
|
const invalidById = new Map(invalidToolCalls.map((call) => [call.id, call]));
|
|
311
|
-
|
|
385
|
+
// Only offered the read-only tools, a model can still name another one.
|
|
386
|
+
const planRefused = new Set(planPending ? toolUseBlocks.filter((b) => !READ_ONLY_TOOLS.has(b.name)).map((b) => b.id) : []);
|
|
387
|
+
const runnableBlocks = toolUseBlocks.filter((b) => !invalidById.has(b.id) && !planRefused.has(b.id));
|
|
312
388
|
// Checkpoint BEFORE the mutating batch: the tool names are known now, and
|
|
313
389
|
// the snapshot must be the true pre-turn tree so /rewind can restore it.
|
|
314
390
|
const mutatingTools = new Set(["edit_file", "multi_edit", "apply_patch", "write_file"]);
|
|
@@ -340,6 +416,11 @@ export async function runTurn(userInput, session, config, hooks) {
|
|
|
340
416
|
// Results in the model's call order, invalid calls answered in place.
|
|
341
417
|
const resultById = new Map(executedResults.flatMap((r) => (r.type === "tool_result" ? [[r.tool_use_id, r]] : [])));
|
|
342
418
|
const toolResults = toolUseBlocks.map((block) => {
|
|
419
|
+
if (planRefused.has(block.id)) {
|
|
420
|
+
const output = `Not run: plan mode allows only read-only tools until the user approves the plan. Finish the plan without calling ${block.name}.`;
|
|
421
|
+
hooks?.onToolEnd?.(block.name, block.input, output, true, 0);
|
|
422
|
+
return { type: "tool_result", tool_use_id: block.id, content: output, is_error: true };
|
|
423
|
+
}
|
|
343
424
|
const invalid = invalidById.get(block.id);
|
|
344
425
|
if (!invalid)
|
|
345
426
|
return resultById.get(block.id);
|
|
@@ -349,8 +430,8 @@ export async function runTurn(userInput, session, config, hooks) {
|
|
|
349
430
|
hooks?.onToolEnd?.(block.name, block.input, output, true, 0);
|
|
350
431
|
return { type: "tool_result", tool_use_id: block.id, content: output, is_error: true };
|
|
351
432
|
});
|
|
352
|
-
session.toolCalls += toolCallCount + invalidById.size;
|
|
353
|
-
turnToolCalls += toolCallCount + invalidById.size;
|
|
433
|
+
session.toolCalls += toolCallCount + invalidById.size + planRefused.size;
|
|
434
|
+
turnToolCalls += toolCallCount + invalidById.size + planRefused.size;
|
|
354
435
|
// Only successful write/edit results justify checks; requested, denied,
|
|
355
436
|
// failed, or cancelled mutations must never launch a follow-up command.
|
|
356
437
|
const successfulMutation = toolUseBlocks.some((block) => mutatingTools.has(block.name)
|
|
@@ -360,12 +441,23 @@ export async function runTurn(userInput, session, config, hooks) {
|
|
|
360
441
|
const cwd = registry.permissionConfig.projectRoot;
|
|
361
442
|
const lintCmd = config.lintTestConfig.lintCmd ?? detectLintCommand(cwd);
|
|
362
443
|
const testCmd = config.lintTestConfig.testCmd ?? detectTestCommand(cwd);
|
|
444
|
+
// The type check first: it is quick, and tests can't pass while it
|
|
445
|
+
// fails. Only after an edit to a typed source file (a README edit
|
|
446
|
+
// doesn't need it).
|
|
447
|
+
const touchedTyped = toolUseBlocks.some((block) => mutatingTools.has(block.name)
|
|
448
|
+
&& TYPED_SOURCE.test(block.name === "apply_patch" ? String(block.input.patch ?? "") : String(block.input.path ?? "")));
|
|
449
|
+
const typecheckCmd = touchedTyped ? config.lintTestConfig.typecheckCmd : undefined;
|
|
363
450
|
const lintFailures = [];
|
|
364
|
-
|
|
451
|
+
let typesFailed = false;
|
|
452
|
+
for (const cmd of new Set([typecheckCmd, lintCmd, testCmd].filter(Boolean))) {
|
|
365
453
|
if (signal?.aborted)
|
|
366
454
|
break;
|
|
367
455
|
if (deniedChecks.has(cmd))
|
|
368
456
|
continue;
|
|
457
|
+
if (typesFailed && cmd === testCmd && cmd !== lintCmd) {
|
|
458
|
+
lintFailures.push(`Tests (${cmd}) were not run: fix the type errors first.`);
|
|
459
|
+
continue;
|
|
460
|
+
}
|
|
369
461
|
const input = { command: cmd, cwd, timeout: 60_000, description: "Verify the completed edit" };
|
|
370
462
|
hooks?.onToolStart?.("shell", input, 1);
|
|
371
463
|
// The same registry and scheduler preserve shell approval, hooks,
|
|
@@ -382,7 +474,9 @@ export async function runTurn(userInput, session, config, hooks) {
|
|
|
382
474
|
if (check.is_error) {
|
|
383
475
|
if (verbose)
|
|
384
476
|
status(`\x1b[33m[post-edit check failed: ${cmd}]\x1b[0m\n`);
|
|
385
|
-
|
|
477
|
+
if (cmd === typecheckCmd)
|
|
478
|
+
typesFailed = true;
|
|
479
|
+
lintFailures.push(`Post-edit check failed (${cmd}):\n${headAndTail(check.output, 2000)}`);
|
|
386
480
|
}
|
|
387
481
|
}
|
|
388
482
|
if (lintFailures.length > 0) {
|
|
@@ -422,8 +516,8 @@ export async function runTurn(userInput, session, config, hooks) {
|
|
|
422
516
|
else if (lastAssistant && typeof lastAssistant.content === "string") {
|
|
423
517
|
text = lastAssistant.content;
|
|
424
518
|
}
|
|
425
|
-
if (hookConfig) {
|
|
426
|
-
await runLifecycleHooks(hookConfig, "Stop", {});
|
|
519
|
+
if (hookConfig && !stopHooksRan) {
|
|
520
|
+
await runLifecycleHooks(hookConfig, "Stop", { stop_hook_active: stopBlocks > 0 });
|
|
427
521
|
}
|
|
428
522
|
return { text, turns: session.turns - turnStart, toolCalls: turnToolCalls, stopReason: signal?.aborted ? "aborted" : endReason };
|
|
429
523
|
}
|
|
@@ -127,6 +127,7 @@ export async function consumeStream(stream, costTracker, hooks, signal) {
|
|
|
127
127
|
const providerName = typeof hooks === "function" ? undefined : hooks?.providerName;
|
|
128
128
|
const content = [];
|
|
129
129
|
let stop_reason = "end_turn";
|
|
130
|
+
let usage;
|
|
130
131
|
let currentText = "";
|
|
131
132
|
let currentReasoning = "";
|
|
132
133
|
// Map block index -> tool state for Anthropic-style index-based IDs
|
|
@@ -209,6 +210,7 @@ export async function consumeStream(stream, costTracker, hooks, signal) {
|
|
|
209
210
|
}
|
|
210
211
|
else if (delta.type === "done") {
|
|
211
212
|
stop_reason = delta.stop_reason;
|
|
213
|
+
usage = delta.usage;
|
|
212
214
|
if (costTracker && delta.usage) {
|
|
213
215
|
recordTokenUsage(costTracker, delta.usage);
|
|
214
216
|
}
|
|
@@ -238,7 +240,7 @@ export async function consumeStream(stream, costTracker, hooks, signal) {
|
|
|
238
240
|
for (const tool of invalidToolCalls) {
|
|
239
241
|
content.push({ type: "tool_use", id: tool.id, name: tool.name, input: {} });
|
|
240
242
|
}
|
|
241
|
-
return { content, stop_reason, invalidToolCalls };
|
|
243
|
+
return { content, stop_reason, invalidToolCalls, usage };
|
|
242
244
|
}
|
|
243
245
|
/**
|
|
244
246
|
* The model-visible part of a tool output. File tools append a full
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Images in a prompt: a path to a png, jpeg, webp or gif in the message
|
|
3
|
+
* (typed, or dropped onto the terminal, which pastes the path, quoted or with
|
|
4
|
+
* escaped spaces) is attached to the message as the image itself, on a model
|
|
5
|
+
* that can see. Terminals don't pass clipboard image data to a program, so a
|
|
6
|
+
* dropped or typed path is how an image gets in, as in Claude Code.
|
|
7
|
+
*/
|
|
8
|
+
import * as fs from "fs";
|
|
9
|
+
import * as path from "path";
|
|
10
|
+
/** Anthropic rejects images over 5MB; the other providers are in the same range. */
|
|
11
|
+
const MAX_IMAGE_BYTES = 5 * 1024 * 1024;
|
|
12
|
+
const MAX_IMAGES = 5;
|
|
13
|
+
const MEDIA_TYPES = {
|
|
14
|
+
".png": "image/png",
|
|
15
|
+
".jpg": "image/jpeg",
|
|
16
|
+
".jpeg": "image/jpeg",
|
|
17
|
+
".webp": "image/webp",
|
|
18
|
+
".gif": "image/gif",
|
|
19
|
+
};
|
|
20
|
+
/** Candidate paths: quoted, backslash-escaped spaces, or plain, with an image extension. */
|
|
21
|
+
const PATH_RE = /'([^']+\.(?:png|jpe?g|webp|gif))'|"([^"]+\.(?:png|jpe?g|webp|gif))"|((?:[^\s'"\\]|\\ )+\.(?:png|jpe?g|webp|gif))/gi;
|
|
22
|
+
/** The message with any image paths in it attached as images. */
|
|
23
|
+
export function attachImages(text, cwd) {
|
|
24
|
+
const attached = [];
|
|
25
|
+
const skipped = [];
|
|
26
|
+
const images = [];
|
|
27
|
+
for (const match of text.matchAll(PATH_RE)) {
|
|
28
|
+
const raw = (match[1] ?? match[2] ?? match[3]).replace(/\\ /g, " ");
|
|
29
|
+
const file = path.resolve(cwd, raw.replace(/^~(?=\/)/, process.env.HOME ?? "~"));
|
|
30
|
+
if (attached.includes(file))
|
|
31
|
+
continue;
|
|
32
|
+
let size;
|
|
33
|
+
try {
|
|
34
|
+
const stat = fs.statSync(file);
|
|
35
|
+
if (!stat.isFile())
|
|
36
|
+
continue;
|
|
37
|
+
size = stat.size;
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
continue; // not a file here: just text
|
|
41
|
+
}
|
|
42
|
+
if (images.length >= MAX_IMAGES) {
|
|
43
|
+
skipped.push(`${raw} (more than ${MAX_IMAGES} images)`);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (size > MAX_IMAGE_BYTES) {
|
|
47
|
+
skipped.push(`${raw} (over 5MB)`);
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
images.push({
|
|
51
|
+
type: "image",
|
|
52
|
+
source: { type: "base64", media_type: MEDIA_TYPES[path.extname(file).toLowerCase()], data: fs.readFileSync(file).toString("base64") },
|
|
53
|
+
});
|
|
54
|
+
attached.push(file);
|
|
55
|
+
}
|
|
56
|
+
return images.length === 0 ? { content: text, attached, skipped } : { content: [{ type: "text", text }, ...images], attached, skipped };
|
|
57
|
+
}
|
package/dist/commands/info.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { estimateMessageTokens } from "../context/token-counter.js";
|
|
2
|
+
import { contextTokens } from "../context/usage.js";
|
|
2
3
|
import { execSync } from "node:child_process";
|
|
3
4
|
const DIM = "\x1b[2m";
|
|
4
5
|
const _BOLD = "\x1b[1m";
|
|
@@ -13,7 +14,10 @@ export function helpCommand(_parts, _ctx) {
|
|
|
13
14
|
/model Interactive model + reasoning picker
|
|
14
15
|
/model add <id> Add a custom model
|
|
15
16
|
/model remove <id> Remove a custom model
|
|
17
|
+
/model <id> Switch to that model on the current provider
|
|
16
18
|
/provider Show configured providers + auth status
|
|
19
|
+
/provider <name> [model] Switch provider (and model) mid-session
|
|
20
|
+
/reasoning [level] Show or set the reasoning effort (none, low, medium, high, xhigh)
|
|
17
21
|
/turns Show turn and tool call counts
|
|
18
22
|
/clear Clear conversation history and terminal screen
|
|
19
23
|
/cwd Show current working directory
|
|
@@ -22,7 +26,7 @@ export function helpCommand(_parts, _ctx) {
|
|
|
22
26
|
/plan Show conversation plan (tool calls so far)
|
|
23
27
|
/undo Undo last user message and response
|
|
24
28
|
/history [n|full] Show last N messages (default 10) with rich formatting
|
|
25
|
-
/compact
|
|
29
|
+
/compact [focus] Compact conversation (LLM checkpoint + knowledge promotion), keeping the focus
|
|
26
30
|
/review Triage the phren review queue (go = manual, auto = model-assisted)
|
|
27
31
|
/context Show context window usage and provider info
|
|
28
32
|
/mode Toggle input mode (steering ↔ queue)
|
|
@@ -144,7 +148,7 @@ export function undoCommand(_parts, ctx) {
|
|
|
144
148
|
return true;
|
|
145
149
|
}
|
|
146
150
|
export function contextCommand(_parts, ctx) {
|
|
147
|
-
const ctxTokens =
|
|
151
|
+
const ctxTokens = contextTokens(ctx.systemPrompt ?? "", ctx.session.messages, ctx.session.log, ctx.session.reportedContext);
|
|
148
152
|
const ctxPct = ctx.contextLimit > 0 ? (ctxTokens / ctx.contextLimit) * 100 : 0;
|
|
149
153
|
const ctxPctStr = ctxPct.toFixed(1);
|
|
150
154
|
const ctxWindowK = ctx.contextLimit >= 1000 ? `${(ctx.contextLimit / 1000).toFixed(0)}k` : String(ctx.contextLimit);
|
package/dist/commands/model.js
CHANGED
|
@@ -4,6 +4,7 @@ import { formatProviderList, formatModelAddHelp, addCustomModel, removeCustomMod
|
|
|
4
4
|
import { normalizeReasoningEffort } from "../models.js";
|
|
5
5
|
const DIM = "\x1b[2m";
|
|
6
6
|
const GREEN = "\x1b[32m";
|
|
7
|
+
const RED = "\x1b[31m";
|
|
7
8
|
const RESET = "\x1b[0m";
|
|
8
9
|
export function modelCommand(parts, ctx) {
|
|
9
10
|
const sub = parts[1]?.toLowerCase();
|
|
@@ -46,31 +47,72 @@ export function modelCommand(parts, ctx) {
|
|
|
46
47
|
process.stderr.write(ok ? `${GREEN}-> Removed ${modelId}${RESET}\n` : `${DIM}Model "${modelId}" not found in custom models.${RESET}\n`);
|
|
47
48
|
return true;
|
|
48
49
|
}
|
|
50
|
+
// /model <id>: that model on the current provider, keeping the effort.
|
|
51
|
+
if (parts[1] && sub !== "add" && sub !== "remove" && sub !== "rm") {
|
|
52
|
+
return switchModel(ctx, { model: parts[1], reasoning: ctx.currentReasoning ?? null });
|
|
53
|
+
}
|
|
49
54
|
// /model (no sub) -- interactive picker
|
|
50
55
|
if (!ctx.providerName) {
|
|
51
56
|
process.stderr.write(`${DIM}Provider not configured. Start with --provider to set one.${RESET}\n`);
|
|
52
57
|
return true;
|
|
53
58
|
}
|
|
54
|
-
const applyResult = (result) =>
|
|
55
|
-
if (result && ctx.onModelChange) {
|
|
56
|
-
ctx.onModelChange(result);
|
|
57
|
-
const reasoningLabel = result.reasoning ? ` (reasoning: ${result.reasoning})` : "";
|
|
58
|
-
process.stderr.write(`${GREEN}-> ${result.model}${reasoningLabel}${RESET}\n`);
|
|
59
|
-
}
|
|
60
|
-
else if (result) {
|
|
61
|
-
process.stderr.write(`${DIM}Model selected: ${result.model} -- restart to apply.${RESET}\n`);
|
|
62
|
-
}
|
|
63
|
-
};
|
|
59
|
+
const applyResult = (result) => (result ? switchModel(ctx, result) : true);
|
|
64
60
|
if (ctx.pickModel) {
|
|
65
|
-
return ctx.pickModel().then(
|
|
61
|
+
return ctx.pickModel().then(applyResult);
|
|
66
62
|
}
|
|
67
63
|
showModelPicker(ctx.providerName, ctx.currentModel, ctx.currentReasoning, process.stdout).then(applyResult);
|
|
68
64
|
return true;
|
|
69
65
|
}
|
|
70
|
-
|
|
71
|
-
|
|
66
|
+
/**
|
|
67
|
+
* Apply a model, provider or effort change through the host and keep the
|
|
68
|
+
* command context in step with it, so the next /model or /reasoning starts
|
|
69
|
+
* from what is running now.
|
|
70
|
+
*/
|
|
71
|
+
export async function switchModel(ctx, result) {
|
|
72
|
+
if (!ctx.onModelChange) {
|
|
73
|
+
process.stderr.write(`${DIM}Model selected: ${result.model || "(default)"} -- restart to apply.${RESET}\n`);
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
const provider = await ctx.onModelChange(result);
|
|
78
|
+
if (provider) {
|
|
79
|
+
ctx.provider = provider;
|
|
80
|
+
ctx.providerName = provider.name;
|
|
81
|
+
ctx.currentModel = provider.model;
|
|
82
|
+
ctx.currentReasoning = provider.reasoningEffort ?? null;
|
|
83
|
+
}
|
|
84
|
+
const name = provider ? `${provider.name}/${provider.model ?? "default"}` : result.model;
|
|
85
|
+
const effort = provider?.reasoningEffort ?? result.reasoning;
|
|
86
|
+
process.stderr.write(`${GREEN}-> ${name}${effort ? ` (reasoning: ${effort})` : ""}${RESET}\n`);
|
|
87
|
+
}
|
|
88
|
+
catch (err) {
|
|
89
|
+
process.stderr.write(`${RED}Could not switch: ${err instanceof Error ? err.message : String(err)}${RESET}\n`);
|
|
90
|
+
}
|
|
72
91
|
return true;
|
|
73
92
|
}
|
|
93
|
+
/** /reasoning [level]: show the effort, or set it on the current model. */
|
|
94
|
+
export function reasoningCommand(parts, ctx) {
|
|
95
|
+
const arg = parts[1];
|
|
96
|
+
if (!arg) {
|
|
97
|
+
process.stderr.write(`${DIM}Reasoning: ${ctx.currentReasoning ?? "model default"}. Set it with /reasoning none|low|medium|high|xhigh.${RESET}\n`);
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
const level = normalizeReasoningEffort(arg);
|
|
101
|
+
if (!level) {
|
|
102
|
+
process.stderr.write(`${RED}Unknown reasoning level "${arg}". Use none, low, medium, high or xhigh.${RESET}\n`);
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
return switchModel(ctx, { model: ctx.currentModel ?? "", reasoning: level });
|
|
106
|
+
}
|
|
107
|
+
/** /provider lists providers; /provider <name> [model] switches to one mid-session. */
|
|
108
|
+
export function providerCommand(parts, ctx) {
|
|
109
|
+
if (!parts[1]) {
|
|
110
|
+
process.stderr.write(formatProviderList());
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
// Another provider's effort scale may differ: start from its default.
|
|
114
|
+
return switchModel(ctx, { provider: parts[1], model: parts[2] ?? "", reasoning: null });
|
|
115
|
+
}
|
|
74
116
|
export function presetCommand(parts, _ctx) {
|
|
75
117
|
const sub = parts[1]?.toLowerCase();
|
|
76
118
|
if (!sub || sub === "list") {
|
package/dist/commands/session.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { toolResultText } from "../providers/types.js";
|
|
2
|
-
import { seedFromMessages } from "../session/log.js";
|
|
2
|
+
import { SessionLog, seedFromMessages } from "../session/log.js";
|
|
3
|
+
import { listEventLogs, loadEventLog } from "../session/persist.js";
|
|
4
|
+
import { agentUserDir } from "../config.js";
|
|
3
5
|
import { estimateMessageTokens } from "../context/token-counter.js";
|
|
4
6
|
import { planPrune } from "../context/pruner.js";
|
|
5
7
|
import { compactWithLlm } from "../context/compactor.js";
|
|
@@ -144,7 +146,8 @@ export function historyCommand(parts, ctx) {
|
|
|
144
146
|
process.stderr.write(`${DIM}-- end --${RESET}\n`);
|
|
145
147
|
return true;
|
|
146
148
|
}
|
|
147
|
-
export async function compactCommand(
|
|
149
|
+
export async function compactCommand(parts, ctx) {
|
|
150
|
+
const focus = parts.slice(1).join(" ");
|
|
148
151
|
const beforeCount = ctx.session.messages.length;
|
|
149
152
|
const beforeTokens = estimateMessageTokens(ctx.session.messages);
|
|
150
153
|
// LLM checkpoint when a provider is wired up; regex fallback otherwise.
|
|
@@ -158,6 +161,8 @@ export async function compactCommand(_parts, ctx) {
|
|
|
158
161
|
pruneConfig: { contextLimit: ctx.contextLimit, keepRecentTurns: 4 },
|
|
159
162
|
// Manual /compact should always try the LLM, even for small ranges.
|
|
160
163
|
config: { minPrunedTokens: 0 },
|
|
164
|
+
tools: ctx.registry?.getDefinitions?.(),
|
|
165
|
+
focus,
|
|
161
166
|
});
|
|
162
167
|
if (result) {
|
|
163
168
|
ctx.session.log.replaceMessageRange(result.plan.startIndex, result.plan.endIndex, result.plan.summaryMessage);
|
|
@@ -216,30 +221,86 @@ export function diffCommand(parts, _ctx) {
|
|
|
216
221
|
}
|
|
217
222
|
return true;
|
|
218
223
|
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
224
|
+
/** Sessions /resume can offer: this store's, or ~/.phren-agent's for this directory without one. */
|
|
225
|
+
function resumableSessions(ctx) {
|
|
226
|
+
const current = ctx.session.log.header.sessionId;
|
|
227
|
+
const listed = ctx.phrenPath
|
|
228
|
+
? listEventLogs(ctx.phrenPath, { project: ctx.phrenCtx?.project ?? undefined, limit: 30 })
|
|
229
|
+
: listEventLogs(agentUserDir(), { cwd: process.cwd(), limit: 30 });
|
|
230
|
+
return listed.filter((s) => s.sessionId !== current);
|
|
231
|
+
}
|
|
232
|
+
function sessionLabel(s) {
|
|
233
|
+
const when = new Date(s.mtimeMs).toISOString().replace("T", " ").slice(0, 16);
|
|
234
|
+
return { label: s.title || "(no prompt)", detail: `${when} \u00b7 ${s.messages} msgs \u00b7 ${s.sessionId.slice(0, 8)}` };
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* /resume picks an earlier session and loads its conversation into this
|
|
238
|
+
* fresh one: from a picker in the terminal UI, by number or id prefix
|
|
239
|
+
* (`/resume 2`, `/resume 3fa9`), or from a printed list elsewhere.
|
|
240
|
+
*/
|
|
241
|
+
export async function resumeCommand(parts, ctx) {
|
|
242
|
+
if (ctx.session.messages.length > 0) {
|
|
243
|
+
process.stderr.write(`${DIM}Session already has history; /resume only works on a fresh session.${RESET}\n`);
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
const sessions = resumableSessions(ctx);
|
|
247
|
+
const arg = parts[1];
|
|
248
|
+
let chosen;
|
|
249
|
+
if (arg) {
|
|
250
|
+
const index = Number(arg);
|
|
251
|
+
chosen = Number.isInteger(index) && index >= 1 && index <= sessions.length
|
|
252
|
+
? sessions[index - 1]
|
|
253
|
+
: sessions.find((s) => s.sessionId.startsWith(arg));
|
|
254
|
+
if (!chosen) {
|
|
255
|
+
process.stderr.write(`${RED}No session "${arg}". /resume lists them.${RESET}\n`);
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
else if (sessions.length === 0) {
|
|
260
|
+
return resumeLegacySnapshot(ctx);
|
|
261
|
+
}
|
|
262
|
+
else if (ctx.pickFromList) {
|
|
263
|
+
const picked = await ctx.pickFromList("Resume a session", sessions.map(sessionLabel));
|
|
264
|
+
if (picked === null)
|
|
265
|
+
return true;
|
|
266
|
+
chosen = sessions[picked];
|
|
267
|
+
}
|
|
268
|
+
else {
|
|
269
|
+
const lines = sessions.slice(0, 15).map((s, i) => {
|
|
270
|
+
const { label, detail } = sessionLabel(s);
|
|
271
|
+
return ` ${String(i + 1).padStart(2)}. ${label.slice(0, 60)} ${detail}`;
|
|
272
|
+
});
|
|
273
|
+
process.stderr.write(`${DIM}Recent sessions (newest first):\n${lines.join("\n")}\n /resume <n> or /resume <id> to load one${RESET}\n`);
|
|
274
|
+
return true;
|
|
275
|
+
}
|
|
276
|
+
let messages;
|
|
277
|
+
try {
|
|
278
|
+
const { header, events } = loadEventLog(chosen.file);
|
|
279
|
+
messages = SessionLog.restore(header, events, () => { }).getMessages();
|
|
280
|
+
}
|
|
281
|
+
catch (err) {
|
|
282
|
+
process.stderr.write(`${RED}Cannot read that session: ${err instanceof Error ? err.message : String(err)}${RESET}\n`);
|
|
222
283
|
return true;
|
|
223
284
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
285
|
+
seedFromMessages(ctx.session.log, messages);
|
|
286
|
+
process.stderr.write(`${GREEN}-> Resumed ${messages.length} messages from session ${chosen.sessionId.slice(0, 8)}: ${chosen.title}${RESET}\n`);
|
|
287
|
+
process.stderr.write(`${DIM} Use /history to review. The model sees the whole conversation on your next message.${RESET}\n`);
|
|
288
|
+
return true;
|
|
289
|
+
}
|
|
290
|
+
/** Before event logs: the v1 snapshot saved at the end of the last session. */
|
|
291
|
+
function resumeLegacySnapshot(ctx) {
|
|
292
|
+
if (!ctx.phrenPath) {
|
|
227
293
|
process.stderr.write(`${DIM}No previous session to resume.${RESET}\n`);
|
|
228
294
|
return true;
|
|
229
295
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
process.stderr.write(`${DIM}Session already has history; /resume only works on a fresh session.${RESET}\n`);
|
|
296
|
+
const snapshot = loadLastSessionSnapshot(ctx.phrenPath, ctx.phrenCtx?.project ?? undefined);
|
|
297
|
+
if (!snapshot || snapshot.messages.length === 0) {
|
|
298
|
+
process.stderr.write(`${DIM}No previous session to resume.${RESET}\n`);
|
|
234
299
|
return true;
|
|
235
300
|
}
|
|
236
301
|
seedFromMessages(ctx.session.log, snapshot.messages);
|
|
237
|
-
process.stderr.write(`${GREEN}-> Resumed ${
|
|
238
|
-
|
|
239
|
-
process.stderr.write(`${DIM} Project: ${snapshot.project}${RESET}\n`);
|
|
240
|
-
}
|
|
241
|
-
process.stderr.write(`${DIM} Saved: ${snapshot.savedAt}${RESET}\n`);
|
|
242
|
-
process.stderr.write(`${DIM} Use /history to review. The LLM will see the full prior conversation on your next message.${RESET}\n`);
|
|
302
|
+
process.stderr.write(`${GREEN}-> Resumed ${snapshot.messages.length} messages from session ${snapshot.sessionId}${RESET}\n`);
|
|
303
|
+
process.stderr.write(`${DIM} Saved: ${snapshot.savedAt}. Use /history to review.${RESET}\n`);
|
|
243
304
|
return true;
|
|
244
305
|
}
|
|
245
306
|
export function rewindCommand(parts, _ctx) {
|