refactor(src): split oversized modules

This commit is contained in:
Peter Steinberger
2026-01-14 01:08:15 +00:00
parent b2179de839
commit bcbfb357be
675 changed files with 91476 additions and 73453 deletions

BIN
src/signal/.DS_Store vendored Normal file

Binary file not shown.

View File

@@ -454,92 +454,4 @@ describe("monitorSignalProvider tool results", () => {
expect(sendMock).toHaveBeenCalledTimes(1);
});
it("pairs uuid-only senders with a uuid allowlist entry", async () => {
config = {
...config,
channels: {
...config.channels,
signal: {
...config.channels?.signal,
autoStart: false,
dmPolicy: "pairing",
allowFrom: [],
},
},
};
const abortController = new AbortController();
const uuid = "123e4567-e89b-12d3-a456-426614174000";
streamMock.mockImplementation(async ({ onEvent }) => {
const payload = {
envelope: {
sourceUuid: uuid,
sourceName: "Ada",
timestamp: 1,
dataMessage: {
message: "hello",
},
},
};
await onEvent({
event: "receive",
data: JSON.stringify(payload),
});
abortController.abort();
});
await monitorSignalProvider({
autoStart: false,
baseUrl: "http://127.0.0.1:8080",
abortSignal: abortController.signal,
});
await flush();
expect(replyMock).not.toHaveBeenCalled();
expect(upsertPairingRequestMock).toHaveBeenCalledWith(
expect.objectContaining({
channel: "signal",
id: `uuid:${uuid}`,
meta: expect.objectContaining({ name: "Ada" }),
}),
);
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 () => {
vi.useFakeTimers();
const abortController = new AbortController();
const randomSpy = vi.spyOn(Math, "random").mockReturnValue(0);
let calls = 0;
streamMock.mockImplementation(async () => {
calls += 1;
if (calls === 1) {
throw new Error("stream dropped");
}
abortController.abort();
});
try {
const monitorPromise = monitorSignalProvider({
autoStart: false,
baseUrl: "http://127.0.0.1:8080",
abortSignal: abortController.signal,
});
await vi.advanceTimersByTimeAsync(1_000);
await monitorPromise;
expect(streamMock).toHaveBeenCalledTimes(2);
} finally {
randomSpy.mockRestore();
vi.useRealTimers();
}
});
});

View File

@@ -0,0 +1,167 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { resetInboundDedupe } from "../auto-reply/reply/inbound-dedupe.js";
import { resetSystemEventsForTest } from "../infra/system-events.js";
import { monitorSignalProvider } from "./monitor.js";
const sendMock = vi.fn();
const replyMock = vi.fn();
const updateLastRouteMock = vi.fn();
let config: Record<string, unknown> = {};
const readAllowFromStoreMock = vi.fn();
const upsertPairingRequestMock = vi.fn();
vi.mock("../config/config.js", async (importOriginal) => {
const actual = await importOriginal<typeof import("../config/config.js")>();
return {
...actual,
loadConfig: () => config,
};
});
vi.mock("../auto-reply/reply.js", () => ({
getReplyFromConfig: (...args: unknown[]) => replyMock(...args),
}));
vi.mock("./send.js", () => ({
sendMessageSignal: (...args: unknown[]) => sendMock(...args),
}));
vi.mock("../pairing/pairing-store.js", () => ({
readChannelAllowFromStore: (...args: unknown[]) =>
readAllowFromStoreMock(...args),
upsertChannelPairingRequest: (...args: unknown[]) =>
upsertPairingRequestMock(...args),
}));
vi.mock("../config/sessions.js", () => ({
resolveStorePath: vi.fn(() => "/tmp/clawdbot-sessions.json"),
updateLastRoute: (...args: unknown[]) => updateLastRouteMock(...args),
}));
const streamMock = vi.fn();
const signalCheckMock = vi.fn();
const signalRpcRequestMock = vi.fn();
vi.mock("./client.js", () => ({
streamSignalEvents: (...args: unknown[]) => streamMock(...args),
signalCheck: (...args: unknown[]) => signalCheckMock(...args),
signalRpcRequest: (...args: unknown[]) => signalRpcRequestMock(...args),
}));
vi.mock("./daemon.js", () => ({
spawnSignalDaemon: vi.fn(() => ({ stop: vi.fn() })),
}));
const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
beforeEach(() => {
resetInboundDedupe();
config = {
messages: { responsePrefix: "PFX" },
channels: {
signal: { autoStart: false, dmPolicy: "open", allowFrom: ["*"] },
},
};
sendMock.mockReset().mockResolvedValue(undefined);
replyMock.mockReset();
updateLastRouteMock.mockReset();
streamMock.mockReset();
signalCheckMock.mockReset().mockResolvedValue({});
signalRpcRequestMock.mockReset().mockResolvedValue({});
readAllowFromStoreMock.mockReset().mockResolvedValue([]);
upsertPairingRequestMock
.mockReset()
.mockResolvedValue({ code: "PAIRCODE", created: true });
resetSystemEventsForTest();
});
describe("monitorSignalProvider tool results", () => {
it("pairs uuid-only senders with a uuid allowlist entry", async () => {
config = {
...config,
channels: {
...config.channels,
signal: {
...config.channels?.signal,
autoStart: false,
dmPolicy: "pairing",
allowFrom: [],
},
},
};
const abortController = new AbortController();
const uuid = "123e4567-e89b-12d3-a456-426614174000";
streamMock.mockImplementation(async ({ onEvent }) => {
const payload = {
envelope: {
sourceUuid: uuid,
sourceName: "Ada",
timestamp: 1,
dataMessage: {
message: "hello",
},
},
};
await onEvent({
event: "receive",
data: JSON.stringify(payload),
});
abortController.abort();
});
await monitorSignalProvider({
autoStart: false,
baseUrl: "http://127.0.0.1:8080",
abortSignal: abortController.signal,
});
await flush();
expect(replyMock).not.toHaveBeenCalled();
expect(upsertPairingRequestMock).toHaveBeenCalledWith(
expect.objectContaining({
channel: "signal",
id: `uuid:${uuid}`,
meta: expect.objectContaining({ name: "Ada" }),
}),
);
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 () => {
vi.useFakeTimers();
const abortController = new AbortController();
const randomSpy = vi.spyOn(Math, "random").mockReturnValue(0);
let calls = 0;
streamMock.mockImplementation(async () => {
calls += 1;
if (calls === 1) {
throw new Error("stream dropped");
}
abortController.abort();
});
try {
const monitorPromise = monitorSignalProvider({
autoStart: false,
baseUrl: "http://127.0.0.1:8080",
abortSignal: abortController.signal,
});
await vi.advanceTimersByTimeAsync(1_000);
await monitorPromise;
expect(streamMock).toHaveBeenCalledTimes(2);
} finally {
randomSpy.mockRestore();
vi.useRealTimers();
}
});
});

View File

@@ -1,60 +1,24 @@
import {
resolveEffectiveMessagesConfig,
resolveHumanDelayConfig,
} from "../agents/identity.js";
import { chunkText, resolveTextChunkLimit } from "../auto-reply/chunk.js";
import { formatAgentEnvelope } from "../auto-reply/envelope.js";
import { dispatchReplyFromConfig } from "../auto-reply/reply/dispatch-from-config.js";
import {
buildHistoryContextFromMap,
clearHistoryEntries,
DEFAULT_GROUP_HISTORY_LIMIT,
type HistoryEntry,
} from "../auto-reply/reply/history.js";
import { createReplyDispatcher } from "../auto-reply/reply/reply-dispatcher.js";
import type { ReplyPayload } from "../auto-reply/types.js";
import type { ClawdbotConfig } from "../config/config.js";
import { loadConfig } from "../config/config.js";
import { resolveStorePath, updateLastRoute } from "../config/sessions.js";
import type { SignalReactionNotificationMode } from "../config/types.js";
import { danger, logVerbose, shouldLogVerbose } from "../globals.js";
import { enqueueSystemEvent } from "../infra/system-events.js";
import { mediaKindFromMime } from "../media/constants.js";
import { danger } from "../globals.js";
import { saveMediaBuffer } from "../media/store.js";
import { buildPairingReply } from "../pairing/pairing-messages.js";
import {
readChannelAllowFromStore,
upsertChannelPairingRequest,
} from "../pairing/pairing-store.js";
import { resolveAgentRoute } from "../routing/resolve-route.js";
import type { RuntimeEnv } from "../runtime.js";
import { normalizeE164 } from "../utils.js";
import { resolveSignalAccount } from "./accounts.js";
import { signalCheck, signalRpcRequest } from "./client.js";
import { spawnSignalDaemon } from "./daemon.js";
import {
formatSignalPairingIdLine,
formatSignalSenderDisplay,
formatSignalSenderId,
isSignalSenderAllowed,
resolveSignalPeerId,
resolveSignalRecipient,
resolveSignalSender,
} from "./identity.js";
import { isSignalSenderAllowed, type resolveSignalSender } from "./identity.js";
import { createSignalEventHandler } from "./monitor/event-handler.js";
import { sendMessageSignal } from "./send.js";
import { runSignalSseLoop } from "./sse-reconnect.js";
type SignalEnvelope = {
sourceNumber?: string | null;
sourceUuid?: string | null;
sourceName?: string | null;
timestamp?: number | null;
dataMessage?: SignalDataMessage | null;
editMessage?: { dataMessage?: SignalDataMessage | null } | null;
syncMessage?: unknown;
reactionMessage?: SignalReactionMessage | null;
};
type SignalReactionMessage = {
emoji?: string | null;
targetAuthor?: string | null;
@@ -67,18 +31,6 @@ type SignalReactionMessage = {
} | null;
};
type SignalDataMessage = {
timestamp?: number;
message?: string | null;
attachments?: Array<SignalAttachment>;
groupInfo?: {
groupId?: string | null;
groupName?: string | null;
} | null;
quote?: { text?: string | null } | null;
reaction?: SignalReactionMessage | null;
};
type SignalAttachment = {
id?: string | null;
contentType?: string | null;
@@ -106,12 +58,6 @@ export type MonitorSignalOpts = {
mediaMaxMb?: number;
};
type SignalReceivePayload = {
account?: string;
envelope?: SignalEnvelope | null;
exception?: { message?: string } | null;
};
function resolveRuntime(opts: MonitorSignalOpts): RuntimeEnv {
return (
opts.runtime ?? {
@@ -406,367 +352,31 @@ export async function monitorSignalProvider(
});
}
const handleEvent = async (event: { event?: string; data?: string }) => {
if (event.event !== "receive" || !event.data) return;
let payload: SignalReceivePayload | null = null;
try {
payload = JSON.parse(event.data) as SignalReceivePayload;
} catch (err) {
runtime.error?.(`failed to parse event: ${String(err)}`);
return;
}
if (payload?.exception?.message) {
runtime.error?.(`receive exception: ${payload.exception.message}`);
}
const envelope = payload?.envelope;
if (!envelope) return;
if (envelope.syncMessage) return;
const sender = resolveSignalSender(envelope);
if (!sender) return;
if (account && sender.kind === "phone") {
if (sender.e164 === normalizeE164(account)) {
return;
}
}
const dataMessage =
envelope.dataMessage ?? envelope.editMessage?.dataMessage;
const reaction = isSignalReactionMessage(envelope.reactionMessage)
? envelope.reactionMessage
: isSignalReactionMessage(dataMessage?.reaction)
? dataMessage?.reaction
: null;
const messageText = (dataMessage?.message ?? "").trim();
const quoteText = dataMessage?.quote?.text?.trim() ?? "";
const hasBodyContent =
Boolean(messageText || quoteText) ||
Boolean(!reaction && dataMessage?.attachments?.length);
if (reaction && !hasBodyContent) {
if (reaction.isRemove) return; // Ignore reaction removals
const emojiLabel = reaction.emoji?.trim() || "emoji";
const senderDisplay = formatSignalSenderDisplay(sender);
const senderName = envelope.sourceName ?? senderDisplay;
logVerbose(`signal reaction: ${emojiLabel} from ${senderName}`);
const targets = resolveSignalReactionTargets(reaction);
const shouldNotify = shouldEmitSignalReactionNotification({
mode: reactionMode,
account,
targets,
sender,
allowlist: reactionAllowlist,
});
if (!shouldNotify) return;
const groupId = reaction.groupInfo?.groupId ?? undefined;
const groupName = reaction.groupInfo?.groupName ?? undefined;
const isGroup = Boolean(groupId);
const senderPeerId = resolveSignalPeerId(sender);
const route = resolveAgentRoute({
cfg,
channel: "signal",
accountId: accountInfo.accountId,
peer: {
kind: isGroup ? "group" : "dm",
id: isGroup ? (groupId ?? "unknown") : senderPeerId,
},
});
const groupLabel = isGroup
? `${groupName ?? "Signal Group"} id:${groupId}`
: undefined;
const messageId = reaction.targetSentTimestamp
? String(reaction.targetSentTimestamp)
: "unknown";
const text = buildSignalReactionSystemEventText({
emojiLabel,
actorLabel: senderName,
messageId,
targetLabel: targets[0]?.display,
groupLabel,
});
const senderId = formatSignalSenderId(sender);
const contextKey = [
"signal",
"reaction",
"added",
messageId,
senderId,
emojiLabel,
groupId ?? "",
]
.filter(Boolean)
.join(":");
enqueueSystemEvent(text, {
sessionKey: route.sessionKey,
contextKey,
});
return;
}
if (!dataMessage) return;
const senderDisplay = formatSignalSenderDisplay(sender);
const senderRecipient = resolveSignalRecipient(sender);
const senderPeerId = resolveSignalPeerId(sender);
const senderAllowId = formatSignalSenderId(sender);
if (!senderRecipient) return;
const senderIdLine = formatSignalPairingIdLine(sender);
const groupId = dataMessage.groupInfo?.groupId ?? undefined;
const groupName = dataMessage.groupInfo?.groupName ?? undefined;
const isGroup = Boolean(groupId);
const storeAllowFrom = await readChannelAllowFromStore("signal").catch(
() => [],
);
const effectiveDmAllow = [...allowFrom, ...storeAllowFrom];
const effectiveGroupAllow = [...groupAllowFrom, ...storeAllowFrom];
const dmAllowed =
dmPolicy === "open"
? true
: isSignalSenderAllowed(sender, effectiveDmAllow);
if (!isGroup) {
if (dmPolicy === "disabled") return;
if (!dmAllowed) {
if (dmPolicy === "pairing") {
const senderId = senderAllowId;
const { code, created } = await upsertChannelPairingRequest({
channel: "signal",
id: senderId,
meta: {
name: envelope.sourceName ?? undefined,
},
});
if (created) {
logVerbose(`signal pairing request sender=${senderId}`);
try {
await sendMessageSignal(
`signal:${senderRecipient}`,
buildPairingReply({
channel: "signal",
idLine: senderIdLine,
code,
}),
{
baseUrl,
account,
maxBytes: mediaMaxBytes,
accountId: accountInfo.accountId,
},
);
} catch (err) {
logVerbose(
`signal pairing reply failed for ${senderId}: ${String(err)}`,
);
}
}
} else {
logVerbose(
`Blocked signal sender ${senderDisplay} (dmPolicy=${dmPolicy})`,
);
}
return;
}
}
if (isGroup && groupPolicy === "disabled") {
logVerbose("Blocked signal group message (groupPolicy: disabled)");
return;
}
if (isGroup && groupPolicy === "allowlist") {
if (effectiveGroupAllow.length === 0) {
logVerbose(
"Blocked signal group message (groupPolicy: allowlist, no groupAllowFrom)",
);
return;
}
if (!isSignalSenderAllowed(sender, effectiveGroupAllow)) {
logVerbose(
`Blocked signal group sender ${senderDisplay} (not in groupAllowFrom)`,
);
return;
}
}
const commandAuthorized = isGroup
? effectiveGroupAllow.length > 0
? isSignalSenderAllowed(sender, effectiveGroupAllow)
: true
: dmAllowed;
let mediaPath: string | undefined;
let mediaType: string | undefined;
let placeholder = "";
const firstAttachment = dataMessage.attachments?.[0];
if (firstAttachment?.id && !ignoreAttachments) {
try {
const fetched = await fetchAttachment({
baseUrl,
account,
attachment: firstAttachment,
sender: senderRecipient,
groupId,
maxBytes: mediaMaxBytes,
});
if (fetched) {
mediaPath = fetched.path;
mediaType =
fetched.contentType ?? firstAttachment.contentType ?? undefined;
}
} catch (err) {
runtime.error?.(danger(`attachment fetch failed: ${String(err)}`));
}
}
const kind = mediaKindFromMime(mediaType ?? undefined);
if (kind) {
placeholder = `<media:${kind}>`;
} else if (dataMessage.attachments?.length) {
placeholder = "<media:attachment>";
}
const bodyText =
messageText || placeholder || dataMessage.quote?.text?.trim() || "";
if (!bodyText) return;
const fromLabel = isGroup
? `${groupName ?? "Signal Group"} id:${groupId}`
: `${envelope.sourceName ?? senderDisplay} id:${senderDisplay}`;
const body = formatAgentEnvelope({
channel: "Signal",
from: fromLabel,
timestamp: envelope.timestamp ?? undefined,
body: bodyText,
});
let combinedBody = body;
const historyKey = isGroup ? String(groupId ?? "unknown") : undefined;
if (isGroup && historyKey && historyLimit > 0) {
combinedBody = buildHistoryContextFromMap({
historyMap: groupHistories,
historyKey,
limit: historyLimit,
entry: {
sender: envelope.sourceName ?? senderDisplay,
body: bodyText,
timestamp: envelope.timestamp ?? undefined,
messageId:
typeof envelope.timestamp === "number"
? String(envelope.timestamp)
: undefined,
},
currentMessage: combinedBody,
formatEntry: (entry) =>
formatAgentEnvelope({
channel: "Signal",
from: fromLabel,
timestamp: entry.timestamp,
body: `${entry.sender}: ${entry.body}${
entry.messageId ? ` [id:${entry.messageId}]` : ""
}`,
}),
});
}
const route = resolveAgentRoute({
cfg,
channel: "signal",
accountId: accountInfo.accountId,
peer: {
kind: isGroup ? "group" : "dm",
id: isGroup ? (groupId ?? "unknown") : senderPeerId,
},
});
const signalTo = isGroup
? `group:${groupId}`
: `signal:${senderRecipient}`;
const ctxPayload = {
Body: combinedBody,
RawBody: bodyText,
CommandBody: bodyText,
From: isGroup
? `group:${groupId ?? "unknown"}`
: `signal:${senderRecipient}`,
To: signalTo,
SessionKey: route.sessionKey,
AccountId: route.accountId,
ChatType: isGroup ? "group" : "direct",
GroupSubject: isGroup ? (groupName ?? undefined) : undefined,
SenderName: envelope.sourceName ?? senderDisplay,
SenderId: senderDisplay,
Provider: "signal" as const,
Surface: "signal" as const,
MessageSid: envelope.timestamp ? String(envelope.timestamp) : undefined,
Timestamp: envelope.timestamp ?? undefined,
MediaPath: mediaPath,
MediaType: mediaType,
MediaUrl: mediaPath,
CommandAuthorized: commandAuthorized,
// Originating channel for reply routing.
OriginatingChannel: "signal" as const,
OriginatingTo: signalTo,
};
if (!isGroup) {
const sessionCfg = cfg.session;
const storePath = resolveStorePath(sessionCfg?.store, {
agentId: route.agentId,
});
await updateLastRoute({
storePath,
sessionKey: route.mainSessionKey,
channel: "signal",
to: senderRecipient,
accountId: route.accountId,
});
}
if (shouldLogVerbose()) {
const preview = body.slice(0, 200).replace(/\n/g, "\\n");
logVerbose(
`signal inbound: from=${ctxPayload.From} len=${body.length} preview="${preview}"`,
);
}
let didSendReply = false;
const dispatcher = createReplyDispatcher({
responsePrefix: resolveEffectiveMessagesConfig(cfg, route.agentId)
.responsePrefix,
humanDelay: resolveHumanDelayConfig(cfg, route.agentId),
deliver: async (payload) => {
await deliverReplies({
replies: [payload],
target: ctxPayload.To,
baseUrl,
account,
accountId: accountInfo.accountId,
runtime,
maxBytes: mediaMaxBytes,
textLimit,
});
didSendReply = true;
},
onError: (err, info) => {
runtime.error?.(
danger(`signal ${info.kind} reply failed: ${String(err)}`),
);
},
});
const { queuedFinal } = await dispatchReplyFromConfig({
ctx: ctxPayload,
cfg,
dispatcher,
replyOptions: {
disableBlockStreaming:
typeof accountInfo.config.blockStreaming === "boolean"
? !accountInfo.config.blockStreaming
: undefined,
},
});
if (!queuedFinal) {
if (isGroup && historyKey && historyLimit > 0 && didSendReply) {
clearHistoryEntries({ historyMap: groupHistories, historyKey });
}
return;
}
if (isGroup && historyKey && historyLimit > 0 && didSendReply) {
clearHistoryEntries({ historyMap: groupHistories, historyKey });
}
};
const handleEvent = createSignalEventHandler({
runtime,
cfg,
baseUrl,
account,
accountId: accountInfo.accountId,
blockStreaming: accountInfo.config.blockStreaming,
historyLimit,
groupHistories,
textLimit,
dmPolicy,
allowFrom,
groupAllowFrom,
groupPolicy,
reactionMode,
reactionAllowlist,
mediaMaxBytes,
ignoreAttachments,
fetchAttachment,
deliverReplies,
resolveSignalReactionTargets,
isSignalReactionMessage,
shouldEmitSignalReactionNotification,
buildSignalReactionSystemEventText,
});
await runSignalSseLoop({
baseUrl,

View File

@@ -0,0 +1,392 @@
import {
resolveEffectiveMessagesConfig,
resolveHumanDelayConfig,
} from "../../agents/identity.js";
import { formatAgentEnvelope } from "../../auto-reply/envelope.js";
import { dispatchReplyFromConfig } from "../../auto-reply/reply/dispatch-from-config.js";
import {
buildHistoryContextFromMap,
clearHistoryEntries,
} from "../../auto-reply/reply/history.js";
import { createReplyDispatcher } from "../../auto-reply/reply/reply-dispatcher.js";
import { resolveStorePath, updateLastRoute } from "../../config/sessions.js";
import { danger, logVerbose, shouldLogVerbose } from "../../globals.js";
import { enqueueSystemEvent } from "../../infra/system-events.js";
import { mediaKindFromMime } from "../../media/constants.js";
import { buildPairingReply } from "../../pairing/pairing-messages.js";
import {
readChannelAllowFromStore,
upsertChannelPairingRequest,
} from "../../pairing/pairing-store.js";
import { resolveAgentRoute } from "../../routing/resolve-route.js";
import { normalizeE164 } from "../../utils.js";
import {
formatSignalPairingIdLine,
formatSignalSenderDisplay,
formatSignalSenderId,
isSignalSenderAllowed,
resolveSignalPeerId,
resolveSignalRecipient,
resolveSignalSender,
} from "../identity.js";
import { sendMessageSignal } from "../send.js";
import type {
SignalEventHandlerDeps,
SignalReceivePayload,
} from "./event-handler.types.js";
export function createSignalEventHandler(deps: SignalEventHandlerDeps) {
return async (event: { event?: string; data?: string }) => {
if (event.event !== "receive" || !event.data) return;
let payload: SignalReceivePayload | null = null;
try {
payload = JSON.parse(event.data) as SignalReceivePayload;
} catch (err) {
deps.runtime.error?.(`failed to parse event: ${String(err)}`);
return;
}
if (payload?.exception?.message) {
deps.runtime.error?.(`receive exception: ${payload.exception.message}`);
}
const envelope = payload?.envelope;
if (!envelope) return;
if (envelope.syncMessage) return;
const sender = resolveSignalSender(envelope);
if (!sender) return;
if (deps.account && sender.kind === "phone") {
if (sender.e164 === normalizeE164(deps.account)) return;
}
const dataMessage =
envelope.dataMessage ?? envelope.editMessage?.dataMessage;
const reaction = deps.isSignalReactionMessage(envelope.reactionMessage)
? envelope.reactionMessage
: deps.isSignalReactionMessage(dataMessage?.reaction)
? dataMessage?.reaction
: null;
const messageText = (dataMessage?.message ?? "").trim();
const quoteText = dataMessage?.quote?.text?.trim() ?? "";
const hasBodyContent =
Boolean(messageText || quoteText) ||
Boolean(!reaction && dataMessage?.attachments?.length);
if (reaction && !hasBodyContent) {
if (reaction.isRemove) return; // Ignore reaction removals
const emojiLabel = reaction.emoji?.trim() || "emoji";
const senderDisplay = formatSignalSenderDisplay(sender);
const senderName = envelope.sourceName ?? senderDisplay;
logVerbose(`signal reaction: ${emojiLabel} from ${senderName}`);
const targets = deps.resolveSignalReactionTargets(reaction);
const shouldNotify = deps.shouldEmitSignalReactionNotification({
mode: deps.reactionMode,
account: deps.account,
targets,
sender,
allowlist: deps.reactionAllowlist,
});
if (!shouldNotify) return;
const groupId = reaction.groupInfo?.groupId ?? undefined;
const groupName = reaction.groupInfo?.groupName ?? undefined;
const isGroup = Boolean(groupId);
const senderPeerId = resolveSignalPeerId(sender);
const route = resolveAgentRoute({
cfg: deps.cfg,
channel: "signal",
accountId: deps.accountId,
peer: {
kind: isGroup ? "group" : "dm",
id: isGroup ? (groupId ?? "unknown") : senderPeerId,
},
});
const groupLabel = isGroup
? `${groupName ?? "Signal Group"} id:${groupId}`
: undefined;
const messageId = reaction.targetSentTimestamp
? String(reaction.targetSentTimestamp)
: "unknown";
const text = deps.buildSignalReactionSystemEventText({
emojiLabel,
actorLabel: senderName,
messageId,
targetLabel: targets[0]?.display,
groupLabel,
});
const senderId = formatSignalSenderId(sender);
const contextKey = [
"signal",
"reaction",
"added",
messageId,
senderId,
emojiLabel,
groupId ?? "",
]
.filter(Boolean)
.join(":");
enqueueSystemEvent(text, { sessionKey: route.sessionKey, contextKey });
return;
}
if (!dataMessage) return;
const senderDisplay = formatSignalSenderDisplay(sender);
const senderRecipient = resolveSignalRecipient(sender);
const senderPeerId = resolveSignalPeerId(sender);
const senderAllowId = formatSignalSenderId(sender);
if (!senderRecipient) return;
const senderIdLine = formatSignalPairingIdLine(sender);
const groupId = dataMessage.groupInfo?.groupId ?? undefined;
const groupName = dataMessage.groupInfo?.groupName ?? undefined;
const isGroup = Boolean(groupId);
const storeAllowFrom = await readChannelAllowFromStore("signal").catch(
() => [],
);
const effectiveDmAllow = [...deps.allowFrom, ...storeAllowFrom];
const effectiveGroupAllow = [...deps.groupAllowFrom, ...storeAllowFrom];
const dmAllowed =
deps.dmPolicy === "open"
? true
: isSignalSenderAllowed(sender, effectiveDmAllow);
if (!isGroup) {
if (deps.dmPolicy === "disabled") return;
if (!dmAllowed) {
if (deps.dmPolicy === "pairing") {
const senderId = senderAllowId;
const { code, created } = await upsertChannelPairingRequest({
channel: "signal",
id: senderId,
meta: { name: envelope.sourceName ?? undefined },
});
if (created) {
logVerbose(`signal pairing request sender=${senderId}`);
try {
await sendMessageSignal(
`signal:${senderRecipient}`,
buildPairingReply({
channel: "signal",
idLine: senderIdLine,
code,
}),
{
baseUrl: deps.baseUrl,
account: deps.account,
maxBytes: deps.mediaMaxBytes,
accountId: deps.accountId,
},
);
} catch (err) {
logVerbose(
`signal pairing reply failed for ${senderId}: ${String(err)}`,
);
}
}
} else {
logVerbose(
`Blocked signal sender ${senderDisplay} (dmPolicy=${deps.dmPolicy})`,
);
}
return;
}
}
if (isGroup && deps.groupPolicy === "disabled") {
logVerbose("Blocked signal group message (groupPolicy: disabled)");
return;
}
if (isGroup && deps.groupPolicy === "allowlist") {
if (effectiveGroupAllow.length === 0) {
logVerbose(
"Blocked signal group message (groupPolicy: allowlist, no groupAllowFrom)",
);
return;
}
if (!isSignalSenderAllowed(sender, effectiveGroupAllow)) {
logVerbose(
`Blocked signal group sender ${senderDisplay} (not in groupAllowFrom)`,
);
return;
}
}
const commandAuthorized = isGroup
? effectiveGroupAllow.length > 0
? isSignalSenderAllowed(sender, effectiveGroupAllow)
: true
: dmAllowed;
let mediaPath: string | undefined;
let mediaType: string | undefined;
let placeholder = "";
const firstAttachment = dataMessage.attachments?.[0];
if (firstAttachment?.id && !deps.ignoreAttachments) {
try {
const fetched = await deps.fetchAttachment({
baseUrl: deps.baseUrl,
account: deps.account,
attachment: firstAttachment,
sender: senderRecipient,
groupId,
maxBytes: deps.mediaMaxBytes,
});
if (fetched) {
mediaPath = fetched.path;
mediaType =
fetched.contentType ?? firstAttachment.contentType ?? undefined;
}
} catch (err) {
deps.runtime.error?.(danger(`attachment fetch failed: ${String(err)}`));
}
}
const kind = mediaKindFromMime(mediaType ?? undefined);
if (kind) placeholder = `<media:${kind}>`;
else if (dataMessage.attachments?.length)
placeholder = "<media:attachment>";
const bodyText =
messageText || placeholder || dataMessage.quote?.text?.trim() || "";
if (!bodyText) return;
const fromLabel = isGroup
? `${groupName ?? "Signal Group"} id:${groupId}`
: `${envelope.sourceName ?? senderDisplay} id:${senderDisplay}`;
const body = formatAgentEnvelope({
channel: "Signal",
from: fromLabel,
timestamp: envelope.timestamp ?? undefined,
body: bodyText,
});
let combinedBody = body;
const historyKey = isGroup ? String(groupId ?? "unknown") : undefined;
if (isGroup && historyKey && deps.historyLimit > 0) {
combinedBody = buildHistoryContextFromMap({
historyMap: deps.groupHistories,
historyKey,
limit: deps.historyLimit,
entry: {
sender: envelope.sourceName ?? senderDisplay,
body: bodyText,
timestamp: envelope.timestamp ?? undefined,
messageId:
typeof envelope.timestamp === "number"
? String(envelope.timestamp)
: undefined,
},
currentMessage: combinedBody,
formatEntry: (entry) =>
formatAgentEnvelope({
channel: "Signal",
from: fromLabel,
timestamp: entry.timestamp,
body: `${entry.sender}: ${entry.body}${entry.messageId ? ` [id:${entry.messageId}]` : ""}`,
}),
});
}
const route = resolveAgentRoute({
cfg: deps.cfg,
channel: "signal",
accountId: deps.accountId,
peer: {
kind: isGroup ? "group" : "dm",
id: isGroup ? (groupId ?? "unknown") : senderPeerId,
},
});
const signalTo = isGroup ? `group:${groupId}` : `signal:${senderRecipient}`;
const ctxPayload = {
Body: combinedBody,
RawBody: bodyText,
CommandBody: bodyText,
From: isGroup
? `group:${groupId ?? "unknown"}`
: `signal:${senderRecipient}`,
To: signalTo,
SessionKey: route.sessionKey,
AccountId: route.accountId,
ChatType: isGroup ? "group" : "direct",
GroupSubject: isGroup ? (groupName ?? undefined) : undefined,
SenderName: envelope.sourceName ?? senderDisplay,
SenderId: senderDisplay,
Provider: "signal" as const,
Surface: "signal" as const,
MessageSid: envelope.timestamp ? String(envelope.timestamp) : undefined,
Timestamp: envelope.timestamp ?? undefined,
MediaPath: mediaPath,
MediaType: mediaType,
MediaUrl: mediaPath,
CommandAuthorized: commandAuthorized,
OriginatingChannel: "signal" as const,
OriginatingTo: signalTo,
};
if (!isGroup) {
const sessionCfg = deps.cfg.session;
const storePath = resolveStorePath(sessionCfg?.store, {
agentId: route.agentId,
});
await updateLastRoute({
storePath,
sessionKey: route.mainSessionKey,
channel: "signal",
to: senderRecipient,
accountId: route.accountId,
});
}
if (shouldLogVerbose()) {
const preview = body.slice(0, 200).replace(/\n/g, "\\n");
logVerbose(
`signal inbound: from=${ctxPayload.From} len=${body.length} preview="${preview}"`,
);
}
let didSendReply = false;
const dispatcher = createReplyDispatcher({
responsePrefix: resolveEffectiveMessagesConfig(deps.cfg, route.agentId)
.responsePrefix,
humanDelay: resolveHumanDelayConfig(deps.cfg, route.agentId),
deliver: async (payload) => {
await deps.deliverReplies({
replies: [payload],
target: ctxPayload.To,
baseUrl: deps.baseUrl,
account: deps.account,
accountId: deps.accountId,
runtime: deps.runtime,
maxBytes: deps.mediaMaxBytes,
textLimit: deps.textLimit,
});
didSendReply = true;
},
onError: (err, info) => {
deps.runtime.error?.(
danger(`signal ${info.kind} reply failed: ${String(err)}`),
);
},
});
const { queuedFinal } = await dispatchReplyFromConfig({
ctx: ctxPayload,
cfg: deps.cfg,
dispatcher,
replyOptions: {
disableBlockStreaming:
typeof deps.blockStreaming === "boolean"
? !deps.blockStreaming
: undefined,
},
});
if (!queuedFinal) {
if (isGroup && historyKey && deps.historyLimit > 0 && didSendReply) {
clearHistoryEntries({ historyMap: deps.groupHistories, historyKey });
}
return;
}
if (isGroup && historyKey && deps.historyLimit > 0 && didSendReply) {
clearHistoryEntries({ historyMap: deps.groupHistories, historyKey });
}
};
}

View File

@@ -0,0 +1,121 @@
import type { HistoryEntry } from "../../auto-reply/reply/history.js";
import type { ReplyPayload } from "../../auto-reply/types.js";
import type { ClawdbotConfig } from "../../config/config.js";
import type {
DmPolicy,
GroupPolicy,
SignalReactionNotificationMode,
} from "../../config/types.js";
import type { RuntimeEnv } from "../../runtime.js";
import type { SignalSender } from "../identity.js";
export type SignalEnvelope = {
sourceNumber?: string | null;
sourceUuid?: string | null;
sourceName?: string | null;
timestamp?: number | null;
dataMessage?: SignalDataMessage | null;
editMessage?: { dataMessage?: SignalDataMessage | null } | null;
syncMessage?: unknown;
reactionMessage?: SignalReactionMessage | null;
};
export type SignalDataMessage = {
timestamp?: number;
message?: string | null;
attachments?: Array<SignalAttachment>;
groupInfo?: {
groupId?: string | null;
groupName?: string | null;
} | null;
quote?: { text?: string | null } | null;
reaction?: SignalReactionMessage | null;
};
export type SignalReactionMessage = {
emoji?: string | null;
targetAuthor?: string | null;
targetAuthorUuid?: string | null;
targetSentTimestamp?: number | null;
isRemove?: boolean | null;
groupInfo?: {
groupId?: string | null;
groupName?: string | null;
} | null;
};
export type SignalAttachment = {
id?: string | null;
contentType?: string | null;
filename?: string | null;
size?: number | null;
};
export type SignalReactionTarget = {
kind: "phone" | "uuid";
id: string;
display: string;
};
export type SignalReceivePayload = {
envelope?: SignalEnvelope | null;
exception?: { message?: string } | null;
};
export type SignalEventHandlerDeps = {
runtime: RuntimeEnv;
cfg: ClawdbotConfig;
baseUrl: string;
account?: string;
accountId: string;
blockStreaming?: boolean;
historyLimit: number;
groupHistories: Map<string, HistoryEntry[]>;
textLimit: number;
dmPolicy: DmPolicy;
allowFrom: string[];
groupAllowFrom: string[];
groupPolicy: GroupPolicy;
reactionMode: SignalReactionNotificationMode;
reactionAllowlist: string[];
mediaMaxBytes: number;
ignoreAttachments: boolean;
fetchAttachment: (params: {
baseUrl: string;
account?: string;
attachment: SignalAttachment;
sender?: string;
groupId?: string;
maxBytes: number;
}) => Promise<{ path: string; contentType?: string } | null>;
deliverReplies: (params: {
replies: ReplyPayload[];
target: string;
baseUrl: string;
account?: string;
accountId?: string;
runtime: RuntimeEnv;
maxBytes: number;
textLimit: number;
}) => Promise<void>;
resolveSignalReactionTargets: (
reaction: SignalReactionMessage,
) => SignalReactionTarget[];
isSignalReactionMessage: (
reaction: SignalReactionMessage | null | undefined,
) => reaction is SignalReactionMessage;
shouldEmitSignalReactionNotification: (params: {
mode?: SignalReactionNotificationMode;
account?: string | null;
targets?: SignalReactionTarget[];
sender?: SignalSender | null;
allowlist?: string[];
}) => boolean;
buildSignalReactionSystemEventText: (params: {
emojiLabel: string;
actorLabel: string;
messageId: string;
targetLabel?: string;
groupLabel?: string;
}) => string;
};