@blogmcp/sdk 0.13.0 → 0.14.0

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 (61) hide show
  1. package/dist/community/links.d.ts +6 -0
  2. package/dist/community/links.js +9 -1
  3. package/dist/community/outcome.d.ts +1 -1
  4. package/dist/community/outcome.js +60 -23
  5. package/dist/community/private/browser.d.ts +18 -0
  6. package/dist/community/private/browser.js +17 -0
  7. package/dist/community/private/client.d.ts +259 -0
  8. package/dist/community/private/client.js +98 -0
  9. package/dist/community/private/dm.d.ts +547 -0
  10. package/dist/community/private/dm.js +852 -0
  11. package/dist/community/private/dpop.d.ts +59 -0
  12. package/dist/community/private/dpop.js +46 -0
  13. package/dist/community/private/encoding.d.ts +17 -0
  14. package/dist/community/private/encoding.js +50 -0
  15. package/dist/community/private/host-state.d.ts +20 -0
  16. package/dist/community/private/host-state.js +15 -0
  17. package/dist/community/private/index.d.ts +51 -0
  18. package/dist/community/private/index.js +71 -0
  19. package/dist/community/private/keystore.d.ts +73 -0
  20. package/dist/community/private/keystore.js +91 -0
  21. package/dist/community/private/marker.d.ts +17 -0
  22. package/dist/community/private/marker.js +11 -0
  23. package/dist/community/private/private-routes.d.ts +6 -0
  24. package/dist/community/private/private-routes.js +72 -0
  25. package/dist/community/private/promise.d.ts +65 -0
  26. package/dist/community/private/promise.js +39 -0
  27. package/dist/community/private/realtime.d.ts +127 -0
  28. package/dist/community/private/realtime.js +199 -0
  29. package/dist/community/private/session.d.ts +175 -0
  30. package/dist/community/private/session.js +441 -0
  31. package/dist/community/private/transport.d.ts +47 -0
  32. package/dist/community/private/transport.js +65 -0
  33. package/dist/community/server/grant-handoff.d.ts +53 -0
  34. package/dist/community/server/grant-handoff.js +96 -0
  35. package/dist/community/server/grant.d.ts +100 -0
  36. package/dist/community/server/grant.js +165 -0
  37. package/dist/community/server/index.d.ts +4 -0
  38. package/dist/community/server/index.js +20 -16
  39. package/dist/community/shapes.d.ts +27 -1
  40. package/dist/community/shapes.js +27 -17
  41. package/package.json +7 -3
  42. package/src/community/links.ts +8 -0
  43. package/src/community/outcome.ts +37 -0
  44. package/src/community/private/browser.ts +31 -0
  45. package/src/community/private/client.ts +304 -0
  46. package/src/community/private/dm.ts +1687 -0
  47. package/src/community/private/dpop.ts +110 -0
  48. package/src/community/private/encoding.ts +71 -0
  49. package/src/community/private/host-state.ts +37 -0
  50. package/src/community/private/index.ts +169 -0
  51. package/src/community/private/keystore.ts +179 -0
  52. package/src/community/private/marker.ts +28 -0
  53. package/src/community/private/private-routes.ts +80 -0
  54. package/src/community/private/promise.ts +125 -0
  55. package/src/community/private/realtime.ts +431 -0
  56. package/src/community/private/session.ts +848 -0
  57. package/src/community/private/transport.ts +125 -0
  58. package/src/community/server/grant-handoff.ts +179 -0
  59. package/src/community/server/grant.ts +292 -0
  60. package/src/community/server/index.ts +6 -0
  61. package/src/community/shapes.ts +46 -1
@@ -0,0 +1,852 @@
1
+ import { assertPrivateBrowser as ye } from "./browser.js";
2
+ ye();
3
+ const ze = Object.freeze([
4
+ "dm/list",
5
+ "dm/open",
6
+ "dm/messages",
7
+ "dm/send",
8
+ "dm/edit",
9
+ "dm/delete",
10
+ "dm/hide",
11
+ "dm/clear",
12
+ "dm/read",
13
+ "dm/typing",
14
+ "requests/list",
15
+ "requests/accept",
16
+ "requests/delete",
17
+ "requests/block-report",
18
+ "blocks/add",
19
+ "blocks/remove",
20
+ "blocks/list",
21
+ "audience/check",
22
+ "reports/preview",
23
+ "reports/submit",
24
+ "cases/list",
25
+ "cases/get",
26
+ "statements/get",
27
+ "appeals/submit",
28
+ "export/download"
29
+ ]), Pe = Object.freeze({}), O = 50, Se = 3e3, se = 7e3, W = Object.freeze({ baseMs: 1e3, maxMs: 3e4, boundedMs: 12e4 }), Ae = 20, Te = Object.freeze(
30
+ /* @__PURE__ */ new Set(["NETWORK", "BUSY", "UNAVAILABLE", "TOO_FAST", "INTERNAL", "LEDGER_UNAVAILABLE"])
31
+ ), Me = Object.freeze(/* @__PURE__ */ new Set(["TOO_FAST", "INTERNAL"]));
32
+ function Oe(n) {
33
+ return Te.has(n.code) ? !0 : n.code === "BAD_ANSWER" && (n.status === null || n.status >= 500 || n.status === 408 || n.status === 429);
34
+ }
35
+ function we(n) {
36
+ return Me.has(n.code) ? !0 : n.code === "BAD_ANSWER" && (n.status === null || n.status === 408 || n.status === 429);
37
+ }
38
+ const qe = Object.freeze(["visible", "deleted", "removed"]), Ue = Object.freeze(["m", "e", "d", "t", "rq", "rd", "resync"]);
39
+ function Ce(n) {
40
+ switch (n.k) {
41
+ case "m":
42
+ case "d":
43
+ case "rq":
44
+ case "resync":
45
+ return "inbox";
46
+ case "e":
47
+ return "thread";
48
+ case "t":
49
+ return "typing";
50
+ case "rd":
51
+ return "receipt";
52
+ default:
53
+ return "other";
54
+ }
55
+ }
56
+ const ae = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i, xe = new Set(qe), l = () => ({ ok: !1, code: "VALIDATION", status: null }), h = () => ({ ok: !1, code: "BAD_ANSWER", status: 200 }), z = () => ({ ok: !1, code: "CANCELLED", status: null }), c = (n) => typeof n == "string" && ae.test(n), w = (n) => typeof n == "number" && Number.isSafeInteger(n) && n >= 1, S = (n) => typeof n == "object" && n !== null && !Array.isArray(n);
57
+ function F(n, i) {
58
+ if (!S(n)) return null;
59
+ const d = typeof n.conversationId == "string" ? n.conversationId.toLowerCase() : null;
60
+ if (d === null || !ae.test(d) || i !== void 0 && d !== i.toLowerCase() || !w(n.seq) || typeof n.id != "string" || n.id === "" || typeof n.rev != "number" || !Number.isSafeInteger(n.rev) || n.rev < 0 || typeof n.state != "string" || !xe.has(n.state)) return null;
61
+ const v = n.state, p = v === "visible" ? n.text : null;
62
+ return v === "visible" && typeof p != "string" || typeof n.createdAt != "string" ? null : Object.freeze({
63
+ conversationId: d,
64
+ seq: n.seq,
65
+ id: n.id,
66
+ rev: n.rev,
67
+ mine: n.mine === !0,
68
+ replyToSeq: w(n.replyToSeq) ? n.replyToSeq : null,
69
+ state: v,
70
+ text: p,
71
+ editedMark: n.editedMark === !0,
72
+ createdAt: n.createdAt,
73
+ editedAt: typeof n.editedAt == "string" ? n.editedAt : null,
74
+ clientMsgId: c(n.clientMsgId) ? n.clientMsgId.toLowerCase() : null
75
+ });
76
+ }
77
+ function ne(n, i) {
78
+ if (!Array.isArray(n)) return null;
79
+ const d = [];
80
+ for (const v of n) {
81
+ const p = F(v, i);
82
+ p && d.push(p);
83
+ }
84
+ return d.sort((v, p) => v.seq - p.seq);
85
+ }
86
+ const re = (n) => typeof n == "number" && Number.isSafeInteger(n) && n >= 0, I = (n) => typeof n == "string" && n !== "", G = (n) => n == null ? null : typeof n == "string" ? n : void 0, le = (n) => n === "open" || n === "request", Ee = (n) => n === "rules" || n === "illegal", Re = Object.freeze({
87
+ kind: "member",
88
+ memberId: null,
89
+ handle: null,
90
+ displayName: null,
91
+ avatarPreset: null,
92
+ deleted: !0
93
+ });
94
+ function Y(n) {
95
+ if (!S(n) || n.kind !== "member") return null;
96
+ if (n.deleted === !0 && n.memberId === null) return Re;
97
+ if (!c(n.memberId) || !I(n.handle) || !I(n.displayName)) return null;
98
+ const i = G(n.avatarPreset);
99
+ return i === void 0 ? null : Object.freeze({ kind: "member", memberId: n.memberId.toLowerCase(), handle: n.handle, displayName: n.displayName, avatarPreset: i });
100
+ }
101
+ function Le(n) {
102
+ if (!S(n) || !c(n.conversationId) || !le(n.state)) return null;
103
+ const i = Y(n.counterpart);
104
+ return !i || !I(n.lastActivityAt) || !re(n.lastSeq) || !re(n.unread) ? null : Object.freeze({
105
+ conversationId: n.conversationId.toLowerCase(),
106
+ state: n.state,
107
+ counterpart: i,
108
+ lastActivityAt: n.lastActivityAt,
109
+ lastSeq: n.lastSeq,
110
+ unread: n.unread
111
+ });
112
+ }
113
+ function _e(n) {
114
+ if (!S(n) || !c(n.conversationId) || !I(n.createdAt)) return null;
115
+ const i = Y(n.from), d = G(n.lastMessageAt);
116
+ return !i || d === void 0 ? null : Object.freeze({ conversationId: n.conversationId.toLowerCase(), from: i, createdAt: n.createdAt, lastMessageAt: d });
117
+ }
118
+ function Ne(n) {
119
+ if (!S(n) || !c(n.memberId) || !I(n.blockedAt)) return null;
120
+ const i = Y(n.author);
121
+ return i ? Object.freeze({ memberId: n.memberId.toLowerCase(), author: i, blockedAt: n.blockedAt }) : null;
122
+ }
123
+ function oe(n) {
124
+ return !S(n) || !c(n.caseId) || !I(n.state) || !Ee(n.line) || !I(n.openedAt) || n.decisionId !== void 0 && n.decisionId !== null && !c(n.decisionId) ? null : Object.freeze({
125
+ caseId: n.caseId.toLowerCase(),
126
+ state: n.state,
127
+ line: n.line,
128
+ openedAt: n.openedAt,
129
+ decisionId: c(n.decisionId) ? n.decisionId.toLowerCase() : null
130
+ });
131
+ }
132
+ function P(n, i) {
133
+ if (!Array.isArray(n)) return null;
134
+ const d = [];
135
+ for (const v of n) {
136
+ const p = i(v);
137
+ p && d.push(p);
138
+ }
139
+ return d;
140
+ }
141
+ function ie(n) {
142
+ if (typeof n != "string") return null;
143
+ const i = n.trim().replace(/^@/, "");
144
+ return i === "" || i.length > 64 ? null : i;
145
+ }
146
+ function _(n) {
147
+ return typeof n == "string" && n.trim() !== "" ? n : null;
148
+ }
149
+ function Ve(n, i = {}) {
150
+ const d = n?.session;
151
+ if (!d || typeof d.call != "function") throw new TypeError("[blogmcp-sdk] createPrivateDmClient needs the private client (createPrivateClient)");
152
+ const v = Math.max(1, i.retry?.baseMs ?? W.baseMs), p = Math.max(v, i.retry?.maxMs ?? W.maxMs), ce = Math.max(0, i.retry?.boundedMs ?? W.boundedMs), ue = i.random ?? Math.random, de = i.randomUUID ?? (() => globalThis.crypto.randomUUID()), q = i.now ?? Date.now, fe = () => {
153
+ try {
154
+ const e = i.self?.() ?? null;
155
+ return c(e) ? e.toLowerCase() : null;
156
+ } catch {
157
+ return null;
158
+ }
159
+ };
160
+ let k = !1;
161
+ const A = /* @__PURE__ */ new Map(), g = [], U = /* @__PURE__ */ new Set(), T = /* @__PURE__ */ new Map(), N = /* @__PURE__ */ new Map(), V = /* @__PURE__ */ new Map(), m = /* @__PURE__ */ new Map();
162
+ async function K(e, t) {
163
+ try {
164
+ return { out: await d.call(e, t), threw: !1 };
165
+ } catch {
166
+ return { out: { ok: !1, code: "NETWORK", status: null }, threw: !0 };
167
+ }
168
+ }
169
+ const f = async (e, t = {}) => (await K(e, t)).out;
170
+ async function M(e, t) {
171
+ const s = await f(e, t);
172
+ return s.ok ? { ok: !0, value: null } : s;
173
+ }
174
+ async function H(e, t, s) {
175
+ const r = await f(e, t);
176
+ if (!r.ok) return r;
177
+ const o = F(r.value.message, s);
178
+ return o ? (m.get(s)?.merge([o]), { ok: !0, value: { message: o } }) : h();
179
+ }
180
+ function X(e) {
181
+ return Object.freeze({
182
+ clientMsgId: e.clientMsgId,
183
+ conversationId: e.conversationId,
184
+ text: e.text,
185
+ replyToSeq: e.replyToSeq,
186
+ state: e.state,
187
+ attempts: e.attempts,
188
+ lastCode: e.lastCode
189
+ });
190
+ }
191
+ function C(e) {
192
+ m.get(e)?.changed();
193
+ }
194
+ function x(e) {
195
+ e.gone = !0;
196
+ const t = g.indexOf(e);
197
+ t >= 0 && g.splice(t, 1), e.timer !== null && clearTimeout(e.timer), e.timer = null;
198
+ const s = e.wake;
199
+ e.wake = null, s?.(), C(e.conversationId);
200
+ }
201
+ function he(e) {
202
+ const t = Math.min(p, v * 2 ** Math.min(e - 1, 30)), s = Math.min(Math.max(ue(), 0), 1);
203
+ return Math.round(t / 2 + s * (t / 2));
204
+ }
205
+ function me(e, t) {
206
+ return new Promise((s) => {
207
+ const r = () => {
208
+ e.timer !== null && clearTimeout(e.timer), e.timer = null, e.wake = null, s();
209
+ };
210
+ e.wake = r, e.timer = setTimeout(r, t);
211
+ });
212
+ }
213
+ function j(e, t) {
214
+ e.lastCode = t.code, e.state = "failed", C(e.conversationId), e.settle(t);
215
+ }
216
+ async function ge(e) {
217
+ let t = 0, s = null;
218
+ for (; ; ) {
219
+ if (e.gone) return;
220
+ e.state = "sending", e.attempts++, C(e.conversationId);
221
+ const { out: r, threw: o } = await K("dm/send", {
222
+ conversationId: e.conversationId,
223
+ clientMsgId: e.clientMsgId,
224
+ text: e.text,
225
+ ...e.replyToSeq !== null ? { replyToSeq: e.replyToSeq } : {}
226
+ });
227
+ if (e.gone) return;
228
+ if (s !== null && (t += Math.max(0, q() - s), s = null), r.ok) {
229
+ const a = F(r.value.message, e.conversationId);
230
+ if (a) {
231
+ x(e), m.get(e.conversationId)?.merge([a]), e.settle({ ok: !0, value: { message: a } });
232
+ return;
233
+ }
234
+ j(e, h());
235
+ return;
236
+ }
237
+ if (!Oe(r)) {
238
+ j(e, r);
239
+ return;
240
+ }
241
+ if (o || we(r)) {
242
+ if (t >= ce) {
243
+ j(e, r);
244
+ return;
245
+ }
246
+ s = q();
247
+ }
248
+ e.lastCode = r.code, e.state = "waiting", C(e.conversationId), await me(e, he(e.attempts));
249
+ }
250
+ }
251
+ async function ve(e) {
252
+ if (!U.has(e)) {
253
+ U.add(e);
254
+ try {
255
+ for (; ; ) {
256
+ if (k) return;
257
+ const t = g.find((s) => s.conversationId === e && !s.gone && s.state !== "failed");
258
+ if (!t) return;
259
+ try {
260
+ await ge(t);
261
+ } catch {
262
+ !t.gone && t.state !== "failed" && j(t, { ok: !1, code: "NETWORK", status: null });
263
+ }
264
+ }
265
+ } finally {
266
+ U.delete(e);
267
+ }
268
+ }
269
+ }
270
+ function $(e, t, s, r, o) {
271
+ let a;
272
+ const u = new Promise((B) => {
273
+ a = B;
274
+ });
275
+ let y = !1;
276
+ const b = {
277
+ clientMsgId: r,
278
+ conversationId: e,
279
+ text: t,
280
+ replyToSeq: s,
281
+ state: "queued",
282
+ attempts: 0,
283
+ lastCode: null,
284
+ gone: !1,
285
+ wake: null,
286
+ timer: null,
287
+ settle: (B) => {
288
+ y || (y = !0, a(B));
289
+ },
290
+ done: u
291
+ }, L = o ? g.indexOf(o) : -1;
292
+ return o && (o.gone = !0), L >= 0 ? g.splice(L, 1, b) : g.push(b), C(e), ve(e).catch(() => {
293
+ }), b;
294
+ }
295
+ function J(e) {
296
+ return {
297
+ clientMsgId: e.clientMsgId,
298
+ done: e.done,
299
+ cancel() {
300
+ e.gone || (x(e), e.settle(z()));
301
+ }
302
+ };
303
+ }
304
+ function Q(e) {
305
+ const t = c(e?.conversationId) ? e.conversationId.toLowerCase() : null, s = _(e?.text), r = e?.replyToSeq === void 0 ? null : w(e.replyToSeq) ? e.replyToSeq : void 0, o = de().toLowerCase();
306
+ return k ? { sending: { clientMsgId: o, done: Promise.resolve(z()), cancel: () => {
307
+ } }, queued: !1 } : t === null || s === null || r === void 0 ? { sending: { clientMsgId: o, done: Promise.resolve(l()), cancel: () => {
308
+ } }, queued: !1 } : { sending: J($(t, s, r, o)), queued: !0 };
309
+ }
310
+ const pe = (e) => Q(e).sending;
311
+ async function Z(e, t) {
312
+ if (!c(e) || !w(t)) return l();
313
+ const s = e.toLowerCase(), r = N.get(s);
314
+ if (r && await r.catch(() => {
315
+ }), (T.get(s) ?? 0) >= t) return { ok: !0, value: { sent: !1, notificationsRead: 0 } };
316
+ const o = (async () => {
317
+ const a = await f("dm/read", { conversationId: s, seq: t });
318
+ if (!a.ok) return a;
319
+ T.set(s, Math.max(T.get(s) ?? 0, t));
320
+ const u = a.value?.notificationsRead;
321
+ return { ok: !0, value: { sent: !0, notificationsRead: typeof u == "number" && Number.isSafeInteger(u) && u > 0 ? u : 0 } };
322
+ })();
323
+ N.set(s, o);
324
+ try {
325
+ return await o;
326
+ } finally {
327
+ N.get(s) === o && N.delete(s);
328
+ }
329
+ }
330
+ async function be(e) {
331
+ if (!c(e)) return l();
332
+ const t = e.toLowerCase(), s = q(), r = V.get(t);
333
+ if (r !== void 0 && s - r < Se) return { ok: !0, value: { sent: !1 } };
334
+ V.set(t, s);
335
+ const o = await f("dm/typing", { conversationId: t });
336
+ return o.ok ? { ok: !0, value: { sent: !0 } } : o;
337
+ }
338
+ class Ie {
339
+ conversationId;
340
+ bySeq = /* @__PURE__ */ new Map();
341
+ listeners = /* @__PURE__ */ new Set();
342
+ hasOlder = !1;
343
+ loaded = !1;
344
+ loadingCount = 0;
345
+ readUpTo = null;
346
+ typingUntil = 0;
347
+ typingTimer = null;
348
+ error = null;
349
+ ended = !1;
350
+ snapshot = null;
351
+ /** `loadNewer` coalesces: one read at a time, and one more after it when an event came in meanwhile. */
352
+ newer = null;
353
+ newerAgain = !1;
354
+ /**
355
+ * An event about this conversation came in while the first page was on its way: that page may have been read
356
+ * before the change, so the thread reads again once it has loaded rather than drop the event.
357
+ */
358
+ missed = !1;
359
+ constructor(t) {
360
+ this.conversationId = t;
361
+ }
362
+ state() {
363
+ if (this.snapshot) return this.snapshot;
364
+ const t = [...this.bySeq.values()].sort((s, r) => s.seq - r.seq);
365
+ return this.snapshot = Object.freeze({
366
+ conversationId: this.conversationId,
367
+ messages: Object.freeze(t),
368
+ pending: Object.freeze(g.filter((s) => s.conversationId === this.conversationId && !s.gone).map(X)),
369
+ hasOlder: this.hasOlder,
370
+ loading: this.loadingCount > 0,
371
+ loaded: this.loaded,
372
+ readUpTo: this.readUpTo,
373
+ typing: this.typingUntil > q(),
374
+ error: this.error
375
+ }), this.snapshot;
376
+ }
377
+ subscribe(t) {
378
+ return this.ended ? () => {
379
+ } : (this.listeners.add(t), t(this.state()), () => {
380
+ this.listeners.delete(t);
381
+ });
382
+ }
383
+ changed() {
384
+ if (this.ended) return;
385
+ this.snapshot = null;
386
+ const t = this.state();
387
+ for (const s of [...this.listeners])
388
+ try {
389
+ s(t);
390
+ } catch (r) {
391
+ queueMicrotask(() => {
392
+ throw r;
393
+ });
394
+ }
395
+ }
396
+ /** Keep each message at its highest revision; a pending copy settles when its stored row arrives. */
397
+ merge(t) {
398
+ if (this.ended) return;
399
+ let s = !1;
400
+ for (const r of t) {
401
+ const o = this.bySeq.get(r.seq);
402
+ if (!(o && r.rev <= o.rev) && (this.bySeq.set(r.seq, r), s = !0, r.clientMsgId !== null)) {
403
+ const a = g.find((u) => u.clientMsgId === r.clientMsgId && !u.gone);
404
+ a && (x(a), a.settle({ ok: !0, value: { message: r } }));
405
+ }
406
+ }
407
+ s && this.changed();
408
+ }
409
+ drop(t) {
410
+ if (t === "all") this.bySeq.clear();
411
+ else for (const s of t) this.bySeq.delete(s);
412
+ this.changed();
413
+ }
414
+ /**
415
+ * The receipt a page carried: a number moves `readUpTo` forward (a live `rd` may already have moved it further, so
416
+ * never back); null — receipts are no longer mutual — clears it; undefined (not said) leaves it.
417
+ */
418
+ receipt(t) {
419
+ if (t !== void 0) {
420
+ if (t === null) {
421
+ this.readUpTo !== null && (this.readUpTo = null, this.changed());
422
+ return;
423
+ }
424
+ (this.readUpTo === null || t > this.readUpTo) && (this.readUpTo = t, this.changed());
425
+ }
426
+ }
427
+ highest() {
428
+ let t = 0;
429
+ for (const s of this.bySeq.keys()) s > t && (t = s);
430
+ return t;
431
+ }
432
+ lowest() {
433
+ let t = null;
434
+ for (const s of this.bySeq.keys()) (t === null || s < t) && (t = s);
435
+ return t;
436
+ }
437
+ async page(t, s) {
438
+ this.loadingCount++, this.changed();
439
+ try {
440
+ const r = await ee({ conversationId: this.conversationId, ...t });
441
+ return this.ended || (this.error = r.ok ? null : r.code, r.ok && (s !== void 0 && this.joinOrReplace(r.value.messages, s), this.merge(r.value.messages), this.receipt(r.value.counterpartReadSeq))), r;
442
+ } finally {
443
+ this.loadingCount--, this.changed();
444
+ }
445
+ }
446
+ async loadLatest() {
447
+ const t = this.highest(), s = !this.loaded, r = await this.page({ limit: O }, t);
448
+ return r.ok ? (this.ended || (s && (this.hasOlder = r.value.hasMore), this.loaded = !0, this.changed(), this.missed && (this.missed = !1, this.refresh())), { ok: !0, value: null }) : r;
449
+ }
450
+ /**
451
+ * A page that does not join the loaded messages (it starts after the newest loaded one + 1): the messages in
452
+ * between were never read, so the loaded ones below the page go and `loadOlder` reads down from the page — the
453
+ * thread never shows a gap as if nothing had been written there.
454
+ */
455
+ joinOrReplace(t, s) {
456
+ const r = t.length > 0 ? t[0].seq : null;
457
+ if (!(r === null || s === 0 || r <= s + 1)) {
458
+ for (const o of [...this.bySeq.keys()]) o < r && this.bySeq.delete(o);
459
+ this.hasOlder = !0;
460
+ }
461
+ }
462
+ async loadOlder() {
463
+ const t = this.lowest();
464
+ if (!this.loaded || t === null) return this.loadLatest();
465
+ if (t <= 1)
466
+ return this.hasOlder = !1, this.changed(), { ok: !0, value: null };
467
+ const s = await this.page({ beforeSeq: t, limit: O });
468
+ return s.ok ? (this.ended || (this.hasOlder = s.value.hasMore, this.changed()), { ok: !0, value: null }) : s;
469
+ }
470
+ loadNewer() {
471
+ if (this.newer)
472
+ return this.newerAgain = !0, this.newer;
473
+ const t = (async () => {
474
+ try {
475
+ let s = { ok: !0, value: null };
476
+ do {
477
+ this.newerAgain = !1;
478
+ const r = await this.readAfter(this.highest());
479
+ s = r.ok ? { ok: !0, value: null } : r;
480
+ } while (s.ok && this.newerAgain && !this.ended);
481
+ return s;
482
+ } finally {
483
+ this.newer = null;
484
+ }
485
+ })();
486
+ return this.newer = t, t;
487
+ }
488
+ /**
489
+ * Every page after `after`, at most PRIVATE_DM_REREAD_PAGES of them; `seen` collects the seqs the answers carried.
490
+ * When the pages run out before the newest message, the newest page is read instead and the loaded messages below
491
+ * it go (`joinOrReplace`), so the thread holds no gap. Answers whether the read reached the newest message.
492
+ */
493
+ async readAfter(t, s) {
494
+ if (!this.loaded) {
495
+ const a = await this.loadLatest();
496
+ return a.ok ? { ok: !0, value: { complete: !1 } } : a;
497
+ }
498
+ let r = t;
499
+ for (let a = 0; a < Ae && !this.ended; a++) {
500
+ const u = await this.page({ afterSeq: r, limit: O });
501
+ if (!u.ok) return u;
502
+ for (const b of u.value.messages) s?.add(b.seq);
503
+ const y = u.value.messages[u.value.messages.length - 1];
504
+ if (!u.value.hasMore || !y || y.seq <= r) return { ok: !0, value: { complete: !0 } };
505
+ r = y.seq;
506
+ }
507
+ if (this.ended) return { ok: !0, value: { complete: !1 } };
508
+ const o = await this.page({ limit: O }, r);
509
+ return o.ok ? { ok: !0, value: { complete: !1 } } : o;
510
+ }
511
+ /**
512
+ * Re-read every loaded message (after a `resync`). A loaded message the answers no longer carry — hidden or cleared
513
+ * on another device, or a row this client can no longer read — goes, once the read reached the newest message.
514
+ */
515
+ async refresh() {
516
+ const t = this.lowest();
517
+ if (!this.loaded || t === null) return this.loadLatest();
518
+ const s = /* @__PURE__ */ new Set(), r = await this.readAfter(t - 1, s);
519
+ if (!r.ok) return r;
520
+ if (r.value.complete && !this.ended) {
521
+ const o = [...this.bySeq.keys()].filter((a) => a >= t && !s.has(a));
522
+ o.length > 0 && this.drop(o);
523
+ }
524
+ return { ok: !0, value: null };
525
+ }
526
+ /** Re-read one loaded message after an edit or a delete; one newer than all loaded reads the rest too. */
527
+ reread(t) {
528
+ const s = this.bySeq.get(t);
529
+ if (!s) {
530
+ t > this.highest() && this.loadNewer();
531
+ return;
532
+ }
533
+ this.page({ afterSeq: t - 1, limit: 1, sinceRev: s.rev });
534
+ }
535
+ apply(t) {
536
+ if (!(this.ended || t.c !== void 0 && t.c.toLowerCase() !== this.conversationId)) {
537
+ if (!this.loaded && this.loadingCount > 0 && (t.k === "m" || t.k === "e" || t.k === "d" || t.k === "resync")) {
538
+ this.missed = !0;
539
+ return;
540
+ }
541
+ switch (t.k) {
542
+ case "m":
543
+ this.loaded && (t.s === void 0 || t.s > this.highest()) && this.loadNewer();
544
+ return;
545
+ case "e":
546
+ case "d":
547
+ this.loaded && t.s !== void 0 && this.reread(t.s);
548
+ return;
549
+ case "rd": {
550
+ if (t.s === void 0) return;
551
+ const s = fe();
552
+ if (s !== null && typeof t.r == "string" && t.r.toLowerCase() === s) {
553
+ T.set(this.conversationId, Math.max(T.get(this.conversationId) ?? 0, t.s));
554
+ return;
555
+ }
556
+ (this.readUpTo === null || t.s > this.readUpTo) && (this.readUpTo = t.s, this.changed());
557
+ return;
558
+ }
559
+ case "t":
560
+ this.typingUntil = q() + se, this.typingTimer !== null && clearTimeout(this.typingTimer), this.typingTimer = setTimeout(() => {
561
+ this.typingTimer = null, this.changed();
562
+ }, se), this.changed();
563
+ return;
564
+ case "resync":
565
+ this.loaded && this.refresh();
566
+ return;
567
+ default:
568
+ return;
569
+ }
570
+ }
571
+ }
572
+ send(t, s = {}) {
573
+ const { sending: r, queued: o } = Q({ conversationId: this.conversationId, text: t, ...s.replyToSeq !== void 0 ? { replyToSeq: s.replyToSeq } : {} });
574
+ return o && A.delete(this.conversationId), r;
575
+ }
576
+ markRead() {
577
+ const t = this.highest();
578
+ return t < 1 ? Promise.resolve({ ok: !0, value: { sent: !1, notificationsRead: 0 } }) : Z(this.conversationId, t);
579
+ }
580
+ close() {
581
+ this.ended || (this.ended = !0, this.typingTimer !== null && clearTimeout(this.typingTimer), this.typingTimer = null, this.bySeq.clear(), this.listeners.clear(), this.snapshot = null, m.get(this.conversationId) === this && m.delete(this.conversationId));
582
+ }
583
+ }
584
+ async function ee(e) {
585
+ if (!c(e?.conversationId)) return l();
586
+ const t = e.conversationId.toLowerCase(), s = { conversationId: t };
587
+ if (e.afterSeq !== void 0) {
588
+ if (typeof e.afterSeq != "number" || !Number.isSafeInteger(e.afterSeq) || e.afterSeq < 0) return l();
589
+ s.afterSeq = e.afterSeq;
590
+ }
591
+ if (e.beforeSeq !== void 0) {
592
+ if (!w(e.beforeSeq) || e.afterSeq !== void 0) return l();
593
+ s.beforeSeq = e.beforeSeq;
594
+ }
595
+ if (e.sinceRev !== void 0) {
596
+ if (typeof e.sinceRev != "number" || !Number.isSafeInteger(e.sinceRev) || e.sinceRev < 0) return l();
597
+ s.sinceRev = e.sinceRev;
598
+ }
599
+ const r = e.limit === void 0 ? O : e.limit;
600
+ if (!Number.isSafeInteger(r) || r < 1) return l();
601
+ s.limit = Math.min(r, O);
602
+ const o = await f("dm/messages", s);
603
+ if (!o.ok) return o;
604
+ const a = ne(o.value.messages, t);
605
+ if (a === null) return h();
606
+ const u = o.value.messages, y = typeof o.value.hasMore == "boolean" ? o.value.hasMore : u.length >= s.limit, b = o.value.counterpartReadSeq, L = b === null ? null : typeof b == "number" && Number.isSafeInteger(b) && b >= 0 ? b : void 0;
607
+ return { ok: !0, value: { messages: Object.freeze(a), hasMore: y, ...L !== void 0 ? { counterpartReadSeq: L } : {} } };
608
+ }
609
+ const E = (e) => c(e) ? { conversationId: e.toLowerCase() } : null, R = (e) => {
610
+ const t = E(e?.conversationId);
611
+ return t && w(e?.seq) ? { ...t, seq: e.seq } : null;
612
+ };
613
+ function ke(e) {
614
+ if (!c(e)) throw new TypeError("[blogmcp-sdk] dm.thread needs a conversation id");
615
+ const t = e.toLowerCase(), s = m.get(t);
616
+ if (s) return s;
617
+ const r = new Ie(t);
618
+ return k ? (r.close(), r) : (m.set(t, r), r);
619
+ }
620
+ const D = (e) => typeof e == "string" && e !== "" && e.length <= 64;
621
+ async function te(e, t) {
622
+ const s = await f(e, t);
623
+ return s.ok ? c(s.value.caseId) ? { ok: !0, value: { caseId: s.value.caseId.toLowerCase() } } : h() : s;
624
+ }
625
+ return {
626
+ async inbox(e) {
627
+ if (e !== void 0 && !D(e)) return l();
628
+ const t = await f("dm/list", e === void 0 ? {} : { cursor: e });
629
+ if (!t.ok) return t;
630
+ const s = P(t.value.items, Le);
631
+ return s === null ? h() : { ok: !0, value: { items: Object.freeze(s), nextCursor: D(t.value.nextCursor) ? t.value.nextCursor : null } };
632
+ },
633
+ async open(e) {
634
+ const t = ie(e);
635
+ if (t === null) return l();
636
+ const s = await f("dm/open", { handle: t });
637
+ if (!s.ok) return s;
638
+ const { conversationId: r, state: o } = s.value;
639
+ return !c(r) || !le(o) ? h() : { ok: !0, value: Object.freeze({ conversationId: r.toLowerCase(), state: o }) };
640
+ },
641
+ messages: ee,
642
+ send: pe,
643
+ async edit(e) {
644
+ const t = R(e), s = _(e?.text);
645
+ return !t || s === null || typeof e.rev != "number" || !Number.isSafeInteger(e.rev) || e.rev < 1 ? l() : H("dm/edit", { ...t, rev: e.rev, text: s }, t.conversationId);
646
+ },
647
+ async delete(e) {
648
+ const t = R(e);
649
+ return t ? H("dm/delete", t, t.conversationId) : l();
650
+ },
651
+ async hide(e) {
652
+ const t = R(e);
653
+ if (!t) return l();
654
+ const s = await M("dm/hide", t);
655
+ return s.ok && m.get(t.conversationId)?.drop([t.seq]), s;
656
+ },
657
+ async clear(e) {
658
+ const t = E(e);
659
+ if (!t) return l();
660
+ const s = await M("dm/clear", t);
661
+ return s.ok && m.get(t.conversationId)?.drop("all"), s;
662
+ },
663
+ read: Z,
664
+ typing: be,
665
+ requests: {
666
+ async list(e) {
667
+ if (e !== void 0 && !D(e)) return l();
668
+ const t = await f("requests/list", e === void 0 ? {} : { cursor: e });
669
+ if (!t.ok) return t;
670
+ const s = P(t.value.items, _e);
671
+ return s === null ? h() : { ok: !0, value: { items: Object.freeze(s), nextCursor: D(t.value.nextCursor) ? t.value.nextCursor : null } };
672
+ },
673
+ async accept(e) {
674
+ const t = E(e);
675
+ return t ? M("requests/accept", t) : l();
676
+ },
677
+ async delete(e) {
678
+ const t = E(e);
679
+ if (!t) return l();
680
+ const s = await M("requests/delete", t);
681
+ return s.ok && m.get(t.conversationId)?.close(), s;
682
+ },
683
+ async blockReport(e) {
684
+ const t = E(e?.conversationId);
685
+ if (!t || typeof e.category != "string" || e.category === "") return l();
686
+ const s = e.text === void 0 ? void 0 : _(e.text);
687
+ if (s === null) return l();
688
+ const r = await te("requests/block-report", { ...t, category: e.category, ...s !== void 0 ? { text: s } : {} });
689
+ return r.ok && m.get(t.conversationId)?.close(), r;
690
+ }
691
+ },
692
+ blocks: {
693
+ async add(e) {
694
+ return c(e) ? M("blocks/add", { memberId: e.toLowerCase() }) : l();
695
+ },
696
+ async remove(e) {
697
+ return c(e) ? M("blocks/remove", { memberId: e.toLowerCase() }) : l();
698
+ },
699
+ async list() {
700
+ const e = await f("blocks/list");
701
+ if (!e.ok) return e;
702
+ const t = P(e.value.blocks, Ne);
703
+ return t === null ? h() : { ok: !0, value: { blocks: Object.freeze(t) } };
704
+ }
705
+ },
706
+ audience: {
707
+ async check(e) {
708
+ const t = ie(e);
709
+ if (t === null) return l();
710
+ const s = await f("audience/check", { handle: t });
711
+ if (!s.ok) return s;
712
+ const r = s.value.canWrite;
713
+ return r !== "direct" && r !== "request" && r !== "no" ? h() : { ok: !0, value: { canWrite: r } };
714
+ }
715
+ },
716
+ reports: {
717
+ async preview(e) {
718
+ const t = R(e);
719
+ if (!t || e.line !== "rules" && e.line !== "illegal") return l();
720
+ const s = await f("reports/preview", { ...t, line: e.line });
721
+ if (!s.ok) return s;
722
+ const r = ne(s.value.messages, t.conversationId), o = s.value.recipients;
723
+ if (r === null || !S(o)) return h();
724
+ let a;
725
+ if (o.kind === "platform_ts") a = Object.freeze({ kind: "platform_ts" });
726
+ else if (o.kind === "authorised_persons" && Array.isArray(o.names) && o.names.every((u) => typeof u == "string"))
727
+ a = Object.freeze({ kind: "authorised_persons", names: Object.freeze([...o.names]) });
728
+ else return h();
729
+ return { ok: !0, value: { messages: Object.freeze(r), recipients: a } };
730
+ },
731
+ async submit(e) {
732
+ const t = R(e);
733
+ if (!t || e.line !== "rules" && e.line !== "illegal" || typeof e.category != "string" || e.category === "") return l();
734
+ const s = e.text === void 0 ? void 0 : _(e.text);
735
+ return s === null ? l() : te("reports/submit", {
736
+ ...t,
737
+ line: e.line,
738
+ category: e.category,
739
+ ...s !== void 0 ? { text: s } : {},
740
+ // Ticked by default (§7.5): only an explicit `false` leaves the sender unblocked.
741
+ block: e.block !== !1
742
+ });
743
+ }
744
+ },
745
+ cases: {
746
+ async list(e) {
747
+ if (e !== void 0 && (typeof e != "string" || e === "")) return l();
748
+ const t = await f("cases/list", e === void 0 ? {} : { cursor: e });
749
+ if (!t.ok) return t;
750
+ const s = P(t.value.items, oe);
751
+ return s === null ? h() : { ok: !0, value: { items: Object.freeze(s), nextCursor: I(t.value.nextCursor) ? t.value.nextCursor : null } };
752
+ },
753
+ async get(e) {
754
+ if (!c(e)) return l();
755
+ const t = await f("cases/get", { caseId: e.toLowerCase() });
756
+ if (!t.ok) return t;
757
+ const s = oe(t.value.case);
758
+ return s ? { ok: !0, value: { case: s } } : h();
759
+ }
760
+ },
761
+ statements: {
762
+ async get(e) {
763
+ if (!c(e)) return l();
764
+ const t = await f("statements/get", { decisionId: e.toLowerCase() });
765
+ if (!t.ok) return t;
766
+ const s = t.value, r = G(s.closesAt);
767
+ return !c(s.decisionId) || typeof s.statement != "string" || !I(s.decidedAt) || r === void 0 ? h() : { ok: !0, value: Object.freeze({ decisionId: s.decisionId.toLowerCase(), statement: s.statement, decidedAt: s.decidedAt, closesAt: r }) };
768
+ }
769
+ },
770
+ appeals: {
771
+ async submit(e) {
772
+ const t = _(e?.text);
773
+ if (!c(e?.decisionId) || t === null) return l();
774
+ const s = await f("appeals/submit", { decisionId: e.decisionId.toLowerCase(), text: t });
775
+ if (!s.ok) return s;
776
+ const { appealId: r, submittedAt: o } = s.value;
777
+ return !I(r) || !I(o) ? h() : { ok: !0, value: Object.freeze({ appealId: r, submittedAt: o }) };
778
+ }
779
+ },
780
+ export: {
781
+ async download() {
782
+ const e = await f("export/download");
783
+ return e.ok ? e.value.confirmSent === !0 ? { ok: !0, value: Object.freeze({ confirmSent: !0 }) } : e.value.confirmSent !== void 0 && e.value.confirmSent !== !1 ? h() : S(e.value.export) ? { ok: !0, value: Object.freeze({ export: e.value.export }) } : h() : e;
784
+ }
785
+ },
786
+ drafts: {
787
+ get: (e) => A.get(String(e).toLowerCase()) ?? "",
788
+ set(e, t) {
789
+ if (k || !c(e)) return;
790
+ const s = e.toLowerCase();
791
+ typeof t != "string" || t === "" ? A.delete(s) : A.set(s, t);
792
+ },
793
+ delete: (e) => {
794
+ A.delete(String(e).toLowerCase());
795
+ },
796
+ clear: () => A.clear()
797
+ },
798
+ outbox: {
799
+ pending(e) {
800
+ const t = e === void 0 ? null : String(e).toLowerCase();
801
+ return Object.freeze(g.filter((s) => !s.gone && (t === null || s.conversationId === t)).map(X));
802
+ },
803
+ retryNow() {
804
+ for (const e of [...g]) e.wake?.();
805
+ },
806
+ retry(e) {
807
+ const t = g.find((s) => s.clientMsgId === String(e).toLowerCase() && !s.gone);
808
+ return k || !t || t.state !== "failed" ? null : J($(t.conversationId, t.text, t.replyToSeq, t.clientMsgId, t));
809
+ },
810
+ discard(e) {
811
+ const t = g.find((s) => s.clientMsgId === String(e).toLowerCase() && !s.gone);
812
+ return t ? (x(t), t.settle(z()), !0) : !1;
813
+ }
814
+ },
815
+ thread: ke,
816
+ apply(e) {
817
+ const t = Ce(e);
818
+ if (k || t === "other") return t;
819
+ if (e.k === "resync")
820
+ for (const s of [...m.values()]) s.apply(e);
821
+ else e.c !== void 0 && m.get(e.c.toLowerCase())?.apply(e);
822
+ return t;
823
+ },
824
+ close() {
825
+ if (!k) {
826
+ k = !0, A.clear();
827
+ for (const e of [...g])
828
+ x(e), e.settle(z());
829
+ for (const e of [...m.values()]) e.close();
830
+ m.clear(), T.clear(), V.clear();
831
+ }
832
+ }
833
+ };
834
+ }
835
+ export {
836
+ Ue as PRIVATE_DM_EVENT_KINDS,
837
+ qe as PRIVATE_DM_MESSAGE_STATES,
838
+ ze as PRIVATE_DM_OPS,
839
+ Pe as PRIVATE_DM_OPS_AHEAD,
840
+ O as PRIVATE_DM_PAGE_MAX,
841
+ Ae as PRIVATE_DM_REREAD_PAGES,
842
+ W as PRIVATE_DM_RETRY,
843
+ Te as PRIVATE_DM_RETRYABLE,
844
+ Me as PRIVATE_DM_RETRY_BOUNDED,
845
+ Se as PRIVATE_DM_TYPING_INTERVAL_MS,
846
+ se as PRIVATE_DM_TYPING_SHOW_MS,
847
+ Ve as createPrivateDmClient,
848
+ Ce as dmEventEffect,
849
+ we as isBoundedDmFailure,
850
+ Oe as isRetryableDmFailure,
851
+ F as privateDmMessageOf
852
+ };