@pmate/chat-sdk 0.12.0-next.83 → 0.12.0-next.84

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/README.md CHANGED
@@ -338,3 +338,18 @@ A discussion cannot open another discussion. Thread-internal quoted replies keep
338
338
  the same root. Close each discussion when its view closes; closing the parent
339
339
  also closes its discussions. Agents working on a platform supporting Message
340
340
  Threads should put their replies in the incoming Message Thread.
341
+
342
+ ## App-wide group identity
343
+
344
+ ### Background
345
+
346
+ A group is addressed by `(appId, groupId)`. Team membership governs access but is
347
+ not part of the public destination identity. Callers can read group metadata
348
+ without supplying a Team; the Hub resolves the canonical Team and checks access.
349
+
350
+ Both group creation APIs generate a UUID when no ID is supplied. A definitive
351
+ `GROUP_ID_CONFLICT` selects another UUID, with at most eight attempts. Timeouts,
352
+ permission errors and unknown delivery outcomes retain the original identity;
353
+ they must never create a second group. Durable creation retries reuse the same
354
+ creation command. Server-side conditional appends enforce uniqueness across Hub
355
+ instances. Deploy that server contract before enabling Team-free group lookup.
@@ -1,3 +1,4 @@
1
+ import { createUniqueGroup } from "../runtime/groupIdentity.js";
1
2
  import { createMessagePresenter } from "../messageView.js";
2
3
  import { createChat as createSession } from "../runtime/createChat.js";
3
4
  import { asChatError } from "../runtime/chatError.js";
@@ -272,9 +273,11 @@ export class ChatEngine {
272
273
  getGroup(threadId, options = {}) { return this.threadOperation(threadId, async () => this.group(await this.advanced.getGroup(threadId, options))); }
273
274
  createGroup(input) {
274
275
  return this.operation(async () => {
275
- const groupId = input.groupId ?? crypto.randomUUID(), threadId = await this.getConversation({ groupId });
276
- const group = await this.advanced.createGroup({ id: threadId, kind: "group", targetId: groupId, teamId: this.options.teamId }, { title: input.title, members: input.members });
277
- return { ...this.group(group), threadId };
276
+ return createUniqueGroup(input.groupId, async (groupId) => {
277
+ const threadId = await this.getConversation({ groupId });
278
+ const group = await this.advanced.createGroup({ id: threadId, kind: "group", targetId: groupId, teamId: this.options.teamId }, { title: input.title, members: input.members });
279
+ return { ...this.group(group), threadId };
280
+ });
278
281
  });
279
282
  }
280
283
  updateGroup(threadId, changes) { return this.threadOperation(threadId, async () => this.group(await this.advanced.updateGroup(threadId, changes))); }
@@ -109,7 +109,7 @@ export declare class ChatClient {
109
109
  teamId?: string;
110
110
  }): Promise<ChatRuntimeMessage>;
111
111
  createGroup(input: {
112
- groupId: string;
112
+ groupId?: string;
113
113
  teamId?: string;
114
114
  members: string[];
115
115
  title?: string;
@@ -1,3 +1,4 @@
1
+ import { createUniqueGroup } from "./groupIdentity.js";
1
2
  import { withChatTeam } from "./teamScope.js";
2
3
  import { OnlineReachReceiver } from "./reach.js";
3
4
  import { OnlineNotificationReceiver } from "./notification.js";
@@ -232,18 +233,18 @@ export class ChatClient {
232
233
  });
233
234
  }
234
235
  async createGroup(input) {
235
- return this.sendChat(input.groupId, {
236
+ return createUniqueGroup(input.groupId, groupId => this.sendChat(groupId, {
236
237
  kind: "group",
237
238
  threadKind: "group",
238
239
  action: "groupCreate",
239
240
  payload: {
240
241
  appId: this.options.appId,
241
- groupId: input.groupId,
242
+ groupId,
242
243
  ...(input.teamId !== undefined ? { teamId: input.teamId } : {}),
243
244
  members: input.members,
244
245
  title: input.title,
245
246
  },
246
- }, input.members);
247
+ }, input.members));
247
248
  }
248
249
  async updateGroup(input) {
249
250
  return this.sendChat(input.groupId, {
@@ -1,3 +1,4 @@
1
+ import { groupIdCollision } from "./groupIdentity.js";
1
2
  import { Dexie } from "dexie";
2
3
  import { validMessageIdentifier } from "./outgoingContent.js";
3
4
  import { outboxDatabase, prepareOutboxSchema } from "./outboxSchema.js";
@@ -500,7 +501,7 @@ export class DurableChatSender {
500
501
  catch (error) {
501
502
  // A failed delivery can already be durable remotely, including the first
502
503
  // lost ACK. Proof recovery never sends a second wire in this attempt.
503
- if (statefulGroup(record.message) && this.options.reconcile && transmitting && !this.closed && !this.closing && !this.withdrawn) {
504
+ if (!groupIdCollision(error) && statefulGroup(record.message) && this.options.reconcile && transmitting && !this.closed && !this.closing && !this.withdrawn) {
504
505
  try {
505
506
  if (await this.reconcile(record)) {
506
507
  await this.confirm([hash]);
@@ -0,0 +1,4 @@
1
+ /** Only a definitive create rejection may choose a new UUID. Unknown delivery
2
+ * outcomes retain the original message/idempotency identity. */
3
+ export declare function groupIdCollision(error: unknown): boolean;
4
+ export declare function createUniqueGroup<T>(initialId: string | undefined, create: (groupId: string) => Promise<T>, uuid?: () => string): Promise<T>;
@@ -0,0 +1,26 @@
1
+ /** Only a definitive create rejection may choose a new UUID. Unknown delivery
2
+ * outcomes retain the original message/idempotency identity. */
3
+ export function groupIdCollision(error) {
4
+ let current = error;
5
+ const seen = new Set();
6
+ while (current instanceof Error && !seen.has(current)) {
7
+ seen.add(current);
8
+ if (current.message === "GROUP_ID_CONFLICT")
9
+ return true;
10
+ current = current.cause;
11
+ }
12
+ return false;
13
+ }
14
+ export async function createUniqueGroup(initialId, create, uuid = () => crypto.randomUUID()) {
15
+ let id = initialId ?? uuid();
16
+ for (let attempt = 0; attempt < 8; attempt++)
17
+ try {
18
+ return await create(id);
19
+ }
20
+ catch (error) {
21
+ if (!groupIdCollision(error) || attempt === 7)
22
+ throw error;
23
+ id = uuid();
24
+ }
25
+ throw new Error("GROUP_ID_GENERATION_EXHAUSTED");
26
+ }
@@ -67,7 +67,6 @@ export class SocialReadClient {
67
67
  return value;
68
68
  }
69
69
  group(input, signal) {
70
- this.requireTeam(input.teamId);
71
70
  return this.request("group", { groupId: input.groupId, ...this.teamQuery(input.teamId) }, signal);
72
71
  }
73
72
  relationship(input) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pmate/chat-sdk",
3
- "version": "0.12.0-next.83",
3
+ "version": "0.12.0-next.84",
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",