fix: validate lmstudio discovered context lengths

This commit is contained in:
Peter Steinberger
2026-05-28 18:10:55 -04:00
parent 8e806e9125
commit 80f7e36ddc
3 changed files with 42 additions and 21 deletions

View File

@@ -10,6 +10,7 @@ import {
fetchLmstudioModels,
} from "./models.fetch.js";
import {
mapLmstudioWireEntry,
normalizeLmstudioConfiguredCatalogEntry,
normalizeLmstudioProviderConfig,
resolveLmstudioInferenceBase,
@@ -160,6 +161,23 @@ describe("lmstudio-models", () => {
});
});
it("drops malformed discovered context metadata", () => {
const model = mapLmstudioWireEntry({
type: "llm",
key: "bad-context",
max_context_length: 32768.5,
loaded_instances: [{ id: "loaded", config: { context_length: Number.POSITIVE_INFINITY } }],
});
expect(model).toMatchObject({
id: "bad-context",
contextWindow: SELF_HOSTED_DEFAULT_CONTEXT_WINDOW,
contextTokens: LMSTUDIO_DEFAULT_LOAD_CONTEXT_LENGTH,
maxTokens: SELF_HOSTED_DEFAULT_MAX_TOKENS,
loaded: false,
});
});
it("resolves reasoning capability for supported and unsupported options", () => {
expect(resolveLmstudioReasoningCapability({ capabilities: undefined })).toBe(false);
expect(
@@ -499,6 +517,24 @@ describe("lmstudio-models", () => {
expectLoadContextLength(fetchMock, 8192);
});
it("omits malformed context lengths before loading models", async () => {
const fetchMock = createModelLoadFetchMock({
loadedContextLength: 4096.5,
maxContextLength: 32768.5,
});
vi.stubGlobal("fetch", asFetch(fetchMock));
await expect(
ensureLmstudioModelLoaded({
baseUrl: "http://localhost:1234/v1",
modelKey: "qwen3-8b-instruct",
requestedContextLength: 8192.5,
}),
).resolves.toBeUndefined();
expectLoadContextLength(fetchMock, LMSTUDIO_DEFAULT_LOAD_CONTEXT_LENGTH);
});
it("throws when model discovery fails", async () => {
const fetchMock = vi.fn(async () => ({
ok: false,