@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/tui/ink-entry.js
CHANGED
|
@@ -2,7 +2,8 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
2
2
|
import { render } from "ink";
|
|
3
3
|
import { createSession, runTurn } from "../agent-loop.js";
|
|
4
4
|
import { useSlashCommands } from "./hooks/useSlashCommands.js";
|
|
5
|
-
import { decodeDiffPayload, DIFF_MARKER } from "../multi/diff-renderer.js";
|
|
5
|
+
import { decodeDiffPayload, DIFF_MARKER, renderInlineDiff } from "../multi/diff-renderer.js";
|
|
6
|
+
import { formatToolInput } from "./tool-render.js";
|
|
6
7
|
import * as os from "os";
|
|
7
8
|
import { execSync } from "node:child_process";
|
|
8
9
|
import * as path from "node:path";
|
|
@@ -10,6 +11,7 @@ import { loadInputMode, saveInputMode, savePermissionMode } from "../settings.js
|
|
|
10
11
|
import { nextPermissionMode } from "./ansi.js";
|
|
11
12
|
import { App } from "./components/App.js";
|
|
12
13
|
import { createRequire } from "node:module";
|
|
14
|
+
import { getTheme, THEME_NAMES } from "./themes.js";
|
|
13
15
|
const _require = createRequire(import.meta.url);
|
|
14
16
|
const AGENT_VERSION = _require("../../package.json").version;
|
|
15
17
|
export async function startInkTui(config, spawner) {
|
|
@@ -22,7 +24,33 @@ export async function startInkTui(config, spawner) {
|
|
|
22
24
|
const inputHistory = [];
|
|
23
25
|
let running = false;
|
|
24
26
|
let verbose = false;
|
|
27
|
+
let theme = getTheme();
|
|
25
28
|
let msgCounter = 0;
|
|
29
|
+
// Permission prompt state — when set, input field captures y/n/a/s
|
|
30
|
+
let permissionResolve = null;
|
|
31
|
+
// Ink-compatible askUser: shows prompt in chat, waits for y/n in input
|
|
32
|
+
config.registry.askUser = async (toolName, input, reason) => {
|
|
33
|
+
const { addAllow } = await import("../permissions/allowlist.js");
|
|
34
|
+
const summary = Object.keys(input).length > 0
|
|
35
|
+
? `${toolName}(${Object.entries(input).map(([k, v]) => `${k}: ${JSON.stringify(v).slice(0, 40)}`).join(", ")})`
|
|
36
|
+
: toolName;
|
|
37
|
+
completedMessages.push({
|
|
38
|
+
id: nextId(),
|
|
39
|
+
kind: "status",
|
|
40
|
+
text: `\x1b[1m\x1b[33m[PERMISSION]\x1b[0m ${toolName}\n ${reason}\n ${summary}\n \x1b[2m[y]es [n]o [a]llow-tool [s]ession-allow\x1b[0m`,
|
|
41
|
+
});
|
|
42
|
+
update();
|
|
43
|
+
return new Promise((resolve) => {
|
|
44
|
+
permissionResolve = (allowed) => {
|
|
45
|
+
permissionResolve = null;
|
|
46
|
+
resolve(allowed);
|
|
47
|
+
};
|
|
48
|
+
// Store the addAllow function for a/s responses
|
|
49
|
+
permissionResolve.toolName = toolName;
|
|
50
|
+
permissionResolve.input = input;
|
|
51
|
+
permissionResolve.addAllow = addAllow;
|
|
52
|
+
});
|
|
53
|
+
};
|
|
26
54
|
// Mutable render state — updated then pushed to React via rerender()
|
|
27
55
|
const completedMessages = [];
|
|
28
56
|
let streamingText = "";
|
|
@@ -30,6 +58,7 @@ export async function startInkTui(config, spawner) {
|
|
|
30
58
|
let thinkStartTime = 0;
|
|
31
59
|
let thinkElapsed = null;
|
|
32
60
|
let currentToolCalls = [];
|
|
61
|
+
let activeTool = null;
|
|
33
62
|
function nextId() {
|
|
34
63
|
return `msg-${++msgCounter}`;
|
|
35
64
|
}
|
|
@@ -46,10 +75,34 @@ export async function startInkTui(config, spawner) {
|
|
|
46
75
|
}
|
|
47
76
|
// Re-render the Ink app with current state
|
|
48
77
|
let rerender = null;
|
|
78
|
+
// Ink app instance — set after render(), used by closures for clear()
|
|
79
|
+
let appInstance = null;
|
|
80
|
+
// Batched update for high-frequency events (streaming deltas)
|
|
81
|
+
let updateScheduled = false;
|
|
82
|
+
function scheduleUpdate() {
|
|
83
|
+
if (updateScheduled)
|
|
84
|
+
return;
|
|
85
|
+
updateScheduled = true;
|
|
86
|
+
queueMicrotask(() => {
|
|
87
|
+
updateScheduled = false;
|
|
88
|
+
update();
|
|
89
|
+
});
|
|
90
|
+
}
|
|
49
91
|
function update() {
|
|
50
92
|
if (!rerender)
|
|
51
93
|
return;
|
|
52
|
-
|
|
94
|
+
// Determine what to display based on selected view
|
|
95
|
+
const agentConvo = selectedAgentId ? agentConvos.get(selectedAgentId) : null;
|
|
96
|
+
// Prefix IDs so <Static> re-renders after agent tab switch
|
|
97
|
+
const prefix = `v${viewSwitchCounter}-`;
|
|
98
|
+
const rawMessages = agentConvo ? agentConvo.messages : completedMessages;
|
|
99
|
+
const displayMessages = rawMessages.map((m) => ({ ...m, id: prefix + m.id }));
|
|
100
|
+
const displayStreaming = agentConvo ? agentConvo.streamingText : streamingText;
|
|
101
|
+
const displayToolCalls = agentConvo ? [...agentConvo.toolCalls] : [...currentToolCalls];
|
|
102
|
+
const displayActiveTool = agentConvo ? agentConvo.activeTool : activeTool;
|
|
103
|
+
const displayThinking = agentConvo ? false : thinking;
|
|
104
|
+
const displayRunning = agentConvo ? (spawner?.getAgent(selectedAgentId)?.status === "running" || false) : running;
|
|
105
|
+
rerender(_jsx(App, { state: getAppState(), completedMessages: displayMessages, streamingText: displayStreaming, completedToolCalls: displayToolCalls, activeTool: displayActiveTool, thinking: displayThinking, thinkStartTime: thinkStartTime, thinkElapsed: agentConvo ? null : thinkElapsed, steerQueue: agentConvo ? [] : [...steerQueueBuf], running: displayRunning, showBanner: !selectedAgentId, inputHistory: [...inputHistory], verbose: verbose, theme: theme, onSubmit: handleSubmit, onPermissionCycle: handlePermissionCycle, onCancelTurn: handleCancelTurn, onExit: handleExit, agents: agentTabs.length > 0 ? [{ id: "__main__", name: "phren", status: running ? "running" : "idle", color: "magentaBright" }, ...agentTabs] : undefined, selectedAgentId: selectedAgentId ?? (agentTabs.length > 0 ? "__main__" : undefined), onCancelAgent: handleCancelAgent, onSelectAgent: (id) => handleSelectAgent(id === "__main__" ? null : id) }));
|
|
53
106
|
}
|
|
54
107
|
function handlePermissionCycle() {
|
|
55
108
|
const next = nextPermissionMode(config.registry.permissionConfig.mode);
|
|
@@ -76,6 +129,7 @@ export async function startInkTui(config, spawner) {
|
|
|
76
129
|
undoStack: [],
|
|
77
130
|
providerName: config.provider.name,
|
|
78
131
|
currentModel: config.provider.model,
|
|
132
|
+
currentReasoning: config.provider.reasoningEffort ?? null,
|
|
79
133
|
provider: config.provider,
|
|
80
134
|
systemPrompt: config.systemPrompt,
|
|
81
135
|
spawner,
|
|
@@ -86,7 +140,7 @@ export async function startInkTui(config, spawner) {
|
|
|
86
140
|
onModelChange: async (result) => {
|
|
87
141
|
try {
|
|
88
142
|
const { resolveProvider } = await import("../providers/resolve.js");
|
|
89
|
-
const newProvider = resolveProvider(config.provider.name, result.model);
|
|
143
|
+
const newProvider = resolveProvider(config.provider.name, result.model, undefined, result.reasoning ?? undefined);
|
|
90
144
|
config.provider = newProvider;
|
|
91
145
|
const { buildSystemPrompt } = await import("../system-prompt.js");
|
|
92
146
|
config.systemPrompt = buildSystemPrompt(config.systemPrompt.split("\n## Last session")[0], null, { name: newProvider.name, model: result.model });
|
|
@@ -103,6 +157,31 @@ export async function startInkTui(config, spawner) {
|
|
|
103
157
|
const line = input.trim();
|
|
104
158
|
if (!line)
|
|
105
159
|
return;
|
|
160
|
+
// Permission prompt active — intercept y/n/a/s
|
|
161
|
+
if (permissionResolve) {
|
|
162
|
+
const key = line.toLowerCase();
|
|
163
|
+
const meta = permissionResolve;
|
|
164
|
+
if (key === "y" || key === "yes") {
|
|
165
|
+
completedMessages.push({ id: nextId(), kind: "status", text: "\x1b[32m✓ Allowed\x1b[0m" });
|
|
166
|
+
permissionResolve(true);
|
|
167
|
+
}
|
|
168
|
+
else if (key === "a") {
|
|
169
|
+
completedMessages.push({ id: nextId(), kind: "status", text: "\x1b[32m✓ Allowed (tool for session)\x1b[0m" });
|
|
170
|
+
meta.addAllow(meta.toolName, meta.input, "tool");
|
|
171
|
+
permissionResolve(true);
|
|
172
|
+
}
|
|
173
|
+
else if (key === "s") {
|
|
174
|
+
completedMessages.push({ id: nextId(), kind: "status", text: "\x1b[32m✓ Allowed (session)\x1b[0m" });
|
|
175
|
+
meta.addAllow(meta.toolName, meta.input, "session");
|
|
176
|
+
permissionResolve(true);
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
completedMessages.push({ id: nextId(), kind: "status", text: "\x1b[31m✗ Denied\x1b[0m" });
|
|
180
|
+
permissionResolve(false);
|
|
181
|
+
}
|
|
182
|
+
update();
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
106
185
|
// Track input history (skip duplicates of the last entry)
|
|
107
186
|
if (inputHistory.length === 0 || inputHistory[inputHistory.length - 1] !== line) {
|
|
108
187
|
inputHistory.push(line);
|
|
@@ -147,12 +226,63 @@ export async function startInkTui(config, spawner) {
|
|
|
147
226
|
update();
|
|
148
227
|
return;
|
|
149
228
|
}
|
|
229
|
+
// /agent <name> — switch to an agent's conversation, /agent (no args) — back to main
|
|
230
|
+
if (line.startsWith("/agent")) {
|
|
231
|
+
const arg = line.slice(6).trim();
|
|
232
|
+
if (!arg || arg === "main" || arg === "phren") {
|
|
233
|
+
handleSelectAgent(null);
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
// /agents — list all agents
|
|
237
|
+
if (arg === "s") {
|
|
238
|
+
const agents = spawner?.listAgents() ?? [];
|
|
239
|
+
if (agents.length === 0) {
|
|
240
|
+
completedMessages.push({ id: nextId(), kind: "status", text: "No agents spawned." });
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
const lines = agents.map((a) => ` ${a.displayName || a.id} [${a.status}]`).join("\n");
|
|
244
|
+
completedMessages.push({ id: nextId(), kind: "status", text: `Agents:\n${lines}` });
|
|
245
|
+
}
|
|
246
|
+
update();
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
// Find agent by name or id
|
|
250
|
+
const agents = spawner?.listAgents() ?? [];
|
|
251
|
+
const match = agents.find((a) => a.displayName === arg || a.id === arg || a.displayName?.includes(arg));
|
|
252
|
+
if (match) {
|
|
253
|
+
handleSelectAgent(match.id);
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
completedMessages.push({ id: nextId(), kind: "status", text: `Agent "${arg}" not found. Use /agents to list.` });
|
|
257
|
+
update();
|
|
258
|
+
}
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
150
261
|
if (line === "/verbose") {
|
|
151
262
|
verbose = !verbose;
|
|
152
263
|
completedMessages.push({ id: nextId(), kind: "status", text: `Verbose: ${verbose ? "on" : "off"}` });
|
|
153
264
|
update();
|
|
154
265
|
return;
|
|
155
266
|
}
|
|
267
|
+
if (line === "/theme") {
|
|
268
|
+
const idx = THEME_NAMES.indexOf(theme.name);
|
|
269
|
+
const next = THEME_NAMES[(idx + 1) % THEME_NAMES.length];
|
|
270
|
+
theme = getTheme(next);
|
|
271
|
+
completedMessages.push({ id: nextId(), kind: "status", text: `Theme: ${theme.name} (${THEME_NAMES.join(", ")})` });
|
|
272
|
+
update();
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
// /clear — also wipe Ink completed messages and clear screen
|
|
276
|
+
if (line === "/clear") {
|
|
277
|
+
completedMessages.length = 0;
|
|
278
|
+
if (appInstance)
|
|
279
|
+
appInstance.clear();
|
|
280
|
+
else
|
|
281
|
+
process.stdout.write("\x1b[2J\x1b[H");
|
|
282
|
+
slashCommands.tryHandleCommand(line);
|
|
283
|
+
update();
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
156
286
|
// Slash commands — capture stderr output and display as status message
|
|
157
287
|
if (line.startsWith("/")) {
|
|
158
288
|
if (slashCommands.tryHandleCommand(line)) {
|
|
@@ -171,17 +301,34 @@ export async function startInkTui(config, spawner) {
|
|
|
171
301
|
update();
|
|
172
302
|
return;
|
|
173
303
|
}
|
|
174
|
-
//
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
304
|
+
// If a spawned agent is selected, send input to THAT agent
|
|
305
|
+
if (selectedAgentId && selectedAgentId !== "__main__" && spawner) {
|
|
306
|
+
const agent = spawner.getAgent(selectedAgentId);
|
|
307
|
+
const agentName = agent?.displayName || selectedAgentId;
|
|
308
|
+
completedMessages.push({ id: nextId(), kind: "user", text: `[→ ${agentName}] ${line}` });
|
|
309
|
+
if (agent?.status === "idle") {
|
|
310
|
+
spawner.wakeAgent(selectedAgentId, { message: line, from: "user" });
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
spawner.sendToAgent(selectedAgentId, line, "user");
|
|
314
|
+
}
|
|
315
|
+
update();
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
// Normal user message — defer static push to next tick so dynamic area is clean
|
|
319
|
+
update(); // flush clean state first (empty input)
|
|
320
|
+
setImmediate(() => {
|
|
321
|
+
completedMessages.push({ id: nextId(), kind: "user", text: line });
|
|
322
|
+
update();
|
|
323
|
+
runAgentTurn(line);
|
|
324
|
+
});
|
|
178
325
|
}
|
|
179
326
|
// TurnHooks bridge — updates mutable state, calls update()
|
|
180
327
|
const tuiHooks = {
|
|
181
328
|
onTextDelta: (text) => {
|
|
182
329
|
thinking = false;
|
|
183
330
|
streamingText += text;
|
|
184
|
-
|
|
331
|
+
scheduleUpdate();
|
|
185
332
|
},
|
|
186
333
|
onTextDone: () => {
|
|
187
334
|
// streaming complete — finalized in runAgentTurn
|
|
@@ -189,16 +336,19 @@ export async function startInkTui(config, spawner) {
|
|
|
189
336
|
onTextBlock: (text) => {
|
|
190
337
|
thinking = false;
|
|
191
338
|
streamingText += text;
|
|
192
|
-
|
|
339
|
+
scheduleUpdate();
|
|
193
340
|
},
|
|
194
|
-
onToolStart: (
|
|
341
|
+
onToolStart: (name, input, _count) => {
|
|
195
342
|
thinking = false;
|
|
343
|
+
activeTool = { name, preview: formatToolInput(name, input) };
|
|
196
344
|
update();
|
|
197
345
|
},
|
|
198
346
|
onToolEnd: (name, input, output, isError, dur) => {
|
|
347
|
+
activeTool = null;
|
|
199
348
|
const diffData = (name === "edit_file" || name === "write_file") ? decodeDiffPayload(output) : null;
|
|
200
349
|
const cleanOutput = diffData ? output.slice(0, output.indexOf(DIFF_MARKER)) : output;
|
|
201
|
-
|
|
350
|
+
const diffRendered = diffData ? renderInlineDiff(diffData.oldContent, diffData.newContent, diffData.filePath, theme.diff) : undefined;
|
|
351
|
+
currentToolCalls.push({ name, input, output: cleanOutput, isError, durationMs: dur, diffRendered });
|
|
202
352
|
update();
|
|
203
353
|
},
|
|
204
354
|
getSteeringInput: () => {
|
|
@@ -216,6 +366,8 @@ export async function startInkTui(config, spawner) {
|
|
|
216
366
|
return result;
|
|
217
367
|
},
|
|
218
368
|
};
|
|
369
|
+
// Phren's past-tense verbs for turn summaries
|
|
370
|
+
const PAST_VERBS = ["Recalled", "Reasoned", "Connected", "Synthesized", "Reflected", "Distilled", "Threaded", "Mapped"];
|
|
219
371
|
async function runAgentTurn(userInput) {
|
|
220
372
|
running = true;
|
|
221
373
|
thinking = true;
|
|
@@ -223,6 +375,8 @@ export async function startInkTui(config, spawner) {
|
|
|
223
375
|
thinkElapsed = null;
|
|
224
376
|
streamingText = "";
|
|
225
377
|
currentToolCalls = [];
|
|
378
|
+
activeTool = null;
|
|
379
|
+
const pastVerb = PAST_VERBS[Math.floor(Math.random() * PAST_VERBS.length)];
|
|
226
380
|
update();
|
|
227
381
|
try {
|
|
228
382
|
await runTurn(userInput, session, config, tuiHooks);
|
|
@@ -245,8 +399,10 @@ export async function startInkTui(config, spawner) {
|
|
|
245
399
|
}
|
|
246
400
|
streamingText = "";
|
|
247
401
|
currentToolCalls = [];
|
|
402
|
+
activeTool = null;
|
|
248
403
|
running = false;
|
|
249
|
-
thinkElapsed = elapsed
|
|
404
|
+
thinkElapsed = `${pastVerb} for ${elapsed}s`;
|
|
405
|
+
process.stdout.write("\x07"); // terminal bell on completion
|
|
250
406
|
update();
|
|
251
407
|
// Clear elapsed indicator after a brief display
|
|
252
408
|
setTimeout(() => {
|
|
@@ -268,19 +424,162 @@ export async function startInkTui(config, spawner) {
|
|
|
268
424
|
runAgentTurn(queued);
|
|
269
425
|
}
|
|
270
426
|
}
|
|
271
|
-
//
|
|
272
|
-
|
|
273
|
-
// Clear screen
|
|
274
|
-
|
|
427
|
+
// ── Multi-agent state ──────────────────────────────────────────────────
|
|
428
|
+
// TeamAgents: each gets their OWN conversation (separate from main stream).
|
|
429
|
+
// Switch between them with /agent <name>. Clear screen + re-render on switch.
|
|
430
|
+
let selectedAgentId = null; // null = main orchestrator
|
|
431
|
+
let agentTabs = [];
|
|
432
|
+
const TAB_COLORS = ["cyan", "magenta", "yellow", "green", "blue", "red", "white", "cyanBright"];
|
|
433
|
+
const agentConvos = new Map();
|
|
434
|
+
function getOrCreateConvo(agentId) {
|
|
435
|
+
let convo = agentConvos.get(agentId);
|
|
436
|
+
if (!convo) {
|
|
437
|
+
convo = { messages: [], streamingText: "", toolCalls: [], activeTool: null };
|
|
438
|
+
agentConvos.set(agentId, convo);
|
|
439
|
+
}
|
|
440
|
+
return convo;
|
|
441
|
+
}
|
|
442
|
+
function rebuildAgentTabs() {
|
|
443
|
+
if (!spawner) {
|
|
444
|
+
agentTabs = [];
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
agentTabs = spawner.listAgents().map((a, i) => ({
|
|
448
|
+
id: a.id,
|
|
449
|
+
name: a.displayName || a.task.slice(0, 20),
|
|
450
|
+
status: a.status,
|
|
451
|
+
color: TAB_COLORS[i % TAB_COLORS.length],
|
|
452
|
+
}));
|
|
453
|
+
}
|
|
454
|
+
// Switch views: clear screen so <Static> re-renders with fresh IDs
|
|
455
|
+
let viewSwitchCounter = 0;
|
|
456
|
+
function handleSelectAgent(agentId) {
|
|
457
|
+
if (agentId === selectedAgentId)
|
|
458
|
+
return;
|
|
459
|
+
selectedAgentId = agentId;
|
|
460
|
+
viewSwitchCounter++;
|
|
461
|
+
if (appInstance)
|
|
462
|
+
appInstance.clear();
|
|
463
|
+
else
|
|
464
|
+
process.stdout.write("\x1b[2J\x1b[H");
|
|
465
|
+
update();
|
|
466
|
+
}
|
|
467
|
+
function handleCancelAgent() {
|
|
468
|
+
if (selectedAgentId && selectedAgentId !== "__main__" && spawner) {
|
|
469
|
+
const agent = spawner.getAgent(selectedAgentId);
|
|
470
|
+
if (agent && (agent.status === "running" || agent.status === "starting")) {
|
|
471
|
+
spawner.cancel(selectedAgentId);
|
|
472
|
+
const convo = getOrCreateConvo(selectedAgentId);
|
|
473
|
+
convo.messages.push({ id: nextId(), kind: "status", text: "Cancelled" });
|
|
474
|
+
update();
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
handleCancelTurn();
|
|
479
|
+
}
|
|
480
|
+
// Wire spawner events — each agent's output goes to its OWN conversation
|
|
481
|
+
if (spawner) {
|
|
482
|
+
spawner.on("text_delta", (agentId, text) => {
|
|
483
|
+
const convo = getOrCreateConvo(agentId);
|
|
484
|
+
convo.streamingText += text;
|
|
485
|
+
// Only update if this agent is currently selected
|
|
486
|
+
if (selectedAgentId === agentId)
|
|
487
|
+
update();
|
|
488
|
+
});
|
|
489
|
+
spawner.on("text_block", (agentId, text) => {
|
|
490
|
+
const convo = getOrCreateConvo(agentId);
|
|
491
|
+
convo.streamingText += text;
|
|
492
|
+
if (selectedAgentId === agentId)
|
|
493
|
+
update();
|
|
494
|
+
});
|
|
495
|
+
spawner.on("tool_start", (agentId, toolName, input) => {
|
|
496
|
+
const convo = getOrCreateConvo(agentId);
|
|
497
|
+
convo.activeTool = { name: toolName, preview: formatToolInput(toolName, input) };
|
|
498
|
+
rebuildAgentTabs();
|
|
499
|
+
if (selectedAgentId === agentId)
|
|
500
|
+
update();
|
|
501
|
+
});
|
|
502
|
+
spawner.on("tool_end", (agentId, toolName, input, output, isError, durationMs) => {
|
|
503
|
+
const convo = getOrCreateConvo(agentId);
|
|
504
|
+
convo.activeTool = null;
|
|
505
|
+
const diffData = (toolName === "edit_file" || toolName === "write_file") ? decodeDiffPayload(output) : null;
|
|
506
|
+
const cleanOutput = diffData ? output.slice(0, output.indexOf(DIFF_MARKER)) : output;
|
|
507
|
+
const diffRendered = diffData ? renderInlineDiff(diffData.oldContent, diffData.newContent, diffData.filePath, theme.diff) : undefined;
|
|
508
|
+
convo.toolCalls.push({ name: toolName, input, output: cleanOutput, isError, durationMs, diffRendered });
|
|
509
|
+
rebuildAgentTabs();
|
|
510
|
+
if (selectedAgentId === agentId)
|
|
511
|
+
update();
|
|
512
|
+
});
|
|
513
|
+
spawner.on("done", (agentId, result) => {
|
|
514
|
+
const convo = getOrCreateConvo(agentId);
|
|
515
|
+
if (convo.streamingText || convo.toolCalls.length > 0) {
|
|
516
|
+
convo.messages.push({
|
|
517
|
+
id: nextId(),
|
|
518
|
+
kind: "assistant",
|
|
519
|
+
text: convo.streamingText || result.finalText,
|
|
520
|
+
toolCalls: convo.toolCalls.length > 0 ? [...convo.toolCalls] : undefined,
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
else if (result.finalText) {
|
|
524
|
+
convo.messages.push({ id: nextId(), kind: "assistant", text: result.finalText });
|
|
525
|
+
}
|
|
526
|
+
convo.streamingText = "";
|
|
527
|
+
convo.toolCalls = [];
|
|
528
|
+
convo.activeTool = null;
|
|
529
|
+
rebuildAgentTabs();
|
|
530
|
+
// Notify main stream that an agent completed
|
|
531
|
+
completedMessages.push({
|
|
532
|
+
id: nextId(),
|
|
533
|
+
kind: "status",
|
|
534
|
+
text: `\x1b[2m${spawner.getAgent(agentId)?.displayName || agentId} completed (${result.turns} turns, ${result.toolCalls} tools)\x1b[0m`,
|
|
535
|
+
});
|
|
536
|
+
update();
|
|
537
|
+
});
|
|
538
|
+
spawner.on("error", (agentId, error) => {
|
|
539
|
+
const convo = getOrCreateConvo(agentId);
|
|
540
|
+
convo.messages.push({ id: nextId(), kind: "status", text: `Error: ${error}` });
|
|
541
|
+
convo.streamingText = "";
|
|
542
|
+
convo.activeTool = null;
|
|
543
|
+
rebuildAgentTabs();
|
|
544
|
+
update();
|
|
545
|
+
});
|
|
546
|
+
spawner.on("idle", (agentId, reason) => {
|
|
547
|
+
const convo = getOrCreateConvo(agentId);
|
|
548
|
+
convo.messages.push({ id: nextId(), kind: "status", text: `idle (${reason})` });
|
|
549
|
+
rebuildAgentTabs();
|
|
550
|
+
update();
|
|
551
|
+
});
|
|
552
|
+
spawner.on("status", (agentId, message) => {
|
|
553
|
+
const convo = getOrCreateConvo(agentId);
|
|
554
|
+
convo.messages.push({ id: nextId(), kind: "status", text: message });
|
|
555
|
+
if (selectedAgentId === agentId)
|
|
556
|
+
update();
|
|
557
|
+
});
|
|
558
|
+
spawner.on("message", (from, to, content) => {
|
|
559
|
+
// Show DM notification in main stream
|
|
560
|
+
const preview = content.length > 80 ? content.slice(0, 80) + "..." : content;
|
|
561
|
+
completedMessages.push({
|
|
562
|
+
id: nextId(),
|
|
563
|
+
kind: "status",
|
|
564
|
+
text: `\x1b[2m${from} → ${to}: ${preview}\x1b[0m`,
|
|
565
|
+
});
|
|
566
|
+
update();
|
|
567
|
+
});
|
|
568
|
+
spawner.on("exit", () => {
|
|
569
|
+
rebuildAgentTabs();
|
|
570
|
+
update();
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
// Set terminal title
|
|
275
574
|
const projectName = config.phrenCtx?.project ?? "phren";
|
|
276
575
|
process.title = "phren";
|
|
277
576
|
process.stdout.write(`\x1b]0;phren \xb7 ${projectName}\x07`); // set terminal window title
|
|
278
577
|
// Initial render
|
|
279
|
-
const app = render(_jsx(App, { state: getAppState(), completedMessages: [], streamingText: "", completedToolCalls: [], thinking: false, thinkStartTime: 0, thinkElapsed: null, steerQueue: [], running: false, showBanner: true, inputHistory: [], verbose: verbose, onSubmit: handleSubmit, onPermissionCycle: handlePermissionCycle, onCancelTurn: handleCancelTurn, onExit: handleExit }), { exitOnCtrlC: false });
|
|
578
|
+
const app = render(_jsx(App, { state: getAppState(), completedMessages: [], streamingText: "", completedToolCalls: [], activeTool: null, thinking: false, thinkStartTime: 0, thinkElapsed: null, steerQueue: [], running: false, showBanner: true, inputHistory: [], verbose: verbose, theme: theme, onSubmit: handleSubmit, onPermissionCycle: handlePermissionCycle, onCancelTurn: handleCancelTurn, onExit: handleExit, agents: undefined, selectedAgentId: undefined, onCancelAgent: handleCancelAgent, onSelectAgent: handleSelectAgent }), { exitOnCtrlC: false });
|
|
280
579
|
rerender = app.rerender;
|
|
580
|
+
appInstance = app;
|
|
281
581
|
const done = new Promise((r) => { resolveSession = r; });
|
|
282
582
|
app.waitUntilExit().then(() => {
|
|
283
|
-
process.stdout.write("\x1b[?2004l"); // disable bracketed paste
|
|
284
583
|
if (resolveSession)
|
|
285
584
|
resolveSession(session);
|
|
286
585
|
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mouse support — SGR extended mode for terminal mouse events.
|
|
3
|
+
* Enables click detection for agent tab switching and hyperlink clicks.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Enable mouse tracking on the terminal.
|
|
7
|
+
* Uses SGR extended mode (\x1b[?1006h) for coordinates > 223.
|
|
8
|
+
* Also enables normal tracking (\x1b[?1000h) for basic button events.
|
|
9
|
+
*/
|
|
10
|
+
export function enableMouse(stdout) {
|
|
11
|
+
if (!stdout.isTTY)
|
|
12
|
+
return;
|
|
13
|
+
stdout.write("\x1b[?1000h"); // Normal tracking (button press/release)
|
|
14
|
+
stdout.write("\x1b[?1006h"); // SGR extended mode
|
|
15
|
+
}
|
|
16
|
+
/** Disable mouse tracking and restore normal terminal behavior. */
|
|
17
|
+
export function disableMouse(stdout) {
|
|
18
|
+
if (!stdout.isTTY)
|
|
19
|
+
return;
|
|
20
|
+
stdout.write("\x1b[?1006l");
|
|
21
|
+
stdout.write("\x1b[?1000l");
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Try to parse an SGR mouse event from raw input.
|
|
25
|
+
* SGR format: \x1b[<button;column;row(M|m)
|
|
26
|
+
* M = press, m = release
|
|
27
|
+
* Returns null if the input is not a mouse event.
|
|
28
|
+
*/
|
|
29
|
+
export function parseMouseEvent(input) {
|
|
30
|
+
// SGR extended: \x1b[<Cb;Cx;CyM or \x1b[<Cb;Cx;Cym
|
|
31
|
+
const match = input.match(/^\x1b\[<(\d+);(\d+);(\d+)([Mm])$/);
|
|
32
|
+
if (!match)
|
|
33
|
+
return null;
|
|
34
|
+
const code = parseInt(match[1], 10);
|
|
35
|
+
const x = parseInt(match[2], 10);
|
|
36
|
+
const y = parseInt(match[3], 10);
|
|
37
|
+
const isRelease = match[4] === "m";
|
|
38
|
+
const shift = (code & 4) !== 0;
|
|
39
|
+
const alt = (code & 8) !== 0;
|
|
40
|
+
const ctrl = (code & 16) !== 0;
|
|
41
|
+
const baseCode = code & ~(4 | 8 | 16); // strip modifier bits
|
|
42
|
+
let button = "none";
|
|
43
|
+
let type = isRelease ? "release" : "press";
|
|
44
|
+
if (baseCode === 0)
|
|
45
|
+
button = "left";
|
|
46
|
+
else if (baseCode === 1)
|
|
47
|
+
button = "middle";
|
|
48
|
+
else if (baseCode === 2)
|
|
49
|
+
button = "right";
|
|
50
|
+
else if (baseCode === 64)
|
|
51
|
+
button = "scrollUp";
|
|
52
|
+
else if (baseCode === 65)
|
|
53
|
+
button = "scrollDown";
|
|
54
|
+
else if (baseCode === 32) {
|
|
55
|
+
button = "left";
|
|
56
|
+
type = "move";
|
|
57
|
+
}
|
|
58
|
+
else if (baseCode === 33) {
|
|
59
|
+
button = "middle";
|
|
60
|
+
type = "move";
|
|
61
|
+
}
|
|
62
|
+
else if (baseCode === 34) {
|
|
63
|
+
button = "right";
|
|
64
|
+
type = "move";
|
|
65
|
+
}
|
|
66
|
+
else if (baseCode === 35) {
|
|
67
|
+
button = "none";
|
|
68
|
+
type = "move";
|
|
69
|
+
}
|
|
70
|
+
return { type, button, x, y, shift, ctrl, alt };
|
|
71
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Terminal control — manages scroll regions and fixed status bar.
|
|
3
|
+
* Uses DECSTBM (Set Top and Bottom Margins) to create a scrollable area
|
|
4
|
+
* that excludes the bottom N lines, keeping the status bar pinned.
|
|
5
|
+
*/
|
|
6
|
+
export class TerminalControl {
|
|
7
|
+
statusHeight = 1;
|
|
8
|
+
enabled = false;
|
|
9
|
+
resizeHandler = null;
|
|
10
|
+
/** Enable scroll regions. Call after Ink render() is set up. */
|
|
11
|
+
enable(stdout) {
|
|
12
|
+
if (!stdout.isTTY || this.enabled)
|
|
13
|
+
return;
|
|
14
|
+
this.enabled = true;
|
|
15
|
+
this.setScrollRegion(stdout);
|
|
16
|
+
this.resizeHandler = () => {
|
|
17
|
+
this.setScrollRegion(stdout);
|
|
18
|
+
this.renderStatusBar(stdout, this.lastContent);
|
|
19
|
+
};
|
|
20
|
+
stdout.on("resize", this.resizeHandler);
|
|
21
|
+
}
|
|
22
|
+
/** Disable scroll regions and restore full terminal. */
|
|
23
|
+
disable(stdout) {
|
|
24
|
+
if (!this.enabled)
|
|
25
|
+
return;
|
|
26
|
+
this.enabled = false;
|
|
27
|
+
// Reset scroll region to full screen
|
|
28
|
+
stdout.write("\x1b[r");
|
|
29
|
+
// Clear status bar line
|
|
30
|
+
const rows = stdout.rows || 24;
|
|
31
|
+
stdout.write(`\x1b[${rows};1H\x1b[2K`);
|
|
32
|
+
if (this.resizeHandler) {
|
|
33
|
+
stdout.off("resize", this.resizeHandler);
|
|
34
|
+
this.resizeHandler = null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
lastContent = "";
|
|
38
|
+
setScrollRegion(stdout) {
|
|
39
|
+
const rows = stdout.rows || 24;
|
|
40
|
+
// Set scrollable region to exclude bottom statusHeight lines
|
|
41
|
+
stdout.write(`\x1b[1;${rows - this.statusHeight}r`);
|
|
42
|
+
// Move cursor back to the scroll region (don't leave it in the status area)
|
|
43
|
+
stdout.write(`\x1b[${rows - this.statusHeight};1H`);
|
|
44
|
+
}
|
|
45
|
+
/** Render content in the fixed status bar area. */
|
|
46
|
+
renderStatusBar(stdout, content) {
|
|
47
|
+
if (!this.enabled)
|
|
48
|
+
return;
|
|
49
|
+
this.lastContent = content;
|
|
50
|
+
const rows = stdout.rows || 24;
|
|
51
|
+
const cols = stdout.columns || 80;
|
|
52
|
+
// Save cursor, move to status line, clear it, write content, restore cursor
|
|
53
|
+
stdout.write("\x1b[s"); // save cursor
|
|
54
|
+
stdout.write(`\x1b[${rows};1H`); // move to last row
|
|
55
|
+
stdout.write("\x1b[2K"); // clear line
|
|
56
|
+
stdout.write(content.slice(0, cols)); // write (truncate to width)
|
|
57
|
+
stdout.write("\x1b[u"); // restore cursor
|
|
58
|
+
}
|
|
59
|
+
get isEnabled() {
|
|
60
|
+
return this.enabled;
|
|
61
|
+
}
|
|
62
|
+
}
|