fix: honor default account in plugin commands

This commit is contained in:
Tak Hoffman
2026-04-03 16:13:24 -05:00
parent 5a94909654
commit 001e0c1f65
2 changed files with 74 additions and 1 deletions

View File

@@ -536,4 +536,76 @@ describe("registerPluginCommand", () => {
sessionId: "session-123",
});
});
it("passes the effective default account to plugin command handlers when accountId is omitted", async () => {
setActivePluginRegistry(
createTestRegistry([
{
pluginId: "line",
source: "test",
plugin: {
...createChannelTestPluginBase({
id: "line",
label: "LINE",
config: {
listAccountIds: () => ["default", "work"],
defaultAccountId: () => "work",
resolveAccount: (_cfg, accountId) => ({ accountId: accountId ?? "work" }),
},
}),
bindings: {
resolveCommandConversation: ({
originatingTo,
commandTo,
fallbackTo,
}: {
originatingTo?: string;
commandTo?: string;
fallbackTo?: string;
}) => {
const rawTarget = [originatingTo, commandTo, fallbackTo].find(Boolean)?.trim();
if (!rawTarget) {
return null;
}
return {
conversationId: rawTarget.replace(/^line:/i, "").replace(/^user:/i, ""),
};
},
},
},
},
]),
);
let receivedCtx:
| {
accountId?: string;
}
| undefined;
const handler = async (ctx: { accountId?: string }) => {
receivedCtx = ctx;
return { text: "ok" };
};
const result = await executePluginCommand({
command: {
name: "accountcheck",
description: "Demo command",
acceptsArgs: false,
handler,
pluginId: "demo-plugin",
},
channel: "line",
senderId: "U123",
isAuthorizedSender: true,
commandBody: "/accountcheck",
config: {} as never,
from: "line:user:U1234567890abcdef1234567890abcdef",
});
expect(result).toEqual({ text: "ok" });
expect(receivedCtx).toMatchObject({
accountId: "work",
});
});
});

View File

@@ -191,6 +191,7 @@ export async function executePluginCommand(params: {
messageThreadId: params.messageThreadId,
threadParentId: params.threadParentId,
});
const effectiveAccountId = bindingConversation?.accountId ?? params.accountId;
const ctx: PluginCommandContext = {
senderId,
@@ -205,7 +206,7 @@ export async function executePluginCommand(params: {
config,
from: params.from,
to: params.to,
accountId: params.accountId,
accountId: effectiveAccountId,
messageThreadId: params.messageThreadId,
threadParentId: params.threadParentId,
requestConversationBinding: async (bindingParams) => {