@@ -117,13 +117,17 @@ func handleAttachment(
117117 }); err != nil {
118118 return
119119 }
120+ service .AgentConnected (request .Context (), agent )
121+ defer service .AgentDisconnected (context .Background (), agent )
120122
121123 ctx , cancel := context .WithCancel (request .Context ())
122124 defer cancel ()
123125
124126 readErrCh := make (chan error , 1 )
125127 go func () {
126- readErrCh <- consumeAttachmentFrames (ctx , connection , writer , session , attachmentReadTimeout ())
128+ readErrCh <- consumeAttachmentFrames (ctx , connection , writer , session , attachmentReadTimeout (), func (event protocol.Event ) {
129+ service .AgentWakeDelivered (context .Background (), agent , event )
130+ })
127131 }()
128132
129133 heartbeatTicker := time .NewTicker (attachmentHeartbeatInterval () / 2 )
@@ -140,6 +144,7 @@ func handleAttachment(
140144 observability .Logger (request .Context (), "transport.attach" , "agent_id" , agent .ID ).
141145 Warn ("attachment websocket read error" , "error" , err )
142146 }
147+ publishPendingWakeFailures (service , agent , session , err )
143148 return
144149 case <- heartbeatTicker .C :
145150 if err := writer .write (protocol.AttachmentFrame {
@@ -156,6 +161,9 @@ func handleAttachment(
156161 continue
157162 }
158163 session .NoteSent (event .ID )
164+ if attachmentWakeEvent (event , service .Network ().ID , agent ) {
165+ session .NoteWakeSent (event .ID , event )
166+ }
159167
160168 if err := writer .write (protocol.AttachmentFrame {
161169 Op : protocol .AttachmentOpEvent ,
@@ -164,12 +172,19 @@ func handleAttachment(
164172 Cursor : event .ID ,
165173 Event : & event ,
166174 }); err != nil {
175+ publishPendingWakeFailures (service , agent , session , err )
167176 return
168177 }
169178 }
170179 }
171180}
172181
182+ func publishPendingWakeFailures (service Service , agent protocol.Actor , session * attachmentSession , err error ) {
183+ for _ , event := range session .PendingWakes () {
184+ service .AgentWakeFailed (context .Background (), agent , event , err )
185+ }
186+ }
187+
173188type attachmentWriter struct {
174189 connection * websocket.Conn
175190 mu sync.Mutex
@@ -253,6 +268,22 @@ func attachedAgentMessage(message *protocol.Message, networkID string, agentID s
253268 return false
254269}
255270
271+ func attachmentWakeEvent (event protocol.Event , networkID string , agent protocol.Actor ) bool {
272+ if event .Type != protocol .EventTypeMessageCreated || event .Message == nil {
273+ return false
274+ }
275+ message := event .Message
276+ if message .Target .Kind == protocol .TargetKindDM {
277+ return participantsIncludeAttachedAgent (message .Target .ParticipantIDs , networkID , agent .ID )
278+ }
279+ for _ , mention := range message .Mentions {
280+ if protocol .ActorMatches (networkID , agent .ID , mention ) || mention == agent .Name {
281+ return true
282+ }
283+ }
284+ return false
285+ }
286+
256287func participantsIncludeAttachedAgent (participants []string , networkID string , agentID string ) bool {
257288 for _ , participantID := range participants {
258289 if protocol .ActorMatches (networkID , agentID , participantID ) {
@@ -268,6 +299,7 @@ func consumeAttachmentFrames(
268299 writer * attachmentWriter ,
269300 session * attachmentSession ,
270301 readTimeout time.Duration ,
302+ onWakeAck func (protocol.Event ),
271303) error {
272304 for {
273305 select {
@@ -292,12 +324,16 @@ func consumeAttachmentFrames(
292324
293325 switch frame .Op {
294326 case protocol .AttachmentOpAck :
295- if ! session .Ack (frame .Cursor ) {
327+ event , wake , ok := session .Ack (frame .Cursor )
328+ if ! ok {
296329 if err := writeAttachmentError (writer , "unexpected ACK cursor" ); err != nil {
297330 return err
298331 }
299332 return fmt .Errorf ("unexpected ACK cursor %q" , frame .Cursor )
300333 }
334+ if wake && onWakeAck != nil {
335+ onWakeAck (event )
336+ }
301337 case protocol .AttachmentOpPong :
302338 continue
303339 case protocol .AttachmentOpPing :
0 commit comments