Open History to see the last commit
1import * as Dialog from "../../components/ui/Dialog";
2import {
3 ComposerContextId,
4 COMPOSER_CONTEXT_TERMINAL_TEXT_MAX_CHARS,
5 type EnvironmentId,
6 type ThreadId,
7} from "@t3tools/contracts";
8import { formatComposerContextReference } from "@t3tools/shared/composerContextReferences";
9import { useState } from "react";
10import { Platform, Pressable, ScrollView, View } from "react-native";
11import { useSafeAreaInsets } from "react-native-safe-area-context";
12import { REVIEW_MONO_FONT_FAMILY } from "../review/reviewDiffRendering";
13import { AppText as Text } from "../../components/ui/AppText";
14import { Sheet } from "../../components/ui/Sheet";
15import { Button } from "../../components/ui/Button";
16import { uuidv4 } from "../../lib/uuid";
17import { insertComposerDraftContext } from "../../state/use-composer-drafts";
18
19export function TerminalContextSheet(props: {
20 text: string;
21 environmentId: EnvironmentId;
22 threadId: ThreadId;
23 terminalId: string;
24 terminalLabel: string;
25 onClose: () => void;
26 onAttach: () => void;
27}) {
28 const insets = useSafeAreaInsets();
29 const lines = props.text.replace(/\n+$/, "").split("\n");
30 const [range, setRange] = useState({ start: 0, end: lines.length - 1 });
31 const [anchor, setAnchor] = useState<number | null>(null);
32 const selectedText = lines.slice(range.start, range.end + 1).join("\n");
33 const tooLarge = selectedText.length > COMPOSER_CONTEXT_TERMINAL_TEXT_MAX_CHARS;
34 const attach = () => {
35 if (!selectedText.trim() || tooLarge) return;
36 const record = {
37 version: 1 as const,
38 kind: "terminal" as const,
39 contextId: ComposerContextId.make(uuidv4()),
40 label: `${props.terminalLabel} · visible lines ${range.start + 1}–${range.end + 1}`,
41 terminalId: props.terminalId,
42 terminalLabel: `${props.terminalLabel} (visible output)`,
43 lineStart: range.start + 1,
44 lineEnd: range.end + 1,
45 text: selectedText,
46 };
47 if (
48 !insertComposerDraftContext(`${props.environmentId}:${props.threadId}`, {
49 text: formatComposerContextReference(record),
50 context: { version: 1, records: [record] },
51 })
52 ) {
53 Dialog.alert("Too many context items", "Remove some context from the draft and try again.");
54 return;
55 }
56 props.onAttach();
57 };
58 return (
59 <Sheet title="Terminal output" onClose={props.onClose}>
60 <ScrollView
61 nestedScrollEnabled
62 className="flex-1"
63 contentInsetAdjustmentBehavior={Platform.OS === "ios" ? "automatic" : "never"}
64 contentContainerStyle={{ paddingHorizontal: 20, paddingTop: 12, paddingBottom: 20 }}
65 >
66 <Text className="pb-4 text-sm text-foreground-muted">
67 Tap the first and last line to select a range.
68 </Text>
69 <View className="overflow-hidden rounded-xl bg-card-alt">
70 {lines.map((line, index) => (
71 <Pressable
72 key={index}
73 accessibilityRole="button"
74 accessibilityLabel={`Line ${index + 1}: ${line}`}
75 accessibilityState={{ selected: index >= range.start && index <= range.end }}
76 onPress={() => {
77 if (anchor === null) {
78 setAnchor(index);
79 setRange({ start: index, end: index });
80 } else {
81 setRange({ start: Math.min(anchor, index), end: Math.max(anchor, index) });
82 setAnchor(null);
83 }
84 }}
85 className={
86 index >= range.start && index <= range.end
87 ? "flex-row gap-3 bg-primary/10 px-3 py-2"
88 : "flex-row gap-3 px-3 py-2"
89 }
90 >
91 <Text
92 className="w-8 text-right text-sm text-foreground-muted"
93 style={{ fontFamily: REVIEW_MONO_FONT_FAMILY }}
94 >
95 {index + 1}
96 </Text>
97 <Text
98 className="flex-1 text-sm text-foreground"
99 style={{ fontFamily: REVIEW_MONO_FONT_FAMILY }}
100 >
101 {line || " "}
102 </Text>
103 </Pressable>
104 ))}
105 </View>
106 </ScrollView>
107 <View
108 className="gap-3 border-t border-border px-5 pt-4"
109 style={{ paddingBottom: Math.max(16, insets.bottom) }}
110 >
111 {tooLarge ? (
112 <Text className="text-sm text-foreground-muted">
113 Select fewer lines to fit the context limit.
114 </Text>
115 ) : null}
116 <Button
117 label="Attach selected output"
118 tone="primary"
119 fullWidth
120 disabled={!selectedText.trim() || tooLarge}
121 onPress={attach}
122 />
123 </View>
124 </Sheet>
125 );
126}