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