@web-auto/camo 0.2.0 → 0.2.2

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 (114) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +586 -586
  3. package/bin/browser-service.mjs +11 -11
  4. package/bin/camo.mjs +22 -22
  5. package/package.json +48 -48
  6. package/scripts/build.mjs +19 -19
  7. package/scripts/bump-version.mjs +34 -34
  8. package/scripts/check-file-size.mjs +80 -80
  9. package/scripts/file-size-policy.json +12 -2
  10. package/scripts/install.mjs +76 -76
  11. package/scripts/release.sh +54 -54
  12. package/src/autoscript/action-providers/index.mjs +6 -6
  13. package/src/autoscript/impact-engine.mjs +78 -78
  14. package/src/autoscript/runtime.mjs +1017 -1017
  15. package/src/autoscript/schema.mjs +376 -376
  16. package/src/cli.mjs +405 -405
  17. package/src/commands/attach.mjs +141 -141
  18. package/src/commands/autoscript.mjs +1011 -1011
  19. package/src/commands/browser.mjs +1255 -1255
  20. package/src/commands/container.mjs +401 -401
  21. package/src/commands/cookies.mjs +69 -69
  22. package/src/commands/create.mjs +98 -98
  23. package/src/commands/devtools.mjs +349 -349
  24. package/src/commands/events.mjs +152 -152
  25. package/src/commands/highlight-mode.mjs +24 -24
  26. package/src/commands/init.mjs +68 -68
  27. package/src/commands/lifecycle.mjs +275 -275
  28. package/src/commands/mouse.mjs +45 -45
  29. package/src/commands/profile.mjs +46 -46
  30. package/src/commands/record.mjs +115 -115
  31. package/src/commands/system.mjs +14 -14
  32. package/src/commands/window.mjs +123 -123
  33. package/src/container/change-notifier.mjs +362 -362
  34. package/src/container/element-filter.mjs +143 -143
  35. package/src/container/index.mjs +3 -3
  36. package/src/container/runtime-core/checkpoint.mjs +209 -209
  37. package/src/container/runtime-core/index.mjs +21 -21
  38. package/src/container/runtime-core/operations/index.mjs +774 -774
  39. package/src/container/runtime-core/operations/selector-scripts.mjs +277 -277
  40. package/src/container/runtime-core/operations/tab-pool.mjs +746 -746
  41. package/src/container/runtime-core/operations/viewport.mjs +189 -189
  42. package/src/container/runtime-core/search.mjs +190 -190
  43. package/src/container/runtime-core/subscription.mjs +224 -224
  44. package/src/container/runtime-core/utils.mjs +94 -94
  45. package/src/container/runtime-core/validation.mjs +127 -127
  46. package/src/container/runtime-core.mjs +1 -1
  47. package/src/container/subscription-registry.mjs +459 -459
  48. package/src/core/actions.mjs +561 -561
  49. package/src/core/browser.mjs +266 -266
  50. package/src/core/index.mjs +52 -52
  51. package/src/core/utils.mjs +91 -91
  52. package/src/events/daemon-entry.mjs +33 -33
  53. package/src/events/daemon.mjs +80 -80
  54. package/src/events/progress-log.mjs +109 -109
  55. package/src/events/ws-server.mjs +239 -239
  56. package/src/lib/client.mjs +200 -200
  57. package/src/lifecycle/cleanup.mjs +83 -83
  58. package/src/lifecycle/lock.mjs +126 -126
  59. package/src/lifecycle/session-registry.mjs +279 -279
  60. package/src/lifecycle/session-view.mjs +76 -76
  61. package/src/lifecycle/session-watchdog.mjs +281 -281
  62. package/src/services/browser-service/index.js +671 -671
  63. package/src/services/browser-service/internal/BrowserSession.input.test.js +389 -389
  64. package/src/services/browser-service/internal/BrowserSession.js +325 -304
  65. package/src/services/browser-service/internal/ElementRegistry.js +60 -60
  66. package/src/services/browser-service/internal/ProfileLock.js +84 -84
  67. package/src/services/browser-service/internal/SessionManager.js +184 -184
  68. package/src/services/browser-service/internal/SessionManager.test.js +39 -39
  69. package/src/services/browser-service/internal/browser-session/cookies.js +144 -144
  70. package/src/services/browser-service/internal/browser-session/input-ops.js +222 -222
  71. package/src/services/browser-service/internal/browser-session/input-pipeline.js +144 -144
  72. package/src/services/browser-service/internal/browser-session/logging.js +46 -46
  73. package/src/services/browser-service/internal/browser-session/navigation.js +38 -38
  74. package/src/services/browser-service/internal/browser-session/page-hooks.js +442 -442
  75. package/src/services/browser-service/internal/browser-session/page-management.js +302 -302
  76. package/src/services/browser-service/internal/browser-session/page-management.test.js +148 -148
  77. package/src/services/browser-service/internal/browser-session/recording.js +198 -198
  78. package/src/services/browser-service/internal/browser-session/runtime-events.js +61 -61
  79. package/src/services/browser-service/internal/browser-session/session-core.js +84 -84
  80. package/src/services/browser-service/internal/browser-session/session-state.js +38 -38
  81. package/src/services/browser-service/internal/browser-session/types.js +14 -14
  82. package/src/services/browser-service/internal/browser-session/utils.js +95 -95
  83. package/src/services/browser-service/internal/browser-session/viewport-manager.js +46 -46
  84. package/src/services/browser-service/internal/browser-session/viewport.js +215 -215
  85. package/src/services/browser-service/internal/container-matcher.js +851 -851
  86. package/src/services/browser-service/internal/container-registry.js +182 -182
  87. package/src/services/browser-service/internal/engine-manager.js +259 -259
  88. package/src/services/browser-service/internal/fingerprint.js +203 -203
  89. package/src/services/browser-service/internal/heartbeat.js +137 -137
  90. package/src/services/browser-service/internal/logging.js +46 -46
  91. package/src/services/browser-service/internal/page-runtime/runtime.js +1317 -1317
  92. package/src/services/browser-service/internal/pageRuntime.js +28 -28
  93. package/src/services/browser-service/internal/runtimeInjector.js +31 -31
  94. package/src/services/browser-service/internal/service-process-logger.js +140 -140
  95. package/src/services/browser-service/internal/state-bus.js +45 -45
  96. package/src/services/browser-service/internal/storage-paths.js +42 -42
  97. package/src/services/browser-service/internal/ws-server.js +1194 -1194
  98. package/src/services/browser-service/internal/ws-server.test.js +58 -58
  99. package/src/services/browser-service/server.mjs +6 -6
  100. package/src/services/controller/cli-bridge.js +93 -93
  101. package/src/services/controller/container-index.js +50 -50
  102. package/src/services/controller/container-storage.js +36 -36
  103. package/src/services/controller/controller-actions.js +207 -207
  104. package/src/services/controller/controller.js +1138 -1138
  105. package/src/services/controller/selectors.js +54 -54
  106. package/src/services/controller/transport.js +125 -125
  107. package/src/utils/args.mjs +26 -26
  108. package/src/utils/browser-service.mjs +544 -544
  109. package/src/utils/command-log.mjs +64 -64
  110. package/src/utils/config.mjs +214 -214
  111. package/src/utils/fingerprint.mjs +181 -181
  112. package/src/utils/help.mjs +216 -216
  113. package/src/utils/js-policy.mjs +13 -13
  114. package/src/utils/ws-client.mjs +30 -30
@@ -1,46 +1,46 @@
1
- import { listProfiles, createProfile, deleteProfile, setDefaultProfile, getDefaultProfile, loadConfig, saveConfig } from '../utils/config.mjs';
2
-
3
- export async function handleProfileCommand(args) {
4
- const sub = args[1];
5
- const profileId = args[2];
6
-
7
- if (sub === 'list' || !sub) {
8
- const profiles = listProfiles();
9
- const defaultProfile = getDefaultProfile();
10
- console.log(JSON.stringify({ ok: true, profiles, defaultProfile, count: profiles.length }, null, 2));
11
- return;
12
- }
13
-
14
- if (sub === 'create') {
15
- if (!profileId) throw new Error('Usage: camo profile create <profileId>');
16
- createProfile(profileId);
17
- console.log(`Created profile: ${profileId}`);
18
- return;
19
- }
20
-
21
- if (sub === 'delete' || sub === 'remove') {
22
- if (!profileId) throw new Error('Usage: camo profile delete <profileId>');
23
- deleteProfile(profileId);
24
- const cfg = loadConfig();
25
- if (cfg.defaultProfile === profileId) {
26
- cfg.defaultProfile = null;
27
- saveConfig(cfg);
28
- }
29
- console.log(`Deleted profile: ${profileId}`);
30
- return;
31
- }
32
-
33
- if (sub === 'default') {
34
- if (!profileId) {
35
- console.log(JSON.stringify({ ok: true, defaultProfile: getDefaultProfile() }, null, 2));
36
- return;
37
- }
38
- const profiles = listProfiles();
39
- if (!profiles.includes(profileId)) throw new Error(`Profile not found: ${profileId}`);
40
- setDefaultProfile(profileId);
41
- console.log(`Default profile set to: ${profileId}`);
42
- return;
43
- }
44
-
45
- throw new Error('Usage: camo profile <list|create|delete|default> [profileId]');
46
- }
1
+ import { listProfiles, createProfile, deleteProfile, setDefaultProfile, getDefaultProfile, loadConfig, saveConfig } from '../utils/config.mjs';
2
+
3
+ export async function handleProfileCommand(args) {
4
+ const sub = args[1];
5
+ const profileId = args[2];
6
+
7
+ if (sub === 'list' || !sub) {
8
+ const profiles = listProfiles();
9
+ const defaultProfile = getDefaultProfile();
10
+ console.log(JSON.stringify({ ok: true, profiles, defaultProfile, count: profiles.length }, null, 2));
11
+ return;
12
+ }
13
+
14
+ if (sub === 'create') {
15
+ if (!profileId) throw new Error('Usage: camo profile create <profileId>');
16
+ createProfile(profileId);
17
+ console.log(`Created profile: ${profileId}`);
18
+ return;
19
+ }
20
+
21
+ if (sub === 'delete' || sub === 'remove') {
22
+ if (!profileId) throw new Error('Usage: camo profile delete <profileId>');
23
+ deleteProfile(profileId);
24
+ const cfg = loadConfig();
25
+ if (cfg.defaultProfile === profileId) {
26
+ cfg.defaultProfile = null;
27
+ saveConfig(cfg);
28
+ }
29
+ console.log(`Deleted profile: ${profileId}`);
30
+ return;
31
+ }
32
+
33
+ if (sub === 'default') {
34
+ if (!profileId) {
35
+ console.log(JSON.stringify({ ok: true, defaultProfile: getDefaultProfile() }, null, 2));
36
+ return;
37
+ }
38
+ const profiles = listProfiles();
39
+ if (!profiles.includes(profileId)) throw new Error(`Profile not found: ${profileId}`);
40
+ setDefaultProfile(profileId);
41
+ console.log(`Default profile set to: ${profileId}`);
42
+ return;
43
+ }
44
+
45
+ throw new Error('Usage: camo profile <list|create|delete|default> [profileId]');
46
+ }
@@ -1,115 +1,115 @@
1
- import path from 'node:path';
2
- import { getDefaultProfile } from '../utils/config.mjs';
3
- import { callAPI, ensureBrowserService } from '../utils/browser-service.mjs';
4
-
5
- function readFlagValue(args, names) {
6
- for (let i = 0; i < args.length; i += 1) {
7
- if (!names.includes(args[i])) continue;
8
- const value = args[i + 1];
9
- if (!value || String(value).startsWith('-')) return null;
10
- return value;
11
- }
12
- return null;
13
- }
14
-
15
- function collectPositionals(args, startIndex = 2) {
16
- const values = [];
17
- for (let i = startIndex; i < args.length; i += 1) {
18
- const token = args[i];
19
- if (!token || String(token).startsWith('-')) continue;
20
- const prev = args[i - 1];
21
- if (prev && ['--profile', '-p', '--name', '--output', '--reason'].includes(prev)) {
22
- continue;
23
- }
24
- values.push(String(token));
25
- }
26
- return values;
27
- }
28
-
29
- function resolveProfileId(args, startIndex = 2) {
30
- const explicit = readFlagValue(args, ['--profile', '-p']);
31
- if (explicit) return explicit;
32
- const positionals = collectPositionals(args, startIndex);
33
- if (positionals.length > 0) return positionals[0];
34
- return getDefaultProfile();
35
- }
36
-
37
- function parseOverlayFlag(args) {
38
- if (args.includes('--no-overlay')) return false;
39
- if (args.includes('--overlay')) return true;
40
- return null;
41
- }
42
-
43
- async function handleRecordStart(args) {
44
- await ensureBrowserService();
45
- const profileId = resolveProfileId(args, 2);
46
- if (!profileId) {
47
- throw new Error('Usage: camo record start [profileId] [--name <name>] [--output <file>] [--overlay|--no-overlay]');
48
- }
49
- if (args.includes('--name') && !readFlagValue(args, ['--name'])) {
50
- throw new Error('Usage: camo record start [profileId] --name <name>');
51
- }
52
- if (args.includes('--output') && !readFlagValue(args, ['--output'])) {
53
- throw new Error('Usage: camo record start [profileId] --output <file>');
54
- }
55
- const name = readFlagValue(args, ['--name']);
56
- const output = readFlagValue(args, ['--output']);
57
- const overlay = parseOverlayFlag(args);
58
- const result = await callAPI('record:start', {
59
- profileId,
60
- ...(name ? { name } : {}),
61
- ...(output ? { outputPath: path.resolve(output) } : {}),
62
- ...(overlay !== null ? { overlay } : {}),
63
- });
64
- console.log(JSON.stringify(result, null, 2));
65
- }
66
-
67
- async function handleRecordStop(args) {
68
- await ensureBrowserService();
69
- const profileId = resolveProfileId(args, 2);
70
- if (!profileId) {
71
- throw new Error('Usage: camo record stop [profileId]');
72
- }
73
- const reason = readFlagValue(args, ['--reason']) || 'manual';
74
- const result = await callAPI('record:stop', { profileId, reason });
75
- console.log(JSON.stringify(result, null, 2));
76
- }
77
-
78
- async function handleRecordStatus(args) {
79
- await ensureBrowserService();
80
- const profileId = resolveProfileId(args, 2);
81
- if (!profileId) {
82
- throw new Error('Usage: camo record status [profileId]');
83
- }
84
- const result = await callAPI('record:status', { profileId });
85
- console.log(JSON.stringify(result, null, 2));
86
- }
87
-
88
- export async function handleRecordCommand(args) {
89
- const sub = String(args[1] || '').trim().toLowerCase();
90
- if (!sub) {
91
- console.log(`Usage: camo record <start|stop|status> [options]
92
- camo record start [profileId] [--name <name>] [--output <file>] [--overlay|--no-overlay]
93
- camo record stop [profileId] [--reason <text>]
94
- camo record status [profileId]
95
- `);
96
- return;
97
- }
98
- switch (sub) {
99
- case 'start':
100
- await handleRecordStart(args);
101
- return;
102
- case 'stop':
103
- await handleRecordStop(args);
104
- return;
105
- case 'status':
106
- await handleRecordStatus(args);
107
- return;
108
- default:
109
- console.log(`Usage: camo record <start|stop|status> [options]
110
- camo record start [profileId] [--name <name>] [--output <file>] [--overlay|--no-overlay]
111
- camo record stop [profileId] [--reason <text>]
112
- camo record status [profileId]
113
- `);
114
- }
115
- }
1
+ import path from 'node:path';
2
+ import { getDefaultProfile } from '../utils/config.mjs';
3
+ import { callAPI, ensureBrowserService } from '../utils/browser-service.mjs';
4
+
5
+ function readFlagValue(args, names) {
6
+ for (let i = 0; i < args.length; i += 1) {
7
+ if (!names.includes(args[i])) continue;
8
+ const value = args[i + 1];
9
+ if (!value || String(value).startsWith('-')) return null;
10
+ return value;
11
+ }
12
+ return null;
13
+ }
14
+
15
+ function collectPositionals(args, startIndex = 2) {
16
+ const values = [];
17
+ for (let i = startIndex; i < args.length; i += 1) {
18
+ const token = args[i];
19
+ if (!token || String(token).startsWith('-')) continue;
20
+ const prev = args[i - 1];
21
+ if (prev && ['--profile', '-p', '--name', '--output', '--reason'].includes(prev)) {
22
+ continue;
23
+ }
24
+ values.push(String(token));
25
+ }
26
+ return values;
27
+ }
28
+
29
+ function resolveProfileId(args, startIndex = 2) {
30
+ const explicit = readFlagValue(args, ['--profile', '-p']);
31
+ if (explicit) return explicit;
32
+ const positionals = collectPositionals(args, startIndex);
33
+ if (positionals.length > 0) return positionals[0];
34
+ return getDefaultProfile();
35
+ }
36
+
37
+ function parseOverlayFlag(args) {
38
+ if (args.includes('--no-overlay')) return false;
39
+ if (args.includes('--overlay')) return true;
40
+ return null;
41
+ }
42
+
43
+ async function handleRecordStart(args) {
44
+ await ensureBrowserService();
45
+ const profileId = resolveProfileId(args, 2);
46
+ if (!profileId) {
47
+ throw new Error('Usage: camo record start [profileId] [--name <name>] [--output <file>] [--overlay|--no-overlay]');
48
+ }
49
+ if (args.includes('--name') && !readFlagValue(args, ['--name'])) {
50
+ throw new Error('Usage: camo record start [profileId] --name <name>');
51
+ }
52
+ if (args.includes('--output') && !readFlagValue(args, ['--output'])) {
53
+ throw new Error('Usage: camo record start [profileId] --output <file>');
54
+ }
55
+ const name = readFlagValue(args, ['--name']);
56
+ const output = readFlagValue(args, ['--output']);
57
+ const overlay = parseOverlayFlag(args);
58
+ const result = await callAPI('record:start', {
59
+ profileId,
60
+ ...(name ? { name } : {}),
61
+ ...(output ? { outputPath: path.resolve(output) } : {}),
62
+ ...(overlay !== null ? { overlay } : {}),
63
+ });
64
+ console.log(JSON.stringify(result, null, 2));
65
+ }
66
+
67
+ async function handleRecordStop(args) {
68
+ await ensureBrowserService();
69
+ const profileId = resolveProfileId(args, 2);
70
+ if (!profileId) {
71
+ throw new Error('Usage: camo record stop [profileId]');
72
+ }
73
+ const reason = readFlagValue(args, ['--reason']) || 'manual';
74
+ const result = await callAPI('record:stop', { profileId, reason });
75
+ console.log(JSON.stringify(result, null, 2));
76
+ }
77
+
78
+ async function handleRecordStatus(args) {
79
+ await ensureBrowserService();
80
+ const profileId = resolveProfileId(args, 2);
81
+ if (!profileId) {
82
+ throw new Error('Usage: camo record status [profileId]');
83
+ }
84
+ const result = await callAPI('record:status', { profileId });
85
+ console.log(JSON.stringify(result, null, 2));
86
+ }
87
+
88
+ export async function handleRecordCommand(args) {
89
+ const sub = String(args[1] || '').trim().toLowerCase();
90
+ if (!sub) {
91
+ console.log(`Usage: camo record <start|stop|status> [options]
92
+ camo record start [profileId] [--name <name>] [--output <file>] [--overlay|--no-overlay]
93
+ camo record stop [profileId] [--reason <text>]
94
+ camo record status [profileId]
95
+ `);
96
+ return;
97
+ }
98
+ switch (sub) {
99
+ case 'start':
100
+ await handleRecordStart(args);
101
+ return;
102
+ case 'stop':
103
+ await handleRecordStop(args);
104
+ return;
105
+ case 'status':
106
+ await handleRecordStatus(args);
107
+ return;
108
+ default:
109
+ console.log(`Usage: camo record <start|stop|status> [options]
110
+ camo record start [profileId] [--name <name>] [--output <file>] [--overlay|--no-overlay]
111
+ camo record stop [profileId] [--reason <text>]
112
+ camo record status [profileId]
113
+ `);
114
+ }
115
+ }
@@ -1,14 +1,14 @@
1
- import { callAPI, ensureBrowserService } from '../utils/browser-service.mjs';
2
-
3
- export async function handleSystemCommand(args) {
4
- await ensureBrowserService();
5
-
6
- const sub = args[1];
7
-
8
- if (sub === 'display') {
9
- const result = await callAPI('system:display', {});
10
- console.log(JSON.stringify(result, null, 2));
11
- } else {
12
- throw new Error('Usage: camo system display');
13
- }
14
- }
1
+ import { callAPI, ensureBrowserService } from '../utils/browser-service.mjs';
2
+
3
+ export async function handleSystemCommand(args) {
4
+ await ensureBrowserService();
5
+
6
+ const sub = args[1];
7
+
8
+ if (sub === 'display') {
9
+ const result = await callAPI('system:display', {});
10
+ console.log(JSON.stringify(result, null, 2));
11
+ } else {
12
+ throw new Error('Usage: camo system display');
13
+ }
14
+ }
@@ -1,123 +1,123 @@
1
- import { getDefaultProfile, setProfileWindowSize } from '../utils/config.mjs';
2
- import { callAPI, ensureBrowserService } from '../utils/browser-service.mjs';
3
- import { getPositionals } from '../utils/args.mjs';
4
-
5
- function sleep(ms) {
6
- return new Promise((resolve) => setTimeout(resolve, ms));
7
- }
8
-
9
- function computeTargetViewport(measured) {
10
- const innerWidth = Math.max(320, Number(measured?.innerWidth || 0) || 0);
11
- const innerHeight = Math.max(240, Number(measured?.innerHeight || 0) || 0);
12
- const outerWidth = Math.max(320, Number(measured?.outerWidth || 0) || innerWidth);
13
- const outerHeight = Math.max(240, Number(measured?.outerHeight || 0) || innerHeight);
14
-
15
- const rawDeltaW = Math.max(0, outerWidth - innerWidth);
16
- const rawDeltaH = Math.max(0, outerHeight - innerHeight);
17
- const frameW = rawDeltaW > 400 ? 16 : Math.min(rawDeltaW, 120);
18
- const frameH = rawDeltaH > 400 ? 88 : Math.min(rawDeltaH, 180);
19
-
20
- return {
21
- width: Math.max(320, outerWidth - frameW),
22
- height: Math.max(240, outerHeight - frameH),
23
- frameW,
24
- frameH,
25
- };
26
- }
27
-
28
- async function probeWindowMetrics(profileId) {
29
- const measured = await callAPI('evaluate', {
30
- profileId,
31
- script: '({ innerWidth: window.innerWidth, innerHeight: window.innerHeight, outerWidth: window.outerWidth, outerHeight: window.outerHeight })',
32
- });
33
- return measured?.result || {};
34
- }
35
-
36
- export async function handleWindowCommand(args) {
37
- await ensureBrowserService();
38
-
39
- const sub = args[1];
40
- const profileId = getPositionals(args, 2)[0] || getDefaultProfile();
41
- if (!profileId) throw new Error('No profile specified and no default profile set');
42
-
43
- if (sub === 'move') {
44
- const xIdx = args.indexOf('--x');
45
- const yIdx = args.indexOf('--y');
46
- if (xIdx === -1 || yIdx === -1) throw new Error('Usage: camo window move [profileId] --x <x> --y <y>');
47
- const x = parseInt(args[xIdx + 1]);
48
- const y = parseInt(args[yIdx + 1]);
49
- const result = await callAPI('window:move', { profileId, x, y });
50
- console.log(JSON.stringify(result, null, 2));
51
- } else if (sub === 'resize') {
52
- const widthIdx = args.indexOf('--width');
53
- const heightIdx = args.indexOf('--height');
54
- if (widthIdx === -1 || heightIdx === -1) throw new Error('Usage: camo window resize [profileId] --width <w> --height <h>');
55
- const width = parseInt(args[widthIdx + 1]);
56
- const height = parseInt(args[heightIdx + 1]);
57
- const result = await callAPI('window:resize', { profileId, width, height });
58
-
59
- const attempts = 4;
60
- const settleMs = 120;
61
- const tolerancePx = 3;
62
- let measured = {};
63
- let verified = {};
64
- let viewport = null;
65
- let targetViewportWidth = 1280;
66
- let targetViewportHeight = 720;
67
- let frameW = 16;
68
- let frameH = 88;
69
- let matched = false;
70
-
71
- await sleep(settleMs);
72
- for (let i = 0; i < attempts; i += 1) {
73
- measured = await probeWindowMetrics(profileId);
74
- const target = computeTargetViewport(measured);
75
- targetViewportWidth = target.width;
76
- targetViewportHeight = target.height;
77
- frameW = target.frameW;
78
- frameH = target.frameH;
79
-
80
- viewport = await callAPI('page:setViewport', {
81
- profileId,
82
- width: targetViewportWidth,
83
- height: targetViewportHeight,
84
- });
85
-
86
- await sleep(settleMs);
87
- verified = await probeWindowMetrics(profileId);
88
- const dw = Math.abs((Number(verified?.innerWidth || 0)) - targetViewportWidth);
89
- const dh = Math.abs((Number(verified?.innerHeight || 0)) - targetViewportHeight);
90
- if (dw <= tolerancePx && dh <= tolerancePx) {
91
- matched = true;
92
- break;
93
- }
94
- }
95
-
96
- const measuredOuterWidth = Number(verified?.outerWidth);
97
- const measuredOuterHeight = Number(verified?.outerHeight);
98
- const savedWindow = setProfileWindowSize(
99
- profileId,
100
- Number.isFinite(measuredOuterWidth) ? measuredOuterWidth : width,
101
- Number.isFinite(measuredOuterHeight) ? measuredOuterHeight : height,
102
- );
103
-
104
- console.log(JSON.stringify({
105
- ok: true,
106
- profileId,
107
- window: result,
108
- measured: measured || null,
109
- verified: verified || null,
110
- targetViewport: {
111
- width: targetViewportWidth,
112
- height: targetViewportHeight,
113
- frameW,
114
- frameH,
115
- matched,
116
- },
117
- profileWindow: savedWindow?.window || null,
118
- viewport,
119
- }, null, 2));
120
- } else {
121
- throw new Error('Usage: camo window <move|resize> [profileId] --x <x> --y <y> / --width <w> --height <h>');
122
- }
123
- }
1
+ import { getDefaultProfile, setProfileWindowSize } from '../utils/config.mjs';
2
+ import { callAPI, ensureBrowserService } from '../utils/browser-service.mjs';
3
+ import { getPositionals } from '../utils/args.mjs';
4
+
5
+ function sleep(ms) {
6
+ return new Promise((resolve) => setTimeout(resolve, ms));
7
+ }
8
+
9
+ function computeTargetViewport(measured) {
10
+ const innerWidth = Math.max(320, Number(measured?.innerWidth || 0) || 0);
11
+ const innerHeight = Math.max(240, Number(measured?.innerHeight || 0) || 0);
12
+ const outerWidth = Math.max(320, Number(measured?.outerWidth || 0) || innerWidth);
13
+ const outerHeight = Math.max(240, Number(measured?.outerHeight || 0) || innerHeight);
14
+
15
+ const rawDeltaW = Math.max(0, outerWidth - innerWidth);
16
+ const rawDeltaH = Math.max(0, outerHeight - innerHeight);
17
+ const frameW = rawDeltaW > 400 ? 16 : Math.min(rawDeltaW, 120);
18
+ const frameH = rawDeltaH > 400 ? 88 : Math.min(rawDeltaH, 180);
19
+
20
+ return {
21
+ width: Math.max(320, outerWidth - frameW),
22
+ height: Math.max(240, outerHeight - frameH),
23
+ frameW,
24
+ frameH,
25
+ };
26
+ }
27
+
28
+ async function probeWindowMetrics(profileId) {
29
+ const measured = await callAPI('evaluate', {
30
+ profileId,
31
+ script: '({ innerWidth: window.innerWidth, innerHeight: window.innerHeight, outerWidth: window.outerWidth, outerHeight: window.outerHeight })',
32
+ });
33
+ return measured?.result || {};
34
+ }
35
+
36
+ export async function handleWindowCommand(args) {
37
+ await ensureBrowserService();
38
+
39
+ const sub = args[1];
40
+ const profileId = getPositionals(args, 2)[0] || getDefaultProfile();
41
+ if (!profileId) throw new Error('No profile specified and no default profile set');
42
+
43
+ if (sub === 'move') {
44
+ const xIdx = args.indexOf('--x');
45
+ const yIdx = args.indexOf('--y');
46
+ if (xIdx === -1 || yIdx === -1) throw new Error('Usage: camo window move [profileId] --x <x> --y <y>');
47
+ const x = parseInt(args[xIdx + 1]);
48
+ const y = parseInt(args[yIdx + 1]);
49
+ const result = await callAPI('window:move', { profileId, x, y });
50
+ console.log(JSON.stringify(result, null, 2));
51
+ } else if (sub === 'resize') {
52
+ const widthIdx = args.indexOf('--width');
53
+ const heightIdx = args.indexOf('--height');
54
+ if (widthIdx === -1 || heightIdx === -1) throw new Error('Usage: camo window resize [profileId] --width <w> --height <h>');
55
+ const width = parseInt(args[widthIdx + 1]);
56
+ const height = parseInt(args[heightIdx + 1]);
57
+ const result = await callAPI('window:resize', { profileId, width, height });
58
+
59
+ const attempts = 4;
60
+ const settleMs = 120;
61
+ const tolerancePx = 3;
62
+ let measured = {};
63
+ let verified = {};
64
+ let viewport = null;
65
+ let targetViewportWidth = 1280;
66
+ let targetViewportHeight = 720;
67
+ let frameW = 16;
68
+ let frameH = 88;
69
+ let matched = false;
70
+
71
+ await sleep(settleMs);
72
+ for (let i = 0; i < attempts; i += 1) {
73
+ measured = await probeWindowMetrics(profileId);
74
+ const target = computeTargetViewport(measured);
75
+ targetViewportWidth = target.width;
76
+ targetViewportHeight = target.height;
77
+ frameW = target.frameW;
78
+ frameH = target.frameH;
79
+
80
+ viewport = await callAPI('page:setViewport', {
81
+ profileId,
82
+ width: targetViewportWidth,
83
+ height: targetViewportHeight,
84
+ });
85
+
86
+ await sleep(settleMs);
87
+ verified = await probeWindowMetrics(profileId);
88
+ const dw = Math.abs((Number(verified?.innerWidth || 0)) - targetViewportWidth);
89
+ const dh = Math.abs((Number(verified?.innerHeight || 0)) - targetViewportHeight);
90
+ if (dw <= tolerancePx && dh <= tolerancePx) {
91
+ matched = true;
92
+ break;
93
+ }
94
+ }
95
+
96
+ const measuredOuterWidth = Number(verified?.outerWidth);
97
+ const measuredOuterHeight = Number(verified?.outerHeight);
98
+ const savedWindow = setProfileWindowSize(
99
+ profileId,
100
+ Number.isFinite(measuredOuterWidth) ? measuredOuterWidth : width,
101
+ Number.isFinite(measuredOuterHeight) ? measuredOuterHeight : height,
102
+ );
103
+
104
+ console.log(JSON.stringify({
105
+ ok: true,
106
+ profileId,
107
+ window: result,
108
+ measured: measured || null,
109
+ verified: verified || null,
110
+ targetViewport: {
111
+ width: targetViewportWidth,
112
+ height: targetViewportHeight,
113
+ frameW,
114
+ frameH,
115
+ matched,
116
+ },
117
+ profileWindow: savedWindow?.window || null,
118
+ viewport,
119
+ }, null, 2));
120
+ } else {
121
+ throw new Error('Usage: camo window <move|resize> [profileId] --x <x> --y <y> / --width <w> --height <h>');
122
+ }
123
+ }