Open History to see the last commit
1import type { CommandOutputView } from "@t3tools/client-runtime/state/command-output";
2import type { EnvironmentId, ThreadId, TurnItemId } from "@t3tools/contracts";
3import { TERMINAL_PALETTES, terminalOutputSpans } from "@t3tools/shared/terminalOutput";
4import { T, Var } from "gt-react";
5import { useLayoutEffect, useRef } from "react";
6
7import { useTheme } from "../../hooks/useTheme";
8import { useServerConfigs } from "../../state/entities";
9import { orchestrationEnvironment } from "../../state/orchestration";
10import { useEnvironmentQuery } from "../../state/query";
11import type { TurnItemSource } from "./timelineRowContext";
12
13const FOLLOW_SLACK_PX = 8;
14
15export function useCommandOutputStreaming(environmentId: EnvironmentId): boolean {
16 return (
17 useServerConfigs().get(environmentId)?.environment.capabilities.commandOutputStreaming === true
18 );
19}
20
21/**
22 * Output of one expanded command row: streams while the command runs, then
23 * shows its final output. Mount it only when `useCommandOutputStreaming` holds
24 * or a `source` serves the output.
25 * It subscribes only while mounted, so collapsed rows and other threads receive nothing.
26 */
27export function CommandOutputPanel(props: {
28 readonly environmentId: EnvironmentId;
29 readonly threadId: ThreadId;
30 readonly itemId: TurnItemId;
31 readonly source?: TurnItemSource["commandOutput"] | undefined;
32}) {
33 const input = { threadId: props.threadId, itemId: props.itemId };
34 const { data, error } = useEnvironmentQuery<CommandOutputView, unknown>(
35 props.source
36 ? props.source(input)
37 : orchestrationEnvironment.v2.commandOutput({ environmentId: props.environmentId, input }),
38 );
39 const scrollRef = useRef<HTMLDivElement>(null);
40 const followRef = useRef(true);
41 const text = data?.output.text ?? "";
42 const { resolvedTheme } = useTheme();
43
44 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- pins the output scroller's scrollTop to the bottom after each commit while the user follows
45 useLayoutEffect(() => {
46 const element = scrollRef.current;
47 if (element && followRef.current) element.scrollTop = element.scrollHeight;
48 });
49
50 if (text.length === 0) {
51 if (error) {
52 return (
53 <div className="text-destructive">
54 <T>
55 Couldn't load output: <Var>{error}</Var>
56 </T>
57 </div>
58 );
59 }
60 if (data === null) {
61 return (
62 <div className="text-muted-foreground italic">
63 <T>Loading output…</T>
64 </div>
65 );
66 }
67 return data.running ? null : (
68 <div className="text-muted-foreground italic">
69 <T>No output.</T>
70 </div>
71 );
72 }
73 // Parse after all hooks to preserve the text/theme compiler cache.
74 const spans = terminalOutputSpans(text, TERMINAL_PALETTES[resolvedTheme]);
75 return (
76 <div
77 ref={scrollRef}
78 onScroll={(event) => {
79 const element = event.currentTarget;
80 followRef.current =
81 element.scrollHeight - element.scrollTop - element.clientHeight <= FOLLOW_SLACK_PX;
82 }}
83 className="max-h-80 overflow-auto text-muted-foreground"
84 >
85 {data?.output.truncated ? (
86 <div className="italic">
87 <T>Earlier output not shown</T>
88 </div>
89 ) : null}
90 {spans.map((span, index) =>
91 span.style === null ? (
92 span.text
93 ) : (
94 // Spans never reorder and are rebuilt on each update.
95 // oxlint-disable-next-line react/no-array-index-key
96 <span key={index} style={span.style}>
97 {span.text}
98 </span>
99 ),
100 )}
101 </div>
102 );
103}