ldrouter 1.11.17 → 1.13.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.
@@ -2,6 +2,7 @@
2
2
  import { decryptSecret, decryptCustomHeaders } from '../auth/crypto.js';
3
3
  import { GatewayError } from '../errors.js';
4
4
  import { buildHeaders, stripSlash } from '../providers/index.js';
5
+ import { debugUpstream, errorLine, formatError, truncate } from '../logging/debug.js';
5
6
  export function providerToUpstreamConfig(p) {
6
7
  let apiKey;
7
8
  let customHeaders;
@@ -34,7 +35,7 @@ export function providerToUpstreamConfig(p) {
34
35
  totalTimeoutMs: p.totalTimeoutMs,
35
36
  };
36
37
  }
37
- export async function callUpstreamNonStreaming(cfg, url, payload) {
38
+ export async function callUpstreamNonStreaming(cfg, url, payload, requestId = '-') {
38
39
  const ctl = new AbortController();
39
40
  const timer = setTimeout(() => ctl.abort(), cfg.totalTimeoutMs);
40
41
  const start = Date.now();
@@ -47,6 +48,7 @@ export async function callUpstreamNonStreaming(cfg, url, payload) {
47
48
  });
48
49
  const text = await res.text();
49
50
  const ttft = Date.now() - start;
51
+ logUpstreamResponse(requestId, res.status, res.statusText, res.headers, ttft, text);
50
52
  return {
51
53
  status: res.status,
52
54
  ok: res.ok,
@@ -58,6 +60,7 @@ export async function callUpstreamNonStreaming(cfg, url, payload) {
58
60
  }
59
61
  catch (e) {
60
62
  const err = e;
63
+ errorLine(requestId, 'UPSTREAM FETCH ERROR', [`url=${url}`, `afterMs=${Date.now() - start}`, ...formatError(e)]);
61
64
  if (err.name === 'AbortError') {
62
65
  throw new GatewayError('timeout_error', 'Upstream request timed out', { status: 504, cause: e });
63
66
  }
@@ -73,11 +76,28 @@ export function extractUpstreamRequestId(headers) {
73
76
  }
74
77
  return headers['x-request-id'] ?? headers['request-id'] ?? headers['x-amzn-requestid'] ?? null;
75
78
  }
79
+ /** docs/13 §14: upstream response summary (sanitized headers) + error body for non-2xx. */
80
+ function logUpstreamResponse(requestId, status, statusText, headers, durationMs, body) {
81
+ const h = (name) => headers.get(name) ?? undefined;
82
+ debugUpstream(requestId, 'UPSTREAM RESPONSE', [
83
+ `status=${status}`,
84
+ `statusText=${statusText}`,
85
+ `content-type=${h('content-type') ?? 'undefined'}`,
86
+ `content-length=${h('content-length') ?? 'undefined'}`,
87
+ `transfer-encoding=${h('transfer-encoding') ?? 'undefined'}`,
88
+ `server=${h('server') ?? 'undefined'}`,
89
+ `durationToHeadersMs=${durationMs}`,
90
+ `upstreamRequestId=${extractUpstreamRequestId(headers) ?? 'null'}`,
91
+ ]);
92
+ if (status < 200 || status >= 300) {
93
+ errorLine(requestId, 'UPSTREAM ERROR BODY', [truncate(body, 4000)]);
94
+ }
95
+ }
76
96
  /**
77
97
  * Call upstream with SSE streaming. Invokes onChunk for each SSE event.
78
98
  * Returns a promise resolving when the stream completes or rejects on failure.
79
99
  */
80
- export async function callUpstreamStreaming(cfg, url, payload, onChunk) {
100
+ export async function callUpstreamStreaming(cfg, url, payload, onChunk, requestId = '-') {
81
101
  const ctl = new AbortController();
82
102
  const totalTimer = setTimeout(() => ctl.abort(), cfg.totalTimeoutMs);
83
103
  const start = Date.now();
@@ -98,8 +118,10 @@ export async function callUpstreamStreaming(cfg, url, payload, onChunk) {
98
118
  });
99
119
  if (!res.ok || !res.body) {
100
120
  const text = await res.text();
121
+ logUpstreamResponse(requestId, res.status, res.statusText, res.headers, Date.now() - start, text);
101
122
  throw new UpstreamHttpError(res.status, text, extractUpstreamRequestId(res.headers));
102
123
  }
124
+ logUpstreamResponse(requestId, res.status, res.statusText, res.headers, Date.now() - start, '');
103
125
  // First-token watchdog
104
126
  firstTokenTimer = setTimeout(() => ctl.abort(), cfg.firstTokenTimeoutMs);
105
127
  resetIdle();
@@ -161,6 +183,12 @@ export async function callUpstreamStreaming(cfg, url, payload, onChunk) {
161
183
  throw new GatewayError('upstream_error', `Upstream HTTP ${e.status}: ${e.bodyExcerpt}`, { status: 502, cause: e });
162
184
  }
163
185
  const err = e;
186
+ errorLine(requestId, 'UPSTREAM FETCH ERROR', [
187
+ `url=${url}`,
188
+ `afterMs=${Date.now() - start}`,
189
+ `ttftKnown=${ttft !== null}`,
190
+ ...formatError(e),
191
+ ]);
164
192
  if (err.name === 'AbortError') {
165
193
  if (ttft === null && firstTokenTimer) {
166
194
  throw new GatewayError('timeout_error', 'Upstream first token timeout', { status: 504, cause: e });