|
| 1 | +use serde_json::json; |
| 2 | + |
| 3 | +use crate::actions::export_import::{import_json_payload, ExportActionReport, ImportActionReport}; |
| 4 | +use crate::actions::recall::RecallReport; |
| 5 | + |
| 6 | +pub fn print_recall_report(report: RecallReport, json_output: bool) -> Result<(), String> { |
| 7 | + if json_output { |
| 8 | + let payload: Vec<_> = report |
| 9 | + .results |
| 10 | + .into_iter() |
| 11 | + .map(|result| { |
| 12 | + json!({ |
| 13 | + "memory": result.memory, |
| 14 | + "score": result.score, |
| 15 | + "ranking": result.ranking, |
| 16 | + }) |
| 17 | + }) |
| 18 | + .collect(); |
| 19 | + println!( |
| 20 | + "{}", |
| 21 | + serde_json::to_string(&payload).map_err(|err| err.to_string())? |
| 22 | + ); |
| 23 | + } else { |
| 24 | + for result in report.results { |
| 25 | + println!( |
| 26 | + "{} [{}] {} score={:.3}", |
| 27 | + result.memory.id, result.memory.ring, result.memory.summary, result.score |
| 28 | + ); |
| 29 | + } |
| 30 | + } |
| 31 | + Ok(()) |
| 32 | +} |
| 33 | + |
| 34 | +pub fn print_export_report(report: ExportActionReport, json_output: bool) -> Result<(), String> { |
| 35 | + if let Some(jsonl) = report.jsonl { |
| 36 | + print!("{jsonl}"); |
| 37 | + return Ok(()); |
| 38 | + } |
| 39 | + let Some(output) = report.output else { |
| 40 | + return Err("export action did not return output path or JSONL".to_string()); |
| 41 | + }; |
| 42 | + if json_output { |
| 43 | + println!( |
| 44 | + "{}", |
| 45 | + serde_json::to_string(&json!({ |
| 46 | + "ok": true, |
| 47 | + "path": output, |
| 48 | + "memory_count": report.report.memory_count, |
| 49 | + "sensitive_included": report.report.sensitive_included, |
| 50 | + "superseded_included": report.report.superseded_included, |
| 51 | + })) |
| 52 | + .map_err(|err| err.to_string())? |
| 53 | + ); |
| 54 | + } else { |
| 55 | + println!( |
| 56 | + "Tree Ring Memory export complete: {} memories -> {}", |
| 57 | + report.report.memory_count, |
| 58 | + output.display() |
| 59 | + ); |
| 60 | + } |
| 61 | + Ok(()) |
| 62 | +} |
| 63 | + |
| 64 | +pub fn print_import_report(report: ImportActionReport, json_output: bool) -> Result<(), String> { |
| 65 | + if json_output { |
| 66 | + println!("{}", import_json_payload(&report)); |
| 67 | + } else { |
| 68 | + println!( |
| 69 | + "Tree Ring Memory import complete: valid={} inserted={} replaced={} skipped_duplicates={} dry_run={}", |
| 70 | + report.report.valid_count, |
| 71 | + report.report.inserted_count, |
| 72 | + report.report.replaced_count, |
| 73 | + report.report.skipped_duplicate_count, |
| 74 | + report.report.dry_run |
| 75 | + ); |
| 76 | + } |
| 77 | + Ok(()) |
| 78 | +} |
0 commit comments