@@ -131,6 +131,7 @@ func (c *Client) SubscribeEvents(ctx context.Context, id string) (<-chan any, er
132 go func() {
132 go func() {
133 defer rsp.Body.Close()
133 defer rsp.Body.Close()
135 scr := bufio.NewReader(rsp.Body)
136 scr := bufio.NewReader(rsp.Body)
@@ -139,8 +140,15 @@ func (c *Client) SubscribeEvents(ctx context.Context, id string) (<-chan any, er
141 if err != nil {
142 if err != nil {
143+ if ctx.Err() != nil {
142 slog.Error("Reading from events stream", "error", err)
146 slog.Error("Reading from events stream", "error", err)
143− time.Sleep(time.Second * 2)
148+ case <-time.After(time.Second * 2):
146 line = bytes.TrimSpace(line)
154 line = bytes.TrimSpace(line)
@@ -166,43 +174,63 @@ func (c *Client) SubscribeEvents(ctx context.Context, id string) (<-chan any, er
166 case pubsub.PayloadTypeLSPEvent:
174 case pubsub.PayloadTypeLSPEvent:
167 var e pubsub.Event[proto.LSPEvent]
175 var e pubsub.Event[proto.LSPEvent]
168 _ = json.Unmarshal(p.Payload, &e)
176 _ = json.Unmarshal(p.Payload, &e)
169− sendEvent(ctx, events, e)
177+ if !sendEvent(ctx, events, e) {
170 case pubsub.PayloadTypeMCPEvent:
180 case pubsub.PayloadTypeMCPEvent:
171 var e pubsub.Event[proto.MCPEvent]
181 var e pubsub.Event[proto.MCPEvent]
172 _ = json.Unmarshal(p.Payload, &e)
182 _ = json.Unmarshal(p.Payload, &e)
173− sendEvent(ctx, events, e)
183+ if !sendEvent(ctx, events, e) {
174 case pubsub.PayloadTypePermissionRequest:
186 case pubsub.PayloadTypePermissionRequest:
175 var e pubsub.Event[proto.PermissionRequest]
187 var e pubsub.Event[proto.PermissionRequest]
176 _ = json.Unmarshal(p.Payload, &e)
188 _ = json.Unmarshal(p.Payload, &e)
177− sendEvent(ctx, events, e)
189+ if !sendEvent(ctx, events, e) {
178 case pubsub.PayloadTypePermissionNotification:
192 case pubsub.PayloadTypePermissionNotification:
179 var e pubsub.Event[proto.PermissionNotification]
193 var e pubsub.Event[proto.PermissionNotification]
180 _ = json.Unmarshal(p.Payload, &e)
194 _ = json.Unmarshal(p.Payload, &e)
181− sendEvent(ctx, events, e)
195+ if !sendEvent(ctx, events, e) {
182 case pubsub.PayloadTypeMessage:
198 case pubsub.PayloadTypeMessage:
183 var e pubsub.Event[proto.Message]
199 var e pubsub.Event[proto.Message]
184 _ = json.Unmarshal(p.Payload, &e)
200 _ = json.Unmarshal(p.Payload, &e)
185− sendEvent(ctx, events, e)
201+ if !sendEvent(ctx, events, e) {
186 case pubsub.PayloadTypeSession:
204 case pubsub.PayloadTypeSession:
187 var e pubsub.Event[proto.Session]
205 var e pubsub.Event[proto.Session]
188 _ = json.Unmarshal(p.Payload, &e)
206 _ = json.Unmarshal(p.Payload, &e)
189− sendEvent(ctx, events, e)
207+ if !sendEvent(ctx, events, e) {
190 case pubsub.PayloadTypeFile:
210 case pubsub.PayloadTypeFile:
191 var e pubsub.Event[proto.File]
211 var e pubsub.Event[proto.File]
192 _ = json.Unmarshal(p.Payload, &e)
212 _ = json.Unmarshal(p.Payload, &e)
193− sendEvent(ctx, events, e)
213+ if !sendEvent(ctx, events, e) {
194 case pubsub.PayloadTypeAgentEvent:
216 case pubsub.PayloadTypeAgentEvent:
195 var e pubsub.Event[proto.AgentEvent]
217 var e pubsub.Event[proto.AgentEvent]
196 _ = json.Unmarshal(p.Payload, &e)
218 _ = json.Unmarshal(p.Payload, &e)
197− sendEvent(ctx, events, e)
219+ if !sendEvent(ctx, events, e) {
198 case pubsub.PayloadTypeConfigChanged:
222 case pubsub.PayloadTypeConfigChanged:
199 var e pubsub.Event[proto.ConfigChanged]
223 var e pubsub.Event[proto.ConfigChanged]
200 _ = json.Unmarshal(p.Payload, &e)
224 _ = json.Unmarshal(p.Payload, &e)
201− sendEvent(ctx, events, e)
225+ if !sendEvent(ctx, events, e) {
202 case pubsub.PayloadTypeSkillsEvent:
228 case pubsub.PayloadTypeSkillsEvent:
203 var e pubsub.Event[proto.SkillsEvent]
229 var e pubsub.Event[proto.SkillsEvent]
204 _ = json.Unmarshal(p.Payload, &e)
230 _ = json.Unmarshal(p.Payload, &e)
205− sendEvent(ctx, events, e)
231+ if !sendEvent(ctx, events, e) {
207 slog.Warn("Unknown event type", "type", p.Type)
235 slog.Warn("Unknown event type", "type", p.Type)
@@ -213,12 +241,15 @@ func (c *Client) SubscribeEvents(ctx context.Context, id string) (<-chan any, er
213 return events, nil
241 return events, nil
216−func sendEvent(ctx context.Context, evc chan any, ev any) {
244+func sendEvent(ctx context.Context, evc chan any, ev any) bool {
245+ if ctx.Err() != nil {
218 case evc <- ev:
249 case evc <- ev:
219 case <-ctx.Done():
251 case <-ctx.Done():
221− return
252+ return false