agentxjs 1.0.2 → 1.1.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/browser.cjs +68 -718
- package/dist/browser.cjs.map +1 -1
- package/dist/browser.js +8 -702
- package/dist/browser.js.map +1 -1
- package/dist/index.cjs +109 -734
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +33 -715
- package/dist/index.js.map +1 -1
- package/package.json +17 -25
- package/LICENSE +0 -21
package/dist/browser.cjs
CHANGED
|
@@ -1,709 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
7
|
-
};
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
3
|
+
var agentx = require('@agentxjs/types/agentx');
|
|
4
|
+
var event = require('@agentxjs/types/event');
|
|
5
|
+
var common = require('@agentxjs/common');
|
|
12
6
|
|
|
13
|
-
//
|
|
14
|
-
|
|
15
|
-
return LoggerFactoryImpl.getLogger(name);
|
|
16
|
-
}
|
|
17
|
-
var __defProp2, __defNormalProp, __publicField, _ConsoleLogger, ConsoleLogger, externalFactory, LoggerFactoryImpl;
|
|
18
|
-
var init_dist = __esm({
|
|
19
|
-
"../common/dist/index.js"() {
|
|
20
|
-
__defProp2 = Object.defineProperty;
|
|
21
|
-
__defNormalProp = (obj, key, value) => key in obj ? __defProp2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
22
|
-
__publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
23
|
-
_ConsoleLogger = class _ConsoleLogger2 {
|
|
24
|
-
constructor(name, options = {}) {
|
|
25
|
-
__publicField(this, "name");
|
|
26
|
-
__publicField(this, "level");
|
|
27
|
-
__publicField(this, "colors");
|
|
28
|
-
__publicField(this, "timestamps");
|
|
29
|
-
this.name = name;
|
|
30
|
-
this.level = options.level ?? "info";
|
|
31
|
-
this.colors = options.colors ?? this.isNodeEnvironment();
|
|
32
|
-
this.timestamps = options.timestamps ?? true;
|
|
33
|
-
}
|
|
34
|
-
debug(message, context) {
|
|
35
|
-
if (this.isDebugEnabled()) {
|
|
36
|
-
this.log("DEBUG", message, context);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
info(message, context) {
|
|
40
|
-
if (this.isInfoEnabled()) {
|
|
41
|
-
this.log("INFO", message, context);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
warn(message, context) {
|
|
45
|
-
if (this.isWarnEnabled()) {
|
|
46
|
-
this.log("WARN", message, context);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
error(message, context) {
|
|
50
|
-
if (this.isErrorEnabled()) {
|
|
51
|
-
if (message instanceof Error) {
|
|
52
|
-
this.log("ERROR", message.message, { ...context, stack: message.stack });
|
|
53
|
-
} else {
|
|
54
|
-
this.log("ERROR", message, context);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
isDebugEnabled() {
|
|
59
|
-
return this.getLevelValue(this.level) <= this.getLevelValue("debug");
|
|
60
|
-
}
|
|
61
|
-
isInfoEnabled() {
|
|
62
|
-
return this.getLevelValue(this.level) <= this.getLevelValue("info");
|
|
63
|
-
}
|
|
64
|
-
isWarnEnabled() {
|
|
65
|
-
return this.getLevelValue(this.level) <= this.getLevelValue("warn");
|
|
66
|
-
}
|
|
67
|
-
isErrorEnabled() {
|
|
68
|
-
return this.getLevelValue(this.level) <= this.getLevelValue("error");
|
|
69
|
-
}
|
|
70
|
-
getLevelValue(level) {
|
|
71
|
-
const levels = {
|
|
72
|
-
debug: 0,
|
|
73
|
-
info: 1,
|
|
74
|
-
warn: 2,
|
|
75
|
-
error: 3,
|
|
76
|
-
silent: 4
|
|
77
|
-
};
|
|
78
|
-
return levels[level];
|
|
79
|
-
}
|
|
80
|
-
log(level, message, context) {
|
|
81
|
-
const parts = [];
|
|
82
|
-
if (this.timestamps) {
|
|
83
|
-
parts.push((/* @__PURE__ */ new Date()).toISOString());
|
|
84
|
-
}
|
|
85
|
-
if (this.colors) {
|
|
86
|
-
const color = _ConsoleLogger2.COLORS[level];
|
|
87
|
-
parts.push(`${color}${level.padEnd(5)}${_ConsoleLogger2.COLORS.RESET}`);
|
|
88
|
-
} else {
|
|
89
|
-
parts.push(level.padEnd(5));
|
|
90
|
-
}
|
|
91
|
-
parts.push(`[${this.name}]`);
|
|
92
|
-
parts.push(message);
|
|
93
|
-
const logLine = parts.join(" ");
|
|
94
|
-
const consoleMethod = this.getConsoleMethod(level);
|
|
95
|
-
if (context && Object.keys(context).length > 0) {
|
|
96
|
-
consoleMethod(logLine, context);
|
|
97
|
-
} else {
|
|
98
|
-
consoleMethod(logLine);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
getConsoleMethod(level) {
|
|
102
|
-
switch (level) {
|
|
103
|
-
case "DEBUG":
|
|
104
|
-
return console.debug.bind(console);
|
|
105
|
-
case "INFO":
|
|
106
|
-
return console.info.bind(console);
|
|
107
|
-
case "WARN":
|
|
108
|
-
return console.warn.bind(console);
|
|
109
|
-
case "ERROR":
|
|
110
|
-
return console.error.bind(console);
|
|
111
|
-
default:
|
|
112
|
-
return console.log.bind(console);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
isNodeEnvironment() {
|
|
116
|
-
return typeof process !== "undefined" && process.versions?.node !== void 0;
|
|
117
|
-
}
|
|
118
|
-
};
|
|
119
|
-
__publicField(_ConsoleLogger, "COLORS", {
|
|
120
|
-
DEBUG: "\x1B[36m",
|
|
121
|
-
INFO: "\x1B[32m",
|
|
122
|
-
WARN: "\x1B[33m",
|
|
123
|
-
ERROR: "\x1B[31m",
|
|
124
|
-
RESET: "\x1B[0m"
|
|
125
|
-
});
|
|
126
|
-
ConsoleLogger = _ConsoleLogger;
|
|
127
|
-
externalFactory = null;
|
|
128
|
-
LoggerFactoryImpl = class {
|
|
129
|
-
static getLogger(nameOrClass) {
|
|
130
|
-
const name = typeof nameOrClass === "string" ? nameOrClass : nameOrClass.name;
|
|
131
|
-
if (this.loggers.has(name)) {
|
|
132
|
-
return this.loggers.get(name);
|
|
133
|
-
}
|
|
134
|
-
const lazyLogger = this.createLazyLogger(name);
|
|
135
|
-
this.loggers.set(name, lazyLogger);
|
|
136
|
-
return lazyLogger;
|
|
137
|
-
}
|
|
138
|
-
static configure(config) {
|
|
139
|
-
this.config = { ...this.config, ...config };
|
|
140
|
-
}
|
|
141
|
-
static reset() {
|
|
142
|
-
this.loggers.clear();
|
|
143
|
-
this.config = { defaultLevel: "info" };
|
|
144
|
-
externalFactory = null;
|
|
145
|
-
}
|
|
146
|
-
static createLazyLogger(name) {
|
|
147
|
-
let realLogger = null;
|
|
148
|
-
const getRealLogger = () => {
|
|
149
|
-
if (!realLogger) {
|
|
150
|
-
realLogger = this.createLogger(name);
|
|
151
|
-
}
|
|
152
|
-
return realLogger;
|
|
153
|
-
};
|
|
154
|
-
return {
|
|
155
|
-
name,
|
|
156
|
-
level: this.config.defaultLevel || "info",
|
|
157
|
-
debug: (message, context) => getRealLogger().debug(message, context),
|
|
158
|
-
info: (message, context) => getRealLogger().info(message, context),
|
|
159
|
-
warn: (message, context) => getRealLogger().warn(message, context),
|
|
160
|
-
error: (message, context) => getRealLogger().error(message, context),
|
|
161
|
-
isDebugEnabled: () => getRealLogger().isDebugEnabled(),
|
|
162
|
-
isInfoEnabled: () => getRealLogger().isInfoEnabled(),
|
|
163
|
-
isWarnEnabled: () => getRealLogger().isWarnEnabled(),
|
|
164
|
-
isErrorEnabled: () => getRealLogger().isErrorEnabled()
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
static createLogger(name) {
|
|
168
|
-
if (externalFactory) {
|
|
169
|
-
return externalFactory.getLogger(name);
|
|
170
|
-
}
|
|
171
|
-
if (this.config.defaultImplementation) {
|
|
172
|
-
return this.config.defaultImplementation(name);
|
|
173
|
-
}
|
|
174
|
-
return new ConsoleLogger(name, {
|
|
175
|
-
level: this.config.defaultLevel,
|
|
176
|
-
...this.config.consoleOptions
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
};
|
|
180
|
-
__publicField(LoggerFactoryImpl, "loggers", /* @__PURE__ */ new Map());
|
|
181
|
-
__publicField(LoggerFactoryImpl, "config", {
|
|
182
|
-
defaultLevel: "info"
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
// ../network/dist/chunk-63P5VUHB.js
|
|
188
|
-
async function createWebSocketClient(options) {
|
|
189
|
-
if (isBrowser) {
|
|
190
|
-
const client = new BrowserWebSocketClient(options);
|
|
191
|
-
await client.connect();
|
|
192
|
-
return client;
|
|
193
|
-
} else {
|
|
194
|
-
const client = new WebSocketClient(options);
|
|
195
|
-
await client.connect();
|
|
196
|
-
return client;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
var __defProp3, __defNormalProp2, __publicField2, logger, isBrowser, WebSocketClient, BrowserWebSocketClient;
|
|
200
|
-
var init_chunk_63P5VUHB = __esm({
|
|
201
|
-
"../network/dist/chunk-63P5VUHB.js"() {
|
|
202
|
-
init_dist();
|
|
203
|
-
__defProp3 = Object.defineProperty;
|
|
204
|
-
__defNormalProp2 = (obj, key, value) => key in obj ? __defProp3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
205
|
-
__publicField2 = (obj, key, value) => __defNormalProp2(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
206
|
-
logger = createLogger("network/WebSocketClient");
|
|
207
|
-
isBrowser = typeof globalThis !== "undefined" && typeof globalThis.window !== "undefined" && typeof globalThis.window.WebSocket !== "undefined";
|
|
208
|
-
WebSocketClient = class {
|
|
209
|
-
constructor(options) {
|
|
210
|
-
__publicField2(this, "ws", null);
|
|
211
|
-
__publicField2(this, "serverUrl");
|
|
212
|
-
__publicField2(this, "messageHandlers", /* @__PURE__ */ new Set());
|
|
213
|
-
__publicField2(this, "openHandlers", /* @__PURE__ */ new Set());
|
|
214
|
-
__publicField2(this, "closeHandlers", /* @__PURE__ */ new Set());
|
|
215
|
-
__publicField2(this, "errorHandlers", /* @__PURE__ */ new Set());
|
|
216
|
-
if (isBrowser) {
|
|
217
|
-
throw new Error(
|
|
218
|
-
"Use createBrowserWebSocketClient() in browser environment for auto-reconnect support"
|
|
219
|
-
);
|
|
220
|
-
}
|
|
221
|
-
this.serverUrl = options.serverUrl;
|
|
222
|
-
}
|
|
223
|
-
get readyState() {
|
|
224
|
-
if (!this.ws) return "closed";
|
|
225
|
-
const state = this.ws.readyState;
|
|
226
|
-
if (state === 0) return "connecting";
|
|
227
|
-
if (state === 1) return "open";
|
|
228
|
-
if (state === 2) return "closing";
|
|
229
|
-
return "closed";
|
|
230
|
-
}
|
|
231
|
-
async connect() {
|
|
232
|
-
if (this.ws) {
|
|
233
|
-
throw new Error("Already connected or connecting");
|
|
234
|
-
}
|
|
235
|
-
const { WebSocket: NodeWebSocket } = await import('ws');
|
|
236
|
-
this.ws = new NodeWebSocket(this.serverUrl);
|
|
237
|
-
return new Promise((resolve, reject) => {
|
|
238
|
-
const onOpen = () => {
|
|
239
|
-
logger.info("WebSocket connected", { serverUrl: this.serverUrl });
|
|
240
|
-
for (const handler of this.openHandlers) {
|
|
241
|
-
handler();
|
|
242
|
-
}
|
|
243
|
-
resolve();
|
|
244
|
-
};
|
|
245
|
-
const onError = (err) => {
|
|
246
|
-
logger.error("WebSocket connection failed", {
|
|
247
|
-
serverUrl: this.serverUrl,
|
|
248
|
-
error: err?.message
|
|
249
|
-
});
|
|
250
|
-
reject(err || new Error("WebSocket connection failed"));
|
|
251
|
-
};
|
|
252
|
-
this.ws.once("open", onOpen);
|
|
253
|
-
this.ws.once("error", onError);
|
|
254
|
-
this.ws.on("message", (data) => {
|
|
255
|
-
const message = data.toString();
|
|
256
|
-
for (const handler of this.messageHandlers) {
|
|
257
|
-
handler(message);
|
|
258
|
-
}
|
|
259
|
-
});
|
|
260
|
-
this.ws.on("close", () => {
|
|
261
|
-
logger.warn("WebSocket closed");
|
|
262
|
-
for (const handler of this.closeHandlers) {
|
|
263
|
-
handler();
|
|
264
|
-
}
|
|
265
|
-
});
|
|
266
|
-
this.ws.on("error", (err) => {
|
|
267
|
-
logger.error("WebSocket error", { error: err.message });
|
|
268
|
-
for (const handler of this.errorHandlers) {
|
|
269
|
-
handler(err);
|
|
270
|
-
}
|
|
271
|
-
});
|
|
272
|
-
});
|
|
273
|
-
}
|
|
274
|
-
send(message) {
|
|
275
|
-
if (!this.ws || this.ws.readyState !== 1) {
|
|
276
|
-
throw new Error("WebSocket is not open");
|
|
277
|
-
}
|
|
278
|
-
this.ws.send(message);
|
|
279
|
-
}
|
|
280
|
-
onMessage(handler) {
|
|
281
|
-
this.messageHandlers.add(handler);
|
|
282
|
-
return () => {
|
|
283
|
-
this.messageHandlers.delete(handler);
|
|
284
|
-
};
|
|
285
|
-
}
|
|
286
|
-
onOpen(handler) {
|
|
287
|
-
this.openHandlers.add(handler);
|
|
288
|
-
return () => {
|
|
289
|
-
this.openHandlers.delete(handler);
|
|
290
|
-
};
|
|
291
|
-
}
|
|
292
|
-
onClose(handler) {
|
|
293
|
-
this.closeHandlers.add(handler);
|
|
294
|
-
return () => {
|
|
295
|
-
this.closeHandlers.delete(handler);
|
|
296
|
-
};
|
|
297
|
-
}
|
|
298
|
-
onError(handler) {
|
|
299
|
-
this.errorHandlers.add(handler);
|
|
300
|
-
return () => {
|
|
301
|
-
this.errorHandlers.delete(handler);
|
|
302
|
-
};
|
|
303
|
-
}
|
|
304
|
-
close() {
|
|
305
|
-
if (this.ws) {
|
|
306
|
-
this.ws.close();
|
|
307
|
-
this.ws = null;
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
dispose() {
|
|
311
|
-
this.close();
|
|
312
|
-
this.messageHandlers.clear();
|
|
313
|
-
this.openHandlers.clear();
|
|
314
|
-
this.closeHandlers.clear();
|
|
315
|
-
this.errorHandlers.clear();
|
|
316
|
-
}
|
|
317
|
-
};
|
|
318
|
-
BrowserWebSocketClient = class {
|
|
319
|
-
// Track if this is a reconnection
|
|
320
|
-
constructor(options) {
|
|
321
|
-
__publicField2(this, "ws", null);
|
|
322
|
-
__publicField2(this, "serverUrl");
|
|
323
|
-
__publicField2(this, "options");
|
|
324
|
-
__publicField2(this, "messageHandlers", /* @__PURE__ */ new Set());
|
|
325
|
-
__publicField2(this, "openHandlers", /* @__PURE__ */ new Set());
|
|
326
|
-
__publicField2(this, "closeHandlers", /* @__PURE__ */ new Set());
|
|
327
|
-
__publicField2(this, "errorHandlers", /* @__PURE__ */ new Set());
|
|
328
|
-
__publicField2(this, "hasConnectedBefore", false);
|
|
329
|
-
if (!isBrowser) {
|
|
330
|
-
throw new Error("BrowserWebSocketClient can only be used in browser environment");
|
|
331
|
-
}
|
|
332
|
-
this.serverUrl = options.serverUrl;
|
|
333
|
-
this.options = {
|
|
334
|
-
autoReconnect: true,
|
|
335
|
-
minReconnectionDelay: 1e3,
|
|
336
|
-
maxReconnectionDelay: 1e4,
|
|
337
|
-
maxRetries: Infinity,
|
|
338
|
-
connectionTimeout: 4e3,
|
|
339
|
-
debug: false,
|
|
340
|
-
...options
|
|
341
|
-
};
|
|
342
|
-
}
|
|
343
|
-
get readyState() {
|
|
344
|
-
if (!this.ws) return "closed";
|
|
345
|
-
const state = this.ws.readyState;
|
|
346
|
-
if (state === 0) return "connecting";
|
|
347
|
-
if (state === 1) return "open";
|
|
348
|
-
if (state === 2) return "closing";
|
|
349
|
-
return "closed";
|
|
350
|
-
}
|
|
351
|
-
async connect() {
|
|
352
|
-
if (this.ws) {
|
|
353
|
-
throw new Error("Already connected or connecting");
|
|
354
|
-
}
|
|
355
|
-
if (this.options.autoReconnect) {
|
|
356
|
-
const ReconnectingWebSocket = (await import('reconnecting-websocket')).default;
|
|
357
|
-
this.ws = new ReconnectingWebSocket(this.serverUrl, [], {
|
|
358
|
-
maxReconnectionDelay: this.options.maxReconnectionDelay,
|
|
359
|
-
minReconnectionDelay: this.options.minReconnectionDelay,
|
|
360
|
-
reconnectionDelayGrowFactor: 1.3,
|
|
361
|
-
connectionTimeout: this.options.connectionTimeout,
|
|
362
|
-
maxRetries: this.options.maxRetries,
|
|
363
|
-
debug: this.options.debug
|
|
364
|
-
});
|
|
365
|
-
} else {
|
|
366
|
-
this.ws = new WebSocket(this.serverUrl);
|
|
367
|
-
}
|
|
368
|
-
return new Promise((resolve, reject) => {
|
|
369
|
-
const onOpen = () => {
|
|
370
|
-
if (this.hasConnectedBefore) {
|
|
371
|
-
logger.info("WebSocket reconnected successfully", { serverUrl: this.serverUrl });
|
|
372
|
-
} else {
|
|
373
|
-
logger.info("WebSocket connected", { serverUrl: this.serverUrl });
|
|
374
|
-
this.hasConnectedBefore = true;
|
|
375
|
-
}
|
|
376
|
-
for (const handler of this.openHandlers) {
|
|
377
|
-
handler();
|
|
378
|
-
}
|
|
379
|
-
resolve();
|
|
380
|
-
};
|
|
381
|
-
const onError = (_event) => {
|
|
382
|
-
logger.error("WebSocket connection failed", { serverUrl: this.serverUrl });
|
|
383
|
-
const error = new Error("WebSocket connection failed");
|
|
384
|
-
for (const handler of this.errorHandlers) {
|
|
385
|
-
handler(error);
|
|
386
|
-
}
|
|
387
|
-
reject(error);
|
|
388
|
-
};
|
|
389
|
-
this.ws.addEventListener("open", onOpen, { once: true });
|
|
390
|
-
this.ws.addEventListener("error", onError, { once: true });
|
|
391
|
-
this.ws.addEventListener("message", ((event) => {
|
|
392
|
-
const message = event.data;
|
|
393
|
-
for (const handler of this.messageHandlers) {
|
|
394
|
-
handler(message);
|
|
395
|
-
}
|
|
396
|
-
}));
|
|
397
|
-
this.ws.addEventListener("close", (() => {
|
|
398
|
-
logger.info("WebSocket closed, attempting to reconnect...");
|
|
399
|
-
for (const handler of this.closeHandlers) {
|
|
400
|
-
handler();
|
|
401
|
-
}
|
|
402
|
-
}));
|
|
403
|
-
this.ws.addEventListener("error", ((_event) => {
|
|
404
|
-
logger.error("WebSocket error");
|
|
405
|
-
const error = new Error("WebSocket error");
|
|
406
|
-
for (const handler of this.errorHandlers) {
|
|
407
|
-
handler(error);
|
|
408
|
-
}
|
|
409
|
-
}));
|
|
410
|
-
});
|
|
411
|
-
}
|
|
412
|
-
send(message) {
|
|
413
|
-
if (!this.ws || this.ws.readyState !== 1) {
|
|
414
|
-
throw new Error("WebSocket is not open");
|
|
415
|
-
}
|
|
416
|
-
this.ws.send(message);
|
|
417
|
-
}
|
|
418
|
-
onMessage(handler) {
|
|
419
|
-
this.messageHandlers.add(handler);
|
|
420
|
-
return () => {
|
|
421
|
-
this.messageHandlers.delete(handler);
|
|
422
|
-
};
|
|
423
|
-
}
|
|
424
|
-
onOpen(handler) {
|
|
425
|
-
this.openHandlers.add(handler);
|
|
426
|
-
return () => {
|
|
427
|
-
this.openHandlers.delete(handler);
|
|
428
|
-
};
|
|
429
|
-
}
|
|
430
|
-
onClose(handler) {
|
|
431
|
-
this.closeHandlers.add(handler);
|
|
432
|
-
return () => {
|
|
433
|
-
this.closeHandlers.delete(handler);
|
|
434
|
-
};
|
|
435
|
-
}
|
|
436
|
-
onError(handler) {
|
|
437
|
-
this.errorHandlers.add(handler);
|
|
438
|
-
return () => {
|
|
439
|
-
this.errorHandlers.delete(handler);
|
|
440
|
-
};
|
|
441
|
-
}
|
|
442
|
-
close() {
|
|
443
|
-
if (this.ws) {
|
|
444
|
-
this.ws.close();
|
|
445
|
-
this.ws = null;
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
dispose() {
|
|
449
|
-
this.close();
|
|
450
|
-
this.messageHandlers.clear();
|
|
451
|
-
this.openHandlers.clear();
|
|
452
|
-
this.closeHandlers.clear();
|
|
453
|
-
this.errorHandlers.clear();
|
|
454
|
-
}
|
|
455
|
-
};
|
|
456
|
-
}
|
|
457
|
-
});
|
|
458
|
-
|
|
459
|
-
// ../network/dist/index.js
|
|
460
|
-
var dist_exports = {};
|
|
461
|
-
__export(dist_exports, {
|
|
462
|
-
WebSocketClient: () => WebSocketClient,
|
|
463
|
-
WebSocketServer: () => WebSocketServer,
|
|
464
|
-
createWebSocketClient: () => createWebSocketClient
|
|
465
|
-
});
|
|
466
|
-
var logger2, WebSocketConnection, WebSocketServer;
|
|
467
|
-
var init_dist2 = __esm({
|
|
468
|
-
"../network/dist/index.js"() {
|
|
469
|
-
init_chunk_63P5VUHB();
|
|
470
|
-
init_dist();
|
|
471
|
-
logger2 = createLogger("network/WebSocketServer");
|
|
472
|
-
WebSocketConnection = class {
|
|
473
|
-
constructor(ws, options) {
|
|
474
|
-
__publicField2(this, "id");
|
|
475
|
-
__publicField2(this, "ws");
|
|
476
|
-
__publicField2(this, "messageHandlers", /* @__PURE__ */ new Set());
|
|
477
|
-
__publicField2(this, "closeHandlers", /* @__PURE__ */ new Set());
|
|
478
|
-
__publicField2(this, "errorHandlers", /* @__PURE__ */ new Set());
|
|
479
|
-
__publicField2(this, "heartbeatInterval");
|
|
480
|
-
__publicField2(this, "isAlive", true);
|
|
481
|
-
this.ws = ws;
|
|
482
|
-
this.id = `conn_${Date.now()}_${Math.random().toString(36).substring(2, 8)}`;
|
|
483
|
-
if (options.heartbeat !== false) {
|
|
484
|
-
const interval = options.heartbeatInterval || 3e4;
|
|
485
|
-
ws.on("pong", () => {
|
|
486
|
-
this.isAlive = true;
|
|
487
|
-
logger2.debug("Heartbeat pong received", { id: this.id });
|
|
488
|
-
});
|
|
489
|
-
this.heartbeatInterval = setInterval(() => {
|
|
490
|
-
if (!this.isAlive) {
|
|
491
|
-
logger2.warn("Client heartbeat timeout, terminating connection", { id: this.id });
|
|
492
|
-
clearInterval(this.heartbeatInterval);
|
|
493
|
-
ws.terminate();
|
|
494
|
-
return;
|
|
495
|
-
}
|
|
496
|
-
this.isAlive = false;
|
|
497
|
-
ws.ping();
|
|
498
|
-
logger2.debug("Heartbeat ping sent", { id: this.id });
|
|
499
|
-
}, interval);
|
|
500
|
-
}
|
|
501
|
-
ws.on("message", (data) => {
|
|
502
|
-
const message = data.toString();
|
|
503
|
-
for (const handler of this.messageHandlers) {
|
|
504
|
-
handler(message);
|
|
505
|
-
}
|
|
506
|
-
});
|
|
507
|
-
ws.on("close", () => {
|
|
508
|
-
if (this.heartbeatInterval) {
|
|
509
|
-
clearInterval(this.heartbeatInterval);
|
|
510
|
-
}
|
|
511
|
-
for (const handler of this.closeHandlers) {
|
|
512
|
-
handler();
|
|
513
|
-
}
|
|
514
|
-
});
|
|
515
|
-
ws.on("error", (err) => {
|
|
516
|
-
if (this.heartbeatInterval) {
|
|
517
|
-
clearInterval(this.heartbeatInterval);
|
|
518
|
-
}
|
|
519
|
-
for (const handler of this.errorHandlers) {
|
|
520
|
-
handler(err);
|
|
521
|
-
}
|
|
522
|
-
});
|
|
523
|
-
}
|
|
524
|
-
send(message) {
|
|
525
|
-
if (this.ws.readyState === 1) {
|
|
526
|
-
this.ws.send(message);
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
onMessage(handler) {
|
|
530
|
-
this.messageHandlers.add(handler);
|
|
531
|
-
return () => {
|
|
532
|
-
this.messageHandlers.delete(handler);
|
|
533
|
-
};
|
|
534
|
-
}
|
|
535
|
-
onClose(handler) {
|
|
536
|
-
this.closeHandlers.add(handler);
|
|
537
|
-
return () => {
|
|
538
|
-
this.closeHandlers.delete(handler);
|
|
539
|
-
};
|
|
540
|
-
}
|
|
541
|
-
onError(handler) {
|
|
542
|
-
this.errorHandlers.add(handler);
|
|
543
|
-
return () => {
|
|
544
|
-
this.errorHandlers.delete(handler);
|
|
545
|
-
};
|
|
546
|
-
}
|
|
547
|
-
close() {
|
|
548
|
-
if (this.heartbeatInterval) {
|
|
549
|
-
clearInterval(this.heartbeatInterval);
|
|
550
|
-
}
|
|
551
|
-
this.ws.close();
|
|
552
|
-
}
|
|
553
|
-
};
|
|
554
|
-
WebSocketServer = class {
|
|
555
|
-
constructor(options = {}) {
|
|
556
|
-
__publicField2(this, "wss", null);
|
|
557
|
-
__publicField2(this, "connections", /* @__PURE__ */ new Set());
|
|
558
|
-
__publicField2(this, "connectionHandlers", /* @__PURE__ */ new Set());
|
|
559
|
-
__publicField2(this, "options");
|
|
560
|
-
__publicField2(this, "attachedToServer", false);
|
|
561
|
-
this.options = options;
|
|
562
|
-
}
|
|
563
|
-
async listen(port, host = "0.0.0.0") {
|
|
564
|
-
if (this.wss) {
|
|
565
|
-
throw new Error("Server already listening");
|
|
566
|
-
}
|
|
567
|
-
if (this.attachedToServer) {
|
|
568
|
-
throw new Error(
|
|
569
|
-
"Cannot listen when attached to existing server. The server should call listen() instead."
|
|
570
|
-
);
|
|
571
|
-
}
|
|
572
|
-
const { WebSocketServer: WSS } = await import('ws');
|
|
573
|
-
this.wss = new WSS({ port, host });
|
|
574
|
-
this.wss.on("connection", (ws) => {
|
|
575
|
-
this.handleConnection(ws);
|
|
576
|
-
});
|
|
577
|
-
logger2.info("WebSocket server listening", { port, host });
|
|
578
|
-
}
|
|
579
|
-
attach(server, path = "/ws") {
|
|
580
|
-
if (this.wss) {
|
|
581
|
-
throw new Error("Server already initialized");
|
|
582
|
-
}
|
|
583
|
-
import('ws').then(({ WebSocketServer: WSS }) => {
|
|
584
|
-
this.wss = new WSS({ noServer: true });
|
|
585
|
-
server.on("upgrade", (request, socket, head) => {
|
|
586
|
-
const url = new URL(request.url || "", `http://${request.headers.host}`);
|
|
587
|
-
if (url.pathname === path) {
|
|
588
|
-
this.wss.handleUpgrade(request, socket, head, (ws) => {
|
|
589
|
-
this.wss.emit("connection", ws, request);
|
|
590
|
-
});
|
|
591
|
-
} else {
|
|
592
|
-
socket.destroy();
|
|
593
|
-
}
|
|
594
|
-
});
|
|
595
|
-
this.wss.on("connection", (ws) => {
|
|
596
|
-
this.handleConnection(ws);
|
|
597
|
-
});
|
|
598
|
-
this.attachedToServer = true;
|
|
599
|
-
logger2.info("WebSocket attached to existing HTTP server", { path });
|
|
600
|
-
});
|
|
601
|
-
}
|
|
602
|
-
handleConnection(ws) {
|
|
603
|
-
const connection = new WebSocketConnection(ws, this.options);
|
|
604
|
-
this.connections.add(connection);
|
|
605
|
-
logger2.info("Client connected", {
|
|
606
|
-
connectionId: connection.id,
|
|
607
|
-
totalConnections: this.connections.size
|
|
608
|
-
});
|
|
609
|
-
connection.onClose(() => {
|
|
610
|
-
this.connections.delete(connection);
|
|
611
|
-
logger2.info("Client disconnected", {
|
|
612
|
-
connectionId: connection.id,
|
|
613
|
-
totalConnections: this.connections.size
|
|
614
|
-
});
|
|
615
|
-
});
|
|
616
|
-
for (const handler of this.connectionHandlers) {
|
|
617
|
-
handler(connection);
|
|
618
|
-
}
|
|
619
|
-
}
|
|
620
|
-
onConnection(handler) {
|
|
621
|
-
this.connectionHandlers.add(handler);
|
|
622
|
-
return () => {
|
|
623
|
-
this.connectionHandlers.delete(handler);
|
|
624
|
-
};
|
|
625
|
-
}
|
|
626
|
-
broadcast(message) {
|
|
627
|
-
for (const connection of this.connections) {
|
|
628
|
-
connection.send(message);
|
|
629
|
-
}
|
|
630
|
-
}
|
|
631
|
-
async close() {
|
|
632
|
-
if (!this.wss) return;
|
|
633
|
-
for (const connection of this.connections) {
|
|
634
|
-
connection.close();
|
|
635
|
-
}
|
|
636
|
-
this.connections.clear();
|
|
637
|
-
if (!this.attachedToServer) {
|
|
638
|
-
await new Promise((resolve) => {
|
|
639
|
-
this.wss.close(() => resolve());
|
|
640
|
-
});
|
|
641
|
-
}
|
|
642
|
-
this.wss = null;
|
|
643
|
-
}
|
|
644
|
-
async dispose() {
|
|
645
|
-
await this.close();
|
|
646
|
-
this.connectionHandlers.clear();
|
|
647
|
-
}
|
|
648
|
-
};
|
|
649
|
-
}
|
|
650
|
-
});
|
|
651
|
-
|
|
652
|
-
// ../types/dist/agentx.js
|
|
653
|
-
function isRemoteConfig(config) {
|
|
654
|
-
return "serverUrl" in config && typeof config.serverUrl === "string";
|
|
655
|
-
}
|
|
656
|
-
function isLocalConfig(config) {
|
|
657
|
-
return !isRemoteConfig(config);
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
// ../types/dist/event.js
|
|
661
|
-
function isFromSource(event, source) {
|
|
662
|
-
return event.source === source;
|
|
663
|
-
}
|
|
664
|
-
function hasIntent(event, intent) {
|
|
665
|
-
return event.intent === intent;
|
|
666
|
-
}
|
|
667
|
-
function isRequest(event) {
|
|
668
|
-
return event.intent === "request";
|
|
669
|
-
}
|
|
670
|
-
function isResult(event) {
|
|
671
|
-
return event.intent === "result";
|
|
672
|
-
}
|
|
673
|
-
function isNotification(event) {
|
|
674
|
-
return event.intent === "notification";
|
|
675
|
-
}
|
|
676
|
-
function isCommandEvent(event) {
|
|
677
|
-
return event.source === "command";
|
|
678
|
-
}
|
|
679
|
-
function isCommandRequest(event) {
|
|
680
|
-
return event.source === "command" && event.category === "request";
|
|
681
|
-
}
|
|
682
|
-
function isCommandResponse(event) {
|
|
683
|
-
return event.source === "command" && event.category === "response";
|
|
684
|
-
}
|
|
685
|
-
function isAgentStreamEvent(event) {
|
|
686
|
-
return event.source === "agent" && event.category === "stream";
|
|
687
|
-
}
|
|
688
|
-
function isAgentStateEvent(event) {
|
|
689
|
-
return event.source === "agent" && event.category === "state";
|
|
690
|
-
}
|
|
691
|
-
function isAgentMessageEvent(event) {
|
|
692
|
-
return event.source === "agent" && event.category === "message";
|
|
693
|
-
}
|
|
694
|
-
function isAgentTurnEvent(event) {
|
|
695
|
-
return event.source === "agent" && event.category === "turn";
|
|
696
|
-
}
|
|
697
|
-
function isAgentEvent(event) {
|
|
698
|
-
return event.source === "agent";
|
|
699
|
-
}
|
|
700
|
-
|
|
701
|
-
// src/createAgentX.ts
|
|
702
|
-
init_dist();
|
|
703
|
-
var remoteLogger = createLogger("agentx/RemoteClient");
|
|
7
|
+
// src/browser.ts
|
|
8
|
+
var remoteLogger = common.createLogger("agentx/RemoteClient");
|
|
704
9
|
async function createRemoteAgentX(serverUrl) {
|
|
705
|
-
const { createWebSocketClient
|
|
706
|
-
const client = await
|
|
10
|
+
const { createWebSocketClient } = await import('@agentxjs/network');
|
|
11
|
+
const client = await createWebSocketClient({
|
|
707
12
|
serverUrl,
|
|
708
13
|
autoReconnect: true,
|
|
709
14
|
minReconnectionDelay: 1e3,
|
|
@@ -830,7 +135,7 @@ async function createRemoteAgentX(serverUrl) {
|
|
|
830
135
|
|
|
831
136
|
// src/browser.ts
|
|
832
137
|
async function createAgentX(config) {
|
|
833
|
-
if (!config || !isRemoteConfig(config)) {
|
|
138
|
+
if (!config || !agentx.isRemoteConfig(config)) {
|
|
834
139
|
throw new Error(
|
|
835
140
|
'Browser environment only supports remote mode. Please provide { serverUrl: "ws://..." } configuration.'
|
|
836
141
|
);
|
|
@@ -838,22 +143,67 @@ async function createAgentX(config) {
|
|
|
838
143
|
return createRemoteAgentX(config.serverUrl);
|
|
839
144
|
}
|
|
840
145
|
|
|
146
|
+
Object.defineProperty(exports, "isLocalConfig", {
|
|
147
|
+
enumerable: true,
|
|
148
|
+
get: function () { return agentx.isLocalConfig; }
|
|
149
|
+
});
|
|
150
|
+
Object.defineProperty(exports, "isRemoteConfig", {
|
|
151
|
+
enumerable: true,
|
|
152
|
+
get: function () { return agentx.isRemoteConfig; }
|
|
153
|
+
});
|
|
154
|
+
Object.defineProperty(exports, "hasIntent", {
|
|
155
|
+
enumerable: true,
|
|
156
|
+
get: function () { return event.hasIntent; }
|
|
157
|
+
});
|
|
158
|
+
Object.defineProperty(exports, "isAgentEvent", {
|
|
159
|
+
enumerable: true,
|
|
160
|
+
get: function () { return event.isAgentEvent; }
|
|
161
|
+
});
|
|
162
|
+
Object.defineProperty(exports, "isAgentMessageEvent", {
|
|
163
|
+
enumerable: true,
|
|
164
|
+
get: function () { return event.isAgentMessageEvent; }
|
|
165
|
+
});
|
|
166
|
+
Object.defineProperty(exports, "isAgentStateEvent", {
|
|
167
|
+
enumerable: true,
|
|
168
|
+
get: function () { return event.isAgentStateEvent; }
|
|
169
|
+
});
|
|
170
|
+
Object.defineProperty(exports, "isAgentStreamEvent", {
|
|
171
|
+
enumerable: true,
|
|
172
|
+
get: function () { return event.isAgentStreamEvent; }
|
|
173
|
+
});
|
|
174
|
+
Object.defineProperty(exports, "isAgentTurnEvent", {
|
|
175
|
+
enumerable: true,
|
|
176
|
+
get: function () { return event.isAgentTurnEvent; }
|
|
177
|
+
});
|
|
178
|
+
Object.defineProperty(exports, "isCommandEvent", {
|
|
179
|
+
enumerable: true,
|
|
180
|
+
get: function () { return event.isCommandEvent; }
|
|
181
|
+
});
|
|
182
|
+
Object.defineProperty(exports, "isCommandRequest", {
|
|
183
|
+
enumerable: true,
|
|
184
|
+
get: function () { return event.isCommandRequest; }
|
|
185
|
+
});
|
|
186
|
+
Object.defineProperty(exports, "isCommandResponse", {
|
|
187
|
+
enumerable: true,
|
|
188
|
+
get: function () { return event.isCommandResponse; }
|
|
189
|
+
});
|
|
190
|
+
Object.defineProperty(exports, "isFromSource", {
|
|
191
|
+
enumerable: true,
|
|
192
|
+
get: function () { return event.isFromSource; }
|
|
193
|
+
});
|
|
194
|
+
Object.defineProperty(exports, "isNotification", {
|
|
195
|
+
enumerable: true,
|
|
196
|
+
get: function () { return event.isNotification; }
|
|
197
|
+
});
|
|
198
|
+
Object.defineProperty(exports, "isRequest", {
|
|
199
|
+
enumerable: true,
|
|
200
|
+
get: function () { return event.isRequest; }
|
|
201
|
+
});
|
|
202
|
+
Object.defineProperty(exports, "isResult", {
|
|
203
|
+
enumerable: true,
|
|
204
|
+
get: function () { return event.isResult; }
|
|
205
|
+
});
|
|
841
206
|
exports.createAgentX = createAgentX;
|
|
842
207
|
exports.createRemoteAgentX = createRemoteAgentX;
|
|
843
|
-
exports.hasIntent = hasIntent;
|
|
844
|
-
exports.isAgentEvent = isAgentEvent;
|
|
845
|
-
exports.isAgentMessageEvent = isAgentMessageEvent;
|
|
846
|
-
exports.isAgentStateEvent = isAgentStateEvent;
|
|
847
|
-
exports.isAgentStreamEvent = isAgentStreamEvent;
|
|
848
|
-
exports.isAgentTurnEvent = isAgentTurnEvent;
|
|
849
|
-
exports.isCommandEvent = isCommandEvent;
|
|
850
|
-
exports.isCommandRequest = isCommandRequest;
|
|
851
|
-
exports.isCommandResponse = isCommandResponse;
|
|
852
|
-
exports.isFromSource = isFromSource;
|
|
853
|
-
exports.isLocalConfig = isLocalConfig;
|
|
854
|
-
exports.isNotification = isNotification;
|
|
855
|
-
exports.isRemoteConfig = isRemoteConfig;
|
|
856
|
-
exports.isRequest = isRequest;
|
|
857
|
-
exports.isResult = isResult;
|
|
858
208
|
//# sourceMappingURL=browser.cjs.map
|
|
859
209
|
//# sourceMappingURL=browser.cjs.map
|