fix(agents): fall back to agents.defaults.model when agent has no model config

When an agent in agents.list lacks a model configuration, resolveAgentModelPrimary
now correctly falls back to cfg.agents.defaults.model instead of returning undefined.

This fixes embedded agents (like slug-generator) silently falling back to a hardcoded
DEFAULT_MODEL when the main agent omits the model block but global defaults are configured.

Fixes openclaw#24168
This commit is contained in:
Billion Bian
2026-02-23 13:56:54 +08:00
committed by Gustavo Madeira Santana
parent db32677f1d
commit b9a61aa0d2
2 changed files with 52 additions and 5 deletions

View File

@@ -59,6 +59,38 @@ describe("resolveAgentConfig", () => {
});
});
it("falls back to agents.defaults.model when agent has no model configured", () => {
const cfgWithStringDefault: OpenClawConfig = {
agents: {
defaults: {
model: "anthropic/claude-sonnet-4",
},
list: [{ id: "main" }],
},
};
expect(resolveAgentModelPrimary(cfgWithStringDefault, "main")).toBe("anthropic/claude-sonnet-4");
const cfgWithObjectDefault: OpenClawConfig = {
agents: {
defaults: {
model: {
primary: "openai/gpt-5.2",
fallbacks: ["anthropic/claude-sonnet-4"],
},
},
list: [{ id: "main" }],
},
};
expect(resolveAgentModelPrimary(cfgWithObjectDefault, "main")).toBe("openai/gpt-5.2");
const cfgNoDefaults: OpenClawConfig = {
agents: {
list: [{ id: "main" }],
},
};
expect(resolveAgentModelPrimary(cfgNoDefaults, "main")).toBeUndefined();
});
it("supports per-agent model primary+fallbacks", () => {
const cfg: OpenClawConfig = {
agents: {

View File

@@ -144,14 +144,29 @@ export function resolveAgentSkillsFilter(
export function resolveAgentModelPrimary(cfg: OpenClawConfig, agentId: string): string | undefined {
const raw = resolveAgentConfig(cfg, agentId)?.model;
if (!raw) {
if (raw) {
if (typeof raw === "string") {
const trimmed = raw.trim();
if (trimmed) {
return trimmed;
}
} else {
const primary = raw.primary?.trim();
if (primary) {
return primary;
}
}
}
// Fallback to agents.defaults.model when agent has no model configured
const defaultModel = cfg.agents?.defaults?.model;
if (!defaultModel) {
return undefined;
}
if (typeof raw === "string") {
return raw.trim() || undefined;
if (typeof defaultModel === "string") {
return defaultModel.trim() || undefined;
}
const primary = raw.primary?.trim();
return primary || undefined;
return defaultModel.primary?.trim() || undefined;
}
export function resolveAgentModelFallbacksOverride(