@bridgebrew/sessions 0.0.0-stage → 0.0.8
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 +43 -2
- package/answers.ts +167 -0
- package/browser/branch-actions.tsx +166 -0
- package/browser/changes.tsx +415 -0
- package/browser/children.tsx +650 -0
- package/browser/conversation.tsx +2381 -0
- package/browser/decision-questions.tsx +191 -0
- package/browser/elapsed.tsx +44 -0
- package/browser/gallery.tsx +218 -0
- package/browser/index.tsx +3434 -0
- package/browser/media-origins.tsx +38 -0
- package/browser/mobile.tsx +663 -0
- package/browser/open-link.tsx +30 -0
- package/browser/pull-request.tsx +116 -0
- package/browser/questions.tsx +156 -0
- package/browser/session-scheduled.tsx +203 -0
- package/browser/subagent-view.tsx +136 -0
- package/browser/subagents.tsx +339 -0
- package/browser/tabs.tsx +228 -0
- package/browser/tsconfig.json +15 -0
- package/browser/views/view.tsx +1064 -0
- package/browser/views.tsx +166 -0
- package/contract.ts +287 -0
- package/delegations.ts +1975 -0
- package/diff.ts +88 -0
- package/docs/en/images.mdx +51 -0
- package/docs/en/index.mdx +22 -0
- package/docs/en/meta.json +4 -0
- package/docs/en/organize-sessions.mdx +53 -0
- package/docs/en/start-a-session.mdx +63 -0
- package/docs/meta.json +4 -0
- package/git.ts +223 -0
- package/listing.ts +382 -0
- package/package.json +1023 -4
- package/peers.ts +677 -0
- package/pull-request-watch.ts +282 -0
- package/pull-requests.ts +606 -0
- package/server.ts +5920 -0
- package/text.ts +19 -0
- package/tsconfig.json +3 -0
- package/viewer-activity.ts +57 -0
- package/views/examples.ts +153 -0
- package/views/library.ts +1116 -0
|
@@ -0,0 +1,2381 @@
|
|
|
1
|
+
// The conversation of a session, as research mockups session-ux-v1 (boards A, B, C, E): each turn is the user's message
|
|
2
|
+
// (a bubble on the right: its time, Copy and Reuse on hover; a long one folds), then the agent's work, then its answer.
|
|
3
|
+
// The work (tool calls, the agent's words between them, closed decisions, mode changes) is one quiet line per tool call,
|
|
4
|
+
// runs of the same kind grouped ("Read 3 files"), each opening in place on what it did (an edit's changed lines, a
|
|
5
|
+
// command's input and output, the image a read looked at, the screenshot or video a tool's output shows in Markdown). A turn that completed folds its work behind one line, "Worked for 2m 14s · 9 actions ·
|
|
6
|
+
// 2 files +18 −6", opened on a click; a running, interrupted or failed one stays open. The running turn says "Working
|
|
7
|
+
// for 0:42" and keeps its last thought in view. The answer (the agent's last words after its last tool call) is plain
|
|
8
|
+
// Markdown, its code blocks with Wrap and Copy; under it the files the turn changed (Review opens the session's
|
|
9
|
+
// changes) and Copy, its time and how long it took (always on the last turn, on hover before). Notices, messages
|
|
10
|
+
// between sessions, commits and unfinished turn ends stay in view.
|
|
11
|
+
// The timeline's rows: an open decision shows on the row of the tool call it asks about (or on its own row),
|
|
12
|
+
// its options there (bridge#194); a compaction is one working row, "Compacting for 12s", then a quiet "Compacted"
|
|
13
|
+
// (bridge#195); a scheduled task's prompt is a compact row naming its task, the prompt a click away (bridge#198), and a
|
|
14
|
+
// message Bridge itself sent the agent (`from: 'bridge'`) a quiet notice row naming Bridge, not the user's bubble; the
|
|
15
|
+
// running turn's working line names its newest tool call (bridge#199); a tool call's change counts open the session's
|
|
16
|
+
// changes on that file (bridge#200). Child sessions (bridge#212 S3, ./children.tsx): the children a turn delegated are
|
|
17
|
+
// a group of cards after its work, in view when it folds, their `delegate_session` rows hidden; Bridge's message with
|
|
18
|
+
// their results or questions is one block linking each child; a child's task (and a follow-up, a message from another
|
|
19
|
+
// session) shows without Bridge's block, under whom it came from; a child's question or result waiting in the queue
|
|
20
|
+
// names its child.
|
|
21
|
+
import * as React from 'react';
|
|
22
|
+
import Markdown, { defaultUrlTransform, type Components } from 'react-markdown';
|
|
23
|
+
import remarkGfm from 'remark-gfm';
|
|
24
|
+
import {
|
|
25
|
+
Button,
|
|
26
|
+
Icon,
|
|
27
|
+
ScreenLink,
|
|
28
|
+
Tooltip,
|
|
29
|
+
TooltipContent,
|
|
30
|
+
TooltipTrigger,
|
|
31
|
+
cn,
|
|
32
|
+
copyText,
|
|
33
|
+
type ScreenLinkProps,
|
|
34
|
+
formatDate,
|
|
35
|
+
} from '@bridge/ui';
|
|
36
|
+
import {
|
|
37
|
+
AttachedImages,
|
|
38
|
+
MarkdownMedia,
|
|
39
|
+
MediaOrigins,
|
|
40
|
+
MediaWorkspace,
|
|
41
|
+
markdownImages,
|
|
42
|
+
mediaUrl,
|
|
43
|
+
outputMedia,
|
|
44
|
+
viewedImage,
|
|
45
|
+
type Attached,
|
|
46
|
+
} from '@bridge/session-ui/media';
|
|
47
|
+
import { oneLine } from '@bridge/session-ui/one-line';
|
|
48
|
+
import { cutText } from '../text.ts';
|
|
49
|
+
import type { PluginComponent } from '../views/library.ts';
|
|
50
|
+
import type { ViewComponentRenderer, ViewTools } from './views/view.tsx';
|
|
51
|
+
import { useMediaOrigins } from './media-origins.tsx';
|
|
52
|
+
import { ViewLine } from './views.tsx';
|
|
53
|
+
import { answerSummary, isQuestion, type Answer, type Asked, type Question } from '../answers.ts';
|
|
54
|
+
import { DecisionQuestions } from './decision-questions.tsx';
|
|
55
|
+
import { Elapsed, Spinner, duration } from './elapsed.tsx';
|
|
56
|
+
import {
|
|
57
|
+
ChildCards,
|
|
58
|
+
ChildrenMessage,
|
|
59
|
+
LineageContext,
|
|
60
|
+
TaskFrom,
|
|
61
|
+
cardsByTurn,
|
|
62
|
+
hiddenCalls,
|
|
63
|
+
sameCards,
|
|
64
|
+
taskOf,
|
|
65
|
+
type Delegation,
|
|
66
|
+
type Lineage,
|
|
67
|
+
} from './children.tsx';
|
|
68
|
+
import { SubagentCards, sameSubagents, subagentsByTurn, type SubagentCard } from './subagents.tsx';
|
|
69
|
+
|
|
70
|
+
export { Spinner };
|
|
71
|
+
|
|
72
|
+
/** Whether a mode change's `options` (`{ plan?, auto? }`, where a decision's are a list) holds `option`. */
|
|
73
|
+
const optionOn = (options: unknown, option: 'plan' | 'auto') =>
|
|
74
|
+
typeof options === 'object' &&
|
|
75
|
+
options !== null &&
|
|
76
|
+
Object.entries(options).some(([key, value]) => key === option && value === true);
|
|
77
|
+
|
|
78
|
+
/** A child's question a `user/message` of Bridge's delivers: the child, its decision, the text asking it. */
|
|
79
|
+
export interface ChildQuestion {
|
|
80
|
+
session: string;
|
|
81
|
+
decision: string;
|
|
82
|
+
text: string;
|
|
83
|
+
}
|
|
84
|
+
/** The questions decision `event` asks (the harness contract's `questions`), none for an old driver's. */
|
|
85
|
+
const questionsAsked = (event: Stored) =>
|
|
86
|
+
event.type === 'decision/asked' && event.questions?.length
|
|
87
|
+
? (event.questions as Question[])
|
|
88
|
+
: undefined;
|
|
89
|
+
/** Decision `event` as `answers.ts` reads it. */
|
|
90
|
+
const askedOf = (event: Stored): Asked => ({
|
|
91
|
+
title: event.title,
|
|
92
|
+
options: event.options,
|
|
93
|
+
kind: event.kind === 'question' || event.kind === 'approval' ? event.kind : undefined,
|
|
94
|
+
questions: questionsAsked(event),
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
export interface Stored {
|
|
98
|
+
seq: number;
|
|
99
|
+
time?: string;
|
|
100
|
+
type: string;
|
|
101
|
+
id?: string;
|
|
102
|
+
sha?: string;
|
|
103
|
+
files?: string[];
|
|
104
|
+
request?: string;
|
|
105
|
+
question?: string;
|
|
106
|
+
to?: string;
|
|
107
|
+
/** A peer line's sender; on a `user/message`, `'bridge'` when Bridge itself sent it (not the user). */
|
|
108
|
+
from?: string;
|
|
109
|
+
verdict?: string;
|
|
110
|
+
by?: string;
|
|
111
|
+
text?: string;
|
|
112
|
+
title?: string;
|
|
113
|
+
/** A `view` line (research-036): its program, and the view it patched. */
|
|
114
|
+
program?: string;
|
|
115
|
+
base?: number;
|
|
116
|
+
/** A frozen view's answers by query, and when it was frozen; a dynamic view has no snapshot and `dynamic: true`. */
|
|
117
|
+
snapshot?: Record<string, unknown>;
|
|
118
|
+
at?: string;
|
|
119
|
+
dynamic?: boolean;
|
|
120
|
+
/** A `view_frozen` line (research-036 P5): the view it freezes. */
|
|
121
|
+
view?: number;
|
|
122
|
+
/** The plugin components the view calls, as declared when it was shown (research-036 P2). */
|
|
123
|
+
components?: PluginComponent[];
|
|
124
|
+
kind?: string;
|
|
125
|
+
status?: string;
|
|
126
|
+
reason?: string;
|
|
127
|
+
message?: string;
|
|
128
|
+
interrupted?: boolean;
|
|
129
|
+
input?: unknown;
|
|
130
|
+
output?: unknown;
|
|
131
|
+
options?: { id: string; label: string; kind?: string }[];
|
|
132
|
+
option?: string | null;
|
|
133
|
+
expiresAt?: string;
|
|
134
|
+
mode?: string;
|
|
135
|
+
effort?: string;
|
|
136
|
+
name?: string;
|
|
137
|
+
auto?: boolean;
|
|
138
|
+
changes?: Change[];
|
|
139
|
+
usage?: Usage;
|
|
140
|
+
attachments?: Attached[];
|
|
141
|
+
/** A `decision/asked` line's tool call (its `id`), when the agent said which call it asks about. */
|
|
142
|
+
tool?: string;
|
|
143
|
+
/** A `user/message` a scheduled task sent: its task. */
|
|
144
|
+
scheduled?: { id: string; title: string };
|
|
145
|
+
/** A `user/message` of Bridge's delivering children's results: their delegations (`delegations.ts`). */
|
|
146
|
+
delegations?: string[];
|
|
147
|
+
/** A `user/message` of Bridge's delivering children's questions: each child, its decision, the text asking it; a
|
|
148
|
+
* `decision/asked`'s questions (the harness contract's, `questionsAsked`). */
|
|
149
|
+
questions?: ChildQuestion[] | Question[];
|
|
150
|
+
/** A `decision/answered`'s answers to its questions (`option` null then). */
|
|
151
|
+
answers?: { question: string; options?: string[]; text?: string }[];
|
|
152
|
+
/** A `user/message` of Bridge's another session's agent sent: that session. */
|
|
153
|
+
sender?: string;
|
|
154
|
+
/** A `user/message` put into a running turn (bridge#212 S6): drawn where it entered the turn, among its work. */
|
|
155
|
+
steered?: boolean;
|
|
156
|
+
/** A line of a sub-agent of the session's agent (bridge#212 S8, ./subagents.tsx): its id; on a `subagent` line, the
|
|
157
|
+
* sub-agent it tells. */
|
|
158
|
+
subagent?: string;
|
|
159
|
+
/** A `subagent` line's agent kind, its task, and whether its harness can stop it. */
|
|
160
|
+
agent?: string;
|
|
161
|
+
prompt?: string;
|
|
162
|
+
stoppable?: boolean;
|
|
163
|
+
}
|
|
164
|
+
export interface Change {
|
|
165
|
+
path: string;
|
|
166
|
+
added: number;
|
|
167
|
+
removed: number;
|
|
168
|
+
lines: string[];
|
|
169
|
+
more?: number;
|
|
170
|
+
}
|
|
171
|
+
interface Usage {
|
|
172
|
+
used?: number | undefined;
|
|
173
|
+
size?: number | undefined;
|
|
174
|
+
}
|
|
175
|
+
export interface LiveEvent {
|
|
176
|
+
type: string;
|
|
177
|
+
id?: string;
|
|
178
|
+
title?: string;
|
|
179
|
+
kind?: string;
|
|
180
|
+
text?: string;
|
|
181
|
+
status?: string;
|
|
182
|
+
input?: unknown;
|
|
183
|
+
output?: unknown;
|
|
184
|
+
changes?: Change[];
|
|
185
|
+
entries?: { content: string; status: string }[];
|
|
186
|
+
used?: number;
|
|
187
|
+
size?: number;
|
|
188
|
+
/** A `view/draft` event's program so far (research-036 streaming). */
|
|
189
|
+
program?: string;
|
|
190
|
+
/** The sub-agent that emits it (bridge#212 S8). */
|
|
191
|
+
subagent?: string;
|
|
192
|
+
}
|
|
193
|
+
/** The running turn's in-flight part, as the server's accumulator. */
|
|
194
|
+
export interface Live {
|
|
195
|
+
seq: number;
|
|
196
|
+
text: string;
|
|
197
|
+
thought: string;
|
|
198
|
+
tools: Record<string, LiveEvent>;
|
|
199
|
+
plan: { content: string; status: string }[];
|
|
200
|
+
usage?: Usage | null;
|
|
201
|
+
decisions: LiveEvent[];
|
|
202
|
+
/** A view whose program the agent is still writing (research-036 streaming). */
|
|
203
|
+
view?: { id: string; title?: string; program: string };
|
|
204
|
+
/** Its sub-agents' pending text and thought, by sub-agent (bridge#212 S8). */
|
|
205
|
+
subagents?: Record<string, { text: string; thought: string }>;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const verdicts = { ok: 'OK', wait: 'Wait', no: 'No' } as const;
|
|
209
|
+
const toolIcons: Record<string, string> = {
|
|
210
|
+
read: 'eye',
|
|
211
|
+
edit: 'pen-line',
|
|
212
|
+
delete: 'trash',
|
|
213
|
+
move: 'move',
|
|
214
|
+
search: 'search',
|
|
215
|
+
execute: 'terminal',
|
|
216
|
+
think: 'brain',
|
|
217
|
+
fetch: 'globe',
|
|
218
|
+
compact: 'minimize-2',
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
/** A tool input or output as text, cut for display (the full one stays in the history). */
|
|
222
|
+
const shown = (value: unknown) => {
|
|
223
|
+
const text = typeof value === 'string' ? value : JSON.stringify(value, null, 2);
|
|
224
|
+
return text.length > 4000 ? `${cutText(text, 4000)}\n… [truncated]` : text;
|
|
225
|
+
};
|
|
226
|
+
const clock = (iso?: string) =>
|
|
227
|
+
iso ? formatDate(iso, { hour: 'numeric', minute: '2-digit' }) : '';
|
|
228
|
+
const fullDate = (iso?: string) =>
|
|
229
|
+
iso ? formatDate(iso, { dateStyle: 'full', timeStyle: 'short' }) : undefined;
|
|
230
|
+
const between = (from?: string, to?: string) =>
|
|
231
|
+
from && to ? Date.parse(to) - Date.parse(from) : undefined;
|
|
232
|
+
|
|
233
|
+
/** A copy button: the icon turns to a check for a second. */
|
|
234
|
+
function CopyButton({
|
|
235
|
+
text,
|
|
236
|
+
label = 'Copy',
|
|
237
|
+
className,
|
|
238
|
+
}: {
|
|
239
|
+
text: () => string;
|
|
240
|
+
label?: string;
|
|
241
|
+
className?: string;
|
|
242
|
+
}) {
|
|
243
|
+
const [done, setDone] = React.useState<'Copied' | 'Copy failed'>();
|
|
244
|
+
React.useEffect(() => {
|
|
245
|
+
if (!done) return;
|
|
246
|
+
const timer = setTimeout(() => setDone(undefined), 1200);
|
|
247
|
+
return () => clearTimeout(timer);
|
|
248
|
+
}, [done]);
|
|
249
|
+
return (
|
|
250
|
+
<Tooltip>
|
|
251
|
+
<TooltipTrigger
|
|
252
|
+
render={
|
|
253
|
+
<Button
|
|
254
|
+
size="icon-sm"
|
|
255
|
+
aria-label={done ?? label}
|
|
256
|
+
className={cn(
|
|
257
|
+
'text-muted',
|
|
258
|
+
done === 'Copied' && 'text-success',
|
|
259
|
+
done === 'Copy failed' && 'text-destructive',
|
|
260
|
+
className,
|
|
261
|
+
)}
|
|
262
|
+
onClick={() =>
|
|
263
|
+
void copyText(text()).then(
|
|
264
|
+
() => setDone('Copied'),
|
|
265
|
+
() => setDone('Copy failed'),
|
|
266
|
+
)
|
|
267
|
+
}
|
|
268
|
+
/>
|
|
269
|
+
}
|
|
270
|
+
>
|
|
271
|
+
<Icon
|
|
272
|
+
name={done === 'Copied' ? 'check' : done === 'Copy failed' ? 'x' : 'copy'}
|
|
273
|
+
size={14}
|
|
274
|
+
/>
|
|
275
|
+
</TooltipTrigger>
|
|
276
|
+
<TooltipContent>{done ?? label}</TooltipContent>
|
|
277
|
+
</Tooltip>
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** A fenced code block: its language, Wrap and Copy in a header. */
|
|
282
|
+
function CodeBlock({ children }: { children?: React.ReactNode }) {
|
|
283
|
+
const [wrap, setWrap] = React.useState(false);
|
|
284
|
+
const body = React.useRef<HTMLPreElement>(null);
|
|
285
|
+
const child = React.Children.toArray(children)[0];
|
|
286
|
+
const language = React.isValidElement<{ className?: string }>(child)
|
|
287
|
+
? /language-([\w+-]+)/.exec(child.props.className ?? '')?.[1]
|
|
288
|
+
: undefined;
|
|
289
|
+
return (
|
|
290
|
+
<div
|
|
291
|
+
data-code
|
|
292
|
+
className="overflow-hidden rounded-default border border-border-subtle bg-hover/60"
|
|
293
|
+
>
|
|
294
|
+
<div className="flex h-8 items-center gap-0.5 border-b border-border-subtle pl-3 pr-1 text-xs text-muted">
|
|
295
|
+
<span>{language ?? 'text'}</span>
|
|
296
|
+
<span className="flex-1" />
|
|
297
|
+
<Tooltip>
|
|
298
|
+
<TooltipTrigger
|
|
299
|
+
render={
|
|
300
|
+
<Button
|
|
301
|
+
size="icon-sm"
|
|
302
|
+
aria-label="Wrap lines"
|
|
303
|
+
aria-pressed={wrap}
|
|
304
|
+
className={cn('text-muted', wrap && 'text-foreground')}
|
|
305
|
+
onClick={() => setWrap((was) => !was)}
|
|
306
|
+
/>
|
|
307
|
+
}
|
|
308
|
+
>
|
|
309
|
+
<Icon name="text-wrap" size={14} />
|
|
310
|
+
</TooltipTrigger>
|
|
311
|
+
<TooltipContent>Wrap lines</TooltipContent>
|
|
312
|
+
</Tooltip>
|
|
313
|
+
<CopyButton text={() => body.current?.textContent ?? ''} label="Copy code" />
|
|
314
|
+
</div>
|
|
315
|
+
<pre
|
|
316
|
+
ref={body}
|
|
317
|
+
className={cn(
|
|
318
|
+
'overflow-x-auto p-3 font-mono text-xs leading-relaxed [&_code]:bg-transparent [&_code]:p-0 [&_code]:text-xs',
|
|
319
|
+
wrap && 'whitespace-pre-wrap break-words',
|
|
320
|
+
)}
|
|
321
|
+
>
|
|
322
|
+
{children}
|
|
323
|
+
</pre>
|
|
324
|
+
</div>
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/** Markdown in the chat's type, GitHub's flavour (tables, bare URLs as links): no raw HTML (react-markdown escapes it),
|
|
329
|
+
* external links in a new tab, local images and videos shown (`media.tsx`). */
|
|
330
|
+
const markdown: Components = {
|
|
331
|
+
p: ({ node: _node, ...props }) => <p className="leading-relaxed" {...props} />,
|
|
332
|
+
h1: ({ node: _node, ...props }) => <h3 className="text-base font-semibold" {...props} />,
|
|
333
|
+
h2: ({ node: _node, ...props }) => <h4 className="text-base font-semibold" {...props} />,
|
|
334
|
+
h3: ({ node: _node, ...props }) => <h5 className="text-sm font-semibold" {...props} />,
|
|
335
|
+
ul: ({ node: _node, ...props }) => <ul className="list-disc space-y-1 pl-5" {...props} />,
|
|
336
|
+
ol: ({ node: _node, ...props }) => <ol className="list-decimal space-y-1 pl-5" {...props} />,
|
|
337
|
+
blockquote: ({ node: _node, ...props }) => (
|
|
338
|
+
<blockquote className="border-l-2 border-border pl-3 text-secondary" {...props} />
|
|
339
|
+
),
|
|
340
|
+
// A link of this Bridge stays an anchor without a target, which the shell's link rule follows (research-048: a plain
|
|
341
|
+
// click here, ⌘ or a middle click beside, ⌥ on a new desk); another opens in a new tab, which gets no `opener`.
|
|
342
|
+
a: ({ node: _node, ...props }) => {
|
|
343
|
+
let internal = false;
|
|
344
|
+
if (typeof location !== 'undefined' && props.href) {
|
|
345
|
+
try {
|
|
346
|
+
internal = new URL(props.href, location.href).origin === location.origin;
|
|
347
|
+
} catch {
|
|
348
|
+
/* Leave malformed links to the browser. */
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return (
|
|
352
|
+
<a
|
|
353
|
+
className="text-accent underline-offset-2 hover:underline"
|
|
354
|
+
target={internal ? undefined : '_blank'}
|
|
355
|
+
rel={internal ? undefined : 'noopener noreferrer'}
|
|
356
|
+
{...props}
|
|
357
|
+
/>
|
|
358
|
+
);
|
|
359
|
+
},
|
|
360
|
+
table: ({ node: _node, ...props }) => (
|
|
361
|
+
<div className="overflow-x-auto">
|
|
362
|
+
<table className="w-full border-collapse text-[13px]" {...props} />
|
|
363
|
+
</div>
|
|
364
|
+
),
|
|
365
|
+
th: ({ node: _node, ...props }) => (
|
|
366
|
+
<th className="border border-border-subtle px-2 py-1 text-left font-semibold" {...props} />
|
|
367
|
+
),
|
|
368
|
+
td: ({ node: _node, ...props }) => (
|
|
369
|
+
<td className="border border-border-subtle px-2 py-1" {...props} />
|
|
370
|
+
),
|
|
371
|
+
hr: () => <hr className="border-border" />,
|
|
372
|
+
code: ({ node: _node, ...props }) => (
|
|
373
|
+
<code className="rounded-sm bg-hover px-1 py-px font-mono text-[0.85em]" {...props} />
|
|
374
|
+
),
|
|
375
|
+
pre: ({ node: _node, children }) => <CodeBlock>{children}</CodeBlock>,
|
|
376
|
+
img: ({ src, alt }) => (
|
|
377
|
+
<MarkdownMedia
|
|
378
|
+
{...(typeof src === 'string' ? { src } : {})}
|
|
379
|
+
{...(alt === undefined ? {} : { alt })}
|
|
380
|
+
/>
|
|
381
|
+
),
|
|
382
|
+
};
|
|
383
|
+
const remarkPlugins = [remarkGfm];
|
|
384
|
+
const urlTransform = (url: string, key: string) => mediaUrl(url, key, defaultUrlTransform);
|
|
385
|
+
/** The blinking caret after a streaming reply's last block (a pseudo-element: no text). */
|
|
386
|
+
const caret =
|
|
387
|
+
"[&>:last-child]:after:ml-0.5 [&>:last-child]:after:inline-block [&>:last-child]:after:h-[1em] [&>:last-child]:after:w-[0.45em] [&>:last-child]:after:translate-y-[0.15em] [&>:last-child]:after:animate-pulse [&>:last-child]:after:rounded-[1px] [&>:last-child]:after:bg-foreground [&>:last-child]:after:content-['']";
|
|
388
|
+
// Memoized: a streamed delta parses the running reply's Markdown again, never the earlier ones'.
|
|
389
|
+
const Reply = React.memo(function Reply({
|
|
390
|
+
text,
|
|
391
|
+
streaming = false,
|
|
392
|
+
quiet = false,
|
|
393
|
+
}: {
|
|
394
|
+
text: string;
|
|
395
|
+
streaming?: boolean;
|
|
396
|
+
quiet?: boolean;
|
|
397
|
+
}) {
|
|
398
|
+
return (
|
|
399
|
+
<div
|
|
400
|
+
className={cn(
|
|
401
|
+
'flex flex-col break-words',
|
|
402
|
+
quiet ? 'gap-2 text-[13px] text-secondary' : 'gap-3 text-sm text-foreground',
|
|
403
|
+
streaming && caret,
|
|
404
|
+
)}
|
|
405
|
+
>
|
|
406
|
+
<Markdown remarkPlugins={remarkPlugins} components={markdown} urlTransform={urlTransform}>
|
|
407
|
+
{text}
|
|
408
|
+
</Markdown>
|
|
409
|
+
</div>
|
|
410
|
+
);
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
/** Time left until `expiresAt` in its last minute, ticking each second: "expires in 42 s"; nothing before. */
|
|
414
|
+
const Countdown = ({ expiresAt }: { expiresAt: string }) => {
|
|
415
|
+
const [now, setNow] = React.useState(Date.now);
|
|
416
|
+
React.useEffect(() => {
|
|
417
|
+
const timer = setInterval(() => setNow(Date.now()), 1000);
|
|
418
|
+
return () => clearInterval(timer);
|
|
419
|
+
}, []);
|
|
420
|
+
const left = Math.ceil((Date.parse(expiresAt) - now) / 1000);
|
|
421
|
+
if (left > 60) return null;
|
|
422
|
+
const text = left <= 0 ? 'expiring…' : `expires in ${left} s`;
|
|
423
|
+
return (
|
|
424
|
+
<span
|
|
425
|
+
data-countdown
|
|
426
|
+
className={cn(
|
|
427
|
+
'text-xs tabular-nums',
|
|
428
|
+
left <= 10 ? 'font-medium text-destructive' : 'text-muted',
|
|
429
|
+
)}
|
|
430
|
+
>
|
|
431
|
+
{text}
|
|
432
|
+
</span>
|
|
433
|
+
);
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
/** Added and removed line counts, in the diff colours. */
|
|
437
|
+
const Counts = ({ added, removed }: { added: number; removed: number }) => (
|
|
438
|
+
<span className="shrink-0 font-mono text-xs">
|
|
439
|
+
{added > 0 && <span className="text-success">+{added}</span>}
|
|
440
|
+
{added > 0 && removed > 0 && ' '}
|
|
441
|
+
{removed > 0 && <span className="text-destructive">−{removed}</span>}
|
|
442
|
+
</span>
|
|
443
|
+
);
|
|
444
|
+
const sum = (changes: Change[]) =>
|
|
445
|
+
changes.reduce(
|
|
446
|
+
(total, each) => ({ added: total.added + each.added, removed: total.removed + each.removed }),
|
|
447
|
+
{ added: 0, removed: 0 },
|
|
448
|
+
);
|
|
449
|
+
|
|
450
|
+
/** One tool call as the conversation knows it: its call line, its result line, its live state. */
|
|
451
|
+
interface Tool {
|
|
452
|
+
call: Stored;
|
|
453
|
+
result?: Stored | undefined;
|
|
454
|
+
live?: LiveEvent | undefined;
|
|
455
|
+
}
|
|
456
|
+
const statusOf = ({ result, live }: Tool) => result?.status ?? live?.status ?? 'pending';
|
|
457
|
+
const isDone = (tool: Tool) => !['pending', 'running'].includes(statusOf(tool));
|
|
458
|
+
const changesOf = ({ result, live }: Tool) => result?.changes ?? live?.changes ?? [];
|
|
459
|
+
|
|
460
|
+
/** The verb of a run of `count` tool calls of kind `kind`, done or not. */
|
|
461
|
+
const runLabel = (kind: string, count: number, done: boolean) => {
|
|
462
|
+
const [doing, did, what] = (
|
|
463
|
+
{
|
|
464
|
+
read: ['Reading', 'Read', 'files'],
|
|
465
|
+
edit: ['Editing', 'Edited', 'files'],
|
|
466
|
+
delete: ['Deleting', 'Deleted', 'files'],
|
|
467
|
+
move: ['Moving', 'Moved', 'files'],
|
|
468
|
+
search: ['Running', 'Ran', 'searches'],
|
|
469
|
+
execute: ['Running', 'Ran', 'commands'],
|
|
470
|
+
fetch: ['Fetching', 'Fetched', 'pages'],
|
|
471
|
+
think: ['Thinking', 'Thought', 'times'],
|
|
472
|
+
compact: ['Compacting', 'Compacted', 'times'],
|
|
473
|
+
} as Record<string, [string, string, string]>
|
|
474
|
+
)[kind] ?? ['Using', 'Used', 'tools'];
|
|
475
|
+
return `${done ? did : doing} ${count} ${what}`;
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
/** What a tool call did, opened under its line: its edits' changed lines, else its input and output. */
|
|
479
|
+
function ToolDetails({
|
|
480
|
+
tool,
|
|
481
|
+
relative,
|
|
482
|
+
onOpenChange,
|
|
483
|
+
}: {
|
|
484
|
+
tool: Tool;
|
|
485
|
+
relative: (path: string) => string;
|
|
486
|
+
onOpenChange?: ((path: string) => void) | undefined;
|
|
487
|
+
}) {
|
|
488
|
+
const changes = changesOf(tool);
|
|
489
|
+
const output = tool.result?.output ?? tool.live?.output;
|
|
490
|
+
const input = tool.call.input;
|
|
491
|
+
// An image the agent looked at shows, not its path again and its bytes.
|
|
492
|
+
const image = viewedImage(tool.call.kind, input);
|
|
493
|
+
if (image)
|
|
494
|
+
return (
|
|
495
|
+
<div data-who="tool-image" className="ml-[22px] mt-1 mb-2">
|
|
496
|
+
<MarkdownMedia src={image} />
|
|
497
|
+
</div>
|
|
498
|
+
);
|
|
499
|
+
// A screenshot or a video a tool made shows, not its JSON and its image's bytes.
|
|
500
|
+
const media = outputMedia(output);
|
|
501
|
+
if (media.length)
|
|
502
|
+
return (
|
|
503
|
+
<div data-who="tool-image" className="ml-[22px] mt-1 mb-2 flex flex-wrap gap-2">
|
|
504
|
+
{media.map((path) => (
|
|
505
|
+
<MarkdownMedia key={path} src={path} />
|
|
506
|
+
))}
|
|
507
|
+
</div>
|
|
508
|
+
);
|
|
509
|
+
if (changes.length) {
|
|
510
|
+
return (
|
|
511
|
+
<div className="ml-[22px] mt-1 mb-2 grid gap-2">
|
|
512
|
+
{changes.map((change, index) => (
|
|
513
|
+
<div
|
|
514
|
+
key={index}
|
|
515
|
+
className="overflow-hidden rounded-default border border-border-subtle bg-hover/40 text-xs"
|
|
516
|
+
>
|
|
517
|
+
<div className="flex h-7 items-center gap-2 border-b border-border-subtle bg-surface px-2.5 text-muted">
|
|
518
|
+
<span className="min-w-0 truncate font-mono text-secondary">
|
|
519
|
+
{relative(change.path)}
|
|
520
|
+
</span>
|
|
521
|
+
<Counts added={change.added} removed={change.removed} />
|
|
522
|
+
{onOpenChange && (
|
|
523
|
+
<Button
|
|
524
|
+
size="sm"
|
|
525
|
+
className="ml-auto h-6 gap-1 px-1.5 text-xs text-muted"
|
|
526
|
+
onClick={() => onOpenChange(change.path)}
|
|
527
|
+
>
|
|
528
|
+
Open in Changes
|
|
529
|
+
<Icon name="arrow-up-right" size={12} />
|
|
530
|
+
</Button>
|
|
531
|
+
)}
|
|
532
|
+
</div>
|
|
533
|
+
<div className="max-h-72 overflow-auto py-1 font-mono leading-relaxed">
|
|
534
|
+
{change.lines.map((line, at) => (
|
|
535
|
+
<div
|
|
536
|
+
key={at}
|
|
537
|
+
className={cn(
|
|
538
|
+
'whitespace-pre px-2.5',
|
|
539
|
+
line[0] === '+'
|
|
540
|
+
? 'bg-success-subtle text-success'
|
|
541
|
+
: line[0] === '-'
|
|
542
|
+
? 'bg-destructive-subtle text-destructive'
|
|
543
|
+
: 'text-muted',
|
|
544
|
+
)}
|
|
545
|
+
>
|
|
546
|
+
{line || ' '}
|
|
547
|
+
</div>
|
|
548
|
+
))}
|
|
549
|
+
</div>
|
|
550
|
+
{change.more ? (
|
|
551
|
+
<p className="border-t border-border-subtle px-2.5 py-1 text-muted">
|
|
552
|
+
{change.more} more changed line{change.more === 1 ? '' : 's'}
|
|
553
|
+
</p>
|
|
554
|
+
) : null}
|
|
555
|
+
</div>
|
|
556
|
+
))}
|
|
557
|
+
</div>
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
const command =
|
|
561
|
+
tool.call.kind === 'execute' &&
|
|
562
|
+
input &&
|
|
563
|
+
typeof input === 'object' &&
|
|
564
|
+
typeof (input as { command?: unknown }).command === 'string'
|
|
565
|
+
? (input as { command: string }).command
|
|
566
|
+
: undefined;
|
|
567
|
+
return (
|
|
568
|
+
<div className="ml-[22px] mt-1 mb-2 overflow-hidden rounded-default border border-border-subtle bg-hover/40 text-xs">
|
|
569
|
+
{command !== undefined ? (
|
|
570
|
+
<div className="flex min-h-7 items-center gap-2 border-b border-border-subtle bg-surface px-2.5 py-1">
|
|
571
|
+
<span className="min-w-0 flex-1 whitespace-pre-wrap break-all font-mono text-secondary">
|
|
572
|
+
$ {command}
|
|
573
|
+
</span>
|
|
574
|
+
<CopyButton text={() => command} label="Copy command" />
|
|
575
|
+
</div>
|
|
576
|
+
) : (
|
|
577
|
+
input !== undefined && (
|
|
578
|
+
<div className="border-b border-border-subtle">
|
|
579
|
+
<p className="px-2.5 pt-1.5 text-[11px] font-medium text-muted">Input</p>
|
|
580
|
+
<pre className="max-h-40 overflow-auto whitespace-pre-wrap break-all px-2.5 pb-2 pt-1 font-mono text-secondary">
|
|
581
|
+
{shown(input)}
|
|
582
|
+
</pre>
|
|
583
|
+
</div>
|
|
584
|
+
)
|
|
585
|
+
)}
|
|
586
|
+
{output !== undefined ? (
|
|
587
|
+
<div className="relative">
|
|
588
|
+
{command === undefined && (
|
|
589
|
+
<p className="px-2.5 pt-1.5 text-[11px] font-medium text-muted">Output</p>
|
|
590
|
+
)}
|
|
591
|
+
<pre className="max-h-60 overflow-auto whitespace-pre-wrap break-all px-2.5 py-2 font-mono text-secondary">
|
|
592
|
+
{shown(output)}
|
|
593
|
+
</pre>
|
|
594
|
+
</div>
|
|
595
|
+
) : (
|
|
596
|
+
<p className="px-2.5 py-1.5 text-muted">No output</p>
|
|
597
|
+
)}
|
|
598
|
+
</div>
|
|
599
|
+
);
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/** A tool call's title for its line: absolute paths inside the project made relative, Markdown backticks dropped. */
|
|
603
|
+
const titleOf = (tool: Tool, relative: (text: string) => string) =>
|
|
604
|
+
relative((tool.call.title ?? '').replace(/`/g, '')) || tool.call.kind || 'Tool';
|
|
605
|
+
|
|
606
|
+
/** The same tool call, as drawn: its call, result and live state are the same objects. */
|
|
607
|
+
const sameTool = (a: Tool, b: Tool) =>
|
|
608
|
+
a.call === b.call && a.result === b.result && a.live === b.live;
|
|
609
|
+
|
|
610
|
+
/** A tool call's change counts: a button opening the session's changes on its (first) file, when they open. */
|
|
611
|
+
function ChangeCounts({ changes, env }: { changes: Change[]; env: TurnEnv }) {
|
|
612
|
+
const counts = sum(changes);
|
|
613
|
+
const open = env.onOpenChange;
|
|
614
|
+
if (!open || !changes[0]) return <Counts {...counts} />;
|
|
615
|
+
const path = changes[0].path;
|
|
616
|
+
const label = `Open ${env.relative(path)} in Changes`;
|
|
617
|
+
return (
|
|
618
|
+
<Tooltip>
|
|
619
|
+
<TooltipTrigger
|
|
620
|
+
render={
|
|
621
|
+
<button
|
|
622
|
+
type="button"
|
|
623
|
+
aria-label={label}
|
|
624
|
+
data-who="tool-changes"
|
|
625
|
+
className="relative z-10 -my-0.5 shrink-0 cursor-pointer rounded-control-inner border-0 bg-transparent px-1 py-0.5 font-[inherit] hover:bg-active"
|
|
626
|
+
onClick={() => open(path)}
|
|
627
|
+
/>
|
|
628
|
+
}
|
|
629
|
+
>
|
|
630
|
+
<Counts {...counts} />
|
|
631
|
+
</TooltipTrigger>
|
|
632
|
+
<TooltipContent>{label}</TooltipContent>
|
|
633
|
+
</Tooltip>
|
|
634
|
+
);
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/** One tool call's line; it opens on its details when it has any, and shows the decision open on it (`decision`) with
|
|
638
|
+
* its options. Drawn again only when its tool or its decision changed. */
|
|
639
|
+
const ToolLine = React.memo(
|
|
640
|
+
function ToolLine({
|
|
641
|
+
tool,
|
|
642
|
+
env,
|
|
643
|
+
shown,
|
|
644
|
+
decision,
|
|
645
|
+
}: {
|
|
646
|
+
tool: Tool;
|
|
647
|
+
env: TurnEnv;
|
|
648
|
+
shown?: Set<string> | undefined;
|
|
649
|
+
decision?: Stored | undefined;
|
|
650
|
+
}) {
|
|
651
|
+
const { relative } = env;
|
|
652
|
+
const [open, setOpen] = React.useState(false);
|
|
653
|
+
const status = statusOf(tool);
|
|
654
|
+
const changes = changesOf(tool);
|
|
655
|
+
const took = between(tool.call.time, tool.result?.time);
|
|
656
|
+
const failed = status === 'failed';
|
|
657
|
+
const running = !isDone(tool);
|
|
658
|
+
const compaction = tool.call.kind === 'compact';
|
|
659
|
+
// An image the turn's answer already shows (Codex's Markdown after its view) does not show twice: its line stays shut.
|
|
660
|
+
const image = viewedImage(tool.call.kind, tool.call.input);
|
|
661
|
+
const openable =
|
|
662
|
+
!(image && shown?.has(image)) &&
|
|
663
|
+
(changes.length > 0 ||
|
|
664
|
+
tool.call.input !== undefined ||
|
|
665
|
+
(tool.result?.output ?? tool.live?.output) !== undefined);
|
|
666
|
+
const title = titleOf(tool, relative);
|
|
667
|
+
const line = (
|
|
668
|
+
<>
|
|
669
|
+
{running ? (
|
|
670
|
+
<Spinner label="Running" className="text-accent" />
|
|
671
|
+
) : (
|
|
672
|
+
<Icon name={toolIcons[tool.call.kind ?? ''] ?? 'wrench'} size={14} className="shrink-0" />
|
|
673
|
+
)}
|
|
674
|
+
<span
|
|
675
|
+
className={cn(
|
|
676
|
+
'min-w-0 truncate',
|
|
677
|
+
tool.call.kind === 'execute' && 'font-mono text-[12.5px]',
|
|
678
|
+
running && 'text-secondary',
|
|
679
|
+
)}
|
|
680
|
+
>
|
|
681
|
+
{/* A compaction reads as work: how long it runs, then a quiet "Compacted". */}
|
|
682
|
+
{!compaction ? (
|
|
683
|
+
title
|
|
684
|
+
) : running ? (
|
|
685
|
+
<>
|
|
686
|
+
Compacting for <Elapsed since={tool.call.time} short />
|
|
687
|
+
</>
|
|
688
|
+
) : failed ? (
|
|
689
|
+
'Compaction failed'
|
|
690
|
+
) : (
|
|
691
|
+
'Compacted'
|
|
692
|
+
)}
|
|
693
|
+
</span>
|
|
694
|
+
{changes.length > 0 && <ChangeCounts changes={changes} env={env} />}
|
|
695
|
+
{failed && !compaction && <span className="shrink-0 text-xs">· failed</span>}
|
|
696
|
+
{status === 'ended' && <span className="shrink-0 text-xs">· no outcome</span>}
|
|
697
|
+
<span className="ml-auto flex shrink-0 items-center gap-1.5">
|
|
698
|
+
{took !== undefined && (
|
|
699
|
+
<span className="text-xs tabular-nums opacity-0 transition-opacity group-hover/line:opacity-100">
|
|
700
|
+
{duration(took)}
|
|
701
|
+
</span>
|
|
702
|
+
)}
|
|
703
|
+
{openable && (
|
|
704
|
+
<Icon
|
|
705
|
+
name="chevron-right"
|
|
706
|
+
size={14}
|
|
707
|
+
className={cn(
|
|
708
|
+
'opacity-0 transition group-hover/line:opacity-100',
|
|
709
|
+
open && 'rotate-90 opacity-100',
|
|
710
|
+
)}
|
|
711
|
+
/>
|
|
712
|
+
)}
|
|
713
|
+
</span>
|
|
714
|
+
</>
|
|
715
|
+
);
|
|
716
|
+
const classes = cn(
|
|
717
|
+
'group/line -mx-1.5 flex border-0 bg-transparent font-[inherit] min-h-[26px] w-[calc(100%+12px)] items-center gap-2 rounded-control px-1.5 text-left text-[13px]',
|
|
718
|
+
failed ? 'text-destructive' : decision ? 'text-secondary' : 'text-muted',
|
|
719
|
+
openable && 'cursor-pointer hover:bg-hover hover:text-secondary',
|
|
720
|
+
);
|
|
721
|
+
// Change counts that open the changes are a button of their own: the line's toggle lies under the line, not around
|
|
722
|
+
// it (a button holds no button).
|
|
723
|
+
const nested = openable && changes.length > 0 && !!env.onOpenChange;
|
|
724
|
+
return (
|
|
725
|
+
<div data-who="tool" data-status={status}>
|
|
726
|
+
{openable && !nested ? (
|
|
727
|
+
<button
|
|
728
|
+
type="button"
|
|
729
|
+
aria-expanded={open}
|
|
730
|
+
className={classes}
|
|
731
|
+
onClick={() => setOpen((was) => !was)}
|
|
732
|
+
>
|
|
733
|
+
{line}
|
|
734
|
+
</button>
|
|
735
|
+
) : (
|
|
736
|
+
<div className={cn(classes, nested && 'relative')}>
|
|
737
|
+
{nested && (
|
|
738
|
+
<button
|
|
739
|
+
type="button"
|
|
740
|
+
aria-expanded={open}
|
|
741
|
+
aria-label={title}
|
|
742
|
+
className="absolute inset-0 cursor-pointer rounded-control border-0 bg-transparent p-0"
|
|
743
|
+
onClick={() => setOpen((was) => !was)}
|
|
744
|
+
/>
|
|
745
|
+
)}
|
|
746
|
+
{line}
|
|
747
|
+
</div>
|
|
748
|
+
)}
|
|
749
|
+
{open && <ToolDetails tool={tool} relative={relative} onOpenChange={env.onOpenChange} />}
|
|
750
|
+
{decision && <PendingDecision event={decision} env={env} title={title} />}
|
|
751
|
+
</div>
|
|
752
|
+
);
|
|
753
|
+
},
|
|
754
|
+
(a, b) =>
|
|
755
|
+
sameTool(a.tool, b.tool) && a.env === b.env && a.shown === b.shown && a.decision === b.decision,
|
|
756
|
+
);
|
|
757
|
+
|
|
758
|
+
/** A run of tool calls of one kind: one line ("Read 3 files"), opening on theirs, and open while a decision waits on
|
|
759
|
+
* one of them (`decisions`, by position), shown on its line. Drawn again only when one changed. */
|
|
760
|
+
const ToolRun = React.memo(
|
|
761
|
+
function ToolRun({
|
|
762
|
+
tools,
|
|
763
|
+
env,
|
|
764
|
+
shown,
|
|
765
|
+
decisions,
|
|
766
|
+
}: {
|
|
767
|
+
tools: Tool[];
|
|
768
|
+
env: TurnEnv;
|
|
769
|
+
shown?: Set<string> | undefined;
|
|
770
|
+
decisions: (Stored | undefined)[];
|
|
771
|
+
}) {
|
|
772
|
+
const { relative } = env;
|
|
773
|
+
const [opened, setOpen] = React.useState(false);
|
|
774
|
+
const open = opened || decisions.some(Boolean);
|
|
775
|
+
const kind = tools[0]!.call.kind ?? '';
|
|
776
|
+
const done = tools.every(isDone);
|
|
777
|
+
const failed = tools.filter((tool) => statusOf(tool) === 'failed').length;
|
|
778
|
+
const counts = sum(tools.flatMap(changesOf));
|
|
779
|
+
return (
|
|
780
|
+
<div data-who="tool-run">
|
|
781
|
+
<button
|
|
782
|
+
type="button"
|
|
783
|
+
aria-expanded={open}
|
|
784
|
+
className="group/line -mx-1.5 flex min-h-[26px] w-[calc(100%+12px)] cursor-pointer border-0 bg-transparent font-[inherit] items-center gap-2 rounded-control px-1.5 text-left text-[13px] text-muted hover:bg-hover hover:text-secondary"
|
|
785
|
+
onClick={() => setOpen((was) => !was)}
|
|
786
|
+
>
|
|
787
|
+
{done ? (
|
|
788
|
+
<Icon name={toolIcons[kind] ?? 'wrench'} size={14} className="shrink-0" />
|
|
789
|
+
) : (
|
|
790
|
+
<Spinner label="Running" className="text-accent" />
|
|
791
|
+
)}
|
|
792
|
+
<span className="shrink-0 text-secondary">{runLabel(kind, tools.length, done)}</span>
|
|
793
|
+
<span className="min-w-0 truncate text-muted/80">
|
|
794
|
+
{tools
|
|
795
|
+
.map((tool) =>
|
|
796
|
+
titleOf(tool, relative).replace(
|
|
797
|
+
/^(Read|Edit|Write|Search|Fetch|grep|Grep|Glob)\s+/,
|
|
798
|
+
'',
|
|
799
|
+
),
|
|
800
|
+
)
|
|
801
|
+
.join(', ')}
|
|
802
|
+
</span>
|
|
803
|
+
{(counts.added > 0 || counts.removed > 0) && <Counts {...counts} />}
|
|
804
|
+
{failed > 0 && (
|
|
805
|
+
<span className="shrink-0 text-xs text-destructive">· {failed} failed</span>
|
|
806
|
+
)}
|
|
807
|
+
<Icon
|
|
808
|
+
name="chevron-right"
|
|
809
|
+
size={14}
|
|
810
|
+
className={cn(
|
|
811
|
+
'ml-auto shrink-0 opacity-0 transition group-hover/line:opacity-100',
|
|
812
|
+
open && 'rotate-90 opacity-100',
|
|
813
|
+
)}
|
|
814
|
+
/>
|
|
815
|
+
</button>
|
|
816
|
+
{open && (
|
|
817
|
+
<div className="ml-[7px] flex flex-col border-l border-border-subtle pl-[14px]">
|
|
818
|
+
{tools.map((tool, i) => (
|
|
819
|
+
<ToolLine
|
|
820
|
+
key={tool.call.seq}
|
|
821
|
+
tool={tool}
|
|
822
|
+
env={env}
|
|
823
|
+
shown={shown}
|
|
824
|
+
decision={decisions[i]}
|
|
825
|
+
/>
|
|
826
|
+
))}
|
|
827
|
+
</div>
|
|
828
|
+
)}
|
|
829
|
+
</div>
|
|
830
|
+
);
|
|
831
|
+
},
|
|
832
|
+
(a, b) =>
|
|
833
|
+
a.env === b.env &&
|
|
834
|
+
a.shown === b.shown &&
|
|
835
|
+
a.tools.length === b.tools.length &&
|
|
836
|
+
a.tools.every((tool, i) => sameTool(tool, b.tools[i]!) && a.decisions[i] === b.decisions[i]),
|
|
837
|
+
);
|
|
838
|
+
|
|
839
|
+
/** A turn as the conversation shows it. */
|
|
840
|
+
interface Turn {
|
|
841
|
+
key: number;
|
|
842
|
+
start?: Stored;
|
|
843
|
+
user?: Stored;
|
|
844
|
+
/** Work and system lines, in order. */
|
|
845
|
+
items: Stored[];
|
|
846
|
+
/** The answer: the agent's last words after its last tool call or decision (a turn that ended). */
|
|
847
|
+
answer: Stored[];
|
|
848
|
+
end?: Stored;
|
|
849
|
+
/** The history's page starts inside it (its start on an older page): a turn still, its start unknown. */
|
|
850
|
+
partial?: true;
|
|
851
|
+
}
|
|
852
|
+
/** Lines that stay in view when a turn's work folds. */
|
|
853
|
+
const visible = (event: Stored) =>
|
|
854
|
+
event.type === 'notice' ||
|
|
855
|
+
event.type.startsWith('peer/') ||
|
|
856
|
+
event.type === 'commit' ||
|
|
857
|
+
event.type === 'view' ||
|
|
858
|
+
(event.type === 'turn/end' && event.reason !== 'completed');
|
|
859
|
+
|
|
860
|
+
/** The events as turns (lines outside any turn, commits and messages between sessions, are turns of their own);
|
|
861
|
+
* `cut`: older lines remain, so lines before the first `turn/start` are the end of a turn whose start is older. */
|
|
862
|
+
export const turnsOf = (events: Stored[], cut = false): Turn[] => {
|
|
863
|
+
const turns: Turn[] = [];
|
|
864
|
+
let turn: Turn | undefined;
|
|
865
|
+
if (cut && events[0] && events[0].type !== 'turn/start') {
|
|
866
|
+
turn = { key: events[0].seq, items: [], answer: [], partial: true };
|
|
867
|
+
turns.push(turn);
|
|
868
|
+
}
|
|
869
|
+
for (const event of events) {
|
|
870
|
+
if (event.type === 'turn/start') {
|
|
871
|
+
turn = { key: event.seq, start: event, items: [], answer: [] };
|
|
872
|
+
turns.push(turn);
|
|
873
|
+
} else if (!turn) {
|
|
874
|
+
// An answer to a decision a cut turn left open (bridge#201), written after its end: drawn with its decision.
|
|
875
|
+
const ended = turns.at(-1);
|
|
876
|
+
if (
|
|
877
|
+
event.type === 'decision/answered' &&
|
|
878
|
+
ended?.end &&
|
|
879
|
+
ended.items.some((each) => each.type === 'decision/asked' && each.id === event.id)
|
|
880
|
+
)
|
|
881
|
+
ended.items.push(event);
|
|
882
|
+
else turns.push({ key: event.seq, items: [event], answer: [] });
|
|
883
|
+
} else if (event.type === 'user/message' && !event.steered && !turn.user && !turn.items.length)
|
|
884
|
+
turn.user = event;
|
|
885
|
+
else if (event.type === 'turn/end') {
|
|
886
|
+
turn.end = event;
|
|
887
|
+
// The answer: the agent's words after its last other line of work.
|
|
888
|
+
let at = turn.items.length;
|
|
889
|
+
// Lines drawn with another one (a tool's result, a decision's answer) do not end the answer: a tool still open at the
|
|
890
|
+
// turn's end gets its result after the agent's last words.
|
|
891
|
+
// Navigation links can follow useful answer text. Keep the calls in the work,
|
|
892
|
+
// but do not let them move that text behind the completed turn's fold.
|
|
893
|
+
const quiet = (line: Stored) =>
|
|
894
|
+
line.type === 'assistant/message' ||
|
|
895
|
+
line.type === 'tool/result' ||
|
|
896
|
+
line.type === 'decision/answered' ||
|
|
897
|
+
visible(line) ||
|
|
898
|
+
(line.type === 'tool/call' &&
|
|
899
|
+
/^(?:mcp__bridge__|bridge · )?bridge_link(?:\s|$)/.test(line.title ?? ''));
|
|
900
|
+
while (at > 0 && quiet(turn.items[at - 1]!)) at--;
|
|
901
|
+
const tail = turn.items.slice(at);
|
|
902
|
+
turn.answer = tail.filter((each) => each.type === 'assistant/message');
|
|
903
|
+
turn.items = [
|
|
904
|
+
...turn.items.slice(0, at),
|
|
905
|
+
...tail.filter((each) => each.type !== 'assistant/message'),
|
|
906
|
+
];
|
|
907
|
+
if (event.reason !== 'completed') turn.items.push(event);
|
|
908
|
+
turn = undefined;
|
|
909
|
+
} else turn.items.push(event);
|
|
910
|
+
}
|
|
911
|
+
return turns;
|
|
912
|
+
};
|
|
913
|
+
|
|
914
|
+
export interface ConversationProps {
|
|
915
|
+
events: Stored[];
|
|
916
|
+
live: Live | null;
|
|
917
|
+
/** The ids of the decisions a turn Bridge cut left open (the session's `openDecisions`, bridge#201): open on the last
|
|
918
|
+
* turn, answered from its rows. */
|
|
919
|
+
left?: readonly string[];
|
|
920
|
+
/** Older lines than `events` remain (the history comes in pages): the first lines may end a turn begun earlier. */
|
|
921
|
+
cut?: boolean;
|
|
922
|
+
/** The project's folder: paths inside it show relative. */
|
|
923
|
+
folder?: string | undefined;
|
|
924
|
+
/** The workspace's slug: the host serves the images and videos the agent shows from there. */
|
|
925
|
+
workspace?: string;
|
|
926
|
+
/** Puts `text` in the composer. */
|
|
927
|
+
onReuse: (text: string) => void;
|
|
928
|
+
/** Opens the session's changes. */
|
|
929
|
+
onReview: () => void;
|
|
930
|
+
/** The routes a view the agent showed reads and writes (research-036). */
|
|
931
|
+
viewTools?: ViewTools;
|
|
932
|
+
/** Draws a plugin component a view calls, through its plugin (research-036 P2). */
|
|
933
|
+
renderComponent?: ViewComponentRenderer | undefined;
|
|
934
|
+
/** Answers open decision `event` with `option` (`null`: dismissed), from its row; without it, the row offers none. */
|
|
935
|
+
onAnswer?: ((event: Stored, option: Answer) => Promise<void>) | undefined;
|
|
936
|
+
/** Hears whether the row of the open decision `seq` is in view, `undefined` once it is no longer drawn: the composer
|
|
937
|
+
* points to a row out of view. */
|
|
938
|
+
onDecisionShown?: ((seq: number, shown: boolean | undefined) => void) | undefined;
|
|
939
|
+
/** Opens the session's changes on `path` (a tool call's change counts); without it, the counts open nothing. */
|
|
940
|
+
onOpenChange?: ((path: string) => void) | undefined;
|
|
941
|
+
/** The session's lineage (./children.tsx): its children's cards, its parent; without it, none drawn. */
|
|
942
|
+
lineage?: Lineage | undefined;
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
/** No child delegated in a turn: the same array each render. */
|
|
946
|
+
const NO_CARDS: readonly Delegation[] = [];
|
|
947
|
+
/** No sub-agent started in a turn: the same array each render. */
|
|
948
|
+
const NO_SUBAGENTS: readonly SubagentCard[] = [];
|
|
949
|
+
/** `live` without its sub-agents' tool calls (theirs show in their own view), the same object while it has none. */
|
|
950
|
+
const ownLive = (live: Live | null): Live | null => {
|
|
951
|
+
if (!live || !Object.values(live.tools).some((each) => each.subagent !== undefined)) return live;
|
|
952
|
+
return {
|
|
953
|
+
...live,
|
|
954
|
+
tools: Object.fromEntries(
|
|
955
|
+
Object.entries(live.tools).filter(([, each]) => each.subagent === undefined),
|
|
956
|
+
),
|
|
957
|
+
};
|
|
958
|
+
};
|
|
959
|
+
/** No decision left open by a cut turn: the same array each render, so no turn draws again for it. */
|
|
960
|
+
export const NO_DECISIONS: readonly string[] = [];
|
|
961
|
+
|
|
962
|
+
/** The decisions of the last turn by `seq`, with their answer, and which are still open: the running turn's (`live`), or
|
|
963
|
+
* those a turn Bridge cut left open (`left`, their ids, bridge#201). */
|
|
964
|
+
export const decisionsOf = (
|
|
965
|
+
events: Stored[],
|
|
966
|
+
live: Live | null,
|
|
967
|
+
left: readonly string[] = NO_DECISIONS,
|
|
968
|
+
) => {
|
|
969
|
+
const answers = new Map<number, Stored>();
|
|
970
|
+
const askedNow = new Map<string, number>();
|
|
971
|
+
for (const each of events) {
|
|
972
|
+
if (each.type === 'turn/start') askedNow.clear();
|
|
973
|
+
else if (each.type === 'decision/asked') askedNow.set(each.id ?? '', each.seq);
|
|
974
|
+
else if (each.type === 'decision/answered' && askedNow.has(each.id ?? ''))
|
|
975
|
+
answers.set(askedNow.get(each.id ?? '')!, each);
|
|
976
|
+
}
|
|
977
|
+
const isOpen = (event: Stored) =>
|
|
978
|
+
!answers.has(event.seq) &&
|
|
979
|
+
askedNow.get(event.id ?? '') === event.seq &&
|
|
980
|
+
(!!live?.decisions.some((each) => each.id === event.id) || left.includes(event.id ?? ''));
|
|
981
|
+
const open = events.filter((event) => event.type === 'decision/asked' && isOpen(event));
|
|
982
|
+
return { answers, isOpen, open };
|
|
983
|
+
};
|
|
984
|
+
|
|
985
|
+
export function Conversation({
|
|
986
|
+
events,
|
|
987
|
+
live,
|
|
988
|
+
left = NO_DECISIONS,
|
|
989
|
+
cut = false,
|
|
990
|
+
folder,
|
|
991
|
+
workspace,
|
|
992
|
+
onReuse,
|
|
993
|
+
onReview,
|
|
994
|
+
viewTools,
|
|
995
|
+
renderComponent,
|
|
996
|
+
onAnswer,
|
|
997
|
+
onDecisionShown,
|
|
998
|
+
onOpenChange,
|
|
999
|
+
lineage,
|
|
1000
|
+
}: ConversationProps) {
|
|
1001
|
+
/** Turns opened or folded by the user, by key: otherwise a completed turn is folded. */
|
|
1002
|
+
const [unfolded, setUnfolded] = React.useState<Record<number, boolean>>({});
|
|
1003
|
+
const onFold = React.useCallback(
|
|
1004
|
+
(key: number, open: boolean) => setUnfolded((was) => ({ ...was, [key]: open })),
|
|
1005
|
+
[],
|
|
1006
|
+
);
|
|
1007
|
+
const origins = useMediaOrigins();
|
|
1008
|
+
// The callers' handlers, called at their latest: a new one each render redraws no turn.
|
|
1009
|
+
const handlers = React.useRef({ onReuse, onReview, onAnswer, onDecisionShown, onOpenChange });
|
|
1010
|
+
React.useLayoutEffect(() => {
|
|
1011
|
+
handlers.current = { onReuse, onReview, onAnswer, onDecisionShown, onOpenChange };
|
|
1012
|
+
});
|
|
1013
|
+
const answers = !!onAnswer;
|
|
1014
|
+
const opens = !!onOpenChange;
|
|
1015
|
+
const env = React.useMemo<TurnEnv>(
|
|
1016
|
+
() => ({
|
|
1017
|
+
relative: (text: string) =>
|
|
1018
|
+
folder ? text.split(`${folder.replace(/[\\/]$/, '')}/`).join('') : text,
|
|
1019
|
+
onReuse: (text) => handlers.current.onReuse(text),
|
|
1020
|
+
onReview: () => handlers.current.onReview(),
|
|
1021
|
+
onAnswer: answers
|
|
1022
|
+
? (event, option) => handlers.current.onAnswer?.(event, option) ?? Promise.resolve()
|
|
1023
|
+
: undefined,
|
|
1024
|
+
onDecisionShown: (seq, shown) => handlers.current.onDecisionShown?.(seq, shown),
|
|
1025
|
+
onOpenChange: opens ? (path) => handlers.current.onOpenChange?.(path) : undefined,
|
|
1026
|
+
viewTools,
|
|
1027
|
+
renderComponent,
|
|
1028
|
+
}),
|
|
1029
|
+
[folder, viewTools, renderComponent, answers, opens],
|
|
1030
|
+
);
|
|
1031
|
+
// Drawn from the stored lines only: a streamed delta or a tool's live update reuses them, and redraws only the
|
|
1032
|
+
// running turn (`TurnView`, memoized).
|
|
1033
|
+
const { turns, results, frozenOf } = React.useMemo(() => {
|
|
1034
|
+
const frozenOf = new Map<number, Stored>();
|
|
1035
|
+
for (const each of events)
|
|
1036
|
+
if (each.type === 'view_frozen' && typeof each.view === 'number')
|
|
1037
|
+
frozenOf.set(each.view, each);
|
|
1038
|
+
return {
|
|
1039
|
+
// A sub-agent's lines are its own view's (bridge#212 S8): its card stands for them here.
|
|
1040
|
+
turns: turnsOf(
|
|
1041
|
+
events.some((each) => each.subagent !== undefined)
|
|
1042
|
+
? events.filter((each) => each.subagent === undefined)
|
|
1043
|
+
: events,
|
|
1044
|
+
cut,
|
|
1045
|
+
),
|
|
1046
|
+
results: new Map(
|
|
1047
|
+
events.filter((each) => each.type === 'tool/result').map((each) => [each.id, each]),
|
|
1048
|
+
),
|
|
1049
|
+
frozenOf,
|
|
1050
|
+
};
|
|
1051
|
+
}, [events, cut]);
|
|
1052
|
+
const subagents = React.useMemo(() => subagentsByTurn(turns, events), [turns, events]);
|
|
1053
|
+
const shownLive = React.useMemo(() => ownLive(live), [live]);
|
|
1054
|
+
const last = turns.findLast((turn) => turn.start || turn.partial);
|
|
1055
|
+
// Each turn's children (bridge#212): the delegations its calls made, else those made while it was the last started.
|
|
1056
|
+
const delegations = lineage?.delegations ?? NO_CARDS;
|
|
1057
|
+
const cards = React.useMemo(
|
|
1058
|
+
() => cardsByTurn(turns, delegations, results, cut),
|
|
1059
|
+
[turns, delegations, results, cut],
|
|
1060
|
+
);
|
|
1061
|
+
return (
|
|
1062
|
+
<LineageContext.Provider value={lineage ?? null}>
|
|
1063
|
+
<MediaWorkspace.Provider value={workspace}>
|
|
1064
|
+
<MediaOrigins.Provider value={origins}>
|
|
1065
|
+
<ol
|
|
1066
|
+
aria-label="Conversation"
|
|
1067
|
+
className="mx-auto box-border flex w-full max-w-[816px] flex-col px-7 pb-6 pt-6"
|
|
1068
|
+
>
|
|
1069
|
+
{turns.map((turn) => (
|
|
1070
|
+
<TurnView
|
|
1071
|
+
key={turn.key}
|
|
1072
|
+
turn={turn}
|
|
1073
|
+
results={results}
|
|
1074
|
+
frozenOf={frozenOf}
|
|
1075
|
+
live={turn === last ? shownLive : null}
|
|
1076
|
+
left={turn === last ? left : NO_DECISIONS}
|
|
1077
|
+
isLast={turn === last}
|
|
1078
|
+
unfolded={unfolded[turn.key]}
|
|
1079
|
+
onFold={onFold}
|
|
1080
|
+
env={env}
|
|
1081
|
+
cards={cards.get(turn.key) ?? NO_CARDS}
|
|
1082
|
+
subagents={subagents.get(turn.key) ?? NO_SUBAGENTS}
|
|
1083
|
+
/>
|
|
1084
|
+
))}
|
|
1085
|
+
</ol>
|
|
1086
|
+
</MediaOrigins.Provider>
|
|
1087
|
+
</MediaWorkspace.Provider>
|
|
1088
|
+
</LineageContext.Provider>
|
|
1089
|
+
);
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
/** What every turn draws with, the same object while its inputs stay. */
|
|
1093
|
+
interface TurnEnv {
|
|
1094
|
+
relative: (text: string) => string;
|
|
1095
|
+
onReuse: (text: string) => void;
|
|
1096
|
+
onReview: () => void;
|
|
1097
|
+
onAnswer: ((event: Stored, option: Answer) => Promise<void>) | undefined;
|
|
1098
|
+
onDecisionShown: (seq: number, shown: boolean | undefined) => void;
|
|
1099
|
+
onOpenChange: ((path: string) => void) | undefined;
|
|
1100
|
+
viewTools?: ViewTools | undefined;
|
|
1101
|
+
renderComponent?: ViewComponentRenderer | undefined;
|
|
1102
|
+
}
|
|
1103
|
+
interface TurnProps {
|
|
1104
|
+
turn: Turn;
|
|
1105
|
+
/** Every `tool/result` by its tool's id, and every `view_frozen` by the view it freezes (a later turn's too). */
|
|
1106
|
+
results: Map<string | undefined, Stored>;
|
|
1107
|
+
frozenOf: Map<number, Stored>;
|
|
1108
|
+
/** The running turn's in-flight part: the last turn's only (null for the others). */
|
|
1109
|
+
live: Live | null;
|
|
1110
|
+
/** The decisions a cut turn left open: the last turn's only. */
|
|
1111
|
+
left: readonly string[];
|
|
1112
|
+
isLast: boolean;
|
|
1113
|
+
/** Opened (true) or folded (false) by the user. */
|
|
1114
|
+
unfolded: boolean | undefined;
|
|
1115
|
+
onFold: (key: number, open: boolean) => void;
|
|
1116
|
+
env: TurnEnv;
|
|
1117
|
+
/** The children the turn delegated (./children.tsx): their cards, their calls' rows hidden. */
|
|
1118
|
+
cards: readonly Delegation[];
|
|
1119
|
+
/** The sub-agents its agent started (./subagents.tsx): their cards, the calls that started them hidden. */
|
|
1120
|
+
subagents: readonly SubagentCard[];
|
|
1121
|
+
}
|
|
1122
|
+
const sameLines = (a: Stored[], b: Stored[]) =>
|
|
1123
|
+
a === b || (a.length === b.length && a.every((line, i) => line === b[i]));
|
|
1124
|
+
/** A turn draws again only when what it shows changed: its lines (the same objects), its tools' results, its views'
|
|
1125
|
+
* freezing, the live part (the last turn's), its fold. */
|
|
1126
|
+
const sameTurn = (a: TurnProps, b: TurnProps) =>
|
|
1127
|
+
a.live === b.live &&
|
|
1128
|
+
a.left === b.left &&
|
|
1129
|
+
a.isLast === b.isLast &&
|
|
1130
|
+
a.unfolded === b.unfolded &&
|
|
1131
|
+
a.onFold === b.onFold &&
|
|
1132
|
+
a.env === b.env &&
|
|
1133
|
+
sameCards(a.cards, b.cards) &&
|
|
1134
|
+
sameSubagents(a.subagents, b.subagents) &&
|
|
1135
|
+
(a.turn === b.turn ||
|
|
1136
|
+
(a.turn.key === b.turn.key &&
|
|
1137
|
+
a.turn.start === b.turn.start &&
|
|
1138
|
+
a.turn.partial === b.turn.partial &&
|
|
1139
|
+
a.turn.user === b.turn.user &&
|
|
1140
|
+
a.turn.end === b.turn.end &&
|
|
1141
|
+
sameLines(a.turn.items, b.turn.items) &&
|
|
1142
|
+
sameLines(a.turn.answer, b.turn.answer))) &&
|
|
1143
|
+
b.turn.items.every(
|
|
1144
|
+
(line) =>
|
|
1145
|
+
(line.type !== 'tool/call' ||
|
|
1146
|
+
a.results === b.results ||
|
|
1147
|
+
a.results.get(line.id) === b.results.get(line.id)) &&
|
|
1148
|
+
(line.type !== 'view' ||
|
|
1149
|
+
a.frozenOf === b.frozenOf ||
|
|
1150
|
+
a.frozenOf.get(line.seq) === b.frozenOf.get(line.seq)),
|
|
1151
|
+
);
|
|
1152
|
+
|
|
1153
|
+
/** One turn of the conversation (or a line outside any turn). */
|
|
1154
|
+
const TurnView = React.memo(function TurnView({
|
|
1155
|
+
turn,
|
|
1156
|
+
results,
|
|
1157
|
+
frozenOf,
|
|
1158
|
+
live,
|
|
1159
|
+
left,
|
|
1160
|
+
isLast,
|
|
1161
|
+
unfolded,
|
|
1162
|
+
onFold,
|
|
1163
|
+
env,
|
|
1164
|
+
cards,
|
|
1165
|
+
subagents,
|
|
1166
|
+
}: TurnProps) {
|
|
1167
|
+
const { relative, onReuse, onReview, viewTools, renderComponent } = env;
|
|
1168
|
+
// A child's `delegate_session` call is its card (bridge#212): its row and its result are not drawn; so is the call
|
|
1169
|
+
// that started a sub-agent (S8).
|
|
1170
|
+
const hidden = hiddenCalls(turn.items, cards, results);
|
|
1171
|
+
for (const each of subagents) if (each.tool !== undefined) hidden.add(each.tool);
|
|
1172
|
+
const items = hidden.size
|
|
1173
|
+
? turn.items.filter(
|
|
1174
|
+
(each) =>
|
|
1175
|
+
!((each.type === 'tool/call' || each.type === 'tool/result') && hidden.has(each.id)),
|
|
1176
|
+
)
|
|
1177
|
+
: turn.items;
|
|
1178
|
+
// Its decisions: asked and answered in the turn (the open ones, the last turn's, with the live part or left by a cut).
|
|
1179
|
+
const { answers, isOpen } = decisionsOf(turn.items, live, left);
|
|
1180
|
+
// An open decision about one of the turn's tool calls shows on that call's row, by the call's id.
|
|
1181
|
+
const calls = new Set(turn.items.flatMap((each) => (each.type === 'tool/call' ? [each.id] : [])));
|
|
1182
|
+
const pendingOn = new Map<string, Stored>();
|
|
1183
|
+
for (const each of turn.items)
|
|
1184
|
+
if (each.type === 'decision/asked' && each.tool && calls.has(each.tool) && isOpen(each))
|
|
1185
|
+
pendingOn.set(each.tool, each);
|
|
1186
|
+
/** The view the agent is writing, drawn after the running turn's text (research-036 streaming). */
|
|
1187
|
+
const draftView =
|
|
1188
|
+
live?.view && viewTools ? (
|
|
1189
|
+
<ViewLine
|
|
1190
|
+
seq={live.seq}
|
|
1191
|
+
program={live.view.program}
|
|
1192
|
+
{...(live.view.title !== undefined && { title: live.view.title })}
|
|
1193
|
+
streaming
|
|
1194
|
+
dynamic
|
|
1195
|
+
tools={viewTools}
|
|
1196
|
+
renderComponent={renderComponent}
|
|
1197
|
+
onAssistant={onReuse}
|
|
1198
|
+
/>
|
|
1199
|
+
) : null;
|
|
1200
|
+
/** The images the turn's words show (Markdown), whose tool lines stay shut: the same set while its words stay. */
|
|
1201
|
+
const words = [...turn.items, ...turn.answer]
|
|
1202
|
+
.flatMap((each) => (each.type === 'assistant/message' && each.text ? [each.text] : []))
|
|
1203
|
+
.join('\n');
|
|
1204
|
+
const shown = React.useMemo(() => markdownImages(words), [words]);
|
|
1205
|
+
|
|
1206
|
+
const decisionLine = (event: Stored) => {
|
|
1207
|
+
const answered = answers.get(event.seq);
|
|
1208
|
+
const state = isOpen(event)
|
|
1209
|
+
? 'open'
|
|
1210
|
+
: !answered || answered.reason === 'expired'
|
|
1211
|
+
? 'expired'
|
|
1212
|
+
: 'answered';
|
|
1213
|
+
if (state === 'open')
|
|
1214
|
+
// On its tool call's row, else on its own.
|
|
1215
|
+
return pendingOn.get(event.tool ?? '') === event ? null : (
|
|
1216
|
+
<PendingDecision key={event.seq} event={event} env={env} />
|
|
1217
|
+
);
|
|
1218
|
+
const outcome =
|
|
1219
|
+
state === 'expired'
|
|
1220
|
+
? 'Expired'
|
|
1221
|
+
: answered!.answers?.length
|
|
1222
|
+
? answerSummary(askedOf(event), { answers: answered!.answers })
|
|
1223
|
+
: answered!.option == null
|
|
1224
|
+
? 'Dismissed'
|
|
1225
|
+
: (event.options?.find((option) => option.id === answered!.option)?.label ??
|
|
1226
|
+
answered!.option);
|
|
1227
|
+
const allowed =
|
|
1228
|
+
state === 'answered' &&
|
|
1229
|
+
event.options?.find((option) => option.id === answered!.option)?.kind === 'allow';
|
|
1230
|
+
return (
|
|
1231
|
+
<p
|
|
1232
|
+
key={event.seq}
|
|
1233
|
+
data-who="decision"
|
|
1234
|
+
data-state={state}
|
|
1235
|
+
className="flex min-h-[26px] min-w-0 items-center gap-2 text-[13px] text-muted"
|
|
1236
|
+
>
|
|
1237
|
+
<Icon
|
|
1238
|
+
name={allowed ? 'shield-check' : 'shield-x'}
|
|
1239
|
+
size={14}
|
|
1240
|
+
className={cn('shrink-0', allowed && 'text-success')}
|
|
1241
|
+
/>
|
|
1242
|
+
<span data-title className="truncate">
|
|
1243
|
+
{event.title}
|
|
1244
|
+
</span>
|
|
1245
|
+
<span aria-hidden="true">·</span>
|
|
1246
|
+
<span data-outcome className="max-w-[60%] shrink-0 truncate font-medium text-secondary">
|
|
1247
|
+
{outcome}
|
|
1248
|
+
</span>
|
|
1249
|
+
</p>
|
|
1250
|
+
);
|
|
1251
|
+
};
|
|
1252
|
+
const systemLine = (event: Stored, who: string, content: React.ReactNode) => (
|
|
1253
|
+
<p key={event.seq} data-who={who} className="py-1 text-center text-xs text-muted">
|
|
1254
|
+
{content}
|
|
1255
|
+
</p>
|
|
1256
|
+
);
|
|
1257
|
+
const peerLine = (event: Stored) => {
|
|
1258
|
+
if (event.type === 'peer/answer') {
|
|
1259
|
+
const verdict =
|
|
1260
|
+
event.by === 'user'
|
|
1261
|
+
? event.verdict === 'no'
|
|
1262
|
+
? 'Stop'
|
|
1263
|
+
: 'Force continue'
|
|
1264
|
+
: (verdicts[event.verdict as keyof typeof verdicts] ?? event.verdict);
|
|
1265
|
+
return systemLine(
|
|
1266
|
+
event,
|
|
1267
|
+
'peer',
|
|
1268
|
+
`${event.question ? `Answer to “${oneLine(event.question, 80)}”` : 'Answer'}: ${verdict}${event.text ? ` · ${event.text}` : ''} (${event.by === 'user' ? "by you, in its agent's place" : 'by its agent'})`,
|
|
1269
|
+
);
|
|
1270
|
+
}
|
|
1271
|
+
const asking = event.kind === 'ask';
|
|
1272
|
+
const sent = event.type === 'peer/sent';
|
|
1273
|
+
return (
|
|
1274
|
+
<div
|
|
1275
|
+
key={event.seq}
|
|
1276
|
+
data-who="peer"
|
|
1277
|
+
className="my-1 rounded-dialog border border-border p-3 text-sm"
|
|
1278
|
+
>
|
|
1279
|
+
<p className="text-xs font-medium text-muted">
|
|
1280
|
+
{sent
|
|
1281
|
+
? `${asking ? 'Asked' : 'Told'} ${event.title}`
|
|
1282
|
+
: `${asking ? 'Question' : 'Note'} from ${event.title}`}
|
|
1283
|
+
</p>
|
|
1284
|
+
<p className="mt-0.5 whitespace-pre-wrap text-foreground">{event.text}</p>
|
|
1285
|
+
</div>
|
|
1286
|
+
);
|
|
1287
|
+
};
|
|
1288
|
+
/** A line that is not a tool call. */
|
|
1289
|
+
// A `view_frozen` line names a view it freezes (research-036 P5); the view line itself is drawn, with its snapshot.
|
|
1290
|
+
const lineOf = (event: Stored, quiet: boolean) => {
|
|
1291
|
+
if (event.type === 'assistant/message') {
|
|
1292
|
+
return (
|
|
1293
|
+
<div
|
|
1294
|
+
key={event.seq}
|
|
1295
|
+
data-who="assistant"
|
|
1296
|
+
data-interrupted={event.interrupted || undefined}
|
|
1297
|
+
className={quiet ? 'my-1.5' : undefined}
|
|
1298
|
+
>
|
|
1299
|
+
<Reply text={event.text ?? ''} quiet={quiet} />
|
|
1300
|
+
{event.interrupted && (
|
|
1301
|
+
<span className="mt-1 block text-xs italic text-muted"> (interrupted)</span>
|
|
1302
|
+
)}
|
|
1303
|
+
</div>
|
|
1304
|
+
);
|
|
1305
|
+
}
|
|
1306
|
+
if (event.type === 'decision/asked') return decisionLine(event);
|
|
1307
|
+
if (event.type === 'notice') return systemLine(event, 'notice', event.text);
|
|
1308
|
+
// Every version of a view stays where it is in the session (research-036 P5).
|
|
1309
|
+
if (event.type === 'view' && event.program && viewTools) {
|
|
1310
|
+
const frozen = frozenOf.get(event.seq);
|
|
1311
|
+
const snapshot = event.snapshot ?? frozen?.snapshot;
|
|
1312
|
+
const at = event.at ?? frozen?.at;
|
|
1313
|
+
return (
|
|
1314
|
+
<ViewLine
|
|
1315
|
+
key={event.seq}
|
|
1316
|
+
seq={event.seq}
|
|
1317
|
+
program={event.program}
|
|
1318
|
+
components={event.components}
|
|
1319
|
+
renderComponent={renderComponent}
|
|
1320
|
+
tools={viewTools}
|
|
1321
|
+
onAssistant={onReuse}
|
|
1322
|
+
snapshot={snapshot}
|
|
1323
|
+
at={at}
|
|
1324
|
+
dynamic={event.dynamic === true}
|
|
1325
|
+
{...(event.title !== undefined && { title: event.title })}
|
|
1326
|
+
{...(event.base !== undefined && { base: event.base })}
|
|
1327
|
+
/>
|
|
1328
|
+
);
|
|
1329
|
+
}
|
|
1330
|
+
if (event.type === 'mode/changed')
|
|
1331
|
+
return (
|
|
1332
|
+
<p
|
|
1333
|
+
key={event.seq}
|
|
1334
|
+
data-who="mode"
|
|
1335
|
+
className="flex min-h-[26px] items-center gap-2 text-[13px] text-muted"
|
|
1336
|
+
>
|
|
1337
|
+
<Icon name="shield" size={14} />
|
|
1338
|
+
Permissions: {event.name ?? event.mode}
|
|
1339
|
+
{optionOn(event.options, 'plan') && ' · Plan'}
|
|
1340
|
+
{optionOn(event.options, 'auto') && ' · Auto'}
|
|
1341
|
+
</p>
|
|
1342
|
+
);
|
|
1343
|
+
if (event.type === 'effort/changed')
|
|
1344
|
+
return (
|
|
1345
|
+
<p
|
|
1346
|
+
key={event.seq}
|
|
1347
|
+
data-who="effort"
|
|
1348
|
+
className="flex min-h-[26px] items-center gap-2 text-[13px] text-muted"
|
|
1349
|
+
>
|
|
1350
|
+
<Icon name="brain" size={14} />
|
|
1351
|
+
Reasoning: {event.name ?? event.effort}
|
|
1352
|
+
</p>
|
|
1353
|
+
);
|
|
1354
|
+
if (event.type.startsWith('peer/')) return peerLine(event);
|
|
1355
|
+
if (event.type === 'commit')
|
|
1356
|
+
return systemLine(
|
|
1357
|
+
event,
|
|
1358
|
+
'commit',
|
|
1359
|
+
`Committed ${event.sha?.slice(0, 7)}: ${event.message?.split('\n')[0]} (${event.files?.length ?? 0} file${event.files?.length === 1 ? '' : 's'})`,
|
|
1360
|
+
);
|
|
1361
|
+
if (event.type === 'turn/end' && event.reason !== 'completed')
|
|
1362
|
+
return systemLine(
|
|
1363
|
+
event,
|
|
1364
|
+
'end',
|
|
1365
|
+
`Turn ${event.reason}${event.message ? `: ${event.message}` : ''}`,
|
|
1366
|
+
);
|
|
1367
|
+
return null;
|
|
1368
|
+
};
|
|
1369
|
+
/** A message as its turn shows it: the user's, a scheduled task's, Bridge's (its children's results and questions,
|
|
1370
|
+
* another session's words). */
|
|
1371
|
+
const messageOf = (event: Stored) =>
|
|
1372
|
+
event.from === 'bridge' ? (
|
|
1373
|
+
event.delegations?.length || event.questions?.length ? (
|
|
1374
|
+
<ChildrenMessage
|
|
1375
|
+
time={event.time}
|
|
1376
|
+
text={event.text ?? ''}
|
|
1377
|
+
delegations={event.delegations}
|
|
1378
|
+
questions={event.questions as ChildQuestion[] | undefined}
|
|
1379
|
+
/>
|
|
1380
|
+
) : (
|
|
1381
|
+
<BridgeMessage event={event} />
|
|
1382
|
+
)
|
|
1383
|
+
) : event.scheduled ? (
|
|
1384
|
+
<AutomationMessage event={event} />
|
|
1385
|
+
) : (
|
|
1386
|
+
<UserMessage event={event} onReuse={onReuse} />
|
|
1387
|
+
);
|
|
1388
|
+
/** A message steered into a running turn (bridge#212 S6): in place, saying so. */
|
|
1389
|
+
const steeredLine = (event: Stored) => (
|
|
1390
|
+
<div key={event.seq} data-steered={event.seq} className="my-2 flex flex-col">
|
|
1391
|
+
{messageOf(event)}
|
|
1392
|
+
<span className="self-end text-[11px] text-muted">Sent into the running turn</span>
|
|
1393
|
+
</div>
|
|
1394
|
+
);
|
|
1395
|
+
/** A turn's work: tool calls in runs, the rest line by line. */
|
|
1396
|
+
const work = (items: Stored[], quiet: boolean, shown?: Set<string>) => {
|
|
1397
|
+
const out: React.ReactNode[] = [];
|
|
1398
|
+
let run: Tool[] = [];
|
|
1399
|
+
const flush = () => {
|
|
1400
|
+
// A decision on a call keeps its run as it is (its rows keep their state): the run opens on it.
|
|
1401
|
+
const decisions = run.map((tool) => pendingOn.get(tool.call.id ?? ''));
|
|
1402
|
+
if (run.length > 1)
|
|
1403
|
+
out.push(
|
|
1404
|
+
<ToolRun
|
|
1405
|
+
key={run[0]!.call.seq}
|
|
1406
|
+
tools={run}
|
|
1407
|
+
env={env}
|
|
1408
|
+
shown={shown}
|
|
1409
|
+
decisions={decisions}
|
|
1410
|
+
/>,
|
|
1411
|
+
);
|
|
1412
|
+
else if (run.length === 1)
|
|
1413
|
+
out.push(
|
|
1414
|
+
<ToolLine
|
|
1415
|
+
key={run[0]!.call.seq}
|
|
1416
|
+
tool={run[0]!}
|
|
1417
|
+
env={env}
|
|
1418
|
+
shown={shown}
|
|
1419
|
+
decision={decisions[0]}
|
|
1420
|
+
/>,
|
|
1421
|
+
);
|
|
1422
|
+
run = [];
|
|
1423
|
+
};
|
|
1424
|
+
for (const event of items) {
|
|
1425
|
+
// A message steered into the turn, where it entered it.
|
|
1426
|
+
if (event.type === 'user/message' && event.steered) {
|
|
1427
|
+
flush();
|
|
1428
|
+
out.push(steeredLine(event));
|
|
1429
|
+
continue;
|
|
1430
|
+
}
|
|
1431
|
+
if (event.type === 'tool/call') {
|
|
1432
|
+
const tool: Tool = {
|
|
1433
|
+
call: event,
|
|
1434
|
+
result: results.get(event.id),
|
|
1435
|
+
live: live?.tools[event.id ?? ''],
|
|
1436
|
+
};
|
|
1437
|
+
if (run.length && (run[0]!.call.kind ?? '') !== (event.kind ?? '')) flush();
|
|
1438
|
+
run.push(tool);
|
|
1439
|
+
continue;
|
|
1440
|
+
}
|
|
1441
|
+
if (
|
|
1442
|
+
event.type === 'tool/result' ||
|
|
1443
|
+
event.type === 'decision/answered' ||
|
|
1444
|
+
event.type === 'user/message'
|
|
1445
|
+
)
|
|
1446
|
+
continue;
|
|
1447
|
+
flush();
|
|
1448
|
+
const line = lineOf(event, quiet);
|
|
1449
|
+
if (line) out.push(line);
|
|
1450
|
+
}
|
|
1451
|
+
flush();
|
|
1452
|
+
return out;
|
|
1453
|
+
};
|
|
1454
|
+
|
|
1455
|
+
if (!turn.start && !turn.partial)
|
|
1456
|
+
return (
|
|
1457
|
+
<li key={turn.key} className="list-none">
|
|
1458
|
+
{work(items, false)}
|
|
1459
|
+
</li>
|
|
1460
|
+
);
|
|
1461
|
+
const running = isLast && !turn.end && !!live;
|
|
1462
|
+
// A compaction is not an action of the agent's.
|
|
1463
|
+
const tools = items.filter((each) => each.type === 'tool/call' && each.kind !== 'compact');
|
|
1464
|
+
/** What the running turn does now: its newest tool call. */
|
|
1465
|
+
const newest = running ? items.findLast((each) => each.type === 'tool/call') : undefined;
|
|
1466
|
+
const workItems = items.filter((each) => !visible(each));
|
|
1467
|
+
const kept = items.filter(visible);
|
|
1468
|
+
const hasWork = workItems.length > 0 || running;
|
|
1469
|
+
const foldable = !!turn.end && turn.end.reason === 'completed' && workItems.length > 0;
|
|
1470
|
+
const open = !foldable || (unfolded ?? false);
|
|
1471
|
+
const changes = tools.flatMap((call) =>
|
|
1472
|
+
results.get(call.id)?.status === 'failed' ? [] : (results.get(call.id)?.changes ?? []),
|
|
1473
|
+
);
|
|
1474
|
+
const byFile = new Map<string, { added: number; removed: number }>();
|
|
1475
|
+
for (const change of changes) {
|
|
1476
|
+
const was = byFile.get(change.path) ?? { added: 0, removed: 0 };
|
|
1477
|
+
byFile.set(change.path, {
|
|
1478
|
+
added: was.added + change.added,
|
|
1479
|
+
removed: was.removed + change.removed,
|
|
1480
|
+
});
|
|
1481
|
+
}
|
|
1482
|
+
const total = sum(changes);
|
|
1483
|
+
const took = between(turn.start?.time, turn.end?.time);
|
|
1484
|
+
const answerText = turn.answer.map((each) => each.text ?? '').join('\n\n');
|
|
1485
|
+
return (
|
|
1486
|
+
<li
|
|
1487
|
+
key={turn.key}
|
|
1488
|
+
data-turn={turn.key}
|
|
1489
|
+
data-partial={turn.partial}
|
|
1490
|
+
className="group/turn mb-6 flex list-none flex-col"
|
|
1491
|
+
>
|
|
1492
|
+
{turn.user && messageOf(turn.user)}
|
|
1493
|
+
{hasWork && (
|
|
1494
|
+
<div className="mt-3 flex flex-col">
|
|
1495
|
+
{foldable ? (
|
|
1496
|
+
<button
|
|
1497
|
+
type="button"
|
|
1498
|
+
aria-expanded={open}
|
|
1499
|
+
data-who="fold"
|
|
1500
|
+
className="group/fold mb-2 flex h-8 cursor-pointer items-center gap-2 border-0 border-b border-border-subtle bg-transparent p-0 text-left font-[inherit] text-[13px] text-muted hover:text-secondary"
|
|
1501
|
+
onClick={() => onFold(turn.key, !open)}
|
|
1502
|
+
>
|
|
1503
|
+
<Icon
|
|
1504
|
+
name="chevron-right"
|
|
1505
|
+
size={14}
|
|
1506
|
+
className={cn('shrink-0 transition-transform', open && 'rotate-90')}
|
|
1507
|
+
/>
|
|
1508
|
+
<span>{took !== undefined ? `Worked for ${duration(took)}` : 'Worked'}</span>
|
|
1509
|
+
<span className="flex min-w-0 items-center gap-2 truncate text-xs text-muted/80">
|
|
1510
|
+
{tools.length > 0 && (
|
|
1511
|
+
<span>
|
|
1512
|
+
{tools.length} action{tools.length === 1 ? '' : 's'}
|
|
1513
|
+
</span>
|
|
1514
|
+
)}
|
|
1515
|
+
{byFile.size > 0 && (
|
|
1516
|
+
<>
|
|
1517
|
+
<span aria-hidden="true">·</span>
|
|
1518
|
+
<span>
|
|
1519
|
+
{byFile.size} file{byFile.size === 1 ? '' : 's'}
|
|
1520
|
+
</span>
|
|
1521
|
+
<Counts {...total} />
|
|
1522
|
+
</>
|
|
1523
|
+
)}
|
|
1524
|
+
</span>
|
|
1525
|
+
<span
|
|
1526
|
+
title={fullDate(turn.start?.time)}
|
|
1527
|
+
className="ml-auto shrink-0 text-xs tabular-nums opacity-0 transition-opacity group-hover/fold:opacity-100"
|
|
1528
|
+
>
|
|
1529
|
+
{clock(turn.start?.time)}
|
|
1530
|
+
{turn.end?.time ? ` → ${clock(turn.end.time)}` : ''}
|
|
1531
|
+
</span>
|
|
1532
|
+
</button>
|
|
1533
|
+
) : (
|
|
1534
|
+
running && (
|
|
1535
|
+
<div
|
|
1536
|
+
data-who="working"
|
|
1537
|
+
className="mb-2 flex h-8 items-center gap-2 border-b border-border-subtle text-[13px] text-secondary"
|
|
1538
|
+
>
|
|
1539
|
+
<Spinner label="Working" className="text-accent" />
|
|
1540
|
+
<span className="shrink-0">
|
|
1541
|
+
Working for <Elapsed since={turn.start?.time} />
|
|
1542
|
+
</span>
|
|
1543
|
+
{newest && (
|
|
1544
|
+
<>
|
|
1545
|
+
<span aria-hidden="true" className="text-muted">
|
|
1546
|
+
·
|
|
1547
|
+
</span>
|
|
1548
|
+
<span
|
|
1549
|
+
data-who="activity"
|
|
1550
|
+
className={cn(
|
|
1551
|
+
'min-w-0 truncate text-muted',
|
|
1552
|
+
newest.kind === 'execute' && 'font-mono text-[12.5px]',
|
|
1553
|
+
)}
|
|
1554
|
+
>
|
|
1555
|
+
{newest.kind === 'compact'
|
|
1556
|
+
? 'Compacting context'
|
|
1557
|
+
: titleOf({ call: newest }, relative)}
|
|
1558
|
+
</span>
|
|
1559
|
+
</>
|
|
1560
|
+
)}
|
|
1561
|
+
{tools.length > 0 && (
|
|
1562
|
+
<span className="ml-auto shrink-0 text-xs text-muted">
|
|
1563
|
+
{tools.length} action{tools.length === 1 ? '' : 's'}
|
|
1564
|
+
</span>
|
|
1565
|
+
)}
|
|
1566
|
+
</div>
|
|
1567
|
+
)
|
|
1568
|
+
)}
|
|
1569
|
+
{open && work(workItems, true, shown)}
|
|
1570
|
+
{/* Folded, the messages steered into it stay in view (bridge#212 S6). */}
|
|
1571
|
+
{!open &&
|
|
1572
|
+
workItems
|
|
1573
|
+
.filter((each) => each.type === 'user/message' && each.steered)
|
|
1574
|
+
.map(steeredLine)}
|
|
1575
|
+
{running && live?.thought && <Thought text={live.thought} answered={!!live.text} />}
|
|
1576
|
+
</div>
|
|
1577
|
+
)}
|
|
1578
|
+
{/* The children it delegated, in view whether its work is folded or not. */}
|
|
1579
|
+
<ChildCards delegations={cards} />
|
|
1580
|
+
<SubagentCards cards={subagents} results={results} running={running} />
|
|
1581
|
+
{kept.filter((event) => event.type !== 'turn/end').map((event) => lineOf(event, false))}
|
|
1582
|
+
{turn.answer.length > 0 && (
|
|
1583
|
+
<div className="mt-2 flex flex-col gap-3">
|
|
1584
|
+
{turn.answer.map((event) => lineOf(event, false))}
|
|
1585
|
+
</div>
|
|
1586
|
+
)}
|
|
1587
|
+
{kept.filter((event) => event.type === 'turn/end').map((event) => lineOf(event, false))}
|
|
1588
|
+
{running && live?.text && (
|
|
1589
|
+
<div data-who="assistant" aria-busy="true" className="mt-2">
|
|
1590
|
+
<Reply text={live.text} streaming />
|
|
1591
|
+
</div>
|
|
1592
|
+
)}
|
|
1593
|
+
{running && draftView}
|
|
1594
|
+
{turn.end && byFile.size > 0 && (
|
|
1595
|
+
<div
|
|
1596
|
+
data-who="files"
|
|
1597
|
+
className="mt-3 overflow-hidden rounded-dialog border border-border-subtle"
|
|
1598
|
+
>
|
|
1599
|
+
<div className="flex h-9 items-center gap-2 border-b border-border-subtle bg-surface pl-3 pr-1.5 text-[12.5px] text-secondary">
|
|
1600
|
+
<Icon name="files" size={14} className="text-muted" />
|
|
1601
|
+
{byFile.size} file{byFile.size === 1 ? '' : 's'} changed <Counts {...total} />
|
|
1602
|
+
<span className="flex-1" />
|
|
1603
|
+
<Button size="sm" onClick={onReview}>
|
|
1604
|
+
Review changes
|
|
1605
|
+
</Button>
|
|
1606
|
+
</div>
|
|
1607
|
+
{[...byFile].map(([path, counts]) => (
|
|
1608
|
+
<div
|
|
1609
|
+
key={path}
|
|
1610
|
+
className="flex h-7 items-center gap-2 px-3 text-[12.5px] text-secondary"
|
|
1611
|
+
>
|
|
1612
|
+
<Icon name="pen-line" size={13} className="shrink-0 text-muted" />
|
|
1613
|
+
<span className="min-w-0 truncate font-mono text-xs">{relative(path)}</span>
|
|
1614
|
+
<span className="flex-1" />
|
|
1615
|
+
<Counts {...counts} />
|
|
1616
|
+
</div>
|
|
1617
|
+
))}
|
|
1618
|
+
</div>
|
|
1619
|
+
)}
|
|
1620
|
+
{turn.end && (turn.answer.length > 0 || took !== undefined) && (
|
|
1621
|
+
<div
|
|
1622
|
+
data-who="actions"
|
|
1623
|
+
className={cn(
|
|
1624
|
+
'mt-2 flex items-center gap-0.5 text-xs text-muted transition-opacity',
|
|
1625
|
+
!isLast && 'opacity-0 group-hover/turn:opacity-100 focus-within:opacity-100',
|
|
1626
|
+
)}
|
|
1627
|
+
>
|
|
1628
|
+
{turn.answer.length > 0 && <CopyButton text={() => answerText} label="Copy answer" />}
|
|
1629
|
+
{turn.answer.length > 0 && <span className="mx-1.5 h-3.5 w-px bg-border" />}
|
|
1630
|
+
<span className="tabular-nums" title={fullDate(turn.end.time)}>
|
|
1631
|
+
{[clock(turn.end.time), took !== undefined && duration(took)]
|
|
1632
|
+
.filter(Boolean)
|
|
1633
|
+
.join(' · ')}
|
|
1634
|
+
</span>
|
|
1635
|
+
</div>
|
|
1636
|
+
)}
|
|
1637
|
+
</li>
|
|
1638
|
+
);
|
|
1639
|
+
}, sameTurn);
|
|
1640
|
+
|
|
1641
|
+
/** The label of open decision `event`: a question when its driver says so (or it asks questions, or none of its options
|
|
1642
|
+
* allows or denies), else a permission. */
|
|
1643
|
+
const decisionLabel = (event: Stored) => {
|
|
1644
|
+
const asked = askedOf(event);
|
|
1645
|
+
return (
|
|
1646
|
+
asked.kind !== undefined || asked.questions
|
|
1647
|
+
? isQuestion(asked)
|
|
1648
|
+
: !event.options?.some((option) => option.kind === 'allow' || option.kind === 'deny')
|
|
1649
|
+
)
|
|
1650
|
+
? 'Answer needed'
|
|
1651
|
+
: 'Permission needed';
|
|
1652
|
+
};
|
|
1653
|
+
|
|
1654
|
+
/** An open decision in the timeline: what is asked, its countdown, Dismiss and its options (the allow one focused when
|
|
1655
|
+
* nothing else is); Escape picks its deny option. On its tool call's row (`title`: that call's, which the decision's
|
|
1656
|
+
* repeats only for a screen reader when they are the same), else on its own. Says whether it is in view
|
|
1657
|
+
* (`onDecisionShown`): drawn, it is taken as in view until the browser says otherwise. */
|
|
1658
|
+
const PendingDecision = React.memo(function PendingDecision({
|
|
1659
|
+
event,
|
|
1660
|
+
env: { relative, onAnswer, onDecisionShown },
|
|
1661
|
+
title,
|
|
1662
|
+
}: {
|
|
1663
|
+
event: Stored;
|
|
1664
|
+
env: TurnEnv;
|
|
1665
|
+
title?: string;
|
|
1666
|
+
}) {
|
|
1667
|
+
const [busy, setBusy] = React.useState(false);
|
|
1668
|
+
const section = React.useRef<HTMLElement>(null);
|
|
1669
|
+
const actions = React.useRef<HTMLDivElement>(null);
|
|
1670
|
+
const primary = React.useRef<HTMLButtonElement>(null);
|
|
1671
|
+
React.useEffect(() => {
|
|
1672
|
+
const active = document.activeElement;
|
|
1673
|
+
if (!active || active === document.body) primary.current?.focus({ preventScroll: true });
|
|
1674
|
+
}, [event.seq]);
|
|
1675
|
+
React.useLayoutEffect(() => {
|
|
1676
|
+
// In view: its options all in view (its title alone at the bottom is not).
|
|
1677
|
+
const element = actions.current ?? section.current;
|
|
1678
|
+
onDecisionShown(event.seq, true);
|
|
1679
|
+
const observer =
|
|
1680
|
+
element && typeof IntersectionObserver !== 'undefined'
|
|
1681
|
+
? new IntersectionObserver(
|
|
1682
|
+
([entry]) => {
|
|
1683
|
+
if (entry)
|
|
1684
|
+
onDecisionShown(event.seq, entry.isIntersecting && entry.intersectionRatio >= 0.99);
|
|
1685
|
+
},
|
|
1686
|
+
{ threshold: [0, 1] },
|
|
1687
|
+
)
|
|
1688
|
+
: undefined;
|
|
1689
|
+
if (element) observer?.observe(element);
|
|
1690
|
+
return () => {
|
|
1691
|
+
observer?.disconnect();
|
|
1692
|
+
onDecisionShown(event.seq, undefined);
|
|
1693
|
+
};
|
|
1694
|
+
}, [event.seq, onDecisionShown]);
|
|
1695
|
+
const answer = (option: Answer) => {
|
|
1696
|
+
if (!onAnswer) return;
|
|
1697
|
+
setBusy(true);
|
|
1698
|
+
onAnswer(event, option)
|
|
1699
|
+
.finally(() => setBusy(false))
|
|
1700
|
+
.catch((cause: unknown) => console.error('sessions: the answer failed', cause));
|
|
1701
|
+
};
|
|
1702
|
+
const asked = relative(event.title ?? '');
|
|
1703
|
+
const deny = event.options?.find((option) => option.kind === 'deny');
|
|
1704
|
+
const allow = event.options?.find((option) => option.kind === 'allow');
|
|
1705
|
+
const label = decisionLabel(event);
|
|
1706
|
+
// A question is read whole (its options mean nothing without it); a permission's title is its tool call's, one line.
|
|
1707
|
+
const question = label === 'Answer needed';
|
|
1708
|
+
// Its questions (an agent's question tool), each drawn with its own words: no title above them.
|
|
1709
|
+
const questions = questionsAsked(event);
|
|
1710
|
+
return (
|
|
1711
|
+
<section
|
|
1712
|
+
ref={section}
|
|
1713
|
+
aria-label={label}
|
|
1714
|
+
data-who="decision"
|
|
1715
|
+
data-state="open"
|
|
1716
|
+
data-decision-row={event.seq}
|
|
1717
|
+
className={cn(
|
|
1718
|
+
'my-1 rounded-default border border-warning/40 bg-warning-subtle px-3 pb-2.5 pt-2 text-[13px]',
|
|
1719
|
+
title !== undefined && 'ml-[22px]',
|
|
1720
|
+
)}
|
|
1721
|
+
onKeyDown={(keyEvent) => {
|
|
1722
|
+
if (keyEvent.key === 'Escape' && deny && !busy && onAnswer) {
|
|
1723
|
+
keyEvent.preventDefault();
|
|
1724
|
+
answer(deny.id);
|
|
1725
|
+
}
|
|
1726
|
+
}}
|
|
1727
|
+
>
|
|
1728
|
+
<div className="flex min-w-0 items-center gap-2">
|
|
1729
|
+
<span className="size-1.5 shrink-0 rounded-full bg-warning" />
|
|
1730
|
+
<span className="shrink-0 font-medium text-warning-text">{label}</span>
|
|
1731
|
+
{!question && (
|
|
1732
|
+
<span
|
|
1733
|
+
data-title
|
|
1734
|
+
className={cn(
|
|
1735
|
+
'min-w-0 truncate text-secondary',
|
|
1736
|
+
title !== undefined && asked === title && 'sr-only',
|
|
1737
|
+
)}
|
|
1738
|
+
>
|
|
1739
|
+
{asked}
|
|
1740
|
+
</span>
|
|
1741
|
+
)}
|
|
1742
|
+
<span className="flex-1" />
|
|
1743
|
+
{event.expiresAt && <Countdown expiresAt={event.expiresAt} />}
|
|
1744
|
+
</div>
|
|
1745
|
+
{question && !questions && (
|
|
1746
|
+
<p data-title className="mt-1 whitespace-pre-wrap break-words text-foreground">
|
|
1747
|
+
{asked}
|
|
1748
|
+
</p>
|
|
1749
|
+
)}
|
|
1750
|
+
{questions && !onAnswer && (
|
|
1751
|
+
<ul data-title className="mt-1 list-none p-0 text-foreground">
|
|
1752
|
+
{questions.map((each) => (
|
|
1753
|
+
<li key={each.id} className="whitespace-pre-wrap break-words">
|
|
1754
|
+
{each.header ? `${each.header}: ` : ''}
|
|
1755
|
+
{each.question}
|
|
1756
|
+
</li>
|
|
1757
|
+
))}
|
|
1758
|
+
</ul>
|
|
1759
|
+
)}
|
|
1760
|
+
{questions && onAnswer && (
|
|
1761
|
+
<div ref={actions}>
|
|
1762
|
+
<DecisionQuestions
|
|
1763
|
+
questions={questions}
|
|
1764
|
+
busy={busy}
|
|
1765
|
+
onAnswer={answer}
|
|
1766
|
+
primary={primary}
|
|
1767
|
+
/>
|
|
1768
|
+
</div>
|
|
1769
|
+
)}
|
|
1770
|
+
{onAnswer && !questions && (
|
|
1771
|
+
<div ref={actions} className="mt-2 flex flex-wrap items-center justify-end gap-2">
|
|
1772
|
+
<span className="mr-auto hidden text-xs text-muted sm:inline">
|
|
1773
|
+
{allow && '↵ allow'}
|
|
1774
|
+
{allow && deny && ' · '}
|
|
1775
|
+
{deny && 'Esc deny'}
|
|
1776
|
+
</span>
|
|
1777
|
+
<Button size="sm" variant="ghost" disabled={busy} onClick={() => answer(null)}>
|
|
1778
|
+
Dismiss
|
|
1779
|
+
</Button>
|
|
1780
|
+
{event.options?.map((option) => (
|
|
1781
|
+
<Button
|
|
1782
|
+
key={option.id}
|
|
1783
|
+
ref={option === allow ? primary : undefined}
|
|
1784
|
+
data-primary={option === allow || undefined}
|
|
1785
|
+
data-option
|
|
1786
|
+
size="sm"
|
|
1787
|
+
variant={option === allow ? 'primary' : 'outline'}
|
|
1788
|
+
disabled={busy}
|
|
1789
|
+
onClick={() => answer(option.id)}
|
|
1790
|
+
>
|
|
1791
|
+
{option.label}
|
|
1792
|
+
</Button>
|
|
1793
|
+
))}
|
|
1794
|
+
</div>
|
|
1795
|
+
)}
|
|
1796
|
+
</section>
|
|
1797
|
+
);
|
|
1798
|
+
});
|
|
1799
|
+
|
|
1800
|
+
/** The running turn's thought: its last paragraph in view, all of it on a click. */
|
|
1801
|
+
function Thought({ text, answered }: { text: string; answered: boolean }) {
|
|
1802
|
+
const [all, setAll] = React.useState(false);
|
|
1803
|
+
const paragraphs = text
|
|
1804
|
+
.split(/\n\s*\n/)
|
|
1805
|
+
.map((each) => each.trim())
|
|
1806
|
+
.filter(Boolean);
|
|
1807
|
+
return (
|
|
1808
|
+
<button
|
|
1809
|
+
type="button"
|
|
1810
|
+
aria-expanded={all}
|
|
1811
|
+
className="my-1.5 flex cursor-pointer gap-2 font-[inherit] rounded-default border border-border-subtle bg-surface px-3 py-2 text-left text-[13px] text-secondary"
|
|
1812
|
+
onClick={() => setAll((was) => !was)}
|
|
1813
|
+
>
|
|
1814
|
+
<Icon name="brain" size={14} className="mt-0.5 shrink-0 text-muted" />
|
|
1815
|
+
<span className="sr-only">{answered ? 'Thought' : 'Thinking'}</span>
|
|
1816
|
+
<span
|
|
1817
|
+
data-who="thought"
|
|
1818
|
+
className={cn('min-w-0 whitespace-pre-wrap', !all && 'line-clamp-3')}
|
|
1819
|
+
>
|
|
1820
|
+
{all ? text : (paragraphs.at(-1) ?? text)}
|
|
1821
|
+
</span>
|
|
1822
|
+
</button>
|
|
1823
|
+
);
|
|
1824
|
+
}
|
|
1825
|
+
|
|
1826
|
+
/** The user's message: its images (`AttachedImages`), then a bubble, folded past about ten lines; its time, Copy and
|
|
1827
|
+
* Reuse on hover. */
|
|
1828
|
+
const UserMessage = React.memo(function UserMessage({
|
|
1829
|
+
event,
|
|
1830
|
+
onReuse,
|
|
1831
|
+
}: {
|
|
1832
|
+
event: Stored;
|
|
1833
|
+
onReuse: (text: string) => void;
|
|
1834
|
+
}) {
|
|
1835
|
+
const [full, setFull] = React.useState(false);
|
|
1836
|
+
const [long, setLong] = React.useState(false);
|
|
1837
|
+
const bubble = React.useRef<HTMLDivElement>(null);
|
|
1838
|
+
React.useLayoutEffect(() => {
|
|
1839
|
+
const element = bubble.current;
|
|
1840
|
+
if (element) setLong(element.scrollHeight > element.clientHeight + 4);
|
|
1841
|
+
}, [event.text]);
|
|
1842
|
+
// A child's task (bridge#212): its prompt, without Bridge's block before it, under whom it came from.
|
|
1843
|
+
const task = taskOf(event.text);
|
|
1844
|
+
const text = task?.text ?? event.text;
|
|
1845
|
+
return (
|
|
1846
|
+
<div className="group/user flex flex-col items-end pl-12">
|
|
1847
|
+
{task && <TaskFrom kind={task.kind} role={task.role} sender={event.sender} />}
|
|
1848
|
+
{event.attachments?.length ? (
|
|
1849
|
+
<div className="flex w-full justify-end">
|
|
1850
|
+
<AttachedImages images={event.attachments} />
|
|
1851
|
+
</div>
|
|
1852
|
+
) : null}
|
|
1853
|
+
{event.text ? (
|
|
1854
|
+
<div
|
|
1855
|
+
data-who="user"
|
|
1856
|
+
ref={bubble}
|
|
1857
|
+
className={cn(
|
|
1858
|
+
'relative max-w-full whitespace-pre-wrap break-words rounded-2xl rounded-br-md bg-accent-subtle px-4 py-2 text-sm text-foreground',
|
|
1859
|
+
!full && 'max-h-60 overflow-hidden',
|
|
1860
|
+
)}
|
|
1861
|
+
>
|
|
1862
|
+
{text}
|
|
1863
|
+
</div>
|
|
1864
|
+
) : null}
|
|
1865
|
+
{long && (
|
|
1866
|
+
<Button size="sm" className="mt-1 text-muted" onClick={() => setFull((was) => !was)}>
|
|
1867
|
+
{full ? 'Show less' : 'Show full message'}
|
|
1868
|
+
</Button>
|
|
1869
|
+
)}
|
|
1870
|
+
<div className="mt-0.5 flex items-center gap-0.5 text-xs text-muted opacity-0 transition-opacity focus-within:opacity-100 group-hover/user:opacity-100">
|
|
1871
|
+
<span className="mr-1 tabular-nums" title={fullDate(event.time)}>
|
|
1872
|
+
{clock(event.time)}
|
|
1873
|
+
</span>
|
|
1874
|
+
<CopyButton text={() => text ?? ''} label="Copy message" />
|
|
1875
|
+
<Tooltip>
|
|
1876
|
+
<TooltipTrigger
|
|
1877
|
+
render={
|
|
1878
|
+
<Button
|
|
1879
|
+
size="icon-sm"
|
|
1880
|
+
aria-label="Reuse"
|
|
1881
|
+
className="text-muted"
|
|
1882
|
+
onClick={() => onReuse(text ?? '')}
|
|
1883
|
+
/>
|
|
1884
|
+
}
|
|
1885
|
+
>
|
|
1886
|
+
<Icon name="corner-down-left" size={14} />
|
|
1887
|
+
</TooltipTrigger>
|
|
1888
|
+
<TooltipContent>Reuse: put it back in the composer</TooltipContent>
|
|
1889
|
+
</Tooltip>
|
|
1890
|
+
</div>
|
|
1891
|
+
</div>
|
|
1892
|
+
);
|
|
1893
|
+
});
|
|
1894
|
+
|
|
1895
|
+
/** A message Bridge itself sent the agent (what became of a change it asked for, a cut turn's continuation): a quiet
|
|
1896
|
+
* notice row naming Bridge, never the user's bubble. */
|
|
1897
|
+
function BridgeMessage({ event }: { event: Stored }) {
|
|
1898
|
+
// A follow-up from the parent, a message from another session (bridge#212): whom from, then its text.
|
|
1899
|
+
const task = taskOf(event.text);
|
|
1900
|
+
if (task)
|
|
1901
|
+
return (
|
|
1902
|
+
<div data-who="bridge" title={fullDate(event.time)} className="flex flex-col py-1">
|
|
1903
|
+
<TaskFrom kind={task.kind} sender={event.sender} />
|
|
1904
|
+
<p className="min-w-0 whitespace-pre-wrap break-words text-sm text-secondary">
|
|
1905
|
+
{task.text}
|
|
1906
|
+
</p>
|
|
1907
|
+
</div>
|
|
1908
|
+
);
|
|
1909
|
+
return (
|
|
1910
|
+
<p
|
|
1911
|
+
data-who="bridge"
|
|
1912
|
+
title={fullDate(event.time)}
|
|
1913
|
+
className="flex items-start gap-1.5 py-1 text-xs text-muted"
|
|
1914
|
+
>
|
|
1915
|
+
<Icon name="bell" size={12} className="mt-0.5 shrink-0" />
|
|
1916
|
+
<span className="shrink-0 font-medium text-secondary">Bridge</span>
|
|
1917
|
+
<span className="min-w-0 break-words">{event.text}</span>
|
|
1918
|
+
</p>
|
|
1919
|
+
);
|
|
1920
|
+
}
|
|
1921
|
+
|
|
1922
|
+
/** A prompt a scheduled task sent: one compact row naming its task, the prompt on a click; its time and Copy on hover. */
|
|
1923
|
+
const AutomationMessage = React.memo(function AutomationMessage({ event }: { event: Stored }) {
|
|
1924
|
+
const [open, setOpen] = React.useState(false);
|
|
1925
|
+
const body = React.useId();
|
|
1926
|
+
return (
|
|
1927
|
+
<div className="group/user flex flex-col items-end pl-12">
|
|
1928
|
+
<div
|
|
1929
|
+
data-who="automation"
|
|
1930
|
+
className={cn(
|
|
1931
|
+
'min-w-0 max-w-full overflow-hidden rounded-2xl rounded-br-md border border-border-subtle bg-surface text-sm',
|
|
1932
|
+
open && 'w-full',
|
|
1933
|
+
)}
|
|
1934
|
+
>
|
|
1935
|
+
<button
|
|
1936
|
+
type="button"
|
|
1937
|
+
aria-expanded={open}
|
|
1938
|
+
aria-controls={body}
|
|
1939
|
+
className="flex w-full min-w-0 cursor-pointer items-center gap-2 border-0 bg-transparent px-3.5 py-2 text-left font-[inherit] text-secondary hover:text-foreground"
|
|
1940
|
+
onClick={() => setOpen((was) => !was)}
|
|
1941
|
+
>
|
|
1942
|
+
<Icon name="alarm-clock" size={14} className="shrink-0 text-muted" />
|
|
1943
|
+
<span className="shrink-0 text-muted">Scheduled task</span>
|
|
1944
|
+
<span data-title className="min-w-0 flex-1 truncate text-foreground">
|
|
1945
|
+
{event.scheduled?.title}
|
|
1946
|
+
</span>
|
|
1947
|
+
<Icon
|
|
1948
|
+
name="chevron-down"
|
|
1949
|
+
size={14}
|
|
1950
|
+
className={cn('shrink-0 text-muted transition-transform', open && 'rotate-180')}
|
|
1951
|
+
/>
|
|
1952
|
+
</button>
|
|
1953
|
+
{open && (
|
|
1954
|
+
<div
|
|
1955
|
+
id={body}
|
|
1956
|
+
data-who="automation-prompt"
|
|
1957
|
+
className="max-h-96 overflow-auto whitespace-pre-wrap break-words border-t border-border-subtle px-3.5 py-2.5 text-foreground"
|
|
1958
|
+
>
|
|
1959
|
+
{event.text}
|
|
1960
|
+
</div>
|
|
1961
|
+
)}
|
|
1962
|
+
</div>
|
|
1963
|
+
<div className="mt-0.5 flex items-center gap-0.5 text-xs text-muted opacity-0 transition-opacity focus-within:opacity-100 group-hover/user:opacity-100">
|
|
1964
|
+
<span className="mr-1 tabular-nums" title={fullDate(event.time)}>
|
|
1965
|
+
{clock(event.time)}
|
|
1966
|
+
</span>
|
|
1967
|
+
<CopyButton text={() => event.text ?? ''} label="Copy prompt" />
|
|
1968
|
+
</div>
|
|
1969
|
+
</div>
|
|
1970
|
+
);
|
|
1971
|
+
});
|
|
1972
|
+
|
|
1973
|
+
/** The running turn's plan above the composer: its progress on one line, the steps on a click. */
|
|
1974
|
+
export function Tasks({ plan }: { plan: { content: string; status: string }[] }) {
|
|
1975
|
+
const [open, setOpen] = React.useState(false);
|
|
1976
|
+
// A cancelled step (ACP's newer schema) is dropped from the progress: neither current nor counted.
|
|
1977
|
+
const steps = plan.filter((entry) => entry.status !== 'cancelled');
|
|
1978
|
+
const done = steps.filter((entry) => entry.status === 'completed').length;
|
|
1979
|
+
const current =
|
|
1980
|
+
steps.find((entry) => entry.status === 'in_progress') ??
|
|
1981
|
+
steps.find((entry) => entry.status !== 'completed');
|
|
1982
|
+
return (
|
|
1983
|
+
<div
|
|
1984
|
+
data-who="plan"
|
|
1985
|
+
className="mx-3 rounded-t-dialog border border-b-0 border-border bg-surface text-[12.5px]"
|
|
1986
|
+
>
|
|
1987
|
+
<button
|
|
1988
|
+
type="button"
|
|
1989
|
+
aria-expanded={open}
|
|
1990
|
+
className="flex h-9 w-full cursor-pointer items-center gap-2 border-0 bg-transparent px-3 text-left font-[inherit] text-secondary"
|
|
1991
|
+
onClick={() => setOpen((was) => !was)}
|
|
1992
|
+
>
|
|
1993
|
+
<Icon name="list-checks" size={14} className="shrink-0 text-muted" />
|
|
1994
|
+
<span className="shrink-0">
|
|
1995
|
+
Tasks · {Math.min(done + 1, steps.length)} of {steps.length}
|
|
1996
|
+
</span>
|
|
1997
|
+
<span className="flex w-28 shrink-0 gap-0.5" aria-hidden="true">
|
|
1998
|
+
{steps.map((entry, index) => (
|
|
1999
|
+
<span
|
|
2000
|
+
key={index}
|
|
2001
|
+
className={cn(
|
|
2002
|
+
'h-1 flex-1 rounded-full',
|
|
2003
|
+
entry.status === 'completed'
|
|
2004
|
+
? 'bg-success'
|
|
2005
|
+
: entry.status === 'in_progress'
|
|
2006
|
+
? 'bg-accent'
|
|
2007
|
+
: 'bg-border',
|
|
2008
|
+
)}
|
|
2009
|
+
/>
|
|
2010
|
+
))}
|
|
2011
|
+
</span>
|
|
2012
|
+
<span className="min-w-0 truncate text-foreground">{current?.content}</span>
|
|
2013
|
+
<Icon
|
|
2014
|
+
name="chevron-up"
|
|
2015
|
+
size={14}
|
|
2016
|
+
className={cn('ml-auto shrink-0 text-muted transition-transform', open && 'rotate-180')}
|
|
2017
|
+
/>
|
|
2018
|
+
</button>
|
|
2019
|
+
{open && (
|
|
2020
|
+
<ol aria-label="Plan" className="grid max-h-56 gap-1.5 overflow-auto px-3 pb-3">
|
|
2021
|
+
{plan.map((entry, index) => (
|
|
2022
|
+
<li key={index} data-status={entry.status} className="flex items-center gap-2 text-sm">
|
|
2023
|
+
{entry.status === 'completed' ? (
|
|
2024
|
+
<span className="flex size-4 shrink-0 items-center justify-center rounded-full bg-success text-success-foreground">
|
|
2025
|
+
<Icon name="check" size={11} strokeWidth={3} />
|
|
2026
|
+
</span>
|
|
2027
|
+
) : entry.status === 'in_progress' ? (
|
|
2028
|
+
<Spinner
|
|
2029
|
+
label="In progress"
|
|
2030
|
+
className="size-4 items-center justify-center text-accent"
|
|
2031
|
+
/>
|
|
2032
|
+
) : entry.status === 'cancelled' ? (
|
|
2033
|
+
<span className="flex size-4 shrink-0 items-center justify-center text-muted">
|
|
2034
|
+
<Icon name="x" size={12} />
|
|
2035
|
+
</span>
|
|
2036
|
+
) : (
|
|
2037
|
+
<span className="size-4 shrink-0 rounded-full border-[1.5px] border-muted" />
|
|
2038
|
+
)}
|
|
2039
|
+
<span
|
|
2040
|
+
className={
|
|
2041
|
+
entry.status === 'completed' || entry.status === 'cancelled'
|
|
2042
|
+
? 'text-muted line-through'
|
|
2043
|
+
: 'text-foreground'
|
|
2044
|
+
}
|
|
2045
|
+
>
|
|
2046
|
+
{entry.content}
|
|
2047
|
+
</span>
|
|
2048
|
+
</li>
|
|
2049
|
+
))}
|
|
2050
|
+
</ol>
|
|
2051
|
+
)}
|
|
2052
|
+
</div>
|
|
2053
|
+
);
|
|
2054
|
+
}
|
|
2055
|
+
|
|
2056
|
+
/** An open decision, docked on the composer. Its row in the timeline (`onRow`, out of view) holds its options: the dock
|
|
2057
|
+
* only points to it, Show scrolling there. Else (its row not drawn: its turn begun on an older page) what is asked, its
|
|
2058
|
+
* options (the allow one focused when nothing else is), Dismiss, and its countdown; Escape picks its deny option. */
|
|
2059
|
+
export function DecisionDock({
|
|
2060
|
+
event,
|
|
2061
|
+
busy,
|
|
2062
|
+
more = 0,
|
|
2063
|
+
relative = (text) => text,
|
|
2064
|
+
onAnswer,
|
|
2065
|
+
onRow = false,
|
|
2066
|
+
}: {
|
|
2067
|
+
event: Stored;
|
|
2068
|
+
busy: boolean;
|
|
2069
|
+
more?: number;
|
|
2070
|
+
relative?: (text: string) => string;
|
|
2071
|
+
onAnswer: (option: Answer) => void;
|
|
2072
|
+
onRow?: boolean;
|
|
2073
|
+
}) {
|
|
2074
|
+
const primary = React.useRef<HTMLButtonElement>(null);
|
|
2075
|
+
React.useEffect(() => {
|
|
2076
|
+
const active = document.activeElement;
|
|
2077
|
+
if (!onRow && (!active || active === document.body)) primary.current?.focus();
|
|
2078
|
+
}, [event.seq, onRow]);
|
|
2079
|
+
if (onRow)
|
|
2080
|
+
return (
|
|
2081
|
+
<section
|
|
2082
|
+
aria-label="Waiting above"
|
|
2083
|
+
data-who="decision-pointer"
|
|
2084
|
+
className="flex h-9 items-center gap-2 rounded-t-dialog border border-b-0 border-warning/50 bg-warning-subtle pl-3.5 pr-1.5 text-[12.5px]"
|
|
2085
|
+
>
|
|
2086
|
+
<span className="size-1.5 shrink-0 rounded-full bg-warning" />
|
|
2087
|
+
<span className="shrink-0 font-semibold text-warning-text">{decisionLabel(event)}</span>
|
|
2088
|
+
<span className="min-w-0 truncate font-mono text-xs text-secondary">
|
|
2089
|
+
{relative(event.title ?? '')}
|
|
2090
|
+
</span>
|
|
2091
|
+
{more > 0 && <span className="shrink-0 text-xs text-muted">+{more} more</span>}
|
|
2092
|
+
<span className="flex-1" />
|
|
2093
|
+
<Button
|
|
2094
|
+
size="sm"
|
|
2095
|
+
className="shrink-0 gap-1"
|
|
2096
|
+
onClick={() => {
|
|
2097
|
+
const row = document.querySelector<HTMLElement>(`[data-decision-row="${event.seq}"]`);
|
|
2098
|
+
row?.scrollIntoView?.({ block: 'center', behavior: 'smooth' });
|
|
2099
|
+
// Its allow option, else its first one (a question's).
|
|
2100
|
+
(
|
|
2101
|
+
row?.querySelector<HTMLElement>('[data-primary]') ??
|
|
2102
|
+
row?.querySelector<HTMLElement>('[data-option]')
|
|
2103
|
+
)?.focus({ preventScroll: true });
|
|
2104
|
+
}}
|
|
2105
|
+
>
|
|
2106
|
+
Show
|
|
2107
|
+
<Icon name="arrow-up" size={13} />
|
|
2108
|
+
</Button>
|
|
2109
|
+
</section>
|
|
2110
|
+
);
|
|
2111
|
+
const deny = event.options?.find((option) => option.kind === 'deny');
|
|
2112
|
+
const allow = event.options?.find((option) => option.kind === 'allow');
|
|
2113
|
+
const questions = questionsAsked(event);
|
|
2114
|
+
if (questions)
|
|
2115
|
+
return (
|
|
2116
|
+
<section
|
|
2117
|
+
aria-label={decisionLabel(event)}
|
|
2118
|
+
data-who="decision-dock"
|
|
2119
|
+
className="max-h-[50vh] overflow-y-auto rounded-t-dialog border border-b-0 border-warning/50 bg-warning-subtle px-3.5 pb-3 pt-2.5 text-[13px]"
|
|
2120
|
+
>
|
|
2121
|
+
<div className="flex items-center gap-2 text-[12.5px]">
|
|
2122
|
+
<span className="size-1.5 shrink-0 rounded-full bg-warning" />
|
|
2123
|
+
<span className="font-semibold text-warning-text">{decisionLabel(event)}</span>
|
|
2124
|
+
{more > 0 && <span className="text-xs text-muted">+{more} more waiting</span>}
|
|
2125
|
+
<span className="flex-1" />
|
|
2126
|
+
{event.expiresAt && <Countdown expiresAt={event.expiresAt} />}
|
|
2127
|
+
</div>
|
|
2128
|
+
<DecisionQuestions
|
|
2129
|
+
questions={questions}
|
|
2130
|
+
busy={busy}
|
|
2131
|
+
onAnswer={onAnswer}
|
|
2132
|
+
primary={primary}
|
|
2133
|
+
/>
|
|
2134
|
+
</section>
|
|
2135
|
+
);
|
|
2136
|
+
return (
|
|
2137
|
+
<section
|
|
2138
|
+
aria-label={decisionLabel(event)}
|
|
2139
|
+
data-who="decision-dock"
|
|
2140
|
+
className="rounded-t-dialog border border-b-0 border-warning/50 bg-warning-subtle px-3.5 pb-3 pt-2.5"
|
|
2141
|
+
onKeyDown={(keyEvent) => {
|
|
2142
|
+
if (keyEvent.key === 'Escape' && deny && !busy) {
|
|
2143
|
+
keyEvent.preventDefault();
|
|
2144
|
+
onAnswer(deny.id);
|
|
2145
|
+
}
|
|
2146
|
+
}}
|
|
2147
|
+
>
|
|
2148
|
+
<div className="flex items-center gap-2 text-[12.5px]">
|
|
2149
|
+
<span className="size-1.5 shrink-0 rounded-full bg-warning" />
|
|
2150
|
+
<span className="font-semibold text-warning-text">{decisionLabel(event)}</span>
|
|
2151
|
+
{more > 0 && <span className="text-xs text-muted">+{more} more waiting</span>}
|
|
2152
|
+
<span className="flex-1" />
|
|
2153
|
+
{event.expiresAt && <Countdown expiresAt={event.expiresAt} />}
|
|
2154
|
+
</div>
|
|
2155
|
+
<p
|
|
2156
|
+
data-title
|
|
2157
|
+
className="mt-1.5 break-words rounded-default border border-border-subtle bg-surface px-2.5 py-1.5 font-mono text-xs text-foreground"
|
|
2158
|
+
>
|
|
2159
|
+
{relative(event.title ?? '')}
|
|
2160
|
+
</p>
|
|
2161
|
+
<div className="mt-2.5 flex flex-wrap items-center justify-end gap-2">
|
|
2162
|
+
<span className="mr-auto hidden text-xs text-muted sm:inline">
|
|
2163
|
+
{allow && '↵ allow'}
|
|
2164
|
+
{allow && deny && ' · '}
|
|
2165
|
+
{deny && 'Esc deny'}
|
|
2166
|
+
</span>
|
|
2167
|
+
<Button variant="ghost" disabled={busy} onClick={() => onAnswer(null)}>
|
|
2168
|
+
Dismiss
|
|
2169
|
+
</Button>
|
|
2170
|
+
{event.options?.map((option) => (
|
|
2171
|
+
<Button
|
|
2172
|
+
key={option.id}
|
|
2173
|
+
ref={option === allow ? primary : undefined}
|
|
2174
|
+
variant={option === allow ? 'primary' : 'outline'}
|
|
2175
|
+
disabled={busy}
|
|
2176
|
+
onClick={() => onAnswer(option.id)}
|
|
2177
|
+
>
|
|
2178
|
+
{option.label}
|
|
2179
|
+
</Button>
|
|
2180
|
+
))}
|
|
2181
|
+
</div>
|
|
2182
|
+
</section>
|
|
2183
|
+
);
|
|
2184
|
+
}
|
|
2185
|
+
|
|
2186
|
+
/** A message in a session's queue: the user's, or a scheduled task's run (`scheduled`, its task). */
|
|
2187
|
+
export interface QueuedItem {
|
|
2188
|
+
id: string;
|
|
2189
|
+
text: string;
|
|
2190
|
+
at: string;
|
|
2191
|
+
attachments?: unknown[];
|
|
2192
|
+
scheduled?: { id: string; title: string };
|
|
2193
|
+
from?: 'bridge';
|
|
2194
|
+
/** A child's results waiting (bridge#212): their delegations. */
|
|
2195
|
+
delegations?: string[];
|
|
2196
|
+
/** A child's questions waiting: the child asking each. */
|
|
2197
|
+
questions?: { session: string }[];
|
|
2198
|
+
}
|
|
2199
|
+
/** A note another session left for this one's next turn (its inbox): who, and where a click on its name goes. */
|
|
2200
|
+
export interface QueuedNote {
|
|
2201
|
+
from: string;
|
|
2202
|
+
title: string;
|
|
2203
|
+
text: string;
|
|
2204
|
+
at: string;
|
|
2205
|
+
to: ScreenLinkProps['to'];
|
|
2206
|
+
}
|
|
2207
|
+
|
|
2208
|
+
/** Who sent a waiting message, before its text: you, Bridge, a scheduled task, or another session (a link to it). */
|
|
2209
|
+
function Sender({ item, note }: { item?: QueuedItem; note?: QueuedNote }) {
|
|
2210
|
+
if (note)
|
|
2211
|
+
return (
|
|
2212
|
+
<span data-sender="session" className="flex min-w-0 max-w-[40%] shrink-0 items-center gap-1">
|
|
2213
|
+
<span className="shrink-0 text-muted">From</span>
|
|
2214
|
+
<ScreenLink
|
|
2215
|
+
to={note.to}
|
|
2216
|
+
className="truncate text-secondary underline-offset-2 hover:text-foreground hover:underline"
|
|
2217
|
+
>
|
|
2218
|
+
{note.title || 'New chat'}
|
|
2219
|
+
</ScreenLink>
|
|
2220
|
+
</span>
|
|
2221
|
+
);
|
|
2222
|
+
if (item?.from === 'bridge' && (item.questions?.length || item.delegations?.length))
|
|
2223
|
+
return <ChildSender item={item} />;
|
|
2224
|
+
if (item?.from === 'bridge')
|
|
2225
|
+
return (
|
|
2226
|
+
<span data-sender="bridge" className="shrink-0 text-muted">
|
|
2227
|
+
Bridge
|
|
2228
|
+
</span>
|
|
2229
|
+
);
|
|
2230
|
+
if (item?.scheduled)
|
|
2231
|
+
return (
|
|
2232
|
+
<span
|
|
2233
|
+
data-sender="scheduled"
|
|
2234
|
+
className="flex min-w-0 max-w-[40%] shrink-0 items-center gap-1 text-muted"
|
|
2235
|
+
>
|
|
2236
|
+
<span className="shrink-0">Scheduled task</span>
|
|
2237
|
+
<span className="truncate text-secondary">{item.scheduled.title}</span>
|
|
2238
|
+
</span>
|
|
2239
|
+
);
|
|
2240
|
+
return (
|
|
2241
|
+
<span data-sender="you" className="shrink-0 text-muted">
|
|
2242
|
+
You
|
|
2243
|
+
</span>
|
|
2244
|
+
);
|
|
2245
|
+
}
|
|
2246
|
+
|
|
2247
|
+
/** Who a child's question or result waiting in its parent's queue is from (bridge#212): that child, a link to it. */
|
|
2248
|
+
function ChildSender({ item }: { item: QueuedItem }) {
|
|
2249
|
+
const lineage = React.useContext(LineageContext);
|
|
2250
|
+
const asking = item.questions?.[0]?.session;
|
|
2251
|
+
const child =
|
|
2252
|
+
asking ??
|
|
2253
|
+
lineage?.delegations.find((each) => each.id === item.delegations?.[0])?.child ??
|
|
2254
|
+
undefined;
|
|
2255
|
+
const title = (child && lineage?.sessions.get(child)?.title) || 'a child session';
|
|
2256
|
+
return (
|
|
2257
|
+
<span data-sender="child" className="flex min-w-0 max-w-[40%] shrink-0 items-center gap-1">
|
|
2258
|
+
<span className="shrink-0 text-muted">{asking ? 'Question from' : 'Result from'}</span>
|
|
2259
|
+
{child && lineage ? (
|
|
2260
|
+
<ScreenLink
|
|
2261
|
+
to={lineage.to(child)}
|
|
2262
|
+
className="truncate text-secondary underline-offset-2 hover:text-foreground hover:underline"
|
|
2263
|
+
>
|
|
2264
|
+
{title}
|
|
2265
|
+
</ScreenLink>
|
|
2266
|
+
) : (
|
|
2267
|
+
<span className="truncate text-secondary">{title}</span>
|
|
2268
|
+
)}
|
|
2269
|
+
</span>
|
|
2270
|
+
);
|
|
2271
|
+
}
|
|
2272
|
+
|
|
2273
|
+
/** Messages waiting for this session's next turn, above the composer, each saying who sent it: the notes other sessions
|
|
2274
|
+
* left (read at the start of its next turn, by its agent alone), then the queue (each sent as a turn of its own after
|
|
2275
|
+
* the running one): Send now (into the running turn when its harness steers, else it stops the turn), Edit (back to
|
|
2276
|
+
* the composer), Remove. */
|
|
2277
|
+
export function Queued({
|
|
2278
|
+
items,
|
|
2279
|
+
notes = [],
|
|
2280
|
+
steers = false,
|
|
2281
|
+
onSend,
|
|
2282
|
+
onEdit,
|
|
2283
|
+
onRemove,
|
|
2284
|
+
}: {
|
|
2285
|
+
items: QueuedItem[];
|
|
2286
|
+
notes?: QueuedNote[];
|
|
2287
|
+
/** The session's harness steers a running turn (bridge#212 S6): Send now puts the message into it. */
|
|
2288
|
+
steers?: boolean;
|
|
2289
|
+
onSend: (item: string) => void;
|
|
2290
|
+
onEdit: (item: { id: string; text: string }) => void;
|
|
2291
|
+
onRemove: (item: string) => void;
|
|
2292
|
+
}) {
|
|
2293
|
+
const count = notes.length + items.length;
|
|
2294
|
+
if (!count) return null;
|
|
2295
|
+
const counted = (index: number) =>
|
|
2296
|
+
index === 0 && <span className="shrink-0 text-muted">{count} queued</span>;
|
|
2297
|
+
return (
|
|
2298
|
+
<section
|
|
2299
|
+
aria-label="Queued messages"
|
|
2300
|
+
data-who="queue"
|
|
2301
|
+
className="mx-3 rounded-t-dialog border border-b-0 border-border bg-surface py-1 text-[12.5px]"
|
|
2302
|
+
>
|
|
2303
|
+
{notes.map((note, index) => (
|
|
2304
|
+
<div
|
|
2305
|
+
key={note.at + note.from}
|
|
2306
|
+
data-note={note.from}
|
|
2307
|
+
title="Its agent reads it at the start of its next turn"
|
|
2308
|
+
className="flex h-8 items-center gap-2 pl-3 pr-1.5 text-secondary"
|
|
2309
|
+
>
|
|
2310
|
+
<Icon name="mail" size={14} className="shrink-0 text-muted" />
|
|
2311
|
+
{counted(index)}
|
|
2312
|
+
<Sender note={note} />
|
|
2313
|
+
<span className="min-w-0 flex-1 truncate text-foreground">{oneLine(note.text, 200)}</span>
|
|
2314
|
+
</div>
|
|
2315
|
+
))}
|
|
2316
|
+
{items.map((item, index) => (
|
|
2317
|
+
<div
|
|
2318
|
+
key={item.id}
|
|
2319
|
+
data-queued={item.id}
|
|
2320
|
+
className="flex h-8 items-center gap-2 pl-3 pr-1.5 text-secondary"
|
|
2321
|
+
>
|
|
2322
|
+
<Icon
|
|
2323
|
+
name={item.scheduled ? 'alarm-clock' : 'list-plus'}
|
|
2324
|
+
size={14}
|
|
2325
|
+
className="shrink-0 text-muted"
|
|
2326
|
+
/>
|
|
2327
|
+
{counted(notes.length + index)}
|
|
2328
|
+
<Sender item={item} />
|
|
2329
|
+
<span className="min-w-0 flex-1 truncate text-foreground">{oneLine(item.text, 200)}</span>
|
|
2330
|
+
{item.attachments?.length ? (
|
|
2331
|
+
<span className="flex shrink-0 items-center gap-1 text-muted">
|
|
2332
|
+
<Icon name="image" size={13} />
|
|
2333
|
+
{item.attachments.length}
|
|
2334
|
+
</span>
|
|
2335
|
+
) : null}
|
|
2336
|
+
<Button
|
|
2337
|
+
size="sm"
|
|
2338
|
+
onClick={() => onSend(item.id)}
|
|
2339
|
+
title={
|
|
2340
|
+
steers
|
|
2341
|
+
? 'Send this into the running turn now'
|
|
2342
|
+
: 'Stop the running turn and send this now'
|
|
2343
|
+
}
|
|
2344
|
+
>
|
|
2345
|
+
Send now
|
|
2346
|
+
</Button>
|
|
2347
|
+
<Tooltip>
|
|
2348
|
+
<TooltipTrigger
|
|
2349
|
+
render={
|
|
2350
|
+
<Button
|
|
2351
|
+
size="icon-sm"
|
|
2352
|
+
aria-label="Edit"
|
|
2353
|
+
className="text-muted"
|
|
2354
|
+
onClick={() => onEdit(item)}
|
|
2355
|
+
/>
|
|
2356
|
+
}
|
|
2357
|
+
>
|
|
2358
|
+
<Icon name="square-pen" size={14} />
|
|
2359
|
+
</TooltipTrigger>
|
|
2360
|
+
<TooltipContent>Edit: back to the composer</TooltipContent>
|
|
2361
|
+
</Tooltip>
|
|
2362
|
+
<Tooltip>
|
|
2363
|
+
<TooltipTrigger
|
|
2364
|
+
render={
|
|
2365
|
+
<Button
|
|
2366
|
+
size="icon-sm"
|
|
2367
|
+
aria-label="Remove"
|
|
2368
|
+
className="text-muted"
|
|
2369
|
+
onClick={() => onRemove(item.id)}
|
|
2370
|
+
/>
|
|
2371
|
+
}
|
|
2372
|
+
>
|
|
2373
|
+
<Icon name="x" size={14} />
|
|
2374
|
+
</TooltipTrigger>
|
|
2375
|
+
<TooltipContent>Remove</TooltipContent>
|
|
2376
|
+
</Tooltip>
|
|
2377
|
+
</div>
|
|
2378
|
+
))}
|
|
2379
|
+
</section>
|
|
2380
|
+
);
|
|
2381
|
+
}
|