From 10b22f5774d6ac052a55b286d853aed9c8881463 Mon Sep 17 00:00:00 2001 From: hermes Date: Fri, 9 Oct 2026 13:59:40 +0000 Subject: [PATCH] hx config init: write a commented sample config --- README.md | 2 +- crates/hx-cli/src/config.sample.toml | 43 ++++++++++++++++++++++ crates/hx-cli/src/main.rs | 53 ++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 crates/hx-cli/src/config.sample.toml diff --git a/README.md b/README.md index 4f86bcc..547f4e0 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ Lens findings and affected-test results appear after each turn as a `┄ lens `/help`, `/skills`, `/lsp`, `/mcp` open in a side panel (`esc` closes). `NO_COLOR` disables colour, `HX_ASCII=1` uses ASCII symbols. Messages typed while the agent works are queued. -Config: `~/.hx/config.toml`, overridden per project by `.hx/config.toml`: +Config: `~/.hx/config.toml`, overridden per project by `.hx/config.toml`. `hx config init [--project] [--force]` writes a fully commented sample with every key. Example: ```toml provider = "claude" # or "codex" diff --git a/crates/hx-cli/src/config.sample.toml b/crates/hx-cli/src/config.sample.toml new file mode 100644 index 0000000..01131b0 --- /dev/null +++ b/crates/hx-cli/src/config.sample.toml @@ -0,0 +1,43 @@ +## hx config: ~/.hx/config.toml (global) or .hx/config.toml (project, overrides global). +## Every setting is commented out and shows its default or an example; uncomment to change it. + +# provider = "claude" # claude | codex | custom; default: the first logged-in provider +# model = "claude-sonnet-5-5" # model for the chosen provider +# claude_model = "claude-sonnet-5-5" # default: the account's latest Sonnet +# codex_model = "gpt-5.6-sol" # default: Codex's top-listed model +# explore_model = "claude-haiku-4-5" # read-only `task` subagents; default: cheapest tier of the provider +# remember_model = true # reuse the last model picked with /model or /provider +# compact_at = 900000 # auto-compact at this many tokens; default: 90% of the context window +# max_tokens = 16384 +# permissions = "ask" # ask | yolo (bash/write/edit run without a prompt) +# mouse = true # wheel scrolling; false keeps terminal text selection +# codemode = false # `codemode` tool: JS scripts that call other tools in a sandbox +# system_prompt = "..." # replaces the built-in prompt; AGENTS.md is still appended + +## OpenAI-compatible endpoint (Ollama, vLLM, LM Studio, OpenRouter), used with provider = "custom". +# [openai_compat] +# base_url = "http://localhost:11434/v1" +# api_key_env = "OPENROUTER_API_KEY" # name of the env var holding the key; the key never goes here +# models = [] # fixed list; empty = ask GET {base_url}/models +# context_window = 128000 # for models that don't report one + +# [lsp] +# mode = "turn_end" # turn_end | agent_end | off +# tests = "turn_end" # affected tests: turn_end | agent_end | off +# disable = [] # built-in server ids, e.g. ["clangd"] +# enable_runners = [] # opt-in (slow): ["clippy"] +# disable_runners = [] # ruff, biome, eslint, ast-grep, rustfmt, ruff-format, biome-format + +## Add or override an LSP server (same id replaces a built-in). +# [[lsp.servers]] +# id = "zls" +# language = "zig" +# extensions = ["zig"] +# command = ["zls"] +# roots = ["build.zig"] +# install = ["brew", "install", "zls"] # offered by the lsp tool when `zls` is missing + +## Subprocess extensions (JSON-RPC over stdio); global and project lists are concatenated. +# [[extensions]] +# command = ["node", "my-ext.js"] +# timeout_secs = 10 diff --git a/crates/hx-cli/src/main.rs b/crates/hx-cli/src/main.rs index 350858e..4c5a363 100644 --- a/crates/hx-cli/src/main.rs +++ b/crates/hx-cli/src/main.rs @@ -74,8 +74,28 @@ enum Cmd { }, /// Serve lens diagnostics + LSP navigation as an MCP server over stdio. McpServe, + /// Config file helpers. + Config { + #[command(subcommand)] + action: ConfigAction, + }, } +#[derive(Subcommand)] +enum ConfigAction { + /// Write a commented sample config to ~/.hx/config.toml (or .hx/config.toml with --project). + Init { + #[arg(long)] + project: bool, + /// Replace an existing file. + #[arg(long)] + force: bool, + }, +} + +/// Every key commented out; `sample_config_parses` keeps it in sync with `Config`. +const SAMPLE_CONFIG: &str = include_str!("config.sample.toml"); + #[derive(Subcommand)] enum McpAction { /// OAuth login for an HTTP MCP server from mcp.json. @@ -510,6 +530,24 @@ async fn main() -> Result<()> { print!("{}", hub.system_prompt()); return Ok(()); } + Some(Cmd::Config { + action: ConfigAction::Init { project, force }, + }) => { + let p = if project { + std::env::current_dir()?.join(".hx/config.toml") + } else { + context::hx_home().join("config.toml") + }; + if p.exists() && !force { + anyhow::bail!("{} exists; use --force to replace it", p.display()); + } + if let Some(d) = p.parent() { + std::fs::create_dir_all(d)?; + } + std::fs::write(&p, SAMPLE_CONFIG)?; + println!("wrote {}", p.display()); + return Ok(()); + } Some(Cmd::McpServe) => { let cwd = std::env::current_dir()?; let mgr = Arc::new(hx_lens::manager::Manager::new( @@ -930,3 +968,18 @@ mod tests { assert_eq!(pricing("gpt-5.6-terra").unwrap().input, 2.5); } } + +#[cfg(test)] +mod config_tests { + #[test] + fn sample_config_parses() { + let s = super::SAMPLE_CONFIG; + toml::from_str::(s).expect("commented sample"); + let on: String = s + .lines() + .map(|l| l.strip_prefix("# ").unwrap_or(l)) + .collect::>() + .join("\n"); + toml::from_str::(&on).expect("uncommented sample must match Config"); + } +} -- 2.54.1