@spunto/design-system 0.83.1 → 0.84.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
RotateCcwIcon,
|
|
19
19
|
SparklesIcon,
|
|
20
20
|
UserIcon,
|
|
21
|
+
UsersIcon,
|
|
21
22
|
WrenchIcon,
|
|
22
23
|
} from "lucide-react"
|
|
23
24
|
|
|
@@ -41,6 +42,15 @@ import {
|
|
|
41
42
|
runStats,
|
|
42
43
|
toolSubject,
|
|
43
44
|
} from "./tool-stack"
|
|
45
|
+
import {
|
|
46
|
+
type Subagent,
|
|
47
|
+
SubagentGroup,
|
|
48
|
+
buildSubagent,
|
|
49
|
+
indexSubagents,
|
|
50
|
+
isSubagentCall,
|
|
51
|
+
subagentCalls,
|
|
52
|
+
subagentStatus,
|
|
53
|
+
} from "./subagents"
|
|
44
54
|
import type { AttachmentUrl, PendingMessage, TaskDraft, TaskEvent } from "./types"
|
|
45
55
|
|
|
46
56
|
/**
|
|
@@ -88,6 +98,8 @@ type Item =
|
|
|
88
98
|
| { kind: "tool"; entry: ToolCall }
|
|
89
99
|
/** A chain of machinery, folded — see `tool-stack.tsx`. */
|
|
90
100
|
| { kind: "run"; run: ToolRun }
|
|
101
|
+
/** Subagents started together, off the session's thread — see `subagents.tsx`. */
|
|
102
|
+
| { kind: "agents"; agents: Subagent[] }
|
|
91
103
|
|
|
92
104
|
/**
|
|
93
105
|
* A marker on the rail. `tone` is what carries meaning at a glance, so it stays a small set.
|
|
@@ -244,6 +256,30 @@ function ToolRunEntry({
|
|
|
244
256
|
)
|
|
245
257
|
}
|
|
246
258
|
|
|
259
|
+
/** Subagents on the rail: one marker for the batch, busy while any of them is. */
|
|
260
|
+
function SubagentsEntry({
|
|
261
|
+
agents,
|
|
262
|
+
attachmentUrl,
|
|
263
|
+
onOpenStack,
|
|
264
|
+
live,
|
|
265
|
+
}: {
|
|
266
|
+
agents: Subagent[]
|
|
267
|
+
attachmentUrl: AttachmentUrl
|
|
268
|
+
onOpenStack: (seq: number) => void
|
|
269
|
+
live: boolean
|
|
270
|
+
}) {
|
|
271
|
+
const statuses = agents.map((agent) => subagentStatus(agent, live))
|
|
272
|
+
return (
|
|
273
|
+
<Entry className="pb-3">
|
|
274
|
+
<Marker
|
|
275
|
+
icon={<UsersIcon className="h-3 w-3" />}
|
|
276
|
+
tone={statuses.includes("running") ? "pending" : statuses.includes("failed") ? "danger" : "muted"}
|
|
277
|
+
/>
|
|
278
|
+
<SubagentGroup agents={agents} attachmentUrl={attachmentUrl} onOpenStack={onOpenStack} live={live} />
|
|
279
|
+
</Entry>
|
|
280
|
+
)
|
|
281
|
+
}
|
|
282
|
+
|
|
247
283
|
function ThinkingEntry({ event }: { event: TaskEvent }) {
|
|
248
284
|
const [open, setOpen] = useState(false)
|
|
249
285
|
const text = str(event.payload.text)
|
|
@@ -628,14 +664,17 @@ export function handedBack(events: TaskEvent[]): boolean {
|
|
|
628
664
|
/** `useTimeline`'s fold, pure — exported for the tests (jsdom cannot lay the list out). */
|
|
629
665
|
export function foldTimeline(events: TaskEvent[]) {
|
|
630
666
|
{
|
|
631
|
-
const flat: Item[] = []
|
|
667
|
+
const flat: (Item | { kind: "agent"; agent: Subagent })[] = []
|
|
632
668
|
const byCallId = new Map<string, ToolCall>()
|
|
669
|
+
const agentsByCallId = new Map<string, Subagent>()
|
|
633
670
|
const raw: TaskEvent[] = []
|
|
671
|
+
// What subagents did is nested under the call that started each one, not told on this thread.
|
|
672
|
+
const { top, index } = indexSubagents(events)
|
|
634
673
|
let lastMessage = ""
|
|
635
674
|
// The queued reply whose way to the session the events below are still telling.
|
|
636
675
|
let tracking: ReplyDelivery | null = null
|
|
637
676
|
|
|
638
|
-
for (const event of
|
|
677
|
+
for (const event of top) {
|
|
639
678
|
// ── A queued reply, and its way to the session ──
|
|
640
679
|
if (event.type === "machine.wake" || event.type === "reply.failed") {
|
|
641
680
|
// Steps of a delivery, drawn under the message they are about rather than as rows. One
|
|
@@ -694,6 +733,13 @@ export function foldTimeline(events: TaskEvent[]) {
|
|
|
694
733
|
if (event.type === "raw") raw.push(event)
|
|
695
734
|
continue
|
|
696
735
|
}
|
|
736
|
+
if (event.type === "tool.call" && isSubagentCall(event, index)) {
|
|
737
|
+
const agent = buildSubagent(event, index)
|
|
738
|
+
const id = str(event.payload.callId)
|
|
739
|
+
if (id) agentsByCallId.set(id, agent)
|
|
740
|
+
flat.push({ kind: "agent", agent })
|
|
741
|
+
continue
|
|
742
|
+
}
|
|
697
743
|
if (event.type === "tool.call") {
|
|
698
744
|
const entry: ToolCall = { call: event }
|
|
699
745
|
const id = str(event.payload.callId)
|
|
@@ -702,6 +748,11 @@ export function foldTimeline(events: TaskEvent[]) {
|
|
|
702
748
|
continue
|
|
703
749
|
}
|
|
704
750
|
if (event.type === "tool.result") {
|
|
751
|
+
const agent = agentsByCallId.get(str(event.payload.callId))
|
|
752
|
+
if (agent) {
|
|
753
|
+
agent.result = event
|
|
754
|
+
continue
|
|
755
|
+
}
|
|
705
756
|
const entry = byCallId.get(str(event.payload.callId))
|
|
706
757
|
// A result whose call we never saw (ingestion that started mid-session) still gets a
|
|
707
758
|
// line: dropping it would hide a tool failure.
|
|
@@ -733,6 +784,7 @@ export function foldTimeline(events: TaskEvent[]) {
|
|
|
733
784
|
// working to report, so what sits between two of them *is* one stretch of work.
|
|
734
785
|
const items: Item[] = []
|
|
735
786
|
let buffer: RunItem[] = []
|
|
787
|
+
let lastWasAgent = false
|
|
736
788
|
|
|
737
789
|
const flush = () => {
|
|
738
790
|
if (buffer.length === 0) return
|
|
@@ -745,6 +797,16 @@ export function foldTimeline(events: TaskEvent[]) {
|
|
|
745
797
|
}
|
|
746
798
|
|
|
747
799
|
for (const item of flat) {
|
|
800
|
+
// Subagents started one after the other — the same turn fanning out — are one block.
|
|
801
|
+
if (item.kind === "agent") {
|
|
802
|
+
flush()
|
|
803
|
+
const last = items[items.length - 1]
|
|
804
|
+
if (last?.kind === "agents" && lastWasAgent) last.agents.push(item.agent)
|
|
805
|
+
else items.push({ kind: "agents", agents: [item.agent] })
|
|
806
|
+
lastWasAgent = true
|
|
807
|
+
continue
|
|
808
|
+
}
|
|
809
|
+
lastWasAgent = false
|
|
748
810
|
if (item.kind === "tool") {
|
|
749
811
|
buffer.push({ kind: "tool", entry: item.entry })
|
|
750
812
|
continue
|
|
@@ -765,7 +827,12 @@ export function foldTimeline(events: TaskEvent[]) {
|
|
|
765
827
|
let runIndex = 0
|
|
766
828
|
for (const item of items) {
|
|
767
829
|
if (item.kind === "tool") calls.push({ ...item.entry, runIndex: runIndex++ })
|
|
768
|
-
else if (item.kind === "
|
|
830
|
+
else if (item.kind === "agents") {
|
|
831
|
+
for (const agent of item.agents)
|
|
832
|
+
for (const entry of [{ call: agent.call, result: agent.result }, ...subagentCalls(agent)])
|
|
833
|
+
calls.push({ ...entry, runIndex })
|
|
834
|
+
runIndex += 1
|
|
835
|
+
} else if (item.kind === "run") {
|
|
769
836
|
for (const entry of runCalls(item.run)) calls.push({ ...entry, runIndex })
|
|
770
837
|
runIndex += 1
|
|
771
838
|
}
|
|
@@ -788,6 +855,7 @@ type Row = Item | { kind: "pending"; text: string; previews?: string[] } | { kin
|
|
|
788
855
|
* tool entry's own call (or the orphaned result standing in for it) is already enough. */
|
|
789
856
|
function rowKey(row: Row): string | number {
|
|
790
857
|
if (row.kind === "tool") return row.entry.call.seq
|
|
858
|
+
if (row.kind === "agents") return row.agents[0].call.seq
|
|
791
859
|
if (row.kind === "run") return row.run.items[0].kind === "tool" ? row.run.items[0].entry.call.seq : row.run.items[0].event.seq
|
|
792
860
|
if (row.kind === "event") return row.event.seq
|
|
793
861
|
return row.kind
|
|
@@ -827,6 +895,8 @@ function Row({
|
|
|
827
895
|
if (row.kind === "working") return <WorkingEntry />
|
|
828
896
|
if (row.kind === "run")
|
|
829
897
|
return <ToolRunEntry run={row.run} attachmentUrl={attachmentUrl} onOpenStack={onOpenStack} />
|
|
898
|
+
if (row.kind === "agents")
|
|
899
|
+
return <SubagentsEntry agents={row.agents} attachmentUrl={attachmentUrl} onOpenStack={onOpenStack} live={live} />
|
|
830
900
|
if (row.kind === "tool")
|
|
831
901
|
return <ToolEntry entry={row.entry} attachmentUrl={attachmentUrl} onOpenStack={onOpenStack} />
|
|
832
902
|
if (row.event.type === "thinking") return <ThinkingEntry event={row.event} />
|
|
@@ -50,6 +50,17 @@ export type { TaskDetailsProps } from "./task-details"
|
|
|
50
50
|
// ── The conversation ────────────────────────────────────────────────────────────────────────
|
|
51
51
|
export { TaskEventStream } from "./event-stream"
|
|
52
52
|
export type { ReplyDelivery, TaskEventStreamProps } from "./event-stream"
|
|
53
|
+
export {
|
|
54
|
+
SubagentGroup,
|
|
55
|
+
buildSubagent,
|
|
56
|
+
indexSubagents,
|
|
57
|
+
isSubagentCall,
|
|
58
|
+
subagentActivity,
|
|
59
|
+
subagentReport,
|
|
60
|
+
subagentStatus,
|
|
61
|
+
subagentTitle,
|
|
62
|
+
} from "./subagents"
|
|
63
|
+
export type { Subagent, SubagentIndex, SubagentItem, SubagentStatus } from "./subagents"
|
|
53
64
|
export { INTERACTIVE_PROTOCOLS, TaskComposer } from "./composer"
|
|
54
65
|
export type { TaskComposerProps } from "./composer"
|
|
55
66
|
export { AgentMarkdown, renderInlineMarkdown } from "./agent-markdown"
|
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { useState, type ReactNode } from "react"
|
|
4
|
+
import { CheckIcon, ChevronRightIcon, CircleAlertIcon, CircleSlashIcon, Loader2Icon, UsersIcon } from "lucide-react"
|
|
5
|
+
|
|
6
|
+
import { cn } from "../../utils"
|
|
7
|
+
import { AgentMarkdown } from "./agent-markdown"
|
|
8
|
+
import { taskSpan, useElapsed } from "./task-state"
|
|
9
|
+
import { type RunItem, type ToolCall, ToolRunBody, toolSubject } from "./tool-stack"
|
|
10
|
+
import type { AttachmentUrl, TaskEvent } from "./types"
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The subagents of a session — agents it started itself, through one of its own tool calls
|
|
14
|
+
* (Claude Code's `Agent`), and that work beside it: in the foreground while the call waits, or in
|
|
15
|
+
* the background while the turn goes on.
|
|
16
|
+
*
|
|
17
|
+
* On the stream they are two kinds of lines (`docs/agent-contract.md`): what a subagent *does*
|
|
18
|
+
* carries `parentCallId`, the call that started it, and a `subagent` line says where it stands —
|
|
19
|
+
* what it was asked, what it is doing now, how it ended — restated whole on every change. Told
|
|
20
|
+
* flat, its twenty reads land on the session's thread as if the session had made them, and three
|
|
21
|
+
* subagents in parallel interleave into one unreadable run. So they are taken off the thread and
|
|
22
|
+
* drawn where they started: **one block per batch of calls**, a row per subagent saying what it is
|
|
23
|
+
* doing right now, and its own transcript one click away — the shape t3code gives Claude Code's
|
|
24
|
+
* subagents, and the one the CLI draws in a terminal.
|
|
25
|
+
*
|
|
26
|
+
* A rendering decision, like the runs of `tool-stack.tsx`: the stored stream stays flat.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
const str = (v: unknown): string => (typeof v === "string" ? v : "")
|
|
30
|
+
const bool = (v: unknown): boolean => v === true
|
|
31
|
+
const num = (v: unknown): number | null => (typeof v === "number" && Number.isFinite(v) ? v : null)
|
|
32
|
+
|
|
33
|
+
export type SubagentStatus = "running" | "completed" | "failed" | "stopped"
|
|
34
|
+
|
|
35
|
+
/** What a subagent's own transcript is made of — the session's timeline, one level down. */
|
|
36
|
+
export type SubagentItem =
|
|
37
|
+
| { kind: "tool"; entry: ToolCall }
|
|
38
|
+
| { kind: "thinking"; event: TaskEvent }
|
|
39
|
+
| { kind: "message"; event: TaskEvent }
|
|
40
|
+
| { kind: "agent"; agent: Subagent }
|
|
41
|
+
|
|
42
|
+
export type Subagent = {
|
|
43
|
+
/** The call that started it — its input is what it was asked. */
|
|
44
|
+
call: TaskEvent
|
|
45
|
+
/** That call's answer: the report of a foreground subagent, an acknowledgement for a background one. */
|
|
46
|
+
result?: TaskEvent
|
|
47
|
+
/** The latest `subagent` line about it, if the runner says where it stands. */
|
|
48
|
+
state: TaskEvent | null
|
|
49
|
+
transcript: SubagentItem[]
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Where every nested line lives, by the call it is nested under. */
|
|
53
|
+
export type SubagentIndex = {
|
|
54
|
+
children: Map<string, TaskEvent[]>
|
|
55
|
+
states: Map<string, TaskEvent>
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Split a stream into the session's own thread and what its subagents did. `top` is what the
|
|
60
|
+
* timeline folds as before; the rest is reached through the calls that started each subagent.
|
|
61
|
+
*/
|
|
62
|
+
export function indexSubagents(events: TaskEvent[]): { top: TaskEvent[]; index: SubagentIndex } {
|
|
63
|
+
const top: TaskEvent[] = []
|
|
64
|
+
const children = new Map<string, TaskEvent[]>()
|
|
65
|
+
const states = new Map<string, TaskEvent>()
|
|
66
|
+
for (const event of events) {
|
|
67
|
+
if (event.type === "subagent") {
|
|
68
|
+
const id = str(event.payload.callId)
|
|
69
|
+
if (id) states.set(id, event)
|
|
70
|
+
continue
|
|
71
|
+
}
|
|
72
|
+
const parent = str(event.payload.parentCallId)
|
|
73
|
+
if (!parent) {
|
|
74
|
+
top.push(event)
|
|
75
|
+
continue
|
|
76
|
+
}
|
|
77
|
+
const list = children.get(parent)
|
|
78
|
+
if (list) list.push(event)
|
|
79
|
+
else children.set(parent, [event])
|
|
80
|
+
}
|
|
81
|
+
return { top, index: { children, states } }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Whether a tool call started a subagent. Said by the stream when the runner tells (a `subagent`
|
|
86
|
+
* line, or lines nested under it); else read off the call — Claude Code's `Agent` (once `Task`)
|
|
87
|
+
* with what it asks of one — so a session from before the runner said so still draws its subagents
|
|
88
|
+
* as such, with the call's answer as their report.
|
|
89
|
+
*/
|
|
90
|
+
export function isSubagentCall(call: TaskEvent, index: SubagentIndex): boolean {
|
|
91
|
+
const id = str(call.payload.callId)
|
|
92
|
+
if (id && (index.states.has(id) || index.children.has(id))) return true
|
|
93
|
+
const name = str(call.payload.name)
|
|
94
|
+
if (name !== "Agent" && name !== "Task") return false
|
|
95
|
+
const input = parseInput(str(call.payload.input))
|
|
96
|
+
return !!input && (typeof input.subagent_type === "string" || typeof input.prompt === "string")
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** A subagent, and recursively the subagents it started in turn. */
|
|
100
|
+
export function buildSubagent(call: TaskEvent, index: SubagentIndex): Subagent {
|
|
101
|
+
const id = str(call.payload.callId)
|
|
102
|
+
const transcript: SubagentItem[] = []
|
|
103
|
+
const byCallId = new Map<string, ToolCall>()
|
|
104
|
+
const agents = new Map<string, Subagent>()
|
|
105
|
+
for (const event of (id && index.children.get(id)) || []) {
|
|
106
|
+
if (event.type === "tool.call") {
|
|
107
|
+
const callId = str(event.payload.callId)
|
|
108
|
+
if (isSubagentCall(event, index)) {
|
|
109
|
+
const agent = buildSubagent(event, index)
|
|
110
|
+
if (callId) agents.set(callId, agent)
|
|
111
|
+
transcript.push({ kind: "agent", agent })
|
|
112
|
+
continue
|
|
113
|
+
}
|
|
114
|
+
const entry: ToolCall = { call: event }
|
|
115
|
+
if (callId) byCallId.set(callId, entry)
|
|
116
|
+
transcript.push({ kind: "tool", entry })
|
|
117
|
+
} else if (event.type === "tool.result") {
|
|
118
|
+
const callId = str(event.payload.callId)
|
|
119
|
+
const agent = agents.get(callId)
|
|
120
|
+
const entry = byCallId.get(callId)
|
|
121
|
+
if (agent) agent.result = event
|
|
122
|
+
else if (entry) entry.result = event
|
|
123
|
+
else transcript.push({ kind: "tool", entry: { call: event, result: event } })
|
|
124
|
+
} else if (event.type === "thinking") {
|
|
125
|
+
transcript.push({ kind: "thinking", event })
|
|
126
|
+
} else if (event.type === "message" && str(event.payload.text).trim()) {
|
|
127
|
+
transcript.push({ kind: "message", event })
|
|
128
|
+
}
|
|
129
|
+
// `usage` and `plan` of a subagent: its cost is the session's (the sidebar sums it), its
|
|
130
|
+
// todo list is its own business.
|
|
131
|
+
}
|
|
132
|
+
return { call, state: (id && index.states.get(id)) || null, transcript }
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function parseInput(input: string): Record<string, unknown> | null {
|
|
136
|
+
if (!input.startsWith("{")) return null
|
|
137
|
+
try {
|
|
138
|
+
const parsed = JSON.parse(input) as unknown
|
|
139
|
+
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? (parsed as Record<string, unknown>) : null
|
|
140
|
+
} catch {
|
|
141
|
+
return null
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Where it stands. The runner's word when there is one; else the call's answer — a call answered
|
|
147
|
+
* is a subagent done. `live: false` (the task can no longer move) turns a subagent still said to
|
|
148
|
+
* be running into a stopped one: whatever ran it is gone, and a clock ticking forever on it would
|
|
149
|
+
* be a lie.
|
|
150
|
+
*/
|
|
151
|
+
export function subagentStatus(agent: Subagent, live = true): SubagentStatus {
|
|
152
|
+
const said = str(agent.state?.payload.status)
|
|
153
|
+
let status: SubagentStatus
|
|
154
|
+
if (said === "running" || said === "completed" || said === "failed" || said === "stopped") status = said
|
|
155
|
+
else if (agent.result) status = bool(agent.result.payload.isError) ? "failed" : "completed"
|
|
156
|
+
else status = "running"
|
|
157
|
+
return status === "running" && !live ? "stopped" : status
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** What it was asked, in a few words. */
|
|
161
|
+
export function subagentTitle(agent: Subagent): string {
|
|
162
|
+
const said = str(agent.state?.payload.description).trim()
|
|
163
|
+
if (said) return said
|
|
164
|
+
const input = parseInput(str(agent.call.payload.input))
|
|
165
|
+
const description = str(input?.description).trim()
|
|
166
|
+
return description || toolSubject(str(agent.call.payload.input)) || "Subagent"
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function subagentKind(agent: Subagent): string {
|
|
170
|
+
return str(agent.state?.payload.kind) || str(parseInput(str(agent.call.payload.input))?.subagent_type)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Its report: what it said when it ended, else the call's answer when that is the report. */
|
|
174
|
+
export function subagentReport(agent: Subagent): string {
|
|
175
|
+
const summary = str(agent.state?.payload.summary).trim()
|
|
176
|
+
if (summary) return summary
|
|
177
|
+
// A background subagent's call is answered at once, with an acknowledgement, not a report.
|
|
178
|
+
if (bool(agent.state?.payload.background)) return ""
|
|
179
|
+
return str(agent.result?.payload.output).trim()
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** The last call it made, as a line — what it is doing when the runner does not say. */
|
|
183
|
+
function lastMove(agent: Subagent): string {
|
|
184
|
+
for (let i = agent.transcript.length - 1; i >= 0; i--) {
|
|
185
|
+
const item = agent.transcript[i]
|
|
186
|
+
if (item.kind === "tool") return `${str(item.entry.call.payload.name)} ${toolSubject(str(item.entry.call.payload.input))}`.trim()
|
|
187
|
+
if (item.kind === "agent") return `Agent ${subagentTitle(item.agent)}`
|
|
188
|
+
}
|
|
189
|
+
return ""
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** One line under its title: what it is doing now, or how it ended. */
|
|
193
|
+
export function subagentActivity(agent: Subagent, status: SubagentStatus): string {
|
|
194
|
+
if (status === "running") {
|
|
195
|
+
return str(agent.state?.payload.activity).trim() || lastMove(agent) || "Starting…"
|
|
196
|
+
}
|
|
197
|
+
const report = subagentReport(agent)
|
|
198
|
+
// A line of a row, not markdown: its first sentence, without the marks.
|
|
199
|
+
const firstLine = report
|
|
200
|
+
.split("\n")
|
|
201
|
+
.map((l) => l.replace(/^[#>*\-\s]+/, "").replace(/\*\*|__|`/g, "").trim())
|
|
202
|
+
.find(Boolean)
|
|
203
|
+
if (firstLine) return firstLine
|
|
204
|
+
const calls = num(agent.state?.payload.toolUses) ?? agent.transcript.filter((i) => i.kind === "tool").length
|
|
205
|
+
return calls > 0 ? `${calls} tool ${calls === 1 ? "call" : "calls"}` : ""
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** How long it ran, once it has ended. `null` while running — that is `useElapsed`'s job. */
|
|
209
|
+
function subagentDuration(agent: Subagent, status: SubagentStatus): string | null {
|
|
210
|
+
if (status === "running") return null
|
|
211
|
+
const ms = num(agent.state?.payload.durationMs)
|
|
212
|
+
if (ms !== null) return taskSpan(new Date(0).toISOString(), new Date(ms).toISOString())
|
|
213
|
+
const end = agent.state?.ts ?? agent.result?.ts
|
|
214
|
+
return taskSpan(agent.call.ts, end)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const STATUS_LABEL: Record<SubagentStatus, string> = {
|
|
218
|
+
running: "Running",
|
|
219
|
+
completed: "Done",
|
|
220
|
+
failed: "Failed",
|
|
221
|
+
stopped: "Stopped",
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function StatusIcon({ status }: { status: SubagentStatus }) {
|
|
225
|
+
if (status === "running") return <Loader2Icon className="h-3 w-3 animate-spin text-amber-600 dark:text-amber-500" />
|
|
226
|
+
if (status === "completed") return <CheckIcon className="h-3 w-3 text-green-600 dark:text-green-500" />
|
|
227
|
+
if (status === "failed") return <CircleAlertIcon className="h-3 w-3 text-destructive" />
|
|
228
|
+
return <CircleSlashIcon className="h-3 w-3 text-muted-foreground" />
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// ── A batch of them ───────────────────────────────────────────────────────────────────────────
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* The subagents one turn started together, as one block: how many, how many still at work, and a
|
|
235
|
+
* row each. Open by default — a block that hid what three agents are doing right now would be
|
|
236
|
+
* hiding the only thing happening in the session.
|
|
237
|
+
*/
|
|
238
|
+
export function SubagentGroup({
|
|
239
|
+
agents,
|
|
240
|
+
attachmentUrl,
|
|
241
|
+
onOpenStack,
|
|
242
|
+
live,
|
|
243
|
+
}: {
|
|
244
|
+
agents: Subagent[]
|
|
245
|
+
attachmentUrl: AttachmentUrl
|
|
246
|
+
onOpenStack?: (seq: number) => void
|
|
247
|
+
live: boolean
|
|
248
|
+
}) {
|
|
249
|
+
const [open, setOpen] = useState(true)
|
|
250
|
+
const statuses = agents.map((agent) => subagentStatus(agent, live))
|
|
251
|
+
const running = statuses.filter((s) => s === "running").length
|
|
252
|
+
const failed = statuses.filter((s) => s === "failed").length
|
|
253
|
+
const elapsed = useElapsed(running > 0 ? agents[0].call.ts : null)
|
|
254
|
+
const tally = running > 0 ? `${running} working` : failed > 0 ? `${failed} failed` : "all done"
|
|
255
|
+
|
|
256
|
+
return (
|
|
257
|
+
<div className="min-w-0 rounded-lg border bg-card" data-slot="subagents">
|
|
258
|
+
<button
|
|
259
|
+
type="button"
|
|
260
|
+
onClick={() => setOpen((v) => !v)}
|
|
261
|
+
className="flex w-full items-center gap-2.5 rounded-t-lg px-3 py-2 text-left transition-colors hover:bg-muted/40"
|
|
262
|
+
>
|
|
263
|
+
<span className="flex shrink-0 -space-x-1.5">
|
|
264
|
+
{agents.slice(0, 3).map((agent, i) => (
|
|
265
|
+
<span
|
|
266
|
+
key={agent.call.seq}
|
|
267
|
+
className={cn(
|
|
268
|
+
"flex h-5 w-5 items-center justify-center rounded-full border bg-background text-muted-foreground",
|
|
269
|
+
statuses[i] === "running" && "border-amber-500/50 text-amber-600 dark:text-amber-500",
|
|
270
|
+
)}
|
|
271
|
+
>
|
|
272
|
+
<UsersIcon className="h-2.5 w-2.5" />
|
|
273
|
+
</span>
|
|
274
|
+
))}
|
|
275
|
+
</span>
|
|
276
|
+
<span className="min-w-0 flex-1">
|
|
277
|
+
<span className="block text-xs font-medium">
|
|
278
|
+
{agents.length} {agents.length === 1 ? "subagent" : "subagents"}
|
|
279
|
+
</span>
|
|
280
|
+
<span
|
|
281
|
+
className={cn(
|
|
282
|
+
"block text-[10px]",
|
|
283
|
+
running > 0 ? "text-amber-600 dark:text-amber-500" : failed > 0 ? "text-destructive" : "text-muted-foreground",
|
|
284
|
+
)}
|
|
285
|
+
>
|
|
286
|
+
{tally}
|
|
287
|
+
</span>
|
|
288
|
+
</span>
|
|
289
|
+
{elapsed && <span className="shrink-0 font-mono text-[10px] text-muted-foreground tabular-nums">{elapsed}</span>}
|
|
290
|
+
<ChevronRightIcon
|
|
291
|
+
className={cn("h-3.5 w-3.5 shrink-0 text-muted-foreground/60 transition-transform", open && "rotate-90")}
|
|
292
|
+
/>
|
|
293
|
+
</button>
|
|
294
|
+
{open && (
|
|
295
|
+
<div className="space-y-1 border-t p-1.5">
|
|
296
|
+
{agents.map((agent) => (
|
|
297
|
+
<SubagentRow
|
|
298
|
+
key={agent.call.seq}
|
|
299
|
+
agent={agent}
|
|
300
|
+
attachmentUrl={attachmentUrl}
|
|
301
|
+
onOpenStack={onOpenStack}
|
|
302
|
+
live={live}
|
|
303
|
+
/>
|
|
304
|
+
))}
|
|
305
|
+
</div>
|
|
306
|
+
)}
|
|
307
|
+
</div>
|
|
308
|
+
)
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** One subagent: what it was asked, what it is doing or how it ended, and its transcript on demand. */
|
|
312
|
+
function SubagentRow({
|
|
313
|
+
agent,
|
|
314
|
+
attachmentUrl,
|
|
315
|
+
onOpenStack,
|
|
316
|
+
live,
|
|
317
|
+
}: {
|
|
318
|
+
agent: Subagent
|
|
319
|
+
attachmentUrl: AttachmentUrl
|
|
320
|
+
onOpenStack?: (seq: number) => void
|
|
321
|
+
live: boolean
|
|
322
|
+
}) {
|
|
323
|
+
const [open, setOpen] = useState(false)
|
|
324
|
+
const status = subagentStatus(agent, live)
|
|
325
|
+
const elapsed = useElapsed(status === "running" ? agent.call.ts : null)
|
|
326
|
+
const took = subagentDuration(agent, status)
|
|
327
|
+
const kind = subagentKind(agent)
|
|
328
|
+
const activity = subagentActivity(agent, status)
|
|
329
|
+
|
|
330
|
+
return (
|
|
331
|
+
<div className="min-w-0 rounded-md border border-border/60" data-slot="subagent" data-status={status}>
|
|
332
|
+
<button
|
|
333
|
+
type="button"
|
|
334
|
+
onClick={() => setOpen((v) => !v)}
|
|
335
|
+
className="group/agent flex w-full items-center gap-2.5 rounded-md px-2.5 py-1.5 text-left transition-colors hover:bg-muted/40"
|
|
336
|
+
>
|
|
337
|
+
<span className="flex h-5 w-5 shrink-0 items-center justify-center rounded-full border bg-background">
|
|
338
|
+
<StatusIcon status={status} />
|
|
339
|
+
</span>
|
|
340
|
+
<span className="min-w-0 flex-1">
|
|
341
|
+
<span className="flex min-w-0 items-baseline gap-1.5">
|
|
342
|
+
<span className="truncate text-xs font-medium">{subagentTitle(agent)}</span>
|
|
343
|
+
<span
|
|
344
|
+
className={cn(
|
|
345
|
+
"shrink-0 text-[10px]",
|
|
346
|
+
status === "running"
|
|
347
|
+
? "text-amber-600 dark:text-amber-500"
|
|
348
|
+
: status === "failed"
|
|
349
|
+
? "text-destructive"
|
|
350
|
+
: "text-muted-foreground",
|
|
351
|
+
)}
|
|
352
|
+
>
|
|
353
|
+
{STATUS_LABEL[status]}
|
|
354
|
+
</span>
|
|
355
|
+
{kind && <span className="hidden shrink-0 text-[10px] text-muted-foreground/70 sm:inline">{kind}</span>}
|
|
356
|
+
</span>
|
|
357
|
+
{activity && (
|
|
358
|
+
<span className="block truncate text-[11px] text-muted-foreground">{activity}</span>
|
|
359
|
+
)}
|
|
360
|
+
</span>
|
|
361
|
+
<span
|
|
362
|
+
className={cn(
|
|
363
|
+
"shrink-0 font-mono text-[10px] tabular-nums",
|
|
364
|
+
status === "running" ? "font-medium text-amber-600 dark:text-amber-500" : "text-muted-foreground/70",
|
|
365
|
+
)}
|
|
366
|
+
>
|
|
367
|
+
{status === "running" ? elapsed : took}
|
|
368
|
+
</span>
|
|
369
|
+
<ChevronRightIcon
|
|
370
|
+
className={cn(
|
|
371
|
+
"h-3 w-3 shrink-0 text-muted-foreground/50 transition-transform group-hover/agent:text-muted-foreground",
|
|
372
|
+
open && "rotate-90",
|
|
373
|
+
)}
|
|
374
|
+
/>
|
|
375
|
+
</button>
|
|
376
|
+
{open && <SubagentTranscript agent={agent} attachmentUrl={attachmentUrl} onOpenStack={onOpenStack} live={live} />}
|
|
377
|
+
</div>
|
|
378
|
+
)
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** What it was asked, everything it did, and what it reported — its own timeline, indented. */
|
|
382
|
+
function SubagentTranscript({
|
|
383
|
+
agent,
|
|
384
|
+
attachmentUrl,
|
|
385
|
+
onOpenStack,
|
|
386
|
+
live,
|
|
387
|
+
}: {
|
|
388
|
+
agent: Subagent
|
|
389
|
+
attachmentUrl: AttachmentUrl
|
|
390
|
+
onOpenStack?: (seq: number) => void
|
|
391
|
+
live: boolean
|
|
392
|
+
}) {
|
|
393
|
+
const prompt = str(parseInput(str(agent.call.payload.input))?.prompt).trim()
|
|
394
|
+
const report = subagentReport(agent)
|
|
395
|
+
// The report is usually its last message again (Claude Code's report *is* that message): said once.
|
|
396
|
+
const last = agent.transcript[agent.transcript.length - 1]
|
|
397
|
+
const reportIsLast = last?.kind === "message" && str(last.event.payload.text).trim() === report
|
|
398
|
+
|
|
399
|
+
const blocks: ReactNode[] = []
|
|
400
|
+
let run: RunItem[] = []
|
|
401
|
+
const flush = () => {
|
|
402
|
+
if (run.length === 0) return
|
|
403
|
+
blocks.push(<ToolRunBody key={`run-${blocks.length}`} run={{ items: run }} attachmentUrl={attachmentUrl} onOpenStack={onOpenStack} />)
|
|
404
|
+
run = []
|
|
405
|
+
}
|
|
406
|
+
for (const item of agent.transcript) {
|
|
407
|
+
if (item.kind === "tool") run.push({ kind: "tool", entry: item.entry })
|
|
408
|
+
else if (item.kind === "thinking") run.push({ kind: "thinking", event: item.event })
|
|
409
|
+
else {
|
|
410
|
+
flush()
|
|
411
|
+
if (item.kind === "message")
|
|
412
|
+
blocks.push(
|
|
413
|
+
<AgentMarkdown key={item.event.seq} text={str(item.event.payload.text)} className="text-xs [&_p]:my-1" />,
|
|
414
|
+
)
|
|
415
|
+
else
|
|
416
|
+
blocks.push(
|
|
417
|
+
<SubagentGroup
|
|
418
|
+
key={item.agent.call.seq}
|
|
419
|
+
agents={[item.agent]}
|
|
420
|
+
attachmentUrl={attachmentUrl}
|
|
421
|
+
onOpenStack={onOpenStack}
|
|
422
|
+
live={live}
|
|
423
|
+
/>,
|
|
424
|
+
)
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
flush()
|
|
428
|
+
|
|
429
|
+
return (
|
|
430
|
+
<div className="space-y-2 border-t border-border/60 px-2.5 pt-2 pb-2.5" data-slot="subagent-transcript">
|
|
431
|
+
{prompt && (
|
|
432
|
+
<details className="group/prompt">
|
|
433
|
+
<summary className="cursor-pointer list-none text-[10px] font-medium uppercase tracking-wide text-muted-foreground hover:text-foreground">
|
|
434
|
+
Prompt
|
|
435
|
+
</summary>
|
|
436
|
+
<p className="mt-1 whitespace-pre-wrap rounded-md bg-muted/50 px-2 py-1.5 text-[11px] leading-relaxed">{prompt}</p>
|
|
437
|
+
</details>
|
|
438
|
+
)}
|
|
439
|
+
{blocks.length > 0 ? (
|
|
440
|
+
blocks
|
|
441
|
+
) : (
|
|
442
|
+
<p className="text-[11px] text-muted-foreground">
|
|
443
|
+
{subagentStatus(agent, live) === "running" ? "Nothing yet — its first step is on its way." : "Nothing it did was captured."}
|
|
444
|
+
</p>
|
|
445
|
+
)}
|
|
446
|
+
{report && !reportIsLast && (
|
|
447
|
+
<div>
|
|
448
|
+
<p className="mb-0.5 text-[10px] font-medium uppercase tracking-wide text-muted-foreground">Report</p>
|
|
449
|
+
<AgentMarkdown text={report} className="text-xs [&_p]:my-1" />
|
|
450
|
+
</div>
|
|
451
|
+
)}
|
|
452
|
+
</div>
|
|
453
|
+
)
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/** Every tool call a subagent made, its own subagents' included — for the session's stack. */
|
|
457
|
+
export function subagentCalls(agent: Subagent): ToolCall[] {
|
|
458
|
+
const out: ToolCall[] = []
|
|
459
|
+
for (const item of agent.transcript) {
|
|
460
|
+
if (item.kind === "tool") out.push(item.entry)
|
|
461
|
+
else if (item.kind === "agent") out.push({ call: item.agent.call, result: item.agent.result }, ...subagentCalls(item.agent))
|
|
462
|
+
}
|
|
463
|
+
return out
|
|
464
|
+
}
|
|
@@ -21,6 +21,7 @@ import { formatCost, formatTokens } from "./session-usage"
|
|
|
21
21
|
import { TaskCockpit } from "./task-cockpit"
|
|
22
22
|
import { TaskDetails } from "./task-details"
|
|
23
23
|
import { TaskList } from "./task-list"
|
|
24
|
+
import { SubagentGroup, subagentActivity, subagentStatus } from "./subagents"
|
|
24
25
|
import { taskSpan } from "./task-state"
|
|
25
26
|
import { TaskTitle } from "./task-title"
|
|
26
27
|
import { runStats, toolSubject, toolVerb, type ToolRun } from "./tool-stack"
|
|
@@ -911,3 +912,69 @@ describe("the composer while a reply is on its way", () => {
|
|
|
911
912
|
expect(screen.getByText(/your message is queued/i)).toBeTruthy()
|
|
912
913
|
})
|
|
913
914
|
})
|
|
915
|
+
|
|
916
|
+
describe("subagents", () => {
|
|
917
|
+
const agentCall = (seq: number, callId: string, description: string) =>
|
|
918
|
+
ev(seq, "tool.call", { callId, name: "Agent", input: JSON.stringify({ subagent_type: "general-purpose", description, prompt: `Do: ${description}` }) })
|
|
919
|
+
const fanOut = [
|
|
920
|
+
ev(1, "message", { role: "user", text: "look around" }),
|
|
921
|
+
agentCall(2, "a", "Read the README"),
|
|
922
|
+
ev(3, "subagent", { callId: "a", status: "running", description: "Read the README", background: true }),
|
|
923
|
+
ev(4, "tool.result", { callId: "a", isError: false, output: "Async agent launched successfully." }),
|
|
924
|
+
agentCall(5, "b", "List the apps"),
|
|
925
|
+
ev(6, "subagent", { callId: "b", status: "running", description: "List the apps", background: true }),
|
|
926
|
+
ev(7, "tool.result", { callId: "b", isError: false, output: "Async agent launched successfully." }),
|
|
927
|
+
ev(8, "tool.call", { callId: "a1", name: "Read", input: '{"file_path":"README.md"}', parentCallId: "a" }),
|
|
928
|
+
ev(9, "tool.call", { callId: "b1", name: "Bash", input: '{"command":"ls apps"}', parentCallId: "b" }),
|
|
929
|
+
ev(10, "subagent", { callId: "b", status: "running", description: "List the apps", background: true, activity: "Running ls apps", lastTool: "Bash" }),
|
|
930
|
+
ev(11, "tool.result", { callId: "a1", isError: false, output: "# Spunto", parentCallId: "a" }),
|
|
931
|
+
ev(12, "message", { role: "assistant", text: "The README is about Spunto.", parentCallId: "a" }),
|
|
932
|
+
ev(13, "subagent", { callId: "a", status: "completed", description: "Read the README", background: true, summary: "The README is about Spunto.", durationMs: 4200 }),
|
|
933
|
+
ev(14, "message", { role: "assistant", text: "Both are on it." }),
|
|
934
|
+
]
|
|
935
|
+
|
|
936
|
+
it("takes what they do off the session's thread, and draws the batch as one block", () => {
|
|
937
|
+
const { items, calls } = foldTimeline(fanOut)
|
|
938
|
+
expect(items.map((i) => i.kind)).toEqual(["event", "agents", "event"])
|
|
939
|
+
const block = items[1] as Extract<(typeof items)[number], { kind: "agents" }>
|
|
940
|
+
expect(block.agents.map((a) => [a.call.payload.callId, subagentStatus(a)])).toEqual([
|
|
941
|
+
["a", "completed"],
|
|
942
|
+
["b", "running"],
|
|
943
|
+
])
|
|
944
|
+
expect(block.agents[0].transcript.map((i) => i.kind)).toEqual(["tool", "message"])
|
|
945
|
+
// The session's last word is its own, not the subagent's report.
|
|
946
|
+
expect((items[2] as { event: TaskEvent }).event.seq).toBe(14)
|
|
947
|
+
// The stack still holds every call, nested ones included.
|
|
948
|
+
expect(calls.map((c) => c.call.payload.callId)).toEqual(["a", "a1", "b", "b1"])
|
|
949
|
+
})
|
|
950
|
+
|
|
951
|
+
it("says what each is doing now, or how it ended", () => {
|
|
952
|
+
const block = foldTimeline(fanOut).items[1] as { kind: "agents"; agents: Parameters<typeof subagentStatus>[0][] }
|
|
953
|
+
const [a, b] = block.agents
|
|
954
|
+
expect(subagentActivity(b, subagentStatus(b))).toBe("Running ls apps")
|
|
955
|
+
expect(subagentActivity(a, subagentStatus(a))).toBe("The README is about Spunto.")
|
|
956
|
+
// A task that can no longer move has no subagent still running.
|
|
957
|
+
expect(subagentStatus(b, false)).toBe("stopped")
|
|
958
|
+
})
|
|
959
|
+
|
|
960
|
+
it("draws a row per subagent, and its transcript on demand", () => {
|
|
961
|
+
const block = foldTimeline(fanOut).items[1] as { kind: "agents"; agents: Parameters<typeof subagentStatus>[0][] }
|
|
962
|
+
render(<SubagentGroup agents={block.agents} attachmentUrl={() => ""} live />)
|
|
963
|
+
expect(screen.getByText("2 subagents")).toBeTruthy()
|
|
964
|
+
expect(screen.getByText("1 working")).toBeTruthy()
|
|
965
|
+
fireEvent.click(screen.getByText("Read the README"))
|
|
966
|
+
expect(screen.getByText("Read")).toBeTruthy()
|
|
967
|
+
expect(screen.getByText("Prompt")).toBeTruthy()
|
|
968
|
+
})
|
|
969
|
+
|
|
970
|
+
it("still reads an Agent call from before the runner said so, its answer as the report", () => {
|
|
971
|
+
const { items } = foldTimeline([
|
|
972
|
+
agentCall(1, "x", "Explore the API"),
|
|
973
|
+
ev(2, "tool.result", { callId: "x", isError: false, output: "It has 40 modules." }),
|
|
974
|
+
])
|
|
975
|
+
const block = items[0] as { kind: "agents"; agents: Parameters<typeof subagentStatus>[0][] }
|
|
976
|
+
expect(block.kind).toBe("agents")
|
|
977
|
+
expect(subagentStatus(block.agents[0])).toBe("completed")
|
|
978
|
+
expect(subagentActivity(block.agents[0], "completed")).toBe("It has 40 modules.")
|
|
979
|
+
})
|
|
980
|
+
})
|