Chat tools: argument validation, replace, file snapshots, server-fetched images, show_run, show_diff head/paths, schedule_followup
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
package chattools
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ValidateArguments checks a call's arguments against the tool's declared
|
||||
// input schema: a JSON object holding only declared fields, every required
|
||||
// one, each of its declared type (and enum). The schemas promise
|
||||
// additionalProperties false, so a misnamed field is refused with a message
|
||||
// naming the expected ones rather than silently ignored.
|
||||
func ValidateArguments(tool string, arguments json.RawMessage) error {
|
||||
schema := toolSchema(tool)
|
||||
if schema == nil {
|
||||
return fmt.Errorf("unknown tool %s", tool)
|
||||
}
|
||||
properties, _ := schema["properties"].(map[string]any)
|
||||
required, _ := schema["required"].([]string)
|
||||
expected := expectedFields(properties, required)
|
||||
var fields map[string]json.RawMessage
|
||||
decoder := json.NewDecoder(bytes.NewReader(arguments))
|
||||
if err := decoder.Decode(&fields); err != nil || fields == nil {
|
||||
return fmt.Errorf("the arguments must be a JSON object; %s takes %s", tool, expected)
|
||||
}
|
||||
var unknown []string
|
||||
for name := range fields {
|
||||
if _, ok := properties[name]; !ok {
|
||||
unknown = append(unknown, name)
|
||||
}
|
||||
}
|
||||
if len(unknown) > 0 {
|
||||
sort.Strings(unknown)
|
||||
return fmt.Errorf("unknown %s %s; %s takes %s", plural(len(unknown), "argument", "arguments"), quoteAll(unknown), tool, expected)
|
||||
}
|
||||
for _, name := range required {
|
||||
if raw, ok := fields[name]; !ok || string(raw) == "null" {
|
||||
return fmt.Errorf("%s is required; %s takes %s", name, tool, expected)
|
||||
}
|
||||
}
|
||||
for name, raw := range fields {
|
||||
if string(raw) == "null" {
|
||||
continue
|
||||
}
|
||||
property, _ := properties[name].(map[string]any)
|
||||
if err := checkType(name, property, raw); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func toolSchema(tool string) map[string]any {
|
||||
for _, definition := range definitions() {
|
||||
if definition.Name == tool {
|
||||
return definition.InputSchema
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkType(name string, property map[string]any, raw json.RawMessage) error {
|
||||
kind, _ := property["type"].(string)
|
||||
switch kind {
|
||||
case "string":
|
||||
var text string
|
||||
if json.Unmarshal(raw, &text) != nil {
|
||||
return fmt.Errorf("%s must be a string", name)
|
||||
}
|
||||
if values, ok := property["enum"].([]string); ok && !slices.Contains(values, text) {
|
||||
return fmt.Errorf("%s must be one of %s", name, strings.Join(values, ", "))
|
||||
}
|
||||
case "integer":
|
||||
var number json.Number
|
||||
decoder := json.NewDecoder(bytes.NewReader(raw))
|
||||
decoder.UseNumber()
|
||||
if bytes.HasPrefix(bytes.TrimSpace(raw), []byte(`"`)) || decoder.Decode(&number) != nil {
|
||||
return fmt.Errorf("%s must be an integer", name)
|
||||
}
|
||||
if _, err := number.Int64(); err != nil {
|
||||
return fmt.Errorf("%s must be an integer", name)
|
||||
}
|
||||
case "boolean":
|
||||
var flag bool
|
||||
if json.Unmarshal(raw, &flag) != nil {
|
||||
return fmt.Errorf("%s must be true or false", name)
|
||||
}
|
||||
case "array":
|
||||
var list []string
|
||||
if json.Unmarshal(raw, &list) != nil {
|
||||
return fmt.Errorf("%s must be an array of strings", name)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// expectedFields names a tool's fields, required ones first and marked.
|
||||
func expectedFields(properties map[string]any, required []string) string {
|
||||
var names []string
|
||||
for _, name := range required {
|
||||
names = append(names, name+" (required)")
|
||||
}
|
||||
var optional []string
|
||||
for name := range properties {
|
||||
if !slices.Contains(required, name) {
|
||||
optional = append(optional, name)
|
||||
}
|
||||
}
|
||||
sort.Strings(optional)
|
||||
names = append(names, optional...)
|
||||
if len(names) == 0 {
|
||||
return "no arguments"
|
||||
}
|
||||
return strings.Join(names, ", ")
|
||||
}
|
||||
|
||||
func quoteAll(names []string) string {
|
||||
quoted := make([]string, len(names))
|
||||
for i, name := range names {
|
||||
quoted[i] = fmt.Sprintf("%q", name)
|
||||
}
|
||||
return strings.Join(quoted, ", ")
|
||||
}
|
||||
|
||||
func plural(n int, one, many string) string {
|
||||
if n == 1 {
|
||||
return one
|
||||
}
|
||||
return many
|
||||
}
|
||||
Reference in New Issue
Block a user