oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package coordinator
2
3import (
4 "amadan.net/oficina/equipo/internal/runner/provider"
5 "amadan.net/oficina/equipo/internal/runnerproto"
6 "amadan.net/oficina/equipo/internal/taskplan"
7 "bytes"
8 "encoding/json"
9 "reflect"
10 "strings"
11 "unicode/utf8"
12)
13
14func Names() []string { return provider.CoordinatorTools() }
15func argument(name string) any {
16 switch name {
17 case "equipo_status":
18 return &EquipoStatus{}
19 case "read_project":
20 return &ReadProject{}
21 case "read_task":
22 return &ReadTask{}
23 case "propose_project":
24 return &ProposeProject{}
25 case "propose_plan":
26 return &ProposePlan{}
27 case "ask_person":
28 return &AskPerson{}
29 case "route_to_task":
30 return &RouteToTask{}
31 case "post_message":
32 return &PostMessage{}
33 case "suggest_preference":
34 return &SuggestPreference{}
35 case "suggest_specialist", "suggest_playbook":
36 return &SuggestDraft{}
37 }
38 return nil
39}
40func Text(s string, limit int) bool {
41 return strings.TrimSpace(s) != "" && len(s) <= limit && utf8.ValidString(s)
42}
43func ids(ss []string, allowEmpty bool) bool {
44 if (!allowEmpty && len(ss) == 0) || len(ss) > 32 {
45 return false
46 }
47 seen := map[string]bool{}
48 for _, s := range ss {
49 if !Text(s, 256) || seen[s] {
50 return false
51 }
52 seen[s] = true
53 }
54 return true
55}
56func Validate(call CoordinatorToolCall) (any, error) {
57 v := argument(call.Name)
58 if v == nil || !Text(call.RequestID, 256) || len(call.Arguments) > 65536 || runnerproto.DecodeLimit(bytes.NewReader(call.Arguments), v, 65536) != nil {
59 return nil, runnerproto.ErrInvalid
60 }
61 valid := false
62 switch a := v.(type) {
63 case *EquipoStatus:
64 valid = true
65 case *ReadProject:
66 valid = a.ProjectID > 0
67 case *ReadTask:
68 valid = Text(a.TaskID, 256)
69 case *ProposeProject:
70 valid = Text(a.Repository, 512) && Text(a.Why, 2048)
71 case *ProposePlan:
72 p := a.Plan.TaskPlan()
73 if p.ProjectID == 0 {
74 p.ProjectID = 1
75 }
76 defaultPlan := len(p.Steps) == 0 && (taskplan.Publication(p.Kind) || p.Kind == taskplan.Investigation)
77 valid = Text(a.MessageID, 256) && a.Plan.ProjectID >= 0 && (taskplan.Validate(p) == nil || defaultPlan) && len(p.PlaybookID) <= 256 && p.PlaybookRevision >= 0
78 if p.Kind == taskplan.Recommendation && (p.PlaybookID != "" || p.PlaybookRevision != 0) {
79 valid = false
80 }
81 for _, s := range a.Plan.Steps {
82 valid = valid && len(s.Instructions) <= 8192
83 }
84 case *AskPerson:
85 valid = Text(a.MessageID, 256) && Text(a.Question, 8192) && len(a.Options) <= 32 && a.Options != nil
86 seen := map[string]bool{}
87 for _, o := range a.Options {
88 valid = valid && Text(o.ID, 256) && Text(o.Label, 512) && !seen[o.ID]
89 seen[o.ID] = true
90 }
91 case *RouteToTask:
92 valid = Text(a.MessageID, 256) && Text(a.TaskID, 256)
93 case *PostMessage:
94 valid = Text(a.Body, 16384) && a.SourceRefs != nil && len(a.SourceRefs) <= 32 && (len(a.SourceRefs) == 0 || taskplan.ValidateAnswer(taskplan.AnswerResult{Body: a.Body, Sources: a.SourceRefs}) == nil)
95 case *SuggestPreference:
96 valid = Text(a.SourceMessageID, 256) && Text(a.Text, 8192)
97 case *SuggestDraft:
98 valid = ids(a.SourceMessageIDs, false) && Text(a.Draft.Name, 256) && Text(a.Draft.Instructions, 8192)
99 }
100 if !valid {
101 return nil, runnerproto.ErrInvalid
102 }
103 return v, nil
104}
105
106// Schemas and decoding use the same explicit argument types. No authority fields
107// or provider configuration can be supplied through these objects.
108func Schemas(descriptions map[string]string) []map[string]any {
109 out := []map[string]any{}
110 for _, name := range Names() {
111 schema := schemaFor(reflect.TypeOf(argument(name)).Elem())
112 props := schema["properties"].(map[string]any)
113 props["request_id"] = map[string]any{"type": "string", "minLength": 1, "maxLength": 256}
114 schema["required"] = append(schema["required"].([]string), "request_id")
115 out = append(out, map[string]any{"name": name, "description": descriptions[name], "inputSchema": schema})
116 }
117 return out
118}
119func schemaFor(t reflect.Type) map[string]any {
120 switch t.Kind() {
121 case reflect.Struct:
122 props := map[string]any{}
123 required := []string{}
124 for i := 0; i < t.NumField(); i++ {
125 f := t.Field(i)
126 tag := strings.Split(f.Tag.Get("json"), ",")
127 props[tag[0]] = schemaFor(f.Type)
128 if len(tag) == 1 {
129 required = append(required, tag[0])
130 }
131 }
132 return map[string]any{"type": "object", "properties": props, "required": required, "additionalProperties": false}
133 case reflect.Slice:
134 return map[string]any{"type": "array", "items": schemaFor(t.Elem()), "maxItems": 32}
135 case reflect.Int64:
136 return map[string]any{"type": "integer", "minimum": 0}
137 default:
138 return map[string]any{"type": "string", "maxLength": 16384}
139 }
140}
141func Canonical(call CoordinatorToolCall) (CoordinatorToolCall, error) {
142 v, err := Validate(call)
143 if err != nil {
144 return call, err
145 }
146 call.Arguments, err = json.Marshal(v)
147 return call, err
148}
149