import type { Theme, Judges, Model, ModelFamily, Provider, TransitionMatrix, AssessmentItem, ApiError } from '../types.js'; // This helper centralizes our fetch logic and error handling. async function fetchAPI(url: string, options?: RequestInit): Promise { const response = await fetch(url, options); if (!response.ok) { const errorBody = (await response.json().catch(() => ({ error: 'An unknown error occurred' }))) as ApiError; const errorMessage = errorBody.error || `HTTP error! Status: ${response.status}`; throw new Error(errorMessage); } return response.json() as Promise; } // --- API Functions --- export const getThemes = (): Promise => { return fetchAPI('/api/themes'); }; export const getJudges = async (): Promise => { return fetchAPI('/api/judges') }; export const getModels = async (): Promise => { return fetchAPI('/api/models') } export const getModelFamilies = async (): Promise => { return fetchAPI('/api/models_families') } export const getProviders = async (): Promise => { return fetchAPI('/api/providers') } export const getReclassificationData = ( judge1: string, judge1Classification: string, judge2: string, judge2Classification: string, theme?: string, model?: string, modelFamily?: string, provider?: string ): Promise => { // Build the query string from the parameters const params = new URLSearchParams({ judge1, judge1Classification, judge2, judge2Classification }); // Only add the theme parameter if it's provided if (theme) { params.append('theme', theme); } if (model) { params.append('model', model); } if (modelFamily) { params.append('modelFamily', modelFamily); } if (provider) { params.append('provider', provider); } return fetchAPI(`/api/reclassification?${params.toString()}`); }; export const getAssessmentItems = ( judge1: string, judge1Classification: string, fromCategory: string, judge2: string, judge2Classification: string, toCategory: string, theme?: string, model?: string, modelFamily?: string, provider?: string ): Promise => { const params = new URLSearchParams({ judge1, judge1Classification, fromCategory, judge2, judge2Classification, toCategory }); if (theme) { params.append('theme', theme); } if (model) { params.append('model', model); } if (modelFamily) { params.append('modelFamily', modelFamily); } if (provider) { params.append('provider', provider); } return fetchAPI(`/api/mismatches?${params.toString()}`); }