mslxdff 0.1.28 → 0.1.29

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/routes.js +42 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mslxdff",
3
- "version": "0.1.28",
3
+ "version": "0.1.29",
4
4
  "description": "测试项目,请勿使用。",
5
5
  "type": "module",
6
6
  "bin": {
package/src/routes.js CHANGED
@@ -84,19 +84,27 @@ async function relay(res, upRes, body, { onFirstChunk, streamTimeoutMs = STREAM_
84
84
  res.setHeader("Cache-Control", "no-cache");
85
85
  res.setHeader("Connection", "keep-alive");
86
86
  let ttf = null;
87
+ let interrupted = false;
87
88
  if (upRes.body) {
88
89
  let first = true;
89
90
  let wroteAny = false;
90
91
  let timedOut = false;
91
- const timer = setTimeout(() => {
92
+ // whole-stream ceiling (TTFB + generation): cancels the upstream body so
93
+ // the loop exits and we proactively end the stream instead of hanging.
94
+ let genTooLong = false;
95
+ const genTimer = GEN_TIMEOUT_MS
96
+ ? setTimeout(() => {
97
+ genTooLong = true;
98
+ if (typeof upRes.body.cancel === "function") upRes.body.cancel().catch(() => {});
99
+ }, GEN_TIMEOUT_MS)
100
+ : null;
101
+ const firstTimer = setTimeout(() => {
92
102
  timedOut = true;
93
- // nothing written yet — cancel the upstream body so the loop can exit
94
- // and we can fail over to the next model cleanly.
95
103
  if (typeof upRes.body.cancel === "function") upRes.body.cancel().catch(() => {});
96
104
  }, streamTimeoutMs);
97
105
  try {
98
106
  for await (const chunk of upRes.body) {
99
- if (timedOut) break;
107
+ if (timedOut || genTooLong) break;
100
108
  if (first) {
101
109
  first = false;
102
110
  ttf = Math.round(performance.now() - t0);
@@ -106,20 +114,25 @@ async function relay(res, upRes, body, { onFirstChunk, streamTimeoutMs = STREAM_
106
114
  res.write(chunk);
107
115
  }
108
116
  } catch (err) {
109
- timedOut = true;
117
+ // body threw — if we already started, treat as a mid-stream interrupt
118
+ if (!wroteAny) { timedOut = true; genTooLong = false; }
110
119
  } finally {
111
- clearTimeout(timer);
120
+ if (firstTimer) clearTimeout(firstTimer);
121
+ if (genTimer) clearTimeout(genTimer);
112
122
  }
113
123
  if (timedOut && !wroteAny) {
114
124
  // nothing written to res yet — safe to drop this model and let the
115
125
  // caller fail over to the next one. Do NOT write/end res here.
116
126
  return { status: STREAM_TIMEOUT_MS, ttfMs: null, totalMs: Math.round(performance.now() - t0), aborted: true };
117
127
  }
118
- if (timedOut && wroteAny) {
119
- // we'd already started streaming when it died — can't fail over, just
120
- // end the response so the client sees a clean EOF.
128
+ if (genTooLong || (timedOut && wroteAny)) {
129
+ // we'd already started streaming when it exceeded the ceiling — can't
130
+ // cleanly fail over a half-written response, just end it so the client
131
+ // sees a clean EOF rather than hanging. interrupted signals the caller
132
+ // to remember this model as slow.
133
+ interrupted = true;
121
134
  try { res.end(); } catch { /* ignore */ }
122
- return { status: 200, ttfMs: ttf, totalMs: Math.round(performance.now() - t0), aborted: false };
135
+ return { status: 200, ttfMs: ttf, totalMs: Math.round(performance.now() - t0), aborted: false, interrupted };
123
136
  }
124
137
  }
125
138
  const totalMs = Math.round(performance.now() - t0);
@@ -255,6 +268,15 @@ export const STREAM_TIMEOUT_MS = (() => {
255
268
  return Number.isInteger(n) && n > 0 ? n : 25_000;
256
269
  })();
257
270
 
271
+ // Ceiling on the total wall-clock a single streamed response may run (TTFB +
272
+ // generation + relay). Once exceeded we proactively end the stream so the
273
+ // client gets a clean EOF instead of hanging on a very slow model; the model
274
+ // is then flagged slow and demoted for the next request. Set to 0 to disable.
275
+ export const GEN_TIMEOUT_MS = (() => {
276
+ const n = Number(process.env.MSLXDFF_GEN_TIMEOUT_MS);
277
+ return Number.isInteger(n) && n > 0 ? n : 20_000;
278
+ })();
279
+
258
280
  async function racePeerCandidates(candidates, ctx) {
259
281
  for (let i = 0; i < candidates.length; i += PEER_RACE_LIMIT) {
260
282
  const batch = candidates.slice(i, i + PEER_RACE_LIMIT);
@@ -414,6 +436,16 @@ const ROUTES = [
414
436
  upRes = null;
415
437
  continue;
416
438
  }
439
+ if (out.interrupted) {
440
+ // the whole stream ran past GEN_TIMEOUT_MS — we ended it proactively
441
+ // so the client got a clean EOF instead of hanging. Remember this
442
+ // model as slow so the next request prefers a faster one.
443
+ if (auto) await auto.recordError(model, { status: 200, slow: true, note: `gen timeout ${GEN_TIMEOUT_MS}ms` });
444
+ evt("slow-model", { model, elapsedMs: out.totalMs ?? (Date.now() - startedAt), threshold: GEN_TIMEOUT_MS, interrupted: true });
445
+ logCall(model, 200);
446
+ evt("result", { model, status: out.status, via: "local", timing: upRes._t ?? null, ttfMs: out.ttfMs, totalMs: out.totalMs, interrupted: true });
447
+ return;
448
+ }
417
449
  // A model that took a long wall-clock time (TTFB + generation + relay)
418
450
  // gets remembered as slow so the next request prefers a faster one.
419
451
  const elapsed = Date.now() - startedAt;