@spunto/design-system 0.27.0 → 0.30.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.
Files changed (32) hide show
  1. package/README.md +35 -1
  2. package/package.json +6 -1
  3. package/src/components/sheet.tsx +7 -1
  4. package/src/components/tasks/agent-markdown.tsx +322 -0
  5. package/src/components/tasks/attachments.ts +152 -0
  6. package/src/components/tasks/command-log.tsx +102 -0
  7. package/src/components/tasks/composer.tsx +252 -0
  8. package/src/components/tasks/conversation-list.tsx +238 -0
  9. package/src/components/tasks/diff-panel.tsx +534 -0
  10. package/src/components/tasks/event-stream.tsx +860 -0
  11. package/src/components/tasks/index.ts +111 -0
  12. package/src/components/tasks/machine.tsx +455 -0
  13. package/src/components/tasks/new-task-dialog.tsx +301 -0
  14. package/src/components/tasks/pull-request.tsx +179 -0
  15. package/src/components/tasks/session-usage.tsx +165 -0
  16. package/src/components/tasks/task-attachments.tsx +475 -0
  17. package/src/components/tasks/task-cockpit-mobile.tsx +272 -0
  18. package/src/components/tasks/task-cockpit.tsx +225 -0
  19. package/src/components/tasks/task-details.tsx +239 -0
  20. package/src/components/tasks/task-list.tsx +272 -0
  21. package/src/components/tasks/task-state.tsx +180 -0
  22. package/src/components/tasks/tasks.test.tsx +205 -0
  23. package/src/components/tasks/tool-stack.tsx +692 -0
  24. package/src/components/tasks/types.ts +220 -0
  25. package/src/components/tasks/worker-link.tsx +126 -0
  26. package/src/components/workers/index.ts +5 -2
  27. package/src/components/workers/types.ts +22 -0
  28. package/src/components/workers/worker-cockpit.tsx +17 -2
  29. package/src/components/workers/worker-status-history.test.ts +85 -0
  30. package/src/components/workers/worker-status-history.tsx +166 -0
  31. package/src/index.ts +1 -1
  32. package/src/utils.ts +23 -0
@@ -0,0 +1,166 @@
1
+ "use client"
2
+
3
+ // The life of a worker, as its status history tells it (RFC 0027, step 8).
4
+ //
5
+ // Two readings of the same journal, stacked:
6
+ //
7
+ // - a **strip** — one segment per stay, width proportional to how long it lasted. It answers
8
+ // "what did this worker mostly do" at a glance: a strip that is all green is a healthy worker, a
9
+ // strip with a grey tail is one that died, a strip striped yellow/grey is a node that flaps.
10
+ // - a **list**, newest first — what each stay was, how long it lasted, who wrote it, and why when
11
+ // the control plane said why ("Ran out of memory (exit 137)"). This is the "why did it die on
12
+ // Tuesday" answer, without the logs of a container that no longer exists.
13
+ //
14
+ // Durations are *derived*, never sent: a stay lasts until the next transition, and the current one
15
+ // lasts until `now`. Same rule as `SetupWaterfall` ("the next milestone minus this one"), for the
16
+ // whole lifecycle instead of the setup.
17
+ //
18
+ // Colours and labels come from `workerStatusConfig`/`resolveWorkerStatus` — the history does not
19
+ // get its own palette, so a stay in the strip is the colour the pill was while it lasted.
20
+
21
+ import { useState } from "react"
22
+ import { ChevronDownIcon } from "lucide-react"
23
+
24
+ import { cn, formatElapsed, formatShortDate } from "../../utils"
25
+ import type { WorkerStatusEvent } from "./types"
26
+ import { WorkerStatusDot, resolveWorkerStatus, type WorkerStatus } from "./worker-status"
27
+
28
+ /** Who wrote a transition, phrased. An unknown source is shown as sent rather than dropped. */
29
+ const SOURCE_LABELS: Record<string, string> = {
30
+ spawn: "spawn",
31
+ "agent-event": "agent",
32
+ reconciliation: "reconciliation",
33
+ command: "user action",
34
+ "setup-monitor": "setup monitor",
35
+ "boot-recovery": "API restart",
36
+ "node-disconnect": "node disconnect",
37
+ }
38
+
39
+ /**
40
+ * Labels the status table can't give. In a pill, `provisioning` and `starting` both read
41
+ * "Setting up…" because a pill only says *that* something is in flight; in a history they are two
42
+ * consecutive rows, and two identical labels in a row read like a duplicate.
43
+ */
44
+ const HISTORY_LABELS: Record<string, string> = {
45
+ provisioning: "Provisioning",
46
+ }
47
+
48
+ export interface WorkerStatusSpan {
49
+ event: WorkerStatusEvent
50
+ status: WorkerStatus
51
+ /** Label for a stay — the pill's, minus the trailing "…" once the stay is over. */
52
+ label: string
53
+ start: number
54
+ end: number
55
+ duration: number
56
+ /** The last span: the worker is still in it, and `end` is `now`. */
57
+ current: boolean
58
+ }
59
+
60
+ /**
61
+ * Journal → stays, oldest first. Pure, so the dashboard can derive the same numbers without
62
+ * drawing anything (a "time to ready" metric, a test).
63
+ *
64
+ * Tolerant like the rest of the package: rows are ordered by `seq` then `at` whatever order they
65
+ * arrive in, a row with an unparsable `at` is skipped rather than producing `NaN` widths, and a
66
+ * clock that went backwards yields a zero-length stay, not a negative one.
67
+ */
68
+ export function statusHistorySpans(events: WorkerStatusEvent[], now: number = Date.now()): WorkerStatusSpan[] {
69
+ const rows = events
70
+ .map((event) => ({ event, start: Date.parse(event.at) }))
71
+ .filter((r) => Number.isFinite(r.start))
72
+ .sort((a, b) => (a.event.seq ?? 0) - (b.event.seq ?? 0) || a.start - b.start)
73
+
74
+ return rows.map(({ event, start }, i) => {
75
+ const current = i === rows.length - 1
76
+ const end = current ? Math.max(start, now) : rows[i + 1].start
77
+ const resolved = resolveWorkerStatus({ id: "", state: event.to, statusMeta: event.meta })
78
+ // Only the stay the worker is *still* in may pulse — a green dot that pings on a row from last
79
+ // Tuesday would claim a liveness nobody observed.
80
+ const status = current ? resolved : { ...resolved, running: false }
81
+ const label = HISTORY_LABELS[event.to] ?? (current ? status.label : status.label.replace(/…$/, ""))
82
+ return { event, status, label, start, end, duration: Math.max(0, end - start), current }
83
+ })
84
+ }
85
+
86
+ export interface WorkerStatusHistoryProps {
87
+ /** The journal, in any order. Empty → nothing is drawn. */
88
+ events: WorkerStatusEvent[]
89
+ /** Where the current stay ends. The component doesn't tick: the app re-renders on its poll. */
90
+ now?: number | Date
91
+ /** Rows shown before "Show N earlier". The strip always covers everything. */
92
+ limit?: number
93
+ className?: string
94
+ }
95
+
96
+ export function WorkerStatusHistory({ events, now, limit = 5, className }: WorkerStatusHistoryProps) {
97
+ const [expanded, setExpanded] = useState(false)
98
+ const nowMs = now instanceof Date ? now.getTime() : (now ?? Date.now())
99
+ const spans = statusHistorySpans(events, nowMs)
100
+ if (!spans.length) return null
101
+
102
+ const total = spans.reduce((sum, s) => sum + s.duration, 0)
103
+ const newestFirst = [...spans].reverse()
104
+ const hidden = expanded ? 0 : Math.max(0, newestFirst.length - limit)
105
+ const rows = hidden ? newestFirst.slice(0, limit) : newestFirst
106
+
107
+ return (
108
+ <div data-slot="worker-status-history" className={cn("rounded-lg border border-border/40 bg-muted/20 text-xs", className)}>
109
+ <div className="space-y-1.5 border-b border-border/40 px-3 py-2.5">
110
+ <div className="flex items-center justify-between">
111
+ <span className="font-mono text-[11px] font-medium text-muted-foreground">
112
+ {spans.length} {spans.length === 1 ? "state" : "states"}
113
+ </span>
114
+ <span className="font-mono text-[11px] tabular-nums text-foreground/80">{formatElapsed(total)}</span>
115
+ </div>
116
+ {/* flex-grow by duration, with a floor: a 0.8 s provisioning next to three days of Running
117
+ must still be a visible notch, or the strip would lie about what happened. */}
118
+ <div className="flex h-2.5 gap-px overflow-hidden rounded-sm" role="img" aria-label="Time spent in each state">
119
+ {spans.map((s, i) => (
120
+ <span
121
+ key={i}
122
+ className={cn("min-w-[3px] basis-0", s.status.dotClass)}
123
+ style={{ flexGrow: total > 0 ? s.duration : 1 }}
124
+ title={`${s.label}: ${formatElapsed(s.duration)}`}
125
+ />
126
+ ))}
127
+ </div>
128
+ </div>
129
+
130
+ <ol className="px-3 py-2.5">
131
+ {rows.map((s, i) => {
132
+ const source = s.event.source ? (SOURCE_LABELS[s.event.source] ?? s.event.source) : null
133
+ const last = i === rows.length - 1 && !hidden
134
+ return (
135
+ <li key={`${s.event.seq ?? ""}-${s.event.at}`} className="relative pb-2.5 pl-4 last:pb-0">
136
+ {/* The thread between two dots. Absent under the oldest row shown: nothing before it. */}
137
+ {!last && <span aria-hidden className="absolute top-3 bottom-0 left-[3px] w-px bg-border" />}
138
+ <WorkerStatusDot status={s.status} className="absolute top-[5px] left-0" />
139
+ <div className="flex items-baseline justify-between gap-2">
140
+ <span className={cn("truncate font-medium", s.current ? "text-foreground" : "text-foreground/80")}>{s.label}</span>
141
+ <span className="shrink-0 font-mono text-[11px] tabular-nums text-muted-foreground/80">{formatElapsed(s.duration)}</span>
142
+ </div>
143
+ {s.status.detail && <p className="mt-0.5 text-[11px] leading-snug text-foreground/70">{s.status.detail}</p>}
144
+ <p className="mt-0.5 truncate text-[10px] text-muted-foreground/60">
145
+ {s.current ? "since " : ""}
146
+ {formatShortDate(s.start, { time: true })}
147
+ {source && <> · {source}</>}
148
+ </p>
149
+ </li>
150
+ )
151
+ })}
152
+ </ol>
153
+
154
+ {hidden > 0 && (
155
+ <button
156
+ type="button"
157
+ onClick={() => setExpanded(true)}
158
+ className="flex w-full cursor-pointer items-center justify-center gap-1 border-t border-border/40 py-1.5 text-[11px] text-muted-foreground transition-colors hover:text-foreground"
159
+ >
160
+ <ChevronDownIcon className="h-3 w-3" />
161
+ Show {hidden} earlier
162
+ </button>
163
+ )}
164
+ </div>
165
+ )
166
+ }
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  // @spunto/design-system — shared UI primitives, helpers, and color constants.
2
2
  // Tokens ship as a CSS file: `import "@spunto/design-system/styles.css"`.
3
3
 
4
- export { cn, formatDuration, formatRelativeTime } from "./utils"
4
+ export { cn, formatDuration, formatElapsed, formatRelativeTime } from "./utils"
5
5
  export * from "./colors"
6
6
 
7
7
  // Domain (Spunto-specific) components live behind their own entry point —
package/src/utils.ts CHANGED
@@ -50,3 +50,26 @@ export function formatDuration(ms: number): string {
50
50
  if (ms >= 1000) return `${(ms / 1000).toFixed(ms >= 10_000 ? 0 : 1)}s`
51
51
  return `${Math.max(0, Math.round(ms))}ms`
52
52
  }
53
+
54
+ /**
55
+ * A span that may last days: "40s", "4m 12s", "11m", "3h 12m", "3d 4h".
56
+ *
57
+ * `formatDuration` is for a setup phase and stops at seconds ("11520s"); this one is for how long
58
+ * a worker *stayed* somewhere, where the range is a second to a week. Two units at most, and the
59
+ * second is dropped once it stops carrying information (the seconds of an 11-minute build).
60
+ */
61
+ export function formatElapsed(ms: number): string {
62
+ if (ms < 10_000) return formatDuration(ms)
63
+ // Rounded once, to the second, before splitting: 59.8 s is "1m", not "0m 59s" nor "60s".
64
+ const totalSeconds = Math.round(ms / 1000)
65
+ if (totalSeconds < 60) return `${totalSeconds}s`
66
+ const minutes = Math.floor(totalSeconds / 60)
67
+ if (minutes < 60) {
68
+ const seconds = totalSeconds % 60
69
+ return minutes < 10 && seconds ? `${minutes}m ${seconds}s` : `${minutes}m`
70
+ }
71
+ const hours = Math.floor(minutes / 60)
72
+ if (hours < 24) return minutes % 60 ? `${hours}h ${minutes % 60}m` : `${hours}h`
73
+ const days = Math.floor(hours / 24)
74
+ return hours % 24 ? `${days}d ${hours % 24}h` : `${days}d`
75
+ }