@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.
Files changed (86) hide show
  1. package/README.md +2 -1
  2. package/backend/artifacts/generate.ts +11 -5
  3. package/backend/auth/permissions.ts +8 -8
  4. package/backend/engine/README.md +11 -8
  5. package/backend/engine/adapters/codex/error-handler.ts +1 -1
  6. package/backend/engine/adapters/qwen/error-handler.ts +1 -1
  7. package/backend/engine/docs/adapter-contract.md +66 -2
  8. package/backend/engine/docs/adding-an-engine.md +46 -7
  9. package/backend/engine/docs/frontend-and-chat.md +80 -17
  10. package/backend/engine/docs/lessons-learned.md +126 -7
  11. package/backend/engine/docs/quick-reference.md +12 -3
  12. package/backend/engine/docs/{system-tools.md → stack.md} +35 -6
  13. package/backend/engine/docs/websocket-routes.md +54 -15
  14. package/backend/engine/install-runner.ts +4 -4
  15. package/backend/engine/resolve-model.ts +87 -0
  16. package/backend/engine/structured-helpers.test.ts +64 -0
  17. package/backend/engine/structured-helpers.ts +105 -13
  18. package/backend/mcp/README.md +32 -11
  19. package/backend/mcp/internal/config.ts +3 -20
  20. package/backend/mcp/internal/servers/browser-automation/actions/console.ts +65 -0
  21. package/backend/mcp/internal/servers/browser-automation/actions/dom-analyze.ts +202 -0
  22. package/backend/mcp/internal/servers/browser-automation/actions/index.ts +31 -0
  23. package/backend/mcp/internal/servers/browser-automation/actions/input.ts +332 -0
  24. package/backend/mcp/internal/servers/browser-automation/actions/inspect.ts +158 -0
  25. package/backend/mcp/internal/servers/browser-automation/actions/navigation.ts +157 -0
  26. package/backend/mcp/internal/servers/browser-automation/actions/shared.ts +60 -0
  27. package/backend/mcp/internal/servers/browser-automation/actions/tabs.ts +146 -0
  28. package/backend/mcp/internal/servers/browser-automation/actions/types.ts +63 -0
  29. package/backend/mcp/internal/servers/browser-automation/context.ts +82 -0
  30. package/backend/mcp/internal/servers/browser-automation/description.ts +59 -0
  31. package/backend/mcp/internal/servers/browser-automation/format.ts +56 -0
  32. package/backend/mcp/internal/servers/browser-automation/index.ts +36 -784
  33. package/backend/mcp/internal/servers/browser-automation/paths.ts +71 -0
  34. package/backend/mcp/internal/servers/browser-automation/runner.ts +260 -0
  35. package/backend/mcp/internal/servers/browser-automation/schema.ts +26 -0
  36. package/backend/preview/browser/browser-console-manager.ts +310 -156
  37. package/backend/preview/browser/browser-dialog-handler.ts +45 -16
  38. package/backend/preview/browser/browser-host-bridge.ts +471 -0
  39. package/backend/preview/browser/browser-interaction-handler.ts +868 -297
  40. package/backend/preview/browser/browser-mcp-control.ts +7 -2
  41. package/backend/preview/browser/browser-native-ui-handler.ts +455 -81
  42. package/backend/preview/browser/browser-navigation-tracker.ts +56 -0
  43. package/backend/preview/browser/browser-pool.ts +83 -9
  44. package/backend/preview/browser/browser-preview-service.ts +432 -53
  45. package/backend/preview/browser/browser-tab-manager.ts +16 -16
  46. package/backend/preview/browser/browser-video-capture.ts +1334 -386
  47. package/backend/preview/browser/capture-profile.ts +213 -0
  48. package/backend/preview/browser/element-locator.ts +357 -0
  49. package/backend/preview/browser/frame-targeting.ts +92 -0
  50. package/backend/preview/browser/scripts/audio-stream.ts +258 -78
  51. package/backend/preview/browser/scripts/host-bridge.ts +1277 -0
  52. package/backend/preview/browser/scripts/video-stream.ts +803 -177
  53. package/backend/preview/browser/types.ts +397 -37
  54. package/backend/utils/cli.ts +1 -1
  55. package/backend/utils/path-enrich.ts +2 -2
  56. package/backend/ws/README.md +1 -1
  57. package/backend/ws/artifacts/generate.ts +2 -1
  58. package/backend/ws/engine/codex/accounts.ts +1 -1
  59. package/backend/ws/engine/codex/status.ts +1 -1
  60. package/backend/ws/git/branch-name.ts +11 -5
  61. package/backend/ws/git/commit-message.ts +11 -5
  62. package/backend/ws/index.ts +3 -3
  63. package/backend/ws/preview/browser/host.ts +95 -0
  64. package/backend/ws/preview/browser/interact.ts +204 -10
  65. package/backend/ws/preview/browser/native-ui.ts +31 -1
  66. package/backend/ws/preview/browser/tab-info.ts +10 -1
  67. package/backend/ws/preview/browser/tab.ts +43 -0
  68. package/backend/ws/preview/browser/webcodecs.ts +180 -53
  69. package/backend/ws/preview/index.ts +58 -1
  70. package/backend/ws/{system-tools → stack}/access.ts +1 -1
  71. package/backend/ws/stack/index.ts +16 -0
  72. package/backend/ws/{system-tools → stack}/install.ts +7 -7
  73. package/backend/ws/{system-tools → stack}/status.ts +7 -7
  74. package/dist/assets/{index-CEGW8vEt.js → index-DH_GO0Lf.js} +1 -1
  75. package/dist/assets/{index-B8YRz8wG.js → index-igSQ3JaM.js} +1 -1
  76. package/dist/assets/main-BD3-qPfT.css +1 -0
  77. package/dist/assets/{main-D-2ByODy.js → main-D-d8SFfW.js} +384 -358
  78. package/dist/assets/preview-video.worker-DcpDhCWL.js +1 -0
  79. package/dist/index.html +2 -2
  80. package/package.json +1 -1
  81. package/shared/types/stores/settings.ts +6 -0
  82. package/backend/mcp/internal/servers/browser-automation/actions.ts +0 -159
  83. package/backend/mcp/internal/servers/browser-automation/browser.ts +0 -375
  84. package/backend/mcp/internal/servers/browser-automation/inspection.ts +0 -495
  85. package/backend/ws/system-tools/index.ts +0 -16
  86. package/dist/assets/main-D8I0MfRQ.css +0 -1
@@ -260,6 +260,62 @@ export class BrowserNavigationTracker extends EventEmitter {
260
260
  }
261
261
  }
262
262
 
263
+ /**
264
+ * Reuse the tab's tracking CDP session, creating one if setup failed or ran
265
+ * before this call. History reads happen on every navigation, so attaching a
266
+ * fresh session each time would be wasteful.
267
+ */
268
+ private async getCdpSession(sessionId: string, page: Page): Promise<CDPSession> {
269
+ const existing = this.cdpSessions.get(sessionId);
270
+ if (existing) return existing;
271
+
272
+ const cdp = await page.createCDPSession();
273
+ this.cdpSessions.set(sessionId, cdp);
274
+ return cdp;
275
+ }
276
+
277
+ /**
278
+ * Read the tab's real navigation history from CDP.
279
+ *
280
+ * `window.history.length` cannot answer "can I go forward?" and over-counts
281
+ * for SPAs; `Page.getNavigationHistory` returns the actual entry list plus
282
+ * the current index, which is what a real browser's buttons read.
283
+ */
284
+ async getNavigationHistory(
285
+ sessionId: string,
286
+ page: Page
287
+ ): Promise<{ currentIndex: number; entries: Array<{ id: number; url: string; title: string }> } | null> {
288
+ try {
289
+ const cdp = await this.getCdpSession(sessionId, page);
290
+ const history = await cdp.send('Page.getNavigationHistory');
291
+ return {
292
+ currentIndex: history.currentIndex,
293
+ entries: history.entries.map((entry) => ({
294
+ id: entry.id,
295
+ url: entry.url,
296
+ title: entry.title || entry.url
297
+ }))
298
+ };
299
+ } catch (error) {
300
+ debug.warn('preview', `⚠️ Failed to read navigation history for ${sessionId}:`, error);
301
+ return null;
302
+ }
303
+ }
304
+
305
+ /**
306
+ * Jump to a specific history entry by CDP entry id.
307
+ */
308
+ async navigateToHistoryEntry(sessionId: string, page: Page, entryId: number): Promise<boolean> {
309
+ try {
310
+ const cdp = await this.getCdpSession(sessionId, page);
311
+ await cdp.send('Page.navigateToHistoryEntry', { entryId });
312
+ return true;
313
+ } catch (error) {
314
+ debug.warn('preview', `⚠️ Failed to navigate to history entry ${entryId} for ${sessionId}:`, error);
315
+ return false;
316
+ }
317
+ }
318
+
263
319
  /**
264
320
  * Cleanup CDP session for a tab
265
321
  */
@@ -15,6 +15,7 @@
15
15
  * - Direct launch() ensures puppeteer-extra wraps ALL page creation correctly.
16
16
  */
17
17
 
18
+ import { existsSync } from 'fs';
18
19
  import type { Browser, BrowserContext, Page } from 'puppeteer';
19
20
  import { debug } from '$shared/utils/logger';
20
21
  import puppeteer from 'puppeteer-extra';
@@ -45,15 +46,88 @@ const DEFAULT_CONFIG: PoolConfig = {
45
46
  };
46
47
 
47
48
  /**
48
- * Chrome launch arguments for stealth (matches test-cf.ts exactly)
49
+ * Chrome launch arguments.
50
+ *
51
+ * Three groups, kept separate because they answer different questions:
52
+ * stealth (Cloudflare bypass — matches test-cf.ts), capture (what the preview
53
+ * pipeline needs from the renderer), and host adaptation (what a headless VPS
54
+ * needs that a desktop doesn't).
49
55
  */
50
- const CHROME_ARGS = [
51
- '--no-sandbox',
52
- '--disable-blink-features=AutomationControlled',
53
- '--window-size=1366,768',
54
- '--autoplay-policy=no-user-gesture-required',
55
- '--disable-features=AudioServiceOutOfProcess,WebRtcHideLocalIpsWithMdns'
56
- ];
56
+ function buildChromeArgs(): string[] {
57
+ // Chrome only honours the last `--disable-features`, so every entry has to
58
+ // live in one combined flag.
59
+ const disabledFeatures = [
60
+ 'AudioServiceOutOfProcess',
61
+ 'WebRtcHideLocalIpsWithMdns',
62
+ // Occlusion detection throttles renderers Chrome thinks nobody is
63
+ // looking at — which is every headless tab, including the one we are
64
+ // actively streaming.
65
+ 'CalculateNativeWinOcclusion'
66
+ ];
67
+
68
+ const args = [
69
+ '--no-sandbox',
70
+ '--disable-blink-features=AutomationControlled',
71
+ '--window-size=1366,768',
72
+ '--autoplay-policy=no-user-gesture-required',
73
+
74
+ // In-page capture (getDisplayMedia({preferCurrentTab})) — lets the
75
+ // encoder read compositor frames directly instead of round-tripping
76
+ // JPEG through CDP. Without this the call would wait on a picker that
77
+ // can never be answered in headless.
78
+ '--auto-accept-this-tab-capture',
79
+
80
+ // A headless tab is never "visible" or "focused", and Chrome
81
+ // aggressively de-prioritises renderers in that state — timers get
82
+ // clamped and compositing stalls, which reads as a frozen preview.
83
+ '--disable-background-timer-throttling',
84
+ '--disable-backgrounding-occluded-windows',
85
+ '--disable-renderer-backgrounding',
86
+
87
+ // Containers and small VPS instances often ship a 64MB /dev/shm;
88
+ // exceeding it crashes the renderer mid-stream.
89
+ '--disable-dev-shm-usage',
90
+
91
+ '--no-first-run',
92
+ '--no-default-browser-check',
93
+ '--disable-hang-monitor',
94
+ '--metrics-recording-only',
95
+ '--force-color-profile=srgb'
96
+ ];
97
+
98
+ if (shouldDisableGpu()) {
99
+ // Without a real GPU, Chrome falls back to SwiftShader — a software
100
+ // GL implementation whose setup and per-frame cost exceed plain CPU
101
+ // rasterisation for the 2D content a preview shows.
102
+ args.push('--disable-gpu', '--disable-software-rasterizer');
103
+ }
104
+
105
+ args.push(`--disable-features=${disabledFeatures.join(',')}`);
106
+
107
+ return args;
108
+ }
109
+
110
+ /**
111
+ * Whether this host benefits from skipping GPU compositing entirely.
112
+ *
113
+ * `auto` only disables it on Linux hosts with no render node, i.e. headless
114
+ * servers — a Linux desktop keeps its GPU so WebGL previews still work.
115
+ */
116
+ function shouldDisableGpu(): boolean {
117
+ const override = (process.env.CLOPEN_PREVIEW_GPU || '').trim().toLowerCase();
118
+ if (override === 'off') return true;
119
+ if (override === 'on') return false;
120
+
121
+ if (process.platform !== 'linux') return false;
122
+
123
+ try {
124
+ return !existsSync('/dev/dri');
125
+ } catch {
126
+ return true;
127
+ }
128
+ }
129
+
130
+ const CHROME_ARGS = buildChromeArgs();
57
131
 
58
132
  class BrowserPool {
59
133
  private browser: Browser | null = null;
@@ -104,7 +178,7 @@ class BrowserPool {
104
178
  // via the distro package manager (Linux).
105
179
  const executablePath = getChromeExecutablePath();
106
180
  if (!executablePath) {
107
- throw new Error('Chrome not installed. Go to Settings → System Tools and click Install.');
181
+ throw new Error('Chrome not installed. Go to Settings → Stack and click Install.');
108
182
  }
109
183
  debug.log('preview', ` using Chrome at: ${executablePath}`);
110
184