121 lines
3.2 KiB
TypeScript
121 lines
3.2 KiB
TypeScript
import { invoke } from "@tauri-apps/api/core";
|
|
import { createDefaultRuntimeSnapshot } from "./runtime-utils";
|
|
import type {
|
|
RuntimeConfig,
|
|
RuntimeGpuInfo,
|
|
RuntimeModelFile,
|
|
RuntimePickDirResult,
|
|
RuntimeSnapshot,
|
|
} from "./types";
|
|
|
|
const STORAGE_KEY = "forgelm_runtime_snapshot_v1";
|
|
|
|
function canUseLocalStorage(): boolean {
|
|
return typeof window !== "undefined" && typeof window.localStorage !== "undefined";
|
|
}
|
|
|
|
function readFallbackSnapshot(): RuntimeSnapshot {
|
|
if (!canUseLocalStorage()) {
|
|
return createDefaultRuntimeSnapshot();
|
|
}
|
|
|
|
const raw = window.localStorage.getItem(STORAGE_KEY);
|
|
if (!raw) {
|
|
return createDefaultRuntimeSnapshot();
|
|
}
|
|
|
|
try {
|
|
return { ...createDefaultRuntimeSnapshot(), ...JSON.parse(raw) } as RuntimeSnapshot;
|
|
} catch {
|
|
return createDefaultRuntimeSnapshot();
|
|
}
|
|
}
|
|
|
|
function writeFallbackSnapshot(snapshot: RuntimeSnapshot): RuntimeSnapshot {
|
|
if (canUseLocalStorage()) {
|
|
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(snapshot));
|
|
}
|
|
return snapshot;
|
|
}
|
|
|
|
export async function bootstrapRuntime(): Promise<RuntimeSnapshot> {
|
|
try {
|
|
return await invoke<RuntimeSnapshot>("bootstrap_runtime_command");
|
|
} catch {
|
|
return readFallbackSnapshot();
|
|
}
|
|
}
|
|
|
|
export async function getRuntimeSnapshot(): Promise<RuntimeSnapshot> {
|
|
try {
|
|
return await invoke<RuntimeSnapshot>("get_runtime_snapshot_command");
|
|
} catch {
|
|
return readFallbackSnapshot();
|
|
}
|
|
}
|
|
|
|
export async function saveRuntimeConfig(config: RuntimeConfig): Promise<RuntimeSnapshot> {
|
|
try {
|
|
return await invoke<RuntimeSnapshot>("save_runtime_config_command_handler", { config });
|
|
} catch {
|
|
const snapshot = readFallbackSnapshot();
|
|
return writeFallbackSnapshot({ ...snapshot, config });
|
|
}
|
|
}
|
|
|
|
export async function startRuntime(): Promise<RuntimeSnapshot> {
|
|
try {
|
|
return await invoke<RuntimeSnapshot>("start_runtime_command");
|
|
} catch {
|
|
const snapshot = readFallbackSnapshot();
|
|
return writeFallbackSnapshot({
|
|
...snapshot,
|
|
status: "error",
|
|
last_error: { code: "tauri_unavailable", message: "Local runtime control requires the Tauri desktop app." },
|
|
});
|
|
}
|
|
}
|
|
|
|
export async function stopRuntime(): Promise<RuntimeSnapshot> {
|
|
try {
|
|
return await invoke<RuntimeSnapshot>("stop_runtime_command");
|
|
} catch {
|
|
const snapshot = readFallbackSnapshot();
|
|
return writeFallbackSnapshot({ ...snapshot, status: "stopped", process_running: false, ready: false });
|
|
}
|
|
}
|
|
|
|
export async function queryRuntimeGpu(): Promise<RuntimeGpuInfo | null> {
|
|
try {
|
|
return await invoke<RuntimeGpuInfo>("query_gpu");
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function scanRuntimeModels(dir: string): Promise<RuntimeModelFile[]> {
|
|
return invoke<RuntimeModelFile[]>("scan_models", { dir });
|
|
}
|
|
|
|
export async function pickRuntimeModelsDir(): Promise<RuntimePickDirResult | null> {
|
|
try {
|
|
return await invoke<RuntimePickDirResult>("pick_models_dir");
|
|
} catch (error) {
|
|
if (String(error) === "cancelled") {
|
|
return null;
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
export async function pickRuntimeModelFiles(): Promise<RuntimeModelFile[]> {
|
|
try {
|
|
return await invoke<RuntimeModelFile[]>("pick_model_files");
|
|
} catch (error) {
|
|
if (String(error) === "cancelled") {
|
|
return [];
|
|
}
|
|
throw error;
|
|
}
|
|
}
|