@jsonstudio/llms 0.6.3405 → 0.6.3409
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/dist/conversion/shared/tool-call-id-manager.js +18 -21
- package/dist/native/router_hotpath_napi.node +0 -0
- package/dist/router/virtual-router/bootstrap/routing-config.d.ts +2 -1
- package/dist/router/virtual-router/bootstrap/routing-config.js +47 -2
- package/dist/router/virtual-router/engine-selection/tier-load-balancing.d.ts +16 -0
- package/dist/router/virtual-router/engine-selection/tier-load-balancing.js +120 -0
- package/dist/router/virtual-router/engine-selection/tier-selection-quota-integration.d.ts +2 -0
- package/dist/router/virtual-router/engine-selection/tier-selection-quota-integration.js +44 -66
- package/dist/router/virtual-router/engine-selection/tier-selection-select.js +53 -84
- package/dist/router/virtual-router/types.d.ts +16 -0
- package/package.json +1 -1
|
@@ -35,11 +35,9 @@ export class ToolCallIdManager {
|
|
|
35
35
|
*/
|
|
36
36
|
generateId() {
|
|
37
37
|
assertToolCallIdManagerNativeAvailable();
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
return normalizeIdValueWithNative(undefined, true);
|
|
38
|
+
const output = transformToolCallIdWithNative(this.state, '');
|
|
39
|
+
this.state = output.state;
|
|
40
|
+
return output.id;
|
|
43
41
|
}
|
|
44
42
|
/**
|
|
45
43
|
* 规范化工具调用 ID
|
|
@@ -50,13 +48,9 @@ export class ToolCallIdManager {
|
|
|
50
48
|
normalizeId(id) {
|
|
51
49
|
assertToolCallIdManagerNativeAvailable();
|
|
52
50
|
const raw = typeof id === 'string' ? id : '';
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
return output.id;
|
|
57
|
-
}
|
|
58
|
-
const trimmed = raw.trim();
|
|
59
|
-
return trimmed ? trimmed : normalizeIdValueWithNative(undefined, true);
|
|
51
|
+
const output = transformToolCallIdWithNative(this.state, raw);
|
|
52
|
+
this.state = output.state;
|
|
53
|
+
return output.id;
|
|
60
54
|
}
|
|
61
55
|
/**
|
|
62
56
|
* 规范化工具调用 ID(带别名注册)
|
|
@@ -68,18 +62,21 @@ export class ToolCallIdManager {
|
|
|
68
62
|
normalizeIdWithAlias(id, aliasMap) {
|
|
69
63
|
assertToolCallIdManagerNativeAvailable();
|
|
70
64
|
const raw = typeof id === 'string' ? id : '';
|
|
71
|
-
if (this.options.style !== 'preserve') {
|
|
72
|
-
return this.normalizeId(raw);
|
|
73
|
-
}
|
|
74
65
|
const trimmed = raw.trim();
|
|
75
|
-
if (
|
|
76
|
-
|
|
66
|
+
if (this.options.style === 'preserve' && trimmed && aliasMap?.has(trimmed)) {
|
|
67
|
+
const existing = aliasMap.get(trimmed);
|
|
68
|
+
const aliasState = this.state.aliasMap;
|
|
69
|
+
if (aliasState && typeof aliasState === 'object') {
|
|
70
|
+
aliasState[trimmed] = existing;
|
|
71
|
+
}
|
|
72
|
+
return existing;
|
|
77
73
|
}
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
74
|
+
const output = transformToolCallIdWithNative(this.state, raw);
|
|
75
|
+
this.state = output.state;
|
|
76
|
+
if (this.options.style === 'preserve' && trimmed && aliasMap) {
|
|
77
|
+
aliasMap.set(trimmed, output.id);
|
|
81
78
|
}
|
|
82
|
-
return
|
|
79
|
+
return output.id;
|
|
83
80
|
}
|
|
84
81
|
/**
|
|
85
82
|
* 批量规范化工具调用 ID
|
|
Binary file
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type RoutingPools } from '../types.js';
|
|
1
|
+
import { type RoutingPools, type RoutePoolLoadBalancingPolicy } from '../types.js';
|
|
2
2
|
export interface NormalizedRoutePoolConfig {
|
|
3
3
|
id: string;
|
|
4
4
|
priority: number;
|
|
@@ -6,6 +6,7 @@ export interface NormalizedRoutePoolConfig {
|
|
|
6
6
|
targets: string[];
|
|
7
7
|
mode?: 'round-robin' | 'priority';
|
|
8
8
|
force?: boolean;
|
|
9
|
+
loadBalancing?: RoutePoolLoadBalancingPolicy;
|
|
9
10
|
}
|
|
10
11
|
export declare function normalizeRouting(source: Record<string, unknown>): Record<string, NormalizedRoutePoolConfig[]>;
|
|
11
12
|
export declare function expandRoutingTable(routingSource: Record<string, NormalizedRoutePoolConfig[]>, aliasIndex: Map<string, string[]>, modelIndex: Map<string, {
|
|
@@ -91,7 +91,8 @@ export function expandRoutingTable(routingSource, aliasIndex, modelIndex) {
|
|
|
91
91
|
backup: pool.backup,
|
|
92
92
|
targets: sortedTargets,
|
|
93
93
|
...(pool.mode ? { mode: pool.mode } : {}),
|
|
94
|
-
...(pool.force ? { force: true } : {})
|
|
94
|
+
...(pool.force ? { force: true } : {}),
|
|
95
|
+
...(pool.loadBalancing ? { loadBalancing: pool.loadBalancing } : {})
|
|
95
96
|
});
|
|
96
97
|
}
|
|
97
98
|
}
|
|
@@ -134,6 +135,7 @@ function normalizeRoutePoolEntry(routeName, entry, index, total) {
|
|
|
134
135
|
const mode = normalizeRoutePoolMode(record.mode ?? record.strategy ?? record.routingMode);
|
|
135
136
|
const force = record.force === true ||
|
|
136
137
|
(typeof record.force === 'string' && record.force.trim().toLowerCase() === 'true');
|
|
138
|
+
const loadBalancing = normalizeRoutePoolLoadBalancing(record.loadBalancing);
|
|
137
139
|
return targets.length
|
|
138
140
|
? {
|
|
139
141
|
id,
|
|
@@ -141,10 +143,53 @@ function normalizeRoutePoolEntry(routeName, entry, index, total) {
|
|
|
141
143
|
backup,
|
|
142
144
|
targets,
|
|
143
145
|
...(mode ? { mode } : {}),
|
|
144
|
-
...(force ? { force: true } : {})
|
|
146
|
+
...(force ? { force: true } : {}),
|
|
147
|
+
...(loadBalancing ? { loadBalancing } : {})
|
|
145
148
|
}
|
|
146
149
|
: null;
|
|
147
150
|
}
|
|
151
|
+
function normalizeRoutePoolLoadBalancing(input) {
|
|
152
|
+
if (!input || typeof input !== 'object' || Array.isArray(input)) {
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
const record = input;
|
|
156
|
+
const strategy = normalizeWeightedStrategy(record.strategy);
|
|
157
|
+
const weightsRaw = record.weights && typeof record.weights === 'object' && !Array.isArray(record.weights)
|
|
158
|
+
? record.weights
|
|
159
|
+
: {};
|
|
160
|
+
const weights = {};
|
|
161
|
+
for (const [key, value] of Object.entries(weightsRaw)) {
|
|
162
|
+
if (typeof value === 'number' && Number.isFinite(value) && value > 0) {
|
|
163
|
+
weights[key] = value;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (!strategy && Object.keys(weights).length === 0) {
|
|
167
|
+
return undefined;
|
|
168
|
+
}
|
|
169
|
+
return {
|
|
170
|
+
...(strategy ? { strategy } : {}),
|
|
171
|
+
...(Object.keys(weights).length ? { weights } : {})
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
function normalizeWeightedStrategy(value) {
|
|
175
|
+
if (typeof value !== 'string') {
|
|
176
|
+
return undefined;
|
|
177
|
+
}
|
|
178
|
+
const normalized = value.trim().toLowerCase();
|
|
179
|
+
if (!normalized) {
|
|
180
|
+
return undefined;
|
|
181
|
+
}
|
|
182
|
+
if (normalized === 'weighted') {
|
|
183
|
+
return 'weighted';
|
|
184
|
+
}
|
|
185
|
+
if (normalized === 'sticky') {
|
|
186
|
+
return 'sticky';
|
|
187
|
+
}
|
|
188
|
+
if (normalized === 'round-robin' || normalized === 'round_robin' || normalized === 'roundrobin' || normalized === 'rr') {
|
|
189
|
+
return 'round-robin';
|
|
190
|
+
}
|
|
191
|
+
return undefined;
|
|
192
|
+
}
|
|
148
193
|
function normalizeRoutePoolMode(value) {
|
|
149
194
|
if (typeof value !== 'string') {
|
|
150
195
|
return undefined;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { LoadBalancingPolicy, RoutePoolTier } from '../types.js';
|
|
2
|
+
import type { ProviderRegistry } from '../provider-registry.js';
|
|
3
|
+
export type ResolvedTierLoadBalancing = {
|
|
4
|
+
strategy: LoadBalancingPolicy['strategy'];
|
|
5
|
+
weights?: Record<string, number>;
|
|
6
|
+
};
|
|
7
|
+
export declare function resolveTierLoadBalancing(tier: RoutePoolTier, globalPolicy?: LoadBalancingPolicy): ResolvedTierLoadBalancing;
|
|
8
|
+
export declare function resolveGroupWeight(groupId: string, weights?: Record<string, number>): number;
|
|
9
|
+
export declare function buildGroupWeights(groups: Map<string, string[]>, weights?: Record<string, number>): Record<string, number> | undefined;
|
|
10
|
+
export declare function hasNonUniformWeights(candidates: string[], weights?: Record<string, number>): boolean;
|
|
11
|
+
export declare function buildCandidateWeights(opts: {
|
|
12
|
+
candidates: string[];
|
|
13
|
+
providerRegistry: ProviderRegistry;
|
|
14
|
+
staticWeights?: Record<string, number>;
|
|
15
|
+
dynamicWeights?: Record<string, number>;
|
|
16
|
+
}): Record<string, number> | undefined;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { extractProviderId, getProviderModelId } from './key-parsing.js';
|
|
2
|
+
export function resolveTierLoadBalancing(tier, globalPolicy) {
|
|
3
|
+
const tierPolicy = tier.loadBalancing;
|
|
4
|
+
return {
|
|
5
|
+
strategy: tierPolicy?.strategy ?? globalPolicy?.strategy ?? 'round-robin',
|
|
6
|
+
weights: tierPolicy?.weights ?? globalPolicy?.weights
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
export function resolveGroupWeight(groupId, weights) {
|
|
10
|
+
if (!weights) {
|
|
11
|
+
return 1;
|
|
12
|
+
}
|
|
13
|
+
const direct = weights[groupId];
|
|
14
|
+
if (typeof direct === 'number' && Number.isFinite(direct) && direct > 0) {
|
|
15
|
+
return direct;
|
|
16
|
+
}
|
|
17
|
+
const providerId = groupId.split('.')[0] ?? groupId;
|
|
18
|
+
const providerOnly = weights[providerId];
|
|
19
|
+
if (typeof providerOnly === 'number' && Number.isFinite(providerOnly) && providerOnly > 0) {
|
|
20
|
+
return providerOnly;
|
|
21
|
+
}
|
|
22
|
+
return 1;
|
|
23
|
+
}
|
|
24
|
+
export function buildGroupWeights(groups, weights) {
|
|
25
|
+
if (!groups.size || !weights) {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
const out = {};
|
|
29
|
+
let hasExplicit = false;
|
|
30
|
+
for (const [groupId] of groups.entries()) {
|
|
31
|
+
const resolved = resolveGroupWeight(groupId, weights);
|
|
32
|
+
out[groupId] = resolved;
|
|
33
|
+
if (resolved !== 1) {
|
|
34
|
+
hasExplicit = true;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return hasExplicit ? out : undefined;
|
|
38
|
+
}
|
|
39
|
+
export function hasNonUniformWeights(candidates, weights) {
|
|
40
|
+
if (!weights || candidates.length < 2) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
let ref;
|
|
44
|
+
for (const key of candidates) {
|
|
45
|
+
const raw = weights[key];
|
|
46
|
+
if (typeof raw !== 'number' || !Number.isFinite(raw)) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (ref === undefined) {
|
|
50
|
+
ref = raw;
|
|
51
|
+
}
|
|
52
|
+
else if (Math.abs(raw - ref) > 1e-6) {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
export function buildCandidateWeights(opts) {
|
|
59
|
+
const { candidates, providerRegistry, staticWeights, dynamicWeights } = opts;
|
|
60
|
+
if ((!staticWeights || Object.keys(staticWeights).length === 0) && (!dynamicWeights || Object.keys(dynamicWeights).length === 0)) {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
const out = {};
|
|
64
|
+
let hasExplicit = false;
|
|
65
|
+
for (const key of candidates) {
|
|
66
|
+
const dynamic = dynamicWeights?.[key];
|
|
67
|
+
const staticWeight = resolveCandidateWeight(key, staticWeights, providerRegistry);
|
|
68
|
+
const resolved = multiplyPositiveWeights(dynamic, staticWeight);
|
|
69
|
+
if (resolved !== undefined) {
|
|
70
|
+
out[key] = resolved;
|
|
71
|
+
if (resolved !== 1) {
|
|
72
|
+
hasExplicit = true;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (!hasExplicit) {
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
return out;
|
|
80
|
+
}
|
|
81
|
+
function resolveCandidateWeight(key, weights, providerRegistry) {
|
|
82
|
+
if (!weights) {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
const direct = normalizePositiveWeight(weights[key]);
|
|
86
|
+
if (direct !== undefined) {
|
|
87
|
+
return direct;
|
|
88
|
+
}
|
|
89
|
+
const providerId = extractProviderId(key) ?? '';
|
|
90
|
+
if (!providerId) {
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
const modelId = getProviderModelId(key, providerRegistry) ?? '';
|
|
95
|
+
if (modelId) {
|
|
96
|
+
const grouped = normalizePositiveWeight(weights[`${providerId}.${modelId}`]);
|
|
97
|
+
if (grouped !== undefined) {
|
|
98
|
+
return grouped;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
// Ignore registry misses and fall back to provider-only weight.
|
|
104
|
+
}
|
|
105
|
+
return normalizePositiveWeight(weights[providerId]);
|
|
106
|
+
}
|
|
107
|
+
function normalizePositiveWeight(value) {
|
|
108
|
+
return typeof value === 'number' && Number.isFinite(value) && value > 0 ? value : undefined;
|
|
109
|
+
}
|
|
110
|
+
function multiplyPositiveWeights(...values) {
|
|
111
|
+
let resolved;
|
|
112
|
+
for (const value of values) {
|
|
113
|
+
const normalized = normalizePositiveWeight(value);
|
|
114
|
+
if (normalized === undefined) {
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
resolved = resolved === undefined ? normalized : Math.max(1, Math.round(resolved * normalized));
|
|
118
|
+
}
|
|
119
|
+
return resolved;
|
|
120
|
+
}
|
|
@@ -3,6 +3,7 @@ import { type ResolvedContextWeightedConfig } from '../context-weighted.js';
|
|
|
3
3
|
import { type ResolvedHealthWeightedConfig } from '../health-weighted.js';
|
|
4
4
|
import type { RoutePoolTier } from '../types.js';
|
|
5
5
|
import type { SelectionDeps, TrySelectFromTierOptions } from './selection-deps.js';
|
|
6
|
+
import { type ResolvedTierLoadBalancing } from './tier-load-balancing.js';
|
|
6
7
|
export declare function selectProviderKeyWithQuotaBuckets(opts: {
|
|
7
8
|
routeName: string;
|
|
8
9
|
tier: RoutePoolTier;
|
|
@@ -19,6 +20,7 @@ export declare function selectProviderKeyWithQuotaBuckets(opts: {
|
|
|
19
20
|
nowForWeights: number;
|
|
20
21
|
healthWeightedCfg: ResolvedHealthWeightedConfig;
|
|
21
22
|
contextWeightedCfg: ResolvedContextWeightedConfig;
|
|
23
|
+
tierLoadBalancing: ResolvedTierLoadBalancing;
|
|
22
24
|
quotaView: NonNullable<SelectionDeps['quotaView']>;
|
|
23
25
|
isAvailable: (key: string) => boolean;
|
|
24
26
|
selectFirstAvailable: (keys: string[]) => string | null;
|
|
@@ -2,6 +2,7 @@ import { computeContextMultiplier } from '../context-weighted.js';
|
|
|
2
2
|
import { computeHealthWeight } from '../health-weighted.js';
|
|
3
3
|
import { buildQuotaBuckets } from './native-router-hotpath.js';
|
|
4
4
|
import { computeContextWeightMultipliers } from './context-weight-multipliers.js';
|
|
5
|
+
import { buildCandidateWeights, buildGroupWeights, hasNonUniformWeights } from './tier-load-balancing.js';
|
|
5
6
|
import { pickPriorityGroup } from './tier-priority.js';
|
|
6
7
|
import { extractProviderId, getProviderModelId } from './key-parsing.js';
|
|
7
8
|
function buildPrimaryTargetGroups(candidates, deps) {
|
|
@@ -25,52 +26,8 @@ function buildPrimaryTargetGroups(candidates, deps) {
|
|
|
25
26
|
}
|
|
26
27
|
return groups;
|
|
27
28
|
}
|
|
28
|
-
function resolveGroupWeight(groupId, weights) {
|
|
29
|
-
if (!weights) {
|
|
30
|
-
return 1;
|
|
31
|
-
}
|
|
32
|
-
const direct = weights[groupId];
|
|
33
|
-
if (typeof direct === 'number' && Number.isFinite(direct) && direct > 0) {
|
|
34
|
-
return direct;
|
|
35
|
-
}
|
|
36
|
-
const providerId = groupId.split('.')[0] ?? groupId;
|
|
37
|
-
const providerOnly = weights[providerId];
|
|
38
|
-
if (typeof providerOnly === 'number' && Number.isFinite(providerOnly) && providerOnly > 0) {
|
|
39
|
-
return providerOnly;
|
|
40
|
-
}
|
|
41
|
-
return 1;
|
|
42
|
-
}
|
|
43
|
-
function buildGroupWeights(groups, weights) {
|
|
44
|
-
if (!groups.size) {
|
|
45
|
-
return undefined;
|
|
46
|
-
}
|
|
47
|
-
const out = {};
|
|
48
|
-
for (const [groupId] of groups.entries()) {
|
|
49
|
-
out[groupId] = resolveGroupWeight(groupId, weights);
|
|
50
|
-
}
|
|
51
|
-
return out;
|
|
52
|
-
}
|
|
53
|
-
function hasNonUniformWeights(candidates, weights) {
|
|
54
|
-
if (!weights || candidates.length < 2) {
|
|
55
|
-
return false;
|
|
56
|
-
}
|
|
57
|
-
let ref;
|
|
58
|
-
for (const key of candidates) {
|
|
59
|
-
const raw = weights[key];
|
|
60
|
-
if (typeof raw !== 'number' || !Number.isFinite(raw)) {
|
|
61
|
-
continue;
|
|
62
|
-
}
|
|
63
|
-
if (ref === undefined) {
|
|
64
|
-
ref = raw;
|
|
65
|
-
}
|
|
66
|
-
else if (Math.abs(raw - ref) > 1e-6) {
|
|
67
|
-
return true;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
return false;
|
|
71
|
-
}
|
|
72
29
|
export function selectProviderKeyWithQuotaBuckets(opts) {
|
|
73
|
-
const { routeName, tier, stickyKey, candidates, isSafePool, deps, options, contextResult, warnRatio, isRecoveryAttempt, now, nowForWeights, healthWeightedCfg, contextWeightedCfg, quotaView, isAvailable, selectFirstAvailable, applyAliasStickyQueuePinning, preferAntigravityAliasesOnRetry } = opts;
|
|
30
|
+
const { routeName, tier, stickyKey, candidates, isSafePool, deps, options, contextResult, warnRatio, isRecoveryAttempt, now, nowForWeights, healthWeightedCfg, contextWeightedCfg, tierLoadBalancing, quotaView, isAvailable, selectFirstAvailable, applyAliasStickyQueuePinning, preferAntigravityAliasesOnRetry } = opts;
|
|
74
31
|
const bucketInputs = candidates.map((key, order) => {
|
|
75
32
|
const entry = quotaView(key);
|
|
76
33
|
const penaltyRaw = entry?.selectionPenalty;
|
|
@@ -100,35 +57,56 @@ export function selectProviderKeyWithQuotaBuckets(opts) {
|
|
|
100
57
|
bucketCandidates = preferAntigravityAliasesOnRetry(bucketCandidates);
|
|
101
58
|
}
|
|
102
59
|
bucketCandidates = applyAliasStickyQueuePinning(bucketCandidates);
|
|
103
|
-
const
|
|
60
|
+
const quotaWeights = {};
|
|
104
61
|
for (const item of bucket) {
|
|
105
62
|
if (healthWeightedCfg.enabled) {
|
|
106
63
|
const entry = quotaView(item.key);
|
|
107
64
|
const { weight } = computeHealthWeight(entry, nowForWeights, healthWeightedCfg);
|
|
108
|
-
|
|
65
|
+
quotaWeights[item.key] = weight;
|
|
109
66
|
}
|
|
110
67
|
else {
|
|
111
|
-
|
|
68
|
+
quotaWeights[item.key] = Math.max(1, Math.floor(100 / (1 + Math.max(0, item.penalty))));
|
|
112
69
|
}
|
|
113
70
|
}
|
|
114
|
-
|
|
71
|
+
const contextWeights = (() => {
|
|
72
|
+
if (!isSafePool || !contextWeightedCfg.enabled) {
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
115
75
|
const ctx = computeContextWeightMultipliers({
|
|
116
76
|
candidates: bucketCandidates,
|
|
117
77
|
usage: contextResult.usage,
|
|
118
78
|
warnRatio,
|
|
119
79
|
cfg: contextWeightedCfg
|
|
120
80
|
});
|
|
121
|
-
if (ctx) {
|
|
122
|
-
|
|
123
|
-
const m = computeContextMultiplier({
|
|
124
|
-
effectiveSafeRefTokens: ctx.ref,
|
|
125
|
-
effectiveSafeTokens: ctx.eff[key] ?? 1,
|
|
126
|
-
cfg: contextWeightedCfg
|
|
127
|
-
});
|
|
128
|
-
bucketWeights[key] = Math.max(1, Math.round((bucketWeights[key] ?? 1) * m));
|
|
129
|
-
}
|
|
81
|
+
if (!ctx) {
|
|
82
|
+
return undefined;
|
|
130
83
|
}
|
|
131
|
-
|
|
84
|
+
const out = {};
|
|
85
|
+
for (const key of bucketCandidates) {
|
|
86
|
+
const m = computeContextMultiplier({
|
|
87
|
+
effectiveSafeRefTokens: ctx.ref,
|
|
88
|
+
effectiveSafeTokens: ctx.eff[key] ?? 1,
|
|
89
|
+
cfg: contextWeightedCfg
|
|
90
|
+
});
|
|
91
|
+
out[key] = Math.max(1, Math.round(100 * m));
|
|
92
|
+
}
|
|
93
|
+
return out;
|
|
94
|
+
})();
|
|
95
|
+
const bucketWeights = buildCandidateWeights({
|
|
96
|
+
candidates: bucketCandidates,
|
|
97
|
+
providerRegistry: deps.providerRegistry,
|
|
98
|
+
staticWeights: tierLoadBalancing.weights,
|
|
99
|
+
dynamicWeights: Object.keys(quotaWeights).length || contextWeights
|
|
100
|
+
? Object.fromEntries(bucketCandidates.map((key) => {
|
|
101
|
+
const quotaWeight = quotaWeights[key];
|
|
102
|
+
const contextWeight = contextWeights?.[key];
|
|
103
|
+
const combined = typeof contextWeight === 'number'
|
|
104
|
+
? Math.max(1, Math.round((quotaWeight ?? 1) * contextWeight))
|
|
105
|
+
: quotaWeight;
|
|
106
|
+
return [key, combined ?? 1];
|
|
107
|
+
}))
|
|
108
|
+
: undefined
|
|
109
|
+
});
|
|
132
110
|
if (tier.mode === 'priority') {
|
|
133
111
|
if (!isRecoveryAttempt) {
|
|
134
112
|
const group = pickPriorityGroup({
|
|
@@ -142,13 +120,13 @@ export function selectProviderKeyWithQuotaBuckets(opts) {
|
|
|
142
120
|
}
|
|
143
121
|
const groupWeights = {};
|
|
144
122
|
for (const key of group.groupCandidates) {
|
|
145
|
-
groupWeights[key] = bucketWeights[key] ?? 1;
|
|
123
|
+
groupWeights[key] = bucketWeights?.[key] ?? 1;
|
|
146
124
|
}
|
|
147
125
|
const allowGrouped = !hasNonUniformWeights(group.groupCandidates, bucketWeights);
|
|
148
|
-
if (allowGrouped &&
|
|
126
|
+
if (allowGrouped && tierLoadBalancing.strategy !== 'sticky') {
|
|
149
127
|
const groups = buildPrimaryTargetGroups(group.groupCandidates, deps);
|
|
150
128
|
if (groups.size > 0) {
|
|
151
|
-
const groupWeightMap = buildGroupWeights(groups,
|
|
129
|
+
const groupWeightMap = buildGroupWeights(groups, tierLoadBalancing.weights);
|
|
152
130
|
const selected = deps.loadBalancer.selectGrouped({
|
|
153
131
|
routeName: `${routeName}:${tier.id}:priority:${priority}:group:${group.groupId}`,
|
|
154
132
|
groups,
|
|
@@ -185,17 +163,17 @@ export function selectProviderKeyWithQuotaBuckets(opts) {
|
|
|
185
163
|
continue;
|
|
186
164
|
}
|
|
187
165
|
const allowGrouped = !hasNonUniformWeights(bucketCandidates, bucketWeights);
|
|
188
|
-
if (allowGrouped &&
|
|
166
|
+
if (allowGrouped && tierLoadBalancing.strategy !== 'sticky') {
|
|
189
167
|
const groups = buildPrimaryTargetGroups(bucketCandidates, deps);
|
|
190
168
|
if (groups.size > 0) {
|
|
191
|
-
const groupWeightMap = buildGroupWeights(groups,
|
|
169
|
+
const groupWeightMap = buildGroupWeights(groups, tierLoadBalancing.weights);
|
|
192
170
|
const selected = deps.loadBalancer.selectGrouped({
|
|
193
171
|
routeName: `${routeName}:${tier.id}:${priority}`,
|
|
194
172
|
groups,
|
|
195
173
|
stickyKey: options.allowAliasRotation ? undefined : stickyKey,
|
|
196
174
|
weights: groupWeightMap,
|
|
197
175
|
availabilityCheck: isAvailable
|
|
198
|
-
}, tier.mode === 'round-robin' ? 'round-robin' :
|
|
176
|
+
}, tier.mode === 'round-robin' ? 'round-robin' : tierLoadBalancing.strategy);
|
|
199
177
|
if (selected) {
|
|
200
178
|
return selected;
|
|
201
179
|
}
|
|
@@ -207,7 +185,7 @@ export function selectProviderKeyWithQuotaBuckets(opts) {
|
|
|
207
185
|
stickyKey: options.allowAliasRotation ? undefined : stickyKey,
|
|
208
186
|
weights: bucketWeights,
|
|
209
187
|
availabilityCheck: isAvailable
|
|
210
|
-
}, tier.mode === 'round-robin' ? 'round-robin' :
|
|
188
|
+
}, tier.mode === 'round-robin' ? 'round-robin' : tierLoadBalancing.strategy);
|
|
211
189
|
if (selected) {
|
|
212
190
|
return selected;
|
|
213
191
|
}
|
|
@@ -2,6 +2,7 @@ import { computeContextMultiplier } from '../context-weighted.js';
|
|
|
2
2
|
import { pinCandidatesByAliasQueue, resolveAliasSelectionStrategy } from './alias-selection.js';
|
|
3
3
|
import { computeContextWeightMultipliers } from './context-weight-multipliers.js';
|
|
4
4
|
import { extractKeyAlias, extractProviderId, getProviderModelId } from './key-parsing.js';
|
|
5
|
+
import { buildCandidateWeights, buildGroupWeights, hasNonUniformWeights, resolveTierLoadBalancing } from './tier-load-balancing.js';
|
|
5
6
|
import { pickPriorityGroup } from './tier-priority.js';
|
|
6
7
|
import { selectProviderKeyWithQuotaBuckets } from './tier-selection-quota-integration.js';
|
|
7
8
|
function buildPrimaryTargetGroups(candidates, deps) {
|
|
@@ -25,50 +26,6 @@ function buildPrimaryTargetGroups(candidates, deps) {
|
|
|
25
26
|
}
|
|
26
27
|
return groups;
|
|
27
28
|
}
|
|
28
|
-
function resolveGroupWeight(groupId, weights) {
|
|
29
|
-
if (!weights) {
|
|
30
|
-
return 1;
|
|
31
|
-
}
|
|
32
|
-
const direct = weights[groupId];
|
|
33
|
-
if (typeof direct === 'number' && Number.isFinite(direct) && direct > 0) {
|
|
34
|
-
return direct;
|
|
35
|
-
}
|
|
36
|
-
const providerId = groupId.split('.')[0] ?? groupId;
|
|
37
|
-
const providerOnly = weights[providerId];
|
|
38
|
-
if (typeof providerOnly === 'number' && Number.isFinite(providerOnly) && providerOnly > 0) {
|
|
39
|
-
return providerOnly;
|
|
40
|
-
}
|
|
41
|
-
return 1;
|
|
42
|
-
}
|
|
43
|
-
function buildGroupWeights(groups, weights) {
|
|
44
|
-
if (!groups.size) {
|
|
45
|
-
return undefined;
|
|
46
|
-
}
|
|
47
|
-
const out = {};
|
|
48
|
-
for (const [groupId] of groups.entries()) {
|
|
49
|
-
out[groupId] = resolveGroupWeight(groupId, weights);
|
|
50
|
-
}
|
|
51
|
-
return out;
|
|
52
|
-
}
|
|
53
|
-
function hasNonUniformWeights(candidates, weights) {
|
|
54
|
-
if (!weights || candidates.length < 2) {
|
|
55
|
-
return false;
|
|
56
|
-
}
|
|
57
|
-
let ref;
|
|
58
|
-
for (const key of candidates) {
|
|
59
|
-
const raw = weights[key];
|
|
60
|
-
if (typeof raw !== 'number' || !Number.isFinite(raw)) {
|
|
61
|
-
continue;
|
|
62
|
-
}
|
|
63
|
-
if (ref === undefined) {
|
|
64
|
-
ref = raw;
|
|
65
|
-
}
|
|
66
|
-
else if (Math.abs(raw - ref) > 1e-6) {
|
|
67
|
-
return true;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
return false;
|
|
71
|
-
}
|
|
72
29
|
function applyAliasStickyQueuePinning(opts) {
|
|
73
30
|
const { candidates, orderedTargets, deps, excludedKeys } = opts;
|
|
74
31
|
if (!Array.isArray(candidates) || candidates.length < 2) {
|
|
@@ -202,6 +159,7 @@ function preferAntigravityAliasesOnRetry(opts) {
|
|
|
202
159
|
export function selectProviderKeyFromCandidatePool(opts) {
|
|
203
160
|
const { routeName, tier, stickyKey, candidates, isSafePool, deps, options, contextResult, warnRatio, excludedKeys, isRecoveryAttempt, now, nowForWeights, healthWeightedCfg, contextWeightedCfg } = opts;
|
|
204
161
|
const quotaView = deps.quotaView;
|
|
162
|
+
const tierLoadBalancing = resolveTierLoadBalancing(tier, deps.loadBalancer.getPolicy());
|
|
205
163
|
const isAvailable = (key) => {
|
|
206
164
|
if (!quotaView) {
|
|
207
165
|
return deps.healthManager.isAvailable(key);
|
|
@@ -265,28 +223,33 @@ export function selectProviderKeyFromCandidatePool(opts) {
|
|
|
265
223
|
if (!group) {
|
|
266
224
|
return null;
|
|
267
225
|
}
|
|
268
|
-
const weights = (
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
226
|
+
const weights = buildCandidateWeights({
|
|
227
|
+
candidates: group.groupCandidates,
|
|
228
|
+
providerRegistry: deps.providerRegistry,
|
|
229
|
+
staticWeights: tierLoadBalancing.weights,
|
|
230
|
+
dynamicWeights: (() => {
|
|
231
|
+
if (!isSafePool)
|
|
232
|
+
return undefined;
|
|
233
|
+
const ctx = computeContextWeightMultipliers({ candidates: group.groupCandidates, usage: contextResult.usage, warnRatio, cfg: contextWeightedCfg });
|
|
234
|
+
if (!ctx)
|
|
235
|
+
return undefined;
|
|
236
|
+
const out = {};
|
|
237
|
+
for (const key of group.groupCandidates) {
|
|
238
|
+
const m = computeContextMultiplier({
|
|
239
|
+
effectiveSafeRefTokens: ctx.ref,
|
|
240
|
+
effectiveSafeTokens: ctx.eff[key] ?? 1,
|
|
241
|
+
cfg: contextWeightedCfg
|
|
242
|
+
});
|
|
243
|
+
out[key] = Math.max(1, Math.round(100 * m));
|
|
244
|
+
}
|
|
245
|
+
return out;
|
|
246
|
+
})()
|
|
247
|
+
});
|
|
285
248
|
const allowGrouped = !hasNonUniformWeights(group.groupCandidates, weights);
|
|
286
|
-
if (allowGrouped &&
|
|
249
|
+
if (allowGrouped && tierLoadBalancing.strategy !== 'sticky') {
|
|
287
250
|
const groups = buildPrimaryTargetGroups(group.groupCandidates, deps);
|
|
288
251
|
if (groups.size > 0) {
|
|
289
|
-
const groupWeights = buildGroupWeights(groups,
|
|
252
|
+
const groupWeights = buildGroupWeights(groups, tierLoadBalancing.weights);
|
|
290
253
|
const selected = deps.loadBalancer.selectGrouped({
|
|
291
254
|
routeName: `${routeName}:${tier.id}:priority:group:${group.groupId}`,
|
|
292
255
|
groups,
|
|
@@ -307,35 +270,40 @@ export function selectProviderKeyFromCandidatePool(opts) {
|
|
|
307
270
|
availabilityCheck: isAvailable
|
|
308
271
|
}, 'round-robin');
|
|
309
272
|
}
|
|
310
|
-
const weights = (
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
273
|
+
const weights = buildCandidateWeights({
|
|
274
|
+
candidates: pinnedCandidates,
|
|
275
|
+
providerRegistry: deps.providerRegistry,
|
|
276
|
+
staticWeights: tierLoadBalancing.weights,
|
|
277
|
+
dynamicWeights: (() => {
|
|
278
|
+
if (!isSafePool || !contextWeightedCfg.enabled)
|
|
279
|
+
return undefined;
|
|
280
|
+
const ctx = computeContextWeightMultipliers({ candidates: pinnedCandidates, usage: contextResult.usage, warnRatio, cfg: contextWeightedCfg });
|
|
281
|
+
if (!ctx)
|
|
282
|
+
return undefined;
|
|
283
|
+
const out = {};
|
|
284
|
+
for (const key of pinnedCandidates) {
|
|
285
|
+
const m = computeContextMultiplier({
|
|
286
|
+
effectiveSafeRefTokens: ctx.ref,
|
|
287
|
+
effectiveSafeTokens: ctx.eff[key] ?? 1,
|
|
288
|
+
cfg: contextWeightedCfg
|
|
289
|
+
});
|
|
290
|
+
out[key] = Math.max(1, Math.round(100 * m));
|
|
291
|
+
}
|
|
292
|
+
return out;
|
|
293
|
+
})()
|
|
294
|
+
});
|
|
327
295
|
const allowGrouped = !hasNonUniformWeights(pinnedCandidates, weights);
|
|
328
|
-
if (allowGrouped &&
|
|
296
|
+
if (allowGrouped && tierLoadBalancing.strategy !== 'sticky') {
|
|
329
297
|
const groups = buildPrimaryTargetGroups(pinnedCandidates, deps);
|
|
330
298
|
if (groups.size > 0) {
|
|
331
|
-
const groupWeights = buildGroupWeights(groups,
|
|
299
|
+
const groupWeights = buildGroupWeights(groups, tierLoadBalancing.weights);
|
|
332
300
|
const selected = deps.loadBalancer.selectGrouped({
|
|
333
301
|
routeName: `${routeName}:${tier.id}`,
|
|
334
302
|
groups,
|
|
335
303
|
stickyKey: options.allowAliasRotation ? undefined : stickyKey,
|
|
336
304
|
weights: groupWeights,
|
|
337
305
|
availabilityCheck: isAvailable
|
|
338
|
-
}, tier.mode === 'round-robin' ? 'round-robin' :
|
|
306
|
+
}, tier.mode === 'round-robin' ? 'round-robin' : tierLoadBalancing.strategy);
|
|
339
307
|
if (selected) {
|
|
340
308
|
return selected;
|
|
341
309
|
}
|
|
@@ -347,7 +315,7 @@ export function selectProviderKeyFromCandidatePool(opts) {
|
|
|
347
315
|
stickyKey: options.allowAliasRotation ? undefined : stickyKey,
|
|
348
316
|
weights,
|
|
349
317
|
availabilityCheck: isAvailable
|
|
350
|
-
}, tier.mode === 'round-robin' ? 'round-robin' :
|
|
318
|
+
}, tier.mode === 'round-robin' ? 'round-robin' : tierLoadBalancing.strategy);
|
|
351
319
|
}
|
|
352
320
|
return selectProviderKeyWithQuotaBuckets({
|
|
353
321
|
routeName,
|
|
@@ -365,6 +333,7 @@ export function selectProviderKeyFromCandidatePool(opts) {
|
|
|
365
333
|
nowForWeights,
|
|
366
334
|
healthWeightedCfg,
|
|
367
335
|
contextWeightedCfg,
|
|
336
|
+
tierLoadBalancing,
|
|
368
337
|
quotaView,
|
|
369
338
|
isAvailable,
|
|
370
339
|
selectFirstAvailable,
|
|
@@ -7,6 +7,17 @@ export declare const DEFAULT_ROUTE = "default";
|
|
|
7
7
|
export declare const ROUTE_PRIORITY: string[];
|
|
8
8
|
export type RoutingInstructionMode = 'force' | 'sticky' | 'none';
|
|
9
9
|
export type RoutePoolMode = 'round-robin' | 'priority';
|
|
10
|
+
export interface RoutePoolLoadBalancingPolicy {
|
|
11
|
+
/**
|
|
12
|
+
* Optional pool-level override for provider selection strategy.
|
|
13
|
+
* When omitted, Virtual Router falls back to the global loadBalancing.strategy.
|
|
14
|
+
*/
|
|
15
|
+
strategy?: 'round-robin' | 'weighted' | 'sticky';
|
|
16
|
+
/**
|
|
17
|
+
* Optional pool-local weights. Keys may target runtime keys, provider.model groups, or provider ids.
|
|
18
|
+
*/
|
|
19
|
+
weights?: Record<string, number>;
|
|
20
|
+
}
|
|
10
21
|
export interface RoutePoolTier {
|
|
11
22
|
id: string;
|
|
12
23
|
targets: string[];
|
|
@@ -25,6 +36,11 @@ export interface RoutePoolTier {
|
|
|
25
36
|
* - routing.web_search: force server-side web_search flow.
|
|
26
37
|
*/
|
|
27
38
|
force?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Optional pool-scoped load-balancing override. This lets different route pools
|
|
41
|
+
* use different strategies/weights without mutating the global policy.
|
|
42
|
+
*/
|
|
43
|
+
loadBalancing?: RoutePoolLoadBalancingPolicy;
|
|
28
44
|
}
|
|
29
45
|
export type RoutingPools = Record<string, RoutePoolTier[]>;
|
|
30
46
|
export type StreamingPreference = 'auto' | 'always' | 'never';
|