dsh-agent-toolkit 0.2.7 → 0.2.8

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 CHANGED
@@ -1835,6 +1835,15 @@ function createFeishuApi(client) {
1835
1835
  data,
1836
1836
  mediaType
1837
1837
  };
1838
+ },
1839
+ async getBotOpenId() {
1840
+ const res = await client.request({
1841
+ method: "GET",
1842
+ url: "https://open.feishu.cn/open-apis/bot/v3/info"
1843
+ });
1844
+ const openId = res.bot?.open_id;
1845
+ if (typeof openId !== "string" || openId.length === 0) throw new Error(`获取机器人信息失败:code=${res.code} msg=${res.msg}`);
1846
+ return openId;
1838
1847
  }
1839
1848
  };
1840
1849
  }
@@ -1888,8 +1897,10 @@ function parsePostContent(content) {
1888
1897
  * SDK handler 收到的 data 即事件体(README 示例 `data.message` 直接解构);
1889
1898
  * 兼容包一层 { event } 的形态。过滤:机器人消息、非 text/post/image 类型、
1890
1899
  * 群内未 @机器人、无文本且无图片。
1900
+ * 群消息 @ 校验身份:mention 的 open_id 必须等于本机器人(应用若持全部群消息权限,
1901
+ * 会收到 @ 其他机器人的消息,不能误触发)。
1891
1902
  */
1892
- function parseMessageEvent(data) {
1903
+ function parseMessageEvent(data, botOpenId) {
1893
1904
  const wrapped = data;
1894
1905
  const event = wrapped.event ?? wrapped;
1895
1906
  if (event.sender?.sender_type !== "user") return null;
@@ -1900,7 +1911,7 @@ function parseMessageEvent(data) {
1900
1911
  if (typeof msg.content !== "string") return null;
1901
1912
  if (typeof msg.message_id !== "string" || typeof msg.chat_id !== "string") return null;
1902
1913
  if (msg.chat_type !== "p2p" && msg.chat_type !== "group") return null;
1903
- if (msg.chat_type === "group" && !(msg.mentions ?? []).some((m) => m.mentioned_type === "bot")) return null;
1914
+ if (msg.chat_type === "group" && !(msg.mentions ?? []).some((m) => m.mentioned_type === "bot" && m.id?.open_id === botOpenId)) return null;
1904
1915
  let text = "";
1905
1916
  let imageKeys = [];
1906
1917
  if (msg.message_type === "text") try {
@@ -2353,11 +2364,12 @@ const feishuChannel = {
2353
2364
  appId,
2354
2365
  appSecret: bot.secret
2355
2366
  }));
2367
+ const botOpenId = await api.getBotOpenId();
2356
2368
  const dedup = new MessageDedup();
2357
2369
  const dispatcher = new lark.EventDispatcher({}).register({
2358
2370
  "im.message.message_read_v1": async () => void 0,
2359
2371
  "im.message.receive_v1": async (data) => {
2360
- const parsed = parseMessageEvent(data);
2372
+ const parsed = parseMessageEvent(data, botOpenId);
2361
2373
  if (parsed === null || !dedup.check(parsed.messageId)) return;
2362
2374
  const reply = new FeishuReplyHandle(api, parsed.chatId, tunables, log);
2363
2375
  const loadImages = parsed.imageKeys.length > 0 ? async () => Promise.all(parsed.imageKeys.map(async (key) => api.downloadImage(parsed.messageId, key))) : void 0;
@@ -2696,10 +2708,6 @@ var Router = class {
2696
2708
  this.onWarn(`[project-bot] 会话 ${sessionId} 挂载 workspace 失败:${error instanceof Error ? error.message : String(error)}`);
2697
2709
  }
2698
2710
  }
2699
- /** 有 agentOptions 原样透传;无则回退宿主默认模型(存量 bot 不抛 no provider/model)。 */
2700
- resolveOptions(bot) {
2701
- return bot.agentOptions ?? this.defaultModel();
2702
- }
2703
2711
  /** injectSender 开启时向 hooks.sections 末尾追加 sender 段(主/角色形态通用)。 */
2704
2712
  withSenderSection(hooks, bot, userId) {
2705
2713
  if (!this.injectSender) return hooks;
@@ -2715,7 +2723,7 @@ var Router = class {
2715
2723
  }
2716
2724
  /**
2717
2725
  * 按 bot.agentRef 解析会话组装(agentOptions + 创作期 hooks):
2718
- * - 缺省/指向 main → 主 Agent 形态:bot 自带 persona/tools + 默认模型回退;
2726
+ * - 缺省/指向 main → 主 Agent 形态:bot 自带 persona/tools + 模型(自配 agentOptions 优先,缺省回退宿主默认模型);
2719
2727
  * - 指向角色 → 角色形态:persona 单 section + tools.restrict + role.model;
2720
2728
  * - 指向不存在角色 → warn 并降级为主 Agent 形态。
2721
2729
  */
@@ -2725,7 +2733,7 @@ var Router = class {
2725
2733
  if (role === void 0 || ref === "main") {
2726
2734
  if (role === void 0 && ref !== "main") this.onWarn(`[project-bot] bot "${bot.id}" 的 agentRef "${ref}" 不存在,降级绑定主 Agent`);
2727
2735
  return {
2728
- agentOptions: this.resolveOptions(bot),
2736
+ agentOptions: bot.agentOptions ?? this.defaultModel(),
2729
2737
  hooks: this.withSenderSection(hooksOf(bot), bot, userId)
2730
2738
  };
2731
2739
  }
@@ -2735,7 +2743,7 @@ var Router = class {
2735
2743
  text: role.persona
2736
2744
  }];
2737
2745
  return {
2738
- agentOptions: role.model ?? this.resolveOptions(bot),
2746
+ agentOptions: role.model ?? this.defaultModel(),
2739
2747
  hooks: this.withSenderSection({
2740
2748
  ...sections.length > 0 ? { sections } : {},
2741
2749
  ...role.tools !== void 0 ? { tools: role.tools.allow } : {}