Files
alan 1de74f76bb Chat info: Session section with native id, resume command and export
The chat information sheet gains a Session section: the harness's native
session id (copy), harness, directory, a verified resume command (copy)
and Export conversation (Markdown), Export JSON and, for Claude Code,
Export native session (the JSONL transcript).

Server: chat_sessions rows carry resume_command and native_export; new
chat_export command builds Markdown/JSON from the same paged history
chat_messages serves (bounded at 8 MiB), refuses non-operator sessions.
App: exports go through the artifact file bridge (web download, share
sheet on phones).
2026-11-03 10:59:41 +00:00

58 lines
1.8 KiB
Go

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
}