package coordinator import ( "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/runnerproto" "amadan.net/oficina/equipo/internal/taskplan" "bytes" "encoding/json" "reflect" "strings" "unicode/utf8" ) func Names() []string { return provider.CoordinatorTools() } func argument(name string) any { switch name { case "equipo_status": return &EquipoStatus{} case "read_project": return &ReadProject{} case "read_task": return &ReadTask{} case "propose_project": return &ProposeProject{} case "propose_plan": return &ProposePlan{} case "ask_person": return &AskPerson{} case "route_to_task": return &RouteToTask{} case "post_message": return &PostMessage{} case "suggest_preference": return &SuggestPreference{} case "suggest_specialist", "suggest_playbook": return &SuggestDraft{} } return nil } func Text(s string, limit int) bool { return strings.TrimSpace(s) != "" && len(s) <= limit && utf8.ValidString(s) } func ids(ss []string, allowEmpty bool) bool { if (!allowEmpty && len(ss) == 0) || len(ss) > 32 { return false } seen := map[string]bool{} for _, s := range ss { if !Text(s, 256) || seen[s] { return false } seen[s] = true } return true } func Validate(call CoordinatorToolCall) (any, error) { v := argument(call.Name) if v == nil || !Text(call.RequestID, 256) || len(call.Arguments) > 65536 || runnerproto.DecodeLimit(bytes.NewReader(call.Arguments), v, 65536) != nil { return nil, runnerproto.ErrInvalid } valid := false switch a := v.(type) { case *EquipoStatus: valid = true case *ReadProject: valid = a.ProjectID > 0 case *ReadTask: valid = Text(a.TaskID, 256) case *ProposeProject: valid = Text(a.Repository, 512) && Text(a.Why, 2048) case *ProposePlan: p := a.Plan.TaskPlan() if p.ProjectID == 0 { p.ProjectID = 1 } defaultPlan := len(p.Steps) == 0 && (taskplan.Publication(p.Kind) || p.Kind == taskplan.Investigation) valid = Text(a.MessageID, 256) && a.Plan.ProjectID >= 0 && (taskplan.Validate(p) == nil || defaultPlan) && len(p.PlaybookID) <= 256 && p.PlaybookRevision >= 0 if p.Kind == taskplan.Recommendation && (p.PlaybookID != "" || p.PlaybookRevision != 0) { valid = false } for _, s := range a.Plan.Steps { valid = valid && len(s.Instructions) <= 8192 } case *AskPerson: valid = Text(a.MessageID, 256) && Text(a.Question, 8192) && len(a.Options) <= 32 && a.Options != nil seen := map[string]bool{} for _, o := range a.Options { valid = valid && Text(o.ID, 256) && Text(o.Label, 512) && !seen[o.ID] seen[o.ID] = true } case *RouteToTask: valid = Text(a.MessageID, 256) && Text(a.TaskID, 256) case *PostMessage: 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) case *SuggestPreference: valid = Text(a.SourceMessageID, 256) && Text(a.Text, 8192) case *SuggestDraft: valid = ids(a.SourceMessageIDs, false) && Text(a.Draft.Name, 256) && Text(a.Draft.Instructions, 8192) } if !valid { return nil, runnerproto.ErrInvalid } return v, nil } // Schemas and decoding use the same explicit argument types. No authority fields // or provider configuration can be supplied through these objects. func Schemas(descriptions map[string]string) []map[string]any { out := []map[string]any{} for _, name := range Names() { schema := schemaFor(reflect.TypeOf(argument(name)).Elem()) props := schema["properties"].(map[string]any) props["request_id"] = map[string]any{"type": "string", "minLength": 1, "maxLength": 256} schema["required"] = append(schema["required"].([]string), "request_id") out = append(out, map[string]any{"name": name, "description": descriptions[name], "inputSchema": schema}) } return out } func schemaFor(t reflect.Type) map[string]any { switch t.Kind() { case reflect.Struct: props := map[string]any{} required := []string{} for i := 0; i < t.NumField(); i++ { f := t.Field(i) tag := strings.Split(f.Tag.Get("json"), ",") props[tag[0]] = schemaFor(f.Type) if len(tag) == 1 { required = append(required, tag[0]) } } return map[string]any{"type": "object", "properties": props, "required": required, "additionalProperties": false} case reflect.Slice: return map[string]any{"type": "array", "items": schemaFor(t.Elem()), "maxItems": 32} case reflect.Int64: return map[string]any{"type": "integer", "minimum": 0} default: return map[string]any{"type": "string", "maxLength": 16384} } } func Canonical(call CoordinatorToolCall) (CoordinatorToolCall, error) { v, err := Validate(call) if err != nil { return call, err } call.Arguments, err = json.Marshal(v) return call, err }