openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
51 lines (50 loc) • 2.61 kB
JavaScript
import { t as createSubsystemLogger } from "./subsystem-BHJiy01V.mjs";
import { r as extractAssistantTranscriptSourceText } from "./chat-message-content-DNUdbbJ8.mjs";
import { t as applyTranscriptSenderIdentityToWrite } from "./user-turn-transcript.metadata-GFtsH_a_.mjs";
import { n as getGlobalHookRunner } from "./hook-runner-global-DmYyIVDp.mjs";
import { i as consumeAdjustedParamsForToolCall } from "./agent-tools.before-tool-call.state-C5F7vnXY.mjs";
//#region src/agents/harness/hook-helpers.ts
const log = createSubsystemLogger("agents/harness");
/** Runs best-effort after-tool-call hooks for a completed tool invocation. */
async function runAgentHarnessAfterToolCallHook(e) {
const t = consumeAdjustedParamsForToolCall(e.toolCallId, e.runId);
const n = t && typeof t === "object" ? t : e.startArgs;
const r = getGlobalHookRunner();
if (!r?.hasHooks("after_tool_call")) return;
try {
await r.runAfterToolCall({
toolName: e.toolName,
params: structuredClone(n),
...e.runId ? { runId: e.runId } : {},
toolCallId: e.toolCallId,
...e.result ? { result: e.result } : {},
...e.error ? { error: e.error } : {},
...e.startedAt != null ? { durationMs: Date.now() - e.startedAt } : {}
}, {
toolName: e.toolName,
...e.agentId ? { agentId: e.agentId } : {},
...e.sessionId ? { sessionId: e.sessionId } : {},
...e.sessionKey ? { sessionKey: e.sessionKey } : {},
...e.runId ? { runId: e.runId } : {},
...e.channelId ? { channelId: e.channelId } : {},
toolCallId: e.toolCallId
});
} catch (t) {
log.warn(`after_tool_call hook failed: tool=${e.toolName} error=${String(t)}`);
}
}
/** Runs before-message-write hooks and returns the possibly rewritten message. */
function runAgentHarnessBeforeMessageWriteHook(e) {
const t = e.prepareAssistantTranscriptMessage && e.message.role === "assistant" && Reflect.get(e.message, "display") !== false ? extractAssistantTranscriptSourceText(e.message) : void 0;
const n = getGlobalHookRunner();
const r = !e.skipBeforeMessageWriteHooks && n?.hasHooks("before_message_write") ? applyTranscriptSenderIdentityToWrite(e.message, () => {
const t = n.runBeforeMessageWrite({ message: e.message }, {
agentId: e.agentId,
sessionKey: e.sessionKey
});
return t?.block ? null : t?.message ?? e.message;
}) ?? null : e.message;
return r?.role === "assistant" && Reflect.get(r, "display") !== false && t !== void 0 && e.prepareAssistantTranscriptMessage ? e.prepareAssistantTranscriptMessage(r, t) : r;
}
//#endregion
export { runAgentHarnessBeforeMessageWriteHook as n, runAgentHarnessAfterToolCallHook as t };