-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.ts
More file actions
272 lines (259 loc) · 9.31 KB
/
run.ts
File metadata and controls
272 lines (259 loc) · 9.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import { stdin as input, stdout as output } from 'node:process'
import { createInterface } from 'node:readline/promises'
import type { AgentEvent, IConversationTurn } from '../index.ts'
import { createAgent } from '../index.ts'
import { loadConfig } from './config.ts'
const HISTORY_LIMIT = 16
const truncate = (s: string, n = 240): string => {
const flat = s.replace(/\s+/g, ' ').trim()
return flat.length > n ? `${flat.slice(0, n)}...` : flat
}
const formatJson = (v: unknown): string => {
try {
return truncate(JSON.stringify(v))
} catch {
return String(v)
}
}
export const runRepl = async (): Promise<void> => {
const config = loadConfig()
const mcpNames = Object.keys(config.mcpServers)
// Resolve effective per-stage models: the override block wins over the
// legacy single-string shortcut, which in turn wins over the top-level model.
const plannerModelEff = config.planner?.model ?? config.plannerModel ?? config.model
const synthModelEff = config.synthesizer?.model ?? config.synthesizerModel ?? config.model
console.log(
`[session] model=${config.model} planner=${plannerModelEff} synth=${synthModelEff} provider=${config.providerType}`,
)
console.log(
`[session] maxIterations=${config.maxIterations} maxStepsPerTask=${config.maxStepsPerTask} timeoutMs=${config.llmTimeoutMs ?? 'none'} retries=${config.llmMaxRetries ?? 2}`,
)
console.log(`[mcp] servers=${mcpNames.length ? mcpNames.join(', ') : '(none)'}`)
console.log('[hint] commands: /status, /tools, /history, /reset, /reconnect, /exit\n')
let streamingFinal = false
let streamingThought = false
const onEvent = (event: AgentEvent): void => {
switch (event.type) {
case 'log':
console.log(`[${event.level}] ${event.message}`)
break
case 'plan.thought-delta':
if (!streamingThought) {
process.stdout.write('\n[plan] ')
streamingThought = true
}
process.stdout.write(event.delta)
break
case 'plan.step-added':
// Show the partial step as soon as the planner has streamed enough
// to display. The same step may be revised before plan.created lands;
// we re-render from plan.created when it arrives (cleaner than tracking
// partial->final diffs in the REPL).
if (streamingThought) {
process.stdout.write('\n')
streamingThought = false
}
console.log(` ${event.index + 1}. ${event.step.description}`)
break
case 'plan.created':
if (streamingThought) {
process.stdout.write('\n')
streamingThought = false
} else {
console.log(`\n[plan] ${event.plan.thought}`)
}
for (const [i, s] of event.plan.steps.entries()) {
const tools = s.suggestedTools?.length ? ` (tools: ${s.suggestedTools.join(', ')})` : ''
console.log(` ${i + 1}. ${s.description}${tools}`)
}
break
case 'plan.revised':
console.log(`\n[replan] reason=${event.reason}`)
for (const [i, s] of event.plan.steps.entries()) {
console.log(` ${i + 1}. ${s.description}`)
}
break
case 'step.start':
console.log(`\n[step ${event.index + 1}] ${event.step.description}`)
break
case 'step.tool-call':
console.log(` -> ${event.name} ${formatJson(event.input)}`)
break
case 'step.tool-result': {
const status = event.ok ? 'ok' : 'fail'
console.log(` <- ${event.name} ${status} ${formatJson(event.output)}`)
break
}
case 'step.complete':
console.log(
` = ${truncate(event.result.summary, 400)} (${event.result.durationMs}ms, ${event.result.toolCalls.length} tool calls)`,
)
break
case 'replan.decision':
if (event.mode !== 'continue') {
console.log(`[replan] ${event.mode} (${event.cause}): ${event.reason}`)
}
break
case 'usage':
// Per-run total surfaces in [meta] via result.usage. We do not
// accumulate here.
break
case 'retry':
// If the planner is retrying, the partial thought we already streamed
// is no longer authoritative - flush the line and reset the flag.
if (event.phase === 'plan' && streamingThought) {
process.stdout.write('\n')
streamingThought = false
}
console.log(`[retry] ${event.phase} attempt=${event.attempt}: ${event.error}`)
break
case 'budget.exceeded':
console.log(`[budget] token cap reached: ${event.tokens}/${event.cap} - finishing early`)
break
case 'revisions.exceeded':
console.log(`[budget] max ${event.cap} replan-revisions reached - finishing early`)
break
case 'final.text-delta':
if (!streamingFinal) {
process.stdout.write('\nassistant> ')
streamingFinal = true
}
process.stdout.write(event.delta)
break
case 'final':
if (streamingFinal) {
process.stdout.write('\n\n')
streamingFinal = false
} else {
console.log(`\nassistant> ${event.text}\n`)
}
break
case 'error':
// A planner failure may interrupt mid-stream. Flush the partial
// [plan] line so the next output (fallback plan or error) starts
// cleanly, mirroring what the retry case does.
if (event.phase === 'plan' && streamingThought) {
process.stdout.write('\n')
streamingThought = false
}
console.error(`[error] (${event.phase}) ${event.error.message}`)
break
}
}
const agent = await createAgent(config, onEvent)
const tools = agent.listTools()
console.log(
`[tools] available=${tools.length}${tools.length ? `: ${tools.map((t) => t.name).join(', ')}` : ''}`,
)
const rl = createInterface({ input, output })
const history: IConversationTurn[] = []
let abortController: AbortController | null = null
// Use process-level SIGINT instead of rl.on('SIGINT'): the latter only fires
// while rl.question() is actively reading. During `await agent.run(...)`
// readline is idle, so Ctrl-C would otherwise hit Node's default handler
// (terminate). With this listener Ctrl-C cancels the run if one is active,
// and gracefully closes the REPL otherwise.
const onSigInt = () => {
if (abortController) {
console.log('\n[abort] cancelling current run...')
abortController.abort()
} else {
rl.close()
}
}
process.on('SIGINT', onSigInt)
try {
while (true) {
let prompt: string
try {
prompt = (await rl.question('\nyou> ')).trim()
} catch {
break
}
if (!prompt) {
continue
}
if (prompt === '/exit' || prompt === '/quit') {
break
}
if (prompt === '/tools') {
if (tools.length === 0) {
console.log('[tools] (none)')
} else {
for (const t of tools) {
console.log(` - ${t.name}: ${truncate(t.description, 200)}`)
}
}
continue
}
if (prompt === '/status') {
console.log(
`[status] model=${config.model} planner=${plannerModelEff} synth=${synthModelEff}`,
)
console.log(
`[status] tools=${tools.length} mcp=${mcpNames.join(', ') || '(none)'} historyTurns=${history.length}`,
)
continue
}
if (prompt === '/history') {
if (history.length === 0) {
console.log('[history] (empty)')
} else {
for (const t of history) {
console.log(` ${t.role}: ${truncate(t.content, 200)}`)
}
}
continue
}
if (prompt === '/reset') {
history.length = 0
console.log('[history] cleared')
continue
}
if (prompt === '/reconnect') {
try {
await agent.reconnect()
const fresh = agent.listTools()
tools.length = 0
for (const t of fresh) {
tools.push(t)
}
console.log(`[mcp] reconnected, ${tools.length} tools available`)
} catch (err) {
console.error('[error] reconnect failed:', (err as Error).message)
}
continue
}
streamingFinal = false
streamingThought = false
abortController = new AbortController()
try {
const result = await agent.run({
input: prompt,
history,
signal: abortController.signal,
})
history.push({ role: 'user', content: prompt })
history.push({ role: 'assistant', content: result.text })
while (history.length > HISTORY_LIMIT) {
history.shift()
}
console.log(
`[meta] iterations=${result.iterations} steps=${result.trace.length} tokens=${result.usage.totalTokens} (in=${result.usage.inputTokens} out=${result.usage.outputTokens})`,
)
} catch (err) {
if ((err as { name?: string })?.name === 'AbortError') {
console.log('[abort] run cancelled')
} else {
console.error('[error]', (err as Error).message)
}
} finally {
abortController = null
}
}
} finally {
process.off('SIGINT', onSigInt)
rl.close()
await agent.close().catch((err: unknown) => console.error('[cleanup] agent', err))
}
}