-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathagent-manager-parallel.ts
More file actions
302 lines (259 loc) · 10.5 KB
/
agent-manager-parallel.ts
File metadata and controls
302 lines (259 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import type { Theme } from "@mariozechner/pi-coding-agent";
import { matchesKey, truncateToWidth, visibleWidth } from "@mariozechner/pi-tui";
import type { TextEditorState } from "./text-editor.js";
import { createEditorState, handleEditorInput, renderEditor, wrapText, getCursorDisplayPos, ensureCursorVisible } from "./text-editor.js";
import { pad, row, renderHeader, renderFooter, fuzzyFilter } from "./render-helpers.js";
export interface ParallelSlot {
agentName: string;
customTask: string;
}
export interface ParallelState {
slots: ParallelSlot[];
cursor: number;
scrollOffset: number;
mode: "browse" | "add" | "edit-task";
addQuery: string;
addCursor: number;
editIndex: number;
editEditor: TextEditorState | null;
}
export type ParallelAction =
| { type: "proceed" }
| { type: "back" };
export interface AgentOption {
name: string;
description: string;
model?: string;
}
const CONTENT_HEIGHT = 14;
const SLOT_VIEWPORT_BROWSE = 12;
const SLOT_VIEWPORT_COMPACT = 5;
const ADD_RESULTS_MAX = 5;
export function createParallelState(agentNames: string[]): ParallelState {
return {
slots: agentNames.map((name) => ({ agentName: name, customTask: "" })),
cursor: 0,
scrollOffset: 0,
mode: "browse",
addQuery: "",
addCursor: 0,
editIndex: -1,
editEditor: null,
};
}
function clampSlotScroll(state: ParallelState, viewport: number): void {
if (state.slots.length === 0) { state.cursor = 0; state.scrollOffset = 0; return; }
state.cursor = Math.max(0, Math.min(state.cursor, state.slots.length - 1));
const maxOffset = Math.max(0, state.slots.length - viewport);
state.scrollOffset = Math.max(0, Math.min(state.scrollOffset, maxOffset));
if (state.cursor < state.scrollOffset) state.scrollOffset = state.cursor;
else if (state.cursor >= state.scrollOffset + viewport) state.scrollOffset = state.cursor - viewport + 1;
}
export function handleParallelInput(
state: ParallelState,
agents: AgentOption[],
data: string,
width: number,
): ParallelAction | undefined {
switch (state.mode) {
case "browse": return handleBrowse(state, data);
case "add": return handleAdd(state, agents, data);
case "edit-task": return handleEditTask(state, data, width);
}
}
function handleBrowse(state: ParallelState, data: string): ParallelAction | undefined {
if (matchesKey(data, "escape") || matchesKey(data, "ctrl+c")) return { type: "back" };
if (matchesKey(data, "up")) { state.cursor = Math.max(0, state.cursor - 1); clampSlotScroll(state, SLOT_VIEWPORT_BROWSE); return; }
if (matchesKey(data, "down")) { state.cursor = Math.min(state.slots.length - 1, state.cursor + 1); clampSlotScroll(state, SLOT_VIEWPORT_BROWSE); return; }
if (matchesKey(data, "ctrl+a")) {
state.mode = "add";
state.addQuery = "";
state.addCursor = 0;
return;
}
if (matchesKey(data, "delete") || matchesKey(data, "ctrl+d")) {
if (state.slots.length > 1) {
state.slots.splice(state.cursor, 1);
state.cursor = Math.min(state.cursor, state.slots.length - 1);
clampSlotScroll(state, SLOT_VIEWPORT_BROWSE);
}
return;
}
if (matchesKey(data, "return")) {
if (state.slots.length === 0) return;
state.mode = "edit-task";
state.editIndex = state.cursor;
state.editEditor = createEditorState(state.slots[state.cursor]!.customTask);
return;
}
if (matchesKey(data, "ctrl+r")) {
if (state.slots.length >= 2) return { type: "proceed" };
return;
}
return;
}
function handleAdd(state: ParallelState, agents: AgentOption[], data: string): ParallelAction | undefined {
if (matchesKey(data, "escape") || matchesKey(data, "ctrl+c")) { state.mode = "browse"; return; }
const filtered = fuzzyFilter(agents, state.addQuery);
if (matchesKey(data, "up")) { state.addCursor = Math.max(0, state.addCursor - 1); return; }
if (matchesKey(data, "down")) { state.addCursor = Math.min(Math.max(0, filtered.length - 1), state.addCursor + 1); return; }
if (matchesKey(data, "return")) {
const selected = filtered[state.addCursor];
if (selected) {
state.slots.push({ agentName: selected.name, customTask: "" });
state.cursor = state.slots.length - 1;
clampSlotScroll(state, SLOT_VIEWPORT_BROWSE);
}
state.mode = "browse";
return;
}
if (matchesKey(data, "backspace")) {
state.addQuery = state.addQuery.slice(0, -1);
state.addCursor = 0;
return;
}
if (data.length === 1 && data.charCodeAt(0) >= 32) {
state.addQuery += data;
state.addCursor = 0;
return;
}
return;
}
function handleEditTask(state: ParallelState, data: string, width: number): ParallelAction | undefined {
if (!state.editEditor) { state.mode = "browse"; return; }
if (matchesKey(data, "escape") || matchesKey(data, "ctrl+c")) {
state.editEditor = null;
state.mode = "browse";
return;
}
if (matchesKey(data, "return")) {
const slot = state.slots[state.editIndex];
if (slot) slot.customTask = state.editEditor.buffer.trim();
state.editEditor = null;
state.mode = "browse";
return;
}
if (matchesKey(data, "tab")) return;
const innerW = width - 2;
const boxInnerWidth = Math.max(10, innerW - 4);
const nextState = handleEditorInput(state.editEditor, data, boxInnerWidth);
if (nextState) state.editEditor = nextState;
return;
}
function renderSlotLine(
slot: ParallelSlot,
slotNumber: number,
isCursor: boolean,
width: number,
theme: Theme,
): string {
const cursor = isCursor ? theme.fg("accent", "▸") : " ";
const num = theme.fg("accent", slotNumber.toString().padStart(2));
const name = isCursor ? theme.fg("accent", slot.agentName) : slot.agentName;
const nameWidth = 16;
const prefix = `${cursor} ${num} `;
const prefixVis = visibleWidth(prefix);
const nameStr = pad(truncateToWidth(name, nameWidth), nameWidth);
if (slot.customTask) {
const taskWidth = Math.max(0, width - 2 - prefixVis - nameWidth - 3);
const taskPreview = theme.fg("dim", `"${truncateToWidth(slot.customTask, Math.max(0, taskWidth - 2))}"`);
return ` ${prefix}${nameStr} ${taskPreview}`;
}
return ` ${prefix}${nameStr}`;
}
export function renderParallel(
state: ParallelState,
agents: AgentOption[],
width: number,
theme: Theme,
): string[] {
const lines: string[] = [];
lines.push(renderHeader(" Parallel Builder ", width, theme));
lines.push(row("", width, theme));
const contentLines: string[] = [];
if (state.mode === "browse") {
clampSlotScroll(state, SLOT_VIEWPORT_BROWSE);
const start = state.scrollOffset;
const end = Math.min(state.slots.length, start + SLOT_VIEWPORT_BROWSE);
for (let i = start; i < end; i++) {
contentLines.push(renderSlotLine(state.slots[i]!, i + 1, i === state.cursor, width, theme));
}
} else if (state.mode === "add") {
const slotLines = Math.min(state.slots.length, SLOT_VIEWPORT_COMPACT);
const slotStart = Math.max(0, state.slots.length - slotLines);
for (let i = slotStart; i < state.slots.length; i++) {
contentLines.push(renderSlotLine(state.slots[i]!, i + 1, false, width, theme));
}
contentLines.push("");
const searchIcon = theme.fg("dim", "◎");
const cursor = theme.fg("accent", "│");
const placeholder = theme.fg("dim", "\x1b[3msearch agents...\x1b[23m");
const queryDisplay = state.addQuery ? `${state.addQuery}${cursor}` : `${cursor}${placeholder}`;
contentLines.push(` ${searchIcon} ${queryDisplay}`);
const filtered = fuzzyFilter(agents, state.addQuery);
const addStart = Math.max(0, Math.min(state.addCursor - ADD_RESULTS_MAX + 1, filtered.length - ADD_RESULTS_MAX));
const addEnd = Math.min(filtered.length, addStart + ADD_RESULTS_MAX);
for (let i = addStart; i < addEnd; i++) {
const a = filtered[i]!;
const isCursor = i === state.addCursor;
const cur = isCursor ? theme.fg("accent", "▸") : " ";
const nameStr = isCursor ? theme.fg("accent", a.name) : a.name;
const descWidth = Math.max(0, width - 2 - 1 - 1 - 16 - 2);
contentLines.push(` ${cur} ${pad(truncateToWidth(nameStr, 16), 16)} ${theme.fg("dim", truncateToWidth(a.description, descWidth))}`);
}
} else if (state.mode === "edit-task") {
const slotLines = Math.min(state.slots.length, SLOT_VIEWPORT_COMPACT);
const slotStart = Math.max(0, Math.min(state.editIndex - Math.floor(slotLines / 2), state.slots.length - slotLines));
for (let i = slotStart; i < slotStart + slotLines; i++) {
contentLines.push(renderSlotLine(state.slots[i]!, i + 1, i === state.editIndex, width, theme));
}
contentLines.push("");
const slot = state.slots[state.editIndex];
const label = slot ? `Task for ${slot.agentName} (slot ${state.editIndex + 1}):` : "Task:";
contentLines.push(` ${theme.fg("dim", label)}`);
const innerW = width - 2;
const boxInnerWidth = Math.max(10, innerW - 4);
contentLines.push(` \u250C${"\u2500".repeat(boxInnerWidth)}\u2510`);
if (state.editEditor) {
const editorState = { ...state.editEditor };
const wrapped = wrapText(editorState.buffer, boxInnerWidth);
const cursorPos = getCursorDisplayPos(editorState.cursor, wrapped.starts);
editorState.viewportOffset = ensureCursorVisible(cursorPos.line, 1, editorState.viewportOffset);
const editorLine = renderEditor(editorState, boxInnerWidth, 1)[0] ?? "";
contentLines.push(` \u2502${pad(editorLine, boxInnerWidth)}\u2502`);
} else {
contentLines.push(` \u2502${pad("", boxInnerWidth)}\u2502`);
}
contentLines.push(` \u2514${"\u2500".repeat(boxInnerWidth)}\u2518`);
contentLines.push(` ${theme.fg("dim", "Empty = use shared task")}`);
}
for (const line of contentLines) lines.push(row(line, width, theme));
for (let i = contentLines.length; i < CONTENT_HEIGHT; i++) lines.push(row("", width, theme));
let statusText = "";
if (state.mode === "browse") {
if (state.slots.length < 2) {
statusText = theme.fg("dim", `${state.slots.length} agent — add at least 2 for parallel`);
} else {
statusText = theme.fg("dim", formatSlotSummary(state.slots));
}
}
lines.push(row(statusText ? ` ${statusText}` : "", width, theme));
let footerText: string;
if (state.mode === "add") {
footerText = " [enter] add [esc] cancel ";
} else if (state.mode === "edit-task") {
footerText = " [enter] save [esc] cancel ";
} else {
footerText = " [ctrl+a] add [del] remove [enter] edit task [ctrl+r] continue [esc] back ";
}
lines.push(renderFooter(footerText, width, theme));
return lines;
}
function formatSlotSummary(slots: ParallelSlot[]): string {
const counts = new Map<string, number>();
for (const s of slots) counts.set(s.agentName, (counts.get(s.agentName) ?? 0) + 1);
return [...counts.entries()].map(([name, count]) => count > 1 ? `${count}\u00D7 ${name}` : name).join(" + ");
}
export function formatParallelTitle(slots: ParallelSlot[]): string {
return `Parallel: ${formatSlotSummary(slots)}`;
}