|
| 1 | +package cliutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + |
| 6 | + "golang.org/x/xerrors" |
| 7 | + |
| 8 | + "github.com/coder/coder/v2/codersdk" |
| 9 | +) |
| 10 | + |
| 11 | +// Queue is a FIFO queue with a fixed size. If the size is exceeded, the first |
| 12 | +// item is dropped. |
| 13 | +type Queue[T any] struct { |
| 14 | + cond *sync.Cond |
| 15 | + items []T |
| 16 | + mu sync.Mutex |
| 17 | + size int |
| 18 | + closed bool |
| 19 | + pred func(x T) (T, bool) |
| 20 | +} |
| 21 | + |
| 22 | +// NewQueue creates a queue with the given size. |
| 23 | +func NewQueue[T any](size int) *Queue[T] { |
| 24 | + q := &Queue[T]{ |
| 25 | + items: make([]T, 0, size), |
| 26 | + size: size, |
| 27 | + } |
| 28 | + q.cond = sync.NewCond(&q.mu) |
| 29 | + return q |
| 30 | +} |
| 31 | + |
| 32 | +// WithPredicate adds the given predicate function, which can control what is |
| 33 | +// pushed to the queue. |
| 34 | +func (q *Queue[T]) WithPredicate(pred func(x T) (T, bool)) *Queue[T] { |
| 35 | + q.pred = pred |
| 36 | + return q |
| 37 | +} |
| 38 | + |
| 39 | +// Close aborts any pending pops and makes future pushes error. |
| 40 | +func (q *Queue[T]) Close() { |
| 41 | + q.mu.Lock() |
| 42 | + defer q.mu.Unlock() |
| 43 | + q.closed = true |
| 44 | + q.cond.Broadcast() |
| 45 | +} |
| 46 | + |
| 47 | +// Push adds an item to the queue. If closed, returns an error. |
| 48 | +func (q *Queue[T]) Push(x T) error { |
| 49 | + q.mu.Lock() |
| 50 | + defer q.mu.Unlock() |
| 51 | + if q.closed { |
| 52 | + return xerrors.New("queue has been closed") |
| 53 | + } |
| 54 | + // Potentially mutate or skip the push using the predicate. |
| 55 | + if q.pred != nil { |
| 56 | + var ok bool |
| 57 | + x, ok = q.pred(x) |
| 58 | + if !ok { |
| 59 | + return nil |
| 60 | + } |
| 61 | + } |
| 62 | + // Remove the first item from the queue if it has gotten too big. |
| 63 | + if len(q.items) >= q.size { |
| 64 | + q.items = q.items[1:] |
| 65 | + } |
| 66 | + q.items = append(q.items, x) |
| 67 | + q.cond.Broadcast() |
| 68 | + return nil |
| 69 | +} |
| 70 | + |
| 71 | +// Pop removes and returns the first item from the queue, waiting until there is |
| 72 | +// something to pop if necessary. If closed, returns false. |
| 73 | +func (q *Queue[T]) Pop() (T, bool) { |
| 74 | + var head T |
| 75 | + q.mu.Lock() |
| 76 | + defer q.mu.Unlock() |
| 77 | + for len(q.items) == 0 && !q.closed { |
| 78 | + q.cond.Wait() |
| 79 | + } |
| 80 | + if q.closed { |
| 81 | + return head, false |
| 82 | + } |
| 83 | + head, q.items = q.items[0], q.items[1:] |
| 84 | + return head, true |
| 85 | +} |
| 86 | + |
| 87 | +func (q *Queue[T]) Len() int { |
| 88 | + q.mu.Lock() |
| 89 | + defer q.mu.Unlock() |
| 90 | + return len(q.items) |
| 91 | +} |
| 92 | + |
| 93 | +type reportTask struct { |
| 94 | + link string |
| 95 | + messageID int64 |
| 96 | + selfReported bool |
| 97 | + state codersdk.WorkspaceAppStatusState |
| 98 | + summary string |
| 99 | +} |
| 100 | + |
| 101 | +// statusQueue is a Queue that: |
| 102 | +// 1. Only pushes items that are not duplicates. |
| 103 | +// 2. Preserves the existing message and URI when one a message is not provided. |
| 104 | +// 3. Ignores "working" updates from the status watcher. |
| 105 | +type StatusQueue struct { |
| 106 | + Queue[reportTask] |
| 107 | + // lastMessageID is the ID of the last *user* message that we saw. A user |
| 108 | + // message only happens when interacting via the API (as opposed to |
| 109 | + // interacting with the terminal directly). |
| 110 | + lastMessageID int64 |
| 111 | +} |
| 112 | + |
| 113 | +func (q *StatusQueue) Push(report reportTask) error { |
| 114 | + q.mu.Lock() |
| 115 | + defer q.mu.Unlock() |
| 116 | + if q.closed { |
| 117 | + return xerrors.New("queue has been closed") |
| 118 | + } |
| 119 | + var lastReport reportTask |
| 120 | + if len(q.items) > 0 { |
| 121 | + lastReport = q.items[len(q.items)-1] |
| 122 | + } |
| 123 | + // Use "working" status if this is a new user message. If this is not a new |
| 124 | + // user message, and the status is "working" and not self-reported (meaning it |
| 125 | + // came from the screen watcher), then it means one of two things: |
| 126 | + // 1. The LLM is still working, in which case our last status will already |
| 127 | + // have been "working", so there is nothing to do. |
| 128 | + // 2. The user has interacted with the terminal directly. For now, we are |
| 129 | + // ignoring these updates. This risks missing cases where the user |
| 130 | + // manually submits a new prompt and the LLM becomes active and does not |
| 131 | + // update itself, but it avoids spamming useless status updates as the user |
| 132 | + // is typing, so the tradeoff is worth it. In the future, if we can |
| 133 | + // reliably distinguish between user and LLM activity, we can change this. |
| 134 | + if report.messageID > q.lastMessageID { |
| 135 | + report.state = codersdk.WorkspaceAppStatusStateWorking |
| 136 | + } else if report.state == codersdk.WorkspaceAppStatusStateWorking && !report.selfReported { |
| 137 | + q.mu.Unlock() |
| 138 | + return nil |
| 139 | + } |
| 140 | + // Preserve previous message and URI if there was no message. |
| 141 | + if report.summary == "" { |
| 142 | + report.summary = lastReport.summary |
| 143 | + if report.link == "" { |
| 144 | + report.link = lastReport.link |
| 145 | + } |
| 146 | + } |
| 147 | + // Avoid queueing duplicate updates. |
| 148 | + if report.state == lastReport.state && |
| 149 | + report.link == lastReport.link && |
| 150 | + report.summary == lastReport.summary { |
| 151 | + return nil |
| 152 | + } |
| 153 | + // Drop the first item if the queue has gotten too big. |
| 154 | + if len(q.items) >= q.size { |
| 155 | + q.items = q.items[1:] |
| 156 | + } |
| 157 | + q.items = append(q.items, report) |
| 158 | + q.cond.Broadcast() |
| 159 | + return nil |
| 160 | +} |
0 commit comments