Skip to content

Commit 33ebd9d

Browse files
committed
remove event ids, update event tests to account for screen updates
1 parent d4e1aed commit 33ebd9d

File tree

2 files changed

+5
-19
lines changed

2 files changed

+5
-19
lines changed

lib/httpapi/events.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ type ScreenUpdateBody struct {
5252
}
5353

5454
type Event struct {
55-
// Id's are monotonically increasing integers within a single subscription.
56-
// They are not globally unique.
57-
Id int
5855
Type EventType
5956
Payload any
6057
}
@@ -64,7 +61,6 @@ type EventEmitter struct {
6461
messages []st.ConversationMessage
6562
status AgentStatus
6663
chans map[int]chan Event
67-
chanEventIdx map[int]int
6864
chanIdx int
6965
subscriptionBufSize int
7066
screen string
@@ -94,7 +90,6 @@ func NewEventEmitter(subscriptionBufSize int) *EventEmitter {
9490
messages: make([]st.ConversationMessage, 0),
9591
status: AgentStatusRunning,
9692
chans: make(map[int]chan Event),
97-
chanEventIdx: make(map[int]int),
9893
chanIdx: 0,
9994
subscriptionBufSize: subscriptionBufSize,
10095
}
@@ -109,11 +104,9 @@ func (e *EventEmitter) notifyChannels(eventType EventType, payload any) {
109104
for _, chanId := range chanIds {
110105
ch := e.chans[chanId]
111106
event := Event{
112-
Id: e.chanEventIdx[chanId],
113107
Type: eventType,
114108
Payload: payload,
115109
}
116-
e.chanEventIdx[chanId]++
117110

118111
select {
119112
case ch <- event:
@@ -182,20 +175,17 @@ func (e *EventEmitter) UpdateScreenAndEmitChanges(newScreen string) {
182175
// Assumes the caller holds the lock.
183176
func (e *EventEmitter) currentStateAsEvents() []Event {
184177
events := make([]Event, 0, len(e.messages)+2)
185-
for i, msg := range e.messages {
178+
for _, msg := range e.messages {
186179
events = append(events, Event{
187-
Id: i,
188180
Type: EventTypeMessageUpdate,
189181
Payload: MessageUpdateBody{Id: msg.Id, Role: msg.Role, Message: msg.Message, Time: msg.Time},
190182
})
191183
}
192184
events = append(events, Event{
193-
Id: len(e.messages),
194185
Type: EventTypeStatusChange,
195186
Payload: StatusChangeBody{Status: e.status},
196187
})
197188
events = append(events, Event{
198-
Id: len(e.messages) + 1,
199189
Type: EventTypeScreenUpdate,
200190
Payload: ScreenUpdateBody{Screen: strings.TrimRight(e.screen, mf.WhiteSpaceChars)},
201191
})
@@ -214,7 +204,6 @@ func (e *EventEmitter) Subscribe() (int, <-chan Event, []Event) {
214204
// Once a channel becomes full, it will be closed.
215205
ch := make(chan Event, e.subscriptionBufSize)
216206
e.chans[e.chanIdx] = ch
217-
e.chanEventIdx[e.chanIdx] = len(stateEvents)
218207
e.chanIdx++
219208
return e.chanIdx - 1, ch, stateEvents
220209
}
@@ -223,7 +212,6 @@ func (e *EventEmitter) Subscribe() (int, <-chan Event, []Event) {
223212
func (e *EventEmitter) unsubscribeInner(chanId int) {
224213
close(e.chans[chanId])
225214
delete(e.chans, chanId)
226-
delete(e.chanEventIdx, chanId)
227215
}
228216

229217
func (e *EventEmitter) Unsubscribe(chanId int) {

lib/httpapi/events_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ func TestEventEmitter(t *testing.T) {
1616
assert.Empty(t, ch)
1717
assert.Equal(t, []Event{
1818
{
19-
Id: 0,
2019
Type: EventTypeStatusChange,
2120
Payload: StatusChangeBody{Status: AgentStatusRunning},
2221
},
22+
{
23+
Type: EventTypeScreenUpdate,
24+
Payload: ScreenUpdateBody{Screen: ""},
25+
},
2326
}, stateEvents)
2427

2528
now := time.Now()
@@ -28,7 +31,6 @@ func TestEventEmitter(t *testing.T) {
2831
})
2932
newEvent := <-ch
3033
assert.Equal(t, Event{
31-
Id: 1,
3234
Type: EventTypeMessageUpdate,
3335
Payload: MessageUpdateBody{Id: 1, Message: "Hello, world!", Role: st.ConversationRoleUser, Time: now},
3436
}, newEvent)
@@ -39,22 +41,19 @@ func TestEventEmitter(t *testing.T) {
3941
})
4042
newEvent = <-ch
4143
assert.Equal(t, Event{
42-
Id: 2,
4344
Type: EventTypeMessageUpdate,
4445
Payload: MessageUpdateBody{Id: 1, Message: "Hello, world! (updated)", Role: st.ConversationRoleUser, Time: now},
4546
}, newEvent)
4647

4748
newEvent = <-ch
4849
assert.Equal(t, Event{
49-
Id: 3,
5050
Type: EventTypeMessageUpdate,
5151
Payload: MessageUpdateBody{Id: 2, Message: "What's up?", Role: st.ConversationRoleAgent, Time: now},
5252
}, newEvent)
5353

5454
emitter.UpdateStatusAndEmitChanges(st.ConversationStatusStable)
5555
newEvent = <-ch
5656
assert.Equal(t, Event{
57-
Id: 4,
5857
Type: EventTypeStatusChange,
5958
Payload: StatusChangeBody{Status: AgentStatusStable},
6059
}, newEvent)
@@ -75,7 +74,6 @@ func TestEventEmitter(t *testing.T) {
7574
for _, ch := range channels {
7675
newEvent := <-ch
7776
assert.Equal(t, Event{
78-
Id: 1,
7977
Type: EventTypeMessageUpdate,
8078
Payload: MessageUpdateBody{Id: 1, Message: "Hello, world!", Role: st.ConversationRoleUser, Time: now},
8179
}, newEvent)

0 commit comments

Comments
 (0)