mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-18 20:07:26 +00:00
Move dynamic import of @mariozechner/pi-coding-agent into the try/catch so transient module resolution errors don't poison the model catalog cache with a rejected promise. This previously caused Discord/Telegram handlers and heartbeat to fail until process restart if the import failed once.
88 lines
2.9 KiB
TypeScript
88 lines
2.9 KiB
TypeScript
import { type ClawdbotConfig, loadConfig } from "../config/config.js";
|
|
import { resolveClawdbotAgentDir } from "./agent-paths.js";
|
|
import { ensureClawdbotModelsJson } from "./models-config.js";
|
|
|
|
export type ModelCatalogEntry = {
|
|
id: string;
|
|
name: string;
|
|
provider: string;
|
|
contextWindow?: number;
|
|
reasoning?: boolean;
|
|
};
|
|
|
|
type DiscoveredModel = {
|
|
id: string;
|
|
name?: string;
|
|
provider: string;
|
|
contextWindow?: number;
|
|
reasoning?: boolean;
|
|
};
|
|
|
|
let modelCatalogPromise: Promise<ModelCatalogEntry[]> | null = null;
|
|
|
|
export function resetModelCatalogCacheForTest() {
|
|
modelCatalogPromise = null;
|
|
}
|
|
|
|
export async function loadModelCatalog(params?: {
|
|
config?: ClawdbotConfig;
|
|
useCache?: boolean;
|
|
}): Promise<ModelCatalogEntry[]> {
|
|
if (params?.useCache === false) {
|
|
modelCatalogPromise = null;
|
|
}
|
|
if (modelCatalogPromise) return modelCatalogPromise;
|
|
|
|
modelCatalogPromise = (async () => {
|
|
try {
|
|
// 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 agentDir = resolveClawdbotAgentDir();
|
|
const authStorage = piSdk.discoverAuthStorage(agentDir);
|
|
const registry = piSdk.discoverModels(authStorage, agentDir) as
|
|
| {
|
|
getAll: () => Array<DiscoveredModel>;
|
|
}
|
|
| Array<DiscoveredModel>;
|
|
const entries = Array.isArray(registry) ? registry : registry.getAll();
|
|
for (const entry of entries) {
|
|
const id = String(entry?.id ?? "").trim();
|
|
if (!id) continue;
|
|
const provider = String(entry?.provider ?? "").trim();
|
|
if (!provider) continue;
|
|
const name = String(entry?.name ?? id).trim() || id;
|
|
const contextWindow =
|
|
typeof entry?.contextWindow === "number" && entry.contextWindow > 0
|
|
? entry.contextWindow
|
|
: undefined;
|
|
const reasoning = typeof entry?.reasoning === "boolean" ? entry.reasoning : undefined;
|
|
models.push({ id, name, provider, contextWindow, reasoning });
|
|
}
|
|
|
|
if (models.length === 0) {
|
|
// If we found nothing, don't cache this result so we can try again.
|
|
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 {
|
|
// Don't poison the cache on transient dependency/filesystem issues.
|
|
modelCatalogPromise = null;
|
|
return [];
|
|
}
|
|
})();
|
|
|
|
return modelCatalogPromise;
|
|
}
|