-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathagent-manager-list.ts
More file actions
276 lines (237 loc) · 8.98 KB
/
agent-manager-list.ts
File metadata and controls
276 lines (237 loc) · 8.98 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
import type { Theme } from "@mariozechner/pi-coding-agent";
import type { AgentSource } from "./agents.ts";
import { matchesKey, truncateToWidth, visibleWidth } from "@mariozechner/pi-tui";
import { pad, row, renderHeader, renderFooter, fuzzyFilter, formatScrollInfo } from "./render-helpers.ts";
export interface ListAgent {
id: string;
name: string;
description: string;
model?: string;
source: AgentSource;
overrideScope?: "user" | "project";
disabled?: boolean;
kind: "agent" | "chain";
stepCount?: number;
}
export interface ListState {
cursor: number;
scrollOffset: number;
filterQuery: string;
selected: string[];
}
export type ListAction =
| { type: "open-detail"; id: string }
| { type: "clone"; id: string }
| { type: "new" }
| { type: "delete"; id: string }
| { type: "run-chain"; ids: string[] }
| { type: "run-parallel"; ids: string[] }
| { type: "close" };
const LIST_VIEWPORT_HEIGHT = 8;
function selectionCount(selected: string[], id: string): number {
let count = 0;
for (const s of selected) if (s === id) count++;
return count;
}
function clampCursor(state: ListState, filtered: ListAgent[]): void {
if (filtered.length === 0) {
state.cursor = 0;
state.scrollOffset = 0;
return;
}
state.cursor = Math.max(0, Math.min(state.cursor, filtered.length - 1));
const maxOffset = Math.max(0, filtered.length - LIST_VIEWPORT_HEIGHT);
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 + LIST_VIEWPORT_HEIGHT) {
state.scrollOffset = state.cursor - LIST_VIEWPORT_HEIGHT + 1;
}
}
export function handleListInput(state: ListState, agents: ListAgent[], data: string): ListAction | undefined {
const filtered = fuzzyFilter(agents, state.filterQuery);
if (matchesKey(data, "escape") || matchesKey(data, "ctrl+c")) {
if (state.filterQuery.length > 0) {
state.filterQuery = "";
state.cursor = 0;
state.scrollOffset = 0;
return;
}
if (state.selected.length > 0) {
state.selected.length = 0;
return;
}
return { type: "close" };
}
if (matchesKey(data, "return")) {
if (filtered.length > 0) {
const agent = filtered[state.cursor];
if (agent) return { type: "open-detail", id: agent.id };
}
return;
}
if (matchesKey(data, "up") || matchesKey(data, "down")) {
if (matchesKey(data, "up")) state.cursor -= 1;
if (matchesKey(data, "down")) state.cursor += 1;
clampCursor(state, filtered);
return;
}
if (matchesKey(data, "backspace")) {
if (state.filterQuery.length > 0) {
state.filterQuery = state.filterQuery.slice(0, -1);
state.cursor = 0;
state.scrollOffset = 0;
}
return;
}
if (matchesKey(data, "alt+n")) {
return { type: "new" };
}
if (matchesKey(data, "ctrl+k")) {
const agent = filtered[state.cursor];
if (agent) return { type: "clone", id: agent.id };
return;
}
if (matchesKey(data, "ctrl+d") || matchesKey(data, "delete")) {
const agent = filtered[state.cursor];
if (agent) return { type: "delete", id: agent.id };
return;
}
if (matchesKey(data, "tab")) {
const agent = filtered[state.cursor];
if (!agent) return;
if (agent.kind !== "agent") return;
state.selected.push(agent.id);
return;
}
if (matchesKey(data, "shift+tab")) {
const agent = filtered[state.cursor];
if (!agent) return;
const lastIdx = state.selected.lastIndexOf(agent.id);
if (lastIdx >= 0) state.selected.splice(lastIdx, 1);
return;
}
if (matchesKey(data, "ctrl+r")) {
if (state.selected.length > 0) return { type: "run-chain", ids: [...state.selected] };
const agent = filtered[state.cursor];
if (agent && agent.kind === "agent") return { type: "run-chain", ids: [agent.id] };
return;
}
if (matchesKey(data, "ctrl+p")) {
if (state.selected.length > 0) return { type: "run-parallel", ids: [...state.selected] };
const agent = filtered[state.cursor];
if (agent && agent.kind === "agent") return { type: "run-parallel", ids: [agent.id] };
return;
}
if (data.length === 1 && data.charCodeAt(0) >= 32) {
state.filterQuery += data;
state.cursor = 0;
state.scrollOffset = 0;
return;
}
return;
}
export function renderList(
state: ListState,
agents: ListAgent[],
width: number,
theme: Theme,
statusMessage?: { text: string; type: "error" | "info" },
): string[] {
const lines: string[] = [];
const filtered = fuzzyFilter(agents, state.filterQuery);
clampCursor(state, filtered);
const agentCount = agents.filter((a) => a.kind === "agent").length;
const chainCount = agents.filter((a) => a.kind === "chain").length;
const headerText = chainCount
? ` Subagents [${agentCount} agents ${chainCount} chains] `
: ` Subagents [${agentCount}] `;
lines.push(renderHeader(headerText, width, theme));
lines.push(row("", width, theme));
const cursor = theme.fg("accent", "│");
const searchIcon = theme.fg("dim", "◎");
const placeholder = theme.fg("dim", "\x1b[3mtype to filter...\x1b[23m");
const queryDisplay = state.filterQuery ? `${state.filterQuery}${cursor}` : `${cursor}${placeholder}`;
lines.push(row(` ${searchIcon} ${queryDisplay}`, width, theme));
lines.push(row("", width, theme));
const userNames = new Set(agents.filter((a) => a.source === "user" && a.kind === "agent").map((a) => a.name));
const startIdx = state.scrollOffset;
const endIdx = Math.min(filtered.length, startIdx + LIST_VIEWPORT_HEIGHT);
const visible = filtered.slice(startIdx, endIdx);
if (filtered.length === 0) {
lines.push(row(` ${theme.fg("dim", "No matching agents")}`, width, theme));
for (let i = 1; i < LIST_VIEWPORT_HEIGHT; i++) lines.push(row("", width, theme));
} else {
const innerW = width - 2;
const nameWidth = 16;
const modelWidth = 12;
const scopeWidth = 21;
for (let i = 0; i < visible.length; i++) {
const agent = visible[i]!;
const index = startIdx + i;
const isCursor = index === state.cursor;
const count = selectionCount(state.selected, agent.id);
const isShadowed = agent.kind === "agent" && agent.source === "project" && userNames.has(agent.name);
const cursorChar = isCursor ? theme.fg("accent", ">") : " ";
const selectBadge = count > 0 ? theme.fg("accent", String(count).padStart(2)) : " ";
const shadowMarker = isShadowed ? theme.fg("warning", "!") : " ";
const prefix = `${cursorChar}${selectBadge}${shadowMarker} `;
const modelRaw = agent.kind === "chain" ? `${agent.stepCount ?? 0} steps` : (agent.model ?? "default");
const modelDisplay = modelRaw.includes("/") ? modelRaw.split("/").pop() ?? modelRaw : modelRaw;
const nameText = isCursor ? theme.fg("accent", agent.name) : agent.name;
const modelText = theme.fg("dim", modelDisplay);
const scopeLabel = agent.kind === "chain"
? "[chain]"
: agent.source === "builtin"
? (agent.disabled
? (agent.overrideScope ? `[builtin off+${agent.overrideScope}]` : "[builtin off]")
: (agent.overrideScope ? `[builtin+${agent.overrideScope}]` : "[builtin]"))
: agent.source === "project"
? "[proj]"
: "[user]";
const scopeBadge = theme.fg("dim", scopeLabel);
const descText = theme.fg("dim", agent.description);
const descWidth = Math.max(0, innerW - 1 - visibleWidth(prefix) - nameWidth - modelWidth - scopeWidth - 3);
const line =
prefix +
pad(truncateToWidth(nameText, nameWidth), nameWidth) +
" " +
pad(truncateToWidth(modelText, modelWidth), modelWidth) +
" " +
pad(scopeBadge, scopeWidth) +
" " +
truncateToWidth(descText, descWidth);
lines.push(row(` ${line}`, width, theme));
}
for (let i = visible.length; i < LIST_VIEWPORT_HEIGHT; i++) {
lines.push(row("", width, theme));
}
}
const scrollInfo = formatScrollInfo(state.scrollOffset, Math.max(0, filtered.length - (state.scrollOffset + LIST_VIEWPORT_HEIGHT)));
const selectedNames = state.selected
.map((id) => agents.find((a) => a.id === id))
.filter((a): a is ListAgent => Boolean(a))
.map((a) => a.name);
const preview = selectedNames.length > 0 ? truncateToWidth(selectedNames.join(" → "), width - 4) : "";
lines.push(row("", width, theme));
if (statusMessage) {
const color = statusMessage.type === "error" ? "error" : "success";
lines.push(row(` ${theme.fg(color, truncateToWidth(statusMessage.text, width - 4))}`, width, theme));
} else if (preview) {
lines.push(row(` ${theme.fg("dim", preview)}`, width, theme));
} else {
const cursorAgent = filtered[state.cursor];
const desc = cursorAgent ? truncateToWidth(cursorAgent.description, width - 4) : "";
const content = desc || scrollInfo;
lines.push(row(content ? ` ${theme.fg("dim", content)}` : "", width, theme));
}
lines.push(row("", width, theme));
const selCount = state.selected.length;
const footerText = selCount > 1
? ` [ctrl+r] chain [ctrl+p] parallel [tab] add [shift+tab] remove [esc] clear (${selCount}) `
: selCount === 1
? " [ctrl+r] run [ctrl+p] parallel [tab] add more [shift+tab] remove [esc] clear "
: " [enter] view [ctrl+r] run [tab] select [alt+n] new [esc] close ";
lines.push(renderFooter(footerText, width, theme));
return lines;
}