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

terminalContext.ts

104 lines3.3 KB

1import type { ThreadId } from "@t3tools/contracts";
2import {
3 formatComposerContextReference,
4 toKindScopedComposerContextId,
5} from "@t3tools/shared/composerContextReferences";
6​
7export interface TerminalContextSelection {
8 terminalId: string;
9 terminalLabel: string;
10 lineStart: number;
11 lineEnd: number;
12 text: string;
14​
15export interface TerminalContextDraft extends TerminalContextSelection {
16 id: string;
17 threadId: ThreadId;
18 createdAt: string;
20​
21/** Legacy ordinal placeholder from drafts saved before context references. Migration only. */
22export const INLINE_TERMINAL_CONTEXT_PLACEHOLDER = "\uFFFC";
23​
24export interface TerminalContextReferenceSource {
25 id: string;
26 terminalLabel: string;
27 lineStart: number;
28 lineEnd: number;
30​
31/** The canonical inline link that stands for this context in the prompt. */
32export function formatTerminalContextReference(context: TerminalContextReferenceSource): string {
33 return formatComposerContextReference({
34 kind: "terminal",
35 contextId: toKindScopedComposerContextId("terminal", context.id),
36 label: formatTerminalContextLabel(context),
37 });
39​
40export function normalizeTerminalContextText(text: string): string {
41 return text.replace(/\r\n/g, "\n").replace(/^\n+|\n+$/g, "");
43​
44export function hasTerminalContextText(context: { text: string }): boolean {
45 return normalizeTerminalContextText(context.text).length > 0;
47​
48export function isTerminalContextExpired(context: { text: string }): boolean {
49 return !hasTerminalContextText(context);
51​
52export function filterTerminalContextsWithText<T extends { text: string }>(
53 contexts: ReadonlyArray<T>,
54): T[] {
55 return contexts.filter((context) => hasTerminalContextText(context));
57​
58function normalizeTerminalContextSelection(
59 selection: TerminalContextSelection,
60): TerminalContextSelection | null {
61 const text = normalizeTerminalContextText(selection.text);
62 const terminalId = selection.terminalId.trim();
63 const terminalLabel = selection.terminalLabel.trim();
64 if (text.length === 0 || terminalId.length === 0 || terminalLabel.length === 0) {
65 return null;
66 }
67 const lineStart = Math.max(1, Math.floor(selection.lineStart));
68 const lineEnd = Math.max(lineStart, Math.floor(selection.lineEnd));
69 return {
70 terminalId,
71 terminalLabel,
72 lineStart,
73 lineEnd,
74 text,
75 };
77​
78function formatTerminalContextRange(selection: { lineStart: number; lineEnd: number }): string {
79 return selection.lineStart === selection.lineEnd
80 ? `line ${selection.lineStart}`
81 : `lines ${selection.lineStart}-${selection.lineEnd}`;
83​
84export function formatTerminalContextLabel(selection: {
85 terminalLabel: string;
86 lineStart: number;
87 lineEnd: number;
88}): string {
89 return `${selection.terminalLabel} ${formatTerminalContextRange(selection)}`;
91​
92/** Binds legacy U+FFFC placeholders to contexts in array order; leftover placeholders vanish. */
93export function migrateLegacyTerminalContextPlaceholders(
94 prompt: string,
95 contexts: ReadonlyArray<TerminalContextReferenceSource>,
96): string {
97 if (!prompt.includes(INLINE_TERMINAL_CONTEXT_PLACEHOLDER)) return prompt;
98 let index = 0;
99 return prompt.replaceAll(INLINE_TERMINAL_CONTEXT_PLACEHOLDER, () => {
100 const context = contexts[index];
101 index += 1;
102 return context ? formatTerminalContextReference(context) : "";
103 });