chore: format + fix telegram thread ids

This commit is contained in:
Peter Steinberger
2026-01-15 09:13:13 +00:00
parent 5599603bdb
commit 4291d56e0b
8 changed files with 90 additions and 65 deletions

View File

@@ -18,4 +18,3 @@ describe("formatRawAssistantErrorForUi", () => {
expect(formatRawAssistantErrorForUi("")).toContain("unknown error"); expect(formatRawAssistantErrorForUi("")).toContain("unknown error");
}); });
}); });

View File

@@ -155,11 +155,7 @@ export function resolveCommandAuthorization(params: {
const senderId = matchedSender ?? senderCandidates[0]; const senderId = matchedSender ?? senderCandidates[0];
const enforceOwner = Boolean(dock?.commands?.enforceOwnerForCommands); const enforceOwner = Boolean(dock?.commands?.enforceOwnerForCommands);
const isOwner = const isOwner = !enforceOwner || allowAll || ownerList.length === 0 || Boolean(matchedSender);
!enforceOwner ||
allowAll ||
ownerList.length === 0 ||
Boolean(matchedSender);
const isAuthorizedSender = commandAuthorized && isOwner; const isAuthorizedSender = commandAuthorized && isOwner;
return { return {

View File

@@ -8,6 +8,17 @@ function parseReplyToMessageId(replyToId?: string | null) {
return Number.isFinite(parsed) ? parsed : undefined; return Number.isFinite(parsed) ? parsed : undefined;
} }
function parseThreadId(threadId?: string | number | null) {
if (threadId == null) return undefined;
if (typeof threadId === "number") {
return Number.isFinite(threadId) ? Math.trunc(threadId) : undefined;
}
const trimmed = threadId.trim();
if (!trimmed) return undefined;
const parsed = Number.parseInt(trimmed, 10);
return Number.isFinite(parsed) ? parsed : undefined;
}
export const telegramOutbound: ChannelOutboundAdapter = { export const telegramOutbound: ChannelOutboundAdapter = {
deliveryMode: "direct", deliveryMode: "direct",
chunker: markdownToTelegramHtmlChunks, chunker: markdownToTelegramHtmlChunks,
@@ -25,10 +36,11 @@ export const telegramOutbound: ChannelOutboundAdapter = {
sendText: async ({ to, text, accountId, deps, replyToId, threadId }) => { sendText: async ({ to, text, accountId, deps, replyToId, threadId }) => {
const send = deps?.sendTelegram ?? sendMessageTelegram; const send = deps?.sendTelegram ?? sendMessageTelegram;
const replyToMessageId = parseReplyToMessageId(replyToId); const replyToMessageId = parseReplyToMessageId(replyToId);
const messageThreadId = parseThreadId(threadId);
const result = await send(to, text, { const result = await send(to, text, {
verbose: false, verbose: false,
textMode: "html", textMode: "html",
messageThreadId: threadId ?? undefined, messageThreadId,
replyToMessageId, replyToMessageId,
accountId: accountId ?? undefined, accountId: accountId ?? undefined,
}); });
@@ -37,11 +49,12 @@ export const telegramOutbound: ChannelOutboundAdapter = {
sendMedia: async ({ to, text, mediaUrl, accountId, deps, replyToId, threadId }) => { sendMedia: async ({ to, text, mediaUrl, accountId, deps, replyToId, threadId }) => {
const send = deps?.sendTelegram ?? sendMessageTelegram; const send = deps?.sendTelegram ?? sendMessageTelegram;
const replyToMessageId = parseReplyToMessageId(replyToId); const replyToMessageId = parseReplyToMessageId(replyToId);
const messageThreadId = parseThreadId(threadId);
const result = await send(to, text, { const result = await send(to, text, {
verbose: false, verbose: false,
mediaUrl, mediaUrl,
textMode: "html", textMode: "html",
messageThreadId: threadId ?? undefined, messageThreadId,
replyToMessageId, replyToMessageId,
accountId: accountId ?? undefined, accountId: accountId ?? undefined,
}); });

View File

@@ -36,6 +36,23 @@ import type { ChannelPlugin } from "./types.js";
const meta = getChatChannelMeta("telegram"); const meta = getChatChannelMeta("telegram");
function parseReplyToMessageId(replyToId?: string | null) {
if (!replyToId) return undefined;
const parsed = Number.parseInt(replyToId, 10);
return Number.isFinite(parsed) ? parsed : undefined;
}
function parseThreadId(threadId?: string | number | null) {
if (threadId == null) return undefined;
if (typeof threadId === "number") {
return Number.isFinite(threadId) ? Math.trunc(threadId) : undefined;
}
const trimmed = threadId.trim();
if (!trimmed) return undefined;
const parsed = Number.parseInt(trimmed, 10);
return Number.isFinite(parsed) ? parsed : undefined;
}
export const telegramPlugin: ChannelPlugin<ResolvedTelegramAccount> = { export const telegramPlugin: ChannelPlugin<ResolvedTelegramAccount> = {
id: "telegram", id: "telegram",
meta: { meta: {
@@ -231,29 +248,25 @@ export const telegramPlugin: ChannelPlugin<ResolvedTelegramAccount> = {
}, },
sendText: async ({ to, text, accountId, deps, replyToId, threadId }) => { sendText: async ({ to, text, accountId, deps, replyToId, threadId }) => {
const send = deps?.sendTelegram ?? sendMessageTelegram; const send = deps?.sendTelegram ?? sendMessageTelegram;
const replyToMessageId = replyToId ? Number.parseInt(replyToId, 10) : undefined; const replyToMessageId = parseReplyToMessageId(replyToId);
const resolvedReplyToMessageId = Number.isFinite(replyToMessageId) const messageThreadId = parseThreadId(threadId);
? replyToMessageId
: undefined;
const result = await send(to, text, { const result = await send(to, text, {
verbose: false, verbose: false,
messageThreadId: threadId ?? undefined, messageThreadId,
replyToMessageId: resolvedReplyToMessageId, replyToMessageId,
accountId: accountId ?? undefined, accountId: accountId ?? undefined,
}); });
return { channel: "telegram", ...result }; return { channel: "telegram", ...result };
}, },
sendMedia: async ({ to, text, mediaUrl, accountId, deps, replyToId, threadId }) => { sendMedia: async ({ to, text, mediaUrl, accountId, deps, replyToId, threadId }) => {
const send = deps?.sendTelegram ?? sendMessageTelegram; const send = deps?.sendTelegram ?? sendMessageTelegram;
const replyToMessageId = replyToId ? Number.parseInt(replyToId, 10) : undefined; const replyToMessageId = parseReplyToMessageId(replyToId);
const resolvedReplyToMessageId = Number.isFinite(replyToMessageId) const messageThreadId = parseThreadId(threadId);
? replyToMessageId
: undefined;
const result = await send(to, text, { const result = await send(to, text, {
verbose: false, verbose: false,
mediaUrl, mediaUrl,
messageThreadId: threadId ?? undefined, messageThreadId,
replyToMessageId: resolvedReplyToMessageId, replyToMessageId,
accountId: accountId ?? undefined, accountId: accountId ?? undefined,
}); });
return { channel: "telegram", ...result }; return { channel: "telegram", ...result };

View File

@@ -29,7 +29,12 @@ export function normalizeSlackChannelType(
channelId?: string | null, channelId?: string | null,
): SlackMessageEvent["channel_type"] { ): SlackMessageEvent["channel_type"] {
const normalized = channelType?.trim().toLowerCase(); const normalized = channelType?.trim().toLowerCase();
if (normalized === "im" || normalized === "mpim" || normalized === "channel" || normalized === "group") { if (
normalized === "im" ||
normalized === "mpim" ||
normalized === "channel" ||
normalized === "group"
) {
return normalized; return normalized;
} }
return inferSlackChannelType(channelId) ?? "channel"; return inferSlackChannelType(channelId) ?? "channel";

View File

@@ -28,4 +28,3 @@ describe("extractTextFromMessage", () => {
expect(text).toContain("unknown error"); expect(text).toContain("unknown error");
}); });
}); });