package ai import ( "context" "encoding/json" "fmt" "strings" "unicode/utf8" ) // Tool is one callable panel operation. Summary is generated by the server so // the approval card never relies on the model's own description. type Tool struct { Name string Description string Parameters map[string]any Risk Risk Summary func(args map[string]any) string Run func(ctx context.Context, args map[string]any) (string, error) } // Registry is the fixed server-side tool whitelist. type Registry struct { tools map[string]Tool } // NewRegistry returns an empty registry. func NewRegistry() *Registry { return &Registry{tools: map[string]Tool{}} } // Register adds one tool. func (r *Registry) Register(t Tool) { r.tools[t.Name] = t } // Get looks a tool up by the name supplied by the model. func (r *Registry) Get(name string) (Tool, bool) { t, ok := r.tools[name] return t, ok } // Defs returns the OpenAI function definitions. func (r *Registry) Defs() []ToolDef { out := make([]ToolDef, 0, len(r.tools)) for _, t := range r.tools { out = append(out, ToolDef{ Type: "function", Function: FunctionDef{ Name: t.Name, Description: t.Description, Parameters: t.Parameters, }, }) } return out } func objectSchema(properties map[string]any, required ...string) map[string]any { schema := map[string]any{ "type": "object", "properties": properties, } if len(required) > 0 { schema["required"] = required } return schema } func prop(kind, desc string) map[string]any { return map[string]any{"type": kind, "description": desc} } func arrayProp(desc string, itemType string) map[string]any { return map[string]any{ "type": "array", "description": desc, "items": map[string]any{"type": itemType}, } } func argString(args map[string]any, key string) string { if v, ok := args[key].(string); ok { return strings.TrimSpace(v) } return "" } func argInt(args map[string]any, key string) (int, bool) { switch v := args[key].(type) { case float64: return int(v), true case int: return v, true case json.Number: n, err := v.Int64() if err != nil { return 0, false } return int(n), true } return 0, false } func argFloat(args map[string]any, key string, def float64) float64 { switch v := args[key].(type) { case float64: return v case int: return float64(v) } return def } func argStrings(args map[string]any, key string) []string { raw, ok := args[key].([]any) if !ok { if one := argString(args, key); one != "" { return []string{one} } return nil } out := make([]string, 0, len(raw)) for _, item := range raw { if s, ok := item.(string); ok && strings.TrimSpace(s) != "" { out = append(out, strings.TrimSpace(s)) } } return out } func boolArg(args map[string]any, key string) bool { if v, ok := args[key].(bool); ok { return v } return false } // clipResult keeps tool output within the context budget. func clipResult(text string, maxBytes int) string { if maxBytes <= 0 { maxBytes = 8 << 10 } if len(text) <= maxBytes { return text } // Keep valid UTF-8 when cutting. cut := text[:maxBytes] for len(cut) > 0 { if utf8.ValidString(cut) { break } cut = cut[:len(cut)-1] } return cut + "\n…(输出已截断)" } func jsonText(v any) (string, error) { raw, err := json.Marshal(v) if err != nil { return "", fmt.Errorf("序列化工具结果失败: %w", err) } return string(raw), nil }