@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,58 @@
|
|
|
1
|
+
import type {SettingItem, SettingsList} from '@earendil-works/pi-tui'
|
|
2
|
+
import {getKeybindings} from '@earendil-works/pi-tui'
|
|
3
|
+
|
|
4
|
+
import type {Focusable} from './box'
|
|
5
|
+
|
|
6
|
+
type Paint = (text: string) => string
|
|
7
|
+
|
|
8
|
+
// A header or gap has no values and no submenu, so SettingsList ignores Enter on it.
|
|
9
|
+
const header = (title: string, paint: Paint): SettingItem => ({
|
|
10
|
+
id: `header:${title}`,
|
|
11
|
+
label: paint(title.toUpperCase()),
|
|
12
|
+
currentValue: ''
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
const gap = (title: string): SettingItem => ({id: `gap:${title}`, label: '', currentValue: ''})
|
|
16
|
+
|
|
17
|
+
const settable = (item: SettingItem): boolean =>
|
|
18
|
+
item.values !== undefined || item.submenu !== undefined
|
|
19
|
+
|
|
20
|
+
// SettingsList steps its cursor one row at a time and cannot skip a row, so up and down are
|
|
21
|
+
// handled here and land on the next settable row. Keys inside an open submenu pass straight through.
|
|
22
|
+
const skipInert = (list: SettingsList, items: SettingItem[]): Focusable => {
|
|
23
|
+
let cursor = items.findIndex(settable)
|
|
24
|
+
let inSubmenu = false
|
|
25
|
+
|
|
26
|
+
for (const item of items) {
|
|
27
|
+
const open = item.submenu
|
|
28
|
+
if (open === undefined) continue
|
|
29
|
+
|
|
30
|
+
item.submenu = (current, done) => {
|
|
31
|
+
inSubmenu = true
|
|
32
|
+
return open(current, (value, options) => {
|
|
33
|
+
inSubmenu = false
|
|
34
|
+
done(value, options)
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const move = (step: number) => {
|
|
40
|
+
do cursor = (cursor + step + items.length) % items.length
|
|
41
|
+
while (!settable(items[cursor] as SettingItem))
|
|
42
|
+
list.selectItem((items[cursor] as SettingItem).id)
|
|
43
|
+
}
|
|
44
|
+
list.selectItem((items[cursor] as SettingItem).id)
|
|
45
|
+
return {
|
|
46
|
+
render: width => list.render(width),
|
|
47
|
+
invalidate: () => list.invalidate(),
|
|
48
|
+
handleInput: data => {
|
|
49
|
+
const keys = getKeybindings()
|
|
50
|
+
if (inSubmenu) list.handleInput(data)
|
|
51
|
+
else if (keys.matches(data, 'tui.select.up')) move(-1)
|
|
52
|
+
else if (keys.matches(data, 'tui.select.down')) move(1)
|
|
53
|
+
else list.handleInput(data)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export {gap, header, settable, skipInert}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type {ExtensionAPI} from '@earendil-works/pi-coding-agent'
|
|
2
|
+
|
|
3
|
+
import config from './config/command'
|
|
4
|
+
import webSearch from './search/search'
|
|
5
|
+
import docsTool from './subagent/docs/docs'
|
|
6
|
+
import fetchTool from './subagent/fetch/fetch'
|
|
7
|
+
import plan from './plan/plan'
|
|
8
|
+
import subagent from './subagent/subagent'
|
|
9
|
+
|
|
10
|
+
export default function (pi: ExtensionAPI) {
|
|
11
|
+
config(pi)
|
|
12
|
+
subagent(pi)
|
|
13
|
+
fetchTool(pi)
|
|
14
|
+
docsTool(pi)
|
|
15
|
+
webSearch(pi)
|
|
16
|
+
plan(pi)
|
|
17
|
+
}
|
package/src/plan/ask.ts
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import {readFile, stat} from 'node:fs/promises'
|
|
2
|
+
|
|
3
|
+
import {createReadTool, defineTool} from '@earendil-works/pi-coding-agent'
|
|
4
|
+
import {stripTerminalSequences} from '@earendil-works/pi-tui'
|
|
5
|
+
import {type Static, Type} from 'typebox'
|
|
6
|
+
|
|
7
|
+
import type {GoalTask} from './goal'
|
|
8
|
+
import {goalTask} from './goal'
|
|
9
|
+
|
|
10
|
+
const askToolName = 'ask_user'
|
|
11
|
+
const none = 'NONE'
|
|
12
|
+
const maxOptions = 5
|
|
13
|
+
|
|
14
|
+
enum AskErrors {
|
|
15
|
+
NotYet = 'Not asking yet. Give the GOAL line.',
|
|
16
|
+
Question = 'Ask the user one question.',
|
|
17
|
+
OnePerCall = 'One question per call. Ask again after this answer.',
|
|
18
|
+
Options = `Give 2 to ${maxOptions} options.`,
|
|
19
|
+
Evidence = 'The evidence line is not in that file. Copy one whole line exactly, or use NONE.',
|
|
20
|
+
Closed = 'The user closed the question.'
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Every string the model sees here was measured as written (PLANNING_MODE.md, section 10).
|
|
24
|
+
const askDescription = `Ask the user one question. ${AskErrors.Options} Call once per question.`
|
|
25
|
+
const askParameters = Type.Object({
|
|
26
|
+
question: Type.String(),
|
|
27
|
+
options: Type.Array(Type.String()),
|
|
28
|
+
evidence: Type.Object({
|
|
29
|
+
file: Type.String({description: 'Path of a file you read, or NONE.'}),
|
|
30
|
+
line: Type.String({description: 'One whole line, copied exactly from the file, or NONE.'})
|
|
31
|
+
})
|
|
32
|
+
})
|
|
33
|
+
const acceptRules = `The user accepted the goal.
|
|
34
|
+
You are planning, not implementing. Do not change any file.
|
|
35
|
+
Now find what is still open before the work can start. Read the code you need.
|
|
36
|
+
If something is unclear, call ${askToolName} with one question and options. Ask one question per call and wait for the answer.
|
|
37
|
+
When nothing is open, answer with one line: DONE`
|
|
38
|
+
const decisionsLabel = 'Decisions so far:'
|
|
39
|
+
// pi-task's hand-over paragraph, copied because pi-task is not a dependency.
|
|
40
|
+
const settled =
|
|
41
|
+
'PLANNING DECISIONS — these were settled with the user before this task was started. '
|
|
42
|
+
+ 'They are authoritative: implement them as written, and do not re-open or contradict '
|
|
43
|
+
+ 'them. They may not cover everything; decide anything they leave open as usual.'
|
|
44
|
+
|
|
45
|
+
type Evidence = Static<typeof askParameters>['evidence']
|
|
46
|
+
type Question = {question: string; options: string[]; evidence?: Evidence}
|
|
47
|
+
type Decision = {question: string; answer: string}
|
|
48
|
+
|
|
49
|
+
type AskHost = {
|
|
50
|
+
cwd: string
|
|
51
|
+
open: () => boolean
|
|
52
|
+
/** False when this assistant turn already asked. */
|
|
53
|
+
claim: () => boolean
|
|
54
|
+
stored: (question: string) => string | undefined
|
|
55
|
+
/** Returns what takes the question off the screen again. */
|
|
56
|
+
show: (question: Question, answer: (text: string) => void) => () => void
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const sameQuestion = (left: string, right: string): boolean => {
|
|
60
|
+
const key = (text: string): string => text.toLowerCase().replace(/\s+/g, ' ').trim()
|
|
61
|
+
|
|
62
|
+
return key(left) === key(right)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const distinctOptions = (options: string[]): string[] => [
|
|
66
|
+
...new Set(
|
|
67
|
+
options.map(option => stripTerminalSequences(option).trim()).filter(option => option !== '')
|
|
68
|
+
)
|
|
69
|
+
]
|
|
70
|
+
|
|
71
|
+
const isNone = ({file, line}: Evidence): boolean => file.trim() === none && line.trim() === none
|
|
72
|
+
|
|
73
|
+
// A pipe or a device never ends, and the wait on it would hold the slot.
|
|
74
|
+
const regularFile = async (path: string): Promise<void> => {
|
|
75
|
+
if (!(await stat(path)).isFile()) throw new Error(path)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// pi's read tool finds the file, so a path the model read with resolves the same way here.
|
|
79
|
+
const fileText = async (
|
|
80
|
+
cwd: string,
|
|
81
|
+
file: string,
|
|
82
|
+
signal: AbortSignal | undefined
|
|
83
|
+
): Promise<string> => {
|
|
84
|
+
let text = ''
|
|
85
|
+
const read = createReadTool(cwd, {
|
|
86
|
+
operations: {
|
|
87
|
+
access: regularFile,
|
|
88
|
+
readFile: async path => {
|
|
89
|
+
const buffer = await readFile(path, {signal})
|
|
90
|
+
text = buffer.toString('utf8')
|
|
91
|
+
return buffer
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
})
|
|
95
|
+
await read.execute(askToolName, {path: file, limit: 1}, signal)
|
|
96
|
+
return text
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const quoted = async (
|
|
100
|
+
cwd: string,
|
|
101
|
+
evidence: Evidence,
|
|
102
|
+
signal: AbortSignal | undefined
|
|
103
|
+
): Promise<boolean> => {
|
|
104
|
+
if (isNone(evidence)) return true
|
|
105
|
+
|
|
106
|
+
const wanted = evidence.line.trim()
|
|
107
|
+
|
|
108
|
+
if (wanted === '') return false
|
|
109
|
+
|
|
110
|
+
try {
|
|
111
|
+
const text = await fileText(cwd, evidence.file, signal)
|
|
112
|
+
return text.split('\n').some(row => row.trim() === wanted)
|
|
113
|
+
} catch {
|
|
114
|
+
return false
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const transcript = (decisions: Decision[]): string[] =>
|
|
119
|
+
decisions.flatMap(({question, answer}, index) => [
|
|
120
|
+
`Q${String(index + 1)}: ${question}`,
|
|
121
|
+
`A${String(index + 1)}: ${answer}`
|
|
122
|
+
])
|
|
123
|
+
|
|
124
|
+
const planHead = (unseen: GoalTask, decisions: Decision[]): string[] => {
|
|
125
|
+
const answered = decisions.length === 0 ? [] : [decisionsLabel, ...transcript(decisions)]
|
|
126
|
+
|
|
127
|
+
return [goalTask(unseen), ...answered].filter(line => line !== '')
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const questionsTask = (unseen: GoalTask, decisions: Decision[]): string => {
|
|
131
|
+
const head = planHead(unseen, decisions)
|
|
132
|
+
|
|
133
|
+
if (head.length === 0) return acceptRules
|
|
134
|
+
return [...head, '', acceptRules].join('\n')
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const handOverMessage = (
|
|
138
|
+
goal: string,
|
|
139
|
+
decisions: Decision[],
|
|
140
|
+
plan: string,
|
|
141
|
+
changes: string[]
|
|
142
|
+
): string => {
|
|
143
|
+
const answers = decisions.length === 0 ? [] : [settled, transcript(decisions).join('\n')]
|
|
144
|
+
const asked = changes.map(change => goalTask({change})).join('\n')
|
|
145
|
+
|
|
146
|
+
return [goal, ...answers, plan, asked].filter(part => part !== '').join('\n\n')
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const shown = (
|
|
150
|
+
host: AskHost,
|
|
151
|
+
signal: AbortSignal | undefined,
|
|
152
|
+
question: Question
|
|
153
|
+
): Promise<string> =>
|
|
154
|
+
new Promise((answered, closed) => {
|
|
155
|
+
// Claimed last, so a refused or already answered call does not use up the turn.
|
|
156
|
+
if (!host.claim()) {
|
|
157
|
+
closed(new Error(AskErrors.OnePerCall))
|
|
158
|
+
return
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const onScreen: {hide?: () => void} = {}
|
|
162
|
+
|
|
163
|
+
// A guard can stop the run while the panel stays open, so the question must go too.
|
|
164
|
+
const gone = (): void => {
|
|
165
|
+
onScreen.hide?.()
|
|
166
|
+
closed(new Error(AskErrors.Closed))
|
|
167
|
+
}
|
|
168
|
+
signal?.addEventListener('abort', gone, {once: true})
|
|
169
|
+
onScreen.hide = host.show(question, text => {
|
|
170
|
+
signal?.removeEventListener('abort', gone)
|
|
171
|
+
answered(text)
|
|
172
|
+
})
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
const askTool = (host: AskHost) =>
|
|
176
|
+
defineTool({
|
|
177
|
+
name: askToolName,
|
|
178
|
+
label: 'Ask user',
|
|
179
|
+
description: askDescription,
|
|
180
|
+
parameters: askParameters,
|
|
181
|
+
async execute(_toolCallId, params, signal) {
|
|
182
|
+
if (!host.open()) throw new Error(AskErrors.NotYet)
|
|
183
|
+
if (stripTerminalSequences(params.question).trim() === '')
|
|
184
|
+
throw new Error(AskErrors.Question)
|
|
185
|
+
|
|
186
|
+
const options = distinctOptions(params.options)
|
|
187
|
+
|
|
188
|
+
if (options.length < 2 || options.length > maxOptions)
|
|
189
|
+
throw new Error(AskErrors.Options)
|
|
190
|
+
const verified = await quoted(host.cwd, params.evidence, signal)
|
|
191
|
+
|
|
192
|
+
// pi leaves a waiting tool unsettled on abort, and a listener added after it never fires.
|
|
193
|
+
if (signal?.aborted) throw new Error(AskErrors.Closed)
|
|
194
|
+
if (!verified) throw new Error(AskErrors.Evidence)
|
|
195
|
+
|
|
196
|
+
const stored = host.stored(params.question)
|
|
197
|
+
const answer =
|
|
198
|
+
stored
|
|
199
|
+
?? (await shown(host, signal, {
|
|
200
|
+
question: params.question,
|
|
201
|
+
options,
|
|
202
|
+
evidence: isNone(params.evidence) ? undefined : params.evidence
|
|
203
|
+
}))
|
|
204
|
+
|
|
205
|
+
// The ground guard reads repeat, since a reworded question has a new call key.
|
|
206
|
+
return {
|
|
207
|
+
content: [{type: 'text', text: `Answer: ${answer}`}],
|
|
208
|
+
details: {repeat: stored !== undefined}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
export {
|
|
214
|
+
acceptRules,
|
|
215
|
+
AskErrors,
|
|
216
|
+
askTool,
|
|
217
|
+
askToolName,
|
|
218
|
+
distinctOptions,
|
|
219
|
+
handOverMessage,
|
|
220
|
+
planHead,
|
|
221
|
+
questionsTask,
|
|
222
|
+
quoted,
|
|
223
|
+
regularFile,
|
|
224
|
+
sameQuestion
|
|
225
|
+
}
|
|
226
|
+
export type {AskHost, Decision, Question}
|
package/src/plan/goal.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const goalLabel = 'GOAL:'
|
|
2
|
+
const goalLayout = `${goalLabel} <one sentence, what changes for the user>`
|
|
3
|
+
|
|
4
|
+
const goalPrompt = `You turn a request into one goal sentence for a coding task.
|
|
5
|
+
Read a file only when the request names something you must check.
|
|
6
|
+
Someone else does the task after you. You only name the goal.
|
|
7
|
+
Answer with one line and nothing else:
|
|
8
|
+
${goalLayout}`
|
|
9
|
+
|
|
10
|
+
const noGoalNote = `[SYSTEM NOTE: Your reply had no GOAL line, so it was not accepted. Answer again with one line and nothing else:
|
|
11
|
+
${goalLayout}]`
|
|
12
|
+
|
|
13
|
+
type GoalTask = {
|
|
14
|
+
request?: string
|
|
15
|
+
goal?: string
|
|
16
|
+
change?: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const goalTask = ({request, goal, change}: GoalTask): string =>
|
|
20
|
+
[
|
|
21
|
+
['Request', request],
|
|
22
|
+
['Current goal', goal],
|
|
23
|
+
['Change asked by the user', change]
|
|
24
|
+
]
|
|
25
|
+
.filter(([, value]) => value !== undefined)
|
|
26
|
+
.map(([label, value]) => `${label}: ${value}`)
|
|
27
|
+
.join('\n')
|
|
28
|
+
|
|
29
|
+
const parseGoal = (text: string): string | undefined => {
|
|
30
|
+
const line = text
|
|
31
|
+
.split('\n')
|
|
32
|
+
.map(row => row.replace(/[`*]/g, '').trim())
|
|
33
|
+
.find(row => row.startsWith(goalLabel))
|
|
34
|
+
const goal = line?.slice(goalLabel.length).trim()
|
|
35
|
+
|
|
36
|
+
return goal === undefined || goal === '' ? undefined : goal
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const sendBackGoal = (reply: string): string | undefined =>
|
|
40
|
+
parseGoal(reply) === undefined ? noGoalNote : undefined
|
|
41
|
+
|
|
42
|
+
export {goalPrompt, goalTask, noGoalNote, parseGoal, sendBackGoal}
|
|
43
|
+
export type {GoalTask}
|