Skip to content

Commit 9df5c42

Browse files
committed
fix streaming parsing issue for multiple onStream calls for same block
1 parent 705601d commit 9df5c42

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/hooks/use-workflow-execution.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -398,23 +398,22 @@ export function useWorkflowExecution() {
398398
}
399399

400400
const streamCompletionTimes = new Map<string, number>()
401+
const processedFirstChunk = new Set<string>()
401402

402403
const onStream = async (streamingExecution: StreamingExecution) => {
403404
const promise = (async () => {
404405
if (!streamingExecution.stream) return
405406
const reader = streamingExecution.stream.getReader()
406407
const blockId = (streamingExecution.execution as any)?.blockId
407408

408-
let isFirstChunk = true
409-
410-
if (blockId) {
409+
if (blockId && !streamedContent.has(blockId)) {
411410
streamedContent.set(blockId, '')
412411
}
412+
413413
try {
414414
while (true) {
415415
const { done, value } = await reader.read()
416416
if (done) {
417-
// Record when this stream completed
418417
if (blockId) {
419418
streamCompletionTimes.set(blockId, Date.now())
420419
}
@@ -425,13 +424,12 @@ export function useWorkflowExecution() {
425424
streamedContent.set(blockId, (streamedContent.get(blockId) || '') + chunk)
426425
}
427426

428-
// Add separator before first chunk if this isn't the first block
429427
let chunkToSend = chunk
430-
if (isFirstChunk && streamedContent.size > 1) {
431-
chunkToSend = `\n\n${chunk}`
432-
isFirstChunk = false
433-
} else if (isFirstChunk) {
434-
isFirstChunk = false
428+
if (blockId && !processedFirstChunk.has(blockId)) {
429+
processedFirstChunk.add(blockId)
430+
if (streamedContent.size > 1) {
431+
chunkToSend = `\n\n${chunk}`
432+
}
435433
}
436434

437435
controller.enqueue(encodeSSE({ blockId, chunk: chunkToSend }))

0 commit comments

Comments
 (0)