跳转到内容
OmniRoute source

OmniRoute Auto-Combo Engine (中文 (简体))

2. cost / eco — 最便宜的健康提供者

Section titled “2. cost / eco — 最便宜的健康提供者”

按 costPer1MTokens 对候选池进行升序排序,并选择最便宜的候选项。 首先筛除状态为 OPEN 的候选项。

class CostStrategyImpl implements RouterStrategy {
readonly name = "cost";
readonly description = "Always selects cheapest available provider";
select(pool, context) {
const healthy = pool.filter((c) => c.circuitBreakerState !== "OPEN");
const sorted = [...healthy].sort((a, b) => a.costPer1MTokens - b.costPer1MTokens);
return { provider: sorted[0].provider /* ... */ };
}
}

适用场景:成本敏感型工作负载、批处理或后台任务。

别名:cost、eco


3. latency / fast — p95 延迟最低,并带有可靠性惩罚

Section titled “3. latency / fast — p95 延迟最低,并带有可靠性惩罚”

按 p95LatencyMs + (errorRate * 1000) 排序。错误率惩罚机制可确保不可靠的提供者排名更低,即使其标称延迟很低。

class LatencyStrategyImpl implements RouterStrategy {
readonly name = "latency";
readonly description = "Prioritizes lowest p95 latency with reliability weighting";
select(pool, context) {
const healthy = pool.filter((c) => c.circuitBreakerState !== "OPEN");
const sorted = [...healthy].sort(
(a, b) => a.p95LatencyMs + a.errorRate * 1000 - (b.p95LatencyMs + b.errorRate * 1000)
);
return { provider: sorted[0].provider /* ... */ };
}
}

适用场景:对延迟敏感的工作负载,例如实时聊天、自动补全或交互式编码助手。

别名:latency、fast


4. sla-aware / sla — 延迟/错误/成本 SLO 合规性

Section titled “4. sla-aware / sla — 延迟/错误/成本 SLO 合规性”

根据每个候选项满足已配置 SLO 策略的程度对其评分:

因素 权重 公式
延迟分数 35% threshold / max(value, ε)
错误分数 35% threshold / max(value, ε)
健康分数 15% 1.0(CLOSED)/ 0.5(HALF_OPEN)/ 0.0(OPEN)
成本分数 10% threshold / max(value, ε) 或反向归一化
稳定性分数 5% 延迟标准差的反向归一化值

当 hardConstraints: true 时,候选项首先按违规分数(超出任意 SLO 的程度)排序,然后按综合分数排序。否则仅按综合分数排序。

class SLAStrategyImpl implements RouterStrategy {
readonly name = "sla-aware";
readonly description =
"Selects the provider most likely to satisfy latency, error-rate, and cost SLOs";
select(pool, context) {
// ... 根据策略对每个候选项评分:{ targetP95Ms, maxErrorRate, maxCostPer1MTokens, hardConstraints }
}
}

SLA 字段(在组合配置中设置):

{
"strategy": "auto",
"config": {
"routerStrategy": "sla-aware",
"slaTargetP95Ms": 1500,
"slaMaxErrorRate": 0.05,
"slaMaxCostPer1MTokens": 5,
"slaHardConstraints": true
}
}

适用场景:具有严格延迟、错误率或成本预算的生产工作负载。

别名:sla-aware、sla


5. lkgp — 最后一个已知良好的提供者优先

Section titled “5. lkgp — 最后一个已知良好的提供者优先”

首先尝试最后一个已知良好的提供者(如果已设置),然后回退到 rules 策略。适用于会话粘性——由同一提供者处理对话中的后续请求。

class LKGPStrategyImpl implements RouterStrategy {
readonly name = "lkgp";
readonly description = "Tries last known good provider first, then falls back to rules";
select(pool, context) {
if (context.lkgpEnabled === false) {
return getStrategy("rules").select(pool, context);
}
if (context.lastKnownGoodProvider) {
const candidates = pool.filter(
(c) => c.provider === context.lastKnownGoodProvider && c.circuitBreakerState !== "OPEN"
);
if (candidates.length > 0) {
return { provider: candidates[0].provider /* ... */ };
}
}
// 回退到 rules 策略
return getStrategy("rules").select(pool, context);
}
}

适用场景:希望由同一提供者处理后续请求的多轮对话(例如,为了保持缓存、上下文连续性或定价一致性)。

别名:lkgp(无其他别名)


你可以通过公共 API 注册自己的 RouterStrategy 实现:

import {
registerStrategy,
type RouterStrategy,
} from "@omniroute/open-sse/services/autoCombo/routerStrategy";
class MyCustomStrategy implements RouterStrategy {
readonly name = "my-custom";
readonly description = "My custom routing strategy";
select(pool, context) {
// 在此处添加你的路由逻辑
return {
provider: pool[0].provider,
model: pool[0].model,
strategy: this.name,
reason: "MyCustomStrategy: ...",
candidatesConsidered: pool.length,
finalScore: 1.0,
};
}
}
registerStrategy("my-custom", new MyCustomStrategy());

然后使用它:

{
"strategy": "auto",
"config": {
"routerStrategy": "my-custom"
}
}

使用场景 策略 原因
均衡型工作负载 rules 默认策略——综合考虑所有因素
最小化成本 cost 始终选择最便宜的提供者
最小化延迟 latency 选择最快且可靠的提供者
严格的 SLO sla-aware 按 p95/错误率/成本阈值进行筛选
多轮聊天 lkgp 会话粘性

SLA 感知字段:

{
"strategy": "auto",
"config": {
"routerStrategy": "sla-aware",
"slaTargetP95Ms": 1500,
"slaMaxErrorRate": 0.05,
"slaMaxCostPer1MTokens": 5,
"slaHardConstraints": true
}
}

30 多个模型在 6 种任务类型(coding、review、planning、analysis、debugging、documentation)中进行评分。支持通配符模式(例如,*-coder → 较高的编码分数)。

包括不带变体的 auto(默认值),以及 autoPrefix.ts 中声明的 6 个 AutoVariant 值,共有 7 个可调用的模型 ID:

auto、auto/coding、auto/fast、auto/cheap、auto/offline、auto/smart、auto/lkgp

(AutoVariant 本身枚举了 6 个值;第 7 个选项是“不使用变体”——即不带后缀的 auto——由 parseAutoPrefix() 处理为 variant: undefined。)

16 因子评分函数(open-sse/services/autoCombo/scoring.ts)将层级归属视为两个信号:tierPriority(0.0476)和 tierAffinity(0.0476)。有关完整的 DEFAULT_WEIGHTS 集合,请参阅上方的规范评分因子表——各方案的覆盖配置(ship-fast/cost-saver/quality-first/ offline-friendly)列在“每种方案的权重配置”表中。

仅凭层级不会强制优先选择 Tier 1——如果 Tier 1 的延迟较高,或 成本与质量的权衡不理想,则 Tier 2 会胜出。若要强制按层级排序,请使用组合 策略 priority,并按层级排列提供者。

若要显著偏向 Tier 1(订阅),请提高 tierPriority 权重:

{
"strategy": "auto",
"config": { "auto": { "weights": { "tierPriority": 0.3, "costInv": 0.05 } } }
}

有关层级定义和提供者分类,请参阅 docs/marketing/TIERS.md。

确定性路由决策矩阵(npm run test:combo:matrix)

Section titled “确定性路由决策矩阵(npm run test:combo:matrix)”

tests/integration/combo-matrix/*.test.ts 通过模拟的上游,在真实组合管线中对全部 19 种 公开策略的路由决策进行端到端验证。 覆盖范围包括:

  • 全部 19 种 ROUTING_STRATEGY_VALUES 策略(ordered、weighted、cost、context、fusion 等)。
  • quota-share(内部)端到端测试:通过真实的 selectQuotaShareTarget 接缝(registerQuotaFetcher / setLKGP / __setHeadroomSaturationFetcherForTests),验证 DRR 公平性与饱和时降权。
  • 覆盖各种目标数量下的 context-relay 通用交接行为。

此测试套件在 CI(test:integration 作业)中使用 --test-concurrency=1 和 --test-force-exit 运行,因此具有确定性,且不需要实时凭据。

受控的实时冒烟测试(不在 CI 中——使用真实提供者)

Section titled “受控的实时冒烟测试(不在 CI 中——使用真实提供者)”
命令 功能
npm run test:combo:live 使用 RUN_COMBO_LIVE=1 进行进程内真实路由;创建实时 OmniRoute 数据库的快照
npm run test:combo:live:vps 对实时 OmniRoute 服务器发起 HTTP 调用(设置 COMBO_LIVE_BASE_URL)
npm run test:combo:live:vps:failover 同上,但包含刻意设计的故障转移场景

这些冒烟测试会验证真实的线缆路径(组合 → 提供者 → 补全)。它们被有意 排除在 CI 之外,因为需要实时凭据和 VPS 访问权限。


文件 用途
open-sse/services/autoCombo/scoring.ts 16 因子评分函数、DEFAULT_WEIGHTS、池归一化
open-sse/services/autoCombo/taskFitness.ts 模型 × 任务适配度查找
open-sse/services/autoCombo/engine.ts 选择逻辑、多臂老虎机、预算上限
open-sse/services/autoCombo/selfHealing.ts 排除、探测、事件模式
open-sse/services/autoCombo/modePacks.ts 6 个权重配置(ship-fast、cost-saver、quality-first、offline-friendly、reliability-first、chaos-mode)
open-sse/services/autoCombo/autoPrefix.ts auto/ 前缀解析器 + 6 种变体
open-sse/services/autoCombo/virtualFactory.ts 根据实时连接在内存中构建 AutoComboConfig
open-sse/services/autoCombo/providerRegistryAccessor.ts 用于模拟提供者注册表的测试钩子
src/shared/constants/routingStrategies.ts ROUTING_STRATEGY_VALUES(19 种策略)
src/sse/handlers/chat.ts 集成:auto-prefix 短路逻辑

OmniRoute 源码 (a58000c7685f)

HagiCode

HagiCode 是一套智能体编码工作台:结构化工作流、多 Agent 并行执行与 Hero Dungeon 视图,把想法变成真正交付的软件。

让想法更快变成好用的软件,让智能编码更聪明、更高效,也更有趣。

HagiCode 浅色主题主界面截图
  • Smart结构化工作流将意图转化为从想法到交付的可执行路径。
  • Efficient多 Agent 工作流让调研、实现与审阅并行推进。
  • FunHero Dungeon 让长时间编码协作更直观、更有参与感。
访问 HagiCode