openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
115 lines (114 loc) • 4.55 kB
JavaScript
import { t as createSubsystemLogger } from "./subsystem-BHJiy01V.mjs";
import "./tool-policy-BR4v75RA.mjs";
import { t as AUTOMATIONS_TOOL_NAME } from "./automations-tool-name-DNRDs0aJ.mjs";
import { c as normalizeToolPolicyName } from "./tool-policy-shared-BHvBopvF.mjs";
import { n as createRuntimeToolMatcher } from "./tool-policy-match-DzwVnbV4.mjs";
import { _ as normalizeInputProvenance, u as isCompletionReportInputProvenance } from "./input-provenance-DupvvURj.mjs";
import { n as copyPluginToolMeta } from "./tool-metadata-rqUpUHlb.mjs";
import { t as ToolAuthorizationError } from "./tool-input-error-B1hgavSw.mjs";
import "./common-njQNtpnW.mjs";
import { s as withPreparedSessionCapabilityRead } from "./subagent-session-store--G7wm9iV.mjs";
import { t as hasVerifiedRequesterCompletionHandoff } from "./requester-tool-policy-BFuyRJjU.mjs";
//#region src/agents/delegation-capability.ts
const log = createSubsystemLogger("agents/delegation-capability");
const NEW_DELEGATION_TOOL_NAMES = /* @__PURE__ */ new Set([
"codex_session_send",
"llm-task",
"openclaw",
"sessions_send",
"sessions_spawn"
]);
const REPORT_ONLY_TOOL_ACTIONS = /* @__PURE__ */ new Map([
[AUTOMATIONS_TOOL_NAME, /* @__PURE__ */ new Set([
"get",
"list",
"remove",
"runs",
"status"
])],
["image_generate", /* @__PURE__ */ new Set(["list", "status"])],
["music_generate", /* @__PURE__ */ new Set(["list", "status"])],
["video_generate", /* @__PURE__ */ new Set(["list", "status"])]
]);
const REPORT_ONLY_ERROR = "New delegation is unavailable while reporting a completion through a fallback model.";
/** Retries and a sticky rate-limit fallback tried first both run a fallback model. */
function isFallbackAttempt(e) {
return e.isFallbackRetry || e.modelRoutingProvenance.stage === "fallback";
}
function resolveDelegationCapability(e) {
if (!isCompletionReportInputProvenance(e.inputProvenance)) return "full";
if (e.disableTools === true) return "report_only";
const t = e.fallbackActive ? "host_only" : "full";
if (e.fallbackActive) {
const t = e.requesterContinuation;
if (!t?.trustedInternalHandoff?.settleBatch || !hasVerifiedRequesterCompletionHandoff({
...t,
inputProvenance: normalizeInputProvenance(e.inputProvenance)
})) return "report_only";
}
if (e.toolsAllow === void 0) return t;
return [...NEW_DELEGATION_TOOL_NAMES].some(createRuntimeToolMatcher(e.toolsAllow)) ? t : "report_only";
}
/** Prepare lineage through its worker owner only for supported requester continuations. */
function prepareDelegationCapability(e) {
const { run: t } = e;
const n = {
fallbackActive: e.fallbackActive,
inputProvenance: t.inputProvenance,
disableTools: t.disableTools,
toolsAllow: t.toolsAllow,
requesterContinuation: {
config: t.config,
sessionKey: e.sessionKey,
sessionId: e.sessionId,
modelProvider: e.modelProvider,
modelId: e.modelId,
trustedInternalHandoff: t.trustedInternalHandoff
}
};
return e.fallbackActive && t.trustedInternalHandoff?.settleBatch ? withPreparedSessionCapabilityRead(() => resolveDelegationCapability(n)) : resolveDelegationCapability(n);
}
function readToolAction(e) {
if (!e || typeof e !== "object" || Array.isArray(e)) return "";
const t = e.action;
return typeof t === "string" ? t.trim().toLowerCase() : "";
}
function wrapReportOnlyTool(e, t) {
const n = new Proxy(e, { get(e, n, r) {
if (n !== "execute") return Reflect.get(e, n, r);
return async (n, r, i, a) => {
if (!t.has(readToolAction(r))) throw new ToolAuthorizationError(REPORT_ONLY_ERROR);
return await e.execute(n, r, i, a);
};
} });
copyPluginToolMeta(e, n);
return n;
}
/**
* Enforces the run's delegation capability after ordinary tool authorization.
* Tool names and safe actions here are explicit built-in/plugin contracts: the
* gate removes task launchers while retaining status, history, and cleanup.
*/
function applyDelegationCapability(e, t) {
if (t !== "report_only") return e;
const n = [];
const r = [];
const i = e.flatMap((e) => {
const t = normalizeToolPolicyName(e.name);
if (NEW_DELEGATION_TOOL_NAMES.has(t)) {
n.push(t);
return [];
}
const i = REPORT_ONLY_TOOL_ACTIONS.get(t);
if (!i) return [e];
r.push(t);
return [wrapReportOnlyTool(e, i)];
});
if (n.length > 0 || r.length > 0) log.debug("delegation capability restricted run tools", {
removed: n,
narrowed: r
});
return i;
}
//#endregion
export { resolveDelegationCapability as i, isFallbackAttempt as n, prepareDelegationCapability as r, applyDelegationCapability as t };