openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
318 lines (317 loc) • 11 kB
JavaScript
import { t as AsyncContext } from "./async-context-BYDAvG9A.mjs";
import { u as throwSqliteLifecycleErrors } from "./sqlite-lifecycle-errors-D8BU4VLa.mjs";
import { t as warnPluginSdkDeprecation } from "./sdk-deprecation-D5N9acnu.mjs";
//#region src/agents/harness/host-private-capabilities.ts
const replyQueueInputAdapters = /* @__PURE__ */ new WeakMap();
/** Keep adapter metadata on the existing host owner without changing its public shape. */
function bindReplyQueueInputAdapter(e, t) {
replyQueueInputAdapters.set(e, t);
return e;
}
function readReplyQueueInputAdapter(e) {
return (e && replyQueueInputAdapters.get(e)) ?? "legacy-question-category";
}
const questionDispatchCapabilities = /* @__PURE__ */ new WeakMap();
function bindQuestionDispatchCapability(e, t, n) {
questionDispatchCapabilities.set(e, t);
if (n) questionDispatchCapabilities.set(n, t);
}
function readQuestionDispatchCapability(e) {
return e ? questionDispatchCapabilities.get(e) : void 0;
}
/** Preserve reservation refusal tracking on every final policy check. */
function bindQuestionDispatchGuard(e, t, n) {
const r = t?.prepareCurrent && (async () => {
try {
e();
await t?.prepareCurrent?.();
e();
} catch (e) {
n(e);
}
});
const guard = (t) => {
try {
e();
t();
e();
} catch (e) {
n(e);
}
};
const i = readQuestionDispatchCapability(t?.assertCurrent);
const a = i?.callerRead;
bindQuestionDispatchCapability(e, {
assertCompatibilityCurrent: () => guard(i?.assertCompatibilityCurrent ?? e),
callerRead: a && {
reads: a.reads,
prepareCurrent: r,
assertPrepared: (e) => guard(() => a.assertPrepared(e)),
retainCurrent: () => {
let e;
try {
e = a.retainCurrent();
} catch (e) {
return n(e);
}
return {
release: e.release,
assertCurrent: () => guard(e.assertCurrent)
};
}
}
}, r);
return r;
}
function prepareQuestionGatewayDispatch(e, t = false) {
const n = readQuestionDispatchCapability(e?.assertCurrent);
return {
assertDispatchCurrent: t ? e?.assertCurrent : n?.assertCompatibilityCurrent ?? e?.assertCurrent,
prepareDispatchCurrent: e?.prepareCurrent
};
}
const callerReadPreparers = /* @__PURE__ */ new WeakMap();
const toolAuthorityReadScope = new AsyncContext(Symbol("toolAuthorityReads"));
const workerToolPreparations = /* @__PURE__ */ new WeakMap();
function bindWorkerToolPreparation(e, t = []) {
workerToolPreparations.set(e.prepareCurrent, { complete: t.every((e) => workerToolPreparations.get(e.prepareCurrent)?.complete === true) });
return e;
}
function isToolAuthorityReadCaptureActive() {
return toolAuthorityReadScope.getStore() !== void 0;
}
function recordPreparedToolAuthorityRead(e) {
toolAuthorityReadScope.getStore()?.reads.push(e);
}
async function capturePreparedToolAuthorityReads(e) {
const t = workerToolPreparations.get(e.prepareCurrent);
const n = {
reads: [],
complete: t?.complete ?? false
};
if (t) await toolAuthorityReadScope.run(n, e.prepareCurrent);
else await e.prepareCurrent();
e.assertCurrent();
return {
reads: n.reads,
assertCompatibility: !n.complete || !n.reads.length ? e.compatAssertCurrent : void 0
};
}
function bindReplyToolAuthorityCallerRead(e, t) {
callerReadPreparers.set(e, t);
}
/** Select one policy owner before delivery preparation can yield to a successor attempt. */
function captureReplyToolAuthorityCallerBinding(e, t, n, r, i) {
i();
const a = e && n ? callerReadPreparers.get(e)?.(t, n, r, i) : void 0;
if (!a) {
const e = toolAuthorityReadScope.getStore();
if (e) e.complete = false;
return;
}
a.assertCurrent();
return {
assertCurrent: () => a.assertCurrent(),
joinObservation: a.joinObservation,
prepare: async () => {
a.assertCurrent();
try {
const e = await a.prepare();
const t = toolAuthorityReadScope.getStore();
if (t && !e) t.complete = false;
return e;
} finally {
a.assertCurrent();
}
}
};
}
async function prepareReplyToolAuthorityCallerRead(e, t, n, r, i) {
return captureReplyToolAuthorityCallerBinding(e, t, n, r, i)?.prepare();
}
function assertQuestionCompatibilityCurrent(e, t, n) {
e();
const r = t?.retainCurrent();
try {
if (r) r.assertCurrent();
else {
warnPluginSdkDeprecation({
family: "reply-tool-authority-sync-preparation",
method: "question dispatcher authority.assertCurrent synchronous preparation",
replacement: "await authority.assertCurrentAsync() before the final effect assertion"
});
n();
}
} finally {
r?.release();
}
e();
}
/** Compose captured policy reads with the question owner's final synchronous admission. */
async function prepareQuestionInputAuthority(e) {
const t = e.toolAuthorityPreparation;
if (!t) return e;
const assertActive = () => {
e.assertCurrent();
t.assertCurrent();
};
assertActive();
const n = await capturePreparedToolAuthorityReads(t);
assertActive();
const r = !n.assertCompatibility && n.reads.length > 0 && n.reads.every((e) => e.retainCurrent);
const assertLegacyReads = () => {
for (const e of n.reads) e.assertLegacyCurrent();
};
const prepareCurrent = async () => {
assertActive();
await t.prepareCurrent();
if (!r) assertLegacyReads();
assertActive();
};
const i = r ? {
reads: n.reads.flatMap((e) => e.reads),
prepareCurrent,
assertPrepared: (e) => {
assertActive();
let t = 0;
for (const r of n.reads) {
r.assertPrepared(e.slice(t, t + r.reads.length));
t += r.reads.length;
}
assertActive();
},
retainCurrent: () => {
const e = [];
const release = () => {
const t = [];
for (const n of e.toReversed()) try {
n.release();
} catch (e) {
t.push(e);
}
throwSqliteLifecycleErrors(t, "Question authority reader release failed");
};
try {
for (const t of n.reads) e.push(t.retainCurrent());
return {
release,
assertCurrent: () => {
assertActive();
e.forEach((e) => e.assertCurrent());
assertActive();
}
};
} catch (e) {
release();
throw e;
}
}
} : void 0;
const assertCompatibilityCurrent = () => assertQuestionCompatibilityCurrent(assertActive, i, () => {
t.compatAssertCurrent();
assertLegacyReads();
});
const a = r ? assertActive : () => {
assertActive();
t.compatAssertCurrent();
assertActive();
};
bindQuestionDispatchCapability(a, {
assertCompatibilityCurrent,
callerRead: i
});
return {
kind: e.kind,
assertCurrent: a,
prepareCurrent
};
}
async function prepareQuestionCallerAuthority(e, t, n) {
n();
const r = await e.prepareCaller?.(t);
n();
const assertCompatibilityCurrent = () => assertQuestionCompatibilityCurrent(n, r, () => e.assertCaller(t));
const i = r ? n : assertCompatibilityCurrent;
bindQuestionDispatchCapability(i, {
assertCompatibilityCurrent,
callerRead: r
});
return {
kind: "source-bound",
assertCurrent: i,
prepareCurrent: async () => {
n();
if (r) await r.prepareCurrent();
else e.assertCaller(t);
n();
}
};
}
const questionAnswerScope = new AsyncContext(Symbol("questionAnswerAuthority"));
const questionAnswerCapabilities = /* @__PURE__ */ new WeakMap();
/** Retain the creator's prepared policy; a matching hash alone never grants authority. */
function createAgentQuestionAnswerAuthority(e) {
return Object.freeze({
sessionKey: e.sessionKey.trim(),
requesterProfileId: e.requesterProfileId,
assertActive: e.assertActive,
admitTranscriptAnswer: e.admitTranscriptAnswer,
prepareCaller: e.prepareCaller,
assertCaller: (t) => {
e.assertActive();
const n = e.project(t);
e.assertActive();
if (!e.fingerprint || n !== e.fingerprint) throw new Error("question answer caller policy does not match its creator");
}
});
}
function withAgentQuestionAnswerAuthority(e, t) {
return questionAnswerScope.run(e, t);
}
function registerAgentHarnessQuestionAnswerAuthority(e, t) {
questionAnswerCapabilities.set(e, t);
}
/** An explicit host carrier cannot fall back to an unrelated ambient creator. */
function resolveAgentQuestionAnswerAuthority(e) {
return e ? questionAnswerCapabilities.get(e) : questionAnswerScope.getStore();
}
function captureAgentQuestionAnswerAuthority(e) {
const t = questionAnswerScope.getStore();
t?.assertActive();
if (t && t.sessionKey !== e.trim()) throw new Error("question creator authority belongs to another session");
return t;
}
const retainedBeforeToolCallRunners = /* @__PURE__ */ new WeakMap();
/** Retain issued policy without importing the capability constructor and its tool graph. */
function retainBeforeToolCallForNativeHookRelay(e) {
return retainedBeforeToolCallRunners.get(e)?.();
}
function registerAgentHarnessBeforeToolCallRetention(e, t) {
retainedBeforeToolCallRunners.set(e, t);
}
const scheduledToolProjectionCapabilities = /* @__PURE__ */ new WeakMap();
const ttsProvenanceTransferCapabilities = /* @__PURE__ */ new WeakMap();
function registerAgentHarnessScheduledToolProjectionCapability(e) {
scheduledToolProjectionCapabilities.set(e.hostCapabilities, {
ownerPluginId: e.ownerPluginId,
create: e.create
});
}
/** Resolves a private issuer only for the exact authoritative plugin owner. */
function resolveAgentHarnessScheduledToolProjectionCapability(e) {
const t = scheduledToolProjectionCapabilities.get(e.hostCapabilities);
return t?.ownerPluginId === e.ownerPluginId ? t.create : void 0;
}
function registerAgentHarnessTtsProvenanceTransferCapability(e) {
ttsProvenanceTransferCapabilities.set(e.hostCapabilities, {
ownerPluginId: e.ownerPluginId,
transfer: e.transfer
});
}
/** Resolves private TTS delivery transfer only for the exact authoritative plugin owner. */
function resolveAgentHarnessTtsProvenanceTransferCapability(e) {
const t = ttsProvenanceTransferCapabilities.get(e.hostCapabilities);
return t?.ownerPluginId === e.ownerPluginId ? t.transfer : void 0;
}
//#endregion
export { resolveAgentQuestionAnswerAuthority as C, resolveAgentHarnessTtsProvenanceTransferCapability as S, withAgentQuestionAnswerAuthority as T, registerAgentHarnessBeforeToolCallRetention as _, captureAgentQuestionAnswerAuthority as a, registerAgentHarnessTtsProvenanceTransferCapability as b, createAgentQuestionAnswerAuthority as c, prepareQuestionGatewayDispatch as d, prepareQuestionInputAuthority as f, recordPreparedToolAuthorityRead as g, readReplyQueueInputAdapter as h, bindWorkerToolPreparation as i, isToolAuthorityReadCaptureActive as l, readQuestionDispatchCapability as m, bindReplyQueueInputAdapter as n, capturePreparedToolAuthorityReads as o, prepareReplyToolAuthorityCallerRead as p, bindReplyToolAuthorityCallerRead as r, captureReplyToolAuthorityCallerBinding as s, bindQuestionDispatchGuard as t, prepareQuestionCallerAuthority as u, registerAgentHarnessQuestionAnswerAuthority as v, retainBeforeToolCallForNativeHookRelay as w, resolveAgentHarnessScheduledToolProjectionCapability as x, registerAgentHarnessScheduledToolProjectionCapability as y };