dsh-message-gateway 0.1.18 → 0.1.20
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/lib/index.js +282 -50
- package/lib/index.js.map +4 -4
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -3201,22 +3201,199 @@ function sanitizeSecrets(text) {
|
|
|
3201
3201
|
|
|
3202
3202
|
// src/host/wecom-bridge.ts
|
|
3203
3203
|
import { WSClient } from "@wecom/aibot-node-sdk";
|
|
3204
|
+
|
|
3205
|
+
// src/host/wecom-smartsheet.ts
|
|
3206
|
+
var WecomSmartsheetClient = class {
|
|
3207
|
+
constructor(creds) {
|
|
3208
|
+
this.creds = creds;
|
|
3209
|
+
}
|
|
3210
|
+
token = null;
|
|
3211
|
+
tokenExpiresAt = 0;
|
|
3212
|
+
/**
|
|
3213
|
+
* 获取有效的 access_token(自动内存缓存与续期)。
|
|
3214
|
+
*/
|
|
3215
|
+
async getAccessToken() {
|
|
3216
|
+
const now = Date.now();
|
|
3217
|
+
if (this.token && now < this.tokenExpiresAt - 6e4) {
|
|
3218
|
+
return this.token;
|
|
3219
|
+
}
|
|
3220
|
+
const corpId = this.creds.corpId || this.creds.botId || "";
|
|
3221
|
+
const secret = this.creds.secret || "";
|
|
3222
|
+
if (!corpId || !secret) {
|
|
3223
|
+
throw new Error("\u4F01\u4E1A\u5FAE\u4FE1\u51ED\u636E\u4E0D\u5B8C\u6574\uFF08\u7F3A\u5C11 botId/corpId \u6216 secret\uFF09");
|
|
3224
|
+
}
|
|
3225
|
+
const url = `https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=${encodeURIComponent(corpId)}&corpsecret=${encodeURIComponent(secret)}`;
|
|
3226
|
+
const res = await fetch(url, { signal: AbortSignal.timeout(1e4) });
|
|
3227
|
+
const data = await res.json();
|
|
3228
|
+
if (data.errcode !== 0 || !data.access_token) {
|
|
3229
|
+
throw new Error(`\u83B7\u53D6\u4F01\u4E1A\u5FAE\u4FE1 access_token \u5931\u8D25 [${data.errcode}]: ${data.errmsg || "\u672A\u77E5\u9519\u8BEF"}`);
|
|
3230
|
+
}
|
|
3231
|
+
this.token = data.access_token;
|
|
3232
|
+
this.tokenExpiresAt = now + (data.expires_in ?? 7200) * 1e3;
|
|
3233
|
+
return this.token;
|
|
3234
|
+
}
|
|
3235
|
+
/**
|
|
3236
|
+
* 映射字段类型为企微官方标准枚举。
|
|
3237
|
+
*/
|
|
3238
|
+
mapFieldType(type) {
|
|
3239
|
+
switch (type?.toLowerCase()) {
|
|
3240
|
+
case "number":
|
|
3241
|
+
return "FIELD_TYPE_NUMBER";
|
|
3242
|
+
case "single_select":
|
|
3243
|
+
case "select":
|
|
3244
|
+
return "FIELD_TYPE_SINGLE_SELECT";
|
|
3245
|
+
case "multi_select":
|
|
3246
|
+
return "FIELD_TYPE_MULTI_SELECT";
|
|
3247
|
+
case "date":
|
|
3248
|
+
case "date_time":
|
|
3249
|
+
case "datetime":
|
|
3250
|
+
return "FIELD_TYPE_DATE_TIME";
|
|
3251
|
+
case "user":
|
|
3252
|
+
case "member":
|
|
3253
|
+
return "FIELD_TYPE_USER";
|
|
3254
|
+
case "checkbox":
|
|
3255
|
+
return "FIELD_TYPE_CHECKBOX";
|
|
3256
|
+
case "text":
|
|
3257
|
+
default:
|
|
3258
|
+
return "FIELD_TYPE_TEXT";
|
|
3259
|
+
}
|
|
3260
|
+
}
|
|
3261
|
+
/**
|
|
3262
|
+
* 创建一份全新的在线智能表格(Smartsheet)。
|
|
3263
|
+
*/
|
|
3264
|
+
async createSmartsheet(options) {
|
|
3265
|
+
try {
|
|
3266
|
+
const token = await this.getAccessToken();
|
|
3267
|
+
const url = `https://qyapi.weixin.qq.com/cgi-bin/wedoc/smartsheet/create?access_token=${encodeURIComponent(token)}`;
|
|
3268
|
+
const fields = options.fields && options.fields.length > 0 ? options.fields.map((f) => {
|
|
3269
|
+
const fieldType = this.mapFieldType(f.type);
|
|
3270
|
+
const item = {
|
|
3271
|
+
field_title: f.title,
|
|
3272
|
+
field_type: fieldType
|
|
3273
|
+
};
|
|
3274
|
+
if ((fieldType === "FIELD_TYPE_SINGLE_SELECT" || fieldType === "FIELD_TYPE_MULTI_SELECT") && f.options) {
|
|
3275
|
+
item.property = {
|
|
3276
|
+
options: f.options.map((opt) => ({ text: String(opt) }))
|
|
3277
|
+
};
|
|
3278
|
+
}
|
|
3279
|
+
return item;
|
|
3280
|
+
}) : [
|
|
3281
|
+
{ field_title: "\u6807\u9898", field_type: "FIELD_TYPE_TEXT" },
|
|
3282
|
+
{ field_title: "\u72B6\u6001", field_type: "FIELD_TYPE_SINGLE_SELECT", property: { options: [{ text: "\u672A\u5F00\u59CB" }, { text: "\u8FDB\u884C\u4E2D" }, { text: "\u5DF2\u5B8C\u6210" }] } },
|
|
3283
|
+
{ field_title: "\u8D1F\u8D23\u4EBA", field_type: "FIELD_TYPE_USER" },
|
|
3284
|
+
{ field_title: "\u622A\u6B62\u65E5\u671F", field_type: "FIELD_TYPE_DATE_TIME" }
|
|
3285
|
+
];
|
|
3286
|
+
const payload = {
|
|
3287
|
+
doc_name: options.title || "\u65B0\u5EFA\u667A\u80FD\u8868\u683C",
|
|
3288
|
+
sheet_title: options.sheetTitle || "\u6570\u636E\u8868",
|
|
3289
|
+
fields
|
|
3290
|
+
};
|
|
3291
|
+
if (options.adminUsers && options.adminUsers.length > 0) {
|
|
3292
|
+
payload.admin_users = options.adminUsers;
|
|
3293
|
+
}
|
|
3294
|
+
const res = await fetch(url, {
|
|
3295
|
+
method: "POST",
|
|
3296
|
+
headers: { "content-type": "application/json" },
|
|
3297
|
+
body: JSON.stringify(payload),
|
|
3298
|
+
signal: AbortSignal.timeout(15e3)
|
|
3299
|
+
});
|
|
3300
|
+
const data = await res.json();
|
|
3301
|
+
if (data.errcode !== 0 || !data.docid) {
|
|
3302
|
+
return {
|
|
3303
|
+
ok: false,
|
|
3304
|
+
error: `\u521B\u5EFA\u667A\u80FD\u8868\u683C\u5931\u8D25 [${data.errcode}]: ${data.errmsg || "\u672A\u77E5\u9519\u8BEF"}`
|
|
3305
|
+
};
|
|
3306
|
+
}
|
|
3307
|
+
if (options.records && options.records.length > 0) {
|
|
3308
|
+
await this.addRecords(data.docid, options.records).catch((err) => {
|
|
3309
|
+
console.warn("[WecomSmartsheetClient] \u5199\u5165\u521D\u59CB\u6570\u636E\u5931\u8D25\uFF08\u8868\u683C\u5DF2\u521B\u5EFA\u6210\u529F\uFF09:", err);
|
|
3310
|
+
});
|
|
3311
|
+
}
|
|
3312
|
+
return {
|
|
3313
|
+
ok: true,
|
|
3314
|
+
docid: data.docid,
|
|
3315
|
+
url: data.url || `https://doc.weixin.qq.com/smartsheet/${data.docid}`
|
|
3316
|
+
};
|
|
3317
|
+
} catch (error) {
|
|
3318
|
+
return {
|
|
3319
|
+
ok: false,
|
|
3320
|
+
error: error instanceof Error ? error.message : String(error)
|
|
3321
|
+
};
|
|
3322
|
+
}
|
|
3323
|
+
}
|
|
3324
|
+
/**
|
|
3325
|
+
* 向指定的智能表格中追加行记录。
|
|
3326
|
+
*/
|
|
3327
|
+
async addRecords(docid, records, sheetId) {
|
|
3328
|
+
try {
|
|
3329
|
+
const token = await this.getAccessToken();
|
|
3330
|
+
const url = `https://qyapi.weixin.qq.com/cgi-bin/wedoc/smartsheet/add_records?access_token=${encodeURIComponent(token)}`;
|
|
3331
|
+
const recordItems = records.map((rec) => ({
|
|
3332
|
+
values: rec
|
|
3333
|
+
}));
|
|
3334
|
+
const payload = {
|
|
3335
|
+
docid,
|
|
3336
|
+
records: recordItems
|
|
3337
|
+
};
|
|
3338
|
+
if (sheetId) {
|
|
3339
|
+
payload.sheet_id = sheetId;
|
|
3340
|
+
}
|
|
3341
|
+
const res = await fetch(url, {
|
|
3342
|
+
method: "POST",
|
|
3343
|
+
headers: { "content-type": "application/json" },
|
|
3344
|
+
body: JSON.stringify(payload),
|
|
3345
|
+
signal: AbortSignal.timeout(15e3)
|
|
3346
|
+
});
|
|
3347
|
+
const data = await res.json();
|
|
3348
|
+
if (data.errcode !== 0) {
|
|
3349
|
+
return { ok: false, error: `\u6DFB\u52A0\u8BB0\u5F55\u5931\u8D25 [${data.errcode}]: ${data.errmsg || "\u672A\u77E5\u9519\u8BEF"}` };
|
|
3350
|
+
}
|
|
3351
|
+
return { ok: true };
|
|
3352
|
+
} catch (error) {
|
|
3353
|
+
return { ok: false, error: error instanceof Error ? error.message : String(error) };
|
|
3354
|
+
}
|
|
3355
|
+
}
|
|
3356
|
+
};
|
|
3357
|
+
|
|
3358
|
+
// src/host/wecom-bridge.ts
|
|
3204
3359
|
var WecomBridge = class {
|
|
3205
3360
|
constructor(cred, callbacks) {
|
|
3206
3361
|
this.cred = cred;
|
|
3207
3362
|
this.callbacks = callbacks;
|
|
3363
|
+
this.smartsheetClient = new WecomSmartsheetClient({
|
|
3364
|
+
botId: cred.botId,
|
|
3365
|
+
secret: cred.secret
|
|
3366
|
+
});
|
|
3208
3367
|
}
|
|
3209
3368
|
client = null;
|
|
3369
|
+
smartsheetClient = null;
|
|
3210
3370
|
started = false;
|
|
3211
3371
|
status = { state: "idle", detail: "", connectedAt: null };
|
|
3212
3372
|
setStatus(state, detail = "") {
|
|
3213
3373
|
this.status = {
|
|
3214
3374
|
state,
|
|
3215
3375
|
detail,
|
|
3216
|
-
connectedAt: state === "connected" ? Date.now() :
|
|
3376
|
+
connectedAt: state === "connected" ? this.status.connectedAt ?? Date.now() : null
|
|
3217
3377
|
};
|
|
3218
3378
|
this.callbacks.onStatus(this.status);
|
|
3219
3379
|
}
|
|
3380
|
+
/** 获取智能表格操作客户端 */
|
|
3381
|
+
getSmartsheetClient() {
|
|
3382
|
+
return this.smartsheetClient;
|
|
3383
|
+
}
|
|
3384
|
+
/** 获取底层已认证连接的 WSClient 实例 */
|
|
3385
|
+
getClient() {
|
|
3386
|
+
return this.client;
|
|
3387
|
+
}
|
|
3388
|
+
/**
|
|
3389
|
+
* 创建企业微信智能表格。
|
|
3390
|
+
*/
|
|
3391
|
+
async createSmartsheet(options) {
|
|
3392
|
+
if (!this.smartsheetClient) {
|
|
3393
|
+
return { ok: false, error: "\u4F01\u4E1A\u5FAE\u4FE1\u667A\u80FD\u8868\u683C\u5BA2\u6237\u7AEF\u672A\u521D\u59CB\u5316" };
|
|
3394
|
+
}
|
|
3395
|
+
return await this.smartsheetClient.createSmartsheet(options);
|
|
3396
|
+
}
|
|
3220
3397
|
/** 建立常驻连接(断线由 SDK 自动指数退避重连)。 */
|
|
3221
3398
|
start() {
|
|
3222
3399
|
if (this.started) return;
|
|
@@ -3224,7 +3401,9 @@ var WecomBridge = class {
|
|
|
3224
3401
|
this.setStatus("connecting");
|
|
3225
3402
|
const client = new WSClient({ botId: this.cred.botId, secret: this.cred.secret });
|
|
3226
3403
|
this.client = client;
|
|
3227
|
-
client.on("authenticated", () =>
|
|
3404
|
+
client.on("authenticated", () => {
|
|
3405
|
+
this.setStatus("connected", `botId ${this.cred.botId}`);
|
|
3406
|
+
});
|
|
3228
3407
|
client.on("message.text", (frame) => {
|
|
3229
3408
|
const content = frame.body?.text?.content ?? "";
|
|
3230
3409
|
if (content.trim() !== "") this.callbacks.onText(content, frame);
|
|
@@ -4373,18 +4552,18 @@ var ImapClient = class {
|
|
|
4373
4552
|
return match[1].trim().split(/\s+/).filter((s) => s !== "").map(Number);
|
|
4374
4553
|
}
|
|
4375
4554
|
/** FETCH 头部与正文(BODY.PEEK 不置已读)。 */
|
|
4376
|
-
async fetchMessage(
|
|
4555
|
+
async fetchMessage(id) {
|
|
4377
4556
|
const res = await this.command(
|
|
4378
|
-
`FETCH ${
|
|
4557
|
+
`FETCH ${id} (UID BODY.PEEK[HEADER.FIELDS (FROM SUBJECT MESSAGE-ID REFERENCES IN-REPLY-TO)] BODY.PEEK[TEXT])`,
|
|
4379
4558
|
3e4
|
|
4380
4559
|
);
|
|
4381
|
-
const uid = res.match(/UID (\d+)/)?.[1] ?? String(
|
|
4560
|
+
const uid = res.match(/UID (\d+)/)?.[1] ?? String(id);
|
|
4382
4561
|
const header = literalAt(res, "BODY[HEADER.FIELDS (FROM SUBJECT MESSAGE-ID REFERENCES IN-REPLY-TO)]")?.value ?? "";
|
|
4383
4562
|
const text = literalAt(res, "BODY[TEXT]")?.value ?? "";
|
|
4384
4563
|
return { uid, header, text };
|
|
4385
4564
|
}
|
|
4386
|
-
async markSeen(
|
|
4387
|
-
await this.command(`STORE ${
|
|
4565
|
+
async markSeen(id) {
|
|
4566
|
+
await this.command(`STORE ${id} +FLAGS (\\Seen)`);
|
|
4388
4567
|
}
|
|
4389
4568
|
close() {
|
|
4390
4569
|
try {
|
|
@@ -4647,9 +4826,9 @@ var EmailBridge = class {
|
|
|
4647
4826
|
this.setStatus("connected", "email");
|
|
4648
4827
|
}
|
|
4649
4828
|
const ids = await this.imap.searchUnseen();
|
|
4650
|
-
for (const
|
|
4829
|
+
for (const id of ids) {
|
|
4651
4830
|
if (this.stopped) break;
|
|
4652
|
-
await this.processMessage(
|
|
4831
|
+
await this.processMessage(id);
|
|
4653
4832
|
}
|
|
4654
4833
|
await sleep(POLL_MS);
|
|
4655
4834
|
} catch (error) {
|
|
@@ -4661,16 +4840,16 @@ var EmailBridge = class {
|
|
|
4661
4840
|
}
|
|
4662
4841
|
}
|
|
4663
4842
|
}
|
|
4664
|
-
async processMessage(
|
|
4843
|
+
async processMessage(id) {
|
|
4665
4844
|
if (this.imap === null) return;
|
|
4666
4845
|
let fetched;
|
|
4667
4846
|
try {
|
|
4668
|
-
fetched = await this.imap.fetchMessage(
|
|
4847
|
+
fetched = await this.imap.fetchMessage(id);
|
|
4669
4848
|
} catch {
|
|
4670
4849
|
return;
|
|
4671
4850
|
}
|
|
4672
4851
|
if (this.seen.has(fetched.uid)) {
|
|
4673
|
-
void this.imap.markSeen(
|
|
4852
|
+
void this.imap.markSeen(id).catch(() => {
|
|
4674
4853
|
});
|
|
4675
4854
|
return;
|
|
4676
4855
|
}
|
|
@@ -4682,7 +4861,7 @@ var EmailBridge = class {
|
|
|
4682
4861
|
const from = parseAddress(headerField(fetched.header, "From"));
|
|
4683
4862
|
const subject = headerField(fetched.header, "Subject");
|
|
4684
4863
|
const text = cleanBody(fetched.text);
|
|
4685
|
-
void this.imap.markSeen(
|
|
4864
|
+
void this.imap.markSeen(id).catch(() => {
|
|
4686
4865
|
});
|
|
4687
4866
|
if (from === "" || text.trim() === "") return;
|
|
4688
4867
|
const frame = { from, subject, threadKey };
|
|
@@ -4797,10 +4976,6 @@ var BridgeManager = class {
|
|
|
4797
4976
|
void this.pushStream(p, true);
|
|
4798
4977
|
this.finishPending(key);
|
|
4799
4978
|
return;
|
|
4800
|
-
} else {
|
|
4801
|
-
id.sink.stream(id.frame, p.streamId, "\u2705 \u5904\u7406\u5B8C\u6210", true);
|
|
4802
|
-
this.finishPending(key);
|
|
4803
|
-
return;
|
|
4804
4979
|
}
|
|
4805
4980
|
}
|
|
4806
4981
|
}
|
|
@@ -4978,26 +5153,26 @@ var BridgeManager = class {
|
|
|
4978
5153
|
* 外部平台消息统一入口:命令优先,否则注入该聊天独立 agent 会话,
|
|
4979
5154
|
* 回复经 sink 流式回发。所有已打通的平台共用此管线。
|
|
4980
5155
|
*/
|
|
4981
|
-
async handleExternalMessage(
|
|
5156
|
+
async handleExternalMessage(id, rawText) {
|
|
4982
5157
|
if (this.disposed) return;
|
|
4983
5158
|
const text = stripMention(rawText);
|
|
4984
5159
|
const reply = (content) => {
|
|
4985
|
-
|
|
5160
|
+
id.sink.stream(id.frame, `cmd-${Date.now().toString(36)}`, content, true);
|
|
4986
5161
|
};
|
|
4987
5162
|
try {
|
|
4988
|
-
if (
|
|
4989
|
-
if (await this.handleCommand(text,
|
|
5163
|
+
if (id.chatType === "group" && !this.config.groupReply) return;
|
|
5164
|
+
if (await this.handleCommand(text, id.key, reply)) return;
|
|
4990
5165
|
if (this.awaiting !== null) {
|
|
4991
5166
|
console.warn("[dsh-message-gateway] busy: webhook in flight, drop message");
|
|
4992
5167
|
return;
|
|
4993
5168
|
}
|
|
4994
|
-
if (this.pendingMap.has(
|
|
4995
|
-
|
|
5169
|
+
if (this.pendingMap.has(id.key)) {
|
|
5170
|
+
id.sink.stream(id.frame, `busy-${Date.now().toString(36)}`, this.t("busy"), true);
|
|
4996
5171
|
return;
|
|
4997
5172
|
}
|
|
4998
|
-
const route = this.matchRoute(
|
|
5173
|
+
const route = this.matchRoute(id.key, text);
|
|
4999
5174
|
const routedText = route !== null && route.prefix !== "" ? text.slice(route.prefix.length).trim() : text;
|
|
5000
|
-
const agent = await this.ensureAgentForKey(
|
|
5175
|
+
const agent = await this.ensureAgentForKey(id.key, route?.rule);
|
|
5001
5176
|
if (agent === null) {
|
|
5002
5177
|
console.warn("[dsh-message-gateway] no dedicated agent available");
|
|
5003
5178
|
return;
|
|
@@ -5010,8 +5185,8 @@ var BridgeManager = class {
|
|
|
5010
5185
|
};
|
|
5011
5186
|
const session = agent.session;
|
|
5012
5187
|
const p = {
|
|
5013
|
-
frame:
|
|
5014
|
-
sink:
|
|
5188
|
+
frame: id.frame,
|
|
5189
|
+
sink: id.sink,
|
|
5015
5190
|
streamId: `gw-${Date.now().toString(36)}`,
|
|
5016
5191
|
buffer: "",
|
|
5017
5192
|
cursor: session.seq,
|
|
@@ -5024,25 +5199,25 @@ var BridgeManager = class {
|
|
|
5024
5199
|
startedAt: Date.now(),
|
|
5025
5200
|
heartbeat: null
|
|
5026
5201
|
};
|
|
5027
|
-
this.pendingMap.set(
|
|
5028
|
-
if (
|
|
5202
|
+
this.pendingMap.set(id.key, p);
|
|
5203
|
+
if (id.sink.ack !== false) {
|
|
5029
5204
|
p.ackSent = true;
|
|
5030
|
-
|
|
5205
|
+
id.sink.stream(id.frame, p.streamId, this.t("ack"), false);
|
|
5031
5206
|
p.heartbeat = setInterval(() => {
|
|
5032
|
-
if (this.pendingMap.get(
|
|
5207
|
+
if (this.pendingMap.get(id.key) !== p || p.pushed) return;
|
|
5033
5208
|
const elapsed = Math.round((Date.now() - p.startedAt) / 1e3);
|
|
5034
|
-
|
|
5209
|
+
id.sink.stream(id.frame, p.streamId, `${this.t("ack")}\uFF08${elapsed}s\uFF09`, false);
|
|
5035
5210
|
}, ACK_HEARTBEAT_INTERVAL);
|
|
5036
5211
|
p.heartbeat.unref?.();
|
|
5037
5212
|
}
|
|
5038
5213
|
agent.send(message, "next-turn", true);
|
|
5039
|
-
console.log("[dsh-message-gateway] sent to agent", { key:
|
|
5214
|
+
console.log("[dsh-message-gateway] sent to agent", { key: id.key, text: sanitizeSecrets(text.slice(0, 60)), baseSeq: p.cursor });
|
|
5040
5215
|
p.timer = setInterval(() => {
|
|
5041
|
-
if (this.pendingMap.get(
|
|
5042
|
-
this.pollPending(
|
|
5216
|
+
if (this.pendingMap.get(id.key) !== p) return;
|
|
5217
|
+
this.pollPending(id.key, p, session);
|
|
5043
5218
|
}, 400);
|
|
5044
5219
|
p.fallback = setTimeout(() => {
|
|
5045
|
-
if (this.pendingMap.get(
|
|
5220
|
+
if (this.pendingMap.get(id.key) === p) this.finishPending(id.key);
|
|
5046
5221
|
}, 2 * 60 * 1e3);
|
|
5047
5222
|
p.fallback.unref?.();
|
|
5048
5223
|
} catch (error) {
|
|
@@ -5483,16 +5658,16 @@ ${secret}`;
|
|
|
5483
5658
|
this.email = null;
|
|
5484
5659
|
}
|
|
5485
5660
|
/** 任意桥接平台的状态(telegram/discord/qq/email)。 */
|
|
5486
|
-
bridgeStatus(
|
|
5487
|
-
if (
|
|
5488
|
-
if (
|
|
5489
|
-
if (
|
|
5490
|
-
if (
|
|
5661
|
+
bridgeStatus(id) {
|
|
5662
|
+
if (id === "telegram") return this.telegram?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
5663
|
+
if (id === "discord") return this.discord?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
5664
|
+
if (id === "qq") return this.qq?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
5665
|
+
if (id === "email") return this.email?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
5491
5666
|
return { state: "idle", detail: "", connectedAt: null };
|
|
5492
5667
|
}
|
|
5493
5668
|
/** 将桥状态并入存储状态。 */
|
|
5494
|
-
mergeStatus(stored,
|
|
5495
|
-
const live =
|
|
5669
|
+
mergeStatus(stored, id = "wecom-aibot") {
|
|
5670
|
+
const live = id === "wecom-aibot" ? this.wecomStatus() : this.bridgeStatus(id);
|
|
5496
5671
|
if (live.state === "connected") {
|
|
5497
5672
|
return { state: "connected", detail: live.detail, testedAt: live.connectedAt ?? stored?.testedAt ?? null };
|
|
5498
5673
|
}
|
|
@@ -5764,8 +5939,8 @@ var PLATFORMS = [
|
|
|
5764
5939
|
fields: [{ key: "secret", labelKey: "field.webhookSecret", placeholderKey: "field.secret.ph", kind: "secret" }]
|
|
5765
5940
|
}
|
|
5766
5941
|
];
|
|
5767
|
-
function platformDef(
|
|
5768
|
-
return PLATFORMS.find((p) => p.id ===
|
|
5942
|
+
function platformDef(id) {
|
|
5943
|
+
return PLATFORMS.find((p) => p.id === id);
|
|
5769
5944
|
}
|
|
5770
5945
|
async function httpJson(url, init) {
|
|
5771
5946
|
const controller = new AbortController();
|
|
@@ -5803,8 +5978,8 @@ function tcpBanner(host, port, timeoutMs = 8e3) {
|
|
|
5803
5978
|
});
|
|
5804
5979
|
});
|
|
5805
5980
|
}
|
|
5806
|
-
async function testPlatform(
|
|
5807
|
-
switch (
|
|
5981
|
+
async function testPlatform(id, cred) {
|
|
5982
|
+
switch (id) {
|
|
5808
5983
|
case "telegram": {
|
|
5809
5984
|
const { status, body } = await httpJson(`https://api.telegram.org/bot${cred.token ?? ""}/getMe`);
|
|
5810
5985
|
const r = body;
|
|
@@ -6763,9 +6938,9 @@ function apply(ctx, config = Config({})) {
|
|
|
6763
6938
|
}
|
|
6764
6939
|
});
|
|
6765
6940
|
const disposeRoutes = registerGatewayRoutes(ctx, manager);
|
|
6766
|
-
let
|
|
6941
|
+
let disposeTools;
|
|
6767
6942
|
if (ctx.tools) {
|
|
6768
|
-
|
|
6943
|
+
const disposeSend = ctx.tools.register(defineTool({
|
|
6769
6944
|
name: "send_chat_message",
|
|
6770
6945
|
description: "\u5411\u5DF2\u8FDE\u63A5\u7684\u6D88\u606F\u5E73\u53F0\uFF08Telegram / Discord / \u4F01\u4E1A\u5FAE\u4FE1\u667A\u80FD\u673A\u5668\u4EBA / Email \u7B49\uFF09\u4E3B\u52A8\u63A8\u9001\u6587\u672C\u6D88\u606F\u3002\u4F8B\u5982\u628A\u4EE3\u7801\u603B\u7ED3\u3001\u4EFB\u52A1\u7ED3\u679C\u63A8\u9001\u81F3\u6307\u5B9A\u7684\u7FA4\u804A\u6216\u79C1\u4FE1\u9891\u9053\u3002",
|
|
6771
6946
|
parameters: {
|
|
@@ -6829,9 +7004,66 @@ function apply(ctx, config = Config({})) {
|
|
|
6829
7004
|
return `\u5DF2\u6210\u529F\u63A8\u9001\u81F3 [${platform}] \u76EE\u6807 ${target}`;
|
|
6830
7005
|
}
|
|
6831
7006
|
}));
|
|
7007
|
+
const disposeSmartsheet = ctx.tools.register(defineTool({
|
|
7008
|
+
name: "wecom_create_smartsheet",
|
|
7009
|
+
description: "\u5728\u4F01\u4E1A\u5FAE\u4FE1\u4E2D\u521B\u5EFA\u4E00\u4EFD\u5168\u65B0\u7684\u5728\u7EBF\u667A\u80FD\u8868\u683C\uFF08Smartsheet\uFF09\uFF0C\u652F\u6301\u5B9A\u4E49\u8868\u683C\u6807\u9898\u3001\u5B57\u6BB5\u5217\uFF08\u6587\u672C/\u5355\u9009/\u591A\u9009/\u4EBA\u5458/\u65E5\u671F/\u6570\u5B57\u7B49\uFF09\u4E0E\u521D\u59CB\u6570\u636E\u884C\uFF0C\u5E76\u8FD4\u56DE\u667A\u80FD\u8868\u683C\u5728\u7EBF\u8BBF\u95EE\u94FE\u63A5\u3002",
|
|
7010
|
+
parameters: {
|
|
7011
|
+
title: {
|
|
7012
|
+
type: "string",
|
|
7013
|
+
required: true,
|
|
7014
|
+
description: "\u667A\u80FD\u8868\u683C\u6587\u6863\u6807\u9898\uFF08\u5982\uFF1A2026\u5E74Q3\u9879\u76EE\u5F00\u53D1\u6392\u671F\u8868\uFF09"
|
|
7015
|
+
},
|
|
7016
|
+
sheetTitle: {
|
|
7017
|
+
type: "string",
|
|
7018
|
+
description: "\u9996\u4E2A\u5DE5\u4F5C\u8868\u540D\u79F0\uFF08\u9ED8\u8BA4\uFF1A\u6570\u636E\u8868\uFF09"
|
|
7019
|
+
},
|
|
7020
|
+
fields: {
|
|
7021
|
+
type: "array",
|
|
7022
|
+
description: '\u5217\u5B57\u6BB5\u5B9A\u4E49\u5217\u8868\uFF0C\u5982 [{"title": "\u4EFB\u52A1\u540D\u79F0", "type": "text"}, {"title": "\u72B6\u6001", "type": "single_select", "options": ["\u5F85\u529E", "\u8FDB\u884C\u4E2D", "\u5DF2\u5B8C\u6210"]}]'
|
|
7023
|
+
},
|
|
7024
|
+
records: {
|
|
7025
|
+
type: "array",
|
|
7026
|
+
description: '\u521D\u59CB\u586B\u5165\u7684\u6570\u636E\u884C\u5217\u8868\uFF0C\u5982 [{"\u4EFB\u52A1\u540D\u79F0": "\u9700\u6C42\u8BC4\u5BA1", "\u72B6\u6001": "\u5DF2\u5B8C\u6210"}]'
|
|
7027
|
+
}
|
|
7028
|
+
},
|
|
7029
|
+
output: {
|
|
7030
|
+
schema: { type: "string" },
|
|
7031
|
+
render: (_args, value) => [{ type: "text", text: String(value) }]
|
|
7032
|
+
},
|
|
7033
|
+
async execute(args) {
|
|
7034
|
+
const title = String(args.title ?? "").trim();
|
|
7035
|
+
if (!title) return "\u9519\u8BEF\uFF1A\u8868\u683C\u6807\u9898 title \u4E3A\u5FC5\u586B\u9879";
|
|
7036
|
+
const store = await loadStore();
|
|
7037
|
+
const wecomCred = store.platforms["wecom-aibot"] || store.platforms["wecom"];
|
|
7038
|
+
if (!wecomCred || !wecomCred.secret && !wecomCred.corpId && !wecomCred.botId) {
|
|
7039
|
+
return "\u9519\u8BEF\uFF1A\u672A\u5728\u6D88\u606F\u5E73\u53F0\u4E2D\u914D\u7F6E\u4F01\u4E1A\u5FAE\u4FE1\u51ED\u636E\uFF0C\u8BF7\u5148\u5728\u4FA7\u8FB9\u680F\u300C\u6D88\u606F\u5E73\u53F0\u300D\u4E2D\u914D\u7F6E\u4F01\u4E1A\u5FAE\u4FE1\u667A\u80FD\u673A\u5668\u4EBA\u6216\u81EA\u5EFA\u5E94\u7528\u51ED\u636E";
|
|
7040
|
+
}
|
|
7041
|
+
const client = new WecomSmartsheetClient({
|
|
7042
|
+
botId: wecomCred.botId,
|
|
7043
|
+
secret: wecomCred.secret,
|
|
7044
|
+
corpId: wecomCred.corpId
|
|
7045
|
+
});
|
|
7046
|
+
const res = await client.createSmartsheet({
|
|
7047
|
+
title,
|
|
7048
|
+
sheetTitle: args.sheetTitle,
|
|
7049
|
+
fields: args.fields,
|
|
7050
|
+
records: args.records
|
|
7051
|
+
});
|
|
7052
|
+
if (!res.ok) {
|
|
7053
|
+
return `\u521B\u5EFA\u667A\u80FD\u8868\u683C\u5931\u8D25\uFF1A${res.error}`;
|
|
7054
|
+
}
|
|
7055
|
+
return `\u2705 \u667A\u80FD\u8868\u683C\u300C${title}\u300D\u521B\u5EFA\u6210\u529F\uFF01
|
|
7056
|
+
- \u6587\u6863 ID: ${res.docid}
|
|
7057
|
+
- \u5728\u7EBF\u8BBF\u95EE\u94FE\u63A5: ${res.url}`;
|
|
7058
|
+
}
|
|
7059
|
+
}));
|
|
7060
|
+
disposeTools = () => {
|
|
7061
|
+
disposeSend();
|
|
7062
|
+
disposeSmartsheet();
|
|
7063
|
+
};
|
|
6832
7064
|
}
|
|
6833
7065
|
return () => {
|
|
6834
|
-
|
|
7066
|
+
disposeTools?.();
|
|
6835
7067
|
disposeRoutes();
|
|
6836
7068
|
void manager.dispose();
|
|
6837
7069
|
};
|