@caveman-ai/cli 1.3.1 → 1.3.3

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.
@@ -105,8 +105,57 @@ var ROUTES_BY_API = {
105
105
  "openai-responses": "/w/pi/openai/v1",
106
106
  "google-generative-ai": "/w/pi/v1beta"
107
107
  };
108
- function routeForApi(gateway, api) {
109
- const path = api ? ROUTES_BY_API[api] : void 0;
108
+ var ROUTES_BY_PROVIDER_API = {
109
+ "opencode-go": {
110
+ "anthropic-messages": "/w/pi/compat/opencode-go",
111
+ "openai-completions": "/w/pi/compat/opencode-go/v1",
112
+ "openai-responses": "/w/pi/compat/opencode-go/v1"
113
+ }
114
+ };
115
+ var UPSTREAM_HOSTS_BY_PROVIDER = {
116
+ anthropic: "api.anthropic.com",
117
+ openai: "api.openai.com",
118
+ google: "generativelanguage.googleapis.com",
119
+ "opencode-go": "opencode.ai"
120
+ };
121
+ function upstreamHostFor(provider) {
122
+ return provider ? UPSTREAM_HOSTS_BY_PROVIDER[provider] : void 0;
123
+ }
124
+ function hostOf(url) {
125
+ if (!url) return void 0;
126
+ try {
127
+ return new URL(url).host.toLowerCase();
128
+ } catch {
129
+ return void 0;
130
+ }
131
+ }
132
+ var COMPAT_NAME_RE = /^[a-z0-9][a-z0-9._-]{0,63}$/;
133
+ function compatUpstreamFor(provider, compatUpstreams) {
134
+ if (!compatUpstreams || !COMPAT_NAME_RE.test(provider)) return void 0;
135
+ const url = compatUpstreams[provider];
136
+ return typeof url === "string" ? url : void 0;
137
+ }
138
+ var COMPAT_SUFFIX_BY_API = {
139
+ "anthropic-messages": "",
140
+ "openai-completions": "/v1",
141
+ "openai-responses": "/v1"
142
+ };
143
+ function routeForApi(gateway, api, provider, originalBaseUrl, compatUpstreams) {
144
+ if (provider) {
145
+ const mount = compatUpstreamFor(provider, compatUpstreams);
146
+ if (mount !== void 0) {
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;
156
+ }
157
+ const table = (provider ? ROUTES_BY_PROVIDER_API[provider] : void 0) ?? ROUTES_BY_API;
158
+ const path = api ? table[api] : void 0;
110
159
  return path ? joinUrl(gateway, path) : void 0;
111
160
  }
112
161
  function joinUrl(base, path) {
@@ -120,6 +169,10 @@ function isLoopbackUrl(url) {
120
169
  return false;
121
170
  }
122
171
  }
172
+ function boundedString(value, max) {
173
+ if (Buffer.byteLength(value, "utf8") <= max) return value;
174
+ return Buffer.from(value, "utf8").subarray(0, max).toString("utf8").replace(/�+$/, "");
175
+ }
123
176
 
124
177
  // ../pi-extension/src/lifecycle.ts
125
178
  var HOOK_TIMEOUT_MS = 2e3;
@@ -236,6 +289,17 @@ var ProviderRouter = class {
236
289
  gateway;
237
290
  gateOpen = false;
238
291
  overridden = /* @__PURE__ */ new Set();
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();
301
+ // Named compat mounts the running proxy published in its run-state file.
302
+ compatUpstreams;
239
303
  applying = false;
240
304
  warnedModels = /* @__PURE__ */ new Set();
241
305
  constructor(pi, notify) {
@@ -244,11 +308,12 @@ var ProviderRouter = class {
244
308
  }
245
309
  // openGate is called once per session after the recovery gate held. Refuses
246
310
  // non-loopback gateways: managed routing needs auth proof v1 does not carry.
247
- async openGate(gateway, ctx) {
311
+ async openGate(gateway, ctx, compatUpstreams) {
248
312
  if (!isLoopbackUrl(gateway)) {
249
313
  this.notify("Caveman: direct mode, no compression this session (gateway is not loopback)", "warning");
250
314
  return;
251
315
  }
316
+ this.compatUpstreams = compatUpstreams;
252
317
  this.gateway = gateway;
253
318
  this.gateOpen = true;
254
319
  await this.apply(ctx.model, ctx);
@@ -266,7 +331,10 @@ var ProviderRouter = class {
266
331
  async apply(model, ctx) {
267
332
  if (!this.gateOpen || this.applying || !this.gateway) return;
268
333
  if (!model) return;
269
- const route = routeForApi(this.gateway, model.api);
334
+ const key = `${model.provider}/${model.id}`;
335
+ if (!this.overridden.has(model.provider)) this.snapshot(model, ctx);
336
+ const original = this.originals.get(key) ?? model.baseUrl;
337
+ const route = routeForApi(this.gateway, model.api, model.provider, original, this.compatUpstreams);
270
338
  let oauth = true;
271
339
  try {
272
340
  oauth = ctx.modelRegistry.isUsingOAuth(model);
@@ -274,11 +342,12 @@ var ProviderRouter = class {
274
342
  }
275
343
  if (!route || oauth) {
276
344
  this.clearOverrides();
277
- const key = `${model.provider}/${model.id}`;
278
345
  if (!this.warnedModels.has(key)) {
279
346
  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");
347
+ const mount = compatUpstreamFor(model.provider, this.compatUpstreams);
348
+ 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}` : `unsupported provider/API "${model.provider}/${model.api}"`;
350
+ this.notify(boundedString(`Caveman: pass-through for ${key} (${reason}); no compression`, MAX_MESSAGE_BYTES), "warning");
282
351
  }
283
352
  return;
284
353
  }
@@ -290,6 +359,7 @@ var ProviderRouter = class {
290
359
  this.overridden.delete(provider);
291
360
  }
292
361
  }
362
+ this.originals.set(key, original);
293
363
  this.pi.registerProvider(model.provider, { baseUrl: route });
294
364
  this.overridden.add(model.provider);
295
365
  const refreshed = ctx.modelRegistry.find(model.provider, model.id);
@@ -305,6 +375,19 @@ var ProviderRouter = class {
305
375
  this.applying = false;
306
376
  }
307
377
  }
378
+ // snapshot records the real base URL of every model of this provider, so a
379
+ // later selection inside the same provider is gated against its OWN endpoint.
380
+ snapshot(model, ctx) {
381
+ try {
382
+ for (const candidate of ctx.modelRegistry.getAll()) {
383
+ if (candidate.provider === model.provider && typeof candidate.baseUrl === "string") {
384
+ this.originals.set(`${candidate.provider}/${candidate.id}`, candidate.baseUrl);
385
+ }
386
+ }
387
+ } catch {
388
+ }
389
+ if (typeof model.baseUrl === "string") this.originals.set(`${model.provider}/${model.id}`, model.baseUrl);
390
+ }
308
391
  clearOverrides() {
309
392
  for (const provider of this.overridden) {
310
393
  try {
@@ -313,6 +396,7 @@ var ProviderRouter = class {
313
396
  }
314
397
  }
315
398
  this.overridden.clear();
399
+ this.originals.clear();
316
400
  }
317
401
  };
318
402
 
@@ -596,7 +680,22 @@ function gatewayUrl() {
596
680
  }
597
681
  return "http://127.0.0.1:8787";
598
682
  }
599
- function runStateRecoveryViaMcp(gateway) {
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
+ function readRunState(gateway) {
600
699
  let port;
601
700
  try {
602
701
  const url = new URL(gateway);
@@ -614,7 +713,7 @@ function runStateRecoveryViaMcp(gateway) {
614
713
  } catch {
615
714
  return void 0;
616
715
  }
617
- return Boolean(state.recovery_via_mcp);
716
+ return { recoveryViaMcp: Boolean(state.recovery_via_mcp), compatUpstreams: compatUpstreamsOf(state.compat_upstreams) };
618
717
  } catch {
619
718
  return void 0;
620
719
  }
@@ -714,18 +813,18 @@ function index_default(pi) {
714
813
  }
715
814
  const gateway = gatewayUrl();
716
815
  const alive = await proxyAlive(gateway);
717
- const published = runStateRecoveryViaMcp(gateway);
816
+ const published = readRunState(gateway);
718
817
  const recoveryReady = alive ? await recovery.ensure() : false;
719
818
  gateDone = true;
720
- if (!alive || published === void 0) {
819
+ if (!alive || !published) {
721
820
  notify(ctx, "Caveman: direct mode, no compression this session (local proxy not running)", "warning");
722
821
  return;
723
822
  }
724
- if (!published || !recoveryReady) {
823
+ if (!published.recoveryViaMcp || !recoveryReady) {
725
824
  notify(ctx, "Caveman: direct mode, no compression this session (recovery not available \u2014 run `caveman doctor pi`)", "warning");
726
825
  return;
727
826
  }
728
- await router.openGate(gateway, ctx);
827
+ await router.openGate(gateway, ctx, published.compatUpstreams);
729
828
  }));
730
829
  pi.on("model_select", GUARD_model_select(async (event, ctx) => {
731
830
  if (gateDone) await router?.apply(event.model, ctx);