|
| 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 | +typeQueue[Tany]struct { |
| 14 | +cond*sync.Cond |
| 15 | +items []T |
| 16 | +mu sync.Mutex |
| 17 | +sizeint |
| 18 | +closedbool |
| 19 | +predfunc(xT) (T,bool) |
| 20 | +} |
| 21 | + |
| 22 | +// NewQueue creates a queue with the given size. |
| 23 | +funcNewQueue[Tany](sizeint)*Queue[T] { |
| 24 | +q:=&Queue[T]{ |
| 25 | +items:make([]T,0,size), |
| 26 | +size:size, |
| 27 | +} |
| 28 | +q.cond=sync.NewCond(&q.mu) |
| 29 | +returnq |
| 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(predfunc(xT) (T,bool))*Queue[T] { |
| 35 | +q.pred=pred |
| 36 | +returnq |
| 37 | +} |
| 38 | + |
| 39 | +// Close aborts any pending pops and makes future pushes error. |
| 40 | +func (q*Queue[T])Close() { |
| 41 | +q.mu.Lock() |
| 42 | +deferq.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(xT)error { |
| 49 | +q.mu.Lock() |
| 50 | +deferq.mu.Unlock() |
| 51 | +ifq.closed { |
| 52 | +returnxerrors.New("queue has been closed") |
| 53 | +} |
| 54 | +// Potentially mutate or skip the push using the predicate. |
| 55 | +ifq.pred!=nil { |
| 56 | +varokbool |
| 57 | +x,ok=q.pred(x) |
| 58 | +if!ok { |
| 59 | +returnnil |
| 60 | +} |
| 61 | +} |
| 62 | +// Remove the first item from the queue if it has gotten too big. |
| 63 | +iflen(q.items)>=q.size { |
| 64 | +q.items=q.items[1:] |
| 65 | +} |
| 66 | +q.items=append(q.items,x) |
| 67 | +q.cond.Broadcast() |
| 68 | +returnnil |
| 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 | +varheadT |
| 75 | +q.mu.Lock() |
| 76 | +deferq.mu.Unlock() |
| 77 | +forlen(q.items)==0&&!q.closed { |
| 78 | +q.cond.Wait() |
| 79 | +} |
| 80 | +ifq.closed { |
| 81 | +returnhead,false |
| 82 | +} |
| 83 | +head,q.items=q.items[0],q.items[1:] |
| 84 | +returnhead,true |
| 85 | +} |
| 86 | + |
| 87 | +func (q*Queue[T])Len()int { |
| 88 | +q.mu.Lock() |
| 89 | +deferq.mu.Unlock() |
| 90 | +returnlen(q.items) |
| 91 | +} |
| 92 | + |
| 93 | +typereportTaskstruct { |
| 94 | +linkstring |
| 95 | +messageIDint64 |
| 96 | +selfReportedbool |
| 97 | +state codersdk.WorkspaceAppStatusState |
| 98 | +summarystring |
| 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 | +typeStatusQueuestruct { |
| 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 | +lastMessageIDint64 |
| 111 | +} |
| 112 | + |
| 113 | +func (q*StatusQueue)Push(reportreportTask)error { |
| 114 | +q.mu.Lock() |
| 115 | +deferq.mu.Unlock() |
| 116 | +ifq.closed { |
| 117 | +returnxerrors.New("queue has been closed") |
| 118 | +} |
| 119 | +varlastReportreportTask |
| 120 | +iflen(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 | +ifreport.messageID>q.lastMessageID { |
| 135 | +report.state=codersdk.WorkspaceAppStatusStateWorking |
| 136 | +}elseifreport.state==codersdk.WorkspaceAppStatusStateWorking&&!report.selfReported { |
| 137 | +q.mu.Unlock() |
| 138 | +returnnil |
| 139 | +} |
| 140 | +// Preserve previous message and URI if there was no message. |
| 141 | +ifreport.summary=="" { |
| 142 | +report.summary=lastReport.summary |
| 143 | +ifreport.link=="" { |
| 144 | +report.link=lastReport.link |
| 145 | +} |
| 146 | +} |
| 147 | +// Avoid queueing duplicate updates. |
| 148 | +ifreport.state==lastReport.state&& |
| 149 | +report.link==lastReport.link&& |
| 150 | +report.summary==lastReport.summary { |
| 151 | +returnnil |
| 152 | +} |
| 153 | +// Drop the first item if the queue has gotten too big. |
| 154 | +iflen(q.items)>=q.size { |
| 155 | +q.items=q.items[1:] |
| 156 | +} |
| 157 | +q.items=append(q.items,report) |
| 158 | +q.cond.Broadcast() |
| 159 | +returnnil |
| 160 | +} |