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

terminalContext.ts

110 lines3.3 KB

1import type { ThreadId } from "@t3tools/contracts";
2import { msg } from "gt-react";
3import {
4 formatComposerContextReference,
5 toKindScopedComposerContextId,
6} from "@t3tools/shared/composerContextReferences";
7import type { MessageResolver } from "~/i18n/messages";
8​
9export interface TerminalContextSelection {
10 terminalId: string;
11 terminalLabel: string;
12 lineStart: number;
13 lineEnd: number;
14 text: string;
16​
17export interface TerminalContextDraft extends TerminalContextSelection {
18 id: string;
19 threadId: ThreadId;
20 createdAt: string;
22​
23export const INLINE_TERMINAL_CONTEXT_PLACEHOLDER = "\uFFFC";
24​
25export interface TerminalContextReferenceSource {
26 id: string;
27 terminalLabel: string;
28 lineStart: number;
29 lineEnd: number;
31​
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​
44function 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 formatTerminalContextRange(selection: { lineStart: number; lineEnd: number }): string {
59 return selection.lineStart === selection.lineEnd
60 ? `line ${selection.lineStart}`
61 : `lines ${selection.lineStart}-${selection.lineEnd}`;
63​
64export function formatTerminalContextLabel(selection: {
65 terminalLabel: string;
66 lineStart: number;
67 lineEnd: number;
68}): string {
69 return `${selection.terminalLabel} ${formatTerminalContextRange(selection)}`;
71​
72export function terminalContextDisplayLabel(
73 {
74 terminalLabel,
75 lineStart,
76 lineEnd,
77 }: Pick<TerminalContextSelection, "terminalLabel" | "lineStart" | "lineEnd">,
78 resolve: MessageResolver,
79): string {
80 return lineStart === lineEnd
81 ? resolve(msg("{terminalLabel} line {lineStart}", { terminalLabel, lineStart }))
82 : resolve(
83 msg("{terminalLabel} lines {lineStart}-{lineEnd}", { terminalLabel, lineStart, lineEnd }),
84 );
86​
87export function terminalContextDisplayLabels(
88 contexts: ReadonlyArray<TerminalContextReferenceSource>,
89 resolve: MessageResolver,
90): ReadonlyMap<string, string> {
91 return new Map(
92 contexts.map((context) => [
93 toKindScopedComposerContextId("terminal", context.id),
94 terminalContextDisplayLabel(context, resolve),
95 ]),
96 );
98​
99export function migrateLegacyTerminalContextPlaceholders(
100 prompt: string,
101 contexts: ReadonlyArray<TerminalContextReferenceSource>,
102): string {
103 if (!prompt.includes(INLINE_TERMINAL_CONTEXT_PLACEHOLDER)) return prompt;
104 let index = 0;
105 return prompt.replaceAll(INLINE_TERMINAL_CONTEXT_PLACEHOLDER, () => {
106 const context = contexts[index];
107 index += 1;
108 return context ? formatTerminalContextReference(context) : "";
109 });