fix(hooks): prefer shared outbound conversation context

This commit is contained in:
Vincent Koc
2026-04-22 11:49:25 -07:00
parent 62a4abbc9f
commit 5fbafa7e47
2 changed files with 29 additions and 1 deletions

View File

@@ -88,6 +88,30 @@ describe("thread-ownership plugin", () => {
);
});
it("prefers shared conversationId over non-canonical Slack target shapes", async () => {
vi.mocked(globalThis.fetch).mockResolvedValue(
new Response(JSON.stringify({ owner: "test-agent" }), { status: 200 }),
);
const result = await hooks.message_sending(
{
content: "hello",
replyToId: "1234.5678",
to: "channel:C123",
},
{ channelId: "slack", conversationId: "C123" },
);
expect(result).toBeUndefined();
expect(globalThis.fetch).toHaveBeenCalledWith(
"http://localhost:8750/api/v1/ownership/C123/1234.5678",
expect.objectContaining({
method: "POST",
body: JSON.stringify({ agent_id: "test-agent" }),
}),
);
});
it("cancels when thread owned by another agent", async () => {
vi.mocked(globalThis.fetch).mockResolvedValue(
new Response(JSON.stringify({ owner: "other-agent" }), { status: 409 }),

View File

@@ -104,7 +104,11 @@ export default definePluginEntry({
resolveThreadToken(event.threadId) ||
resolveThreadToken(event.metadata?.threadId) ||
resolveThreadToken(event.metadata?.threadTs);
const channelId = (event.metadata?.channelId as string) ?? event.to;
const channelId =
normalizeOptionalString(ctx.conversationId) ||
normalizeOptionalString(event.metadata?.channelId) ||
normalizeOptionalString(event.to) ||
"";
if (!threadTs) {
return undefined;
}