@@ -107,8 +107,9 @@ import { ExplanatoryParams, InvokeOutput, ToolApprovalException } from './tools/
107107import { FileSearch , FileSearchParams } from './tools/fileSearch'
108108import { diffLines } from 'diff'
109109import { CodeSearch } from './tools/codeSearch'
110- import { genericErrorMsg } from './constants'
110+ import { genericErrorMsg , maxAgentLoopIterations } from './constants'
111111import { URI } from 'vscode-uri'
112+ import { AgenticChatError } from './errors'
112113
113114type ChatHandlers = Omit <
114115 LspHandlers < Chat > ,
@@ -472,10 +473,9 @@ export class AgenticChatController implements ChatHandlers {
472473 let currentRequestInput = { ...initialRequestInput }
473474 let finalResult : Result < AgenticChatResultWithMetadata , string > | null = null
474475 let iterationCount = 0
475- const maxIterations = 100 // Safety limit to prevent infinite loops
476476 metric . recordStart ( )
477477
478- while ( iterationCount < maxIterations ) {
478+ while ( iterationCount < maxAgentLoopIterations ) {
479479 iterationCount ++
480480 this . #debug( `Agent loop iteration ${ iterationCount } for conversation id:` , conversationIdentifier || '' )
481481
@@ -579,8 +579,8 @@ export class AgenticChatController implements ChatHandlers {
579579 currentRequestInput = this . #updateRequestInputWithToolResults( currentRequestInput , toolResults , content )
580580 }
581581
582- if ( iterationCount >= maxIterations ) {
583- this . #log ( 'Agent loop reached maximum iterations limit' )
582+ if ( iterationCount >= maxAgentLoopIterations ) {
583+ throw new AgenticChatError ( 'Agent loop reached iteration limit' , 'MaxAgentLoopIterations ')
584584 }
585585
586586 this . #stoppedToolUses. clear ( )
@@ -1312,9 +1312,9 @@ export class AgenticChatController implements ChatHandlers {
13121312 triggerContext : TriggerContext ,
13131313 isNewConversation : boolean ,
13141314 chatResultStream : AgenticChatResultStream
1315- ) : Promise < ChatResult | ResponseError < ChatResult > > {
1315+ ) : Promise < ChatResult > {
13161316 if ( ! result . success ) {
1317- return new ResponseError < ChatResult > ( LSPErrorCodes . RequestFailed , result . error )
1317+ throw new AgenticChatError ( result . error , 'FailedResult' )
13181318 }
13191319 const conversationId = session . conversationId
13201320 this . #debug( 'Final session conversation id:' , conversationId || '' )
@@ -1402,9 +1402,9 @@ export class AgenticChatController implements ChatHandlers {
14021402 return createAuthFollowUpResult ( authFollowType )
14031403 }
14041404
1405- // Show backend error messages to the customer .
1406- if ( err . code === 'QModelResponse' ) {
1407- this . #features. logging . error ( `QModelResponse Error : ${ JSON . stringify ( err . cause ) } ` )
1405+ // These are errors we want to show custom messages in chat for .
1406+ if ( err . code === 'QModelResponse' || err . code === 'MaxAgentLoopIterations' ) {
1407+ this . #features. logging . error ( `${ err . code } : ${ JSON . stringify ( err . cause ) } ` )
14081408 return new ResponseError < ChatResult > ( LSPErrorCodes . RequestFailed , err . message , {
14091409 type : 'answer' ,
14101410 body : err . message ,
0 commit comments