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

composerContextReferences.ts

130 lines4.7 KB

1import type { ComposerContextId, ComposerContextKind } from "@t3tools/contracts";
2import {
3 collectComposerContextReferences,
4 formatComposerContextReference,
5 replaceComposerContextReferences,
6} from "@t3tools/shared/composerContextReferences";
7​
8/**
9 * Prompt-string operations on inline context references, independent of kind. Each context
10 * kind's draft record supplies a `ComposerContextReference` (kind, id, label); the prompt owns
11 * where the reference sits.
12 */
13​
14export interface ComposerContextReference {
15 kind: ComposerContextKind;
16 contextId: string;
17 label: string;
19​
20/** Only for importing canonical records: undo one namespace before rebuilding a draft. */
21export function producerIdFromComposerContextId(
22 kind: ComposerContextKind,
23 contextId: string,
24): string {
25 const prefix = `${kind}_`;
26 return contextId.startsWith(prefix) ? contextId.slice(prefix.length) : contextId;
28​
29export function formatInlineContextReference(reference: ComposerContextReference): string {
30 return formatComposerContextReference({
31 kind: reference.kind,
32 contextId: reference.contextId as ComposerContextId,
33 label: reference.label,
34 });
36​
37/** Payload ids referenced by the prompt, once each in first-occurrence order. */
38export function collectInlineContextIds(prompt: string): string[] {
39 return Array.from(
40 new Set(collectComposerContextReferences(prompt).map((occurrence) => occurrence.contextId)),
41 );
43​
44/** Prose without any context link, for "does this prompt say anything" checks. */
45export function stripInlineContextReferences(prompt: string): string {
46 return replaceComposerContextReferences(prompt, () => "");
48​
49function isBoundaryWhitespace(char: string | undefined): boolean {
50 return char === undefined || char === " " || char === "\n" || char === "\t" || char === "\r";
52​
53/** Replaces a selected range with chips, keeping word boundaries and the trailing caret space. */
54export function inlineContextReferenceReplacement(
55 prompt: string,
56 selection: { start: number; end: number },
57 references: ReadonlyArray<ComposerContextReference>,
58): { start: number; end: number; text: string } {
59 const start = Math.max(0, Math.min(prompt.length, Math.floor(selection.start)));
60 const end = Math.max(start, Math.min(prompt.length, Math.floor(selection.end)));
61 const needsLeadingSpace = !isBoundaryWhitespace(prompt[start - 1]);
62 return {
63 start,
64 end: prompt[end] === " " ? end + 1 : end,
65 text: `${needsLeadingSpace ? " " : ""}${references.map(formatInlineContextReference).join(" ")} `,
66 };
68​
69/** Inserts a link at the cursor, padding with spaces only where words would otherwise join. */
70export function insertInlineContextReference(
71 prompt: string,
72 cursorInput: number,
73 reference: ComposerContextReference,
74): { prompt: string; cursor: number } {
75 const edit = inlineContextReferenceReplacement(prompt, { start: cursorInput, end: cursorInput }, [
76 reference,
77 ]);
78 return {
79 prompt: `${prompt.slice(0, edit.start)}${edit.text}${prompt.slice(edit.end)}`,
80 cursor: edit.start + edit.text.length,
81 };
83​
84/** Appends a link at the end of the prompt: the fallback when the caret is unknown. */
85export function appendInlineContextReference(
86 prompt: string,
87 reference: ComposerContextReference,
88): string {
89 return insertInlineContextReference(prompt, prompt.length, reference).prompt;
91​
92/** Removes every reference to `contextId` plus one neighbouring space each so words don't join. */
93export function removeInlineContextReference(
94 prompt: string,
95 contextId: string,
96): { prompt: string; cursor: number } {
97 const occurrences = collectComposerContextReferences(prompt).filter(
98 (candidate) => candidate.contextId === contextId,
99 );
100 if (occurrences.length === 0) return { prompt, cursor: prompt.length };
101 let result = prompt;
102 let cursor = prompt.length;
103 for (const occurrence of occurrences.reverse()) {
104 let { start, end } = occurrence;
105 if (result[end] === " ") end += 1;
106 else if (result[start - 1] === " ") start -= 1;
107 result = `${result.slice(0, start)}${result.slice(end)}`;
108 cursor = start;
109 }
110 if (cursor >= result.length) {
111 result = result.trimEnd();
112 cursor = result.length;
113 }
114 return { prompt: result, cursor };
116​
117/** Appends links for records the prompt does not reference yet, in the order given. */
118export function ensureInlineContextReferences(
119 prompt: string,
120 references: ReadonlyArray<ComposerContextReference>,
121): string {
122 const referenced = new Set(collectInlineContextIds(prompt));
123 let result = prompt;
124 for (const reference of references) {
125 if (referenced.has(reference.contextId)) continue;
126 referenced.add(reference.contextId);
127 result = appendInlineContextReference(result, reference);
128 }
129 return result;