import type { WebAiCapabilities, WebAiResponse } from '@/types/api'; import { apiRequest } from './api'; type RawCapabilities = { enabled: boolean; provider: string; requiresConsent: boolean; maxQuestionLength: number; notice: string; }; type RawWebAiResponse = { mode: string; status: string; answer: string; sources?: Array<{ title: string; url: string; snippet: string; score?: number }>; provider: string; notice: string; redacted: boolean; requiresConsent: boolean; }; const domainOf = (value: string) => { try { return new URL(value).hostname; } catch { return ''; } }; export const getWebAiCapabilities = async (): Promise => { const raw = await apiRequest({ url: '/api/aihr/web-ai/capabilities', method: 'GET', timeout: 15000 }); return { enabled: raw.enabled, requiresConsent: raw.requiresConsent, providerName: raw.provider, maxQuestionLength: raw.maxQuestionLength, notice: raw.notice }; }; export const queryWebAi = async (question: string, consent: boolean): Promise => { const raw = await apiRequest({ url: '/api/aihr/web-ai/query', method: 'POST', data: { question, consent }, timeout: 45000 }); return { requestId: `web:${Date.now()}`, status: raw.status, provider: raw.provider, answer: raw.answer || '', citations: (raw.sources || []).map((source) => ({ title: source.title, url: source.url, domain: domainOf(source.url), snippet: source.snippet })), noEvidence: raw.status !== 'OK', warning: raw.notice, redacted: raw.redacted }; };