Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "~/index.css";
import { EnvironmentId, ProviderDriverKind, ThreadId, TurnItemId } from "@t3tools/contracts";
import * as DateTime from "effect/DateTime";
import { expect, it, vi } from "vite-plus/test";
import { page } from "vite-plus/test/browser";
import { page, userEvent } from "vite-plus/test/browser";
vi.mock(import("./CommandOutputPanel"), () => ({
useCommandOutputStreaming: () => true,
CommandOutputPanel: () => <div>Command output stream</div>,
Expand Down Expand Up @@ -77,22 +77,25 @@ it("opens full command details for short labels and keeps pending tasks current"
);
await using screen = await render(view("Audit"));
await screen.getByRole("button", { name: "Details", exact: true }).click();
await vi.waitFor(() => expect(page.getByRole("dialog").element().textContent).toContain(command));
const popup = page.getByRole("dialog");
await expect.element(popup.getByText(command)).not.toBeInTheDocument();
await popup.getByRole("button", { name: /^Audit/ }).click();
await vi.waitFor(() => expect(popup.element().textContent).toContain(command));
await expect.element(page.getByText("Command output stream")).not.toBeInTheDocument();

await screen.rerender(view(command));
await expect.element(page.getByRole("heading", { name: "Command", exact: true })).toBeVisible();
await expect.element(popup.getByRole("button", { name: /^python3 audit\.py/ })).toBeVisible();
await screen.rerender(view());
await expect.element(page.getByRole("heading", { name: "Command", exact: true })).toBeVisible();
await expect.element(popup.getByRole("button", { name: /^python3 audit\.py/ })).toBeVisible();

await screen.rerender(view("Check remaining modules", "running"));
await expect
.element(page.getByRole("heading", { name: "Check remaining modules" }))
.element(popup.getByRole("button", { name: /^Check remaining modules/ }))
.toBeVisible();
await expect.element(page.getByText("Command output stream")).toBeVisible();
await screen.rerender(view("Native fallback", "completed", false));
await expect.element(page.getByText("Command output stream")).toBeVisible();
await page.getByRole("button", { name: "Close", exact: true }).click();
await userEvent.keyboard("{Escape}");
await expect.element(page.getByRole("dialog")).not.toBeInTheDocument();
});

Expand Down
248 changes: 162 additions & 86 deletions apps/web/src/components/chat/BackgroundWorkDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,41 @@ import type {
ThreadId,
} from "@t3tools/contracts";
import type { ThreadStructure } from "@t3tools/client-runtime/state/shell";
import { presentPendingBackgroundWork } from "@t3tools/client-runtime/state/thread-execution";
import { commandDisplayText } from "@t3tools/client-runtime/work-log/command-label";
import { formatDuration } from "@t3tools/shared/orchestrationTimingCopy";
import { backgroundWorkHoldsCompletion } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
import { T, useGT } from "gt-react";
import { useState } from "react";
import * as DateTime from "effect/DateTime";
import { T, useGT, useMessages } from "gt-react";
import {
ActivityIcon,
ArrowRightIcon,
BotIcon,
ChevronDownIcon,
ChevronUpIcon,
CircleDashedIcon,
TerminalIcon,
} from "lucide-react";
import { useId, useState } from "react";

import { tickTextContent } from "~/lib/tickTextContent";
import { cn } from "~/lib/utils";
import { useEnvironmentProviders } from "~/state/environments";
import { InlineButton } from "../ui/button";
import {
Dialog,
DialogHeader,
DialogPanel,
DialogPopup,
DialogTitle,
DialogTrigger,
} from "../ui/dialog";
import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";
import { CommandOutputPanel, useCommandOutputStreaming } from "./CommandOutputPanel";
import { ShellCommandBlock } from "./ShellCommandBlock";
import { ThreadDetailsControl } from "./ThreadDetailsControl";
import { ThreadDetailsSection } from "./ThreadDetailsSection";
import { ThreadRelationshipIcon, threadRelationshipStatusLabel } from "./ThreadRelationshipIcon";
import { THREAD_DETAILS_PANEL_ROW_TITLE_CLASS } from "./threadDetailsPanelStyles";

const KIND_ICONS = {
subagent: BotIcon,
command: TerminalIcon,
monitor: ActivityIcon,
background_task: CircleDashedIcon,
} as const;

export function BackgroundWorkDetails(props: {
readonly environmentId: EnvironmentId;
Expand All @@ -28,93 +48,149 @@ export function BackgroundWorkDetails(props: {
readonly onOpenThread: (threadId: ThreadId) => void;
}) {
const gt = useGT();
const kindLabel = (kind: string) => {
switch (kind) {
case "subagent":
return gt("subagent", { $context: "kind of background work" });
case "command":
return gt("command", { $context: "kind of background work" });
case "monitor":
return gt("monitor", { $context: "kind of background work" });
case "background_task":
return gt("background task", { $context: "kind of background work" });
default:
return kind.replaceAll("_", " ");
}
};
const m = useMessages();
const headingId = useId();
const [open, setOpen] = useState(false);
const [expandedTaskId, setExpandedTaskId] = useState<string | null>(null);
const streamsOutput = useCommandOutputStreaming(props.environmentId);
const providers = useEnvironmentProviders().get(props.environmentId);
const presentation = presentPendingBackgroundWork(props.tasks, m);
const rows = (presentation?.items ?? []).flatMap((presented) => {
const task = props.tasks.find((candidate) => candidate.taskId === presented.taskId);
if (task === undefined) return [];
const itemKey = task.nativeItemId ?? task.taskId;
const item = props.turnItems.findLast((candidate) =>
task.itemId === undefined
? (candidate.nativeItemRef?.nativeId || candidate.id) === itemKey
: candidate.id === task.itemId,
);
const command = task.command ?? (item?.type === "command_execution" ? item.input : undefined);
const commandLabel = command ? commandDisplayText(command) : undefined;
const itemId = task.itemId ?? (item?.type === "command_execution" ? item.id : undefined);
return [
{
task,
title: task.description === undefined && commandLabel ? commandLabel : presented.label,
mono:
commandLabel !== undefined &&
(task.description === undefined || presented.label === commandLabel),
command,
outputItemId: itemId && streamsOutput && item?.status === "running" ? itemId : undefined,
outputThreadId: task.sourceThreadId ?? item?.threadId ?? props.threadId,
activity:
item?.type === "subagent" ? item.progress?.replace(/\s+/gu, " ").trim() || null : null,
startedAtMs: item?.startedAt ? DateTime.toEpochMillis(item.startedAt) : null,
provider:
task.kind === "subagent"
? providers?.find((provider) => provider.instanceId === task.providerInstanceId)
: undefined,
},
];
});
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger render={<InlineButton />}>
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger render={<InlineButton />}>
<T>Details</T>
</DialogTrigger>
<DialogPopup>
<DialogHeader>
<DialogTitle>
<T>Background work</T>
</DialogTitle>
</DialogHeader>
<DialogPanel>
<div className="space-y-5">
{props.tasks.map((task) => {
const item = props.turnItems.findLast((candidate) =>
task.itemId === undefined
? (candidate.nativeItemRef?.nativeId || candidate.id) === task.taskId
: candidate.id === task.itemId,
);
const command =
task.command ?? (item?.type === "command_execution" ? item.input : undefined);
const description = task.description?.trim();
const title =
description && description !== command?.trim()
? description
: command
? gt("Command", { $context: "title of a background shell command" })
: kindLabel(task.kind);
const itemId =
task.itemId ?? (item?.type === "command_execution" ? item.id : undefined);
</PopoverTrigger>
<PopoverPopup side="top" align="end" width="lg" padding="none" aria-labelledby={headingId}>
<ThreadDetailsSection headingId={headingId} title={gt("Background work")} separated={false}>
<ul className="m-0 list-none p-0">
{rows.map((row) => {
const { task } = row;
const childThreadId = task.kind === "subagent" ? task.childThreadId : undefined;
const expandable =
childThreadId === undefined &&
(row.command !== undefined || row.outputItemId !== undefined);
const expanded = expandable && expandedTaskId === task.taskId;
const status = backgroundWorkHoldsCompletion([task]) ? "waiting" : "running";
const startedAtMs = row.startedAtMs;
const ToggleIcon = expanded ? ChevronUpIcon : ChevronDownIcon;
return (
<section key={task.taskId} className="space-y-2 text-sm">
<div className="flex items-baseline justify-between gap-3">
<h3 className="font-medium">{title}</h3>
<span className="shrink-0 text-xs text-muted-foreground">
{backgroundWorkHoldsCompletion([task])
? gt("Waiting", { $context: "status of background work" })
: gt("Running", { $context: "status of background work" })}
<li key={task.taskId} className="group">
<ThreadDetailsControl
size="sm"
variant="ghost"
part="row"
multiline
disabled={childThreadId === undefined && !expandable}
aria-expanded={expandable ? expanded : undefined}
onClick={() => {
if (childThreadId !== undefined) {
setOpen(false);
props.onOpenThread(childThreadId);
} else {
setExpandedTaskId(expanded ? null : task.taskId);
}
}}
>
<ThreadRelationshipIcon
driver={row.provider?.driver}
provider={row.provider}
fallbackIcon={KIND_ICONS[task.kind]}
status={status}
/>
<span className="min-w-0 flex-1">
<span
className={cn(
THREAD_DETAILS_PANEL_ROW_TITLE_CLASS,
row.mono && "font-mono text-xs",
)}
>
{row.title}
</span>
{row.activity ? (
<span className="mt-1 block truncate text-2xs font-normal text-muted-foreground">
{row.activity}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
</span>
) : null}
</span>
</div>
{command ? (
<div className="rounded-md border border-border bg-muted/20 p-3 font-mono text-xs whitespace-pre-wrap break-words">
<ShellCommandBlock command={command} />
</div>
) : null}
{itemId && streamsOutput && item?.status === "running" ? (
<div className="font-mono text-xs whitespace-pre-wrap break-words">
<CommandOutputPanel
environmentId={props.environmentId}
threadId={task.sourceThreadId ?? item?.threadId ?? props.threadId}
itemId={itemId}
<span className="shrink-0 text-2xs font-normal tabular-nums text-muted-foreground">
{threadRelationshipStatusLabel(status, m)}
{startedAtMs === null ? null : (
<>
{" · "}
<span
ref={tickTextContent(
(nowMs) => formatDuration(Math.max(1_000, nowMs - startedAtMs), m),
true,
)}
/>
</>
)}
</span>
{childThreadId !== undefined ? (
<ArrowRightIcon className="size-3 shrink-0 text-muted-foreground opacity-0 transition-opacity group-hover:opacity-100" />
) : expandable ? (
<ToggleIcon
className={cn(
"size-3 shrink-0 text-muted-foreground transition-opacity group-hover:opacity-100",
!expanded && "opacity-0",
)}
/>
) : null}
</ThreadDetailsControl>
{expanded ? (
<div className="mx-2.5 mt-1 mb-2 space-y-2 font-mono text-xs whitespace-pre-wrap break-words">
{row.command ? (
<div className="max-h-32 overflow-y-auto rounded-md border border-border bg-muted/20 p-2">
<ShellCommandBlock command={row.command} />
</div>
) : null}
{row.outputItemId ? (
<CommandOutputPanel
environmentId={props.environmentId}
threadId={row.outputThreadId}
itemId={row.outputItemId}
/>
) : null}
</div>
) : null}
{childThreadId ? (
<InlineButton
onClick={() => {
setOpen(false);
props.onOpenThread(childThreadId);
}}
>
<T>Open subagent</T>
</InlineButton>
) : null}
</section>
</li>
);
})}
</div>
</DialogPanel>
</DialogPopup>
</Dialog>
</ul>
</ThreadDetailsSection>
</PopoverPopup>
</Popover>
);
}
13 changes: 4 additions & 9 deletions apps/web/src/components/chat/ThreadRelationshipsControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ import {
THREAD_DETAILS_PANEL_LABEL_CLASS,
THREAD_DETAILS_PANEL_SPLIT_GROUP_CLASS,
THREAD_DETAILS_PANEL_ROW_CONTENT_CLASS,
THREAD_DETAILS_PANEL_ROW_TITLE_CLASS,
THREAD_DETAILS_PANEL_SPLIT_SEPARATOR_CLASS,
} from "./threadDetailsPanelStyles";

Expand Down Expand Up @@ -336,9 +337,7 @@ function RemoteSubagentRow(props: {
status={status}
/>
<span className="min-w-0 flex-1">
<span className="block truncate text-left text-sm font-medium leading-4 text-foreground/85">
{title}
</span>
<span className={THREAD_DETAILS_PANEL_ROW_TITLE_CLASS}>{title}</span>
</span>
<span
className={`flex max-w-1/2 shrink-0 gap-1 text-2xs ${failed ? "text-destructive" : "text-muted-foreground"}`}
Expand Down Expand Up @@ -438,9 +437,7 @@ function DelegatedFromRow(props: {
status={null}
/>
<span className="min-w-0 flex-1">
<span className="block truncate text-left text-sm font-medium leading-4 text-foreground/85">
{title}
</span>
<span className={THREAD_DETAILS_PANEL_ROW_TITLE_CLASS}>{title}</span>
</span>
{label ? (
<span className="max-w-1/2 shrink-0 truncate text-2xs text-muted-foreground">
Expand Down Expand Up @@ -888,9 +885,7 @@ export function ThreadRelationshipsPanel(props: {
status={rowStatus}
/>
<span className="min-w-0 flex-1">
<span className="block truncate text-left text-sm font-medium leading-4 text-foreground/85">
{threadTitle}
</span>
<span className={THREAD_DETAILS_PANEL_ROW_TITLE_CLASS}>{threadTitle}</span>
</span>
{agent ? null : (
<ArrowRightIcon className="size-3 shrink-0 text-muted-foreground opacity-0 transition-opacity group-hover:opacity-100" />
Expand Down
3 changes: 3 additions & 0 deletions apps/web/src/components/chat/threadDetailsPanelStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const THREAD_DETAILS_PANEL_SPLIT_GROUP_SURFACE_CLASS = `${THREAD_DETAILS_PANEL_H

export const THREAD_DETAILS_PANEL_ROW_CONTENT_CLASS = "gap-2.5 px-2.5 text-left";

export const THREAD_DETAILS_PANEL_ROW_TITLE_CLASS =
"block truncate text-left text-sm font-medium leading-4 text-foreground/85";

export const THREAD_DETAILS_PANEL_LABEL_CLASS =
"[text-box:trim-both_cap_alphabetic] overflow-x-clip overflow-y-visible";

Expand Down
9 changes: 1 addition & 8 deletions apps/web/src/i18n/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -3309,15 +3309,8 @@
"3390f0b2396998c0": "Auswahl im Eingabefeld zitieren",
"e5dc8d61a6d178ed": "Auswahl kürzen",
"a5b06aaaba1d742f": "Zitieren",
"b1d8a5a24bd684e3": "Subagent",
"5a805b37d8940816": "Befehl",
"da2da87e78bd6b11": "Monitor",
"b034f8434dd8479d": "Hintergrundaufgabe",
"519dd506361059c9": "Befehl",
"b851b1f4c772dea4": "Wartet",
"83915f0968cde2cc": "Läuft",
"b90e87f4b7125237": "Details",
"16dc67f5429d8bae": "Hintergrundarbeit",
"668e353261860b41": "Hintergrundarbeit",
"2188eee8f81b0f51": "Subagent öffnen",
"3b03fc47eba632b4": "{count, plural, one {{formattedCount} geänderte Datei} other {{formattedCount} geänderte Dateien}}",
"ec9e0be8ec693302": "Alle Ordner einklappen",
Expand Down
Loading
Loading