@mjasnikovs/pi-nop 0.0.0-stage → 0.1.2
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/LICENSE +21 -0
- package/README.md +116 -2
- package/package.json +87 -5
- package/src/config/box.ts +29 -0
- package/src/config/command.ts +138 -0
- package/src/config/config.ts +238 -0
- package/src/config/sections.ts +58 -0
- package/src/index.ts +17 -0
- package/src/plan/ask.ts +226 -0
- package/src/plan/goal.ts +43 -0
- package/src/plan/panel.ts +567 -0
- package/src/plan/plan.ts +581 -0
- package/src/plan/steps.ts +192 -0
- package/src/plan/track.ts +131 -0
- package/src/search/brave.ts +56 -0
- package/src/search/ddg.ts +67 -0
- package/src/search/engines.ts +63 -0
- package/src/search/exa.ts +74 -0
- package/src/search/search.ts +58 -0
- package/src/subagent/answer.ts +151 -0
- package/src/subagent/budget.ts +19 -0
- package/src/subagent/describe.ts +8 -0
- package/src/subagent/docs/docs.ts +123 -0
- package/src/subagent/docs/files.ts +90 -0
- package/src/subagent/docs/header.ts +60 -0
- package/src/subagent/docs/locate.ts +262 -0
- package/src/subagent/docs/manifest.ts +43 -0
- package/src/subagent/docs/npm.ts +287 -0
- package/src/subagent/fetch/digest.ts +379 -0
- package/src/subagent/fetch/errors.ts +10 -0
- package/src/subagent/fetch/fetch.ts +176 -0
- package/src/subagent/fetch/pdf.ts +120 -0
- package/src/subagent/guards/churn.ts +37 -0
- package/src/subagent/guards/circle.ts +49 -0
- package/src/subagent/guards/ground.ts +75 -0
- package/src/subagent/guards/guard.ts +24 -0
- package/src/subagent/guards/guards.ts +107 -0
- package/src/subagent/guards/key.ts +13 -0
- package/src/subagent/guards/leak.ts +42 -0
- package/src/subagent/guards/lines.ts +24 -0
- package/src/subagent/guards/loop.ts +77 -0
- package/src/subagent/guards/repeat.ts +70 -0
- package/src/subagent/guards/stale.ts +38 -0
- package/src/subagent/guards/toolCall.ts +46 -0
- package/src/subagent/guards/visited.ts +89 -0
- package/src/subagent/platform.ts +81 -0
- package/src/subagent/progress.ts +34 -0
- package/src/subagent/slots.ts +42 -0
- package/src/subagent/subagent.ts +537 -0
- package/src/subagent/subagentTui.ts +292 -0
- package/src/tui/hidden.ts +16 -0
- package/src/tui/spinner.ts +64 -0
- package/src/types/turndown-plugin-gfm.d.ts +5 -0
|
@@ -0,0 +1,567 @@
|
|
|
1
|
+
import type {Theme} from '@earendil-works/pi-coding-agent'
|
|
2
|
+
import {getSelectListTheme} from '@earendil-works/pi-coding-agent'
|
|
3
|
+
import type {SelectItem, SelectListTheme} from '@earendil-works/pi-tui'
|
|
4
|
+
import {
|
|
5
|
+
Input,
|
|
6
|
+
Key,
|
|
7
|
+
matchesKey,
|
|
8
|
+
SelectList,
|
|
9
|
+
stripTerminalSequences,
|
|
10
|
+
truncateToWidth,
|
|
11
|
+
wrapTextWithAnsi
|
|
12
|
+
} from '@earendil-works/pi-tui'
|
|
13
|
+
|
|
14
|
+
import type {Focusable} from '../config/box'
|
|
15
|
+
import {oneLine} from '../subagent/subagentTui'
|
|
16
|
+
import {formatElapsed, Spinner, spinnerFrames} from '../tui/spinner'
|
|
17
|
+
import type {Decision, Question} from './ask'
|
|
18
|
+
import type {Steps} from './steps'
|
|
19
|
+
import type {Progress, StepMark} from './track'
|
|
20
|
+
|
|
21
|
+
const steps = ['Goal', 'Questions', 'Steps', 'Hand over'] as const
|
|
22
|
+
const escapeKey = Key.escape
|
|
23
|
+
const enterKey = Key.enter
|
|
24
|
+
const upKey = Key.up
|
|
25
|
+
const downKey = Key.down
|
|
26
|
+
|
|
27
|
+
type PanelState = Steps & {
|
|
28
|
+
request?: string
|
|
29
|
+
goal?: string
|
|
30
|
+
accepted: boolean
|
|
31
|
+
asked: boolean
|
|
32
|
+
decisions: Decision[]
|
|
33
|
+
handed: boolean
|
|
34
|
+
progress?: Progress
|
|
35
|
+
question?: Question
|
|
36
|
+
running: boolean
|
|
37
|
+
status: string
|
|
38
|
+
error?: string
|
|
39
|
+
failedText?: string
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
type Action =
|
|
43
|
+
| 'change'
|
|
44
|
+
| 'accept'
|
|
45
|
+
| 'retry'
|
|
46
|
+
| 'answer'
|
|
47
|
+
| 'ask'
|
|
48
|
+
| 'plan'
|
|
49
|
+
| 'finish'
|
|
50
|
+
| 'changeGoal'
|
|
51
|
+
| 'handOver'
|
|
52
|
+
| 'edit'
|
|
53
|
+
| 'stop'
|
|
54
|
+
| 'close'
|
|
55
|
+
| 'leave'
|
|
56
|
+
type PanelActions = Record<Action, (text: string) => void>
|
|
57
|
+
type Row = SelectItem & {action: Action; text: string}
|
|
58
|
+
|
|
59
|
+
const row = (action: Action, label: string): Row => ({value: action, label, action, text: ''})
|
|
60
|
+
const rows = {
|
|
61
|
+
accept: row('accept', 'Accept the goal'),
|
|
62
|
+
retry: row('retry', 'Try again'),
|
|
63
|
+
ask: row('ask', 'Ask questions'),
|
|
64
|
+
plan: row('plan', 'Plan steps'),
|
|
65
|
+
finish: row('finish', 'Finish questions'),
|
|
66
|
+
changeGoal: row('changeGoal', 'Change goal'),
|
|
67
|
+
handOver: row('handOver', 'Send to main context'),
|
|
68
|
+
edit: row('edit', 'Edit plan'),
|
|
69
|
+
stop: row('stop', 'Stop tracking'),
|
|
70
|
+
close: row('close', 'Close')
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const shown = (text: string): string => stripTerminalSequences(text)
|
|
74
|
+
const numbered = (text: string, index: number): string => `${String(index + 1)}. ${text}`
|
|
75
|
+
|
|
76
|
+
const optionRows = (question: Question): Row[] =>
|
|
77
|
+
question.options.map((option, index) => ({
|
|
78
|
+
value: `option-${String(index)}`,
|
|
79
|
+
label: numbered(oneLine(shown(option)), index),
|
|
80
|
+
action: 'answer',
|
|
81
|
+
text: option
|
|
82
|
+
}))
|
|
83
|
+
|
|
84
|
+
const menuFor = (state: PanelState): Row[] => {
|
|
85
|
+
if (state.running) return []
|
|
86
|
+
|
|
87
|
+
const retry = {...rows.retry, text: state.failedText ?? ''}
|
|
88
|
+
const again = state.failedText === undefined ? [] : [retry]
|
|
89
|
+
|
|
90
|
+
// A request with no goal and no run is one whose run failed.
|
|
91
|
+
if (state.goal === undefined) return state.request === undefined ? [] : [retry, rows.close]
|
|
92
|
+
if (state.handed) return [rows.stop, rows.close]
|
|
93
|
+
if (state.question !== undefined)
|
|
94
|
+
return [...optionRows(state.question), rows.finish, rows.changeGoal, rows.close]
|
|
95
|
+
if (!state.accepted) return [rows.accept, ...again, rows.handOver, rows.close]
|
|
96
|
+
if (!state.asked) return [rows.ask, rows.handOver, rows.changeGoal, rows.close]
|
|
97
|
+
if (state.steps.length === 0) return [rows.plan, rows.handOver, rows.changeGoal, rows.close]
|
|
98
|
+
return [rows.handOver, ...again, rows.edit, rows.changeGoal, rows.close]
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const sameRows = (left: Row[], right: Row[]): boolean =>
|
|
102
|
+
left.length === right.length && left.every((item, index) => item.value === right[index]?.value)
|
|
103
|
+
|
|
104
|
+
// A list that scrolls shows at least one row and pi-tui's position line under it.
|
|
105
|
+
const scrolling = 2
|
|
106
|
+
|
|
107
|
+
const sharedInOrder = (room: number, lists: string[][]): string[][] => {
|
|
108
|
+
let left = room
|
|
109
|
+
|
|
110
|
+
return lists.map(list => {
|
|
111
|
+
const kept = list.slice(0, left)
|
|
112
|
+
left -= kept.length
|
|
113
|
+
return kept
|
|
114
|
+
})
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const takesText = (state: PanelState): boolean =>
|
|
118
|
+
!state.running
|
|
119
|
+
&& !state.handed
|
|
120
|
+
&& (!state.accepted || state.question !== undefined || state.steps.length > 0)
|
|
121
|
+
|
|
122
|
+
const currentStep = (state: PanelState): number => {
|
|
123
|
+
if (state.handed) return steps.length
|
|
124
|
+
if (!state.accepted) return steps.indexOf('Goal')
|
|
125
|
+
if (!state.asked) return steps.indexOf('Questions')
|
|
126
|
+
if (state.running || state.steps.length === 0) return steps.indexOf('Steps')
|
|
127
|
+
return steps.indexOf('Hand over')
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const stepStrip = (theme: Theme, current: number): string =>
|
|
131
|
+
steps
|
|
132
|
+
.map((step, index) => {
|
|
133
|
+
if (index < current) return theme.fg('success', `✓ ${step}`)
|
|
134
|
+
if (index === current) return theme.fg('accent', theme.bold(`● ${step}`))
|
|
135
|
+
return theme.fg('dim', `○ ${step}`)
|
|
136
|
+
})
|
|
137
|
+
.join(theme.fg('dim', ' '))
|
|
138
|
+
|
|
139
|
+
// While the user types, Enter belongs to the input, so the selected row loses its highlight.
|
|
140
|
+
const listTheme = (active: boolean): SelectListTheme => {
|
|
141
|
+
const theme = getSelectListTheme()
|
|
142
|
+
|
|
143
|
+
if (active) return theme
|
|
144
|
+
return {...theme, selectedText: text => text}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// What the body gives up to fit the screen. Each step keeps the cuts of the one before it.
|
|
148
|
+
type Compact = {
|
|
149
|
+
answers: boolean
|
|
150
|
+
goal: boolean
|
|
151
|
+
options: boolean
|
|
152
|
+
steps: boolean
|
|
153
|
+
files: boolean
|
|
154
|
+
stepCount: boolean
|
|
155
|
+
}
|
|
156
|
+
const roomy: Compact = {
|
|
157
|
+
answers: false,
|
|
158
|
+
goal: false,
|
|
159
|
+
options: false,
|
|
160
|
+
steps: false,
|
|
161
|
+
files: false,
|
|
162
|
+
stepCount: false
|
|
163
|
+
}
|
|
164
|
+
const tightest: Compact = {
|
|
165
|
+
answers: true,
|
|
166
|
+
goal: true,
|
|
167
|
+
options: true,
|
|
168
|
+
steps: true,
|
|
169
|
+
files: true,
|
|
170
|
+
stepCount: true
|
|
171
|
+
}
|
|
172
|
+
const compactSteps: Compact[] = [
|
|
173
|
+
roomy,
|
|
174
|
+
{...roomy, answers: true},
|
|
175
|
+
{...roomy, answers: true, goal: true},
|
|
176
|
+
{...roomy, answers: true, goal: true, options: true},
|
|
177
|
+
{...tightest, files: false, stepCount: false},
|
|
178
|
+
{...tightest, stepCount: false},
|
|
179
|
+
tightest
|
|
180
|
+
]
|
|
181
|
+
|
|
182
|
+
const body = (theme: Theme, state: PanelState, width: number, compact: Compact): string[] => {
|
|
183
|
+
if (state.goal === undefined) {
|
|
184
|
+
if (state.request === undefined) return [theme.fg('dim', 'Type what you want done.')]
|
|
185
|
+
return [
|
|
186
|
+
theme.fg('accent', theme.bold('REQUEST')),
|
|
187
|
+
...wrapTextWithAnsi(state.request, width)
|
|
188
|
+
]
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const mark = state.accepted ? theme.fg('success', '✓ ') : ''
|
|
192
|
+
const title = mark + theme.fg('accent', theme.bold('GOAL'))
|
|
193
|
+
const goal =
|
|
194
|
+
compact.goal ?
|
|
195
|
+
[truncateToWidth(`${title} ${oneLine(state.goal)}`, width, '…')]
|
|
196
|
+
: [title, ...wrapTextWithAnsi(state.goal, width)]
|
|
197
|
+
|
|
198
|
+
return [
|
|
199
|
+
...goal,
|
|
200
|
+
...answered(theme, state.decisions, width, compact.answers),
|
|
201
|
+
...planned(theme, state, width, compact),
|
|
202
|
+
...asking(theme, state.question, width, compact.options)
|
|
203
|
+
]
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
type Planned = Steps & {progress?: Progress}
|
|
207
|
+
|
|
208
|
+
const stepMarks = (theme: Theme): Record<StepMark, string> => ({
|
|
209
|
+
done: theme.fg('success', '✓ '),
|
|
210
|
+
open: theme.fg('dim', '○ '),
|
|
211
|
+
untracked: theme.fg('dim', '· ')
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
const fileMark = (theme: Theme, written: boolean | undefined): string => {
|
|
215
|
+
if (written === undefined) return ''
|
|
216
|
+
return stepMarks(theme)[written ? 'done' : 'open']
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const outsideCount = (outside: string[]): string[] =>
|
|
220
|
+
outside.length === 0 ? [] : [`Outside: ${String(outside.length)}`]
|
|
221
|
+
|
|
222
|
+
const fileCount = ({files, progress}: Planned): string[] => {
|
|
223
|
+
const outside = outsideCount(progress?.outside ?? [])
|
|
224
|
+
|
|
225
|
+
if (files.length === 0) return outside
|
|
226
|
+
if (progress === undefined) return [`Files: ${String(files.length)}`]
|
|
227
|
+
return [`Files: ${String(progress.written)}/${String(progress.total)}`, ...outside]
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const titled = (theme: Theme, title: string, lines: string[]): string[] =>
|
|
231
|
+
lines.length === 0 ? [] : ['', theme.fg('accent', theme.bold(title)), ...lines]
|
|
232
|
+
|
|
233
|
+
const outsideRows = (theme: Theme, outside: string[], width: number): string[] =>
|
|
234
|
+
titled(
|
|
235
|
+
theme,
|
|
236
|
+
'OUTSIDE',
|
|
237
|
+
outside.flatMap(path => wrapTextWithAnsi(shown(path), width))
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
const fileList = (theme: Theme, plan: Planned, width: number): string[] =>
|
|
241
|
+
titled(
|
|
242
|
+
theme,
|
|
243
|
+
'FILES',
|
|
244
|
+
plan.files.flatMap(({path, state}, index) =>
|
|
245
|
+
wrapTextWithAnsi(
|
|
246
|
+
`${fileMark(theme, plan.progress?.files[index])}${shown(path)} ${theme.fg('dim', `(${state})`)}`,
|
|
247
|
+
width
|
|
248
|
+
)
|
|
249
|
+
)
|
|
250
|
+
)
|
|
251
|
+
|
|
252
|
+
const fileRows = (theme: Theme, plan: Planned, width: number, counted: boolean): string[] => {
|
|
253
|
+
const counts = fileCount(plan)
|
|
254
|
+
|
|
255
|
+
if (counted) return counts.length === 0 ? [] : ['', ...counts]
|
|
256
|
+
return [
|
|
257
|
+
...fileList(theme, plan, width),
|
|
258
|
+
...outsideRows(theme, plan.progress?.outside ?? [], width)
|
|
259
|
+
]
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const stepRows = (theme: Theme, plan: Planned, width: number, short: boolean): string[] =>
|
|
263
|
+
plan.steps.flatMap((step, index) => {
|
|
264
|
+
const mark = plan.progress?.steps[index]
|
|
265
|
+
const text =
|
|
266
|
+
(mark === undefined ? '' : stepMarks(theme)[mark]) + numbered(shown(step), index)
|
|
267
|
+
|
|
268
|
+
return short ? [truncateToWidth(oneLine(text), width, '…')] : wrapTextWithAnsi(text, width)
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
const stepCount = ({steps: planSteps, progress}: Planned): string => {
|
|
272
|
+
const count = `Steps: ${String(planSteps.length)}`
|
|
273
|
+
|
|
274
|
+
if (progress === undefined) return count
|
|
275
|
+
return `${count} · done ${String(progress.steps.filter(mark => mark === 'done').length)}`
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const verifyRows = (theme: Theme, verify: string[], width: number): string[] =>
|
|
279
|
+
titled(
|
|
280
|
+
theme,
|
|
281
|
+
'VERIFY',
|
|
282
|
+
verify.flatMap(command => wrapTextWithAnsi(`- ${shown(command)}`, width))
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
const planned = (theme: Theme, plan: Planned, width: number, compact: Compact): string[] => {
|
|
286
|
+
if (plan.steps.length === 0) return []
|
|
287
|
+
|
|
288
|
+
// No stage cuts the commands: the main agent runs them, so the user sees them whole first.
|
|
289
|
+
const verify = verifyRows(theme, plan.verify, width)
|
|
290
|
+
|
|
291
|
+
if (compact.stepCount) return [stepCount(plan), ...fileCount(plan), ...verify]
|
|
292
|
+
return [
|
|
293
|
+
'',
|
|
294
|
+
theme.fg('accent', theme.bold('STEPS')),
|
|
295
|
+
...stepRows(theme, plan, width, compact.steps),
|
|
296
|
+
...fileRows(theme, plan, width, compact.files),
|
|
297
|
+
...verify
|
|
298
|
+
]
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const answered = (
|
|
302
|
+
theme: Theme,
|
|
303
|
+
decisions: Decision[],
|
|
304
|
+
width: number,
|
|
305
|
+
counted: boolean
|
|
306
|
+
): string[] => {
|
|
307
|
+
if (counted && decisions.length > 0)
|
|
308
|
+
return [theme.fg('success', '✓ ') + `Answered questions: ${String(decisions.length)}`]
|
|
309
|
+
return decisions.flatMap(({question, answer}) => [
|
|
310
|
+
'',
|
|
311
|
+
...wrapTextWithAnsi(theme.fg('muted', shown(question)), width),
|
|
312
|
+
...wrapTextWithAnsi(theme.fg('success', '✓ ') + shown(answer), width)
|
|
313
|
+
])
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const optionTexts = (question: Question, width: number): string[][] =>
|
|
317
|
+
question.options.map((option, index) => wrapTextWithAnsi(numbered(shown(option), index), width))
|
|
318
|
+
|
|
319
|
+
type Asked = {heading: string[]; text: string[]; source: string[]}
|
|
320
|
+
const nothingAsked: Asked = {heading: [], text: [], source: []}
|
|
321
|
+
|
|
322
|
+
const asked = (theme: Theme, question: Question | undefined, width: number): Asked => {
|
|
323
|
+
if (question === undefined) return nothingAsked
|
|
324
|
+
|
|
325
|
+
const {evidence} = question
|
|
326
|
+
const quote =
|
|
327
|
+
evidence === undefined ?
|
|
328
|
+
[]
|
|
329
|
+
: [theme.fg('dim', `${shown(evidence.file)}: ${shown(evidence.line).trim()}`)]
|
|
330
|
+
|
|
331
|
+
return {
|
|
332
|
+
heading: [theme.fg('accent', theme.bold('QUESTION'))],
|
|
333
|
+
text: wrapTextWithAnsi(shown(question.question), width),
|
|
334
|
+
source: quote.flatMap(line => wrapTextWithAnsi(line, width))
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const asking = (
|
|
339
|
+
theme: Theme,
|
|
340
|
+
question: Question | undefined,
|
|
341
|
+
width: number,
|
|
342
|
+
bare: boolean
|
|
343
|
+
): string[] => {
|
|
344
|
+
if (question === undefined) return []
|
|
345
|
+
|
|
346
|
+
const {heading, text, source} = asked(theme, question, width)
|
|
347
|
+
return [
|
|
348
|
+
'',
|
|
349
|
+
...heading,
|
|
350
|
+
...text,
|
|
351
|
+
...source,
|
|
352
|
+
...(bare ? [] : optionTexts(question, width).flat())
|
|
353
|
+
]
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
class PlanPanel implements Focusable {
|
|
357
|
+
private readonly input = new Input({prompt: '> '})
|
|
358
|
+
private readonly spinner: Spinner
|
|
359
|
+
private state: PanelState = {
|
|
360
|
+
accepted: false,
|
|
361
|
+
asked: false,
|
|
362
|
+
handed: false,
|
|
363
|
+
decisions: [],
|
|
364
|
+
steps: [],
|
|
365
|
+
files: [],
|
|
366
|
+
verify: [],
|
|
367
|
+
running: false,
|
|
368
|
+
status: ''
|
|
369
|
+
}
|
|
370
|
+
private selected = 0
|
|
371
|
+
|
|
372
|
+
constructor(
|
|
373
|
+
private readonly theme: Theme,
|
|
374
|
+
requestRepaint: () => void,
|
|
375
|
+
private readonly actions: PanelActions,
|
|
376
|
+
// pi cuts an overlay at the bottom and never says how tall it may be.
|
|
377
|
+
private readonly maxLines: () => number
|
|
378
|
+
) {
|
|
379
|
+
this.spinner = new Spinner(requestRepaint)
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
setState(state: PanelState): void {
|
|
383
|
+
if (state.running && !this.state.running) this.spinner.startedAt = Date.now()
|
|
384
|
+
|
|
385
|
+
if (!state.running) {
|
|
386
|
+
this.spinner.startedAt = undefined
|
|
387
|
+
this.spinner.stop()
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// Text typed for a question that left the screen must not turn into a goal change.
|
|
391
|
+
if (!takesText(state) || state.question !== this.state.question) this.input.setValue('')
|
|
392
|
+
// A repaint with the same rows must not move the highlight: Enter would pick another row.
|
|
393
|
+
if (
|
|
394
|
+
!sameRows(menuFor(state), menuFor(this.state))
|
|
395
|
+
|| state.question !== this.state.question
|
|
396
|
+
)
|
|
397
|
+
this.selected = 0
|
|
398
|
+
|
|
399
|
+
this.state = state
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
render(width: number): string[] {
|
|
403
|
+
const limit = this.maxLines()
|
|
404
|
+
const hints = ['', ...wrapTextWithAnsi(this.hints(), width)]
|
|
405
|
+
const controls = this.controls(width, limit - hints.length)
|
|
406
|
+
const picked = this.picked(width)
|
|
407
|
+
|
|
408
|
+
for (const compact of compactSteps) {
|
|
409
|
+
const pane = compact.options ? picked.padded : []
|
|
410
|
+
const lines = [...this.head(width, compact), ...controls, ...pane, ...hints]
|
|
411
|
+
if (lines.length <= limit) return lines
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
return [...this.squeezed(width, limit - hints.length, picked.padded), ...hints]
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// Nothing fits, so the menu may scroll to leave the question text its room.
|
|
418
|
+
private squeezed(width: number, room: number, picked: string[]): string[] {
|
|
419
|
+
const {theme, state} = this
|
|
420
|
+
const {heading, text, source} = asked(theme, state.question, width)
|
|
421
|
+
const controls = this.controls(width, room - text.length)
|
|
422
|
+
const kept = sharedInOrder(Math.max(0, room - controls.length), [
|
|
423
|
+
text,
|
|
424
|
+
source,
|
|
425
|
+
heading,
|
|
426
|
+
picked,
|
|
427
|
+
['']
|
|
428
|
+
])
|
|
429
|
+
const [words, quote, title, option, gap] = kept
|
|
430
|
+
const divider = title.length === 0 ? [] : ['']
|
|
431
|
+
const spare = room - controls.length - kept.flat().length - divider.length
|
|
432
|
+
const context = this.head(width, tightest, {...state, question: undefined}).slice(0, -1)
|
|
433
|
+
const above = spare > 0 ? [...context.slice(-spare), ...divider] : []
|
|
434
|
+
|
|
435
|
+
return [...above, ...title, ...words, ...quote, ...gap, ...controls, ...option]
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
private head(width: number, compact: Compact, state = this.state): string[] {
|
|
439
|
+
return [
|
|
440
|
+
stepStrip(this.theme, currentStep(state)),
|
|
441
|
+
'',
|
|
442
|
+
...body(this.theme, state, width, compact),
|
|
443
|
+
''
|
|
444
|
+
]
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
private controls(width: number, room: number): string[] {
|
|
448
|
+
const input = takesText(this.state) ? this.input.render(width) : []
|
|
449
|
+
const gap = input.length === 0 ? [] : ['']
|
|
450
|
+
const rowCount = menuFor(this.state).length
|
|
451
|
+
const status = this.status(width, room - input.length - gap.length - rowCount - 1)
|
|
452
|
+
const above = [...status, ...(status.length === 0 ? [] : ['']), ...input]
|
|
453
|
+
const menu = this.menu(width, room - above.length - gap.length)
|
|
454
|
+
|
|
455
|
+
return [...above, ...(menu.length === 0 ? [] : [...gap, ...menu])]
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
// padded is as tall as the tallest option, so the rows above do not jump between options.
|
|
459
|
+
private picked(width: number): {text: string[]; padded: string[]} {
|
|
460
|
+
const {question} = this.state
|
|
461
|
+
const texts = question === undefined ? [] : optionTexts(question, width)
|
|
462
|
+
const text = texts[this.selected] ?? []
|
|
463
|
+
const tallest = Math.max(0, ...texts.map(lines => lines.length))
|
|
464
|
+
|
|
465
|
+
return {
|
|
466
|
+
text,
|
|
467
|
+
padded: [...text, ...Array.from({length: tallest - text.length}, () => '')]
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
invalidate(): void {
|
|
472
|
+
this.input.invalidate()
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
handleInput(data: string): void {
|
|
476
|
+
const menu = menuFor(this.state)
|
|
477
|
+
const typing = this.input.getValue() !== ''
|
|
478
|
+
|
|
479
|
+
if (matchesKey(data, escapeKey)) {
|
|
480
|
+
if (typing) this.input.setValue('')
|
|
481
|
+
else this.actions.leave('')
|
|
482
|
+
} else if (this.state.running) return
|
|
483
|
+
else if (matchesKey(data, enterKey)) {
|
|
484
|
+
if (typing) this.submit()
|
|
485
|
+
else this.pick(menu)
|
|
486
|
+
} else if (!typing && menu.length > 0 && matchesKey(data, upKey))
|
|
487
|
+
this.selected = (this.selected + menu.length - 1) % menu.length
|
|
488
|
+
else if (!typing && menu.length > 0 && matchesKey(data, downKey))
|
|
489
|
+
this.selected = (this.selected + 1) % menu.length
|
|
490
|
+
else if (takesText(this.state)) this.input.handleInput(data)
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
private submit(): void {
|
|
494
|
+
const text = this.input.getValue().trim()
|
|
495
|
+
this.input.setValue('')
|
|
496
|
+
if (text === '') return
|
|
497
|
+
if (this.state.question === undefined) this.actions.change(text)
|
|
498
|
+
else this.actions.answer(text)
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
private pick(menu: Row[]): void {
|
|
502
|
+
const picked = menu[this.selected]
|
|
503
|
+
|
|
504
|
+
if (picked !== undefined) this.actions[picked.action](picked.text)
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
private menu(width: number, room: number): string[] {
|
|
508
|
+
const items = menuFor(this.state)
|
|
509
|
+
|
|
510
|
+
if (items.length === 0) return []
|
|
511
|
+
|
|
512
|
+
const visible = items.length <= room ? items.length : Math.max(1, room - scrolling + 1)
|
|
513
|
+
const list = new SelectList(items, visible, listTheme(this.input.getValue() === ''))
|
|
514
|
+
list.setSelectedIndex(this.selected)
|
|
515
|
+
return list.render(width)
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
private hints(): string {
|
|
519
|
+
const {theme, state} = this
|
|
520
|
+
const menu = menuFor(state)
|
|
521
|
+
|
|
522
|
+
return theme.fg('muted', this.hintKeys(menu).join(' · '))
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
private hintKeys(menu: Row[]): string[] {
|
|
526
|
+
if (this.state.running) return ['esc close']
|
|
527
|
+
if (this.input.getValue() !== '') return ['enter sends', 'esc clears']
|
|
528
|
+
if (menu.length === 0) return ['type your request', 'enter sends', 'esc close']
|
|
529
|
+
|
|
530
|
+
const enter = this.enterHint(menu[this.selected])
|
|
531
|
+
|
|
532
|
+
if (!takesText(this.state)) return ['↑↓ pick', enter, 'esc close']
|
|
533
|
+
return [this.typeHint(), '↑↓ pick', enter, 'esc close']
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
private enterHint(picked: Row | undefined): string {
|
|
537
|
+
if (picked?.action === 'answer') return `enter answer ${String(this.selected + 1)}`
|
|
538
|
+
return `enter ${(picked?.label ?? '').toLowerCase()}`
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
private typeHint(): string {
|
|
542
|
+
if (this.state.question !== undefined) return 'type your answer'
|
|
543
|
+
return this.state.goal === undefined ? 'type a new request' : 'type to change'
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
private status(width: number, room: number): string[] {
|
|
547
|
+
const {theme, state} = this
|
|
548
|
+
|
|
549
|
+
if (state.running) return [this.spinnerLine()]
|
|
550
|
+
if (state.error === undefined) return []
|
|
551
|
+
return wrapTextWithAnsi(theme.fg('error', state.error), width).slice(0, Math.max(1, room))
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
private spinnerLine(): string {
|
|
555
|
+
this.spinner.painted()
|
|
556
|
+
this.spinner.start()
|
|
557
|
+
return (
|
|
558
|
+
this.theme.fg('accent', spinnerFrames[this.spinner.frame] ?? '')
|
|
559
|
+
+ ' '
|
|
560
|
+
+ this.theme.fg('muted', `subagent · ${formatElapsed(this.spinner.elapsedMs())} · `)
|
|
561
|
+
+ this.theme.fg('toolOutput', this.state.status || 'starting…')
|
|
562
|
+
)
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
export {escapeKey, PlanPanel, rows, steps}
|
|
567
|
+
export type {PanelActions, PanelState}
|