/** * Backend API Hook - REST API calls to WidgetTDC Backend */ import { API_URL } from '../config/api'; interface APIResponse { success: boolean; data?: T; error?: string; } export const useBackendAPI = () => { const fetchAPI = async ( endpoint: string, options?: RequestInit ): Promise> => { try { const response = await fetch(`${API_URL}${endpoint}`, { ...options, headers: { 'Content-Type': 'application/json', ...options?.headers, }, }); const data = await response.json(); if (!response.ok) { return { success: false, error: data.error || 'Request failed' }; } return { success: true, data }; } catch (error) { return { success: false, error: (error as Error).message }; } }; // Health check const getHealth = () => fetchAPI('/health'); // MCP Tool execution via REST const callMCPTool = async (tool: string, params?: Record) => { return fetchAPI('/api/mcp/route', { method: 'POST', body: JSON.stringify({ tool, params }), }); }; // Knowledge API const searchKnowledge = (query: string) => fetchAPI(`/api/knowledge/search?q=${encodeURIComponent(query)}`); // Memory API const getMemoryEntities = (orgId: string, userId?: string) => fetchAPI(`/api/memory/entities?orgId=${orgId}${userId ? `&userId=${userId}` : ''}`); // Notes API const getNotes = (userId: string, orgId: string) => fetchAPI(`/api/notes?userId=${userId}&orgId=${orgId}`); // System Info const getSystemInfo = () => fetchAPI('/api/sys'); // Logs const getLogs = () => fetchAPI('/api/logs'); return { fetchAPI, getHealth, callMCPTool, searchKnowledge, getMemoryEntities, getNotes, getSystemInfo, getLogs, }; }; export default useBackendAPI;