@caveman-ai/cli 1.3.3 → 2.0.0
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/LICENSE +202 -21
- package/NOTICE +66 -0
- package/README.md +30 -1
- package/dist/agent-skills.generated.js +2 -2
- package/dist/agents.generated.js +24 -24
- package/dist/binaries.generated.js +1 -1
- package/dist/caveman-delegate-mcp.mjs +0 -0
- package/dist/caveman-pi-extension.mjs +291 -104
- package/dist/index.js +1638 -383
- package/dist/learn-autopilot.js +447 -0
- package/dist/learn-trends.js +132 -0
- package/dist/learn-tui.js +31 -31
- package/dist/native-hook-fast.js +19 -6
- package/dist/native-pack.generated.js +1 -1
- package/dist/openclaw-provider-compat.js +111 -0
- package/dist/provider-routing.js +147 -0
- package/dist/stats-cli.js +105 -0
- package/package.json +12 -12
|
@@ -1,3 +1,109 @@
|
|
|
1
|
+
// src/provider-routing.ts
|
|
2
|
+
var COMPAT_NAME_RE = /^[a-z0-9][a-z0-9._-]{0,63}$/;
|
|
3
|
+
function endpoint(raw) {
|
|
4
|
+
if (!raw || /[\s?#]/.test(raw)) return void 0;
|
|
5
|
+
try {
|
|
6
|
+
const url = new URL(raw);
|
|
7
|
+
if (!["http:", "https:"].includes(url.protocol) || !url.hostname || url.username || url.password || url.search || url.hash || url.pathname.includes("//")) return void 0;
|
|
8
|
+
if (/%|\\/.test(raw) || /(?:^|\/)\.{1,2}(?:\/|$)/.test(raw)) return void 0;
|
|
9
|
+
return url;
|
|
10
|
+
} catch {
|
|
11
|
+
return void 0;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function publishedUpstreamsOf(raw) {
|
|
15
|
+
const out = /* @__PURE__ */ Object.create(null);
|
|
16
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return out;
|
|
17
|
+
for (const [name, value] of Object.entries(raw)) {
|
|
18
|
+
if (!COMPAT_NAME_RE.test(name)) continue;
|
|
19
|
+
out[name] = typeof value === "string" && endpoint(value) ? value : "";
|
|
20
|
+
}
|
|
21
|
+
return out;
|
|
22
|
+
}
|
|
23
|
+
function trimTrailingSlashes(s) {
|
|
24
|
+
let end = s.length;
|
|
25
|
+
while (end > 0 && s.charCodeAt(end - 1) === 47) end--;
|
|
26
|
+
return s.slice(0, end);
|
|
27
|
+
}
|
|
28
|
+
function append(base, path) {
|
|
29
|
+
return base.origin + base.pathname.replace(/\/+$/, "") + path;
|
|
30
|
+
}
|
|
31
|
+
function compatTarget(base, path) {
|
|
32
|
+
const leftPath = base.pathname.replace(/^\/+|\/+$/g, "");
|
|
33
|
+
const rightPath = path.replace(/^\/+|\/+$/g, "");
|
|
34
|
+
const left = leftPath ? leftPath.split("/") : [];
|
|
35
|
+
const right = rightPath ? rightPath.split("/") : [];
|
|
36
|
+
let overlap = Math.min(left.length, right.length);
|
|
37
|
+
while (overlap > 0 && left.slice(-overlap).join("/") !== right.slice(0, overlap).join("/")) overlap--;
|
|
38
|
+
return base.origin + "/" + [...left, ...right.slice(overlap)].join("/");
|
|
39
|
+
}
|
|
40
|
+
function verifiedProviderRoute(gateway, api, provider, originalBaseUrl, published) {
|
|
41
|
+
const original = endpoint(originalBaseUrl);
|
|
42
|
+
if (!original || !endpoint(gateway)) return void 0;
|
|
43
|
+
const family = api === "anthropic-messages" ? "anthropic" : api === "openai-completions" || api === "openai-responses" ? "openai" : api === "google-generative-ai" ? "gemini" : void 0;
|
|
44
|
+
if (!family) return void 0;
|
|
45
|
+
const operation = api === "anthropic-messages" ? "/v1/messages" : api === "openai-completions" ? "/chat/completions" : api === "openai-responses" ? "/responses" : "/models/caveman-route-proof:streamGenerateContent";
|
|
46
|
+
const expected = append(original, operation);
|
|
47
|
+
const mount = COMPAT_NAME_RE.test(provider) && published?.compat_upstreams && Object.hasOwn(published.compat_upstreams, provider) ? published.compat_upstreams[provider] : void 0;
|
|
48
|
+
if (mount !== void 0) {
|
|
49
|
+
const upstream2 = endpoint(mount);
|
|
50
|
+
if (!upstream2 || family === "gemini") return void 0;
|
|
51
|
+
const suffix = family === "openai" ? "/v1" : "";
|
|
52
|
+
if (compatTarget(upstream2, suffix + operation) !== expected) return void 0;
|
|
53
|
+
return trimTrailingSlashes(gateway) + `/compat/${provider}${suffix}`;
|
|
54
|
+
}
|
|
55
|
+
const upstream = endpoint(published?.provider_upstreams?.[family]);
|
|
56
|
+
if (!upstream) return void 0;
|
|
57
|
+
const versions = family === "gemini" ? ["/v1beta", "/v1"] : [family === "openai" ? "/v1" : ""];
|
|
58
|
+
for (const version of versions) {
|
|
59
|
+
if (append(upstream, version + operation) === expected) {
|
|
60
|
+
return trimTrailingSlashes(gateway) + `/${family}${version}`;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return void 0;
|
|
64
|
+
}
|
|
65
|
+
var COPIED_HEADERS = ["content-type", "content-encoding", "accept", "accept-encoding", "idempotency-key", "openai-organization", "openai-project", "session_id", "x-session-id", "x-client-request-id", "x-session-affinity", "anthropic-version", "anthropic-beta", "api-version", "user-agent"];
|
|
66
|
+
var COMPAT_HEADERS = {
|
|
67
|
+
openrouter: ["http-referer", "x-openrouter-title", "x-openrouter-categories"],
|
|
68
|
+
nvidia: ["x-billing-invoke-origin"],
|
|
69
|
+
opencode: ["x-opencode-session", "x-opencode-client", "x-opencode-project", "x-opencode-request"],
|
|
70
|
+
"opencode-go": ["x-opencode-session", "x-opencode-client", "x-opencode-project", "x-opencode-request"]
|
|
71
|
+
};
|
|
72
|
+
function publishedForwardHeadersOf(raw) {
|
|
73
|
+
const out = /* @__PURE__ */ Object.create(null);
|
|
74
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return out;
|
|
75
|
+
for (const [name, values] of Object.entries(raw)) {
|
|
76
|
+
if (COMPAT_NAME_RE.test(name) && Array.isArray(values)) out[name] = values.filter((v) => typeof v === "string").map((v) => v.toLowerCase());
|
|
77
|
+
}
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
function unforwardedProviderHeaders(api, provider, headers, published) {
|
|
81
|
+
if (headers == null) return [];
|
|
82
|
+
if (typeof headers !== "object" || Array.isArray(headers)) return ["invalid header configuration"];
|
|
83
|
+
const allowed = new Set(COPIED_HEADERS);
|
|
84
|
+
if (api === "openai-completions" || api === "openai-responses") allowed.add("authorization");
|
|
85
|
+
if (api === "anthropic-messages") {
|
|
86
|
+
allowed.add("authorization");
|
|
87
|
+
allowed.add("x-api-key");
|
|
88
|
+
}
|
|
89
|
+
if (api === "google-generative-ai") {
|
|
90
|
+
allowed.add("authorization");
|
|
91
|
+
allowed.add("x-goog-api-key");
|
|
92
|
+
allowed.add("x-goog-user-project");
|
|
93
|
+
}
|
|
94
|
+
if (published?.compat_upstreams && Object.hasOwn(published.compat_upstreams, provider)) {
|
|
95
|
+
for (const name of published.compat_forward_headers?.[provider] ?? []) allowed.add(name.toLowerCase());
|
|
96
|
+
if (Object.hasOwn(COMPAT_HEADERS, provider)) for (const name of COMPAT_HEADERS[provider]) allowed.add(name);
|
|
97
|
+
}
|
|
98
|
+
return Object.entries(headers).filter(([name, value]) => {
|
|
99
|
+
if (value === void 0) return false;
|
|
100
|
+
const lower = name.toLowerCase();
|
|
101
|
+
if (lower === "authorization") return typeof value !== "string" || !/^Bearer +\S+$/i.test(value.trim());
|
|
102
|
+
if (["x-api-key", "x-goog-api-key"].includes(lower) && (typeof value !== "string" || !value.trim())) return true;
|
|
103
|
+
return value !== null && !allowed.has(lower);
|
|
104
|
+
}).map(([name]) => name).sort();
|
|
105
|
+
}
|
|
106
|
+
|
|
1
107
|
// ../pi-extension/src/index.ts
|
|
2
108
|
import { readFileSync as readFileSync2 } from "node:fs";
|
|
3
109
|
import { homedir as homedir3 } from "node:os";
|
|
@@ -129,32 +235,24 @@ function hostOf(url) {
|
|
|
129
235
|
return void 0;
|
|
130
236
|
}
|
|
131
237
|
}
|
|
132
|
-
var
|
|
238
|
+
var COMPAT_NAME_RE2 = /^[a-z0-9][a-z0-9._-]{0,63}$/;
|
|
133
239
|
function compatUpstreamFor(provider, compatUpstreams) {
|
|
134
|
-
if (!compatUpstreams || !
|
|
240
|
+
if (!compatUpstreams || !COMPAT_NAME_RE2.test(provider)) return void 0;
|
|
135
241
|
const url = compatUpstreams[provider];
|
|
136
242
|
return typeof url === "string" ? url : void 0;
|
|
137
243
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
const host = hostOf(mount);
|
|
148
|
-
if (!host) return void 0;
|
|
149
|
-
if (hostOf(originalBaseUrl) !== host) return void 0;
|
|
150
|
-
const suffix = api ? COMPAT_SUFFIX_BY_API[api] : void 0;
|
|
151
|
-
return suffix === void 0 ? void 0 : joinUrl(gateway, `/w/pi/compat/${provider}${suffix}`);
|
|
152
|
-
}
|
|
153
|
-
const expected = UPSTREAM_HOSTS_BY_PROVIDER[provider];
|
|
154
|
-
if (!expected) return void 0;
|
|
155
|
-
if (originalBaseUrl !== void 0 && hostOf(originalBaseUrl) !== expected) return void 0;
|
|
244
|
+
function routeForApi(gateway, api, provider, originalBaseUrl, compatUpstreams, providerUpstreams) {
|
|
245
|
+
if (originalBaseUrl !== void 0) {
|
|
246
|
+
return verifiedProviderRoute(
|
|
247
|
+
joinUrl(gateway, "/w/pi"),
|
|
248
|
+
api,
|
|
249
|
+
provider ?? "",
|
|
250
|
+
originalBaseUrl,
|
|
251
|
+
{ compat_upstreams: compatUpstreams, provider_upstreams: providerUpstreams }
|
|
252
|
+
);
|
|
156
253
|
}
|
|
157
254
|
const table = (provider ? ROUTES_BY_PROVIDER_API[provider] : void 0) ?? ROUTES_BY_API;
|
|
255
|
+
if (provider && !UPSTREAM_HOSTS_BY_PROVIDER[provider]) return void 0;
|
|
158
256
|
const path = api ? table[api] : void 0;
|
|
159
257
|
return path ? joinUrl(gateway, path) : void 0;
|
|
160
258
|
}
|
|
@@ -282,25 +380,80 @@ function taskContinuation(text) {
|
|
|
282
380
|
return /^(?:please\s+)?(?:continue|go ahead|keep going|proceed|do (?:it|that)|fix (?:it|that)|retry|try again|explain (?:it|that)|what do you mean|yes|yep|yeah|why\??|how\??)[.!?\s]*$/.test(prompt);
|
|
283
381
|
}
|
|
284
382
|
|
|
383
|
+
// ../pi-extension/src/provider-compat.ts
|
|
384
|
+
function unpreservedAttributionHeaders(model, resolvedHeaders, sessionId, telemetryEnv = process.env.PI_TELEMETRY) {
|
|
385
|
+
let host;
|
|
386
|
+
try {
|
|
387
|
+
host = new URL(model.baseUrl).hostname;
|
|
388
|
+
} catch {
|
|
389
|
+
}
|
|
390
|
+
const expected = {};
|
|
391
|
+
const telemetryDisabled = telemetryEnv !== void 0 && !["1", "true", "yes"].includes(telemetryEnv.toLowerCase());
|
|
392
|
+
if (!telemetryDisabled) {
|
|
393
|
+
if (model.provider !== "openrouter" && model.baseUrl.includes("openrouter.ai")) {
|
|
394
|
+
Object.assign(expected, { "HTTP-Referer": "https://pi.dev", "X-OpenRouter-Title": "pi", "X-OpenRouter-Categories": "cli-agent" });
|
|
395
|
+
}
|
|
396
|
+
if (model.provider !== "nvidia" && host === "integrate.api.nvidia.com") expected["X-BILLING-INVOKE-ORIGIN"] = "Pi";
|
|
397
|
+
if (!["cloudflare-workers-ai", "cloudflare-ai-gateway"].includes(model.provider) && ["api.cloudflare.com", "gateway.ai.cloudflare.com"].includes(host ?? "")) expected["User-Agent"] = "pi-coding-agent";
|
|
398
|
+
}
|
|
399
|
+
if (!["opencode", "opencode-go"].includes(model.provider) && host === "opencode.ai") {
|
|
400
|
+
expected["x-opencode-session"] = sessionId;
|
|
401
|
+
expected["x-opencode-client"] = "pi";
|
|
402
|
+
}
|
|
403
|
+
return Object.entries(expected).filter(([name, value]) => {
|
|
404
|
+
if (resolvedHeaders && Object.hasOwn(resolvedHeaders, name)) return false;
|
|
405
|
+
return value === void 0 || model.headers?.[name] !== value;
|
|
406
|
+
}).map(([name]) => name).sort();
|
|
407
|
+
}
|
|
408
|
+
function compatForRoutedModel(model) {
|
|
409
|
+
const provider = model.provider;
|
|
410
|
+
const url = model.baseUrl;
|
|
411
|
+
const openRouter = provider === "openrouter" || url.includes("openrouter.ai");
|
|
412
|
+
if (model.api === "openai-responses") {
|
|
413
|
+
const explicit2 = model.compat;
|
|
414
|
+
return { ...explicit2, sessionAffinityFormat: explicit2?.sessionAffinityFormat ?? (openRouter ? "openrouter" : "openai") };
|
|
415
|
+
}
|
|
416
|
+
if (model.api !== "openai-completions") return model.compat;
|
|
417
|
+
const explicit = model.compat;
|
|
418
|
+
const zai = provider === "zai" || provider === "zai-coding-cn" || url.includes("api.z.ai") || url.includes("open.bigmodel.cn");
|
|
419
|
+
const together = provider === "together" || url.includes("api.together.ai") || url.includes("api.together.xyz");
|
|
420
|
+
const moonshot = provider === "moonshotai" || provider === "moonshotai-cn" || url.includes("api.moonshot.");
|
|
421
|
+
const cloudflareWorkers = provider === "cloudflare-workers-ai" || url.includes("api.cloudflare.com");
|
|
422
|
+
const cloudflareGateway = provider === "cloudflare-ai-gateway" || url.includes("gateway.ai.cloudflare.com");
|
|
423
|
+
const nvidia = provider === "nvidia" || url.includes("integrate.api.nvidia.com");
|
|
424
|
+
const antLing = provider === "ant-ling" || url.includes("api.ant-ling.com");
|
|
425
|
+
const grok = provider === "xai" || url.includes("api.x.ai");
|
|
426
|
+
const deepseek = provider === "deepseek" || url.toLowerCase().includes("deepseek.com");
|
|
427
|
+
const nonStandard = nvidia || provider === "cerebras" || url.includes("cerebras.ai") || grok || together || url.includes("chutes.ai") || deepseek || zai || moonshot || provider === "opencode" || url.includes("opencode.ai") || cloudflareWorkers || cloudflareGateway || antLing;
|
|
428
|
+
const maxTokens = url.includes("chutes.ai") || deepseek || moonshot || cloudflareGateway || together || nvidia || antLing || zai;
|
|
429
|
+
const openRouterDeveloper = openRouter && (model.id.startsWith("anthropic/") || model.id.startsWith("openai/"));
|
|
430
|
+
return {
|
|
431
|
+
...explicit,
|
|
432
|
+
supportsStore: explicit?.supportsStore ?? !nonStandard,
|
|
433
|
+
supportsDeveloperRole: explicit?.supportsDeveloperRole ?? (openRouterDeveloper || !nonStandard && !openRouter),
|
|
434
|
+
supportsReasoningEffort: explicit?.supportsReasoningEffort ?? !(grok || zai || moonshot || together || cloudflareGateway || nvidia || antLing),
|
|
435
|
+
maxTokensField: explicit?.maxTokensField ?? (maxTokens ? "max_tokens" : "max_completion_tokens"),
|
|
436
|
+
requiresReasoningContentOnAssistantMessages: explicit?.requiresReasoningContentOnAssistantMessages ?? deepseek,
|
|
437
|
+
thinkingFormat: explicit?.thinkingFormat ?? (deepseek ? "deepseek" : zai ? "zai" : together ? "together" : antLing ? "ant-ling" : openRouter ? "openrouter" : "openai"),
|
|
438
|
+
supportsStrictMode: explicit?.supportsStrictMode ?? !(moonshot || together || cloudflareGateway || nvidia),
|
|
439
|
+
sessionAffinityFormat: explicit?.sessionAffinityFormat ?? (openRouter ? "openrouter" : "openai"),
|
|
440
|
+
supportsLongCacheRetention: explicit?.supportsLongCacheRetention ?? !(together || cloudflareWorkers || cloudflareGateway || nvidia || antLing)
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
|
|
285
444
|
// ../pi-extension/src/provider.ts
|
|
286
445
|
var ProviderRouter = class {
|
|
287
446
|
pi;
|
|
288
447
|
notify;
|
|
289
448
|
gateway;
|
|
290
449
|
gateOpen = false;
|
|
291
|
-
|
|
292
|
-
// originals keeps the base URL of each model, keyed "<provider>/<id>". After
|
|
293
|
-
// an override, the registry reports the gateway route for EVERY model of that
|
|
294
|
-
// provider, so the originals are not available from the registry. Per model,
|
|
295
|
-
// not per provider: Pi lets each model carry its own baseUrl, and a
|
|
296
|
-
// same-provider model pointing at another endpoint must not inherit the
|
|
297
|
-
// default model's URL (#973). Pi keeps extension overrides in memory only, so
|
|
298
|
-
// a new pi process always starts from the original base URLs and this map
|
|
299
|
-
// never sees a stale override from an earlier process.
|
|
300
|
-
originals = /* @__PURE__ */ new Map();
|
|
450
|
+
routed;
|
|
301
451
|
// Named compat mounts the running proxy published in its run-state file.
|
|
302
452
|
compatUpstreams;
|
|
453
|
+
providerUpstreams;
|
|
454
|
+
compatForwardHeaders;
|
|
303
455
|
applying = false;
|
|
456
|
+
gateGeneration = 0;
|
|
304
457
|
warnedModels = /* @__PURE__ */ new Set();
|
|
305
458
|
constructor(pi, notify) {
|
|
306
459
|
this.pi = pi;
|
|
@@ -308,95 +461,138 @@ var ProviderRouter = class {
|
|
|
308
461
|
}
|
|
309
462
|
// openGate is called once per session after the recovery gate held. Refuses
|
|
310
463
|
// non-loopback gateways: managed routing needs auth proof v1 does not carry.
|
|
311
|
-
async openGate(gateway, ctx, compatUpstreams) {
|
|
464
|
+
async openGate(gateway, ctx, compatUpstreams, providerUpstreams, compatForwardHeaders) {
|
|
465
|
+
if (!await this.closeGate(ctx)) return;
|
|
312
466
|
if (!isLoopbackUrl(gateway)) {
|
|
313
467
|
this.notify("Caveman: direct mode, no compression this session (gateway is not loopback)", "warning");
|
|
314
468
|
return;
|
|
315
469
|
}
|
|
316
470
|
this.compatUpstreams = compatUpstreams;
|
|
471
|
+
this.providerUpstreams = providerUpstreams;
|
|
472
|
+
this.compatForwardHeaders = compatForwardHeaders;
|
|
317
473
|
this.gateway = gateway;
|
|
318
474
|
this.gateOpen = true;
|
|
319
475
|
await this.apply(ctx.model, ctx);
|
|
320
476
|
}
|
|
321
|
-
closeGate() {
|
|
477
|
+
async closeGate(ctx) {
|
|
322
478
|
this.gateOpen = false;
|
|
323
|
-
this.
|
|
479
|
+
this.gateGeneration++;
|
|
480
|
+
return this.restoreCurrentModel(ctx);
|
|
324
481
|
}
|
|
325
482
|
routing() {
|
|
326
|
-
return this.
|
|
483
|
+
return this.routed !== void 0;
|
|
327
484
|
}
|
|
328
|
-
// apply routes one model
|
|
485
|
+
// apply routes one model object, or restores direct mode when the model
|
|
329
486
|
// has no verified route. Called from the gate and from model_select; the
|
|
330
487
|
// applying flag swallows the model_select echo of our own setModel call.
|
|
331
488
|
async apply(model, ctx) {
|
|
332
489
|
if (!this.gateOpen || this.applying || !this.gateway) return;
|
|
333
490
|
if (!model) return;
|
|
491
|
+
const gateGeneration = this.gateGeneration;
|
|
334
492
|
const key = `${model.provider}/${model.id}`;
|
|
335
|
-
|
|
336
|
-
const
|
|
337
|
-
const
|
|
493
|
+
const original = this.isOwnedRoute(model) ? this.routed.originalBaseUrl : model.baseUrl;
|
|
494
|
+
const route = routeForApi(this.gateway, model.api, model.provider, original, this.compatUpstreams, this.providerUpstreams);
|
|
495
|
+
const compatibilityIssue = model.api === "openai-completions" && original.includes("api.openai.com") ? "Pi cannot preserve this OpenAI Chat endpoint's prompt cache keys through routing" : void 0;
|
|
496
|
+
let headerIssue;
|
|
338
497
|
let oauth = true;
|
|
339
498
|
try {
|
|
340
499
|
oauth = ctx.modelRegistry.isUsingOAuth(model);
|
|
341
500
|
} catch {
|
|
342
501
|
}
|
|
343
|
-
if (!route
|
|
344
|
-
|
|
502
|
+
if (!oauth && route && !compatibilityIssue) {
|
|
503
|
+
try {
|
|
504
|
+
const auth = await ctx.modelRegistry.getApiKeyAndHeaders(model);
|
|
505
|
+
if (!auth.ok) throw new Error("request auth unavailable");
|
|
506
|
+
const { headers } = auth;
|
|
507
|
+
const published = {
|
|
508
|
+
...this.compatUpstreams ? { compat_upstreams: this.compatUpstreams } : {},
|
|
509
|
+
...this.compatForwardHeaders ? { compat_forward_headers: this.compatForwardHeaders } : {}
|
|
510
|
+
};
|
|
511
|
+
const missingHeaders = [.../* @__PURE__ */ new Set([
|
|
512
|
+
...unforwardedProviderHeaders(model.api, model.provider, model.headers, published),
|
|
513
|
+
...unforwardedProviderHeaders(model.api, model.provider, headers, published)
|
|
514
|
+
])].sort();
|
|
515
|
+
if (missingHeaders.length) {
|
|
516
|
+
const remedy = missingHeaders.some((name) => ["authorization", "x-api-key", "x-goog-api-key"].includes(name.toLowerCase())) ? "the proxy cannot preserve this authentication override; keep the provider direct" : `configure compat.${model.provider}.forward_headers`;
|
|
517
|
+
headerIssue = `provider headers ${missingHeaders.join(", ")} are not forwarded; ${remedy}`;
|
|
518
|
+
}
|
|
519
|
+
if (!headerIssue) {
|
|
520
|
+
let sessionId;
|
|
521
|
+
try {
|
|
522
|
+
sessionId = ctx.sessionManager.getSessionId();
|
|
523
|
+
} catch {
|
|
524
|
+
}
|
|
525
|
+
const attribution = unpreservedAttributionHeaders({ ...model, baseUrl: original }, headers, sessionId);
|
|
526
|
+
if (attribution.length) headerIssue = `Pi cannot preserve URL-derived headers ${attribution.join(", ")} for this provider alias; use its canonical provider ID or explicit headers`;
|
|
527
|
+
}
|
|
528
|
+
} catch {
|
|
529
|
+
headerIssue = "provider request headers could not be resolved";
|
|
530
|
+
}
|
|
531
|
+
if (!this.gateOpen || this.gateGeneration !== gateGeneration || ctx.model !== model) return;
|
|
532
|
+
}
|
|
533
|
+
if (!route || oauth || compatibilityIssue || headerIssue) {
|
|
534
|
+
if (!await this.restoreCurrentModel(ctx)) return;
|
|
345
535
|
if (!this.warnedModels.has(key)) {
|
|
346
536
|
this.warnedModels.add(key);
|
|
347
537
|
const mount = compatUpstreamFor(model.provider, this.compatUpstreams);
|
|
348
538
|
const expected = mount !== void 0 ? hostOf(mount) : upstreamHostFor(model.provider);
|
|
349
|
-
const reason = oauth ? "OAuth/subscription credentials are not routed" : expected === void 0 ? `no compat mount named "${model.provider}" in the local proxy; add compat.${model.provider}.base_url to caveman.yaml to route it` : hostOf(original) !== expected ? `provider endpoint ${hostOf(original) ?? original} is not ${expected}` : `
|
|
539
|
+
const reason = oauth ? "OAuth/subscription credentials are not routed" : compatibilityIssue ?? headerIssue ?? (expected === void 0 ? `no compat mount named "${model.provider}" in the local proxy; add compat.${model.provider}.base_url to caveman.yaml to route it` : hostOf(original) !== expected ? `provider endpoint ${hostOf(original) ?? original} is not ${expected}` : `provider endpoint path or API "${model.provider}/${model.api}" is not verified by the running proxy`);
|
|
350
540
|
this.notify(boundedString(`Caveman: pass-through for ${key} (${reason}); no compression`, MAX_MESSAGE_BYTES), "warning");
|
|
351
541
|
}
|
|
352
542
|
return;
|
|
353
543
|
}
|
|
544
|
+
if (this.isOwnedRoute(model) && model.baseUrl === route) return;
|
|
354
545
|
this.applying = true;
|
|
355
546
|
try {
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
547
|
+
this.routed = {
|
|
548
|
+
provider: model.provider,
|
|
549
|
+
id: model.id,
|
|
550
|
+
originalBaseUrl: original,
|
|
551
|
+
route,
|
|
552
|
+
originalCompat: model.compat,
|
|
553
|
+
hadCompat: Object.hasOwn(model, "compat")
|
|
554
|
+
};
|
|
555
|
+
const compat = compatForRoutedModel(model);
|
|
556
|
+
if (!await this.pi.setModel({ ...model, baseUrl: route, ...compat === void 0 ? {} : { compat } })) {
|
|
557
|
+
if (await this.restoreCurrentModel(ctx)) {
|
|
558
|
+
this.notify("Caveman: direct mode, no compression this session (model selection failed)", "warning");
|
|
360
559
|
}
|
|
361
|
-
}
|
|
362
|
-
this.originals.set(key, original);
|
|
363
|
-
this.pi.registerProvider(model.provider, { baseUrl: route });
|
|
364
|
-
this.overridden.add(model.provider);
|
|
365
|
-
const refreshed = ctx.modelRegistry.find(model.provider, model.id);
|
|
366
|
-
if (!refreshed || !await this.pi.setModel(refreshed)) {
|
|
367
|
-
this.clearOverrides();
|
|
368
|
-
this.notify("Caveman: direct mode, no compression this session (model re-resolution failed)", "warning");
|
|
369
560
|
return;
|
|
370
561
|
}
|
|
371
562
|
} catch {
|
|
372
|
-
this.
|
|
373
|
-
|
|
563
|
+
if (await this.restoreCurrentModel(ctx)) {
|
|
564
|
+
this.notify("Caveman: direct mode, no compression this session (model selection failed)", "warning");
|
|
565
|
+
}
|
|
374
566
|
} finally {
|
|
375
567
|
this.applying = false;
|
|
376
568
|
}
|
|
377
569
|
}
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
570
|
+
isOwnedRoute(model) {
|
|
571
|
+
return !!model && !!this.routed && model.provider === this.routed.provider && model.id === this.routed.id && model.baseUrl === this.routed.route;
|
|
572
|
+
}
|
|
573
|
+
async restoreCurrentModel(ctx) {
|
|
574
|
+
const current = ctx.model;
|
|
575
|
+
if (!this.isOwnedRoute(current)) {
|
|
576
|
+
this.routed = void 0;
|
|
577
|
+
return true;
|
|
578
|
+
}
|
|
579
|
+
const { originalBaseUrl, originalCompat, hadCompat } = this.routed;
|
|
580
|
+
const direct = { ...current, baseUrl: originalBaseUrl };
|
|
581
|
+
if (hadCompat) direct.compat = originalCompat;
|
|
582
|
+
else delete direct.compat;
|
|
583
|
+
const wasApplying = this.applying;
|
|
584
|
+
this.applying = true;
|
|
381
585
|
try {
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
}
|
|
586
|
+
if (await this.pi.setModel(direct)) {
|
|
587
|
+
this.routed = void 0;
|
|
588
|
+
return true;
|
|
386
589
|
}
|
|
387
590
|
} catch {
|
|
591
|
+
} finally {
|
|
592
|
+
this.applying = wasApplying;
|
|
388
593
|
}
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
clearOverrides() {
|
|
392
|
-
for (const provider of this.overridden) {
|
|
393
|
-
try {
|
|
394
|
-
this.pi.unregisterProvider(provider);
|
|
395
|
-
} catch {
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
this.overridden.clear();
|
|
399
|
-
this.originals.clear();
|
|
594
|
+
this.notify("Caveman: could not restore the model's direct endpoint; select the model again before continuing", "warning");
|
|
595
|
+
return false;
|
|
400
596
|
}
|
|
401
597
|
};
|
|
402
598
|
|
|
@@ -531,7 +727,7 @@ var RecoveryClient = class {
|
|
|
531
727
|
await this.call("initialize", {
|
|
532
728
|
protocolVersion: "2024-11-05",
|
|
533
729
|
capabilities: {},
|
|
534
|
-
clientInfo: { name: "caveman-pi-extension", version: "0.
|
|
730
|
+
clientInfo: { name: "caveman-pi-extension", version: "0.2.0" }
|
|
535
731
|
}, void 0, INIT_TIMEOUT_MS);
|
|
536
732
|
this.notify("notifications/initialized");
|
|
537
733
|
if (this.disposed) {
|
|
@@ -680,25 +876,11 @@ function gatewayUrl() {
|
|
|
680
876
|
}
|
|
681
877
|
return "http://127.0.0.1:8787";
|
|
682
878
|
}
|
|
683
|
-
function compatUpstreamsOf(raw) {
|
|
684
|
-
const out = /* @__PURE__ */ Object.create(null);
|
|
685
|
-
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return out;
|
|
686
|
-
for (const [name, url] of Object.entries(raw)) {
|
|
687
|
-
if (!COMPAT_NAME_RE.test(name) || typeof url !== "string") continue;
|
|
688
|
-
try {
|
|
689
|
-
const protocol = new URL(url).protocol;
|
|
690
|
-
if (protocol !== "http:" && protocol !== "https:") continue;
|
|
691
|
-
} catch {
|
|
692
|
-
continue;
|
|
693
|
-
}
|
|
694
|
-
out[name] = url;
|
|
695
|
-
}
|
|
696
|
-
return out;
|
|
697
|
-
}
|
|
698
879
|
function readRunState(gateway) {
|
|
699
880
|
let port;
|
|
700
881
|
try {
|
|
701
882
|
const url = new URL(gateway);
|
|
883
|
+
if (!isLoopbackUrl(gateway) || url.pathname !== "/" || url.search || url.hash || url.username || url.password) return void 0;
|
|
702
884
|
port = url.port || (url.protocol === "https:" ? "443" : "80");
|
|
703
885
|
} catch {
|
|
704
886
|
return void 0;
|
|
@@ -706,37 +888,38 @@ function readRunState(gateway) {
|
|
|
706
888
|
try {
|
|
707
889
|
const state = JSON.parse(readFileSync2(join4(cavemanHome(), "run", `${port}.json`), "utf8"));
|
|
708
890
|
if (state?.schema !== "caveman.proxy.run.v1") return void 0;
|
|
709
|
-
if (
|
|
891
|
+
if (!Number.isSafeInteger(state.pid) || state.pid < 1 || state.port !== Number(port) || typeof state.instance_token !== "string" || !state.instance_token) return void 0;
|
|
710
892
|
if (state.owner !== "wrap" && state.owner !== "start") return void 0;
|
|
711
893
|
try {
|
|
712
894
|
process.kill(state.pid, 0);
|
|
713
895
|
} catch {
|
|
714
896
|
return void 0;
|
|
715
897
|
}
|
|
716
|
-
return { recoveryViaMcp:
|
|
898
|
+
return { instanceToken: state.instance_token, recoveryViaMcp: state.recovery_via_mcp === true, compatUpstreams: publishedUpstreamsOf(state.compat_upstreams), providerUpstreams: publishedUpstreamsOf(state.provider_upstreams), compatForwardHeaders: publishedForwardHeadersOf(state.compat_forward_headers) };
|
|
717
899
|
} catch {
|
|
718
900
|
return void 0;
|
|
719
901
|
}
|
|
720
902
|
}
|
|
721
|
-
async function proxyAliveOnce(gateway) {
|
|
903
|
+
async function proxyAliveOnce(gateway, instanceToken) {
|
|
722
904
|
const controller = new AbortController();
|
|
723
905
|
const timer = setTimeout(() => controller.abort(), HEALTH_TIMEOUT_MS);
|
|
724
906
|
try {
|
|
725
|
-
const response = await fetch(`${gateway.replace(/\/+$/, "")}/health/live`, { signal: controller.signal });
|
|
907
|
+
const response = await fetch(`${gateway.replace(/\/+$/, "")}/health/live`, { signal: controller.signal, redirect: "error" });
|
|
726
908
|
await response.body?.cancel();
|
|
727
|
-
return response.ok;
|
|
909
|
+
return response.ok && response.headers.get("x-caveman-instance") === instanceToken;
|
|
728
910
|
} catch {
|
|
729
911
|
return false;
|
|
730
912
|
} finally {
|
|
731
913
|
clearTimeout(timer);
|
|
732
914
|
}
|
|
733
915
|
}
|
|
734
|
-
async function
|
|
916
|
+
async function readLiveRunState(gateway) {
|
|
735
917
|
for (let attempt = 0; attempt < 3; attempt++) {
|
|
736
|
-
|
|
918
|
+
const state = readRunState(gateway);
|
|
919
|
+
if (state && await proxyAliveOnce(gateway, state.instanceToken)) return state;
|
|
737
920
|
await new Promise((resolve2) => setTimeout(resolve2, 300));
|
|
738
921
|
}
|
|
739
|
-
return
|
|
922
|
+
return void 0;
|
|
740
923
|
}
|
|
741
924
|
function index_default(pi) {
|
|
742
925
|
const bridge = new HookBridge();
|
|
@@ -799,6 +982,10 @@ function index_default(pi) {
|
|
|
799
982
|
coreContext = void 0;
|
|
800
983
|
pendingContext = [];
|
|
801
984
|
pendingBytes = 0;
|
|
985
|
+
if (!await router.closeGate(ctx)) {
|
|
986
|
+
gateDone = true;
|
|
987
|
+
return;
|
|
988
|
+
}
|
|
802
989
|
try {
|
|
803
990
|
sessionId = ctx.sessionManager.getSessionId() || "default";
|
|
804
991
|
} catch {
|
|
@@ -812,11 +999,10 @@ function index_default(pi) {
|
|
|
812
999
|
return;
|
|
813
1000
|
}
|
|
814
1001
|
const gateway = gatewayUrl();
|
|
815
|
-
const
|
|
816
|
-
const
|
|
817
|
-
const recoveryReady = alive ? await recovery.ensure() : false;
|
|
1002
|
+
const published = await readLiveRunState(gateway);
|
|
1003
|
+
const recoveryReady = published ? await recovery.ensure() : false;
|
|
818
1004
|
gateDone = true;
|
|
819
|
-
if (!
|
|
1005
|
+
if (!published) {
|
|
820
1006
|
notify(ctx, "Caveman: direct mode, no compression this session (local proxy not running)", "warning");
|
|
821
1007
|
return;
|
|
822
1008
|
}
|
|
@@ -824,12 +1010,13 @@ function index_default(pi) {
|
|
|
824
1010
|
notify(ctx, "Caveman: direct mode, no compression this session (recovery not available \u2014 run `caveman doctor pi`)", "warning");
|
|
825
1011
|
return;
|
|
826
1012
|
}
|
|
827
|
-
await router.openGate(gateway, ctx, published.compatUpstreams);
|
|
1013
|
+
await router.openGate(gateway, ctx, published.compatUpstreams, published.providerUpstreams, published.compatForwardHeaders);
|
|
828
1014
|
}));
|
|
829
1015
|
pi.on("model_select", GUARD_model_select(async (event, ctx) => {
|
|
830
1016
|
if (gateDone) await router?.apply(event.model, ctx);
|
|
831
1017
|
}));
|
|
832
1018
|
pi.on("before_agent_start", GUARD_before_agent_start(async (event, ctx) => {
|
|
1019
|
+
if (gateDone) await router?.apply(ctx.model, ctx);
|
|
833
1020
|
const response = await bridge.call("UserPromptSubmit", {
|
|
834
1021
|
session_id: sessionId,
|
|
835
1022
|
model: ctx.model?.id,
|
|
@@ -875,9 +1062,9 @@ function index_default(pi) {
|
|
|
875
1062
|
const context = additionalContextOf(response);
|
|
876
1063
|
if (context) coreContext = context;
|
|
877
1064
|
}));
|
|
878
|
-
pi.on("session_shutdown", GUARD_session_shutdown(async () => {
|
|
1065
|
+
pi.on("session_shutdown", GUARD_session_shutdown(async (_event, ctx) => {
|
|
879
1066
|
await bridge.call("SessionEnd", { session_id: sessionId });
|
|
880
|
-
router?.closeGate();
|
|
1067
|
+
await router?.closeGate(ctx);
|
|
881
1068
|
recovery.dispose();
|
|
882
1069
|
}));
|
|
883
1070
|
}
|