mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 02:11:23 +00:00
fix: wire 9 unwired plugin hooks to core code (openclaw#14882) thanks @shtse8
Verified: - GitHub CI checks green (non-skipped) Co-authored-by: shtse8 <8020099+shtse8@users.noreply.github.com>
This commit is contained in:
@@ -2,6 +2,7 @@ import type { AgentEvent } from "@mariozechner/pi-agent-core";
|
||||
import type { EmbeddedPiSubscribeContext } from "./pi-embedded-subscribe.handlers.types.js";
|
||||
import { emitAgentEvent } from "../infra/agent-events.js";
|
||||
import { createInlineCodeState } from "../markdown/code-spans.js";
|
||||
import { getGlobalHookRunner } from "../plugins/hook-runner-global.js";
|
||||
|
||||
export function handleAgentStart(ctx: EmbeddedPiSubscribeContext) {
|
||||
ctx.log.debug(`embedded run agent start: runId=${ctx.params.runId}`);
|
||||
@@ -33,6 +34,21 @@ export function handleAutoCompactionStart(ctx: EmbeddedPiSubscribeContext) {
|
||||
stream: "compaction",
|
||||
data: { phase: "start" },
|
||||
});
|
||||
|
||||
// Run before_compaction plugin hook (fire-and-forget)
|
||||
const hookRunner = getGlobalHookRunner();
|
||||
if (hookRunner?.hasHooks("before_compaction")) {
|
||||
void hookRunner
|
||||
.runBeforeCompaction(
|
||||
{
|
||||
messageCount: ctx.params.session.messages?.length ?? 0,
|
||||
},
|
||||
{},
|
||||
)
|
||||
.catch((err) => {
|
||||
ctx.log.warn(`before_compaction hook failed: ${String(err)}`);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function handleAutoCompactionEnd(
|
||||
@@ -57,6 +73,24 @@ export function handleAutoCompactionEnd(
|
||||
stream: "compaction",
|
||||
data: { phase: "end", willRetry },
|
||||
});
|
||||
|
||||
// Run after_compaction plugin hook (fire-and-forget)
|
||||
if (!willRetry) {
|
||||
const hookRunnerEnd = getGlobalHookRunner();
|
||||
if (hookRunnerEnd?.hasHooks("after_compaction")) {
|
||||
void hookRunnerEnd
|
||||
.runAfterCompaction(
|
||||
{
|
||||
messageCount: ctx.params.session.messages?.length ?? 0,
|
||||
compactedCount: ctx.getCompactionCount(),
|
||||
},
|
||||
{},
|
||||
)
|
||||
.catch((err) => {
|
||||
ctx.log.warn(`after_compaction hook failed: ${String(err)}`);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function handleAgentEnd(ctx: EmbeddedPiSubscribeContext) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { AgentEvent } from "@mariozechner/pi-agent-core";
|
||||
import type { EmbeddedPiSubscribeContext } from "./pi-embedded-subscribe.handlers.types.js";
|
||||
import { emitAgentEvent } from "../infra/agent-events.js";
|
||||
import { getGlobalHookRunner } from "../plugins/hook-runner-global.js";
|
||||
import { normalizeTextForComparison } from "./pi-embedded-helpers.js";
|
||||
import { isMessagingTool, isMessagingToolSendAction } from "./pi-embedded-messaging.js";
|
||||
import {
|
||||
@@ -13,6 +14,8 @@ import {
|
||||
import { inferToolMetaFromArgs } from "./pi-embedded-utils.js";
|
||||
import { normalizeToolName } from "./tool-policy.js";
|
||||
|
||||
/** Track tool execution start times and args for after_tool_call hook */
|
||||
const toolStartData = new Map<string, { startTime: number; args: unknown }>();
|
||||
function extendExecMeta(toolName: string, args: unknown, meta?: string): string | undefined {
|
||||
const normalized = toolName.trim().toLowerCase();
|
||||
if (normalized !== "exec" && normalized !== "bash") {
|
||||
@@ -51,6 +54,9 @@ export async function handleToolExecutionStart(
|
||||
const toolCallId = String(evt.toolCallId);
|
||||
const args = evt.args;
|
||||
|
||||
// Track start time and args for after_tool_call hook
|
||||
toolStartData.set(toolCallId, { startTime: Date.now(), args });
|
||||
|
||||
if (toolName === "read") {
|
||||
const record = args && typeof args === "object" ? (args as Record<string, unknown>) : {};
|
||||
const filePath = typeof record.path === "string" ? record.path.trim() : "";
|
||||
@@ -226,4 +232,36 @@ export function handleToolExecutionEnd(
|
||||
ctx.emitToolOutput(toolName, meta, outputText);
|
||||
}
|
||||
}
|
||||
|
||||
// Run after_tool_call plugin hook (fire-and-forget)
|
||||
const hookRunner = getGlobalHookRunner();
|
||||
if (hookRunner?.hasHooks("after_tool_call")) {
|
||||
const startData = toolStartData.get(toolCallId);
|
||||
toolStartData.delete(toolCallId);
|
||||
const durationMs = startData?.startTime != null ? Date.now() - startData.startTime : undefined;
|
||||
const toolArgs = startData?.args;
|
||||
void hookRunner
|
||||
.runAfterToolCall(
|
||||
{
|
||||
toolName,
|
||||
params: (toolArgs && typeof toolArgs === "object" ? toolArgs : {}) as Record<
|
||||
string,
|
||||
unknown
|
||||
>,
|
||||
result: sanitizedResult,
|
||||
error: isToolError ? extractToolErrorMessage(sanitizedResult) : undefined,
|
||||
durationMs,
|
||||
},
|
||||
{
|
||||
toolName,
|
||||
agentId: undefined,
|
||||
sessionKey: undefined,
|
||||
},
|
||||
)
|
||||
.catch((err) => {
|
||||
ctx.log.warn(`after_tool_call hook failed: tool=${toolName} error=${String(err)}`);
|
||||
});
|
||||
} else {
|
||||
toolStartData.delete(toolCallId);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user