| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- /* eslint-disable max-lines -- Lexical slash 插件的 trigger/面板/键盘导航同属一个协议状态机,oxfmt 换行后略超 400 行。 */
- /**
- * SlashCommandPlugin — Lexical trigger 面板插件
- *
- * 处理 `/` 面板里的 slash commands 和 subagents:
- * 1. `/` 直接展示 ZCode Agent 广播的真实 slash commands,并补充可用 subagents
- * 2. 面板通过 portal 渲染到输入区上方的独立挂载层,展开时直接覆盖消息区
- * 3. 支持 Esc 关闭、上下键切换、Enter / Tab 选中,以及跟随输入做模糊搜索
- */
- import { useCallback, useEffect, useMemo, useRef, useState } from "react";
- import type { ZCodeProvider } from "@zcode/shared";
- import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
- import { createPortal } from "react-dom";
- import {
- $createTextNode,
- BLUR_COMMAND,
- COMMAND_PRIORITY_CRITICAL,
- COMMAND_PRIORITY_LOW,
- KEY_ARROW_DOWN_COMMAND,
- KEY_ARROW_UP_COMMAND,
- KEY_ENTER_COMMAND,
- KEY_ESCAPE_COMMAND,
- KEY_TAB_COMMAND,
- } from "lexical";
- import { useSubagents } from "@/hooks/useSubagents.js";
- import { useSkills } from "@/hooks/useSkills.js";
- import { buildSlashApplyMentionPayload } from "@/lib/slashApplyMentionPayload.js";
- import { filterSkillsForProvider } from "@/lib/skillSourceFilter.js";
- import { useZCodeIntl } from "./i18n/IntlProvider.js";
- import { useSlashCommands } from "./hooks/useSlashCommands.js";
- import { $createPromptMentionNode } from "./mentions/nodes/PromptMentionNode.js";
- import {
- extractActivePromptInputTrigger,
- filterPromptInputSuggestions,
- getActivePromptInputTokenTailLength,
- getBestPromptInputSuggestionIndex,
- getPromptInputTriggerSignature,
- type ActivePromptInputTrigger,
- type PromptInputSuggestionItem,
- } from "./lib/promptInputTriggers.js";
- import { MentionPanel } from "./mentions/components/MentionPanel.js";
- import {
- buildAppSlashCommandSuggestions,
- buildSkillSuggestions,
- buildSubagentSuggestions,
- buildSlashSuggestions,
- getTextAroundCursor,
- isAppSlashCommandSuggestion,
- normalizeSlashCommandValue,
- type SlashCommandPluginProps,
- } from "./slashCommandHelpers.js";
- import { useSlashCommandMentionPanelSections } from "./slashCommandPanelSections.js";
- import { getCurrentTextNodeSelection } from "./mentions/mentionHelpers.js";
- import {
- getActivePromptInputTokenReplacementRange,
- reconcileActivePromptInputTokenSnapshot,
- type ActivePromptInputTokenSnapshot,
- } from "./mentions/activePromptInputToken.js";
- import { shouldSlashPanelProcessUpdate } from "./lib/slashPanelUpdateFilter.js";
- export function SlashCommandPlugin({
- workspacePath,
- workspaceIdentity,
- sessionId,
- provider,
- container,
- disabled = false,
- excludedCommandNames,
- appCommands,
- }: SlashCommandPluginProps & { provider: ZCodeProvider }) {
- const [editor] = useLexicalComposerContext();
- const { intl, locale } = useZCodeIntl();
- const [activeTrigger, setActiveTrigger] = useState<ActivePromptInputTrigger | null>(null);
- // 远程 workspace 的 slashCommands 写在 workspaceIdentity 桶。
- // 这里只按 workspacePath 读取会落到 path 桶,表现为 ZCode Agent 已收到 available_commands_update 但 / 面板为空。
- const commands = useSlashCommands(workspacePath, workspaceIdentity);
- const {
- agents,
- loading: subagentsLoading,
- error: subagentsError,
- } = useSubagents(workspacePath, provider, workspaceIdentity);
- const {
- skills,
- loading: skillsLoading,
- error: skillsError,
- } = useSkills({
- workspacePath,
- workspaceIdentity,
- sessionId: sessionId ?? null,
- enabled: !disabled && activeTrigger?.trigger === "/",
- });
- const [selectedIndex, setSelectedIndex] = useState(0);
- const dismissedSignatureRef = useRef<string | null>(null);
- const activeSignatureRef = useRef<string | null>(null);
- const activeTokenRef = useRef<ActivePromptInputTokenSnapshot | null>(null);
- const commandSuggestions = useMemo(() => {
- const excluded = new Set((excludedCommandNames ?? []).map(normalizeSlashCommandValue));
- const cliSuggestions = buildSlashSuggestions(commands).filter(
- (item) => !excluded.has(item.value),
- );
- // App 层命令追加在 CLI catalog 之后展示;CLI 已提供同名命令时以 CLI 为准,避免遮蔽。
- const cliValues = new Set(cliSuggestions.map((item) => item.value));
- const appSuggestions = buildAppSlashCommandSuggestions(appCommands ?? []).filter(
- (item) => !cliValues.has(item.value) && !excluded.has(item.value),
- );
- return [...cliSuggestions, ...appSuggestions];
- }, [appCommands, commands, excludedCommandNames]);
- const subagentSuggestions = useMemo(() => buildSubagentSuggestions(agents), [agents]);
- const skillSuggestions = useMemo(
- () =>
- buildSkillSuggestions(
- filterSkillsForProvider(skills, provider).filter((skill) => skill.enabled),
- locale,
- ),
- [locale, provider, skills],
- );
- const filteredCommandSuggestions = useMemo(
- () => filterPromptInputSuggestions(commandSuggestions, activeTrigger?.query ?? null),
- [commandSuggestions, activeTrigger?.query],
- );
- const filteredSubagentSuggestions = useMemo(
- () => filterPromptInputSuggestions(subagentSuggestions, activeTrigger?.query ?? null),
- [subagentSuggestions, activeTrigger?.query],
- );
- const filteredSkillSuggestions = useMemo(
- () => filterPromptInputSuggestions(skillSuggestions, activeTrigger?.query ?? null),
- [skillSuggestions, activeTrigger?.query],
- );
- const filteredSuggestions = useMemo(
- () => [
- ...filteredCommandSuggestions,
- ...filteredSkillSuggestions,
- ...filteredSubagentSuggestions,
- ],
- [filteredCommandSuggestions, filteredSkillSuggestions, filteredSubagentSuggestions],
- );
- const activeSignature = useMemo(
- () => getPromptInputTriggerSignature(activeTrigger),
- [activeTrigger],
- );
- const isOpen = !disabled && activeTrigger !== null;
- useEffect(() => {
- activeSignatureRef.current = activeSignature;
- }, [activeSignature]);
- useEffect(() => {
- // 面板展示需要保留 commands/subagents 分组顺序,但键盘默认选中不能固定落在第一个分组。
- // 例如 `/rev` 时 command 描述里的弱匹配可能排在 subagent 分组前面,导致无法默认选中最佳 subagent。
- // 这里按统一的模糊评分选择全局最佳项,同时不改变面板的分组展示顺序。
- setSelectedIndex(
- getBestPromptInputSuggestionIndex(filteredSuggestions, activeTrigger?.query ?? null),
- );
- }, [activeSignature, activeTrigger?.query, filteredSuggestions]);
- useEffect(() => {
- setSelectedIndex((current) => {
- if (filteredSuggestions.length === 0) {
- return 0;
- }
- return Math.min(current, filteredSuggestions.length - 1);
- });
- }, [filteredSuggestions.length]);
- useEffect(() => {
- if (!disabled) {
- return;
- }
- setActiveTrigger(null);
- setSelectedIndex(0);
- activeTokenRef.current = null;
- }, [disabled]);
- useEffect(() => {
- return editor.registerUpdateListener(({ dirtyElements, dirtyLeaves, editorState, tags }) => {
- editorState.read(() => {
- // 历史导航回填含 / 的历史条目时,不应重新打开 slash 面板,
- // 否则面板以 COMMAND_PRIORITY_CRITICAL 注册方向键处理器,吞掉后续历史翻阅按键。
- if (!shouldSlashPanelProcessUpdate(tags)) {
- activeTokenRef.current = null;
- setActiveTrigger(null);
- return;
- }
- if (disabled) {
- activeTokenRef.current = null;
- setActiveTrigger(null);
- return;
- }
- const selectionState = getCurrentTextNodeSelection();
- const cursorText = selectionState ?? getTextAroundCursor();
- if (!cursorText) {
- activeTokenRef.current = null;
- dismissedSignatureRef.current = null;
- setActiveTrigger(null);
- return;
- }
- const nextActiveToken = selectionState
- ? reconcileActivePromptInputTokenSnapshot(
- activeTokenRef.current,
- selectionState,
- dirtyElements.size === 0 && dirtyLeaves.size === 0,
- )
- : null;
- const nextActiveTrigger = selectionState
- ? nextActiveToken
- : extractActivePromptInputTrigger(cursorText.textBeforeCursor);
- if (!nextActiveTrigger || nextActiveTrigger.trigger !== "/") {
- activeTokenRef.current = null;
- dismissedSignatureRef.current = null;
- setActiveTrigger(null);
- return;
- }
- activeTokenRef.current = nextActiveToken;
- const nextSignature = getPromptInputTriggerSignature(nextActiveTrigger);
- if (
- dismissedSignatureRef.current !== null &&
- dismissedSignatureRef.current !== nextSignature
- ) {
- dismissedSignatureRef.current = null;
- }
- if (dismissedSignatureRef.current === nextSignature) {
- setActiveTrigger(null);
- return;
- }
- setActiveTrigger((current) => {
- if (
- current?.trigger === nextActiveTrigger.trigger &&
- current.query === nextActiveTrigger.query
- ) {
- return current;
- }
- return nextActiveTrigger;
- });
- });
- });
- }, [disabled, editor]);
- const applySuggestion = useCallback(
- (suggestion: PromptInputSuggestionItem) => {
- const isAppCommand = isAppSlashCommandSuggestion(suggestion);
- editor.update(() => {
- const selectionState = getCurrentTextNodeSelection();
- if (!selectionState) {
- return;
- }
- const snapshotRange = getActivePromptInputTokenReplacementRange(
- activeTokenRef.current,
- selectionState,
- );
- const activeSlashTrigger = snapshotRange
- ? activeTokenRef.current
- : extractActivePromptInputTrigger(selectionState.textBeforeCursor);
- if (!activeSlashTrigger || activeSlashTrigger.trigger !== "/") {
- return;
- }
- const tokenStart =
- snapshotRange?.start ?? selectionState.cursorOffset - activeSlashTrigger.query.length - 1;
- const tokenEnd =
- snapshotRange?.end ??
- selectionState.cursorOffset +
- getActivePromptInputTokenTailLength(
- activeSlashTrigger,
- selectionState.textAfterCursor,
- suggestion.value,
- );
- selectionState.selection.setTextNodeRange(
- selectionState.node,
- tokenStart,
- selectionState.node,
- tokenEnd,
- );
- if (isAppCommand) {
- // App 层命令"选中即执行":只移除输入中的 `/xxx` token,不插入 mention、不发送。
- selectionState.selection.removeText();
- return;
- }
- const mentionNode = $createPromptMentionNode(buildSlashApplyMentionPayload(suggestion));
- const trailingWhitespace = $createTextNode(" ");
- selectionState.selection.insertNodes([mentionNode, trailingWhitespace]);
- trailingWhitespace.selectEnd();
- });
- dismissedSignatureRef.current = null;
- activeTokenRef.current = null;
- setActiveTrigger(null);
- setSelectedIndex(0);
- if (isAppCommand) {
- appCommands
- ?.find((command) => normalizeSlashCommandValue(command.value) === suggestion.value)
- ?.run();
- return;
- }
- requestAnimationFrame(() => {
- editor.focus();
- });
- },
- [appCommands, editor],
- );
- const selectSuggestion = useCallback(
- (index: number) => {
- const suggestion = filteredSuggestions[index];
- if (!suggestion) {
- return false;
- }
- applySuggestion(suggestion);
- return true;
- },
- [applySuggestion, filteredSuggestions],
- );
- useEffect(() => {
- if (!isOpen) {
- return;
- }
- const unregisterDown = editor.registerCommand(
- KEY_ARROW_DOWN_COMMAND,
- (event) => {
- if (filteredSuggestions.length === 0) {
- return false;
- }
- event?.preventDefault();
- event?.stopPropagation();
- setSelectedIndex((prev) => Math.min(prev + 1, filteredSuggestions.length - 1));
- return true;
- },
- COMMAND_PRIORITY_CRITICAL,
- );
- const unregisterUp = editor.registerCommand(
- KEY_ARROW_UP_COMMAND,
- (event) => {
- if (filteredSuggestions.length === 0) {
- return false;
- }
- event?.preventDefault();
- event?.stopPropagation();
- setSelectedIndex((prev) => Math.max(prev - 1, 0));
- return true;
- },
- COMMAND_PRIORITY_CRITICAL,
- );
- const unregisterEnter = editor.registerCommand(
- KEY_ENTER_COMMAND,
- (event) => {
- if (!selectSuggestion(selectedIndex)) {
- return false;
- }
- event?.preventDefault();
- event?.stopPropagation();
- return true;
- },
- COMMAND_PRIORITY_CRITICAL,
- );
- const unregisterTab = editor.registerCommand(
- KEY_TAB_COMMAND,
- (event) => {
- if (!selectSuggestion(selectedIndex)) {
- return false;
- }
- event?.preventDefault();
- event?.stopPropagation();
- return true;
- },
- COMMAND_PRIORITY_CRITICAL,
- );
- const unregisterEscape = editor.registerCommand(
- KEY_ESCAPE_COMMAND,
- (event) => {
- event?.preventDefault();
- event?.stopPropagation();
- // 如果 Esc 关闭后只记 query,不区分 `/` 和 `@`,两个触发器同名查询会互相把面板压住。
- // 这里保存 trigger + query 组合签名,只有当前 token 真正变化后才重新打开,避免一关闭就立刻弹回。
- dismissedSignatureRef.current = activeSignatureRef.current;
- setActiveTrigger(null);
- setSelectedIndex(0);
- return true;
- },
- COMMAND_PRIORITY_CRITICAL,
- );
- const unregisterBlur = editor.registerCommand(
- BLUR_COMMAND,
- () => {
- dismissedSignatureRef.current = null;
- activeTokenRef.current = null;
- setActiveTrigger(null);
- setSelectedIndex(0);
- return false;
- },
- COMMAND_PRIORITY_LOW,
- );
- return () => {
- unregisterDown();
- unregisterUp();
- unregisterEnter();
- unregisterTab();
- unregisterEscape();
- unregisterBlur();
- };
- }, [editor, filteredSuggestions.length, isOpen, selectSuggestion, selectedIndex]);
- const panelTitle = intl.formatMessage({ id: "chat.slash.title" });
- const hasActiveQuery = (activeTrigger?.query ?? "").trim().length > 0;
- const panelDescription = hasActiveQuery
- ? ""
- : intl.formatMessage({ id: "chat.slash.searchHint" });
- const panelSections = useSlashCommandMentionPanelSections(
- intl,
- commands.length,
- filteredCommandSuggestions,
- filteredSkillSuggestions,
- skillsLoading,
- skillsError,
- filteredSubagentSuggestions,
- subagentsLoading,
- subagentsError,
- );
- if (!isOpen || !container) {
- return null;
- }
- return createPortal(
- <MentionPanel
- title={panelTitle}
- description={panelDescription}
- trigger="/"
- sections={panelSections}
- emptyText=""
- selectedIndex={selectedIndex}
- hasActiveQuery={hasActiveQuery}
- onSelect={selectSuggestion}
- />,
- container,
- );
- }
|