
























@@ -3,11 +3,15 @@ import { loadConfig } from "../../config/config.js";
33import { normalizeCronJobCreate, normalizeCronJobPatch } from "../../cron/normalize.js";
44import type { CronDelivery, CronMessageChannel } from "../../cron/types.js";
55import { normalizeHttpWebhookUrl } from "../../cron/webhook-url.js";
6-import { parseAgentSessionKey } from "../../sessions/session-key-utils.js";
6+import {
7+parseAgentSessionKey,
8+parseThreadSessionSuffix,
9+} from "../../sessions/session-key-utils.js";
710import { extractTextFromChatContent } from "../../shared/chat-content.js";
811import {
912normalizeLowercaseStringOrEmpty,
1013normalizeOptionalLowercaseString,
14+normalizeOptionalString,
1115} from "../../shared/string-coerce.js";
1216import { isRecord, truncateUtf16Safe } from "../../utils.js";
1317import {
@@ -398,12 +402,37 @@ function stripThreadSuffixFromSessionKey(sessionKey: string): string {
398402return parent ? parent : sessionKey;
399403}
400404405+function resolveTelegramDirectThreadId(params: {
406+peerId: string;
407+threadId?: string;
408+}): string | undefined {
409+const threadId = normalizeOptionalString(params.threadId);
410+if (!threadId) {
411+return undefined;
412+}
413+const peerId = normalizeOptionalString(params.peerId);
414+if (!peerId) {
415+return undefined;
416+}
417+const [threadChatId, ...threadIdParts] = threadId.split(":");
418+if (threadIdParts.length === 0) {
419+return threadId;
420+}
421+if (normalizeOptionalLowercaseString(threadChatId) !== peerId) {
422+return undefined;
423+}
424+return normalizeOptionalString(threadIdParts.join(":"));
425+}
426+401427function inferDeliveryFromSessionKey(agentSessionKey?: string): CronDelivery | null {
402428const rawSessionKey = agentSessionKey?.trim();
403429if (!rawSessionKey) {
404430return null;
405431}
406-const parsed = parseAgentSessionKey(stripThreadSuffixFromSessionKey(rawSessionKey));
432+const threadSuffix = parseThreadSessionSuffix(rawSessionKey);
433+const parsed = parseAgentSessionKey(
434+threadSuffix.baseSessionKey ?? stripThreadSuffixFromSessionKey(rawSessionKey),
435+);
407436if (!parsed || !parsed.rest) {
408437return null;
409438}
@@ -444,10 +473,26 @@ function inferDeliveryFromSessionKey(agentSessionKey?: string): CronDelivery | n
444473channel = normalizeOptionalLowercaseString(parts[0]) as CronMessageChannel | undefined;
445474}
446475476+const marker = parts[markerIndex];
447477const delivery: CronDelivery = { mode: "announce", to: peerId };
448478if (channel) {
449479delivery.channel = channel;
450480}
481+if (channel === "telegram" && markerIndex === 2) {
482+const accountId = normalizeOptionalString(parts[1]);
483+if (accountId) {
484+delivery.accountId = accountId;
485+}
486+}
487+if (channel === "telegram" && (marker === "direct" || marker === "dm")) {
488+const threadId = resolveTelegramDirectThreadId({
489+ peerId,
490+threadId: threadSuffix.threadId,
491+});
492+if (threadId) {
493+delivery.threadId = threadId;
494+}
495+}
451496return delivery;
452497}
453498此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。