import { memo, useState } from "react"; import { Bot, Cable, GoalIcon, MessagesSquare, ScrollText, SquareSlash, WandSparkles, } from "lucide-react"; import { cn } from "@/components/lib/utils.js"; import { FileDisplayInline } from "@/lib/fileDisplay.js"; import { isTrustedPluginIconSource } from "@/lib/pluginIconSource.js"; import { usePluginReferenceIconProjection } from "@/v4/pluginReferenceIconContext.js"; import { getPromptMentionVariantClassName, PROMPT_MENTION_BASE_CLASS_NAME, } from "@/mentions/mentionChip.js"; import { formatSkillMentionDisplayLabel, parseMentionMarkdown, } from "@/mentions/mentionMarkdown.js"; import { parseV4VisibleSlashCommand } from "@/v4/slashCommands.js"; const GOAL_QUERY_TOKEN_PATTERN = /^(\s*)(\/(?:goal|target))(?=\s|$)([\s\S]*)$/i; const EMPTY_ATTACHMENTS: readonly unknown[] = []; export interface V4UserInputGoalQueryDisplay { leadingText: string; commandText: string; trailingText: string; } /** * 只解析发送入口会消费的 goal query;普通正文和携带附件的同名文本保持原样。 * 原因:用户消息展示不能仅凭包含 `/goal` 就重猜 command intent,否则上下文 prompt * 在隐藏附加块后会被误画成 goal 控制命令。 */ export function parseV4UserInputGoalQuery( text: string, attachments: readonly unknown[] = EMPTY_ATTACHMENTS, contextAttachmentCount = 0, ): V4UserInputGoalQueryDisplay | null { const command = parseV4VisibleSlashCommand(text, attachments, { contextAttachmentCount, }); if (!command || command.kind === "compact") return null; const match = GOAL_QUERY_TOKEN_PATTERN.exec(text); if (!match) return null; return { leadingText: match[1] ?? "", commandText: match[2] ?? "", trailingText: match[3] ?? "", }; } type V4UserInputMentionPart = ReturnType[number]; function normalizeCommandMentionLabel(label: string): string { return label.trim().replace(/^\/+/, "").toLowerCase(); } function mentionClassName(category: Parameters[0]) { return cn( "mx-0.5 max-w-full", PROMPT_MENTION_BASE_CLASS_NAME, // userInput 正文使用 text-ui-base,与 assistant 消息体保持一致。 "text-ui-base leading-6", getPromptMentionVariantClassName(category), ); } function V4UserInputMention({ part, authoritativeGoal, pluginIcon, }: { part: Exclude; authoritativeGoal: boolean; pluginIcon?: string; }) { if (part.type === "file" || part.type === "directory") { return ( ); } if (part.type === "skill") { return ( ); } if (part.type === "session") { return ( ); } if (part.type === "plugin") { // Plugin 引用在气泡里渲染为 chip:不进 file 分支、不可作外链打开。 return ( {part.label} ); } if (part.type === "subagent") { return ( ); } const commandName = normalizeCommandMentionLabel(part.label); if ((commandName === "goal" || commandName === "target") && !authoritativeGoal) { // 旧版纯文本嗅探会把带附件的 `/goal` 普通 prompt 也画成控制命令。 // V4 只允许发送入口确认的首个 goal token 使用特殊 UI,其余情况必须保持用户原文。 return `/${part.label}`; } return ( {commandName === "goal" || commandName === "target" ? ( ); } function PluginUserMessageIcon({ src }: { src?: string }) { const [failedSrc, setFailedSrc] = useState(null); const showImage = isTrustedPluginIconSource(src) && failedSrc !== src; if (!showImage) { return