| 1 | package personapi |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/kernel" |
| 5 | "amadan.net/oficina/equipo/internal/equipo/setup" |
| 6 | "context" |
| 7 | "errors" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/equipo/library" |
| 13 | "amadan.net/oficina/equipo/internal/equipo/planning" |
| 14 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 15 | "gorm.io/gorm" |
| 16 | ) |
| 17 | |
| 18 | func (s *Native) Read(ctx context.Context, a kernel.Actor, op, id string) (out Result, err error) { |
| 19 | switch op { |
| 20 | case "devices": |
| 21 | v, e := s.Devices().List(ctx, a) |
| 22 | rev := Revision(v) |
| 23 | if id != "" { |
| 24 | for _, d := range v { |
| 25 | if d.ID == id { |
| 26 | rev = d.Revision |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | return Result{Value: v, Revision: rev}, e |
| 31 | case "teams", "link": |
| 32 | v, e := s.TeamLinks(ctx, a) |
| 33 | items := []TeamLink{} |
| 34 | for _, row := range v { |
| 35 | p := apiLink(row) |
| 36 | if op == "link" && row.ID == id { |
| 37 | return Result{Value: p, Revision: row.Revision}, e |
| 38 | } |
| 39 | items = append(items, p) |
| 40 | } |
| 41 | if op == "link" && e == nil { |
| 42 | e = s.NotFound |
| 43 | } |
| 44 | return Result{Value: items, Revision: Revision(items)}, e |
| 45 | case "specialists", "specialist": |
| 46 | var rows []library.Specialist |
| 47 | if op == "specialist" { |
| 48 | v, e := s.Specialist(ctx, a, id) |
| 49 | if e != nil { |
| 50 | return out, e |
| 51 | } |
| 52 | rows = []library.Specialist{v} |
| 53 | } else { |
| 54 | rows, err = s.ListSpecialists(ctx, a) |
| 55 | if err != nil { |
| 56 | return out, err |
| 57 | } |
| 58 | } |
| 59 | var counts map[string]planning.SpecialistWorkView |
| 60 | err = s.ReadTeam(ctx, a, func(tx *gorm.DB, t kernel.Team) error { |
| 61 | v, e := library.Counts(tx, t.ID, time.Now().UTC()) |
| 62 | if e == nil { |
| 63 | counts = v |
| 64 | } |
| 65 | return nil |
| 66 | }) |
| 67 | if err != nil { |
| 68 | return out, err |
| 69 | } |
| 70 | items := []Specialist{} |
| 71 | for _, row := range rows { |
| 72 | p := ProjectSpecialist(row) |
| 73 | if v, ok := counts[row.Key]; ok && v.CountsKnown { |
| 74 | n := int64(v.Working + v.Queued + v.Waiting + v.Finished) |
| 75 | p.WorkCount = &n |
| 76 | } |
| 77 | items = append(items, p) |
| 78 | } |
| 79 | if op == "specialist" { |
| 80 | return Result{Value: items[0], Revision: items[0].Revision}, nil |
| 81 | } |
| 82 | return Result{Value: items, Revision: Revision(items)}, nil |
| 83 | case "playbooks", "playbook": |
| 84 | if op == "playbook" { |
| 85 | return apiPlaybook(s.Playbook(ctx, a, id)) |
| 86 | } |
| 87 | v, e := s.Playbooks(ctx, a) |
| 88 | items := []Playbook{} |
| 89 | for _, row := range v { |
| 90 | items = append(items, ProjectPlaybook(row)) |
| 91 | } |
| 92 | return Result{Value: items, Revision: Revision(items)}, e |
| 93 | case "templates", "template": |
| 94 | v, e := s.SpecialistTemplates(ctx, a) |
| 95 | if e != nil { |
| 96 | return out, e |
| 97 | } |
| 98 | v = append(v, library.PlaybookCatalog()...) |
| 99 | items := []Template{} |
| 100 | for _, row := range v { |
| 101 | kind := "specialist" |
| 102 | if len(row.Ref.ID) > 9 && row.Ref.ID[:9] == "playbook_" { |
| 103 | kind = "playbook" |
| 104 | } |
| 105 | p := Template{ID: row.Ref.ID, Version: row.Ref.Version, Kind: kind, GroupKey: row.GroupKey, NameKey: row.NameKey, DescriptionKey: row.DescriptionKey, InstructionsKey: row.InstructionsKey, Icon: row.Icon} |
| 106 | if op == "template" && p.ID == id { |
| 107 | return Result{Value: p, Revision: p.Version}, nil |
| 108 | } |
| 109 | items = append(items, p) |
| 110 | } |
| 111 | if op == "template" { |
| 112 | return out, s.NotFound |
| 113 | } |
| 114 | return Result{Value: items, Revision: Revision(items)}, nil |
| 115 | case "specialist_diff", "playbook_diff": |
| 116 | var diff library.TemplateDiff |
| 117 | var ref library.TemplateRef |
| 118 | if op == "specialist_diff" { |
| 119 | v, e := s.Specialist(ctx, a, id) |
| 120 | if e != nil { |
| 121 | return out, e |
| 122 | } |
| 123 | ref.ID = v.TemplateID |
| 124 | } else { |
| 125 | v, e := s.Playbook(ctx, a, id) |
| 126 | if e != nil { |
| 127 | return out, e |
| 128 | } |
| 129 | ref.ID = v.Template.ID |
| 130 | } |
| 131 | catalog := append(library.Catalog(), library.PlaybookCatalog()...) |
| 132 | for _, v := range catalog { |
| 133 | if v.Ref.ID == ref.ID && v.Ref.Version > ref.Version { |
| 134 | ref.Version = v.Ref.Version |
| 135 | } |
| 136 | } |
| 137 | if op == "specialist_diff" { |
| 138 | diff, err = s.SpecialistTemplateDiff(ctx, a, id, ref) |
| 139 | } else { |
| 140 | diff, err = s.PlaybookTemplateDiff(ctx, a, id, ref) |
| 141 | } |
| 142 | return Result{Value: ProjectDiff(diff), Revision: diff.CurrentRevision}, err |
| 143 | case "connections", "connection": |
| 144 | v, e := s.Connections(ctx, a) |
| 145 | items := []Connection{} |
| 146 | for _, row := range v { |
| 147 | p := Connection{ID: row.ID, RunnerID: row.RunnerID, Label: row.Label, Revision: row.Revision, RevokedAt: row.RevokedAt, Models: []ConnectionModel{}} |
| 148 | for _, r := range row.Reports { |
| 149 | if p.Label == "" { |
| 150 | p.Label = r.AccountLabel |
| 151 | } |
| 152 | var allowance *int64 |
| 153 | if r.AllowanceKnown { |
| 154 | n := r.AllowanceRemaining |
| 155 | allowance = &n |
| 156 | } |
| 157 | p.Models = append(p.Models, ConnectionModel{Provider: r.Provider, Family: r.Family, Model: r.Model, Kind: r.Kind, Available: r.Available && row.RevokedAt == nil, AllowanceRemaining: allowance}) |
| 158 | } |
| 159 | if op == "connection" && row.ID == id { |
| 160 | return Result{Value: p, Revision: p.Revision}, e |
| 161 | } |
| 162 | items = append(items, p) |
| 163 | } |
| 164 | if op == "connection" && e == nil { |
| 165 | e = s.NotFound |
| 166 | } |
| 167 | return Result{Value: items, Revision: Revision(items)}, e |
| 168 | case "onboarding": |
| 169 | v, e := s.Onboarding(ctx, a) |
| 170 | return Result{Value: apiOnboarding(v), Revision: v.Revision}, e |
| 171 | case "account_setup": |
| 172 | v, e := s.AccountSetup(ctx, a, id) |
| 173 | return Result{Value: s.apiAccountSetup(v), Revision: v.Revision}, e |
| 174 | case "invite": |
| 175 | v, e := s.InvitationSummary(ctx, a, id) |
| 176 | return Result{Value: apiInvite(v), Revision: v.Revision}, e |
| 177 | } |
| 178 | err = s.ReadTeam(ctx, a, func(tx *gorm.DB, t kernel.Team) error { |
| 179 | switch op { |
| 180 | case "roster", "roster_member": |
| 181 | if e := s.Owner(tx, a); e != nil { |
| 182 | return e |
| 183 | } |
| 184 | rows := []kernel.Member{} |
| 185 | if e := tx.Order("address").Find(&rows).Error; e != nil { |
| 186 | return e |
| 187 | } |
| 188 | items := []RosterMember{} |
| 189 | for _, m := range rows { |
| 190 | p := RosterMember{Person: Person{Address: m.Address, DisplayIdentity: m.Address, Role: string(m.Role), Active: m.Active}, Revision: m.Revision} |
| 191 | if op == "roster_member" && m.Address == strings.ToLower(strings.TrimSpace(id)) { |
| 192 | out = Result{Value: p, Revision: m.Revision} |
| 193 | return nil |
| 194 | } |
| 195 | items = append(items, p) |
| 196 | } |
| 197 | if op == "roster_member" { |
| 198 | out = Result{Value: Ack{}, Revision: 0} |
| 199 | } else { |
| 200 | out = Result{Value: items, Revision: Revision(items)} |
| 201 | } |
| 202 | return nil |
| 203 | case "budgets": |
| 204 | if e := s.Owner(tx, a); e != nil { |
| 205 | return e |
| 206 | } |
| 207 | b, e := ReadBudgetsAt(tx, true, s.Unavailable) |
| 208 | if e != nil { |
| 209 | return e |
| 210 | } |
| 211 | p := Budgets{Revision: b.Revision, BudgetsInput: BudgetsInput{Team: projectLimits(b.Team), Projects: map[string]PeriodLimits{}}} |
| 212 | for n, l := range b.Projects { |
| 213 | p.Projects[strconv.FormatInt(int64(n), 10)] = projectLimits(l) |
| 214 | } |
| 215 | out = Result{Value: p, Revision: p.Revision} |
| 216 | return nil |
| 217 | case "accounts": |
| 218 | rows := []kernel.Account{} |
| 219 | if e := tx.Find(&rows).Error; e != nil { |
| 220 | return e |
| 221 | } |
| 222 | items := []Account{} |
| 223 | for _, r := range rows { |
| 224 | items = append(items, Account{ID: strconv.FormatInt(r.ID, 10), Label: r.Label, Revision: r.Revision}) |
| 225 | } |
| 226 | out = Result{Value: items, Revision: Revision(items)} |
| 227 | return nil |
| 228 | case "projects", "project_detail": |
| 229 | rows := []kernel.Project{} |
| 230 | q := tx |
| 231 | if op == "project_detail" { |
| 232 | n, e := s.apiNumericID(id, false) |
| 233 | if e != nil { |
| 234 | return e |
| 235 | } |
| 236 | q = tx.Where("id=?", n) |
| 237 | } |
| 238 | if e := q.Find(&rows).Error; e != nil { |
| 239 | return e |
| 240 | } |
| 241 | items := []Project{} |
| 242 | for _, r := range rows { |
| 243 | owners := []kernel.DecisionOwner{} |
| 244 | if e := tx.Where("project_id=?", r.ID).Order("decision").Find(&owners).Error; e != nil { |
| 245 | return e |
| 246 | } |
| 247 | items = append(items, apiProject(kernel.ProjectDetail{Project: r, Owners: owners})) |
| 248 | } |
| 249 | if op == "project_detail" { |
| 250 | if len(items) != 1 { |
| 251 | return s.NotFound |
| 252 | } |
| 253 | out = Result{Value: items[0], Revision: items[0].Revision} |
| 254 | } else { |
| 255 | out = Result{Value: items, Revision: Revision(items)} |
| 256 | } |
| 257 | return nil |
| 258 | case "project_settings": |
| 259 | n, e := s.apiNumericID(id, false) |
| 260 | if e != nil { |
| 261 | return e |
| 262 | } |
| 263 | v, e := ProjectSettingsAt(tx, a, n, t.ID) |
| 264 | if e != nil { |
| 265 | return e |
| 266 | } |
| 267 | out = Result{Value: v, Revision: Revision(v)} |
| 268 | return nil |
| 269 | case "runners", "runner": |
| 270 | var rows []setup.RunnerRecord |
| 271 | if e := tx.Where("person=? AND instance_id=?", a.Address, t.ID).Find(&rows).Error; e != nil { |
| 272 | return e |
| 273 | } |
| 274 | items := []Runner{} |
| 275 | for _, r := range rows { |
| 276 | p := Runner{ID: r.ID, Label: r.Label, Revision: r.Revision, RevokedAt: r.RevokedAt} |
| 277 | if op == "runner" && r.ID == id { |
| 278 | out = Result{Value: p, Revision: p.Revision} |
| 279 | return nil |
| 280 | } |
| 281 | items = append(items, p) |
| 282 | } |
| 283 | if op == "runner" { |
| 284 | return s.NotFound |
| 285 | } |
| 286 | out = Result{Value: items, Revision: Revision(items)} |
| 287 | return nil |
| 288 | } |
| 289 | return s.NotFound |
| 290 | }) |
| 291 | return out, err |
| 292 | } |
| 293 | func projectLimits(v team.PeriodLimits) PeriodLimits { |
| 294 | return PeriodLimits{Calls: v.Calls, InputTokens: v.InputTokens, OutputTokens: v.OutputTokens, Seconds: v.Seconds} |
| 295 | } |
| 296 | |
| 297 | func ProjectSettingsAt(tx *gorm.DB, a kernel.Actor, n int64, teamID string) (ProjectSettings, error) { |
| 298 | var project kernel.Project |
| 299 | if e := tx.First(&project, n).Error; e != nil { |
| 300 | return ProjectSettings{}, e |
| 301 | } |
| 302 | var e error |
| 303 | v := ProjectSettings{Defaults: []PlaybookDefault{}} |
| 304 | var rows []planning.ApprovalSetting |
| 305 | if e = tx.Where("project_id=? AND person IN ?", n, []string{"", a.Address}).Find(&rows).Error; e != nil { |
| 306 | return ProjectSettings{}, e |
| 307 | } |
| 308 | for _, r := range rows { |
| 309 | if r.Person == "" { |
| 310 | v.ApprovalMode = string(r.Mode) |
| 311 | v.ApprovalRevision = r.Revision |
| 312 | } else { |
| 313 | v.MyApprovalMode = string(r.Mode) |
| 314 | v.MyApprovalRevision = r.Revision |
| 315 | } |
| 316 | } |
| 317 | var defs []library.PlaybookDefault |
| 318 | if e = tx.Where("project_id=? AND team_id=?", n, teamID).Find(&defs).Error; e != nil { |
| 319 | return ProjectSettings{}, e |
| 320 | } |
| 321 | for _, d := range defs { |
| 322 | v.Defaults = append(v.Defaults, PlaybookDefault{Kind: d.Kind, PlaybookID: d.PlaybookID, Revision: d.Revision}) |
| 323 | } |
| 324 | c, ids, e := planning.ConnectionChoiceAt(tx, a.Address, n) |
| 325 | if e != nil && !errors.Is(e, gorm.ErrRecordNotFound) { |
| 326 | return ProjectSettings{}, e |
| 327 | } |
| 328 | v.Connections = ConnectionChoiceInput{ConnectionIDs: ids, PinnedID: c.PinnedID, Mode: c.Mode} |
| 329 | v.ConnectionsRevision = c.Revision |
| 330 | |
| 331 | var rules planning.ProjectRules |
| 332 | e = tx.First(&rules, "project_id=?", n).Error |
| 333 | if e != nil && !errors.Is(e, gorm.ErrRecordNotFound) { |
| 334 | return ProjectSettings{}, e |
| 335 | } |
| 336 | v.Rules = rules.Text |
| 337 | v.RulesRevision = rules.Revision |
| 338 | return v, nil |
| 339 | } |
| 340 | |