use crate::context::ContextReader;
use crate::context::ir::{IrMessage, IrPart, build_tool_call, build_tool_result, extract_text};
use crate::message::{
AssistantResponse, Context, ContextListing, ConversationMessage, Entry, MessageKind,
TextContent, ToolCall,
};
use anyhow::Context as _;
use serde_json::Value;
use std::fs;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
pub struct CodexReader {
file_path: Option<PathBuf>,
}
impl CodexReader {
pub fn new(file_path: PathBuf) -> Self {
Self {
file_path: Some(file_path),
}
}
pub fn empty() -> Self {
Self { file_path: None }
}
}
impl ContextReader for CodexReader {
fn list_contexts(&self) -> anyhow::Result<Vec<ContextListing>> {
let Some(file_path) = &self.file_path else {
return Ok(Vec::new());
};
let file =
fs::File::open(file_path).with_context(|| format!("open {}", file_path.display()))?;
let mut reader = BufReader::new(file);
let mut first_line = String::new();
reader
.read_line(&mut first_line)
.with_context(|| format!("read {}", file_path.display()))?;
let first_line = first_line.trim_end();
if first_line.is_empty() {
anyhow::bail!("empty file");
}
let session: Value = serde_json::from_str(first_line)?;
if session["type"].as_str() != Some("session_meta") {
anyhow::bail!("first line is not a Codex session header");
}
let payload = &session["payload"];
let id = payload["id"].as_str().unwrap_or("unknown").to_string();
let cwd = payload["cwd"].as_str().unwrap_or("");
let timestamp = payload["timestamp"]
.as_str()
.or_else(|| session["timestamp"].as_str())
.unwrap_or("");
let detail = match (timestamp.is_empty(), cwd.is_empty()) {
(true, true) => String::new(),
(false, true) => timestamp.to_string(),
(true, false) => format!("cwd: {cwd}"),
(false, false) => format!("{timestamp}, cwd: {cwd}"),
};
Ok(vec![ContextListing {
id,
detail,
path: Some(file_path.clone()),
}])
}
fn read_context(&self, _context_id: &str) -> anyhow::Result<Context> {
let Some(file_path) = &self.file_path else {
anyhow::bail!("no sessions found");
};
let file =
fs::File::open(file_path).with_context(|| format!("open {}", file_path.display()))?;
let reader = BufReader::new(file);
let mut entries = Vec::new();
let mut messages = Vec::new();
let mut previous_entry_id = String::new();
for (line_num, line_result) in reader.lines().enumerate() {
let line = line_result
.with_context(|| format!("{}:{}: read error", file_path.display(), line_num + 1))?;
if line.trim().is_empty() {
continue;
}
let entry_json: Value = serde_json::from_str(&line).with_context(|| {
format!("{}:{}: JSON parse error", file_path.display(), line_num + 1)
})?;
if entry_json["type"].as_str() == Some("session_meta") {
continue;
}
let Some(kind) = codex_message_kind(&entry_json) else {
continue;
};
let payload = &entry_json["payload"];
let entry_id = codex_entry_id(payload, line_num);
let parent_id = previous_entry_id.clone();
let metadata = codex_metadata(&entry_json, payload);
let mut message = ConversationMessage::new(entry_id.clone(), kind);
message.metadata = metadata;
messages.push(message);
entries.push(Entry {
id: entry_id.clone(),
parent_id,
});
previous_entry_id = entry_id;
}
Ok(Context { entries, messages })
}
}
fn codex_message_kind(entry: &Value) -> Option<MessageKind> {
if entry["type"].as_str() != Some("response_item") {
return None;
}
let payload = &entry["payload"];
match payload["type"].as_str()? {
"message" => Some(codex_text_or_assistant(payload)),
"function_call" | "custom_tool_call" | "local_shell_call" => {
let call = codex_tool_call(payload);
Some(MessageKind::AssistantResponse(AssistantResponse {
thinking: Vec::new(),
tool_calls: vec![call],
text: String::new(),
}))
}
"reasoning" => {
let mut ir = IrMessage::new();
for part in payload["summary"].as_array().cloned().unwrap_or_default() {
if let Some(text) = part["text"].as_str() {
ir.push(IrPart::Thinking(text.to_string()));
}
}
Some(MessageKind::AssistantResponse(AssistantResponse {
thinking: ir.thinking(),
tool_calls: Vec::new(),
text: String::new(),
}))
}
"function_call_output" | "custom_tool_call_output" => {
let tool_name = payload["call_id"].as_str().unwrap_or("tool").to_string();
let content = codex_output_text(payload);
let is_error = payload["status"].as_str() == Some("failed");
Some(MessageKind::ToolResultData(build_tool_result(
tool_name, content, is_error,
)))
}
_ => None,
}
}
fn codex_text_or_assistant(payload: &Value) -> MessageKind {
let role = payload["role"].as_str().unwrap_or("unknown").to_string();
let text = extract_text(Some(&payload["content"]));
if role == "assistant" {
MessageKind::AssistantResponse(AssistantResponse {
thinking: Vec::new(),
tool_calls: Vec::new(),
text,
})
} else {
MessageKind::TextContent(TextContent { role, text })
}
}
fn codex_entry_id(payload: &Value, line_num: usize) -> String {
let fallback = || format!("codex-{line_num}");
let Some(id) = payload["id"]
.as_str()
.or_else(|| payload["call_id"].as_str())
else {
return fallback();
};
if matches!(
payload["type"].as_str(),
Some("function_call_output" | "custom_tool_call_output")
) {
format!("{id}-output")
} else {
id.to_string()
}
}
fn codex_tool_call(payload: &Value) -> ToolCall {
let name = payload["name"]
.as_str()
.or_else(|| payload["command"].as_str())
.or_else(|| payload["type"].as_str())
.unwrap_or("tool")
.to_string();
let arguments = if let Some(command) = payload["action"]["command"].as_str() {
serde_json::json!({ "command": command })
} else {
let raw = payload.get("arguments").or_else(|| payload.get("input"));
build_tool_call(name.clone(), raw).arguments
};
ToolCall { name, arguments }
}
fn codex_output_text(payload: &Value) -> String {
for key in ["output", "content"] {
let Some(value) = payload.get(key) else {
continue;
};
if let Some(s) = value.as_str() {
return s.to_string();
}
if value.is_array() {
return extract_text(Some(value));
}
if !value.is_null() {
return value.to_string();
}
}
String::new()
}
fn codex_metadata(entry: &Value, payload: &Value) -> std::collections::BTreeMap<String, Value> {
let mut metadata = std::collections::BTreeMap::new();
if let Some(kind) = entry["type"].as_str() {
metadata.insert("entry_type".to_string(), Value::String(kind.to_string()));
}
if let Some(kind) = payload["type"].as_str() {
metadata.insert("payload_type".to_string(), Value::String(kind.to_string()));
}
if let Some(call_id) = payload["call_id"].as_str() {
metadata.insert("call_id".to_string(), Value::String(call_id.to_string()));
}
if let Some(role) = payload["role"].as_str() {
metadata.insert("role".to_string(), Value::String(role.to_string()));
}
metadata
}