aai-lark-bridge 0.2.33 → 0.2.35

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/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.33",
7
+ version: "0.2.35",
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",
@@ -7244,12 +7244,10 @@ function createClaudeSeat(opts) {
7244
7244
  if (!c) return { status: await startLogin(openId) };
7245
7245
  const recent = now() - (refreshedAt.get(openId) ?? -Infinity) < REFRESH_COOLDOWN_MS && c.expiresAt - now() >= MIN_AFTER_REFRESH_MS;
7246
7246
  if (c.expiresAt - now() < MIN_TOKEN_LEFT_MS && !recent) {
7247
- const before = c.expiresAt;
7248
7247
  const ok = await opts.cli.refresh(dir).catch(() => false);
7249
7248
  c = readCredentials(dir);
7250
7249
  const left = c ? c.expiresAt - now() : 0;
7251
- const fresh = Boolean(c) && (left >= MIN_TOKEN_LEFT_MS || c.expiresAt > before && left >= MIN_AFTER_REFRESH_MS);
7252
- if (!ok || !c || !fresh) {
7250
+ if (!ok || !c || left < MIN_AFTER_REFRESH_MS) {
7253
7251
  log.warn("user-gateway", "claude.refresh-failed", { sender: tail62(openId), proof, runId });
7254
7252
  return { status: "refresh_failed" };
7255
7253
  }
@@ -14767,6 +14765,10 @@ var PendingQueue = class {
14767
14765
  });
14768
14766
  return 1;
14769
14767
  }
14768
+ /** 有消息在排队、或正在 flush / 运行(被 block)的 scope:还没占上 ActiveRuns 的也算。 */
14769
+ scopes() {
14770
+ return [.../* @__PURE__ */ new Set([...this.map.keys(), ...this.blocked])];
14771
+ }
14770
14772
  cancel(scope) {
14771
14773
  const entry = this.map.get(scope);
14772
14774
  if (!entry) return [];
@@ -15022,6 +15024,33 @@ async function observeWorkerRun(i) {
15022
15024
  }
15023
15025
  return report;
15024
15026
  }
15027
+ var ReportInbox = class {
15028
+ constructor(max = 10, ttlMs = 24 * 60 * 60 * 1e3, now = Date.now) {
15029
+ this.max = max;
15030
+ this.ttlMs = ttlMs;
15031
+ this.now = now;
15032
+ }
15033
+ max;
15034
+ ttlMs;
15035
+ now;
15036
+ byChat = /* @__PURE__ */ new Map();
15037
+ add(chatId, text) {
15038
+ const list = this.byChat.get(chatId) ?? [];
15039
+ list.push({ text, at: this.now() });
15040
+ while (list.length > this.max) list.shift();
15041
+ this.byChat.set(chatId, list);
15042
+ }
15043
+ /** 取走这个私聊攒下的回流(过了 TTL 的丢掉)。 */
15044
+ take(chatId) {
15045
+ const list = this.byChat.get(chatId) ?? [];
15046
+ this.byChat.delete(chatId);
15047
+ const cutoff = this.now() - this.ttlMs;
15048
+ return list.filter((r) => r.at >= cutoff).map((r) => r.text);
15049
+ }
15050
+ };
15051
+ function reportsInstruction(reports) {
15052
+ return "\u4F60\u4E4B\u524D\u6D3E\u5230\u8BDD\u9898\u91CC\u7684\u4EFB\u52A1\u6709\u65B0\u7684\u7EC8\u6001\uFF0C\u4E0B\u9762\u662F\u7CFB\u7EDF\u56DE\u6D41\uFF0C\u53EA\u7528\u6765\u56DE\u6EAF\uFF0C\u4E0D\u662F\u7528\u6237\u8BF4\u7684\u8BDD\uFF0C\u4E0D\u8981\u4E3A\u5B83\u4EEC\u5355\u72EC\u56DE\u590D\u7528\u6237\uFF1B\u7528\u6237\u540E\u9762\u95EE\u8D77\u65F6\u51ED trace \u8BA4\u662F\u54EA\u4E2A\u8BDD\u9898\u3002\n\n" + reports.join("\n\n");
15053
+ }
15025
15054
 
15026
15055
  // src/bridge/bot/quote.ts
15027
15056
  import { normalize } from "@larksuite/channel";
@@ -15833,6 +15862,7 @@ async function startChannel(deps) {
15833
15862
  const channel = createLarkChannel(opts);
15834
15863
  installStreamResilience(channel);
15835
15864
  const media = new MediaCache(channel, deps.appPaths?.mediaDir);
15865
+ const reportInbox = new ReportInbox();
15836
15866
  const pending = new PendingQueue(DEBOUNCE_MS, (scope, batch, arrival) => {
15837
15867
  const firstMsg = batch[0];
15838
15868
  if (!firstMsg) return;
@@ -15866,8 +15896,9 @@ async function startChannel(deps) {
15866
15896
  batch,
15867
15897
  arrival,
15868
15898
  pushToRouter: (m) => {
15869
- pending.push(m.chatId, m);
15899
+ reportInbox.add(m.chatId, m.content);
15870
15900
  },
15901
+ reportInbox,
15871
15902
  controls,
15872
15903
  cotClient,
15873
15904
  callbackAuth,
@@ -16047,7 +16078,7 @@ async function startChannel(deps) {
16047
16078
  const spawnInbox = spawnDir ? startSpawnInbox({
16048
16079
  dir: spawnDir,
16049
16080
  recent: recentDm,
16050
- activeScopes: () => activeRuns.scopes(),
16081
+ activeScopes: () => [.../* @__PURE__ */ new Set([...activeRuns.scopes(), ...pending.scopes()])],
16051
16082
  limit: MAX_DM_WORKERS,
16052
16083
  isRoot: (id) => isDispatchRoot(spawnDir, id),
16053
16084
  register: (id, chatId) => registerDispatchRoot(spawnDir, id, chatId),
@@ -16307,7 +16338,8 @@ async function intakeMessage(deps) {
16307
16338
  log.info("intake", "command", { scope, droppedPending: dropped.length });
16308
16339
  return;
16309
16340
  }
16310
- if (workerScope && !activeRuns.get(scope) && runningWorkers(activeRuns.scopes(), emsg.chatId) >= MAX_DM_WORKERS) {
16341
+ const inFlightScopes = [.../* @__PURE__ */ new Set([...activeRuns.scopes(), ...pending.scopes()])];
16342
+ if (workerScope && !inFlightScopes.includes(scope) && runningWorkers(inFlightScopes, emsg.chatId) >= MAX_DM_WORKERS) {
16311
16343
  log.info("intake", "worker-limit", { scope, limit: MAX_DM_WORKERS });
16312
16344
  await channel.send(emsg.chatId, { markdown: `\u4F60\u540C\u65F6\u5728\u8DD1\u7684\u4EFB\u52A1\u5DF2\u7ECF\u6709 ${MAX_DM_WORKERS} \u4E2A\uFF0C\u7B49\u5176\u4E2D\u4E00\u4E2A\u7ED3\u675F\u540E\u518D\u53D1\u8FD9\u6761\u3002` }, { replyTo: emsg.messageId }).catch((err) => log.warn("intake", "worker-limit-reply-failed", { err: String(err) }));
16313
16345
  return;
@@ -16415,6 +16447,7 @@ async function runAgentBatchInner(deps) {
16415
16447
  batch,
16416
16448
  arrival,
16417
16449
  pushToRouter,
16450
+ reportInbox,
16418
16451
  controls,
16419
16452
  cotClient,
16420
16453
  callbackAuth,
@@ -16488,11 +16521,13 @@ async function runAgentBatchInner(deps) {
16488
16521
  const prevModel = lastRunModelByScope.get(scope);
16489
16522
  const modelSwitched = prevModel !== void 0 && prevModel !== modelSelection;
16490
16523
  lastRunModelByScope.set(scope, modelSelection);
16524
+ const reportsForRouter = firstMsg.chatType === "p2p" && scope === chatId && !binding ? reportInbox?.take(chatId) ?? [] : [];
16491
16525
  const extraInstructions = [
16492
16526
  ...modelSwitched ? [
16493
16527
  `\u7528\u6237\u521A\u628A\u672C\u4F1A\u8BDD\u4F7F\u7528\u7684\u6A21\u578B\u5207\u6362\u4E3A\u300C${modelLabel(agentKind, modelPref)}\u300D\u3002\u4E4B\u524D\u7684\u5BF9\u8BDD\u91CC\u53EF\u80FD\u63D0\u5230\u522B\u7684\u6A21\u578B,\u8BF7\u4EE5\u5F53\u524D\u6A21\u578B\u4E3A\u51C6;\u82E5\u88AB\u95EE\u5230\u4F60\u7528\u7684\u662F\u4EC0\u4E48\u6A21\u578B,\u636E\u6B64\u56DE\u7B54\u3002`
16494
16528
  ] : [],
16495
- ...deps.userRead ? [deps.userRead.instruction] : []
16529
+ ...deps.userRead ? [deps.userRead.instruction] : [],
16530
+ ...reportsForRouter.length > 0 ? [reportsInstruction(reportsForRouter)] : []
16496
16531
  ];
16497
16532
  const prompt = buildPrompt(
16498
16533
  batch,
@@ -16516,11 +16551,12 @@ async function runAgentBatchInner(deps) {
16516
16551
  replyInThread: sendOpts.replyInThread === true
16517
16552
  });
16518
16553
  const accessDecision = firstMsg.chatType === "p2p" ? canUseDm(controls.profileConfig, controls, firstMsg.senderId) : canUseGroup(controls.profileConfig, controls, firstMsg.chatId, firstMsg.senderId);
16554
+ const workerScope = firstMsg.chatType === "p2p" && scope.startsWith(`${chatId}:`);
16519
16555
  const scopeContext = {
16520
16556
  source: "im",
16521
16557
  chatId,
16522
16558
  actorId: firstMsg.senderId,
16523
- ...threadId ? { threadId } : {}
16559
+ ...threadId && !workerScope ? { threadId } : {}
16524
16560
  };
16525
16561
  const capability = controls.profileConfig.agentKind === "codex" ? codexCapability(controls.profileConfig) : claudeCapability(controls.profileConfig);
16526
16562
  const flow = await startRunFlow({