Compare commits
7
Commits
v0.1.7
...
4e4d996a6f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4e4d996a6f | ||
|
|
25936457c9 | ||
|
|
6ebf1c3180 | ||
|
|
15d42c279c | ||
|
|
7218d1433b | ||
|
|
fa0ff339f6 | ||
|
|
93bd2693c5 |
No files matched your search
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"version": "0.1.6",
|
"version": "0.1.8",
|
||||||
"sha256": {
|
"sha256": {
|
||||||
"aarch64-apple-darwin": "52669aa98fd6d710cf78f5faf75530eaab00df0e1fafe74d36155ba6f2a1c999",
|
"aarch64-apple-darwin": "89479288ebac3e60981095c5e7ffc0057bd9b8ecb2a108d1e49d41f777bbba45",
|
||||||
"x86_64-unknown-linux-musl": "a1f140d6c114dce386ad676f8fdfdf89792f192cc47462a694da7b4bb5d95263"
|
"x86_64-unknown-linux-musl": "a1517f27cf9f57dbfb8e2072419a47c3b3d7e0c6fa13285e0152e5bd325f8831"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+22
-11
@@ -474,6 +474,17 @@ fn pricing(model: &str) -> Option<hx_ai::Pricing> {
|
|||||||
static COMPAT: std::sync::OnceLock<hx_ai::openai_compat::Config> = std::sync::OnceLock::new();
|
static COMPAT: std::sync::OnceLock<hx_ai::openai_compat::Config> = std::sync::OnceLock::new();
|
||||||
/// `codemode` from config: also offer the codemode tool to claude.
|
/// `codemode` from config: also offer the codemode tool to claude.
|
||||||
static CODEMODE: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
static CODEMODE: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||||
|
/// claude_cli sees only the `hx mcp-serve` tools: MCP is script-only with codemode, else `mcp__hx__*`.
|
||||||
|
fn via() -> hx_mcp::Via {
|
||||||
|
match (
|
||||||
|
CLAUDE_CLI.get().is_some(),
|
||||||
|
CODEMODE.get().copied().unwrap_or(false),
|
||||||
|
) {
|
||||||
|
(true, true) => hx_mcp::Via::Codemode,
|
||||||
|
(true, false) => hx_mcp::Via::ClaudeCli,
|
||||||
|
_ => hx_mcp::Via::Direct,
|
||||||
|
}
|
||||||
|
}
|
||||||
/// `claude_cli` from config: extra args for the `claude` binary.
|
/// `claude_cli` from config: extra args for the `claude` binary.
|
||||||
static CLAUDE_CLI: std::sync::OnceLock<Vec<String>> = std::sync::OnceLock::new();
|
static CLAUDE_CLI: std::sync::OnceLock<Vec<String>> = std::sync::OnceLock::new();
|
||||||
|
|
||||||
@@ -749,21 +760,21 @@ async fn main() -> Result<()> {
|
|||||||
.permit
|
.permit
|
||||||
.set(hx_core::approve::remote_permit(p.into()));
|
.set(hx_core::approve::remote_permit(p.into()));
|
||||||
}
|
}
|
||||||
|
// claude keeps its own tools; hx's MCP servers are added directly, or only through `codemode`
|
||||||
|
let cfg = Config::load(&cwd)?;
|
||||||
|
let mut servers =
|
||||||
|
hx_mcp::config::load(&context::hx_home(), &cwd).map_err(anyhow::Error::msg)?;
|
||||||
|
// a server entry that is hx itself would spawn mcp-serve recursively
|
||||||
|
servers.retain(|_, s| !s.args.iter().any(|a| a == "mcp-serve"));
|
||||||
|
let hub = Arc::new(hx_mcp::Hub::start(servers).await);
|
||||||
if std::env::var_os("HX_CODEMODE").is_some() {
|
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();
|
let mut all = hx_tools::registry();
|
||||||
hx_tools::web::register(&mut all, web_search(&cfg));
|
hx_tools::web::register(&mut all, web_search(&cfg));
|
||||||
hub.register(&mut all);
|
hub.register(&mut all);
|
||||||
all.add(hx_lens::LspTool::new(mgr.clone()));
|
all.add(hx_lens::LspTool::new(mgr.clone()));
|
||||||
reg.add(hx_tools::codemode::Codemode::new(&all));
|
reg.add(hx_tools::codemode::Codemode::new(&all));
|
||||||
|
} else {
|
||||||
|
hub.register(&mut reg);
|
||||||
}
|
}
|
||||||
hx_mcp::serve::serve(
|
hx_mcp::serve::serve(
|
||||||
reg,
|
reg,
|
||||||
@@ -917,7 +928,7 @@ async fn main() -> Result<()> {
|
|||||||
mcp.wait_all(std::time::Duration::from_secs(1)).await;
|
mcp.wait_all(std::time::Duration::from_secs(1)).await;
|
||||||
trace("mcp");
|
trace("mcp");
|
||||||
mcp.register(&mut agent.tools);
|
mcp.register(&mut agent.tools);
|
||||||
let idx = mcp.system_prompt();
|
let idx = mcp.system_prompt_for(via());
|
||||||
if !idx.is_empty() {
|
if !idx.is_empty() {
|
||||||
agent.append_system(&idx);
|
agent.append_system(&idx);
|
||||||
}
|
}
|
||||||
@@ -1151,7 +1162,7 @@ async fn main() -> Result<()> {
|
|||||||
.unwrap_or(context::DEFAULT_PROMPT),
|
.unwrap_or(context::DEFAULT_PROMPT),
|
||||||
context::prompt_tail(&context::hx_home(), &cwd)
|
context::prompt_tail(&context::hx_home(), &cwd)
|
||||||
);
|
);
|
||||||
let idx = mcp.system_prompt();
|
let idx = mcp.system_prompt_for(via());
|
||||||
if !idx.is_empty() {
|
if !idx.is_empty() {
|
||||||
sys.push_str("\n\n");
|
sys.push_str("\n\n");
|
||||||
sys.push_str(&idx);
|
sys.push_str(&idx);
|
||||||
|
|||||||
@@ -135,6 +135,16 @@ async fn load(cfg: &Server) -> (State, Option<Arc<Client>>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// How the model reaches the lazy MCP tools.
|
||||||
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum Via {
|
||||||
|
Direct,
|
||||||
|
/// claude_cli + codemode: only inside `codemode` scripts
|
||||||
|
Codemode,
|
||||||
|
/// claude_cli without codemode: `mcp__hx__*` tools served by `hx mcp-serve`
|
||||||
|
ClaudeCli,
|
||||||
|
}
|
||||||
|
|
||||||
impl Hub {
|
impl Hub {
|
||||||
/// Connect to all servers in parallel and wait for every one (CLI commands, tests).
|
/// Connect to all servers in parallel and wait for every one (CLI commands, tests).
|
||||||
pub async fn start(cfgs: BTreeMap<String, Server>) -> Self {
|
pub async fn start(cfgs: BTreeMap<String, Server>) -> Self {
|
||||||
@@ -395,10 +405,18 @@ impl Hub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn system_prompt(&self) -> String {
|
pub fn system_prompt(&self) -> String {
|
||||||
|
self.system_prompt_for(Via::Direct)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn system_prompt_for(&self, via: Via) -> String {
|
||||||
let idx = self.index();
|
let idx = self.index();
|
||||||
let mut s = String::new();
|
let mut s = String::new();
|
||||||
if !idx.is_empty() {
|
if !idx.is_empty() {
|
||||||
s.push_str("# MCP tools\nCall with `mcp_call` (server, tool, arguments). Use `mcp_describe` first to get the argument schema.\n");
|
s.push_str(match via {
|
||||||
|
Via::Codemode => "# MCP tools\nNot top-level tools: reach them only through `codemode`. Inside a script: `await tools.mcp_call({server, tool, arguments})`; `await tools.mcp_describe({server, tool})` gives the argument schema (also `tools.mcp_read({server, uri})`).\n",
|
||||||
|
Via::ClaudeCli => "# MCP tools\nCall with the `mcp__hx__mcp_call` tool (server, tool, arguments). Use `mcp__hx__mcp_describe` first to get the argument schema (`mcp__hx__mcp_read` reads resources).\n",
|
||||||
|
Via::Direct => "# MCP tools\nCall with `mcp_call` (server, tool, arguments). Use `mcp_describe` first to get the argument schema.\n",
|
||||||
|
});
|
||||||
for (_, b) in idx {
|
for (_, b) in idx {
|
||||||
s.push_str(&b);
|
s.push_str(&b);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,6 +36,16 @@ async fn stdio_lazy_index_describe_call() {
|
|||||||
let sp = h.system_prompt();
|
let sp = h.system_prompt();
|
||||||
assert!(sp.contains("- f.echo: Echo a message back\n"), "{sp}");
|
assert!(sp.contains("- f.echo: Echo a message back\n"), "{sp}");
|
||||||
assert!(!sp.contains("second line"));
|
assert!(!sp.contains("second line"));
|
||||||
|
let cm = h.system_prompt_for(crate::Via::Codemode);
|
||||||
|
assert!(
|
||||||
|
cm.contains("only through `codemode`") && cm.contains("- f.echo:"),
|
||||||
|
"{cm}"
|
||||||
|
);
|
||||||
|
assert!(!sp.contains("codemode"));
|
||||||
|
assert!(
|
||||||
|
h.system_prompt_for(crate::Via::ClaudeCli)
|
||||||
|
.contains("mcp__hx__mcp_call")
|
||||||
|
);
|
||||||
assert!(sp.contains("mem://notes") && sp.contains("Use echo for tests."));
|
assert!(sp.contains("mem://notes") && sp.contains("Use echo for tests."));
|
||||||
let st = h.status();
|
let st = h.status();
|
||||||
assert!(
|
assert!(
|
||||||
|
|||||||
@@ -86,7 +86,10 @@ impl Codemode {
|
|||||||
let mut names = BTreeMap::new();
|
let mut names = BTreeMap::new();
|
||||||
let mut list = String::new();
|
let mut list = String::new();
|
||||||
let mut hidden = 0;
|
let mut hidden = 0;
|
||||||
for s in reg.specs() {
|
// MCP tools first so builtins cannot push them out of the listing budget
|
||||||
|
let mut specs = reg.specs();
|
||||||
|
specs.sort_by_key(|s| !(s.name.contains("__") || s.name.starts_with("mcp_")));
|
||||||
|
for s in specs {
|
||||||
let id = ident(&s.name);
|
let id = ident(&s.name);
|
||||||
let first = s.description.split(". ").next().unwrap_or("").trim();
|
let first = s.description.split(". ").next().unwrap_or("").trim();
|
||||||
let line = format!("- tools.{id}({}): {first}\n", sig(&s.input_schema));
|
let line = format!("- tools.{id}({}): {first}\n", sig(&s.input_schema));
|
||||||
|
|||||||
@@ -902,13 +902,21 @@ fn on_mouse(app: &mut App, m: crossterm::event::MouseEvent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// The chat text as drawn, full height (for extracting a selection).
|
/// The chat text as drawn, full height (for extracting a selection).
|
||||||
fn chat_buf(app: &App) -> Buffer {
|
/// Off-screen chat render plus, per row, whether it continues a soft-wrapped line.
|
||||||
|
fn chat_buf(app: &App) -> (Buffer, Vec<bool>) {
|
||||||
let w = app.areas.chat.width;
|
let w = app.areas.chat.width;
|
||||||
let para = Paragraph::new(app.t.lines(app.expanded, w)).wrap(Wrap { trim: false });
|
let lines = app.t.lines(app.expanded, w);
|
||||||
|
let mut cont = vec![];
|
||||||
|
for l in &lines {
|
||||||
|
let n = Paragraph::new(l.clone()).wrap(Wrap { trim: false }).line_count(w).max(1);
|
||||||
|
cont.push(false);
|
||||||
|
cont.extend(std::iter::repeat(true).take(n - 1));
|
||||||
|
}
|
||||||
|
let para = Paragraph::new(lines).wrap(Wrap { trim: false });
|
||||||
let r = Rect::new(0, 0, w, para.line_count(w) as u16);
|
let r = Rect::new(0, 0, w, para.line_count(w) as u16);
|
||||||
let mut b = Buffer::empty(r);
|
let mut b = Buffer::empty(r);
|
||||||
Widget::render(para, r, &mut b);
|
Widget::render(para, r, &mut b);
|
||||||
b
|
(b, cont)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Put `t` on the clipboard (OSC 52) and say so in the footer.
|
/// Put `t` on the clipboard (OSC 52) and say so in the footer.
|
||||||
@@ -973,13 +981,13 @@ fn select_mouse(app: &mut App, m: crossterm::event::MouseEvent) {
|
|||||||
app.sel = Some(select::Sel { anchor: p, head: p });
|
app.sel = Some(select::Sel { anchor: p, head: p });
|
||||||
app.drag = true;
|
app.drag = true;
|
||||||
} else {
|
} else {
|
||||||
let b = chat_buf(app);
|
let (b, cont) = chat_buf(app);
|
||||||
let s = if n == 2 {
|
let s = if n == 2 {
|
||||||
select::word(&b, p.0, p.1)
|
select::word(&b, p.0, p.1)
|
||||||
} else {
|
} else {
|
||||||
select::line(&b, p.0)
|
select::line(&b, p.0)
|
||||||
};
|
};
|
||||||
let t = select::text(&b, &s);
|
let t = select::text(&b, &s, &cont);
|
||||||
app.sel = Some(s);
|
app.sel = Some(s);
|
||||||
copy(app, &t);
|
copy(app, &t);
|
||||||
}
|
}
|
||||||
@@ -993,7 +1001,8 @@ fn select_mouse(app: &mut App, m: crossterm::event::MouseEvent) {
|
|||||||
app.drag = false;
|
app.drag = false;
|
||||||
match app.sel {
|
match app.sel {
|
||||||
Some(s) if s.anchor != p => {
|
Some(s) if s.anchor != p => {
|
||||||
let t = select::text(&chat_buf(app), &s);
|
let (b, cont) = chat_buf(app);
|
||||||
|
let t = select::text(&b, &s, &cont);
|
||||||
copy(app, &t);
|
copy(app, &t);
|
||||||
}
|
}
|
||||||
_ => app.sel = None,
|
_ => app.sel = None,
|
||||||
|
|||||||
+35
-10
@@ -55,13 +55,15 @@ pub fn paint(buf: &mut Buffer, area: Rect, top: u16, s: &Sel) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Selected text from a buffer rendered at origin (0, 0); trailing blanks per row are dropped.
|
/// Selected text from a buffer rendered at origin (0, 0); trailing blanks per row are dropped.
|
||||||
pub fn text(buf: &Buffer, s: &Sel) -> String {
|
/// `cont[row]` = row is a soft-wrap continuation: joined to the previous row, not newline-split.
|
||||||
|
pub fn text(buf: &Buffer, s: &Sel, cont: &[bool]) -> String {
|
||||||
let (w, h) = (buf.area.width, buf.area.height);
|
let (w, h) = (buf.area.width, buf.area.height);
|
||||||
if w == 0 || h == 0 {
|
if w == 0 || h == 0 {
|
||||||
return String::new();
|
return String::new();
|
||||||
}
|
}
|
||||||
let (a, b) = s.span();
|
let (a, b) = s.span();
|
||||||
let mut rows = vec![];
|
let mut out = String::new();
|
||||||
|
let mut prev_full = false;
|
||||||
for row in a.0..=b.0.min(h - 1) {
|
for row in a.0..=b.0.min(h - 1) {
|
||||||
let Some((lo, hi)) = s.cols(row, w) else {
|
let Some((lo, hi)) = s.cols(row, w) else {
|
||||||
continue;
|
continue;
|
||||||
@@ -72,9 +74,22 @@ pub fn text(buf: &Buffer, s: &Sel) -> String {
|
|||||||
line.push_str(sym);
|
line.push_str(sym);
|
||||||
x += Span::raw(sym).width().max(1) as u16; // skip the trailing cell of wide chars
|
x += Span::raw(sym).width().max(1) as u16; // skip the trailing cell of wide chars
|
||||||
}
|
}
|
||||||
rows.push(line.trim_end().to_string());
|
let line = line.trim_end();
|
||||||
|
let full = Span::raw(line).width() as u16 >= w; // word broken mid-token
|
||||||
|
if row > a.0 {
|
||||||
|
if cont.get(row as usize) == Some(&true) {
|
||||||
|
// ponytail: wrap at a space leaves no trace; assume one unless the row was full
|
||||||
|
if !prev_full {
|
||||||
|
out.push(' ');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
out.push('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out.push_str(line);
|
||||||
|
prev_full = full;
|
||||||
}
|
}
|
||||||
rows.join("\n")
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_word(sym: &str) -> bool {
|
fn is_word(sym: &str) -> bool {
|
||||||
@@ -151,28 +166,38 @@ mod tests {
|
|||||||
anchor: (0, 6),
|
anchor: (0, 6),
|
||||||
head: (1, 5),
|
head: (1, 5),
|
||||||
};
|
};
|
||||||
assert_eq!(text(&b, &fwd), "world\nsecond");
|
assert_eq!(text(&b, &fwd, &[]), "world\nsecond");
|
||||||
let rev = Sel {
|
let rev = Sel {
|
||||||
anchor: (1, 5),
|
anchor: (1, 5),
|
||||||
head: (0, 6),
|
head: (0, 6),
|
||||||
};
|
};
|
||||||
assert_eq!(text(&b, &rev), "world\nsecond");
|
assert_eq!(text(&b, &rev, &[]), "world\nsecond");
|
||||||
let wide = Sel {
|
let wide = Sel {
|
||||||
anchor: (1, 7),
|
anchor: (1, 7),
|
||||||
head: (1, 10),
|
head: (1, 10),
|
||||||
};
|
};
|
||||||
assert_eq!(text(&b, &wide), "日本");
|
assert_eq!(text(&b, &wide, &[]), "日本");
|
||||||
let past = Sel {
|
let past = Sel {
|
||||||
anchor: (2, 0),
|
anchor: (2, 0),
|
||||||
head: (9, 3),
|
head: (9, 3),
|
||||||
};
|
};
|
||||||
assert_eq!(text(&b, &past), "third");
|
assert_eq!(text(&b, &past, &[]), "third");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn soft_wrap_rows_join_without_newline() {
|
||||||
|
let b = buf("hello\nworld\nnext", 10);
|
||||||
|
let s = Sel {
|
||||||
|
anchor: (0, 0),
|
||||||
|
head: (2, 9),
|
||||||
|
};
|
||||||
|
assert_eq!(text(&b, &s, &[false, true, false]), "hello world\nnext");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn word_and_line_pick() {
|
fn word_and_line_pick() {
|
||||||
let b = buf("see src/main.rs now", 30);
|
let b = buf("see src/main.rs now", 30);
|
||||||
assert_eq!(text(&b, &word(&b, 0, 6)), "src/main.rs");
|
assert_eq!(text(&b, &word(&b, 0, 6), &[]), "src/main.rs");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
word(&b, 0, 3),
|
word(&b, 0, 3),
|
||||||
Sel {
|
Sel {
|
||||||
@@ -180,7 +205,7 @@ mod tests {
|
|||||||
head: (0, 3)
|
head: (0, 3)
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
assert_eq!(text(&b, &line(&b, 0)), "see src/main.rs now");
|
assert_eq!(text(&b, &line(&b, 0), &[]), "see src/main.rs now");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Reference in new issue
Block a user