@jeffusion/bungee-core 6.16.2 → 6.18.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.
Files changed (45) hide show
  1. package/dist/main.js +5691 -2189
  2. package/dist/observability-worker.js +3257 -1491
  3. package/dist/plugins/anthropic-request-sanitizer/index.js.map +2 -2
  4. package/dist/plugins/anthropic-tool-name-transformer/index.js.map +2 -2
  5. package/dist/plugins/chatgpt-oauth/index.js +37 -15
  6. package/dist/plugins/chatgpt-oauth/index.js.map +5 -5
  7. package/dist/plugins/codex-router/index.js +21 -3
  8. package/dist/plugins/codex-router/index.js.map +5 -5
  9. package/dist/plugins/deepseek-reasoning-fix/index.js.map +2 -2
  10. package/dist/plugins/key-access/index.js +1 -1
  11. package/dist/plugins/key-access/index.js.map +4 -4
  12. package/dist/plugins/key-access/ingress.js +1 -1
  13. package/dist/plugins/key-access/ingress.js.map +3 -3
  14. package/dist/plugins/key-rate-limit/index.js.map +2 -2
  15. package/dist/plugins/llm-protocol-adapter/control.js +1 -1
  16. package/dist/plugins/llm-protocol-adapter/control.js.map +3 -3
  17. package/dist/plugins/llm-protocol-adapter/index.js +1 -1
  18. package/dist/plugins/llm-protocol-adapter/index.js.map +5 -5
  19. package/dist/plugins/llm-tps/index.js.map +2 -2
  20. package/dist/plugins/model-mapping/index.js +1 -1
  21. package/dist/plugins/model-mapping/index.js.map +4 -4
  22. package/dist/plugins/models-dev/index.js.map +2 -2
  23. package/dist/plugins/request-body-redaction/control.js +58 -0
  24. package/dist/plugins/request-body-redaction/control.js.map +10 -0
  25. package/dist/plugins/request-body-redaction/index.js +7049 -0
  26. package/dist/plugins/request-body-redaction/index.js.map +28 -0
  27. package/dist/plugins/request-body-redaction/manifest.json +83 -0
  28. package/dist/plugins/request-body-redaction/ui/RedactionRulesEditor.svelte +475 -0
  29. package/dist/plugins/request-body-redaction/ui/RequestBodyRedactionPage.svelte +674 -0
  30. package/dist/plugins/request-body-redaction/ui/RequestBodyRedactionStats.svelte +218 -0
  31. package/dist/plugins/request-body-redaction/ui/route-bindings.ts +69 -0
  32. package/dist/plugins/request-logs/index.js +505 -118
  33. package/dist/plugins/request-logs/index.js.map +11 -10
  34. package/dist/plugins/request-logs/ingress.js +3 -23
  35. package/dist/plugins/request-logs/ingress.js.map +3 -3
  36. package/dist/plugins/request-logs/ui/RequestLogsPage.svelte +215 -130
  37. package/dist/plugins/signature-repair/index.js.map +2 -2
  38. package/dist/plugins/token-budget/index.js.map +2 -2
  39. package/dist/plugins/token-metering/index.js.map +2 -2
  40. package/dist/plugins/token-stats/control.js +111 -40
  41. package/dist/plugins/token-stats/control.js.map +6 -6
  42. package/dist/plugins/token-stats/index.js +92 -39
  43. package/dist/plugins/token-stats/index.js.map +5 -5
  44. package/dist/plugins/user-auth/index.js.map +2 -2
  45. package/package.json +3 -2
@@ -1,4 +1,7 @@
1
1
  // @bun
2
+ // plugins/token-stats/server/control.ts
3
+ import { SQLiteError } from "bun:sqlite";
4
+
2
5
  // plugins/models-dev/contract.ts
3
6
  var MODELS_DEV_SOURCE_URL = "https://models.dev/api.json";
4
7
  var MODELS_DEV_CATALOG_SERVICE_ID = "models-dev.catalog.v1";
@@ -268,26 +271,66 @@ class SQLiteTokenStatsMetering {
268
271
  constructor(observation) {
269
272
  this.observation = observation;
270
273
  }
271
- async recordAttempt(row) {
272
- return this.observation.withDatabase((db) => {
273
- validateTokenStatsAttempt(row);
274
- const transaction = db.transaction(() => {
275
- db.query(`
276
- INSERT OR IGNORE INTO token_stats_attempts (
277
- attempt_id, request_id, finished_at_ms, route_id, upstream_id, provider, outcome, model, key_id,
278
- input_tokens, output_tokens, input_source, output_source,
279
- cache_read_tokens, cache_write_tokens, cost_usd, observation_incomplete
280
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
281
- `).run(row.attempt_id, row.request_id, row.finished_at_ms, row.route_id, row.upstream_id, row.provider, row.outcome, row.model, row.key_id ?? null, row.input_tokens, row.output_tokens, row.input_source, row.output_source, row.cache_read_tokens, row.cache_write_tokens, row.cost_usd, row.observation_incomplete ? 1 : 0);
282
- db.query(`
283
- DELETE FROM token_stats_attempts WHERE attempt_id IN (
284
- SELECT attempt_id FROM token_stats_attempts INDEXED BY idx_token_stats_attempts_finished
285
- WHERE finished_at_ms < ? ORDER BY finished_at_ms LIMIT 500
286
- )
287
- `).run(Date.now() - TOKEN_STATS_RETENTION_MS);
274
+ async recordAttempt(row, deadlineAt) {
275
+ row = { ...row };
276
+ let remainingMs;
277
+ if (deadlineAt === undefined) {
278
+ const directDeadline = performance.now() + 50;
279
+ remainingMs = () => directDeadline - performance.now();
280
+ } else {
281
+ if (!Number.isSafeInteger(deadlineAt))
282
+ throw new Error("invalid token-stats write deadline");
283
+ const budget = this.observation.writeBudgetMs?.(deadlineAt);
284
+ if (!budget)
285
+ throw new Error("token-stats write deadline clock unavailable");
286
+ remainingMs = budget;
287
+ }
288
+ const requireWriteBudget = () => {
289
+ if (remainingMs() < TOKEN_STATS_BUSY_TIMEOUT_MS)
290
+ throw new Error("token-stats write deadline expired");
291
+ };
292
+ requireWriteBudget();
293
+ const maxAttempts = deadlineAt === undefined ? 8 : Infinity;
294
+ for (let attempt = 1;; attempt++) {
295
+ const busy = await this.observation.withDatabase((db) => {
296
+ validateTokenStatsAttempt(row);
297
+ const transaction = db.transaction(() => {
298
+ if (deadlineAt !== undefined)
299
+ requireWriteBudget();
300
+ db.query(`
301
+ INSERT OR IGNORE INTO token_stats_attempts (
302
+ attempt_id, request_id, finished_at_ms, route_id, upstream_id, provider, outcome, model, key_id,
303
+ input_tokens, output_tokens, input_source, output_source,
304
+ cache_read_tokens, cache_write_tokens, cost_usd, observation_incomplete
305
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
306
+ `).run(row.attempt_id, row.request_id, row.finished_at_ms, row.route_id, row.upstream_id, row.provider, row.outcome, row.model, row.key_id ?? null, row.input_tokens, row.output_tokens, row.input_source, row.output_source, row.cache_read_tokens, row.cache_write_tokens, row.cost_usd, row.observation_incomplete ? 1 : 0);
307
+ db.query(`
308
+ DELETE FROM token_stats_attempts WHERE attempt_id IN (
309
+ SELECT attempt_id FROM token_stats_attempts INDEXED BY idx_token_stats_attempts_finished
310
+ WHERE finished_at_ms < ? ORDER BY finished_at_ms LIMIT 500
311
+ )
312
+ `).run(Date.now() - TOKEN_STATS_RETENTION_MS);
313
+ if (deadlineAt !== undefined)
314
+ requireWriteBudget();
315
+ });
316
+ return withReportingWriteTimeout(db, () => {
317
+ try {
318
+ transaction.immediate();
319
+ } catch (error) {
320
+ if (error instanceof Error && error.name === "SQLiteError" && "code" in error && error.code === "SQLITE_BUSY" && db.inTransaction === false)
321
+ return error;
322
+ throw error;
323
+ }
324
+ });
288
325
  });
289
- withReportingWriteTimeout(db, () => transaction.immediate());
290
- });
326
+ if (!busy)
327
+ return;
328
+ if (attempt === maxAttempts || remainingMs() < TOKEN_STATS_BUSY_TIMEOUT_MS)
329
+ throw busy;
330
+ await new Promise((resolve) => setTimeout(resolve, 0));
331
+ if (remainingMs() < TOKEN_STATS_BUSY_TIMEOUT_MS)
332
+ throw busy;
333
+ }
291
334
  }
292
335
  async queryPricingModels(input) {
293
336
  const window = queryBounds(input);
@@ -573,13 +616,29 @@ var pendingCount = 0;
573
616
  var drainTimer;
574
617
  var draining = false;
575
618
  var lastDropLogAt = 0;
576
- var lastFailureLogAt = 0;
577
- function logRateLimited(logger, kind, error) {
619
+ var lastFailureLogAt = new Map;
620
+ function failureCode(error) {
621
+ if (error instanceof DOMException && error.name === "TimeoutError")
622
+ return "rpc_timeout";
623
+ if (error instanceof Error && error.name === "DataAdmissionError" && "code" in error) {
624
+ switch (error.code) {
625
+ case "invalid_worker_rpc":
626
+ case "worker_not_admitted":
627
+ case "rpc_busy":
628
+ case "rpc_capacity":
629
+ return error.code;
630
+ default:
631
+ return "rpc_rejected";
632
+ }
633
+ }
634
+ return "unclassified";
635
+ }
636
+ function logRateLimited(logger, stage, error) {
578
637
  try {
579
638
  logger.onFailure?.();
580
639
  } catch {}
581
640
  const now = Date.now();
582
- if (kind === "drop") {
641
+ if (stage === "drop") {
583
642
  if (now - lastDropLogAt < LOG_INTERVAL_MS)
584
643
  return;
585
644
  lastDropLogAt = now;
@@ -588,13 +647,12 @@ function logRateLimited(logger, kind, error) {
588
647
  } catch {}
589
648
  return;
590
649
  }
591
- if (now - lastFailureLogAt < LOG_INTERVAL_MS)
592
- return;
593
- lastFailureLogAt = now;
594
650
  try {
595
- logger.error("Token stats attempt processing failed; dropping metering row", {
596
- error: error instanceof Error ? error.message : error === undefined ? "attempt task returned no row" : String(error)
597
- });
651
+ const failure = `${stage}/${failureCode(error)}`;
652
+ if (now - (lastFailureLogAt.get(failure) ?? 0) < LOG_INTERVAL_MS)
653
+ return;
654
+ lastFailureLogAt.set(failure, now);
655
+ logger.error(`Token stats attempt processing failed; dropping metering row [${failure}]`);
598
656
  } catch {}
599
657
  }
600
658
  function scheduleDrain() {
@@ -612,17 +670,15 @@ async function drainOneAttempt() {
612
670
  if (!item)
613
671
  return;
614
672
  draining = true;
673
+ let stage = "prepare";
615
674
  try {
616
- try {
617
- const row = await item.task();
618
- if (row === undefined) {
619
- logRateLimited(item.logger, "failure");
620
- } else {
621
- await item.metering.recordAttempt(row);
622
- }
623
- } catch (error) {
624
- logRateLimited(item.logger, "failure", error);
625
- }
675
+ const row = await item.task();
676
+ if (row === undefined)
677
+ return logRateLimited(item.logger, "missing_row");
678
+ stage = "write";
679
+ await item.metering.recordAttempt(row);
680
+ } catch (error) {
681
+ logRateLimited(item.logger, stage, error);
626
682
  } finally {
627
683
  pendingCount--;
628
684
  draining = false;
@@ -1034,8 +1090,23 @@ function createControl(context) {
1034
1090
  var control_default = { supportsOnlinePreparation: true, createControl };
1035
1091
  function createObservationAdapter(observation) {
1036
1092
  const leaf = new SQLiteTokenStatsMetering(observation);
1093
+ let nextWriteFailureLogAt = 0;
1037
1094
  return Object.freeze({
1038
- recordAttempt: leaf.recordAttempt.bind(leaf),
1095
+ async recordAttempt(row, deadlineAt) {
1096
+ try {
1097
+ await leaf.recordAttempt(row, deadlineAt);
1098
+ } catch (error) {
1099
+ const now = Date.now();
1100
+ if (now >= nextWriteFailureLogAt) {
1101
+ nextWriteFailureLogAt = now + 60000;
1102
+ try {
1103
+ const requestId = typeof row?.request_id === "string" && /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i.test(row.request_id) ? row.request_id : undefined;
1104
+ logger.error({ requestId, sqliteErrno: error instanceof SQLiteError ? error.errno : undefined }, "Token stats observation write failed");
1105
+ } catch {}
1106
+ }
1107
+ throw error;
1108
+ }
1109
+ },
1039
1110
  queryWindowSnapshot: leaf.queryWindowSnapshot.bind(leaf),
1040
1111
  queryPricingModels: leaf.queryPricingModels.bind(leaf),
1041
1112
  listClientModels: leaf.listClientModels.bind(leaf)
@@ -1047,4 +1118,4 @@ export {
1047
1118
  control_default as default
1048
1119
  };
1049
1120
 
1050
- //# debugId=965474093B0BDEB764756E2164756E21
1121
+ //# debugId=EA92F7EB842DE65764756E2164756E21