@evomap/evolver 1.90.0 → 2.0.0-beta.1
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/README.md +27 -564
- package/bin/evolver-llm-proxy.js +3 -0
- package/bin/evolver-mcp.js +2 -0
- package/bin/evolver-proxy.js +3 -0
- package/bin/evolver.js +4 -0
- package/index.js +1 -3670
- package/package.json +32 -62
- package/CONTRIBUTING.md +0 -19
- package/LICENSE +0 -641
- package/README.ja-JP.md +0 -521
- package/README.ko-KR.md +0 -520
- package/README.zh-CN.md +0 -531
- package/SKILL.md +0 -365
- package/assets/cover.png +0 -0
- package/assets/gep/genes.seed.json +0 -496
- package/conformance/savings-core/constants.json +0 -30
- package/conformance/savings-core/golden-vectors.json +0 -333
- package/scripts/a2a_export.js +0 -63
- package/scripts/a2a_ingest.js +0 -79
- package/scripts/a2a_promote.js +0 -118
- package/scripts/analyze_by_skill.js +0 -121
- package/scripts/build_binaries.js +0 -479
- package/scripts/check-changelog.js +0 -166
- package/scripts/extract_log.js +0 -85
- package/scripts/generate_history.js +0 -75
- package/scripts/gep_append_event.js +0 -96
- package/scripts/gep_personality_report.js +0 -234
- package/scripts/human_report.js +0 -147
- package/scripts/recall-verify-report.js +0 -234
- package/scripts/recover_loop.js +0 -61
- package/scripts/refresh_stars_badge.js +0 -168
- package/scripts/seed-merchants.js +0 -91
- package/scripts/skill2recipes.js +0 -118
- package/scripts/suggest_version.js +0 -89
- package/scripts/validate-modules.js +0 -38
- package/scripts/validate-suite.js +0 -78
- package/src/adapters/claudeCode.js +0 -194
- package/src/adapters/codex.js +0 -216
- package/src/adapters/cursor.js +0 -91
- package/src/adapters/hookAdapter.js +0 -469
- package/src/adapters/kiro.js +0 -195
- package/src/adapters/opencode.js +0 -326
- package/src/adapters/scripts/_lockPaths.js +0 -74
- package/src/adapters/scripts/_memoryFiltering.js +0 -35
- package/src/adapters/scripts/_runtimePaths.js +0 -440
- package/src/adapters/scripts/evolver-session-end.js +0 -321
- package/src/adapters/scripts/evolver-session-start.js +0 -587
- package/src/adapters/scripts/evolver-signal-detect.js +0 -98
- package/src/adapters/scripts/evolver-task-recall.js +0 -173
- package/src/atp/atpExecute.js +0 -283
- package/src/atp/atpTaskPickup.js +0 -233
- package/src/atp/autoBuyer.js +0 -382
- package/src/atp/autoDeliver.js +0 -215
- package/src/atp/cli.js +0 -354
- package/src/atp/cliAutobuyPrompt.js +0 -154
- package/src/atp/consumerAgent.js +0 -157
- package/src/atp/defaultHandler.js +0 -69
- package/src/atp/heartbeatSignalsHandler.js +0 -254
- package/src/atp/hubClient.js +0 -317
- package/src/atp/index.js +0 -38
- package/src/atp/merchantAgent.js +0 -118
- package/src/atp/protocol.js +0 -41
- package/src/atp/questionComposer.js +0 -133
- package/src/atp/serviceHelper.js +0 -92
- package/src/canary.js +0 -13
- package/src/config.js +0 -351
- package/src/evolve/guards.js +0 -1
- package/src/evolve/pipeline/collect.js +0 -1
- package/src/evolve/pipeline/dispatch.js +0 -1
- package/src/evolve/pipeline/enrich.js +0 -1
- package/src/evolve/pipeline/hub.js +0 -1
- package/src/evolve/pipeline/select.js +0 -1
- package/src/evolve/pipeline/signals.js +0 -1
- package/src/evolve/utils.js +0 -1
- package/src/evolve.js +0 -1
- package/src/experiment/agentRunner.js +0 -229
- package/src/experiment/cli.js +0 -159
- package/src/experiment/comparison.js +0 -233
- package/src/experiment/metrics.js +0 -75
- package/src/forceUpdate.js +0 -991
- package/src/gep/a2a.js +0 -173
- package/src/gep/a2aProtocol.js +0 -1
- package/src/gep/analyzer.js +0 -35
- package/src/gep/antiAbuseTelemetry.js +0 -1
- package/src/gep/assetCallLog.js +0 -197
- package/src/gep/assetStore.js +0 -723
- package/src/gep/assets.js +0 -36
- package/src/gep/autoDistillConv.js +0 -1
- package/src/gep/autoDistillLlm.js +0 -1
- package/src/gep/bridge.js +0 -138
- package/src/gep/candidateEval.js +0 -1
- package/src/gep/candidates.js +0 -1
- package/src/gep/claimNudge.js +0 -121
- package/src/gep/cliContracts.js +0 -1190
- package/src/gep/contentHash.js +0 -1
- package/src/gep/conversationDistiller.js +0 -1
- package/src/gep/conversationSniffer.js +0 -1
- package/src/gep/crypto.js +0 -1
- package/src/gep/curriculum.js +0 -1
- package/src/gep/deviceId.js +0 -1
- package/src/gep/directoryClient.js +0 -115
- package/src/gep/envFingerprint.js +0 -1
- package/src/gep/epigenetics.js +0 -1
- package/src/gep/execBridge.js +0 -1
- package/src/gep/executionTrace.js +0 -291
- package/src/gep/explore.js +0 -1
- package/src/gep/featureFlags.js +0 -121
- package/src/gep/gitOps.js +0 -265
- package/src/gep/hash.js +0 -1
- package/src/gep/hostErrorClassifier.js +0 -34
- package/src/gep/hubFetch.js +0 -1
- package/src/gep/hubReview.js +0 -1
- package/src/gep/hubSearch.js +0 -1
- package/src/gep/hubVerify.js +0 -1
- package/src/gep/idleScheduler.js +0 -400
- package/src/gep/issueReporter.js +0 -416
- package/src/gep/learningSignals.js +0 -1
- package/src/gep/llmReview.js +0 -92
- package/src/gep/localStateAwareness.js +0 -243
- package/src/gep/mailboxTransport.js +0 -119
- package/src/gep/memoryGraph.js +0 -1
- package/src/gep/memoryGraphAdapter.js +0 -1
- package/src/gep/mutation.js +0 -1
- package/src/gep/narrativeMemory.js +0 -1
- package/src/gep/oauthLogin.js +0 -181
- package/src/gep/openPRRegistry.js +0 -1
- package/src/gep/paths.js +0 -522
- package/src/gep/personality.js +0 -1
- package/src/gep/policyCheck.js +0 -1
- package/src/gep/portable.js +0 -103
- package/src/gep/privacyClient.js +0 -235
- package/src/gep/prompt.js +0 -1
- package/src/gep/questionGenerator.js +0 -518
- package/src/gep/recallInject.js +0 -1
- package/src/gep/recallVerifier.js +0 -1
- package/src/gep/reflection.js +0 -1
- package/src/gep/sanitize.js +0 -264
- package/src/gep/savingsCore.js +0 -1
- package/src/gep/schemas/capsule.js +0 -170
- package/src/gep/schemas/gene.js +0 -154
- package/src/gep/schemas/index.js +0 -8
- package/src/gep/schemas/protocol.js +0 -51
- package/src/gep/schemas/task.js +0 -74
- package/src/gep/selector.js +0 -1
- package/src/gep/selfPR.js +0 -469
- package/src/gep/signals.js +0 -776
- package/src/gep/skill2gep.js +0 -1056
- package/src/gep/skill2gepAudit.js +0 -303
- package/src/gep/skill2recipes.js +0 -511
- package/src/gep/skillDistiller.js +0 -1
- package/src/gep/skillPublisher.js +0 -358
- package/src/gep/solidify.js +0 -1
- package/src/gep/strategy.js +0 -1
- package/src/gep/taskReceiver.js +0 -575
- package/src/gep/tokenSavings.js +0 -1
- package/src/gep/trajectoryExport.js +0 -1
- package/src/gep/validationReport.js +0 -55
- package/src/gep/validator/index.js +0 -411
- package/src/gep/validator/reporter.js +0 -210
- package/src/gep/validator/sandboxExecutor.js +0 -480
- package/src/gep/validator/stakeBootstrap.js +0 -357
- package/src/gep/workspaceKeychain.js +0 -1
- package/src/ops/cleanup.js +0 -80
- package/src/ops/commentary.js +0 -60
- package/src/ops/health_check.js +0 -104
- package/src/ops/index.js +0 -11
- package/src/ops/innovation.js +0 -67
- package/src/ops/lifecycle.js +0 -798
- package/src/ops/self_repair.js +0 -76
- package/src/ops/skills_monitor.js +0 -147
- package/src/ops/trigger.js +0 -33
- package/src/proxy/clientSettings.js +0 -405
- package/src/proxy/envelope.js +0 -59
- package/src/proxy/extensions/dmHandler.js +0 -45
- package/src/proxy/extensions/sessionHandler.js +0 -141
- package/src/proxy/extensions/skillUpdater.js +0 -64
- package/src/proxy/extensions/traceControl.js +0 -1
- package/src/proxy/index.js +0 -1395
- package/src/proxy/inject.js +0 -1
- package/src/proxy/lifecycle/manager.js +0 -1568
- package/src/proxy/mailbox/state.js +0 -207
- package/src/proxy/mailbox/store.js +0 -602
- package/src/proxy/router/cache_passthrough.js +0 -26
- package/src/proxy/router/features.js +0 -84
- package/src/proxy/router/gemini_route.js +0 -154
- package/src/proxy/router/messages_route.js +0 -535
- package/src/proxy/router/model_router.js +0 -113
- package/src/proxy/router/models_route.js +0 -52
- package/src/proxy/router/ollama_route.js +0 -103
- package/src/proxy/router/responses_route.js +0 -170
- package/src/proxy/router/vertex_route.js +0 -110
- package/src/proxy/server/http.js +0 -363
- package/src/proxy/server/routes.js +0 -558
- package/src/proxy/server/settings.js +0 -115
- package/src/proxy/sync/engine.js +0 -179
- package/src/proxy/sync/inbound.js +0 -211
- package/src/proxy/sync/outbound.js +0 -361
- package/src/proxy/task/monitor.js +0 -131
- package/src/proxy/trace/extractor.js +0 -1
- package/src/proxy/trace/usage.js +0 -1
- package/src/solo/breaker.js +0 -25
- package/src/solo/gitGuard.js +0 -65
- package/src/webui/client/clientJs/assets.js +0 -111
- package/src/webui/client/clientJs/bootstrap.js +0 -92
- package/src/webui/client/clientJs/common.js +0 -77
- package/src/webui/client/clientJs/i18n.js +0 -366
- package/src/webui/client/clientJs/index.js +0 -35
- package/src/webui/client/clientJs/interactions.js +0 -351
- package/src/webui/client/clientJs/overview.js +0 -152
- package/src/webui/client/clientJs/personality.js +0 -285
- package/src/webui/client/clientJs/pipelines.js +0 -330
- package/src/webui/client/indexHtml.js +0 -221
- package/src/webui/client/static.js +0 -23
- package/src/webui/client/stylesCss.js +0 -639
- package/src/webui/client/vendor/README.md +0 -15
- package/src/webui/client/vendor/echarts.min.js +0 -45
- package/src/webui/index.js +0 -14
- package/src/webui/observer/assets.js +0 -146
- package/src/webui/observer/index.js +0 -37
- package/src/webui/observer/interactions.js +0 -127
- package/src/webui/observer/jsonl.js +0 -75
- package/src/webui/observer/paths.js +0 -46
- package/src/webui/observer/personality.js +0 -43
- package/src/webui/observer/pipelineEvents.js +0 -58
- package/src/webui/observer/redact.js +0 -63
- package/src/webui/observer/runs.js +0 -356
- package/src/webui/observer/safety.js +0 -57
- package/src/webui/observer/skills.js +0 -70
- package/src/webui/observer/status.js +0 -71
- package/src/webui/server/http.js +0 -138
- package/src/webui/server/routes.js +0 -41
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// GET /v1/models — model-list probe passthrough (format-aware routing, no translation). Many clients (codex,
|
|
4
|
-
// opencode, cursor in OpenAI mode, OpenAI/Anthropic SDKs) hit GET /v1/models on startup to validate the endpoint
|
|
5
|
-
// and list models; without a route they get a 404 and may fail to initialize or fall back. Route by the client's
|
|
6
|
-
// intended provider: Anthropic clients send the `anthropic-version` header → Anthropic /v1/models; everyone else
|
|
7
|
-
// → OpenAI /v1/models (the convention's home). Forwarded verbatim to the native upstream, no body, no translation.
|
|
8
|
-
// Not traced: it is a metadata probe, not an LLM call.
|
|
9
|
-
|
|
10
|
-
function detectModelsProvider(headers = {}) {
|
|
11
|
-
const lower = {};
|
|
12
|
-
for (const [k, v] of Object.entries(headers || {})) lower[String(k).toLowerCase()] = v;
|
|
13
|
-
// anthropic-version (and anthropic-beta) are sent by every Anthropic SDK request and by nothing else.
|
|
14
|
-
if (lower['anthropic-version'] || lower['anthropic-beta']) return 'anthropic';
|
|
15
|
-
return 'openai';
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function buildModelsHandler({ anthropicProxy, openAIProxy, logger } = {}) {
|
|
19
|
-
if (typeof anthropicProxy !== 'function' || typeof openAIProxy !== 'function') {
|
|
20
|
-
throw new Error('buildModelsHandler requires anthropicProxy(path,body,opts) + openAIProxy(path,body,opts)');
|
|
21
|
-
}
|
|
22
|
-
const log = logger || console;
|
|
23
|
-
|
|
24
|
-
return async ({ headers }) => {
|
|
25
|
-
const inboundHeaders = headers || {};
|
|
26
|
-
const provider = detectModelsProvider(inboundHeaders);
|
|
27
|
-
const [proxyFn, reqPath, mode] = provider === 'anthropic'
|
|
28
|
-
? [anthropicProxy, '/v1/models', 'anthropic']
|
|
29
|
-
: [openAIProxy, '/models', 'openai'];
|
|
30
|
-
|
|
31
|
-
let up;
|
|
32
|
-
try {
|
|
33
|
-
up = await proxyFn(reqPath, null, { method: 'GET', inboundHeaders, upstreamMode: mode });
|
|
34
|
-
} catch (err) {
|
|
35
|
-
const status = Number(err && err.statusCode) || 502;
|
|
36
|
-
return { status, body: { error: (err && err.message) || 'models upstream request failed' } };
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
let raw = '';
|
|
40
|
-
try { raw = up.text ? await up.text() : ''; } catch { raw = ''; }
|
|
41
|
-
let body;
|
|
42
|
-
try {
|
|
43
|
-
body = raw ? JSON.parse(raw) : {};
|
|
44
|
-
} catch {
|
|
45
|
-
log.warn?.(JSON.stringify({ event: 'models_fallback', reason: 'upstream_non_json', upstream_status: up.status }));
|
|
46
|
-
body = { error: raw };
|
|
47
|
-
}
|
|
48
|
-
return { status: up.status, body };
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
module.exports = { buildModelsHandler, detectModelsProvider };
|
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// Ollama native passthrough (local/self-hosted model server). Native paths POST /api/chat and /api/generate are
|
|
4
|
-
// forwarded verbatim to the Ollama upstream (EVOMAP_OLLAMA_BASE_URL, default 127.0.0.1:11434). Format-aware, no
|
|
5
|
-
// translation: an Ollama-shaped request goes to Ollama. Streaming is newline-delimited JSON (NDJSON), not SSE —
|
|
6
|
-
// the trace tee scans it the same way. apiPath is fixed per registration (/api/chat vs /api/generate). Ollama is
|
|
7
|
-
// typically auth-less; an optional bearer (EVOMAP_OLLAMA_API_KEY) covers a remote/protected instance.
|
|
8
|
-
|
|
9
|
-
const { createProxyTrace } = require('../trace/extractor');
|
|
10
|
-
|
|
11
|
-
function upstreamStatus(err, fallback = 502) {
|
|
12
|
-
const status = Number(err && err.statusCode);
|
|
13
|
-
return Number.isFinite(status) ? status : fallback;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function asUpstreamError(err, fallback = 502) {
|
|
17
|
-
if (err && err.statusCode && /^ollama upstream /.test(err.message || '')) return err;
|
|
18
|
-
const out = new Error('ollama upstream request failed');
|
|
19
|
-
out.statusCode = upstreamStatus(err, fallback);
|
|
20
|
-
out.cause = err;
|
|
21
|
-
return out;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function responseToBody(raw, status, headers, log) {
|
|
25
|
-
if (!raw) return {};
|
|
26
|
-
try {
|
|
27
|
-
return JSON.parse(raw);
|
|
28
|
-
} catch {
|
|
29
|
-
log.warn?.(JSON.stringify({
|
|
30
|
-
event: 'ollama_fallback',
|
|
31
|
-
reason: 'upstream_non_json',
|
|
32
|
-
upstream_status: status,
|
|
33
|
-
content_type: (headers && headers['content-type']) || '',
|
|
34
|
-
response_bytes: Buffer.byteLength(raw),
|
|
35
|
-
}));
|
|
36
|
-
return { error: raw };
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function buildOllamaHandler({ ollamaProxy, logger, traceStore, onTraceQueued, apiPath = '/api/chat' } = {}) {
|
|
41
|
-
if (typeof ollamaProxy !== 'function') {
|
|
42
|
-
throw new Error('buildOllamaHandler requires ollamaProxy(path, body, opts)');
|
|
43
|
-
}
|
|
44
|
-
const log = logger || console;
|
|
45
|
-
|
|
46
|
-
return async ({ body, headers }) => {
|
|
47
|
-
const inboundHeaders = headers || {};
|
|
48
|
-
const originalModel = body && typeof body.model === 'string' ? body.model : null;
|
|
49
|
-
|
|
50
|
-
let trace = null;
|
|
51
|
-
try {
|
|
52
|
-
trace = createProxyTrace({
|
|
53
|
-
route: `POST ${apiPath}`,
|
|
54
|
-
headers: inboundHeaders,
|
|
55
|
-
body,
|
|
56
|
-
upstreamMode: 'ollama',
|
|
57
|
-
originalModel,
|
|
58
|
-
chosenModel: originalModel,
|
|
59
|
-
store: traceStore,
|
|
60
|
-
logger: traceStore ? log : null,
|
|
61
|
-
onTraceQueued,
|
|
62
|
-
});
|
|
63
|
-
} catch (_) { /* best-effort trace; never break the request */ }
|
|
64
|
-
|
|
65
|
-
let upstream;
|
|
66
|
-
try {
|
|
67
|
-
upstream = await ollamaProxy(apiPath, body, { inboundHeaders, upstreamMode: 'ollama' });
|
|
68
|
-
} catch (err) {
|
|
69
|
-
const wrapped = asUpstreamError(err, upstreamStatus(err));
|
|
70
|
-
trace?.record({ status: wrapped.statusCode, error: wrapped, upstreamMode: 'ollama', model: originalModel });
|
|
71
|
-
throw wrapped;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (upstream.stream) {
|
|
75
|
-
const forwardHeaders = {};
|
|
76
|
-
const ct = upstream.headers && upstream.headers['content-type'];
|
|
77
|
-
if (ct) forwardHeaders['Content-Type'] = ct;
|
|
78
|
-
trace?.recordStreamStart({ status: upstream.status, upstreamMode: 'ollama', model: originalModel, headers: forwardHeaders });
|
|
79
|
-
return {
|
|
80
|
-
status: upstream.status,
|
|
81
|
-
// Tee the NDJSON body so the deferred trace captures the final chunk's eval counts + done_reason. Bytes unchanged.
|
|
82
|
-
stream: trace ? trace.observeStream(upstream.stream) : upstream.stream,
|
|
83
|
-
headers: forwardHeaders,
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
let raw = '';
|
|
88
|
-
if (upstream.text) {
|
|
89
|
-
try {
|
|
90
|
-
raw = await upstream.text();
|
|
91
|
-
} catch (err) {
|
|
92
|
-
const wrapped = asUpstreamError(err, upstreamStatus(err));
|
|
93
|
-
trace?.record({ status: wrapped.statusCode, error: wrapped, upstreamMode: 'ollama', model: originalModel });
|
|
94
|
-
throw wrapped;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
const respBody = responseToBody(raw, upstream.status, upstream.headers, log);
|
|
98
|
-
trace?.record({ status: upstream.status, responseBody: respBody, upstreamMode: 'ollama', model: originalModel, headers: upstream.headers });
|
|
99
|
-
return { status: upstream.status, body: respBody };
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
module.exports = { buildOllamaHandler, responseToBody };
|
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const { createProxyTrace } = require('../trace/extractor');
|
|
4
|
-
|
|
5
|
-
const OPENAI_RESPONSE_HEADER_ALLOWLIST = new Set([
|
|
6
|
-
'openai-processing-ms',
|
|
7
|
-
'openai-version',
|
|
8
|
-
'retry-after',
|
|
9
|
-
'x-request-id',
|
|
10
|
-
]);
|
|
11
|
-
|
|
12
|
-
function hasOpenAIUpstreamCredential() {
|
|
13
|
-
if (process.env.EVOMAP_OPENAI_API_KEY || process.env.OPENAI_API_KEY) return true;
|
|
14
|
-
return false;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function upstreamStatus(err, fallback = 502) {
|
|
18
|
-
const status = Number(err && err.statusCode);
|
|
19
|
-
return Number.isFinite(status) ? status : fallback;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function safeOpenAIConfigDiagnostic(err) {
|
|
23
|
-
const message = err && typeof err.message === 'string' ? err.message : '';
|
|
24
|
-
if (message.startsWith('[proxy] EVOMAP_OPENAI_BASE_URL ')) return message;
|
|
25
|
-
return '';
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function asUpstreamError(err, fallback = 502) {
|
|
29
|
-
if (err && err.statusCode && /^openai upstream /.test(err.message || '')) return err;
|
|
30
|
-
const diagnostic = safeOpenAIConfigDiagnostic(err);
|
|
31
|
-
const message = diagnostic
|
|
32
|
-
? `openai upstream request failed: ${diagnostic}`
|
|
33
|
-
: 'openai upstream request failed';
|
|
34
|
-
const out = new Error(message);
|
|
35
|
-
out.statusCode = upstreamStatus(err, fallback);
|
|
36
|
-
out.cause = err;
|
|
37
|
-
return out;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function responseToBody(raw, status, headers, log) {
|
|
41
|
-
if (!raw) return {};
|
|
42
|
-
try {
|
|
43
|
-
return JSON.parse(raw);
|
|
44
|
-
} catch {
|
|
45
|
-
log.warn?.(JSON.stringify({
|
|
46
|
-
event: 'openai_responses_fallback',
|
|
47
|
-
reason: 'upstream_non_json',
|
|
48
|
-
upstream_status: status,
|
|
49
|
-
content_type: headers && headers['content-type'] || '',
|
|
50
|
-
response_bytes: Buffer.byteLength(raw),
|
|
51
|
-
}));
|
|
52
|
-
return { error: raw };
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function copyOpenAIResponseHeaders(headers = {}) {
|
|
57
|
-
const out = {};
|
|
58
|
-
for (const [name, value] of Object.entries(headers || {})) {
|
|
59
|
-
const lower = String(name || '').toLowerCase();
|
|
60
|
-
if (!OPENAI_RESPONSE_HEADER_ALLOWLIST.has(lower) && !lower.startsWith('x-ratelimit-')) continue;
|
|
61
|
-
if (value === undefined || value === null) continue;
|
|
62
|
-
const headerValue = Array.isArray(value) ? value.join(', ') : String(value);
|
|
63
|
-
if (/[\r\n]/.test(headerValue)) continue;
|
|
64
|
-
out[lower] = headerValue;
|
|
65
|
-
}
|
|
66
|
-
return out;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// Generic OpenAI passthrough handler. `upstreamPath` selects the OpenAI endpoint (/responses for codex's
|
|
70
|
-
// Responses API, /chat/completions for the Chat Completions API used by cursor's OpenAI mode + generic OpenAI
|
|
71
|
-
// clients). Both share the same upstream, auth, header allow-list, trace, and stream tee — the only difference
|
|
72
|
-
// is the path + the trace route label. No translation: each OpenAI dialect goes to its native OpenAI endpoint.
|
|
73
|
-
function buildResponsesHandler({ openAIProxy, logger, traceStore, onTraceQueued, upstreamPath = '/responses', traceRoute = 'POST /v1/responses' } = {}) {
|
|
74
|
-
if (typeof openAIProxy !== 'function') {
|
|
75
|
-
throw new Error('buildResponsesHandler requires openAIProxy(path, body, opts)');
|
|
76
|
-
}
|
|
77
|
-
const log = logger || console;
|
|
78
|
-
|
|
79
|
-
return async ({ body, headers }) => {
|
|
80
|
-
const inboundHeaders = headers || {};
|
|
81
|
-
if (!hasOpenAIUpstreamCredential()) {
|
|
82
|
-
throw Object.assign(new Error('openai api key required'), { statusCode: 401 });
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const originalModel = body && typeof body.model === 'string' ? body.model : null;
|
|
86
|
-
let trace = null;
|
|
87
|
-
try {
|
|
88
|
-
trace = createProxyTrace({
|
|
89
|
-
route: traceRoute,
|
|
90
|
-
headers: inboundHeaders,
|
|
91
|
-
body,
|
|
92
|
-
upstreamMode: 'openai',
|
|
93
|
-
originalModel,
|
|
94
|
-
chosenModel: originalModel,
|
|
95
|
-
store: traceStore,
|
|
96
|
-
logger: traceStore ? log : null,
|
|
97
|
-
onTraceQueued,
|
|
98
|
-
});
|
|
99
|
-
} catch (_) { /* best-effort trace; never break the request */ }
|
|
100
|
-
|
|
101
|
-
let upstream;
|
|
102
|
-
try {
|
|
103
|
-
upstream = await openAIProxy(upstreamPath, body, {
|
|
104
|
-
inboundHeaders,
|
|
105
|
-
upstreamMode: 'openai',
|
|
106
|
-
});
|
|
107
|
-
} catch (err) {
|
|
108
|
-
const wrapped = asUpstreamError(err, upstreamStatus(err));
|
|
109
|
-
trace?.record({ status: wrapped.statusCode, error: wrapped, upstreamMode: 'openai', model: originalModel });
|
|
110
|
-
throw wrapped;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (upstream.stream) {
|
|
114
|
-
const forwardHeaders = copyOpenAIResponseHeaders(upstream.headers);
|
|
115
|
-
const ct = upstream.headers && upstream.headers['content-type'];
|
|
116
|
-
if (ct) forwardHeaders['Content-Type'] = ct;
|
|
117
|
-
trace?.recordStreamStart({
|
|
118
|
-
status: upstream.status,
|
|
119
|
-
upstreamMode: 'openai',
|
|
120
|
-
model: originalModel,
|
|
121
|
-
headers: forwardHeaders,
|
|
122
|
-
});
|
|
123
|
-
return {
|
|
124
|
-
status: upstream.status,
|
|
125
|
-
// Tee the codex SSE body so the deferred trace captures usage + response.id from response.completed.
|
|
126
|
-
// Bytes forward unchanged; emits once on stream end/cancel/error.
|
|
127
|
-
stream: trace ? trace.observeStream(upstream.stream) : upstream.stream,
|
|
128
|
-
headers: forwardHeaders,
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
let raw = '';
|
|
133
|
-
if (upstream.text) {
|
|
134
|
-
try {
|
|
135
|
-
raw = await upstream.text();
|
|
136
|
-
} catch (err) {
|
|
137
|
-
const wrapped = asUpstreamError(err, upstreamStatus(err));
|
|
138
|
-
trace?.record({ status: wrapped.statusCode, error: wrapped, upstreamMode: 'openai', model: originalModel });
|
|
139
|
-
throw wrapped;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
const respBody = responseToBody(raw, upstream.status, upstream.headers, log);
|
|
143
|
-
trace?.record({
|
|
144
|
-
status: upstream.status,
|
|
145
|
-
responseBody: respBody,
|
|
146
|
-
upstreamMode: 'openai',
|
|
147
|
-
model: originalModel,
|
|
148
|
-
headers: upstream.headers,
|
|
149
|
-
});
|
|
150
|
-
return {
|
|
151
|
-
status: upstream.status,
|
|
152
|
-
body: respBody,
|
|
153
|
-
headers: copyOpenAIResponseHeaders(upstream.headers),
|
|
154
|
-
};
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// OpenAI Chat Completions ingress (cursor's OpenAI mode + generic OpenAI clients). Same OpenAI upstream as the
|
|
159
|
-
// Responses handler, just the /chat/completions endpoint — point an OpenAI-Chat client's base URL at the proxy.
|
|
160
|
-
function buildChatCompletionsHandler(opts = {}) {
|
|
161
|
-
return buildResponsesHandler({ ...opts, upstreamPath: '/chat/completions', traceRoute: 'POST /v1/chat/completions' });
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
module.exports = {
|
|
165
|
-
buildResponsesHandler,
|
|
166
|
-
buildChatCompletionsHandler,
|
|
167
|
-
copyOpenAIResponseHeaders,
|
|
168
|
-
hasOpenAIUpstreamCredential,
|
|
169
|
-
responseToBody,
|
|
170
|
-
};
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// Vertex AI Gemini passthrough (enterprise GCP). Same Gemini request/response body as the AI Studio route, but
|
|
4
|
-
// the native Vertex path — POST /v1/projects/<project>/locations/<location>/publishers/google/models/<model>:
|
|
5
|
-
// generateContent | :streamGenerateContent — a region-specific upstream (<location>-aiplatform.googleapis.com),
|
|
6
|
-
// and OAuth Bearer auth (EVOMAP_VERTEX_ACCESS_TOKEN). Forwarded verbatim, no translation. Trace reuses the Gemini
|
|
7
|
-
// shape (usageMetadata + candidates[].finishReason), so only the path + base + auth differ from the AI Studio route.
|
|
8
|
-
|
|
9
|
-
const { createProxyTrace } = require('../trace/extractor');
|
|
10
|
-
const { parseModelAction } = require('./gemini_route');
|
|
11
|
-
|
|
12
|
-
function upstreamStatus(err, fallback = 502) {
|
|
13
|
-
const status = Number(err && err.statusCode);
|
|
14
|
-
return Number.isFinite(status) ? status : fallback;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function asUpstreamError(err, fallback = 502) {
|
|
18
|
-
if (err && err.statusCode && /^vertex /.test(err.message || '')) return err;
|
|
19
|
-
const out = new Error('vertex upstream request failed');
|
|
20
|
-
out.statusCode = upstreamStatus(err, fallback);
|
|
21
|
-
out.cause = err;
|
|
22
|
-
return out;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function responseToBody(raw, status, headers, log) {
|
|
26
|
-
if (!raw) return {};
|
|
27
|
-
try {
|
|
28
|
-
return JSON.parse(raw);
|
|
29
|
-
} catch {
|
|
30
|
-
log.warn?.(JSON.stringify({
|
|
31
|
-
event: 'vertex_fallback', reason: 'upstream_non_json', upstream_status: status,
|
|
32
|
-
content_type: (headers && headers['content-type']) || '', response_bytes: Buffer.byteLength(raw),
|
|
33
|
-
}));
|
|
34
|
-
return { error: raw };
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// Region-specific Vertex base. EVOMAP_VERTEX_BASE_URL overrides (e.g. the global aiplatform endpoint); otherwise
|
|
39
|
-
// derive <location>-aiplatform.googleapis.com. `global` uses the un-prefixed host.
|
|
40
|
-
function vertexBaseUrl(location) {
|
|
41
|
-
const override = (process.env.EVOMAP_VERTEX_BASE_URL || '').trim();
|
|
42
|
-
if (override) return override.replace(/\/+$/, '');
|
|
43
|
-
const loc = String(location || '').trim();
|
|
44
|
-
if (!loc || loc === 'global') return 'https://aiplatform.googleapis.com';
|
|
45
|
-
return `https://${loc}-aiplatform.googleapis.com`;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function buildVertexHandler({ vertexProxy, logger, traceStore, onTraceQueued } = {}) {
|
|
49
|
-
if (typeof vertexProxy !== 'function') {
|
|
50
|
-
throw new Error('buildVertexHandler requires vertexProxy(path, body, opts)');
|
|
51
|
-
}
|
|
52
|
-
const log = logger || console;
|
|
53
|
-
|
|
54
|
-
return async ({ body, headers, params, query }) => {
|
|
55
|
-
const inboundHeaders = headers || {};
|
|
56
|
-
const project = (params && params.project) || '';
|
|
57
|
-
const location = (params && params.location) || '';
|
|
58
|
-
const modelAction = (params && params.modelAction) || '';
|
|
59
|
-
const { model } = parseModelAction(modelAction);
|
|
60
|
-
const baseUrl = vertexBaseUrl(location);
|
|
61
|
-
const qs = query && Object.keys(query).length ? '?' + new URLSearchParams(query).toString() : '';
|
|
62
|
-
const reqPath = `/v1/projects/${project}/locations/${location}/publishers/google/models/${modelAction}${qs}`;
|
|
63
|
-
|
|
64
|
-
let trace = null;
|
|
65
|
-
try {
|
|
66
|
-
trace = createProxyTrace({
|
|
67
|
-
route: `POST /v1/projects/${project}/locations/${location}/publishers/google/models/${modelAction}`,
|
|
68
|
-
headers: inboundHeaders, body, upstreamMode: 'vertex', originalModel: model, chosenModel: model,
|
|
69
|
-
store: traceStore, logger: traceStore ? log : null, onTraceQueued,
|
|
70
|
-
});
|
|
71
|
-
} catch (_) { /* best-effort trace */ }
|
|
72
|
-
|
|
73
|
-
let upstream;
|
|
74
|
-
try {
|
|
75
|
-
upstream = await vertexProxy(reqPath, body, { baseUrl, inboundHeaders, upstreamMode: 'vertex' });
|
|
76
|
-
} catch (err) {
|
|
77
|
-
const wrapped = asUpstreamError(err, upstreamStatus(err));
|
|
78
|
-
trace?.record({ status: wrapped.statusCode, error: wrapped, upstreamMode: 'vertex', model });
|
|
79
|
-
throw wrapped;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (upstream.stream) {
|
|
83
|
-
const forwardHeaders = {};
|
|
84
|
-
const ct = upstream.headers && upstream.headers['content-type'];
|
|
85
|
-
if (ct) forwardHeaders['Content-Type'] = ct;
|
|
86
|
-
trace?.recordStreamStart({ status: upstream.status, upstreamMode: 'vertex', model, headers: forwardHeaders });
|
|
87
|
-
return {
|
|
88
|
-
status: upstream.status,
|
|
89
|
-
stream: trace ? trace.observeStream(upstream.stream) : upstream.stream,
|
|
90
|
-
headers: forwardHeaders,
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
let raw = '';
|
|
95
|
-
if (upstream.text) {
|
|
96
|
-
try {
|
|
97
|
-
raw = await upstream.text();
|
|
98
|
-
} catch (err) {
|
|
99
|
-
const wrapped = asUpstreamError(err, upstreamStatus(err));
|
|
100
|
-
trace?.record({ status: wrapped.statusCode, error: wrapped, upstreamMode: 'vertex', model });
|
|
101
|
-
throw wrapped;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
const respBody = responseToBody(raw, upstream.status, upstream.headers, log);
|
|
105
|
-
trace?.record({ status: upstream.status, responseBody: respBody, upstreamMode: 'vertex', model, headers: upstream.headers });
|
|
106
|
-
return { status: upstream.status, body: respBody };
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
module.exports = { buildVertexHandler, vertexBaseUrl };
|