Skip to content

A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io

5.5 KB
Raw Blame
//! Qoder's committed transcript records use the same user/assistant/tool block
//! schema as Claude. Reuse its reader while keeping discovery and roots separate.
use super::{
    claude,
    utils::{FileParseCache, ScanStatus},
};
use crate::{
    app_config::AppType,
    qoder_config::{self, QoderEdition},
    session_manager::{SessionMessage, SessionMeta},
};
use std::{
    path::{Path, PathBuf},
    sync::LazyLock,
};

static GLOBAL_CACHE: LazyLock<FileParseCache> = LazyLock::new(FileParseCache::new);
static CN_CACHE: LazyLock<FileParseCache> = LazyLock::new(FileParseCache::new);

/// 两个版本的会话都列出来,各自带着对应版本的恢复命令。
pub fn session_roots() -> Vec<PathBuf> {
    QoderEdition::ALL
        .into_iter()
        .map(|edition| qoder_config::data_dir(edition).join("projects"))
        .collect()
}

pub fn scan_sessions() -> Vec<SessionMeta> {
    scan_sessions_with_status(&mut ScanStatus::default())
}

pub(crate) fn scan_sessions_with_status(status: &mut ScanStatus) -> Vec<SessionMeta> {
    let mut sessions = Vec::new();
    for edition in QoderEdition::ALL {
        let cache: &FileParseCache = match edition {
            QoderEdition::Cn => &CN_CACHE,
            QoderEdition::Global => &GLOBAL_CACHE,
        };
        let root = qoder_config::data_dir(edition).join("projects");
        sessions.extend(scan_root(edition, &root, cache, status));
    }
    sessions
}

fn scan_root(
    edition: QoderEdition,
    root: &Path,
    cache: &FileParseCache,
    status: &mut ScanStatus,
) -> Vec<SessionMeta> {
    let mut files = Vec::new();
    for project in status.entries(root) {
        let project = project.path();
        if !status.is_dir(&project) {
            continue;
        }
        // Subagents and session sidecars live one level deeper. They are not
        // standalone conversations and must not be listed a second time.
        for entry in status.entries(&project) {
            let path = entry.path();
            if path.extension().is_some_and(|ext| ext == "jsonl")
                && entry.file_type().is_ok_and(|kind| kind.is_file())
            {
                files.push(path);
            }
        }
    }
    let mut sessions = cache.scan_with_status(files, claude::scan_session_file, status);
    for session in &mut sessions {
        session.provider_id = AppType::Qoder.as_str().into();
        session.resume_command = Some(resume_command(edition, &session.session_id));
    }
    sessions
}

fn resume_command(edition: QoderEdition, id: &str) -> String {
    let (variable, default_directory) = if edition == QoderEdition::Cn {
        ("QODERCN_CONFIG_DIR", ".qoder-cn")
    } else {
        ("QODER_CONFIG_DIR", ".qoder")
    };
    let directory = qoder_config::data_dir(edition);
    let command = qoder_config::cli_executable(edition);
    let quote = |text: &str| {
        if cfg!(windows) {
            format!("'{}'", text.replace('\'', "''"))
        } else {
            format!("'{}'", text.replace('\'', "'\"'\"'"))
        }
    };
    let command = format!("{command} --resume {}", quote(id));
    if directory == crate::config::get_home_dir().join(default_directory) {
        command
    } else if cfg!(windows) {
        format!(
            "$env:{variable}={}; {command}",
            quote(&directory.to_string_lossy())
        )
    } else {
        format!(
            "{variable}={} {command}",
            quote(&directory.to_string_lossy())
        )
    }
}

pub fn load_messages(path: &Path) -> Result<Vec<SessionMessage>, String> {
    let (_, path) = crate::session_manager::content::resolve_under_roots(
        AppType::Qoder.as_str(),
        path,
        &session_roots(),
    )?;
    claude::load_messages(&path)
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn qoder_discovers_editions_independently_and_skips_subagents() {
        let dir = tempfile::tempdir().unwrap();
        let project = dir.path().join("project");
        std::fs::create_dir_all(project.join("session/subagents")).unwrap();
        let records = [
            json!({"type":"user","sessionId":"session","cwd":"/work/repo","uuid":"u1","timestamp":"2026-10-22T00:00:00Z","message":{"role":"user","content":"Fix Qoder integration"}}),
            json!({"type":"assistant","sessionId":"session","uuid":"a1","parentUuid":"u1","timestamp":"2026-10-22T00:01:00Z","message":{"role":"assistant","content":[{"type":"text","text":"Done"}]}}),
        ];
        let text = records
            .iter()
            .map(|value| value.to_string())
            .collect::<Vec<_>>()
            .join("\n");
        std::fs::write(project.join("session.jsonl"), &text).unwrap();
        std::fs::write(project.join("session/subagents/agent-test.jsonl"), &text).unwrap();
        for edition in QoderEdition::ALL {
            let sessions = scan_root(
                edition,
                dir.path(),
                &FileParseCache::new(),
                &mut ScanStatus::default(),
            );
            assert_eq!(sessions.len(), 1);
            assert_eq!(sessions[0].provider_id, "qoder");
            assert_eq!(sessions[0].project_dir.as_deref(), Some("/work/repo"));
            assert!(sessions[0]
                .resume_command
                .as_ref()
                .unwrap()
                .contains(qoder_config::cli_executable(edition)));
            let messages = claude::load_messages(&project.join("session.jsonl")).unwrap();
            assert_eq!(messages.len(), 2);
            assert_eq!(messages[1].content, "Done");
        }
    }
}