1use std::fs;
7use std::io::{self, BufRead, Write as _};
8use std::path::Path;
9
10use crate::config::{self, Config, LlmSection, Provider};
11use crate::paths;
12
13const GREEN: &str = "\x1b[32m";
15const YELLOW: &str = "\x1b[33m";
16const RED: &str = "\x1b[31m";
17const BOLD: &str = "\x1b[1m";
18const DIM: &str = "\x1b[2m";
19const RESET: &str = "\x1b[0m";
20
21const MEMORY_TEMPLATE: &str = "# Memory\n\n\
22<!-- recall-echo: Curated memory. Distilled facts, preferences, patterns. -->\n\
23<!-- Keep under 200 lines. Only write confirmed, stable information. -->\n";
24
25const ARCHIVE_TEMPLATE: &str = "# Conversation Archive\n\n\
26| # | Date | Session | Topics | Messages | Duration |\n\
27|---|------|---------|--------|----------|----------|\n";
28
29enum Status {
30 Created,
31 Exists,
32 Error,
33}
34
35fn print_status(status: Status, msg: &str) {
36 match status {
37 Status::Created => eprintln!(" {GREEN}✓{RESET} {msg}"),
38 Status::Exists => eprintln!(" {YELLOW}~{RESET} {msg}"),
39 Status::Error => eprintln!(" {RED}✗{RESET} {msg}"),
40 }
41}
42
43fn ensure_dir(path: &Path) {
44 if !path.exists() {
45 if let Err(e) = fs::create_dir_all(path) {
46 print_status(
47 Status::Error,
48 &format!("Failed to create {}: {e}", path.display()),
49 );
50 }
51 }
52}
53
54fn write_if_not_exists(path: &Path, content: &str, label: &str) {
55 if path.exists() {
56 print_status(
57 Status::Exists,
58 &format!("{label} already exists — preserved"),
59 );
60 } else {
61 match fs::write(path, content) {
62 Ok(()) => print_status(Status::Created, &format!("Created {label}")),
63 Err(e) => print_status(Status::Error, &format!("Failed to create {label}: {e}")),
64 }
65 }
66}
67
68fn prompt_provider(reader: &mut dyn BufRead) -> Option<Provider> {
71 if !atty_check() {
73 return if paths::detect_claude_code().is_some() {
75 Some(Provider::ClaudeCode)
76 } else {
77 Some(Provider::Anthropic)
78 };
79 }
80
81 let is_cc = paths::detect_claude_code().is_some();
82 let default_label = if is_cc { "3" } else { "1" };
83
84 eprintln!("\n{BOLD}LLM provider for entity extraction:{RESET}");
85 eprintln!(
86 " {BOLD}1{RESET}) anthropic {DIM}— Claude API{}",
87 if !is_cc { " (default)" } else { "" }
88 );
89 eprintln!(" {BOLD}2{RESET}) ollama {DIM}— Local models via Ollama{RESET}");
90 eprintln!(
91 " {BOLD}3{RESET}) claude-code {DIM}— Uses `claude -p` subprocess{}",
92 if is_cc { " (default)" } else { "" }
93 );
94 eprintln!(
95 " {BOLD}4{RESET}) skip {DIM}— Configure later with `recall-echo config`{RESET}"
96 );
97 eprint!("\n Choice [{default_label}]: ");
98 io::stderr().flush().ok();
99
100 let mut input = String::new();
101 if reader.read_line(&mut input).is_err() {
102 return None;
103 }
104
105 match input.trim() {
106 "" => {
107 if is_cc {
108 Some(Provider::ClaudeCode)
109 } else {
110 Some(Provider::Anthropic)
111 }
112 }
113 "1" | "anthropic" => Some(Provider::Anthropic),
114 "2" | "ollama" => Some(Provider::Openai),
115 "3" | "claude-code" => Some(Provider::ClaudeCode),
116 "4" | "skip" => None,
117 _ => {
118 let default = if is_cc {
119 Provider::ClaudeCode
120 } else {
121 Provider::Anthropic
122 };
123 eprintln!(
124 " {YELLOW}~{RESET} Unknown choice, defaulting to {}",
125 default
126 );
127 Some(default)
128 }
129 }
130}
131
132fn configure_llm(reader: &mut dyn BufRead, memory_dir: &Path) -> bool {
135 if !config::exists(memory_dir) {
136 if let Some(provider) = prompt_provider(reader) {
137 let is_cc = provider == Provider::ClaudeCode;
138 let cfg = Config {
139 llm: LlmSection {
140 provider: provider.clone(),
141 model: String::new(),
142 api_base: String::new(),
143 },
144 ..Config::default()
145 };
146 match config::save(memory_dir, &cfg) {
147 Ok(()) => {
148 let display_name = match &provider {
149 Provider::Anthropic => "anthropic",
150 Provider::Openai => "ollama (openai-compat)",
151 Provider::ClaudeCode => "claude-code",
152 };
153 print_status(
154 Status::Created,
155 &format!("Created .recall-echo.toml (provider: {display_name})"),
156 );
157 }
158 Err(e) => print_status(Status::Error, &format!("Failed to write config: {e}")),
159 }
160 return is_cc;
161 }
162 print_status(
163 Status::Exists,
164 "Skipped LLM config — run `recall-echo config set provider <name>` later",
165 );
166 } else {
167 print_status(
168 Status::Exists,
169 ".recall-echo.toml already exists — preserved",
170 );
171 let cfg = config::load(memory_dir);
173 return cfg.llm.provider == Provider::ClaudeCode;
174 }
175 false
176}
177
178#[cfg(feature = "graph")]
180fn init_graph(memory_dir: &Path) {
181 let graph_dir = memory_dir.join("graph");
182 if graph_dir.exists() {
183 print_status(Status::Exists, "graph/ already exists — preserved");
184 return;
185 }
186
187 match tokio::runtime::Runtime::new() {
188 Ok(rt) => match rt.block_on(crate::graph::GraphMemory::open(&graph_dir)) {
189 Ok(_) => print_status(Status::Created, "Created graph/ (SurrealDB + fastembed)"),
190 Err(e) => print_status(Status::Error, &format!("Failed to init graph: {e}")),
191 },
192 Err(e) => print_status(Status::Error, &format!("Failed to start runtime: {e}")),
193 }
194}
195
196fn configure_hooks(_entity_root: &Path) -> bool {
200 let claude_dir = match paths::detect_claude_code() {
201 Some(dir) => dir,
202 None => return false,
203 };
204
205 let settings_path = claude_dir.join("settings.json");
206 let recall_bin = std::env::current_exe()
207 .ok()
208 .and_then(|p| p.to_str().map(String::from))
209 .unwrap_or_else(|| "recall-echo".into());
210
211 let archive_cmd = format!("{recall_bin} archive-session");
212 let checkpoint_cmd = format!("{recall_bin} checkpoint --trigger precompact");
213
214 let mut settings: serde_json::Value = if settings_path.exists() {
216 fs::read_to_string(&settings_path)
217 .ok()
218 .and_then(|s| serde_json::from_str(&s).ok())
219 .unwrap_or_else(|| serde_json::json!({}))
220 } else {
221 serde_json::json!({})
222 };
223
224 let hooks = settings.as_object_mut().and_then(|o| {
225 o.entry("hooks")
226 .or_insert_with(|| serde_json::json!({}))
227 .as_object_mut()
228 });
229
230 let hooks = match hooks {
231 Some(h) => h,
232 None => {
233 print_status(Status::Error, "Could not parse settings.json hooks");
234 return false;
235 }
236 };
237
238 let mut changed = false;
239
240 if !hook_exists(hooks, "SessionEnd", &archive_cmd) {
242 let arr = hooks
243 .entry("SessionEnd")
244 .or_insert_with(|| serde_json::json!([]))
245 .as_array_mut();
246 if let Some(arr) = arr {
247 arr.push(serde_json::json!({
248 "hooks": [{"type": "command", "command": archive_cmd}]
249 }));
250 changed = true;
251 }
252 }
253
254 if !hook_exists(hooks, "PreCompact", &checkpoint_cmd) {
256 let arr = hooks
257 .entry("PreCompact")
258 .or_insert_with(|| serde_json::json!([]))
259 .as_array_mut();
260 if let Some(arr) = arr {
261 arr.push(serde_json::json!({
262 "hooks": [{"type": "command", "command": checkpoint_cmd}]
263 }));
264 changed = true;
265 }
266 }
267
268 if changed {
269 match serde_json::to_string_pretty(&settings) {
270 Ok(content) => match fs::write(&settings_path, content) {
271 Ok(()) => {
272 print_status(
273 Status::Created,
274 "Configured SessionEnd + PreCompact hooks in settings.json",
275 );
276 return true;
277 }
278 Err(e) => print_status(
279 Status::Error,
280 &format!("Failed to write settings.json: {e}"),
281 ),
282 },
283 Err(e) => print_status(Status::Error, &format!("Failed to serialize settings: {e}")),
284 }
285 } else {
286 print_status(Status::Exists, "Hooks already configured in settings.json");
287 return true;
288 }
289
290 false
291}
292
293fn hook_exists(
295 hooks: &serde_json::Map<String, serde_json::Value>,
296 event: &str,
297 command: &str,
298) -> bool {
299 if let Some(arr) = hooks.get(event).and_then(|v| v.as_array()) {
300 for group in arr {
301 if let Some(inner) = group.get("hooks").and_then(|h| h.as_array()) {
302 for hook in inner {
303 if let Some(cmd) = hook.get("command").and_then(|c| c.as_str()) {
304 if cmd.contains("recall-echo archive-session")
306 && command.contains("archive-session")
307 {
308 return true;
309 }
310 if cmd.contains("recall-echo checkpoint") && command.contains("checkpoint")
311 {
312 return true;
313 }
314 }
315 }
316 }
317 }
318 }
319 false
320}
321
322fn atty_check() -> bool {
324 #[cfg(unix)]
325 {
326 extern "C" {
327 fn isatty(fd: std::os::raw::c_int) -> std::os::raw::c_int;
328 }
329 unsafe { isatty(2) != 0 }
330 }
331 #[cfg(not(unix))]
332 {
333 false
334 }
335}
336
337pub fn run(entity_root: &Path) -> Result<(), String> {
349 let stdin = io::stdin();
350 let mut reader = stdin.lock();
351 run_with_reader(entity_root, &mut reader)
352}
353
354pub fn run_with_reader(entity_root: &Path, reader: &mut dyn BufRead) -> Result<(), String> {
356 if !entity_root.exists() {
357 return Err(format!(
358 "Directory not found: {}\n Create the directory first, or run from a valid path.",
359 entity_root.display()
360 ));
361 }
362
363 eprintln!("\n{BOLD}recall-echo{RESET} — initializing memory system\n");
364
365 let memory_dir = entity_root.join("memory");
366 let conversations_dir = memory_dir.join("conversations");
367 ensure_dir(&memory_dir);
368 ensure_dir(&conversations_dir);
369
370 write_if_not_exists(&memory_dir.join("MEMORY.md"), MEMORY_TEMPLATE, "MEMORY.md");
372
373 write_if_not_exists(&memory_dir.join("EPHEMERAL.md"), "", "EPHEMERAL.md");
375
376 write_if_not_exists(
378 &memory_dir.join("ARCHIVE.md"),
379 ARCHIVE_TEMPLATE,
380 "ARCHIVE.md",
381 );
382
383 #[cfg(feature = "graph")]
385 init_graph(&memory_dir);
386
387 let is_claude_code = configure_llm(reader, &memory_dir);
389
390 let hooks_configured = if is_claude_code {
392 configure_hooks(entity_root)
393 } else {
394 false
395 };
396
397 eprintln!("\n{BOLD}Setup complete.{RESET} Memory system is ready.\n");
399 eprintln!(" Layer 1 (MEMORY.md) — Curated facts, always in context");
400 eprintln!(" Layer 2 (EPHEMERAL.md) — Rolling window of recent sessions (FIFO, max 5)");
401 eprintln!(" Layer 3 (Archive) — Full conversations in memory/conversations/");
402 #[cfg(feature = "graph")]
403 eprintln!(" Layer 0 (Graph) — Knowledge graph with semantic search");
404 eprintln!();
405 eprintln!(" Run `recall-echo status` to check memory health.");
406 eprintln!(" Run `recall-echo config show` to view configuration.");
407 if hooks_configured {
408 eprintln!(" Hooks configured — archiving happens automatically.");
409 }
410 eprintln!();
411
412 Ok(())
413}
414
415#[cfg(test)]
416mod tests {
417 use super::*;
418 use std::io::Cursor;
419
420 #[test]
421 fn init_creates_directories_and_files() {
422 let tmp = tempfile::tempdir().unwrap();
423 let root = tmp.path().to_path_buf();
424 let mut reader = Cursor::new(b"4\n" as &[u8]); run_with_reader(&root, &mut reader).unwrap();
427
428 assert!(root.join("memory/MEMORY.md").exists());
429 assert!(root.join("memory/EPHEMERAL.md").exists());
430 assert!(root.join("memory/ARCHIVE.md").exists());
431 assert!(root.join("memory/conversations").exists());
432 }
433
434 #[test]
435 fn init_is_idempotent() {
436 let tmp = tempfile::tempdir().unwrap();
437 let root = tmp.path().to_path_buf();
438 let mut reader = Cursor::new(b"4\n" as &[u8]);
439
440 run_with_reader(&root, &mut reader).unwrap();
441 fs::write(root.join("memory/MEMORY.md"), "custom content").unwrap();
442
443 let mut reader2 = Cursor::new(b"4\n" as &[u8]);
444 run_with_reader(&root, &mut reader2).unwrap();
445 let content = fs::read_to_string(root.join("memory/MEMORY.md")).unwrap();
446 assert_eq!(content, "custom content");
447 }
448
449 #[test]
450 fn init_fails_if_root_missing() {
451 let mut reader = Cursor::new(b"" as &[u8]);
452 let result = run_with_reader(Path::new("/nonexistent/path"), &mut reader);
453 assert!(result.is_err());
454 }
455
456 #[test]
457 fn archive_template_has_header() {
458 let tmp = tempfile::tempdir().unwrap();
459 let mut reader = Cursor::new(b"4\n" as &[u8]);
460 run_with_reader(tmp.path(), &mut reader).unwrap();
461 let content = fs::read_to_string(tmp.path().join("memory/ARCHIVE.md")).unwrap();
462 assert!(content.contains("# Conversation Archive"));
463 assert!(content.contains("| # | Date"));
464 }
465}