Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

composer-editor-mentions.ts

179 lines4.7 KB

1import type { AssistantCitation } from "@t3tools/contracts";
2import { collectAssistantCitations } from "@t3tools/shared/assistantCitations";
3import { collectComposerContextReferences } from "@t3tools/shared/composerContextReferences";
4import {
5 collectComposerInlineTokens,
6 type ComposerInlineToken,
7} from "@t3tools/shared/composerInlineTokens";
8​
9export type ComposerPromptSegment =
10 | {
11 type: "text";
12 text: string;
13 }
14 | {
15 type: "mention";
16 path: string;
17 source: string;
18 }
19 | {
20 type: "skill";
21 name: string;
22 }
23 | {
24 type: "citation";
25 citation: AssistantCitation;
26 source: string;
27 }
28 | {
29 type: "context-reference";
30 kind: string;
31 contextId: string;
32 label: string;
33 source: string;
34 };
35​
36function rangeIncludesIndex(start: number, end: number, index: number): boolean {
37 return start <= index && index < end;
39​
40function pushTextSegment(segments: ComposerPromptSegment[], text: string): void {
41 if (!text) return;
42 const last = segments[segments.length - 1];
43 if (last && last.type === "text") {
44 last.text += text;
45 return;
46 }
47 segments.push({ type: "text", text });
49​
50function forEachPromptTextSlice(
51 prompt: string,
52 visitor: (text: string, promptOffset: number) => boolean | void,
53): boolean {
54 return prompt.length > 0 && visitor(prompt, 0) === true;
56​
57function forEachMentionMatch(
58 prompt: string,
59 visitor: (
60 match: Extract<ComposerInlineToken, { type: "mention" }>,
61 promptOffset: number,
62 ) => boolean | void,
63): boolean {
64 return forEachPromptTextSlice(prompt, (text, promptOffset) => {
65 for (const match of collectComposerPromptInlineTokens(text)) {
66 if (match.type !== "mention") {
67 continue;
68 }
69 if (visitor(match, promptOffset) === true) {
70 return true;
71 }
72 }
73 return false;
74 });
76​
77export function collectComposerPromptInlineTokens(text: string) {
78 const tokens = collectComposerInlineTokens(text);
79 const citations = collectAssistantCitations(text);
80 const references = collectComposerContextReferences(text);
81 if (citations.length === 0 && references.length === 0) return tokens;
82​
83 // An unfinished @ mention can otherwise consume the start of a link label.
84 const links = [
85 ...citations.map((match) => ({ ...match, type: "citation" as const })),
86 ...references.map((match) => ({ ...match, type: "context-reference" as const })),
87 ];
88 return [
89 ...tokens.filter(
90 (token) => !links.some((link) => token.start < link.end && token.end > link.start),
91 ),
92 ...links,
93 ].sort((left, right) => left.start - right.start);
95​
96function splitPromptTextIntoComposerSegments(text: string): ComposerPromptSegment[] {
97 const segments: ComposerPromptSegment[] = [];
98 if (!text) {
99 return segments;
100 }
101​
102 const tokenMatches = collectComposerPromptInlineTokens(text);
103 let cursor = 0;
104 for (const match of tokenMatches) {
105 if (match.start < cursor) {
106 continue;
107 }
108​
109 if (match.start > cursor) {
110 pushTextSegment(segments, text.slice(cursor, match.start));
111 }
112​
113 if (match.type === "citation") {
114 segments.push({ type: "citation", citation: match.citation, source: match.source });
115 } else if (match.type === "context-reference") {
116 segments.push({
117 type: "context-reference",
118 kind: match.kind,
119 contextId: match.contextId,
120 label: match.label,
121 source: match.source,
122 });
123 } else if (match.type === "mention") {
124 segments.push({
125 type: "mention",
126 path: match.value,
127 source: match.source,
128 });
129 } else {
130 segments.push({ type: "skill", name: match.value });
131 }
132​
133 cursor = match.end;
134 }
135​
136 if (cursor < text.length) {
137 pushTextSegment(segments, text.slice(cursor));
138 }
139​
140 return segments;
142​
143export function selectionTouchesMentionBoundary(
144 prompt: string,
145 start: number,
146 end: number,
147): boolean {
148 if (!prompt || start >= end) {
149 return false;
150 }
151​
152 return forEachMentionMatch(prompt, (match, promptOffset) => {
153 const mentionStart = promptOffset + match.start;
154 const mentionEnd = promptOffset + match.end;
155 const beforeMentionIndex = mentionStart - 1;
156 const afterMentionIndex = mentionEnd;
157​
158 if (
159 beforeMentionIndex >= 0 &&
160 /\s/.test(prompt[beforeMentionIndex] ?? "") &&
161 rangeIncludesIndex(start, end, beforeMentionIndex)
162 ) {
163 return true;
164 }
165​
166 if (
167 afterMentionIndex < prompt.length &&
168 /\s/.test(prompt[afterMentionIndex] ?? "") &&
169 rangeIncludesIndex(start, end, afterMentionIndex)
170 ) {
171 return true;
172 }
173 return false;
174 });
176​
177export function splitPromptIntoComposerSegments(prompt: string): ComposerPromptSegment[] {
178 return splitPromptTextIntoComposerSegments(prompt);