UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

279 lines (278 loc) • 11.5 kB
import { s as throwSqliteLifecycleErrors } from "./sqlite-lifecycle-errors-DuOygjcn.mjs"; import { AsyncLocalStorage } from "node:async_hooks"; //#region src/agents/harness/host-private-capabilities.ts const questionDispatchCapabilities = /* @__PURE__ */ new WeakMap(); function bindQuestionDispatchCapability(assertCurrent, capability, prepareCurrent) { questionDispatchCapabilities.set(assertCurrent, capability); if (prepareCurrent) questionDispatchCapabilities.set(prepareCurrent, capability); } function readQuestionDispatchCapability(assertCurrent) { return assertCurrent ? questionDispatchCapabilities.get(assertCurrent) : void 0; } /** Preserve reservation refusal tracking on every final policy check and native grant. */ function bindQuestionDispatchGuard(assertCurrent, authority, refuse) { const prepareCurrent = authority?.prepareCurrent && (async () => { try { assertCurrent(); await authority?.prepareCurrent?.(); assertCurrent(); } catch (error) { refuse(error); } }); const guard = (check) => { try { assertCurrent(); check(); assertCurrent(); } catch (error) { refuse(error); } }; const capability = readQuestionDispatchCapability(authority?.assertCurrent); const callerRead = capability?.callerRead; bindQuestionDispatchCapability(assertCurrent, { assertCompatibilityCurrent: () => guard(capability?.assertCompatibilityCurrent ?? assertCurrent), callerRead: callerRead && { reads: callerRead.reads, prepareCurrent, assertPrepared: (reads) => guard(() => callerRead.assertPrepared(reads)), retainNative: () => { let native; try { native = callerRead.retainNative(); } catch (error) { return refuse(error); } return { release: native.release, assertCurrent: () => guard(native.assertCurrent) }; } } }, prepareCurrent); return prepareCurrent; } function prepareQuestionGatewayDispatch(authority, inProcess = false) { const capability = readQuestionDispatchCapability(authority?.assertCurrent); return { assertDispatchCurrent: inProcess ? authority?.assertCurrent : capability?.assertCompatibilityCurrent ?? authority?.assertCurrent, prepareDispatchCurrent: authority?.prepareCurrent }; } const callerReadPreparers = /* @__PURE__ */ new WeakMap(); const toolAuthorityReadScope = new AsyncLocalStorage(); const workerToolPreparations = /* @__PURE__ */ new WeakMap(); function bindWorkerToolPreparation(preparation, dependencies = []) { workerToolPreparations.set(preparation.prepareCurrent, { complete: dependencies.every((dependency) => workerToolPreparations.get(dependency.prepareCurrent)?.complete === true) }); return preparation; } function isToolAuthorityReadCaptureActive() { return toolAuthorityReadScope.getStore() !== void 0; } function recordPreparedToolAuthorityRead(read) { toolAuthorityReadScope.getStore()?.reads.push(read); } async function capturePreparedToolAuthorityReads(preparation) { const metadata = workerToolPreparations.get(preparation.prepareCurrent); const scope = { reads: [], complete: metadata?.complete ?? false }; if (metadata) await toolAuthorityReadScope.run(scope, preparation.prepareCurrent); else await preparation.prepareCurrent(); preparation.assertCurrent(); return { reads: scope.reads, assertCompatibility: !scope.complete || !scope.reads.length ? preparation.compatAssertCurrent : void 0 }; } function bindReplyToolAuthorityCallerRead(projector, prepare) { callerReadPreparers.set(projector, prepare); } async function prepareReplyToolAuthorityCallerRead(projector, caller, fingerprint, route, assertCurrent) { assertCurrent(); const prepared = projector && fingerprint ? await callerReadPreparers.get(projector)?.(caller, fingerprint, route, assertCurrent) : void 0; const scope = toolAuthorityReadScope.getStore(); if (scope && !prepared) scope.complete = false; return prepared; } function assertQuestionCompatibilityCurrent(assertActive, callerRead, fallback) { assertActive(); const native = callerRead?.retainNative(); try { if (native) native.assertCurrent(); else fallback(); } finally { native?.release(); } assertActive(); } /** Compose captured policy reads with the question owner's final synchronous admission. */ async function prepareQuestionInputAuthority(authority) { const preparation = authority.toolAuthorityPreparation; if (!preparation) return authority; const assertActive = () => { authority.assertCurrent(); preparation.assertCurrent(); }; assertActive(); const captured = await capturePreparedToolAuthorityReads(preparation); assertActive(); const supported = !captured.assertCompatibility && captured.reads.length > 0 && captured.reads.every((read) => read.retainNative); const assertLegacyReads = () => { for (const read of captured.reads) read.assertLegacyCurrent(); }; const prepareCurrent = async () => { assertActive(); await preparation.prepareCurrent(); if (!supported) assertLegacyReads(); assertActive(); }; const callerRead = supported ? { reads: captured.reads.flatMap((read) => read.reads), prepareCurrent, assertPrepared: (reads) => { assertActive(); let offset = 0; for (const read of captured.reads) { read.assertPrepared(reads.slice(offset, offset + read.reads.length)); offset += read.reads.length; } assertActive(); }, retainNative: () => { const retained = []; const release = () => { const errors = []; for (const read of retained.toReversed()) try { read.release(); } catch (error) { errors.push(error); } throwSqliteLifecycleErrors(errors, "Question authority reader release failed"); }; try { for (const read of captured.reads) retained.push(read.retainNative()); return { release, assertCurrent: () => { assertActive(); retained.forEach((read) => read.assertCurrent()); assertActive(); } }; } catch (error) { release(); throw error; } } } : void 0; const assertCompatibilityCurrent = () => assertQuestionCompatibilityCurrent(assertActive, callerRead, () => { preparation.compatAssertCurrent(); assertLegacyReads(); }); const assertCurrent = supported ? assertActive : () => { assertActive(); preparation.compatAssertCurrent(); assertActive(); }; bindQuestionDispatchCapability(assertCurrent, { assertCompatibilityCurrent, callerRead }); return { kind: authority.kind, assertCurrent, prepareCurrent }; } async function prepareQuestionCallerAuthority(authority, caller, assertActive) { assertActive(); const callerRead = await authority.prepareCaller?.(caller); assertActive(); const assertCompatibilityCurrent = () => assertQuestionCompatibilityCurrent(assertActive, callerRead, () => authority.assertCaller(caller)); const assertCurrent = callerRead ? assertActive : assertCompatibilityCurrent; bindQuestionDispatchCapability(assertCurrent, { assertCompatibilityCurrent, callerRead }); return { kind: "source-bound", assertCurrent, prepareCurrent: async () => { assertActive(); if (callerRead) await callerRead.prepareCurrent(); else authority.assertCaller(caller); assertActive(); } }; } const questionAnswerScope = new AsyncLocalStorage(); const questionAnswerCapabilities = /* @__PURE__ */ new WeakMap(); /** Retain the creator's prepared policy; a matching hash alone never grants authority. */ function createAgentQuestionAnswerAuthority(params) { return Object.freeze({ sessionKey: params.sessionKey.trim(), requesterProfileId: params.requesterProfileId, assertActive: params.assertActive, admitTranscriptAnswer: params.admitTranscriptAnswer, prepareCaller: params.prepareCaller, assertCaller: (caller) => { params.assertActive(); const projected = params.project(caller); params.assertActive(); if (!params.fingerprint || projected !== params.fingerprint) throw new Error("question answer caller policy does not match its creator"); } }); } function withAgentQuestionAnswerAuthority(authority, run) { return questionAnswerScope.run(authority, run); } function registerAgentHarnessQuestionAnswerAuthority(hostCapabilities, authority) { questionAnswerCapabilities.set(hostCapabilities, authority); } /** An explicit host carrier cannot fall back to an unrelated ambient creator. */ function resolveAgentQuestionAnswerAuthority(hostCapabilities) { return hostCapabilities ? questionAnswerCapabilities.get(hostCapabilities) : questionAnswerScope.getStore(); } function captureAgentQuestionAnswerAuthority(sessionKey) { const authority = questionAnswerScope.getStore(); authority?.assertActive(); if (authority && authority.sessionKey !== sessionKey.trim()) throw new Error("question creator authority belongs to another session"); return authority; } const retainedBeforeToolCallRunners = /* @__PURE__ */ new WeakMap(); /** Retain issued policy without importing the capability constructor and its tool graph. */ function retainBeforeToolCallForNativeHookRelay(runBeforeToolCall) { return retainedBeforeToolCallRunners.get(runBeforeToolCall)?.(); } function registerAgentHarnessBeforeToolCallRetention(runBeforeToolCall, retain) { retainedBeforeToolCallRunners.set(runBeforeToolCall, retain); } const scheduledToolProjectionCapabilities = /* @__PURE__ */ new WeakMap(); const ttsProvenanceTransferCapabilities = /* @__PURE__ */ new WeakMap(); function registerAgentHarnessScheduledToolProjectionCapability(params) { scheduledToolProjectionCapabilities.set(params.hostCapabilities, Object.freeze({ ownerPluginId: params.ownerPluginId, create: params.create })); } /** Resolves a private issuer only for the exact authoritative plugin owner. */ function resolveAgentHarnessScheduledToolProjectionCapability(params) { const capability = scheduledToolProjectionCapabilities.get(params.hostCapabilities); return capability?.ownerPluginId === params.ownerPluginId ? capability.create : void 0; } function registerAgentHarnessTtsProvenanceTransferCapability(params) { ttsProvenanceTransferCapabilities.set(params.hostCapabilities, Object.freeze({ ownerPluginId: params.ownerPluginId, transfer: params.transfer })); } /** Resolves private TTS delivery transfer only for the exact authoritative plugin owner. */ function resolveAgentHarnessTtsProvenanceTransferCapability(params) { const capability = ttsProvenanceTransferCapabilities.get(params.hostCapabilities); return capability?.ownerPluginId === params.ownerPluginId ? capability.transfer : void 0; } //#endregion export { withAgentQuestionAnswerAuthority as S, registerAgentHarnessTtsProvenanceTransferCapability as _, capturePreparedToolAuthorityReads as a, resolveAgentQuestionAnswerAuthority as b, prepareQuestionCallerAuthority as c, prepareReplyToolAuthorityCallerRead as d, readQuestionDispatchCapability as f, registerAgentHarnessScheduledToolProjectionCapability as g, registerAgentHarnessQuestionAnswerAuthority as h, captureAgentQuestionAnswerAuthority as i, prepareQuestionGatewayDispatch as l, registerAgentHarnessBeforeToolCallRetention as m, bindReplyToolAuthorityCallerRead as n, createAgentQuestionAnswerAuthority as o, recordPreparedToolAuthorityRead as p, bindWorkerToolPreparation as r, isToolAuthorityReadCaptureActive as s, bindQuestionDispatchGuard as t, prepareQuestionInputAuthority as u, resolveAgentHarnessScheduledToolProjectionCapability as v, retainBeforeToolCallForNativeHookRelay as x, resolveAgentHarnessTtsProvenanceTransferCapability as y };