| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "slices" |
| 6 | "strings" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | // orderedProjects shares the person's star order and the team's recent work |
| 13 | // between the list, deterministic placement choices and Coordinator context. |
| 14 | func (t *txn) orderedProjects(person string) ([]projectRecord, error) { |
| 15 | var rows []projectRecord |
| 16 | err := t.db.Model(&projectRecord{}).Select("chat_projects.*"). |
| 17 | Joins("LEFT JOIN chat_project_stars s ON s.project_id = chat_projects.id AND s.person = ?", person). |
| 18 | Where("chat_projects.state = ?", wire.ProjectActive). |
| 19 | Order("coalesce(s.starred, 0) DESC, coalesce((SELECT max(updated_at) FROM chat_tasks WHERE project_id = chat_projects.id), chat_projects.updated_at) DESC, chat_projects.repo").Find(&rows).Error |
| 20 | return rows, err |
| 21 | } |
| 22 | |
| 23 | func (t *txn) projectListExtras(person string, h wire.Hello, out *wire.ProjectList) error { |
| 24 | me, err := t.s.authority.Me(t.db, person) |
| 25 | if err != nil { |
| 26 | return err |
| 27 | } |
| 28 | state := "unavailable" |
| 29 | if len(h.Repos) > 0 || h.Agent != "" { |
| 30 | state = "connected" |
| 31 | } |
| 32 | out.Connections = []wire.ProjectConnection{{ID: "amadan", Forge: "amadan", Title: t.s.authority.Copy("projects.amadan"), State: state, CanAdd: me.Role == "owner"}} |
| 33 | stars, err := t.stars(person) |
| 34 | if err != nil { |
| 35 | return err |
| 36 | } |
| 37 | rows, err := t.orderedProjects(person) |
| 38 | if err != nil { |
| 39 | return err |
| 40 | } |
| 41 | have := map[string]bool{} |
| 42 | ordered := make([]wire.Project, 0, len(rows)) |
| 43 | for _, p := range rows { |
| 44 | for _, v := range out.Items { |
| 45 | if v.ID == p.ID { |
| 46 | v.Starred = stars[p.ID].Starred |
| 47 | ordered = append(ordered, v) |
| 48 | have[v.Repo] = true |
| 49 | break |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | out.Items = ordered |
| 54 | out.CanAdd = []wire.ReachableProject{} |
| 55 | for _, repo := range out.Allowed { |
| 56 | if have[repo] { |
| 57 | continue |
| 58 | } |
| 59 | var info *wire.RepoInfo |
| 60 | for i := range h.Catalog { |
| 61 | if h.Catalog[i].Repo == repo { |
| 62 | info = &h.Catalog[i] |
| 63 | break |
| 64 | } |
| 65 | } |
| 66 | v := wire.ReachableProject{Repo: repo, Name: ProjectName(repo), Title: ProjectTitle(repo), Forge: "amadan", ConnectionID: "amadan", DescriptionSource: "repository"} |
| 67 | if info != nil { |
| 68 | v.Description = clipText(info.Summary, 500) |
| 69 | } |
| 70 | out.CanAdd = append(out.CanAdd, v) |
| 71 | } |
| 72 | return nil |
| 73 | } |
| 74 | |
| 75 | // projectBlock is rebuilt at dispatch, including retries. Every value is a |
| 76 | // JSON string: embedded newlines and apparent prompt instructions stay data. |
| 77 | func (t *txn) projectBlock(person string) (string, error) { |
| 78 | rows, err := t.orderedProjects(person) |
| 79 | if err != nil { |
| 80 | return "", err |
| 81 | } |
| 82 | stars, err := t.stars(person) |
| 83 | if err != nil { |
| 84 | return "", err |
| 85 | } |
| 86 | before := t.viewer |
| 87 | t.viewer = person |
| 88 | defer func() { t.viewer = before }() |
| 89 | var b strings.Builder |
| 90 | if t.s.authority.Copy != nil { |
| 91 | b.WriteString(t.s.authority.Copy("projects.context")) |
| 92 | } |
| 93 | for i, p := range rows { |
| 94 | if i == 30 { |
| 95 | break |
| 96 | } |
| 97 | v, err := t.projectView(p) |
| 98 | if err != nil { |
| 99 | return "", err |
| 100 | } |
| 101 | names := append(slices.Clone(v.Names), v.PersonalNames...) |
| 102 | if len(names) > 24 { |
| 103 | names = names[:24] |
| 104 | } |
| 105 | for i := range names { |
| 106 | names[i] = clipText(runnerproto.RedactText(names[i], nil), 120) |
| 107 | } |
| 108 | line := struct { |
| 109 | Title string `json:"title"` |
| 110 | TitleSource string `json:"title_source"` |
| 111 | Repo string `json:"repo"` |
| 112 | Names []string `json:"trusted_names"` |
| 113 | Starred bool `json:"starred"` |
| 114 | DescriptionSource string `json:"description_source"` |
| 115 | Description string `json:"description"` |
| 116 | }{clipText(runnerproto.RedactText(v.Title, nil), 120), v.TitleSource, v.Repo, names, stars[p.ID].Starred, v.DescriptionSource, clipText(runnerproto.RedactText(v.Description, nil), 500)} |
| 117 | raw, _ := json.Marshal(line) |
| 118 | b.WriteByte('\n') |
| 119 | b.Write(raw) |
| 120 | } |
| 121 | return b.String(), nil |
| 122 | } |
| 123 | |