feat(pairing): show sender ids across providers

This commit is contained in:
Peter Steinberger
2026-01-08 23:19:13 +00:00
parent 7c7858a519
commit e0439df4ce
13 changed files with 156 additions and 2 deletions

View File

@@ -60,5 +60,28 @@ describe("pairing cli", () => {
expect.stringContaining("telegramUserId=123"),
);
});
});
it("labels Discord ids as discordUserId", async () => {
const { registerPairingCli } = await import("./pairing-cli.js");
listProviderPairingRequests.mockResolvedValueOnce([
{
id: "999",
code: "DEF456",
createdAt: "2026-01-08T00:00:00Z",
lastSeenAt: "2026-01-08T00:00:00Z",
meta: { tag: "Ada#0001" },
},
]);
const log = vi.spyOn(console, "log").mockImplementation(() => {});
const program = new Command();
program.name("test");
registerPairingCli(program);
await program.parseAsync(["pairing", "list", "--provider", "discord"], {
from: "user",
});
expect(log).toHaveBeenCalledWith(
expect.stringContaining("discordUserId=999"),
);
});
});

View File

@@ -22,6 +22,15 @@ const PROVIDERS: PairingProvider[] = [
"whatsapp",
];
const PROVIDER_ID_LABELS: Record<PairingProvider, string> = {
telegram: "telegramUserId",
discord: "discordUserId",
slack: "slackUserId",
signal: "signalNumber",
imessage: "imessageSenderId",
whatsapp: "whatsappSenderId",
};
function parseProvider(raw: unknown): PairingProvider {
const value = (
typeof raw === "string"
@@ -93,7 +102,7 @@ export function registerPairingCli(program: Command) {
}
for (const r of requests) {
const meta = r.meta ? JSON.stringify(r.meta) : "";
const idLabel = provider === "telegram" ? "telegramUserId" : "id";
const idLabel = PROVIDER_ID_LABELS[provider];
console.log(
`${r.code} ${idLabel}=${r.id}${meta ? ` meta=${meta}` : ""} ${r.createdAt}`,
);

View File

@@ -5,6 +5,8 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
const sendMock = vi.fn();
const updateLastRouteMock = vi.fn();
const dispatchMock = vi.fn();
const readAllowFromStoreMock = vi.fn();
const upsertPairingRequestMock = vi.fn();
vi.mock("./send.js", () => ({
sendMessageDiscord: (...args: unknown[]) => sendMock(...args),
@@ -12,6 +14,12 @@ vi.mock("./send.js", () => ({
vi.mock("../auto-reply/reply/dispatch-from-config.js", () => ({
dispatchReplyFromConfig: (...args: unknown[]) => dispatchMock(...args),
}));
vi.mock("../pairing/pairing-store.js", () => ({
readProviderAllowFromStore: (...args: unknown[]) =>
readAllowFromStoreMock(...args),
upsertProviderPairingRequest: (...args: unknown[]) =>
upsertPairingRequestMock(...args),
}));
vi.mock("../config/sessions.js", async (importOriginal) => {
const actual = await importOriginal<typeof import("../config/sessions.js")>();
return {
@@ -29,6 +37,10 @@ beforeEach(() => {
dispatcher.sendFinalReply({ text: "hi" });
return { queuedFinal: true, counts: { final: 1 } };
});
readAllowFromStoreMock.mockReset().mockResolvedValue([]);
upsertPairingRequestMock
.mockReset()
.mockResolvedValue({ code: "PAIRCODE", created: true });
vi.resetModules();
});
@@ -99,6 +111,76 @@ describe("discord tool result dispatch", () => {
expect(sendMock.mock.calls[0]?.[1]).toMatch(/^PFX /);
}, 10000);
it("replies with pairing code and sender id when dmPolicy is pairing", async () => {
const { createDiscordMessageHandler } = await import("./monitor.js");
const cfg = {
agent: { model: "anthropic/claude-opus-4-5", workspace: "/tmp/clawd" },
session: { store: "/tmp/clawdbot-sessions.json" },
discord: { dm: { enabled: true, policy: "pairing", allowFrom: [] } },
routing: { allowFrom: [] },
} as ReturnType<typeof import("../config/config.js").loadConfig>;
const handler = createDiscordMessageHandler({
cfg,
discordConfig: cfg.discord,
accountId: "default",
token: "token",
runtime: {
log: vi.fn(),
error: vi.fn(),
exit: (code: number): never => {
throw new Error(`exit ${code}`);
},
},
botUserId: "bot-id",
guildHistories: new Map(),
historyLimit: 0,
mediaMaxBytes: 10_000,
textLimit: 2000,
replyToMode: "off",
dmEnabled: true,
groupDmEnabled: false,
});
const client = {
fetchChannel: vi.fn().mockResolvedValue({
type: ChannelType.DM,
name: "dm",
}),
} as unknown as Client;
await handler(
{
message: {
id: "m1",
content: "hello",
channelId: "c1",
timestamp: new Date().toISOString(),
type: MessageType.Default,
attachments: [],
embeds: [],
mentionedEveryone: false,
mentionedUsers: [],
mentionedRoles: [],
author: { id: "u2", bot: false, username: "Ada" },
},
author: { id: "u2", bot: false, username: "Ada" },
guild_id: null,
},
client,
);
expect(dispatchMock).not.toHaveBeenCalled();
expect(upsertPairingRequestMock).toHaveBeenCalled();
expect(sendMock).toHaveBeenCalledTimes(1);
expect(String(sendMock.mock.calls[0]?.[1] ?? "")).toContain(
"Your Discord user id: u2",
);
expect(String(sendMock.mock.calls[0]?.[1] ?? "")).toContain(
"Pairing code: PAIRCODE",
);
}, 10000);
it("accepts guild messages when mentionPatterns match", async () => {
const { createDiscordMessageHandler } = await import("./monitor.js");
const cfg = {

View File

@@ -594,6 +594,8 @@ export function createDiscordMessageHandler(params: {
[
"Clawdbot: access not configured.",
"",
`Your Discord user id: ${author.id}`,
"",
`Pairing code: ${code}`,
"",
"Ask the bot owner to approve with:",
@@ -1436,6 +1438,8 @@ function createDiscordNativeCommand(params: {
content: [
"Clawdbot: access not configured.",
"",
`Your Discord user id: ${user.id}`,
"",
`Pairing code: ${code}`,
"",
"Ask the bot owner to approve with:",

View File

@@ -284,6 +284,9 @@ describe("monitorIMessageProvider", () => {
expect(replyMock).not.toHaveBeenCalled();
expect(upsertPairingRequestMock).toHaveBeenCalled();
expect(sendMock).toHaveBeenCalledTimes(1);
expect(String(sendMock.mock.calls[0]?.[1] ?? "")).toContain(
"Your iMessage sender id: +15550001111",
);
expect(String(sendMock.mock.calls[0]?.[1] ?? "")).toContain(
"Pairing code: PAIRCODE",
);

View File

@@ -259,6 +259,8 @@ export async function monitorIMessageProvider(
[
"Clawdbot: access not configured.",
"",
`Your iMessage sender id: ${senderId}`,
"",
`Pairing code: ${code}`,
"",
"Ask the bot owner to approve with:",

View File

@@ -146,6 +146,9 @@ describe("monitorSignalProvider tool results", () => {
expect(replyMock).not.toHaveBeenCalled();
expect(upsertPairingRequestMock).toHaveBeenCalled();
expect(sendMock).toHaveBeenCalledTimes(1);
expect(String(sendMock.mock.calls[0]?.[1] ?? "")).toContain(
"Your Signal number: +15550001111",
);
expect(String(sendMock.mock.calls[0]?.[1] ?? "")).toContain(
"Pairing code: PAIRCODE",
);
@@ -237,6 +240,9 @@ describe("monitorSignalProvider tool results", () => {
);
expect(sendMock).toHaveBeenCalledTimes(1);
expect(sendMock.mock.calls[0]?.[0]).toBe(`signal:${uuid}`);
expect(String(sendMock.mock.calls[0]?.[1] ?? "")).toContain(
`Your Signal sender id: uuid:${uuid}`,
);
});
it("reconnects after stream errors until aborted", async () => {

View File

@@ -317,6 +317,10 @@ export async function monitorSignalProvider(
const senderRecipient = resolveSignalRecipient(sender);
const senderPeerId = resolveSignalPeerId(sender);
const senderAllowId = formatSignalSenderId(sender);
const senderIdLine =
sender.kind === "phone"
? `Your Signal number: ${sender.e164}`
: `Your Signal sender id: ${senderAllowId}`;
if (!senderRecipient) return;
const groupId = dataMessage.groupInfo?.groupId ?? undefined;
const groupName = dataMessage.groupInfo?.groupName ?? undefined;
@@ -351,6 +355,8 @@ export async function monitorSignalProvider(
[
"Clawdbot: access not configured.",
"",
senderIdLine,
"",
`Pairing code: ${code}`,
"",
"Ask the bot owner to approve with:",

View File

@@ -567,6 +567,9 @@ describe("monitorSlackProvider tool results", () => {
expect(replyMock).not.toHaveBeenCalled();
expect(upsertPairingRequestMock).toHaveBeenCalled();
expect(sendMock).toHaveBeenCalledTimes(1);
expect(String(sendMock.mock.calls[0]?.[1] ?? "")).toContain(
"Your Slack user id: U1",
);
expect(String(sendMock.mock.calls[0]?.[1] ?? "")).toContain(
"Pairing code: PAIRCODE",
);

View File

@@ -827,6 +827,8 @@ export async function monitorSlackProvider(opts: MonitorSlackOpts = {}) {
[
"Clawdbot: access not configured.",
"",
`Your Slack user id: ${directUserId}`,
"",
`Pairing code: ${code}`,
"",
"Ask the bot owner to approve with:",
@@ -1720,6 +1722,8 @@ export async function monitorSlackProvider(opts: MonitorSlackOpts = {}) {
text: [
"Clawdbot: access not configured.",
"",
`Your Slack user id: ${command.user_id}`,
"",
`Pairing code: ${code}`,
"",
"Ask the bot owner to approve with:",

View File

@@ -313,6 +313,8 @@ export async function monitorWebInbox(options: {
text: [
"Clawdbot: access not configured.",
"",
`Your WhatsApp sender id: ${candidate}`,
"",
`Pairing code: ${code}`,
"",
"Ask the bot owner to approve with:",

View File

@@ -670,6 +670,9 @@ describe("web monitor inbox", () => {
// Should NOT send read receipts for blocked senders (privacy + avoids Baileys Bad MAC churn).
expect(sock.readMessages).not.toHaveBeenCalled();
expect(sock.sendMessage).toHaveBeenCalledTimes(1);
expect(sock.sendMessage).toHaveBeenCalledWith("999@s.whatsapp.net", {
text: expect.stringContaining("Your WhatsApp sender id: +999"),
});
expect(sock.sendMessage).toHaveBeenCalledWith("999@s.whatsapp.net", {
text: expect.stringContaining("Pairing code: PAIRCODE"),
});
@@ -1121,6 +1124,9 @@ describe("web monitor inbox", () => {
await new Promise((resolve) => setImmediate(resolve));
expect(onMessage).not.toHaveBeenCalled();
expect(sock.sendMessage).toHaveBeenCalledTimes(1);
expect(sock.sendMessage).toHaveBeenCalledWith("999@s.whatsapp.net", {
text: expect.stringContaining("Your WhatsApp sender id: +999"),
});
expect(sock.sendMessage).toHaveBeenCalledWith("999@s.whatsapp.net", {
text: expect.stringContaining("Pairing code: PAIRCODE"),
});
@@ -1274,6 +1280,9 @@ describe("web monitor inbox", () => {
expect(onMessage).not.toHaveBeenCalled();
expect(upsertPairingRequestMock).toHaveBeenCalledTimes(1);
expect(sock.sendMessage).toHaveBeenCalledWith("999@s.whatsapp.net", {
text: expect.stringContaining("Your WhatsApp sender id: +999"),
});
expect(sock.sendMessage).toHaveBeenCalledWith("999@s.whatsapp.net", {
text: expect.stringContaining("Pairing code: PAIRCODE"),
});