Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CommandOutputPanel.tsx

85 lines3.2 KB

1import type { EnvironmentId, ThreadId, TurnItemId } from "@t3tools/contracts";
2import { TERMINAL_PALETTES, terminalOutputSpans } from "@t3tools/shared/terminalOutput";
3import { useLayoutEffect, useMemo, 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 );
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 */
25export function CommandOutputPanel(props: {
26 readonly environmentId: EnvironmentId;
27 readonly threadId: ThreadId;
28 readonly itemId: TurnItemId;
29}) {
30 const { data, error } = useEnvironmentQuery(
31 orchestrationEnvironment.v2.commandOutput({
32 environmentId: props.environmentId,
33 input: { threadId: props.threadId, itemId: props.itemId },
34 }),
35 );
36 const scrollRef = useRef<HTMLDivElement>(null);
37 // Follow new output until the reader scrolls up; scrolling back down resumes.
38 const followRef = useRef(true);
39 const text = data?.output.text ?? "";
40 const { resolvedTheme } = useTheme();
41 const spans = useMemo(
42 () => terminalOutputSpans(text, TERMINAL_PALETTES[resolvedTheme]),
43 [text, resolvedTheme],
44 );
45​
46 // This panel only re-renders when its output changes, so keep the end in view after each.
47 useLayoutEffect(() => {
48 const element = scrollRef.current;
49 if (element && followRef.current) element.scrollTop = element.scrollHeight;
50 });
51​
52 if (text.length === 0) {
53 if (error) return <div className="text-destructive">Couldn&apos;t load output: {error}</div>;
54 if (data === null) return <div className="text-muted-foreground italic">Loading output…</div>;
55 return data.running ? null : <div className="text-muted-foreground italic">No output.</div>;
56 }
57 return (
58 <div>
59 {data?.output.truncated ? (
60 <div className="text-muted-foreground italic">Earlier output not shown</div>
61 ) : null}
62 <div
63 ref={scrollRef}
64 onScroll={(event) => {
65 const element = event.currentTarget;
66 followRef.current =
67 element.scrollHeight - element.scrollTop - element.clientHeight <= FOLLOW_SLACK_PX;
68 }}
69 className="max-h-80 overflow-auto text-muted-foreground"
70 >
71 {spans.map((span, index) =>
72 span.style === null ? (
73 span.text
74 ) : (
75 // Spans are rebuilt from scratch on every update and never reorder.
76 // oxlint-disable-next-line react/no-array-index-key
77 <span key={index} style={span.style}>
78 {span.text}
79 </span>
80 ),
81 )}
82 </div>
83 </div>
84 );