aai-lark-bridge 0.2.1 → 0.2.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/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";
@@ -851,9 +851,19 @@ function* translateEvent(raw) {
851
851
  inputTokens: evt.usage.input_tokens,
852
852
  outputTokens: evt.usage.output_tokens,
853
853
  cachedInputTokens: evt.usage.cache_read_input_tokens,
854
- costUsd: evt.total_cost_usd
854
+ costUsd: evt.total_cost_usd,
855
+ ...typeof evt.subtype === "string" ? { subtype: evt.subtype } : {}
855
856
  };
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
864
+ };
865
+ return;
866
+ }
857
867
  if (typeof evt.subtype === "string" && evt.subtype !== "success") {
858
868
  const detail = typeof evt.result === "string" && evt.result.trim() ? `: ${evt.result}` : "";
859
869
  yield {
@@ -958,7 +968,13 @@ var ClaudeAdapter = class {
958
968
  if (!opts.cwd) {
959
969
  throw new Error("cwd is required for ClaudeAdapter.run");
960
970
  }
961
- 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
+ );
962
978
  const args = [
963
979
  "-p",
964
980
  "--output-format",
@@ -971,6 +987,9 @@ var ClaudeAdapter = class {
971
987
  ];
972
988
  if (opts.sessionId) args.push("--resume", opts.sessionId);
973
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");
974
993
  let child;
975
994
  try {
976
995
  child = spawnProcess(this.binary, args, {
@@ -987,7 +1006,11 @@ var ClaudeAdapter = class {
987
1006
  cwd: opts.cwd ?? process.cwd(),
988
1007
  hasSession: Boolean(opts.sessionId),
989
1008
  promptChars: opts.prompt.length,
990
- 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 } : {}
991
1014
  });
992
1015
  const stderrTail = new StderrTail();
993
1016
  let runtimeError = null;
@@ -1016,7 +1039,7 @@ var ClaudeAdapter = class {
1016
1039
  const stopGraceMs = opts.stopGraceMs ?? 5e3;
1017
1040
  return {
1018
1041
  runId: opts.runId,
1019
- events: createEventStream(child, stderrTail, () => runtimeError),
1042
+ events: createEventStream(child, stderrTail, () => runtimeError, opts.maxBudgetUsd),
1020
1043
  async stop() {
1021
1044
  if (child.exitCode !== null || child.signalCode !== null) return;
1022
1045
  log.info("agent", "stop-sigterm", { pid: child.pid ?? null, graceMs: stopGraceMs });
@@ -1058,7 +1081,7 @@ var ClaudeAdapter = class {
1058
1081
  };
1059
1082
  }
1060
1083
  };
1061
- async function* createEventStream(child, stderrTail, getError) {
1084
+ async function* createEventStream(child, stderrTail, getError, maxBudgetUsd) {
1062
1085
  if (!child.pid) {
1063
1086
  const err = getError();
1064
1087
  yield {
@@ -1091,7 +1114,8 @@ async function* createEventStream(child, stderrTail, getError) {
1091
1114
  }
1092
1115
  for (const event of translateEvent(parsed)) {
1093
1116
  if (event.type === "done" || event.type === "error") sawTerminal = true;
1094
- yield event;
1117
+ if (event.type === "error" && event.budgetHit && maxBudgetUsd !== void 0) yield { ...event, budgetUsd: maxBudgetUsd };
1118
+ else yield event;
1095
1119
  }
1096
1120
  }
1097
1121
  } finally {