(params: StreamingOrchestrationParams)
| 74 | } |
| 75 | |
| 76 | export function createSSEStream(params: StreamingOrchestrationParams): ReadableStream { |
| 77 | const { |
| 78 | requestPayload, |
| 79 | userId, |
| 80 | streamId, |
| 81 | executionId, |
| 82 | runId, |
| 83 | chatId, |
| 84 | currentChat, |
| 85 | isNewChat, |
| 86 | message, |
| 87 | titleModel, |
| 88 | titleProvider, |
| 89 | requestId, |
| 90 | workspaceId, |
| 91 | orchestrateOptions, |
| 92 | otelRoot, |
| 93 | } = params |
| 94 | |
| 95 | // Reuse caller's root if provided; otherwise start our own. |
| 96 | const activeOtelRoot = |
| 97 | otelRoot ?? |
| 98 | startCopilotOtelRoot({ |
| 99 | requestId, |
| 100 | route: orchestrateOptions.goRoute, |
| 101 | chatId, |
| 102 | workflowId: orchestrateOptions.workflowId, |
| 103 | executionId, |
| 104 | runId, |
| 105 | streamId, |
| 106 | transport: CopilotTransport.Stream, |
| 107 | }) |
| 108 | |
| 109 | const abortController = new AbortController() |
| 110 | registerActiveStream(streamId, abortController) |
| 111 | |
| 112 | const publisher = new StreamWriter({ streamId, chatId, requestId }) |
| 113 | |
| 114 | // Classify cancel: signal.reason (explicit-stop set) wins, then |
| 115 | // clientDisconnected, else Unknown (latent contract bug — log it). |
| 116 | const recordCancelled = (errorMessage?: string): CopilotRequestCancelReasonValue => { |
| 117 | const rawReason = abortController.signal.reason |
| 118 | let cancelReason: CopilotRequestCancelReasonValue |
| 119 | if (isExplicitStopReason(rawReason)) { |
| 120 | cancelReason = CopilotRequestCancelReason.ExplicitStop |
| 121 | } else if (publisher.clientDisconnected) { |
| 122 | cancelReason = CopilotRequestCancelReason.ClientDisconnect |
| 123 | } else { |
| 124 | cancelReason = CopilotRequestCancelReason.Unknown |
| 125 | const serializedReason = |
| 126 | rawReason === undefined |
| 127 | ? 'undefined' |
| 128 | : rawReason instanceof Error |
| 129 | ? `${rawReason.name}: ${rawReason.message}` |
| 130 | : typeof rawReason === 'string' |
| 131 | ? rawReason |
| 132 | : (() => { |
| 133 | try { |
no test coverage detected