@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,199 +1,199 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import { resolveRecordsRoot } from '../storage-paths.js';
4
- export class BrowserSessionRecording {
5
- profileId;
6
- getCurrentUrl;
7
- getContext;
8
- recordingStream = null;
9
- recording = {
10
- active: false,
11
- enabled: false,
12
- name: null,
13
- outputPath: null,
14
- overlay: false,
15
- startedAt: null,
16
- endedAt: null,
17
- eventCount: 0,
18
- lastEventAt: null,
19
- lastError: null,
20
- };
21
- bindRecorderBridge = () => { };
22
- installRecorderRuntime = async () => { };
23
- constructor(profileId, getCurrentUrl, getContext) {
24
- this.profileId = profileId;
25
- this.getCurrentUrl = getCurrentUrl;
26
- this.getContext = getContext;
27
- }
28
- setBindRecorderBridge(fn) {
29
- this.bindRecorderBridge = fn;
30
- }
31
- setInstallRecorderRuntime(fn) {
32
- this.installRecorderRuntime = fn;
33
- }
34
- getRecordingStatus() {
35
- return { ...this.recording };
36
- }
37
- normalizeRecordingName(raw) {
38
- const text = String(raw || '').trim();
39
- const fallback = `record-${this.profileId}`;
40
- if (!text)
41
- return fallback;
42
- return text.replace(/[^a-zA-Z0-9._-]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 120) || fallback;
43
- }
44
- buildRecordingFilename(name) {
45
- const stamp = new Date().toISOString().replace(/[:.]/g, '-');
46
- return `${stamp}-${name}.jsonl`;
47
- }
48
- resolveRecordingOutputPath(options) {
49
- const name = this.normalizeRecordingName(options?.name);
50
- const rawOutput = String(options?.outputPath || '').trim();
51
- if (!rawOutput) {
52
- const root = path.join(resolveRecordsRoot(), this.profileId);
53
- return path.join(root, this.buildRecordingFilename(name));
54
- }
55
- const absolute = path.isAbsolute(rawOutput) ? rawOutput : path.resolve(rawOutput);
56
- if (absolute.endsWith(path.sep)) {
57
- return path.join(absolute, this.buildRecordingFilename(name));
58
- }
59
- if (fs.existsSync(absolute) && fs.statSync(absolute).isDirectory()) {
60
- return path.join(absolute, this.buildRecordingFilename(name));
61
- }
62
- return absolute;
63
- }
64
- async startRecording(options = {}) {
65
- const outputPath = this.resolveRecordingOutputPath(options);
66
- const name = this.normalizeRecordingName(options?.name);
67
- const overlay = options?.overlay !== false;
68
- if (this.recordingStream) {
69
- await this.stopRecording({ reason: 'restart' });
70
- }
71
- await fs.promises.mkdir(path.dirname(outputPath), { recursive: true });
72
- const stream = fs.createWriteStream(outputPath, { flags: 'a', encoding: 'utf-8' });
73
- stream.on('error', (err) => {
74
- this.recording.lastError = err?.message || String(err);
75
- this.recording.enabled = false;
76
- });
77
- this.recordingStream = stream;
78
- this.recording = {
79
- active: true,
80
- enabled: true,
81
- name,
82
- outputPath,
83
- overlay,
84
- startedAt: Date.now(),
85
- endedAt: null,
86
- eventCount: 0,
87
- lastEventAt: null,
88
- lastError: null,
89
- };
90
- const context = this.getContext();
91
- if (context) {
92
- const pages = context.pages().filter((p) => !p.isClosed());
93
- for (const page of pages) {
94
- this.bindRecorderBridge(page);
95
- // eslint-disable-next-line no-await-in-loop
96
- await this.installRecorderRuntime(page, 'recording_start').catch(() => { });
97
- }
98
- }
99
- this.writeRecordingEvent('recording.start', { name, outputPath, overlay }, { allowWhenDisabled: true });
100
- return this.getRecordingStatus();
101
- }
102
- async stopRecording(options = {}) {
103
- if (!this.recordingStream) {
104
- this.recording.active = false;
105
- this.recording.enabled = false;
106
- this.recording.overlay = false;
107
- this.recording.endedAt = Date.now();
108
- const context = this.getContext();
109
- if (context) {
110
- const pages = context.pages().filter((p) => !p.isClosed());
111
- for (const page of pages) {
112
- // eslint-disable-next-line no-await-in-loop
113
- await this.destroyRecorderRuntimeOnPage(page).catch(() => { });
114
- }
115
- }
116
- return this.getRecordingStatus();
117
- }
118
- this.writeRecordingEvent('recording.stop', { reason: options.reason || 'manual' }, { allowWhenDisabled: true });
119
- this.recording.enabled = false;
120
- this.recording.active = false;
121
- this.recording.overlay = false;
122
- this.recording.endedAt = Date.now();
123
- const context = this.getContext();
124
- if (context) {
125
- const pages = context.pages().filter((p) => !p.isClosed());
126
- for (const page of pages) {
127
- // eslint-disable-next-line no-await-in-loop
128
- await this.destroyRecorderRuntimeOnPage(page).catch(() => { });
129
- }
130
- }
131
- const stream = this.recordingStream;
132
- this.recordingStream = null;
133
- await new Promise((resolve) => {
134
- stream.end(() => resolve());
135
- });
136
- return this.getRecordingStatus();
137
- }
138
- writeRecordingEvent(type, payload = {}, options = {}) {
139
- if (!this.recordingStream || !this.recording.active)
140
- return;
141
- if (!this.recording.enabled && !options.allowWhenDisabled)
142
- return;
143
- const eventTs = Date.now();
144
- const entry = {
145
- ts: eventTs,
146
- profileId: this.profileId,
147
- sessionId: this.profileId,
148
- type,
149
- url: options.pageUrl || this.getCurrentUrl() || null,
150
- payload,
151
- };
152
- try {
153
- this.recordingStream.write(`${JSON.stringify(entry)}\n`);
154
- this.recording.eventCount += 1;
155
- this.recording.lastEventAt = eventTs;
156
- }
157
- catch (err) {
158
- this.recording.lastError = err?.message || String(err);
159
- }
160
- }
161
- handleRecorderEvent(page, evt) {
162
- const type = String(evt?.type || '').trim();
163
- if (!type)
164
- return;
165
- const pageUrl = String(evt?.href || page?.url?.() || this.getCurrentUrl() || '');
166
- const payload = evt?.payload && typeof evt.payload === 'object' ? evt.payload : {};
167
- if (type === 'recording.toggled') {
168
- if (!this.recording.active) {
169
- this.recording.enabled = false;
170
- return;
171
- }
172
- this.recording.enabled = payload.enabled !== false;
173
- this.writeRecordingEvent(type, payload, { pageUrl, allowWhenDisabled: true });
174
- return;
175
- }
176
- if (type === 'recording.runtime_ready') {
177
- this.writeRecordingEvent(type, payload, { pageUrl, allowWhenDisabled: true });
178
- return;
179
- }
180
- this.writeRecordingEvent(type, payload, { pageUrl });
181
- }
182
- recordPageVisit(page, reason) {
183
- const pageUrl = page?.url?.() || this.getCurrentUrl() || null;
184
- if (!pageUrl)
185
- return;
186
- this.writeRecordingEvent('page.visit', { reason, title: null }, { pageUrl });
187
- }
188
- async destroyRecorderRuntimeOnPage(page) {
189
- if (!page || page.isClosed())
190
- return;
191
- await page.evaluate(() => {
192
- const runtime = window.__camoRecorderV1__;
193
- if (!runtime || typeof runtime.destroy !== 'function')
194
- return null;
195
- return runtime.destroy();
196
- });
197
- }
198
- }
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { resolveRecordsRoot } from '../storage-paths.js';
4
+ export class BrowserSessionRecording {
5
+ profileId;
6
+ getCurrentUrl;
7
+ getContext;
8
+ recordingStream = null;
9
+ recording = {
10
+ active: false,
11
+ enabled: false,
12
+ name: null,
13
+ outputPath: null,
14
+ overlay: false,
15
+ startedAt: null,
16
+ endedAt: null,
17
+ eventCount: 0,
18
+ lastEventAt: null,
19
+ lastError: null,
20
+ };
21
+ bindRecorderBridge = () => { };
22
+ installRecorderRuntime = async () => { };
23
+ constructor(profileId, getCurrentUrl, getContext) {
24
+ this.profileId = profileId;
25
+ this.getCurrentUrl = getCurrentUrl;
26
+ this.getContext = getContext;
27
+ }
28
+ setBindRecorderBridge(fn) {
29
+ this.bindRecorderBridge = fn;
30
+ }
31
+ setInstallRecorderRuntime(fn) {
32
+ this.installRecorderRuntime = fn;
33
+ }
34
+ getRecordingStatus() {
35
+ return { ...this.recording };
36
+ }
37
+ normalizeRecordingName(raw) {
38
+ const text = String(raw || '').trim();
39
+ const fallback = `record-${this.profileId}`;
40
+ if (!text)
41
+ return fallback;
42
+ return text.replace(/[^a-zA-Z0-9._-]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 120) || fallback;
43
+ }
44
+ buildRecordingFilename(name) {
45
+ const stamp = new Date().toISOString().replace(/[:.]/g, '-');
46
+ return `${stamp}-${name}.jsonl`;
47
+ }
48
+ resolveRecordingOutputPath(options) {
49
+ const name = this.normalizeRecordingName(options?.name);
50
+ const rawOutput = String(options?.outputPath || '').trim();
51
+ if (!rawOutput) {
52
+ const root = path.join(resolveRecordsRoot(), this.profileId);
53
+ return path.join(root, this.buildRecordingFilename(name));
54
+ }
55
+ const absolute = path.isAbsolute(rawOutput) ? rawOutput : path.resolve(rawOutput);
56
+ if (absolute.endsWith(path.sep)) {
57
+ return path.join(absolute, this.buildRecordingFilename(name));
58
+ }
59
+ if (fs.existsSync(absolute) && fs.statSync(absolute).isDirectory()) {
60
+ return path.join(absolute, this.buildRecordingFilename(name));
61
+ }
62
+ return absolute;
63
+ }
64
+ async startRecording(options = {}) {
65
+ const outputPath = this.resolveRecordingOutputPath(options);
66
+ const name = this.normalizeRecordingName(options?.name);
67
+ const overlay = options?.overlay !== false;
68
+ if (this.recordingStream) {
69
+ await this.stopRecording({ reason: 'restart' });
70
+ }
71
+ await fs.promises.mkdir(path.dirname(outputPath), { recursive: true });
72
+ const stream = fs.createWriteStream(outputPath, { flags: 'a', encoding: 'utf-8' });
73
+ stream.on('error', (err) => {
74
+ this.recording.lastError = err?.message || String(err);
75
+ this.recording.enabled = false;
76
+ });
77
+ this.recordingStream = stream;
78
+ this.recording = {
79
+ active: true,
80
+ enabled: true,
81
+ name,
82
+ outputPath,
83
+ overlay,
84
+ startedAt: Date.now(),
85
+ endedAt: null,
86
+ eventCount: 0,
87
+ lastEventAt: null,
88
+ lastError: null,
89
+ };
90
+ const context = this.getContext();
91
+ if (context) {
92
+ const pages = context.pages().filter((p) => !p.isClosed());
93
+ for (const page of pages) {
94
+ this.bindRecorderBridge(page);
95
+ // eslint-disable-next-line no-await-in-loop
96
+ await this.installRecorderRuntime(page, 'recording_start').catch(() => { });
97
+ }
98
+ }
99
+ this.writeRecordingEvent('recording.start', { name, outputPath, overlay }, { allowWhenDisabled: true });
100
+ return this.getRecordingStatus();
101
+ }
102
+ async stopRecording(options = {}) {
103
+ if (!this.recordingStream) {
104
+ this.recording.active = false;
105
+ this.recording.enabled = false;
106
+ this.recording.overlay = false;
107
+ this.recording.endedAt = Date.now();
108
+ const context = this.getContext();
109
+ if (context) {
110
+ const pages = context.pages().filter((p) => !p.isClosed());
111
+ for (const page of pages) {
112
+ // eslint-disable-next-line no-await-in-loop
113
+ await this.destroyRecorderRuntimeOnPage(page).catch(() => { });
114
+ }
115
+ }
116
+ return this.getRecordingStatus();
117
+ }
118
+ this.writeRecordingEvent('recording.stop', { reason: options.reason || 'manual' }, { allowWhenDisabled: true });
119
+ this.recording.enabled = false;
120
+ this.recording.active = false;
121
+ this.recording.overlay = false;
122
+ this.recording.endedAt = Date.now();
123
+ const context = this.getContext();
124
+ if (context) {
125
+ const pages = context.pages().filter((p) => !p.isClosed());
126
+ for (const page of pages) {
127
+ // eslint-disable-next-line no-await-in-loop
128
+ await this.destroyRecorderRuntimeOnPage(page).catch(() => { });
129
+ }
130
+ }
131
+ const stream = this.recordingStream;
132
+ this.recordingStream = null;
133
+ await new Promise((resolve) => {
134
+ stream.end(() => resolve());
135
+ });
136
+ return this.getRecordingStatus();
137
+ }
138
+ writeRecordingEvent(type, payload = {}, options = {}) {
139
+ if (!this.recordingStream || !this.recording.active)
140
+ return;
141
+ if (!this.recording.enabled && !options.allowWhenDisabled)
142
+ return;
143
+ const eventTs = Date.now();
144
+ const entry = {
145
+ ts: eventTs,
146
+ profileId: this.profileId,
147
+ sessionId: this.profileId,
148
+ type,
149
+ url: options.pageUrl || this.getCurrentUrl() || null,
150
+ payload,
151
+ };
152
+ try {
153
+ this.recordingStream.write(`${JSON.stringify(entry)}\n`);
154
+ this.recording.eventCount += 1;
155
+ this.recording.lastEventAt = eventTs;
156
+ }
157
+ catch (err) {
158
+ this.recording.lastError = err?.message || String(err);
159
+ }
160
+ }
161
+ handleRecorderEvent(page, evt) {
162
+ const type = String(evt?.type || '').trim();
163
+ if (!type)
164
+ return;
165
+ const pageUrl = String(evt?.href || page?.url?.() || this.getCurrentUrl() || '');
166
+ const payload = evt?.payload && typeof evt.payload === 'object' ? evt.payload : {};
167
+ if (type === 'recording.toggled') {
168
+ if (!this.recording.active) {
169
+ this.recording.enabled = false;
170
+ return;
171
+ }
172
+ this.recording.enabled = payload.enabled !== false;
173
+ this.writeRecordingEvent(type, payload, { pageUrl, allowWhenDisabled: true });
174
+ return;
175
+ }
176
+ if (type === 'recording.runtime_ready') {
177
+ this.writeRecordingEvent(type, payload, { pageUrl, allowWhenDisabled: true });
178
+ return;
179
+ }
180
+ this.writeRecordingEvent(type, payload, { pageUrl });
181
+ }
182
+ recordPageVisit(page, reason) {
183
+ const pageUrl = page?.url?.() || this.getCurrentUrl() || null;
184
+ if (!pageUrl)
185
+ return;
186
+ this.writeRecordingEvent('page.visit', { reason, title: null }, { pageUrl });
187
+ }
188
+ async destroyRecorderRuntimeOnPage(page) {
189
+ if (!page || page.isClosed())
190
+ return;
191
+ await page.evaluate(() => {
192
+ const runtime = window.__camoRecorderV1__;
193
+ if (!runtime || typeof runtime.destroy !== 'function')
194
+ return null;
195
+ return runtime.destroy();
196
+ });
197
+ }
198
+ }
199
199
  //# sourceMappingURL=recording.js.map
@@ -1,62 +1,62 @@
1
- import { getStateBus } from '../state-bus.js';
2
- import { logDebug } from '../logging.js';
3
- const stateBus = getStateBus();
4
- export function createRuntimeEventManager(sessionId) {
5
- const observers = new Set();
6
- function addObserver(observer) {
7
- observers.add(observer);
8
- logDebug('browser-service', 'runtimeObserver:add', { sessionId, total: observers.size });
9
- return () => {
10
- observers.delete(observer);
11
- logDebug('browser-service', 'runtimeObserver:remove', { sessionId, total: observers.size });
12
- };
13
- }
14
- function emit(event) {
15
- const payload = {
16
- ts: Date.now(),
17
- sessionId,
18
- ...event,
19
- };
20
- logDebug('browser-service', 'runtimeEvent', {
21
- sessionId,
22
- type: event?.type || 'unknown',
23
- observers: observers.size,
24
- });
25
- observers.forEach((observer) => {
26
- try {
27
- observer(payload);
28
- }
29
- catch (err) {
30
- console.warn('[BrowserSession] runtime observer error', err);
31
- }
32
- });
33
- publishState(payload);
34
- }
35
- function publishState(payload) {
36
- try {
37
- stateBus.setState(`browser-session:${sessionId}`, {
38
- status: 'running',
39
- lastRuntimeEvent: payload?.type || 'unknown',
40
- lastUrl: payload?.pageUrl || '',
41
- lastUpdate: payload?.ts || Date.now(),
42
- });
43
- stateBus.publish('browser.runtime.event', payload);
44
- }
45
- catch (err) {
46
- logDebug('browser-service', 'runtimeEvent:stateBus:error', {
47
- sessionId,
48
- error: err?.message || err,
49
- });
50
- }
51
- }
52
- function clearObservers() {
53
- observers.clear();
54
- }
55
- return {
56
- addObserver,
57
- emit,
58
- publishState,
59
- clearObservers,
60
- };
61
- }
1
+ import { getStateBus } from '../state-bus.js';
2
+ import { logDebug } from '../logging.js';
3
+ const stateBus = getStateBus();
4
+ export function createRuntimeEventManager(sessionId) {
5
+ const observers = new Set();
6
+ function addObserver(observer) {
7
+ observers.add(observer);
8
+ logDebug('browser-service', 'runtimeObserver:add', { sessionId, total: observers.size });
9
+ return () => {
10
+ observers.delete(observer);
11
+ logDebug('browser-service', 'runtimeObserver:remove', { sessionId, total: observers.size });
12
+ };
13
+ }
14
+ function emit(event) {
15
+ const payload = {
16
+ ts: Date.now(),
17
+ sessionId,
18
+ ...event,
19
+ };
20
+ logDebug('browser-service', 'runtimeEvent', {
21
+ sessionId,
22
+ type: event?.type || 'unknown',
23
+ observers: observers.size,
24
+ });
25
+ observers.forEach((observer) => {
26
+ try {
27
+ observer(payload);
28
+ }
29
+ catch (err) {
30
+ console.warn('[BrowserSession] runtime observer error', err);
31
+ }
32
+ });
33
+ publishState(payload);
34
+ }
35
+ function publishState(payload) {
36
+ try {
37
+ stateBus.setState(`browser-session:${sessionId}`, {
38
+ status: 'running',
39
+ lastRuntimeEvent: payload?.type || 'unknown',
40
+ lastUrl: payload?.pageUrl || '',
41
+ lastUpdate: payload?.ts || Date.now(),
42
+ });
43
+ stateBus.publish('browser.runtime.event', payload);
44
+ }
45
+ catch (err) {
46
+ logDebug('browser-service', 'runtimeEvent:stateBus:error', {
47
+ sessionId,
48
+ error: err?.message || err,
49
+ });
50
+ }
51
+ }
52
+ function clearObservers() {
53
+ observers.clear();
54
+ }
55
+ return {
56
+ addObserver,
57
+ emit,
58
+ publishState,
59
+ clearObservers,
60
+ };
61
+ }
62
62
  //# sourceMappingURL=runtime-events.js.map