@@ -19,7 +19,7 @@ import (
1919// ErrUnknownTool is returned when a tool call references a tool that is not registered.
2020var ErrUnknownTool = errors .New ("unknown tool" )
2121
22- const maxAgentIterations = 20
22+ const defaultMaxAgentIterations = 20
2323
2424// ToolCall represents a tool invocation.
2525type ToolCall struct {
@@ -74,6 +74,9 @@ type Agent struct {
7474 Skills []Skill
7575 Messages []Message
7676
77+ // MaxIterations overrides the default iteration limit when set to > 0.
78+ MaxIterations int
79+
7780 // Context compaction config.
7881 contextConfig ContextConfig
7982
@@ -361,7 +364,12 @@ func (a *Agent) Run(ctx context.Context, systemPrompt, userMessage string, emitF
361364
362365 opts := a .completionOpts ()
363366
364- for i := 0 ; i < maxAgentIterations ; i ++ {
367+ maxIter := defaultMaxAgentIterations
368+ if a .MaxIterations > 0 {
369+ maxIter = a .MaxIterations
370+ }
371+
372+ for i := 0 ; i < maxIter ; i ++ {
365373 a .logger .Info ("agent iteration" , "step" , i + 1 )
366374 emit (Event {Type : string (EventTurnStart ), Content : fmt .Sprintf ("turn %d" , i + 1 )})
367375
@@ -376,17 +384,11 @@ func (a *Agent) Run(ctx context.Context, systemPrompt, userMessage string, emitF
376384 response string
377385 err error
378386 )
379- if ts , ok := a .provider .(TokenStreamer ); ok {
380- response , err = RetryWithResult (ctx , DefaultRetryConfig , func () (string , error ) {
381- return ts .CompleteStream (ctx , messages , opts , func (token string ) {
382- emit (Event {Type : string (EventTokenUpdate ), Content : token })
383- })
387+ response , err = RetryWithResult (ctx , DefaultRetryConfig , func () (string , error ) {
388+ return a .provider .CompleteStream (ctx , messages , opts , func (token string ) {
389+ emit (Event {Type : string (EventTokenUpdate ), Content : token })
384390 })
385- } else {
386- response , err = RetryWithResult (ctx , DefaultRetryConfig , func () (string , error ) {
387- return a .provider .Complete (ctx , messages , opts )
388- })
389- }
391+ })
390392 if err != nil {
391393 emit (Event {Type : string (EventError ), Content : err .Error (), IsError : true })
392394 return "" , fmt .Errorf ("provider error at step %d: %w" , i + 1 , err )
@@ -419,7 +421,7 @@ func (a *Agent) Run(ctx context.Context, systemPrompt, userMessage string, emitF
419421 emit (Event {Type : string (EventTurnEnd ), Content : "" })
420422 }
421423
422- return "" , fmt .Errorf ("agent exceeded max iterations (%d)" , maxAgentIterations )
424+ return "" , fmt .Errorf ("agent exceeded max iterations (%d)" , maxIter )
423425}
424426
425427func (a * Agent ) emit (e Event ) {
@@ -846,7 +848,12 @@ func (a *Agent) PromptMessages(ctx context.Context, messages []Message) chan Eve
846848
847849 opts := a .completionOpts ()
848850
849- for i := 0 ; i < maxAgentIterations ; i ++ {
851+ maxIter := defaultMaxAgentIterations
852+ if a .MaxIterations > 0 {
853+ maxIter = a .MaxIterations
854+ }
855+
856+ for i := 0 ; i < maxIter ; i ++ {
850857 // Check for cancellation before each turn.
851858 select {
852859 case <- loopCtx .Done ():
@@ -868,17 +875,11 @@ func (a *Agent) PromptMessages(ctx context.Context, messages []Message) chan Eve
868875 response string
869876 turnErr error
870877 )
871- if ts , ok := a .provider .(TokenStreamer ); ok {
872- response , turnErr = RetryWithResult (loopCtx , DefaultRetryConfig , func () (string , error ) {
873- return ts .CompleteStream (loopCtx , fullMessages , opts , func (token string ) {
874- emitFn (Event {Type : string (EventTokenUpdate ), Content : token })
875- })
878+ response , turnErr = RetryWithResult (loopCtx , DefaultRetryConfig , func () (string , error ) {
879+ return a .provider .CompleteStream (loopCtx , fullMessages , opts , func (token string ) {
880+ emitFn (Event {Type : string (EventTokenUpdate ), Content : token })
876881 })
877- } else {
878- response , turnErr = RetryWithResult (loopCtx , DefaultRetryConfig , func () (string , error ) {
879- return a .provider .Complete (loopCtx , fullMessages , opts )
880- })
881- }
882+ })
882883 if turnErr != nil {
883884 emitFn (Event {Type : string (EventError ), Content : turnErr .Error (), IsError : true })
884885 break
0 commit comments