Skip to content

Commit 42c3b4e

Browse files
Bill LeoutsakosBill Leoutsakos
authored andcommitted
fix(streaming): suppress mixed invocation outputs
1 parent b983bd2 commit 42c3b4e

2 files changed

Lines changed: 12 additions & 13 deletions

File tree

apps/sim/lib/workflows/streaming/streaming.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ describe('createStreamingResponse agent-events-v1', () => {
10431043
expect(events.some((event) => event.event === 'final')).toBe(true)
10441044
})
10451045

1046-
it('suppresses final selected output when streamed content is keyed by invocation', async () => {
1046+
it('suppresses repeated selected output when one invocation streams', async () => {
10471047
const stream = await createStreamingResponse({
10481048
requestId: 'request-invocation-selected-output',
10491049
streamConfig: {
@@ -1068,15 +1068,16 @@ describe('createStreamingResponse agent-events-v1', () => {
10681068
},
10691069
} as any)
10701070
await onBlockComplete('agent-1', { content: 'current answer' }, 'invoke-current')
1071+
await onBlockComplete('agent-1', { content: 'later answer' }, 'invoke-later')
10711072

10721073
return {
10731074
success: true,
10741075
output: { content: 'current answer' },
10751076
logs: [
10761077
{
10771078
blockId: 'agent-1',
1078-
blockExecutionId: 'invoke-stale',
1079-
output: { content: 'stale answer' },
1079+
blockExecutionId: 'invoke-later',
1080+
output: { content: 'later answer' },
10801081
startedAt: new Date().toISOString(),
10811082
endedAt: new Date().toISOString(),
10821083
durationMs: 1,

apps/sim/lib/workflows/streaming/streaming.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export function agentStreamProtocolResponseHeaders(options: {
129129

130130
interface StreamingState {
131131
streamedChunks: Map<string, string[]>
132+
streamedBlockIds: Set<string>
132133
processedOutputs: Set<string>
133134
streamCompletionTimes: Map<string, number>
134135
completedBlockIds: Set<string>
@@ -319,6 +320,7 @@ async function buildMinimalResult(
319320
result: ExecutionResult,
320321
selectedOutputs: string[] | undefined,
321322
streamedContent: Map<string, string>,
323+
streamedBlockIds: Set<string>,
322324
completedBlockIds: Set<string>,
323325
streamedSelectedOutputKeys: Set<string>,
324326
requestId: string,
@@ -387,15 +389,8 @@ async function buildMinimalResult(
387389
let selectedOutputBytes = assertSelectedOutputBytes(minimalResult.output)
388390
for (const descriptor of getSelectedOutputDescriptors(selectedOutputs)) {
389391
const { blockId, path } = descriptor
390-
const blockLogs = result.logs.filter((log: BlockLog) => log.blockId === blockId)
391392

392-
if (
393-
streamedContent.has(blockId) ||
394-
blockLogs.some(
395-
(log: BlockLog) =>
396-
log.blockExecutionId !== undefined && streamedContent.has(log.blockExecutionId)
397-
)
398-
) {
393+
if (streamedContent.has(blockId) || streamedBlockIds.has(blockId)) {
399394
continue
400395
}
401396

@@ -417,7 +412,7 @@ async function buildMinimalResult(
417412
continue
418413
}
419414

420-
const blockLog = blockLogs[0]
415+
const blockLog = result.logs.find((log: BlockLog) => log.blockId === blockId)
421416
if (!blockLog?.output) {
422417
continue
423418
}
@@ -545,6 +540,7 @@ export async function createStreamingResponse(
545540
async start(controller) {
546541
const state: StreamingState = {
547542
streamedChunks: new Map(),
543+
streamedBlockIds: new Set(),
548544
processedOutputs: new Set(),
549545
streamCompletionTimes: new Map(),
550546
completedBlockIds: new Set(),
@@ -689,6 +685,7 @@ export async function createStreamingResponse(
689685
}
690686

691687
const textChunk = decoder.decode(value, { stream: true })
688+
state.streamedBlockIds.add(blockId)
692689
if (!state.streamedChunks.has(streamKey)) {
693690
state.streamedChunks.set(streamKey, [])
694691
}
@@ -728,7 +725,7 @@ export async function createStreamingResponse(
728725
return
729726
}
730727

731-
if (state.streamedChunks.has(blockExecutionId ?? blockId)) {
728+
if (state.streamedBlockIds.has(blockId)) {
732729
return
733730
}
734731

@@ -880,6 +877,7 @@ export async function createStreamingResponse(
880877
result,
881878
streamConfig.selectedOutputs,
882879
streamedContent,
880+
state.streamedBlockIds,
883881
state.completedBlockIds,
884882
state.streamedSelectedOutputKeys,
885883
requestId,

0 commit comments

Comments
 (0)