@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,275 +1,275 @@
1
- #!/usr/bin/env node
2
- /**
3
- * Lifecycle commands: cleanup, force-stop, lock management, session recovery
4
- */
5
- import { getDefaultProfile } from '../utils/config.mjs';
6
- import { callAPI, ensureBrowserService, checkBrowserService, getResolvedSessions } from '../utils/browser-service.mjs';
7
- import { resolveProfileId } from '../utils/args.mjs';
8
- import { acquireLock, getLockInfo, releaseLock, cleanupStaleLocks, listActiveLocks } from '../lifecycle/lock.mjs';
9
- import {
10
- getSessionInfo, unregisterSession, markSessionClosed, cleanupStaleSessions,
11
- listRegisteredSessions, updateSession
12
- } from '../lifecycle/session-registry.mjs';
13
- import { stopAllSessionWatchdogs, stopSessionWatchdog } from '../lifecycle/session-watchdog.mjs';
14
-
15
- function rejectIndirectSessionTargeting(commandName, args) {
16
- if (args.includes('--id')) {
17
- throw new Error(`Usage: camo ${commandName} [profileId] (direct profile only; --id is only supported by "camo stop")`);
18
- }
19
- if (args.includes('--alias')) {
20
- throw new Error(`Usage: camo ${commandName} [profileId] (direct profile only; --alias is only supported by "camo stop")`);
21
- }
22
- }
23
-
24
- export async function handleCleanupCommand(args) {
25
- rejectIndirectSessionTargeting('cleanup', args);
26
- const sub = args[1];
27
-
28
- if (sub === 'locks') {
29
- const cleaned = cleanupStaleLocks();
30
- console.log(JSON.stringify({ ok: true, cleanedStaleLocks: cleaned }, null, 2));
31
- return;
32
- }
33
-
34
- if (sub === 'sessions') {
35
- const cleaned = cleanupStaleSessions();
36
- console.log(JSON.stringify({ ok: true, cleanedStaleSessions: cleaned }, null, 2));
37
- return;
38
- }
39
-
40
- if (sub === 'all') {
41
- const serviceUp = await checkBrowserService();
42
- const results = [];
43
-
44
- if (serviceUp) {
45
- try {
46
- const status = await callAPI('getStatus', {});
47
- const sessions = Array.isArray(status?.sessions) ? status.sessions : [];
48
-
49
- for (const session of sessions) {
50
- let stopError = null;
51
- try {
52
- await callAPI('stop', { profileId: session.profileId });
53
- } catch (err) {
54
- stopError = err;
55
- } finally {
56
- stopSessionWatchdog(session.profileId);
57
- releaseLock(session.profileId);
58
- markSessionClosed(session.profileId);
59
- }
60
- results.push(
61
- stopError
62
- ? { profileId: session.profileId, ok: false, error: stopError.message }
63
- : { profileId: session.profileId, ok: true },
64
- );
65
- }
66
- } catch {}
67
- }
68
-
69
- // Cleanup stale locks and sessions
70
- const cleanedLocks = cleanupStaleLocks();
71
- const cleanedSessions = cleanupStaleSessions();
72
- stopAllSessionWatchdogs();
73
-
74
- console.log(JSON.stringify({
75
- ok: true,
76
- sessions: results,
77
- cleanedStaleLocks: cleanedLocks,
78
- cleanedStaleSessions: cleanedSessions,
79
- }, null, 2));
80
- return;
81
- }
82
-
83
- // Clean specific profile
84
- const profileId = resolveProfileId(args, 1, getDefaultProfile);
85
- if (!profileId) {
86
- throw new Error('Usage: camo cleanup [profileId] | camo cleanup all | camo cleanup locks | camo cleanup sessions');
87
- }
88
-
89
- const serviceUp = await checkBrowserService();
90
- if (serviceUp) {
91
- try {
92
- await callAPI('stop', { profileId });
93
- } catch {}
94
- }
95
-
96
- stopSessionWatchdog(profileId);
97
- releaseLock(profileId);
98
- markSessionClosed(profileId);
99
- console.log(JSON.stringify({ ok: true, profileId }, null, 2));
100
- }
101
-
102
- export async function handleForceStopCommand(args) {
103
- rejectIndirectSessionTargeting('force-stop', args);
104
- await ensureBrowserService();
105
- const profileId = resolveProfileId(args, 1, getDefaultProfile);
106
- if (!profileId) throw new Error('Usage: camo force-stop [profileId]');
107
-
108
- try {
109
- const result = await callAPI('stop', { profileId, force: true });
110
- stopSessionWatchdog(profileId);
111
- releaseLock(profileId);
112
- markSessionClosed(profileId);
113
- console.log(JSON.stringify({ ok: true, profileId, ...result }, null, 2));
114
- } catch (err) {
115
- // Even if stop fails, cleanup local state
116
- stopSessionWatchdog(profileId);
117
- releaseLock(profileId);
118
- markSessionClosed(profileId);
119
- console.log(JSON.stringify({ ok: true, profileId, warning: 'Session stopped locally but remote stop failed: ' + err.message }, null, 2));
120
- }
121
- }
122
-
123
- export async function handleLockCommand(args) {
124
- const sub = args[1];
125
-
126
- if (sub === 'list') {
127
- const locks = listActiveLocks();
128
- console.log(JSON.stringify({ ok: true, locks, count: locks.length }, null, 2));
129
- return;
130
- }
131
-
132
- if (sub === 'cleanup') {
133
- const cleaned = cleanupStaleLocks();
134
- console.log(JSON.stringify({ ok: true, cleanedStaleLocks: cleaned }, null, 2));
135
- return;
136
- }
137
-
138
- const profileId = resolveProfileId(args, 1, getDefaultProfile);
139
- if (!profileId) {
140
- throw new Error('Usage: camo lock list | camo lock cleanup | camo lock [profileId]');
141
- }
142
-
143
- const info = getLockInfo(profileId);
144
- if (!info) {
145
- console.log(JSON.stringify({ ok: true, locked: false, profileId }, null, 2));
146
- return;
147
- }
148
-
149
- console.log(JSON.stringify({ ok: true, locked: true, ...info }, null, 2));
150
- }
151
-
152
- export async function handleUnlockCommand(args) {
153
- const profileId = resolveProfileId(args, 1, getDefaultProfile);
154
- if (!profileId) throw new Error('Usage: camo unlock [profileId]');
155
-
156
- const released = releaseLock(profileId);
157
- console.log(JSON.stringify({ ok: released, profileId, released }, null, 2));
158
- }
159
-
160
- export async function handleSessionsCommand(args) {
161
- const serviceUp = await checkBrowserService();
162
- const registeredSessions = listRegisteredSessions();
163
- let merged = [];
164
- if (serviceUp) {
165
- try {
166
- merged = await getResolvedSessions();
167
- } catch {
168
- merged = [];
169
- }
170
- } else {
171
- merged = registeredSessions
172
- .filter((item) => String(item?.status || '').trim() !== 'closed')
173
- .map((item) => ({
174
- profileId: item.profileId,
175
- sessionId: item.sessionId || item.profileId,
176
- instanceId: item.instanceId || item.sessionId || item.profileId,
177
- alias: item.alias || null,
178
- url: item.url || null,
179
- mode: item.mode || null,
180
- ownerPid: Number(item?.ownerPid || item?.pid || 0) || null,
181
- headless: item.headless === true,
182
- idleTimeoutMs: Number(item.idleTimeoutMs || 0) || 0,
183
- idleMs: 0,
184
- idle: false,
185
- live: false,
186
- registered: true,
187
- orphaned: true,
188
- needsRecovery: String(item.status || '').trim() === 'active',
189
- registryStatus: item.status || null,
190
- lastSeen: item.lastSeen || null,
191
- lastActivityAt: item.lastActivityAt || null,
192
- }));
193
- }
194
-
195
- console.log(JSON.stringify({
196
- ok: true,
197
- serviceUp,
198
- sessions: merged,
199
- count: merged.length,
200
- registered: registeredSessions.length,
201
- live: merged.filter((item) => item.live).length,
202
- orphaned: merged.filter(s => s.orphaned).length,
203
- }, null, 2));
204
- }
205
-
206
- export async function handleInstancesCommand(args) {
207
- await handleSessionsCommand(args);
208
- }
209
-
210
- export async function handleRecoverCommand(args) {
211
- const profileId = resolveProfileId(args, 1, getDefaultProfile);
212
- if (!profileId) throw new Error('Usage: camo recover [profileId]');
213
-
214
- const reg = getSessionInfo(profileId);
215
- if (!reg) {
216
- console.log(JSON.stringify({ ok: false, error: 'No registered session found for profile', profileId }, null, 2));
217
- return;
218
- }
219
-
220
- const serviceUp = await checkBrowserService();
221
-
222
- if (!serviceUp) {
223
- // Service is down - session cannot be recovered, clean up
224
- stopSessionWatchdog(profileId);
225
- unregisterSession(profileId);
226
- releaseLock(profileId);
227
- console.log(JSON.stringify({
228
- ok: true,
229
- recovered: false,
230
- reason: 'browser_service_down',
231
- profileId,
232
- message: 'Browser service is down. Session cleaned up. Restart with: camo start ' + profileId,
233
- }, null, 2));
234
- return;
235
- }
236
-
237
- // Service is up - check if session is still there
238
- try {
239
- const sessions = await getResolvedSessions();
240
- const existing = sessions.find((item) => item.profileId === profileId && item.live);
241
-
242
- if (existing) {
243
- // Session is alive - update registry
244
- updateSession(profileId, {
245
- sessionId: existing.sessionId || existing.profileId,
246
- url: existing.url,
247
- status: 'active',
248
- recoveredAt: Date.now(),
249
- });
250
- acquireLock(profileId, { sessionId: existing.sessionId || existing.profileId });
251
- console.log(JSON.stringify({
252
- ok: true,
253
- recovered: true,
254
- profileId,
255
- sessionId: existing.sessionId || existing.profileId,
256
- url: existing.url,
257
- message: 'Session reconnected successfully',
258
- }, null, 2));
259
- } else {
260
- // Session not in browser service - clean up
261
- stopSessionWatchdog(profileId);
262
- unregisterSession(profileId);
263
- releaseLock(profileId);
264
- console.log(JSON.stringify({
265
- ok: true,
266
- recovered: false,
267
- reason: 'session_not_found',
268
- profileId,
269
- message: 'Session not found in browser service. Cleaned up. Restart with: camo start ' + profileId,
270
- }, null, 2));
271
- }
272
- } catch (err) {
273
- console.log(JSON.stringify({ ok: false, error: err.message, profileId }, null, 2));
274
- }
275
- }
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Lifecycle commands: cleanup, force-stop, lock management, session recovery
4
+ */
5
+ import { getDefaultProfile } from '../utils/config.mjs';
6
+ import { callAPI, ensureBrowserService, checkBrowserService, getResolvedSessions } from '../utils/browser-service.mjs';
7
+ import { resolveProfileId } from '../utils/args.mjs';
8
+ import { acquireLock, getLockInfo, releaseLock, cleanupStaleLocks, listActiveLocks } from '../lifecycle/lock.mjs';
9
+ import {
10
+ getSessionInfo, unregisterSession, markSessionClosed, cleanupStaleSessions,
11
+ listRegisteredSessions, updateSession
12
+ } from '../lifecycle/session-registry.mjs';
13
+ import { stopAllSessionWatchdogs, stopSessionWatchdog } from '../lifecycle/session-watchdog.mjs';
14
+
15
+ function rejectIndirectSessionTargeting(commandName, args) {
16
+ if (args.includes('--id')) {
17
+ throw new Error(`Usage: camo ${commandName} [profileId] (direct profile only; --id is only supported by "camo stop")`);
18
+ }
19
+ if (args.includes('--alias')) {
20
+ throw new Error(`Usage: camo ${commandName} [profileId] (direct profile only; --alias is only supported by "camo stop")`);
21
+ }
22
+ }
23
+
24
+ export async function handleCleanupCommand(args) {
25
+ rejectIndirectSessionTargeting('cleanup', args);
26
+ const sub = args[1];
27
+
28
+ if (sub === 'locks') {
29
+ const cleaned = cleanupStaleLocks();
30
+ console.log(JSON.stringify({ ok: true, cleanedStaleLocks: cleaned }, null, 2));
31
+ return;
32
+ }
33
+
34
+ if (sub === 'sessions') {
35
+ const cleaned = cleanupStaleSessions();
36
+ console.log(JSON.stringify({ ok: true, cleanedStaleSessions: cleaned }, null, 2));
37
+ return;
38
+ }
39
+
40
+ if (sub === 'all') {
41
+ const serviceUp = await checkBrowserService();
42
+ const results = [];
43
+
44
+ if (serviceUp) {
45
+ try {
46
+ const status = await callAPI('getStatus', {});
47
+ const sessions = Array.isArray(status?.sessions) ? status.sessions : [];
48
+
49
+ for (const session of sessions) {
50
+ let stopError = null;
51
+ try {
52
+ await callAPI('stop', { profileId: session.profileId });
53
+ } catch (err) {
54
+ stopError = err;
55
+ } finally {
56
+ stopSessionWatchdog(session.profileId);
57
+ releaseLock(session.profileId);
58
+ markSessionClosed(session.profileId);
59
+ }
60
+ results.push(
61
+ stopError
62
+ ? { profileId: session.profileId, ok: false, error: stopError.message }
63
+ : { profileId: session.profileId, ok: true },
64
+ );
65
+ }
66
+ } catch {}
67
+ }
68
+
69
+ // Cleanup stale locks and sessions
70
+ const cleanedLocks = cleanupStaleLocks();
71
+ const cleanedSessions = cleanupStaleSessions();
72
+ stopAllSessionWatchdogs();
73
+
74
+ console.log(JSON.stringify({
75
+ ok: true,
76
+ sessions: results,
77
+ cleanedStaleLocks: cleanedLocks,
78
+ cleanedStaleSessions: cleanedSessions,
79
+ }, null, 2));
80
+ return;
81
+ }
82
+
83
+ // Clean specific profile
84
+ const profileId = resolveProfileId(args, 1, getDefaultProfile);
85
+ if (!profileId) {
86
+ throw new Error('Usage: camo cleanup [profileId] | camo cleanup all | camo cleanup locks | camo cleanup sessions');
87
+ }
88
+
89
+ const serviceUp = await checkBrowserService();
90
+ if (serviceUp) {
91
+ try {
92
+ await callAPI('stop', { profileId });
93
+ } catch {}
94
+ }
95
+
96
+ stopSessionWatchdog(profileId);
97
+ releaseLock(profileId);
98
+ markSessionClosed(profileId);
99
+ console.log(JSON.stringify({ ok: true, profileId }, null, 2));
100
+ }
101
+
102
+ export async function handleForceStopCommand(args) {
103
+ rejectIndirectSessionTargeting('force-stop', args);
104
+ await ensureBrowserService();
105
+ const profileId = resolveProfileId(args, 1, getDefaultProfile);
106
+ if (!profileId) throw new Error('Usage: camo force-stop [profileId]');
107
+
108
+ try {
109
+ const result = await callAPI('stop', { profileId, force: true });
110
+ stopSessionWatchdog(profileId);
111
+ releaseLock(profileId);
112
+ markSessionClosed(profileId);
113
+ console.log(JSON.stringify({ ok: true, profileId, ...result }, null, 2));
114
+ } catch (err) {
115
+ // Even if stop fails, cleanup local state
116
+ stopSessionWatchdog(profileId);
117
+ releaseLock(profileId);
118
+ markSessionClosed(profileId);
119
+ console.log(JSON.stringify({ ok: true, profileId, warning: 'Session stopped locally but remote stop failed: ' + err.message }, null, 2));
120
+ }
121
+ }
122
+
123
+ export async function handleLockCommand(args) {
124
+ const sub = args[1];
125
+
126
+ if (sub === 'list') {
127
+ const locks = listActiveLocks();
128
+ console.log(JSON.stringify({ ok: true, locks, count: locks.length }, null, 2));
129
+ return;
130
+ }
131
+
132
+ if (sub === 'cleanup') {
133
+ const cleaned = cleanupStaleLocks();
134
+ console.log(JSON.stringify({ ok: true, cleanedStaleLocks: cleaned }, null, 2));
135
+ return;
136
+ }
137
+
138
+ const profileId = resolveProfileId(args, 1, getDefaultProfile);
139
+ if (!profileId) {
140
+ throw new Error('Usage: camo lock list | camo lock cleanup | camo lock [profileId]');
141
+ }
142
+
143
+ const info = getLockInfo(profileId);
144
+ if (!info) {
145
+ console.log(JSON.stringify({ ok: true, locked: false, profileId }, null, 2));
146
+ return;
147
+ }
148
+
149
+ console.log(JSON.stringify({ ok: true, locked: true, ...info }, null, 2));
150
+ }
151
+
152
+ export async function handleUnlockCommand(args) {
153
+ const profileId = resolveProfileId(args, 1, getDefaultProfile);
154
+ if (!profileId) throw new Error('Usage: camo unlock [profileId]');
155
+
156
+ const released = releaseLock(profileId);
157
+ console.log(JSON.stringify({ ok: released, profileId, released }, null, 2));
158
+ }
159
+
160
+ export async function handleSessionsCommand(args) {
161
+ const serviceUp = await checkBrowserService();
162
+ const registeredSessions = listRegisteredSessions();
163
+ let merged = [];
164
+ if (serviceUp) {
165
+ try {
166
+ merged = await getResolvedSessions();
167
+ } catch {
168
+ merged = [];
169
+ }
170
+ } else {
171
+ merged = registeredSessions
172
+ .filter((item) => String(item?.status || '').trim() !== 'closed')
173
+ .map((item) => ({
174
+ profileId: item.profileId,
175
+ sessionId: item.sessionId || item.profileId,
176
+ instanceId: item.instanceId || item.sessionId || item.profileId,
177
+ alias: item.alias || null,
178
+ url: item.url || null,
179
+ mode: item.mode || null,
180
+ ownerPid: Number(item?.ownerPid || item?.pid || 0) || null,
181
+ headless: item.headless === true,
182
+ idleTimeoutMs: Number(item.idleTimeoutMs || 0) || 0,
183
+ idleMs: 0,
184
+ idle: false,
185
+ live: false,
186
+ registered: true,
187
+ orphaned: true,
188
+ needsRecovery: String(item.status || '').trim() === 'active',
189
+ registryStatus: item.status || null,
190
+ lastSeen: item.lastSeen || null,
191
+ lastActivityAt: item.lastActivityAt || null,
192
+ }));
193
+ }
194
+
195
+ console.log(JSON.stringify({
196
+ ok: true,
197
+ serviceUp,
198
+ sessions: merged,
199
+ count: merged.length,
200
+ registered: registeredSessions.length,
201
+ live: merged.filter((item) => item.live).length,
202
+ orphaned: merged.filter(s => s.orphaned).length,
203
+ }, null, 2));
204
+ }
205
+
206
+ export async function handleInstancesCommand(args) {
207
+ await handleSessionsCommand(args);
208
+ }
209
+
210
+ export async function handleRecoverCommand(args) {
211
+ const profileId = resolveProfileId(args, 1, getDefaultProfile);
212
+ if (!profileId) throw new Error('Usage: camo recover [profileId]');
213
+
214
+ const reg = getSessionInfo(profileId);
215
+ if (!reg) {
216
+ console.log(JSON.stringify({ ok: false, error: 'No registered session found for profile', profileId }, null, 2));
217
+ return;
218
+ }
219
+
220
+ const serviceUp = await checkBrowserService();
221
+
222
+ if (!serviceUp) {
223
+ // Service is down - session cannot be recovered, clean up
224
+ stopSessionWatchdog(profileId);
225
+ unregisterSession(profileId);
226
+ releaseLock(profileId);
227
+ console.log(JSON.stringify({
228
+ ok: true,
229
+ recovered: false,
230
+ reason: 'browser_service_down',
231
+ profileId,
232
+ message: 'Browser service is down. Session cleaned up. Restart with: camo start ' + profileId,
233
+ }, null, 2));
234
+ return;
235
+ }
236
+
237
+ // Service is up - check if session is still there
238
+ try {
239
+ const sessions = await getResolvedSessions();
240
+ const existing = sessions.find((item) => item.profileId === profileId && item.live);
241
+
242
+ if (existing) {
243
+ // Session is alive - update registry
244
+ updateSession(profileId, {
245
+ sessionId: existing.sessionId || existing.profileId,
246
+ url: existing.url,
247
+ status: 'active',
248
+ recoveredAt: Date.now(),
249
+ });
250
+ acquireLock(profileId, { sessionId: existing.sessionId || existing.profileId });
251
+ console.log(JSON.stringify({
252
+ ok: true,
253
+ recovered: true,
254
+ profileId,
255
+ sessionId: existing.sessionId || existing.profileId,
256
+ url: existing.url,
257
+ message: 'Session reconnected successfully',
258
+ }, null, 2));
259
+ } else {
260
+ // Session not in browser service - clean up
261
+ stopSessionWatchdog(profileId);
262
+ unregisterSession(profileId);
263
+ releaseLock(profileId);
264
+ console.log(JSON.stringify({
265
+ ok: true,
266
+ recovered: false,
267
+ reason: 'session_not_found',
268
+ profileId,
269
+ message: 'Session not found in browser service. Cleaned up. Restart with: camo start ' + profileId,
270
+ }, null, 2));
271
+ }
272
+ } catch (err) {
273
+ console.log(JSON.stringify({ ok: false, error: err.message, profileId }, null, 2));
274
+ }
275
+ }
@@ -1,45 +1,45 @@
1
- import { getDefaultProfile } from '../utils/config.mjs';
2
- import { callAPI, ensureBrowserService } from '../utils/browser-service.mjs';
3
- import { getPositionals } from '../utils/args.mjs';
4
-
5
- const INPUT_ACTION_TIMEOUT_MS = (() => {
6
- const parsed = Number(process.env.CAMO_INPUT_ACTION_TIMEOUT_MS);
7
- if (!Number.isFinite(parsed) || parsed <= 0) return 30000;
8
- return Math.max(1000, Math.floor(parsed));
9
- })();
10
-
11
- export async function handleMouseCommand(args) {
12
- await ensureBrowserService();
13
-
14
- const sub = args[1];
15
- const profileId = getPositionals(args, 2)[0] || getDefaultProfile();
16
- if (!profileId) throw new Error('No profile specified and no default profile set');
17
-
18
- if (sub === 'click') {
19
- // Use existing click command? We already have click command for element clicking.
20
- // This is for raw mouse click at coordinates.
21
- const xIdx = args.indexOf('--x');
22
- const yIdx = args.indexOf('--y');
23
- const buttonIdx = args.indexOf('--button');
24
- const clicksIdx = args.indexOf('--clicks');
25
- const delayIdx = args.indexOf('--delay');
26
- if (xIdx === -1 || yIdx === -1) throw new Error('Usage: camo mouse click [profileId] --x <x> --y <y> [--button left|right|middle] [--clicks <n>] [--delay <ms>]');
27
- const x = parseInt(args[xIdx + 1]);
28
- const y = parseInt(args[yIdx + 1]);
29
- const button = buttonIdx >= 0 ? args[buttonIdx + 1] : 'left';
30
- const clicks = clicksIdx >= 0 ? parseInt(args[clicksIdx + 1]) : 1;
31
- const delay = delayIdx >= 0 ? parseInt(args[delayIdx + 1]) : undefined;
32
- const result = await callAPI('mouse:click', { profileId, x, y, button, clicks, delay }, { timeoutMs: INPUT_ACTION_TIMEOUT_MS });
33
- console.log(JSON.stringify(result, null, 2));
34
- } else if (sub === 'wheel') {
35
- const deltaXIdx = args.indexOf('--deltax');
36
- const deltaYIdx = args.indexOf('--deltay');
37
- if (deltaXIdx === -1 && deltaYIdx === -1) throw new Error('Usage: camo mouse wheel [profileId] [--deltax <px>] [--deltay <px>]');
38
- const deltaX = deltaXIdx >= 0 ? parseInt(args[deltaXIdx + 1]) : 0;
39
- const deltaY = deltaYIdx >= 0 ? parseInt(args[deltaYIdx + 1]) : 0;
40
- const result = await callAPI('mouse:wheel', { profileId, deltaX, deltaY }, { timeoutMs: INPUT_ACTION_TIMEOUT_MS });
41
- console.log(JSON.stringify(result, null, 2));
42
- } else {
43
- throw new Error('Usage: camo mouse <click|wheel> [profileId] [options]');
44
- }
45
- }
1
+ import { getDefaultProfile } from '../utils/config.mjs';
2
+ import { callAPI, ensureBrowserService } from '../utils/browser-service.mjs';
3
+ import { getPositionals } from '../utils/args.mjs';
4
+
5
+ const INPUT_ACTION_TIMEOUT_MS = (() => {
6
+ const parsed = Number(process.env.CAMO_INPUT_ACTION_TIMEOUT_MS);
7
+ if (!Number.isFinite(parsed) || parsed <= 0) return 30000;
8
+ return Math.max(1000, Math.floor(parsed));
9
+ })();
10
+
11
+ export async function handleMouseCommand(args) {
12
+ await ensureBrowserService();
13
+
14
+ const sub = args[1];
15
+ const profileId = getPositionals(args, 2)[0] || getDefaultProfile();
16
+ if (!profileId) throw new Error('No profile specified and no default profile set');
17
+
18
+ if (sub === 'click') {
19
+ // Use existing click command? We already have click command for element clicking.
20
+ // This is for raw mouse click at coordinates.
21
+ const xIdx = args.indexOf('--x');
22
+ const yIdx = args.indexOf('--y');
23
+ const buttonIdx = args.indexOf('--button');
24
+ const clicksIdx = args.indexOf('--clicks');
25
+ const delayIdx = args.indexOf('--delay');
26
+ if (xIdx === -1 || yIdx === -1) throw new Error('Usage: camo mouse click [profileId] --x <x> --y <y> [--button left|right|middle] [--clicks <n>] [--delay <ms>]');
27
+ const x = parseInt(args[xIdx + 1]);
28
+ const y = parseInt(args[yIdx + 1]);
29
+ const button = buttonIdx >= 0 ? args[buttonIdx + 1] : 'left';
30
+ const clicks = clicksIdx >= 0 ? parseInt(args[clicksIdx + 1]) : 1;
31
+ const delay = delayIdx >= 0 ? parseInt(args[delayIdx + 1]) : undefined;
32
+ const result = await callAPI('mouse:click', { profileId, x, y, button, clicks, delay }, { timeoutMs: INPUT_ACTION_TIMEOUT_MS });
33
+ console.log(JSON.stringify(result, null, 2));
34
+ } else if (sub === 'wheel') {
35
+ const deltaXIdx = args.indexOf('--deltax');
36
+ const deltaYIdx = args.indexOf('--deltay');
37
+ if (deltaXIdx === -1 && deltaYIdx === -1) throw new Error('Usage: camo mouse wheel [profileId] [--deltax <px>] [--deltay <px>]');
38
+ const deltaX = deltaXIdx >= 0 ? parseInt(args[deltaXIdx + 1]) : 0;
39
+ const deltaY = deltaYIdx >= 0 ? parseInt(args[deltaYIdx + 1]) : 0;
40
+ const result = await callAPI('mouse:wheel', { profileId, deltaX, deltaY }, { timeoutMs: INPUT_ACTION_TIMEOUT_MS });
41
+ console.log(JSON.stringify(result, null, 2));
42
+ } else {
43
+ throw new Error('Usage: camo mouse <click|wheel> [profileId] [options]');
44
+ }
45
+ }