@bridgebrew/sessions 0.0.34 → 0.0.43
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/branch-name.ts +45 -0
- package/browser/branch-actions.tsx +26 -47
- package/browser/conversation.tsx +123 -3
- package/browser/details.tsx +408 -0
- package/browser/index.tsx +685 -497
- package/browser/mobile.tsx +118 -8
- package/browser/pull-request.tsx +9 -6
- package/browser/session-scheduled.tsx +6 -3
- package/browser/subagent-view.tsx +2 -2
- package/contract.ts +6 -0
- package/delegations.ts +6 -7
- package/package.json +51 -10
- package/server.ts +642 -236
- package/text.ts +9 -0
- package/browser/changes.tsx +0 -464
- package/git.ts +0 -318
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
// The open session's details panel, on the right of the conversation (plugins#239 §1): its git rows (its branch, its
|
|
2
|
+
// uncommitted changes as a folder tree, its Git row, and the one Commit changes dialog), drawn by the `git` plugin into
|
|
3
|
+
// `sessions.details` (bridge-plugins#256) from the changes this panel reads (`POST /sessions/changes`); its pull
|
|
4
|
+
// request once linked (its state and checks, `./pull-request.tsx`); its gallery, tags and marks. The panel holds which
|
|
5
|
+
// file the dialog is open at: the git rows open it (the Git row, Open diff, a file of the tree), and so does the
|
|
6
|
+
// conversation (a turn's Review changes, a tool call's file: `diffAsked`). Without the `git` plugin, a quiet Install Git
|
|
7
|
+
// row. Each section says why it is empty: no git, the default branch (no pull request), no GitHub plugin.
|
|
8
|
+
// The same panel is the phone's Details screen (`./mobile.tsx`), pushed with the shell's Back; its git rows come from the
|
|
9
|
+
// `git.session-details` factory there.
|
|
10
|
+
import * as React from 'react';
|
|
11
|
+
import { Button, Icon, TagInput, cn, isRecord } from '@bridge/ui';
|
|
12
|
+
import type { ChangesAnswer } from '@bridgebrew/git/contract.ts';
|
|
13
|
+
import { PullRequestPill, type SessionPullRequest } from './pull-request.tsx';
|
|
14
|
+
|
|
15
|
+
/** A POST to the sessions API (`path` after `/sessions`): its JSON answer, or a rejection with the refusal's `code`. */
|
|
16
|
+
export type Post = (path: string, body: object) => Promise<any>;
|
|
17
|
+
|
|
18
|
+
/** The phone's item for session `id`'s details: under the session's in the shell's tree of items (`parent/child`), so
|
|
19
|
+
* the shell's Back, as the browser's, returns to the session. */
|
|
20
|
+
const DETAILS = '/details';
|
|
21
|
+
export const detailsItem = (id: string) => `${id}${DETAILS}`;
|
|
22
|
+
/** The session an item names, and whether it asks for its details. */
|
|
23
|
+
export const detailsOf = (item: string) =>
|
|
24
|
+
item.endsWith(DETAILS)
|
|
25
|
+
? { session: item.slice(0, -DETAILS.length), details: true }
|
|
26
|
+
: { session: item, details: false };
|
|
27
|
+
|
|
28
|
+
/** The fields of a session the panel reads. */
|
|
29
|
+
export interface PanelSession {
|
|
30
|
+
id: string;
|
|
31
|
+
node: string;
|
|
32
|
+
branch?: { name: string; folder: string; temporary?: true };
|
|
33
|
+
pullRequest?: SessionPullRequest;
|
|
34
|
+
tags?: string[];
|
|
35
|
+
files?: string[];
|
|
36
|
+
claims?: { path: string; note?: string }[];
|
|
37
|
+
commits?: unknown[];
|
|
38
|
+
lastTurn?: { endedAt: string };
|
|
39
|
+
running?: boolean;
|
|
40
|
+
}
|
|
41
|
+
/** One row of the panel (plugins#239, the owner's rows): its icon, its title, its value or action on the right, and what
|
|
42
|
+
* it holds under it (the changes' tree); a region named `name`. */
|
|
43
|
+
export const Row = ({
|
|
44
|
+
name,
|
|
45
|
+
icon,
|
|
46
|
+
title = name,
|
|
47
|
+
value,
|
|
48
|
+
children,
|
|
49
|
+
}: {
|
|
50
|
+
name: string;
|
|
51
|
+
icon: string;
|
|
52
|
+
title?: React.ReactNode;
|
|
53
|
+
value?: React.ReactNode;
|
|
54
|
+
children?: React.ReactNode;
|
|
55
|
+
}) => (
|
|
56
|
+
<section aria-label={name} className="flex flex-col gap-1 px-4 py-1.5">
|
|
57
|
+
<div className="flex min-h-8 min-w-0 items-center gap-2">
|
|
58
|
+
<Icon name={icon} size={14} className="shrink-0 text-muted" />
|
|
59
|
+
<span className="min-w-0 max-w-[60%] shrink-0 truncate font-medium">{title}</span>
|
|
60
|
+
<span className="flex min-w-0 flex-1 items-center justify-end gap-1.5 text-right text-xs text-secondary">
|
|
61
|
+
{value}
|
|
62
|
+
</span>
|
|
63
|
+
</div>
|
|
64
|
+
{children}
|
|
65
|
+
</section>
|
|
66
|
+
);
|
|
67
|
+
const Note = ({ children }: { children: React.ReactNode }) => (
|
|
68
|
+
<span className="text-xs text-muted">{children}</span>
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
/** What `sessions.details`' entries get (the `git` plugin's rows, contract `sessions` 1.1). */
|
|
72
|
+
export interface GitRowsProps {
|
|
73
|
+
slug: string;
|
|
74
|
+
session: PanelSession;
|
|
75
|
+
/** The changes as the panel read them: `undefined` while read, `null` when unavailable. */
|
|
76
|
+
changes: ChangesAnswer | null | undefined;
|
|
77
|
+
post: Post;
|
|
78
|
+
Row: typeof Row;
|
|
79
|
+
/** The Commit changes dialog: open at a file (none: its top), or closed. */
|
|
80
|
+
diff: { path?: string } | undefined;
|
|
81
|
+
onDiff: (at: { path?: string } | undefined) => void;
|
|
82
|
+
readOnly: boolean;
|
|
83
|
+
onChanged: () => void;
|
|
84
|
+
onOpenSession: (id: string) => void;
|
|
85
|
+
pullRequestOpen: boolean;
|
|
86
|
+
onGitAnswer?: (() => void) | undefined;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const TAGS_MAX = 5;
|
|
90
|
+
/** A session's tags (up to five), the workspace's offered as suggestions; `onChange`: the tags after an edit. */
|
|
91
|
+
export function SessionTags({
|
|
92
|
+
tags = [],
|
|
93
|
+
known,
|
|
94
|
+
onChange,
|
|
95
|
+
bare = false,
|
|
96
|
+
}: {
|
|
97
|
+
tags?: string[] | undefined;
|
|
98
|
+
known: string[];
|
|
99
|
+
onChange: (tags: string[]) => void;
|
|
100
|
+
/** Without its own tag icon (a row of the details panel draws one). */
|
|
101
|
+
bare?: boolean;
|
|
102
|
+
}) {
|
|
103
|
+
const listId = React.useId();
|
|
104
|
+
return (
|
|
105
|
+
<div className={cn('flex items-center gap-2', bare && 'min-w-0 flex-1')}>
|
|
106
|
+
{!bare && <Icon name="tag" size={14} className="shrink-0 text-muted" />}
|
|
107
|
+
<TagInput
|
|
108
|
+
aria-label="Tags"
|
|
109
|
+
list={listId}
|
|
110
|
+
placeholder={tags.length < TAGS_MAX ? 'Add a tag…' : ''}
|
|
111
|
+
value={tags}
|
|
112
|
+
onChange={(next: string[]) => onChange(next.slice(0, TAGS_MAX))}
|
|
113
|
+
className="min-w-0 flex-1"
|
|
114
|
+
/>
|
|
115
|
+
<datalist id={listId}>
|
|
116
|
+
{known
|
|
117
|
+
.filter((each) => !tags.includes(each))
|
|
118
|
+
.map((each) => (
|
|
119
|
+
<option key={each} value={each} />
|
|
120
|
+
))}
|
|
121
|
+
</datalist>
|
|
122
|
+
</div>
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** The file of `files` (relative to `repository`) that `wanted` names, as an agent named it (absolute, or relative to
|
|
127
|
+
* the session's folder): the same path first, else one ending the other (`src/app.ts` before `app.ts`). */
|
|
128
|
+
export const fileNamed = <T extends { path: string }>(
|
|
129
|
+
files: T[],
|
|
130
|
+
wanted: string,
|
|
131
|
+
repository: string,
|
|
132
|
+
) =>
|
|
133
|
+
files.find(
|
|
134
|
+
({ path }) => wanted === path || wanted === `${repository.replace(/\/$/, '')}/${path}`,
|
|
135
|
+
) ??
|
|
136
|
+
files.find(({ path }) => wanted.endsWith(`/${path}`)) ??
|
|
137
|
+
files.find(({ path }) => path.endsWith(`/${wanted}`));
|
|
138
|
+
|
|
139
|
+
/** The changes `/changes` answered, read as `ChangesAnswer` (its lists empty when missing); `null` when not one. */
|
|
140
|
+
const changesIn = (found: unknown): ChangesAnswer | null => {
|
|
141
|
+
if (!isRecord(found) || (found.repository !== null && typeof found.repository !== 'string')) return null;
|
|
142
|
+
const strings = (value: unknown) =>
|
|
143
|
+
Array.isArray(value) ? value.filter((each): each is string => typeof each === 'string') : [];
|
|
144
|
+
return {
|
|
145
|
+
repository: found.repository,
|
|
146
|
+
...(found.git === 'missing' && { git: 'missing' as const }),
|
|
147
|
+
files: Array.isArray(found.files)
|
|
148
|
+
? found.files.flatMap((each: unknown) =>
|
|
149
|
+
isRecord(each) && typeof each.path === 'string'
|
|
150
|
+
? [
|
|
151
|
+
{
|
|
152
|
+
path: each.path,
|
|
153
|
+
pending: each.pending === true,
|
|
154
|
+
...(typeof each.added === 'number' && { added: each.added }),
|
|
155
|
+
...(typeof each.removed === 'number' && { removed: each.removed }),
|
|
156
|
+
},
|
|
157
|
+
]
|
|
158
|
+
: [],
|
|
159
|
+
)
|
|
160
|
+
: [],
|
|
161
|
+
diff: typeof found.diff === 'string' ? found.diff : '',
|
|
162
|
+
...(found.byFile === true && { byFile: true }),
|
|
163
|
+
...(found.cut === true && { cut: true }),
|
|
164
|
+
branch:
|
|
165
|
+
isRecord(found.branch) && typeof found.branch.name === 'string'
|
|
166
|
+
? {
|
|
167
|
+
name: found.branch.name,
|
|
168
|
+
isDefault: found.branch.isDefault === true,
|
|
169
|
+
remote: found.branch.remote === true,
|
|
170
|
+
upstream: found.branch.upstream === true,
|
|
171
|
+
unpushed: typeof found.branch.unpushed === 'number' ? found.branch.unpushed : 0,
|
|
172
|
+
}
|
|
173
|
+
: null,
|
|
174
|
+
commits: [],
|
|
175
|
+
shared: [],
|
|
176
|
+
others: strings(found.others),
|
|
177
|
+
...(found.github === 'missing' || found.github === 'ready' ? { github: found.github } : {}),
|
|
178
|
+
};
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
/** The panel of `session` (drawn afresh for each: nothing of the previous one's answers lands on the next). */
|
|
182
|
+
export function DetailsPanel(props: {
|
|
183
|
+
session: PanelSession;
|
|
184
|
+
/** The workspace whose Branches route lists its branches (a worktree's own, research-045). */
|
|
185
|
+
slug: string;
|
|
186
|
+
known: string[];
|
|
187
|
+
post: Post;
|
|
188
|
+
onChanged: () => void;
|
|
189
|
+
/** Its pull request's menu: Sessions' pull request routes. */
|
|
190
|
+
actPullRequest: (action: 'watch' | 'unwatch' | 'unlink') => void;
|
|
191
|
+
/** A phone shown read-only: tags are listed, not edited; the dialog shows the diff alone. */
|
|
192
|
+
readOnly?: boolean;
|
|
193
|
+
/** The diff asked from outside the panel (a tool call's file in the conversation): the Commit changes dialog opens at
|
|
194
|
+
* `path` (none: its top) once the changes are read, again at each new `at`. */
|
|
195
|
+
diffAsked?: { path?: string; at: number } | undefined;
|
|
196
|
+
/** The ask opened the dialog: the page forgets it, so a panel drawn again later does not open it again. */
|
|
197
|
+
onDiffAnswered?: (() => void) | undefined;
|
|
198
|
+
/** Its git rows: `sessions.details` (the `git` plugin's); nothing without that plugin's page. */
|
|
199
|
+
gitRows: (props: GitRowsProps) => React.ReactNode;
|
|
200
|
+
/** Another session sharing files, opened from the dialog. */
|
|
201
|
+
onOpenSession?: ((id: string) => void) | undefined;
|
|
202
|
+
/** After each Git action's answer (its pull request read again). */
|
|
203
|
+
onGitAnswer?: (() => void) | undefined;
|
|
204
|
+
/** Installs the `git` plugin in this workspace (its absence's one action). */
|
|
205
|
+
onInstallGit?: (() => Promise<void>) | undefined;
|
|
206
|
+
/** The rows the page adds (the desktop's): the gallery, and its marks (`Row`s: pinned, settled, snoozed, scheduled,
|
|
207
|
+
* its parent, its stage), after the tags. */
|
|
208
|
+
gallery?: React.ReactNode;
|
|
209
|
+
marks?: React.ReactNode;
|
|
210
|
+
}) {
|
|
211
|
+
return <Panel key={props.session.id} {...props} />;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function Panel({
|
|
215
|
+
session,
|
|
216
|
+
slug,
|
|
217
|
+
known,
|
|
218
|
+
post,
|
|
219
|
+
onChanged,
|
|
220
|
+
actPullRequest,
|
|
221
|
+
readOnly = false,
|
|
222
|
+
diffAsked,
|
|
223
|
+
onDiffAnswered,
|
|
224
|
+
gitRows,
|
|
225
|
+
onOpenSession,
|
|
226
|
+
onGitAnswer,
|
|
227
|
+
onInstallGit,
|
|
228
|
+
gallery,
|
|
229
|
+
marks,
|
|
230
|
+
}: React.ComponentProps<typeof DetailsPanel>) {
|
|
231
|
+
const [error, setError] = React.useState('');
|
|
232
|
+
const [changes, setChanges] = React.useState<ChangesAnswer | null>();
|
|
233
|
+
/** Bumped to read the changes again though the session did not change (the Git plugin just installed). */
|
|
234
|
+
const [reads, setReads] = React.useState(0);
|
|
235
|
+
const changesKey = JSON.stringify([
|
|
236
|
+
reads,
|
|
237
|
+
session.id,
|
|
238
|
+
session.files,
|
|
239
|
+
session.commits?.length,
|
|
240
|
+
session.lastTurn?.endedAt,
|
|
241
|
+
]);
|
|
242
|
+
React.useEffect(() => {
|
|
243
|
+
let live = true;
|
|
244
|
+
post('/changes', { id: session.id }).then(
|
|
245
|
+
(found: unknown) => live && setChanges(changesIn(found)),
|
|
246
|
+
(cause: unknown) => {
|
|
247
|
+
if (!live) return;
|
|
248
|
+
if (isRecord(cause) && cause.code === 'not_a_repository')
|
|
249
|
+
setChanges(changesIn({ repository: null }));
|
|
250
|
+
else {
|
|
251
|
+
setError(cause instanceof Error ? cause.message : String(cause));
|
|
252
|
+
setChanges(null);
|
|
253
|
+
}
|
|
254
|
+
},
|
|
255
|
+
);
|
|
256
|
+
return () => {
|
|
257
|
+
live = false;
|
|
258
|
+
};
|
|
259
|
+
}, [changesKey]);
|
|
260
|
+
/** The Commit changes dialog, and the file it opened at (none: its top). */
|
|
261
|
+
const [diffAt, setDiffAt] = React.useState<{ path?: string }>();
|
|
262
|
+
// Asked from outside: opened once the changes are read, each ask once (the panel may be drawn for it).
|
|
263
|
+
const answered = React.useRef<number>(undefined);
|
|
264
|
+
React.useEffect(() => {
|
|
265
|
+
if (!diffAsked || changes === undefined || answered.current === diffAsked.at) return;
|
|
266
|
+
answered.current = diffAsked.at;
|
|
267
|
+
onDiffAnswered?.();
|
|
268
|
+
const { path } = diffAsked;
|
|
269
|
+
if (path === undefined) return setDiffAt({});
|
|
270
|
+
// A tool call names the file by its full path, the diff by its path in the repository.
|
|
271
|
+
const repository = changes?.repository?.replace(/\/$/, '');
|
|
272
|
+
const pending = (changes?.files ?? [])
|
|
273
|
+
.filter((each) => each.pending && !each.path.endsWith('/'))
|
|
274
|
+
.map((each) => ({ path: each.path }));
|
|
275
|
+
const found = repository ? fileNamed(pending, path, repository) : undefined;
|
|
276
|
+
setDiffAt({
|
|
277
|
+
path:
|
|
278
|
+
found?.path ??
|
|
279
|
+
(repository && path.startsWith(`${repository}/`) ? path.slice(repository.length + 1) : path),
|
|
280
|
+
});
|
|
281
|
+
}, [diffAsked?.at, changes]);
|
|
282
|
+
|
|
283
|
+
const missing = changes?.git === 'missing';
|
|
284
|
+
const noGit = changes === null || changes?.repository === null;
|
|
285
|
+
const [installing, setInstalling] = React.useState(false);
|
|
286
|
+
const pullRequestOpen =
|
|
287
|
+
!!session.branch &&
|
|
288
|
+
session.pullRequest?.headBranch === session.branch.name &&
|
|
289
|
+
(session.pullRequest.state === 'open' || session.pullRequest.state === 'draft');
|
|
290
|
+
|
|
291
|
+
return (
|
|
292
|
+
<div aria-label="Session details" className="flex min-h-0 flex-col py-2 text-sm">
|
|
293
|
+
{!missing &&
|
|
294
|
+
gitRows({
|
|
295
|
+
slug,
|
|
296
|
+
session,
|
|
297
|
+
changes,
|
|
298
|
+
post,
|
|
299
|
+
Row,
|
|
300
|
+
diff: diffAt,
|
|
301
|
+
onDiff: setDiffAt,
|
|
302
|
+
readOnly,
|
|
303
|
+
onChanged,
|
|
304
|
+
onOpenSession: (id) => {
|
|
305
|
+
setDiffAt(undefined);
|
|
306
|
+
onOpenSession?.(id);
|
|
307
|
+
},
|
|
308
|
+
pullRequestOpen,
|
|
309
|
+
onGitAnswer,
|
|
310
|
+
})}
|
|
311
|
+
{missing && (
|
|
312
|
+
<Row
|
|
313
|
+
name="Git"
|
|
314
|
+
icon="git-branch"
|
|
315
|
+
value={
|
|
316
|
+
readOnly || !onInstallGit ? (
|
|
317
|
+
<Note>Git needs the Git plugin.</Note>
|
|
318
|
+
) : (
|
|
319
|
+
<Button
|
|
320
|
+
size="sm"
|
|
321
|
+
variant="ghost"
|
|
322
|
+
className="-mr-2"
|
|
323
|
+
disabled={installing}
|
|
324
|
+
onClick={() => {
|
|
325
|
+
setInstalling(true);
|
|
326
|
+
setError('');
|
|
327
|
+
onInstallGit()
|
|
328
|
+
.then(
|
|
329
|
+
() => {
|
|
330
|
+
setReads((was) => was + 1);
|
|
331
|
+
onChanged();
|
|
332
|
+
},
|
|
333
|
+
(cause: unknown) =>
|
|
334
|
+
setError(cause instanceof Error ? cause.message : String(cause)),
|
|
335
|
+
)
|
|
336
|
+
.finally(() => setInstalling(false));
|
|
337
|
+
}}
|
|
338
|
+
>
|
|
339
|
+
{installing ? 'Installing…' : 'Install Git'}
|
|
340
|
+
</Button>
|
|
341
|
+
)
|
|
342
|
+
}
|
|
343
|
+
/>
|
|
344
|
+
)}
|
|
345
|
+
<div role="separator" className="mx-4 my-1 h-px bg-border" />
|
|
346
|
+
<Row
|
|
347
|
+
name="Pull request"
|
|
348
|
+
icon="git-pull-request"
|
|
349
|
+
value={
|
|
350
|
+
session.pullRequest ? (
|
|
351
|
+
<PullRequestPill
|
|
352
|
+
pullRequest={session.pullRequest}
|
|
353
|
+
act={readOnly ? undefined : actPullRequest}
|
|
354
|
+
/>
|
|
355
|
+
) : (
|
|
356
|
+
<Note>
|
|
357
|
+
{missing
|
|
358
|
+
? 'Pull requests need the Git plugin.'
|
|
359
|
+
: noGit
|
|
360
|
+
? 'Not a git repository.'
|
|
361
|
+
: changes?.branch?.isDefault
|
|
362
|
+
? 'On the default branch: no pull request.'
|
|
363
|
+
: changes?.github === 'missing'
|
|
364
|
+
? 'Pull requests need the GitHub plugin.'
|
|
365
|
+
: 'None yet. The Git button opens one.'}
|
|
366
|
+
</Note>
|
|
367
|
+
)
|
|
368
|
+
}
|
|
369
|
+
>
|
|
370
|
+
{session.pullRequest?.title && (
|
|
371
|
+
<span className="min-w-0 truncate pl-[22px] text-xs text-secondary">
|
|
372
|
+
{session.pullRequest.title}
|
|
373
|
+
</span>
|
|
374
|
+
)}
|
|
375
|
+
</Row>
|
|
376
|
+
{gallery}
|
|
377
|
+
<Row
|
|
378
|
+
name="Tags"
|
|
379
|
+
icon="tag"
|
|
380
|
+
value={
|
|
381
|
+
readOnly ? (
|
|
382
|
+
<Note>
|
|
383
|
+
{session.tags?.length ? session.tags.map((tag) => `#${tag}`).join(' ') : 'No tags.'}
|
|
384
|
+
</Note>
|
|
385
|
+
) : (
|
|
386
|
+
<SessionTags
|
|
387
|
+
bare
|
|
388
|
+
tags={session.tags}
|
|
389
|
+
known={known}
|
|
390
|
+
onChange={(tags) => {
|
|
391
|
+
setError('');
|
|
392
|
+
post('/tags', { id: session.id, tags }).then(onChanged, (cause: unknown) =>
|
|
393
|
+
setError(cause instanceof Error ? cause.message : String(cause)),
|
|
394
|
+
);
|
|
395
|
+
}}
|
|
396
|
+
/>
|
|
397
|
+
)
|
|
398
|
+
}
|
|
399
|
+
/>
|
|
400
|
+
{marks}
|
|
401
|
+
{error && (
|
|
402
|
+
<p role="alert" className="m-0 px-4 py-2 text-xs text-destructive">
|
|
403
|
+
{error}
|
|
404
|
+
</p>
|
|
405
|
+
)}
|
|
406
|
+
</div>
|
|
407
|
+
);
|
|
408
|
+
}
|