openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
53 lines (52 loc) • 2.47 kB
JavaScript
import { t as createSubsystemLogger } from "./subsystem-BzXSmsuh.js";
import { t as getGlobalHookRunner } from "./hook-runner-global-BnyQREr6.js";
import { i as consumeAdjustedParamsForToolCall } from "./agent-tools.before-tool-call-BkVrW03y.js";
//#region src/agents/harness/hook-helpers.ts
/**
* Agent harness tool/message hook helpers.
*
* Harnesses use this to dispatch after-tool-call and before-message-write hooks
* while isolating hook failures from the runtime path.
*/
const log = createSubsystemLogger("agents/harness");
/** Runs best-effort after-tool-call hooks for a completed tool invocation. */
async function runAgentHarnessAfterToolCallHook(params) {
const hookRunner = getGlobalHookRunner();
if (!hookRunner?.hasHooks("after_tool_call")) return;
const adjustedArgs = consumeAdjustedParamsForToolCall(params.toolCallId, params.runId);
const eventArgs = adjustedArgs && typeof adjustedArgs === "object" ? adjustedArgs : params.startArgs;
try {
await hookRunner.runAfterToolCall({
toolName: params.toolName,
params: eventArgs,
...params.runId ? { runId: params.runId } : {},
toolCallId: params.toolCallId,
...params.result ? { result: params.result } : {},
...params.error ? { error: params.error } : {},
...params.startedAt != null ? { durationMs: Date.now() - params.startedAt } : {}
}, {
toolName: params.toolName,
...params.agentId ? { agentId: params.agentId } : {},
...params.sessionId ? { sessionId: params.sessionId } : {},
...params.sessionKey ? { sessionKey: params.sessionKey } : {},
...params.runId ? { runId: params.runId } : {},
...params.channelId ? { channelId: params.channelId } : {},
toolCallId: params.toolCallId
});
} catch (error) {
log.warn(`after_tool_call hook failed: tool=${params.toolName} error=${String(error)}`);
}
}
/** Runs before-message-write hooks and returns the possibly rewritten message. */
function runAgentHarnessBeforeMessageWriteHook(params) {
const hookRunner = getGlobalHookRunner();
if (!hookRunner?.hasHooks("before_message_write")) return params.message;
const result = hookRunner.runBeforeMessageWrite({ message: params.message }, {
...params.agentId ? { agentId: params.agentId } : {},
...params.sessionKey ? { sessionKey: params.sessionKey } : {}
});
if (result?.block) return null;
return result?.message ?? params.message;
}
//#endregion
export { runAgentHarnessBeforeMessageWriteHook as n, runAgentHarnessAfterToolCallHook as t };