aai-lark-bridge 0.2.41 → 0.2.43

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 CHANGED
@@ -49,7 +49,7 @@ lark-cli --version # 见下方说明
49
49
 
50
50
  ### SDLC 文档评论确认(可选)
51
51
 
52
- `preferences.sdlcConfirmationWorkspace` 可设为 SDLC 工作区绝对路径,默认关闭。普通文档评论先保存最小线索,再唤醒固定只读核验;事件正文不作为授权。需同时安装并启用 SDLC 评论核验,部署后实测事件覆盖;配置与暂停方式见仓库 `plugins/aai-sdlc/agent/README.md`。
52
+ `preferences.sdlcConfirmationWorkspace` 可设为 SDLC 工作区绝对路径,默认关闭。普通文档评论先保存最小线索,再唤醒固定只读核验;事件正文不作为授权。需同时安装并启用 SDLC 评论核验,部署后实测事件覆盖;配置与暂停方式见仓库 `services/sdlc-agent/README.md`。
53
53
 
54
54
  ### 第 2 步:安装
55
55
 
package/dist/cli.js CHANGED
@@ -4,7 +4,7 @@ import { Command } from "commander";
4
4
  // package.json
5
5
  var package_default = {
6
6
  name: "aai-lark-bridge",
7
- version: "0.2.41",
7
+ version: "0.2.43",
8
8
  description: "Lark AI Native Bridge - Lark\u57FA\u7840\u8BBE\u65BD\u5C42\u548CCLI\u96C6\u6210",
9
9
  type: "module",
10
10
  main: "./dist/index.js",
@@ -15332,6 +15332,73 @@ async function removeReaction(channel, messageId, reactionId) {
15332
15332
  }
15333
15333
  }
15334
15334
 
15335
+ // src/bridge/bot/reaction-confirm.ts
15336
+ var POSITIVE_EMOJIS = /* @__PURE__ */ new Set(["OK", "THUMBSUP", "DONE", "JIAYI", "LGTM"]);
15337
+ var CONFIRM_WINDOW_MS = 45 * 6e4;
15338
+ var SCOPE_MAX = 500;
15339
+ function isConfirmQuestion(text) {
15340
+ const tail = text.trim().split(/[。!!\n]/).map((s) => s.trim()).filter(Boolean).pop() ?? "";
15341
+ return /[??]$/.test(tail) && /吗|是否|要不要|可不可以|对不对|行不行|可以不/.test(tail);
15342
+ }
15343
+ var ReactionConfirmer = class {
15344
+ constructor(now = Date.now) {
15345
+ this.now = now;
15346
+ }
15347
+ now;
15348
+ byUserMsg = /* @__PURE__ */ new Map();
15349
+ latestByScope = /* @__PURE__ */ new Map();
15350
+ used = /* @__PURE__ */ new Set();
15351
+ /** 一批用户消息开始跑时登记:该 scope 里 bot 之后的回复,都是对这条消息的回复。 */
15352
+ noteHandled(scope, msg) {
15353
+ const prev = this.latestByScope.get(scope);
15354
+ if (prev) this.byUserMsg.delete(prev);
15355
+ this.byUserMsg.set(msg.messageId, { scope, msg });
15356
+ this.latestByScope.set(scope, msg.messageId);
15357
+ while (this.latestByScope.size > SCOPE_MAX) {
15358
+ const oldest = this.latestByScope.keys().next().value;
15359
+ const id = this.latestByScope.get(oldest);
15360
+ this.latestByScope.delete(oldest);
15361
+ if (id) this.byUserMsg.delete(id);
15362
+ }
15363
+ }
15364
+ async handle(evt, channel) {
15365
+ const reject4 = (reason) => {
15366
+ log.info("reaction-confirm", "ignored", { reason, messageId: evt.messageId });
15367
+ return void 0;
15368
+ };
15369
+ if (evt.action !== "added") return reject4("not-added");
15370
+ if (!POSITIVE_EMOJIS.has(evt.emojiType)) return reject4("not-positive");
15371
+ if (this.used.has(evt.messageId)) return reject4("already-used");
15372
+ let bot;
15373
+ try {
15374
+ bot = await channel.fetchMessage(evt.messageId);
15375
+ } catch (err) {
15376
+ log.warn("reaction-confirm", "fetch-failed", { messageId: evt.messageId, err: String(err) });
15377
+ return void 0;
15378
+ }
15379
+ if (!bot) return reject4("message-not-found");
15380
+ if (bot.senderIsBot !== true) return reject4("not-bot-message");
15381
+ const parentId = bot.replyToMessageId;
15382
+ const handled = parentId ? this.byUserMsg.get(parentId) : void 0;
15383
+ if (!handled || !parentId) return reject4("not-reply-to-handled-message");
15384
+ if (this.latestByScope.get(handled.scope) !== parentId) return reject4("not-latest");
15385
+ if (handled.msg.chatId !== bot.chatId) return reject4("chat-mismatch");
15386
+ if (evt.operator.openId !== handled.msg.senderId) return reject4("not-asker");
15387
+ if (!isConfirmQuestion(bot.content)) return reject4("not-a-question");
15388
+ const t = this.now();
15389
+ if (t - handled.msg.createTime > CONFIRM_WINDOW_MS) return reject4("expired");
15390
+ this.used.add(evt.messageId);
15391
+ if (this.used.size > 1e3) this.used.delete(this.used.values().next().value);
15392
+ const msg = {
15393
+ ...handled.msg,
15394
+ content: `\u53EF\u4EE5\uFF08\u7528\u6237\u70B9\u4E86 ${evt.emojiType} \u8868\u60C5\u786E\u8BA4\uFF09`,
15395
+ resources: [],
15396
+ createTime: t
15397
+ };
15398
+ return { scope: handled.scope, msg };
15399
+ }
15400
+ };
15401
+
15335
15402
  // src/bridge/bot/channel-registry.ts
15336
15403
  var active2 = /* @__PURE__ */ new Map();
15337
15404
  function registerChannel(profile2) {
@@ -15891,6 +15958,7 @@ async function startChannel(deps) {
15891
15958
  }) : void 0;
15892
15959
  const activePolicyFingerprints = /* @__PURE__ */ new Map();
15893
15960
  const lastRunModelByScope = /* @__PURE__ */ new Map();
15961
+ const reactionConfirmer = new ReactionConfirmer();
15894
15962
  const cotClient = new CotClient({
15895
15963
  tenant: cfg.accounts.app.tenant,
15896
15964
  appId: cfg.accounts.app.id,
@@ -15989,6 +16057,7 @@ async function startChannel(deps) {
15989
16057
  callbackAuth,
15990
16058
  activePolicyFingerprints,
15991
16059
  lastRunModelByScope,
16060
+ reactionConfirmer,
15992
16061
  scope,
15993
16062
  mode
15994
16063
  });
@@ -16033,6 +16102,34 @@ async function startChannel(deps) {
16033
16102
  })
16034
16103
  ).catch((err) => log.fail("intake", err));
16035
16104
  },
16105
+ reaction: async (evt) => {
16106
+ markInbound("reaction");
16107
+ const hit = await reactionConfirmer.handle(evt, channel).catch((err) => {
16108
+ log.fail("reaction-confirm", err);
16109
+ return void 0;
16110
+ });
16111
+ if (!hit) return;
16112
+ log.info("reaction-confirm", "accepted", { scope: hit.scope, emoji: evt.emojiType, msgId: hit.msg.messageId });
16113
+ await withTrace(
16114
+ { chatId: hit.msg.chatId, msgId: hit.msg.messageId },
16115
+ () => intakeMessage({
16116
+ channel,
16117
+ agent,
16118
+ sessions: sessions2,
16119
+ sessionCatalog,
16120
+ workspaces,
16121
+ activeRuns,
16122
+ pending,
16123
+ recent: recentDm,
16124
+ msg: hit.msg,
16125
+ controls,
16126
+ chatModeCache,
16127
+ logThreadModeOverride,
16128
+ executor,
16129
+ pool
16130
+ })
16131
+ ).catch((err) => log.fail("intake", err));
16132
+ },
16036
16133
  reject: (evt) => {
16037
16134
  markInbound("reject");
16038
16135
  log.info("intake", "reject", { chatId: evt.chatId, reason: evt.reason });
@@ -16539,6 +16636,7 @@ async function runAgentBatchInner(deps) {
16539
16636
  callbackAuth,
16540
16637
  activePolicyFingerprints,
16541
16638
  lastRunModelByScope,
16639
+ reactionConfirmer,
16542
16640
  scope,
16543
16641
  mode
16544
16642
  } = deps;
@@ -16546,6 +16644,7 @@ async function runAgentBatchInner(deps) {
16546
16644
  const firstMsg = batch[0];
16547
16645
  const lastMsg = batch[batch.length - 1];
16548
16646
  if (!firstMsg || !lastMsg) return;
16647
+ reactionConfirmer.noteHandled(scope, lastMsg);
16549
16648
  const chatId = firstMsg.chatId;
16550
16649
  const threadId = firstMsg.threadId;
16551
16650
  const bindingResult = deps.preBinding ? deps.preBinding.result : firstMsg.chatType === "p2p" ? dmBinding(controls, firstMsg, scope) : void 0;
@@ -20519,6 +20618,18 @@ function createGateway(opts) {
20519
20618
  const now = opts.now ?? Date.now;
20520
20619
  const sleep3 = opts.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms).unref()));
20521
20620
  const handles = /* @__PURE__ */ new Map();
20621
+ const gone = /* @__PURE__ */ new Map();
20622
+ const GONE_MAX = 500;
20623
+ function forget(handle2, reason, runId, at = now()) {
20624
+ handles.delete(handle2);
20625
+ gone.set(handle2, { reason, runId, at });
20626
+ if (gone.size > GONE_MAX) gone.delete(gone.keys().next().value);
20627
+ }
20628
+ function noteExpired(event, api, handle2) {
20629
+ const g = handle2 ? gone.get(handle2) : void 0;
20630
+ const reason = !handle2 ? "no_handle" : g ? g.reason : "unknown";
20631
+ log.warn("user-gateway", event, { api, reason, runId: g?.runId, agoSec: g ? Math.round((now() - g.at) / 1e3) : void 0 });
20632
+ }
20522
20633
  const used = /* @__PURE__ */ new Map();
20523
20634
  const issuedFor = /* @__PURE__ */ new Map();
20524
20635
  const inFlight = /* @__PURE__ */ new Set();
@@ -20615,7 +20726,7 @@ function createGateway(opts) {
20615
20726
  let expired = false;
20616
20727
  for (const [h, v] of handles)
20617
20728
  if (v.expiresAt <= t) {
20618
- handles.delete(h);
20729
+ forget(h, "ttl", v.runId, v.expiresAt);
20619
20730
  expired = true;
20620
20731
  }
20621
20732
  if (expired) void cleanupTmp();
@@ -20827,7 +20938,7 @@ function createGateway(opts) {
20827
20938
  const e = larkError(r);
20828
20939
  if (!r.timedOut && e.type === "authentication") {
20829
20940
  await rm14(authorizedMark(h.openId), { force: true });
20830
- for (const [k, v] of handles) if (v.openId === h.openId) handles.delete(k);
20941
+ for (const [k, v] of handles) if (v.openId === h.openId) forget(k, "auth", v.runId);
20831
20942
  try {
20832
20943
  await startAuth(h.openId);
20833
20944
  } catch (err) {
@@ -20950,12 +21061,16 @@ function createGateway(opts) {
20950
21061
  const h = issue(true);
20951
21062
  return { status: "ok", handle: h.handle, expiresAt: h.expiresAt, db: db.enabled };
20952
21063
  },
21064
+ /** 请求没带句柄就来读或查库:记一条带原因的 expired(server 调)。 */
21065
+ noteMissingHandle(event, api) {
21066
+ noteExpired(event, api, "");
21067
+ },
20953
21068
  /** DELETE /handles/{runId}:运行结束,作废这次运行的句柄。 */
20954
21069
  dropHandles(runId) {
20955
21070
  let n = 0;
20956
21071
  for (const [h, v] of handles) {
20957
21072
  if (v.runId === runId) {
20958
- handles.delete(h);
21073
+ forget(h, "run_end", v.runId);
20959
21074
  n++;
20960
21075
  }
20961
21076
  }
@@ -20972,6 +21087,7 @@ function createGateway(opts) {
20972
21087
  async read(handle2, api, input) {
20973
21088
  sweep();
20974
21089
  const h = handles.get(handle2);
21090
+ if (!h) noteExpired("read.expired", api, handle2);
20975
21091
  if (!h || h.kind !== "dm") return { status: "expired" };
20976
21092
  if (!h.read) {
20977
21093
  log.info("user-gateway", "read.need-auth", { identity: "user", sender: tail6(h.openId), messageId: h.messageId, runId: h.runId, api, detail: "handle-without-read" });
@@ -21007,7 +21123,7 @@ function createGateway(opts) {
21007
21123
  const v = await verify(messageId, "revoke");
21008
21124
  if (typeof v === "string") return { status: v };
21009
21125
  if (v.text?.trim() !== REVOKE_TEXT2) return { status: "not_revoke" };
21010
- for (const [h, x] of handles) if (x.openId === v.openId) handles.delete(h);
21126
+ for (const [h, x] of handles) if (x.openId === v.openId) forget(h, "revoke", x.runId);
21011
21127
  const had = existsSync10(userDir(v.openId));
21012
21128
  await rm14(userDir(v.openId), { recursive: true, force: true });
21013
21129
  bound2.delete(userDir(v.openId));
@@ -21147,7 +21263,10 @@ function createGateway(opts) {
21147
21263
  if (!db.enabled) return { status: "off" };
21148
21264
  sweep();
21149
21265
  const h = handles.get(handle2);
21150
- if (!h) return { status: "expired" };
21266
+ if (!h) {
21267
+ noteExpired("db.expired", "db-query", handle2);
21268
+ return { status: "expired" };
21269
+ }
21151
21270
  const name = typeof input.db === "string" && input.db ? input.db : "lotus";
21152
21271
  const sql = typeof input.sql === "string" ? input.sql : "";
21153
21272
  const fields = { sender: tail6(h.openId), runId: h.runId, kind: h.kind, db: name, sql: sql.slice(0, SQL_LOG_MAX) };
@@ -21246,7 +21365,10 @@ function createGatewayServer(opts) {
21246
21365
  };
21247
21366
  if (method === "POST" && path === DB_QUERY) {
21248
21367
  const handle2 = bearer();
21249
- if (!handle2) return send2(res, 200, { status: "expired" });
21368
+ if (!handle2) {
21369
+ gateway.noteMissingHandle("db.expired", "db-query");
21370
+ return send2(res, 200, { status: "expired" });
21371
+ }
21250
21372
  return send2(res, 200, await gateway.dbQuery(handle2, await readBody(req)));
21251
21373
  }
21252
21374
  const read = method === "POST" ? /^\/read\/([a-z-]+)$/.exec(path) : null;
@@ -21254,7 +21376,10 @@ function createGatewayServer(opts) {
21254
21376
  const api = read[1] ?? "";
21255
21377
  if (!isReadApi(api)) return send2(res, 404, { status: "unknown_api" });
21256
21378
  const handle2 = bearer();
21257
- if (!handle2) return send2(res, 200, { status: "expired" });
21379
+ if (!handle2) {
21380
+ gateway.noteMissingHandle("read.expired", api);
21381
+ return send2(res, 200, { status: "expired" });
21382
+ }
21258
21383
  return send2(res, 200, await gateway.read(handle2, api, await readBody(req)));
21259
21384
  }
21260
21385
  return send2(res, 404, { status: "not_found" });