summaryrefslogtreecommitdiff
path: root/bot.go
diff options
context:
space:
mode:
authorGrail Finder <wohilas@gmail.com>2026-02-28 15:40:52 +0300
committerGrail Finder <wohilas@gmail.com>2026-02-28 15:40:52 +0300
commit32be271aa3c14c73d5a1d9d8d03432f836594401 (patch)
tree9b9065eec8461799730c2f8e76abb7c7a7684324 /bot.go
parent133ec279385d38c2b597f5404ad8c8143080f161 (diff)
Feat (tools): file_edit
Diffstat (limited to 'bot.go')
-rw-r--r--bot.go31
1 files changed, 27 insertions, 4 deletions
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)<think>.*?</think>`)
+
// 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 &lt; -> <=
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 {