1- import { AgentResponseTrace , insertTrace } from '@codebuff/bigquery'
1+ import { insertTrace } from '@codebuff/bigquery'
22import { trackEvent } from '@codebuff/common/analytics'
33import {
44 ASYNC_AGENTS_ENABLED ,
@@ -9,17 +9,9 @@ import {
99 getToolCallString ,
1010 renderToolResults ,
1111} from '@codebuff/common/tools/utils'
12- import { CodebuffMessage } from '@codebuff/common/types/message'
13- import { PrintModeObject } from '@codebuff/common/types/print-mode'
14- import {
15- AgentState ,
16- ToolResult ,
17- type AgentTemplateType ,
18- } from '@codebuff/common/types/session-state'
1912import { buildArray } from '@codebuff/common/util/array'
20- import { ProjectFileContext } from '@codebuff/common/util/file'
2113import { generateCompactId } from '@codebuff/common/util/string'
22- import { WebSocket } from 'ws'
14+
2315import { asyncAgentManager } from './async-agent-manager'
2416import { getFileReadingUpdates } from './get-file-reading-updates'
2517import { checkLiveUserInput } from './live-user-inputs'
@@ -28,7 +20,6 @@ import { runProgrammaticStep } from './run-programmatic-step'
2820import { additionalSystemPrompts } from './system-prompt/prompts'
2921import { saveAgentRequest } from './system-prompt/save-agent-request'
3022import { agentTemplates } from './templates/agent-list'
31- import { AgentRegistry } from './templates/agent-registry'
3223import { getAgentPrompt } from './templates/strings'
3324import { processStreamWithTools } from './tools/stream-parser'
3425import { logger } from './util/logger'
@@ -46,6 +37,18 @@ import { simplifyReadFileResults } from './util/simplify-tool-results'
4637import { countTokensJson } from './util/token-counter'
4738import { getRequestContext } from './websockets/request-context'
4839
40+ import type { AgentRegistry } from './templates/agent-registry'
41+ import type { AgentResponseTrace } from '@codebuff/bigquery'
42+ import type { CodebuffMessage } from '@codebuff/common/types/message'
43+ import type { PrintModeObject } from '@codebuff/common/types/print-mode'
44+ import type {
45+ AgentTemplateType ,
46+ AgentState ,
47+ ToolResult ,
48+ } from '@codebuff/common/types/session-state'
49+ import type { ProjectFileContext } from '@codebuff/common/util/file'
50+ import type { WebSocket } from 'ws'
51+
4952export interface AgentOptions {
5053 userId : string | undefined
5154 userInputId : string
@@ -64,7 +67,7 @@ export interface AgentOptions {
6467
6568export const runAgentStep = async (
6669 ws : WebSocket ,
67- options : AgentOptions
70+ options : AgentOptions ,
6871) : Promise < {
6972 agentState : AgentState
7073 fullResponse : string
@@ -111,13 +114,13 @@ export const runAgentStep = async (
111114 role : 'user' as const ,
112115 content : asUserMessage ( prompt ) ,
113116 } ,
114- ]
117+ ] ,
115118 )
116119
117120 // Check number of assistant messages since last user message with prompt
118121 if ( agentState . stepsRemaining <= 0 ) {
119122 logger . warn (
120- `Detected too many consecutive assistant messages without user prompt`
123+ `Detected too many consecutive assistant messages without user prompt` ,
121124 )
122125
123126 const warningString = [
@@ -136,7 +139,7 @@ export const runAgentStep = async (
136139 {
137140 role : 'user' ,
138141 content : asSystemMessage (
139- `The assistant has responded too many times in a row. The assistant's turn has automatically been ended. The number of responses can be changed in codebuff.json.`
142+ `The assistant has responded too many times in a row. The assistant's turn has automatically been ended. The number of responses can be changed in codebuff.json.` ,
140143 ) ,
141144 } ,
142145 ] ,
@@ -174,7 +177,7 @@ export const runAgentStep = async (
174177 const toolResults : ToolResult [ ] = [ ]
175178
176179 const updatedFiles = addedFiles . filter ( ( f ) =>
177- updatedFilePaths . includes ( f . path )
180+ updatedFilePaths . includes ( f . path ) ,
178181 )
179182
180183 if ( updatedFiles . length > 0 ) {
@@ -211,7 +214,7 @@ export const runAgentStep = async (
211214
212215 // Check for pending messages from other agents
213216 const pendingMessages = asyncAgentManager . getAndClearMessages (
214- agentState . agentId
217+ agentState . agentId ,
215218 )
216219 for ( const message of pendingMessages ) {
217220 toolResults . push ( {
@@ -225,7 +228,7 @@ export const runAgentStep = async (
225228 const agentTemplate = agentRegistry [ agentType ]
226229 if ( ! agentTemplate ) {
227230 throw new Error (
228- `Agent template not found for type: ${ agentType } . Available types: ${ Object . keys ( agentTemplates ) . join ( ', ' ) } `
231+ `Agent template not found for type: ${ agentType } . Available types: ${ Object . keys ( agentTemplates ) . join ( ', ' ) } ` ,
229232 )
230233 }
231234
@@ -234,7 +237,7 @@ export const runAgentStep = async (
234237 { type : 'stepPrompt' } ,
235238 fileContext ,
236239 agentState ,
237- agentRegistry
240+ agentRegistry ,
238241 )
239242
240243 // Extract instructions prompt to match hasPrompt && {...} pattern
@@ -244,7 +247,7 @@ export const runAgentStep = async (
244247 { type : 'instructionsPrompt' } ,
245248 fileContext ,
246249 agentState ,
247- agentRegistry
250+ agentRegistry ,
248251 )
249252 : undefined
250253
@@ -261,7 +264,7 @@ export const runAgentStep = async (
261264 // Actual user prompt!
262265 role : 'user' as const ,
263266 content : asUserMessage (
264- `${ prompt ?? '' } ${ params ? `\n\n${ JSON . stringify ( params , null , 2 ) } ` : '' } `
267+ `${ prompt ?? '' } ${ params ? `\n\n${ JSON . stringify ( params , null , 2 ) } ` : '' } ` ,
265268 ) ,
266269 } ,
267270 prompt &&
@@ -270,7 +273,7 @@ export const runAgentStep = async (
270273 content : asSystemInstruction (
271274 additionalSystemPrompts [
272275 prompt as keyof typeof additionalSystemPrompts
273- ]
276+ ] ,
274277 ) ,
275278 } ,
276279 ] ,
@@ -285,7 +288,7 @@ export const runAgentStep = async (
285288 role : 'user' as const ,
286289 content : stepPrompt ,
287290 timeToLive : 'agentStep' as const ,
288- }
291+ } ,
289292 )
290293
291294 agentState . messageHistory = agentMessagesUntruncated
@@ -298,7 +301,7 @@ export const runAgentStep = async (
298301 ...options ,
299302 ws,
300303 template : agentTemplate ,
301- }
304+ } ,
302305 )
303306 agentState = newAgentState
304307 if ( endTurn ) {
@@ -328,23 +331,23 @@ export const runAgentStep = async (
328331 { type : 'systemPrompt' } ,
329332 fileContext ,
330333 agentState ,
331- agentRegistry
334+ agentRegistry ,
332335 ) ) ?? ''
333336 const systemTokens = countTokensJson ( system )
334337
335338 // Possibly truncated messagesWithUserMessage + cache.
336339 const agentMessages = getCoreMessagesSubset (
337340 agentState . messageHistory ,
338341 systemTokens ,
339- supportsCacheControl ( agentTemplate . model )
342+ supportsCacheControl ( agentTemplate . model ) ,
340343 )
341344
342345 const debugPromptCaching = false
343346 if ( debugPromptCaching ) {
344347 // Store the agent request to a file for debugging
345348 await saveAgentRequest (
346349 coreMessagesWithSystem ( agentMessages , system ) ,
347- userInputId
350+ userInputId ,
348351 )
349352 }
350353
@@ -363,10 +366,11 @@ export const runAgentStep = async (
363366 agentTemplate,
364367 duration : Date . now ( ) - startTime ,
365368 } ,
366- `Start agent ${ agentType } step ${ iterationNum } (${ userInputId } ${ prompt ? ` - Prompt: ${ prompt . slice ( 0 , 20 ) } ` : '' } )`
369+ `Start agent ${ agentType } step ${ iterationNum } (${ userInputId } ${ prompt ? ` - Prompt: ${ prompt . slice ( 0 , 20 ) } ` : '' } )` ,
367370 )
368371
369372 let fullResponse = ''
373+ toolResults . length = 0
370374
371375 const stream = getStream ( coreMessagesWithSystem ( agentMessages , system ) )
372376
@@ -419,7 +423,7 @@ export const runAgentStep = async (
419423
420424 let finalMessageHistoryWithToolResults = expireMessages (
421425 state . messages ,
422- 'agentStep'
426+ 'agentStep' ,
423427 )
424428
425429 // Handle /compact command: replace message history with the summary
@@ -431,7 +435,7 @@ export const runAgentStep = async (
431435 {
432436 role : 'user' ,
433437 content : asSystemMessage (
434- `The following is a summary of the conversation between you and the user. The conversation continues after this summary:\n\n${ fullResponse } `
438+ `The following is a summary of the conversation between you and the user. The conversation continues after this summary:\n\n${ fullResponse } ` ,
435439 ) ,
436440 } ,
437441 ]
@@ -453,7 +457,7 @@ export const runAgentStep = async (
453457 agentTemplate,
454458 duration : Date . now ( ) - startTime ,
455459 } ,
456- `End agent ${ agentType } step ${ iterationNum } (${ userInputId } ${ prompt ? ` - Prompt: ${ prompt . slice ( 0 , 20 ) } ` : '' } )`
460+ `End agent ${ agentType } step ${ iterationNum } (${ userInputId } ${ prompt ? ` - Prompt: ${ prompt . slice ( 0 , 20 ) } ` : '' } )` ,
457461 )
458462 const shouldEndTurn =
459463 toolCalls . some ( ( call ) => call . toolName === 'end_turn' ) ||
@@ -506,7 +510,7 @@ export const loopAgentSteps = async (
506510 userId : string | undefined
507511 clientSessionId : string
508512 onResponseChunk : ( chunk : string | PrintModeObject ) => void
509- }
513+ } ,
510514) => {
511515 const agentTemplate = agentRegistry [ agentType ]
512516 if ( ! agentTemplate ) {
@@ -545,7 +549,7 @@ export const loopAgentSteps = async (
545549
546550 if ( shouldEndTurn ) {
547551 const hasEndTurn = fullResponse . includes (
548- getToolCallString ( 'end_turn' , { } )
552+ getToolCallString ( 'end_turn' , { } ) ,
549553 )
550554 return {
551555 agentState : newAgentState ,
0 commit comments