-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathportfolio.ts
More file actions
1207 lines (1184 loc) · 50.9 KB
/
Copy pathportfolio.ts
File metadata and controls
1207 lines (1184 loc) · 50.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* V3 portfolio router.
*
* This is deliberately local and deterministic: feature extraction, eligibility
* checks and scoring read only request data plus the in-process model registry.
* It is therefore safe for the hot path and provides a stable baseline for the
* RouterBench evaluation before health telemetry / an optional judge are added.
*/
import { DEFAULT_MODEL_CAPABILITIES } from "./model-capabilities.js";
import { getFallbackChain, selectModel } from "./selector.js";
import { RulesStrategy } from "./strategy.js";
import {
HISTORICAL_MODEL_PROFILES,
LIVE_MODEL_PROFILES,
type ModelPerformanceProfile,
} from "./model-profiles.js";
import type { RouterOptions, RouterStrategy, TaskType, Tier, TierConfig } from "./types.js";
import { inferToolRequirement } from "./tool-intent.js";
type TaskFeatures = {
taskType: TaskType;
estimatedInputTokens: number;
hasCode: boolean;
needsTools: boolean;
toolsAvailable: boolean;
needsVision: boolean;
needsStructuredOutput: boolean;
latencySensitive: boolean;
highStakes: boolean;
language: "zh" | "other";
likelyParallelToolCalls: boolean;
complexMultiToolPlan: boolean;
agentDomain: "airline" | "retail" | "web_research" | "other";
deepWebResearch: boolean;
agentRisk: "standard" | "high" | "complex_high" | "policy_exception_simple" | "policy_exception";
terminalToolSignal: boolean;
terminalSafetySensitive: boolean;
implicitTerminalCode: boolean;
};
const DEFAULT_PORTFOLIO_WEIGHTS = {
auto: {
quality: 0.47,
capability: 0.2,
cost: 0.18,
speed: 0.07,
reliability: 0.03,
legacy: 0.05,
},
eco: { quality: 0.36, capability: 0.2, cost: 0.28, speed: 0.1, reliability: 0.04, legacy: 0.02 },
premium: {
quality: 0.58,
capability: 0.2,
cost: 0.08,
speed: 0.06,
reliability: 0.06,
legacy: 0.02,
},
highStakesBoost: { quality: 0.08, reliability: 0.05 },
latencySensitiveSpeedBoost: 0.08,
affinityFloorGap: { auto: 0.1, eco: 0.22, premium: 0.05 },
} as const;
/**
* Detect turns that probably need several tool calls. This is a
* deliberately conservative request-side feature: it uses only the prompt
* and visible tool count, never benchmark categories or expected answers.
*/
function likelyNeedsParallelToolCalls(
prompt: string,
needsTools: boolean,
toolCount: number | undefined,
toolNames: readonly string[] | undefined,
): boolean {
if (!needsTools || toolCount === undefined || toolCount < 1) return false;
const text = prompt.trim();
const explicitRepeat =
/\b(?:in parallel|simultaneously|concurrently|for each|each of|every one|both|(?:two|three|multiple|several)\s+(?:cities|locations|items|tasks|orders|users|files))\b|并行|同时|分别|每个|各自|(?:两个|三个|多个)(?:城市|地点|项目|任务|订单|用户|文件)|cada uno|para cada|simult[aá]neamente/i.test(
text,
);
if (explicitRepeat) return true;
const sentenceClauses = text
.split(/[.!?。!?]+/)
.map((part) => part.trim())
.filter((part) => part.length >= 8);
if (
(/\b(?:also|additionally|furthermore)\b|另外|此外|그리고/i.test(text) &&
sentenceClauses.length >= 2) ||
/\band\s+(?:also|for the)\b/i.test(text)
)
return true;
const pairedQuantity =
/\b\d+(?:\.\d+)?\s+(?:and|or)\s+\d+(?:\.\d+)?\s*(?:gb|mb|tb|kg|g|ml|oz|cups?|cores?|cpus?)\b/i.test(
text,
);
if (pairedQuantity) return true;
// Distinctive tokens from two visible tool names are a strong local signal
// for a multi-operation turn (for example add_task + delete_task).
const operationTokens = new Set([
"add",
"delete",
"remove",
"cancel",
"return",
"exchange",
"modify",
"book",
"transfer",
"send",
"upload",
"download",
"create",
"close",
]);
const lowered = text.toLowerCase();
const matchedOperationTokens = new Set(
(toolNames ?? [])
.flatMap((name) => name.toLowerCase().split(/[^a-z0-9\u3400-\u9fff]+/))
.filter((token) => operationTokens.has(token) && lowered.includes(token)),
);
// A single workflow naturally mentions domain nouns like order/item plus
// one action. Upgrade only when two different visible operation verbs are
// requested (for example cancel + book or add + delete).
if (matchedOperationTokens.size >= 2) return true;
// Repeated food/logging entries are commonly expressed as several lines,
// each with its own quantity rather than an explicit "for each" phrase.
const nonEmptyLines = text
.split(/\r?\n/)
.map((line) => line.trim())
.filter(Boolean);
const quantityMentions =
text.match(
/\b(?:\d+(?:\.\d+)?|one|two|three|four|five|six|seven|eight|nine|ten)\s*(?:oz|ounce|ounces|g|gram|grams|kg|ml|cups?|pieces?|tablespoons?)\b/gi,
) ?? [];
if (nonEmptyLines.length >= 2 && quantityMentions.length >= 2) return true;
// Weather prompts provide a useful language-independent high-confidence
// pattern: a single lookup tool plus multiple locations joined in one turn.
const repeatedLookup =
/\b(?:weather|climate|clima|tiempo|temperature|snow|news|report)\b|天气|气象|温度|降雪|新闻|报告/i.test(
text,
);
const multiLocationConnector = /\b(?:and also|both|y|e)\b|还有|以及|和|、/i.test(text);
const commaSeparatedLocations = (text.match(/[,,]/g) ?? []).length >= 2;
if (repeatedLookup && (multiLocationConnector || commaSeparatedLocations)) return true;
const distinctOrderParts =
/\b(?:food|meal)\b[\s\S]*\bdrink\b|\bdrink\b[\s\S]*\b(?:food|meal)\b/i.test(text);
const koreanParallelClauses = (text.match(/,/g) ?? []).length >= 3 && /하고|그리고/.test(text);
return distinctOrderParts || koreanParallelClauses;
}
function classifyTask(
prompt: string,
systemPrompt: string | undefined,
options: RouterOptions,
): TaskFeatures {
const fullText = `${systemPrompt ?? ""} ${prompt}`;
const estimatedInputTokens = Math.ceil(fullText.length / 4);
// Feature regexes need request shape and intent, not the entire document.
// Sample both ends so a long pasted artifact keeps the task instruction at
// either boundary, while the full length still drives capacity decisions.
const scanLimit = Math.max(1, Math.min(8_000, options.config.classifier.promptTruncationChars));
const sample = (value: string): string => {
if (value.length <= scanLimit) return value;
const prefixLength = Math.ceil(scanLimit / 2);
return `${value.slice(0, prefixLength)}\n${value.slice(-(scanLimit - prefixLength))}`;
};
const scannedPrompt = sample(prompt);
const scannedSystemPrompt = sample(systemPrompt ?? "");
const scannedFullText = `${scannedSystemPrompt} ${scannedPrompt}`;
const text = scannedPrompt.toLowerCase();
const explicitCodeSignal =
/```|\b(?:typescript|javascript|python|rust|java|sql|stack trace|traceback|exception)\b|\.(?:ts|tsx|js|py|go|rs)\b/i.test(
scannedPrompt,
);
// `class` is common in non-code Agent domains (for example airline cabin
// class). Treat code constructs as code only when the prompt also contains
// an implementation/editing cue, instead of letting a single ambiguous
// noun redirect an entire tool session to the code-agent portfolio.
const codeConstructSignal =
/\b(?:implement|refactor|debug|write|edit|modify|create|define|review|fix)\b[\s\S]{0,48}\b(?:api|function|class|method)\b|\b(?:api|function|class|method)\b[\s\S]{0,48}\b(?:code|implementation|typescript|javascript|python|rust|java)\b/i.test(
scannedPrompt,
);
const nativeCodeSignal =
/\b(?:programmed|written|implemented?|code)\s+(?:in|using)\s+(?:c\+\+|c|rust|go)\b/i.test(
scannedPrompt,
);
const hasCode = explicitCodeSignal || codeConstructSignal || nativeCodeSignal;
const toolsAvailable = options.hasTools ?? false;
const needsTools =
options.requiresTools ??
(toolsAvailable && inferToolRequirement(scannedPrompt, scannedSystemPrompt));
const likelyParallelToolCalls = likelyNeedsParallelToolCalls(
scannedPrompt,
needsTools,
options.toolCount,
options.toolNames,
);
const normalizedToolNames = (options.toolNames ?? []).map((name) => name.toLowerCase());
const airlineToolSignal = normalizedToolNames.some((name) =>
/(?:flight|reservation|airport|baggage|passenger)/.test(name),
);
const retailToolSignal = normalizedToolNames.some((name) =>
/(?:order|product|item|return|exchange|address)/.test(name),
);
const webResearchToolSignal = normalizedToolNames.some((name) =>
/^(?:web_?search|web_?fetch)$/.test(name),
);
const agentDomain =
airlineToolSignal && !retailToolSignal
? "airline"
: retailToolSignal && !airlineToolSignal
? "retail"
: webResearchToolSignal
? "web_research"
: "other";
// Distinguish a cheap lookup from a BrowseComp-like investigation. These
// prompts require joining several clues, resolving an entity, and ending in
// one exact answer; complete agent trajectories show that treating them as
// ordinary search causes long, costly loops. This is request/tool-surface
// evidence only and does not depend on a benchmark id or hidden answer.
const clueConnectors =
scannedFullText.match(
/\b(?:after|before|while|where|whose|which|in \d{4}|as of|over \d+|another|also|furthermore)\b|(?:之后|之前|其中|截至|超过|另一个|此外)/gi,
) ?? [];
const entityResolutionSignal =
/\b(?:identify|who (?:is|was)|what (?:is|was) the name|which (?:person|player|company|country|city)|find the (?:person|player|name|entity))\b|(?:找出|识别|是谁|哪位|名称是什么)/i.test(
scannedFullText,
);
const exactAnswerSignal =
/\b(?:exact answer|single best-supported answer|following clues|multiple public sources)\b|(?:精确答案|根据.*线索|多个公开来源)/i.test(
scannedFullText,
);
const deepWebResearch =
agentDomain === "web_research" &&
(exactAnswerSignal ||
(entityResolutionSignal && (clueConnectors.length >= 3 || prompt.length >= 320)));
const globalOptimizationSignal =
/\b(?:cheapest|lowest[- ]price|least expensive|most expensive|highest(?:[- ]priced)?|largest|smallest|maximum|minimum|best available|closest|not (?:cost|exceed))\b|最便宜|最低价|最贵|最高价|最大|最小/i.test(
scannedPrompt,
);
const globalScopeSignal =
/\b(?:everything|all (?:(?:my|your|their|the) )?(?:future |upcoming )?(?:items|orders|passengers|flights|reservations|bookings)|every (?:item|order|passenger|flight|reservation|booking))\b|全部|所有|每个/i.test(
scannedPrompt,
);
const globalChoiceSignal = globalOptimizationSignal || globalScopeSignal;
const crossRecordSignal =
/\b(?:another|other|different|previous)\s+(?:order|reservation|booking|account|address)\b|另一(?:个)?(?:订单|预订|账户|地址)|其他(?:订单|预订|账户|地址)/i.test(
scannedPrompt,
);
const reservationIds = scannedPrompt.match(/\b[A-Z0-9]{6}\b/g) ?? [];
const crossReservationBatchSignal =
agentDomain === "airline" &&
(/\b(?:two|three|multiple|several)(?:\s+of\s+(?:my|our|the))?\s+(?:upcoming\s+)?(?:reservations?|bookings?)\b|\b(?:a\s+)?(?:second|third)\s+(?:reservation|booking)\b/i.test(
scannedPrompt,
) ||
new Set(reservationIds).size >= 2);
const conditionalGlobalWorkflowSignal =
agentDomain === "airline" &&
globalScopeSignal &&
/\b(?:if|that (?:contain|have)|longer than|shorter than|under|over|at (?:most|least)|wherever possible)\b|如果|超过|少于|不超过|尽可能/i.test(
scannedPrompt,
) &&
/\b(?:cancel|change|upgrade|move|book)\b[\s\S]*\b(?:cancel|change|upgrade|move|book)\b|取消[\s\S]*(?:升级|更改)|升级[\s\S]*(?:取消|更改)/i.test(
scannedPrompt,
);
// A refund explicitly targeted at a named/non-original card can conflict
// with account state and require escalation rather than a substitute action.
// This narrow feature is visible on the first turn and avoids sending every
// ordinary return workflow to the expensive policy specialist.
const policyExceptionSignal =
agentDomain === "retail" &&
/\b(?:return|refund|send back|get (?:my |the )?money back)\b|退货|退款|退回/i.test(
scannedPrompt,
) &&
/\b(?:amex|american express|visa|mastercard|credit card|debit card|different card|another card|other card)\b|信用卡|借记卡|其他卡|另一张卡/i.test(
scannedPrompt,
);
// A comparative selector can mention two products while requesting only
// one write (for example "send back the pricier one"). Three-repeat tau2
// calibration found no quality gain from the policy specialist on these
// single-write cases, so keep them in a distinct, lower-cost risk band.
const singleSelectedPolicyException =
policyExceptionSignal &&
/\b(?:return|refund|send back)\b[^.!?。!?]{0,96}\b(?:the )?(?:pricier|cheaper|more expensive|less expensive|costlier|one)\b/i.test(
scannedPrompt,
);
// Returns and exchanges often pivot after confirmation (return -> rethink
// -> exchange -> choose a variant). That future state is not visible to a
// task-start router, so treat the observable workflow verb as the risk cue.
// Simpler cancellation and one-field order edits stay on the standard path.
const negotiatedWorkflowSignal =
agentDomain === "retail" && /\b(?:return|exchange)\b|退货|退回|换货|交换/i.test(scannedPrompt);
const numberedSteps = (scannedPrompt.match(/(?:^|\s)\d+(?:\.\d+)*[.)]\s+/g) ?? []).length;
const complexMultiToolPlan =
likelyParallelToolCalls &&
((options.toolCount ?? 0) >= 6 || numberedSteps >= 3 || prompt.length > 1_200);
let agentRisk: TaskFeatures["agentRisk"] =
needsTools && singleSelectedPolicyException
? "policy_exception_simple"
: needsTools && policyExceptionSignal
? "policy_exception"
: // Airline prompts that require a global optimum (for example the
// cheapest itinerary across several candidates) are materially harder
// than applying one change to every passenger in a known reservation.
// Full-session evidence supports Sonnet for the former, while upgrading
// the latter merely because it says "all passengers" caused a large cost
// increase without a quality gain.
needsTools &&
agentDomain === "airline" &&
(globalOptimizationSignal || conditionalGlobalWorkflowSignal)
? "complex_high"
: needsTools &&
(likelyParallelToolCalls ||
globalChoiceSignal ||
crossRecordSignal ||
crossReservationBatchSignal ||
negotiatedWorkflowSignal)
? "high"
: "standard";
const needsVision = options.hasVision ?? false;
const needsStructuredOutput = options.requiresStructuredOutput ?? false;
const latencySensitive =
/\b(?:urgent|asap|fast|quick|low latency|real[- ]time)\b|尽快|马上|快速|低延迟/i.test(
scannedFullText,
);
const highStakes =
/\b(?:production|security|payment|legal|medical|financial|audit)\b|生产|安全|支付|法律|医疗|财务|审计/i.test(
scannedFullText,
);
// Terminal tasks often describe the desired artifact rather than naming a
// programming language. Treat only small, deterministic local build/file
// work as implicit code. Operational deployment, credentials, destructive
// work, evaluation, vision, and broad search stay on the stronger generic
// tool-agent path. This is a request-side feature, not a benchmark ID list.
const terminalToolSignal = normalizedToolNames.some((name) =>
/^(?:terminalexec|terminalinspect|terminalsendkeys)$/.test(name),
);
const simpleTerminalArtifact =
/\b(?:create|write|convert|generate|build|implement|run|fix|repair|debug|make)\b[\s\S]{0,120}\b(?:file|script|csv|parquet|json|txt|server|endpoint)\b/i.test(
scannedPrompt,
);
// Multi-file repair is qualitatively different from fixing one known local
// script. The agent must preserve state across inspections, infer ordering
// and dependencies, edit several artifacts, and close the loop with tests.
// A frozen Terminal-Bench trajectory showed the economy model spending 27
// turns repeatedly rereading files without making an edit, while Opus solved
// the same task in eight turns. Promote this request-visible pattern before
// model scoring; no benchmark ID or expected answer is consulted.
const terminalComplexRepair =
terminalToolSignal &&
/\b(?:multiple|several)\s+(?:scripts?|files?|components?)\b|\b(?:pipeline|dependencies)\b[\s\S]{0,100}\b(?:fail|issue|fix|repair|run|execute)\b|\b(?:identify|find|fix|repair)\s+(?:and\s+)?(?:fix\s+)?all\s+(?:the\s+)?issues\b/i.test(
scannedPrompt,
);
// One artifact that must be accepted by multiple compilers/runtimes is not
// a routine file-writing task. It requires reasoning across incompatible
// grammars and validating every execution path; cheap code models can
// produce plausible-looking source that satisfies neither toolchain.
const mentionedTerminalRuntimes = new Set(
(scannedPrompt.match(/\b(?:gcc|clang|rustc|javac|go\s+build|node|python)\b/gi) ?? []).map(
(name) => name.toLowerCase().replace(/\s+/g, " "),
),
);
const terminalCrossRuntimeArtifact =
terminalToolSignal &&
(/\bpolyglot\b/i.test(scannedPrompt) ||
/\b(?:both|each)\b[\s\S]{0,120}\b(?:compilers?|runtimes?|toolchains?)\b/i.test(
scannedPrompt,
) ||
(mentionedTerminalRuntimes.size >= 2 &&
/\b(?:compile|build|run|execute)\b/i.test(scannedPrompt)));
// Framework-to-native ports combine binary checkpoint inspection, weight
// export, tensor-layout reasoning, image/data decoding, and a separately
// compiled runtime. A task-start router can see this boundary directly in
// the request (for example PyTorch state_dict -> a pure C CLI); treating it
// like routine single-file C work caused a 30-turn read/retry loop in a
// frozen official Terminal-Bench trajectory.
const terminalFrameworkToNativeArtifact =
terminalToolSignal &&
/\b(?:pytorch|tensorflow|jax|onnx|state[_ -]?dict|checkpoint|safetensors?)\b|\.(?:pth|pt|onnx)\b/i.test(
scannedPrompt,
) &&
/\b(?:pure|native|programmed|written|implemented?)\s+(?:in|using)\s+(?:c\+\+|c|rust|go)\b|\b(?:c\+\+|c|rust|go)\s+(?:program|binary|executable|cli|tool|implementation)\b/i.test(
scannedPrompt,
) &&
/\b(?:inference|model|weights?|tensor|export|convert|load)\b/i.test(scannedPrompt);
if (
needsTools &&
(terminalComplexRepair || terminalCrossRuntimeArtifact || terminalFrameworkToNativeArtifact) &&
(agentRisk === "standard" || agentRisk === "high")
)
agentRisk = "complex_high";
const complexTerminalOperation =
/\b(?:git|ssh|nginx|https|certificate|authentication|credential|deploy|production|encrypt|gpg|shred|securely delete|decommission|benchmark|evaluate|embedding|chess|image|search the web|schema|statistical|statistics|aggregate|join|multiple inputs?)\b/i.test(
scannedPrompt,
);
// A bare "token" is not a credential signal: blockchain, tokenizer, and
// LLM tasks use that word routinely (for example "token transfers"). Only
// treat it as sensitive when the prompt gives it an authentication/secret
// qualifier. API keys remain an unambiguous high-risk signal on their own.
const terminalCredentialSignal =
/\b(?:ssh|nginx|certificate|authentication|credentials?|passwords?|api keys?|deploy|production|encrypt|gpg|shred|securely delete|decommission)\b/i.test(
scannedPrompt,
) ||
/\b(?:access|auth|authentication|bearer|secret|api)\s+tokens?\b|\btokens?\s+(?:secret|credential|authentication)\b/i.test(
scannedPrompt,
);
const terminalSafetySensitive = terminalToolSignal && (highStakes || terminalCredentialSignal);
const implicitTerminalCode =
needsTools &&
terminalToolSignal &&
agentRisk === "standard" &&
!highStakes &&
!complexTerminalOperation &&
numberedSteps < 3 &&
prompt.length <= 1_000 &&
simpleTerminalArtifact;
const language = /[\u3400-\u9fff]/.test(scannedFullText) ? "zh" : "other";
const multipleChoiceSignals = (scannedPrompt.match(/(?:^|\n)\s*[A-D][.)]\s+/gim) ?? []).length;
const numericSignals = (scannedPrompt.match(/-?\d+(?:[.,]\d+)?/g) ?? []).length;
const compactMathProblem =
!hasCode &&
prompt.length < 2_500 &&
numericSignals >= 2 &&
(/[+×÷=%$€£¥]|\b(?:total|each|per|times|half|twice|percent|how many|how much|calculate)\b/i.test(
scannedPrompt,
) ||
/[??]\s*$/.test(scannedPrompt.trim()) ||
numericSignals >= 3);
let taskType: TaskType = "chat";
if (needsVision) taskType = "vision";
else if (estimatedInputTokens > 80_000) taskType = "long_context";
else if (needsTools && (hasCode || implicitTerminalCode)) taskType = "code_agent";
else if (needsTools && likelyParallelToolCalls && !complexMultiToolPlan)
taskType = "tool_agent_parallel";
else if (needsTools) taskType = "tool_agent";
else if (multipleChoiceSignals >= 3) taskType = "reasoning_mcq";
else if (compactMathProblem) taskType = "reasoning_math";
else if (
/\b(?:bug|debug|error|failure|failing|regression|crash|修复|报错|错误|调试)\b/i.test(text)
)
taskType = "debug";
else if (hasCode || /\b(?:refactor|implement|patch|edit|rewrite|重构|实现|修改)\b/i.test(text))
taskType = "code_edit";
else if (needsStructuredOutput || /\b(?:extract|json|schema|csv|字段|提取)\b/i.test(text))
taskType = "extraction";
else if (
/\b(?:prove|derive|theorem|formal|mathematical|reasoning|证明|推导|定理|数学)\b/i.test(text)
)
taskType = "reasoning";
return {
taskType,
estimatedInputTokens,
hasCode,
needsTools,
toolsAvailable,
needsVision,
needsStructuredOutput,
latencySensitive,
highStakes,
language,
likelyParallelToolCalls,
complexMultiToolPlan,
agentDomain,
deepWebResearch,
agentRisk,
terminalToolSignal,
terminalSafetySensitive,
implicitTerminalCode,
};
}
function affinity(
modelId: string,
task: TaskType,
language: TaskFeatures["language"] = "other",
agentDomain: TaskFeatures["agentDomain"] = "other",
deepWebResearch = false,
agentRisk: TaskFeatures["agentRisk"] = "standard",
terminalToolSignal = false,
terminalSafetySensitive = false,
): number {
const id = modelId.toLowerCase();
// Model family names are intentionally similar (for example
// `gemini-2.5-flash` vs `gemini-2.5-flash-lite`). A substring match lets a
// smaller sibling inherit a capability claim that was measured only for the
// flagship. Keep these assignments model-exact; a sibling can be added only
// with its own evidence.
const modelName = id.slice(id.indexOf("/") + 1);
const match = (values: string[], score: number) =>
values.some((value) => modelName === value) ? score : 0;
const base = 0.68;
switch (task) {
case "code_agent":
if (terminalToolSignal && agentRisk === "complex_high") {
// Strong native tool loop until the Responses function-output fix is
// deployed on both gateways; keep Codex available below the floor.
return Math.max(
base,
match(["claude-sonnet-5"], 1),
match(["gpt-5.3-codex"], 0.87),
match(["gpt-5-mini"], 0.78),
match(["gemini-3.5-flash"], 0.76),
);
}
// Seven valid full agent + official Terminal-Bench trajectories
// (2026-07-28) gave GPT-5 Mini 4/7 resolved tasks versus 1/7 for the
// prior dynamic code-agent choice. Its token-normalized total cost was
// higher in this small calibration, so keep Codex and Sonnet's quality
// priors above it; admitting Mini to the scoring band lets the normal
// quality/cost profile choose it without erasing stronger fallbacks.
// DeepSeek V4 Pro is kept below the primary band after two consecutive
// mid-trajectory provider timeouts contaminated its calibration runs.
return Math.max(
base,
match(["gpt-5.3-codex"], 1),
match(["claude-sonnet-5"], 0.98),
match(["gpt-5-mini"], 0.96),
match(["gemini-3.5-flash"], 0.92),
match(["kimi-k3"], 0.9),
match(["deepseek-v4-pro", "glm-5.2"], 0.88),
);
case "tool_agent":
if (terminalToolSignal && agentRisk === "complex_high") {
// Keep the Responses-API Codex path outside auto's affinity floor
// until the gateway fix that preserves function_call_output is live
// on both chains. Sonnet has a verified native multi-turn tool loop
// and is the safe strong default for this band today.
return Math.max(
base,
match(["claude-sonnet-5"], 1),
match(["gpt-5.3-codex"], 0.87),
match(["gpt-5-mini"], 0.78),
match(["gemini-3.5-flash"], 0.76),
);
}
if (terminalToolSignal && !terminalSafetySensitive) {
// Seven official Terminal-Bench calibration trajectories favoured
// GPT-5 Mini over the prior dynamic choice. Admit Codex/Sonnet as
// close fallbacks, but let actual request cost break the tie.
return Math.max(
base,
match(["gpt-5-mini"], 1),
match(["gpt-5.3-codex"], 0.98),
match(["claude-sonnet-5"], 0.9),
match(["gemini-3.5-flash"], 0.89),
);
}
if (terminalToolSignal && terminalSafetySensitive) {
// Two complete agent observations on the public
// Terminal-Bench new-encrypt-command task ended in Codex repeating
// the same TerminalExec input until the loop guard fired. Keep Codex
// as an availability fallback, but below the safety-band affinity
// floor until its Responses function-output path is verified on both
// gateways. This does not change explicit code-agent routing.
return Math.max(
base,
match(["claude-sonnet-5"], 1),
match(["claude-opus-4.8"], 0.9),
match(["gpt-5.3-codex"], 0.84),
);
}
if (agentDomain === "web_research") {
// Complete-session BrowseComp calibration supersedes the earlier
// single-case Opus promotion: strict deduplicated evidence has Sonnet
// 5 at 2/9 versus Opus 5 at 0/3, while Opus also costs more and has a
// much longer tail. Keep Opus as an availability fallback until a
// larger stable-provider sample supports promotion.
return deepWebResearch
? Math.max(
base,
match(["claude-sonnet-5"], 1),
match(["gpt-5-mini"], 0.88),
match(["gemini-3.5-flash"], 0.84),
match(["claude-opus-5"], 0.8),
match(["claude-opus-4.8"], 0.78),
)
: Math.max(
base,
match(["claude-sonnet-5"], 1),
match(["gpt-5-mini"], 0.88),
match(["gemini-3.5-flash"], 0.86),
match(["claude-opus-5"], 0.84),
match(["claude-opus-4.8"], 0.82),
);
}
// Full-trajectory tau2 calibration (2026-07-28, official gpt-4.1
// simulator): Sonnet 5 completed both an airline policy task and a
// retail multi-write task with reward 1.0. Gemini 3.5 Flash emitted
// function calls as plain text after the first structured calls and
// looped to the 100-step ceiling on the airline task. Keep only the
// trajectory-validated primary inside the scoring band; the remaining
// eligible models are retained below as availability fallbacks.
if (agentDomain === "retail") {
// Full-session calibration: GPT-5 Mini completed two local/single
// retail workflows at a fraction of Sonnet's token cost. It remains
// ineligible for promotion when the prompt asks for multiple actions,
// cross-record discovery, or a global optimum; those trajectories
// exposed unstable write arguments in prior calibration. DeepSeek V4
// Pro completed all three high-risk retail calibration trajectories
// that included global-choice, cross-record, and multi-write behavior;
// Sonnet completed one of the same three.
if (agentRisk === "standard") {
return Math.max(
base,
match(["gpt-5-mini"], 1),
match(["claude-sonnet-5"], 0.88),
match(["gemini-3.5-flash"], 0.82),
match(["gpt-5.3-codex"], 0.81),
match(["kimi-k3"], 0.78),
match(["deepseek-v4-pro"], 0.76),
);
}
if (agentRisk === "policy_exception") {
return Math.max(
base,
match(["gpt-4.1"], 1),
match(["claude-sonnet-5"], 0.9),
match(["deepseek-v4-pro"], 0.82),
match(["gpt-5-mini"], 0.8),
match(["gpt-4o-mini"], 0.76),
);
}
if (agentRisk === "policy_exception_simple") {
return Math.max(
base,
match(["gpt-5-mini"], 1),
match(["gpt-4.1"], 0.86),
match(["deepseek-v4-pro"], 0.82),
match(["gpt-4o-mini"], 0.8),
);
}
return Math.max(
base,
match(["deepseek-v4-pro"], 1),
match(["claude-sonnet-5"], 0.88),
match(["gemini-3.5-flash"], 0.82),
match(["gpt-5.3-codex"], 0.81),
match(["kimi-k3"], 0.78),
match(["gpt-5-mini"], 0.76),
);
}
// Standard airline workflows stay on GPT-5 Mini: six full-session
// development trajectories gave it the same 5/6 success as Sonnet at
// roughly one order of magnitude lower normalized token cost. A held-out
// high-risk cabin/date negotiation then produced a persistent empty
// assistant turn on Mini even after a semantic retry, while Sonnet 5
// completed the identical official tau2 trajectory with reward 1.0.
// Promote only global optimization / conditional-global work. A known
// batch of reservations is operationally high-risk but still a
// structured tool workflow: current Tau evidence has Mini and Sonnet at
// equal task reward there, while Sonnet costs roughly two orders of
// magnitude more on the long-tail case.
if (agentDomain === "airline") {
if (agentRisk === "complex_high") {
return Math.max(
base,
match(["claude-sonnet-5"], 1),
match(["gpt-5-mini"], 0.78),
match(["gemini-3.5-flash"], 0.76),
match(["deepseek-v4-pro"], 0.74),
);
}
return Math.max(
base,
match(["gpt-5-mini"], 1),
match(["claude-sonnet-5"], 0.9),
match(["gemini-3.5-flash"], 0.8),
match(["deepseek-v4-pro"], 0.76),
);
}
return Math.max(
base,
match(["claude-sonnet-5"], 1),
match(["gemini-3.5-flash"], 0.88),
match(["gpt-5.3-codex"], 0.87),
match(["gpt-5-mini"], 0.84),
match(["kimi-k3"], 0.85),
match(["deepseek-v4-pro"], 0.82),
);
case "tool_agent_parallel":
if (terminalToolSignal) {
// Multi-file Terminal work is not equivalent to a one-turn parallel
// function-call benchmark. Sonnet is the strongest trajectory-tested
// cost-controlled default; Opus remains a close safety fallback.
return terminalSafetySensitive
? Math.max(
base,
match(["claude-sonnet-5"], 1),
match(["claude-opus-4.8"], 0.9),
match(["gpt-5.3-codex"], 0.86),
)
: Math.max(
base,
match(["gpt-5-mini"], 1),
match(["gpt-5.3-codex"], 0.98),
match(["claude-sonnet-5"], 0.92),
match(["gemini-3.5-flash"], 0.88),
);
}
if (agentDomain === "web_research") {
return deepWebResearch
? Math.max(
base,
match(["claude-sonnet-5"], 1),
match(["gpt-5-mini"], 0.88),
match(["gemini-3.5-flash"], 0.84),
match(["claude-opus-5"], 0.8),
match(["claude-opus-4.8"], 0.78),
)
: Math.max(
base,
match(["claude-sonnet-5"], 1),
match(["gpt-5-mini"], 0.88),
match(["gemini-3.5-flash"], 0.86),
match(["claude-opus-5"], 0.84),
match(["claude-opus-4.8"], 0.82),
);
}
if (agentDomain === "retail") {
if (agentRisk === "policy_exception") {
return Math.max(
base,
match(["gpt-4.1"], 1),
match(["claude-sonnet-5"], 0.9),
match(["deepseek-v4-pro"], 0.82),
match(["gpt-5-mini"], 0.8),
match(["gpt-4o-mini"], 0.76),
);
}
if (agentRisk === "policy_exception_simple") {
return Math.max(
base,
match(["gpt-5-mini"], 1),
match(["gpt-4.1"], 0.86),
match(["deepseek-v4-pro"], 0.82),
match(["gpt-4o-mini"], 0.8),
);
}
return Math.max(
base,
match(["deepseek-v4-pro"], 1),
match(["claude-sonnet-5"], 0.88),
match(["claude-opus-4.8"], 0.84),
match(["gpt-5-mini"], 0.78),
match(["gemini-3.5-flash"], 0.76),
);
}
if (agentDomain === "airline") {
return agentRisk === "complex_high"
? Math.max(
base,
match(["claude-sonnet-5"], 1),
match(["gpt-5-mini"], 0.78),
match(["claude-opus-4.8"], 0.76),
match(["gemini-3.5-flash"], 0.74),
)
: Math.max(
base,
match(["gpt-5-mini"], 1),
match(["claude-sonnet-5"], 0.9),
match(["gemini-3.5-flash"], 0.8),
);
}
// RouterBench calibration, 2026-07-26: Opus 4.8 produced complete
// multi-call payloads on 2/3 multilingual BFCL parallel cases. Gemini
// 3.5 Flash, Sonnet 5, DeepSeek V4 Pro, and Grok 4.5 were 0/3. This
// narrow prior only applies after the conservative prompt feature above.
return Math.max(
base,
match(["claude-opus-4.8"], 1),
match(["claude-sonnet-5"], 0.84),
match(["grok-4.5"], 0.82),
match(["gemini-3.5-flash"], 0.8),
match(["deepseek-v4-pro"], 0.78),
);
case "code_edit":
case "debug":
return Math.max(
base,
match(["gpt-5.3-codex"], 1),
match(["claude-sonnet-4.6"], 0.94),
match(["glm-5.2"], 0.9),
match(["deepseek-v4-pro"], 0.86),
);
case "reasoning":
return Math.max(
base,
match(["claude-sonnet-5", "claude-sonnet-4.6"], 0.98),
match(["deepseek-v4-pro"], 0.95),
match(["grok-4.5"], 0.94),
match(["gemini-3.1-pro", "gemini-3.5-flash"], 0.92),
);
case "reasoning_mcq":
// RouterBench calibration (2026-07-28, six stratified GPQA Diamond
// tasks, identical agent adapter and 512-token budget): Gemini 3
// Flash Preview scored 5/6, Gemini 3.5 Flash 4/6, and Gemini 3.1 Pro
// 3/6 while costing ~170x more than Flash. Keep the measured winner as
// the narrow default; version recency alone is not a quality signal.
// Unused host tools must not change this reasoning-only model choice.
return Math.max(
base,
match(["gemini-3-flash-preview"], 1),
match(["gemini-3.5-flash"], 0.91),
match(["grok-4.5"], 0.9),
match(["claude-sonnet-5"], 0.88),
match(["deepseek-v4-pro"], 0.84),
);
case "reasoning_math":
// Same calibration, five multilingual MGSM tasks: Gemini 3.5 Flash was
// 5/5 with the lowest cost and latency; four current flagships were 4/5
// and Kimi K2.7 was 3/5.
return Math.max(
base,
match(["gemini-3.5-flash"], 1),
match(["grok-4.5"], 0.93),
match(["claude-sonnet-5", "deepseek-v4-pro", "kimi-k3"], 0.9),
);
case "vision":
return Math.max(
base,
match(["gemini-3.1-pro"], 0.96),
match(["qwen3.7-max", "claude-sonnet-4.6", "kimi-k3", "grok-4.3"], 0.9),
);
case "long_context":
// Long-context eligibility is necessary but not sufficient: a provider
// can advertise a 1M window yet return an empty completion near that
// boundary. Keep the proven long-context flagship in the lead and put
// less-established alternatives in a separate affinity band so price
// alone cannot displace it.
return Math.max(
base,
match(["gemini-3.1-pro"], 1),
match(["qwen3.7-max", "glm-5.2"], 0.89),
match(["gemini-3.5-flash"], 0.88),
match(["deepseek-v4-pro"], 0.85),
);
case "extraction": {
// A structured extraction must preserve both the output contract and the
// source-language fields. For Mandarin input, keep the language-native
// Kimi candidate in a distinct affinity band. This is deliberately a
// candidate-pool decision (rather than a brittle post-hoc override): it
// still falls back normally if that model is unavailable or ineligible.
// Kimi K3 costs ~5x its retired sibling K2.7, so the band must be wider
// than the auto affinityFloorGap (0.10) or price alone re-selects a
// non-native model for Mandarin input; 0.12 keeps K3 alone in the
// primary band for zh and leaves every other language untouched.
const kimiExtractionAffinity = language === "zh" ? 1 : 0.9;
const otherExtractionAffinity = language === "zh" ? 0.88 : 0.9;
return Math.max(
base,
match(["gemini-3.5-flash", "gemini-2.5-flash", "gpt-4o-mini"], otherExtractionAffinity),
match(["claude-sonnet-5", "claude-sonnet-4.6"], otherExtractionAffinity),
match(["kimi-k3"], kimiExtractionAffinity),
);
}
default:
return Math.max(
base,
match(["gemini-3.5-flash", "gemini-2.5-flash", "kimi-k3"], 0.86),
);
}
}
function evidenceCandidates(task: TaskType): string[] {
if (task === "code_agent") {
return [
"openai/gpt-5.3-codex",
"anthropic/claude-sonnet-5",
"openai/gpt-5-mini",
"google/gemini-3.5-flash",
"moonshot/kimi-k3",
"deepseek/deepseek-v4-pro",
];
}
if (task === "tool_agent") {
return [
"anthropic/claude-sonnet-5",
"anthropic/claude-opus-5",
"openai/gpt-5-mini",
"openai/gpt-4.1",
"openai/gpt-4o-mini",
"google/gemini-3.5-flash",
"openai/gpt-5.3-codex",
"moonshot/kimi-k3",
"deepseek/deepseek-v4-pro",
];
}
if (task === "tool_agent_parallel") {
return [
"anthropic/claude-opus-5",
"anthropic/claude-opus-4.8",
"anthropic/claude-sonnet-5",
"openai/gpt-5-mini",
"openai/gpt-4.1",
"openai/gpt-4o-mini",
"xai/grok-4.5",
"google/gemini-3.5-flash",
"deepseek/deepseek-v4-pro",
];
}
if (task === "long_context") {
return [
"google/gemini-3.1-pro",
"deepseek/deepseek-v4-pro",
"qwen/qwen3.7-max",
"zai/glm-5.2",
"google/gemini-3.5-flash",
];
}
if (task === "reasoning_mcq") {
return [
"google/gemini-3-flash-preview",
"google/gemini-3.5-flash",
"xai/grok-4.5",
"anthropic/claude-sonnet-5",
"deepseek/deepseek-v4-pro",
];
}
if (task === "extraction") {
// Kimi K3 is no longer on the auto MEDIUM chain (K2.7 was); the
// language-native extraction band in affinity() needs it in the pool.
return ["moonshot/kimi-k3", "google/gemini-3.5-flash", "anthropic/claude-sonnet-5"];
}
if (task === "reasoning_math") {
return [
"google/gemini-3.5-flash",
"xai/grok-4.5",
"anthropic/claude-sonnet-5",
"deepseek/deepseek-v4-pro",
"moonshot/kimi-k3",
];
}
return [];
}
function isEligible(
modelId: string,
features: TaskFeatures,
maxOutputTokens: number,
options: RouterOptions,
): boolean {
const model = options.modelCapabilities?.[modelId] ?? DEFAULT_MODEL_CAPABILITIES[modelId];
// Preserve compatibility for temporarily catalog-less fallback IDs. They are
// kept behind known-model candidates but are not silently dropped.
if (!model) return true;
if (features.needsTools && !model.supportsTools) return false;
if (features.needsVision && !model.supportsVision) return false;
if (features.needsStructuredOutput && !model.supportsTools) return false;
if (model.maxOutputTokens < maxOutputTokens) return false;
return model.contextWindow >= (features.estimatedInputTokens + maxOutputTokens) * 1.1;
}
function estimatedCost(
modelId: string,
options: RouterOptions,
inputTokens: number,
outputTokens: number,
): number {
const price = options.modelPricing.get(modelId);
if (!price) return Number.POSITIVE_INFINITY;
if (price.flatPrice !== undefined) return price.flatPrice;
return (inputTokens * price.inputPrice + outputTokens * price.outputPrice) / 1_000_000;
}
function profileScore(
modelId: string,
options: RouterOptions,
now: Date,
):
| { quality?: number; speed: number; tailSpeed: number; reliability: number; freshness: number }
| undefined {
const profile: ModelPerformanceProfile | undefined =
options.modelPerformance?.[modelId] ??
LIVE_MODEL_PROFILES[modelId] ??
HISTORICAL_MODEL_PROFILES[modelId];
if (!profile) return undefined;
const measuredAt = Date.parse(profile.measuredAt);
if (!Number.isFinite(measuredAt)) return undefined;
const ageDays = Math.max(0, (now.getTime() - measuredAt) / 86_400_000);
// A 30-day half-life makes old data a tie-breaker only. Small probe runs are
// also weak evidence: three quick samples should not overturn a curated
// tier ordering merely because of a transient provider tail. Callers that
// inject an observation without a sample count retain the legacy full
// confidence behaviour for compatibility.
const sampleConfidence =
profile.samples === undefined ? 1 : Math.min(1, Math.max(0, profile.samples) / 10);
const freshness = Math.pow(0.5, ageDays / 30) * sampleConfidence;
const quality =
profile.intelligenceIndex === undefined
? undefined