@jeffusion/bungee-core 6.16.1 → 6.17.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 (37) hide show
  1. package/dist/main.js +5700 -2198
  2. package/dist/observability-worker.js +3245 -1495
  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-logs/index.js +505 -118
  24. package/dist/plugins/request-logs/index.js.map +11 -10
  25. package/dist/plugins/request-logs/ingress.js +3 -23
  26. package/dist/plugins/request-logs/ingress.js.map +3 -3
  27. package/dist/plugins/request-logs/ui/RequestLogsPage.svelte +215 -130
  28. package/dist/plugins/signature-repair/index.js.map +2 -2
  29. package/dist/plugins/token-budget/index.js.map +2 -2
  30. package/dist/plugins/token-metering/index.js.map +2 -2
  31. package/dist/plugins/token-stats/control.js +257 -61
  32. package/dist/plugins/token-stats/control.js.map +7 -6
  33. package/dist/plugins/token-stats/index.js +271 -127
  34. package/dist/plugins/token-stats/index.js.map +8 -8
  35. package/dist/plugins/token-stats/manifest.json +2 -2
  36. package/dist/plugins/user-auth/index.js.map +2 -2
  37. package/package.json +3 -2
@@ -580,12 +580,15 @@ class CodexProtocolError extends Error {
580
580
  kind;
581
581
  reason;
582
582
  diagnostic;
583
- constructor(kind, message, reason, diagnostic) {
583
+ upstreamProtocol;
584
+ constructor(kind, message, reason, diagnostic, upstreamProtocol) {
584
585
  super(message);
585
586
  this.name = "CodexProtocolError";
586
587
  this.kind = kind;
587
588
  this.reason = reason;
588
589
  this.diagnostic = diagnostic;
590
+ if (upstreamProtocol)
591
+ this.upstreamProtocol = upstreamProtocol;
589
592
  }
590
593
  }
591
594
  var UNSUPPORTED_RESPONSES_FIELDS = [
@@ -864,10 +867,10 @@ async function* parseCodexSSE(source, options = {}) {
864
867
  }
865
868
  const parsed = frame.json;
866
869
  if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
867
- throw new CodexProtocolError("invalid_sse", "Codex response contained invalid SSE JSON");
870
+ throw new CodexProtocolError("invalid_sse", "Codex response contained invalid SSE JSON", undefined, undefined, "protocol_error");
868
871
  const object = parsed;
869
872
  if (typeof object.type !== "string" || !object.type)
870
- throw new CodexProtocolError("invalid_sse", "Codex SSE event has no type");
873
+ throw new CodexProtocolError("invalid_sse", "Codex SSE event has no type", undefined, undefined, "protocol_error");
871
874
  yield { type: object.type, data: object, id: frame.id, envelope: frame };
872
875
  }
873
876
  } catch (error) {
@@ -971,7 +974,7 @@ function incompleteFinishReason(reason) {
971
974
  return "content_filter";
972
975
  if (reason === "max_tokens" || reason === "max_output_tokens")
973
976
  return "length";
974
- throw new CodexProtocolError("incomplete", `Codex response has an unsupported incomplete reason: ${reason || "missing"}`, reason);
977
+ throw new CodexProtocolError("incomplete", `Codex response has an unsupported incomplete reason: ${reason || "missing"}`, reason, undefined, "terminal");
975
978
  }
976
979
 
977
980
  class CodexResponseProcessor {
@@ -1071,7 +1074,7 @@ class CodexResponseProcessor {
1071
1074
  const keys = this.messageKeys(data, item);
1072
1075
  const owners = new Set(keys.map((key) => this.messageOwners.get(key)).filter((owner) => owner !== undefined));
1073
1076
  if (owners.size > 1)
1074
- throw new CodexProtocolError("invalid_response", "Codex message event identifiers point to different messages");
1077
+ throw new CodexProtocolError("invalid_response", "Codex message event identifiers point to different messages", undefined, undefined, "protocol_error");
1075
1078
  const owner = owners.values().next().value ?? keys[0];
1076
1079
  for (const key of keys)
1077
1080
  this.messageOwners.set(key, owner);
@@ -1105,18 +1108,18 @@ class CodexResponseProcessor {
1105
1108
  return [];
1106
1109
  }
1107
1110
  if (type === "response.failed" || data.response?.status === "failed")
1108
- throw new CodexProtocolError("failed", "Codex response failed", undefined, upstreamErrorDiagnostic(data, "Codex response failed", this.redactDiagnostic));
1111
+ throw new CodexProtocolError("failed", "Codex response failed", undefined, upstreamErrorDiagnostic(data, "Codex response failed", this.redactDiagnostic), "terminal");
1109
1112
  if (this.sawDone && !this.terminal)
1110
1113
  throw new CodexProtocolError("incomplete", "Codex emitted an event after [DONE] without a terminal response");
1111
1114
  if (this.terminal) {
1112
1115
  if (type === "error" || type === "response.failed")
1113
- throw new CodexProtocolError("failed", "Codex response failed after terminal event", undefined, upstreamErrorDiagnostic(data, "Codex response failed after terminal event", this.redactDiagnostic));
1116
+ throw new CodexProtocolError("failed", "Codex response failed after terminal event", undefined, upstreamErrorDiagnostic(data, "Codex response failed after terminal event", this.redactDiagnostic), "terminal");
1114
1117
  if (type === "response.completed" || type === "response.incomplete")
1115
1118
  return [];
1116
1119
  return [];
1117
1120
  }
1118
1121
  if (type === "error")
1119
- throw new CodexProtocolError("failed", "Codex response contained an error event", undefined, upstreamErrorDiagnostic(data, "Codex response contained an error event", this.redactDiagnostic));
1122
+ throw new CodexProtocolError("failed", "Codex response contained an error event", undefined, upstreamErrorDiagnostic(data, "Codex response contained an error event", this.redactDiagnostic), "terminal");
1120
1123
  if (type === "response.created" || type === "response.in_progress") {
1121
1124
  const response = data.response ?? {};
1122
1125
  this.responseId ||= String(response.id ?? "");
@@ -1238,7 +1241,7 @@ class CodexResponseProcessor {
1238
1241
  const completed = this.completedText.get(owner);
1239
1242
  if (completed !== undefined) {
1240
1243
  if (completed !== fullText)
1241
- throw new CodexProtocolError("invalid_response", "Codex repeated message completion changed its text");
1244
+ throw new CodexProtocolError("invalid_response", "Codex repeated message completion changed its text", undefined, undefined, "protocol_error");
1242
1245
  return output;
1243
1246
  }
1244
1247
  const previous = this.messageTextValue(data, item);
@@ -1267,7 +1270,7 @@ class CodexResponseProcessor {
1267
1270
  const response = data.response ?? {};
1268
1271
  const incomplete = type === "response.incomplete" || response.status === "incomplete";
1269
1272
  if (response.status && !["completed", "incomplete"].includes(response.status))
1270
- throw new CodexProtocolError("invalid_response", "Codex response has an unsupported terminal status");
1273
+ throw new CodexProtocolError("invalid_response", "Codex response has an unsupported terminal status", undefined, undefined, "protocol_error");
1271
1274
  const incompleteReason = incomplete ? String(response.incomplete_details?.reason ?? "") : "";
1272
1275
  const incompleteFinish = incomplete ? incompleteFinishReason(incompleteReason) : undefined;
1273
1276
  this.terminal = incomplete ? "incomplete" : "completed";
@@ -1278,7 +1281,7 @@ class CodexResponseProcessor {
1278
1281
  if (response.service_tier)
1279
1282
  this.serviceTier = String(response.service_tier);
1280
1283
  if (response.output !== undefined && !Array.isArray(response.output))
1281
- throw new CodexProtocolError("invalid_response", "Codex response output is not an array");
1284
+ throw new CodexProtocolError("invalid_response", "Codex response output is not an array", undefined, undefined, "protocol_error");
1282
1285
  if (Array.isArray(response.output))
1283
1286
  response.output.forEach((item, index) => {
1284
1287
  this.validateOutputItem(item);
@@ -1302,7 +1305,7 @@ class CodexResponseProcessor {
1302
1305
  const keys = [data.item_id ? `item:${String(data.item_id)}` : "", data.output_index !== undefined ? `output:${String(data.output_index)}` : "", data.item?.id ? `item:${String(data.item.id)}` : ""].filter(Boolean);
1303
1306
  const matches = [...new Set(keys.map((key) => this.tools.get(key)).filter((state) => Boolean(state)))];
1304
1307
  if (matches.length > 1)
1305
- throw new CodexProtocolError("invalid_response", "Codex tool event identifiers point to different tool calls");
1308
+ throw new CodexProtocolError("invalid_response", "Codex tool event identifiers point to different tool calls", undefined, undefined, "protocol_error");
1306
1309
  if (matches.length === 1)
1307
1310
  return matches[0];
1308
1311
  if (keys.length > 0) {
@@ -1666,6 +1669,11 @@ function completionFromError(error, redact) {
1666
1669
  return { status: "failed", code: "invalid_response" };
1667
1670
  return { status: "failed", code: "body_error", error: errorDiagnostic(error, "transport", "Upstream response body could not be read", redact) };
1668
1671
  }
1672
+ function reportProtocolError(error, report) {
1673
+ if (!(error instanceof CodexProtocolError) || !error.upstreamProtocol || !report)
1674
+ return;
1675
+ report({ kind: error.upstreamProtocol, outcome: error.kind === "incomplete" ? "incomplete" : "failed", code: error.kind });
1676
+ }
1669
1677
  function bodyConsumer(signal) {
1670
1678
  return { id: "chatgpt-oauth", mandatory: true, signal };
1671
1679
  }
@@ -1762,7 +1770,7 @@ function nativeTerminal(event) {
1762
1770
  return { status: "failed", code: "invalid_response" };
1763
1771
  return status === "completed" ? { status } : { status, code: status === "failed" ? "upstream_error" : "incomplete" };
1764
1772
  }
1765
- function protocolStreamBody(handle, target, includeUsage, signal, redact, preserveCustomTools = false, native = false) {
1773
+ function protocolStreamBody(handle, target, includeUsage, signal, redact, preserveCustomTools = false, native = false, report) {
1766
1774
  const protocolController = new AbortController;
1767
1775
  const onAbort = () => protocolController.abort(signal.reason ?? "cancelled");
1768
1776
  if (signal.aborted)
@@ -1790,12 +1798,18 @@ function protocolStreamBody(handle, target, includeUsage, signal, redact, preser
1790
1798
  if (native) {
1791
1799
  const outcome = nativeTerminal(event);
1792
1800
  if (outcome) {
1801
+ report?.({
1802
+ kind: "code" in outcome && outcome.code === "invalid_response" ? "protocol_error" : "terminal",
1803
+ outcome: outcome.status,
1804
+ code: "code" in outcome ? outcome.code : undefined
1805
+ });
1793
1806
  terminalOutcome = outcome;
1794
1807
  settle(outcome);
1795
1808
  }
1796
1809
  } else if (event.type === "response.completed" || event.type === "response.incomplete") {
1797
1810
  const final = processor.finish();
1798
1811
  terminalOutcome = final.terminal === "completed" ? { status: "completed" } : { status: "incomplete", code: "incomplete" };
1812
+ report?.({ kind: "terminal", outcome: terminalOutcome.status, code: "code" in terminalOutcome ? terminalOutcome.code : undefined });
1799
1813
  settle(terminalOutcome);
1800
1814
  }
1801
1815
  if (target === "responses")
@@ -1814,6 +1828,7 @@ function protocolStreamBody(handle, target, includeUsage, signal, redact, preser
1814
1828
  `;
1815
1829
  settle(outcome);
1816
1830
  } catch (error) {
1831
+ reportProtocolError(error, report);
1817
1832
  const outcome = completionFromError(error, redact);
1818
1833
  settle(outcome.status === "cancelled" ? terminalOutcome ?? outcome : outcome);
1819
1834
  if (!native && outcome.status === "failed" && outcome.error) {
@@ -1978,6 +1993,7 @@ class ChatgptOauthAdapter {
1978
1993
  if (current.native)
1979
1994
  this.nativeResponses.add(result.response);
1980
1995
  const state = current;
1996
+ const report = context.responseOrigin === "upstream" ? context.reportUpstreamProtocol : undefined;
1981
1997
  const trackCompletion = (completion) => {
1982
1998
  if (!state.native)
1983
1999
  convertedCompletions.add(completion);
@@ -1989,6 +2005,8 @@ class ChatgptOauthAdapter {
1989
2005
  return response;
1990
2006
  };
1991
2007
  const errorResponse = (status, completion, error) => {
2008
+ if (error?.source !== "upstream")
2009
+ context.responseOrigin = "gateway";
1992
2010
  const response = mark(new Response(JSON.stringify({ error: errorBody2(error) }), {
1993
2011
  status,
1994
2012
  statusText: result.response.statusText,
@@ -2009,6 +2027,8 @@ class ChatgptOauthAdapter {
2009
2027
  }
2010
2028
  if (!result.response.ok) {
2011
2029
  const failure = await readHttpError(context);
2030
+ if (failure.status === "failed")
2031
+ report?.({ kind: "http_error", outcome: "failed", code: "upstream_http_error" });
2012
2032
  return errorResponse(result.response.status, joinCompletion(result.completion, Promise.resolve(failure), context.signal), "error" in failure ? failure.error : undefined);
2013
2033
  }
2014
2034
  if (state.target === "models") {
@@ -2054,7 +2074,7 @@ class ChatgptOauthAdapter {
2054
2074
  return errorResponse(502, joinCompletion(result.completion, bodyCompletion, context.signal));
2055
2075
  }
2056
2076
  if (state.stream) {
2057
- const converted = protocolStreamBody(context.bodyHandle, state.target, state.includeUsage, context.signal, context.redactDiagnostic, state.siwc, state.native);
2077
+ const converted = protocolStreamBody(context.bodyHandle, state.target, state.includeUsage, context.signal, context.redactDiagnostic, state.siwc, state.native, report);
2058
2078
  const streamCompletion = Promise.race([
2059
2079
  converted.completion,
2060
2080
  result.completion.then((upstream) => upstream.status === "failed" ? upstream : converted.completion, () => ({ status: "failed", code: "upstream_completion" }))
@@ -2076,12 +2096,14 @@ class ChatgptOauthAdapter {
2076
2096
  target: state.target,
2077
2097
  redactDiagnostic: context.redactDiagnostic
2078
2098
  });
2099
+ report?.({ kind: "terminal", outcome: stateResult.terminal, code: stateResult.terminal === "incomplete" ? "incomplete" : undefined });
2079
2100
  const body = state.target === "chat" ? responsesToChatCompletion(stateResult.response, state.siwc) : stateResult.response;
2080
2101
  const protocolCompletion = Promise.resolve(stateResult.terminal === "completed" ? { status: "completed" } : { status: "incomplete", code: "incomplete" });
2081
2102
  const nonStreamCompletion = joinCompletion(result.completion, protocolCompletion, context.signal);
2082
2103
  trackCompletion(nonStreamCompletion);
2083
2104
  return { response: mark(new Response(JSON.stringify(body), { status: result.response.status, statusText: result.response.statusText, headers: responseHeaders(result.response, "application/json; charset=utf-8") })), completion: nonStreamCompletion };
2084
2105
  } catch (error) {
2106
+ reportProtocolError(error, report);
2085
2107
  const failure = completionFromError(error, context.redactDiagnostic);
2086
2108
  const failedCompletion = joinCompletion(result.completion, Promise.resolve(failure), context.signal);
2087
2109
  return errorResponse(502, failedCompletion, "error" in failure ? failure.error : undefined);
@@ -2131,4 +2153,4 @@ export {
2131
2153
  server_default as default
2132
2154
  };
2133
2155
 
2134
- //# debugId=7D45F987B1FCBF9164756E2164756E21
2156
+ //# debugId=EF45E9D40A3D0FDA64756E2164756E21