Skip to content

Commit a4cd2f3

Browse files
committed
Fix subagent persistence
1 parent 90352fe commit a4cd2f3

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

apps/sim/lib/copilot/chat-streaming.ts

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS
251251
}, 15_000)
252252

253253
try {
254-
await orchestrateCopilotStream(requestPayload, {
254+
const result = await orchestrateCopilotStream(requestPayload, {
255255
...orchestrateOptions,
256256
executionId,
257257
runId,
@@ -261,21 +261,61 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS
261261
},
262262
})
263263

264+
if (abortController.signal.aborted) {
265+
logger.info(`[${requestId}] Stream aborted by explicit stop`)
266+
await eventWriter.close().catch(() => {})
267+
await setStreamMeta(streamId, { status: 'cancelled', userId, executionId, runId })
268+
await updateRunStatus(runId, 'cancelled', { completedAt: new Date() }).catch(() => {})
269+
return
270+
}
271+
272+
if (!result.success) {
273+
const errorMessage =
274+
result.error ||
275+
result.errors?.[0] ||
276+
'An unexpected error occurred while processing the response.'
277+
278+
if (clientDisconnected) {
279+
logger.info(`[${requestId}] Stream ended after client disconnect`)
280+
await eventWriter.close().catch(() => {})
281+
await setStreamMeta(streamId, { status: 'cancelled', userId, executionId, runId })
282+
await updateRunStatus(runId, 'cancelled', { completedAt: new Date() }).catch(() => {})
283+
return
284+
}
285+
286+
logger.error(`[${requestId}] Orchestration returned failure`, {
287+
error: errorMessage,
288+
})
289+
await eventWriter.close()
290+
await setStreamMeta(streamId, {
291+
status: 'error',
292+
userId,
293+
executionId,
294+
runId,
295+
error: errorMessage,
296+
})
297+
await updateRunStatus(runId, 'error', {
298+
completedAt: new Date(),
299+
error: errorMessage,
300+
}).catch(() => {})
301+
return
302+
}
303+
264304
await eventWriter.close()
265305
await setStreamMeta(streamId, { status: 'complete', userId, executionId, runId })
266306
await updateRunStatus(runId, 'complete', { completedAt: new Date() }).catch(() => {})
267307
} catch (error) {
268308
if (abortController.signal.aborted) {
269309
logger.info(`[${requestId}] Stream aborted by explicit stop`)
270310
await eventWriter.close().catch(() => {})
271-
await setStreamMeta(streamId, { status: 'complete', userId, executionId, runId })
311+
await setStreamMeta(streamId, { status: 'cancelled', userId, executionId, runId })
272312
await updateRunStatus(runId, 'cancelled', { completedAt: new Date() }).catch(() => {})
273313
return
274314
}
275315
if (clientDisconnected) {
276316
logger.info(`[${requestId}] Stream ended after client disconnect`)
277317
await eventWriter.close().catch(() => {})
278-
await setStreamMeta(streamId, { status: 'complete', userId, executionId, runId })
318+
await setStreamMeta(streamId, { status: 'cancelled', userId, executionId, runId })
279319
await updateRunStatus(runId, 'cancelled', { completedAt: new Date() }).catch(() => {})
280320
return
281321
}

apps/sim/lib/copilot/orchestrator/stream/buffer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function getMetaKey(streamId: string) {
7575
return `${getStreamKeyPrefix(streamId)}:meta`
7676
}
7777

78-
export type StreamStatus = 'active' | 'complete' | 'error'
78+
export type StreamStatus = 'active' | 'complete' | 'cancelled' | 'error'
7979

8080
export type StreamMeta = {
8181
status: StreamStatus

0 commit comments

Comments
 (0)