@@ -52,6 +52,9 @@ type AgentConfig struct {
5252// ToolCallHandler is a function type for handling tool calls as they happen
5353type ToolCallHandler func (toolName , toolArgs string )
5454
55+ // ToolExecutionHandler is a function type for handling tool execution start/end
56+ type ToolExecutionHandler func (toolName string , isStarting bool )
57+
5558// ToolResultHandler is a function type for handling tool results
5659type ToolResultHandler func (toolName , toolArgs , result string , isError bool )
5760
@@ -348,7 +351,7 @@ func (a *Agent) Stream(ctx context.Context, input []*schema.Message, opts ...com
348351
349352// GenerateWithLoop processes messages with a custom loop that displays tool calls in real-time
350353func (a * Agent ) GenerateWithLoop (ctx context.Context , messages []* schema.Message ,
351- onToolCall ToolCallHandler , onToolResult ToolResultHandler , onResponse ResponseHandler , onToolCallContent ToolCallContentHandler ) (* schema.Message , error ) {
354+ onToolCall ToolCallHandler , onToolExecution ToolExecutionHandler , onToolResult ToolResultHandler , onResponse ResponseHandler , onToolCallContent ToolCallContentHandler ) (* schema.Message , error ) {
352355
353356 // Create a copy of messages to avoid modifying the original
354357 workingMessages := make ([]* schema.Message , len (messages ))
@@ -408,7 +411,18 @@ func (a *Agent) GenerateWithLoop(ctx context.Context, messages []*schema.Message
408411
409412 // Execute the tool
410413 if selectedTool , exists := toolMap [toolCall .Function .Name ]; exists {
414+ // Notify tool execution start
415+ if onToolExecution != nil {
416+ onToolExecution (toolCall .Function .Name , true )
417+ }
418+
411419 output , err := selectedTool .(tool.InvokableTool ).InvokableRun (ctx , toolCall .Function .Arguments )
420+
421+ // Notify tool execution end
422+ if onToolExecution != nil {
423+ onToolExecution (toolCall .Function .Name , false )
424+ }
425+
412426 if err != nil {
413427 errorMsg := fmt .Sprintf ("Tool execution error: %v" , err )
414428 toolMessage := schema .ToolMessage (errorMsg , toolCall .ID )
0 commit comments