| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | "time" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/wire" |
| 9 | ) |
| 10 | |
| 11 | // lookupWindow is how far back an earlier lookup question may come from. |
| 12 | const lookupWindow = 30 * time.Minute |
| 13 | |
| 14 | // lookupPack is what the query writer sees (spec 2026-11-26-assistant- |
| 15 | // abilities §4.2 step 1): this turn's newest composer message in the |
| 16 | // Assistant lane and, only when it is a follow-up (isFollowUp), up to 2 |
| 17 | // earlier composer messages from turns that ran a lookup (shown or |
| 18 | // failed) in the last 30 minutes, newest first, never from before the |
| 19 | // person's last reset; the second only when the first was a follow-up |
| 20 | // itself. The city and date are the person's zone's (AD7). It is cut to |
| 21 | // 4 KiB, the oldest earlier question first. With no composer message in |
| 22 | // the turn there is no pack (ok false). |
| 23 | func (t *txn) lookupPack(person string, turn turnRecord, now time.Time) (wire.LookupPack, bool, error) { |
| 24 | var pack wire.LookupPack |
| 25 | mine, err := t.newestComposer(person, turn.ID) |
| 26 | if err != nil || mine.ID == "" { |
| 27 | return pack, false, err |
| 28 | } |
| 29 | pack.Question = mine.Text |
| 30 | // The person's own zone, read for the pack: with none known (or one |
| 31 | // that can't be read) no city is made up, and the date is UTC's. |
| 32 | var z zoneRecord |
| 33 | if err := t.db.Where("person = ?", person).Limit(1).Find(&z).Error; err != nil { |
| 34 | return pack, false, err |
| 35 | } |
| 36 | loc, known := time.UTC, false |
| 37 | if z.Zone != "" { |
| 38 | if l, ok := LoadZone(z.Zone); ok { |
| 39 | loc, known = l, true |
| 40 | } |
| 41 | } |
| 42 | pack.Date = now.In(loc).Format("2006-01-02, Monday") |
| 43 | if known { |
| 44 | pack.City = zoneCity(z.Zone) |
| 45 | } |
| 46 | if isFollowUp(mine.Text) { |
| 47 | since := now.Add(-lookupWindow) |
| 48 | var reset []time.Time |
| 49 | if err := t.db.Model(&commandRecord{}).Where("person = ? AND kind = ?", person, wire.FrameReset).Order("created_at DESC").Limit(1).Pluck("created_at", &reset).Error; err != nil { |
| 50 | return pack, false, err |
| 51 | } |
| 52 | if len(reset) == 1 && reset[0].After(since) { |
| 53 | since = reset[0] |
| 54 | } |
| 55 | // Each earlier lookup turn's question is its newest composer |
| 56 | // message, as its own pack's was: never another message batched |
| 57 | // into that turn. |
| 58 | var earlier []messageRecord |
| 59 | if err := t.db.Where("person = ? AND role = ? AND origin = ? AND lane = ? AND seq < ? AND created_at >= ? AND turn_id <> ? AND turn_id IN (SELECT turn_id FROM chat_lookups WHERE person = ? AND state IN ?) AND seq = (SELECT max(m2.seq) FROM chat_messages m2 WHERE m2.turn_id = chat_messages.turn_id AND m2.person = chat_messages.person AND m2.role = chat_messages.role AND m2.origin = chat_messages.origin AND m2.lane = chat_messages.lane)", person, wire.RolePerson, originComposer, wire.LaneAssistant, mine.Seq, since, turn.ID, person, []string{lookupShown, lookupFailed}).Order("seq DESC").Limit(2).Find(&earlier).Error; err != nil { |
| 60 | return pack, false, err |
| 61 | } |
| 62 | for i, m := range earlier { |
| 63 | pack.Earlier = append(pack.Earlier, m.Text) |
| 64 | if i == 0 && !isFollowUp(m.Text) { |
| 65 | break |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | for packSize(pack) > wire.MaxLookupPack && len(pack.Earlier) > 0 { |
| 70 | pack.Earlier = pack.Earlier[:len(pack.Earlier)-1] |
| 71 | } |
| 72 | if len(pack.Earlier) == 0 { |
| 73 | pack.Earlier = nil |
| 74 | } |
| 75 | for packSize(pack) > wire.MaxLookupPack && pack.Question != "" { |
| 76 | pack.Question = cutRunes(pack.Question, len(pack.Question)-(packSize(pack)-wire.MaxLookupPack)-8) |
| 77 | } |
| 78 | return pack, true, nil |
| 79 | } |
| 80 | |
| 81 | // newestComposer is the turn's newest composer message in the person's |
| 82 | // Assistant lane: the lookup's question, whole. |
| 83 | func (t *txn) newestComposer(person, turn string) (messageRecord, error) { |
| 84 | var m messageRecord |
| 85 | err := t.db.Where("turn_id = ? AND person = ? AND role = ? AND origin = ? AND lane = ?", turn, person, wire.RolePerson, originComposer, wire.LaneAssistant).Order("seq DESC").Limit(1).Find(&m).Error |
| 86 | return m, err |
| 87 | } |
| 88 | |
| 89 | func packSize(p wire.LookupPack) int { |
| 90 | raw, _ := json.Marshal(p) |
| 91 | return len(raw) |
| 92 | } |
| 93 | |
| 94 | // cutRunes is s at most n bytes, on a rune boundary. |
| 95 | func cutRunes(s string, n int) string { |
| 96 | if n <= 0 { |
| 97 | return "" |
| 98 | } |
| 99 | if len(s) <= n { |
| 100 | return s |
| 101 | } |
| 102 | for n > 0 && n < len(s) && s[n]&0xc0 == 0x80 { |
| 103 | n-- |
| 104 | } |
| 105 | return s[:n] |
| 106 | } |
| 107 | |
| 108 | // zoneCity is a zone's city (AD7): its last path element with |
| 109 | // underscores as spaces ("Europe/Dublin" is "Dublin"); none for UTC and |
| 110 | // Etc/*. |
| 111 | func zoneCity(zone string) string { |
| 112 | if zone == "" || zone == "UTC" || zone == "Local" || strings.HasPrefix(zone, "Etc/") || !strings.Contains(zone, "/") { |
| 113 | return "" |
| 114 | } |
| 115 | return strings.ReplaceAll(zone[strings.LastIndex(zone, "/")+1:], "_", " ") |
| 116 | } |
| 117 | |