Skip to content

Commit 4599916

Browse files
authored
Fix tool call content messages (#63)
* Implement non-interactive mode * fix
1 parent 8f8169a commit 4599916

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

cmd/root.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,21 @@ func runNonInteractiveMode(ctx context.Context, mcpAgent *agent.Agent, cli *ui.C
244244
}
245245
}
246246
},
247+
248+
// Tool call content handler - called when content accompanies tool calls
249+
func(content string) {
250+
if !quiet && cli != nil {
251+
// Stop spinner before displaying content
252+
if currentSpinner != nil {
253+
currentSpinner.Stop()
254+
currentSpinner = nil
255+
}
256+
cli.DisplayAssistantMessageWithModel(content, modelName)
257+
// Start spinner again for tool calls
258+
currentSpinner = ui.NewSpinner("Thinking...")
259+
currentSpinner.Start()
260+
}
261+
},
247262
)
248263

249264
// Make sure spinner is stopped if still running
@@ -344,6 +359,18 @@ func runInteractiveMode(ctx context.Context, mcpAgent *agent.Agent, cli *ui.CLI,
344359
currentSpinner = nil
345360
}
346361
},
362+
// Tool call content handler - called when content accompanies tool calls
363+
func(content string) {
364+
// Stop spinner before displaying content
365+
if currentSpinner != nil {
366+
currentSpinner.Stop()
367+
currentSpinner = nil
368+
}
369+
cli.DisplayAssistantMessageWithModel(content, modelName)
370+
// Start spinner again for tool calls
371+
currentSpinner = ui.NewSpinner("Thinking...")
372+
currentSpinner.Start()
373+
},
347374
)
348375

349376
// Make sure spinner is stopped if still running

internal/agent/agent.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ type ToolResultHandler func(toolName, toolArgs, result string, isError bool)
5858
// ResponseHandler is a function type for handling LLM responses
5959
type ResponseHandler func(content string)
6060

61+
// ToolCallContentHandler is a function type for handling content that accompanies tool calls
62+
type ToolCallContentHandler func(content string)
63+
6164
func firstChunkStreamToolCallChecker(_ context.Context, sr *schema.StreamReader[*schema.Message]) (bool, error) {
6265
defer sr.Close()
6366

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

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

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

392395
// Check if this is a tool call or final response
393396
if len(response.ToolCalls) > 0 {
397+
// Display any content that accompanies the tool calls
398+
if response.Content != "" && onToolCallContent != nil {
399+
onToolCallContent(response.Content)
400+
}
401+
394402
// Handle tool calls
395403
for _, toolCall := range response.ToolCalls {
396404
// Notify about tool call

0 commit comments

Comments
 (0)