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