aai-lark-bridge 0.2.37 → 0.2.38

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.37",
7
+ version: "0.2.38",
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",
@@ -7497,17 +7497,17 @@ async function requestUserRead(cfg, messageId, deps = {}) {
7497
7497
  const gatewayRunId = randomUUID();
7498
7498
  try {
7499
7499
  const r = await callGateway(cfg, "POST", "/handles", { messageId, runId: gatewayRunId }, deps);
7500
+ const withDb = (base) => r.db === true && cfg.dbPrompt ? `${base}
7501
+
7502
+ ${cfg.dbPrompt}` : base;
7503
+ const env = (handle2) => ({ [HANDLE_ENV]: handle2, [GATEWAY_ENV]: cfg.gatewayUrl });
7500
7504
  if (r.status === "ok" && typeof r.handle === "string") {
7501
7505
  log.info("user-read", "handle", { messageId, gatewayRunId });
7502
- return {
7503
- kind: "handle",
7504
- gatewayRunId,
7505
- env: { [HANDLE_ENV]: r.handle, [GATEWAY_ENV]: cfg.gatewayUrl },
7506
- // 网关按查库名单决定这个句柄能不能查库(intent 0049);能查才把部署方写的说明给 agent
7507
- prompt: r.db === true && cfg.dbPrompt ? `${PROMPT_TOOL}
7508
-
7509
- ${cfg.dbPrompt}` : PROMPT_TOOL
7510
- };
7506
+ return { kind: "handle", gatewayRunId, env: env(r.handle), prompt: withDb(PROMPT_TOOL) };
7507
+ }
7508
+ if ((r.status === "need_auth" || r.status === "auth_failed") && typeof r.handle === "string") {
7509
+ log.info("user-read", "handle", { messageId, gatewayRunId, authorized: false });
7510
+ return { kind: "handle", gatewayRunId, env: env(r.handle), prompt: withDb(r.status === "need_auth" ? PROMPT_NEED_AUTH : PROMPT_AUTH_FAILED) };
7511
7511
  }
7512
7512
  log.info("user-read", "no-handle", { messageId, status: String(r.status) });
7513
7513
  if (r.status === "need_auth") return { kind: "need_auth", prompt: PROMPT_NEED_AUTH };
@@ -16446,12 +16446,13 @@ async function runUserReadBatch(deps) {
16446
16446
  const first = deps.batch[0];
16447
16447
  const last = deps.batch[deps.batch.length - 1];
16448
16448
  const cfg = deps.controls.profileConfig.larkCli.perSenderUser;
16449
- if (!first || !last || first.chatType !== "p2p" || !cfg?.enabled || deps.controls.profileConfig.agentKind !== "claude") {
16449
+ if (!first || !last || !cfg?.enabled || deps.controls.profileConfig.agentKind !== "claude") {
16450
16450
  return runAgentBatchInner(deps);
16451
16451
  }
16452
- const preBinding = deps.preBinding ?? { result: dmBinding(deps.controls, first, deps.scope) };
16453
- if (preBinding.result?.kind === "bound") return runAgentBatchInner({ ...deps, preBinding });
16454
- if (deps.batch.length === 1 && isRevokeRequest(last.content)) {
16452
+ const isP2p = first.chatType === "p2p";
16453
+ const preBinding = deps.preBinding ?? (isP2p ? { result: dmBinding(deps.controls, first, deps.scope) } : void 0);
16454
+ const bound2 = preBinding?.result?.kind === "bound";
16455
+ if (isP2p && !bound2 && deps.batch.length === 1 && isRevokeRequest(last.content)) {
16455
16456
  const text = await revokeUserRead(cfg, last.messageId);
16456
16457
  await deps.channel.send(first.chatId, { markdown: text }, { replyTo: last.messageId });
16457
16458
  return;
@@ -16460,7 +16461,7 @@ async function runUserReadBatch(deps) {
16460
16461
  try {
16461
16462
  await runAgentBatchInner({
16462
16463
  ...deps,
16463
- preBinding,
16464
+ ...preBinding ? { preBinding } : {},
16464
16465
  userRead: { ...outcome.kind === "handle" ? { extraEnv: outcome.env } : {}, instruction: outcome.prompt }
16465
16466
  });
16466
16467
  } finally {
@@ -19785,13 +19786,9 @@ var TASK_HANDLE_TTL_MS = 150 * 6e4;
19785
19786
  var SQL_LOG_MAX = 2e3;
19786
19787
  var LOCK_WAIT_SEC = 60;
19787
19788
  var DB_NAME = /^[a-z0-9_-]{1,40}$/;
19788
- var OPEN_ID = /^ou_[A-Za-z0-9_-]{1,100}$/;
19789
- var ALL = "*";
19790
- var isDbReader = (db, openId) => db.readers.includes(ALL) || db.readers.includes(openId);
19791
19789
  function dbFromEnv(env, cmd = "") {
19792
19790
  const argv = cmd.trim() ? cmd.trim().split(/\s+/) : [];
19793
- const readers = (env.SDLC_DB_READERS ?? "").split(",").map((s) => s.trim()).filter((s) => s === ALL || OPEN_ID.test(s));
19794
- return { enabled: (env.SDLC_DB_GATEWAY ?? "").trim() === "on" && argv.length > 0, readers, cmd: argv };
19791
+ return { enabled: (env.SDLC_DB_GATEWAY ?? "").trim() === "on" && argv.length > 0, cmd: argv };
19795
19792
  }
19796
19793
  function realDbRunner(cmd) {
19797
19794
  return (args, timeoutMs) => new Promise((resolve3) => {
@@ -19968,7 +19965,7 @@ var RATE_LIMIT_BACKOFF_MS = [2e3, 5e3, 1e4];
19968
19965
  var REVOKE_TEXT2 = "\u64A4\u9500\u6388\u6743";
19969
19966
  var MESSAGE_ID = /^om_[A-Za-z0-9_-]{1,100}$/;
19970
19967
  var CHAT_ID = /^oc_[A-Za-z0-9_-]{1,100}$/;
19971
- var OPEN_ID2 = /^ou_[A-Za-z0-9_-]{1,100}$/;
19968
+ var OPEN_ID = /^ou_[A-Za-z0-9_-]{1,100}$/;
19972
19969
  var RUN_ID = /^[A-Za-z0-9._:-]{1,200}$/;
19973
19970
  var NONCE = /^[0-9a-f]{32}$/;
19974
19971
  var TASK_GUID = /^[A-Za-z0-9-]{8,100}$/;
@@ -20160,7 +20157,7 @@ function createGateway(opts) {
20160
20157
  return msg.code === 0 ? "not_found" : "lark_error";
20161
20158
  }
20162
20159
  const sender = isRecord6(item.sender) ? item.sender : {};
20163
- if (sender.sender_type !== "user" || sender.id_type !== "open_id" || typeof sender.id !== "string" || !OPEN_ID2.test(sender.id)) {
20160
+ if (sender.sender_type !== "user" || sender.id_type !== "open_id" || typeof sender.id !== "string" || !OPEN_ID.test(sender.id)) {
20164
20161
  return "not_user";
20165
20162
  }
20166
20163
  const created = Number(item.create_time);
@@ -20238,7 +20235,7 @@ function createGateway(opts) {
20238
20235
  const members = Array.isArray(task.members) ? task.members : [];
20239
20236
  return members.filter((m) => isRecord6(m) && m.role === "assignee" && typeof m.id === "string").map((m) => m.id);
20240
20237
  }
20241
- const db = opts.db ?? { enabled: false, readers: [], cmd: [] };
20238
+ const db = opts.db ?? { enabled: false, cmd: [] };
20242
20239
  const runDb = opts.dbRunner ?? realDbRunner(db.cmd);
20243
20240
  const seat = createClaudeSeat({
20244
20241
  stateDir: opts.stateDir,
@@ -20302,7 +20299,7 @@ function createGateway(opts) {
20302
20299
  };
20303
20300
  }
20304
20301
  return {
20305
- /** POST /handles:核实提问人,发句柄;没授权就发卡片并返回 need_auth。 */
20302
+ /** POST /handles:核实提问人(私聊或群里的消息),发句柄;没授权就发卡片并返回 need_auth,查库开着时同一轮仍带只能查库的句柄(intent 0068)。 */
20306
20303
  async requestHandle(input) {
20307
20304
  let messageId, runId;
20308
20305
  try {
@@ -20311,27 +20308,33 @@ function createGateway(opts) {
20311
20308
  } catch {
20312
20309
  return { status: "bad_request" };
20313
20310
  }
20314
- const v = await verify(messageId, "handle");
20311
+ const v = await verify(messageId, "handle", false);
20315
20312
  if (typeof v === "string") {
20316
20313
  log.info("user-gateway", "handle.refused", { messageId, runId, result: v });
20317
20314
  return { status: v };
20318
20315
  }
20316
+ const issue = (read) => {
20317
+ const handle2 = randomBytes10(32).toString("base64url");
20318
+ const expiresAt = now() + HANDLE_TTL_MS;
20319
+ handles.set(handle2, { openId: v.openId, messageId, runId, expiresAt, kind: "dm", db: db.enabled, read });
20320
+ log.info("user-gateway", "handle.issued", { identity: "user", sender: tail6(v.openId), messageId, runId, db: db.enabled, read });
20321
+ return { handle: handle2, expiresAt };
20322
+ };
20319
20323
  if (!isAuthorized(v.openId)) {
20324
+ let started = true;
20320
20325
  try {
20321
20326
  await startAuth(v.openId);
20322
20327
  } catch (err) {
20328
+ started = false;
20323
20329
  log.warn("user-gateway", "auth.start-failed", { sender: tail6(v.openId), messageId, runId, detail: String(err) });
20324
- return { status: "auth_failed" };
20325
20330
  }
20326
- log.info("user-gateway", "handle.need-auth", { sender: tail6(v.openId), messageId, runId });
20327
- return { status: "need_auth" };
20331
+ if (started) log.info("user-gateway", "handle.need-auth", { sender: tail6(v.openId), messageId, runId });
20332
+ const h2 = db.enabled ? issue(false) : void 0;
20333
+ const extra = h2 ? { handle: h2.handle, expiresAt: h2.expiresAt, db: true } : {};
20334
+ return started ? { status: "need_auth", ...extra } : { status: "auth_failed", ...extra };
20328
20335
  }
20329
- const handle2 = randomBytes10(32).toString("base64url");
20330
- const expiresAt = now() + HANDLE_TTL_MS;
20331
- const canDb = db.enabled && isDbReader(db, v.openId);
20332
- handles.set(handle2, { openId: v.openId, messageId, runId, expiresAt, kind: "dm", db: canDb });
20333
- log.info("user-gateway", "handle.issued", { identity: "user", sender: tail6(v.openId), messageId, runId, db: canDb });
20334
- return { status: "ok", handle: handle2, expiresAt, db: canDb };
20336
+ const h = issue(true);
20337
+ return { status: "ok", handle: h.handle, expiresAt: h.expiresAt, db: db.enabled };
20335
20338
  },
20336
20339
  /** DELETE /handles/{runId}:运行结束,作废这次运行的句柄。 */
20337
20340
  dropHandles(runId) {
@@ -20350,6 +20353,10 @@ function createGateway(opts) {
20350
20353
  sweep();
20351
20354
  const h = handles.get(handle2);
20352
20355
  if (!h || h.kind !== "dm") return { status: "expired" };
20356
+ if (!h.read) {
20357
+ log.info("user-gateway", "read.need-auth", { identity: "user", sender: tail6(h.openId), messageId: h.messageId, runId: h.runId, api, detail: "handle-without-read" });
20358
+ return { status: "need_auth" };
20359
+ }
20353
20360
  let args;
20354
20361
  try {
20355
20362
  args = readArgs(api, input);
@@ -20423,7 +20430,7 @@ function createGateway(opts) {
20423
20430
  let taskGuid, openId, runId;
20424
20431
  try {
20425
20432
  taskGuid = str2(input, "taskGuid", TASK_GUID);
20426
- openId = str2(input, "openId", OPEN_ID2);
20433
+ openId = str2(input, "openId", OPEN_ID);
20427
20434
  runId = str2(input, "runId", RUN_ID);
20428
20435
  } catch {
20429
20436
  return { status: "bad_request" };
@@ -20445,7 +20452,7 @@ function createGateway(opts) {
20445
20452
  async claudeReady(input) {
20446
20453
  let openId;
20447
20454
  try {
20448
- openId = str2(input, "openId", OPEN_ID2);
20455
+ openId = str2(input, "openId", OPEN_ID);
20449
20456
  } catch {
20450
20457
  return { status: "bad_request" };
20451
20458
  }
@@ -20473,7 +20480,7 @@ function createGateway(opts) {
20473
20480
  let openId, card, nonce;
20474
20481
  const code = typeof input.code === "string" ? normalizeCode(input.code) : "";
20475
20482
  try {
20476
- openId = str2(input, "openId", OPEN_ID2);
20483
+ openId = str2(input, "openId", OPEN_ID);
20477
20484
  card = str2(input, "cardMessageId", MESSAGE_ID);
20478
20485
  nonce = str2(input, "nonce", NONCE);
20479
20486
  } catch {
@@ -20503,25 +20510,20 @@ function createGateway(opts) {
20503
20510
  /**
20504
20511
  * POST /db/task-handle:调度的只读运行(只读分析、出文档、补报告)起 claude 前凭任务申请查库句柄(intent 0049 R-04)。
20505
20512
  * 负责人得是这个任务的 assignee(网关向飞书核对,同 0048 的 /claude/task-token);不看私聊查库名单(spec D-3)。
20506
- * 评测(intent 0050,带 eval: true):评测的任务号不是真飞书任务,不带 taskGuid,改按私聊查库名单放行,日志记 task=eval。
20513
+ * 评测(intent 0050,带 eval: true):评测的任务号不是真飞书任务,不带 taskGuid,只看查库总开关(intent 0068 起没有名单),日志记 task=eval。
20507
20514
  */
20508
20515
  async dbTaskHandle(input) {
20509
20516
  const isEval = input.eval === true;
20510
20517
  let taskGuid = null, openId, runId;
20511
20518
  try {
20512
20519
  if (!isEval) taskGuid = str2(input, "taskGuid", TASK_GUID);
20513
- openId = str2(input, "openId", OPEN_ID2);
20520
+ openId = str2(input, "openId", OPEN_ID);
20514
20521
  runId = str2(input, "runId", RUN_ID);
20515
20522
  } catch {
20516
20523
  return { status: "bad_request" };
20517
20524
  }
20518
20525
  if (!db.enabled) return { status: "off" };
20519
- if (taskGuid === null) {
20520
- if (!isDbReader(db, openId)) {
20521
- log.warn("user-gateway", "db.handle-refused", { sender: tail6(openId), runId, task: "eval", result: "not_reader" });
20522
- return { status: "not_reader" };
20523
- }
20524
- } else {
20526
+ if (taskGuid !== null) {
20525
20527
  const assignees = await taskAssignees(taskGuid);
20526
20528
  if (assignees === "lark_error") return { status: "lark_error" };
20527
20529
  if (!assignees.includes(openId)) {
@@ -20531,7 +20533,7 @@ function createGateway(opts) {
20531
20533
  }
20532
20534
  const handle2 = randomBytes10(32).toString("base64url");
20533
20535
  const expiresAt = now() + TASK_HANDLE_TTL_MS;
20534
- handles.set(handle2, { openId, messageId: "", runId, expiresAt, kind: "task", db: true });
20536
+ handles.set(handle2, { openId, messageId: "", runId, expiresAt, kind: "task", db: true, read: false });
20535
20537
  log.info("user-gateway", "db.handle-issued", { sender: tail6(openId), runId, task: taskGuid ?? "eval" });
20536
20538
  return { status: "ok", handle: handle2, expiresAt };
20537
20539
  },
@@ -20683,7 +20685,7 @@ async function runUserGateway(opts) {
20683
20685
  exec: defaultExec,
20684
20686
  // Claude 席位开关(intent 0048):容器的 SDLC_CLAUDE_SEAT、SDLC_CLAUDE_SEAT_LIST,缺省关
20685
20687
  seat: seatFromEnv(process.env),
20686
- // 查库开关(intent 0049):容器的 SDLC_DB_GATEWAY、SDLC_DB_READERS,缺省关
20688
+ // 查库开关(intent 0049):容器的 SDLC_DB_GATEWAY,缺省关;开了谁都能查,没有名单(intent 0068)
20687
20689
  db: dbFromEnv(process.env, opts.dbCmd)
20688
20690
  });
20689
20691
  const pass = fetchToken(opts.tokenCmd);