From f560ecf70baa163b7f384b4d8162bf41026e80f9 Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Wed, 18 Feb 2026 21:22:58 +0300 Subject: Card: coding assistant --- bot.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'bot.go') 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 { -- cgit v1.2.3