aai-lark-bridge 0.2.54 → 0.2.55

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.54",
7
+ version: "0.2.55",
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",
@@ -717,9 +717,9 @@ function normalizeLarkCli(input, agentKind) {
717
717
  function normalizeClaudeSeat(input, agentKind) {
718
718
  if (!input || typeof input !== "object" || Array.isArray(input)) return void 0;
719
719
  const raw = input;
720
- const mode = raw.mode === "list" || raw.mode === "all" ? raw.mode : "off";
720
+ const mode = raw.mode === "list" || raw.mode === "all" || raw.mode === "agent" ? raw.mode : "off";
721
721
  const list = Array.isArray(raw.list) ? raw.list.filter((x) => typeof x === "string" && /^ou_[A-Za-z0-9_-]{1,100}$/.test(x)) : [];
722
- if (mode === "off") return { mode, list, gatewayUrl: String(raw.gatewayUrl ?? ""), tokenCommand: String(raw.tokenCommand ?? "") };
722
+ if (mode === "off" || mode === "agent") return { mode, list, gatewayUrl: String(raw.gatewayUrl ?? ""), tokenCommand: String(raw.tokenCommand ?? "") };
723
723
  if (agentKind !== "claude") throw new Error("larkCli.claudeSeat \u53EA\u652F\u6301 claude profile");
724
724
  if (typeof raw.gatewayUrl !== "string" || !/^https?:\/\/[^\s]+$/.test(raw.gatewayUrl)) throw new Error("larkCli.claudeSeat.gatewayUrl \u8981\u662F http(s) \u5730\u5740");
725
725
  if (typeof raw.tokenCommand !== "string" || !raw.tokenCommand.trim()) throw new Error("larkCli.claudeSeat.tokenCommand \u4E0D\u80FD\u4E3A\u7A7A");
@@ -7384,7 +7384,7 @@ function createClaudeSeat(opts) {
7384
7384
  }
7385
7385
  function seatFromEnv(env) {
7386
7386
  const raw = (env.SDLC_CLAUDE_SEAT ?? "off").trim();
7387
- const mode = raw === "list" || raw === "all" ? raw : "off";
7387
+ const mode = raw === "list" || raw === "all" || raw === "agent" ? raw : "off";
7388
7388
  const list = (env.SDLC_CLAUDE_SEAT_LIST ?? "").split(",").map((s) => s.trim()).filter((s) => /^ou_[A-Za-z0-9_-]{1,100}$/.test(s));
7389
7389
  return { mode, list };
7390
7390
  }
@@ -7691,6 +7691,9 @@ function seatCovers(cfg, senderId) {
7691
7691
  if (!cfg || !senderId) return false;
7692
7692
  return cfg.mode === "all" || cfg.mode === "list" && cfg.list.includes(senderId);
7693
7693
  }
7694
+ function isAgentSeat(cfg) {
7695
+ return cfg?.mode === "agent";
7696
+ }
7694
7697
  function isClaudeLogout(text) {
7695
7698
  return text.trim() === LOGOUT_TEXT;
7696
7699
  }
@@ -7774,6 +7777,106 @@ async function submitClaudeLogin(cfg, sub, deps = {}) {
7774
7777
  }
7775
7778
  }
7776
7779
 
7780
+ // src/bridge/bot/claude-quota.ts
7781
+ var TZ_OFFSET_MS = 7 * 60 * 6e4;
7782
+ var DAY_MS = 24 * 60 * 6e4;
7783
+ var QUOTA_FALLBACK_MS = 30 * 6e4;
7784
+ var WEEKDAYS = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"];
7785
+ var MONTHS = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
7786
+ var CN_WEEKDAYS = ["\u5468\u65E5", "\u5468\u4E00", "\u5468\u4E8C", "\u5468\u4E09", "\u5468\u56DB", "\u5468\u4E94", "\u5468\u516D"];
7787
+ var HIT_RE = /hit your (?:[A-Za-z0-9.+-]+ )*limit\b([^\n]*)/i;
7788
+ var TIME_RE = /^(\d{1,2})(?::(\d{2}))?\s*(am|pm)?\b/i;
7789
+ function detectQuota(text, now = /* @__PURE__ */ new Date()) {
7790
+ const m = HIT_RE.exec(text);
7791
+ if (!m) return void 0;
7792
+ const rest = m[1] ?? "";
7793
+ const r = /\bresets\s+(.+)$/i.exec(rest);
7794
+ const at = r ? parseReset(r[1].trim(), now.getTime()) : void 0;
7795
+ return at === void 0 ? { resumeAt: new Date(now.getTime() + QUOTA_FALLBACK_MS), fallback: true } : { resumeAt: new Date(at), fallback: false };
7796
+ }
7797
+ function parseReset(raw, nowMs) {
7798
+ let s = raw;
7799
+ let weekday;
7800
+ let monthDay;
7801
+ const wd = /^([A-Za-z]{3})[a-z]*\.?,?\s+/.exec(s);
7802
+ if (wd) {
7803
+ const key = wd[1].toLowerCase();
7804
+ const w = WEEKDAYS.indexOf(key);
7805
+ const mo = MONTHS.indexOf(key);
7806
+ if (w >= 0) {
7807
+ weekday = w;
7808
+ s = s.slice(wd[0].length);
7809
+ } else if (mo >= 0) {
7810
+ const d = /^(\d{1,2}),?\s+/.exec(s.slice(wd[0].length));
7811
+ if (!d) return void 0;
7812
+ monthDay = { month: mo, day: Number(d[1]) };
7813
+ s = s.slice(wd[0].length + d[0].length);
7814
+ } else {
7815
+ return void 0;
7816
+ }
7817
+ }
7818
+ const t = TIME_RE.exec(s);
7819
+ if (!t) return void 0;
7820
+ let hour = Number(t[1]);
7821
+ const minute = t[2] === void 0 ? 0 : Number(t[2]);
7822
+ const ampm = t[3]?.toLowerCase();
7823
+ if (!ampm && t[2] === void 0) return void 0;
7824
+ if (minute > 59) return void 0;
7825
+ if (ampm) {
7826
+ if (hour < 1 || hour > 12) return void 0;
7827
+ hour = hour % 12 + (ampm === "pm" ? 12 : 0);
7828
+ } else if (hour > 23) {
7829
+ return void 0;
7830
+ }
7831
+ const local = new Date(nowMs + TZ_OFFSET_MS);
7832
+ const localMidnight = Date.UTC(local.getUTCFullYear(), local.getUTCMonth(), local.getUTCDate());
7833
+ const clock = (hour * 60 + minute) * 6e4;
7834
+ const nowLocal = nowMs + TZ_OFFSET_MS;
7835
+ let atLocal;
7836
+ if (monthDay) {
7837
+ atLocal = Date.UTC(local.getUTCFullYear(), monthDay.month, monthDay.day) + clock;
7838
+ if (atLocal <= nowLocal) atLocal = Date.UTC(local.getUTCFullYear() + 1, monthDay.month, monthDay.day) + clock;
7839
+ } else if (weekday !== void 0) {
7840
+ const diff = (weekday - local.getUTCDay() + 7) % 7;
7841
+ atLocal = localMidnight + diff * DAY_MS + clock;
7842
+ if (atLocal <= nowLocal) atLocal += 7 * DAY_MS;
7843
+ } else {
7844
+ atLocal = localMidnight + clock;
7845
+ if (atLocal <= nowLocal) atLocal += DAY_MS;
7846
+ }
7847
+ return atLocal - TZ_OFFSET_MS;
7848
+ }
7849
+ function formatResume(at) {
7850
+ const local = new Date(at.getTime() + TZ_OFFSET_MS);
7851
+ const hh = String(local.getUTCHours()).padStart(2, "0");
7852
+ const mm = String(local.getUTCMinutes()).padStart(2, "0");
7853
+ return `${CN_WEEKDAYS[local.getUTCDay()]} ${hh}:${mm}`;
7854
+ }
7855
+ function quotaReply(at) {
7856
+ return `Claude \u989D\u5EA6\u7528\u5B8C\uFF0C\u7EA6 ${formatResume(at)} \u6062\u590D\uFF0C\u5230\u65F6\u518D\u95EE\u6211`;
7857
+ }
7858
+ var PROXY_DOWN_EXIT = 76;
7859
+ var PROXY_DOWN_REPLY = "Claude \u6682\u65F6\u8FDE\u4E0D\u4E0A\uFF0C\u5DF2\u901A\u77E5\u7EF4\u62A4\u4EBA";
7860
+ function isProxyDownExit(errorMsg) {
7861
+ return !!errorMsg && new RegExp(`^claude exited with code ${PROXY_DOWN_EXIT}\\b`).test(errorMsg);
7862
+ }
7863
+ function agentSeatNotice(state, now = /* @__PURE__ */ new Date()) {
7864
+ if (state.terminal === "error") {
7865
+ if (isProxyDownExit(state.errorMsg)) return PROXY_DOWN_REPLY;
7866
+ const hit = detectQuota([state.errorMsg ?? "", state.finalText ?? ""].join("\n"), now);
7867
+ return hit ? quotaReply(hit.resumeAt) : void 0;
7868
+ }
7869
+ if (state.terminal === "done" && state.finalText && state.finalText.trim().length <= 300) {
7870
+ const hit = detectQuota(state.finalText, now);
7871
+ return hit ? quotaReply(hit.resumeAt) : void 0;
7872
+ }
7873
+ return void 0;
7874
+ }
7875
+ function withNotice(state, text) {
7876
+ const { errorMsg: _e, apiError: _a, authError: _u, budgetHit: _b, budgetUsd: _bu, ...rest } = state;
7877
+ return { ...rest, terminal: "done", footer: null, finalText: text, blocks: [{ kind: "text", content: text, streaming: false }] };
7878
+ }
7879
+
7777
7880
  // src/bridge/bot/channel.ts
7778
7881
  import { dirname as dirname19, join as join30 } from "path";
7779
7882
 
@@ -17312,6 +17415,7 @@ async function runAgentBatchInner(deps) {
17312
17415
  ...threadId && !workerScope ? { threadId } : {}
17313
17416
  };
17314
17417
  const capability = controls.profileConfig.agentKind === "codex" ? codexCapability(controls.profileConfig) : claudeCapability(controls.profileConfig);
17418
+ const agentSeat = controls.profileConfig.agentKind === "claude" && isAgentSeat(controls.profileConfig.larkCli.claudeSeat);
17315
17419
  const flow = await startRunFlow({
17316
17420
  scopeId: scope,
17317
17421
  scope: scopeContext,
@@ -17338,7 +17442,7 @@ async function runAgentBatchInner(deps) {
17338
17442
  now: Date.now(),
17339
17443
  stopGraceMs: getAgentStopGraceMs(controls.cfg),
17340
17444
  ...binding && taskPlan?.ok ? { task: { binding, maxBudgetUsd: taskPlan.maxBudgetUsd } } : {},
17341
- ...deps.userRead?.extraEnv || deps.claudeEnv || firstMsg.chatType === "p2p" ? { extraEnv: { ...deps.claudeEnv, ...deps.userRead?.extraEnv, ...firstMsg.chatType === "p2p" ? { SDLC_DM_CHAT: chatId } : {}, ...firstMsg.chatType === "p2p" && scope === chatId ? { SDLC_DM_MSG: lastMsg.messageId } : {} } } : {},
17445
+ ...deps.userRead?.extraEnv || deps.claudeEnv || firstMsg.chatType === "p2p" || agentSeat ? { extraEnv: { ...deps.claudeEnv, ...deps.userRead?.extraEnv, ...firstMsg.chatType === "p2p" ? { SDLC_DM_CHAT: chatId } : {}, ...firstMsg.chatType === "p2p" && scope === chatId ? { SDLC_DM_MSG: lastMsg.messageId } : {}, ...agentSeat ? { SDLC_USAGE_SCENE: firstMsg.chatType === "p2p" ? "dm" : "group" } : {} } } : {},
17342
17446
  observability: {
17343
17447
  profile: controls.profile,
17344
17448
  agent: capability.agentId,
@@ -17435,6 +17539,14 @@ async function runAgentBatchInner(deps) {
17435
17539
  recordSession.costDelta = (total) => runSessionId ? sessionCatalog.recordCost({ scopeId: scope, agentId: "claude", cwdRealpath: runCwd, policyFingerprint: flow.policy.policyFingerprint, sessionId: runSessionId }, total) : total;
17436
17540
  }
17437
17541
  const startState = binding ? { ...initialState, task: { footer: taskFooterLine(binding) } } : initialState;
17542
+ if (agentSeat) {
17543
+ recordSession.onTerminal = (state) => {
17544
+ const notice = agentSeatNotice(state);
17545
+ if (!notice) return state;
17546
+ log.info("agent", "seat-notice", { scope, terminal: state.terminal, notice });
17547
+ return withNotice(state, notice);
17548
+ };
17549
+ }
17438
17550
  if (binding) {
17439
17551
  const hooks = taskRunHooks({
17440
17552
  binding,
@@ -18035,6 +18147,7 @@ async function processAgentStream(handle2, events, scope, idleTimeoutMs, recordS
18035
18147
  if (state.budgetHit && !state.task?.budgetNote && recordSession.onBudgetHit) {
18036
18148
  state = recordSession.onBudgetHit(state);
18037
18149
  }
18150
+ if (state.terminal !== "running" && recordSession.onTerminal) state = recordSession.onTerminal(state);
18038
18151
  if (state.footer !== prevFooter || state.terminal !== prevTerminal) {
18039
18152
  log.info("card", "transition", { footer: state.footer, terminal: state.terminal });
18040
18153
  }
@@ -18052,6 +18165,7 @@ async function processAgentStream(handle2, events, scope, idleTimeoutMs, recordS
18052
18165
  } else {
18053
18166
  state = finalizeIfRunning(state);
18054
18167
  }
18168
+ if (recordSession.onTerminal) state = recordSession.onTerminal(state);
18055
18169
  }
18056
18170
  log.info("card", "final", { scope, terminal: state.terminal, interrupted: handle2.interrupted });
18057
18171
  reportMetric("run_e2e_ms", Date.now() - runStart2, { terminal: state.terminal });