import { authFetch } from "@/features/auth"; async function readError(response: Response): Promise { try { const payload = (await response.json()) as { detail?: string; message?: string }; return payload.detail || payload.message || `Request failed (${response.status})`; } catch { return `Request failed (${response.status})`; } } async function parseJson(response: Response): Promise { if (!response.ok) { throw new Error(await readError(response)); } return (await response.json()) as T; } export interface CheckpointInfo { display_name: string; path: string; loss?: number | null; } export interface ModelCheckpoints { name: string; checkpoints: CheckpointInfo[]; base_model?: string | null; peft_type?: string | null; lora_rank?: number | null; } export interface CheckpointListResponse { outputs_dir: string; models: ModelCheckpoints[]; } export interface ExportOperationResponse { success: boolean; message: string; details?: Record | null; } export async function fetchCheckpoints(): Promise { const response = await authFetch("/api/models/checkpoints"); return parseJson(response); } export async function loadCheckpoint(params: { checkpoint_path: string; max_seq_length?: number; load_in_4bit?: boolean; }): Promise { const response = await authFetch("/api/export/load-checkpoint", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(params), }); return parseJson(response); } export async function exportMerged(params: { save_directory: string; format_type?: string; push_to_hub?: boolean; repo_id?: string | null; hf_token?: string | null; private?: boolean; }): Promise { const response = await authFetch("/api/export/export/merged", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(params), }); return parseJson(response); } export async function exportBase(params: { save_directory: string; push_to_hub?: boolean; repo_id?: string | null; hf_token?: string | null; private?: boolean; base_model_id?: string | null; }): Promise { const response = await authFetch("/api/export/export/base", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(params), }); return parseJson(response); } export async function exportGGUF(params: { save_directory: string; quantization_method: string; push_to_hub?: boolean; repo_id?: string | null; hf_token?: string | null; }): Promise { const response = await authFetch("/api/export/export/gguf", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(params), }); return parseJson(response); } export async function exportLoRA(params: { save_directory: string; push_to_hub?: boolean; repo_id?: string | null; hf_token?: string | null; private?: boolean; }): Promise { const response = await authFetch("/api/export/export/lora", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(params), }); return parseJson(response); } export async function cleanupExport(): Promise { const response = await authFetch("/api/export/cleanup", { method: "POST" }); return parseJson(response); }