@drsamonline/prober 0.12.36 → 0.12.38
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.
- package/package.json +1 -1
- package/src/index.js +74 -11
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -93,6 +93,40 @@ export const FIXTURES = {
|
|
|
93
93
|
|
|
94
94
|
const DEFAULT_TIMEOUT_MS = 8000;
|
|
95
95
|
|
|
96
|
+
/**
|
|
97
|
+
* How many entries a sweep keeps in flight at once.
|
|
98
|
+
*
|
|
99
|
+
* The sweep used to be an unbounded `Promise.all`, which is fast and wrong: 41
|
|
100
|
+
* simultaneous handshakes from one IP is what a provider's rate limiter answers
|
|
101
|
+
* with 429, and a 429 is recorded as "not suitable", so a working provider gets
|
|
102
|
+
* demoted for being asked too eagerly. Eight is the number of providers a free
|
|
103
|
+
* tier typically allows a burst from without complaint, and it still finishes a
|
|
104
|
+
* 41-entry sweep in a few seconds rather than the minutes a serial loop took.
|
|
105
|
+
*/
|
|
106
|
+
export const DEFAULT_PROBE_CONCURRENCY = 8;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Run `fn` over `items` with at most `limit` in flight, keeping the input order.
|
|
110
|
+
*
|
|
111
|
+
* Order matters: `sui probe` prints one row per catalogue entry and the state map
|
|
112
|
+
* is built from these results, so a pool that returned results as they finished
|
|
113
|
+
* would shuffle the table between runs.
|
|
114
|
+
*/
|
|
115
|
+
async function mapPool(items, limit, fn) {
|
|
116
|
+
const results = new Array(items.length);
|
|
117
|
+
let next = 0;
|
|
118
|
+
const worker = async () => {
|
|
119
|
+
for (;;) {
|
|
120
|
+
const index = next++;
|
|
121
|
+
if (index >= items.length) return;
|
|
122
|
+
results[index] = await fn(items[index], index);
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
const width = Math.max(1, Math.min(Number(limit) || DEFAULT_PROBE_CONCURRENCY, items.length));
|
|
126
|
+
await Promise.all(Array.from({ length: width }, worker));
|
|
127
|
+
return results;
|
|
128
|
+
}
|
|
129
|
+
|
|
96
130
|
/**
|
|
97
131
|
* Probe one catalogue entry.
|
|
98
132
|
*
|
|
@@ -182,7 +216,7 @@ export async function probeTarget(entry, opts = {}) {
|
|
|
182
216
|
models: discoveredModels.length,
|
|
183
217
|
note: res.ok ? `answered /models (${discoveredModels.length} model(s))` : `HTTP ${res.status}`,
|
|
184
218
|
};
|
|
185
|
-
});
|
|
219
|
+
}, timeoutMs + 250);
|
|
186
220
|
|
|
187
221
|
// ---- readiness ----------------------------------------------------------
|
|
188
222
|
// If liveness did not even reach the host, readiness cannot tell us anything
|
|
@@ -229,7 +263,7 @@ export async function probeTarget(entry, opts = {}) {
|
|
|
229
263
|
sample = content || (typeof reasoning === 'string' ? reasoning : '');
|
|
230
264
|
const ok = typeof sample === 'string' && sample.trim().length > 0;
|
|
231
265
|
return { ok, status: res.status, note: ok ? 'returned text' : 'empty completion' };
|
|
232
|
-
});
|
|
266
|
+
}, timeoutMs + 250);
|
|
233
267
|
|
|
234
268
|
if (!levels.readiness.ok) reasons.push(`readiness: ${levels.readiness.note}`);
|
|
235
269
|
|
|
@@ -330,7 +364,7 @@ async function probeStreaming({ fetchImpl, base, headers, entry, timeoutMs, mode
|
|
|
330
364
|
frames,
|
|
331
365
|
deltas,
|
|
332
366
|
};
|
|
333
|
-
});
|
|
367
|
+
}, timeoutMs + 250);
|
|
334
368
|
}
|
|
335
369
|
|
|
336
370
|
/** A JSON request has to come back as JSON, not as a promise to be JSON. */
|
|
@@ -353,7 +387,7 @@ async function probeJson({ fetchImpl, base, headers, entry, timeoutMs, model })
|
|
|
353
387
|
} catch {
|
|
354
388
|
return { ok: false, status: res.status, note: 'returned text that is not JSON' };
|
|
355
389
|
}
|
|
356
|
-
});
|
|
390
|
+
}, timeoutMs + 250);
|
|
357
391
|
}
|
|
358
392
|
|
|
359
393
|
/**
|
|
@@ -374,7 +408,7 @@ async function probeTools({ fetchImpl, base, headers, entry, timeoutMs, model })
|
|
|
374
408
|
const message = json?.choices?.[0]?.message;
|
|
375
409
|
const ok = Boolean(message && (Array.isArray(message.tool_calls) || typeof message.content === 'string'));
|
|
376
410
|
return { ok, status: res.status, note: ok ? 'tool schema accepted' : 'response had neither tool_calls nor content' };
|
|
377
|
-
});
|
|
411
|
+
}, timeoutMs + 250);
|
|
378
412
|
}
|
|
379
413
|
|
|
380
414
|
/**
|
|
@@ -397,10 +431,38 @@ function probeModel(entry, discovered = []) {
|
|
|
397
431
|
return 'default';
|
|
398
432
|
}
|
|
399
433
|
|
|
400
|
-
|
|
434
|
+
/**
|
|
435
|
+
* Race a promise against a hard deadline.
|
|
436
|
+
*
|
|
437
|
+
* `AbortSignal.timeout` does not interrupt a DNS lookup, so a network that drops
|
|
438
|
+
* packets instead of refusing them leaves the fetch pending past its own timeout.
|
|
439
|
+
* Measured here: `sui probe --timeout 2000` against a blocked network ran for more
|
|
440
|
+
* than five minutes because every entry's liveness call never settled. The abort
|
|
441
|
+
* is still what normally fires — this is the backstop for the case where it cannot.
|
|
442
|
+
*/
|
|
443
|
+
function withDeadline(promise, deadlineMs) {
|
|
444
|
+
if (!deadlineMs) return promise;
|
|
445
|
+
let timer;
|
|
446
|
+
const deadline = new Promise((_, reject) => {
|
|
447
|
+
timer = setTimeout(() => {
|
|
448
|
+
const err = new Error(`no answer in ${deadlineMs} ms`);
|
|
449
|
+
err.name = 'TimeoutError';
|
|
450
|
+
reject(err);
|
|
451
|
+
}, deadlineMs);
|
|
452
|
+
});
|
|
453
|
+
return Promise.race([promise, deadline]).finally(() => clearTimeout(timer));
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* Time a level, and never let it outlive its timeout.
|
|
458
|
+
*
|
|
459
|
+
* @param {() => Promise<any>} fn
|
|
460
|
+
* @param {number} [deadlineMs] a hard ceiling, slightly above the fetch's own timeout
|
|
461
|
+
*/
|
|
462
|
+
async function timed(fn, deadlineMs) {
|
|
401
463
|
const t0 = performance.now();
|
|
402
464
|
try {
|
|
403
|
-
const result = await fn();
|
|
465
|
+
const result = await withDeadline(fn(), deadlineMs);
|
|
404
466
|
return { ...result, ms: Math.round(performance.now() - t0) };
|
|
405
467
|
} catch (err) {
|
|
406
468
|
const e = /** @type {any} */ (err);
|
|
@@ -453,11 +515,12 @@ export class Prober {
|
|
|
453
515
|
* @param {{
|
|
454
516
|
* entries: any[], intervalMs?: number, failuresBeforeDisable?: number,
|
|
455
517
|
* fetchImpl?: typeof fetch, timeoutMs?: number, now?: () => number,
|
|
456
|
-
* onResult?: (result: any, state: any) => void
|
|
518
|
+
* concurrency?: number, onResult?: (result: any, state: any) => void
|
|
457
519
|
* }} opts
|
|
458
520
|
*/
|
|
459
521
|
constructor(opts) {
|
|
460
522
|
this.entries = opts.entries ?? [];
|
|
523
|
+
this.concurrency = Number(opts.concurrency ?? DEFAULT_PROBE_CONCURRENCY) || DEFAULT_PROBE_CONCURRENCY;
|
|
461
524
|
this.intervalMs = opts.intervalMs ?? 3600_000;
|
|
462
525
|
this.failuresBeforeDisable = opts.failuresBeforeDisable ?? 3;
|
|
463
526
|
this.fetchImpl = opts.fetchImpl ?? globalThis.fetch;
|
|
@@ -487,11 +550,11 @@ export class Prober {
|
|
|
487
550
|
this.runs = 0;
|
|
488
551
|
}
|
|
489
552
|
|
|
490
|
-
/** Probe everything once,
|
|
553
|
+
/** Probe everything once, `concurrency` at a time, and fold the results into state. */
|
|
491
554
|
async runOnce() {
|
|
492
555
|
this.runs += 1;
|
|
493
|
-
const results = await
|
|
494
|
-
|
|
556
|
+
const results = await mapPool(this.entries, this.concurrency, (entry) =>
|
|
557
|
+
probeTarget(entry, { fetchImpl: this.fetchImpl, timeoutMs: this.timeoutMs, now: this.now, allowLocal: this.allowLocal }),
|
|
495
558
|
);
|
|
496
559
|
|
|
497
560
|
for (const result of results) {
|