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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/tw-control/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ description = "Control-plane API server over a unix socket"

[dependencies]
tw-api = { workspace = true }
tw-types = { workspace = true }
# 列网卡用 getifaddrs —— 比引一个 crate 便宜
libc = { workspace = true }
tw-config = { workspace = true }
Expand Down Expand Up @@ -47,4 +48,5 @@ serde_yaml_ng = { workspace = true }
tempfile = "3"
tw-observe = { workspace = true }
tw-api = { workspace = true }
tw-types = { workspace = true }
tokio = { workspace = true, features = ["rt", "macros"] }
165 changes: 116 additions & 49 deletions crates/tw-control/src/chatgpt.rs

Large diffs are not rendered by default.

60 changes: 44 additions & 16 deletions crates/tw-control/src/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ use tw_adopt::clients::{Gateway, adoptable, manual_only};
use tw_adopt::{detect, plan};

use crate::ControlState;
use tw_types::msg;

type Fail = (StatusCode, String);
use crate::{Fail, fail};

fn find(id: &str) -> Result<tw_adopt::clients::Client, Fail> {
adoptable()
.into_iter()
.find(|c| c.id == id)
.ok_or_else(|| (StatusCode::NOT_FOUND, format!("未知的客户端 {id}")))
adoptable().into_iter().find(|c| c.id == id).ok_or_else(|| {
fail(
StatusCode::NOT_FOUND,
msg!("control.client_unknown", client = id => "`{client}` is not a client we know."),
)
})
}

/// 客户端该连的地址。
Expand Down Expand Up @@ -49,19 +52,26 @@ fn key_for(
.find(|c| c.name == n)
.cloned()
.ok_or_else(|| {
(
fail(
StatusCode::NOT_FOUND,
format!("config.yaml 中没有名为「{n}」的网关密钥"),
msg!(
"control.key_not_found", key = n =>
"config.yaml has no gateway key named `{key}`."
),
)
});
}
if let Some(c) = cfg.client_key(client) {
return Ok(c.clone());
}
cfg.default_client().cloned().ok_or_else(|| {
(
fail(
StatusCode::CONFLICT,
"config.yaml 中尚无网关密钥,请先创建网关密钥,再接管客户端".to_string(),
msg!(
"control.no_keys" =>
"config.yaml has no gateway key yet. Create one before pointing a client at the \
gateway."
),
)
})
}
Expand Down Expand Up @@ -122,7 +132,12 @@ async fn ensure_key(
)?)
})
.await
.map_err(|e| (StatusCode::CONFLICT, format!("无法创建网关密钥:{e}")))?;
.map_err(|e| {
fail(
StatusCode::CONFLICT,
msg!("control.key_create_failed", detail = e => "The gateway key could not be created: {detail}"),
)
})?;
Ok(Gateway {
base: gateway_base(s),
key: Some(key),
Expand All @@ -149,7 +164,12 @@ async fn bind(s: &ControlState, key: &str, client: &str) -> Result<(), Fail> {
)?)
})
.await
.map_err(|e| (StatusCode::CONFLICT, format!("无法记录密钥归属:{e}")))?;
.map_err(|e| {
fail(
StatusCode::CONFLICT,
msg!("control.key_bind_failed", detail = e => "The key's owner could not be recorded: {detail}"),
)
})?;
Ok(())
}

Expand Down Expand Up @@ -216,7 +236,7 @@ pub async fn list(State(s): State<ControlState>) -> Result<Json<tw_api::ClientsR
/// **界面上永远不显示真正的密钥**,diff 里也不行 —— 用户会截图这一屏
/// 来问「这样对吗」。落盘写的仍然是真值,[`tw_api::PlanView`] 上那两个
/// 字段的文档里写清了这一点。
const MASK: &str = "«config.yaml 中的网关密钥»";
const MASK: &str = "«the gateway key from config.yaml»";

fn mask(text: &str, key: Option<&str>) -> String {
match key {
Expand Down Expand Up @@ -298,7 +318,7 @@ fn bad(e: plan::PlanError) -> Fail {
plan::PlanError::ForeignSidecar { .. } => StatusCode::CONFLICT,
_ => StatusCode::BAD_REQUEST,
};
(code, e.to_string())
fail(code, msg!("control.adopt_failed", detail = e => "{detail}"))
}

/// 落盘。**用户在 diff 上点过确认之后才该到这里。**
Expand Down Expand Up @@ -346,7 +366,12 @@ pub async fn restore(
// ---------------------------------------------------------------- MCP 矩阵

fn mcp_target(id: &str) -> Result<tw_adopt::mcp::Target, Fail> {
tw_adopt::mcp::target(id).map_err(|e| (StatusCode::NOT_FOUND, e.to_string()))
tw_adopt::mcp::target(id).map_err(|e| {
fail(
StatusCode::NOT_FOUND,
msg!("control.mcp_target_unknown", detail = e => "{detail}"),
)
})
}

fn mcp_err(e: tw_adopt::mcp::McpError) -> Fail {
Expand All @@ -358,7 +383,7 @@ fn mcp_err(e: tw_adopt::mcp::McpError) -> Fail {
tw_adopt::mcp::McpError::NotCopyable { .. } => StatusCode::NOT_IMPLEMENTED,
_ => StatusCode::BAD_REQUEST,
};
(code, e.to_string())
fail(code, msg!("control.mcp_failed", detail = e => "{detail}"))
}

/// 能写和不能写的分别是哪些。
Expand Down Expand Up @@ -386,7 +411,10 @@ fn mcp_plan(s: &ControlState, req: &tw_api::McpOpRequest) -> Result<tw_adopt::mc
let v = tw_adopt::mcp::read_server(&from, &s.home, &req.name).map_err(mcp_err)?;
tw_adopt::mcp::plan_copy(&to, &s.home, &req.name, &v).map_err(mcp_err)
}
other => Err((StatusCode::BAD_REQUEST, format!("不支持的操作 {other}"))),
other => Err(fail(
StatusCode::BAD_REQUEST,
msg!("control.unsupported_action", action = other => "`{action}` is not an action we support."),
)),
}
}

Expand Down
58 changes: 35 additions & 23 deletions crates/tw-control/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ pub enum ApplyError {
#[error("{0}")]
Rejected(tw_config::Rejected),
/// 校验过了但运行时对象建不起来 —— 同样保持旧的。
#[error("配置可以解析,但无法应用:{0}")]
#[error("the configuration parses but cannot be applied: {0}")]
Build(String),
/// patch 指的那个位置有问题。**和 Build 分开**:那句「配置能读但
/// 用不起来」会让人去查配置,而该查的是这次请求写的路径。
#[error("{0}")]
BadPath(String),
#[error("配置版本不一致:本次修改基于 {base},当前版本为 {current}。请刷新后重新修改")]
#[error(
"version mismatch: this edit is based on {base}, and the current version is {current}. Refresh and edit again"
)]
Stale { base: String, current: String },
/// 按资源改(上游、代理、价目表)时的失败:名字撞了、找不到、值写不进去。
#[error(transparent)]
Expand Down Expand Up @@ -104,7 +106,9 @@ impl ConfigManager {
origin: origin.slug().to_string(),
at_ms: now_ms(),
});
tracing::warn!("配置未通过校验,继续使用上一版本:{r}");
tracing::warn!(
"the configuration did not validate; staying on the previous version: {r}"
);
ApplyError::Rejected(r)
})?;
self.gateway
Expand All @@ -115,7 +119,7 @@ impl ConfigManager {
// 着的 —— 于是「回到上一版」在列表上就是第二条,不用数。
let _ = tw_config::history::snapshot(&self.path, text, origin);
let version = store::version_of(text);
tracing::info!(%version, origin = origin.slug(), "配置已生效");
tracing::info!(%version, origin = origin.slug(), "the configuration is in effect");
self.bus.emit(tw_api::Event::ConfigReloaded {
id: self.bus.next_id(),
version: version.clone(),
Expand Down Expand Up @@ -193,7 +197,7 @@ impl ConfigManager {
.iter()
.rev()
.find(|v| v.version == version || v.version.ends_with(version))
.ok_or_else(|| ApplyError::BadPath(format!("版本历史中没有 {version}")))?;
.ok_or_else(|| ApplyError::BadPath(format!("the version history has no {version}")))?;
let text = tw_config::history::read(target)?;
let cur = self.current().ok();
// 回滚也走同一条写入路径,所以它同样会:校验、存历史、防回环。
Expand Down Expand Up @@ -229,10 +233,12 @@ pub fn spawn_watcher(
tokio::spawn(async move {
while rx.recv().await.is_some() {
match mgr.reload_from_disk().await {
Ok(Some(v)) => tracing::debug!(version = %v, "配置文件已被修改,已重新加载"),
Ok(None) => tracing::trace!("文件变更由本进程写入,忽略"),
Ok(Some(v)) => {
tracing::debug!(version = %v, "the configuration file changed and was reloaded")
}
Ok(None) => tracing::trace!("this process wrote that change; ignoring it"),
// 错误已经在 apply_text 里发过事件了,这里只记一行
Err(e) => tracing::debug!("重新加载失败:{e}"),
Err(e) => tracing::debug!("the reload failed: {e}"),
}
}
});
Expand All @@ -247,10 +253,12 @@ pub async fn path_at(
axum::extract::State(s): axum::extract::State<crate::ControlState>,
axum::extract::Query(q): axum::extract::Query<AtQuery>,
) -> Result<axum::Json<tw_api::ConfigAt>, crate::Fail> {
let cur = s
.cfg
.current()
.map_err(|e| crate::fail(axum::http::StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
let cur = s.cfg.current().map_err(|e| {
crate::fail(
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
tw_types::msg!("control.internal", detail = e => "{detail}"),
)
})?;
let path = tw_yaml::path_at(&cur.text, q.offset.min(cur.text.len()));
let mut out = tw_api::ConfigAt {
section: None,
Expand Down Expand Up @@ -322,7 +330,7 @@ pub fn resolve_path(text: &str, pointer: &str) -> Result<Vec<tw_yaml::Step>, Str
Some(i) => out.push(tw_yaml::Step::Index(i)),
None => {
return Err(format!(
"{pointer} 中没有名为「{seg}」的项。列表项按 name 查找,也可以使用下标"
"{pointer} has no entry named `{seg}`. List entries are found by name, or by index."
));
}
}
Expand Down Expand Up @@ -368,37 +376,41 @@ impl ConfigManager {
// **`insert` 而不是 `set`。**配置里绝大多数字段是可选的、
// 默认不写的,只能改「用户碰巧写过」的字段,等于表单模式在
// 他最需要的时候是死的。已经写过的走 `set`,那是它的第一步。
tw_yaml::insert(&text, &steps, &scalar)
.map_err(|e| ApplyError::BadPath(format!("修改 {path} 失败:{e}")))?
tw_yaml::insert(&text, &steps, &scalar).map_err(|e| {
ApplyError::BadPath(format!("{path} could not be changed: {e}"))
})?
}
tw_api::PatchOp::Append { path, item } => {
let steps = resolve_path(&text, path).map_err(ApplyError::BadPath)?;
tw_yaml::append(&text, &steps, item)
.map_err(|e| ApplyError::BadPath(format!("向 {path} 添加项失败:{e}")))?
tw_yaml::append(&text, &steps, item).map_err(|e| {
ApplyError::BadPath(format!("nothing could be added to {path}: {e}"))
})?
}
tw_api::PatchOp::Remove { path } => {
let steps = resolve_path(&text, path).map_err(ApplyError::BadPath)?;
// 路径指向那一项,最后一步就是它在列表里的位置。
// **按名字解析、按下标删** —— 名字是用户写的,下标是
// 我们刚刚算出来的,中间没有任何一次用户可见的重排。
let (last, parent) = steps.split_last().ok_or_else(|| {
ApplyError::BadPath(format!("{path} 没有指向列表中的某一项"))
ApplyError::BadPath(format!("{path} does not point at an entry of a list"))
})?;
let tw_yaml::Step::Index(i) = last else {
return Err(ApplyError::BadPath(format!(
"{path} 没有指向列表中的某一项。删除时需要指定具体的项,例如 /clients/codex"
"{path} does not point at an entry of a list. A delete names the entry, as in /clients/codex."
)));
};
tw_yaml::remove(&text, parent, *i)
.map_err(|e| ApplyError::BadPath(format!("删除 {path} 失败:{e}")))?
tw_yaml::remove(&text, parent, *i).map_err(|e| {
ApplyError::BadPath(format!("{path} could not be deleted: {e}"))
})?
}
tw_api::PatchOp::Clear { path } => {
// 要清的列表可能根本还没写进文件 —— 「一个都不给」
// 正是用户第一次碰 `allow` 的那一下。`resolve_path`
// 对没写过的键会原样留成 Key,走得通。
let steps = resolve_path(&text, path).map_err(ApplyError::BadPath)?;
tw_yaml::clear_seq(&text, &steps)
.map_err(|e| ApplyError::BadPath(format!("清空 {path} 失败:{e}")))?
tw_yaml::clear_seq(&text, &steps).map_err(|e| {
ApplyError::BadPath(format!("{path} could not be cleared: {e}"))
})?
}
};
}
Expand Down
Loading
Loading