Open History to see the last commit
1import type { EnvironmentId, ThreadId, TurnItemId } from "@t3tools/contracts";
2import { TERMINAL_PALETTES, terminalOutputSpans } from "@t3tools/shared/terminalOutput";
3import type React from "react";
4import { useMemo, useRef } from "react";
5import { ScrollView, View } from "react-native";
6
7import { AppText as Text } from "../../components/ui/AppText";
8import { useServerConfigs } from "../../state/entities";
9import { orchestrationEnvironment } from "../../state/orchestration";
10import { useEnvironmentQuery } from "../../state/query";
11
12// Within this many points of the end still counts as following the output.
13const FOLLOW_SLACK = 8;
14
15/** True when the environment streams command output; older servers only return final output. */
16export function useCommandOutputStreaming(environmentId: EnvironmentId): boolean {
17 return (
18 useServerConfigs().get(environmentId)?.environment.capabilities.commandOutputStreaming === true
19 );
20}
21
22/**
23 * Output of one expanded command row: streams while the command runs, then
24 * shows its final output. Mount it only when `useCommandOutputStreaming` holds.
25 * It subscribes only while mounted, so collapsed rows and other threads receive nothing.
26 */
27export function ThreadCommandOutput(props: {
28 readonly environmentId: EnvironmentId;
29 readonly threadId: ThreadId;
30 readonly itemId: TurnItemId;
31 readonly themeAppearance: "light" | "dark";
32}) {
33 const { data, error } = useEnvironmentQuery(
34 orchestrationEnvironment.v2.commandOutput({
35 environmentId: props.environmentId,
36 input: { threadId: props.threadId, itemId: props.itemId },
37 }),
38 );
39 const scrollRef = useRef<React.ComponentRef<typeof ScrollView>>(null);
40 // Follow new output until the reader scrolls up; scrolling back down resumes.
41 const followRef = useRef(true);
42 const text = data?.output.text ?? "";
43 const spans = useMemo(
44 () => terminalOutputSpans(text, TERMINAL_PALETTES[props.themeAppearance]),
45 [text, props.themeAppearance],
46 );
47 if (text.length === 0) {
48 const message = error
49 ? `Couldn't load output: ${error}`
50 : data === null
51 ? "Loading output…"
52 : data.running
53 ? null
54 : "No output.";
55 return message === null ? null : (
56 <Text className="pt-1.5 font-mono text-2xs leading-normal text-foreground-muted">
57 {message}
58 </Text>
59 );
60 }
61 return (
62 <View className="pt-1.5">
63 {data?.output.truncated ? (
64 <Text className="pb-0.5 text-3xs text-foreground-subtle">Earlier output not shown</Text>
65 ) : null}
66 <ScrollView
67 ref={scrollRef}
68 nestedScrollEnabled
69 directionalLockEnabled
70 showsVerticalScrollIndicator
71 className="max-h-60"
72 contentContainerStyle={{ paddingRight: 8 }}
73 scrollEventThrottle={100}
74 onScroll={(event) => {
75 const { contentOffset, contentSize, layoutMeasurement } = event.nativeEvent;
76 followRef.current =
77 contentSize.height - contentOffset.y - layoutMeasurement.height <= FOLLOW_SLACK;
78 }}
79 onContentSizeChange={() => {
80 if (followRef.current) scrollRef.current?.scrollToEnd({ animated: false });
81 }}
82 >
83 <Text selectable className="font-mono text-2xs leading-normal text-foreground-muted">
84 {spans.map((span, index) =>
85 span.style === null ? (
86 span.text
87 ) : (
88 // Spans are rebuilt from scratch on every update and never reorder.
89 // oxlint-disable-next-line react/no-array-index-key
90 <Text key={index} style={span.style}>
91 {span.text}
92 </Text>
93 ),
94 )}
95 </Text>
96 </ScrollView>
97 </View>
98 );
99}