Download src/services/api.ts from HyperCluster/Fara-BrowserUse: direct link, hf CLI and curl.
- Browser
- Download file 3.71 kB
-
https://huggingface.co/spaces/HyperCluster/Fara-BrowserUse/resolve/main/src/services/api.ts
- Command line
-
hf download hf://spaces/HyperCluster/Fara-BrowserUse/src/services/api.ts
-
curl -L -o api.ts https://huggingface.co/spaces/HyperCluster/Fara-BrowserUse/resolve/main/src/services/api.ts
3.71 kB
| import { getApiBaseUrl } from '@/config/api'; | |
| /** | |
| * Fetch available models from the backend | |
| */ | |
| export async function fetchAvailableModels(): Promise<string[]> { | |
| 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<string> { | |
| 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<void> { | |
| 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<void> { | |
| 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' | |
| }; | |
| } | |
| } | |