@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.
Files changed (44) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +43 -2
  3. package/answers.ts +167 -0
  4. package/browser/branch-actions.tsx +166 -0
  5. package/browser/changes.tsx +415 -0
  6. package/browser/children.tsx +650 -0
  7. package/browser/conversation.tsx +2381 -0
  8. package/browser/decision-questions.tsx +191 -0
  9. package/browser/elapsed.tsx +44 -0
  10. package/browser/gallery.tsx +218 -0
  11. package/browser/index.tsx +3434 -0
  12. package/browser/media-origins.tsx +38 -0
  13. package/browser/mobile.tsx +663 -0
  14. package/browser/open-link.tsx +30 -0
  15. package/browser/pull-request.tsx +116 -0
  16. package/browser/questions.tsx +156 -0
  17. package/browser/session-scheduled.tsx +203 -0
  18. package/browser/subagent-view.tsx +136 -0
  19. package/browser/subagents.tsx +339 -0
  20. package/browser/tabs.tsx +228 -0
  21. package/browser/tsconfig.json +15 -0
  22. package/browser/views/view.tsx +1064 -0
  23. package/browser/views.tsx +166 -0
  24. package/contract.ts +287 -0
  25. package/delegations.ts +1975 -0
  26. package/diff.ts +88 -0
  27. package/docs/en/images.mdx +51 -0
  28. package/docs/en/index.mdx +22 -0
  29. package/docs/en/meta.json +4 -0
  30. package/docs/en/organize-sessions.mdx +53 -0
  31. package/docs/en/start-a-session.mdx +63 -0
  32. package/docs/meta.json +4 -0
  33. package/git.ts +223 -0
  34. package/listing.ts +382 -0
  35. package/package.json +1023 -4
  36. package/peers.ts +677 -0
  37. package/pull-request-watch.ts +282 -0
  38. package/pull-requests.ts +606 -0
  39. package/server.ts +5920 -0
  40. package/text.ts +19 -0
  41. package/tsconfig.json +3 -0
  42. package/viewer-activity.ts +57 -0
  43. package/views/examples.ts +153 -0
  44. package/views/library.ts +1116 -0
@@ -0,0 +1,166 @@
1
+ // research-036 (OpenUI POC): a view the agent showed with `show_view`, drawn with the kit in the conversation. Its queries
2
+ // and mutations call the workspace's routes as the user; `@ToAssistant` puts its text in the composer.
3
+ import * as React from 'react';
4
+ import { Button, EmptyState, HttpError, Icon, refusalOf } from '@bridge/ui';
5
+ import type { PluginComponent } from '../views/library.ts';
6
+ import { View, type ViewComponentRenderer, type ViewTools } from './views/view.tsx';
7
+
8
+ /** An answer's JSON body (none when it is empty); a refused answer throws its refusal. */
9
+ const json = async (response: Response): Promise<unknown> => {
10
+ const text = await response.text();
11
+ const body: unknown = text ? JSON.parse(text) : undefined;
12
+ if (!response.ok) throw new HttpError(response.status, refusalOf(body, response.status));
13
+ return body;
14
+ };
15
+
16
+ /** The routes of workspace `slug` a view reads and writes, through `fetch` (the page's `workspace.fetch`). */
17
+ export const viewTools = (
18
+ slug: string,
19
+ fetch: (url: string, init?: RequestInit) => Promise<Response>,
20
+ ): ViewTools => {
21
+ const url = (plugin: string, path: string, query?: Record<string, string>) =>
22
+ `/${slug}/api/${plugin}${path}${query && Object.keys(query).length ? `?${new URLSearchParams(query)}` : ''}`;
23
+ return {
24
+ read: (plugin, path, query) => fetch(url(plugin, path, query)).then(json),
25
+ write: (plugin, method, path, body, query) =>
26
+ fetch(url(plugin, path, query), {
27
+ method,
28
+ ...(body !== undefined && {
29
+ headers: { 'content-type': 'application/json' },
30
+ body: JSON.stringify(body),
31
+ }),
32
+ }).then(json),
33
+ };
34
+ };
35
+
36
+ export function ViewLine({
37
+ seq,
38
+ title,
39
+ program,
40
+ base,
41
+ snapshot,
42
+ at,
43
+ dynamic,
44
+ streaming,
45
+ components,
46
+ tools,
47
+ renderComponent,
48
+ onAssistant,
49
+ }: {
50
+ seq: number;
51
+ title?: string;
52
+ program: string;
53
+ base?: number;
54
+ /** A frozen view's query answers, and when it was frozen (research-036 P5): it draws from them, no route called. */
55
+ snapshot?: Record<string, unknown> | undefined;
56
+ at?: string | undefined;
57
+ /** A dynamic view: its queries run each time it is shown. */
58
+ dynamic?: boolean | undefined;
59
+ /** A view the agent is still writing: its program grows as the call's input comes (research-036 streaming). */
60
+ streaming?: boolean | undefined;
61
+ /** The declarations of the plugin components the view calls (research-036 P2). */
62
+ components?: PluginComponent[] | undefined;
63
+ tools: ViewTools;
64
+ /** Draws a plugin component the view calls, through its plugin. */
65
+ renderComponent?: ViewComponentRenderer | undefined;
66
+ onAssistant: (text: string) => void;
67
+ }) {
68
+ // A frozen view's time, in the user's own clock (HH:MM).
69
+ const time = (() => {
70
+ if (at === undefined) return undefined;
71
+ const when = new Date(at);
72
+ if (Number.isNaN(when.getTime())) return undefined;
73
+ return `${String(when.getHours()).padStart(2, '0')}:${String(when.getMinutes()).padStart(2, '0')}`;
74
+ })();
75
+ return (
76
+ <section
77
+ data-who="view"
78
+ data-seq={seq}
79
+ aria-label={title ?? 'View'}
80
+ className="mt-3 overflow-hidden rounded-dialog border border-border-subtle"
81
+ >
82
+ <header className="flex h-9 items-center gap-2 border-b border-border-subtle bg-surface px-3 text-[12.5px] text-secondary">
83
+ <Icon name="layout-dashboard" size={14} className="text-muted" />
84
+ <span className="min-w-0 truncate">{title ?? 'View'}</span>
85
+ {streaming ? (
86
+ <span className="text-xs text-muted">· writing…</span>
87
+ ) : snapshot !== undefined && time !== undefined ? (
88
+ <span className="text-xs text-muted">· {time}</span>
89
+ ) : dynamic ? (
90
+ <span className="text-xs text-muted">· live</span>
91
+ ) : base !== undefined ? (
92
+ <span className="text-xs text-muted">· updated</span>
93
+ ) : null}
94
+ </header>
95
+ <div className="p-4">
96
+ <View
97
+ program={program}
98
+ snapshot={snapshot}
99
+ components={components}
100
+ renderComponent={renderComponent}
101
+ tools={tools}
102
+ streaming={streaming === true}
103
+ onAssistant={(text, context) =>
104
+ // In the composer, the context in plain sight: the user reads it, and sends it or not (owner's choice A).
105
+ onAssistant(
106
+ context === undefined
107
+ ? text
108
+ : `${text}\n\nContexte de la vue « ${title ?? 'View'} » : ${JSON.stringify(context)}`,
109
+ )
110
+ }
111
+ />
112
+ </div>
113
+ </section>
114
+ );
115
+ }
116
+
117
+ /**
118
+ * A view's component whose plugin is not active here: what it is, and the ask that proposes activating the plugin on
119
+ * the session's node (research-036 P2). An administrator approves it in Requests.
120
+ */
121
+ export function Missing({
122
+ plugin,
123
+ name,
124
+ session,
125
+ post,
126
+ }: {
127
+ plugin: string;
128
+ name: string;
129
+ session: string;
130
+ post: (path: string, body: object) => Promise<unknown>;
131
+ }) {
132
+ const [asked, setAsked] = React.useState(false);
133
+ const [refusal, setRefusal] = React.useState<string>();
134
+
135
+ return (
136
+ <EmptyState
137
+ data-slot="view-component-missing"
138
+ icon={<Icon name="puzzle" size={20} className="text-muted" />}
139
+ title={`${name} comes from the plugin ${plugin}, not active here.`}
140
+ className="rounded-dialog border border-border-subtle"
141
+ >
142
+ {asked ? (
143
+ <p>Asked: an administrator approves it in Requests.</p>
144
+ ) : (
145
+ <>
146
+ {refusal !== undefined && <p className="text-destructive">{refusal}</p>}
147
+ <div className="flex w-full justify-end">
148
+ <Button
149
+ variant="outline"
150
+ size="sm"
151
+ onClick={() => {
152
+ void post('/views/activate', { id: session, plugin })
153
+ .then(() => setAsked(true))
154
+ .catch((cause: unknown) => {
155
+ setRefusal(cause instanceof Error ? cause.message : String(cause));
156
+ });
157
+ }}
158
+ >
159
+ Ask to activate {plugin}
160
+ </Button>
161
+ </div>
162
+ </>
163
+ )}
164
+ </EmptyState>
165
+ );
166
+ }
package/contract.ts ADDED
@@ -0,0 +1,287 @@
1
+ // The contract of the `sessions` key (research-087 §7.2, bridge#395): what the host and other plugins call on whatever
2
+ // plugin answers as `sessions`, and what it keeps for them. Bridge's built-in Sessions and `@bridgebrew/sessions` (the
3
+ // same key, which replaces it in one operation) both keep it; `packages/host/test/sessions-contract.test.ts` checks it
4
+ // on a real workspace, against either (`BRIDGE_SESSIONS_PLUGIN`).
5
+ //
6
+ // Its data: the storage domains `sessions/sessions` (objects, the session records), `sessions/history` (a log, one stream
7
+ // per session), `sessions/pins` (objects) and `sessions/gallery` (a log), version 1, private to the key: the next plugin
8
+ // answering as `sessions` reads what the last one wrote. A newer stored version than the declared one is refused at
9
+ // start, so a version change is a contract change.
10
+ //
11
+ // Beyond routes, what others name:
12
+ // - its screen `sessions` (Point and project open `{ plugin: 'sessions', id: 'sessions' }`, a session as its item);
13
+ // - the page slot `sessions.tab` it draws beside a conversation, `{ session, node, Tab }` for each entry (bridge-plugins'
14
+ // browser fills it);
15
+ // - the folder `<workspace>/.bridge/data/sessions/workspace/`, which the host serves media from (`http-files.ts`): the
16
+ // agents' scratch folder of sessions made before research-061;
17
+ // - the host lends a workspace's Sessions to its stages and keeps it disabled in them (`stage.ts` `lentPlugins`,
18
+ // `inertPlugins`), and keeps its tools for a session bound to a stage (`mcp.ts` `SESSION_PLUGINS`); a new project's
19
+ // node activates it (`bridge-record.ts`).
20
+ // Every page route reads the node from the header `x-bridge-node` (`root` without it).
21
+ //
22
+ // Everything else of a sessions plugin is its own: its other routes, its page, its agent tools, the rest of its record.
23
+ // A change here is a contract change: Bridge first, then the plugins that implement or call it.
24
+
25
+ /** A route of the contract: the worker request `key: 'sessions'` with this method and path, or the page's
26
+ * `/<slug>/api/sessions<path>`. `agent: false`: only the user (the host, a page) may call it, never a session's agent. */
27
+ export interface ContractRoute {
28
+ method: 'GET' | 'POST' | 'DELETE';
29
+ path: string;
30
+ agent?: false;
31
+ /** Who calls it, and what it is for. */
32
+ why: string;
33
+ }
34
+
35
+ /** The routes the host calls (research-087 §1.1). */
36
+ export const hostRoutes = [
37
+ {
38
+ method: 'GET',
39
+ path: '/sessions/restart',
40
+ agent: false,
41
+ why: 'reload-gate, install-coordinator: is a turn running? `RestartActivity`',
42
+ },
43
+ {
44
+ method: 'POST',
45
+ path: '/sessions/restart',
46
+ agent: false,
47
+ why: 'reload-gate, install-coordinator: hold new turns for a reload or a restart, `RestartBody` → `{ ready }`',
48
+ },
49
+ {
50
+ method: 'DELETE',
51
+ path: '/sessions/restart',
52
+ agent: false,
53
+ why: 'reload-gate, install-coordinator: release the hold; what was queued meanwhile starts',
54
+ },
55
+ {
56
+ method: 'POST',
57
+ path: '/sessions/queue',
58
+ agent: false,
59
+ why: "reload (`?id=`, `QueueBody` with from: 'bridge'): tell the session whose `bridge reload` ran how it went",
60
+ },
61
+ {
62
+ method: 'POST',
63
+ path: '/sessions/worktree',
64
+ why: 'mcp `stage_add` / `stage_remove`: bind the calling session to a stage, or back with null',
65
+ },
66
+ {
67
+ method: 'POST',
68
+ path: '/sessions/worktree-gone',
69
+ agent: false,
70
+ why: 'admin, before a stage goes: stop and unbind its sessions but `caller` → `{ sessions: string[] }`',
71
+ },
72
+ {
73
+ method: 'POST',
74
+ path: '/sessions/open-link',
75
+ why: "mcp `bridge_link` show: `{ path }` on the calling session's page → `Shown`; a caller that is no session: 403",
76
+ },
77
+ {
78
+ method: 'GET',
79
+ path: '/sessions',
80
+ why: "start (project-branches): each session's folder (`worktree`, `branch`): a branch in use is never removed",
81
+ },
82
+ {
83
+ method: 'POST',
84
+ path: '/sessions/write',
85
+ agent: false,
86
+ why: 'start (project-branches): `{ prompt, sessions? }` → `{ text, profile, model? }`; refused, the host writes its own',
87
+ },
88
+ {
89
+ method: 'GET',
90
+ path: '/sessions/models',
91
+ why: 'check: a 400 there is an agent not ready, not a failed screen',
92
+ },
93
+ ] as const satisfies readonly ContractRoute[];
94
+
95
+ /** The routes Bridge's own plugins and bridge-plugins' `browser` call (research-087 §1.3). */
96
+ export const pageRoutes = [
97
+ { method: 'GET', path: '/sessions', why: 'Point, project Overview (`?worktree=`): `Listing`' },
98
+ {
99
+ method: 'POST',
100
+ path: '/sessions',
101
+ agent: false,
102
+ why: 'Point, the prompt: a new session `{ profile, model?, mode?, options?, effort?, title?, target?, worktree?, branch?, newBranch? }` → `{ id, … }`',
103
+ },
104
+ {
105
+ method: 'GET',
106
+ path: '/sessions/watch',
107
+ agent: false,
108
+ why: 'Point: `?id=&after=` → server-sent events, one `WatchMessage` each',
109
+ },
110
+ {
111
+ method: 'GET',
112
+ path: '/sessions/events',
113
+ agent: false,
114
+ why: '`?id=&limit=&before=` → `{ events, live, more }`',
115
+ },
116
+ {
117
+ method: 'POST',
118
+ path: '/sessions/queue',
119
+ agent: false,
120
+ why: 'the prompt: `?id=` `QueueBody`; with no turn running it starts one',
121
+ },
122
+ {
123
+ method: 'POST',
124
+ path: '/sessions/turn',
125
+ agent: false,
126
+ why: '`?id=` `{ text, attachments? }`: one turn at a time',
127
+ },
128
+ {
129
+ method: 'POST',
130
+ path: '/sessions/answer',
131
+ agent: false,
132
+ why: 'Point: `?id=` `{ decision, option }` or `{ decision, answers }`',
133
+ },
134
+ {
135
+ method: 'POST',
136
+ path: '/sessions/interrupt',
137
+ agent: false,
138
+ why: 'the prompt: `?id=`, stop its turn',
139
+ },
140
+ {
141
+ method: 'GET',
142
+ path: '/sessions/models',
143
+ why: 'the prompt: `?profile=` → `[{ id, name, description?, current? }]`',
144
+ },
145
+ { method: 'GET', path: '/sessions/modes', why: 'the prompt: `?profile=` → the modes' },
146
+ {
147
+ method: 'POST',
148
+ path: '/sessions/mode',
149
+ agent: false,
150
+ why: 'the prompt: `?id=` `{ mode, options? }`',
151
+ },
152
+ {
153
+ method: 'POST',
154
+ path: '/sessions/effort',
155
+ agent: false,
156
+ why: 'the prompt: `?id=` `{ effort }`',
157
+ },
158
+ {
159
+ method: 'POST',
160
+ path: '/sessions/attach',
161
+ agent: false,
162
+ why: 'the prompt: `?id=`, the image as the body, its `content-type`, its name URI-encoded in `x-bridge-name` → `Attached`, which `/queue` then carries',
163
+ },
164
+ {
165
+ method: 'POST',
166
+ path: '/sessions/gallery',
167
+ why: "bridge-plugins' browser: `{ path, name? }` pins what it produced",
168
+ },
169
+ ] as const satisfies readonly ContractRoute[];
170
+
171
+ /** `GET /sessions/restart`: how many sessions have something under way (a turn preparing, running, or its end being
172
+ * saved), each named. `operations.length === active`; an answer of another shape never authorises a restart. */
173
+ export interface RestartActivity {
174
+ active: number;
175
+ operations: { id: string; title: string; state: 'preparing' | 'running' | 'saving' }[];
176
+ }
177
+ /** `POST /sessions/restart`: none or `{}`, ready only when idle; `wait`, hold new turns now and ready once the running
178
+ * ones ended; `force`, ready at once (`reload`: the plugin whose reload cuts the turns, named in their continuation).
179
+ * Ready or not, a `wait` or a ready answer holds new turns until `DELETE`. */
180
+ export interface RestartBody {
181
+ force?: boolean;
182
+ wait?: boolean;
183
+ reload?: string;
184
+ }
185
+ /** `POST /sessions/queue?id=`: a message for the session, sent at once when no turn runs, else after it. `from:
186
+ * 'bridge'`: Bridge's own words, marked so in its history (`user/message` with `from: 'bridge'`). */
187
+ export interface QueueBody {
188
+ text: string;
189
+ attachments?: Attached[];
190
+ from?: 'bridge';
191
+ }
192
+ /** What the user's page did with a page an agent asked to show. */
193
+ export type Shown = { shown: 'visible' | 'replaced' | 'opened' } | { shown: false; reason: string };
194
+
195
+ /** An image a message carries: uploaded by `POST /sessions/attach`, then named in `QueueBody.attachments`. */
196
+ export interface Attached {
197
+ blob: string;
198
+ size: number;
199
+ type: string;
200
+ name: string;
201
+ }
202
+ /** A session in `GET /sessions`'s `sessions`, the fields other plugins read; a sessions plugin may add more. */
203
+ export interface Listed {
204
+ id: string;
205
+ node: string;
206
+ title: string;
207
+ /** The harness profile (`harness`, its older name, carries the same). */
208
+ profile: string;
209
+ harness: string;
210
+ createdAt: string;
211
+ updatedAt?: string;
212
+ model?: string;
213
+ mode?: string;
214
+ effort?: string;
215
+ options?: Record<string, unknown>;
216
+ /** What the user pointed at when it started (Point reopens the session started on the same target). */
217
+ target?: Record<string, unknown>;
218
+ running: boolean;
219
+ /** A decision waits for the user. */
220
+ waiting: boolean;
221
+ /** The stage it works in. */
222
+ worktree?: { slug: string; folder: string };
223
+ /** The branch of its project it works on. */
224
+ branch?: { name: string; folder: string };
225
+ /** With `?touching=<path>`: one of its files is that path or under it. */
226
+ touches?: boolean;
227
+ archived?: boolean;
228
+ }
229
+ /** `GET /sessions` (`?archived=1`, `?worktree=<slug>`, `?touching=<path>`): the sessions of the node of `x-bridge-node`
230
+ * and of its descendants (each says its node), newest activity first, with the harnesses the prompt offers there. */
231
+ export interface Listing {
232
+ project: { path: string } | null;
233
+ workspace: { slug: string; folder: string } | null;
234
+ harnesses: ({ name: string; available: boolean; enabled?: boolean } & Record<string, unknown>)[];
235
+ sessions: Listed[];
236
+ }
237
+
238
+ /** A history line as routes and watchers carry it: `seq` is its number in the session's log. The types other plugins
239
+ * read: `turn/start`, `user/message` (`text`, `from?`), `assistant/message` (`text`), `decision/asked` (`id`),
240
+ * `turn/end` (`reason`). */
241
+ export type Line = { seq: number; type: string; id?: string } & Record<string, unknown>;
242
+ /** The running turn's part not stored yet, in a watch `snapshot`: the text being written and the decisions open (Point
243
+ * shows both). */
244
+ export interface Live {
245
+ text: string;
246
+ decisions: ({ id: string } & Record<string, unknown>)[];
247
+ }
248
+ /** One message of `GET /sessions/watch`, one server-sent event each (`data: <JSON>`): first the stored lines past `after` (`stored`, the
249
+ * older ones when they do not fit one `snapshot`), then `snapshot` (`live`: the running turn's part not stored yet, or
250
+ * null), then each line stored (`stored`), each live event of a running turn (`live`), a queue change (`queue`), a page
251
+ * to show (`open-link`, answered at `POST /sessions/open-link/answer`), and `ping` when idle. */
252
+ export type WatchMessage =
253
+ | { type: 'stored'; event: Line }
254
+ | {
255
+ type: 'snapshot';
256
+ events: Line[];
257
+ live: (Live & Record<string, unknown>) | null;
258
+ more?: boolean;
259
+ }
260
+ /** Point reads `text` (`{ type: 'text', text }`, appended to the live text) and `decision` (`{ type: 'decision', id, …
261
+ * }`, a decision asked); a stored `decision/answered` (`id`) or `turn/end` closes it. */
262
+ | { type: 'live'; event: { type: string } & Record<string, unknown> }
263
+ | { type: 'queue' }
264
+ | { type: 'open-link'; path: string; turn?: string; request: string }
265
+ | { type: 'ping' };
266
+
267
+ /** A session as another plugin's server sees it (`ctx.sessions.describe`): no content (preview, target, history). */
268
+ export interface SessionView {
269
+ id: string;
270
+ node: string;
271
+ title: string;
272
+ harness: string;
273
+ model?: string;
274
+ archived: boolean;
275
+ running: boolean;
276
+ waiting: boolean;
277
+ updatedAt?: string;
278
+ }
279
+ /** The `sessions` service (`ctx.sessions`). Bridge-plugins' `browser` reads titles and closes the browsers of archived
280
+ * sessions with it. */
281
+ export interface SessionsService {
282
+ /** Session `id` as other plugins may see it, `undefined` when there is none. */
283
+ describe(id: string): Promise<SessionView | undefined>;
284
+ }
285
+
286
+ /** The scheduler's action types a sessions plugin registers, which the scheduler's own tasks name (`tasks.ts`). */
287
+ export const schedulerActions = ['sessions.start', 'sessions.prompt'] as const;