@pmate/chat-sdk 0.14.0-seq.1 → 0.14.0-seq.2

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.
@@ -109,7 +109,7 @@ export declare class ProfileChat implements Chat {
109
109
  private loadGroup;
110
110
  private refreshChangedGroups;
111
111
  refreshGroup(id: string, options?: ReadOptions): Promise<ChatGroup>;
112
- private waitGroup;
112
+ private confirmGroup;
113
113
  createGroup(input: {
114
114
  groupId?: string;
115
115
  title: string;
@@ -410,32 +410,59 @@ export class ProfileChat {
410
410
  await pending.catch(() => { });
411
411
  return this.getGroup(id, options);
412
412
  }
413
- async waitGroup(id, previous) {
414
- // Delivered commands have an accepted index cursor; the API is the authority.
415
- const group = await this.refreshGroup(id);
416
- if (previous !== undefined && group.revision === previous)
417
- throw new ChatError("unavailable", "Waiting for group confirmation");
418
- return group;
413
+ async confirmGroup(id, matches) {
414
+ for (const delay of [0, 150, 400]) {
415
+ if (delay)
416
+ await new Promise(resolve => setTimeout(resolve, delay));
417
+ this.assertAccessible(id);
418
+ try {
419
+ const group = await this.refreshGroup(id);
420
+ if (matches(group))
421
+ return group;
422
+ }
423
+ catch (error) {
424
+ if (!(error instanceof SocialReadError) || ![404, 503].includes(error.status))
425
+ throw error;
426
+ }
427
+ }
428
+ throw new ChatError("unavailable", "Waiting for group confirmation");
419
429
  }
420
430
  async createGroup(input) {
421
431
  return createUniqueGroup(input.groupId, async (groupId) => {
422
- const id = await this.resolveThreadId({ groupId });
423
- await this.sendControl(id, { groupCreate: { title: input.title, members: input.members } });
424
- return { ...await this.waitGroup(id), threadId: id };
432
+ const threadId = await this.resolveThreadId({ groupId });
433
+ const group = await this.sendControl(threadId, { groupCreate: { title: input.title, members: input.members } });
434
+ return { ...group, threadId };
425
435
  });
426
436
  }
427
- async sendControl(id, content) {
437
+ async sendControl(id, content, previous) {
428
438
  const parent = this.parent(id), identity = this.host.identity;
429
439
  if (!parent?.internal?.targetId)
430
440
  throw new ChatError("unavailable", "Conversation is not prepared");
431
441
  const input = outgoingContent({ ...identity, teamId: this.teamId }, { id: groupThreadId(identity.appId, parent.internal.targetId, this.teamId), kind: "group", targetId: parent.internal.targetId, teamId: this.teamId, memberIds: parent.group?.members }, content);
432
442
  const wire = createRuntimeMsg(identity.profileId, input.to, 1, input.body);
433
443
  wire.recipients = input.recipients;
434
- await this.sendPrepared(id, wire, { waitFor: "delivered" });
435
- await this.host.synchronize();
444
+ try {
445
+ await this.sendPrepared(id, wire, { waitFor: "delivered" });
446
+ }
447
+ catch (error) {
448
+ // An interrupted acknowledgement can leave the group change committed.
449
+ const group = await this.confirmGroup(id, current => current.revision === wire.hash).catch(() => undefined);
450
+ if (!group)
451
+ throw error;
452
+ await this.host.submit({ type: "commandResult", commandId: wire.hash, state: "confirmed" }, true).catch(() => { });
453
+ return group;
454
+ }
455
+ // Group metadata is authoritative independently of the Inbox catch-up.
456
+ return this.confirmGroup(id, group => group.revision !== previous);
457
+ }
458
+ async updateGroup(id, changes) {
459
+ const previous = await this.getGroup(id);
460
+ return this.sendControl(id, { groupUpdate: { ...changes, expectedRevision: changes.expectedRevision ?? previous.revision } }, previous.revision);
461
+ }
462
+ async dissolveGroup(id, options = {}) {
463
+ const previous = await this.getGroup(id);
464
+ return this.sendControl(id, { groupDissolve: { expectedRevision: options.expectedRevision ?? previous.revision } }, previous.revision);
436
465
  }
437
- async updateGroup(id, changes) { const previous = await this.getGroup(id); await this.sendControl(id, { groupUpdate: { ...changes, expectedRevision: changes.expectedRevision ?? previous.revision } }); return this.waitGroup(id, previous.revision); }
438
- async dissolveGroup(id, options = {}) { const previous = await this.getGroup(id); await this.sendControl(id, { groupDissolve: { expectedRevision: options.expectedRevision ?? previous.revision } }); return this.waitGroup(id, previous.revision); }
439
466
  synchronizeReading() { return this.host.synchronize(); }
440
467
  revokeAccess() { return this.host.submit({ type: "teamAccess", teamId: this.teamId, allowed: false }, true); }
441
468
  /** Call only after a fresh authoritative Team membership check succeeds. */
@@ -22,6 +22,25 @@ export type MessageThreadSummary = {
22
22
  lastReplyAt: number | null;
23
23
  participantProfileIds: string[];
24
24
  };
25
+ export type TeamIssue = {
26
+ id: string;
27
+ teamId: string;
28
+ discussionId: string;
29
+ threadId: string;
30
+ title: string;
31
+ description: string;
32
+ status: "open" | "closed";
33
+ assigneeId: string | null;
34
+ revision: number;
35
+ createdBy: string;
36
+ createdAt: number;
37
+ updatedBy: string;
38
+ updatedAt: number;
39
+ };
40
+ export type TeamIssuePage = {
41
+ issues: TeamIssue[];
42
+ nextAfterId: string | null;
43
+ };
25
44
  export type SocialGroup = {
26
45
  appId: string;
27
46
  groupId: string;
@@ -74,6 +93,7 @@ export declare class SocialReadClient {
74
93
  threadRootId?: string;
75
94
  mainOnly?: boolean;
76
95
  }, signal?: AbortSignal): Promise<SocialMessagePage>;
96
+ private validateMessagePage;
77
97
  messageThreads(input: {
78
98
  threadId: string;
79
99
  teamId?: string;
@@ -84,6 +104,22 @@ export declare class SocialReadClient {
84
104
  revision: number;
85
105
  items: MessageThreadSummary[];
86
106
  }>;
107
+ teamIssues(input: {
108
+ teamId: string;
109
+ afterId?: string;
110
+ limit?: number;
111
+ }, signal?: AbortSignal): Promise<TeamIssuePage>;
112
+ teamIssue(input: {
113
+ teamId: string;
114
+ id: string;
115
+ }, signal?: AbortSignal): Promise<TeamIssue>;
116
+ issueMessagePage(input: {
117
+ teamId: string;
118
+ issueId: string;
119
+ limit?: number;
120
+ before?: string;
121
+ after?: string;
122
+ }, signal?: AbortSignal): Promise<SocialMessagePage>;
87
123
  group(input: {
88
124
  groupId: string;
89
125
  teamId?: string;
@@ -92,6 +128,7 @@ export declare class SocialReadClient {
92
128
  peerId: string;
93
129
  teamId?: string;
94
130
  }): Promise<SocialRelationship | null>;
131
+ private requireMerryIssueTeam;
95
132
  private teamQuery;
96
133
  private requireTeam;
97
134
  private list;
@@ -1,4 +1,19 @@
1
1
  import { messageThreadRoot, validMessageThreadRoot } from "./messageThread.js";
2
+ import { groupThreadId, isValidChatTeamId } from "./thread.js";
3
+ const issueId = (value) => typeof value === "string" && /^iss_[a-f0-9]{64}$/.test(value);
4
+ function teamIssue(value, teamId) {
5
+ if (!value || typeof value !== "object")
6
+ return false;
7
+ const row = value;
8
+ return row.teamId === teamId && issueId(row.id) && row.discussionId === row.id &&
9
+ row.threadId === groupThreadId("pmate:merry", row.discussionId, teamId) &&
10
+ typeof row.title === "string" && typeof row.description === "string" &&
11
+ (row.status === "open" || row.status === "closed") &&
12
+ (row.assigneeId === null || typeof row.assigneeId === "string") &&
13
+ typeof row.revision === "number" && Number.isSafeInteger(row.revision) && row.revision > 0 &&
14
+ typeof row.createdBy === "string" && Number.isSafeInteger(row.createdAt) &&
15
+ typeof row.updatedBy === "string" && Number.isSafeInteger(row.updatedAt);
16
+ }
2
17
  export class SocialReadError extends Error {
3
18
  status;
4
19
  constructor(status, message) {
@@ -36,6 +51,16 @@ export class SocialReadClient {
36
51
  throw new SocialReadError(400, "INVALID_MESSAGE_LOOKUP");
37
52
  const value = await this.request("messages", { threadId: input.threadId, ...this.teamQuery(input.teamId), limit: String(limit),
38
53
  ...(input.threadRootId === undefined ? {} : { threadRootId: input.threadRootId }), ...(input.mainOnly === undefined ? {} : { mainOnly: String(input.mainOnly) }), ...(input.before === undefined ? {} : { before: input.before }), ...(input.after === undefined ? {} : { after: input.after }), ...(input.messageId === undefined ? {} : { messageId: input.messageId }) }, signal);
54
+ this.validateMessagePage(value, input, limit);
55
+ if (value.messages.some(row => {
56
+ const root = messageThreadRoot(row.body);
57
+ const content = row.body?.content;
58
+ return input.mainOnly && root !== undefined || input.threadRootId !== undefined && root !== input.threadRootId && !content?.reaction && !content?.recall;
59
+ }))
60
+ throw new SocialReadError(502, "INVALID_MESSAGE_THREAD_PAGE_RESPONSE");
61
+ return value;
62
+ }
63
+ validateMessagePage(value, input, limit) {
39
64
  const cursor = (token) => typeof token === "string" && /^mp1:\d+:[0-9a-f]{64}$/.test(token);
40
65
  if (!value || !Array.isArray(value.messages) || value.messages.length > limit || !cursor(value.checkpoint)
41
66
  || (value.nextBefore !== null && !cursor(value.nextBefore)) || typeof value.hasMoreChanges !== "boolean" || typeof value.resetRequired !== "boolean"
@@ -44,13 +69,6 @@ export class SocialReadClient {
44
69
  throw new SocialReadError(502, "INVALID_MESSAGE_PAGE_RESPONSE");
45
70
  if (input.messageId !== undefined && (value.messages.some(row => row.messageId !== input.messageId) || value.nextBefore !== null || value.hasMoreChanges || value.resetRequired))
46
71
  throw new SocialReadError(502, "INVALID_MESSAGE_LOOKUP_RESPONSE");
47
- if (value.messages.some(row => {
48
- const root = messageThreadRoot(row.body);
49
- const content = row.body?.content;
50
- return input.mainOnly && root !== undefined || input.threadRootId !== undefined && root !== input.threadRootId && !content?.reaction && !content?.recall;
51
- }))
52
- throw new SocialReadError(502, "INVALID_MESSAGE_THREAD_PAGE_RESPONSE");
53
- return value;
54
72
  }
55
73
  async messageThreads(input, signal) {
56
74
  this.requireTeam(input.teamId);
@@ -66,6 +84,56 @@ export class SocialReadClient {
66
84
  throw new SocialReadError(502, "INVALID_MESSAGE_THREAD_SUMMARY_RESPONSE");
67
85
  return value;
68
86
  }
87
+ async teamIssues(input, signal) {
88
+ this.requireMerryIssueTeam(input.teamId);
89
+ const limit = input.limit ?? 50;
90
+ if (!Number.isSafeInteger(limit) || limit < 1 || limit > 100 ||
91
+ (input.afterId !== undefined && !issueId(input.afterId))) {
92
+ throw new SocialReadError(400, "INVALID_ISSUE_QUERY");
93
+ }
94
+ const page = await this.request("issues", {
95
+ teamId: input.teamId,
96
+ limit: String(limit),
97
+ ...(input.afterId === undefined ? {} : { afterId: input.afterId }),
98
+ }, signal);
99
+ if (!page || !Array.isArray(page.issues) || page.issues.length > limit ||
100
+ page.issues.some((row, index) => {
101
+ const prior = index === 0 ? input.afterId : page.issues[index - 1]?.id;
102
+ return !teamIssue(row, input.teamId) || (prior !== undefined && row.id <= prior);
103
+ }) || (page.nextAfterId !== null && (!issueId(page.nextAfterId) ||
104
+ page.issues.length !== limit || page.issues.at(-1)?.id !== page.nextAfterId))) {
105
+ throw new SocialReadError(502, "INVALID_ISSUE_PAGE_RESPONSE");
106
+ }
107
+ return page;
108
+ }
109
+ async teamIssue(input, signal) {
110
+ this.requireMerryIssueTeam(input.teamId);
111
+ if (!issueId(input.id))
112
+ throw new SocialReadError(400, "INVALID_ISSUE_ID");
113
+ const data = await this.request("issues", { teamId: input.teamId, id: input.id }, signal);
114
+ if (!teamIssue(data?.issue, input.teamId) || data.issue.id !== input.id) {
115
+ throw new SocialReadError(502, "INVALID_ISSUE_RESPONSE");
116
+ }
117
+ return data.issue;
118
+ }
119
+ async issueMessagePage(input, signal) {
120
+ const { teamId, issueId: id, before, after, limit = 50 } = input;
121
+ this.requireMerryIssueTeam(teamId);
122
+ if (!issueId(id))
123
+ throw new SocialReadError(400, "INVALID_ISSUE_ID");
124
+ if (!Number.isSafeInteger(limit) || limit < 1 || limit > 500 || (before !== undefined && after !== undefined)) {
125
+ throw new SocialReadError(400, "INVALID_MESSAGE_PAGE");
126
+ }
127
+ const page = await this.request("issues/messages", {
128
+ issueId: id, teamId, limit: String(limit),
129
+ ...(before === undefined ? {} : { before }), ...(after === undefined ? {} : { after }),
130
+ }, signal);
131
+ this.validateMessagePage(page, { teamId, threadId: groupThreadId("pmate:merry", id, teamId) }, limit);
132
+ if (page.messages.some(row => row.threadKind !== "group" || row.to !== id)) {
133
+ throw new SocialReadError(502, "INVALID_ISSUE_MESSAGE_PAGE_RESPONSE");
134
+ }
135
+ return page;
136
+ }
69
137
  group(input, signal) {
70
138
  return this.request("group", { groupId: input.groupId, ...this.teamQuery(input.teamId) }, signal);
71
139
  }
@@ -73,6 +141,11 @@ export class SocialReadClient {
73
141
  this.requireTeam(input.teamId);
74
142
  return this.request("relationship", { peerId: input.peerId, ...this.teamQuery(input.teamId) });
75
143
  }
144
+ requireMerryIssueTeam(teamId) {
145
+ if (this.options.appId !== "pmate:merry" || !isValidChatTeamId(teamId)) {
146
+ throw new SocialReadError(400, "MERRY_ISSUE_TEAM_REQUIRED");
147
+ }
148
+ }
76
149
  teamQuery(teamId) {
77
150
  return teamId === undefined ? {} : { teamId };
78
151
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pmate/chat-sdk",
3
- "version": "0.14.0-seq.1",
3
+ "version": "0.14.0-seq.2",
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",