Initial commit: CarrotAssistant backend (Go + Gin + SQLite, OpenAI-compatible agent runtime)

This commit is contained in:
2026-07-06 16:10:55 +08:00
commit 5f84739b18
40 changed files with 4781 additions and 0 deletions

36
internal/llm/util.go Normal file
View File

@@ -0,0 +1,36 @@
package llm
import (
"encoding/json"
)
// parseArguments decodes a tool-call arguments JSON string into a map. An
// empty or malformed string yields an empty map rather than an error, so a
// misbehaving model never crashes the loop — the executor will simply receive
// no arguments and the audit log will show what the model actually produced.
func parseArguments(raw string) (map[string]any, error) {
if raw == "" {
return map[string]any{}, nil
}
var out map[string]any
if err := json.Unmarshal([]byte(raw), &out); err != nil {
return map[string]any{}, err
}
if out == nil {
out = map[string]any{}
}
return out, nil
}
// marshalArguments serialises a map back to a JSON string for the assistant
// echo message. A nil map yields "{}".
func marshalArguments(args map[string]any) string {
if args == nil {
return "{}"
}
b, err := json.Marshal(args)
if err != nil {
return "{}"
}
return string(b)
}