summaryrefslogtreecommitdiff
path: root/bot.go
diff options
context:
space:
mode:
authorGrail Finder <wohilas@gmail.com>2026-02-18 21:22:58 +0300
committerGrail Finder <wohilas@gmail.com>2026-02-18 21:22:58 +0300
commitf560ecf70baa163b7f384b4d8162bf41026e80f9 (patch)
tree73bbc5316e26b1b544c8ce58c381f3f0f722bd20 /bot.go
parentf40f09390b7ccf365b41fa1cc134432537b50cad (diff)
Card: coding assistant
Diffstat (limited to 'bot.go')
-rw-r--r--bot.go19
1 files changed, 19 insertions, 0 deletions
diff --git a/bot.go b/bot.go
index ab55400..1843fb1 100644
--- a/bot.go
+++ b/bot.go
@@ -886,6 +886,8 @@ func cleanChatBody() {
// convertJSONToMapStringString unmarshals JSON into map[string]interface{} and converts all values to strings.
func convertJSONToMapStringString(jsonStr string) (map[string]string, error) {
+ // Extract JSON object from string - models may output extra text after JSON
+ jsonStr = extractJSON(jsonStr)
var raw map[string]interface{}
if err := json.Unmarshal([]byte(jsonStr), &raw); err != nil {
return nil, err
@@ -911,6 +913,23 @@ func convertJSONToMapStringString(jsonStr string) (map[string]string, error) {
return result, nil
}
+// extractJSON finds the first { and last } to extract only the JSON object
+// This handles cases where models output extra text after JSON
+func extractJSON(s string) string {
+ // Try direct parse first - if it works, return as-is
+ var dummy map[string]interface{}
+ if err := json.Unmarshal([]byte(s), &dummy); err == nil {
+ return s
+ }
+ // Otherwise find JSON boundaries
+ start := strings.Index(s, "{")
+ end := strings.LastIndex(s, "}")
+ if start >= 0 && end > start {
+ return s[start : end+1]
+ }
+ return s
+}
+
// unmarshalFuncCall unmarshals a JSON tool call, converting numeric arguments to strings.
func unmarshalFuncCall(jsonStr string) (*models.FuncCall, error) {
type tempFuncCall struct {