Spaces:
Sleeping
Sleeping
| import axios from 'axios'; | |
| import type { | |
| GridState, | |
| GridConfig, | |
| Algorithm, | |
| SearchResult, | |
| PlanResult, | |
| ComparisonResult, | |
| AlgorithmInfo, | |
| Position, | |
| Segment, | |
| Tunnel, | |
| } from '../types'; | |
| // Use environment variable or default to localhost for development | |
| // In production (HF Spaces), use relative URLs since nginx proxies /api | |
| const API_BASE = import.meta.env.VITE_API_URL || ''; | |
| const api = axios.create({ | |
| baseURL: API_BASE, | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| }, | |
| }); | |
| // Health check | |
| export async function checkHealth(): Promise<{ status: string }> { | |
| const response = await api.get('/api/health'); | |
| return response.data; | |
| } | |
| // List algorithms | |
| export async function getAlgorithms(): Promise<AlgorithmInfo[]> { | |
| const response = await api.get('/api/algorithms'); | |
| return response.data.algorithms; | |
| } | |
| // Generate random grid | |
| export async function generateGrid(config: GridConfig = {}): Promise<{ | |
| initialState: string; | |
| traffic: string; | |
| parsed: GridState; | |
| }> { | |
| const response = await api.post('/api/grid/generate', { | |
| width: config.width, | |
| height: config.height, | |
| num_stores: config.numStores, | |
| num_destinations: config.numDestinations, | |
| num_tunnels: config.numTunnels, | |
| obstacle_density: config.obstacleDensity ?? 0.1, | |
| }); | |
| return { | |
| initialState: response.data.initial_state, | |
| traffic: response.data.traffic, | |
| parsed: response.data.parsed, | |
| }; | |
| } | |
| // Find path | |
| export async function findPath( | |
| gridWidth: number, | |
| gridHeight: number, | |
| start: Position, | |
| goal: Position, | |
| segments: Segment[], | |
| tunnels: Tunnel[], | |
| strategy: Algorithm | |
| ): Promise<SearchResult> { | |
| const response = await api.post('/api/search/path', { | |
| grid_width: gridWidth, | |
| grid_height: gridHeight, | |
| start, | |
| goal, | |
| segments: segments.map(s => ({ | |
| src: s.src, | |
| dst: s.dst, | |
| traffic: s.traffic, | |
| })), | |
| tunnels: tunnels.map(t => ({ | |
| entrance1: t.entrance1, | |
| entrance2: t.entrance2, | |
| })), | |
| strategy, | |
| }); | |
| return { | |
| plan: response.data.plan, | |
| cost: response.data.cost, | |
| nodesExpanded: response.data.nodes_expanded, | |
| runtimeMs: response.data.runtime_ms, | |
| memoryKb: response.data.memory_kb, | |
| cpuPercent: response.data.cpu_percent, | |
| path: response.data.path, | |
| steps: response.data.steps, | |
| }; | |
| } | |
| // Create plan | |
| export async function createPlan( | |
| initialState: string, | |
| traffic: string, | |
| strategy: Algorithm, | |
| visualize: boolean = false | |
| ): Promise<PlanResult> { | |
| const response = await api.post('/api/search/plan', { | |
| initial_state: initialState, | |
| traffic, | |
| strategy, | |
| visualize, | |
| }); | |
| return { | |
| output: response.data.output, | |
| assignments: response.data.assignments, | |
| totalCost: response.data.total_cost, | |
| totalNodesExpanded: response.data.total_nodes_expanded, | |
| runtimeMs: response.data.runtime_ms, | |
| memoryKb: response.data.memory_kb, | |
| cpuPercent: response.data.cpu_percent, | |
| }; | |
| } | |
| // Compare algorithms | |
| export async function compareAlgorithms( | |
| initialState: string, | |
| traffic: string | |
| ): Promise<{ comparisons: ComparisonResult[]; optimalCost: number }> { | |
| const response = await api.post('/api/search/compare', { | |
| initial_state: initialState, | |
| traffic, | |
| }); | |
| return { | |
| comparisons: response.data.comparisons.map((c: any) => ({ | |
| algorithm: c.algorithm, | |
| name: c.name, | |
| plan: c.plan, | |
| cost: c.cost, | |
| nodesExpanded: c.nodes_expanded, | |
| runtimeMs: c.runtime_ms, | |
| memoryKb: c.memory_kb, | |
| cpuPercent: c.cpu_percent, | |
| isOptimal: c.is_optimal, | |
| })), | |
| optimalCost: response.data.optimal_cost, | |
| }; | |
| } | |
| export default api; | |