import { mkdirSync, existsSync } from "node:fs"; import { readFile, writeFile } from "node:fs/promises"; import { dirname } from "node:path"; export type EmbedderConfig = { backend: "llama_cpp_vulkan"; vulkanDevice?: string; runtimeTag: string; runtimeRepo: string; runtimeAsset: string; runtimeBinary: string; modelRepo: string; modelRevision: string; modelFile: string; }; export async function loadOrCreateConfig(path: string, legacyPath?: string): Promise { const existingPath = existsSync(path) ? path : legacyPath && existsSync(legacyPath) ? legacyPath : null; if (existingPath) { const parsed = parseJsonc(await readFile(existingPath, "utf8")) as EmbedderConfig; const normalized = normalizeConfig(parsed); if (existingPath !== path) { mkdirSync(dirname(path), { recursive: true }); await writeFile(path, renderConfigJsonc(normalized)); } return normalized; } mkdirSync(dirname(path), { recursive: true }); const config = defaultConfig(); await writeFile(path, renderConfigJsonc(config)); return config; } function defaultConfig(): EmbedderConfig { return { backend: "llama_cpp_vulkan", runtimeTag: "b8215", runtimeRepo: "ggml-org/llama.cpp", runtimeAsset: "llama-b8215-bin-ubuntu-vulkan-x64.tar.gz", runtimeBinary: "llama-server", modelRepo: "ggml-org/embeddinggemma-300m-qat-q8_0-GGUF", modelRevision: "main", modelFile: "embeddinggemma-300m-qat-Q8_0.gguf", }; } function normalizeConfig(config: EmbedderConfig): EmbedderConfig { const next = { ...config }; if (next.vulkanDevice !== undefined) { const trimmed = next.vulkanDevice.trim(); if (trimmed.length === 0) { delete next.vulkanDevice; } else if (!/^Vulkan\d+$/.test(trimmed)) { throw new Error("embedder config field `vulkanDevice` must look like `Vulkan0`"); } else { next.vulkanDevice = trimmed; } } if ( next.modelRepo === "ggml-org/embeddinggemma-300m-qat-q8_0-GGUF" && (next.modelFile === "embeddinggemma-300M_Q8_0.gguf" || next.modelFile === "embeddinggemma-300M-Q8_0.gguf") ) { next.modelFile = "embeddinggemma-300m-qat-Q8_0.gguf"; } if (!("runtimeRepo" in next) || !next.runtimeRepo) { next.runtimeRepo = "ggml-org/llama.cpp"; } if (!("runtimeAsset" in next) || !next.runtimeAsset) { next.runtimeAsset = "llama-b8215-bin-ubuntu-vulkan-x64.tar.gz"; } return next; } function renderConfigJsonc(config: EmbedderConfig): string { const optionalDevice = config.vulkanDevice ? ` "vulkanDevice": "${config.vulkanDevice}",\n` : ' // "vulkanDevice": "Vulkan0",\n'; return `{ // Optional Vulkan device pin for llama.cpp, for example "Vulkan0" or "Vulkan1". // Omit this field to let llama.cpp choose devices automatically. ${optionalDevice} "backend": "llama_cpp_vulkan", "runtimeTag": "${config.runtimeTag}", "runtimeRepo": "${config.runtimeRepo}", "runtimeAsset": "${config.runtimeAsset}", "runtimeBinary": "${config.runtimeBinary}", "modelRepo": "${config.modelRepo}", "modelRevision": "${config.modelRevision}", "modelFile": "${config.modelFile}" } `; } function parseJsonc(input: string): unknown { return JSON.parse(removeTrailingCommas(stripJsonComments(input))); } // JSONC here only needs comments and trailing commas for the user-edited config. function stripJsonComments(input: string): string { let output = ""; let inString = false; let escaping = false; let inLineComment = false; let inBlockComment = false; for (let index = 0; index < input.length; index += 1) { const current = input[index]; const next = input[index + 1]; if (inLineComment) { if (current === "\n") { inLineComment = false; output += current; } continue; } if (inBlockComment) { if (current === "*" && next === "/") { inBlockComment = false; index += 1; } continue; } if (inString) { output += current; if (escaping) { escaping = false; } else if (current === "\\") { escaping = true; } else if (current === "\"") { inString = false; } continue; } if (current === "\"") { inString = true; output += current; continue; } if (current === "/" && next === "/") { inLineComment = true; index += 1; continue; } if (current === "/" && next === "*") { inBlockComment = true; index += 1; continue; } output += current; } return output; } function removeTrailingCommas(input: string): string { let output = ""; let inString = false; let escaping = false; for (let index = 0; index < input.length; index += 1) { const current = input[index]; if (inString) { output += current; if (escaping) { escaping = false; } else if (current === "\\") { escaping = true; } else if (current === "\"") { inString = false; } continue; } if (current === "\"") { inString = true; output += current; continue; } if (current === ",") { const remainder = input.slice(index + 1); const match = remainder.match(/^\s*([}\]])/); if (match) { continue; } } output += current; } return output; }