use std::fs;
use super::CommandResult;
use crate::tui::app::App;
pub fn memory(app: &mut App, arg: Option<&str>) -> CommandResult {
if !app.use_memory {
return CommandResult::error(
"user memory is disabled. Enable with `[memory] enabled = true` in `~/.deepseek/config.toml` or `DEEPSEEK_MEMORY=on` in your environment, then restart the TUI.",
);
}
let path = app.memory_path.clone();
let sub = arg.unwrap_or("show").trim();
match sub {
"" | "show" => {
let body = match fs::read_to_string(&path) {
Ok(text) if text.trim().is_empty() => format!(
"{}\n(empty — add via `# foo` from the composer or have the model use the `remember` tool)",
path.display()
),
Ok(text) => format!("{}\n\n{}", path.display(), text.trim_end()),
Err(_) => format!(
"{}\n(file does not exist yet — add via `# foo` from the composer to create it)",
path.display()
),
};
CommandResult::message(body)
}
"path" => CommandResult::message(path.display().to_string()),
"clear" => match fs::write(&path, "") {
Ok(()) => CommandResult::message(format!("memory cleared: {}", path.display())),
Err(err) => CommandResult::error(format!("failed to clear {}: {err}", path.display())),
},
"edit" => CommandResult::message(format!(
"to edit your memory file, run:\n\n ${{VISUAL:-${{EDITOR:-vi}}}} {}",
path.display()
)),
_ => CommandResult::error(format!(
"unknown subcommand `{sub}`. usage: /memory [show|path|clear|edit]"
)),
}
}