(req: NextRequest)
| 71 | } |
| 72 | |
| 73 | export async function GET(req: NextRequest) { |
| 74 | try { |
| 75 | const { searchParams } = new URL(req.url) |
| 76 | const workflowId = searchParams.get('workflowId') |
| 77 | const workspaceId = searchParams.get('workspaceId') |
| 78 | const chatId = searchParams.get('chatId') |
| 79 | |
| 80 | const { userId: authenticatedUserId, isAuthenticated } = |
| 81 | await authenticateCopilotRequestSessionOnly() |
| 82 | if (!isAuthenticated || !authenticatedUserId) { |
| 83 | return createUnauthorizedResponse() |
| 84 | } |
| 85 | |
| 86 | if (chatId) { |
| 87 | const chat = await getAccessibleCopilotChat(chatId, authenticatedUserId) |
| 88 | if (!chat) { |
| 89 | return NextResponse.json({ success: false, error: 'Chat not found' }, { status: 404 }) |
| 90 | } |
| 91 | |
| 92 | let streamSnapshot: { |
| 93 | events: ReturnType<typeof toStreamBatchEvent>[] |
| 94 | previewSessions: Awaited<ReturnType<typeof readFilePreviewSessions>> |
| 95 | status: string |
| 96 | } | null = null |
| 97 | if (chat.conversationId) { |
| 98 | try { |
| 99 | const [events, previewSessions, run] = await Promise.all([ |
| 100 | readEvents(chat.conversationId, '0'), |
| 101 | readFilePreviewSessions(chat.conversationId).catch((error) => { |
| 102 | logger.warn('Failed to read preview sessions for copilot chat', { |
| 103 | chatId, |
| 104 | conversationId: chat.conversationId, |
| 105 | error: toError(error).message, |
| 106 | }) |
| 107 | return [] |
| 108 | }), |
| 109 | getLatestRunForStream(chat.conversationId, authenticatedUserId).catch((error) => { |
| 110 | logger.warn('Failed to fetch latest run for copilot chat snapshot', { |
| 111 | chatId, |
| 112 | conversationId: chat.conversationId, |
| 113 | error: toError(error).message, |
| 114 | }) |
| 115 | return null |
| 116 | }), |
| 117 | ]) |
| 118 | |
| 119 | streamSnapshot = { |
| 120 | events: events.map(toStreamBatchEvent), |
| 121 | previewSessions, |
| 122 | status: |
| 123 | typeof run?.status === 'string' |
| 124 | ? run.status |
| 125 | : events.length > 0 |
| 126 | ? 'active' |
| 127 | : 'unknown', |
| 128 | } |
| 129 | } catch (error) { |
| 130 | logger.warn('Failed to load copilot chat stream snapshot', { |
nothing calls this directly
no test coverage detected