oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package library
2
3import (
4 "encoding/json"
5 "reflect"
6 "slices"
7
8 "amadan.net/oficina/equipo/internal/equipo/kernel"
9)
10
11type TemplateRef struct {
12 ID string
13 Version int64
14}
15type TemplateDiff struct {
16 From, To TemplateRef
17 CurrentRevision int64
18 Old, Current, Suggested map[string]json.RawMessage
19}
20type Template struct {
21 Ref TemplateRef
22 Group, GroupKey, NameKey, DescriptionKey, InstructionsKey, Icon string
23}
24
25func Catalog() []Template {
26 rows := []Template{}
27 for _, g := range []struct {
28 group, icon string
29 ids []string
30 }{
31 {"quality", "check-circle", []string{"qa", "accessibility", "performance"}},
32 {"safety", "shield", []string{"security", "privacy"}},
33 {"running", "sliders", []string{"ops", "data", "support"}},
34 {"words", "type", []string{"docs", "localisation", "seo"}},
35 {"", "plus", []string{"blank"}},
36 } {
37 for _, id := range g.ids {
38 key := "templates." + id
39 groupKey := ""
40 if g.group != "" {
41 groupKey = "templates.group." + g.group
42 }
43 rows = append(rows, Template{Ref: TemplateRef{id, 1}, Group: g.group, GroupKey: groupKey, NameKey: key + ".name", DescriptionKey: key + ".description", InstructionsKey: key + ".instructions", Icon: g.icon})
44 }
45 }
46 return rows
47}
48func (s *Store) Templates(actor kernel.Actor) ([]Template, error) {
49 if _, err := s.deps.Authorize(s.tx, actor); err != nil {
50 return nil, err
51 }
52 return s.deps.Catalog(), nil
53}
54func (s *Store) template(ref TemplateRef) (SpecialistInput, error) {
55 for _, t := range s.deps.Catalog() {
56 if t.Ref == ref {
57 return SpecialistInput{Name: s.deps.Copy(t.NameKey), Icon: t.Icon, Instructions: s.deps.Copy(t.InstructionsKey), MaxConcurrent: 1}, nil
58 }
59 }
60 return SpecialistInput{}, s.deps.NotFound
61}
62func (s *Store) Install(actor kernel.Actor, ref TemplateRef, m kernel.Mutation) (Specialist, error) {
63 // Replay is keyed by the pinned reference, not text that a later binary might
64 // resolve differently. Authorization is always checked first.
65 a, err := s.deps.Authorize(s.tx, actor)
66 if err != nil {
67 return Specialist{}, err
68 }
69 q, row, replay, err := s.replay(a, actor, "specialist.install", ref.ID, m, ref)
70 if err != nil || replay {
71 return row, err
72 }
73 in, err := s.template(ref)
74 if err != nil {
75 return Specialist{}, err
76 }
77 if m.Revision != 0 {
78 return Specialist{}, s.deps.Invalid
79 }
80 return s.insert(a, actor, in, ref, q)
81}
82func fields(row Specialist) map[string]json.RawMessage {
83 result := map[string]json.RawMessage{}
84 for k, v := range map[string]any{"name": row.Name, "icon": row.Icon, "instructions": row.StandingInstructions, "allowed_models": row.AllowedModels, "tools": row.Tools, "project_ids": row.ProjectIDs, "max_concurrent": row.MaxConcurrent} {
85 raw, _ := json.Marshal(v)
86 result[k] = raw
87 }
88 return result
89}
90func (s *Store) Diff(actor kernel.Actor, key string, target TemplateRef) (TemplateDiff, error) {
91 row, err := s.Get(actor, key)
92 if err != nil {
93 return TemplateDiff{}, err
94 }
95 if row.TemplateID == "" || row.TemplateID != target.ID || target.Version < row.TemplateVersion {
96 return TemplateDiff{}, s.deps.Invalid
97 }
98 in, err := s.template(target)
99 if err != nil {
100 return TemplateDiff{}, err
101 }
102 var old map[string]json.RawMessage
103 if err = json.Unmarshal([]byte(row.TemplateBase), &old); err != nil {
104 return TemplateDiff{}, err
105 }
106 var suggested Specialist
107 assign(&suggested, in)
108 return TemplateDiff{From: TemplateRef{row.TemplateID, row.TemplateVersion}, To: target, CurrentRevision: row.Revision, Old: old, Current: fields(row), Suggested: fields(suggested)}, nil
109}
110func (s *Store) Apply(actor kernel.Actor, key string, diff TemplateDiff, selected []string, m kernel.Mutation) (Specialist, error) {
111 a, row, err := s.editor(actor, key)
112 if err != nil {
113 return Specialist{}, err
114 }
115 q, saved, replay, err := s.replay(a, actor, "specialist.template_update", key, m, struct {
116 Diff TemplateDiff
117 Selected []string
118 }{diff, selected})
119 if err != nil || replay {
120 return saved, err
121 }
122 current, err := s.Diff(actor, key, diff.To)
123 if err != nil {
124 return Specialist{}, err
125 }
126 if m.Revision != diff.CurrentRevision || !equalDiff(current, diff) {
127 return Specialist{}, s.deps.Conflict
128 }
129 if len(selected) == 0 {
130 return Specialist{}, s.deps.Invalid
131 }
132 values := fields(row)
133 seen := []string{}
134 for _, key := range selected {
135 v, ok := diff.Suggested[key]
136 if !ok || slices.Contains(seen, key) {
137 return Specialist{}, s.deps.Invalid
138 }
139 seen = append(seen, key)
140 values[key] = v
141 }
142 in := Input(row)
143 for key, dst := range map[string]any{"name": &in.Name, "icon": &in.Icon, "instructions": &in.Instructions, "allowed_models": &in.AllowedModels, "tools": &in.Tools, "project_ids": &in.ProjectIDs, "max_concurrent": &in.MaxConcurrent} {
144 if err = json.Unmarshal(values[key], dst); err != nil {
145 return Specialist{}, s.deps.Invalid
146 }
147 }
148 row.TemplateVersion = diff.To.Version
149 raw, _ := json.Marshal(diff.Suggested)
150 row.TemplateBase = string(raw)
151 return s.update(actor, row, in, m, q)
152}
153func equalDiff(a, b TemplateDiff) bool {
154 if a.From != b.From || a.To != b.To || a.CurrentRevision != b.CurrentRevision {
155 return false
156 }
157 for _, pair := range [][2]map[string]json.RawMessage{{a.Old, b.Old}, {a.Current, b.Current}, {a.Suggested, b.Suggested}} {
158 if len(pair[0]) != len(pair[1]) {
159 return false
160 }
161 for key, v := range pair[0] {
162 var x, y any
163 if json.Unmarshal(v, &x) != nil || json.Unmarshal(pair[1][key], &y) != nil || !reflect.DeepEqual(x, y) {
164 return false
165 }
166 }
167 }
168 return true
169}
170