|
| 1 | +//! TSA-based session connectors |
| 2 | +//! |
| 3 | +//! These connectors wrap terraphim-session-analyzer's connectors |
| 4 | +//! to provide enhanced parsing capabilities. |
| 5 | +
|
| 6 | +use super::from_normalized_session; |
| 7 | +use crate::connector::{ConnectorStatus, ImportOptions, SessionConnector}; |
| 8 | +use crate::model::Session; |
| 9 | +use anyhow::Result; |
| 10 | +use async_trait::async_trait; |
| 11 | +use std::path::PathBuf; |
| 12 | +use terraphim_session_analyzer::connectors::{ |
| 13 | + ImportOptions as ClaImportOptions, SessionConnector as ClaSessionConnector, |
| 14 | +}; |
| 15 | + |
| 16 | +/// TSA-powered Claude Code connector |
| 17 | +/// |
| 18 | +/// Uses terraphim-session-analyzer for enhanced parsing with agent attribution, |
| 19 | +/// tool tracking, and detailed analytics. |
| 20 | +#[derive(Debug, Default)] |
| 21 | +pub struct ClaClaudeConnector { |
| 22 | + inner: terraphim_session_analyzer::connectors::ClaudeCodeConnector, |
| 23 | +} |
| 24 | + |
| 25 | +#[async_trait] |
| 26 | +impl SessionConnector for ClaClaudeConnector { |
| 27 | + fn source_id(&self) -> &str { |
| 28 | + "claude-code" |
| 29 | + } |
| 30 | + |
| 31 | + fn display_name(&self) -> &str { |
| 32 | + "Claude Code (CLA)" |
| 33 | + } |
| 34 | + |
| 35 | + fn detect(&self) -> ConnectorStatus { |
| 36 | + match self.inner.detect() { |
| 37 | + terraphim_session_analyzer::connectors::ConnectorStatus::Available { |
| 38 | + path, |
| 39 | + sessions_estimate, |
| 40 | + } => ConnectorStatus::Available { |
| 41 | + path, |
| 42 | + sessions_estimate, |
| 43 | + }, |
| 44 | + terraphim_session_analyzer::connectors::ConnectorStatus::NotFound => { |
| 45 | + ConnectorStatus::NotFound |
| 46 | + } |
| 47 | + terraphim_session_analyzer::connectors::ConnectorStatus::Error(e) => { |
| 48 | + ConnectorStatus::Error(e) |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + fn default_path(&self) -> Option<PathBuf> { |
| 54 | + self.inner.default_path() |
| 55 | + } |
| 56 | + |
| 57 | + async fn import(&self, options: &ImportOptions) -> Result<Vec<Session>> { |
| 58 | + let cla_options = to_cla_options(options); |
| 59 | + |
| 60 | + // CLA import is synchronous, wrap in blocking task |
| 61 | + // Create a new connector inside the blocking task since it's stateless |
| 62 | + let sessions = tokio::task::spawn_blocking(move || { |
| 63 | + let connector = terraphim_session_analyzer::connectors::ClaudeCodeConnector; |
| 64 | + connector.import(&cla_options) |
| 65 | + }) |
| 66 | + .await??; |
| 67 | + |
| 68 | + Ok(sessions |
| 69 | + .into_iter() |
| 70 | + .map(|ns| from_normalized_session(ns, "cla")) |
| 71 | + .collect()) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/// CLA-powered Cursor IDE connector |
| 76 | +/// |
| 77 | +/// Uses claude-log-analyzer's Cursor connector for SQLite parsing. |
| 78 | +#[cfg(feature = "tsa-full")] |
| 79 | +#[derive(Debug, Default)] |
| 80 | +pub struct ClaCursorConnector { |
| 81 | + inner: terraphim_session_analyzer::connectors::cursor::CursorConnector, |
| 82 | +} |
| 83 | + |
| 84 | +#[cfg(feature = "tsa-full")] |
| 85 | +#[async_trait] |
| 86 | +impl SessionConnector for ClaCursorConnector { |
| 87 | + fn source_id(&self) -> &str { |
| 88 | + "cursor" |
| 89 | + } |
| 90 | + |
| 91 | + fn display_name(&self) -> &str { |
| 92 | + "Cursor IDE" |
| 93 | + } |
| 94 | + |
| 95 | + fn detect(&self) -> ConnectorStatus { |
| 96 | + match self.inner.detect() { |
| 97 | + terraphim_session_analyzer::connectors::ConnectorStatus::Available { |
| 98 | + path, |
| 99 | + sessions_estimate, |
| 100 | + } => ConnectorStatus::Available { |
| 101 | + path, |
| 102 | + sessions_estimate, |
| 103 | + }, |
| 104 | + terraphim_session_analyzer::connectors::ConnectorStatus::NotFound => { |
| 105 | + ConnectorStatus::NotFound |
| 106 | + } |
| 107 | + terraphim_session_analyzer::connectors::ConnectorStatus::Error(e) => { |
| 108 | + ConnectorStatus::Error(e) |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + fn default_path(&self) -> Option<PathBuf> { |
| 114 | + self.inner.default_path() |
| 115 | + } |
| 116 | + |
| 117 | + async fn import(&self, options: &ImportOptions) -> Result<Vec<Session>> { |
| 118 | + let cla_options = to_cla_options(options); |
| 119 | + |
| 120 | + // CLA import is synchronous, wrap in blocking task |
| 121 | + // Create a new connector inside the blocking task since it's stateless |
| 122 | + let sessions = tokio::task::spawn_blocking(move || { |
| 123 | + let connector = terraphim_session_analyzer::connectors::cursor::CursorConnector; |
| 124 | + connector.import(&cla_options) |
| 125 | + }) |
| 126 | + .await??; |
| 127 | + |
| 128 | + Ok(sessions |
| 129 | + .into_iter() |
| 130 | + .map(|ns| from_normalized_session(ns, "cursor")) |
| 131 | + .collect()) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +/// Convert our ImportOptions to CLA's ImportOptions |
| 136 | +fn to_cla_options(options: &ImportOptions) -> ClaImportOptions { |
| 137 | + ClaImportOptions { |
| 138 | + path: options.path.clone(), |
| 139 | + since: options.since, |
| 140 | + until: options.until, |
| 141 | + limit: options.limit, |
| 142 | + incremental: options.incremental, |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +// Placeholder for when tsa-full is not enabled |
| 147 | +#[cfg(not(feature = "tsa-full"))] |
| 148 | +#[derive(Debug, Default)] |
| 149 | +pub struct ClaCursorConnector; |
| 150 | + |
| 151 | +#[cfg(not(feature = "tsa-full"))] |
| 152 | +#[async_trait] |
| 153 | +impl SessionConnector for ClaCursorConnector { |
| 154 | + fn source_id(&self) -> &str { |
| 155 | + "cursor-stub" |
| 156 | + } |
| 157 | + |
| 158 | + fn display_name(&self) -> &str { |
| 159 | + "Cursor IDE (requires tsa-full feature)" |
| 160 | + } |
| 161 | + |
| 162 | + fn detect(&self) -> ConnectorStatus { |
| 163 | + ConnectorStatus::Error("Cursor support requires tsa-full feature".to_string()) |
| 164 | + } |
| 165 | + |
| 166 | + fn default_path(&self) -> Option<PathBuf> { |
| 167 | + None |
| 168 | + } |
| 169 | + |
| 170 | + async fn import(&self, _options: &ImportOptions) -> Result<Vec<Session>> { |
| 171 | + anyhow::bail!("Cursor support requires tsa-full feature") |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +#[cfg(test)] |
| 176 | +mod tests { |
| 177 | + use super::*; |
| 178 | + |
| 179 | + #[test] |
| 180 | + fn test_cla_claude_connector() { |
| 181 | + let connector = ClaClaudeConnector::default(); |
| 182 | + assert_eq!(connector.source_id(), "claude-code"); |
| 183 | + assert_eq!(connector.display_name(), "Claude Code (CLA)"); |
| 184 | + } |
| 185 | + |
| 186 | + #[cfg(feature = "tsa-full")] |
| 187 | + #[test] |
| 188 | + fn test_cla_cursor_connector() { |
| 189 | + let connector = ClaCursorConnector::default(); |
| 190 | + assert_eq!(connector.source_id(), "cursor"); |
| 191 | + assert_eq!(connector.display_name(), "Cursor IDE"); |
| 192 | + } |
| 193 | +} |
0 commit comments