topicInputMessages.ts 1.1 KB

123456789101112131415161718192021222324252627282930
  1. import type { UserInputRow } from "@zcode/shared/zcode-protocol-v4";
  2. /** 合批是执行边界,不能把多人消息展示成一个人的正文。 */
  3. export function expandTopicInputMessages(row: UserInputRow): UserInputRow[] {
  4. const source = row.botGroupSource;
  5. if (!source?.messages?.length) return [row];
  6. const claimed = new Set(source.messages.flatMap((message) => message.attachmentIndexes));
  7. const shared = row.attachments?.filter((_, index) => !claimed.has(index)) ?? [];
  8. return source.messages.map((message, index) => ({
  9. ...row,
  10. text: message.text,
  11. conversationQuotes: message.conversationQuotes ?? [],
  12. attachments: [
  13. ...(row.attachments?.filter((_, attachmentIndex) =>
  14. message.attachmentIndexes.includes(attachmentIndex),
  15. ) ?? []),
  16. ...(index === 0 ? shared : []),
  17. ],
  18. botGroupSource: {
  19. ...source,
  20. messages: undefined,
  21. topicContext: undefined,
  22. topicHistory: undefined,
  23. messageId: message.messageId,
  24. contentParts: message.contentParts,
  25. senderId: message.senderId,
  26. senderName: message.senderName,
  27. },
  28. }));
  29. }