ChannelMentionContent.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { Fragment } from "react";
  2. import { AtSign } from "lucide-react";
  3. import {
  4. channelContentText,
  5. type ChannelContentPart,
  6. type BotGroupInputSource,
  7. } from "@zcode/shared";
  8. import { PROMPT_MENTION_BASE_CLASS_NAME } from "@/mentions/mentionChip.js";
  9. /** 只渲染可信消息节点;复制出的 @name 普通文本不能恢复平台身份。 */
  10. export function ChannelMentionContent({
  11. parts,
  12. text,
  13. }: {
  14. parts?: readonly ChannelContentPart[];
  15. text: string;
  16. }) {
  17. if (!parts || channelContentText(parts) !== text) return <>{text}</>;
  18. return (
  19. <span className="whitespace-pre-wrap break-words">
  20. {parts.map((part, index) =>
  21. part.type === "text" ? (
  22. <Fragment key={index}>{part.text}</Fragment>
  23. ) : (
  24. <span
  25. key={`${part.refId}:${index}`}
  26. data-channel-mention={part.channel}
  27. title={`${part.name} · ${part.channel === "lark" ? "Lark" : "飞书 / Feishu"}`}
  28. className={`${PROMPT_MENTION_BASE_CLASS_NAME} mx-0.5 max-w-full rounded bg-surface-hover px-1 text-foreground`}
  29. >
  30. <AtSign className="size-3.5 shrink-0" aria-hidden="true" />
  31. <span className="min-w-0 truncate">{part.name}</span>
  32. <span className="text-ui-xs text-foreground-subtle">
  33. {part.channel === "lark" ? "Lark" : "Feishu"}
  34. </span>
  35. </span>
  36. ),
  37. )}
  38. </span>
  39. );
  40. }
  41. /** 合批队列按原发送者与正文顺序投影,不能把末条的节点套到整批文本上。 */
  42. export function BotChannelMessageContent({
  43. source,
  44. text,
  45. }: {
  46. source?: BotGroupInputSource;
  47. text: string;
  48. }) {
  49. const messages = source?.messages;
  50. const parts =
  51. messages && messages.length > 1
  52. ? messages.flatMap((message, index): ChannelContentPart[] => [
  53. {
  54. type: "text",
  55. text: `${index ? "\n\n" : ""}${message.senderName || message.senderId}: `,
  56. },
  57. ...(message.contentParts && channelContentText(message.contentParts) === message.text
  58. ? message.contentParts
  59. : [{ type: "text" as const, text: message.text }]),
  60. ])
  61. : (messages?.[0]?.contentParts ?? source?.contentParts);
  62. return <ChannelMentionContent parts={parts} text={text} />;
  63. }