konoha-mcp 2.2.23 → 2.2.24
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/NOTICE +38 -0
- package/README.md +3 -3
- package/apps/web/build/adapter-node.js +1 -1
- package/apps/web/build/build-meta.json +2 -2
- package/apps/web/build/client/_app/immutable/entry/{app.TYH87iYy.js → app.CKJ0NhrX.js} +2 -2
- package/apps/web/build/client/_app/immutable/entry/app.CKJ0NhrX.js.br +0 -0
- package/apps/web/build/client/_app/immutable/entry/app.CKJ0NhrX.js.gz +0 -0
- package/apps/web/build/client/_app/immutable/nodes/{0.D7dPIEWA.js → 0.7A6fQJ6z.js} +1 -1
- package/apps/web/build/client/_app/immutable/nodes/0.7A6fQJ6z.js.br +0 -0
- package/apps/web/build/client/_app/immutable/nodes/{0.D7dPIEWA.js.gz → 0.7A6fQJ6z.js.gz} +0 -0
- package/apps/web/build/client/_app/immutable/nodes/{13.TOXVaTBr.js → 13.CQB_Ed6W.js} +1 -1
- package/apps/web/build/client/_app/immutable/nodes/13.CQB_Ed6W.js.br +0 -0
- package/apps/web/build/client/_app/immutable/nodes/{13.TOXVaTBr.js.gz → 13.CQB_Ed6W.js.gz} +0 -0
- package/apps/web/build/client/_app/version.json +1 -1
- package/apps/web/build/client/_app/version.json.br +0 -0
- package/apps/web/build/client/_app/version.json.gz +0 -0
- package/apps/web/build/server/chunks/instance.js +3 -3
- package/apps/web/build/server/chunks/internal.js +1 -1
- package/apps/web/build/server/entries/pages/_layout.svelte.js +1 -1
- package/apps/web/build/server/entries/pages/_layout.svelte.js.map +1 -1
- package/apps/web/build/server/entries/pages/docs/_page.svelte.js +1 -1
- package/apps/web/build/server/entries/pages/docs/_page.svelte.js.map +1 -1
- package/apps/web/build/server/manifest-full.js +1 -1
- package/apps/web/build/server/manifest.js +1 -1
- package/apps/web/build/server/nodes/0.js +1 -1
- package/apps/web/build/server/nodes/13.js +1 -1
- package/apps/web/build/server/server.js +1 -1
- package/apps/web/package.json +1 -1
- package/bin/commands/bridge.js +62 -4
- package/bin/commands/doctor.js +2 -0
- package/bin/commands/doctor_services.js +57 -0
- package/bin/commands/init.js +16 -5
- package/bin/commands/init_clients.js +95 -8
- package/bin/lib/config_defaults.js +3 -0
- package/bin/lib/runtime_install.js +141 -54
- package/package.json +2 -1
- package/patches/gpt-tokenizer@4.1.0.patch +15 -0
- package/src/agent_contract.js +1 -1
- package/src/agent_deployer.js +48 -35
- package/src/agent_file_safety.js +57 -0
- package/src/bridge/gateway.js +2 -0
- package/src/bridge/gateway_adapters.js +155 -29
- package/src/bridge/handlers/anthropic.js +95 -26
- package/src/bridge/handlers/openai.js +18 -5
- package/src/bridge/sidecar/raw.js +10 -2
- package/src/bridge/stream_usage.js +66 -0
- package/src/client_commands.js +190 -0
- package/src/db_bridges.js +53 -7
- package/src/db_schema.js +11 -2
- package/src/file_tools_mcp.js +17 -9
- package/src/global_skill_sync.js +3 -1
- package/src/mcp/langgraph/edges/router.js +3 -0
- package/src/mcp/langgraph/index.js +22 -3
- package/src/mcp/langgraph/nodes/dispatch.js +3 -0
- package/src/mcp/langgraph/nodes/execute.js +87 -13
- package/src/mcp/langgraph/nodes/review.js +13 -1
- package/src/mcp/langgraph/slice_executor_registry.js +22 -0
- package/src/mcp/langgraph/state.js +5 -0
- package/src/mcp/lazy_subagent.js +11 -1
- package/src/mcp/memory_project_context.js +4 -1
- package/src/mcp/memory_subagent_runner.js +2 -1
- package/src/mcp/skills_common.js +1 -0
- package/src/mcp/subagent_spawner.js +7 -0
- package/src/mcp/todo_walkthrough_sync.js +7 -0
- package/src/mcp/tool_dispatch.js +16 -0
- package/src/mcp/workflow_completion_helpers.js +6 -1
- package/src/mcp/workflow_gates.js +118 -5
- package/src/mcp/workflow_sannin.js +9 -3
- package/src/mcp_tool_manifest.json +5 -1
- package/src/provider_usage_logger.js +92 -0
- package/src/schema_ddl.js +17 -1
- package/src/templates/AGENTS.md +1 -1
- package/src/templates/GEMINI.md +1 -1
- package/src/templates/skills/kage-skill/SKILL.md +1 -1
- package/src/templates/skills/konoha/SKILL.md +23 -165
- package/src/templates/skills/konoha/references/contract.md +1 -1
- package/src/templates/skills/konoha/references/peragent-usage.md +1 -1
- package/src/token_usage.js +57 -0
- package/src/tools_savings_logger.js +26 -8
- package/src/web/controllers/core_controller.js +10 -1
- package/src/workflow/jonin-builder/live_browser_monitor.js +23 -7
- package/apps/web/build/client/_app/immutable/entry/app.TYH87iYy.js.br +0 -0
- package/apps/web/build/client/_app/immutable/entry/app.TYH87iYy.js.gz +0 -0
- package/apps/web/build/client/_app/immutable/nodes/0.D7dPIEWA.js.br +0 -0
- package/apps/web/build/client/_app/immutable/nodes/13.TOXVaTBr.js.br +0 -0
- package/src/templates/skills/konoha/references/architecture.md +0 -99
- package/src/templates/skills/konoha/references/benchmark.md +0 -123
- package/src/templates/skills/konoha/references/docs.md +0 -46
- package/src/templates/skills/konoha/references/test.md +0 -71
- package/src/templates/skills/konoha/references/workflow.md +0 -104
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "konoha-mcp",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.24",
|
|
4
4
|
"description": "Autonomous multi-agent MCP orchestration platform powered by LangGraph state machines, 8 specialist ninja agents, SQLite FTS5 skill indexing, and per-turn token governors saving 83–98% context across 8 AI coding clients",
|
|
5
5
|
"homepage": "https://github.com/andycungkrinx91/konoha#readme",
|
|
6
6
|
"bugs": "https://github.com/andycungkrinx91/konoha/issues",
|
|
@@ -117,6 +117,7 @@
|
|
|
117
117
|
"ci:act": "act -W .github/workflows/cross-platform.yml -j checks --matrix os:ubuntu-latest",
|
|
118
118
|
"ci:act:shard": "act -W .github/workflows/cross-platform.yml -j test --matrix os:ubuntu-latest --matrix shard:1",
|
|
119
119
|
"test:sandbox": "node scripts/rtk_exec.js node scripts/sandbox.js",
|
|
120
|
+
"audit:socket": "node scripts/rtk_exec.js node scripts/socket_audit_optional.js",
|
|
120
121
|
"test:mcp": "node scripts/rtk_exec.js node tests/test_mcp_protocol.js && node scripts/rtk_exec.js node tests/test_mcp_e2e.js",
|
|
121
122
|
"test:workflow": "node scripts/rtk_exec.js node tests/test_workflow_gates.js && node scripts/rtk_exec.js node tests/test_workflow_loop.js",
|
|
122
123
|
"test:jonin": "node scripts/rtk_exec.js node tests/test_build_workflows.js && node scripts/rtk_exec.js node tests/test_taste_skill_jonin.js && node scripts/rtk_exec.js node tests/test_jonin_universal_design.js",
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
diff --git a/src/scraper/README.md b/src/scraper/README.md
|
|
2
|
+
deleted file mode 100644
|
|
3
|
+
index cd9594ccf987634b358aed651d6124f23bad158e..0000000000000000000000000000000000000000
|
|
4
|
+
diff --git a/src/scraper/modelScrape.browser.js b/src/scraper/modelScrape.browser.js
|
|
5
|
+
deleted file mode 100644
|
|
6
|
+
index cfd7ff875f242a1cffaa3a7bb252c13fb1fec2df..0000000000000000000000000000000000000000
|
|
7
|
+
diff --git a/src/scraper/modelScrape.browser.test.js b/src/scraper/modelScrape.browser.test.js
|
|
8
|
+
deleted file mode 100644
|
|
9
|
+
index 7819ced89dd9f55b68bddb39ef8c73f8644457fa..0000000000000000000000000000000000000000
|
|
10
|
+
diff --git a/src/scraper/modelScrape.node.test.ts b/src/scraper/modelScrape.node.test.ts
|
|
11
|
+
deleted file mode 100644
|
|
12
|
+
index 641575d4617f31f149d0943c80648faa0baa359e..0000000000000000000000000000000000000000
|
|
13
|
+
diff --git a/src/scraper/modelScrape.node.ts b/src/scraper/modelScrape.node.ts
|
|
14
|
+
deleted file mode 100644
|
|
15
|
+
index a4c3549d4e8ae710ffac351111f7a4270272cdd6..0000000000000000000000000000000000000000
|
package/src/agent_contract.js
CHANGED
package/src/agent_deployer.js
CHANGED
|
@@ -208,47 +208,58 @@ function cleanObsoleteAgentDirs(_home = null) {
|
|
|
208
208
|
function deployGlobalClientConfigs(options) {
|
|
209
209
|
const { pythonCmd, serverPath, agents, projectRoot, deployProject, home } = options;
|
|
210
210
|
const effectiveHome = home || process.env.HOME || process.env.USERPROFILE || os.homedir();
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
} catch (_) { /* intentional best-effort fallback */ }
|
|
238
|
-
|
|
239
|
-
try {
|
|
240
|
-
const piManager = require("./pi_manager");
|
|
211
|
+
// One client failing must not stop the others, but it must not disappear
|
|
212
|
+
// either: swallowing every error made a half-deployed setup look complete.
|
|
213
|
+
const failures = [];
|
|
214
|
+
const run = (client, fn) => {
|
|
215
|
+
try { fn(); } catch (err) { failures.push({ client, error: err.message }); }
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
run('claudecode', () => mcpClientsManager.ensureClaudeCodeSetup({
|
|
219
|
+
pythonCmd, serverPath, silent: true,
|
|
220
|
+
ruleContent: generateClaudeCodeMd(agents), agents, projectRoot, deployProject, home: effectiveHome
|
|
221
|
+
}));
|
|
222
|
+
|
|
223
|
+
run('opencode', () => require('./opencode_manager').ensureOpenCodeSetup({
|
|
224
|
+
pythonCmd, serverPath, agents, projectRoot, deployProject, silent: true, home: effectiveHome
|
|
225
|
+
}));
|
|
226
|
+
|
|
227
|
+
run('codex', () => require('./codex_manager').ensureCodexSetup({
|
|
228
|
+
pythonCmd, serverPath, agents, projectRoot, deployProject, silent: true, home: effectiveHome
|
|
229
|
+
}));
|
|
230
|
+
|
|
231
|
+
run('commandcode', () => mcpClientsManager.ensureCommandCodeSetup({
|
|
232
|
+
pythonCmd, serverPath, agents, projectRoot, deployProject, silent: true, home: effectiveHome
|
|
233
|
+
}));
|
|
234
|
+
|
|
235
|
+
run('pi', () => {
|
|
236
|
+
const piManager = require('./pi_manager');
|
|
241
237
|
if (options.force || piManager.isPiInstalled()) {
|
|
242
238
|
piManager.ensurePiSetup({ pythonCmd, serverPath, silent: true, home: effectiveHome, force: options.force, skipDetection: options.force });
|
|
243
239
|
}
|
|
244
|
-
}
|
|
240
|
+
});
|
|
245
241
|
|
|
246
|
-
|
|
247
|
-
const ompManager = require(
|
|
242
|
+
run('omp', () => {
|
|
243
|
+
const ompManager = require('./omp_manager');
|
|
248
244
|
if (options.force || ompManager.isOmpInstalled()) {
|
|
249
245
|
ompManager.ensureOmpSetup({ pythonCmd, serverPath, silent: true, home: effectiveHome, force: options.force });
|
|
250
246
|
}
|
|
251
|
-
}
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
let commands = null;
|
|
250
|
+
run('commands', () => {
|
|
251
|
+
commands = require('./client_commands').deployKonohaCommands({
|
|
252
|
+
home: effectiveHome,
|
|
253
|
+
projectRoot: deployProject ? projectRoot : null,
|
|
254
|
+
force: options.force
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
if (failures.length > 0) {
|
|
259
|
+
const detail = failures.map((f) => `${f.client}: ${f.error}`).join('; ');
|
|
260
|
+
process.stderr.write(`[konoha deploy] ${failures.length} client deployment(s) incomplete: ${detail}\n`);
|
|
261
|
+
}
|
|
262
|
+
return { failures, commands };
|
|
252
263
|
}
|
|
253
264
|
|
|
254
265
|
function regenerateAndDeploy(silentOrOptions = false) {
|
|
@@ -325,7 +336,7 @@ function regenerateAndDeploy(silentOrOptions = false) {
|
|
|
325
336
|
} catch (_) { /* intentional fallback */ }
|
|
326
337
|
|
|
327
338
|
cleanObsoleteAgentDirs(effectiveHome);
|
|
328
|
-
deployGlobalClientConfigs({ pythonCmd, serverPath, agents, projectRoot, deployProject, home: effectiveHome, force });
|
|
339
|
+
const deployment = deployGlobalClientConfigs({ pythonCmd, serverPath, agents, projectRoot, deployProject, home: effectiveHome, force });
|
|
329
340
|
|
|
330
341
|
try {
|
|
331
342
|
deployUtils.syncSoulAcrossClients({ home: effectiveHome, silent });
|
|
@@ -343,6 +354,7 @@ function regenerateAndDeploy(silentOrOptions = false) {
|
|
|
343
354
|
} catch { /* intentional fallback */ }
|
|
344
355
|
}
|
|
345
356
|
|
|
357
|
+
return { ok: deployment.failures.length === 0, failures: deployment.failures, commands: deployment.commands };
|
|
346
358
|
}
|
|
347
359
|
|
|
348
360
|
function deployProjectWorkspace(_projectRoot, _options = {}) {
|
|
@@ -360,6 +372,7 @@ module.exports = {
|
|
|
360
372
|
stripLegacyKonohaPreamble,
|
|
361
373
|
isProtectedUserPath,
|
|
362
374
|
cleanObsoleteAgentDirs,
|
|
375
|
+
deployGlobalClientConfigs,
|
|
363
376
|
regenerateAndDeploy,
|
|
364
377
|
deployProjectWorkspace
|
|
365
378
|
};
|
package/src/agent_file_safety.js
CHANGED
|
@@ -152,6 +152,62 @@ function _writeProtectedFileSafe(targetPath, content, isOwnedFn, conflictMeta =
|
|
|
152
152
|
return { status: 'written', path: targetPath };
|
|
153
153
|
}
|
|
154
154
|
|
|
155
|
+
const AGENT_PROFILE_DIRS = Object.freeze([
|
|
156
|
+
['.claude', 'agents'],
|
|
157
|
+
['.cursor', 'agents'],
|
|
158
|
+
['.codex', 'agents'],
|
|
159
|
+
['.config', 'opencode', 'agents'],
|
|
160
|
+
['.commandcode', 'agents'],
|
|
161
|
+
['.omp', 'agent', 'agents']
|
|
162
|
+
]);
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Classifies one parked `.konoha-proposed` candidate against the live file:
|
|
166
|
+
* `foreign` the live file is not Konoha's (or is unreadable), so Konoha's
|
|
167
|
+
* version never landed
|
|
168
|
+
* `stale` the live file is Konoha's own and differs from the parked copy;
|
|
169
|
+
* the candidate is residue, since Konoha's content is in place
|
|
170
|
+
* `identical` the parked copy equals the live file exactly
|
|
171
|
+
*/
|
|
172
|
+
function _classifyAgentConflict(dir, entry) {
|
|
173
|
+
const proposed = path.join(dir, entry);
|
|
174
|
+
const target = path.join(dir, entry.slice(0, -'.konoha-proposed'.length));
|
|
175
|
+
const backup = `${target}.konoha-backup`;
|
|
176
|
+
const record = (kind) => ({ path: target, proposed, backup: fileExists(backup) ? backup : null, kind });
|
|
177
|
+
if (!fileExists(target)) return record('foreign');
|
|
178
|
+
let live;
|
|
179
|
+
try {
|
|
180
|
+
live = fs.readFileSync(target, 'utf8');
|
|
181
|
+
if (live === fs.readFileSync(proposed, 'utf8')) return record('identical');
|
|
182
|
+
} catch (_) {
|
|
183
|
+
return record('foreign');
|
|
184
|
+
}
|
|
185
|
+
return record(isKonohaOwnedAgentFile(target) ? 'stale' : 'foreign');
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Reports agent files where Konoha's candidate was parked at
|
|
190
|
+
* `.konoha-proposed` instead of being deployed. Read-only: nothing is applied
|
|
191
|
+
* and nothing is deleted, because the file in place may be user-owned. Without
|
|
192
|
+
* this the conflict is invisible and the foreign profile silently stays in
|
|
193
|
+
* effect — which is how a client ends up spawning an agent nobody can resolve.
|
|
194
|
+
* @param {string} home user home directory
|
|
195
|
+
* @returns {Array<{path:string, proposed:string, backup:string|null, kind:string}>}
|
|
196
|
+
*/
|
|
197
|
+
function listAgentConflicts(home) {
|
|
198
|
+
const root = home || require('os').homedir();
|
|
199
|
+
const conflicts = [];
|
|
200
|
+
for (const segments of AGENT_PROFILE_DIRS) {
|
|
201
|
+
const dir = path.join(root, ...segments);
|
|
202
|
+
let entries = [];
|
|
203
|
+
try { entries = fs.readdirSync(dir); } catch (_) { continue; }
|
|
204
|
+
for (const entry of entries) {
|
|
205
|
+
if (entry.endsWith('.konoha-proposed')) conflicts.push(_classifyAgentConflict(dir, entry));
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return conflicts;
|
|
209
|
+
}
|
|
210
|
+
|
|
155
211
|
/**
|
|
156
212
|
* Safely writes an agent definition file without clobbering user-authored files.
|
|
157
213
|
* Pre-existing foreign files stay intact, receive a one-time `.konoha-backup`,
|
|
@@ -208,6 +264,7 @@ module.exports = {
|
|
|
208
264
|
isKonohaManagedRuleFile,
|
|
209
265
|
writeAgentFileSafe,
|
|
210
266
|
writeManagedFileSafe,
|
|
267
|
+
listAgentConflicts,
|
|
211
268
|
snapshotNonKonohaRuleFiles,
|
|
212
269
|
restoreNonKonohaRuleFiles
|
|
213
270
|
};
|
package/src/bridge/gateway.js
CHANGED
|
@@ -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 {
|
|
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
|
|
123
|
-
|
|
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' }
|
|
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
|
|
143
|
-
|
|
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
|
|
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
|
|
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
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
*
|
|
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
|
|
253
|
+
const measured = normalizeUsage(openAiResp && openAiResp.usage);
|
|
206
254
|
const result = {
|
|
207
|
-
input_tokens:
|
|
208
|
-
output_tokens:
|
|
255
|
+
input_tokens: measured.available ? measured.input_tokens : null,
|
|
256
|
+
output_tokens: measured.available ? measured.output_tokens : null
|
|
209
257
|
};
|
|
210
|
-
|
|
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
|
-
|
|
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, {
|
|
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
|