use crate::VERSION;
use crate::analysis::claude_analyzer::{
analyze_claude_conversations_with_mode, analyze_claude_logs,
};
use crate::analysis::codex_analyzer::analyze_codex_conversations_with_mode;
use crate::analysis::common_state::AnalysisMode;
use crate::analysis::copilot_analyzer::analyze_copilot_conversations_with_mode;
use crate::analysis::detector::detect_extension_type;
use crate::analysis::gemini_analyzer::{
analyze_gemini_conversations_with_mode, analyze_gemini_session,
};
use crate::constants::buffer;
use crate::models::{
ClaudeCodeLog, CodeAnalysis, CodexLog, CopilotSession, ExtensionType, GeminiSession,
};
use crate::utils::{get_current_user, get_machine_id, read_json, read_jsonl};
use anyhow::{Context, Result};
use serde_json::Value;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;
const AUTODETECT_PEEK_LINES: usize = 8;
pub fn analyze_jsonl_file<P: AsRef<Path>>(path: P) -> Result<Value> {
let analysis = analyze_jsonl_file_typed(path)?;
if analysis.records.is_empty() && analysis.extension_name.is_empty() {
return Ok(serde_json::json!({}));
}
Ok(serde_json::to_value(&analysis)?)
}
pub fn analyze_jsonl_file_typed<P: AsRef<Path>>(path: P) -> Result<CodeAnalysis> {
analyze_jsonl_file_typed_with_mode(path, AnalysisMode::Full)
}
pub fn analyze_jsonl_file_typed_with_mode<P: AsRef<Path>>(
path: P,
mode: AnalysisMode,
) -> Result<CodeAnalysis> {
let path = path.as_ref();
if let Some(analysis) = stream_analyze_autodetect(path, mode)? {
return Ok(analysis);
}
let data = match read_jsonl(path) {
Ok(data) => data,
Err(_) => read_json(path)?,
};
if data.is_empty() {
return Ok(empty_analysis());
}
let ext_type = detect_extension_type(&data)?;
dispatch_by_vec(data, ext_type, mode)
}
pub fn analyze_session_file_typed_as<P: AsRef<Path>>(
path: P,
provider: ExtensionType,
mode: AnalysisMode,
) -> Result<CodeAnalysis> {
let path = path.as_ref();
if let Some(analysis) = stream_analyze_known(path, provider, mode)? {
return Ok(analysis);
}
let data = match read_jsonl(path) {
Ok(data) => data,
Err(_) => read_json(path)?,
};
if data.is_empty() {
return Ok(empty_analysis());
}
dispatch_by_vec(data, provider, mode)
}
fn stream_analyze_known(
path: &Path,
provider: ExtensionType,
mode: AnalysisMode,
) -> Result<Option<CodeAnalysis>> {
let file =
File::open(path).with_context(|| format!("Failed to open file: {}", path.display()))?;
let mut reader = BufReader::with_capacity(buffer::FILE_READ_BUFFER, file);
let first_line = match read_next_non_empty_line(&mut reader)? {
Some(line) => line,
None => return Ok(None), };
let first_value: Value = match serde_json::from_str(first_line.trim()) {
Ok(v) => v,
Err(_) => return Ok(None), };
let analysis = dispatch_streaming_buffered(provider, vec![first_value], reader, mode)?;
Ok(Some(finalize(analysis, provider)))
}
fn stream_analyze_autodetect(path: &Path, mode: AnalysisMode) -> Result<Option<CodeAnalysis>> {
let file =
File::open(path).with_context(|| format!("Failed to open file: {}", path.display()))?;
let mut reader = BufReader::with_capacity(buffer::FILE_READ_BUFFER, file);
let mut buffered: Vec<Value> = Vec::with_capacity(AUTODETECT_PEEK_LINES);
let mut first_line_was_json = None::<bool>;
while buffered.len() < AUTODETECT_PEEK_LINES {
let line = match read_next_non_empty_line(&mut reader)? {
Some(line) => line,
None => break,
};
match serde_json::from_str::<Value>(line.trim()) {
Ok(v) => {
first_line_was_json.get_or_insert(true);
buffered.push(v);
}
Err(_) => {
if buffered.is_empty() {
first_line_was_json = Some(false);
}
break;
}
}
}
if first_line_was_json == Some(false) {
return Ok(None);
}
if buffered.is_empty() {
return Ok(None);
}
let ext = detect_extension_type(&buffered)?;
let analysis = dispatch_streaming_buffered(ext, buffered, reader, mode)?;
Ok(Some(finalize(analysis, ext)))
}
fn read_next_non_empty_line<R: BufRead>(reader: &mut R) -> Result<Option<String>> {
let mut line = String::new();
loop {
line.clear();
let n = reader
.read_line(&mut line)
.context("Failed to read line from session file")?;
if n == 0 {
return Ok(None);
}
if !line.trim().is_empty() {
return Ok(Some(line));
}
}
}
fn dispatch_streaming_buffered(
ext: ExtensionType,
buffered: Vec<Value>,
mut reader: BufReader<File>,
mode: AnalysisMode,
) -> Result<CodeAnalysis> {
match ext {
ExtensionType::ClaudeCode => {
let buffered_iter = buffered
.into_iter()
.filter_map(|v| serde_json::from_value::<ClaudeCodeLog>(v).ok());
let rest = iter_jsonl_typed::<ClaudeCodeLog>(&mut reader);
analyze_claude_logs(buffered_iter.chain(rest), mode)
}
ExtensionType::Codex => {
let mut logs: Vec<CodexLog> = Vec::with_capacity(64);
for v in buffered {
if let Ok(log) = serde_json::from_value::<CodexLog>(v) {
logs.push(log);
}
}
for log in iter_jsonl_typed::<CodexLog>(&mut reader) {
logs.push(log);
}
analyze_codex_conversations_with_mode(&logs, mode)
}
ExtensionType::Copilot => {
let first = buffered
.into_iter()
.next()
.context("Copilot session missing top-level object")?;
let session: CopilotSession =
serde_json::from_value(first).context("Failed to parse Copilot session")?;
analyze_copilot_conversations_with_mode(session, mode)
}
ExtensionType::Gemini => {
let first = buffered
.into_iter()
.next()
.context("Gemini session missing top-level object")?;
let session: GeminiSession =
serde_json::from_value(first).context("Failed to parse Gemini session")?;
analyze_gemini_session(session, mode)
}
}
}
fn iter_jsonl_typed<'a, T>(reader: &'a mut BufReader<File>) -> impl Iterator<Item = T> + 'a
where
T: serde::de::DeserializeOwned + 'a,
{
reader.lines().filter_map(|line| {
let line = line.ok()?;
let trimmed = line.trim();
if trimmed.is_empty() {
return None;
}
serde_json::from_str::<T>(trimmed).ok()
})
}
fn dispatch_by_vec(
data: Vec<Value>,
ext_type: ExtensionType,
mode: AnalysisMode,
) -> Result<CodeAnalysis> {
let analysis = match ext_type {
ExtensionType::ClaudeCode => analyze_claude_conversations_with_mode(data, mode)?,
ExtensionType::Codex => {
let logs: Vec<CodexLog> = data
.into_iter()
.filter_map(|v| serde_json::from_value(v).ok())
.collect();
analyze_codex_conversations_with_mode(&logs, mode)?
}
ExtensionType::Copilot => {
let session: CopilotSession = serde_json::from_value(data[0].clone())?;
analyze_copilot_conversations_with_mode(session, mode)?
}
ExtensionType::Gemini => analyze_gemini_conversations_with_mode(data, mode)?,
};
Ok(finalize(analysis, ext_type))
}
fn empty_analysis() -> CodeAnalysis {
CodeAnalysis {
user: String::new(),
extension_name: String::new(),
insights_version: String::new(),
machine_id: String::new(),
records: Vec::new(),
}
}
fn finalize(mut analysis: CodeAnalysis, ext_type: ExtensionType) -> CodeAnalysis {
analysis.user = get_current_user();
analysis.extension_name = ext_type.to_string();
analysis.machine_id = get_machine_id().to_string();
analysis.insights_version = VERSION.to_string();
analysis
}