diff --git a/packages/platform-crossgram/src/index.ts b/packages/platform-crossgram/src/index.ts index 98a34f28..946d103f 100644 --- a/packages/platform-crossgram/src/index.ts +++ b/packages/platform-crossgram/src/index.ts @@ -267,6 +267,8 @@ export class QQNTPlatform implements IMPlatform { private reactionResourceWarmup?: Promise private readonly grayTipFilters: readonly string[] private readonly originSessions = new Map() + /** 每个会话一条发送队列的队尾,见 `queueConversationSend`。 */ + private readonly conversationSends = new Map>() private readonly splitOutgoingMessages = new Map() private readonly multiForwardMessages = new Map>() private readonly multiForwardPreviewJobs = new Map>() @@ -1199,6 +1201,38 @@ export class QQNTPlatform implements IMPlatform { conversation: IMConversationRef, content: IMMessageInput, options: IMTransferOptions = {}, + ): Promise> { + return this.queueConversationSend(conversation.id, () => + this.deliverMessage(session, conversation, content, options)) + } + + /** + * 让同一会话的发送按发起顺序落库。 + * + * QQ 的房间顺序就是落库顺序,而带媒体的消息要先传完字节才发出送消息请求 + * (`QQNTClient.sendMessage` 里 upload 在 POST 之前)。一批消息并发发起时, + * 落库顺序因此变成「上传完成顺序」:带图的那条总是排到最后,相邻的纯文本也可能 + * 互相插队。串成一条队列后,发起顺序即落库顺序。 + */ + private queueConversationSend(conversationId: string, run: () => Promise): Promise { + const previous = this.conversationSends.get(conversationId) ?? Promise.resolve() + const current = previous.catch(() => undefined).then(run) + this.conversationSends.set(conversationId, current) + // 队尾结算后清掉,避免会话表无限增长;只清自己这一环。 + const release = () => { + if (this.conversationSends.get(conversationId) === current) { + this.conversationSends.delete(conversationId) + } + } + current.then(release, release) + return current + } + + private async deliverMessage( + session: PlatformSession, + conversation: IMConversationRef, + content: IMMessageInput, + options: IMTransferOptions = {}, ): Promise> { type ResolvedMentionUser = Awaited> const resolvedMentionUsers = new Map>() diff --git a/packages/platform-crossgram/src/platform.test.ts b/packages/platform-crossgram/src/platform.test.ts index b8938bba..beb05872 100644 --- a/packages/platform-crossgram/src/platform.test.ts +++ b/packages/platform-crossgram/src/platform.test.ts @@ -4247,6 +4247,37 @@ describe('QQNTPlatform mapping', () => { expect(progress).toEqual([1, 2]) }) + it('lands a burst of sends in issue order even when the media one uploads first', async () => { + const platform = new QQNTPlatform() + const landed: string[] = [] + platform.client.sendMessage = vi.fn(async (_conversation: string, parts) => { + const media = parts.some((part) => part.type === 'media') + // 带媒体的发送要先把字节传完才发出送消息请求,这里用等待模拟上传耗时。 + if (media) await new Promise((resolve) => setTimeout(resolve, 20)) + landed.push(media ? 'image' : 'text') + return { + id: `sent-${landed.length}`, conversationId: '2:group', senderId: 'self', timestamp: 10, + outgoing: true, + parts: parts.map((part) => part.type === 'text' ? part : { type: 'text' as const, text: '' }), + } + }) + const image = { + kind: 'image' as const, name: 'a.png', mimeType: 'image/png', size: 3, + source: { size: 3, async *stream() { yield new Uint8Array([1, 2, 3]) } }, + } + + await Promise.all([ + platform.sendMessage(session, { id: '2:group' }, { parts: [ + { type: 'text', text: 'before' }, + { type: 'media', media: image }, + { type: 'text', text: 'after' }, + ] }), + platform.sendMessage(session, { id: '2:group' }, { parts: [{ type: 'text', text: 'next' }] }), + ]) + + expect(landed).toEqual(['image', 'text']) + }) + it('passes the message element order to the bridge', async () => { const platform = new QQNTPlatform() const image = (name: string) => ({