Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(picker): der LM-Studio-Knopf legt den Anbieter mit an, statt in die Sackgasse zu fuehren

purple8aa110fdb125
diff --git a/src/components/models/ModelSelector.tsx b/src/components/models/ModelSelector.tsx
index 78982fc3..66f58efb 100644
--- a/src/components/models/ModelSelector.tsx
+++ b/src/components/models/ModelSelector.tsx
@@ -14,6 +14,7 @@ import { canUseTools, resolveToolSupport, type ToolSupport } from '../../lib/too
import { backendCall } from '../../api/backend'
import { listLoadedLmStudioModels, loadLmStudioModel, unloadLmStudioModel } from '../../api/lmstudio'
import { isLmStudioProvider } from '../../lib/hf-to-provider'
+import { lmStudioSlotUpdate, adoptionReplacesBuiltinEngine } from '../../lib/lmstudio-backend-adopt'
import { nextProbeDelayMs } from '../../lib/probe-backoff'
import type { AIModel } from '../../types/models'
@@ -122,6 +123,10 @@ function LmStudioServerHint({ onStarted }: { onStarted: () => void }) {
const [starting, setStarting] = useState(false)
const [startError, setStartError] = useState('')
const [dismissed, setDismissed] = useState(LM_HINT_DISMISSED_THIS_SESSION)
+ // Starting the server also hands LM Studio the local backend slot (see
+ // lib/lmstudio-backend-adopt). When the built-in engine holds that slot the
+ // user learns it here, before the click, together with the way back.
+ const replacesBuiltinEngine = useProviderStore((s) => adoptionReplacesBuiltinEngine(s.providers.openai))
useEffect(() => {
let cancelled = false
@@ -153,6 +158,13 @@ function LmStudioServerHint({ onStarted }: { onStarted: () => void }) {
if (fresh) {
setStatus(fresh)
if (fresh.running) {
+ // A running server is only half of what the sentence above
+ // promises. The picker lists ENABLED provider slots, so without
+ // this the models stay invisible and the button leads into a dead
+ // end (Nebenbefund 4, R8 re-measure). Same call the
+ // BackendSelector makes, no LM-Studio-only path.
+ const update = lmStudioSlotUpdate(useProviderStore.getState().providers.openai)
+ if (update) useProviderStore.getState().setProviderConfig('openai', update)
onStarted()
break
}
@@ -186,6 +198,11 @@ function LmStudioServerHint({ onStarted }: { onStarted: () => void }) {
{starting ? <Loader2 size={10} className="animate-spin" /> : <PlayCircle size={10} />}
<span>{starting ? 'Starting LM Studio server…' : 'Start LM Studio Server'}</span>
</button>
+ {replacesBuiltinEngine && (
+ <p className="text-[0.55rem] text-gray-500 dark:text-gray-400 mt-1 leading-snug">
+ This also makes LM Studio your local chat backend in place of the built-in engine. You can switch back under Settings, AI Backends, Providers.
+ </p>
+ )}
{startError && (
<p className="text-[0.55rem] text-red-600/80 dark:text-red-300/70 mt-1 leading-snug">{startError}</p>
)}
diff --git a/src/lib/__tests__/lmstudio-backend-adopt.test.ts b/src/lib/__tests__/lmstudio-backend-adopt.test.ts
new file mode 100644
index 00000000..8c4c1644
--- /dev/null
+++ b/src/lib/__tests__/lmstudio-backend-adopt.test.ts
@@ -0,0 +1,125 @@
+/**
+ * Round 9 of the 2.6.7 run, Nebenbefund 4 of the R8 re-measure (Windows box,
+ * installed build): the chat picker's banner said "Start it to pick LM Studio
+ * models here", the button really started the server (port 1234 listening,
+ * /v1/models answering with real models), and the picker still offered nothing
+ * but the built-in engine and the cloud. Reason: no enabled provider slot
+ * pointed at LM Studio, and the picker lists slots.
+ *
+ * These lock in the decision the click now makes, and the honesty the banner
+ * owes before it.
+ */
+import { describe, it, expect } from 'vitest'
+import { readFileSync } from 'fs'
+import { fileURLToPath } from 'url'
+import { dirname, join } from 'path'
+import {
+ lmStudioSlotUpdate,
+ slotPointsAtLmStudio,
+ adoptionReplacesBuiltinEngine,
+ LM_STUDIO_PRESET,
+} from '../lmstudio-backend-adopt'
+
+const __filename = fileURLToPath(import.meta.url)
+const __dirname = dirname(__filename)
+
+const BUILTIN_SLOT = { enabled: true, baseUrl: 'http://127.0.0.1:8127/v1', managed: true }
+const LMS_URL = 'http://localhost:1234/v1'
+
+describe('LM_STUDIO_PRESET', () => {
+ it('is the shipped preset, not a second copy of the URL', () => {
+ expect(LM_STUDIO_PRESET.id).toBe('lmstudio')
+ expect(LM_STUDIO_PRESET.name).toBe('LM Studio')
+ expect(LM_STUDIO_PRESET.baseUrl).toBe(LMS_URL)
+ expect(LM_STUDIO_PRESET.providerId).toBe('openai')
+ })
+})
+
+describe('lmStudioSlotUpdate', () => {
+ it('takes the slot over from the app-managed built-in engine', () => {
+ const update = lmStudioSlotUpdate(BUILTIN_SLOT)
+ expect(update).toEqual({
+ enabled: true,
+ name: 'LM Studio',
+ baseUrl: LMS_URL,
+ isLocal: true,
+ managed: false,
+ })
+ })
+
+ // The load-bearing field. `managed` left at true keeps useModels on
+ // list_bundled_models (isManagedBuiltinActive) while the URL already points
+ // at LM Studio, so the picker would STILL show no LM Studio model and the
+ // dead end would survive the fix.
+ it('clears managed explicitly rather than just omitting it', () => {
+ const update = lmStudioSlotUpdate(BUILTIN_SLOT)!
+ expect(Object.prototype.hasOwnProperty.call(update, 'managed')).toBe(true)
+ expect(update.managed).toBe(false)
+ })
+
+ it('only flips enabled when the slot already points at LM Studio', () => {
+ expect(lmStudioSlotUpdate({ enabled: false, baseUrl: LMS_URL, managed: false }))
+ .toEqual({ enabled: true })
+ })
+
+ it('keeps a user label for the same port instead of renaming it', () => {
+ const update = lmStudioSlotUpdate({ enabled: false, baseUrl: LMS_URL, managed: false })!
+ expect(update.name).toBeUndefined()
+ expect(update.baseUrl).toBeUndefined()
+ })
+
+ it('has nothing to change when LM Studio already owns the slot and is on', () => {
+ expect(lmStudioSlotUpdate({ enabled: true, baseUrl: LMS_URL, managed: false })).toBeNull()
+ })
+
+ it('ignores a trailing slash and case when comparing the URL', () => {
+ expect(lmStudioSlotUpdate({ enabled: true, baseUrl: 'http://LOCALHOST:1234/v1/', managed: false })).toBeNull()
+ })
+
+ it('takes the slot over from another external backend on a different port', () => {
+ // vLLM on 8000. The user pressed the LM Studio button, so LM Studio wins.
+ const update = lmStudioSlotUpdate({ enabled: true, baseUrl: 'http://localhost:8000/v1', managed: false })!
+ expect(update.baseUrl).toBe(LMS_URL)
+ expect(update.name).toBe('LM Studio')
+ })
+
+ it('never treats an empty slot URL as a match', () => {
+ expect(slotPointsAtLmStudio({ enabled: true, baseUrl: '', managed: false })).toBe(false)
+ expect(lmStudioSlotUpdate({ enabled: true, baseUrl: '', managed: false })?.baseUrl).toBe(LMS_URL)
+ })
+})
+
+describe('adoptionReplacesBuiltinEngine', () => {
+ it('is true exactly while the managed built-in engine holds the slot', () => {
+ expect(adoptionReplacesBuiltinEngine(BUILTIN_SLOT)).toBe(true)
+ expect(adoptionReplacesBuiltinEngine({ ...BUILTIN_SLOT, managed: false })).toBe(false)
+ expect(adoptionReplacesBuiltinEngine({ enabled: true, baseUrl: LMS_URL })).toBe(false)
+ })
+})
+
+// The wiring itself. ModelSelector is hook/store/Tauri-heavy and has no render
+// harness in this repo (see model-selector-lms.test.ts), so the guard reads the
+// source, the same way AppShell-backend-autoenable.test.ts does.
+describe('ModelSelector LM Studio banner', () => {
+ const src = readFileSync(join(__dirname, '../../components/models/ModelSelector.tsx'), 'utf8')
+
+ it('adopts the openai slot once the server reports running', () => {
+ // Pre-fix the running branch called onStarted() and nothing else, which is
+ // what left the picker empty.
+ expect(src).toMatch(/lmStudioSlotUpdate\(\s*useProviderStore\.getState\(\)\.providers\.openai\s*\)/)
+ expect(src).toMatch(/setProviderConfig\('openai',\s*update\)/)
+ })
+
+ it('adopts before it refetches, or the refetch reads the old slot', () => {
+ const adopt = src.indexOf("setProviderConfig('openai', update)")
+ const refetch = src.indexOf('onStarted()', adopt)
+ expect(adopt).toBeGreaterThan(-1)
+ expect(refetch).toBeGreaterThan(adopt)
+ })
+
+ it('says out loud that the built-in engine loses the slot, and names the way back', () => {
+ expect(src).toContain('adoptionReplacesBuiltinEngine')
+ expect(src).toMatch(/This also makes LM Studio your local chat backend in place of the built-in engine/)
+ expect(src).toMatch(/Settings, AI Backends, Providers/)
+ })
+})
diff --git a/src/lib/lmstudio-backend-adopt.ts b/src/lib/lmstudio-backend-adopt.ts
new file mode 100644
index 00000000..dd0b3af2
--- /dev/null
+++ b/src/lib/lmstudio-backend-adopt.ts
@@ -0,0 +1,79 @@
+/**
+ * Adopting LM Studio as the local chat backend.
+ *
+ * Round 9 of the 2.6.7 run, Nebenbefund 4 of the R8 re-measure: the chat model
+ * picker offers "Start LM Studio Server" over the sentence "Start it to pick
+ * LM Studio models here". The button really does start the server (port 1234
+ * listens, /v1/models answers), and then nothing appears in the picker,
+ * because no provider slot points at LM Studio. The user is left in a dead
+ * end with a promise that was only half kept.
+ *
+ * The picker lists models from the ENABLED provider slots. LM Studio speaks
+ * the OpenAI protocol, so it lives in the `openai` slot, exactly like every
+ * other openai-compatible local backend. That slot holds the app's built-in
+ * engine by default (`managed: true`), which is why the startup auto-detect in
+ * AppShell refuses to take it: nobody asked for a backend swap there.
+ *
+ * Here somebody did ask. Pressing a button labelled "Start LM Studio Server"
+ * under a sentence about picking LM Studio models is the user saying what they
+ * want, so the click carries the slot with it, through the same
+ * setProviderConfig('openai', ...) call the BackendSelector modal uses. No
+ * second mechanism, no LM-Studio-only path.
+ *
+ * The one thing that must not happen quietly is the built-in engine losing its
+ * slot without the user knowing. `adoptionReplacesBuiltinEngine` is what the
+ * banner asks so it can say so BEFORE the click, and name the way back.
+ */
+
+import { PROVIDER_PRESETS, type ProviderConfig } from '../api/providers/types'
+
+/** The shipped LM Studio preset: name and default URL come from one place. */
+export const LM_STUDIO_PRESET = PROVIDER_PRESETS.find((p) => p.id === 'lmstudio')!
+
+/** The part of the `openai` slot this decision reads. */
+export interface OpenAiSlotView {
+ enabled: boolean
+ baseUrl: string
+ managed?: boolean
+}
+
+/** Trailing slashes and case are not a different server. */
+function sameUrl(a: string, b: string): boolean {
+ const norm = (u: string) => (u || '').trim().replace(/\/+$/, '').toLowerCase()
+ return norm(a) === norm(b) && norm(a) !== ''
+}
+
+/** Is the slot already pointing at LM Studio (whether enabled or not). */
+export function slotPointsAtLmStudio(slot: OpenAiSlotView): boolean {
+ return !slot.managed && sameUrl(slot.baseUrl, LM_STUDIO_PRESET.baseUrl)
+}
+
+/**
+ * Would adopting LM Studio push the app-managed built-in engine out of the
+ * slot. The banner states this before the click; nothing else depends on it.
+ */
+export function adoptionReplacesBuiltinEngine(slot: OpenAiSlotView): boolean {
+ return slot.managed === true
+}
+
+/**
+ * What the `openai` slot has to become so the picker can list LM Studio
+ * models. `null` means it already can and the click has nothing to change.
+ */
+export function lmStudioSlotUpdate(slot: OpenAiSlotView): Partial<ProviderConfig> | null {
+ if (slotPointsAtLmStudio(slot)) {
+ // Right URL already. Only the enabled flag can still be in the way, and
+ // rewriting name/baseUrl here would throw away a user's own label for a
+ // second LM Studio port.
+ return slot.enabled ? null : { enabled: true }
+ }
+ return {
+ enabled: true,
+ name: LM_STUDIO_PRESET.name,
+ baseUrl: LM_STUDIO_PRESET.baseUrl,
+ isLocal: true,
+ // Must be cleared explicitly: left at true the model list would keep
+ // reading the bundled GGUFs while the URL points at LM Studio.
+ managed: false,
+ }
+}