@drsamonline/prober 0.12.35 → 0.12.37
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 +83 -14
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -66,11 +66,13 @@ export const PROBE_FIXTURE_VERSION = '1.0.0';
|
|
|
66
66
|
export const FIXTURES = {
|
|
67
67
|
readiness: {
|
|
68
68
|
messages: [{ role: 'user', content: 'Reply with the single word: ready' }],
|
|
69
|
-
|
|
69
|
+
// A reasoning model thinks before it speaks. 8 tokens were spent entirely on
|
|
70
|
+
// thinking, so the reply was empty and a working provider was marked unusable.
|
|
71
|
+
max_tokens: 256,
|
|
70
72
|
},
|
|
71
73
|
jsonSchema: {
|
|
72
74
|
messages: [{ role: 'user', content: 'Return JSON with exactly one key "ok", set to true.' }],
|
|
73
|
-
max_tokens:
|
|
75
|
+
max_tokens: 256,
|
|
74
76
|
response_format: { type: 'json_object' },
|
|
75
77
|
},
|
|
76
78
|
tools: {
|
|
@@ -91,6 +93,40 @@ export const FIXTURES = {
|
|
|
91
93
|
|
|
92
94
|
const DEFAULT_TIMEOUT_MS = 8000;
|
|
93
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
|
+
|
|
94
130
|
/**
|
|
95
131
|
* Probe one catalogue entry.
|
|
96
132
|
*
|
|
@@ -180,7 +216,7 @@ export async function probeTarget(entry, opts = {}) {
|
|
|
180
216
|
models: discoveredModels.length,
|
|
181
217
|
note: res.ok ? `answered /models (${discoveredModels.length} model(s))` : `HTTP ${res.status}`,
|
|
182
218
|
};
|
|
183
|
-
});
|
|
219
|
+
}, timeoutMs + 250);
|
|
184
220
|
|
|
185
221
|
// ---- readiness ----------------------------------------------------------
|
|
186
222
|
// If liveness did not even reach the host, readiness cannot tell us anything
|
|
@@ -220,10 +256,14 @@ export async function probeTarget(entry, opts = {}) {
|
|
|
220
256
|
return { ok: false, status: res.status, note: `HTTP ${res.status}: ${firstLine(body)}` };
|
|
221
257
|
}
|
|
222
258
|
const json = await res.json().catch(() => null);
|
|
223
|
-
|
|
259
|
+
const message = json?.choices?.[0]?.message ?? {};
|
|
260
|
+
// a reasoning model leaves `content` empty and puts the answer in `reasoning`
|
|
261
|
+
const content = typeof message.content === 'string' ? message.content : '';
|
|
262
|
+
const reasoning = typeof message.reasoning_content === 'string' ? message.reasoning_content : message.reasoning;
|
|
263
|
+
sample = content || (typeof reasoning === 'string' ? reasoning : '');
|
|
224
264
|
const ok = typeof sample === 'string' && sample.trim().length > 0;
|
|
225
265
|
return { ok, status: res.status, note: ok ? 'returned text' : 'empty completion' };
|
|
226
|
-
});
|
|
266
|
+
}, timeoutMs + 250);
|
|
227
267
|
|
|
228
268
|
if (!levels.readiness.ok) reasons.push(`readiness: ${levels.readiness.note}`);
|
|
229
269
|
|
|
@@ -324,7 +364,7 @@ async function probeStreaming({ fetchImpl, base, headers, entry, timeoutMs, mode
|
|
|
324
364
|
frames,
|
|
325
365
|
deltas,
|
|
326
366
|
};
|
|
327
|
-
});
|
|
367
|
+
}, timeoutMs + 250);
|
|
328
368
|
}
|
|
329
369
|
|
|
330
370
|
/** A JSON request has to come back as JSON, not as a promise to be JSON. */
|
|
@@ -347,7 +387,7 @@ async function probeJson({ fetchImpl, base, headers, entry, timeoutMs, model })
|
|
|
347
387
|
} catch {
|
|
348
388
|
return { ok: false, status: res.status, note: 'returned text that is not JSON' };
|
|
349
389
|
}
|
|
350
|
-
});
|
|
390
|
+
}, timeoutMs + 250);
|
|
351
391
|
}
|
|
352
392
|
|
|
353
393
|
/**
|
|
@@ -368,7 +408,7 @@ async function probeTools({ fetchImpl, base, headers, entry, timeoutMs, model })
|
|
|
368
408
|
const message = json?.choices?.[0]?.message;
|
|
369
409
|
const ok = Boolean(message && (Array.isArray(message.tool_calls) || typeof message.content === 'string'));
|
|
370
410
|
return { ok, status: res.status, note: ok ? 'tool schema accepted' : 'response had neither tool_calls nor content' };
|
|
371
|
-
});
|
|
411
|
+
}, timeoutMs + 250);
|
|
372
412
|
}
|
|
373
413
|
|
|
374
414
|
/**
|
|
@@ -391,10 +431,38 @@ function probeModel(entry, discovered = []) {
|
|
|
391
431
|
return 'default';
|
|
392
432
|
}
|
|
393
433
|
|
|
394
|
-
|
|
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) {
|
|
395
463
|
const t0 = performance.now();
|
|
396
464
|
try {
|
|
397
|
-
const result = await fn();
|
|
465
|
+
const result = await withDeadline(fn(), deadlineMs);
|
|
398
466
|
return { ...result, ms: Math.round(performance.now() - t0) };
|
|
399
467
|
} catch (err) {
|
|
400
468
|
const e = /** @type {any} */ (err);
|
|
@@ -447,11 +515,12 @@ export class Prober {
|
|
|
447
515
|
* @param {{
|
|
448
516
|
* entries: any[], intervalMs?: number, failuresBeforeDisable?: number,
|
|
449
517
|
* fetchImpl?: typeof fetch, timeoutMs?: number, now?: () => number,
|
|
450
|
-
* onResult?: (result: any, state: any) => void
|
|
518
|
+
* concurrency?: number, onResult?: (result: any, state: any) => void
|
|
451
519
|
* }} opts
|
|
452
520
|
*/
|
|
453
521
|
constructor(opts) {
|
|
454
522
|
this.entries = opts.entries ?? [];
|
|
523
|
+
this.concurrency = Number(opts.concurrency ?? DEFAULT_PROBE_CONCURRENCY) || DEFAULT_PROBE_CONCURRENCY;
|
|
455
524
|
this.intervalMs = opts.intervalMs ?? 3600_000;
|
|
456
525
|
this.failuresBeforeDisable = opts.failuresBeforeDisable ?? 3;
|
|
457
526
|
this.fetchImpl = opts.fetchImpl ?? globalThis.fetch;
|
|
@@ -481,11 +550,11 @@ export class Prober {
|
|
|
481
550
|
this.runs = 0;
|
|
482
551
|
}
|
|
483
552
|
|
|
484
|
-
/** Probe everything once,
|
|
553
|
+
/** Probe everything once, `concurrency` at a time, and fold the results into state. */
|
|
485
554
|
async runOnce() {
|
|
486
555
|
this.runs += 1;
|
|
487
|
-
const results = await
|
|
488
|
-
|
|
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 }),
|
|
489
558
|
);
|
|
490
559
|
|
|
491
560
|
for (const result of results) {
|