Documentation
¶
Overview ¶
Package opencode implements the Agent interface for OpenCode.
Index ¶
- Constants
- Variables
- func CalculateTokenUsageFromBytes(data []byte, startMessageIndex int) *agent.TokenUsage
- func ExtractAllUserPrompts(data []byte) ([]string, error)
- func ExtractModifiedFiles(data []byte) ([]string, error)
- func ExtractTextFromParts(parts []Part) string
- func NewOpenCodeAgent() agent.Agent
- func SanitizePathForOpenCode(path string) string
- func SliceFromMessage(data []byte, startMessageIndex int) ([]byte, error)
- type Cache
- type ExportMessage
- type ExportSession
- type MessageInfo
- type OpenCodeAgent
- func (a *OpenCodeAgent) AreHooksInstalled() bool
- func (a *OpenCodeAgent) CalculateTokenUsage(sessionRef string, fromOffset int) (*agent.TokenUsage, error)
- func (a *OpenCodeAgent) ChunkTranscript(content []byte, maxSize int) ([][]byte, error)
- func (a *OpenCodeAgent) Description() string
- func (a *OpenCodeAgent) DetectPresence() (bool, error)
- func (a *OpenCodeAgent) ExtractModifiedFilesFromOffset(path string, startOffset int) ([]string, int, error)
- func (a *OpenCodeAgent) ExtractPrompts(sessionRef string, fromOffset int) ([]string, error)
- func (a *OpenCodeAgent) ExtractSummary(sessionRef string) (string, error)
- func (a *OpenCodeAgent) FormatResumeCommand(sessionID string) string
- func (a *OpenCodeAgent) GetSessionDir(repoPath string) (string, error)
- func (a *OpenCodeAgent) GetSessionID(input *agent.HookInput) string
- func (a *OpenCodeAgent) GetSupportedHooks() []agent.HookType
- func (a *OpenCodeAgent) GetTranscriptPosition(path string) (int, error)
- func (a *OpenCodeAgent) HookNames() []string
- func (a *OpenCodeAgent) InstallHooks(localDev bool, force bool) (int, error)
- func (a *OpenCodeAgent) IsPreview() bool
- func (a *OpenCodeAgent) Name() agent.AgentName
- func (a *OpenCodeAgent) ParseHookEvent(hookName string, stdin io.Reader) (*agent.Event, error)
- func (a *OpenCodeAgent) PrepareTranscript(sessionRef string) error
- func (a *OpenCodeAgent) ProtectedDirs() []string
- func (a *OpenCodeAgent) ReadSession(input *agent.HookInput) (*agent.AgentSession, error)
- func (a *OpenCodeAgent) ReadTranscript(sessionRef string) ([]byte, error)
- func (a *OpenCodeAgent) ReassembleTranscript(chunks [][]byte) ([]byte, error)
- func (a *OpenCodeAgent) ResolveSessionFile(sessionDir, agentSessionID string) string
- func (a *OpenCodeAgent) Type() agent.AgentType
- func (a *OpenCodeAgent) UninstallHooks() error
- func (a *OpenCodeAgent) WriteSession(session *agent.AgentSession) error
- type Part
- type SessionInfo
- type Time
- type Tokens
- type ToolState
Constants ¶
const ( HookNameSessionStart = "session-start" HookNameSessionEnd = "session-end" HookNameTurnStart = "turn-start" HookNameTurnEnd = "turn-end" HookNameCompaction = "compaction" )
Hook name constants — these become CLI subcommands under `entire hooks opencode`.
Variables ¶
var FileModificationTools = []string{
"edit",
"write",
"patch",
}
FileModificationTools are tools in OpenCode that modify files on disk. These match the actual tool names from OpenCode's source:
- edit: internal/llm/tools/edit.go (EditToolName)
- write: internal/llm/tools/write.go (WriteToolName)
- patch: internal/llm/tools/patch.go (PatchToolName)
Functions ¶
func CalculateTokenUsageFromBytes ¶
func CalculateTokenUsageFromBytes(data []byte, startMessageIndex int) *agent.TokenUsage
CalculateTokenUsageFromBytes computes token usage from raw export JSON transcript bytes starting at the given message offset. This is a package-level function used by the condensation path (which has bytes, not a file path).
func ExtractAllUserPrompts ¶
ExtractAllUserPrompts extracts all user prompts from raw export JSON transcript bytes. This is a package-level function used by the condensation path.
func ExtractModifiedFiles ¶
ExtractModifiedFiles extracts modified file paths from raw export JSON transcript bytes. This is the bytes-based equivalent of ExtractModifiedFilesFromOffset, used by ReadSession.
func ExtractTextFromParts ¶
ExtractTextFromParts extracts text content from message parts.
func NewOpenCodeAgent ¶
NewOpenCodeAgent creates a new OpenCode agent instance.
func SanitizePathForOpenCode ¶
SanitizePathForOpenCode converts a path to a safe directory name. Replaces any non-alphanumeric character with a dash (same approach as Claude/Gemini).
func SliceFromMessage ¶
SliceFromMessage returns an OpenCode export transcript scoped to messages starting from startMessageIndex. This is the OpenCode equivalent of transcript.SliceFromLine — for OpenCode's JSON format, scoping is done by message index rather than line offset. Returns the original data if startMessageIndex <= 0. Returns nil, nil if startMessageIndex exceeds the number of messages.
Types ¶
type ExportMessage ¶
type ExportMessage struct {
Info MessageInfo `json:"info"`
Parts []Part `json:"parts"`
}
ExportMessage represents a single message in the export format. Each message contains info (metadata) and parts (content).
type ExportSession ¶
type ExportSession struct {
Info SessionInfo `json:"info"`
Messages []ExportMessage `json:"messages"`
}
ExportSession represents the top-level structure of `opencode export` output. This is OpenCode's native format for session data.
func ParseExportSession ¶
func ParseExportSession(data []byte) (*ExportSession, error)
ParseExportSession parses export JSON content into an ExportSession structure.
type MessageInfo ¶
type MessageInfo struct {
ID string `json:"id"`
SessionID string `json:"sessionID,omitempty"`
Role string `json:"role"` // "user" or "assistant"
Time Time `json:"time"`
Tokens *Tokens `json:"tokens,omitempty"`
Cost float64 `json:"cost,omitempty"`
}
MessageInfo contains message metadata.
type OpenCodeAgent ¶
type OpenCodeAgent struct{}
func (*OpenCodeAgent) AreHooksInstalled ¶
func (a *OpenCodeAgent) AreHooksInstalled() bool
AreHooksInstalled checks if the Entire plugin file exists and contains the marker.
func (*OpenCodeAgent) CalculateTokenUsage ¶
func (a *OpenCodeAgent) CalculateTokenUsage(sessionRef string, fromOffset int) (*agent.TokenUsage, error)
CalculateTokenUsage computes token usage from assistant messages starting at the given offset.
func (*OpenCodeAgent) ChunkTranscript ¶
func (a *OpenCodeAgent) ChunkTranscript(content []byte, maxSize int) ([][]byte, error)
ChunkTranscript splits an OpenCode export JSON transcript by distributing messages across chunks. OpenCode uses JSON format with {"info": {...}, "messages": [...]} structure.
func (*OpenCodeAgent) Description ¶
func (a *OpenCodeAgent) Description() string
func (*OpenCodeAgent) DetectPresence ¶
func (a *OpenCodeAgent) DetectPresence() (bool, error)
func (*OpenCodeAgent) ExtractModifiedFilesFromOffset ¶
func (a *OpenCodeAgent) ExtractModifiedFilesFromOffset(path string, startOffset int) ([]string, int, error)
ExtractModifiedFilesFromOffset extracts files modified by tool calls from the given message offset.
func (*OpenCodeAgent) ExtractPrompts ¶
func (a *OpenCodeAgent) ExtractPrompts(sessionRef string, fromOffset int) ([]string, error)
ExtractPrompts extracts user prompt strings from the transcript starting at the given offset.
func (*OpenCodeAgent) ExtractSummary ¶
func (a *OpenCodeAgent) ExtractSummary(sessionRef string) (string, error)
ExtractSummary extracts the last assistant message content as a summary.
func (*OpenCodeAgent) FormatResumeCommand ¶
func (a *OpenCodeAgent) FormatResumeCommand(sessionID string) string
func (*OpenCodeAgent) GetSessionDir ¶
func (a *OpenCodeAgent) GetSessionDir(repoPath string) (string, error)
GetSessionDir returns the directory where Entire stores OpenCode session transcripts. Transcripts are ephemeral handoff files between the TS plugin and the Go hook handler. Once checkpointed, the data lives on git refs and the file is disposable. Stored in os.TempDir()/entire-opencode/<sanitized-path>/ to avoid squatting on OpenCode's own directories (~/.opencode/ is project-level, not home-level).
func (*OpenCodeAgent) GetSessionID ¶
func (a *OpenCodeAgent) GetSessionID(input *agent.HookInput) string
func (*OpenCodeAgent) GetSupportedHooks ¶
func (a *OpenCodeAgent) GetSupportedHooks() []agent.HookType
GetSupportedHooks returns the normalized lifecycle events this agent supports. OpenCode's native hooks map to standard agent lifecycle events:
- session-start → HookSessionStart
- session-end → HookSessionEnd
- turn-start → HookUserPromptSubmit (user prompt triggers a turn)
- turn-end → HookStop (agent response complete)
Note: HookNames() returns 5 hooks (including "compaction"), but GetSupportedHooks() returns only 4. The "compaction" hook is OpenCode-specific with no standard HookType mapping — it is handled via ParseHookEvent but not advertised as a standard lifecycle event.
func (*OpenCodeAgent) GetTranscriptPosition ¶
func (a *OpenCodeAgent) GetTranscriptPosition(path string) (int, error)
GetTranscriptPosition returns the number of messages in the transcript.
func (*OpenCodeAgent) HookNames ¶
func (a *OpenCodeAgent) HookNames() []string
HookNames returns the hook verbs this agent supports.
func (*OpenCodeAgent) InstallHooks ¶
func (a *OpenCodeAgent) InstallHooks(localDev bool, force bool) (int, error)
InstallHooks writes the Entire plugin file to .opencode/plugins/entire.ts. Returns 1 if the plugin was installed, 0 if already present (idempotent).
func (*OpenCodeAgent) IsPreview ¶
func (a *OpenCodeAgent) IsPreview() bool
func (*OpenCodeAgent) Name ¶
func (a *OpenCodeAgent) Name() agent.AgentName
func (*OpenCodeAgent) ParseHookEvent ¶
ParseHookEvent translates OpenCode hook calls into normalized lifecycle events.
func (*OpenCodeAgent) PrepareTranscript ¶
func (a *OpenCodeAgent) PrepareTranscript(sessionRef string) error
PrepareTranscript ensures the OpenCode transcript file is up-to-date by calling `opencode export`. OpenCode's transcript is created/updated via `opencode export`, but condensation may need fresh data mid-turn (e.g., during mid-turn commits or resumed sessions where the cached file is stale). This method always refreshes the transcript to ensure the latest agent activity is captured.
func (*OpenCodeAgent) ProtectedDirs ¶
func (a *OpenCodeAgent) ProtectedDirs() []string
func (*OpenCodeAgent) ReadSession ¶
func (a *OpenCodeAgent) ReadSession(input *agent.HookInput) (*agent.AgentSession, error)
func (*OpenCodeAgent) ReadTranscript ¶
func (a *OpenCodeAgent) ReadTranscript(sessionRef string) ([]byte, error)
ReadTranscript reads the transcript for a session. The sessionRef is expected to be a path to the export JSON file.
func (*OpenCodeAgent) ReassembleTranscript ¶
func (a *OpenCodeAgent) ReassembleTranscript(chunks [][]byte) ([]byte, error)
ReassembleTranscript merges OpenCode export JSON chunks by combining their message arrays.
func (*OpenCodeAgent) ResolveSessionFile ¶
func (a *OpenCodeAgent) ResolveSessionFile(sessionDir, agentSessionID string) string
func (*OpenCodeAgent) Type ¶
func (a *OpenCodeAgent) Type() agent.AgentType
func (*OpenCodeAgent) UninstallHooks ¶
func (a *OpenCodeAgent) UninstallHooks() error
UninstallHooks removes the Entire plugin file.
func (*OpenCodeAgent) WriteSession ¶
func (a *OpenCodeAgent) WriteSession(session *agent.AgentSession) error
type Part ¶
type Part struct {
Type string `json:"type"` // "text", "tool", etc.
Text string `json:"text,omitempty"`
Tool string `json:"tool,omitempty"`
CallID string `json:"callID,omitempty"`
State *ToolState `json:"state,omitempty"`
}
Part represents a message part (text, tool, etc.).
type SessionInfo ¶
type SessionInfo struct {
ID string `json:"id"`
Title string `json:"title,omitempty"`
CreatedAt int64 `json:"createdAt,omitempty"`
UpdatedAt int64 `json:"updatedAt,omitempty"`
}
SessionInfo contains session metadata from the export.