* fix(ollama): inject num_ctx for OpenAI-compatible transport * fix(ollama): discover per-model context and preserve higher limits * fix(agents): prefer matching provider model for fallback limits * fix(types): require numeric token limits in provider model merge * fix(types): accept unknown payload in ollama num_ctx wrapper * fix(types): simplify ollama settled-result extraction * config(models): add provider flag for Ollama OpenAI num_ctx injection * config(schema): allow provider num_ctx injection flag * config(labels): label provider num_ctx injection flag * config(help): document provider num_ctx injection flag * agents(ollama): gate OpenAI num_ctx injection with provider config * tests(ollama): cover provider num_ctx injection flag behavior * docs(config): list provider num_ctx injection option * docs(ollama): document OpenAI num_ctx injection toggle * docs(config): clarify merge token-limit precedence * config(help): note merge uses higher model token limits * fix(ollama): cap /api/show discovery concurrency * fix(ollama): restrict num_ctx injection to OpenAI compat * tests(ollama): cover ipv6 and compat num_ctx gating * fix(ollama): detect remote compat endpoints for ollama-labeled providers * fix(ollama): cap per-model /api/show lookups to bound discovery load
200 lines
6.7 KiB
TypeScript
200 lines
6.7 KiB
TypeScript
import fs from "node:fs/promises";
|
|
import path from "node:path";
|
|
import { type OpenClawConfig, loadConfig } from "../config/config.js";
|
|
import { isRecord } from "../utils.js";
|
|
import { resolveOpenClawAgentDir } from "./agent-paths.js";
|
|
import {
|
|
normalizeProviders,
|
|
type ProviderConfig,
|
|
resolveImplicitBedrockProvider,
|
|
resolveImplicitCopilotProvider,
|
|
resolveImplicitProviders,
|
|
} from "./models-config.providers.js";
|
|
|
|
type ModelsConfig = NonNullable<OpenClawConfig["models"]>;
|
|
|
|
const DEFAULT_MODE: NonNullable<ModelsConfig["mode"]> = "merge";
|
|
|
|
function resolvePreferredTokenLimit(explicitValue: number, implicitValue: number): number {
|
|
// Keep catalog refresh behavior for stale low values while preserving
|
|
// intentional larger user overrides (for example Ollama >128k contexts).
|
|
return explicitValue > implicitValue ? explicitValue : implicitValue;
|
|
}
|
|
|
|
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 implicitById = new Map(
|
|
implicitModels.map((model) => [getId(model), model] as const).filter(([id]) => Boolean(id)),
|
|
);
|
|
const seen = new Set<string>();
|
|
|
|
const mergedModels = explicitModels.map((explicitModel) => {
|
|
const id = getId(explicitModel);
|
|
if (!id) {
|
|
return explicitModel;
|
|
}
|
|
seen.add(id);
|
|
const implicitModel = implicitById.get(id);
|
|
if (!implicitModel) {
|
|
return explicitModel;
|
|
}
|
|
|
|
// Refresh capability metadata from the implicit catalog while preserving
|
|
// user-specific fields (cost, headers, compat, etc.) on explicit entries.
|
|
// reasoning is treated as user-overridable: if the user has explicitly set
|
|
// it in their config (key present), honour that value; otherwise fall back
|
|
// to the built-in catalog default so new reasoning models work out of the
|
|
// box without requiring every user to configure it.
|
|
return {
|
|
...explicitModel,
|
|
input: implicitModel.input,
|
|
reasoning: "reasoning" in explicitModel ? explicitModel.reasoning : implicitModel.reasoning,
|
|
contextWindow: resolvePreferredTokenLimit(
|
|
explicitModel.contextWindow,
|
|
implicitModel.contextWindow,
|
|
),
|
|
maxTokens: resolvePreferredTokenLimit(explicitModel.maxTokens, implicitModel.maxTokens),
|
|
};
|
|
});
|
|
|
|
for (const implicitModel of implicitModels) {
|
|
const id = getId(implicitModel);
|
|
if (!id || seen.has(id)) {
|
|
continue;
|
|
}
|
|
seen.add(id);
|
|
mergedModels.push(implicitModel);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
async function readJson(pathname: string): Promise<unknown> {
|
|
try {
|
|
const raw = await fs.readFile(pathname, "utf8");
|
|
return JSON.parse(raw) as unknown;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function ensureOpenClawModelsJson(
|
|
config?: OpenClawConfig,
|
|
agentDirOverride?: string,
|
|
): Promise<{ agentDir: string; wrote: boolean }> {
|
|
const cfg = config ?? loadConfig();
|
|
const agentDir = agentDirOverride?.trim() ? agentDirOverride.trim() : resolveOpenClawAgentDir();
|
|
|
|
const explicitProviders = cfg.models?.providers ?? {};
|
|
const implicitProviders = await resolveImplicitProviders({ agentDir, explicitProviders });
|
|
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 };
|
|
}
|
|
|
|
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 = {};
|
|
for (const [key, entry] of Object.entries(existingProviders)) {
|
|
mergedProviders[key] = entry;
|
|
}
|
|
for (const [key, newEntry] of Object.entries(providers)) {
|
|
const existing = existingProviders[key] as
|
|
| (NonNullable<ModelsConfig["providers"]>[string] & {
|
|
apiKey?: string;
|
|
baseUrl?: string;
|
|
})
|
|
| undefined;
|
|
if (existing) {
|
|
const preserved: Record<string, unknown> = {};
|
|
if (typeof existing.apiKey === "string" && existing.apiKey) {
|
|
preserved.apiKey = existing.apiKey;
|
|
}
|
|
if (typeof existing.baseUrl === "string" && existing.baseUrl) {
|
|
preserved.baseUrl = existing.baseUrl;
|
|
}
|
|
mergedProviders[key] = { ...newEntry, ...preserved };
|
|
} else {
|
|
mergedProviders[key] = newEntry;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
const normalizedProviders = normalizeProviders({
|
|
providers: mergedProviders,
|
|
agentDir,
|
|
});
|
|
const next = `${JSON.stringify({ providers: normalizedProviders }, null, 2)}\n`;
|
|
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 };
|
|
}
|