The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { |
| 2 | EnvironmentId, |
| 3 | OrchestrationV2PendingBackgroundTask, |
| 4 | ThreadId, |
| 5 | } from "@t3tools/contracts"; |
| 6 | import type { ThreadStructure } from "@t3tools/client-runtime/state/shell"; |
| 7 | import { backgroundWorkHoldsCompletion } from "@t3tools/shared/orchestrationV2PendingBackgroundWork"; |
| 8 | import { T, useGT } from "gt-react"; |
| 9 | import { useState } from "react"; |
| 10 | |
| 11 | import { InlineButton } from "../ui/button"; |
| 12 | import { |
| 13 | Dialog, |
| 14 | DialogHeader, |
| 15 | DialogPanel, |
| 16 | DialogPopup, |
| 17 | DialogTitle, |
| 18 | DialogTrigger, |
| 19 | } from "../ui/dialog"; |
| 20 | import { CommandOutputPanel, useCommandOutputStreaming } from "./CommandOutputPanel"; |
| 21 | import { ShellCommandBlock } from "./ShellCommandBlock"; |
| 22 | |
| 23 | export function BackgroundWorkDetails(props: { |
| 24 | readonly environmentId: EnvironmentId; |
| 25 | readonly threadId: ThreadId; |
| 26 | readonly tasks: ReadonlyArray<OrchestrationV2PendingBackgroundTask>; |
| 27 | readonly turnItems: ThreadStructure["turnItems"]; |
| 28 | readonly onOpenThread: (threadId: ThreadId) => void; |
| 29 | }) { |
| 30 | const gt = useGT(); |
| 31 | const kindLabel = (kind: string) => { |
| 32 | switch (kind) { |
| 33 | case "subagent": |
| 34 | return gt("subagent", { $context: "kind of background work" }); |
| 35 | case "command": |
| 36 | return gt("command", { $context: "kind of background work" }); |
| 37 | case "monitor": |
| 38 | return gt("monitor", { $context: "kind of background work" }); |
| 39 | case "background_task": |
| 40 | return gt("background task", { $context: "kind of background work" }); |
| 41 | default: |
| 42 | return kind.replaceAll("_", " "); |
| 43 | } |
| 44 | }; |
| 45 | const [open, setOpen] = useState(false); |
| 46 | const streamsOutput = useCommandOutputStreaming(props.environmentId); |
| 47 | return ( |
| 48 | <Dialog open={open} onOpenChange={setOpen}> |
| 49 | <DialogTrigger render={<InlineButton />}> |
| 50 | <T>Details</T> |
| 51 | </DialogTrigger> |
| 52 | <DialogPopup> |
| 53 | <DialogHeader> |
| 54 | <DialogTitle> |
| 55 | <T>Background work</T> |
| 56 | </DialogTitle> |
| 57 | </DialogHeader> |
| 58 | <DialogPanel> |
| 59 | <div className="space-y-5"> |
| 60 | {props.tasks.map((task) => { |
| 61 | const item = props.turnItems.findLast((candidate) => |
| 62 | task.itemId === undefined |
| 63 | ? (candidate.nativeItemRef?.nativeId || candidate.id) === task.taskId |
| 64 | : candidate.id === task.itemId, |
| 65 | ); |
| 66 | const command = |
| 67 | task.command ?? (item?.type === "command_execution" ? item.input : undefined); |
| 68 | const description = task.description?.trim(); |
| 69 | const title = |
| 70 | description && description !== command?.trim() |
| 71 | ? description |
| 72 | : command |
| 73 | ? gt("Command", { $context: "title of a background shell command" }) |
| 74 | : kindLabel(task.kind); |
| 75 | const itemId = |
| 76 | task.itemId ?? (item?.type === "command_execution" ? item.id : undefined); |
| 77 | const childThreadId = task.kind === "subagent" ? task.childThreadId : undefined; |
| 78 | return ( |
| 79 | <section key={task.taskId} className="space-y-2 text-sm"> |
| 80 | <div className="flex items-baseline justify-between gap-3"> |
| 81 | <h3 className="font-medium">{title}</h3> |
| 82 | <span className="shrink-0 text-xs text-muted-foreground"> |
| 83 | {backgroundWorkHoldsCompletion([task]) |
| 84 | ? gt("Waiting", { $context: "status of background work" }) |
| 85 | : gt("Running", { $context: "status of background work" })} |
| 86 | </span> |
| 87 | </div> |
| 88 | {command ? ( |
| 89 | <div className="rounded-md border border-border bg-muted/20 p-3 font-mono text-xs whitespace-pre-wrap break-words"> |
| 90 | <ShellCommandBlock command={command} /> |
| 91 | </div> |
| 92 | ) : null} |
| 93 | {itemId && streamsOutput && item?.status === "running" ? ( |
| 94 | <div className="font-mono text-xs whitespace-pre-wrap break-words"> |
| 95 | <CommandOutputPanel |
| 96 | environmentId={props.environmentId} |
| 97 | threadId={task.sourceThreadId ?? item?.threadId ?? props.threadId} |
| 98 | itemId={itemId} |
| 99 | /> |
| 100 | </div> |
| 101 | ) : null} |
| 102 | {childThreadId ? ( |
| 103 | <InlineButton |
| 104 | onClick={() => { |
| 105 | setOpen(false); |
| 106 | props.onOpenThread(childThreadId); |
| 107 | }} |
| 108 | > |
| 109 | <T>Open subagent</T> |
| 110 | </InlineButton> |
| 111 | ) : null} |
| 112 | </section> |
| 113 | ); |
| 114 | })} |
| 115 | </div> |
| 116 | </DialogPanel> |
| 117 | </DialogPopup> |
| 118 | </Dialog> |
| 119 | ); |
| 120 | } |