@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,650 @@
|
|
|
1
|
+
// Child sessions in the session views (bridge#212 S3, research-082 section 8, UI): what a session delegated to its
|
|
2
|
+
// children (its `delegations`, `delegations.ts`) and its place in the lineage, drawn from the listing.
|
|
3
|
+
// - The parent's timeline: the children one turn delegated are one group of cards after that turn's work
|
|
4
|
+
// (`ChildCards`), in view whether the work is folded or not; each card says its title, its status (a spinner while it runs, "Waiting
|
|
5
|
+
// for you" while its child waits on a decision, then a ✓ or ! disc), its progress or its result's first line, and its
|
|
6
|
+
// elapsed time, and opens its child (a link: the shell's navigation, Back returns). A delegation goes to the turn
|
|
7
|
+
// whose `delegate_session` call made it (its `clientRequestId` in the call's input, or its id in the call's output),
|
|
8
|
+
// else to the last turn started before it (`cardsByTurn`); the call's own tool row is hidden once its card is there
|
|
9
|
+
// (`hiddenCalls`).
|
|
10
|
+
// - Bridge's message delivering children's results or questions (`user/message {from: 'bridge', delegations,
|
|
11
|
+
// questions}`) is one block (`ChildrenMessage`): a row per child (its status and a link to it), a row per question
|
|
12
|
+
// (the child asking it, Open), the message's text a click away.
|
|
13
|
+
// - The background-work banner (`BackgroundWork`), docked on the composer as the queue is: each running child, what it
|
|
14
|
+
// does (working, waiting for you) and since when, Open and Stop. Stop is the session Stop on the child (`POST
|
|
15
|
+
// /sessions/interrupt`, the same as the child's own Stop), not `cancel_delegation`: the child's delegation then ends
|
|
16
|
+
// `interrupted` and its result (its last words) still reaches the parent's agent, which so learns the user stopped
|
|
17
|
+
// it; a cancel is the agent's own way to drop work and tells it nothing.
|
|
18
|
+
// - Links: a child's view names its parent (`ParentLink`) and keeps its prompt (`TaskMessage`: the delegation's
|
|
19
|
+
// `<bridge_delegation>` block dropped, "Task from …" over it); Bridge's messages with a `sender` say whom from.
|
|
20
|
+
// The page gives the listing's sessions, where a link goes and what Stop calls through `LineageContext`.
|
|
21
|
+
import * as React from 'react';
|
|
22
|
+
import {
|
|
23
|
+
Button,
|
|
24
|
+
Card,
|
|
25
|
+
Icon,
|
|
26
|
+
ScreenLink,
|
|
27
|
+
StatusIcon,
|
|
28
|
+
type FollowTarget,
|
|
29
|
+
cn,
|
|
30
|
+
formatDate,
|
|
31
|
+
} from '@bridge/ui';
|
|
32
|
+
import { oneLine } from '@bridge/session-ui/one-line';
|
|
33
|
+
import { Elapsed, Spinner, duration } from './elapsed.tsx';
|
|
34
|
+
import type { ChildState } from '../listing.ts';
|
|
35
|
+
|
|
36
|
+
/** A delegation as the parent's record keeps it (`delegations.ts`, `delegation`). */
|
|
37
|
+
export interface Delegation {
|
|
38
|
+
id: string;
|
|
39
|
+
child?: string | undefined;
|
|
40
|
+
title: string;
|
|
41
|
+
clientRequestId?: string | undefined;
|
|
42
|
+
status: 'running' | 'completed' | 'failed' | 'interrupted' | 'cancelled';
|
|
43
|
+
createdAt: string;
|
|
44
|
+
endedAt?: string | undefined;
|
|
45
|
+
result?: string | undefined;
|
|
46
|
+
error?: string | undefined;
|
|
47
|
+
delivery?: 'queued' | 'sending' | 'sent' | 'read' | 'dropped' | undefined;
|
|
48
|
+
/** Its child's worktree is being made (S7): its task not sent yet. */
|
|
49
|
+
preparing?: true | undefined;
|
|
50
|
+
}
|
|
51
|
+
/** A listed session, as the lineage reads it: its title, where it is, what it does. */
|
|
52
|
+
export interface Kin {
|
|
53
|
+
id: string;
|
|
54
|
+
node: string;
|
|
55
|
+
title: string;
|
|
56
|
+
parent?: string | undefined;
|
|
57
|
+
running?: boolean | undefined;
|
|
58
|
+
waiting?: boolean | undefined;
|
|
59
|
+
}
|
|
60
|
+
/** What the page gives the views of its sessions' lineage. */
|
|
61
|
+
export interface Lineage {
|
|
62
|
+
/** The listed sessions, by id. */
|
|
63
|
+
sessions: ReadonlyMap<string, Kin>;
|
|
64
|
+
/** The shown session's delegations: its cards, its results' messages. */
|
|
65
|
+
delegations: readonly Delegation[];
|
|
66
|
+
/** The shown session's parent, when it is a child: its task came from there. */
|
|
67
|
+
parent?: string | undefined;
|
|
68
|
+
/** Where a link to session `id` goes. */
|
|
69
|
+
to: (id: string) => FollowTarget;
|
|
70
|
+
/** Stops child session `id` (its session Stop); without it, no Stop. */
|
|
71
|
+
stop?: ((id: string) => void) | undefined;
|
|
72
|
+
/** Where a link to the shown session's sub-agent `id` goes (bridge#212 S8, ./subagents.tsx); without it, none. */
|
|
73
|
+
subagentTo?: ((id: string) => FollowTarget) | undefined;
|
|
74
|
+
/** Stops the shown session's sub-agent `id` through its harness; without it, no Stop. */
|
|
75
|
+
stopSubagent?: ((id: string) => void) | undefined;
|
|
76
|
+
}
|
|
77
|
+
export const LineageContext = React.createContext<Lineage | null>(null);
|
|
78
|
+
const NO_DELEGATIONS: readonly Delegation[] = [];
|
|
79
|
+
|
|
80
|
+
/** The lines of a timeline these helpers read (`Stored`, ./conversation.tsx). */
|
|
81
|
+
interface Line {
|
|
82
|
+
seq: number;
|
|
83
|
+
type: string;
|
|
84
|
+
time?: string | undefined;
|
|
85
|
+
title?: string | undefined;
|
|
86
|
+
input?: unknown;
|
|
87
|
+
output?: unknown;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** A `delegate_session` call, by its title as the harness names a tool of the `bridge` MCP server. */
|
|
91
|
+
export const isDelegateCall = (line: Line) =>
|
|
92
|
+
line.type === 'tool/call' &&
|
|
93
|
+
/^(?:mcp__bridge__|bridge · )?delegate_session(?:\s|$)/.test(line.title ?? '');
|
|
94
|
+
|
|
95
|
+
/** A call's input as a record (an agent's arguments, or their JSON). */
|
|
96
|
+
const recordOf = (value: unknown): Record<string, unknown> | undefined => {
|
|
97
|
+
if (typeof value === 'string') {
|
|
98
|
+
try {
|
|
99
|
+
return recordOf(JSON.parse(value) as unknown);
|
|
100
|
+
} catch {
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
105
|
+
? Object.fromEntries(Object.entries(value))
|
|
106
|
+
: undefined;
|
|
107
|
+
};
|
|
108
|
+
/** The delegation `call` made (`result`: that call's `tool/result`), when one of `delegations` is it: the same
|
|
109
|
+
* `clientRequestId` in its input, or its id in what it answered. */
|
|
110
|
+
export const delegationOfCall = (
|
|
111
|
+
call: Line,
|
|
112
|
+
result: Line | undefined,
|
|
113
|
+
delegations: readonly Delegation[],
|
|
114
|
+
) => {
|
|
115
|
+
if (!isDelegateCall(call)) return undefined;
|
|
116
|
+
const key = recordOf(call.input)?.clientRequestId;
|
|
117
|
+
const answered = (() => {
|
|
118
|
+
const output = result?.output ?? call.output;
|
|
119
|
+
if (output === undefined) return '';
|
|
120
|
+
return typeof output === 'string' ? output : JSON.stringify(output);
|
|
121
|
+
})();
|
|
122
|
+
return delegations.find(
|
|
123
|
+
(each) =>
|
|
124
|
+
(typeof key === 'string' && key === each.clientRequestId) ||
|
|
125
|
+
(answered !== '' && answered.includes(each.id)),
|
|
126
|
+
);
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/** The turns' delegations, by turn key: each in the turn whose `delegate_session` call made it, else in the last turn
|
|
130
|
+
* started at or before it; one older than the first turn here (`cut`: an earlier page holds it) is in none. */
|
|
131
|
+
export const cardsByTurn = (
|
|
132
|
+
turns: { key: number; start?: Line | undefined; items: Line[] }[],
|
|
133
|
+
delegations: readonly Delegation[],
|
|
134
|
+
results: ReadonlyMap<string | undefined, Line>,
|
|
135
|
+
cut: boolean,
|
|
136
|
+
): Map<number, Delegation[]> => {
|
|
137
|
+
const byTurn = new Map<number, Delegation[]>();
|
|
138
|
+
if (!delegations.length) return byTurn;
|
|
139
|
+
const placed = new Set<string>();
|
|
140
|
+
const add = (key: number, each: Delegation) => {
|
|
141
|
+
placed.add(each.id);
|
|
142
|
+
byTurn.set(key, [...(byTurn.get(key) ?? []), each]);
|
|
143
|
+
};
|
|
144
|
+
for (const turn of turns)
|
|
145
|
+
for (const line of turn.items) {
|
|
146
|
+
const made =
|
|
147
|
+
line.type === 'tool/call'
|
|
148
|
+
? delegationOfCall(line, results.get((line as { id?: string }).id), delegations)
|
|
149
|
+
: undefined;
|
|
150
|
+
if (made && !placed.has(made.id)) add(turn.key, made);
|
|
151
|
+
}
|
|
152
|
+
const started = turns.filter((turn) => turn.start?.time);
|
|
153
|
+
for (const each of [...delegations].sort((a, b) => a.createdAt.localeCompare(b.createdAt))) {
|
|
154
|
+
if (placed.has(each.id)) continue;
|
|
155
|
+
const turn =
|
|
156
|
+
started.findLast((one) => one.start!.time! <= each.createdAt) ??
|
|
157
|
+
(cut ? undefined : started[0]);
|
|
158
|
+
if (turn) add(turn.key, each);
|
|
159
|
+
}
|
|
160
|
+
for (const [key, cards] of byTurn)
|
|
161
|
+
byTurn.set(
|
|
162
|
+
key,
|
|
163
|
+
cards.sort((a, b) => a.createdAt.localeCompare(b.createdAt)),
|
|
164
|
+
);
|
|
165
|
+
return byTurn;
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
/** The `delegate_session` calls of `items` whose card is among `cards`: their rows are not drawn. */
|
|
169
|
+
export const hiddenCalls = (
|
|
170
|
+
items: Line[],
|
|
171
|
+
cards: readonly Delegation[],
|
|
172
|
+
results: ReadonlyMap<string | undefined, Line>,
|
|
173
|
+
) =>
|
|
174
|
+
new Set(
|
|
175
|
+
cards.length
|
|
176
|
+
? items.flatMap((line) => {
|
|
177
|
+
const made =
|
|
178
|
+
line.type === 'tool/call'
|
|
179
|
+
? delegationOfCall(line, results.get((line as { id?: string }).id), cards)
|
|
180
|
+
: undefined;
|
|
181
|
+
return made ? [(line as { id?: string }).id] : [];
|
|
182
|
+
})
|
|
183
|
+
: [],
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
/** Two lists of cards drawing the same (a turn is not drawn again for an unchanged listing). */
|
|
187
|
+
export const sameCards = (a: readonly Delegation[], b: readonly Delegation[]) =>
|
|
188
|
+
a === b ||
|
|
189
|
+
(a.length === b.length &&
|
|
190
|
+
a.every((each, i) => {
|
|
191
|
+
const other = b[i]!;
|
|
192
|
+
return (
|
|
193
|
+
each.id === other.id &&
|
|
194
|
+
each.status === other.status &&
|
|
195
|
+
each.title === other.title &&
|
|
196
|
+
each.child === other.child &&
|
|
197
|
+
each.preparing === other.preparing &&
|
|
198
|
+
each.endedAt === other.endedAt &&
|
|
199
|
+
each.result === other.result &&
|
|
200
|
+
each.error === other.error
|
|
201
|
+
);
|
|
202
|
+
}));
|
|
203
|
+
|
|
204
|
+
const STATUS_LABELS: Record<Delegation['status'], string> = {
|
|
205
|
+
running: 'Running',
|
|
206
|
+
completed: 'Done',
|
|
207
|
+
failed: 'Failed',
|
|
208
|
+
interrupted: 'Stopped',
|
|
209
|
+
cancelled: 'Cancelled',
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
/** A delegation's status in its one slot: a spinner while it runs (a bell while its child waits for the user), a ✓ disc
|
|
213
|
+
* done, a ! disc failed or stopped, a crossed circle cancelled. */
|
|
214
|
+
export function StatusMark({
|
|
215
|
+
delegation,
|
|
216
|
+
waiting,
|
|
217
|
+
}: {
|
|
218
|
+
delegation: Pick<Delegation, 'status'>;
|
|
219
|
+
waiting: boolean;
|
|
220
|
+
}) {
|
|
221
|
+
const label = waiting ? 'Waiting for you' : STATUS_LABELS[delegation.status];
|
|
222
|
+
return (
|
|
223
|
+
<span
|
|
224
|
+
data-slot="child-status"
|
|
225
|
+
className="inline-flex size-4 shrink-0 items-center justify-center"
|
|
226
|
+
>
|
|
227
|
+
{delegation.status === 'running' ? (
|
|
228
|
+
waiting ? (
|
|
229
|
+
<span role="img" aria-label={label} className="inline-flex text-accent">
|
|
230
|
+
<Icon name="bell" size={14} />
|
|
231
|
+
</span>
|
|
232
|
+
) : (
|
|
233
|
+
<Spinner label={label} className="text-accent" />
|
|
234
|
+
)
|
|
235
|
+
) : delegation.status === 'cancelled' ? (
|
|
236
|
+
<span role="img" aria-label={label} className="inline-flex text-muted">
|
|
237
|
+
<Icon name="ban" size={14} />
|
|
238
|
+
</span>
|
|
239
|
+
) : (
|
|
240
|
+
<StatusIcon
|
|
241
|
+
aria-label={label}
|
|
242
|
+
status={
|
|
243
|
+
delegation.status === 'completed'
|
|
244
|
+
? 'resolved'
|
|
245
|
+
: delegation.status === 'failed'
|
|
246
|
+
? 'error'
|
|
247
|
+
: 'warning'
|
|
248
|
+
}
|
|
249
|
+
/>
|
|
250
|
+
)}
|
|
251
|
+
</span>
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const CHILD_STATES: Record<ChildState, string> = {
|
|
256
|
+
working: 'Working',
|
|
257
|
+
asking: 'Asking',
|
|
258
|
+
done: 'Done',
|
|
259
|
+
failed: 'Failed',
|
|
260
|
+
stopped: 'Stopped',
|
|
261
|
+
cancelled: 'Cancelled',
|
|
262
|
+
};
|
|
263
|
+
/** A child's state in words with its icon, as its row in the sessions list says it (one slot, the line's end): a
|
|
264
|
+
* spinner working, a bell asking (a question or an approval waits), a ✓ disc done, a ! disc failed or stopped (as its card), a ban cancelled. */
|
|
265
|
+
export function ChildStateMark({ state }: { state: ChildState }) {
|
|
266
|
+
return (
|
|
267
|
+
<span
|
|
268
|
+
data-slot="child-state"
|
|
269
|
+
data-state={state}
|
|
270
|
+
className={cn(
|
|
271
|
+
'inline-flex shrink-0 items-center gap-1 text-xs',
|
|
272
|
+
state === 'asking'
|
|
273
|
+
? 'font-medium text-accent'
|
|
274
|
+
: state === 'failed'
|
|
275
|
+
? 'text-destructive'
|
|
276
|
+
: state === 'working'
|
|
277
|
+
? 'text-secondary'
|
|
278
|
+
: 'text-muted',
|
|
279
|
+
)}
|
|
280
|
+
>
|
|
281
|
+
{state === 'working' ? (
|
|
282
|
+
<Icon
|
|
283
|
+
name="loader-circle"
|
|
284
|
+
size={12}
|
|
285
|
+
className="animate-spin text-accent motion-reduce:animate-none"
|
|
286
|
+
/>
|
|
287
|
+
) : state === 'asking' ? (
|
|
288
|
+
<Icon name="bell" size={12} />
|
|
289
|
+
) : state === 'cancelled' ? (
|
|
290
|
+
<Icon name="ban" size={12} />
|
|
291
|
+
) : (
|
|
292
|
+
<StatusIcon
|
|
293
|
+
aria-hidden
|
|
294
|
+
size="sm"
|
|
295
|
+
status={state === 'done' ? 'resolved' : state === 'failed' ? 'error' : 'warning'}
|
|
296
|
+
/>
|
|
297
|
+
)}
|
|
298
|
+
{CHILD_STATES[state]}
|
|
299
|
+
</span>
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** A running child whose worktree is being made (S7), as its card and its row say it. */
|
|
304
|
+
export const PREPARING = 'Preparing its branch and stage';
|
|
305
|
+
/** What a delegation did so far: its child's state while it runs, else its result's first line or its error. */
|
|
306
|
+
const progressOf = (delegation: Delegation, child: Kin | undefined) => {
|
|
307
|
+
if (delegation.status === 'running')
|
|
308
|
+
return child?.waiting
|
|
309
|
+
? 'Waiting for you'
|
|
310
|
+
: child?.running
|
|
311
|
+
? 'Working'
|
|
312
|
+
: delegation.preparing
|
|
313
|
+
? PREPARING
|
|
314
|
+
: 'Starting';
|
|
315
|
+
if (delegation.error) return delegation.error;
|
|
316
|
+
if (delegation.result) return oneLine(delegation.result, 200);
|
|
317
|
+
return delegation.status === 'cancelled' ? 'Cancelled' : 'No answer';
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
/** A link (drawn as a button) opening child session `id`: the shell's navigation, a history entry. */
|
|
321
|
+
function OpenButton({ to, title }: { to: FollowTarget; title: string }) {
|
|
322
|
+
return (
|
|
323
|
+
<Button
|
|
324
|
+
size="sm"
|
|
325
|
+
variant="outline"
|
|
326
|
+
aria-label={`Open ${title}`}
|
|
327
|
+
className="shrink-0"
|
|
328
|
+
render={<ScreenLink to={to} className="no-underline hover:no-underline" />}
|
|
329
|
+
>
|
|
330
|
+
Open
|
|
331
|
+
</Button>
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/** One child's card in its parent's timeline. */
|
|
336
|
+
function ChildCard({ delegation, lineage }: { delegation: Delegation; lineage: Lineage }) {
|
|
337
|
+
const child = delegation.child ? lineage.sessions.get(delegation.child) : undefined;
|
|
338
|
+
const waiting = delegation.status === 'running' && !!child?.waiting;
|
|
339
|
+
const took =
|
|
340
|
+
delegation.endedAt !== undefined
|
|
341
|
+
? Date.parse(delegation.endedAt) - Date.parse(delegation.createdAt)
|
|
342
|
+
: undefined;
|
|
343
|
+
return (
|
|
344
|
+
<Card
|
|
345
|
+
size="sm"
|
|
346
|
+
data-delegation={delegation.id}
|
|
347
|
+
data-status={waiting ? 'waiting' : delegation.status}
|
|
348
|
+
className="flex flex-col gap-0.5"
|
|
349
|
+
>
|
|
350
|
+
<div className="flex h-7 min-w-0 items-center gap-2">
|
|
351
|
+
<StatusMark delegation={delegation} waiting={waiting} />
|
|
352
|
+
{delegation.child ? (
|
|
353
|
+
<ScreenLink
|
|
354
|
+
to={lineage.to(delegation.child)}
|
|
355
|
+
className="min-w-0 flex-1 truncate text-sm font-medium"
|
|
356
|
+
>
|
|
357
|
+
{delegation.title}
|
|
358
|
+
</ScreenLink>
|
|
359
|
+
) : (
|
|
360
|
+
<span className="min-w-0 flex-1 truncate text-sm font-medium">{delegation.title}</span>
|
|
361
|
+
)}
|
|
362
|
+
<span
|
|
363
|
+
data-who="elapsed"
|
|
364
|
+
title={formatDate(delegation.createdAt, { dateStyle: 'full', timeStyle: 'short' })}
|
|
365
|
+
className="shrink-0 text-xs tabular-nums text-muted"
|
|
366
|
+
>
|
|
367
|
+
{delegation.status === 'running' ? (
|
|
368
|
+
<Elapsed since={delegation.createdAt} />
|
|
369
|
+
) : took !== undefined ? (
|
|
370
|
+
duration(took)
|
|
371
|
+
) : null}
|
|
372
|
+
</span>
|
|
373
|
+
{delegation.child && (
|
|
374
|
+
<OpenButton to={lineage.to(delegation.child)} title={delegation.title} />
|
|
375
|
+
)}
|
|
376
|
+
</div>
|
|
377
|
+
<p
|
|
378
|
+
data-who="progress"
|
|
379
|
+
className={cn(
|
|
380
|
+
'min-w-0 truncate pl-6 text-xs',
|
|
381
|
+
waiting ? 'font-medium text-accent' : 'text-secondary',
|
|
382
|
+
delegation.status === 'failed' && 'text-destructive',
|
|
383
|
+
)}
|
|
384
|
+
>
|
|
385
|
+
{progressOf(delegation, child)}
|
|
386
|
+
</p>
|
|
387
|
+
</Card>
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/** The children one turn delegated, together: a card each. */
|
|
392
|
+
export function ChildCards({ delegations }: { delegations: readonly Delegation[] }) {
|
|
393
|
+
const lineage = React.useContext(LineageContext);
|
|
394
|
+
if (!lineage || !delegations.length) return null;
|
|
395
|
+
const running = delegations.filter((each) => each.status === 'running').length;
|
|
396
|
+
return (
|
|
397
|
+
<section aria-label="Child sessions" data-who="children" className="mt-2 flex flex-col gap-1.5">
|
|
398
|
+
<p className="flex h-6 items-center gap-1.5 text-xs text-muted">
|
|
399
|
+
<Icon name="users" size={13} />
|
|
400
|
+
Delegated to {delegations.length} session{delegations.length === 1 ? '' : 's'}
|
|
401
|
+
{running > 0 && ` · ${running} running`}
|
|
402
|
+
</p>
|
|
403
|
+
{delegations.map((each) => (
|
|
404
|
+
<ChildCard key={each.id} delegation={each} lineage={lineage} />
|
|
405
|
+
))}
|
|
406
|
+
</section>
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** A question a child asked, as Bridge's message to its parent carries it. */
|
|
411
|
+
interface Asked {
|
|
412
|
+
session: string;
|
|
413
|
+
decision: string;
|
|
414
|
+
text: string;
|
|
415
|
+
}
|
|
416
|
+
/** Bridge's message delivering children's results (`delegations`) and questions: a row per child and per question,
|
|
417
|
+
* the message's text on a click. */
|
|
418
|
+
export function ChildrenMessage({
|
|
419
|
+
time,
|
|
420
|
+
text,
|
|
421
|
+
delegations = [],
|
|
422
|
+
questions = [],
|
|
423
|
+
}: {
|
|
424
|
+
time?: string | undefined;
|
|
425
|
+
text: string;
|
|
426
|
+
delegations?: string[] | undefined;
|
|
427
|
+
questions?: Asked[] | undefined;
|
|
428
|
+
}) {
|
|
429
|
+
const lineage = React.useContext(LineageContext);
|
|
430
|
+
const [open, setOpen] = React.useState(false);
|
|
431
|
+
const body = React.useId();
|
|
432
|
+
const known = lineage?.delegations ?? NO_DELEGATIONS;
|
|
433
|
+
const results = delegations.map(
|
|
434
|
+
(id) =>
|
|
435
|
+
known.find((each) => each.id === id) ?? {
|
|
436
|
+
id,
|
|
437
|
+
title: id,
|
|
438
|
+
status: 'completed' as const,
|
|
439
|
+
createdAt: '',
|
|
440
|
+
},
|
|
441
|
+
);
|
|
442
|
+
const heading = [
|
|
443
|
+
results.length && `${results.length === 1 ? 'A result' : `${results.length} results`}`,
|
|
444
|
+
questions.length &&
|
|
445
|
+
`${questions.length === 1 ? 'a question' : `${questions.length} questions`}`,
|
|
446
|
+
]
|
|
447
|
+
.filter(Boolean)
|
|
448
|
+
.join(' and ');
|
|
449
|
+
return (
|
|
450
|
+
<div
|
|
451
|
+
data-who="children-message"
|
|
452
|
+
title={time ? formatDate(time, { dateStyle: 'full', timeStyle: 'short' }) : undefined}
|
|
453
|
+
className="rounded-dialog border border-border-subtle bg-surface text-sm"
|
|
454
|
+
>
|
|
455
|
+
<div className="flex h-9 items-center gap-2 pl-3 pr-1.5 text-xs text-muted">
|
|
456
|
+
<Icon name="bell" size={12} className="shrink-0" />
|
|
457
|
+
<span className="font-medium text-secondary">Bridge</span>
|
|
458
|
+
<span className="min-w-0 flex-1 truncate">
|
|
459
|
+
{heading.charAt(0).toUpperCase() + heading.slice(1)} from child sessions
|
|
460
|
+
</span>
|
|
461
|
+
<Button
|
|
462
|
+
size="sm"
|
|
463
|
+
aria-expanded={open}
|
|
464
|
+
aria-controls={body}
|
|
465
|
+
className="shrink-0 text-muted"
|
|
466
|
+
onClick={() => setOpen((was) => !was)}
|
|
467
|
+
>
|
|
468
|
+
{open ? 'Hide message' : 'Show message'}
|
|
469
|
+
</Button>
|
|
470
|
+
</div>
|
|
471
|
+
<ul className="m-0 list-none border-t border-border-subtle px-3 py-1">
|
|
472
|
+
{results.map((each) => {
|
|
473
|
+
const child = each.child ? lineage?.sessions.get(each.child) : undefined;
|
|
474
|
+
return (
|
|
475
|
+
<li key={each.id} data-result={each.id} className="flex h-8 min-w-0 items-center gap-2">
|
|
476
|
+
<StatusMark delegation={each} waiting={false} />
|
|
477
|
+
{each.child && lineage ? (
|
|
478
|
+
<ScreenLink to={lineage.to(each.child)} className="min-w-0 truncate font-medium">
|
|
479
|
+
{each.title}
|
|
480
|
+
</ScreenLink>
|
|
481
|
+
) : (
|
|
482
|
+
<span className="min-w-0 truncate font-medium">{each.title}</span>
|
|
483
|
+
)}
|
|
484
|
+
<span className="min-w-0 flex-1 truncate text-xs text-secondary">
|
|
485
|
+
{each.status === 'running'
|
|
486
|
+
? progressOf(each, child)
|
|
487
|
+
: `${STATUS_LABELS[each.status]}${each.result || each.error ? ` · ${progressOf(each, child)}` : ''}`}
|
|
488
|
+
</span>
|
|
489
|
+
{each.child && lineage && (
|
|
490
|
+
<OpenButton to={lineage.to(each.child)} title={each.title} />
|
|
491
|
+
)}
|
|
492
|
+
</li>
|
|
493
|
+
);
|
|
494
|
+
})}
|
|
495
|
+
{questions.map((asked) => {
|
|
496
|
+
const child = lineage?.sessions.get(asked.session);
|
|
497
|
+
const title = child?.title ?? 'A child session';
|
|
498
|
+
return (
|
|
499
|
+
<li
|
|
500
|
+
key={asked.decision}
|
|
501
|
+
data-question={asked.decision}
|
|
502
|
+
className="flex h-8 min-w-0 items-center gap-2"
|
|
503
|
+
>
|
|
504
|
+
<span className="inline-flex size-4 shrink-0 items-center justify-center text-accent">
|
|
505
|
+
<Icon name="circle-help" size={14} />
|
|
506
|
+
</span>
|
|
507
|
+
<span className="shrink-0 text-xs text-muted">Question from</span>
|
|
508
|
+
{lineage ? (
|
|
509
|
+
<ScreenLink to={lineage.to(asked.session)} className="min-w-0 truncate font-medium">
|
|
510
|
+
{title}
|
|
511
|
+
</ScreenLink>
|
|
512
|
+
) : (
|
|
513
|
+
<span className="min-w-0 truncate font-medium">{title}</span>
|
|
514
|
+
)}
|
|
515
|
+
<span className="min-w-0 flex-1 truncate text-xs text-secondary">
|
|
516
|
+
{child?.waiting ? 'Waiting for an answer' : ''}
|
|
517
|
+
</span>
|
|
518
|
+
{lineage && <OpenButton to={lineage.to(asked.session)} title={title} />}
|
|
519
|
+
</li>
|
|
520
|
+
);
|
|
521
|
+
})}
|
|
522
|
+
</ul>
|
|
523
|
+
{open && (
|
|
524
|
+
<div
|
|
525
|
+
id={body}
|
|
526
|
+
data-who="children-text"
|
|
527
|
+
className="max-h-96 overflow-auto whitespace-pre-wrap break-words border-t border-border-subtle px-3 py-2.5 text-[13px] text-foreground"
|
|
528
|
+
>
|
|
529
|
+
{text}
|
|
530
|
+
</div>
|
|
531
|
+
)}
|
|
532
|
+
</div>
|
|
533
|
+
);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/** The running children of the shown session, docked on its composer: what each does, since when, Open and Stop. */
|
|
537
|
+
export function BackgroundWork() {
|
|
538
|
+
const lineage = React.useContext(LineageContext);
|
|
539
|
+
const running = (lineage?.delegations ?? NO_DELEGATIONS).filter(
|
|
540
|
+
(each) => each.status === 'running' && each.child,
|
|
541
|
+
);
|
|
542
|
+
if (!lineage || !running.length) return null;
|
|
543
|
+
return (
|
|
544
|
+
<section
|
|
545
|
+
aria-label="Background work"
|
|
546
|
+
data-who="background"
|
|
547
|
+
className="mx-3 rounded-t-dialog border border-b-0 border-border bg-surface py-1 text-[12.5px]"
|
|
548
|
+
>
|
|
549
|
+
<p className="m-0 flex h-7 items-center gap-1.5 pl-3 text-xs text-muted">
|
|
550
|
+
<Icon name="users" size={13} />
|
|
551
|
+
Background work · {running.length} running
|
|
552
|
+
</p>
|
|
553
|
+
{running.map((each) => {
|
|
554
|
+
const child = lineage.sessions.get(each.child!);
|
|
555
|
+
const waiting = !!child?.waiting;
|
|
556
|
+
return (
|
|
557
|
+
<div
|
|
558
|
+
key={each.id}
|
|
559
|
+
data-background={each.id}
|
|
560
|
+
data-status={waiting ? 'waiting' : 'running'}
|
|
561
|
+
className="flex h-8 min-w-0 items-center gap-2 pl-3 pr-1.5 text-secondary"
|
|
562
|
+
>
|
|
563
|
+
<StatusMark delegation={each} waiting={waiting} />
|
|
564
|
+
<span className="min-w-0 flex-1 truncate text-foreground">{each.title}</span>
|
|
565
|
+
<span className={cn('shrink-0', waiting ? 'font-medium text-accent' : 'text-muted')}>
|
|
566
|
+
{progressOf(each, child)}
|
|
567
|
+
</span>
|
|
568
|
+
<span className="w-12 shrink-0 text-right text-muted">
|
|
569
|
+
<Elapsed since={each.createdAt} />
|
|
570
|
+
</span>
|
|
571
|
+
<OpenButton to={lineage.to(each.child!)} title={each.title} />
|
|
572
|
+
{lineage.stop && (
|
|
573
|
+
<Button
|
|
574
|
+
size="sm"
|
|
575
|
+
variant="outline"
|
|
576
|
+
aria-label={`Stop ${each.title}`}
|
|
577
|
+
className="shrink-0"
|
|
578
|
+
onClick={() => lineage.stop!(each.child!)}
|
|
579
|
+
>
|
|
580
|
+
Stop
|
|
581
|
+
</Button>
|
|
582
|
+
)}
|
|
583
|
+
</div>
|
|
584
|
+
);
|
|
585
|
+
})}
|
|
586
|
+
</section>
|
|
587
|
+
);
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
/** A child session's parent, beside its title: a link to it. */
|
|
591
|
+
export function ParentLink({ parent }: { parent: string }) {
|
|
592
|
+
const lineage = React.useContext(LineageContext);
|
|
593
|
+
if (!lineage) return null;
|
|
594
|
+
const title = lineage.sessions.get(parent)?.title;
|
|
595
|
+
return (
|
|
596
|
+
<ScreenLink
|
|
597
|
+
to={lineage.to(parent)}
|
|
598
|
+
data-who="parent"
|
|
599
|
+
aria-label={`Parent session${title ? `: ${title}` : ''}`}
|
|
600
|
+
className="inline-flex min-w-0 max-w-56 shrink items-center gap-1 text-xs text-secondary no-underline hover:text-foreground"
|
|
601
|
+
>
|
|
602
|
+
<Icon name="arrow-up" size={13} className="shrink-0" />
|
|
603
|
+
<span className="shrink-0 text-muted">Parent</span>
|
|
604
|
+
<span className="truncate">{title ?? 'session'}</span>
|
|
605
|
+
</ScreenLink>
|
|
606
|
+
);
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
/** Bridge's block before a child's task (`<bridge_delegation>`, `<bridge_prompt>`), with the role line before it. */
|
|
610
|
+
const BLOCK =
|
|
611
|
+
/^(?:Act as the [\w-]+ sub-agent for this task\.\n\n)?<bridge_(delegation|prompt)>\n[\s\S]*?\n<\/bridge_\1>\n\n/;
|
|
612
|
+
/** A message's text without Bridge's block before a delegated task or another session's message, and its role; none
|
|
613
|
+
* when it has no such block. */
|
|
614
|
+
export const taskOf = (text: string | undefined) => {
|
|
615
|
+
const found = text?.match(BLOCK);
|
|
616
|
+
if (!found || text === undefined) return undefined;
|
|
617
|
+
const role = /^Act as the ([\w-]+) sub-agent/.exec(text)?.[1];
|
|
618
|
+
return { text: text.slice(found[0].length), kind: found[1] as 'delegation' | 'prompt', role };
|
|
619
|
+
};
|
|
620
|
+
|
|
621
|
+
/** Who a task or a message from another session came from, over it: its parent (a link), or the session `sender`. */
|
|
622
|
+
export function TaskFrom({
|
|
623
|
+
kind,
|
|
624
|
+
role,
|
|
625
|
+
sender,
|
|
626
|
+
}: {
|
|
627
|
+
kind: 'delegation' | 'prompt';
|
|
628
|
+
role?: string | undefined;
|
|
629
|
+
sender?: string | undefined;
|
|
630
|
+
}) {
|
|
631
|
+
const lineage = React.useContext(LineageContext);
|
|
632
|
+
// A child's first task has no sender: it came from its parent.
|
|
633
|
+
const from = sender ?? (kind === 'delegation' ? lineage?.parent : undefined);
|
|
634
|
+
const shown = from ? lineage?.sessions.get(from) : undefined;
|
|
635
|
+
const title = shown?.title ?? (kind === 'delegation' ? 'its parent session' : 'another session');
|
|
636
|
+
return (
|
|
637
|
+
<p data-who="task-from" className="mb-1 flex min-w-0 items-center gap-1.5 text-xs text-muted">
|
|
638
|
+
<Icon name={kind === 'delegation' ? 'users' : 'mail'} size={12} className="shrink-0" />
|
|
639
|
+
<span className="shrink-0">{kind === 'delegation' ? 'Task from' : 'Message from'}</span>
|
|
640
|
+
{from && lineage ? (
|
|
641
|
+
<ScreenLink to={lineage.to(from)} className="min-w-0 truncate text-secondary">
|
|
642
|
+
{title}
|
|
643
|
+
</ScreenLink>
|
|
644
|
+
) : (
|
|
645
|
+
<span className="min-w-0 truncate text-secondary">{title}</span>
|
|
646
|
+
)}
|
|
647
|
+
{role && <span className="shrink-0">· as {role}</span>}
|
|
648
|
+
</p>
|
|
649
|
+
);
|
|
650
|
+
}
|