@caveman-ai/cli 1.3.1 → 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.
@@ -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";
@@ -105,8 +206,49 @@ var ROUTES_BY_API = {
105
206
  "openai-responses": "/w/pi/openai/v1",
106
207
  "google-generative-ai": "/w/pi/v1beta"
107
208
  };
108
- function routeForApi(gateway, api) {
109
- const path = api ? ROUTES_BY_API[api] : void 0;
209
+ var ROUTES_BY_PROVIDER_API = {
210
+ "opencode-go": {
211
+ "anthropic-messages": "/w/pi/compat/opencode-go",
212
+ "openai-completions": "/w/pi/compat/opencode-go/v1",
213
+ "openai-responses": "/w/pi/compat/opencode-go/v1"
214
+ }
215
+ };
216
+ var UPSTREAM_HOSTS_BY_PROVIDER = {
217
+ anthropic: "api.anthropic.com",
218
+ openai: "api.openai.com",
219
+ google: "generativelanguage.googleapis.com",
220
+ "opencode-go": "opencode.ai"
221
+ };
222
+ function upstreamHostFor(provider) {
223
+ return provider ? UPSTREAM_HOSTS_BY_PROVIDER[provider] : void 0;
224
+ }
225
+ function hostOf(url) {
226
+ if (!url) return void 0;
227
+ try {
228
+ return new URL(url).host.toLowerCase();
229
+ } catch {
230
+ return void 0;
231
+ }
232
+ }
233
+ var COMPAT_NAME_RE2 = /^[a-z0-9][a-z0-9._-]{0,63}$/;
234
+ function compatUpstreamFor(provider, compatUpstreams) {
235
+ if (!compatUpstreams || !COMPAT_NAME_RE2.test(provider)) return void 0;
236
+ const url = compatUpstreams[provider];
237
+ return typeof url === "string" ? url : void 0;
238
+ }
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
+ );
248
+ }
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;
251
+ const path = api ? table[api] : void 0;
110
252
  return path ? joinUrl(gateway, path) : void 0;
111
253
  }
112
254
  function joinUrl(base, path) {
@@ -120,6 +262,10 @@ function isLoopbackUrl(url) {
120
262
  return false;
121
263
  }
122
264
  }
265
+ function boundedString(value, max) {
266
+ if (Buffer.byteLength(value, "utf8") <= max) return value;
267
+ return Buffer.from(value, "utf8").subarray(0, max).toString("utf8").replace(/�+$/, "");
268
+ }
123
269
 
124
270
  // ../pi-extension/src/lifecycle.ts
125
271
  var HOOK_TIMEOUT_MS = 2e3;
@@ -229,14 +375,80 @@ function taskContinuation(text) {
229
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);
230
376
  }
231
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
+
232
439
  // ../pi-extension/src/provider.ts
233
440
  var ProviderRouter = class {
234
441
  pi;
235
442
  notify;
236
443
  gateway;
237
444
  gateOpen = false;
238
- overridden = /* @__PURE__ */ new Set();
445
+ routed;
446
+ // Named compat mounts the running proxy published in its run-state file.
447
+ compatUpstreams;
448
+ providerUpstreams;
449
+ compatForwardHeaders;
239
450
  applying = false;
451
+ gateGeneration = 0;
240
452
  warnedModels = /* @__PURE__ */ new Set();
241
453
  constructor(pi, notify) {
242
454
  this.pi = pi;
@@ -244,75 +456,138 @@ var ProviderRouter = class {
244
456
  }
245
457
  // openGate is called once per session after the recovery gate held. Refuses
246
458
  // non-loopback gateways: managed routing needs auth proof v1 does not carry.
247
- async openGate(gateway, ctx) {
459
+ async openGate(gateway, ctx, compatUpstreams, providerUpstreams, compatForwardHeaders) {
460
+ if (!await this.closeGate(ctx)) return;
248
461
  if (!isLoopbackUrl(gateway)) {
249
462
  this.notify("Caveman: direct mode, no compression this session (gateway is not loopback)", "warning");
250
463
  return;
251
464
  }
465
+ this.compatUpstreams = compatUpstreams;
466
+ this.providerUpstreams = providerUpstreams;
467
+ this.compatForwardHeaders = compatForwardHeaders;
252
468
  this.gateway = gateway;
253
469
  this.gateOpen = true;
254
470
  await this.apply(ctx.model, ctx);
255
471
  }
256
- closeGate() {
472
+ async closeGate(ctx) {
257
473
  this.gateOpen = false;
258
- this.clearOverrides();
474
+ this.gateGeneration++;
475
+ return this.restoreCurrentModel(ctx);
259
476
  }
260
477
  routing() {
261
- return this.gateOpen && this.overridden.size > 0;
478
+ return this.routed !== void 0;
262
479
  }
263
- // apply routes one model's provider, or restores direct mode when the model
480
+ // apply routes one model object, or restores direct mode when the model
264
481
  // has no verified route. Called from the gate and from model_select; the
265
482
  // applying flag swallows the model_select echo of our own setModel call.
266
483
  async apply(model, ctx) {
267
484
  if (!this.gateOpen || this.applying || !this.gateway) return;
268
485
  if (!model) return;
269
- const route = routeForApi(this.gateway, model.api);
486
+ const gateGeneration = this.gateGeneration;
487
+ const key = `${model.provider}/${model.id}`;
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;
270
492
  let oauth = true;
271
493
  try {
272
494
  oauth = ctx.modelRegistry.isUsingOAuth(model);
273
495
  } catch {
274
496
  }
275
- if (!route || oauth) {
276
- this.clearOverrides();
277
- const key = `${model.provider}/${model.id}`;
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;
278
530
  if (!this.warnedModels.has(key)) {
279
531
  this.warnedModels.add(key);
280
- const reason = oauth ? "OAuth/subscription credentials are not routed" : `unsupported API "${model.api}"`;
281
- this.notify(`Caveman: pass-through for ${key} (${reason}); no compression`, "warning");
532
+ const mount = compatUpstreamFor(model.provider, this.compatUpstreams);
533
+ const expected = mount !== void 0 ? hostOf(mount) : upstreamHostFor(model.provider);
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`);
535
+ this.notify(boundedString(`Caveman: pass-through for ${key} (${reason}); no compression`, MAX_MESSAGE_BYTES), "warning");
282
536
  }
283
537
  return;
284
538
  }
539
+ if (this.isOwnedRoute(model) && model.baseUrl === route) return;
285
540
  this.applying = true;
286
541
  try {
287
- for (const provider of this.overridden) {
288
- if (provider !== model.provider) {
289
- this.pi.unregisterProvider(provider);
290
- this.overridden.delete(provider);
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");
291
554
  }
292
- }
293
- this.pi.registerProvider(model.provider, { baseUrl: route });
294
- this.overridden.add(model.provider);
295
- const refreshed = ctx.modelRegistry.find(model.provider, model.id);
296
- if (!refreshed || !await this.pi.setModel(refreshed)) {
297
- this.clearOverrides();
298
- this.notify("Caveman: direct mode, no compression this session (model re-resolution failed)", "warning");
299
555
  return;
300
556
  }
301
557
  } catch {
302
- this.clearOverrides();
303
- this.notify("Caveman: direct mode, no compression this session (provider override failed)", "warning");
558
+ if (await this.restoreCurrentModel(ctx)) {
559
+ this.notify("Caveman: direct mode, no compression this session (model selection failed)", "warning");
560
+ }
304
561
  } finally {
305
562
  this.applying = false;
306
563
  }
307
564
  }
308
- clearOverrides() {
309
- for (const provider of this.overridden) {
310
- try {
311
- this.pi.unregisterProvider(provider);
312
- } catch {
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;
580
+ try {
581
+ if (await this.pi.setModel(direct)) {
582
+ this.routed = void 0;
583
+ return true;
313
584
  }
585
+ } catch {
586
+ } finally {
587
+ this.applying = wasApplying;
314
588
  }
315
- this.overridden.clear();
589
+ this.notify("Caveman: could not restore the model's direct endpoint; select the model again before continuing", "warning");
590
+ return false;
316
591
  }
317
592
  };
318
593
 
@@ -447,7 +722,7 @@ var RecoveryClient = class {
447
722
  await this.call("initialize", {
448
723
  protocolVersion: "2024-11-05",
449
724
  capabilities: {},
450
- clientInfo: { name: "caveman-pi-extension", version: "0.1.0" }
725
+ clientInfo: { name: "caveman-pi-extension", version: "0.1.1" }
451
726
  }, void 0, INIT_TIMEOUT_MS);
452
727
  this.notify("notifications/initialized");
453
728
  if (this.disposed) {
@@ -596,10 +871,11 @@ function gatewayUrl() {
596
871
  }
597
872
  return "http://127.0.0.1:8787";
598
873
  }
599
- function runStateRecoveryViaMcp(gateway) {
874
+ function readRunState(gateway) {
600
875
  let port;
601
876
  try {
602
877
  const url = new URL(gateway);
878
+ if (!isLoopbackUrl(gateway) || url.pathname !== "/" || url.search || url.hash || url.username || url.password) return void 0;
603
879
  port = url.port || (url.protocol === "https:" ? "443" : "80");
604
880
  } catch {
605
881
  return void 0;
@@ -607,37 +883,38 @@ function runStateRecoveryViaMcp(gateway) {
607
883
  try {
608
884
  const state = JSON.parse(readFileSync2(join4(cavemanHome(), "run", `${port}.json`), "utf8"));
609
885
  if (state?.schema !== "caveman.proxy.run.v1") return void 0;
610
- if (typeof state.pid !== "number" || typeof state.instance_token !== "string") return void 0;
886
+ if (!Number.isSafeInteger(state.pid) || state.pid < 1 || state.port !== Number(port) || typeof state.instance_token !== "string" || !state.instance_token) return void 0;
611
887
  if (state.owner !== "wrap" && state.owner !== "start") return void 0;
612
888
  try {
613
889
  process.kill(state.pid, 0);
614
890
  } catch {
615
891
  return void 0;
616
892
  }
617
- return Boolean(state.recovery_via_mcp);
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) };
618
894
  } catch {
619
895
  return void 0;
620
896
  }
621
897
  }
622
- async function proxyAliveOnce(gateway) {
898
+ async function proxyAliveOnce(gateway, instanceToken) {
623
899
  const controller = new AbortController();
624
900
  const timer = setTimeout(() => controller.abort(), HEALTH_TIMEOUT_MS);
625
901
  try {
626
- 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" });
627
903
  await response.body?.cancel();
628
- return response.ok;
904
+ return response.ok && response.headers.get("x-caveman-instance") === instanceToken;
629
905
  } catch {
630
906
  return false;
631
907
  } finally {
632
908
  clearTimeout(timer);
633
909
  }
634
910
  }
635
- async function proxyAlive(gateway) {
911
+ async function readLiveRunState(gateway) {
636
912
  for (let attempt = 0; attempt < 3; attempt++) {
637
- if (await proxyAliveOnce(gateway)) return true;
913
+ const state = readRunState(gateway);
914
+ if (state && await proxyAliveOnce(gateway, state.instanceToken)) return state;
638
915
  await new Promise((resolve2) => setTimeout(resolve2, 300));
639
916
  }
640
- return false;
917
+ return void 0;
641
918
  }
642
919
  function index_default(pi) {
643
920
  const bridge = new HookBridge();
@@ -700,6 +977,10 @@ function index_default(pi) {
700
977
  coreContext = void 0;
701
978
  pendingContext = [];
702
979
  pendingBytes = 0;
980
+ if (!await router.closeGate(ctx)) {
981
+ gateDone = true;
982
+ return;
983
+ }
703
984
  try {
704
985
  sessionId = ctx.sessionManager.getSessionId() || "default";
705
986
  } catch {
@@ -713,24 +994,24 @@ function index_default(pi) {
713
994
  return;
714
995
  }
715
996
  const gateway = gatewayUrl();
716
- const alive = await proxyAlive(gateway);
717
- const published = runStateRecoveryViaMcp(gateway);
718
- const recoveryReady = alive ? await recovery.ensure() : false;
997
+ const published = await readLiveRunState(gateway);
998
+ const recoveryReady = published ? await recovery.ensure() : false;
719
999
  gateDone = true;
720
- if (!alive || published === void 0) {
1000
+ if (!published) {
721
1001
  notify(ctx, "Caveman: direct mode, no compression this session (local proxy not running)", "warning");
722
1002
  return;
723
1003
  }
724
- if (!published || !recoveryReady) {
1004
+ if (!published.recoveryViaMcp || !recoveryReady) {
725
1005
  notify(ctx, "Caveman: direct mode, no compression this session (recovery not available \u2014 run `caveman doctor pi`)", "warning");
726
1006
  return;
727
1007
  }
728
- await router.openGate(gateway, ctx);
1008
+ await router.openGate(gateway, ctx, published.compatUpstreams, published.providerUpstreams, published.compatForwardHeaders);
729
1009
  }));
730
1010
  pi.on("model_select", GUARD_model_select(async (event, ctx) => {
731
1011
  if (gateDone) await router?.apply(event.model, ctx);
732
1012
  }));
733
1013
  pi.on("before_agent_start", GUARD_before_agent_start(async (event, ctx) => {
1014
+ if (gateDone) await router?.apply(ctx.model, ctx);
734
1015
  const response = await bridge.call("UserPromptSubmit", {
735
1016
  session_id: sessionId,
736
1017
  model: ctx.model?.id,
@@ -776,9 +1057,9 @@ function index_default(pi) {
776
1057
  const context = additionalContextOf(response);
777
1058
  if (context) coreContext = context;
778
1059
  }));
779
- pi.on("session_shutdown", GUARD_session_shutdown(async () => {
1060
+ pi.on("session_shutdown", GUARD_session_shutdown(async (_event, ctx) => {
780
1061
  await bridge.call("SessionEnd", { session_id: sessionId });
781
- router?.closeGate();
1062
+ await router?.closeGate(ctx);
782
1063
  recovery.dispose();
783
1064
  }));
784
1065
  }