fix(signal): canonicalize message targets in tool and inbound flows

This commit is contained in:
Josh Avant
2026-02-17 14:17:22 -08:00
committed by GitHub
parent 9a2c39419e
commit b20339a232
6 changed files with 135 additions and 30 deletions

View File

@@ -28,6 +28,7 @@ import {
resolveWhatsAppGroupRequireMention,
resolveWhatsAppGroupToolPolicy,
} from "./plugins/group-mentions.js";
import { normalizeSignalMessagingTarget } from "./plugins/normalize/signal.js";
import type {
ChannelCapabilities,
ChannelCommandAdapter,
@@ -97,16 +98,32 @@ const formatDiscordAllowFrom = (allowFrom: Array<string | number>) =>
)
.filter(Boolean);
function buildDirectOrGroupThreadToolContext(params: {
function resolveDirectOrGroupChannelId(context: ChannelThreadingContext): string | undefined {
const isDirect = context.ChatType?.toLowerCase() === "direct";
return (isDirect ? (context.From ?? context.To) : context.To)?.trim() || undefined;
}
function buildSignalThreadToolContext(params: {
context: ChannelThreadingContext;
hasRepliedRef: ChannelThreadingToolContext["hasRepliedRef"];
}): ChannelThreadingToolContext {
const isDirect = params.context.ChatType?.toLowerCase() === "direct";
const channelId =
(isDirect ? (params.context.From ?? params.context.To) : params.context.To)?.trim() ||
undefined;
const currentChannelIdRaw = resolveDirectOrGroupChannelId(params.context);
const currentChannelId = currentChannelIdRaw
? (normalizeSignalMessagingTarget(currentChannelIdRaw) ?? currentChannelIdRaw.trim())
: undefined;
return {
currentChannelId: channelId,
currentChannelId,
currentThreadTs: params.context.ReplyToId,
hasRepliedRef: params.hasRepliedRef,
};
}
function buildIMessageThreadToolContext(params: {
context: ChannelThreadingContext;
hasRepliedRef: ChannelThreadingToolContext["hasRepliedRef"];
}): ChannelThreadingToolContext {
return {
currentChannelId: resolveDirectOrGroupChannelId(params.context),
currentThreadTs: params.context.ReplyToId,
hasRepliedRef: params.hasRepliedRef,
};
@@ -437,7 +454,7 @@ const DOCKS: Record<ChatChannelId, ChannelDock> = {
},
threading: {
buildToolContext: ({ context, hasRepliedRef }) =>
buildDirectOrGroupThreadToolContext({ context, hasRepliedRef }),
buildSignalThreadToolContext({ context, hasRepliedRef }),
},
},
imessage: {
@@ -462,7 +479,7 @@ const DOCKS: Record<ChatChannelId, ChannelDock> = {
},
threading: {
buildToolContext: ({ context, hasRepliedRef }) =>
buildDirectOrGroupThreadToolContext({ context, hasRepliedRef }),
buildIMessageThreadToolContext({ context, hasRepliedRef }),
},
},
};

View File

@@ -35,27 +35,36 @@ export function normalizeSignalMessagingTarget(raw: string): string | undefined
const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
const UUID_COMPACT_PATTERN = /^[0-9a-f]{32}$/i;
export function looksLikeSignalTargetId(raw: string): boolean {
const trimmed = raw.trim();
if (!trimmed) {
return false;
}
if (/^(signal:)?(group:|username:|u:)/i.test(trimmed)) {
return true;
}
if (/^(signal:)?uuid:/i.test(trimmed)) {
const stripped = trimmed
.replace(/^signal:/i, "")
.replace(/^uuid:/i, "")
.trim();
if (!stripped) {
return false;
export function looksLikeSignalTargetId(raw: string, normalized?: string): boolean {
const candidates = [raw, normalized ?? ""].map((value) => value.trim()).filter(Boolean);
for (const candidate of candidates) {
if (/^(signal:)?(group:|username:|u:)/i.test(candidate)) {
return true;
}
if (/^(signal:)?uuid:/i.test(candidate)) {
const stripped = candidate
.replace(/^signal:/i, "")
.replace(/^uuid:/i, "")
.trim();
if (!stripped) {
continue;
}
if (UUID_PATTERN.test(stripped) || UUID_COMPACT_PATTERN.test(stripped)) {
return true;
}
continue;
}
const withoutSignalPrefix = candidate.replace(/^signal:/i, "").trim();
// Accept UUIDs (used by signal-cli for reactions)
if (UUID_PATTERN.test(withoutSignalPrefix) || UUID_COMPACT_PATTERN.test(withoutSignalPrefix)) {
return true;
}
if (/^\+?\d{3,}$/.test(withoutSignalPrefix)) {
return true;
}
return UUID_PATTERN.test(stripped) || UUID_COMPACT_PATTERN.test(stripped);
}
// Accept UUIDs (used by signal-cli for reactions)
if (UUID_PATTERN.test(trimmed) || UUID_COMPACT_PATTERN.test(trimmed)) {
return true;
}
return /^\+?\d{3,}$/.test(trimmed);
return false;
}

View File

@@ -42,6 +42,11 @@ describe("signal target normalization", () => {
expect(looksLikeSignalTargetId("signal:uuid:123e4567-e89b-12d3-a456-426614174000")).toBe(true);
});
it("accepts signal-prefixed E.164 targets for detection", () => {
expect(looksLikeSignalTargetId("signal:+15551234567")).toBe(true);
expect(looksLikeSignalTargetId("signal:15551234567")).toBe(true);
});
it("accepts compact UUIDs for target detection", () => {
expect(looksLikeSignalTargetId("123e4567e89b12d3a456426614174000")).toBe(true);
expect(looksLikeSignalTargetId("uuid:123e4567e89b12d3a456426614174000")).toBe(true);