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