Something went wrong. Try again.
An archive of agent session transcripts, captured over OTLP and served as MCP tools
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105package ship
import ( "bufio" "encoding/json" "errors" "io" "io/fs" "os" "path/filepath" "sort" "strconv" "strings" "time")
// transcripts returns the path of every .jsonl file under dir, relative// to dir. Subagents' transcripts are in subdirectories, so the search// goes all the way down.func transcripts(dir string) ([]string, error) { var files []string err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { if errors.Is(err, fs.ErrNotExist) && path == dir { return filepath.SkipDir } if err != nil { return err } if !d.IsDir() && strings.HasSuffix(path, ".jsonl") { rel, err := filepath.Rel(dir, path) if err != nil { return err } files = append(files, rel) } return nil }) sort.Strings(files) return files, err}
// entry is one line of a transcript, with the fields that the shipper// sends as attributes.type entry struct { file string line []byte end int64 session string key string time time.Time}
// read calls each for every complete line of the file after offset. A// line without its newline is still being written, so read stops before// it, and the next run reads it.func read(path, rel string, offset int64, each func(entry) error) error { f, err := os.Open(path) if err != nil { return err } defer f.Close() if _, err := f.Seek(offset, io.SeekStart); err != nil { return err }
stem := strings.TrimSuffix(filepath.Base(rel), ".jsonl") reader := bufio.NewReaderSize(f, 1<<20) for { line, err := reader.ReadBytes('\n') if err == io.EOF { return nil } if err != nil { return err } start := offset offset += int64(len(line)) if err := each(parse(rel, stem, line[:len(line)-1], start, offset)); err != nil { return err } }}
// parse reads the entry's uuid, session, and time. An entry without a// uuid is keyed by its offset in the file, and an entry without a// session takes the file's name, which is the session ID.func parse(rel, stem string, line []byte, start, end int64) entry { var fields struct { UUID string `json:"uuid"` SessionID string `json:"sessionId"` Timestamp string `json:"timestamp"` } json.Unmarshal(line, &fields)
e := entry{file: rel, line: line, end: end, session: fields.SessionID, key: fields.UUID} if e.session == "" { e.session = stem } if e.key == "" { e.key = "offset:" + strconv.FormatInt(start, 10) } e.time, _ = time.Parse(time.RFC3339Nano, fields.Timestamp) return e}