@myrialabs/clopen 0.4.21 → 0.4.22
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/README.md +2 -1
- package/backend/artifacts/generate.ts +11 -5
- package/backend/auth/permissions.ts +8 -8
- package/backend/engine/README.md +11 -8
- package/backend/engine/adapters/codex/error-handler.ts +1 -1
- package/backend/engine/adapters/qwen/error-handler.ts +1 -1
- package/backend/engine/docs/adapter-contract.md +66 -2
- package/backend/engine/docs/adding-an-engine.md +46 -7
- package/backend/engine/docs/frontend-and-chat.md +80 -17
- package/backend/engine/docs/lessons-learned.md +126 -7
- package/backend/engine/docs/quick-reference.md +12 -3
- package/backend/engine/docs/{system-tools.md → stack.md} +35 -6
- package/backend/engine/docs/websocket-routes.md +54 -15
- package/backend/engine/install-runner.ts +4 -4
- package/backend/engine/resolve-model.ts +87 -0
- package/backend/engine/structured-helpers.test.ts +64 -0
- package/backend/engine/structured-helpers.ts +105 -13
- package/backend/mcp/README.md +32 -11
- package/backend/mcp/internal/config.ts +3 -20
- package/backend/mcp/internal/servers/browser-automation/actions/console.ts +65 -0
- package/backend/mcp/internal/servers/browser-automation/actions/dom-analyze.ts +202 -0
- package/backend/mcp/internal/servers/browser-automation/actions/index.ts +31 -0
- package/backend/mcp/internal/servers/browser-automation/actions/input.ts +332 -0
- package/backend/mcp/internal/servers/browser-automation/actions/inspect.ts +158 -0
- package/backend/mcp/internal/servers/browser-automation/actions/navigation.ts +157 -0
- package/backend/mcp/internal/servers/browser-automation/actions/shared.ts +60 -0
- package/backend/mcp/internal/servers/browser-automation/actions/tabs.ts +146 -0
- package/backend/mcp/internal/servers/browser-automation/actions/types.ts +63 -0
- package/backend/mcp/internal/servers/browser-automation/context.ts +82 -0
- package/backend/mcp/internal/servers/browser-automation/description.ts +59 -0
- package/backend/mcp/internal/servers/browser-automation/format.ts +56 -0
- package/backend/mcp/internal/servers/browser-automation/index.ts +36 -784
- package/backend/mcp/internal/servers/browser-automation/paths.ts +71 -0
- package/backend/mcp/internal/servers/browser-automation/runner.ts +260 -0
- package/backend/mcp/internal/servers/browser-automation/schema.ts +26 -0
- package/backend/preview/browser/browser-console-manager.ts +310 -156
- package/backend/preview/browser/browser-dialog-handler.ts +45 -16
- package/backend/preview/browser/browser-host-bridge.ts +471 -0
- package/backend/preview/browser/browser-interaction-handler.ts +868 -297
- package/backend/preview/browser/browser-mcp-control.ts +7 -2
- package/backend/preview/browser/browser-native-ui-handler.ts +455 -81
- package/backend/preview/browser/browser-navigation-tracker.ts +56 -0
- package/backend/preview/browser/browser-pool.ts +83 -9
- package/backend/preview/browser/browser-preview-service.ts +432 -53
- package/backend/preview/browser/browser-tab-manager.ts +16 -16
- package/backend/preview/browser/browser-video-capture.ts +1334 -386
- package/backend/preview/browser/capture-profile.ts +213 -0
- package/backend/preview/browser/element-locator.ts +357 -0
- package/backend/preview/browser/frame-targeting.ts +92 -0
- package/backend/preview/browser/scripts/audio-stream.ts +258 -78
- package/backend/preview/browser/scripts/host-bridge.ts +1277 -0
- package/backend/preview/browser/scripts/video-stream.ts +803 -177
- package/backend/preview/browser/types.ts +397 -37
- package/backend/utils/cli.ts +1 -1
- package/backend/utils/path-enrich.ts +2 -2
- package/backend/ws/README.md +1 -1
- package/backend/ws/artifacts/generate.ts +2 -1
- package/backend/ws/engine/codex/accounts.ts +1 -1
- package/backend/ws/engine/codex/status.ts +1 -1
- package/backend/ws/git/branch-name.ts +11 -5
- package/backend/ws/git/commit-message.ts +11 -5
- package/backend/ws/index.ts +3 -3
- package/backend/ws/preview/browser/host.ts +95 -0
- package/backend/ws/preview/browser/interact.ts +204 -10
- package/backend/ws/preview/browser/native-ui.ts +31 -1
- package/backend/ws/preview/browser/tab-info.ts +10 -1
- package/backend/ws/preview/browser/tab.ts +43 -0
- package/backend/ws/preview/browser/webcodecs.ts +180 -53
- package/backend/ws/preview/index.ts +58 -1
- package/backend/ws/{system-tools → stack}/access.ts +1 -1
- package/backend/ws/stack/index.ts +16 -0
- package/backend/ws/{system-tools → stack}/install.ts +7 -7
- package/backend/ws/{system-tools → stack}/status.ts +7 -7
- package/dist/assets/{index-CEGW8vEt.js → index-DH_GO0Lf.js} +1 -1
- package/dist/assets/{index-B8YRz8wG.js → index-igSQ3JaM.js} +1 -1
- package/dist/assets/main-BD3-qPfT.css +1 -0
- package/dist/assets/{main-D-2ByODy.js → main-D-d8SFfW.js} +384 -358
- package/dist/assets/preview-video.worker-DcpDhCWL.js +1 -0
- package/dist/index.html +2 -2
- package/package.json +1 -1
- package/shared/types/stores/settings.ts +6 -0
- package/backend/mcp/internal/servers/browser-automation/actions.ts +0 -159
- package/backend/mcp/internal/servers/browser-automation/browser.ts +0 -375
- package/backend/mcp/internal/servers/browser-automation/inspection.ts +0 -495
- package/backend/ws/system-tools/index.ts +0 -16
- package/dist/assets/main-D8I0MfRQ.css +0 -1
|
@@ -8,6 +8,7 @@ import { t } from 'elysia';
|
|
|
8
8
|
import { createRouter } from '$shared/utils/ws-server';
|
|
9
9
|
import { execGit } from '../../git/git-executor';
|
|
10
10
|
import { initializeEngine } from '../../engine';
|
|
11
|
+
import { resolveGenerationTarget } from '../../engine/resolve-model';
|
|
11
12
|
import type { EngineType } from '$shared/types/unified';
|
|
12
13
|
import type { GeneratedCommitMessage } from '$shared/types/git';
|
|
13
14
|
import { debug } from '$shared/utils/logger';
|
|
@@ -62,7 +63,8 @@ export const commitMessageHandler = createRouter()
|
|
|
62
63
|
data: t.Object({
|
|
63
64
|
projectId: t.String(),
|
|
64
65
|
engine: t.String(),
|
|
65
|
-
|
|
66
|
+
/** Hint only — the provider is re-derived from the engine catalog. */
|
|
67
|
+
providerSlug: t.Optional(t.String()),
|
|
66
68
|
modelId: t.String(),
|
|
67
69
|
format: t.Union([t.Literal('single-line'), t.Literal('multi-line')]),
|
|
68
70
|
customPrompt: t.Optional(t.String()),
|
|
@@ -110,14 +112,18 @@ ${rawDiff}`;
|
|
|
110
112
|
? `${defaultPrompt}\n\nAdditional constraints:\n${extra}`
|
|
111
113
|
: defaultPrompt;
|
|
112
114
|
|
|
113
|
-
|
|
115
|
+
// The caller's providerSlug/account can be stale (see resolve-model.ts).
|
|
116
|
+
const target = await resolveGenerationTarget(engine, data.modelId, data.providerSlug);
|
|
117
|
+
|
|
118
|
+
debug.log('git', `Generating commit message via ${engineType}/${target.providerSlug}/${target.modelId}`);
|
|
114
119
|
|
|
115
120
|
const result = await engine.generateStructured<GeneratedCommitMessage>({
|
|
116
121
|
prompt,
|
|
117
|
-
providerSlug:
|
|
118
|
-
modelId:
|
|
122
|
+
providerSlug: target.providerSlug,
|
|
123
|
+
modelId: target.modelId,
|
|
119
124
|
schema: COMMIT_MESSAGE_SCHEMA,
|
|
120
|
-
projectPath: cwd
|
|
125
|
+
projectPath: cwd,
|
|
126
|
+
...(target.accountId != null && { accountId: target.accountId })
|
|
121
127
|
});
|
|
122
128
|
|
|
123
129
|
// Format structured output into conventional commit string
|
package/backend/ws/index.ts
CHANGED
|
@@ -29,7 +29,7 @@ import { systemRouter } from './system';
|
|
|
29
29
|
import { tunnelRouter } from './tunnel';
|
|
30
30
|
import { gitRouter } from './git';
|
|
31
31
|
import { engineRouter } from './engine';
|
|
32
|
-
import {
|
|
32
|
+
import { stackRouter } from './stack';
|
|
33
33
|
import { dbClientRouter } from './db-client';
|
|
34
34
|
import { mcpRouter } from './mcp';
|
|
35
35
|
import { skillsRouter } from './skills';
|
|
@@ -77,8 +77,8 @@ export const wsRouter = createRouter()
|
|
|
77
77
|
// AI Engine Management
|
|
78
78
|
.merge(engineRouter)
|
|
79
79
|
|
|
80
|
-
//
|
|
81
|
-
.merge(
|
|
80
|
+
// Stack (install Git, Claude Code, OpenCode, Chrome binaries)
|
|
81
|
+
.merge(stackRouter)
|
|
82
82
|
|
|
83
83
|
// DB Client (global database management)
|
|
84
84
|
.merge(dbClientRouter)
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser Host Bridge WebSocket Handler
|
|
3
|
+
*
|
|
4
|
+
* Carries the viewer's answer back to a capability the headless browser asked
|
|
5
|
+
* for — a location fix, a camera stream, clipboard contents, a file selection.
|
|
6
|
+
* The request itself is pushed by BrowserHostBridge; this is the return leg.
|
|
7
|
+
*
|
|
8
|
+
* **PROJECT ISOLATION**: the request id is only meaningful inside the caller's
|
|
9
|
+
* own project service, so a response from one project can never settle
|
|
10
|
+
* another's pending request.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { t } from 'elysia';
|
|
14
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
15
|
+
import { debug } from '$shared/utils/logger';
|
|
16
|
+
import { requireBrowserPreviewAccess } from '../access';
|
|
17
|
+
|
|
18
|
+
export const hostPreviewHandler = createRouter()
|
|
19
|
+
.on('preview:browser-host-response', {
|
|
20
|
+
data: t.Object({
|
|
21
|
+
requestId: t.String({ minLength: 1 }),
|
|
22
|
+
ok: t.Boolean(),
|
|
23
|
+
result: t.Optional(t.Any()),
|
|
24
|
+
error: t.Optional(
|
|
25
|
+
t.Object({
|
|
26
|
+
name: t.Optional(t.String()),
|
|
27
|
+
message: t.Optional(t.String()),
|
|
28
|
+
code: t.Optional(t.Number())
|
|
29
|
+
})
|
|
30
|
+
)
|
|
31
|
+
})
|
|
32
|
+
}, async ({ data, conn }) => {
|
|
33
|
+
const { projectId, previewService } = requireBrowserPreviewAccess(conn);
|
|
34
|
+
|
|
35
|
+
const delivered = previewService.respondToHostRequest(data.requestId, {
|
|
36
|
+
ok: data.ok,
|
|
37
|
+
result: data.result,
|
|
38
|
+
error: data.error
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
if (!delivered) {
|
|
42
|
+
// Expected whenever the page gave up first or the tab closed while the
|
|
43
|
+
// viewer was still deciding — not worth surfacing as an error.
|
|
44
|
+
debug.log('preview', `⏭️ Host response for unknown request ${data.requestId} (project: ${projectId})`);
|
|
45
|
+
}
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Push a streamed result into the page.
|
|
50
|
+
*
|
|
51
|
+
* Speech recognition does not fit request/response: one `start()` produces a
|
|
52
|
+
* run of interim and final results, so the viewer keeps sending these until
|
|
53
|
+
* recognition ends.
|
|
54
|
+
*/
|
|
55
|
+
.on('preview:browser-host-event', {
|
|
56
|
+
data: t.Object({
|
|
57
|
+
tabId: t.String({ minLength: 1 }),
|
|
58
|
+
kind: t.String({ minLength: 1 }),
|
|
59
|
+
payload: t.Optional(t.Any())
|
|
60
|
+
})
|
|
61
|
+
}, async ({ data, conn }) => {
|
|
62
|
+
const { previewService } = requireBrowserPreviewAccess(conn);
|
|
63
|
+
await previewService.dispatchHostEvent(data.tabId, data.kind, data.payload);
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Deliver a value chosen in the viewer's own colour or date picker.
|
|
68
|
+
*/
|
|
69
|
+
.on('preview:browser-native-picker-input', {
|
|
70
|
+
data: t.Object({
|
|
71
|
+
tabId: t.String({ minLength: 1 }),
|
|
72
|
+
pickerId: t.String({ minLength: 1 }),
|
|
73
|
+
value: t.String()
|
|
74
|
+
})
|
|
75
|
+
}, async ({ data, conn }) => {
|
|
76
|
+
const { previewService } = requireBrowserPreviewAccess(conn);
|
|
77
|
+
await previewService.handleNativePickerResponse(data.tabId, data.pickerId, data.value);
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
.emit('preview:browser-native-picker', t.Object({
|
|
81
|
+
sessionId: t.String(),
|
|
82
|
+
pickerId: t.String(),
|
|
83
|
+
inputType: t.String(),
|
|
84
|
+
value: t.String(),
|
|
85
|
+
min: t.Optional(t.String()),
|
|
86
|
+
max: t.Optional(t.String()),
|
|
87
|
+
step: t.Optional(t.String()),
|
|
88
|
+
boundingBox: t.Object({
|
|
89
|
+
x: t.Number(),
|
|
90
|
+
y: t.Number(),
|
|
91
|
+
width: t.Number(),
|
|
92
|
+
height: t.Number()
|
|
93
|
+
}),
|
|
94
|
+
timestamp: t.Number()
|
|
95
|
+
}));
|
|
@@ -10,7 +10,8 @@ import { ws } from '$backend/utils/ws';
|
|
|
10
10
|
import type { KeyInput } from 'puppeteer';
|
|
11
11
|
import { debug } from '$shared/utils/logger';
|
|
12
12
|
import { sleep } from '$shared/utils/async';
|
|
13
|
-
import { requireBrowserPreviewAccess } from '../access';
|
|
13
|
+
import { requireBrowserPreviewAccess, requireBrowserTabAccess } from '../access';
|
|
14
|
+
import { resolveFrameTarget } from '$backend/preview/browser/frame-targeting';
|
|
14
15
|
|
|
15
16
|
// Throttle cursor detection evaluate calls per session (100ms = ~10/sec is plenty)
|
|
16
17
|
const lastCursorEvalTime = new Map<string, number>();
|
|
@@ -56,6 +57,7 @@ export const interactPreviewHandler = createRouter()
|
|
|
56
57
|
shiftKey: t.Optional(t.Boolean()),
|
|
57
58
|
clearFirst: t.Optional(t.Boolean()),
|
|
58
59
|
scale: t.Optional(t.Number()),
|
|
60
|
+
dpr: t.Optional(t.Number()),
|
|
59
61
|
width: t.Optional(t.Number()),
|
|
60
62
|
height: t.Optional(t.Number()),
|
|
61
63
|
deviceSize: t.Optional(t.String()),
|
|
@@ -152,6 +154,15 @@ export const interactPreviewHandler = createRouter()
|
|
|
152
154
|
break;
|
|
153
155
|
}
|
|
154
156
|
|
|
157
|
+
// Colour and date inputs open pickers Chrome draws in the
|
|
158
|
+
// browser process, which never reach the screencast — the
|
|
159
|
+
// viewer renders the equivalent control instead.
|
|
160
|
+
const pickerInfo = await previewService.checkForNativePicker(session.id, action.x!, action.y!);
|
|
161
|
+
if (pickerInfo) {
|
|
162
|
+
debug.log('preview', `🎨 Native picker input detected at (${action.x}, ${action.y})`);
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
|
|
155
166
|
// Perform the click - this does down + up atomically
|
|
156
167
|
await session.page.mouse.click(action.x!, action.y!);
|
|
157
168
|
} catch (error) {
|
|
@@ -282,8 +293,65 @@ export const interactPreviewHandler = createRouter()
|
|
|
282
293
|
}
|
|
283
294
|
break;
|
|
284
295
|
|
|
296
|
+
case 'paste':
|
|
297
|
+
// Insert into whatever holds focus, in whichever frame that
|
|
298
|
+
// is. Synthesising Ctrl+V would paste the *headless* browser's
|
|
299
|
+
// clipboard, which is not the one the user copied into.
|
|
300
|
+
if (typeof action.text === 'string') {
|
|
301
|
+
for (const frame of session.page.frames()) {
|
|
302
|
+
try {
|
|
303
|
+
const inserted = await frame.evaluate((text: string) => {
|
|
304
|
+
const active = document.activeElement as HTMLElement | null;
|
|
305
|
+
if (!active) return false;
|
|
306
|
+
|
|
307
|
+
const isField = active.tagName === 'INPUT' || active.tagName === 'TEXTAREA';
|
|
308
|
+
if (!isField && !active.isContentEditable) return false;
|
|
309
|
+
|
|
310
|
+
if (isField) {
|
|
311
|
+
const field = active as HTMLInputElement | HTMLTextAreaElement;
|
|
312
|
+
const start = field.selectionStart ?? field.value.length;
|
|
313
|
+
const end = field.selectionEnd ?? field.value.length;
|
|
314
|
+
field.value = field.value.slice(0, start) + text + field.value.slice(end);
|
|
315
|
+
const caret = start + text.length;
|
|
316
|
+
field.setSelectionRange(caret, caret);
|
|
317
|
+
} else {
|
|
318
|
+
document.execCommand('insertText', false, text);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
active.dispatchEvent(new Event('input', { bubbles: true }));
|
|
322
|
+
active.dispatchEvent(new Event('change', { bubbles: true }));
|
|
323
|
+
return true;
|
|
324
|
+
}, action.text);
|
|
325
|
+
if (inserted) break;
|
|
326
|
+
} catch {
|
|
327
|
+
// Frame detached mid-paste; try the next one.
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
break;
|
|
332
|
+
|
|
333
|
+
case 'stop':
|
|
334
|
+
// `window.stop()` is what the browser's own Stop button calls:
|
|
335
|
+
// it halts the current load and every pending subresource,
|
|
336
|
+
// leaving whatever has already rendered on screen.
|
|
337
|
+
try {
|
|
338
|
+
await session.page.evaluate(() => window.stop());
|
|
339
|
+
} catch {
|
|
340
|
+
// Mid-navigation the context can already be gone — the load
|
|
341
|
+
// it would have stopped is over either way.
|
|
342
|
+
}
|
|
343
|
+
break;
|
|
344
|
+
|
|
285
345
|
case 'scroll':
|
|
286
346
|
try {
|
|
347
|
+
// CDP dispatches a wheel at the *current* pointer position,
|
|
348
|
+
// so a scroll that arrives without one first lands wherever
|
|
349
|
+
// the last click left the cursor — scrolling the wrong
|
|
350
|
+
// container and stranding hover state there. Touch scrolling
|
|
351
|
+
// has no pointer of its own, hence the explicit move.
|
|
352
|
+
if (action.x !== undefined && action.y !== undefined) {
|
|
353
|
+
await session.page.mouse.move(action.x, action.y, { steps: 1 });
|
|
354
|
+
}
|
|
287
355
|
await session.page.mouse.wheel({ deltaX: action.deltaX || 0, deltaY: action.deltaY || 0 });
|
|
288
356
|
} catch (error) {
|
|
289
357
|
if (error instanceof Error && isNavigationError(error)) {
|
|
@@ -445,20 +513,25 @@ export const interactPreviewHandler = createRouter()
|
|
|
445
513
|
break;
|
|
446
514
|
|
|
447
515
|
case 'scale-update':
|
|
448
|
-
//
|
|
449
|
-
//
|
|
450
|
-
//
|
|
451
|
-
//
|
|
516
|
+
// Recovery path: the frontend sends this when the stream
|
|
517
|
+
// looks stuck.
|
|
518
|
+
//
|
|
519
|
+
// It no longer carries display metrics. Those are tracked
|
|
520
|
+
// per viewer — a tab can be watched from several devices
|
|
521
|
+
// with different screens — and reach the source through
|
|
522
|
+
// `preview:browser-stream-display`, which says which
|
|
523
|
+
// viewer they describe. An interaction cannot.
|
|
452
524
|
if (action.scale && action.scale > 0 && action.scale <= 1) {
|
|
453
525
|
session.scale = action.scale;
|
|
454
|
-
debug.log('preview', `📐 Scale updated for tab ${tabId}: ${action.scale}
|
|
526
|
+
debug.log('preview', `📐 Scale updated for tab ${tabId}: ${action.scale}, refreshing capture`);
|
|
455
527
|
|
|
456
528
|
const updated = await previewService.refreshWebCodecsScreencast(session.id);
|
|
457
529
|
if (!updated) {
|
|
458
|
-
|
|
459
|
-
//
|
|
460
|
-
|
|
461
|
-
|
|
530
|
+
// Restarting from here would tear the capture down for
|
|
531
|
+
// every viewer of this tab and rebuild it with none.
|
|
532
|
+
// The viewer that noticed the stall owns its own
|
|
533
|
+
// recovery and will re-handshake.
|
|
534
|
+
debug.warn('preview', `⚠️ Screencast refresh failed for tab ${tabId}, leaving recovery to the viewer`);
|
|
462
535
|
}
|
|
463
536
|
}
|
|
464
537
|
break;
|
|
@@ -472,6 +545,10 @@ export const interactPreviewHandler = createRouter()
|
|
|
472
545
|
|
|
473
546
|
debug.log('preview', `📱 Viewport updated for tab ${tabId}: ${action.width}x${action.height}`);
|
|
474
547
|
|
|
548
|
+
// The hot-swap recomputes capture geometry from the new
|
|
549
|
+
// viewport and the viewers' own display metrics, which
|
|
550
|
+
// each of them reports separately.
|
|
551
|
+
|
|
475
552
|
// Hot-swap viewport without reconnection
|
|
476
553
|
const updated = await previewService.updateWebCodecsViewport(session.id, action.width, action.height);
|
|
477
554
|
if (!updated) {
|
|
@@ -518,6 +595,123 @@ export const interactPreviewHandler = createRouter()
|
|
|
518
595
|
}
|
|
519
596
|
})
|
|
520
597
|
|
|
598
|
+
/**
|
|
599
|
+
* Read (and optionally cut) the page's current text selection.
|
|
600
|
+
*
|
|
601
|
+
* Ctrl+C inside the preview cannot be forwarded as a keystroke: that would
|
|
602
|
+
* fill the *headless* browser's clipboard, which nothing the user can paste
|
|
603
|
+
* into ever reads. The text is returned instead so the viewer can put it on
|
|
604
|
+
* their own clipboard.
|
|
605
|
+
*/
|
|
606
|
+
.http('preview:browser-selection', {
|
|
607
|
+
data: t.Object({
|
|
608
|
+
cut: t.Optional(t.Boolean())
|
|
609
|
+
}),
|
|
610
|
+
response: t.Object({
|
|
611
|
+
text: t.String()
|
|
612
|
+
})
|
|
613
|
+
}, async ({ data, conn }) => {
|
|
614
|
+
const { tab } = requireBrowserTabAccess(conn);
|
|
615
|
+
|
|
616
|
+
// Selections inside an iframe are invisible to the top document, so every
|
|
617
|
+
// frame is asked until one reports a selection.
|
|
618
|
+
for (const frame of tab.page.frames()) {
|
|
619
|
+
try {
|
|
620
|
+
const text = await frame.evaluate((remove: boolean) => {
|
|
621
|
+
const selected = window.getSelection()?.toString() ?? '';
|
|
622
|
+
if (selected && remove) document.execCommand('delete');
|
|
623
|
+
return selected;
|
|
624
|
+
}, !!data.cut);
|
|
625
|
+
if (text) return { text };
|
|
626
|
+
} catch {
|
|
627
|
+
// Detached or cross-origin mid-navigation; keep looking.
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
return { text: '' };
|
|
632
|
+
})
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
* Report whether the element at a point accepts text.
|
|
636
|
+
*
|
|
637
|
+
* This is what decides the on-screen keyboard, and it is a *hit test* rather
|
|
638
|
+
* than a focus read for a reason. Focus is only settled once the tap has been
|
|
639
|
+
* dispatched and the page has run its handlers, so asking for it right after
|
|
640
|
+
* a tap answered for the *previous* tap: the first tap on a field reported
|
|
641
|
+
* "not editable" and only the second one raised the keyboard, while a tap on
|
|
642
|
+
* empty space after typing still reported the old field and popped the
|
|
643
|
+
* keyboard back up. The element under the finger is known before any of that
|
|
644
|
+
* happens, so the viewer can ask on `touchstart` and act on `touchend` —
|
|
645
|
+
* which is also the only moment iOS will honour a programmatic focus.
|
|
646
|
+
*/
|
|
647
|
+
.http('preview:browser-hit-test', {
|
|
648
|
+
data: t.Object({
|
|
649
|
+
x: t.Number(),
|
|
650
|
+
y: t.Number()
|
|
651
|
+
}),
|
|
652
|
+
response: t.Object({
|
|
653
|
+
editable: t.Boolean(),
|
|
654
|
+
inputType: t.Optional(t.String())
|
|
655
|
+
})
|
|
656
|
+
}, async ({ data, conn }) => {
|
|
657
|
+
const { tab } = requireBrowserTabAccess(conn);
|
|
658
|
+
|
|
659
|
+
try {
|
|
660
|
+
// Fields inside an iframe are invisible to the top document, where
|
|
661
|
+
// `elementFromPoint` returns the <iframe> element itself.
|
|
662
|
+
const target = await resolveFrameTarget(tab.page, data.x, data.y);
|
|
663
|
+
|
|
664
|
+
return await target.frame.evaluate(
|
|
665
|
+
(point: { x: number; y: number }) => {
|
|
666
|
+
const el = document.elementFromPoint(point.x, point.y) as HTMLElement | null;
|
|
667
|
+
if (!el) return { editable: false };
|
|
668
|
+
|
|
669
|
+
// A field is often wrapped — the tap lands on a label, an icon or
|
|
670
|
+
// the padded box around the input rather than the input itself.
|
|
671
|
+
const field = el.closest(
|
|
672
|
+
'input, textarea, [contenteditable=""], [contenteditable="true"]'
|
|
673
|
+
) as HTMLInputElement | HTMLTextAreaElement | HTMLElement | null;
|
|
674
|
+
if (!field) return { editable: false };
|
|
675
|
+
|
|
676
|
+
if (field.isContentEditable) return { editable: true, inputType: 'contenteditable' };
|
|
677
|
+
|
|
678
|
+
const tag = field.tagName;
|
|
679
|
+
if (tag === 'TEXTAREA') return { editable: true, inputType: 'textarea' };
|
|
680
|
+
|
|
681
|
+
const type = ((field as HTMLInputElement).type || 'text').toLowerCase();
|
|
682
|
+
const nonText = [
|
|
683
|
+
'checkbox',
|
|
684
|
+
'radio',
|
|
685
|
+
'button',
|
|
686
|
+
'submit',
|
|
687
|
+
'reset',
|
|
688
|
+
'file',
|
|
689
|
+
'image',
|
|
690
|
+
'range',
|
|
691
|
+
'color',
|
|
692
|
+
'date',
|
|
693
|
+
'datetime-local',
|
|
694
|
+
'month',
|
|
695
|
+
'time',
|
|
696
|
+
'week'
|
|
697
|
+
];
|
|
698
|
+
if (nonText.includes(type)) return { editable: false };
|
|
699
|
+
if ((field as HTMLInputElement).readOnly || (field as HTMLInputElement).disabled) {
|
|
700
|
+
return { editable: false };
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
return { editable: true, inputType: type };
|
|
704
|
+
},
|
|
705
|
+
{ x: target.x, y: target.y }
|
|
706
|
+
);
|
|
707
|
+
} catch {
|
|
708
|
+
// Detached or mid-navigation — treat as "no field" so the viewer leaves
|
|
709
|
+
// the keyboard alone rather than raising it over nothing.
|
|
710
|
+
return { editable: false };
|
|
711
|
+
}
|
|
712
|
+
})
|
|
713
|
+
|
|
714
|
+
|
|
521
715
|
// Server → Client Events (independent declarations)
|
|
522
716
|
.emit('preview:browser-interacted', t.Object({
|
|
523
717
|
action: t.String(),
|
|
@@ -146,6 +146,19 @@ export const nativeUIPreviewHandler = createRouter()
|
|
|
146
146
|
timestamp: t.Number()
|
|
147
147
|
}))
|
|
148
148
|
|
|
149
|
+
/**
|
|
150
|
+
* The page's dialog has been answered — by whoever got there first.
|
|
151
|
+
*
|
|
152
|
+
* A dialog belongs to the page, not to a viewer, but each viewer was shown
|
|
153
|
+
* its own copy of the prompt. Without this the other devices keep an overlay
|
|
154
|
+
* for a dialog that no longer exists, and answering it again does nothing.
|
|
155
|
+
*/
|
|
156
|
+
.emit('preview:browser-dialog-closed', t.Object({
|
|
157
|
+
sessionId: t.String(),
|
|
158
|
+
dialogId: t.String(),
|
|
159
|
+
timestamp: t.Number()
|
|
160
|
+
}))
|
|
161
|
+
|
|
149
162
|
.emit('preview:browser-print', t.Object({
|
|
150
163
|
sessionId: t.String(),
|
|
151
164
|
timestamp: t.Number()
|
|
@@ -194,10 +207,16 @@ export const nativeUIPreviewHandler = createRouter()
|
|
|
194
207
|
isLink: t.Boolean(),
|
|
195
208
|
isImage: t.Boolean(),
|
|
196
209
|
isInput: t.Boolean(),
|
|
210
|
+
isEditable: t.Boolean(),
|
|
197
211
|
isTextSelected: t.Boolean(),
|
|
212
|
+
selectedText: t.Optional(t.String()),
|
|
198
213
|
linkUrl: t.Optional(t.String()),
|
|
214
|
+
linkText: t.Optional(t.String()),
|
|
199
215
|
imageUrl: t.Optional(t.String()),
|
|
200
|
-
|
|
216
|
+
mediaUrl: t.Optional(t.String()),
|
|
217
|
+
mediaType: t.Optional(t.String()),
|
|
218
|
+
inputType: t.Optional(t.String()),
|
|
219
|
+
pageUrl: t.Optional(t.String())
|
|
201
220
|
}),
|
|
202
221
|
timestamp: t.Number()
|
|
203
222
|
}))
|
|
@@ -210,6 +229,17 @@ export const nativeUIPreviewHandler = createRouter()
|
|
|
210
229
|
url: t.String()
|
|
211
230
|
}))
|
|
212
231
|
|
|
232
|
+
// "Open in Your Browser" — leaves the preview entirely and hands the URL to
|
|
233
|
+
// the viewer's own browser.
|
|
234
|
+
.emit('preview:browser-open-url-host', t.Object({
|
|
235
|
+
url: t.String()
|
|
236
|
+
}))
|
|
237
|
+
|
|
238
|
+
// "Inspect" — asks the viewer to reveal the console panel.
|
|
239
|
+
.emit('preview:browser-open-inspector', t.Object({
|
|
240
|
+
timestamp: t.Number()
|
|
241
|
+
}))
|
|
242
|
+
|
|
213
243
|
.emit('preview:browser-download-image', t.Object({
|
|
214
244
|
base64: t.String(),
|
|
215
245
|
type: t.String(),
|
|
@@ -20,11 +20,14 @@ export const tabInfoPreviewHandler = createRouter()
|
|
|
20
20
|
tabId: t.String(),
|
|
21
21
|
url: t.String(),
|
|
22
22
|
title: t.String(),
|
|
23
|
+
favicon: t.Optional(t.String()),
|
|
23
24
|
quality: t.String(),
|
|
24
25
|
isStreaming: t.Boolean(),
|
|
25
26
|
deviceSize: t.String(),
|
|
26
27
|
rotation: t.String(),
|
|
27
|
-
isActive: t.Boolean()
|
|
28
|
+
isActive: t.Boolean(),
|
|
29
|
+
canGoBack: t.Boolean(),
|
|
30
|
+
canGoForward: t.Boolean()
|
|
28
31
|
})
|
|
29
32
|
}, async ({ data, conn }) => {
|
|
30
33
|
const { tabId } = data;
|
|
@@ -52,11 +55,14 @@ export const tabInfoPreviewHandler = createRouter()
|
|
|
52
55
|
tabId: t.String(),
|
|
53
56
|
url: t.String(),
|
|
54
57
|
title: t.String(),
|
|
58
|
+
favicon: t.Optional(t.String()),
|
|
55
59
|
quality: t.String(),
|
|
56
60
|
isStreaming: t.Boolean(),
|
|
57
61
|
deviceSize: t.String(),
|
|
58
62
|
rotation: t.String(),
|
|
59
63
|
isActive: t.Boolean(),
|
|
64
|
+
canGoBack: t.Boolean(),
|
|
65
|
+
canGoForward: t.Boolean(),
|
|
60
66
|
isMcpControlled: t.Boolean()
|
|
61
67
|
})),
|
|
62
68
|
activeTabId: t.Union([t.String(), t.Null()]),
|
|
@@ -77,11 +83,14 @@ export const tabInfoPreviewHandler = createRouter()
|
|
|
77
83
|
tabId: tab.id,
|
|
78
84
|
url: tab.url,
|
|
79
85
|
title: tab.title,
|
|
86
|
+
favicon: tab.favicon,
|
|
80
87
|
quality: tab.quality,
|
|
81
88
|
isStreaming: tab.isStreaming,
|
|
82
89
|
deviceSize: tab.deviceSize,
|
|
83
90
|
rotation: tab.rotation,
|
|
84
91
|
isActive: tab.isActive,
|
|
92
|
+
canGoBack: tab.canGoBack,
|
|
93
|
+
canGoForward: tab.canGoForward,
|
|
85
94
|
isMcpControlled: browserMcpControl.isTabControlled(tab.id, projectId)
|
|
86
95
|
})),
|
|
87
96
|
activeTabId: activeTab?.id || null,
|
|
@@ -106,6 +106,49 @@ export const tabPreviewHandler = createRouter()
|
|
|
106
106
|
};
|
|
107
107
|
})
|
|
108
108
|
|
|
109
|
+
// Walk the tab's history (Back / Forward)
|
|
110
|
+
.http('preview:browser-tab-history-go', {
|
|
111
|
+
data: t.Object({
|
|
112
|
+
direction: t.Union([t.Literal('back'), t.Literal('forward')]),
|
|
113
|
+
tabId: t.Optional(t.String())
|
|
114
|
+
}),
|
|
115
|
+
response: t.Object({
|
|
116
|
+
moved: t.Boolean(),
|
|
117
|
+
tabId: t.String()
|
|
118
|
+
})
|
|
119
|
+
}, async ({ data, conn }) => {
|
|
120
|
+
const { projectId, previewService, tab } = requireBrowserTabAccess(conn, data.tabId);
|
|
121
|
+
|
|
122
|
+
debug.log('preview', `↩️ History ${data.direction} for tab ${tab.id} (project: ${projectId})`);
|
|
123
|
+
|
|
124
|
+
const moved = await previewService.goHistory(tab.id, data.direction === 'back' ? -1 : 1);
|
|
125
|
+
|
|
126
|
+
return { moved, tabId: tab.id };
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
// Read the tab's history list — backs the long-press dropdown on Back
|
|
130
|
+
.http('preview:browser-tab-history', {
|
|
131
|
+
data: t.Object({
|
|
132
|
+
tabId: t.Optional(t.String())
|
|
133
|
+
}),
|
|
134
|
+
response: t.Object({
|
|
135
|
+
entries: t.Array(t.Object({
|
|
136
|
+
id: t.Number(),
|
|
137
|
+
url: t.String(),
|
|
138
|
+
title: t.String()
|
|
139
|
+
})),
|
|
140
|
+
currentIndex: t.Number(),
|
|
141
|
+
canGoBack: t.Boolean(),
|
|
142
|
+
canGoForward: t.Boolean()
|
|
143
|
+
})
|
|
144
|
+
}, async ({ data, conn }) => {
|
|
145
|
+
const { previewService, tab } = requireBrowserTabAccess(conn, data.tabId);
|
|
146
|
+
|
|
147
|
+
const state = await previewService.getHistoryState(tab.id);
|
|
148
|
+
|
|
149
|
+
return state ?? { entries: [], currentIndex: 0, canGoBack: false, canGoForward: false };
|
|
150
|
+
})
|
|
151
|
+
|
|
109
152
|
// Close browser tab
|
|
110
153
|
.http('preview:browser-tab-close', {
|
|
111
154
|
data: t.Object({
|