feat(web): add the Agents panel
Surfaces the subagent data the previous PRs record. A new right-panel tab lists every subagent on the thread with its role, model, status, token usage, recent activity, and the runs it has been activated for. Grouping lives in client-runtime rather than the component so it stays testable: workflow coordinators own their phases, phase status derives from its members, and members orphaned by a missing coordinator fall back to the flat list rather than disappearing. A coordinator is an agent in its own right, so it renders as a card rather than a bare heading — it has a model, usage and activations, and before its members are spawned it is the only row on the thread. It is counted in the tallies for the same reason, but only while it has no members, since afterwards they represent the same work and counting both double-reports it. Without that, a thread running a workflow showed "0 active" with nothing listed and unexplained tokens in the header. Usage follows the same rule: the total keeps whatever the members did not account for. Dropping the coordinator outright erased the whole workflow whenever a provider reported workflow usage but omitted per-agent tokens, and the header read "Usage unavailable" for a workflow that had spent thousands. Read-only: this only renders what the projection already contains.
This commit is contained in:
1 parent
e151f62c2a
commit
e5cbabf0b1
8 files changed
+844
-23
No files matched your search
@@ -0,0 +1,143 @@
|
||||
import {
|
||||
NodeId,
|
||||
ProviderDriverKind,
|
||||
ProviderInstanceId,
|
||||
RunId,
|
||||
ThreadId,
|
||||
type OrchestrationV2Subagent,
|
||||
} from "@t3tools/contracts";
|
||||
import * as DateTime from "effect/DateTime";
|
||||
import { renderToStaticMarkup } from "react-dom/server";
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import { AgentsPanelV2 } from "./AgentsPanelV2";
|
||||
import { makeThreadProjectionFixture } from "../test-fixtures";
|
||||
|
||||
const now = DateTime.makeUnsafe("2026-07-26T00:00:00.000Z");
|
||||
const workflowId = NodeId.make("workflow-1");
|
||||
|
||||
const agent = (id: string, input: Partial<OrchestrationV2Subagent> = {}): OrchestrationV2Subagent =>
|
||||
({
|
||||
id: NodeId.make(id),
|
||||
threadId: ThreadId.make("thread-test"),
|
||||
runId: RunId.make("run-test"),
|
||||
parentNodeId: NodeId.make("root-node"),
|
||||
origin: "provider_native",
|
||||
createdBy: "agent",
|
||||
driver: ProviderDriverKind.make("claudeAgent"),
|
||||
providerInstanceId: ProviderInstanceId.make("claudeAgent"),
|
||||
providerThreadId: null,
|
||||
childThreadId: null,
|
||||
nativeTaskRef: null,
|
||||
prompt: "Work",
|
||||
title: id,
|
||||
model: "claude-opus-4-1",
|
||||
kind: "subagent",
|
||||
role: { name: "general-purpose", source: "provider" },
|
||||
status: "running",
|
||||
result: null,
|
||||
usage: null,
|
||||
currentActivationId: null,
|
||||
activationCount: 1,
|
||||
workflow: null,
|
||||
workflowMembership: null,
|
||||
recentActivity: [],
|
||||
startedAt: now,
|
||||
completedAt: null,
|
||||
updatedAt: now,
|
||||
...input,
|
||||
}) as OrchestrationV2Subagent;
|
||||
|
||||
const renderPanel = (subagents: ReadonlyArray<OrchestrationV2Subagent>) =>
|
||||
renderToStaticMarkup(
|
||||
<AgentsPanelV2 projection={{ ...makeThreadProjectionFixture(), subagents }} />,
|
||||
);
|
||||
|
||||
describe("AgentsPanelV2", () => {
|
||||
it("renders a workflow coordinator as a card before its members exist", () => {
|
||||
// The Claude-only workflow path. A coordinator arrives before any member
|
||||
// does, so rendering it as a bare heading left the panel showing a title,
|
||||
// no rows, and "0 active" while the workflow was running.
|
||||
const markup = renderPanel([
|
||||
agent("workflow-1", {
|
||||
kind: "workflow",
|
||||
title: "Refactor sweep",
|
||||
role: { name: "workflow-coordinator", source: "app_default" },
|
||||
status: "running",
|
||||
usage: { totalTokens: 5000 },
|
||||
workflow: { phases: [{ index: 0, title: "Research" }] },
|
||||
}),
|
||||
]);
|
||||
|
||||
expect(markup).toContain("Refactor sweep");
|
||||
expect(markup).toContain("workflow-coordinator");
|
||||
// Its own model and usage only appear if it rendered as a card.
|
||||
expect(markup).toContain("claude-opus-4-1");
|
||||
expect(markup).toContain("5k tokens");
|
||||
expect(markup).toContain("1 active");
|
||||
expect(markup).toContain("Research");
|
||||
});
|
||||
|
||||
it("renders workflow members under their phase", () => {
|
||||
const markup = renderPanel([
|
||||
agent("workflow-1", {
|
||||
kind: "workflow",
|
||||
title: "Refactor sweep",
|
||||
status: "running",
|
||||
workflow: {
|
||||
phases: [
|
||||
{ index: 0, title: "Research" },
|
||||
{ index: 1, title: "Implement" },
|
||||
],
|
||||
},
|
||||
}),
|
||||
agent("researcher", {
|
||||
kind: "workflow_agent",
|
||||
title: "researcher",
|
||||
status: "completed",
|
||||
workflowMembership: {
|
||||
workflowSubagentId: workflowId,
|
||||
agentIndex: 0,
|
||||
phaseIndex: 0,
|
||||
attempt: 1,
|
||||
},
|
||||
}),
|
||||
agent("implementer", {
|
||||
kind: "workflow_agent",
|
||||
title: "implementer",
|
||||
status: "running",
|
||||
workflowMembership: {
|
||||
workflowSubagentId: workflowId,
|
||||
agentIndex: 0,
|
||||
phaseIndex: 1,
|
||||
attempt: 1,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
expect(markup).toContain("Research");
|
||||
expect(markup).toContain("Implement");
|
||||
expect(markup).toContain("researcher");
|
||||
expect(markup).toContain("implementer");
|
||||
// Members represent the coordinator's work, so only they are tallied.
|
||||
expect(markup).toContain("1 active");
|
||||
expect(markup).toContain("1 settled");
|
||||
});
|
||||
|
||||
it("keeps a member visible when its coordinator is missing", () => {
|
||||
const markup = renderPanel([
|
||||
agent("orphan", {
|
||||
kind: "workflow_agent",
|
||||
title: "orphaned-worker",
|
||||
workflowMembership: {
|
||||
workflowSubagentId: workflowId,
|
||||
agentIndex: 0,
|
||||
phaseIndex: 0,
|
||||
attempt: 1,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
expect(markup).toContain("orphaned-worker");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,257 @@
|
||||
import {
|
||||
deriveOrchestrationV2SubagentPanelState,
|
||||
formatSubagentTokenCount,
|
||||
} from "@t3tools/client-runtime/state/orchestration-v2-subagents";
|
||||
import type {
|
||||
OrchestrationV2Subagent,
|
||||
OrchestrationV2SubagentActivation,
|
||||
OrchestrationV2ThreadProjection,
|
||||
} from "@t3tools/contracts";
|
||||
import {
|
||||
ActivityIcon,
|
||||
BotIcon,
|
||||
ChevronDownIcon,
|
||||
CircleDotIcon,
|
||||
CoinsIcon,
|
||||
GitBranchIcon,
|
||||
WorkflowIcon,
|
||||
} from "lucide-react";
|
||||
import { useMemo } from "react";
|
||||
import * as DateTime from "effect/DateTime";
|
||||
|
||||
import { cn } from "~/lib/utils";
|
||||
import { Badge } from "./ui/badge";
|
||||
import { ScrollArea } from "./ui/scroll-area";
|
||||
|
||||
const statusTone = (status: OrchestrationV2Subagent["status"]) =>
|
||||
status === "failed"
|
||||
? "bg-destructive"
|
||||
: status === "running"
|
||||
? "bg-emerald-500"
|
||||
: status === "pending" || status === "waiting"
|
||||
? "bg-amber-500"
|
||||
: status === "idle"
|
||||
? "bg-sky-500"
|
||||
: "bg-muted-foreground/50";
|
||||
|
||||
const usageSummary = (agent: OrchestrationV2Subagent) => {
|
||||
if (agent.usage === null) return null;
|
||||
return [
|
||||
`${formatSubagentTokenCount(agent.usage.totalTokens)} tokens`,
|
||||
agent.usage.toolUses === undefined ? null : `${agent.usage.toolUses} tools`,
|
||||
]
|
||||
.filter((value): value is string => value !== null)
|
||||
.join(" · ");
|
||||
};
|
||||
|
||||
const activationLabel = (activation: OrchestrationV2SubagentActivation) => {
|
||||
const usage =
|
||||
activation.usage === null
|
||||
? null
|
||||
: `${formatSubagentTokenCount(activation.usage.totalTokens)} tokens`;
|
||||
return [`Run ${activation.ordinal}`, activation.status, usage]
|
||||
.filter((value): value is string => value !== null)
|
||||
.join(" · ");
|
||||
};
|
||||
|
||||
const keyedActivities = (agent: OrchestrationV2Subagent) => {
|
||||
const occurrences = new Map<string, number>();
|
||||
return agent.recentActivity.map((activity) => {
|
||||
const base = `${DateTime.formatIso(activity.at)}:${activity.summary}`;
|
||||
const occurrence = (occurrences.get(base) ?? 0) + 1;
|
||||
occurrences.set(base, occurrence);
|
||||
return { activity, key: `${base}:${occurrence}` };
|
||||
});
|
||||
};
|
||||
|
||||
function AgentCard(props: {
|
||||
agent: OrchestrationV2Subagent;
|
||||
activations: ReadonlyArray<OrchestrationV2SubagentActivation>;
|
||||
}) {
|
||||
const { agent } = props;
|
||||
const detail = agent.progress?.trim() || agent.result?.trim() || agent.prompt.trim();
|
||||
const usage = usageSummary(agent);
|
||||
const activities = keyedActivities(agent);
|
||||
return (
|
||||
<article
|
||||
className="rounded-lg border border-border/70 bg-card/60 p-3"
|
||||
data-orchestration-v2-agent={agent.id}
|
||||
data-agent-status={agent.status}
|
||||
>
|
||||
<div className="flex min-w-0 items-start gap-2">
|
||||
<BotIcon className="mt-0.5 size-4 shrink-0 text-muted-foreground" />
|
||||
<div className="min-w-0 flex-1">
|
||||
<div className="flex min-w-0 items-center gap-2">
|
||||
<span className={cn("size-2 shrink-0 rounded-full", statusTone(agent.status))} />
|
||||
<h4 className="truncate text-sm font-medium">
|
||||
{agent.title?.trim() || agent.role.name}
|
||||
</h4>
|
||||
<span className="ml-auto shrink-0 font-mono text-[10px] text-muted-foreground">
|
||||
{agent.status}
|
||||
</span>
|
||||
</div>
|
||||
<div className="mt-1.5 flex flex-wrap items-center gap-1.5">
|
||||
<Badge variant="secondary" size="sm">
|
||||
{agent.role.name}
|
||||
</Badge>
|
||||
{agent.role.source === "app_default" ? (
|
||||
<Badge variant="outline" size="sm">
|
||||
default role
|
||||
</Badge>
|
||||
) : null}
|
||||
{agent.model === null ? null : (
|
||||
<Badge variant="outline" size="sm">
|
||||
{agent.model}
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
{detail.length > 0 ? (
|
||||
<p className="mt-2 line-clamp-2 text-xs leading-relaxed text-muted-foreground">
|
||||
{detail}
|
||||
</p>
|
||||
) : null}
|
||||
<div className="mt-2 flex flex-wrap gap-x-3 gap-y-1 font-mono text-[10px] text-muted-foreground">
|
||||
{usage === null ? null : (
|
||||
<span className="flex items-center gap-1">
|
||||
<CoinsIcon className="size-3" />
|
||||
{usage}
|
||||
</span>
|
||||
)}
|
||||
<span className="flex items-center gap-1">
|
||||
<GitBranchIcon className="size-3" />
|
||||
{agent.activationCount} {agent.activationCount === 1 ? "run" : "runs"}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{agent.recentActivity.length > 0 || props.activations.length > 0 ? (
|
||||
<details className="group mt-3 border-t border-border/60 pt-2">
|
||||
<summary className="flex cursor-pointer list-none items-center gap-1 text-[11px] text-muted-foreground [&::-webkit-details-marker]:hidden">
|
||||
<ChevronDownIcon className="size-3 transition-transform group-open:rotate-180" />
|
||||
Activity and runs
|
||||
</summary>
|
||||
<div className="mt-2 space-y-2 pl-1">
|
||||
{activities.map(({ activity, key }) => (
|
||||
<div key={key} className="flex items-start gap-2 text-xs">
|
||||
<ActivityIcon className="mt-0.5 size-3 shrink-0 text-muted-foreground" />
|
||||
<span className="break-words">{activity.summary}</span>
|
||||
</div>
|
||||
))}
|
||||
{props.activations.map((activation) => (
|
||||
<div key={activation.id} className="flex items-start gap-2 text-xs">
|
||||
<CircleDotIcon className="mt-0.5 size-3 shrink-0 text-muted-foreground" />
|
||||
<span>{activationLabel(activation)}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</details>
|
||||
) : null}
|
||||
</article>
|
||||
);
|
||||
}
|
||||
|
||||
export function AgentsPanelV2(props: { projection: OrchestrationV2ThreadProjection | null }) {
|
||||
const subagents = props.projection?.subagents ?? null;
|
||||
const activations = props.projection?.subagentActivations ?? null;
|
||||
const panel = useMemo(
|
||||
() =>
|
||||
subagents === null || activations === null
|
||||
? null
|
||||
: deriveOrchestrationV2SubagentPanelState({
|
||||
subagents,
|
||||
activations,
|
||||
}),
|
||||
[activations, subagents],
|
||||
);
|
||||
|
||||
if (panel === null || panel.groups.length === 0) {
|
||||
return (
|
||||
<div
|
||||
className="flex min-h-0 flex-1 items-center justify-center p-6 text-center"
|
||||
data-orchestration-v2-agents-empty
|
||||
>
|
||||
<div className="max-w-xs">
|
||||
<BotIcon className="mx-auto size-7 text-muted-foreground" />
|
||||
<h3 className="mt-3 text-sm font-medium">No agents yet</h3>
|
||||
<p className="mt-1 text-xs leading-relaxed text-muted-foreground">
|
||||
Provider-native and delegated agents will appear here with their usage, roles, runs, and
|
||||
recent activity.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex min-h-0 flex-1 flex-col" data-orchestration-v2-agents-panel>
|
||||
<header className="border-b border-border/70 px-4 py-3">
|
||||
<div className="flex items-center gap-2">
|
||||
<BotIcon className="size-4" />
|
||||
<h2 className="text-sm font-medium">Agents</h2>
|
||||
<span className="ml-auto font-mono text-xs text-muted-foreground">
|
||||
{panel.totalTokens === null
|
||||
? "Usage unavailable"
|
||||
: `${formatSubagentTokenCount(panel.totalTokens)} tokens`}
|
||||
</span>
|
||||
</div>
|
||||
<div className="mt-2 flex flex-wrap gap-3 font-mono text-[10px] text-muted-foreground">
|
||||
<span>{panel.activeCount} active</span>
|
||||
<span>{panel.waitingCount} waiting</span>
|
||||
<span>{panel.settledCount} settled</span>
|
||||
</div>
|
||||
</header>
|
||||
<ScrollArea className="min-h-0 flex-1">
|
||||
<div className="space-y-4 p-3">
|
||||
{panel.groups.map((group, groupIndex) => (
|
||||
<section
|
||||
key={group.workflow?.id ?? `direct:${groupIndex}`}
|
||||
className="space-y-2"
|
||||
data-agent-group={group.workflow?.id ?? "direct"}
|
||||
>
|
||||
{group.workflow === null ? null : (
|
||||
<>
|
||||
<div className="flex items-center gap-2 px-1">
|
||||
<WorkflowIcon className="size-4 text-muted-foreground" />
|
||||
<h3 className="truncate text-xs font-medium">
|
||||
{group.workflow.title?.trim() || "Workflow"}
|
||||
</h3>
|
||||
</div>
|
||||
{/* The coordinator is an agent too: it has its own model,
|
||||
usage and activations, and is the only row present at all
|
||||
before its members are spawned. */}
|
||||
<AgentCard
|
||||
agent={group.workflow}
|
||||
activations={panel.activationsBySubagentId.get(group.workflow.id) ?? []}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
{group.phases.map((phase) => (
|
||||
<div key={phase.index} className="space-y-2">
|
||||
<div className="flex items-center gap-2 px-1 text-[11px] text-muted-foreground">
|
||||
<span className="font-mono">{phase.index + 1}</span>
|
||||
<span className="truncate">{phase.title}</span>
|
||||
<span className="ml-auto font-mono">{phase.status}</span>
|
||||
</div>
|
||||
{phase.agents.map((agent) => (
|
||||
<AgentCard
|
||||
key={agent.id}
|
||||
agent={agent}
|
||||
activations={panel.activationsBySubagentId.get(agent.id) ?? []}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
))}
|
||||
{group.agents.map((agent) => (
|
||||
<AgentCard
|
||||
key={agent.id}
|
||||
agent={agent}
|
||||
activations={panel.activationsBySubagentId.get(agent.id) ?? []}
|
||||
/>
|
||||
))}
|
||||
</section>
|
||||
))}
|
||||
</div>
|
||||
</ScrollArea>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -422,6 +422,9 @@ const PreviewPanel = lazy(() =>
|
||||
import("./preview/PreviewPanel").then((module) => ({ default: module.PreviewPanel })),
|
||||
);
|
||||
const DiffPanel = lazy(() => import("./DiffPanel"));
|
||||
const AgentsPanelV2 = lazy(() =>
|
||||
import("./AgentsPanelV2").then((module) => ({ default: module.AgentsPanelV2 })),
|
||||
);
|
||||
const FilePreviewPanel = lazy(() => import("./files/FilePreviewPanel"));
|
||||
const EMPTY_PENDING_FILE_SURFACE_IDS: ReadonlySet<string> = new Set();
|
||||
const TYPE_TO_FOCUS_EDITABLE_SELECTOR = [
|
||||
@@ -3284,6 +3287,10 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
onDiffPanelOpen,
|
||||
planSidebarOpen,
|
||||
]);
|
||||
const addAgentsSurface = useCallback(() => {
|
||||
if (!activeThreadRef) return;
|
||||
useRightPanelStore.getState().open(activeThreadRef, "agents");
|
||||
}, [activeThreadRef]);
|
||||
const openChangesFromThreadPanel = useCallback(() => {
|
||||
addDiffSurface();
|
||||
}, [addDiffSurface]);
|
||||
@@ -6047,6 +6054,10 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
initialGitScope={initialDiffPanelGitScope}
|
||||
/>
|
||||
</Suspense>
|
||||
) : activeRightPanelSurface?.kind === "agents" ? (
|
||||
<Suspense fallback={null}>
|
||||
<AgentsPanelV2 projection={serverProjection} />
|
||||
</Suspense>
|
||||
) : activeRightPanelSurface?.kind === "plan" ? (
|
||||
<PlanSidebar
|
||||
activePlan={activePlan}
|
||||
@@ -6166,33 +6177,26 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
showThreadPanelControl={!inlineRightPanelOwnsTitleBar}
|
||||
/>
|
||||
);
|
||||
const threadPanelHeaderControl = (
|
||||
<div className="workspace-titlebar-controls z-50 [-webkit-app-region:no-drag]">
|
||||
<PanelLayoutControls
|
||||
{...panelToggleControlProps}
|
||||
showTerminalControl={false}
|
||||
showRightPanelControl={false}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
const panelLayoutControls = (
|
||||
<div className="workspace-titlebar-controls z-50 gap-1 [-webkit-app-region:no-drag]">
|
||||
{rightPanelOpen && !shouldUsePlanSidebarSheet ? (
|
||||
<RightPanelMaximizeControl
|
||||
maximized={rightPanelMaximized}
|
||||
onToggle={toggleRightPanelMaximized}
|
||||
/>
|
||||
) : null}
|
||||
{panelToggleControls}
|
||||
</div>
|
||||
);
|
||||
const rightPanelLayoutControls = (
|
||||
<div className="flex h-full shrink-0 items-center gap-1 [-webkit-app-region:no-drag]">
|
||||
<RightPanelMaximizeControl
|
||||
maximized={rightPanelMaximized}
|
||||
onToggle={toggleRightPanelMaximized}
|
||||
/>
|
||||
<PanelLayoutControls {...panelToggleControlProps} />
|
||||
</div>
|
||||
);
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={workspaceLayoutRef}
|
||||
className="relative flex min-h-0 min-w-0 flex-1 overflow-hidden bg-background"
|
||||
>
|
||||
{rightPanelOpen && !shouldUsePlanSidebarSheet ? panelLayoutControls : null}
|
||||
<div
|
||||
className={cn(
|
||||
"flex min-h-0 min-w-0 flex-col overflow-x-hidden",
|
||||
@@ -6217,11 +6221,7 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
COLLAPSED_SIDEBAR_TITLEBAR_INSET_CLASS,
|
||||
)}
|
||||
>
|
||||
{inlineRightPanelOwnsTitleBar
|
||||
? threadPanelHeaderControl
|
||||
: !rightPanelOpen
|
||||
? panelLayoutControls
|
||||
: null}
|
||||
{!inlineRightPanelOwnsTitleBar && !rightPanelOpen ? panelLayoutControls : null}
|
||||
<ChatHeader
|
||||
activeThreadEnvironmentId={activeThread.environmentId}
|
||||
activeThreadTitle={activeThread.title}
|
||||
@@ -6585,6 +6585,7 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
mode="inline"
|
||||
maximized={rightPanelMaximized}
|
||||
inlineSize={previewPanelInlineSize}
|
||||
layoutControls={rightPanelLayoutControls}
|
||||
surfaces={rightPanelState.surfaces}
|
||||
activeSurfaceId={activeRightPanelSurface?.id ?? null}
|
||||
pendingSurfaceIds={pendingFileSurfaceIds}
|
||||
@@ -6597,6 +6598,7 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
onCloseAllSurfaces={closeAllRightPanelSurfaces}
|
||||
onCopyFilePath={copyRightPanelFilePath}
|
||||
onAddBrowser={createBrowserSurface}
|
||||
onAddAgents={addAgentsSurface}
|
||||
onAddTerminal={addTerminalSurface}
|
||||
onAddDiff={addDiffSurface}
|
||||
onAddFiles={addFilesSurface}
|
||||
@@ -6625,6 +6627,7 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
onCloseAllSurfaces={closeAllRightPanelSurfaces}
|
||||
onCopyFilePath={copyRightPanelFilePath}
|
||||
onAddBrowser={createBrowserSurface}
|
||||
onAddAgents={addAgentsSurface}
|
||||
onAddTerminal={addTerminalSurface}
|
||||
onAddDiff={addDiffSurface}
|
||||
onAddFiles={addFilesSurface}
|
||||
|
||||
@@ -1,6 +1,15 @@
|
||||
import type { ContextMenuItem, PreviewSessionSnapshot } from "@t3tools/contracts";
|
||||
import { getTerminalLabel } from "@t3tools/shared/terminalLabels";
|
||||
import { ClipboardList, FileDiff, Files, Globe2, Plus, TerminalSquare, X } from "lucide-react";
|
||||
import {
|
||||
BotIcon,
|
||||
ClipboardList,
|
||||
FileDiff,
|
||||
Files,
|
||||
Globe2,
|
||||
Plus,
|
||||
TerminalSquare,
|
||||
X,
|
||||
} from "lucide-react";
|
||||
import {
|
||||
type MouseEvent as ReactMouseEvent,
|
||||
type ReactElement,
|
||||
@@ -43,6 +52,7 @@ interface RightPanelTabsProps {
|
||||
onCloseAllSurfaces: () => void;
|
||||
onCopyFilePath: (relativePath: string) => void;
|
||||
onAddBrowser: () => void;
|
||||
onAddAgents: () => void;
|
||||
onAddTerminal: () => void;
|
||||
onAddDiff: () => void;
|
||||
onAddFiles: () => void;
|
||||
@@ -90,6 +100,7 @@ function SurfaceMenuItem(props: {
|
||||
|
||||
function RightPanelEmptyState(props: {
|
||||
onAddBrowser: () => void;
|
||||
onAddAgents: () => void;
|
||||
onAddTerminal: () => void;
|
||||
onAddDiff: () => void;
|
||||
onAddFiles: () => void;
|
||||
@@ -98,6 +109,14 @@ function RightPanelEmptyState(props: {
|
||||
filesAvailable: boolean;
|
||||
}) {
|
||||
const actions = [
|
||||
{
|
||||
label: "Agents",
|
||||
description: "Inspect agent roles, runs, usage, and activity.",
|
||||
icon: BotIcon,
|
||||
available: true,
|
||||
disabledReason: null,
|
||||
onClick: props.onAddAgents,
|
||||
},
|
||||
{
|
||||
label: "Browser",
|
||||
description: "Open a local app or URL.",
|
||||
@@ -194,6 +213,8 @@ function surfaceTitle(
|
||||
terminalLabelsById: ReadonlyMap<string, string>,
|
||||
): string {
|
||||
switch (surface.kind) {
|
||||
case "agents":
|
||||
return "Agents";
|
||||
case "diff":
|
||||
return "Diff";
|
||||
case "files":
|
||||
@@ -246,6 +267,8 @@ function SurfaceIcon({
|
||||
theme: "light" | "dark";
|
||||
}) {
|
||||
switch (surface.kind) {
|
||||
case "agents":
|
||||
return <BotIcon className="size-3.5 shrink-0" />;
|
||||
case "preview": {
|
||||
const snapshot = surface.resourceId ? sessions[surface.resourceId] : null;
|
||||
const url = !snapshot || snapshot.navStatus._tag === "Idle" ? null : snapshot.navStatus.url;
|
||||
@@ -446,6 +469,10 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
|
||||
<Plus className="size-4" />
|
||||
</MenuTrigger>
|
||||
<MenuPopup align="start" side="bottom" sideOffset={6} className="min-w-44">
|
||||
<SurfaceMenuItem available onClick={props.onAddAgents}>
|
||||
<BotIcon />
|
||||
Agents
|
||||
</SurfaceMenuItem>
|
||||
<SurfaceMenuItem
|
||||
available={props.browserAvailable}
|
||||
disabledReason={SURFACE_DISABLED_REASONS.browser}
|
||||
@@ -485,6 +512,7 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
|
||||
{props.activeSurfaceId === null ? (
|
||||
<RightPanelEmptyState
|
||||
onAddBrowser={props.onAddBrowser}
|
||||
onAddAgents={props.onAddAgents}
|
||||
onAddTerminal={props.onAddTerminal}
|
||||
onAddDiff={props.onAddDiff}
|
||||
onAddFiles={props.onAddFiles}
|
||||
|
||||
@@ -15,7 +15,15 @@ import { createJSONStorage, persist } from "zustand/middleware";
|
||||
import { resolveStorage } from "./lib/storage";
|
||||
import type { ThreadPanelPresentation } from "./rightPanelLayout";
|
||||
|
||||
export const RIGHT_PANEL_KINDS = ["plan", "diff", "files", "file", "preview", "terminal"] as const;
|
||||
export const RIGHT_PANEL_KINDS = [
|
||||
"plan",
|
||||
"agents",
|
||||
"diff",
|
||||
"files",
|
||||
"file",
|
||||
"preview",
|
||||
"terminal",
|
||||
] as const;
|
||||
export type RightPanelKind = (typeof RIGHT_PANEL_KINDS)[number];
|
||||
|
||||
export type RightPanelSurface =
|
||||
@@ -30,6 +38,7 @@ export type RightPanelSurface =
|
||||
splitDirection?: "horizontal" | "vertical";
|
||||
}
|
||||
| { id: "diff"; kind: "diff" }
|
||||
| { id: "agents"; kind: "agents" }
|
||||
| { id: "files"; kind: "files" }
|
||||
| {
|
||||
id: `file:${string}`;
|
||||
@@ -104,6 +113,8 @@ const singletonSurface = (
|
||||
kind: Exclude<RightPanelKind, "file" | "preview" | "terminal">,
|
||||
): RightPanelSurface => {
|
||||
switch (kind) {
|
||||
case "agents":
|
||||
return { id: "agents", kind };
|
||||
case "diff":
|
||||
return { id: "diff", kind };
|
||||
case "files":
|
||||
|
||||
@@ -79,6 +79,10 @@
|
||||
"types": "./src/state/orchestrationV2Projection.ts",
|
||||
"default": "./src/state/orchestrationV2Projection.ts"
|
||||
},
|
||||
"./state/orchestration-v2-subagents": {
|
||||
"types": "./src/state/orchestrationV2Subagents.ts",
|
||||
"default": "./src/state/orchestrationV2Subagents.ts"
|
||||
},
|
||||
"./state/presentation": {
|
||||
"types": "./src/state/presentation.ts",
|
||||
"default": "./src/state/presentation.ts"
|
||||
|
||||
@@ -0,0 +1,228 @@
|
||||
import {
|
||||
NodeId,
|
||||
ProviderDriverKind,
|
||||
ProviderInstanceId,
|
||||
RunId,
|
||||
SubagentActivationId,
|
||||
ThreadId,
|
||||
type OrchestrationV2Subagent,
|
||||
type OrchestrationV2SubagentActivation,
|
||||
} from "@t3tools/contracts";
|
||||
import * as DateTime from "effect/DateTime";
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import {
|
||||
deriveOrchestrationV2SubagentPanelState,
|
||||
formatSubagentTokenCount,
|
||||
} from "./orchestrationV2Subagents.ts";
|
||||
|
||||
const now = DateTime.makeUnsafe("2026-07-26T00:00:00.000Z");
|
||||
const threadId = ThreadId.make("thread-agents");
|
||||
const runId = RunId.make("run-agents");
|
||||
const workflowId = NodeId.make("workflow-1");
|
||||
|
||||
const agent = (
|
||||
id: string,
|
||||
input: Partial<OrchestrationV2Subagent> = {},
|
||||
): OrchestrationV2Subagent => ({
|
||||
id: NodeId.make(id),
|
||||
threadId,
|
||||
runId,
|
||||
parentNodeId: NodeId.make("root-node"),
|
||||
origin: "provider_native",
|
||||
createdBy: "agent",
|
||||
driver: ProviderDriverKind.make("claudeAgent"),
|
||||
providerInstanceId: ProviderInstanceId.make("claudeAgent"),
|
||||
providerThreadId: null,
|
||||
childThreadId: null,
|
||||
nativeTaskRef: null,
|
||||
prompt: "Work",
|
||||
title: id,
|
||||
model: "claude-opus-4-1",
|
||||
kind: "subagent",
|
||||
role: { name: "general-purpose", source: "provider" },
|
||||
status: "running",
|
||||
result: null,
|
||||
usage: null,
|
||||
currentActivationId: null,
|
||||
activationCount: 1,
|
||||
workflow: null,
|
||||
workflowMembership: null,
|
||||
recentActivity: [],
|
||||
startedAt: now,
|
||||
completedAt: null,
|
||||
updatedAt: now,
|
||||
...input,
|
||||
});
|
||||
|
||||
const activation = (
|
||||
id: string,
|
||||
subagentId: OrchestrationV2Subagent["id"],
|
||||
ordinal: number,
|
||||
): OrchestrationV2SubagentActivation => ({
|
||||
id: SubagentActivationId.make(id),
|
||||
threadId,
|
||||
subagentId,
|
||||
runId,
|
||||
providerTurnId: null,
|
||||
ordinal,
|
||||
status: "completed",
|
||||
usage: { totalTokens: ordinal * 100 },
|
||||
startedAt: now,
|
||||
completedAt: now,
|
||||
updatedAt: now,
|
||||
});
|
||||
|
||||
describe("deriveOrchestrationV2SubagentPanelState", () => {
|
||||
it("groups workflow agents by phase without double-counting workflow usage", () => {
|
||||
const workflow = agent("workflow-1", {
|
||||
kind: "workflow",
|
||||
role: { name: "workflow-coordinator", source: "app_default" },
|
||||
status: "completed",
|
||||
usage: { totalTokens: 900 },
|
||||
workflow: {
|
||||
phases: [
|
||||
{ index: 0, title: "Research" },
|
||||
{ index: 1, title: "Implement" },
|
||||
],
|
||||
},
|
||||
});
|
||||
const researcher = agent("researcher", {
|
||||
kind: "workflow_agent",
|
||||
usage: { totalTokens: 400 },
|
||||
workflowMembership: {
|
||||
workflowSubagentId: workflowId,
|
||||
agentIndex: 1,
|
||||
phaseIndex: 0,
|
||||
attempt: 1,
|
||||
},
|
||||
});
|
||||
const auditor = agent("auditor", {
|
||||
kind: "workflow_agent",
|
||||
status: "completed",
|
||||
workflowMembership: {
|
||||
workflowSubagentId: workflowId,
|
||||
agentIndex: 0,
|
||||
phaseIndex: 0,
|
||||
attempt: 1,
|
||||
},
|
||||
});
|
||||
const implementer = agent("implementer", {
|
||||
kind: "workflow_agent",
|
||||
status: "idle",
|
||||
usage: { totalTokens: 500 },
|
||||
workflowMembership: {
|
||||
workflowSubagentId: workflowId,
|
||||
agentIndex: 1,
|
||||
phaseIndex: 1,
|
||||
attempt: 1,
|
||||
},
|
||||
});
|
||||
const activations = [
|
||||
activation("activation-researcher-1", researcher.id, 1),
|
||||
activation("activation-researcher-2", researcher.id, 2),
|
||||
];
|
||||
|
||||
const result = deriveOrchestrationV2SubagentPanelState({
|
||||
subagents: [workflow, researcher, auditor, implementer],
|
||||
activations,
|
||||
});
|
||||
|
||||
expect(result.groups).toHaveLength(1);
|
||||
expect(result.groups[0]?.phases.map((phase) => phase.title)).toEqual(["Research", "Implement"]);
|
||||
expect(result.groups[0]?.phases[0]?.agents).toEqual([auditor, researcher]);
|
||||
expect(result.activeCount).toBe(1);
|
||||
expect(result.settledCount).toBe(2);
|
||||
expect(result.totalTokens).toBe(900);
|
||||
expect(result.activationsBySubagentId.get(researcher.id)).toEqual(activations);
|
||||
});
|
||||
|
||||
it("distinguishes unavailable usage from a reported zero", () => {
|
||||
const unavailable = deriveOrchestrationV2SubagentPanelState({
|
||||
subagents: [agent("unreported")],
|
||||
activations: [],
|
||||
});
|
||||
const reportedZero = deriveOrchestrationV2SubagentPanelState({
|
||||
subagents: [agent("reported-zero", { usage: { totalTokens: 0 } })],
|
||||
activations: [],
|
||||
});
|
||||
|
||||
expect(unavailable.totalTokens).toBeNull();
|
||||
expect(reportedZero.totalTokens).toBe(0);
|
||||
});
|
||||
|
||||
it("keeps workflow usage the members did not account for", () => {
|
||||
// A provider can report the workflow's own usage while omitting per-agent
|
||||
// tokens. Excluding the coordinator outright then erased the entire
|
||||
// workflow from the total and the header read "Usage unavailable".
|
||||
const workflow = agent("workflow-1", {
|
||||
kind: "workflow",
|
||||
status: "completed",
|
||||
usage: { totalTokens: 5000 },
|
||||
workflow: { phases: [] },
|
||||
});
|
||||
const silentMember = agent("silent-member", {
|
||||
kind: "workflow_agent",
|
||||
status: "completed",
|
||||
workflowMembership: {
|
||||
workflowSubagentId: workflowId,
|
||||
agentIndex: 0,
|
||||
phaseIndex: null,
|
||||
attempt: 1,
|
||||
},
|
||||
});
|
||||
const partialMember = agent("partial-member", {
|
||||
kind: "workflow_agent",
|
||||
status: "completed",
|
||||
usage: { totalTokens: 200 },
|
||||
workflowMembership: {
|
||||
workflowSubagentId: workflowId,
|
||||
agentIndex: 1,
|
||||
phaseIndex: null,
|
||||
attempt: 1,
|
||||
},
|
||||
});
|
||||
|
||||
expect(
|
||||
deriveOrchestrationV2SubagentPanelState({
|
||||
subagents: [workflow, silentMember],
|
||||
activations: [],
|
||||
}).totalTokens,
|
||||
).toBe(5000);
|
||||
expect(
|
||||
deriveOrchestrationV2SubagentPanelState({
|
||||
subagents: [workflow, silentMember, partialMember],
|
||||
activations: [],
|
||||
}).totalTokens,
|
||||
).toBe(5000);
|
||||
});
|
||||
|
||||
it("counts a workflow that has no members yet", () => {
|
||||
// Before its members are spawned the coordinator is the only row on the
|
||||
// thread. Excluding it reported "0 active" while a workflow was visibly
|
||||
// running, with nothing listed to explain the tokens in the header.
|
||||
const running = deriveOrchestrationV2SubagentPanelState({
|
||||
subagents: [
|
||||
agent("workflow-1", {
|
||||
kind: "workflow",
|
||||
status: "running",
|
||||
usage: { totalTokens: 5000 },
|
||||
workflow: { phases: [{ index: 0, title: "Research" }] },
|
||||
}),
|
||||
],
|
||||
activations: [],
|
||||
});
|
||||
|
||||
expect(running.activeCount).toBe(1);
|
||||
expect(running.settledCount).toBe(0);
|
||||
expect(running.totalTokens).toBe(5000);
|
||||
expect(running.groups[0]?.workflow?.id).toBe(workflowId);
|
||||
});
|
||||
|
||||
it("formats compact token counts", () => {
|
||||
expect(formatSubagentTokenCount(999)).toBe("999");
|
||||
expect(formatSubagentTokenCount(1_200)).toBe("1.2k");
|
||||
expect(formatSubagentTokenCount(999_999)).toBe("1M");
|
||||
expect(formatSubagentTokenCount(2_000_000)).toBe("2M");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,147 @@
|
||||
import type {
|
||||
OrchestrationV2Subagent,
|
||||
OrchestrationV2SubagentActivation,
|
||||
} from "@t3tools/contracts";
|
||||
|
||||
export interface OrchestrationV2SubagentPhaseGroup {
|
||||
readonly index: number;
|
||||
readonly title: string;
|
||||
readonly status: "pending" | "running" | "done";
|
||||
readonly agents: ReadonlyArray<OrchestrationV2Subagent>;
|
||||
}
|
||||
|
||||
export interface OrchestrationV2SubagentGroup {
|
||||
readonly workflow: OrchestrationV2Subagent | null;
|
||||
readonly phases: ReadonlyArray<OrchestrationV2SubagentPhaseGroup>;
|
||||
readonly agents: ReadonlyArray<OrchestrationV2Subagent>;
|
||||
}
|
||||
|
||||
export interface OrchestrationV2SubagentPanelState {
|
||||
readonly groups: ReadonlyArray<OrchestrationV2SubagentGroup>;
|
||||
readonly activationsBySubagentId: ReadonlyMap<
|
||||
string,
|
||||
ReadonlyArray<OrchestrationV2SubagentActivation>
|
||||
>;
|
||||
readonly activeCount: number;
|
||||
readonly waitingCount: number;
|
||||
readonly settledCount: number;
|
||||
readonly totalTokens: number | null;
|
||||
}
|
||||
|
||||
export const isSettledOrchestrationV2Subagent = (agent: OrchestrationV2Subagent) =>
|
||||
agent.status === "idle" ||
|
||||
agent.status === "completed" ||
|
||||
agent.status === "failed" ||
|
||||
agent.status === "cancelled" ||
|
||||
agent.status === "interrupted";
|
||||
|
||||
const phaseStatus = (agents: ReadonlyArray<OrchestrationV2Subagent>) =>
|
||||
agents.length === 0
|
||||
? ("pending" as const)
|
||||
: agents.every(isSettledOrchestrationV2Subagent)
|
||||
? ("done" as const)
|
||||
: ("running" as const);
|
||||
|
||||
export function deriveOrchestrationV2SubagentPanelState(input: {
|
||||
readonly subagents: ReadonlyArray<OrchestrationV2Subagent>;
|
||||
readonly activations: ReadonlyArray<OrchestrationV2SubagentActivation>;
|
||||
}): OrchestrationV2SubagentPanelState {
|
||||
const activationsBySubagentId = new Map<string, OrchestrationV2SubagentActivation[]>();
|
||||
for (const activation of input.activations) {
|
||||
const current = activationsBySubagentId.get(activation.subagentId) ?? [];
|
||||
current.push(activation);
|
||||
activationsBySubagentId.set(activation.subagentId, current);
|
||||
}
|
||||
for (const activations of activationsBySubagentId.values()) {
|
||||
activations.sort((left, right) => left.ordinal - right.ordinal);
|
||||
}
|
||||
|
||||
const workflows = input.subagents.filter((agent) => agent.kind === "workflow");
|
||||
const membersByWorkflow = new Map<string, OrchestrationV2Subagent[]>();
|
||||
const direct: OrchestrationV2Subagent[] = [];
|
||||
for (const agent of input.subagents) {
|
||||
if (agent.kind === "workflow") continue;
|
||||
const workflowId = agent.workflowMembership?.workflowSubagentId;
|
||||
if (workflowId === undefined) {
|
||||
direct.push(agent);
|
||||
continue;
|
||||
}
|
||||
const members = membersByWorkflow.get(workflowId) ?? [];
|
||||
members.push(agent);
|
||||
membersByWorkflow.set(workflowId, members);
|
||||
}
|
||||
|
||||
const groups: OrchestrationV2SubagentGroup[] = workflows.map((workflow) => {
|
||||
const members = membersByWorkflow.get(workflow.id) ?? [];
|
||||
membersByWorkflow.delete(workflow.id);
|
||||
const phases = (workflow.workflow?.phases ?? []).map((phase) => {
|
||||
const agents = members
|
||||
.filter((agent) => agent.workflowMembership?.phaseIndex === phase.index)
|
||||
.toSorted(
|
||||
(left, right) =>
|
||||
(left.workflowMembership?.agentIndex ?? 0) -
|
||||
(right.workflowMembership?.agentIndex ?? 0),
|
||||
);
|
||||
return { ...phase, status: phaseStatus(agents), agents };
|
||||
});
|
||||
const phasedIds = new Set(phases.flatMap((phase) => phase.agents.map((agent) => agent.id)));
|
||||
return {
|
||||
workflow,
|
||||
phases,
|
||||
agents: members.filter((agent) => !phasedIds.has(agent.id)),
|
||||
};
|
||||
});
|
||||
for (const orphaned of membersByWorkflow.values()) direct.push(...orphaned);
|
||||
if (direct.length > 0) groups.push({ workflow: null, phases: [], agents: direct });
|
||||
|
||||
const workers = input.subagents.filter((agent) => agent.kind !== "workflow");
|
||||
// A coordinator's usage already includes its members', so counting both
|
||||
// double-reports the thread. Only drop it when the members actually account
|
||||
// for it: a provider that reports workflow usage but omits per-agent tokens
|
||||
// would otherwise erase the whole workflow from the total.
|
||||
const memberTotalsByWorkflow = new Map<string, number>();
|
||||
for (const agent of workers) {
|
||||
const workflowId = agent.workflowMembership?.workflowSubagentId;
|
||||
if (workflowId === undefined) continue;
|
||||
memberTotalsByWorkflow.set(
|
||||
workflowId,
|
||||
(memberTotalsByWorkflow.get(workflowId) ?? 0) + (agent.usage?.totalTokens ?? 0),
|
||||
);
|
||||
}
|
||||
const reportedUsage = input.subagents.flatMap((agent) => {
|
||||
if (agent.usage === null) return [];
|
||||
if (agent.kind !== "workflow") return [agent.usage.totalTokens];
|
||||
const memberTotal = memberTotalsByWorkflow.get(agent.id);
|
||||
if (memberTotal === undefined) return [agent.usage.totalTokens];
|
||||
// Keep whatever the members did not account for. Dropping the coordinator
|
||||
// outright erases the whole workflow whenever a provider reports its usage
|
||||
// but omits per-agent tokens.
|
||||
return memberTotal >= agent.usage.totalTokens ? [] : [agent.usage.totalTokens - memberTotal];
|
||||
});
|
||||
// Count the workers, plus any coordinator that has no members yet. A
|
||||
// coordinator with members is represented by them and would double-count;
|
||||
// one without is the only row on the thread, and omitting it reported
|
||||
// "0 active" while a workflow was visibly running.
|
||||
const counted = input.subagents.filter(
|
||||
(agent) => agent.kind !== "workflow" || !memberTotalsByWorkflow.has(agent.id),
|
||||
);
|
||||
return {
|
||||
groups,
|
||||
activationsBySubagentId,
|
||||
activeCount: counted.filter((agent) => agent.status === "pending" || agent.status === "running")
|
||||
.length,
|
||||
waitingCount: counted.filter((agent) => agent.status === "waiting").length,
|
||||
settledCount: counted.filter(isSettledOrchestrationV2Subagent).length,
|
||||
totalTokens:
|
||||
reportedUsage.length === 0
|
||||
? null
|
||||
: reportedUsage.reduce((total, tokens) => total + tokens, 0),
|
||||
};
|
||||
}
|
||||
|
||||
export const formatSubagentTokenCount = (totalTokens: number) =>
|
||||
totalTokens >= 999_500
|
||||
? `${(totalTokens / 1_000_000).toFixed(1).replace(/\.0$/, "")}M`
|
||||
: totalTokens >= 1_000
|
||||
? `${(totalTokens / 1_000).toFixed(1).replace(/\.0$/, "")}k`
|
||||
: String(totalTokens);
|
||||
Reference in new issue
Block a user