| 1 | //! Import sessions created by other agent harnesses (Claude Code, Codex CLI, |
| 2 | //! OpenCode, PiCodingAgent, GrokBuild) into coding-agent's own session store so |
| 3 | //! they can be continued and show up in `/resume` like any local session. |
| 4 | //! |
| 5 | //! Each harness persists a human-readable transcript on disk; we read it back, |
| 6 | //! translate the messages into coding-agent's canonical form |
| 7 | //! (`role`/`content`/`tool_calls`/`tool_call_id`/`images`) and insert them into |
| 8 | //! the `sessions`/`messages` tables. A `source` column on the sessions table |
| 9 | //! records which external session a row came from so re-importing refreshes |
| 10 | //! the transcript in place instead of duplicating it. |
| 11 | |
| 12 | use std::collections:: |
| 13 | use std::fs; |
| 14 | use std::path:: |
| 15 | |
| 16 | use anyhow::{Context |
| 17 | use rusqlite::{Connection |
| 18 | use serde_json::{Value |
| 19 | |
| 20 | use crate::db; |
| 21 | use crate::llm::{ImagePart |
| 22 | |
| 23 | #[derive(Clone |
| 24 | pub enum Tool |
| 25 | ClaudeCode, |
| 26 | Codex, |
| 27 | OpenCode, |
| 28 | Pi, |
| 29 | GrokBuild, |
| 30 | } |
| 31 | |
| 32 | impl Tool { |
| 33 | pub fn parse( |
| 34 | match s.trim() |
| 35 | "claude" | "claude-code" |
| 36 | "codex" | "codex-cli" |
| 37 | "opencode" | "oc" |
| 38 | "pi" | "picoding" |
| 39 | "grok" | "grokbuild" |
| 40 | _ => None |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | pub fn name( |
| 45 | match self { |
| 46 | Tool::ClaudeCode |
| 47 | Tool::Codex |
| 48 | Tool::OpenCode |
| 49 | Tool::Pi |
| 50 | Tool::GrokBuild |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | pub fn label( |
| 55 | match self { |
| 56 | Tool::ClaudeCode |
| 57 | Tool::Codex |
| 58 | Tool::OpenCode |
| 59 | Tool::Pi |
| 60 | Tool::GrokBuild |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | pub fn all() |
| 65 | vec![ |
| 66 | Tool::ClaudeCode |
| 67 | Tool::Codex |
| 68 | Tool::OpenCode |
| 69 | Tool::Pi |
| 70 | Tool::GrokBuild |
| 71 | ] |
| 72 | } |
| 73 | |
| 74 | pub fn names() |
| 75 | Tool::all() |
| 76 | .iter() |
| 77 | .map(|t| |
| 78 | .collect::<Vec |
| 79 | .join(", ") |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /// A single external session as shown in the import picker. |
| 84 | pub struct ExternalSession |
| 85 | /// Stable reference: the transcript's file path, or an OpenCode session id. |
| 86 | pub id: String |
| 87 | pub title: String |
| 88 | pub cwd: String |
| 89 | pub model: String |
| 90 | pub updated_at: i64 |
| 91 | } |
| 92 | |
| 93 | /// A message in coding-agent's canonical form, ready to be stored. |
| 94 | pub struct ImportedMessage |
| 95 | pub role: String |
| 96 | pub content: String |
| 97 | pub tool_call_id: Option |
| 98 | pub name: Option |
| 99 | pub tool_calls: Vec |
| 100 | pub images: Vec |
| 101 | } |
| 102 | |
| 103 | pub struct Imported |
| 104 | pub id: i64 |
| 105 | pub uuid: String |
| 106 | pub name: String |
| 107 | pub model: String |
| 108 | pub message_count: usize |
| 109 | } |
| 110 | |
| 111 | const MAX_TITLE: usize |
| 112 | |
| 113 | fn trunc(s: |
| 114 | let chars: Vec |
| 115 | let mut out |
| 116 | if s.chars() |
| 117 | out.push('… |
| 118 | } |
| 119 | out |
| 120 | } |
| 121 | |
| 122 | fn home() -> PathBuf |
| 123 | std::env::var |
| 124 | } |
| 125 | |
| 126 | fn env_dir(var: |
| 127 | if let Ok |
| 128 | && !p.trim |
| 129 | { |
| 130 | PathBuf::from( |
| 131 | } else { |
| 132 | fallback |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | fn parse_iso(s: |
| 137 | chrono::DateTime:: |
| 138 | .ok() |
| 139 | .map(|d| |
| 140 | } |
| 141 | |
| 142 | fn file_mtime(path: |
| 143 | fs::metadata(path |
| 144 | .and_then(|m| |
| 145 | .ok() |
| 146 | .and_then(|t| |
| 147 | .map(|d| |
| 148 | .unwrap_or(0) |
| 149 | } |
| 150 | |
| 151 | fn read_lines(path: |
| 152 | fs::read_to_string(path |
| 153 | .map(|s| |
| 154 | .unwrap_or_default() |
| 155 | } |
| 156 | |
| 157 | /// Walk a directory tree collecting every `*.jsonl` file (skipping hidden |
| 158 | /// `*.lock` variants and subagent subdirectories). |
| 159 | fn walk_jsonl(dir: |
| 160 | if out.len() |
| 161 | return; |
| 162 | } |
| 163 | let Ok( |
| 164 | return; |
| 165 | }; |
| 166 | for e in rd |
| 167 | let p = |
| 168 | if p.is_dir() |
| 169 | walk_jsonl(&p, |
| 170 | } else if p |
| 171 | .file_name() |
| 172 | .and_then(|s| |
| 173 | .map(|s| |
| 174 | .unwrap_or(false) |
| 175 | && !p.to_string_lossy |
| 176 | { |
| 177 | out.push(p |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /// Flatten a `content` value (plain string, or an array of text/image blocks) |
| 183 | /// into plain text. |
| 184 | fn flatten_text(v: |
| 185 | match v { |
| 186 | Value::String( |
| 187 | Value::Array( |
| 188 | .iter() |
| 189 | .filter_map(|b| |
| 190 | .collect::<Vec |
| 191 | .join("\n" |
| 192 | Value::Null |
| 193 | _ => String |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | fn first_user_text(lines: |
| 198 | for raw in lines |
| 199 | let Ok( |
| 200 | continue; |
| 201 | }; |
| 202 | if v.get( |
| 203 | continue; |
| 204 | } |
| 205 | let text = |
| 206 | .get("message") |
| 207 | .and_then(|m| |
| 208 | .map(flatten_text) |
| 209 | .unwrap_or_default(); |
| 210 | if !text.trim |
| 211 | return Some(trunc |
| 212 | } |
| 213 | } |
| 214 | None |
| 215 | } |
| 216 | |
| 217 | // --------------------------------------------------------------------------- |
| 218 | // Discovery: list sessions a harness has on disk. |
| 219 | // --------------------------------------------------------------------------- |
| 220 | |
| 221 | /// List up to `limit` most-recently-updated sessions from a harness. |
| 222 | pub fn discover(tool |
| 223 | let mut sessions |
| 224 | Tool::ClaudeCode |
| 225 | Tool::Codex |
| 226 | Tool::OpenCode |
| 227 | Tool::Pi |
| 228 | Tool::GrokBuild |
| 229 | }; |
| 230 | sessions.sort_by_key(|s |
| 231 | sessions.truncate(limit |
| 232 | Ok(sessions) |
| 233 | } |
| 234 | |
| 235 | fn discover_claude() -> Result |
| 236 | let root = |
| 237 | if !root.is_dir |
| 238 | return Ok(Vec |
| 239 | } |
| 240 | let mut files |
| 241 | walk_jsonl(&root, |
| 242 | let mut out |
| 243 | for f in files |
| 244 | let Some( |
| 245 | continue; |
| 246 | }; |
| 247 | out.push(s |
| 248 | } |
| 249 | Ok(out) |
| 250 | } |
| 251 | |
| 252 | fn parse_claude_meta(path: |
| 253 | let mtime = |
| 254 | let mut title |
| 255 | let mut session_id |
| 256 | let mut created |
| 257 | let mut updated |
| 258 | for raw in read_lines |
| 259 | let Ok( |
| 260 | continue; |
| 261 | }; |
| 262 | let Some( |
| 263 | continue; |
| 264 | }; |
| 265 | match t { |
| 266 | "ai-title" | "custom-title" |
| 267 | if let Some |
| 268 | .get("aiTitle") |
| 269 | .or_else(|| v |
| 270 | .or_else(|| v |
| 271 | .and_then(|x| |
| 272 | .map(str::trim |
| 273 | .filter(|s| |
| 274 | { |
| 275 | title = s. |
| 276 | } |
| 277 | } |
| 278 | "summary" => { |
| 279 | if title.is_empty() |
| 280 | && let Some |
| 281 | { |
| 282 | title = s. |
| 283 | } |
| 284 | } |
| 285 | "last-prompt" => { |
| 286 | if title.is_empty() |
| 287 | && let Some |
| 288 | { |
| 289 | title = trunc( |
| 290 | } |
| 291 | } |
| 292 | _ => {} |
| 293 | } |
| 294 | if session_id.is_empty() |
| 295 | && let Some |
| 296 | { |
| 297 | session_id = id. |
| 298 | } |
| 299 | if let Some |
| 300 | .get("timestamp") |
| 301 | .and_then(|x| |
| 302 | .and_then(parse_iso) |
| 303 | { |
| 304 | created = created. |
| 305 | updated = updated. |
| 306 | } |
| 307 | } |
| 308 | if title.is_empty() |
| 309 | title = first_user_text( |
| 310 | } |
| 311 | let id = |
| 312 | path.file_stem()?.to_string_lossy |
| 313 | } else { |
| 314 | session_id |
| 315 | }; |
| 316 | let cwd = |
| 317 | .parent() |
| 318 | .and_then(|p| |
| 319 | .map(|s| |
| 320 | .unwrap_or_default(); |
| 321 | Some(ExternalSession |
| 322 | id, |
| 323 | title: trunc(& |
| 324 | cwd, |
| 325 | model: String |
| 326 | updated_at: updated.max |
| 327 | }) |
| 328 | } |
| 329 | |
| 330 | fn codex_root() -> PathBuf |
| 331 | env_dir("CODEX_HOME", |
| 332 | } |
| 333 | |
| 334 | fn discover_codex() -> Result |
| 335 | let root = |
| 336 | if !root.is_dir |
| 337 | return Ok(Vec |
| 338 | } |
| 339 | let mut files |
| 340 | walk_jsonl(&root, |
| 341 | let mut out |
| 342 | for f in files |
| 343 | if !f |
| 344 | .file_name() |
| 345 | .and_then(|s| |
| 346 | .map(|s| |
| 347 | .unwrap_or(false) |
| 348 | { |
| 349 | continue; |
| 350 | } |
| 351 | let Some( |
| 352 | continue; |
| 353 | }; |
| 354 | out.push(s |
| 355 | } |
| 356 | Ok(out) |
| 357 | } |
| 358 | |
| 359 | fn parse_codex_meta(path: |
| 360 | let lines = |
| 361 | let mut id |
| 362 | let mut cwd |
| 363 | for raw in & |
| 364 | let Ok( |
| 365 | continue; |
| 366 | }; |
| 367 | if v.get( |
| 368 | continue; |
| 369 | } |
| 370 | let p = |
| 371 | id = p |
| 372 | .get("id") |
| 373 | .and_then(|x| |
| 374 | .unwrap_or("") |
| 375 | .to_string(); |
| 376 | cwd = p |
| 377 | .get("cwd") |
| 378 | .and_then(|x| |
| 379 | .unwrap_or("") |
| 380 | .to_string(); |
| 381 | break; |
| 382 | } |
| 383 | if id.is_empty() |
| 384 | return None; |
| 385 | } |
| 386 | let title = |
| 387 | Some(ExternalSession |
| 388 | id, |
| 389 | title: trunc(& |
| 390 | cwd, |
| 391 | model: String |
| 392 | updated_at: file_mtime(path |
| 393 | }) |
| 394 | } |
| 395 | |
| 396 | fn opencode_db_path() -> PathBuf |
| 397 | if let Ok |
| 398 | && !p.trim |
| 399 | { |
| 400 | return PathBuf::from |
| 401 | } |
| 402 | let base = |
| 403 | base.join("opencode |
| 404 | } |
| 405 | |
| 406 | fn open_opencode(path: |
| 407 | // OpenCode runs with WAL; read-only opens can fail without write access to |
| 408 | // the shm file, so fall back to a normal open for a pure SELECT workload. |
| 409 | Connection::open_with_flags( |
| 410 | .or_else(|_| |
| 411 | .with_context(|| format! |
| 412 | } |
| 413 | |
| 414 | fn discover_opencode() -> Result |
| 415 | let path = |
| 416 | if !path.is_file |
| 417 | return Ok(Vec |
| 418 | } |
| 419 | let conn = |
| 420 | let mut stmt |
| 421 | "SELECT id, title, directory, model, time_updated FROM session |
| 422 | ORDER BY time_updated DESC LIMIT 200", |
| 423 | )?; |
| 424 | let rows = |
| 425 | Ok(( |
| 426 | r.get::<_ |
| 427 | r.get::<_ |
| 428 | r.get::<_ |
| 429 | r.get::<_ |
| 430 | r.get::<_ |
| 431 | )) |
| 432 | })?; |
| 433 | let mut out |
| 434 | for row in rows |
| 435 | let (id, |
| 436 | let model = |
| 437 | .and_then(|j| |
| 438 | .and_then(|v| |
| 439 | .unwrap_or_default(); |
| 440 | out.push(ExternalSession |
| 441 | id, |
| 442 | title: if title |
| 443 | "(untitled)".into() |
| 444 | } else { |
| 445 | trunc(&title, |
| 446 | }, |
| 447 | cwd: dir, |
| 448 | model, |
| 449 | updated_at: ts / |
| 450 | }); |
| 451 | } |
| 452 | Ok(out) |
| 453 | } |
| 454 | |
| 455 | fn pi_root() -> PathBuf |
| 456 | home().join(" |
| 457 | } |
| 458 | |
| 459 | fn discover_pi() -> Result |
| 460 | let root = |
| 461 | if !root.is_dir |
| 462 | return Ok(Vec |
| 463 | } |
| 464 | let mut files |
| 465 | walk_jsonl(&root, |
| 466 | let mut out |
| 467 | for f in files |
| 468 | let Some( |
| 469 | continue; |
| 470 | }; |
| 471 | out.push(s |
| 472 | } |
| 473 | Ok(out) |
| 474 | } |
| 475 | |
| 476 | fn parse_pi_meta(path: |
| 477 | let lines = |
| 478 | let mut id |
| 479 | let mut cwd |
| 480 | let mut model |
| 481 | let mut title |
| 482 | for raw in & |
| 483 | let Ok( |
| 484 | continue; |
| 485 | }; |
| 486 | match v.get( |
| 487 | Some("session") |
| 488 | id = v |
| 489 | .get("id") |
| 490 | .and_then(|x| |
| 491 | .unwrap_or("") |
| 492 | .to_string(); |
| 493 | cwd = v |
| 494 | .get("cwd") |
| 495 | .and_then(|x| |
| 496 | .unwrap_or("") |
| 497 | .to_string(); |
| 498 | } |
| 499 | Some("model_change") |
| 500 | if model.is_empty() |
| 501 | model = v |
| 502 | .get("modelId") |
| 503 | .and_then(|x| |
| 504 | .unwrap_or("") |
| 505 | .to_string(); |
| 506 | } |
| 507 | } |
| 508 | Some("session_info") |
| 509 | if let Some |
| 510 | .get("name") |
| 511 | .and_then(|x| |
| 512 | .map(str::trim |
| 513 | .filter(|s| |
| 514 | { |
| 515 | title = n. |
| 516 | } |
| 517 | } |
| 518 | _ => {} |
| 519 | } |
| 520 | } |
| 521 | if id.is_empty() |
| 522 | return None; |
| 523 | } |
| 524 | if title.is_empty() |
| 525 | title = first_user_text( |
| 526 | } |
| 527 | Some(ExternalSession |
| 528 | id, |
| 529 | title: trunc(& |
| 530 | cwd, |
| 531 | model, |
| 532 | updated_at: file_mtime(path |
| 533 | }) |
| 534 | } |
| 535 | |
| 536 | fn grok_root() -> PathBuf |
| 537 | env_dir("GROK_HOME", |
| 538 | } |
| 539 | |
| 540 | fn discover_grok() -> Result |
| 541 | let root = |
| 542 | if !root.is_dir |
| 543 | return Ok(Vec |
| 544 | } |
| 545 | let mut out |
| 546 | for dir in grok_session_dirs |
| 547 | let mut id |
| 548 | .file_name() |
| 549 | .map(|s| |
| 550 | .unwrap_or_default(); |
| 551 | let mut title |
| 552 | let mut cwd |
| 553 | let mut model |
| 554 | let mut updated |
| 555 | let summary = |
| 556 | if summary.is_file() |
| 557 | && let Ok |
| 558 | && let Ok |
| 559 | { |
| 560 | if let Some |
| 561 | .get("info") |
| 562 | .and_then(|i| |
| 563 | .and_then(|x| |
| 564 | { |
| 565 | id = i. |
| 566 | } |
| 567 | if let Some |
| 568 | .get("info") |
| 569 | .and_then(|i| |
| 570 | .and_then(|x| |
| 571 | { |
| 572 | cwd = c. |
| 573 | } |
| 574 | title = v |
| 575 | .get("session_summary") |
| 576 | .or_else(|| v |
| 577 | .and_then(|x| |
| 578 | .unwrap_or("") |
| 579 | .to_string(); |
| 580 | model = v |
| 581 | .get("current_model_id") |
| 582 | .and_then(|x| |
| 583 | .unwrap_or("") |
| 584 | .to_string(); |
| 585 | if let Some |
| 586 | .get("updated_at") |
| 587 | .and_then(|x| |
| 588 | .and_then(parse_iso) |
| 589 | { |
| 590 | updated = ts; |
| 591 | } |
| 592 | } |
| 593 | if title.is_empty() |
| 594 | && let Some |
| 595 | { |
| 596 | title = trunc( |
| 597 | } |
| 598 | out.push(ExternalSession |
| 599 | id, |
| 600 | title: if title |
| 601 | "(untitled)".into() |
| 602 | } else { |
| 603 | trunc(&title, |
| 604 | }, |
| 605 | cwd, |
| 606 | model, |
| 607 | updated_at: updated, |
| 608 | }); |
| 609 | } |
| 610 | Ok(out) |
| 611 | } |
| 612 | |
| 613 | fn first_grok_user(dir: |
| 614 | let f = |
| 615 | let lines = |
| 616 | for raw in & |
| 617 | let Ok( |
| 618 | continue; |
| 619 | }; |
| 620 | if v.get( |
| 621 | let t = |
| 622 | if !t.trim |
| 623 | return Some(t |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | None |
| 628 | } |
| 629 | |
| 630 | // --------------------------------------------------------------------------- |
| 631 | // Transcript parsing: translate a harness transcript into canonical messages. |
| 632 | // --------------------------------------------------------------------------- |
| 633 | |
| 634 | fn read_transcript(tool: |
| 635 | match tool { |
| 636 | Tool::OpenCode |
| 637 | Tool::GrokBuild |
| 638 | let dir = |
| 639 | .with_context(|| format! |
| 640 | parse_grok(&dir) |
| 641 | } |
| 642 | _ => { |
| 643 | let path = |
| 644 | .with_context(|| format! |
| 645 | match tool { |
| 646 | Tool::ClaudeCode |
| 647 | Tool::Codex |
| 648 | Tool::Pi |
| 649 | _ => unreachable! |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | /// Resolve a discovery id (session uuid) back to the transcript file path, |
| 656 | /// because Claude/Codex/Pi discovery returns ids, not paths. |
| 657 | fn resolve_path(tool: |
| 658 | let candidates = |
| 659 | Tool::ClaudeCode |
| 660 | let mut v |
| 661 | let root = |
| 662 | if root.is_dir() |
| 663 | walk_jsonl(&root, |
| 664 | } |
| 665 | v |
| 666 | } |
| 667 | Tool::Codex |
| 668 | let mut v |
| 669 | let root = |
| 670 | if root.is_dir() |
| 671 | walk_jsonl(&root, |
| 672 | } |
| 673 | v |
| 674 | } |
| 675 | Tool::Pi |
| 676 | let mut v |
| 677 | let root = |
| 678 | if root.is_dir() |
| 679 | walk_jsonl(&root, |
| 680 | } |
| 681 | v |
| 682 | } |
| 683 | Tool::GrokBuild |
| 684 | let mut v |
| 685 | let root = |
| 686 | if root.is_dir() |
| 687 | for e in fs |
| 688 | let dir = |
| 689 | if dir.is_dir() |
| 690 | let chat = |
| 691 | if chat.is_file() |
| 692 | v.push(chat |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | v |
| 698 | } |
| 699 | Tool::OpenCode |
| 700 | }; |
| 701 | for p in candidates |
| 702 | let name = |
| 703 | // Codex/Pi file names embed the session uuid; Claude's stem is the id. |
| 704 | if name.contains( |
| 705 | return Some(p |
| 706 | } |
| 707 | } |
| 708 | // If the id is itself an existing file path, use it directly. |
| 709 | if Path::new |
| 710 | return Some(PathBuf |
| 711 | } |
| 712 | None |
| 713 | } |
| 714 | |
| 715 | /// Locate a GrokBuild session directory by its session id (from summary.json). |
| 716 | fn grok_session_dir(id: |
| 717 | let root = |
| 718 | if !root.is_dir |
| 719 | return None; |
| 720 | } |
| 721 | for dir in grok_session_dirs |
| 722 | if dir |
| 723 | .file_name() |
| 724 | .map(|s| |
| 725 | .unwrap_or_default() |
| 726 | == id |
| 727 | { |
| 728 | return Some(dir |
| 729 | } |
| 730 | let summary = |
| 731 | if summary.is_file() |
| 732 | && let Ok |
| 733 | && let Ok |
| 734 | && v.get( |
| 735 | .and_then(|i| |
| 736 | .and_then(|x| |
| 737 | == Some(id |
| 738 | { |
| 739 | return Some(dir |
| 740 | } |
| 741 | } |
| 742 | None |
| 743 | } |
| 744 | |
| 745 | /// Session dirs live either directly under the sessions root (older layout) |
| 746 | /// or nested one level deeper under a per-cwd directory. |
| 747 | fn grok_session_dirs(root: |
| 748 | let mut out |
| 749 | for e in fs |
| 750 | let dir = |
| 751 | if !dir.is_dir |
| 752 | continue; |
| 753 | } |
| 754 | let nested = |
| 755 | if nested.is_file() |
| 756 | out.push(dir |
| 757 | } else if let |
| 758 | for s in sub |
| 759 | if s.path() |
| 760 | out.push(s |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | out |
| 766 | } |
| 767 | |
| 768 | // --- Claude Code --- |
| 769 | |
| 770 | fn parse_claude(path: |
| 771 | let mut by_uuid |
| 772 | let mut parents |
| 773 | let mut title |
| 774 | let mut model |
| 775 | let mut leaf |
| 776 | for raw in read_lines |
| 777 | let Ok( |
| 778 | continue; |
| 779 | }; |
| 780 | let Some( |
| 781 | continue; |
| 782 | }; |
| 783 | if let Some |
| 784 | leaf = Some |
| 785 | } |
| 786 | // Every entry participates in the parentUuid chain, including |
| 787 | // system/subagent entries, so walking the tail crosses them. |
| 788 | if let Some |
| 789 | parents.insert( |
| 790 | uuid.to_string(), |
| 791 | v.get("parentUuid |
| 792 | .and_then(|x| |
| 793 | .unwrap_or("") |
| 794 | .to_string(), |
| 795 | ); |
| 796 | } |
| 797 | match t { |
| 798 | "user" | "assistant" |
| 799 | if let Some |
| 800 | by_uuid.insert(uuid |
| 801 | } |
| 802 | if model.is_empty() |
| 803 | && let Some |
| 804 | .get("message") |
| 805 | .and_then(|m| |
| 806 | .and_then(|x| |
| 807 | { |
| 808 | model = m. |
| 809 | } |
| 810 | } |
| 811 | "ai-title" | "custom-title" |
| 812 | if title.is_empty() |
| 813 | && let Some |
| 814 | .get("aiTitle") |
| 815 | .or_else(|| v |
| 816 | .or_else(|| v |
| 817 | .and_then(|x| |
| 818 | .map(str::trim |
| 819 | .filter(|s| |
| 820 | { |
| 821 | title = s. |
| 822 | } |
| 823 | } |
| 824 | "summary" => { |
| 825 | if title.is_empty() |
| 826 | && let Some |
| 827 | { |
| 828 | title = s. |
| 829 | } |
| 830 | } |
| 831 | "last-prompt" => { |
| 832 | if title.is_empty() |
| 833 | && let Some |
| 834 | { |
| 835 | title = trunc( |
| 836 | } |
| 837 | } |
| 838 | _ => {} |
| 839 | } |
| 840 | } |
| 841 | // Rebuild the linear conversation by walking parentUuid from the current |
| 842 | // leaf back to the root, then reversing. The chain may pass through |
| 843 | // system/subagent entries that are absent from by_uuid. |
| 844 | let leaf = |
| 845 | by_uuid |
| 846 | .iter() |
| 847 | .max_by_key(|(_ |
| 848 | .map(|(u |
| 849 | }); |
| 850 | let mut order |
| 851 | let mut tool_names |
| 852 | if let Some |
| 853 | for _ in 0 |
| 854 | if let Some |
| 855 | order.push(v |
| 856 | } |
| 857 | let next = |
| 858 | if next.is_empty() |
| 859 | break; |
| 860 | } |
| 861 | cur = next; |
| 862 | } |
| 863 | order.reverse(); |
| 864 | } |
| 865 | let msgs = |
| 866 | .iter() |
| 867 | .flat_map(|v| |
| 868 | .collect::<Vec |
| 869 | Ok((title, |
| 870 | } |
| 871 | |
| 872 | fn claude_message(v: |
| 873 | let role = |
| 874 | let content = |
| 875 | .get("message") |
| 876 | .and_then(|m| |
| 877 | .cloned() |
| 878 | .unwrap_or(Value::Null |
| 879 | match role { |
| 880 | "assistant" => { |
| 881 | let mut text |
| 882 | let mut calls |
| 883 | if let Some |
| 884 | text = s. |
| 885 | } else if let |
| 886 | for b in arr |
| 887 | match b.get( |
| 888 | "text" => { |
| 889 | if let Some |
| 890 | if !text.is_empty |
| 891 | text.push('\ |
| 892 | } |
| 893 | text.push_str(t |
| 894 | } |
| 895 | } |
| 896 | "tool_use" => { |
| 897 | let id = |
| 898 | .get("id") |
| 899 | .and_then(|x| |
| 900 | .unwrap_or("") |
| 901 | .to_string(); |
| 902 | let name = |
| 903 | .get("name") |
| 904 | .and_then(|x| |
| 905 | .unwrap_or("tool") |
| 906 | .to_string(); |
| 907 | tool_names.insert(id |
| 908 | calls.push(ToolCall |
| 909 | id, |
| 910 | name, |
| 911 | arguments: b.get |
| 912 | }); |
| 913 | } |
| 914 | _ => {} |
| 915 | } |
| 916 | } |
| 917 | } |
| 918 | if !text.is_empty |
| 919 | vec![ImportedMessage |
| 920 | role: "assistant". |
| 921 | content: text, |
| 922 | tool_call_id: None |
| 923 | name: None |
| 924 | tool_calls: calls, |
| 925 | images: Vec |
| 926 | }] |
| 927 | } else { |
| 928 | Vec::new() |
| 929 | } |
| 930 | } |
| 931 | "user" => { |
| 932 | let mut text |
| 933 | let mut images |
| 934 | let mut results |
| 935 | if let Some |
| 936 | text = s. |
| 937 | } else if let |
| 938 | for b in arr |
| 939 | match b.get( |
| 940 | "text" => { |
| 941 | if let Some |
| 942 | if !text.is_empty |
| 943 | text.push('\ |
| 944 | } |
| 945 | text.push_str(t |
| 946 | } |
| 947 | } |
| 948 | "image" => { |
| 949 | if let (Some |
| 950 | b.get("source |
| 951 | .and_then(|s| |
| 952 | .and_then(|x| |
| 953 | b.get("source |
| 954 | .and_then(|s| |
| 955 | .and_then(|x| |
| 956 | ) { |
| 957 | images.push(ImagePart |
| 958 | media_type: mt.to_string |
| 959 | data_base64: data.to_string |
| 960 | }); |
| 961 | } |
| 962 | } |
| 963 | "tool_result" => { |
| 964 | let tid = |
| 965 | .get("tool_use_id") |
| 966 | .and_then(|x| |
| 967 | .unwrap_or("") |
| 968 | .to_string(); |
| 969 | let name = |
| 970 | .get(&tid) |
| 971 | .cloned() |
| 972 | .unwrap_or_else(|| "tool |
| 973 | results.push(ImportedMessage |
| 974 | role: "tool". |
| 975 | content: flatten_text(& |
| 976 | tool_call_id: if tid |
| 977 | name: Some |
| 978 | tool_calls: Vec |
| 979 | images: Vec |
| 980 | }); |
| 981 | } |
| 982 | _ => {} |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | let mut out |
| 987 | if !text.is_empty |
| 988 | out.push(ImportedMessage |
| 989 | role: "user". |
| 990 | content: text, |
| 991 | tool_call_id: None |
| 992 | name: None |
| 993 | tool_calls: Vec::new |
| 994 | images, |
| 995 | }); |
| 996 | } |
| 997 | out.extend(results |
| 998 | out |
| 999 | } |
| 1000 | _ => Vec |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | // --- Codex CLI --- |
| 1005 | |
| 1006 | fn parse_codex(path: |
| 1007 | let mut msgs |
| 1008 | let mut title |
| 1009 | let mut model |
| 1010 | let mut call_names |
| 1011 | for raw in read_lines |
| 1012 | let Ok( |
| 1013 | continue; |
| 1014 | }; |
| 1015 | let Some( |
| 1016 | continue; |
| 1017 | }; |
| 1018 | match ty { |
| 1019 | "session_meta" => { |
| 1020 | let p = |
| 1021 | model = p |
| 1022 | .get("model") |
| 1023 | .or_else(|| p |
| 1024 | .and_then(|x| |
| 1025 | .unwrap_or("") |
| 1026 | .to_string(); |
| 1027 | } |
| 1028 | "response_item" => { |
| 1029 | let p = |
| 1030 | let Some( |
| 1031 | continue; |
| 1032 | }; |
| 1033 | match pt { |
| 1034 | "message" => { |
| 1035 | let role = |
| 1036 | let content = |
| 1037 | if content.trim() |
| 1038 | continue; |
| 1039 | } |
| 1040 | match role { |
| 1041 | "user" => { |
| 1042 | if title.is_empty() |
| 1043 | title = trunc( |
| 1044 | } |
| 1045 | msgs.push(ImportedMessage |
| 1046 | role: "user". |
| 1047 | content, |
| 1048 | tool_call_id: None |
| 1049 | name: None |
| 1050 | tool_calls: Vec::new |
| 1051 | images: Vec::new |
| 1052 | }); |
| 1053 | } |
| 1054 | "assistant" => msgs |
| 1055 | role: "assistant". |
| 1056 | content, |
| 1057 | tool_call_id: None |
| 1058 | name: None |
| 1059 | tool_calls: Vec::new |
| 1060 | images: Vec::new |
| 1061 | }), |
| 1062 | _ => {} |
| 1063 | } |
| 1064 | } |
| 1065 | "function_call" | "custom_tool_call" |
| 1066 | let id = |
| 1067 | .get("call_id") |
| 1068 | .or_else(|| p |
| 1069 | .and_then(|x| |
| 1070 | .unwrap_or("") |
| 1071 | .to_string(); |
| 1072 | let name = |
| 1073 | .get("name") |
| 1074 | .and_then(|x| |
| 1075 | .unwrap_or("tool") |
| 1076 | .to_string(); |
| 1077 | if !id.is_empty |
| 1078 | call_names.insert(id |
| 1079 | } |
| 1080 | let args = |
| 1081 | .get("arguments") |
| 1082 | .and_then(parse_arguments) |
| 1083 | .unwrap_or(Value::Null |
| 1084 | let call = |
| 1085 | id, |
| 1086 | name, |
| 1087 | arguments: args, |
| 1088 | }; |
| 1089 | push_assistant_call(&mut msgs |
| 1090 | } |
| 1091 | "function_call_output" | "custom_tool_call_output" |
| 1092 | let id = |
| 1093 | .get("call_id") |
| 1094 | .or_else(|| p |
| 1095 | .and_then(|x| |
| 1096 | .unwrap_or("") |
| 1097 | .to_string(); |
| 1098 | let output = |
| 1099 | .get("output") |
| 1100 | .or_else(|| p |
| 1101 | .map(flatten_text) |
| 1102 | .unwrap_or_default(); |
| 1103 | if id.is_empty() |
| 1104 | continue; |
| 1105 | } |
| 1106 | let name = |
| 1107 | .get(&id) |
| 1108 | .cloned() |
| 1109 | .unwrap_or_else(|| "tool |
| 1110 | msgs.push(ImportedMessage |
| 1111 | role: "tool". |
| 1112 | content: output, |
| 1113 | tool_call_id: Some |
| 1114 | name: Some |
| 1115 | tool_calls: Vec |
| 1116 | images: Vec |
| 1117 | }); |
| 1118 | } |
| 1119 | _ => {} |
| 1120 | } |
| 1121 | } |
| 1122 | _ => {} |
| 1123 | } |
| 1124 | } |
| 1125 | Ok((title, |
| 1126 | } |
| 1127 | |
| 1128 | fn parse_arguments(v: |
| 1129 | match v { |
| 1130 | Value::String( |
| 1131 | other => Some |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | fn push_assistant_call(msgs: |
| 1136 | if let Some |
| 1137 | && last.role == |
| 1138 | && last.content.is_empty |
| 1139 | && last.tool_calls.is_empty |
| 1140 | { |
| 1141 | last.tool_calls.push( |
| 1142 | } else { |
| 1143 | msgs.push(ImportedMessage |
| 1144 | role: "assistant". |
| 1145 | content: String::new |
| 1146 | tool_call_id: None |
| 1147 | name: None |
| 1148 | tool_calls: vec! |
| 1149 | images: Vec::new |
| 1150 | }); |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | // --- OpenCode --- |
| 1155 | |
| 1156 | fn parse_opencode(session_id: |
| 1157 | let path = |
| 1158 | if !path.is_file |
| 1159 | anyhow::bail!( |
| 1160 | } |
| 1161 | let conn = |
| 1162 | let (title, |
| 1163 | .query_row( |
| 1164 | "SELECT title, model FROM session WHERE id=?1", |
| 1165 | params![session_id], |
| 1166 | |r| Ok |
| 1167 | ) |
| 1168 | .optional() |
| 1169 | .with_context(|| format! |
| 1170 | .unwrap_or_default(); |
| 1171 | let model = |
| 1172 | .and_then(|j| |
| 1173 | .and_then(|v| |
| 1174 | .unwrap_or_default(); |
| 1175 | |
| 1176 | let mut msgs |
| 1177 | // Sub-session ids already claimed by a `task` call, so sibling task calls |
| 1178 | // resolve to distinct subsessions. |
| 1179 | let mut used_tasks |
| 1180 | let mut stmt |
| 1181 | conn.prepare("SELECT id, data FROM message WHERE session_id=?1 ORDER BY time_created, id |
| 1182 | let rows = |
| 1183 | Ok((r. |
| 1184 | })?; |
| 1185 | for row in rows |
| 1186 | let (mid, |
| 1187 | let v: Value |
| 1188 | let role = |
| 1189 | let mut parts |
| 1190 | conn.prepare("SELECT data FROM part WHERE message_id=?1 ORDER BY time_created, id |
| 1191 | let part_rows = |
| 1192 | let part_values: Vec |
| 1193 | .collect::<rusqlite:: |
| 1194 | .into_iter() |
| 1195 | .map(|s| |
| 1196 | .collect(); |
| 1197 | match role { |
| 1198 | "user" => { |
| 1199 | let mut text |
| 1200 | let mut images |
| 1201 | for p in & |
| 1202 | match p.get( |
| 1203 | "text" => push_text |
| 1204 | "image" => { |
| 1205 | if let (Some |
| 1206 | p.get("mediaType |
| 1207 | .or_else(|| p |
| 1208 | .and_then(|x| |
| 1209 | p.get("data |
| 1210 | .and_then(|d| |
| 1211 | .or_else(|| p |
| 1212 | .and_then(|x| |
| 1213 | ) { |
| 1214 | images.push(ImagePart |
| 1215 | media_type: mt.to_string |
| 1216 | data_base64: d.to_string |
| 1217 | }); |
| 1218 | } |
| 1219 | } |
| 1220 | _ => {} |
| 1221 | } |
| 1222 | } |
| 1223 | if !text.is_empty |
| 1224 | msgs.push(ImportedMessage |
| 1225 | role: "user". |
| 1226 | content: text, |
| 1227 | tool_call_id: None |
| 1228 | name: None |
| 1229 | tool_calls: Vec::new |
| 1230 | images, |
| 1231 | }); |
| 1232 | } |
| 1233 | } |
| 1234 | "assistant" => { |
| 1235 | let mut text |
| 1236 | let mut calls |
| 1237 | let mut results |
| 1238 | for p in & |
| 1239 | match p.get( |
| 1240 | "text" => push_text |
| 1241 | "tool" => { |
| 1242 | let tid = |
| 1243 | .get("callID") |
| 1244 | .and_then(|x| |
| 1245 | .unwrap_or("") |
| 1246 | .to_string(); |
| 1247 | let name = |
| 1248 | .get("tool") |
| 1249 | .and_then(|x| |
| 1250 | .unwrap_or("tool") |
| 1251 | .to_string(); |
| 1252 | let state = |
| 1253 | let mut input |
| 1254 | if name == " |
| 1255 | && let Some |
| 1256 | &conn, |
| 1257 | session_id, |
| 1258 | state.get("output |
| 1259 | &mut used_tasks, |
| 1260 | ) |
| 1261 | { |
| 1262 | let mut budget |
| 1263 | let children = |
| 1264 | &conn, |
| 1265 | &sub, |
| 1266 | 0, |
| 1267 | &mut budget, |
| 1268 | &mut used_tasks, |
| 1269 | ); |
| 1270 | if !children.is_empty |
| 1271 | && let Some |
| 1272 | { |
| 1273 | map.insert("children |
| 1274 | } |
| 1275 | } |
| 1276 | calls.push(ToolCall |
| 1277 | id: tid.clone |
| 1278 | name: name.clone |
| 1279 | arguments: input, |
| 1280 | }); |
| 1281 | let output = |
| 1282 | results.push(ImportedMessage |
| 1283 | role: "tool". |
| 1284 | content: output, |
| 1285 | tool_call_id: if tid |
| 1286 | name: Some |
| 1287 | tool_calls: Vec |
| 1288 | images: Vec |
| 1289 | }); |
| 1290 | } |
| 1291 | _ => {} |
| 1292 | } |
| 1293 | } |
| 1294 | if !text.is_empty |
| 1295 | msgs.push(ImportedMessage |
| 1296 | role: "assistant". |
| 1297 | content: text, |
| 1298 | tool_call_id: None |
| 1299 | name: None |
| 1300 | tool_calls: calls, |
| 1301 | images: Vec::new |
| 1302 | }); |
| 1303 | } |
| 1304 | msgs.extend(results |
| 1305 | } |
| 1306 | _ => {} |
| 1307 | } |
| 1308 | } |
| 1309 | Ok((title, |
| 1310 | } |
| 1311 | |
| 1312 | /// Cap on the number of tool-call nodes collected into one sub-agent tree, so a |
| 1313 | /// pathological session can't balloon the stored transcript. |
| 1314 | const SUBAGENT_TREE_BUDGET: usize |
| 1315 | |
| 1316 | /// Resolve the subsession a `task` call spawned. OpenCode's task output carries |
| 1317 | /// `task_id: <id>`; if that is missing, fall back to the parent session's child |
| 1318 | /// sessions in creation order, skipping ids already claimed by other task calls. |
| 1319 | fn opencode_task_session( |
| 1320 | conn: &Connection |
| 1321 | parent_id: &str |
| 1322 | output: Option |
| 1323 | used: &mut |
| 1324 | ) -> Option<String |
| 1325 | if let Some |
| 1326 | && let Some |
| 1327 | && !id.is_empty |
| 1328 | { |
| 1329 | used.insert(id |
| 1330 | return Some(id |
| 1331 | } |
| 1332 | let mut stmt |
| 1333 | .prepare("SELECT id FROM session WHERE parent_id=?1 ORDER BY time_created") |
| 1334 | .ok()?; |
| 1335 | let rows = |
| 1336 | .query_map(params! |
| 1337 | .ok()?; |
| 1338 | for row in rows |
| 1339 | if !used.contains |
| 1340 | used.insert(row |
| 1341 | return Some(row |
| 1342 | } |
| 1343 | } |
| 1344 | None |
| 1345 | } |
| 1346 | |
| 1347 | /// The sub-session id a task spawned, from its output string. OpenCode writes |
| 1348 | /// `<task id="ses_..." state="...">`; older versions may use `task_id: ses_...`. |
| 1349 | fn opencode_task_id(o: |
| 1350 | let o = |
| 1351 | if let Some |
| 1352 | return rest.split_whitespace() |
| 1353 | } |
| 1354 | const MARK: & |
| 1355 | if let Some |
| 1356 | let rest = |
| 1357 | let id = |
| 1358 | if !id.is_empty |
| 1359 | return Some(id |
| 1360 | } |
| 1361 | } |
| 1362 | None |
| 1363 | } |
| 1364 | |
| 1365 | /// The tool calls a sub-agent performed, in order, as `{"name", "arguments", |
| 1366 | /// "children"}` nodes. Nested `task` calls recurse into their own subsessions; |
| 1367 | /// `budget` bounds the total number of nodes across the whole tree. |
| 1368 | fn opencode_subagent_children( |
| 1369 | conn: &Connection |
| 1370 | subsession_id: &str |
| 1371 | depth: usize |
| 1372 | budget: &mut |
| 1373 | used: &mut |
| 1374 | ) -> Vec<Value |
| 1375 | let mut out |
| 1376 | if depth > 6 |
| 1377 | return out; |
| 1378 | } |
| 1379 | let Ok( |
| 1380 | conn.prepare("SELECT id, data FROM message WHERE session_id=?1 ORDER BY time_created, id |
| 1381 | else { |
| 1382 | return out; |
| 1383 | }; |
| 1384 | let Ok( |
| 1385 | Ok((r. |
| 1386 | }) else { |
| 1387 | return out; |
| 1388 | }; |
| 1389 | for row in rows |
| 1390 | let (mid, |
| 1391 | if *budget == |
| 1392 | break; |
| 1393 | } |
| 1394 | let Ok( |
| 1395 | continue; |
| 1396 | }; |
| 1397 | if v.get( |
| 1398 | continue; |
| 1399 | } |
| 1400 | let Ok( |
| 1401 | conn.prepare("SELECT data FROM part WHERE message_id=?1 ORDER BY time_created, id |
| 1402 | else { |
| 1403 | continue; |
| 1404 | }; |
| 1405 | let Ok( |
| 1406 | continue; |
| 1407 | }; |
| 1408 | for pdata in prows |
| 1409 | if *budget == |
| 1410 | break; |
| 1411 | } |
| 1412 | let Ok( |
| 1413 | continue; |
| 1414 | }; |
| 1415 | if p.get( |
| 1416 | continue; |
| 1417 | } |
| 1418 | let name = |
| 1419 | .get("tool") |
| 1420 | .and_then(|x| |
| 1421 | .unwrap_or("tool") |
| 1422 | .to_string(); |
| 1423 | let state = |
| 1424 | let args = |
| 1425 | let mut node |
| 1426 | *budget -= 1; |
| 1427 | if name == " |
| 1428 | && let Some |
| 1429 | opencode_task_session(conn, subsession_id |
| 1430 | { |
| 1431 | let kids = |
| 1432 | if let Some |
| 1433 | map.insert("children |
| 1434 | } |
| 1435 | } |
| 1436 | out.push(node |
| 1437 | } |
| 1438 | } |
| 1439 | out |
| 1440 | } |
| 1441 | |
| 1442 | fn push_text(text: |
| 1443 | if let Some |
| 1444 | if !text.is_empty |
| 1445 | text.push('\ |
| 1446 | } |
| 1447 | text.push_str(t |
| 1448 | } |
| 1449 | } |
| 1450 | |
| 1451 | // --- Pi (PiCodingAgent) --- |
| 1452 | |
| 1453 | fn parse_pi(path: |
| 1454 | let mut all |
| 1455 | let mut parents |
| 1456 | let mut model |
| 1457 | let mut title |
| 1458 | for raw in read_lines |
| 1459 | let Ok( |
| 1460 | continue; |
| 1461 | }; |
| 1462 | let Some( |
| 1463 | continue; |
| 1464 | }; |
| 1465 | match t { |
| 1466 | "model_change" => { |
| 1467 | if model.is_empty() |
| 1468 | model = v |
| 1469 | .get("modelId") |
| 1470 | .and_then(|x| |
| 1471 | .unwrap_or("") |
| 1472 | .to_string(); |
| 1473 | } |
| 1474 | } |
| 1475 | "session_info" => { |
| 1476 | if let Some |
| 1477 | .get("name") |
| 1478 | .and_then(|x| |
| 1479 | .map(str::trim |
| 1480 | .filter(|s| |
| 1481 | { |
| 1482 | title = n. |
| 1483 | } |
| 1484 | } |
| 1485 | _ => {} |
| 1486 | } |
| 1487 | // Every entry participates in the id/parentId chain, including |
| 1488 | // model_change/session_info/compaction markers. |
| 1489 | let id = |
| 1490 | .get("id") |
| 1491 | .and_then(|x| |
| 1492 | .unwrap_or("") |
| 1493 | .to_string(); |
| 1494 | if !id.is_empty |
| 1495 | let parent = |
| 1496 | .get("parentId") |
| 1497 | .and_then(|x| |
| 1498 | .unwrap_or("") |
| 1499 | .to_string(); |
| 1500 | parents.insert(id |
| 1501 | all.push(v |
| 1502 | } |
| 1503 | } |
| 1504 | if title.is_empty() |
| 1505 | for raw in read_lines |
| 1506 | let Ok( |
| 1507 | continue; |
| 1508 | }; |
| 1509 | if v.get( |
| 1510 | && v.pointer( |
| 1511 | { |
| 1512 | let t = |
| 1513 | .get("message") |
| 1514 | .and_then(|m| |
| 1515 | .map(flatten_text) |
| 1516 | .unwrap_or_default(); |
| 1517 | if !t.trim |
| 1518 | title = trunc( |
| 1519 | break; |
| 1520 | } |
| 1521 | } |
| 1522 | } |
| 1523 | } |
| 1524 | // Follow the current tail: the last entry nobody points at, walking |
| 1525 | // parentId back to the root. |
| 1526 | let child_ids: std |
| 1527 | let leaf = |
| 1528 | .iter() |
| 1529 | .rev() |
| 1530 | .find(|v| |
| 1531 | let id = |
| 1532 | !child_ids.contains(id |
| 1533 | }) |
| 1534 | .map(|v| |
| 1535 | v.get("id |
| 1536 | .and_then(|x| |
| 1537 | .unwrap_or("") |
| 1538 | .to_string() |
| 1539 | }); |
| 1540 | let mut order |
| 1541 | if let Some |
| 1542 | for _ in 0 |
| 1543 | let Some( |
| 1544 | .iter() |
| 1545 | .find(|v| |
| 1546 | else { |
| 1547 | break; |
| 1548 | }; |
| 1549 | order.push(v |
| 1550 | let next = |
| 1551 | if next.is_empty() |
| 1552 | break; |
| 1553 | } |
| 1554 | cur = next; |
| 1555 | } |
| 1556 | order.reverse(); |
| 1557 | } |
| 1558 | let msgs = |
| 1559 | Ok((title, |
| 1560 | } |
| 1561 | |
| 1562 | fn pi_message(v: |
| 1563 | match v.get( |
| 1564 | Some("message") |
| 1565 | let msg = |
| 1566 | let role = |
| 1567 | let content = |
| 1568 | match role { |
| 1569 | "user" => { |
| 1570 | let mut text |
| 1571 | let mut images |
| 1572 | if let Some |
| 1573 | text = s. |
| 1574 | } else if let |
| 1575 | for b in arr |
| 1576 | match b.get( |
| 1577 | "text" => { |
| 1578 | if let Some |
| 1579 | if !text.is_empty |
| 1580 | text.push('\ |
| 1581 | } |
| 1582 | text.push_str(t |
| 1583 | } |
| 1584 | } |
| 1585 | "image" => { |
| 1586 | if let (Some |
| 1587 | b.get("mimeType |
| 1588 | b.get("data |
| 1589 | ) { |
| 1590 | images.push(ImagePart |
| 1591 | media_type: mt.to_string |
| 1592 | data_base64: d.to_string |
| 1593 | }); |
| 1594 | } |
| 1595 | } |
| 1596 | _ => {} |
| 1597 | } |
| 1598 | } |
| 1599 | } |
| 1600 | if !text.is_empty |
| 1601 | vec![ImportedMessage |
| 1602 | role: "user". |
| 1603 | content: text, |
| 1604 | tool_call_id: None |
| 1605 | name: None |
| 1606 | tool_calls: Vec |
| 1607 | images, |
| 1608 | }] |
| 1609 | } else { |
| 1610 | Vec::new() |
| 1611 | } |
| 1612 | } |
| 1613 | "assistant" => { |
| 1614 | let mut text |
| 1615 | let mut calls |
| 1616 | if let Some |
| 1617 | text = s. |
| 1618 | } else if let |
| 1619 | for b in arr |
| 1620 | match b.get( |
| 1621 | "text" => { |
| 1622 | if let Some |
| 1623 | if !text.is_empty |
| 1624 | text.push('\ |
| 1625 | } |
| 1626 | text.push_str(t |
| 1627 | } |
| 1628 | } |
| 1629 | "toolCall" => { |
| 1630 | let id = |
| 1631 | .get("id") |
| 1632 | .and_then(|x| |
| 1633 | .unwrap_or("") |
| 1634 | .to_string(); |
| 1635 | let name = |
| 1636 | .get("name") |
| 1637 | .and_then(|x| |
| 1638 | .unwrap_or("tool") |
| 1639 | .to_string(); |
| 1640 | calls.push(ToolCall |
| 1641 | id, |
| 1642 | name, |
| 1643 | arguments: b |
| 1644 | .get("arguments") |
| 1645 | .cloned() |
| 1646 | .unwrap_or(Value::Null |
| 1647 | }); |
| 1648 | } |
| 1649 | _ => {} |
| 1650 | } |
| 1651 | } |
| 1652 | } |
| 1653 | if !text.is_empty |
| 1654 | vec![ImportedMessage |
| 1655 | role: "assistant". |
| 1656 | content: text, |
| 1657 | tool_call_id: None |
| 1658 | name: None |
| 1659 | tool_calls: calls, |
| 1660 | images: Vec |
| 1661 | }] |
| 1662 | } else { |
| 1663 | Vec::new() |
| 1664 | } |
| 1665 | } |
| 1666 | _ => Vec |
| 1667 | } |
| 1668 | } |
| 1669 | Some("toolResult") |
| 1670 | let tid = |
| 1671 | .get("toolCallId") |
| 1672 | .and_then(|x| |
| 1673 | .unwrap_or("") |
| 1674 | .to_string(); |
| 1675 | let name = |
| 1676 | .get("toolName") |
| 1677 | .and_then(|x| |
| 1678 | .unwrap_or("tool") |
| 1679 | .to_string(); |
| 1680 | vec![ImportedMessage |
| 1681 | role: "tool". |
| 1682 | content: v.get |
| 1683 | tool_call_id: if tid |
| 1684 | name: Some |
| 1685 | tool_calls: Vec |
| 1686 | images: Vec |
| 1687 | }] |
| 1688 | } |
| 1689 | _ => Vec |
| 1690 | } |
| 1691 | } |
| 1692 | |
| 1693 | // --- GrokBuild --- |
| 1694 | |
| 1695 | fn parse_grok(dir: |
| 1696 | let mut model |
| 1697 | let mut title |
| 1698 | let summary = |
| 1699 | if summary.is_file() |
| 1700 | && let Ok |
| 1701 | && let Ok |
| 1702 | { |
| 1703 | title = v |
| 1704 | .get("session_summary") |
| 1705 | .or_else(|| v |
| 1706 | .and_then(|x| |
| 1707 | .unwrap_or("") |
| 1708 | .to_string(); |
| 1709 | model = v |
| 1710 | .get("current_model_id") |
| 1711 | .and_then(|x| |
| 1712 | .unwrap_or("") |
| 1713 | .to_string(); |
| 1714 | } |
| 1715 | let chat = |
| 1716 | let mut msgs |
| 1717 | for raw in read_lines |
| 1718 | let Ok( |
| 1719 | continue; |
| 1720 | }; |
| 1721 | match v.get( |
| 1722 | Some("system") |
| 1723 | Some("user") |
| 1724 | let text = |
| 1725 | if !text.trim |
| 1726 | msgs.push(ImportedMessage |
| 1727 | role: "user". |
| 1728 | content: text, |
| 1729 | tool_call_id: None |
| 1730 | name: None |
| 1731 | tool_calls: Vec::new |
| 1732 | images: Vec::new |
| 1733 | }); |
| 1734 | } |
| 1735 | } |
| 1736 | Some("assistant") |
| 1737 | let text = |
| 1738 | if !text.trim |
| 1739 | msgs.push(ImportedMessage |
| 1740 | role: "assistant". |
| 1741 | content: text, |
| 1742 | tool_call_id: None |
| 1743 | name: None |
| 1744 | tool_calls: Vec::new |
| 1745 | images: Vec::new |
| 1746 | }); |
| 1747 | } |
| 1748 | } |
| 1749 | Some("tool_result") |
| 1750 | let tid = |
| 1751 | .get("tool_call_id") |
| 1752 | .and_then(|x| |
| 1753 | .unwrap_or("") |
| 1754 | .to_string(); |
| 1755 | let text = |
| 1756 | if !text.trim |
| 1757 | msgs.push(ImportedMessage |
| 1758 | role: "tool". |
| 1759 | content: text, |
| 1760 | tool_call_id: if tid |
| 1761 | name: Some |
| 1762 | tool_calls: Vec |
| 1763 | images: Vec |
| 1764 | }); |
| 1765 | } |
| 1766 | } |
| 1767 | Some("tool_use") |
| 1768 | let id = |
| 1769 | .get("id") |
| 1770 | .and_then(|x| |
| 1771 | .unwrap_or("") |
| 1772 | .to_string(); |
| 1773 | let name = |
| 1774 | .get("name") |
| 1775 | .and_then(|x| |
| 1776 | .unwrap_or("tool") |
| 1777 | .to_string(); |
| 1778 | push_assistant_call( |
| 1779 | &mut msgs, |
| 1780 | ToolCall { |
| 1781 | id, |
| 1782 | name, |
| 1783 | arguments: v.get |
| 1784 | }, |
| 1785 | ); |
| 1786 | } |
| 1787 | _ => {} |
| 1788 | } |
| 1789 | } |
| 1790 | Ok((title, |
| 1791 | } |
| 1792 | |
| 1793 | // --------------------------------------------------------------------------- |
| 1794 | // Import: translate + write into the coding-agent database. |
| 1795 | // --------------------------------------------------------------------------- |
| 1796 | |
| 1797 | #[allow(clippy:: |
| 1798 | pub fn import_session( |
| 1799 | conn: &Connection |
| 1800 | tool: &Tool |
| 1801 | id: &str |
| 1802 | provider: &str |
| 1803 | default_model: &str |
| 1804 | override_model: Option |
| 1805 | ) -> Result<Imported |
| 1806 | let transcript = |
| 1807 | .with_context(|| format! |
| 1808 | let (title, |
| 1809 | let model = |
| 1810 | if detected_model.is_empty() |
| 1811 | default_model.to_string() |
| 1812 | } else { |
| 1813 | detected_model |
| 1814 | } |
| 1815 | }); |
| 1816 | |
| 1817 | let source = |
| 1818 | let (sid, |
| 1819 | Some(sid) |
| 1820 | let uuid = |
| 1821 | .with_context(|| format! |
| 1822 | .uuid; |
| 1823 | db::delete_all_messages(conn |
| 1824 | (sid, uuid) |
| 1825 | } |
| 1826 | None => db |
| 1827 | }; |
| 1828 | for m in & |
| 1829 | let tool_calls_json = |
| 1830 | None |
| 1831 | } else { |
| 1832 | serde_json::to_string(&m |
| 1833 | }; |
| 1834 | let images_json = |
| 1835 | None |
| 1836 | } else { |
| 1837 | serde_json::to_string(&m |
| 1838 | }; |
| 1839 | db::add_message( |
| 1840 | conn, |
| 1841 | sid, |
| 1842 | &m.role, |
| 1843 | Some(&m |
| 1844 | m.tool_call_id.as_deref(), |
| 1845 | m.name.as_deref(), |
| 1846 | tool_calls_json.as_deref(), |
| 1847 | images_json.as_deref(), |
| 1848 | None, // |
| 1849 | )?; |
| 1850 | } |
| 1851 | let name = |
| 1852 | "(untitled)".to_string() |
| 1853 | } else { |
| 1854 | trunc(&title, |
| 1855 | }; |
| 1856 | db::update_session_meta(conn |
| 1857 | db::set_session_source(conn |
| 1858 | Ok(Imported |
| 1859 | id: sid, |
| 1860 | uuid, |
| 1861 | name, |
| 1862 | model, |
| 1863 | message_count: msgs.len |
| 1864 | }) |
| 1865 | } |
| 1866 | |
| 1867 | #[cfg(test)] |
| 1868 | mod tests { |
| 1869 | use super::*; |
| 1870 | use std::sync |
| 1871 | use std::sync |
| 1872 | |
| 1873 | static NEXT: AtomicUsize |
| 1874 | |
| 1875 | fn temp_path(name: |
| 1876 | static DIR: OnceLock |
| 1877 | let dir = |
| 1878 | let d = |
| 1879 | fs::create_dir_all(&d |
| 1880 | d |
| 1881 | }); |
| 1882 | let n = |
| 1883 | dir.join(format! |
| 1884 | } |
| 1885 | |
| 1886 | fn write_temp(name: |
| 1887 | let p = |
| 1888 | fs::write(&p |
| 1889 | p |
| 1890 | } |
| 1891 | |
| 1892 | fn roles(msgs: |
| 1893 | msgs.iter().map |
| 1894 | } |
| 1895 | |
| 1896 | #[test] |
| 1897 | fn claude_transcript_rebuilds_linear_chain() { |
| 1898 | let p = |
| 1899 | "claude", |
| 1900 | r#"{"type":"user","uuid":"u1","parentUuid":null,"sessionId":"s1","timestamp":"2026-01-01T00:00:00Z","message":{"role":"user","content":"fix the bug"}} |
| 1901 | {"type":"assistant","uuid":"a1","parentUuid":"u1","sessionId":"s1","timestamp":"2026-01-01T00:00:01Z","message":{"role":"assistant","model":"claude-opus-4","content":[{"type":"text","text":"looking at it"},{"type":"tool_use","id":"t1","name":"Read","input":{"file_path":"/x"}}]}} |
| 1902 | {"type":"user","uuid":"u2","parentUuid":"a1","sessionId":"s1","timestamp":"2026-01-01T00:00:02Z","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"t1","content":"file contents"}]}} |
| 1903 | {"type":"assistant","uuid":"a2","parentUuid":"u2","sessionId":"s1","timestamp":"2026-01-01T00:00:03Z","message":{"role":"assistant","content":"done"}} |
| 1904 | {"type":"ai-title","uuid":"t1","parentUuid":"a2","sessionId":"s1","timestamp":"2026-01-01T00:00:04Z","aiTitle":"Fix the bug"} |
| 1905 | {"type":"summary","summary":"Fix the bug","leafUuid":"a2"} |
| 1906 | "#, |
| 1907 | ); |
| 1908 | let (title, |
| 1909 | assert_eq!(title, |
| 1910 | assert_eq!(model, |
| 1911 | assert_eq!(roles( |
| 1912 | assert_eq!(msgs[0 |
| 1913 | assert_eq!(msgs[1 |
| 1914 | assert_eq!(msgs[1 |
| 1915 | assert_eq!(msgs[1 |
| 1916 | assert_eq!(msgs[2 |
| 1917 | assert_eq!(msgs[2 |
| 1918 | assert_eq!(msgs[2 |
| 1919 | assert_eq!(msgs[3 |
| 1920 | } |
| 1921 | |
| 1922 | #[test] |
| 1923 | fn codex_transcript_maps_calls_and_results() { |
| 1924 | let p = |
| 1925 | "codex", |
| 1926 | r#"{"timestamp":"2026-01-01T00:00:00Z","type":"session_meta","payload":{"id":"s1","cwd":"/w","model_provider":"openai"}} |
| 1927 | {"timestamp":"2026-01-01T00:00:01Z","type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"input_text","text":"hello"}]}} |
| 1928 | {"timestamp":"2026-01-01T00:00:02Z","type":"response_item","payload":{"type":"message","role":"assistant","content":[{"type":"output_text","text":"hi"}]}} |
| 1929 | {"timestamp":"2026-01-01T00:00:03Z","type":"response_item","payload":{"type":"function_call","name":"exec_command","arguments":"{\"cmd\":\"ls\"}","call_id":"c1"}} |
| 1930 | {"timestamp":"2026-01-01T00:00:04Z","type":"response_item","payload":{"type":"function_call_output","call_id":"c1","output":"a.txt\n"}} |
| 1931 | "#, |
| 1932 | ); |
| 1933 | let (title, |
| 1934 | assert_eq!(title, |
| 1935 | assert_eq!(roles( |
| 1936 | assert_eq!(msgs[2 |
| 1937 | assert_eq!(msgs[2 |
| 1938 | assert_eq!(msgs[2 |
| 1939 | assert_eq!(msgs[3 |
| 1940 | assert_eq!(msgs[3 |
| 1941 | assert_eq!(msgs[3 |
| 1942 | } |
| 1943 | |
| 1944 | #[test] |
| 1945 | fn pi_transcript_follows_tail() { |
| 1946 | let p = |
| 1947 | "pi", |
| 1948 | r#"{"type":"session","version":3,"id":"ps1","timestamp":"2026-01-01T00:00:00Z","cwd":"/w"} |
| 1949 | {"type":"message","id":"m1","parentId":null,"timestamp":"2026-01-01T00:00:01Z","message":{"role":"user","content":[{"type":"text","text":"hi"}]}} |
| 1950 | {"type":"message","id":"m2","parentId":"m1","timestamp":"2026-01-01T00:00:02Z","message":{"role":"assistant","content":[{"type":"text","text":"yo"},{"type":"toolCall","id":"c1","name":"Bash","arguments":{"command":"ls"}}]}} |
| 1951 | {"type":"toolResult","id":"m3","parentId":"m2","timestamp":"2026-01-01T00:00:03Z","toolCallId":"c1","toolName":"Bash","content":"out"} |
| 1952 | {"type":"model_change","id":"m4","parentId":"m3","timestamp":"2026-01-01T00:00:04Z","provider":"openai","modelId":"gpt-5"} |
| 1953 | {"type":"message","id":"m5","parentId":"m4","timestamp":"2026-01-01T00:00:05Z","message":{"role":"assistant","content":"done"}} |
| 1954 | "#, |
| 1955 | ); |
| 1956 | let (title, |
| 1957 | assert_eq!(title, |
| 1958 | assert_eq!(model, |
| 1959 | assert_eq!(roles( |
| 1960 | assert_eq!(msgs[1 |
| 1961 | assert_eq!(msgs[2 |
| 1962 | assert_eq!(msgs[2 |
| 1963 | assert_eq!(msgs[3 |
| 1964 | } |
| 1965 | |
| 1966 | #[test] |
| 1967 | fn grok_transcript_maps_text_and_tool_results() { |
| 1968 | let dir = |
| 1969 | fs::create_dir_all(&dir |
| 1970 | fs::write( |
| 1971 | dir.join("summary.json |
| 1972 | r#"{"info":{"id":"g1","cwd":"/w"},"session_summary":"Fix the tab","current_model_id":"grok-4.5"}"#, |
| 1973 | ) |
| 1974 | .unwrap(); |
| 1975 | fs::write( |
| 1976 | dir.join("chat_history.jsonl |
| 1977 | r#"{"type":"system","content":"you are grok"} |
| 1978 | {"type":"user","content":[{"type":"text","text":"fix it"}]} |
| 1979 | {"type":"reasoning","summary":[{"type":"summary_text","text":"thinking"}]} |
| 1980 | {"type":"assistant","content":"sure"} |
| 1981 | {"type":"tool_result","tool_call_id":"tc1","content":"ok"} |
| 1982 | "#, |
| 1983 | ) |
| 1984 | .unwrap(); |
| 1985 | let (title, |
| 1986 | assert_eq!(title, |
| 1987 | assert_eq!(model, |
| 1988 | assert_eq!(roles( |
| 1989 | assert_eq!(msgs[2 |
| 1990 | assert_eq!(msgs[2 |
| 1991 | } |
| 1992 | |
| 1993 | #[test] |
| 1994 | fn opencode_transcript_reads_parts() { |
| 1995 | let db = |
| 1996 | { |
| 1997 | let conn = |
| 1998 | conn.execute_batch( |
| 1999 | "CREATE TABLE session (id TEXT PRIMARY KEY, title TEXT, model TEXT); |
| 2000 | CREATE TABLE message (id TEXT PRIMARY KEY, session_id TEXT, time_created INTEGER, data TEXT); |
| 2001 | CREATE TABLE part (id TEXT PRIMARY KEY, message_id TEXT, session_id TEXT, time_created INTEGER, data TEXT);", |
| 2002 | ) |
| 2003 | .unwrap(); |
| 2004 | conn.execute( |
| 2005 | "INSERT INTO session (id, title, model) VALUES ('ses1', 'Build the thing', '{\"id\": |
| 2006 | [], |
| 2007 | ) |
| 2008 | .unwrap(); |
| 2009 | conn.execute( |
| 2010 | "INSERT INTO message (id, session_id, time_created, data) VALUES |
| 2011 | ('msg1','ses1',1,'{\"role\":\"user |
| 2012 | ('msg2','ses1',2,'{\"role\":\"assistant |
| 2013 | [], |
| 2014 | ) |
| 2015 | .unwrap(); |
| 2016 | conn.execute( |
| 2017 | "INSERT INTO part (id, message_id, session_id, time_created, data) VALUES |
| 2018 | ('p1','msg1','ses1',1,'{\"type\":\"text |
| 2019 | ('p2','msg2','ses1',2,'{\"type\":\"text |
| 2020 | ('p3','msg2','ses1',3,'{\"type\":\"tool |
| 2021 | [], |
| 2022 | ) |
| 2023 | .unwrap(); |
| 2024 | } |
| 2025 | // SAFETY: tests run single-threaded for this module's env reads. |
| 2026 | unsafe { std::env |
| 2027 | let (title, |
| 2028 | assert_eq!(title, |
| 2029 | assert_eq!(model, |
| 2030 | assert_eq!(roles( |
| 2031 | assert_eq!(msgs[0 |
| 2032 | assert_eq!(msgs[1 |
| 2033 | assert_eq!(msgs[1 |
| 2034 | assert_eq!(msgs[2 |
| 2035 | assert_eq!(msgs[2 |
| 2036 | let _ = |
| 2037 | } |
| 2038 | } |