openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
181 lines (180 loc) • 6.76 kB
JavaScript
import { p as shortenHomePath } from "./utils-C8YECn2Q.mjs";
import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs";
import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs";
import "./session-key-CqAyZeVk.mjs";
import { n as listAgentEntries } from "./agent-roster-t1ooUdXp.mjs";
import { A as tryResolveLegacyCompatibilityAgentId, E as toAgentEntriesRecord, T as resolveSoleAgentId, a as resolveAgentDir, f as resolveAgentWorkspaceDir, y as resolveMutableAgentEntry } from "./agent-scope-config-DmXgFGKP.mjs";
import { n as normalizeAgentModelMapForConfig, o as resolveAgentModelFallbackValues, r as normalizeAgentModelRefForConfig, u as toAgentModelListLike } from "./model-input-DrM4DWHG.mjs";
import "./legacy.default-agent-owner-DpmHgK0A.mjs";
import { t as applyPrimaryModel } from "./provider-model-primary-C2E-I43s.mjs";
import { r as ensureWorkspaceAndSessions } from "./onboard-helpers-I6fwzX78.mjs";
import { isDeepStrictEqual } from "node:util";
//#region src/commands/onboard-agent-target.ts
function splitAgentModelDefaults({ model: e, utilityModel: t, models: n, modelPolicy: r, ...i }) {
return {
rest: i,
modelDefaults: {
...e !== void 0 ? { model: e } : {},
...t !== void 0 ? { utilityModel: t } : {},
...n !== void 0 ? { models: n } : {},
...r !== void 0 ? { modelPolicy: r } : {}
}
};
}
function resolveOnboardingAgentTarget(e, t) {
const n = normalizeAgentId(t ?? tryResolveLegacyCompatibilityAgentId(e) ?? resolveSoleAgentId(e));
return {
agentId: n,
agentDir: resolveAgentDir(e, n),
workspaceDir: resolveAgentWorkspaceDir(e, n)
};
}
/** Resolve the configured System Agent as the owner of onboarding effects. */
function resolveSystemAgentOnboardingTarget(e) {
return resolveOnboardingAgentTarget(e, e.agents?.defaults?.systemAgent?.agentId);
}
/** Resolve onboarding setup to its existing or pending first-agent owner. */
function resolveOnboardingSetupTarget(e, t) {
if (e.agents?.ownership === "explicit") return resolveSystemAgentOnboardingTarget(e);
if (t) return {
...resolveOnboardingAgentTarget(e, t.name),
workspaceDir: t.workspaceDir
};
return resolveOnboardingAgentTarget(e);
}
async function ensureOnboardingAgentWorkspace(e, t, n) {
try {
return await ensureWorkspaceAndSessions(e.workspaceDir, t, {
...n,
agentId: e.agentId
});
} catch (t) {
throw new Error(`Workspace provisioning for agent "${e.agentId}" at ${shortenHomePath(e.workspaceDir)} failed: ${formatErrorMessage(t)}`, { cause: t });
}
}
function replaceOnboardingAgentEntry(e, t, n, r) {
const i = listAgentEntries(e);
const a = i.findIndex((e) => normalizeAgentId(e.id) === n.agentId);
const o = [...i];
const s = {
id: a >= 0 ? i[a].id : n.agentId,
...r
};
if (a >= 0) o[a] = s;
else o.push(s);
return {
...t,
agents: {
...e.agents,
entries: toAgentEntriesRecord(o)
}
};
}
function applyOnboardingWorkspace(e, t, n) {
const r = resolveMutableAgentEntry(e, t.agentId);
if (r?.workspace !== void 0 || e.agents?.ownership === "explicit" && r) return replaceOnboardingAgentEntry(e, e, t, {
...r,
workspace: n
});
return {
...e,
agents: {
...e.agents,
defaults: {
...e.agents?.defaults,
workspace: n
}
}
};
}
function applyOnboardingPrimaryModel(e, t, n) {
const r = resolveMutableAgentEntry(e, t.agentId);
if (r?.model === void 0 && e.agents?.ownership !== "explicit") return applyPrimaryModel(e, n);
const i = normalizeAgentModelRefForConfig(n);
const a = resolveAgentModelFallbackValues(r?.model).map((e) => normalizeAgentModelRefForConfig(e));
const o = normalizeAgentModelMapForConfig(r?.models ?? {});
return replaceOnboardingAgentEntry(e, e, t, {
...r,
model: {
...a.length > 0 ? { fallbacks: a } : {},
primary: i
},
models: {
...o,
[i]: o[i] ?? {}
}
});
}
/** Apply a utility selection to its existing agent/default owner without changing primary. */
function applyOnboardingUtilityModel(e, t, n) {
const r = normalizeAgentModelRefForConfig(n);
const i = resolveMutableAgentEntry(e, t.agentId);
const a = i?.utilityModel === void 0 && e.agents?.ownership !== "explicit";
const o = a ? e.agents?.defaults?.utilityModel : i?.utilityModel;
const s = typeof o === "object" ? {
...o,
primary: r
} : r;
if (a) return {
...e,
agents: {
...e.agents,
defaults: {
...e.agents?.defaults,
utilityModel: s
}
}
};
return replaceOnboardingAgentEntry(e, e, t, {
...i,
utilityModel: s
});
}
/** Expose one agent's effective model settings through the defaults-based provider contract. */
function prepareAgentModelDefaults(e, t) {
const n = resolveMutableAgentEntry(e, t.agentId);
return {
...e,
agents: {
...e.agents,
defaults: {
...e.agents?.defaults,
...splitAgentModelDefaults(n ?? {}).modelDefaults
}
}
};
}
/** Apply a model-default mutation to one agent without flattening it globally. */
function applyAgentModelDefaults(e, t, n) {
return projectAgentModelDefaults(e, t, n(prepareAgentModelDefaults(e, t)));
}
/** Move a defaults-based model mutation onto one agent while preserving its other config changes. */
function projectAgentModelDefaults(e, t, n) {
const r = resolveMutableAgentEntry(e, t.agentId);
if (!r && e.agents?.ownership !== "explicit") return n;
const i = n.agents?.defaults;
const a = e.agents?.defaults;
const o = r?.models !== void 0 ? i?.models : Object.fromEntries(Object.entries(i?.models ?? {}).filter(([e, t]) => !Object.hasOwn(a?.models ?? {}, e) || !isDeepStrictEqual(t, a?.models?.[e])));
const s = r?.model !== void 0 || !isDeepStrictEqual(toAgentModelListLike(i?.model), toAgentModelListLike(a?.model));
const c = r?.modelPolicy !== void 0 || !isDeepStrictEqual(i?.modelPolicy, a?.modelPolicy);
const l = r?.utilityModel !== void 0 || i?.utilityModel !== a?.utilityModel;
const u = {
...splitAgentModelDefaults(r ?? {}).rest,
...s && i?.model !== void 0 ? { model: i.model } : {},
...o && Object.keys(o).length > 0 ? { models: o } : {},
...c && i?.modelPolicy !== void 0 ? { modelPolicy: i.modelPolicy } : {},
...l && i?.utilityModel !== void 0 ? { utilityModel: i.utilityModel } : {}
};
return replaceOnboardingAgentEntry({
...e,
agents: {
...e.agents,
...a || i ? { defaults: {
...splitAgentModelDefaults(i ?? {}).rest,
...splitAgentModelDefaults(a ?? {}).modelDefaults
} } : {}
}
}, n, t, u);
}
//#endregion
export { ensureOnboardingAgentWorkspace as a, resolveOnboardingAgentTarget as c, applyOnboardingWorkspace as i, resolveOnboardingSetupTarget as l, applyOnboardingPrimaryModel as n, prepareAgentModelDefaults as o, applyOnboardingUtilityModel as r, projectAgentModelDefaults as s, applyAgentModelDefaults as t, resolveSystemAgentOnboardingTarget as u };