@pmate/chat-sdk 0.12.0-next.16 → 0.12.0-next.18

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.
@@ -1,3 +1,4 @@
1
+ import type { ConversationReadMessage } from "./conversationRead.js";
1
2
  import { type ReadCountsResult } from "./readCounts.js";
2
3
  import type { OnlineReachEvent } from "./reach.js";
3
4
  import { type MessageReadersQuery } from "./messageReaders.js";
@@ -74,6 +75,8 @@ export declare class ChatSession<T extends StoredMessage> {
74
75
  subscribeReading(listener: () => void): () => void;
75
76
  refreshReading(): void;
76
77
  synchronizeReading(cursor?: string, profileId?: string): Promise<import("./readIndex.js").ReadIndexState>;
78
+ /** Explicit text-history acknowledgement; unheard audio stays unread. */
79
+ markRead(threadId: string, messages?: readonly ConversationReadMessage[]): Promise<import("./readIndex.js").ReadIndexState>;
77
80
  updateReading(transform: Parameters<ReadingClient["update"]>[0]): Promise<import("./readIndex.js").ReadIndexState>;
78
81
  private assertOpen;
79
82
  getMessages(threadId: string, options?: {
@@ -121,6 +121,8 @@ export class ChatSession {
121
121
  subscribeReading(listener) { this.assertOpen(); return this.reading.subscribe(listener); }
122
122
  refreshReading() { this.assertOpen(); this.reading.refresh(); }
123
123
  synchronizeReading(cursor, profileId) { this.assertOpen(); return this.reading.synchronize(cursor, profileId); }
124
+ /** Explicit text-history acknowledgement; unheard audio stays unread. */
125
+ markRead(threadId, messages = []) { this.assertOpen(); return this.reading.markRead(threadId, messages); }
124
126
  updateReading(transform) { this.assertOpen(); return this.reading.update(transform); }
125
127
  assertOpen() { if (this.closed)
126
128
  throw new Error("CHAT_SESSION_CLOSED"); }
@@ -0,0 +1,13 @@
1
+ import { type ReadIndexState } from "./readIndex.js";
2
+ /** Optional observations whose index metadata may still be in flight. */
3
+ export type ConversationReadMessage = {
4
+ id: string;
5
+ threadId: string;
6
+ mine?: boolean;
7
+ from?: string;
8
+ recalled?: boolean;
9
+ readPolicy?: "visible" | "explicit";
10
+ kind?: string;
11
+ };
12
+ /** Explicitly mark ordinary text history read; unheard audio stays unread. */
13
+ export declare function recordConversationRead(state: ReadIndexState, conversationId: string, messages?: readonly ConversationReadMessage[], operationId?: () => `${string}-${string}-${string}-${string}-${string}`): ReadIndexState;
@@ -0,0 +1,34 @@
1
+ import { recordReadIntent, sourcesInConversation } from "./readIndex.js";
2
+ /** Explicitly mark ordinary text history read; unheard audio stays unread. */
3
+ export function recordConversationRead(state, conversationId, messages = [], operationId = () => crypto.randomUUID()) {
4
+ if (!conversationId)
5
+ throw new Error("INVALID_READING_THREAD");
6
+ const sources = sourcesInConversation(state, conversationId);
7
+ if (new Set(sources.map(source => source.teamId)).size > 1)
8
+ throw new Error("READ_CONVERSATION_TEAM_MISMATCH");
9
+ let next = state;
10
+ const indexedIds = new Set();
11
+ for (const source of sources) {
12
+ const incoming = source.messages.filter(message => {
13
+ indexedIds.add(message.id);
14
+ return !message.recalled && message.from !== state.profileId && message.recipients.includes(state.profileId);
15
+ });
16
+ const latest = incoming.filter(message => message.readPolicy === "visible" && message.seq > source.readThroughSeq)
17
+ .reduce((last, message) => !last || message.seq > last.seq ? message : last, undefined);
18
+ if (latest)
19
+ next = recordReadIntent(next, { operationId: operationId(), threadId: source.id, messageId: latest.id, readPolicy: "visible" });
20
+ }
21
+ // Live/history messages can precede their read-index metadata. Persist their
22
+ // observations so leaving the page or reloading cannot resurrect the badge.
23
+ const observed = new Set(next.observedReadIntents?.filter(intent => intent.threadId === conversationId).map(intent => intent.messageId));
24
+ for (const message of messages) {
25
+ if (message.threadId !== conversationId || message.mine || message.from === state.profileId || message.recalled || indexedIds.has(message.id) || observed.has(message.id))
26
+ continue;
27
+ const readPolicy = message.readPolicy ?? (message.kind === "voice" ? "explicit" : "visible");
28
+ if (message.kind === "voice" || readPolicy === "explicit")
29
+ continue;
30
+ next = recordReadIntent(next, { operationId: operationId(), threadId: conversationId, messageId: message.id, readPolicy });
31
+ observed.add(message.id);
32
+ }
33
+ return next;
34
+ }
@@ -29,3 +29,4 @@ export * from "./durableSender.js";
29
29
  export * from "./chatSession.js";
30
30
  export * from "./messageReaders.js";
31
31
  export * from "./createChat.js";
32
+ export * from "./conversationRead.js";
@@ -29,3 +29,4 @@ export * from "./durableSender.js";
29
29
  export * from "./chatSession.js";
30
30
  export * from "./messageReaders.js";
31
31
  export * from "./createChat.js";
32
+ export * from "./conversationRead.js";
@@ -74,8 +74,11 @@ export class ReadCountsClient {
74
74
  this.heartbeat = setInterval(() => { if (!this.closed && this.thread === threadId)
75
75
  this.renew(); }, 20_000);
76
76
  // Recover changes that occurred before a receiver/subscription became ready.
77
- if (recovered && this.revision >= 0)
77
+ if (recovered) {
78
+ // A fresh cache predates the new lease and cannot cover its delivery gap.
79
+ this.force = true;
78
80
  this.schedule(0);
81
+ }
79
82
  }).catch(error => {
80
83
  if (this.closed || generation !== this.generation)
81
84
  return;
@@ -1,3 +1,4 @@
1
+ import { type ConversationReadMessage } from "./conversationRead.js";
1
2
  import { type ReadIndexPersistence, type ReadIndexState } from "./readIndex.js";
2
3
  export type ReadingClientOptions = {
3
4
  appId: string;
@@ -35,6 +36,7 @@ export declare class ReadingClient {
35
36
  refresh: () => void;
36
37
  synchronize(targetCursor?: string, profileId?: string): Promise<ReadIndexState>;
37
38
  update(transform: (state: ReadIndexState) => ReadIndexState): Promise<ReadIndexState>;
39
+ markRead(threadId: string, messages?: readonly ConversationReadMessage[]): Promise<ReadIndexState>;
38
40
  recordVisible(threadId: string, messageIds: string[]): Promise<ReadIndexState>;
39
41
  recordExplicit(threadId: string, messageIds: string[]): Promise<ReadIndexState>;
40
42
  private record;
@@ -1,3 +1,4 @@
1
+ import { recordConversationRead } from "./conversationRead.js";
1
2
  import { BatchedReadIndex } from "./batchedReadIndex.js";
2
3
  import { WindowedTasks } from "./coalescer.js";
3
4
  import { IndexedDbReadIndex, recordReadIntent } from "./readIndex.js";
@@ -95,6 +96,9 @@ export class ReadingClient {
95
96
  }
96
97
  return next;
97
98
  }
99
+ markRead(threadId, messages = []) {
100
+ return this.update(state => recordConversationRead(state, threadId, messages));
101
+ }
98
102
  recordVisible(threadId, messageIds) { return this.record(threadId, messageIds, "visible"); }
99
103
  recordExplicit(threadId, messageIds) { return this.record(threadId, messageIds, "explicit"); }
100
104
  record(threadId, ids, readPolicy) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pmate/chat-sdk",
3
- "version": "0.12.0-next.16",
3
+ "version": "0.12.0-next.18",
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",