@@ -14,7 +14,35 @@ import (
1414 "github.com/cnjack/jcode/internal/toolpolicy"
1515)
1616
17- const maxIterations = 1000
17+ const defaultMaxIterations = 1000
18+
19+ type agentOptions struct {
20+ maxIterations int
21+ }
22+
23+ // AgentOption configures one ChatModelAgent without changing the defaults used by
24+ // existing transports.
25+ type AgentOption func (* agentOptions )
26+
27+ // WithMaxIterations bounds the model/tool loop for the constructed agent.
28+ // Non-positive values retain the default for backward compatibility.
29+ func WithMaxIterations (limit int ) AgentOption {
30+ return func (options * agentOptions ) {
31+ if limit > 0 {
32+ options .maxIterations = limit
33+ }
34+ }
35+ }
36+
37+ func resolveAgentOptions (options ... AgentOption ) agentOptions {
38+ resolved := agentOptions {maxIterations : defaultMaxIterations }
39+ for _ , apply := range options {
40+ if apply != nil {
41+ apply (& resolved )
42+ }
43+ }
44+ return resolved
45+ }
1846
1947type ApprovalFunc func (ctx context.Context , toolName , toolArgs string ) (bool , error )
2048
@@ -47,10 +75,12 @@ func NewAgent(
4775 approvalFunc ApprovalFunc ,
4876 middlewares []adk.ChatModelAgentMiddleware ,
4977 handlers []adk.ChatModelAgentMiddleware ,
78+ options ... AgentOption ,
5079) (* adk.ChatModelAgent , error ) {
5180 return newAgent (
5281 ctx , chatmodel , tools , nil , toolDisclosureGroups {},
5382 instruction , approvalFunc , middlewares , handlers ,
83+ options ... ,
5484 )
5585}
5686
@@ -64,7 +94,9 @@ func newAgent(
6494 approvalFunc ApprovalFunc ,
6595 middlewares []adk.ChatModelAgentMiddleware ,
6696 handlers []adk.ChatModelAgentMiddleware ,
97+ options ... AgentOption ,
6798) (* adk.ChatModelAgent , error ) {
99+ resolvedOptions := resolveAgentOptions (options ... )
68100 // Handler order is outermost → innermost: tracing middlewares first, then the
69101 // tool-search middleware, then the caller's state-rewriting handlers, and
70102 // finally the hook + approval + safe-tool-error stack. Tool search must inspect
@@ -138,7 +170,7 @@ func newAgent(
138170 Tools : append ([]tool.BaseTool (nil ), directTools ... ),
139171 },
140172 },
141- MaxIterations : maxIterations ,
173+ MaxIterations : resolvedOptions . maxIterations ,
142174 Handlers : enhanced ,
143175 ModelRetryConfig : & adk.ModelRetryConfig {
144176 MaxRetries : 5 ,
0 commit comments