oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package chat
2
3import (
4 "encoding/json"
5 "regexp"
6 "strings"
7
8 "amadan.net/oficina/equipo/internal/wire"
9 "gorm.io/gorm"
10)
11
12// What a reply shows of its turn's lookups (spec 2026-11-26-assistant-
13// abilities §4.6): every lookup the turn's final attempt showed, in call
14// order, at most 2, each its answer (links taken out) and its sources. A
15// lookup from an earlier attempt is never shown.
16
17// withLookups sets each Assistant reply's lookups.
18func withLookups(tx *gorm.DB, views []wire.Message) error {
19 for i := range views {
20 if views[i].Role != wire.RoleAssistant || views[i].TurnID == "" {
21 continue
22 }
23 var attempt []int64
24 if err := tx.Model(&turnRecord{}).Where("id = ?", views[i].TurnID).Pluck("attempt", &attempt).Error; err != nil {
25 return err
26 }
27 if len(attempt) != 1 {
28 continue
29 }
30 got, err := lookupsFor(tx, views[i].TurnID, attempt[0])
31 if err != nil {
32 return err
33 }
34 views[i].Lookups = got
35 }
36 return nil
37}
38
39// lookupsFor are the lookups turn's attempt showed, in call order, at
40// most MaxLookupsPerReply.
41func lookupsFor(tx *gorm.DB, turn string, attempt int64) ([]wire.AssistantLookup, error) {
42 var rows []lookupRecord
43 if err := tx.Where("turn_id = ? AND attempt = ? AND state = ?", turn, attempt, lookupShown).Order("seq").Limit(wire.MaxLookupsPerReply).Find(&rows).Error; err != nil {
44 return nil, err
45 }
46 var out []wire.AssistantLookup
47 for _, r := range rows {
48 var sources []wire.LookupSource
49 if json.Unmarshal([]byte(r.Sources), &sources) != nil || len(sources) == 0 {
50 continue
51 }
52 out = append(out, wire.AssistantLookup{Answer: stripLinks(r.Answer), Sources: sources})
53 }
54 return out, nil
55}
56
57var (
58 mdLink = regexp.MustCompile(`!?\[([^\]\n]*)\]\([^)\n]*\)`)
59 autoLink = regexp.MustCompile(`<(?:https?|ftp|mailto):[^>\s]*>`)
60 bareLink = regexp.MustCompile(`(?i)\b(?:https?|ftp)://[^\s<>()]+`)
61)
62
63// stripLinks takes the links out of an answer: a Markdown link becomes
64// its text; an address goes. The only links under a reply are the
65// sources the instance recorded.
66func stripLinks(s string) string {
67 s = mdLink.ReplaceAllString(s, "$1")
68 s = autoLink.ReplaceAllString(s, "")
69 s = bareLink.ReplaceAllString(s, "")
70 return strings.TrimSpace(s)
71}
72
73// plainLookups is m for a client that can't draw answers (§4.6): each
74// answer after the text, then a "Sources:" line with the addresses.
75// Presentation only, at read time: never stored, never the Assistant's.
76func plainLookups(m wire.Message, sourcesLine string) wire.Message {
77 if len(m.Lookups) == 0 {
78 return m
79 }
80 parts := []string{}
81 if t := strings.TrimSpace(m.Text); t != "" {
82 parts = append(parts, t)
83 }
84 for _, l := range m.Lookups {
85 var urls []string
86 for _, s := range l.Sources {
87 urls = append(urls, s.URL)
88 }
89 parts = append(parts, l.Answer+"\n\n"+sourcesLine+" "+strings.Join(urls, " "))
90 }
91 m.Text, m.Lookups = strings.Join(parts, "\n\n"), nil
92 return m
93}
94