@pmate/chat-sdk 0.12.0-next.11 → 0.12.0-next.13

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.
@@ -10,6 +10,7 @@ export type ChatSessionOptions<T extends StoredMessage> = {
10
10
  messages: MessageClientOptions<T>;
11
11
  connection: ChatClientOptions;
12
12
  sharedConnection?: ChatClient;
13
+ activeThreadId?: string;
13
14
  reading: ReadingClientOptions;
14
15
  decode: (message: ChatRuntimeMessage) => T | undefined;
15
16
  onError: (error: unknown) => void;
@@ -20,7 +21,7 @@ export type ChatThreadSnapshot<T> = {
20
21
  hasEarlier: boolean;
21
22
  resetVersion: number;
22
23
  complete: boolean;
23
- unread: ReturnType<typeof unreadInConversation>;
24
+ unread: ReturnType<typeof unreadInConversation> | undefined;
24
25
  };
25
26
  export type ChatSummarySnapshot<T> = {
26
27
  threads: readonly {
@@ -29,8 +30,8 @@ export type ChatSummarySnapshot<T> = {
29
30
  unread: ChatThreadSnapshot<T>["unread"];
30
31
  outgoingCount: number;
31
32
  }[];
32
- unreadMessageCount: number;
33
- badgeCount: number;
33
+ unreadMessageCount: number | undefined;
34
+ badgeCount: number | undefined;
34
35
  connection: ChatConnectionState;
35
36
  };
36
37
  /** Owns all browser message clients. Hosts subscribe to stable thread snapshots. */
@@ -64,7 +64,7 @@ export class ChatSession {
64
64
  this.publishGlobal();
65
65
  if (state === "connected") {
66
66
  const since = Date.now();
67
- this.catchUp.schedule("scope", () => this.messages.restore(since));
67
+ this.catchUp.schedule("scope", () => this.messages.restore(since, options.activeThreadId));
68
68
  if (this.readingStarted)
69
69
  this.reading.refresh();
70
70
  }
@@ -207,7 +207,7 @@ export class ChatSession {
207
207
  if (!snapshot) {
208
208
  const messages = this.messages.getSnapshot(threadId);
209
209
  const state = this.reading.getSnapshot();
210
- snapshot = { messages: messages?.messages ?? [], hasEarlier: messages?.hasEarlier ?? false, resetVersion: messages?.resetVersion ?? 0, complete: messages?.complete ?? false, outgoing: this.sender.snapshot(threadId), unread: state?.threads.some(thread => (thread.conversationId ?? thread.id) === threadId) ? unreadInConversation(state, threadId) : { unreadMessageCount: 0, unreadMentionCount: 0, badgeCount: 0 } };
210
+ snapshot = { messages: messages?.messages ?? [], hasEarlier: messages?.hasEarlier ?? false, resetVersion: messages?.resetVersion ?? 0, complete: messages?.complete ?? false, outgoing: this.sender.snapshot(threadId), unread: state?.threads.some(thread => (thread.conversationId ?? thread.id) === threadId) ? unreadInConversation(state, threadId) : undefined };
211
211
  this.snapshots.set(threadId, snapshot);
212
212
  }
213
213
  return snapshot;
@@ -250,7 +250,9 @@ export class ChatSession {
250
250
  const snapshot = this.getSnapshot(threadId);
251
251
  return { threadId, lastMessage: snapshot.messages.at(-1), unread: snapshot.unread, outgoingCount: snapshot.outgoing.length };
252
252
  });
253
- return { threads, unreadMessageCount: threads.reduce((sum, t) => sum + t.unread.unreadMessageCount, 0), badgeCount: threads.reduce((sum, t) => sum + t.unread.badgeCount, 0), connection: this.connectionState };
253
+ const state = this.reading.getSnapshot();
254
+ const known = Boolean(state && state.syncCursor !== "0" && threads.every(thread => thread.unread !== undefined));
255
+ return { threads, unreadMessageCount: known ? threads.reduce((sum, t) => sum + (t.unread?.unreadMessageCount ?? 0), 0) : undefined, badgeCount: known ? threads.reduce((sum, t) => sum + (t.unread?.badgeCount ?? 0), 0) : undefined, connection: this.connectionState };
254
256
  }
255
257
  publishGlobal() {
256
258
  const next = this.buildGlobal();
@@ -11,6 +11,8 @@ export type CreateChatOptions = {
11
11
  profileId: string;
12
12
  teamId: string;
13
13
  token: string;
14
+ /** Limit reconnect reconciliation for a dedicated conversation document. */
15
+ threadId?: string;
14
16
  endpoint?: string;
15
17
  clientInstanceId?: string;
16
18
  fetch?: typeof fetch;
@@ -16,6 +16,7 @@ export function createChat(options) {
16
16
  };
17
17
  return new ChatSession({
18
18
  sharedConnection: options.connection,
19
+ activeThreadId: options.threadId,
19
20
  messages: { scope: { appId, accountId, profileId, teamId }, transport: { load: (thread, signal) => page(thread, signal), changes: (thread, after, signal) => page(thread, signal, { after }), older: (thread, before, signal) => page(thread, signal, { before }) }, onError },
20
21
  reading: { appId, profileId, sessionToken: token, hubHttpUrl: http, fetch: options.fetch, onError },
21
22
  connection: { appId, profileId, teamId, hubUrl: ws, clientInstanceId: options.clientInstanceId, fetchImpl: options.fetch,
@@ -59,7 +59,7 @@ export declare class MessageClient<T extends StoredMessage> {
59
59
  subscribe(thread: string, listener: () => void): () => void;
60
60
  subscribeAll(listener: (threadId: string) => void): () => void;
61
61
  /** Close the connection gap for saved windows with four bounded background lanes. */
62
- restore(since: number): Promise<void>;
62
+ restore(since: number, threadId?: string): Promise<void>;
63
63
  read(thread: string, limit?: number, refresh?: boolean): Promise<MessageSnapshot<T>>;
64
64
  /** Application connection/wake events invalidate, rather than fetching directly. */
65
65
  invalidate(thread: string): void;
@@ -71,8 +71,8 @@ export class MessageClient {
71
71
  }
72
72
  subscribeAll(listener) { this.allListeners.add(listener); return () => { this.allListeners.delete(listener); }; }
73
73
  /** Close the connection gap for saved windows with four bounded background lanes. */
74
- async restore(since) {
75
- const queue = (await this.storage.windows()).filter(window => (window.syncedAt ?? 0) < since && !window.revoked);
74
+ async restore(since, threadId) {
75
+ const queue = (await this.storage.windows()).filter(window => (window.syncedAt ?? 0) < since && !window.revoked && (threadId === undefined || window.thread === threadId));
76
76
  await Promise.all(Array.from({ length: Math.min(4, queue.length) }, async () => {
77
77
  while (queue.length && !this.closed && !this.closing) {
78
78
  const window = queue.shift();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pmate/chat-sdk",
3
- "version": "0.12.0-next.11",
3
+ "version": "0.12.0-next.13",
4
4
  "description": "Cross-platform PMate chat input components, voice input primitives, and ASR helpers",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",