pi

package
v0.74.1 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package pi provides an adapter for Pi AI agent sessions (embedded in OpenClaw). Pi stores sessions as JSONL files in ~/.openclaw/agents/main/sessions/.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewWatcher

func NewWatcher(sessionsDir string) (<-chan adapter.Event, io.Closer, error)

NewWatcher creates a watcher for Pi session changes. Pi stores sessions as .jsonl files in a flat directory (~/.openclaw/agents/main/sessions/).

Types

type Adapter

type Adapter struct {
	// contains filtered or unexported fields
}

Adapter implements the adapter.Adapter interface for Pi sessions.

func New

func New() *Adapter

New creates a new Pi adapter.

func (*Adapter) Capabilities

func (a *Adapter) Capabilities() adapter.CapabilitySet

Capabilities returns the supported features.

func (*Adapter) Detect

func (a *Adapter) Detect(projectRoot string) (bool, error)

Detect checks if Pi sessions exist for the given project.

func (*Adapter) ID

func (a *Adapter) ID() string

ID returns the adapter identifier.

func (*Adapter) Icon

func (a *Adapter) Icon() string

Icon returns the adapter icon for badge display.

func (*Adapter) Messages

func (a *Adapter) Messages(sessionID string) ([]adapter.Message, error)

Messages returns all messages for the given session. Uses caching with incremental parsing for append-only growth optimization.

func (*Adapter) Name

func (a *Adapter) Name() string

Name returns the human-readable adapter name.

func (*Adapter) Sessions

func (a *Adapter) Sessions(projectRoot string) ([]adapter.Session, error)

Sessions returns all sessions for the given project, sorted by update time.

func (*Adapter) Usage

func (a *Adapter) Usage(sessionID string) (*adapter.UsageStats, error)

Usage returns aggregate usage stats for the given session.

func (*Adapter) Watch

func (a *Adapter) Watch(projectRoot string) (<-chan adapter.Event, io.Closer, error)

Watch returns a channel that emits events when session data changes.

func (*Adapter) WatchScope

func (a *Adapter) WatchScope() adapter.WatchScope

WatchScope returns Global because Pi watches a global sessions directory.

type ContentBlock

type ContentBlock struct {
	Type              string          `json:"type"`                        // "text", "thinking", "toolCall", "image"
	Text              string          `json:"text,omitempty"`              // for text blocks
	Thinking          string          `json:"thinking,omitempty"`          // for thinking blocks
	ThinkingSignature string          `json:"thinkingSignature,omitempty"` // for thinking blocks
	ID                string          `json:"id,omitempty"`                // for toolCall: the tool use ID
	Name              string          `json:"name,omitempty"`              // for toolCall: tool name
	Arguments         json.RawMessage `json:"arguments,omitempty"`         // for toolCall: tool input
}

ContentBlock represents a single block in a message's content array.

type Cost

type Cost struct {
	Input      float64 `json:"input"`
	Output     float64 `json:"output"`
	CacheRead  float64 `json:"cacheRead"`
	CacheWrite float64 `json:"cacheWrite"`
	Total      float64 `json:"total"`
}

Cost holds pre-calculated cost breakdown in dollars.

type Details

type Details struct {
	Diff string `json:"diff,omitempty"`
}

Details holds extra information attached to toolResult messages.

type MessageContent

type MessageContent struct {
	Role       string          `json:"role"`                 // "user", "assistant", "toolResult"
	Content    json.RawMessage `json:"content"`              // array of ContentBlock
	Model      string          `json:"model,omitempty"`      // model ID for assistant messages
	Provider   string          `json:"provider,omitempty"`   // e.g. "anthropic"
	API        string          `json:"api,omitempty"`        // e.g. "anthropic-messages"
	Usage      *Usage          `json:"usage,omitempty"`      // token usage for assistant messages
	StopReason string          `json:"stopReason,omitempty"` // e.g. "end_turn"
	ToolCallID string          `json:"toolCallId,omitempty"` // for toolResult: links to toolCall block ID
	ToolName   string          `json:"toolName,omitempty"`   // for toolResult: name of the tool
	Details    *Details        `json:"details,omitempty"`    // for toolResult: extra info (e.g. diff)
}

MessageContent holds the message payload for type="message" lines.

type RawLine

type RawLine struct {
	Type      string          `json:"type"`              // "session", "message", "model_change", "thinking_level_change", "custom"
	ID        string          `json:"id"`                // line identifier
	ParentID  *string         `json:"parentId"`          // parent line reference (nullable)
	Timestamp time.Time       `json:"timestamp"`         // line timestamp
	Message   *MessageContent `json:"message,omitempty"` // populated for type="message"

	// Session header fields (type="session")
	Version int    `json:"version,omitempty"` // session format version
	CWD     string `json:"cwd,omitempty"`     // working directory

	// Model change fields (type="model_change")
	Provider string `json:"provider,omitempty"` // e.g. "anthropic"
	ModelID  string `json:"modelId,omitempty"`  // e.g. "claude-opus-4-5"

	// Thinking level change fields (type="thinking_level_change")
	ThinkingLevel string `json:"thinkingLevel,omitempty"` // e.g. "low", "medium", "high"

	// Custom event fields (type="custom")
	CustomType string          `json:"customType,omitempty"` // e.g. "model-snapshot"
	Data       json.RawMessage `json:"data,omitempty"`       // arbitrary event data
}

RawLine represents any JSONL line from a Pi session file. Fields are a superset; only relevant fields are populated per line type.

type SessionMetadata

type SessionMetadata struct {
	Path             string    // absolute path to session file
	SessionID        string    // unique session ID from header
	CWD              string    // working directory from header
	Version          int       // session format version
	FirstMsg         time.Time // timestamp of first user/assistant message
	LastMsg          time.Time // timestamp of last user/assistant message
	MsgCount         int       // count of user/assistant messages
	TotalTokens      int       // sum of tokens across all assistant messages
	EstCost          float64   // total cost from pre-calculated usage.cost.total
	PrimaryModel     string    // most-used model in session
	FirstUserMessage string    // content of first user message (used as title)
	SessionCategory  string    // "interactive", "cron", "system"
	CronJobName      string    // extracted job name from "[cron:UUID name]" prefix
	SourceChannel    string    // "telegram", "whatsapp", "direct"
}

SessionMetadata holds extracted metadata about a Pi session file.

type Usage

type Usage struct {
	Input       int   `json:"input"`       // input tokens
	Output      int   `json:"output"`      // output tokens
	CacheRead   int   `json:"cacheRead"`   // cache read tokens
	CacheWrite  int   `json:"cacheWrite"`  // cache write tokens
	TotalTokens int   `json:"totalTokens"` // total tokens
	Cost        *Cost `json:"cost,omitempty"`
}

Usage tracks token counts and cost for an assistant message.

Jump to

Keyboard shortcuts

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