Files
nanobot/webui/src/components/CliAppMentionText.tsx
T

410 lines
13 KiB
TypeScript
Raw Normal View History

import { useMemo, type ReactNode } from "react";
2026-08-03 16:03:31 +08:00
import { useTranslation } from "react-i18next";
2026-05-22 22:25:12 +08:00
import {
INLINE_TOKEN_HIGHLIGHT_COLOR,
InlineTokenHighlight,
} from "@/components/InlineTokenHighlight";
2026-07-13 13:11:46 +08:00
import { useLogoFallback } from "@/hooks/useLogoFallback";
import { logoFallbackUrls } from "@/lib/provider-brand";
import type {
CliAppInfo,
McpPresetInfo,
SessionHandle,
SessionMention,
} from "@/lib/types";
2026-05-22 22:25:12 +08:00
import { cn } from "@/lib/utils";
2026-07-07 14:42:41 +08:00
type CliAppMentionSegment =
2026-05-22 22:25:12 +08:00
| { kind: "text"; text: string }
| { kind: "cli"; text: string; app: CliAppInfo };
export type CapabilityMentionSegment =
| CliAppMentionSegment
2026-08-02 22:04:59 +08:00
| { kind: "mcp"; text: string; preset: McpPresetInfo }
| { kind: "handle"; text: string; handle: SessionHandle };
export type SessionReferenceSegment =
| { kind: "text"; text: string }
2026-08-02 22:04:59 +08:00
| { kind: "session"; text: string; mention: SessionMention };
export interface TokenSelection<T> {
mention: T;
start: number;
end: number;
}
export type SessionHandleSelection = TokenSelection<SessionHandle>;
export type SessionMentionSelection = TokenSelection<SessionMention>;
const SESSION_HANDLE_COLOR_COUNT = 8;
export function sessionHandleColor(colorSlot: number): string {
const slot = Number.isFinite(colorSlot)
? Math.abs(Math.trunc(colorSlot)) % SESSION_HANDLE_COLOR_COUNT
: 0;
return `var(--session-handle-${slot})`;
}
export function SessionHandleHighlight({
handle,
children,
className,
testId,
}: {
handle: Pick<SessionHandle, "color_slot" | "name">;
children: ReactNode;
className?: string;
testId?: string;
}) {
return (
<span
className="inline border-b-2"
style={{ borderBottomColor: sessionHandleColor(handle.color_slot) }}
>
<InlineTokenHighlight
testId={testId}
className={cn("text-foreground", className)}
>
{children}
</InlineTokenHighlight>
</span>
);
}
2026-05-22 22:25:12 +08:00
export function cliAppInitials(app: CliAppInfo): string {
const value = app.display_name || app.name;
return (
value
.split(/\s+/)
.filter(Boolean)
.slice(0, 2)
.map((part) => part[0]?.toUpperCase())
.join("") || app.name.slice(0, 2).toUpperCase()
);
}
export function mcpPresetInitials(preset: Pick<McpPresetInfo, "name" | "display_name">): string {
const value = preset.display_name || preset.name;
return (
value
.split(/\s+/)
.filter(Boolean)
.slice(0, 2)
.map((part) => part[0]?.toUpperCase())
.join("") || preset.name.slice(0, 2).toUpperCase()
);
}
export function splitCapabilityMentionSegments(
value: string,
cliApps: CliAppInfo[],
mcpPresets: McpPresetInfo[] = [],
sessionHandles: SessionHandle[] = [],
handleSelections?: SessionHandleSelection[],
): CapabilityMentionSegment[] {
if (!value || (cliApps.length === 0 && mcpPresets.length === 0 && sessionHandles.length === 0)) {
return value ? [{ kind: "text", text: value }] : [];
}
const cliAppsByName = new Map(
cliApps
.filter((app) => app.installed)
.map((app) => [app.name.toLowerCase(), app]),
);
const mcpPresetsByName = new Map(
mcpPresets
.filter((preset) => preset.installed && preset.configured)
.map((preset) => [preset.name.toLowerCase(), preset]),
);
const handlesByName = new Map(
sessionHandles.map((handle) => [handle.name.toLowerCase(), handle]),
2026-08-02 22:04:59 +08:00
);
const selectedSessionNames = new Set(
(handleSelections ?? []).map((selection) => selection.mention.name.toLowerCase()),
);
if (cliAppsByName.size === 0 && mcpPresetsByName.size === 0 && handlesByName.size === 0) {
return [{ kind: "text", text: value }];
}
const segments: CapabilityMentionSegment[] = [];
2026-08-02 22:04:59 +08:00
const mentionRe = /(^|[\s([{])@([\p{L}\p{N}_-]+)(?=$|[^\p{L}\p{N}_-])/giu;
let cursor = 0;
let match: RegExpExecArray | null;
while ((match = mentionRe.exec(value)) !== null) {
const prefix = match[1] ?? "";
const name = match[2] ?? "";
const key = name.toLowerCase();
const mentionStart = match.index + prefix.length;
const mentionEnd = mentionStart + name.length + 1;
const handle = handleSelections
? selectedSessionNames.has(key) ? handlesByName.get(key) : undefined
: handlesByName.get(key);
const app = handle ? null : cliAppsByName.get(key);
const preset = handle || app ? null : mcpPresetsByName.get(key);
if (!app && !preset && !handle) continue;
if (mentionStart > cursor) {
segments.push({ kind: "text", text: value.slice(cursor, mentionStart) });
}
if (app) {
segments.push({ kind: "cli", text: value.slice(mentionStart, mentionEnd), app });
} else if (preset) {
segments.push({ kind: "mcp", text: value.slice(mentionStart, mentionEnd), preset });
} else if (handle) {
segments.push({ kind: "handle", text: value.slice(mentionStart, mentionEnd), handle });
}
cursor = mentionEnd;
}
if (cursor < value.length) segments.push({ kind: "text", text: value.slice(cursor) });
return segments.length ? segments : [{ kind: "text", text: value }];
}
export function splitSessionReferenceSegments(
value: string,
sessionMentions: SessionMention[] = [],
sessionSelections?: SessionMentionSelection[],
allowLegacyAt = false,
): SessionReferenceSegment[] {
if (!value || sessionMentions.length === 0) return value ? [{ kind: "text", text: value }] : [];
const sessionsByName = new Map(
sessionMentions.map((mention) => [mention.name.toLowerCase(), mention]),
);
const selectedSessionByStart = new Map(
(sessionSelections ?? []).map((selection) => [selection.start, selection]),
);
const segments: SessionReferenceSegment[] = [];
const referenceRe = allowLegacyAt
? /(^|[\s([{])([#@])([\p{L}\p{N}_-]+)(?=$|[^\p{L}\p{N}_-])/giu
: /(^|[\s([{])(#)([\p{L}\p{N}_-]+)(?=$|[^\p{L}\p{N}_-])/giu;
let cursor = 0;
let match: RegExpExecArray | null;
while ((match = referenceRe.exec(value)) !== null) {
const prefix = match[1] ?? "";
const name = match[3] ?? "";
const start = match.index + prefix.length;
const end = start + name.length + 1;
const selected = selectedSessionByStart.get(start);
const mention = sessionSelections
? selected?.end === end && selected.mention.name.toLowerCase() === name.toLowerCase()
? selected.mention
: undefined
: sessionsByName.get(name.toLowerCase());
if (!mention) continue;
if (start > cursor) segments.push({ kind: "text", text: value.slice(cursor, start) });
segments.push({ kind: "session", text: value.slice(start, end), mention });
cursor = end;
}
if (cursor < value.length) segments.push({ kind: "text", text: value.slice(cursor) });
return segments.length ? segments : [{ kind: "text", text: value }];
}
export function CapabilityMentionToken({
segment,
variant,
isHero = false,
}: {
segment: Exclude<CapabilityMentionSegment, { kind: "text" }>;
variant: "composer" | "message";
isHero?: boolean;
}) {
if (segment.kind === "cli") {
return (
<CliAppMentionToken
app={segment.app}
label={segment.text}
variant={variant}
isHero={isHero}
/>
);
}
if (segment.kind === "mcp") {
return (
<McpPresetMentionToken
preset={segment.preset}
label={segment.text}
variant={variant}
isHero={isHero}
/>
);
}
return <SessionHandleToken handle={segment.handle} label={segment.text} variant={variant} />;
}
export function SessionHandleToken({
handle,
label,
variant,
}: {
handle: SessionHandle;
label: string;
variant: "composer" | "message";
}) {
const testIdPrefix = variant === "composer" ? "composer" : "message";
const color = sessionHandleColor(handle.color_slot);
const token = (
<SessionHandleHighlight
handle={handle}
testId={`${testIdPrefix}-handle-mention-${handle.name}`}
className={variant === "composer" ? "font-normal" : undefined}
>
{label}
</SessionHandleHighlight>
);
if (variant === "composer" || !handle.session_key) return token;
return (
<a
href={`#/chat/${encodeURIComponent(handle.session_key)}`}
className="rounded-sm underline-offset-2 hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/60"
style={{ textDecorationColor: color }}
>
{token}
</a>
);
}
export function SessionReferenceToken({
2026-08-02 22:04:59 +08:00
mention,
label,
variant,
}: {
mention: SessionMention;
label: string;
variant: "composer" | "message";
}) {
const testIdPrefix = variant === "composer" ? "composer" : "message";
2026-08-02 23:15:54 +08:00
const token = (
2026-08-02 22:04:59 +08:00
<InlineTokenHighlight
testId={`${testIdPrefix}-session-reference-${mention.name}`}
2026-08-02 22:04:59 +08:00
title={`Session: ${mention.title || mention.name}`}
color={INLINE_TOKEN_HIGHLIGHT_COLOR}
2026-08-06 17:48:28 +08:00
className={variant === "composer" ? "font-normal" : undefined}
2026-08-02 22:04:59 +08:00
>
{label}
</InlineTokenHighlight>
);
2026-08-02 23:15:54 +08:00
if (variant === "composer") return token;
return (
<a
href={`#/chat/${encodeURIComponent(mention.session_key)}`}
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 }}
2026-08-02 23:15:54 +08:00
>
{token}
</a>
);
2026-08-02 22:04:59 +08:00
}
2026-05-22 22:25:12 +08:00
export function CliAppMentionToken({
app,
label,
variant,
isHero = false,
}: {
app: CliAppInfo;
label: string;
variant: "composer" | "message";
isHero?: boolean;
}) {
2026-08-03 16:03:31 +08:00
const { t } = useTranslation();
const color = app.brand_color || INLINE_TOKEN_HIGHLIGHT_COLOR;
2026-05-22 22:25:12 +08:00
const mentionName = label.startsWith("@") ? label.slice(1) : label;
const logoUrls = useMemo(() => logoFallbackUrls(app.logo_url), [app.logo_url]);
2026-07-13 13:11:46 +08:00
const { logoUrl, onLogoError, onLogoLoad } = useLogoFallback(logoUrls);
const showLogo = Boolean(logoUrl);
2026-05-22 22:25:12 +08:00
const testIdPrefix = variant === "composer" ? "composer" : "message";
return (
<InlineTokenHighlight
testId={`${testIdPrefix}-cli-mention-${app.name}`}
2026-08-03 16:03:31 +08:00
title={t("thread.composer.mentions.cliTitle", { name: app.display_name || app.name })}
color={color}
2026-08-06 17:48:28 +08:00
className={variant === "composer" ? "font-normal" : undefined}
2026-05-22 22:25:12 +08:00
>
<span
className={cn("relative inline-block", showLogo && "text-transparent")}
style={{ lineHeight: "inherit" }}
>
@
{showLogo ? (
<span
data-testid={`${testIdPrefix}-cli-mention-logo-${app.name}`}
className={cn(
2026-08-14 07:07:09 +09:00
"absolute left-1/2 top-1/2 grid place-items-center overflow-hidden rounded-mark",
2026-05-22 22:25:12 +08:00
"-translate-x-1/2 -translate-y-1/2",
isHero ? "h-[0.74em] w-[0.74em]" : "h-[0.72em] w-[0.72em]",
)}
>
<img
src={logoUrl ?? ""}
2026-05-22 22:25:12 +08:00
alt=""
className="h-full w-full object-contain"
2026-07-13 13:11:46 +08:00
decoding="async"
loading="lazy"
onLoad={onLogoLoad}
onError={onLogoError}
/>
</span>
) : null}
</span>
{mentionName}
</InlineTokenHighlight>
);
}
export function McpPresetMentionToken({
preset,
label,
variant,
isHero = false,
}: {
preset: McpPresetInfo;
label: string;
variant: "composer" | "message";
isHero?: boolean;
}) {
2026-08-03 16:03:31 +08:00
const { t } = useTranslation();
const color = preset.brand_color || INLINE_TOKEN_HIGHLIGHT_COLOR;
const mentionName = label.startsWith("@") ? label.slice(1) : label;
const logoUrls = useMemo(() => logoFallbackUrls(preset.logo_url), [preset.logo_url]);
2026-07-13 13:11:46 +08:00
const { logoUrl, onLogoError, onLogoLoad } = useLogoFallback(logoUrls);
const showLogo = Boolean(logoUrl);
const testIdPrefix = variant === "composer" ? "composer" : "message";
return (
<InlineTokenHighlight
testId={`${testIdPrefix}-mcp-mention-${preset.name}`}
2026-08-03 16:03:31 +08:00
title={t("thread.composer.mentions.mcpTitle", { name: preset.display_name || preset.name })}
color={color}
2026-08-06 17:48:28 +08:00
className={variant === "composer" ? "font-normal" : undefined}
>
<span
className={cn("relative inline-block", showLogo && "text-transparent")}
style={{ lineHeight: "inherit" }}
>
@
{showLogo ? (
<span
data-testid={`${testIdPrefix}-mcp-mention-logo-${preset.name}`}
className={cn(
2026-08-14 07:07:09 +09:00
"absolute left-1/2 top-1/2 grid place-items-center overflow-hidden rounded-mark",
"-translate-x-1/2 -translate-y-1/2",
isHero ? "h-[0.74em] w-[0.74em]" : "h-[0.72em] w-[0.72em]",
)}
>
<img
src={logoUrl ?? ""}
alt=""
className="h-full w-full object-contain"
2026-07-13 13:11:46 +08:00
decoding="async"
loading="lazy"
onLoad={onLogoLoad}
onError={onLogoError}
2026-05-22 22:25:12 +08:00
/>
</span>
) : null}
</span>
{mentionName}
</InlineTokenHighlight>
2026-05-22 22:25:12 +08:00
);
}