@capybara-hq/plugin-browser 0.1.0-rc.1 → 0.1.0-rc.11
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/.capybara-plugin/plugin.json +3 -3
- package/README.md +191 -24
- package/dist/index.mjs +409 -118
- package/package.json +2 -2
- package/ui/panel.js +922 -132
- package/ui/service/daemon.mjs +3144 -464
package/ui/panel.js
CHANGED
|
@@ -3,8 +3,69 @@
|
|
|
3
3
|
// daemon/protocol.ts
|
|
4
4
|
var VIEWPORT = { width: 1280, height: 800 };
|
|
5
5
|
var IDLE_STOP_MS = 15 * 6e4;
|
|
6
|
+
var VIEWER_TEXT_MAX_CHARS = 1e4;
|
|
7
|
+
function clipCodePoints(text, max) {
|
|
8
|
+
if (text.length <= max) return text;
|
|
9
|
+
return Array.from(text).slice(0, max).join("");
|
|
10
|
+
}
|
|
11
|
+
var VIEWER_CURSORS = [
|
|
12
|
+
"default",
|
|
13
|
+
"pointer",
|
|
14
|
+
"text",
|
|
15
|
+
"vertical-text",
|
|
16
|
+
"crosshair",
|
|
17
|
+
"move",
|
|
18
|
+
"grab",
|
|
19
|
+
"grabbing",
|
|
20
|
+
"not-allowed",
|
|
21
|
+
"no-drop",
|
|
22
|
+
"wait",
|
|
23
|
+
"progress",
|
|
24
|
+
"help",
|
|
25
|
+
"context-menu",
|
|
26
|
+
"cell",
|
|
27
|
+
"copy",
|
|
28
|
+
"alias",
|
|
29
|
+
"zoom-in",
|
|
30
|
+
"zoom-out",
|
|
31
|
+
"col-resize",
|
|
32
|
+
"row-resize",
|
|
33
|
+
"ew-resize",
|
|
34
|
+
"ns-resize",
|
|
35
|
+
"nesw-resize",
|
|
36
|
+
"nwse-resize",
|
|
37
|
+
"n-resize",
|
|
38
|
+
"e-resize",
|
|
39
|
+
"s-resize",
|
|
40
|
+
"w-resize",
|
|
41
|
+
"ne-resize",
|
|
42
|
+
"nw-resize",
|
|
43
|
+
"se-resize",
|
|
44
|
+
"sw-resize",
|
|
45
|
+
"all-scroll",
|
|
46
|
+
"none"
|
|
47
|
+
];
|
|
48
|
+
function toViewerCursor(value) {
|
|
49
|
+
return typeof value === "string" && VIEWER_CURSORS.includes(value) ? value : "default";
|
|
50
|
+
}
|
|
51
|
+
var FRAME_TAG = 0;
|
|
52
|
+
var FRAME_HEADER_BYTES = 9;
|
|
53
|
+
function decodeFrame(m) {
|
|
54
|
+
if (m.byteLength < FRAME_HEADER_BYTES || m[0] !== FRAME_TAG) return null;
|
|
55
|
+
const v = new DataView(m.buffer, m.byteOffset, m.byteLength);
|
|
56
|
+
return {
|
|
57
|
+
seq: v.getUint32(1),
|
|
58
|
+
width: v.getUint16(5),
|
|
59
|
+
height: v.getUint16(7),
|
|
60
|
+
jpeg: m.subarray(FRAME_HEADER_BYTES)
|
|
61
|
+
};
|
|
62
|
+
}
|
|
6
63
|
var DEFAULT_FRAME_MAX_BYTES = 256 * 1024;
|
|
64
|
+
var CONVERSATION_IDLE_MS = 15 * 6e4;
|
|
65
|
+
var EVICT_MIN_IDLE_MS = 5 * 6e4;
|
|
7
66
|
var HANDOFF_KEEPALIVE_MS = 24 * 60 * 6e4;
|
|
67
|
+
var FAVICON_MAX_BYTES = 8 * 1024;
|
|
68
|
+
var isIconDataUrl = (s) => typeof s === "string" && s.length <= 64 + Math.ceil(FAVICON_MAX_BYTES * 4 / 3) && /^data:image\/(png|x-icon|gif|jpeg|webp|svg\+xml);base64,[A-Za-z0-9+/=]+$/.test(s);
|
|
8
69
|
|
|
9
70
|
// ui-src/panel/i18n.ts
|
|
10
71
|
var STRINGS = {
|
|
@@ -18,11 +79,17 @@
|
|
|
18
79
|
closeTab: "Close tab",
|
|
19
80
|
takeover: "Take over",
|
|
20
81
|
handback: "Hand back to the agent",
|
|
21
|
-
|
|
22
|
-
|
|
82
|
+
statusUser: "You are in control",
|
|
83
|
+
statusUserDetail: "the agent is paused; press Esc to hand back",
|
|
84
|
+
statusAgent: "The agent is working",
|
|
23
85
|
statusUserElsewhere: "You are controlling the browser from another view",
|
|
24
|
-
statusLoading: "
|
|
25
|
-
statusConnecting: "Connecting…",
|
|
86
|
+
statusLoading: "loading the page",
|
|
87
|
+
statusConnecting: "Connecting to the browser…",
|
|
88
|
+
tabs: "Tabs",
|
|
89
|
+
more: "More",
|
|
90
|
+
verifyTitle: "Verification needed",
|
|
91
|
+
verifyBody: "The site asks for a human check. Hand back when you are done; the agent continues from here.",
|
|
92
|
+
verifyGo: "Verify",
|
|
26
93
|
blocked: "Blocked by the network setting:",
|
|
27
94
|
allow: "Allow",
|
|
28
95
|
dialog: "The page shows a dialog:",
|
|
@@ -40,7 +107,14 @@
|
|
|
40
107
|
secure: "Secure connection",
|
|
41
108
|
fileDialog: "The page opened a file chooser; the agent can pick a file with its upload tool.",
|
|
42
109
|
allowFailed: "Could not allow the host:",
|
|
43
|
-
answerFailed: "Could not send your answer:"
|
|
110
|
+
answerFailed: "Could not send your answer:",
|
|
111
|
+
subTask: "Sub-task",
|
|
112
|
+
otherConversations: "Other conversations",
|
|
113
|
+
moveHere: "Move here",
|
|
114
|
+
tabInUse: "Another conversation is using this tab; try again shortly",
|
|
115
|
+
tabCap: "This conversation has the most tabs it can keep; close one first",
|
|
116
|
+
treeEmptyTitle: "The agent has not opened a page in this conversation yet",
|
|
117
|
+
treeEmptyBody: "Its pages show here as it opens them."
|
|
44
118
|
},
|
|
45
119
|
zh: {
|
|
46
120
|
title: "浏览器",
|
|
@@ -52,11 +126,17 @@
|
|
|
52
126
|
closeTab: "关闭标签页",
|
|
53
127
|
takeover: "接管",
|
|
54
128
|
handback: "交还智能体",
|
|
55
|
-
|
|
56
|
-
|
|
129
|
+
statusUser: "你正在操作",
|
|
130
|
+
statusUserDetail: "智能体已暂停,按 Esc 交还",
|
|
131
|
+
statusAgent: "智能体正在操作",
|
|
57
132
|
statusUserElsewhere: "你正在另一个视图中控制浏览器",
|
|
58
|
-
statusLoading: "
|
|
59
|
-
statusConnecting: "
|
|
133
|
+
statusLoading: "正在加载页面",
|
|
134
|
+
statusConnecting: "正在连接浏览器…",
|
|
135
|
+
tabs: "标签页",
|
|
136
|
+
more: "更多",
|
|
137
|
+
verifyTitle: "需要你完成验证",
|
|
138
|
+
verifyBody: "网站要求人工验证。完成后交还,智能体从这里继续。",
|
|
139
|
+
verifyGo: "去验证",
|
|
60
140
|
blocked: "被网络设置拦截:",
|
|
61
141
|
allow: "允许",
|
|
62
142
|
dialog: "页面弹出了对话框:",
|
|
@@ -74,20 +154,472 @@
|
|
|
74
154
|
secure: "安全连接",
|
|
75
155
|
fileDialog: "页面打开了文件选择框;智能体可以用上传工具选择文件。",
|
|
76
156
|
allowFailed: "无法允许该主机:",
|
|
77
|
-
answerFailed: "无法发送你的回答:"
|
|
157
|
+
answerFailed: "无法发送你的回答:",
|
|
158
|
+
subTask: "子任务",
|
|
159
|
+
otherConversations: "其他对话",
|
|
160
|
+
moveHere: "移到此对话",
|
|
161
|
+
tabInUse: "该标签页正被其他对话使用,请稍后再试",
|
|
162
|
+
tabCap: "此对话的标签页已达上限,请先关闭一个",
|
|
163
|
+
treeEmptyTitle: "智能体尚未在此对话中打开网页",
|
|
164
|
+
treeEmptyBody: "它打开的网页会显示在这里。"
|
|
78
165
|
}
|
|
79
166
|
};
|
|
80
167
|
function t(locale, key) {
|
|
81
168
|
return STRINGS[locale][key];
|
|
82
169
|
}
|
|
83
170
|
|
|
171
|
+
// ui-src/panel/input.ts
|
|
172
|
+
var MOVE_EVERY_MS = 33;
|
|
173
|
+
var PRESS_MAX_MS = 1e3;
|
|
174
|
+
function mountInput(opts) {
|
|
175
|
+
const win = opts.window;
|
|
176
|
+
const doc = win.document;
|
|
177
|
+
const { view, stage, send, interactive } = opts;
|
|
178
|
+
const sink = doc.createElement("textarea");
|
|
179
|
+
sink.id = "input-sink";
|
|
180
|
+
sink.setAttribute("aria-label", opts.label);
|
|
181
|
+
sink.setAttribute("autocomplete", "off");
|
|
182
|
+
sink.setAttribute("autocapitalize", "off");
|
|
183
|
+
sink.setAttribute("autocorrect", "off");
|
|
184
|
+
sink.spellcheck = false;
|
|
185
|
+
sink.tabIndex = -1;
|
|
186
|
+
Object.assign(sink.style, {
|
|
187
|
+
position: "absolute",
|
|
188
|
+
left: "0px",
|
|
189
|
+
top: "0px",
|
|
190
|
+
width: "1px",
|
|
191
|
+
height: "1px",
|
|
192
|
+
padding: "0",
|
|
193
|
+
border: "0",
|
|
194
|
+
margin: "0",
|
|
195
|
+
opacity: "0",
|
|
196
|
+
resize: "none",
|
|
197
|
+
overflow: "hidden",
|
|
198
|
+
pointerEvents: "none",
|
|
199
|
+
whiteSpace: "pre"
|
|
200
|
+
});
|
|
201
|
+
stage.appendChild(sink);
|
|
202
|
+
const press = doc.createElement("div");
|
|
203
|
+
press.id = "press";
|
|
204
|
+
press.hidden = true;
|
|
205
|
+
press.setAttribute("aria-hidden", "true");
|
|
206
|
+
Object.assign(press.style, {
|
|
207
|
+
position: "absolute",
|
|
208
|
+
width: "18px",
|
|
209
|
+
height: "18px",
|
|
210
|
+
margin: "-9px 0 0 -9px",
|
|
211
|
+
borderRadius: "50%",
|
|
212
|
+
border: "2px solid var(--cb-accent, Highlight)",
|
|
213
|
+
pointerEvents: "none"
|
|
214
|
+
});
|
|
215
|
+
stage.appendChild(press);
|
|
216
|
+
let pressTimer;
|
|
217
|
+
let pressReleased = false;
|
|
218
|
+
let cursor = "default";
|
|
219
|
+
let focusWanted = false;
|
|
220
|
+
let held = null;
|
|
221
|
+
const downKeys = /* @__PURE__ */ new Map();
|
|
222
|
+
let lastPointer = { x: 0, y: 0 };
|
|
223
|
+
let moveTimer;
|
|
224
|
+
let nextMove = null;
|
|
225
|
+
const inStage = (clientX, clientY) => {
|
|
226
|
+
const r = stage.getBoundingClientRect();
|
|
227
|
+
return { left: clientX - r.left, top: clientY - r.top };
|
|
228
|
+
};
|
|
229
|
+
function placeSink(clientX, clientY) {
|
|
230
|
+
const p = inStage(clientX, clientY);
|
|
231
|
+
sink.style.left = `${Math.round(p.left)}px`;
|
|
232
|
+
sink.style.top = `${Math.round(p.top)}px`;
|
|
233
|
+
}
|
|
234
|
+
function showPress(clientX, clientY) {
|
|
235
|
+
const p = inStage(clientX, clientY);
|
|
236
|
+
press.style.left = `${Math.round(p.left)}px`;
|
|
237
|
+
press.style.top = `${Math.round(p.top)}px`;
|
|
238
|
+
press.setAttribute("data-state", "down");
|
|
239
|
+
press.hidden = false;
|
|
240
|
+
pressReleased = false;
|
|
241
|
+
if (pressTimer !== void 0) clearTimeout(pressTimer);
|
|
242
|
+
pressTimer = setTimeout(hidePress, PRESS_MAX_MS);
|
|
243
|
+
}
|
|
244
|
+
function hidePress() {
|
|
245
|
+
if (pressTimer !== void 0) clearTimeout(pressTimer);
|
|
246
|
+
pressTimer = void 0;
|
|
247
|
+
press.hidden = true;
|
|
248
|
+
press.removeAttribute("data-state");
|
|
249
|
+
pressReleased = false;
|
|
250
|
+
}
|
|
251
|
+
function applyCursor() {
|
|
252
|
+
view.style.cursor = interactive() ? cursor : "default";
|
|
253
|
+
}
|
|
254
|
+
function queueMove(p) {
|
|
255
|
+
nextMove = p;
|
|
256
|
+
if (moveTimer !== void 0) return;
|
|
257
|
+
flushMove();
|
|
258
|
+
}
|
|
259
|
+
function flushMove() {
|
|
260
|
+
const p = nextMove;
|
|
261
|
+
nextMove = null;
|
|
262
|
+
if (p === null) {
|
|
263
|
+
moveTimer = void 0;
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
if (interactive()) send({ t: "mouse", kind: "move", ...p });
|
|
267
|
+
moveTimer = setTimeout(flushMove, MOVE_EVERY_MS);
|
|
268
|
+
}
|
|
269
|
+
function dropMove() {
|
|
270
|
+
nextMove = null;
|
|
271
|
+
}
|
|
272
|
+
view.addEventListener("mousedown", (e) => {
|
|
273
|
+
if (!interactive()) return;
|
|
274
|
+
e.preventDefault();
|
|
275
|
+
const button = e.button === 2 ? "right" : "left";
|
|
276
|
+
held = button;
|
|
277
|
+
dropMove();
|
|
278
|
+
placeSink(e.clientX, e.clientY);
|
|
279
|
+
sink.focus();
|
|
280
|
+
showPress(e.clientX, e.clientY);
|
|
281
|
+
lastPointer = opts.toPage(e.clientX, e.clientY);
|
|
282
|
+
send({ t: "mouse", kind: "down", ...lastPointer, button });
|
|
283
|
+
});
|
|
284
|
+
const release = (e) => {
|
|
285
|
+
if (held === null) return;
|
|
286
|
+
const button = held;
|
|
287
|
+
held = null;
|
|
288
|
+
if (!interactive()) return;
|
|
289
|
+
dropMove();
|
|
290
|
+
press.setAttribute("data-state", "up");
|
|
291
|
+
pressReleased = true;
|
|
292
|
+
send({ t: "mouse", kind: "up", ...opts.toPage(e.clientX, e.clientY), button });
|
|
293
|
+
};
|
|
294
|
+
win.addEventListener("mouseup", release);
|
|
295
|
+
const moved = (e) => {
|
|
296
|
+
if (!interactive()) return;
|
|
297
|
+
lastPointer = opts.toPage(e.clientX, e.clientY);
|
|
298
|
+
queueMove(lastPointer);
|
|
299
|
+
};
|
|
300
|
+
view.addEventListener("mousemove", moved);
|
|
301
|
+
win.addEventListener("mousemove", (e) => {
|
|
302
|
+
if (held !== null && e.target !== view) moved(e);
|
|
303
|
+
});
|
|
304
|
+
view.addEventListener(
|
|
305
|
+
"wheel",
|
|
306
|
+
(e) => {
|
|
307
|
+
if (!interactive()) return;
|
|
308
|
+
e.preventDefault();
|
|
309
|
+
dropMove();
|
|
310
|
+
send({
|
|
311
|
+
t: "mouse",
|
|
312
|
+
kind: "wheel",
|
|
313
|
+
...opts.toPage(e.clientX, e.clientY),
|
|
314
|
+
dx: e.deltaX,
|
|
315
|
+
dy: e.deltaY
|
|
316
|
+
});
|
|
317
|
+
},
|
|
318
|
+
{ passive: false }
|
|
319
|
+
);
|
|
320
|
+
view.addEventListener("contextmenu", (e) => e.preventDefault());
|
|
321
|
+
const composing = (e) => e.isComposing || e.key === "Process" || e.keyCode === 229;
|
|
322
|
+
const isPasteShortcut = (e) => (e.metaKey || e.ctrlKey) && !e.altKey && e.key.toLowerCase() === "v";
|
|
323
|
+
function sendText(text) {
|
|
324
|
+
if (text === "") return;
|
|
325
|
+
const t2 = clipCodePoints(text, VIEWER_TEXT_MAX_CHARS);
|
|
326
|
+
send({ t: "key", kind: "char", key: t2, text: t2 });
|
|
327
|
+
}
|
|
328
|
+
sink.addEventListener("keydown", (e) => {
|
|
329
|
+
if (!interactive() || e.key === "Escape" || composing(e) || isPasteShortcut(e)) return;
|
|
330
|
+
e.preventDefault();
|
|
331
|
+
downKeys.set(e.key, e.code);
|
|
332
|
+
send({ t: "key", kind: "down", key: e.key, code: e.code });
|
|
333
|
+
});
|
|
334
|
+
sink.addEventListener("keyup", (e) => {
|
|
335
|
+
if (!interactive() || e.key === "Escape" || composing(e) || isPasteShortcut(e)) return;
|
|
336
|
+
e.preventDefault();
|
|
337
|
+
downKeys.delete(e.key);
|
|
338
|
+
send({ t: "key", kind: "up", key: e.key, code: e.code });
|
|
339
|
+
});
|
|
340
|
+
function releaseAll() {
|
|
341
|
+
if (interactive()) {
|
|
342
|
+
for (const [key, code] of downKeys) send({ t: "key", kind: "up", key, code });
|
|
343
|
+
if (held !== null) send({ t: "mouse", kind: "up", ...lastPointer, button: held });
|
|
344
|
+
}
|
|
345
|
+
downKeys.clear();
|
|
346
|
+
held = null;
|
|
347
|
+
}
|
|
348
|
+
win.addEventListener("blur", releaseAll);
|
|
349
|
+
sink.addEventListener("blur", releaseAll);
|
|
350
|
+
doc.addEventListener("visibilitychange", () => {
|
|
351
|
+
if (doc.visibilityState === "hidden") releaseAll();
|
|
352
|
+
});
|
|
353
|
+
sink.addEventListener("compositionend", (e) => {
|
|
354
|
+
if (interactive()) sendText(e.data ?? "");
|
|
355
|
+
sink.value = "";
|
|
356
|
+
});
|
|
357
|
+
sink.addEventListener("input", (e) => {
|
|
358
|
+
const ie = e;
|
|
359
|
+
if (ie.isComposing || (ie.inputType ?? "").includes("Composition")) return;
|
|
360
|
+
if (interactive()) sendText(sink.value);
|
|
361
|
+
sink.value = "";
|
|
362
|
+
});
|
|
363
|
+
sink.addEventListener("paste", (e) => {
|
|
364
|
+
e.preventDefault();
|
|
365
|
+
if (!interactive()) return;
|
|
366
|
+
sendText(e.clipboardData?.getData("text/plain") ?? "");
|
|
367
|
+
});
|
|
368
|
+
return {
|
|
369
|
+
sink,
|
|
370
|
+
sync() {
|
|
371
|
+
applyCursor();
|
|
372
|
+
if (!interactive()) {
|
|
373
|
+
held = null;
|
|
374
|
+
downKeys.clear();
|
|
375
|
+
nextMove = null;
|
|
376
|
+
hidePress();
|
|
377
|
+
if (doc.activeElement === sink) sink.blur();
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
if (focusWanted) {
|
|
381
|
+
focusWanted = false;
|
|
382
|
+
sink.focus();
|
|
383
|
+
}
|
|
384
|
+
},
|
|
385
|
+
frame() {
|
|
386
|
+
if (pressReleased) hidePress();
|
|
387
|
+
},
|
|
388
|
+
cursor(value) {
|
|
389
|
+
cursor = toViewerCursor(value);
|
|
390
|
+
applyCursor();
|
|
391
|
+
},
|
|
392
|
+
focus() {
|
|
393
|
+
focusWanted = !interactive();
|
|
394
|
+
sink.focus();
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// ui-src/panel/page.ts
|
|
400
|
+
var ICONS = {
|
|
401
|
+
back: '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m15 18-6-6 6-6"/></svg>',
|
|
402
|
+
forward: '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m9 18 6-6-6-6"/></svg>',
|
|
403
|
+
reload: '<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21 12a9 9 0 1 1-3-6.7L21 8"/><path d="M21 3v5h-5"/></svg>',
|
|
404
|
+
close: '<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true"><path d="M18 6 6 18M6 6l12 12"/></svg>',
|
|
405
|
+
plus: '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true"><path d="M12 5v14M5 12h14"/></svg>',
|
|
406
|
+
lock: '<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true"><rect x="5" y="11" width="14" height="10" rx="2"/><path d="M8 11V7a4 4 0 0 1 8 0v4"/></svg>',
|
|
407
|
+
globe: '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="9"/><path d="M3 12h18M12 3a14 14 0 0 1 0 18M12 3a14 14 0 0 0 0 18"/></svg>',
|
|
408
|
+
more: '<svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><circle cx="5" cy="12" r="1.6"/><circle cx="12" cy="12" r="1.6"/><circle cx="19" cy="12" r="1.6"/></svg>',
|
|
409
|
+
spin: '<svg class="spin" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" aria-hidden="true"><path d="M21 12a9 9 0 1 1-9-9"/></svg>',
|
|
410
|
+
dot: '<svg width="12" height="12" viewBox="0 0 24 24" aria-hidden="true"><circle cx="12" cy="12" r="4" fill="currentColor"/></svg>',
|
|
411
|
+
hand: '<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M9 11V5a2 2 0 0 1 4 0v6"/><path d="M13 10V8a2 2 0 0 1 4 0v5a7 7 0 0 1-7 7h-.5A6.5 6.5 0 0 1 5 17.5L4 15a2 2 0 0 1 3.4-2L9 15"/></svg>',
|
|
412
|
+
pause: '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true"><rect x="6" y="5" width="4" height="14" rx="1"/><rect x="14" y="5" width="4" height="14" rx="1"/></svg>'
|
|
413
|
+
};
|
|
414
|
+
var bone = (style) => `<span class="bone" style="${style}"></span>`;
|
|
415
|
+
var PANEL_HTML = `<!doctype html>
|
|
416
|
+
<html lang="en">
|
|
417
|
+
<head>
|
|
418
|
+
<meta charset="utf-8">
|
|
419
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
420
|
+
<title>Browser</title>
|
|
421
|
+
<style>
|
|
422
|
+
:root {
|
|
423
|
+
color-scheme: light;
|
|
424
|
+
--ground: color-mix(in srgb, var(--cb-ink, #1A1A18) 5%, var(--cb-bg, #FAF9F6));
|
|
425
|
+
--frame-shadow: 0 1px 2px rgba(26,26,24,.06), 0 4px 16px rgba(26,26,24,.06);
|
|
426
|
+
--bone: color-mix(in srgb, var(--cb-ink, #1A1A18) 7%, transparent);
|
|
427
|
+
--radius: 6px;
|
|
428
|
+
}
|
|
429
|
+
:root[data-theme="dark"] {
|
|
430
|
+
color-scheme: dark;
|
|
431
|
+
--ground: var(--cb-bg, #0A0A0A);
|
|
432
|
+
--frame-shadow: 0 0 0 1px rgba(255,255,255,.08);
|
|
433
|
+
}
|
|
434
|
+
* { box-sizing: border-box; }
|
|
435
|
+
html, body { height: 100%; }
|
|
436
|
+
body {
|
|
437
|
+
margin: 0; position: relative; overflow: hidden;
|
|
438
|
+
background: var(--cb-panel-2, Canvas); color: var(--cb-ink, CanvasText);
|
|
439
|
+
font-family: var(--cb-font, ui-monospace, "SF Mono", Menlo, monospace);
|
|
440
|
+
font-size: var(--cb-type-body-size, 13px); line-height: var(--cb-type-body-line, 20px);
|
|
441
|
+
}
|
|
442
|
+
/* nothing before the host's tokens and language (init) */
|
|
443
|
+
body:not([data-ready]) { visibility: hidden; }
|
|
444
|
+
[hidden] { display: none !important; }
|
|
445
|
+
button, input { font: inherit; color: inherit; margin: 0; }
|
|
446
|
+
button {
|
|
447
|
+
display: inline-grid; place-items: center; flex: none; height: 28px; min-width: 28px;
|
|
448
|
+
padding: 0; border: 0; border-radius: var(--radius); background: transparent; cursor: pointer;
|
|
449
|
+
white-space: nowrap;
|
|
450
|
+
}
|
|
451
|
+
button:hover:not(:disabled) { background: var(--cb-hover, rgba(26,26,24,.04)); }
|
|
452
|
+
button:disabled { color: var(--cb-disabled, GrayText); cursor: default; }
|
|
453
|
+
button:focus-visible, input:focus-visible { outline: 2px solid var(--cb-focus-ring, Highlight); outline-offset: 1px; }
|
|
454
|
+
.primary, .secondary, .outline { padding: 0 14px; font-weight: 600; }
|
|
455
|
+
.primary { background: var(--cb-accent, #CC4609); color: var(--cb-on-accent, #fff); }
|
|
456
|
+
.primary:hover:not(:disabled) { background: var(--cb-accent-hover, var(--cb-accent, #CC4609)); }
|
|
457
|
+
.secondary { font-weight: 400; box-shadow: inset 0 0 0 1px var(--cb-line, #D9D8D2); }
|
|
458
|
+
.outline { padding: 0 12px; color: var(--cb-accent, #CC4609); box-shadow: inset 0 0 0 1px var(--cb-accent, #CC4609); }
|
|
459
|
+
|
|
460
|
+
.layer { position: absolute; inset: 0; display: flex; flex-direction: column; }
|
|
461
|
+
/* laid out underneath while the skeleton or the idle state shows, so the switch never reflows */
|
|
462
|
+
#app[data-loading], #app[data-idle] { visibility: hidden; }
|
|
463
|
+
.row { flex: none; display: flex; align-items: center; padding: 0 12px; }
|
|
464
|
+
|
|
465
|
+
/* tab strip */
|
|
466
|
+
#tabs { height: 32px; align-items: stretch; gap: 4px; }
|
|
467
|
+
#tablist { flex: 1 1 auto; min-width: 0; display: flex; align-items: stretch; gap: 2px; overflow-x: auto; scrollbar-width: none; }
|
|
468
|
+
/* a group of the tree's other conversations: a quiet label before its tabs, set apart by space */
|
|
469
|
+
#tablist .group { flex: none; align-self: center; margin-left: 12px; padding: 0 2px; font-size: var(--cb-type-micro-size, 11px); line-height: var(--cb-type-micro-line, 14px); color: var(--cb-muted, #5E5E5A); white-space: nowrap; }
|
|
470
|
+
#others { align-self: center; height: 24px; padding: 0 8px; font-size: var(--cb-type-micro-size, 11px); color: var(--cb-muted, #5E5E5A); }
|
|
471
|
+
#others[aria-expanded="true"] { background: var(--cb-hover, rgba(26,26,24,.04)); }
|
|
472
|
+
.tab { display: flex; align-items: center; gap: 6px; flex: 0 1 auto; min-width: 64px; max-width: 200px; padding: 0 10px; color: var(--cb-muted, #5E5E5A); }
|
|
473
|
+
.tab[data-active="true"] { color: var(--cb-ink, #1A1A18); font-weight: 500; box-shadow: inset 0 -2px 0 var(--cb-accent, #CC4609); padding-right: 6px; }
|
|
474
|
+
/* the icon slot: always a deliberate 14 px glyph (spinner while loading, globe without an icon)
|
|
475
|
+
or the site's icon, the same box in every state so nothing shifts when the icon arrives */
|
|
476
|
+
.fav { display: inline-grid; place-items: center; width: 14px; height: 14px; flex: none; color: var(--cb-muted, #5E5E5A); }
|
|
477
|
+
.fav svg { width: 14px; height: 14px; }
|
|
478
|
+
img.fav { object-fit: contain; border-radius: 2px; }
|
|
479
|
+
.fav[data-glyph="loading"] { color: var(--cb-accent, #CC4609); }
|
|
480
|
+
.tab .label { flex: 1; min-width: 0; display: block; height: auto; text-align: left; overflow: hidden; text-overflow: ellipsis; font-weight: inherit; }
|
|
481
|
+
.tab .label:hover:not(:disabled) { background: transparent; }
|
|
482
|
+
.tab .label:disabled { color: inherit; }
|
|
483
|
+
.tab .x { width: 20px; min-width: 20px; height: 20px; border-radius: 4px; color: var(--cb-muted, #5E5E5A); }
|
|
484
|
+
#tablist .add { align-self: center; color: var(--cb-muted, #5E5E5A); }
|
|
485
|
+
|
|
486
|
+
/* toolbar */
|
|
487
|
+
#bar { height: 44px; gap: 4px; }
|
|
488
|
+
#field { flex: 1 1 0; min-width: 0; height: 28px; margin-left: 4px; position: relative; display: flex; align-items: center; border-radius: var(--radius); background: var(--cb-hover, rgba(26,26,24,.05)); }
|
|
489
|
+
#field[data-edit] { background: var(--cb-panel, #fff); box-shadow: inset 0 0 0 1px var(--cb-line, #D9D8D2); }
|
|
490
|
+
#lock { display: inline-flex; flex: none; margin-left: 10px; color: var(--cb-muted, #5E5E5A); }
|
|
491
|
+
#url { flex: 1; min-width: 0; height: 100%; padding: 0 10px 0 6px; border: 0; outline: 0; background: transparent; color: transparent; caret-color: var(--cb-ink, #1A1A18); text-overflow: ellipsis; }
|
|
492
|
+
#lock[hidden] + #url, #lock[hidden] ~ #urltext { padding-left: 10px; }
|
|
493
|
+
#url:focus { color: var(--cb-ink, #1A1A18); }
|
|
494
|
+
#urltext { position: absolute; inset: 0 10px 0 26px; display: flex; align-items: center; pointer-events: none; white-space: nowrap; overflow: hidden; }
|
|
495
|
+
#lock[hidden] ~ #urltext { left: 10px; padding-left: 0; }
|
|
496
|
+
#urltext span:first-child { flex: none; max-width: 100%; overflow: hidden; text-overflow: ellipsis; }
|
|
497
|
+
#urltext span:last-child { color: var(--cb-muted, #5E5E5A); overflow: hidden; text-overflow: ellipsis; }
|
|
498
|
+
#url:focus ~ #urltext { visibility: hidden; }
|
|
499
|
+
#takeover, #handback { margin-left: 4px; }
|
|
500
|
+
/* the takeover slot is always reserved: gone only when hand back takes its place */
|
|
501
|
+
#takeover[hidden]:not([data-gone]) { display: inline-grid !important; visibility: hidden; }
|
|
502
|
+
#tabsbtn { flex: 1 1 0; min-width: 0; display: flex; align-items: center; gap: 6px; padding: 0 8px; background: var(--cb-hover, rgba(26,26,24,.05)); text-align: left; }
|
|
503
|
+
#tabsbtn .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
|
|
504
|
+
#tabsbtn .count { min-width: 18px; height: 18px; padding: 0 4px; border-radius: 4px; box-shadow: inset 0 0 0 1px var(--cb-line-strong, #B5B5AE); font-size: 11px; line-height: 18px; text-align: center; color: var(--cb-muted, #5E5E5A); }
|
|
505
|
+
#tabsbtn, #more { display: none; }
|
|
506
|
+
@media (max-width: 440px) {
|
|
507
|
+
#tabs, #forward, #reload, #field { display: none; }
|
|
508
|
+
#tabsbtn { display: flex; }
|
|
509
|
+
#more { display: inline-grid; color: var(--cb-muted, #5E5E5A); }
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/* status line: one fixed-height line */
|
|
513
|
+
#status { height: 24px; gap: 6px; margin: 0; padding: 0 16px; font-size: var(--cb-type-micro-size, 11px); line-height: var(--cb-type-micro-line, 14px); color: var(--cb-muted, #5E5E5A); white-space: nowrap; overflow: hidden; }
|
|
514
|
+
#status .ico { display: inline-flex; flex: none; }
|
|
515
|
+
#status .lead { flex: none; }
|
|
516
|
+
#status .detail { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
|
|
517
|
+
#status[data-tone="agent"] .ico, #status[data-tone="agent"] .lead { color: var(--cb-accent, #CC4609); }
|
|
518
|
+
#status[data-tone="user"] .ico, #status[data-tone="user"] .lead { color: var(--cb-ink, #1A1A18); }
|
|
519
|
+
.spin { animation: spin 1s linear infinite; }
|
|
520
|
+
@keyframes spin { to { transform: rotate(360deg); } }
|
|
521
|
+
|
|
522
|
+
/* needs the person: a warn strip in the status line's place */
|
|
523
|
+
.warn { flex: none; display: flex; align-items: center; gap: 10px; padding: 8px 12px 8px 16px; background: color-mix(in srgb, var(--cb-warn, #8F6500) 8%, transparent); }
|
|
524
|
+
.warn > .ico { display: inline-flex; flex: none; color: var(--cb-warn, #8F6500); }
|
|
525
|
+
.warn .text { flex: 1; min-width: 0; display: flex; flex-direction: column; }
|
|
526
|
+
.warn .title { color: var(--cb-warn, #8F6500); font-weight: 500; overflow-wrap: anywhere; }
|
|
527
|
+
.warn .body { font-family: var(--cb-font-prose, system-ui, sans-serif); color: var(--cb-muted, #5E5E5A); overflow-wrap: anywhere; }
|
|
528
|
+
.warn .actions { display: flex; gap: 6px; flex: none; }
|
|
529
|
+
|
|
530
|
+
/* the picture, contained and centred on a neutral ground */
|
|
531
|
+
#stage { flex: 1; min-height: 0; position: relative; display: flex; align-items: center; justify-content: center; padding: 12px; overflow: hidden; background: var(--ground); }
|
|
532
|
+
#view { display: block; flex: none; border-radius: var(--radius); background: #fff; box-shadow: var(--frame-shadow); outline: none; }
|
|
533
|
+
#stage[data-control="user"] #view { box-shadow: 0 0 0 2px var(--cb-accent, #CC4609), 0 4px 16px rgba(26,26,24,.08); }
|
|
534
|
+
#view:focus-visible { box-shadow: 0 0 0 2px var(--cb-focus-ring, Highlight), var(--frame-shadow); }
|
|
535
|
+
#flash { position: absolute; width: 24px; height: 24px; margin: -12px 0 0 -12px; border: 2px solid var(--cb-accent, #CC4609); border-radius: 50%; pointer-events: none; }
|
|
536
|
+
.center { position: absolute; inset: 0; display: flex; flex-direction: column; gap: 6px; align-items: center; justify-content: center; padding: 16px; text-align: center; background: var(--ground); }
|
|
537
|
+
.center h2 { margin: 0; font-size: inherit; font-weight: 600; }
|
|
538
|
+
.center p { margin: 0; font-family: var(--cb-font-prose, system-ui, sans-serif); color: var(--cb-muted, #5E5E5A); }
|
|
539
|
+
.center button { margin-top: 6px; }
|
|
540
|
+
/* short-lived notices float over the picture's bottom edge: they never move it */
|
|
541
|
+
#strips { position: absolute; left: 12px; right: 12px; bottom: 12px; display: flex; flex-direction: column; gap: 6px; pointer-events: none; }
|
|
542
|
+
.strip { display: flex; align-items: center; gap: 8px; padding: 6px 6px 6px 12px; pointer-events: auto; border-radius: var(--cb-radius-md, 8px); background: var(--cb-raised, #fff); box-shadow: var(--cb-shadow-float, 0 8px 24px rgba(26,26,24,.08)), 0 0 0 1px var(--cb-line-soft, #E8E7E2); }
|
|
543
|
+
.strip p { flex: 1; min-width: 0; margin: 0; overflow-wrap: anywhere; }
|
|
544
|
+
.strip .label { color: var(--cb-muted, #5E5E5A); }
|
|
545
|
+
.strip:not(:has(button)) { padding-right: 12px; }
|
|
546
|
+
|
|
547
|
+
/* menus (narrow: tabs, more) */
|
|
548
|
+
.menu { position: absolute; z-index: 2; min-width: 180px; max-width: calc(100% - 24px); max-height: 60%; overflow: auto; padding: 4px; border-radius: var(--cb-radius-md, 8px); background: var(--cb-raised, #fff); box-shadow: var(--cb-shadow-float, 0 8px 24px rgba(26,26,24,.08)), 0 0 0 1px var(--cb-line-soft, #E8E7E2); }
|
|
549
|
+
.menu button { display: flex; width: 100%; justify-content: flex-start; gap: 8px; padding: 0 8px; text-align: left; overflow: hidden; text-overflow: ellipsis; }
|
|
550
|
+
.menu button[aria-current="true"] { font-weight: 600; }
|
|
551
|
+
.menu .heading { margin: 0; padding: 6px 8px 2px; font-size: var(--cb-type-micro-size, 11px); line-height: var(--cb-type-micro-line, 14px); color: var(--cb-muted, #5E5E5A); }
|
|
552
|
+
.menu .heading:not(:first-child) { margin-top: 4px; }
|
|
553
|
+
.menu button .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
|
|
554
|
+
.menu button .hint { flex: none; margin-left: 8px; color: var(--cb-accent, #CC4609); }
|
|
555
|
+
|
|
556
|
+
/* the skeleton: the panel's own shape, quietly pulsing */
|
|
557
|
+
.bone { display: block; flex: none; border-radius: 2px; background: var(--bone); animation: pulse 1.6s ease-in-out infinite; }
|
|
558
|
+
@keyframes pulse { 50% { opacity: .5; } }
|
|
559
|
+
#skeleton .shot { width: 100%; max-height: 100%; aspect-ratio: 16 / 10; display: flex; flex-direction: column; gap: 12px; padding: 20px; border-radius: var(--radius); background: var(--cb-panel, #fff); box-shadow: 0 1px 2px rgba(26,26,24,.06); }
|
|
560
|
+
:root[data-theme="dark"] #skeleton .shot { background: var(--cb-raised, #141413); box-shadow: var(--frame-shadow); }
|
|
561
|
+
#skeleton .narrow { display: none; }
|
|
562
|
+
@media (max-width: 440px) {
|
|
563
|
+
#skeleton .wide { display: none; }
|
|
564
|
+
#skeleton .narrow { display: block; }
|
|
565
|
+
}
|
|
566
|
+
@media (prefers-reduced-motion: reduce) { .bone, .spin { animation: none; } }
|
|
567
|
+
</style>
|
|
568
|
+
</head>
|
|
569
|
+
<body>
|
|
570
|
+
<div id="skeleton" class="layer" aria-busy="true">
|
|
571
|
+
<div class="row wide" style="height:32px;gap:8px;padding:0 22px;display:flex">${bone("width:14px;height:14px;border-radius:3px")}${bone("width:96px;height:10px")}</div>
|
|
572
|
+
<div class="row" style="height:44px;gap:4px">${bone("width:28px;height:28px;border-radius:6px")}<span class="wide" style="display:contents">${bone("width:28px;height:28px;border-radius:6px")}${bone("width:28px;height:28px;border-radius:6px")}</span>${bone("flex:1;height:28px;margin-left:4px;border-radius:6px")}${bone("width:56px;height:28px;margin-left:4px;border-radius:6px")}</div>
|
|
573
|
+
<p id="skeleton-status" class="row" role="status" style="height:24px;margin:0;padding:0 16px;font-size:11px;line-height:14px;color:var(--cb-muted,#5E5E5A)"></p>
|
|
574
|
+
<div style="flex:1;min-height:0;display:flex;align-items:center;justify-content:center;padding:12px;background:var(--ground)">
|
|
575
|
+
<div class="shot">${bone("width:40%;height:12px")}${bone("width:75%;height:10px")}${bone("width:60%;height:10px")}${bone("flex:1;border-radius:4px")}</div>
|
|
576
|
+
</div>
|
|
577
|
+
</div>
|
|
578
|
+
<div id="app" class="layer" data-loading>
|
|
579
|
+
<div id="tabs" class="row"><div id="tablist" role="tablist"></div><button type="button" id="others" aria-haspopup="menu" hidden></button></div>
|
|
580
|
+
<div id="bar" class="row">
|
|
581
|
+
<button type="button" id="back">${ICONS.back}</button>
|
|
582
|
+
<button type="button" id="forward">${ICONS.forward}</button>
|
|
583
|
+
<button type="button" id="reload">${ICONS.reload}</button>
|
|
584
|
+
<button type="button" id="tabsbtn" aria-haspopup="menu"><span class="fav" data-glyph="site">${ICONS.globe}</span><span class="name" id="tabsbtn-name"></span><span class="count" id="tabsbtn-count"></span></button>
|
|
585
|
+
<label id="field"><span id="lock" role="img" hidden>${ICONS.lock}</span><input id="url" type="text" spellcheck="false" autocomplete="off"><span id="urltext"><span id="urltext-host"></span><span id="urltext-path"></span></span></label>
|
|
586
|
+
<button type="button" id="more" aria-haspopup="menu">${ICONS.more}</button>
|
|
587
|
+
<button type="button" id="takeover" class="primary" hidden></button>
|
|
588
|
+
<button type="button" id="handback" class="outline" hidden></button>
|
|
589
|
+
</div>
|
|
590
|
+
<p id="status" class="row" role="status"><span class="ico" id="status-icon"></span><span class="lead" id="status-lead"></span><span class="detail" id="status-detail"></span></p>
|
|
591
|
+
<div id="verify" class="warn" hidden><span class="ico">${ICONS.pause}</span><div class="text"><span class="title" id="verify-title"></span><span class="body" id="verify-body"></span></div><div class="actions"><button type="button" id="verify-go" class="primary"></button></div></div>
|
|
592
|
+
<div id="handoff" class="warn" hidden><span class="ico">${ICONS.pause}</span><div class="text"><span class="title" id="handoff-label"></span><span class="body" id="handoff-message"></span></div><div class="actions"><button type="button" id="handoff-done" class="primary"></button><button type="button" id="handoff-cancel" class="secondary"></button></div></div>
|
|
593
|
+
<div id="stage">
|
|
594
|
+
<canvas id="view" tabindex="0"></canvas>
|
|
595
|
+
<div id="flash" hidden></div>
|
|
596
|
+
<div id="strips">
|
|
597
|
+
<div id="blocked" class="strip" hidden><p><span id="blocked-label" class="label"></span> <span id="blocked-host"></span></p><button type="button" id="allow" class="primary"></button></div>
|
|
598
|
+
<div id="dialog" class="strip" hidden><p><span id="dialog-label" class="label"></span> <span id="dialog-text"></span></p></div>
|
|
599
|
+
<div id="notice" class="strip" role="alert" hidden><p><span id="notice-text"></span></p></div>
|
|
600
|
+
</div>
|
|
601
|
+
</div>
|
|
602
|
+
</div>
|
|
603
|
+
<div id="empty" class="layer center" hidden>
|
|
604
|
+
<h2 id="empty-title"></h2>
|
|
605
|
+
<p id="empty-body"></p>
|
|
606
|
+
<button type="button" id="reconnect" class="secondary"></button>
|
|
607
|
+
<button type="button" id="empty-others" class="secondary" aria-haspopup="menu" hidden></button>
|
|
608
|
+
</div>
|
|
609
|
+
<div id="menu" class="menu" role="menu" hidden></div>
|
|
610
|
+
<script src="panel.js"><\/script>
|
|
611
|
+
</body>
|
|
612
|
+
</html>
|
|
613
|
+
`;
|
|
614
|
+
|
|
84
615
|
// ui-src/panel/panel.ts
|
|
85
616
|
var PROTOCOL = "capybara-embed/1";
|
|
86
617
|
var ASPECT = VIEWPORT.width / VIEWPORT.height;
|
|
87
618
|
var SIDE_MAX_WIDTH = 640;
|
|
619
|
+
var MAX_DPR = 1.25;
|
|
620
|
+
var STAGE_PAD = 12;
|
|
88
621
|
var HIDDEN_CLOSE_MS = 6e4;
|
|
89
622
|
var COMPACT_BELOW = 600;
|
|
90
|
-
var MOVE_INTERVAL_MS = 50;
|
|
91
623
|
var RESTART_RETRY_MS = [1e3, 3e3];
|
|
92
624
|
var EMPTY_POLL_MS = 1e4;
|
|
93
625
|
var EMPTY_POLL_FOR_MS = 30 * 6e4;
|
|
@@ -127,11 +659,15 @@
|
|
|
127
659
|
let sock = null;
|
|
128
660
|
let mode = "connecting";
|
|
129
661
|
let tabs = [];
|
|
662
|
+
let otherTabs = [];
|
|
130
663
|
let control = "agent";
|
|
131
664
|
let lastState = null;
|
|
132
665
|
let retried401 = false;
|
|
133
666
|
let attempts = 0;
|
|
134
667
|
let restartRetries = 0;
|
|
668
|
+
let wasLive = false;
|
|
669
|
+
let quiet = false;
|
|
670
|
+
let full = false;
|
|
135
671
|
let retryTimer;
|
|
136
672
|
let emptyTimer;
|
|
137
673
|
let emptySince;
|
|
@@ -146,22 +682,49 @@
|
|
|
146
682
|
let blockedUrl = "";
|
|
147
683
|
let pendingHandoff = null;
|
|
148
684
|
let sentSize = "";
|
|
149
|
-
let lastMove = Number.NEGATIVE_INFINITY;
|
|
150
685
|
let image = null;
|
|
151
|
-
|
|
686
|
+
let layer = "skeleton";
|
|
687
|
+
const compact = () => {
|
|
688
|
+
const sw = win.screen?.width ?? 0;
|
|
689
|
+
return sw > 0 && sw < COMPACT_BELOW;
|
|
690
|
+
};
|
|
152
691
|
const text = (key) => t(locale, key);
|
|
153
692
|
const interactive = () => !compact() && control === "user";
|
|
693
|
+
const treeEmpty = () => mode === "live" && lastState !== null && tabs.length === 0;
|
|
154
694
|
function send(msg) {
|
|
155
695
|
if (sock === null) return;
|
|
156
696
|
sock.send(new TextEncoder().encode(JSON.stringify(msg)));
|
|
157
697
|
}
|
|
158
|
-
const
|
|
698
|
+
const input = mountInput({
|
|
699
|
+
window: win,
|
|
700
|
+
view,
|
|
701
|
+
stage,
|
|
702
|
+
send,
|
|
703
|
+
interactive: () => interactive(),
|
|
704
|
+
toPage: (clientX, clientY) => {
|
|
705
|
+
const r = view.getBoundingClientRect();
|
|
706
|
+
return mapPoint(clientX - r.left, clientY - r.top, { x: 0, y: 0, w: r.width, h: r.height });
|
|
707
|
+
},
|
|
708
|
+
label: t(locale, "view")
|
|
709
|
+
});
|
|
710
|
+
function askMode(next) {
|
|
711
|
+
post({ protocol: PROTOCOL, type: "panel-mode", full: next });
|
|
712
|
+
}
|
|
713
|
+
function hostMode(next) {
|
|
714
|
+
if (next === full) return;
|
|
715
|
+
full = next;
|
|
716
|
+
onBoxChange();
|
|
717
|
+
}
|
|
159
718
|
function setLabels() {
|
|
160
719
|
const set = (id, key) => {
|
|
161
720
|
$(id).textContent = text(key);
|
|
162
721
|
};
|
|
163
722
|
set("takeover", "takeover");
|
|
164
723
|
set("handback", "handback");
|
|
724
|
+
set("verify-title", "verifyTitle");
|
|
725
|
+
set("verify-body", "verifyBody");
|
|
726
|
+
set("verify-go", "verifyGo");
|
|
727
|
+
set("skeleton-status", "statusConnecting");
|
|
165
728
|
set("allow", "allow");
|
|
166
729
|
set("reconnect", "reconnect");
|
|
167
730
|
set("blocked-label", "blocked");
|
|
@@ -169,16 +732,17 @@
|
|
|
169
732
|
set("handoff-label", "handoff");
|
|
170
733
|
set("handoff-done", "handoffDone");
|
|
171
734
|
set("handoff-cancel", "handoffCancel");
|
|
735
|
+
input.sink.setAttribute("aria-label", text("view"));
|
|
172
736
|
$("lock").setAttribute("aria-label", text("secure"));
|
|
173
737
|
$("lock").setAttribute("title", text("secure"));
|
|
174
738
|
for (const [id, key] of [
|
|
175
739
|
["back", "back"],
|
|
176
740
|
["forward", "forward"],
|
|
177
|
-
["reload", "reload"]
|
|
741
|
+
["reload", "reload"],
|
|
742
|
+
["more", "more"]
|
|
178
743
|
]) {
|
|
179
744
|
$(id).setAttribute("aria-label", text(key));
|
|
180
745
|
$(id).setAttribute("title", text(key));
|
|
181
|
-
$(id).textContent = id === "back" ? "<" : id === "forward" ? ">" : "↻";
|
|
182
746
|
}
|
|
183
747
|
url.setAttribute("aria-label", text("address"));
|
|
184
748
|
view.setAttribute("aria-label", text("view"));
|
|
@@ -188,32 +752,62 @@
|
|
|
188
752
|
function renderStatus() {
|
|
189
753
|
const el = $("status");
|
|
190
754
|
el.setAttribute("data-control", control);
|
|
191
|
-
let
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
755
|
+
let tone = "quiet";
|
|
756
|
+
let icon = "";
|
|
757
|
+
let lead = null;
|
|
758
|
+
let detail = null;
|
|
759
|
+
if (mode === "connecting") lead = "statusConnecting";
|
|
760
|
+
else if (mode === "live" && control === "user") {
|
|
761
|
+
if (interactive()) {
|
|
762
|
+
tone = "user";
|
|
763
|
+
icon = ICONS.hand;
|
|
764
|
+
lead = "statusUser";
|
|
765
|
+
detail = "statusUserDetail";
|
|
766
|
+
} else lead = "statusUserElsewhere";
|
|
767
|
+
} else if (mode === "live") {
|
|
768
|
+
tone = "agent";
|
|
769
|
+
const loading = lastState?.loading === true;
|
|
770
|
+
icon = loading ? ICONS.spin : ICONS.dot;
|
|
771
|
+
lead = "statusAgent";
|
|
772
|
+
if (loading) detail = "statusLoading";
|
|
773
|
+
}
|
|
774
|
+
el.setAttribute("data-tone", tone);
|
|
775
|
+
$("status-icon").innerHTML = icon;
|
|
776
|
+
$("status-lead").textContent = lead === null ? "" : text(lead);
|
|
777
|
+
$("status-detail").textContent = detail === null ? "" : ` · ${text(detail)}`;
|
|
197
778
|
}
|
|
198
779
|
function renderChrome() {
|
|
199
780
|
const watchOnly = compact();
|
|
781
|
+
const readOnly = !interactive();
|
|
200
782
|
const isLive = mode === "live";
|
|
201
783
|
$("takeover").hidden = watchOnly || !isLive || control === "user";
|
|
202
784
|
$("handback").hidden = watchOnly || !isLive || control !== "user";
|
|
785
|
+
$("takeover").toggleAttribute("data-gone", watchOnly || isLive && control === "user");
|
|
786
|
+
const verifying = isLive && control === "agent" && tabs.find((x) => x.active)?.blocker?.kind === "verification" && !watchOnly;
|
|
787
|
+
$("verify").hidden = !verifying;
|
|
788
|
+
$("takeover").className = verifying ? "secondary" : "primary";
|
|
789
|
+
$("handoff").hidden = pendingHandoff === null || verifying;
|
|
790
|
+
$("status").hidden = verifying || pendingHandoff !== null;
|
|
791
|
+
$("stage").setAttribute("data-control", isLive ? control : "agent");
|
|
792
|
+
$("field").toggleAttribute("data-edit", interactive());
|
|
203
793
|
for (const id of ["back", "forward", "reload"])
|
|
204
|
-
$(id).disabled =
|
|
205
|
-
|
|
206
|
-
|
|
794
|
+
$(id).disabled = readOnly;
|
|
795
|
+
$("more").disabled = readOnly;
|
|
796
|
+
url.readOnly = readOnly;
|
|
797
|
+
input.sync();
|
|
207
798
|
$("lock").hidden = !isLive || lastState?.secure !== true;
|
|
208
799
|
const showEmpty = mode === "empty" || mode === "offline" || mode === "error";
|
|
209
|
-
$("empty").hidden = !showEmpty;
|
|
210
800
|
$("reconnect").hidden = !showEmpty;
|
|
211
801
|
const keys = {
|
|
212
802
|
empty: ["emptyTitle", "emptyBody"],
|
|
213
803
|
offline: ["offlineTitle", "offlineBody"],
|
|
214
|
-
error: ["errorTitle", "errorBody"]
|
|
804
|
+
error: ["errorTitle", "errorBody"],
|
|
805
|
+
tree: ["treeEmptyTitle", "treeEmptyBody"]
|
|
215
806
|
};
|
|
216
|
-
const k = keys[mode];
|
|
807
|
+
const k = keys[treeEmpty() ? "tree" : mode];
|
|
808
|
+
const emptyOthers = $("empty-others");
|
|
809
|
+
emptyOthers.hidden = !treeEmpty() || otherTabs.length === 0;
|
|
810
|
+
emptyOthers.textContent = othersLabel();
|
|
217
811
|
if (k !== void 0) {
|
|
218
812
|
$("empty-title").textContent = text(k[0]);
|
|
219
813
|
$("empty-body").textContent = text(k[1]);
|
|
@@ -221,27 +815,58 @@
|
|
|
221
815
|
$("blocked").hidden = blockedHost === "";
|
|
222
816
|
$("blocked-host").textContent = blockedHost;
|
|
223
817
|
renderStatus();
|
|
818
|
+
renderLoading();
|
|
819
|
+
}
|
|
820
|
+
function renderLoading() {
|
|
821
|
+
const idleMode = mode === "empty" || mode === "offline" || mode === "error" || treeEmpty();
|
|
822
|
+
let next;
|
|
823
|
+
if (idleMode) next = "idle";
|
|
824
|
+
else if (mode === "live" && image !== null) next = "app";
|
|
825
|
+
else if (mode === "live" && layer === "idle") next = "idle";
|
|
826
|
+
else next = "skeleton";
|
|
827
|
+
layer = next;
|
|
828
|
+
$("app").toggleAttribute("data-loading", next === "skeleton");
|
|
829
|
+
$("app").toggleAttribute("data-idle", next === "idle");
|
|
830
|
+
$("skeleton").hidden = next !== "skeleton";
|
|
831
|
+
$("empty").hidden = next !== "idle";
|
|
832
|
+
if (next === "app") onBoxChange();
|
|
224
833
|
}
|
|
225
834
|
function renderTabs() {
|
|
226
|
-
const box = $("
|
|
835
|
+
const box = $("tablist");
|
|
227
836
|
box.textContent = "";
|
|
228
|
-
const watchOnly =
|
|
837
|
+
const watchOnly = !interactive();
|
|
838
|
+
const subs = subGroups();
|
|
839
|
+
let group = "own";
|
|
229
840
|
for (const tab of tabs) {
|
|
841
|
+
if (tab.group !== group) {
|
|
842
|
+
group = tab.group;
|
|
843
|
+
if (group !== "own") {
|
|
844
|
+
const label2 = doc.createElement("span");
|
|
845
|
+
label2.className = "group";
|
|
846
|
+
label2.setAttribute("role", "presentation");
|
|
847
|
+
label2.textContent = groupLabel(group, subs);
|
|
848
|
+
box.appendChild(label2);
|
|
849
|
+
}
|
|
850
|
+
}
|
|
230
851
|
const el = doc.createElement("div");
|
|
231
852
|
el.className = "tab";
|
|
232
853
|
el.setAttribute("role", "tab");
|
|
233
854
|
el.setAttribute("data-active", String(tab.active));
|
|
855
|
+
el.setAttribute("data-group", tab.group);
|
|
856
|
+
el.setAttribute("aria-selected", String(tab.active));
|
|
857
|
+
el.appendChild(favOf(tab));
|
|
234
858
|
const label = doc.createElement("button");
|
|
235
859
|
label.type = "button";
|
|
236
860
|
label.className = "label";
|
|
237
|
-
label.textContent = tab
|
|
238
|
-
label.
|
|
239
|
-
label.addEventListener("click", () =>
|
|
861
|
+
label.textContent = tabName(tab);
|
|
862
|
+
label.title = tabName(tab);
|
|
863
|
+
label.addEventListener("click", () => pickTab(tab));
|
|
240
864
|
el.appendChild(label);
|
|
241
|
-
if (!watchOnly) {
|
|
865
|
+
if (!watchOnly && tab.active && control === "user") {
|
|
242
866
|
const close = doc.createElement("button");
|
|
243
867
|
close.type = "button";
|
|
244
|
-
close.
|
|
868
|
+
close.className = "x";
|
|
869
|
+
close.innerHTML = ICONS.close;
|
|
245
870
|
close.setAttribute("aria-label", text("closeTab"));
|
|
246
871
|
close.addEventListener("click", () => send({ t: "tab", action: "close", tabId: tab.id }));
|
|
247
872
|
el.appendChild(close);
|
|
@@ -251,21 +876,151 @@
|
|
|
251
876
|
if (!watchOnly) {
|
|
252
877
|
const add = doc.createElement("button");
|
|
253
878
|
add.type = "button";
|
|
254
|
-
add.
|
|
879
|
+
add.className = "add";
|
|
880
|
+
add.innerHTML = ICONS.plus;
|
|
255
881
|
add.setAttribute("aria-label", text("newTab"));
|
|
256
882
|
add.addEventListener("click", () => send({ t: "tab", action: "open" }));
|
|
257
883
|
box.appendChild(add);
|
|
258
884
|
}
|
|
885
|
+
const others = $("others");
|
|
886
|
+
others.hidden = otherTabs.length === 0;
|
|
887
|
+
others.textContent = othersLabel();
|
|
888
|
+
const active = tabs.find((x) => x.active);
|
|
889
|
+
$("tabsbtn").querySelector(".fav")?.replaceWith(favOf(active));
|
|
890
|
+
$("tabsbtn-name").textContent = active === void 0 ? "" : hostOf(active.url) || tabName(active);
|
|
891
|
+
$("tabsbtn-count").textContent = String(tabs.length);
|
|
892
|
+
$("tabsbtn").setAttribute("aria-label", `${text("tabs")}: ${tabs.length}`);
|
|
893
|
+
}
|
|
894
|
+
function pickTab(tab) {
|
|
895
|
+
if (interactive()) send({ t: "tab", action: "switch", tabId: tab.id });
|
|
896
|
+
else if (!tab.active) send({ t: "show", tabId: tab.id });
|
|
897
|
+
}
|
|
898
|
+
const subGroups = () => [
|
|
899
|
+
...new Set(tabs.map((x) => x.group).filter((g) => g !== "own"))
|
|
900
|
+
];
|
|
901
|
+
const groupLabel = (group, subs) => {
|
|
902
|
+
if (subs.length <= 1) return text("subTask");
|
|
903
|
+
const n = /^sub(\d+)$/.exec(group)?.[1];
|
|
904
|
+
return n === void 0 ? text("subTask") : `${text("subTask")} ${n}`;
|
|
905
|
+
};
|
|
906
|
+
const othersLabel = () => `${text("otherConversations")} (${otherTabs.length})`;
|
|
907
|
+
function otherItems() {
|
|
908
|
+
return otherTabs.map((tab) => ({
|
|
909
|
+
label: tab.title !== "" ? tab.title : tab.url === "about:blank" ? text("newTab") : tab.url,
|
|
910
|
+
hint: text("moveHere"),
|
|
911
|
+
// another conversation's tab carries no icon (by design): the neutral globe
|
|
912
|
+
fav: () => glyph("site"),
|
|
913
|
+
run: () => send({ t: "tab", action: "adopt", tabId: tab.id })
|
|
914
|
+
}));
|
|
915
|
+
}
|
|
916
|
+
function openOthers(anchor) {
|
|
917
|
+
openMenu(anchor, [{ label: othersLabel(), heading: true }, ...otherItems()]);
|
|
918
|
+
}
|
|
919
|
+
const tabName = (tab) => tab.title !== "" ? tab.title : tab.url === "about:blank" ? text("newTab") : tab.url;
|
|
920
|
+
function favOf(tab) {
|
|
921
|
+
if (tab !== void 0 && isIconDataUrl(tab.favicon)) {
|
|
922
|
+
const img = doc.createElement("img");
|
|
923
|
+
img.className = "fav";
|
|
924
|
+
img.alt = "";
|
|
925
|
+
img.src = tab.favicon;
|
|
926
|
+
img.addEventListener("error", () => img.replaceWith(glyph("site")), { once: true });
|
|
927
|
+
return img;
|
|
928
|
+
}
|
|
929
|
+
return glyph(tab?.loading === true ? "loading" : "site");
|
|
930
|
+
}
|
|
931
|
+
function glyph(kind) {
|
|
932
|
+
const fav = doc.createElement("span");
|
|
933
|
+
fav.className = "fav";
|
|
934
|
+
fav.setAttribute("data-glyph", kind);
|
|
935
|
+
fav.innerHTML = kind === "loading" ? ICONS.spin : ICONS.globe;
|
|
936
|
+
return fav;
|
|
937
|
+
}
|
|
938
|
+
function hostOf(u) {
|
|
939
|
+
try {
|
|
940
|
+
return new URL(u).host;
|
|
941
|
+
} catch {
|
|
942
|
+
return "";
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
function renderUrl(u) {
|
|
946
|
+
let host = u;
|
|
947
|
+
let rest = "";
|
|
948
|
+
try {
|
|
949
|
+
const p = new URL(u);
|
|
950
|
+
if (p.protocol === "http:" || p.protocol === "https:") {
|
|
951
|
+
host = p.host;
|
|
952
|
+
rest = `${p.pathname === "/" ? "" : p.pathname}${p.search}${p.hash}`;
|
|
953
|
+
}
|
|
954
|
+
} catch {
|
|
955
|
+
}
|
|
956
|
+
$("urltext-host").textContent = host;
|
|
957
|
+
$("urltext-path").textContent = rest;
|
|
958
|
+
}
|
|
959
|
+
function openMenu(anchor, items) {
|
|
960
|
+
const menu = $("menu");
|
|
961
|
+
menu.textContent = "";
|
|
962
|
+
for (const it of items) {
|
|
963
|
+
if (it.heading === true || it.run === void 0) {
|
|
964
|
+
const h = doc.createElement("p");
|
|
965
|
+
h.className = "heading";
|
|
966
|
+
h.setAttribute("role", "presentation");
|
|
967
|
+
h.textContent = it.label;
|
|
968
|
+
menu.appendChild(h);
|
|
969
|
+
continue;
|
|
970
|
+
}
|
|
971
|
+
const run = it.run;
|
|
972
|
+
const b = doc.createElement("button");
|
|
973
|
+
b.type = "button";
|
|
974
|
+
b.setAttribute("role", "menuitem");
|
|
975
|
+
if (it.fav !== void 0) b.appendChild(it.fav());
|
|
976
|
+
const name = doc.createElement("span");
|
|
977
|
+
name.className = "name";
|
|
978
|
+
name.textContent = it.label;
|
|
979
|
+
b.appendChild(name);
|
|
980
|
+
if (it.hint !== void 0) {
|
|
981
|
+
const hint = doc.createElement("span");
|
|
982
|
+
hint.className = "hint";
|
|
983
|
+
hint.textContent = it.hint;
|
|
984
|
+
b.appendChild(hint);
|
|
985
|
+
}
|
|
986
|
+
if (it.current) b.setAttribute("aria-current", "true");
|
|
987
|
+
b.addEventListener("click", () => {
|
|
988
|
+
closeMenu();
|
|
989
|
+
run();
|
|
990
|
+
});
|
|
991
|
+
menu.appendChild(b);
|
|
992
|
+
}
|
|
993
|
+
const r = anchor.getBoundingClientRect();
|
|
994
|
+
menu.style.top = `${Math.round(r.bottom + 4)}px`;
|
|
995
|
+
menu.style.maxWidth = `${Math.max(0, win.innerWidth - 24)}px`;
|
|
996
|
+
menu.style.right = "12px";
|
|
997
|
+
menu.hidden = false;
|
|
998
|
+
const width = menu.getBoundingClientRect().width;
|
|
999
|
+
const right = Math.max(12, Math.round(win.innerWidth - r.right));
|
|
1000
|
+
menu.style.right = `${Math.max(12, Math.min(right, Math.round(win.innerWidth - 12 - width)))}px`;
|
|
1001
|
+
anchor.setAttribute("aria-expanded", "true");
|
|
1002
|
+
menu.querySelector("button")?.focus();
|
|
1003
|
+
}
|
|
1004
|
+
function closeMenu() {
|
|
1005
|
+
$("menu").hidden = true;
|
|
1006
|
+
for (const id of ["tabsbtn", "more", "others", "empty-others"])
|
|
1007
|
+
$(id).removeAttribute("aria-expanded");
|
|
259
1008
|
}
|
|
260
1009
|
function applyState(s) {
|
|
261
1010
|
lastState = s;
|
|
262
|
-
pendingHandoff = s.
|
|
1011
|
+
pendingHandoff = s.handoffs.at(-1) ?? null;
|
|
263
1012
|
$("handoff").hidden = pendingHandoff === null;
|
|
264
1013
|
$("handoff-message").textContent = pendingHandoff?.message ?? "";
|
|
265
1014
|
tabs = s.tabs;
|
|
1015
|
+
otherTabs = s.otherTabs;
|
|
1016
|
+
if (tabs.length === 0) image = null;
|
|
1017
|
+
const was = control;
|
|
266
1018
|
control = s.control;
|
|
1019
|
+
if (full && was === "user" && control === "agent") askMode(false);
|
|
1020
|
+
if (was === "user" && control === "agent" && pendingNav?.sentOn === sock) pendingNav = null;
|
|
267
1021
|
const active = tabs.find((x) => x.active);
|
|
268
1022
|
if (doc.activeElement !== url) url.value = active?.url ?? "";
|
|
1023
|
+
renderUrl(active?.url ?? "");
|
|
269
1024
|
renderTabs();
|
|
270
1025
|
renderChrome();
|
|
271
1026
|
if (sock !== null) {
|
|
@@ -301,6 +1056,7 @@
|
|
|
301
1056
|
}
|
|
302
1057
|
if (p.sentOn !== sock && sock !== null) {
|
|
303
1058
|
p.sentOn = sock;
|
|
1059
|
+
if (control !== "user" && !compact()) takeover();
|
|
304
1060
|
send({ t: "nav", url: p.url });
|
|
305
1061
|
}
|
|
306
1062
|
}
|
|
@@ -332,14 +1088,23 @@
|
|
|
332
1088
|
}
|
|
333
1089
|
function layout() {
|
|
334
1090
|
const st = stage.getBoundingClientRect();
|
|
335
|
-
const box = letterbox(
|
|
1091
|
+
const box = letterbox(
|
|
1092
|
+
Math.max(0, Math.floor(st.width) - 2 * STAGE_PAD),
|
|
1093
|
+
Math.max(0, Math.floor(st.height) - 2 * STAGE_PAD)
|
|
1094
|
+
);
|
|
336
1095
|
view.style.width = `${box.w}px`;
|
|
337
1096
|
view.style.height = `${box.h}px`;
|
|
338
1097
|
const dpr = win.devicePixelRatio || 1;
|
|
339
|
-
const cap = control === "user" ? VIEWPORT.width : SIDE_MAX_WIDTH;
|
|
340
|
-
const w = Math.max(1, Math.min(cap, Math.round(box.w * dpr)));
|
|
1098
|
+
const cap = control === "user" || full ? VIEWPORT.width : SIDE_MAX_WIDTH;
|
|
1099
|
+
const w = Math.max(1, Math.min(cap, Math.round(box.w * Math.min(dpr, MAX_DPR))));
|
|
341
1100
|
return { w, h: Math.max(1, Math.round(w / ASPECT)) };
|
|
342
1101
|
}
|
|
1102
|
+
function onBoxChange() {
|
|
1103
|
+
if (sock === null || mode !== "live") return;
|
|
1104
|
+
const { w, h } = layout();
|
|
1105
|
+
if (sentSize !== `${w}x${h}`) sizeMessage("resize");
|
|
1106
|
+
draw();
|
|
1107
|
+
}
|
|
343
1108
|
function sizeMessage(kind) {
|
|
344
1109
|
const { w, h } = layout();
|
|
345
1110
|
sentSize = `${w}x${h}`;
|
|
@@ -356,19 +1121,25 @@
|
|
|
356
1121
|
ctx?.drawImage(image, 0, 0, w, h);
|
|
357
1122
|
}
|
|
358
1123
|
async function onFrame(m) {
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
1124
|
+
if (!treeEmpty()) {
|
|
1125
|
+
try {
|
|
1126
|
+
const first = image === null;
|
|
1127
|
+
image = await decode(m.jpeg);
|
|
1128
|
+
if (first) renderLoading();
|
|
1129
|
+
draw();
|
|
1130
|
+
input.frame();
|
|
1131
|
+
} catch {
|
|
1132
|
+
}
|
|
366
1133
|
}
|
|
367
1134
|
send({ t: "frame-ack", seq: m.seq });
|
|
368
1135
|
}
|
|
369
1136
|
function show(next) {
|
|
370
1137
|
mode = next;
|
|
371
|
-
if (next === "empty")
|
|
1138
|
+
if (next === "empty" || next === "offline" || next === "error") image = null;
|
|
1139
|
+
if (next === "empty") {
|
|
1140
|
+
wasLive = false;
|
|
1141
|
+
armEmptyPoll();
|
|
1142
|
+
}
|
|
372
1143
|
renderChrome();
|
|
373
1144
|
}
|
|
374
1145
|
function armEmptyPoll() {
|
|
@@ -379,7 +1150,7 @@
|
|
|
379
1150
|
if (mode !== "empty" || emptySince === void 0) return;
|
|
380
1151
|
if (Date.now() - emptySince >= EMPTY_POLL_FOR_MS) return;
|
|
381
1152
|
if (doc.visibilityState === "hidden") armEmptyPoll();
|
|
382
|
-
else void connect();
|
|
1153
|
+
else void connect(true);
|
|
383
1154
|
}, EMPTY_POLL_MS);
|
|
384
1155
|
}
|
|
385
1156
|
function stop() {
|
|
@@ -395,10 +1166,15 @@
|
|
|
395
1166
|
s.close();
|
|
396
1167
|
}
|
|
397
1168
|
}
|
|
398
|
-
async function connect() {
|
|
1169
|
+
async function connect(lookOnly = false) {
|
|
399
1170
|
stop();
|
|
400
1171
|
const mine = gen;
|
|
401
|
-
|
|
1172
|
+
quiet = lookOnly && mode === "empty";
|
|
1173
|
+
if (quiet) armEmptyPoll();
|
|
1174
|
+
else {
|
|
1175
|
+
image = null;
|
|
1176
|
+
show("connecting");
|
|
1177
|
+
}
|
|
402
1178
|
let ticketUrl;
|
|
403
1179
|
try {
|
|
404
1180
|
const res = await doFetch("stream-ticket", {
|
|
@@ -411,10 +1187,10 @@
|
|
|
411
1187
|
if (mine !== gen) return;
|
|
412
1188
|
const code = body.code ?? body.error?.code;
|
|
413
1189
|
if (code === "service_not_running") return show("empty");
|
|
414
|
-
if (!res.ok || typeof body.url !== "string") return show("offline");
|
|
1190
|
+
if (!res.ok || typeof body.url !== "string") return show(quiet ? "empty" : "offline");
|
|
415
1191
|
ticketUrl = body.url;
|
|
416
1192
|
} catch {
|
|
417
|
-
if (mine === gen) show("offline");
|
|
1193
|
+
if (mine === gen) show(quiet ? "empty" : "offline");
|
|
418
1194
|
return;
|
|
419
1195
|
}
|
|
420
1196
|
const ws = new SocketCtor(ticketUrl);
|
|
@@ -430,31 +1206,42 @@
|
|
|
430
1206
|
};
|
|
431
1207
|
}
|
|
432
1208
|
async function onMessage(data) {
|
|
433
|
-
let
|
|
434
|
-
if (typeof data === "string")
|
|
435
|
-
else if (data instanceof ArrayBuffer)
|
|
436
|
-
else if (ArrayBuffer.isView(data))
|
|
1209
|
+
let bytes;
|
|
1210
|
+
if (typeof data === "string") bytes = new TextEncoder().encode(data);
|
|
1211
|
+
else if (data instanceof ArrayBuffer) bytes = new Uint8Array(data);
|
|
1212
|
+
else if (ArrayBuffer.isView(data))
|
|
1213
|
+
bytes = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
437
1214
|
else if (typeof data.arrayBuffer === "function")
|
|
438
|
-
|
|
1215
|
+
bytes = new Uint8Array(await data.arrayBuffer());
|
|
439
1216
|
else return;
|
|
1217
|
+
retried401 = false;
|
|
1218
|
+
attempts = 0;
|
|
1219
|
+
restartRetries = 0;
|
|
1220
|
+
const frame = decodeFrame(bytes);
|
|
1221
|
+
if (frame !== null) {
|
|
1222
|
+
void onFrame(frame);
|
|
1223
|
+
return;
|
|
1224
|
+
}
|
|
440
1225
|
let msg;
|
|
441
1226
|
try {
|
|
442
|
-
msg = JSON.parse(
|
|
1227
|
+
msg = JSON.parse(new TextDecoder().decode(bytes));
|
|
443
1228
|
} catch {
|
|
444
1229
|
return;
|
|
445
1230
|
}
|
|
446
|
-
retried401 = false;
|
|
447
|
-
attempts = 0;
|
|
448
|
-
restartRetries = 0;
|
|
449
1231
|
switch (msg.t) {
|
|
1232
|
+
case "ping":
|
|
1233
|
+
send({ t: "pong", id: msg.id });
|
|
1234
|
+
break;
|
|
1235
|
+
case "cursor":
|
|
1236
|
+
input.cursor(msg.cursor);
|
|
1237
|
+
break;
|
|
450
1238
|
case "state":
|
|
451
1239
|
mode = "live";
|
|
1240
|
+
wasLive = true;
|
|
1241
|
+
quiet = false;
|
|
452
1242
|
emptySince = void 0;
|
|
453
1243
|
applyState(msg);
|
|
454
1244
|
break;
|
|
455
|
-
case "frame":
|
|
456
|
-
void onFrame(msg);
|
|
457
|
-
break;
|
|
458
1245
|
case "blocked":
|
|
459
1246
|
blockedHost = msg.host;
|
|
460
1247
|
blockedUrl = msg.url;
|
|
@@ -463,16 +1250,24 @@
|
|
|
463
1250
|
case "dialog":
|
|
464
1251
|
showDialog(msg);
|
|
465
1252
|
break;
|
|
1253
|
+
case "notice":
|
|
1254
|
+
if (msg.code === "tab_in_use") notice("tabInUse", "");
|
|
1255
|
+
else if (msg.code === "tab_cap") notice("tabCap", "");
|
|
1256
|
+
break;
|
|
466
1257
|
}
|
|
467
1258
|
}
|
|
468
1259
|
function onClose(code) {
|
|
469
1260
|
sock = null;
|
|
470
1261
|
lastState = null;
|
|
1262
|
+
if (quiet && code !== 4401 && code !== 4409 && code !== 1009) {
|
|
1263
|
+
show("empty");
|
|
1264
|
+
return;
|
|
1265
|
+
}
|
|
471
1266
|
if (code === 4401 && !retried401) {
|
|
472
1267
|
retried401 = true;
|
|
473
|
-
void connect();
|
|
1268
|
+
void connect(quiet);
|
|
474
1269
|
} else if (code === 4404) {
|
|
475
|
-
const wait = RESTART_RETRY_MS[restartRetries];
|
|
1270
|
+
const wait = wasLive ? RESTART_RETRY_MS[restartRetries] : void 0;
|
|
476
1271
|
if (wait === void 0) show("empty");
|
|
477
1272
|
else {
|
|
478
1273
|
restartRetries++;
|
|
@@ -494,13 +1289,14 @@
|
|
|
494
1289
|
}
|
|
495
1290
|
function takeover() {
|
|
496
1291
|
send({ t: "takeover" });
|
|
497
|
-
|
|
498
|
-
|
|
1292
|
+
askMode(true);
|
|
1293
|
+
input.focus();
|
|
499
1294
|
renderChrome();
|
|
500
1295
|
}
|
|
501
1296
|
function handback() {
|
|
1297
|
+
pendingNav = null;
|
|
502
1298
|
send({ t: "handback" });
|
|
503
|
-
|
|
1299
|
+
askMode(false);
|
|
504
1300
|
renderChrome();
|
|
505
1301
|
if (pendingHandoff !== null) void answerHandoff(true);
|
|
506
1302
|
}
|
|
@@ -530,6 +1326,7 @@
|
|
|
530
1326
|
blockedHost = "";
|
|
531
1327
|
blockedUrl = "";
|
|
532
1328
|
renderChrome();
|
|
1329
|
+
if (!interactive()) return;
|
|
533
1330
|
if (target === "") {
|
|
534
1331
|
send({ t: "nav", action: "reload" });
|
|
535
1332
|
return;
|
|
@@ -544,11 +1341,13 @@
|
|
|
544
1341
|
const res = await doFetch("handoff-answer", {
|
|
545
1342
|
method: "POST",
|
|
546
1343
|
headers: { authorization: `Bearer ${token}`, "content-type": "application/json" },
|
|
547
|
-
|
|
1344
|
+
// the handoff's own conversation (a sub-agent's when it asked): the kernel looks the
|
|
1345
|
+
// question up by it and the callId, which is unique only within its run
|
|
1346
|
+
body: JSON.stringify({ callId: h.callId, done, handoffConversationId: h.conversationId }),
|
|
548
1347
|
credentials: "omit"
|
|
549
1348
|
});
|
|
550
1349
|
if (res.status === 404) {
|
|
551
|
-
if (pendingHandoff?.callId === h.callId) {
|
|
1350
|
+
if (pendingHandoff?.callId === h.callId && pendingHandoff.conversationId === h.conversationId) {
|
|
552
1351
|
pendingHandoff = null;
|
|
553
1352
|
$("handoff").hidden = true;
|
|
554
1353
|
}
|
|
@@ -563,27 +1362,43 @@
|
|
|
563
1362
|
return;
|
|
564
1363
|
}
|
|
565
1364
|
}
|
|
566
|
-
function onPointer(kind, e) {
|
|
567
|
-
if (compact()) return;
|
|
568
|
-
if (kind === "move") {
|
|
569
|
-
if (!interactive() || e.timeStamp - lastMove < MOVE_INTERVAL_MS) return;
|
|
570
|
-
lastMove = e.timeStamp;
|
|
571
|
-
}
|
|
572
|
-
const r = view.getBoundingClientRect();
|
|
573
|
-
const p = mapPoint(e.clientX - r.left, e.clientY - r.top, {
|
|
574
|
-
x: 0,
|
|
575
|
-
y: 0,
|
|
576
|
-
w: r.width,
|
|
577
|
-
h: r.height
|
|
578
|
-
});
|
|
579
|
-
send({
|
|
580
|
-
t: "mouse",
|
|
581
|
-
kind,
|
|
582
|
-
...p,
|
|
583
|
-
...kind === "move" ? {} : { button: e.button === 2 ? "right" : "left" }
|
|
584
|
-
});
|
|
585
|
-
}
|
|
586
1365
|
$("takeover").addEventListener("click", takeover);
|
|
1366
|
+
$("verify-go").addEventListener("click", takeover);
|
|
1367
|
+
$("tabsbtn").addEventListener("click", (e) => {
|
|
1368
|
+
const items = [];
|
|
1369
|
+
const subs = subGroups();
|
|
1370
|
+
let group = "own";
|
|
1371
|
+
for (const tab of tabs) {
|
|
1372
|
+
if (tab.group !== group) {
|
|
1373
|
+
group = tab.group;
|
|
1374
|
+
if (group !== "own") items.push({ label: groupLabel(group, subs), heading: true });
|
|
1375
|
+
}
|
|
1376
|
+
items.push({
|
|
1377
|
+
label: tabName(tab),
|
|
1378
|
+
current: tab.active,
|
|
1379
|
+
fav: () => favOf(tab),
|
|
1380
|
+
run: () => pickTab(tab)
|
|
1381
|
+
});
|
|
1382
|
+
}
|
|
1383
|
+
if (interactive())
|
|
1384
|
+
items.push({ label: text("newTab"), run: () => send({ t: "tab", action: "open" }) });
|
|
1385
|
+
if (otherTabs.length > 0) items.push({ label: othersLabel(), heading: true }, ...otherItems());
|
|
1386
|
+
if (items.length > 0) openMenu(e.currentTarget, items);
|
|
1387
|
+
});
|
|
1388
|
+
$("others").addEventListener("click", (e) => openOthers(e.currentTarget));
|
|
1389
|
+
$("empty-others").addEventListener("click", (e) => openOthers(e.currentTarget));
|
|
1390
|
+
$("more").addEventListener("click", (e) => {
|
|
1391
|
+
openMenu(e.currentTarget, [
|
|
1392
|
+
{ label: text("forward"), run: () => send({ t: "nav", action: "forward" }) },
|
|
1393
|
+
{ label: text("reload"), run: () => send({ t: "nav", action: "reload" }) }
|
|
1394
|
+
]);
|
|
1395
|
+
});
|
|
1396
|
+
doc.addEventListener("mousedown", (e) => {
|
|
1397
|
+
const m = $("menu");
|
|
1398
|
+
if (m.hidden || m.contains(e.target)) return;
|
|
1399
|
+
if (e.target?.closest?.("#tabsbtn, #more, #others, #empty-others")) return;
|
|
1400
|
+
closeMenu();
|
|
1401
|
+
});
|
|
587
1402
|
$("handback").addEventListener("click", handback);
|
|
588
1403
|
$("handoff-done").addEventListener("click", () => void answerHandoff(true));
|
|
589
1404
|
$("handoff-cancel").addEventListener("click", () => void answerHandoff(false));
|
|
@@ -597,54 +1412,26 @@
|
|
|
597
1412
|
$("forward").addEventListener("click", () => send({ t: "nav", action: "forward" }));
|
|
598
1413
|
$("reload").addEventListener("click", () => send({ t: "nav", action: "reload" }));
|
|
599
1414
|
url.addEventListener("keydown", (e) => {
|
|
600
|
-
if (e.key !== "Enter" ||
|
|
1415
|
+
if (e.key !== "Enter" || !interactive() || url.value.trim() === "") return;
|
|
601
1416
|
send({ t: "nav", url: normalizeUrl(url.value) });
|
|
602
|
-
|
|
603
|
-
});
|
|
604
|
-
view.addEventListener("mousedown", (e) => onPointer("down", e));
|
|
605
|
-
view.addEventListener("mouseup", (e) => onPointer("up", e));
|
|
606
|
-
view.addEventListener("mousemove", (e) => onPointer("move", e));
|
|
607
|
-
view.addEventListener("contextmenu", (e) => e.preventDefault());
|
|
608
|
-
view.addEventListener(
|
|
609
|
-
"wheel",
|
|
610
|
-
(e) => {
|
|
611
|
-
if (compact()) return;
|
|
612
|
-
e.preventDefault();
|
|
613
|
-
const r = view.getBoundingClientRect();
|
|
614
|
-
const p = mapPoint(e.clientX - r.left, e.clientY - r.top, {
|
|
615
|
-
x: 0,
|
|
616
|
-
y: 0,
|
|
617
|
-
w: r.width,
|
|
618
|
-
h: r.height
|
|
619
|
-
});
|
|
620
|
-
send({ t: "mouse", kind: "wheel", ...p, dx: e.deltaX, dy: e.deltaY });
|
|
621
|
-
},
|
|
622
|
-
{ passive: false }
|
|
623
|
-
);
|
|
624
|
-
view.addEventListener("keydown", (e) => {
|
|
625
|
-
if (!interactive() || e.key === "Escape" || e.isComposing) return;
|
|
626
|
-
e.preventDefault();
|
|
627
|
-
send({ t: "key", kind: "down", key: e.key, code: e.code });
|
|
628
|
-
});
|
|
629
|
-
view.addEventListener("keyup", (e) => {
|
|
630
|
-
if (!interactive() || e.key === "Escape" || e.isComposing) return;
|
|
631
|
-
e.preventDefault();
|
|
632
|
-
send({ t: "key", kind: "up", key: e.key, code: e.code });
|
|
633
|
-
});
|
|
634
|
-
view.addEventListener("compositionend", (e) => {
|
|
635
|
-
if (interactive() && e.data !== "") send({ t: "key", kind: "char", key: e.data, text: e.data });
|
|
1417
|
+
input.focus();
|
|
636
1418
|
});
|
|
637
1419
|
doc.addEventListener("keydown", (e) => {
|
|
638
|
-
if (e.key
|
|
1420
|
+
if (e.key !== "Escape") return;
|
|
1421
|
+
if (!$("menu").hidden) {
|
|
1422
|
+
closeMenu();
|
|
1423
|
+
return;
|
|
1424
|
+
}
|
|
1425
|
+
if (interactive()) handback();
|
|
639
1426
|
});
|
|
640
1427
|
win.addEventListener("resize", () => {
|
|
1428
|
+
onBoxChange();
|
|
641
1429
|
if (sock === null || mode !== "live") return;
|
|
642
|
-
const { w, h } = layout();
|
|
643
|
-
if (sentSize !== `${w}x${h}`) sizeMessage("resize");
|
|
644
1430
|
renderChrome();
|
|
645
1431
|
renderTabs();
|
|
646
|
-
draw();
|
|
647
1432
|
});
|
|
1433
|
+
const Ro = win.ResizeObserver;
|
|
1434
|
+
if (Ro !== void 0) new Ro(onBoxChange).observe(stage);
|
|
648
1435
|
doc.addEventListener("visibilitychange", () => {
|
|
649
1436
|
if (doc.visibilityState === "hidden") {
|
|
650
1437
|
if (hiddenTimer === void 0) {
|
|
@@ -661,7 +1448,7 @@
|
|
|
661
1448
|
hiddenTimer = void 0;
|
|
662
1449
|
if (pausedForHidden) {
|
|
663
1450
|
pausedForHidden = false;
|
|
664
|
-
void connect();
|
|
1451
|
+
void connect(true);
|
|
665
1452
|
}
|
|
666
1453
|
}
|
|
667
1454
|
});
|
|
@@ -687,6 +1474,7 @@
|
|
|
687
1474
|
applyTheme(m.tokens, m.theme);
|
|
688
1475
|
setLabels();
|
|
689
1476
|
renderChrome();
|
|
1477
|
+
doc.body.setAttribute("data-ready", "");
|
|
690
1478
|
if (!started) {
|
|
691
1479
|
started = true;
|
|
692
1480
|
void connect();
|
|
@@ -695,6 +1483,8 @@
|
|
|
695
1483
|
token = m.token;
|
|
696
1484
|
} else if (m.type === "theme") {
|
|
697
1485
|
applyTheme(m.tokens, m.theme);
|
|
1486
|
+
} else if (m.type === "panel-mode" && typeof m.full === "boolean") {
|
|
1487
|
+
hostMode(m.full);
|
|
698
1488
|
}
|
|
699
1489
|
});
|
|
700
1490
|
}
|