Skip to content

armgit / Wake

Public Imported from GitHub
0

All your AI agent sessions in one place — browse, search, resume. Rust + GPUI.

Rust
18.9 KB
Raw Blame
use super::grok_group::{self, GroupCtx};
use super::parse_utils::*;
use super::AgentAdapter;
use crate::models::*;
use anyhow::Result;
use serde_json::Value;
use std::collections::HashMap;
use std::fs;
use std::io::BufReader;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};

/// Grok Build(xAI 的 coding CLI):`~/.grok/sessions/<url编码cwd>/<uuid>/` 一目录
/// 一会话。主文件 `updates.jsonl` 是 ACP 风格完整流水({timestamp(unix秒),
/// method:"session/update",params:{update:{sessionUpdate:…}}}),chunk 流式落盘
/// 需按角色段合并;`chat_history.jsonl` 是被压缩过的上下文快照,不能当全量史。
/// `summary.json` 边车给 cwd/标题/git 信息。同级 `session_search.sqlite` 是
/// Grok 自己的索引,`prompt_history.jsonl` 在 cwd 目录级——都不是会话文件。
pub struct GrokAdapter {
    root: PathBuf,
    grok_home: PathBuf,
    group: Mutex<Option<Arc<GroupCtx>>>,
    /// 确认过的 home(`homes()`):默认实例与选中 home 形态(里面有 sessions/)的自定义根才有。
    /// 与 `grok_home` 不同——直接选了名叫 sessions 的目录时,读边车会按名字去父目录找,
    /// 但那不是用户选的范围,skill 不往那里看(Codex review 2026-10-15)
    home: Option<PathBuf>,
}

impl GrokAdapter {
    pub fn new() -> Self {
        let grok_home = super::home_dir().unwrap_or_default().join(".grok");
        Self {
            root: grok_home.join("sessions"),
            home: Some(grok_home.clone()),
            grok_home,
            group: Mutex::new(None),
        }
    }

    fn from_custom_dir(dir: PathBuf) -> Self {
        let (root, grok_home, home) = if dir.join("sessions").is_dir() {
            (dir.join("sessions"), dir.clone(), Some(dir))
        } else if dir.file_name().is_some_and(|name| name == "sessions") {
            let parent = dir.parent().unwrap_or(&dir).to_path_buf();
            (dir, parent, None)
        } else {
            // 保留旧行为:自定义目录也可以直接就是 sessions root。
            // 此时没有结构证据允许越过用户选择的边界去父目录读 sidecar。
            (dir.clone(), dir, None)
        };
        Self {
            root,
            grok_home,
            group: Mutex::new(None),
            home,
        }
    }

    fn group_ctx(&self) -> Arc<GroupCtx> {
        let mut group = self.group.lock().unwrap();
        if group.is_none() {
            *group = Some(grok_group::load_group_ctx(&self.root, &self.grok_home));
        }
        Arc::clone(group.as_ref().unwrap())
    }
}

/// summary.json 边车
struct Summary {
    cwd: String,
    title: String,
    created_ms: i64,
    updated_ms: i64,
    git_branch: Option<String>,
    model: Option<String>,
    git_remotes: Vec<String>,
}

fn read_summary(updates_path: &Path) -> Summary {
    let mut s = Summary {
        cwd: String::new(),
        title: String::new(),
        created_ms: 0,
        updated_ms: 0,
        git_branch: None,
        model: None,
        git_remotes: Vec::new(),
    };
    let path = updates_path.with_file_name("summary.json");
    if let Ok(raw) = fs::read_to_string(&path) {
        if let Ok(v) = serde_json::from_str::<Value>(&raw) {
            if let Some(c) = v
                .get("info")
                .and_then(|i| i.get("cwd"))
                .and_then(|x| x.as_str())
            {
                s.cwd = c.to_string();
            }
            // generated_title 与 session_summary 通常同值,前者更语义化
            for k in ["generated_title", "session_summary"] {
                if let Some(t) = v.get(k).and_then(|x| x.as_str()) {
                    if !t.trim().is_empty() {
                        s.title = t.to_string();
                        break;
                    }
                }
            }
            if let Some(t) = v.get("created_at").and_then(|x| x.as_str()) {
                s.created_ms = iso_ms(t);
            }
            if let Some(t) = v.get("updated_at").and_then(|x| x.as_str()) {
                s.updated_ms = iso_ms(t);
            }
            s.git_branch = v
                .get("head_branch")
                .and_then(|x| x.as_str())
                .filter(|b| !b.is_empty())
                .map(String::from);
            s.model = v
                .get("current_model_id")
                .and_then(|x| x.as_str())
                .filter(|m| !m.is_empty())
                .map(String::from);
            if let Some(remotes) = v.get("git_remotes").and_then(Value::as_array) {
                s.git_remotes = remotes
                    .iter()
                    .filter_map(Value::as_str)
                    .filter(|remote| !remote.is_empty())
                    .map(String::from)
                    .collect();
            }
        }
    }
    s
}

/// 流水重放的段状态:一个用户回合断一次,assistant 期间 thought/message/tool
/// 交错全并入同一条助手消息
struct Replay {
    messages: Vec<TranscriptMessage>,
    tool_index: HashMap<String, (usize, usize)>,
    /// 当前累积段;None = 段外
    cur_role: Option<Role>,
    cur_text: String,
    cur_thinking: String,
    cur_tools: Vec<ToolCallView>,
    cur_images: Vec<ImageAttachment>,
    cur_ts: i64,
}

impl Replay {
    fn new() -> Self {
        Self {
            messages: Vec::new(),
            tool_index: HashMap::new(),
            cur_role: None,
            cur_text: String::new(),
            cur_thinking: String::new(),
            cur_tools: Vec::new(),
            cur_images: Vec::new(),
            cur_ts: 0,
        }
    }

    fn flush(&mut self) {
        let Some(role) = self.cur_role.take() else {
            return;
        };
        let text = std::mem::take(&mut self.cur_text);
        let thinking = std::mem::take(&mut self.cur_thinking);
        let tools = std::mem::take(&mut self.cur_tools);
        let images = std::mem::take(&mut self.cur_images);
        if text.trim().is_empty()
            && thinking.trim().is_empty()
            && tools.is_empty()
            && images.is_empty()
        {
            return;
        }
        let mut m = text_msg(role, &text, self.cur_ts);
        if !thinking.trim().is_empty() {
            m.thinking = Some(clip(thinking.trim(), MAX_TOOL_IO).0);
        }
        m.images = images;
        let base = self.messages.len();
        for (ix, tc) in tools.into_iter().enumerate() {
            self.tool_index.insert(tc.id.clone(), (base, ix));
            m.tool_calls.push(tc);
        }
        self.messages.push(m);
    }

    /// 进入(或延续)一个角色段
    fn ensure(&mut self, role: Role, ts: i64) {
        if self.cur_role != Some(role) {
            self.flush();
            self.cur_role = Some(role);
            self.cur_ts = ts;
        }
    }
}

/// user/agent chunk 的 content 文本({content:{type:text,text}}),借用零分配
fn chunk_text(update: &Value) -> &str {
    update
        .pointer("/content/text")
        .and_then(Value::as_str)
        .unwrap_or_default()
}

/// Grok 的文本 chunk 是增量片段,不能经 `content_parts` 的 trim/join,
/// 否则模型在 token 边界写入的空格会被吃掉。只有图片块走通用附件解析。
fn append_message_chunk(rp: &mut Replay, update: &Value, role: Role, ts: i64, decode_images: bool) {
    rp.ensure(role, ts);
    let content = update.get("content").unwrap_or(&Value::Null);
    if has_image_content(content) {
        let parsed = content_parts(content, decode_images);
        let mut combined = ParsedContent {
            text: std::mem::take(&mut rp.cur_text),
            images: std::mem::take(&mut rp.cur_images),
        };
        combined.append_with_separator(parsed, "");
        rp.cur_text = combined.text;
        rp.cur_images = combined.images;
    } else {
        rp.cur_text.push_str(chunk_text(update));
    }
}

/// tool_call_update 回填:完成态带 content(文本输出,覆盖式取最终态),
/// status failed 置错;初始 tool_call 缺 rawInput 时由 update 补
fn apply_tool_update(
    tc: &mut ToolCallView,
    update: &Value,
    decode_images: bool,
) -> Vec<ImageAttachment> {
    let mut images = Vec::new();
    if let Some(content) = update.get("content") {
        let parsed = content_parts(content, decode_images);
        if !parsed.text.is_empty() {
            tc.output = Some(clip(&parsed.text, MAX_TOOL_IO).0);
        }
        images = parsed.images;
    }
    if update.get("status").and_then(|v| v.as_str()) == Some("failed") {
        tc.is_error = true;
    }
    if tc.input.is_none() {
        if let Some(raw) = update.get("rawInput").filter(|r| !r.is_null()) {
            tc.input_preview = make_preview(raw);
            if let Ok(json) = serde_json::to_string_pretty(raw) {
                tc.input = Some(clip(&json, MAX_TOOL_IO).0);
            }
        }
    }
    images
}

/// `sessionUpdate` 的分类。解析按它分派,`file_ref` 判空壳也问它(`opens_message`),
/// 两边是同一个判据、不会分家
#[derive(Clone, Copy)]
enum UpdateKind {
    /// user / agent 的文本 chunk
    Chunk(Role),
    Thought,
    ToolCall,
    /// 只回填已有的调用,自己不起消息
    ToolCallUpdate,
    /// 已知的非内容更新:任务后台化 / 压缩等
    Known,
    Unknown,
}

impl UpdateKind {
    fn of(update: &Value) -> Self {
        match update.get("sessionUpdate").and_then(Value::as_str) {
            Some("user_message_chunk") => Self::Chunk(Role::User),
            Some("agent_message_chunk") => Self::Chunk(Role::Assistant),
            Some("agent_thought_chunk") => Self::Thought,
            Some("tool_call") => Self::ToolCall,
            Some("tool_call_update") => Self::ToolCallUpdate,
            Some(
                "task_backgrounded"
                | "task_completed"
                | "auto_compact_started"
                | "auto_compact_completed"
                | "compaction_checkpoint"
                | "plan"
                | "current_mode_update",
            ) => Self::Known,
            _ => Self::Unknown,
        }
    }

    fn opens_message(self) -> bool {
        match self {
            Self::Chunk(_) | Self::Thought | Self::ToolCall => true,
            Self::ToolCallUpdate | Self::Known | Self::Unknown => false,
        }
    }
}

/// 一条会起消息的更新都没有的 updates.jsonl:Grok 一开会话就落盘,没发消息就关掉的
/// 会话只剩插件钩子之类的流水,原先被收成一条条 Untitled 空记录(PR #55)。流式读、
/// 命中即停,不设大小上限(理由同 cursor.rs 的 `is_turn_marker_only`:钩子输出多大
/// 没有结构保证)。读不出来按有内容放行,交给解析去报错
fn is_empty_shell(path: &Path) -> bool {
    let Ok(file) = fs::File::open(path) else {
        return false;
    };
    for row in jsonl_values(BufReader::new(file)) {
        let Ok(row) = row else {
            return false;
        };
        let update = row.as_ref().and_then(|row| row.pointer("/params/update"));
        if update.is_some_and(|update| UpdateKind::of(update).opens_message()) {
            return false;
        }
    }
    true
}

fn parse_grok_updates(path: &Path, decode_images: bool) -> Result<(Vec<TranscriptMessage>, u32)> {
    let _image_budget = transcript_image_decode_budget(decode_images);
    let file = fs::File::open(path)?;
    let reader = BufReader::with_capacity(1 << 20, file);
    let mut rp = Replay::new();
    let mut unknown = 0u32;

    for row in jsonl_values(reader) {
        let Some(row) = row? else {
            unknown += 1;
            continue;
        };
        let ts = row.get("timestamp").map(to_epoch_ms).unwrap_or(0);
        let Some(update) = row.get("params").and_then(|p| p.get("update")) else {
            unknown += 1;
            continue;
        };
        match UpdateKind::of(update) {
            UpdateKind::Chunk(role) => {
                append_message_chunk(&mut rp, update, role, ts, decode_images);
            }
            UpdateKind::Thought => {
                rp.ensure(Role::Assistant, ts);
                rp.cur_thinking.push_str(chunk_text(update));
            }
            UpdateKind::ToolCall => {
                rp.ensure(Role::Assistant, ts);
                let id = update
                    .get("toolCallId")
                    .and_then(|v| v.as_str())
                    .unwrap_or_default();
                let name = update
                    .get("title")
                    .and_then(|v| v.as_str())
                    .unwrap_or_default();
                let input = update.get("rawInput").cloned().unwrap_or(Value::Null);
                rp.cur_tools
                    .push(tool_call_view(id.to_string(), name, &input, None, false));
            }
            UpdateKind::ToolCallUpdate => {
                let Some(id) = update.get("toolCallId").and_then(|v| v.as_str()) else {
                    continue;
                };
                // 目标 tool 可能还在当前未 flush 的段里,也可能已入 messages
                if let Some(tool_ix) = rp.cur_tools.iter().position(|tc| tc.id == id) {
                    let images =
                        apply_tool_update(&mut rp.cur_tools[tool_ix], update, decode_images);
                    let mut images = images;
                    for image in &mut images {
                        image.text_offset = rp.cur_text.len();
                    }
                    rp.cur_images.extend(images);
                } else if let Some(&(mi, ti)) = rp.tool_index.get(id) {
                    let message = &mut rp.messages[mi];
                    let images =
                        apply_tool_update(&mut message.tool_calls[ti], update, decode_images);
                    append_images_to_message_end(message, images);
                }
            }
            UpdateKind::Known => {}
            UpdateKind::Unknown => {
                unknown += 1;
            }
        }
    }
    rp.flush();
    let mut messages = rp.messages;
    assign_seq(&mut messages);
    Ok((messages, unknown))
}

fn build_meta(
    r: &SessionFileRef,
    side: &Summary,
    messages: &[TranscriptMessage],
    grok_home: &Path,
    group: &GroupCtx,
) -> SessionMeta {
    let title = session_title(&side.title, false, messages);
    let msg_ts_max = messages
        .iter()
        .filter_map(|m| m.timestamp)
        .max()
        .unwrap_or(0);
    let (project_path, project_name) =
        grok_group::canonical_project(&side.cwd, &side.git_remotes, grok_home, &r.native_id, group);
    SessionMeta {
        key: format!("grok:{}", r.native_id),
        host: String::new(),
        id: r.native_id.clone(),
        agent: AgentId::Grok,
        title,
        project_path,
        project_name,
        file_path: r.file_path.clone(),
        created_at: if side.created_ms > 0 {
            side.created_ms
        } else {
            r.mtime_ms
        },
        updated_at: match side.updated_ms.max(msg_ts_max) {
            t if t > 0 => t,
            _ => r.mtime_ms,
        },
        message_count: messages
            .iter()
            .filter(|m| m.kind == MessageKind::Text)
            .count() as i64,
        size_bytes: r.size,
        git_branch: side.git_branch.clone(),
        model: side.model.clone(),
        tokens_used: None,
        archived: false,
        source: None,
        spawned: grok_group::is_subagent(group, &r.native_id),
        favorite: false,
        pinned: false,
    }
}

impl AgentAdapter for GrokAdapter {
    fn agent(&self) -> AgentId {
        AgentId::Grok
    }

    fn begin_scan(&self) {
        *self.group.lock().unwrap() = Some(grok_group::load_group_ctx(&self.root, &self.grok_home));
    }

    fn manages_parent_links(&self) -> bool {
        true
    }

    fn parent_links(&self) -> Option<Vec<(String, String)>> {
        Some(grok_group::parent_links(&self.group_ctx()))
    }

    /// 登记表里的子会话都是 subagent 派出去的,而登记长在父会话目录里
    fn linked_children_are_spawned(&self) -> bool {
        true
    }

    fn is_snapshot_event(&self, path: &Path) -> bool {
        path.strip_prefix(&self.root).ok().is_some_and(|relative| {
            relative
                .components()
                .any(|component| component.as_os_str() == "subagents")
        })
    }

    fn list_session_files(&self) -> Result<Vec<SessionFileRef>> {
        let mut refs = Vec::new();
        let Ok(cwds) = fs::read_dir(&self.root) else {
            return Ok(refs);
        };
        // session_search.sqlite 等根级文件对 read_dir 自然失败跳过;
        // 会话主文件的判定(存在、非空、不是空壳、native_id)统一走 file_ref
        for cwd_dir in cwds.flatten() {
            let Ok(sessions) = fs::read_dir(cwd_dir.path()) else {
                continue;
            };
            for sess in sessions.flatten() {
                if let Some(r) = self.file_ref(&sess.path().join("updates.jsonl")) {
                    refs.push(r);
                }
            }
        }
        Ok(refs)
    }

    fn file_ref(&self, path: &Path) -> Option<SessionFileRef> {
        // 只认会话目录里的 updates.jsonl;prompt_history/chat_history/events 都不是主文件
        if path.file_name()?.to_string_lossy() != "updates.jsonl" {
            return None;
        }
        let session_dir = path.parent()?;
        let mut r = default_file_ref(self.agent(), path)?;
        if is_empty_shell(path) {
            return None;
        }
        r.native_id = session_dir.file_name()?.to_string_lossy().to_string();
        Some(r)
    }

    fn session_paths(&self, meta: &SessionMeta) -> Vec<String> {
        // 会话是 <uuid>/ 整个目录(summary/events/plan 等边车),整目录进废纸篓
        Path::new(&meta.file_path)
            .parent()
            .map(|d| vec![d.to_string_lossy().to_string()])
            .unwrap_or_else(|| vec![meta.file_path.clone()])
    }

    fn parse_session(&self, r: &SessionFileRef) -> Result<ParsedSession> {
        let (messages, unknown) = parse_grok_updates(Path::new(&r.file_path), false)?;
        let side = read_summary(Path::new(&r.file_path));
        let meta = build_meta(r, &side, &messages, &self.grok_home, &self.group_ctx());
        Ok(ParsedSession::derive(meta, &messages, unknown))
    }

    fn parse_transcript(&self, r: &SessionFileRef) -> Result<ParsedTranscript> {
        let (messages, unknown) = parse_grok_updates(Path::new(&r.file_path), true)?;
        let side = read_summary(Path::new(&r.file_path));
        Ok(ParsedTranscript {
            meta: build_meta(r, &side, &messages, &self.grok_home, &self.group_ctx()),
            mainline: messages,
            sidechains: Vec::new(),
            unknown_line_count: unknown,
        })
    }

    fn cleanup_paths(&self, meta: &SessionMeta) -> Option<Vec<String>> {
        Some(self.session_paths(meta))
    }

    fn with_custom_root(&self, dir: PathBuf) -> Box<dyn AgentAdapter> {
        Box::new(Self::from_custom_dir(dir))
    }

    fn data_roots(&self) -> Vec<PathBuf> {
        vec![self.root.clone()]
    }

    fn homes(&self) -> Vec<PathBuf> {
        self.home.iter().cloned().collect()
    }
}