Skip to content

Commit 0001b40

Browse files
committed
refactor: improve chat event handling logic for session key matching
- Updated the chat event handling to clarify the matching logic for idempotency and session keys. - Enhanced comments for better understanding of session key acceptance during ongoing interactions, ensuring improved responsiveness in chat events.
1 parent 250d853 commit 0001b40

1 file changed

Lines changed: 3 additions & 5 deletions

File tree

lib/chat-stream.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,17 @@ export function handleChatEvent(
8787
// Ignore inline-completion traffic
8888
if (idempotencyKey?.startsWith('completion-')) return
8989

90-
// Match by idempotency key or session key fallback
90+
// Match by idempotency key OR session key
9191
const matchesIdem = !!(idempotencyKey && state.sentKeys.has(idempotencyKey))
92-
// Session-key match: accept if isSending OR if this is a final/delta/error/aborted event
93-
// (isSending may be false after HMR rebuild, but we still want the reply)
9492
const isReplyEvent =
9593
eventState === 'delta' ||
9694
eventState === 'final' ||
9795
eventState === 'error' ||
9896
eventState === 'aborted' ||
9997
eventState === 'tool_use' ||
10098
eventState === 'tool_start'
101-
const matchesSession =
102-
!idempotencyKey && eventSessionKey === state.sessionKey && (state.isSending || isReplyEvent)
99+
// Session-key match: accept reply events for our session even if sentKeys was lost (HMR)
100+
const matchesSession = eventSessionKey === state.sessionKey && (state.isSending || isReplyEvent)
103101
if (!matchesIdem && !matchesSession) {
104102
debugLog('Ignoring unrelated chat event', {
105103
eventState,

0 commit comments

Comments
 (0)