| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "cmp" |
| 5 | "context" |
| 6 | "fmt" |
| 7 | "slices" |
| 8 | |
| 9 | "gorm.io/gorm" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // ConnLabel is how one connection is named to its person, the same on |
| 15 | // every page and card that names it: the Connections page, the Usage |
| 16 | // page, advice and the scheduler's hold lines (Paul, 2026-11-12: "Claude" |
| 17 | // beside "Claude (paul@tito.io)" looked alike). |
| 18 | type ConnLabel struct { |
| 19 | // ID is the connection's (its Connections row is |
| 20 | // #connection-<id>). |
| 21 | ID string |
| 22 | // Provider is the provider's key (claude), for its mark; Name is |
| 23 | // how it is said (Claude). |
| 24 | Provider, Name string |
| 25 | // Account tells the connection apart from others of its provider: |
| 26 | // the email the provider named, else the email the person typed |
| 27 | // (Stated), else "signed in by link" for a token connection; |
| 28 | // numbered when two would still read alike. "" when nothing does. |
| 29 | Account string |
| 30 | Stated bool |
| 31 | // AddEmail: a token connection with no email, which the person can |
| 32 | // name on its Connections row. |
| 33 | AddEmail bool |
| 34 | // Named is the provider and account: Claude (paul@tito.io). Text |
| 35 | // is the whole label on one line, "(added by you)" after a typed |
| 36 | // email: Claude (paul@tito.io) (added by you). |
| 37 | Named, Text string |
| 38 | } |
| 39 | |
| 40 | // Said is the account part as one row says it: the email, with "(added |
| 41 | // by you)" when the person typed it. |
| 42 | func (l ConnLabel) Said(copy func(string) string) string { |
| 43 | if l.Stated { |
| 44 | return l.Account + " " + copy("chat.connections.you_said") |
| 45 | } |
| 46 | return l.Account |
| 47 | } |
| 48 | |
| 49 | // ProviderName is how a provider is named to a person. |
| 50 | func ProviderName(p string) string { |
| 51 | switch p { |
| 52 | case wire.ProviderClaude: |
| 53 | return "Claude" |
| 54 | case wire.ProviderCodex: |
| 55 | return "Codex" |
| 56 | } |
| 57 | if kp, ok := wire.APIKeyProviders[p]; ok { |
| 58 | return kp.Name |
| 59 | } |
| 60 | return p |
| 61 | } |
| 62 | |
| 63 | // labelled is a connection the person holds (or held): not an attempt |
| 64 | // still connecting, nor one that ended before it connected. |
| 65 | func labelled(c wire.Connection) bool { |
| 66 | return c.State != wire.ConnConnecting && !(c.State == wire.ConnError && c.LocalID == "") |
| 67 | } |
| 68 | |
| 69 | // ConnectionLabels names each of a person's connections, by id. stated |
| 70 | // is the email the person typed for a connection, by id; copy is the |
| 71 | // instance's copy. Two held connections of one provider that would read |
| 72 | // alike are numbered in the order they were made (then by id): the |
| 73 | // first keeps the plain label, so adding one never renames another; |
| 74 | // the next reads "Claude (signed in by link, 2)". |
| 75 | func ConnectionLabels(conns []wire.Connection, stated map[string]string, copy func(string) string) map[string]ConnLabel { |
| 76 | out := make(map[string]ConnLabel, len(conns)) |
| 77 | held := make([]wire.Connection, 0, len(conns)) |
| 78 | for _, c := range conns { |
| 79 | l := ConnLabel{ID: c.ID, Provider: c.Provider, Name: ProviderName(c.Provider)} |
| 80 | switch { |
| 81 | case c.Label != "": |
| 82 | l.Account = c.Label |
| 83 | case c.Kind == wire.KindToken && stated[c.ID] != "": |
| 84 | l.Account, l.Stated = stated[c.ID], true |
| 85 | case c.Kind == wire.KindToken: |
| 86 | l.Account = copy("chat.connections.signed_in_by_link") |
| 87 | l.AddEmail = c.LocalID != "" && c.State != wire.ConnSignedOut |
| 88 | } |
| 89 | out[c.ID] = l |
| 90 | if labelled(c) { |
| 91 | held = append(held, c) |
| 92 | } |
| 93 | } |
| 94 | slices.SortStableFunc(held, func(a, b wire.Connection) int { |
| 95 | return cmp.Or(a.CreatedAt.Compare(b.CreatedAt), cmp.Compare(a.ID, b.ID)) |
| 96 | }) |
| 97 | seen := map[[3]string]int{} |
| 98 | for _, c := range held { |
| 99 | l := out[c.ID] |
| 100 | key := [3]string{l.Provider, l.Account, fmt.Sprint(l.Stated)} |
| 101 | seen[key]++ |
| 102 | if n := seen[key]; n > 1 { |
| 103 | if l.Account == "" { |
| 104 | l.Account = fmt.Sprint(n) |
| 105 | } else { |
| 106 | l.Account = fmt.Sprintf(copy("chat.connections.numbered"), l.Account, n) |
| 107 | } |
| 108 | } |
| 109 | out[c.ID] = l |
| 110 | } |
| 111 | for id, l := range out { |
| 112 | l.Named = l.Name |
| 113 | if l.Account != "" { |
| 114 | l.Named = fmt.Sprintf(copy("chat.connections.named"), l.Name, l.Account) |
| 115 | } |
| 116 | l.Text = l.Named |
| 117 | if l.Stated { |
| 118 | l.Text += " " + copy("chat.connections.you_said") |
| 119 | } |
| 120 | out[id] = l |
| 121 | } |
| 122 | return out |
| 123 | } |
| 124 | |
| 125 | // LabelOf is c's label from labels, or its provider's name when c is |
| 126 | // not among them. |
| 127 | func LabelOf(labels map[string]ConnLabel, c wire.Connection) ConnLabel { |
| 128 | if l, ok := labels[c.ID]; ok { |
| 129 | return l |
| 130 | } |
| 131 | return providerLabel(c.Provider) |
| 132 | } |
| 133 | |
| 134 | // HoldLabel is the label of the connection a held step waits on, by its |
| 135 | // local id and provider (on runner, when the step has one), or the |
| 136 | // provider's name when the person holds no such connection. |
| 137 | func HoldLabel(conns []wire.Connection, labels map[string]ConnLabel, runner, local, provider string) ConnLabel { |
| 138 | for _, c := range conns { |
| 139 | if c.LocalID != "" && c.LocalID == local && c.Provider == provider && (runner == "" || c.RunnerID == runner) { |
| 140 | return LabelOf(labels, c) |
| 141 | } |
| 142 | } |
| 143 | return providerLabel(provider) |
| 144 | } |
| 145 | |
| 146 | // providerLabel names a connection by its provider alone. |
| 147 | func providerLabel(p string) ConnLabel { |
| 148 | n := ProviderName(p) |
| 149 | return ConnLabel{Provider: p, Name: n, Named: n, Text: n} |
| 150 | } |
| 151 | |
| 152 | // heldLabel is HoldLabel for the person's connections in the store. |
| 153 | func (t *txn) heldLabel(person, runner string, h *wire.StepHold) ConnLabel { |
| 154 | recs, err := t.connections(person) |
| 155 | if err != nil { |
| 156 | recs = nil |
| 157 | } |
| 158 | conns := make([]wire.Connection, 0, len(recs)) |
| 159 | for _, r := range recs { |
| 160 | conns = append(conns, connectionView(r)) |
| 161 | } |
| 162 | return HoldLabel(conns, labelsOf(recs, t.copyOf), runner, h.Connection, h.Provider) |
| 163 | } |
| 164 | |
| 165 | // copyOf is the instance's copy for key, unformatted. |
| 166 | func (t *txn) copyOf(key string) string { return t.words(key) } |
| 167 | |
| 168 | // labelsOf labels connection records, their stated emails included. |
| 169 | func labelsOf(recs []connectionRecord, copy func(string) string) map[string]ConnLabel { |
| 170 | conns := make([]wire.Connection, 0, len(recs)) |
| 171 | stated := map[string]string{} |
| 172 | for _, r := range recs { |
| 173 | conns = append(conns, connectionView(r)) |
| 174 | if r.StatedEmail != "" { |
| 175 | stated[r.ID] = r.StatedEmail |
| 176 | } |
| 177 | } |
| 178 | return ConnectionLabels(conns, stated, copy) |
| 179 | } |
| 180 | |
| 181 | // LabelledConnections reads the person's connections with their shared |
| 182 | // labels, the emails they typed included, in one read. |
| 183 | func (s *Store) LabelledConnections(ctx context.Context, person string) (out []wire.Connection, labels map[string]ConnLabel, err error) { |
| 184 | out = []wire.Connection{} |
| 185 | err = s.read(ctx, func(tx *gorm.DB) error { |
| 186 | if err := s.authority.CurrentPerson(ctx, tx); err != nil { |
| 187 | return err |
| 188 | } |
| 189 | t := &txn{s: s, db: tx} |
| 190 | rows, err := t.connections(person) |
| 191 | for _, r := range rows { |
| 192 | out = append(out, connectionView(r)) |
| 193 | } |
| 194 | labels = labelsOf(rows, t.copyOf) |
| 195 | return err |
| 196 | }) |
| 197 | return out, labels, err |
| 198 | } |
| 199 | |
| 200 | // connectionLabel is one of the person's connections' label, by its |
| 201 | // instance id; its provider's name when it cannot be read. |
| 202 | func (t *txn) connectionLabel(r connectionRecord) string { |
| 203 | recs, err := t.connections(r.Person) |
| 204 | if err != nil { |
| 205 | return ProviderName(r.Provider) |
| 206 | } |
| 207 | return LabelOf(labelsOf(recs, t.copyOf), connectionView(r)).Text |
| 208 | } |
| 209 | |