@poolse/sdk 3.0.0 → 3.2.0-dev.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/index.cjs CHANGED
@@ -106,6 +106,7 @@ var AuthError = class extends ApiError {
106
106
  };
107
107
 
108
108
  // src/realtime/realtime.ts
109
+ var TOKEN_REFRESH_ON_ERROR_MS = 3e4;
109
110
  var PoolseRealtime = class {
110
111
  config;
111
112
  tokenCache;
@@ -115,6 +116,8 @@ var PoolseRealtime = class {
115
116
  conversations = /* @__PURE__ */ new Map();
116
117
  userChannel = null;
117
118
  status = "idle";
119
+ paused = false;
120
+ lastTokenRefreshMs = 0;
118
121
  statusListeners = /* @__PURE__ */ new Set();
119
122
  constructor(config, tokenCache, opts = {}) {
120
123
  this.config = config;
@@ -170,9 +173,15 @@ var PoolseRealtime = class {
170
173
  });
171
174
  socket.onError((err) => {
172
175
  this.setStatus("reconnecting");
176
+ if (Date.now() - this.lastTokenRefreshMs > TOKEN_REFRESH_ON_ERROR_MS) {
177
+ this.lastTokenRefreshMs = Date.now();
178
+ void this.tokenCache.getToken({ forceRefresh: true }).catch(() => {
179
+ });
180
+ }
173
181
  this.config.onSocketError?.(new PoolseError(`socket error: ${String(err)}`));
174
182
  });
175
183
  this.socket = socket;
184
+ this.lastTokenRefreshMs = Date.now();
176
185
  void this.tokenCache.getToken().catch((err) => {
177
186
  this.config.onSocketError?.(
178
187
  new PoolseError(`token fetch failed before socket open: ${String(err)}`)
@@ -181,8 +190,35 @@ var PoolseRealtime = class {
181
190
  socket.connect();
182
191
  });
183
192
  }
193
+ /**
194
+ * Close the socket but keep every channel handle and its listeners
195
+ * (for an app going to the background). `resume()` reopens it and
196
+ * Phoenix rejoins the channels. Events sent in between are not
197
+ * replayed; the React hooks reload from REST on reconnect.
198
+ */
199
+ pause() {
200
+ if (!this.socket || this.paused) return;
201
+ this.paused = true;
202
+ this.setStatus("closed");
203
+ this.socket.disconnect();
204
+ }
205
+ /** Reopen a socket closed by `pause()`, with a fresh token. */
206
+ resume() {
207
+ const socket = this.socket;
208
+ if (!socket || !this.paused) return;
209
+ this.paused = false;
210
+ this.setStatus("connecting");
211
+ void this.tokenCache.getToken().catch((err) => {
212
+ this.config.onSocketError?.(
213
+ new PoolseError(`token fetch failed before socket open: ${String(err)}`)
214
+ );
215
+ }).finally(() => {
216
+ if (this.socket === socket && !this.paused) socket.connect();
217
+ });
218
+ }
184
219
  /** Close the socket and tear down every joined channel. */
185
220
  disconnect() {
221
+ this.paused = false;
186
222
  this.setStatus("closed");
187
223
  this.conversations.forEach((c) => c._destroy());
188
224
  this.conversations.clear();
@@ -206,7 +242,10 @@ var PoolseRealtime = class {
206
242
  throw new PoolseError("socket not initialised \u2014 call connect() first");
207
243
  }
208
244
  const channel = this.socket.channel(`conversation:${conversationId}`, {});
209
- const handle = new ConversationChannel(conversationId, channel);
245
+ const handle = new ConversationChannel(conversationId, channel, () => {
246
+ if (this.conversations.get(conversationId) === handle)
247
+ this.conversations.delete(conversationId);
248
+ });
210
249
  this.conversations.set(conversationId, handle);
211
250
  handle._join();
212
251
  return handle;
@@ -255,9 +294,15 @@ var ConversationChannel = class {
255
294
  // state here and replay it on subscribe.
256
295
  presenceState = {};
257
296
  presenceStateSeen = false;
258
- constructor(conversationId, channel) {
297
+ closedListeners = /* @__PURE__ */ new Set();
298
+ constructor(conversationId, channel, onGone) {
259
299
  this.conversationId = conversationId;
260
300
  this.channel = channel;
301
+ channel.onClose(() => {
302
+ onGone?.();
303
+ this.closedListeners.forEach((l) => l());
304
+ this.closedListeners.clear();
305
+ });
261
306
  channel.on("presence_state", (payload) => {
262
307
  this.presenceState = payload ?? {};
263
308
  this.presenceStateSeen = true;
@@ -286,6 +331,10 @@ var ConversationChannel = class {
286
331
  onMessageDeleted(fn) {
287
332
  return this.subscribe("message:deleted", fn);
288
333
  }
334
+ /** An AI agent's reply as it's written (`message:stream`); see `MessageStreamEvent`. */
335
+ onMessageStream(fn) {
336
+ return this.subscribe("message:stream", fn);
337
+ }
289
338
  onTypingStart(fn) {
290
339
  return this.subscribe("typing:start", fn);
291
340
  }
@@ -332,6 +381,18 @@ var ConversationChannel = class {
332
381
  onPresenceDiff(fn) {
333
382
  return this.subscribe("presence_diff", fn);
334
383
  }
384
+ /**
385
+ * The channel closed for good: the server removed you from the
386
+ * conversation, or `leave()` / `disconnect()` was called. No more
387
+ * events arrive on this handle; `realtime.conversation(id)` returns a
388
+ * fresh one.
389
+ */
390
+ onClosed(fn) {
391
+ this.closedListeners.add(fn);
392
+ return () => {
393
+ this.closedListeners.delete(fn);
394
+ };
395
+ }
335
396
  /** Current presence snapshot for sync access — usually used in tests. */
336
397
  getPresenceState() {
337
398
  return this.presenceState;
@@ -374,6 +435,8 @@ var UserChannel = class {
374
435
  mentionBound = false;
375
436
  conversationCreatedBound = false;
376
437
  conversationUpdatedBound = false;
438
+ // One Phoenix binding per event, fanned out — as in ConversationChannel.
439
+ listeners = /* @__PURE__ */ new Map();
377
440
  constructor(userId, channel) {
378
441
  this.userId = userId;
379
442
  this.channel = channel;
@@ -442,17 +505,47 @@ var UserChannel = class {
442
505
  this.conversationUpdatedListeners.delete(fn);
443
506
  };
444
507
  }
508
+ /**
509
+ * Your ring for a call ended — answered on another device, declined,
510
+ * cancelled by the caller or timed out. Stop ringing on this device.
511
+ */
512
+ onRingEnded(fn) {
513
+ return this.subscribe("call:ring_ended", fn);
514
+ }
515
+ /**
516
+ * A call you were part of (rung or joined) is over. Arrives on the
517
+ * user channel, which the app stays joined to even after leaving the
518
+ * conversation's channel mid-call.
519
+ */
520
+ onCallFinished(fn) {
521
+ return this.subscribe("call:finished", fn);
522
+ }
445
523
  /** @internal */
446
524
  _join() {
447
525
  this.channel.join();
448
526
  }
449
527
  /** @internal */
450
528
  _destroy() {
529
+ this.listeners.clear();
451
530
  this.mentionListeners.clear();
452
531
  this.conversationCreatedListeners.clear();
453
532
  this.conversationUpdatedListeners.clear();
454
533
  this.channel.leave();
455
534
  }
535
+ subscribe(event, fn) {
536
+ let set = this.listeners.get(event);
537
+ if (!set) {
538
+ set = /* @__PURE__ */ new Set();
539
+ this.listeners.set(event, set);
540
+ this.channel.on(event, (payload) => {
541
+ this.listeners.get(event)?.forEach((l) => l(payload));
542
+ });
543
+ }
544
+ set.add(fn);
545
+ return () => {
546
+ set.delete(fn);
547
+ };
548
+ }
456
549
  };
457
550
  function deriveWsUrl(apiUrl) {
458
551
  return apiUrl.replace(/^http/, "ws");
@@ -504,7 +597,7 @@ var AttachmentsResource = class {
504
597
  * });
505
598
  * await chat.conversations.one(convId).messages.send({
506
599
  * body: 'Look at this!',
507
- * custom_data: { attachment_id: att.id },
600
+ * attachment_ids: [att.id],
508
601
  * });
509
602
  * ```
510
603
  *
@@ -1026,10 +1119,10 @@ var CallTokensResource = class {
1026
1119
  /**
1027
1120
  * POST /v1/conversations/:conversationId/call-token
1028
1121
  *
1029
- * Resolves `null` when the deployment has no SFU configured (503).
1030
- * That is deliberately not an error — the caller falls back to the
1031
- * peer-to-peer mesh, which is how every call worked before the SFU
1032
- * existed. A 403 still throws: that one really is a refusal.
1122
+ * Resolves `null` when the deployment has no SFU configured (503):
1123
+ * calls are unavailable there, which the caller shows as such rather
1124
+ * than as a failed call. A 403 still throws: that one really is a
1125
+ * refusal.
1033
1126
  */
1034
1127
  async create(conversationId, signal) {
1035
1128
  try {
@@ -1131,7 +1224,7 @@ var CallRoom = class {
1131
1224
  room = null;
1132
1225
  status = "idle";
1133
1226
  selfId = null;
1134
- // Reference counted like VoiceRoom: a call screen and a roster can
1227
+ // Reference counted: a call screen and a roster can
1135
1228
  // both hold the same call, and the second holder must not open a
1136
1229
  // second SFU connection or tear the first one down on leaving.
1137
1230
  holders = 0;
@@ -1220,8 +1313,8 @@ var CallRoom = class {
1220
1313
  * Connect to the SFU.
1221
1314
  *
1222
1315
  * Resolves `true` once media is flowing, `false` when the deployment
1223
- * has no SFU — the caller should fall back to the mesh rather than
1224
- * treat that as a failed call.
1316
+ * has no SFU — calls are unavailable there; show that rather than a
1317
+ * failed call.
1225
1318
  */
1226
1319
  async join() {
1227
1320
  if (this.room) {
@@ -1873,7 +1966,7 @@ var Poolse = class {
1873
1966
  *
1874
1967
  * import * as livekit from 'livekit-client';
1875
1968
  * const call = poolse.call(conversationId, { livekit, video: true });
1876
- * const usingSfu = await call.join(); // false = no SFU, use the mesh
1969
+ * const joined = await call.join(); // false = calls aren't available on this deployment
1877
1970
  */
1878
1971
  call(conversationId, opts) {
1879
1972
  return new CallRoom({
@@ -1893,7 +1986,7 @@ var Poolse = class {
1893
1986
  };
1894
1987
 
1895
1988
  // src/version.ts
1896
- var version = "3.0.0";
1989
+ var version = "3.2.0-dev.0";
1897
1990
 
1898
1991
  // src/call-log.ts
1899
1992
  function parseCallLogBody(body) {