fix(webui): unify skill and session link highlighting (#6454)

* fix(webui): refine skill reference highlighting

* fix(webui): highlight composer skills and align session links
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-25 21:42:53 +08:00
1 parent d14c54c31c
commit 424d2debc2
12 files changed
+305 -91

No files matched your search

@@ -3,6 +3,7 @@ import type { ReactNode } from "react";
import { cn } from "@/lib/utils";
export const INLINE_TOKEN_HIGHLIGHT_COLOR = "var(--inline-token-highlight)";
export const INLINE_REFERENCE_HIGHLIGHT_COLOR = "var(--inline-reference-highlight)";
export function InlineTokenHighlight({
children,
@@ -24,7 +24,7 @@ import { MarkdownCodeBlock } from "@/components/markdown-diagrams/MarkdownCodeBl
import { MarkdownCodeContext } from "@/components/markdown-diagrams/context";
import { WebLink } from "@/components/WebLink";
import {
INLINE_TOKEN_HIGHLIGHT_COLOR,
INLINE_REFERENCE_HIGHLIGHT_COLOR,
InlineTokenHighlight,
} from "@/components/InlineTokenHighlight";
import {
@@ -629,9 +629,9 @@ export default function MarkdownTextRenderer({
<a
href={sessionHref}
className="rounded-sm underline-offset-2 hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/60"
style={{ textDecorationColor: INLINE_TOKEN_HIGHLIGHT_COLOR }}
style={{ textDecorationColor: INLINE_REFERENCE_HIGHLIGHT_COLOR }}
>
<InlineTokenHighlight color={INLINE_TOKEN_HIGHLIGHT_COLOR}>
<InlineTokenHighlight color={INLINE_REFERENCE_HIGHLIGHT_COLOR}>
{markdownChildren}
</InlineTokenHighlight>
</a>
@@ -0,0 +1,43 @@
import {
INLINE_REFERENCE_HIGHLIGHT_COLOR,
InlineTokenHighlight,
} from "@/components/InlineTokenHighlight";
import type { CapabilityMentionSegment } from "@/lib/composer-mention-text";
export type InlineReferenceSegment =
| CapabilityMentionSegment
| { kind: "skill"; text: string; name: string };
export function withSkillReferences(segments: CapabilityMentionSegment[]): InlineReferenceSegment[] {
const result: InlineReferenceSegment[] = [];
for (const segment of segments) {
if (segment.kind !== "text") {
result.push(segment);
continue;
}
let cursor = 0;
for (const match of segment.text.matchAll(/\$([A-Za-z0-9_-]+)/g)) {
if (match.index > cursor) {
result.push({ kind: "text", text: segment.text.slice(cursor, match.index) });
}
result.push({ kind: "skill", text: match[0], name: match[0].slice(1) });
cursor = match.index + match[0].length;
}
if (cursor < segment.text.length) {
result.push({ kind: "text", text: segment.text.slice(cursor) });
}
}
return result;
}
export function SkillReferenceToken({ name, testId }: { name: string; testId: string }) {
return (
<InlineTokenHighlight
testId={testId}
className="font-normal"
color={INLINE_REFERENCE_HIGHLIGHT_COLOR}
>
<span className="opacity-90">$</span>{name}
</InlineTokenHighlight>
);
}
+7 -68
View File
@@ -1,70 +1,11 @@
import { Fragment } from "react";
import { useTranslation } from "react-i18next";
import {
CapabilityMentionToken,
splitCapabilityMentionSegments,
} from "@/components/CliAppMentionText";
import {
INLINE_TOKEN_HIGHLIGHT_COLOR,
InlineTokenHighlight,
} from "@/components/InlineTokenHighlight";
import { SkillReferenceToken, withSkillReferences } from "@/components/SkillReferenceText";
import type { CliAppInfo, McpPresetInfo, SessionMention } from "@/lib/types";
import type { CapabilityMentionSegment } from "@/lib/composer-mention-text";
type SkillReferenceSegment =
| { kind: "text"; text: string }
| { kind: "skill"; text: string; name: string };
type UserMessageSegment =
| CapabilityMentionSegment
| { kind: "skill"; text: string; name: string };
function splitSkillReferenceSegments(value: string): SkillReferenceSegment[] {
if (!value) return [];
const segments: SkillReferenceSegment[] = [];
const referenceRe = /\$([A-Za-z0-9_-]+)/g;
let cursor = 0;
let match: RegExpExecArray | null;
while ((match = referenceRe.exec(value)) !== null) {
const name = match[1] ?? "";
if (match.index > cursor) {
segments.push({ kind: "text", text: value.slice(cursor, match.index) });
}
segments.push({
kind: "skill",
text: value.slice(match.index, referenceRe.lastIndex),
name,
});
cursor = referenceRe.lastIndex;
}
if (cursor < value.length) {
segments.push({ kind: "text", text: value.slice(cursor) });
}
return segments.length ? segments : [{ kind: "text", text: value }];
}
function splitUserMessageSegments(
value: string,
cliApps: CliAppInfo[],
mcpPresets: McpPresetInfo[],
sessionMentions: SessionMention[],
): UserMessageSegment[] {
const segments: UserMessageSegment[] = [];
for (const segment of splitCapabilityMentionSegments(
value,
cliApps,
mcpPresets,
sessionMentions,
)) {
if (segment.kind === "text") {
segments.push(...splitSkillReferenceSegments(segment.text));
} else {
segments.push(segment);
}
}
return segments;
}
export function UserMessageText({
text,
@@ -77,8 +18,9 @@ export function UserMessageText({
mcpPresets: McpPresetInfo[];
sessionMentions?: SessionMention[];
}) {
const { t } = useTranslation();
const segments = splitUserMessageSegments(text, cliApps, mcpPresets, sessionMentions);
const segments = withSkillReferences(
splitCapabilityMentionSegments(text, cliApps, mcpPresets, sessionMentions),
);
return (
<>
{segments.map((segment, index) => {
@@ -86,14 +28,11 @@ export function UserMessageText({
return <Fragment key={`text-${index}`}>{segment.text}</Fragment>;
}
if (segment.kind === "skill") return (
<InlineTokenHighlight
<SkillReferenceToken
key={`skill-${segment.name}-${index}`}
name={segment.name}
testId={`message-skill-reference-${segment.name.toLowerCase()}`}
title={t("message.skill", { name: segment.name })}
color={INLINE_TOKEN_HIGHLIGHT_COLOR}
>
{segment.text}
</InlineTokenHighlight>
/>
);
return (
<CapabilityMentionToken
+22 -9
View File
@@ -45,10 +45,14 @@ import {
mcpPresetInitials,
splitCapabilityMentionSegments,
} from "@/components/CliAppMentionText";
import {
SkillReferenceToken,
withSkillReferences,
type InlineReferenceSegment,
} from "@/components/SkillReferenceText";
import {
composerMentionText,
isCapabilityMentionStart,
type CapabilityMentionSegment,
} from "@/lib/composer-mention-text";
import { INLINE_TOKEN_HIGHLIGHT_COLOR } from "@/components/InlineTokenHighlight";
import {
@@ -1715,9 +1719,11 @@ export function ThreadComposer({
const showCliAppMenu = filteredMentionCandidates.length > 0;
const showAnyPalette = showSlashMenu || showCliAppMenu;
const hasMentionDecorations = displayMentionSegments.some(
(segment) => segment.kind !== "text",
const inlineSegments = useMemo(
() => withSkillReferences(displayMentionSegments),
[displayMentionSegments],
);
const hasInlineDecorations = inlineSegments.some((segment) => segment.kind !== "text");
const activeCliMentionApps = useMemo(() => {
const seen = new Set<string>();
return mentionSegments.flatMap((segment) => {
@@ -2705,10 +2711,10 @@ export function ThreadComposer({
) : null}
<GoalStateStrip goalState={goalState} />
<div className="thread-composer-input relative min-w-0">
{hasMentionDecorations ? (
<ComposerCliMentionOverlay
{hasInlineDecorations ? (
<ComposerInlineReferenceOverlay
overlayRef={mentionOverlayRef}
segments={displayMentionSegments}
segments={inlineSegments}
isHero={isHero}
isComposing={mentionInput.isComposing}
className={inputTextClasses}
@@ -2746,7 +2752,7 @@ export function ThreadComposer({
"relative z-10 block caret-foreground placeholder:text-muted-foreground",
"focus:outline-none focus-visible:outline-none",
"disabled:cursor-not-allowed",
hasMentionDecorations && "text-transparent selection:bg-primary/20",
hasInlineDecorations && "text-transparent selection:bg-primary/20",
)}
/>
</div>
@@ -2989,7 +2995,7 @@ function QueuedPromptPreview({
);
}
function ComposerCliMentionOverlay({
function ComposerInlineReferenceOverlay({
overlayRef,
segments,
isHero,
@@ -2998,7 +3004,7 @@ function ComposerCliMentionOverlay({
ghostRange,
}: {
overlayRef: Ref<HTMLDivElement>;
segments: CapabilityMentionSegment[];
segments: InlineReferenceSegment[];
isHero: boolean;
isComposing: boolean;
className: string;
@@ -3023,6 +3029,13 @@ function ComposerCliMentionOverlay({
if (segment.kind === "text") {
return <span key={`text-${index}`}>{segment.text}</span>;
}
if (segment.kind === "skill") {
return <SkillReferenceToken
key={`skill-${segment.name}-${index}`}
name={segment.name}
testId={`composer-skill-reference-${segment.name.toLowerCase()}`}
/>;
}
const isGhost = ghostRange?.start === start && ghostRange.end === offset;
const identity = `${segment.kind}-${segment.kind === "cli"
? segment.app.name : segment.kind === "mcp" ? segment.preset.name : segment.mention.session_key}`;
+6 -4
View File
@@ -54,6 +54,7 @@
--input: 40 8% 90.5%;
--ring: 0 0% 3.9%;
--inline-token-highlight: #ad450b;
--inline-reference-highlight: rgb(244 169 73);
--code-comment: #62626b;
--code-string: #166534;
--code-keyword: #7e22ce;
@@ -139,6 +140,7 @@
--input: var(--border);
--ring: 0 0% 83.1%;
--inline-token-highlight: #ef8e30;
--inline-reference-highlight: rgb(244 169 73);
--code-comment: #c8c8d0;
--code-string: #b5d99c;
--code-keyword: #e3a5f7;
@@ -1445,7 +1447,7 @@
.automation-create-composer .thread-composer-surface {
max-inline-size: none;
}
.automation-create-composer .thread-composer-input textarea {
.automation-create-composer .thread-composer-input > :is(textarea, [aria-hidden]) {
padding-inline: var(--settings-inset);
}
.automation-view-switch .segmented-control button {
@@ -1489,7 +1491,7 @@
white-space: nowrap;
text-overflow: ellipsis;
}
.thread-composer-collapsible[data-compact] .thread-composer-input textarea {
.thread-composer-collapsible[data-compact] .thread-composer-input > :is(textarea, [aria-hidden]) {
display: block;
block-size: 3.125rem !important;
}
@@ -1497,7 +1499,7 @@
.thread-composer-collapsible .thread-composer-input {
inline-size: 100%;
}
.thread-composer-collapsible .thread-composer-input textarea {
.thread-composer-collapsible .thread-composer-input > :is(textarea, [aria-hidden]) {
display: block;
min-block-size: 3.5rem;
padding-block: 1.125rem;
@@ -1505,7 +1507,7 @@
.thread-composer-collapsible[data-compact] .thread-composer-input {
inline-size: max(0px, calc(100% - var(--composer-compact-controls-width, 15rem) - 1.5rem));
}
.thread-composer-collapsible[data-compact] .thread-composer-input textarea {
.thread-composer-collapsible[data-compact] .thread-composer-input > :is(textarea, [aria-hidden]) {
display: block;
block-size: 3.5rem !important;
min-block-size: 3.5rem;
@@ -21,10 +21,10 @@ describe("MarkdownTextRenderer", () => {
expect(link).toHaveClass("text-blue-500", "dark:text-blue-300");
});
it("renders canonical session references as same-tab links", () => {
it.each(["#session/", "#/chat/"])("renders %s session references as same-tab links", (prefix) => {
render(
<MarkdownTextRenderer>
{"We discussed this in [收费设计](#session/websocket%3Apricing)."}
{`We discussed this in [收费设计](${prefix}websocket%3Apricing).`}
</MarkdownTextRenderer>,
);
@@ -32,7 +32,10 @@ describe("MarkdownTextRenderer", () => {
expect(link).toHaveAttribute("href", "#/chat/websocket%3Apricing");
expect(link).not.toHaveAttribute("target");
expect(link.getAttribute("style")).toContain(
"text-decoration-color: var(--inline-token-highlight)",
"text-decoration-color: var(--inline-reference-highlight)",
);
expect(link.firstElementChild?.getAttribute("style")).toContain(
"color: var(--inline-reference-highlight)",
);
});
+3 -2
View File
@@ -509,13 +509,14 @@ describe("MessageBubble", () => {
const skill = screen.getByTestId("message-skill-reference-github");
expect(skill).toHaveTextContent(/^\$github$/);
expect(skill).toHaveClass(
"font-[550]",
"font-normal",
"transition-colors",
"duration-150",
);
expect(skill.getAttribute("style")).not.toContain("text-shadow");
expect(skill.getAttribute("style")).toContain("var(--inline-token-highlight)");
expect(skill.getAttribute("style")).toContain("var(--inline-reference-highlight)");
expect(skill.className).not.toMatch(/(?:^|\s)(?:bg-|border|ring|rounded)/);
expect(skill).not.toHaveAttribute("title");
expect(screen.getByTestId("message-cli-mention-zoom")).toHaveTextContent("@Zoom");
expect(skill.parentElement).toHaveTextContent("Ask $github to review this with @Zoom");
});
+53 -2
View File
@@ -2562,6 +2562,54 @@ describe("ThreadComposer", () => {
}]);
});
it("highlights literal skill references while preserving partial edits, clipboard text and sending", () => {
const onSend = vi.fn();
render(<ThreadComposer onSend={onSend} />);
const input = screen.getByLabelText("Message input") as HTMLTextAreaElement;
const text = "like $workflow 与 $skill-creator,$";
fireEvent.change(input, { target: { value: text, selectionStart: text.length } });
expect(screen.getByTestId("composer-skill-reference-workflow")).toHaveTextContent(/^\$workflow$/);
expect(screen.getByTestId("composer-skill-reference-skill-creator")).toHaveTextContent(/^\$skill-creator$/);
expect(input).toHaveValue(text);
expect(input.previousElementSibling).toHaveAttribute("aria-hidden", "true");
expect(input.previousElementSibling?.textContent).toBe(text);
const start = text.indexOf("workflow") + 1;
const end = start + 4;
input.setSelectionRange(start, end);
const setData = vi.fn();
fireEvent.copy(input, { clipboardData: { setData } });
expect(setData).toHaveBeenCalledWith("text/plain", text.slice(start, end));
fireEvent.cut(input, { clipboardData: { setData } });
expect(input).toHaveValue(text.slice(0, start) + text.slice(end));
fireEvent.keyDown(input, { key: "z", ctrlKey: true });
expect(input).toHaveValue(text);
expect(input.selectionStart).toBe(start);
expect(input.selectionEnd).toBe(end);
fireEvent.click(screen.getByRole("button", { name: "Send message" }));
expect(onSend).toHaveBeenCalledWith(text, undefined, undefined);
});
it("mirrors skill references during composition without submitting the IME confirmation", () => {
const onSend = vi.fn();
render(<ThreadComposer onSend={onSend} />);
const input = screen.getByLabelText("Message input") as HTMLTextAreaElement;
fireEvent.change(input, { target: { value: "$workflow " } });
fireEvent.compositionStart(input);
fireEvent.change(input, { target: { value: "$workflow zhong" } });
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.compositionEnd(input, { data: "中" });
fireEvent.click(screen.getByRole("button", { name: "Send message" }));
expect(onSend).toHaveBeenCalledWith("$workflow 中", undefined, undefined);
});
it("opens skills only from a $ reference and prioritizes the skill name", () => {
const skillName = "arxiv-intelligence-filter";
render(
@@ -2613,6 +2661,8 @@ describe("ThreadComposer", () => {
fireEvent.keyDown(input, { key: "Tab" });
expect(input).toHaveValue(`please use $${skillName} `);
expect(screen.getByTestId(`composer-skill-reference-${skillName}`))
.toHaveTextContent(`$${skillName}`);
});
it("ranks skill name matches ahead of earlier description matches", () => {
@@ -2737,16 +2787,17 @@ describe("ThreadComposer", () => {
it("keeps literal mixed-script spacing in both composer input layers", () => {
render(<ThreadComposer onSend={vi.fn()} cliApps={CLI_APPS} />);
const input = screen.getByLabelText("Message input");
const value = "定位原因, 提 @gimp issue, i并提";
const value = "定位原因, 提 @gimp issue, i并提 $workflow";
fireEvent.change(input, { target: { value, selectionStart: value.length } });
const display = "定位原因, 提 @\u00a0GIMP issue, i并提";
const display = "定位原因, 提 @\u00a0GIMP issue, i并提 $workflow";
const overlay = input.previousElementSibling;
expect(input).toHaveValue(display);
expect(overlay).toHaveAttribute("aria-hidden", "true");
expect(overlay?.textContent).toBe(display);
expect(input).toHaveClass("[text-autospace:no-autospace]");
expect(overlay).toHaveClass("[text-autospace:no-autospace]");
expect(screen.getByTestId("composer-skill-reference-workflow")).toHaveTextContent("$workflow");
});
it("renders a CLI app mention logo inline without moving the text cursor slot", () => {