Open History to see the last commit
1import { collectComposerModelMentions } from "@t3tools/client-runtime/composer-model-mentions";
2import type { EnvironmentId, ScopedThreadRef, ServerProviderSkill } from "@t3tools/contracts";
3import { formatProviderSkillDisplayName } from "@t3tools/shared/inlineSkills";
4import { Children, cloneElement, isValidElement, type ReactNode } from "react";
5import { matchInlineSkills } from "@t3tools/shared/inlineSkills";
6
7import { useRightPanelStore } from "~/rightPanelStore";
8import { SKILL_CHIP_ICON_SVG } from "../composerInlineChip";
9import { ContextChipPopover } from "../contextChipParts";
10import { Button } from "../ui/button";
11import { ModelMentionChip } from "./ModelMentionChip";
12
13function SkillInlineText(props: {
14 text: string;
15 skills: ReadonlyArray<ServerProviderSkill>;
16 threadRef: ScopedThreadRef | undefined;
17 environmentId: EnvironmentId | null;
18}) {
19 const chips: Array<{ start: number; end: number; node: ReactNode }> = [];
20 for (const { start, end, skill, rawText } of matchInlineSkills(props.text, props.skills)) {
21 chips.push({
22 start,
23 end,
24 node: (
25 <SkillChipPopover
26 key={`${start}:${skill.name}`}
27 label={formatProviderSkillDisplayName(skill)}
28 description={skillPopoverDescription(skill)}
29 copyMarkdown={rawText}
30 onViewInstructions={skillInstructionsOpener(props.threadRef, skill)}
31 />
32 ),
33 });
34 }
35 for (const mention of collectComposerModelMentions(props.text)) {
36 chips.push({
37 start: mention.start,
38 end: mention.end,
39 node: (
40 <ModelMentionChip
41 key={`${mention.start}:model`}
42 environmentId={props.environmentId}
43 instanceId={mention.instanceId}
44 model={mention.model}
45 source={mention.source}
46 />
47 ),
48 });
49 }
50 if (chips.length === 0) {
51 return <>{props.text}</>;
52 }
53
54 const nodes: ReactNode[] = [];
55 let cursor = 0;
56 for (const chip of chips.toSorted((left, right) => left.start - right.start)) {
57 if (chip.start < cursor) continue;
58 if (chip.start > cursor) {
59 nodes.push(props.text.slice(cursor, chip.start));
60 }
61 nodes.push(chip.node);
62 cursor = chip.end;
63 }
64 if (cursor < props.text.length) {
65 nodes.push(props.text.slice(cursor));
66 }
67 return <>{nodes}</>;
68}
69
70export function renderSkillInlineMarkdownChildren(
71 children: ReactNode,
72 skills: ReadonlyArray<ServerProviderSkill>,
73 threadRef: ScopedThreadRef | undefined,
74 environmentId: EnvironmentId | null,
75): ReactNode {
76 return Children.map(children, (child) => {
77 if (typeof child === "string") {
78 return (
79 <SkillInlineText
80 text={child}
81 skills={skills}
82 threadRef={threadRef}
83 environmentId={environmentId}
84 />
85 );
86 }
87 if (!isValidElement<{ children?: ReactNode; node?: { tagName?: string } }>(child)) {
88 return child;
89 }
90 // Custom markdown components replace intrinsic types; check their hast tags.
91 const markdownTagName = typeof child.type === "string" ? child.type : child.props.node?.tagName;
92 if (markdownTagName === "code" || markdownTagName === "a") {
93 return child;
94 }
95 if (!("children" in child.props)) {
96 return child;
97 }
98 return cloneElement(
99 child,
100 undefined,
101 renderSkillInlineMarkdownChildren(child.props.children, skills, threadRef, environmentId),
102 );
103 });
104}
105
106export function skillPopoverDescription(
107 skill: Pick<ServerProviderSkill, "description" | "shortDescription">,
108) {
109 return skill.description?.trim() || skill.shortDescription?.trim() || null;
110}
111
112export function skillInstructionsOpener(
113 threadRef: ScopedThreadRef | null | undefined,
114 skill: Pick<ServerProviderSkill, "path"> | undefined,
115) {
116 if (!threadRef || !skill?.path) return undefined;
117 return () => useRightPanelStore.getState().openFile(threadRef, skill.path);
118}
119
120export function SkillChipPopover(props: {
121 label: string;
122 description: string | null | undefined;
123 copyMarkdown?: string | undefined;
124 onViewInstructions: (() => void) | undefined;
125}) {
126 return (
127 <ContextChipPopover
128 kind="skill"
129 icon={<SkillChipIcon />}
130 label={props.label}
131 accessibleLabel={`Skill ${props.label}`}
132 copyMarkdown={props.copyMarkdown}
133 >
134 <div className="space-y-3 p-2 text-sm">
135 <p className="font-medium">{props.label}</p>
136 <p>{props.description || "No description is available for this skill."}</p>
137 {props.onViewInstructions ? (
138 <Button variant="outline" size="sm" onClick={props.onViewInstructions}>
139 View instructions
140 </Button>
141 ) : null}
142 </div>
143 </ContextChipPopover>
144 );
145}
146
147export function SkillChipIcon() {
148 return (
149 <span
150 aria-hidden="true"
151 className="contents"
152 dangerouslySetInnerHTML={{ __html: SKILL_CHIP_ICON_SVG }}
153 />
154 );
155}