Skip to content

Commit c5f2cc8

Browse files
committed
fix(qa): preserve early Telegram reply mappings
1 parent 00acb49 commit c5f2cc8

2 files changed

Lines changed: 60 additions & 31 deletions

File tree

‎extensions/qa-lab/src/live-transports/telegram/adapter.runtime.test.ts‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,19 @@ describe("Telegram QA transport adapter", () => {
170170
};
171171
});
172172
mocks.userbotSend
173-
.mockResolvedValueOnce({ messageId: 10 })
173+
.mockImplementationOnce(async () => {
174+
await onUpdate?.({
175+
kind: "message",
176+
chatId: -100123,
177+
messageId: 11,
178+
senderId: 200,
179+
senderUsername: "sut_bot",
180+
replyToMessageId: 10,
181+
timestamp: 100_000,
182+
text: "preview",
183+
});
184+
return { messageId: 10 };
185+
})
174186
.mockResolvedValueOnce({ messageId: 12 });
175187
const addInboundMessage = vi.fn().mockResolvedValue({ id: "in-1" });
176188
const addOutboundMessage = vi.fn().mockResolvedValue({ id: "out-1" });
@@ -193,16 +205,6 @@ describe("Telegram QA transport adapter", () => {
193205
expect.objectContaining({ accountId: "sut", senderId: "100" }),
194206
);
195207

196-
await onUpdate?.({
197-
kind: "message",
198-
chatId: -100123,
199-
messageId: 11,
200-
senderId: 200,
201-
senderUsername: "sut_bot",
202-
replyToMessageId: 10,
203-
timestamp: 100_000,
204-
text: "preview",
205-
});
206208
expect(addOutboundMessage).toHaveBeenCalledWith(
207209
expect.objectContaining({
208210
to: "group:logical-room",

‎extensions/qa-lab/src/live-transports/telegram/adapter.runtime.ts‎

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,10 @@ export async function createTelegramQaTransportAdapter(
116116
let logicalConversationKind: "channel" | "direct" | "group" = "channel";
117117
const nativeMessageIds = new Map<string, number>();
118118
const busMessageIds = new Map<number, string>();
119+
let sendsInFlight = 0;
120+
let deferredReplies: TelegramUserbotUpdate[] = [];
119121

120-
const observeUpdate = async (update: TelegramUserbotUpdate) => {
121-
observerState.updateCount += 1;
122-
observerState.relevantUpdateKinds.add(update.kind);
123-
if (
124-
update.chatId !== Number(credentialLease.payload.groupId) ||
125-
update.senderId !== Number(credentialLease.payload.sutBotId)
126-
) {
127-
observerState.filteredCount += 1;
128-
return;
129-
}
130-
observerState.matchedCount += 1;
122+
const publishUpdate = async (update: TelegramUserbotUpdate) => {
131123
const existingMessageId = busMessageIds.get(update.messageId);
132124
if (update.kind === "edit" && existingMessageId) {
133125
await context.messages.editMessage({
@@ -151,6 +143,28 @@ export async function createTelegramQaTransportAdapter(
151143
busMessageIds.set(update.messageId, outbound.id);
152144
};
153145

146+
const observeUpdate = async (update: TelegramUserbotUpdate) => {
147+
observerState.updateCount += 1;
148+
observerState.relevantUpdateKinds.add(update.kind);
149+
if (
150+
update.chatId !== Number(credentialLease.payload.groupId) ||
151+
update.senderId !== Number(credentialLease.payload.sutBotId)
152+
) {
153+
observerState.filteredCount += 1;
154+
return;
155+
}
156+
observerState.matchedCount += 1;
157+
if (
158+
sendsInFlight > 0 &&
159+
update.replyToMessageId &&
160+
!busMessageIds.has(update.replyToMessageId)
161+
) {
162+
deferredReplies.push(update);
163+
return;
164+
}
165+
await publishUpdate(update);
166+
};
167+
154168
try {
155169
stateRoot = skillRuntime.createStateRoot();
156170
const restored = skillRuntime.restoreCredential(credentialLease.payload, stateRoot);
@@ -216,21 +230,34 @@ export async function createTelegramQaTransportAdapter(
216230
logicalConversationKind = input.conversation.kind;
217231
const text = renderTelegramQaInboundText(input, credentialLease.payload.sutUsername);
218232
const nativeReplyToId = input.replyToId ? nativeMessageIds.get(input.replyToId) : undefined;
219-
const sent = await activeUserbot.send({ text, replyToMessageId: nativeReplyToId });
220-
const message = await context.messages.addInboundMessage({
221-
...input,
222-
accountId,
223-
senderId: credentialLease.payload.testerUserId,
224-
});
225-
nativeMessageIds.set(message.id, sent.messageId);
226-
busMessageIds.set(sent.messageId, message.id);
227-
return message;
233+
sendsInFlight += 1;
234+
try {
235+
const sent = await activeUserbot.send({ text, replyToMessageId: nativeReplyToId });
236+
const message = await context.messages.addInboundMessage({
237+
...input,
238+
accountId,
239+
senderId: credentialLease.payload.testerUserId,
240+
});
241+
nativeMessageIds.set(message.id, sent.messageId);
242+
busMessageIds.set(sent.messageId, message.id);
243+
const readyReplies = deferredReplies.filter(
244+
(update) => update.replyToMessageId && busMessageIds.has(update.replyToMessageId),
245+
);
246+
deferredReplies = deferredReplies.filter((update) => !readyReplies.includes(update));
247+
for (const update of readyReplies) {
248+
await publishUpdate(update);
249+
}
250+
return message;
251+
} finally {
252+
sendsInFlight -= 1;
253+
}
228254
},
229255
resetTransport: () => {
230256
logicalConversationId = credentialLease.payload.groupId;
231257
logicalConversationKind = "channel";
232258
nativeMessageIds.clear();
233259
busMessageIds.clear();
260+
deferredReplies = [];
234261
observerState.updateCount = 0;
235262
observerState.filteredCount = 0;
236263
observerState.matchedCount = 0;

0 commit comments

Comments
 (0)