openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
212 lines (211 loc) • 9.75 kB
JavaScript
import { _ as resolveStateDir } from "./paths-ViKUYWUK.js";
import { t as createSubsystemLogger } from "./subsystem-DISldKSB.js";
import { y as resolveUserPath } from "./utils-CS0Ikux6.js";
import { t as resolveAgentModelFallbackValues } from "./model-input-BB2wSAHb.js";
import { T as parseAgentSessionKey, c as normalizeAgentId, t as DEFAULT_AGENT_ID, u as resolveAgentIdFromSessionKey } from "./session-key-DAhnzjyr.js";
import { r as normalizeStringEntries } from "./string-normalization-CohoSMRS.js";
import { g as resolveDefaultAgentWorkspaceDir } from "./workspace-D4K6QX9X.js";
import fs from "node:fs";
import path from "node:path";
//#region src/agents/skills/filter.ts
function normalizeSkillFilter(skillFilter) {
if (skillFilter === void 0) return;
return normalizeStringEntries(skillFilter);
}
function normalizeSkillFilterForComparison(skillFilter) {
const normalized = normalizeSkillFilter(skillFilter);
if (normalized === void 0) return;
return Array.from(new Set(normalized)).toSorted();
}
function matchesSkillFilter(cached, next) {
const cachedNormalized = normalizeSkillFilterForComparison(cached);
const nextNormalized = normalizeSkillFilterForComparison(next);
if (cachedNormalized === void 0 || nextNormalized === void 0) return cachedNormalized === nextNormalized;
if (cachedNormalized.length !== nextNormalized.length) return false;
return cachedNormalized.every((entry, index) => entry === nextNormalized[index]);
}
//#endregion
//#region src/agents/agent-scope.ts
const log = createSubsystemLogger("agent-scope");
/** Strip null bytes from paths to prevent ENOTDIR errors. */
function stripNullBytes(s) {
return s.replace(/\0/g, "");
}
let defaultAgentWarned = false;
function listAgentEntries(cfg) {
const list = cfg.agents?.list;
if (!Array.isArray(list)) return [];
return list.filter((entry) => Boolean(entry && typeof entry === "object"));
}
function listAgentIds(cfg) {
const agents = listAgentEntries(cfg);
if (agents.length === 0) return [DEFAULT_AGENT_ID];
const seen = /* @__PURE__ */ new Set();
const ids = [];
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];
}
function resolveDefaultAgentId(cfg) {
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;
log.warn("Multiple agents marked default=true; using the first entry as default.");
}
const chosen = (defaults[0] ?? agents[0])?.id?.trim();
return normalizeAgentId(chosen || "main");
}
function resolveSessionAgentIds(params) {
const defaultAgentId = resolveDefaultAgentId(params.config ?? {});
const explicitAgentIdRaw = typeof params.agentId === "string" ? params.agentId.trim().toLowerCase() : "";
const explicitAgentId = explicitAgentIdRaw ? normalizeAgentId(explicitAgentIdRaw) : null;
const sessionKey = params.sessionKey?.trim();
const normalizedSessionKey = sessionKey ? sessionKey.toLowerCase() : void 0;
const parsed = normalizedSessionKey ? parseAgentSessionKey(normalizedSessionKey) : null;
return {
defaultAgentId,
sessionAgentId: explicitAgentId ?? (parsed?.agentId ? normalizeAgentId(parsed.agentId) : defaultAgentId)
};
}
function resolveSessionAgentId(params) {
return resolveSessionAgentIds(params).sessionAgentId;
}
function resolveAgentEntry(cfg, agentId) {
const id = normalizeAgentId(agentId);
return listAgentEntries(cfg).find((entry) => normalizeAgentId(entry.id) === id);
}
function resolveAgentConfig(cfg, agentId) {
const entry = resolveAgentEntry(cfg, normalizeAgentId(agentId));
if (!entry) return;
return {
name: typeof entry.name === "string" ? entry.name : void 0,
workspace: typeof entry.workspace === "string" ? entry.workspace : void 0,
agentDir: typeof entry.agentDir === "string" ? entry.agentDir : void 0,
model: typeof entry.model === "string" || entry.model && typeof entry.model === "object" ? entry.model : void 0,
thinkingDefault: entry.thinkingDefault,
reasoningDefault: entry.reasoningDefault,
fastModeDefault: entry.fastModeDefault,
skills: Array.isArray(entry.skills) ? entry.skills : void 0,
memorySearch: entry.memorySearch,
humanDelay: entry.humanDelay,
heartbeat: entry.heartbeat,
identity: entry.identity,
groupChat: entry.groupChat,
subagents: typeof entry.subagents === "object" && entry.subagents ? entry.subagents : void 0,
sandbox: entry.sandbox,
tools: entry.tools
};
}
function resolveAgentSkillsFilter(cfg, agentId) {
return normalizeSkillFilter(resolveAgentConfig(cfg, agentId)?.skills);
}
function resolveModelPrimary(raw) {
if (typeof raw === "string") return raw.trim() || void 0;
if (!raw || typeof raw !== "object") return;
const primary = raw.primary;
if (typeof primary !== "string") return;
return primary.trim() || void 0;
}
function resolveAgentExplicitModelPrimary(cfg, agentId) {
const raw = resolveAgentConfig(cfg, agentId)?.model;
return resolveModelPrimary(raw);
}
function resolveAgentEffectiveModelPrimary(cfg, agentId) {
return resolveAgentExplicitModelPrimary(cfg, agentId) ?? resolveModelPrimary(cfg.agents?.defaults?.model);
}
function resolveAgentModelPrimary(cfg, agentId) {
return resolveAgentExplicitModelPrimary(cfg, agentId);
}
function resolveAgentModelFallbacksOverride(cfg, agentId) {
const raw = resolveAgentConfig(cfg, agentId)?.model;
if (!raw || typeof raw === "string") return;
if (!Object.hasOwn(raw, "fallbacks")) return;
return Array.isArray(raw.fallbacks) ? raw.fallbacks : void 0;
}
function resolveFallbackAgentId(params) {
const explicitAgentId = typeof params.agentId === "string" ? params.agentId.trim() : "";
if (explicitAgentId) return normalizeAgentId(explicitAgentId);
return resolveAgentIdFromSessionKey(params.sessionKey);
}
function resolveRunModelFallbacksOverride(params) {
if (!params.cfg) return;
return resolveAgentModelFallbacksOverride(params.cfg, resolveFallbackAgentId({
agentId: params.agentId,
sessionKey: params.sessionKey
}));
}
function hasConfiguredModelFallbacks(params) {
const fallbacksOverride = resolveRunModelFallbacksOverride(params);
const defaultFallbacks = resolveAgentModelFallbackValues(params.cfg?.agents?.defaults?.model);
return (fallbacksOverride ?? defaultFallbacks).length > 0;
}
function resolveEffectiveModelFallbacks(params) {
const agentFallbacksOverride = resolveAgentModelFallbacksOverride(params.cfg, params.agentId);
if (!params.hasSessionModelOverride) return agentFallbacksOverride;
const defaultFallbacks = resolveAgentModelFallbackValues(params.cfg.agents?.defaults?.model);
return agentFallbacksOverride ?? defaultFallbacks;
}
function resolveAgentWorkspaceDir(cfg, agentId) {
const id = normalizeAgentId(agentId);
const configured = resolveAgentConfig(cfg, id)?.workspace?.trim();
if (configured) return stripNullBytes(resolveUserPath(configured));
if (id === resolveDefaultAgentId(cfg)) {
const fallback = cfg.agents?.defaults?.workspace?.trim();
if (fallback) return stripNullBytes(resolveUserPath(fallback));
return stripNullBytes(resolveDefaultAgentWorkspaceDir(process.env));
}
const stateDir = resolveStateDir(process.env);
return stripNullBytes(path.join(stateDir, `workspace-${id}`));
}
function normalizePathForComparison(input) {
const resolved = path.resolve(stripNullBytes(resolveUserPath(input)));
let normalized = resolved;
try {
normalized = fs.realpathSync.native(resolved);
} catch {}
if (process.platform === "win32") return normalized.toLowerCase();
return normalized;
}
function isPathWithinRoot(candidatePath, rootPath) {
const relative = path.relative(rootPath, candidatePath);
return relative === "" || !relative.startsWith("..") && !path.isAbsolute(relative);
}
function resolveAgentIdsByWorkspacePath(cfg, workspacePath) {
const normalizedWorkspacePath = normalizePathForComparison(workspacePath);
const ids = listAgentIds(cfg);
const matches = [];
for (let index = 0; index < ids.length; index += 1) {
const id = ids[index];
const workspaceDir = normalizePathForComparison(resolveAgentWorkspaceDir(cfg, id));
if (!isPathWithinRoot(normalizedWorkspacePath, workspaceDir)) continue;
matches.push({
id,
workspaceDir,
order: index
});
}
matches.sort((left, right) => {
const workspaceLengthDelta = right.workspaceDir.length - left.workspaceDir.length;
if (workspaceLengthDelta !== 0) return workspaceLengthDelta;
return left.order - right.order;
});
return matches.map((entry) => entry.id);
}
function resolveAgentIdByWorkspacePath(cfg, workspacePath) {
return resolveAgentIdsByWorkspacePath(cfg, workspacePath)[0];
}
function resolveAgentDir(cfg, agentId, env = 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");
}
//#endregion
export { resolveRunModelFallbacksOverride as _, resolveAgentDir as a, matchesSkillFilter as b, resolveAgentIdByWorkspacePath as c, resolveAgentModelPrimary as d, resolveAgentSkillsFilter as f, resolveFallbackAgentId as g, resolveEffectiveModelFallbacks as h, resolveAgentConfig as i, resolveAgentIdsByWorkspacePath as l, resolveDefaultAgentId as m, listAgentEntries as n, resolveAgentEffectiveModelPrimary as o, resolveAgentWorkspaceDir as p, listAgentIds as r, resolveAgentExplicitModelPrimary as s, hasConfiguredModelFallbacks as t, resolveAgentModelFallbacksOverride as u, resolveSessionAgentId as v, normalizeSkillFilter as x, resolveSessionAgentIds as y };