aai-lark-bridge 0.1.3 → 0.1.4

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.3",
7
+ version: "0.1.4",
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",
@@ -4854,10 +4854,25 @@ import { readFile as readFile10 } from "fs/promises";
4854
4854
 
4855
4855
  // src/bridge/agent/lark-channel-env.ts
4856
4856
  import { join as join16 } from "path";
4857
+ var COMPETING_AGENT_ENV_VARS = [
4858
+ "HERMES_EXEC_ASK",
4859
+ "HERMES_GATEWAY_TOKEN",
4860
+ "HERMES_HOME",
4861
+ "HERMES_QUIET",
4862
+ "HERMES_SESSION_KEY",
4863
+ "OPENCLAW_CLI",
4864
+ "OPENCLAW_CONFIG_PATH",
4865
+ "OPENCLAW_GATEWAY_PORT",
4866
+ "OPENCLAW_HOME",
4867
+ "OPENCLAW_SERVICE_MARKER",
4868
+ "OPENCLAW_SERVICE_VERSION",
4869
+ "OPENCLAW_SHELL",
4870
+ "OPENCLAW_STATE_DIR"
4871
+ ];
4857
4872
  function buildLarkChannelEnv(context) {
4858
- const env = {
4859
- LARK_CHANNEL: "1"
4860
- };
4873
+ const env = {};
4874
+ for (const key of COMPETING_AGENT_ENV_VARS) env[key] = void 0;
4875
+ env.LARK_CHANNEL = "1";
4861
4876
  const profile2 = nonEmpty(context?.profile);
4862
4877
  if (profile2) env.LARK_CHANNEL_PROFILE = profile2;
4863
4878
  const rootDir = nonEmpty(context?.rootDir);
@@ -6099,6 +6114,15 @@ function* translateEvent(raw) {
6099
6114
  };
6100
6115
  return;
6101
6116
  }
6117
+ if (evt.type === "system" && evt.subtype === "api_retry" && typeof evt.attempt === "number") {
6118
+ yield {
6119
+ type: "api_retry",
6120
+ attempt: evt.attempt,
6121
+ ...typeof evt.max_retries === "number" ? { maxRetries: evt.max_retries } : {}
6122
+ };
6123
+ return;
6124
+ }
6125
+ if (evt.type === "assistant" && evt.is_api_error_message === true) return;
6102
6126
  if (evt.type === "assistant" && evt.message?.content) {
6103
6127
  for (const block of evt.message.content) {
6104
6128
  if (block.type === "text" && typeof block.text === "string" && block.text) {
@@ -6144,9 +6168,24 @@ function* translateEvent(raw) {
6144
6168
  };
6145
6169
  return;
6146
6170
  }
6171
+ if (evt.is_error === true) {
6172
+ const apiError = evt.terminal_reason === "api_error";
6173
+ yield {
6174
+ type: "error",
6175
+ message: errorMessage2(evt),
6176
+ terminationReason: "failed",
6177
+ ...apiError ? { apiError: true } : {}
6178
+ };
6179
+ return;
6180
+ }
6147
6181
  yield { type: "done", sessionId: evt.session_id, terminationReason: "normal" };
6148
6182
  }
6149
6183
  }
6184
+ function errorMessage2(evt) {
6185
+ const text = typeof evt.result === "string" ? evt.result.trim() : "";
6186
+ if (text) return text;
6187
+ return evt.terminal_reason === "api_error" ? "claude \u4E0E\u4E0A\u6E38 API \u7684\u8FDE\u63A5\u4E2D\u65AD" : `claude \u5F02\u5E38\u7EC8\u6B62\uFF08terminal_reason=${evt.terminal_reason ?? "unknown"}\uFF09`;
6188
+ }
6150
6189
 
6151
6190
  // src/bridge/agent/stderr-tail.ts
6152
6191
  var DEFAULT_KEEP_BYTES = 64 * 1024;
@@ -6329,6 +6368,7 @@ async function* createEventStream(child, stderrTail, getError) {
6329
6368
  }
6330
6369
  const rl = createInterface3({ input: child.stdout, crlfDelay: Infinity });
6331
6370
  let sawStdout = false;
6371
+ let sawTerminal = false;
6332
6372
  let silentExitTimer;
6333
6373
  const closeSilentStdout = () => {
6334
6374
  silentExitTimer = setTimeout(() => {
@@ -6347,13 +6387,17 @@ async function* createEventStream(child, stderrTail, getError) {
6347
6387
  } catch {
6348
6388
  continue;
6349
6389
  }
6350
- yield* translateEvent(parsed);
6390
+ for (const event of translateEvent(parsed)) {
6391
+ if (event.type === "done" || event.type === "error") sawTerminal = true;
6392
+ yield event;
6393
+ }
6351
6394
  }
6352
6395
  } finally {
6353
6396
  if (silentExitTimer) clearTimeout(silentExitTimer);
6354
6397
  child.removeListener("exit", closeSilentStdout);
6355
6398
  rl.close();
6356
6399
  }
6400
+ if (sawTerminal) return;
6357
6401
  const earlyRuntimeError = getError();
6358
6402
  if (earlyRuntimeError && child.exitCode === null && child.signalCode === null) {
6359
6403
  yield {
@@ -6641,7 +6685,7 @@ var CodexJsonlTranslator = class {
6641
6685
  }
6642
6686
  translateTerminalError(raw, fallback) {
6643
6687
  this.terminal = true;
6644
- const message = errorMessage2(raw, fallback);
6688
+ const message = errorMessage3(raw, fallback);
6645
6689
  return [
6646
6690
  {
6647
6691
  type: "error",
@@ -6651,7 +6695,7 @@ var CodexJsonlTranslator = class {
6651
6695
  ];
6652
6696
  }
6653
6697
  translateNonTerminalError(raw, fallback) {
6654
- const message = errorMessage2(raw, fallback);
6698
+ const message = errorMessage3(raw, fallback);
6655
6699
  this.lastNonTerminalError = message;
6656
6700
  log.warn("jsonl", "error_event", { message: truncate(message, 500) });
6657
6701
  return [];
@@ -6669,7 +6713,7 @@ function stringValue(value) {
6669
6713
  function numberValue(value) {
6670
6714
  return typeof value === "number" ? value : void 0;
6671
6715
  }
6672
- function errorMessage2(raw, fallback) {
6716
+ function errorMessage3(raw, fallback) {
6673
6717
  const nested = recordValue(raw.error);
6674
6718
  return stringValue(raw.message) ?? stringValue(nested?.message) ?? stringValue(raw.error) ?? fallback;
6675
6719
  }
@@ -7045,12 +7089,12 @@ function accountCurrentCard(info) {
7045
7089
  };
7046
7090
  }
7047
7091
  function accountFormCard(opts = {}) {
7048
- const { initialTenant = "feishu", prefillAppId, errorMessage: errorMessage4 } = opts;
7092
+ const { initialTenant = "feishu", prefillAppId, errorMessage: errorMessage5 } = opts;
7049
7093
  const bodyElements = [];
7050
- if (errorMessage4) {
7094
+ if (errorMessage5) {
7051
7095
  bodyElements.push({
7052
7096
  tag: "markdown",
7053
- content: `\u274C **\u6821\u9A8C\u5931\u8D25**\uFF1A${errorMessage4}`
7097
+ content: `\u274C **\u6821\u9A8C\u5931\u8D25**\uFF1A${errorMessage5}`
7054
7098
  });
7055
7099
  }
7056
7100
  bodyElements.push({
@@ -8143,12 +8187,18 @@ function renderCard(state, options = {}) {
8143
8187
  const mins = state.idleTimeoutMinutes ?? 0;
8144
8188
  elements.push(noteMd(`_\u23F1 ${mins} \u5206\u949F\u65E0\u54CD\u5E94,\u5DF2\u81EA\u52A8\u7EC8\u6B62_`));
8145
8189
  } else if (state.terminal === "error" && state.errorMsg) {
8146
- elements.push(noteMd(`\u26A0\uFE0F agent \u5931\u8D25\uFF1A${state.errorMsg}`));
8190
+ elements.push(
8191
+ noteMd(
8192
+ state.apiError ? `\u26A0\uFE0F \u4E0A\u6E38 API \u8FDE\u63A5\u4E2D\u65AD\uFF1A${state.errorMsg}
8193
+
8194
+ _\u4E0D\u662F\u8FD9\u6761\u8BF7\u6C42\u7684\u95EE\u9898\uFF0C\u91CD\u53D1\u5373\u53EF\u7EE7\u7EED\uFF08\u4F1A\u8BDD\u4E0A\u4E0B\u6587\u6CA1\u4E22\uFF09\u3002_` : `\u26A0\uFE0F agent \u5931\u8D25\uFF1A${state.errorMsg}`
8195
+ )
8196
+ );
8147
8197
  } else if (state.terminal === "done" && elements.length === 0) {
8148
8198
  elements.push(noteMd("_\uFF08\u672A\u8FD4\u56DE\u5185\u5BB9\uFF09_"));
8149
8199
  }
8150
8200
  if (state.terminal === "running") {
8151
- if (state.footer) elements.push(footerStatus(state.footer));
8201
+ if (state.footer) elements.push(footerStatus(state.footer, state.apiRetry));
8152
8202
  elements.push(stopButton(options));
8153
8203
  }
8154
8204
  return deepMaskEmails({
@@ -8260,15 +8310,21 @@ function stopButton(options) {
8260
8310
  behaviors: [{ type: "callback", value }]
8261
8311
  };
8262
8312
  }
8263
- function footerStatus(status) {
8264
- const text = status === "thinking" ? "\u{1F9E0} \u6B63\u5728\u601D\u8003" : status === "tool_running" ? "\u{1F9F0} \u6B63\u5728\u8C03\u7528\u5DE5\u5177" : "\u270D\uFE0F \u6B63\u5728\u8F93\u51FA";
8313
+ function footerStatus(status, apiRetry) {
8314
+ const text = status === "thinking" ? "\u{1F9E0} \u6B63\u5728\u601D\u8003" : status === "tool_running" ? "\u{1F9F0} \u6B63\u5728\u8C03\u7528\u5DE5\u5177" : status === "api_retry" ? apiRetryText(apiRetry) : "\u270D\uFE0F \u6B63\u5728\u8F93\u51FA";
8265
8315
  return noteMd(text);
8266
8316
  }
8317
+ function apiRetryText(apiRetry) {
8318
+ if (!apiRetry) return "\u{1F50C} API \u8FDE\u63A5\u4E2D\u65AD,\u6B63\u5728\u91CD\u8BD5";
8319
+ const of = apiRetry.maxRetries ? `/${apiRetry.maxRetries}` : "";
8320
+ return `\u{1F50C} API \u8FDE\u63A5\u4E2D\u65AD,\u6B63\u5728\u91CD\u8BD5 (${apiRetry.attempt}${of})`;
8321
+ }
8267
8322
  function summaryText(state) {
8268
8323
  if (state.terminal === "interrupted") return "\u5DF2\u4E2D\u65AD";
8269
8324
  if (state.terminal === "idle_timeout") return "\u5DF2\u8D85\u65F6";
8270
- if (state.terminal === "error") return "\u51FA\u9519";
8325
+ if (state.terminal === "error") return state.apiError ? "API \u8FDE\u63A5\u4E2D\u65AD" : "\u51FA\u9519";
8271
8326
  if (state.terminal === "done") return "\u5DF2\u5B8C\u6210";
8327
+ if (state.footer === "api_retry") return "API \u91CD\u8BD5\u4E2D";
8272
8328
  if (state.footer === "tool_running") return "\u6B63\u5728\u8C03\u7528\u5DE5\u5177";
8273
8329
  if (state.footer === "streaming") return "\u6B63\u5728\u8F93\u51FA";
8274
8330
  return "\u601D\u8003\u4E2D";
@@ -8346,12 +8402,27 @@ function reduce(state, evt) {
8346
8402
  });
8347
8403
  return { ...state, blocks };
8348
8404
  }
8405
+ // Claude Code retries the upstream API up to 10 times over ~3 minutes
8406
+ // without emitting anything else. Park the attempt in the footer so the
8407
+ // card shows progress instead of freezing; any subsequent real event
8408
+ // moves the footer on and hides it again.
8409
+ case "api_retry": {
8410
+ return {
8411
+ ...state,
8412
+ apiRetry: {
8413
+ attempt: evt.attempt,
8414
+ ...evt.maxRetries !== void 0 ? { maxRetries: evt.maxRetries } : {}
8415
+ },
8416
+ footer: "api_retry"
8417
+ };
8418
+ }
8349
8419
  case "error": {
8350
8420
  const terminal = evt.terminationReason === "interrupted" ? "interrupted" : evt.terminationReason === "timeout" ? "idle_timeout" : "error";
8351
8421
  return {
8352
8422
  ...state,
8353
8423
  terminal,
8354
8424
  errorMsg: terminal === "error" ? evt.message : state.errorMsg,
8425
+ ...terminal === "error" && evt.apiError ? { apiError: true } : {},
8355
8426
  footer: null
8356
8427
  };
8357
8428
  }
@@ -8555,7 +8626,7 @@ async function listCodexThreadHistory(options) {
8555
8626
  const fail = (err) => {
8556
8627
  if (settled) return;
8557
8628
  reject4(
8558
- err instanceof CodexHistoryError ? err : new CodexHistoryError("spawn-failed", errorMessage3(err))
8629
+ err instanceof CodexHistoryError ? err : new CodexHistoryError("spawn-failed", errorMessage4(err))
8559
8630
  );
8560
8631
  cleanup({ kill: true });
8561
8632
  };
@@ -8737,7 +8808,7 @@ function numberValue2(input) {
8737
8808
  function recordValue2(input) {
8738
8809
  return input && typeof input === "object" && !Array.isArray(input) ? input : void 0;
8739
8810
  }
8740
- function errorMessage3(err) {
8811
+ function errorMessage4(err) {
8741
8812
  return err instanceof Error ? err.message : String(err);
8742
8813
  }
8743
8814
 
@@ -10865,9 +10936,9 @@ async function submitAccount(ctx) {
10865
10936
  );
10866
10937
  forgetManagedCard(formMsgId);
10867
10938
  };
10868
- const finishFailure = async (errorMessage4) => {
10939
+ const finishFailure = async (errorMessage5) => {
10869
10940
  await waitForSettle();
10870
- await updateManagedCard(channel, formMsgId, accountFailureCard(errorMessage4)).catch((err) => console.warn("[account] mark old form failed:", err));
10941
+ await updateManagedCard(channel, formMsgId, accountFailureCard(errorMessage5)).catch((err) => console.warn("[account] mark old form failed:", err));
10871
10942
  forgetManagedCard(formMsgId);
10872
10943
  const retry = accountFormCard({
10873
10944
  initialTenant: tenant,
@@ -11989,9 +12060,13 @@ function renderText(state) {
11989
12060
  const mins = state.idleTimeoutMinutes ?? 0;
11990
12061
  parts.push(`_\u23F1 ${mins} \u5206\u949F\u65E0\u54CD\u5E94,\u5DF2\u81EA\u52A8\u7EC8\u6B62_`);
11991
12062
  } else if (state.terminal === "error" && state.errorMsg) {
11992
- parts.push(`\u26A0\uFE0F agent \u5931\u8D25:${state.errorMsg}`);
12063
+ parts.push(
12064
+ state.apiError ? `\u26A0\uFE0F \u4E0A\u6E38 API \u8FDE\u63A5\u4E2D\u65AD:${state.errorMsg}
12065
+
12066
+ _\u4E0D\u662F\u8FD9\u6761\u8BF7\u6C42\u7684\u95EE\u9898,\u91CD\u53D1\u5373\u53EF\u7EE7\u7EED(\u4F1A\u8BDD\u4E0A\u4E0B\u6587\u6CA1\u4E22)\u3002_` : `\u26A0\uFE0F agent \u5931\u8D25:${state.errorMsg}`
12067
+ );
11993
12068
  } else if (state.terminal === "running" && state.footer) {
11994
- parts.push(footerLine(state.footer));
12069
+ parts.push(footerLine(state.footer, state.apiRetry));
11995
12070
  }
11996
12071
  return maskEmails(parts.join("\n\n"));
11997
12072
  }
@@ -12004,9 +12079,14 @@ function renderBlock(block) {
12004
12079
  function toolLine(tool) {
12005
12080
  return `> ${toolHeaderText(tool)}`;
12006
12081
  }
12007
- function footerLine(status) {
12082
+ function footerLine(status, apiRetry) {
12008
12083
  if (status === "thinking") return "_\u{1F9E0} \u6B63\u5728\u601D\u8003\u2026_";
12009
12084
  if (status === "tool_running") return "_\u{1F9F0} \u6B63\u5728\u8C03\u7528\u5DE5\u5177\u2026_";
12085
+ if (status === "api_retry") {
12086
+ const of = apiRetry?.maxRetries ? `/${apiRetry.maxRetries}` : "";
12087
+ const attempt = apiRetry ? ` (${apiRetry.attempt}${of})` : "";
12088
+ return `_\u{1F50C} API \u8FDE\u63A5\u4E2D\u65AD,\u6B63\u5728\u91CD\u8BD5${attempt}\u2026_`;
12089
+ }
12010
12090
  return "_\u270D\uFE0F \u6B63\u5728\u8F93\u51FA\u2026_";
12011
12091
  }
12012
12092