Open History to see the last commit
1import type { EnvironmentId, ThreadId, TurnItemId } from "@t3tools/contracts";
2import { TERMINAL_PALETTES, terminalOutputSpans } from "@t3tools/shared/terminalOutput";
3import { useLayoutEffect, useRef } from "react";
4
5import { useTheme } from "../../hooks/useTheme";
6import { useServerConfigs } from "../../state/entities";
7import { orchestrationEnvironment } from "../../state/orchestration";
8import { useEnvironmentQuery } from "../../state/query";
9
10// Within this many pixels of the end still counts as following the output.
11const FOLLOW_SLACK_PX = 8;
12
13/** True when the environment streams command output; older servers only return final output. */
14export function useCommandOutputStreaming(environmentId: EnvironmentId): boolean {
15 return (
16 useServerConfigs().get(environmentId)?.environment.capabilities.commandOutputStreaming === true
17 );
18}
19
20/**
21 * Output of one expanded command row: streams while the command runs, then
22 * shows its final output. Mount it only when `useCommandOutputStreaming` holds.
23 * It subscribes only while mounted, so collapsed rows and other threads receive nothing.
24 */
25// Parsing ANSI output is linear in its length, and the compiler would redo it on
26// every render because the layout effect runs after it.
27function useTerminalOutputSpans(text: string, resolvedTheme: keyof typeof TERMINAL_PALETTES) {
28 "use memo";
29 return terminalOutputSpans(text, TERMINAL_PALETTES[resolvedTheme]);
30}
31
32export function CommandOutputPanel(props: {
33 readonly environmentId: EnvironmentId;
34 readonly threadId: ThreadId;
35 readonly itemId: TurnItemId;
36}) {
37 const { data, error } = useEnvironmentQuery(
38 orchestrationEnvironment.v2.commandOutput({
39 environmentId: props.environmentId,
40 input: { threadId: props.threadId, itemId: props.itemId },
41 }),
42 );
43 const scrollRef = useRef<HTMLDivElement>(null);
44 // Follow new output until the reader scrolls up; scrolling back down resumes.
45 const followRef = useRef(true);
46 const text = data?.output.text ?? "";
47 const { resolvedTheme } = useTheme();
48 const spans = useTerminalOutputSpans(text, resolvedTheme);
49
50 // This panel only re-renders when its output changes, so keep the end in view after each.
51 useLayoutEffect(() => {
52 const element = scrollRef.current;
53 if (element && followRef.current) element.scrollTop = element.scrollHeight;
54 });
55
56 if (text.length === 0) {
57 if (error) return <div className="text-destructive">Couldn't load output: {error}</div>;
58 if (data === null) return <div className="text-muted-foreground italic">Loading output…</div>;
59 return data.running ? null : <div className="text-muted-foreground italic">No output.</div>;
60 }
61 return (
62 <div>
63 {data?.output.truncated ? (
64 <div className="text-muted-foreground italic">Earlier output not shown</div>
65 ) : null}
66 <div
67 ref={scrollRef}
68 onScroll={(event) => {
69 const element = event.currentTarget;
70 followRef.current =
71 element.scrollHeight - element.scrollTop - element.clientHeight <= FOLLOW_SLACK_PX;
72 }}
73 className="max-h-80 overflow-auto text-muted-foreground"
74 >
75 {spans.map((span, index) =>
76 span.style === null ? (
77 span.text
78 ) : (
79 // Spans are rebuilt from scratch on every update and never reorder.
80 // oxlint-disable-next-line react/no-array-index-key
81 <span key={index} style={span.style}>
82 {span.text}
83 </span>
84 ),
85 )}
86 </div>
87 </div>
88 );
89}