@arbidocs/sdk 0.3.75 → 0.3.77

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
@@ -4408,6 +4408,12 @@ function formatStreamSummary(result, elapsedTime) {
4408
4408
  var consumeSSEStream = streamSSE;
4409
4409
  var AUTH_TIMEOUT_MS = 1e4;
4410
4410
  var MAX_BACKOFF_MS = 3e4;
4411
+ var WebSocketAuthError = class extends Error {
4412
+ constructor(reason) {
4413
+ super(`WebSocket auth failed: ${reason}`);
4414
+ this.name = "WebSocketAuthError";
4415
+ }
4416
+ };
4411
4417
  function connectWebSocket(options) {
4412
4418
  const { baseUrl, accessToken, onMessage, onClose } = options;
4413
4419
  const url = client.buildWebSocketUrl(baseUrl);
@@ -4435,7 +4441,7 @@ function connectWebSocket(options) {
4435
4441
  resolve2({ close: () => ws.close() });
4436
4442
  } else {
4437
4443
  ws.close();
4438
- reject(new Error(`WebSocket auth failed: ${msg.reason || "unknown"}`));
4444
+ reject(new WebSocketAuthError(msg.reason || "unknown"));
4439
4445
  }
4440
4446
  }
4441
4447
  return;
@@ -4458,52 +4464,66 @@ async function connectWithReconnect(options) {
4458
4464
  const {
4459
4465
  maxRetries = 10,
4460
4466
  initialDelayMs = 1e3,
4467
+ getAccessToken,
4468
+ refreshAuth,
4461
4469
  onReconnecting,
4462
4470
  onReconnected,
4463
4471
  onReconnectFailed,
4464
4472
  onClose,
4465
- ...connectOpts
4473
+ onMessage,
4474
+ baseUrl,
4475
+ accessToken
4466
4476
  } = options;
4477
+ const resolveToken = getAccessToken ?? (() => accessToken);
4467
4478
  let closed = false;
4468
- let reconnectTimer = null;
4479
+ let cancelDelay = null;
4469
4480
  let currentConnection = null;
4481
+ const connectOnce = async () => {
4482
+ const token = await resolveToken();
4483
+ if (!token) throw new WebSocketAuthError("no access token available");
4484
+ return connectWebSocket({
4485
+ baseUrl,
4486
+ accessToken: token,
4487
+ onMessage,
4488
+ onClose: (code, reason) => {
4489
+ currentConnection = null;
4490
+ onClose?.(code, reason);
4491
+ if (!closed) void scheduleReconnect();
4492
+ }
4493
+ });
4494
+ };
4470
4495
  const scheduleReconnect = async () => {
4471
4496
  for (let attempt = 1; attempt <= maxRetries; attempt++) {
4472
4497
  if (closed) return;
4473
4498
  const delay = Math.min(initialDelayMs * Math.pow(2, attempt - 1), MAX_BACKOFF_MS);
4474
4499
  onReconnecting?.(attempt, maxRetries);
4475
4500
  await new Promise((resolve2) => {
4476
- reconnectTimer = setTimeout(resolve2, delay);
4501
+ const timer = setTimeout(resolve2, delay);
4502
+ cancelDelay = () => {
4503
+ clearTimeout(timer);
4504
+ resolve2();
4505
+ };
4477
4506
  });
4507
+ cancelDelay = null;
4478
4508
  if (closed) return;
4479
4509
  try {
4480
- currentConnection = await connectWebSocket({
4481
- ...connectOpts,
4482
- onClose: (code, reason) => {
4483
- currentConnection = null;
4484
- onClose?.(code, reason);
4485
- if (!closed) scheduleReconnect();
4486
- }
4487
- });
4510
+ currentConnection = await connectOnce();
4488
4511
  onReconnected?.();
4489
4512
  return;
4490
- } catch {
4513
+ } catch (err) {
4514
+ if (err instanceof WebSocketAuthError && refreshAuth) {
4515
+ await refreshAuth().catch(() => {
4516
+ });
4517
+ }
4491
4518
  }
4492
4519
  }
4493
4520
  if (!closed) onReconnectFailed?.();
4494
4521
  };
4495
- currentConnection = await connectWebSocket({
4496
- ...connectOpts,
4497
- onClose: (code, reason) => {
4498
- currentConnection = null;
4499
- onClose?.(code, reason);
4500
- if (!closed) scheduleReconnect();
4501
- }
4502
- });
4522
+ currentConnection = await connectOnce();
4503
4523
  return {
4504
4524
  close: () => {
4505
4525
  closed = true;
4506
- if (reconnectTimer) clearTimeout(reconnectTimer);
4526
+ cancelDelay?.();
4507
4527
  currentConnection?.close();
4508
4528
  currentConnection = null;
4509
4529
  }
@@ -7123,10 +7143,14 @@ var OpenClawOrchestrator = class {
7123
7143
 
7124
7144
  // src/listen/dm-listener.ts
7125
7145
  async function startDmListener(options) {
7126
- const { arbi, accessToken, baseUrl, crypto, orchestrator, parentExtId, onLog, onError } = options;
7146
+ const { arbi, baseUrl, crypto, orchestrator, parentExtId, onLog, onError } = options;
7127
7147
  const log = onLog ?? (() => {
7128
7148
  });
7129
7149
  const logError = onError ?? ((msg) => console.error(msg));
7150
+ const initialToken = arbi.session.getState().accessToken;
7151
+ if (!initialToken) {
7152
+ throw new Error("No access token \u2014 run `arbi login` first.");
7153
+ }
7130
7154
  const handleNotification = async (msg) => {
7131
7155
  if (msg.type !== "user_message") return;
7132
7156
  const senderExtId = msg.sender?.external_id;
@@ -7196,7 +7220,10 @@ async function startDmListener(options) {
7196
7220
  }
7197
7221
  const ws = await connectWithReconnect({
7198
7222
  baseUrl,
7199
- accessToken,
7223
+ accessToken: initialToken,
7224
+ getAccessToken: () => arbi.session.getState().accessToken,
7225
+ refreshAuth: () => arbi.auth.relogin(),
7226
+ maxRetries: Infinity,
7200
7227
  onMessage: (msg) => {
7201
7228
  if ("sender" in msg && "recipient" in msg) {
7202
7229
  const notification = msg;
@@ -7208,14 +7235,12 @@ async function startDmListener(options) {
7208
7235
  }
7209
7236
  },
7210
7237
  onReconnecting: (attempt, max) => {
7211
- log(`WebSocket reconnecting (${attempt}/${max})...`);
7238
+ const limit = Number.isFinite(max) ? `/${max}` : "";
7239
+ log(`WebSocket reconnecting (attempt ${attempt}${limit})...`);
7212
7240
  },
7213
7241
  onReconnected: () => {
7214
7242
  log("WebSocket reconnected");
7215
7243
  },
7216
- onReconnectFailed: () => {
7217
- logError("WebSocket reconnection failed \u2014 listener stopped");
7218
- },
7219
7244
  onClose: (_code, reason) => {
7220
7245
  log(`WebSocket closed: ${reason || "no reason"}`);
7221
7246
  }
@@ -7236,6 +7261,7 @@ exports.ClaudeOrchestrator = ClaudeOrchestrator;
7236
7261
  exports.DOC_TERMINAL_STATUSES = DOC_TERMINAL_STATUSES;
7237
7262
  exports.FileConfigStore = FileConfigStore;
7238
7263
  exports.OpenClawOrchestrator = OpenClawOrchestrator;
7264
+ exports.WebSocketAuthError = WebSocketAuthError;
7239
7265
  exports.agentconfig = agentconfig_exports;
7240
7266
  exports.agents = agents_exports;
7241
7267
  exports.assistant = assistant_exports;