@jeffusion/bungee-core 5.24.0 → 5.24.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -326,7 +326,9 @@
326
326
  "GET"
327
327
  ],
328
328
  "outboundHeaders": {
329
- "passthrough": [],
329
+ "passthrough": [
330
+ "If-None-Match"
331
+ ],
330
332
  "set": {
331
333
  "Accept": "application/json",
332
334
  "User-Agent": "codex_cli_rs/0.153.3 (Mac OS 26.3.1; arm64) iTerm.app/3.6.9",
@@ -388,7 +390,9 @@
388
390
  "GET"
389
391
  ],
390
392
  "outboundHeaders": {
391
- "passthrough": [],
393
+ "passthrough": [
394
+ "If-None-Match"
395
+ ],
392
396
  "set": {
393
397
  "Accept": "application/json",
394
398
  "User-Agent": "Bungee/5.11.0",
@@ -3427,6 +3427,38 @@ var model_template_0_160_1_default = {
3427
3427
  };
3428
3428
 
3429
3429
  // plugins/codex-router/server/catalog.ts
3430
+ class CatalogSnapshots {
3431
+ now;
3432
+ entries = new Map;
3433
+ constructor(now = Date.now) {
3434
+ this.now = now;
3435
+ }
3436
+ get(key) {
3437
+ const entry = this.entries.get(key);
3438
+ if (entry) {
3439
+ this.entries.delete(key);
3440
+ this.entries.set(key, entry);
3441
+ }
3442
+ return entry;
3443
+ }
3444
+ fresh(snapshot) {
3445
+ return snapshot.expiresAt > this.now();
3446
+ }
3447
+ put(key, native, upstreamEtag) {
3448
+ this.entries.delete(key);
3449
+ if (Buffer.byteLength(JSON.stringify(native), "utf8") > 256 * 1024)
3450
+ return;
3451
+ if (this.entries.size >= 32)
3452
+ this.entries.delete(this.entries.keys().next().value);
3453
+ this.entries.set(key, { native: structuredClone(native), upstreamEtag, expiresAt: this.now() + 600000 });
3454
+ }
3455
+ invalidate(key) {
3456
+ this.entries.delete(key);
3457
+ }
3458
+ clear() {
3459
+ this.entries.clear();
3460
+ }
3461
+ }
3430
3462
  function mergeCatalog(native, bindings, catalog) {
3431
3463
  if (!native || typeof native !== "object" || Array.isArray(native))
3432
3464
  throw new Error("codex_router_invalid_catalog");
@@ -3552,6 +3584,7 @@ var CodexRouterPlugin = definePlugin(class {
3552
3584
  history;
3553
3585
  validators;
3554
3586
  catalogRequests = new Map;
3587
+ snapshots = new CatalogSnapshots;
3555
3588
  conversion;
3556
3589
  constructor(options) {
3557
3590
  this.bindings = parseBindings(options?.models);
@@ -3565,7 +3598,10 @@ var CodexRouterPlugin = definePlugin(class {
3565
3598
  try {
3566
3599
  this.validators = catalogValidatorsClient(context.services);
3567
3600
  } catch {}
3568
- context.services.onDispose?.(() => this.catalogRequests.clear());
3601
+ context.services.onDispose?.(() => {
3602
+ this.catalogRequests.clear();
3603
+ this.snapshots.clear();
3604
+ });
3569
3605
  }
3570
3606
  bodyRequirements(context) {
3571
3607
  return { request: context.method === "POST" && /\/responses$/.test(context.url.pathname) ? "json-write" : "none", response: context.method === "GET" && /\/models$/.test(context.url.pathname) ? ["json"] : [] };
@@ -3702,10 +3738,41 @@ var CodexRouterPlugin = definePlugin(class {
3702
3738
  for (const key of Object.keys(context.headers))
3703
3739
  if (["if-none-match", "if-modified-since"].includes(key.toLowerCase()))
3704
3740
  delete context.headers[key];
3741
+ const record = this.catalogRequests.get(context.requestId);
3742
+ if (record) {
3743
+ record.snapshot = this.snapshots.get(record.key);
3744
+ if (record.snapshot?.upstreamEtag)
3745
+ context.headers["if-none-match"] = record.snapshot.upstreamEtag;
3746
+ }
3705
3747
  }
3706
3748
  return context;
3707
3749
  });
3750
+ hooks.onInterceptRequest.tapPromise("codex-router.catalog-cache", async (context) => {
3751
+ if (context.method !== "GET" || !/\/models$/.test(context.originalUrl.pathname))
3752
+ return;
3753
+ const record = this.catalogRequests.get(context.requestId);
3754
+ const revalidate = Object.entries(context.headers).some(([key, value]) => key.toLowerCase() === "cache-control" && /(?:^|,)\s*(?:no-cache|max-age=0)\s*(?:,|$)/i.test(value));
3755
+ if (!record?.snapshot || !this.snapshots.fresh(record.snapshot) || revalidate)
3756
+ return;
3757
+ const response = await this.catalogResponse(record.snapshot.native, new Headers, record);
3758
+ return { action: "respond", response };
3759
+ });
3708
3760
  hooks.onRawResponse.tapPromise("codex-router.catalog-notification", async (result, context) => {
3761
+ if (context.method === "GET" && /\/models$/.test(context.originalUrl.pathname)) {
3762
+ const record = this.catalogRequests.get(context.requestId);
3763
+ if (record && [401, 403].includes(result.response.status))
3764
+ this.snapshots.invalidate(record.key);
3765
+ if (result.response.status === 304) {
3766
+ if (!record?.snapshot?.upstreamEtag)
3767
+ throw new DataAdmissionError(502, "codex_router_catalog_without_snapshot");
3768
+ const headers = new Headers(result.response.headers);
3769
+ for (const key of ["content-length", "content-encoding", "transfer-encoding"])
3770
+ headers.delete(key);
3771
+ headers.set("content-type", "application/json");
3772
+ headers.set("etag", headers.get("etag") ?? record.snapshot.upstreamEtag);
3773
+ return { ...result, response: new Response(JSON.stringify(record.snapshot.native), { headers }) };
3774
+ }
3775
+ }
3709
3776
  if (context.method === "POST" && /\/responses$/.test(context.originalUrl.pathname)) {
3710
3777
  await this.rewriteCatalogNotification(result.response, this.catalogRequests.get(context.requestId));
3711
3778
  }
@@ -3716,25 +3783,31 @@ var CodexRouterPlugin = definePlugin(class {
3716
3783
  if (context.method !== "GET" || !/\/models$/.test(context.originalUrl.pathname) || !response.ok)
3717
3784
  return response;
3718
3785
  const native = await context.bodyHandle.json({ id: "codex-router.catalog", mandatory: true });
3719
- const body = JSON.stringify(mergeCatalog(native, this.bindings, this.conversion));
3720
- const etag = catalogEtag(body);
3721
- const headers = new Headers(response.headers);
3722
- for (const key of ["content-length", "content-encoding", "etag", "last-modified"])
3723
- headers.delete(key);
3724
- headers.set("content-type", "application/json");
3725
- headers.set("cache-control", "private, no-store");
3726
- headers.set("etag", etag);
3727
- if (record && this.validators) {
3728
- try {
3729
- await this.validators.put({ key: record.key, etag }, { signal: record.signal, timeoutMs: 1000, operationId: crypto.randomUUID() });
3730
- } catch {}
3731
- }
3732
- return new Response(body, { status: response.status, headers });
3786
+ const merged = await this.catalogResponse(native, response.headers, record, response.status);
3787
+ if (record && !record.signal.aborted)
3788
+ this.snapshots.put(record.key, native, response.headers.get("etag"));
3789
+ return merged;
3733
3790
  });
3734
3791
  hooks.onFinally.tap("codex-router.catalog-cleanup", (context) => {
3735
3792
  this.catalogRequests.delete(context.requestId);
3736
3793
  });
3737
3794
  }
3795
+ async catalogResponse(native, sourceHeaders, record, status = 200) {
3796
+ const body = JSON.stringify(mergeCatalog(native, this.bindings, this.conversion));
3797
+ const etag = catalogEtag(body);
3798
+ const headers = new Headers(sourceHeaders);
3799
+ for (const key of ["content-length", "content-encoding", "etag", "last-modified"])
3800
+ headers.delete(key);
3801
+ headers.set("content-type", "application/json");
3802
+ headers.set("cache-control", "private, no-store");
3803
+ headers.set("etag", etag);
3804
+ if (record && this.validators) {
3805
+ try {
3806
+ await this.validators.put({ key: record.key, etag }, { signal: record.signal, timeoutMs: 1000, operationId: crypto.randomUUID() });
3807
+ } catch {}
3808
+ }
3809
+ return new Response(body, { status, headers });
3810
+ }
3738
3811
  async rewriteCatalogNotification(response, record) {
3739
3812
  response.headers.delete("x-models-etag");
3740
3813
  if (record && this.validators) {
@@ -3747,6 +3820,7 @@ var CodexRouterPlugin = definePlugin(class {
3747
3820
  }
3748
3821
  reset() {
3749
3822
  this.catalogRequests.clear();
3823
+ this.snapshots.clear();
3750
3824
  }
3751
3825
  });
3752
3826
  var server_default = CodexRouterPlugin;
@@ -3755,4 +3829,4 @@ export {
3755
3829
  server_default as default
3756
3830
  };
3757
3831
 
3758
- //# debugId=68FE5D69208B581A64756E2164756E21
3832
+ //# debugId=F4766DDE3923022964756E2164756E21