mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 21:48:27 +00:00
feat: add tool_result_persist hook
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import type { SessionManager } from "@mariozechner/pi-coding-agent";
|
||||
|
||||
import { getGlobalHookRunner } from "../plugins/hook-runner-global.js";
|
||||
import { installSessionToolResultGuard } from "./session-tool-result-guard.js";
|
||||
|
||||
export type GuardedSessionManager = SessionManager & {
|
||||
@@ -11,12 +12,38 @@ export type GuardedSessionManager = SessionManager & {
|
||||
* 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): GuardedSessionManager {
|
||||
export function guardSessionManager(
|
||||
sessionManager: SessionManager,
|
||||
opts?: { agentId?: string; sessionKey?: string },
|
||||
): GuardedSessionManager {
|
||||
if (typeof (sessionManager as GuardedSessionManager).flushPendingToolResults === "function") {
|
||||
return sessionManager as GuardedSessionManager;
|
||||
}
|
||||
|
||||
const guard = installSessionToolResultGuard(sessionManager);
|
||||
const hookRunner = getGlobalHookRunner();
|
||||
const transform = hookRunner?.hasHooks("tool_result_persist")
|
||||
? (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, {
|
||||
transformToolResultForPersistence: transform,
|
||||
});
|
||||
(sessionManager as GuardedSessionManager).flushPendingToolResults = guard.flushPendingToolResults;
|
||||
return sessionManager as GuardedSessionManager;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user