@pmate/chat-sdk 0.12.0-next.87 → 0.12.0-next.88
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/lib/chatError.d.ts
CHANGED
|
@@ -9,6 +9,14 @@ export declare class ChatError extends Error {
|
|
|
9
9
|
tables?: string;
|
|
10
10
|
mode?: string;
|
|
11
11
|
attempt?: number;
|
|
12
|
+
route?: string;
|
|
13
|
+
method?: string;
|
|
14
|
+
operationId?: string;
|
|
15
|
+
clientRequestId?: string;
|
|
16
|
+
serverRequestId?: string;
|
|
17
|
+
serverCode?: string;
|
|
18
|
+
status?: number;
|
|
19
|
+
durationMs?: number;
|
|
12
20
|
};
|
|
13
21
|
readonly retryable: boolean;
|
|
14
22
|
readonly status?: number;
|
|
@@ -74,6 +74,7 @@ export declare class ChatEngine {
|
|
|
74
74
|
private threadSubscribers;
|
|
75
75
|
private openQueue;
|
|
76
76
|
private opening;
|
|
77
|
+
private threadRecovery;
|
|
77
78
|
private drainOpens;
|
|
78
79
|
/** Listen first, then open the local window; concurrent consumers share the initial read. */
|
|
79
80
|
subscribeThread(threadId: string, listener: (snapshot: ChatThreadSnapshot) => void, options?: SubscribeOptions<ChatThreadSnapshot>): () => void;
|
|
@@ -108,15 +108,18 @@ export class ChatEngine {
|
|
|
108
108
|
}
|
|
109
109
|
threadSubscribers = new Map();
|
|
110
110
|
openQueue = new Set();
|
|
111
|
-
opening =
|
|
111
|
+
opening = new Set();
|
|
112
|
+
threadRecovery = new Map();
|
|
112
113
|
drainOpens() {
|
|
113
|
-
while (!this.closed && this.opening < 4 && this.openQueue.size) {
|
|
114
|
-
const id = this.openQueue.
|
|
114
|
+
while (!this.closed && this.opening.size < 4 && this.openQueue.size) {
|
|
115
|
+
const id = [...this.openQueue].find(id => !this.opening.has(id));
|
|
116
|
+
if (id === undefined)
|
|
117
|
+
break;
|
|
115
118
|
this.openQueue.delete(id);
|
|
116
119
|
if (!this.threadSubscribers.has(id))
|
|
117
120
|
continue;
|
|
118
|
-
this.opening
|
|
119
|
-
void this.getThread(id).catch(() => { }).finally(() => { this.opening
|
|
121
|
+
this.opening.add(id);
|
|
122
|
+
void this.getThread(id).catch(() => { }).finally(() => { this.opening.delete(id); this.drainOpens(); });
|
|
120
123
|
}
|
|
121
124
|
}
|
|
122
125
|
/** Listen first, then open the local window; concurrent consumers share the initial read. */
|
|
@@ -124,6 +127,20 @@ export class ChatEngine {
|
|
|
124
127
|
const off = this.subscribe(threadId, listener, options), count = this.threadSubscribers.get(threadId) ?? 0;
|
|
125
128
|
this.threadSubscribers.set(threadId, count + 1);
|
|
126
129
|
if (!count) {
|
|
130
|
+
// Track coverage independently of consumer field filters. A canonical history
|
|
131
|
+
// reset also invalidates open discussion windows sharing that source epoch.
|
|
132
|
+
let complete = this.advanced.getSnapshot(threadId).complete;
|
|
133
|
+
this.threadRecovery.set(threadId, this.advanced.subscribeChanges(threadId, () => {
|
|
134
|
+
const next = this.advanced.getSnapshot(threadId).complete, lost = complete && !next;
|
|
135
|
+
complete = next;
|
|
136
|
+
if (lost)
|
|
137
|
+
queueMicrotask(() => {
|
|
138
|
+
if (this.closed || !this.advanced.active || !this.threadSubscribers.has(threadId) || this.getSnapshot(threadId).status !== "pending")
|
|
139
|
+
return;
|
|
140
|
+
this.openQueue.add(threadId);
|
|
141
|
+
this.drainOpens();
|
|
142
|
+
});
|
|
143
|
+
}, ["complete"]));
|
|
127
144
|
this.openQueue.add(threadId);
|
|
128
145
|
this.drainOpens();
|
|
129
146
|
}
|
|
@@ -139,6 +156,8 @@ export class ChatEngine {
|
|
|
139
156
|
else {
|
|
140
157
|
this.threadSubscribers.delete(threadId);
|
|
141
158
|
this.openQueue.delete(threadId);
|
|
159
|
+
this.threadRecovery.get(threadId)?.();
|
|
160
|
+
this.threadRecovery.delete(threadId);
|
|
142
161
|
}
|
|
143
162
|
};
|
|
144
163
|
}
|
|
@@ -283,5 +302,5 @@ export class ChatEngine {
|
|
|
283
302
|
updateGroup(threadId, changes) { return this.threadOperation(threadId, async () => this.group(await this.advanced.updateGroup(threadId, changes))); }
|
|
284
303
|
dissolveGroup(threadId, options = {}) { return this.threadOperation(threadId, async () => this.group(await this.advanced.dissolveGroup(threadId, options))); }
|
|
285
304
|
revokeAccess() { return this.operation(() => this.advanced.revokeAccess()); }
|
|
286
|
-
close() { this.closed = true; return this.operation(() => this.advanced.close()).finally(() => { this.errors.clear(); this.targets.clear(); this.listeners.clear(); this.threadSubscribers.clear(); this.openQueue.clear(); this.views.clear(); this.summary = undefined; this.present = createMessagePresenter(); this.messageLists = new WeakMap(); this.groups = new WeakMap(); this.redactedQuotes = new WeakMap(); }); }
|
|
305
|
+
close() { this.closed = true; this.threadRecovery.forEach(off => off()); this.threadRecovery.clear(); return this.operation(() => this.advanced.close()).finally(() => { this.errors.clear(); this.targets.clear(); this.listeners.clear(); this.threadSubscribers.clear(); this.openQueue.clear(); this.views.clear(); this.summary = undefined; this.present = createMessagePresenter(); this.messageLists = new WeakMap(); this.groups = new WeakMap(); this.redactedQuotes = new WeakMap(); }); }
|
|
287
306
|
}
|
|
@@ -2,6 +2,40 @@ import { asChatError, ChatError } from "./chatError.js";
|
|
|
2
2
|
import { isOffline, isOfflineNetworkFailure } from "./connectivity.js";
|
|
3
3
|
import { IndexedDbResponseCache, isResponseCacheAuthorityError, assertResponseCacheAccess } from "./responseCache.js";
|
|
4
4
|
import { messageScopeKey } from "./messageStore.js";
|
|
5
|
+
const requestId = () => crypto.randomUUID();
|
|
6
|
+
const safeId = (value) => value && /^[a-zA-Z0-9_.:@-]{1,128}$/.test(value) ? value : undefined;
|
|
7
|
+
// Error bodies are untrusted; never retain their text, scope, or arbitrary fields.
|
|
8
|
+
async function failureCode(response) {
|
|
9
|
+
if (response.bodyUsed || response.body?.locked)
|
|
10
|
+
return;
|
|
11
|
+
const reader = response.body?.getReader();
|
|
12
|
+
if (!reader)
|
|
13
|
+
return;
|
|
14
|
+
let timer;
|
|
15
|
+
try {
|
|
16
|
+
const deadline = new Promise((_, reject) => { timer = setTimeout(() => reject(new Error("READ_COUNTS_DIAGNOSTIC_TIMEOUT")), 250); });
|
|
17
|
+
let text = "", bytes = 0;
|
|
18
|
+
const decoder = new TextDecoder();
|
|
19
|
+
while (true) {
|
|
20
|
+
const part = await Promise.race([reader.read(), deadline]);
|
|
21
|
+
if (part.done)
|
|
22
|
+
break;
|
|
23
|
+
bytes += part.value.byteLength;
|
|
24
|
+
if (bytes > 4096)
|
|
25
|
+
return;
|
|
26
|
+
text += decoder.decode(part.value, { stream: true });
|
|
27
|
+
}
|
|
28
|
+
const code = JSON.parse(text + decoder.decode())?.message;
|
|
29
|
+
return typeof code === "string" && /^(SOCIAL_READ_(UNAVAILABLE|IDENTITY_UNAVAILABLE|FORBIDDEN)|INVALID_READ_COUNTS|UNAUTHORIZED|INVALID_SOCIAL_READ_IDENTITY|READ_RECEIVER_NOT_READY)$/.test(code) ? code : undefined;
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
finally {
|
|
35
|
+
clearTimeout(timer);
|
|
36
|
+
void reader.cancel().catch(() => { });
|
|
37
|
+
}
|
|
38
|
+
}
|
|
5
39
|
let subscriptionSequence = 0;
|
|
6
40
|
function nextSubscriptionSequence() { subscriptionSequence = Math.max(Date.now() * 1000, subscriptionSequence + 1); return subscriptionSequence; }
|
|
7
41
|
/** One active conversation; fixed event window, bounded batches, no count polling. */
|
|
@@ -13,6 +47,7 @@ export class ReadCountsClient {
|
|
|
13
47
|
heartbeat;
|
|
14
48
|
controller;
|
|
15
49
|
generation = 0;
|
|
50
|
+
operationId = requestId();
|
|
16
51
|
retries = 0;
|
|
17
52
|
closed = false;
|
|
18
53
|
closing;
|
|
@@ -54,15 +89,29 @@ export class ReadCountsClient {
|
|
|
54
89
|
}
|
|
55
90
|
async post(path, body, signal) {
|
|
56
91
|
const scopeVersion = this.options.access ? await assertResponseCacheAccess(this.options.access) : undefined;
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
92
|
+
const clientRequestId = requestId(), started = performance.now();
|
|
93
|
+
const diagnostic = { source: "read-counts", route: path, method: "POST", operationId: this.operationId, clientRequestId, attempt: path.endsWith("/counts") ? this.retries + 1 : 1 };
|
|
94
|
+
try {
|
|
95
|
+
const response = await (this.options.fetch ?? fetch)(`${this.options.hubHttpUrl.replace(/\/$/, "")}${path}`, { method: "POST", headers: { authorization: `Bearer ${this.options.token}`, "content-type": "application/json", "x-request-id": clientRequestId }, body: JSON.stringify(body), signal: signal ? AbortSignal.any([signal, AbortSignal.timeout(8000)]) : AbortSignal.timeout(8000), redirect: "error" });
|
|
96
|
+
diagnostic.status = response.status;
|
|
97
|
+
diagnostic.serverRequestId = safeId(response.headers.get("x-request-id"));
|
|
98
|
+
if (!response.ok) {
|
|
99
|
+
diagnostic.serverCode = await failureCode(response);
|
|
100
|
+
throw Object.assign(new Error(`READ_COUNTS_HTTP_${response.status}`), { status: response.status });
|
|
101
|
+
}
|
|
102
|
+
const envelope = await response.json();
|
|
103
|
+
if (envelope.success !== true)
|
|
104
|
+
throw new Error("INVALID_READ_COUNTS_RESPONSE");
|
|
105
|
+
diagnostic.durationMs = Math.round(performance.now() - started);
|
|
106
|
+
// Keep validation and retry behavior unchanged; add only transport diagnostics.
|
|
107
|
+
if (this.options.access)
|
|
108
|
+
await assertResponseCacheAccess(this.options.access, scopeVersion);
|
|
109
|
+
return envelope.data;
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
diagnostic.durationMs = Math.round(performance.now() - started);
|
|
113
|
+
throw Object.assign(error instanceof Error ? error : new Error("READ_COUNTS_REQUEST_FAILED"), { diagnostic });
|
|
114
|
+
}
|
|
66
115
|
}
|
|
67
116
|
subscription(threadId, release) {
|
|
68
117
|
if (release && isOffline())
|
|
@@ -126,6 +175,7 @@ export class ReadCountsClient {
|
|
|
126
175
|
return;
|
|
127
176
|
}
|
|
128
177
|
const old = this.thread;
|
|
178
|
+
this.operationId = requestId();
|
|
129
179
|
this.generation++;
|
|
130
180
|
this.controller?.abort();
|
|
131
181
|
clearTimeout(this.timer);
|
package/package.json
CHANGED