import { createSubsystemLogger } from "openclaw/plugin-sdk/logging-core"; import type { ModelDefinitionConfig } from "openclaw/plugin-sdk/provider-model-shared"; import { SELF_HOSTED_DEFAULT_COST } from "openclaw/plugin-sdk/provider-setup"; import { fetchWithSsrFGuard, type SsrFPolicy } from "openclaw/plugin-sdk/ssrf-runtime"; import { LMSTUDIO_DEFAULT_LOAD_CONTEXT_LENGTH } from "./defaults.js"; import {
buildLmstudioModelName,
mapLmstudioWireEntry,
resolveLmstudioServerBase,
resolveLoadedContextWindow,
type LmstudioModelWire,
} from "./models.js"; import { buildLmstudioAuthHeaders } from "./runtime.js";
/** Discovers LLM models from LM Studio and maps them to OpenClaw model definitions. */
export async function discoverLmstudioModels(
params: DiscoverLmstudioModelsParams,
): Promise<ModelDefinitionConfig[]> { const fetched = await fetchLmstudioModels({
baseUrl: params.baseUrl,
apiKey: params.apiKey,
headers: params.headers,
fetchImpl: params.fetchImpl,
}); const quiet = params.quiet; if (!fetched.reachable) { if (!quiet) {
log.debug(`Failed to discover LM Studio models: ${String(fetched.error)}`);
} return [];
} if (fetched.status !== undefined && fetched.status >= 400) { if (!quiet) {
log.debug(`Failed to discover LM Studio models: ${fetched.status}`);
} return [];
} const models = fetched.models; if (models.length === 0) { if (!quiet) {
log.debug("No LM Studio models found on local instance");
} return [];
}
return models
.map((entry): ModelDefinitionConfig | null => { const base = mapLmstudioWireEntry(entry); if (!base) { returnnull;
} return {
id: base.id, // Runtime display: include format/vision/tool-use/loaded tags in the name.
name: buildLmstudioModelName(base),
reasoning: base.reasoning,
input: base.input,
cost: SELF_HOSTED_DEFAULT_COST,
compat: { supportsUsageInStreaming: true },
contextWindow: base.contextWindow,
contextTokens: base.contextTokens,
maxTokens: base.maxTokens,
};
})
.filter((entry): entry is ModelDefinitionConfig => entry !== null);
}
/** Ensures a model is loaded in LM Studio before first real inference/embedding call. */
export async function ensureLmstudioModelLoaded(params: {
baseUrl?: string;
apiKey?: string;
headers?: Record<string, string>;
ssrfPolicy?: SsrFPolicy;
modelKey: string;
requestedContextLength?: number;
timeoutMs?: number; /** Injectable fetch implementation; defaults to the global fetch. */
fetchImpl?: typeof fetch;
}): Promise<void> { const modelKey = params.modelKey.trim(); if (!modelKey) { thrownew Error("LM Studio model key is required");
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.