fix(webui): match slash command highlighting to skills

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-26 17:33:27 +08:00
1 parent dfb3843b4d
commit 3230013963
6 files changed
+97 -21

No files matched your search

+7 -4
View File
@@ -1,21 +1,24 @@
import {
INLINE_TOKEN_HIGHLIGHT_COLOR,
INLINE_REFERENCE_HIGHLIGHT_COLOR,
InlineTokenHighlight,
} from "@/components/InlineTokenHighlight";
interface SlashCommandTextProps {
command: string;
testId?: string;
}
export function SlashCommandText({
command,
testId = "message-slash-command",
}: SlashCommandTextProps) {
return (
<InlineTokenHighlight
testId="message-slash-command"
color={INLINE_TOKEN_HIGHLIGHT_COLOR}
testId={testId}
className="font-normal"
color={INLINE_REFERENCE_HIGHLIGHT_COLOR}
>
{command}
<span className="opacity-90">/</span>{command.slice(1)}
</InlineTokenHighlight>
);
}
+13 -2
View File
@@ -55,6 +55,7 @@ import {
isCapabilityMentionStart,
} from "@/lib/composer-mention-text";
import { INLINE_TOKEN_HIGHLIGHT_COLOR } from "@/components/InlineTokenHighlight";
import { SlashCommandText } from "@/components/SlashCommandText";
import {
ChevronDown,
ChevronUp,
@@ -138,6 +139,7 @@ import { requestSkillsRefresh } from "@/lib/skill-events";
import {
isRegisteredSlashCommand,
isSideChannelLifecycle,
matchingSlashCommand,
slashCommandLifecycle,
} from "@/lib/slash-command";
import {
@@ -1721,7 +1723,8 @@ export function ThreadComposer({
() => withSkillReferences(displayMentionSegments),
[displayMentionSegments],
);
const hasInlineDecorations = inlineSegments.some((segment) => segment.kind !== "text");
const slashCommand = matchingSlashCommand(sessionDragInsertion?.value ?? draftText, slashCommands);
const hasInlineDecorations = slashCommand !== null || inlineSegments.some((segment) => segment.kind !== "text");
const activeCliMentionApps = useMemo(() => {
const seen = new Set<string>();
return mentionSegments.flatMap((segment) => {
@@ -2714,6 +2717,7 @@ export function ThreadComposer({
<ComposerInlineReferenceOverlay
overlayRef={mentionOverlayRef}
segments={inlineSegments}
slashCommand={slashCommand?.command ?? null}
isHero={isHero}
isComposing={mentionInput.isComposing}
className={inputTextClasses}
@@ -2997,6 +3001,7 @@ function QueuedPromptPreview({
function ComposerInlineReferenceOverlay({
overlayRef,
segments,
slashCommand,
isHero,
isComposing,
className,
@@ -3004,6 +3009,7 @@ function ComposerInlineReferenceOverlay({
}: {
overlayRef: Ref<HTMLDivElement>;
segments: InlineReferenceSegment[];
slashCommand: string | null;
isHero: boolean;
isComposing: boolean;
className: string;
@@ -3026,7 +3032,12 @@ function ComposerInlineReferenceOverlay({
const start = offset;
offset += segment.text.length;
if (segment.kind === "text") {
return <span key={`text-${index}`}>{segment.text}</span>;
return <span key={`text-${index}`}>
{start === 0 && slashCommand ? <>
<SlashCommandText command={slashCommand} testId="composer-slash-command" />
{segment.text.slice(slashCommand.length)}
</> : segment.text}
</span>;
}
if (segment.kind === "skill") {
return <SkillReferenceToken
+11 -4
View File
@@ -434,13 +434,15 @@ describe("MessageBubble", () => {
const command = screen.getByTestId("message-slash-command");
expect(command).toHaveTextContent("/model");
expect(command).toHaveClass(
"font-[550]",
"font-normal",
"transition-colors",
"duration-150",
);
expect(command).not.toHaveClass("font-mono");
expect(command.getAttribute("style")).not.toContain("text-shadow");
expect(command.getAttribute("style")).toContain("var(--inline-token-highlight)");
expect(command.getAttribute("style")).toContain("var(--inline-reference-highlight)");
expect(command.firstElementChild).toHaveTextContent(/^\/$/);
expect(command.firstElementChild).toHaveClass("opacity-90");
expect(command.className).not.toMatch(/(?:^|\s)(?:bg-|border|ring|rounded)/);
expect(command.parentElement).toHaveTextContent("/model gpt-5");
expect(command.parentElement).toHaveClass("rounded-floating", "bg-secondary/70");
@@ -475,7 +477,7 @@ describe("MessageBubble", () => {
const message: UIMessage = {
id: "u-command-mention",
role: "user",
content: "/goal ask @zoom to schedule the review",
content: "/goal ask $github and @zoom to schedule the review",
createdAt: Date.now(),
};
@@ -487,7 +489,12 @@ describe("MessageBubble", () => {
/>,
);
expect(screen.getByTestId("message-slash-command")).toHaveTextContent("/goal");
const command = screen.getByTestId("message-slash-command");
const skill = screen.getByTestId("message-skill-reference-github");
expect(command).toHaveTextContent("/goal");
expect(skill).toHaveTextContent("$github");
expect(command.className).toBe(skill.className);
expect(command.getAttribute("style")).toBe(skill.getAttribute("style"));
expect(screen.getByTestId("message-cli-mention-zoom")).toHaveTextContent("@Zoom");
});
+43 -6
View File
@@ -1870,6 +1870,8 @@ describe("ThreadComposer", () => {
fireEvent.keyDown(input, { key: "Enter" });
expect(input).toHaveValue("/history ");
expect(screen.getByTestId("composer-slash-command")).toHaveTextContent("/history");
expect(input.previousElementSibling?.textContent).toBe("/history ");
expect(onSend).not.toHaveBeenCalled();
expect(screen.queryByRole("listbox", { name: "Slash commands" })).not.toBeInTheDocument();
});
@@ -2562,6 +2564,38 @@ describe("ThreadComposer", () => {
}]);
});
it("highlights slash commands alongside skills without changing native edits or sending", () => {
const onSend = vi.fn();
render(<ThreadComposer onSend={onSend} slashCommands={COMMANDS} />);
const input = screen.getByLabelText("Message input") as HTMLTextAreaElement;
const text = "/history $workflow 中文 English";
fireEvent.change(input, { target: { value: text, selectionStart: text.length } });
const command = screen.getByTestId("composer-slash-command");
const skill = screen.getByTestId("composer-skill-reference-workflow");
expect(command).toHaveTextContent("/history");
expect(command.className).toBe(skill.className);
expect(command.getAttribute("style")).toBe(skill.getAttribute("style"));
expect(input.previousElementSibling).toHaveAttribute("aria-hidden", "true");
expect(input.previousElementSibling?.textContent).toBe(text);
input.setSelectionRange(1, 4);
const setData = vi.fn();
fireEvent.copy(input, { clipboardData: { setData } });
expect(setData).toHaveBeenCalledWith("text/plain", "his");
fireEvent.cut(input, { clipboardData: { setData } });
expect(input).toHaveValue("/tory $workflow 中文 English");
expect(screen.queryByTestId("composer-slash-command")).not.toBeInTheDocument();
fireEvent.keyDown(input, { key: "z", ctrlKey: true });
expect(input).toHaveValue(text);
expect(input.selectionStart).toBe(1);
expect(input.selectionEnd).toBe(4);
expect(screen.getByTestId("composer-slash-command")).toHaveTextContent("/history");
fireEvent.click(screen.getByRole("button", { name: "Send message" }));
expect(onSend).toHaveBeenCalledWith(text, undefined, { sideChannel: true });
});
it("highlights literal skill references while preserving partial edits, clipboard text and sending", () => {
const onSend = vi.fn();
render(<ThreadComposer onSend={onSend} />);
@@ -2592,22 +2626,23 @@ describe("ThreadComposer", () => {
expect(onSend).toHaveBeenCalledWith(text, undefined, undefined);
});
it("mirrors skill references during composition without submitting the IME confirmation", () => {
it.each(["", "/history "])("mirrors inline references during composition without submitting the IME confirmation: %s", (prefix) => {
const onSend = vi.fn();
render(<ThreadComposer onSend={onSend} />);
render(<ThreadComposer onSend={onSend} slashCommands={COMMANDS} />);
const input = screen.getByLabelText("Message input") as HTMLTextAreaElement;
fireEvent.change(input, { target: { value: "$workflow " } });
fireEvent.change(input, { target: { value: `${prefix}$workflow ` } });
fireEvent.compositionStart(input);
fireEvent.change(input, { target: { value: "$workflow zhong" } });
fireEvent.change(input, { target: { value: `${prefix}$workflow zhong` } });
if (prefix) expect(screen.getByTestId("composer-slash-command")).toHaveTextContent("/history");
expect(screen.getByTestId("composer-skill-reference-workflow")).toHaveTextContent(/^\$workflow$/);
expect(input.previousElementSibling?.textContent).toBe(input.value);
expect(input.previousElementSibling).toHaveClass("z-20");
fireEvent.keyDown(input, { key: "Enter", isComposing: true });
expect(onSend).not.toHaveBeenCalled();
fireEvent.change(input, { target: { value: "$workflow 中" } });
fireEvent.change(input, { target: { value: `${prefix}$workflow 中` } });
fireEvent.compositionEnd(input, { data: "中" });
fireEvent.click(screen.getByRole("button", { name: "Send message" }));
expect(onSend).toHaveBeenCalledWith("$workflow 中", undefined, undefined);
expect(onSend).toHaveBeenCalledWith(`${prefix}$workflow 中`, undefined, prefix ? { sideChannel: true } : undefined);
});
it("opens skills only from a $ reference and prioritizes the skill name", () => {
@@ -3305,6 +3340,7 @@ describe("ThreadComposer", () => {
const input = screen.getByLabelText("Message input");
fireEvent.change(input, { target: { value: "/new with a title" } });
expect(screen.queryByTestId("composer-slash-command")).not.toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Send message" }));
expect(onSend).toHaveBeenCalledWith("/new with a title", undefined, undefined);
@@ -3542,6 +3578,7 @@ describe("ThreadComposer", () => {
const { rerender } = render(composer(true));
const input = screen.getByLabelText("Message input");
fireEvent.change(input, { target: { value: content } });
expect(screen.queryByTestId("composer-slash-command")).not.toBeInTheDocument();
fireEvent.keyDown(input, { key: "Enter" });
expect(onSend).not.toHaveBeenCalled();