@bridgebrew/project 0.0.1 → 0.0.2
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/branches.ts +1220 -0
- package/browser/branches.tsx +124 -8
- package/browser/overview.tsx +3 -2
- package/package.json +10 -3
- package/server.ts +85 -1
package/browser/branches.tsx
CHANGED
|
@@ -5,8 +5,10 @@
|
|
|
5
5
|
// button and its menu (`@bridge/session-ui/git-actions`), Activate, and "…" for the rest (Delete, with why it is
|
|
6
6
|
// refused). New branch starts one from the default branch; a selected branch takes a prompt whose session works on it.
|
|
7
7
|
// Activate makes a branch the active one: everything Bridge loads from the project (its plugins, host-level code such as
|
|
8
|
-
// the Bridge UI kit) follows it, without a restart. Data and actions from `/
|
|
9
|
-
// (
|
|
8
|
+
// the Bridge UI kit) follows it, without a restart. Data and actions from this plugin's `/branches` routes
|
|
9
|
+
// (../branches.ts, bridgebrew/bridge#557). New, Push, Pull, Create PR and Activate may run on past their route: the
|
|
10
|
+
// row says which runs, then why it failed, and the screen reads the branches again until it ends. Without the GitHub
|
|
11
|
+
// plugin, every git action works and a notice offers to install it for pull requests.
|
|
10
12
|
import * as React from 'react';
|
|
11
13
|
import {
|
|
12
14
|
Badge,
|
|
@@ -43,16 +45,43 @@ import type {} from '@bridge/ui-renderer/client';
|
|
|
43
45
|
import { SessionPrompt } from '@bridge/session-ui/prompt';
|
|
44
46
|
import {
|
|
45
47
|
GitActions,
|
|
46
|
-
branchesRoute,
|
|
47
48
|
type BranchesAnswer,
|
|
49
|
+
type BranchesRoute,
|
|
48
50
|
type GitBranch,
|
|
49
51
|
} from '@bridge/session-ui/git-actions';
|
|
50
52
|
|
|
53
|
+
/** This plugin's Branches route of project `node` in workspace `slug`, or one of its actions. */
|
|
54
|
+
export const projectBranchesRoute: BranchesRoute = (slug, node, action) =>
|
|
55
|
+
`/${encodeURIComponent(slug)}/api/project/branches${action ? `/${action}` : ''}?node=${encodeURIComponent(node)}`;
|
|
56
|
+
|
|
57
|
+
/** An action that may run on past its route: running, or how it ended. */
|
|
58
|
+
interface Job {
|
|
59
|
+
action: string;
|
|
60
|
+
running: boolean;
|
|
61
|
+
error?: string;
|
|
62
|
+
}
|
|
51
63
|
interface View extends BranchesAnswer {
|
|
52
64
|
project: { title: string; folder: string };
|
|
65
|
+
/** Whether the workspace runs the GitHub plugin, which pull requests need. */
|
|
66
|
+
github?: 'ready' | 'missing';
|
|
53
67
|
/** Host-level code loaded from the project: why Activate is refused (an installed Bridge), its last build's error. */
|
|
54
68
|
hostLevel?: { refusal?: string; error?: string };
|
|
69
|
+
branches: (GitBranch & { job?: Job })[];
|
|
70
|
+
/** New branches being made, by name. */
|
|
71
|
+
jobs?: (Job & { name: string })[];
|
|
72
|
+
/** A fetch or a GitHub read runs: the next reading shows what it read. */
|
|
73
|
+
refreshing?: boolean;
|
|
55
74
|
}
|
|
75
|
+
/** What each action is called while it runs, and when it failed. */
|
|
76
|
+
const JOB_WORDS: Record<string, [running: string, failed: string]> = {
|
|
77
|
+
new: ['Preparing…', 'Not made'],
|
|
78
|
+
push: ['Pushing…', 'Push failed'],
|
|
79
|
+
pull: ['Pulling…', 'Pull failed'],
|
|
80
|
+
pr: ['Opening PR…', 'PR failed'],
|
|
81
|
+
activate: ['Activating…', 'Activate failed'],
|
|
82
|
+
};
|
|
83
|
+
/** How often the screen reads the branches again while an action runs on. */
|
|
84
|
+
const POLL_MS = 2_000;
|
|
56
85
|
|
|
57
86
|
/** Name, Active, behind | ahead of the default branch, uncommitted changes, pull request, actions: each mark in a slot
|
|
58
87
|
* of its own, so it never moves. Changes takes the width left before the name (it grows to 196px first), so a 1024
|
|
@@ -218,9 +247,11 @@ export function Branches({ ctx, node }: { ctx: Context; node: string }) {
|
|
|
218
247
|
const [naming, setNaming] = React.useState(false);
|
|
219
248
|
const [selected, setSelected] = React.useState<string>();
|
|
220
249
|
const [running, setRunning] = React.useState<{ action: string; name?: string }>();
|
|
250
|
+
const [installing, setInstalling] = React.useState(false);
|
|
251
|
+
const [reads, setReads] = React.useState(0);
|
|
221
252
|
React.useEffect(() => {
|
|
222
253
|
let gone = false;
|
|
223
|
-
fetch(
|
|
254
|
+
fetch(projectBranchesRoute(slug, node), { cache: 'no-store' })
|
|
224
255
|
.then(readJson)
|
|
225
256
|
.then((body: unknown) => {
|
|
226
257
|
if (gone) return;
|
|
@@ -239,20 +270,52 @@ export function Branches({ ctx, node }: { ctx: Context; node: string }) {
|
|
|
239
270
|
return () => {
|
|
240
271
|
gone = true;
|
|
241
272
|
};
|
|
242
|
-
}, [slug, node]);
|
|
273
|
+
}, [slug, node, reads]);
|
|
274
|
+
// While an action runs on past its route, the branches are read again until it ends.
|
|
275
|
+
const runsOn =
|
|
276
|
+
!!view?.refreshing ||
|
|
277
|
+
!!view?.branches.some((branch) => branch.job?.running) ||
|
|
278
|
+
!!view?.jobs?.some((job) => job.running);
|
|
279
|
+
React.useEffect(() => {
|
|
280
|
+
if (!runsOn) return;
|
|
281
|
+
const timer = setTimeout(() => setReads((count) => count + 1), POLL_MS);
|
|
282
|
+
return () => clearTimeout(timer);
|
|
283
|
+
}, [runsOn, view]);
|
|
284
|
+
/** Installs the GitHub plugin in this workspace (the Plugins screen's own install), then reads the branches again. */
|
|
285
|
+
const installGitHub = async () => {
|
|
286
|
+
setInstalling(true);
|
|
287
|
+
try {
|
|
288
|
+
const res = await fetch(`/_bridge/workspaces/${encodeURIComponent(slug)}/plugins/install`, {
|
|
289
|
+
method: 'POST',
|
|
290
|
+
headers: { 'content-type': 'application/json' },
|
|
291
|
+
body: JSON.stringify({ source: '@bridgebrew/github' }),
|
|
292
|
+
});
|
|
293
|
+
await readJson(res);
|
|
294
|
+
toast({ variant: 'success', title: 'GitHub installed' });
|
|
295
|
+
setReads((count) => count + 1);
|
|
296
|
+
} catch (cause) {
|
|
297
|
+
toast({
|
|
298
|
+
variant: 'error',
|
|
299
|
+
title: 'GitHub is not installed',
|
|
300
|
+
description: cause instanceof Error ? cause.message : String(cause),
|
|
301
|
+
});
|
|
302
|
+
} finally {
|
|
303
|
+
setInstalling(false);
|
|
304
|
+
}
|
|
305
|
+
};
|
|
243
306
|
/** Posts `action`; shows the branches it answers. */
|
|
244
307
|
const run = async (action: string, body: Record<string, unknown>, branch?: string) => {
|
|
245
308
|
setRunning({ action, ...(branch !== undefined && { name: branch }) });
|
|
246
309
|
try {
|
|
247
310
|
// The host's own answer: typed once where it is read.
|
|
248
311
|
const answer = (await readJson(
|
|
249
|
-
await fetch(
|
|
312
|
+
await fetch(projectBranchesRoute(slug, node, action), {
|
|
250
313
|
method: 'POST',
|
|
251
314
|
headers: { 'content-type': 'application/json' },
|
|
252
315
|
body: JSON.stringify(body),
|
|
253
316
|
}),
|
|
254
317
|
)) as View & {
|
|
255
|
-
result?: { name?: string; left?: { plugin: string; reason: string }[] };
|
|
318
|
+
result?: { name?: string; running?: string; left?: { plugin: string; reason: string }[] };
|
|
256
319
|
};
|
|
257
320
|
setView(answer);
|
|
258
321
|
if (action === 'new') {
|
|
@@ -260,6 +323,11 @@ export function Branches({ ctx, node }: { ctx: Context; node: string }) {
|
|
|
260
323
|
setNaming(false);
|
|
261
324
|
if (answer.result?.name) setSelected(answer.result.name);
|
|
262
325
|
}
|
|
326
|
+
if (answer.result?.running)
|
|
327
|
+
toast({
|
|
328
|
+
title: `${JOB_WORDS[answer.result.running]?.[0] ?? 'Running…'}`,
|
|
329
|
+
description: 'It runs on: its row says when it ends.',
|
|
330
|
+
});
|
|
263
331
|
if (action === 'activate' && answer.result?.left?.length)
|
|
264
332
|
toast({
|
|
265
333
|
variant: 'error',
|
|
@@ -307,6 +375,39 @@ export function Branches({ ctx, node }: { ctx: Context; node: string }) {
|
|
|
307
375
|
{spinning('new') ? 'Preparing…' : 'New branch'}
|
|
308
376
|
</Button>
|
|
309
377
|
</header>
|
|
378
|
+
{view.github === 'missing' && (
|
|
379
|
+
<p className="m-0 flex items-center gap-1.5 rounded-md border border-border px-3 py-2 text-xs text-secondary">
|
|
380
|
+
<Icon name="info" size={12} className="shrink-0" />
|
|
381
|
+
<span className="min-w-0 flex-1 break-words">
|
|
382
|
+
Pull requests need the GitHub plugin: every other action works without it.
|
|
383
|
+
</span>
|
|
384
|
+
<Button size="sm" variant="outline" disabled={installing} onClick={() => void installGitHub()}>
|
|
385
|
+
{installing ? 'Installing…' : 'Install GitHub'}
|
|
386
|
+
</Button>
|
|
387
|
+
</p>
|
|
388
|
+
)}
|
|
389
|
+
{view.jobs
|
|
390
|
+
?.filter((job) => job.running || job.error)
|
|
391
|
+
.map((job) => (
|
|
392
|
+
<p
|
|
393
|
+
key={job.name}
|
|
394
|
+
role={job.error ? 'alert' : undefined}
|
|
395
|
+
className={cn(
|
|
396
|
+
'm-0 flex items-start gap-1.5 rounded-md border px-3 py-2 text-xs',
|
|
397
|
+
job.error ? 'border-destructive/30 text-destructive' : 'border-border text-secondary',
|
|
398
|
+
)}
|
|
399
|
+
>
|
|
400
|
+
<Icon
|
|
401
|
+
name={job.error ? 'triangle-alert' : 'loader-circle'}
|
|
402
|
+
size={12}
|
|
403
|
+
className={cn('mt-0.5 shrink-0', !job.error && 'animate-spin motion-reduce:animate-none')}
|
|
404
|
+
/>
|
|
405
|
+
<span className="min-w-0 break-words">
|
|
406
|
+
<span className="font-mono">{job.name}</span>:{' '}
|
|
407
|
+
{job.error ? `${JOB_WORDS.new![1]}: ${job.error}` : JOB_WORDS.new![0]}
|
|
408
|
+
</span>
|
|
409
|
+
</p>
|
|
410
|
+
))}
|
|
310
411
|
{view.hostLevel?.refusal && (
|
|
311
412
|
<p className="m-0 flex items-start gap-1.5 rounded-md border border-border px-3 py-2 text-xs text-secondary">
|
|
312
413
|
<Icon name="info" size={12} className="mt-0.5 shrink-0" />
|
|
@@ -363,6 +464,20 @@ export function Branches({ ctx, node }: { ctx: Context; node: string }) {
|
|
|
363
464
|
Active
|
|
364
465
|
</Badge>
|
|
365
466
|
)}
|
|
467
|
+
{branch.job && (branch.job.running || branch.job.error) && (
|
|
468
|
+
<Badge
|
|
469
|
+
title={branch.job.error}
|
|
470
|
+
className={cn(
|
|
471
|
+
'h-4 shrink-0 gap-1 px-1.5 text-[11px]',
|
|
472
|
+
branch.job.error ? 'bg-destructive/10 text-destructive' : 'bg-muted text-secondary',
|
|
473
|
+
)}
|
|
474
|
+
>
|
|
475
|
+
{branch.job.running && (
|
|
476
|
+
<Icon name="loader-circle" size={10} className="animate-spin motion-reduce:animate-none" />
|
|
477
|
+
)}
|
|
478
|
+
{JOB_WORDS[branch.job.action]?.[branch.job.running ? 0 : 1] ?? branch.job.action}
|
|
479
|
+
</Badge>
|
|
480
|
+
)}
|
|
366
481
|
</TableCell>
|
|
367
482
|
<TableCell className="px-2">
|
|
368
483
|
<VsDefault branch={branch} base={base} scale={scale} />
|
|
@@ -382,7 +497,8 @@ export function Branches({ ctx, node }: { ctx: Context; node: string }) {
|
|
|
382
497
|
slug={slug}
|
|
383
498
|
node={node}
|
|
384
499
|
branch={branch}
|
|
385
|
-
|
|
500
|
+
route={projectBranchesRoute}
|
|
501
|
+
disabled={busy || !!branch.job?.running}
|
|
386
502
|
onAnswer={(answer) =>
|
|
387
503
|
setView((was) => (was ? { ...was, branches: answer.branches } : was))
|
|
388
504
|
}
|
package/browser/overview.tsx
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
// A project's Overview (research-083): its home, which its row in the sidebar opens, its Sessions and
|
|
2
2
|
// Branches listed under it. Its folder and git state (`project`'s `/projects?git=1`), its branches and what each holds
|
|
3
|
-
// (`/
|
|
3
|
+
// (this plugin's `/branches`, a git project's), its latest sessions (`sessions`), each a link to its screen. The start of
|
|
4
4
|
// the project dashboard.
|
|
5
5
|
import * as React from 'react';
|
|
6
6
|
import { EmptyState, Icon, ScreenLink, isRecord, readJson } from '@bridge/ui';
|
|
7
7
|
import type { Context } from 'cordis';
|
|
8
8
|
import type {} from '@bridge/ui-renderer/client';
|
|
9
|
+
import { projectBranchesRoute } from './branches.tsx';
|
|
9
10
|
|
|
10
11
|
interface Project {
|
|
11
12
|
id: string;
|
|
@@ -99,7 +100,7 @@ export function Overview({ ctx, node }: { ctx: Context; node: string }) {
|
|
|
99
100
|
// Branches only for a repository of its own (the host refuses the others).
|
|
100
101
|
const listed = found?.git?.own
|
|
101
102
|
? await read(
|
|
102
|
-
|
|
103
|
+
projectBranchesRoute(slug, node),
|
|
103
104
|
)
|
|
104
105
|
: undefined;
|
|
105
106
|
if (gone) return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bridgebrew/project",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "A project is a folder of work with its bridge.yml, and a node with its own sessions: code, documents, anything; Bridge plugins can be made there too.",
|
|
6
6
|
"keywords": [
|
|
@@ -79,7 +79,14 @@
|
|
|
79
79
|
}
|
|
80
80
|
},
|
|
81
81
|
"deadlines": {
|
|
82
|
-
"/projects/new": 120000
|
|
82
|
+
"/projects/new": 120000,
|
|
83
|
+
"/branches/new": 120000,
|
|
84
|
+
"/branches/activate": 120000,
|
|
85
|
+
"/branches/commit": 120000,
|
|
86
|
+
"/branches/push": 120000,
|
|
87
|
+
"/branches/pull": 120000,
|
|
88
|
+
"/branches/pr": 120000,
|
|
89
|
+
"/branches/remove": 120000
|
|
83
90
|
},
|
|
84
91
|
"provenance": "official"
|
|
85
92
|
},
|
|
@@ -98,6 +105,6 @@
|
|
|
98
105
|
"typecheck": "bridge plugin check ."
|
|
99
106
|
},
|
|
100
107
|
"engines": {
|
|
101
|
-
"bridge": ">=0.0.1-nightly.
|
|
108
|
+
"bridge": ">=0.0.1-nightly.20261118.2"
|
|
102
109
|
}
|
|
103
110
|
}
|
package/server.ts
CHANGED
|
@@ -19,6 +19,7 @@ import { readdirSync, realpathSync } from 'node:fs';
|
|
|
19
19
|
import { isAbsolute, join, relative, resolve } from 'node:path';
|
|
20
20
|
import type { Context } from 'cordis';
|
|
21
21
|
import { z } from 'zod';
|
|
22
|
+
import { createBranches, processGit, type GitHubLike } from './branches.ts';
|
|
22
23
|
|
|
23
24
|
const folder = z.strictObject({ path: z.string(), title: z.string() });
|
|
24
25
|
type Folder = z.infer<typeof folder>;
|
|
@@ -37,11 +38,15 @@ const newBody = z.union([
|
|
|
37
38
|
clone: z.string().min(1).optional(),
|
|
38
39
|
}),
|
|
39
40
|
]);
|
|
41
|
+
/** How long a model may take to write a commit message or a pull request's texts (Sessions' own route's deadline). */
|
|
42
|
+
const WRITE_MS = 120_000;
|
|
43
|
+
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
44
|
+
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
40
45
|
/** Folders never offered as a project: Bridge's and tools' own. */
|
|
41
46
|
const hidden = new Set(['node_modules', '.git', '.bridge']);
|
|
42
47
|
|
|
43
48
|
export const name = 'project';
|
|
44
|
-
export const inject = ['router', 'storage', 'nodes', 'bridgeNodes'];
|
|
49
|
+
export const inject = ['router', 'storage', 'nodes', 'bridgeNodes', 'processes', 'workspace'];
|
|
45
50
|
|
|
46
51
|
/** A 4xx the route answers itself (the router's `clientError` brand). */
|
|
47
52
|
const refused = (code: string, message: string, status = 400, details?: object) =>
|
|
@@ -249,6 +254,85 @@ export async function apply(ctx: Context) {
|
|
|
249
254
|
input: newBody,
|
|
250
255
|
handle: add(true),
|
|
251
256
|
});
|
|
257
|
+
// A project's branches (./branches.ts, bridgebrew/bridge#557): the Branches screen's routes, never an agent's (the
|
|
258
|
+
// person's own screen), and the `projectBranches` service Sessions makes a session's branch with.
|
|
259
|
+
const branches = createBranches({
|
|
260
|
+
slug: ctx.workspace.slug,
|
|
261
|
+
dataDir: join(ctx.workspace.dataDir, name),
|
|
262
|
+
git: processGit(ctx),
|
|
263
|
+
projects: () => ctx.bridgeNodes.projects(),
|
|
264
|
+
codeFrom: (input) => ctx.bridgeNodes.codeFrom(input),
|
|
265
|
+
follow: (input) => ctx.bridgeNodes.follow(input),
|
|
266
|
+
install: (folder) => ctx.bridgeNodes.install({ folder }),
|
|
267
|
+
removeWorktree: (input) => ctx.bridgeNodes.removeWorktree(input),
|
|
268
|
+
pluginsRepository: (plugins) => ctx.bridgeNodes.pluginsRepository({ plugins }),
|
|
269
|
+
write: async (node, prompt, sessions) => {
|
|
270
|
+
const response = await ctx.router.dispatch(
|
|
271
|
+
{
|
|
272
|
+
method: 'POST',
|
|
273
|
+
key: 'sessions',
|
|
274
|
+
path: '/sessions/write',
|
|
275
|
+
query: new URLSearchParams(),
|
|
276
|
+
headers: { 'x-bridge-node': node, 'content-type': 'application/json' },
|
|
277
|
+
body: new TextEncoder().encode(JSON.stringify({ prompt, sessions })),
|
|
278
|
+
actor: 'user',
|
|
279
|
+
},
|
|
280
|
+
AbortSignal.timeout(WRITE_MS),
|
|
281
|
+
);
|
|
282
|
+
if (!('body' in response)) {
|
|
283
|
+
response.abort();
|
|
284
|
+
throw new Error('Sessions answered a stream');
|
|
285
|
+
}
|
|
286
|
+
const body: unknown = response.body ? JSON.parse(response.body) : undefined;
|
|
287
|
+
const text = isRecord(body) ? body.text : undefined;
|
|
288
|
+
if (response.status === 200 && typeof text === 'string') return text;
|
|
289
|
+
const error = isRecord(body) && isRecord(body.error) ? body.error.message : undefined;
|
|
290
|
+
throw new Error(typeof error === 'string' ? error : `Sessions did not write it (${response.status})`);
|
|
291
|
+
},
|
|
292
|
+
github: () => {
|
|
293
|
+
const service: unknown = ctx.get('github', false);
|
|
294
|
+
return isRecord(service) &&
|
|
295
|
+
typeof service.repositoryOfFolder === 'function' &&
|
|
296
|
+
typeof service.pullRequestsForBranch === 'function' &&
|
|
297
|
+
typeof service.rest === 'function'
|
|
298
|
+
? // oxlint-disable-next-line bridge/no-new-unchecked-cast -- its three functions checked above, `github`'s own service
|
|
299
|
+
(service as unknown as GitHubLike)
|
|
300
|
+
: undefined;
|
|
301
|
+
},
|
|
302
|
+
});
|
|
303
|
+
const nodeOf = (query: URLSearchParams, here: string) => query.get('node') || here;
|
|
304
|
+
ctx.router.route(ctx, {
|
|
305
|
+
method: 'GET',
|
|
306
|
+
path: '/branches',
|
|
307
|
+
agent: false,
|
|
308
|
+
description: "A git project's branches (?node=): { project, github, hostLevel?, branches, jobs? }",
|
|
309
|
+
query: z.object({ node: z.string().optional() }),
|
|
310
|
+
handle: async ({ query, node }) => branches.view(nodeOf(query, node)),
|
|
311
|
+
});
|
|
312
|
+
for (const action of ['new', 'activate', 'commit', 'push', 'pull', 'pr', 'remove'])
|
|
313
|
+
ctx.router.route(ctx, {
|
|
314
|
+
method: 'POST',
|
|
315
|
+
path: `/branches/${action}`,
|
|
316
|
+
agent: false,
|
|
317
|
+
description: `${action} on a git project's branch (?node=): the branches after it, and { result }`,
|
|
318
|
+
query: z.object({ node: z.string().optional() }),
|
|
319
|
+
handle: async ({ query, node, body }) => {
|
|
320
|
+
let input: unknown = {};
|
|
321
|
+
try {
|
|
322
|
+
input = JSON.parse(new TextDecoder().decode(body ?? new Uint8Array()) || '{}');
|
|
323
|
+
} catch {}
|
|
324
|
+
try {
|
|
325
|
+
return await branches.act(nodeOf(query, node), action, isRecord(input) ? input : {});
|
|
326
|
+
} catch (error) {
|
|
327
|
+
throw refused('refused', error instanceof Error ? error.message : String(error), 409);
|
|
328
|
+
}
|
|
329
|
+
},
|
|
330
|
+
});
|
|
331
|
+
ctx.provide('projectBranches', {
|
|
332
|
+
newBranch: branches.newBranch,
|
|
333
|
+
branchForPlugins: branches.branchForPlugins,
|
|
334
|
+
});
|
|
335
|
+
|
|
252
336
|
// Every project the files list has its facet from the start (one made by hand while this plugin was stopped).
|
|
253
337
|
await listed().catch(() => {});
|
|
254
338
|
}
|