Skip to content

feat: import and resume Claude Code CLI sessions (closes #10)#28

Merged
op7418 merged 1 commit intoop7418:mainfrom
liuxiaopai-ai:feat/import-claude-sessions
Feb 9, 2026
Merged

feat: import and resume Claude Code CLI sessions (closes #10)#28
op7418 merged 1 commit intoop7418:mainfrom
liuxiaopai-ai:feat/import-claude-sessions

Conversation

@liuxiaopai-ai
Copy link
Contributor

Summary

Implements the ability to browse and import conversation history from Claude Code CLI into CodePilot, with full resume support. This closes #10.

Problem

Users who have been using Claude Code CLI have accumulated conversation history stored as JSONL files in ~/.claude/projects/. There was no way to view or continue these conversations in CodePilot.

Solution

JSONL Parser (src/lib/claude-session-parser.ts)

  • Parses Claude Code session files from ~/.claude/projects/<encoded-project-path>/<session-uuid>.jsonl
  • Handles all JSONL entry types: queue-operation, user, assistant
  • Extracts structured content blocks (text, tool_use, tool_result)
  • Uses cwd field from JSONL entries as authoritative working directory (encoded folder names are lossy for paths containing hyphens)
  • File size guard (50MB max) to prevent memory issues with very large sessions
  • Gracefully handles malformed JSONL lines

REST API

  • GET /api/claude-sessions — Lists all available CLI sessions with metadata (project name, git branch, message counts, timestamps, file size)
  • POST /api/claude-sessions/import — Imports a session into CodePilot database
    • Binds sdk_session_id so imported conversations can be resumed via the Claude Agent SDK
    • Duplicate import protection: returns 409 with existingSessionId if already imported
    • Stores messages using CodePilot's existing convention (plain text for text-only, structured JSON for tool calls)

UI (src/components/layout/ImportSessionDialog.tsx)

  • "Import CLI Session" button added to the sidebar (ChatListPanel)
  • Dialog shows all CLI sessions with:
    • Project name and git branch badge
    • First message preview
    • Message count, relative time, file size
    • Search/filter by project, message content, or branch
  • One-click import with loading state
  • On duplicate import (409), navigates to the existing session instead of showing an error

How It Works

  1. User clicks "Import CLI Session" in the sidebar
  2. Dialog scans ~/.claude/projects/ and displays available sessions
  3. User clicks "Import" on a session
  4. Backend parses the JSONL file, creates a CodePilot session, imports all messages, and binds the SDK session ID
  5. User is navigated to the imported session and can continue the conversation

Testing

  • 17 new unit tests covering:
    • Path decoding (decodeProjectPath)
    • Session discovery and listing (listClaudeSessions)
    • Full session parsing with text, tool_use, and tool_result blocks
    • Multi-turn conversations
    • Empty/malformed content handling
    • Preview truncation
    • Session info extraction
  • All 38 tests pass (17 new + 21 existing)
  • TypeScript compiles with zero errors

Files Changed

File Change
src/lib/claude-session-parser.ts New — JSONL parser core
src/app/api/claude-sessions/route.ts New — List API
src/app/api/claude-sessions/import/route.ts New — Import API with dedup
src/components/layout/ImportSessionDialog.tsx New — Browse & import dialog
src/components/layout/ChatListPanel.tsx Modified — Sidebar button
src/__tests__/unit/claude-session-parser.test.ts New — 17 unit tests

Known Limitations

  • listClaudeSessions reads all JSONL files on each call; users with hundreds of CLI sessions may see a brief delay when opening the dialog (rare in practice, and only triggered on dialog open)
  • Encoded project folder names containing hyphens are ambiguous (e.g., -root-my-project could decode to /root/my-project or /root/my/project), but this is mitigated by using the cwd field from JSONL entries as the authoritative path

Add ability to browse and import conversation history from Claude Code CLI
into CodePilot, with full resume support via SDK session ID.

Features:
- JSONL parser for Claude Code session files (~/.claude/projects/)
- REST API: GET /api/claude-sessions (list) + POST /api/claude-sessions/import
- Import dialog UI accessible from sidebar 'Import CLI Session' button
- Imported sessions bind sdk_session_id for seamless resume in CodePilot
- Duplicate import protection (409 with redirect to existing session)
- File size guard (50MB max) to prevent memory issues

Files changed:
- src/lib/claude-session-parser.ts — JSONL parser core
- src/app/api/claude-sessions/route.ts — list API
- src/app/api/claude-sessions/import/route.ts — import API with dedup
- src/components/layout/ImportSessionDialog.tsx — browse & import UI
- src/components/layout/ChatListPanel.tsx — sidebar integration
- src/__tests__/unit/claude-session-parser.test.ts — 17 unit tests
@op7418 op7418 merged commit c2c53e9 into op7418:main Feb 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can I pick up where I left off from a previous Claude conversation?

2 participants