|
| 1 | +//! Generic "imported" adapter. |
| 2 | +//! |
| 3 | +//! Reads canonical `Conversation` JSON lines from `~/.lovcode/imports/<source>/*.jsonl`, |
| 4 | +//! one full conversation per line. This is the escape hatch for any data |
| 5 | +//! source we don't have a first-class adapter for yet: ChatGPT exports, |
| 6 | +//! Gemini exports, Claude.ai web dumps, etc. |
| 7 | +//! |
| 8 | +//! A small converter binary or skill writes the canonical JSON; the adapter |
| 9 | +//! itself stays trivial. |
| 10 | +
|
| 11 | +use super::SourceAdapter; |
| 12 | +use crate::types::Conversation; |
| 13 | +use anyhow::{Context, Result}; |
| 14 | +use std::fs::File; |
| 15 | +use std::io::{BufRead, BufReader}; |
| 16 | +use std::path::{Path, PathBuf}; |
| 17 | +use walkdir::WalkDir; |
| 18 | + |
| 19 | +pub struct ImportedAdapter { |
| 20 | + pub id: &'static str, |
| 21 | + pub name: &'static str, |
| 22 | + pub subdir: &'static str, |
| 23 | +} |
| 24 | + |
| 25 | +impl ImportedAdapter { |
| 26 | + pub const fn chatgpt() -> Self { |
| 27 | + Self { id: "chatgpt", name: "ChatGPT (imported)", subdir: "chatgpt" } |
| 28 | + } |
| 29 | + pub const fn gemini() -> Self { |
| 30 | + Self { id: "gemini", name: "Gemini (imported)", subdir: "gemini" } |
| 31 | + } |
| 32 | + pub const fn claude_desktop() -> Self { |
| 33 | + Self { id: "claude-desktop", name: "Claude Desktop (imported)", subdir: "claude-desktop" } |
| 34 | + } |
| 35 | + |
| 36 | + fn root(&self) -> PathBuf { |
| 37 | + dirs::home_dir() |
| 38 | + .unwrap_or_else(|| PathBuf::from(".")) |
| 39 | + .join(".lovcode") |
| 40 | + .join("imports") |
| 41 | + .join(self.subdir) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl SourceAdapter for ImportedAdapter { |
| 46 | + fn id(&self) -> &'static str { self.id } |
| 47 | + fn name(&self) -> &'static str { self.name } |
| 48 | + |
| 49 | + fn discover(&self) -> Result<Vec<PathBuf>> { |
| 50 | + let root = self.root(); |
| 51 | + if !root.exists() { |
| 52 | + return Ok(Vec::new()); |
| 53 | + } |
| 54 | + Ok(WalkDir::new(&root) |
| 55 | + .into_iter() |
| 56 | + .filter_map(|e| e.ok()) |
| 57 | + .filter(|e| e.path().extension().and_then(|s| s.to_str()) == Some("jsonl")) |
| 58 | + .map(|e| e.into_path()) |
| 59 | + .collect()) |
| 60 | + } |
| 61 | + |
| 62 | + fn parse(&self, _path: &Path) -> Result<Conversation> { |
| 63 | + anyhow::bail!("ImportedAdapter is multi-conversation; use parse_many") |
| 64 | + } |
| 65 | + |
| 66 | + fn parse_many(&self, path: &Path) -> Result<Vec<Conversation>> { |
| 67 | + let mut out = read_imported_file(path)?; |
| 68 | + // Stamp the source id onto each conversation so downstream filtering |
| 69 | + // and per-source counts stay accurate regardless of what the writer |
| 70 | + // put in the canonical JSON. |
| 71 | + for c in &mut out { |
| 72 | + c.source = self.id.to_string(); |
| 73 | + } |
| 74 | + Ok(out) |
| 75 | + } |
| 76 | + |
| 77 | + fn watch_roots(&self) -> Vec<PathBuf> { |
| 78 | + vec![self.root()] |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/// Each line in the file is one canonical `Conversation`. The watcher path |
| 83 | +/// is whole-file; we need the per-line stream too. Public so the indexer |
| 84 | +/// can stream them out. |
| 85 | +pub fn read_imported_file(path: &Path) -> Result<Vec<Conversation>> { |
| 86 | + let file = File::open(path).with_context(|| format!("open {}", path.display()))?; |
| 87 | + let reader = BufReader::new(file); |
| 88 | + let mut out = Vec::new(); |
| 89 | + for line in reader.lines().map_while(Result::ok) { |
| 90 | + if line.trim().is_empty() { continue; } |
| 91 | + match serde_json::from_str::<Conversation>(&line) { |
| 92 | + Ok(c) => out.push(c), |
| 93 | + Err(e) => tracing::warn!(error = %e, path = %path.display(), "skipped malformed import line"), |
| 94 | + } |
| 95 | + } |
| 96 | + Ok(out) |
| 97 | +} |
0 commit comments