58 lines
1.8 KiB
JavaScript
58 lines
1.8 KiB
JavaScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { pathToFileURL } from 'node:url';
|
|
import { importMemind, resolveMemindLib } from './lib-path.mjs';
|
|
|
|
const ADMIN_METHODS = [
|
|
'getVisionSettings',
|
|
'setVisionKey',
|
|
'clearVisionKey',
|
|
'listExecutorBindings',
|
|
'setExecutorBinding',
|
|
'listExecutorRuntimeConfigs',
|
|
'listExecutorLaunchPlans',
|
|
'listExecutorLaunchStates',
|
|
'launchExecutor',
|
|
'stopExecutor',
|
|
'restartExecutor',
|
|
];
|
|
|
|
function hasAdminMethods(createFactory) {
|
|
if (typeof createFactory !== 'function') return false;
|
|
const probe = createFactory(null, {});
|
|
return ADMIN_METHODS.every((method) => typeof probe?.[method] === 'function');
|
|
}
|
|
|
|
async function importLlmProvidersModule(modulePath) {
|
|
return import(pathToFileURL(modulePath).href);
|
|
}
|
|
|
|
export async function loadCreateLlmProviderService() {
|
|
const primary = await importMemind('llm-providers.mjs');
|
|
if (hasAdminMethods(primary.createLlmProviderService)) {
|
|
return primary.createLlmProviderService;
|
|
}
|
|
|
|
const libRoot = resolveMemindLib();
|
|
const fallbackCandidates = [
|
|
path.join(libRoot, 'llm-providers.mjs.bak-20260625-221206'),
|
|
path.join(libRoot, 'llm-providers.admin.mjs'),
|
|
];
|
|
|
|
for (const candidate of fallbackCandidates) {
|
|
if (!fs.existsSync(candidate)) continue;
|
|
const fallback = await importLlmProvidersModule(candidate);
|
|
if (hasAdminMethods(fallback.createLlmProviderService)) {
|
|
console.warn(
|
|
`[admin] Memind llm-providers.mjs 缺少管理端方法,已回退到 ${path.basename(candidate)}`,
|
|
);
|
|
return fallback.createLlmProviderService;
|
|
}
|
|
}
|
|
|
|
throw new Error(
|
|
'Memind llm-providers.mjs 缺少 getVisionSettings 等管理端方法。' +
|
|
'请更新 MEMIND_LIB_ROOT 下的 llm-providers.mjs,或保留 llm-providers.mjs.bak-20260625-221206 备份文件。',
|
|
);
|
|
}
|