pijsonl

package
v0.12.2 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package pijsonl provides shared parsing primitives for Pi's session JSONL format. It is consumed both by the in-tree pi agent (cmd/entire/cli/agent/pi) and by the transcript compaction package (transcript). Keeping these in one place ensures active-branch resolution, line counting, and offset slicing stay byte- compatible across both call sites.

Index

Constants

View Source
const (
	RoleUser       = "user"
	RoleAssistant  = "assistant"
	RoleToolResult = "toolResult"
)

Role values present on Message.Role.

View Source
const ContentTypeText = "text"

ContentTypeText is the content-block `type` value for text blocks.

View Source
const EntryTypeMessage = "message"

EntryTypeMessage is the JSONL `type` value for conversational entries.

View Source
const MaxScannerLine = 10 * 1024 * 1024

MaxScannerLine is the maximum size of a single JSONL line we will parse. Pi tool calls can embed full file contents in arguments — 10 MB matches what other in-tree transcript scanners use (codex, copilot, droid).

Variables

This section is empty.

Functions

func CountLines

func CountLines(data []byte) int

CountLines returns the number of newline-terminated (or final unterminated) lines in data, including blank lines. Matches the offset semantics used by SkipLines and the compact-format StartLine.

func DecodeStringContent

func DecodeStringContent(raw json.RawMessage) string

DecodeStringContent returns the raw string when content is a plain string, or "" when it's a JSON array (caller should decode as []ContentItem).

func ForEachActiveMessage

func ForEachActiveMessage(data []byte, fromOffset int, fn func(Entry)) error

ForEachActiveMessage invokes fn for every message entry on the active conversation branch, starting at line fromOffset.

It owns the parsing skeleton shared by all Pi transcript analysis: active- branch resolution runs on the FULL data (so parentId chains stay intact) while iteration starts at fromOffset, lines that fail to unmarshal or are off the active branch are skipped, and only entries with type=="message" reach fn. Callers apply their own role filter inside fn. Empty data is a no-op.

func NewScanner

func NewScanner(data []byte) *bufio.Scanner

NewScanner returns a bufio.Scanner pre-configured with the Pi line-size limit.

func ResolveActiveBranch

func ResolveActiveBranch(data []byte) map[string]bool

ResolveActiveBranch walks a Pi transcript tree and returns the set of entry IDs on the active conversation branch (root → most-recent message).

Pi sessions form a tree: every entry has id and parentId. When the user /forks or /clones mid-conversation, the JSONL file accumulates entries from BOTH branches. Without filtering, downstream analysis double-counts tokens, files, and prompts.

IMPORTANT: callers must pass FULL transcript bytes, not bytes already truncated by SkipLines. Resolving over a truncated buffer yields a disconnected tree where parentId pointers no longer reach the root, causing abandoned-branch entries to leak in.

Returns nil when the transcript has no tree structure (every entry has no parent or all entries are linear) — callers should treat nil as "all entries are on the active branch".

func SkipLines

func SkipLines(data []byte, n int) []byte

SkipLines returns data with the first n newline-terminated lines removed. Returns nil if data has fewer than n lines.

Types

type ContentItem

type ContentItem struct {
	Type      string          `json:"type"`
	Text      string          `json:"text,omitempty"`
	Name      string          `json:"name,omitempty"`
	ID        string          `json:"id,omitempty"`
	Arguments json.RawMessage `json:"arguments,omitempty"`
}

ContentItem is one element of a Pi message's content array.

type Entry

type Entry struct {
	Type      string  `json:"type"`
	ID        string  `json:"id"`
	Timestamp string  `json:"timestamp"`
	Message   Message `json:"message"`
}

Entry is the outer shell of one Pi JSONL line.

type Message

type Message struct {
	Role       string          `json:"role"`
	Content    json.RawMessage `json:"content"`
	Usage      *Usage          `json:"usage,omitempty"`
	Model      string          `json:"model,omitempty"`
	StopReason string          `json:"stopReason,omitempty"`
	ToolCallID string          `json:"toolCallId,omitempty"`
	ToolName   string          `json:"toolName,omitempty"`
	IsError    bool            `json:"isError,omitempty"`
}

Message is the inner Pi `message` object on entries with type=="message".

type Usage

type Usage struct {
	Input      int `json:"input"`
	Output     int `json:"output"`
	CacheRead  int `json:"cacheRead"`
	CacheWrite int `json:"cacheWrite"`
	// CacheWrite1h is the part of CacheWrite written with the 1h TTL, and
	// Reasoning the part of Output spent reasoning. nil means not reported.
	CacheWrite1h *int `json:"cacheWrite1h,omitempty"`
	Reasoning    *int `json:"reasoning,omitempty"`
}

Usage mirrors pi-ai's Usage struct (token-count fields only).

Jump to

Keyboard shortcuts

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