@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,292 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type AgentToolResult,
|
|
3
|
+
getMarkdownTheme,
|
|
4
|
+
type Theme,
|
|
5
|
+
type ToolDefinition,
|
|
6
|
+
type ToolRenderResultOptions
|
|
7
|
+
} from '@earendil-works/pi-coding-agent'
|
|
8
|
+
import type {AgentToolUpdateCallback} from '@earendil-works/pi-agent-core'
|
|
9
|
+
import type {TSchema} from 'typebox'
|
|
10
|
+
import type {Component} from '@earendil-works/pi-tui'
|
|
11
|
+
import {Container, Markdown, Spacer, Text, truncateToWidth} from '@earendil-works/pi-tui'
|
|
12
|
+
|
|
13
|
+
import {formatElapsed, Spinner, spinnerFrames} from '../tui/spinner'
|
|
14
|
+
import {progressLabel} from './progress'
|
|
15
|
+
import type {SubagentDetails, SubagentProgress, SubagentUpdate} from './progress'
|
|
16
|
+
|
|
17
|
+
type SubagentPhase = 'queued' | 'running' | 'done' | 'error'
|
|
18
|
+
|
|
19
|
+
type SubagentToolResult = AgentToolResult<SubagentDetails | undefined>
|
|
20
|
+
type SubagentToolUpdate = AgentToolUpdateCallback<SubagentDetails | undefined> | undefined
|
|
21
|
+
|
|
22
|
+
const queued = progressLabel({type: 'queued'})
|
|
23
|
+
const clockIcon = '◷'
|
|
24
|
+
const barWidth = 10
|
|
25
|
+
const barFilled = '■'
|
|
26
|
+
const barEmpty = '□'
|
|
27
|
+
const gutter = ' '
|
|
28
|
+
const fullReset = '\u001b[0m'
|
|
29
|
+
const softReset = '\u001b[39m\u001b[22m'
|
|
30
|
+
const codeFence = /```[a-zA-Z0-9]*/g
|
|
31
|
+
|
|
32
|
+
class SubagentRows extends Spinner implements Component {
|
|
33
|
+
private rows: [string, string, string] = ['', '', '']
|
|
34
|
+
|
|
35
|
+
running = false
|
|
36
|
+
|
|
37
|
+
// Spelled out because bun's coverage blames a synthesized constructor on the lines below it.
|
|
38
|
+
constructor(requestRepaint: () => void) {
|
|
39
|
+
super(requestRepaint)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
setRows(rows: [string, string, string]) {
|
|
43
|
+
this.rows = rows
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
setRunning(running: boolean) {
|
|
47
|
+
this.running = running
|
|
48
|
+
|
|
49
|
+
if (!running) this.stop()
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
render(width: number): string[] {
|
|
53
|
+
this.painted()
|
|
54
|
+
|
|
55
|
+
if (this.running) this.start()
|
|
56
|
+
return this.rows.map(row =>
|
|
57
|
+
truncateToWidth(row, Math.max(1, width), '', true).replaceAll(fullReset, softReset)
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
invalidate() {}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
class CallSlot implements Component {
|
|
65
|
+
constructor(
|
|
66
|
+
private readonly state: SubagentRenderState,
|
|
67
|
+
private readonly rows: SubagentRows
|
|
68
|
+
) {}
|
|
69
|
+
|
|
70
|
+
render(width: number): string[] {
|
|
71
|
+
return this.state.settled ? [] : this.rows.render(width)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
invalidate() {
|
|
75
|
+
this.rows.invalidate()
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
type SubagentRenderState = {
|
|
80
|
+
rows?: SubagentRows
|
|
81
|
+
call?: CallSlot
|
|
82
|
+
body?: {text: string; markdown: Markdown}
|
|
83
|
+
expanded?: {container: Container; spacer: Spacer; error: Text}
|
|
84
|
+
settled?: boolean
|
|
85
|
+
details?: SubagentDetails
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
type SubagentTool = ToolDefinition<TSchema, SubagentDetails, SubagentRenderState>
|
|
89
|
+
|
|
90
|
+
type SubagentRenderContext = Parameters<NonNullable<SubagentTool['renderCall']>>[2] & {
|
|
91
|
+
state: SubagentRenderState
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const rowsFor = (ctx: SubagentRenderContext): SubagentRows => {
|
|
95
|
+
const rows = (ctx.state.rows ??= new SubagentRows(ctx.invalidate))
|
|
96
|
+
|
|
97
|
+
if (ctx.executionStarted && rows.startedAt === undefined) rows.startedAt = Date.now()
|
|
98
|
+
return rows
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const formatK = (tokens: number): string =>
|
|
102
|
+
tokens >= 9950 ? String(Math.round(tokens / 1000)) : (tokens / 1000).toFixed(1)
|
|
103
|
+
|
|
104
|
+
const formatContext = (details: SubagentDetails | undefined): {bar: string; label: string} => {
|
|
105
|
+
const usage = details?.contextUsage
|
|
106
|
+
const tokens = usage?.tokens ?? null
|
|
107
|
+
const window = usage?.contextWindow ?? 0
|
|
108
|
+
|
|
109
|
+
if (tokens === null || window <= 0) return {bar: barEmpty.repeat(barWidth), label: '?/?'}
|
|
110
|
+
|
|
111
|
+
const ratio = Math.min(1, Math.max(0, tokens / window))
|
|
112
|
+
const filled = Math.round(ratio * barWidth)
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
bar: barFilled.repeat(filled) + barEmpty.repeat(barWidth - filled),
|
|
116
|
+
label: `${formatK(tokens)}/${formatK(window)}k`
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const statusIcon = (theme: Theme, phase: SubagentPhase, frame: number): string => {
|
|
121
|
+
if (phase === 'queued') return theme.fg('muted', clockIcon)
|
|
122
|
+
if (phase === 'running') return theme.fg('accent', spinnerFrames[frame])
|
|
123
|
+
if (phase === 'error') return theme.fg('error', '✗')
|
|
124
|
+
return theme.fg('success', '✓')
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const oneLine = (text: string | undefined): string =>
|
|
128
|
+
(text ?? '').replace(codeFence, ' ').replace(/\s+/g, ' ').trim()
|
|
129
|
+
|
|
130
|
+
const firstText = (result: SubagentToolResult): string => {
|
|
131
|
+
const part = result.content.find(item => item.type === 'text')
|
|
132
|
+
|
|
133
|
+
return part === undefined ? '' : part.text
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// A turn of only tool calls has no text, but it proves the run is past starting.
|
|
137
|
+
const idleMessage = (details: SubagentDetails | undefined): string =>
|
|
138
|
+
details === undefined ? 'starting…' : 'working…'
|
|
139
|
+
|
|
140
|
+
const statusMessage = (
|
|
141
|
+
details: SubagentDetails | undefined,
|
|
142
|
+
phase: SubagentPhase,
|
|
143
|
+
fallbackMessage: string
|
|
144
|
+
): string =>
|
|
145
|
+
phase === 'error' ?
|
|
146
|
+
oneLine(fallbackMessage) || oneLine(details?.lastAssistantText) || 'failed'
|
|
147
|
+
: oneLine(details?.lastAssistantText) || oneLine(fallbackMessage) || idleMessage(details)
|
|
148
|
+
|
|
149
|
+
const phaseFor = (
|
|
150
|
+
options: ToolRenderResultOptions,
|
|
151
|
+
isError: boolean,
|
|
152
|
+
text: string
|
|
153
|
+
): SubagentPhase => {
|
|
154
|
+
if (options.isPartial) return text === queued ? 'queued' : 'running'
|
|
155
|
+
if (isError) return 'error'
|
|
156
|
+
return 'done'
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const restartsLabel = (theme: Theme, restarts: number): string =>
|
|
160
|
+
restarts === 0 ? '' : theme.fg('muted', ' • RESTART ') + theme.fg('dim', String(restarts))
|
|
161
|
+
|
|
162
|
+
const buildRows = (
|
|
163
|
+
name: string,
|
|
164
|
+
title: string | undefined,
|
|
165
|
+
details: SubagentDetails | undefined,
|
|
166
|
+
phase: SubagentPhase,
|
|
167
|
+
fallbackMessage: string,
|
|
168
|
+
theme: Theme,
|
|
169
|
+
rows: SubagentRows
|
|
170
|
+
): [string, string, string] => {
|
|
171
|
+
const {bar, label} = formatContext(details)
|
|
172
|
+
const liveMs = rows.elapsedMs()
|
|
173
|
+
const elapsedMs = Math.max(liveMs, details?.elapsedMs ?? 0)
|
|
174
|
+
const message = statusMessage(details, phase, fallbackMessage)
|
|
175
|
+
|
|
176
|
+
return [
|
|
177
|
+
`${statusIcon(theme, phase, rows.frame)} `
|
|
178
|
+
+ theme.fg('toolTitle', theme.bold(name))
|
|
179
|
+
+ theme.fg('muted', ' - ')
|
|
180
|
+
+ theme.fg('dim', oneLine(title) || '…'),
|
|
181
|
+
gutter + theme.fg(phase === 'error' ? 'error' : 'toolOutput', message),
|
|
182
|
+
gutter
|
|
183
|
+
+ theme.fg('muted', 'TIME ')
|
|
184
|
+
+ theme.fg('dim', formatElapsed(elapsedMs))
|
|
185
|
+
+ theme.fg('muted', ' • TURN ')
|
|
186
|
+
+ theme.fg('dim', String(details?.assistantMessages ?? 0))
|
|
187
|
+
+ restartsLabel(theme, details?.restarts ?? 0)
|
|
188
|
+
+ theme.fg('muted', ' • ')
|
|
189
|
+
+ theme.fg('accent', bar)
|
|
190
|
+
+ ' '
|
|
191
|
+
+ theme.fg('dim', label)
|
|
192
|
+
]
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const markdownFor = (state: SubagentRenderState, text: string): Markdown => {
|
|
196
|
+
const cached = state.body
|
|
197
|
+
|
|
198
|
+
if (cached !== undefined && cached.text === text) return cached.markdown
|
|
199
|
+
|
|
200
|
+
const markdown = new Markdown(text, 0, 0, getMarkdownTheme())
|
|
201
|
+
state.body = {text, markdown}
|
|
202
|
+
return markdown
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// pi carries a partial result as text plus details, so only a turn brings details.
|
|
206
|
+
const toolUpdate = (event: SubagentProgress): SubagentToolResult => ({
|
|
207
|
+
content: [{type: 'text', text: progressLabel(event)}],
|
|
208
|
+
details: event.type === 'turn' ? event.details : undefined
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
const toolProgress = (onUpdate: SubagentToolUpdate): SubagentUpdate =>
|
|
212
|
+
onUpdate === undefined ? undefined : event => onUpdate(toolUpdate(event))
|
|
213
|
+
|
|
214
|
+
const renderCall = (
|
|
215
|
+
name: string,
|
|
216
|
+
title: string | undefined,
|
|
217
|
+
theme: Theme,
|
|
218
|
+
ctx: SubagentRenderContext
|
|
219
|
+
): Component => {
|
|
220
|
+
const rows = rowsFor(ctx)
|
|
221
|
+
|
|
222
|
+
if (!ctx.state.settled) {
|
|
223
|
+
rows.setRunning(true)
|
|
224
|
+
rows.setRows(buildRows(name, title, undefined, 'running', '', theme, rows))
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return (ctx.state.call ??= new CallSlot(ctx.state, rows))
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const renderResult = (
|
|
231
|
+
name: string,
|
|
232
|
+
title: string | undefined,
|
|
233
|
+
result: SubagentToolResult,
|
|
234
|
+
options: ToolRenderResultOptions,
|
|
235
|
+
theme: Theme,
|
|
236
|
+
ctx: SubagentRenderContext
|
|
237
|
+
): Component => {
|
|
238
|
+
const rows = rowsFor(ctx)
|
|
239
|
+
ctx.state.settled = true
|
|
240
|
+
|
|
241
|
+
if (result.details !== undefined)
|
|
242
|
+
ctx.state.details = {
|
|
243
|
+
...result.details,
|
|
244
|
+
lastAssistantText:
|
|
245
|
+
result.details.lastAssistantText || (ctx.state.details?.lastAssistantText ?? '')
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const text = firstText(result)
|
|
249
|
+
const phase = phaseFor(options, ctx.isError, text)
|
|
250
|
+
// Waiting for a slot is not run time, so the clock restarts once the run begins.
|
|
251
|
+
if (phase === 'queued') rows.startedAt = undefined
|
|
252
|
+
rows.setRunning(phase === 'running')
|
|
253
|
+
rows.setRows(
|
|
254
|
+
buildRows(
|
|
255
|
+
name,
|
|
256
|
+
title ?? ctx.state.details?.task,
|
|
257
|
+
ctx.state.details,
|
|
258
|
+
phase,
|
|
259
|
+
text,
|
|
260
|
+
theme,
|
|
261
|
+
rows
|
|
262
|
+
)
|
|
263
|
+
)
|
|
264
|
+
|
|
265
|
+
if (phase === 'running' || !options.expanded || text.trim() === '') return rows
|
|
266
|
+
|
|
267
|
+
const expanded = (ctx.state.expanded ??= {
|
|
268
|
+
container: new Container(),
|
|
269
|
+
spacer: new Spacer(1),
|
|
270
|
+
error: new Text('', 0, 0)
|
|
271
|
+
})
|
|
272
|
+
expanded.error.setText(theme.fg('error', text))
|
|
273
|
+
expanded.container.clear()
|
|
274
|
+
expanded.container.addChild(rows)
|
|
275
|
+
expanded.container.addChild(expanded.spacer)
|
|
276
|
+
expanded.container.addChild(
|
|
277
|
+
phase === 'error' ? expanded.error : markdownFor(ctx.state, text.trim())
|
|
278
|
+
)
|
|
279
|
+
return expanded.container
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export {
|
|
283
|
+
CallSlot,
|
|
284
|
+
clockIcon,
|
|
285
|
+
oneLine,
|
|
286
|
+
renderCall,
|
|
287
|
+
renderResult,
|
|
288
|
+
SubagentRows,
|
|
289
|
+
toolProgress,
|
|
290
|
+
toolUpdate
|
|
291
|
+
}
|
|
292
|
+
export type {SubagentRenderState}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type {OverlayHandle} from '@earendil-works/pi-tui'
|
|
2
|
+
|
|
3
|
+
const whileHidden = async <T>(
|
|
4
|
+
overlay: OverlayHandle | undefined,
|
|
5
|
+
run: () => Promise<T>
|
|
6
|
+
): Promise<T> => {
|
|
7
|
+
overlay?.setHidden(true)
|
|
8
|
+
|
|
9
|
+
try {
|
|
10
|
+
return await run()
|
|
11
|
+
} finally {
|
|
12
|
+
overlay?.setHidden(false)
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export {whileHidden}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']
|
|
2
|
+
const spinnerIntervalMs = 100
|
|
3
|
+
const idleTicks = 20
|
|
4
|
+
|
|
5
|
+
const formatElapsed = (elapsedMs: number): string => {
|
|
6
|
+
const seconds = Math.max(0, Math.floor(elapsedMs / 1000))
|
|
7
|
+
const minutes = Math.floor(seconds / 60)
|
|
8
|
+
const hours = Math.floor(minutes / 60)
|
|
9
|
+
const clock = `${String(minutes % 60).padStart(2, '0')}:${String(seconds % 60).padStart(2, '0')}`
|
|
10
|
+
|
|
11
|
+
if (hours === 0) return clock
|
|
12
|
+
return `${String(hours)}:${clock}`
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Ticks only while something paints it: after idleTicks unseen ticks the timer parks itself.
|
|
16
|
+
class Spinner {
|
|
17
|
+
private timer: ReturnType<typeof setInterval> | undefined
|
|
18
|
+
private missedTicks = 0
|
|
19
|
+
|
|
20
|
+
frame = 0
|
|
21
|
+
startedAt: number | undefined
|
|
22
|
+
|
|
23
|
+
constructor(private readonly requestRepaint: () => void) {}
|
|
24
|
+
|
|
25
|
+
painted() {
|
|
26
|
+
this.missedTicks = 0
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
elapsedMs(): number {
|
|
30
|
+
if (this.startedAt === undefined) return 0
|
|
31
|
+
return Date.now() - this.startedAt
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
start() {
|
|
35
|
+
if (this.timer !== undefined) return
|
|
36
|
+
|
|
37
|
+
const timer = setInterval(() => {
|
|
38
|
+
this.missedTicks += 1
|
|
39
|
+
|
|
40
|
+
if (this.missedTicks > idleTicks) {
|
|
41
|
+
this.stop()
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
this.frame = (this.frame + 1) % spinnerFrames.length
|
|
46
|
+
this.requestRepaint()
|
|
47
|
+
}, spinnerIntervalMs)
|
|
48
|
+
timer.unref?.()
|
|
49
|
+
this.timer = timer
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
isTicking(): boolean {
|
|
53
|
+
return this.timer !== undefined
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
stop() {
|
|
57
|
+
if (this.timer === undefined) return
|
|
58
|
+
|
|
59
|
+
clearInterval(this.timer)
|
|
60
|
+
this.timer = undefined
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export {formatElapsed, idleTicks, Spinner, spinnerFrames, spinnerIntervalMs}
|