From 32be271aa3c14c73d5a1d9d8d03432f836594401 Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Sat, 28 Feb 2026 15:40:52 +0300 Subject: Feat (tools): file_edit --- bot.go | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'bot.go') diff --git a/bot.go b/bot.go index fb0e9bf..d35b4dd 100644 --- a/bot.go +++ b/bot.go @@ -66,6 +66,8 @@ var ( LocalModels = []string{} ) +var thinkBlockRE = regexp.MustCompile(`(?s).*?`) + // parseKnownToTag extracts known_to list from content using configured tag. // Returns cleaned content and list of character names. func parseKnownToTag(content string) []string { @@ -933,7 +935,9 @@ out: if err := updateStorageChat(activeChatName, chatBody.Messages); err != nil { logger.Warn("failed to update storage", "error", err, "name", activeChatName) } - if findCall(respText.String(), toolResp.String()) { + // Strip think blocks before parsing for tool calls + respTextNoThink := thinkBlockRE.ReplaceAllString(respText.String(), "") + if findCall(respTextNoThink, toolResp.String()) { return nil } // Check if this message was sent privately to specific characters @@ -1077,11 +1081,30 @@ func findCall(msg, toolCall string) bool { if jsStr == "" { // no tool call case return false } - prefix := "__tool_call__\n" - suffix := "\n__tool_call__" - jsStr = strings.TrimSuffix(strings.TrimPrefix(jsStr, prefix), suffix) + // Remove prefix/suffix with flexible whitespace handling + jsStr = strings.TrimSpace(jsStr) + jsStr = strings.TrimPrefix(jsStr, "__tool_call__") + jsStr = strings.TrimSuffix(jsStr, "__tool_call__") + jsStr = strings.TrimSpace(jsStr) // HTML-decode the JSON string to handle encoded characters like < -> <= decodedJsStr := html.UnescapeString(jsStr) + // Try to find valid JSON bounds (first { to last }) + start := strings.Index(decodedJsStr, "{") + end := strings.LastIndex(decodedJsStr, "}") + if start == -1 || end == -1 || end <= start { + logger.Error("failed to find valid JSON in tool call", "json_string", decodedJsStr) + toolResponseMsg := models.RoleMsg{ + Role: cfg.ToolRole, + Content: "Error processing tool call: no valid JSON found. Please check the JSON format.", + } + chatBody.Messages = append(chatBody.Messages, toolResponseMsg) + crr := &models.ChatRoundReq{ + Role: cfg.AssistantRole, + } + chatRoundChan <- crr + return true + } + decodedJsStr = decodedJsStr[start : end+1] var err error fc, err = unmarshalFuncCall(decodedJsStr) if err != nil { -- cgit v1.2.3