aai-lark-bridge 0.1.0 → 0.1.1

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.1.0",
7
+ version: "0.1.1",
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",
@@ -11759,47 +11759,86 @@ function signatureMatches(actual, expected) {
11759
11759
 
11760
11760
  // src/bridge/card/callback-store.ts
11761
11761
  import { readFile as readFile13 } from "fs/promises";
11762
+ var DEFAULT_TTL_MS2 = 48 * 60 * 60 * 1e3;
11763
+ var DEFAULT_MAX_ENTRIES = 5e3;
11762
11764
  var CallbackNonceStore = class {
11763
11765
  path;
11766
+ ttlMs;
11767
+ now;
11768
+ maxEntries;
11764
11769
  nonces = /* @__PURE__ */ new Map();
11765
11770
  saving = Promise.resolve();
11766
- constructor(path) {
11771
+ constructor(path, options) {
11767
11772
  this.path = path;
11773
+ this.ttlMs = options?.ttlMs ?? DEFAULT_TTL_MS2;
11774
+ this.now = options?.now ?? Date.now;
11775
+ this.maxEntries = options?.maxEntries ?? DEFAULT_MAX_ENTRIES;
11768
11776
  }
11769
11777
  async load() {
11770
11778
  try {
11771
11779
  const raw = JSON.parse(await readFile13(this.path, "utf8"));
11772
11780
  if (!raw || typeof raw !== "object") return;
11773
11781
  this.nonces.clear();
11774
- for (const [nonce, state] of Object.entries(raw)) {
11775
- if (state === "used" || state === "revoked") this.nonces.set(nonce, state);
11782
+ for (const [nonce, value] of Object.entries(raw)) {
11783
+ const entry = decodeEntry(value);
11784
+ if (!entry) continue;
11785
+ if (this.isExpired(entry)) continue;
11786
+ this.nonces.set(nonce, entry);
11776
11787
  }
11788
+ this.compact();
11777
11789
  } catch (err) {
11778
11790
  if (err.code === "ENOENT") return;
11779
11791
  log.fail("callback-nonce", err, { step: "load" });
11780
11792
  }
11781
11793
  }
11782
11794
  state(nonce) {
11783
- return this.nonces.get(nonce);
11795
+ return this.nonces.get(nonce)?.state;
11784
11796
  }
11785
11797
  consume(nonce) {
11786
11798
  if (this.nonces.has(nonce)) return false;
11787
- this.nonces.set(nonce, "used");
11799
+ this.nonces.set(nonce, { state: "used", ts: this.now() });
11800
+ this.compact();
11788
11801
  this.schedulePersist();
11789
11802
  return true;
11790
11803
  }
11791
11804
  revoke(nonce) {
11792
- this.nonces.set(nonce, "revoked");
11805
+ this.nonces.set(nonce, { state: "revoked", ts: this.now() });
11806
+ this.compact();
11793
11807
  this.schedulePersist();
11794
11808
  }
11795
11809
  async flush() {
11796
11810
  await this.saving;
11797
11811
  }
11812
+ /** Size of the in-memory nonce map (test/diagnostic surface). */
11813
+ get size() {
11814
+ return this.nonces.size;
11815
+ }
11816
+ isExpired(entry) {
11817
+ return this.now() - entry.ts >= this.ttlMs;
11818
+ }
11819
+ /**
11820
+ * Drop expired entries, then prune oldest entries down to `maxEntries`.
11821
+ * Called after every mutation so the store never grows unboundedly.
11822
+ */
11823
+ compact() {
11824
+ if (this.nonces.size === 0) return;
11825
+ for (const [nonce, entry] of this.nonces) {
11826
+ if (this.isExpired(entry)) this.nonces.delete(nonce);
11827
+ }
11828
+ if (this.nonces.size > this.maxEntries) {
11829
+ const over = this.nonces.size - this.maxEntries;
11830
+ let removed = 0;
11831
+ for (const nonce of this.nonces.keys()) {
11832
+ this.nonces.delete(nonce);
11833
+ if (++removed >= over) break;
11834
+ }
11835
+ }
11836
+ }
11798
11837
  schedulePersist() {
11799
11838
  this.saving = this.saving.then(async () => {
11800
11839
  await writeFileAtomic(
11801
11840
  this.path,
11802
- `${JSON.stringify(Object.fromEntries(this.nonces), null, 2)}
11841
+ `${JSON.stringify(this.serialize(), null, 2)}
11803
11842
  `,
11804
11843
  { mode: 384 }
11805
11844
  );
@@ -11807,7 +11846,24 @@ var CallbackNonceStore = class {
11807
11846
  log.fail("callback-nonce", err, { step: "persist" });
11808
11847
  });
11809
11848
  }
11849
+ serialize() {
11850
+ const out = {};
11851
+ for (const [nonce, entry] of this.nonces) out[nonce] = entry;
11852
+ return out;
11853
+ }
11810
11854
  };
11855
+ function decodeEntry(value) {
11856
+ if (value === "used" || value === "revoked") {
11857
+ return { state: value, ts: 0 };
11858
+ }
11859
+ if (value && typeof value === "object") {
11860
+ const v = value;
11861
+ if (v.state === "used" || v.state === "revoked") {
11862
+ return { state: v.state, ts: typeof v.ts === "number" ? v.ts : 0 };
11863
+ }
11864
+ }
11865
+ return void 0;
11866
+ }
11811
11867
 
11812
11868
  // src/bridge/card/text-renderer.ts
11813
11869
  function renderText(state) {
@@ -13805,6 +13861,7 @@ function stringValue3(value) {
13805
13861
  var DEBOUNCE_MS = 600;
13806
13862
  var STREAM_TERMINAL_GRACE_MS = 3e3;
13807
13863
  var REACTION_CLEANUP_GRACE_MS = 1e3;
13864
+ var STREAM_TRUSTED_LIFETIME_MS = 9 * 6e4;
13808
13865
  var BRIDGE_AGENT_INSTRUCTIONS = [
13809
13866
  "\u4F60\u5728 bridge \u8FDB\u7A0B\u4E2D\u8FD0\u884C\uFF0C\u666E\u901A lark-cli \u4F1A\u7EE7\u627F LARK_CHANNEL=1 \u5E76\u8FDB\u5165 bridge-bound \u6A21\u5F0F\u3002",
13810
13867
  "\u4E0D\u8981 unset LARK_CHANNEL / LARK_CHANNEL_HOME / LARK_CHANNEL_PROFILE / LARKSUITE_CLI_CONFIG_DIR\uFF0C\u4E5F\u4E0D\u8981\u7528 env -u LARK_CHANNEL \u7ED5\u56DE\u672C\u673A\u666E\u901A\u914D\u7F6E\u3002",
@@ -14649,6 +14706,21 @@ async function runAgentBatch(deps) {
14649
14706
  sendOpts,
14650
14707
  cardRenderOptions
14651
14708
  });
14709
+ } else if (streamOutlivedCardLifetime(progress)) {
14710
+ log.warn("stream", "outlived-card-lifetime", {
14711
+ scope,
14712
+ mode: replyMode,
14713
+ openMs: Date.now() - (progress.openedAt() ?? Date.now())
14714
+ });
14715
+ await sendFinalReply({
14716
+ channel,
14717
+ chatId,
14718
+ scope,
14719
+ state: tailTextState(filterForPrefs(latestState)),
14720
+ replyMode,
14721
+ sendOpts,
14722
+ cardRenderOptions
14723
+ });
14652
14724
  }
14653
14725
  } else if (replyMode === "markdown") {
14654
14726
  let latestState = initialState;
@@ -14714,6 +14786,21 @@ async function runAgentBatch(deps) {
14714
14786
  sendOpts,
14715
14787
  cardRenderOptions
14716
14788
  });
14789
+ } else if (streamOutlivedCardLifetime(progress)) {
14790
+ log.warn("stream", "outlived-card-lifetime", {
14791
+ scope,
14792
+ mode: replyMode,
14793
+ openMs: Date.now() - (progress.openedAt() ?? Date.now())
14794
+ });
14795
+ await sendFinalReply({
14796
+ channel,
14797
+ chatId,
14798
+ scope,
14799
+ state: tailTextState(filterForPrefs(latestState)),
14800
+ replyMode,
14801
+ sendOpts,
14802
+ cardRenderOptions
14803
+ });
14717
14804
  }
14718
14805
  } else {
14719
14806
  const finalState = await processAgentStream(
@@ -14744,6 +14831,7 @@ async function runAgentBatch(deps) {
14744
14831
  }
14745
14832
  function createLazyProgressStream(scope, mode, open4) {
14746
14833
  let stream2;
14834
+ let openedAt;
14747
14835
  let givenUp = false;
14748
14836
  let settle;
14749
14837
  const settled = new Promise((resolve2, reject4) => {
@@ -14754,9 +14842,11 @@ function createLazyProgressStream(scope, mode, open4) {
14754
14842
  return {
14755
14843
  settled,
14756
14844
  opened: () => stream2 !== void 0,
14845
+ openedAt: () => openedAt,
14757
14846
  ensureOpen: () => {
14758
14847
  if (stream2) return;
14759
14848
  log.info("outbound", "progress-stream-open", { scope, mode });
14849
+ openedAt = Date.now();
14760
14850
  stream2 = open4();
14761
14851
  settle(stream2);
14762
14852
  },
@@ -14766,6 +14856,20 @@ function createLazyProgressStream(scope, mode, open4) {
14766
14856
  }
14767
14857
  };
14768
14858
  }
14859
+ function streamOutlivedCardLifetime(progress) {
14860
+ const openedAt = progress.openedAt();
14861
+ if (openedAt === void 0 || progress.abandoned()) return false;
14862
+ return Date.now() - openedAt > STREAM_TRUSTED_LIFETIME_MS;
14863
+ }
14864
+ function tailTextState(state) {
14865
+ const lastText = [...state.blocks].reverse().find((b) => b.kind === "text");
14866
+ return {
14867
+ ...state,
14868
+ blocks: lastText ? [lastText] : [],
14869
+ reasoning: { content: "", active: false },
14870
+ footer: null
14871
+ };
14872
+ }
14769
14873
  function shouldOpenProgressStream(state) {
14770
14874
  if (state.terminal !== "running") return false;
14771
14875
  return renderText({ ...state, footer: null }).trim() !== "";