Skip to content

Commit 2b653a6

Browse files
Bill LeoutsakosBill Leoutsakos
authored andcommitted
fix(pi): stream only final plan content
1 parent 8874a99 commit 2b653a6

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

apps/sim/executor/handlers/pi/pi-handler.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,4 +931,41 @@ describe('PiBlockHandler', () => {
931931
expect(text).toContain('streamed')
932932
expect(result.execution.output.content).toBe('streamed')
933933
})
934+
935+
it('streams only the canonical final document for Plan mode', async () => {
936+
mockRunCloudPlan.mockImplementation(async (_params, runCtx) => {
937+
runCtx.onEvent({ type: 'text', text: 'Inspecting files...' })
938+
runCtx.onEvent({ type: 'final', text: '# Final Plan\n\n1. Make the change.' })
939+
return {
940+
totals: {
941+
finalText: '# Final Plan\n\n1. Make the change.',
942+
inputTokens: 0,
943+
outputTokens: 0,
944+
toolCalls: [],
945+
},
946+
}
947+
})
948+
949+
const result = (await handler.execute(ctx({ stream: true, selectedOutputs: ['blk'] }), block, {
950+
mode: 'cloud_plan',
951+
task: 'plan it',
952+
model: 'claude',
953+
owner: 'o',
954+
repo: 'r',
955+
githubToken: 'ghp',
956+
})) as StreamingExecution
957+
958+
const reader = result.stream.getReader()
959+
const decoder = new TextDecoder()
960+
let text = ''
961+
for (;;) {
962+
const { done, value } = await reader.read()
963+
if (done) break
964+
text += decoder.decode(value)
965+
}
966+
967+
expect(text).toBe('# Final Plan\n\n1. Make the change.')
968+
expect(text).not.toContain('Inspecting files...')
969+
expect(result.execution.output.content).toBe('# Final Plan\n\n1. Make the change.')
970+
})
934971
})

apps/sim/executor/handlers/pi/pi-handler.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ export class PiBlockHandler implements BlockHandler {
510510
try {
511511
const result = await backend(params, {
512512
onEvent: (event) => {
513+
if (params.mode === 'cloud_plan') return
513514
const text = streamTextForEvent(event)
514515
if (text) controller.enqueue(encoder.encode(text))
515516
},
@@ -519,6 +520,9 @@ export class PiBlockHandler implements BlockHandler {
519520
controller.error(new Error(result.totals.errorMessage))
520521
return
521522
}
523+
if (params.mode === 'cloud_plan' && result.totals.finalText) {
524+
controller.enqueue(encoder.encode(result.totals.finalText))
525+
}
522526
Object.assign(
523527
output,
524528
this.buildOutput(

0 commit comments

Comments
 (0)