@pmate/chat-sdk 0.9.3 → 0.9.5

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.
@@ -267,6 +267,8 @@ export class ChatClient {
267
267
  const profileId = this.profileId();
268
268
  const auth = createRuntimeMsg(profileId, "@hub", ChatMsgOp.AUTH, {
269
269
  token,
270
+ appId: this.options.appId,
271
+ notificationReceiver: !!this.options.onNotification,
270
272
  as: "user",
271
273
  profileId,
272
274
  clientInstanceId: this.instanceId,
@@ -13,3 +13,4 @@ export * from "./call.js";
13
13
  export * from "./presence.js";
14
14
  export * from "./meeting.js";
15
15
  export * from "./notification.js";
16
+ export * from "./readSync.js";
@@ -13,3 +13,4 @@ export * from "./call.js";
13
13
  export * from "./presence.js";
14
14
  export * from "./meeting.js";
15
15
  export * from "./notification.js";
16
+ export * from "./readSync.js";
@@ -0,0 +1,23 @@
1
+ import { type ReadIndexPersistence, type ReadIndexState } from "./readIndex.js";
2
+ export type ReadSyncOptions = {
3
+ appId: string;
4
+ profileId: string;
5
+ sessionToken: string;
6
+ hubHttpUrl: string;
7
+ persistence: ReadIndexPersistence;
8
+ fetch?: typeof globalThis.fetch;
9
+ onChange?: (state: ReadIndexState) => void;
10
+ };
11
+ /** Keeps local operations until an authoritative projection covers them. */
12
+ export declare class ReadSyncClient {
13
+ private readonly options;
14
+ private readonly controller;
15
+ private inFlight?;
16
+ private readAfter;
17
+ constructor(options: ReadSyncOptions);
18
+ loadLocal(): Promise<ReadIndexState>;
19
+ stop(): void;
20
+ synchronize(): Promise<ReadIndexState>;
21
+ private request;
22
+ private run;
23
+ }
@@ -0,0 +1,71 @@
1
+ import { createReadIndex, installReadSnapshot } from "./readIndex.js";
2
+ /** Keeps local operations until an authoritative projection covers them. */
3
+ export class ReadSyncClient {
4
+ options;
5
+ controller = new AbortController();
6
+ inFlight;
7
+ readAfter = 0;
8
+ constructor(options) {
9
+ this.options = options;
10
+ }
11
+ async loadLocal() {
12
+ return await this.options.persistence.load(this.options.appId, this.options.profileId)
13
+ ?? createReadIndex(this.options.appId, this.options.profileId);
14
+ }
15
+ stop() { this.controller.abort(); }
16
+ synchronize() {
17
+ if (this.inFlight)
18
+ return this.inFlight;
19
+ const task = this.run().finally(() => { if (this.inFlight === task)
20
+ this.inFlight = undefined; });
21
+ this.inFlight = task;
22
+ return task;
23
+ }
24
+ async request(path, init = {}) {
25
+ if (this.controller.signal.aborted)
26
+ throw new Error("READ_SYNC_STOPPED");
27
+ const timeout = new AbortController();
28
+ const abort = () => timeout.abort();
29
+ this.controller.signal.addEventListener("abort", abort, { once: true });
30
+ const timer = setTimeout(abort, 10_000);
31
+ try {
32
+ const response = await (this.options.fetch ?? globalThis.fetch)(`${this.options.hubHttpUrl.replace(/\/$/, "")}${path}`, {
33
+ ...init, signal: timeout.signal, headers: { ...init.headers, authorization: `Bearer ${this.options.sessionToken}` },
34
+ });
35
+ if (!response.ok)
36
+ throw new Error(`READ_SYNC_HTTP_${response.status}`);
37
+ const envelope = await response.json();
38
+ if (envelope.success !== true || envelope.data === undefined)
39
+ throw new Error("INVALID_READ_SYNC_RESPONSE");
40
+ return envelope.data;
41
+ }
42
+ finally {
43
+ clearTimeout(timer);
44
+ this.controller.signal.removeEventListener("abort", abort);
45
+ }
46
+ }
47
+ async run() {
48
+ const { appId, profileId, persistence } = this.options;
49
+ const current = await this.loadLocal();
50
+ for (const operation of current.pending) {
51
+ const receipt = await this.request("/api/social/reading", {
52
+ method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ ...operation, appId, profileId }),
53
+ });
54
+ if (receipt.operationId !== operation.operationId || receipt.accepted !== true
55
+ || !Number.isSafeInteger(receipt.acceptedAt) || receipt.acceptedAt <= 0)
56
+ throw new Error("INVALID_READING_RECEIPT");
57
+ this.readAfter = Math.max(this.readAfter, receipt.acceptedAt);
58
+ }
59
+ const query = new URLSearchParams({ appId, profileId });
60
+ const snapshot = await this.request(`/api/social/reading/snapshot?${query}`, {
61
+ headers: this.readAfter ? { "x-pmate-read-after-time": String(this.readAfter) } : {},
62
+ });
63
+ if (this.controller.signal.aborted)
64
+ throw new Error("READ_SYNC_STOPPED");
65
+ // Merge into the latest transaction, including reads made while HTTP was pending.
66
+ const next = await persistence.update(appId, profileId, state => installReadSnapshot(state, snapshot));
67
+ if (!this.controller.signal.aborted)
68
+ this.options.onChange?.(next);
69
+ return next;
70
+ }
71
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pmate/chat-sdk",
3
- "version": "0.9.3",
3
+ "version": "0.9.5",
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",