package claude import ( "context" "errors" "fmt" "io" "os" "unicode/utf8" "git.a-wan.net/alan/pipelines/internal/protocol" ) // NativeExportLimit bounds the Claude Code session file chat_export returns. const NativeExportLimit = 8 << 20 // NativeExport returns Claude Code's own JSONL transcript for one session, the // file `claude --resume` reads. It is a plain read of the retained file; no CLI // process is started. func (c *Chat) NativeExport(ctx context.Context, id string) (protocol.ChatExportFile, error) { path := findTranscriptFile(id) if path == "" { return protocol.ChatExportFile{}, errors.New("Claude Code has not written a transcript for session " + id + " yet") } file, err := os.Open(path) if err != nil { return protocol.ChatExportFile{}, err } defer file.Close() info, err := file.Stat() if err != nil { return protocol.ChatExportFile{}, err } if !info.Mode().IsRegular() { return protocol.ChatExportFile{}, errors.New("the Claude Code transcript is not a regular file") } if info.Size() > NativeExportLimit { return protocol.ChatExportFile{}, fmt.Errorf("the Claude Code transcript is %d bytes, over the %d byte export limit; it is at %s on the server", info.Size(), NativeExportLimit, path) } data, err := io.ReadAll(io.LimitReader(file, NativeExportLimit+1)) if err != nil { return protocol.ChatExportFile{}, err } if len(data) > NativeExportLimit { return protocol.ChatExportFile{}, fmt.Errorf("the Claude Code transcript grew past the %d byte export limit", NativeExportLimit) } if !utf8.Valid(data) { return protocol.ChatExportFile{}, errors.New("the Claude Code transcript is not valid UTF-8") } return protocol.ChatExportFile{ SessionID: id, Format: protocol.ChatExportNative, Name: id + ".jsonl", Mime: "application/x-ndjson", Content: string(data), }, nil }