import { getApiBaseUrl } from '@/config/api'; /** * Fetch available models from the backend */ export async function fetchAvailableModels(): Promise { try { const response = await fetch(`${getApiBaseUrl()}/models`); if (!response.ok) { throw new Error('Failed to fetch models'); } const data = await response.json(); // Backend returns array directly return Array.isArray(data) ? data : (data.models || []); } catch (error) { console.error('Error fetching models:', error); // Return default model on error return ['microsoft/Fara-7B']; } } /** * Generate a random instruction from the backend */ export async function generateRandomQuestion(): Promise { try { const response = await fetch(`${getApiBaseUrl()}/random-question`); if (!response.ok) { throw new Error('Failed to generate instruction'); } const data = await response.json(); return data.question || data.instruction || 'Search for the latest AI news'; } catch (error) { console.error('Error generating question:', error); // Return a default question on error return 'Search for the latest news about AI agents'; } } /** * Update step evaluation (vote) */ export async function updateStepEvaluation( traceId: string, stepId: string, evaluation: 'like' | 'dislike' | 'neutral' ): Promise { try { const response = await fetch(`${getApiBaseUrl()}/traces/${traceId}/steps/${stepId}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ step_evaluation: evaluation, }), }); if (!response.ok) { console.warn('Failed to update step evaluation (endpoint may not exist)'); } } catch (error) { console.warn('Error updating step evaluation:', error); } } /** * Update trace evaluation (overall task feedback) */ export async function updateTraceEvaluation( traceId: string, evaluation: 'success' | 'failed' | 'not_evaluated' ): Promise { try { const response = await fetch(`${getApiBaseUrl()}/traces/${traceId}/evaluation`, { method: 'PATCH', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ user_evaluation: evaluation, }), }); if (!response.ok) { console.warn('Failed to update trace evaluation (endpoint may not exist)'); } } catch (error) { console.warn('Error updating trace evaluation:', error); } } /** * Upload a trace to Modal's trace storage endpoint * Returns success status and any error message * * Note: This calls the backend server which proxies to Modal with proper auth, * keeping Modal credentials server-side only. */ export async function uploadTraceToModal(traceData: object): Promise<{ success: boolean; message?: string; error?: string; }> { try { const response = await fetch(`${getApiBaseUrl()}/traces`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(traceData), }); if (!response.ok) { const errorText = await response.text(); console.error('Failed to upload trace:', errorText); return { success: false, error: `HTTP ${response.status}: ${errorText}` }; } const result = await response.json(); return result; } catch (error) { console.error('Error uploading trace:', error); return { success: false, error: error instanceof Error ? error.message : 'Unknown error' }; } }