Documentation
¶
Overview ¶
Package chatimport reads the conversation history a user exported from Claude.ai or ChatGPT themselves, and files each conversation as a read-only chat (ADR-074).
Neither service offers a supported way for a third-party program to read a consumer account's conversations (OQ-22), so this is an import, never a sync: the user asks the service for their data, downloads the archive it emails them, and hands Caprock the archive or the conversations.json inside it. Caprock reads the file on this machine and calls nothing (rule 4).
Neither service documents the file. The shapes read here are the ones open-source readers of the exports agree on (sources in OQ-22, read 2026-10-17), and every field is optional: a field a later export drops reads as empty, never as a failed import.
Both archives carry a conversations.json that is one JSON array of conversations, which can run to hundreds of megabytes; it is decoded one conversation at a time, so memory follows the largest conversation, not the archive.
Index ¶
- Constants
- Variables
- func File(p string, fn func(Conversation) error) error
- func IsSource(agent string) bool
- func JSON(r io.Reader, fn func(Conversation) error) error
- func Save(ctx context.Context, st *store.Store, c Conversation, res *Result) error
- func SessionID(source, id string) string
- func SourceName(source string) string
- func Zip(r io.ReaderAt, size int64, fn func(Conversation) error) error
- type Conversation
- type Message
- type Result
Constants ¶
const ( SourceClaudeAI = "claude.ai" SourceChatGPT = "chatgpt" )
Sources: what a conversation came from. The same values are stored as the imported chat's sessions.agent, so every screen that names an agent names the service.
const MaxText = 64000
MaxText caps one imported message, in runes. Higher than the live cap on an agent's prose (event.MaxAssistantText): an import is the user's own archive, read once, and the end of a long answer is often the part they kept it for.
Variables ¶
var ErrNotExport = errors.New("this is not a Claude.ai or ChatGPT export: no conversations.json with conversations in it")
ErrNotExport is returned for a file that is neither export.
Functions ¶
func File ¶
func File(p string, fn func(Conversation) error) error
File reads an export at p — the zip the service emailed, or the conversations.json from inside it — and calls fn for each conversation. An error from fn stops the read with that error.
func JSON ¶
func JSON(r io.Reader, fn func(Conversation) error) error
JSON reads a conversations.json: an array of conversations, each told apart by its own shape — Claude.ai's carry chat_messages, ChatGPT's a mapping.
func Save ¶
Save files one conversation as a read-only chat and counts what it did.
The conversation is a session (ADR-071: a chat is a session) with `chat = 1`, `status = 'ended'`, no folder, no process, the source as its agent and the service's id as its native id. Each message is an event keyed by the service's message id, so a re-import adds only the messages that are new. The events carry no tokens and no cost and are marked `internal`, which every aggregate already leaves out: a conversation held on Claude.ai or ChatGPT is not work done on this machine, and must not move a day's activity or spend. The conversation view reads them all the same. No daily row, no session_stats row: nothing to take back out on delete.
func SessionID ¶
SessionID is the Caprock session an imported conversation is stored as: the source and the service's own id, so importing the same export again lands on the same row.
func SourceName ¶
SourceName is how a source is written on screen.
Types ¶
type Conversation ¶
type Conversation struct {
Source string
ID string // the service's own conversation id
Title string
Model string // the model the service names, when it names one
Created time.Time
Updated time.Time
// Messages is the conversation as the service shows it: the branch that
// ends at its current message, not the edited versions beside it.
Messages []Message
}
Conversation is one conversation from an export.
type Result ¶
type Result struct {
// Conversations is how many the export held that had something to read.
Conversations int `json:"conversations"`
// Added is new chats; Updated is chats already imported that the export
// carried new messages for; Unchanged is chats already imported as they
// are.
Added int `json:"added"`
Updated int `json:"updated"`
Unchanged int `json:"unchanged"`
// Deleted is conversations the user deleted from Caprock after an earlier
// import: they stay deleted (ADR-037's tombstone).
Deleted int `json:"deleted"`
// Empty is conversations with no message to show.
Empty int `json:"empty"`
// Messages is how many messages were added.
Messages int `json:"messages"`
// Sources counts the conversations read per source.
Sources map[string]int `json:"sources"`
}
Result is what one import did, counted per conversation.