@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,38 @@
|
|
|
1
|
+
// The external origins the host declared for the page (`GET /_bridge/host/origins`): one fetch per page, then the same
|
|
2
|
+
// sets for every conversation. Any failure gives none, so a remote image or video stays a link.
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import { mediaOrigins } from '@bridge/session-ui/media';
|
|
5
|
+
import { readJson } from '@bridge/ui';
|
|
6
|
+
|
|
7
|
+
type Origins = ReturnType<typeof mediaOrigins>;
|
|
8
|
+
|
|
9
|
+
const none = mediaOrigins(undefined);
|
|
10
|
+
let pending: Promise<Origins> | undefined;
|
|
11
|
+
const load = () => {
|
|
12
|
+
pending ??= fetch('/_bridge/host/origins')
|
|
13
|
+
.then((res) => readJson(res))
|
|
14
|
+
.then(mediaOrigins, () => none);
|
|
15
|
+
return pending;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/** The origins of the host's answer: empty until it arrives, and when it cannot be read. */
|
|
19
|
+
export function useMediaOrigins(): Origins {
|
|
20
|
+
const [origins, setOrigins] = React.useState<Origins>(none);
|
|
21
|
+
React.useEffect(() => {
|
|
22
|
+
let live = true;
|
|
23
|
+
void load()
|
|
24
|
+
.then((value) => {
|
|
25
|
+
if (live) setOrigins(value);
|
|
26
|
+
})
|
|
27
|
+
// A page that cannot read the host's origins still shows its images as links: say what happened, never throw.
|
|
28
|
+
.catch((error: unknown) => {
|
|
29
|
+
console.warn(
|
|
30
|
+
`the host's origins could not be read: ${error instanceof Error ? error.message : String(error)}`,
|
|
31
|
+
);
|
|
32
|
+
});
|
|
33
|
+
return () => {
|
|
34
|
+
live = false;
|
|
35
|
+
};
|
|
36
|
+
}, []);
|
|
37
|
+
return origins;
|
|
38
|
+
}
|
|
@@ -0,0 +1,663 @@
|
|
|
1
|
+
// The Sessions screen on a phone (research-020 D5: triage and answer the agents). Filled under `mobileScreen` (the shell
|
|
2
|
+
// draws the header: burger, or Back while an item is open, and the title). No item: the sessions as shelves, the ones
|
|
3
|
+
// waiting for the user first; an item: that session's conversation, its open questions and decision, and the prompt
|
|
4
|
+
// pinned at the bottom. The data is the desktop's: the node's listing (./listing.ts), the `/watch` stream resumed by
|
|
5
|
+
// `seq`, the conversation, questions and prompt components. No diff review, gallery, tabs, shortcuts or menus. A child
|
|
6
|
+
// session (bridge#212) has no row: its parent's row works or waits while it does, and its parent's cards open it.
|
|
7
|
+
import * as React from 'react';
|
|
8
|
+
import { Badge, Button, EmptyState, Icon, cn, readJson } from '@bridge/ui';
|
|
9
|
+
import type { Context } from 'cordis';
|
|
10
|
+
import { SessionPrompt } from '@bridge/session-ui/prompt';
|
|
11
|
+
import type { Attached } from '@bridge/session-ui/media';
|
|
12
|
+
import {
|
|
13
|
+
Conversation,
|
|
14
|
+
DecisionDock,
|
|
15
|
+
Queued,
|
|
16
|
+
Spinner,
|
|
17
|
+
Tasks,
|
|
18
|
+
decisionsOf,
|
|
19
|
+
type Live,
|
|
20
|
+
type LiveEvent,
|
|
21
|
+
type Stored,
|
|
22
|
+
} from './conversation.tsx';
|
|
23
|
+
import { Questions } from './questions.tsx';
|
|
24
|
+
import {
|
|
25
|
+
afterLive,
|
|
26
|
+
afterStored,
|
|
27
|
+
ago,
|
|
28
|
+
listChanges,
|
|
29
|
+
notesOf,
|
|
30
|
+
reset,
|
|
31
|
+
useLeftDecisions,
|
|
32
|
+
useItemTitles,
|
|
33
|
+
useListing,
|
|
34
|
+
type Listing,
|
|
35
|
+
type Session,
|
|
36
|
+
} from './index.tsx';
|
|
37
|
+
import { inbox, lineage } from '../listing.ts';
|
|
38
|
+
import type { Lineage } from './children.tsx';
|
|
39
|
+
import { answerBody, type Answer } from '../answers.ts';
|
|
40
|
+
|
|
41
|
+
/** Settled rows shown before "Show more". */
|
|
42
|
+
const SETTLED_SHOWN = 10;
|
|
43
|
+
|
|
44
|
+
type Shelf = 'needs' | 'pinned' | 'active' | 'working' | 'snoozed' | 'settled';
|
|
45
|
+
|
|
46
|
+
/** The state a row shows, the first that holds: waiting for the user, running, failed unread, unread, else idle. */
|
|
47
|
+
const stateOf = (each: Session) => {
|
|
48
|
+
if (each.waiting)
|
|
49
|
+
return each.questions?.some((question) => question.expired) ? 'Your call' : 'Needs you';
|
|
50
|
+
if (each.running) return 'Running';
|
|
51
|
+
if (each.unread && each.lastTurn?.reason === 'failed') return 'Failed';
|
|
52
|
+
if (each.unread) return 'Done';
|
|
53
|
+
if (each.woke) return 'Woke';
|
|
54
|
+
return undefined;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export function MobileSessions({
|
|
58
|
+
ctx,
|
|
59
|
+
node,
|
|
60
|
+
item,
|
|
61
|
+
readOnly,
|
|
62
|
+
setActions,
|
|
63
|
+
setChrome,
|
|
64
|
+
}: {
|
|
65
|
+
ctx: Context;
|
|
66
|
+
node: string;
|
|
67
|
+
item: string | undefined;
|
|
68
|
+
readOnly: boolean;
|
|
69
|
+
/** The phone shell: `{ bar: false }` hides its bottom bar while the new-session prompt shows over the list. */
|
|
70
|
+
setChrome?: ((chrome: { bar?: boolean }) => void) | undefined;
|
|
71
|
+
/** The phone shell's buttons; absent from an older shell. */
|
|
72
|
+
setActions?:
|
|
73
|
+
| ((
|
|
74
|
+
actions: readonly {
|
|
75
|
+
id: string;
|
|
76
|
+
label: string;
|
|
77
|
+
icon: string;
|
|
78
|
+
placement?: 'top' | 'primary';
|
|
79
|
+
onSelect: () => void;
|
|
80
|
+
disabled?: boolean;
|
|
81
|
+
}[],
|
|
82
|
+
) => void)
|
|
83
|
+
| undefined;
|
|
84
|
+
}) {
|
|
85
|
+
const { listing } = useListing(ctx, node);
|
|
86
|
+
// The header names an open session by its title, not its id.
|
|
87
|
+
useItemTitles(ctx, listing?.sessions);
|
|
88
|
+
if (!listing) return <section aria-label="Screen" className="flex min-h-0 flex-1" />;
|
|
89
|
+
if (!listing.project && !listing.workspace)
|
|
90
|
+
return (
|
|
91
|
+
<section aria-label="Screen" className="flex min-h-0 flex-1 items-center justify-center p-6">
|
|
92
|
+
<EmptyState role="note" className="text-sm text-secondary">
|
|
93
|
+
Sessions live in projects and on the workspace. Open one to start a session.
|
|
94
|
+
</EmptyState>
|
|
95
|
+
</section>
|
|
96
|
+
);
|
|
97
|
+
const shown = item ? listing.sessions.find((each) => each.id === item) : undefined;
|
|
98
|
+
return item ? (
|
|
99
|
+
<MobileSession
|
|
100
|
+
key={item}
|
|
101
|
+
ctx={ctx}
|
|
102
|
+
node={node}
|
|
103
|
+
id={item}
|
|
104
|
+
session={shown}
|
|
105
|
+
listing={listing}
|
|
106
|
+
readOnly={readOnly}
|
|
107
|
+
/>
|
|
108
|
+
) : (
|
|
109
|
+
<MobileList
|
|
110
|
+
ctx={ctx}
|
|
111
|
+
node={node}
|
|
112
|
+
listing={listing}
|
|
113
|
+
readOnly={readOnly}
|
|
114
|
+
setActions={setActions}
|
|
115
|
+
setChrome={setChrome}
|
|
116
|
+
/>
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function Row({ each, onOpen }: { each: Session; onOpen: () => void }) {
|
|
121
|
+
const state = stateOf(each);
|
|
122
|
+
const needs = state === 'Needs you' || state === 'Your call';
|
|
123
|
+
return (
|
|
124
|
+
<button
|
|
125
|
+
type="button"
|
|
126
|
+
data-session={each.id}
|
|
127
|
+
data-state={state ?? 'idle'}
|
|
128
|
+
onClick={onOpen}
|
|
129
|
+
className={cn(
|
|
130
|
+
'flex min-h-14 w-full cursor-pointer flex-col justify-center gap-0.5 border-0 border-b border-solid border-border bg-transparent px-4 py-2.5 text-left active:bg-hover',
|
|
131
|
+
needs && 'bg-accent/10',
|
|
132
|
+
)}
|
|
133
|
+
>
|
|
134
|
+
<span className="flex items-center gap-2">
|
|
135
|
+
<span className="min-w-0 flex-1 truncate text-base font-semibold text-foreground">
|
|
136
|
+
{each.title}
|
|
137
|
+
</span>
|
|
138
|
+
{each.running && !needs ? (
|
|
139
|
+
<Spinner />
|
|
140
|
+
) : state ? (
|
|
141
|
+
<span
|
|
142
|
+
className={cn(
|
|
143
|
+
'inline-flex shrink-0 items-center gap-1 text-xs font-medium',
|
|
144
|
+
needs ? 'text-accent' : state === 'Failed' ? 'text-destructive' : 'text-secondary',
|
|
145
|
+
)}
|
|
146
|
+
>
|
|
147
|
+
{needs && <Icon name="bell" size={12} />}
|
|
148
|
+
{state}
|
|
149
|
+
</span>
|
|
150
|
+
) : null}
|
|
151
|
+
<span className="shrink-0 text-xs text-muted">{ago(each.updatedAt ?? each.createdAt)}</span>
|
|
152
|
+
</span>
|
|
153
|
+
<span className="truncate text-sm text-secondary">{each.preview ?? 'No messages yet'}</span>
|
|
154
|
+
</button>
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function MobileList({
|
|
159
|
+
ctx,
|
|
160
|
+
node,
|
|
161
|
+
listing,
|
|
162
|
+
readOnly,
|
|
163
|
+
setActions,
|
|
164
|
+
setChrome,
|
|
165
|
+
}: {
|
|
166
|
+
ctx: Context;
|
|
167
|
+
node: string;
|
|
168
|
+
listing: Listing;
|
|
169
|
+
readOnly: boolean;
|
|
170
|
+
setChrome?: ((chrome: { bar?: boolean }) => void) | undefined;
|
|
171
|
+
setActions?:
|
|
172
|
+
| ((
|
|
173
|
+
actions: readonly {
|
|
174
|
+
id: string;
|
|
175
|
+
label: string;
|
|
176
|
+
icon: string;
|
|
177
|
+
placement?: 'top' | 'primary';
|
|
178
|
+
onSelect: () => void;
|
|
179
|
+
disabled?: boolean;
|
|
180
|
+
}[],
|
|
181
|
+
) => void)
|
|
182
|
+
| undefined;
|
|
183
|
+
}) {
|
|
184
|
+
const [open, setOpen] = React.useState<Partial<Record<Shelf, boolean>>>({});
|
|
185
|
+
/** Working sessions fold into Working (bridge#210), as on a computer: the node's "Fold working sessions" setting. */
|
|
186
|
+
const folding = React.useSyncExternalStore(
|
|
187
|
+
ctx.nodes.subscribe,
|
|
188
|
+
() => ctx.nodes.settings('sessions', node)?.values.inbox?.value !== false,
|
|
189
|
+
);
|
|
190
|
+
const [allSettled, setAllSettled] = React.useState(false);
|
|
191
|
+
const [composing, setComposing] = React.useState(false);
|
|
192
|
+
// The shell's round "+" beside its bar; none while the new-session prompt is open.
|
|
193
|
+
React.useEffect(() => {
|
|
194
|
+
if (readOnly || composing) return;
|
|
195
|
+
setActions?.([
|
|
196
|
+
{
|
|
197
|
+
id: 'new',
|
|
198
|
+
label: 'New session',
|
|
199
|
+
icon: 'plus',
|
|
200
|
+
placement: 'primary',
|
|
201
|
+
onSelect: () => setComposing(true),
|
|
202
|
+
},
|
|
203
|
+
]);
|
|
204
|
+
return () => setActions?.([]);
|
|
205
|
+
}, [setActions, readOnly, composing]);
|
|
206
|
+
// The new-session prompt shows over the list: the shell's bar would cover it.
|
|
207
|
+
React.useEffect(() => {
|
|
208
|
+
if (!composing) return;
|
|
209
|
+
setChrome?.({ bar: false });
|
|
210
|
+
return () => setChrome?.({});
|
|
211
|
+
}, [composing, setChrome]);
|
|
212
|
+
// A child session has no row of its own (bridge#212): its parent works or waits while it does, its cards open it.
|
|
213
|
+
const top = lineage(listing.sessions).top;
|
|
214
|
+
const awake = top.filter((each) => !each.snoozedUntil);
|
|
215
|
+
// The sessions waiting for the user come first, apart from their shelf.
|
|
216
|
+
const needs = top.filter((each) => each.waiting);
|
|
217
|
+
const rest = (each: Session) => !each.waiting;
|
|
218
|
+
const pinned = awake
|
|
219
|
+
.filter((each) => rest(each) && each.pinnedAt)
|
|
220
|
+
.sort((a, b) => (a.pinOrder ?? 0) - (b.pinOrder ?? 0));
|
|
221
|
+
const unsorted = awake.filter((each) => rest(each) && !each.pinnedAt && !each.settledAt);
|
|
222
|
+
const { active, working } = folding ? inbox(unsorted) : { active: unsorted, working: [] };
|
|
223
|
+
const snoozed = top
|
|
224
|
+
.filter((each) => rest(each) && each.snoozedUntil)
|
|
225
|
+
.sort((a, b) => a.snoozedUntil!.localeCompare(b.snoozedUntil!));
|
|
226
|
+
const settled = awake
|
|
227
|
+
.filter((each) => rest(each) && !each.pinnedAt && each.settledAt)
|
|
228
|
+
.sort((a, b) => b.settledAt!.localeCompare(a.settledAt!));
|
|
229
|
+
const shelves: [Shelf, string, Session[]][] = [
|
|
230
|
+
['needs', 'Waiting for you', needs],
|
|
231
|
+
['pinned', 'Pinned', pinned],
|
|
232
|
+
['active', 'Active', active],
|
|
233
|
+
['working', 'Working', working],
|
|
234
|
+
['snoozed', 'Snoozed', snoozed],
|
|
235
|
+
['settled', 'Settled', settled],
|
|
236
|
+
];
|
|
237
|
+
if (composing)
|
|
238
|
+
return (
|
|
239
|
+
<NewSession
|
|
240
|
+
ctx={ctx}
|
|
241
|
+
node={node}
|
|
242
|
+
onClose={() => setComposing(false)}
|
|
243
|
+
onCreated={(id) => {
|
|
244
|
+
setComposing(false);
|
|
245
|
+
ctx.screen.open(id);
|
|
246
|
+
}}
|
|
247
|
+
/>
|
|
248
|
+
);
|
|
249
|
+
return (
|
|
250
|
+
<section aria-label="Screen" className={cn('relative flex min-h-0 flex-1 flex-col', reset)}>
|
|
251
|
+
<div className="min-h-0 flex-1 overflow-y-auto pb-[var(--mobile-inset-bottom,env(safe-area-inset-bottom))]">
|
|
252
|
+
{listing.sessions.length === 0 && (
|
|
253
|
+
<EmptyState className="text-sm text-secondary">
|
|
254
|
+
No sessions yet.{readOnly ? '' : ' Tap + to start one.'}
|
|
255
|
+
</EmptyState>
|
|
256
|
+
)}
|
|
257
|
+
{shelves.map(([key, title, sessions]) => {
|
|
258
|
+
if (!sessions.length) return null;
|
|
259
|
+
const isOpen = open[key] ?? (key !== 'settled' && key !== 'working');
|
|
260
|
+
const rows =
|
|
261
|
+
key === 'settled' && !allSettled ? sessions.slice(0, SETTLED_SHOWN) : sessions;
|
|
262
|
+
return (
|
|
263
|
+
<div key={key} data-shelf={key}>
|
|
264
|
+
<button
|
|
265
|
+
type="button"
|
|
266
|
+
aria-expanded={isOpen}
|
|
267
|
+
onClick={() => setOpen((was) => ({ ...was, [key]: !isOpen }))}
|
|
268
|
+
className={cn(
|
|
269
|
+
'flex min-h-11 w-full cursor-pointer items-center gap-2 border-0 border-b border-solid border-border bg-background px-4 text-left text-xs font-semibold uppercase tracking-wide',
|
|
270
|
+
key === 'needs' ? 'text-accent' : 'text-secondary',
|
|
271
|
+
)}
|
|
272
|
+
>
|
|
273
|
+
<Icon name={isOpen ? 'chevron-down' : 'chevron-right'} size={14} />
|
|
274
|
+
<span className="flex-1">{title}</span>
|
|
275
|
+
<Badge>{sessions.length}</Badge>
|
|
276
|
+
</button>
|
|
277
|
+
{isOpen && (
|
|
278
|
+
<>
|
|
279
|
+
{rows.map((each) => (
|
|
280
|
+
<Row key={each.id} each={each} onOpen={() => ctx.screen.open(each.id)} />
|
|
281
|
+
))}
|
|
282
|
+
{rows.length < sessions.length && (
|
|
283
|
+
<Button
|
|
284
|
+
type="button"
|
|
285
|
+
className="min-h-11 w-full text-accent"
|
|
286
|
+
onClick={() => setAllSettled(true)}
|
|
287
|
+
>
|
|
288
|
+
Show more
|
|
289
|
+
</Button>
|
|
290
|
+
)}
|
|
291
|
+
</>
|
|
292
|
+
)}
|
|
293
|
+
</div>
|
|
294
|
+
);
|
|
295
|
+
})}
|
|
296
|
+
</div>
|
|
297
|
+
</section>
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** A new session: the prompt alone (the model picker is the prompt's own); its first message creates the session. */
|
|
302
|
+
function NewSession({
|
|
303
|
+
ctx,
|
|
304
|
+
node,
|
|
305
|
+
onClose,
|
|
306
|
+
onCreated,
|
|
307
|
+
}: {
|
|
308
|
+
ctx: Context;
|
|
309
|
+
node: string;
|
|
310
|
+
onClose: () => void;
|
|
311
|
+
onCreated: (id: string) => void;
|
|
312
|
+
}) {
|
|
313
|
+
const { workspace } = ctx;
|
|
314
|
+
return (
|
|
315
|
+
<section aria-label="Screen" className={cn('flex min-h-0 flex-1 flex-col', reset)}>
|
|
316
|
+
<div className="flex min-h-0 flex-1 flex-col items-center justify-center gap-2 px-6 text-center">
|
|
317
|
+
<span className="flex size-10 items-center justify-center rounded-full border border-border bg-surface text-secondary">
|
|
318
|
+
<Icon name="bot" size={20} />
|
|
319
|
+
</span>
|
|
320
|
+
<p className="text-lg font-semibold">New session</p>
|
|
321
|
+
<Button variant="outline" className="min-h-11" onClick={onClose}>
|
|
322
|
+
Cancel
|
|
323
|
+
</Button>
|
|
324
|
+
</div>
|
|
325
|
+
<div className={promptBox}>
|
|
326
|
+
<SessionPrompt
|
|
327
|
+
variant="mobile"
|
|
328
|
+
ctx={ctx}
|
|
329
|
+
at={{ slug: workspace.slug, node }}
|
|
330
|
+
session={undefined}
|
|
331
|
+
onCreated={onCreated}
|
|
332
|
+
/>
|
|
333
|
+
</div>
|
|
334
|
+
</section>
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** The prompt's frame: above the home bar (the prompt's `mobile` variant does the rest). */
|
|
339
|
+
const promptBox = 'shrink-0 px-2 pb-[max(env(safe-area-inset-bottom),0.5rem)]';
|
|
340
|
+
|
|
341
|
+
function MobileSession({
|
|
342
|
+
ctx,
|
|
343
|
+
node,
|
|
344
|
+
id,
|
|
345
|
+
session,
|
|
346
|
+
listing,
|
|
347
|
+
readOnly,
|
|
348
|
+
}: {
|
|
349
|
+
ctx: Context;
|
|
350
|
+
node: string;
|
|
351
|
+
id: string;
|
|
352
|
+
session: Session | undefined;
|
|
353
|
+
listing: Listing;
|
|
354
|
+
readOnly: boolean;
|
|
355
|
+
}) {
|
|
356
|
+
const { workspace } = ctx;
|
|
357
|
+
const { store } = useListing(ctx, node);
|
|
358
|
+
const api = `/${workspace.slug}/api/sessions/sessions`;
|
|
359
|
+
const headers = { 'content-type': 'application/json', 'x-bridge-node': node };
|
|
360
|
+
const [events, setEvents] = React.useState<Stored[]>([]);
|
|
361
|
+
const [live, setLive] = React.useState<Live | null>(null);
|
|
362
|
+
// Its children's cards and its parent (bridge#212), each a link to that session.
|
|
363
|
+
const kin = React.useMemo(
|
|
364
|
+
() => new Map(listing.sessions.map((each) => [each.id, each])),
|
|
365
|
+
[listing.sessions],
|
|
366
|
+
);
|
|
367
|
+
const lineageOf = React.useMemo<Lineage | undefined>(
|
|
368
|
+
() =>
|
|
369
|
+
session && (session.parent !== undefined || session.delegations?.length)
|
|
370
|
+
? {
|
|
371
|
+
sessions: kin,
|
|
372
|
+
delegations: session.delegations ?? [],
|
|
373
|
+
parent: session.parent,
|
|
374
|
+
to: (to) => ({ item: to }),
|
|
375
|
+
}
|
|
376
|
+
: undefined,
|
|
377
|
+
[kin, session?.delegations, session?.parent],
|
|
378
|
+
);
|
|
379
|
+
const [synced, setSynced] = React.useState(false);
|
|
380
|
+
const [error, setError] = React.useState('');
|
|
381
|
+
const [answering, setAnswering] = React.useState<Record<number, boolean>>({});
|
|
382
|
+
/** Whether the open decisions drawn on a timeline row are in view, by `seq`: the dock points to one out of view. */
|
|
383
|
+
const [onRows, setOnRows] = React.useState<Record<number, boolean>>({});
|
|
384
|
+
const onDecisionShown = React.useCallback(
|
|
385
|
+
(seq: number, shown: boolean | undefined) =>
|
|
386
|
+
setOnRows((was) => {
|
|
387
|
+
if (was[seq] === shown) return was;
|
|
388
|
+
const { [seq]: _gone, ...rest } = was;
|
|
389
|
+
return shown === undefined ? rest : { ...rest, [seq]: shown };
|
|
390
|
+
}),
|
|
391
|
+
[],
|
|
392
|
+
);
|
|
393
|
+
const viewport = React.useRef<HTMLDivElement>(null);
|
|
394
|
+
const stick = React.useRef(true);
|
|
395
|
+
const promptHandle = React.useRef<{
|
|
396
|
+
setText(text: string): void;
|
|
397
|
+
addText(session: string, text: string, images?: Attached[]): void;
|
|
398
|
+
focus(): void;
|
|
399
|
+
openModels(): void;
|
|
400
|
+
}>(null);
|
|
401
|
+
const latest = React.useRef(store);
|
|
402
|
+
latest.current = store;
|
|
403
|
+
const load = () => latest.current?.load().catch(() => {});
|
|
404
|
+
const report = (cause: unknown) => {
|
|
405
|
+
if ((cause as Error)?.name !== 'AbortError')
|
|
406
|
+
setError(cause instanceof Error ? cause.message : String(cause));
|
|
407
|
+
};
|
|
408
|
+
const post = (path: string, body: object) =>
|
|
409
|
+
workspace
|
|
410
|
+
.fetch(`${api}${path}`, {
|
|
411
|
+
method: 'POST',
|
|
412
|
+
headers,
|
|
413
|
+
body: JSON.stringify(body),
|
|
414
|
+
})
|
|
415
|
+
.then(readJson);
|
|
416
|
+
const visit = () => void post('/triage', { id, action: 'visit' }).then(load, () => {});
|
|
417
|
+
// This session is shown: not polled (its own stream lists again), not counted unread.
|
|
418
|
+
React.useEffect(() => store?.watch(id), [store, id]);
|
|
419
|
+
React.useEffect(visit, [id]);
|
|
420
|
+
|
|
421
|
+
// One subscription, resumed after the last `seq` held, 1 s later and doubling to 30 s after an error.
|
|
422
|
+
React.useEffect(() => {
|
|
423
|
+
let last = 0;
|
|
424
|
+
let stop: (() => void) | undefined;
|
|
425
|
+
let retry: ReturnType<typeof setTimeout> | undefined;
|
|
426
|
+
let delay = 1000;
|
|
427
|
+
let closed = false;
|
|
428
|
+
/** A snapshot came already: the next one is a reconnect's. */
|
|
429
|
+
let resumed = false;
|
|
430
|
+
const resulted = new Set<string>();
|
|
431
|
+
const add = (lines: Stored[]) => {
|
|
432
|
+
const fresh = lines.filter((line) => line.seq > last);
|
|
433
|
+
if (!fresh.length) return;
|
|
434
|
+
last = fresh.at(-1)!.seq;
|
|
435
|
+
for (const line of fresh) {
|
|
436
|
+
if (line.type === 'turn/start') resulted.clear();
|
|
437
|
+
if (line.type === 'tool/result' && line.id) resulted.add(line.id);
|
|
438
|
+
}
|
|
439
|
+
if (fresh.some((line) => listChanges.has(line.type))) void load();
|
|
440
|
+
setEvents((was) => [...was, ...fresh]);
|
|
441
|
+
};
|
|
442
|
+
const subscribe = () => {
|
|
443
|
+
let isSynced = false;
|
|
444
|
+
let retried = false;
|
|
445
|
+
const again = () => {
|
|
446
|
+
stop?.();
|
|
447
|
+
setSynced(false);
|
|
448
|
+
if (closed || retried) return;
|
|
449
|
+
retried = true;
|
|
450
|
+
retry = setTimeout(subscribe, delay);
|
|
451
|
+
delay = Math.min(delay * 2, 30_000);
|
|
452
|
+
};
|
|
453
|
+
stop = workspace.stream(`${api}/watch?id=${encodeURIComponent(id)}&after=${last}`, {
|
|
454
|
+
persistent: false,
|
|
455
|
+
onLost: again,
|
|
456
|
+
onEnd: again,
|
|
457
|
+
onItem: (value) => {
|
|
458
|
+
const data = value as
|
|
459
|
+
| { type: 'snapshot'; events: Stored[]; live: Live | null }
|
|
460
|
+
| { type: 'stored'; event: Stored }
|
|
461
|
+
| { type: 'live'; event: LiveEvent }
|
|
462
|
+
| { type: string };
|
|
463
|
+
if (data.type === 'snapshot') {
|
|
464
|
+
const snap = data as { events: Stored[]; live: Live | null };
|
|
465
|
+
isSynced = true;
|
|
466
|
+
delay = 1000;
|
|
467
|
+
setSynced(true);
|
|
468
|
+
// A reconnect: the `queue` notices of the drop are lost (a message queued, removed or sent elsewhere).
|
|
469
|
+
if (resumed) void load();
|
|
470
|
+
resumed = true;
|
|
471
|
+
add(snap.events);
|
|
472
|
+
setLive(snap.live);
|
|
473
|
+
} else if (data.type === 'stored') {
|
|
474
|
+
const { event } = data as { event: Stored };
|
|
475
|
+
if (event.seq <= last) return;
|
|
476
|
+
add([event]);
|
|
477
|
+
if (isSynced && event.type === 'turn/end') visit();
|
|
478
|
+
if (isSynced) setLive((was) => afterStored(was, event));
|
|
479
|
+
} else if (data.type === 'queue') {
|
|
480
|
+
void load();
|
|
481
|
+
} else if (data.type === 'live' && isSynced) {
|
|
482
|
+
const { event } = data as { event: LiveEvent };
|
|
483
|
+
if (!(event.type === 'tool' && resulted.has(event.id ?? '')))
|
|
484
|
+
setLive((was) => afterLive(was, event));
|
|
485
|
+
}
|
|
486
|
+
},
|
|
487
|
+
});
|
|
488
|
+
};
|
|
489
|
+
const close = () => {
|
|
490
|
+
closed = true;
|
|
491
|
+
stop?.();
|
|
492
|
+
clearTimeout(retry);
|
|
493
|
+
};
|
|
494
|
+
if (workspace.signal.aborted) return;
|
|
495
|
+
workspace.signal.addEventListener('abort', close, { once: true });
|
|
496
|
+
subscribe();
|
|
497
|
+
return () => {
|
|
498
|
+
workspace.signal.removeEventListener('abort', close);
|
|
499
|
+
close();
|
|
500
|
+
};
|
|
501
|
+
}, [id]);
|
|
502
|
+
|
|
503
|
+
// New content keeps the conversation at its end while the reader is there.
|
|
504
|
+
React.useEffect(() => {
|
|
505
|
+
const element = viewport.current;
|
|
506
|
+
if (!element) return;
|
|
507
|
+
const onScroll = () => {
|
|
508
|
+
stick.current = element.scrollHeight - element.scrollTop - element.clientHeight < 80;
|
|
509
|
+
};
|
|
510
|
+
element.addEventListener('scroll', onScroll);
|
|
511
|
+
return () => element.removeEventListener('scroll', onScroll);
|
|
512
|
+
}, []);
|
|
513
|
+
React.useLayoutEffect(() => {
|
|
514
|
+
const element = viewport.current;
|
|
515
|
+
if (element && stick.current) element.scrollTop = element.scrollHeight;
|
|
516
|
+
}, [events, live]);
|
|
517
|
+
|
|
518
|
+
const running = live !== null;
|
|
519
|
+
const left = useLeftDecisions(session);
|
|
520
|
+
const { open: openDecisions } = decisionsOf(events, live, left);
|
|
521
|
+
// A decision whose row is in view is answered there; the dock takes the first other one.
|
|
522
|
+
const waiting = openDecisions.filter((each) => onRows[each.seq] !== true);
|
|
523
|
+
const docked = waiting[0];
|
|
524
|
+
const folder =
|
|
525
|
+
session && session.node !== node
|
|
526
|
+
? undefined
|
|
527
|
+
: (listing.project?.path ?? listing.workspace?.folder);
|
|
528
|
+
const relative = (value: string) =>
|
|
529
|
+
folder ? value.split(`${folder.replace(/[\\/]$/, '')}/`).join('') : value;
|
|
530
|
+
const answer = async (seq: number, decision: string, option: Answer) => {
|
|
531
|
+
setError('');
|
|
532
|
+
setAnswering((was) => ({ ...was, [seq]: true }));
|
|
533
|
+
try {
|
|
534
|
+
await post(`/answer?id=${encodeURIComponent(id)}`, answerBody(decision, option));
|
|
535
|
+
} catch (cause) {
|
|
536
|
+
if ((cause as { code?: unknown })?.code !== 'decision_closed') report(cause);
|
|
537
|
+
} finally {
|
|
538
|
+
setAnswering(({ [seq]: _done, ...rest }) => rest);
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
const interrupt = () =>
|
|
542
|
+
void post(`/interrupt?id=${encodeURIComponent(id)}`, {}).then(load, report);
|
|
543
|
+
|
|
544
|
+
return (
|
|
545
|
+
<section aria-label="Screen" className={cn('flex min-h-0 flex-1 flex-col', reset)}>
|
|
546
|
+
<div ref={viewport} className="min-h-0 flex-1 overflow-x-hidden overflow-y-auto">
|
|
547
|
+
<Conversation
|
|
548
|
+
events={events}
|
|
549
|
+
live={live}
|
|
550
|
+
left={left}
|
|
551
|
+
folder={folder}
|
|
552
|
+
workspace={workspace.slug}
|
|
553
|
+
onReuse={(value) => {
|
|
554
|
+
promptHandle.current?.setText(value);
|
|
555
|
+
promptHandle.current?.focus();
|
|
556
|
+
}}
|
|
557
|
+
onReview={() => {}}
|
|
558
|
+
onAnswer={(event, option) => answer(event.seq, event.id!, option)}
|
|
559
|
+
onDecisionShown={onDecisionShown}
|
|
560
|
+
lineage={lineageOf}
|
|
561
|
+
/>
|
|
562
|
+
</div>
|
|
563
|
+
{session && (
|
|
564
|
+
<Questions
|
|
565
|
+
questions={session.questions ?? []}
|
|
566
|
+
running={running}
|
|
567
|
+
onAnswer={(request, verdict) =>
|
|
568
|
+
post('/peer/answer', { request, verdict }).then(load, report)
|
|
569
|
+
}
|
|
570
|
+
onExtend={(request) => post('/peer/extend', { request }).then(load, report)}
|
|
571
|
+
/>
|
|
572
|
+
)}
|
|
573
|
+
{error && (
|
|
574
|
+
<p role="alert" className="shrink-0 px-4 pb-1 text-xs text-destructive">
|
|
575
|
+
{error}
|
|
576
|
+
</p>
|
|
577
|
+
)}
|
|
578
|
+
{readOnly ? (
|
|
579
|
+
<p
|
|
580
|
+
role="note"
|
|
581
|
+
className="shrink-0 px-4 py-3 text-center text-sm text-secondary pb-[max(env(safe-area-inset-bottom),0.75rem)]"
|
|
582
|
+
>
|
|
583
|
+
Read only
|
|
584
|
+
</p>
|
|
585
|
+
) : (
|
|
586
|
+
<div className={promptBox}>
|
|
587
|
+
{running && (
|
|
588
|
+
<Button
|
|
589
|
+
variant="outline"
|
|
590
|
+
className="mb-2 min-h-11 w-full gap-2"
|
|
591
|
+
aria-label="Interrupt"
|
|
592
|
+
onClick={interrupt}
|
|
593
|
+
>
|
|
594
|
+
<Icon name="square" size={12} className="fill-current" />
|
|
595
|
+
Interrupt
|
|
596
|
+
</Button>
|
|
597
|
+
)}
|
|
598
|
+
<SessionPrompt
|
|
599
|
+
variant="mobile"
|
|
600
|
+
ctx={ctx}
|
|
601
|
+
at={{ slug: workspace.slug, node }}
|
|
602
|
+
session={id}
|
|
603
|
+
running={running}
|
|
604
|
+
handle={promptHandle}
|
|
605
|
+
above={
|
|
606
|
+
<>
|
|
607
|
+
{live && live.plan.length > 0 && <Tasks plan={live.plan} />}
|
|
608
|
+
{session && (
|
|
609
|
+
<Queued
|
|
610
|
+
items={session.queue ?? []}
|
|
611
|
+
notes={notesOf(session)}
|
|
612
|
+
onSend={(queued) =>
|
|
613
|
+
void post(`/queue/send?id=${encodeURIComponent(id)}`, {
|
|
614
|
+
item: queued,
|
|
615
|
+
}).then(load, report)
|
|
616
|
+
}
|
|
617
|
+
onEdit={(queued) => {
|
|
618
|
+
// Back to this session's draft, with its images, added to what is written there, through the
|
|
619
|
+
// prompt of the click: another session opened meanwhile has unmounted it (its draft stays).
|
|
620
|
+
const back =
|
|
621
|
+
session.queue?.find((each) => each.id === queued.id)?.attachments ?? [];
|
|
622
|
+
const prompt = promptHandle.current;
|
|
623
|
+
void post(`/queue/remove?id=${encodeURIComponent(id)}`, {
|
|
624
|
+
item: queued.id,
|
|
625
|
+
}).then(() => {
|
|
626
|
+
prompt?.addText(id, queued.text, back);
|
|
627
|
+
promptHandle.current?.focus();
|
|
628
|
+
return load();
|
|
629
|
+
}, report);
|
|
630
|
+
}}
|
|
631
|
+
onRemove={(queued) =>
|
|
632
|
+
void post(`/queue/remove?id=${encodeURIComponent(id)}`, {
|
|
633
|
+
item: queued,
|
|
634
|
+
}).then(load, report)
|
|
635
|
+
}
|
|
636
|
+
/>
|
|
637
|
+
)}
|
|
638
|
+
{docked && (
|
|
639
|
+
<DecisionDock
|
|
640
|
+
key={docked.seq}
|
|
641
|
+
event={docked}
|
|
642
|
+
relative={relative}
|
|
643
|
+
busy={!!answering[docked.seq]}
|
|
644
|
+
more={waiting.length - 1}
|
|
645
|
+
onRow={onRows[docked.seq] === false}
|
|
646
|
+
onAnswer={(option) => void answer(docked.seq, docked.id!, option)}
|
|
647
|
+
/>
|
|
648
|
+
)}
|
|
649
|
+
</>
|
|
650
|
+
}
|
|
651
|
+
accessory={
|
|
652
|
+
!synced ? (
|
|
653
|
+
<p role="status" className="text-xs text-muted">
|
|
654
|
+
Reconnecting…
|
|
655
|
+
</p>
|
|
656
|
+
) : undefined
|
|
657
|
+
}
|
|
658
|
+
/>
|
|
659
|
+
</div>
|
|
660
|
+
)}
|
|
661
|
+
</section>
|
|
662
|
+
);
|
|
663
|
+
}
|