Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

9.0 KB
Raw Blame
import { invoke } from '@tauri-apps/api/core'
import { listen, type UnlistenFn } from '@tauri-apps/api/event'
import {
 SessionInfo,
 DeviceList,
 UnloadResult,
 ReloadReport,
 EngineInfo,
 EngineVersion,
 GgufMetadata,
 DiagnosticsEvent,
 DiagnosticsJob,
 DiagnosticsPreset,
} from './types'

// Helpers
function asNumber(v: any, defaultValue = 0): number {
 if (v === '' || v === null || v === undefined) return defaultValue
 const n = Number(v)
 return isFinite(n) ? n : defaultValue
}

const I32_MAX = 2147483647
const I32_MIN = -2147483648

/**
 * Coerces to a value llama.cpp's int32 args accept, clamping rather than
 * overflowing. Exported because the extension needs the same clamp for
 * `timeout`, and a second hand-rolled one would drift.
 */
export function asI32(v: any, defaultValue = 0): number {
 const n = Math.trunc(asNumber(v, defaultValue))
 if (n > I32_MAX) return I32_MAX
 if (n < I32_MIN) return I32_MIN
 return n
}

export async function loadLlamaModel(
 modelId: string,
 isEmbedding: boolean = false
): Promise<SessionInfo> {
 return await invoke('plugin:llamacpp|load_llama_model', {
  modelId,
  isEmbedding,
 })
}

export async function unloadLlamaModel(modelId: string): Promise<UnloadResult> {
 return await invoke('plugin:llamacpp|unload_llama_model', { modelId })
}

export async function ensureSessionReady(
 modelId: string,
 isEmbedding: boolean = false
): Promise<SessionInfo> {
 return await invoke('plugin:llamacpp|ensure_session_ready', {
  modelId,
  isEmbedding,
 })
}

export async function getDevices(
 backendPath: string,
 envs: Record<string, string> = {}
): Promise<DeviceList[]> {
 return await invoke('plugin:llamacpp|get_devices', {
  backendPath,
  envs,
 })
}

export async function generateApiKey(
 modelId: string,
 apiSecret: string
): Promise<string> {
 return await invoke('plugin:llamacpp|generate_api_key', {
  modelId,
  apiSecret,
 })
}

export async function findSessionByModel(
 modelId: string
): Promise<SessionInfo | null> {
 return await invoke('plugin:llamacpp|find_session_by_model', { modelId })
}

export async function getLoadedModels(): Promise<string[]> {
 return await invoke('plugin:llamacpp|get_loaded_models')
}

/**
 * Starts the supervised in-process engine worker. Replaces `startRouter`: no
 * downloaded backend binary, and the port is OS-assigned then reported back
 * rather than guessed.
 */
/**
 * `slotCacheMib` caps the directory holding each thread's KV cache, which lets
 * a conversation resume without re-reading its prompt. 0 turns that off; a
 * single saved conversation runs to hundreds of MiB, so it is a budget rather
 * than a boolean.
 */
export async function startEngine(
 presetPath: string,
 modelsMax: number,
 slotCacheMib: number = 0,
 envs: Record<string, string> = {}
): Promise<EngineInfo> {
 return await invoke('plugin:llamacpp|start_engine', {
  presetPath,
  modelsMax,
  slotCacheMib: asI32(slotCacheMib),
  envs,
 })
}

export async function stopEngine(): Promise<void> {
 return await invoke('plugin:llamacpp|stop_engine')
}

/** Null when no worker is running, including after one died. */
export async function getEngineInfo(): Promise<EngineInfo | null> {
 return await invoke('plugin:llamacpp|get_engine_info')
}

/**
 * The bundled engine's identity, from compile-time constants.
 *
 * Unlike `getEngineInfo`, this answers whether or not a worker is running --
 * the engine is linked into the app, so its version is a property of the build
 * rather than of any process.
 */
export async function getEngineVersion(): Promise<EngineVersion> {
 return await invoke('plugin:llamacpp|get_engine_version')
}

/**
 * The devices the shipped engine can offload to.
 *
 * Replaces the `getDevices(backendPath, envs)` shell-out to a downloaded
 * `llama-server --list-devices`: there is no downloaded binary any more, and
 * the engine is statically linked into the worker.
 */
export async function engineDevices(
 envs: Record<string, string> = {}
): Promise<DeviceList[]> {
 return await invoke('plugin:llamacpp|engine_devices', { envs })
}

/**
 * Applies a regenerated preset to the running worker without restarting it.
 *
 * `modelsMax` is optional; omitting it keeps the worker's current value. Unlike
 * the router, the worker can be resized, so the embedding slot bonus changing
 * no longer forces a cold restart.
 */
/**
 * Kills the worker without waiting for it to unwind. Backs the force-quit the
 * busy-on-exit dialog offers; use `stopEngine` everywhere else.
 */
export async function forceStopEngine(): Promise<void> {
 return await invoke('plugin:llamacpp|force_stop_engine')
}

/**
 * True when nothing is generating, so the model can be reconfigured or
 * unloaded. Omit `modelId` to ask about the whole worker.
 */
export async function engineSlotsIdle(modelId?: string): Promise<boolean> {
 return await invoke('plugin:llamacpp|engine_slots_idle', { modelId })
}

/**
 * Claims the worker for a restart. `false` means a request holds it or a
 * generation is running, so nothing was claimed and the worker must be left
 * alone. On `true`, native requests (the local API server, the agent loop and
 * the model-lifecycle commands) wait until `endEngineRestart`, so none can
 * resolve the old worker's address after this answer. Always pair a `true`
 * with `endEngineRestart`.
 */
export async function beginEngineRestart(): Promise<boolean> {
 return await invoke('plugin:llamacpp|begin_engine_restart')
}

/** Lets native requests through again. Harmless without a restart in progress. */
export async function endEngineRestart(): Promise<void> {
 return await invoke('plugin:llamacpp|end_engine_restart')
}

/**
 * Drops saved KV cache: a thread's (under every model it was used with), or a
 * whole model's. Returns how many were dropped.
 *
 * `cacheDir` is the preset's `slot-save-path`. With no worker running the erase
 * is done on disk instead, so deleting a thread reclaims its cache whether or
 * not a model happens to be loaded; without the directory there is nowhere to
 * look and the answer is 0.
 */
export async function eraseThreadSlotState(args: {
 threadId?: string
 modelId?: string
 cacheDir?: string
}): Promise<number> {
 return await invoke('plugin:llamacpp|erase_thread_slot_state', {
  threadId: args.threadId,
  modelId: args.modelId,
  cacheDir: args.cacheDir,
 })
}

export async function reloadEngineModels(
 presetPath: string,
 modelsMax?: number,
 slotCacheMib?: number
): Promise<ReloadReport> {
 return await invoke('plugin:llamacpp|reload_engine_models', {
  presetPath,
  modelsMax,
  slotCacheMib:
   slotCacheMib === undefined ? undefined : asI32(slotCacheMib),
 })
}

// GGUF commands
export async function readGgufMetadata(path: string): Promise<GgufMetadata> {
 return await invoke('plugin:llamacpp|read_gguf_metadata', { path })
}

/**
 * Which of `names` exist as tensors in a gguf. Exact names, mirroring
 * llama.cpp's `gguf_find_tensor`: the facts worth knowing are yes/no ones
 * (`markov_w1.weight` marks a DSpark draft), and listing every tensor of a
 * large model to answer one would be far more data than the answer.
 */
export async function findGgufTensors(
 path: string,
 names: string[]
): Promise<string[]> {
 return await invoke('plugin:llamacpp|find_gguf_tensors', { path, names })
}

export async function isModelSupported(
 path: string,
 ctxSize?: number
): Promise<'RED' | 'YELLOW' | 'GREEN'> {
 return await invoke('plugin:llamacpp|is_model_supported', {
  path,
  ctxSize,
 })
}

// Cleanup commands
export async function cleanupLlamaProcesses(): Promise<void> {
 return await invoke('plugin:llamacpp|cleanup_llama_processes')
}

// The ggml op diagnostic

/** Every event of every run arrives here, tagged with its job id. */
const DIAGNOSTICS_EVENT = 'llamacpp-diagnostics'

/**
 * Starts a hardware check and returns its id. One run at a time: the check
 * saturates every core, so a second would make both unreliable.
 *
 * Events arrive via `onDiagnosticsEvent` until one carries `event: 'done'`,
 * which is emitted on every path including a run that could not start.
 *
 * `model` is a model id and is required by the `model` preset, which resolves
 * it to its gguf and runs only the families that model's graph contains. The
 * other presets ignore it.
 */
export async function startDiagnostics(
 preset: DiagnosticsPreset,
 devices: string[] = [],
 envs: Record<string, string> = {},
 model?: string
): Promise<DiagnosticsJob> {
 return await invoke('plugin:llamacpp|start_diagnostics', {
  preset,
  devices,
  envs,
  model,
 })
}

/**
 * Asks a run to stop. Resolves once the request is made, not once it lands --
 * the terminal `done` event is what says the run is over.
 */
export async function cancelDiagnostics(job: string): Promise<void> {
 return await invoke('plugin:llamacpp|cancel_diagnostics', { job })
}

/**
 * Subscribes to the run stream. Filter on `job`: events from a superseded run
 * can still be in flight when a new one starts.
 */
export async function onDiagnosticsEvent(
 handler: (event: DiagnosticsEvent) => void
): Promise<UnlistenFn> {
 return await listen<DiagnosticsEvent>(DIAGNOSTICS_EVENT, (e) =>
  handler(e.payload)
 )
}

export * from './types'