oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package chatstate
2
3import (
4 "amadan.net/oficina/equipo/internal/equipo/ownermode"
5 "errors"
6 "gorm.io/gorm"
7)
8
9type Card struct {
10 Thinking bool `json:",omitempty"`
11 ConnectionLabel string
12 PartialText string `json:",omitempty"`
13 Sequence int64 `json:"-"`
14 ActionKeys []string `json:",omitempty"`
15 CopyKey, WaitingTaskID, WaitingTaskURL string `json:",omitempty"`
16 ID, Kind, ResourceID, TaskID string
17 Revision int64
18}
19
20func Status(tx *gorm.DB, binding, person string, generation int64) ([]Card, error) {
21 var turns []struct {
22 ID, State, Claim, PartialText, ReplyMessageID string
23 AttemptID, Revision, Sequence int64
24 }
25 if err := tx.Table("coordinator_turns").Where("binding_id=? AND generation=? AND state NOT IN ('delivered','superseded')", binding, generation).Order("sequence DESC").Limit(50).Find(&turns).Error; err != nil {
26 return nil, err
27 }
28 // Messages are visible immediately, before the runner's next poll queues them.
29 var pending []struct {
30 ID string
31 Sequence int64
32 }
33 if err := tx.Table("conversation_messages m").Select("m.id,m.sequence").Joins("JOIN conversations c ON c.id=m.conversation_id").Where("c.person=? AND m.author_kind='person' AND NOT EXISTS (SELECT 1 FROM coordinator_turns t WHERE t.message_id=m.id AND (t.state='delivered' OR t.generation=? OR t.superseded_by IN (SELECT id FROM coordinator_turns WHERE state='delivered')))", person, generation).Order("m.sequence DESC").Limit(1).Scan(&pending).Error; err != nil {
34 return nil, err
35 }
36 var cards []Card
37 queuedSequence := int64(-1)
38 if len(pending) > 0 {
39 queuedSequence = pending[0].Sequence
40 cards = append(cards, Card{ID: pending[0].ID, Kind: "coordinator", Thinking: true, Sequence: queuedSequence})
41 }
42 for _, turn := range turns {
43 if turn.State == "queued" {
44 if queuedSequence >= turn.Sequence {
45 continue
46 }
47 queuedSequence = turn.Sequence
48 }
49 if turn.State == "waiting_connection" || turn.State == "waiting_task" || turn.State == "admission_denied" {
50 var connection struct{ Label, LocalID string }
51 if err := tx.Table("connections c").Select("c.label,c.local_id").Joins("JOIN coordinator_bindings b ON b.connection_id=c.id").Where("b.id=? AND c.person=?", binding, person).Take(&connection).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
52 return nil, err
53 }
54 label := connection.Label
55 if label == "" {
56 label = connection.LocalID
57 }
58 cards = append(cards, Card{ID: turn.ID, Kind: "coordinator", ResourceID: turn.ID, Revision: turn.Revision, Thinking: true, ConnectionLabel: label, Sequence: turn.Sequence})
59 continue
60 }
61 state, actions, show, err := ownermode.TurnStatus(tx, turn.State, turn.AttemptID, turn.Claim)
62 if err != nil {
63 return nil, err
64 }
65 if !show && (turn.State == "queued" || turn.State == "claimed" || turn.State == "running") && turn.ReplyMessageID == "" {
66 cards = append(cards, Card{ID: turn.ID, Kind: "coordinator", ResourceID: turn.ID, Revision: turn.Revision, Thinking: true, PartialText: turn.PartialText, Sequence: turn.Sequence})
67 }
68 if show {
69 cards = append(cards, Card{ID: turn.ID, Kind: "coordinator", ResourceID: turn.ID, Revision: turn.Revision, CopyKey: "coordinator." + state, ActionKeys: actions})
70 }
71 }
72 return cards, nil
73}
74