Skip to content
This repository was archived by the owner on Apr 26, 2026. It is now read-only.

Add scripts/ importer for Claude Code sessions into OpenCode DB#14

Open
Aptul9 wants to merge 1 commit into
unixfox:masterfrom
Aptul9:feat/import-script
Open

Add scripts/ importer for Claude Code sessions into OpenCode DB#14
Aptul9 wants to merge 1 commit into
unixfox:masterfrom
Aptul9:feat/import-script

Conversation

@Aptul9

@Aptul9 Aptul9 commented Apr 17, 2026

Copy link
Copy Markdown

Summary

Python script that reads ~/.claude/projects/*/*.jsonl (Claude Code CLI session history) and inserts the conversations into the OpenCode SQLite database, so previously-used Claude Code sessions become visible in the OpenCode UI.

Lives under scripts/ and does not touch any plugin source. Opt-in tool.

Features

  • Interactive REPL: keyword-based search across title / first prompt / cwd, pagination (last N), batch selection (1 3 5, 1-5, all)
  • Honors custom-title events so chats renamed from the Claude Code UI keep their title after import
  • Reuses the tool-name mapping conventions from src/tool-mapping.ts (Edit/Write/Bash/... → lowercase; mcp__server__toolserver_tool; internal CLI tools are skipped)
  • Matches the jsonl cwd against OpenCode project.worktree; falls back to the global project when no match exists (session.directory still reflects the original cwd so the chat appears under the correct folder in the UI)
  • Per-turn max() aggregation for token usage, mirroring the runtime lastIterationUsage() behavior to avoid inflating cumulative counts

Preserved

User prompts, assistant text and reasoning (thinking), tool calls with input/output (matched by tool_use_id), per-turn token totals, chronological order.

Not preserved

Images and binary attachments, sub-agent chains (isSidechain: true), thinking signatures, real git snapshot hashes (placeholder 000...000).

Requirements

  • Python 3 stdlib only (sqlite3), no external deps
  • OpenCode closed during the import (SQLite WAL conflicts)

Test plan

  • Import of a real .jsonl session: session, message, and part rows correctly created; reasoning, text, and tool calls visible in the UI
  • Token counters no longer inflated (max-per-field across assistant events, not sum)
  • custom-title chats render with the user-chosen title in OpenCode
  • cwd → project fallback to global works for folders that have no dedicated OpenCode project

Docs: see scripts/README.md.

Split from a previous combined PR per maintainer request. Purely additive (new files under scripts/), does not modify any existing source.

Python script that reads ~/.claude/projects/*/\*.jsonl and inserts the
conversations into the OpenCode SQLite database so they become visible
in the UI.

Interactive REPL: type a keyword to filter by title / first prompt /
cwd, then a number to import. Handles custom-title events (chats
renamed from the Claude Code UI), groups events into turns, remaps
tool names to the opencode conventions used by tool-mapping.ts, and
uses the per-turn max usage to avoid the double-count bug that would
otherwise inflate the context counter.

Falls back to the 'global' project when the jsonl cwd does not match
any opencode project worktree; session.directory still preserves the
original path so the chat appears under the correct folder in the UI.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant