UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

154 lines (153 loc) • 6.74 kB
//#region packages/agent-core/src/internal-hooks.ts const repetitionGuardByAgent = /* @__PURE__ */ new WeakMap(); /** Host policy only: no plugin-facing option or user setting. */ function setInternalRepetitionGuard(e, t) { repetitionGuardByAgent.set(e, t); } function getInternalRepetitionGuard(e) { return repetitionGuardByAgent.get(e); } const beforeToolBatchByAgent = /* @__PURE__ */ new WeakMap(); const toolTurnCompletionByAgent = /* @__PURE__ */ new WeakMap(); const toolTurnInputScopeByHook = /* @__PURE__ */ new WeakMap(); /** Keep the accepted input identity across same-input Agent.continue() calls. */ function getInternalToolTurnInputScope(e) { let t = toolTurnInputScopeByHook.get(e); if (!t) { t = {}; toolTurnInputScopeByHook.set(e, t); } return t; } /** Queueing, cancellation and context-only updates do not commit a new input. */ function commitInternalToolTurnInput(e) { if (e) toolTurnInputScopeByHook.set(e, {}); } /** A source-start veto, translated into an unstarted outcome rather than a fatal error. */ var InternalToolLoopBlocked = class extends Error { constructor(e) { super(e.reason); this.intervention = e; } }; const toolBatchLifecycleByResult = /* @__PURE__ */ new WeakMap(); const syncSteeringGetterByCallback = /* @__PURE__ */ new WeakMap(); const steeringQueueObserverByCallback = /* @__PURE__ */ new WeakMap(); const toolExecutionPreparerByTool = /* @__PURE__ */ new WeakMap(); const toolResultAcknowledgementByValue = /* @__PURE__ */ new WeakMap(); const toolResultProvenanceByValue = /* @__PURE__ */ new WeakMap(); const toolResultContentSourceByValue = /* @__PURE__ */ new WeakMap(); /** Copy present private state without clearing a target's existing slot. */ function copyInternalSlot(e, t, n) { const r = e.get(t); if (r) e.set(n, r); } /** Install OpenClaw-owned loop control without adding a plugin-facing Agent option. */ function setInternalBeforeToolBatch(e, t) { if (t) beforeToolBatchByAgent.set(e, t); else beforeToolBatchByAgent.delete(e); } function getInternalBeforeToolBatch(e) { return beforeToolBatchByAgent.get(e); } function setInternalToolTurnCompletion(e, t) { if (t) toolTurnCompletionByAgent.set(e, t); else toolTurnCompletionByAgent.delete(e); } function getInternalToolTurnCompletion(e) { return toolTurnCompletionByAgent.get(e); } /** Attach scheduler lifecycle ownership without widening the public admission result. */ function attachInternalToolBatchLifecycle(e, t) { toolBatchLifecycleByResult.set(e, t); return e; } function takeInternalToolBatchLifecycle(e) { const t = toolBatchLifecycleByResult.get(e); toolBatchLifecycleByResult.delete(e); return t; } /** Attach Agent-owned synchronous draining to the exact public async callback identity. */ function attachInternalSyncSteeringGetter(e, t, n) { syncSteeringGetterByCallback.set(e, t); attachInternalSteeringQueueObserver(e, n); return e; } /** Wrapping a getter must retain observation of its exact underlying queue. */ function attachInternalSteeringQueueObserver(e, t) { if (t) steeringQueueObserverByCallback.set(e, t); } function getInternalSteeringQueueObserver(e) { return e ? steeringQueueObserverByCallback.get(e) : void 0; } function getInternalSyncSteeringGetter(e) { return syncSteeringGetterByCallback.get(e); } /** Attach OpenClaw-owned two-phase execution without changing the public AgentTool shape. */ function attachInternalToolExecutionPreparer(e, t) { toolExecutionPreparerByTool.set(e, t); return e; } function getInternalToolExecutionPreparer(e) { return toolExecutionPreparerByTool.get(e); } /** Preserve private execution ownership when an adapter replaces a tool object. */ function copyInternalToolExecutionPreparer(e, t) { copyInternalSlot(toolExecutionPreparerByTool, e, t); return t; } /** Keep a destructive tool-side commit behind the result persistence boundary. */ function attachInternalToolResultAcknowledgement(e, t) { toolResultAcknowledgementByValue.set(e, t); return e; } function attachInternalToolResultProvenance(e, t) { if (t) toolResultProvenanceByValue.set(e, t); else toolResultProvenanceByValue.delete(e); return e; } function getInternalToolResultProvenance(e) { return toolResultProvenanceByValue.get(e); } /** * Mark one result or thrown error as carrying content from a nested call whose * tool declares a result content source. Dispatchers such as Tool Search and * Code Mode use this because their own static declaration cannot describe each * call; finalization combines it with the executed tool's declaration. */ function attachInternalToolResultContentSource(e, t) { toolResultContentSourceByValue.set(e, t); return e; } function getInternalToolResultContentSource(e) { return toolResultContentSourceByValue.get(e); } /** Carry private commit ownership through result transforms and message construction. */ function copyInternalToolResultState(e, t) { copyInternalSlot(toolResultAcknowledgementByValue, e, t); copyInternalSlot(toolResultProvenanceByValue, e, t); copyInternalSlot(toolResultContentSourceByValue, e, t); return t; } /** * Feedback must not change no-progress hashes: call only after raw outcome * recording, or for rejected calls whose validation result admission captured. */ function appendToolLoopWarning(e, t) { return copyInternalToolResultState(e, { ...e, content: [...e.content ?? [], { type: "text", text: `[System note: Tool-loop warning after ${t.count} repeated calls. Change your approach or stop if you are not making progress.]` }] }); } /** Commit one tool result after its owning message has attached. */ function acknowledgeInternalToolResult(e) { const t = toolResultAcknowledgementByValue.get(e); if (!t) return; toolResultAcknowledgementByValue.delete(e); t(); } //#endregion export { setInternalBeforeToolBatch as C, takeInternalToolBatchLifecycle as E, getInternalToolTurnInputScope as S, setInternalToolTurnCompletion as T, getInternalSyncSteeringGetter as _, attachInternalSyncSteeringGetter as a, getInternalToolResultProvenance as b, attachInternalToolResultAcknowledgement as c, commitInternalToolTurnInput as d, copyInternalToolExecutionPreparer as f, getInternalSteeringQueueObserver as g, getInternalRepetitionGuard as h, attachInternalSteeringQueueObserver as i, attachInternalToolResultContentSource as l, getInternalBeforeToolBatch as m, acknowledgeInternalToolResult as n, attachInternalToolBatchLifecycle as o, copyInternalToolResultState as p, appendToolLoopWarning as r, attachInternalToolExecutionPreparer as s, InternalToolLoopBlocked as t, attachInternalToolResultProvenance as u, getInternalToolExecutionPreparer as v, setInternalRepetitionGuard as w, getInternalToolTurnCompletion as x, getInternalToolResultContentSource as y };