konoha-mcp 2.2.23 → 2.2.25

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.
Files changed (132) hide show
  1. package/NOTICE +14 -8
  2. package/README.md +20 -5
  3. package/apps/web/build/adapter-node.js +1 -1
  4. package/apps/web/build/build-meta.json +2 -2
  5. package/apps/web/build/client/_app/immutable/entry/{app.TYH87iYy.js → app.DtoBIrs4.js} +2 -2
  6. package/apps/web/build/client/_app/immutable/entry/app.DtoBIrs4.js.br +0 -0
  7. package/apps/web/build/client/_app/immutable/entry/app.DtoBIrs4.js.gz +0 -0
  8. package/apps/web/build/client/_app/immutable/nodes/{0.D7dPIEWA.js → 0.B8Iw7Xl1.js} +1 -1
  9. package/apps/web/build/client/_app/immutable/nodes/0.B8Iw7Xl1.js.br +0 -0
  10. package/apps/web/build/client/_app/immutable/nodes/{0.D7dPIEWA.js.gz → 0.B8Iw7Xl1.js.gz} +0 -0
  11. package/apps/web/build/client/_app/immutable/nodes/{13.TOXVaTBr.js → 13.CQB_Ed6W.js} +1 -1
  12. package/apps/web/build/client/_app/immutable/nodes/13.CQB_Ed6W.js.br +0 -0
  13. package/apps/web/build/client/_app/immutable/nodes/{13.TOXVaTBr.js.gz → 13.CQB_Ed6W.js.gz} +0 -0
  14. package/apps/web/build/client/_app/version.json +1 -1
  15. package/apps/web/build/client/_app/version.json.br +0 -0
  16. package/apps/web/build/client/_app/version.json.gz +0 -0
  17. package/apps/web/build/server/chunks/instance.js +3 -3
  18. package/apps/web/build/server/chunks/internal.js +1 -1
  19. package/apps/web/build/server/entries/pages/_layout.svelte.js +1 -1
  20. package/apps/web/build/server/entries/pages/_layout.svelte.js.map +1 -1
  21. package/apps/web/build/server/entries/pages/docs/_page.svelte.js +1 -1
  22. package/apps/web/build/server/entries/pages/docs/_page.svelte.js.map +1 -1
  23. package/apps/web/build/server/manifest-full.js +1 -1
  24. package/apps/web/build/server/manifest.js +1 -1
  25. package/apps/web/build/server/nodes/0.js +1 -1
  26. package/apps/web/build/server/nodes/13.js +1 -1
  27. package/apps/web/build/server/server.js +1 -1
  28. package/apps/web/package.json +1 -1
  29. package/assets/demo-agy.webp +0 -0
  30. package/assets/demo-claude.webp +0 -0
  31. package/assets/demo-codex.webp +0 -0
  32. package/assets/demo-commandcode.webp +0 -0
  33. package/assets/demo-cursor.webp +0 -0
  34. package/assets/demo-opencode.webp +0 -0
  35. package/assets/demo-pi.webp +0 -0
  36. package/assets/demo-skill-embed.webp +0 -0
  37. package/assets/demo-skills.webp +0 -0
  38. package/assets/demo-web.webp +0 -0
  39. package/assets/demo.webp +0 -0
  40. package/assets/konoha-akatsuki-flow.webp +0 -0
  41. package/assets/konoha-anbu-flow.webp +0 -0
  42. package/assets/konoha-flow-12-cli-upgrade-progress-engine.webp +0 -0
  43. package/assets/konoha-jonin-flow.webp +0 -0
  44. package/assets/konoha-kage-gate.webp +0 -0
  45. package/assets/konoha-langgraph-flow.webp +0 -0
  46. package/assets/konoha-orchestration-flow.webp +0 -0
  47. package/assets/konoha-qa-flow.webp +0 -0
  48. package/assets/konoha-telegram-flow.webp +0 -0
  49. package/assets/testing.webp +0 -0
  50. package/bin/commands/bridge.js +62 -4
  51. package/bin/commands/doctor.js +2 -0
  52. package/bin/commands/doctor_services.js +57 -0
  53. package/bin/commands/init.js +29 -8
  54. package/bin/commands/init_clients.js +95 -8
  55. package/bin/commands/init_migration.js +32 -1
  56. package/bin/lib/config_defaults.js +3 -0
  57. package/bin/lib/runtime_install.js +147 -54
  58. package/node_modules/jszip/package.json +2 -2
  59. package/node_modules/string_decoder/package.json +1 -1
  60. package/package.json +10 -3
  61. package/patches/gpt-tokenizer@4.1.0.patch +15 -0
  62. package/patches/jszip@3.10.2.patch +11 -0
  63. package/patches/string_decoder@1.3.0.patch +12 -0
  64. package/src/agent_contract.js +2 -2
  65. package/src/agent_deployer.js +48 -35
  66. package/src/agent_file_safety.js +57 -0
  67. package/src/antigravity_subagent_hook.js +30 -12
  68. package/src/bridge/gateway.js +2 -0
  69. package/src/bridge/gateway_adapters.js +155 -29
  70. package/src/bridge/handlers/anthropic.js +95 -26
  71. package/src/bridge/handlers/openai.js +18 -5
  72. package/src/bridge/sidecar/raw.js +10 -2
  73. package/src/bridge/stream_usage.js +66 -0
  74. package/src/chunin_knowledge_ddl.js +95 -0
  75. package/src/client_commands.js +190 -0
  76. package/src/db_bridges.js +53 -7
  77. package/src/db_schema.js +14 -2
  78. package/src/deploy_installer.js +212 -11
  79. package/src/deploy_utils.js +4 -0
  80. package/src/file_tools/common.js +65 -3
  81. package/src/file_tools_mcp.js +17 -9
  82. package/src/file_tools_router.js +67 -6
  83. package/src/global_skill_sync.js +3 -1
  84. package/src/mcp/chunin_knowledge.js +509 -0
  85. package/src/mcp/langgraph/edges/router.js +3 -0
  86. package/src/mcp/langgraph/index.js +22 -3
  87. package/src/mcp/langgraph/nodes/dispatch.js +3 -0
  88. package/src/mcp/langgraph/nodes/execute.js +87 -13
  89. package/src/mcp/langgraph/nodes/review.js +13 -1
  90. package/src/mcp/langgraph/slice_executor_registry.js +22 -0
  91. package/src/mcp/langgraph/state.js +5 -0
  92. package/src/mcp/lazy_subagent.js +11 -1
  93. package/src/mcp/memory_project_context.js +4 -1
  94. package/src/mcp/memory_subagent_runner.js +2 -1
  95. package/src/mcp/skills_common.js +1 -0
  96. package/src/mcp/subagent_spawner.js +7 -0
  97. package/src/mcp/todo_walkthrough_sync.js +7 -0
  98. package/src/mcp/tool_dispatch.js +18 -1
  99. package/src/mcp/web_search.js +27 -3
  100. package/src/mcp/workflow_completion_helpers.js +6 -1
  101. package/src/mcp/workflow_gates.js +118 -5
  102. package/src/mcp/workflow_sannin.js +9 -3
  103. package/src/mcp_clients/common.js +31 -0
  104. package/src/mcp_tool_manifest.json +36 -1
  105. package/src/pi_fs_helpers.js +31 -0
  106. package/src/pi_manager.js +68 -28
  107. package/src/provider_usage_logger.js +92 -0
  108. package/src/schema_ddl.js +17 -1
  109. package/src/templates/AGENTS.md +1 -1
  110. package/src/templates/GEMINI.md +1 -1
  111. package/src/templates/skills/chunin-skill/SKILL.md +34 -0
  112. package/src/templates/skills/chunin-skill/references/deep-research-analyst.md +27 -10
  113. package/src/templates/skills/kage-skill/SKILL.md +1 -1
  114. package/src/templates/skills/konoha/SKILL.md +23 -165
  115. package/src/templates/skills/konoha/references/contract.md +2 -2
  116. package/src/templates/skills/konoha/references/peragent-usage.md +1 -1
  117. package/src/token_usage.js +57 -0
  118. package/src/tools_savings_logger.js +26 -8
  119. package/src/url_canonical.js +58 -0
  120. package/src/vector_pipeline.js +6 -4
  121. package/src/vector_search.js +4 -2
  122. package/src/web/controllers/core_controller.js +10 -1
  123. package/src/workflow/jonin-builder/live_browser_monitor.js +23 -7
  124. package/apps/web/build/client/_app/immutable/entry/app.TYH87iYy.js.br +0 -0
  125. package/apps/web/build/client/_app/immutable/entry/app.TYH87iYy.js.gz +0 -0
  126. package/apps/web/build/client/_app/immutable/nodes/0.D7dPIEWA.js.br +0 -0
  127. package/apps/web/build/client/_app/immutable/nodes/13.TOXVaTBr.js.br +0 -0
  128. package/src/templates/skills/konoha/references/architecture.md +0 -99
  129. package/src/templates/skills/konoha/references/benchmark.md +0 -123
  130. package/src/templates/skills/konoha/references/docs.md +0 -46
  131. package/src/templates/skills/konoha/references/test.md +0 -71
  132. package/src/templates/skills/konoha/references/workflow.md +0 -104
@@ -39,22 +39,36 @@ const GLOBAL_IDE_AGENTS_DIR = path.join(HOME, '.gemini', 'antigravity-ide', 'age
39
39
  const GLOBAL_CONFIG_AGENTS_DIR = path.join(HOME, '.gemini', 'antigravity-cli', 'mcp');
40
40
 
41
41
  /**
42
- * Official ninja roster — the ONLY agent directory names allowed inside a
43
- * session's `.agents/agents/` directory. Anything else (e.g. a leaked
44
- * `define_subagent` artifact from a previous orchestrator run, or stale
45
- * `test-*` debug agents) is purged by `cleanStaleSessionAgents` so it cannot
46
- * shadow the real roster and break `Agent(name: ...)` invocations.
42
+ * Official ninja roster — the directory names Konoha itself deploys into a
43
+ * session's `.agents/agents/` directory. `cleanStaleSessionAgents` reports any
44
+ * other entry but never removes it: a custom or third-party agent is not
45
+ * Konoha's to delete, and the global roster dirs are the authority.
47
46
  */
48
47
  const _OFFICIAL_NINJAS = getCanonicalAgentNames();
49
48
 
50
49
  /**
51
- * Remove any directory under `base` whose name is NOT in the official ninja
52
- * whitelist. Defense-in-depth against leaked `define_subagent` artifacts and
53
- * stale test agents. Safe-by-default: silent on every error path.
50
+ * Report directories under `base` whose name is not in the official ninja
51
+ * roster. Leaked `define_subagent` artifacts and stale `test-*` debug agents
52
+ * can shadow the real roster and break `Agent(name: ...)` invocations, so they
53
+ * are surfaced to the caller.
54
+ *
55
+ * Ownership boundary invariant: this never deletes anything. Konoha does not
56
+ * own custom or third-party agent directories inside `agents/` or session
57
+ * `brain/` directories, so removal stays with the user.
58
+ *
59
+ * @returns {string[]} non-roster directory names found (empty on any error)
54
60
  */
55
- function cleanStaleSessionAgents(_base) {
56
- // Ownership boundary invariant: Konoha NEVER deletes custom or third-party
57
- // agent directories inside `agents/` or session `brain/` directories.
61
+ function cleanStaleSessionAgents(base) {
62
+ const allowed = new Set(_OFFICIAL_NINJAS.map((n) => String(n).toLowerCase()));
63
+ const found = [];
64
+ try {
65
+ if (!base || !fs.existsSync(base)) return found;
66
+ for (const entry of fs.readdirSync(base, { withFileTypes: true })) {
67
+ if (!entry.isDirectory()) continue;
68
+ if (!allowed.has(entry.name.toLowerCase())) found.push(entry.name);
69
+ }
70
+ } catch (_) { /* intentional best-effort: reporting must never crash the runtime */ }
71
+ return found;
58
72
  }
59
73
 
60
74
  function loadAgentsFromGlobalDir() {
@@ -92,7 +106,11 @@ function loadAgents() {
92
106
  function deploySessionAgents(agents, brainDir) {
93
107
  if (!brainDir) return;
94
108
  const base = path.join(brainDir, '.agents', 'agents');
95
- cleanStaleSessionAgents(base);
109
+ // Report, never delete: a non-roster directory may be the user's own agent.
110
+ const stale = cleanStaleSessionAgents(base);
111
+ if (stale.length > 0) {
112
+ process.stderr.write(`⚠ Antigravity session has non-roster agent dirs (not removed): ${stale.join(', ')}\n`);
113
+ }
96
114
  const antigravityManager = require('./antigravity_manager');
97
115
  for (const agent of agents) {
98
116
  try {
@@ -90,6 +90,8 @@ function routeIncomingRequest(req, res, activeBridges) {
90
90
  }
91
91
  if (req.method === 'POST' && p === '/v1/messages/count_tokens') {
92
92
  readBody(req).catch(() => {});
93
+ // Liveness reply, not a measurement: no tokenizer exists here, and this 0
94
+ // is never persisted as provider usage.
93
95
  sendJson(res, 200, { input_tokens: 0 });
94
96
  return;
95
97
  }
@@ -12,8 +12,27 @@ const {
12
12
  pipeWithModelRewrite
13
13
  } = require('./gateway_router');
14
14
  const { dedupeToolDefinitions, capToolOutputs } = require('./tool_payload');
15
- const { extractCachedTokens } = require('./cache_capability');
15
+ const { normalizeUsage } = require('../token_usage');
16
+ const { recordProviderUsage, PROVIDER_USAGE_SOURCES } = require('../provider_usage_logger');
16
17
  const { validateRequestBudget, LIMITS } = require('./request_budget');
18
+ const { supportsStreamUsage } = require('./stream_usage');
19
+
20
+ // Anthropic's wire schema requires integer usage counts: `input_tokens` and
21
+ // `output_tokens` on a message, and `output_tokens` on a `message_delta`.
22
+ // When the provider reported neither there is no schema-valid usage to emit,
23
+ // and fabricating zeros would misreport billing. Fail instead.
24
+ const USAGE_UNAVAILABLE_MESSAGE =
25
+ 'Upstream reported no token usage; refusing to emit a schema-invalid Anthropic response';
26
+
27
+ /**
28
+ * Persist what the upstream provider actually reported. Best-effort: the
29
+ * response has already been written, so a telemetry failure must not surface.
30
+ */
31
+ function recordUpstreamUsage(usage, meta) {
32
+ try {
33
+ recordProviderUsage({ source: PROVIDER_USAGE_SOURCES.GATEWAY, usage, ...meta });
34
+ } catch (_) { /* telemetry must never break a response */ }
35
+ }
17
36
 
18
37
  function extractAnthropicSystemText(content) {
19
38
  if (typeof content === 'string') return content;
@@ -21,6 +40,19 @@ function extractAnthropicSystemText(content) {
21
40
  return '';
22
41
  }
23
42
 
43
+ /**
44
+ * Resolve the active client id; best-effort lazy load so require-time
45
+ * failures (missing db) do not abort the bridge.
46
+ */
47
+ function resolveClient() {
48
+ try {
49
+ const { detectActiveClient } = require('../tools_savings_logger');
50
+ return detectActiveClient() || 'unattributed';
51
+ } catch (_) {
52
+ return 'unattributed';
53
+ }
54
+ }
55
+
24
56
  function convertContentBlock(block, state) {
25
57
  if (block.type === 'text') {
26
58
  state.textParts.push({ type: 'text', text: block.text || '' });
@@ -119,10 +151,14 @@ function writeAnthropicToolCallBlocks(res, toolCalls, hasText) {
119
151
  }
120
152
 
121
153
  function streamEmptyAnthropicMessage(res, model, msgId, openAiResp) {
122
- const startUsage = buildAnthropicUsage(openAiResp);
123
- const startMessage = { id: `msg_${msgId}`, type: 'message', role: 'assistant', model: model || 'unknown', content: [], stop_reason: null, usage: startUsage };
154
+ const usage = completeAnthropicUsage(openAiResp);
155
+ if (!usage) {
156
+ sendAnthropicErrorResponse(res, 502, 'api_error', USAGE_UNAVAILABLE_MESSAGE, true);
157
+ return;
158
+ }
159
+ const startMessage = { id: `msg_${msgId}`, type: 'message', role: 'assistant', model: model || 'unknown', content: [], stop_reason: null, usage };
124
160
  res.write(`event: message_start\ndata: ${JSON.stringify({ type: 'message_start', message: startMessage })}\n\n`);
125
- res.write(`event: message_stop\ndata: ${JSON.stringify({ type: 'message_stop', delta: { stop_reason: 'stop' }, usage: { output_tokens: buildAnthropicUsage(openAiResp).output_tokens } })}\n\n`);
161
+ res.write(`event: message_stop\ndata: ${JSON.stringify({ type: 'message_stop', delta: { stop_reason: 'stop' } })}\n\n`);
126
162
  res.end();
127
163
  }
128
164
 
@@ -139,8 +175,12 @@ function streamOpenAIResponseToAnthropic(openAiResp, options) {
139
175
  const toolCalls = msg.tool_calls || [];
140
176
  const textContent = (msg.content || '').toString();
141
177
 
142
- const mainUsage = buildAnthropicUsage(openAiResp);
143
- const mainMessage = { id: `msg_${msgId}`, type: 'message', role: 'assistant', model: model || 'unknown', content: [], stop_reason: null, usage: mainUsage };
178
+ const usage = completeAnthropicUsage(openAiResp);
179
+ if (!usage) {
180
+ sendAnthropicErrorResponse(res, 502, 'api_error', USAGE_UNAVAILABLE_MESSAGE, true);
181
+ return;
182
+ }
183
+ const mainMessage = { id: `msg_${msgId}`, type: 'message', role: 'assistant', model: model || 'unknown', content: [], stop_reason: null, usage };
144
184
  res.write(`event: message_start\ndata: ${JSON.stringify({ type: 'message_start', message: mainMessage })}\n\n`);
145
185
 
146
186
  if (textContent) {
@@ -152,7 +192,7 @@ function streamOpenAIResponseToAnthropic(openAiResp, options) {
152
192
  writeAnthropicToolCallBlocks(res, toolCalls, Boolean(textContent));
153
193
 
154
194
  const stopReason = choice.finish_reason || 'stop';
155
- res.write(`event: message_delta\ndata: ${JSON.stringify({ type: 'message_delta', delta: { stop_reason: stopReason === 'tool_calls' ? 'tool_use' : 'end_turn', stop_sequence: null }, usage: { output_tokens: 0 } })}\n\n`);
195
+ res.write(`event: message_delta\ndata: ${JSON.stringify({ type: 'message_delta', delta: { stop_reason: stopReason === 'tool_calls' ? 'tool_use' : 'end_turn', stop_sequence: null }, usage })}\n\n`);
156
196
  res.write('event: message_stop\ndata: {}\n\n');
157
197
  res.end();
158
198
  }
@@ -175,6 +215,12 @@ function nonStreamOpenAIResponseToAnthropic(openAiResp, options) {
175
215
  contentBlocks.push({ type: 'text', text: textContent });
176
216
  }
177
217
 
218
+ const usage = completeAnthropicUsage(openAiResp);
219
+ if (!usage) {
220
+ sendAnthropicErrorResponse(res, 502, 'api_error', USAGE_UNAVAILABLE_MESSAGE, false);
221
+ return;
222
+ }
223
+
178
224
  sendJson(res, 200, {
179
225
  id: `msg_${msgId}`,
180
226
  type: 'message',
@@ -183,7 +229,7 @@ function nonStreamOpenAIResponseToAnthropic(openAiResp, options) {
183
229
  content: contentBlocks,
184
230
  stop_reason: toolCalls.length > 0 ? 'tool_use' : 'end_turn',
185
231
  stop_sequence: null,
186
- usage: buildAnthropicUsage(openAiResp)
232
+ usage
187
233
  });
188
234
  }
189
235
 
@@ -196,23 +242,59 @@ function openAIResponseToAnthropic(openAiResp, options) {
196
242
  }
197
243
 
198
244
  /**
199
- * Translate upstream OpenAI-shaped usage into an Anthropic usage object.
200
- * Token counts are passed through unchanged; cached_tokens is added only when
201
- * the upstream actually reported it, so consumers can tell "no cache" apart
202
- * from "provider gave no breakdown".
245
+ * Translate upstream [OI]-shaped usage into an Anthropic usage object.
246
+ *
247
+ * Counts come only from what the provider reported: an unreported count stays
248
+ * null, which is how a consumer tells "provider gave no breakdown" apart from
249
+ * "provider measured zero". `normalizeUsage` also accepts Anthropic-native
250
+ * field spellings, so an already-translated upstream is not re-estimated.
203
251
  */
204
252
  function buildAnthropicUsage(openAiResp) {
205
- const usage = openAiResp && openAiResp.usage ? openAiResp.usage : null;
253
+ const measured = normalizeUsage(openAiResp && openAiResp.usage);
206
254
  const result = {
207
- input_tokens: usage && Number.isFinite(usage.prompt_tokens) ? usage.prompt_tokens : null,
208
- output_tokens: usage && Number.isFinite(usage.completion_tokens) ? usage.completion_tokens : null
255
+ input_tokens: measured.available ? measured.input_tokens : null,
256
+ output_tokens: measured.available ? measured.output_tokens : null
209
257
  };
210
- const cached = extractCachedTokens(usage);
211
- if (cached !== null) result.cache_read_input_tokens = cached;
258
+ if (measured.cache_read_input_tokens !== null) result.cache_read_input_tokens = measured.cache_read_input_tokens;
212
259
  return result;
213
260
  }
214
261
 
215
- function buildOpenAiPayloadFromAnthropic(body, targetModel) {
262
+ /**
263
+ * Anthropic's wire `usage` declares integer counts. When a provider reported
264
+ * nothing, the honest options are (a) omit the field or (b) emit a fabricated
265
+ * 0. We omit: a client that null-coalesces is correct, a client that trusts a
266
+ * fake zero is not. A genuine provider zero (measured, `0`) still passes
267
+ * through, so "unknown" and "measured zero" stay distinguishable on the wire.
268
+ */
269
+ function anthropicUsageWire(usage) {
270
+ const out = {};
271
+ for (const [key, value] of Object.entries(usage || {})) {
272
+ // Anthropic token counts are integers; a fractional or unsafe value is not
273
+ // a measurement we can publish.
274
+ if (Number.isSafeInteger(value) && value >= 0) out[key] = value;
275
+ }
276
+ return out;
277
+ }
278
+
279
+ /**
280
+ * Build the wire usage only when it satisfies the Anthropic schema. A message
281
+ * and a `message_start` both require integer `input_tokens` and `output_tokens`,
282
+ * and a `message_delta` requires `output_tokens`; the SDK accumulator reads
283
+ * `usage.output_tokens` unconditionally. A provider that reported neither count
284
+ * leaves no schema-valid usage, so this returns null and the caller fails the
285
+ * request instead of emitting `{}` or a fabricated zero.
286
+ */
287
+ function completeAnthropicUsage(openAiResp) {
288
+ const wire = anthropicUsageWire(buildAnthropicUsage(openAiResp));
289
+ if (typeof wire.input_tokens !== 'number' || typeof wire.output_tokens !== 'number') return null;
290
+ return wire;
291
+ }
292
+
293
+ /**
294
+ * Whether this bridge may be sent `stream_options.include_usage`. The decision
295
+ * lives in `./stream_usage`: an unrecognised provider is never sent the field.
296
+ */
297
+ function buildOpenAiPayloadFromAnthropic(body, targetModel, bridgeConfig) {
216
298
  const isStream = body.stream === true;
217
299
  const openAiPayload = {
218
300
  model: targetModel,
@@ -232,12 +314,38 @@ function buildOpenAiPayloadFromAnthropic(body, targetModel) {
232
314
  if (body.compactContext === true) {
233
315
  openAiPayload.compactContext = true;
234
316
  }
317
+ // The [OI] Chat Completions streaming contract gates the final usage-bearing
318
+ // chunk behind this field, and `coalesceOpenAiStreamToAnthropic` reads
319
+ // `chunk.usage`; without it every streamed response's token columns stay null.
320
+ if (isStream && supportsStreamUsage(bridgeConfig)) openAiPayload.stream_options = { include_usage: true };
235
321
  for (const k of Object.keys(openAiPayload)) {
236
322
  if (openAiPayload[k] === undefined || openAiPayload[k] === null) delete openAiPayload[k];
237
323
  }
238
324
  return openAiPayload;
239
325
  }
240
326
 
327
+ /**
328
+ * Fail a translated Anthropic request. Streams get an `error` SSE event, which
329
+ * the SDK surfaces verbatim; it precedes `message_start`, so the accumulator
330
+ * never sees an out-of-order or schema-invalid event.
331
+ */
332
+ function sendAnthropicErrorResponse(res, statusCode, errType, message, isStream) {
333
+ const body = { type: 'error', error: { type: errType, message } };
334
+ if (isStream) {
335
+ if (!res.headersSent) res.writeHead(statusCode, { 'Content-Type': 'text/event-stream' });
336
+ if (!res.writableEnded) {
337
+ res.write(`event: error\ndata: ${JSON.stringify(body)}\n\n`);
338
+ res.end();
339
+ }
340
+ return;
341
+ }
342
+ if (res.headersSent) {
343
+ if (!res.writableEnded) res.end();
344
+ return;
345
+ }
346
+ sendJson(res, statusCode, body);
347
+ }
348
+
241
349
  function sendAnthropicUpstreamError(raw, statusCode, isStream, res) {
242
350
  if (res.headersSent) return;
243
351
  if (isStream) res.writeHead(statusCode, { 'Content-Type': 'text/event-stream' });
@@ -273,7 +381,7 @@ function accumulateToolCallDeltas(delta, toolCalls) {
273
381
  }
274
382
  }
275
383
 
276
- function coalesceOpenAiStreamToAnthropic(buffer, targetModel, msgId, res) {
384
+ function coalesceOpenAiStreamToAnthropic(buffer, targetModel, msgId, res, usageMeta) {
277
385
  const lines = buffer.split('\n').filter((l) => l.startsWith('data:'));
278
386
  let content = '';
279
387
  const toolCalls = [];
@@ -302,17 +410,18 @@ function coalesceOpenAiStreamToAnthropic(buffer, targetModel, msgId, res) {
302
410
  }]
303
411
  };
304
412
  if (streamUsage) openAiResp.usage = streamUsage;
413
+ recordUpstreamUsage(streamUsage, { ...usageMeta, model: targetModel, stream: true });
305
414
  openAIResponseToAnthropic(openAiResp, { model: targetModel, msgId, isStream: true, res });
306
415
  }
307
416
 
308
- function pipeAnthropicStreamResponse(forwardRes, targetModel, res, resetStreamTimeout) {
417
+ function pipeAnthropicStreamResponse(forwardRes, targetModel, res, resetStreamTimeout, usageMeta) {
309
418
  let buffer = '';
310
419
  const msgId = `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
311
420
  forwardRes.on('data', (chunk) => {
312
421
  resetStreamTimeout();
313
422
  buffer += chunk.toString('utf8');
314
423
  });
315
- forwardRes.on('end', () => coalesceOpenAiStreamToAnthropic(buffer, targetModel, msgId, res));
424
+ forwardRes.on('end', () => coalesceOpenAiStreamToAnthropic(buffer, targetModel, msgId, res, usageMeta));
316
425
  forwardRes.on('error', (err) => {
317
426
  if (!res.headersSent) {
318
427
  sendJson(res, 502, { type: 'error', error: { type: 'api_error', message: `Upstream stream error: ${err.message}` } });
@@ -320,10 +429,11 @@ function pipeAnthropicStreamResponse(forwardRes, targetModel, res, resetStreamTi
320
429
  });
321
430
  }
322
431
 
323
- function handleAnthropicNonStreamData(raw, targetModel, res) {
432
+ function handleAnthropicNonStreamData(raw, targetModel, res, usageMeta) {
324
433
  try {
325
434
  const parsed = JSON.parse(raw);
326
435
  const msgId = `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
436
+ recordUpstreamUsage(parsed && parsed.usage, { ...usageMeta, model: targetModel, stream: false });
327
437
  openAIResponseToAnthropic(parsed, { model: targetModel, msgId, isStream: false, res });
328
438
  } catch (err) {
329
439
  if (!res.headersSent) {
@@ -332,10 +442,10 @@ function handleAnthropicNonStreamData(raw, targetModel, res) {
332
442
  }
333
443
  }
334
444
 
335
- function pipeAnthropicNonStreamResponse(forwardRes, targetModel, res) {
445
+ function pipeAnthropicNonStreamResponse(forwardRes, targetModel, res, usageMeta) {
336
446
  let raw = '';
337
447
  forwardRes.on('data', (c) => { raw += c.toString('utf8'); });
338
- forwardRes.on('end', () => handleAnthropicNonStreamData(raw, targetModel, res));
448
+ forwardRes.on('end', () => handleAnthropicNonStreamData(raw, targetModel, res, usageMeta));
339
449
  forwardRes.on('error', (err) => {
340
450
  if (!res.headersSent) {
341
451
  sendJson(res, 502, { type: 'error', error: { type: 'api_error', message: `Upstream error: ${err.message}` } });
@@ -344,16 +454,16 @@ function pipeAnthropicNonStreamResponse(forwardRes, targetModel, res) {
344
454
  }
345
455
 
346
456
  function handleAnthropicResponse(forwardRes, options) {
347
- const { isStream, targetModel, res, resetStreamTimeout } = options;
457
+ const { isStream, targetModel, res, resetStreamTimeout, usageMeta } = options;
348
458
  if (forwardRes.statusCode >= 400) {
349
459
  handleAnthropicUpstreamError(forwardRes, isStream, res);
350
460
  return;
351
461
  }
352
462
  if (isStream) {
353
- pipeAnthropicStreamResponse(forwardRes, targetModel, res, resetStreamTimeout);
463
+ pipeAnthropicStreamResponse(forwardRes, targetModel, res, resetStreamTimeout, usageMeta);
354
464
  return;
355
465
  }
356
- pipeAnthropicNonStreamResponse(forwardRes, targetModel, res);
466
+ pipeAnthropicNonStreamResponse(forwardRes, targetModel, res, usageMeta);
357
467
  }
358
468
 
359
469
  function setupStreamEvents(forwardReq, res, options) {
@@ -429,7 +539,7 @@ async function handleAnthropicMessages(activeBridges, req, res) {
429
539
  }
430
540
  const effectiveBody = budget.payload || body;
431
541
  const isStream = effectiveBody.stream === true;
432
- const openAiPayload = buildOpenAiPayloadFromAnthropic(effectiveBody, targetModel);
542
+ const openAiPayload = buildOpenAiPayloadFromAnthropic(effectiveBody, targetModel, active.bridgeConfig);
433
543
  const rewrittenBody = JSON.stringify(openAiPayload);
434
544
 
435
545
  const headers = sanitizeForwardHeaders(req.headers);
@@ -454,7 +564,17 @@ async function handleAnthropicMessages(activeBridges, req, res) {
454
564
  agent: httpAgent,
455
565
  timeout: STREAM_TIMEOUT_MS
456
566
  }, (forwardRes) => {
457
- handleAnthropicResponse(forwardRes, { isStream, targetModel, res, resetStreamTimeout });
567
+ handleAnthropicResponse(forwardRes, {
568
+ isStream,
569
+ targetModel,
570
+ res,
571
+ resetStreamTimeout,
572
+ usageMeta: {
573
+ provider: (active.bridgeConfig && active.bridgeConfig.provider) || null,
574
+ bridgeName,
575
+ client: resolveClient()
576
+ }
577
+ });
458
578
  });
459
579
 
460
580
  setupStreamEvents(forwardReq, res, { bridgeName, getLastActivity: () => lastActivityTime });
@@ -543,6 +663,12 @@ module.exports = {
543
663
  extractAnthropicSystemText,
544
664
  anthropicToolsToOpenAi,
545
665
  buildOpenAiPayloadFromAnthropic,
666
+ buildAnthropicUsage,
667
+ anthropicUsageWire,
668
+ completeAnthropicUsage,
669
+ supportsStreamUsage,
670
+ sendAnthropicErrorResponse,
671
+ USAGE_UNAVAILABLE_MESSAGE,
546
672
  openAIResponseToAnthropic,
547
673
  handleAnthropicMessages,
548
674
  handleGeminiRequest
@@ -7,6 +7,33 @@ const { extractAllImages } = require('../images');
7
7
  const { callRawInference } = require('../sidecar/raw');
8
8
  const { sanitizeRequest } = require('../sanitize');
9
9
 
10
+ /**
11
+ * Anthropic wire usage for an inference result, or null when the provider did
12
+ * not report both counts. Lazy require keeps this module loadable on its own.
13
+ * The schema requires integer `input_tokens` and `output_tokens`, so an
14
+ * incomplete pair cannot be emitted as `{}` and must never become a zero.
15
+ */
16
+ function usageWire(raw) {
17
+ const { buildAnthropicUsage, anthropicUsageWire } = require('../gateway_adapters');
18
+ const wire = anthropicUsageWire(buildAnthropicUsage(raw));
19
+ if (typeof wire.input_tokens !== 'number' || typeof wire.output_tokens !== 'number') return null;
20
+ return wire;
21
+ }
22
+
23
+ /** Persist provider-reported counts from this path; best-effort only. */
24
+ function recordUsage(ctx, raw, stream) {
25
+ try {
26
+ const { recordProviderUsage, PROVIDER_USAGE_SOURCES } = require('../../provider_usage_logger');
27
+ recordProviderUsage({
28
+ source: PROVIDER_USAGE_SOURCES.GATEWAY,
29
+ usage: raw && raw.usage,
30
+ provider: (ctx.bridgeConfig && ctx.bridgeConfig.provider) || null,
31
+ model: ctx.requestedModel || null,
32
+ stream
33
+ });
34
+ } catch (_) { /* telemetry must never break a response */ }
35
+ }
36
+
10
37
  // Map numeric model enum values → GetModelResponse string enum (same as chat.js)
11
38
  const VALUE_TO_MODEL_ENUM = {
12
39
  1018: 'MODEL_PLACEHOLDER_M18', // Flash
@@ -129,7 +156,7 @@ function extractAnthropicText(content) {
129
156
 
130
157
  // Anthropic SSE response builders
131
158
 
132
- function buildAnthropicMessage(id, model) {
159
+ function buildAnthropicMessage(id, model, usage) {
133
160
  return {
134
161
  id: `msg_${id}`,
135
162
  type: 'message',
@@ -138,15 +165,52 @@ function buildAnthropicMessage(id, model) {
138
165
  content: [],
139
166
  stop_reason: null,
140
167
  stop_sequence: null,
141
- usage: { input_tokens: 0, output_tokens: 0 },
168
+ usage,
142
169
  };
143
170
  }
144
171
 
172
+ /** Open the SSE response without committing to a `message_start` yet. */
173
+ function ensureAnthropicStreamHeaders(res) {
174
+ if (res.headersSent) return;
175
+ res.setHeader('Content-Type', 'text/event-stream');
176
+ res.setHeader('Cache-Control', 'no-cache');
177
+ res.setHeader('Connection', 'keep-alive');
178
+ res.writeHead(200);
179
+ }
180
+
181
+ /**
182
+ * Fail the request in Anthropic's error shape. Before any response is written
183
+ * the stream still gets an `error` event, which the SDK yields verbatim; it
184
+ * must precede `message_start` because the accumulator rejects out-of-order
185
+ * events.
186
+ */
187
+ function sendUsageUnavailable(res, isStream) {
188
+ const { USAGE_UNAVAILABLE_MESSAGE } = require('../gateway_adapters');
189
+ const body = { type: 'error', error: { type: 'api_error', message: USAGE_UNAVAILABLE_MESSAGE } };
190
+ if (isStream) {
191
+ ensureAnthropicStreamHeaders(res);
192
+ writeAnthropicEvent(res, 'error', body);
193
+ res.end();
194
+ return;
195
+ }
196
+ sendJson(res, 502, body);
197
+ }
198
+
145
199
  function writeAnthropicEvent(res, event, data) {
146
200
  res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
147
201
  }
148
202
 
149
- function streamRawAnthropicResponse(res, raw, responseText) {
203
+ function streamRawAnthropicResponse(res, raw, responseText, msgId, model) {
204
+ const wireUsage = usageWire(raw);
205
+ if (!wireUsage) {
206
+ sendUsageUnavailable(res, true);
207
+ return;
208
+ }
209
+ ensureAnthropicStreamHeaders(res);
210
+ writeAnthropicEvent(res, 'message_start', {
211
+ type: 'message_start',
212
+ message: buildAnthropicMessage(msgId, model, wireUsage),
213
+ });
150
214
  if (raw.toolCalls && raw.toolCalls.length > 0) {
151
215
  for (let i = 0; i < raw.toolCalls.length; i++) {
152
216
  const tc = raw.toolCalls[i];
@@ -165,7 +229,7 @@ function streamRawAnthropicResponse(res, raw, responseText) {
165
229
  writeAnthropicEvent(res, 'message_delta', {
166
230
  type: 'message_delta',
167
231
  delta: { stop_reason: 'tool_use', stop_sequence: null },
168
- usage: { output_tokens: 0 },
232
+ usage: wireUsage,
169
233
  });
170
234
  } else {
171
235
  writeAnthropicEvent(res, 'content_block_start', {
@@ -184,7 +248,7 @@ function streamRawAnthropicResponse(res, raw, responseText) {
184
248
  writeAnthropicEvent(res, 'message_delta', {
185
249
  type: 'message_delta',
186
250
  delta: { stop_reason: 'end_turn', stop_sequence: null },
187
- usage: { output_tokens: 0 },
251
+ usage: wireUsage,
188
252
  });
189
253
  }
190
254
  writeAnthropicEvent(res, 'message_stop', { type: 'message_stop' });
@@ -201,6 +265,11 @@ function sendNonStreamAnthropicResponse(res, raw, responseText, msgId, modelKey)
201
265
  }
202
266
  }
203
267
  if (responseText) content.push({ type: 'text', text: responseText });
268
+ const wireUsage = usageWire(raw);
269
+ if (!wireUsage) {
270
+ sendUsageUnavailable(res, false);
271
+ return;
272
+ }
204
273
  sendJson(res, 200, {
205
274
  id: `msg_${msgId}`,
206
275
  type: 'message',
@@ -209,7 +278,7 @@ function sendNonStreamAnthropicResponse(res, raw, responseText, msgId, modelKey)
209
278
  content,
210
279
  stop_reason: raw.toolCalls ? 'tool_use' : 'end_turn',
211
280
  stop_sequence: null,
212
- usage: { input_tokens: 0, output_tokens: 0 },
281
+ usage: wireUsage,
213
282
  });
214
283
  }
215
284
 
@@ -308,14 +377,7 @@ async function handleAnthropicMessages(ctx, req, res) {
308
377
 
309
378
  const initiateStream = () => {
310
379
  if (isStream && !headersSentForStream) {
311
- res.setHeader('Content-Type', 'text/event-stream');
312
- res.setHeader('Cache-Control', 'no-cache');
313
- res.setHeader('Connection', 'keep-alive');
314
- res.writeHead(200);
315
- writeAnthropicEvent(res, 'message_start', {
316
- type: 'message_start',
317
- message: buildAnthropicMessage(msgId, resolved.key),
318
- });
380
+ ensureAnthropicStreamHeaders(res);
319
381
  headersSentForStream = true;
320
382
  }
321
383
  };
@@ -344,10 +406,11 @@ async function handleAnthropicMessages(ctx, req, res) {
344
406
  }
345
407
  const responseText = raw.content || '';
346
408
  log(ctx, `✅ [Anthropic] Raw inference succeeded (${responseText.length} chars)`);
409
+ recordUsage(ctx, raw, isStream);
347
410
 
348
411
  if (isStream) {
349
412
  initiateStream();
350
- streamRawAnthropicResponse(res, raw, responseText);
413
+ streamRawAnthropicResponse(res, raw, responseText, msgId, resolved.key);
351
414
  } else {
352
415
  sendNonStreamAnthropicResponse(res, raw, responseText, msgId, resolved.key);
353
416
  }
@@ -362,8 +425,9 @@ async function handleAnthropicMessages(ctx, req, res) {
362
425
  }
363
426
 
364
427
  // POST /v1/messages/count_tokens (mock)
365
- // Claude CLI and Cherry Studio send this before every conversation.
366
- // Return a plausible mock so they don't abort.
428
+ // Claude CLI and Cherry Studio send this before every conversation and only
429
+ // check that it answers; no tokenizer exists here. The 0 is a liveness reply,
430
+ // not a provider measurement, and is never persisted as usage.
367
431
 
368
432
  async function handleCountTokens(ctx, req, res) {
369
433
  // Consume the body so the socket stays clean
@@ -379,15 +443,16 @@ async function handleCountTokens(ctx, req, res) {
379
443
  // Anthropic format adapter for OpenAI provider
380
444
  // Converts Anthropic → OpenAI → callRawInference → Anthropic response
381
445
 
382
- function streamAnthropicToOpenAi(res, responseText, msgId, model) {
383
- res.setHeader('Content-Type', 'text/event-stream');
384
- res.setHeader('Cache-Control', 'no-cache');
385
- res.setHeader('Connection', 'keep-alive');
386
- res.writeHead(200);
446
+ function streamAnthropicToOpenAi(res, responseText, msgId, model, usage) {
447
+ if (!usage) {
448
+ sendUsageUnavailable(res, true);
449
+ return;
450
+ }
451
+ ensureAnthropicStreamHeaders(res);
387
452
 
388
453
  writeAnthropicEvent(res, 'message_start', {
389
454
  type: 'message_start',
390
- message: buildAnthropicMessage(msgId, model),
455
+ message: buildAnthropicMessage(msgId, model, usage),
391
456
  });
392
457
  writeAnthropicEvent(res, 'content_block_start', {
393
458
  type: 'content_block_start', index: 0, content_block: { type: 'text', text: '' },
@@ -398,7 +463,7 @@ function streamAnthropicToOpenAi(res, responseText, msgId, model) {
398
463
  writeAnthropicEvent(res, 'content_block_stop', { type: 'content_block_stop', index: 0 });
399
464
  writeAnthropicEvent(res, 'message_delta', {
400
465
  type: 'message_delta', delta: { stop_reason: responseText ? 'end_turn' : 'tool_use', stop_sequence: null },
401
- usage: { output_tokens: 0 },
466
+ usage,
402
467
  });
403
468
  writeAnthropicEvent(res, 'message_stop', { type: 'message_stop' });
404
469
  res.end();
@@ -420,13 +485,17 @@ async function handleAnthropicToOpenAi(ctx, req, res, ...rest) {
420
485
  if (!raw || (!raw.content && !raw.toolCalls)) throw new Error('Empty response');
421
486
 
422
487
  const responseText = raw.content || '';
488
+ recordUsage(ctx, raw, isStream);
423
489
  if (isStream) {
424
- streamAnthropicToOpenAi(res, responseText, msgId, ctx.requestedModel);
490
+ streamAnthropicToOpenAi(res, responseText, msgId, ctx.requestedModel, usageWire(raw));
425
491
  } else {
426
492
  sendNonStreamAnthropicResponse(res, raw, responseText, msgId, ctx.requestedModel);
427
493
  }
428
494
  } catch (err) {
429
- sendJson(res, 500, { type: 'error', error: { type: 'api_error', message: err.message } });
495
+ // A stream may already have written headers and pings, so a JSON body here
496
+ // would be a second, invalid response on the same connection.
497
+ const { sendAnthropicErrorResponse } = require('../gateway_adapters');
498
+ sendAnthropicErrorResponse(res, 500, 'api_error', err.message, isStream);
430
499
  }
431
500
  }
432
501