Skip to content

Commit 146b39d

Browse files
committed
fix(lmstudio): preserve caches on cancellation and cover discovery contracts
1 parent 3a2c435 commit 146b39d

7 files changed

Lines changed: 133 additions & 31 deletions

File tree

‎docs/providers/lm-studio-http.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ Zoo Code discovers models without `@lmstudio/sdk`. It tries these endpoints in o
88
| `GET /api/v0/models` (LM Studio 0.3.6+) | LLM/VLM IDs, vision inferred from `vlm`, maximum context |
99
| `GET /v1/models` | IDs with Zoo Code's existing default metadata |
1010

11-
Only HTTP 404 or 405 triggers the next discovery endpoint. Authentication errors, rate limits, network failures, cancellation, and invalid response bodies do not trigger fallback. Older APIs do not expose the native model-load operation; chat/JIT remains responsible for loading on those servers.
11+
HTTP 404 or 405 starts the next discovery endpoint. HTTP 200 with a body of `{"error": "<message>"}` also starts it. Authentication errors, network failures, timeouts, cancellation and other invalid bodies do not.
12+
13+
All three discovery requests share a 15-second deadline. Explicit model loading has a separate 10-minute timeout. A failed refresh clears cached loaded-detail flags and instance aliases for that server; caller cancellation preserves them.
14+
Older APIs do not expose the native model-load operation; chat/JIT remains responsible for loading on those servers.
1215

1316
Sources: [native API changelog](https://lmstudio.ai/docs/developer/api-changelog), [legacy API reference](https://lmstudio.ai/docs/developer/rest/endpoints).
1417

‎src/api/providers/__tests__/lmstudio.spec.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ describe("LmStudioHandler", () => {
9595
mockGetModelsFromCache.mockReturnValue(undefined)
9696
})
9797

98+
it.each([undefined, "", "http://localhost:1234/"])("normalizes handler URL %s", async (lmStudioBaseUrl) => {
99+
const normalized = new LmStudioHandler({ ...mockOptions, lmStudioBaseUrl })
100+
mockGetModels.mockResolvedValue({})
101+
await normalized.ensureModelFetched()
102+
normalized.getModel()
103+
const scope = { provider: providerIdentifiers.lmstudio, baseUrl: "http://localhost:1234" }
104+
expect(mockGetModels).toHaveBeenCalledWith(scope, expect.anything())
105+
expect(mockGetModelsFromCache).toHaveBeenCalledWith(scope, normalized)
106+
})
107+
98108
describe("constructor", () => {
99109
it("should initialize with provided options", () => {
100110
expect(handler).toBeInstanceOf(LmStudioHandler)

‎src/api/providers/fetchers/__tests__/lmstudio.test.ts‎

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ import {
99
hasLoadedFullDetails,
1010
LM_STUDIO_MODEL_LOAD_TIMEOUT_MS,
1111
} from "../lmstudio"
12+
import { MODEL_CATALOG_FETCH_TIMEOUT_MS } from "../modelCache"
1213
import type { ApiHandler } from "../../../index"
1314

1415
import nativeFixture from "./fixtures/lmstudio-model-details.json"
1516

1617
const mockFlushModels = vi.hoisted(() => vi.fn())
17-
vi.mock("../modelCache", () => ({ flushModels: mockFlushModels, MODEL_CATALOG_FETCH_TIMEOUT_MS: 15_000 }))
18+
vi.mock("../modelCache", async (importOriginal) => ({
19+
...(await importOriginal<typeof import("../modelCache")>()),
20+
flushModels: mockFlushModels,
21+
}))
1822

1923
const get = vi.spyOn(axios, "get")
2024
const post = vi.spyOn(axios, "post")
@@ -75,6 +79,66 @@ describe("LM Studio HTTP discovery", () => {
7579
},
7680
)
7781

82+
it.each([{ error: 42 }, { unexpected: true }])("rejects an invalid v0 body %j", async (data) => {
83+
get.mockRejectedValueOnce(httpError(404)).mockResolvedValueOnce({ data })
84+
expect(await getLMStudioModels(baseUrl)).toEqual({})
85+
expect(get).toHaveBeenCalledTimes(2)
86+
})
87+
88+
it("reports an OpenAI error body after HTTP fallback failures", async () => {
89+
get.mockRejectedValueOnce(httpError(404))
90+
.mockRejectedValueOnce(httpError(405))
91+
.mockResolvedValueOnce({ data: { error: "OpenAI endpoint unavailable" } })
92+
expect(await getLMStudioModels(baseUrl)).toEqual({})
93+
expect(console.error).toHaveBeenCalledWith(expect.any(String), "OpenAI endpoint unavailable")
94+
})
95+
96+
it("reports discovery deadline expiration as a timeout", async () => {
97+
const deadline = new AbortController()
98+
const timeout = vi.spyOn(AbortSignal, "timeout").mockReturnValue(deadline.signal)
99+
try {
100+
get.mockImplementationOnce(async () => {
101+
deadline.abort(new DOMException("deadline", "TimeoutError"))
102+
throw new AxiosError("cancelled", "ERR_CANCELED")
103+
})
104+
expect(await getLMStudioModels(baseUrl)).toEqual({})
105+
expect(console.error).toHaveBeenCalledWith(expect.any(String), "LM Studio model discovery timed out")
106+
} finally {
107+
timeout.mockRestore()
108+
}
109+
})
110+
111+
it("retains the first server error when every endpoint fails", async () => {
112+
get.mockResolvedValueOnce({ data: { error: "Invalid API key" } })
113+
.mockResolvedValueOnce({ data: { error: "second error" } })
114+
.mockResolvedValueOnce({ data: { error: "last error" } })
115+
expect(await getLMStudioModels(baseUrl)).toEqual({})
116+
expect(get).toHaveBeenCalledTimes(3)
117+
expect(console.error).toHaveBeenCalledWith(expect.any(String), "Invalid API key")
118+
})
119+
120+
it("preserves a healthy refresh when an older flight is cancelled", async () => {
121+
const controller = new AbortController()
122+
let rejectFlight: (error: Error) => void = () => {
123+
throw new Error("flight not started")
124+
}
125+
get.mockImplementationOnce(
126+
() =>
127+
new Promise((_resolve, reject) => {
128+
rejectFlight = reject
129+
}),
130+
)
131+
const cancelled = getLMStudioModels(baseUrl, { signal: controller.signal })
132+
const assertion = expect(cancelled).rejects.toMatchObject({ name: "AbortError" })
133+
get.mockResolvedValueOnce(nativeResponse())
134+
await getLMStudioModels(baseUrl)
135+
controller.abort()
136+
rejectFlight(new Error("cancelled"))
137+
await assertion
138+
expect(hasLoadedFullDetails(modelKey, baseUrl)).toBe(true)
139+
expect(getLMStudioModelAlias("smollm2-135m-instruct", baseUrl)).toBeDefined()
140+
})
141+
78142
it("prefers a valid native catalog over an accompanying error string", async () => {
79143
get.mockResolvedValueOnce({ data: { ...nativeResponse().data, error: "ignored" } })
80144
expect(Object.keys(await getLMStudioModels(baseUrl))).toEqual([modelKey])
@@ -333,7 +397,7 @@ describe("LM Studio explicit loading", () => {
333397
const pending = forceFullModelDetailsLoad(baseUrl, modelKey, handler)
334398
await vi.waitFor(() => expect(requestSignal).toBe(deadline.signal))
335399
expect(timeout).toHaveBeenLastCalledWith(
336-
phase === "discovery" ? 15_000 : LM_STUDIO_MODEL_LOAD_TIMEOUT_MS,
400+
phase === "discovery" ? MODEL_CATALOG_FETCH_TIMEOUT_MS : LM_STUDIO_MODEL_LOAD_TIMEOUT_MS,
337401
)
338402
deadline.abort()
339403
await pending

‎src/api/providers/fetchers/lmstudio.ts‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { z } from "zod"
33

44
import { type ModelInfo, lMStudioDefaultModelInfo, providerIdentifiers } from "@roo-code/types"
55

6-
import { mergeAbortSignalAndTimeout, throwIfAborted } from "../utils/abort-signal"
6+
import { isTimeoutAbort, mergeAbortSignalAndTimeout, throwIfAborted } from "../utils/abort-signal"
77
import { getLoggableOrigin } from "../utils/endpoint-origin"
88
import { normalizeLMStudioBaseUrl } from "../utils/lmstudio"
99

@@ -61,6 +61,7 @@ const errorBodySchema = z.object({ error: z.string() })
6161

6262
async function discoverModels(baseUrl: string, externalSignal?: AbortSignal) {
6363
const signal = mergeAbortSignalAndTimeout(externalSignal, MODEL_CATALOG_FETCH_TIMEOUT_MS)
64+
let firstServerError: Error | undefined
6465
try {
6566
const { data } = await axios.get<unknown>(`${baseUrl}/api/v1/models`, { signal })
6667
const parsedList = nativeListSchema.safeParse(data)
@@ -71,8 +72,11 @@ async function discoverModels(baseUrl: string, externalSignal?: AbortSignal) {
7172
})
7273
return { version: "v1" as const, models }
7374
}
74-
if (!errorBodySchema.safeParse(data).success) throw parsedList.error
75+
const serverError = errorBodySchema.safeParse(data)
76+
if (!serverError.success) throw parsedList.error
77+
firstServerError ??= new Error(serverError.data.error)
7578
} catch (error) {
79+
if (isTimeoutAbort(signal)) throw new Error("LM Studio model discovery timed out")
7680
throwIfAborted(signal)
7781
if (!isUnsupportedEndpoint(error)) throw error
7882
}
@@ -81,14 +85,28 @@ async function discoverModels(baseUrl: string, externalSignal?: AbortSignal) {
8185
const { data } = await axios.get<unknown>(`${baseUrl}/api/v0/models`, { signal })
8286
const parsedList = legacyListSchema.safeParse(data)
8387
if (parsedList.success) return { version: "v0" as const, models: parsedList.data.data }
84-
if (!errorBodySchema.safeParse(data).success) throw parsedList.error
88+
const serverError = errorBodySchema.safeParse(data)
89+
if (!serverError.success) throw parsedList.error
90+
firstServerError ??= new Error(serverError.data.error)
8591
} catch (error) {
92+
if (isTimeoutAbort(signal)) throw new Error("LM Studio model discovery timed out")
8693
throwIfAborted(signal)
8794
if (!isUnsupportedEndpoint(error)) throw error
8895
}
8996

90-
const { data } = await axios.get<unknown>(`${baseUrl}/v1/models`, { signal })
91-
return { version: "openai" as const, models: openAIListSchema.parse(data).data }
97+
let data: unknown
98+
try {
99+
;({ data } = await axios.get<unknown>(`${baseUrl}/v1/models`, { signal }))
100+
} catch (error) {
101+
if (isTimeoutAbort(signal)) throw new Error("LM Studio model discovery timed out")
102+
throwIfAborted(signal)
103+
// Keep the earlier server message only when this endpoint is also unsupported.
104+
throw isUnsupportedEndpoint(error) ? (firstServerError ?? error) : error
105+
}
106+
const parsed = openAIListSchema.safeParse(data)
107+
if (parsed.success) return { version: "openai" as const, models: parsed.data.data }
108+
const serverError = errorBodySchema.safeParse(data)
109+
throw firstServerError ?? (serverError.success ? new Error(serverError.data.error) : parsed.error)
92110
}
93111

94112
// Prefer the instance explicitly selected by the user; otherwise use server order.
@@ -118,7 +136,7 @@ function logModelError(action: string, error: unknown, baseUrl: string) {
118136
}
119137

120138
export const forceFullModelDetailsLoad = async (
121-
baseUrl: string,
139+
baseUrl: string | undefined,
122140
modelId: string,
123141
handler: ApiHandler,
124142
): Promise<void> => {
@@ -204,9 +222,9 @@ export async function getLMStudioModels(
204222
modelsWithLoadedDetails.set(baseUrl, loadedDetails)
205223
instanceAliases.set(baseUrl, aliases)
206224
} catch (error) {
225+
throwIfAborted(opts?.signal)
207226
modelsWithLoadedDetails.delete(baseUrl)
208227
instanceAliases.delete(baseUrl)
209-
throwIfAborted(opts?.signal)
210228
logModelError("fetching", error, baseUrl)
211229
}
212230
return models

‎src/core/webview/ClineProvider.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ export class ClineProvider
667667
)
668668
) {
669669
await forceFullModelDetailsLoad(
670-
cline.apiConfiguration.lmStudioBaseUrl ?? "http://localhost:1234",
670+
cline.apiConfiguration.lmStudioBaseUrl,
671671
cline.apiConfiguration.lmStudioModelId!,
672672
buildApiHandler({ ...cline.apiConfiguration, apiProvider: providerIdentifiers.lmstudio }),
673673
)

‎src/core/webview/__tests__/ClineProvider.spec.ts‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,19 +1057,23 @@ describe("ClineProvider", () => {
10571057
expect(forceFullModelDetailsLoad).toHaveBeenCalledWith("http://localhost:1234", "test-model", expect.anything())
10581058
})
10591059

1060-
test("does not reload full model details when the LM Studio model is already loaded", async () => {
1061-
vi.mocked(hasLoadedFullDetails).mockReturnValue(true)
1060+
test.each([undefined, "http://localhost:1234"])(
1061+
"does not reload full model details already loaded at %s",
1062+
async (lmStudioBaseUrl) => {
1063+
vi.mocked(hasLoadedFullDetails).mockReturnValue(true)
10621064

1063-
await provider.performPreparationTasks({
1064-
apiConfiguration: {
1065-
apiProvider: providerIdentifiers.lmstudio,
1066-
lmStudioBaseUrl: "http://localhost:1234",
1067-
lmStudioModelId: "test-model",
1068-
},
1069-
} as Task)
1065+
await provider.performPreparationTasks({
1066+
apiConfiguration: {
1067+
apiProvider: providerIdentifiers.lmstudio,
1068+
lmStudioBaseUrl,
1069+
lmStudioModelId: "test-model",
1070+
},
1071+
} as Task)
10701072

1071-
expect(forceFullModelDetailsLoad).not.toHaveBeenCalled()
1072-
})
1073+
expect(hasLoadedFullDetails).toHaveBeenCalledWith("test-model", lmStudioBaseUrl)
1074+
expect(forceFullModelDetailsLoad).not.toHaveBeenCalled()
1075+
},
1076+
)
10731077

10741078
test("resolveWebviewView hydrates the saved terminalProfile into the process-wide Terminal state", async () => {
10751079
const setTerminalProfileSpy = vi.spyOn(Terminal, "setTerminalProfile").mockImplementation(() => {})

‎src/core/webview/__tests__/webviewMessageHandler.spec.ts‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -913,15 +913,18 @@ describe("webviewMessageHandler - requestLmStudioModels", () => {
913913
})
914914
})
915915

916-
it.each([undefined, ""])("uses the default server cache key for persisted URL %s", async (lmStudioBaseUrl) => {
917-
mockClineProvider.getState = vi.fn().mockResolvedValue({ apiConfiguration: { lmStudioBaseUrl } })
918-
mockGetModels.mockResolvedValue({})
919-
await webviewMessageHandler(mockClineProvider, { type: "requestLmStudioModels" })
920-
expect(mockGetModels).toHaveBeenCalledWith(
921-
{ provider: providerIdentifiers.lmstudio, baseUrl: "http://localhost:1234" },
922-
expect.anything(),
923-
)
924-
})
916+
it.each([undefined, "", "http://localhost:1234/"])(
917+
"uses the default server cache key for persisted URL %s",
918+
async (lmStudioBaseUrl) => {
919+
mockClineProvider.getState = vi.fn().mockResolvedValue({ apiConfiguration: { lmStudioBaseUrl } })
920+
mockGetModels.mockResolvedValue({})
921+
await webviewMessageHandler(mockClineProvider, { type: "requestLmStudioModels" })
922+
expect(mockGetModels).toHaveBeenCalledWith(
923+
{ provider: providerIdentifiers.lmstudio, baseUrl: "http://localhost:1234" },
924+
expect.anything(),
925+
)
926+
},
927+
)
925928

926929
it("prefers the request payload base URL over persisted settings", async () => {
927930
mockGetLMStudioModels.mockResolvedValue({})

0 commit comments

Comments
 (0)