Repository navigation
Expand file tree
/
Copy pathclient.js
More file actions
614 lines (582 loc) · 25.3 KB
/
Copy pathclient.js
File metadata and controls
614 lines (582 loc) · 25.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/**
* dsh-zcode-browser-use — sidebar client.
*
* The agent's browser is Playwright-driven and headless. This half exists for
* the OTHER browser: the plugin's own browser, mounted in DSH's right sidebar
* as a real Chromium guest.
*
* It does not use DSH's built-in Browser tab type. That tab owns a browser with
* a different cookie jar, so pointing it at a page behind a login only ever
* shows the login screen. Instead this half registers its own tab type and
* renders its own webview element, with its own persistent partition: a
* separate process, its own cookies, no headless fingerprint, and
* sendInputEvent for trusted input — so the agent can drive it and the user can
* take over.
*
* The host half queues commands on /browser-use/own/command; this half runs
* them against the guest and posts the outcome to /browser-use/own/result.
*
* The shell fetches this bundle from /plugins/dsh-zcode-browser-use/client.js
* and runs it through window.__ModuleLoader__; it uses no other plugin.
*/
window.__ModuleLoader__.load({
id: "dsh-zcode-browser-use",
factory: (require) => {
var module = { exports: {} };
var exports = module.exports;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
/**
* Services this client half needs.
*
* Cordis refuses to read a service property until the plugin declares it:
* without this list the probe reported
* `cannot get property "sidebarRightTabs" without inject`, ctx.get() returned
* undefined, and the tab type was never registered.
*/
const inject = ["sidebarRight", "sidebarRightTabs", "slots"];
/** Tab type this half owns; unique across every registration. */
const OWN_ID = "dsh-zcode-browser-use/own";
/** Its kind, used with ctx.sidebarRight.openTab. */
const OWN_KIND = "zcode-own-browser";
/**
* Workspace storage identity handed to the desktop bridge.
*
* The main process maps it to one session partition for the lifetime of the
* application: the same string keeps the same cookies within a run. It does
* NOT survive a restart — partitions are random per run, which is also true
* of DSH's own sidebar browser.
*/
const WORKSPACE_KEY = "zcode-browser-use";
/** Fallback partition label, used only when no guest reservation exists yet. */
const PARTITION = "persist:zcode-browser-use";
const COMMAND_URL = "/browser-use/own/command";
const RESULT_URL = "/browser-use/own/result";
const STATUS_URL = "/browser-use/own/status";
/** Delay before re-polling after a transport error. */
const RETRY_MS = 1500;
/** Delay between attempts to reach the sidebar services, and how many. */
const REGISTER_RETRY_MS = 300;
const REGISTER_ATTEMPTS = 120;
/** Selector for the elements a snapshot exposes as addressable references. */
const SNAPSHOT_SELECTOR =
'a[href],button,input,select,textarea,summary,[role],[contenteditable="true"],[onclick]';
/** Upper bound on references returned by one snapshot. */
const MAX_REFS = 300;
/** The mounted guest, or null while no tab hosts it. */
let guest = null;
/** What activation managed to do; published with the status so a failure is inspectable. */
const registration = { tabs: false, slots: false, error: null, probe: null, element: null, build: "probe-6" };
/** Summarize one value for the diagnostics payload. */
function describe(value) {
if (value === undefined) return "undefined";
if (value === null) return "null";
return typeof value;
}
/**
* Record what this client context actually offers.
*
* The sidebar services are provided under these names, but they are not
* reachable as plain properties here, and reading them the wrong way yields
* undefined, which silently skipped registration. Keep the evidence.
*/
function probeServices(ctx) {
const names = ["sidebarRightTabs", "sidebarRight", "slots", "layout"];
const byName = {};
const byProperty = {};
for (const name of names) {
try {
byName[name] = describe(typeof ctx.get === "function" ? ctx.get(name) : "no-ctx-get");
} catch (error) {
byName[name] = "throw:" + String((error && error.message) || error);
}
try {
byProperty[name] = describe(ctx[name]);
} catch (error) {
byProperty[name] = "throw:" + String((error && error.message) || error);
}
}
let keys = [];
try {
keys = Object.keys(ctx).slice(0, 48);
} catch (error) {
keys = ["throw:" + String((error && error.message) || error)];
}
return { byName: byName, byProperty: byProperty, keys: keys };
}
/** Resolve one locator to the page script that finds the element. */
function finderSource(locator) {
var ref = JSON.stringify(locator.ref || "");
var selector = JSON.stringify(locator.selector || "");
var role = JSON.stringify(locator.role || "");
var name = JSON.stringify(locator.name || "");
return (
"(function () {" +
" var ref = " + ref + ", selector = " + selector + ", role = " + role + ", name = " + name + ";" +
" if (ref) return document.querySelector('[data-dsh-ref=\"' + ref + '\"]');" +
" if (selector) return document.querySelector(selector);" +
" var wanted = name.toLowerCase(), wantedRole = role.toLowerCase();" +
" var nodes = document.querySelectorAll('a,button,input,select,textarea,summary,[role]');" +
" for (var i = 0; i < nodes.length; i += 1) {" +
" var el = nodes[i];" +
" var label = (el.getAttribute('aria-label') || el.placeholder || el.innerText || el.value || el.title || '').toString().trim();" +
" var elRole = (el.getAttribute('role') || el.tagName.toLowerCase());" +
" if (wanted && label.toLowerCase().indexOf(wanted) < 0) continue;" +
" if (wantedRole && elRole.toLowerCase() !== wantedRole) continue;" +
" return el;" +
" }" +
" return null;" +
"})()"
);
}
/** Turn a Playwright-style key name into an Electron sendInputEvent key. */
function keyEvent(key) {
var parts = String(key).split("+");
var main = parts.pop();
var modifiers = [];
for (var i = 0; i < parts.length; i += 1) {
var lower = parts[i].toLowerCase();
modifiers.push(lower === "cmd" || lower === "command" ? "meta" : lower === "ctrl" ? "control" : lower);
}
var aliases = { Esc: "Escape", Return: "Enter", ArrowUp: "Up", ArrowDown: "Down", ArrowLeft: "Left", ArrowRight: "Right" };
var keyCode = aliases[main] || (main.length === 1 ? main : main.charAt(0).toUpperCase() + main.slice(1));
return { keyCode: keyCode, modifiers: modifiers };
}
/** Run one script inside the guest and yield its JSON value. */
function run(code) {
return guest.element.executeJavaScript(code, true);
}
/** Wait until the guest stops loading, or the budget runs out. */
function settle() {
return new Promise((resolve) => {
var timer = setTimeout(done, 4000);
function done() {
clearTimeout(timer);
guest.element.removeEventListener("did-stop-loading", done);
resolve();
}
guest.element.addEventListener("did-stop-loading", done);
setTimeout(done, 60);
});
}
/**
* Wait until the guest is attached and has emitted its first dom-ready.
*
* Electron refuses loadURL/sendInputEvent/executeJavaScript before that:
* "The WebView must be attached to the DOM and the dom-ready event emitted".
*/
function whenReady() {
return guest !== null && guest.ready !== undefined ? guest.ready : Promise.resolve();
}
/** Read the guest's identity, tolerating a guest that is not ready yet. */
function identity() {
try {
identity.error = null;
return { url: guest.element.getURL(), title: guest.element.getTitle() };
} catch (error) {
// Keep the raw message: swallowing it is how a real failure stayed
// invisible for two rounds.
identity.error = String((error && error.message) || error);
if (registration.element !== null) registration.element.error = identity.error;
return { url: null, title: null };
}
}
/** Point the guest at a URL and wait for it. */
async function load(url) {
await whenReady();
// loadURL is allowed once dom-ready fired, and unlike assigning src it
// leaves the bootstrap lease marker in the URL untouched.
guest.element.loadURL(url);
await settle();
await new Promise((resolve) => setTimeout(resolve, 250));
}
/** Execute one command against the guest. */
async function dispatch(command) {
await whenReady();
if (command.kind === "open") {
if (typeof command.url === "string" && command.url) await load(command.url);
return Object.assign({ open: true, partition: (guest && guest.partition) || PARTITION }, identity());
}
if (command.kind === "navigate") {
await load(command.url);
return Object.assign({ partition: (guest && guest.partition) || PARTITION }, identity());
}
if (command.kind === "snapshot") {
return Object.assign({ partition: (guest && guest.partition) || PARTITION }, await run(
"(function () {" +
" var selector = " + JSON.stringify(SNAPSHOT_SELECTOR) + ", maxRefs = " + MAX_REFS + ";" +
" var elements = [], index = 0;" +
" var nodes = document.querySelectorAll(selector);" +
" for (var i = 0; i < nodes.length; i += 1) {" +
" var el = nodes[i], rect = el.getBoundingClientRect(), style = window.getComputedStyle(el);" +
" if (style.visibility === 'hidden' || style.display === 'none' || style.opacity === '0') continue;" +
" if (rect.width < 1 && rect.height < 1) continue;" +
" index += 1;" +
" var ref = 'r' + index;" +
" el.setAttribute('data-dsh-ref', ref);" +
" var label = (el.getAttribute('aria-label') || el.placeholder || el.innerText || el.value || el.getAttribute('title') || '').toString().trim().replace(/\s+/g, ' ');" +
" var tag = el.tagName.toLowerCase();" +
" var entry = { ref: ref, tag: tag, role: el.getAttribute('role') || tag, name: label.slice(0, 160), disabled: el.disabled === true };" +
" if (el.getAttribute('type')) entry.type = el.getAttribute('type');" +
" if (tag === 'input' || tag === 'select' || tag === 'textarea') entry.value = typeof el.value === 'string' ? el.value.slice(0, 80) : '';" +
" elements.push(entry);" +
" if (elements.length >= maxRefs) break;" +
" }" +
" return { url: location.href, title: document.title, text: (document.body ? document.body.innerText : '').slice(0, 4000), elements: elements };" +
"})()",
));
}
if (command.kind === "click") {
const box = await run(
"(function () { var el = " + finderSource(command) + ";" +
" if (!el) return null;" +
" el.scrollIntoView({ block: 'center', inline: 'center' });" +
" var r = el.getBoundingClientRect();" +
" return { x: Math.round(r.left + r.width / 2), y: Math.round(r.top + r.height / 2) }; })()",
);
if (box === null || box === undefined) throw new Error("找不到要点击的元素");
guest.element.focus();
guest.element.sendInputEvent({ type: "mouseDown", x: box.x, y: box.y, button: "left", clickCount: 1 });
guest.element.sendInputEvent({ type: "mouseUp", x: box.x, y: box.y, button: "left", clickCount: 1 });
await settle();
return Object.assign({ clicked: true, at: box, partition: (guest && guest.partition) || PARTITION }, identity());
}
if (command.kind === "type") {
const focused = await run(
"(function () { var el = " + finderSource(command) + ";" +
" if (!el) return false; el.focus(); if (el.select) el.select(); return true; })()",
);
if (focused !== true) throw new Error("找不到要输入的输入框");
guest.element.focus();
for (const char of String(command.text)) guest.element.sendInputEvent({ type: "char", keyCode: char });
if (command.submit === true) {
guest.element.sendInputEvent({ type: "keyDown", keyCode: "Enter" });
guest.element.sendInputEvent({ type: "keyUp", keyCode: "Enter" });
}
await settle();
return Object.assign({ typed: true, partition: (guest && guest.partition) || PARTITION }, identity());
}
if (command.kind === "press") {
const key = keyEvent(command.key);
guest.element.focus();
guest.element.sendInputEvent({ type: "keyDown", keyCode: key.keyCode, modifiers: key.modifiers });
guest.element.sendInputEvent({ type: "keyUp", keyCode: key.keyCode, modifiers: key.modifiers });
await settle();
return Object.assign({ pressed: true, partition: (guest && guest.partition) || PARTITION }, identity());
}
if (command.kind === "screenshot") {
const image = await guest.element.capturePage();
const dataUrl = image.toDataURL();
return Object.assign({ base64: String(dataUrl).split(",")[1] || "" }, identity());
}
throw new Error("unknown own-browser command: " + String(command.kind));
}
/** Report the guest's state to the host half. */
function report() {
if (guest === null) {
return fetch(STATUS_URL, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ open: false, registration: registration }),
cache: "no-store",
}).catch(() => undefined);
}
const id = identity();
return fetch(STATUS_URL, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ open: true, partition: (guest && guest.partition) || PARTITION, url: id.url, title: id.title, registration: registration, identityError: identity.error || null }),
cache: "no-store",
}).catch(() => undefined);
}
/** The tab body: one guest, owned by this plugin. */
function OwnBrowserBody(props) {
const react = require("react");
const host = react.useRef(null);
react.useEffect(() => {
let disposed = false;
let mounted = null;
const mount = async () => {
const bridge = window.dshDesktop && window.dshDesktop.browser;
if (bridge === undefined || typeof bridge.acquire !== "function") {
registration.element = { error: "dshDesktop.browser bridge unavailable" };
report();
return;
}
let reservation;
try {
// The main process issues the lease and hardens the session. A guest
// attaches only when its src carries that lease id — a plugin-made
// webview without one is refused and never gets a guest at all.
reservation = await bridge.acquire(WORKSPACE_KEY);
} catch (error) {
registration.element = { error: String((error && error.message) || error) };
report();
return;
}
if (disposed) {
await bridge.release(reservation.lease).catch(() => undefined);
return;
}
const element = document.createElement("webview");
// Exactly the attributes the main process checks at will-attach-webview.
element.dataset.sidebarBrowserFrame = "webview";
element.setAttribute("name", reservation.lease);
element.setAttribute("partition", reservation.partition);
element.setAttribute("allowpopups", "");
element.setAttribute("src", "about:blank#" + reservation.lease);
element.style.width = "100%";
element.style.height = "100%";
element.style.display = "flex";
host.current.appendChild(element);
registration.element = {
tag: element.tagName,
isConnected: element.isConnected === true,
hasLoadURL: typeof element.loadURL,
hasGetURL: typeof element.getURL,
hasSendInputEvent: typeof element.sendInputEvent,
hasCapturePage: typeof element.capturePage,
partition: reservation.partition,
lease: reservation.lease,
domReady: false,
error: null,
};
mounted = {
element: element,
ready: null,
lease: reservation.lease,
bridge: bridge,
partition: reservation.partition,
};
mounted.ready = new Promise((resolve) => {
const done = () => {
element.removeEventListener("dom-ready", done);
if (registration.element !== null) registration.element.domReady = true;
resolve();
};
element.addEventListener("dom-ready", done, { once: true });
// Never leave the command queue waiting on an event that already fired.
setTimeout(done, 5000);
});
guest = mounted;
element.addEventListener("did-navigate", report);
element.addEventListener("did-navigate-in-page", report);
report();
};
mount().catch((error) => {
registration.element = { error: String((error && error.message) || error) };
report();
});
return () => {
disposed = true;
const current = mounted;
if (guest === current) guest = null;
if (current !== null) {
current.element.remove();
if (typeof current.bridge.release === "function") {
current.bridge.release(current.lease).catch(() => undefined);
}
}
report();
};
}, []);
return react.createElement("div", {
ref: host,
style: { width: "100%", height: "100%", display: "flex", background: "#fff" },
});
}
/**
* Resolve one service by name.
*
* Plain property access yields undefined on the client context, and the
* sidebar services may not be provided yet when this plugin activates, so
* every lookup goes by name and callers retry.
*/
function service(ctx, name) {
if (typeof ctx.get === "function") {
try {
const found = ctx.get(name);
if (found !== undefined) return found;
} catch (error) {
// fall through to the property form
}
}
return ctx[name];
}
/** Register the tab type once the registry exists. Never throws. */
function ensureTabType(ctx) {
if (registration.tabs === true) return true;
const tabs = service(ctx, "sidebarRightTabs");
if (tabs === undefined) return false;
try {
ctx.effect(
() => tabs.register({
id: OWN_ID,
kind: OWN_KIND,
title: () => "自有浏览器",
keepMounted: true,
}),
"dsh-zcode-browser-use: own browser tab type",
);
registration.tabs = true;
return true;
} catch (error) {
registration.error = String((error && error.message) || error);
console.error("[browser-use] tab type not registered:", error);
return false;
}
}
/** Contribute the tab body once the slot registry exists. Never throws. */
function ensureTabBody(ctx) {
if (registration.slots === true) return true;
const slots = service(ctx, "slots");
if (slots === undefined) return false;
// The body has to be contributed from inside the slot's own scope. A bare
// slots.register for a slot this scope does not own throws, and a throwing
// client entry fails the whole web boot. Prefer inject, fall back to a
// direct register.
const contribute = () => slots.register(
{ name: "sidebar.right.pane.tab", key: OWN_ID },
OwnBrowserBody,
);
try {
ctx.effect(
() => (typeof slots.inject === "function"
? slots.inject("sidebar.right.pane.tab", contribute)
: contribute()),
"dsh-zcode-browser-use: own browser body",
);
registration.slots = true;
return true;
} catch (error) {
registration.error = String((error && error.message) || error);
console.error("[browser-use] tab body not contributed:", error);
return false;
}
}
/**
* Register the tab type and its body, retrying while the sidebar services
* are still being provided.
*
* Activation order is not guaranteed — the sidebar package provides these
* services itself, and this plugin may well activate first. One attempt and
* a silent skip is what left the tab type missing.
*
* @param ctx - the client plugin context.
* @param attempt - tries left before giving up and reporting.
*/
function registerTab(ctx, attempt) {
if (registration.tabs !== true || registration.slots !== true) registration.probe = probeServices(ctx);
const typed = ensureTabType(ctx);
const bodied = ensureTabBody(ctx);
if (typed && bodied) {
registration.error = null;
report();
return;
}
if (attempt <= 1) {
report();
return;
}
setTimeout(() => registerTab(ctx, attempt - 1), REGISTER_RETRY_MS);
}
/** Open (or focus) the plugin's own browser tab. */
function openOwnTab(ctx, url) {
const sidebar = ctx.get("sidebarRight");
if (sidebar === undefined) throw new Error("右侧栏不可用,无法打开自有浏览器");
sidebar.openTab(OWN_KIND, {
params: url ? { url: url } : {},
revealIfOpened: true,
});
}
/** Poll for commands until the plugin unloads. */
function startCommandLoop(ctx) {
let stopped = false;
const sendResult = async (id, payload) => {
await fetch(RESULT_URL, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(Object.assign({ id: id }, payload)),
cache: "no-store",
}).catch(() => undefined);
};
const loop = async () => {
if (stopped) return;
let command = null;
try {
const res = await fetch(COMMAND_URL, { method: "GET", cache: "no-store" });
if (res.ok) {
const body = await res.json();
command = body ? body.command : null;
}
} catch (error) {
command = null;
}
if (command) {
if (command.kind === "open") {
try {
openOwnTab(ctx, typeof command.url === "string" ? command.url : undefined);
await new Promise((resolve) => setTimeout(resolve, 600));
if (guest === null) {
await sendResult(command.id, { ok: false, error: "自有浏览器标签页没有挂载" });
} else {
const value = await dispatch(command);
await sendResult(command.id, Object.assign({ ok: true }, value));
}
} catch (error) {
await sendResult(command.id, { ok: false, error: String((error && error.message) || error) });
}
} else if (guest === null) {
await sendResult(command.id, { ok: false, error: "自有浏览器还没打开,先调用 own_browser_open" });
} else {
try {
const value = await dispatch(command);
await sendResult(command.id, Object.assign({ ok: true }, value));
} catch (error) {
await sendResult(command.id, { ok: false, error: String((error && error.message) || error) });
}
}
await report();
} else {
await new Promise((resolve) => setTimeout(resolve, RETRY_MS));
}
if (!stopped) loop().catch(() => undefined);
};
ctx.effect(
() => {
loop().catch((error) => {
console.error("[browser-use] own browser command loop stopped:", error);
});
return () => {
stopped = true;
};
},
"dsh-zcode-browser-use: own browser commands",
);
}
/**
* Register the plugin's own sidebar browser and start serving commands.
* @param ctx - the client plugin context providing sidebarRight and slots.
*/
function apply(ctx) {
// A client entry that throws fails the whole web boot. Nothing in here may
// escape: worst case the sidebar tab is missing, never the application.
try {
registerTab(ctx, REGISTER_ATTEMPTS);
} catch (error) {
console.error("[browser-use] own browser tab type not registered:", error);
report();
}
try {
startCommandLoop(ctx);
} catch (error) {
console.error("[browser-use] own browser command loop not started:", error);
}
// Report at activation: otherwise a registration failure is only visible
// after a guest mounts, which is exactly the case that never happens.
report();
}
exports.apply = apply;
exports.inject = inject;
return module.exports;
},
});