goosedump 0.5.2

Coding agent context data browser
// SPDX-License-Identifier: LGPL-2.1-or-later
// Copyright (C) Jarkko Sakkinen 2026

//! Reader for Claude Code session transcripts.
//!
//! Claude Code stores one session per JSONL file under
//! `~/.claude/projects/<encoded-cwd>/<session-id>.jsonl`.
//!
//! Each line is a record; conversation turns carry a `message` field holding
//! Anthropic Messages API content blocks (`text`, `thinking`, `tool_use`,
//! and—on user turns—`tool_result`). Records are linked into a tree by `uuid`
//! and `parentUuid`, which feeds the lineage filtering shared across readers.

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 }
    }
}

/// The session id is the file stem. For a main session the stem equals the
/// `sessionId` field; subagent sidechains under `subagents/` carry the parent
/// session's `sessionId`, so the stem is the only collision-free identifier.
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();

        // Records do not begin with a fixed header line, so scan until the
        // first turn that carries the session metadata fields.
        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();
        // `tool_result` blocks reference their call by `tool_use_id` only, so
        // remember the name advertised by each preceding `tool_use` block.
        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(),
        });
    }

    // User turns are either plain text or a batch of tool results.
    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),
    })
}