ConversationUserInputContent.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import { memo, useState } from "react";
  2. import {
  3. Bot,
  4. Cable,
  5. GoalIcon,
  6. MessagesSquare,
  7. ScrollText,
  8. SquareSlash,
  9. WandSparkles,
  10. } from "lucide-react";
  11. import { cn } from "@/components/lib/utils.js";
  12. import { FileDisplayInline } from "@/lib/fileDisplay.js";
  13. import { isTrustedPluginIconSource } from "@/lib/pluginIconSource.js";
  14. import { usePluginReferenceIconProjection } from "@/v4/pluginReferenceIconContext.js";
  15. import {
  16. getPromptMentionVariantClassName,
  17. PROMPT_MENTION_BASE_CLASS_NAME,
  18. } from "@/mentions/mentionChip.js";
  19. import {
  20. formatSkillMentionDisplayLabel,
  21. parseMentionMarkdown,
  22. } from "@/mentions/mentionMarkdown.js";
  23. import { parseV4VisibleSlashCommand } from "@/v4/slashCommands.js";
  24. const GOAL_QUERY_TOKEN_PATTERN = /^(\s*)(\/(?:goal|target))(?=\s|$)([\s\S]*)$/i;
  25. const EMPTY_ATTACHMENTS: readonly unknown[] = [];
  26. export interface V4UserInputGoalQueryDisplay {
  27. leadingText: string;
  28. commandText: string;
  29. trailingText: string;
  30. }
  31. /**
  32. * 只解析发送入口会消费的 goal query;普通正文和携带附件的同名文本保持原样。
  33. * 原因:用户消息展示不能仅凭包含 `/goal` 就重猜 command intent,否则上下文 prompt
  34. * 在隐藏附加块后会被误画成 goal 控制命令。
  35. */
  36. export function parseV4UserInputGoalQuery(
  37. text: string,
  38. attachments: readonly unknown[] = EMPTY_ATTACHMENTS,
  39. contextAttachmentCount = 0,
  40. ): V4UserInputGoalQueryDisplay | null {
  41. const command = parseV4VisibleSlashCommand(text, attachments, {
  42. contextAttachmentCount,
  43. });
  44. if (!command || command.kind === "compact") return null;
  45. const match = GOAL_QUERY_TOKEN_PATTERN.exec(text);
  46. if (!match) return null;
  47. return {
  48. leadingText: match[1] ?? "",
  49. commandText: match[2] ?? "",
  50. trailingText: match[3] ?? "",
  51. };
  52. }
  53. type V4UserInputMentionPart = ReturnType<typeof parseMentionMarkdown>[number];
  54. function normalizeCommandMentionLabel(label: string): string {
  55. return label.trim().replace(/^\/+/, "").toLowerCase();
  56. }
  57. function mentionClassName(category: Parameters<typeof getPromptMentionVariantClassName>[0]) {
  58. return cn(
  59. "mx-0.5 max-w-full",
  60. PROMPT_MENTION_BASE_CLASS_NAME,
  61. // userInput 正文使用 text-ui-base,与 assistant 消息体保持一致。
  62. "text-ui-base leading-6",
  63. getPromptMentionVariantClassName(category),
  64. );
  65. }
  66. function V4UserInputMention({
  67. part,
  68. authoritativeGoal,
  69. pluginIcon,
  70. }: {
  71. part: Exclude<V4UserInputMentionPart, { type: "text" }>;
  72. authoritativeGoal: boolean;
  73. pluginIcon?: string;
  74. }) {
  75. if (part.type === "file" || part.type === "directory") {
  76. return (
  77. <span className={mentionClassName("files")}>
  78. <FileDisplayInline
  79. path={part.label}
  80. options={{
  81. className: "inline-flex min-w-0 max-w-full items-center gap-1 align-middle",
  82. iconSize: 16,
  83. kind: part.type === "directory" ? "directory" : "file",
  84. fileNameClassName: "truncate text-ui-base leading-6 font-medium text-current",
  85. }}
  86. />
  87. </span>
  88. );
  89. }
  90. if (part.type === "skill") {
  91. return (
  92. <span className={mentionClassName("skills")}>
  93. <WandSparkles aria-hidden="true" className="size-4 shrink-0" />
  94. {formatSkillMentionDisplayLabel(part.label)}
  95. </span>
  96. );
  97. }
  98. if (part.type === "session") {
  99. return (
  100. <span className={mentionClassName("sessions")}>
  101. <MessagesSquare aria-hidden="true" className="size-4 shrink-0" />
  102. {part.label}
  103. </span>
  104. );
  105. }
  106. if (part.type === "plugin") {
  107. // Plugin 引用在气泡里渲染为 chip:不进 file 分支、不可作外链打开。
  108. return (
  109. <span className={mentionClassName("plugins")} data-plugin-mention-id={part.pluginId}>
  110. <PluginUserMessageIcon src={pluginIcon} />
  111. {part.label}
  112. </span>
  113. );
  114. }
  115. if (part.type === "subagent") {
  116. return (
  117. <span className={mentionClassName("subagents")}>
  118. <Bot aria-hidden="true" className="size-4 shrink-0" strokeWidth={1.5} />
  119. {part.label}
  120. </span>
  121. );
  122. }
  123. const commandName = normalizeCommandMentionLabel(part.label);
  124. if ((commandName === "goal" || commandName === "target") && !authoritativeGoal) {
  125. // 旧版纯文本嗅探会把带附件的 `/goal` 普通 prompt 也画成控制命令。
  126. // V4 只允许发送入口确认的首个 goal token 使用特殊 UI,其余情况必须保持用户原文。
  127. return `/${part.label}`;
  128. }
  129. return (
  130. <span
  131. {...(authoritativeGoal ? { "data-v4-user-input-command": "goal" } : {})}
  132. className={mentionClassName("commands")}
  133. >
  134. {commandName === "goal" || commandName === "target" ? (
  135. <GoalIcon aria-hidden="true" className="size-4 shrink-0" />
  136. ) : commandName === "compact" ? (
  137. <ScrollText aria-hidden="true" className="size-4 shrink-0" />
  138. ) : (
  139. <SquareSlash aria-hidden="true" className="size-4 shrink-0" />
  140. )}
  141. {/* Bug 根因:authoritative goal 使用原始 slash token 回显,导致用户气泡重复暴露
  142. 控制语法。标签保留 Goal 语义,只省略 `/`;复制、编辑和协议仍使用原始 row.text。 */}
  143. {part.label}
  144. </span>
  145. );
  146. }
  147. function PluginUserMessageIcon({ src }: { src?: string }) {
  148. const [failedSrc, setFailedSrc] = useState<string | null>(null);
  149. const showImage = isTrustedPluginIconSource(src) && failedSrc !== src;
  150. if (!showImage) {
  151. return <Cable aria-hidden="true" className="size-4 shrink-0" />;
  152. }
  153. return (
  154. <img
  155. src={src}
  156. alt=""
  157. aria-hidden="true"
  158. draggable={false}
  159. data-plugin-mention-icon="true"
  160. className="inline-block size-4 shrink-0 rounded-sm object-contain align-middle"
  161. onError={() => setFailedSrc(src ?? null)}
  162. />
  163. );
  164. }
  165. export const ConversationUserInputContent = memo(function ConversationUserInputContent({
  166. text,
  167. attachments = EMPTY_ATTACHMENTS,
  168. contextAttachmentCount = 0,
  169. }: {
  170. text: string;
  171. attachments?: readonly unknown[];
  172. contextAttachmentCount?: number;
  173. }) {
  174. const pluginIconProjection = usePluginReferenceIconProjection();
  175. const goalQuery = parseV4UserInputGoalQuery(text, attachments, contextAttachmentCount);
  176. const parts = parseMentionMarkdown(text);
  177. const authoritativeGoalPartIndex = goalQuery
  178. ? parts.findIndex(
  179. (part) =>
  180. part.type === "command" &&
  181. ["goal", "target"].includes(normalizeCommandMentionLabel(part.label)),
  182. )
  183. : -1;
  184. return (
  185. <>
  186. {parts.map((part, index) => {
  187. if (part.type === "text") {
  188. return part.text;
  189. }
  190. return (
  191. <V4UserInputMention
  192. key={`${part.type}-${index}`}
  193. part={part}
  194. authoritativeGoal={index === authoritativeGoalPartIndex}
  195. pluginIcon={
  196. part.type === "plugin" && part.pluginId
  197. ? pluginIconProjection?.iconByPluginId.get(part.pluginId)
  198. : undefined
  199. }
  200. />
  201. );
  202. })}
  203. </>
  204. );
  205. });