@mjasnikovs/pi-nop 0.0.0-stage → 0.1.0
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 +114 -2
- package/assets/hero.png +0 -0
- package/assets/hero.svg +1 -0
- package/package.json +85 -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 +133 -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 +89 -0
- package/src/subagent/docs/header.ts +60 -0
- package/src/subagent/docs/locate.ts +261 -0
- package/src/subagent/docs/manifest.ts +43 -0
- package/src/subagent/docs/npm.ts +247 -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 +115 -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/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
package/src/plan/plan.ts
ADDED
|
@@ -0,0 +1,581 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CustomEntry,
|
|
3
|
+
ExtensionAPI,
|
|
4
|
+
ExtensionContext,
|
|
5
|
+
SessionEntry
|
|
6
|
+
} from '@earendil-works/pi-coding-agent'
|
|
7
|
+
import type {OverlayHandle} from '@earendil-works/pi-tui'
|
|
8
|
+
import Value from 'typebox/value'
|
|
9
|
+
|
|
10
|
+
import {boxed, frameRows} from '../config/box'
|
|
11
|
+
import {modelSpec} from '../config/config'
|
|
12
|
+
import {configuredOptions, subagentSession} from '../subagent/subagent'
|
|
13
|
+
import type {SubagentSession, SubagentUpdate} from '../subagent/subagent'
|
|
14
|
+
import {progressLabel} from '../subagent/progress'
|
|
15
|
+
import {oneLine} from '../subagent/subagentTui'
|
|
16
|
+
import {whileHidden} from '../tui/hidden'
|
|
17
|
+
import {askTool, askToolName, handOverMessage, planHead, questionsTask, sameQuestion} from './ask'
|
|
18
|
+
import type {AskHost, Decision, Question} from './ask'
|
|
19
|
+
import {goalPrompt, goalTask, parseGoal, sendBackGoal} from './goal'
|
|
20
|
+
import {PlanPanel} from './panel'
|
|
21
|
+
import type {PanelActions, PanelState} from './panel'
|
|
22
|
+
import {editText, misplaced, parseSteps, sendBackSteps, stepsTask, stepsText} from './steps'
|
|
23
|
+
import type {PlanFile, Steps} from './steps'
|
|
24
|
+
import {diskTools, progress, stamps, statusText, writes} from './track'
|
|
25
|
+
import type {Progress} from './track'
|
|
26
|
+
|
|
27
|
+
const entryType = 'plan-mode'
|
|
28
|
+
const statusKey = 'plan'
|
|
29
|
+
const title = 'PLAN'
|
|
30
|
+
const noGoalLine = 'The subagent gave no GOAL line. Send your text again or reword it.'
|
|
31
|
+
const noStepsLine = 'The subagent gave no steps that pass the checks.'
|
|
32
|
+
const tuiOnly = 'Plan mode needs the interactive UI.'
|
|
33
|
+
// No maxHeight: pi would cut the rows and the input off the bottom. The panel fits itself.
|
|
34
|
+
const overlayOptions = {width: '80%'} as const
|
|
35
|
+
const closeTitle = 'Close plan? A running subagent stops.'
|
|
36
|
+
const keepLabel = 'Keep planning'
|
|
37
|
+
const stopLabel = 'Close and stop'
|
|
38
|
+
const editTitle = 'Edit plan'
|
|
39
|
+
const layoutTitle =
|
|
40
|
+
'Edit plan: keep STEPS:, FILES:, VERIFY: with 1. <step>, - <path> (existing|new), - <command>'
|
|
41
|
+
const misplacedTitles: Record<PlanFile['state'], (path: string) => string> = {
|
|
42
|
+
existing: path => `Edit plan: ${path} is tagged existing but is not a file on disk`,
|
|
43
|
+
new: path => `Edit plan: ${path} is tagged new but is on disk`
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
type PlanState = Steps & {
|
|
47
|
+
request?: string
|
|
48
|
+
goal?: string
|
|
49
|
+
accepted: boolean
|
|
50
|
+
decisions: Decision[]
|
|
51
|
+
/** The questions step ran to its end or the user finished it. */
|
|
52
|
+
asked: boolean
|
|
53
|
+
/** The plan went to the main agent and its files are tracked. */
|
|
54
|
+
handed: boolean
|
|
55
|
+
/** What each FILES row was on disk at hand over. */
|
|
56
|
+
disk?: string[]
|
|
57
|
+
/** Typed steps changes since the last saved edit, which is final. The goal predates them. */
|
|
58
|
+
changes: string[]
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** What the session was opened with, so a settings change opens a fresh one. */
|
|
62
|
+
type Opened = {model?: string; thinkingLevel?: string}
|
|
63
|
+
|
|
64
|
+
type Run = {
|
|
65
|
+
request: string
|
|
66
|
+
typed?: string
|
|
67
|
+
status: string
|
|
68
|
+
turns: number
|
|
69
|
+
controller: AbortController
|
|
70
|
+
questions: boolean
|
|
71
|
+
askedThisTurn: boolean
|
|
72
|
+
waiting?: {question: Question; answer: (text: string) => void}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const checkEdit = async (
|
|
76
|
+
cwd: string,
|
|
77
|
+
saved: Steps,
|
|
78
|
+
text: string
|
|
79
|
+
): Promise<{plan: Steps} | {reason: string} | 'unchanged'> => {
|
|
80
|
+
const plan = parseSteps(text)
|
|
81
|
+
|
|
82
|
+
if (plan === undefined) return {reason: layoutTitle}
|
|
83
|
+
// Before the disk check: a file made since planning must not refuse a plan left as it was.
|
|
84
|
+
if (Value.Equal(plan, {steps: saved.steps, files: saved.files, verify: saved.verify}))
|
|
85
|
+
return 'unchanged'
|
|
86
|
+
|
|
87
|
+
const wrong = await misplaced(cwd, plan.files, undefined)
|
|
88
|
+
return wrong === undefined ? {plan} : {reason: misplacedTitles[wrong.state](wrong.path)}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const idle: PlanState = {
|
|
92
|
+
accepted: false,
|
|
93
|
+
decisions: [],
|
|
94
|
+
asked: false,
|
|
95
|
+
handed: false,
|
|
96
|
+
changes: [],
|
|
97
|
+
steps: [],
|
|
98
|
+
files: [],
|
|
99
|
+
verify: []
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// The goal prompt allows no text before the answer, so tool turns report only their count.
|
|
103
|
+
const runStatus = (text: string, turns: number): string => {
|
|
104
|
+
if (turns === 0) return oneLine(text)
|
|
105
|
+
return oneLine(text) || `turn ${String(turns)}`
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const isPlanEntry = (entry: SessionEntry): entry is CustomEntry<PlanState> =>
|
|
109
|
+
entry.type === 'custom' && entry.customType === entryType
|
|
110
|
+
|
|
111
|
+
// An entry saved before a later step was built lacks that step's fields.
|
|
112
|
+
const lastPlan = (ctx: ExtensionContext): {state: PlanState; after: SessionEntry[]} => {
|
|
113
|
+
const branch = ctx.sessionManager.getBranch()
|
|
114
|
+
const at = branch.findLastIndex(entry => isPlanEntry(entry) && entry.data !== undefined)
|
|
115
|
+
const saved = branch[at]
|
|
116
|
+
|
|
117
|
+
if (saved === undefined || !isPlanEntry(saved)) return {state: idle, after: []}
|
|
118
|
+
return {state: {...idle, ...saved.data}, after: branch.slice(at + 1)}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export default function (pi: ExtensionAPI) {
|
|
122
|
+
let state = idle
|
|
123
|
+
let running: Run | undefined
|
|
124
|
+
let error: string | undefined
|
|
125
|
+
let subagent: {session: SubagentSession; knowsGoal: boolean; opened: Opened} | undefined
|
|
126
|
+
let repaint: (() => void) | undefined
|
|
127
|
+
let tracked: Progress | undefined
|
|
128
|
+
let failedText: string | undefined
|
|
129
|
+
let trackRuns = 0
|
|
130
|
+
|
|
131
|
+
// The only place panel state comes from, so a new field cannot miss a copy.
|
|
132
|
+
const panelState = (): PanelState => ({
|
|
133
|
+
request: state.request ?? running?.request ?? failedText,
|
|
134
|
+
goal: state.goal,
|
|
135
|
+
accepted: state.accepted,
|
|
136
|
+
asked: state.asked,
|
|
137
|
+
decisions: state.decisions,
|
|
138
|
+
steps: state.steps,
|
|
139
|
+
files: state.files,
|
|
140
|
+
verify: state.verify,
|
|
141
|
+
handed: state.handed,
|
|
142
|
+
progress: tracked,
|
|
143
|
+
question: running?.waiting?.question,
|
|
144
|
+
running: running !== undefined && running.waiting === undefined,
|
|
145
|
+
status: running?.status ?? '',
|
|
146
|
+
error,
|
|
147
|
+
failedText
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
const save = (next: PlanState): void => {
|
|
151
|
+
failedText = undefined
|
|
152
|
+
state = next
|
|
153
|
+
pi.appendEntry(entryType, state)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const track = async (ctx: ExtensionContext): Promise<void> => {
|
|
157
|
+
trackRuns += 1
|
|
158
|
+
const run = trackRuns
|
|
159
|
+
const next =
|
|
160
|
+
state.handed ?
|
|
161
|
+
await progress(ctx.cwd, state, writes(lastPlan(ctx).after), state.disk)
|
|
162
|
+
: undefined
|
|
163
|
+
|
|
164
|
+
if (run !== trackRuns) return
|
|
165
|
+
tracked = next
|
|
166
|
+
|
|
167
|
+
// A reload or a new session while the paths resolved leaves a ctx that throws.
|
|
168
|
+
try {
|
|
169
|
+
ctx.ui.setStatus(statusKey, next === undefined ? undefined : statusText(next))
|
|
170
|
+
} catch {
|
|
171
|
+
return
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
repaint?.()
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const handOver = async (ctx: ExtensionContext): Promise<void> => {
|
|
178
|
+
const plan = state
|
|
179
|
+
const {goal, decisions} = plan
|
|
180
|
+
const message = handOverMessage(goal ?? '', decisions, stepsText(plan), plan.changes)
|
|
181
|
+
const disk = await stamps(ctx.cwd, plan)
|
|
182
|
+
|
|
183
|
+
// A new request or a tree move while the stamps were read replaced this plan.
|
|
184
|
+
if (state !== plan) return
|
|
185
|
+
|
|
186
|
+
// A reload while the stamps were read leaves a pi that throws.
|
|
187
|
+
try {
|
|
188
|
+
save(plan.steps.length > 0 ? {...plan, handed: true, disk} : idle)
|
|
189
|
+
// Without deliverAs pi refuses a message while the main agent is busy.
|
|
190
|
+
if (goal !== undefined) pi.sendUserMessage(message, {deliverAs: 'followUp'})
|
|
191
|
+
} catch {
|
|
192
|
+
return
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
void track(ctx)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const saveEdit = (opened: PlanState, plan: Steps): void => {
|
|
199
|
+
// A restore while the edit was open replaced this plan.
|
|
200
|
+
if (state !== opened) return
|
|
201
|
+
|
|
202
|
+
closeSubagent()
|
|
203
|
+
|
|
204
|
+
// A reload while the files were checked leaves a pi that throws.
|
|
205
|
+
try {
|
|
206
|
+
save({...state, ...plan, changes: []})
|
|
207
|
+
} catch {
|
|
208
|
+
return
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
repaint?.()
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const stopRun = (): void => {
|
|
215
|
+
running?.controller.abort()
|
|
216
|
+
running = undefined
|
|
217
|
+
error = undefined
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const closeSubagent = (): void => {
|
|
221
|
+
stopRun()
|
|
222
|
+
subagent?.session.dispose()
|
|
223
|
+
subagent = undefined
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const taskFor = (text: string): string => {
|
|
227
|
+
if (state.request === undefined) return goalTask({request: text})
|
|
228
|
+
if (subagent?.knowsGoal === true) return goalTask({change: text})
|
|
229
|
+
return goalTask({request: state.request, goal: state.goal, change: text})
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const askHost = (cwd: string): AskHost => ({
|
|
233
|
+
cwd,
|
|
234
|
+
open: () => running?.questions === true,
|
|
235
|
+
claim: () => {
|
|
236
|
+
if (running?.askedThisTurn !== false) return false
|
|
237
|
+
running.askedThisTurn = true
|
|
238
|
+
return true
|
|
239
|
+
},
|
|
240
|
+
stored: question =>
|
|
241
|
+
state.decisions.find(decision => sameQuestion(decision.question, question))?.answer,
|
|
242
|
+
show: (question, reply) => {
|
|
243
|
+
const waiting = {question, answer: reply}
|
|
244
|
+
if (running !== undefined) running.waiting = waiting
|
|
245
|
+
repaint?.()
|
|
246
|
+
return () => {
|
|
247
|
+
if (running?.waiting !== waiting) return
|
|
248
|
+
running.waiting = undefined
|
|
249
|
+
repaint?.()
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
// The tool is in the session from its first turn: a tool change empties the cache, and the
|
|
255
|
+
// steps turn pays that once.
|
|
256
|
+
const openSubagent = (
|
|
257
|
+
ctx: ExtensionContext
|
|
258
|
+
): {session: SubagentSession; knowsGoal: boolean} => {
|
|
259
|
+
const options = configuredOptions(ctx, 'subagent')
|
|
260
|
+
const opened = {
|
|
261
|
+
model: options.model === undefined ? undefined : modelSpec(options.model),
|
|
262
|
+
thinkingLevel: options.thinkingLevel
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (subagent !== undefined && !Value.Equal(subagent.opened, opened)) closeSubagent()
|
|
266
|
+
|
|
267
|
+
subagent ??= {
|
|
268
|
+
session: subagentSession({
|
|
269
|
+
...options,
|
|
270
|
+
prompt: goalPrompt,
|
|
271
|
+
customTools: [askTool(askHost(ctx.cwd))],
|
|
272
|
+
waitingTools: [askToolName]
|
|
273
|
+
}),
|
|
274
|
+
knowsGoal: false,
|
|
275
|
+
opened
|
|
276
|
+
}
|
|
277
|
+
return subagent
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const start = (request: string, questions: boolean, typed?: string): Run => {
|
|
281
|
+
stopRun()
|
|
282
|
+
const run: Run = {
|
|
283
|
+
request,
|
|
284
|
+
typed,
|
|
285
|
+
status: '',
|
|
286
|
+
turns: 0,
|
|
287
|
+
controller: new AbortController(),
|
|
288
|
+
questions,
|
|
289
|
+
askedThisTurn: false
|
|
290
|
+
}
|
|
291
|
+
running = run
|
|
292
|
+
repaint?.()
|
|
293
|
+
return run
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const onProgress =
|
|
297
|
+
(run: Run): SubagentUpdate =>
|
|
298
|
+
event => {
|
|
299
|
+
if (event.type === 'turn') {
|
|
300
|
+
run.turns += 1
|
|
301
|
+
run.askedThisTurn = false
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
run.status = runStatus(progressLabel(event), run.turns)
|
|
305
|
+
repaint?.()
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const failed = (run: Run, failure: unknown): void => {
|
|
309
|
+
if (run.controller.signal.aborted) return
|
|
310
|
+
closeSubagent()
|
|
311
|
+
error = failure instanceof Error ? failure.message : String(failure)
|
|
312
|
+
failedText = run.typed
|
|
313
|
+
repaint?.()
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const ask = async (ctx: ExtensionContext): Promise<void> => {
|
|
317
|
+
const current = openSubagent(ctx)
|
|
318
|
+
const unseen = current.knowsGoal ? {} : {request: state.request, goal: state.goal}
|
|
319
|
+
const run = start(state.request ?? '', true)
|
|
320
|
+
|
|
321
|
+
try {
|
|
322
|
+
await current.session.prompt(
|
|
323
|
+
() => questionsTask(unseen, state.decisions),
|
|
324
|
+
run.controller.signal,
|
|
325
|
+
onProgress(run)
|
|
326
|
+
)
|
|
327
|
+
if (run.controller.signal.aborted) return
|
|
328
|
+
current.knowsGoal = true
|
|
329
|
+
save({...state, asked: true})
|
|
330
|
+
void planSteps(ctx)
|
|
331
|
+
} catch (failure) {
|
|
332
|
+
failed(run, failure)
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
const planSteps = async (ctx: ExtensionContext, change?: string): Promise<void> => {
|
|
337
|
+
const current = openSubagent(ctx)
|
|
338
|
+
const unseen = {request: state.request, goal: state.goal}
|
|
339
|
+
const head = current.knowsGoal ? [] : planHead(unseen, state.decisions)
|
|
340
|
+
const run = start(state.request ?? '', false, change)
|
|
341
|
+
const check = sendBackSteps(ctx.cwd, run.controller.signal)
|
|
342
|
+
|
|
343
|
+
try {
|
|
344
|
+
// Without the ask tool: refused calls after the questions cost restarts and the model
|
|
345
|
+
// answered a refusal with a GOAL line (PLANNING_MODE.md, sections 15 and 16).
|
|
346
|
+
const {text: reply} = await current.session.prompt(
|
|
347
|
+
stepsTask(head, state, change),
|
|
348
|
+
run.controller.signal,
|
|
349
|
+
onProgress(run),
|
|
350
|
+
check,
|
|
351
|
+
[askToolName]
|
|
352
|
+
)
|
|
353
|
+
// The send back is skipped once the restarts are spent, so the last reply is unchecked.
|
|
354
|
+
const plan = parseSteps(reply)
|
|
355
|
+
const fault = await check(reply)
|
|
356
|
+
|
|
357
|
+
if (run.controller.signal.aborted) return
|
|
358
|
+
if (plan === undefined || fault !== undefined) throw new Error(noStepsLine)
|
|
359
|
+
|
|
360
|
+
current.knowsGoal = true
|
|
361
|
+
running = undefined
|
|
362
|
+
save({
|
|
363
|
+
...state,
|
|
364
|
+
...plan,
|
|
365
|
+
changes: change === undefined ? state.changes : [...state.changes, change]
|
|
366
|
+
})
|
|
367
|
+
repaint?.()
|
|
368
|
+
} catch (failure) {
|
|
369
|
+
failed(run, failure)
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const answerQuestion = (text: string): void => {
|
|
374
|
+
const waiting = running?.waiting
|
|
375
|
+
|
|
376
|
+
if (running === undefined || waiting === undefined) return
|
|
377
|
+
running.waiting = undefined
|
|
378
|
+
// Saved before the tool returns, so no way of ending the run can lose it.
|
|
379
|
+
save({
|
|
380
|
+
...state,
|
|
381
|
+
decisions: [...state.decisions, {question: waiting.question.question, answer: text}]
|
|
382
|
+
})
|
|
383
|
+
waiting.answer(text)
|
|
384
|
+
repaint?.()
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const refine = async (ctx: ExtensionContext, text: string): Promise<void> => {
|
|
388
|
+
const request = state.request ?? text
|
|
389
|
+
// Opened first: a fresh session must be told the request and the goal.
|
|
390
|
+
const current = openSubagent(ctx)
|
|
391
|
+
const task = taskFor(text)
|
|
392
|
+
const run = start(request, false, text)
|
|
393
|
+
|
|
394
|
+
try {
|
|
395
|
+
const {text: reply} = await current.session.prompt(
|
|
396
|
+
task,
|
|
397
|
+
run.controller.signal,
|
|
398
|
+
onProgress(run),
|
|
399
|
+
sendBackGoal
|
|
400
|
+
)
|
|
401
|
+
const goal = parseGoal(reply)
|
|
402
|
+
|
|
403
|
+
if (goal === undefined) {
|
|
404
|
+
// The session's last reply is not the shown goal, so the next change resends it.
|
|
405
|
+
current.knowsGoal = false
|
|
406
|
+
error = noGoalLine
|
|
407
|
+
failedText = text
|
|
408
|
+
} else {
|
|
409
|
+
current.knowsGoal = true
|
|
410
|
+
save({...idle, request, goal})
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
running = undefined
|
|
414
|
+
repaint?.()
|
|
415
|
+
} catch (failure) {
|
|
416
|
+
failed(run, failure)
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
const open = (ctx: ExtensionContext): Promise<void> => {
|
|
421
|
+
let overlay: OverlayHandle | undefined
|
|
422
|
+
|
|
423
|
+
// pi's setHidden(false) on a closed overlay focuses the dead panel, so done() comes last.
|
|
424
|
+
const askClose = async (done: () => void): Promise<void> => {
|
|
425
|
+
const answer = await whileHidden(overlay, () =>
|
|
426
|
+
ctx.ui.select(closeTitle, [keepLabel, stopLabel])
|
|
427
|
+
)
|
|
428
|
+
|
|
429
|
+
if (answer === stopLabel) done()
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const editLoop = async (): Promise<void> => {
|
|
433
|
+
const opened = state
|
|
434
|
+
let prompt = editTitle
|
|
435
|
+
let text = editText(opened)
|
|
436
|
+
|
|
437
|
+
for (;;) {
|
|
438
|
+
const edited = await ctx.ui.editor(prompt, text)
|
|
439
|
+
|
|
440
|
+
if (edited === undefined || edited === '') return
|
|
441
|
+
|
|
442
|
+
const checked = await checkEdit(ctx.cwd, opened, edited)
|
|
443
|
+
|
|
444
|
+
if (checked === 'unchanged') return
|
|
445
|
+
|
|
446
|
+
if ('plan' in checked) {
|
|
447
|
+
saveEdit(opened, checked.plan)
|
|
448
|
+
return
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
prompt = checked.reason
|
|
452
|
+
text = edited
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
const change = (text: string): void =>
|
|
457
|
+
void (state.steps.length > 0 ? planSteps(ctx, text) : refine(ctx, text))
|
|
458
|
+
|
|
459
|
+
return ctx.ui.custom<void>(
|
|
460
|
+
(tui, theme, _keybindings, done) => {
|
|
461
|
+
const actions: PanelActions = {
|
|
462
|
+
change,
|
|
463
|
+
retry: change,
|
|
464
|
+
accept: () => {
|
|
465
|
+
error = undefined
|
|
466
|
+
save({...state, accepted: true})
|
|
467
|
+
void ask(ctx)
|
|
468
|
+
},
|
|
469
|
+
answer: answerQuestion,
|
|
470
|
+
ask: () => void ask(ctx),
|
|
471
|
+
plan: () => void planSteps(ctx),
|
|
472
|
+
finish: () => {
|
|
473
|
+
closeSubagent()
|
|
474
|
+
save({...state, asked: true})
|
|
475
|
+
void planSteps(ctx)
|
|
476
|
+
},
|
|
477
|
+
changeGoal: () => {
|
|
478
|
+
closeSubagent()
|
|
479
|
+
save({...idle, request: state.request, goal: state.goal})
|
|
480
|
+
repaint?.()
|
|
481
|
+
},
|
|
482
|
+
handOver: () => {
|
|
483
|
+
done()
|
|
484
|
+
void handOver(ctx)
|
|
485
|
+
},
|
|
486
|
+
edit: () => void whileHidden(overlay, editLoop),
|
|
487
|
+
stop: () => {
|
|
488
|
+
save(idle)
|
|
489
|
+
void track(ctx)
|
|
490
|
+
},
|
|
491
|
+
close: () => done(),
|
|
492
|
+
leave: () => {
|
|
493
|
+
if (state.handed) done()
|
|
494
|
+
else void askClose(done)
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
const panel = new PlanPanel(
|
|
498
|
+
theme,
|
|
499
|
+
() => tui.requestRender(),
|
|
500
|
+
actions,
|
|
501
|
+
() => tui.terminal.rows - frameRows
|
|
502
|
+
)
|
|
503
|
+
repaint = (): void => {
|
|
504
|
+
panel.setState(panelState())
|
|
505
|
+
tui.requestRender()
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
panel.setState(panelState())
|
|
509
|
+
return {
|
|
510
|
+
...boxed(
|
|
511
|
+
panel,
|
|
512
|
+
title,
|
|
513
|
+
text => theme.fg('border', text),
|
|
514
|
+
text => theme.fg('accent', text)
|
|
515
|
+
),
|
|
516
|
+
dispose: () => {
|
|
517
|
+
repaint = undefined
|
|
518
|
+
failedText = undefined
|
|
519
|
+
closeSubagent()
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
},
|
|
523
|
+
{
|
|
524
|
+
overlay: true,
|
|
525
|
+
overlayOptions,
|
|
526
|
+
onHandle: handle => {
|
|
527
|
+
overlay = handle
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
)
|
|
531
|
+
}
|
|
532
|
+
pi.registerCommand('plan', {
|
|
533
|
+
description: 'Open the planning panel. Text after the command is sent as the request.',
|
|
534
|
+
handler: async (args, ctx) => {
|
|
535
|
+
if (ctx.mode !== 'tui') {
|
|
536
|
+
ctx.ui.notify(tuiOnly, 'warning')
|
|
537
|
+
return
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
const text = args.trim()
|
|
541
|
+
|
|
542
|
+
if (text !== '' && state !== idle) {
|
|
543
|
+
save(idle)
|
|
544
|
+
void track(ctx)
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
// Not awaited: a reload drops the overlay unsettled, and a waiting handler blocks pi.
|
|
548
|
+
open(ctx).catch((failure: unknown) => {
|
|
549
|
+
closeSubagent()
|
|
550
|
+
ctx.ui.notify(String(failure), 'error')
|
|
551
|
+
})
|
|
552
|
+
if (text !== '') void refine(ctx, text)
|
|
553
|
+
}
|
|
554
|
+
})
|
|
555
|
+
|
|
556
|
+
const restore = (_event: unknown, ctx: ExtensionContext): void => {
|
|
557
|
+
state = lastPlan(ctx).state
|
|
558
|
+
failedText = undefined
|
|
559
|
+
void track(ctx)
|
|
560
|
+
}
|
|
561
|
+
pi.on('session_start', restore)
|
|
562
|
+
pi.on('session_tree', restore)
|
|
563
|
+
// Not turn_end: pi rebuilds the whole context for a turn_end handler, in every session.
|
|
564
|
+
pi.on('tool_execution_end', ({toolName}, ctx) => {
|
|
565
|
+
if (state.handed && diskTools.has(toolName)) void track(ctx)
|
|
566
|
+
})
|
|
567
|
+
pi.on('session_shutdown', closeSubagent)
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
export {
|
|
571
|
+
closeTitle,
|
|
572
|
+
editTitle,
|
|
573
|
+
keepLabel,
|
|
574
|
+
layoutTitle,
|
|
575
|
+
misplacedTitles,
|
|
576
|
+
noGoalLine,
|
|
577
|
+
noStepsLine,
|
|
578
|
+
stopLabel,
|
|
579
|
+
tuiOnly
|
|
580
|
+
}
|
|
581
|
+
export type {PlanState}
|