Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,21 @@ func runNonInteractiveMode(ctx context.Context, mcpAgent *agent.Agent, cli *ui.C
}
}
},

// Tool call content handler - called when content accompanies tool calls
func(content string) {
if !quiet && cli != nil {
// Stop spinner before displaying content
if currentSpinner != nil {
currentSpinner.Stop()
currentSpinner = nil
}
cli.DisplayAssistantMessageWithModel(content, modelName)
// Start spinner again for tool calls
currentSpinner = ui.NewSpinner("Thinking...")
currentSpinner.Start()
}
},
)

// Make sure spinner is stopped if still running
Expand Down Expand Up @@ -344,6 +359,18 @@ func runInteractiveMode(ctx context.Context, mcpAgent *agent.Agent, cli *ui.CLI,
currentSpinner = nil
}
},
// Tool call content handler - called when content accompanies tool calls
func(content string) {
// Stop spinner before displaying content
if currentSpinner != nil {
currentSpinner.Stop()
currentSpinner = nil
}
cli.DisplayAssistantMessageWithModel(content, modelName)
// Start spinner again for tool calls
currentSpinner = ui.NewSpinner("Thinking...")
currentSpinner.Start()
},
)

// Make sure spinner is stopped if still running
Expand Down
10 changes: 9 additions & 1 deletion internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ type ToolResultHandler func(toolName, toolArgs, result string, isError bool)
// ResponseHandler is a function type for handling LLM responses
type ResponseHandler func(content string)

// ToolCallContentHandler is a function type for handling content that accompanies tool calls
type ToolCallContentHandler func(content string)

func firstChunkStreamToolCallChecker(_ context.Context, sr *schema.StreamReader[*schema.Message]) (bool, error) {
defer sr.Close()

Expand Down Expand Up @@ -345,7 +348,7 @@ func (a *Agent) Stream(ctx context.Context, input []*schema.Message, opts ...com

// GenerateWithLoop processes messages with a custom loop that displays tool calls in real-time
func (a *Agent) GenerateWithLoop(ctx context.Context, messages []*schema.Message,
onToolCall ToolCallHandler, onToolResult ToolResultHandler, onResponse ResponseHandler) (*schema.Message, error) {
onToolCall ToolCallHandler, onToolResult ToolResultHandler, onResponse ResponseHandler, onToolCallContent ToolCallContentHandler) (*schema.Message, error) {

// Create a copy of messages to avoid modifying the original
workingMessages := make([]*schema.Message, len(messages))
Expand Down Expand Up @@ -391,6 +394,11 @@ func (a *Agent) GenerateWithLoop(ctx context.Context, messages []*schema.Message

// Check if this is a tool call or final response
if len(response.ToolCalls) > 0 {
// Display any content that accompanies the tool calls
if response.Content != "" && onToolCallContent != nil {
onToolCallContent(response.Content)
}

// Handle tool calls
for _, toolCall := range response.ToolCalls {
// Notify about tool call
Expand Down