跳到內容
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
}
}

針對 6 種任務類型(coding、review、planning、analysis、debugging、documentation)為 30 多個模型評分。支援萬用字元模式(例如,*-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 DB 的快照
npm run test:combo:live:vps 對即時 OmniRoute 伺服器發出 HTTP 呼叫(設定 COMBO_LIVE_BASE_URL)
npm run test:combo:live:vps:failover 同上,但包含刻意設計的容錯移轉情境

這些冒煙測試會驗證真實的線路路徑(組合 → 提供者 → 完成)。由於需要即時憑證與 VPS 存取權限,因此刻意將其排除於 CI 之外。


檔案 用途
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 種權重設定檔(快速交付、省成本、品質優先、離線友善、可靠性優先、混沌模式)
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 是智慧代理程式開發工作台,結合結構化工作流程、多代理程式執行與 Hero Dungeon 介面,將想法化為交付成果。

以更聰明、更快速且更有趣的智慧代理程式工作流程,打造實用的軟體。

HagiCode 淺色主題介面畫面
  • Smart結構化流程將意圖轉化為從構想到交付的可執行步驟。
  • Efficient多代理程式工作流程讓研究、實作與審查並行進行。
  • FunHero Dungeon 讓長時間的程式協作更直覺、更有參與感。
造訪 HagiCode