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.

106 KB
Raw Blame
/**
 * @file The llama.cpp built-in: `activate(ctx)` registers the in-process
 * engine with the host's engine manager and provides `ServiceKeys.Llamacpp`.
 * @module llamacpp-extension/src/index
 */

import {
 AIEngine,
 type EmbeddingEngine,
 getJanDataFolderPath,
 fs,
 joinPath,
 modelInfo,
 SessionInfo,
 UnloadResult,
 chatCompletion,
 chatCompletionChunk,
 ImportOptions,
 type SpecDraftKind,
 chatCompletionRequest,
 events,
 AppEvent,
 DownloadEvent,
 ServiceKeys,
 type DownloadService,
 type InternalExtensionContext,
 type LlamacppService,
 type LoopbackFetch,
 type ServiceRegistry,
 chatCompletionRequestMessage,
 SettingComponentProps,
 DropdownComponentProps,
 logger,
} from '@janhq/core'
import {
 readSettingsFile,
 writeSettingsFile,
 settingsFileExists,
} from './settings-store'
import { applyPlatformFitDefault } from './fit-default'
import { findModelEntries } from './model-tree'
import { applyLegacyLoadMode, parseLoadMode } from './load-mode'

import {
 getProxyConfig,
 buildEmbedBatches,
 mergeEmbedResponses,
 detectEmbeddingFromGgufMeta,
 detectMtpLayersFromGgufMeta,
 detectExpertCountFromGgufMeta,
 resolveSpecDraftKind,
 detectTemplateKwargsFromChatTemplate,
 getDefaultEmbeddingModelId,
 setDefaultEmbeddingModelId,
 fileName,
 type EmbedBatchResult,
} from './util'
import {
 generatePreset,
 threadCacheDir,
 DEFAULT_EMBEDDING_UBATCH,
} from './preset'
import {
 evaluateEmbeddingVector,
 evaluateGpuOffload,
 backendFromDeviceIds,
 countAcceleratorDevices,
 type EmbeddingVectorProblem,
 type GpuOffloadCheck,
 type ReadinessStatus,
} from './readiness'
import {
 getBackendSetting,
 setBackendSetting,
 removeBackendSetting,
} from './backend-settings'
import {
 asI32,
 loadLlamaModel,
 readGgufMetadata,
 findGgufTensors,
 isModelSupported,
 unloadLlamaModel,
 startEngine,
 stopEngine,
 beginEngineRestart,
 endEngineRestart,
 eraseThreadSlotState,
 getEngineInfo as pluginGetEngineInfo,
 getEngineVersion as pluginGetEngineVersion,
 reloadEngineModels,
 engineDevices,
 generateApiKey as pluginGenerateApiKey,
 findSessionByModel as pluginFindSessionByModel,
 ensureSessionReady as pluginEnsureSessionReady,
 getLoadedModels as pluginGetLoadedModels,
 LlamacppConfig,
 DownloadItem,
 ModelConfig,
 TemplateKwarg,
 EmbeddingResponse,
 ModelProps,
 DeviceList,
 EngineVersion,
} from '@janhq/tauri-plugin-llamacpp-api'
import { overrideAmdVulkanMemory } from './deviceMemory'
import { getSystemUsage, getSystemInfo } from '@janhq/tauri-plugin-hardware-api'

const EMBEDDING_CHECK_VERSION = 3
const MTP_CHECK_VERSION = 1
const EXPERT_CHECK_VERSION = 1

// The tensor that marks a DSpark draft: it is a DFlash one plus this Markov
// head (common/speculative.cpp, llama-arch.cpp LLM_TENSOR_DSPARK_MARKOV_W1).
const MARKOV_HEAD_TENSOR = 'markov_w1.weight'
const TEMPLATE_KWARGS_CHECK_VERSION = 1

// Provider settings that end up in `router.preset.ini` (`[*]` global section
// in preset.ts). Mutating any of these needs the preset reloaded so the new
// value is read; cosmetic / process-only keys (models_max, timeout,
// llamacpp_env) are handled separately or not at all.
const PRESET_AFFECTING_KEYS = new Set<string>([
 'fit',
 'fit_target',
 'fit_ctx',
 'flash_attn',
 'cache_type_k',
 'cache_type_v',
 'parallel',
 'cont_batching',
 'threads',
 'threads_batch',
 'n_predict',
 'batch_size',
 'ubatch_size',
 'n_cpu_moe',
 'n_cpu_ffn',
 'no_kv_offload',
 'device',
 'split_mode',
 'main_gpu',
 'load_mode',
 'rope_scaling',
 'rope_freq_base',
 'rope_freq_scale',
 'ctx_shift',
 'cache_ram',
 // persist_thread_cache decides whether slot-save-path is emitted at all;
 // thread_cache_size is not a preset key but rides the same reload, which
 // carries it to the worker (see reload_engine_models).
 'persist_thread_cache',
 'thread_cache_size',
 'cache_reuse',
 'swa_full',
 'keep',
 'kv_unified',
 'tensor_split',
 'no_op_offload',
 'ctx_checkpoints',
 'checkpoint_min_step',
 'kv_unified_per_slot',
 'reasoning_preserve',
 'lazy_mode',
])


type YamlSettingValue = string | number | boolean | null

type PersistedProviderSetting = {
 controller_props?: {
  value?: unknown
 }
}

type PersistedModelState = {
 id?: unknown
 settings?: Record<string, PersistedProviderSetting>
}

const MODEL_PROVIDER_STORE_KEY = 'model-provider'
const EMBEDDER_BOOTSTRAP_KEY = 'llamacpp-embedder-bootstrapped'
/** Set once the user has agreed to the first-run download. */
const SETUP_CONSENT_KEY = 'llamacpp-first-run-setup-started'
const FALLBACK_EMBEDDING_MODEL_ID = 'sentence-transformer-mini'
const FALLBACK_EMBEDDING_MODEL_URL =
 'https://huggingface.co/second-state/All-MiniLM-L6-v2-Embedding-GGUF/resolve/main/all-MiniLM-L6-v2-ggml-model-f16.gguf?download=true'
const LLAMACPP_MODEL_SETTINGS_BACKFILL_KEY =
 'llamacpp_model_yaml_backfill_v2'

/// The GPU-layers value the old UI shipped as its default. v1 of the backfill
/// copied it into every model.yml, which pins offload to 100 layers and defeats
/// llama.cpp's own -1 (auto, VRAM-aware) -- an OOM on a small GPU. v2 removes it
/// again. A user who deliberately chose exactly 100 loses that choice, which is
/// the right trade: 100 was never a considered value, it was the default.
const LEGACY_NGL_DEFAULT = 100
/// Separate from the backfill key: the sweep now reaches nested Hub folders,
/// and every existing install already has the backfill key set.
const LEGACY_NGL_SWEPT_KEY = 'llamacpp_legacy_ngl_swept_v1'

/// The context size the web-app seeded into every llamacpp model while fit was
/// off by default; see migrateSeededCtxSize.
const SEEDED_CTX_SIZE = 8192
const SEEDED_CTX_CLEARED_KEY = 'llamacpp_seeded_ctx_cleared_v1'

/// Matches the `timeout` default in settings.json.
const DEFAULT_TIMEOUT = 600

// Short and non-empty: enough to exercise tokenize plus pooling without making
// setup wait on a long prompt.
const EMBEDDING_PROBE_TEXT = 'jan setup embedding probe'

export interface EmbeddingModelReport {
 status: ReadinessStatus
 modelId?: string
 dimension?: number
 problem?: EmbeddingVectorProblem
 error?: string
 /** The engine has not finished setting up, so nothing was concluded. */
 pending?: boolean
}

export interface GpuOffloadReport extends GpuOffloadCheck {
 backend: string
 /** Set when the device probe itself failed, leaving `reason` undetermined. */
 error?: string
 /** The engine has not finished setting up, so nothing was concluded. */
 pending?: boolean
}

// Sampling defaults are floats/ints where 0 is a meaningful value (e.g.
// temperature=0), so unlike ctx_len these coercions keep 0 and only reject
// blank/non-finite input.
const coerceFloatSetting = (v: unknown): YamlSettingValue => {
 if (v === '' || v == null) return null
 const n = typeof v === 'number' ? v : Number(v)
 return Number.isFinite(n) ? n : null
}
const coerceIntSetting = (v: unknown): YamlSettingValue => {
 if (v === '' || v == null) return null
 const n = typeof v === 'number' ? v : Number(v)
 return Number.isFinite(n) ? Math.floor(n) : null
}

const MODEL_SETTINGS_YAML_MAPPING: Record<
 string,
 {
  yamlKey: string
  coerce: (v: unknown) => YamlSettingValue
 }
> = {
 // Sampling defaults: persisted to model.yml and emitted into the router
 // preset so they apply server-side to every request (chat and external API),
 // overridable per-request. Keys mirror MODEL_SAMPLING_SETTING_KEYS in the
 // web-app transport.
 temperature: { yamlKey: 'temperature', coerce: coerceFloatSetting },
 top_k: { yamlKey: 'top_k', coerce: coerceIntSetting },
 top_p: { yamlKey: 'top_p', coerce: coerceFloatSetting },
 min_p: { yamlKey: 'min_p', coerce: coerceFloatSetting },
 // Not coerceIntSetting: upstream throws on a negative window on both the
 // preset path and the per-request path, so a legacy -1 from the old
 // "-1 = full context" UI has to be dropped, not passed through. Omitting the
 // key lets upstream's own default (64) apply.
 repeat_last_n: {
  yamlKey: 'repeat_last_n',
  coerce: (v) => {
   if (v === '' || v == null) return null
   const n = typeof v === 'number' ? v : Number(v)
   return Number.isFinite(n) && n >= 0 ? Math.floor(n) : null
  },
 },
 repeat_penalty: { yamlKey: 'repeat_penalty', coerce: coerceFloatSetting },
 presence_penalty: { yamlKey: 'presence_penalty', coerce: coerceFloatSetting },
 frequency_penalty: {
  yamlKey: 'frequency_penalty',
  coerce: coerceFloatSetting,
 },
 ctx_len: {
  yamlKey: 'ctx_size',
  coerce: (v) => {
   if (v === '' || v == null) return null
   const n = typeof v === 'number' ? v : Number(v)
   return Number.isFinite(n) && n > 0 ? Math.floor(n) : null
  },
 },
 ngl: {
  yamlKey: 'n_gpu_layers',
  coerce: (v) => {
   if (v === '' || v == null) return null
   const n = typeof v === 'number' ? v : Number(v)
   // -1 is auto (VRAM-aware) and -2 or below means all layers, so the floor
   // is -2 rather than 0.
   return Number.isFinite(n) && n >= -2 ? Math.floor(n) : null
  },
 },
 chat_template: {
  yamlKey: 'chat_template',
  coerce: (v) =>
   typeof v === 'string' && v.trim().length > 0 ? v : null,
 },
 grammar: {
  yamlKey: 'grammar',
  coerce: (v) =>
   typeof v === 'string' && v.trim().length > 0 ? v : null,
 },
 batch_size: {
  yamlKey: 'batch_size',
  coerce: (v) => {
   if (v === '' || v == null) return null
   const n = typeof v === 'number' ? v : Number(v)
   return Number.isFinite(n) && n > 0 ? Math.floor(n) : null
  },
 },
 ubatch_size: {
  yamlKey: 'ubatch_size',
  coerce: (v) => {
   if (v === '' || v == null) return null
   const n = typeof v === 'number' ? v : Number(v)
   return Number.isFinite(n) && n > 0 ? Math.floor(n) : null
  },
 },
 cpu_moe: {
  yamlKey: 'cpu_moe',
  coerce: (v) => (v === true ? true : null),
 },
 n_cpu_moe: {
  yamlKey: 'n_cpu_moe',
  coerce: (v) => {
   if (v === '' || v == null) return null
   const n = typeof v === 'number' ? v : Number(v)
   return Number.isFinite(n) && n > 0 ? Math.floor(n) : null
  },
 },
 n_cpu_ffn: {
  yamlKey: 'n_cpu_ffn',
  coerce: (v) => {
   if (v === '' || v == null) return null
   const n = typeof v === 'number' ? v : Number(v)
   return Number.isFinite(n) && n > 0 ? Math.floor(n) : null
  },
 },
 moe_cache_mib: {
  yamlKey: 'moe_cache_mib',
  coerce: (v) => {
   if (v === '' || v == null) return null
   const n = typeof v === 'number' ? v : Number(v)
   return Number.isFinite(n) && n > 0 ? Math.floor(n) : null
  },
 },
 moe_cache_cpu_share: {
  yamlKey: 'moe_cache_cpu_share',
  coerce: (v) => {
   if (v === '' || v == null) return null
   const n = typeof v === 'number' ? v : Number(v)
   return Number.isFinite(n) && n > 0 && n <= 1 ? n : null
  },
 },
 no_kv_offload: {
  yamlKey: 'no_kv_offload',
  coerce: (v) => (v === true ? true : null),
 },
 override_tensor_buffer_t: {
  yamlKey: 'override_tensor',
  coerce: (v) =>
   typeof v === 'string' && v.trim().length > 0 ? v.trim() : null,
 },
 offload_mmproj: {
  yamlKey: 'mmproj_offload',
  coerce: (v) => (v === false ? false : null),
 },
 // Empty is the UI's "inherit global": null drops the key, so the model gets
 // no load-mode line. Any valid value, auto included, is kept because it
 // overrides a non-auto global.
 load_mode: { yamlKey: 'load_mode', coerce: parseLoadMode },
}

async function readPersistedLlamacppModels(): Promise<PersistedModelState[]> {
 try {
  const raw = await getBackendSetting(MODEL_PROVIDER_STORE_KEY)
  if (!raw) return []
  const parsed = JSON.parse(raw)
  const providers = parsed?.state?.providers
  if (!Array.isArray(providers)) return []
  const llamacpp = providers.find(
   (provider: { provider?: unknown }) => provider?.provider === 'llamacpp'
  )
  return Array.isArray(llamacpp?.models) ? llamacpp.models : []
 } catch (error) {
  logger.warn('Failed to read persisted llamacpp model settings:', error)
  return []
 }
}

// Folder structure for llamacpp extension:
// <Jan's data folder>/llamacpp
//  - models/<modelId>/
//    - model.yml (required)
//    - model.gguf (optional, present if downloaded from URL)
//    - mmproj.gguf (optional, present if mmproj exists and it was downloaded from URL)
// Contents of model.yml can be found in ModelConfig interface
//
//  - backends/<backend_version>/<backend_type>/
//    - build/bin/llama-server (or llama-server.exe on Windows)
//
//  - lib/
//    - e.g. libcudart.so.12

// The package name: what the settings entries carry as their owner, and the
// key the settings lived under in webview storage before the file store.
const PACKAGE_NAME = '@janhq/llamacpp-extension'

/**
 * Runs the engine on the extension host: registers it with the engine
 * manager and publishes the runtime queries the app makes of it.
 */
export async function activate(ctx: InternalExtensionContext): Promise<void> {
 const engine = new llamacpp_extension(
  ctx.internal.services,
  ctx.internal.loopbackFetch
 )
 ctx.subscriptions.add(ctx.internal.engines.register(engine))
 ctx.subscriptions.add({ dispose: () => engine.dispose() })
 const service: LlamacppService = {
  getModelProps: (modelId) => engine.getModelProps(modelId),
  unload: (modelId) => engine.unload(modelId),
  getDevices: () => engine.getDevices(),
  reconcileEngineDevices: () => engine.reconcileEngineDevices(),
 }
 ctx.subscriptions.add(ctx.internal.services.provide(ServiceKeys.Llamacpp, service))
 // As under the old extension manager, a failed startup is logged and the
 // engine stays registered, so the provider and its models remain listed.
 await engine.start().catch((error: unknown) =>
  ctx.log.error('Engine startup failed:', error)
 )
}

export default class llamacpp_extension extends AIEngine implements EmbeddingEngine {
 provider: string = 'llamacpp'
 timeout: number = 600
 llamacpp_env: string = ''
 readonly providerId: string = 'llamacpp'

 private config: LlamacppConfig
 private providerPath!: string
 private apiSecret: string = 'JustAskNow'
 private pendingDownloads: Map<string, Promise<void>> = new Map()
 /** Keyed by modelId; two imports of one model would cancel each other. */
 private pendingImports: Map<string, Promise<void>> = new Map()
 private embedderBootstrapError?: string
 /**
  * True while the fallback embedder is being fetched.
  *
  * The old readiness gate keyed off "no backend selected yet", which happened
  * to cover this window on a fresh install. The engine ships with the app now,
  * so that proxy is gone and this is the direct signal -- without it, a normal
  * first-run download is reported as a failed embedding check.
  */
 private embedderBootstrapping = false
 private loadingModels = new Map<string, Promise<SessionInfo>>() // Track loading promises

 private enginePort?: number
 private engineApiKey?: string
 private engineVersion?: EngineVersion
 private presetPath?: string
 private engineStartLock: Promise<void> | null = null
 // Requests holding the engine's port and key (see acquireEngineLease), and the
 // gate a device restart raises so that none is admitted while it decides.
 private engineLeases = 0
 private engineGate: Promise<void> | null = null
 // Reconciliations run one after another; see reconcileEngineDevices.
 private reconcileQueue: Promise<void> = Promise.resolve()
 private userModelsMax: number = 1
 private engineEmbeddingBonus: number = 0
 private loadedChatOrder: string[] = []

 // The engine worker spawn runs off the onLoad critical path; awaited via
 // ensureEngineReady() before any model load so inference never races it.
 private backgroundInit?: Promise<void>
 /** Single-flight provisioning run; see ensureProvisioned(). */
 private provisioning?: Promise<void>

 constructor(
  private readonly services: Pick<ServiceRegistry, 'get'>,
  // Requests to the engine on loopback go through the host's proxy-bypassing
  // client; see LoopbackFetch.
  private readonly loopbackFetch: LoopbackFetch
 ) {
  super()
 }

 async start(): Promise<void> {
  await this.migrateLocalStorageToFile()

  // The engine is bundled at a pinned version, so there is no backend or
  // version to migrate or select. `registerSettings` drops the keys that no
  // longer exist in SETTINGS (llamacpp_version, llamacpp_backend,
  // check_for_updates, auto_update_engine, verify_backend_deps) from the
  // persisted set on its own.
  const settings = structuredClone(SETTINGS)
  applyPlatformFitDefault(settings, IS_MAC)
  await this.registerSettings(settings)

  let loadedConfig: any = {}
  for (const item of settings) {
   const defaultValue = item.controllerProps.value
   // Use the potentially updated default value from the settings array as the fallback for getSetting
   loadedConfig[item.key] = await this.getSetting<typeof defaultValue>(
    item.key,
    defaultValue
   )
  }
  this.config = loadedConfig as LlamacppConfig

  // No fit migration: the settings file stores every value with no record of
  // who set it, so a stored `fit: false` cannot be told apart from an explicit
  // off. Existing installs keep what they have; the default reaches new ones.
  await this.migrateAutoUnloadToModelsMax()

  await this.migrateParallelToDefault()

  this.timeout = asI32(this.config.timeout, DEFAULT_TIMEOUT) || DEFAULT_TIMEOUT
  this.llamacpp_env = this.config.llamacpp_env

  // This sets the base directory where model files for this provider are stored.
  await this.getProviderPath()

  await this.migrateLegacyNgl()

  await this.migratePersistedModelSettingsToYaml()

  // After the backfill, which is what copied the seed into model.yml.
  await this.migrateSeededCtxSize()

  // Deferred off onLoad so the UI unblocks; performLoad awaits it via
  // ensureEngineReady(). The engine is bundled with the app now, so there is
  // no backend catalog fetch and no hundreds-of-megabytes download to ask
  // consent for -- only the fallback embedder, which bootstrapDefaultEmbedder
  // still gates on setup consent.
  this.backgroundInit = this.ensureProvisioned()
 }

 private async hasSetupConsent(): Promise<boolean> {
  try {
   return Boolean(await getBackendSetting(SETUP_CONSENT_KEY))
  } catch (e) {
   // A readable answer is not worth blocking startup over; erring towards
   // "not consented" only defers work the user can still trigger.
   logger.warn('Could not read the first-run setup flag:', e)
   return false
  }
 }

 /**
  * Runs the first-run provisioning the setup screen asked for, and remembers
  * that it was asked so a later launch does not wait again.
  */
 async startFirstRunSetup(): Promise<void> {
  try {
   await setBackendSetting(SETUP_CONSENT_KEY, 'true')
  } catch (e) {
   logger.warn('Could not persist the first-run setup flag:', e)
  }
  await this.ensureProvisioned()
 }

 /**
  * Starts the engine, then installs the fallback embedder. Single-flight:
  * startup, the setup screen and the first model load can all ask for it, and
  * only one run happens.
  *
  * There is no backend selection step any more -- the engine is linked into
  * the shipped worker at a pinned version, so the ordering dance between
  * "download a backend first" and "start first for availability" is gone.
  */
 private ensureProvisioned(): Promise<void> {
  this.provisioning ??= (async () => {
   try {
    await this.startEngine()
   } catch (e) {
    logger.error('Engine failed to start during provisioning:', e)
    this.reportMissingLibrariesFromError(e)
   }
   await this.bootstrapDefaultEmbedder()
  })()
  this.backgroundInit = this.provisioning
  return this.provisioning
 }

 /**
  * One-shot startup install of the fallback embedder so the router reserves
  * the +1 embedding slot from its first start instead of importing the model
  * mid-session on the first RAG call. Runs after the router is up so the
  * download never delays chat availability; the import's preset refresh then
  * resizes models_max via an idle restart (nothing is loaded yet at startup).
  * The persisted flag keeps this from resurrecting a model the user deleted,
  * and is only set on success so a failed download retries next launch.
  */
 private async bootstrapDefaultEmbedder(): Promise<void> {
  try {
   if (await getBackendSetting(EMBEDDER_BOOTSTRAP_KEY)) return
   if (!(await this.hasEmbedderInstalled())) {
    // Set only around the actual fetch, and only when there is one to do:
    // an install that is already present must not flash a pending state.
    this.embedderBootstrapping = true
    await this.import(FALLBACK_EMBEDDING_MODEL_ID, {
     modelPath: FALLBACK_EMBEDDING_MODEL_URL,
    })
    // A stopped or cancelled download resolves without throwing, so the
    // install has to be confirmed before the one-shot flag is recorded --
    // otherwise bootstrap marks itself done and never retries.
    if (!(await this.hasEmbedderInstalled())) {
     throw new Error(
      `Import of "${FALLBACK_EMBEDDING_MODEL_ID}" did not complete`
     )
    }
    logger.info(
     `Pre-installed fallback embedding model "${FALLBACK_EMBEDDING_MODEL_ID}" at startup`
    )
   }
   await setBackendSetting(EMBEDDER_BOOTSTRAP_KEY, 'true')
   this.embedderBootstrapError = undefined
  } catch (e) {
   this.embedderBootstrapError = e instanceof Error ? e.message : String(e)
   logger.warn(
    'Fallback embedder bootstrap failed (will import on demand):',
    e
   )
  } finally {
   // In `finally` so a thrown import cannot leave the checklist reporting
   // "downloading" for the rest of the session.
   this.embedderBootstrapping = false
  }
 }

 private async hasEmbedderInstalled(): Promise<boolean> {
  const models = await this.list()
  return models.some((m) => (m as { embedding?: boolean }).embedding === true)
 }

 /**
  * Why the startup embedder install failed, for setup to report. Undefined
  * when it succeeded or has not run; the import is retried on demand, so this
  * is advisory rather than terminal.
  */
 /**
  * A launch failure that names unresolvable libraries is the one dependency
  * problem a bundled engine still has: the GPU driver runtime (CUDA, Vulkan)
  * lives on the user's machine, not in the app. Raises the same dependency
  * dialog the old pre-flight backend verification did.
  */
 private reportMissingLibrariesFromError(error: unknown): void {
  const err = error as { code?: string; missing_libraries?: unknown }
  if (err?.code !== 'MISSING_SHARED_LIBRARY') return

  const libs = Array.isArray(err.missing_libraries)
   ? err.missing_libraries.filter(
    (lib): lib is string => typeof lib === 'string'
   )
   : []
  if (libs.length === 0) return

  // The engine is bundled, so there is no selected variant to name. The
  // unresolved library itself identifies the runtime that is missing, and
  // getBackendDisplayName matches on exactly these substrings.
  const joined = libs.join(' ').toLowerCase()
  const backend = ['cuda', 'vulkan', 'hip', 'metal'].find((b) =>
   joined.includes(b)
  )
  events.emit(AppEvent.onBackendVerificationFailed, {
   backend: backend ?? '',
   missingLibraries: libs,
  })
 }

 getEmbedderBootstrapError(): string | undefined {
  return this.embedderBootstrapError
 }

 /**
  * Proves the embedding model can actually produce a usable vector, rather
  * than inferring health from a completed download. Never throws: setup
  * reports the problem and lets the user continue.
  */
 async verifyEmbeddingModel(): Promise<EmbeddingModelReport> {
  // Downloading it is not a defect. Probing mid-download would fail on a
  // model that is simply not there yet and report a warning for it.
  if (this.embedderBootstrapping) {
   return { status: 'ok', pending: true }
  }

  let modelId: string | undefined
  try {
   const sInfo = await this.ensureEmbeddingModelLoaded()
   modelId = sInfo.model_id
   const response = await this.embed([EMBEDDING_PROBE_TEXT])
   const check = evaluateEmbeddingVector(response?.data?.[0]?.embedding)
   return {
    status: check.ok ? 'ok' : 'warning',
    modelId,
    dimension: check.dimension,
    problem: check.problem,
   }
  } catch (e) {
   // A failed startup install is the more specific cause, and it is the one
   // the user can act on.
   return {
    status: 'warning',
    modelId,
    error:
     this.getEmbedderBootstrapError() ??
     (e instanceof Error ? e.message : String(e)),
   }
  }
 }

 /**
  * Detects a GPU backend that runs entirely on the CPU. Such a router starts
  * cleanly and reports healthy, so this comparison against the engine's own
  * device list is the only signal that offload never happened.
  */
 async verifyGpuOffload(): Promise<GpuOffloadReport> {
  let devices: DeviceList[]
  try {
   devices = await this.getDevices()
  } catch (e) {
   // Without a device list there is no basis for a reason code, and guessing
   // one would point the user at the wrong fix.
   return {
    status: 'warning',
    backend: '',
    gpuExpected: false,
    engineDeviceCount: 0,
    error: e instanceof Error ? e.message : String(e),
   }
  }

  let hardwareGpuCount = 0
  try {
   hardwareGpuCount = (await getSystemInfo())?.gpus?.length ?? 0
  } catch (e) {
   logger.warn('Hardware GPU probe failed during setup verification:', e)
  }

  const deviceIds = devices.map((d) => d.id)
  return {
   backend: backendFromDeviceIds(deviceIds),
   ...evaluateGpuOffload({
    engineDeviceCount: countAcceleratorDevices(deviceIds),
    hardwareGpuCount,
   }),
  }
 }

 override async getSettings(): Promise<SettingComponentProps[]> {
  return readSettingsFile()
 }

 override async updateSettings(
  componentProps: Partial<SettingComponentProps>[]
 ): Promise<void> {
  const current = await readSettingsFile()
  const changed: { key: string; value: unknown }[] = []
  const updated = current.length
   ? current.map((s) => {
    const patch = componentProps.find((p) => p.key === s.key)
    if (patch?.controllerProps) {
     const nextValue = (patch.controllerProps as { value?: unknown })
      .value
     const prevValue = (s.controllerProps as { value?: unknown }).value
     if (nextValue !== prevValue) {
      changed.push({ key: s.key, value: nextValue })
     }
     return {
      ...s,
      controllerProps: { ...s.controllerProps, value: nextValue },
     } as SettingComponentProps
    }
    return s
   })
   : ((): SettingComponentProps[] => {
    const arr = componentProps as SettingComponentProps[]
    for (const s of arr) {
     changed.push({
      key: s.key,
      value: (s.controllerProps as { value?: unknown })?.value,
     })
    }
    return arr
   })()
  await writeSettingsFile(updated)
  const previousConfig = { ...this.config }
  const previousTimeout = this.timeout
  const previousEnv = this.llamacpp_env
  for (const { key, value } of changed) {
   this.onSettingUpdate(key, value)
  }
  if (this.presetRefreshTimer) {
   clearTimeout(this.presetRefreshTimer)
   this.presetRefreshTimer = null
   // Startup migrations are included in the initial preset. Once loaded,
   // an awaited settings save must include engine application, not merely
   // schedule it after the caller has already sent its next request.
   if (this.backgroundInit) {
    try {
     await this.refreshEnginePreset()
    } catch (error) {
     this.config = previousConfig
     this.timeout = previousTimeout
     this.llamacpp_env = previousEnv
     await writeSettingsFile(current)
     throw error
    }
   }
  }
 }

 private async registerSettings(
  settings: SettingComponentProps[]
 ): Promise<void> {
  settings.forEach((s) => {
   s.extensionName = PACKAGE_NAME
  })
  const old = await readSettingsFile()
  if (old.length) {
   // Before the merge below: the stored mlock / no_mmap checkboxes have no
   // counterpart in `settings` any more, so the merge would silently drop them.
   applyLegacyLoadMode(settings, old)
   settings.forEach((s) => {
    const prev = old.find((o) => o.key === s.key)
    if (!prev) return
    const cp = s.controllerProps as Record<string, unknown>
    const pcp = prev.controllerProps as Record<string, unknown>
    if (pcp.value !== undefined) cp.value = pcp.value
    if ('options' in cp) {
     const newOptions = (cp.options as unknown[]) ?? []
     if (newOptions.length === 0 && Array.isArray(pcp.options)) {
      cp.options = pcp.options
     }
     const opts = (cp.options as { value: unknown }[]) ?? []
     if (opts.length && !opts.some((o) => o.value === cp.value)) {
      cp.value = opts[0]?.value
     }
    }
   })
  }
  await writeSettingsFile(settings)
 }

 // The ONLY sanctioned localStorage use in this extension: a one-time read +
 // clear migrating pre-backend llamacpp settings into the file store. All
 // other persistence goes through backend-settings.ts. Guarded by
 // no-localstorage.test.ts via the `localstorage-migration-allowed` markers.
 private async migrateLocalStorageToFile(): Promise<void> {
  if (await settingsFileExists()) return
  let raw: string | null = null
  try {
   raw = localStorage.getItem(PACKAGE_NAME) // localstorage-migration-allowed
  } catch {
   raw = null
  }
  if (!raw) return
  try {
   const parsed = JSON.parse(raw)
   if (Array.isArray(parsed)) {
    await writeSettingsFile(parsed as SettingComponentProps[])
    logger.info(
     `Migrated llamacpp settings from localStorage → file (${parsed.length} entries)`
    )
   }
  } catch (e) {
   logger.warn('Failed to migrate localStorage settings to file:', e)
   return
  }
  try {
   localStorage.removeItem(PACKAGE_NAME) // localstorage-migration-allowed
  } catch (e) {
   logger.warn('Failed to clear migrated localStorage entry:', e)
  }
 }

 /**
  * Serialized: two concurrent calls would each run the stop-then-spawn
  * sequence, and the second would kill the worker the first had just brought
  * up. A late caller gets the in-flight start rather than a restart.
  */
 private async startEngine(): Promise<void> {
  const inflight = this.engineStartLock
  if (inflight) {
   logger.info('startEngine already in progress; awaiting the in-flight run')
   await inflight.catch(() => undefined)
   return
  }
  this.engineStartLock = this.runStartEngine().finally(() => {
   this.engineStartLock = null
  })
  return this.engineStartLock
 }

 private async runStartEngine(): Promise<void> {
  const providerPath = await this.getProviderPath()
  const janDataFolderPath = await getJanDataFolderPath()
  const { path: presetPath, embeddingCount } = await generatePreset(
   providerPath,
   janDataFolderPath,
   this.config
  )

  const modelsMax = this.resolveModelsMax(embeddingCount)

  // Idempotent in the plugin: a second start returns the running worker
  // rather than orphaning it, so a redundant call is a no-op instead of a
  // cold restart. That is also why there is no adoption dance here: the
  // worker exits on stdin EOF, which the OS delivers when this process dies
  // by any means -- including SIGKILL, where kill_on_drop never runs -- so
  // there is nothing to adopt or reap. Not because a child dies with its
  // parent; it does not, it gets reparented.
  const envs: Record<string, string> = {}
  envs['LLAMA_ARG_TIMEOUT'] = String(this.timeout)
  if (this.llamacpp_env) this.parseEnvFromString(envs, this.llamacpp_env)

  const info = await startEngine(
   presetPath,
   modelsMax,
   this.resolveThreadCacheBudget(),
   envs
  )
  this.enginePort = info.port
  this.engineApiKey = info.api_key
  this.presetPath = presetPath
  // The load log never says which GPU llama.cpp picked, so record what the
  // worker initialised and which devices the user's setting names.
  logger.info(
   `Engine devices at start: ${info.devices ? this.deviceKeys(info.devices).join(', ') : 'not reported'}; device setting: ${String(this.config.device ?? '') || '(auto)'}`
  )
  logger.info(
   `Engine worker started on port ${info.port} (pid ${info.pid}, models_max=${modelsMax}, ${info.models.length} models registered, preset=${presetPath})`
  )
 }

 /**
  * Drops a thread's saved prompt cache, for a thread the user deleted.
  *
  * Best-effort by design: the cache is invisible to the user and the store
  * prunes by size anyway, so failing here must not make deleting a thread
  * fail. Returns how many states were dropped.
  */
 async forgetThreadCache(threadId: string): Promise<number> {
  try {
   return await eraseThreadSlotState({
    threadId,
    cacheDir: threadCacheDir(await this.getProviderPath()),
   })
  } catch (e) {
   logger.warn(`could not drop the saved cache for thread ${threadId}: ${e}`)
   return 0
  }
 }

 /**
  * The disk budget for saved thread caches, in MiB.
  *
  * 0 is the off switch on the worker side, so the toggle and the size collapse
  * into one number: a disabled feature and a zero budget are the same thing,
  * and keeping them separate would let the worker create a directory it would
  * never write to.
  */
 private resolveThreadCacheBudget(): number {
  if (this.config.persist_thread_cache === false) return 0
  const raw = Number(this.config.thread_cache_size)
  if (!Number.isFinite(raw) || raw <= 0) return 0
  return Math.floor(raw)
 }

 /**
  * `models_max` including the embedding slot bonus.
  *
  * Reserves one extra slot when any embedder is installed so loading it does
  * not evict the user's chat model. Only one embedding model is ever resident
  * (RAG calls load() once per request), so the bonus is +1 regardless of how
  * many are installed. 0 (unlimited) stays unlimited.
  */
 private resolveModelsMax(embeddingCount: number): number {
  const raw = (this.config as { models_max?: unknown }).models_max
  let modelsMax = 1
  if (typeof raw === 'number') modelsMax = raw
  else if (typeof raw === 'string' && raw.trim().length > 0) {
   const n = parseInt(raw, 10)
   if (!Number.isNaN(n) && n >= 0) modelsMax = n
  }
  this.userModelsMax = modelsMax
  const bonus = modelsMax > 0 && embeddingCount > 0 ? 1 : 0
  this.engineEmbeddingBonus = bonus
  return modelsMax + bonus
 }

 /**
  * Apply a preset change (model added/removed/renamed or a per-model setting)
  * to the running worker. Models whose preset section is unchanged stay
  * loaded, so an embedder import or a settings write no longer cold-reloads
  * the model the user is talking to.
  *
  * Unlike the router this also resizes `models_max`, so a change in the
  * embedding slot bonus no longer needs a restart either -- which was the one
  * remaining case that evicted a live chat model.
  */
 private async refreshEnginePreset(): Promise<void> {
  if (!(await this.getEngineInfo())) {
   await this.startEngine() // cold start
   return
  }

  const providerPath = await this.getProviderPath()
  const janDataFolderPath = await getJanDataFolderPath()
  const { path: presetPath, embeddingCount } = await generatePreset(
   providerPath,
   janDataFolderPath,
   this.config
  )
  this.presetPath = presetPath

  try {
   const report = await reloadEngineModels(
    presetPath,
    this.resolveModelsMax(embeddingCount),
    this.resolveThreadCacheBudget()
   )
   logger.info(
    `Engine preset reloaded: +${report.added.length} ~${report.changed.length} -${report.removed.length}, ${report.kept.length} kept loaded`
   )
  } catch (e) {
   logger.warn('Live engine reload failed; falling back to restart:', e)
   await this.startEngine()
  }
 }

 /**
  * Public accessor for downstream consumers. Returns `null` if the engine
  * worker hasn't started successfully yet.
  */
 async getEngineInfo(): Promise<{ port: number; apiKey: string } | null> {
  // Deliberately not answered from the cached port: get_engine_info is the
  // one place a worker that died is noticed -- it reaps the handle and
  // reports nothing running -- and every caller here uses this as the
  // liveness probe before deciding whether to respawn. Answering from
  // enginePort would keep handing out a closed port for the rest of the
  // session, which is exactly the crash the worker process exists to survive.
  try {
   const info = await pluginGetEngineInfo()
   if (info) {
    this.enginePort = info.port
    this.engineApiKey = info.api_key
    return { port: info.port, apiKey: info.api_key }
   }
   this.enginePort = undefined
   this.engineApiKey = undefined
  } catch (e) {
   logger.warn('get_engine_info failed:', e)
  }
  return null
 }

 /**
  * The bundled engine's identity. Cached after the first call: these are
  * compile-time constants of the plugin, so they cannot change while the app
  * is running, and the settings screen asks on every mount.
  */
 async getEngineVersion(): Promise<EngineVersion> {
  if (!this.engineVersion) {
   this.engineVersion = await pluginGetEngineVersion()
  }
  return this.engineVersion
 }

 /**
  * Fetch runtime properties for a loaded model from the router's
  * `/props?model=<id>` endpoint. Returns `undefined` if the router isn't
  * running, the model isn't loaded, or the response is unusable. `nCtx` is
  * the post-fit value — what `fit_ctx` settled on — so it's the right
  * denominator for the token-usage popup.
  */
 async getModelProps(
  modelId: string
 ): Promise<
  | (ModelProps & {
   modalities?: { vision: boolean; video: boolean; audio: boolean }
  })
  | undefined
 > {
  const engine = await this.getEngineInfo()
  if (!engine || !modelId) return undefined
  // A request naming an unloaded model loads it on demand, so `/props` alone
  // would trigger a load. Gate on the loaded-set first.
  try {
   const loaded = await this.getLoadedModels()
   if (!loaded.includes(modelId)) return undefined
  } catch {
   return undefined
  }
  try {
   const url = `http://127.0.0.1:${engine.port}/props?model=${encodeURIComponent(modelId)}`
   const res = await this.loopbackFetch(url, {
    headers: { Authorization: `Bearer ${engine.apiKey}` },
   })
   if (!res.ok) return undefined
   const json = (await res.json()) as {
    default_generation_settings?: { n_ctx?: number }
    total_slots?: number
    model_alias?: string
    is_sleeping?: boolean
    modalities?: { vision?: boolean; video?: boolean; audio?: boolean }
   }
   const n = json?.default_generation_settings?.n_ctx
   if (typeof n !== 'number' || n <= 0) return undefined
   const m = json.modalities
   return {
    nCtx: n,
    totalSlots:
     typeof json.total_slots === 'number' ? json.total_slots : undefined,
    modelAlias: json.model_alias,
    isSleeping: !!json.is_sleeping,
    modalities: m
     ? { vision: !!m.vision, video: !!m.video, audio: !!m.audio }
     : undefined,
   }
  } catch {
   return undefined
  }
 }

 private async readMmprojCapabilities(
  mmprojPath: string
 ): Promise<{ vision: boolean; audio: boolean }> {
  try {
   const janDataFolderPath = await getJanDataFolderPath()
   const fullPath = await joinPath([janDataFolderPath, mmprojPath])
   const meta = (await readGgufMetadata(fullPath)).metadata ?? {}
   const truthy = (v: string | undefined) =>
    typeof v === 'string' && v.toLowerCase() === 'true'
   const vision = truthy(meta['clip.has_vision_encoder'])
   const audio = truthy(meta['clip.has_audio_encoder'])
   if (!vision && !audio) return { vision: true, audio: false }
   return { vision, audio }
  } catch (error) {
   logger.warn('Failed to read mmproj capabilities:', error)
   return { vision: true, audio: false }
  }
 }

 private async migrateAutoUnloadToModelsMax(): Promise<void> {
  const MIGRATION_KEY = 'llamacpp_models_max_migrated_v1'
  if (await getBackendSetting(MIGRATION_KEY)) return

  try {
   const old = await this.getSetting<boolean | undefined>(
    'auto_unload',
    undefined
   )
   if (old !== undefined) {
    const targetValue = old ? '1' : '0'
    const settings = await this.getSettings()
    await this.updateSettings(
     settings.map((item) => {
      if (item.key === 'models_max') {
       item.controllerProps.value = targetValue
      }
      return item
     })
    )
     ; (this.config as any).models_max = targetValue
    logger.info(
     `Migrated auto_unload=${old} -> models_max=${targetValue}`
    )
   }
  } catch (e) {
   logger.warn('migrateAutoUnloadToModelsMax failed:', e)
   return
  }

  await setBackendSetting(MIGRATION_KEY, '1')
 }

 // The old default was 1 plus two slots Jan reserved for background work and
 // Cowork; both now share slot 0, so the reservation is gone and a persisted
 // 1 would pin the engine to a single slot rather than letting it resolve its
 // own count. Only the old default value is moved.
 private async migrateParallelToDefault(): Promise<void> {
  const MIGRATION_KEY = 'llamacpp_parallel_default_v1'
  if (await getBackendSetting(MIGRATION_KEY)) return

  try {
   if (asI32(this.config.parallel, 0) === 1) {
    const settings = await this.getSettings()
    await this.updateSettings(
     settings.map((item) => {
      if (item.key === 'parallel') {
       item.controllerProps.value = 0
      }
      return item
     })
    )
    this.config.parallel = 0
    logger.info('Migrated parallel: 1 -> 0 (llama.cpp default)')
   }
  } catch (e) {
   logger.warn('migrateParallelToDefault failed:', e)
   return
  }

  await setBackendSetting(MIGRATION_KEY, '1')
 }

 private async migratePersistedModelSettingsToYaml(): Promise<void> {
  if (await getBackendSetting(LLAMACPP_MODEL_SETTINGS_BACKFILL_KEY)) return

  const persistedModels = await readPersistedLlamacppModels()
  const providerPath = await this.getProviderPath()

  if (persistedModels.length === 0) {
   await setBackendSetting(LLAMACPP_MODEL_SETTINGS_BACKFILL_KEY, '1')
   return
  }

  for (const persistedModel of persistedModels) {
   const modelId =
    typeof persistedModel.id === 'string' ? persistedModel.id : undefined
   if (!modelId || !persistedModel.settings) continue

   const configPath = await joinPath([
    providerPath,
    'models',
    modelId,
    'model.yml',
   ])
   if (!(await fs.existsSync(configPath))) continue

   const cfg = (await fs.readYaml<ModelConfig>(configPath)) as ModelConfig &
    Record<string, unknown>

   let touched = false

   // Undo v1's n_gpu_layers stamp so upstream's auto offload applies again.
   if (cfg.n_gpu_layers === LEGACY_NGL_DEFAULT) {
    delete (cfg as Record<string, unknown>).n_gpu_layers
    touched = true
    logger.info(
     `Removed legacy n_gpu_layers=${LEGACY_NGL_DEFAULT} from ${modelId}; GPU offload is auto again`
    )
   }

   for (const [sidebarKey, persistedSetting] of Object.entries(
    persistedModel.settings
   )) {
    const mapping = MODEL_SETTINGS_YAML_MAPPING[sidebarKey]
    if (!mapping) continue

    if (mapping.yamlKey in cfg) continue

    const value = persistedSetting?.controller_props?.value
    // Never re-copy the legacy default; that is what v1 did.
    if (sidebarKey === 'ngl' && Number(value) === LEGACY_NGL_DEFAULT) {
     continue
    }

    const next = mapping.coerce(value)
    if (next === null) continue

     ; (cfg as Record<string, unknown>)[mapping.yamlKey] = next
    touched = true
   }

   if (touched) {
    await fs.writeYaml(configPath, cfg)
   }
  }

  await setBackendSetting(LLAMACPP_MODEL_SETTINGS_BACKFILL_KEY, '1')
 }

 /**
  * One-shot: removes v1's `n_gpu_layers: 100` stamp from every model.yml. It
  * lives in the yaml, not in the persisted settings, so it is swept even when
  * nothing is persisted.
  */
 private async migrateLegacyNgl(): Promise<void> {
  if (await getBackendSetting(LEGACY_NGL_SWEPT_KEY)) return
  await this.stripModelYamlValue(
   await this.getProviderPath(),
   'n_gpu_layers',
   LEGACY_NGL_DEFAULT
  )
  await setBackendSetting(LEGACY_NGL_SWEPT_KEY, '1')
 }

 /**
  * One-shot: drops the `ctx_size: 8192` the web-app seeded into every model
  * (and the settings backfill then copied into model.yml), so auto-fit may
  * shrink the context down to `fit_ctx`; upstream's fit leaves a set context
  * alone. The seed has no provenance, so 8192 is taken to be it. Nothing
  * changes with fit off: preset.ts's `[*] ctx-size` is 8192 too.
  */
 private async migrateSeededCtxSize(): Promise<void> {
  if (await getBackendSetting(SEEDED_CTX_CLEARED_KEY)) return
  await this.stripModelYamlValue(
   await this.getProviderPath(),
   'ctx_size',
   SEEDED_CTX_SIZE
  )
  await setBackendSetting(SEEDED_CTX_CLEARED_KEY, '1')
 }

 /**
  * Removes `key` from every model.yml where it equals `value`.
  *
  * Walks the models directory rather than the persisted settings because a
  * stamped value is in the yaml even for models that no longer appear in the
  * persisted provider settings, so iterating the persisted list would miss them.
  */
 private async stripModelYamlValue(
  providerPath: string,
  key: string,
  value: number
 ): Promise<void> {
  const modelsDir = await joinPath([providerPath, 'models'])
  if (!(await fs.existsSync(modelsDir))) return

  for (const { configPath } of await findModelEntries(modelsDir)) {
   try {
    // Read as a bag rather than ModelConfig: that type covers the
    // plugin's own fields, not every per-model key the sidebar writes.
    const cfg = await fs.readYaml<Record<string, unknown>>(configPath)
    if (cfg?.[key] !== value) continue
    delete cfg[key]
    await fs.writeYaml(configPath, cfg)
    logger.info(`Removed ${key}=${value} from ${configPath}`)
   } catch (e) {
    // Per-model isolation: one unreadable yaml must not block the sweep.
    logger.warn(`${key} sweep skipped ${configPath}:`, e)
   }
  }
 }

 async getProviderPath(): Promise<string> {
  if (!this.providerPath) {
   this.providerPath = await joinPath([
    await getJanDataFolderPath(),
    this.providerId,
   ])
  }
  return this.providerPath
 }

 dispose(): void {
  // Deliberately does NOT stop the engine worker. It outlives any single
  // extension instance: the app owns its lifetime and stops it on
  // ExitRequested/Exit. An extension teardown is not an app exit -- React
  // StrictMode and HMR both deactivate and immediately reactivate us, and
  // awaiting backgroundInit here (as this used to) meant blocking until the
  // worker was up, then killing exactly the process it had just started.
  this.enginePort = undefined
  this.engineApiKey = undefined
 }

 onSettingUpdate<T>(key: string, value: T): void {
  this.config[key] = value

  if (key === 'llamacpp_env') {
   this.llamacpp_env = value as string
  } else if (key === 'timeout') {
   // Clamped through the plugin's own coercion: the field is a text input, so
   // clearing it yields '' and a bare cast produced NaN. 0 or negative would
   // make the request timeout fire immediately and abort every stream.
   this.timeout = asI32(value, DEFAULT_TIMEOUT) || DEFAULT_TIMEOUT
  } else if (key === 'models_max') {
   // Not a `[*]` preset key, so it is deliberately absent from
   // PRESET_AFFECTING_KEYS -- but the worker still has to be told, or
   // "Max Concurrently Loaded Models" only ever updated this.config.
   this.schedulePresetRefresh()
  } else if (PRESET_AFFECTING_KEYS.has(key)) {
   // The running worker was started with the previous preset; without a
   // reload the new value is invisible to inference. Debounced so a flurry
   // of slider/dropdown updates collapses into one bounce.
   this.schedulePresetRefresh()
  }
 }

 private presetRefreshTimer: ReturnType<typeof setTimeout> | null = null
 private schedulePresetRefresh(): void {
  if (this.presetRefreshTimer) clearTimeout(this.presetRefreshTimer)
  this.presetRefreshTimer = setTimeout(() => {
   this.presetRefreshTimer = null
   this.refreshEnginePreset().catch((e) =>
    logger.warn('Preset refresh after settings update failed:', e)
   )
  }, 600)
 }

 private async generateApiKey(modelId: string, port: string): Promise<string> {
  const hash = await pluginGenerateApiKey(modelId + port, this.apiSecret)
  return hash
 }

 override async get(modelId: string): Promise<modelInfo | undefined> {
  const modelPath = await joinPath([
   await this.getProviderPath(),
   'models',
   modelId,
  ])
  const path = await joinPath([modelPath, 'model.yml'])

  if (!(await fs.existsSync(path))) return undefined

  const modelConfig = await fs.readYaml<ModelConfig>(path)

  const isEmbedding = await this.resolveEmbeddingConfig(modelId, modelConfig)
  await this.resolveMtpLayersConfig(modelId, modelConfig)
  await this.resolveExpertCountConfig(modelId, modelConfig)
  const templateKwargs = await this.resolveTemplateKwargsConfig(
   modelId,
   modelConfig
  )

  return {
   id: modelId,
   name: modelConfig.name ?? modelId,
   quant_type: undefined, // TODO: parse quantization type from model.yml or model.gguf
   providerId: this.provider,
   port: 0, // port is not known until the model is loaded
   sizeBytes: modelConfig.size_bytes ?? 0,
   embedding: isEmbedding,
   template_kwargs: templateKwargs,
  } as modelInfo
 }

 async getModelContextLimit(modelId: string): Promise<number | undefined> {
  const config = await fs.readYaml<ModelConfig>(
   await joinPath([await this.getProviderPath(), 'models', modelId, 'model.yml'])
  )
  const path = await joinPath([await getJanDataFolderPath(), config.model_path])
  const { metadata } = await readGgufMetadata(path)
  const limit = Number(metadata?.[`${metadata?.['general.architecture']}.context_length`])
  return Number.isInteger(limit) && limit > 0 ? limit : undefined
 }

 /**
  * Checks if embedding status is known. If not, reads GGUF, detects it,
  * and updates the model.yml for future performance.
  */
 private async resolveEmbeddingConfig(
  modelId: string,
  modelConfig: ModelConfig
 ): Promise<boolean> {
  const cfg = modelConfig as ModelConfig & { embedding_check_v?: number }
  const hasFlag = typeof cfg.embedding === 'boolean'
  const upToDate = cfg.embedding_check_v === EMBEDDING_CHECK_VERSION
  if (hasFlag && upToDate) {
   return cfg.embedding as boolean
  }
  if (hasFlag && cfg.embedding === true) {
   return true
  }

  let isEmbedding = false
  try {
   const janDataFolderPath = await getJanDataFolderPath()
   const fullModelPath = await joinPath([
    janDataFolderPath,
    modelConfig.model_path,
   ])

   if (await fs.existsSync(fullModelPath)) {
    const metadata = await readGgufMetadata(fullModelPath)
    if (detectEmbeddingFromGgufMeta(metadata.metadata)) {
     isEmbedding = true
    }
   }
  } catch (e) {
   logger.warn(`Failed to check metadata for ${modelId}`, e)
   return cfg.embedding === true
  }

  try {
   const configPath = await joinPath([
    await this.getProviderPath(),
    'models',
    modelId,
    'model.yml',
   ])

   cfg.embedding = isEmbedding
   cfg.embedding_check_v = EMBEDDING_CHECK_VERSION
   if (isEmbedding) {
    const c = cfg as ModelConfig & {
     pooling?: string
     ubatch_size?: number
     batch_size?: number
    }
    if (!c.pooling) c.pooling = 'mean'
    if (!c.ubatch_size) c.ubatch_size = 2048
    if (!c.batch_size) c.batch_size = 2048
   }

   await fs.writeYaml(configPath, cfg)
  } catch (e) {
   logger.warn(`Failed to update config for ${modelId}`, e)
  }

  return isEmbedding
 }

 /**
  * Record the GGUF's MoE expert count in model.yml, which the preset reads to
  * decide whether `moe-cache-mib` may be emitted: llama.cpp refuses to load a
  * model without experts when it is set. Backfilled lazily, like mtp_layers,
  * for models imported before the count was recorded.
  */
 private async resolveExpertCountConfig(
  modelId: string,
  modelConfig: ModelConfig
 ): Promise<number> {
  const cfg = modelConfig as ModelConfig & {
   expert_count?: number
   expert_check_v?: number
  }
  if (
   typeof cfg.expert_count === 'number' &&
   cfg.expert_check_v === EXPERT_CHECK_VERSION
  ) {
   return cfg.expert_count
  }

  let expertCount = 0
  try {
   const janDataFolderPath = await getJanDataFolderPath()
   const fullModelPath = await joinPath([
    janDataFolderPath,
    modelConfig.model_path,
   ])
   if (await fs.existsSync(fullModelPath)) {
    const metadata = await readGgufMetadata(fullModelPath)
    expertCount = detectExpertCountFromGgufMeta(metadata.metadata)
   }
  } catch (e) {
   logger.warn(`Failed to check expert count for ${modelId}`, e)
   return cfg.expert_count ?? 0
  }

  try {
   const configPath = await joinPath([
    await this.getProviderPath(),
    'models',
    modelId,
    'model.yml',
   ])
   cfg.expert_count = expertCount
   cfg.expert_check_v = EXPERT_CHECK_VERSION
   await fs.writeYaml(configPath, cfg)
  } catch (e) {
   logger.warn(`Failed to persist expert count for ${modelId}`, e)
  }

  return expertCount
 }

 private async resolveMtpLayersConfig(
  modelId: string,
  modelConfig: ModelConfig
 ): Promise<number> {
  const cfg = modelConfig as ModelConfig & {
   mtp_layers?: number
   mtp_check_v?: number
  }
  if (
   typeof cfg.mtp_layers === 'number' &&
   cfg.mtp_check_v === MTP_CHECK_VERSION
  ) {
   return cfg.mtp_layers
  }

  let mtpLayers = 0
  try {
   const janDataFolderPath = await getJanDataFolderPath()
   const fullModelPath = await joinPath([
    janDataFolderPath,
    modelConfig.model_path,
   ])
   if (await fs.existsSync(fullModelPath)) {
    const metadata = await readGgufMetadata(fullModelPath)
    mtpLayers = detectMtpLayersFromGgufMeta(metadata.metadata)
   }
  } catch (e) {
   logger.warn(`Failed to check MTP metadata for ${modelId}`, e)
   return cfg.mtp_layers ?? 0
  }

  try {
   const configPath = await joinPath([
    await this.getProviderPath(),
    'models',
    modelId,
    'model.yml',
   ])
   cfg.mtp_layers = mtpLayers
   cfg.mtp_check_v = MTP_CHECK_VERSION
   await fs.writeYaml(configPath, cfg)
  } catch (e) {
   logger.warn(`Failed to persist MTP layers for ${modelId}`, e)
  }

  return mtpLayers
 }

 /**
  * Detect which chat-template kwargs (e.g. `preserve_thinking`) a model's
  * embedded jinja template accepts, caching the result in model.yml. Migrates
  * pre-existing models lazily the first time list() sees a stale check version.
  */
 private async resolveTemplateKwargsConfig(
  modelId: string,
  modelConfig: ModelConfig
 ): Promise<TemplateKwarg[]> {
  const cfg = modelConfig as ModelConfig & {
   template_kwargs?: TemplateKwarg[]
   template_kwargs_check_v?: number
  }
  if (
   Array.isArray(cfg.template_kwargs) &&
   cfg.template_kwargs_check_v === TEMPLATE_KWARGS_CHECK_VERSION
  ) {
   return cfg.template_kwargs
  }

  let kwargs: TemplateKwarg[] = []
  try {
   const janDataFolderPath = await getJanDataFolderPath()
   const fullModelPath = await joinPath([
    janDataFolderPath,
    modelConfig.model_path,
   ])
   if (await fs.existsSync(fullModelPath)) {
    const metadata = await readGgufMetadata(fullModelPath)
    kwargs = detectTemplateKwargsFromChatTemplate(
     metadata.metadata?.['tokenizer.chat_template']
    )
   }
  } catch (e) {
   logger.warn(`Failed to check template kwargs for ${modelId}`, e)
   return cfg.template_kwargs ?? []
  }

  try {
   const configPath = await joinPath([
    await this.getProviderPath(),
    'models',
    modelId,
    'model.yml',
   ])
   cfg.template_kwargs = kwargs
   cfg.template_kwargs_check_v = TEMPLATE_KWARGS_CHECK_VERSION
   await fs.writeYaml(configPath, cfg)
  } catch (e) {
   logger.warn(`Failed to persist template kwargs for ${modelId}`, e)
  }

  return kwargs
 }

 // Implement the required LocalProvider interface methods
 override async list(): Promise<modelInfo[]> {
  const modelsDir = await joinPath([await this.getProviderPath(), 'models'])
  if (!(await fs.existsSync(modelsDir))) {
   await fs.mkdir(modelsDir)
  }

  // Legacy migration is best-effort: a failure here must never blank the
  // model list. Pre-router users without anything to migrate would never
  // notice, but a single throw used to bubble out of list() and return [].
  try {
   await this.migrateLegacyModels()
  } catch (err) {
   logger.warn(`list: migrateLegacyModels failed, continuing: ${String(err)}`)
  }

  const modelIds = (await findModelEntries(modelsDir)).map((e) => e.modelId)

  // Per-model isolation: one malformed model.yml (or a write_yaml failure
  // inside resolveEmbeddingConfig) must not discard the other entries.
  let modelInfos: modelInfo[] = []
  for (const modelId of modelIds) {
   try {
    const path = await joinPath([modelsDir, modelId, 'model.yml'])
    const modelConfig = await fs.readYaml<ModelConfig>(path)
    const isEmbedding = await this.resolveEmbeddingConfig(
     modelId,
     modelConfig
    )
    const templateKwargs = await this.resolveTemplateKwargsConfig(
     modelId,
     modelConfig
    )

    const capabilities: string[] = []
    if (modelConfig.mmproj_path) {
     const caps = await this.readMmprojCapabilities(
      modelConfig.mmproj_path
     )
     if (caps.vision) capabilities.push('vision')
     if (caps.audio) capabilities.push('audio')
     // 'video' is intentionally NOT derived from the mmproj here — video
     // support also depends on the backend being built with MTMD_VIDEO,
     // which the GGUF can't reveal. It's reconciled from /props after the
     // model loads (see useReconcileVideoCapability).
    }

    const mp = modelConfig.model_path ?? ''
    const isAbsolute = mp.startsWith('/') || /^[A-Za-z]:[\\/]/.test(mp)

    const modelInfo = {
     id: modelId,
     name: modelConfig.name ?? modelId,
     quant_type: undefined, // TODO: parse quantization type from model.yml or model.gguf
     providerId: this.provider,
     port: 0, // port is not known until the model is loaded
     sizeBytes: modelConfig.size_bytes ?? 0,
     embedding: isEmbedding,
     imported: isAbsolute,
     capabilities: capabilities.length > 0 ? capabilities : undefined,
     template_kwargs: templateKwargs,
    } as modelInfo
    modelInfos.push(modelInfo)
   } catch (err) {
    logger.warn(`list: skipping model ${modelId}: ${String(err)}`)
   }
  }

  return modelInfos
 }

 private async migrateLegacyModels() {
  // Attempt to migrate only once
  if ((await getBackendSetting('cortex_models_migrated')) === 'true') return

  const janDataFolderPath = await getJanDataFolderPath()
  const modelsDir = await joinPath([janDataFolderPath, 'models'])
  if (!(await fs.existsSync(modelsDir))) return

  // DFS
  let stack = [modelsDir]
  while (stack.length > 0) {
   const currentDir = stack.pop()

   const files = await fs.readdirSync(currentDir)
   for (const child of files) {
    try {
     const childPath = await joinPath([currentDir, child])
     const stat = await fs.fileStat(childPath)
     if (
      files.some((e) => e.endsWith('model.yml')) &&
      !child.endsWith('model.yml')
     )
      continue
     if (!stat.isDirectory && child.endsWith('.yml')) {
      // check if model.yml exists
      const modelConfigPath = child
      if (await fs.existsSync(modelConfigPath)) {
       const legacyModelConfig = await fs.readYaml<{
        files: string[]
        model: string
       }>(modelConfigPath)
       const legacyModelPath = legacyModelConfig.files?.[0]
       if (!legacyModelPath) continue
       // +1 to remove the leading slash
       // NOTE: this does not handle Windows path \\
       let modelId = currentDir.slice(modelsDir.length + 1)

       modelId =
        modelId !== 'imported'
         ? modelId.replace(/^(cortex\.so|huggingface\.co)[\/\\]/, '')
         : fileName(child).replace('.yml', '')

       const modelName = legacyModelConfig.model ?? modelId
       const configPath = await joinPath([
        await this.getProviderPath(),
        'models',
        modelId,
        'model.yml',
       ])
       if (await fs.existsSync(configPath)) continue // Don't reimport

       // this is relative to Jan's data folder
       const modelDir = `${this.providerId}/models/${modelId}`

       let size_bytes = (
        await fs.fileStat(
         await joinPath([janDataFolderPath, legacyModelPath])
        )
       ).size

       const modelConfig = {
        model_path: legacyModelPath,
        mmproj_path: undefined, // legacy models do not have mmproj
        name: modelName,
        size_bytes,
       } as ModelConfig
       await fs.mkdir(await joinPath([janDataFolderPath, modelDir]))
       await fs.writeYaml(configPath, modelConfig)
       continue
      }
     }
    } catch (error) {
     logger.error(`Error migrating model ${child}:`, error)
    }
   }

   let subdirs: string[] = []
   try {
    subdirs = await fs.readdirSync(currentDir)
   } catch (err) {
    logger.warn(
     `migrateLegacyModels: readdir failed for ${currentDir}: ${String(err)}`
    )
    continue
   }
   for (const child of subdirs) {
    try {
     const dirInfo = await fs.fileStat(child)
     if (!dirInfo?.isDirectory) continue
     stack.push(child)
    } catch (err) {
     logger.warn(
      `migrateLegacyModels: stat failed for ${child}: ${String(err)}`
     )
    }
   }
  }
  await setBackendSetting('cortex_models_migrated', 'true')
 }

 /**
  * Update a model with new information.
  * @param modelId
  * @param model
  */
 async update(modelId: string, model: Partial<modelInfo>): Promise<void> {
  const modelFolderPath = await joinPath([
   await this.getProviderPath(),
   'models',
   modelId,
  ])
  const modelConfig = await fs.readYaml<ModelConfig>(
   await joinPath([modelFolderPath, 'model.yml'])
  )
  const newFolderPath = await joinPath([
   await this.getProviderPath(),
   'models',
   model.id,
  ])
  // Check if newFolderPath exists
  if (await fs.existsSync(newFolderPath)) {
   throw new Error(`Model with ID ${model.id} already exists`)
  }
  const newModelConfigPath = await joinPath([newFolderPath, 'model.yml'])
  await fs.mv(modelFolderPath, newFolderPath).then(() =>
   // now replace what values have previous model name with format
   fs.writeYaml(newModelConfigPath, {
    ...modelConfig,
    model_path: modelConfig?.model_path?.replace(
     `${this.providerId}/models/${modelId}`,
     `${this.providerId}/models/${model.id}`
    ),
    mmproj_path: modelConfig?.mmproj_path?.replace(
     `${this.providerId}/models/${modelId}`,
     `${this.providerId}/models/${model.id}`
    ),
   })
  )

  // The router's preset still references the old model id until we
  // regenerate; without this a `POST /models/load <new-id>` would 404.
  try {
   await this.refreshEnginePreset()
  } catch (e) {
   logger.warn(`Router restart after model rename (${modelId} → ${model.id}) failed`, e)
  }
 }

 /**
  * Joins concurrent imports of the same model instead of starting a second one.
  * Two callers racing (startup embedder bootstrap and an on-demand embed) both
  * passed the `model.yml` existence guard, then registered the same download
  * task id -- which Rust resolves by cancelling the first and deleting its
  * partial file. The loser then returned without writing `model.yml`, so its
  * caller went on to load a model that was never installed.
  */
 override async import(modelId: string, opts: ImportOptions): Promise<void> {
  const inFlight = this.pendingImports.get(modelId)
  if (inFlight) {
   logger.info(`Joining in-flight import of "${modelId}"`)
   return inFlight
  }

  const task = this.runImport(modelId, opts).finally(() => {
   this.pendingImports.delete(modelId)
  })
  this.pendingImports.set(modelId, task)
  return task
 }

 private async runImport(modelId: string, opts: ImportOptions): Promise<void> {
  const isValidModelId = (id: string) => {
   // only allow alphanumeric, underscore, hyphen, and dot characters in modelId
   if (!/^[a-zA-Z0-9/_\-\.]+$/.test(id)) return false

   // check for empty parts or path traversal
   const parts = id.split('/')
   return parts.every((s) => s !== '' && s !== '.' && s !== '..')
  }

  if (!isValidModelId(modelId))
   throw new Error(
    `Invalid modelId: ${modelId}. Only alphanumeric and / _ - . characters are allowed.`
   )

  const configPath = await joinPath([
   await this.getProviderPath(),
   'models',
   modelId,
   'model.yml',
  ])
  if (await fs.existsSync(configPath))
   throw new Error(`Model ${modelId} already exists`)

  // this is relative to Jan's data folder
  const modelDir = `${this.providerId}/models/${modelId}`

  // we only use these from opts
  // opts.modelPath: URL to the model file
  // opts.mmprojPath: URL to the mmproj file

  let downloadItems: DownloadItem[] = []

  const maybeDownload = async (path: string, saveName: string) => {
   // if URL, add to downloadItems, and return local path
   if (path.startsWith('https://')) {
    const localPath = `${modelDir}/${saveName}`
    downloadItems.push({
     url: path,
     save_path: localPath,
     proxy: await getProxyConfig(),
     sha256:
      saveName === 'model.gguf'
       ? opts.modelSha256
       : saveName === 'mmproj.gguf'
        ? opts.mmprojSha256
        : undefined,
     size:
      saveName === 'model.gguf'
       ? opts.modelSize
       : saveName === 'mmproj.gguf'
        ? opts.mmprojSize
        : undefined,
     model_id: modelId,
    })
    return localPath
   }

   // if local file (absolute path), check if it exists
   // and return the path
   if (!(await fs.existsSync(path)))
    throw new Error(`File not found: ${path}`)
   return path
  }

  let modelPath = await maybeDownload(opts.modelPath, 'model.gguf')
  let mmprojPath = opts.mmprojPath
   ? await maybeDownload(opts.mmprojPath, 'mmproj.gguf')
   : undefined
  // Speculative-decoding draft companion; paired with the main model. The
  // file name stays `mtp.gguf` for every flavour: it is the local name of
  // the paired draft, and changing it would orphan existing installs.
  let draftModelPath = opts.specDraftPath
   ? await maybeDownload(opts.specDraftPath, 'mtp.gguf')
   : undefined

  if (downloadItems.length > 0) {
   try {
    // emit download update event on progress
    const onProgress = (transferred: number, total: number) => {
     events.emit(DownloadEvent.onFileDownloadUpdate, {
      modelId,
      percent: transferred / total,
      size: { transferred, total },
      downloadType: 'Model',
     })
    }
    await this.downloads().downloadFiles(
     downloadItems,
     this.createDownloadTaskId(modelId),
     onProgress
    )
   } catch (error) {
    const errorMessage =
     error instanceof Error ? error.message : String(error)

    // Check if this is a cancellation
    const isCancellationError =
     errorMessage.includes('Download cancelled') ||
     errorMessage.includes('Validation cancelled') ||
     errorMessage.includes('Hash computation cancelled') ||
     errorMessage.includes('cancelled') ||
     errorMessage.includes('aborted')

    // Check if this is a validation failure
    const isValidationError =
     errorMessage.includes('Hash verification failed') ||
     errorMessage.includes('Size verification failed') ||
     errorMessage.includes('Failed to verify file')

    // Pause and cancel both surface here as a cancellation; treat as a
    // stop (emit stopped, return) so it never becomes an error toast.
    if (isCancellationError) {
     logger.info('Download stopped for model:', modelId)
     events.emit(DownloadEvent.onFileDownloadStopped, {
      modelId,
      downloadType: 'Model',
     })
     return
    }

    logger.error('Error downloading model:', modelId, opts, error)
    if (isValidationError) {
     // Cancel any other download tasks for this model
     try {
      this.abortImport(modelId)
     } catch (cancelError) {
      logger.warn('Failed to cancel download task:', cancelError)
     }

     // Emit validation failure event
     events.emit(DownloadEvent.onModelValidationFailed, {
      modelId,
      downloadType: 'Model',
      error: errorMessage,
      reason: 'validation_failed',
     })
    } else {
     // Regular download error
     events.emit(DownloadEvent.onFileDownloadError, {
      modelId,
      downloadType: 'Model',
      error: errorMessage,
     })
    }
    throw error
   }
  }

  // Validate GGUF files
  const janDataFolderPath = await getJanDataFolderPath()
  const fullModelPath = await joinPath([janDataFolderPath, modelPath])
  let isEmbedding = false
  let mtpLayers = 0
  let expertCount = 0
  let specKind: SpecDraftKind = 'mtp'
  let templateKwargs: TemplateKwarg[] = []
  let resolvedName: string | undefined

  try {
   // Validate main model file
   const modelMetadata = await readGgufMetadata(fullModelPath)
   logger.info(
    `Model GGUF validation successful: version ${modelMetadata.version}, tensors: ${modelMetadata.tensor_count}`
   )

   if (detectEmbeddingFromGgufMeta(modelMetadata.metadata)) {
    isEmbedding = true
   }
   mtpLayers = detectMtpLayersFromGgufMeta(modelMetadata.metadata)
   expertCount = detectExpertCountFromGgufMeta(modelMetadata.metadata)
   templateKwargs = detectTemplateKwargsFromChatTemplate(
    modelMetadata.metadata?.['tokenizer.chat_template']
   )

   const rawName = modelMetadata.metadata?.['general.name']
   if (typeof rawName === 'string') {
    const normalized = rawName.trim().replace(/\s+/g, '-')
    if (normalized.length > 0) resolvedName = normalized
   }

   // Validate mmproj file if present
   if (mmprojPath) {
    const fullMmprojPath = await joinPath([janDataFolderPath, mmprojPath])
    const mmprojMetadata = await readGgufMetadata(fullMmprojPath)
    logger.info(
     `Mmproj GGUF validation successful: version ${mmprojMetadata.version}, tensors: ${mmprojMetadata.tensor_count}`
    )
   }

   // Validate the draft, decide which spec type it implements, and read
   // its head count (the main gguf usually lacks nextn_predict_layers when
   // MTP ships as a separate file).
   if (draftModelPath) {
    const fullDraftPath = await joinPath([janDataFolderPath, draftModelPath])
    const draftMetadata = await readGgufMetadata(fullDraftPath)
    // Only asked for a DFlash-architecture draft, since that is the one
    // case a tensor decides (DSpark = DFlash + Markov head).
    const hasMarkovHead =
     draftMetadata.metadata?.['general.architecture'] === 'dflash'
      ? (await findGgufTensors(fullDraftPath, [MARKOV_HEAD_TENSOR]))
       .length > 0
      : undefined
    specKind = resolveSpecDraftKind(draftMetadata.metadata, {
     hasMarkovHead,
     hint: opts.specDraftKind,
    })
    const draftLayers = detectMtpLayersFromGgufMeta(draftMetadata.metadata)
    // Only an MTP head reports nextn layers; the other flavours have none,
    // and the count is what makes the settings panel offer the toggle.
    mtpLayers = draftLayers > 0 ? draftLayers : Math.max(mtpLayers, 1)
   }
  } catch (error) {
   logger.error('GGUF validation failed:', error)
   throw new Error(
    `Invalid GGUF file(s): ${error.message || 'File format validation failed'
    }`
   )
  }

  // Calculate file sizes
  let size_bytes = (await fs.fileStat(fullModelPath)).size
  if (mmprojPath) {
   size_bytes += (
    await fs.fileStat(await joinPath([janDataFolderPath, mmprojPath]))
   ).size
  }
  if (draftModelPath) {
   size_bytes += (
    await fs.fileStat(await joinPath([janDataFolderPath, draftModelPath]))
   ).size
  }

  if (!resolvedName) {
   resolvedName = fileName(opts.modelPath).replace(/\.gguf$/i, '') || modelId
  }

  // TODO: add updateModelConfig() method
  const modelConfig = {
   model_path: modelPath,
   mmproj_path: mmprojPath,
   name: resolvedName,
   size_bytes,
   model_sha256: opts.modelSha256,
   model_size_bytes: opts.modelSize,
   mmproj_sha256: opts.mmprojSha256,
   mmproj_size_bytes: opts.mmprojSize,
   embedding: isEmbedding,
   embedding_check_v: EMBEDDING_CHECK_VERSION,
   mtp_layers: mtpLayers,
   mtp_check_v: MTP_CHECK_VERSION,
   expert_count: expertCount,
   expert_check_v: EXPERT_CHECK_VERSION,
   template_kwargs: templateKwargs,
   template_kwargs_check_v: TEMPLATE_KWARGS_CHECK_VERSION,
   // A separate draft gguf is downloaded only to be used, so enable it by
   // default. Embedded-MTP models stay opt-in (no flag written here) and
   // need no spec_type: their flavour is always draft-mtp.
   ...(draftModelPath
    ? {
     mtp_model_path: draftModelPath,
     mtp: true,
     spec_type: `draft-${specKind}`,
    }
    : {}),
   ...(isEmbedding
    ? { pooling: 'mean', ubatch_size: 2048, batch_size: 2048 }
    : {}),
  } as ModelConfig
  await fs.mkdir(await joinPath([janDataFolderPath, modelDir]))
  await fs.writeYaml(configPath, modelConfig)
  events.emit(AppEvent.onModelImported, {
   modelId,
   modelPath,
   mmprojPath,
   size_bytes,
   model_sha256: opts.modelSha256,
   model_size_bytes: opts.modelSize,
   mmproj_sha256: opts.mmprojSha256,
   mmproj_size_bytes: opts.mmprojSize,
   embedding: isEmbedding,
  })

  if (downloadItems.length > 0) {
   events.emit(DownloadEvent.onFileDownloadAndVerificationSuccess, {
    modelId,
    downloadType: 'Model',
   })
  }

  try {
   await this.refreshEnginePreset()
  } catch (e) {
   logger.warn(`Router refresh after import(${modelId}) failed:`, e)
  }
 }

 /**
  * Deletes the entire model folder for a given modelId
  * @param modelId The model ID to delete
  */
 private async deleteModelFolder(modelId: string): Promise<void> {
  try {
   const modelDir = await joinPath([
    await this.getProviderPath(),
    'models',
    modelId,
   ])

   if (await fs.existsSync(modelDir)) {
    logger.info(`Cleaning up model directory: ${modelDir}`)
    await fs.rm(modelDir)
   }
  } catch (deleteError) {
   logger.warn('Failed to delete model directory:', deleteError)
  }
 }

 override async abortImport(modelId: string): Promise<void> {
  // Cancel any active download task
  // prepend provider name to avoid name collision
  const taskId = this.createDownloadTaskId(modelId)
  try {
   await this.downloads().cancelDownload(taskId)
  } catch (cancelError) {
   logger.warn('Failed to cancel download task:', cancelError)
  }

  // Delete the entire model folder if it exists (for validation failures)
  await this.deleteModelFolder(modelId)
 }

 override async pauseImport(modelId: string): Promise<void> {
  const taskId = this.createDownloadTaskId(modelId)
  // Pause keeps the partial .tmp for resume; the model folder is preserved.
  await this.downloads().pauseDownload(taskId)
 }

 private async getRandomPort(): Promise<number> {
  return 49152 + Math.floor(Math.random() * (65535 - 49152))
 }

 private parseEnvFromString(
  target: Record<string, string>,
  envString: string
 ): void {
  envString
   .split(';')
   .filter((pair) => pair.trim())
   .forEach((pair) => {
    const [key, ...valueParts] = pair.split('=')
    const cleanKey = key?.trim()

    if (
     cleanKey &&
     valueParts.length > 0 &&
     !cleanKey.startsWith('LLAMA')
    ) {
     target[cleanKey] = valueParts.join('=').trim()
    }
   })
 }

 override async load(
  modelId: string,
  _settings?: unknown,
  isEmbedding: boolean = false
 ): Promise<SessionInfo> {
  const sInfo = await this.findSessionByModel(modelId)
  if (sInfo) {
   throw new Error('Model already loaded!!')
  }

  if (this.loadingModels.has(modelId)) {
   return this.loadingModels.get(modelId)!
  }

  const loadingPromise = this.performLoad(modelId, isEmbedding)
  this.loadingModels.set(modelId, loadingPromise)

  try {
   return await loadingPromise
  } finally {
   this.loadingModels.delete(modelId)
  }
 }

 // Awaits the deferred startup, then makes one direct attempt if the worker
 // still isn't up. Safe to call redundantly: start_engine returns the running
 // worker rather than spawning a second one.
 private async ensureEngineReady(): Promise<void> {
  await this.ensureProvisioned().catch(() => undefined)
  if (!(await this.getEngineInfo())) {
   await this.startEngine()
  }
 }

 private async performLoad(
  modelId: string,
  isEmbedding: boolean = false
 ): Promise<SessionInfo> {
  await this.ensureEngineReady()
  // The worker lists GPUs once, at start. A laptop that left eco mode (or lost
  // its dGPU) since then would load this model onto the wrong device, and the
  // user would have to find Settings > Hardware > Refresh to fix it.
  try {
   await this.reconcileEngineDevices(modelId)
  } catch (e) {
   // A failed restart surfaces as "engine is not running" just below.
   logger.warn('Could not re-check the engine devices before loading:', e)
  }
  if (!(await this.getEngineInfo())) {
   throw new Error(
    'The llama.cpp engine is not running. Please restart the app.'
   )
  }

  if (!isEmbedding) {
   await this.evictChatIfAtCapacity(modelId)
  }

  try {
   const info = await loadLlamaModel(modelId, isEmbedding)
   if (!isEmbedding) {
    this.loadedChatOrder = this.loadedChatOrder.filter((m) => m !== modelId)
    this.loadedChatOrder.push(modelId)
   }
   return info
  } catch (error) {
   logger.error('Error in load command:\n', error)
   throw error
  }
 }

 /**
  * Enforce `userModelsMax` against chat models only. Reconciles the local
  * FIFO against the router's loaded set, then unloads the oldest chat model
  * if loading `incomingModelId` would exceed the user-configured cap.
  */
 private async evictChatIfAtCapacity(incomingModelId: string): Promise<void> {
  if (this.userModelsMax <= 0) return // unlimited

  let loaded: string[] = []
  try {
   loaded = await this.getLoadedModels()
  } catch {
   // If we can't introspect, fall back to the local FIFO — better to
   // over-evict than to violate the cap.
   loaded = [...this.loadedChatOrder]
  }
  const loadedSet = new Set(loaded)
  this.loadedChatOrder = this.loadedChatOrder.filter(
   (m) => loadedSet.has(m) && m !== incomingModelId
  )

  while (this.loadedChatOrder.length >= this.userModelsMax) {
   const victim = this.loadedChatOrder.shift()
   if (!victim) break
   try {
    const result = await unloadLlamaModel(victim)
    if (!result.success) {
     logger.warn(
      `Pre-eviction of ${victim} reported failure: ${result.error}`
     )
    } else {
     logger.info(
      `Pre-evicted chat model ${victim} to make room for ${incomingModelId}`
     )
    }
   } catch (e) {
    logger.warn(`Pre-eviction of ${victim} threw:`, e)
   }
  }
 }

 override async unload(modelId: string): Promise<UnloadResult> {
  const sInfo = await this.findSessionByModel(modelId)
  if (!sInfo) {
   throw new Error(`No active session found for model: ${modelId}`)
  }
  try {
   const result = await unloadLlamaModel(modelId)
   if (result.success) {
    this.loadedChatOrder = this.loadedChatOrder.filter((m) => m !== modelId)
    logger.info(`Successfully unloaded model ${modelId}`)
   } else {
    logger.warn(`Failed to unload model ${modelId}: ${result.error}`)
   }
   return result
  } catch (error) {
   logger.error('Error in unload command:', error)
   return {
    success: false,
    error: `Failed to unload model: ${error}`,
   }
  }
 }

 private downloads(): DownloadService {
  const service = this.services.get(ServiceKeys.Downloads)
  if (!service) throw new Error('Downloads service is not available')
  return service
 }

 /**
  * The id becomes a Tauri event name (`download-<taskId>`), which cannot contain
  * a dot. Dots are replaced rather than truncated at: truncating collapsed every
  * `Jan-v3.*` quant onto one id, and Rust cancels an in-flight task whose id
  * repeats -- deleting its partial file -- so downloading one quant destroyed
  * another's, and pause/cancel hit whichever quant happened to be registered.
  */
 private createDownloadTaskId(modelId: string) {
  // prepend provider to make taskId unique across providers
  return `${this.provider}/${modelId.replace(/\./g, '-')}`
 }

 private async *handleStreamingResponse(
  url: string,
  headers: HeadersInit,
  body: string,
  abortController?: AbortController
 ): AsyncIterable<chatCompletionChunk> {
  // AbortSignal.any() is not available in all runtimes (e.g. WebKit/JavaScriptCore),
  // so we manually combine the timeout and external abort signals.
  const combinedController = new AbortController()
  const timeoutId = setTimeout(
   () => combinedController.abort(new Error('Request timed out')),
   this.timeout * 1000
  )
  if (abortController?.signal) {
   if (abortController.signal.aborted) {
    combinedController.abort(abortController.signal.reason)
   } else {
    abortController.signal.addEventListener(
     'abort',
     () => combinedController.abort(abortController.signal.reason),
     { once: true }
    )
   }
  }
  const response = await this.loopbackFetch(url, {
   method: 'POST',
   headers,
   body,
   connectTimeout: Number(this.timeout) * 1000, // default 10 minutes
   signal: combinedController.signal,
  }).finally(() => clearTimeout(timeoutId))
  if (!response.ok) {
   const errorData = await response.json().catch(() => null)
   throw new Error(
    `API request failed with status ${response.status}: ${JSON.stringify(
     errorData
    )}`
   )
  }

  if (!response.body) {
   throw new Error('Response body is null')
  }

  const reader = response.body.getReader()
  const decoder = new TextDecoder('utf-8')
  let buffer = ''
  let jsonStr = ''
  try {
   while (true) {
    const { done, value } = await reader.read()

    if (done) {
     break
    }

    buffer += decoder.decode(value, { stream: true })

    // Process complete lines in the buffer
    const lines = buffer.split('\n')
    buffer = lines.pop() || '' // Keep the last incomplete line in the buffer

    for (const line of lines) {
     const trimmedLine = line.trim()
     if (!trimmedLine || trimmedLine === 'data: [DONE]') {
      continue
     }

     if (trimmedLine.startsWith('data: ')) {
      jsonStr = trimmedLine.slice(6)
     } else if (trimmedLine.startsWith('error: ')) {
      jsonStr = trimmedLine.slice(7)
      const error = JSON.parse(jsonStr)
      throw new Error(error.message)
     } else {
      // it should not normally reach here
      throw new Error('Malformed chunk')
     }
     try {
      const data = JSON.parse(jsonStr)
      const chunk = data as chatCompletionChunk

      yield chunk
     } catch (e) {
      logger.error('Error parsing JSON from stream or server error:', e)
      // re‑throw so the async iterator terminates with an error
      throw e
     }
    }
   }
  } finally {
   reader.releaseLock()
  }
 }

 private async findSessionByModel(
  modelId: string
 ): Promise<SessionInfo | null> {
  try {
   return await pluginFindSessionByModel(modelId)
  } catch (e) {
   logger.error(e)
   throw new Error(String(e))
  }
 }

 private async ensureHealthySession(modelId: string): Promise<SessionInfo> {
  return pluginEnsureSessionReady(modelId, false)
 }

 override async chat(
  opts: chatCompletionRequest,
  abortController?: AbortController
 ): Promise<chatCompletion | AsyncIterable<chatCompletionChunk>> {
  const release = await this.acquireEngineLease()
  let streaming = false
  try {
   const result = await this.chatOnEngine(opts, abortController)
   if (opts.stream && Symbol.asyncIterator in result) {
    // The caller reads the stream after this returns, so the lease goes
    // with it. An abort drops it too, in case the stream is never read; one
    // that already happened (before this call, or while the session was
    // being resolved) fires no event from here on, so it is checked now.
    streaming = true
    const signal = abortController?.signal
    if (signal?.aborted) {
     release()
    } else {
     signal?.addEventListener('abort', release, { once: true })
    }
    return this.releaseWhenDone(release, result)
   }
   return result
  } finally {
   if (!streaming) release()
  }
 }

 private async chatOnEngine(
  opts: chatCompletionRequest,
  abortController?: AbortController
 ): Promise<chatCompletion | AsyncIterable<chatCompletionChunk>> {
  const sessionInfo = await this.ensureHealthySession(opts.model)
  const baseUrl = `http://localhost:${sessionInfo.port}/v1`
  const url = `${baseUrl}/chat/completions`
  const headers = {
   'Content-Type': 'application/json',
   'Authorization': `Bearer ${sessionInfo.api_key}`,
  }
  // always enable prompt progress return if stream is true
  // Requires llamacpp version > b6399
  // Example json returned from server
  // {"choices":[{"finish_reason":null,"index":0,"delta":{"role":"assistant","content":null}}],"created":1758113912,"id":"chatcmpl-UwZwgxQKyJMo7WzMzXlsi90YTUK2BJro","model":"qwen","system_fingerprint":"b1-e4912fc","object":"chat.completion.chunk","prompt_progress":{"total":36,"cache":0,"processed":36,"time_ms":5706760300}}
  // (chunk.prompt_progress?.processed / chunk.prompt_progress?.total) * 100
  // chunk.prompt_progress?.cache is for past tokens already in kv cache
  opts.return_progress = true
  // Per-chunk timings so callers can track live token counts during
  // generation instead of only once the stream finishes.
  opts.timings_per_token = true

  const body = JSON.stringify(opts)
  if (opts.stream) {
   return this.handleStreamingResponse(url, headers, body, abortController)
  }
  // Handle non-streaming response
  const response = await this.loopbackFetch(url, {
   method: 'POST',
   headers,
   body,
   signal: abortController?.signal,
  })

  if (!response.ok) {
   const errorData = await response.json().catch(() => null)
   throw new Error(
    `API request failed with status ${response.status}: ${JSON.stringify(
     errorData
    )}`
   )
  }

  const completionResponse = (await response.json()) as chatCompletion

  return completionResponse
 }

 override async delete(modelId: string): Promise<void> {
  const modelDir = await joinPath([
   await this.getProviderPath(),
   'models',
   modelId,
  ])

  if (!(await fs.existsSync(await joinPath([modelDir, 'model.yml'])))) {
   throw new Error(`Model ${modelId} does not exist`)
  }

  await fs.rm(modelDir)

  try {
   await this.refreshEnginePreset()
  } catch (e) {
   logger.warn(`Router refresh after delete(${modelId}) failed:`, e)
  }
 }

 override async getLoadedModels(): Promise<string[]> {
  try {
   let models: string[] = await pluginGetLoadedModels()
   return models
  } catch (e) {
   logger.error(e)
   throw new Error(e)
  }
 }

 /**
  * Check if mmproj.gguf file exists for a given model ID
  * @param modelId - The model ID to check for mmproj.gguf
  * @returns Promise<boolean> - true if mmproj.gguf exists, false otherwise
  */
 async checkMmprojExists(modelId: string): Promise<boolean> {
  try {
   const modelConfigPath = await joinPath([
    await this.getProviderPath(),
    'models',
    modelId,
    'model.yml',
   ])

   const modelConfig = await fs.readYaml<ModelConfig>(modelConfigPath)

   // If mmproj_path is not defined in YAML, return false
   if (modelConfig.mmproj_path) {
    return true
   }

   const mmprojPath = await joinPath([
    await this.getProviderPath(),
    'models',
    modelId,
    'mmproj.gguf',
   ])
   return await fs.existsSync(mmprojPath)
  } catch (e) {
   logger.error(`Error checking mmproj.gguf for model ${modelId}:`, e)
   return false
  }
 }

 async getMtpInfo(modelId: string): Promise<{
  mtp_layers: number
  mtp: boolean
  spec_draft_n_max?: number
  spec_draft_n_min?: number
  spec_draft_p_min?: number
  spec_draft_sampling?: string
  spec_draft_n_ubatch?: number
 }> {
  const path = await joinPath([
   await this.getProviderPath(),
   'models',
   modelId,
   'model.yml',
  ])
  if (!(await fs.existsSync(path))) {
   return { mtp_layers: 0, mtp: false }
  }
  const cfg = (await fs.readYaml<ModelConfig>(path)) as ModelConfig & {
   mtp_layers?: number
   mtp?: boolean
   spec_draft_n_max?: number
   spec_draft_n_min?: number
   spec_draft_p_min?: number
   spec_draft_sampling?: string
   spec_draft_n_ubatch?: number
  }
  return {
   mtp_layers: typeof cfg.mtp_layers === 'number' ? cfg.mtp_layers : 0,
   mtp: cfg.mtp === true,
   spec_draft_n_max: cfg.spec_draft_n_max,
   spec_draft_n_min: cfg.spec_draft_n_min,
   spec_draft_p_min: cfg.spec_draft_p_min,
   spec_draft_sampling: cfg.spec_draft_sampling,
   spec_draft_n_ubatch: cfg.spec_draft_n_ubatch,
  }
 }

 async updateMtpSettings(
  modelId: string,
  patch: {
   mtp?: boolean
   spec_draft_n_max?: number | null
   spec_draft_n_min?: number | null
   spec_draft_p_min?: number | null
   spec_draft_sampling?: string | null
   spec_draft_n_ubatch?: number | null
  }
 ): Promise<void> {
  const configPath = await joinPath([
   await this.getProviderPath(),
   'models',
   modelId,
   'model.yml',
  ])
  if (!(await fs.existsSync(configPath))) {
   throw new Error(`model.yml not found for ${modelId}`)
  }
  const cfg = (await fs.readYaml<ModelConfig>(configPath)) as ModelConfig & {
   mtp?: boolean
   spec_draft_n_max?: number
   spec_draft_n_min?: number
   spec_draft_p_min?: number
   spec_draft_sampling?: string
   spec_draft_n_ubatch?: number
  }

  if (typeof patch.mtp === 'boolean') cfg.mtp = patch.mtp
  const assignNumeric = (
   key:
    | 'spec_draft_n_max'
    | 'spec_draft_n_min'
    | 'spec_draft_p_min'
    | 'spec_draft_n_ubatch',
   value: number | null | undefined
  ) => {
   if (value === null) {
    delete cfg[key]
   } else if (typeof value === 'number' && Number.isFinite(value)) {
    cfg[key] = value
   }
  }
  if ('spec_draft_n_max' in patch) assignNumeric('spec_draft_n_max', patch.spec_draft_n_max)
  if ('spec_draft_n_min' in patch) assignNumeric('spec_draft_n_min', patch.spec_draft_n_min)
  if ('spec_draft_p_min' in patch) assignNumeric('spec_draft_p_min', patch.spec_draft_p_min)
  if ('spec_draft_n_ubatch' in patch)
   assignNumeric('spec_draft_n_ubatch', patch.spec_draft_n_ubatch)
  if (patch.spec_draft_sampling === null) {
   delete cfg.spec_draft_sampling
  } else if (typeof patch.spec_draft_sampling === 'string') {
   cfg.spec_draft_sampling = patch.spec_draft_sampling
  }

  await fs.writeYaml(configPath, cfg)

  try {
   await this.refreshEnginePreset()
  } catch (e) {
   logger.warn(`Failed to restart router after MTP update for ${modelId}`, e)
  }
 }

 /**
  * Persist a per-model setting into `model.yml` and apply the engine preset
  * before resolving. A failed application restores the saved configuration
  * and rejects so the caller can report the error.
  *
  * Sidebar keys are mapped to the canonical `model.yml` / preset keys here.
  * Keys not in the mapping are silently ignored because they are Jan-side
  * concerns or are not emitted by `preset.ts`.
  */
 async updateModelSettings(
  modelId: string,
  patch: Record<string, string | number | boolean | null | undefined>
 ): Promise<void> {
  const configPath = await joinPath([
   await this.getProviderPath(),
   'models',
   modelId,
   'model.yml',
  ])
  if (!(await fs.existsSync(configPath))) {
   throw new Error(`model.yml not found for ${modelId}`)
  }
  const cfg = (await fs.readYaml<ModelConfig>(configPath)) as ModelConfig & Record<string, unknown>
  const previousConfig = { ...cfg }

  let touched = false
  for (const [sidebarKey, value] of Object.entries(patch)) {
   const m = MODEL_SETTINGS_YAML_MAPPING[sidebarKey]
   if (!m) continue
   const next = m.coerce(value)
   if (next === null) {
    if (m.yamlKey in cfg) {
     delete (cfg as Record<string, unknown>)[m.yamlKey]
     touched = true
    }
   } else {
    ; (cfg as Record<string, unknown>)[m.yamlKey] = next
    touched = true
   }
  }

  if (!touched) return

  await fs.writeYaml(configPath, cfg)

  try {
   await this.refreshEnginePreset()
  } catch (e) {
   await fs.writeYaml(configPath, previousConfig)
   throw e
  }
 }

 /** Stable identity of a device list: id and name, not the changing free memory. */
 private deviceKeys(list: ReadonlyArray<{ id: string; name: string }>): string[] {
  return list.map((d) => `${d.id}|${d.name}`).sort()
 }

 /** The machine's devices right now, or null when they could not be listed. */
 private async currentDeviceKeys(
  envs: Record<string, string>
 ): Promise<string[] | null> {
  try {
   const list = await engineDevices(envs)
   return Array.isArray(list) ? this.deviceKeys(list) : null
  } catch (e) {
   logger.warn('Could not enumerate the engine devices:', e)
   return null
  }
 }

 /**
  * A request holds this from before it resolves the worker's port and key
  * until it is done with them, so a restart cannot stop the worker in between:
  * `reconcileEngineDevices` declines while any lease is held, and while it
  * decides, new leases queue behind it. The worker's own idle check cannot
  * cover that, because it only sees requests the worker has already admitted.
  */
 private async acquireEngineLease(): Promise<() => void> {
  while (this.engineGate) await this.engineGate
  // No await between the last gate check and this increment.
  this.engineLeases++
  let released = false
  return () => {
   if (released) return
   released = true
   this.engineLeases--
  }
 }

 private async withEngineLease<T>(fn: () => Promise<T>): Promise<T> {
  const release = await this.acquireEngineLease()
  try {
   return await fn()
  } finally {
   release()
  }
 }

 /**
  * Keeps the lease for as long as a stream is being read, and drops it when
  * the reader finishes, fails or stops early. A stream that is never read
  * keeps it until the request is aborted; that only defers a restart.
  */
 private async *releaseWhenDone<T>(
  release: () => void,
  stream: AsyncIterable<T>
 ): AsyncIterable<T> {
  try {
   yield* stream
  } finally {
   release()
  }
 }

 /**
  * Restarts the engine when the machine now offers different devices than the
  * running worker initialised, so a GPU that appeared after launch is used for
  * the next load instead of only being listed in Settings.
  *
  * The baseline is what the worker itself reported when it started, so it is
  * right even if the GPU appeared between the worker starting and this call,
  * and for a worker this page did not start. A worker that reported nothing
  * has no baseline, so the answer is 'unchanged'.
  *
  * It never interrupts work: a request holding a lease, a generation in
  * flight, or a load of a model other than `loadingModelId` returns 'busy' and
  * leaves the worker alone. A load passes its own id because it is already
  * registered as loading by the time it asks.
  *
  * Two layers keep requests safe. The lease and gate here cover this
  * extension's own requests. The native local API proxy, the agent loop and
  * the model-lifecycle commands hold a lease in Rust, from before they read
  * the worker's address until their response is finished, and
  * `beginEngineRestart` only agrees when none is held; while the restart runs,
  * those requests wait for the new worker instead of failing.
  */
 async reconcileEngineDevices(
  loadingModelId?: string
 ): Promise<'unchanged' | 'restarted' | 'busy'> {
  // One at a time. Each owns the gate from its idle check until its restart is
  // done; a second one raising its own gate would, on finishing, clear it
  // while the first is still deciding, and a request admitted in between
  // would never be seen by the first one's idle check.
  const run = this.reconcileQueue.then(() =>
   this.reconcileOnce(loadingModelId)
  )
  this.reconcileQueue = run.then(
   () => undefined,
   () => undefined
  )
  return run
 }

 private async reconcileOnce(
  loadingModelId?: string
 ): Promise<'unchanged' | 'restarted' | 'busy'> {
  const engine = await pluginGetEngineInfo().catch(() => null)
  if (!engine?.devices) return 'unchanged'
  const before = this.deviceKeys(engine.devices)

  const envs: Record<string, string> = {}
  if (this.llamacpp_env) this.parseEnvFromString(envs, this.llamacpp_env)
  const now = await this.currentDeviceKeys(envs)
  if (!now || now.join('\n') === before.join('\n')) return 'unchanged'

  // The gate goes up before the idle check and stays up through the stop, so
  // no request can be admitted between them. The extension targets ES2018,
  // which has no Promise.withResolvers, hence the executor.
  let openGate = () => { }
  this.engineGate = new Promise<void>((resolve) => {
   openGate = resolve
  })
  try {
   const otherLoads = [...this.loadingModels.keys()].some(
    (id) => id !== loadingModelId
   )
   // The native side claims the worker last: it refuses while a proxy, agent
   // or lifecycle request holds it or a generation runs, and once it agrees,
   // every native request waits until the restart is over.
   if (this.engineLeases > 0 || otherLoads || !(await beginEngineRestart())) {
    logger.info('Device list changed but the engine is busy; not restarting')
    return 'busy'
   }

   try {
    logger.info(
     `Device list changed (${before.join(', ')} -> ${now.join(', ')}); restarting the engine`
    )
    await stopEngine()
    this.loadedChatOrder = []
    this.enginePort = undefined
    this.engineApiKey = undefined
    await this.startEngine()
    return 'restarted'
   } finally {
    await endEngineRestart().catch(() => undefined)
   }
  } finally {
   this.engineGate = null
   openGate()
  }
 }

 async getDevices(): Promise<DeviceList[]> {
  // set envs
  const envs: Record<string, string> = {}
  if (this.llamacpp_env) this.parseEnvFromString(envs, this.llamacpp_env)

  try {
   const dList = await engineDevices(envs)
   // On Linux with AMD GPUs, llama.cpp via Vulkan may report UMA (shared) memory as device-local.
   // For clearer UX, override with dedicated VRAM from the hardware plugin when available.
   try {
    const sysInfo = await getSystemInfo()
    if (sysInfo?.os_type === 'linux' && Array.isArray(sysInfo.gpus)) {
     const usage = await getSystemUsage()
     if (usage && Array.isArray(usage.gpus)) {
      return overrideAmdVulkanMemory(dList, sysInfo.gpus, usage.gpus)
     }
    }
   } catch (e) {
    logger.warn('Device memory override (AMD/Linux) failed:', e)
   }

   return dList
  } catch (error) {
   logger.error('Failed to query devices:\n', error)
   throw new Error('Failed to load llamacpp backend')
  }
 }

 /**
  * Resolves the default/preferred embedding model, importing and loading
  * sentence-transformer-mini as the fallback, then ensures a session exists.
  * Shared by embed() and getEmbeddingContextSize() so both agree on which
  * model is "the" embedding model.
  */
 private async ensureEmbeddingModelLoaded(): Promise<SessionInfo> {
  const downloadedModelList = await this.list()
  const installedEmbedding = downloadedModelList.filter(
   (m) => (m as any).embedding === true
  )
  const hasMini = downloadedModelList.some(
   (m) => m.id === FALLBACK_EMBEDDING_MODEL_ID
  )
  let preferred = await getDefaultEmbeddingModelId('llamacpp')

  if (!preferred && installedEmbedding.length === 1 && !hasMini) {
   preferred = installedEmbedding[0].id
   await setDefaultEmbeddingModelId('llamacpp', preferred)
   logger.info(
    `Auto-promoted "${preferred}" as default embedding model (single installed model, sentence-transformer-mini not present)`
   )
  }

  const preferredMatch =
   preferred && installedEmbedding.find((m) => m.id === preferred)

  if (preferred && !preferredMatch) {
   logger.warn(
    `Default embedding model "${preferred}" not installed; falling back to sentence-transformer-mini`
   )
  }

  const targetModelId = preferredMatch
   ? (preferred as string)
   : FALLBACK_EMBEDDING_MODEL_ID

  let sInfo = await this.findSessionByModel(targetModelId)
  if (!sInfo) {
   if (targetModelId === FALLBACK_EMBEDDING_MODEL_ID && !hasMini) {
    await this.import(FALLBACK_EMBEDDING_MODEL_ID, {
     modelPath: FALLBACK_EMBEDDING_MODEL_URL,
    })
   }
   sInfo = await this.load(targetModelId, undefined, true)
  }
  return sInfo as SessionInfo
 }

 /**
  * Actual post-fit context window of the embedding model, read from the
  * router's /props endpoint (the same source getModelProps uses for chat
  * models). Used by RAG ingestion to size chunks so they don't exceed the
  * model's n_ctx (e.g. sentence-transformer-mini natively caps at 256).
  */
 async getEmbeddingContextSize(): Promise<number | undefined> {
  const sInfo = await this.ensureEmbeddingModelLoaded()
  const props = await this.getModelProps(sInfo.model_id)
  return props?.nCtx
 }

 /**
  * Real token counts from the embedding model's own tokenizer via /tokenize
  * on its session port. Char-based chunking can't reliably predict token
  * count (subword tokenizers vary widely by content), so callers that need
  * a hard guarantee against exceed_context_size_error should verify with
  * this rather than estimating from character length.
  */
 async countEmbeddingTokens(texts: string[]): Promise<number[]> {
  return this.withEngineLease(() => this.countEmbeddingTokensOnEngine(texts))
 }

 private async countEmbeddingTokensOnEngine(
  texts: string[]
 ): Promise<number[]> {
  const sInfo = await this.ensureEmbeddingModelLoaded()
  const counts: number[] = []
  for (const text of texts) {
   const res = await this.loopbackFetch(`http://localhost:${sInfo.port}/tokenize`, {
    method: 'POST',
    headers: {
     'Content-Type': 'application/json',
     'Authorization': `Bearer ${sInfo.api_key}`,
    },
    body: JSON.stringify({ content: text, model: sInfo.model_id }),
   })
   if (!res.ok) {
    throw new Error(`Tokenize request failed with status ${res.status}`)
   }
   const json = (await res.json()) as { tokens?: unknown[] }
   counts.push(Array.isArray(json.tokens) ? json.tokens.length : 0)
  }
  return counts
 }

 /**
  * Token budget for one embedding request.
  *
  * Deliberately not the engine-wide `ubatch_size`: preset.ts pins every
  * embedding model's section to its own ubatch (DEFAULT_EMBEDDING_UBATCH) and
  * to `ctx-size = 0`, so the real ceiling is the embedder's own trained
  * context -- 512 on MiniLM. llama.cpp rejects a batch wider than either with
  * no retry path, so the budget is the smaller of the two.
  */
 private async embedBatchBudget(sInfo: SessionInfo): Promise<number> {
  let budget = DEFAULT_EMBEDDING_UBATCH
  try {
   const props = await this.getModelProps(sInfo.model_id)
   const nCtx = props?.nCtx
   if (typeof nCtx === 'number' && nCtx > 0) {
    budget = Math.min(budget, nCtx)
   }
  } catch (e) {
   // Without /props the pinned ubatch is still a valid ceiling.
   logger.warn('Could not read embedder context size; using the pinned ubatch:', e)
  }
  return budget
 }

 async embed(text: string[]): Promise<EmbeddingResponse> {
  // Held for every batch: the session's port and key are resolved once, so a
  // restart between batches would fail the rest of them. Taken before the
  // model is ensured, so a load this triggers sees a held lease and does not
  // restart the engine under the embedding it was started for.
  return this.withEngineLease(() => this.embedOnEngine(text))
 }

 private async embedOnEngine(text: string[]): Promise<EmbeddingResponse> {
  const sInfo = await this.ensureEmbeddingModelLoaded()
  const batches = buildEmbedBatches(text, await this.embedBatchBudget(sInfo))

  const attemptRequest = async (
   session: SessionInfo,
   batchInput: string[]
  ) => {
   const baseUrl = `http://localhost:${session.port}/v1/embeddings`
   const headers = {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${session.api_key}`,
   }
   const body = JSON.stringify({
    input: batchInput,
    model: session.model_id,
    encoding_format: 'float',
   })
   const response = await this.loopbackFetch(baseUrl, {
    method: 'POST',
    headers,
    body,
   })
   return response
  }

  const sendBatch = async (batchInput: string[]) => {
   const response = await attemptRequest(sInfo, batchInput)
   if (!response.ok) {
    const errorData = await response.json().catch(() => null)
    throw new Error(
     `API request failed with status ${response.status}: ${JSON.stringify(errorData)}`
    )
   }
   return (await response.json()) as EmbedBatchResult
  }

  const batchResults: Array<{ result: EmbedBatchResult; offset: number }> = []
  for (const { batch, offset } of batches) {
   const result = await sendBatch(batch)
   batchResults.push({ result, offset })
  }

  return mergeEmbedResponses(
   sInfo.model_id,
   batchResults
  ) as EmbeddingResponse
 }

 /**
  * Check if a tool is supported by the model
  * Currently read from GGUF chat_template
  * @param modelId
  * @returns
  */
 async isToolSupported(modelId: string): Promise<boolean> {
  const janDataFolderPath = await getJanDataFolderPath()
  const modelConfigPath = await joinPath([
   this.providerPath,
   'models',
   modelId,
   'model.yml',
  ])
  const modelConfig = await fs.readYaml<ModelConfig>(modelConfigPath)
  // model option is required
  // NOTE: model_path and mmproj_path can be either relative to Jan's data folder or absolute path
  const modelPath = await joinPath([
   janDataFolderPath,
   modelConfig.model_path,
  ])
  return (await readGgufMetadata(modelPath)).metadata?.[
   'tokenizer.chat_template'
  ]?.includes('tools')
 }

 /**
  * Check the support status of a model by its path (local/remote)
  *
  * Returns:
  * - "RED"    → weights don't fit in total memory
  * - "YELLOW" → weights fit in VRAM but need system RAM, or KV cache doesn't fit
  * - "GREEN"  → both weights + KV cache fit in VRAM
  */
 async isModelSupported(
  path: string,
  ctxSize?: number
 ): Promise<'RED' | 'YELLOW' | 'GREEN'> {
  try {
   const result = await isModelSupported(path, Number(ctxSize))
   return result
  } catch (e) {
   throw new Error(String(e))
  }
 }

 /**
  * Validate GGUF file and check for unsupported architectures like CLIP
  */
 async validateGgufFile(filePath: string): Promise<{
  isValid: boolean
  error?: string
  metadata?: any
 }> {
  try {
   logger.info(`Validating GGUF file: ${filePath}`)
   const metadata = await readGgufMetadata(filePath)

   // Check if architecture is 'clip' which is not supported for text generation
   const architecture = metadata.metadata?.['general.architecture']
   logger.info(`Model architecture: ${architecture}`)

   if (architecture === 'clip') {
    const errorMessage =
     'This model has CLIP architecture and cannot be imported as a text generation model. CLIP models are designed for vision tasks and require different handling.'
    logger.error('CLIP architecture detected:', architecture)
    return {
     isValid: false,
     error: errorMessage,
     metadata,
    }
   }

   logger.info('Model validation passed. Architecture:', architecture)
   return {
    isValid: true,
    metadata,
   }
  } catch (error) {
   logger.error('Failed to validate GGUF file:', error)
   return {
    isValid: false,
    error: `Failed to read model metadata: ${error instanceof Error ? error.message : 'Unknown error'
     }`,
   }
  }
 }

 async getTokensCount(opts: chatCompletionRequest): Promise<number> {
  let imageTokens = 0
  const hasImages = opts.messages.some(
   (msg) =>
    Array.isArray(msg.content) &&
    msg.content.some((content) => content.type === 'image_url')
  )

  if (hasImages) {
   try {
    const janDataFolderPath = await getJanDataFolderPath()
    const modelConfigPath = await joinPath([
     this.providerPath,
     'models',
     opts.model,
     'model.yml',
    ])
    const modelConfig = await fs.readYaml<ModelConfig>(modelConfigPath)
    if (modelConfig.mmproj_path) {
     const mmprojPath = await joinPath([
      janDataFolderPath,
      modelConfig.mmproj_path,
     ])
     const metadata = await readGgufMetadata(mmprojPath)
     imageTokens = await this.calculateImageTokens(
      opts.messages,
      metadata.metadata
     )
    }
   } catch (error) {
    logger.warn('Failed to calculate image tokens:', error)
    imageTokens = this.estimateImageTokensFallback(opts.messages)
   }
  }

  let textChars = 0
  for (const msg of opts.messages) {
   if (typeof msg.content === 'string') {
    textChars += msg.content.length
   } else if (Array.isArray(msg.content)) {
    for (const part of msg.content) {
     if (part.type === 'text' && typeof part.text === 'string') {
      textChars += part.text.length
     }
    }
   }
  }
  const textTokens = Math.ceil(textChars / 4)
  return textTokens + imageTokens
 }

 private async calculateImageTokens(
  messages: chatCompletionRequestMessage[],
  metadata: Record<string, string>
 ): Promise<number> {
  // Extract vision parameters from metadata
  const projectionDim =
   Math.floor(Number(metadata['clip.vision.projection_dim']) / 10) || 256

  // Count images in messages
  let imageCount = 0
  for (const message of messages) {
   if (Array.isArray(message.content)) {
    imageCount += message.content.filter(
     (content) => content.type === 'image_url'
    ).length
   }
  }

  logger.info(
   `Calculated ${projectionDim} tokens per image, ${imageCount} images total`
  )
  return projectionDim * imageCount - imageCount // remove the lingering <__image__> placeholder token
 }

 private estimateImageTokensFallback(
  messages: chatCompletionRequestMessage[]
 ): number {
  // Fallback estimation if metadata reading fails
  const estimatedTokensPerImage = 256 // Gemma's siglip

  let imageCount = 0
  for (const message of messages) {
   if (Array.isArray(message.content)) {
    imageCount += message.content.filter(
     (content) => content.type === 'image_url'
    ).length
   }
  }

  logger.warn(
   `Fallback estimation: ${estimatedTokensPerImage} tokens per image, ${imageCount} images total`
  )
  return imageCount * estimatedTokensPerImage - imageCount // remove the lingering <__image__> placeholder token
 }
}