aai-lark-bridge 0.2.40 → 0.2.42

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.40",
7
+ version: "0.2.42",
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",
@@ -20519,6 +20519,18 @@ function createGateway(opts) {
20519
20519
  const now = opts.now ?? Date.now;
20520
20520
  const sleep3 = opts.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms).unref()));
20521
20521
  const handles = /* @__PURE__ */ new Map();
20522
+ const gone = /* @__PURE__ */ new Map();
20523
+ const GONE_MAX = 500;
20524
+ function forget(handle2, reason, runId, at = now()) {
20525
+ handles.delete(handle2);
20526
+ gone.set(handle2, { reason, runId, at });
20527
+ if (gone.size > GONE_MAX) gone.delete(gone.keys().next().value);
20528
+ }
20529
+ function noteExpired(event, api, handle2) {
20530
+ const g = handle2 ? gone.get(handle2) : void 0;
20531
+ const reason = !handle2 ? "no_handle" : g ? g.reason : "unknown";
20532
+ log.warn("user-gateway", event, { api, reason, runId: g?.runId, agoSec: g ? Math.round((now() - g.at) / 1e3) : void 0 });
20533
+ }
20522
20534
  const used = /* @__PURE__ */ new Map();
20523
20535
  const issuedFor = /* @__PURE__ */ new Map();
20524
20536
  const inFlight = /* @__PURE__ */ new Set();
@@ -20615,7 +20627,7 @@ function createGateway(opts) {
20615
20627
  let expired = false;
20616
20628
  for (const [h, v] of handles)
20617
20629
  if (v.expiresAt <= t) {
20618
- handles.delete(h);
20630
+ forget(h, "ttl", v.runId, v.expiresAt);
20619
20631
  expired = true;
20620
20632
  }
20621
20633
  if (expired) void cleanupTmp();
@@ -20827,7 +20839,7 @@ function createGateway(opts) {
20827
20839
  const e = larkError(r);
20828
20840
  if (!r.timedOut && e.type === "authentication") {
20829
20841
  await rm14(authorizedMark(h.openId), { force: true });
20830
- for (const [k, v] of handles) if (v.openId === h.openId) handles.delete(k);
20842
+ for (const [k, v] of handles) if (v.openId === h.openId) forget(k, "auth", v.runId);
20831
20843
  try {
20832
20844
  await startAuth(h.openId);
20833
20845
  } catch (err) {
@@ -20844,7 +20856,8 @@ function createGateway(opts) {
20844
20856
  const started = Date.now();
20845
20857
  const { messageId, fileKey, opts: ropts } = readFileParams(input);
20846
20858
  const dir = tmpDirOf(h.runId);
20847
- const path = join29(dir, fileKey);
20859
+ const outName = `${fileKey}.bin`;
20860
+ let path = join29(dir, outName);
20848
20861
  const fail = (result, extra = {}) => log.warn("user-gateway", "read.failed", { ...fields, result, ...extra });
20849
20862
  if (!existsSync10(path)) {
20850
20863
  if (await tmpBytes() >= (opts.tmpMaxBytes ?? MAX_TMP_BYTES)) {
@@ -20857,7 +20870,7 @@ function createGateway(opts) {
20857
20870
  dl = (async () => {
20858
20871
  await mkdir19(dir, { recursive: true, mode: 448 });
20859
20872
  const r2 = await opts.exec(
20860
- ["im", "+messages-resources-download", "--message-id", messageId, "--file-key", fileKey, "--type", "file", "--as", "user", "--output", fileKey],
20873
+ ["im", "+messages-resources-download", "--message-id", messageId, "--file-key", fileKey, "--type", "file", "--as", "user", "--output", outName],
20861
20874
  env(userDir(h.openId)),
20862
20875
  DOWNLOAD_TIMEOUT_MS,
20863
20876
  dir
@@ -20872,11 +20885,20 @@ function createGateway(opts) {
20872
20885
  return larkFailure(h, fields, r);
20873
20886
  }
20874
20887
  }
20888
+ if (!existsSync10(path)) {
20889
+ try {
20890
+ const hit = (await readdir6(dir)).filter((n) => n.startsWith(fileKey));
20891
+ if (hit.length === 1) path = join29(dir, hit[0]);
20892
+ } catch {
20893
+ }
20894
+ }
20875
20895
  let size;
20876
20896
  try {
20877
20897
  size = (await stat8(path)).size;
20878
20898
  } catch {
20879
- return { status: "expired" };
20899
+ if (![...handles.values()].includes(h)) return { status: "expired" };
20900
+ fail("lark_error", { detail: "\u4E0B\u8F7D\u540E\u627E\u4E0D\u5230\u6587\u4EF6" });
20901
+ return { status: "lark_error", detail: "\u4E0B\u8F7D\u540E\u627E\u4E0D\u5230\u6587\u4EF6" };
20880
20902
  }
20881
20903
  if (size > MAX_FILE_BYTES) {
20882
20904
  await rm14(path, { force: true });
@@ -20940,12 +20962,16 @@ function createGateway(opts) {
20940
20962
  const h = issue(true);
20941
20963
  return { status: "ok", handle: h.handle, expiresAt: h.expiresAt, db: db.enabled };
20942
20964
  },
20965
+ /** 请求没带句柄就来读或查库:记一条带原因的 expired(server 调)。 */
20966
+ noteMissingHandle(event, api) {
20967
+ noteExpired(event, api, "");
20968
+ },
20943
20969
  /** DELETE /handles/{runId}:运行结束,作废这次运行的句柄。 */
20944
20970
  dropHandles(runId) {
20945
20971
  let n = 0;
20946
20972
  for (const [h, v] of handles) {
20947
20973
  if (v.runId === runId) {
20948
- handles.delete(h);
20974
+ forget(h, "run_end", v.runId);
20949
20975
  n++;
20950
20976
  }
20951
20977
  }
@@ -20962,6 +20988,7 @@ function createGateway(opts) {
20962
20988
  async read(handle2, api, input) {
20963
20989
  sweep();
20964
20990
  const h = handles.get(handle2);
20991
+ if (!h) noteExpired("read.expired", api, handle2);
20965
20992
  if (!h || h.kind !== "dm") return { status: "expired" };
20966
20993
  if (!h.read) {
20967
20994
  log.info("user-gateway", "read.need-auth", { identity: "user", sender: tail6(h.openId), messageId: h.messageId, runId: h.runId, api, detail: "handle-without-read" });
@@ -20997,7 +21024,7 @@ function createGateway(opts) {
20997
21024
  const v = await verify(messageId, "revoke");
20998
21025
  if (typeof v === "string") return { status: v };
20999
21026
  if (v.text?.trim() !== REVOKE_TEXT2) return { status: "not_revoke" };
21000
- for (const [h, x] of handles) if (x.openId === v.openId) handles.delete(h);
21027
+ for (const [h, x] of handles) if (x.openId === v.openId) forget(h, "revoke", x.runId);
21001
21028
  const had = existsSync10(userDir(v.openId));
21002
21029
  await rm14(userDir(v.openId), { recursive: true, force: true });
21003
21030
  bound2.delete(userDir(v.openId));
@@ -21137,7 +21164,10 @@ function createGateway(opts) {
21137
21164
  if (!db.enabled) return { status: "off" };
21138
21165
  sweep();
21139
21166
  const h = handles.get(handle2);
21140
- if (!h) return { status: "expired" };
21167
+ if (!h) {
21168
+ noteExpired("db.expired", "db-query", handle2);
21169
+ return { status: "expired" };
21170
+ }
21141
21171
  const name = typeof input.db === "string" && input.db ? input.db : "lotus";
21142
21172
  const sql = typeof input.sql === "string" ? input.sql : "";
21143
21173
  const fields = { sender: tail6(h.openId), runId: h.runId, kind: h.kind, db: name, sql: sql.slice(0, SQL_LOG_MAX) };
@@ -21236,7 +21266,10 @@ function createGatewayServer(opts) {
21236
21266
  };
21237
21267
  if (method === "POST" && path === DB_QUERY) {
21238
21268
  const handle2 = bearer();
21239
- if (!handle2) return send2(res, 200, { status: "expired" });
21269
+ if (!handle2) {
21270
+ gateway.noteMissingHandle("db.expired", "db-query");
21271
+ return send2(res, 200, { status: "expired" });
21272
+ }
21240
21273
  return send2(res, 200, await gateway.dbQuery(handle2, await readBody(req)));
21241
21274
  }
21242
21275
  const read = method === "POST" ? /^\/read\/([a-z-]+)$/.exec(path) : null;
@@ -21244,7 +21277,10 @@ function createGatewayServer(opts) {
21244
21277
  const api = read[1] ?? "";
21245
21278
  if (!isReadApi(api)) return send2(res, 404, { status: "unknown_api" });
21246
21279
  const handle2 = bearer();
21247
- if (!handle2) return send2(res, 200, { status: "expired" });
21280
+ if (!handle2) {
21281
+ gateway.noteMissingHandle("read.expired", api);
21282
+ return send2(res, 200, { status: "expired" });
21283
+ }
21248
21284
  return send2(res, 200, await gateway.read(handle2, api, await readBody(req)));
21249
21285
  }
21250
21286
  return send2(res, 404, { status: "not_found" });