Open History to see the last commit
1import { Children, cloneElement, isValidElement, type ReactNode } from "react";
2import type { ServerProviderSkill } from "@t3tools/contracts";
3import { formatProviderSkillDisplayName } from "@t3tools/client-runtime/providerSkills";
4
5import {
6 CHAT_INLINE_CHIP_CLASS_NAME,
7 CHAT_INLINE_CHIP_LABEL_CLASS_NAME,
8 COMPOSER_INLINE_CHIP_ICON_CLASS_NAME,
9 SKILL_CHIP_ICON_SVG,
10} from "../composerInlineChip";
11import { cn } from "~/lib/utils";
12
13const SKILL_TOKEN_REGEX =
14 /(^|\s)\$(?![0-9][0-9_]*(?:[kKmMbBtT]|[eE][0-9]+)?(?:\s|$))(?=[a-zA-Z0-9:_-]*[a-zA-Z])([a-zA-Z0-9][a-zA-Z0-9:_-]*)(?=\s|$)/g;
15
16type InlineSkill = Pick<ServerProviderSkill, "name" | "displayName">;
17
18export function SkillInlineText(props: { text: string; skills: ReadonlyArray<InlineSkill> }) {
19 const nodes: ReactNode[] = [];
20 let cursor = 0;
21
22 for (const match of props.text.matchAll(SKILL_TOKEN_REGEX)) {
23 const prefix = match[1] ?? "";
24 const name = match[2] ?? "";
25 const start = (match.index ?? 0) + prefix.length;
26 const rawText = `$${name}`;
27 const skill = props.skills.find((candidate) => candidate.name === name);
28 if (!skill) {
29 continue;
30 }
31
32 if (start > cursor) {
33 nodes.push(props.text.slice(cursor, start));
34 }
35 nodes.push(<SkillChip key={`${start}:${name}`} skill={skill} rawText={rawText} />);
36 cursor = start + rawText.length;
37 }
38
39 if (cursor === 0) {
40 return <>{props.text}</>;
41 }
42 if (cursor < props.text.length) {
43 nodes.push(props.text.slice(cursor));
44 }
45 return <>{nodes}</>;
46}
47
48export function renderSkillInlineMarkdownChildren(
49 children: ReactNode,
50 skills: ReadonlyArray<InlineSkill>,
51): ReactNode {
52 return Children.map(children, (child) => {
53 if (typeof child === "string") {
54 return <SkillInlineText text={child} skills={skills} />;
55 }
56 if (!isValidElement<{ children?: ReactNode; node?: { tagName?: string } }>(child)) {
57 return child;
58 }
59 // Custom react-markdown components replace the intrinsic type, so also
60 // check the hast node they carry.
61 const markdownTagName = typeof child.type === "string" ? child.type : child.props.node?.tagName;
62 if (markdownTagName === "code" || markdownTagName === "a") {
63 return child;
64 }
65 if (!("children" in child.props)) {
66 return child;
67 }
68 return cloneElement(
69 child,
70 undefined,
71 renderSkillInlineMarkdownChildren(child.props.children, skills),
72 );
73 });
74}
75
76function SkillChip(props: { skill: InlineSkill; rawText: string }) {
77 return (
78 <span className="inline-flex align-middle leading-none" data-markdown-copy={props.rawText}>
79 <span
80 className={cn(
81 CHAT_INLINE_CHIP_CLASS_NAME,
82 "border-fuchsia-500/25 bg-fuchsia-500/12 text-fuchsia-700 dark:text-fuchsia-300",
83 )}
84 >
85 <span
86 aria-hidden="true"
87 className={COMPOSER_INLINE_CHIP_ICON_CLASS_NAME}
88 dangerouslySetInnerHTML={{ __html: SKILL_CHIP_ICON_SVG }}
89 />
90 <span className={CHAT_INLINE_CHIP_LABEL_CLASS_NAME}>
91 {formatProviderSkillDisplayName(props.skill)}
92 </span>
93 </span>
94 </span>
95 );
96}