Initial commit: CarrotAssistant backend (Go + Gin + SQLite, OpenAI-compatible agent runtime)
This commit is contained in:
36
internal/llm/util.go
Normal file
36
internal/llm/util.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user