aai-lark-bridge 0.2.0 → 0.2.3

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/index.d.ts CHANGED
@@ -66,6 +66,8 @@ type AgentEvent = {
66
66
  cachedInputTokens?: number;
67
67
  reasoningOutputTokens?: number;
68
68
  costUsd?: number;
69
+ /** claude's result subtype (`success`, `error_max_budget_usd`, …), for the spend ledger. */
70
+ subtype?: string;
69
71
  }
70
72
  /**
71
73
  * The agent lost its connection to the upstream model API and is retrying
@@ -102,6 +104,14 @@ type AgentEvent = {
102
104
  * `apiError`.
103
105
  */
104
106
  authError?: boolean;
107
+ /**
108
+ * The run stopped because it reached `--max-budget-usd` (claude reports
109
+ * `subtype: "error_max_budget_usd"`). Not a failure of the request: the
110
+ * session is intact and the next message continues it. intent 0030 R-B03.
111
+ */
112
+ budgetHit?: boolean;
113
+ /** The per-run cap that was hit, in USD (set by the adapter when known). */
114
+ budgetUsd?: number;
105
115
  };
106
116
  interface AgentRunOptions {
107
117
  runId: string;
@@ -121,6 +131,22 @@ interface AgentRunOptions {
121
131
  * are adapter-specific.
122
132
  */
123
133
  stopGraceMs?: number;
134
+ /**
135
+ * Per-run spend cap in USD, passed to claude as `--max-budget-usd`. claude
136
+ * checks it per invocation and `total_cost_usd` is cumulative over a resumed
137
+ * session, so callers pass the cap for this run only, never adding what the
138
+ * session already spent (intent 0030 R-B14). Codex ignores it.
139
+ */
140
+ maxBudgetUsd?: number;
141
+ /** claude `--settings <file>` (task bindings, intent 0030 R-C05). Codex ignores it. */
142
+ settingsFile?: string;
143
+ /**
144
+ * File whose content is appended to the bridge system prompt (still one
145
+ * `--append-system-prompt-file`). Codex ignores it.
146
+ */
147
+ extraSystemPromptFile?: string;
148
+ /** claude `--strict-mcp-config`: no MCP servers beyond those passed explicitly. Codex ignores it. */
149
+ strictMcp?: boolean;
124
150
  }
125
151
  interface AgentRun {
126
152
  readonly runId: string;
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/bridge/agent/claude/adapter.ts
2
- import { mkdtempSync, rmSync, writeFileSync } from "fs";
2
+ import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs";
3
3
  import { tmpdir } from "os";
4
4
  import { join as join3 } from "path";
5
5
  import { createInterface } from "readline";
@@ -554,6 +554,14 @@ bridge \u4F1A\u7ED9\u4F60\u7684\u5B50\u8FDB\u7A0B\u6CE8\u5165\u5F53\u524D\u8FD0\
554
554
 
555
555
  \u914D\u7F6E\u6587\u4EF6\u53EF\u80FD\u662F\u591A profile \u7ED3\u6784,\u4E0D\u8981\u5047\u8BBE\u6839\u5C42\u4E00\u5B9A\u6709\u65E7\u7248\u5355 profile \u7684 \`accounts.app\`;\u786E\u5B9E\u9700\u8981\u8BFB\u53D6\u914D\u7F6E\u65F6\u6309\u5F53\u524D profile \u53D6\u503C,\u4E14\u4E0D\u8981\u8F93\u51FA\u5BC6\u94A5\u3002
556
556
 
557
+ ## \u98DE\u4E66\u4EFB\u52A1\u7684\u76EE\u6807\u4E0E\u6267\u884C\u8BF4\u660E
558
+
559
+ - \u65B0\u5EFA\u98DE\u4E66\u4EFB\u52A1\u6216\u5B9E\u8D28\u6027\u6821\u51C6\u4EFB\u52A1\u65F6\uFF0C\u5148\u8BFB lark-task skill\u3002\u6807\u9898\u6982\u62EC\u4EA4\u4ED8\u7ED3\u679C\uFF0Cdescription \u5FC5\u987B\u5199\u5165\u53EF\u72EC\u7ACB\u9605\u8BFB\u7684\u6267\u884C\u8BF4\u660E\uFF0C\u4E0D\u80FD\u53EA\u91CD\u590D\u6807\u9898\u6216\u8D34\u4F1A\u8BAE\u94FE\u63A5\u3002
560
+ - \u63CF\u8FF0\u5305\u542B\uFF1A\u4EFB\u52A1\u76EE\u6807\uFF08\u89E3\u51B3\u4EC0\u4E48\u95EE\u9898\u3001\u9884\u671F\u7ED3\u679C\uFF09\u3001\u4E1A\u52A1\u89C4\u5219\uFF08\u6761\u4EF6\u3001\u4F18\u5148\u7EA7\u3001\u7F3A\u5931/\u5F02\u5E38\u503C\u53CA\u8FB9\u754C\uFF09\u3001\u6539\u52A8\u903B\u8F91\u4E0E\u8303\u56F4\uFF08\u5F53\u524D\u884C\u4E3A\u2192\u76EE\u6807\u884C\u4E3A\u3001\u6D89\u53CA\u7684\u6D41\u7A0B/\u7CFB\u7EDF\u548C\u4E00\u81F4\u6027\u8981\u6C42\uFF09\u3001\u9A8C\u6536\u6807\u51C6\uFF08\u53EF\u9A8C\u8BC1\u7ED3\u679C\u548C\u5FC5\u8981\u793A\u4F8B\uFF09\u3001\u5F85\u786E\u8BA4\u4E8B\u9879\u3001\u6765\u6E90\u4F9D\u636E\uFF08\u7528\u6237\u786E\u8BA4\u3001\u6587\u6863/\u6D88\u606F/\u4F1A\u8BAE\u94FE\u63A5\u53CA\u53EF\u7528\u7684\u53D1\u8A00\u65F6\u95F4\u70B9\uFF09\u3002\u975E\u5F00\u53D1\u4EFB\u52A1\u628A\u6539\u52A8\u903B\u8F91\u5199\u6210\u6267\u884C\u6B65\u9AA4\u4E0E\u4EA4\u4ED8\u7269\uFF0C\u4E0D\u5F3A\u586B\u4EE3\u7801\u7EC6\u8282\u3002
561
+ - \u7528\u6237\u672C\u8F6E\u660E\u786E\u7684\u6821\u51C6\u4F18\u5148\u4E8E\u65E7\u4EFB\u52A1\u6216\u81EA\u52A8\u6458\u8981\u3002\u4F1A\u8BAE\u4EFB\u52A1\u6838\u5BF9\u9010\u5B57\u7A3F\uFF1B\u65E0\u6CD5\u83B7\u53D6\u65F6\u5982\u5B9E\u6807\u660E\u4EC5\u4F9D\u636E\u6458\u8981\u3002\u533A\u5206\u5DF2\u786E\u8BA4\u51B3\u7B56\u3001\u5EFA\u8BAE\u548C\u5F85\u8BA8\u8BBA\u65B9\u6848\uFF0C\u4E0D\u5C06\u591A\u4E2A\u72EC\u7ACB\u9700\u6C42\u6DF7\u6210\u4E00\u6761\uFF0C\u4E0D\u4E3A\u8865\u9F50\u680F\u76EE\u7F16\u9020\u4E1A\u52A1\u89C4\u5219\u3001\u4ED3\u5E93/\u5B57\u6BB5\u540D\u3001\u8D1F\u8D23\u4EBA\u6216\u622A\u6B62\u65E5\u671F\u3002\u4FE1\u606F\u7F3A\u5931\u5199\u201C\u5F85\u786E\u8BA4\u201D\u53CA\u5176\u5F71\u54CD\uFF0C\u5DF2\u6709\u4FE1\u606F\u4ECD\u5B8C\u6574\u5199\u5165\u3002
562
+ - \u201C\u6240\u6709\u540C\u7C7B\u903B\u8F91\u540C\u6B65\u201D\u8981\u5217\u660E\u53D7\u5F71\u54CD\u7684\u5C55\u793A\u3001\u8BA1\u7B97\u3001\u6C47\u603B\u3001\u5BFC\u51FA\u6216\u5176\u4ED6\u6D88\u8D39\u5165\u53E3\uFF1B\u4EC5\u5217\u6709\u4F9D\u636E\u7684\u8303\u56F4\u3002\u5199\u5165/\u540C\u6B65\u89C4\u5219\u4E0E\u8BFB\u53D6/\u6D88\u8D39\u89C4\u5219\u5206\u522B\u8BF4\u660E\uFF0C\u907F\u514D\u628A\u201C\u4E0D\u66F4\u65B0\u5B57\u6BB5\u201D\u8BEF\u89E3\u6210\u201C\u7EE7\u7EED\u7528\u65E7\u503C\u201D\u6216\u201C\u7F3A\u5931\u8865\u96F6\u201D\u3002
563
+ - \u6309\u7528\u6237\u5DF2\u6388\u6743\u7684\u8303\u56F4\u6267\u884C\uFF1B\u53EA\u6539\u622A\u6B62\u65E5\u671F\u3001\u8D1F\u8D23\u4EBA\u6216\u72B6\u6001\u65F6\uFF0C\u4E0D\u987A\u5E26\u91CD\u5199\u5DF2\u6709\u63CF\u8FF0\u3002\u66F4\u65B0\u524D\u8BFB\u53D6\u539F\u4EFB\u52A1\u5E76\u4FDD\u7559\u6709\u6548\u4E0A\u4E0B\u6587\uFF1B\u521B\u5EFA/\u66F4\u65B0\u540E\u8BFB\u56DE\u6807\u9898\u4E0E\u63CF\u8FF0\u6838\u9A8C\uFF0C\u8FD4\u56DE\u98DE\u4E66\u4EFB\u52A1\u94FE\u63A5\u3002
564
+
557
565
  ## \u6545\u969C\u8BCA\u65AD\u4E0E\u5B8C\u6210\u72B6\u6001
558
566
 
559
567
  - \u7528\u6237\u4ECE\u98DE\u4E66\u9605\u8BFB\u7B54\u590D\uFF0C\u65E0\u6CD5\u6253\u5F00\u672C\u673A\u7EDD\u5BF9\u8DEF\u5F84\u3001file:// \u6216\u672C\u673A\u7F16\u8F91\u5668\u94FE\u63A5\u3002\u4EE3\u7801\u4EA4\u4ED8\u63D0\u4F9B\u5DF2\u521B\u5EFA\u5E76\u6838\u5B9E\u7684 MR URL\uFF1B\u9700\u8981\u4EA4\u4ED8\u6587\u4EF6\u65F6\u6309\u4EFB\u52A1\u6388\u6743\u4E0A\u4F20\u5230\u98DE\u4E66\u5E76\u6838\u9A8C\u8FD4\u56DE\u94FE\u63A5\u6216\u9644\u4EF6\u3002\u53EA\u5199\u5165\u672C\u673A\u65F6\u660E\u786E\u201C\u4EC5\u4FDD\u5B58\u5728\u672C\u673A\uFF0C\u5C1A\u672A\u4E0A\u4F20\u201D\uFF0C\u53EF\u7528\u7EAF\u6587\u672C\u8DEF\u5F84\u4F9B\u5B9A\u4F4D\uFF0C\u4F46\u4E0D\u80FD\u5305\u88C5\u6210\u5DF2\u4EA4\u4ED8\u7684\u53EF\u70B9\u51FB\u94FE\u63A5\u3002\u51C6\u5907\u597D MR \u63CF\u8FF0\u4E0D\u7B49\u4E8E\u5DF2\u63D0\u4EA4 MR\u3002
@@ -843,8 +851,18 @@ function* translateEvent(raw) {
843
851
  inputTokens: evt.usage.input_tokens,
844
852
  outputTokens: evt.usage.output_tokens,
845
853
  cachedInputTokens: evt.usage.cache_read_input_tokens,
846
- costUsd: evt.total_cost_usd
854
+ costUsd: evt.total_cost_usd,
855
+ ...typeof evt.subtype === "string" ? { subtype: evt.subtype } : {}
856
+ };
857
+ }
858
+ if (evt.subtype === "error_max_budget_usd") {
859
+ yield {
860
+ type: "error",
861
+ message: "claude stopped at the per-run budget cap",
862
+ terminationReason: "failed",
863
+ budgetHit: true
847
864
  };
865
+ return;
848
866
  }
849
867
  if (typeof evt.subtype === "string" && evt.subtype !== "success") {
850
868
  const detail = typeof evt.result === "string" && evt.result.trim() ? `: ${evt.result}` : "";
@@ -950,7 +968,13 @@ var ClaudeAdapter = class {
950
968
  if (!opts.cwd) {
951
969
  throw new Error("cwd is required for ClaudeAdapter.run");
952
970
  }
953
- const systemPromptFile = writeSystemPromptFile(buildBridgeSystemPrompt(this.botIdentity));
971
+ const bridgePrompt = buildBridgeSystemPrompt(this.botIdentity);
972
+ const extraPrompt = opts.extraSystemPromptFile ? readFileSync(opts.extraSystemPromptFile, "utf8") : "";
973
+ const systemPromptFile = writeSystemPromptFile(
974
+ extraPrompt.trim() ? `${bridgePrompt}
975
+
976
+ ${extraPrompt}` : bridgePrompt
977
+ );
954
978
  const args = [
955
979
  "-p",
956
980
  "--output-format",
@@ -963,6 +987,9 @@ var ClaudeAdapter = class {
963
987
  ];
964
988
  if (opts.sessionId) args.push("--resume", opts.sessionId);
965
989
  if (opts.model) args.push("--model", opts.model);
990
+ if (opts.maxBudgetUsd !== void 0 && opts.maxBudgetUsd > 0) args.push("--max-budget-usd", String(opts.maxBudgetUsd));
991
+ if (opts.settingsFile) args.push("--settings", opts.settingsFile);
992
+ if (opts.strictMcp) args.push("--strict-mcp-config");
966
993
  let child;
967
994
  try {
968
995
  child = spawnProcess(this.binary, args, {
@@ -979,7 +1006,11 @@ var ClaudeAdapter = class {
979
1006
  cwd: opts.cwd ?? process.cwd(),
980
1007
  hasSession: Boolean(opts.sessionId),
981
1008
  promptChars: opts.prompt.length,
982
- model: opts.model
1009
+ model: opts.model,
1010
+ permissionMode: opts.permissionMode ?? CLAUDE_DEFAULT_PERMISSION_MODE,
1011
+ ...opts.maxBudgetUsd !== void 0 ? { maxBudgetUsd: opts.maxBudgetUsd } : {},
1012
+ ...opts.settingsFile ? { settings: opts.settingsFile } : {},
1013
+ ...opts.strictMcp ? { strictMcp: true } : {}
983
1014
  });
984
1015
  const stderrTail = new StderrTail();
985
1016
  let runtimeError = null;
@@ -1008,7 +1039,7 @@ var ClaudeAdapter = class {
1008
1039
  const stopGraceMs = opts.stopGraceMs ?? 5e3;
1009
1040
  return {
1010
1041
  runId: opts.runId,
1011
- events: createEventStream(child, stderrTail, () => runtimeError),
1042
+ events: createEventStream(child, stderrTail, () => runtimeError, opts.maxBudgetUsd),
1012
1043
  async stop() {
1013
1044
  if (child.exitCode !== null || child.signalCode !== null) return;
1014
1045
  log.info("agent", "stop-sigterm", { pid: child.pid ?? null, graceMs: stopGraceMs });
@@ -1050,7 +1081,7 @@ var ClaudeAdapter = class {
1050
1081
  };
1051
1082
  }
1052
1083
  };
1053
- async function* createEventStream(child, stderrTail, getError) {
1084
+ async function* createEventStream(child, stderrTail, getError, maxBudgetUsd) {
1054
1085
  if (!child.pid) {
1055
1086
  const err = getError();
1056
1087
  yield {
@@ -1083,7 +1114,8 @@ async function* createEventStream(child, stderrTail, getError) {
1083
1114
  }
1084
1115
  for (const event of translateEvent(parsed)) {
1085
1116
  if (event.type === "done" || event.type === "error") sawTerminal = true;
1086
- yield event;
1117
+ if (event.type === "error" && event.budgetHit && maxBudgetUsd !== void 0) yield { ...event, budgetUsd: maxBudgetUsd };
1118
+ else yield event;
1087
1119
  }
1088
1120
  }
1089
1121
  } finally {