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

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 (40) hide show
  1. package/README.md +1 -3
  2. package/lib/chatContract.d.ts +2 -0
  3. package/lib/chatTypes.d.ts +3 -0
  4. package/lib/internal/chatEngine.d.ts +3 -7
  5. package/lib/messageView.js +1 -1
  6. package/lib/runtime/activity.d.ts +2 -1
  7. package/lib/runtime/activity.js +4 -1
  8. package/lib/runtime/botAuth.d.ts +1 -0
  9. package/lib/runtime/botAuth.js +1 -0
  10. package/lib/runtime/chatClient.d.ts +2 -0
  11. package/lib/runtime/chatClient.js +38 -7
  12. package/lib/runtime/chatSession.d.ts +2 -0
  13. package/lib/runtime/groupClient.d.ts +2 -0
  14. package/lib/runtime/groupClient.js +5 -1
  15. package/lib/runtime/inboxDeletion.d.ts +7 -0
  16. package/lib/runtime/inboxDeletion.js +21 -0
  17. package/lib/runtime/inboxGroup.js +6 -2
  18. package/lib/runtime/inboxStore.js +33 -3
  19. package/lib/runtime/messageControls.js +6 -3
  20. package/lib/runtime/messageStore.d.ts +1 -0
  21. package/lib/runtime/messageTimeline.d.ts +1 -0
  22. package/lib/runtime/messageTimeline.js +8 -5
  23. package/lib/runtime/outgoingContent.d.ts +2 -0
  24. package/lib/runtime/outgoingContent.js +6 -2
  25. package/lib/runtime/profileChat.d.ts +5 -1
  26. package/lib/runtime/profileChat.js +43 -16
  27. package/lib/runtime/profileCoordinator.d.ts +5 -0
  28. package/lib/runtime/profileCoordinator.js +51 -6
  29. package/lib/runtime/profileDatabase.d.ts +1 -0
  30. package/lib/runtime/profileDatabase.js +2 -0
  31. package/lib/runtime/profileLegacyInbox.js +1 -1
  32. package/lib/runtime/profileMemoryStore.d.ts +1 -0
  33. package/lib/runtime/profileMemoryStore.js +1 -1
  34. package/lib/runtime/profileOwner.js +2 -2
  35. package/lib/runtime/profileProjection.d.ts +2 -0
  36. package/lib/runtime/profileProjection.js +76 -9
  37. package/lib/runtime/profileStoreTypes.d.ts +2 -0
  38. package/lib/runtime/socialReadClient.d.ts +53 -0
  39. package/lib/runtime/socialReadClient.js +114 -11
  40. package/package.json +6 -7
@@ -1,4 +1,20 @@
1
1
  import { messageThreadRoot, validMessageThreadRoot } from "./messageThread.js";
2
+ import { groupThreadId, isValidChatTeamId } from "./thread.js";
3
+ const taskId = (value) => typeof value === "string" && /^(?:iss|tsk)_[a-f0-9]{64}$/.test(value);
4
+ function teamTask(value, teamId) {
5
+ if (!value || typeof value !== "object")
6
+ return false;
7
+ const row = value;
8
+ return row.teamId === teamId && taskId(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.kind === "standard" || row.kind === "flow") &&
12
+ (row.status === "todo" || row.status === "inProgress" || row.status === "done") &&
13
+ (row.assigneeId === null || typeof row.assigneeId === "string") &&
14
+ typeof row.revision === "number" && Number.isSafeInteger(row.revision) && row.revision > 0 &&
15
+ typeof row.createdBy === "string" && Number.isSafeInteger(row.createdAt) &&
16
+ typeof row.updatedBy === "string" && Number.isSafeInteger(row.updatedAt);
17
+ }
2
18
  export class SocialReadError extends Error {
3
19
  status;
4
20
  constructor(status, message) {
@@ -7,6 +23,7 @@ export class SocialReadError extends Error {
7
23
  this.name = "SocialReadError";
8
24
  }
9
25
  }
26
+ export const groupReadBatchSize = 32;
10
27
  /** Authenticated profile reads through Hub; no fallback to public indexer URLs.
11
28
  * Create a new client when identity changes, so in-flight requests retain their
12
29
  * original account scope. Team ownership is validated again by the server.
@@ -36,6 +53,16 @@ export class SocialReadClient {
36
53
  throw new SocialReadError(400, "INVALID_MESSAGE_LOOKUP");
37
54
  const value = await this.request("messages", { threadId: input.threadId, ...this.teamQuery(input.teamId), limit: String(limit),
38
55
  ...(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);
56
+ this.validateMessagePage(value, input, limit);
57
+ if (value.messages.some(row => {
58
+ const root = messageThreadRoot(row.body);
59
+ const content = row.body?.content;
60
+ return input.mainOnly && root !== undefined || input.threadRootId !== undefined && root !== input.threadRootId && !content?.reaction && !content?.recall;
61
+ }))
62
+ throw new SocialReadError(502, "INVALID_MESSAGE_THREAD_PAGE_RESPONSE");
63
+ return value;
64
+ }
65
+ validateMessagePage(value, input, limit) {
39
66
  const cursor = (token) => typeof token === "string" && /^mp1:\d+:[0-9a-f]{64}$/.test(token);
40
67
  if (!value || !Array.isArray(value.messages) || value.messages.length > limit || !cursor(value.checkpoint)
41
68
  || (value.nextBefore !== null && !cursor(value.nextBefore)) || typeof value.hasMoreChanges !== "boolean" || typeof value.resetRequired !== "boolean"
@@ -44,13 +71,6 @@ export class SocialReadClient {
44
71
  throw new SocialReadError(502, "INVALID_MESSAGE_PAGE_RESPONSE");
45
72
  if (input.messageId !== undefined && (value.messages.some(row => row.messageId !== input.messageId) || value.nextBefore !== null || value.hasMoreChanges || value.resetRequired))
46
73
  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
74
  }
55
75
  async messageThreads(input, signal) {
56
76
  this.requireTeam(input.teamId);
@@ -66,13 +86,91 @@ export class SocialReadClient {
66
86
  throw new SocialReadError(502, "INVALID_MESSAGE_THREAD_SUMMARY_RESPONSE");
67
87
  return value;
68
88
  }
89
+ async teamTasks(input, signal) {
90
+ this.requireMerryTaskTeam(input.teamId);
91
+ const limit = input.limit ?? 50;
92
+ if (!Number.isSafeInteger(limit) || limit < 1 || limit > 100 ||
93
+ (input.afterId !== undefined && !taskId(input.afterId))) {
94
+ throw new SocialReadError(400, "INVALID_TASK_QUERY");
95
+ }
96
+ const page = await this.request("v2/tasks", {
97
+ teamId: input.teamId,
98
+ limit: String(limit),
99
+ ...(input.afterId === undefined ? {} : { afterId: input.afterId }),
100
+ }, signal);
101
+ if (!page || !Array.isArray(page.tasks) || page.tasks.length > limit ||
102
+ page.tasks.some((row, index) => {
103
+ const prior = index === 0 ? input.afterId : page.tasks[index - 1]?.id;
104
+ return !teamTask(row, input.teamId) || (prior !== undefined && row.id <= prior);
105
+ }) || (page.nextAfterId !== null && (!taskId(page.nextAfterId) ||
106
+ page.tasks.length !== limit || page.tasks.at(-1)?.id !== page.nextAfterId))) {
107
+ throw new SocialReadError(502, "INVALID_TASK_PAGE_RESPONSE");
108
+ }
109
+ return page;
110
+ }
111
+ async teamTask(input, signal) {
112
+ this.requireMerryTaskTeam(input.teamId);
113
+ if (!taskId(input.id))
114
+ throw new SocialReadError(400, "INVALID_TASK_ID");
115
+ const data = await this.request("v2/tasks", { teamId: input.teamId, id: input.id }, signal);
116
+ if (!teamTask(data?.task, input.teamId) || data.task.id !== input.id) {
117
+ throw new SocialReadError(502, "INVALID_TASK_RESPONSE");
118
+ }
119
+ return data.task;
120
+ }
121
+ async taskMessagePage(input, signal) {
122
+ const { teamId, taskId: id, before, after, limit = 50 } = input;
123
+ this.requireMerryTaskTeam(teamId);
124
+ if (!taskId(id))
125
+ throw new SocialReadError(400, "INVALID_TASK_ID");
126
+ if (!Number.isSafeInteger(limit) || limit < 1 || limit > 500 || (before !== undefined && after !== undefined)) {
127
+ throw new SocialReadError(400, "INVALID_MESSAGE_PAGE");
128
+ }
129
+ const page = await this.request("v2/tasks/messages", {
130
+ taskId: id, teamId, limit: String(limit),
131
+ ...(before === undefined ? {} : { before }), ...(after === undefined ? {} : { after }),
132
+ }, signal);
133
+ this.validateMessagePage(page, { teamId, threadId: groupThreadId("pmate:merry", id, teamId) }, limit);
134
+ if (page.messages.some(row => row.threadKind !== "group" || row.to !== id)) {
135
+ throw new SocialReadError(502, "INVALID_TASK_MESSAGE_PAGE_RESPONSE");
136
+ }
137
+ return page;
138
+ }
69
139
  group(input, signal) {
70
140
  return this.request("group", { groupId: input.groupId, ...this.teamQuery(input.teamId) }, signal);
71
141
  }
142
+ async groupBatch(input, signal) {
143
+ const expected = new Set(input.groupIds);
144
+ if (!input.teamId || !expected.size || input.groupIds.length > groupReadBatchSize) {
145
+ throw new SocialReadError(400, "INVALID_GROUP_IDS");
146
+ }
147
+ const data = await this.request("groups/batch", {}, signal, { ...input, groupIds: [...expected] });
148
+ if (!Array.isArray(data) || data.length !== expected.size) {
149
+ throw new SocialReadError(502, "INVALID_GROUP_BATCH_RESPONSE");
150
+ }
151
+ return data.map(row => {
152
+ if (typeof row?.groupId !== "string" || !expected.delete(row.groupId)) {
153
+ throw new SocialReadError(502, "INVALID_GROUP_BATCH_RESPONSE");
154
+ }
155
+ if (row.status === 200 && row.data?.groupId === row.groupId &&
156
+ row.data.teamId === input.teamId && row.data.appId === this.options.appId) {
157
+ return { groupId: row.groupId, data: row.data };
158
+ }
159
+ if (Number.isSafeInteger(row.status) && row.status >= 400 && row.status <= 599 && typeof row.error?.message === "string") {
160
+ return { groupId: row.groupId, error: new SocialReadError(row.status, row.error.message) };
161
+ }
162
+ throw new SocialReadError(502, "INVALID_GROUP_BATCH_RESPONSE");
163
+ });
164
+ }
72
165
  relationship(input) {
73
166
  this.requireTeam(input.teamId);
74
167
  return this.request("relationship", { peerId: input.peerId, ...this.teamQuery(input.teamId) });
75
168
  }
169
+ requireMerryTaskTeam(teamId) {
170
+ if (this.options.appId !== "pmate:merry" || !isValidChatTeamId(teamId)) {
171
+ throw new SocialReadError(400, "MERRY_TASK_TEAM_REQUIRED");
172
+ }
173
+ }
76
174
  teamQuery(teamId) {
77
175
  return teamId === undefined ? {} : { teamId };
78
176
  }
@@ -86,10 +184,15 @@ export class SocialReadClient {
86
184
  throw new SocialReadError(502, "INVALID_SOCIAL_READ_RESPONSE");
87
185
  return value;
88
186
  }
89
- async request(action, query, signal) {
90
- const search = new URLSearchParams({ ...query, appId: this.options.appId, profileId: this.options.profileId });
91
- const result = await (this.options.fetch ?? fetch)(`${(this.options.hubHttpUrl ?? "https://hub.pmate.chat").replace(/\/+$/, "")}/api/social/${action}?${search}`, {
92
- headers: { authorization: `Bearer ${this.options.token}`, accept: "application/json" },
187
+ async request(action, query, signal, body) {
188
+ const scope = { appId: this.options.appId, profileId: this.options.profileId };
189
+ const suffix = body ? "" : `?${new URLSearchParams({ ...query, ...scope })}`;
190
+ const headers = { authorization: `Bearer ${this.options.token}`, accept: "application/json" };
191
+ if (body)
192
+ headers["content-type"] = "application/json";
193
+ const result = await (this.options.fetch ?? fetch)(`${(this.options.hubHttpUrl ?? "https://hub.pmate.chat").replace(/\/+$/, "")}/api/social/${action}${suffix}`, {
194
+ headers,
195
+ ...(body ? { method: "POST", body: JSON.stringify({ ...body, ...scope }) } : {}),
93
196
  cache: "no-store", redirect: "error", signal: signal ? AbortSignal.any([signal, AbortSignal.timeout(15_000)]) : AbortSignal.timeout(15_000),
94
197
  });
95
198
  const envelope = await result.json().catch(() => null);
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.10",
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",
@@ -81,8 +81,9 @@
81
81
  "react-dom": "*"
82
82
  },
83
83
  "devDependencies": {
84
- "@storybook/addon-docs": "^9.0.17",
85
- "@storybook/react-vite": "9.0.17",
84
+ "esbuild": "0.25.12",
85
+ "@tailwindcss/node": "4.1.18",
86
+ "@tailwindcss/oxide": "4.1.18",
86
87
  "@tailwindcss/vite": "^4.1.3",
87
88
  "@types/react": "^19.2.7",
88
89
  "@types/react-dom": "^19.2.3",
@@ -90,17 +91,15 @@
90
91
  "react": "^19.1.1",
91
92
  "react-dom": "^19.1.1",
92
93
  "rimraf": "^6.1.2",
93
- "storybook": "^9.0.17",
94
94
  "tailwindcss": "^4.0.14",
95
95
  "typescript": "5.9.2",
96
96
  "vitest": "^4.0.17"
97
97
  },
98
98
  "scripts": {
99
+ "preview:component": "merry-plugin browser open --plugin react-preview",
99
100
  "clean": "rimraf lib",
100
101
  "build": "pnpm run clean && tsc -p tsconfig.build.json && cp src/voice/recorder-processor.js lib/voice/recorder-processor.js",
101
102
  "npm:publish": "pnpm publish --access public --registry https://registry.npmjs.org/",
102
- "test": "vitest run",
103
- "storybook": "storybook dev -p 6009",
104
- "build-storybook": "storybook build --output-dir dist"
103
+ "test": "vitest run"
105
104
  }
106
105
  }