agentic-server 0.31.1 → 0.31.2

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 (3) hide show
  1. package/esm/router.js +19 -12
  2. package/package.json +1 -1
  3. package/router.js +19 -12
package/esm/router.js CHANGED
@@ -22,6 +22,22 @@ const log = new Logger('agentic-server');
22
22
  * with `400`, or report that `err` was something else. A client mistake must
23
23
  * not be metered or reported as a provider failure.
24
24
  */
25
+ /**
26
+ * A signal that fires when the caller hangs up before its response is sent, so
27
+ * the upstream call it was waiting on stops too: a worker restart or a timed-out
28
+ * caller otherwise leaves the provider computing an answer nobody reads, billed
29
+ * and holding the provider's capacity from the retry that replaces it. The
30
+ * request stream's own 'close' fires as soon as its body is consumed, so the
31
+ * response is what reports the disconnect.
32
+ */
33
+ function abortOnHangUp(res) {
34
+ const abort = new AbortController();
35
+ res.on('close', () => {
36
+ if (!res.writableEnded)
37
+ abort.abort();
38
+ });
39
+ return abort.signal;
40
+ }
25
41
  function rejectBadRequest(res, err) {
26
42
  if (!(err instanceof BadRequestError))
27
43
  return false;
@@ -133,21 +149,11 @@ export const createRouter = (options) => {
133
149
  const transformed = transformChatRequest(provider, req.body || {});
134
150
  const body = streaming ? withUsageStreamOptions(transformed) : transformed;
135
151
  const headers = buildProviderHeaders(provider);
136
- const abort = new AbortController();
137
- // A client that hangs up mid-stream stops the upstream read; the request
138
- // stream's own 'close' fires as soon as the body is consumed, so the
139
- // response is what reports the disconnect.
140
- if (streaming) {
141
- res.on('close', () => {
142
- if (!res.writableEnded)
143
- abort.abort();
144
- });
145
- }
146
152
  const upstream = await fetch(upstreamUrl, {
147
153
  method: 'POST',
148
154
  headers,
149
155
  body: JSON.stringify(body),
150
- ...(streaming ? { signal: abort.signal } : {})
156
+ signal: abortOnHangUp(res)
151
157
  });
152
158
  const latencyMs = Number(process.hrtime.bigint() - startTime) / 1e6;
153
159
  if (!upstream.ok) {
@@ -275,7 +281,8 @@ export const createRouter = (options) => {
275
281
  const upstream = await fetch(upstreamUrl, {
276
282
  method: 'POST',
277
283
  headers,
278
- body: JSON.stringify(body)
284
+ body: JSON.stringify(body),
285
+ signal: abortOnHangUp(res)
279
286
  });
280
287
  const latencyMs = Number(process.hrtime.bigint() - startTime) / 1e6;
281
288
  if (!upstream.ok) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agentic-server",
3
- "version": "0.31.1",
3
+ "version": "0.31.2",
4
4
  "description": "Standalone OpenAI-compatible LLM gateway — multi-provider proxy with pluggable inference metering",
5
5
  "main": "index.js",
6
6
  "module": "esm/index.js",
package/router.js CHANGED
@@ -25,6 +25,22 @@ const log = new logger_1.Logger('agentic-server');
25
25
  * with `400`, or report that `err` was something else. A client mistake must
26
26
  * not be metered or reported as a provider failure.
27
27
  */
28
+ /**
29
+ * A signal that fires when the caller hangs up before its response is sent, so
30
+ * the upstream call it was waiting on stops too: a worker restart or a timed-out
31
+ * caller otherwise leaves the provider computing an answer nobody reads, billed
32
+ * and holding the provider's capacity from the retry that replaces it. The
33
+ * request stream's own 'close' fires as soon as its body is consumed, so the
34
+ * response is what reports the disconnect.
35
+ */
36
+ function abortOnHangUp(res) {
37
+ const abort = new AbortController();
38
+ res.on('close', () => {
39
+ if (!res.writableEnded)
40
+ abort.abort();
41
+ });
42
+ return abort.signal;
43
+ }
28
44
  function rejectBadRequest(res, err) {
29
45
  if (!(err instanceof errors_1.BadRequestError))
30
46
  return false;
@@ -137,21 +153,11 @@ const createRouter = (options) => {
137
153
  const transformed = (0, transforms_1.transformChatRequest)(provider, req.body || {});
138
154
  const body = streaming ? (0, streaming_1.withUsageStreamOptions)(transformed) : transformed;
139
155
  const headers = (0, providers_1.buildProviderHeaders)(provider);
140
- const abort = new AbortController();
141
- // A client that hangs up mid-stream stops the upstream read; the request
142
- // stream's own 'close' fires as soon as the body is consumed, so the
143
- // response is what reports the disconnect.
144
- if (streaming) {
145
- res.on('close', () => {
146
- if (!res.writableEnded)
147
- abort.abort();
148
- });
149
- }
150
156
  const upstream = await fetch(upstreamUrl, {
151
157
  method: 'POST',
152
158
  headers,
153
159
  body: JSON.stringify(body),
154
- ...(streaming ? { signal: abort.signal } : {})
160
+ signal: abortOnHangUp(res)
155
161
  });
156
162
  const latencyMs = Number(process.hrtime.bigint() - startTime) / 1e6;
157
163
  if (!upstream.ok) {
@@ -279,7 +285,8 @@ const createRouter = (options) => {
279
285
  const upstream = await fetch(upstreamUrl, {
280
286
  method: 'POST',
281
287
  headers,
282
- body: JSON.stringify(body)
288
+ body: JSON.stringify(body),
289
+ signal: abortOnHangUp(res)
283
290
  });
284
291
  const latencyMs = Number(process.hrtime.bigint() - startTime) / 1e6;
285
292
  if (!upstream.ok) {