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

composer-editor-mentions.ts

215 lines6.1 KB

1import { collectComposerModelMentions } from "@t3tools/client-runtime/composer-model-mentions";
2import type { AssistantCitation } from "@t3tools/contracts";
3import { collectAssistantCitations } from "@t3tools/shared/assistantCitations";
4import { collectComposerContextReferences } from "@t3tools/shared/composerContextReferences";
5import {
6 collectComposerInlineTokens,
7 composerMathRanges,
8 type ComposerInlineToken,
9} from "@t3tools/shared/composerInlineTokens";
10import { markdownMathRanges } from "~/markdown/wordsHast";
11​
12export type ComposerPromptSegment =
13 | {
14 type: "text";
15 text: string;
16 }
17 | {
18 type: "mention";
19 path: string;
20 source: string;
21 }
22 | {
23 type: "skill";
24 name: string;
25 source: string;
26 }
27 | {
28 type: "model";
29 instanceId: string;
30 model: string;
31 source: string;
32 }
33 | {
34 type: "citation";
35 citation: AssistantCitation;
36 source: string;
37 }
38 | {
39 type: "context-reference";
40 kind: string;
41 contextId: string;
42 label: string;
43 source: string;
44 };
45​
46function rangeIncludesIndex(start: number, end: number, index: number): boolean {
47 return start <= index && index < end;
49​
50function pushTextSegment(segments: ComposerPromptSegment[], text: string): void {
51 if (!text) return;
52 const last = segments[segments.length - 1];
53 if (last && last.type === "text") {
54 last.text += text;
55 return;
56 }
57 segments.push({ type: "text", text });
59​
60function forEachPromptTextSlice(
61 prompt: string,
62 visitor: (text: string, promptOffset: number) => boolean | void,
63): boolean {
64 return prompt.length > 0 && visitor(prompt, 0) === true;
66​
67function forEachMentionMatch(
68 prompt: string,
69 visitor: (
70 match: Extract<ComposerInlineToken, { type: "mention" }>,
71 promptOffset: number,
72 ) => boolean | void,
73): boolean {
74 return forEachPromptTextSlice(prompt, (text, promptOffset) => {
75 for (const match of collectComposerPromptInlineTokens(text)) {
76 if (match.type !== "mention") {
77 continue;
78 }
79 if (visitor(match, promptOffset) === true) {
80 return true;
81 }
82 }
83 return false;
84 });
86​
87// Like `$skill`, a typed reference waits for whitespace so `gpt-6` cannot chip while typing `gpt-6.1`.
88function collectModelTokens(text: string, inlineTokens: ReadonlyArray<ComposerInlineToken>) {
89 const mentions = collectComposerModelMentions(text).filter((mention) =>
90 /\s/.test(text[mention.end] ?? ""),
91 );
92 if (mentions.length === 0) return [];
93 const taken = [...inlineTokens, ...composerMathRanges(text, markdownMathRanges)];
94 return mentions
95 .filter(
96 (mention) => !taken.some((range) => mention.start < range.end && mention.end > range.start),
97 )
98 .map((mention) => ({ ...mention, type: "model" as const }));
100​
101export function collectComposerPromptInlineTokens(text: string) {
102 const inlineTokens = collectComposerInlineTokens(text, { findMath: markdownMathRanges });
103 const models = collectModelTokens(text, inlineTokens);
104 const tokens =
105 models.length === 0
106 ? inlineTokens
107 : [...inlineTokens, ...models].sort((left, right) => left.start - right.start);
108 const citations = collectAssistantCitations(text);
109 const references = collectComposerContextReferences(text);
110 if (citations.length === 0 && references.length === 0) return tokens;
111​
112 // An unfinished @ mention can otherwise consume the start of a link label.
113 const links = [
114 ...citations.map((match) => ({ ...match, type: "citation" as const })),
115 ...references.map((match) => ({ ...match, type: "context-reference" as const })),
116 ];
117 return [
118 ...tokens.filter(
119 (token) => !links.some((link) => token.start < link.end && token.end > link.start),
120 ),
121 ...links,
122 ].sort((left, right) => left.start - right.start);
124​
125function splitPromptTextIntoComposerSegments(text: string): ComposerPromptSegment[] {
126 const segments: ComposerPromptSegment[] = [];
127 if (!text) {
128 return segments;
129 }
130​
131 const tokenMatches = collectComposerPromptInlineTokens(text);
132 let cursor = 0;
133 for (const match of tokenMatches) {
134 if (match.start < cursor) {
135 continue;
136 }
137​
138 if (match.start > cursor) {
139 pushTextSegment(segments, text.slice(cursor, match.start));
140 }
141​
142 if (match.type === "citation") {
143 segments.push({ type: "citation", citation: match.citation, source: match.source });
144 } else if (match.type === "context-reference") {
145 segments.push({
146 type: "context-reference",
147 kind: match.kind,
148 contextId: match.contextId,
149 label: match.label,
150 source: match.source,
151 });
152 } else if (match.type === "mention") {
153 segments.push({
154 type: "mention",
155 path: match.value,
156 source: match.source,
157 });
158 } else if (match.type === "model") {
159 segments.push({
160 type: "model",
161 instanceId: match.instanceId,
162 model: match.model,
163 source: match.source,
164 });
165 } else {
166 segments.push({ type: "skill", name: match.value, source: match.source });
167 }
168​
169 cursor = match.end;
170 }
171​
172 if (cursor < text.length) {
173 pushTextSegment(segments, text.slice(cursor));
174 }
175​
176 return segments;
178​
179export function selectionTouchesMentionBoundary(
180 prompt: string,
181 start: number,
182 end: number,
183): boolean {
184 if (!prompt || start >= end) {
185 return false;
186 }
187​
188 return forEachMentionMatch(prompt, (match, promptOffset) => {
189 const mentionStart = promptOffset + match.start;
190 const mentionEnd = promptOffset + match.end;
191 const beforeMentionIndex = mentionStart - 1;
192 const afterMentionIndex = mentionEnd;
193​
194 if (
195 beforeMentionIndex >= 0 &&
196 /\s/.test(prompt[beforeMentionIndex] ?? "") &&
197 rangeIncludesIndex(start, end, beforeMentionIndex)
198 ) {
199 return true;
200 }
201​
202 if (
203 afterMentionIndex < prompt.length &&
204 /\s/.test(prompt[afterMentionIndex] ?? "") &&
205 rangeIncludesIndex(start, end, afterMentionIndex)
206 ) {
207 return true;
208 }
209 return false;
210 });
212​
213export function splitPromptIntoComposerSegments(prompt: string): ComposerPromptSegment[] {
214 return splitPromptTextIntoComposerSegments(prompt);