@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,537 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AgentSession,
|
|
3
|
+
ExtensionAPI,
|
|
4
|
+
ExtensionContext,
|
|
5
|
+
SessionStats,
|
|
6
|
+
ToolDefinition
|
|
7
|
+
} from '@earendil-works/pi-coding-agent'
|
|
8
|
+
import {
|
|
9
|
+
createAgentSession,
|
|
10
|
+
DefaultResourceLoader,
|
|
11
|
+
getAgentDir,
|
|
12
|
+
SessionManager,
|
|
13
|
+
SettingsManager
|
|
14
|
+
} from '@earendil-works/pi-coding-agent'
|
|
15
|
+
import type {ThinkingLevel} from '@earendil-works/pi-agent-core'
|
|
16
|
+
import type {Api, AssistantMessage, Model} from '@earendil-works/pi-ai'
|
|
17
|
+
import {contentText} from '@earendil-works/pi-ai'
|
|
18
|
+
import {type Static, type TObject, Type} from 'typebox'
|
|
19
|
+
|
|
20
|
+
import {
|
|
21
|
+
concurrency,
|
|
22
|
+
findModel,
|
|
23
|
+
inherit,
|
|
24
|
+
missingModel,
|
|
25
|
+
modelOf,
|
|
26
|
+
read,
|
|
27
|
+
safeConfig,
|
|
28
|
+
thinkingOf,
|
|
29
|
+
unlimited
|
|
30
|
+
} from '../config/config'
|
|
31
|
+
import type {NopTool} from '../config/config'
|
|
32
|
+
import type {GuardHit} from './guards/guard'
|
|
33
|
+
import type {GuardSettings} from './guards/guards'
|
|
34
|
+
import {guardSettings, watchGuards} from './guards/guards'
|
|
35
|
+
import type {SubagentDetails, SubagentUpdate} from './progress'
|
|
36
|
+
import {slots} from './slots'
|
|
37
|
+
import {renderCall, renderResult, toolProgress} from './subagentTui'
|
|
38
|
+
|
|
39
|
+
enum SubagentErrors {
|
|
40
|
+
EmptyOutput = 'Empty model output',
|
|
41
|
+
Aborted = 'Subagent aborted',
|
|
42
|
+
RunFailed = 'Subagent run failed: ',
|
|
43
|
+
NoModel = 'No model selected',
|
|
44
|
+
Truncated = 'Subagent output cut off at the model output limit',
|
|
45
|
+
Compacted = 'Subagent context was compacted, so its answer would not rest on its source',
|
|
46
|
+
Disposed = 'Subagent session was closed',
|
|
47
|
+
Killed = 'Subagent killed by the guard: '
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
enum SubagentTools {
|
|
51
|
+
Read = 'read',
|
|
52
|
+
Grep = 'grep',
|
|
53
|
+
Find = 'find',
|
|
54
|
+
Ls = 'ls'
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
type SubagentOptions = {
|
|
58
|
+
cwd: string
|
|
59
|
+
prompt: string
|
|
60
|
+
tools: SubagentTools[]
|
|
61
|
+
thinkingLevel?: ThinkingLevel
|
|
62
|
+
model?: Model<Api>
|
|
63
|
+
customTools?: ToolDefinition[]
|
|
64
|
+
waitingTools?: string[]
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// A function is read again on every attempt, so a restart can carry what the run learned.
|
|
68
|
+
type SubagentTask = string | (() => string)
|
|
69
|
+
|
|
70
|
+
type SubagentResult = {
|
|
71
|
+
text: string
|
|
72
|
+
details: SubagentDetails
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The note sent back for an answer the caller cannot use. Undefined accepts the answer. */
|
|
76
|
+
type SendBack = (answer: string) => string | undefined | Promise<string | undefined>
|
|
77
|
+
|
|
78
|
+
type SubagentSession = {
|
|
79
|
+
/** `without` names tools the model does not get for this prompt. */
|
|
80
|
+
prompt: (
|
|
81
|
+
task: SubagentTask,
|
|
82
|
+
signal: AbortSignal | undefined,
|
|
83
|
+
onUpdate: SubagentUpdate,
|
|
84
|
+
sendBack?: SendBack,
|
|
85
|
+
without?: string[]
|
|
86
|
+
) => Promise<SubagentResult>
|
|
87
|
+
dispose: () => void
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// A cancelled call is reported as such, never as whatever failed underneath it.
|
|
91
|
+
const unlessAborted = (signal: AbortSignal | undefined, error: unknown): void => {
|
|
92
|
+
if (signal?.aborted) throw new Error(SubagentErrors.Aborted, {cause: error})
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const sentBack = 'answer'
|
|
96
|
+
const turnText = (message: AssistantMessage): string => contentText(message.content, '').trim()
|
|
97
|
+
|
|
98
|
+
const buildDetails = (
|
|
99
|
+
stats: SessionStats,
|
|
100
|
+
task: string,
|
|
101
|
+
last: AssistantMessage,
|
|
102
|
+
lastAssistantText: string,
|
|
103
|
+
startedAt: number,
|
|
104
|
+
restarts: number
|
|
105
|
+
): SubagentDetails => ({
|
|
106
|
+
...stats,
|
|
107
|
+
model: last.model,
|
|
108
|
+
provider: last.provider,
|
|
109
|
+
stopReason: last.stopReason,
|
|
110
|
+
timestamp: last.timestamp,
|
|
111
|
+
task,
|
|
112
|
+
lastAssistantText,
|
|
113
|
+
elapsedMs: performance.now() - startedAt,
|
|
114
|
+
restarts
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
const defaultOptions = (cwd: string): SubagentOptions => ({
|
|
118
|
+
cwd,
|
|
119
|
+
prompt: '',
|
|
120
|
+
tools: [SubagentTools.Read, SubagentTools.Grep, SubagentTools.Find, SubagentTools.Ls]
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
const configuredOptions = (ctx: ExtensionContext, tool: NopTool): SubagentOptions => {
|
|
124
|
+
const nop = safeConfig(ctx)
|
|
125
|
+
const thinkingLevel = read(nop, thinkingOf(tool))
|
|
126
|
+
const spec = read(nop, modelOf(tool))
|
|
127
|
+
const model = spec === inherit ? undefined : findModel(ctx.modelRegistry, spec)
|
|
128
|
+
|
|
129
|
+
if (spec !== inherit && model === undefined) ctx.ui.notify(missingModel(spec), 'warning')
|
|
130
|
+
return {
|
|
131
|
+
...defaultOptions(ctx.cwd),
|
|
132
|
+
thinkingLevel: thinkingLevel === inherit ? ctx.thinkingLevel : thinkingLevel,
|
|
133
|
+
model: model ?? ctx.model
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const toolNames = (options: SubagentOptions): string[] => [
|
|
138
|
+
...options.tools,
|
|
139
|
+
...(options.customTools ?? []).map(tool => tool.name)
|
|
140
|
+
]
|
|
141
|
+
|
|
142
|
+
const buildSession = async (options: SubagentOptions): Promise<AgentSession> => {
|
|
143
|
+
const customTools = options.customTools ?? []
|
|
144
|
+
const settingsManager = SettingsManager.create(options.cwd, getAgentDir())
|
|
145
|
+
|
|
146
|
+
const loader = new DefaultResourceLoader({
|
|
147
|
+
cwd: options.cwd,
|
|
148
|
+
agentDir: getAgentDir(),
|
|
149
|
+
settingsManager,
|
|
150
|
+
noExtensions: true,
|
|
151
|
+
noSkills: true,
|
|
152
|
+
noPromptTemplates: true,
|
|
153
|
+
noThemes: true,
|
|
154
|
+
noContextFiles: true,
|
|
155
|
+
systemPrompt: options.prompt
|
|
156
|
+
})
|
|
157
|
+
await loader.reload()
|
|
158
|
+
// A compacted page is a summary, and the model answers from it as if it were the page.
|
|
159
|
+
// Overflowing must fail the run instead. Reload rereads the files, so this goes after it.
|
|
160
|
+
settingsManager.applyOverrides({compaction: {enabled: false}})
|
|
161
|
+
|
|
162
|
+
const {session} = await createAgentSession({
|
|
163
|
+
cwd: options.cwd,
|
|
164
|
+
sessionManager: SessionManager.inMemory(options.cwd),
|
|
165
|
+
settingsManager,
|
|
166
|
+
resourceLoader: loader,
|
|
167
|
+
// pi drops a custom tool whose name is not on the allowlist.
|
|
168
|
+
tools: toolNames(options),
|
|
169
|
+
customTools,
|
|
170
|
+
thinkingLevel: options.thinkingLevel,
|
|
171
|
+
model: options.model
|
|
172
|
+
})
|
|
173
|
+
return session
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
type TurnWatch = {
|
|
177
|
+
last?: AssistantMessage
|
|
178
|
+
compacted: boolean
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const watchTurn = (
|
|
182
|
+
session: AgentSession,
|
|
183
|
+
task: string,
|
|
184
|
+
startedAt: number,
|
|
185
|
+
restarts: number,
|
|
186
|
+
onUpdate: SubagentUpdate
|
|
187
|
+
): {watch: TurnWatch; stop: () => void} => {
|
|
188
|
+
const watch: TurnWatch = {compacted: false}
|
|
189
|
+
let lastText = ''
|
|
190
|
+
const stop = session.subscribe(event => {
|
|
191
|
+
// Compaction is off in buildSession; this catches it coming back through a pi change.
|
|
192
|
+
if (event.type === 'compaction_start') {
|
|
193
|
+
watch.compacted = true
|
|
194
|
+
void session.abort()
|
|
195
|
+
return
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (event.type !== 'turn_end' || event.message.role !== 'assistant') return
|
|
199
|
+
|
|
200
|
+
watch.last = event.message
|
|
201
|
+
const text = turnText(event.message)
|
|
202
|
+
if (text !== '') lastText = text
|
|
203
|
+
|
|
204
|
+
const details = buildDetails(
|
|
205
|
+
session.getSessionStats(),
|
|
206
|
+
task,
|
|
207
|
+
event.message,
|
|
208
|
+
lastText,
|
|
209
|
+
startedAt,
|
|
210
|
+
restarts
|
|
211
|
+
)
|
|
212
|
+
onUpdate?.({type: 'turn', text: lastText, details})
|
|
213
|
+
})
|
|
214
|
+
return {watch, stop}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
type Attempt = {
|
|
218
|
+
watch: TurnWatch
|
|
219
|
+
hit: GuardHit | undefined
|
|
220
|
+
/** Drops the attempt's messages, pi's own branch pattern, so the next one starts clean. */
|
|
221
|
+
rewind: () => void
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const runAttempt = async (
|
|
225
|
+
session: AgentSession,
|
|
226
|
+
text: string,
|
|
227
|
+
task: string,
|
|
228
|
+
startedAt: number,
|
|
229
|
+
restarts: number,
|
|
230
|
+
onUpdate: SubagentUpdate,
|
|
231
|
+
settings: GuardSettings,
|
|
232
|
+
contextWindow: number
|
|
233
|
+
): Promise<Attempt> => {
|
|
234
|
+
const leaf = session.sessionManager.getLeafId()
|
|
235
|
+
const messages = [...session.agent.state.messages]
|
|
236
|
+
const {watch, stop} = watchTurn(session, task, startedAt, restarts, onUpdate)
|
|
237
|
+
const guards = watchGuards(session, settings, contextWindow)
|
|
238
|
+
|
|
239
|
+
try {
|
|
240
|
+
await session.prompt(text, {expandPromptTemplates: false})
|
|
241
|
+
} finally {
|
|
242
|
+
stop()
|
|
243
|
+
guards.stop()
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return {
|
|
247
|
+
watch,
|
|
248
|
+
hit: guards.hit(),
|
|
249
|
+
rewind: () => {
|
|
250
|
+
if (leaf === null) session.sessionManager.resetLeaf()
|
|
251
|
+
else session.sessionManager.branch(leaf)
|
|
252
|
+
session.agent.state.messages = messages
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// A failed or cut-off run is reported as that, not sent back.
|
|
258
|
+
const rejection = (last: AssistantMessage, sendBack: SendBack | undefined): ReturnType<SendBack> =>
|
|
259
|
+
last.stopReason === 'stop' ? sendBack?.(turnText(last)) : undefined
|
|
260
|
+
|
|
261
|
+
const answerText = (last: AssistantMessage): string => {
|
|
262
|
+
const {stopReason, errorMessage} = last
|
|
263
|
+
|
|
264
|
+
if (stopReason === 'error' || stopReason === 'aborted')
|
|
265
|
+
throw new Error(SubagentErrors.RunFailed + (errorMessage ?? stopReason))
|
|
266
|
+
if (stopReason === 'length') throw new Error(SubagentErrors.Truncated)
|
|
267
|
+
|
|
268
|
+
const text = turnText(last)
|
|
269
|
+
|
|
270
|
+
if (text === '') throw new Error(SubagentErrors.EmptyOutput + emptyDetail(last))
|
|
271
|
+
return text
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// What the empty turn held, so a model that answers inside its thinking can be told apart.
|
|
275
|
+
const emptyDetail = (last: AssistantMessage): string =>
|
|
276
|
+
` (${last.stopReason}; blocks: ${last.content.map(block => block.type).join(', ') || 'none'})`
|
|
277
|
+
|
|
278
|
+
// Every subagent run goes through prompt, so the slot cap covers all of them.
|
|
279
|
+
const subagentSession = (options: SubagentOptions): SubagentSession => {
|
|
280
|
+
let opening: Promise<AgentSession> | undefined
|
|
281
|
+
let session: AgentSession | undefined
|
|
282
|
+
let disposed = false
|
|
283
|
+
|
|
284
|
+
const dispose = (): void => {
|
|
285
|
+
if (disposed) return
|
|
286
|
+
disposed = true
|
|
287
|
+
session?.dispose()
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const open = async (): Promise<AgentSession> => {
|
|
291
|
+
const built = await buildSession(options)
|
|
292
|
+
|
|
293
|
+
if (disposed) {
|
|
294
|
+
built.dispose()
|
|
295
|
+
throw new Error(SubagentErrors.Disposed)
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
session = built
|
|
299
|
+
return built
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const turn = async (
|
|
303
|
+
task: SubagentTask,
|
|
304
|
+
signal: AbortSignal | undefined,
|
|
305
|
+
onUpdate: SubagentUpdate,
|
|
306
|
+
settings: GuardSettings,
|
|
307
|
+
sendBack: SendBack | undefined,
|
|
308
|
+
without: string[]
|
|
309
|
+
): Promise<SubagentResult> => {
|
|
310
|
+
const startedAt = performance.now()
|
|
311
|
+
|
|
312
|
+
if (disposed) throw new Error(SubagentErrors.Disposed)
|
|
313
|
+
if (signal?.aborted) throw new Error(SubagentErrors.Aborted)
|
|
314
|
+
|
|
315
|
+
onUpdate?.({type: 'started'})
|
|
316
|
+
opening ??= open()
|
|
317
|
+
const current = await opening
|
|
318
|
+
const contextWindow = current.model?.contextWindow ?? 0
|
|
319
|
+
current.setActiveToolsByName(toolNames(options).filter(name => !without.includes(name)))
|
|
320
|
+
|
|
321
|
+
const onAbort = () => {
|
|
322
|
+
void current.abort()
|
|
323
|
+
}
|
|
324
|
+
signal?.addEventListener('abort', onAbort, {once: true})
|
|
325
|
+
|
|
326
|
+
try {
|
|
327
|
+
let hint: string | undefined
|
|
328
|
+
let note: string | undefined
|
|
329
|
+
|
|
330
|
+
for (let restarts = 0; ; restarts += 1) {
|
|
331
|
+
if (signal?.aborted) throw new Error(SubagentErrors.Aborted)
|
|
332
|
+
|
|
333
|
+
const text = typeof task === 'string' ? task : task()
|
|
334
|
+
const {watch, hit, rewind} = await runAttempt(
|
|
335
|
+
current,
|
|
336
|
+
[hint, note ?? text].filter(part => part !== undefined).join('\n\n'),
|
|
337
|
+
text,
|
|
338
|
+
startedAt,
|
|
339
|
+
restarts,
|
|
340
|
+
onUpdate,
|
|
341
|
+
settings,
|
|
342
|
+
contextWindow
|
|
343
|
+
)
|
|
344
|
+
|
|
345
|
+
if (disposed) throw new Error(SubagentErrors.Disposed)
|
|
346
|
+
if (signal?.aborted) throw new Error(SubagentErrors.Aborted)
|
|
347
|
+
|
|
348
|
+
if (watch.compacted) {
|
|
349
|
+
dispose()
|
|
350
|
+
throw new Error(SubagentErrors.Compacted)
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
if (hit === undefined) {
|
|
354
|
+
if (watch.last === undefined) throw new Error(SubagentErrors.EmptyOutput)
|
|
355
|
+
|
|
356
|
+
const sent =
|
|
357
|
+
restarts < settings.restarts ?
|
|
358
|
+
await rejection(watch.last, sendBack)
|
|
359
|
+
: undefined
|
|
360
|
+
|
|
361
|
+
if (disposed) throw new Error(SubagentErrors.Disposed)
|
|
362
|
+
if (signal?.aborted) throw new Error(SubagentErrors.Aborted)
|
|
363
|
+
|
|
364
|
+
// The rejected answer stays in the conversation, so the note goes alone.
|
|
365
|
+
if (sent !== undefined) {
|
|
366
|
+
note = sent
|
|
367
|
+
hint = undefined
|
|
368
|
+
onUpdate?.({type: 'restarting', guard: sentBack})
|
|
369
|
+
continue
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
const answer = answerText(watch.last)
|
|
373
|
+
return {
|
|
374
|
+
text: answer,
|
|
375
|
+
details: buildDetails(
|
|
376
|
+
current.getSessionStats(),
|
|
377
|
+
text,
|
|
378
|
+
watch.last,
|
|
379
|
+
answer,
|
|
380
|
+
startedAt,
|
|
381
|
+
restarts
|
|
382
|
+
)
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
if (restarts >= settings.restarts) throw new Error(SubagentErrors.Killed + hit.id)
|
|
387
|
+
|
|
388
|
+
rewind()
|
|
389
|
+
onUpdate?.({type: 'restarting', guard: hit.id})
|
|
390
|
+
hint = hit.hint
|
|
391
|
+
}
|
|
392
|
+
} finally {
|
|
393
|
+
signal?.removeEventListener('abort', onAbort)
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
const prompt = (
|
|
398
|
+
task: SubagentTask,
|
|
399
|
+
signal: AbortSignal | undefined,
|
|
400
|
+
onUpdate: SubagentUpdate,
|
|
401
|
+
sendBack?: SendBack,
|
|
402
|
+
without: string[] = []
|
|
403
|
+
): Promise<SubagentResult> => {
|
|
404
|
+
// Read on every prompt, so a session kept open follows the current cap and guards.
|
|
405
|
+
const nop = safeConfig()
|
|
406
|
+
const cap = read(nop, concurrency)
|
|
407
|
+
slots.setCap(cap === unlimited ? Number.POSITIVE_INFINITY : cap)
|
|
408
|
+
return slots.run(
|
|
409
|
+
() =>
|
|
410
|
+
turn(
|
|
411
|
+
task,
|
|
412
|
+
signal,
|
|
413
|
+
onUpdate,
|
|
414
|
+
{...guardSettings(nop), waits: new Set(options.waitingTools)},
|
|
415
|
+
sendBack,
|
|
416
|
+
without
|
|
417
|
+
),
|
|
418
|
+
() => onUpdate?.({type: 'queued'})
|
|
419
|
+
)
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
return {prompt, dispose}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
const runSubagent = async (
|
|
426
|
+
task: string,
|
|
427
|
+
signal: AbortSignal | undefined,
|
|
428
|
+
onUpdate: SubagentUpdate,
|
|
429
|
+
options: SubagentOptions
|
|
430
|
+
): Promise<SubagentResult> => {
|
|
431
|
+
const handle = subagentSession(options)
|
|
432
|
+
|
|
433
|
+
try {
|
|
434
|
+
return await handle.prompt(task, signal, onUpdate)
|
|
435
|
+
} finally {
|
|
436
|
+
handle.dispose()
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// How a tool turns its params into the subagent task. It gets the configured options and may
|
|
441
|
+
// narrow them (prompt, tools). Everything else (run, progress, TUI) is shared and not optional.
|
|
442
|
+
type SubagentPrepare<T extends TObject> = (
|
|
443
|
+
params: Static<T>,
|
|
444
|
+
signal: AbortSignal | undefined,
|
|
445
|
+
options: SubagentOptions
|
|
446
|
+
) => Promise<{
|
|
447
|
+
task: string
|
|
448
|
+
options: SubagentOptions
|
|
449
|
+
/** When set, the run still happens but its answer is raised as the error this builds. */
|
|
450
|
+
failure?: (text: string) => string
|
|
451
|
+
/** Checks the answer and returns what the caller gets; throws to reject it. */
|
|
452
|
+
verify?: (text: string) => string
|
|
453
|
+
}>
|
|
454
|
+
|
|
455
|
+
type SubagentToolSpec<T extends TObject> = {
|
|
456
|
+
name: NopTool
|
|
457
|
+
label: string
|
|
458
|
+
description: string
|
|
459
|
+
parameters: T
|
|
460
|
+
title: (args: Static<T> | undefined) => string | undefined
|
|
461
|
+
prepare: SubagentPrepare<T>
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
const registerSubagentTool = <T extends TObject>(pi: ExtensionAPI, spec: SubagentToolSpec<T>) => {
|
|
465
|
+
pi.registerTool<T, SubagentDetails | undefined>({
|
|
466
|
+
name: spec.name,
|
|
467
|
+
label: spec.label,
|
|
468
|
+
description: spec.description,
|
|
469
|
+
parameters: spec.parameters,
|
|
470
|
+
async execute(_toolCallId, params, signal, onUpdate, ctx) {
|
|
471
|
+
const {task, options, failure, verify} = await spec.prepare(
|
|
472
|
+
params,
|
|
473
|
+
signal,
|
|
474
|
+
configuredOptions(ctx, spec.name)
|
|
475
|
+
)
|
|
476
|
+
const {text, details} = await runSubagent(task, signal, toolProgress(onUpdate), options)
|
|
477
|
+
|
|
478
|
+
if (failure !== undefined) throw new Error(failure(text))
|
|
479
|
+
return {
|
|
480
|
+
content: [{type: 'text', text: verify === undefined ? text : verify(text)}],
|
|
481
|
+
details: {...details, task: spec.title(params) ?? task}
|
|
482
|
+
}
|
|
483
|
+
},
|
|
484
|
+
renderCall: (args, theme, ctx) => renderCall(spec.name, spec.title(args), theme, ctx),
|
|
485
|
+
renderResult: (result, options, theme, ctx) =>
|
|
486
|
+
renderResult(spec.name, spec.title(ctx.args), result, options, theme, ctx)
|
|
487
|
+
})
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
export default function (pi: ExtensionAPI) {
|
|
491
|
+
registerSubagentTool(pi, {
|
|
492
|
+
name: 'subagent',
|
|
493
|
+
label: 'Subagent',
|
|
494
|
+
description:
|
|
495
|
+
'Dispatch an isolated read-only subagent to investigate and return its CONCLUSION — '
|
|
496
|
+
+ 'not the raw evidence. USE THIS FIRST, instead of running your own '
|
|
497
|
+
+ 'ls/grep/find/read, whenever a question spans MULTIPLE files or means '
|
|
498
|
+
+ 'searching/scanning code you have not already located. Doing it yourself '
|
|
499
|
+
+ 'floods your context with raw file output; the subagent reads in isolation '
|
|
500
|
+
+ 'and returns only the answer. You can dispatch several in one turn for '
|
|
501
|
+
+ 'independent questions.\n'
|
|
502
|
+
+ '\n'
|
|
503
|
+
+ 'Good fits:\n'
|
|
504
|
+
+ '- "Where/how is X handled in this repo?" across unfamiliar code\n'
|
|
505
|
+
+ '- Audits and pattern scans across many files ("every place we log PII")\n'
|
|
506
|
+
+ '- Tracing a flow across layers (router → service → database)\n'
|
|
507
|
+
+ '- Summarising long test output, logs, or shell output you do not need verbatim\n'
|
|
508
|
+
+ '\n'
|
|
509
|
+
+ 'Skip when:\n'
|
|
510
|
+
+ '- You already know the exact file — call `read` directly\n'
|
|
511
|
+
+ '- The task needs writes/edits (subagent is read-only)',
|
|
512
|
+
parameters: Type.Object({
|
|
513
|
+
task: Type.String({description: 'Task for the subagent to perform.'})
|
|
514
|
+
}),
|
|
515
|
+
title: args => args?.task,
|
|
516
|
+
prepare: (params, _signal, options) => Promise.resolve({task: params.task, options})
|
|
517
|
+
})
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
export {
|
|
521
|
+
configuredOptions,
|
|
522
|
+
registerSubagentTool,
|
|
523
|
+
runSubagent,
|
|
524
|
+
SubagentErrors,
|
|
525
|
+
subagentSession,
|
|
526
|
+
SubagentTools,
|
|
527
|
+
unlessAborted
|
|
528
|
+
}
|
|
529
|
+
export type {
|
|
530
|
+
SendBack,
|
|
531
|
+
SubagentDetails,
|
|
532
|
+
SubagentOptions,
|
|
533
|
+
SubagentSession,
|
|
534
|
+
SubagentTask,
|
|
535
|
+
SubagentToolSpec,
|
|
536
|
+
SubagentUpdate
|
|
537
|
+
}
|