Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion crates/tw-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,14 +402,27 @@ pub struct ProviderView {
pub redact_explicit: bool,
}

/// 一条规则。
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RouteView {
pub struct RuleView {
pub name: String,
pub to: String,
/// `when` 的人话摘要。空 = 兜底
pub conditions: Vec<String>,
}

/// 一条路由 —— 一组规则,加上它分给了谁。
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RouteView {
pub name: String,
/// 对所有密钥生效
pub default: bool,
/// 分到这条路由的密钥名。**默认路由这里是空的** —— 它不需要分配,
/// 而列出「所有密钥」会让人以为那是一次显式的分配。
pub clients: Vec<String>,
pub rules: Vec<RuleView>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GroupView {
pub name: String,
Expand Down
24 changes: 22 additions & 2 deletions crates/tw-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,12 @@ pub struct Config {
pub limits: Limits,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub groups: Vec<tw_engine::Group>,
/// 路由规则。同上,不写就是「按声明顺序故障转移」。
/// 路由。一条路由是一组按顺序求值的规则。
///
/// **不写就是「按声明顺序故障转移」** —— 那条默认路由由引擎合成,
/// 不写进文件(`generate_initial` 的六行里没有它)。
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub routes: Vec<tw_engine::Route>,
pub routes: Vec<tw_engine::RouteSet>,
}

/// 便于构造,**不代表一份可用的配置** —— `providers` 和 `clients` 都是
Expand Down Expand Up @@ -108,6 +111,7 @@ impl Default for Client {
key: String::new(),
max_concurrent: None,
allow: None,
routes: Vec::new(),
}
}
}
Expand Down Expand Up @@ -137,6 +141,14 @@ impl Config {
self.providers.iter().map(|p| p.name.clone()).collect(),
self.groups.clone(),
self.routes.clone(),
// 密钥 → 它分到的路由。**引擎不认识密钥这个概念** ——
// 它只需要「这个名字要过哪几条路由」,所以映射在这里拍平,
// 而不是把整个 `clients` 交进去。
self.clients
.iter()
.filter(|c| !c.routes.is_empty())
.map(|c| (c.name.clone(), c.routes.clone()))
.collect(),
)
}
}
Expand Down Expand Up @@ -311,6 +323,14 @@ pub struct Client {
/// - 写 `[]` → **一个都不给**。「临时禁用这个客户端」的正当用法
#[serde(default, skip_serializing_if = "Option::is_none")]
pub allow: Option<Vec<String>>,
/// 这把密钥额外走哪几条路由,按写的顺序求值。
///
/// **默认路由不用写在这里** —— 它对每一把密钥都生效。这里写的是
/// 「在默认之上,这把密钥还要过哪些规则」。
///
/// 一条路由可以分给多把密钥,不用复制。
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub routes: Vec<String>,
/// 网关密钥。`tw-` 前缀是刻意的:用户在客户端配置里看到它时,
/// 一眼就知道这不是某个上游的真 key。
pub key: String,
Expand Down
2 changes: 1 addition & 1 deletion crates/tw-config/src/reload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ mod tests {
fn a_route_pointing_at_a_deleted_group_is_caught_before_anything_swaps_in() {
// **语法正确但语义错误的配置最危险**:YAML 完全合法,运行时却会
// 把请求路由到空处。
let bad = "version: 1\nclients:\n - name: c\n key: tw-k\nproviders:\n - name: a\n base_url: https://x\n key: k\nroutes:\n - name: r\n to: 已经删掉的组\n";
let bad = "version: 1\nclients:\n - name: c\n key: tw-k\nproviders:\n - name: a\n base_url: https://x\n key: k\nroutes:\n - name: 默认\n default: true\n rules:\n - name: r\n to: 已经删掉的组\n";
let r = try_parse(bad).unwrap_err();
assert_eq!(r.stage, Stage::Semantics);
assert!(r.message.contains("已经删掉的组"), "{r:?}");
Expand Down
5 changes: 4 additions & 1 deletion crates/tw-control/src/dryrun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,11 @@ pub async fn dry_run(

// 每条规则的下场。**先走一遍这个,再问结果** —— 顺序反过来的话,
// 「命中了哪条」会变成唯一的输出,而那正是不够用的那半个答案。
// **只列这把密钥真的会过的规则。**试算问的是「我这个请求会怎么走」,
// 而分给别的密钥的规则对这个请求没有任何影响 —— 列出来只会让人以为
// 它们被跳过了,而实际上它们根本不在这条求值链上。
let mut trace = Vec::new();
for r in engine.routes() {
for r in engine.rules_for_client(&f.client) {
if r.when.is_phase_two() {
trace.push(tw_api::RuleTrace {
name: r.name.clone(),
Expand Down
36 changes: 25 additions & 11 deletions crates/tw-control/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,17 +250,31 @@ async fn overview(State(s): State<ControlState>) -> Json<tw_api::Overview> {
routes: engine
.routes()
.iter()
.map(|r| tw_api::RouteView {
name: r.name.clone(),
// 阶段二的规则没有去向 —— 它们只改参数或拒绝
to: r.to.clone().unwrap_or_else(|| {
if r.deny.is_some() {
"拒绝".into()
} else {
"(只改参数)".into()
}
}),
conditions: describe_when(&r.when),
.map(|set| tw_api::RouteView {
name: set.name.clone(),
default: set.default,
clients: cfg
.clients
.iter()
.filter(|c| c.routes.contains(&set.name))
.map(|c| c.name.clone())
.collect(),
rules: set
.rules
.iter()
.map(|r| tw_api::RuleView {
name: r.name.clone(),
// 阶段二的规则没有去向 —— 它们只改参数或拒绝
to: r.to.clone().unwrap_or_else(|| {
if r.deny.is_some() {
"拒绝".into()
} else {
"(只改参数)".into()
}
}),
conditions: describe_when(&r.when),
})
.collect(),
})
.collect(),
groups: engine
Expand Down
25 changes: 14 additions & 11 deletions crates/tw-control/tests/dryrun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@ groups:
type: load-balance
providers: [官方, 中转]
routes:
- name: 带缓存的必须走官方
when: { cache: true }
to: 官方
- name: 超长上下文降级
when: { input_tokens: ">200k" }
set: { model: claude-haiku-4-5 }
- name: 图片一律拒绝
when: { image: true }
deny: 这个上游不收图片
- name: 其余都试试
to: 都试试
- name: 默认
default: true
rules:
- name: 带缓存的必须走官方
when: { cache: true }
to: 官方
- name: 超长上下文降级
when: { input_tokens: ">200k" }
set: { model: claude-haiku-4-5 }
- name: 图片一律拒绝
when: { image: true }
deny: 这个上游不收图片
- name: 其余都试试
to: 都试试
"#;

fn app() -> (tempfile::TempDir, axum::Router) {
Expand Down
Loading
Loading