miaopanel
1package ai
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "strings"
8 "unicode/utf8"
9)
10
11// Tool is one callable panel operation. Summary is generated by the server so
12// the approval card never relies on the model's own description.
13type Tool struct {
14 Name string
15 Description string
16 Parameters map[string]any
17 Risk Risk
18 Summary func(args map[string]any) string
19 Run func(ctx context.Context, args map[string]any) (string, error)
20}
21
22// Registry is the fixed server-side tool whitelist.
23type Registry struct {
24 tools map[string]Tool
25}
26
27// NewRegistry returns an empty registry.
28func NewRegistry() *Registry {
29 return &Registry{tools: map[string]Tool{}}
30}
31
32// Register adds one tool.
33func (r *Registry) Register(t Tool) {
34 r.tools[t.Name] = t
35}
36
37// Get looks a tool up by the name supplied by the model.
38func (r *Registry) Get(name string) (Tool, bool) {
39 t, ok := r.tools[name]
40 return t, ok
41}
42
43// Defs returns the OpenAI function definitions.
44func (r *Registry) Defs() []ToolDef {
45 out := make([]ToolDef, 0, len(r.tools))
46 for _, t := range r.tools {
47 out = append(out, ToolDef{
48 Type: "function",
49 Function: FunctionDef{
50 Name: t.Name,
51 Description: t.Description,
52 Parameters: t.Parameters,
53 },
54 })
55 }
56 return out
57}
58
59func objectSchema(properties map[string]any, required ...string) map[string]any {
60 schema := map[string]any{
61 "type": "object",
62 "properties": properties,
63 }
64 if len(required) > 0 {
65 schema["required"] = required
66 }
67 return schema
68}
69
70func prop(kind, desc string) map[string]any {
71 return map[string]any{"type": kind, "description": desc}
72}
73
74func arrayProp(desc string, itemType string) map[string]any {
75 return map[string]any{
76 "type": "array",
77 "description": desc,
78 "items": map[string]any{"type": itemType},
79 }
80}
81
82func argString(args map[string]any, key string) string {
83 if v, ok := args[key].(string); ok {
84 return strings.TrimSpace(v)
85 }
86 return ""
87}
88
89func argInt(args map[string]any, key string) (int, bool) {
90 switch v := args[key].(type) {
91 case float64:
92 return int(v), true
93 case int:
94 return v, true
95 case json.Number:
96 n, err := v.Int64()
97 if err != nil {
98 return 0, false
99 }
100 return int(n), true
101 }
102 return 0, false
103}
104
105func argFloat(args map[string]any, key string, def float64) float64 {
106 switch v := args[key].(type) {
107 case float64:
108 return v
109 case int:
110 return float64(v)
111 }
112 return def
113}
114
115func argStrings(args map[string]any, key string) []string {
116 raw, ok := args[key].([]any)
117 if !ok {
118 if one := argString(args, key); one != "" {
119 return []string{one}
120 }
121 return nil
122 }
123 out := make([]string, 0, len(raw))
124 for _, item := range raw {
125 if s, ok := item.(string); ok && strings.TrimSpace(s) != "" {
126 out = append(out, strings.TrimSpace(s))
127 }
128 }
129 return out
130}
131
132func boolArg(args map[string]any, key string) bool {
133 if v, ok := args[key].(bool); ok {
134 return v
135 }
136 return false
137}
138
139// clipResult keeps tool output within the context budget.
140func clipResult(text string, maxBytes int) string {
141 if maxBytes <= 0 {
142 maxBytes = 8 << 10
143 }
144 if len(text) <= maxBytes {
145 return text
146 }
147 // Keep valid UTF-8 when cutting.
148 cut := text[:maxBytes]
149 for len(cut) > 0 {
150 if utf8.ValidString(cut) {
151 break
152 }
153 cut = cut[:len(cut)-1]
154 }
155 return cut + "\n…(输出已截断)"
156}
157
158func jsonText(v any) (string, error) {
159 raw, err := json.Marshal(v)
160 if err != nil {
161 return "", fmt.Errorf("序列化工具结果失败: %w", err)
162 }
163 return string(raw), nil
164}