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:
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>
|
||||
);
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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}`;
|
||||
|
||||
Reference in new issue
Block a user