({
request,
streamId,
afterCursor,
batchMode,
authenticatedUserId,
rootSpan,
rootContext,
}: {
request: NextRequest
streamId: string
afterCursor: string
batchMode: boolean
authenticatedUserId: string
rootSpan: Span
rootContext: Context
})
| 181 | }) |
| 182 | |
| 183 | async function handleResumeRequestBody({ |
| 184 | request, |
| 185 | streamId, |
| 186 | afterCursor, |
| 187 | batchMode, |
| 188 | authenticatedUserId, |
| 189 | rootSpan, |
| 190 | rootContext, |
| 191 | }: { |
| 192 | request: NextRequest |
| 193 | streamId: string |
| 194 | afterCursor: string |
| 195 | batchMode: boolean |
| 196 | authenticatedUserId: string |
| 197 | rootSpan: Span |
| 198 | rootContext: Context |
| 199 | }) { |
| 200 | const run = await getLatestRunForStream(streamId, authenticatedUserId).catch((err) => { |
| 201 | logger.warn('Failed to fetch latest run for stream', { |
| 202 | streamId, |
| 203 | error: getErrorMessage(err), |
| 204 | }) |
| 205 | return null |
| 206 | }) |
| 207 | logger.info('[Resume] Stream lookup', { |
| 208 | streamId, |
| 209 | afterCursor, |
| 210 | batchMode, |
| 211 | hasRun: !!run, |
| 212 | runStatus: run?.status, |
| 213 | }) |
| 214 | if (!run) { |
| 215 | rootSpan.setAttribute(TraceAttr.CopilotResumeOutcome, CopilotResumeOutcome.StreamNotFound) |
| 216 | rootSpan.end() |
| 217 | return NextResponse.json({ error: 'Stream not found' }, { status: 404 }) |
| 218 | } |
| 219 | rootSpan.setAttribute(TraceAttr.CopilotRunStatus, run.status) |
| 220 | |
| 221 | if (batchMode) { |
| 222 | const afterSeq = afterCursor || '0' |
| 223 | const [events, previewSessions] = await Promise.all([ |
| 224 | readEvents(streamId, afterSeq), |
| 225 | readFilePreviewSessions(streamId).catch((error) => { |
| 226 | logger.warn('Failed to read preview sessions for stream batch', { |
| 227 | streamId, |
| 228 | error: getErrorMessage(error), |
| 229 | }) |
| 230 | return [] |
| 231 | }), |
| 232 | ]) |
| 233 | const batchEvents = events.map(toStreamBatchEvent) |
| 234 | logger.info('[Resume] Batch response', { |
| 235 | streamId, |
| 236 | afterCursor: afterSeq, |
| 237 | eventCount: batchEvents.length, |
| 238 | previewSessionCount: previewSessions.length, |
| 239 | runStatus: run.status, |
| 240 | }) |
no test coverage detected