Repository navigation
Expand file tree
/
Copy pathselector.ts
More file actions
256 lines (232 loc) · 9.24 KB
/
Copy pathselector.ts
File metadata and controls
256 lines (232 loc) · 9.24 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
/**
* Tier → Model Selection
*
* Maps a classification tier to the cheapest capable model.
* Builds RoutingDecision metadata with cost estimates and savings.
*/
import type { Tier, TierConfig, RoutingDecision } from "./types.js";
export type ModelPricing = {
inputPrice: number; // per 1M tokens
outputPrice: number; // per 1M tokens
/** Active promo flat price per request (overrides token-based pricing when set) */
flatPrice?: number;
};
// The savings baseline is a price anchor, not "the current flagship" — it is
// deliberately NOT bumped every time a new Opus ships. Opus 4.7, 4.8 and 5 all
// bill $5/$25, so moving it would change no reported number while breaking
// comparability with historical journal entries. Only move it if the Opus tier
// itself is repriced.
const BASELINE_MODEL_ID = "anthropic/claude-opus-4.7";
// Hardcoded fallback: Claude Opus 4.7 pricing (per 1M tokens)
// Used when baseline model not found in dynamic pricing map
const BASELINE_INPUT_PRICE = 5.0;
const BASELINE_OUTPUT_PRICE = 25.0;
/**
* Select the primary model for a tier and build the RoutingDecision.
*
* Cost contract: the estimates here are raw token economics — no server
* margin, no minimum-payment floor. `calculateModelCost` below applies both
* to match the actual x402 charge, and hosts that display money (ClawRouter's
* proxy does) recompute through it rather than reading these fields. Folding
* the margin in here would double-apply it in every such host.
*/
export function selectModel(
tier: Tier,
confidence: number,
method: "rules" | "llm" | "portfolio",
reasoning: string,
tierConfigs: Record<Tier, TierConfig>,
modelPricing: Map<string, ModelPricing>,
estimatedInputTokens: number,
maxOutputTokens: number,
routingProfile?: "free" | "eco" | "auto" | "premium",
agenticScore?: number,
): RoutingDecision {
const tierConfig = tierConfigs[tier];
const model = tierConfig.primary;
const pricing = modelPricing.get(model);
let costEstimate: number;
if (pricing?.flatPrice !== undefined) {
costEstimate = pricing.flatPrice;
} else {
const inputPrice = pricing?.inputPrice ?? 0;
const outputPrice = pricing?.outputPrice ?? 0;
costEstimate =
(estimatedInputTokens / 1_000_000) * inputPrice + (maxOutputTokens / 1_000_000) * outputPrice;
}
// Baseline: what the anchor Opus tier would cost (see BASELINE_MODEL_ID)
const opusPricing = modelPricing.get(BASELINE_MODEL_ID);
const opusInputPrice = opusPricing?.inputPrice ?? BASELINE_INPUT_PRICE;
const opusOutputPrice = opusPricing?.outputPrice ?? BASELINE_OUTPUT_PRICE;
const baselineInput = (estimatedInputTokens / 1_000_000) * opusInputPrice;
const baselineOutput = (maxOutputTokens / 1_000_000) * opusOutputPrice;
const baselineCost = baselineInput + baselineOutput;
// Premium profile doesn't calculate savings (it's about quality, not cost)
const savings =
routingProfile === "premium"
? 0
: baselineCost > 0
? Math.max(0, (baselineCost - costEstimate) / baselineCost)
: 0;
return {
model,
tier,
confidence,
method,
reasoning,
costEstimate,
baselineCost,
savings,
...(agenticScore !== undefined && { agenticScore }),
};
}
/**
* Get the ordered fallback chain for a tier: [primary, ...fallbacks].
*/
export function getFallbackChain(tier: Tier, tierConfigs: Record<Tier, TierConfig>): string[] {
const config = tierConfigs[tier];
return [config.primary, ...config.fallback];
}
/**
* Calculate charge-accurate cost fields for a specific model (server margin
* and minimum-payment floor included — see the cost contract on selectModel).
* Hosts call this to display money and to recost a decision after a fallback.
*/
// Server-side margin applied to all x402 payments (must match blockrun server's MARGIN_PERCENT)
const SERVER_MARGIN_PERCENT = 5;
// Minimum payment enforced by CDP Facilitator (must match blockrun server's MIN_PAYMENT_USD)
const MIN_PAYMENT_USD = 0.001;
export function calculateModelCost(
model: string,
modelPricing: Map<string, ModelPricing>,
estimatedInputTokens: number,
maxOutputTokens: number,
routingProfile?: "free" | "eco" | "auto" | "premium",
): { costEstimate: number; baselineCost: number; savings: number } {
const pricing = modelPricing.get(model);
let costEstimate: number;
if (pricing?.flatPrice !== undefined) {
// Active promo: fixed cost per request
costEstimate = Math.max(pricing.flatPrice * (1 + SERVER_MARGIN_PERCENT / 100), MIN_PAYMENT_USD);
} else {
// Defensive: guard against undefined price fields (not just undefined pricing)
const inputPrice = pricing?.inputPrice ?? 0;
const outputPrice = pricing?.outputPrice ?? 0;
const inputCost = (estimatedInputTokens / 1_000_000) * inputPrice;
const outputCost = (maxOutputTokens / 1_000_000) * outputPrice;
// Include server margin + minimum payment to match actual x402 charge
costEstimate = Math.max(
(inputCost + outputCost) * (1 + SERVER_MARGIN_PERCENT / 100),
MIN_PAYMENT_USD,
);
}
// Baseline: what the anchor Opus tier would cost (see BASELINE_MODEL_ID)
const opusPricing = modelPricing.get(BASELINE_MODEL_ID);
const opusInputPrice = opusPricing?.inputPrice ?? BASELINE_INPUT_PRICE;
const opusOutputPrice = opusPricing?.outputPrice ?? BASELINE_OUTPUT_PRICE;
const baselineInput = (estimatedInputTokens / 1_000_000) * opusInputPrice;
const baselineOutput = (maxOutputTokens / 1_000_000) * opusOutputPrice;
const baselineCost = baselineInput + baselineOutput;
// Premium profile doesn't calculate savings (it's about quality, not cost)
const savings =
routingProfile === "premium"
? 0
: baselineCost > 0
? Math.max(0, (baselineCost - costEstimate) / baselineCost)
: 0;
return { costEstimate, baselineCost, savings };
}
/**
* Filter a model list to only those that support tool calling.
* When hasTools is false, returns the list unchanged.
* When all models lack tool calling support, returns the full list as a fallback
* (better to let the API error than produce an empty chain).
*/
export function filterByToolCalling(
models: string[],
hasTools: boolean,
supportsToolCalling: (modelId: string) => boolean,
): string[] {
if (!hasTools) return models;
const filtered = models.filter(supportsToolCalling);
return filtered.length > 0 ? filtered : models;
}
/**
* Filter a model list to only those that support vision (image inputs).
* When hasVision is false, returns the list unchanged.
* When all models lack vision support, returns the full list as a fallback
* (better to let the API error than produce an empty chain).
*/
export function filterByVision(
models: string[],
hasVision: boolean,
supportsVision: (modelId: string) => boolean,
): string[] {
if (!hasVision) return models;
const filtered = models.filter(supportsVision);
return filtered.length > 0 ? filtered : models;
}
/**
* Filter a model list to remove user-excluded models.
* When all models are excluded, returns the full list as a fallback
* (same safety pattern as filterByToolCalling/filterByVision).
*/
export function filterByExcludeList(models: string[], excludeList: Set<string>): string[] {
if (excludeList.size === 0) return models;
const filtered = models.filter((m) => !excludeList.has(m));
return filtered.length > 0 ? filtered : models;
}
/**
* Get the fallback chain filtered by context length.
* Only returns models that can handle the estimated total context.
*
* @param tier - The tier to get fallback chain for
* @param tierConfigs - Tier configurations
* @param estimatedTotalTokens - Estimated total context (input + output)
* @param getContextWindow - Function to get context window for a model ID
* @returns Filtered list of models that can handle the context
*/
export function getFallbackChainFiltered(
tier: Tier,
tierConfigs: Record<Tier, TierConfig>,
estimatedTotalTokens: number,
getContextWindow: (modelId: string) => number | undefined,
): string[] {
const fullChain = getFallbackChain(tier, tierConfigs);
// Filter to models that can handle the context
const filtered = fullChain.filter((modelId) => {
const contextWindow = getContextWindow(modelId);
if (contextWindow === undefined) {
// Unknown model - include it (let API reject if needed)
return true;
}
// Add 10% buffer for safety
return contextWindow >= estimatedTotalTokens * 1.1;
});
// If all models filtered out, return the original chain
// (let the API error out - better than no options)
if (filtered.length === 0) {
return fullChain;
}
return filtered;
}
/**
* Filter an already-ranked candidate list by total context and requested output.
* Unlike the legacy tier helper, this supports the V3 portfolio order.
*/
export function filterCandidatesByCapacity(
models: string[],
estimatedInputTokens: number,
requestedOutputTokens: number,
getCapabilities: (modelId: string) => { contextWindow: number; maxOutput: number } | undefined,
): string[] {
const filtered = models.filter((modelId) => {
const capabilities = getCapabilities(modelId);
if (!capabilities) return true;
return (
capabilities.contextWindow >= (estimatedInputTokens + requestedOutputTokens) * 1.1 &&
capabilities.maxOutput >= requestedOutputTokens
);
});
return filtered;
}