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
71 changes: 56 additions & 15 deletions crates/tw-adopt/src/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ pub fn adoptable() -> Vec<Client> {
Client {
id: "zed",
name: "Zed",
config: ".config/zed/settings.json",
config: crate::paths::zed_settings(),
format: Format::Json,
takes_effect: TakesEffect::Immediately,
shadowed_by: &[],
Expand All @@ -298,7 +298,7 @@ pub fn adoptable() -> Vec<Client> {
"Zed keeps its key outside the configuration file, so it has to be filled in once in Zed's settings.",
)],
verified: Verified::FieldsOnly,
marker: &[".config/zed"],
marker: &[crate::paths::ZED_DIR],
process: &["Zed"],
env_vars: &[],
key_elsewhere: Some((
Expand Down Expand Up @@ -346,7 +346,7 @@ pub struct ManualOnly {
/// 为它生成专用密钥时用的标识:`cursor` / `continue` / `gemini-cli`
pub id: &'static str,
pub name: &'static str,
/// 这个客户端的码前缀:每一步是 `<prefix>.<后缀>`,提醒是 `<prefix>.caveat`
/// 这个客户端的码前缀:每一步是 `<prefix>.<后缀>`,提醒也是 `<prefix>.<后缀>`(见 `caveat`)
code: &'static str,
/// 手动配置的几步,每步「码的后缀,英文原句」。
///
Expand All @@ -356,7 +356,9 @@ pub struct ManualOnly {
steps: &'static [(&'static str, &'static str)],
/// 填带 `/v1` 的地址还是不带的
v1: bool,
caveat: &'static str,
/// 提醒,「码的后缀,英文原句」。**后缀也可以按平台分** —— 两个平台
/// 说的不是一句话时,用各自的码,界面才能各翻各的
caveat: (&'static str, &'static str),
}

impl ManualOnly {
Expand Down Expand Up @@ -384,10 +386,11 @@ impl ManualOnly {
/// 接管不了的那一句提醒。**必须和步骤一起给** —— 只说怎么配、不说
/// 配完还漏什么,等于说了假话
pub fn caveat(&self) -> Msg {
let (suffix, text) = self.caveat;
Msg {
code: format!("{}.caveat", self.code),
code: format!("{}.{suffix}", self.code),
args: BTreeMap::new(),
text: self.caveat.to_string(),
text: text.to_string(),
}
}
}
Expand All @@ -410,14 +413,20 @@ pub fn manual_only() -> Vec<ManualOnly> {
),
],
v1: true,
caveat: "Tab completion and inline edit still go to Cursor's own service rather than the gateway, so only part of Cursor is covered.",
caveat: (
"caveat",
"Tab completion and inline edit still go to Cursor's own service rather than the gateway, so only part of Cursor is covered.",
),
},
ManualOnly {
id: "continue",
name: "Continue",
code: "adopt.manual.continue",
steps: &[
#[cfg(not(windows))]
("open", "Open ~/.continue/config.yaml."),
#[cfg(windows)]
("open_windows", r"Open %USERPROFILE%\.continue\config.yaml."),
(
"entry",
"Add an entry to the models list with provider set to openai, apiBase set to the gateway address and apiKey set to the key.",
Expand All @@ -428,24 +437,47 @@ pub fn manual_only() -> Vec<ManualOnly> {
// 改写,不是替换一个标量。我们的 YAML 补丁只做后者
// (见 crate::yaml 开头那段)。宁可少接管一个客户端,也不
// 要写一段我们自己没把握的结构。
caveat: "This needs a new entry in the models list, which is not written automatically; follow the steps above.",
caveat: (
"caveat",
"This needs a new entry in the models list, which is not written automatically; follow the steps above.",
),
},
ManualOnly {
id: "gemini-cli",
name: "Gemini CLI",
code: "adopt.manual.gemini_cli",
// **Windows 上没有 shell 配置文件可 export**:用户级环境变量用 setx
// 写,写完只对之后打开的终端生效。两个平台各用各的码
#[cfg(not(windows))]
steps: &[
(
"export",
"In the shell configuration, export GOOGLE_GEMINI_BASE_URL set to the gateway address and GEMINI_API_KEY set to the key.",
),
("reopen", "Then reopen the terminal."),
],
#[cfg(windows)]
steps: &[
(
"setx",
"In a terminal, run setx GOOGLE_GEMINI_BASE_URL followed by the gateway address, and setx GEMINI_API_KEY followed by the key.",
),
("reopen", "Then reopen the terminal."),
],
v1: false,
// 它只认环境变量,没有可写的配置字段。改 .zshrc 超出了
// 「只改 endpoint 和 key 字段」的边界 ——
// **报告是我们的职责,修改是他的权利。**
caveat: "Gemini CLI reads the endpoint only from the environment. ThinkWatch does not edit shell configuration files, so add it by hand.",
#[cfg(not(windows))]
caveat: (
"caveat",
"Gemini CLI reads the endpoint only from the environment. ThinkWatch does not edit shell configuration files, so add it by hand.",
),
#[cfg(windows)]
caveat: (
"caveat_windows",
"Gemini CLI reads the endpoint only from the environment. ThinkWatch does not change environment variables, so add them by hand.",
),
},
]
}
Expand Down Expand Up @@ -592,7 +624,7 @@ impl Client {
/// **没检测到它的时候也要给。**配置文件不在默认位置、或者装在别的
/// 用户目录下时,检测不到不等于用不了 —— 照着做一样能接上。
pub fn manual_steps(&self) -> Vec<Msg> {
let file = format!("~/{}", self.config);
let file = crate::paths::shown(self.config);
let mut out = vec![Msg {
code: "adopt.manual.file".into(),
args: BTreeMap::from([("file".to_string(), file.clone())]),
Expand Down Expand Up @@ -620,10 +652,13 @@ impl Client {
}

pub fn config_path(&self, home: &std::path::Path) -> PathBuf {
home.join(self.config)
crate::paths::under(home, self.config)
}
pub fn shadow_paths(&self, home: &std::path::Path) -> Vec<PathBuf> {
self.shadowed_by.iter().map(|p| home.join(p)).collect()
self.shadowed_by
.iter()
.map(|p| crate::paths::under(home, p))
.collect()
}
/// 注释前缀。JSON 没有 —— 那时哨兵走旁文件。
pub fn comment_prefix(&self) -> Option<&'static str> {
Expand Down Expand Up @@ -726,9 +761,9 @@ mod tests {
let m = manual_only();
let cursor = m.iter().find(|c| c.name == "Cursor").unwrap();
assert!(
cursor.caveat.contains("Tab completion"),
cursor.caveat.1.contains("Tab completion"),
"{}",
cursor.caveat
cursor.caveat.1
);
// 地址是真实的地址,而不是一个让用户自己去找的说法;**不在句子里**,
// 界面单独给它一个复制按钮
Expand Down Expand Up @@ -802,7 +837,13 @@ mod tests {
}
for c in adoptable() {
let steps = c.manual_steps();
assert_eq!(steps[0].arg("file"), format!("~/{}", c.config), "{}", c.id);
// 写法按平台(`~/…` 或 `%USERPROFILE%\…`),各自的样子见 paths 里那条测试
assert_eq!(
steps[0].arg("file"),
crate::paths::shown(c.config),
"{}",
c.id
);
assert!(!edits(&c, &gw).is_empty(), "{}:没有要写的字段", c.id);
}
// Zed 的密钥不在配置文件里,多一步在它自己的设置里填
Expand Down
8 changes: 6 additions & 2 deletions crates/tw-adopt/src/detect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ pub fn detect_one(c: &Client, home: &Path) -> Detected {
Detected {
id: c.id,
name: c.name,
installed: c.marker.iter().any(|m| home.join(m).exists()) || text.is_some(),
installed: c
.marker
.iter()
.any(|m| crate::paths::under(home, m).exists())
|| text.is_some(),
has_config: text.is_some(),
adopted_at_ms: rec
.filter(|r: &SidecarRecord| r.client == c.id)
Expand Down Expand Up @@ -573,7 +577,7 @@ pub fn diagnose(c: &Client, home: &Path, project: Option<&Path>) -> Vec<Finding>

// 三、项目级配置盖住了用户级
if let Some(proj) = project {
let local = proj.join(c.config);
let local = crate::paths::under(proj, c.config);
if local.exists() {
out.push(Finding {
level: Level::Suspect,
Expand Down
4 changes: 2 additions & 2 deletions crates/tw-adopt/src/mcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub fn targets() -> Vec<Target> {
Target {
client: "zed",
name: "Zed",
config: ".config/zed/settings.json",
config: crate::paths::zed_settings(),
format: Format::Json,
key: "context_servers",
copyable: false,
Expand All @@ -170,7 +170,7 @@ pub fn target(client: &str) -> Result<Target, McpError> {

impl Target {
pub fn path(&self, home: &Path) -> PathBuf {
home.join(self.config)
crate::paths::under(home, self.config)
}
fn check(&self) -> Result<(), McpError> {
if self.copyable {
Expand Down
80 changes: 79 additions & 1 deletion crates/tw-adopt/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,57 @@
//!
//! 机器级的那一个(管理策略)没有 home 可言,它读环境变量。

use std::path::PathBuf;
use std::path::{Path, PathBuf};

/// 把一条用 `/` 写的相对路径接到 `base` 下面。
///
/// **按 `/` 拆开逐段接,不直接 `join`。**这些相对路径在源码里一律用 `/` 写,
/// 直接 `base.join(".claude/settings.json")` 在 Windows 上得到的是
/// `C:\Users\x\.claude/settings.json` —— 文件照样找得到(Windows 两种分隔符
/// 都认),但界面上显示的就是这么一串正反斜杠混着的路径。逐段接出来的是
/// 那个平台自己的写法。
pub fn under(base: &Path, rel: &str) -> PathBuf {
rel.split('/')
.filter(|c| !c.is_empty())
.fold(base.to_path_buf(), |p, c| p.join(c))
}

/// 给人看的写法:「打开 … 」那一步里的路径。
///
/// macOS 上是 `~/.claude/settings.json`;Windows 上没有 `~`,写成资源管理器
/// 地址栏里能直接粘贴的 `%USERPROFILE%\.claude\settings.json`。
pub fn shown(rel: &str) -> String {
#[cfg(windows)]
{
format!(r"%USERPROFILE%\{}", rel.replace('/', r"\"))
}
#[cfg(not(windows))]
{
format!("~/{rel}")
}
}

/// Zed 的设置文件,相对 home。
///
/// Windows 上的 Zed 按那个平台的习惯放在漫游的 AppData 下,不是 `~/.config`。
pub fn zed_settings() -> &'static str {
#[cfg(windows)]
{
"AppData/Roaming/Zed/settings.json"
}
#[cfg(not(windows))]
{
".config/zed/settings.json"
}
}

/// Zed 装过的痕迹:它的设置目录。**是常量不是函数**:它要放进
/// `marker: &[…]` 那个静态切片里,函数的返回值进不去。
pub const ZED_DIR: &str = if cfg!(windows) {
"AppData/Roaming/Zed"
} else {
".config/zed"
};

/// Claude Desktop 的配置,相对 home。
///
Expand Down Expand Up @@ -74,6 +124,34 @@ mod tests {
);
}

/// 接出来的每一段都是一个组件:没有哪一段里还夹着 `/`。
#[test]
fn a_relative_path_is_joined_one_component_at_a_time() {
let p = under(Path::new("/h"), ".claude/settings.json");
let parts: Vec<_> = p.components().map(|c| c.as_os_str().to_owned()).collect();
assert_eq!(parts.last().unwrap(), "settings.json");
assert_eq!(parts[parts.len() - 2], ".claude");
assert!(
parts
.iter()
.all(|c| !c.to_string_lossy().contains('/') || c == "/")
);
assert_eq!(
under(Path::new("/h"), "a//b/"),
Path::new("/h").join("a").join("b")
);
}

#[test]
fn the_shown_path_is_written_the_platform_way() {
let s = shown(".claude/settings.json");
if cfg!(windows) {
assert_eq!(s, r"%USERPROFILE%\.claude\settings.json");
} else {
assert_eq!(s, "~/.claude/settings.json");
}
}

/// 机器级的那一个反过来:必须是绝对的。
#[test]
fn the_managed_policy_is_machine_wide() {
Expand Down
5 changes: 4 additions & 1 deletion crates/tw-control/tests/adopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,10 @@ async fn every_client_says_how_to_connect_it_by_hand() {

let cc = v.clients.iter().find(|c| c.id == "claude-code").unwrap();
assert_eq!(cc.manual.endpoint, base, "Claude Code 要的地址不带 /v1");
assert_eq!(cc.manual.steps[0].arg("file"), "~/.claude/settings.json");
assert_eq!(
cc.manual.steps[0].arg("file"),
tw_adopt::paths::shown(".claude/settings.json")
);
let url = cc
.manual
.fields
Expand Down
Loading
Loading