claude_cli: offer codemode to claude (codemode = true), hx tools inside it ask via the permission bridge

This commit is contained in:
hermes committed 2026-10-16 20:42:10 +00:00
1 parent 785effef11
commit fdeaaf5cf2
4 files changed
+111 -27

No files matched your search

+37 -9
View File
@@ -454,6 +454,8 @@ fn pricing(model: &str) -> Option<hx_ai::Pricing> {
/// `[openai_compat]` from config, set once at startup.
static COMPAT: std::sync::OnceLock<hx_ai::openai_compat::Config> = std::sync::OnceLock::new();
/// `codemode` from config: also offer the codemode tool to claude.
static CODEMODE: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
/// `claude_cli` from config: extra args for the `claude` binary.
static CLAUDE_CLI: std::sync::OnceLock<Vec<String>> = std::sync::OnceLock::new();
@@ -464,11 +466,14 @@ fn provider(k: Kind) -> hx_ai::Result<Arc<dyn Provider>> {
hx_ai::claude_cli::ClaudeCli::new(CLAUDE_CLI.get().cloned().unwrap_or_default());
let c = c.with_spawner(Arc::new(|bin| Box::pin(claude_command(bin))));
Arc::new(match std::env::current_exe() {
Ok(e) => c.with_mcp(
e.to_string_lossy().into(),
vec!["mcp-serve".into()],
serde_json::json!({ hx_core::approve::ENV: hx_core::approve::sock_path() }),
),
Ok(e) => c.with_mcp(e.to_string_lossy().into(), vec!["mcp-serve".into()], {
let mut env =
serde_json::json!({ hx_core::approve::ENV: hx_core::approve::sock_path() });
if CODEMODE.get().copied().unwrap_or(false) {
env["HX_CODEMODE"] = "1".into();
}
env
}),
Err(_) => c,
})
}
@@ -708,13 +713,35 @@ async fn main() -> Result<()> {
));
let mut reg = hx_core::tool::Registry::default();
reg.add(hx_lens::LspTool::new(mgr.clone()));
if let Some(p) = std::env::var_os(hx_core::approve::ENV) {
reg.add(hx_core::approve::Approve(p.into()));
}
let cx = ToolCtx {
cwd,
cwd: cwd.clone(),
state: Default::default(),
};
let sock = std::env::var_os(hx_core::approve::ENV);
if let Some(p) = &sock {
reg.add(hx_core::approve::Approve(p.into()));
// tools run by codemode ask hx for permission like any other
let _ = cx
.state
.permit
.set(hx_core::approve::remote_permit(p.into()));
}
if std::env::var_os("HX_CODEMODE").is_some() {
// claude keeps its own tools; only `codemode` is added, and scripts may call the hx tools
let cfg = Config::load(&cwd)?;
let hub = Arc::new(
hx_mcp::Hub::start(
hx_mcp::config::load(&context::hx_home(), &cwd)
.map_err(anyhow::Error::msg)?,
)
.await,
);
let mut all = hx_tools::registry();
hx_tools::web::register(&mut all, web_search(&cfg));
hub.register(&mut all);
all.add(hx_lens::LspTool::new(mgr.clone()));
reg.add(hx_tools::codemode::Codemode::new(&all));
}
hx_mcp::serve::serve(
reg,
cx,
@@ -749,6 +776,7 @@ async fn main() -> Result<()> {
&& hx_ai::claude_cli::ClaudeCli::available())
.then(Vec::new),
};
let _ = CODEMODE.set(cfg.codemode.unwrap_or(false));
if let Some(a) = cli_args {
let _ = CLAUDE_CLI.set(a);
}