@bridgebrew/point 0.0.0-stage → 0.0.1
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/browser/browser.tsx +1056 -0
- package/browser/near.ts +31 -0
- package/browser/place.ts +135 -0
- package/browser/started.ts +35 -0
- package/browser/target.ts +256 -0
- package/browser/tsconfig.json +15 -0
- package/package.json +54 -4
- package/tsconfig.json +1 -0
- package/README.md +0 -3
|
@@ -0,0 +1,1056 @@
|
|
|
1
|
+
// Point, then ask (research-009): hold Option+Shift (⌥⇧) and the pointer selects. Hover frames the element under it, a
|
|
2
|
+
// click selects it, a drag draws a zone; Option+Shift+Cmd (⌥⇧⌘; Alt+Shift+Ctrl off macOS) takes the finest element (an
|
|
3
|
+
// icon's path, a span) instead of its control. Option alone is the window manager's (⌥ drag moves a window from
|
|
4
|
+
// anywhere in it, research-018): Point leaves it to the page. Its gestures are declared to the shell's shortcut registry
|
|
5
|
+
// (`shell.shortcuts.gesture`), which lists them in the help and refuses a clash. Everything is selectable, this
|
|
6
|
+
// plugin's own windows too. Each selection keeps its frame and a bar: Edit opens
|
|
7
|
+
// a window to ask the agent about it, Delete hides it now and asks the agent to delete it in code, × deselects.
|
|
8
|
+
// A window is a session (`sessions`) placed where the selection's code lives (./place.ts), with the selection as its
|
|
9
|
+
// `target`; windows move by their title bar and resize by their corner. The dock (bottom right) lists the sessions
|
|
10
|
+
// started here, latest first, and starts one with no target ("New session": the workspace session). A window lists the
|
|
11
|
+
// sessions about its target, to reopen one and read why the code is as it is: those started on it (the latest reopens
|
|
12
|
+
// by itself) and those that changed its plugin's code (`?touching=` its folder).
|
|
13
|
+
// Fills `shell`'s `overlay`; a `_host` plugin, so it is on every page.
|
|
14
|
+
import * as React from 'react';
|
|
15
|
+
import {
|
|
16
|
+
Button,
|
|
17
|
+
Icon,
|
|
18
|
+
PaneNote,
|
|
19
|
+
Select,
|
|
20
|
+
SelectContent,
|
|
21
|
+
SelectItem,
|
|
22
|
+
SelectTrigger,
|
|
23
|
+
cn,
|
|
24
|
+
readJson,
|
|
25
|
+
} from '@bridge/ui';
|
|
26
|
+
import type { Context } from 'cordis';
|
|
27
|
+
import { cutText } from '@bridge/kernel/text';
|
|
28
|
+
import type {} from '@bridge/ui-renderer/client';
|
|
29
|
+
import { SessionPrompt } from '@bridge/session-ui/prompt';
|
|
30
|
+
import { near, type Side } from './near.ts';
|
|
31
|
+
import { readStarted, SESSIONS_KEY, type Started } from './started.ts';
|
|
32
|
+
import { placeOf, type Place } from './place.ts';
|
|
33
|
+
import {
|
|
34
|
+
canDeleteTarget,
|
|
35
|
+
elementTarget,
|
|
36
|
+
labelOf,
|
|
37
|
+
pick,
|
|
38
|
+
pluginOf,
|
|
39
|
+
sameTarget,
|
|
40
|
+
zoneTarget,
|
|
41
|
+
type Here,
|
|
42
|
+
type Target,
|
|
43
|
+
} from './target.ts';
|
|
44
|
+
|
|
45
|
+
export const name = 'point';
|
|
46
|
+
export const inject = ['slots', 'shell', 'hostNodes', 'streams'];
|
|
47
|
+
|
|
48
|
+
/** A pointer move past this many pixels with the button down draws a zone. */
|
|
49
|
+
const DRAG = 6;
|
|
50
|
+
/** macOS: ⌘ picks the finest element; elsewhere Ctrl (the platform told as `windows` tells it). */
|
|
51
|
+
const mac =
|
|
52
|
+
typeof navigator !== 'undefined' &&
|
|
53
|
+
/Mac|iPhone|iPad/.test(navigator.platform || navigator.userAgent);
|
|
54
|
+
/** Point's modifiers held: ⌥⇧ (Alt+Shift). */
|
|
55
|
+
const pointing = (event: { altKey: boolean; shiftKey: boolean }) => event.altKey && event.shiftKey;
|
|
56
|
+
/** The finest element wanted: ⌥⇧⌘ (Alt+Shift+Ctrl off macOS). */
|
|
57
|
+
const finest = (event: { metaKey: boolean; ctrlKey: boolean }) =>
|
|
58
|
+
mac ? event.metaKey : event.ctrlKey;
|
|
59
|
+
/** Point's gestures, declared to the shell's registry (the help lists them, a clash is refused). */
|
|
60
|
+
const gestures = [
|
|
61
|
+
{
|
|
62
|
+
modifiers: 'Alt+Shift',
|
|
63
|
+
action: 'click',
|
|
64
|
+
label: 'Point: select what the pointer frames, to ask about it',
|
|
65
|
+
},
|
|
66
|
+
{ modifiers: 'Alt+Shift', action: 'drag', label: 'Point: draw a zone to ask about' },
|
|
67
|
+
{
|
|
68
|
+
modifiers: mac ? 'Alt+Shift+Meta' : 'Alt+Shift+Ctrl',
|
|
69
|
+
action: 'click',
|
|
70
|
+
label: "Point: select the finest element (an icon's path, a span)",
|
|
71
|
+
},
|
|
72
|
+
] as const;
|
|
73
|
+
/** Windows and the dock: under the kit's popups (`z-50`), so their own menus open over them. */
|
|
74
|
+
const UNDER_POPUPS = 40;
|
|
75
|
+
|
|
76
|
+
interface Selection {
|
|
77
|
+
id: number;
|
|
78
|
+
element?: Element;
|
|
79
|
+
target: Target;
|
|
80
|
+
label: string;
|
|
81
|
+
}
|
|
82
|
+
interface Win {
|
|
83
|
+
id: number;
|
|
84
|
+
x: number;
|
|
85
|
+
y: number;
|
|
86
|
+
target?: Target;
|
|
87
|
+
label: string;
|
|
88
|
+
intent?: 'delete';
|
|
89
|
+
started?: Started | undefined;
|
|
90
|
+
/** The side of its selection it opened on; above it, `bottom` is where its bottom stays as its height changes. */
|
|
91
|
+
side?: Side;
|
|
92
|
+
bottom?: number;
|
|
93
|
+
}
|
|
94
|
+
type Stored = {
|
|
95
|
+
seq: number;
|
|
96
|
+
type: string;
|
|
97
|
+
text?: string;
|
|
98
|
+
id?: string;
|
|
99
|
+
title?: string;
|
|
100
|
+
status?: string;
|
|
101
|
+
options?: { id: string; name: string }[];
|
|
102
|
+
reason?: string;
|
|
103
|
+
};
|
|
104
|
+
/** A decision a session asks, answered from its conversation. */
|
|
105
|
+
interface Decision {
|
|
106
|
+
id: string;
|
|
107
|
+
title: string;
|
|
108
|
+
options: { id: string; name: string }[];
|
|
109
|
+
}
|
|
110
|
+
/** A message of a session's watch stream (`GET /sessions/watch`): its snapshot, a stored line, a live one, a ping. */
|
|
111
|
+
type Watched =
|
|
112
|
+
| { type: 'snapshot'; events: Stored[]; live?: { text?: string; decisions?: Decision[] } | null }
|
|
113
|
+
| { type: 'stored'; event: Stored }
|
|
114
|
+
| { type: 'live'; event: { type: 'text'; text: string } | ({ type: 'decision' } & Decision) }
|
|
115
|
+
| { type: 'ping' };
|
|
116
|
+
|
|
117
|
+
const api = (slug: string) => `/${encodeURIComponent(slug)}/api/sessions/sessions`;
|
|
118
|
+
|
|
119
|
+
/** Where `selection` is in the viewport: its element's box, else the box its target kept (a zone). */
|
|
120
|
+
const rectOf = ({ element, target }: Selection) =>
|
|
121
|
+
element
|
|
122
|
+
? element.getBoundingClientRect()
|
|
123
|
+
: {
|
|
124
|
+
left: target.box.x - scrollX,
|
|
125
|
+
top: target.box.y - scrollY,
|
|
126
|
+
width: target.box.width,
|
|
127
|
+
height: target.box.height,
|
|
128
|
+
};
|
|
129
|
+
/** The floating prompt's size, before a session opens the whole window. */
|
|
130
|
+
const PROMPT_WIDTH = 380;
|
|
131
|
+
const PROMPT_HEIGHT = 140;
|
|
132
|
+
|
|
133
|
+
/** The frame drawn over a viewport rect. */
|
|
134
|
+
function Frame({
|
|
135
|
+
rect,
|
|
136
|
+
className,
|
|
137
|
+
children,
|
|
138
|
+
}: {
|
|
139
|
+
rect: { left: number; top: number; width: number; height: number };
|
|
140
|
+
className?: string;
|
|
141
|
+
children?: React.ReactNode;
|
|
142
|
+
}) {
|
|
143
|
+
return (
|
|
144
|
+
<div
|
|
145
|
+
className={cn(
|
|
146
|
+
'pointer-events-none fixed z-[1000] rounded-[3px] outline-2 outline-offset-1',
|
|
147
|
+
className,
|
|
148
|
+
)}
|
|
149
|
+
style={{ left: rect.left, top: rect.top, width: rect.width, height: rect.height }}
|
|
150
|
+
>
|
|
151
|
+
{children}
|
|
152
|
+
</div>
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** One selection: its frame where its element is now, and its bar. */
|
|
157
|
+
function Selected({
|
|
158
|
+
selection,
|
|
159
|
+
onEdit,
|
|
160
|
+
onDelete,
|
|
161
|
+
onClose,
|
|
162
|
+
}: {
|
|
163
|
+
selection: Selection;
|
|
164
|
+
onEdit: () => void;
|
|
165
|
+
onDelete: () => void;
|
|
166
|
+
onClose: () => void;
|
|
167
|
+
}) {
|
|
168
|
+
const { element, target } = selection;
|
|
169
|
+
const rect = rectOf(selection);
|
|
170
|
+
return (
|
|
171
|
+
<Frame rect={rect} className="outline-accent">
|
|
172
|
+
<div
|
|
173
|
+
data-point-bar
|
|
174
|
+
// Above the element, below it near the top edge; from its left edge near the left of the page.
|
|
175
|
+
className={cn(
|
|
176
|
+
'pointer-events-auto absolute flex items-center gap-0.5 rounded-md border border-border bg-surface p-0.5 shadow-md',
|
|
177
|
+
rect.top < 40 ? 'top-full mt-1.5' : '-top-8',
|
|
178
|
+
rect.left + rect.width < 260 ? 'left-0' : 'right-0',
|
|
179
|
+
)}
|
|
180
|
+
>
|
|
181
|
+
<span className="max-w-48 truncate px-1.5 text-xs text-secondary">
|
|
182
|
+
{target.customizationSlot ?? target.plugin ?? 'page'} · {selection.label}
|
|
183
|
+
</span>
|
|
184
|
+
<Button
|
|
185
|
+
size="icon-sm"
|
|
186
|
+
variant="ghost"
|
|
187
|
+
aria-label="Ask about it"
|
|
188
|
+
disabled={!!target.customizationBlocked}
|
|
189
|
+
title={target.customizationBlocked}
|
|
190
|
+
onClick={onEdit}
|
|
191
|
+
>
|
|
192
|
+
<Icon name="square-pen" />
|
|
193
|
+
</Button>
|
|
194
|
+
{element && canDeleteTarget(target) && (
|
|
195
|
+
<Button size="icon-sm" variant="destructive" aria-label="Delete it" onClick={onDelete}>
|
|
196
|
+
<Icon name="trash" />
|
|
197
|
+
</Button>
|
|
198
|
+
)}
|
|
199
|
+
{target.customizationBlocked && (
|
|
200
|
+
<span role="status" className="max-w-72 px-1 text-xs text-secondary">
|
|
201
|
+
{target.customizationBlocked}
|
|
202
|
+
</span>
|
|
203
|
+
)}
|
|
204
|
+
<Button size="icon-sm" variant="ghost" aria-label="Deselect" onClick={onClose}>
|
|
205
|
+
<Icon name="x" />
|
|
206
|
+
</Button>
|
|
207
|
+
</div>
|
|
208
|
+
</Frame>
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** The page carrier's `open`, set by `apply` (a `_host` plugin has no `workspace`). */
|
|
213
|
+
let openStream: Context['streams']['open'];
|
|
214
|
+
|
|
215
|
+
/** A session's conversation, watched: messages, tool calls, and the running turn's text; open decisions answered here. */
|
|
216
|
+
/** The session's history and live turn; `onRunning` follows whether a turn runs (the prompt's Stop and queue). */
|
|
217
|
+
function Conversation({
|
|
218
|
+
started,
|
|
219
|
+
onRunning,
|
|
220
|
+
}: {
|
|
221
|
+
started: Started;
|
|
222
|
+
onRunning: (running: boolean) => void;
|
|
223
|
+
}) {
|
|
224
|
+
const [events, setEvents] = React.useState<Stored[]>([]);
|
|
225
|
+
const [text, setText] = React.useState('');
|
|
226
|
+
const [decisions, setDecisions] = React.useState<Decision[]>([]);
|
|
227
|
+
const viewport = React.useRef<HTMLDivElement>(null);
|
|
228
|
+
React.useEffect(() => {
|
|
229
|
+
let last = 0;
|
|
230
|
+
|
|
231
|
+
const add = (lines: Stored[]) => {
|
|
232
|
+
const fresh = lines.filter((line) => line.seq > last);
|
|
233
|
+
if (!fresh.length) return;
|
|
234
|
+
last = fresh.at(-1)!.seq;
|
|
235
|
+
setEvents((was) => [...was, ...fresh]);
|
|
236
|
+
// A settled segment leaves the running text.
|
|
237
|
+
if (fresh.some((line) => line.type === 'assistant/message' || line.type === 'turn/end'))
|
|
238
|
+
setText('');
|
|
239
|
+
if (fresh.some((line) => line.type === 'decision/answered' || line.type === 'turn/end'))
|
|
240
|
+
setDecisions((was) =>
|
|
241
|
+
was.filter(
|
|
242
|
+
(each) => !fresh.some((line) => line.type === 'turn/end' || line.id === each.id),
|
|
243
|
+
),
|
|
244
|
+
);
|
|
245
|
+
};
|
|
246
|
+
// On the page carrier, persistent: reopened from 0 after a drop, `add` keeps only what is new.
|
|
247
|
+
return openStream(`${api(started.slug)}/watch?id=${encodeURIComponent(started.id)}&after=0`, {
|
|
248
|
+
onItem: (value) => {
|
|
249
|
+
const data = value as Watched;
|
|
250
|
+
if (data.type === 'snapshot') {
|
|
251
|
+
add(data.events);
|
|
252
|
+
setText(data.live?.text ?? '');
|
|
253
|
+
setDecisions(data.live?.decisions ?? []);
|
|
254
|
+
} else if (data.type === 'stored') add([data.event]);
|
|
255
|
+
else if (data.type === 'live' && data.event.type === 'text') {
|
|
256
|
+
const chunk = data.event.text;
|
|
257
|
+
setText((was) => was + chunk);
|
|
258
|
+
} else if (data.type === 'live' && data.event.type === 'decision') {
|
|
259
|
+
const asked = data.event;
|
|
260
|
+
setDecisions((was) => [...was, asked]);
|
|
261
|
+
}
|
|
262
|
+
},
|
|
263
|
+
});
|
|
264
|
+
}, [started.slug, started.id]);
|
|
265
|
+
React.useLayoutEffect(() => {
|
|
266
|
+
const element = viewport.current;
|
|
267
|
+
if (element) element.scrollTop = element.scrollHeight;
|
|
268
|
+
}, [events, text, decisions]);
|
|
269
|
+
const answer = (decision: string, option: string | null) => {
|
|
270
|
+
void fetch(`${api(started.slug)}/answer?id=${encodeURIComponent(started.id)}`, {
|
|
271
|
+
method: 'POST',
|
|
272
|
+
headers: { 'content-type': 'application/json' },
|
|
273
|
+
body: JSON.stringify({ decision, option }),
|
|
274
|
+
});
|
|
275
|
+
};
|
|
276
|
+
const running =
|
|
277
|
+
events.findLast((each: Stored) => each.type === 'turn/start' || each.type === 'turn/end')
|
|
278
|
+
?.type === 'turn/start';
|
|
279
|
+
React.useEffect(() => onRunning(running), [running]);
|
|
280
|
+
return (
|
|
281
|
+
<div
|
|
282
|
+
ref={viewport}
|
|
283
|
+
className="flex min-h-0 flex-1 flex-col gap-2 overflow-auto px-3 py-2 text-sm [&>*]:shrink-0"
|
|
284
|
+
>
|
|
285
|
+
{events.map((each) => {
|
|
286
|
+
if (each.type === 'user/message')
|
|
287
|
+
return (
|
|
288
|
+
<div
|
|
289
|
+
key={each.seq}
|
|
290
|
+
className="self-end rounded-lg bg-hover px-2.5 py-1.5 whitespace-pre-wrap"
|
|
291
|
+
>
|
|
292
|
+
{each.text}
|
|
293
|
+
</div>
|
|
294
|
+
);
|
|
295
|
+
if (each.type === 'assistant/message')
|
|
296
|
+
return (
|
|
297
|
+
<div key={each.seq} className="whitespace-pre-wrap">
|
|
298
|
+
{each.text}
|
|
299
|
+
</div>
|
|
300
|
+
);
|
|
301
|
+
if (each.type === 'tool/call')
|
|
302
|
+
return (
|
|
303
|
+
<div key={each.seq} className="truncate text-xs text-muted">
|
|
304
|
+
<Icon name="wrench" size={12} className="mr-1 inline" />
|
|
305
|
+
{each.title}
|
|
306
|
+
</div>
|
|
307
|
+
);
|
|
308
|
+
if (each.type === 'notice')
|
|
309
|
+
return (
|
|
310
|
+
<div key={each.seq} className="text-xs text-warning">
|
|
311
|
+
{each.text}
|
|
312
|
+
</div>
|
|
313
|
+
);
|
|
314
|
+
if (each.type === 'turn/end' && each.reason === 'failed')
|
|
315
|
+
return (
|
|
316
|
+
<div key={each.seq} className="text-xs text-destructive">
|
|
317
|
+
The turn failed.
|
|
318
|
+
</div>
|
|
319
|
+
);
|
|
320
|
+
return null;
|
|
321
|
+
})}
|
|
322
|
+
{text && <div className="whitespace-pre-wrap">{text}</div>}
|
|
323
|
+
{decisions.map((decision) => (
|
|
324
|
+
<div key={decision.id} className="rounded-md border border-border p-2">
|
|
325
|
+
<div className="mb-1.5 text-xs">{decision.title}</div>
|
|
326
|
+
<div className="flex flex-wrap gap-1">
|
|
327
|
+
{decision.options.map((option) => (
|
|
328
|
+
<Button
|
|
329
|
+
key={option.id}
|
|
330
|
+
size="sm"
|
|
331
|
+
variant="outline"
|
|
332
|
+
onClick={() => answer(decision.id, option.id)}
|
|
333
|
+
>
|
|
334
|
+
{option.name}
|
|
335
|
+
</Button>
|
|
336
|
+
))}
|
|
337
|
+
</div>
|
|
338
|
+
</div>
|
|
339
|
+
))}
|
|
340
|
+
{running && !text && !decisions.length && (
|
|
341
|
+
<Icon
|
|
342
|
+
name="loader-circle"
|
|
343
|
+
size={14}
|
|
344
|
+
className="animate-spin text-muted motion-reduce:animate-none"
|
|
345
|
+
/>
|
|
346
|
+
)}
|
|
347
|
+
</div>
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/** A session of the listing (`GET /sessions`). */
|
|
352
|
+
interface Listed {
|
|
353
|
+
id: string;
|
|
354
|
+
node: string;
|
|
355
|
+
title: string;
|
|
356
|
+
harness: string;
|
|
357
|
+
mode?: string;
|
|
358
|
+
createdAt: string;
|
|
359
|
+
updatedAt?: string;
|
|
360
|
+
target?: Record<string, unknown>;
|
|
361
|
+
touches?: boolean;
|
|
362
|
+
tags?: string[];
|
|
363
|
+
archived?: boolean;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/** When `iso` was, shortly. */
|
|
367
|
+
const ago = (iso: string) => {
|
|
368
|
+
const minutes = Math.round((Date.now() - Date.parse(iso)) / 60_000);
|
|
369
|
+
return minutes < 1
|
|
370
|
+
? 'now'
|
|
371
|
+
: minutes < 60
|
|
372
|
+
? `${minutes} min`
|
|
373
|
+
: minutes < 1440
|
|
374
|
+
? `${Math.round(minutes / 60)} h`
|
|
375
|
+
: `${Math.round(minutes / 1440)} d`;
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
/** A floating window: a session about `win.target` (none: the workspace session). It opens the latest session already
|
|
379
|
+
* about that target, if any; its menu lists them all and "New session", which a first message starts. */
|
|
380
|
+
function Window({
|
|
381
|
+
win,
|
|
382
|
+
here,
|
|
383
|
+
onSession,
|
|
384
|
+
onClose,
|
|
385
|
+
onFront,
|
|
386
|
+
ctx,
|
|
387
|
+
}: {
|
|
388
|
+
win: Win;
|
|
389
|
+
here: Here;
|
|
390
|
+
ctx: Context;
|
|
391
|
+
/** The window shows `session`, or a new one to start (`undefined`). */
|
|
392
|
+
onSession: (session: Started | undefined) => void;
|
|
393
|
+
onClose: () => void;
|
|
394
|
+
onFront: () => void;
|
|
395
|
+
}) {
|
|
396
|
+
const [pos, setPos] = React.useState({ x: win.x, y: win.y });
|
|
397
|
+
const [place, setPlace] = React.useState<Place | undefined>();
|
|
398
|
+
const { shell } = ctx;
|
|
399
|
+
/** The prompt Point's "Prompt" setting names, Bridge-wide (none: the first registered), followed live. */
|
|
400
|
+
const promptChoice = React.useSyncExternalStore(ctx.hostNodes.subscribe, () => {
|
|
401
|
+
const value = ctx.hostNodes.settings('point', 'root')?.values.prompt?.value;
|
|
402
|
+
return typeof value === 'string' ? value : undefined;
|
|
403
|
+
});
|
|
404
|
+
const [error, setError] = React.useState('');
|
|
405
|
+
const [running, setRunning] = React.useState(false);
|
|
406
|
+
/** The prompt, given the delete intent's request; the title of the session its first message creates. */
|
|
407
|
+
const prompt = React.useRef<{
|
|
408
|
+
setText(text: string): void;
|
|
409
|
+
addText(session: string, text: string): void;
|
|
410
|
+
focus(): void;
|
|
411
|
+
openModels(): void;
|
|
412
|
+
}>(null);
|
|
413
|
+
const titled = React.useRef('');
|
|
414
|
+
const started = win.started;
|
|
415
|
+
/** The session shown last: a new chat started from it (another model) is where it was. */
|
|
416
|
+
const lastStarted = React.useRef(started);
|
|
417
|
+
if (started) lastStarted.current = started;
|
|
418
|
+
/** The sessions already about the target, latest first: started on it (`pointed`), or that changed its plugin's code. */
|
|
419
|
+
const [earlier, setEarlier] = React.useState<Listed[]>([]);
|
|
420
|
+
/** The latest earlier session was opened, or the user chose: no more automatic opening. */
|
|
421
|
+
const decided = React.useRef(!!win.started || !win.target);
|
|
422
|
+
const findPlace = () =>
|
|
423
|
+
placeOf(
|
|
424
|
+
win.target?.customizationOwner ?? win.target?.plugin,
|
|
425
|
+
win.target?.workspace ?? here.workspace,
|
|
426
|
+
).then(setPlace, (cause) =>
|
|
427
|
+
setPlace({ error: cause instanceof Error ? cause.message : String(cause) }),
|
|
428
|
+
);
|
|
429
|
+
React.useEffect(() => {
|
|
430
|
+
if (started && !win.target) return;
|
|
431
|
+
void findPlace();
|
|
432
|
+
}, []);
|
|
433
|
+
/** Where the session is, or will be: its slug and node. */
|
|
434
|
+
const at = started ?? (place && !('error' in place) ? place : undefined);
|
|
435
|
+
/** Where the prompt sends: `at`, else the last one (a new chat after a session reopened from the dock, which has no
|
|
436
|
+
* place loaded, starts where that session was). */
|
|
437
|
+
const lastAt = React.useRef(at);
|
|
438
|
+
if (at) lastAt.current = at;
|
|
439
|
+
const promptAt = at ?? lastAt.current;
|
|
440
|
+
const nodeHeaders = at && { 'content-type': 'application/json', 'x-bridge-node': at.node };
|
|
441
|
+
// The sessions there about the target (the latest started on it reopens by itself).
|
|
442
|
+
React.useEffect(() => {
|
|
443
|
+
if (!at) return;
|
|
444
|
+
const code = win.target && place && !('error' in place) ? place.path : undefined;
|
|
445
|
+
const touching = code ? `touching=${encodeURIComponent(code)}` : '';
|
|
446
|
+
// The archived ones too, to read why the code is as it is.
|
|
447
|
+
const archived: Promise<{ sessions: Listed[] }> = win.target
|
|
448
|
+
? fetch(`${api(at.slug)}?archived=1${touching && `&${touching}`}`, {
|
|
449
|
+
...(nodeHeaders ? { headers: nodeHeaders } : {}),
|
|
450
|
+
})
|
|
451
|
+
.then(readJson)
|
|
452
|
+
.then((body) => body as { sessions: Listed[] })
|
|
453
|
+
.catch(() => ({ sessions: [] }))
|
|
454
|
+
: Promise.resolve({ sessions: [] });
|
|
455
|
+
fetch(`${api(at.slug)}${touching && `?${touching}`}`, {
|
|
456
|
+
...(nodeHeaders ? { headers: nodeHeaders } : {}),
|
|
457
|
+
})
|
|
458
|
+
.then(readJson)
|
|
459
|
+
.then((body) => body as { sessions: Listed[] })
|
|
460
|
+
.then(
|
|
461
|
+
async (body) => {
|
|
462
|
+
const target = win.target;
|
|
463
|
+
const pointed = (each: Listed) =>
|
|
464
|
+
!!target && !!each.target && sameTarget(each.target, target);
|
|
465
|
+
const about = (each: Listed) => pointed(each) || !!each.touches;
|
|
466
|
+
const old = (await archived).sessions
|
|
467
|
+
.filter(about)
|
|
468
|
+
.map((each) => ({ ...each, archived: true }));
|
|
469
|
+
setEarlier(target ? [...body.sessions.filter(about), ...old] : []);
|
|
470
|
+
// Only a session started on this very target reopens by itself.
|
|
471
|
+
const latest = body.sessions.find(pointed);
|
|
472
|
+
if (!decided.current && latest && place && !('error' in place)) {
|
|
473
|
+
decided.current = true;
|
|
474
|
+
onSession(startedOf(latest, place));
|
|
475
|
+
}
|
|
476
|
+
},
|
|
477
|
+
(cause) => setError(cause instanceof Error ? cause.message : String(cause)),
|
|
478
|
+
);
|
|
479
|
+
}, [at?.slug, at?.node, started?.id]);
|
|
480
|
+
const drag = (event: React.PointerEvent) => {
|
|
481
|
+
if ((event.target as Element).closest('button')) return;
|
|
482
|
+
const from = { x: event.clientX - pos.x, y: event.clientY - top };
|
|
483
|
+
moved.current = true;
|
|
484
|
+
const move = (each: PointerEvent) =>
|
|
485
|
+
setPos({ x: Math.max(0, each.clientX - from.x), y: Math.max(0, each.clientY - from.y) });
|
|
486
|
+
window.addEventListener('pointermove', move);
|
|
487
|
+
window.addEventListener('pointerup', () => window.removeEventListener('pointermove', move), {
|
|
488
|
+
once: true,
|
|
489
|
+
});
|
|
490
|
+
};
|
|
491
|
+
// The prompt, focused once it shows; the delete intent's request in it.
|
|
492
|
+
React.useEffect(() => {
|
|
493
|
+
if (!at) return;
|
|
494
|
+
if (win.intent === 'delete' && !started)
|
|
495
|
+
prompt.current?.setText('Delete this element from the code.');
|
|
496
|
+
prompt.current?.focus();
|
|
497
|
+
}, [!!at]);
|
|
498
|
+
/** Only the prompt until a session shows; then the whole window, moved to stay on screen. */
|
|
499
|
+
const full = !!started;
|
|
500
|
+
const section = React.useRef<HTMLElement>(null);
|
|
501
|
+
/** Its height, measured; moved by hand, it stays where it was put. */
|
|
502
|
+
const [height, setHeight] = React.useState(PROMPT_HEIGHT);
|
|
503
|
+
const moved = React.useRef(false);
|
|
504
|
+
/** The prompt's height as it opened, until touched: it is never resized smaller than that. */
|
|
505
|
+
const [opened, setOpened] = React.useState<number>();
|
|
506
|
+
const touched = React.useRef(false);
|
|
507
|
+
React.useLayoutEffect(() => {
|
|
508
|
+
const measured = section.current?.offsetHeight;
|
|
509
|
+
if (measured && measured !== height) setHeight(measured);
|
|
510
|
+
if (!full && !touched.current && measured && measured !== opened) setOpened(measured);
|
|
511
|
+
});
|
|
512
|
+
/** Above its selection, unmoved: its bottom stays next to the selection, whatever its height. */
|
|
513
|
+
const anchored = win.side === 'above' && win.bottom !== undefined && !moved.current;
|
|
514
|
+
const top = anchored ? win.bottom! - height : pos.y;
|
|
515
|
+
React.useEffect(() => {
|
|
516
|
+
if (!full) return;
|
|
517
|
+
// The width the prompt was resized to stays; the window takes its own height.
|
|
518
|
+
section.current?.style.removeProperty('height');
|
|
519
|
+
setPos((was) => ({
|
|
520
|
+
x: Math.max(0, Math.min(was.x, window.innerWidth - 380 - 8)),
|
|
521
|
+
y: Math.max(0, Math.min(anchored ? win.bottom! - 420 : was.y, window.innerHeight - 420 - 8)),
|
|
522
|
+
}));
|
|
523
|
+
moved.current = true;
|
|
524
|
+
}, [full]);
|
|
525
|
+
const blocked =
|
|
526
|
+
win.target?.customizationBlocked ??
|
|
527
|
+
(win.intent === 'delete' && win.target && !canDeleteTarget(win.target)
|
|
528
|
+
? 'Customization slots cannot be deleted.'
|
|
529
|
+
: undefined);
|
|
530
|
+
const openInSessions = () => {
|
|
531
|
+
if (!started) return;
|
|
532
|
+
// The Sessions view shows the session it last showed on that node.
|
|
533
|
+
try {
|
|
534
|
+
localStorage.setItem(
|
|
535
|
+
`bridge.sessions.last.${JSON.stringify([started.slug, started.node])}`,
|
|
536
|
+
started.id,
|
|
537
|
+
);
|
|
538
|
+
} catch {}
|
|
539
|
+
shell.setCurrent(started.slug);
|
|
540
|
+
history.pushState(null, '', `/${started.slug}/`);
|
|
541
|
+
shell.setScreen({ plugin: 'sessions', id: 'sessions' }, started.node);
|
|
542
|
+
};
|
|
543
|
+
/** An earlier session as the window shows it. */
|
|
544
|
+
const startedOf = (each: Listed, at: { slug: string; label: string }): Started => ({
|
|
545
|
+
slug: at.slug,
|
|
546
|
+
node: each.node,
|
|
547
|
+
id: each.id,
|
|
548
|
+
title: each.title,
|
|
549
|
+
where: at.label,
|
|
550
|
+
at: new Date().toISOString(),
|
|
551
|
+
});
|
|
552
|
+
const choose = (id: string) => {
|
|
553
|
+
decided.current = true;
|
|
554
|
+
const chosen = earlier.find((each) => each.id === id);
|
|
555
|
+
if (chosen && place && !('error' in place)) onSession(startedOf(chosen, place));
|
|
556
|
+
else onSession(undefined);
|
|
557
|
+
};
|
|
558
|
+
/** The sessions already about this target: one is reopened, or a new one started. */
|
|
559
|
+
const sessionsAbout = (
|
|
560
|
+
<Select
|
|
561
|
+
items={{
|
|
562
|
+
new: 'New session',
|
|
563
|
+
...Object.fromEntries(earlier.map((each) => [each.id, each.title])),
|
|
564
|
+
}}
|
|
565
|
+
value={started?.id ?? 'new'}
|
|
566
|
+
onValueChange={(value) => choose(value as string)}
|
|
567
|
+
>
|
|
568
|
+
<SelectTrigger
|
|
569
|
+
size="sm"
|
|
570
|
+
aria-label="Sessions about this"
|
|
571
|
+
title="Sessions about this"
|
|
572
|
+
className="w-auto shrink-0 rounded-full text-xs"
|
|
573
|
+
>
|
|
574
|
+
<Icon name="messages-square" size={12} />
|
|
575
|
+
{earlier.length}
|
|
576
|
+
</SelectTrigger>
|
|
577
|
+
<SelectContent className="w-72">
|
|
578
|
+
<SelectItem value="new">
|
|
579
|
+
<span className="flex items-center gap-1.5">
|
|
580
|
+
<Icon name="plus" size={12} />
|
|
581
|
+
New session
|
|
582
|
+
</span>
|
|
583
|
+
</SelectItem>
|
|
584
|
+
{earlier.map((each) => (
|
|
585
|
+
<SelectItem key={each.id} value={each.id} className="h-auto! py-1.5">
|
|
586
|
+
<span className="flex min-w-0 flex-1 items-center gap-2">
|
|
587
|
+
{each.touches &&
|
|
588
|
+
!(each.target && win.target && sameTarget(each.target, win.target)) ? (
|
|
589
|
+
<span
|
|
590
|
+
title="Changed this plugin's code"
|
|
591
|
+
className="inline-flex shrink-0 text-muted"
|
|
592
|
+
>
|
|
593
|
+
<Icon name="git-branch" size={12} />
|
|
594
|
+
</span>
|
|
595
|
+
) : (
|
|
596
|
+
<span title="Started on this" className="inline-flex shrink-0 text-muted">
|
|
597
|
+
<Icon name="map-pin" size={12} />
|
|
598
|
+
</span>
|
|
599
|
+
)}
|
|
600
|
+
<span className="min-w-0 flex-1 truncate">{each.title}</span>
|
|
601
|
+
{each.archived && (
|
|
602
|
+
<span className="inline-flex shrink-0 text-muted" title="Archived: read only">
|
|
603
|
+
<Icon name="archive" size={12} />
|
|
604
|
+
</span>
|
|
605
|
+
)}
|
|
606
|
+
<span className="shrink-0 text-xs text-muted">
|
|
607
|
+
{ago(each.updatedAt ?? each.createdAt)}
|
|
608
|
+
</span>
|
|
609
|
+
</span>
|
|
610
|
+
</SelectItem>
|
|
611
|
+
))}
|
|
612
|
+
</SelectContent>
|
|
613
|
+
</Select>
|
|
614
|
+
);
|
|
615
|
+
const where =
|
|
616
|
+
started?.where ??
|
|
617
|
+
(place === undefined
|
|
618
|
+
? (lastStarted.current?.where ?? 'Finding where its code lives…')
|
|
619
|
+
: 'error' in place
|
|
620
|
+
? place.error
|
|
621
|
+
: place.label);
|
|
622
|
+
const left = Math.max(0, Math.min(pos.x, window.innerWidth - 380 - 8));
|
|
623
|
+
return (
|
|
624
|
+
<section
|
|
625
|
+
ref={section}
|
|
626
|
+
data-point-window
|
|
627
|
+
aria-label={win.label}
|
|
628
|
+
onPointerDown={() => {
|
|
629
|
+
onFront();
|
|
630
|
+
touched.current = true;
|
|
631
|
+
// Touched (resized, moved, typed in): it stays where it is, its bottom no longer held by the selection.
|
|
632
|
+
if (anchored) {
|
|
633
|
+
setPos({ x: pos.x, y: top });
|
|
634
|
+
moved.current = true;
|
|
635
|
+
}
|
|
636
|
+
}}
|
|
637
|
+
className={cn(
|
|
638
|
+
// Never resized smaller than it opened: 380px wide, the prompt's height, the window's 420px.
|
|
639
|
+
'pointer-events-auto fixed flex w-[380px] min-w-[380px] resize flex-col overflow-hidden text-foreground',
|
|
640
|
+
full && 'h-[420px] min-h-[420px] rounded-lg border border-border bg-background shadow-xl',
|
|
641
|
+
)}
|
|
642
|
+
style={{
|
|
643
|
+
...(!full && opened !== undefined && { minHeight: opened }),
|
|
644
|
+
left,
|
|
645
|
+
top: Math.max(0, top),
|
|
646
|
+
// Resized, it stops at the page's edges, as a window at the screen's.
|
|
647
|
+
maxWidth: window.innerWidth - left - 8,
|
|
648
|
+
maxHeight: window.innerHeight - Math.max(0, top) - 8,
|
|
649
|
+
zIndex: UNDER_POPUPS,
|
|
650
|
+
}}
|
|
651
|
+
>
|
|
652
|
+
{!full && (
|
|
653
|
+
// Only the prompt: a handle to move it, the sessions already about the target, and Close.
|
|
654
|
+
<div
|
|
655
|
+
onPointerDown={drag}
|
|
656
|
+
className="flex cursor-grab items-center justify-end gap-1 px-2 pb-1 active:cursor-grabbing"
|
|
657
|
+
>
|
|
658
|
+
{(error || place === undefined) && (
|
|
659
|
+
<span
|
|
660
|
+
className={cn(
|
|
661
|
+
'mr-auto truncate rounded-full bg-surface px-2 py-0.5 text-xs shadow-sm',
|
|
662
|
+
error ? 'text-destructive' : 'text-muted',
|
|
663
|
+
)}
|
|
664
|
+
>
|
|
665
|
+
{error || where}
|
|
666
|
+
</span>
|
|
667
|
+
)}
|
|
668
|
+
{earlier.length > 0 && sessionsAbout}
|
|
669
|
+
<Button
|
|
670
|
+
size="icon-sm"
|
|
671
|
+
variant="ghost"
|
|
672
|
+
aria-label="Close"
|
|
673
|
+
className="rounded-full bg-surface shadow-sm"
|
|
674
|
+
onClick={onClose}
|
|
675
|
+
>
|
|
676
|
+
<Icon name="x" />
|
|
677
|
+
</Button>
|
|
678
|
+
</div>
|
|
679
|
+
)}
|
|
680
|
+
{full && (
|
|
681
|
+
<header
|
|
682
|
+
onPointerDown={drag}
|
|
683
|
+
className="flex cursor-grab items-center gap-1 border-b border-border py-1 pr-1 pl-3 active:cursor-grabbing"
|
|
684
|
+
>
|
|
685
|
+
<div className="min-w-0 flex-1">
|
|
686
|
+
<div className="truncate text-sm font-medium">
|
|
687
|
+
{started?.title ?? (lastStarted.current && !win.target ? 'New session' : win.label)}
|
|
688
|
+
</div>
|
|
689
|
+
<div
|
|
690
|
+
className={cn(
|
|
691
|
+
'truncate text-xs',
|
|
692
|
+
place && 'error' in place ? 'text-destructive' : 'text-muted',
|
|
693
|
+
)}
|
|
694
|
+
>
|
|
695
|
+
{win.target?.plugin ? `${win.target.plugin} → ` : ''}
|
|
696
|
+
{place && 'error' in place ? 'Not editable here' : where}
|
|
697
|
+
</div>
|
|
698
|
+
</div>
|
|
699
|
+
{earlier.length > 0 && sessionsAbout}
|
|
700
|
+
{started && (
|
|
701
|
+
<Button
|
|
702
|
+
size="icon-sm"
|
|
703
|
+
variant="ghost"
|
|
704
|
+
aria-label="Open in Sessions"
|
|
705
|
+
onClick={openInSessions}
|
|
706
|
+
>
|
|
707
|
+
<Icon name="scroll-text" />
|
|
708
|
+
</Button>
|
|
709
|
+
)}
|
|
710
|
+
<Button size="icon-sm" variant="ghost" aria-label="Close" onClick={onClose}>
|
|
711
|
+
<Icon name="x" />
|
|
712
|
+
</Button>
|
|
713
|
+
</header>
|
|
714
|
+
)}
|
|
715
|
+
{place && 'error' in place && !started && (
|
|
716
|
+
// Whole, not truncated: the reason the code cannot be edited here.
|
|
717
|
+
<div
|
|
718
|
+
className={cn(
|
|
719
|
+
'flex flex-col gap-2 px-3 py-2 text-xs',
|
|
720
|
+
!full && 'mb-1 rounded-lg border border-border bg-background shadow-sm',
|
|
721
|
+
)}
|
|
722
|
+
>
|
|
723
|
+
<p className="break-words text-destructive">{place.error}</p>
|
|
724
|
+
</div>
|
|
725
|
+
)}
|
|
726
|
+
{started && <Conversation started={started} onRunning={setRunning} />}
|
|
727
|
+
{full && error && <div className="px-3 pb-1 text-xs text-destructive">{error}</div>}
|
|
728
|
+
{promptAt && (
|
|
729
|
+
<div className={cn(full ? 'border-t border-border py-2' : 'flex min-h-0 flex-1 flex-col')}>
|
|
730
|
+
<SessionPrompt
|
|
731
|
+
ctx={ctx}
|
|
732
|
+
use={promptChoice}
|
|
733
|
+
at={{ slug: promptAt.slug, node: promptAt.node }}
|
|
734
|
+
session={started?.id}
|
|
735
|
+
create={{
|
|
736
|
+
title: (text) => (titled.current = cutText(`${win.label} — ${text}`, 60)),
|
|
737
|
+
...(win.target && { target: win.target }),
|
|
738
|
+
...(place && 'branch' in place && place.branch && { branch: place.branch }),
|
|
739
|
+
...(place && 'stage' in place && place.stage && { worktree: place.stage }),
|
|
740
|
+
draft: `point.${win.label}`,
|
|
741
|
+
}}
|
|
742
|
+
onCreated={(id: string) => {
|
|
743
|
+
decided.current = true;
|
|
744
|
+
onSession({
|
|
745
|
+
slug: promptAt.slug,
|
|
746
|
+
node: promptAt.node,
|
|
747
|
+
id,
|
|
748
|
+
title: titled.current,
|
|
749
|
+
where,
|
|
750
|
+
at: new Date().toISOString(),
|
|
751
|
+
});
|
|
752
|
+
}}
|
|
753
|
+
onNewChat={() => {
|
|
754
|
+
// Another agent or model: a new session in this window, the draft carried by the prompt.
|
|
755
|
+
decided.current = true;
|
|
756
|
+
onSession(undefined);
|
|
757
|
+
}}
|
|
758
|
+
running={!!started && running}
|
|
759
|
+
variant="compact"
|
|
760
|
+
placeholder={win.target ? `Ask about ${win.label}…` : 'Ask about this workspace…'}
|
|
761
|
+
{...(blocked && { blocked })}
|
|
762
|
+
handle={prompt}
|
|
763
|
+
/>
|
|
764
|
+
</div>
|
|
765
|
+
)}
|
|
766
|
+
</section>
|
|
767
|
+
);
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
/** The sessions started here, latest first, reopened in a window; and a session with no target. */
|
|
771
|
+
function Dock({
|
|
772
|
+
started,
|
|
773
|
+
onOpen,
|
|
774
|
+
onNew,
|
|
775
|
+
}: {
|
|
776
|
+
started: Started[];
|
|
777
|
+
onOpen: (each: Started) => void;
|
|
778
|
+
onNew: () => void;
|
|
779
|
+
}) {
|
|
780
|
+
const [open, setOpen] = React.useState(false);
|
|
781
|
+
return (
|
|
782
|
+
<div
|
|
783
|
+
data-point-dock
|
|
784
|
+
className="pointer-events-auto fixed right-3 bottom-3 flex flex-col items-end gap-1"
|
|
785
|
+
style={{ zIndex: UNDER_POPUPS }}
|
|
786
|
+
>
|
|
787
|
+
{open && (
|
|
788
|
+
<div className="flex max-h-[60vh] w-72 flex-col overflow-hidden rounded-lg border border-border bg-surface shadow-xl">
|
|
789
|
+
<div className="flex items-center justify-between border-b border-border py-1 pr-1 pl-3">
|
|
790
|
+
<span className="text-sm font-medium">Pointed sessions</span>
|
|
791
|
+
<Button size="sm" variant="ghost" onClick={onNew}>
|
|
792
|
+
<Icon name="plus" />
|
|
793
|
+
New session
|
|
794
|
+
</Button>
|
|
795
|
+
</div>
|
|
796
|
+
<div className="overflow-auto py-1">
|
|
797
|
+
{started.length === 0 && (
|
|
798
|
+
<PaneNote className="px-3">
|
|
799
|
+
Hold {mac ? 'Option+Shift' : 'Alt+Shift'} and click anything to ask about it.
|
|
800
|
+
</PaneNote>
|
|
801
|
+
)}
|
|
802
|
+
{started.map((each) => (
|
|
803
|
+
<button
|
|
804
|
+
key={`${each.slug}/${each.id}`}
|
|
805
|
+
type="button"
|
|
806
|
+
onClick={() => onOpen(each)}
|
|
807
|
+
className="flex w-full cursor-pointer flex-col px-3 py-1.5 text-left hover:bg-hover"
|
|
808
|
+
>
|
|
809
|
+
<span className="w-full truncate text-sm">{each.title}</span>
|
|
810
|
+
<span className="w-full truncate text-xs text-muted">{each.where}</span>
|
|
811
|
+
</button>
|
|
812
|
+
))}
|
|
813
|
+
</div>
|
|
814
|
+
</div>
|
|
815
|
+
)}
|
|
816
|
+
<Button
|
|
817
|
+
size="icon"
|
|
818
|
+
variant="outline"
|
|
819
|
+
aria-label="Pointed sessions"
|
|
820
|
+
aria-expanded={open}
|
|
821
|
+
onClick={() => setOpen((was) => !was)}
|
|
822
|
+
className="rounded-full shadow-md"
|
|
823
|
+
>
|
|
824
|
+
<Icon name="messages-square" />
|
|
825
|
+
</Button>
|
|
826
|
+
</div>
|
|
827
|
+
);
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
function Point({ ctx }: { ctx: Context }) {
|
|
831
|
+
const { shell } = ctx;
|
|
832
|
+
const { current, node } = React.useSyncExternalStore(shell.subscribe, shell.get);
|
|
833
|
+
const here: Here = { workspace: current?.slug, node };
|
|
834
|
+
const hereRef = React.useRef(here);
|
|
835
|
+
hereRef.current = here;
|
|
836
|
+
const [hover, setHover] = React.useState<Element | undefined>();
|
|
837
|
+
const [zone, setZone] = React.useState<
|
|
838
|
+
{ left: number; top: number; width: number; height: number } | undefined
|
|
839
|
+
>();
|
|
840
|
+
const [selections, setSelections] = React.useState<Selection[]>([]);
|
|
841
|
+
const [windows, setWindows] = React.useState<Win[]>([]);
|
|
842
|
+
const [started, setStarted] = React.useState<Started[]>(readStarted);
|
|
843
|
+
const [, setTick] = React.useState(0);
|
|
844
|
+
const next = React.useRef(1);
|
|
845
|
+
|
|
846
|
+
// Frames follow their elements: redrawn on scroll, resize, and every half second (layout changes).
|
|
847
|
+
React.useEffect(() => {
|
|
848
|
+
if (!selections.length) return;
|
|
849
|
+
const redraw = () => setTick((tick) => tick + 1);
|
|
850
|
+
const timer = setInterval(() => {
|
|
851
|
+
// An element gone (its view changed, its plugin reloaded) loses its selection.
|
|
852
|
+
setSelections((was) =>
|
|
853
|
+
was.some((each) => each.element && !each.element.isConnected)
|
|
854
|
+
? was.filter((each) => !each.element || each.element.isConnected)
|
|
855
|
+
: was,
|
|
856
|
+
);
|
|
857
|
+
redraw();
|
|
858
|
+
}, 500);
|
|
859
|
+
window.addEventListener('scroll', redraw, true);
|
|
860
|
+
window.addEventListener('resize', redraw);
|
|
861
|
+
return () => {
|
|
862
|
+
clearInterval(timer);
|
|
863
|
+
window.removeEventListener('scroll', redraw, true);
|
|
864
|
+
window.removeEventListener('resize', redraw);
|
|
865
|
+
};
|
|
866
|
+
}, [selections.length]);
|
|
867
|
+
|
|
868
|
+
// Option+Shift held: the pointer picks (Option alone is the window manager's). Capture phase, so the page under it
|
|
869
|
+
// neither sees the press nor the click.
|
|
870
|
+
React.useEffect(() => {
|
|
871
|
+
let press: { x: number; y: number } | undefined;
|
|
872
|
+
// One selection at a time: a new pick replaces the last.
|
|
873
|
+
const add = (selection: Omit<Selection, 'id'>) =>
|
|
874
|
+
setSelections([{ ...selection, id: next.current++ }]);
|
|
875
|
+
const move = (event: PointerEvent) => {
|
|
876
|
+
if (!pointing(event)) {
|
|
877
|
+
setHover(undefined);
|
|
878
|
+
return;
|
|
879
|
+
}
|
|
880
|
+
if (press && Math.hypot(event.clientX - press.x, event.clientY - press.y) > DRAG) {
|
|
881
|
+
setHover(undefined);
|
|
882
|
+
setZone({
|
|
883
|
+
left: Math.min(press.x, event.clientX),
|
|
884
|
+
top: Math.min(press.y, event.clientY),
|
|
885
|
+
width: Math.abs(event.clientX - press.x),
|
|
886
|
+
height: Math.abs(event.clientY - press.y),
|
|
887
|
+
});
|
|
888
|
+
return;
|
|
889
|
+
}
|
|
890
|
+
setHover(event.target instanceof Element ? pick(event.target, finest(event)) : undefined);
|
|
891
|
+
};
|
|
892
|
+
const down = (event: PointerEvent) => {
|
|
893
|
+
if (!pointing(event) || event.button !== 0) return;
|
|
894
|
+
event.preventDefault();
|
|
895
|
+
event.stopPropagation();
|
|
896
|
+
press = { x: event.clientX, y: event.clientY };
|
|
897
|
+
};
|
|
898
|
+
const up = (event: PointerEvent) => {
|
|
899
|
+
if (!press) return;
|
|
900
|
+
event.preventDefault();
|
|
901
|
+
event.stopPropagation();
|
|
902
|
+
const dragged = Math.hypot(event.clientX - press.x, event.clientY - press.y) > DRAG;
|
|
903
|
+
press = undefined;
|
|
904
|
+
setZone((drawn) => {
|
|
905
|
+
if (dragged && drawn) {
|
|
906
|
+
const target = zoneTarget(drawn, hereRef.current);
|
|
907
|
+
add({ target, label: labelOf(target) });
|
|
908
|
+
}
|
|
909
|
+
return undefined;
|
|
910
|
+
});
|
|
911
|
+
if (!dragged && event.target instanceof Element) {
|
|
912
|
+
const element = pick(event.target, finest(event));
|
|
913
|
+
const target = elementTarget(element, hereRef.current);
|
|
914
|
+
add({ element, target, label: labelOf(target, element) });
|
|
915
|
+
}
|
|
916
|
+
};
|
|
917
|
+
// Option+click on a link downloads it, on anything else it is the page's click: neither while picking (⌥⇧).
|
|
918
|
+
const click = (event: MouseEvent) => {
|
|
919
|
+
if (!pointing(event)) return;
|
|
920
|
+
event.preventDefault();
|
|
921
|
+
event.stopPropagation();
|
|
922
|
+
};
|
|
923
|
+
// Either modifier released: no more framing.
|
|
924
|
+
const key = (event: KeyboardEvent) => {
|
|
925
|
+
if ((event.key === 'Alt' || event.key === 'Shift') && event.type === 'keyup')
|
|
926
|
+
setHover(undefined);
|
|
927
|
+
};
|
|
928
|
+
window.addEventListener('pointermove', move, true);
|
|
929
|
+
window.addEventListener('pointerdown', down, true);
|
|
930
|
+
window.addEventListener('pointerup', up, true);
|
|
931
|
+
window.addEventListener('click', click, true);
|
|
932
|
+
window.addEventListener('keyup', key, true);
|
|
933
|
+
const blur = () => setHover(undefined);
|
|
934
|
+
window.addEventListener('blur', blur);
|
|
935
|
+
return () => {
|
|
936
|
+
window.removeEventListener('pointermove', move, true);
|
|
937
|
+
window.removeEventListener('pointerdown', down, true);
|
|
938
|
+
window.removeEventListener('pointerup', up, true);
|
|
939
|
+
window.removeEventListener('click', click, true);
|
|
940
|
+
window.removeEventListener('keyup', key, true);
|
|
941
|
+
window.removeEventListener('blur', blur);
|
|
942
|
+
};
|
|
943
|
+
}, []);
|
|
944
|
+
|
|
945
|
+
/** Opens a window: next to `rect` (a selection), on the side with room; without one (the dock), at the top right. */
|
|
946
|
+
const openWindow = (
|
|
947
|
+
win: Omit<Win, 'id' | 'x' | 'y'>,
|
|
948
|
+
rect?: { left: number; top: number; width: number; height: number },
|
|
949
|
+
) => {
|
|
950
|
+
const offset = (windows.length % 6) * 24;
|
|
951
|
+
const placed = rect && near(rect, PROMPT_WIDTH, PROMPT_HEIGHT, innerWidth, innerHeight);
|
|
952
|
+
const at = placed
|
|
953
|
+
? { ...placed, ...(placed.side === 'above' && { bottom: placed.y + PROMPT_HEIGHT }) }
|
|
954
|
+
: { x: Math.max(80, innerWidth - 420 - offset), y: 64 + offset };
|
|
955
|
+
setWindows((was) => [...was, { ...win, ...at, id: next.current++ }]);
|
|
956
|
+
};
|
|
957
|
+
const remember = (session: Started) => {
|
|
958
|
+
setStarted((was) => {
|
|
959
|
+
const list = [
|
|
960
|
+
session,
|
|
961
|
+
...was.filter((each) => !(each.slug === session.slug && each.id === session.id)),
|
|
962
|
+
].slice(0, 50);
|
|
963
|
+
try {
|
|
964
|
+
localStorage.setItem(SESSIONS_KEY, JSON.stringify(list));
|
|
965
|
+
} catch {}
|
|
966
|
+
return list;
|
|
967
|
+
});
|
|
968
|
+
};
|
|
969
|
+
const close = (id: number) => setWindows((was) => was.filter((each) => each.id !== id));
|
|
970
|
+
const front = (id: number) =>
|
|
971
|
+
setWindows((was) =>
|
|
972
|
+
was.at(-1)?.id === id
|
|
973
|
+
? was
|
|
974
|
+
: [...was.filter((each) => each.id !== id), was.find((each) => each.id === id)!],
|
|
975
|
+
);
|
|
976
|
+
const deselect = (id: number) => setSelections((was) => was.filter((each) => each.id !== id));
|
|
977
|
+
|
|
978
|
+
return (
|
|
979
|
+
// No box, no stacking context: frames sit over everything, windows and the dock under the kit's popups (z-50), which
|
|
980
|
+
// their own menus open in.
|
|
981
|
+
<div data-point className="contents">
|
|
982
|
+
{hover && (
|
|
983
|
+
<Frame rect={hover.getBoundingClientRect()} className="bg-accent/5 outline-accent/70">
|
|
984
|
+
<span className="absolute -top-5 left-0 rounded-sm bg-accent px-1 text-[11px] leading-4 whitespace-nowrap text-accent-foreground">
|
|
985
|
+
{pluginOf(hover) ?? 'page'} · {hover.tagName.toLowerCase()}
|
|
986
|
+
</span>
|
|
987
|
+
</Frame>
|
|
988
|
+
)}
|
|
989
|
+
{zone && (
|
|
990
|
+
<Frame
|
|
991
|
+
rect={zone}
|
|
992
|
+
className="border border-dashed border-accent bg-accent/5 outline-transparent"
|
|
993
|
+
/>
|
|
994
|
+
)}
|
|
995
|
+
{selections.map((selection) => (
|
|
996
|
+
<Selected
|
|
997
|
+
key={selection.id}
|
|
998
|
+
selection={selection}
|
|
999
|
+
onClose={() => deselect(selection.id)}
|
|
1000
|
+
onEdit={() => {
|
|
1001
|
+
if (selection.target.customizationBlocked) return;
|
|
1002
|
+
deselect(selection.id);
|
|
1003
|
+
openWindow({ target: selection.target, label: selection.label }, rectOf(selection));
|
|
1004
|
+
}}
|
|
1005
|
+
onDelete={() => {
|
|
1006
|
+
if (!canDeleteTarget(selection.target)) return;
|
|
1007
|
+
// Hidden now; the agent's edit removes it for good (the page reloads that plugin).
|
|
1008
|
+
(selection.element as HTMLElement | undefined)?.style.setProperty('display', 'none');
|
|
1009
|
+
const rect = rectOf(selection);
|
|
1010
|
+
deselect(selection.id);
|
|
1011
|
+
openWindow(
|
|
1012
|
+
{ target: selection.target, label: selection.label, intent: 'delete' },
|
|
1013
|
+
rect,
|
|
1014
|
+
);
|
|
1015
|
+
}}
|
|
1016
|
+
/>
|
|
1017
|
+
))}
|
|
1018
|
+
{/* At one z-index, the last one drawn is in front: `front` moves a window last. */}
|
|
1019
|
+
{windows.map((win) => (
|
|
1020
|
+
<Window
|
|
1021
|
+
key={win.id}
|
|
1022
|
+
win={win}
|
|
1023
|
+
here={here}
|
|
1024
|
+
ctx={ctx}
|
|
1025
|
+
onSession={(session) => {
|
|
1026
|
+
if (session) remember(session);
|
|
1027
|
+
setWindows((was) =>
|
|
1028
|
+
was.map((each) => (each.id === win.id ? { ...each, started: session } : each)),
|
|
1029
|
+
);
|
|
1030
|
+
}}
|
|
1031
|
+
onClose={() => close(win.id)}
|
|
1032
|
+
onFront={() => front(win.id)}
|
|
1033
|
+
/>
|
|
1034
|
+
))}
|
|
1035
|
+
<Dock
|
|
1036
|
+
started={started}
|
|
1037
|
+
onNew={() => openWindow({ label: 'New session' })}
|
|
1038
|
+
onOpen={(each) => {
|
|
1039
|
+
remember({ ...each, at: new Date().toISOString() });
|
|
1040
|
+
const shown = windows.find(
|
|
1041
|
+
(win) => win.started?.slug === each.slug && win.started.id === each.id,
|
|
1042
|
+
);
|
|
1043
|
+
if (shown) front(shown.id);
|
|
1044
|
+
else openWindow({ label: each.title, started: each });
|
|
1045
|
+
}}
|
|
1046
|
+
/>
|
|
1047
|
+
</div>
|
|
1048
|
+
);
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
export function apply(ctx: Context) {
|
|
1052
|
+
openStream = ctx.streams.open;
|
|
1053
|
+
ctx.slots.register({ name: 'overlay', id: 'point' }, () => <Point ctx={ctx} />);
|
|
1054
|
+
// Point's gestures, for the shell's help and its conflict check (removed when this plugin is disposed).
|
|
1055
|
+
for (const gesture of gestures) ctx.effect(() => ctx.shell.shortcuts.gesture(gesture, name));
|
|
1056
|
+
}
|