Open History to see the last commit
1import {
2 type EnvironmentId,
3 type OrchestrationV2PendingBackgroundTask,
4 type OrchestrationV2RemoteTaskChild,
5 type ScopedThreadRef,
6 type ThreadId,
7} from "@t3tools/contracts";
8import { scopeThreadRef } from "@t3tools/client-runtime/environment";
9import type { ThreadStructure } from "@t3tools/client-runtime/state/shell";
10import { backgroundWorkHoldsCompletion } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
11import { T, useGT } from "gt-react";
12import { useState } from "react";
13
14import { useSideThreadTrigger } from "../../hooks/useSideThreadTrigger";
15import { useLinkedThreadOpener } from "../../state/linkedThreadView";
16import { Button, InlineButton } from "../ui/button";
17import {
18 Dialog,
19 DialogHeader,
20 DialogPanel,
21 DialogPopup,
22 DialogTitle,
23 DialogTrigger,
24} from "../ui/dialog";
25import { CommandOutputPanel, useCommandOutputStreaming } from "./CommandOutputPanel";
26import { ShellCommandBlock } from "./ShellCommandBlock";
27
28function RemoteSubagentOpenButton(props: {
29 readonly parentRef: ScopedThreadRef;
30 readonly remoteChild: OrchestrationV2RemoteTaskChild;
31 readonly title: string;
32 readonly onOpened: () => void;
33}) {
34 const opener = useLinkedThreadOpener(props.parentRef, props.remoteChild, props.title);
35 const trigger = useSideThreadTrigger();
36 if (!opener) return null;
37 return (
38 <Button
39 size="sm"
40 variant="outline"
41 {...trigger(
42 (intent) => {
43 props.onOpened();
44 opener.open(intent);
45 },
46 { panel: opener.mode === "linked" },
47 )}
48 >
49 <T>Open subagent</T>
50 </Button>
51 );
52}
53
54export function BackgroundWorkDetails(props: {
55 readonly environmentId: EnvironmentId;
56 readonly threadId: ThreadId;
57 readonly tasks: ReadonlyArray<OrchestrationV2PendingBackgroundTask>;
58 readonly turnItems: ThreadStructure["turnItems"];
59 readonly onOpenThread: (threadId: ThreadId) => void;
60}) {
61 const gt = useGT();
62 const kindLabel = (kind: string) => {
63 switch (kind) {
64 case "subagent":
65 return gt({ context: "kind of background work" })`subagent`;
66 case "command":
67 return gt({ context: "kind of background work" })`command`;
68 case "monitor":
69 return gt({ context: "kind of background work" })`monitor`;
70 case "background_task":
71 return gt({ context: "kind of background work" })`background task`;
72 default:
73 return kind.replaceAll("_", " ");
74 }
75 };
76 const [open, setOpen] = useState(false);
77 const streamsOutput = useCommandOutputStreaming(props.environmentId);
78 const parentRef = scopeThreadRef(props.environmentId, props.threadId);
79 return (
80 <Dialog open={open} onOpenChange={setOpen}>
81 <DialogTrigger render={<InlineButton />}>
82 <T>Details</T>
83 </DialogTrigger>
84 <DialogPopup>
85 <DialogHeader>
86 <DialogTitle>
87 <T>Background work</T>
88 </DialogTitle>
89 </DialogHeader>
90 <DialogPanel>
91 <div className="space-y-5">
92 {props.tasks.map((task) => {
93 const item = props.turnItems.findLast((candidate) =>
94 task.itemId === undefined
95 ? (candidate.nativeItemRef?.nativeId || candidate.id) ===
96 (task.nativeItemId ?? task.taskId)
97 : candidate.id === task.itemId,
98 );
99 const command =
100 task.command ?? (item?.type === "command_execution" ? item.input : undefined);
101 const description = task.description?.trim();
102 const title =
103 description && description !== command?.trim()
104 ? description
105 : command
106 ? gt({ context: "title of a background shell command" })`Command`
107 : kindLabel(task.kind);
108 const itemId =
109 task.itemId ?? (item?.type === "command_execution" ? item.id : undefined);
110 const childThreadId = task.kind === "subagent" ? task.childThreadId : undefined;
111 const remoteChild =
112 task.kind === "subagent" && item?.type === "subagent"
113 ? item.remoteChild
114 : undefined;
115 return (
116 <section key={task.taskId} className="space-y-2 text-sm">
117 <div className="flex items-baseline justify-between gap-3">
118 <h3 className="font-medium">{title}</h3>
119 <span className="shrink-0 text-xs text-muted-foreground">
120 {backgroundWorkHoldsCompletion([task])
121 ? gt({ context: "status of background work" })`Waiting`
122 : gt({ context: "status of background work" })`Running`}
123 </span>
124 </div>
125 {command ? (
126 <div className="rounded-md border border-border bg-muted/20 p-3 font-mono text-xs whitespace-pre-wrap break-words">
127 <ShellCommandBlock command={command} />
128 </div>
129 ) : null}
130 {itemId && streamsOutput && item?.status === "running" ? (
131 <div className="font-mono text-xs whitespace-pre-wrap break-words">
132 <CommandOutputPanel
133 environmentId={props.environmentId}
134 threadId={task.sourceThreadId ?? item?.threadId ?? props.threadId}
135 itemId={itemId}
136 />
137 </div>
138 ) : null}
139 {childThreadId ? (
140 <Button
141 size="sm"
142 variant="outline"
143 onClick={() => {
144 setOpen(false);
145 props.onOpenThread(childThreadId);
146 }}
147 >
148 <T>Open subagent</T>
149 </Button>
150 ) : remoteChild ? (
151 <RemoteSubagentOpenButton
152 parentRef={parentRef}
153 remoteChild={remoteChild}
154 title={title}
155 onOpened={() => setOpen(false)}
156 />
157 ) : null}
158 </section>
159 );
160 })}
161 </div>
162 </DialogPanel>
163 </DialogPopup>
164 </Dialog>
165 );
166}