@pmate/chat-sdk 0.12.0-next.90 → 0.12.0-next.92

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.
@@ -115,10 +115,10 @@ export class InboxClient {
115
115
  initial = await this.request("check", { afterSeq: cursor.seq, epoch: cursor.epoch });
116
116
  if (!cursor || initial?.resetRequired) {
117
117
  const bootstrap = await this.request("bootstrap");
118
- cursor = await this.store.bootstrap(bootstrap);
118
+ cursor = await this.store.bootstrap(bootstrap, cursor ?? null);
119
119
  this.notify([]);
120
120
  initial = undefined;
121
- const target = inboxSequence(bootstrap.headSeq), start = inboxSequence(cursor.seq);
121
+ const start = inboxSequence(cursor.seq), target = inboxSequence(bootstrap.headSeq) > start ? inboxSequence(bootstrap.headSeq) : start;
122
122
  this.publish({ stage: "syncing", percent: 0, processed: "0", total: (target - start).toString() });
123
123
  do {
124
124
  const page = await this.request("events", { afterSeq: cursor.seq, throughSeq: target.toString(), epoch: cursor.epoch });
@@ -52,7 +52,7 @@ type InboxLease = {
52
52
  expiresAt: number;
53
53
  };
54
54
  export type InboxMessage = ChatReaderSocialMessage & {
55
- seq: number;
55
+ seq?: number;
56
56
  recalled?: boolean;
57
57
  };
58
58
  type MessageRow = {
@@ -117,7 +117,7 @@ export declare class IndexedDbInbox {
117
117
  claimLease(token: string, now?: number): import("dexie").PromiseExtended<boolean>;
118
118
  releaseLease(token: string): import("dexie").PromiseExtended<void>;
119
119
  private identity;
120
- bootstrap(input: InboxBootstrap): Promise<InboxCursor>;
120
+ bootstrap(input: InboxBootstrap, expected?: InboxCursor | null): Promise<InboxCursor>;
121
121
  apply(page: InboxPage): Promise<{
122
122
  cursor: {
123
123
  seq: string;
@@ -144,7 +144,7 @@ export declare class IndexedDbInbox {
144
144
  recipients?: string[];
145
145
  clientCreatedAt?: number;
146
146
  serverCreatedAt: number;
147
- seq: number;
147
+ seq?: number;
148
148
  }[]>;
149
149
  conversationMessages(thread: string, teamId: string): Promise<{
150
150
  recalled: boolean;
@@ -160,7 +160,7 @@ export declare class IndexedDbInbox {
160
160
  recipients?: string[];
161
161
  clientCreatedAt?: number;
162
162
  serverCreatedAt: number;
163
- seq: number;
163
+ seq?: number;
164
164
  }[]>;
165
165
  conversationMessage(thread: string, teamId: string, id: string): Promise<{
166
166
  sourceThreadId?: string;
@@ -176,7 +176,7 @@ export declare class IndexedDbInbox {
176
176
  recipients?: string[];
177
177
  clientCreatedAt?: number;
178
178
  serverCreatedAt: number;
179
- seq: number;
179
+ seq?: number;
180
180
  } | undefined>;
181
181
  conversationChanges(thread: string, teamId: string, afterSeq: string, afterId?: string): Promise<{
182
182
  changeSeq: string;
@@ -54,12 +54,20 @@ export class IndexedDbInbox {
54
54
  await this.leases.delete(this.owner); }); }
55
55
  identity(data) { if (data.appId !== this.scope.appId || data.profileId !== this.scope.profileId)
56
56
  throw new Error("INBOX_IDENTITY_MISMATCH"); }
57
- async bootstrap(input) {
57
+ async bootstrap(input, expected) {
58
58
  this.identity(input);
59
59
  const start = inboxSequence(input.startSeq), head = inboxSequence(input.headSeq);
60
60
  if (!input.epoch || start < 1n || start > head + 1n || !Number.isSafeInteger(input.windowStart) || !Array.isArray(input.basis))
61
61
  throw new Error("INVALID_INBOX_BOOTSTRAP");
62
62
  return this.db.transaction("rw", this.tables(), async () => {
63
+ const current = await this.cursor();
64
+ // A slower tab must never erase facts another tab committed while this
65
+ // bootstrap request was in flight. The caller catches up from that cursor.
66
+ if (expected !== undefined && JSON.stringify(current ?? null) !== JSON.stringify(expected)) {
67
+ if (current)
68
+ return current;
69
+ throw new Error("INBOX_BOOTSTRAP_CHANGED");
70
+ }
63
71
  // A reset rebuilds current state, while older message cache stays available.
64
72
  const old = await this.messages.where("owner").equals(this.owner).toArray();
65
73
  await this.messages.bulkDelete(old.filter(row => row.acceptedAt >= input.windowStart).map(row => [row.owner, row.thread, row.id]));
@@ -116,7 +124,7 @@ export class IndexedDbInbox {
116
124
  switch (event.kind) {
117
125
  case "messageAdded": {
118
126
  const message = data, thread = message.threadId;
119
- if (!thread || !message.messageId || !message.from || !Number.isSafeInteger(message.seq) || message.seq < 1 || !["dm", "group"].includes(message.threadKind))
127
+ if (!thread || !message.messageId || !message.from || typeof message.seq !== "number" || !Number.isSafeInteger(message.seq) || message.seq < 1 || !["dm", "group"].includes(message.threadKind))
120
128
  throw new Error("INVALID_INBOX_MESSAGE");
121
129
  if (message.from !== this.scope.profileId && !message.recipients?.includes(this.scope.profileId) && !(message.threadKind === "dm" && message.to === this.scope.profileId))
122
130
  throw new Error("INBOX_MESSAGE_INACCESSIBLE");
@@ -125,7 +133,6 @@ export class IndexedDbInbox {
125
133
  if (body?.kind === "group") {
126
134
  await this.facts.put({ owner, kind: "groupControl", id: message.messageId, thread, value: data });
127
135
  await this.facts.put({ owner, kind: "structure", id: thread, thread, value: { seq: event.inboxSeq } });
128
- return thread;
129
136
  }
130
137
  if (basis || event.acceptedAt < windowStart)
131
138
  return thread;
@@ -182,6 +189,18 @@ export class IndexedDbInbox {
182
189
  if (!data.pairId || ![data.actorId, data.targetId].includes(this.scope.profileId))
183
190
  throw new Error("INVALID_INBOX_RELATIONSHIP");
184
191
  await this.facts.put({ owner, kind: event.kind, id: data.pairId, thread: data.threadId, value: data });
192
+ // Older relationship logs contain the invitation's identity, without a
193
+ // sequence append. Reconstruct that display item without inventing a read
194
+ // ordinal or replacing a later authoritative message with the same ID.
195
+ if (data.action === "friendRequest" && typeof data.messageId === "string" && !basis && event.acceptedAt >= windowStart) {
196
+ if (!data.threadId || !Number.isSafeInteger(data.createdAt))
197
+ throw new Error("INVALID_INBOX_INVITATION");
198
+ await this.threads.put({ owner, id: data.threadId, kind: "dm", teamId: data.teamId ?? undefined, targetId: data.targetId });
199
+ if (!await this.messages.get([owner, data.threadId, data.messageId])) {
200
+ const value = { appId: this.scope.appId, teamId: data.teamId ?? undefined, threadId: data.threadId, threadKind: "dm", messageId: data.messageId, from: data.actorId, to: data.targetId, recipients: [data.targetId], serverCreatedAt: data.createdAt, body: { kind: "contact", threadKind: "dm", action: "friendRequest", payload: { appId: this.scope.appId, ...(data.teamId ? { teamId: data.teamId } : {}) } } };
201
+ await this.messages.put({ owner, thread: data.threadId, id: data.messageId, acceptedAt: event.acceptedAt, time: data.createdAt, seq: 0, changeSeq: event.inboxSeq.padStart(19, "0"), root: "", recalled: false, value });
202
+ }
203
+ }
185
204
  return data.threadId;
186
205
  }
187
206
  default: throw new Error(`UNSUPPORTED_INBOX_EVENT:${event.kind}`);
@@ -222,6 +241,8 @@ export class IndexedDbInbox {
222
241
  if (!thread)
223
242
  continue;
224
243
  const message = row.value, body = message.body;
244
+ if (body?.kind === "group" || row.seq < 1)
245
+ continue;
225
246
  const mentions = body?.content?.mentions ?? body?.content?.content?.mentions ?? [];
226
247
  const visible = body?.kind === undefined || ["message", "quote"].includes(body.kind) || body.kind === "contact" && ["friendRequest", "requestFriend"].includes(body.action);
227
248
  thread.messages.push({ id: row.id, seq: row.seq, from: message.from, recipients: message.recipients ?? [], threadRootId: row.root || undefined, mentions: [...new Set(Array.isArray(mentions) ? mentions.filter(item => item && typeof item.id === "string" && (this.scope.appId !== "pmate:merry" || !["all", "所有人"].includes(item.name))).map(item => item.id) : [])].sort(), readPolicy: messageRequiresPlayback(message.body) ? "explicit" : "visible", recalled: row.recalled || !visible || body?.kind === "recall" || Boolean(body?.content?.recall), payload: null, readingControl: isReadingControl(message.body, this.scope.appId) });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pmate/chat-sdk",
3
- "version": "0.12.0-next.90",
3
+ "version": "0.12.0-next.92",
4
4
  "description": "Local-first chat SDK, message subscriptions, reading state, and shared chat UI primitives",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",