34 lines
867 B
Go
34 lines
867 B
Go
package app
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
)
|
|
|
|
var errCloudAgentJSONSingleObject = errors.New("参数必须是单个 JSON 对象")
|
|
|
|
// decodeCloudAgentJSONObject is used for model tool arguments. Tool arguments
|
|
// are an untrusted protocol boundary: reject non-objects, unknown fields and
|
|
// trailing JSON instead of silently accepting an ambiguous payload.
|
|
func decodeCloudAgentJSONObject(raw string, target any) error {
|
|
data := bytes.TrimSpace([]byte(raw))
|
|
if len(data) == 0 || data[0] != '{' {
|
|
return errCloudAgentJSONSingleObject
|
|
}
|
|
decoder := json.NewDecoder(bytes.NewReader(data))
|
|
decoder.DisallowUnknownFields()
|
|
if err := decoder.Decode(target); err != nil {
|
|
return err
|
|
}
|
|
var extra any
|
|
if err := decoder.Decode(&extra); err != io.EOF {
|
|
if err == nil {
|
|
return errCloudAgentJSONSingleObject
|
|
}
|
|
return err
|
|
}
|
|
return nil
|
|
}
|