@drsamonline/router 0.12.9 → 0.12.11
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/explain.js +3 -0
- package/src/router.js +15 -0
package/package.json
CHANGED
package/src/explain.js
CHANGED
|
@@ -21,6 +21,9 @@ export function explainPlan(plan) {
|
|
|
21
21
|
if (combo.strategy === 'cheapest') lines.push('Targets are ordered by blended cost, after the quality floor.');
|
|
22
22
|
if (combo.strategy === 'weighted') lines.push('The first target is picked by weight; the rest are fallbacks in weight order.');
|
|
23
23
|
if (combo.strategy === 'latency') lines.push('Targets are ordered by measured p50 from the ledger.');
|
|
24
|
+
if (combo.strategy === 'quota-weighted') {
|
|
25
|
+
lines.push('Targets are ordered by remaining budget headroom divided by turns already answered today, so the target with the most left to give goes first.');
|
|
26
|
+
}
|
|
24
27
|
if (plan.learned) {
|
|
25
28
|
lines.push('Fallback learning is on for this combo: targets with enough recent samples are ordered by success rate, then latency, and the declared order breaks ties.');
|
|
26
29
|
}
|
package/src/router.js
CHANGED
|
@@ -283,6 +283,21 @@ export class Router {
|
|
|
283
283
|
ordered = [...usable].sort((a, b) => a.estimate - b.estimate || Number(b.model.quality ?? 0) - Number(a.model.quality ?? 0));
|
|
284
284
|
} else if (combo.strategy === 'latency') {
|
|
285
285
|
ordered = [...usable].sort((a, b) => this.#p50(a.modelId) - this.#p50(b.modelId));
|
|
286
|
+
} else if (combo.strategy === 'quota-weighted' && usable.length) {
|
|
287
|
+
// OmniRoute calls this the twentieth strategy; here it is the one that uses
|
|
288
|
+
// what this gateway actually measures. A provider at 90% of its cap has less
|
|
289
|
+
// to give than one at 10%, and a target already answering has less than one
|
|
290
|
+
// that is idle — so the score is headroom ÷ (1 + turns today).
|
|
291
|
+
const scored = usable.map((target, index) => {
|
|
292
|
+
const providerId = this.config.models?.[target.modelId]?.provider;
|
|
293
|
+
const cap = providerId ? this.config.budgets?.perProvider?.[providerId] : null;
|
|
294
|
+
const spent = providerId ? this.ledger.providerSpendToday(providerId) : 0;
|
|
295
|
+
const headroom = cap == null ? 1 : Math.max(0, 1 - spent / Math.max(Number(cap), 1e-9));
|
|
296
|
+
const turns = Number(this.ledger.turnsForModel?.(target.modelId) ?? 0);
|
|
297
|
+
return { target, index, score: headroom / (1 + turns) };
|
|
298
|
+
});
|
|
299
|
+
scored.sort((a, b) => b.score - a.score || a.index - b.index);
|
|
300
|
+
ordered = scored.map((x) => x.target);
|
|
286
301
|
} else if (combo.strategy === 'weighted' && usable.length) {
|
|
287
302
|
const primary = pickWeighted(usable, this.random);
|
|
288
303
|
ordered = [primary, ...usable.filter((t) => t.modelId !== primary.modelId).sort((a, b) => b.weight - a.weight)];
|