dsh-plugin-lark-bridge 0.1.3 → 0.1.4
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/README.md +1 -63
- package/dist/bridge.d.ts +15 -3
- package/dist/bridge.d.ts.map +1 -1
- package/dist/client/config-form.d.ts +10 -2
- package/dist/client/config-form.d.ts.map +1 -1
- package/dist/client/config-page.d.ts +8 -0
- package/dist/client/config-page.d.ts.map +1 -1
- package/dist/client/index.d.ts +3 -1
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/locales.d.ts +15 -1
- package/dist/client/locales.d.ts.map +1 -1
- package/dist/client/model-defaults.d.ts +49 -0
- package/dist/client/model-defaults.d.ts.map +1 -0
- package/dist/client/preset-defaults.d.ts +84 -0
- package/dist/client/preset-defaults.d.ts.map +1 -0
- package/dist/client/preset-select.d.ts +26 -0
- package/dist/client/preset-select.d.ts.map +1 -0
- package/dist/client.js +1244 -21
- package/dist/config.d.ts +20 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/constants.d.ts +7 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +432 -90
- package/dist/quote.d.ts +49 -0
- package/dist/quote.d.ts.map +1 -0
- package/dist/reminder.d.ts +1 -1
- package/dist/reminder.d.ts.map +1 -1
- package/dist/sessions.d.ts +10 -4
- package/dist/sessions.d.ts.map +1 -1
- package/dist/tools.d.ts +1 -0
- package/dist/tools.d.ts.map +1 -1
- package/dist/utils.d.ts +2 -0
- package/dist/utils.d.ts.map +1 -1
- package/icon.svg +9 -0
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +30 -19
package/dist/index.mjs
CHANGED
|
@@ -2,8 +2,8 @@ import { spawn } from "node:child_process";
|
|
|
2
2
|
import { createInterface } from "node:readline";
|
|
3
3
|
import { homedir } from "node:os";
|
|
4
4
|
import { dirname, join, resolve } from "node:path";
|
|
5
|
+
import { ReasoningEffortId, createUserMessage } from "@deepseek-ai/dsh-llm";
|
|
5
6
|
import Schema from "@deepseek-ai/schemastery";
|
|
6
|
-
import { createUserMessage } from "@deepseek-ai/dsh-llm";
|
|
7
7
|
import { randomUUID } from "node:crypto";
|
|
8
8
|
import { mkdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
9
9
|
import { SessionId } from "@deepseek-ai/dsh-session";
|
|
@@ -37,6 +37,10 @@ function parseTimestamp(value) {
|
|
|
37
37
|
const parsed = Number(value);
|
|
38
38
|
return Number.isFinite(parsed) && parsed > 0 ? parsed : Date.now();
|
|
39
39
|
}
|
|
40
|
+
/** 转义块内正文的结束标记,避免提前闭合外层块。 */
|
|
41
|
+
function escapeBlockText(value) {
|
|
42
|
+
return value.replaceAll("</lark-message>", "<\\/lark-message>").replaceAll("</quote>", "<\\/quote>").replaceAll("</forwarded_messages>", "<\\/forwarded_messages>");
|
|
43
|
+
}
|
|
40
44
|
/**
|
|
41
45
|
* 把文本包成 system-reminder 帧。
|
|
42
46
|
* @param text - 提醒正文。
|
|
@@ -398,36 +402,175 @@ const PLUGIN_LABEL = "dsh-plugin-lark-bridge";
|
|
|
398
402
|
/** 工具名。 */
|
|
399
403
|
const NOTIFY_TOOL_NAME = "lark_notify";
|
|
400
404
|
/** 桥接会话的固定标题。 */
|
|
401
|
-
const SESSION_TITLE = "
|
|
405
|
+
const SESSION_TITLE = "飞书桥接会话";
|
|
402
406
|
/** 状态目录名,位于 DSH home 下。 */
|
|
403
407
|
const STATE_DIR = "lark-bridge";
|
|
404
408
|
/** 状态文件名。 */
|
|
405
409
|
const STATE_FILE = "state.json";
|
|
406
410
|
/** 单条飞书文本消息的最大字符数。 */
|
|
407
411
|
const MAX_MESSAGE_CHARS = 15e3;
|
|
412
|
+
/** 读取被引消息单次调用的超时毫秒数。 */
|
|
413
|
+
const QUOTE_TIMEOUT_MS = 8e3;
|
|
414
|
+
/** 注入前的静默窗口:队列在此期间没有新消息才发起注入。 */
|
|
415
|
+
const INJECT_SILENCE_MS = 15e3;
|
|
408
416
|
/** 监听重启的初始与最大退避。 */
|
|
409
417
|
const RESTART_MIN_MS = 2e3;
|
|
410
418
|
const RESTART_MAX_MS = 6e4;
|
|
411
419
|
//#endregion
|
|
420
|
+
//#region src/quote.ts
|
|
421
|
+
/** 读取身份顺序:user 优先,bot 兜底。 */
|
|
422
|
+
const IDENTITIES = ["user", "bot"];
|
|
423
|
+
/** 读取失败时的引用模型。 */
|
|
424
|
+
function unavailableQuote(messageId) {
|
|
425
|
+
return {
|
|
426
|
+
messageId,
|
|
427
|
+
from: "user",
|
|
428
|
+
time: "",
|
|
429
|
+
kind: "text",
|
|
430
|
+
text: "",
|
|
431
|
+
unavailable: true
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
/** 从 mget 输出中取出第一条消息。 */
|
|
435
|
+
function firstMessage(value) {
|
|
436
|
+
if (!isRecord(value)) return void 0;
|
|
437
|
+
const messages = value["messages"];
|
|
438
|
+
if (!Array.isArray(messages)) return void 0;
|
|
439
|
+
const first = messages[0];
|
|
440
|
+
return isRecord(first) ? first : void 0;
|
|
441
|
+
}
|
|
442
|
+
/** mget 已格式化的时间文本(`YYYY-MM-DD HH:mm[:ss]`)。 */
|
|
443
|
+
const MGET_TIME = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}(?::\d{2})?$/;
|
|
444
|
+
/** 归一时间:mget 已在本机时区格式化,直接沿用;其他形式交给 formatTime。 */
|
|
445
|
+
function quoteTime(value) {
|
|
446
|
+
if (typeof value !== "string") return "";
|
|
447
|
+
const text = value.trim();
|
|
448
|
+
if (MGET_TIME.test(text)) return text.length === 16 ? `${text}:00` : text;
|
|
449
|
+
return formatTime(text);
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* 把一条 mget 消息转成引用模型。
|
|
453
|
+
* @param messageId - 被引消息标识。
|
|
454
|
+
* @param raw - mget 返回的单条消息。
|
|
455
|
+
*/
|
|
456
|
+
function toQuotedMessage(messageId, raw) {
|
|
457
|
+
const senderType = (isRecord(raw["sender"]) ? raw["sender"] : {})["sender_type"];
|
|
458
|
+
const from = senderType === "bot" || senderType === "app" ? "bot" : "user";
|
|
459
|
+
const type = String(raw["msg_type"] ?? "text");
|
|
460
|
+
const content = typeof raw["content"] === "string" ? raw["content"].trim() : "";
|
|
461
|
+
const time = quoteTime(raw["create_time"]);
|
|
462
|
+
if (type === "merge_forward") return {
|
|
463
|
+
messageId,
|
|
464
|
+
from,
|
|
465
|
+
time,
|
|
466
|
+
kind: "forwarded",
|
|
467
|
+
text: "",
|
|
468
|
+
unavailable: false
|
|
469
|
+
};
|
|
470
|
+
if (type !== "text" && type !== "post") return {
|
|
471
|
+
messageId,
|
|
472
|
+
from,
|
|
473
|
+
time,
|
|
474
|
+
kind: "other",
|
|
475
|
+
text: `[${type} 消息]`,
|
|
476
|
+
unavailable: false
|
|
477
|
+
};
|
|
478
|
+
return {
|
|
479
|
+
messageId,
|
|
480
|
+
from,
|
|
481
|
+
time,
|
|
482
|
+
kind: "text",
|
|
483
|
+
text: content === "" ? `[${type} 消息]` : content,
|
|
484
|
+
unavailable: false
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* 把引用正文截断到上限,去掉代理对残留。
|
|
489
|
+
* @param text - 原始正文。
|
|
490
|
+
* @param limit - 最大字符数。
|
|
491
|
+
*/
|
|
492
|
+
function truncateQuote(text, limit = 500) {
|
|
493
|
+
if (text.length <= limit) return text;
|
|
494
|
+
return `${splitText(text, limit)[0] ?? ""}(已截断)`;
|
|
495
|
+
}
|
|
496
|
+
/** 转义引用正文中的块结束标记。 */
|
|
497
|
+
function escapeBody(text) {
|
|
498
|
+
return text.replaceAll("</quote>", "<\\/quote>").replaceAll("</lark-message>", "<\\/lark-message>");
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* 渲染 `<quote>` 块。
|
|
502
|
+
* @param quote - 被引消息。
|
|
503
|
+
*/
|
|
504
|
+
function formatQuote(quote) {
|
|
505
|
+
const attributes = [`message_id="${escapeAttribute(quote.messageId)}"`, `from="${quote.from}"`];
|
|
506
|
+
if (quote.time !== "") attributes.push(`time="${escapeAttribute(quote.time)}"`);
|
|
507
|
+
let body = "[引用内容读取失败]";
|
|
508
|
+
if (!quote.unavailable) {
|
|
509
|
+
if (quote.kind === "forwarded") body = `<forwarded_messages message_id="${escapeAttribute(quote.messageId)}"/>`;
|
|
510
|
+
else body = escapeBody(truncateQuote(quote.text));
|
|
511
|
+
}
|
|
512
|
+
return `<quote ${attributes.join(" ")}>\n${body}\n</quote>`;
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* 读取被引消息:user 身份优先,bot 兜底,均失败时返回不可用引用。
|
|
516
|
+
* @param cli - lark-cli 适配层。
|
|
517
|
+
* @param messageId - 被引消息标识。
|
|
518
|
+
* @param timeoutMs - 单次调用超时。
|
|
519
|
+
*/
|
|
520
|
+
async function readQuoted(cli, messageId, timeoutMs = QUOTE_TIMEOUT_MS) {
|
|
521
|
+
for (const identity of IDENTITIES) {
|
|
522
|
+
let value;
|
|
523
|
+
try {
|
|
524
|
+
value = await cli.json([
|
|
525
|
+
"im",
|
|
526
|
+
"+messages-mget",
|
|
527
|
+
"--as",
|
|
528
|
+
identity,
|
|
529
|
+
"--message-ids",
|
|
530
|
+
messageId,
|
|
531
|
+
"--no-reactions"
|
|
532
|
+
], { timeoutMs });
|
|
533
|
+
} catch {
|
|
534
|
+
continue;
|
|
535
|
+
}
|
|
536
|
+
const raw = firstMessage(value);
|
|
537
|
+
if (raw === void 0 || raw["deleted"] === true) continue;
|
|
538
|
+
return toQuotedMessage(messageId, raw);
|
|
539
|
+
}
|
|
540
|
+
return unavailableQuote(messageId);
|
|
541
|
+
}
|
|
542
|
+
//#endregion
|
|
412
543
|
//#region src/bridge.ts
|
|
413
544
|
/**
|
|
414
545
|
* 飞书事件桥:消费 bot 身份事件、过滤与去重,并把消息注入桥接会话。
|
|
415
546
|
* 导出 EventBridge 与 formatLarkMessage。
|
|
416
547
|
*/
|
|
548
|
+
/** 把队列条目拼成日志用的 `id@创建时间/到达时间` 列表。 */
|
|
549
|
+
function describeQueue(items) {
|
|
550
|
+
return items.map((item) => `${item.id}@${formatTime(item.createdAt)}/${formatTime(item.receivedAt)}`).join(",");
|
|
551
|
+
}
|
|
552
|
+
/** 返回事件的消息标识。 */
|
|
553
|
+
function messageId(event) {
|
|
554
|
+
return String(event.message_id ?? event.id ?? "");
|
|
555
|
+
}
|
|
556
|
+
/** 返回事件的被引消息标识;没有引用时为空串。 */
|
|
557
|
+
function replyToId(event) {
|
|
558
|
+
return typeof event.reply_to === "string" ? event.reply_to.trim() : "";
|
|
559
|
+
}
|
|
417
560
|
/** 把一条飞书消息渲染为 `<lark-message>` 块。 */
|
|
418
|
-
function formatLarkMessage(event) {
|
|
561
|
+
function formatLarkMessage(event, quote) {
|
|
419
562
|
const time = formatTime(parseTimestamp(event.timestamp ?? event.create_time));
|
|
420
563
|
const text = typeof event.content === "string" ? event.content.trim() : "";
|
|
421
564
|
const type = String(event.message_type ?? event.msg_type ?? "text");
|
|
422
|
-
const
|
|
423
|
-
const
|
|
565
|
+
const id = escapeAttribute(messageId(event));
|
|
566
|
+
const replyTo = escapeAttribute(replyToId(event));
|
|
567
|
+
const attributes = [`message_id="${id}"`];
|
|
568
|
+
if (replyTo !== "") attributes.push(`reply_to="${replyTo}"`);
|
|
424
569
|
if (time !== "") attributes.push(`time="${time}"`);
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
function messageId(event) {
|
|
430
|
-
return String(event.message_id ?? event.id ?? "");
|
|
570
|
+
let body = text === "" ? `[${type} 消息]` : escapeBlockText(text);
|
|
571
|
+
if (type === "merge_forward") body = `<forwarded_messages message_id="${id}"/>`;
|
|
572
|
+
const inner = quote === void 0 ? body : `${formatQuote(quote)}\n${body}`;
|
|
573
|
+
return `<lark-message ${attributes.join(" ")}>\n${inner}\n</lark-message>`;
|
|
431
574
|
}
|
|
432
575
|
/** 判断事件是否来自目标用户的私聊。 */
|
|
433
576
|
function acceptEvent(event, ownerOpenId) {
|
|
@@ -443,6 +586,9 @@ var EventBridge = class {
|
|
|
443
586
|
backoff = RESTART_MIN_MS;
|
|
444
587
|
startedAt = 0;
|
|
445
588
|
stopped = true;
|
|
589
|
+
queue = [];
|
|
590
|
+
silenceTimer;
|
|
591
|
+
draining = false;
|
|
446
592
|
constructor(deps) {
|
|
447
593
|
this.deps = deps;
|
|
448
594
|
}
|
|
@@ -461,17 +607,23 @@ var EventBridge = class {
|
|
|
461
607
|
this.stopped = true;
|
|
462
608
|
if (this.restartTimer !== void 0) clearTimeout(this.restartTimer);
|
|
463
609
|
this.restartTimer = void 0;
|
|
610
|
+
if (this.silenceTimer !== void 0) clearTimeout(this.silenceTimer);
|
|
611
|
+
this.silenceTimer = void 0;
|
|
612
|
+
if (this.queue.length > 0) {
|
|
613
|
+
this.deps.logger.warn(`lark-bridge: dropped ${this.queue.length} queued message(s) [${describeQueue(this.queue)}] on stop`);
|
|
614
|
+
this.queue.length = 0;
|
|
615
|
+
}
|
|
464
616
|
const child = this.child;
|
|
465
617
|
this.child = void 0;
|
|
466
618
|
if (child !== void 0) killChildTree(child);
|
|
467
619
|
}
|
|
468
|
-
/**
|
|
620
|
+
/** 鉴权或工作区状态变化时同步监听;工作区变化不停止已运行的监听。 */
|
|
469
621
|
sync(snapshot) {
|
|
470
|
-
if (snapshot.ready
|
|
471
|
-
|
|
622
|
+
if (!snapshot.ready) {
|
|
623
|
+
this.stop();
|
|
472
624
|
return;
|
|
473
625
|
}
|
|
474
|
-
this.
|
|
626
|
+
if (!this.running && this.deps.getWorkspaceReady()) this.start();
|
|
475
627
|
}
|
|
476
628
|
/** 启动一个消费者子进程。 */
|
|
477
629
|
spawn() {
|
|
@@ -547,27 +699,74 @@ var EventBridge = class {
|
|
|
547
699
|
}, delay);
|
|
548
700
|
this.restartTimer.unref?.();
|
|
549
701
|
}
|
|
550
|
-
/**
|
|
702
|
+
/** 处理一条事件:过滤、去重、过滤过期消息、读取引用并放入队列。 */
|
|
551
703
|
async handleEvent(event) {
|
|
704
|
+
const id = messageId(event);
|
|
705
|
+
if (id === "") return false;
|
|
552
706
|
const auth = this.deps.getAuth();
|
|
553
|
-
if (auth.ready !== true || !this.deps.getWorkspaceReady()) return false;
|
|
554
707
|
const owner = auth.ownerOpenId;
|
|
555
|
-
if (owner === void 0 || owner === "")
|
|
708
|
+
if (auth.ready !== true || owner === void 0 || owner === "") {
|
|
709
|
+
this.deps.logger.warn(`lark-bridge: dropped ${id}: auth not ready`);
|
|
710
|
+
return false;
|
|
711
|
+
}
|
|
556
712
|
if (!acceptEvent(event, owner)) return false;
|
|
557
|
-
const
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
if (
|
|
561
|
-
|
|
713
|
+
const createdAt = parseTimestamp(event.timestamp ?? event.create_time);
|
|
714
|
+
const receivedAt = Date.now();
|
|
715
|
+
if (!await this.deps.store.markSeen(id, createdAt)) return false;
|
|
716
|
+
if (createdAt < this.startedAt) {
|
|
717
|
+
this.deps.logger.warn(`lark-bridge: filtered ${id} created=${formatTime(createdAt)} received=${formatTime(receivedAt)} listenerStart=${formatTime(this.startedAt)}`);
|
|
718
|
+
return false;
|
|
719
|
+
}
|
|
720
|
+
const quote = await this.readQuote(event);
|
|
721
|
+
this.queue.push({
|
|
722
|
+
id,
|
|
723
|
+
createdAt,
|
|
724
|
+
receivedAt,
|
|
725
|
+
block: formatLarkMessage(event, quote)
|
|
726
|
+
});
|
|
727
|
+
this.schedule();
|
|
728
|
+
return true;
|
|
729
|
+
}
|
|
730
|
+
/** 重置静默计时;窗口内没有新消息时发起注入。 */
|
|
731
|
+
schedule() {
|
|
732
|
+
if (this.silenceTimer !== void 0) clearTimeout(this.silenceTimer);
|
|
733
|
+
const timer = setTimeout(() => {
|
|
734
|
+
this.silenceTimer = void 0;
|
|
735
|
+
this.drain();
|
|
736
|
+
}, INJECT_SILENCE_MS);
|
|
737
|
+
timer.unref?.();
|
|
738
|
+
this.silenceTimer = timer;
|
|
739
|
+
}
|
|
740
|
+
/** 取空队列并按创建时间升序批量注入;失败的批次直接丢弃。 */
|
|
741
|
+
async drain() {
|
|
742
|
+
if (this.draining || this.queue.length === 0) return;
|
|
743
|
+
this.draining = true;
|
|
562
744
|
try {
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
745
|
+
while (this.queue.length > 0) {
|
|
746
|
+
const batch = this.queue.splice(0).sort((a, b) => a.createdAt - b.createdAt);
|
|
747
|
+
const text = batch.map((item) => item.block).join("\n\n");
|
|
748
|
+
const ids = batch.map((item) => `${item.id}@${formatTime(item.createdAt)}/${formatTime(item.receivedAt)}`).join(",");
|
|
749
|
+
try {
|
|
750
|
+
const result = await this.deps.sessions.inject(text);
|
|
751
|
+
this.deps.logger.info(`lark-bridge: delivered ${batch.length} message(s) [${ids}] to session ${result.sessionId} (created=${String(result.created)})`);
|
|
752
|
+
} catch (error) {
|
|
753
|
+
this.deps.logger.warn(`lark-bridge: failed ${batch.length} message(s) [${ids}]: ${describeError(error)}`);
|
|
754
|
+
}
|
|
755
|
+
if (this.silenceTimer !== void 0) return;
|
|
756
|
+
}
|
|
757
|
+
} finally {
|
|
758
|
+
this.draining = false;
|
|
759
|
+
if (this.queue.length > 0 && this.silenceTimer === void 0) this.schedule();
|
|
569
760
|
}
|
|
570
761
|
}
|
|
762
|
+
/** 读取被引消息;没有引用或读取失败时返回降级模型。 */
|
|
763
|
+
async readQuote(event) {
|
|
764
|
+
const replyTo = replyToId(event);
|
|
765
|
+
if (replyTo === "") return void 0;
|
|
766
|
+
const quote = await readQuoted(this.deps.getCli(), replyTo);
|
|
767
|
+
if (quote.unavailable) this.deps.logger.warn(`lark-bridge: cannot read quoted message ${replyTo}`);
|
|
768
|
+
return quote;
|
|
769
|
+
}
|
|
571
770
|
};
|
|
572
771
|
//#endregion
|
|
573
772
|
//#region src/workspace-path.ts
|
|
@@ -609,11 +808,37 @@ function toHostWorkspacePath(value, platform = process.platform) {
|
|
|
609
808
|
//#region src/config.ts
|
|
610
809
|
/** 插件配置:单工作区 schema、路径归一化、严格校验与旧键兼容。 */
|
|
611
810
|
/** 配置 schema;严格键校验由 resolveConfig 完成。 */
|
|
612
|
-
const Config = Schema.object({
|
|
811
|
+
const Config = Schema.object({
|
|
812
|
+
workspaceRoot: Schema.string().default("").volatile(),
|
|
813
|
+
sessionProvider: Schema.string().default("").volatile(),
|
|
814
|
+
sessionModel: Schema.string().default("").volatile(),
|
|
815
|
+
sessionEffort: Schema.string().default("").volatile(),
|
|
816
|
+
sessionPreset: Schema.string().default("").volatile()
|
|
817
|
+
});
|
|
613
818
|
/** 默认配置。 */
|
|
614
|
-
const DEFAULT_CONFIG = Object.freeze({
|
|
819
|
+
const DEFAULT_CONFIG = Object.freeze({
|
|
820
|
+
workspaceRoot: "",
|
|
821
|
+
sessionProvider: "",
|
|
822
|
+
sessionModel: "",
|
|
823
|
+
sessionEffort: "",
|
|
824
|
+
sessionPreset: ""
|
|
825
|
+
});
|
|
615
826
|
/** 合法的当前配置键。 */
|
|
616
|
-
const CONFIG_KEYS = /* @__PURE__ */ new Set([
|
|
827
|
+
const CONFIG_KEYS = /* @__PURE__ */ new Set([
|
|
828
|
+
"workspaceRoot",
|
|
829
|
+
"sessionProvider",
|
|
830
|
+
"sessionModel",
|
|
831
|
+
"sessionEffort",
|
|
832
|
+
"sessionPreset"
|
|
833
|
+
]);
|
|
834
|
+
/** 需要校验为字符串的配置键。 */
|
|
835
|
+
const STRING_KEYS = [
|
|
836
|
+
"workspaceRoot",
|
|
837
|
+
"sessionProvider",
|
|
838
|
+
"sessionModel",
|
|
839
|
+
"sessionEffort",
|
|
840
|
+
"sessionPreset"
|
|
841
|
+
];
|
|
617
842
|
/** 旧版本的配置键:接受并丢弃,避免升级后插件加载失败。 */
|
|
618
843
|
const LEGACY_KEYS = /* @__PURE__ */ new Set([
|
|
619
844
|
"profile",
|
|
@@ -632,9 +857,40 @@ function normalizeConfigInput(raw) {
|
|
|
632
857
|
function resolveConfig(raw) {
|
|
633
858
|
const input = normalizeConfigInput(raw);
|
|
634
859
|
for (const key of Object.keys(input)) if (!CONFIG_KEYS.has(key) && !LEGACY_KEYS.has(key)) throw new Error(`${PLUGIN_LABEL}: unknown config key "${key}"`);
|
|
635
|
-
const
|
|
636
|
-
|
|
637
|
-
|
|
860
|
+
for (const key of STRING_KEYS) {
|
|
861
|
+
const value = input[key];
|
|
862
|
+
if (value !== void 0 && value !== null && typeof value !== "string") throw new Error(`${PLUGIN_LABEL}: config ${key} must be a string`);
|
|
863
|
+
}
|
|
864
|
+
const text = (key) => {
|
|
865
|
+
const value = input[key];
|
|
866
|
+
return typeof value === "string" ? value : DEFAULT_CONFIG[key];
|
|
867
|
+
};
|
|
868
|
+
return Object.freeze({
|
|
869
|
+
workspaceRoot: normalizeWorkspacePath(text("workspaceRoot")),
|
|
870
|
+
sessionProvider: text("sessionProvider"),
|
|
871
|
+
sessionModel: text("sessionModel"),
|
|
872
|
+
sessionEffort: text("sessionEffort"),
|
|
873
|
+
sessionPreset: text("sessionPreset")
|
|
874
|
+
});
|
|
875
|
+
}
|
|
876
|
+
/**
|
|
877
|
+
* 从配置派生新建会话的模型选择。
|
|
878
|
+
* provider 与 model 同时非空才成立;effort 只在模型成立时生效。
|
|
879
|
+
*/
|
|
880
|
+
function modelSelectionOf(config) {
|
|
881
|
+
const provider = config.sessionProvider.trim();
|
|
882
|
+
const model = config.sessionModel.trim();
|
|
883
|
+
if (provider === "" || model === "") return void 0;
|
|
884
|
+
const effort = config.sessionEffort.trim();
|
|
885
|
+
return {
|
|
886
|
+
provider,
|
|
887
|
+
model,
|
|
888
|
+
...effort === "" ? {} : { reasoningEffort: ReasoningEffortId(effort) }
|
|
889
|
+
};
|
|
890
|
+
}
|
|
891
|
+
/** 从配置派生新建会话的预设标识;空白按未配置处理。 */
|
|
892
|
+
function presetOf(config) {
|
|
893
|
+
return config.sessionPreset.trim();
|
|
638
894
|
}
|
|
639
895
|
/** 判断值是否是热重载引用。 */
|
|
640
896
|
function isVolatileRef(value) {
|
|
@@ -672,12 +928,16 @@ function isReminderMessage(message) {
|
|
|
672
928
|
}
|
|
673
929
|
//#endregion
|
|
674
930
|
//#region src/reminder.ts
|
|
675
|
-
/**
|
|
931
|
+
/** 提醒正文;说明块含义、回复规则与引用读取方式。 */
|
|
676
932
|
const BRIDGE_REMINDER = [
|
|
677
933
|
"<lark-message> 块是用户通过飞书远程发送的消息。",
|
|
678
934
|
"用户只能看到 lark_notify 发送的内容,看不到本会话的回复。",
|
|
679
935
|
"- 回复飞书消息时调用 lark_notify,把要发给用户的内容放进 text。",
|
|
680
936
|
"- 用户连续发来多条 <lark-message> 时只回复一次,用一次 lark_notify 覆盖全部内容。",
|
|
937
|
+
"- 填入 reply_to(某条的 message_id)可以让这条通知引用该消息。",
|
|
938
|
+
"- 回复用户时引用用户的消息,以提高对应关系的准确性。",
|
|
939
|
+
"- <quote> 是用户引用的原消息;<forwarded_messages> 只给记录 id,需要内容时用 lark-cli im +messages-mget --as user --message-ids <id> 读取。",
|
|
940
|
+
"- 用户在移动端远程与你通信,回复要简洁,不涉及细节。",
|
|
681
941
|
"- 会话里的 text 块用户看不到,尽量少输出,说明性文字也放进 lark_notify。",
|
|
682
942
|
"- 其他来源的用户消息按普通方式回复,不调用 lark_notify。"
|
|
683
943
|
].join("\n");
|
|
@@ -858,7 +1118,7 @@ function registerRoutes(deps) {
|
|
|
858
1118
|
//#endregion
|
|
859
1119
|
//#region src/sessions.ts
|
|
860
1120
|
/**
|
|
861
|
-
*
|
|
1121
|
+
* 桥接会话:工作区校验、创建或恢复、新建标题、挂载、置顶与状态持久化。
|
|
862
1122
|
* 导出 SessionManager。
|
|
863
1123
|
*/
|
|
864
1124
|
/** 把路径解析为绝对路径。 */
|
|
@@ -949,17 +1209,77 @@ var SessionManager = class {
|
|
|
949
1209
|
return;
|
|
950
1210
|
}
|
|
951
1211
|
}
|
|
952
|
-
/**
|
|
1212
|
+
/** 解析可用的预设;名册缺失、读取失败或候选不可用时返回空串。 */
|
|
1213
|
+
async resolvePreset() {
|
|
1214
|
+
const candidate = presetOf(this.deps.getConfig());
|
|
1215
|
+
if (candidate === "") return "";
|
|
1216
|
+
const presets = this.ctx.get("agentPresets");
|
|
1217
|
+
if (presets === void 0) return "";
|
|
1218
|
+
try {
|
|
1219
|
+
if ((await presets.list()).some((row) => row.id === candidate && row.broken === void 0)) return candidate;
|
|
1220
|
+
this.deps.logger.warn(`lark-bridge: configured preset "${candidate}" is unavailable; using the host default`);
|
|
1221
|
+
return "";
|
|
1222
|
+
} catch (error) {
|
|
1223
|
+
this.deps.logger.warn(`lark-bridge: cannot read agent presets; using the host default: ${describeError(error)}`);
|
|
1224
|
+
return "";
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
/** 校验配置的模型选择;服务缺失或校验失败时返回 undefined。 */
|
|
1228
|
+
async resolveSelection() {
|
|
1229
|
+
const selection = modelSelectionOf(this.deps.getConfig());
|
|
1230
|
+
if (selection === void 0) return void 0;
|
|
1231
|
+
const llm = this.ctx.get("llm");
|
|
1232
|
+
if (llm === void 0) {
|
|
1233
|
+
this.deps.logger.warn("lark-bridge: llm service is unavailable; using the host default model");
|
|
1234
|
+
return;
|
|
1235
|
+
}
|
|
1236
|
+
try {
|
|
1237
|
+
await llm.resolveCallConfig(selection);
|
|
1238
|
+
return selection;
|
|
1239
|
+
} catch (error) {
|
|
1240
|
+
this.deps.logger.warn(`lark-bridge: configured model is unavailable; using the host default: ${describeError(error)}`);
|
|
1241
|
+
return;
|
|
1242
|
+
}
|
|
1243
|
+
}
|
|
1244
|
+
/** 把模型选择写入新会话;失败只记录警告。 */
|
|
1245
|
+
applySelection(agent, selection) {
|
|
1246
|
+
try {
|
|
1247
|
+
agent.model.select(selection);
|
|
1248
|
+
} catch (error) {
|
|
1249
|
+
this.deps.logger.warn(`lark-bridge: cannot apply the model selection: ${describeError(error)}`);
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
/** 新建会话,应用预设与模型,设置标题、挂到工作区并置顶。 */
|
|
953
1253
|
async create(cwd, title, workspace) {
|
|
954
|
-
const
|
|
955
|
-
const
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
1254
|
+
const selection = await this.resolveSelection();
|
|
1255
|
+
const preset = await this.resolvePreset();
|
|
1256
|
+
const controller = this.ctx.get("sessionController");
|
|
1257
|
+
let sessionId = randomUUID();
|
|
1258
|
+
let agent;
|
|
1259
|
+
if (controller?.create !== void 0) {
|
|
1260
|
+
sessionId = (await controller.create({
|
|
1261
|
+
sessionId,
|
|
1262
|
+
workspaceId: String(workspace.id),
|
|
1263
|
+
cwd,
|
|
1264
|
+
...preset === "" ? {} : { agentPreset: preset }
|
|
1265
|
+
})).sessionId;
|
|
1266
|
+
const resolved = await controller.resolveAgent(SessionId(sessionId));
|
|
1267
|
+
if ("error" in resolved) {
|
|
1268
|
+
const live = this.ctx.agents.get(SessionId(sessionId));
|
|
1269
|
+
if (live === void 0) throw resolved.error;
|
|
1270
|
+
this.deps.logger.warn(`lark-bridge: cannot resolve the new session; using the live agent: ${describeError(resolved.error)}`);
|
|
1271
|
+
agent = live;
|
|
1272
|
+
} else agent = resolved.agent;
|
|
1273
|
+
} else {
|
|
1274
|
+
if (preset !== "") this.deps.logger.warn("lark-bridge: sessionController is unavailable; the preset was ignored");
|
|
1275
|
+
agent = (await this.ctx.agents.create({
|
|
1276
|
+
sessionId: SessionId(sessionId),
|
|
1277
|
+
agentOptions: this.agentOptions(),
|
|
1278
|
+
meta: { cwd }
|
|
1279
|
+
})).agent;
|
|
1280
|
+
}
|
|
1281
|
+
if (selection !== void 0) this.applySelection(agent, selection);
|
|
1282
|
+
appendTitle(agent.session, title);
|
|
963
1283
|
await this.attach(sessionId, workspace);
|
|
964
1284
|
await this.registry().pinSession(SessionId(sessionId));
|
|
965
1285
|
await this.deps.store.patch({
|
|
@@ -970,15 +1290,11 @@ var SessionManager = class {
|
|
|
970
1290
|
this.deps.logger.info(`lark-bridge: created session ${sessionId} in ${cwd}`);
|
|
971
1291
|
return {
|
|
972
1292
|
sessionId,
|
|
973
|
-
agent
|
|
1293
|
+
agent
|
|
974
1294
|
};
|
|
975
1295
|
}
|
|
976
|
-
/**
|
|
977
|
-
async
|
|
978
|
-
if (createdTitle !== title) {
|
|
979
|
-
appendTitle(agent.session, title);
|
|
980
|
-
await this.deps.store.patch({ title });
|
|
981
|
-
}
|
|
1296
|
+
/** 置顶会话;失败只记日志。 */
|
|
1297
|
+
async pin(sessionId) {
|
|
982
1298
|
try {
|
|
983
1299
|
await this.registry().pinSession(SessionId(sessionId));
|
|
984
1300
|
} catch (error) {
|
|
@@ -999,7 +1315,7 @@ var SessionManager = class {
|
|
|
999
1315
|
if (state.sessionId !== null && state.workspaceRoot !== null && sameWorkspacePath(state.workspaceRoot, cwd)) {
|
|
1000
1316
|
const agent = await this.resume(state.sessionId);
|
|
1001
1317
|
if (agent !== void 0) {
|
|
1002
|
-
await this.
|
|
1318
|
+
await this.pin(state.sessionId);
|
|
1003
1319
|
return {
|
|
1004
1320
|
sessionId: state.sessionId,
|
|
1005
1321
|
agent,
|
|
@@ -1197,9 +1513,22 @@ var StateStore = class {
|
|
|
1197
1513
|
//#endregion
|
|
1198
1514
|
//#region src/tools.ts
|
|
1199
1515
|
/**
|
|
1200
|
-
* `lark_notify` 工具:以 bot
|
|
1516
|
+
* `lark_notify` 工具:以 bot 身份向已绑定用户发送飞书消息,可选精确回复某条用户消息。
|
|
1201
1517
|
* 导出 NOTIFY_TOOL 与 sendNotification。
|
|
1202
1518
|
*/
|
|
1519
|
+
/** 读取可选的 `reply_to`;类型不对时返回错误。 */
|
|
1520
|
+
function readReplyTo(args) {
|
|
1521
|
+
const value = args["reply_to"];
|
|
1522
|
+
if (value === void 0) return {};
|
|
1523
|
+
if (typeof value !== "string" || value.trim() === "") return { error: {
|
|
1524
|
+
ok: false,
|
|
1525
|
+
error: {
|
|
1526
|
+
code: "invalid-reply-to",
|
|
1527
|
+
message: "reply_to 必须是非空字符串"
|
|
1528
|
+
}
|
|
1529
|
+
} };
|
|
1530
|
+
return { replyTo: value.trim() };
|
|
1531
|
+
}
|
|
1203
1532
|
/**
|
|
1204
1533
|
* 校验参数并发送通知。
|
|
1205
1534
|
* @param deps - 配置、CLI 与用户标识来源。
|
|
@@ -1214,7 +1543,8 @@ async function sendNotification(deps, args) {
|
|
|
1214
1543
|
message: "参数必须是对象"
|
|
1215
1544
|
}
|
|
1216
1545
|
};
|
|
1217
|
-
const
|
|
1546
|
+
const record = args;
|
|
1547
|
+
const text = record["text"];
|
|
1218
1548
|
if (typeof text !== "string" || text.trim() === "") return {
|
|
1219
1549
|
ok: false,
|
|
1220
1550
|
error: {
|
|
@@ -1222,6 +1552,8 @@ async function sendNotification(deps, args) {
|
|
|
1222
1552
|
message: "text 必须是非空字符串"
|
|
1223
1553
|
}
|
|
1224
1554
|
};
|
|
1555
|
+
const reply = readReplyTo(record);
|
|
1556
|
+
if (reply.error !== void 0) return reply.error;
|
|
1225
1557
|
const owner = deps.getOwner();
|
|
1226
1558
|
if (owner === void 0 || owner === "") return {
|
|
1227
1559
|
ok: false,
|
|
@@ -1233,46 +1565,57 @@ async function sendNotification(deps, args) {
|
|
|
1233
1565
|
const parts = splitText(text, MAX_MESSAGE_CHARS);
|
|
1234
1566
|
const baseKey = `lark-notify-${randomUUID()}`;
|
|
1235
1567
|
let first;
|
|
1236
|
-
for (const [index, part] of parts.entries())
|
|
1237
|
-
const
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1568
|
+
for (const [index, part] of parts.entries()) {
|
|
1569
|
+
const replyTo = index === 0 ? reply.replyTo : void 0;
|
|
1570
|
+
const target = replyTo === void 0 ? ["--user-id", owner] : ["--message-id", replyTo];
|
|
1571
|
+
try {
|
|
1572
|
+
const value = await deps.getCli().json([
|
|
1573
|
+
"im",
|
|
1574
|
+
replyTo === void 0 ? "+messages-send" : "+messages-reply",
|
|
1575
|
+
"--as",
|
|
1576
|
+
"bot",
|
|
1577
|
+
...target,
|
|
1578
|
+
"--text",
|
|
1579
|
+
"-",
|
|
1580
|
+
"--idempotency-key",
|
|
1581
|
+
`${baseKey}-${index}`.slice(0, 50)
|
|
1582
|
+
], { input: part });
|
|
1583
|
+
const record = typeof value === "object" && value !== null ? value : {};
|
|
1584
|
+
first ??= typeof record["message_id"] === "string" ? record["message_id"] : void 0;
|
|
1585
|
+
} catch (error) {
|
|
1586
|
+
return {
|
|
1587
|
+
ok: false,
|
|
1588
|
+
error: {
|
|
1589
|
+
code: replyTo === void 0 ? "send-failed" : "reply-failed",
|
|
1590
|
+
message: describeError(error)
|
|
1591
|
+
}
|
|
1592
|
+
};
|
|
1593
|
+
}
|
|
1259
1594
|
}
|
|
1260
1595
|
return {
|
|
1261
1596
|
ok: true,
|
|
1262
1597
|
parts: parts.length,
|
|
1263
|
-
...first === void 0 ? {} : { messageId: first }
|
|
1598
|
+
...first === void 0 ? {} : { messageId: first },
|
|
1599
|
+
...reply.replyTo === void 0 ? {} : { replyTo: reply.replyTo }
|
|
1264
1600
|
};
|
|
1265
1601
|
}
|
|
1266
1602
|
/** 构造工具定义。 */
|
|
1267
1603
|
function buildNotifyTool(deps) {
|
|
1268
1604
|
return {
|
|
1269
1605
|
name: NOTIFY_TOOL_NAME,
|
|
1270
|
-
description: "
|
|
1271
|
-
parameters: {
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1606
|
+
description: "以飞书机器人身份给已绑定的用户发送消息。只发往用户与机器人的私聊,不接受其他接收目标。可选 reply_to 精确回复某条用户消息。",
|
|
1607
|
+
parameters: {
|
|
1608
|
+
text: {
|
|
1609
|
+
type: "string",
|
|
1610
|
+
description: "要发送给用户的文本;过长时插件自动按顺序拆分",
|
|
1611
|
+
required: true
|
|
1612
|
+
},
|
|
1613
|
+
reply_to: {
|
|
1614
|
+
type: "string",
|
|
1615
|
+
description: "要回复的用户消息 message_id;省略时直接发送",
|
|
1616
|
+
required: false
|
|
1617
|
+
}
|
|
1618
|
+
},
|
|
1276
1619
|
output: {
|
|
1277
1620
|
schema: {
|
|
1278
1621
|
type: "object",
|
|
@@ -1280,6 +1623,7 @@ function buildNotifyTool(deps) {
|
|
|
1280
1623
|
ok: { type: "boolean" },
|
|
1281
1624
|
parts: { type: "integer" },
|
|
1282
1625
|
messageId: { type: "string" },
|
|
1626
|
+
replyTo: { type: "string" },
|
|
1283
1627
|
error: {
|
|
1284
1628
|
type: "object",
|
|
1285
1629
|
properties: {
|
|
@@ -1295,7 +1639,7 @@ function buildNotifyTool(deps) {
|
|
|
1295
1639
|
const result = value;
|
|
1296
1640
|
if (result.ok) return [{
|
|
1297
1641
|
type: "text",
|
|
1298
|
-
text:
|
|
1642
|
+
text: `${result.replyTo === void 0 ? "已发送给用户" : "已回复给用户"}(${result.parts ?? 1} 条)`
|
|
1299
1643
|
}];
|
|
1300
1644
|
const error = result.error ?? {
|
|
1301
1645
|
code: "unknown",
|
|
@@ -1480,14 +1824,12 @@ function apply(ctx, config) {
|
|
|
1480
1824
|
runCheck();
|
|
1481
1825
|
ctx.on("domain/changed", (change) => {
|
|
1482
1826
|
if (!selection.invalidate(change)) return;
|
|
1483
|
-
bridge.stop();
|
|
1484
1827
|
reconcile();
|
|
1485
1828
|
});
|
|
1486
1829
|
ctx.on("loader/volatile-update", () => {
|
|
1487
1830
|
try {
|
|
1488
1831
|
current = readConfig(config);
|
|
1489
1832
|
selection.reset();
|
|
1490
|
-
bridge.stop();
|
|
1491
1833
|
reconcile();
|
|
1492
1834
|
ctx.logger.info(`[${PLUGIN_LABEL}] config updated`);
|
|
1493
1835
|
runCheck();
|