use regex::Regex;
use serde_json::Value;
use std::path::PathBuf;
use std::sync::LazyLock;
use crate::file_tail::{default_home_dir, read_file_tail};
use crate::result_metadata::ResultMetadata;
use crate::usage_limit::{extract_reset_time, extract_timezone, is_usage_limit_text};
pub const TRANSCRIPT_TAIL_BYTES: u64 = 1024 * 1024;
pub const CLAUDE_TRANSCRIPT_LIMIT_SOURCE: &str = "claude-transcript";
const CONVERSATION_TYPES: [&str; 2] = ["assistant", "user"];
static SESSION_ID_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^[0-9a-zA-Z][0-9a-zA-Z_-]{7,127}$").expect("valid session id regex")
});
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ClaudeTranscriptOptions {
pub working_directory: Option<PathBuf>,
pub claude_config_dir: Option<PathBuf>,
pub home_dir: Option<PathBuf>,
pub max_bytes: Option<u64>,
}
impl ClaudeTranscriptOptions {
pub fn for_run_env(
working_directory: &str,
extra_env: &[(String, String)],
unset_env: &[String],
) -> Self {
let passed = extra_env
.iter()
.rev()
.find(|(name, value)| name == "CLAUDE_CONFIG_DIR" && !value.is_empty())
.map(|(_, value)| PathBuf::from(value));
let claude_config_dir = passed.or_else(|| {
unset_env
.iter()
.any(|name| name == "CLAUDE_CONFIG_DIR")
.then(|| default_home_dir().join(".claude"))
});
Self {
working_directory: Some(PathBuf::from(working_directory))
.filter(|path| !path.as_os_str().is_empty()),
claude_config_dir,
..Self::default()
}
}
fn max_bytes(&self) -> u64 {
self.max_bytes.unwrap_or(TRANSCRIPT_TAIL_BYTES)
}
}
pub fn claude_project_slugs(working_directory: &str) -> Vec<String> {
let old = working_directory.replace('/', "-");
let new: String = working_directory
.chars()
.map(|c| if c.is_ascii_alphanumeric() { c } else { '-' })
.collect();
if old == new {
vec![old]
} else {
vec![old, new]
}
}
pub fn claude_transcript_candidates(
session_id: &str,
options: &ClaudeTranscriptOptions,
) -> Vec<PathBuf> {
let Some(working_directory) = options.working_directory.as_deref() else {
return Vec::new();
};
if !SESSION_ID_PATTERN.is_match(session_id) {
return Vec::new();
}
let config_dir = options
.claude_config_dir
.clone()
.filter(|path| !path.as_os_str().is_empty())
.or_else(|| {
std::env::var_os("CLAUDE_CONFIG_DIR")
.filter(|value| !value.is_empty())
.map(PathBuf::from)
});
let home = options.home_dir.clone().unwrap_or_else(default_home_dir);
let roots = config_dir
.map(|dir| dir.join("projects"))
.into_iter()
.chain(std::iter::once(home.join(".claude").join("projects")));
let slugs = claude_project_slugs(&working_directory.to_string_lossy());
let mut candidates: Vec<PathBuf> = Vec::new();
for root in roots {
for slug in &slugs {
let path = root.join(slug).join(format!("{session_id}.jsonl"));
if !candidates.contains(&path) {
candidates.push(path);
}
}
}
candidates
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ClaudeTranscriptApiError {
pub text: String,
pub error: Option<Value>,
pub timestamp: Option<String>,
}
pub fn claude_transcript_api_error(text: &str) -> Result<ClaudeTranscriptApiError, String> {
for line in text.lines().rev() {
let Ok(entry) = serde_json::from_str::<Value>(line) else {
continue;
};
let Some(kind) = entry
.get("type")
.and_then(Value::as_str)
.filter(|kind| CONVERSATION_TYPES.contains(kind))
else {
continue;
};
if kind != "assistant" || entry.get("isApiErrorMessage") != Some(&Value::Bool(true)) {
return Err(format!("last entry is a {kind} message, not an API error"));
}
let text = entry
.pointer("/message/content")
.and_then(Value::as_array)
.map(|content| {
content
.iter()
.filter(|item| item.get("type").and_then(Value::as_str) == Some("text"))
.filter_map(|item| item.get("text").and_then(Value::as_str))
.filter(|text| !text.is_empty())
.collect::<Vec<_>>()
.join("\n")
})
.unwrap_or_default();
return Ok(ClaudeTranscriptApiError {
text,
error: entry.get("error").filter(|value| !value.is_null()).cloned(),
timestamp: entry
.get("timestamp")
.and_then(Value::as_str)
.map(String::from),
});
}
Err("no conversation entry in transcript tail".to_string())
}
pub fn read_claude_transcript_api_error(
session_id: &str,
options: &ClaudeTranscriptOptions,
) -> Result<(PathBuf, ClaudeTranscriptApiError), String> {
if session_id.is_empty() || options.working_directory.is_none() {
return Err("no session id".to_string());
}
for path in claude_transcript_candidates(session_id, options) {
if let Ok(text) = read_file_tail(&path, options.max_bytes()) {
return claude_transcript_api_error(&text).map(|error| (path, error));
}
}
Err("transcript not found".to_string())
}
fn is_claude_transcript_candidate(metadata: &ResultMetadata) -> bool {
metadata.tool == "claude"
&& !metadata.success
&& metadata.signal.is_none()
&& !metadata.limit_reached
&& metadata
.session_id
.as_deref()
.is_some_and(|id| !id.is_empty())
}
pub fn apply_claude_transcript_usage_limit(
metadata: &mut ResultMetadata,
options: &ClaudeTranscriptOptions,
) -> bool {
if !is_claude_transcript_candidate(metadata) {
return false;
}
let session_id = metadata.session_id.clone().unwrap_or_default();
let Ok((_, transcript)) = read_claude_transcript_api_error(&session_id, options) else {
return false;
};
if !is_usage_limit_text(&transcript.text) {
return false;
}
let reset_time = extract_reset_time(&transcript.text);
metadata.success = false;
metadata.limit_reached = true;
metadata.limit_timezone = extract_timezone(&transcript.text, reset_time.as_deref());
metadata.limit_reset_time = reset_time;
metadata.limit_source = Some(CLAUDE_TRANSCRIPT_LIMIT_SOURCE.to_string());
true
}