@pmate/chat-sdk 0.12.0-next.4 → 0.12.0-next.41

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.
Files changed (50) hide show
  1. package/README.md +41 -1
  2. package/lib/runtime/batchedReadIndex.d.ts +11 -2
  3. package/lib/runtime/batchedReadIndex.js +21 -1
  4. package/lib/runtime/cachedMessage.d.ts +12 -0
  5. package/lib/runtime/cachedMessage.js +18 -0
  6. package/lib/runtime/chatClient.d.ts +1 -0
  7. package/lib/runtime/chatClient.js +1 -1
  8. package/lib/runtime/chatSession.d.ts +78 -8
  9. package/lib/runtime/chatSession.js +504 -39
  10. package/lib/runtime/connectivity.d.ts +4 -0
  11. package/lib/runtime/connectivity.js +8 -0
  12. package/lib/runtime/conversationRead.d.ts +13 -0
  13. package/lib/runtime/conversationRead.js +34 -0
  14. package/lib/runtime/conversationReading.d.ts +28 -0
  15. package/lib/runtime/conversationReading.js +47 -0
  16. package/lib/runtime/createChat.d.ts +4 -5
  17. package/lib/runtime/createChat.js +14 -17
  18. package/lib/runtime/durableSender.d.ts +30 -4
  19. package/lib/runtime/durableSender.js +174 -34
  20. package/lib/runtime/index.d.ts +4 -0
  21. package/lib/runtime/index.js +4 -0
  22. package/lib/runtime/messageClient.d.ts +33 -6
  23. package/lib/runtime/messageClient.js +443 -71
  24. package/lib/runtime/messageControls.d.ts +19 -0
  25. package/lib/runtime/messageControls.js +44 -0
  26. package/lib/runtime/messageLaunch.d.ts +42 -0
  27. package/lib/runtime/messageLaunch.js +127 -0
  28. package/lib/runtime/messageQuotes.d.ts +3 -0
  29. package/lib/runtime/messageQuotes.js +17 -0
  30. package/lib/runtime/messageReaders.d.ts +12 -1
  31. package/lib/runtime/messageReaders.js +67 -3
  32. package/lib/runtime/messageStore.d.ts +47 -2
  33. package/lib/runtime/messageStore.js +233 -10
  34. package/lib/runtime/messageTimeline.d.ts +50 -0
  35. package/lib/runtime/messageTimeline.js +120 -0
  36. package/lib/runtime/readCounts.d.ts +15 -1
  37. package/lib/runtime/readCounts.js +96 -8
  38. package/lib/runtime/readIndex.d.ts +44 -6
  39. package/lib/runtime/readIndex.js +238 -11
  40. package/lib/runtime/readSync.d.ts +1 -0
  41. package/lib/runtime/readSync.js +21 -8
  42. package/lib/runtime/readingClient.d.ts +19 -0
  43. package/lib/runtime/readingClient.js +121 -18
  44. package/lib/runtime/readingContent.d.ts +5 -0
  45. package/lib/runtime/readingContent.js +23 -0
  46. package/lib/runtime/recordCache.d.ts +9 -0
  47. package/lib/runtime/recordCache.js +62 -5
  48. package/lib/runtime/responseCache.d.ts +44 -0
  49. package/lib/runtime/responseCache.js +173 -0
  50. package/package.json +6 -1
@@ -0,0 +1,173 @@
1
+ import { Dexie } from "dexie";
2
+ import { WindowedTasks } from "./coalescer.js";
3
+ export function isResponseCacheAuthorityError(error) {
4
+ return /RESPONSE_CACHE_(ACCESS_REVOKED|SCOPE_CHANGED|INVALIDATED)/.test(String(error));
5
+ }
6
+ export async function assertResponseCacheAccess(access, expectedVersion) {
7
+ if (!access)
8
+ return undefined;
9
+ const current = await access();
10
+ if (!current.allowed)
11
+ throw new Error("RESPONSE_CACHE_ACCESS_REVOKED");
12
+ if (expectedVersion !== undefined && current.version !== expectedVersion)
13
+ throw new Error("RESPONSE_CACHE_SCOPE_CHANGED");
14
+ return current.version;
15
+ }
16
+ /** Small response windows, isolated by identity. Epochs fence late writes after
17
+ * access withdrawal. No localStorage or synchronous writes on the read path. */
18
+ export class IndexedDbResponseCache {
19
+ scope;
20
+ options;
21
+ db;
22
+ rows;
23
+ epochs;
24
+ pending = [];
25
+ tasks = new WindowedTasks(16, () => { });
26
+ running;
27
+ invalidations = new Set();
28
+ closed = false;
29
+ closing;
30
+ constructor(scope, options = {}) {
31
+ this.scope = scope;
32
+ this.options = options;
33
+ this.db = new Dexie(options.name ?? "pmate-chat-response-windows", { indexedDB: options.indexedDB ?? globalThis.indexedDB, IDBKeyRange: globalThis.IDBKeyRange });
34
+ this.db.version(1).stores({ windows: "[scope+key],scope", epochs: "scope" });
35
+ this.rows = this.db.table("windows");
36
+ this.epochs = this.db.table("epochs");
37
+ }
38
+ /** Resolve shared message-scope authority before opening our transaction.
39
+ * A newer authority version retires response windows exactly once; delayed
40
+ * callers can never roll the authority backwards or delete recovered data. */
41
+ async synchronizeAccess() {
42
+ if (!this.options.access)
43
+ return;
44
+ const access = await this.options.access();
45
+ const stored = await this.epochs.get(this.scope);
46
+ const storedVersion = stored?.accessVersion ?? 0;
47
+ if (access.version < storedVersion)
48
+ throw new Error("RESPONSE_CACHE_SCOPE_CHANGED");
49
+ // Ordinary cache reads must not take an IndexedDB write lock. Only an
50
+ // authority transition needs the atomic epoch bump and range deletion.
51
+ if (access.version > storedVersion)
52
+ await this.db.transaction("rw", this.rows, this.epochs, async () => {
53
+ const current = await this.epochs.get(this.scope);
54
+ const version = current?.accessVersion ?? 0;
55
+ if (access.version < version)
56
+ throw new Error("RESPONSE_CACHE_SCOPE_CHANGED");
57
+ if (access.version > version) {
58
+ await this.epochs.put({ scope: this.scope, epoch: (current?.epoch ?? 0) + 1, accessVersion: access.version });
59
+ await this.rows.where("scope").equals(this.scope).delete();
60
+ }
61
+ });
62
+ if (!access.allowed)
63
+ throw new Error("RESPONSE_CACHE_ACCESS_REVOKED");
64
+ }
65
+ async read(key) {
66
+ if (this.closed)
67
+ throw new Error("RESPONSE_CACHE_CLOSED");
68
+ await this.synchronizeAccess();
69
+ return this.db.transaction("r", this.rows, this.epochs, async () => {
70
+ const epoch = (await this.epochs.get(this.scope))?.epoch ?? 0;
71
+ const row = await this.rows.get([this.scope, key]);
72
+ return { epoch, cached: row?.epoch === epoch ? { value: row.value, time: row.time } : undefined };
73
+ });
74
+ }
75
+ write(key, value, epoch) {
76
+ if (this.closed)
77
+ return Promise.reject(new Error("RESPONSE_CACHE_CLOSED"));
78
+ const copy = structuredClone(value), bytes = new TextEncoder().encode(JSON.stringify(copy)).byteLength;
79
+ // Count in-flight data as well as queued data; requests cannot create an
80
+ // unbounded backlog while IndexedDB is blocked by another tab.
81
+ if (bytes > 65536 || this.pending.length >= 128 || this.pending.reduce((n, w) => n + w.row.bytes, 0) + bytes > 1048576)
82
+ return Promise.reject(new Error("RESPONSE_CACHE_BACKPRESSURE"));
83
+ const promise = new Promise((resolve, reject) => this.pending.push({ row: { scope: this.scope, key, epoch, time: Date.now(), bytes, value: copy }, resolve, reject }));
84
+ this.tasks.schedule("write", () => this.flush());
85
+ return promise;
86
+ }
87
+ /** Idempotent shared-scope retirement: an old cleanup cannot erase windows
88
+ * created after the same authority version was already recovered. */
89
+ retire(version) {
90
+ if (this.closed)
91
+ return Promise.reject(new Error("RESPONSE_CACHE_CLOSED"));
92
+ if (!Number.isSafeInteger(version) || version < 1)
93
+ return Promise.reject(new Error("INVALID_RESPONSE_SCOPE_VERSION"));
94
+ const task = this.db.transaction("rw", this.rows, this.epochs, async () => {
95
+ const current = await this.epochs.get(this.scope);
96
+ if ((current?.accessVersion ?? 0) >= version)
97
+ return;
98
+ await this.epochs.put({ scope: this.scope, epoch: (current?.epoch ?? 0) + 1, accessVersion: version });
99
+ await this.rows.where("scope").equals(this.scope).delete();
100
+ });
101
+ this.invalidations.add(task);
102
+ return task.finally(() => this.invalidations.delete(task));
103
+ }
104
+ invalidate() {
105
+ if (this.closed)
106
+ return Promise.reject(new Error("RESPONSE_CACHE_CLOSED"));
107
+ const task = (async () => {
108
+ await this.db.transaction("rw", this.rows, this.epochs, async () => {
109
+ const current = await this.epochs.get(this.scope);
110
+ const epoch = (current?.epoch ?? 0) + 1;
111
+ await this.epochs.put({ scope: this.scope, epoch, accessVersion: current?.accessVersion });
112
+ await this.rows.where("scope").equals(this.scope).delete();
113
+ });
114
+ })();
115
+ this.invalidations.add(task);
116
+ return task.finally(() => this.invalidations.delete(task));
117
+ }
118
+ async flush() {
119
+ if (this.running) {
120
+ await this.running;
121
+ if (this.pending.length)
122
+ return this.flush();
123
+ return;
124
+ }
125
+ const batch = [...this.pending];
126
+ if (!batch.length)
127
+ return;
128
+ const task = (async () => {
129
+ try {
130
+ await this.synchronizeAccess();
131
+ const accepted = await this.db.transaction("rw", this.rows, this.epochs, async () => {
132
+ const epoch = (await this.epochs.get(this.scope))?.epoch ?? 0;
133
+ const valid = batch.filter(write => write.row.epoch === epoch);
134
+ const unique = new Map(valid.map(write => [write.row.key, write.row]));
135
+ await this.rows.bulkPut([...unique.values()]);
136
+ const stored = await this.rows.where("scope").equals(this.scope).toArray();
137
+ stored.sort((a, b) => b.time - a.time || a.key.localeCompare(b.key));
138
+ let bytes = 0;
139
+ const evicted = [];
140
+ stored.forEach((row, index) => { bytes += row.bytes; if (index >= 256 || bytes > 1048576)
141
+ evicted.push([this.scope, row.key]); });
142
+ await this.rows.bulkDelete(evicted);
143
+ return new Set(valid);
144
+ });
145
+ batch.forEach(write => accepted.has(write) ? write.resolve() : write.reject(new Error("RESPONSE_CACHE_INVALIDATED")));
146
+ }
147
+ catch (error) {
148
+ batch.forEach(write => write.reject(error));
149
+ throw error;
150
+ }
151
+ finally {
152
+ this.pending.splice(0, batch.length);
153
+ }
154
+ })().finally(() => { if (this.running === task)
155
+ this.running = undefined; });
156
+ this.running = task;
157
+ return task;
158
+ }
159
+ close() {
160
+ if (this.closing)
161
+ return this.closing;
162
+ this.closed = true;
163
+ this.tasks.close();
164
+ return this.closing = (async () => {
165
+ try {
166
+ await Promise.all([this.flush(), ...this.invalidations]);
167
+ }
168
+ finally {
169
+ this.db.close();
170
+ }
171
+ })();
172
+ }
173
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pmate/chat-sdk",
3
- "version": "0.12.0-next.4",
3
+ "version": "0.12.0-next.41",
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",
@@ -19,6 +19,11 @@
19
19
  "import": "./lib/index.js",
20
20
  "default": "./lib/index.js"
21
21
  },
22
+ "./bootstrap": {
23
+ "types": "./lib/runtime/messageLaunch.d.ts",
24
+ "import": "./lib/runtime/messageLaunch.js",
25
+ "default": "./lib/runtime/messageLaunch.js"
26
+ },
22
27
  "./runtime": {
23
28
  "types": "./lib/runtime/index.d.ts",
24
29
  "import": "./lib/runtime/index.js",