Files
openclaw/extensions/ollama/src/config-compat.ts
Peter Steinberger 8cb53c7b55 perf(doctor): keep bundled doctor contract closures dependency-light (#120698)
* perf(doctor): keep bundled doctor contract closures dependency-light

Doctor contract enumeration cold-loads each plugin's doctor-contract-api
closure via jiti, so a static value import of openclaw/plugin-sdk/runtime-doctor
pulled the state-db/kysely graph (~4.3s per closure) into
listPluginDoctorLegacyConfigRules / listPluginDoctorStateMigrationEntries.

- migrate all light doctor-contract closures (66 files) to the
  dependency-light openclaw/plugin-sdk/runtime-doctor-migrations subpath
- voice-call: load detect/repairOpenClawStateDatabaseSchema* lazily inside
  the migration bodies; keep only a type-only static runtime-doctor import
- matrix: split pure credential record shapes/normalizers into
  credentials-state.ts so the doctor closure no longer imports the sync
  plugin-state store through credentials-read
- guard: doctor-contract-closure-guard.test.ts now forbids static value
  imports of runtime-doctor in closures alongside agent-runtime

* fix(matrix): keep credential revocation record type module-local

Knip production scan flags the export as consumer-less; the type is only
referenced by the exported union and revocation guard signature.
2026-08-08 17:51:31 -07:00

99 lines
3.0 KiB
TypeScript

// Ollama helper module supports config compat behavior.
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
import { asObjectRecord } from "openclaw/plugin-sdk/runtime-doctor-migrations";
import { OLLAMA_CLOUD_BASE_URL, OLLAMA_CLOUD_PROVIDER_ID } from "./defaults.js";
type LegacyConfigRule = {
path: Array<string | number>;
message: string;
match: (value: unknown) => boolean;
};
function isRetiredOllamaCloudBaseUrl(value: unknown): value is string {
if (typeof value !== "string" || !value.trim()) {
return false;
}
try {
return new URL(value.trim()).hostname.toLowerCase() === "ai.ollama.com";
} catch {
return false;
}
}
function findRetiredOllamaCloudBaseUrl(provider: unknown): { key: "baseUrl" | "baseURL" } | null {
const record = asObjectRecord(provider);
if (!record) {
return null;
}
if (isRetiredOllamaCloudBaseUrl(record.baseUrl)) {
return { key: "baseUrl" };
}
if (isRetiredOllamaCloudBaseUrl(record.baseURL)) {
return { key: "baseURL" };
}
return null;
}
export const legacyConfigRules: LegacyConfigRule[] = [
{
path: ["models", "providers", OLLAMA_CLOUD_PROVIDER_ID],
message:
'models.providers.ollama-cloud.baseUrl="https://ai.ollama.com" is retired; use "https://ollama.com". Run "openclaw doctor --fix".',
match: (value) => findRetiredOllamaCloudBaseUrl(value) !== null,
},
];
function migrateOllamaCloudRetiredBaseUrl(config: OpenClawConfig): {
config: OpenClawConfig;
changes: string[];
} | null {
const provider = config.models?.providers?.[OLLAMA_CLOUD_PROVIDER_ID];
const retired = findRetiredOllamaCloudBaseUrl(provider);
if (!retired) {
return null;
}
const nextConfig = structuredClone(config);
const nextModels = asObjectRecord(nextConfig.models) ?? {};
nextConfig.models = nextModels as OpenClawConfig["models"];
const nextProviders = asObjectRecord(nextModels.providers) ?? {};
nextModels.providers = nextProviders;
const nextProvider = asObjectRecord(nextProviders[OLLAMA_CLOUD_PROVIDER_ID]) ?? {};
nextProviders[OLLAMA_CLOUD_PROVIDER_ID] = nextProvider;
const canonicalBaseUrl = nextProvider.baseUrl;
if (
retired.key === "baseURL" &&
typeof canonicalBaseUrl === "string" &&
canonicalBaseUrl.trim() &&
!isRetiredOllamaCloudBaseUrl(canonicalBaseUrl)
) {
delete nextProvider.baseURL;
return {
config: nextConfig,
changes: [
"Removed retired models.providers.ollama-cloud.baseURL while preserving models.providers.ollama-cloud.baseUrl.",
],
};
}
nextProvider.baseUrl = OLLAMA_CLOUD_BASE_URL;
if (retired.key === "baseURL") {
delete nextProvider.baseURL;
}
return {
config: nextConfig,
changes: [
`Updated models.providers.ollama-cloud.${retired.key} from the retired Ollama Cloud endpoint to ${OLLAMA_CLOUD_BASE_URL}.`,
],
};
}
export function normalizeCompatibilityConfig({ cfg }: { cfg: OpenClawConfig }): {
config: OpenClawConfig;
changes: string[];
} {
return migrateOllamaCloudRetiredBaseUrl(cfg) ?? { config: cfg, changes: [] };
}