@holocronlab/botruntime-chat 0.5.5 → 0.5.6

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/index.cjs CHANGED
@@ -1421,6 +1421,60 @@ var maxBodyLength = _100mb;
1421
1421
  var maxContentLength = _100mb;
1422
1422
  var defaultTimeout = 6e4;
1423
1423
  var _createAuthClient = Symbol("_createAuthClient");
1424
+ var PollingSignalListener = class extends EventEmitter {
1425
+ _client;
1426
+ _conversationId;
1427
+ _userKey;
1428
+ _timer;
1429
+ _seen = /* @__PURE__ */ new Set();
1430
+ _status = "disconnected";
1431
+ constructor(client, conversationId, userKey) {
1432
+ super();
1433
+ this._client = client;
1434
+ this._conversationId = conversationId;
1435
+ this._userKey = userKey;
1436
+ }
1437
+ static listen = async (client, conversationId, userKey) => {
1438
+ const listener = new PollingSignalListener(client, conversationId, userKey);
1439
+ await listener.connect();
1440
+ return listener;
1441
+ };
1442
+ get status() {
1443
+ return this._status;
1444
+ }
1445
+ connect = async () => {
1446
+ if (this._status === "connected") return;
1447
+ const initial = await this._client.listMessages({
1448
+ conversationId: this._conversationId,
1449
+ "x-user-key": this._userKey
1450
+ });
1451
+ for (const message of initial.messages) this._seen.add(message.id);
1452
+ this._status = "connected";
1453
+ this._timer = setInterval(() => void this._poll(), 500);
1454
+ };
1455
+ disconnect = async () => {
1456
+ if (this._timer) clearInterval(this._timer);
1457
+ this._timer = void 0;
1458
+ this._status = "disconnected";
1459
+ };
1460
+ _poll = async () => {
1461
+ if (this._status !== "connected") return;
1462
+ try {
1463
+ const result = await this._client.listMessages({
1464
+ conversationId: this._conversationId,
1465
+ "x-user-key": this._userKey
1466
+ });
1467
+ const fresh = result.messages.filter((message) => !this._seen.has(message.id)).sort((a, b) => String(a.createdAt).localeCompare(String(b.createdAt)));
1468
+ for (const message of fresh) {
1469
+ this._seen.add(message.id);
1470
+ this.emit("message_created", message);
1471
+ }
1472
+ } catch (error) {
1473
+ await this.disconnect();
1474
+ this.emit("error", error instanceof Error ? error : new Error(String(error)));
1475
+ }
1476
+ };
1477
+ };
1424
1478
  var Client2 = class _Client {
1425
1479
  constructor(props) {
1426
1480
  this.props = props;
@@ -1494,13 +1548,7 @@ var Client2 = class _Client {
1494
1548
  };
1495
1549
  }
1496
1550
  listenConversation = async ({ id, "x-user-key": userKey }) => {
1497
- const signalListener = await SignalListener.listen({
1498
- url: this._apiUrl,
1499
- conversationId: id,
1500
- userKey,
1501
- debug: this.props.debug ?? false
1502
- });
1503
- return signalListener;
1551
+ return PollingSignalListener.listen(this, id, userKey);
1504
1552
  };
1505
1553
  _call = async (operation, args) => {
1506
1554
  try {
package/dist/index.mjs CHANGED
@@ -138572,6 +138572,54 @@ var maxBodyLength = _100mb;
138572
138572
  var maxContentLength = _100mb;
138573
138573
  var defaultTimeout = 6e4;
138574
138574
  var _createAuthClient = Symbol("_createAuthClient");
138575
+ var PollingSignalListener = class extends EventEmitter {
138576
+ _client;
138577
+ _conversationId;
138578
+ _userKey;
138579
+ _timer;
138580
+ _seen = /* @__PURE__ */ new Set();
138581
+ _status = "disconnected";
138582
+ constructor(client, conversationId, userKey) {
138583
+ super();
138584
+ this._client = client;
138585
+ this._conversationId = conversationId;
138586
+ this._userKey = userKey;
138587
+ }
138588
+ static listen = async (client, conversationId, userKey) => {
138589
+ const listener = new PollingSignalListener(client, conversationId, userKey);
138590
+ await listener.connect();
138591
+ return listener;
138592
+ };
138593
+ get status() {
138594
+ return this._status;
138595
+ }
138596
+ connect = async () => {
138597
+ if (this._status === "connected") return;
138598
+ const initial = await this._client.listMessages({ conversationId: this._conversationId, "x-user-key": this._userKey });
138599
+ for (const message of initial.messages) this._seen.add(message.id);
138600
+ this._status = "connected";
138601
+ this._timer = setInterval(() => void this._poll(), 500);
138602
+ };
138603
+ disconnect = async () => {
138604
+ if (this._timer) clearInterval(this._timer);
138605
+ this._timer = void 0;
138606
+ this._status = "disconnected";
138607
+ };
138608
+ _poll = async () => {
138609
+ if (this._status !== "connected") return;
138610
+ try {
138611
+ const result = await this._client.listMessages({ conversationId: this._conversationId, "x-user-key": this._userKey });
138612
+ const fresh = result.messages.filter((message) => !this._seen.has(message.id)).sort((a, b) => String(a.createdAt).localeCompare(String(b.createdAt)));
138613
+ for (const message of fresh) {
138614
+ this._seen.add(message.id);
138615
+ this.emit("message_created", message);
138616
+ }
138617
+ } catch (error) {
138618
+ await this.disconnect();
138619
+ this.emit("error", error instanceof Error ? error : new Error(String(error)));
138620
+ }
138621
+ };
138622
+ };
138575
138623
  var Client2 = class _Client {
138576
138624
  constructor(props) {
138577
138625
  this.props = props;
@@ -138645,13 +138693,7 @@ var Client2 = class _Client {
138645
138693
  };
138646
138694
  }
138647
138695
  listenConversation = async ({ id, "x-user-key": userKey }) => {
138648
- const signalListener = await SignalListener.listen({
138649
- url: this._apiUrl,
138650
- conversationId: id,
138651
- userKey,
138652
- debug: this.props.debug ?? false
138653
- });
138654
- return signalListener;
138696
+ return PollingSignalListener.listen(this, id, userKey);
138655
138697
  };
138656
138698
  _call = async (operation, args) => {
138657
138699
  try {
package/package.json CHANGED
@@ -1,11 +1,14 @@
1
1
  {
2
2
  "name": "@holocronlab/botruntime-chat",
3
- "version": "0.5.5",
3
+ "version": "0.5.6",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "registry": "https://registry.npmjs.org"
7
7
  },
8
8
  "description": "Chat API client used by the brt CLI (vendored dist of the upstream chat client, MIT).",
9
+ "scripts": {
10
+ "test": "bun test"
11
+ },
9
12
  "main": "./dist/index.cjs",
10
13
  "module": "./dist/index.mjs",
11
14
  "types": "./dist/index.d.ts",