From 49fe3798d103098e7edbb07ff36b1f0988e78979 Mon Sep 17 00:00:00 2001 From: Laura Date: Fri, 12 Sep 2025 15:11:10 +0200 Subject: [PATCH] tool call fixes --- chat.go | 23 +++++++++++++++++++---- internal/tools.txt | 3 ++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/chat.go b/chat.go index 177be39..eac1278 100644 --- a/chat.go +++ b/chat.go @@ -364,8 +364,10 @@ func RunCompletion(ctx context.Context, response *Stream, request *openrouter.Ch defer stream.Close() var ( - id string - tool *ToolCall + id string + open int + close int + tool *ToolCall ) buf := GetFreeBuffer() @@ -405,12 +407,25 @@ func RunCompletion(ctx context.Context, response *Stream, request *openrouter.Ch if len(calls) > 0 { call := calls[0] + if open > 0 && open == close { + continue + } + if tool == nil { tool = &ToolCall{} } - tool.ID += call.ID - tool.Name += call.Function.Name + if call.ID != "" && !strings.HasSuffix(tool.ID, call.ID) { + tool.ID += call.ID + } + + if call.Function.Name != "" && !strings.HasSuffix(tool.Name, call.Function.Name) { + tool.Name += call.Function.Name + } + + open += strings.Count(call.Function.Arguments, "{") + close += strings.Count(call.Function.Arguments, "}") + tool.Args += call.Function.Arguments } else if tool != nil { break diff --git a/internal/tools.txt b/internal/tools.txt index cef20d9..b488c2d 100644 --- a/internal/tools.txt +++ b/internal/tools.txt @@ -18,8 +18,9 @@ Use at most 1 tool call per turn. You have %d turns with tool calls total. Choos - ❌ "detailed analysis of current renewable energy pricing trends and cost comparisons across different technologies" **fetch_contents({urls})** -- Get full page content from 1-5 specific URLs via Exa's contents endpoint +- Get full page content from multiple specific URLs via Exa's contents endpoint - Use when you have exact URLs and need complete text, code, or detailed information +- `urls` is a []string of URLs to return the content of - Provides cleaned, readable content from web pages - Good for: documentation pages, articles, blog posts you found in search results