import type { ServerResponse } from 'node:http'; import { CodingProjectServiceError } from '../../coding-projects/project-service'; import { CodingConversationServiceError } from '../../coding-runtime/conversation-service'; import { sendJson } from '../route-utils'; export function sendCodingRouteError(res: ServerResponse, error: unknown): void { if (error instanceof CodingProjectServiceError || error instanceof CodingConversationServiceError) { sendJson(res, error.status, { success: false, code: error.code, error: error.message, }); return; } if (error instanceof SyntaxError) { sendJson(res, 400, { success: false, code: 'CODING_REQUEST_INVALID', error: 'Request JSON is invalid', }); return; } sendJson(res, 500, { success: false, code: 'CODING_REQUEST_FAILED', error: 'Coding request failed', }); } export function decodeRouteId(value: string): string { try { const decoded = decodeURIComponent(value).trim(); if (!decoded || decoded.length > 128 || decoded.includes('/')) throw new Error(); return decoded; } catch { throw new CodingConversationServiceError( 400, 'CODING_CONVERSATION_REQUEST_INVALID', 'Route identifier is invalid', ); } }