@niwa-labs/ui-kit 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +29 -2
- package/package.json +31 -4
- package/src/agent/diff-view.tsx +62 -0
- package/src/agent/execution-track.tsx +201 -0
- package/src/agent/guard.ts +121 -0
- package/src/agent/guarded-action.tsx +272 -0
- package/src/agent/id-chip.tsx +102 -0
- package/src/agent/markdown.tsx +34 -0
- package/src/agent/payload.ts +42 -0
- package/src/agent/phase-plan.tsx +183 -0
- package/src/agent/plan-impact.ts +104 -0
- package/src/agent/state.tsx +75 -0
- package/src/agent/tool-call.tsx +106 -0
- package/src/agent/transcript.tsx +88 -0
- package/src/execution-track.css +327 -0
- package/src/index.ts +76 -0
- package/src/lib/cn.ts +19 -0
- package/src/page-layout.css +183 -0
- package/src/theme.css +136 -0
- package/src/ui/badge.tsx +32 -0
- package/src/ui/button.tsx +38 -0
- package/src/ui/collapsible.tsx +13 -0
- package/src/ui/data-value.tsx +211 -0
- package/src/ui/message.tsx +81 -0
- package/src/ui/page-layout.tsx +33 -0
- package/src/ui/schema-view.tsx +144 -0
- package/src/ui/spinner.tsx +16 -0
- package/src/ui/tabs.tsx +63 -0
- package/src/ui/tooltip.tsx +50 -0
- package/src/ui/trace.tsx +100 -0
- package/test/browser.test.ts +13 -0
- package/test/diff-view.test.tsx +50 -0
- package/test/guard.test.ts +113 -0
- package/test/guarded-action.test.tsx +229 -0
- package/test/helpers.test.ts +83 -0
- package/test/phase-plan.test.tsx +146 -0
- package/test/plan.test.ts +71 -0
- package/test/render.test.tsx +62 -0
- package/test/structured-values.test.tsx +31 -0
- package/test/tabs.test.tsx +90 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A small icon control for an action that cannot be taken back, with soft protection in place:
|
|
3
|
+
* the first press says what will be affected, a second press or a held press confirms, and a
|
|
4
|
+
* confirmed action can be undone for a few seconds before it is fired. See `guard.ts`.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { UndoIcon } from "lucide-react";
|
|
8
|
+
import { useEffect, useReducer, useRef, useState, type ReactNode } from "react";
|
|
9
|
+
|
|
10
|
+
import { cn } from "../lib/cn.ts";
|
|
11
|
+
import { buttonVariants } from "../ui/button.tsx";
|
|
12
|
+
import { Tooltip, TooltipContent, TooltipTrigger } from "../ui/tooltip.tsx";
|
|
13
|
+
import {
|
|
14
|
+
DEFAULT_GUARD_TIMING,
|
|
15
|
+
firesOnUnmount,
|
|
16
|
+
guardReducer,
|
|
17
|
+
IDLE,
|
|
18
|
+
nextDeadline,
|
|
19
|
+
type GuardState,
|
|
20
|
+
type GuardTiming,
|
|
21
|
+
} from "./guard.ts";
|
|
22
|
+
|
|
23
|
+
export type GuardedActionOptions = {
|
|
24
|
+
/** Called once, when the undo window of a confirmed press closes. */
|
|
25
|
+
onConfirm: () => void;
|
|
26
|
+
timing?: Partial<GuardTiming> | undefined;
|
|
27
|
+
disabled?: boolean | undefined;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type GuardedAction = {
|
|
31
|
+
state: GuardState;
|
|
32
|
+
/** The current time, refreshed while a countdown is shown. */
|
|
33
|
+
now: number;
|
|
34
|
+
timing: GuardTiming;
|
|
35
|
+
/** Props for the button that arms and confirms. */
|
|
36
|
+
buttonProps: {
|
|
37
|
+
disabled: boolean | undefined;
|
|
38
|
+
onPointerDown: (event: { button: number }) => void;
|
|
39
|
+
onPointerUp: () => void;
|
|
40
|
+
onPointerLeave: () => void;
|
|
41
|
+
onPointerCancel: () => void;
|
|
42
|
+
onClick: (event: { detail: number }) => void;
|
|
43
|
+
onKeyDown: (event: { key: string; repeat: boolean; preventDefault(): void }) => void;
|
|
44
|
+
onBlur: () => void;
|
|
45
|
+
};
|
|
46
|
+
/** Back to idle without firing: the undo of a confirmed press. */
|
|
47
|
+
cancel: () => void;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/** Drives the guard with the real clock. */
|
|
51
|
+
export function useGuardedAction({ onConfirm, timing: timingOverride, disabled }: GuardedActionOptions): GuardedAction {
|
|
52
|
+
const timing: GuardTiming = { ...DEFAULT_GUARD_TIMING, ...timingOverride };
|
|
53
|
+
const [state, dispatch] = useReducer(
|
|
54
|
+
(current: GuardState, event: Parameters<typeof guardReducer>[1]) => guardReducer(current, event, timing),
|
|
55
|
+
IDLE,
|
|
56
|
+
);
|
|
57
|
+
const [now, setNow] = useState(() => Date.now());
|
|
58
|
+
|
|
59
|
+
const onConfirmRef = useRef(onConfirm);
|
|
60
|
+
// The handler of the render that confirmed: what fires is what the owner confirmed, even when the
|
|
61
|
+
// control is given another target while the undo window is open.
|
|
62
|
+
const confirmedRef = useRef<(() => void) | null>(null);
|
|
63
|
+
const stateRef = useRef(state);
|
|
64
|
+
useEffect(() => {
|
|
65
|
+
onConfirmRef.current = onConfirm;
|
|
66
|
+
stateRef.current = state;
|
|
67
|
+
if (state.phase === "pending") confirmedRef.current ??= onConfirm;
|
|
68
|
+
else if (state.phase !== "fired") confirmedRef.current = null;
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// Unavailable controls cannot start or complete a gesture; one that becomes unavailable halfway
|
|
72
|
+
// through drops its unconfirmed gesture. A confirmed action stays undoable and is still fired.
|
|
73
|
+
const isDisabled = disabled === true && state.phase !== "pending";
|
|
74
|
+
const gestureOpen = state.phase === "armed" || state.phase === "holding";
|
|
75
|
+
useEffect(() => {
|
|
76
|
+
if (isDisabled && gestureOpen) dispatch({ type: "cancel" });
|
|
77
|
+
}, [isDisabled, gestureOpen]);
|
|
78
|
+
|
|
79
|
+
const fire = () => {
|
|
80
|
+
const handler = confirmedRef.current ?? onConfirmRef.current;
|
|
81
|
+
confirmedRef.current = null;
|
|
82
|
+
handler();
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// Wake up at the next deadline, and refresh the countdown while one is shown.
|
|
86
|
+
const deadline = nextDeadline(state, timing);
|
|
87
|
+
useEffect(() => {
|
|
88
|
+
if (deadline === null) return;
|
|
89
|
+
// A timer may fire a little early; the tick counts as the deadline so the phase always moves on.
|
|
90
|
+
const wake = setTimeout(
|
|
91
|
+
() => dispatch({ type: "tick", now: Math.max(Date.now(), deadline) }),
|
|
92
|
+
Math.max(0, deadline - Date.now()),
|
|
93
|
+
);
|
|
94
|
+
const refresh = setInterval(() => setNow(Date.now()), 250);
|
|
95
|
+
setNow(Date.now());
|
|
96
|
+
return () => {
|
|
97
|
+
clearTimeout(wake);
|
|
98
|
+
clearInterval(refresh);
|
|
99
|
+
};
|
|
100
|
+
}, [deadline]);
|
|
101
|
+
|
|
102
|
+
useEffect(() => {
|
|
103
|
+
if (state.phase !== "fired") return;
|
|
104
|
+
stateRef.current = IDLE;
|
|
105
|
+
dispatch({ type: "reset" });
|
|
106
|
+
fire();
|
|
107
|
+
}, [state]);
|
|
108
|
+
|
|
109
|
+
// A confirmed action whose control goes away is still the owner's decision: fire it.
|
|
110
|
+
useEffect(
|
|
111
|
+
() => () => {
|
|
112
|
+
if (!firesOnUnmount(stateRef.current)) {
|
|
113
|
+
// Hidden with a gesture unfinished: its timers stop with the effects, and it must not be
|
|
114
|
+
// completed by a later wake-up when the control is shown again.
|
|
115
|
+
if (stateRef.current.phase === "armed" || stateRef.current.phase === "holding") {
|
|
116
|
+
stateRef.current = IDLE;
|
|
117
|
+
dispatch({ type: "cancel" });
|
|
118
|
+
}
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
// Hidden and shown again with its state kept, the control must not fire a second time.
|
|
122
|
+
stateRef.current = IDLE;
|
|
123
|
+
// Back to idle from either: a pending control, or one whose firing had not run yet.
|
|
124
|
+
dispatch({ type: "cancel" });
|
|
125
|
+
dispatch({ type: "reset" });
|
|
126
|
+
fire();
|
|
127
|
+
},
|
|
128
|
+
[],
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
state,
|
|
133
|
+
now,
|
|
134
|
+
timing,
|
|
135
|
+
cancel: () => dispatch({ type: "cancel" }),
|
|
136
|
+
buttonProps: {
|
|
137
|
+
disabled: isDisabled,
|
|
138
|
+
onPointerDown: (event) => {
|
|
139
|
+
if (!isDisabled && event.button === 0) dispatch({ type: "holdStart", now: Date.now() });
|
|
140
|
+
},
|
|
141
|
+
onPointerUp: () => {
|
|
142
|
+
if (!isDisabled) dispatch({ type: "holdEnd", now: Date.now() });
|
|
143
|
+
},
|
|
144
|
+
onPointerLeave: () => dispatch({ type: "holdCancel", now: Date.now() }),
|
|
145
|
+
onPointerCancel: () => dispatch({ type: "holdCancel", now: Date.now() }),
|
|
146
|
+
// Pointer presses are handled above; a click with no pointer behind it is a keyboard or an
|
|
147
|
+
// assistive technology activating the button.
|
|
148
|
+
onClick: (event) => {
|
|
149
|
+
if (!isDisabled && event.detail === 0) dispatch({ type: "press", now: Date.now() });
|
|
150
|
+
},
|
|
151
|
+
onKeyDown: (event) => {
|
|
152
|
+
if (event.key === "Escape") dispatch({ type: "cancel" });
|
|
153
|
+
// A held Enter repeats clicks; it must not arm and confirm without a second press.
|
|
154
|
+
else if (event.repeat) event.preventDefault();
|
|
155
|
+
},
|
|
156
|
+
onBlur: () => {
|
|
157
|
+
if (stateRef.current.phase === "armed" || stateRef.current.phase === "holding") dispatch({ type: "cancel" });
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export type GuardButtonProps = {
|
|
164
|
+
guard: GuardedAction;
|
|
165
|
+
icon: ReactNode;
|
|
166
|
+
/** What the action does, for the tooltip and screen readers: "Recompute from here". */
|
|
167
|
+
label: string;
|
|
168
|
+
tone?: "default" | "destructive" | undefined;
|
|
169
|
+
className?: string | undefined;
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
/** The icon button of a guarded action, with a ring that fills while the press is held. */
|
|
173
|
+
export function GuardButton({ guard, icon, label, tone = "default", className }: GuardButtonProps) {
|
|
174
|
+
const { state, timing } = guard;
|
|
175
|
+
const { disabled, ...handlers } = guard.buttonProps;
|
|
176
|
+
const active = state.phase !== "idle";
|
|
177
|
+
return (
|
|
178
|
+
<Tooltip>
|
|
179
|
+
<TooltipTrigger
|
|
180
|
+
// The trigger's own `disabled` only mutes the hint; the button itself must be switched off.
|
|
181
|
+
render={<button type="button" disabled={disabled} />}
|
|
182
|
+
aria-label={label}
|
|
183
|
+
aria-pressed={active}
|
|
184
|
+
data-slot="guard-button"
|
|
185
|
+
data-phase={state.phase}
|
|
186
|
+
className={cn(
|
|
187
|
+
buttonVariants({ variant: "ghost", size: "icon-xs" }),
|
|
188
|
+
"relative text-muted-foreground",
|
|
189
|
+
active && (tone === "destructive" ? "bg-destructive/10 text-destructive" : "bg-muted text-foreground"),
|
|
190
|
+
className,
|
|
191
|
+
)}
|
|
192
|
+
{...handlers}
|
|
193
|
+
>
|
|
194
|
+
{icon}
|
|
195
|
+
<svg aria-hidden="true" viewBox="0 0 24 24" className="pointer-events-none absolute inset-0 size-full! -rotate-90">
|
|
196
|
+
<circle
|
|
197
|
+
cx="12"
|
|
198
|
+
cy="12"
|
|
199
|
+
r="11"
|
|
200
|
+
fill="none"
|
|
201
|
+
stroke="currentColor"
|
|
202
|
+
strokeWidth="1.5"
|
|
203
|
+
pathLength={1}
|
|
204
|
+
strokeDasharray="1"
|
|
205
|
+
strokeDashoffset={state.phase === "holding" ? 0 : 1}
|
|
206
|
+
style={{
|
|
207
|
+
transition: state.phase === "holding" ? `stroke-dashoffset ${timing.holdMs}ms linear` : "none",
|
|
208
|
+
opacity: state.phase === "holding" ? 1 : 0,
|
|
209
|
+
}}
|
|
210
|
+
/>
|
|
211
|
+
</svg>
|
|
212
|
+
</TooltipTrigger>
|
|
213
|
+
<TooltipContent>{label}</TooltipContent>
|
|
214
|
+
</Tooltip>
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export type GuardNoticeProps = {
|
|
219
|
+
guard: GuardedAction;
|
|
220
|
+
/** What will be affected, shown once the control is armed. */
|
|
221
|
+
consequence: ReactNode;
|
|
222
|
+
/** What is about to happen, shown during the undo window: "Recomputing", "Stopping". */
|
|
223
|
+
pending: string;
|
|
224
|
+
tone?: "default" | "destructive" | undefined;
|
|
225
|
+
className?: string | undefined;
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
function secondsLeft(until: number, now: number): number {
|
|
229
|
+
return Math.max(1, Math.ceil((until - now) / 1000));
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** The in-place explanation of an armed or confirmed guarded action; nothing while idle. */
|
|
233
|
+
export function GuardNotice({ guard, consequence, pending, tone = "default", className }: GuardNoticeProps) {
|
|
234
|
+
const { state, now } = guard;
|
|
235
|
+
if (state.phase === "idle" || state.phase === "fired") return null;
|
|
236
|
+
return (
|
|
237
|
+
<div
|
|
238
|
+
role="status"
|
|
239
|
+
aria-live="polite"
|
|
240
|
+
data-slot="guard-notice"
|
|
241
|
+
data-phase={state.phase}
|
|
242
|
+
className={cn(
|
|
243
|
+
"flex min-w-0 flex-wrap items-center gap-x-2 gap-y-1 rounded-md px-2 py-1 text-xs",
|
|
244
|
+
tone === "destructive" ? "bg-destructive/10 text-destructive" : "bg-muted text-foreground",
|
|
245
|
+
className,
|
|
246
|
+
)}
|
|
247
|
+
>
|
|
248
|
+
{state.phase === "pending" ? (
|
|
249
|
+
<>
|
|
250
|
+
<span>
|
|
251
|
+
{pending} in {secondsLeft(state.until, now)}s
|
|
252
|
+
</span>
|
|
253
|
+
<button
|
|
254
|
+
type="button"
|
|
255
|
+
className="inline-flex items-center gap-1 font-medium underline-offset-2 hover:underline [&_svg]:size-3"
|
|
256
|
+
onClick={guard.cancel}
|
|
257
|
+
>
|
|
258
|
+
<UndoIcon />
|
|
259
|
+
Undo
|
|
260
|
+
</button>
|
|
261
|
+
</>
|
|
262
|
+
) : (
|
|
263
|
+
<>
|
|
264
|
+
<span className="min-w-0">{consequence}</span>
|
|
265
|
+
<span className="text-muted-foreground">
|
|
266
|
+
{state.phase === "holding" ? "Keep holding…" : "Press again or hold to confirm · Esc to cancel"}
|
|
267
|
+
</span>
|
|
268
|
+
</>
|
|
269
|
+
)}
|
|
270
|
+
</div>
|
|
271
|
+
);
|
|
272
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An id as a chip: monospace, short, copyable, and a link when the caller knows where the id
|
|
3
|
+
* leads. The chip does not decide the target; the cockpit resolves ids to pages and passes the
|
|
4
|
+
* link in.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { CheckIcon, CopyIcon } from "lucide-react";
|
|
8
|
+
import { useEffect, useState, type ComponentProps, type MouseEvent } from "react";
|
|
9
|
+
|
|
10
|
+
import { cn } from "../lib/cn.ts";
|
|
11
|
+
|
|
12
|
+
/** The prefix of an id such as `run-1f3a9c`, or undefined when the id has none. */
|
|
13
|
+
export function idPrefix(id: string): string | undefined {
|
|
14
|
+
const match = /^([a-z][a-z0-9]*)-[0-9a-z]/.exec(id);
|
|
15
|
+
return match?.[1];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** The id cut to its prefix and the last characters, for places with little room. */
|
|
19
|
+
export function shortId(id: string, keep = 8): string {
|
|
20
|
+
const prefix = idPrefix(id);
|
|
21
|
+
const rest = prefix === undefined ? id : id.slice(prefix.length + 1);
|
|
22
|
+
if (rest.length <= keep + 1) return id;
|
|
23
|
+
const tail = rest.slice(-keep);
|
|
24
|
+
return prefix === undefined ? `…${tail}` : `${prefix}-…${tail}`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type IdChipProps = Omit<ComponentProps<"span">, "children" | "onClick"> & {
|
|
28
|
+
id: string;
|
|
29
|
+
/** Where the id leads. Without it the chip is plain text. */
|
|
30
|
+
href?: string | undefined;
|
|
31
|
+
/** Called on a plain click of the link, to navigate inside the app instead of loading the page. */
|
|
32
|
+
onNavigate?: ((event: MouseEvent<HTMLAnchorElement>) => void) | undefined;
|
|
33
|
+
/** A label shown in place of the id; the id stays in the tooltip and is what gets copied. */
|
|
34
|
+
label?: string | undefined;
|
|
35
|
+
/** Shorten long ids. */
|
|
36
|
+
short?: boolean | undefined;
|
|
37
|
+
/** Show a copy button. */
|
|
38
|
+
copyable?: boolean | undefined;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/** A plain left click without modifiers: one the app may handle itself. */
|
|
42
|
+
export function isPlainClick(event: Pick<MouseEvent, "button" | "metaKey" | "ctrlKey" | "shiftKey" | "altKey" | "defaultPrevented">): boolean {
|
|
43
|
+
return !event.defaultPrevented && event.button === 0 && !event.metaKey && !event.ctrlKey && !event.shiftKey && !event.altKey;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function IdChip({ id, href, onNavigate, label, short = false, copyable = true, className, ...props }: IdChipProps) {
|
|
47
|
+
const [copied, setCopied] = useState(false);
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
if (!copied) return;
|
|
50
|
+
const timer = setTimeout(() => setCopied(false), 1200);
|
|
51
|
+
return () => clearTimeout(timer);
|
|
52
|
+
}, [copied]);
|
|
53
|
+
|
|
54
|
+
const text = label ?? (short ? shortId(id) : id);
|
|
55
|
+
const body =
|
|
56
|
+
href === undefined ? (
|
|
57
|
+
<span className="truncate">{text}</span>
|
|
58
|
+
) : (
|
|
59
|
+
<a
|
|
60
|
+
href={href}
|
|
61
|
+
className="truncate underline-offset-2 hover:underline"
|
|
62
|
+
onClick={(event) => {
|
|
63
|
+
if (onNavigate !== undefined && isPlainClick(event)) {
|
|
64
|
+
event.preventDefault();
|
|
65
|
+
onNavigate(event);
|
|
66
|
+
}
|
|
67
|
+
}}
|
|
68
|
+
>
|
|
69
|
+
{text}
|
|
70
|
+
</a>
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
return (
|
|
74
|
+
<span
|
|
75
|
+
data-slot="id-chip"
|
|
76
|
+
data-prefix={idPrefix(id)}
|
|
77
|
+
title={id}
|
|
78
|
+
className={cn(
|
|
79
|
+
"group/id-chip inline-flex h-5 max-w-full min-w-0 items-center gap-1 rounded-md bg-muted px-1.5 font-mono text-xs text-foreground/80",
|
|
80
|
+
className,
|
|
81
|
+
)}
|
|
82
|
+
{...props}
|
|
83
|
+
>
|
|
84
|
+
{body}
|
|
85
|
+
{copyable ? (
|
|
86
|
+
<button
|
|
87
|
+
type="button"
|
|
88
|
+
aria-label={`Copy ${id}`}
|
|
89
|
+
className="inline-flex shrink-0 text-muted-foreground opacity-0 group-hover/id-chip:opacity-100 hover:text-foreground focus-visible:opacity-100 [&_svg]:size-3"
|
|
90
|
+
onClick={() => {
|
|
91
|
+
void navigator.clipboard?.writeText(id).then(
|
|
92
|
+
() => setCopied(true),
|
|
93
|
+
() => {},
|
|
94
|
+
);
|
|
95
|
+
}}
|
|
96
|
+
>
|
|
97
|
+
{copied ? <CheckIcon /> : <CopyIcon />}
|
|
98
|
+
</button>
|
|
99
|
+
) : null}
|
|
100
|
+
</span>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Markdown that may still be arriving. While a reply streams, unfinished bold, links and code
|
|
3
|
+
* fences are closed for display instead of flashing as raw characters; code is highlighted.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { code } from "@streamdown/code";
|
|
7
|
+
import { memo } from "react";
|
|
8
|
+
import { Streamdown } from "streamdown";
|
|
9
|
+
|
|
10
|
+
import { cn } from "../lib/cn.ts";
|
|
11
|
+
|
|
12
|
+
export type MarkdownProps = {
|
|
13
|
+
/** The text so far. */
|
|
14
|
+
text: string;
|
|
15
|
+
/** More text is coming. */
|
|
16
|
+
streaming?: boolean | undefined;
|
|
17
|
+
className?: string | undefined;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const PLUGINS = { code };
|
|
21
|
+
|
|
22
|
+
export const Markdown = memo(function Markdown({ text, streaming = false, className }: MarkdownProps) {
|
|
23
|
+
return (
|
|
24
|
+
<Streamdown
|
|
25
|
+
className={cn("min-w-0 text-sm leading-relaxed [&>*:first-child]:mt-0 [&>*:last-child]:mb-0", className)}
|
|
26
|
+
mode={streaming ? "streaming" : "static"}
|
|
27
|
+
isAnimating={streaming}
|
|
28
|
+
plugins={PLUGINS}
|
|
29
|
+
shikiTheme={["github-light", "github-dark"]}
|
|
30
|
+
>
|
|
31
|
+
{text}
|
|
32
|
+
</Streamdown>
|
|
33
|
+
);
|
|
34
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** The text of a tool's input or output: strings as they are, anything else as indented JSON. */
|
|
2
|
+
export function formatPayload(value: unknown): string {
|
|
3
|
+
if (value === undefined) return "";
|
|
4
|
+
if (typeof value === "string") return value;
|
|
5
|
+
try {
|
|
6
|
+
return JSON.stringify(value, null, 2) ?? String(value);
|
|
7
|
+
} catch {
|
|
8
|
+
return String(value);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** A duration in milliseconds, short: `850ms`, `4.2s`, `3m 05s`, `1h 02m`. */
|
|
13
|
+
export function formatDuration(ms: number): string {
|
|
14
|
+
if (!Number.isFinite(ms) || ms < 0) return "";
|
|
15
|
+
if (ms < 1000) return `${Math.round(ms)}ms`;
|
|
16
|
+
const seconds = ms / 1000;
|
|
17
|
+
if (seconds < 60) return `${seconds < 10 ? seconds.toFixed(1) : Math.round(seconds)}s`;
|
|
18
|
+
const totalSeconds = Math.floor(seconds);
|
|
19
|
+
const minutes = Math.floor(totalSeconds / 60);
|
|
20
|
+
if (minutes < 60) return `${minutes}m ${String(totalSeconds % 60).padStart(2, "0")}s`;
|
|
21
|
+
return `${Math.floor(minutes / 60)}h ${String(minutes % 60).padStart(2, "0")}m`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* How long ago a moment was, short: `just now`, `5m ago`, `3h ago`, `2d ago`, and past a month the date. The moment is
|
|
26
|
+
* milliseconds since the epoch or an ISO time; a moment that is not one gives an empty text.
|
|
27
|
+
*/
|
|
28
|
+
export function formatAgo(at: number | string, now: number = Date.now()): string {
|
|
29
|
+
const moment = typeof at === "number" ? at : Date.parse(at);
|
|
30
|
+
// A number can be finite and still outside the range a date holds.
|
|
31
|
+
if (Number.isNaN(new Date(moment).getTime())) return "";
|
|
32
|
+
const seconds = Math.round((now - moment) / 1000);
|
|
33
|
+
if (seconds < 0) return "in the future";
|
|
34
|
+
if (seconds < 45) return "just now";
|
|
35
|
+
const minutes = Math.round(seconds / 60);
|
|
36
|
+
if (minutes < 60) return `${String(minutes)}m ago`;
|
|
37
|
+
const hours = Math.round(minutes / 60);
|
|
38
|
+
if (hours < 24) return `${String(hours)}h ago`;
|
|
39
|
+
const days = Math.round(hours / 24);
|
|
40
|
+
if (days < 31) return `${String(days)}d ago`;
|
|
41
|
+
return new Date(moment).toISOString().slice(0, 10);
|
|
42
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The plan of a run: its phases, the steps in each, and how far it has got. Settled phases and
|
|
3
|
+
* steps carry a small recompute control, running ones a stop control; both ask in place before
|
|
4
|
+
* they act (see `guarded-action.tsx`).
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { RotateCcwIcon, SquareIcon } from "lucide-react";
|
|
8
|
+
import { useState, type ReactNode } from "react";
|
|
9
|
+
|
|
10
|
+
import { cn } from "../lib/cn.ts";
|
|
11
|
+
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "../ui/collapsible.tsx";
|
|
12
|
+
import { GuardButton, GuardNotice, useGuardedAction } from "./guarded-action.tsx";
|
|
13
|
+
import type { GuardState, GuardTiming } from "./guard.ts";
|
|
14
|
+
import {
|
|
15
|
+
describeRecompute,
|
|
16
|
+
describeStop,
|
|
17
|
+
type PlanPhase as PlanPhaseShape,
|
|
18
|
+
type PlanStep as PlanStepShape,
|
|
19
|
+
type PlanTarget,
|
|
20
|
+
} from "./plan-impact.ts";
|
|
21
|
+
import { isSettled, StateRing, type WorkState } from "./state.tsx";
|
|
22
|
+
|
|
23
|
+
export type PhasePlanStep = PlanStepShape & {
|
|
24
|
+
/** Short facts on the right of the row: a duration, a cost, "reused from v3". */
|
|
25
|
+
meta?: ReactNode;
|
|
26
|
+
/** Shown under the step, e.g. its last output line or a link to its session. */
|
|
27
|
+
detail?: ReactNode;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type PhasePlanPhase = PlanPhaseShape<PhasePlanStep> & { meta?: ReactNode };
|
|
31
|
+
|
|
32
|
+
export type PhasePlanProps = {
|
|
33
|
+
phases: readonly PhasePlanPhase[];
|
|
34
|
+
/** Recompute from a phase or a step. Without it no recompute control is shown. */
|
|
35
|
+
onRecompute?: ((target: PlanTarget) => void) | undefined;
|
|
36
|
+
/** Stop a phase or a step. Without it no stop control is shown. */
|
|
37
|
+
onStop?: ((target: PlanTarget) => void) | undefined;
|
|
38
|
+
/** Whether a target may be recomputed; every settled target by default. */
|
|
39
|
+
canRecompute?: ((target: PlanTarget) => boolean) | undefined;
|
|
40
|
+
/** Whether a target may be stopped; every running or waiting target by default. */
|
|
41
|
+
canStop?: ((target: PlanTarget) => boolean) | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* The explanation shown before a recompute. By default it is worked out from the plan alone, which
|
|
44
|
+
* cannot tell whether a step that already acted outside is repeated; a host that can forecast it
|
|
45
|
+
* for the chosen target passes the forecast here. Called only while the notice is shown.
|
|
46
|
+
*/
|
|
47
|
+
explainRecompute?: ((target: PlanTarget) => ReactNode) | undefined;
|
|
48
|
+
/** The explanation shown before a stop; worked out from the plan by default. Called only while shown. */
|
|
49
|
+
explainStop?: ((target: PlanTarget) => ReactNode) | undefined;
|
|
50
|
+
timing?: Partial<GuardTiming> | undefined;
|
|
51
|
+
className?: string | undefined;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
type GuardPhase = GuardState["phase"];
|
|
55
|
+
|
|
56
|
+
function isActive(state: WorkState): boolean {
|
|
57
|
+
return state === "running" || state === "waiting";
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
type Controls = Pick<
|
|
61
|
+
PhasePlanProps,
|
|
62
|
+
"onRecompute" | "onStop" | "canRecompute" | "canStop" | "explainRecompute" | "explainStop" | "timing"
|
|
63
|
+
> & { phases: readonly PhasePlanPhase[] };
|
|
64
|
+
|
|
65
|
+
function RowControls({ target, state, controls, children }: { target: PlanTarget; state: WorkState; controls: Controls; children: ReactNode }) {
|
|
66
|
+
const { onRecompute, onStop } = controls;
|
|
67
|
+
const recomputable =
|
|
68
|
+
onRecompute !== undefined && isSettled(state) && state !== "skipped" && (controls.canRecompute?.(target) ?? true);
|
|
69
|
+
const stoppable = onStop !== undefined && isActive(state) && (controls.canStop?.(target) ?? true);
|
|
70
|
+
|
|
71
|
+
const recompute = useGuardedAction({ onConfirm: () => onRecompute?.(target), timing: controls.timing, disabled: !recomputable });
|
|
72
|
+
const stop = useGuardedAction({ onConfirm: () => onStop?.(target), timing: controls.timing, disabled: !stoppable });
|
|
73
|
+
const engaged = recompute.state.phase !== "idle" || stop.state.phase !== "idle";
|
|
74
|
+
// Only an armed or held control shows its explanation, so only then is it worked out: the default
|
|
75
|
+
// one reads the whole plan.
|
|
76
|
+
const explains = (phase: GuardPhase) => phase === "armed" || phase === "holding";
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
<div className="flex min-w-0 flex-col gap-1">
|
|
80
|
+
<div className="group/plan-row flex min-w-0 items-center gap-2">
|
|
81
|
+
{children}
|
|
82
|
+
<div
|
|
83
|
+
className={cn(
|
|
84
|
+
"ml-auto flex shrink-0 items-center gap-0.5 transition-opacity",
|
|
85
|
+
engaged ? "opacity-100" : "opacity-60 group-hover/plan-row:opacity-100 focus-within:opacity-100",
|
|
86
|
+
)}
|
|
87
|
+
>
|
|
88
|
+
{recomputable || recompute.state.phase !== "idle" ? (
|
|
89
|
+
<GuardButton guard={recompute} icon={<RotateCcwIcon />} label="Recompute from here" />
|
|
90
|
+
) : null}
|
|
91
|
+
{stoppable || stop.state.phase !== "idle" ? (
|
|
92
|
+
<GuardButton guard={stop} icon={<SquareIcon />} label="Stop" tone="destructive" />
|
|
93
|
+
) : null}
|
|
94
|
+
</div>
|
|
95
|
+
</div>
|
|
96
|
+
<GuardNotice
|
|
97
|
+
guard={recompute}
|
|
98
|
+
pending="Recomputing"
|
|
99
|
+
consequence={
|
|
100
|
+
explains(recompute.state.phase)
|
|
101
|
+
? (controls.explainRecompute?.(target) ?? describeRecompute(controls.phases, target))
|
|
102
|
+
: null
|
|
103
|
+
}
|
|
104
|
+
/>
|
|
105
|
+
<GuardNotice
|
|
106
|
+
guard={stop}
|
|
107
|
+
pending="Stopping"
|
|
108
|
+
tone="destructive"
|
|
109
|
+
consequence={
|
|
110
|
+
explains(stop.state.phase) ? (controls.explainStop?.(target) ?? describeStop(controls.phases, target)) : null
|
|
111
|
+
}
|
|
112
|
+
/>
|
|
113
|
+
</div>
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function StepRow({ phase, step, controls }: { phase: string; step: PhasePlanStep; controls: Controls }) {
|
|
118
|
+
return (
|
|
119
|
+
<li data-slot="plan-step" data-state={step.state} className="flex min-w-0 flex-col gap-1">
|
|
120
|
+
<RowControls target={{ phase, step: step.id }} state={step.state} controls={controls}>
|
|
121
|
+
<StateRing state={step.state} className="size-2.5" />
|
|
122
|
+
<span
|
|
123
|
+
className={cn(
|
|
124
|
+
"min-w-0 truncate text-sm",
|
|
125
|
+
step.state === "running" && "text-foreground",
|
|
126
|
+
(step.state === "pending" || step.state === "skipped") && "text-muted-foreground",
|
|
127
|
+
step.state === "failed" && "text-destructive",
|
|
128
|
+
)}
|
|
129
|
+
>
|
|
130
|
+
{step.title}
|
|
131
|
+
</span>
|
|
132
|
+
{step.meta === undefined ? null : <span className="shrink-0 text-xs text-muted-foreground tabular-nums">{step.meta}</span>}
|
|
133
|
+
</RowControls>
|
|
134
|
+
{step.detail === undefined ? null : <div className="ml-4.5 min-w-0 text-xs text-muted-foreground">{step.detail}</div>}
|
|
135
|
+
</li>
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function PhaseRow({ phase, controls }: { phase: PhasePlanPhase; controls: Controls }) {
|
|
140
|
+
const done = phase.steps.filter((s) => s.state === "done" || s.state === "reused").length;
|
|
141
|
+
// Open while the phase is not settled, and when it failed; whoever opened or closed it decides from then on.
|
|
142
|
+
const [chosen, choose] = useState<boolean | undefined>(undefined);
|
|
143
|
+
return (
|
|
144
|
+
<Collapsible
|
|
145
|
+
data-slot="plan-phase"
|
|
146
|
+
data-state={phase.state}
|
|
147
|
+
open={chosen ?? (!isSettled(phase.state) || phase.state === "failed")}
|
|
148
|
+
onOpenChange={choose}
|
|
149
|
+
className="flex min-w-0 flex-col gap-1.5"
|
|
150
|
+
>
|
|
151
|
+
<RowControls target={{ phase: phase.id }} state={phase.state} controls={controls}>
|
|
152
|
+
<StateRing state={phase.state} />
|
|
153
|
+
<CollapsibleTrigger className="flex min-w-0 items-center gap-2 rounded-md text-left text-sm font-medium outline-none focus-visible:ring-3 focus-visible:ring-ring/50">
|
|
154
|
+
<span className="truncate">{phase.title}</span>
|
|
155
|
+
<span className="shrink-0 text-xs font-normal text-muted-foreground tabular-nums">
|
|
156
|
+
{done}/{phase.steps.length}
|
|
157
|
+
</span>
|
|
158
|
+
</CollapsibleTrigger>
|
|
159
|
+
{phase.meta === undefined ? null : <span className="shrink-0 text-xs text-muted-foreground tabular-nums">{phase.meta}</span>}
|
|
160
|
+
</RowControls>
|
|
161
|
+
<CollapsibleContent>
|
|
162
|
+
<ol className="ml-1.5 flex min-w-0 flex-col gap-1.5 border-l pl-3">
|
|
163
|
+
{phase.steps.map((step) => (
|
|
164
|
+
<StepRow key={step.id} phase={phase.id} step={step} controls={controls} />
|
|
165
|
+
))}
|
|
166
|
+
</ol>
|
|
167
|
+
</CollapsibleContent>
|
|
168
|
+
</Collapsible>
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export function PhasePlan({ phases, className, ...rest }: PhasePlanProps) {
|
|
173
|
+
const controls: Controls = { ...rest, phases };
|
|
174
|
+
return (
|
|
175
|
+
<ol data-slot="phase-plan" className={cn("flex min-w-0 flex-col gap-3", className)}>
|
|
176
|
+
{phases.map((phase) => (
|
|
177
|
+
<li key={phase.id}>
|
|
178
|
+
<PhaseRow phase={phase} controls={controls} />
|
|
179
|
+
</li>
|
|
180
|
+
))}
|
|
181
|
+
</ol>
|
|
182
|
+
);
|
|
183
|
+
}
|