Skip to content

Commit 5c4cc72

Browse files
committed
fix(telegram): align directive reply prompt timestamps
1 parent 8a3b4c2 commit 5c4cc72

3 files changed

Lines changed: 42 additions & 2 deletions

File tree

extensions/telegram/src/bot-message-dispatch.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,6 +1884,38 @@ describe("dispatchTelegramMessage draft streaming", () => {
18841884
expect(deliverReplies).not.toHaveBeenCalled();
18851885
});
18861886

1887+
it("marks directive-tagged durable finals with the transcript prompt-context timestamp", async () => {
1888+
const transcriptTimestamp = Date.now() + 1_000;
1889+
const context = createContext();
1890+
context.ctxPayload.SessionKey = "agent:default:telegram:direct:123";
1891+
mockDefaultSessionEntry();
1892+
readLatestAssistantTextByIdentity.mockResolvedValue({
1893+
text: "[[reply_to_current]]Final answer",
1894+
timestamp: transcriptTimestamp,
1895+
});
1896+
deliverInboundReplyWithMessageSendContext.mockResolvedValue({
1897+
status: "handled_visible",
1898+
delivery: {
1899+
messageIds: ["2001"],
1900+
visibleReplySent: true,
1901+
},
1902+
});
1903+
dispatchReplyWithBufferedBlockDispatcher.mockImplementation(async ({ dispatcherOptions }) => {
1904+
await dispatcherOptions.deliver({ text: "Final answer" }, { kind: "final" });
1905+
return { queuedFinal: true };
1906+
});
1907+
1908+
await dispatchWithContext({ context, streamMode: "off" });
1909+
1910+
const outbound = expectRecordFields(mockCallArg(deliverInboundReplyWithMessageSendContext), {
1911+
payload: expect.objectContaining({ text: "Final answer" }),
1912+
});
1913+
expectRecordFields(expectRecordFields(outbound.payload, {}).channelData, {
1914+
telegram: { promptContextTimestampMs: transcriptTimestamp },
1915+
});
1916+
expect(deliverReplies).not.toHaveBeenCalled();
1917+
});
1918+
18871919
it("keeps the Telegram edit cap for non-block previews regardless of chunk config", async () => {
18881920
const draftStream = createDraftStream();
18891921
createTelegramDraftStream.mockReturnValue(draftStream);

extensions/telegram/src/bot-message-dispatch.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import {
5454
appendAssistantMirrorMessageByIdentity,
5555
readLatestAssistantTextByIdentity,
5656
} from "openclaw/plugin-sdk/session-transcript-runtime";
57+
import { stripInlineDirectiveTagsForDelivery } from "openclaw/plugin-sdk/text-chunking";
5758
import { resolveTelegramConfigReasoningDefault } from "./agent-config.js";
5859
import { withTelegramApiErrorLogging } from "./api-logging.js";
5960
import type { TelegramBotDeps } from "./bot-deps.js";
@@ -1580,9 +1581,14 @@ export const dispatchTelegramMessage = async ({
15801581
};
15811582
const resolveCurrentTurnTranscriptFinalText = async (): Promise<string | undefined> =>
15821583
(await resolveCurrentTurnTranscriptFinal())?.text;
1584+
const normalizePromptContextTimestampText = (text: string): string =>
1585+
stripInlineDirectiveTagsForDelivery(text).text.trim();
15831586
const resolvePromptContextTimestampMs = async (text: string): Promise<number | undefined> => {
15841587
const final = await resolveCurrentTurnTranscriptFinal();
1585-
if (final?.text.trim() !== text.trim()) {
1588+
if (
1589+
!final ||
1590+
normalizePromptContextTimestampText(final.text) !== normalizePromptContextTimestampText(text)
1591+
) {
15861592
return undefined;
15871593
}
15881594
return final.timestamp;

extensions/telegram/src/bot.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2461,6 +2461,7 @@ describe("createTelegramBot", () => {
24612461
const senderId = 202;
24622462
const visibleReply = "Yep - I'm here now.";
24632463
const replyTimestampMs = 1_778_474_700_000;
2464+
const telegramReplyDate = Math.floor((replyTimestampMs + 5_000) / 1000);
24642465

24652466
await writeDirectTelegramTranscriptContext({
24662467
cfg: config,
@@ -2482,9 +2483,10 @@ describe("createTelegramBot", () => {
24822483
from: { id: senderId, is_bot: false, first_name: "Kesava" },
24832484
reply_to_message: {
24842485
chat: { id: chatId, type: "private" },
2485-
date: Math.floor(replyTimestampMs / 1000),
2486+
date: telegramReplyDate,
24862487
from: { id: 999, is_bot: true, first_name: "OpenClaw" },
24872488
message_id: 736,
2489+
openclaw_prompt_context_timestamp_ms: replyTimestampMs,
24882490
text: visibleReply,
24892491
},
24902492
},

0 commit comments

Comments
 (0)