forked from openclaw/openclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent-scope-config.ts
More file actions
214 lines (200 loc) · 7.15 KB
/
Copy pathagent-scope-config.ts
File metadata and controls
214 lines (200 loc) · 7.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import path from "node:path";
import { resolveStateDir } from "../config/paths.js";
import type {
AgentContextLimitsConfig,
AgentDefaultsConfig,
} from "../config/types.agent-defaults.js";
import type { OpenClawConfig } from "../config/types.js";
import { DEFAULT_AGENT_ID, normalizeAgentId } from "../routing/session-key.js";
import { readStringValue } from "../shared/string-coerce.js";
import { resolveUserPath } from "../utils.js";
import { resolveDefaultAgentWorkspaceDir } from "./workspace-default.js";
type AgentEntry = NonNullable<NonNullable<OpenClawConfig["agents"]>["list"]>[number];
export type ResolvedAgentConfig = {
name?: string;
workspace?: string;
agentDir?: string;
model?: AgentEntry["model"];
thinkingDefault?: AgentEntry["thinkingDefault"];
verboseDefault?: AgentDefaultsConfig["verboseDefault"];
reasoningDefault?: AgentEntry["reasoningDefault"];
fastModeDefault?: AgentEntry["fastModeDefault"];
contextInjection?: AgentEntry["contextInjection"];
bootstrapMaxChars?: AgentEntry["bootstrapMaxChars"];
bootstrapTotalMaxChars?: AgentEntry["bootstrapTotalMaxChars"];
experimental?: AgentDefaultsConfig["experimental"];
skills?: AgentEntry["skills"];
memorySearch?: AgentEntry["memorySearch"];
humanDelay?: AgentEntry["humanDelay"];
tts?: AgentEntry["tts"];
contextLimits?: AgentContextLimitsConfig;
heartbeat?: AgentEntry["heartbeat"];
identity?: AgentEntry["identity"];
groupChat?: AgentEntry["groupChat"];
subagents?: AgentEntry["subagents"];
runRetries?: AgentEntry["runRetries"];
embeddedAgent?: AgentEntry["embeddedAgent"];
sandbox?: AgentEntry["sandbox"];
tools?: AgentEntry["tools"];
};
let defaultAgentWarned = false;
function warnMultipleDefaultAgents(): void {
void import("../logging/subsystem.js")
.then(({ createSubsystemLogger }) => {
createSubsystemLogger("agent-scope").warn(
"Multiple agents marked default=true; using the first entry as default.",
);
})
.catch(() => undefined);
}
/** Strip null bytes from paths to prevent ENOTDIR errors. */
function stripNullBytes(s: string): string {
return s.replaceAll("\0", "");
}
export function listAgentEntries(cfg: OpenClawConfig): AgentEntry[] {
const list = cfg.agents?.list;
if (!Array.isArray(list)) {
return [];
}
return list.filter((entry): entry is AgentEntry => entry !== null && typeof entry === "object");
}
export function listAgentIds(cfg: OpenClawConfig): string[] {
const agents = listAgentEntries(cfg);
if (agents.length === 0) {
return [DEFAULT_AGENT_ID];
}
const seen = new Set<string>();
const ids: string[] = [];
for (const entry of agents) {
const id = normalizeAgentId(entry?.id);
if (seen.has(id)) {
continue;
}
seen.add(id);
ids.push(id);
}
return ids.length > 0 ? ids : [DEFAULT_AGENT_ID];
}
export function resolveDefaultAgentId(cfg: OpenClawConfig): string {
const agents = listAgentEntries(cfg);
if (agents.length === 0) {
return DEFAULT_AGENT_ID;
}
const defaults = agents.filter((agent) => agent?.default);
if (defaults.length > 1 && !defaultAgentWarned) {
defaultAgentWarned = true;
warnMultipleDefaultAgents();
}
const chosen = (defaults[0] ?? agents[0])?.id?.trim();
return normalizeAgentId(chosen || DEFAULT_AGENT_ID);
}
function resolveAgentEntry(cfg: OpenClawConfig, agentId: string): AgentEntry | undefined {
const id = normalizeAgentId(agentId);
return listAgentEntries(cfg).find((entry) => normalizeAgentId(entry.id) === id);
}
export function resolveAgentConfig(
cfg: OpenClawConfig,
agentId: string,
): ResolvedAgentConfig | undefined {
const id = normalizeAgentId(agentId);
const entry = resolveAgentEntry(cfg, id);
if (!entry) {
return undefined;
}
const agentDefaults = cfg.agents?.defaults;
return {
name: readStringValue(entry.name),
workspace: readStringValue(entry.workspace),
agentDir: readStringValue(entry.agentDir),
model:
typeof entry.model === "string" || (entry.model && typeof entry.model === "object")
? entry.model
: undefined,
thinkingDefault: entry.thinkingDefault,
verboseDefault: entry.verboseDefault ?? agentDefaults?.verboseDefault,
reasoningDefault: entry.reasoningDefault,
fastModeDefault: entry.fastModeDefault,
contextInjection: entry.contextInjection,
bootstrapMaxChars: entry.bootstrapMaxChars,
bootstrapTotalMaxChars: entry.bootstrapTotalMaxChars,
experimental:
typeof entry.experimental === "object" && entry.experimental
? { ...agentDefaults?.experimental, ...entry.experimental }
: agentDefaults?.experimental,
skills: Array.isArray(entry.skills) ? entry.skills : undefined,
memorySearch: entry.memorySearch,
humanDelay: entry.humanDelay,
tts: entry.tts,
contextLimits:
typeof entry.contextLimits === "object" && entry.contextLimits
? { ...agentDefaults?.contextLimits, ...entry.contextLimits }
: agentDefaults?.contextLimits,
heartbeat: entry.heartbeat,
identity: entry.identity,
groupChat: entry.groupChat,
subagents: typeof entry.subagents === "object" && entry.subagents ? entry.subagents : undefined,
runRetries:
typeof entry.runRetries === "object" && entry.runRetries
? { ...agentDefaults?.runRetries, ...entry.runRetries }
: agentDefaults?.runRetries,
embeddedAgent:
typeof entry.embeddedAgent === "object" && entry.embeddedAgent
? entry.embeddedAgent
: undefined,
sandbox: entry.sandbox,
tools: entry.tools,
};
}
export function resolveAgentContextLimits(
cfg: OpenClawConfig | undefined,
agentId?: string | null,
): AgentContextLimitsConfig | undefined {
const defaults = cfg?.agents?.defaults?.contextLimits;
if (!cfg || !agentId) {
return defaults;
}
return resolveAgentConfig(cfg, agentId)?.contextLimits ?? defaults;
}
export function resolveAgentWorkspaceDir(
cfg: OpenClawConfig,
agentId: string,
env: NodeJS.ProcessEnv = process.env,
) {
const id = normalizeAgentId(agentId);
const configured = resolveAgentConfig(cfg, id)?.workspace?.trim();
if (configured) {
return stripNullBytes(resolveUserPath(configured, env));
}
const defaultAgentId = resolveDefaultAgentId(cfg);
const fallback = cfg.agents?.defaults?.workspace?.trim();
if (id === defaultAgentId) {
if (fallback) {
return stripNullBytes(resolveUserPath(fallback, env));
}
return stripNullBytes(resolveDefaultAgentWorkspaceDir(env));
}
if (fallback) {
return stripNullBytes(path.join(resolveUserPath(fallback, env), id));
}
const stateDir = resolveStateDir(env);
return stripNullBytes(path.join(stateDir, `workspace-${id}`));
}
export function resolveAgentDir(
cfg: OpenClawConfig,
agentId: string,
env: NodeJS.ProcessEnv = process.env,
) {
const id = normalizeAgentId(agentId);
const configured = resolveAgentConfig(cfg, id)?.agentDir?.trim();
if (configured) {
return resolveUserPath(configured, env);
}
const root = resolveStateDir(env);
return path.join(root, "agents", id, "agent");
}
export function resolveDefaultAgentDir(
cfg: OpenClawConfig,
env: NodeJS.ProcessEnv = process.env,
): string {
return resolveAgentDir(cfg, resolveDefaultAgentId(cfg), env);
}