Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

3.2 KB
Raw Blame
import { isTauri } from "@/lib/api-base";
import type {
  NativeIntent,
  NativePathLeaseResponse,
  NativePathOperation,
} from "./types";

async function invokeNative<T>(
  command: string,
  args?: Record<string, unknown>,
): Promise<T> {
  if (!isTauri) {
    throw new Error(
      "Native desktop features are only available in the Tauri app.",
    );
  }
  const { invoke } = await import("@tauri-apps/api/core");
  return invoke<T>(command, args);
}

export interface NativeDocumentFolderSelection {
  /** Opaque directory lease, deliberately not a filesystem path. */
  token: string;
  displayName: string;
  expiresAtMs?: number;
}

export async function pickNativeDocumentFolder(): Promise<NativeDocumentFolderSelection | null> {
  if (!isTauri) {
    throw new Error(
      "Persistent local folder sync is only available in the desktop app.",
    );
  }
  return invokeNative<NativeDocumentFolderSelection | null>(
    "pick_native_document_folder",
  );
}

export async function drainNativeIntents(): Promise<NativeIntent[]> {
  if (!isTauri) return [];
  return invokeNative<NativeIntent[]>("drain_native_intents");
}

export async function pickHuggingFaceCacheDir(): Promise<string | null> {
  if (!isTauri) return null;
  return invokeNative<string | null>("pick_hugging_face_cache_dir");
}

/** Desktop owner's OS folder dialog. `undefined` = use the in-app browser (web app,
 * managed account confined to its workspace, or the dialog failed to open). */
export async function pickNativeModelFolder(
  isOwner: boolean,
): Promise<string | null | undefined> {
  if (!isTauri || !isOwner) return undefined;
  try {
    return await invokeNative<string | null>("pick_native_model_folder");
  } catch {
    return undefined;
  }
}

export async function registerNativeModelPath(
  path: string,
): Promise<NativeIntent> {
  return invokeNative<NativeIntent>("register_native_model_path", { path });
}

export async function registerNativeAttachmentPath(
  path: string,
): Promise<NativeIntent> {
  return invokeNative<NativeIntent>("register_native_attachment_path", {
    path,
  });
}

export async function registerNativeDatasetPath(
  path: string,
): Promise<NativeIntent> {
  return invokeNative<NativeIntent>("register_native_dataset_path", { path });
}

export async function readNativeAttachmentFile(
  token: string,
): Promise<{ name: string; mimeType: string; base64: string }> {
  return invokeNative<{ name: string; mimeType: string; base64: string }>(
    "read_native_attachment_file",
    { token },
  );
}

export async function consumeNativePathToken(
  token: string,
  operation: NativePathOperation,
): Promise<NativePathLeaseResponse> {
  return invokeNative<NativePathLeaseResponse>("consume_native_path_token", {
    token,
    operation,
  });
}

export async function revealPathToken(token: string): Promise<void> {
  return invokeNative<void>("reveal_path_token", { token });
}

export async function openPathToken(token: string): Promise<void> {
  return invokeNative<void>("open_path_token", { token });
}

// Tauri validates that the path is a real directory.
export async function openModelsDir(path: string): Promise<void> {
  return invokeNative<void>("open_models_dir", { path });
}