agent-web-interface 4.1.1 → 4.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/browser/session-manager.d.ts +28 -1
- package/dist/src/browser/session-manager.d.ts.map +1 -1
- package/dist/src/browser/session-manager.js +73 -0
- package/dist/src/browser/session-manager.js.map +1 -1
- package/dist/src/index.d.ts +10 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +66 -7
- package/dist/src/index.js.map +1 -1
- package/dist/src/server/mcp-server.d.ts +14 -1
- package/dist/src/server/mcp-server.d.ts.map +1 -1
- package/dist/src/server/mcp-server.js +50 -7
- package/dist/src/server/mcp-server.js.map +1 -1
- package/dist/src/server/session-store.d.ts +18 -3
- package/dist/src/server/session-store.d.ts.map +1 -1
- package/dist/src/server/session-store.js +27 -3
- package/dist/src/server/session-store.js.map +1 -1
- package/dist/src/session/session-worker-binding.d.ts +57 -0
- package/dist/src/session/session-worker-binding.d.ts.map +1 -0
- package/dist/src/session/session-worker-binding.js +109 -0
- package/dist/src/session/session-worker-binding.js.map +1 -0
- package/dist/src/shared/services/logging.service.d.ts +8 -0
- package/dist/src/shared/services/logging.service.d.ts.map +1 -1
- package/dist/src/shared/services/logging.service.js +10 -0
- package/dist/src/shared/services/logging.service.js.map +1 -1
- package/dist/src/snapshot/element-resolver.d.ts +35 -14
- package/dist/src/snapshot/element-resolver.d.ts.map +1 -1
- package/dist/src/snapshot/element-resolver.js +109 -60
- package/dist/src/snapshot/element-resolver.js.map +1 -1
- package/dist/src/snapshot/index.d.ts +1 -1
- package/dist/src/snapshot/index.d.ts.map +1 -1
- package/dist/src/snapshot/index.js +1 -1
- package/dist/src/snapshot/index.js.map +1 -1
- package/dist/src/snapshot/snapshot-compiler.d.ts.map +1 -1
- package/dist/src/snapshot/snapshot-compiler.js +99 -2
- package/dist/src/snapshot/snapshot-compiler.js.map +1 -1
- package/dist/src/snapshot/snapshot.types.d.ts +1 -1
- package/dist/src/snapshot/snapshot.types.d.ts.map +1 -1
- package/dist/src/snapshot/snapshot.types.js +1 -0
- package/dist/src/snapshot/snapshot.types.js.map +1 -1
- package/dist/src/state/actionables-filter.d.ts.map +1 -1
- package/dist/src/state/actionables-filter.js +1 -0
- package/dist/src/state/actionables-filter.js.map +1 -1
- package/dist/src/state/state-renderer.d.ts.map +1 -1
- package/dist/src/state/state-renderer.js +2 -0
- package/dist/src/state/state-renderer.js.map +1 -1
- package/dist/src/tools/browser-tools.d.ts +29 -8
- package/dist/src/tools/browser-tools.d.ts.map +1 -1
- package/dist/src/tools/browser-tools.js +93 -131
- package/dist/src/tools/browser-tools.js.map +1 -1
- package/dist/src/tools/canvas-tools.d.ts +32 -0
- package/dist/src/tools/canvas-tools.d.ts.map +1 -0
- package/dist/src/tools/canvas-tools.js +370 -0
- package/dist/src/tools/canvas-tools.js.map +1 -0
- package/dist/src/tools/form-tools.d.ts +2 -4
- package/dist/src/tools/form-tools.d.ts.map +1 -1
- package/dist/src/tools/form-tools.js +6 -38
- package/dist/src/tools/form-tools.js.map +1 -1
- package/dist/src/tools/index.d.ts +6 -4
- package/dist/src/tools/index.d.ts.map +1 -1
- package/dist/src/tools/index.js +13 -4
- package/dist/src/tools/index.js.map +1 -1
- package/dist/src/tools/tool-context.d.ts +53 -0
- package/dist/src/tools/tool-context.d.ts.map +1 -0
- package/dist/src/tools/tool-context.js +119 -0
- package/dist/src/tools/tool-context.js.map +1 -0
- package/dist/src/tools/tool-result.types.d.ts +16 -1
- package/dist/src/tools/tool-result.types.d.ts.map +1 -1
- package/dist/src/tools/tool-result.types.js +11 -0
- package/dist/src/tools/tool-result.types.js.map +1 -1
- package/dist/src/tools/tool-schemas.d.ts +277 -150
- package/dist/src/tools/tool-schemas.d.ts.map +1 -1
- package/dist/src/tools/tool-schemas.js +85 -3
- package/dist/src/tools/tool-schemas.js.map +1 -1
- package/dist/src/worker/chrome-worker-process.d.ts +123 -0
- package/dist/src/worker/chrome-worker-process.d.ts.map +1 -0
- package/dist/src/worker/chrome-worker-process.js +294 -0
- package/dist/src/worker/chrome-worker-process.js.map +1 -0
- package/dist/src/worker/errors/index.d.ts +5 -0
- package/dist/src/worker/errors/index.d.ts.map +1 -0
- package/dist/src/worker/errors/index.js +5 -0
- package/dist/src/worker/errors/index.js.map +1 -0
- package/dist/src/worker/errors/worker.error.d.ts +122 -0
- package/dist/src/worker/errors/worker.error.d.ts.map +1 -0
- package/dist/src/worker/errors/worker.error.js +199 -0
- package/dist/src/worker/errors/worker.error.js.map +1 -0
- package/dist/src/worker/health-monitor.d.ts +141 -0
- package/dist/src/worker/health-monitor.d.ts.map +1 -0
- package/dist/src/worker/health-monitor.js +260 -0
- package/dist/src/worker/health-monitor.js.map +1 -0
- package/dist/src/worker/index.d.ts +16 -0
- package/dist/src/worker/index.d.ts.map +1 -0
- package/dist/src/worker/index.js +19 -0
- package/dist/src/worker/index.js.map +1 -0
- package/dist/src/worker/lease-manager.d.ts +137 -0
- package/dist/src/worker/lease-manager.d.ts.map +1 -0
- package/dist/src/worker/lease-manager.js +334 -0
- package/dist/src/worker/lease-manager.js.map +1 -0
- package/dist/src/worker/multi-tenant-config.d.ts +46 -0
- package/dist/src/worker/multi-tenant-config.d.ts.map +1 -0
- package/dist/src/worker/multi-tenant-config.js +94 -0
- package/dist/src/worker/multi-tenant-config.js.map +1 -0
- package/dist/src/worker/port-allocator.d.ts +96 -0
- package/dist/src/worker/port-allocator.d.ts.map +1 -0
- package/dist/src/worker/port-allocator.js +153 -0
- package/dist/src/worker/port-allocator.js.map +1 -0
- package/dist/src/worker/types.d.ts +218 -0
- package/dist/src/worker/types.d.ts.map +1 -0
- package/dist/src/worker/types.js +38 -0
- package/dist/src/worker/types.js.map +1 -0
- package/dist/src/worker/worker-manager.d.ts +157 -0
- package/dist/src/worker/worker-manager.d.ts.map +1 -0
- package/dist/src/worker/worker-manager.js +500 -0
- package/dist/src/worker/worker-manager.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chrome Worker Process
|
|
3
|
+
*
|
|
4
|
+
* Manages a Chrome browser child process with CDP debugging enabled.
|
|
5
|
+
* Handles process lifecycle: start, stop, crash detection.
|
|
6
|
+
*/
|
|
7
|
+
import { spawn } from 'child_process';
|
|
8
|
+
import { EventEmitter } from 'events';
|
|
9
|
+
import { existsSync } from 'fs';
|
|
10
|
+
import { mkdir } from 'fs/promises';
|
|
11
|
+
import { CHROME_WORKER_ARGS } from './types.js';
|
|
12
|
+
import { WorkerError } from './errors/index.js';
|
|
13
|
+
import { createLogger } from '../shared/services/logging.service.js';
|
|
14
|
+
const logger = createLogger('ChromeWorkerProcess');
|
|
15
|
+
/**
|
|
16
|
+
* Default Chrome executable paths by platform
|
|
17
|
+
*/
|
|
18
|
+
const DEFAULT_CHROME_PATHS = {
|
|
19
|
+
linux: [
|
|
20
|
+
'/usr/bin/google-chrome',
|
|
21
|
+
'/usr/bin/google-chrome-stable',
|
|
22
|
+
'/usr/bin/chromium',
|
|
23
|
+
'/usr/bin/chromium-browser',
|
|
24
|
+
'/snap/bin/chromium',
|
|
25
|
+
],
|
|
26
|
+
darwin: [
|
|
27
|
+
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
|
|
28
|
+
'/Applications/Chromium.app/Contents/MacOS/Chromium',
|
|
29
|
+
],
|
|
30
|
+
win32: [
|
|
31
|
+
'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe',
|
|
32
|
+
'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',
|
|
33
|
+
],
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Find Chrome executable on the system
|
|
37
|
+
*/
|
|
38
|
+
export function findChromePath() {
|
|
39
|
+
const platform = process.platform;
|
|
40
|
+
const paths = DEFAULT_CHROME_PATHS[platform] || [];
|
|
41
|
+
for (const chromePath of paths) {
|
|
42
|
+
if (existsSync(chromePath)) {
|
|
43
|
+
return chromePath;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Manages a Chrome browser child process.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* const worker = new ChromeWorkerProcess({
|
|
54
|
+
* workerId: 'w-123',
|
|
55
|
+
* port: 9300,
|
|
56
|
+
* profileDir: '/var/lib/athena/profiles/tenant-a',
|
|
57
|
+
* });
|
|
58
|
+
*
|
|
59
|
+
* worker.on('started', ({ pid, cdpEndpoint }) => {
|
|
60
|
+
* console.log(`Chrome started with PID ${pid} at ${cdpEndpoint}`);
|
|
61
|
+
* });
|
|
62
|
+
*
|
|
63
|
+
* worker.on('exit', ({ code, signal }) => {
|
|
64
|
+
* console.log(`Chrome exited with code ${code}`);
|
|
65
|
+
* });
|
|
66
|
+
*
|
|
67
|
+
* await worker.start();
|
|
68
|
+
* // ... use the worker ...
|
|
69
|
+
* await worker.stop();
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export class ChromeWorkerProcess extends EventEmitter {
|
|
73
|
+
workerId;
|
|
74
|
+
port;
|
|
75
|
+
profileDir;
|
|
76
|
+
chromePath;
|
|
77
|
+
startupTimeoutMs;
|
|
78
|
+
process = null;
|
|
79
|
+
_state = 'idle';
|
|
80
|
+
_pid;
|
|
81
|
+
_cdpEndpoint;
|
|
82
|
+
constructor(config) {
|
|
83
|
+
super();
|
|
84
|
+
this.workerId = config.workerId;
|
|
85
|
+
this.port = config.port;
|
|
86
|
+
this.profileDir = config.profileDir;
|
|
87
|
+
this.startupTimeoutMs = config.startupTimeoutMs ?? 30_000;
|
|
88
|
+
// Resolve Chrome path
|
|
89
|
+
const chromePath = config.chromePath ?? findChromePath();
|
|
90
|
+
if (!chromePath) {
|
|
91
|
+
throw new Error('Chrome executable not found. Please set CHROME_PATH environment variable or install Chrome.');
|
|
92
|
+
}
|
|
93
|
+
this.chromePath = chromePath;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Get current worker state
|
|
97
|
+
*/
|
|
98
|
+
get state() {
|
|
99
|
+
return this._state;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Get Chrome process ID (if running)
|
|
103
|
+
*/
|
|
104
|
+
get pid() {
|
|
105
|
+
return this._pid;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Get CDP endpoint URL (if running)
|
|
109
|
+
*/
|
|
110
|
+
get cdpEndpoint() {
|
|
111
|
+
return this._cdpEndpoint;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Check if the worker is currently running
|
|
115
|
+
*/
|
|
116
|
+
get isRunning() {
|
|
117
|
+
return this._state === 'running';
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Start the Chrome process
|
|
121
|
+
*
|
|
122
|
+
* @throws WorkerError if process fails to start
|
|
123
|
+
*/
|
|
124
|
+
async start() {
|
|
125
|
+
if (this._state !== 'idle' && this._state !== 'stopped' && this._state !== 'crashed') {
|
|
126
|
+
throw WorkerError.invalidState(this._state, 'start', { workerId: this.workerId });
|
|
127
|
+
}
|
|
128
|
+
this._state = 'starting';
|
|
129
|
+
try {
|
|
130
|
+
// Ensure profile directory exists
|
|
131
|
+
await mkdir(this.profileDir, { recursive: true });
|
|
132
|
+
const args = this.buildArgs();
|
|
133
|
+
logger.debug('Starting Chrome process', {
|
|
134
|
+
workerId: this.workerId,
|
|
135
|
+
chromePath: this.chromePath,
|
|
136
|
+
port: this.port,
|
|
137
|
+
profileDir: this.profileDir,
|
|
138
|
+
});
|
|
139
|
+
this.process = spawn(this.chromePath, args, {
|
|
140
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
141
|
+
detached: false,
|
|
142
|
+
});
|
|
143
|
+
this._pid = this.process.pid;
|
|
144
|
+
// Set up event handlers
|
|
145
|
+
this.setupProcessHandlers();
|
|
146
|
+
// Wait for CDP to become available
|
|
147
|
+
await this.waitForCdp();
|
|
148
|
+
this._state = 'running';
|
|
149
|
+
this._cdpEndpoint = `http://127.0.0.1:${this.port}`;
|
|
150
|
+
this.emit('started', { pid: this._pid, cdpEndpoint: this._cdpEndpoint });
|
|
151
|
+
logger.info('Chrome process started', {
|
|
152
|
+
workerId: this.workerId,
|
|
153
|
+
pid: this._pid,
|
|
154
|
+
cdpEndpoint: this._cdpEndpoint,
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
this._state = 'crashed';
|
|
159
|
+
// Kill orphaned process before cleanup to prevent process leak
|
|
160
|
+
if (this.process) {
|
|
161
|
+
this.process.kill('SIGKILL');
|
|
162
|
+
}
|
|
163
|
+
this.cleanup();
|
|
164
|
+
throw WorkerError.workerStartFailed(this.workerId, error instanceof Error ? error : new Error(String(error)));
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Stop the Chrome process gracefully
|
|
169
|
+
*
|
|
170
|
+
* @param timeoutMs - Time to wait for graceful shutdown before SIGKILL
|
|
171
|
+
*/
|
|
172
|
+
async stop(timeoutMs = 5000) {
|
|
173
|
+
if (!this.process || this._state === 'stopped' || this._state === 'stopping') {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
this._state = 'stopping';
|
|
177
|
+
logger.debug('Stopping Chrome process', {
|
|
178
|
+
workerId: this.workerId,
|
|
179
|
+
pid: this._pid,
|
|
180
|
+
});
|
|
181
|
+
return new Promise((resolve) => {
|
|
182
|
+
const forceKillTimer = setTimeout(() => {
|
|
183
|
+
logger.warning('Chrome process did not exit gracefully, sending SIGKILL', {
|
|
184
|
+
workerId: this.workerId,
|
|
185
|
+
pid: this._pid,
|
|
186
|
+
});
|
|
187
|
+
this.process?.kill('SIGKILL');
|
|
188
|
+
}, timeoutMs);
|
|
189
|
+
const onExit = () => {
|
|
190
|
+
clearTimeout(forceKillTimer);
|
|
191
|
+
resolve();
|
|
192
|
+
};
|
|
193
|
+
this.process.once('exit', onExit);
|
|
194
|
+
// Send SIGTERM for graceful shutdown
|
|
195
|
+
this.process.kill('SIGTERM');
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Force kill the Chrome process immediately
|
|
200
|
+
*/
|
|
201
|
+
kill() {
|
|
202
|
+
if (this.process) {
|
|
203
|
+
this.process.kill('SIGKILL');
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Build Chrome command line arguments
|
|
208
|
+
*/
|
|
209
|
+
buildArgs() {
|
|
210
|
+
return [
|
|
211
|
+
`--remote-debugging-port=${this.port}`,
|
|
212
|
+
'--remote-debugging-address=127.0.0.1',
|
|
213
|
+
`--user-data-dir=${this.profileDir}`,
|
|
214
|
+
...CHROME_WORKER_ARGS,
|
|
215
|
+
];
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Set up process event handlers
|
|
219
|
+
*/
|
|
220
|
+
setupProcessHandlers() {
|
|
221
|
+
if (!this.process)
|
|
222
|
+
return;
|
|
223
|
+
this.process.on('error', (error) => {
|
|
224
|
+
logger.error('Chrome process error', error, {
|
|
225
|
+
workerId: this.workerId,
|
|
226
|
+
});
|
|
227
|
+
this._state = 'crashed';
|
|
228
|
+
this.emit('error', { error });
|
|
229
|
+
this.cleanup();
|
|
230
|
+
});
|
|
231
|
+
this.process.on('exit', (code, signal) => {
|
|
232
|
+
if (this._state !== 'stopping') {
|
|
233
|
+
// Unexpected exit
|
|
234
|
+
this._state = 'crashed';
|
|
235
|
+
logger.warning('Chrome process exited unexpectedly', {
|
|
236
|
+
workerId: this.workerId,
|
|
237
|
+
exitCode: code,
|
|
238
|
+
signal,
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
this._state = 'stopped';
|
|
243
|
+
logger.info('Chrome process stopped', {
|
|
244
|
+
workerId: this.workerId,
|
|
245
|
+
exitCode: code,
|
|
246
|
+
signal,
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
this.emit('exit', { code, signal });
|
|
250
|
+
this.cleanup();
|
|
251
|
+
});
|
|
252
|
+
// Log stderr for debugging
|
|
253
|
+
this.process.stderr?.on('data', (data) => {
|
|
254
|
+
const message = data.toString().trim();
|
|
255
|
+
if (message) {
|
|
256
|
+
logger.debug('Chrome stderr', { workerId: this.workerId, message });
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Wait for CDP endpoint to become available
|
|
262
|
+
*/
|
|
263
|
+
async waitForCdp() {
|
|
264
|
+
const startTime = Date.now();
|
|
265
|
+
const checkInterval = 100;
|
|
266
|
+
while (Date.now() - startTime < this.startupTimeoutMs) {
|
|
267
|
+
try {
|
|
268
|
+
const response = await fetch(`http://127.0.0.1:${this.port}/json/version`, {
|
|
269
|
+
signal: AbortSignal.timeout(1000),
|
|
270
|
+
});
|
|
271
|
+
if (response.ok) {
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
catch {
|
|
276
|
+
// CDP not ready yet, continue waiting
|
|
277
|
+
}
|
|
278
|
+
await new Promise((resolve) => setTimeout(resolve, checkInterval));
|
|
279
|
+
}
|
|
280
|
+
throw new Error(`CDP endpoint did not become available within ${this.startupTimeoutMs}ms`);
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Clean up process references and event listeners
|
|
284
|
+
*/
|
|
285
|
+
cleanup() {
|
|
286
|
+
if (this.process) {
|
|
287
|
+
this.process.removeAllListeners();
|
|
288
|
+
}
|
|
289
|
+
this.process = null;
|
|
290
|
+
this._pid = undefined;
|
|
291
|
+
this._cdpEndpoint = undefined;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
//# sourceMappingURL=chrome-worker-process.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chrome-worker-process.js","sourceRoot":"","sources":["../../../src/worker/chrome-worker-process.ts"],"names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AAE,EAAE,CAAC;oBAChB,OAAO;gBACT,CAAC;YACH,CAAC;YAAC,MAAM,CAAC;gBACP,sCAAsC;YACxC,CAAC;YAED,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC,CAAC;QACrE,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,gDAAgD,IAAI,CAAC,gBAAgB,IAAI,CAAC,CAAC;IAC7F,CAAC;IAED;;OAEG;IACK,OAAO;QACb,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACjB,IAAI,CAAC,OAAO,CAAC,kBAAkB,EAAE,CAAC;QACpC,CAAC;QACD,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,IAAI,CAAC,IAAI,GAAG,SAAS,CAAC;QACtB,IAAI,CAAC,YAAY,GAAG,SAAS,CAAC;IAChC,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/worker/errors/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,WAAW,EAAE,KAAK,eAAe,EAAE,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/worker/errors/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,WAAW,EAAwB,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker Error
|
|
3
|
+
*
|
|
4
|
+
* Standardized error classification for worker operations.
|
|
5
|
+
* Provides error codes for programmatic handling and detailed messages for debugging.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Error codes for worker operations
|
|
9
|
+
*/
|
|
10
|
+
export type WorkerErrorCode = 'WORKER_NOT_FOUND' | 'WORKER_NOT_RUNNING' | 'WORKER_CRASHED' | 'WORKER_START_FAILED' | 'WORKER_STOP_FAILED' | 'LEASE_NOT_FOUND' | 'LEASE_NOT_HELD' | 'LEASE_EXPIRED' | 'LEASE_ALREADY_HELD' | 'PORT_EXHAUSTED' | 'MAX_WORKERS_REACHED' | 'HEALTH_CHECK_FAILED' | 'HEALTH_CHECK_TIMEOUT' | 'INVALID_STATE' | 'OPERATION_FAILED';
|
|
11
|
+
/**
|
|
12
|
+
* Standardized error for worker operations.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* throw new WorkerError(
|
|
17
|
+
* 'Worker crashed unexpectedly',
|
|
18
|
+
* 'WORKER_CRASHED',
|
|
19
|
+
* { workerId: 'w-123', exitCode: 1 }
|
|
20
|
+
* );
|
|
21
|
+
*
|
|
22
|
+
* // Catching and handling by code
|
|
23
|
+
* try {
|
|
24
|
+
* await workerManager.acquireForTenant('tenant-a', 'controller-1');
|
|
25
|
+
* } catch (error) {
|
|
26
|
+
* if (error instanceof WorkerError) {
|
|
27
|
+
* switch (error.code) {
|
|
28
|
+
* case 'MAX_WORKERS_REACHED':
|
|
29
|
+
* console.log('Worker limit reached, try again later');
|
|
30
|
+
* break;
|
|
31
|
+
* case 'LEASE_ALREADY_HELD':
|
|
32
|
+
* console.log('Another session has the lease');
|
|
33
|
+
* break;
|
|
34
|
+
* }
|
|
35
|
+
* }
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare class WorkerError extends Error {
|
|
40
|
+
/**
|
|
41
|
+
* Error code for programmatic handling
|
|
42
|
+
*/
|
|
43
|
+
readonly code: WorkerErrorCode;
|
|
44
|
+
/**
|
|
45
|
+
* Original error that caused this error (if any)
|
|
46
|
+
*/
|
|
47
|
+
readonly cause?: Error;
|
|
48
|
+
/**
|
|
49
|
+
* Additional context for debugging
|
|
50
|
+
*/
|
|
51
|
+
readonly context?: Record<string, unknown>;
|
|
52
|
+
constructor(message: string, code: WorkerErrorCode, context?: Record<string, unknown>, cause?: Error);
|
|
53
|
+
/**
|
|
54
|
+
* Create a JSON-serializable representation of the error
|
|
55
|
+
*/
|
|
56
|
+
toJSON(): Record<string, unknown>;
|
|
57
|
+
/**
|
|
58
|
+
* Type guard to check if an error is a WorkerError
|
|
59
|
+
*/
|
|
60
|
+
static isWorkerError(error: unknown): error is WorkerError;
|
|
61
|
+
/**
|
|
62
|
+
* Create error for worker not found
|
|
63
|
+
*/
|
|
64
|
+
static workerNotFound(workerId: string, context?: Record<string, unknown>): WorkerError;
|
|
65
|
+
/**
|
|
66
|
+
* Create error for worker not running
|
|
67
|
+
*/
|
|
68
|
+
static workerNotRunning(workerId: string, currentState: string, context?: Record<string, unknown>): WorkerError;
|
|
69
|
+
/**
|
|
70
|
+
* Create error for worker crash
|
|
71
|
+
*/
|
|
72
|
+
static workerCrashed(workerId: string, exitCode: number | null, signal: string | null, context?: Record<string, unknown>): WorkerError;
|
|
73
|
+
/**
|
|
74
|
+
* Create error for worker start failure
|
|
75
|
+
*/
|
|
76
|
+
static workerStartFailed(workerId: string, cause: Error, context?: Record<string, unknown>): WorkerError;
|
|
77
|
+
/**
|
|
78
|
+
* Create error for worker stop failure
|
|
79
|
+
*/
|
|
80
|
+
static workerStopFailed(workerId: string, cause: Error, context?: Record<string, unknown>): WorkerError;
|
|
81
|
+
/**
|
|
82
|
+
* Create error for lease not found
|
|
83
|
+
*/
|
|
84
|
+
static leaseNotFound(tenantId: string, context?: Record<string, unknown>): WorkerError;
|
|
85
|
+
/**
|
|
86
|
+
* Create error for lease not held by controller
|
|
87
|
+
*/
|
|
88
|
+
static leaseNotHeld(tenantId: string, controllerId: string, actualControllerId: string, context?: Record<string, unknown>): WorkerError;
|
|
89
|
+
/**
|
|
90
|
+
* Create error for expired lease
|
|
91
|
+
*/
|
|
92
|
+
static leaseExpired(tenantId: string, expiredAt: number, context?: Record<string, unknown>): WorkerError;
|
|
93
|
+
/**
|
|
94
|
+
* Create error for lease already held
|
|
95
|
+
*/
|
|
96
|
+
static leaseAlreadyHeld(tenantId: string, heldByControllerId: string, context?: Record<string, unknown>): WorkerError;
|
|
97
|
+
/**
|
|
98
|
+
* Create error for port exhaustion
|
|
99
|
+
*/
|
|
100
|
+
static portExhausted(portMin: number, portMax: number, context?: Record<string, unknown>): WorkerError;
|
|
101
|
+
/**
|
|
102
|
+
* Create error for max workers reached
|
|
103
|
+
*/
|
|
104
|
+
static maxWorkersReached(maxWorkers: number, currentCount: number, context?: Record<string, unknown>): WorkerError;
|
|
105
|
+
/**
|
|
106
|
+
* Create error for health check failure
|
|
107
|
+
*/
|
|
108
|
+
static healthCheckFailed(workerId: string, cause: Error, context?: Record<string, unknown>): WorkerError;
|
|
109
|
+
/**
|
|
110
|
+
* Create error for health check timeout
|
|
111
|
+
*/
|
|
112
|
+
static healthCheckTimeout(workerId: string, timeoutMs: number, context?: Record<string, unknown>): WorkerError;
|
|
113
|
+
/**
|
|
114
|
+
* Create error for invalid state transition
|
|
115
|
+
*/
|
|
116
|
+
static invalidState(currentState: string, attemptedOperation: string, context?: Record<string, unknown>): WorkerError;
|
|
117
|
+
/**
|
|
118
|
+
* Create error for generic operation failure
|
|
119
|
+
*/
|
|
120
|
+
static operationFailed(operation: string, cause: Error, context?: Record<string, unknown>): WorkerError;
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=worker.error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker.error.d.ts","sourceRoot":"","sources":["../../../../src/worker/errors/worker.error.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;GAEG;AACH,MAAM,MAAM,eAAe,GAEvB,kBAAkB,GAClB,oBAAoB,GACpB,gBAAgB,GAChB,qBAAqB,GACrB,oBAAoB,GAEpB,iBAAiB,GACjB,gBAAgB,GAChB,eAAe,GACf,oBAAoB,GAEpB,gBAAgB,GAChB,qBAAqB,GAErB,qBAAqB,GACrB,sBAAsB,GAEtB,eAAe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Z,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAChC,WAAW;IASd;;OAEG;IACH,MAAM,CAAC,kBAAkB,CACvB,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAChC,WAAW;IAQd;;OAEG;IACH,MAAM,CAAC,YAAY,CACjB,YAAY,EAAE,MAAM,EACpB,kBAAkB,EAAE,MAAM,EAC1B,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAChC,WAAW;IAQd;;OAEG;IACH,MAAM,CAAC,eAAe,CACpB,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,KAAK,EACZ,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAChC,WAAW;CAQf"}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker Error
|
|
3
|
+
*
|
|
4
|
+
* Standardized error classification for worker operations.
|
|
5
|
+
* Provides error codes for programmatic handling and detailed messages for debugging.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Standardized error for worker operations.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* throw new WorkerError(
|
|
13
|
+
* 'Worker crashed unexpectedly',
|
|
14
|
+
* 'WORKER_CRASHED',
|
|
15
|
+
* { workerId: 'w-123', exitCode: 1 }
|
|
16
|
+
* );
|
|
17
|
+
*
|
|
18
|
+
* // Catching and handling by code
|
|
19
|
+
* try {
|
|
20
|
+
* await workerManager.acquireForTenant('tenant-a', 'controller-1');
|
|
21
|
+
* } catch (error) {
|
|
22
|
+
* if (error instanceof WorkerError) {
|
|
23
|
+
* switch (error.code) {
|
|
24
|
+
* case 'MAX_WORKERS_REACHED':
|
|
25
|
+
* console.log('Worker limit reached, try again later');
|
|
26
|
+
* break;
|
|
27
|
+
* case 'LEASE_ALREADY_HELD':
|
|
28
|
+
* console.log('Another session has the lease');
|
|
29
|
+
* break;
|
|
30
|
+
* }
|
|
31
|
+
* }
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export class WorkerError extends Error {
|
|
36
|
+
/**
|
|
37
|
+
* Error code for programmatic handling
|
|
38
|
+
*/
|
|
39
|
+
code;
|
|
40
|
+
/**
|
|
41
|
+
* Original error that caused this error (if any)
|
|
42
|
+
*/
|
|
43
|
+
cause;
|
|
44
|
+
/**
|
|
45
|
+
* Additional context for debugging
|
|
46
|
+
*/
|
|
47
|
+
context;
|
|
48
|
+
constructor(message, code, context, cause) {
|
|
49
|
+
super(message);
|
|
50
|
+
this.name = 'WorkerError';
|
|
51
|
+
this.code = code;
|
|
52
|
+
this.cause = cause;
|
|
53
|
+
this.context = context;
|
|
54
|
+
// Maintains proper stack trace for where error was thrown (V8 only)
|
|
55
|
+
if (Error.captureStackTrace) {
|
|
56
|
+
Error.captureStackTrace(this, WorkerError);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Create a JSON-serializable representation of the error
|
|
61
|
+
*/
|
|
62
|
+
toJSON() {
|
|
63
|
+
return {
|
|
64
|
+
name: this.name,
|
|
65
|
+
message: this.message,
|
|
66
|
+
code: this.code,
|
|
67
|
+
context: this.context,
|
|
68
|
+
cause: this.cause
|
|
69
|
+
? {
|
|
70
|
+
name: this.cause.name,
|
|
71
|
+
message: this.cause.message,
|
|
72
|
+
}
|
|
73
|
+
: undefined,
|
|
74
|
+
stack: this.stack,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Type guard to check if an error is a WorkerError
|
|
79
|
+
*/
|
|
80
|
+
static isWorkerError(error) {
|
|
81
|
+
return error instanceof WorkerError;
|
|
82
|
+
}
|
|
83
|
+
// ==================== Factory Methods ====================
|
|
84
|
+
/**
|
|
85
|
+
* Create error for worker not found
|
|
86
|
+
*/
|
|
87
|
+
static workerNotFound(workerId, context) {
|
|
88
|
+
return new WorkerError('Worker not found', 'WORKER_NOT_FOUND', { workerId, ...context });
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Create error for worker not running
|
|
92
|
+
*/
|
|
93
|
+
static workerNotRunning(workerId, currentState, context) {
|
|
94
|
+
return new WorkerError(`Worker is not running (current state: ${currentState})`, 'WORKER_NOT_RUNNING', { workerId, currentState, ...context });
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Create error for worker crash
|
|
98
|
+
*/
|
|
99
|
+
static workerCrashed(workerId, exitCode, signal, context) {
|
|
100
|
+
return new WorkerError(`Worker crashed (exit code: ${exitCode}, signal: ${signal})`, 'WORKER_CRASHED', { workerId, exitCode, signal, ...context });
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Create error for worker start failure
|
|
104
|
+
*/
|
|
105
|
+
static workerStartFailed(workerId, cause, context) {
|
|
106
|
+
return new WorkerError(`Failed to start worker: ${cause.message}`, 'WORKER_START_FAILED', { workerId, ...context }, cause);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Create error for worker stop failure
|
|
110
|
+
*/
|
|
111
|
+
static workerStopFailed(workerId, cause, context) {
|
|
112
|
+
return new WorkerError(`Failed to stop worker: ${cause.message}`, 'WORKER_STOP_FAILED', { workerId, ...context }, cause);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Create error for lease not found
|
|
116
|
+
*/
|
|
117
|
+
static leaseNotFound(tenantId, context) {
|
|
118
|
+
return new WorkerError('Lease not found for tenant', 'LEASE_NOT_FOUND', {
|
|
119
|
+
tenantId,
|
|
120
|
+
...context,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Create error for lease not held by controller
|
|
125
|
+
*/
|
|
126
|
+
static leaseNotHeld(tenantId, controllerId, actualControllerId, context) {
|
|
127
|
+
return new WorkerError('Lease is held by a different controller', 'LEASE_NOT_HELD', {
|
|
128
|
+
tenantId,
|
|
129
|
+
controllerId,
|
|
130
|
+
actualControllerId,
|
|
131
|
+
...context,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Create error for expired lease
|
|
136
|
+
*/
|
|
137
|
+
static leaseExpired(tenantId, expiredAt, context) {
|
|
138
|
+
return new WorkerError('Lease has expired', 'LEASE_EXPIRED', {
|
|
139
|
+
tenantId,
|
|
140
|
+
expiredAt,
|
|
141
|
+
...context,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Create error for lease already held
|
|
146
|
+
*/
|
|
147
|
+
static leaseAlreadyHeld(tenantId, heldByControllerId, context) {
|
|
148
|
+
return new WorkerError('Lease is already held by another controller', 'LEASE_ALREADY_HELD', {
|
|
149
|
+
tenantId,
|
|
150
|
+
heldByControllerId,
|
|
151
|
+
...context,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Create error for port exhaustion
|
|
156
|
+
*/
|
|
157
|
+
static portExhausted(portMin, portMax, context) {
|
|
158
|
+
return new WorkerError(`No available ports in range ${portMin}-${portMax}`, 'PORT_EXHAUSTED', {
|
|
159
|
+
portMin,
|
|
160
|
+
portMax,
|
|
161
|
+
...context,
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Create error for max workers reached
|
|
166
|
+
*/
|
|
167
|
+
static maxWorkersReached(maxWorkers, currentCount, context) {
|
|
168
|
+
return new WorkerError(`Maximum worker limit reached (${currentCount}/${maxWorkers})`, 'MAX_WORKERS_REACHED', { maxWorkers, currentCount, ...context });
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Create error for health check failure
|
|
172
|
+
*/
|
|
173
|
+
static healthCheckFailed(workerId, cause, context) {
|
|
174
|
+
return new WorkerError(`Health check failed: ${cause.message}`, 'HEALTH_CHECK_FAILED', { workerId, ...context }, cause);
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Create error for health check timeout
|
|
178
|
+
*/
|
|
179
|
+
static healthCheckTimeout(workerId, timeoutMs, context) {
|
|
180
|
+
return new WorkerError(`Health check timed out after ${timeoutMs}ms`, 'HEALTH_CHECK_TIMEOUT', {
|
|
181
|
+
workerId,
|
|
182
|
+
timeoutMs,
|
|
183
|
+
...context,
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Create error for invalid state transition
|
|
188
|
+
*/
|
|
189
|
+
static invalidState(currentState, attemptedOperation, context) {
|
|
190
|
+
return new WorkerError(`Invalid operation "${attemptedOperation}" in state "${currentState}"`, 'INVALID_STATE', { currentState, attemptedOperation, ...context });
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Create error for generic operation failure
|
|
194
|
+
*/
|
|
195
|
+
static operationFailed(operation, cause, context) {
|
|
196
|
+
return new WorkerError(`Operation failed: ${operation} - ${cause.message}`, 'OPERATION_FAILED', { operation, ...context }, cause);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
//# sourceMappingURL=worker.error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker.error.js","sourceRoot":"","sources":["../../../../src/worker/errors/worker.error.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AA2BH;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,OAAO,WAAY,SAAQ,KAAK;IACpC;;OAEG;IACM,IAAI,CAAkB;IAE/B;;OAEG;IACM,KAAK,CAAS;IAEvB;;OAEG;IACM,OAAO,CAA2B;IAE3C,YACE,OAAe,EACf,IAAqB,EACrB,OAAiC,EACjC,KAAa;QAEb,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,aAAa,CAAC;QAC1B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,KAAK,GAA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