@@ -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