37 lines
930 B
Go
37 lines
930 B
Go
|
|
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)
|
||
|
|
}
|