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
+3 -9
View File
@@ -209,11 +209,10 @@ export function resolveLoadedContextWindow(
let contextWindow: number | null = null;
for (const instance of loadedInstances) {
// Discovery payload is external JSON, so tolerate malformed entries.
const length = instance?.config?.context_length;
if (length === undefined || !Number.isFinite(length) || length <= 0) {
const normalized = asPositiveSafeInteger(instance?.config?.context_length);
if (normalized === undefined) {
continue;
}
const normalized = Math.floor(length);
contextWindow = contextWindow === null ? normalized : Math.max(contextWindow, normalized);
}
return contextWindow;
@@ -463,12 +462,7 @@ export function mapLmstudioWireEntry(entry: LmstudioModelWire): LmstudioModelBas
return null;
}
const loadedContextWindow = resolveLoadedContextWindow(entry);
const advertisedContextWindow =
entry.max_context_length !== undefined &&
Number.isFinite(entry.max_context_length) &&
entry.max_context_length > 0
? Math.floor(entry.max_context_length)
: null;
const advertisedContextWindow = asPositiveSafeInteger(entry.max_context_length) ?? null;
const contextWindow = advertisedContextWindow ?? SELF_HOSTED_DEFAULT_CONTEXT_WINDOW;
// Keep native/advertised context window metadata in catalog, but use a practical
// default target for model loading unless callers explicitly override it.