Skip to content

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

address copilot review (round 4): cache LM Studio model list + detect macOS app

Adam Eivy6e2321c0e345
diff --git a/server/services/lmStudioManager.js b/server/services/lmStudioManager.js
index 1125cc426d..72cca1a481 100644
--- a/server/services/lmStudioManager.js
+++ b/server/services/lmStudioManager.js
@@ -7,6 +7,7 @@
import { homedir } from 'os'
import { join, basename } from 'path'
+import { existsSync } from 'fs'
import { readdir, stat, mkdir, copyFile } from 'fs/promises'
import { cosEvents } from './cosEvents.js'
import { fetchWithTimeout } from '../lib/fetchWithTimeout.js'
@@ -31,7 +32,10 @@ const DEFAULT_CONFIG = {
let config = { ...DEFAULT_CONFIG }
let isAvailable = null
let loadedModels = []
-let availableModels = []
+// null = not yet fetched; any array (even empty) = a cached result. Lets the
+// catalog-overlay path (queried per keystroke) reuse the list instead of
+// re-hitting /api/v0/models each time. Busted to null by resetCache().
+let availableModels = null
let lastCheckAt = null
// Status tracking
@@ -142,10 +146,13 @@ async function getLoadedModels(forceRefresh = false) {
}
/**
- * Get all downloaded models (loaded and not-loaded)
+ * Get all downloaded models (loaded and not-loaded).
+ * @param {boolean} [forceRefresh] - bypass the cache (callers that read live
+ * per-model `state`, e.g. embedding-model discovery, should force).
* @returns {Promise<Array>} - All downloaded models with state info
*/
-async function getAvailableModels() {
+async function getAvailableModels(forceRefresh = false) {
+ if (!forceRefresh && availableModels !== null) return availableModels
const available = await checkLMStudioAvailable()
if (!available) {
return []
@@ -353,7 +360,7 @@ async function getEmbeddings(text, options = {}) {
// Auto-discover an embedding model if none specified
let model = options.model
if (!model) {
- const models = await getAvailableModels()
+ const models = await getAvailableModels(true) // need live per-model state
const embeddingModel = models.find(m => m.type === 'embeddings' && m.state === 'loaded')
|| models.find(m => m.type === 'embeddings')
if (embeddingModel) {
@@ -437,10 +444,17 @@ function updateConfig(newConfig) {
function resetCache() {
isAvailable = null
loadedModels = []
- availableModels = []
+ availableModels = null
lastCheckAt = null
}
+// LM Studio can be installed as a macOS app without the `lms` CLI on PATH and
+// without the local server running — mirror scripts/setup-llm.js so status
+// doesn't report "Not installed" (and offer a redundant install) in that case.
+function isAppInstalled() {
+ return process.platform === 'darwin' && existsSync('/Applications/LM Studio.app')
+}
+
// ---- local-disk introspection / import (migrate fast-path) ------------------
const dirExists = (p) => stat(p).then((s) => s.isDirectory()).catch(() => false)
@@ -523,6 +537,7 @@ export {
getStatus,
updateConfig,
resetCache,
+ isAppInstalled,
resolveLocalModel,
importModelFromGguf,
DEFAULT_CONFIG
diff --git a/server/services/localLlm.js b/server/services/localLlm.js
index e9bff6ca8d..0cb0ecedc3 100644
--- a/server/services/localLlm.js
+++ b/server/services/localLlm.js
@@ -232,7 +232,7 @@ export async function listModels(backend, forceRefresh = false) {
if (!isBackend(backend)) return []
const raw = backend === 'ollama'
? await ollamaManager.getInstalledModels(forceRefresh)
- : await lmStudioManager.getAvailableModels()
+ : await lmStudioManager.getAvailableModels(forceRefresh)
return normalizeModels(backend, raw)
}
@@ -245,9 +245,10 @@ export async function getStatus() {
commandExists('ollama', ['--version']),
lmStudioManager.getStatus(),
commandExists('lms', ['version']),
+ // forceRefresh: this is the status/refresh path, so bypass the list cache.
// already normalized; capture (don't swallow) a list failure so the UI can
// distinguish "no models" from "couldn't read the model list".
- listModels('lmstudio').then((models) => ({ models, error: null })).catch((err) => ({ models: [], error: err.message }))
+ listModels('lmstudio', true).then((models) => ({ models, error: null })).catch((err) => ({ models: [], error: err.message }))
])
return {
@@ -263,7 +264,8 @@ export async function getStatus() {
downloadUrl: DOWNLOAD_URL.ollama
},
lmstudio: {
- installed: lmsCli || lmStudioStatus.available,
+ // macOS app bundle counts as installed even with no CLI / server stopped.
+ installed: lmsCli || lmStudioStatus.available || lmStudioManager.isAppInstalled(),
available: lmStudioStatus.available,
hasCli: lmsCli,
baseUrl: lmStudioStatus.baseUrl,
diff --git a/server/services/localLlm.test.js b/server/services/localLlm.test.js
index 89616ee7a2..5db1d40658 100644
--- a/server/services/localLlm.test.js
+++ b/server/services/localLlm.test.js
@@ -25,6 +25,7 @@ const mocks = vi.hoisted(() => ({
downloadModel: vi.fn(async (id) => ({ success: true, modelId: id })),
getStatus: vi.fn(async () => ({ available: false, baseUrl: 'y', loadedModels: 0 })),
resetCache: vi.fn(),
+ isAppInstalled: vi.fn(() => false),
resolveLocalModel: vi.fn(async () => null),
importModelFromGguf: vi.fn(async ({ lmstudioId }) => ({ success: true, modelId: lmstudioId }))
},