fix(webui): match slash command highlighting to skills
This commit is contained in:
1 parent
dfb3843b4d
commit
3230013963
6 files changed
+97
-21
No files matched your search
@@ -4,13 +4,17 @@ import { MessageBubble } from "@/components/MessageBubble";
|
||||
import { ThreadComposer } from "@/components/thread/ThreadComposer";
|
||||
import { ThemeProvider, useTheme } from "@/hooks/useTheme";
|
||||
import { useReadingTypography } from "@/hooks/useReadingTypography";
|
||||
import type { ChatSummary, UIMessage } from "@/lib/types";
|
||||
import type { ChatSummary, SlashCommand, UIMessage } from "@/lib/types";
|
||||
|
||||
const session: ChatSummary = {
|
||||
key: "websocket:typography", channel: "websocket", chatId: "typography",
|
||||
createdAt: null, updatedAt: null, title: "Reference target", preview: "",
|
||||
handle: { id: "handle_12345678901234567890123456789012", name: "typography" },
|
||||
};
|
||||
const commands: SlashCommand[] = [{
|
||||
command: "/goal", title: "Start a goal", description: "Start a sustained goal.",
|
||||
icon: "activity", lifecycle: "agent_turn_with_args", acceptsArgs: true,
|
||||
}];
|
||||
const answer: UIMessage = {
|
||||
id: "reference-answer", role: "assistant", createdAt: 1,
|
||||
content: "[相关会话](#session/websocket%3Atypography) · [外部文档](https://example.com/reference)",
|
||||
@@ -22,8 +26,8 @@ export function InlineReferencesFixture() {
|
||||
const [sent, setSent] = useState<UIMessage[]>([]);
|
||||
return <ThemeProvider theme={theme}>
|
||||
<main className="mx-auto h-full max-w-[var(--content-column-width)] space-y-6 overflow-y-auto p-4">
|
||||
{[answer, ...sent].map(message => <MessageBubble key={message.id} message={message} />)}
|
||||
<ThreadComposer compactWhenIdle sessions={[session]} modelLabel="Fixture" persistDraft={false}
|
||||
{[answer, ...sent].map(message => <MessageBubble key={message.id} message={message} slashCommands={commands} />)}
|
||||
<ThreadComposer compactWhenIdle sessions={[session]} modelLabel="Fixture" persistDraft={false} slashCommands={commands}
|
||||
onSend={text => setSent(current => [...current, {
|
||||
id: `sent-${current.length}`, role: "user", content: text, createdAt: current.length + 2,
|
||||
}])} />
|
||||
|
||||
@@ -78,9 +78,15 @@ for (const narrow of [false, true]) {
|
||||
|
||||
const input = page.getByRole("textbox", { name: "Message input", exact: true });
|
||||
const overlay = page.locator(".thread-composer-input > div[aria-hidden]");
|
||||
const text = "like $workflow 和 $skill-creator,检查中文 English 与换行。";
|
||||
const text = "/goal like $workflow 和 $skill-creator,检查中文 English 与换行。";
|
||||
await input.focus();
|
||||
await input.pressSequentially(text);
|
||||
await input.pressSequentially("/goal ");
|
||||
const composerCommand = page.getByTestId("composer-slash-command");
|
||||
await expect(composerCommand).toHaveText("/goal");
|
||||
await expect(composerCommand).toHaveCSS("color", "rgb(244, 169, 73)");
|
||||
await expect(composerCommand).toHaveCSS("font-weight", "400");
|
||||
await expectMatchingInputLayers(input);
|
||||
await input.pressSequentially(text.slice("/goal ".length));
|
||||
const skill = page.getByTestId("composer-skill-reference-workflow");
|
||||
await expect(skill).toHaveText("$workflow");
|
||||
await expect(skill).toHaveCSS("color", "rgb(244, 169, 73)");
|
||||
@@ -119,7 +125,15 @@ for (const narrow of [false, true]) {
|
||||
await page.getByRole("button", { name: "Send message", exact: true }).click();
|
||||
await expect(page.getByTestId("message-skill-reference-workflow")).toHaveText("$workflow");
|
||||
await expect(page.getByTestId("message-skill-reference-workflow")).toHaveCSS("color", "rgb(244, 169, 73)");
|
||||
const command = page.getByTestId("message-slash-command");
|
||||
const sentSkill = page.getByTestId("message-skill-reference-workflow");
|
||||
await expect(command).toHaveText("/goal");
|
||||
await expect(command).toHaveCSS("color", await sentSkill.evaluate(node => getComputedStyle(node).color));
|
||||
await expect(command).toHaveCSS("font-weight", "400");
|
||||
await expect(command.locator("span")).toHaveText("/");
|
||||
await expect(command.locator("span")).toHaveCSS("opacity", "0.9");
|
||||
await expect(page.locator("[data-user-text-bubble]").last().locator("p")).toHaveText(text);
|
||||
await page.screenshot({ path: info.outputPath("slash-command-and-skill.png") });
|
||||
expect(errors).toEqual([]);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
);
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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");
|
||||
});
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user