mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 04:31:23 +00:00
Synchronous hook that lets plugins inspect and optionally block messages
before they are written to the session JSONL file. Primary use case is
private mode... when enabled, the plugin returns { block: true } and the
message never gets persisted.
The hook runs on the hot path (synchronous, like tool_result_persist).
Handlers execute sequentially in priority order. If any handler returns
{ block: true }, the write is skipped immediately. Handlers can also
return a modified message to write instead of the original.
Changes:
- src/plugins/types.ts: add hook name, event/result types, handler map entry
- src/plugins/hooks.ts: add runBeforeMessageWrite() following tool_result_persist pattern
- src/agents/session-tool-result-guard.ts: invoke hook before every originalAppend() call
- src/agents/session-tool-result-guard-wrapper.ts: wire hook runner to the guard
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
72 lines
2.6 KiB
TypeScript
72 lines
2.6 KiB
TypeScript
import type { SessionManager } from "@mariozechner/pi-coding-agent";
|
|
import { getGlobalHookRunner } from "../plugins/hook-runner-global.js";
|
|
import {
|
|
applyInputProvenanceToUserMessage,
|
|
type InputProvenance,
|
|
} from "../sessions/input-provenance.js";
|
|
import { installSessionToolResultGuard } from "./session-tool-result-guard.js";
|
|
|
|
export type GuardedSessionManager = SessionManager & {
|
|
/** Flush any synthetic tool results for pending tool calls. Idempotent. */
|
|
flushPendingToolResults?: () => void;
|
|
};
|
|
|
|
/**
|
|
* Apply the tool-result guard to a SessionManager exactly once and expose
|
|
* a flush method on the instance for easy teardown handling.
|
|
*/
|
|
export function guardSessionManager(
|
|
sessionManager: SessionManager,
|
|
opts?: {
|
|
agentId?: string;
|
|
sessionKey?: string;
|
|
inputProvenance?: InputProvenance;
|
|
allowSyntheticToolResults?: boolean;
|
|
},
|
|
): GuardedSessionManager {
|
|
if (typeof (sessionManager as GuardedSessionManager).flushPendingToolResults === "function") {
|
|
return sessionManager as GuardedSessionManager;
|
|
}
|
|
|
|
const hookRunner = getGlobalHookRunner();
|
|
const beforeMessageWrite = hookRunner?.hasHooks("before_message_write")
|
|
? (event: { message: import("@mariozechner/pi-agent-core").AgentMessage }) => {
|
|
return hookRunner.runBeforeMessageWrite(event, {
|
|
agentId: opts?.agentId,
|
|
sessionKey: opts?.sessionKey,
|
|
});
|
|
}
|
|
: undefined;
|
|
|
|
const transform = hookRunner?.hasHooks("tool_result_persist")
|
|
? // oxlint-disable-next-line typescript/no-explicit-any
|
|
(message: any, meta: { toolCallId?: string; toolName?: string; isSynthetic?: boolean }) => {
|
|
const out = hookRunner.runToolResultPersist(
|
|
{
|
|
toolName: meta.toolName,
|
|
toolCallId: meta.toolCallId,
|
|
message,
|
|
isSynthetic: meta.isSynthetic,
|
|
},
|
|
{
|
|
agentId: opts?.agentId,
|
|
sessionKey: opts?.sessionKey,
|
|
toolName: meta.toolName,
|
|
toolCallId: meta.toolCallId,
|
|
},
|
|
);
|
|
return out?.message ?? message;
|
|
}
|
|
: undefined;
|
|
|
|
const guard = installSessionToolResultGuard(sessionManager, {
|
|
transformMessageForPersistence: (message) =>
|
|
applyInputProvenanceToUserMessage(message, opts?.inputProvenance),
|
|
transformToolResultForPersistence: transform,
|
|
allowSyntheticToolResults: opts?.allowSyntheticToolResults,
|
|
beforeMessageWriteHook: beforeMessageWrite,
|
|
});
|
|
(sessionManager as GuardedSessionManager).flushPendingToolResults = guard.flushPendingToolResults;
|
|
return sessionManager as GuardedSessionManager;
|
|
}
|