Skip to content

Commit 7bba0c5

Browse files
committed
address comments
1 parent 8c704d0 commit 7bba0c5

2 files changed

Lines changed: 19 additions & 13 deletions

File tree

apps/sim/app/api/copilot/chat/stream/route.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -322,22 +322,22 @@ async function handleResumeRequestBody({
322322
const flushEvents = async () => {
323323
const events = await readEvents(streamId, cursor)
324324
if (events.length > 0) {
325-
totalEventsFlushed += events.length
326325
logger.debug('[Resume] Flushing events', {
327326
streamId,
328327
afterCursor: cursor,
329328
eventCount: events.length,
330329
})
331330
}
332331
for (const envelope of events) {
332+
if (!enqueueEvent(envelope)) {
333+
break
334+
}
335+
totalEventsFlushed += 1
333336
cursor = envelope.stream.cursor ?? String(envelope.seq)
334337
currentRequestId = extractEnvelopeRequestId(envelope) || currentRequestId
335338
if (envelope.type === MothershipStreamV1EventType.complete) {
336339
sawTerminalEvent = true
337340
}
338-
if (!enqueueEvent(envelope)) {
339-
break
340-
}
341341
}
342342
}
343343

@@ -357,13 +357,13 @@ async function handleResumeRequestBody({
357357
reason: options?.reason,
358358
requestId: currentRequestId,
359359
})) {
360+
if (!enqueueEvent(envelope)) {
361+
break
362+
}
360363
cursor = envelope.stream.cursor ?? String(envelope.seq)
361364
if (envelope.type === MothershipStreamV1EventType.complete) {
362365
sawTerminalEvent = true
363366
}
364-
if (!enqueueEvent(envelope)) {
365-
break
366-
}
367367
}
368368
}
369369

@@ -373,14 +373,14 @@ async function handleResumeRequestBody({
373373
const gap = await checkForReplayGap(streamId, afterCursor, currentRequestId)
374374
if (gap) {
375375
for (const envelope of gap.envelopes) {
376+
if (!enqueueEvent(envelope)) {
377+
break
378+
}
376379
cursor = envelope.stream.cursor ?? String(envelope.seq)
377380
currentRequestId = extractEnvelopeRequestId(envelope) || currentRequestId
378381
if (envelope.type === MothershipStreamV1EventType.complete) {
379382
sawTerminalEvent = true
380383
}
381-
if (!enqueueEvent(envelope)) {
382-
break
383-
}
384384
}
385385
return
386386
}

apps/sim/hooks/use-progressive-list.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client'
22

3-
import { useEffect, useState } from 'react'
3+
import { useEffect, useLayoutEffect, useRef, useState } from 'react'
44

55
interface ProgressiveListOptions {
66
/** Number of items to render in the initial batch (most recent items) */
@@ -54,6 +54,11 @@ export function useProgressiveList<T>(
5454
const initialBatch = Math.max(0, options?.initialBatch ?? DEFAULTS.initialBatch)
5555
const batchSize = Math.max(1, options?.batchSize ?? DEFAULTS.batchSize)
5656
const [state, setState] = useState(() => createInitialState(key, items.length, initialBatch))
57+
const latestItemCountRef = useRef(items.length)
58+
59+
useLayoutEffect(() => {
60+
latestItemCountRef.current = items.length
61+
}, [items.length])
5762

5863
const renderState =
5964
state.key === key && (state.count > 0 || items.length === 0 || state.caughtUp)
@@ -105,11 +110,12 @@ export function useProgressiveList<T>(
105110
return prev
106111
}
107112

108-
const count = Math.min(items.length, prev.count + batchSize)
113+
const itemCount = latestItemCountRef.current
114+
const count = Math.min(itemCount, prev.count + batchSize)
109115
return {
110116
key,
111117
count,
112-
caughtUp: count >= items.length,
118+
caughtUp: count >= itemCount,
113119
}
114120
})
115121
})

0 commit comments

Comments
 (0)