import { apiFetch, type PaginatedResponse } from './api'; export interface ActionItem { type: string; params: Record; } export interface ChatResponse { response: string; actions: ActionItem[]; session_id: string; message_id: string; } export interface ActionResponse { action: string; result: unknown; success: boolean; } export interface ChatMessage { id: string; session_id: string; role: 'user' | 'assistant'; content: string; actions?: ActionItem[] | null; created_at?: string; } export interface VoiceResponse { transcription: string; response: string; actions: ActionItem[]; session_id: string; message_id: string; } export async function sendChatMessage( message: string, sessionId?: string ): Promise { return apiFetch('/copilot/chat', { method: 'POST', body: JSON.stringify({ message, session_id: sessionId || undefined }), }); } export async function executeAction( action: string, params: Record, sessionId?: string ): Promise { return apiFetch('/copilot/action', { method: 'POST', body: JSON.stringify({ action, params, session_id: sessionId || undefined }), }); } export async function getChatHistory( page = 1, pageSize = 20, sessionId?: string ): Promise> { let path = `/copilot/history?page=${page}&page_size=${pageSize}`; if (sessionId) { path += `&session_id=${sessionId}`; } return apiFetch>(path); } export async function sendVoiceMessage( audioBase64: string, mimeType = 'audio/webm', sessionId?: string ): Promise { return apiFetch('/copilot/voice', { method: 'POST', body: JSON.stringify({ audio: audioBase64, mime_type: mimeType, session_id: sessionId || undefined, }), }); } export function fileToBase64(file: Blob): Promise { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = () => { const result = reader.result as string; const base64 = result.split(',')[1] || result; resolve(base64); }; reader.onerror = reject; reader.readAsDataURL(file); }); }