openclaw/src/agents/models-config.ts

148 lines
4.5 KiB
TypeScript
Raw Normal View History

2025-12-23 02:48:48 +01:00
import fs from "node:fs/promises";
import path from "node:path";
2026-01-30 03:15:10 +01:00
import { type OpenClawConfig, loadConfig } from "../config/config.js";
import { resolveOpenClawAgentDir } from "./agent-paths.js";
import {
normalizeProviders,
type ProviderConfig,
resolveImplicitBedrockProvider,
resolveImplicitCopilotProvider,
resolveImplicitProviders,
} from "./models-config.providers.js";
2025-12-23 02:48:48 +01:00
2026-01-30 03:15:10 +01:00
type ModelsConfig = NonNullable<OpenClawConfig["models"]>;
2025-12-23 02:48:48 +01:00
const DEFAULT_MODE: NonNullable<ModelsConfig["mode"]> = "merge";
2025-12-23 02:48:48 +01:00
function isRecord(value: unknown): value is Record<string, unknown> {
return Boolean(value && typeof value === "object" && !Array.isArray(value));
}
function mergeProviderModels(implicit: ProviderConfig, explicit: ProviderConfig): ProviderConfig {
const implicitModels = Array.isArray(implicit.models) ? implicit.models : [];
const explicitModels = Array.isArray(explicit.models) ? explicit.models : [];
if (implicitModels.length === 0) {
return { ...implicit, ...explicit };
}
const getId = (model: unknown): string => {
if (!model || typeof model !== "object") {
return "";
}
const id = (model as { id?: unknown }).id;
return typeof id === "string" ? id.trim() : "";
};
const seen = new Set(explicitModels.map(getId).filter(Boolean));
const mergedModels = [
...explicitModels,
...implicitModels.filter((model) => {
const id = getId(model);
if (!id) {
return false;
}
if (seen.has(id)) {
return false;
}
seen.add(id);
return true;
}),
];
return {
...implicit,
...explicit,
models: mergedModels,
};
}
function mergeProviders(params: {
implicit?: Record<string, ProviderConfig> | null;
explicit?: Record<string, ProviderConfig> | null;
}): Record<string, ProviderConfig> {
const out: Record<string, ProviderConfig> = params.implicit ? { ...params.implicit } : {};
for (const [key, explicit] of Object.entries(params.explicit ?? {})) {
const providerKey = key.trim();
if (!providerKey) {
continue;
}
const implicit = out[providerKey];
out[providerKey] = implicit ? mergeProviderModels(implicit, explicit) : explicit;
}
return out;
}
2025-12-23 03:00:04 +01:00
async function readJson(pathname: string): Promise<unknown> {
2025-12-23 02:48:48 +01:00
try {
const raw = await fs.readFile(pathname, "utf8");
return JSON.parse(raw) as unknown;
} catch {
return null;
}
}
2026-01-30 03:15:10 +01:00
export async function ensureOpenClawModelsJson(
config?: OpenClawConfig,
agentDirOverride?: string,
2025-12-23 02:48:48 +01:00
): Promise<{ agentDir: string; wrote: boolean }> {
const cfg = config ?? loadConfig();
2026-01-30 03:15:10 +01:00
const agentDir = agentDirOverride?.trim() ? agentDirOverride.trim() : resolveOpenClawAgentDir();
const explicitProviders = cfg.models?.providers ?? {};
feat: Add Ollama provider with automatic model discovery (#1606) * feat: Add Ollama provider with automatic model discovery - Add Ollama provider builder with automatic model detection - Discover available models from local Ollama instance via /api/tags API - Make resolveImplicitProviders async to support dynamic model discovery - Add comprehensive Ollama documentation with setup and usage guide - Add tests for Ollama provider integration - Update provider index and model providers documentation Closes #1531 * fix: Correct Ollama provider type definitions and error handling - Fix input property type to match ModelDefinitionConfig - Import ModelDefinitionConfig type properly - Fix error template literal to use String() for type safety - Simplify return type signature of discoverOllamaModels * fix: Suppress unhandled promise warnings from ensureClawdbotModelsJson in tests - Cast unused promise returns to 'unknown' to suppress TypeScript warnings - Tests that don't await the promise are intentionally not awaiting it - This fixes the failing test suite caused by unawaited async calls * fix: Skip Ollama model discovery during tests - Check for VITEST or NODE_ENV=test before making HTTP requests - Prevents test timeouts and hangs from network calls - Ollama discovery will still work in production/normal usage * fix: Set VITEST environment variable in test setup - Ensures Ollama discovery is skipped in all test runs - Prevents network calls during tests that could cause timeouts * test: Temporarily skip Ollama provider tests to diagnose CI failures * fix: Make Ollama provider opt-in to avoid breaking existing tests **Root Cause:** The Ollama provider was being added to ALL configurations by default (with a fallback API key of 'ollama-local'), which broke tests that expected NO providers when no API keys were configured. **Solution:** - Removed the default fallback API key for Ollama - Ollama provider now requires explicit configuration via: - OLLAMA_API_KEY environment variable, OR - Ollama profile in auth store - Updated documentation to reflect the explicit configuration requirement - Added a test to verify Ollama is not added by default This fixes all 4 failing test suites: - checks (node, test, pnpm test) - checks (bun, test, bunx vitest run) - checks-windows (node, test, pnpm test) - checks-macos (test, pnpm test) Closes #1531
2026-01-24 22:38:52 +00:00
const implicitProviders = await resolveImplicitProviders({ agentDir });
const providers: Record<string, ProviderConfig> = mergeProviders({
implicit: implicitProviders,
explicit: explicitProviders,
});
const implicitBedrock = await resolveImplicitBedrockProvider({ agentDir, config: cfg });
if (implicitBedrock) {
const existing = providers["amazon-bedrock"];
providers["amazon-bedrock"] = existing
? mergeProviderModels(implicitBedrock, existing)
: implicitBedrock;
}
const implicitCopilot = await resolveImplicitCopilotProvider({ agentDir });
if (implicitCopilot && !providers["github-copilot"]) {
providers["github-copilot"] = implicitCopilot;
}
if (Object.keys(providers).length === 0) {
return { agentDir, wrote: false };
2025-12-23 02:48:48 +01:00
}
const mode = cfg.models?.mode ?? DEFAULT_MODE;
const targetPath = path.join(agentDir, "models.json");
let mergedProviders = providers;
let existingRaw = "";
if (mode === "merge") {
const existing = await readJson(targetPath);
if (isRecord(existing) && isRecord(existing.providers)) {
const existingProviders = existing.providers as Record<
string,
NonNullable<ModelsConfig["providers"]>[string]
>;
mergedProviders = { ...existingProviders, ...providers };
}
}
const normalizedProviders = normalizeProviders({
providers: mergedProviders,
agentDir,
});
const next = `${JSON.stringify({ providers: normalizedProviders }, null, 2)}\n`;
2025-12-23 02:48:48 +01:00
try {
existingRaw = await fs.readFile(targetPath, "utf8");
} catch {
existingRaw = "";
}
if (existingRaw === next) {
return { agentDir, wrote: false };
}
await fs.mkdir(agentDir, { recursive: true, mode: 0o700 });
await fs.writeFile(targetPath, next, { mode: 0o600 });
return { agentDir, wrote: true };
}