use crate::context::ir::{IrMessage, IrPart, build_tool_call, build_tool_result, extract_text};
use crate::context::{ContextReader, for_each_jsonl_record};
use crate::message::{
AssistantResponse, Context, ContextListing, ConversationMessage, Entry, MessageKind,
TextContent,
};
use anyhow::Context as _;
use serde_json::Value;
use std::fs;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
pub struct ClaudeReader {
file_path: PathBuf,
}
impl ClaudeReader {
pub fn new(file_path: PathBuf) -> Self {
Self { file_path }
}
}
fn session_id(file_path: &Path) -> String {
file_path.file_stem().map_or_else(
|| "unknown".to_string(),
|stem| stem.to_string_lossy().into_owned(),
)
}
impl ContextReader for ClaudeReader {
fn list_contexts(&self) -> anyhow::Result<Vec<ContextListing>> {
let file_path = &self.file_path;
let file =
fs::File::open(file_path).with_context(|| format!("open {}", file_path.display()))?;
let reader = BufReader::new(file);
let id = session_id(file_path);
let mut cwd = String::new();
let mut timestamp = String::new();
for line in reader.lines() {
let line = line.with_context(|| format!("read {}", file_path.display()))?;
if line.trim().is_empty() {
continue;
}
let Ok(entry) = serde_json::from_str::<Value>(&line) else {
continue;
};
if cwd.is_empty() {
if let Some(value) = entry["cwd"].as_str() {
cwd = value.to_string();
}
}
if timestamp.is_empty() {
if let Some(value) = entry["timestamp"].as_str() {
timestamp = value.to_string();
}
}
if !cwd.is_empty() && !timestamp.is_empty() {
break;
}
}
let detail = match (timestamp.is_empty(), cwd.is_empty()) {
(true, true) => String::new(),
(false, true) => timestamp,
(true, false) => format!("cwd: {cwd}"),
(false, false) => format!("{timestamp}, cwd: {cwd}"),
};
Ok(vec![ContextListing { id, detail }])
}
fn read_context(&self, _context_id: &str) -> anyhow::Result<Context> {
let mut entries = Vec::new();
let mut messages = Vec::new();
let mut tool_names: std::collections::HashMap<String, String> =
std::collections::HashMap::new();
for_each_jsonl_record(&self.file_path, |line_num, entry_json| {
let Some(message) = entry_json.get("message").filter(|m| m.is_object()) else {
return;
};
let entry_id = match entry_json["uuid"].as_str() {
Some(uuid) => uuid.to_string(),
None => format!("claude-{line_num}"),
};
let parent_id = entry_json["parentUuid"].as_str().unwrap_or("").to_string();
let kind = claude_build_message_kind(message, &mut tool_names);
messages.push(ConversationMessage::new(entry_id.clone(), kind));
entries.push(Entry {
id: entry_id,
parent_id,
});
})?;
Ok(Context { entries, messages })
}
}
fn claude_build_message_kind(
message: &Value,
tool_names: &mut std::collections::HashMap<String, String>,
) -> MessageKind {
let role = message["role"].as_str().unwrap_or("unknown");
let content = message.get("content");
if role == "assistant" {
let mut ir = IrMessage::new();
if let Some(Value::Array(parts)) = content {
for part in parts {
match part["type"].as_str() {
Some("text") => ir.push(IrPart::text(part["text"].as_str().unwrap_or(""))),
Some("thinking" | "redacted_thinking") => {
if let Some(text) = part["thinking"].as_str() {
ir.push(IrPart::thinking(text));
}
}
Some("tool_use") => {
let name = part["name"].as_str().unwrap_or("");
if let Some(id) = part["id"].as_str() {
tool_names.insert(id.to_string(), name.to_string());
}
ir.push(IrPart::tool_call(build_tool_call(name, part.get("input"))));
}
_ => {}
}
}
} else if let Some(Value::String(text)) = content {
ir.push(IrPart::text(text.as_str()));
}
return MessageKind::AssistantResponse(AssistantResponse {
thinking: ir.thinking(),
tool_calls: ir.tool_calls(),
text: ir.text(),
});
}
if let Some(Value::Array(parts)) = content {
for part in parts {
if part["type"].as_str() == Some("tool_result") {
let tool_use_id = part["tool_use_id"].as_str().unwrap_or("");
let tool_name = tool_names
.get(tool_use_id)
.map_or_else(|| "tool".to_string(), Clone::clone);
let result_content = extract_text(part.get("content"));
let is_error = part["is_error"].as_bool().unwrap_or(false);
return MessageKind::ToolResultData(build_tool_result(
tool_name,
result_content,
is_error,
));
}
}
}
MessageKind::TextContent(TextContent {
role: role.to_string(),
text: extract_text(content),
})
}