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

terminalContext.ts

101 lines3.0 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​
21export const INLINE_TERMINAL_CONTEXT_PLACEHOLDER = "\uFFFC";
22​
23export interface TerminalContextReferenceSource {
24 id: string;
25 terminalLabel: string;
26 lineStart: number;
27 lineEnd: number;
29​
30export function formatTerminalContextReference(context: TerminalContextReferenceSource): string {
31 return formatComposerContextReference({
32 kind: "terminal",
33 contextId: toKindScopedComposerContextId("terminal", context.id),
34 label: formatTerminalContextLabel(context),
35 });
37​
38export function normalizeTerminalContextText(text: string): string {
39 return text.replace(/\r\n/g, "\n").replace(/^\n+|\n+$/g, "");
41​
42export function hasTerminalContextText(context: { text: string }): boolean {
43 return normalizeTerminalContextText(context.text).length > 0;
45​
46export function isTerminalContextExpired(context: { text: string }): boolean {
47 return !hasTerminalContextText(context);
49​
50export function filterTerminalContextsWithText<T extends { text: string }>(
51 contexts: ReadonlyArray<T>,
52): T[] {
53 return contexts.filter((context) => hasTerminalContextText(context));
55​
56function normalizeTerminalContextSelection(
57 selection: TerminalContextSelection,
58): TerminalContextSelection | null {
59 const text = normalizeTerminalContextText(selection.text);
60 const terminalId = selection.terminalId.trim();
61 const terminalLabel = selection.terminalLabel.trim();
62 if (text.length === 0 || terminalId.length === 0 || terminalLabel.length === 0) {
63 return null;
64 }
65 const lineStart = Math.max(1, Math.floor(selection.lineStart));
66 const lineEnd = Math.max(lineStart, Math.floor(selection.lineEnd));
67 return {
68 terminalId,
69 terminalLabel,
70 lineStart,
71 lineEnd,
72 text,
73 };
75​
76function formatTerminalContextRange(selection: { lineStart: number; lineEnd: number }): string {
77 return selection.lineStart === selection.lineEnd
78 ? `line ${selection.lineStart}`
79 : `lines ${selection.lineStart}-${selection.lineEnd}`;
81​
82export function formatTerminalContextLabel(selection: {
83 terminalLabel: string;
84 lineStart: number;
85 lineEnd: number;
86}): string {
87 return `${selection.terminalLabel} ${formatTerminalContextRange(selection)}`;
89​
90export function migrateLegacyTerminalContextPlaceholders(
91 prompt: string,
92 contexts: ReadonlyArray<TerminalContextReferenceSource>,
93): string {
94 if (!prompt.includes(INLINE_TERMINAL_CONTEXT_PLACEHOLDER)) return prompt;
95 let index = 0;
96 return prompt.replaceAll(INLINE_TERMINAL_CONTEXT_PLACEHOLDER, () => {
97 const context = contexts[index];
98 index += 1;
99 return context ? formatTerminalContextReference(context) : "";
100 });