-
Notifications
You must be signed in to change notification settings - Fork 948
Expand file tree
/
Copy pathengine.ts
More file actions
737 lines (656 loc) · 21.8 KB
/
engine.ts
File metadata and controls
737 lines (656 loc) · 21.8 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
/**
* QueryEngine - Core agentic loop
*
* Manages the full conversation lifecycle:
* 1. Take user prompt
* 2. Build system prompt with context (git status, project context, tools)
* 3. Call LLM API with tools (via provider abstraction)
* 4. Stream response
* 5. Execute tool calls (concurrent for read-only, serial for mutations)
* 6. Send results back, repeat until done
* 7. Auto-compact when context exceeds threshold
* 8. Retry with exponential backoff on transient errors
*/
import type {
SDKMessage,
QueryEngineConfig,
ToolDefinition,
ToolResult,
ToolContext,
TokenUsage,
} from './types.js'
import type {
LLMProvider,
CreateMessageResponse,
NormalizedMessageParam,
NormalizedTool,
} from './providers/types.js'
import {
estimateMessagesTokens,
estimateCost,
getAutoCompactThreshold,
} from './utils/tokens.js'
import {
shouldAutoCompact,
compactConversation,
microCompactMessages,
createAutoCompactState,
type AutoCompactState,
} from './utils/compact.js'
import {
withRetry,
isPromptTooLongError,
} from './utils/retry.js'
import { getSystemContext, getUserContext } from './utils/context.js'
import { normalizeMessagesForAPI } from './utils/messages.js'
import type { HookRegistry, HookInput, HookOutput } from './hooks.js'
// ============================================================================
// Tool format conversion
// ============================================================================
/** Convert a ToolDefinition to the normalized provider tool format. */
function toProviderTool(tool: ToolDefinition): NormalizedTool {
return {
name: tool.name,
description: tool.description,
input_schema: tool.inputSchema,
}
}
// ============================================================================
// ToolUseBlock (internal type for extracted tool_use blocks)
// ============================================================================
interface ToolUseBlock {
type: 'tool_use'
id: string
name: string
input: any
}
// ============================================================================
// Structured-output JSON extraction
// ============================================================================
/**
* Attempt to parse JSON out of arbitrary assistant text.
*
* Tries, in order:
* 1. Parse the trimmed text as-is.
* 2. Strip Markdown code fences (```json ... ``` or ``` ... ```) and parse.
* 3. Slice from the first `{` to the last `}` (or first `[` / last `]`).
*
* Returns `undefined` if no valid JSON can be recovered, so callers can fall
* back to the previous value rather than overwriting it with garbage.
*/
function tryParseJson(text: string): unknown {
const raw = text.trim()
if (!raw) return undefined
const tryOnce = (candidate: string): unknown => {
try {
return JSON.parse(candidate)
} catch {
return undefined
}
}
// 1. As-is
const direct = tryOnce(raw)
if (direct !== undefined) return direct
// 2. Strip fences (```json\n...\n``` or ```\n...\n```)
const fenced = raw.match(/^```(?:json)?\s*\n([\s\S]*?)\n```$/i)
if (fenced) {
const stripped = tryOnce(fenced[1].trim())
if (stripped !== undefined) return stripped
}
// 3. Slice to the largest JSON-looking substring
const objStart = raw.indexOf('{')
const objEnd = raw.lastIndexOf('}')
if (objStart !== -1 && objEnd > objStart) {
const sliced = tryOnce(raw.slice(objStart, objEnd + 1))
if (sliced !== undefined) return sliced
}
const arrStart = raw.indexOf('[')
const arrEnd = raw.lastIndexOf(']')
if (arrStart !== -1 && arrEnd > arrStart) {
const sliced = tryOnce(raw.slice(arrStart, arrEnd + 1))
if (sliced !== undefined) return sliced
}
return undefined
}
// ============================================================================
// System Prompt Builder
// ============================================================================
/**
* Build the structured-output schema block that gets appended to the system
* prompt. Kept identical regardless of whether the user supplied a custom
* `systemPrompt` or relied on the engine default, so that the model always
* sees the schema when `outputFormat` is set.
*/
function buildStructuredOutputBlock(config: QueryEngineConfig): string | undefined {
if (!config.outputFormat) return undefined
return (
'\n\n# Structured Output Schema\n' +
'You must respond with a JSON object that strictly follows this schema:\n' +
JSON.stringify(config.outputFormat.schema, null, 2) +
'\n\nReply with ONLY the JSON object, no markdown fences, no extra text.'
)
}
async function buildSystemPrompt(config: QueryEngineConfig): Promise<string> {
const structuredBlock = buildStructuredOutputBlock(config)
if (config.systemPrompt) {
let prompt = config.systemPrompt
if (config.appendSystemPrompt) {
prompt += '\n\n' + config.appendSystemPrompt
}
if (structuredBlock) {
prompt += structuredBlock
}
return prompt
}
const parts: string[] = []
parts.push(
'You are an AI assistant with access to tools. Use the tools provided to help the user accomplish their tasks.',
'You should use tools when they would help you complete the task more accurately or efficiently.',
)
// List available tools with descriptions
parts.push('\n# Available Tools\n')
for (const tool of config.tools) {
parts.push(`- **${tool.name}**: ${tool.description}`)
}
// Add agent definitions
if (config.agents && Object.keys(config.agents).length > 0) {
parts.push('\n# Available Subagents\n')
for (const [name, def] of Object.entries(config.agents)) {
parts.push(`- **${name}**: ${def.description}`)
}
}
// System context (git status, etc.)
try {
const sysCtx = await getSystemContext(config.cwd)
if (sysCtx) {
parts.push('\n# Environment\n')
parts.push(sysCtx)
}
} catch {
// Context is best-effort
}
// User context (AGENT.md, date)
try {
const userCtx = await getUserContext(config.cwd)
if (userCtx) {
parts.push('\n# Project Context\n')
parts.push(userCtx)
}
} catch {
// Context is best-effort
}
// Working directory
parts.push(`\n# Working Directory\n${config.cwd}`)
if (config.appendSystemPrompt) {
parts.push('\n' + config.appendSystemPrompt)
}
// Inject the schema for structured output so models that don't support
// OpenAI `response_format` (Anthropic, or OpenAI-compatible servers that
// only accept `{ type: 'json_object' }`) still know what shape to emit.
if (structuredBlock) {
parts.push(structuredBlock)
}
return parts.join('\n')
}
// ============================================================================
// QueryEngine
// ============================================================================
export class QueryEngine {
private config: QueryEngineConfig
private provider: LLMProvider
public messages: NormalizedMessageParam[] = []
private totalUsage: TokenUsage = { input_tokens: 0, output_tokens: 0 }
private totalCost = 0
private turnCount = 0
private compactState: AutoCompactState
private sessionId: string
private apiTimeMs = 0
private hookRegistry?: HookRegistry
constructor(config: QueryEngineConfig) {
this.config = config
this.provider = config.provider
this.compactState = createAutoCompactState()
this.sessionId = config.sessionId || crypto.randomUUID()
this.hookRegistry = config.hookRegistry
}
/**
* Execute hooks for a lifecycle event.
* Returns hook outputs; never throws.
*/
private async executeHooks(
event: import('./hooks.js').HookEvent,
extra?: Partial<HookInput>,
): Promise<HookOutput[]> {
if (!this.hookRegistry?.hasHooks(event)) return []
try {
return await this.hookRegistry.execute(event, {
event,
sessionId: this.sessionId,
cwd: this.config.cwd,
...extra,
})
} catch {
return []
}
}
/**
* Submit a user message and run the agentic loop.
* Yields SDKMessage events as the agent works.
*/
async *submitMessage(
prompt: string | any[],
): AsyncGenerator<SDKMessage> {
// Hook: SessionStart
await this.executeHooks('SessionStart')
// Hook: UserPromptSubmit
const userHookResults = await this.executeHooks('UserPromptSubmit', {
toolInput: prompt,
})
// Check if any hook blocks the submission
if (userHookResults.some((r) => r.block)) {
yield {
type: 'result',
subtype: 'error_during_execution',
is_error: true,
usage: this.totalUsage,
num_turns: 0,
cost: 0,
errors: ['Blocked by UserPromptSubmit hook'],
}
return
}
// Add user message
this.messages.push({ role: 'user', content: prompt as any })
// Build tool definitions for provider
const tools = this.config.tools.map(toProviderTool)
// Build system prompt
const systemPrompt = await buildSystemPrompt(this.config)
// Emit init system message
yield {
type: 'system',
subtype: 'init',
session_id: this.sessionId,
tools: this.config.tools.map(t => t.name),
model: this.config.model,
cwd: this.config.cwd,
mcp_servers: [],
permission_mode: 'bypassPermissions',
} as SDKMessage
// Agentic loop
let turnsRemaining = this.config.maxTurns
let budgetExceeded = false
let maxOutputRecoveryAttempts = 0
let structuredOutput: unknown = undefined
const MAX_OUTPUT_RECOVERY = 3
// Pre-compute the provider-level response_format hint. Only OpenAI-style
// providers will read it; Anthropic ignores it. We always also inject the
// schema into the system prompt for cross-provider compatibility.
const responseFormat = this.config.outputFormat ? { type: 'json_object' as const } : undefined
while (turnsRemaining > 0) {
if (this.config.abortSignal?.aborted) break
// Check budget
if (this.config.maxBudgetUsd && this.totalCost >= this.config.maxBudgetUsd) {
budgetExceeded = true
break
}
// Auto-compact if context is too large
if (shouldAutoCompact(this.messages as any[], this.config.model, this.compactState)) {
await this.executeHooks('PreCompact')
try {
const result = await compactConversation(
this.provider,
this.config.model,
this.messages as any[],
this.compactState,
)
this.messages = result.compactedMessages as NormalizedMessageParam[]
this.compactState = result.state
await this.executeHooks('PostCompact')
} catch {
// Continue with uncompacted messages
}
}
// Micro-compact: truncate large tool results
const apiMessages = microCompactMessages(
normalizeMessagesForAPI(this.messages as any[]),
) as NormalizedMessageParam[]
this.turnCount++
turnsRemaining--
// Make API call with retry via provider
let response: CreateMessageResponse
const apiStart = performance.now()
try {
response = await withRetry(
async () => {
return this.provider.createMessage({
model: this.config.model,
maxTokens: this.config.maxTokens,
system: systemPrompt,
messages: apiMessages,
tools: tools.length > 0 ? tools : undefined,
thinking:
this.config.thinking?.type === 'enabled' &&
this.config.thinking.budgetTokens
? {
type: 'enabled',
budget_tokens: this.config.thinking.budgetTokens,
}
: undefined,
response_format: responseFormat,
})
},
undefined,
this.config.abortSignal,
)
} catch (err: any) {
// Handle prompt-too-long by compacting
if (isPromptTooLongError(err) && !this.compactState.compacted) {
try {
const result = await compactConversation(
this.provider,
this.config.model,
this.messages as any[],
this.compactState,
)
this.messages = result.compactedMessages as NormalizedMessageParam[]
this.compactState = result.state
turnsRemaining++ // Retry this turn
this.turnCount--
continue
} catch {
// Can't compact, give up
}
}
yield {
type: 'result',
subtype: 'error',
usage: this.totalUsage,
num_turns: this.turnCount,
cost: this.totalCost,
}
return
}
// Track API timing
this.apiTimeMs += performance.now() - apiStart
// Track usage (normalized by provider)
if (response.usage) {
this.totalUsage.input_tokens += response.usage.input_tokens
this.totalUsage.output_tokens += response.usage.output_tokens
if (response.usage.cache_creation_input_tokens) {
this.totalUsage.cache_creation_input_tokens =
(this.totalUsage.cache_creation_input_tokens || 0) +
response.usage.cache_creation_input_tokens
}
if (response.usage.cache_read_input_tokens) {
this.totalUsage.cache_read_input_tokens =
(this.totalUsage.cache_read_input_tokens || 0) +
response.usage.cache_read_input_tokens
}
this.totalCost += estimateCost(this.config.model, response.usage)
}
// Add assistant message to conversation
this.messages.push({ role: 'assistant', content: response.content as any })
// Try to extract structured output. We parse on every turn and let the
// last successful parse win, since the final answer is the one the model
// emits after all tool work is done.
if (this.config.outputFormat && response.content.length > 0) {
const textBlock = response.content.find(
(b): b is { type: 'text'; text: string } => b.type === 'text',
)
if (textBlock) {
const parsed = tryParseJson(textBlock.text)
if (parsed !== undefined) {
structuredOutput = parsed
}
}
}
// Yield assistant message
yield {
type: 'assistant',
message: {
role: 'assistant',
content: response.content as any,
},
}
// Handle max_output_tokens recovery
if (
response.stopReason === 'max_tokens' &&
maxOutputRecoveryAttempts < MAX_OUTPUT_RECOVERY
) {
maxOutputRecoveryAttempts++
// Add continuation prompt
this.messages.push({
role: 'user',
content: 'Please continue from where you left off.',
})
continue
}
// Check for tool use
const toolUseBlocks = response.content.filter(
(block): block is ToolUseBlock => block.type === 'tool_use',
)
if (toolUseBlocks.length === 0) {
break // No tool calls - agent is done
}
// Reset max_output recovery counter on successful tool use
maxOutputRecoveryAttempts = 0
// Execute tools (concurrent read-only, serial mutations)
const toolResults = await this.executeTools(toolUseBlocks)
// Yield tool results
for (const result of toolResults) {
yield {
type: 'tool_result',
result: {
tool_use_id: result.tool_use_id,
tool_name: result.tool_name || '',
output:
typeof result.content === 'string'
? result.content
: JSON.stringify(result.content),
},
}
}
// Add tool results to conversation
this.messages.push({
role: 'user',
content: toolResults.map((r) => ({
type: 'tool_result' as const,
tool_use_id: r.tool_use_id,
content:
typeof r.content === 'string'
? r.content
: JSON.stringify(r.content),
is_error: r.is_error,
})),
})
if (response.stopReason === 'end_turn') break
}
// Hook: Stop (end of agentic loop)
await this.executeHooks('Stop')
// Hook: SessionEnd
await this.executeHooks('SessionEnd')
// Yield enriched final result
const endSubtype = budgetExceeded
? 'error_max_budget_usd'
: turnsRemaining <= 0
? 'error_max_turns'
: 'success'
yield {
type: 'result',
subtype: endSubtype,
session_id: this.sessionId,
is_error: endSubtype !== 'success',
num_turns: this.turnCount,
total_cost_usd: this.totalCost,
duration_api_ms: Math.round(this.apiTimeMs),
usage: this.totalUsage,
model_usage: { [this.config.model]: { input_tokens: this.totalUsage.input_tokens, output_tokens: this.totalUsage.output_tokens } },
cost: this.totalCost,
structured_output: structuredOutput,
}
}
/**
* Execute tool calls with concurrency control.
*
* Read-only tools run concurrently (up to 10 at a time).
* Mutation tools run sequentially.
*/
private async executeTools(
toolUseBlocks: ToolUseBlock[],
): Promise<(ToolResult & { tool_name?: string })[]> {
const context: ToolContext = {
cwd: this.config.cwd,
abortSignal: this.config.abortSignal,
provider: this.provider,
model: this.config.model,
apiType: this.provider.apiType,
}
const MAX_CONCURRENCY = parseInt(
process.env.AGENT_SDK_MAX_TOOL_CONCURRENCY || '10',
)
// Partition into read-only (concurrent) and mutation (serial)
const readOnly: Array<{ block: ToolUseBlock; tool?: ToolDefinition }> = []
const mutations: Array<{ block: ToolUseBlock; tool?: ToolDefinition }> = []
for (const block of toolUseBlocks) {
const tool = this.config.tools.find((t) => t.name === block.name)
if (tool?.isReadOnly?.()) {
readOnly.push({ block, tool })
} else {
mutations.push({ block, tool })
}
}
const results: (ToolResult & { tool_name?: string })[] = []
// Execute read-only tools concurrently (batched by MAX_CONCURRENCY)
for (let i = 0; i < readOnly.length; i += MAX_CONCURRENCY) {
const batch = readOnly.slice(i, i + MAX_CONCURRENCY)
const batchResults = await Promise.all(
batch.map((item) =>
this.executeSingleTool(item.block, item.tool, context),
),
)
results.push(...batchResults)
}
// Execute mutation tools sequentially
for (const item of mutations) {
const result = await this.executeSingleTool(item.block, item.tool, context)
results.push(result)
}
return results
}
/**
* Execute a single tool with permission checking.
*/
private async executeSingleTool(
block: ToolUseBlock,
tool: ToolDefinition | undefined,
context: ToolContext,
): Promise<ToolResult & { tool_name?: string }> {
if (!tool) {
return {
type: 'tool_result',
tool_use_id: block.id,
content: `Error: Unknown tool "${block.name}"`,
is_error: true,
tool_name: block.name,
}
}
// Check enabled
if (tool.isEnabled && !tool.isEnabled()) {
return {
type: 'tool_result',
tool_use_id: block.id,
content: `Error: Tool "${block.name}" is not enabled`,
is_error: true,
tool_name: block.name,
}
}
// Check permissions
if (this.config.canUseTool) {
try {
const permission = await this.config.canUseTool(tool, block.input)
if (permission.behavior === 'deny') {
return {
type: 'tool_result',
tool_use_id: block.id,
content: permission.message || `Permission denied for tool "${block.name}"`,
is_error: true,
tool_name: block.name,
}
}
if (permission.updatedInput !== undefined) {
block = { ...block, input: permission.updatedInput }
}
} catch (err: any) {
return {
type: 'tool_result',
tool_use_id: block.id,
content: `Permission check error: ${err.message}`,
is_error: true,
tool_name: block.name,
}
}
}
// Hook: PreToolUse
const preHookResults = await this.executeHooks('PreToolUse', {
toolName: block.name,
toolInput: block.input,
toolUseId: block.id,
})
// Check if any hook blocks this tool
if (preHookResults.some((r) => r.block)) {
const msg = preHookResults.find((r) => r.message)?.message || 'Blocked by PreToolUse hook'
return {
type: 'tool_result',
tool_use_id: block.id,
content: msg,
is_error: true,
tool_name: block.name,
}
}
// Execute the tool
try {
const result = await tool.call(block.input, context)
// Hook: PostToolUse
await this.executeHooks('PostToolUse', {
toolName: block.name,
toolInput: block.input,
toolOutput: typeof result.content === 'string' ? result.content : JSON.stringify(result.content),
toolUseId: block.id,
})
return { ...result, tool_use_id: block.id, tool_name: block.name }
} catch (err: any) {
// Hook: PostToolUseFailure
await this.executeHooks('PostToolUseFailure', {
toolName: block.name,
toolInput: block.input,
toolUseId: block.id,
error: err.message,
})
return {
type: 'tool_result',
tool_use_id: block.id,
content: `Tool execution error: ${err.message}`,
is_error: true,
tool_name: block.name,
}
}
}
/**
* Get current messages for session persistence.
*/
getMessages(): NormalizedMessageParam[] {
return [...this.messages]
}
/**
* Get total usage across all turns.
*/
getUsage(): TokenUsage {
return { ...this.totalUsage }
}
/**
* Get total cost.
*/
getCost(): number {
return this.totalCost
}
}