Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
Resolve LM Studio model files by repo name
Adam Eivy5d925cbac73a
diff --git a/server/services/lmStudioManager.js b/server/services/lmStudioManager.jsindex d6ddff7519..101cdbc691 100644--- a/server/services/lmStudioManager.js+++ b/server/services/lmStudioManager.js@@ -488,17 +488,44 @@ async function getModelsDir() {return candidates[1] // sensible default even if it doesn't exist yet}+const normalizeRepoKey = (s) => String(s || '')+ .split('/').pop()+ .trim()+ .toLowerCase()+ .replace(/[-.]gguf$/i, '')+ .replace(/[-.]mlx[-.].*$/i, '')++async function findModelDir(modelsDir, modelId) {+ const direct = join(modelsDir, ...String(modelId || '').split('/'))+ if (await dirExists(direct)) return direct++ const wanted = normalizeRepoKey(modelId)+ if (!wanted) return null+ const publishers = await readdir(modelsDir).catch(() => [])+ for (const publisher of publishers) {+ const publisherDir = join(modelsDir, publisher)+ if (!(await dirExists(publisherDir))) continue+ const repos = await readdir(publisherDir).catch(() => [])+ const repo = repos.find((name) => normalizeRepoKey(name) === wanted)+ if (repo) return join(publisherDir, repo)+ }+ return null+}+/*** Locate an installed LM Studio model's files on disk (no network). The model- * id maps directly onto the `<publisher>/<repo>` folder. MLX models (safetensors,- * no GGUF) return `{ isMlx: true, ggufPath: null }` so the caller routes them to+ * id usually maps directly onto the `<publisher>/<repo>` folder, but LM Studio+ * can report an API id that differs from the downloaded repo. Fall back to a+ * normalized repo-name scan so `openai/gpt-oss-20b` can still resolve the local+ * `lmstudio-community/gpt-oss-20b-GGUF` folder. MLX models (safetensors, no+ * GGUF) return `{ isMlx: true, ggufPath: null }` so the caller routes them to* re-pull instead of a (impossible) file copy.* @returns {Promise<{ ggufPath: string|null, projectorPath: string|null, isMlx: boolean, isSharded: boolean }|null>}*/async function resolveLocalModel(modelId) {const modelsDir = await getModelsDir()- const dir = join(modelsDir, ...String(modelId || '').split('/'))- if (!(await dirExists(dir))) return null+ const dir = await findModelDir(modelsDir, modelId)+ if (!dir) return nullconst files = await readdir(dir).catch(() => [])if (dirIsMlx(files)) return { ggufPath: null, projectorPath: null, isMlx: true, isSharded: false }const primary = selectPrimaryGguf(files)diff --git a/server/services/lmStudioManager.test.js b/server/services/lmStudioManager.test.jsnew file mode 100644index 0000000000..4a2f349b2e--- /dev/null+++ b/server/services/lmStudioManager.test.js@@ -0,0 +1,57 @@+import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';+import os from 'os';+import path from 'path';+import fs from 'fs';++let tempDir;+let originalModelsDir;++beforeEach(() => {+ tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'portos-lms-models-'));+ originalModelsDir = process.env.LM_STUDIO_MODELS_DIR;+ process.env.LM_STUDIO_MODELS_DIR = tempDir;+ vi.resetModules();+});++afterEach(() => {+ if (originalModelsDir === undefined) delete process.env.LM_STUDIO_MODELS_DIR;+ else process.env.LM_STUDIO_MODELS_DIR = originalModelsDir;+ fs.rmSync(tempDir, { recursive: true, force: true });+});++const writeFile = (rel, content = 'gguf') => {+ const full = path.join(tempDir, ...rel.split('/'));+ fs.mkdirSync(path.dirname(full), { recursive: true });+ fs.writeFileSync(full, content);+ return full;+};++describe('lmStudioManager local model resolution', () => {+ it('resolves a model when the API id maps directly to the repo folder', async () => {+ const gguf = writeFile('lmstudio-community/gpt-oss-20b-GGUF/gpt-oss-20b-MXFP4.gguf');+ const { resolveLocalModel } = await import('./lmStudioManager.js');++ const resolved = await resolveLocalModel('lmstudio-community/gpt-oss-20b-GGUF');++ expect(resolved).toMatchObject({+ ggufPath: gguf,+ projectorPath: null,+ isMlx: false,+ isSharded: false+ });+ });++ it('falls back to a normalized repo scan when LM Studio reports a different API id', async () => {+ const gguf = writeFile('lmstudio-community/gpt-oss-20b-GGUF/gpt-oss-20b-MXFP4.gguf');+ const { resolveLocalModel } = await import('./lmStudioManager.js');++ const resolved = await resolveLocalModel('openai/gpt-oss-20b');++ expect(resolved).toMatchObject({+ ggufPath: gguf,+ projectorPath: null,+ isMlx: false,+ isSharded: false+ });+ });+});