import { SUPABASE_URL } from '@d3ro/core/supabase-config' export type ChatRole = 'user' | 'assistant' export interface ChatRequestMessage { role: ChatRole content: string } export type ChatErrorCode = | 'AUTH_REQUIRED' | 'INVALID_REQUEST' | 'QUOTA_EXCEEDED' | 'MODEL_NOT_ALLOWED' | 'PROVIDER_UNAVAILABLE' | 'TIMEOUT' | 'NETWORK' | 'INVALID_RESPONSE' | 'SERVER_ERROR' | 'CANCELLED' export class ChatServiceError extends Error { constructor( readonly code: ChatErrorCode, readonly retryable: boolean, readonly status: number | null = null, ) { super(code) this.name = 'ChatServiceError' } } export interface SendChatOptions { accessToken: string model?: string | null signal?: AbortSignal timeoutMs?: number } export interface ReportableChatResult { text: string generationId: string } const MAX_MESSAGES = 40 const MAX_MESSAGE_CHARS = 8_000 const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i function isRecord(value: unknown): value is Record { return typeof value === 'object' && value !== null && !Array.isArray(value) } export function normalizeChatMessages(messages: ChatRequestMessage[]): ChatRequestMessage[] { if (messages.length === 0 || messages.length > MAX_MESSAGES) { throw new ChatServiceError('INVALID_REQUEST', false) } return messages.map((message) => { const content = message.content.trim() if ((message.role !== 'user' && message.role !== 'assistant') || content.length === 0 || content.length > MAX_MESSAGE_CHARS) { throw new ChatServiceError('INVALID_REQUEST', false) } return { role: message.role, content } }) } export function extractChatText(value: unknown): string { if (!isRecord(value) || !Array.isArray(value.content)) { throw new ChatServiceError('INVALID_RESPONSE', true) } const text = value.content .filter((block): block is Record => isRecord(block) && block.type === 'text') .map((block) => typeof block.text === 'string' ? block.text : '') .join('') .trim() if (text.length === 0) throw new ChatServiceError('INVALID_RESPONSE', true) return text } function codeForStatus(status: number, body: unknown): ChatErrorCode { const serverCode = isRecord(body) && typeof body.error === 'string' ? body.error : '' if (status === 401) return 'AUTH_REQUIRED' if (status === 400) return 'INVALID_REQUEST' if (status === 403 || serverCode === 'model_not_allowed') return 'MODEL_NOT_ALLOWED' if (status === 429 || serverCode === 'quota_exceeded') return 'QUOTA_EXCEEDED' if (status === 503 || serverCode === 'provider_unavailable') return 'PROVIDER_UNAVAILABLE' if (status === 504 || serverCode === 'provider_timeout') return 'TIMEOUT' return 'SERVER_ERROR' } async function sendChatRequest( messages: ChatRequestMessage[], options: SendChatOptions, generationPurpose: 'command_response' | null, ): Promise<{ text: string; generationId: string | null }> { const token = options.accessToken.trim() if (token.length === 0) throw new ChatServiceError('AUTH_REQUIRED', false, 401) const normalizedMessages = normalizeChatMessages(messages) const controller = new AbortController() const onExternalAbort = (): void => controller.abort() options.signal?.addEventListener('abort', onExternalAbort, { once: true }) const timer = setTimeout(() => controller.abort(), options.timeoutMs ?? 45_000) try { const headers: Record = { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json', } if (generationPurpose !== null) { headers['X-D3RO-Generation-Purpose'] = generationPurpose } const response = await fetch(`${SUPABASE_URL}/functions/v1/llm-proxy`, { method: 'POST', headers, body: JSON.stringify({ messages: normalizedMessages, max_tokens: 1_024, model: options.model?.trim() || undefined, stream: false, }), signal: controller.signal, }) const body: unknown = await response.json().catch(() => null) if (!response.ok) { const code = codeForStatus(response.status, body) throw new ChatServiceError( code, code === 'TIMEOUT' || code === 'PROVIDER_UNAVAILABLE' || code === 'SERVER_ERROR', response.status, ) } const generationId = generationPurpose === null ? null : response.headers?.get('X-D3RO-Generation-Id')?.trim() ?? '' if ( generationPurpose !== null && (typeof generationId !== 'string' || !UUID_PATTERN.test(generationId)) ) { throw new ChatServiceError('INVALID_RESPONSE', true) } return { text: extractChatText(body), generationId: generationId === null ? null : generationId.toLowerCase(), } } catch (error) { if (error instanceof ChatServiceError) throw error if (controller.signal.aborted) { const external = options.signal?.aborted === true throw new ChatServiceError(external ? 'CANCELLED' : 'TIMEOUT', !external) } throw new ChatServiceError('NETWORK', true) } finally { clearTimeout(timer) options.signal?.removeEventListener('abort', onExternalAbort) } } export async function sendChatMessage( messages: ChatRequestMessage[], options: SendChatOptions, ): Promise { return (await sendChatRequest(messages, options, null)).text } export async function sendReportableCommandMessage( messages: ChatRequestMessage[], options: SendChatOptions, ): Promise { const result = await sendChatRequest(messages, options, 'command_response') if (result.generationId === null) throw new ChatServiceError('INVALID_RESPONSE', true) return { text: result.text, generationId: result.generationId } }