import { getErrorMessage } from '@sim/utils/errors' import { type NextRequest, NextResponse } from 'next/server' import { copilotCredentialsContract } from '@/lib/api/contracts/copilot' import { parseRequest } from '@/lib/api/server' import { authenticateCopilotRequestSessionOnly } from '@/lib/copilot/request/http' import { routeExecution } from '@/lib/copilot/tools/server/router' import { withRouteHandler } from '@/lib/core/utils/with-route-handler' /** * GET /api/copilot/credentials * Returns connected OAuth credentials for the authenticated user. * Used by the copilot store for credential masking. */ export const GET = withRouteHandler(async (req: NextRequest) => { const parsed = await parseRequest(copilotCredentialsContract, req, {}) if (!parsed.success) return parsed.response const { userId, isAuthenticated } = await authenticateCopilotRequestSessionOnly() if (!isAuthenticated || !userId) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) } try { const result = await routeExecution('get_credentials', {}, { userId }) return NextResponse.json({ success: true, result }) } catch (error) { return NextResponse.json( { success: false, error: getErrorMessage(error, 'Failed to load credentials'), }, { status: 500 } ) } })