@pmate/chat-sdk 0.14.0-seq.2 → 0.14.0-seq.3
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.
|
@@ -322,7 +322,7 @@ export class ChatClient {
|
|
|
322
322
|
}, ChatMsgKind.DM);
|
|
323
323
|
await this.sendAndWaitForAck(auth);
|
|
324
324
|
}
|
|
325
|
-
async sendAndWaitForAck(msg) {
|
|
325
|
+
async sendAndWaitForAck(msg, recoverAuth = true) {
|
|
326
326
|
const client = this.requireClient();
|
|
327
327
|
if (msg.opcode !== ChatMsgOp.AUTH && this.connection !== "connected") {
|
|
328
328
|
throw new Error(this.connection === "authentication_required" ? "登录已失效,请重新登录" : "聊天连接正在恢复,请稍后重试");
|
|
@@ -345,7 +345,22 @@ export class ChatClient {
|
|
|
345
345
|
pending.reject(error instanceof Error ? error : new Error("Send failed"));
|
|
346
346
|
}
|
|
347
347
|
}
|
|
348
|
-
|
|
348
|
+
try {
|
|
349
|
+
return await ack;
|
|
350
|
+
}
|
|
351
|
+
catch (error) {
|
|
352
|
+
// This ACK explicitly guarantees that Hub rejected the write before
|
|
353
|
+
// processing it. Re-authenticate, then retry the exact envelope once.
|
|
354
|
+
// Disconnects/timeouts have uncertain delivery and must not be replayed here.
|
|
355
|
+
if (!recoverAuth || msg.opcode === ChatMsgOp.AUTH || !(error instanceof ChatAckError)
|
|
356
|
+
|| error.code !== "CHAT_CONNECTION_AUTH_REQUIRED" || this.client !== client
|
|
357
|
+
|| this.connection === "closed" || this.connection === "authentication_required")
|
|
358
|
+
throw error;
|
|
359
|
+
if (this.connection === "connected")
|
|
360
|
+
client.close(false);
|
|
361
|
+
await this.waitUntilReady();
|
|
362
|
+
return this.sendAndWaitForAck(msg, false);
|
|
363
|
+
}
|
|
349
364
|
}
|
|
350
365
|
handleIncoming(message) {
|
|
351
366
|
if (message.opcode === ChatMsgOp.NOTIFICATION) {
|
|
@@ -369,7 +384,7 @@ export class ChatClient {
|
|
|
369
384
|
pending.resolve(ack);
|
|
370
385
|
}
|
|
371
386
|
else {
|
|
372
|
-
pending.reject(new ChatAckError(ack.message || ack.code || "Ack failed"));
|
|
387
|
+
pending.reject(new ChatAckError(ack.message || ack.code || "Ack failed", ack.code));
|
|
373
388
|
}
|
|
374
389
|
}
|
|
375
390
|
return;
|
|
@@ -469,6 +484,11 @@ export class ChatClient {
|
|
|
469
484
|
}
|
|
470
485
|
}
|
|
471
486
|
class ChatAckError extends Error {
|
|
487
|
+
code;
|
|
488
|
+
constructor(message, code) {
|
|
489
|
+
super(message);
|
|
490
|
+
this.code = code;
|
|
491
|
+
}
|
|
472
492
|
}
|
|
473
493
|
export function createChatClient(options) {
|
|
474
494
|
return new ChatClient(options);
|
|
@@ -22,14 +22,15 @@ export type MessageThreadSummary = {
|
|
|
22
22
|
lastReplyAt: number | null;
|
|
23
23
|
participantProfileIds: string[];
|
|
24
24
|
};
|
|
25
|
-
export type
|
|
25
|
+
export type TeamTask = {
|
|
26
26
|
id: string;
|
|
27
27
|
teamId: string;
|
|
28
28
|
discussionId: string;
|
|
29
29
|
threadId: string;
|
|
30
30
|
title: string;
|
|
31
31
|
description: string;
|
|
32
|
-
|
|
32
|
+
kind: "standard" | "flow";
|
|
33
|
+
status: "todo" | "inProgress" | "done";
|
|
33
34
|
assigneeId: string | null;
|
|
34
35
|
revision: number;
|
|
35
36
|
createdBy: string;
|
|
@@ -37,8 +38,8 @@ export type TeamIssue = {
|
|
|
37
38
|
updatedBy: string;
|
|
38
39
|
updatedAt: number;
|
|
39
40
|
};
|
|
40
|
-
export type
|
|
41
|
-
|
|
41
|
+
export type TeamTaskPage = {
|
|
42
|
+
tasks: TeamTask[];
|
|
42
43
|
nextAfterId: string | null;
|
|
43
44
|
};
|
|
44
45
|
export type SocialGroup = {
|
|
@@ -104,18 +105,18 @@ export declare class SocialReadClient {
|
|
|
104
105
|
revision: number;
|
|
105
106
|
items: MessageThreadSummary[];
|
|
106
107
|
}>;
|
|
107
|
-
|
|
108
|
+
teamTasks(input: {
|
|
108
109
|
teamId: string;
|
|
109
110
|
afterId?: string;
|
|
110
111
|
limit?: number;
|
|
111
|
-
}, signal?: AbortSignal): Promise<
|
|
112
|
-
|
|
112
|
+
}, signal?: AbortSignal): Promise<TeamTaskPage>;
|
|
113
|
+
teamTask(input: {
|
|
113
114
|
teamId: string;
|
|
114
115
|
id: string;
|
|
115
|
-
}, signal?: AbortSignal): Promise<
|
|
116
|
-
|
|
116
|
+
}, signal?: AbortSignal): Promise<TeamTask>;
|
|
117
|
+
taskMessagePage(input: {
|
|
117
118
|
teamId: string;
|
|
118
|
-
|
|
119
|
+
taskId: string;
|
|
119
120
|
limit?: number;
|
|
120
121
|
before?: string;
|
|
121
122
|
after?: string;
|
|
@@ -128,7 +129,7 @@ export declare class SocialReadClient {
|
|
|
128
129
|
peerId: string;
|
|
129
130
|
teamId?: string;
|
|
130
131
|
}): Promise<SocialRelationship | null>;
|
|
131
|
-
private
|
|
132
|
+
private requireMerryTaskTeam;
|
|
132
133
|
private teamQuery;
|
|
133
134
|
private requireTeam;
|
|
134
135
|
private list;
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { messageThreadRoot, validMessageThreadRoot } from "./messageThread.js";
|
|
2
2
|
import { groupThreadId, isValidChatTeamId } from "./thread.js";
|
|
3
|
-
const
|
|
4
|
-
function
|
|
3
|
+
const taskId = (value) => typeof value === "string" && /^(?:iss|tsk)_[a-f0-9]{64}$/.test(value);
|
|
4
|
+
function teamTask(value, teamId) {
|
|
5
5
|
if (!value || typeof value !== "object")
|
|
6
6
|
return false;
|
|
7
7
|
const row = value;
|
|
8
|
-
return row.teamId === teamId &&
|
|
8
|
+
return row.teamId === teamId && taskId(row.id) && row.discussionId === row.id &&
|
|
9
9
|
row.threadId === groupThreadId("pmate:merry", row.discussionId, teamId) &&
|
|
10
10
|
typeof row.title === "string" && typeof row.description === "string" &&
|
|
11
|
-
(row.
|
|
11
|
+
(row.kind === "standard" || row.kind === "flow") &&
|
|
12
|
+
(row.status === "todo" || row.status === "inProgress" || row.status === "done") &&
|
|
12
13
|
(row.assigneeId === null || typeof row.assigneeId === "string") &&
|
|
13
14
|
typeof row.revision === "number" && Number.isSafeInteger(row.revision) && row.revision > 0 &&
|
|
14
15
|
typeof row.createdBy === "string" && Number.isSafeInteger(row.createdAt) &&
|
|
@@ -84,53 +85,53 @@ export class SocialReadClient {
|
|
|
84
85
|
throw new SocialReadError(502, "INVALID_MESSAGE_THREAD_SUMMARY_RESPONSE");
|
|
85
86
|
return value;
|
|
86
87
|
}
|
|
87
|
-
async
|
|
88
|
-
this.
|
|
88
|
+
async teamTasks(input, signal) {
|
|
89
|
+
this.requireMerryTaskTeam(input.teamId);
|
|
89
90
|
const limit = input.limit ?? 50;
|
|
90
91
|
if (!Number.isSafeInteger(limit) || limit < 1 || limit > 100 ||
|
|
91
|
-
(input.afterId !== undefined && !
|
|
92
|
-
throw new SocialReadError(400, "
|
|
92
|
+
(input.afterId !== undefined && !taskId(input.afterId))) {
|
|
93
|
+
throw new SocialReadError(400, "INVALID_TASK_QUERY");
|
|
93
94
|
}
|
|
94
|
-
const page = await this.request("
|
|
95
|
+
const page = await this.request("v2/tasks", {
|
|
95
96
|
teamId: input.teamId,
|
|
96
97
|
limit: String(limit),
|
|
97
98
|
...(input.afterId === undefined ? {} : { afterId: input.afterId }),
|
|
98
99
|
}, signal);
|
|
99
|
-
if (!page || !Array.isArray(page.
|
|
100
|
-
page.
|
|
101
|
-
const prior = index === 0 ? input.afterId : page.
|
|
102
|
-
return !
|
|
103
|
-
}) || (page.nextAfterId !== null && (!
|
|
104
|
-
page.
|
|
105
|
-
throw new SocialReadError(502, "
|
|
100
|
+
if (!page || !Array.isArray(page.tasks) || page.tasks.length > limit ||
|
|
101
|
+
page.tasks.some((row, index) => {
|
|
102
|
+
const prior = index === 0 ? input.afterId : page.tasks[index - 1]?.id;
|
|
103
|
+
return !teamTask(row, input.teamId) || (prior !== undefined && row.id <= prior);
|
|
104
|
+
}) || (page.nextAfterId !== null && (!taskId(page.nextAfterId) ||
|
|
105
|
+
page.tasks.length !== limit || page.tasks.at(-1)?.id !== page.nextAfterId))) {
|
|
106
|
+
throw new SocialReadError(502, "INVALID_TASK_PAGE_RESPONSE");
|
|
106
107
|
}
|
|
107
108
|
return page;
|
|
108
109
|
}
|
|
109
|
-
async
|
|
110
|
-
this.
|
|
111
|
-
if (!
|
|
112
|
-
throw new SocialReadError(400, "
|
|
113
|
-
const data = await this.request("
|
|
114
|
-
if (!
|
|
115
|
-
throw new SocialReadError(502, "
|
|
110
|
+
async teamTask(input, signal) {
|
|
111
|
+
this.requireMerryTaskTeam(input.teamId);
|
|
112
|
+
if (!taskId(input.id))
|
|
113
|
+
throw new SocialReadError(400, "INVALID_TASK_ID");
|
|
114
|
+
const data = await this.request("v2/tasks", { teamId: input.teamId, id: input.id }, signal);
|
|
115
|
+
if (!teamTask(data?.task, input.teamId) || data.task.id !== input.id) {
|
|
116
|
+
throw new SocialReadError(502, "INVALID_TASK_RESPONSE");
|
|
116
117
|
}
|
|
117
|
-
return data.
|
|
118
|
+
return data.task;
|
|
118
119
|
}
|
|
119
|
-
async
|
|
120
|
-
const { teamId,
|
|
121
|
-
this.
|
|
122
|
-
if (!
|
|
123
|
-
throw new SocialReadError(400, "
|
|
120
|
+
async taskMessagePage(input, signal) {
|
|
121
|
+
const { teamId, taskId: id, before, after, limit = 50 } = input;
|
|
122
|
+
this.requireMerryTaskTeam(teamId);
|
|
123
|
+
if (!taskId(id))
|
|
124
|
+
throw new SocialReadError(400, "INVALID_TASK_ID");
|
|
124
125
|
if (!Number.isSafeInteger(limit) || limit < 1 || limit > 500 || (before !== undefined && after !== undefined)) {
|
|
125
126
|
throw new SocialReadError(400, "INVALID_MESSAGE_PAGE");
|
|
126
127
|
}
|
|
127
|
-
const page = await this.request("
|
|
128
|
-
|
|
128
|
+
const page = await this.request("v2/tasks/messages", {
|
|
129
|
+
taskId: id, teamId, limit: String(limit),
|
|
129
130
|
...(before === undefined ? {} : { before }), ...(after === undefined ? {} : { after }),
|
|
130
131
|
}, signal);
|
|
131
132
|
this.validateMessagePage(page, { teamId, threadId: groupThreadId("pmate:merry", id, teamId) }, limit);
|
|
132
133
|
if (page.messages.some(row => row.threadKind !== "group" || row.to !== id)) {
|
|
133
|
-
throw new SocialReadError(502, "
|
|
134
|
+
throw new SocialReadError(502, "INVALID_TASK_MESSAGE_PAGE_RESPONSE");
|
|
134
135
|
}
|
|
135
136
|
return page;
|
|
136
137
|
}
|
|
@@ -141,9 +142,9 @@ export class SocialReadClient {
|
|
|
141
142
|
this.requireTeam(input.teamId);
|
|
142
143
|
return this.request("relationship", { peerId: input.peerId, ...this.teamQuery(input.teamId) });
|
|
143
144
|
}
|
|
144
|
-
|
|
145
|
+
requireMerryTaskTeam(teamId) {
|
|
145
146
|
if (this.options.appId !== "pmate:merry" || !isValidChatTeamId(teamId)) {
|
|
146
|
-
throw new SocialReadError(400, "
|
|
147
|
+
throw new SocialReadError(400, "MERRY_TASK_TEAM_REQUIRED");
|
|
147
148
|
}
|
|
148
149
|
}
|
|
149
150
|
teamQuery(teamId) {
|
package/package.json
CHANGED