opencode

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: 12 Imported by: 0

Documentation

Overview

Package opencode provides an adapter for OpenCode that reads sessions with project indexing and session metadata caching.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewWatcher

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

NewWatcher creates a watcher for OpenCode session changes.

func ToolInputString

func ToolInputString(input map[string]any) string

ToolInputString extracts a string representation of tool input.

func ToolOutputString

func ToolOutputString(output any) string

ToolOutputString extracts a string representation of tool output.

Types

type Adapter

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

Adapter implements the adapter.Adapter interface for OpenCode sessions.

func New

func New() *Adapter

New creates a new OpenCode 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 OpenCode sessions exist for the given project.

func (*Adapter) DiscoverRelatedProjectDirs

func (a *Adapter) DiscoverRelatedProjectDirs(mainWorktreePath string) ([]string, error)

DiscoverRelatedProjectDirs scans OpenCode project files for worktree paths related to the given main worktree path. This finds conversations from deleted worktrees by checking if stored worktree paths share the same repository base name.

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 batch reading to minimize file I/O overhead.

func (*Adapter) Name

func (a *Adapter) Name() string

Name returns the human-readable adapter name.

func (*Adapter) SearchMessages

func (a *Adapter) SearchMessages(sessionID, query string, opts adapter.SearchOptions) ([]adapter.MessageMatch, error)

SearchMessages searches message content within a session. Implements adapter.MessageSearcher interface.

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.

type CacheInfo

type CacheInfo struct {
	Read  int `json:"read,omitempty"`
	Write int `json:"write,omitempty"`
}

CacheInfo holds cache-related token information.

type DiffSummary

type DiffSummary struct {
	Additions int `json:"additions"`
	Deletions int `json:"deletions"`
	Files     int `json:"files"`
}

DiffSummary holds aggregate diff statistics for a session.

type FileSource

type FileSource struct {
	Type string          `json:"type"`
	Path string          `json:"path"`
	Text *FileSourceText `json:"text,omitempty"`
}

FileSource holds file reference source information.

type FileSourceText

type FileSourceText struct {
	Value string `json:"value"`
	Start int    `json:"start"`
	End   int    `json:"end"`
}

FileSourceText holds text range for file references.

type Icon

type Icon struct {
	Color string `json:"color,omitempty"`
}

Icon holds project icon settings.

type Message

type Message struct {
	ID         string          `json:"id"`
	SessionID  string          `json:"sessionID"`
	Role       string          `json:"role"` // "user" or "assistant"
	Time       TimeInfo        `json:"time"`
	ParentID   string          `json:"parentID,omitempty"`
	ModelID    string          `json:"modelID,omitempty"`
	ProviderID string          `json:"providerID,omitempty"`
	Mode       string          `json:"mode,omitempty"`
	Agent      string          `json:"agent,omitempty"`
	Path       *PathInfo       `json:"path,omitempty"`
	Cost       float64         `json:"cost,omitempty"`
	Tokens     *TokenInfo      `json:"tokens,omitempty"`
	Finish     string          `json:"finish,omitempty"`
	Summary    *MessageSummary `json:"summary,omitempty"`
	Model      *ModelInfo      `json:"model,omitempty"` // Alternative model location for user messages
}

Message represents an OpenCode message from storage/message/{sessionID}/{messageID}.json.

type MessageSummary

type MessageSummary struct {
	Title string        `json:"title,omitempty"`
	Diffs []SummaryDiff `json:"diffs,omitempty"`
}

MessageSummary holds message summary information.

type ModelInfo

type ModelInfo struct {
	ProviderID string `json:"providerID"`
	ModelID    string `json:"modelID"`
}

ModelInfo holds model information (used in user messages).

type Part

type Part struct {
	ID        string    `json:"id"`
	SessionID string    `json:"sessionID"`
	MessageID string    `json:"messageID"`
	Type      string    `json:"type"` // text, tool, step-start, step-finish, file, patch, compaction
	Time      *PartTime `json:"time,omitempty"`

	// Text part fields
	Text string `json:"text,omitempty"`

	// Tool part fields
	CallID string     `json:"callID,omitempty"`
	Tool   string     `json:"tool,omitempty"`
	State  *ToolState `json:"state,omitempty"`

	// Step part fields
	Snapshot string     `json:"snapshot,omitempty"`
	Reason   string     `json:"reason,omitempty"`
	Cost     float64    `json:"cost,omitempty"`
	Tokens   *TokenInfo `json:"tokens,omitempty"`

	// File part fields (type: "file")
	Mime     string      `json:"mime,omitempty"`
	Filename string      `json:"filename,omitempty"`
	URL      string      `json:"url,omitempty"`
	Source   *FileSource `json:"source,omitempty"`

	// Patch part fields (type: "patch")
	Hash  string   `json:"hash,omitempty"`
	Files []string `json:"files,omitempty"`
}

Part represents a content part from storage/part/{messageID}/{partID}.json.

type PartTime

type PartTime struct {
	Start int64 `json:"start,omitempty"`
	End   int64 `json:"end,omitempty"`
}

PartTime holds timing info for parts.

type PathInfo

type PathInfo struct {
	CWD  string `json:"cwd"`
	Root string `json:"root"`
}

PathInfo holds working directory information.

type Project

type Project struct {
	ID       string   `json:"id"`
	Worktree string   `json:"worktree"`
	VCS      string   `json:"vcs,omitempty"`
	Time     TimeInfo `json:"time"`
	Icon     *Icon    `json:"icon,omitempty"`
}

Project represents an OpenCode project from storage/project/{id}.json.

type Session

type Session struct {
	ID        string       `json:"id"`
	Version   string       `json:"version,omitempty"`
	ProjectID string       `json:"projectID"`
	Directory string       `json:"directory"`
	Title     string       `json:"title,omitempty"`
	ParentID  string       `json:"parentID,omitempty"` // Non-empty for sub-agents
	Time      TimeInfo     `json:"time"`
	Summary   *DiffSummary `json:"summary,omitempty"`
}

Session represents an OpenCode session from storage/session/{projectID}/{sessionID}.json.

type SessionDiffEntry

type SessionDiffEntry struct {
	File      string `json:"file"`
	Before    string `json:"before"`
	After     string `json:"after"`
	Additions *int   `json:"additions"` // nil for binary files
	Deletions *int   `json:"deletions"`
}

SessionDiffEntry represents a file diff from storage/session_diff/{sessionID}.json.

type SessionMetadata

type SessionMetadata struct {
	Path             string
	SessionID        string
	ProjectID        string
	Title            string
	ParentID         string
	FirstMsg         time.Time
	LastMsg          time.Time
	MsgCount         int
	TotalTokens      int
	EstCost          float64
	PrimaryModel     string
	Additions        int
	Deletions        int
	FileCount        int
	FirstUserMessage string // Content of the first user message (for title fallback)
}

SessionMetadata holds aggregated metadata for adapter.Session mapping.

type SummaryDiff

type SummaryDiff struct {
	File      string `json:"file"`
	Additions int    `json:"additions"`
	Deletions int    `json:"deletions"`
}

SummaryDiff represents a diff entry in message summary.

type TimeInfo

type TimeInfo struct {
	Created   int64 `json:"created,omitempty"`
	Updated   int64 `json:"updated,omitempty"`
	Completed int64 `json:"completed,omitempty"`
}

TimeInfo holds timestamp information in Unix milliseconds.

func (TimeInfo) CompletedTime

func (t TimeInfo) CompletedTime() time.Time

CompletedTime converts completed timestamp to time.Time in local timezone.

func (TimeInfo) CreatedTime

func (t TimeInfo) CreatedTime() time.Time

CreatedTime converts created timestamp to time.Time in local timezone.

func (TimeInfo) UpdatedTime

func (t TimeInfo) UpdatedTime() time.Time

UpdatedTime converts updated timestamp to time.Time in local timezone.

type TokenInfo

type TokenInfo struct {
	Input     int        `json:"input,omitempty"`
	Output    int        `json:"output,omitempty"`
	Reasoning int        `json:"reasoning,omitempty"`
	Cache     *CacheInfo `json:"cache,omitempty"`
}

TokenInfo holds token usage information.

type ToolMetadata

type ToolMetadata struct {
	Output      string `json:"output,omitempty"`
	Exit        int    `json:"exit,omitempty"`
	Description string `json:"description,omitempty"`
}

ToolMetadata holds additional tool metadata.

type ToolState

type ToolState struct {
	Status   string         `json:"status"` // "completed", "pending", etc.
	Input    map[string]any `json:"input,omitempty"`
	Output   any            `json:"output,omitempty"` // Can be string or object
	Title    string         `json:"title,omitempty"`
	Metadata *ToolMetadata  `json:"metadata,omitempty"`
	Time     *ToolTime      `json:"time,omitempty"`
}

ToolState holds the state of a tool call.

type ToolTime

type ToolTime struct {
	Start int64 `json:"start,omitempty"`
	End   int64 `json:"end,omitempty"`
}

ToolTime holds timing info for tool execution.

Jump to

Keyboard shortcuts

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