@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,3434 @@
|
|
|
1
|
+
// The Sessions view (AD-29), a chat app: on a project node, a resizable split with the sessions of the node and of the
|
|
2
|
+
// nodes below it on the left (newest activity first: title, last message, time, harness·model, the node of one from below, a spinner while a turn runs, a dot while a decision
|
|
3
|
+
// waits) and the chosen session's conversation on the right. "New chat" (or no session chosen) shows a centred composer
|
|
4
|
+
// with the model picker (./model-picker.tsx: a harness and one of its models, the agent's own default unless another is
|
|
5
|
+
// chosen; ⌘⇧M) and a permission mode (the agent's own modes, its current one chosen); its first message creates the
|
|
6
|
+
// session (titled by that message) and sends it. In a session, the picker shows its model, locked (another one starts a
|
|
7
|
+
// new chat, keeping what is written); Permissions stays a choice, changed at any time (a line says so). A harness whose
|
|
8
|
+
// models cannot be listed is not ready: its fix hint shows and sending waits until another is chosen. The conversation comes from one watch subscription (an `EventSource` on `/sessions/watch`, resumed
|
|
9
|
+
// after the last `seq` it holds): the stored lines and the running turn's live part, drawn by ./conversation.tsx
|
|
10
|
+
// (research mockups session-ux-v1: each turn's work folded behind "Worked for …" once it completed, the answer in view);
|
|
11
|
+
// after an error it reconnects with a growing delay, saying so. Above the composer, docked on it: the running turn's
|
|
12
|
+
// plan (Tasks), the messages queued, and an open decision whose timeline row is out of view (a pointer to it, Show
|
|
13
|
+
// scrolling there) or not drawn (its options, Dismiss, its countdown; the allow option focused, Escape picks the deny
|
|
14
|
+
// one), the header saying "Waiting for you". Send starts a turn; while one runs Stop
|
|
15
|
+
// shows (Escape twice too) and a message written is queued (Queue, or Enter), sent when the turn completes; Send now
|
|
16
|
+
// puts it into the running turn when its harness steers, else stops the turn to send it; Edit takes it back to the composer. Up in an empty composer brings back the last message
|
|
17
|
+
// sent; "Scroll to end" shows when the reader scrolled up. The composer shows the context the agent last reported
|
|
18
|
+
// using. The list refreshes after the view's own actions, when the watched session starts or ends a turn, asks or
|
|
19
|
+
// closes a decision, or its queue changes, and every few seconds while another listed session runs. On the workspace
|
|
20
|
+
// (root), the same chat manages the workspace. Elsewhere, a notice.
|
|
21
|
+
// Triage (research-010 slice 1): the list has three collapsible shelves (open or closed per
|
|
22
|
+
// workspace and node, kept), Pinned (dragged into order, or moved up and down from its menu), Active (newest activity first), Working and Settled (newest settled
|
|
23
|
+
// first, 10 then "Show more"), each hidden when empty; Working and Settled start closed. Unless the node's "Fold working
|
|
24
|
+
// sessions" setting is off, Active is an inbox (bridge#210, `inbox` in ./listing.ts): a session whose agent works folds
|
|
25
|
+
// into Working and comes back on top of Active when it needs the user; "Show archived" lists the archived ones, restored from their menu.
|
|
26
|
+
// A row shows one status: a decision waiting, a turn running, a failed turn unread, unread (a turn ended since it was
|
|
27
|
+
// last opened), else idle (faded in Active). Its hover button settles it (un-settles in Settled); its menu (right-click,
|
|
28
|
+
// "…", and the open session's header) pins, settles, renames (in the header's title field), marks unread, archives.
|
|
29
|
+
// Settle, unpin and archive offer Undo for 5 s (also mod+z outside a text field); mod+shift+s and mod+shift+p settle and
|
|
30
|
+
// pin the open session, mod+shift+[ and ] open the previous and next one. Settling or archiving the open session opens
|
|
31
|
+
// the next Active one. Opening a session, and a turn ending while it is open, marks it read. The Sessions rail tile
|
|
32
|
+
// counts the sessions waiting for a decision, unread or woken, and spins while one runs (as of the last listing).
|
|
33
|
+
// Snooze (research-012), when the workspace has the scheduler (the listing's `snooze`): "Snooze ▸" in the menus (the
|
|
34
|
+
// kit's SnoozeMenu: the node's "Snooze choices" setting, soonest first, and Custom…, a SnoozeDialog) moves a session to the Snoozed shelf (between Active and
|
|
35
|
+
// Settled, soonest wake first, each row saying when it wakes; its hover button wakes it now) until its time; a pinned
|
|
36
|
+
// one asks first whether it stays pinned. Snoozing the open session opens the next Active one; Undo for 5 s. At its
|
|
37
|
+
// time it comes back Woke (a status after Unread) until opened; the page lists again at the soonest wake.
|
|
38
|
+
// Several windows (research-018 slice 5): each copy shows the session its window's `item` names (without one, the last
|
|
39
|
+
// session opened on the node, named as the item in place); choosing a session reports it with `ctx.screen.open`, so the
|
|
40
|
+
// URL, the sidebar and P8 follow, and a ⌘-click on a row opens it in a new window beside (the shell's). Its keys act
|
|
41
|
+
// only in the focused window (the `screen` slot's `focused`) and are declared to the shell's help. The listing, its
|
|
42
|
+
// poll, the snooze timer and the rail mark are shared by the copies on a node (./listing.ts); each keeps its own event
|
|
43
|
+
// stream. Drafts are kept per session for every copy; the details panel and the shelves stay per node.
|
|
44
|
+
// Child sessions (bridge#212 S3, ./children.tsx): a child whose parent is listed has no row on a shelf; it is listed
|
|
45
|
+
// under its parent's row (indented, the group joined by an accent bar on its left; three lines: its agent and its state
|
|
46
|
+
// in words, Working, Asking, Done, Failed, Stopped or Cancelled, then its title, then its worktree and its elapsed or end time;
|
|
47
|
+
// muted once finished), shown or hidden from the parent's "N children" toggle at the end of its
|
|
48
|
+
// last line (shown by default while one of them works, waits, or is the open session); its parent's row works and
|
|
49
|
+
// waits while a descendant does (`lineage`, ./listing.ts). The open session's lineage (`LineageContext`): its
|
|
50
|
+
// children's cards in the conversation, its running children docked on the composer (Open, and Stop: the child's
|
|
51
|
+
// session Stop), and, for a child, a link to its parent beside its title.
|
|
52
|
+
import * as React from 'react';
|
|
53
|
+
import {
|
|
54
|
+
Badge,
|
|
55
|
+
Button,
|
|
56
|
+
ContextMenu,
|
|
57
|
+
ContextMenuTrigger,
|
|
58
|
+
DropdownMenu,
|
|
59
|
+
DropdownMenuContent,
|
|
60
|
+
DropdownMenuItem,
|
|
61
|
+
DropdownMenuSeparator,
|
|
62
|
+
DropdownMenuShortcut,
|
|
63
|
+
DropdownMenuTrigger,
|
|
64
|
+
Dialog,
|
|
65
|
+
DialogBody,
|
|
66
|
+
DialogContent,
|
|
67
|
+
DialogDescription,
|
|
68
|
+
DialogFooter,
|
|
69
|
+
DialogHeader,
|
|
70
|
+
DialogTitle,
|
|
71
|
+
EmptyState,
|
|
72
|
+
Icon,
|
|
73
|
+
Input,
|
|
74
|
+
List,
|
|
75
|
+
Pane,
|
|
76
|
+
PaneBody,
|
|
77
|
+
PaneHeader,
|
|
78
|
+
ListItem,
|
|
79
|
+
ScrollArea,
|
|
80
|
+
Select,
|
|
81
|
+
SelectContent,
|
|
82
|
+
SelectItem,
|
|
83
|
+
SelectTrigger,
|
|
84
|
+
SplitHandle,
|
|
85
|
+
SplitPane,
|
|
86
|
+
SplitView,
|
|
87
|
+
SidebarGroup,
|
|
88
|
+
SidebarGroupContent,
|
|
89
|
+
SidebarGroupTrigger,
|
|
90
|
+
SnoozeDialog,
|
|
91
|
+
SnoozeMenu,
|
|
92
|
+
SortableItem,
|
|
93
|
+
SortableList,
|
|
94
|
+
Tooltip,
|
|
95
|
+
TooltipContent,
|
|
96
|
+
TooltipTrigger,
|
|
97
|
+
cn,
|
|
98
|
+
isRecord,
|
|
99
|
+
readJson,
|
|
100
|
+
snoozeChoicesOf,
|
|
101
|
+
snoozeWakeLabel,
|
|
102
|
+
useToast,
|
|
103
|
+
formatDate,
|
|
104
|
+
formatNumber,
|
|
105
|
+
isComposing,
|
|
106
|
+
modalOpen,
|
|
107
|
+
} from '@bridge/ui';
|
|
108
|
+
import type { Context } from 'cordis';
|
|
109
|
+
import type {} from '@bridge/ui-renderer/client';
|
|
110
|
+
import type { ViewComponentRenderer } from './views/view.tsx';
|
|
111
|
+
import { SessionDetails } from './changes.tsx';
|
|
112
|
+
import { SessionPrompt } from '@bridge/session-ui/prompt';
|
|
113
|
+
import { shownModel, type HarnessModels, type PickerModel } from '@bridge/session-ui/model-picker';
|
|
114
|
+
import { ProviderMark, providerOf } from '@bridge/session-ui/providers';
|
|
115
|
+
import {
|
|
116
|
+
Conversation,
|
|
117
|
+
DecisionDock,
|
|
118
|
+
Queued,
|
|
119
|
+
type QueuedNote,
|
|
120
|
+
Spinner,
|
|
121
|
+
Tasks,
|
|
122
|
+
decisionsOf,
|
|
123
|
+
NO_DECISIONS,
|
|
124
|
+
type Live,
|
|
125
|
+
type LiveEvent,
|
|
126
|
+
type Stored,
|
|
127
|
+
} from './conversation.tsx';
|
|
128
|
+
import { Questions, type Question } from './questions.tsx';
|
|
129
|
+
import { GalleryPins, type Attached, type Pinned, type Viewed } from '@bridge/session-ui/media';
|
|
130
|
+
import { GalleryDialog, type Scope } from './gallery.tsx';
|
|
131
|
+
import { BranchActions, CleanupDialog, cleanupOf, type Cleanup } from './branch-actions.tsx';
|
|
132
|
+
import { PullRequestPill, type SessionPullRequest } from './pull-request.tsx';
|
|
133
|
+
import { viewTools as viewToolsOf, Missing } from './views.tsx';
|
|
134
|
+
import { Tab, TabStrip, TabsProvider, useSessionTabs, type TabSlotProps } from './tabs.tsx';
|
|
135
|
+
import { MobileSessions } from './mobile.tsx';
|
|
136
|
+
import {
|
|
137
|
+
SubagentMarker,
|
|
138
|
+
itemParts,
|
|
139
|
+
subagentItem,
|
|
140
|
+
subagentState,
|
|
141
|
+
type SubagentEntry,
|
|
142
|
+
} from './subagents.tsx';
|
|
143
|
+
import { SubagentView } from './subagent-view.tsx';
|
|
144
|
+
import {
|
|
145
|
+
acquire,
|
|
146
|
+
childState as childStateOf,
|
|
147
|
+
claimMark,
|
|
148
|
+
inbox,
|
|
149
|
+
lineage,
|
|
150
|
+
listingStore,
|
|
151
|
+
type ChildState,
|
|
152
|
+
type ListingStore,
|
|
153
|
+
} from '../listing.ts';
|
|
154
|
+
import {
|
|
155
|
+
BackgroundWork,
|
|
156
|
+
ChildStateMark,
|
|
157
|
+
LineageContext,
|
|
158
|
+
ParentLink,
|
|
159
|
+
type Delegation,
|
|
160
|
+
type Lineage,
|
|
161
|
+
} from './children.tsx';
|
|
162
|
+
import { SessionScheduled } from './session-scheduled.tsx';
|
|
163
|
+
import { Elapsed } from './elapsed.tsx';
|
|
164
|
+
import { showLink } from './open-link.tsx';
|
|
165
|
+
import { viewerActivity } from '../viewer-activity.ts';
|
|
166
|
+
import { answerBody, type Answer } from '../answers.ts';
|
|
167
|
+
|
|
168
|
+
export const name = 'sessions';
|
|
169
|
+
export const inject = ['slots', 'workspace', 'nodes', 'sidebar', 'shell', 'screen'];
|
|
170
|
+
|
|
171
|
+
/** The worktree this page shows, when Sessions is lent to it by its workspace (research-045): the page's slug when it
|
|
172
|
+
* is not this plugin's workspace. Its sessions are listed there only, and a new one is bound to it. */
|
|
173
|
+
export const shownWorktree = (workspace: { slug: string }) => {
|
|
174
|
+
const shown = decodeURIComponent(location.pathname.split('/')[1] ?? '');
|
|
175
|
+
return shown !== '' && shown !== workspace.slug ? shown : undefined;
|
|
176
|
+
};
|
|
177
|
+
/** `url` asking for worktree `worktree`'s sessions only, when there is one. */
|
|
178
|
+
const forWorktree = (url: string, worktree: string | undefined) =>
|
|
179
|
+
worktree === undefined
|
|
180
|
+
? url
|
|
181
|
+
: `${url}${url.includes('?') ? '&' : '?'}worktree=${encodeURIComponent(worktree)}`;
|
|
182
|
+
/** Each worktree's name as the page shows it (its title without its workspace's), by slug (research-045). */
|
|
183
|
+
const useWorktreeNames = (workspace: Context['workspace']) => {
|
|
184
|
+
const [names, setNames] = React.useState<Map<string, string>>(new Map());
|
|
185
|
+
React.useEffect(() => {
|
|
186
|
+
let live = true;
|
|
187
|
+
void workspace
|
|
188
|
+
.fetch('/_bridge/workspaces')
|
|
189
|
+
.then(readJson)
|
|
190
|
+
.then((body) => {
|
|
191
|
+
const listed = (body as { workspaces: { slug: string; title: string; parent?: string }[] })
|
|
192
|
+
.workspaces;
|
|
193
|
+
const title = listed.find(({ slug }) => slug === workspace.slug)?.title ?? workspace.slug;
|
|
194
|
+
if (live)
|
|
195
|
+
setNames(
|
|
196
|
+
new Map(
|
|
197
|
+
listed
|
|
198
|
+
.filter(({ parent }) => parent === workspace.slug)
|
|
199
|
+
.map(({ slug, title: own }) => [slug, own.replace(`${title} · `, '')]),
|
|
200
|
+
),
|
|
201
|
+
);
|
|
202
|
+
})
|
|
203
|
+
.catch(() => {});
|
|
204
|
+
return () => {
|
|
205
|
+
live = false;
|
|
206
|
+
};
|
|
207
|
+
}, [workspace]);
|
|
208
|
+
return names;
|
|
209
|
+
};
|
|
210
|
+
/** The picker's value for a new branch, made at the first send (research-083): no git branch has this name. */
|
|
211
|
+
export const NEW_BRANCH = ' new';
|
|
212
|
+
/** A git project's branches as its Branches screen lists them (research-083), for the new chat's branch; none for a
|
|
213
|
+
* node that is not a git project. `chosen`: the one a new session works on, by default a new branch when the project's
|
|
214
|
+
* setting says so (`newSessionBranch`), else the project's active branch, else main. */
|
|
215
|
+
const useBranches = (slug: string, node: string, startNew = false) => {
|
|
216
|
+
const [branches, setBranches] =
|
|
217
|
+
React.useState<{ name: string; main: boolean; active?: boolean }[]>();
|
|
218
|
+
const [chosen, choose] = React.useState<string>();
|
|
219
|
+
const [version, setVersion] = React.useState(0);
|
|
220
|
+
React.useEffect(() => {
|
|
221
|
+
let gone = false;
|
|
222
|
+
setBranches(undefined);
|
|
223
|
+
choose(undefined);
|
|
224
|
+
if (node === 'root') return;
|
|
225
|
+
fetch(
|
|
226
|
+
`/_bridge/host/branches?workspace=${encodeURIComponent(slug)}&node=${encodeURIComponent(node)}`,
|
|
227
|
+
{ cache: 'no-store' },
|
|
228
|
+
)
|
|
229
|
+
.then((res) => (res.ok ? readJson(res) : undefined))
|
|
230
|
+
.then((body: unknown) => {
|
|
231
|
+
if (gone || !isRecord(body) || !Array.isArray(body.branches)) return;
|
|
232
|
+
// The host's own answer: typed once where it is read.
|
|
233
|
+
const listed = body.branches as { name: string; main: boolean; active?: boolean }[];
|
|
234
|
+
setBranches(listed);
|
|
235
|
+
choose(
|
|
236
|
+
startNew
|
|
237
|
+
? NEW_BRANCH
|
|
238
|
+
: (listed.find((each) => each.active && !each.main) ?? listed.find((each) => each.main))
|
|
239
|
+
?.name,
|
|
240
|
+
);
|
|
241
|
+
})
|
|
242
|
+
.catch((error: unknown) => console.warn('Branches unavailable', error));
|
|
243
|
+
return () => {
|
|
244
|
+
gone = true;
|
|
245
|
+
};
|
|
246
|
+
}, [slug, node, startNew, version]);
|
|
247
|
+
const main = branches?.find((each) => each.main)?.name;
|
|
248
|
+
return {
|
|
249
|
+
branches,
|
|
250
|
+
chosen,
|
|
251
|
+
choose,
|
|
252
|
+
onBranch: chosen !== undefined && chosen !== main && chosen !== NEW_BRANCH ? chosen : undefined,
|
|
253
|
+
newBranch: chosen === NEW_BRANCH,
|
|
254
|
+
/** Reads the branches again (one made or deleted). */
|
|
255
|
+
reload: () => setVersion((now) => now + 1),
|
|
256
|
+
};
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
export interface Session {
|
|
260
|
+
/** Its pull request (research-086). */
|
|
261
|
+
pullRequest?: SessionPullRequest;
|
|
262
|
+
/** The worktree it works in (research-045). */
|
|
263
|
+
worktree?: { slug: string; folder: string };
|
|
264
|
+
/** The branch of its project it works on (research-083); `temporary` until its first message names it. */
|
|
265
|
+
branch?: { name: string; folder: string; temporary?: true };
|
|
266
|
+
/** A child session's parent session (bridge#212). */
|
|
267
|
+
parent?: string;
|
|
268
|
+
/** The work it delegated to its children, with their state and results (bridge#212). */
|
|
269
|
+
delegations?: Delegation[];
|
|
270
|
+
/** Its agent's own sub-agents, the newest (bridge#212 S8). */
|
|
271
|
+
subagents?: SubagentEntry[];
|
|
272
|
+
id: string;
|
|
273
|
+
node: string;
|
|
274
|
+
title: string;
|
|
275
|
+
harness: string;
|
|
276
|
+
model?: string;
|
|
277
|
+
mode?: string;
|
|
278
|
+
createdAt: string;
|
|
279
|
+
updatedAt?: string;
|
|
280
|
+
preview?: string;
|
|
281
|
+
running?: boolean;
|
|
282
|
+
waiting?: boolean;
|
|
283
|
+
unread?: boolean;
|
|
284
|
+
snoozedUntil?: string;
|
|
285
|
+
woke?: boolean;
|
|
286
|
+
pinnedAt?: string;
|
|
287
|
+
pinOrder?: number;
|
|
288
|
+
settledAt?: string;
|
|
289
|
+
settledBy?: 'user' | 'auto';
|
|
290
|
+
lastTurn?: { endedAt: string; reason: 'completed' | 'interrupted' | 'failed' };
|
|
291
|
+
tags?: string[];
|
|
292
|
+
files?: string[];
|
|
293
|
+
commits?: { sha: string; at: string; message: string; files: string[] }[];
|
|
294
|
+
claims?: { path: string; at: string; note?: string }[];
|
|
295
|
+
inbox?: {
|
|
296
|
+
request?: string;
|
|
297
|
+
from: string;
|
|
298
|
+
/** The sender's node (none on a message stored before it was kept). */
|
|
299
|
+
node?: string;
|
|
300
|
+
title: string;
|
|
301
|
+
kind: 'notify' | 'ask';
|
|
302
|
+
text: string;
|
|
303
|
+
at: string;
|
|
304
|
+
}[];
|
|
305
|
+
questions?: Question[];
|
|
306
|
+
/** The decisions a turn Bridge cut left open (bridge#201): answered as a running turn's are. */
|
|
307
|
+
openDecisions?: {
|
|
308
|
+
id: string;
|
|
309
|
+
title: string;
|
|
310
|
+
options: { id: string; label: string; kind: 'allow' | 'deny' | 'other' }[];
|
|
311
|
+
tool?: string;
|
|
312
|
+
}[];
|
|
313
|
+
queue?: {
|
|
314
|
+
id: string;
|
|
315
|
+
text: string;
|
|
316
|
+
at: string;
|
|
317
|
+
attachments?: Attached[];
|
|
318
|
+
scheduled?: { id: string; title: string };
|
|
319
|
+
/** Bridge's words: a child's results (`delegations`), questions (`questions`), another session's message. */
|
|
320
|
+
from?: 'bridge';
|
|
321
|
+
delegations?: string[];
|
|
322
|
+
questions?: { session: string; decision: string; text: string }[];
|
|
323
|
+
}[];
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** The ids of the decisions a turn Bridge cut left open in `session`: the same array while they stay. */
|
|
327
|
+
export const useLeftDecisions = (session: Session | undefined) => {
|
|
328
|
+
const key = (session?.openDecisions ?? []).map((each) => each.id).join('\n');
|
|
329
|
+
return React.useMemo(() => (key ? key.split('\n') : NO_DECISIONS), [key]);
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
/** The notes other sessions left for `session`'s next turn, each with a link to its sender: its session on its node
|
|
333
|
+
* (without the node, an item of the screen the window shows). */
|
|
334
|
+
export const notesOf = (session: Session): QueuedNote[] =>
|
|
335
|
+
(session.inbox ?? [])
|
|
336
|
+
.filter((each) => each.kind === 'notify')
|
|
337
|
+
.map(({ from, node, title, text, at }) => ({
|
|
338
|
+
from,
|
|
339
|
+
title,
|
|
340
|
+
text,
|
|
341
|
+
at,
|
|
342
|
+
to: node
|
|
343
|
+
? { node, screen: { plugin: name, id: 'sessions' }, item: from }
|
|
344
|
+
: { item: from, reuse: true },
|
|
345
|
+
}));
|
|
346
|
+
type Action = 'settle' | 'unsettle' | 'pin' | 'unpin' | 'unread' | 'archive' | 'restore' | 'wake';
|
|
347
|
+
type Shelf = 'pinned' | 'active' | 'working' | 'snoozed' | 'settled' | 'archived';
|
|
348
|
+
/** Settled rows shown before "Show more". */
|
|
349
|
+
const SETTLED_SHOWN = 10;
|
|
350
|
+
/** How long Undo is offered, ms. */
|
|
351
|
+
const UNDO_MS = 5000;
|
|
352
|
+
/** A Mac shows ⌘ for the shortcuts' mod key, others Ctrl. */
|
|
353
|
+
const mac = typeof navigator !== 'undefined' && /Mac|iP(hone|ad)/.test(navigator.platform);
|
|
354
|
+
const shortcut = (key: string) => (mac ? `⇧⌘${key}` : `Ctrl+Shift+${key}`);
|
|
355
|
+
/** Focus is in a text field: mod+z there is the field's own undo. */
|
|
356
|
+
const editing = (target: EventTarget | null) =>
|
|
357
|
+
target instanceof HTMLElement &&
|
|
358
|
+
(target.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(target.tagName));
|
|
359
|
+
export interface Listing {
|
|
360
|
+
project: { path: string } | null;
|
|
361
|
+
workspace: { slug: string; folder?: string } | null;
|
|
362
|
+
profiles?: {
|
|
363
|
+
id: string;
|
|
364
|
+
harness: string;
|
|
365
|
+
driver: string;
|
|
366
|
+
displayName?: string;
|
|
367
|
+
color?: string;
|
|
368
|
+
available: boolean;
|
|
369
|
+
}[];
|
|
370
|
+
harnesses: {
|
|
371
|
+
name: string;
|
|
372
|
+
displayName?: string;
|
|
373
|
+
color?: string;
|
|
374
|
+
driver: string;
|
|
375
|
+
available: boolean;
|
|
376
|
+
/** What its driver can do; `steer`: a message goes into a running turn (bridge#212 S6). */
|
|
377
|
+
capabilities?: { steer?: boolean };
|
|
378
|
+
}[];
|
|
379
|
+
sessions: Session[];
|
|
380
|
+
snooze?: boolean;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** `live` after stored line `event`: a turn starts and ends; a settled segment or tool leaves the live part. */
|
|
384
|
+
export const afterStored = (live: Live | null, event: Stored): Live | null => {
|
|
385
|
+
if (event.type === 'turn/start')
|
|
386
|
+
return { seq: event.seq, text: '', thought: '', tools: {}, plan: [], decisions: [] };
|
|
387
|
+
if (!live || event.type === 'turn/end') return null;
|
|
388
|
+
// A sub-agent's settled words, or its end, leave its own pending part (bridge#212 S8).
|
|
389
|
+
const sub = event.subagent;
|
|
390
|
+
if (sub !== undefined && (event.type === 'assistant/message' || event.type === 'subagent')) {
|
|
391
|
+
const pending = live.subagents?.[sub];
|
|
392
|
+
if (!pending) return { ...live, seq: event.seq };
|
|
393
|
+
const { [sub]: _ended, ...others } = live.subagents!;
|
|
394
|
+
return {
|
|
395
|
+
...live,
|
|
396
|
+
seq: event.seq,
|
|
397
|
+
subagents:
|
|
398
|
+
event.type === 'subagent' && event.status !== 'running'
|
|
399
|
+
? others
|
|
400
|
+
: { ...others, [sub]: { ...pending, text: '' } },
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
if (event.type === 'assistant/message') return { ...live, seq: event.seq, text: '' };
|
|
404
|
+
if (event.type === 'decision/answered')
|
|
405
|
+
return {
|
|
406
|
+
...live,
|
|
407
|
+
seq: event.seq,
|
|
408
|
+
decisions: live.decisions.filter((each) => each.id !== event.id),
|
|
409
|
+
};
|
|
410
|
+
if (event.type === 'tool/result') {
|
|
411
|
+
const { [event.id ?? '']: _settled, ...tools } = live.tools;
|
|
412
|
+
return { ...live, seq: event.seq, tools };
|
|
413
|
+
}
|
|
414
|
+
// The stored view line takes over the draft the agent was writing (research-036 streaming).
|
|
415
|
+
if (event.type === 'view') {
|
|
416
|
+
const { view: _draft, ...rest } = live;
|
|
417
|
+
return { ...rest, seq: event.seq };
|
|
418
|
+
}
|
|
419
|
+
return { ...live, seq: event.seq };
|
|
420
|
+
};
|
|
421
|
+
/** `live` after harness event `event` (as the server's accumulator does). */
|
|
422
|
+
export const afterLive = (live: Live | null, event: LiveEvent): Live | null => {
|
|
423
|
+
if (!live) return live;
|
|
424
|
+
// A sub-agent's words and thought are its own (bridge#212 S8).
|
|
425
|
+
if (event.subagent !== undefined && (event.type === 'text' || event.type === 'thought')) {
|
|
426
|
+
const pending = live.subagents?.[event.subagent] ?? { text: '', thought: '' };
|
|
427
|
+
return {
|
|
428
|
+
...live,
|
|
429
|
+
subagents: {
|
|
430
|
+
...live.subagents,
|
|
431
|
+
[event.subagent]:
|
|
432
|
+
event.type === 'text'
|
|
433
|
+
? { ...pending, text: pending.text + (event.text ?? '') }
|
|
434
|
+
: { ...pending, thought: pending.thought + (event.text ?? '') },
|
|
435
|
+
},
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
if (event.type === 'text') return { ...live, text: live.text + (event.text ?? '') };
|
|
439
|
+
if (event.type === 'thought') return { ...live, thought: live.thought + (event.text ?? '') };
|
|
440
|
+
if (event.type === 'plan') return { ...live, plan: event.entries ?? [] };
|
|
441
|
+
if (event.type === 'usage') return { ...live, usage: { used: event.used, size: event.size } };
|
|
442
|
+
if (event.type === 'decision') return { ...live, decisions: [...live.decisions, event] };
|
|
443
|
+
if (event.type === 'view/draft')
|
|
444
|
+
return {
|
|
445
|
+
...live,
|
|
446
|
+
view: {
|
|
447
|
+
id: event.id ?? '',
|
|
448
|
+
...(event.title !== undefined && { title: event.title }),
|
|
449
|
+
program: event.program ?? '',
|
|
450
|
+
},
|
|
451
|
+
};
|
|
452
|
+
if (event.type === 'tool') {
|
|
453
|
+
const { [event.id ?? '']: _was, ...tools } = live.tools;
|
|
454
|
+
const done = event.status === 'completed' || event.status === 'failed';
|
|
455
|
+
// A show_view call done: its draft goes, as the server's does (its view line follows).
|
|
456
|
+
const { view: _draft, ...rest } = live;
|
|
457
|
+
return {
|
|
458
|
+
...(done && live.view?.id === event.id ? rest : live),
|
|
459
|
+
tools:
|
|
460
|
+
event.status === 'completed' || event.status === 'failed'
|
|
461
|
+
? tools
|
|
462
|
+
: { ...tools, [event.id ?? '']: event },
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
return live;
|
|
466
|
+
};
|
|
467
|
+
/** Stored lines after which the session list is fetched again (its spinner, dot, preview, and order). */
|
|
468
|
+
export const listChanges = new Set([
|
|
469
|
+
'turn/start',
|
|
470
|
+
'turn/end',
|
|
471
|
+
'decision/asked',
|
|
472
|
+
'decision/answered',
|
|
473
|
+
'mode/changed',
|
|
474
|
+
'effort/changed',
|
|
475
|
+
'commit',
|
|
476
|
+
'peer/sent',
|
|
477
|
+
'peer/received',
|
|
478
|
+
'peer/answer',
|
|
479
|
+
'subagent',
|
|
480
|
+
]);
|
|
481
|
+
|
|
482
|
+
/** How long ago `iso` was, short: "now", "5 min", "3 h", "yesterday", a weekday, then a date. */
|
|
483
|
+
/** A wake time as the user reads it: "at 6:00 PM" today, else "Tue 9:00 AM", or a date further than a week. */
|
|
484
|
+
const when = (iso: string) => {
|
|
485
|
+
const date = new Date(iso);
|
|
486
|
+
const time = formatDate(date, { hour: 'numeric', minute: '2-digit' });
|
|
487
|
+
if (date.toDateString() === new Date().toDateString()) return `at ${time}`;
|
|
488
|
+
const days = (date.getTime() - Date.now()) / 86_400_000;
|
|
489
|
+
return days < 6
|
|
490
|
+
? `${formatDate(date, { weekday: 'short' })} ${time}`
|
|
491
|
+
: `${formatDate(date, { day: 'numeric', month: 'short' })} ${time}`;
|
|
492
|
+
};
|
|
493
|
+
export const ago = (iso: string) => {
|
|
494
|
+
const then = new Date(iso);
|
|
495
|
+
const minutes = Math.floor((Date.now() - then.getTime()) / 60_000);
|
|
496
|
+
if (minutes < 1) return 'now';
|
|
497
|
+
if (minutes < 60) return `${minutes} min`;
|
|
498
|
+
const today = new Date();
|
|
499
|
+
today.setHours(0, 0, 0, 0);
|
|
500
|
+
const days = Math.ceil((today.getTime() - then.getTime()) / 86_400_000);
|
|
501
|
+
if (days <= 0) return `${Math.floor(minutes / 60)} h`;
|
|
502
|
+
if (days === 1) return 'yesterday';
|
|
503
|
+
if (days < 7) return formatDate(then, { weekday: 'short' });
|
|
504
|
+
return formatDate(then, { month: 'short', day: 'numeric' });
|
|
505
|
+
};
|
|
506
|
+
/** The page has no full CSS reset: browser margins and list padding cleared at zero specificity (any class wins). */
|
|
507
|
+
export const reset =
|
|
508
|
+
'[:where(&)_:is(p,ol,ul,pre,blockquote,h1,h2,h3,h4,h5,h6,figure)]:m-0 [:where(&)_:is(ol,ul)]:p-0';
|
|
509
|
+
/** Base UI's scroll content is at least as wide as its content: held to the pane's width so rows truncate. */
|
|
510
|
+
const fitWidth = '[&>[data-slot=scroll-area-content]]:min-w-0!';
|
|
511
|
+
/** The lines of each history page the conversation reads, and how near its top (px) the page before it loads. */
|
|
512
|
+
const HISTORY_PAGE = 200;
|
|
513
|
+
const EARLIER_AT = 400;
|
|
514
|
+
interface Earlier {
|
|
515
|
+
id: string;
|
|
516
|
+
/** The first whole turn in view (its `data-turn`) and its distance from the viewport's top, px. */
|
|
517
|
+
anchor?: string | undefined;
|
|
518
|
+
offset?: number;
|
|
519
|
+
/** Else the viewport's height and scroll before the page landed. */
|
|
520
|
+
height?: number;
|
|
521
|
+
top?: number;
|
|
522
|
+
failed?: boolean;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/** The node's listing, shared by every copy of the screen on the page (./listing.ts). */
|
|
526
|
+
export function useListing(ctx: Context, node: string) {
|
|
527
|
+
const { workspace } = ctx;
|
|
528
|
+
const make = (): ListingStore<Listing> => {
|
|
529
|
+
const api = `/${workspace.slug}/api/sessions/sessions`;
|
|
530
|
+
const headers = { 'x-bridge-node': node };
|
|
531
|
+
return listingStore<Listing>({
|
|
532
|
+
list: () =>
|
|
533
|
+
workspace
|
|
534
|
+
.fetch(forWorktree(api, shownWorktree(workspace)), { headers })
|
|
535
|
+
.then(readJson)
|
|
536
|
+
.then((body) => body as Listing),
|
|
537
|
+
tags: () =>
|
|
538
|
+
workspace
|
|
539
|
+
.fetch(`${api}/tags`, { headers })
|
|
540
|
+
.then(readJson)
|
|
541
|
+
.then((body) => (body as { tags: { tag: string }[] }).tags.map(({ tag }) => tag)),
|
|
542
|
+
// The rail tile: sessions waiting for a decision, unread or woken (one shown is being read), spinning while one runs;
|
|
543
|
+
// set by the node of the focused Sessions window (`claimMark`), cleared with the last one.
|
|
544
|
+
mark: (mark) => (mark ? ctx.sidebar.set('sessions', mark) : ctx.sidebar.clear('sessions')),
|
|
545
|
+
});
|
|
546
|
+
};
|
|
547
|
+
const [held, setHeld] = React.useState<ListingStore<Listing>>();
|
|
548
|
+
React.useEffect(() => {
|
|
549
|
+
const { store, release } = acquire(ctx, node, make);
|
|
550
|
+
setHeld(store);
|
|
551
|
+
return release;
|
|
552
|
+
}, [node]);
|
|
553
|
+
const subscribe = React.useCallback(
|
|
554
|
+
(listener: () => void) => held?.subscribe(listener) ?? (() => {}),
|
|
555
|
+
[held],
|
|
556
|
+
);
|
|
557
|
+
const snapshot = React.useSyncExternalStore(subscribe, () => held?.get());
|
|
558
|
+
return { store: held, listing: snapshot?.listing, known: snapshot?.tags ?? noTags };
|
|
559
|
+
}
|
|
560
|
+
const noTags: string[] = [];
|
|
561
|
+
/** Names each listed session for the shell (`ctx.screen.title`): the windows showing it, their bar and breadcrumb, say
|
|
562
|
+
* its title rather than its id, and follow it as it changes; "New session" while it has none. Given again only when
|
|
563
|
+
* it changed. */
|
|
564
|
+
export function useItemTitles(
|
|
565
|
+
ctx: Context,
|
|
566
|
+
sessions: readonly { id: string; title: string; branch?: { name: string } }[] | undefined,
|
|
567
|
+
/** The project's own branch, when it has others: a session without a branch works on it. */
|
|
568
|
+
main?: string,
|
|
569
|
+
) {
|
|
570
|
+
const named = React.useRef(new Map<string, string>());
|
|
571
|
+
React.useEffect(() => {
|
|
572
|
+
for (const each of sessions ?? []) {
|
|
573
|
+
const title = each.title.trim() || 'New session';
|
|
574
|
+
// Its branch in the breadcrumb, in place of "Sessions" (research-083).
|
|
575
|
+
const context = each.branch?.name ?? main;
|
|
576
|
+
const said = `${title}\n${context ?? ''}`;
|
|
577
|
+
if (named.current.get(each.id) === said) continue;
|
|
578
|
+
try {
|
|
579
|
+
ctx.screen.title?.(each.id, title, context);
|
|
580
|
+
named.current.set(each.id, said);
|
|
581
|
+
} catch {
|
|
582
|
+
// An older shell, or an item it does not know: the window names it as it is.
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
}, [sessions, main]);
|
|
586
|
+
}
|
|
587
|
+
/** The shelves kept in this browser (`{}` when none): a stored value is checked before its fields are read. */
|
|
588
|
+
const shelvesOf = (stored: unknown): Partial<Record<Shelf, boolean>> => {
|
|
589
|
+
if (!isRecord(stored)) return {};
|
|
590
|
+
const shelves: Partial<Record<Shelf, boolean>> = {};
|
|
591
|
+
for (const shelf of ['pinned', 'active', 'working', 'snoozed', 'settled', 'archived'] as const)
|
|
592
|
+
if (typeof stored[shelf] === 'boolean') shelves[shelf] = stored[shelf];
|
|
593
|
+
return shelves;
|
|
594
|
+
};
|
|
595
|
+
|
|
596
|
+
/** What a `views.component` entry gets (research-036 P2): the component a view calls, its evaluated props, and the
|
|
597
|
+
* node the session is on. One entry per plugin, keyed by its plugin key, draws all of that plugin's components by
|
|
598
|
+
* `component`; a plugin with no entry there falls back to the ask that proposes activating it. */
|
|
599
|
+
interface ViewComponentSlotProps {
|
|
600
|
+
component: string;
|
|
601
|
+
props: Record<string, unknown>;
|
|
602
|
+
node: string;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/** One copy of the screen, in one window: it shows the session its window's `item` names (research-018 slice 5), and
|
|
606
|
+
* reports the user's choice with `ctx.screen.open` (the URL, the sidebar and P8 follow); without an item, the last
|
|
607
|
+
* session opened on this node, named as the window's item. Its keys act only while its window is `focused`. */
|
|
608
|
+
type RenderTabs = {
|
|
609
|
+
(name: 'sessions.tab', owner: TabSlotProps): React.ReactNode;
|
|
610
|
+
(
|
|
611
|
+
name: 'views.component',
|
|
612
|
+
owner: ViewComponentSlotProps,
|
|
613
|
+
options: { entryKey: string; fallback: React.ReactNode },
|
|
614
|
+
): React.ReactNode;
|
|
615
|
+
};
|
|
616
|
+
|
|
617
|
+
/** What a row's menus and buttons call: the page's own, as it is when they are used. */
|
|
618
|
+
export interface RowActions {
|
|
619
|
+
act: (each: Session, action: Action) => Promise<void>;
|
|
620
|
+
menuItems: (each: Session) => React.ReactNode;
|
|
621
|
+
setTagged: React.Dispatch<React.SetStateAction<string | undefined>>;
|
|
622
|
+
/** Shows or hides session `id`'s children under its row. */
|
|
623
|
+
toggleChildren?: (id: string) => void;
|
|
624
|
+
}
|
|
625
|
+
/** A row of the session list: what it shows, worked out by the page (the same values, the row not drawn again). */
|
|
626
|
+
interface RowProps {
|
|
627
|
+
each: Session;
|
|
628
|
+
shelf: Shelf;
|
|
629
|
+
status: string;
|
|
630
|
+
faded: boolean;
|
|
631
|
+
/** When it wakes (snoozed), else how long ago it was active. */
|
|
632
|
+
stamp: string;
|
|
633
|
+
worktreeName?: string | undefined;
|
|
634
|
+
modelLabel: string;
|
|
635
|
+
provider: ReturnType<typeof providerOf>;
|
|
636
|
+
/** Its node's title, when it is not the page's. */
|
|
637
|
+
nodeTitle?: string | undefined;
|
|
638
|
+
tagged?: string | undefined;
|
|
639
|
+
actions: React.RefObject<RowActions | undefined>;
|
|
640
|
+
/** A child's nesting under its top row (bridge#212): indented, three lines, the group's bar on its left. */
|
|
641
|
+
depth?: number;
|
|
642
|
+
/** A child's state (`childState`), when its work started and when it ended. */
|
|
643
|
+
childState?: ChildState | undefined;
|
|
644
|
+
childSince?: string | undefined;
|
|
645
|
+
childEnded?: string | undefined;
|
|
646
|
+
/** What a working child does, when its state does not say it all ("Preparing its branch and stage"). */
|
|
647
|
+
childDetail?: string | undefined;
|
|
648
|
+
/** A child's agent: its profile's name. */
|
|
649
|
+
agent?: string | undefined;
|
|
650
|
+
/** Its children listed under it, and whether they show (with its sub-agents, `agents`). */
|
|
651
|
+
kids?: number;
|
|
652
|
+
kidsOpen?: boolean;
|
|
653
|
+
/** Its agent's sub-agents listed under it (bridge#212 S8). */
|
|
654
|
+
agents?: number;
|
|
655
|
+
}
|
|
656
|
+
/** A row's menu, its items worked out when it opens (the page as it is then). */
|
|
657
|
+
const RowMenu = ({
|
|
658
|
+
each,
|
|
659
|
+
actions,
|
|
660
|
+
}: {
|
|
661
|
+
each: Session;
|
|
662
|
+
actions: React.RefObject<RowActions | undefined>;
|
|
663
|
+
}) => actions.current!.menuItems(each);
|
|
664
|
+
/** A row's status as its mark (none when idle). */
|
|
665
|
+
const statusMark = (status: string) => {
|
|
666
|
+
if (status === 'decision')
|
|
667
|
+
return (
|
|
668
|
+
<span
|
|
669
|
+
role="img"
|
|
670
|
+
aria-label="Decision waiting"
|
|
671
|
+
className="inline-flex shrink-0 items-center gap-1 text-xs font-medium text-accent"
|
|
672
|
+
>
|
|
673
|
+
<Icon name="bell" size={12} />
|
|
674
|
+
Decision
|
|
675
|
+
</span>
|
|
676
|
+
);
|
|
677
|
+
if (status === 'question')
|
|
678
|
+
return (
|
|
679
|
+
<span
|
|
680
|
+
role="img"
|
|
681
|
+
aria-label="A question past its deadline"
|
|
682
|
+
className="inline-flex shrink-0 items-center gap-1 text-xs font-medium text-accent"
|
|
683
|
+
>
|
|
684
|
+
<Icon name="bell" size={12} />
|
|
685
|
+
Your call
|
|
686
|
+
</span>
|
|
687
|
+
);
|
|
688
|
+
if (status === 'running') return <Spinner />;
|
|
689
|
+
if (status === 'failed')
|
|
690
|
+
return (
|
|
691
|
+
<span role="img" aria-label="Failed" className="inline-flex shrink-0 text-destructive">
|
|
692
|
+
<Icon name="triangle-alert" size={14} />
|
|
693
|
+
</span>
|
|
694
|
+
);
|
|
695
|
+
if (status === 'unread')
|
|
696
|
+
return (
|
|
697
|
+
<span role="img" aria-label="Unread" className="size-2 shrink-0 rounded-full bg-accent" />
|
|
698
|
+
);
|
|
699
|
+
if (status === 'woke')
|
|
700
|
+
return (
|
|
701
|
+
<span
|
|
702
|
+
role="img"
|
|
703
|
+
aria-label="Woke from a snooze"
|
|
704
|
+
className="inline-flex shrink-0 items-center gap-1 text-xs font-medium text-accent"
|
|
705
|
+
>
|
|
706
|
+
<Icon name="alarm-clock" size={12} />
|
|
707
|
+
Woke
|
|
708
|
+
</span>
|
|
709
|
+
);
|
|
710
|
+
return null;
|
|
711
|
+
};
|
|
712
|
+
|
|
713
|
+
/** A row's hover action: settle, wake in Snoozed, un-settle in Settled, restore in Archived. */
|
|
714
|
+
const quickAction = (
|
|
715
|
+
each: Session,
|
|
716
|
+
shelf: Shelf,
|
|
717
|
+
actions: React.RefObject<RowActions | undefined>,
|
|
718
|
+
) => {
|
|
719
|
+
const [action, icon, label]: [Action, string, string] =
|
|
720
|
+
shelf === 'archived'
|
|
721
|
+
? ['restore', 'archive-restore', 'Restore']
|
|
722
|
+
: shelf === 'snoozed'
|
|
723
|
+
? ['wake', 'alarm-clock-off', 'Wake now']
|
|
724
|
+
: shelf === 'settled'
|
|
725
|
+
? ['unsettle', 'undo', 'Un-settle']
|
|
726
|
+
: ['settle', 'check', 'Settle'];
|
|
727
|
+
return (
|
|
728
|
+
<Tooltip>
|
|
729
|
+
<TooltipTrigger
|
|
730
|
+
render={
|
|
731
|
+
<Button
|
|
732
|
+
size="icon-sm"
|
|
733
|
+
aria-label={label}
|
|
734
|
+
disabled={action === 'settle' && each.running}
|
|
735
|
+
onClick={(event) => {
|
|
736
|
+
event.stopPropagation();
|
|
737
|
+
void actions.current!.act(each, action);
|
|
738
|
+
}}
|
|
739
|
+
/>
|
|
740
|
+
}
|
|
741
|
+
>
|
|
742
|
+
<Icon name={icon} size={14} />
|
|
743
|
+
</TooltipTrigger>
|
|
744
|
+
<TooltipContent>{label}</TooltipContent>
|
|
745
|
+
</Tooltip>
|
|
746
|
+
);
|
|
747
|
+
};
|
|
748
|
+
/** A child's row under its parent (bridge#212 S3): the group's accent bar on its left (one a level, joined from the
|
|
749
|
+
* first child to the last); its agent and, at the line's end, its state in words; its title; its worktree (or what it
|
|
750
|
+
* does: "Preparing its branch and stage") and its elapsed time while it works or asks, else when it ended. A finished child
|
|
751
|
+
* reads muted. */
|
|
752
|
+
function ChildRowBody({
|
|
753
|
+
title,
|
|
754
|
+
marker,
|
|
755
|
+
depth,
|
|
756
|
+
state,
|
|
757
|
+
since,
|
|
758
|
+
ended,
|
|
759
|
+
detail,
|
|
760
|
+
agent,
|
|
761
|
+
modelLabel,
|
|
762
|
+
provider,
|
|
763
|
+
worktreeName,
|
|
764
|
+
hover,
|
|
765
|
+
}: {
|
|
766
|
+
title: string;
|
|
767
|
+
/** Beside its agent: what it is when not a child session (a sub-agent). */
|
|
768
|
+
marker?: React.ReactNode;
|
|
769
|
+
depth: number;
|
|
770
|
+
state: ChildState;
|
|
771
|
+
since: string | undefined;
|
|
772
|
+
ended: string | undefined;
|
|
773
|
+
detail: string | undefined;
|
|
774
|
+
agent: string;
|
|
775
|
+
modelLabel: string;
|
|
776
|
+
provider: ReturnType<typeof providerOf>;
|
|
777
|
+
worktreeName: string | undefined;
|
|
778
|
+
hover: React.ReactNode;
|
|
779
|
+
}) {
|
|
780
|
+
const finished =
|
|
781
|
+
state === 'done' || state === 'failed' || state === 'stopped' || state === 'cancelled';
|
|
782
|
+
return (
|
|
783
|
+
<>
|
|
784
|
+
{Array.from({ length: depth }, (_, level) => (
|
|
785
|
+
<span
|
|
786
|
+
key={level}
|
|
787
|
+
aria-hidden
|
|
788
|
+
data-slot="child-bar"
|
|
789
|
+
className="absolute top-0 -bottom-px w-0.5 bg-accent"
|
|
790
|
+
style={{ left: 14 + level * 16 }}
|
|
791
|
+
/>
|
|
792
|
+
))}
|
|
793
|
+
<div data-finished={finished || undefined} className="min-w-0 flex-1">
|
|
794
|
+
<div className="flex h-4 items-center gap-1.5 text-xs text-muted">
|
|
795
|
+
<span
|
|
796
|
+
title={modelLabel}
|
|
797
|
+
className="inline-flex size-3.5 shrink-0 items-center justify-center"
|
|
798
|
+
>
|
|
799
|
+
<ProviderMark provider={provider} size={12} />
|
|
800
|
+
</span>
|
|
801
|
+
<span data-who="agent" className={cn('min-w-0 truncate', !marker && 'flex-1')}>
|
|
802
|
+
{agent}
|
|
803
|
+
</span>
|
|
804
|
+
{marker}
|
|
805
|
+
{marker && <span className="flex-1" />}
|
|
806
|
+
<ChildStateMark state={state} />
|
|
807
|
+
</div>
|
|
808
|
+
<div className="mt-0.5 flex h-5 items-center gap-2">
|
|
809
|
+
<span
|
|
810
|
+
className={cn(
|
|
811
|
+
'min-w-0 flex-1 truncate text-sm font-semibold',
|
|
812
|
+
finished ? 'text-muted' : 'text-foreground',
|
|
813
|
+
)}
|
|
814
|
+
>
|
|
815
|
+
{title}
|
|
816
|
+
</span>
|
|
817
|
+
{hover}
|
|
818
|
+
</div>
|
|
819
|
+
<div className="mt-0.5 flex h-4 items-center gap-2 text-xs text-muted">
|
|
820
|
+
<span className="inline-flex min-w-0 flex-1 items-center gap-1">
|
|
821
|
+
{detail !== undefined && (
|
|
822
|
+
<span data-who="detail" className="truncate">
|
|
823
|
+
{detail}
|
|
824
|
+
</span>
|
|
825
|
+
)}
|
|
826
|
+
{detail === undefined && worktreeName !== undefined && (
|
|
827
|
+
<>
|
|
828
|
+
<Icon name="git-branch" size={12} />
|
|
829
|
+
<span
|
|
830
|
+
data-who="worktree"
|
|
831
|
+
className="truncate"
|
|
832
|
+
title={`Works in the stage ${worktreeName}`}
|
|
833
|
+
>
|
|
834
|
+
{worktreeName}
|
|
835
|
+
</span>
|
|
836
|
+
</>
|
|
837
|
+
)}
|
|
838
|
+
</span>
|
|
839
|
+
<span data-who="elapsed" className="shrink-0 tabular-nums">
|
|
840
|
+
{finished ? (
|
|
841
|
+
ended !== undefined && <span title={`Ended ${when(ended)}`}>{ago(ended)}</span>
|
|
842
|
+
) : (
|
|
843
|
+
<Elapsed since={since} />
|
|
844
|
+
)}
|
|
845
|
+
</span>
|
|
846
|
+
</div>
|
|
847
|
+
</div>
|
|
848
|
+
</>
|
|
849
|
+
);
|
|
850
|
+
}
|
|
851
|
+
export const SessionRow = React.memo(function SessionRow({
|
|
852
|
+
each,
|
|
853
|
+
shelf,
|
|
854
|
+
status,
|
|
855
|
+
faded,
|
|
856
|
+
stamp,
|
|
857
|
+
worktreeName,
|
|
858
|
+
modelLabel,
|
|
859
|
+
provider,
|
|
860
|
+
nodeTitle,
|
|
861
|
+
tagged,
|
|
862
|
+
actions,
|
|
863
|
+
depth = 0,
|
|
864
|
+
kids = 0,
|
|
865
|
+
kidsOpen = false,
|
|
866
|
+
agents = 0,
|
|
867
|
+
childState,
|
|
868
|
+
childSince,
|
|
869
|
+
childEnded,
|
|
870
|
+
childDetail,
|
|
871
|
+
agent,
|
|
872
|
+
}: RowProps) {
|
|
873
|
+
const menu =
|
|
874
|
+
shelf === 'archived' ? (
|
|
875
|
+
<DropdownMenuItem onSelect={() => void actions.current!.act(each, 'restore')}>
|
|
876
|
+
<Icon name="archive-restore" size={14} />
|
|
877
|
+
Restore
|
|
878
|
+
</DropdownMenuItem>
|
|
879
|
+
) : (
|
|
880
|
+
<RowMenu each={each} actions={actions} />
|
|
881
|
+
);
|
|
882
|
+
/** Its quick action and menu, shown while hovered or while its menu is open (not its children's toggle). */
|
|
883
|
+
const hover = (
|
|
884
|
+
<span className="-mr-1 hidden shrink-0 items-center group-hover:flex group-has-[[aria-expanded=true]:not([data-kids])]:flex">
|
|
885
|
+
{quickAction(each, shelf, actions)}
|
|
886
|
+
<DropdownMenu>
|
|
887
|
+
<DropdownMenuTrigger
|
|
888
|
+
render={
|
|
889
|
+
<Button
|
|
890
|
+
size="icon-sm"
|
|
891
|
+
aria-label={`Actions for ${each.title}`}
|
|
892
|
+
onClick={(event) => event.stopPropagation()}
|
|
893
|
+
/>
|
|
894
|
+
}
|
|
895
|
+
>
|
|
896
|
+
<Icon name="ellipsis" size={14} />
|
|
897
|
+
</DropdownMenuTrigger>
|
|
898
|
+
<DropdownMenuContent align="end">{menu}</DropdownMenuContent>
|
|
899
|
+
</DropdownMenu>
|
|
900
|
+
</span>
|
|
901
|
+
);
|
|
902
|
+
return (
|
|
903
|
+
<ContextMenu>
|
|
904
|
+
<ContextMenuTrigger render={<div data-row={each.id} />}>
|
|
905
|
+
<ListItem
|
|
906
|
+
value={shelf === 'archived' ? undefined : each.id}
|
|
907
|
+
size="auto"
|
|
908
|
+
textValue={each.title}
|
|
909
|
+
data-session={each.id}
|
|
910
|
+
data-status={status}
|
|
911
|
+
data-depth={depth || undefined}
|
|
912
|
+
data-parent={depth ? each.parent : undefined}
|
|
913
|
+
className={cn('group px-3', depth ? 'relative py-2' : 'py-2.5')}
|
|
914
|
+
// A child under its parent: indented a step a level (bridge#212).
|
|
915
|
+
{...(depth > 0 && { style: { paddingLeft: 12 + depth * 16 } })}
|
|
916
|
+
>
|
|
917
|
+
{depth > 0 ? (
|
|
918
|
+
<ChildRowBody
|
|
919
|
+
title={each.title}
|
|
920
|
+
depth={depth}
|
|
921
|
+
state={childState ?? 'working'}
|
|
922
|
+
since={childSince}
|
|
923
|
+
ended={childEnded}
|
|
924
|
+
detail={childDetail}
|
|
925
|
+
agent={agent ?? each.harness}
|
|
926
|
+
modelLabel={modelLabel}
|
|
927
|
+
provider={provider}
|
|
928
|
+
worktreeName={worktreeName}
|
|
929
|
+
hover={hover}
|
|
930
|
+
/>
|
|
931
|
+
) : (
|
|
932
|
+
<div
|
|
933
|
+
className={cn(
|
|
934
|
+
'min-w-0 flex-1 transition-opacity',
|
|
935
|
+
faded && 'opacity-60 group-hover:opacity-100',
|
|
936
|
+
)}
|
|
937
|
+
>
|
|
938
|
+
<div className="flex h-5 items-center gap-2">
|
|
939
|
+
<span className="min-w-0 flex-1 truncate text-sm font-semibold text-foreground">
|
|
940
|
+
{each.title}
|
|
941
|
+
</span>
|
|
942
|
+
{/* Where it works, in the workspace's own list (research-083): a stage, by the branches it runs. */}
|
|
943
|
+
{worktreeName !== undefined && (
|
|
944
|
+
<span
|
|
945
|
+
title={`Works in the stage ${worktreeName}`}
|
|
946
|
+
className="inline-flex max-w-32 shrink-0 items-center gap-1 text-xs text-muted"
|
|
947
|
+
>
|
|
948
|
+
<Icon name="git-branch" size={12} />
|
|
949
|
+
<span className="truncate">{worktreeName}</span>
|
|
950
|
+
</span>
|
|
951
|
+
)}
|
|
952
|
+
{statusMark(status)}
|
|
953
|
+
{each.snoozedUntil ? (
|
|
954
|
+
<span
|
|
955
|
+
title={`Wakes ${when(each.snoozedUntil)}`}
|
|
956
|
+
className="inline-flex shrink-0 items-center gap-1 text-xs text-muted group-hover:hidden group-has-[[aria-expanded=true]:not([data-kids])]:hidden"
|
|
957
|
+
>
|
|
958
|
+
<Icon name="alarm-clock" size={12} />
|
|
959
|
+
{stamp}
|
|
960
|
+
</span>
|
|
961
|
+
) : (
|
|
962
|
+
<span className="shrink-0 text-xs text-muted group-hover:hidden group-has-[[aria-expanded=true]:not([data-kids])]:hidden">
|
|
963
|
+
{stamp}
|
|
964
|
+
</span>
|
|
965
|
+
)}
|
|
966
|
+
{hover}
|
|
967
|
+
</div>
|
|
968
|
+
{depth === 0 && (
|
|
969
|
+
<p className="mt-0.5 truncate text-xs text-secondary">
|
|
970
|
+
{each.preview ?? 'No messages yet'}
|
|
971
|
+
</p>
|
|
972
|
+
)}
|
|
973
|
+
<div className={cn('flex flex-wrap items-center gap-1', depth ? 'mt-0.5' : 'mt-1.5')}>
|
|
974
|
+
<Tooltip>
|
|
975
|
+
<TooltipTrigger
|
|
976
|
+
render={
|
|
977
|
+
<span
|
|
978
|
+
aria-label={modelLabel}
|
|
979
|
+
className="inline-flex size-4 shrink-0 items-center justify-center text-foreground"
|
|
980
|
+
/>
|
|
981
|
+
}
|
|
982
|
+
>
|
|
983
|
+
<ProviderMark provider={provider} size={12} />
|
|
984
|
+
</TooltipTrigger>
|
|
985
|
+
<TooltipContent>{modelLabel}</TooltipContent>
|
|
986
|
+
</Tooltip>
|
|
987
|
+
{nodeTitle !== undefined && (
|
|
988
|
+
<Badge className="min-w-0 justify-start gap-1 truncate">
|
|
989
|
+
<Icon name="folder" size={12} />
|
|
990
|
+
{nodeTitle}
|
|
991
|
+
</Badge>
|
|
992
|
+
)}
|
|
993
|
+
{each.tags?.map((tag) => (
|
|
994
|
+
<button
|
|
995
|
+
key={tag}
|
|
996
|
+
type="button"
|
|
997
|
+
data-tag={tag}
|
|
998
|
+
aria-pressed={tagged === tag}
|
|
999
|
+
title={
|
|
1000
|
+
tagged === tag ? 'Show every session' : `Show the sessions tagged ${tag}`
|
|
1001
|
+
}
|
|
1002
|
+
className={cn(
|
|
1003
|
+
'cursor-pointer rounded-full border-0 px-1.5 text-[11px] leading-4 text-secondary hover:bg-border/60',
|
|
1004
|
+
tagged === tag ? 'bg-accent/15 text-accent' : 'bg-border/40',
|
|
1005
|
+
)}
|
|
1006
|
+
onClick={(event) => {
|
|
1007
|
+
event.stopPropagation();
|
|
1008
|
+
actions.current!.setTagged((was) => (was === tag ? undefined : tag));
|
|
1009
|
+
}}
|
|
1010
|
+
onPointerDown={(event) => event.stopPropagation()}
|
|
1011
|
+
>
|
|
1012
|
+
#{tag}
|
|
1013
|
+
</button>
|
|
1014
|
+
))}
|
|
1015
|
+
{/* Its children (bridge#212): shown or hidden under it, the toggle in one place, the line's end. */}
|
|
1016
|
+
{kids + agents > 0 && (
|
|
1017
|
+
<Button
|
|
1018
|
+
size="sm"
|
|
1019
|
+
// Not the row's menu: its children shown keep the row's stamp, its actions hidden (`:not([data-kids])`).
|
|
1020
|
+
data-kids
|
|
1021
|
+
aria-expanded={kidsOpen}
|
|
1022
|
+
aria-label={`${kidsOpen ? 'Hide' : 'Show'} ${[
|
|
1023
|
+
kids > 0 && `${kids} child session${kids === 1 ? '' : 's'}`,
|
|
1024
|
+
agents > 0 && `${agents} sub-agent${agents === 1 ? '' : 's'}`,
|
|
1025
|
+
]
|
|
1026
|
+
.filter(Boolean)
|
|
1027
|
+
.join(' and ')} of ${each.title}`}
|
|
1028
|
+
className="-my-1 ml-auto h-6 gap-1 px-1.5 text-xs text-muted"
|
|
1029
|
+
onClick={(event) => {
|
|
1030
|
+
event.stopPropagation();
|
|
1031
|
+
actions.current!.toggleChildren?.(each.id);
|
|
1032
|
+
}}
|
|
1033
|
+
onPointerDown={(event) => event.stopPropagation()}
|
|
1034
|
+
>
|
|
1035
|
+
<Icon
|
|
1036
|
+
name="chevron-right"
|
|
1037
|
+
size={12}
|
|
1038
|
+
className={cn('transition-transform', kidsOpen && 'rotate-90')}
|
|
1039
|
+
/>
|
|
1040
|
+
{[
|
|
1041
|
+
kids > 0 && `${kids} child${kids === 1 ? '' : 'ren'}`,
|
|
1042
|
+
agents > 0 && `${agents} sub-agent${agents === 1 ? '' : 's'}`,
|
|
1043
|
+
]
|
|
1044
|
+
.filter(Boolean)
|
|
1045
|
+
.join(' · ')}
|
|
1046
|
+
</Button>
|
|
1047
|
+
)}
|
|
1048
|
+
</div>
|
|
1049
|
+
</div>
|
|
1050
|
+
)}
|
|
1051
|
+
</ListItem>
|
|
1052
|
+
</ContextMenuTrigger>
|
|
1053
|
+
<DropdownMenuContent>{menu}</DropdownMenuContent>
|
|
1054
|
+
</ContextMenu>
|
|
1055
|
+
);
|
|
1056
|
+
});
|
|
1057
|
+
|
|
1058
|
+
/** A sub-agent's row under its session's (bridge#212 S8): a child's layout (its bar, its agent with the "Sub-agent"
|
|
1059
|
+
* marker and its state in words, its title, its elapsed or end time), opening its view; no menu (it is no session). */
|
|
1060
|
+
const SubagentRow = React.memo(function SubagentRow({
|
|
1061
|
+
session,
|
|
1062
|
+
sub,
|
|
1063
|
+
depth,
|
|
1064
|
+
sessionRunning,
|
|
1065
|
+
modelLabel,
|
|
1066
|
+
provider,
|
|
1067
|
+
}: {
|
|
1068
|
+
session: string;
|
|
1069
|
+
sub: SubagentEntry;
|
|
1070
|
+
depth: number;
|
|
1071
|
+
sessionRunning: boolean;
|
|
1072
|
+
modelLabel: string;
|
|
1073
|
+
provider: ReturnType<typeof providerOf>;
|
|
1074
|
+
}) {
|
|
1075
|
+
return (
|
|
1076
|
+
<ListItem
|
|
1077
|
+
value={subagentItem(session, sub.id)}
|
|
1078
|
+
size="auto"
|
|
1079
|
+
textValue={sub.title}
|
|
1080
|
+
data-subagent={sub.id}
|
|
1081
|
+
data-parent={session}
|
|
1082
|
+
data-depth={depth}
|
|
1083
|
+
className="group relative px-3 py-2"
|
|
1084
|
+
style={{ paddingLeft: 12 + depth * 16 }}
|
|
1085
|
+
>
|
|
1086
|
+
<ChildRowBody
|
|
1087
|
+
title={sub.title}
|
|
1088
|
+
marker={<SubagentMarker />}
|
|
1089
|
+
depth={depth}
|
|
1090
|
+
state={subagentState(sub.status, sessionRunning)}
|
|
1091
|
+
since={sub.startedAt}
|
|
1092
|
+
ended={sub.endedAt}
|
|
1093
|
+
detail={undefined}
|
|
1094
|
+
agent={sub.agent ?? 'Agent'}
|
|
1095
|
+
modelLabel={modelLabel}
|
|
1096
|
+
provider={provider}
|
|
1097
|
+
worktreeName={undefined}
|
|
1098
|
+
hover={null}
|
|
1099
|
+
/>
|
|
1100
|
+
</ListItem>
|
|
1101
|
+
);
|
|
1102
|
+
});
|
|
1103
|
+
|
|
1104
|
+
function Sessions({
|
|
1105
|
+
ctx,
|
|
1106
|
+
node,
|
|
1107
|
+
screen,
|
|
1108
|
+
item,
|
|
1109
|
+
focused,
|
|
1110
|
+
renderSlot,
|
|
1111
|
+
}: {
|
|
1112
|
+
ctx: Context;
|
|
1113
|
+
node: string;
|
|
1114
|
+
screen: string;
|
|
1115
|
+
item: string | undefined;
|
|
1116
|
+
focused: boolean | undefined;
|
|
1117
|
+
renderSlot: RenderTabs;
|
|
1118
|
+
}) {
|
|
1119
|
+
const { workspace } = ctx;
|
|
1120
|
+
const api = `/${workspace.slug}/api/sessions/sessions`;
|
|
1121
|
+
const worktree = shownWorktree(workspace);
|
|
1122
|
+
const worktreeNames = useWorktreeNames(workspace);
|
|
1123
|
+
// In a stage, its branches (research-083): the one it chose is the new session's by default; the project's
|
|
1124
|
+
// "New session's branch" setting may make it a new one.
|
|
1125
|
+
const startNew = React.useSyncExternalStore(
|
|
1126
|
+
ctx.nodes.subscribe,
|
|
1127
|
+
() => ctx.nodes.settings('sessions', node)?.values.newSessionBranch?.value === 'new',
|
|
1128
|
+
);
|
|
1129
|
+
const branchesAt = shownWorktree(workspace) ?? workspace.slug;
|
|
1130
|
+
const branchOf = useBranches(branchesAt, node, startNew);
|
|
1131
|
+
const headers = { 'content-type': 'application/json', 'x-bridge-node': node };
|
|
1132
|
+
const { store, listing, known } = useListing(ctx, node);
|
|
1133
|
+
useItemTitles(
|
|
1134
|
+
ctx,
|
|
1135
|
+
listing?.sessions,
|
|
1136
|
+
branchOf.branches && branchOf.branches.length > 1
|
|
1137
|
+
? branchOf.branches.find((each) => each.main)?.name
|
|
1138
|
+
: undefined,
|
|
1139
|
+
);
|
|
1140
|
+
/** As of the last render, for calls that end later (a creation, a triage, a stream's event). */
|
|
1141
|
+
const latest = React.useRef({ store, item, focused });
|
|
1142
|
+
latest.current = { store, item, focused };
|
|
1143
|
+
// The session last opened on this node, shown in a window without an item (none after "New chat").
|
|
1144
|
+
const lastKey = `bridge.sessions.last.${JSON.stringify([workspace.slug, node])}`;
|
|
1145
|
+
const readLast = () => {
|
|
1146
|
+
try {
|
|
1147
|
+
return localStorage.getItem(lastKey) ?? undefined;
|
|
1148
|
+
} catch {
|
|
1149
|
+
return undefined;
|
|
1150
|
+
}
|
|
1151
|
+
};
|
|
1152
|
+
/** Without an item: the last session opened here, read when the item goes. */
|
|
1153
|
+
const [fallback, setFallback] = React.useState<string | undefined>(() =>
|
|
1154
|
+
item === undefined ? readLast() : undefined,
|
|
1155
|
+
);
|
|
1156
|
+
React.useEffect(() => {
|
|
1157
|
+
if (item === undefined) setFallback(readLast());
|
|
1158
|
+
}, [item, lastKey]);
|
|
1159
|
+
/** A session chosen while this window was not the focused one (a creation or a triage ending after the focus moved):
|
|
1160
|
+
* shown here, over the item it was chosen on, until the item changes; reported once the window is focused again. */
|
|
1161
|
+
const [override, setOverride] = React.useState<{
|
|
1162
|
+
over: string | undefined;
|
|
1163
|
+
id: string | undefined;
|
|
1164
|
+
}>();
|
|
1165
|
+
// Another item: the choice made apart is dropped.
|
|
1166
|
+
React.useEffect(() => setOverride(undefined), [item]);
|
|
1167
|
+
// A sub-agent's item names its session and it (bridge#212 S8): the session is the one shown, its view the sub-agent's.
|
|
1168
|
+
const { session: asked, subagent: shownSub } = itemParts(
|
|
1169
|
+
override && override.over === item ? override.id : (item ?? fallback),
|
|
1170
|
+
);
|
|
1171
|
+
// A session that is gone (archived, another node's) shows a new chat.
|
|
1172
|
+
const current =
|
|
1173
|
+
asked && listing && !listing.sessions.some((each) => each.id === asked) ? undefined : asked;
|
|
1174
|
+
React.useEffect(() => {
|
|
1175
|
+
try {
|
|
1176
|
+
if (current) localStorage.setItem(lastKey, current);
|
|
1177
|
+
} catch {
|
|
1178
|
+
/* kept for this view only */
|
|
1179
|
+
}
|
|
1180
|
+
}, [lastKey, current]);
|
|
1181
|
+
/** Shows session `id` (none: a new chat) in this window: its item, through the shell, when this window is the focused
|
|
1182
|
+
* one (a choice in it focused it); else here only. A new chat forgets the last session, so a window without an item
|
|
1183
|
+
* shows a new chat too. */
|
|
1184
|
+
const choose = (id: string | undefined) => {
|
|
1185
|
+
if (id === undefined) {
|
|
1186
|
+
try {
|
|
1187
|
+
localStorage.removeItem(lastKey);
|
|
1188
|
+
} catch {
|
|
1189
|
+
/* kept for this view only */
|
|
1190
|
+
}
|
|
1191
|
+
setFallback(undefined);
|
|
1192
|
+
}
|
|
1193
|
+
const { item, focused } = latest.current;
|
|
1194
|
+
if (focused === false) return setOverride({ over: item, id });
|
|
1195
|
+
setOverride(undefined);
|
|
1196
|
+
ctx.screen.open(id);
|
|
1197
|
+
};
|
|
1198
|
+
// Without an item, the session shown is named as this window's item, in place (no history entry: Back leaves the
|
|
1199
|
+
// screen, it does not come back here to name it again), once, while this window is the focused one.
|
|
1200
|
+
// Focused again: a choice made apart becomes this window's item (the URL, the sidebar and P8 follow).
|
|
1201
|
+
React.useEffect(() => {
|
|
1202
|
+
if (focused === false || !override || override.over !== item) return;
|
|
1203
|
+
setOverride(undefined);
|
|
1204
|
+
ctx.screen.open(override.id);
|
|
1205
|
+
}, [focused]);
|
|
1206
|
+
const named = React.useRef(false);
|
|
1207
|
+
React.useEffect(() => {
|
|
1208
|
+
if (item !== undefined) {
|
|
1209
|
+
named.current = false;
|
|
1210
|
+
return;
|
|
1211
|
+
}
|
|
1212
|
+
if (
|
|
1213
|
+
named.current ||
|
|
1214
|
+
focused === false ||
|
|
1215
|
+
!fallback ||
|
|
1216
|
+
!listing?.sessions.some((each) => each.id === fallback)
|
|
1217
|
+
)
|
|
1218
|
+
return;
|
|
1219
|
+
const state = ctx.shell.get();
|
|
1220
|
+
if (
|
|
1221
|
+
state.screen?.plugin !== name ||
|
|
1222
|
+
state.screen.id !== screen ||
|
|
1223
|
+
state.item !== undefined ||
|
|
1224
|
+
state.utility ||
|
|
1225
|
+
state.node !== node
|
|
1226
|
+
)
|
|
1227
|
+
return;
|
|
1228
|
+
named.current = true;
|
|
1229
|
+
// `show`, not `ctx.screen.open`: it names the current window's item in place, without a history entry.
|
|
1230
|
+
ctx.shell.show({ node, screen: { plugin: name, id: screen }, item: fallback });
|
|
1231
|
+
}, [item, fallback, focused, listing]);
|
|
1232
|
+
const [events, setEvents] = React.useState<Stored[]>([]);
|
|
1233
|
+
const [live, setLive] = React.useState<Live | null>(null);
|
|
1234
|
+
/** Older lines than `events` remain on the server (the history comes in pages, the newest first). */
|
|
1235
|
+
const [more, setMore] = React.useState(false);
|
|
1236
|
+
/** An earlier page being read, for session `id`; `height` and `top`: the viewport's before it lands, to keep the
|
|
1237
|
+
* reader's place; `failed`: its read failed, tried again only when the reader scrolls. */
|
|
1238
|
+
const earlier = React.useRef<Earlier>(undefined);
|
|
1239
|
+
/** What the session list's rows call (`SessionRow`), the page's latest. */
|
|
1240
|
+
const rowActions = React.useRef<RowActions>(undefined);
|
|
1241
|
+
/** The watch's snapshot arrived on the current connection. */
|
|
1242
|
+
const [synced, setSynced] = React.useState(false);
|
|
1243
|
+
const [error, setError] = React.useState('');
|
|
1244
|
+
/** Decisions whose answer is in flight, by session and the `seq` of their `decision/asked` (`${session}:${seq}`: a
|
|
1245
|
+
* decision with no row has a seq of its own session's, -1 in each). */
|
|
1246
|
+
const [answering, setAnswering] = React.useState<Record<string, boolean>>({});
|
|
1247
|
+
/** Whether the open decisions drawn on a timeline row are in view, by `seq`: the dock points to one out of view. */
|
|
1248
|
+
const [onRows, setOnRows] = React.useState<Record<number, boolean>>({});
|
|
1249
|
+
const onDecisionShown = React.useCallback(
|
|
1250
|
+
(seq: number, shown: boolean | undefined) =>
|
|
1251
|
+
setOnRows((was) => {
|
|
1252
|
+
if (was[seq] === shown) return was;
|
|
1253
|
+
const { [seq]: _gone, ...rest } = was;
|
|
1254
|
+
return shown === undefined ? rest : { ...rest, [seq]: shown };
|
|
1255
|
+
}),
|
|
1256
|
+
[],
|
|
1257
|
+
);
|
|
1258
|
+
/** The file a tool call's change counts opened the details panel on (`at`: each click scrolls to it again). */
|
|
1259
|
+
const [changeAsked, setChangeAsked] = React.useState<{
|
|
1260
|
+
session: string;
|
|
1261
|
+
path: string;
|
|
1262
|
+
at: number;
|
|
1263
|
+
}>();
|
|
1264
|
+
const viewport = React.useRef<HTMLDivElement>(null);
|
|
1265
|
+
/** The conversation was scrolled to (near) its bottom: new content keeps it there. */
|
|
1266
|
+
const stick = React.useRef(true);
|
|
1267
|
+
/** The reader scrolled up, away from the end: "Scroll to end" shows. */
|
|
1268
|
+
const [away, setAway] = React.useState(false);
|
|
1269
|
+
/** The height of the prompt docked over the conversation's bottom: the conversation ends that far above it. */
|
|
1270
|
+
const [dockHeight, setDockHeight] = React.useState(0);
|
|
1271
|
+
/** The docked prompt, watched from its mounting (a page opened on a session draws it only once the list loaded):
|
|
1272
|
+
* it grows and shrinks (a plan, a queue, a decision, lines typed), and the conversation's end follows it. */
|
|
1273
|
+
const dock = React.useCallback((element: HTMLDivElement | null) => {
|
|
1274
|
+
if (!element) return;
|
|
1275
|
+
const observer = new ResizeObserver(() => setDockHeight(element.offsetHeight));
|
|
1276
|
+
observer.observe(element);
|
|
1277
|
+
return () => observer.disconnect();
|
|
1278
|
+
}, []);
|
|
1279
|
+
/** When Escape was last pressed during a turn: a second one soon after stops it. */
|
|
1280
|
+
const escaped = React.useRef(0);
|
|
1281
|
+
/** The prompt this node's "Prompt" setting names (none: the first registered), followed live. */
|
|
1282
|
+
const promptChoice = React.useSyncExternalStore(ctx.nodes.subscribe, () => {
|
|
1283
|
+
const value = ctx.nodes.settings('sessions', node)?.values.prompt?.value;
|
|
1284
|
+
return typeof value === 'string' ? value : undefined;
|
|
1285
|
+
});
|
|
1286
|
+
/** The choices Snooze offers: this node's "Snooze choices" setting, followed live (read as text: a stable snapshot). */
|
|
1287
|
+
const snoozeSetting = React.useSyncExternalStore(ctx.nodes.subscribe, () =>
|
|
1288
|
+
JSON.stringify(ctx.nodes.settings('sessions', node)?.values.snoozeChoices?.value ?? null),
|
|
1289
|
+
);
|
|
1290
|
+
const snoozeChoices = React.useMemo(
|
|
1291
|
+
() => snoozeChoicesOf(JSON.parse(snoozeSetting) as unknown),
|
|
1292
|
+
[snoozeSetting],
|
|
1293
|
+
);
|
|
1294
|
+
/** The handle the page drives the prompt through (a basic prompt has none). */
|
|
1295
|
+
const promptHandle = React.useRef<{
|
|
1296
|
+
setText(text: string, images?: Attached[]): void;
|
|
1297
|
+
addText(session: string, text: string, images?: Attached[]): void;
|
|
1298
|
+
focus(): void;
|
|
1299
|
+
openModels(): void;
|
|
1300
|
+
}>(null);
|
|
1301
|
+
const { toast, dismiss } = useToast();
|
|
1302
|
+
/** Whether working sessions fold into Working (bridge#210): this node's "Fold working sessions" setting, live. */
|
|
1303
|
+
const folding = React.useSyncExternalStore(
|
|
1304
|
+
ctx.nodes.subscribe,
|
|
1305
|
+
() => ctx.nodes.settings('sessions', node)?.values.inbox?.value !== false,
|
|
1306
|
+
);
|
|
1307
|
+
/** Which shelves are open on this node (Settled and Working closed until opened), kept like the last session. */
|
|
1308
|
+
const shelvesKey = `bridge.sessions.shelves.${JSON.stringify([workspace.slug, node])}`;
|
|
1309
|
+
const [shelves, setShelves] = React.useState<Partial<Record<Shelf, boolean>>>(() => {
|
|
1310
|
+
try {
|
|
1311
|
+
const stored: unknown = JSON.parse(localStorage.getItem(shelvesKey) ?? '{}');
|
|
1312
|
+
return shelvesOf(stored);
|
|
1313
|
+
} catch {
|
|
1314
|
+
return {};
|
|
1315
|
+
}
|
|
1316
|
+
});
|
|
1317
|
+
const isOpen = (shelf: Shelf) => shelves[shelf] ?? (shelf !== 'settled' && shelf !== 'working');
|
|
1318
|
+
const setOpen = (shelf: Shelf, open: boolean) =>
|
|
1319
|
+
setShelves((was) => {
|
|
1320
|
+
const next = { ...was, [shelf]: open };
|
|
1321
|
+
try {
|
|
1322
|
+
localStorage.setItem(shelvesKey, JSON.stringify(next));
|
|
1323
|
+
} catch {
|
|
1324
|
+
/* kept for this view only */
|
|
1325
|
+
}
|
|
1326
|
+
return next;
|
|
1327
|
+
});
|
|
1328
|
+
/** Every settled row shown, past the first `SETTLED_SHOWN`. */
|
|
1329
|
+
const [allSettled, setAllSettled] = React.useState(false);
|
|
1330
|
+
/** The parents whose children the user showed (true) or hid (false) under their row (bridge#212). */
|
|
1331
|
+
const [shownChildren, setShownChildren] = React.useState<Record<string, boolean>>({});
|
|
1332
|
+
/** The folder cleanup an archive offers (research-083). */
|
|
1333
|
+
const [cleanup, setCleanup] = React.useState<Cleanup>();
|
|
1334
|
+
/** The archived sessions, listed while "Show archived" is on (`undefined`: off). */
|
|
1335
|
+
const [archived, setArchived] = React.useState<Session[]>();
|
|
1336
|
+
/** The header's title being edited (`undefined`: the session's title shows). */
|
|
1337
|
+
const [draft, setDraft] = React.useState<string>();
|
|
1338
|
+
const titleField = React.useRef<HTMLInputElement>(null);
|
|
1339
|
+
/** A rename asked from a row's menu: the header's field takes focus once that session shows. */
|
|
1340
|
+
const renameAsked = React.useRef<string | undefined>(undefined);
|
|
1341
|
+
/** The details panel (tags, changes, commit) shows under the header, kept per node. */
|
|
1342
|
+
const detailsKey = `bridge.sessions.details.${JSON.stringify([workspace.slug, node])}`;
|
|
1343
|
+
const [details, setDetails] = React.useState(() => {
|
|
1344
|
+
try {
|
|
1345
|
+
return localStorage.getItem(detailsKey) === '1';
|
|
1346
|
+
} catch {
|
|
1347
|
+
return false;
|
|
1348
|
+
}
|
|
1349
|
+
});
|
|
1350
|
+
const toggleDetails = () =>
|
|
1351
|
+
setDetails((was) => {
|
|
1352
|
+
try {
|
|
1353
|
+
localStorage.setItem(detailsKey, was ? '0' : '1');
|
|
1354
|
+
} catch {
|
|
1355
|
+
/* kept for this view only */
|
|
1356
|
+
}
|
|
1357
|
+
return !was;
|
|
1358
|
+
});
|
|
1359
|
+
/** The sessions list beside the conversation, hidden from the conversation's header, kept like the details. */
|
|
1360
|
+
const listKey = `bridge.sessions.list.${JSON.stringify([workspace.slug, node])}`;
|
|
1361
|
+
const [listShown, setListShown] = React.useState(() => {
|
|
1362
|
+
try {
|
|
1363
|
+
return localStorage.getItem(listKey) !== '0';
|
|
1364
|
+
} catch {
|
|
1365
|
+
return true;
|
|
1366
|
+
}
|
|
1367
|
+
});
|
|
1368
|
+
/** The list's content while it slides in or out: held at the pane's open width (px), so it is clipped, not reflowed;
|
|
1369
|
+
* undefined at rest, and always with reduced motion (no slide). */
|
|
1370
|
+
const listContent = React.useRef<HTMLDivElement>(null);
|
|
1371
|
+
const [listFrozen, setListFrozen] = React.useState<number>();
|
|
1372
|
+
const toggleList = () => {
|
|
1373
|
+
// The pane's open width: its share of the group (the library's flex-grow, in %), shown or not.
|
|
1374
|
+
const pane = listContent.current?.closest<HTMLElement>('[data-panel]');
|
|
1375
|
+
const share = parseFloat(pane?.style.flexGrow ?? '');
|
|
1376
|
+
const group = pane?.parentElement?.clientWidth ?? 0;
|
|
1377
|
+
const still = window.matchMedia?.('(prefers-reduced-motion: reduce)').matches ?? false;
|
|
1378
|
+
setListFrozen(!still && share > 0 && group > 0 ? (group * share) / 100 : undefined);
|
|
1379
|
+
setListShown((was) => {
|
|
1380
|
+
try {
|
|
1381
|
+
localStorage.setItem(listKey, was ? '0' : '1');
|
|
1382
|
+
} catch {
|
|
1383
|
+
/* kept for this view only */
|
|
1384
|
+
}
|
|
1385
|
+
return !was;
|
|
1386
|
+
});
|
|
1387
|
+
};
|
|
1388
|
+
/** The list shows only the sessions with tag `tagged` when set (the workspace's tags, most used first: `known`). */
|
|
1389
|
+
const [tagged, setTagged] = React.useState<string>();
|
|
1390
|
+
/** The session whose custom snooze time is being chosen (the SnoozeDialog), and a pinned one being snoozed, asked
|
|
1391
|
+
* whether it stays pinned. */
|
|
1392
|
+
const [customFor, setCustomFor] = React.useState<Session>();
|
|
1393
|
+
const [pinAsk, setPinAsk] = React.useState<{ session: Session; until: string }>();
|
|
1394
|
+
/** The last triage that offers Undo, while it does (`mod+z`). */
|
|
1395
|
+
const undoable = React.useRef<{ undo: () => void } | null>(null);
|
|
1396
|
+
|
|
1397
|
+
const report = (cause: unknown) => {
|
|
1398
|
+
if ((cause as Error)?.name !== 'AbortError')
|
|
1399
|
+
setError(cause instanceof Error ? cause.message : String(cause));
|
|
1400
|
+
};
|
|
1401
|
+
// Stable: the views' fallback (`renderComponent`) takes it, and a new one would redraw every turn (bridge#193).
|
|
1402
|
+
const post = React.useCallback(
|
|
1403
|
+
(path: string, body: object) =>
|
|
1404
|
+
workspace
|
|
1405
|
+
.fetch(`${api}${path}`, {
|
|
1406
|
+
method: 'POST',
|
|
1407
|
+
headers: { 'content-type': 'application/json', 'x-bridge-node': node },
|
|
1408
|
+
body: JSON.stringify(body),
|
|
1409
|
+
})
|
|
1410
|
+
.then(readJson),
|
|
1411
|
+
[workspace, api, node],
|
|
1412
|
+
);
|
|
1413
|
+
/** The listing again, for every copy (the store's, which also polls while a session no copy shows runs, and wakes
|
|
1414
|
+
* the snoozed); a workspace restarting (`starting`) is tried again, not reported. */
|
|
1415
|
+
const load = (): Promise<boolean> => {
|
|
1416
|
+
const held = latest.current.store;
|
|
1417
|
+
if (!held) return Promise.resolve(false);
|
|
1418
|
+
return held.load().then(
|
|
1419
|
+
() => true,
|
|
1420
|
+
(cause) => {
|
|
1421
|
+
if ((cause as { code?: unknown })?.code !== 'starting') report(cause);
|
|
1422
|
+
return false;
|
|
1423
|
+
},
|
|
1424
|
+
);
|
|
1425
|
+
};
|
|
1426
|
+
const setListing = (change: (was: Listing) => Listing) => latest.current.store?.patch(change);
|
|
1427
|
+
const loadArchived = () =>
|
|
1428
|
+
workspace
|
|
1429
|
+
.fetch(forWorktree(`${api}?archived=1`, shownWorktree(workspace)), { headers })
|
|
1430
|
+
.then(readJson)
|
|
1431
|
+
.then((body) => setArchived((body as Listing).sessions), report);
|
|
1432
|
+
React.useEffect(() => {
|
|
1433
|
+
setArchived(undefined);
|
|
1434
|
+
}, [node]);
|
|
1435
|
+
// The session shown, for the shared listing: not polled (its own stream lists again), not counted unread.
|
|
1436
|
+
React.useEffect(() => store?.watch(current), [store, current]);
|
|
1437
|
+
|
|
1438
|
+
/** The models of each harness of the node and of the listed sessions, fetched once each (again for a failed one when
|
|
1439
|
+
* the model picker opens): the picker's lists, the names the list shows, and each harness's readiness check (an OK
|
|
1440
|
+
* answer, models or none, is ready; a failure is the driver's fix hint). */
|
|
1441
|
+
const [modelsOf, setModelsOf] = React.useState<Record<string, HarnessModels>>({});
|
|
1442
|
+
const fetchModels = (name: string) => {
|
|
1443
|
+
setModelsOf((was) => ({ ...was, [name]: {} }));
|
|
1444
|
+
void workspace
|
|
1445
|
+
.fetch(`${api}/models?profile=${encodeURIComponent(name)}`, { headers })
|
|
1446
|
+
.then(readJson)
|
|
1447
|
+
.then(
|
|
1448
|
+
(body) => setModelsOf((was) => ({ ...was, [name]: { models: body as PickerModel[] } })),
|
|
1449
|
+
(cause) =>
|
|
1450
|
+
setModelsOf((was) => ({
|
|
1451
|
+
...was,
|
|
1452
|
+
[name]: { error: cause instanceof Error ? cause.message : String(cause) },
|
|
1453
|
+
})),
|
|
1454
|
+
);
|
|
1455
|
+
};
|
|
1456
|
+
const wanted = [
|
|
1457
|
+
...new Set([
|
|
1458
|
+
...(listing?.harnesses.filter((each) => each.available).map((each) => each.name) ?? []),
|
|
1459
|
+
...(listing?.sessions.map((each) => each.harness) ?? []),
|
|
1460
|
+
]),
|
|
1461
|
+
]
|
|
1462
|
+
.filter((name) => !(name in modelsOf))
|
|
1463
|
+
.sort()
|
|
1464
|
+
.join('\n');
|
|
1465
|
+
React.useEffect(() => {
|
|
1466
|
+
for (const name of wanted ? wanted.split('\n') : []) fetchModels(name);
|
|
1467
|
+
}, [wanted, node]);
|
|
1468
|
+
React.useEffect(() => setModelsOf({}), [node]);
|
|
1469
|
+
/** Harness `name` as a provider is shown: its entry's display name, else its id (one no longer listed too). */
|
|
1470
|
+
const harnessLabel = (name: string) =>
|
|
1471
|
+
listing?.harnesses.find((each) => each.name === name)?.displayName || name;
|
|
1472
|
+
/** Session `each`'s harness (its display name) and model, by the model's name (the agent's default named when none was chosen). */
|
|
1473
|
+
const modelLabelOf = (each: Session) => {
|
|
1474
|
+
const shown = shownModel(
|
|
1475
|
+
{ harness: each.harness, model: each.model ?? '' },
|
|
1476
|
+
modelsOf[each.harness],
|
|
1477
|
+
);
|
|
1478
|
+
return `${harnessLabel(each.harness)} · ${shown ? `${shown.name}${each.model ? '' : ' (default)'}` : 'Default model'}`;
|
|
1479
|
+
};
|
|
1480
|
+
|
|
1481
|
+
// One subscription for the shown session; after an error, a new one from the last `seq` held, 1 s later, doubling to
|
|
1482
|
+
// 30 s (back to 1 s at a snapshot). It ends with the view or the workspace.
|
|
1483
|
+
React.useEffect(() => {
|
|
1484
|
+
setEvents([]);
|
|
1485
|
+
setLive(null);
|
|
1486
|
+
setMore(false);
|
|
1487
|
+
earlier.current = undefined;
|
|
1488
|
+
setSynced(false);
|
|
1489
|
+
setError('');
|
|
1490
|
+
stick.current = true;
|
|
1491
|
+
setAway(false);
|
|
1492
|
+
if (!current) return;
|
|
1493
|
+
let last = 0;
|
|
1494
|
+
let stop: (() => void) | undefined;
|
|
1495
|
+
let retry: ReturnType<typeof setTimeout> | undefined;
|
|
1496
|
+
let delay = 1000;
|
|
1497
|
+
let closed = false;
|
|
1498
|
+
/** A snapshot came already: the next one is a reconnect's. */
|
|
1499
|
+
let resumed = false;
|
|
1500
|
+
/** Ids with a stored `tool/result` in the current turn: a later live event of theirs is stale. */
|
|
1501
|
+
const resulted = new Set<string>();
|
|
1502
|
+
const add = (lines: Stored[]) => {
|
|
1503
|
+
const fresh = lines.filter((line) => line.seq > last);
|
|
1504
|
+
if (!fresh.length) return;
|
|
1505
|
+
last = fresh.at(-1)!.seq;
|
|
1506
|
+
for (const line of fresh) {
|
|
1507
|
+
if (line.type === 'turn/start') resulted.clear();
|
|
1508
|
+
if (line.type === 'tool/result' && line.id) resulted.add(line.id);
|
|
1509
|
+
}
|
|
1510
|
+
if (fresh.some((line) => listChanges.has(line.type))) void load();
|
|
1511
|
+
setEvents((was) => [...was, ...fresh]);
|
|
1512
|
+
};
|
|
1513
|
+
// Research-069: this page's id for the session's watch, and the server told when the user is on it (a pointer down
|
|
1514
|
+
// or a key once it has the focus, its browser tab shown again) or leaves it (its tab in the background): an agent's
|
|
1515
|
+
// page goes to the page last active only.
|
|
1516
|
+
const page = Array.from(crypto.getRandomValues(new Uint8Array(12)), (byte) =>
|
|
1517
|
+
byte.toString(16).padStart(2, '0'),
|
|
1518
|
+
).join('');
|
|
1519
|
+
const activity = viewerActivity((active) => post('/viewer', { id: current, page, active }));
|
|
1520
|
+
const subscribe = () => {
|
|
1521
|
+
/** The snapshot came on this connection: stored lines before it are history, not the running turn. */
|
|
1522
|
+
let isSynced = false;
|
|
1523
|
+
// On the page carrier, not persistent: on an end or a lost connection this view opens it again from `last`.
|
|
1524
|
+
let retried = false;
|
|
1525
|
+
const again = () => {
|
|
1526
|
+
stop?.();
|
|
1527
|
+
setSynced(false);
|
|
1528
|
+
if (closed || retried) return;
|
|
1529
|
+
retried = true;
|
|
1530
|
+
retry = setTimeout(subscribe, delay);
|
|
1531
|
+
delay = Math.min(delay * 2, 30_000);
|
|
1532
|
+
};
|
|
1533
|
+
// From the start, the newest page only: earlier ones load as the reader scrolls up (`loadEarlier`).
|
|
1534
|
+
const from = last ? `after=${last}` : `after=0&limit=${HISTORY_PAGE}`;
|
|
1535
|
+
stop = workspace.stream(
|
|
1536
|
+
`${api}/watch?id=${encodeURIComponent(current)}&page=${page}&${from}`,
|
|
1537
|
+
{
|
|
1538
|
+
persistent: false,
|
|
1539
|
+
onLost: again,
|
|
1540
|
+
onEnd: again,
|
|
1541
|
+
onItem: (value) => {
|
|
1542
|
+
const data = value as
|
|
1543
|
+
| { type: 'snapshot'; events: Stored[]; live: Live | null; more?: boolean }
|
|
1544
|
+
| { type: 'stored'; event: Stored }
|
|
1545
|
+
| { type: 'live'; event: LiveEvent }
|
|
1546
|
+
| { type: 'ping' }
|
|
1547
|
+
| { type: 'open-link'; path: string; turn?: string; request?: string };
|
|
1548
|
+
if (data.type === 'snapshot') {
|
|
1549
|
+
isSynced = true;
|
|
1550
|
+
activity.connected();
|
|
1551
|
+
delay = 1000;
|
|
1552
|
+
setSynced(true);
|
|
1553
|
+
if (data.more !== undefined) setMore(data.more);
|
|
1554
|
+
// A reconnect: the `queue` notices of the drop are lost (a message queued, removed or sent elsewhere).
|
|
1555
|
+
if (resumed) void load();
|
|
1556
|
+
resumed = true;
|
|
1557
|
+
add(data.events);
|
|
1558
|
+
setLive(data.live);
|
|
1559
|
+
} else if (data.type === 'stored') {
|
|
1560
|
+
if (data.event.seq <= last) return;
|
|
1561
|
+
add([data.event]);
|
|
1562
|
+
// A turn ended while the session is open: the user saw it.
|
|
1563
|
+
if (isSynced && data.event.type === 'turn/end') visit(current);
|
|
1564
|
+
if (isSynced) setLive((was) => afterStored(was, data.event));
|
|
1565
|
+
} else if (data.type === 'open-link' && isSynced) {
|
|
1566
|
+
// Research-069: the window manager decides; nothing said to the user when it shows nothing (the agent
|
|
1567
|
+
// gives the link). The agent's tool waits for this answer.
|
|
1568
|
+
const done = showLink(ctx.shell, data.path, viewport.current, data.turn);
|
|
1569
|
+
if (data.request)
|
|
1570
|
+
void post('/open-link/answer', {
|
|
1571
|
+
request: data.request,
|
|
1572
|
+
...(done.shown ? { shown: done.shown } : { reason: done.reason }),
|
|
1573
|
+
}).catch((cause: unknown) => console.warn('sessions: open-link answer', cause));
|
|
1574
|
+
} else if ((data as { type: string }).type === 'queue') {
|
|
1575
|
+
void load();
|
|
1576
|
+
} else if (
|
|
1577
|
+
data.type === 'live' &&
|
|
1578
|
+
isSynced &&
|
|
1579
|
+
!(data.event.type === 'tool' && resulted.has(data.event.id ?? ''))
|
|
1580
|
+
) {
|
|
1581
|
+
setLive((was) => afterLive(was, data.event));
|
|
1582
|
+
}
|
|
1583
|
+
},
|
|
1584
|
+
},
|
|
1585
|
+
);
|
|
1586
|
+
};
|
|
1587
|
+
const close = () => {
|
|
1588
|
+
closed = true;
|
|
1589
|
+
stop?.();
|
|
1590
|
+
clearTimeout(retry);
|
|
1591
|
+
activity.stop();
|
|
1592
|
+
};
|
|
1593
|
+
if (workspace.signal.aborted) return;
|
|
1594
|
+
workspace.signal.addEventListener('abort', close, { once: true });
|
|
1595
|
+
subscribe();
|
|
1596
|
+
return () => {
|
|
1597
|
+
workspace.signal.removeEventListener('abort', close);
|
|
1598
|
+
close();
|
|
1599
|
+
};
|
|
1600
|
+
}, [current]);
|
|
1601
|
+
|
|
1602
|
+
/** Reads the page before the oldest line held, once at a time; it lands above, keeping the reader's place. */
|
|
1603
|
+
const loadEarlier = React.useRef((_scrolled?: boolean) => {});
|
|
1604
|
+
loadEarlier.current = (scrolled = false) => {
|
|
1605
|
+
if (scrolled && earlier.current?.failed) earlier.current = undefined;
|
|
1606
|
+
const first = events[0]?.seq;
|
|
1607
|
+
if (!current || !more || !synced || earlier.current || first === undefined) return;
|
|
1608
|
+
const asked: Earlier = { id: current };
|
|
1609
|
+
earlier.current = asked;
|
|
1610
|
+
const query = `id=${encodeURIComponent(current)}&before=${first}&limit=${HISTORY_PAGE}`;
|
|
1611
|
+
void workspace
|
|
1612
|
+
.fetch(`${api}/events?${query}`, { headers })
|
|
1613
|
+
.then(readJson)
|
|
1614
|
+
.then((body) => {
|
|
1615
|
+
if (earlier.current !== asked) return;
|
|
1616
|
+
const page = body as { events: Stored[]; more: boolean };
|
|
1617
|
+
const element = viewport.current;
|
|
1618
|
+
if (element) {
|
|
1619
|
+
// The reader's place: the first whole turn in view (a partial one changes as its start arrives).
|
|
1620
|
+
const top = element.getBoundingClientRect().top;
|
|
1621
|
+
const anchor = [
|
|
1622
|
+
...element.querySelectorAll<HTMLElement>('[data-turn]:not([data-partial])'),
|
|
1623
|
+
].find((each) => each.getBoundingClientRect().bottom > top);
|
|
1624
|
+
Object.assign(asked, {
|
|
1625
|
+
anchor: anchor?.dataset.turn,
|
|
1626
|
+
offset: (anchor?.getBoundingClientRect().top ?? top) - top,
|
|
1627
|
+
height: element.scrollHeight,
|
|
1628
|
+
top: element.scrollTop,
|
|
1629
|
+
});
|
|
1630
|
+
} else earlier.current = undefined;
|
|
1631
|
+
setMore(page.more);
|
|
1632
|
+
setEvents((was) => [...page.events.filter((line) => line.seq < first), ...was]);
|
|
1633
|
+
})
|
|
1634
|
+
.catch((cause: unknown) => {
|
|
1635
|
+
if (earlier.current !== asked) return;
|
|
1636
|
+
asked.failed = true;
|
|
1637
|
+
report(cause);
|
|
1638
|
+
});
|
|
1639
|
+
};
|
|
1640
|
+
|
|
1641
|
+
// Opening a session marks it read.
|
|
1642
|
+
React.useEffect(() => {
|
|
1643
|
+
if (current) visit(current);
|
|
1644
|
+
}, [current]);
|
|
1645
|
+
// A rename asked from a row's menu, once its session shows in the header.
|
|
1646
|
+
React.useEffect(() => {
|
|
1647
|
+
if (!renameAsked.current || renameAsked.current !== current || !titleField.current) return;
|
|
1648
|
+
renameAsked.current = undefined;
|
|
1649
|
+
titleField.current.focus();
|
|
1650
|
+
titleField.current.select();
|
|
1651
|
+
});
|
|
1652
|
+
React.useEffect(() => setDraft(undefined), [current]);
|
|
1653
|
+
// In the shell's help, with the plugin's name, while a Sessions screen is shown (the copies share one declaration).
|
|
1654
|
+
React.useEffect(() => {
|
|
1655
|
+
const removals = keys().map((each) => ctx.shell.shortcuts.declare(each, name));
|
|
1656
|
+
return () => {
|
|
1657
|
+
for (const remove of removals) remove();
|
|
1658
|
+
};
|
|
1659
|
+
}, []);
|
|
1660
|
+
// The node of the focused Sessions window marks the Sessions row.
|
|
1661
|
+
React.useEffect(() => {
|
|
1662
|
+
if (store && focused !== false) claimMark(ctx, store);
|
|
1663
|
+
}, [store, focused]);
|
|
1664
|
+
/** The shortcuts, reading this render's state (one listener, registered once); they act only while this window is
|
|
1665
|
+
* the focused one (declared to the shell's registry above). */
|
|
1666
|
+
const onKey = React.useRef<(event: KeyboardEvent) => void>(undefined);
|
|
1667
|
+
React.useEffect(() => {
|
|
1668
|
+
const listener = (event: KeyboardEvent) => onKey.current?.(event);
|
|
1669
|
+
document.addEventListener('keydown', listener);
|
|
1670
|
+
return () => document.removeEventListener('keydown', listener);
|
|
1671
|
+
}, []);
|
|
1672
|
+
|
|
1673
|
+
// Auto-scroll: new content keeps the conversation at its bottom only when the reader is there already.
|
|
1674
|
+
const hasProject = !!(listing?.project || listing?.workspace);
|
|
1675
|
+
React.useEffect(() => {
|
|
1676
|
+
const element = viewport.current;
|
|
1677
|
+
if (!element) return;
|
|
1678
|
+
const onScroll = () => {
|
|
1679
|
+
stick.current = element.scrollHeight - element.scrollTop - element.clientHeight < 80;
|
|
1680
|
+
setAway(element.scrollHeight - element.scrollTop - element.clientHeight > 240);
|
|
1681
|
+
if (element.scrollTop < EARLIER_AT) loadEarlier.current(true);
|
|
1682
|
+
};
|
|
1683
|
+
element.addEventListener('scroll', onScroll);
|
|
1684
|
+
return () => element.removeEventListener('scroll', onScroll);
|
|
1685
|
+
}, [hasProject, current]);
|
|
1686
|
+
React.useLayoutEffect(() => {
|
|
1687
|
+
const element = viewport.current;
|
|
1688
|
+
if (!element) return;
|
|
1689
|
+
// An earlier page landed above: the lines the reader saw stay where they were.
|
|
1690
|
+
const kept = earlier.current;
|
|
1691
|
+
if (kept?.height !== undefined) {
|
|
1692
|
+
earlier.current = undefined;
|
|
1693
|
+
const anchor =
|
|
1694
|
+
kept.anchor !== undefined
|
|
1695
|
+
? element.querySelector<HTMLElement>(`[data-turn="${kept.anchor}"]`)
|
|
1696
|
+
: null;
|
|
1697
|
+
// The anchor back where it was: what grew below it (a streamed delta in the same commit) moves nothing.
|
|
1698
|
+
if (anchor)
|
|
1699
|
+
element.scrollTop +=
|
|
1700
|
+
anchor.getBoundingClientRect().top - element.getBoundingClientRect().top - kept.offset!;
|
|
1701
|
+
else element.scrollTop = kept.top! + element.scrollHeight - kept.height;
|
|
1702
|
+
} else if (stick.current) element.scrollTop = element.scrollHeight;
|
|
1703
|
+
// Too short to scroll, or still at its top: the page before it.
|
|
1704
|
+
if (element.scrollTop < EARLIER_AT) loadEarlier.current();
|
|
1705
|
+
}, [events, live, dockHeight]);
|
|
1706
|
+
|
|
1707
|
+
// The node of a session from below: its title in the tree (kept in sync with it).
|
|
1708
|
+
React.useSyncExternalStore(ctx.nodes.subscribe, () => ctx.nodes.revision);
|
|
1709
|
+
const titleOf = (id: string) => ctx.nodes.list().find((each) => each.id === id)?.title ?? id;
|
|
1710
|
+
const session = listing?.sessions.find((each) => each.id === current);
|
|
1711
|
+
// Its pull request read again, or found from its branch, as it opens (research-086): a change comes back as `queue` on
|
|
1712
|
+
// its watch stream, which lists again.
|
|
1713
|
+
React.useEffect(() => {
|
|
1714
|
+
if (!current) return;
|
|
1715
|
+
void post('/pull-request/refresh', { id: current }).catch((cause: unknown) =>
|
|
1716
|
+
console.warn('sessions: pull request not read', cause),
|
|
1717
|
+
);
|
|
1718
|
+
}, [current, post]);
|
|
1719
|
+
const left = useLeftDecisions(session);
|
|
1720
|
+
// Child sessions (bridge#212): the list's top rows and each parent's children; the shown session's lineage.
|
|
1721
|
+
const family = React.useMemo(() => lineage(listing?.sessions ?? []), [listing?.sessions]);
|
|
1722
|
+
const kin = React.useMemo(
|
|
1723
|
+
() => new Map((listing?.sessions ?? []).map((each) => [each.id, each])),
|
|
1724
|
+
[listing?.sessions],
|
|
1725
|
+
);
|
|
1726
|
+
/** Stops child session `id` (its session Stop, which stops its own children too): its delegation ends interrupted,
|
|
1727
|
+
* and its parent's agent gets its last words. */
|
|
1728
|
+
const stopChild = React.useRef((_id: string) => {});
|
|
1729
|
+
stopChild.current = (id: string) =>
|
|
1730
|
+
void workspace
|
|
1731
|
+
.fetch(`${api}/interrupt?id=${encodeURIComponent(id)}`, { method: 'POST', headers })
|
|
1732
|
+
.then(readJson)
|
|
1733
|
+
.then(load, report);
|
|
1734
|
+
/** Stops sub-agent `sub` of the shown session through its harness (bridge#212 S8). */
|
|
1735
|
+
const stopSub = React.useRef((_sub: string) => {});
|
|
1736
|
+
stopSub.current = (sub: string) => {
|
|
1737
|
+
if (current)
|
|
1738
|
+
void post(`/subagent/stop?id=${encodeURIComponent(current)}`, { subagent: sub }).then(
|
|
1739
|
+
load,
|
|
1740
|
+
report,
|
|
1741
|
+
);
|
|
1742
|
+
};
|
|
1743
|
+
const lineageOf = React.useMemo<Lineage | undefined>(
|
|
1744
|
+
() =>
|
|
1745
|
+
session &&
|
|
1746
|
+
(session.parent !== undefined ||
|
|
1747
|
+
session.delegations?.length ||
|
|
1748
|
+
session.subagents?.length ||
|
|
1749
|
+
shownSub !== undefined)
|
|
1750
|
+
? {
|
|
1751
|
+
sessions: kin,
|
|
1752
|
+
delegations: session.delegations ?? [],
|
|
1753
|
+
parent: session.parent,
|
|
1754
|
+
// A listed session opens in this window's list; another (another project's) from the workspace's.
|
|
1755
|
+
to: (id) =>
|
|
1756
|
+
kin.has(id)
|
|
1757
|
+
? { item: id }
|
|
1758
|
+
: { node: 'root', screen: { plugin: name, id: screen }, item: id },
|
|
1759
|
+
stop: (id) => stopChild.current(id),
|
|
1760
|
+
// Its sub-agents' views are this window's items too (bridge#212 S8).
|
|
1761
|
+
subagentTo: (sub) => ({ item: subagentItem(session.id, sub) }),
|
|
1762
|
+
stopSubagent: (sub) => stopSub.current(sub),
|
|
1763
|
+
}
|
|
1764
|
+
: undefined,
|
|
1765
|
+
[kin, session?.id, session?.delegations, session?.parent, session?.subagents, shownSub, screen],
|
|
1766
|
+
);
|
|
1767
|
+
// Its tabs beside the chat (./tabs.tsx: `sessions.tab`, an agent's browser).
|
|
1768
|
+
const tabs = useSessionTabs(current);
|
|
1769
|
+
/** The header's title, the chat's tab among the session's tabs while it has some. */
|
|
1770
|
+
const withTabs = (title: React.ReactNode) =>
|
|
1771
|
+
tabs.open.length && session ? (
|
|
1772
|
+
<TabStrip
|
|
1773
|
+
open={tabs.open}
|
|
1774
|
+
shown={tabs.shown}
|
|
1775
|
+
tabs={tabs.value}
|
|
1776
|
+
chat={title}
|
|
1777
|
+
title={draft ?? session.title}
|
|
1778
|
+
/>
|
|
1779
|
+
) : (
|
|
1780
|
+
title
|
|
1781
|
+
);
|
|
1782
|
+
// The open session's pinned images (the gallery's), and the gallery itself.
|
|
1783
|
+
const [pins, setPins] = React.useState<Pinned[]>([]);
|
|
1784
|
+
const [galleryOpen, setGalleryOpen] = React.useState(false);
|
|
1785
|
+
const loadPins = (scope: Scope) =>
|
|
1786
|
+
workspace
|
|
1787
|
+
.fetch(
|
|
1788
|
+
`${api}/gallery${scope.session ? `?session=${encodeURIComponent(scope.session)}` : ''}`,
|
|
1789
|
+
{ headers: { ...headers, 'x-bridge-node': scope.node } },
|
|
1790
|
+
)
|
|
1791
|
+
.then(readJson)
|
|
1792
|
+
.then((body) => (body as { pins: Pinned[] }).pins);
|
|
1793
|
+
const sessionNode = session?.node;
|
|
1794
|
+
React.useEffect(() => {
|
|
1795
|
+
setPins([]);
|
|
1796
|
+
if (!current || !sessionNode) return;
|
|
1797
|
+
let live = true;
|
|
1798
|
+
loadPins({ key: 'session', label: 'Session', node: sessionNode, session: current }).then(
|
|
1799
|
+
(found) => {
|
|
1800
|
+
if (live) setPins(found);
|
|
1801
|
+
},
|
|
1802
|
+
() => {},
|
|
1803
|
+
);
|
|
1804
|
+
return () => {
|
|
1805
|
+
live = false;
|
|
1806
|
+
};
|
|
1807
|
+
}, [current, sessionNode]);
|
|
1808
|
+
const unpinImage = (pin: Pinned) =>
|
|
1809
|
+
workspace
|
|
1810
|
+
.fetch(`${api}/gallery/remove`, {
|
|
1811
|
+
method: 'POST',
|
|
1812
|
+
headers,
|
|
1813
|
+
body: JSON.stringify({ pin: pin.id }),
|
|
1814
|
+
})
|
|
1815
|
+
.then(readJson)
|
|
1816
|
+
.then(() => setPins((was) => was.filter((each) => each.id !== pin.id)));
|
|
1817
|
+
const samePin = (image: Viewed) => (pin: Pinned) =>
|
|
1818
|
+
(image.source !== undefined && pin.source === image.source) ||
|
|
1819
|
+
(image.blob !== undefined && pin.blob === image.blob);
|
|
1820
|
+
/** The routes a view the agent showed reads and writes, as the user (research-036). */
|
|
1821
|
+
const viewTools = React.useMemo(
|
|
1822
|
+
() => viewToolsOf(workspace.slug, (url, init) => workspace.fetch(url, init)),
|
|
1823
|
+
[workspace],
|
|
1824
|
+
);
|
|
1825
|
+
/** Draws a view's plugin component through `views.component`, keyed by its plugin (research-036 P2); a plugin not
|
|
1826
|
+
* active here falls back to the ask that proposes activating it on this session's node. */
|
|
1827
|
+
const renderComponent = React.useCallback<ViewComponentRenderer>(
|
|
1828
|
+
({ plugin, name, props }) =>
|
|
1829
|
+
renderSlot(
|
|
1830
|
+
'views.component',
|
|
1831
|
+
{ component: name, props, node: sessionNode ?? node },
|
|
1832
|
+
{
|
|
1833
|
+
entryKey: plugin,
|
|
1834
|
+
fallback: <Missing plugin={plugin} name={name} session={current ?? ''} post={post} />,
|
|
1835
|
+
},
|
|
1836
|
+
),
|
|
1837
|
+
[renderSlot, node, sessionNode, current, post],
|
|
1838
|
+
);
|
|
1839
|
+
const galleryPins = React.useMemo(
|
|
1840
|
+
() =>
|
|
1841
|
+
current && session
|
|
1842
|
+
? {
|
|
1843
|
+
pinned: (image: Viewed) => pins.find(samePin(image)),
|
|
1844
|
+
toggle: async (image: Viewed) => {
|
|
1845
|
+
const pinned = pins.find(samePin(image));
|
|
1846
|
+
try {
|
|
1847
|
+
if (pinned) return await unpinImage(pinned);
|
|
1848
|
+
// Bridge copies it itself, from the file the agent showed or the image attached (never through the page).
|
|
1849
|
+
const pin = (await workspace
|
|
1850
|
+
.fetch(`${api}/gallery?id=${encodeURIComponent(current)}`, {
|
|
1851
|
+
method: 'POST',
|
|
1852
|
+
headers: { ...headers, 'x-bridge-node': session.node },
|
|
1853
|
+
body: JSON.stringify(
|
|
1854
|
+
image.source !== undefined
|
|
1855
|
+
? {
|
|
1856
|
+
path: image.source,
|
|
1857
|
+
name: image.name,
|
|
1858
|
+
...(image.caption && { caption: image.caption }),
|
|
1859
|
+
}
|
|
1860
|
+
: { blob: image.blob, name: image.name },
|
|
1861
|
+
),
|
|
1862
|
+
})
|
|
1863
|
+
.then(readJson)) as Pinned;
|
|
1864
|
+
setPins((was) => [pin, ...was.filter((each) => each.id !== pin.id)]);
|
|
1865
|
+
} catch (cause) {
|
|
1866
|
+
report(cause);
|
|
1867
|
+
}
|
|
1868
|
+
},
|
|
1869
|
+
}
|
|
1870
|
+
: undefined,
|
|
1871
|
+
[current, session?.node, pins],
|
|
1872
|
+
);
|
|
1873
|
+
const running = live !== null;
|
|
1874
|
+
/** Marks session `id` read; the listing follows. */
|
|
1875
|
+
function visit(id: string) {
|
|
1876
|
+
void workspace
|
|
1877
|
+
.fetch(`${api}/triage`, {
|
|
1878
|
+
method: 'POST',
|
|
1879
|
+
headers,
|
|
1880
|
+
body: JSON.stringify({ id, action: 'visit' }),
|
|
1881
|
+
})
|
|
1882
|
+
.then(readJson)
|
|
1883
|
+
.then(load, () => {});
|
|
1884
|
+
}
|
|
1885
|
+
const triage = (
|
|
1886
|
+
id: string,
|
|
1887
|
+
action: Action | 'reorder' | 'snooze',
|
|
1888
|
+
extra: object = {},
|
|
1889
|
+
): Promise<Session> =>
|
|
1890
|
+
workspace
|
|
1891
|
+
.fetch(`${api}/triage`, {
|
|
1892
|
+
method: 'POST',
|
|
1893
|
+
headers,
|
|
1894
|
+
body: JSON.stringify({ id, action, ...extra }),
|
|
1895
|
+
})
|
|
1896
|
+
.then(readJson)
|
|
1897
|
+
.then((body) => body as Session);
|
|
1898
|
+
const failed = (title: string) => (cause: unknown) => {
|
|
1899
|
+
if ((cause as Error)?.name !== 'AbortError')
|
|
1900
|
+
toast({
|
|
1901
|
+
title,
|
|
1902
|
+
description: cause instanceof Error ? cause.message : String(cause),
|
|
1903
|
+
variant: 'error',
|
|
1904
|
+
});
|
|
1905
|
+
};
|
|
1906
|
+
const newChat = () => {
|
|
1907
|
+
choose(undefined);
|
|
1908
|
+
promptHandle.current?.focus();
|
|
1909
|
+
};
|
|
1910
|
+
const answer = async (seq: number, decision: string, option: Answer) => {
|
|
1911
|
+
if (!current) return;
|
|
1912
|
+
const key = `${current}:${seq}`;
|
|
1913
|
+
setError('');
|
|
1914
|
+
setAnswering((was) => ({ ...was, [key]: true }));
|
|
1915
|
+
try {
|
|
1916
|
+
await workspace
|
|
1917
|
+
.fetch(`${api}/answer?id=${encodeURIComponent(current)}`, {
|
|
1918
|
+
method: 'POST',
|
|
1919
|
+
headers,
|
|
1920
|
+
body: JSON.stringify(answerBody(decision, option)),
|
|
1921
|
+
})
|
|
1922
|
+
.then(readJson);
|
|
1923
|
+
} catch (cause) {
|
|
1924
|
+
// Closed meanwhile (another page answered first, or it expired): its stored line shows the outcome.
|
|
1925
|
+
if ((cause as { code?: unknown })?.code !== 'decision_closed') report(cause);
|
|
1926
|
+
} finally {
|
|
1927
|
+
setAnswering(({ [key]: _done, ...rest }) => rest);
|
|
1928
|
+
}
|
|
1929
|
+
};
|
|
1930
|
+
const stop = () => {
|
|
1931
|
+
if (current)
|
|
1932
|
+
void workspace
|
|
1933
|
+
.fetch(`${api}/interrupt?id=${encodeURIComponent(current)}`, { method: 'POST', headers })
|
|
1934
|
+
.then(readJson)
|
|
1935
|
+
.then(load, report);
|
|
1936
|
+
};
|
|
1937
|
+
/** Answers another session's question in its agent's place (Force continue: ok, Stop: no), or gives it more time. */
|
|
1938
|
+
const answerPeer = (request: string, verdict: 'ok' | 'no') =>
|
|
1939
|
+
post('/peer/answer', { request, verdict }).then(load, report);
|
|
1940
|
+
const extendPeer = (request: string) => post('/peer/extend', { request }).then(load, report);
|
|
1941
|
+
/** Scrolls the conversation to its end, which it then follows. */
|
|
1942
|
+
const toEnd = () => {
|
|
1943
|
+
const element = viewport.current;
|
|
1944
|
+
if (!element) return;
|
|
1945
|
+
stick.current = true;
|
|
1946
|
+
setAway(false);
|
|
1947
|
+
element.scrollTo({ top: element.scrollHeight, behavior: 'smooth' });
|
|
1948
|
+
};
|
|
1949
|
+
|
|
1950
|
+
if (!listing) return <section aria-label="Screen" className="flex min-h-0 flex-1" />;
|
|
1951
|
+
if (!listing.project && !listing.workspace) {
|
|
1952
|
+
return (
|
|
1953
|
+
<section
|
|
1954
|
+
aria-label="Screen"
|
|
1955
|
+
className={cn('flex min-h-0 flex-1 items-center justify-center p-6', reset)}
|
|
1956
|
+
>
|
|
1957
|
+
<EmptyState
|
|
1958
|
+
className="max-w-sm"
|
|
1959
|
+
icon={
|
|
1960
|
+
<span className="flex size-10 items-center justify-center rounded-full bg-hover text-secondary">
|
|
1961
|
+
<Icon name="messages-square" size={20} />
|
|
1962
|
+
</span>
|
|
1963
|
+
}
|
|
1964
|
+
>
|
|
1965
|
+
<h1 className="text-base font-semibold text-foreground">Sessions</h1>
|
|
1966
|
+
<p role="note" className="text-sm text-secondary">
|
|
1967
|
+
Sessions live in projects and on the workspace. Open one to start a session.
|
|
1968
|
+
</p>
|
|
1969
|
+
</EmptyState>
|
|
1970
|
+
</section>
|
|
1971
|
+
);
|
|
1972
|
+
}
|
|
1973
|
+
|
|
1974
|
+
const folder = listing.project
|
|
1975
|
+
? (listing.project.path.split(/[\\/]/).filter(Boolean).at(-1) ?? listing.project.path)
|
|
1976
|
+
: listing.workspace!.slug;
|
|
1977
|
+
|
|
1978
|
+
// The shelves: a snoozed session is in Snoozed until it wakes (a pin may wait under it); else a pinned one is in
|
|
1979
|
+
// Pinned whatever else it is (settle clears the pin).
|
|
1980
|
+
// A child is listed under its parent, never on a shelf of its own; its parent works or waits while it does.
|
|
1981
|
+
const shownSessions = tagged
|
|
1982
|
+
? family.top.filter((each) => each.tags?.includes(tagged))
|
|
1983
|
+
: family.top;
|
|
1984
|
+
const snoozed = shownSessions
|
|
1985
|
+
.filter((each) => each.snoozedUntil)
|
|
1986
|
+
.sort((a, b) => a.snoozedUntil!.localeCompare(b.snoozedUntil!));
|
|
1987
|
+
const awake = shownSessions.filter((each) => !each.snoozedUntil);
|
|
1988
|
+
const pinned = awake
|
|
1989
|
+
.filter((each) => each.pinnedAt)
|
|
1990
|
+
.sort((a, b) => (a.pinOrder ?? 0) - (b.pinOrder ?? 0));
|
|
1991
|
+
const unsorted = awake.filter((each) => !each.pinnedAt && !each.settledAt);
|
|
1992
|
+
const { active, working } = folding ? inbox(unsorted) : { active: unsorted, working: [] };
|
|
1993
|
+
const settled = awake
|
|
1994
|
+
.filter((each) => !each.pinnedAt && each.settledAt)
|
|
1995
|
+
.sort((a, b) => b.settledAt!.localeCompare(a.settledAt!));
|
|
1996
|
+
const archivedShown = tagged ? archived?.filter((each) => each.tags?.includes(tagged)) : archived;
|
|
1997
|
+
const settledShown = allSettled ? settled : settled.slice(0, SETTLED_SHOWN);
|
|
1998
|
+
/** The rows in list order, for previous and next. */
|
|
1999
|
+
const ordered = [
|
|
2000
|
+
...(isOpen('pinned') ? pinned : []),
|
|
2001
|
+
...(isOpen('active') ? active : []),
|
|
2002
|
+
...(isOpen('working') ? working : []),
|
|
2003
|
+
...(isOpen('snoozed') ? snoozed : []),
|
|
2004
|
+
...(isOpen('settled') ? settledShown : []),
|
|
2005
|
+
];
|
|
2006
|
+
|
|
2007
|
+
/** Offers Undo for `UNDO_MS`: a toast's button, and `mod+z` meanwhile. */
|
|
2008
|
+
const offerUndo = (title: string, description: string, run: () => Promise<unknown>) => {
|
|
2009
|
+
let done = false;
|
|
2010
|
+
let id = '';
|
|
2011
|
+
const entry = {
|
|
2012
|
+
undo: () => {
|
|
2013
|
+
if (done) return;
|
|
2014
|
+
done = true;
|
|
2015
|
+
if (undoable.current === entry) undoable.current = null;
|
|
2016
|
+
dismiss(id);
|
|
2017
|
+
void run().then(
|
|
2018
|
+
() => Promise.all([load(), archived && loadArchived()]),
|
|
2019
|
+
failed('Could not undo'),
|
|
2020
|
+
);
|
|
2021
|
+
},
|
|
2022
|
+
};
|
|
2023
|
+
id = toast({
|
|
2024
|
+
title,
|
|
2025
|
+
description,
|
|
2026
|
+
duration: UNDO_MS,
|
|
2027
|
+
key: 'sessions-undo',
|
|
2028
|
+
action: { label: 'Undo', onClick: entry.undo },
|
|
2029
|
+
});
|
|
2030
|
+
undoable.current = entry;
|
|
2031
|
+
setTimeout(() => {
|
|
2032
|
+
if (undoable.current === entry) undoable.current = null;
|
|
2033
|
+
}, UNDO_MS);
|
|
2034
|
+
};
|
|
2035
|
+
/** Runs `action` on `each`: settling or archiving the open session opens the next Active one; settle, unpin and
|
|
2036
|
+
* archive offer Undo (undoing a settle pins it back where it was; undoing one that left the open session reopens it). */
|
|
2037
|
+
const act = async (each: Session, action: Action) => {
|
|
2038
|
+
const leaving = each.id === current && (action === 'settle' || action === 'archive');
|
|
2039
|
+
const others = active.filter((other) => other.id !== each.id);
|
|
2040
|
+
const next =
|
|
2041
|
+
others[
|
|
2042
|
+
Math.min(
|
|
2043
|
+
Math.max(
|
|
2044
|
+
active.findIndex((other) => other.id === each.id),
|
|
2045
|
+
0,
|
|
2046
|
+
),
|
|
2047
|
+
others.length - 1,
|
|
2048
|
+
)
|
|
2049
|
+
]?.id;
|
|
2050
|
+
try {
|
|
2051
|
+
await triage(each.id, action);
|
|
2052
|
+
} catch (cause) {
|
|
2053
|
+
failed(`Could not ${action === 'unread' ? 'mark it unread' : action} the session`)(cause);
|
|
2054
|
+
return;
|
|
2055
|
+
}
|
|
2056
|
+
if (leaving) choose(next);
|
|
2057
|
+
/** Undoing a settle or an archive that left the open session opens it again, and snoozes it again when that ended
|
|
2058
|
+
* its snooze (and its time is still ahead). */
|
|
2059
|
+
const back = async () => {
|
|
2060
|
+
if (each.snoozedUntil && Date.parse(each.snoozedUntil) > Date.now())
|
|
2061
|
+
await triage(each.id, 'snooze', { until: each.snoozedUntil });
|
|
2062
|
+
if (leaving) choose(each.id);
|
|
2063
|
+
};
|
|
2064
|
+
if (action === 'settle') {
|
|
2065
|
+
offerUndo('Session settled', each.title, async () => {
|
|
2066
|
+
await triage(each.id, 'unsettle');
|
|
2067
|
+
if (each.pinnedAt) await triage(each.id, 'pin', { order: each.pinOrder });
|
|
2068
|
+
await back();
|
|
2069
|
+
});
|
|
2070
|
+
} else if (action === 'unpin')
|
|
2071
|
+
offerUndo('Session unpinned', each.title, () =>
|
|
2072
|
+
triage(each.id, 'pin', { order: each.pinOrder }),
|
|
2073
|
+
);
|
|
2074
|
+
else if (action === 'archive')
|
|
2075
|
+
offerUndo('Session archived', each.title, () => triage(each.id, 'restore').then(back));
|
|
2076
|
+
await Promise.all([load(), (archived || action === 'restore') && loadArchived()]);
|
|
2077
|
+
// Its branch's last session archived (research-083): its folder may go too, asked.
|
|
2078
|
+
const branch = each.branch;
|
|
2079
|
+
if (
|
|
2080
|
+
action === 'archive' &&
|
|
2081
|
+
branch &&
|
|
2082
|
+
!listing?.sessions.some(
|
|
2083
|
+
(other) => other.id !== each.id && other.branch?.folder === branch.folder,
|
|
2084
|
+
)
|
|
2085
|
+
)
|
|
2086
|
+
// Its own project's: the node shown may be an ancestor (the root lists every session).
|
|
2087
|
+
await cleanupOf(branchesAt, each.node, branch.name)
|
|
2088
|
+
.then(setCleanup)
|
|
2089
|
+
.catch(failed('Its branch could not be read'));
|
|
2090
|
+
};
|
|
2091
|
+
/** Snoozes `each` until `until` (`unpin`: a pinned one leaves Pinned too); the open session leaves for the next Active
|
|
2092
|
+
* one. Undo wakes it and puts back the settle or the pin it cleared. */
|
|
2093
|
+
const snooze = async (each: Session, until: string, unpin = false) => {
|
|
2094
|
+
const leaving = each.id === current;
|
|
2095
|
+
const others = active.filter((other) => other.id !== each.id);
|
|
2096
|
+
const next =
|
|
2097
|
+
others[
|
|
2098
|
+
Math.min(
|
|
2099
|
+
Math.max(
|
|
2100
|
+
active.findIndex((other) => other.id === each.id),
|
|
2101
|
+
0,
|
|
2102
|
+
),
|
|
2103
|
+
others.length - 1,
|
|
2104
|
+
)
|
|
2105
|
+
]?.id;
|
|
2106
|
+
try {
|
|
2107
|
+
await triage(each.id, 'snooze', { until, ...(unpin && { unpin }) });
|
|
2108
|
+
} catch (cause) {
|
|
2109
|
+
failed('Could not snooze the session')(cause);
|
|
2110
|
+
return;
|
|
2111
|
+
}
|
|
2112
|
+
if (leaving) choose(next);
|
|
2113
|
+
offerUndo('Session snoozed', `${each.title}, until ${when(until)}`, async () => {
|
|
2114
|
+
await triage(each.id, 'wake');
|
|
2115
|
+
if (each.settledAt && !each.pinnedAt) await triage(each.id, 'settle');
|
|
2116
|
+
if (unpin && each.pinnedAt) await triage(each.id, 'pin', { order: each.pinOrder });
|
|
2117
|
+
if (leaving) choose(each.id);
|
|
2118
|
+
});
|
|
2119
|
+
await load();
|
|
2120
|
+
};
|
|
2121
|
+
/** A preset or a custom time chosen for `each`: a pinned one asks first whether it stays pinned. */
|
|
2122
|
+
const chooseSnooze = (each: Session, until: string) => {
|
|
2123
|
+
if (each.pinnedAt) setPinAsk({ session: each, until });
|
|
2124
|
+
else void snooze(each, until);
|
|
2125
|
+
};
|
|
2126
|
+
/** A pinned row dragged to a new place: its order between its new neighbours'. */
|
|
2127
|
+
const reorder = (ids: string[]) => {
|
|
2128
|
+
const was = pinned.map((each) => each.id);
|
|
2129
|
+
const first = ids.findIndex((id, index) => id !== was[index]);
|
|
2130
|
+
if (first < 0) return;
|
|
2131
|
+
let last = ids.length - 1;
|
|
2132
|
+
while (ids[last] === was[last]) last--;
|
|
2133
|
+
// Moved up: it is first in the changed run; moved down: last.
|
|
2134
|
+
const moved = ids[first] === was[last] ? ids[first]! : ids[last]!;
|
|
2135
|
+
const at = ids.indexOf(moved);
|
|
2136
|
+
const orderOf = (id: string) => pinned.find((each) => each.id === id)?.pinOrder ?? 0;
|
|
2137
|
+
const before = ids[at - 1];
|
|
2138
|
+
const after = ids[at + 1];
|
|
2139
|
+
const order =
|
|
2140
|
+
before && after
|
|
2141
|
+
? (orderOf(before) + orderOf(after)) / 2
|
|
2142
|
+
: before
|
|
2143
|
+
? orderOf(before) + 1
|
|
2144
|
+
: orderOf(after!) - 1;
|
|
2145
|
+
setListing(
|
|
2146
|
+
(was) =>
|
|
2147
|
+
was && {
|
|
2148
|
+
...was,
|
|
2149
|
+
sessions: was.sessions.map((each) =>
|
|
2150
|
+
each.id === moved ? { ...each, pinOrder: order } : each,
|
|
2151
|
+
),
|
|
2152
|
+
},
|
|
2153
|
+
);
|
|
2154
|
+
void triage(moved, 'reorder', { order }).then(load, failed('Could not move the session'));
|
|
2155
|
+
};
|
|
2156
|
+
/** A pinned session one place up (-1) or down (1): the keyboard's way to reorder. */
|
|
2157
|
+
const move = (each: Session, by: -1 | 1) => {
|
|
2158
|
+
const ids = pinned.map((one) => one.id);
|
|
2159
|
+
const at = ids.indexOf(each.id);
|
|
2160
|
+
const to = at + by;
|
|
2161
|
+
if (at < 0 || to < 0 || to >= ids.length) return;
|
|
2162
|
+
[ids[at], ids[to]] = [ids[to]!, ids[at]!];
|
|
2163
|
+
reorder(ids);
|
|
2164
|
+
};
|
|
2165
|
+
const rename = () => {
|
|
2166
|
+
const title = draft?.trim();
|
|
2167
|
+
setDraft(undefined);
|
|
2168
|
+
if (!session || !title || title === session.title) return;
|
|
2169
|
+
setListing(
|
|
2170
|
+
(was) =>
|
|
2171
|
+
was && {
|
|
2172
|
+
...was,
|
|
2173
|
+
sessions: was.sessions.map((each) =>
|
|
2174
|
+
each.id === session.id ? { ...each, title } : each,
|
|
2175
|
+
),
|
|
2176
|
+
},
|
|
2177
|
+
);
|
|
2178
|
+
void workspace
|
|
2179
|
+
.fetch(`${api}/rename`, {
|
|
2180
|
+
method: 'POST',
|
|
2181
|
+
headers,
|
|
2182
|
+
body: JSON.stringify({ id: session.id, title }),
|
|
2183
|
+
})
|
|
2184
|
+
.then(readJson)
|
|
2185
|
+
.then(load, (cause) => {
|
|
2186
|
+
failed('Could not rename the session')(cause);
|
|
2187
|
+
void load();
|
|
2188
|
+
});
|
|
2189
|
+
};
|
|
2190
|
+
const askRename = (each: Session) => {
|
|
2191
|
+
renameAsked.current = each.id;
|
|
2192
|
+
choose(each.id);
|
|
2193
|
+
};
|
|
2194
|
+
onKey.current = (event) => {
|
|
2195
|
+
// Another window is the one the user works in, or a modal dialog is open over the page: the keys are its own.
|
|
2196
|
+
if (focused === false || modalOpen()) return;
|
|
2197
|
+
// Escape twice within a second stops the running turn (a dialog, a menu or a popup list open takes the first one;
|
|
2198
|
+
// the session lists, always there, do not count).
|
|
2199
|
+
if (
|
|
2200
|
+
event.key === 'Escape' &&
|
|
2201
|
+
!event.defaultPrevented &&
|
|
2202
|
+
running &&
|
|
2203
|
+
!document.querySelector('[role=dialog], [role=menu], [role=listbox]:not([data-slot=list])')
|
|
2204
|
+
) {
|
|
2205
|
+
if (Date.now() - escaped.current < 1000) {
|
|
2206
|
+
escaped.current = 0;
|
|
2207
|
+
stop();
|
|
2208
|
+
} else escaped.current = Date.now();
|
|
2209
|
+
return;
|
|
2210
|
+
}
|
|
2211
|
+
if (!(event.metaKey || event.ctrlKey) || event.altKey) return;
|
|
2212
|
+
if (!event.shiftKey && event.code === 'KeyZ' && undoable.current && !editing(event.target)) {
|
|
2213
|
+
event.preventDefault();
|
|
2214
|
+
undoable.current.undo();
|
|
2215
|
+
return;
|
|
2216
|
+
}
|
|
2217
|
+
if (!event.shiftKey) return;
|
|
2218
|
+
if (event.code === 'KeyS' && session) {
|
|
2219
|
+
event.preventDefault();
|
|
2220
|
+
void act(session, session.settledAt && !session.pinnedAt ? 'unsettle' : 'settle');
|
|
2221
|
+
} else if (event.code === 'KeyP' && session) {
|
|
2222
|
+
event.preventDefault();
|
|
2223
|
+
void act(session, session.pinnedAt ? 'unpin' : 'pin');
|
|
2224
|
+
} else if (event.code === 'KeyM') {
|
|
2225
|
+
event.preventDefault();
|
|
2226
|
+
// ⌘⇧M opens the prompt's model picker (the Prompt service's handle; ⌘⇧M is nothing without one).
|
|
2227
|
+
promptHandle.current?.openModels();
|
|
2228
|
+
} else if (event.code === 'BracketLeft' || event.code === 'BracketRight') {
|
|
2229
|
+
event.preventDefault();
|
|
2230
|
+
const index = ordered.findIndex((each) => each.id === current);
|
|
2231
|
+
const step = event.code === 'BracketLeft' ? -1 : 1;
|
|
2232
|
+
const to = ordered[index < 0 ? (step > 0 ? 0 : ordered.length - 1) : index + step];
|
|
2233
|
+
if (to) choose(to.id);
|
|
2234
|
+
}
|
|
2235
|
+
};
|
|
2236
|
+
|
|
2237
|
+
/** Its status, the first that holds (research-010): a decision waiting, a turn running, a failed turn unread, unread,
|
|
2238
|
+
* woken from a snooze, else idle. The open session is being read. */
|
|
2239
|
+
const rowStatus = (each: Session) => {
|
|
2240
|
+
const unread = each.unread && each.id !== current;
|
|
2241
|
+
if (each.waiting)
|
|
2242
|
+
return each.questions?.some((question) => question.expired) ? 'question' : 'decision';
|
|
2243
|
+
if (each.running) return 'running';
|
|
2244
|
+
if (unread && each.lastTurn?.reason === 'failed') return 'failed';
|
|
2245
|
+
if (unread) return 'unread';
|
|
2246
|
+
return each.woke && each.id !== current ? 'woke' : 'idle';
|
|
2247
|
+
};
|
|
2248
|
+
/** A session's actions, in its menus; `keys`: with the open session's shortcuts. */
|
|
2249
|
+
const menuItems = (each: Session, keys = false) => {
|
|
2250
|
+
const isSettled = !!each.settledAt && !each.pinnedAt;
|
|
2251
|
+
return (
|
|
2252
|
+
<>
|
|
2253
|
+
<DropdownMenuItem onSelect={() => void act(each, each.pinnedAt ? 'unpin' : 'pin')}>
|
|
2254
|
+
<Icon name={each.pinnedAt ? 'pin-off' : 'pin'} size={14} />
|
|
2255
|
+
{each.pinnedAt ? 'Unpin' : 'Pin'}
|
|
2256
|
+
{keys && <DropdownMenuShortcut>{shortcut('P')}</DropdownMenuShortcut>}
|
|
2257
|
+
</DropdownMenuItem>
|
|
2258
|
+
<DropdownMenuItem
|
|
2259
|
+
disabled={!isSettled && each.running}
|
|
2260
|
+
onSelect={() => void act(each, isSettled ? 'unsettle' : 'settle')}
|
|
2261
|
+
>
|
|
2262
|
+
<Icon name={isSettled ? 'undo' : 'check'} size={14} />
|
|
2263
|
+
{isSettled ? 'Un-settle' : 'Settle'}
|
|
2264
|
+
{keys && <DropdownMenuShortcut>{shortcut('S')}</DropdownMenuShortcut>}
|
|
2265
|
+
</DropdownMenuItem>
|
|
2266
|
+
{listing.snooze && (
|
|
2267
|
+
<>
|
|
2268
|
+
<SnoozeMenu
|
|
2269
|
+
choices={snoozeChoices}
|
|
2270
|
+
disabled={each.waiting}
|
|
2271
|
+
onSnooze={(until) => chooseSnooze(each, until)}
|
|
2272
|
+
onCustom={() => setCustomFor(each)}
|
|
2273
|
+
/>
|
|
2274
|
+
{each.snoozedUntil && (
|
|
2275
|
+
<DropdownMenuItem onSelect={() => void act(each, 'wake')}>
|
|
2276
|
+
<Icon name="alarm-clock-off" size={14} />
|
|
2277
|
+
Wake now
|
|
2278
|
+
</DropdownMenuItem>
|
|
2279
|
+
)}
|
|
2280
|
+
</>
|
|
2281
|
+
)}
|
|
2282
|
+
<DropdownMenuItem onSelect={() => askRename(each)}>
|
|
2283
|
+
<Icon name="square-pen" size={14} />
|
|
2284
|
+
Rename
|
|
2285
|
+
</DropdownMenuItem>
|
|
2286
|
+
{each.pinnedAt && pinned.length > 1 && (
|
|
2287
|
+
<>
|
|
2288
|
+
<DropdownMenuItem disabled={pinned[0]?.id === each.id} onSelect={() => move(each, -1)}>
|
|
2289
|
+
<Icon name="arrow-up" size={14} />
|
|
2290
|
+
Move up
|
|
2291
|
+
</DropdownMenuItem>
|
|
2292
|
+
<DropdownMenuItem
|
|
2293
|
+
disabled={pinned.at(-1)?.id === each.id}
|
|
2294
|
+
onSelect={() => move(each, 1)}
|
|
2295
|
+
>
|
|
2296
|
+
<Icon name="arrow-down" size={14} />
|
|
2297
|
+
Move down
|
|
2298
|
+
</DropdownMenuItem>
|
|
2299
|
+
</>
|
|
2300
|
+
)}
|
|
2301
|
+
{each.lastTurn && (!each.unread || each.id === current) && (
|
|
2302
|
+
<DropdownMenuItem onSelect={() => void act(each, 'unread')}>
|
|
2303
|
+
<Icon name="circle-dot" size={14} />
|
|
2304
|
+
Mark unread
|
|
2305
|
+
</DropdownMenuItem>
|
|
2306
|
+
)}
|
|
2307
|
+
<DropdownMenuSeparator />
|
|
2308
|
+
<DropdownMenuItem
|
|
2309
|
+
variant="destructive"
|
|
2310
|
+
disabled={each.running}
|
|
2311
|
+
onSelect={() => void act(each, 'archive')}
|
|
2312
|
+
>
|
|
2313
|
+
<Icon name="archive" size={14} />
|
|
2314
|
+
Archive
|
|
2315
|
+
</DropdownMenuItem>
|
|
2316
|
+
</>
|
|
2317
|
+
);
|
|
2318
|
+
};
|
|
2319
|
+
// Each row's menus and buttons call the page as it is when used; a row draws again only when what it shows changed.
|
|
2320
|
+
rowActions.current = {
|
|
2321
|
+
act,
|
|
2322
|
+
menuItems,
|
|
2323
|
+
setTagged,
|
|
2324
|
+
toggleChildren: (id) => setShownChildren((was) => ({ ...was, [id]: !childrenShown(id) })),
|
|
2325
|
+
};
|
|
2326
|
+
/** Whether `id`'s children show under its row: as the user left it, else while one of them works or waits, or is
|
|
2327
|
+
* (or holds) the open session. */
|
|
2328
|
+
const childrenShown = (id: string): boolean => {
|
|
2329
|
+
const chosen = shownChildren[id];
|
|
2330
|
+
if (chosen !== undefined) return chosen;
|
|
2331
|
+
const within = (at: string, seen: Set<string>): boolean =>
|
|
2332
|
+
// One of its sub-agents runs, or is the one shown (bridge#212 S8).
|
|
2333
|
+
(!!kin.get(at)?.running &&
|
|
2334
|
+
!!kin.get(at)?.subagents?.some((sub) => sub.status === 'running')) ||
|
|
2335
|
+
(at === current && shownSub !== undefined) ||
|
|
2336
|
+
(family.children.get(at) ?? []).some(
|
|
2337
|
+
(child) =>
|
|
2338
|
+
!seen.has(child.id) &&
|
|
2339
|
+
(seen.add(child.id),
|
|
2340
|
+
child.running || child.waiting || child.id === current || within(child.id, seen)),
|
|
2341
|
+
);
|
|
2342
|
+
return within(id, new Set([id]));
|
|
2343
|
+
};
|
|
2344
|
+
/** A row, then its children's while shown (`depth`: its nesting under a top row). */
|
|
2345
|
+
const rows = (each: Session, shelf: Shelf, depth = 0): React.ReactNode[] => {
|
|
2346
|
+
const children = shelf === 'archived' ? [] : (family.children.get(each.id) ?? []);
|
|
2347
|
+
// Its agent's sub-agents (bridge#212 S8), after its children.
|
|
2348
|
+
const agents = shelf === 'archived' ? [] : (each.subagents ?? []);
|
|
2349
|
+
const open = children.length + agents.length > 0 && childrenShown(each.id);
|
|
2350
|
+
return [
|
|
2351
|
+
row(each, shelf, depth, children.length, open, agents.length),
|
|
2352
|
+
...(open && depth < 8
|
|
2353
|
+
? [
|
|
2354
|
+
...children.flatMap((child) => rows(child, shelf, depth + 1)),
|
|
2355
|
+
...agents.map((sub) => (
|
|
2356
|
+
<SubagentRow
|
|
2357
|
+
key={subagentItem(each.id, sub.id)}
|
|
2358
|
+
session={each.id}
|
|
2359
|
+
sub={sub}
|
|
2360
|
+
depth={depth + 1}
|
|
2361
|
+
sessionRunning={!!each.running}
|
|
2362
|
+
modelLabel={modelLabelOf(each).replace(' (default)', '')}
|
|
2363
|
+
provider={providerOf(harnessLabel(each.harness), each.model)}
|
|
2364
|
+
/>
|
|
2365
|
+
)),
|
|
2366
|
+
]
|
|
2367
|
+
: []),
|
|
2368
|
+
];
|
|
2369
|
+
};
|
|
2370
|
+
const row = (each: Session, shelf: Shelf, depth = 0, kids = 0, kidsOpen = false, agents = 0) => {
|
|
2371
|
+
const status = shelf === 'archived' ? 'idle' : rowStatus(each);
|
|
2372
|
+
// A child (bridge#212): its state from its own activity, else its delegation in its parent's record.
|
|
2373
|
+
const child =
|
|
2374
|
+
depth > 0
|
|
2375
|
+
? childStateOf(
|
|
2376
|
+
each,
|
|
2377
|
+
kin.get(each.parent ?? '')?.delegations?.findLast((one) => one.child === each.id),
|
|
2378
|
+
)
|
|
2379
|
+
: undefined;
|
|
2380
|
+
return (
|
|
2381
|
+
<SessionRow
|
|
2382
|
+
key={each.id}
|
|
2383
|
+
each={each}
|
|
2384
|
+
shelf={shelf}
|
|
2385
|
+
status={status}
|
|
2386
|
+
depth={depth}
|
|
2387
|
+
kids={kids}
|
|
2388
|
+
kidsOpen={kidsOpen}
|
|
2389
|
+
agents={agents}
|
|
2390
|
+
{...(child && {
|
|
2391
|
+
childState: child.state,
|
|
2392
|
+
childSince: child.since,
|
|
2393
|
+
childEnded: child.ended,
|
|
2394
|
+
childDetail: child.detail,
|
|
2395
|
+
agent: harnessLabel(each.harness),
|
|
2396
|
+
})}
|
|
2397
|
+
faded={!child && shelf === 'active' && status === 'idle' && each.id !== current}
|
|
2398
|
+
stamp={
|
|
2399
|
+
each.snoozedUntil
|
|
2400
|
+
? snoozeWakeLabel(each.snoozedUntil)
|
|
2401
|
+
: ago(each.updatedAt ?? each.createdAt)
|
|
2402
|
+
}
|
|
2403
|
+
worktreeName={
|
|
2404
|
+
// A child names its worktree in every list; a top row in the workspace's own (research-045).
|
|
2405
|
+
each.worktree && (worktree === undefined || child)
|
|
2406
|
+
? (worktreeNames.get(each.worktree.slug) ?? each.worktree.slug)
|
|
2407
|
+
: // The branch of its project it works on (research-083).
|
|
2408
|
+
each.branch?.name
|
|
2409
|
+
}
|
|
2410
|
+
modelLabel={modelLabelOf(each).replace(' (default)', '')}
|
|
2411
|
+
provider={providerOf(harnessLabel(each.harness), each.model)}
|
|
2412
|
+
nodeTitle={each.node !== node ? titleOf(each.node) : undefined}
|
|
2413
|
+
tagged={tagged}
|
|
2414
|
+
actions={rowActions}
|
|
2415
|
+
/>
|
|
2416
|
+
);
|
|
2417
|
+
};
|
|
2418
|
+
const list = (shelf: Shelf, title: string, rows: React.ReactNode) => (
|
|
2419
|
+
<List
|
|
2420
|
+
aria-label={title}
|
|
2421
|
+
selectionMode={shelf === 'archived' ? 'none' : 'single'}
|
|
2422
|
+
selectedKey={current && shownSub !== undefined ? subagentItem(current, shownSub) : current}
|
|
2423
|
+
onSelectionChange={([key]) => choose(key)}
|
|
2424
|
+
>
|
|
2425
|
+
{rows}
|
|
2426
|
+
</List>
|
|
2427
|
+
);
|
|
2428
|
+
/** A shelf: its label (with its count) opens and closes it; none when empty. */
|
|
2429
|
+
const shelf = (key: Shelf, title: string, sessions: Session[], body: React.ReactNode) =>
|
|
2430
|
+
sessions.length > 0 && (
|
|
2431
|
+
<SidebarGroup
|
|
2432
|
+
key={key}
|
|
2433
|
+
data-shelf={key}
|
|
2434
|
+
expanded={isOpen(key)}
|
|
2435
|
+
onExpandedChange={(open) => setOpen(key, open)}
|
|
2436
|
+
>
|
|
2437
|
+
<SidebarGroupTrigger
|
|
2438
|
+
variant="section"
|
|
2439
|
+
trailing={<Badge>{sessions.length}</Badge>}
|
|
2440
|
+
className="px-4 pb-1.5"
|
|
2441
|
+
>
|
|
2442
|
+
{title}
|
|
2443
|
+
</SidebarGroupTrigger>
|
|
2444
|
+
<SidebarGroupContent className="gap-0">{body}</SidebarGroupContent>
|
|
2445
|
+
</SidebarGroup>
|
|
2446
|
+
);
|
|
2447
|
+
|
|
2448
|
+
/** The context the agent last reported using: the running turn's, else the last turn's. */
|
|
2449
|
+
const usage =
|
|
2450
|
+
live?.usage ?? events.filter((each) => each.type === 'turn/end' && each.usage).at(-1)?.usage;
|
|
2451
|
+
const contextShare =
|
|
2452
|
+
usage?.used !== undefined && usage.size ? Math.min(1, usage.used / usage.size) : undefined;
|
|
2453
|
+
const kilo = (tokens: number) =>
|
|
2454
|
+
tokens >= 10_000 ? `${Math.round(tokens / 1000)}k` : formatNumber(tokens);
|
|
2455
|
+
const { open: askedLines } = decisionsOf(events, live, left);
|
|
2456
|
+
// The running turn begun on an older page (none of its lines' start here): a decision asked there is open in the
|
|
2457
|
+
// live part only, or in the session's decisions a cut left open, docked from there until its line loads.
|
|
2458
|
+
const openDecisions =
|
|
2459
|
+
more && !events.some((each) => each.type === 'turn/start')
|
|
2460
|
+
? [
|
|
2461
|
+
...askedLines,
|
|
2462
|
+
...[...(live?.decisions ?? []), ...(session?.openDecisions ?? [])]
|
|
2463
|
+
.filter((each) => !askedLines.some((line) => line.id === each.id))
|
|
2464
|
+
.map((each, i): Stored => ({ ...each, type: 'decision/asked', seq: -1 - i })),
|
|
2465
|
+
]
|
|
2466
|
+
: askedLines;
|
|
2467
|
+
/** The shown session's folder (its branch's, research-083): paths inside it show relative. */
|
|
2468
|
+
const sessionFolder =
|
|
2469
|
+
session && session.node !== node
|
|
2470
|
+
? undefined
|
|
2471
|
+
: (session?.branch?.folder ?? listing.project?.path ?? listing.workspace?.folder);
|
|
2472
|
+
const relative = (value: string) =>
|
|
2473
|
+
sessionFolder ? value.split(`${sessionFolder.replace(/[\\/]$/, '')}/`).join('') : value;
|
|
2474
|
+
// A decision whose row is in view is answered there; the dock takes the first other one.
|
|
2475
|
+
const waiting = openDecisions.filter((each) => onRows[each.seq] !== true);
|
|
2476
|
+
const docked = waiting[0];
|
|
2477
|
+
/** The usage ring, beside Send (the page's `accessory`). */
|
|
2478
|
+
const usageRing =
|
|
2479
|
+
current && contextShare !== undefined ? (
|
|
2480
|
+
<Tooltip>
|
|
2481
|
+
<TooltipTrigger
|
|
2482
|
+
render={
|
|
2483
|
+
<span
|
|
2484
|
+
role="img"
|
|
2485
|
+
aria-label={`Context ${Math.round(contextShare * 100)}% used`}
|
|
2486
|
+
className="inline-flex h-8 items-center gap-1.5 px-1 text-[11px] tabular-nums text-muted"
|
|
2487
|
+
/>
|
|
2488
|
+
}
|
|
2489
|
+
>
|
|
2490
|
+
<span
|
|
2491
|
+
className="relative size-4 rounded-full"
|
|
2492
|
+
style={{
|
|
2493
|
+
background: `conic-gradient(var(--${contextShare > 0.85 ? 'warning' : 'fg-muted'}) ${contextShare * 360}deg, var(--bg-active) 0deg)`,
|
|
2494
|
+
}}
|
|
2495
|
+
>
|
|
2496
|
+
<span className="absolute inset-[2.5px] rounded-full bg-surface" />
|
|
2497
|
+
</span>
|
|
2498
|
+
{Math.round(contextShare * 100)}%
|
|
2499
|
+
</TooltipTrigger>
|
|
2500
|
+
<TooltipContent>{`Context: ${Math.round(contextShare * 100)}% used · ${kilo(usage!.used!)} of ${kilo(usage!.size!)} tokens`}</TooltipContent>
|
|
2501
|
+
</Tooltip>
|
|
2502
|
+
) : null;
|
|
2503
|
+
/** The running turn's plan, the queued messages and an open decision, drawn inside the prompt above the field. */
|
|
2504
|
+
const above = (
|
|
2505
|
+
<>
|
|
2506
|
+
{/* Its running children (bridge#212), with Open and Stop each. */}
|
|
2507
|
+
{current && <BackgroundWork />}
|
|
2508
|
+
{current && live && live.plan.length > 0 && <Tasks plan={live.plan} />}
|
|
2509
|
+
{session && (
|
|
2510
|
+
<Queued
|
|
2511
|
+
items={session.queue ?? []}
|
|
2512
|
+
notes={notesOf(session)}
|
|
2513
|
+
steers={
|
|
2514
|
+
!!listing?.harnesses.find((each) => each.name === session.harness)?.capabilities?.steer
|
|
2515
|
+
}
|
|
2516
|
+
onSend={(item) =>
|
|
2517
|
+
void post(`/queue/send?id=${encodeURIComponent(session.id)}`, { item }).then(
|
|
2518
|
+
load,
|
|
2519
|
+
report,
|
|
2520
|
+
)
|
|
2521
|
+
}
|
|
2522
|
+
onEdit={(item) => {
|
|
2523
|
+
// Back to the draft of the session it was clicked in, added to what is written there, through the prompt
|
|
2524
|
+
// of the click: the one shown when the queue answers may be another (or none).
|
|
2525
|
+
const id = session.id;
|
|
2526
|
+
const back = session.queue?.find((each) => each.id === item.id)?.attachments ?? [];
|
|
2527
|
+
const prompt = promptHandle.current;
|
|
2528
|
+
void post(`/queue/remove?id=${encodeURIComponent(id)}`, { item: item.id }).then(() => {
|
|
2529
|
+
prompt?.addText(id, item.text, back);
|
|
2530
|
+
promptHandle.current?.focus();
|
|
2531
|
+
return load();
|
|
2532
|
+
}, report);
|
|
2533
|
+
}}
|
|
2534
|
+
onRemove={(item) =>
|
|
2535
|
+
void post(`/queue/remove?id=${encodeURIComponent(session.id)}`, { item }).then(
|
|
2536
|
+
load,
|
|
2537
|
+
report,
|
|
2538
|
+
)
|
|
2539
|
+
}
|
|
2540
|
+
/>
|
|
2541
|
+
)}
|
|
2542
|
+
{docked && (
|
|
2543
|
+
<DecisionDock
|
|
2544
|
+
key={docked.seq}
|
|
2545
|
+
event={docked}
|
|
2546
|
+
relative={relative}
|
|
2547
|
+
busy={!!answering[`${current}:${docked.seq}`]}
|
|
2548
|
+
more={waiting.length - 1}
|
|
2549
|
+
onRow={onRows[docked.seq] === false}
|
|
2550
|
+
onAnswer={(option) => void answer(docked.seq, docked.id!, option)}
|
|
2551
|
+
/>
|
|
2552
|
+
)}
|
|
2553
|
+
</>
|
|
2554
|
+
);
|
|
2555
|
+
/** Reconnecting and the usage ring, beside Send. */
|
|
2556
|
+
const accessory = (
|
|
2557
|
+
<>
|
|
2558
|
+
{!current && branchOf.branches && branchOf.chosen !== undefined && (
|
|
2559
|
+
<Select
|
|
2560
|
+
items={{
|
|
2561
|
+
[NEW_BRANCH]: 'New branch',
|
|
2562
|
+
...Object.fromEntries(branchOf.branches.map((each) => [each.name, each.name])),
|
|
2563
|
+
}}
|
|
2564
|
+
value={branchOf.chosen}
|
|
2565
|
+
onValueChange={(value) => branchOf.choose(value as string)}
|
|
2566
|
+
>
|
|
2567
|
+
<SelectTrigger
|
|
2568
|
+
size="sm"
|
|
2569
|
+
aria-label="The branch the new session works on"
|
|
2570
|
+
title="The branch the new session works on"
|
|
2571
|
+
className="w-auto max-w-44 shrink-0 gap-1 rounded-full text-xs"
|
|
2572
|
+
data-branch-picker
|
|
2573
|
+
>
|
|
2574
|
+
<Icon name={branchOf.newBranch ? 'plus' : 'git-branch'} size={12} />
|
|
2575
|
+
<span className={cn('truncate', !branchOf.newBranch && 'font-mono')}>
|
|
2576
|
+
{branchOf.newBranch ? 'New branch' : branchOf.chosen}
|
|
2577
|
+
</span>
|
|
2578
|
+
</SelectTrigger>
|
|
2579
|
+
<SelectContent className="w-60">
|
|
2580
|
+
{/* Made at the first send, from the project's default branch, named by the first message. */}
|
|
2581
|
+
<SelectItem value={NEW_BRANCH}>
|
|
2582
|
+
<span className="flex min-w-0 items-center gap-1.5">
|
|
2583
|
+
<Icon name="plus" size={12} />
|
|
2584
|
+
<span className="truncate">New branch</span>
|
|
2585
|
+
</span>
|
|
2586
|
+
</SelectItem>
|
|
2587
|
+
{branchOf.branches.map((each) => (
|
|
2588
|
+
<SelectItem key={each.name} value={each.name}>
|
|
2589
|
+
<span className="flex min-w-0 items-center gap-1.5">
|
|
2590
|
+
<Icon name="git-branch" size={12} />
|
|
2591
|
+
<span className="truncate font-mono">{each.name}</span>
|
|
2592
|
+
{each.active && <span className="text-xs text-accent">active</span>}
|
|
2593
|
+
</span>
|
|
2594
|
+
</SelectItem>
|
|
2595
|
+
))}
|
|
2596
|
+
</SelectContent>
|
|
2597
|
+
</Select>
|
|
2598
|
+
)}
|
|
2599
|
+
{/* Fixed after its first message (research-083): read-only, `branch_handoff` alone moves it. */}
|
|
2600
|
+
{current && branchOf.branches && (
|
|
2601
|
+
<span
|
|
2602
|
+
data-branch-fixed
|
|
2603
|
+
title="The branch this session works on: fixed after its first message"
|
|
2604
|
+
className="inline-flex max-w-44 shrink-0 items-center gap-1 text-xs text-muted"
|
|
2605
|
+
>
|
|
2606
|
+
<Icon name="git-branch" size={12} />
|
|
2607
|
+
<span className="truncate font-mono">
|
|
2608
|
+
{session?.branch?.name ?? branchOf.branches.find((each) => each.main)?.name}
|
|
2609
|
+
</span>
|
|
2610
|
+
</span>
|
|
2611
|
+
)}
|
|
2612
|
+
{current && !synced && (
|
|
2613
|
+
<p role="status" className="text-xs text-muted">
|
|
2614
|
+
Reconnecting…
|
|
2615
|
+
</p>
|
|
2616
|
+
)}
|
|
2617
|
+
{usageRing}
|
|
2618
|
+
</>
|
|
2619
|
+
);
|
|
2620
|
+
/** After the prompt created the open session: listed (added by hand if the listing failed), then shown. */
|
|
2621
|
+
const onCreated = (id: string) => {
|
|
2622
|
+
void load().then((ok) => {
|
|
2623
|
+
if (!ok)
|
|
2624
|
+
setListing((was) =>
|
|
2625
|
+
was.sessions.some((each) => each.id === id)
|
|
2626
|
+
? was
|
|
2627
|
+
: {
|
|
2628
|
+
...was,
|
|
2629
|
+
sessions: [
|
|
2630
|
+
{ id, node, title: 'New chat', harness: '', createdAt: new Date().toISOString() },
|
|
2631
|
+
...was.sessions,
|
|
2632
|
+
],
|
|
2633
|
+
},
|
|
2634
|
+
);
|
|
2635
|
+
choose(id);
|
|
2636
|
+
// A new branch made by this send: listed for the next new chat.
|
|
2637
|
+
if (branchOf.newBranch) branchOf.reload();
|
|
2638
|
+
});
|
|
2639
|
+
};
|
|
2640
|
+
const prompt = (
|
|
2641
|
+
<div className="relative w-full shrink-0 pb-4">
|
|
2642
|
+
{error && (
|
|
2643
|
+
<p role="alert" className="mb-2 px-1 text-xs text-destructive">
|
|
2644
|
+
{error}
|
|
2645
|
+
</p>
|
|
2646
|
+
)}
|
|
2647
|
+
<SessionPrompt
|
|
2648
|
+
ctx={ctx}
|
|
2649
|
+
use={promptChoice}
|
|
2650
|
+
at={{ slug: workspace.slug, node }}
|
|
2651
|
+
{...((shownWorktree(workspace) !== undefined ||
|
|
2652
|
+
branchOf.onBranch !== undefined ||
|
|
2653
|
+
branchOf.newBranch) && {
|
|
2654
|
+
create: {
|
|
2655
|
+
...(shownWorktree(workspace) !== undefined && { worktree: shownWorktree(workspace)! }),
|
|
2656
|
+
...(branchOf.onBranch !== undefined && { branch: branchOf.onBranch }),
|
|
2657
|
+
...(branchOf.newBranch && { newBranch: true as const }),
|
|
2658
|
+
},
|
|
2659
|
+
})}
|
|
2660
|
+
session={current}
|
|
2661
|
+
onCreated={onCreated}
|
|
2662
|
+
onNewChat={newChat}
|
|
2663
|
+
running={running}
|
|
2664
|
+
above={above}
|
|
2665
|
+
accessory={accessory}
|
|
2666
|
+
handle={promptHandle}
|
|
2667
|
+
/>
|
|
2668
|
+
</div>
|
|
2669
|
+
);
|
|
2670
|
+
|
|
2671
|
+
/** The button showing or hiding the sessions list, in the pane's header. */
|
|
2672
|
+
const listToggle = (
|
|
2673
|
+
<Tooltip>
|
|
2674
|
+
<TooltipTrigger
|
|
2675
|
+
render={
|
|
2676
|
+
<Button
|
|
2677
|
+
size="icon"
|
|
2678
|
+
aria-label={listShown ? 'Hide sessions list' : 'Show sessions list'}
|
|
2679
|
+
aria-expanded={listShown}
|
|
2680
|
+
className="-ml-2 shrink-0"
|
|
2681
|
+
onClick={toggleList}
|
|
2682
|
+
/>
|
|
2683
|
+
}
|
|
2684
|
+
>
|
|
2685
|
+
<Icon name={listShown ? 'panel-left-close' : 'panel-left-open'} />
|
|
2686
|
+
</TooltipTrigger>
|
|
2687
|
+
<TooltipContent>{listShown ? 'Hide sessions list' : 'Show sessions list'}</TooltipContent>
|
|
2688
|
+
</Tooltip>
|
|
2689
|
+
);
|
|
2690
|
+
|
|
2691
|
+
return (
|
|
2692
|
+
<section
|
|
2693
|
+
aria-label="Screen"
|
|
2694
|
+
className={cn('flex min-h-0 flex-1 overflow-hidden', reset)}
|
|
2695
|
+
onTransitionEnd={(event) => {
|
|
2696
|
+
// The slide ended: shown, the list follows its pane's width again (a drag, a window resized).
|
|
2697
|
+
if (event.propertyName === 'flex-grow' && listShown) setListFrozen(undefined);
|
|
2698
|
+
}}
|
|
2699
|
+
>
|
|
2700
|
+
<SplitView
|
|
2701
|
+
orientation="horizontal"
|
|
2702
|
+
storageKey="sessions/browser/list@v1"
|
|
2703
|
+
// The list's pane (the library sizes it with flex-grow): closed to nothing while hidden, its share kept for
|
|
2704
|
+
// when it comes back; the width and the content's opacity slide only on a toggle, never during a drag.
|
|
2705
|
+
className={cn(
|
|
2706
|
+
!listShown && '[&>[data-slot=resizable-panel-group]>#sessions]:grow-0!',
|
|
2707
|
+
listFrozen !== undefined &&
|
|
2708
|
+
'[&>[data-slot=resizable-panel-group]>#sessions]:transition-[flex-grow] [&>[data-slot=resizable-panel-group]>#sessions]:duration-200 [&>[data-slot=resizable-panel-group]>#sessions]:ease-out',
|
|
2709
|
+
)}
|
|
2710
|
+
>
|
|
2711
|
+
<SplitPane id="sessions" defaultSize="280px" minSize="220px" maxSize="480px">
|
|
2712
|
+
{/* Hidden, the list is out of the tab order and the accessibility tree; anchored right, it slides left. */}
|
|
2713
|
+
<div
|
|
2714
|
+
ref={listContent}
|
|
2715
|
+
inert={!listShown}
|
|
2716
|
+
aria-hidden={listShown ? undefined : true}
|
|
2717
|
+
className={cn(
|
|
2718
|
+
'flex h-full justify-end',
|
|
2719
|
+
!listShown && 'opacity-0',
|
|
2720
|
+
listFrozen !== undefined && 'transition-opacity duration-200 ease-out',
|
|
2721
|
+
)}
|
|
2722
|
+
>
|
|
2723
|
+
<Pane
|
|
2724
|
+
className="w-full shrink-0 bg-background"
|
|
2725
|
+
style={listFrozen === undefined ? undefined : { width: listFrozen }}
|
|
2726
|
+
>
|
|
2727
|
+
<PaneHeader className="h-11 shrink-0 border-b border-border py-0 pl-4 pr-2">
|
|
2728
|
+
<h1 className="text-sm font-semibold">Sessions</h1>
|
|
2729
|
+
<Tooltip>
|
|
2730
|
+
<TooltipTrigger
|
|
2731
|
+
render={<Button size="icon" aria-label="New chat" onClick={newChat} />}
|
|
2732
|
+
>
|
|
2733
|
+
<Icon name="square-pen" />
|
|
2734
|
+
</TooltipTrigger>
|
|
2735
|
+
<TooltipContent>New chat</TooltipContent>
|
|
2736
|
+
</Tooltip>
|
|
2737
|
+
</PaneHeader>
|
|
2738
|
+
<PaneBody viewportClassName={fitWidth}>
|
|
2739
|
+
<nav aria-label="Sessions">
|
|
2740
|
+
{tagged && (
|
|
2741
|
+
<div className="flex items-center gap-1.5 px-4 pb-1 pt-2 text-xs text-secondary">
|
|
2742
|
+
<Icon name="tag" size={12} />
|
|
2743
|
+
Tagged <span className="font-medium text-foreground">#{tagged}</span>
|
|
2744
|
+
<Button
|
|
2745
|
+
size="icon-sm"
|
|
2746
|
+
aria-label="Show every session"
|
|
2747
|
+
className="ml-auto"
|
|
2748
|
+
onClick={() => setTagged(undefined)}
|
|
2749
|
+
>
|
|
2750
|
+
<Icon name="x" size={12} />
|
|
2751
|
+
</Button>
|
|
2752
|
+
</div>
|
|
2753
|
+
)}
|
|
2754
|
+
{listing.sessions.length === 0 && !archived && (
|
|
2755
|
+
<EmptyState>No sessions yet. Start one from the composer.</EmptyState>
|
|
2756
|
+
)}
|
|
2757
|
+
{shelf(
|
|
2758
|
+
'pinned',
|
|
2759
|
+
'Pinned',
|
|
2760
|
+
pinned,
|
|
2761
|
+
<SortableList
|
|
2762
|
+
id="pinned"
|
|
2763
|
+
items={pinned.map((each) => each.id)}
|
|
2764
|
+
onReorder={reorder}
|
|
2765
|
+
itemLabel={(id) => pinned.find((each) => each.id === id)?.title ?? id}
|
|
2766
|
+
className="min-h-0"
|
|
2767
|
+
>
|
|
2768
|
+
{list(
|
|
2769
|
+
'pinned',
|
|
2770
|
+
'Pinned',
|
|
2771
|
+
pinned.map((each) => (
|
|
2772
|
+
<SortableItem key={each.id} id={each.id} dragFullItem="pointer">
|
|
2773
|
+
{rows(each, 'pinned')}
|
|
2774
|
+
</SortableItem>
|
|
2775
|
+
)),
|
|
2776
|
+
)}
|
|
2777
|
+
</SortableList>,
|
|
2778
|
+
)}
|
|
2779
|
+
{shelf(
|
|
2780
|
+
'active',
|
|
2781
|
+
'Active',
|
|
2782
|
+
active,
|
|
2783
|
+
list(
|
|
2784
|
+
'active',
|
|
2785
|
+
'Active',
|
|
2786
|
+
active.flatMap((each) => rows(each, 'active')),
|
|
2787
|
+
),
|
|
2788
|
+
)}
|
|
2789
|
+
{shelf(
|
|
2790
|
+
'working',
|
|
2791
|
+
'Working',
|
|
2792
|
+
working,
|
|
2793
|
+
list(
|
|
2794
|
+
'working',
|
|
2795
|
+
'Working',
|
|
2796
|
+
working.flatMap((each) => rows(each, 'working')),
|
|
2797
|
+
),
|
|
2798
|
+
)}
|
|
2799
|
+
{shelf(
|
|
2800
|
+
'snoozed',
|
|
2801
|
+
'Snoozed',
|
|
2802
|
+
snoozed,
|
|
2803
|
+
list(
|
|
2804
|
+
'snoozed',
|
|
2805
|
+
'Snoozed',
|
|
2806
|
+
snoozed.flatMap((each) => rows(each, 'snoozed')),
|
|
2807
|
+
),
|
|
2808
|
+
)}
|
|
2809
|
+
{shelf(
|
|
2810
|
+
'settled',
|
|
2811
|
+
'Settled',
|
|
2812
|
+
settled,
|
|
2813
|
+
<>
|
|
2814
|
+
{list(
|
|
2815
|
+
'settled',
|
|
2816
|
+
'Settled',
|
|
2817
|
+
settledShown.flatMap((each) => rows(each, 'settled')),
|
|
2818
|
+
)}
|
|
2819
|
+
{settled.length > settledShown.length && (
|
|
2820
|
+
<Button
|
|
2821
|
+
size="sm"
|
|
2822
|
+
className="mx-2 my-1 justify-start text-muted"
|
|
2823
|
+
onClick={() => setAllSettled(true)}
|
|
2824
|
+
>
|
|
2825
|
+
Show {settled.length - settledShown.length} more
|
|
2826
|
+
</Button>
|
|
2827
|
+
)}
|
|
2828
|
+
</>,
|
|
2829
|
+
)}
|
|
2830
|
+
{archivedShown &&
|
|
2831
|
+
shelf(
|
|
2832
|
+
'archived',
|
|
2833
|
+
'Archived',
|
|
2834
|
+
archivedShown,
|
|
2835
|
+
list(
|
|
2836
|
+
'archived',
|
|
2837
|
+
'Archived',
|
|
2838
|
+
archivedShown.map((each) => row(each, 'archived')),
|
|
2839
|
+
),
|
|
2840
|
+
)}
|
|
2841
|
+
{archivedShown?.length === 0 && (
|
|
2842
|
+
<p className="px-4 py-3 text-xs text-muted">No archived sessions.</p>
|
|
2843
|
+
)}
|
|
2844
|
+
<div className="px-2 pb-3 pt-2">
|
|
2845
|
+
<Button
|
|
2846
|
+
size="sm"
|
|
2847
|
+
className="justify-start text-muted"
|
|
2848
|
+
onClick={() => (archived ? setArchived(undefined) : void loadArchived())}
|
|
2849
|
+
>
|
|
2850
|
+
<Icon name="archive" size={14} />
|
|
2851
|
+
{archived ? 'Hide archived' : 'Show archived'}
|
|
2852
|
+
</Button>
|
|
2853
|
+
</div>
|
|
2854
|
+
</nav>
|
|
2855
|
+
</PaneBody>
|
|
2856
|
+
</Pane>
|
|
2857
|
+
</div>
|
|
2858
|
+
</SplitPane>
|
|
2859
|
+
<SplitHandle {...(!listShown && { className: 'hidden' })} />
|
|
2860
|
+
<SplitPane id="session" minSize="360px">
|
|
2861
|
+
<LineageContext.Provider value={lineageOf ?? null}>
|
|
2862
|
+
{session && shownSub !== undefined ? (
|
|
2863
|
+
// A sub-agent's view (bridge#212 S8): read only, from its session's stream.
|
|
2864
|
+
<SubagentView
|
|
2865
|
+
key={subagentItem(session.id, shownSub)}
|
|
2866
|
+
session={session}
|
|
2867
|
+
sub={shownSub}
|
|
2868
|
+
entry={session.subagents?.find((each) => each.id === shownSub)}
|
|
2869
|
+
events={events}
|
|
2870
|
+
live={live}
|
|
2871
|
+
sessionRunning={running || !!session.running}
|
|
2872
|
+
load={() =>
|
|
2873
|
+
workspace
|
|
2874
|
+
.fetch(
|
|
2875
|
+
`${api}/events?id=${encodeURIComponent(session.id)}&subagent=${encodeURIComponent(shownSub)}`,
|
|
2876
|
+
{ headers },
|
|
2877
|
+
)
|
|
2878
|
+
.then(readJson)
|
|
2879
|
+
.then((body) => (body as { events: Stored[] }).events)
|
|
2880
|
+
}
|
|
2881
|
+
folder={sessionFolder}
|
|
2882
|
+
workspace={workspace.slug}
|
|
2883
|
+
listToggle={listToggle}
|
|
2884
|
+
{...(session.subagents?.find((each) => each.id === shownSub)?.stoppable && {
|
|
2885
|
+
onStop: () => stopSub.current(shownSub),
|
|
2886
|
+
})}
|
|
2887
|
+
/>
|
|
2888
|
+
) : (
|
|
2889
|
+
<div className="flex h-full min-w-0 flex-col">
|
|
2890
|
+
<header className="flex h-11 shrink-0 items-center gap-2 border-b border-border px-4">
|
|
2891
|
+
{listToggle}
|
|
2892
|
+
{withTabs(
|
|
2893
|
+
session ? (
|
|
2894
|
+
// The title, renamed in place: Enter or leaving the field keeps it, Escape restores it.
|
|
2895
|
+
<Input
|
|
2896
|
+
ref={titleField}
|
|
2897
|
+
size="sm"
|
|
2898
|
+
aria-label="Session title"
|
|
2899
|
+
value={draft ?? session.title}
|
|
2900
|
+
className="w-auto min-w-16 max-w-[480px] truncate font-semibold field-sizing-content"
|
|
2901
|
+
onChange={(event) => setDraft(event.currentTarget.value)}
|
|
2902
|
+
onBlur={rename}
|
|
2903
|
+
onKeyDown={(event) => {
|
|
2904
|
+
if (event.key === 'Enter' && !isComposing(event))
|
|
2905
|
+
event.currentTarget.blur();
|
|
2906
|
+
else if (event.key === 'Escape') {
|
|
2907
|
+
setDraft(undefined);
|
|
2908
|
+
requestAnimationFrame(() => titleField.current?.blur());
|
|
2909
|
+
}
|
|
2910
|
+
}}
|
|
2911
|
+
/>
|
|
2912
|
+
) : (
|
|
2913
|
+
<h2 className="min-w-0 truncate text-sm font-semibold">
|
|
2914
|
+
{current ? '' : 'New chat'}
|
|
2915
|
+
</h2>
|
|
2916
|
+
),
|
|
2917
|
+
)}
|
|
2918
|
+
{/* A child session's parent (bridge#212): a link to it. */}
|
|
2919
|
+
{session?.parent !== undefined && <ParentLink parent={session.parent} />}
|
|
2920
|
+
{/* Where it works (research-083): its stage, by the branches it runs. */}
|
|
2921
|
+
{session?.worktree && (
|
|
2922
|
+
<span
|
|
2923
|
+
title={`Works in the stage ${worktreeNames.get(session.worktree.slug) ?? session.worktree.slug}`}
|
|
2924
|
+
className="inline-flex max-w-48 shrink-0 items-center gap-1 text-xs text-muted"
|
|
2925
|
+
>
|
|
2926
|
+
<Icon name="git-branch" size={13} />
|
|
2927
|
+
<span className="truncate">
|
|
2928
|
+
{worktreeNames.get(session.worktree.slug) ?? session.worktree.slug}
|
|
2929
|
+
</span>
|
|
2930
|
+
</span>
|
|
2931
|
+
)}
|
|
2932
|
+
{session?.pinnedAt && (
|
|
2933
|
+
<span
|
|
2934
|
+
role="img"
|
|
2935
|
+
aria-label="Pinned"
|
|
2936
|
+
className="inline-flex shrink-0 text-muted"
|
|
2937
|
+
>
|
|
2938
|
+
<Icon name="pin" size={13} />
|
|
2939
|
+
</span>
|
|
2940
|
+
)}
|
|
2941
|
+
{session?.settledAt && !session.pinnedAt && (
|
|
2942
|
+
<Badge className="shrink-0">Settled</Badge>
|
|
2943
|
+
)}
|
|
2944
|
+
{session?.snoozedUntil && (
|
|
2945
|
+
<Badge className="shrink-0 gap-1" title={`Wakes ${when(session.snoozedUntil)}`}>
|
|
2946
|
+
<Icon name="alarm-clock" size={12} />
|
|
2947
|
+
Snoozed · {snoozeWakeLabel(session.snoozedUntil)}
|
|
2948
|
+
</Badge>
|
|
2949
|
+
)}
|
|
2950
|
+
{session && <SessionScheduled ctx={ctx} node={node} session={session} />}
|
|
2951
|
+
<span className="flex-1" />
|
|
2952
|
+
{/* Its branch's git actions, on its folder (research-083). */}
|
|
2953
|
+
{/* Its pull request (research-086), beside the Git button. */}
|
|
2954
|
+
{session?.pullRequest && (
|
|
2955
|
+
<PullRequestPill
|
|
2956
|
+
pullRequest={session.pullRequest}
|
|
2957
|
+
act={(action) =>
|
|
2958
|
+
void post(`/pull-request/${action}`, { id: session.id })
|
|
2959
|
+
.then(() => load())
|
|
2960
|
+
.catch(report)
|
|
2961
|
+
}
|
|
2962
|
+
/>
|
|
2963
|
+
)}
|
|
2964
|
+
{session?.branch && !session.branch.temporary && (
|
|
2965
|
+
<BranchActions
|
|
2966
|
+
slug={branchesAt}
|
|
2967
|
+
node={session.node}
|
|
2968
|
+
branch={session.branch.name}
|
|
2969
|
+
session={session.id}
|
|
2970
|
+
refresh={running}
|
|
2971
|
+
// Its own pull request only when it is this branch's: an agent may link another.
|
|
2972
|
+
pullRequestOpen={
|
|
2973
|
+
session.pullRequest?.headBranch === session.branch.name &&
|
|
2974
|
+
(session.pullRequest.state === 'open' ||
|
|
2975
|
+
session.pullRequest.state === 'draft')
|
|
2976
|
+
}
|
|
2977
|
+
// A Git action done (Create PR among them): its pull request is found or read again.
|
|
2978
|
+
onGitAnswer={() =>
|
|
2979
|
+
void post('/pull-request/refresh', { id: session.id }).catch(report)
|
|
2980
|
+
}
|
|
2981
|
+
/>
|
|
2982
|
+
)}
|
|
2983
|
+
{current &&
|
|
2984
|
+
(running ? (
|
|
2985
|
+
<>
|
|
2986
|
+
{openDecisions.length > 0 ? (
|
|
2987
|
+
<span className="flex items-center gap-1.5 text-xs font-medium text-warning-text">
|
|
2988
|
+
<span className="size-1.5 rounded-full bg-warning" />
|
|
2989
|
+
Waiting for you
|
|
2990
|
+
</span>
|
|
2991
|
+
) : (
|
|
2992
|
+
<span className="flex items-center gap-1.5 text-xs text-secondary">
|
|
2993
|
+
<Spinner />
|
|
2994
|
+
Running…
|
|
2995
|
+
</span>
|
|
2996
|
+
)}
|
|
2997
|
+
<Button variant="outline" size="sm" onClick={stop}>
|
|
2998
|
+
Stop
|
|
2999
|
+
</Button>
|
|
3000
|
+
</>
|
|
3001
|
+
) : (
|
|
3002
|
+
<span className="text-xs text-muted">Idle</span>
|
|
3003
|
+
))}
|
|
3004
|
+
{session && (
|
|
3005
|
+
<Tooltip>
|
|
3006
|
+
<TooltipTrigger
|
|
3007
|
+
render={
|
|
3008
|
+
<Button
|
|
3009
|
+
size="sm"
|
|
3010
|
+
aria-label="Tags and changes"
|
|
3011
|
+
aria-pressed={details}
|
|
3012
|
+
className={cn('gap-1', details && 'bg-border/60')}
|
|
3013
|
+
onClick={toggleDetails}
|
|
3014
|
+
/>
|
|
3015
|
+
}
|
|
3016
|
+
>
|
|
3017
|
+
<Icon name="git-branch" size={14} />
|
|
3018
|
+
{session.files?.length ? session.files.length : null}
|
|
3019
|
+
</TooltipTrigger>
|
|
3020
|
+
<TooltipContent>Tags and changes</TooltipContent>
|
|
3021
|
+
</Tooltip>
|
|
3022
|
+
)}
|
|
3023
|
+
{session && (
|
|
3024
|
+
<Tooltip>
|
|
3025
|
+
<TooltipTrigger
|
|
3026
|
+
render={
|
|
3027
|
+
<Button
|
|
3028
|
+
size="sm"
|
|
3029
|
+
aria-label="Gallery"
|
|
3030
|
+
className="gap-1"
|
|
3031
|
+
onClick={() => setGalleryOpen(true)}
|
|
3032
|
+
/>
|
|
3033
|
+
}
|
|
3034
|
+
>
|
|
3035
|
+
<Icon name="image" size={14} />
|
|
3036
|
+
{pins.length ? pins.length : null}
|
|
3037
|
+
</TooltipTrigger>
|
|
3038
|
+
<TooltipContent>Gallery: the pinned images</TooltipContent>
|
|
3039
|
+
</Tooltip>
|
|
3040
|
+
)}
|
|
3041
|
+
{session && (
|
|
3042
|
+
<GalleryDialog
|
|
3043
|
+
open={galleryOpen}
|
|
3044
|
+
onOpenChange={(next) => {
|
|
3045
|
+
setGalleryOpen(next);
|
|
3046
|
+
if (!next && current && sessionNode)
|
|
3047
|
+
void loadPins({
|
|
3048
|
+
key: 'session',
|
|
3049
|
+
label: 'Session',
|
|
3050
|
+
node: sessionNode,
|
|
3051
|
+
session: current,
|
|
3052
|
+
}).then(setPins, () => {});
|
|
3053
|
+
}}
|
|
3054
|
+
workspace={workspace.slug}
|
|
3055
|
+
scopes={[
|
|
3056
|
+
{
|
|
3057
|
+
key: 'session',
|
|
3058
|
+
label: 'Session',
|
|
3059
|
+
node: session.node,
|
|
3060
|
+
session: session.id,
|
|
3061
|
+
},
|
|
3062
|
+
...(session.node !== 'root'
|
|
3063
|
+
? [{ key: 'project' as const, label: 'Project', node: session.node }]
|
|
3064
|
+
: []),
|
|
3065
|
+
{ key: 'workspace', label: 'Workspace', node: 'root' },
|
|
3066
|
+
]}
|
|
3067
|
+
load={loadPins}
|
|
3068
|
+
unpin={(pin) =>
|
|
3069
|
+
unpinImage(pin).catch((cause) => {
|
|
3070
|
+
report(cause);
|
|
3071
|
+
throw cause;
|
|
3072
|
+
})
|
|
3073
|
+
}
|
|
3074
|
+
onOpenSession={(id) => {
|
|
3075
|
+
setTagged(undefined);
|
|
3076
|
+
choose(id);
|
|
3077
|
+
}}
|
|
3078
|
+
titleOf={(id) => listing?.sessions.find((each) => each.id === id)?.title}
|
|
3079
|
+
/>
|
|
3080
|
+
)}
|
|
3081
|
+
{session && (
|
|
3082
|
+
<DropdownMenu>
|
|
3083
|
+
<DropdownMenuTrigger
|
|
3084
|
+
render={<Button size="icon" aria-label="Session actions" />}
|
|
3085
|
+
>
|
|
3086
|
+
<Icon name="ellipsis" />
|
|
3087
|
+
</DropdownMenuTrigger>
|
|
3088
|
+
<DropdownMenuContent align="end">
|
|
3089
|
+
{menuItems(session, true)}
|
|
3090
|
+
</DropdownMenuContent>
|
|
3091
|
+
</DropdownMenu>
|
|
3092
|
+
)}
|
|
3093
|
+
</header>
|
|
3094
|
+
<div className="relative flex min-h-0 flex-1 flex-col">
|
|
3095
|
+
{session && details && (
|
|
3096
|
+
<SessionDetails
|
|
3097
|
+
session={session}
|
|
3098
|
+
known={known}
|
|
3099
|
+
focus={changeAsked?.session === session.id ? changeAsked : undefined}
|
|
3100
|
+
post={post}
|
|
3101
|
+
onChanged={() => void load()}
|
|
3102
|
+
onOpen={(id) => {
|
|
3103
|
+
setTagged(undefined);
|
|
3104
|
+
choose(id);
|
|
3105
|
+
}}
|
|
3106
|
+
/>
|
|
3107
|
+
)}
|
|
3108
|
+
{current && (
|
|
3109
|
+
<>
|
|
3110
|
+
<ScrollArea
|
|
3111
|
+
className="min-h-0 flex-1"
|
|
3112
|
+
// The page keeps the reader's place itself when an earlier page lands above (no browser anchoring).
|
|
3113
|
+
viewportClassName={cn(fitWidth, '[overflow-anchor:none]')}
|
|
3114
|
+
viewportRef={viewport}
|
|
3115
|
+
>
|
|
3116
|
+
<GalleryPins.Provider value={galleryPins}>
|
|
3117
|
+
<div style={{ paddingBottom: dockHeight }}>
|
|
3118
|
+
<Conversation
|
|
3119
|
+
events={events}
|
|
3120
|
+
live={live}
|
|
3121
|
+
left={left}
|
|
3122
|
+
cut={more}
|
|
3123
|
+
folder={sessionFolder}
|
|
3124
|
+
workspace={workspace.slug}
|
|
3125
|
+
viewTools={viewTools}
|
|
3126
|
+
renderComponent={renderComponent}
|
|
3127
|
+
onReuse={(value) => {
|
|
3128
|
+
promptHandle.current?.setText(value);
|
|
3129
|
+
promptHandle.current?.focus();
|
|
3130
|
+
}}
|
|
3131
|
+
onReview={() => {
|
|
3132
|
+
if (!details) toggleDetails();
|
|
3133
|
+
}}
|
|
3134
|
+
onAnswer={(event, option) => answer(event.seq, event.id!, option)}
|
|
3135
|
+
onDecisionShown={onDecisionShown}
|
|
3136
|
+
onOpenChange={(path) => {
|
|
3137
|
+
setChangeAsked({ session: current, path, at: Date.now() });
|
|
3138
|
+
if (!details) toggleDetails();
|
|
3139
|
+
}}
|
|
3140
|
+
lineage={lineageOf}
|
|
3141
|
+
/>
|
|
3142
|
+
</div>
|
|
3143
|
+
</GalleryPins.Provider>
|
|
3144
|
+
</ScrollArea>
|
|
3145
|
+
{/* Over the conversation's bottom, which scrolls on under it: its margins let the pointer through to the
|
|
3146
|
+
conversation (a wheel, a selection), only the prompt and its controls take it. */}
|
|
3147
|
+
{away && (
|
|
3148
|
+
<Button
|
|
3149
|
+
size="sm"
|
|
3150
|
+
variant="outline"
|
|
3151
|
+
className="absolute left-1/2 z-10 -translate-x-1/2 gap-1.5 rounded-full bg-surface/90 shadow-md backdrop-blur"
|
|
3152
|
+
style={{ bottom: dockHeight + 12 }}
|
|
3153
|
+
onClick={toEnd}
|
|
3154
|
+
>
|
|
3155
|
+
<Icon name="arrow-down" size={14} />
|
|
3156
|
+
Scroll to end
|
|
3157
|
+
</Button>
|
|
3158
|
+
)}
|
|
3159
|
+
</>
|
|
3160
|
+
)}
|
|
3161
|
+
{/* The prompt's one place, a new chat's (centred) or a session's (docked over the conversation's bottom,
|
|
3162
|
+
at most the pane's height: many questions, a long queue scroll then, over the header never): the
|
|
3163
|
+
first message's session shown, the prompt stays the same one and keeps the focus (bridge#265). */}
|
|
3164
|
+
<div
|
|
3165
|
+
ref={current ? dock : undefined}
|
|
3166
|
+
data-slot={current ? 'prompt-dock' : undefined}
|
|
3167
|
+
className={
|
|
3168
|
+
current
|
|
3169
|
+
? 'pointer-events-none absolute inset-x-0 bottom-0 flex max-h-full flex-col overflow-y-auto [&_[role=alert]]:pointer-events-auto [&_button]:pointer-events-auto [&_form>*]:pointer-events-auto [&_input]:pointer-events-auto [&_textarea]:pointer-events-auto'
|
|
3170
|
+
: 'flex min-h-0 flex-1 flex-col items-center justify-center gap-6 pb-[10vh]'
|
|
3171
|
+
}
|
|
3172
|
+
>
|
|
3173
|
+
{current ? (
|
|
3174
|
+
session && (
|
|
3175
|
+
<Questions
|
|
3176
|
+
questions={session.questions ?? []}
|
|
3177
|
+
running={running}
|
|
3178
|
+
onAnswer={answerPeer}
|
|
3179
|
+
onExtend={extendPeer}
|
|
3180
|
+
/>
|
|
3181
|
+
)
|
|
3182
|
+
) : (
|
|
3183
|
+
<div className="flex flex-col items-center gap-2 px-6 text-center">
|
|
3184
|
+
<span className="flex size-10 items-center justify-center rounded-full border border-border bg-surface text-secondary">
|
|
3185
|
+
<Icon name="bot" size={20} />
|
|
3186
|
+
</span>
|
|
3187
|
+
<p className="text-xl font-semibold">
|
|
3188
|
+
{listing.project ? 'What should we build?' : 'What should we set up?'}
|
|
3189
|
+
</p>
|
|
3190
|
+
<p className="flex items-center gap-1.5 text-sm text-muted">
|
|
3191
|
+
<Icon name={listing.project ? 'folder' : 'layout-dashboard'} size={14} />
|
|
3192
|
+
{folder}
|
|
3193
|
+
</p>
|
|
3194
|
+
</div>
|
|
3195
|
+
)}
|
|
3196
|
+
{prompt}
|
|
3197
|
+
</div>
|
|
3198
|
+
{current && (
|
|
3199
|
+
<TabsProvider value={tabs.value}>
|
|
3200
|
+
<div className="pointer-events-none absolute inset-0">
|
|
3201
|
+
{renderSlot('sessions.tab', { session: current, node, Tab })}
|
|
3202
|
+
</div>
|
|
3203
|
+
</TabsProvider>
|
|
3204
|
+
)}
|
|
3205
|
+
</div>
|
|
3206
|
+
</div>
|
|
3207
|
+
)}
|
|
3208
|
+
</LineageContext.Provider>
|
|
3209
|
+
</SplitPane>
|
|
3210
|
+
</SplitView>
|
|
3211
|
+
<SnoozeDialog
|
|
3212
|
+
open={!!customFor}
|
|
3213
|
+
onOpenChange={(open) => {
|
|
3214
|
+
if (!open) setCustomFor(undefined);
|
|
3215
|
+
}}
|
|
3216
|
+
subject={customFor?.title}
|
|
3217
|
+
onSnooze={(until) => customFor && chooseSnooze(customFor, until)}
|
|
3218
|
+
/>
|
|
3219
|
+
<CleanupDialog offer={cleanup} onClose={() => setCleanup(undefined)} />
|
|
3220
|
+
<Dialog
|
|
3221
|
+
open={!!pinAsk}
|
|
3222
|
+
onOpenChange={(open) => {
|
|
3223
|
+
if (!open) setPinAsk(undefined);
|
|
3224
|
+
}}
|
|
3225
|
+
>
|
|
3226
|
+
<DialogContent>
|
|
3227
|
+
<DialogHeader>
|
|
3228
|
+
<DialogTitle>Keep it pinned?</DialogTitle>
|
|
3229
|
+
</DialogHeader>
|
|
3230
|
+
<DialogBody className="pb-3">
|
|
3231
|
+
<DialogDescription>
|
|
3232
|
+
{pinAsk &&
|
|
3233
|
+
`${pinAsk.session.title} is pinned. When it wakes ${when(pinAsk.until)}, it can come back to Pinned, or to Active.`}
|
|
3234
|
+
</DialogDescription>
|
|
3235
|
+
</DialogBody>
|
|
3236
|
+
<DialogFooter className="justify-end gap-2">
|
|
3237
|
+
<Button
|
|
3238
|
+
onClick={() => {
|
|
3239
|
+
const ask = pinAsk!;
|
|
3240
|
+
setPinAsk(undefined);
|
|
3241
|
+
void snooze(ask.session, ask.until, true);
|
|
3242
|
+
}}
|
|
3243
|
+
>
|
|
3244
|
+
Unpin
|
|
3245
|
+
</Button>
|
|
3246
|
+
<Button
|
|
3247
|
+
variant="primary"
|
|
3248
|
+
autoFocus
|
|
3249
|
+
onClick={() => {
|
|
3250
|
+
const ask = pinAsk!;
|
|
3251
|
+
setPinAsk(undefined);
|
|
3252
|
+
void snooze(ask.session, ask.until);
|
|
3253
|
+
}}
|
|
3254
|
+
>
|
|
3255
|
+
Keep pinned
|
|
3256
|
+
</Button>
|
|
3257
|
+
</DialogFooter>
|
|
3258
|
+
</DialogContent>
|
|
3259
|
+
</Dialog>
|
|
3260
|
+
</section>
|
|
3261
|
+
);
|
|
3262
|
+
}
|
|
3263
|
+
|
|
3264
|
+
/** The keys Sessions handles itself (its `onKey`), declared to the shell for the help and for clashes while a Sessions
|
|
3265
|
+
* screen is shown; ⌘ and Ctrl both work, the platform's first. */
|
|
3266
|
+
const keys = () => {
|
|
3267
|
+
const mods = mac ? ['Meta', 'Ctrl'] : ['Ctrl', 'Meta'];
|
|
3268
|
+
const both = (...rest: string[]) => mods.flatMap((mod) => rest.map((key) => `${mod}+${key}`));
|
|
3269
|
+
return [
|
|
3270
|
+
{ keys: 'Escape', label: 'Stop the running turn (twice)' },
|
|
3271
|
+
{
|
|
3272
|
+
keys: both('KeyZ'),
|
|
3273
|
+
label: 'Undo a settle, an unpin, an archive or a snooze (outside a text field)',
|
|
3274
|
+
},
|
|
3275
|
+
{ keys: both('Shift+KeyS'), label: 'Settle or un-settle the open session' },
|
|
3276
|
+
{ keys: both('Shift+KeyP'), label: 'Pin or unpin the open session' },
|
|
3277
|
+
{ keys: both('Shift+KeyM'), label: 'Choose the model' },
|
|
3278
|
+
{ keys: both('Shift+BracketLeft', 'Shift+BracketRight'), label: 'Previous / next session' },
|
|
3279
|
+
];
|
|
3280
|
+
};
|
|
3281
|
+
|
|
3282
|
+
/** Bridge's ⌘K (research-034): the current node's recent sessions, and a search over its sessions; each opens its
|
|
3283
|
+
* session (beside with a window manager, here without). Read when the palette draws, again once 30 s old. */
|
|
3284
|
+
function paletteSessions(ctx: Context) {
|
|
3285
|
+
const api = `/${ctx.workspace.slug}/api/sessions/sessions`;
|
|
3286
|
+
/** The last listing asked (its node, when, its answer to come) and the last one answered (its node's own). */
|
|
3287
|
+
let asked = { node: '', at: 0, listing: Promise.resolve<Session[]>([]) };
|
|
3288
|
+
let loaded = { node: '', sessions: [] as Session[] };
|
|
3289
|
+
const listeners = new Set<() => void>();
|
|
3290
|
+
const listingOf = (node: string) => {
|
|
3291
|
+
if (asked.node === node && Date.now() - asked.at <= 30_000) return asked.listing;
|
|
3292
|
+
const listing = ctx.workspace
|
|
3293
|
+
.fetch(forWorktree(api, shownWorktree(ctx.workspace)), { headers: { 'x-bridge-node': node } })
|
|
3294
|
+
.then(readJson)
|
|
3295
|
+
.then((body) => {
|
|
3296
|
+
const sessions = [...(body as Listing).sessions].sort((a, b) =>
|
|
3297
|
+
(b.updatedAt ?? b.createdAt).localeCompare(a.updatedAt ?? a.createdAt),
|
|
3298
|
+
);
|
|
3299
|
+
if (asked.node === node) {
|
|
3300
|
+
loaded = { node, sessions };
|
|
3301
|
+
for (const listener of listeners) listener();
|
|
3302
|
+
}
|
|
3303
|
+
return sessions;
|
|
3304
|
+
})
|
|
3305
|
+
.catch(() => [] as Session[]);
|
|
3306
|
+
asked = { node, at: Date.now(), listing };
|
|
3307
|
+
return listing;
|
|
3308
|
+
};
|
|
3309
|
+
/** The current node's sessions as last answered (never another node's), asking again once 30 s old. */
|
|
3310
|
+
const current = () => {
|
|
3311
|
+
const node = ctx.shell.get().node;
|
|
3312
|
+
void listingOf(node);
|
|
3313
|
+
return loaded.node === node ? loaded.sessions : [];
|
|
3314
|
+
};
|
|
3315
|
+
const command = (each: Session, group: string) => ({
|
|
3316
|
+
id: each.id,
|
|
3317
|
+
label: each.title || 'New chat',
|
|
3318
|
+
description: [
|
|
3319
|
+
ago(each.updatedAt ?? each.createdAt),
|
|
3320
|
+
each.running && 'running',
|
|
3321
|
+
each.waiting && 'waiting',
|
|
3322
|
+
]
|
|
3323
|
+
.filter(Boolean)
|
|
3324
|
+
.join(' · '),
|
|
3325
|
+
group,
|
|
3326
|
+
icon: 'messages-square',
|
|
3327
|
+
navigation: true,
|
|
3328
|
+
// Shown here; ⌘K's ↵ with a modifier places it (the palette tells the shell).
|
|
3329
|
+
run: () => ctx.screen.open(each.id, { show: true }),
|
|
3330
|
+
});
|
|
3331
|
+
return {
|
|
3332
|
+
depth: 0,
|
|
3333
|
+
level: 'workspace' as const,
|
|
3334
|
+
owner: name,
|
|
3335
|
+
subject: () => undefined,
|
|
3336
|
+
subscribe: (listener: () => void) => {
|
|
3337
|
+
listeners.add(listener);
|
|
3338
|
+
return () => void listeners.delete(listener);
|
|
3339
|
+
},
|
|
3340
|
+
commands: () =>
|
|
3341
|
+
current()
|
|
3342
|
+
.slice(0, 8)
|
|
3343
|
+
.map((each) => command(each, 'Recent sessions')),
|
|
3344
|
+
// Waits for the current node's listing (a cold search answers once it is in).
|
|
3345
|
+
search: async (query: string) => {
|
|
3346
|
+
const words = query.toLowerCase().split(/\s+/).filter(Boolean);
|
|
3347
|
+
return (await listingOf(ctx.shell.get().node))
|
|
3348
|
+
.filter((each) => {
|
|
3349
|
+
const text = `${each.title} ${each.preview ?? ''}`.toLowerCase();
|
|
3350
|
+
return words.every((word) => text.includes(word));
|
|
3351
|
+
})
|
|
3352
|
+
.slice(0, 8)
|
|
3353
|
+
.map((each) => command(each, 'Sessions'));
|
|
3354
|
+
},
|
|
3355
|
+
};
|
|
3356
|
+
}
|
|
3357
|
+
|
|
3358
|
+
export function apply(ctx: Context) {
|
|
3359
|
+
// Its sessions in Bridge's ⌘K, when the `commands` plugin is there (followed if it comes).
|
|
3360
|
+
const palette = paletteSessions(ctx);
|
|
3361
|
+
ctx.effect(() => {
|
|
3362
|
+
let off: (() => void) | undefined;
|
|
3363
|
+
const attach = () => {
|
|
3364
|
+
off?.();
|
|
3365
|
+
off = (ctx.get('commands') as Context['commands'] | undefined)?.register(palette);
|
|
3366
|
+
};
|
|
3367
|
+
attach();
|
|
3368
|
+
const offService = ctx.on('internal/service', (service) => {
|
|
3369
|
+
if (service === 'commands') attach();
|
|
3370
|
+
});
|
|
3371
|
+
return () => {
|
|
3372
|
+
offService();
|
|
3373
|
+
off?.();
|
|
3374
|
+
};
|
|
3375
|
+
});
|
|
3376
|
+
// Declares `sessions.tab` (./tabs.tsx), a list other plugins fill, and `views.component` (research-036 P2), which
|
|
3377
|
+
// other plugins fill too: each entry gets the component a view calls (`{ component, props, node }`), is keyed by its
|
|
3378
|
+
// plugin key, and draws all of that plugin's components by `component`. Not in the shell's slot map, so typed here.
|
|
3379
|
+
type ScreenProps = {
|
|
3380
|
+
node: string;
|
|
3381
|
+
screen: string;
|
|
3382
|
+
item?: string;
|
|
3383
|
+
focused?: boolean;
|
|
3384
|
+
renderSlot: RenderTabs;
|
|
3385
|
+
};
|
|
3386
|
+
// oxlint-disable-next-line bridge/no-new-unchecked-cast -- typing: slots.register's overloads do not name this screen's children
|
|
3387
|
+
const slots = ctx.slots as unknown as {
|
|
3388
|
+
register(
|
|
3389
|
+
options: {
|
|
3390
|
+
name: 'screen';
|
|
3391
|
+
key: string;
|
|
3392
|
+
children: {
|
|
3393
|
+
'sessions.tab': { kind: 'list'; scope: 'root' };
|
|
3394
|
+
'views.component': { kind: 'keyed'; scope: 'root' };
|
|
3395
|
+
};
|
|
3396
|
+
},
|
|
3397
|
+
render: (props: ScreenProps) => React.ReactNode,
|
|
3398
|
+
): void;
|
|
3399
|
+
};
|
|
3400
|
+
slots.register(
|
|
3401
|
+
{
|
|
3402
|
+
name: 'screen',
|
|
3403
|
+
key: name,
|
|
3404
|
+
children: {
|
|
3405
|
+
'sessions.tab': { kind: 'list', scope: 'root' },
|
|
3406
|
+
'views.component': { kind: 'keyed', scope: 'root' },
|
|
3407
|
+
},
|
|
3408
|
+
},
|
|
3409
|
+
({ node, screen, item, focused, renderSlot }) => (
|
|
3410
|
+
<Sessions
|
|
3411
|
+
ctx={ctx}
|
|
3412
|
+
node={node}
|
|
3413
|
+
screen={screen}
|
|
3414
|
+
item={item}
|
|
3415
|
+
focused={focused}
|
|
3416
|
+
renderSlot={renderSlot}
|
|
3417
|
+
/>
|
|
3418
|
+
),
|
|
3419
|
+
);
|
|
3420
|
+
// The phone's variant (research-020 D5), drawn by the shell in the place of `screen` (./mobile.tsx).
|
|
3421
|
+
ctx.slots.register(
|
|
3422
|
+
{ name: 'mobileScreen', key: name },
|
|
3423
|
+
({ node, item, readOnly, setActions, setChrome }) => (
|
|
3424
|
+
<MobileSessions
|
|
3425
|
+
ctx={ctx}
|
|
3426
|
+
node={node}
|
|
3427
|
+
item={item}
|
|
3428
|
+
readOnly={readOnly}
|
|
3429
|
+
setActions={setActions}
|
|
3430
|
+
setChrome={setChrome}
|
|
3431
|
+
/>
|
|
3432
|
+
),
|
|
3433
|
+
);
|
|
3434
|
+
}
|