@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

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