Skip to content

Commit 6c4bcce

Browse files
committed
show spinner when running tool calls
1 parent a88a2fc commit 6c4bcce

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

cmd/root.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,22 @@ func runNonInteractiveMode(ctx context.Context, mcpAgent *agent.Agent, cli *ui.C
253253
cli.DisplayToolCallMessage(toolName, toolArgs)
254254
}
255255
},
256+
// Tool execution handler - called when tool execution starts/ends
257+
func(toolName string, isStarting bool) {
258+
if !quiet && cli != nil {
259+
if isStarting {
260+
// Start spinner for tool execution
261+
currentSpinner = ui.NewSpinner(fmt.Sprintf("Executing %s...", toolName))
262+
currentSpinner.Start()
263+
} else {
264+
// Stop spinner when tool execution completes
265+
if currentSpinner != nil {
266+
currentSpinner.Stop()
267+
currentSpinner = nil
268+
}
269+
}
270+
}
271+
},
256272
// Tool result handler - called when a tool execution completes
257273
func(toolName, toolArgs, result string, isError bool) {
258274
if !quiet && cli != nil {
@@ -372,6 +388,20 @@ func runInteractiveMode(ctx context.Context, mcpAgent *agent.Agent, cli *ui.CLI,
372388
}
373389
cli.DisplayToolCallMessage(toolName, toolArgs)
374390
},
391+
// Tool execution handler - called when tool execution starts/ends
392+
func(toolName string, isStarting bool) {
393+
if isStarting {
394+
// Start spinner for tool execution
395+
currentSpinner = ui.NewSpinner(fmt.Sprintf("Executing %s...", toolName))
396+
currentSpinner.Start()
397+
} else {
398+
// Stop spinner when tool execution completes
399+
if currentSpinner != nil {
400+
currentSpinner.Stop()
401+
currentSpinner = nil
402+
}
403+
}
404+
},
375405
// Tool result handler - called when a tool execution completes
376406
func(toolName, toolArgs, result string, isError bool) {
377407
cli.DisplayToolMessage(toolName, toolArgs, result, isError)

internal/agent/agent.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ type AgentConfig struct {
5252
// ToolCallHandler is a function type for handling tool calls as they happen
5353
type 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
5659
type 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
350353
func (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

Comments
 (0)