@@ -16,35 +16,49 @@ import (
1616 "time"
1717)
1818
19- // AgentConfig is the config for agent.
19+ // AgentConfig holds configuration options for creating a new Agent.
20+ // It includes model configuration, MCP settings, and various behavioral options.
2021type AgentConfig struct {
21- ModelConfig * models.ProviderConfig
22- MCPConfig * config.Config
23- SystemPrompt string
24- MaxSteps int
22+ // ModelConfig specifies the LLM provider and model to use
23+ ModelConfig * models.ProviderConfig
24+ // MCPConfig contains MCP server configurations
25+ MCPConfig * config.Config
26+ // SystemPrompt is the initial system message for the agent
27+ SystemPrompt string
28+ // MaxSteps limits the number of tool calls (0 for unlimited)
29+ MaxSteps int
30+ // StreamingEnabled controls whether responses are streamed
2531 StreamingEnabled bool
26- DebugLogger tools.DebugLogger // Optional debug logger
32+ // DebugLogger is an optional logger for debugging MCP communications
33+ DebugLogger tools.DebugLogger // Optional debug logger
2734}
2835
29- // ToolCallHandler is a function type for handling tool calls as they happen
36+ // ToolCallHandler is a function type for handling tool calls as they happen.
37+ // It receives the tool name and its arguments when a tool is about to be invoked.
3038type ToolCallHandler func (toolName , toolArgs string )
3139
32- // ToolExecutionHandler is a function type for handling tool execution start/end
40+ // ToolExecutionHandler is a function type for handling tool execution start/end events.
41+ // The isStarting parameter indicates whether the tool is starting (true) or finished (false).
3342type ToolExecutionHandler func (toolName string , isStarting bool )
3443
35- // ToolResultHandler is a function type for handling tool results
44+ // ToolResultHandler is a function type for handling tool results.
45+ // It receives the tool name, arguments, result, and whether the result is an error.
3646type ToolResultHandler func (toolName , toolArgs , result string , isError bool )
3747
38- // ResponseHandler is a function type for handling LLM responses
48+ // ResponseHandler is a function type for handling LLM responses.
49+ // It receives the complete response content from the model.
3950type ResponseHandler func (content string )
4051
41- // StreamingResponseHandler is a function type for handling streaming LLM responses
52+ // StreamingResponseHandler is a function type for handling streaming LLM responses.
53+ // It receives content chunks as they are streamed from the model.
4254type StreamingResponseHandler func (content string )
4355
44- // ToolCallContentHandler is a function type for handling content that accompanies tool calls
56+ // ToolCallContentHandler is a function type for handling content that accompanies tool calls.
57+ // It receives any text content that the model generates alongside tool calls.
4558type ToolCallContentHandler func (content string )
4659
47- // Agent is the agent with real-time tool call display.
60+ // Agent represents an AI agent with MCP tool integration and real-time tool call display.
61+ // It manages the interaction between an LLM and various tools through the MCP protocol.
4862type Agent struct {
4963 toolManager * tools.MCPToolManager
5064 model model.ToolCallingChatModel
@@ -55,7 +69,10 @@ type Agent struct {
5569 streamingEnabled bool // Whether streaming is enabled
5670}
5771
58- // NewAgent creates an agent with MCP tool integration and real-time tool call display
72+ // NewAgent creates a new Agent with MCP tool integration and streaming support.
73+ // It initializes the LLM provider, loads MCP tools, and configures the agent
74+ // based on the provided configuration. Returns an error if provider creation
75+ // or tool loading fails.
5976func NewAgent (ctx context.Context , config * AgentConfig ) (* Agent , error ) {
6077 // Create the LLM provider
6178 providerResult , err := models .CreateProvider (ctx , config .ModelConfig )
@@ -98,20 +115,27 @@ func NewAgent(ctx context.Context, config *AgentConfig) (*Agent, error) {
98115 }, nil
99116}
100117
101- // GenerateWithLoopResult contains the result and conversation history
118+ // GenerateWithLoopResult contains the result and conversation history from an agent interaction.
119+ // It includes both the final response and the complete message history with tool interactions.
102120type GenerateWithLoopResult struct {
103- FinalResponse * schema.Message
121+ // FinalResponse is the last message generated by the model
122+ FinalResponse * schema.Message
123+ // ConversationMessages contains all messages in the conversation including tool calls and results
104124 ConversationMessages []* schema.Message // All messages in the conversation (including tool calls and results)
105125}
106126
107- // GenerateWithLoop processes messages with a custom loop that displays tool calls in real-time
127+ // GenerateWithLoop processes messages with a custom loop that displays tool calls in real-time.
128+ // It handles the conversation flow, executing tools as needed and invoking callbacks for various events.
129+ // This method does not support streaming responses; use GenerateWithLoopAndStreaming for streaming support.
108130func (a * Agent ) GenerateWithLoop (ctx context.Context , messages []* schema.Message ,
109131 onToolCall ToolCallHandler , onToolExecution ToolExecutionHandler , onToolResult ToolResultHandler , onResponse ResponseHandler , onToolCallContent ToolCallContentHandler ) (* GenerateWithLoopResult , error ) {
110132
111133 return a .GenerateWithLoopAndStreaming (ctx , messages , onToolCall , onToolExecution , onToolResult , onResponse , onToolCallContent , nil )
112134}
113135
114- // GenerateWithLoopAndStreaming processes messages with a custom loop that displays tool calls in real-time and supports streaming callbacks
136+ // GenerateWithLoopAndStreaming processes messages with a custom loop that displays tool calls in real-time and supports streaming callbacks.
137+ // It handles the conversation flow, executing tools as needed and invoking callbacks for various events including streaming chunks.
138+ // The onStreamingResponse callback is invoked for each content chunk during streaming if streaming is enabled.
115139func (a * Agent ) GenerateWithLoopAndStreaming (ctx context.Context , messages []* schema.Message ,
116140 onToolCall ToolCallHandler , onToolExecution ToolExecutionHandler , onToolResult ToolResultHandler , onResponse ResponseHandler , onToolCallContent ToolCallContentHandler , onStreamingResponse StreamingResponseHandler ) (* GenerateWithLoopResult , error ) {
117141
@@ -256,17 +280,20 @@ func (a *Agent) GenerateWithLoopAndStreaming(ctx context.Context, messages []*sc
256280 }, nil
257281}
258282
259- // GetTools returns the list of available tools
283+ // GetTools returns the list of available tools loaded in the agent.
284+ // These tools are available for the model to use during interactions.
260285func (a * Agent ) GetTools () []tool.BaseTool {
261286 return a .toolManager .GetTools ()
262287}
263288
264- // GetLoadingMessage returns the loading message from provider creation (e.g., GPU fallback info)
289+ // GetLoadingMessage returns the loading message from provider creation.
290+ // This may contain information about GPU fallback or other provider-specific initialization details.
265291func (a * Agent ) GetLoadingMessage () string {
266292 return a .loadingMessage
267293}
268294
269- // GetLoadedServerNames returns the names of successfully loaded MCP servers
295+ // GetLoadedServerNames returns the names of successfully loaded MCP servers.
296+ // This includes both builtin servers and external MCP server configurations.
270297func (a * Agent ) GetLoadedServerNames () []string {
271298 return a .toolManager .GetLoadedServerNames ()
272299}
@@ -486,7 +513,8 @@ func (a *Agent) listenForESC(stopChan chan bool, readyChan chan bool) bool {
486513 }
487514}
488515
489- // Close closes the agent and cleans up resources
516+ // Close closes the agent and cleans up resources.
517+ // It ensures all MCP connections are properly closed and resources are released.
490518func (a * Agent ) Close () error {
491519 return a .toolManager .Close ()
492520}
0 commit comments