| 1 | package conversation |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/rand" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "net/http" |
| 9 | "strconv" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/equipo/planning" |
| 12 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 13 | "gorm.io/gorm" |
| 14 | ) |
| 15 | |
| 16 | type CoordinatorConnection struct { |
| 17 | ID, RunnerID, Label string |
| 18 | Available bool |
| 19 | } |
| 20 | type CoordinatorPage struct { |
| 21 | Binding CoordinatorBinding |
| 22 | Connections []CoordinatorConnection |
| 23 | Limits team.Limits |
| 24 | LimitsRevision int64 |
| 25 | BudgetMissing, Owner bool |
| 26 | RequestID, ResetID, LimitsID, SettingsID string |
| 27 | Settings PersonalSettings |
| 28 | } |
| 29 | |
| 30 | func (s *Application) CoordinatorPage(ctx context.Context, a Actor) (out CoordinatorPage, err error) { |
| 31 | a.Address = s.Normalize(a.Address) |
| 32 | out.RequestID, out.ResetID, out.LimitsID, out.SettingsID = rand.Text(), rand.Text(), rand.Text(), rand.Text() |
| 33 | err = s.Write(ctx, func(tx *gorm.DB) error { |
| 34 | cs := s.Conversation(tx) |
| 35 | teamID, e := cs.deps.Authorize(tx, a) |
| 36 | if e != nil { |
| 37 | return e |
| 38 | } |
| 39 | b, e := cs.Binding(a) |
| 40 | if e != nil && !errors.Is(e, s.NotFound) { |
| 41 | return e |
| 42 | } |
| 43 | out.Binding = b.CoordinatorBinding |
| 44 | out.Owner = s.LimitsAuthority(tx, a, a.Address) == nil |
| 45 | var rows []struct{ ID, RunnerID, ReportsJSON string } |
| 46 | if e = tx.Table("connections").Select("id,runner_id,reports_json").Where("person=?", a.Address).Order("id").Find(&rows).Error; e != nil { |
| 47 | return e |
| 48 | } |
| 49 | for _, row := range rows { |
| 50 | var reports []struct{ AccountLabel string } |
| 51 | if e = json.Unmarshal([]byte(row.ReportsJSON), &reports); e != nil { |
| 52 | return e |
| 53 | } |
| 54 | label := row.ID |
| 55 | if len(reports) > 0 && reports[0].AccountLabel != "" { |
| 56 | label = reports[0].AccountLabel |
| 57 | } |
| 58 | available := false |
| 59 | memberRevision, connectionRevision, err := s.BindingIdentity(tx, a, row.RunnerID, row.ID) |
| 60 | if err == nil { |
| 61 | candidate := BindingRecord{CoordinatorBinding: CoordinatorBinding{Person: a.Address, RunnerID: row.RunnerID, ConnectionID: row.ID}, MemberRevision: memberRevision, ConnectionRevision: connectionRevision} |
| 62 | _, err = s.BindingSelection(tx, candidate) |
| 63 | available = err == nil |
| 64 | } |
| 65 | if err != nil && !errors.Is(err, s.WaitingConnection) && !errors.Is(err, s.Forbidden) && !errors.Is(err, s.NotFound) { |
| 66 | return err |
| 67 | } |
| 68 | out.Connections = append(out.Connections, CoordinatorConnection{ID: row.ID, RunnerID: row.RunnerID, Label: label, Available: available}) |
| 69 | } |
| 70 | var limits struct { |
| 71 | Revision int64 |
| 72 | Limits string |
| 73 | } |
| 74 | e = tx.Table("coordinator_limits").Where("account=? AND person=?", teamID, a.Address).Take(&limits).Error |
| 75 | if errors.Is(e, gorm.ErrRecordNotFound) { |
| 76 | out.BudgetMissing = true |
| 77 | return nil |
| 78 | } |
| 79 | if e != nil { |
| 80 | return e |
| 81 | } |
| 82 | out.LimitsRevision = limits.Revision |
| 83 | return json.Unmarshal([]byte(limits.Limits), &out.Limits) |
| 84 | }) |
| 85 | if err == nil { |
| 86 | out.Settings, err = s.PersonalSettings(ctx, a) |
| 87 | } |
| 88 | return |
| 89 | } |
| 90 | func (s *Application) AnswerClarification(ctx context.Context, a Actor, id, option, text string, m Mutation) error { |
| 91 | a.Address = s.Normalize(a.Address) |
| 92 | return s.Write(ctx, func(tx *gorm.DB) error { |
| 93 | return s.Conversation(tx).AnswerClarification(a, id, option, text, m, s.Now().UTC()) |
| 94 | }) |
| 95 | } |
| 96 | func (h *Handler) Settings(w http.ResponseWriter, r *http.Request) { |
| 97 | a, ok := h.actor(w, r) |
| 98 | if !ok { |
| 99 | return |
| 100 | } |
| 101 | v, err := h.App.CoordinatorPage(r.Context(), a) |
| 102 | if err != nil { |
| 103 | h.Failure(w, r, err) |
| 104 | return |
| 105 | } |
| 106 | h.RenderSettings(w, r, v) |
| 107 | } |
| 108 | func (h *Handler) SaveSettings(w http.ResponseWriter, r *http.Request) { |
| 109 | a, ok := h.actor(w, r) |
| 110 | if !ok { |
| 111 | return |
| 112 | } |
| 113 | m, err := planning.ReadMutation(w, r, h.App.Invalid) |
| 114 | if err == nil { |
| 115 | switch r.PostForm.Get("action") { |
| 116 | |
| 117 | case "reset": |
| 118 | _, err = h.App.ResetCoordinator(r.Context(), a, m) |
| 119 | case "retry": |
| 120 | _, err = h.App.RetryCoordinatorTurn(r.Context(), a, r.PostForm.Get("turn_id"), m) |
| 121 | case "limits": |
| 122 | var limits team.Limits |
| 123 | for key, target := range map[string]*int64{"calls": &limits.Calls, "input_tokens": &limits.InputTokens, "output_tokens": &limits.OutputTokens, "seconds": &limits.Seconds} { |
| 124 | value, e := strconv.ParseInt(r.PostForm.Get(key), 10, 64) |
| 125 | if e != nil || value <= 0 { |
| 126 | err = h.App.Invalid |
| 127 | break |
| 128 | } |
| 129 | *target = value |
| 130 | } |
| 131 | if err == nil { |
| 132 | err = h.App.SetCoordinatorLimits(r.Context(), a, a.Address, limits, m) |
| 133 | } |
| 134 | case "landing": |
| 135 | _, err = h.App.SavePersonalSettings(r.Context(), a, PersonalSettings{Landing: r.PostForm.Get("landing")}, m) |
| 136 | default: |
| 137 | err = h.App.Invalid |
| 138 | } |
| 139 | } |
| 140 | if err != nil { |
| 141 | h.Failure(w, r, err) |
| 142 | return |
| 143 | } |
| 144 | http.Redirect(w, r, "/settings/coordinator", http.StatusSeeOther) |
| 145 | } |
| 146 | |