@xopcai/xopc 0.0.464 → 0.0.465
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/dist/browser-ext/manifest.json +1 -1
- package/dist/extensions/feishu/src/adapters/cli-login.js +23 -49
- package/dist/extensions/feishu/src/adapters/gateway-setup.d.ts +3 -0
- package/dist/extensions/feishu/src/adapters/gateway-setup.js +180 -128
- package/dist/extensions/feishu/src/adapters/onboard-cli.d.ts +1 -1
- package/dist/extensions/feishu/src/adapters/onboard-cli.js +27 -15
- package/dist/extensions/feishu/src/auth/app-registration.d.ts +1 -0
- package/dist/extensions/feishu/src/auth/app-registration.js +12 -9
- package/dist/extensions/feishu/src/outbound/outbound-adapter.js +40 -16
- package/dist/extensions/feishu/src/plugin.d.ts +2 -0
- package/dist/extensions/feishu/src/plugin.js +37 -23
- package/dist/extensions/feishu/src/status/status-adapter.d.ts +1 -1
- package/dist/extensions/feishu/src/status/status-adapter.js +8 -5
- package/dist/extensions/feishu/src/streaming/streaming-adapter.js +97 -69
- package/dist/extensions/feishu/src/transport/client/bot-info.d.ts +10 -0
- package/dist/extensions/feishu/src/transport/client/bot-info.js +34 -0
- package/dist/extensions/feishu/src/transport/client/client.d.ts +7 -1
- package/dist/extensions/feishu/src/transport/client/client.js +6 -5
- package/dist/extensions/feishu/src/transport/client/install-sdk.d.ts +2 -0
- package/dist/extensions/feishu/src/transport/client/install-sdk.js +77 -0
- package/dist/extensions/feishu/src/transport/client/lark-sdk.d.ts +2 -0
- package/dist/extensions/feishu/src/transport/client/lark-sdk.js +27 -3
- package/dist/extensions/feishu/src/transport/client/sdk-catalog.d.ts +1 -0
- package/dist/extensions/feishu/src/transport/client/sdk-catalog.js +317 -0
- package/dist/extensions/feishu/src/transport/reliability/inbound-pipeline.d.ts +1 -1
- package/dist/extensions/feishu/src/transport/reliability/inbound-pipeline.js +13 -5
- package/dist/extensions/feishu/src/transport/reliability/message-ingress.d.ts +21 -0
- package/dist/extensions/feishu/src/transport/reliability/message-ingress.js +313 -0
- package/dist/extensions/feishu/src/transport/socket-mode/monitor.d.ts +1 -0
- package/dist/extensions/feishu/src/transport/socket-mode/monitor.js +56 -171
- package/dist/extensions/feishu/src/transport/webhook/monitor.d.ts +1 -0
- package/dist/extensions/feishu/src/transport/webhook/monitor.js +10 -102
- package/dist/extensions/telegram/src/account-manager.d.ts +1 -0
- package/dist/extensions/telegram/src/account-manager.js +3 -0
- package/dist/extensions/telegram/src/adapters/debounce-keys.js +7 -2
- package/dist/extensions/telegram/src/adapters/gateway-setup.js +113 -63
- package/dist/extensions/telegram/src/adapters/streaming-adapter.js +8 -4
- package/dist/extensions/telegram/src/doctor.js +1 -1
- package/dist/extensions/telegram/src/inbound-coalescer.d.ts +8 -6
- package/dist/extensions/telegram/src/inbound-coalescer.js +47 -88
- package/dist/extensions/telegram/src/inbound-processor.js +5 -1
- package/dist/extensions/telegram/src/outbound-sender.js +16 -23
- package/dist/extensions/telegram/src/plugin.js +80 -57
- package/dist/extensions/telegram/src/polling-session.d.ts +3 -6
- package/dist/extensions/telegram/src/polling-session.js +107 -94
- package/dist/extensions/telegram/src/rich-message.d.ts +4 -0
- package/dist/extensions/telegram/src/rich-message.js +23 -0
- package/dist/extensions/telegram/src/timeout-abort.d.ts +1 -1
- package/dist/extensions/telegram/src/timeout-abort.js +9 -3
- package/dist/extensions/telegram/src/update-offset-store.d.ts +1 -0
- package/dist/extensions/telegram/src/update-offset-store.js +6 -1
- package/dist/extensions/telegram/xopc.extension.json +1 -1
- package/dist/extensions/weixin/src/api/api.d.ts +1 -0
- package/dist/extensions/weixin/src/api/api.js +12 -6
- package/dist/extensions/weixin/src/api/types.d.ts +5 -2
- package/dist/extensions/weixin/src/auth/login-qr.d.ts +7 -1
- package/dist/extensions/weixin/src/auth/login-qr.js +48 -13
- package/dist/extensions/weixin/src/cdn/pic-decrypt.d.ts +2 -2
- package/dist/extensions/weixin/src/cdn/pic-decrypt.js +37 -25
- package/dist/extensions/weixin/src/cli/gateway-qr-login.d.ts +8 -0
- package/dist/extensions/weixin/src/cli/gateway-qr-login.js +45 -6
- package/dist/extensions/weixin/src/cli/qr-login.d.ts +1 -1
- package/dist/extensions/weixin/src/cli/qr-login.js +9 -3
- package/dist/extensions/weixin/src/media/media-download.d.ts +1 -0
- package/dist/extensions/weixin/src/media/media-download.js +4 -5
- package/dist/extensions/weixin/src/messaging/process-message.d.ts +2 -0
- package/dist/extensions/weixin/src/messaging/process-message.js +90 -65
- package/dist/extensions/weixin/src/messaging/quote-cache.d.ts +10 -0
- package/dist/extensions/weixin/src/messaging/quote-cache.js +46 -0
- package/dist/extensions/weixin/src/monitor/monitor.d.ts +3 -3
- package/dist/extensions/weixin/src/monitor/monitor.js +108 -104
- package/dist/extensions/weixin/src/plugin.d.ts +5 -1
- package/dist/extensions/weixin/src/plugin.js +138 -42
- package/dist/gateway/static/root/assets/{capabilities-page-rTjkjo71.js → capabilities-page-U_hQx5ou.js} +2 -2
- package/dist/gateway/static/root/assets/channels-BUzno8Pi.js +1 -0
- package/dist/gateway/static/root/assets/extension-settings-page-BvUtZGLT.js +1 -0
- package/dist/gateway/static/root/assets/index-Cdo4_zon.js +11 -0
- package/dist/gateway/static/root/assets/index-DYTktubl.css +1 -0
- package/dist/gateway/static/root/assets/{route-preload-5iwjWERk.js → route-preload-ClsAFFQc.js} +2 -2
- package/dist/gateway/static/root/assets/schema-form-BxBDgLrg.js +1 -0
- package/dist/gateway/static/root/assets/{settings-page-DNiRlM5x.js → settings-page-M6XwzIyu.js} +1 -1
- package/dist/gateway/static/root/assets/understanding-refresh-store-CM6bUcaJ.js +1 -0
- package/dist/gateway/static/root/assets/user-model-page-CBcGMhrR.js +2 -0
- package/dist/gateway/static/root/index.html +4 -4
- package/dist/package.js +1 -1
- package/dist/src/agent/inbound/inbound-loop.js +14 -0
- package/dist/src/agent/messaging/outbound-coordinator.js +1 -1
- package/dist/src/channels/connection-probe.d.ts +23 -0
- package/dist/src/channels/connection-probe.js +98 -0
- package/dist/src/channels/manager.d.ts +2 -0
- package/dist/src/channels/manager.js +5 -0
- package/dist/src/channels/message-journal.d.ts +25 -0
- package/dist/src/channels/message-journal.js +79 -0
- package/dist/src/channels/outbound-sender.d.ts +8 -0
- package/dist/src/channels/outbound-sender.js +60 -1
- package/dist/src/channels/plugins/types.adapters.d.ts +4 -0
- package/dist/src/channels/verified-inbound.js +20 -14
- package/dist/src/gateway/hono/routes/channels.js +23 -10
- package/dist/src/gateway/hono/routes/home.js +1 -1
- package/dist/src/gateway/hono/routes/user-model.js +48 -20
- package/dist/src/gateway/service.js +5 -5
- package/dist/src/home-intelligence/host.js +5 -5
- package/dist/src/infra/bus/index.d.ts +1 -1
- package/dist/src/infra/bus/queue.d.ts +16 -3
- package/dist/src/infra/bus/queue.js +74 -3
- package/dist/src/infra/debounce.d.ts +1 -0
- package/dist/src/infra/debounce.js +40 -35
- package/dist/src/knowledge-memory/index.js +2 -2
- package/dist/src/knowledge-memory/repository.d.ts +9 -0
- package/dist/src/knowledge-memory/repository.js +12 -2
- package/dist/src/runtime-tools/types.d.ts +1 -1
- package/package.json +5 -6
- package/dist/gateway/static/root/assets/channels-DvB2fwPI.js +0 -1
- package/dist/gateway/static/root/assets/extension-settings-page-DKAJE4h9.js +0 -1
- package/dist/gateway/static/root/assets/index-BGA-G-Bp.css +0 -1
- package/dist/gateway/static/root/assets/index-pmlYYCCU.js +0 -11
- package/dist/gateway/static/root/assets/schema-form-C9TYGa0_.js +0 -1
- package/dist/gateway/static/root/assets/understanding-refresh-store-Cf0DUO_C.js +0 -1
- package/dist/gateway/static/root/assets/user-model-page-CdB9qH57.js +0 -2
|
@@ -1,4 +1,10 @@
|
|
|
1
|
+
import { setTimeout } from "node:timers/promises";
|
|
1
2
|
//#region extensions/feishu/src/auth/app-registration.ts
|
|
3
|
+
/**
|
|
4
|
+
* Feishu/Lark App Registration — QR scan-to-create and OAuth device flow.
|
|
5
|
+
*
|
|
6
|
+
* Shared between `cli-login.ts` (channels login) and `onboard-cli.ts` (onboard wizard).
|
|
7
|
+
*/
|
|
2
8
|
const FEISHU_ACCOUNTS_URL = "https://accounts.feishu.cn";
|
|
3
9
|
const LARK_ACCOUNTS_URL = "https://accounts.larksuite.com";
|
|
4
10
|
const REGISTRATION_PATH = "/oauth/v1/app/registration";
|
|
@@ -6,12 +12,12 @@ const REQUEST_TIMEOUT_MS = 1e4;
|
|
|
6
12
|
function accountsBaseUrl(domain) {
|
|
7
13
|
return domain === "lark" ? LARK_ACCOUNTS_URL : FEISHU_ACCOUNTS_URL;
|
|
8
14
|
}
|
|
9
|
-
async function postRegistration(baseUrl, body) {
|
|
15
|
+
async function postRegistration(baseUrl, body, signal) {
|
|
10
16
|
return await (await fetch(`${baseUrl}${REGISTRATION_PATH}`, {
|
|
11
17
|
method: "POST",
|
|
12
18
|
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
13
19
|
body: new URLSearchParams(body).toString(),
|
|
14
|
-
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS)
|
|
20
|
+
signal: signal ? AbortSignal.any([signal, AbortSignal.timeout(REQUEST_TIMEOUT_MS)]) : AbortSignal.timeout(REQUEST_TIMEOUT_MS)
|
|
15
21
|
})).json();
|
|
16
22
|
}
|
|
17
23
|
/**
|
|
@@ -60,14 +66,14 @@ async function pollAppRegistration(params) {
|
|
|
60
66
|
action: "poll",
|
|
61
67
|
device_code: params.deviceCode,
|
|
62
68
|
tp: "ob_cli_app"
|
|
63
|
-
});
|
|
69
|
+
}, params.signal);
|
|
64
70
|
} catch {
|
|
65
|
-
await
|
|
71
|
+
await setTimeout(intervalSec * 1e3, void 0, { signal: params.signal });
|
|
66
72
|
continue;
|
|
67
73
|
}
|
|
68
74
|
if (pollResponse.user_info?.tenant_brand) {
|
|
69
75
|
const isLark = pollResponse.user_info.tenant_brand === "lark";
|
|
70
|
-
if (!domainSwitched && isLark) {
|
|
76
|
+
if (!domainSwitched && isLark && domain !== "lark" && !pollResponse.client_secret) {
|
|
71
77
|
domain = "lark";
|
|
72
78
|
domainSwitched = true;
|
|
73
79
|
continue;
|
|
@@ -89,7 +95,7 @@ async function pollAppRegistration(params) {
|
|
|
89
95
|
status: "error",
|
|
90
96
|
message: `${pollResponse.error}: ${pollResponse.error_description ?? "unknown"}`
|
|
91
97
|
};
|
|
92
|
-
await
|
|
98
|
+
await setTimeout(intervalSec * 1e3, void 0, { signal: params.signal });
|
|
93
99
|
}
|
|
94
100
|
return { status: "timeout" };
|
|
95
101
|
}
|
|
@@ -113,8 +119,5 @@ async function printQrCode(url) {
|
|
|
113
119
|
console.log(url);
|
|
114
120
|
}
|
|
115
121
|
}
|
|
116
|
-
function sleep(ms) {
|
|
117
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
118
|
-
}
|
|
119
122
|
//#endregion
|
|
120
123
|
export { beginAppRegistration, initAppRegistration, pollAppRegistration, printQrCode };
|
|
@@ -2,6 +2,13 @@ import { resolveFeishuAccount } from "../state/accounts.js";
|
|
|
2
2
|
import { loadMediaForFeishu } from "./media-load.js";
|
|
3
3
|
import { getFeishuBindingByMessageId, recordFeishuMessageBinding } from "../state/message-bindings.js";
|
|
4
4
|
//#region extensions/feishu/src/outbound/outbound-adapter.ts
|
|
5
|
+
function messageIdFromResponse(response) {
|
|
6
|
+
return response?.code === 0 ? response.data?.message_id ?? response.message_id ?? "" : "";
|
|
7
|
+
}
|
|
8
|
+
function feishuRejectedRequest(error) {
|
|
9
|
+
const response = error?.response;
|
|
10
|
+
return response?.status === 400 || response?.status === 403;
|
|
11
|
+
}
|
|
5
12
|
function createFeishuOutboundAdapter() {
|
|
6
13
|
return {
|
|
7
14
|
deliveryMode: "direct",
|
|
@@ -50,10 +57,12 @@ function createFeishuOutboundAdapter() {
|
|
|
50
57
|
}] }
|
|
51
58
|
})
|
|
52
59
|
};
|
|
60
|
+
const sourceMessage = ctx.outboundMetadata?.inboundMessageId;
|
|
61
|
+
const replyToId = ctx.replyToId ?? (ctx.threadId && typeof sourceMessage === "string" ? sourceMessage : void 0);
|
|
53
62
|
const send = async (useCard) => {
|
|
54
63
|
const p = useCard ? payloadCard : payloadText;
|
|
55
|
-
return
|
|
56
|
-
path: { message_id:
|
|
64
|
+
return replyToId ? await api.im.message.reply({
|
|
65
|
+
path: { message_id: replyToId },
|
|
57
66
|
data: {
|
|
58
67
|
...p,
|
|
59
68
|
...ctx.threadId ? { reply_in_thread: true } : {}
|
|
@@ -66,10 +75,25 @@ function createFeishuOutboundAdapter() {
|
|
|
66
75
|
}
|
|
67
76
|
});
|
|
68
77
|
};
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
78
|
+
let response;
|
|
79
|
+
if (preferCard) {
|
|
80
|
+
try {
|
|
81
|
+
response = await send(true);
|
|
82
|
+
} catch (err) {
|
|
83
|
+
if (!feishuRejectedRequest(err)) throw err;
|
|
84
|
+
response = { code: -1 };
|
|
85
|
+
}
|
|
86
|
+
if (typeof response?.code === "number" && response.code !== 0 && response.code !== 99991400 && response.code !== 99991403) response = await send(false);
|
|
87
|
+
} else response = await send(false);
|
|
88
|
+
const messageId = messageIdFromResponse(response);
|
|
89
|
+
if (!messageId) return {
|
|
90
|
+
success: false,
|
|
91
|
+
messageId,
|
|
92
|
+
chatId: to,
|
|
93
|
+
error: "Feishu did not confirm message delivery"
|
|
94
|
+
};
|
|
95
|
+
if (messageId && replyToId) {
|
|
96
|
+
const parent = getFeishuBindingByMessageId(replyToId);
|
|
73
97
|
if (parent) recordFeishuMessageBinding({
|
|
74
98
|
...parent,
|
|
75
99
|
messageId
|
|
@@ -114,7 +138,7 @@ function createFeishuOutboundAdapter() {
|
|
|
114
138
|
chatId: ctx.to,
|
|
115
139
|
error: "Feishu image upload failed"
|
|
116
140
|
};
|
|
117
|
-
const
|
|
141
|
+
const messageId = messageIdFromResponse(ctx.replyToId ? await api.im.message.reply({
|
|
118
142
|
path: { message_id: ctx.replyToId },
|
|
119
143
|
data: {
|
|
120
144
|
msg_type: "image",
|
|
@@ -128,8 +152,7 @@ function createFeishuOutboundAdapter() {
|
|
|
128
152
|
msg_type: "image",
|
|
129
153
|
content: JSON.stringify({ image_key: imageKey })
|
|
130
154
|
}
|
|
131
|
-
});
|
|
132
|
-
const messageId = res?.data?.message_id ?? res?.message_id ?? "";
|
|
155
|
+
}));
|
|
133
156
|
if (messageId && ctx.replyToId) {
|
|
134
157
|
const parent = getFeishuBindingByMessageId(ctx.replyToId);
|
|
135
158
|
if (parent) recordFeishuMessageBinding({
|
|
@@ -138,9 +161,10 @@ function createFeishuOutboundAdapter() {
|
|
|
138
161
|
});
|
|
139
162
|
}
|
|
140
163
|
return {
|
|
141
|
-
success:
|
|
164
|
+
success: Boolean(messageId),
|
|
142
165
|
messageId,
|
|
143
|
-
chatId: ctx.to
|
|
166
|
+
chatId: ctx.to,
|
|
167
|
+
...messageId ? {} : { error: "Feishu did not confirm image delivery" }
|
|
144
168
|
};
|
|
145
169
|
}
|
|
146
170
|
const fileType = guessFileType(loaded.filename);
|
|
@@ -156,7 +180,7 @@ function createFeishuOutboundAdapter() {
|
|
|
156
180
|
chatId: ctx.to,
|
|
157
181
|
error: "Feishu file upload failed"
|
|
158
182
|
};
|
|
159
|
-
const
|
|
183
|
+
const messageId = messageIdFromResponse(ctx.replyToId ? await api.im.message.reply({
|
|
160
184
|
path: { message_id: ctx.replyToId },
|
|
161
185
|
data: {
|
|
162
186
|
msg_type: "file",
|
|
@@ -170,8 +194,7 @@ function createFeishuOutboundAdapter() {
|
|
|
170
194
|
msg_type: "file",
|
|
171
195
|
content: JSON.stringify({ file_key: fileKey })
|
|
172
196
|
}
|
|
173
|
-
});
|
|
174
|
-
const messageId = res?.data?.message_id ?? res?.message_id ?? "";
|
|
197
|
+
}));
|
|
175
198
|
if (messageId && ctx.replyToId) {
|
|
176
199
|
const parent = getFeishuBindingByMessageId(ctx.replyToId);
|
|
177
200
|
if (parent) recordFeishuMessageBinding({
|
|
@@ -180,9 +203,10 @@ function createFeishuOutboundAdapter() {
|
|
|
180
203
|
});
|
|
181
204
|
}
|
|
182
205
|
return {
|
|
183
|
-
success:
|
|
206
|
+
success: Boolean(messageId),
|
|
184
207
|
messageId,
|
|
185
|
-
chatId: ctx.to
|
|
208
|
+
chatId: ctx.to,
|
|
209
|
+
...messageId ? {} : { error: "Feishu did not confirm file delivery" }
|
|
186
210
|
};
|
|
187
211
|
}
|
|
188
212
|
};
|
|
@@ -41,6 +41,8 @@ export declare class FeishuChannelPlugin implements ChannelPlugin<ResolvedFeishu
|
|
|
41
41
|
private bus;
|
|
42
42
|
private cfg;
|
|
43
43
|
private abortControllers;
|
|
44
|
+
private monitorTasks;
|
|
45
|
+
private transportStates;
|
|
44
46
|
private inboundPipeline?;
|
|
45
47
|
config: {
|
|
46
48
|
listAccountIds: (cfg: Config) => string[];
|
|
@@ -13,6 +13,7 @@ import { createFeishuStreamingAdapter } from "./streaming/streaming-adapter.js";
|
|
|
13
13
|
import { readFrameworkAllowFromList } from "./auth/pairing.js";
|
|
14
14
|
import { feishuGatewaySetupActions } from "./adapters/gateway-setup.js";
|
|
15
15
|
import { handleFeishuChannelMessageAction } from "./actions/message-action-handler.js";
|
|
16
|
+
import { feishuInboundScope } from "./transport/reliability/message-ingress.js";
|
|
16
17
|
import { createFeishuInboundPipeline } from "./transport/reliability/inbound-pipeline.js";
|
|
17
18
|
import { isDeepStrictEqual } from "node:util";
|
|
18
19
|
//#region extensions/feishu/src/plugin.ts
|
|
@@ -69,10 +70,8 @@ var FeishuChannelPlugin = class {
|
|
|
69
70
|
pairing = createStandardPairingAdapter("feishu");
|
|
70
71
|
onboard = {
|
|
71
72
|
isConfigured: (config) => {
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
const appSecret = typeof feishu?.appSecret === "string" ? feishu.appSecret.trim() : "";
|
|
75
|
-
return feishu?.enabled === true && Boolean(appId && appSecret);
|
|
73
|
+
const account = resolveFeishuAccount(config);
|
|
74
|
+
return account.enabled && account.configured;
|
|
76
75
|
},
|
|
77
76
|
configure: async (config) => {
|
|
78
77
|
const { feishuOnboardAdapter } = await import("./adapters/onboard-cli.js");
|
|
@@ -87,6 +86,8 @@ var FeishuChannelPlugin = class {
|
|
|
87
86
|
bus;
|
|
88
87
|
cfg;
|
|
89
88
|
abortControllers = /* @__PURE__ */ new Map();
|
|
89
|
+
monitorTasks = /* @__PURE__ */ new Map();
|
|
90
|
+
transportStates = /* @__PURE__ */ new Map();
|
|
90
91
|
inboundPipeline;
|
|
91
92
|
config = {
|
|
92
93
|
listAccountIds: (cfg) => listFeishuAccountIds(cfg),
|
|
@@ -97,7 +98,7 @@ var FeishuChannelPlugin = class {
|
|
|
97
98
|
channelId: "feishu",
|
|
98
99
|
enabled: account.enabled,
|
|
99
100
|
configured: account.configured,
|
|
100
|
-
status: account.configured ?
|
|
101
|
+
status: account.configured ? this.transportStates.get(account.accountId) : "unconfigured"
|
|
101
102
|
})
|
|
102
103
|
};
|
|
103
104
|
security = {
|
|
@@ -137,7 +138,7 @@ var FeishuChannelPlugin = class {
|
|
|
137
138
|
};
|
|
138
139
|
outbound = createFeishuOutboundAdapter();
|
|
139
140
|
streaming = createFeishuStreamingAdapter(() => this.cfg);
|
|
140
|
-
status = createFeishuStatusAdapter();
|
|
141
|
+
status = createFeishuStatusAdapter((accountId) => this.transportStates.get(accountId));
|
|
141
142
|
doctor = createFeishuDoctorAdapter();
|
|
142
143
|
directory = { resolveDisplayName: async (params) => {
|
|
143
144
|
const { createFeishuDirectoryAdapter } = await import("./directory/directory-adapter.js");
|
|
@@ -297,7 +298,7 @@ var FeishuChannelPlugin = class {
|
|
|
297
298
|
const defaultDebounce = this.defaults.queue?.debounceMs ?? 350;
|
|
298
299
|
const enqueueInbound = (work) => {
|
|
299
300
|
const p = this.inboundPipeline;
|
|
300
|
-
if (!p) return this.bus.publishInbound(work.inbound);
|
|
301
|
+
if (!p) return this.bus.publishInbound(work.inbound, work.delivery);
|
|
301
302
|
return p.enqueue(work);
|
|
302
303
|
};
|
|
303
304
|
const monitorDeps = {
|
|
@@ -306,22 +307,35 @@ var FeishuChannelPlugin = class {
|
|
|
306
307
|
enqueueInbound,
|
|
307
308
|
inboundDebounceDefaultMs: defaultDebounce,
|
|
308
309
|
abortSignal: ac.signal,
|
|
310
|
+
onState: (state) => {
|
|
311
|
+
if (!ac.signal.aborted) this.transportStates.set(accountId, state);
|
|
312
|
+
},
|
|
309
313
|
security: { checkAccess: (ctx) => this.security.checkAccess?.(ctx, account, this.cfg) }
|
|
310
314
|
};
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
return;
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
315
|
+
try {
|
|
316
|
+
this.bus.restoreDurableInbound?.(this.id, feishuInboundScope(account));
|
|
317
|
+
const task = (account.connectionMode === "webhook" ? (await import("./transport/webhook/monitor.js")).createFeishuWebhookMonitor(monitorDeps) : (await import("./transport/socket-mode/monitor.js")).createFeishuSocketModeMonitor(monitorDeps)).run().catch((err) => {
|
|
318
|
+
if (ac.signal.aborted) return;
|
|
319
|
+
this.transportStates.set(accountId, "error");
|
|
320
|
+
log.error({
|
|
321
|
+
err,
|
|
322
|
+
accountId
|
|
323
|
+
}, "Feishu monitor exited with error");
|
|
324
|
+
}).finally(() => {
|
|
325
|
+
if (this.abortControllers.get(accountId) !== ac) return;
|
|
326
|
+
this.abortControllers.delete(accountId);
|
|
327
|
+
this.monitorTasks.delete(accountId);
|
|
328
|
+
if (ac.signal.aborted) this.transportStates.delete(accountId);
|
|
329
|
+
});
|
|
330
|
+
this.monitorTasks.set(accountId, task);
|
|
331
|
+
log.info({
|
|
332
|
+
accountId,
|
|
333
|
+
mode: account.connectionMode
|
|
334
|
+
}, "Feishu monitor started");
|
|
335
|
+
} catch (err) {
|
|
336
|
+
this.abortControllers.delete(accountId);
|
|
337
|
+
throw err;
|
|
338
|
+
}
|
|
325
339
|
}
|
|
326
340
|
}
|
|
327
341
|
async stop(accountId) {
|
|
@@ -335,7 +349,7 @@ var FeishuChannelPlugin = class {
|
|
|
335
349
|
const ac = this.abortControllers.get(id);
|
|
336
350
|
if (ac) {
|
|
337
351
|
ac.abort();
|
|
338
|
-
this.
|
|
352
|
+
await this.monitorTasks.get(id);
|
|
339
353
|
}
|
|
340
354
|
}
|
|
341
355
|
if (!accountId) {}
|
|
@@ -343,7 +357,7 @@ var FeishuChannelPlugin = class {
|
|
|
343
357
|
channelIsRunning(cfg) {
|
|
344
358
|
return listFeishuAccountIds(cfg).some((id) => {
|
|
345
359
|
const a = resolveFeishuAccount(cfg, id);
|
|
346
|
-
return a.enabled !== false && a.configured && this.
|
|
360
|
+
return a.enabled !== false && a.configured && this.transportStates.get(id) === "online";
|
|
347
361
|
});
|
|
348
362
|
}
|
|
349
363
|
async onConfigUpdated(cfg) {
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { ChannelStatusAdapter } from '@xopcai/xopc/channels/plugin-types.js';
|
|
2
2
|
import type { ResolvedFeishuAccount } from '../state/accounts.js';
|
|
3
|
-
export declare function createFeishuStatusAdapter(): ChannelStatusAdapter<ResolvedFeishuAccount>;
|
|
3
|
+
export declare function createFeishuStatusAdapter(getState?: (accountId: string) => 'online' | 'connecting' | 'error' | undefined): ChannelStatusAdapter<ResolvedFeishuAccount>;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { probeFeishuBot } from "../transport/client/bot-info.js";
|
|
1
2
|
//#region extensions/feishu/src/status/status-adapter.ts
|
|
2
|
-
function createFeishuStatusAdapter() {
|
|
3
|
+
function createFeishuStatusAdapter(getState = () => void 0) {
|
|
3
4
|
return {
|
|
4
5
|
defaultRuntime: {
|
|
5
6
|
accountId: "default",
|
|
@@ -12,16 +13,18 @@ function createFeishuStatusAdapter() {
|
|
|
12
13
|
channelId: "feishu",
|
|
13
14
|
enabled: account.enabled,
|
|
14
15
|
configured: account.configured,
|
|
15
|
-
status: account.configured ? "
|
|
16
|
+
status: account.configured ? getState(account.accountId) ?? "offline" : "unconfigured"
|
|
16
17
|
}),
|
|
17
|
-
resolveAccountState: ({ configured, enabled }) => {
|
|
18
|
+
resolveAccountState: ({ account, configured, enabled }) => {
|
|
18
19
|
if (!enabled) return "disabled";
|
|
19
20
|
if (!configured) return "error";
|
|
20
|
-
|
|
21
|
+
const state = getState(account.accountId);
|
|
22
|
+
return state === "online" || state === "error" ? state : "offline";
|
|
21
23
|
},
|
|
22
24
|
probeAccount: async ({ account, timeoutMs, cfg }) => {
|
|
23
25
|
return {
|
|
24
|
-
|
|
26
|
+
...await probeFeishuBot(account, AbortSignal.timeout(Math.max(1e3, Math.min(timeoutMs ?? 1e4, 3e4)))).catch(() => ({ ok: false })),
|
|
27
|
+
state: getState(account.accountId),
|
|
25
28
|
timeoutMs,
|
|
26
29
|
domain: account.domain,
|
|
27
30
|
mode: account.connectionMode,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createLogger } from "../../../../src/utils/logger/index.js";
|
|
2
2
|
import { init_logger } from "../../../../src/utils/logger.js";
|
|
3
|
+
import { createRetryRunner } from "../../../../src/infra/retry.js";
|
|
3
4
|
import { resolveFeishuAccount } from "../state/accounts.js";
|
|
4
5
|
import { getFeishuBindingByMessageId, recordFeishuMessageBinding } from "../state/message-bindings.js";
|
|
5
6
|
import { randomUUID } from "node:crypto";
|
|
@@ -22,13 +23,29 @@ function extractFeishuCardKitCreateCardId(raw) {
|
|
|
22
23
|
};
|
|
23
24
|
return fromData(o.data) ?? (o.data && typeof o.data === "object" ? fromData(o.data.data) : void 0) ?? tryId(o.card_id);
|
|
24
25
|
}
|
|
25
|
-
function feishuOpenApiOk(
|
|
26
|
-
|
|
27
|
-
if (typeof res !== "object") return true;
|
|
28
|
-
const code = res.code;
|
|
29
|
-
if (code === void 0 || code === null) return true;
|
|
30
|
-
return Number(code) === 0;
|
|
26
|
+
function feishuOpenApiOk(response) {
|
|
27
|
+
return Boolean(response && typeof response === "object" && Number(response.code) === 0);
|
|
31
28
|
}
|
|
29
|
+
var FeishuUpdateError = class extends Error {
|
|
30
|
+
constructor(code) {
|
|
31
|
+
super("Feishu rejected the stream update");
|
|
32
|
+
this.code = code;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
function checkUpdate(response) {
|
|
36
|
+
if (!feishuOpenApiOk(response)) throw new FeishuUpdateError(Number(response?.code));
|
|
37
|
+
}
|
|
38
|
+
function isRateLimited(error) {
|
|
39
|
+
const response = error?.response;
|
|
40
|
+
const code = error instanceof FeishuUpdateError ? error.code : response?.data?.code;
|
|
41
|
+
return code === 99991400 || code === 230020 || response?.status === 429 && code !== 99991403;
|
|
42
|
+
}
|
|
43
|
+
const retryEdit = createRetryRunner({
|
|
44
|
+
attempts: 3,
|
|
45
|
+
minDelayMs: 500,
|
|
46
|
+
maxDelayMs: 2500,
|
|
47
|
+
shouldRetry: isRateLimited
|
|
48
|
+
});
|
|
32
49
|
function createFeishuStreamingAdapter(getConfig) {
|
|
33
50
|
return { startStream(options) {
|
|
34
51
|
const account = resolveFeishuAccount(getConfig(), options.accountId ?? "default");
|
|
@@ -43,12 +60,15 @@ function createFeishuStreamingAdapter(getConfig) {
|
|
|
43
60
|
let lastText = "";
|
|
44
61
|
let timer = null;
|
|
45
62
|
let aborted = false;
|
|
46
|
-
let
|
|
63
|
+
let ending = false;
|
|
64
|
+
let previewPending = false;
|
|
65
|
+
let deliveredText;
|
|
66
|
+
let finalDelivered = false;
|
|
67
|
+
let editQueue = Promise.resolve();
|
|
47
68
|
let ready = null;
|
|
48
69
|
let cardId;
|
|
49
70
|
let cardSeq = 0;
|
|
50
71
|
const cardElementId = `md_${randomUUID().replace(/-/g, "")}`;
|
|
51
|
-
let fallbackSent = false;
|
|
52
72
|
const renderMode = account.renderMode ?? "auto";
|
|
53
73
|
const preferCard = renderMode === "card" || renderMode === "auto";
|
|
54
74
|
const formatStreamText = async (text) => {
|
|
@@ -79,7 +99,7 @@ function createFeishuStreamingAdapter(getConfig) {
|
|
|
79
99
|
const outbound = await formatStreamText(text);
|
|
80
100
|
if (cardId && preferCard) {
|
|
81
101
|
cardSeq += 1;
|
|
82
|
-
await (await getApi()).cardkit.v1.cardElement.content({
|
|
102
|
+
checkUpdate(await (await getApi()).cardkit.v1.cardElement.content({
|
|
83
103
|
path: {
|
|
84
104
|
card_id: cardId,
|
|
85
105
|
element_id: cardElementId
|
|
@@ -89,74 +109,68 @@ function createFeishuStreamingAdapter(getConfig) {
|
|
|
89
109
|
sequence: cardSeq,
|
|
90
110
|
uuid: `${cardId}:${cardSeq}`
|
|
91
111
|
}
|
|
92
|
-
});
|
|
93
|
-
} else await (await getApi()).im.v1.message.update({
|
|
112
|
+
}));
|
|
113
|
+
} else checkUpdate(await (await getApi()).im.v1.message.update({
|
|
94
114
|
path: { message_id: messageId },
|
|
95
115
|
data: {
|
|
96
116
|
msg_type: "text",
|
|
97
117
|
content: JSON.stringify({ text: outbound })
|
|
98
118
|
}
|
|
99
|
-
});
|
|
100
|
-
|
|
119
|
+
}));
|
|
120
|
+
deliveredText = text;
|
|
101
121
|
};
|
|
102
|
-
const
|
|
103
|
-
if (
|
|
104
|
-
|
|
105
|
-
|
|
122
|
+
const closeCard = async () => {
|
|
123
|
+
if (!cardId) return;
|
|
124
|
+
const id = cardId;
|
|
125
|
+
cardId = void 0;
|
|
106
126
|
try {
|
|
107
|
-
const
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
msg_type: "text",
|
|
120
|
-
content: JSON.stringify({ text: outbound })
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
const mid = res?.data?.message_id ?? res?.message_id ?? void 0;
|
|
124
|
-
if (mid) recordBindingIfReply(mid);
|
|
125
|
-
editedAtLeastOnce = true;
|
|
126
|
-
messageId = mid ?? messageId;
|
|
127
|
-
cardId = void 0;
|
|
128
|
-
} catch (err) {
|
|
129
|
-
log.warn({
|
|
130
|
-
err,
|
|
131
|
-
accountId: account.accountId
|
|
132
|
-
}, "Feishu streaming fallback send failed");
|
|
133
|
-
}
|
|
134
|
-
};
|
|
135
|
-
const flush = async () => {
|
|
136
|
-
if (aborted) return;
|
|
137
|
-
if (ready) await ready;
|
|
138
|
-
if (!messageId) return;
|
|
139
|
-
const text = lastText;
|
|
140
|
-
if (!text.trim()) return;
|
|
141
|
-
try {
|
|
142
|
-
await edit(text);
|
|
127
|
+
const api = await getApi();
|
|
128
|
+
const sequence = ++cardSeq;
|
|
129
|
+
await retryEdit(async () => {
|
|
130
|
+
checkUpdate(await api.cardkit.v1.card.settings({
|
|
131
|
+
path: { card_id: id },
|
|
132
|
+
data: {
|
|
133
|
+
settings: JSON.stringify({ config: { streaming_mode: false } }),
|
|
134
|
+
sequence,
|
|
135
|
+
uuid: `${id}:close:${sequence}`
|
|
136
|
+
}
|
|
137
|
+
}));
|
|
138
|
+
}, "closeFeishuStream");
|
|
143
139
|
} catch (err) {
|
|
144
140
|
log.warn({
|
|
145
|
-
err,
|
|
146
141
|
accountId: account.accountId,
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}, "Feishu streaming edit failed");
|
|
150
|
-
await sendFallbackText(text);
|
|
142
|
+
errorType: err instanceof Error ? err.name : typeof err
|
|
143
|
+
}, "Feishu streaming card could not be closed");
|
|
151
144
|
}
|
|
152
145
|
};
|
|
146
|
+
const flush = (final = false) => {
|
|
147
|
+
editQueue = editQueue.then(async () => {
|
|
148
|
+
if (ready) await ready;
|
|
149
|
+
if (aborted || !lastText.trim()) return;
|
|
150
|
+
const text = lastText;
|
|
151
|
+
if (text !== deliveredText) try {
|
|
152
|
+
if (messageId) await retryEdit(() => edit(text), "updateFeishuStream");
|
|
153
|
+
} catch (err) {
|
|
154
|
+
log.warn({
|
|
155
|
+
err,
|
|
156
|
+
accountId: account.accountId,
|
|
157
|
+
messageId
|
|
158
|
+
}, "Feishu streaming edit failed");
|
|
159
|
+
}
|
|
160
|
+
if (final && !aborted) finalDelivered = text === deliveredText;
|
|
161
|
+
});
|
|
162
|
+
return editQueue;
|
|
163
|
+
};
|
|
153
164
|
const start = async () => {
|
|
154
165
|
const receive_id_type = options.chatId.startsWith("ou_") || options.chatId.startsWith("on_") ? "open_id" : "chat_id";
|
|
155
166
|
const thinking = "Thinking…";
|
|
156
167
|
if (preferCard) {
|
|
157
168
|
const cardSpec = {
|
|
158
169
|
schema: "2.0",
|
|
159
|
-
config: {
|
|
170
|
+
config: {
|
|
171
|
+
update_multi: true,
|
|
172
|
+
streaming_mode: true
|
|
173
|
+
},
|
|
160
174
|
header: { title: {
|
|
161
175
|
tag: "plain_text",
|
|
162
176
|
content: "xopc"
|
|
@@ -213,7 +227,7 @@ function createFeishuStreamingAdapter(getConfig) {
|
|
|
213
227
|
})
|
|
214
228
|
}
|
|
215
229
|
});
|
|
216
|
-
messageId = res?.data?.message_id ?? res?.message_id
|
|
230
|
+
messageId = feishuOpenApiOk(res) ? res?.data?.message_id ?? res?.message_id : void 0;
|
|
217
231
|
if (messageId) recordBindingIfReply(messageId);
|
|
218
232
|
log.info({
|
|
219
233
|
accountId: account.accountId,
|
|
@@ -221,7 +235,7 @@ function createFeishuStreamingAdapter(getConfig) {
|
|
|
221
235
|
messageId,
|
|
222
236
|
mode: "card"
|
|
223
237
|
}, "Feishu streaming started");
|
|
224
|
-
return;
|
|
238
|
+
if (messageId) return;
|
|
225
239
|
}
|
|
226
240
|
cardId = void 0;
|
|
227
241
|
}
|
|
@@ -240,7 +254,7 @@ function createFeishuStreamingAdapter(getConfig) {
|
|
|
240
254
|
content: JSON.stringify({ text: thinking })
|
|
241
255
|
}
|
|
242
256
|
});
|
|
243
|
-
messageId = res?.data?.message_id ?? res?.message_id
|
|
257
|
+
messageId = feishuOpenApiOk(res) ? res?.data?.message_id ?? res?.message_id : void 0;
|
|
244
258
|
if (messageId) recordBindingIfReply(messageId);
|
|
245
259
|
log.info({
|
|
246
260
|
accountId: account.accountId,
|
|
@@ -255,22 +269,33 @@ function createFeishuStreamingAdapter(getConfig) {
|
|
|
255
269
|
accountId: account.accountId
|
|
256
270
|
}, "Feishu streaming start failed");
|
|
257
271
|
});
|
|
272
|
+
const schedulePreview = () => {
|
|
273
|
+
if (timer || ending || aborted || previewPending) return;
|
|
274
|
+
timer = setTimeout(() => {
|
|
275
|
+
timer = null;
|
|
276
|
+
previewPending = true;
|
|
277
|
+
const queuedText = lastText;
|
|
278
|
+
flush().catch((err) => log.warn({ err }, "Feishu streaming flush failed")).finally(() => {
|
|
279
|
+
previewPending = false;
|
|
280
|
+
if (queuedText !== lastText) schedulePreview();
|
|
281
|
+
});
|
|
282
|
+
}, 800);
|
|
283
|
+
};
|
|
258
284
|
return {
|
|
259
285
|
update: (text) => {
|
|
260
286
|
lastText = text;
|
|
261
|
-
|
|
262
|
-
timer = setTimeout(() => {
|
|
263
|
-
flush().catch((err) => log.warn({ err }, "Feishu streaming flush failed"));
|
|
264
|
-
}, 800);
|
|
287
|
+
schedulePreview();
|
|
265
288
|
},
|
|
266
289
|
end: async () => {
|
|
290
|
+
ending = true;
|
|
267
291
|
if (timer) clearTimeout(timer);
|
|
268
|
-
await flush();
|
|
292
|
+
await flush(true);
|
|
293
|
+
await closeCard();
|
|
269
294
|
log.debug({
|
|
270
295
|
accountId: account.accountId,
|
|
271
296
|
chatId: options.chatId,
|
|
272
297
|
messageId,
|
|
273
|
-
|
|
298
|
+
finalDelivered,
|
|
274
299
|
lastTextLen: lastText.length,
|
|
275
300
|
mode: preferCard ? "card" : "text"
|
|
276
301
|
}, "Feishu streaming ended");
|
|
@@ -278,9 +303,12 @@ function createFeishuStreamingAdapter(getConfig) {
|
|
|
278
303
|
abort: async () => {
|
|
279
304
|
aborted = true;
|
|
280
305
|
if (timer) clearTimeout(timer);
|
|
306
|
+
await ready;
|
|
307
|
+
await editQueue;
|
|
308
|
+
await closeCard();
|
|
281
309
|
},
|
|
282
310
|
messageId: () => void 0,
|
|
283
|
-
skipFinalOutbound: () =>
|
|
311
|
+
skipFinalOutbound: () => finalDelivered
|
|
284
312
|
};
|
|
285
313
|
} };
|
|
286
314
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ResolvedFeishuAccount } from '../../state/accounts.js';
|
|
2
|
+
export declare function probeFeishuBot(account: Pick<ResolvedFeishuAccount, 'appId' | 'appSecret' | 'domain'>, signal?: AbortSignal): Promise<{
|
|
3
|
+
ok: false;
|
|
4
|
+
name?: undefined;
|
|
5
|
+
openId?: undefined;
|
|
6
|
+
} | {
|
|
7
|
+
ok: true;
|
|
8
|
+
name: string;
|
|
9
|
+
openId: string;
|
|
10
|
+
}>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { resolveFeishuBaseUrl } from "./client.js";
|
|
2
|
+
//#region extensions/feishu/src/transport/client/bot-info.ts
|
|
3
|
+
async function probeFeishuBot(account, signal = AbortSignal.timeout(1e4)) {
|
|
4
|
+
const baseUrl = resolveFeishuBaseUrl(account.domain);
|
|
5
|
+
if (!baseUrl || !account.appId || !account.appSecret) return { ok: false };
|
|
6
|
+
const authResponse = await fetch(`${baseUrl}/open-apis/auth/v3/tenant_access_token/internal`, {
|
|
7
|
+
method: "POST",
|
|
8
|
+
headers: { "Content-Type": "application/json" },
|
|
9
|
+
body: JSON.stringify({
|
|
10
|
+
app_id: account.appId,
|
|
11
|
+
app_secret: account.appSecret
|
|
12
|
+
}),
|
|
13
|
+
redirect: "error",
|
|
14
|
+
signal
|
|
15
|
+
});
|
|
16
|
+
if (!authResponse.ok) return { ok: false };
|
|
17
|
+
const auth = await authResponse.json();
|
|
18
|
+
if (auth.code !== 0 || !auth.tenant_access_token) return { ok: false };
|
|
19
|
+
const infoResponse = await fetch(`${baseUrl}/open-apis/bot/v3/info`, {
|
|
20
|
+
headers: { Authorization: `Bearer ${auth.tenant_access_token}` },
|
|
21
|
+
redirect: "error",
|
|
22
|
+
signal
|
|
23
|
+
});
|
|
24
|
+
if (!infoResponse.ok) return { ok: false };
|
|
25
|
+
const info = await infoResponse.json();
|
|
26
|
+
if (info.code !== 0 || !info.bot?.open_id) return { ok: false };
|
|
27
|
+
return {
|
|
28
|
+
ok: true,
|
|
29
|
+
name: info.bot.app_name,
|
|
30
|
+
openId: info.bot.open_id
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
export { probeFeishuBot };
|