Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
refactor(install): extract per-agent config-path helpers for reuse
wleche0f63831ec56
diff --git a/crates/ai-memory-cli/src/commands/install_hooks.rs b/crates/ai-memory-cli/src/commands/install_hooks.rsindex 84a789f3..95e43bc4 100644--- a/crates/ai-memory-cli/src/commands/install_hooks.rs+++ b/crates/ai-memory-cli/src/commands/install_hooks.rs@@ -27,6 +27,48 @@ use crate::commands::render_shared::{};use crate::config::Config;+/// `~/.claude/settings.json` — Claude Code hooks live under `hooks`.+pub(crate) fn claude_settings_path() -> anyhow::Result<std::path::PathBuf> {+ Ok(dirs::home_dir()+ .context("could not locate $HOME for ~/.claude/settings.json")?+ .join(".claude")+ .join("settings.json"))+}++/// `~/.codex/hooks.json`.+pub(crate) fn codex_hooks_path() -> anyhow::Result<std::path::PathBuf> {+ Ok(dirs::home_dir()+ .context("could not locate $HOME for ~/.codex/hooks.json")?+ .join(".codex")+ .join("hooks.json"))+}++/// `~/.cursor/hooks.json`.+pub(crate) fn cursor_hooks_path() -> anyhow::Result<std::path::PathBuf> {+ Ok(dirs::home_dir()+ .context("could not locate $HOME for ~/.cursor/hooks.json")?+ .join(".cursor")+ .join("hooks.json"))+}++/// `~/.gemini/settings.json`.+pub(crate) fn gemini_settings_path() -> anyhow::Result<std::path::PathBuf> {+ Ok(dirs::home_dir()+ .context("could not locate $HOME for ~/.gemini/settings.json")?+ .join(".gemini")+ .join("settings.json"))+}++/// `~/.config/opencode/plugins/ai-memory.ts` — OpenCode's plugin file.+pub(crate) fn opencode_plugin_path() -> anyhow::Result<std::path::PathBuf> {+ Ok(dirs::home_dir()+ .context("could not locate $HOME for ~/.config/opencode")?+ .join(".config")+ .join("opencode")+ .join("plugins")+ .join("ai-memory.ts"))+}+/// Run the `install-hooks` subcommand.////// # Errors@@ -109,10 +151,7 @@ fn apply_to_claude_code_settings() -> Result<()> {let path = match &args.config_file {Some(p) => p.clone(),- None => dirs::home_dir()- .context("could not locate $HOME for ~/.claude/settings.json")?- .join(".claude")- .join("settings.json"),+ None => claude_settings_path()?,};let staged = stage_hook_scripts(hooks_dir, "claude-code")?;let payload = build_claude_code_payload(&staged, server_url, auth_token);@@ -182,10 +221,7 @@ fn apply_to_codex_settings() -> Result<()> {let path = match &args.config_file {Some(p) => p.clone(),- None => dirs::home_dir()- .context("could not locate $HOME for ~/.codex/hooks.json")?- .join(".codex")- .join("hooks.json"),+ None => codex_hooks_path()?,};let staged = stage_hook_scripts(hooks_dir, "codex")?;let outcome = merge_codex_hooks(&staged, server_url, auth_token, &path)?;@@ -276,10 +312,7 @@ fn apply_to_cursor_settings() -> Result<()> {let path = match &args.config_file {Some(p) => p.clone(),- None => dirs::home_dir()- .context("could not locate $HOME for ~/.cursor/hooks.json")?- .join(".cursor")- .join("hooks.json"),+ None => cursor_hooks_path()?,};let staged = stage_hook_scripts(hooks_dir, "cursor")?;let outcome = merge_cursor_hooks(&staged, server_url, auth_token, &path)?;@@ -351,10 +384,7 @@ fn apply_to_gemini_settings() -> Result<()> {let path = match &args.config_file {Some(p) => p.clone(),- None => dirs::home_dir()- .context("could not locate $HOME for ~/.gemini/settings.json")?- .join(".gemini")- .join("settings.json"),+ None => gemini_settings_path()?,};let staged = stage_hook_scripts(hooks_dir, "gemini-cli")?;let outcome = merge_gemini_hooks(&staged, server_url, auth_token, &path)?;@@ -415,12 +445,7 @@ fn apply_to_opencode_plugin() -> Result<()> {let path = match &args.config_file {Some(p) => p.clone(),- None => dirs::home_dir()- .context("could not locate $HOME for ~/.config/opencode/plugins")?- .join(".config")- .join("opencode")- .join("plugins")- .join("ai-memory.ts"),+ None => opencode_plugin_path()?,};let body = build_opencode_plugin(server_url, auth_token);diff --git a/crates/ai-memory-cli/src/commands/install_mcp.rs b/crates/ai-memory-cli/src/commands/install_mcp.rsindex 20246fc2..40fd3680 100644--- a/crates/ai-memory-cli/src/commands/install_mcp.rs+++ b/crates/ai-memory-cli/src/commands/install_mcp.rs@@ -58,15 +58,18 @@ pub fn run(config: &Config, args: InstallMcpArgs) -> Result<()> {Ok(())}-/// Resolve the user-config file for this client. Honours-/// `--config-file` when provided, else uses the canonical default-/// per client.-fn resolve_config_file(args: &InstallMcpArgs) -> Result<PathBuf> {- if let Some(p) = &args.config_file {- return Ok(p.clone());- }+/// Default MCP config-file path for a client (ignores any+/// `--config-file` override). Shared by install and uninstall.+///+/// # Errors+/// Returns an error for `Pi` (no MCP config), for Claude Desktop on+/// unsupported OSes, or when `$HOME` can't be resolved.+// used by uninstall in a later task+#[allow(dead_code)]+pub(crate) fn mcp_config_path(client: crate::cli::McpClient) -> Result<PathBuf> {+ use crate::cli::McpClient;let home = dirs::home_dir().context("could not locate $HOME for config-file auto-detect")?;- Ok(match args.client {+ Ok(match client {// Claude Code reads MCP-server registrations from `~/.claude.json`// (the same file `claude mcp add`/`claude mcp list` operate on).// `~/.claude/settings.json` is a separate file for hooks /@@ -109,6 +112,16 @@ fn resolve_config_file(args: &InstallMcpArgs) -> Result<PathBuf> {})}+/// Resolve the user-config file for this client. Honours+/// `--config-file` when provided, else uses the canonical default+/// per client.+fn resolve_config_file(args: &InstallMcpArgs) -> Result<PathBuf> {+ if let Some(p) = &args.config_file {+ return Ok(p.clone());+ }+ mcp_config_path(args.client)+}+/// Mutate the resolved client config file in place. Idempotent —/// re-runs that produce the same content are reported as no-op.fn apply_to_config_file(args: &InstallMcpArgs) -> Result<()> {