mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 01:47:27 +00:00
fix(hooks): backfill reset command hooks for native /new path
This commit is contained in:
committed by
Peter Steinberger
parent
bbdf895d42
commit
aec41a588b
@@ -39,6 +39,96 @@ import { routeReply } from "./route-reply.js";
|
||||
|
||||
let HANDLERS: CommandHandler[] | null = null;
|
||||
|
||||
export type ResetCommandAction = "new" | "reset";
|
||||
|
||||
export async function emitResetCommandHooks(params: {
|
||||
action: ResetCommandAction;
|
||||
ctx: HandleCommandsParams["ctx"];
|
||||
cfg: HandleCommandsParams["cfg"];
|
||||
command: Pick<
|
||||
HandleCommandsParams["command"],
|
||||
"surface" | "senderId" | "channel" | "from" | "to" | "resetHookTriggered"
|
||||
>;
|
||||
sessionKey?: string;
|
||||
sessionEntry?: HandleCommandsParams["sessionEntry"];
|
||||
previousSessionEntry?: HandleCommandsParams["previousSessionEntry"];
|
||||
workspaceDir: string;
|
||||
}): Promise<void> {
|
||||
const hookEvent = createInternalHookEvent("command", params.action, params.sessionKey ?? "", {
|
||||
sessionEntry: params.sessionEntry,
|
||||
previousSessionEntry: params.previousSessionEntry,
|
||||
commandSource: params.command.surface,
|
||||
senderId: params.command.senderId,
|
||||
cfg: params.cfg, // Pass config for LLM slug generation
|
||||
});
|
||||
await triggerInternalHook(hookEvent);
|
||||
params.command.resetHookTriggered = true;
|
||||
|
||||
// Send hook messages immediately if present
|
||||
if (hookEvent.messages.length > 0) {
|
||||
// Use OriginatingChannel/To if available, otherwise fall back to command channel/from
|
||||
// oxlint-disable-next-line typescript/no-explicit-any
|
||||
const channel = params.ctx.OriginatingChannel || (params.command.channel as any);
|
||||
// For replies, use 'from' (the sender) not 'to' (which might be the bot itself)
|
||||
const to = params.ctx.OriginatingTo || params.command.from || params.command.to;
|
||||
|
||||
if (channel && to) {
|
||||
const hookReply = { text: hookEvent.messages.join("\n\n") };
|
||||
await routeReply({
|
||||
payload: hookReply,
|
||||
channel: channel,
|
||||
to: to,
|
||||
sessionKey: params.sessionKey,
|
||||
accountId: params.ctx.AccountId,
|
||||
threadId: params.ctx.MessageThreadId,
|
||||
cfg: params.cfg,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Fire before_reset plugin hook — extract memories before session history is lost
|
||||
const hookRunner = getGlobalHookRunner();
|
||||
if (hookRunner?.hasHooks("before_reset")) {
|
||||
const prevEntry = params.previousSessionEntry;
|
||||
const sessionFile = prevEntry?.sessionFile;
|
||||
// Fire-and-forget: read old session messages and run hook
|
||||
void (async () => {
|
||||
try {
|
||||
const messages: unknown[] = [];
|
||||
if (sessionFile) {
|
||||
const content = await fs.readFile(sessionFile, "utf-8");
|
||||
for (const line of content.split("\n")) {
|
||||
if (!line.trim()) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
const entry = JSON.parse(line);
|
||||
if (entry.type === "message" && entry.message) {
|
||||
messages.push(entry.message);
|
||||
}
|
||||
} catch {
|
||||
// skip malformed lines
|
||||
}
|
||||
}
|
||||
} else {
|
||||
logVerbose("before_reset: no session file available, firing hook with empty messages");
|
||||
}
|
||||
await hookRunner.runBeforeReset(
|
||||
{ sessionFile, messages, reason: params.action },
|
||||
{
|
||||
agentId: params.sessionKey?.split(":")[0] ?? "main",
|
||||
sessionKey: params.sessionKey,
|
||||
sessionId: prevEntry?.sessionId,
|
||||
workspaceDir: params.workspaceDir,
|
||||
},
|
||||
);
|
||||
} catch (err: unknown) {
|
||||
logVerbose(`before_reset hook failed: ${String(err)}`);
|
||||
}
|
||||
})();
|
||||
}
|
||||
}
|
||||
|
||||
export async function handleCommands(params: HandleCommandsParams): Promise<CommandHandlerResult> {
|
||||
if (HANDLERS === null) {
|
||||
HANDLERS = [
|
||||
@@ -79,79 +169,17 @@ export async function handleCommands(params: HandleCommandsParams): Promise<Comm
|
||||
|
||||
// Trigger internal hook for reset/new commands
|
||||
if (resetRequested && params.command.isAuthorizedSender) {
|
||||
const commandAction = resetMatch?.[1] ?? "new";
|
||||
const hookEvent = createInternalHookEvent("command", commandAction, params.sessionKey ?? "", {
|
||||
const commandAction: ResetCommandAction = resetMatch?.[1] === "reset" ? "reset" : "new";
|
||||
await emitResetCommandHooks({
|
||||
action: commandAction,
|
||||
ctx: params.ctx,
|
||||
cfg: params.cfg,
|
||||
command: params.command,
|
||||
sessionKey: params.sessionKey,
|
||||
sessionEntry: params.sessionEntry,
|
||||
previousSessionEntry: params.previousSessionEntry,
|
||||
commandSource: params.command.surface,
|
||||
senderId: params.command.senderId,
|
||||
cfg: params.cfg, // Pass config for LLM slug generation
|
||||
workspaceDir: params.workspaceDir,
|
||||
});
|
||||
await triggerInternalHook(hookEvent);
|
||||
|
||||
// Send hook messages immediately if present
|
||||
if (hookEvent.messages.length > 0) {
|
||||
// Use OriginatingChannel/To if available, otherwise fall back to command channel/from
|
||||
// oxlint-disable-next-line typescript/no-explicit-any
|
||||
const channel = params.ctx.OriginatingChannel || (params.command.channel as any);
|
||||
// For replies, use 'from' (the sender) not 'to' (which might be the bot itself)
|
||||
const to = params.ctx.OriginatingTo || params.command.from || params.command.to;
|
||||
|
||||
if (channel && to) {
|
||||
const hookReply = { text: hookEvent.messages.join("\n\n") };
|
||||
await routeReply({
|
||||
payload: hookReply,
|
||||
channel: channel,
|
||||
to: to,
|
||||
sessionKey: params.sessionKey,
|
||||
accountId: params.ctx.AccountId,
|
||||
threadId: params.ctx.MessageThreadId,
|
||||
cfg: params.cfg,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Fire before_reset plugin hook — extract memories before session history is lost
|
||||
const hookRunner = getGlobalHookRunner();
|
||||
if (hookRunner?.hasHooks("before_reset")) {
|
||||
const prevEntry = params.previousSessionEntry;
|
||||
const sessionFile = prevEntry?.sessionFile;
|
||||
// Fire-and-forget: read old session messages and run hook
|
||||
void (async () => {
|
||||
try {
|
||||
const messages: unknown[] = [];
|
||||
if (sessionFile) {
|
||||
const content = await fs.readFile(sessionFile, "utf-8");
|
||||
for (const line of content.split("\n")) {
|
||||
if (!line.trim()) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
const entry = JSON.parse(line);
|
||||
if (entry.type === "message" && entry.message) {
|
||||
messages.push(entry.message);
|
||||
}
|
||||
} catch {
|
||||
// skip malformed lines
|
||||
}
|
||||
}
|
||||
} else {
|
||||
logVerbose("before_reset: no session file available, firing hook with empty messages");
|
||||
}
|
||||
await hookRunner.runBeforeReset(
|
||||
{ sessionFile, messages, reason: commandAction },
|
||||
{
|
||||
agentId: params.sessionKey?.split(":")[0] ?? "main",
|
||||
sessionKey: params.sessionKey,
|
||||
sessionId: prevEntry?.sessionId,
|
||||
workspaceDir: params.workspaceDir,
|
||||
},
|
||||
);
|
||||
} catch (err: unknown) {
|
||||
logVerbose(`before_reset hook failed: ${String(err)}`);
|
||||
}
|
||||
})();
|
||||
}
|
||||
}
|
||||
|
||||
const allowTextCommands = shouldHandleTextCommands({
|
||||
|
||||
Reference in New Issue
Block a user