jsonl

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 23, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package jsonl provides shared types and iteration utilities for Claude Code JSONL session files.

Index

Constants

View Source
const (
	// MaxFileSize is the maximum JSONL file size ForEachEntry will process.
	// Files exceeding this limit are skipped to prevent OOM from runaway logs.
	MaxFileSize = 100 * 1024 * 1024

	// MaxLineSize is the maximum byte length of a single JSONL line (including
	// the newline terminator). Lines exceeding this limit are skipped.
	MaxLineSize = 1024 * 1024
)

Variables

This section is empty.

Functions

func ForEachEntry

func ForEachEntry(path string, offset int64, visitor EntryVisitor) (int64, error)

ForEachEntry reads a JSONL file from offset, calling visitor for each complete, parseable line. Returns the final byte offset after the last complete line consumed.

Files exceeding MaxFileSize are rejected with an error. Lines exceeding MaxLineSize are skipped with a warning log and parsing continues.

Types

type ContentBlock

type ContentBlock struct {
	Type      string          `json:"type"`
	Name      string          `json:"name,omitempty"`
	ID        string          `json:"id,omitempty"`          // tool_use block ID
	ToolUseID string          `json:"tool_use_id,omitempty"` // tool_result references the tool_use
	Text      string          `json:"text,omitempty"`        // text content block
	Input     json.RawMessage `json:"input,omitempty"`       // tool_use input
	Content   json.RawMessage `json:"content,omitempty"`     // tool_result content
}

ContentBlock is a single block inside a message's content array.

type Entry

type Entry struct {
	Type      string          `json:"type"`
	Subtype   string          `json:"subtype,omitempty"`
	UUID      string          `json:"uuid"`
	SessionID string          `json:"sessionId"`
	Slug      string          `json:"slug"`
	Timestamp string          `json:"timestamp"`
	Cwd       string          `json:"cwd"`
	Message   json.RawMessage `json:"message"`
}

Entry is the top-level structure of a Claude Code JSONL line.

func (*Entry) ParseTimestamp

func (e *Entry) ParseTimestamp() (time.Time, bool)

ParseTimestamp parses the entry's RFC3339Nano timestamp.

type EntryVisitor

type EntryVisitor func(entry *Entry, line []byte) bool

EntryVisitor is called for each parsed JSONL entry. Return false to stop iteration. The line parameter contains the raw bytes of the line without the newline terminator.

type MessageContent

type MessageContent struct {
	Model   string          `json:"model"`
	Role    string          `json:"role"`
	Usage   *TokenUsage     `json:"usage,omitempty"`
	Content json.RawMessage `json:"content"`
}

MessageContent is the message object inside assistant/user entries.

type ProgressData

type ProgressData struct {
	Message struct {
		Type    string          `json:"type"` // "assistant" or "user"
		Message json.RawMessage `json:"message"`
	} `json:"message"`
}

ProgressData wraps the nested data.message structure inside a progress entry.

type ProgressDataHeader

type ProgressDataHeader struct {
	Type string `json:"type"`
}

ProgressDataHeader is used for fast type-checking of progress data.

type ProgressEntry

type ProgressEntry struct {
	Type            string          `json:"type"`
	ToolUseID       string          `json:"toolUseID"`
	ParentToolUseID string          `json:"parentToolUseID"`
	SessionID       string          `json:"sessionId"`
	Slug            string          `json:"slug"`
	Timestamp       string          `json:"timestamp"`
	Data            json.RawMessage `json:"data"`
}

ProgressEntry is the top-level structure for type:"progress" JSONL entries.

type TokenUsage

type TokenUsage struct {
	InputTokens              int `json:"input_tokens"`
	CacheCreationInputTokens int `json:"cache_creation_input_tokens"`
	CacheReadInputTokens     int `json:"cache_read_input_tokens"`
	OutputTokens             int `json:"output_tokens"`
}

TokenUsage represents API token usage from an assistant message.

func (TokenUsage) TotalContext

func (t TokenUsage) TotalContext() int

TotalContext returns the total context tokens (input + cache read + cache creation).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL