mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 17:48:26 +00:00
fix: model catalog cache + TUI editor ctor (#1326) (thanks @dougvk)
This commit is contained in:
@@ -18,10 +18,22 @@ type DiscoveredModel = {
|
||||
reasoning?: boolean;
|
||||
};
|
||||
|
||||
type PiSdkModule = typeof import("@mariozechner/pi-coding-agent");
|
||||
|
||||
let modelCatalogPromise: Promise<ModelCatalogEntry[]> | null = null;
|
||||
let hasLoggedModelCatalogError = false;
|
||||
const defaultImportPiSdk = () => import("@mariozechner/pi-coding-agent");
|
||||
let importPiSdk = defaultImportPiSdk;
|
||||
|
||||
export function resetModelCatalogCacheForTest() {
|
||||
modelCatalogPromise = null;
|
||||
hasLoggedModelCatalogError = false;
|
||||
importPiSdk = defaultImportPiSdk;
|
||||
}
|
||||
|
||||
// Test-only escape hatch: allow mocking the dynamic import to simulate transient failures.
|
||||
export function __setModelCatalogImportForTest(loader?: () => Promise<PiSdkModule>) {
|
||||
importPiSdk = loader ?? defaultImportPiSdk;
|
||||
}
|
||||
|
||||
export async function loadModelCatalog(params?: {
|
||||
@@ -34,16 +46,21 @@ export async function loadModelCatalog(params?: {
|
||||
if (modelCatalogPromise) return modelCatalogPromise;
|
||||
|
||||
modelCatalogPromise = (async () => {
|
||||
const models: ModelCatalogEntry[] = [];
|
||||
const sortModels = (entries: ModelCatalogEntry[]) =>
|
||||
entries.sort((a, b) => {
|
||||
const p = a.provider.localeCompare(b.provider);
|
||||
if (p !== 0) return p;
|
||||
return a.name.localeCompare(b.name);
|
||||
});
|
||||
try {
|
||||
const cfg = params?.config ?? loadConfig();
|
||||
await ensureClawdbotModelsJson(cfg);
|
||||
// IMPORTANT: keep the dynamic import *inside* the try/catch.
|
||||
// If this fails once (e.g. during a pnpm install that temporarily swaps node_modules),
|
||||
// we must not poison the cache with a rejected promise (otherwise all channel handlers
|
||||
// will keep failing until restart).
|
||||
const piSdk = await import("@mariozechner/pi-coding-agent");
|
||||
|
||||
const models: ModelCatalogEntry[] = [];
|
||||
const cfg = params?.config ?? loadConfig();
|
||||
await ensureClawdbotModelsJson(cfg);
|
||||
const piSdk = await importPiSdk();
|
||||
const agentDir = resolveClawdbotAgentDir();
|
||||
const authStorage = piSdk.discoverAuthStorage(agentDir);
|
||||
const registry = piSdk.discoverModels(authStorage, agentDir) as
|
||||
@@ -71,14 +88,17 @@ export async function loadModelCatalog(params?: {
|
||||
modelCatalogPromise = null;
|
||||
}
|
||||
|
||||
return models.sort((a, b) => {
|
||||
const p = a.provider.localeCompare(b.provider);
|
||||
if (p !== 0) return p;
|
||||
return a.name.localeCompare(b.name);
|
||||
});
|
||||
} catch {
|
||||
return sortModels(models);
|
||||
} catch (error) {
|
||||
if (!hasLoggedModelCatalogError) {
|
||||
hasLoggedModelCatalogError = true;
|
||||
console.warn(`[model-catalog] Failed to load model catalog: ${String(error)}`);
|
||||
}
|
||||
// Don't poison the cache on transient dependency/filesystem issues.
|
||||
modelCatalogPromise = null;
|
||||
if (models.length > 0) {
|
||||
return sortModels(models);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user