diff --git a/src/auto-reply/reply/commands-plugin.test.ts b/src/auto-reply/reply/commands-plugin.test.ts index 4fba6858029..0f7f156d4ff 100644 --- a/src/auto-reply/reply/commands-plugin.test.ts +++ b/src/auto-reply/reply/commands-plugin.test.ts @@ -72,4 +72,41 @@ describe("handlePluginCommand", () => { }), ); }); + + it("prefers the target session entry from sessionStore for plugin command metadata", async () => { + matchPluginCommandMock.mockReturnValue({ + command: { name: "card" }, + args: "", + }); + executePluginCommandMock.mockResolvedValue({ text: "from plugin" }); + + const params = buildPluginParams( + "/card", + { + commands: { text: true }, + channels: { whatsapp: { allowFrom: ["*"] } }, + } as OpenClawConfig, + ); + params.sessionEntry = { + sessionId: "wrapper-session", + sessionFile: "/tmp/wrapper-session.jsonl", + updatedAt: Date.now(), + } as HandleCommandsParams["sessionEntry"]; + params.sessionStore = { + [params.sessionKey]: { + sessionId: "target-session", + sessionFile: "/tmp/target-session.jsonl", + updatedAt: Date.now(), + }, + }; + + await handlePluginCommand(params, true); + + expect(executePluginCommandMock).toHaveBeenCalledWith( + expect.objectContaining({ + sessionId: "target-session", + sessionFile: "/tmp/target-session.jsonl", + }), + ); + }); }); diff --git a/src/auto-reply/reply/commands-plugin.ts b/src/auto-reply/reply/commands-plugin.ts index 18f0e9cf37e..eec3303d17e 100644 --- a/src/auto-reply/reply/commands-plugin.ts +++ b/src/auto-reply/reply/commands-plugin.ts @@ -19,6 +19,7 @@ export const handlePluginCommand: CommandHandler = async ( allowTextCommands, ): Promise => { const { command, cfg } = params; + const targetSessionEntry = params.sessionStore?.[params.sessionKey] ?? params.sessionEntry; if (!allowTextCommands) { return null; @@ -40,8 +41,8 @@ export const handlePluginCommand: CommandHandler = async ( isAuthorizedSender: command.isAuthorizedSender, gatewayClientScopes: params.ctx.GatewayClientScopes, sessionKey: params.sessionKey, - sessionId: params.sessionEntry?.sessionId, - sessionFile: params.sessionEntry?.sessionFile, + sessionId: targetSessionEntry?.sessionId, + sessionFile: targetSessionEntry?.sessionFile, commandBody: command.commandBodyNormalized, config: cfg, from: command.from,