Open History to see the last commit
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;
15}
16
17export interface TerminalContextDraft extends TerminalContextSelection {
18 id: string;
19 threadId: ThreadId;
20 createdAt: string;
21}
22
23export const INLINE_TERMINAL_CONTEXT_PLACEHOLDER = "\uFFFC";
24
25export interface TerminalContextReferenceSource {
26 id: string;
27 terminalLabel: string;
28 lineStart: number;
29 lineEnd: number;
30}
31
32export function formatTerminalContextReference(context: TerminalContextReferenceSource): string {
33 return formatComposerContextReference({
34 kind: "terminal",
35 contextId: toKindScopedComposerContextId("terminal", context.id),
36 label: formatTerminalContextLabel(context),
37 });
38}
39
40export function normalizeTerminalContextText(text: string): string {
41 return text.replace(/\r\n/g, "\n").replace(/^\n+|\n+$/g, "");
42}
43
44function hasTerminalContextText(context: { text: string }): boolean {
45 return normalizeTerminalContextText(context.text).length > 0;
46}
47
48export function isTerminalContextExpired(context: { text: string }): boolean {
49 return !hasTerminalContextText(context);
50}
51
52export function filterTerminalContextsWithText<T extends { text: string }>(
53 contexts: ReadonlyArray<T>,
54): T[] {
55 return contexts.filter((context) => hasTerminalContextText(context));
56}
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}`;
62}
63
64export function formatTerminalContextLabel(selection: {
65 terminalLabel: string;
66 lineStart: number;
67 lineEnd: number;
68}): string {
69 return `${selection.terminalLabel} ${formatTerminalContextRange(selection)}`;
70}
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 }}`)
82 : resolve(msg`${{ terminalLabel }} lines ${{ lineStart }}-${{ lineEnd }}`);
83}
84
85export function terminalContextDisplayLabels(
86 contexts: ReadonlyArray<TerminalContextReferenceSource>,
87 resolve: MessageResolver,
88): ReadonlyMap<string, string> {
89 return new Map(
90 contexts.map((context) => [
91 toKindScopedComposerContextId("terminal", context.id),
92 terminalContextDisplayLabel(context, resolve),
93 ]),
94 );
95}
96
97export function migrateLegacyTerminalContextPlaceholders(
98 prompt: string,
99 contexts: ReadonlyArray<TerminalContextReferenceSource>,
100): string {
101 if (!prompt.includes(INLINE_TERMINAL_CONTEXT_PLACEHOLDER)) return prompt;
102 let index = 0;
103 return prompt.replaceAll(INLINE_TERMINAL_CONTEXT_PLACEHOLDER, () => {
104 const context = contexts[index];
105 index += 1;
106 return context ? formatTerminalContextReference(context) : "";
107 });
108}