opencode

package
v0.0.0-...-bbf015a Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsAuthError

func IsAuthError(err error) bool

IsAuthError returns true if the error is an auth error.

func NormalizeBaseURL

func NormalizeBaseURL(raw string) (string, error)

NormalizeBaseURL ensures a base URL is valid and has no trailing slash.

Types

type APIError

type APIError struct {
	StatusCode int
	Body       string
}

APIError captures non-2xx responses from the OpenCode server.

func (*APIError) Error

func (e *APIError) Error() string

type Client

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

Client handles HTTP interactions with an OpenCode server.

func NewClient

func NewClient(baseURL, username, password string) (*Client, error)

NewClient constructs an OpenCode client with basic auth if a password is provided.

func (*Client) AbortSession

func (c *Client) AbortSession(ctx context.Context, sessionID string) error

AbortSession aborts a running session.

func (*Client) CreateSession

func (c *Client) CreateSession(ctx context.Context, title, directory string) (*Session, error)

CreateSession creates a new session with an optional title and directory.

func (*Client) DeleteSession

func (c *Client) DeleteSession(ctx context.Context, sessionID string) error

DeleteSession deletes an OpenCode session.

func (*Client) GetMessage

func (c *Client) GetMessage(ctx context.Context, sessionID, messageID string) (*MessageWithParts, error)

GetMessage fetches a single message and its parts.

func (*Client) ListMessages

func (c *Client) ListMessages(ctx context.Context, sessionID string, limit int) ([]MessageWithParts, error)

ListMessages lists recent messages in a session.

func (*Client) ListSessions

func (c *Client) ListSessions(ctx context.Context) ([]Session, error)

ListSessions returns all sessions from the server.

func (*Client) RejectQuestion

func (c *Client) RejectQuestion(ctx context.Context, requestID string) error

func (*Client) ReplyQuestion

func (c *Client) ReplyQuestion(ctx context.Context, requestID string, answers [][]string) error

func (*Client) RespondPermission

func (c *Client) RespondPermission(ctx context.Context, sessionID, permissionID, response string) error

func (*Client) SendMessageAsync

func (c *Client) SendMessageAsync(ctx context.Context, sessionID, messageID string, parts []PartInput) error

SendMessageAsync sends a message to a session asynchronously. The server returns 204 immediately; the assistant response is delivered via SSE.

func (*Client) StreamEvents

func (c *Client) StreamEvents(ctx context.Context) (<-chan Event, <-chan error)

StreamEvents connects to the OpenCode /event SSE endpoint. It returns a channel of events and a channel for errors.

func (*Client) UpdateSessionTitle

func (c *Client) UpdateSessionTitle(ctx context.Context, sessionID, title string) (*Session, error)

UpdateSessionTitle updates the title of an OpenCode session.

type Event

type Event struct {
	Type       string          `json:"type"`
	Properties json.RawMessage `json:"properties"`
}

Event represents a server-sent event from the OpenCode event stream.

func (Event) DecodeInfo

func (e Event) DecodeInfo(target any) error

DecodeInfo decodes the info payload inside an event into the provided target.

type Message

type Message struct {
	ID         string      `json:"id"`
	SessionID  string      `json:"sessionID"`
	Role       string      `json:"role"`
	ParentID   string      `json:"parentID,omitempty"`
	Agent      string      `json:"agent,omitempty"`
	ModelID    string      `json:"modelID,omitempty"`
	ProviderID string      `json:"providerID,omitempty"`
	Mode       string      `json:"mode,omitempty"`
	Finish     string      `json:"finish,omitempty"`
	Cost       float64     `json:"cost,omitempty"`
	Tokens     *TokenUsage `json:"tokens,omitempty"`
	Time       MessageTime `json:"time"`
}

Message represents the info block for a session message.

type MessageTime

type MessageTime struct {
	Created   Timestamp `json:"created"`
	Completed Timestamp `json:"completed,omitempty"`
}

MessageTime holds timing info for a message.

type MessageWithParts

type MessageWithParts struct {
	Info  Message `json:"info"`
	Parts []Part  `json:"parts"`
}

MessageWithParts bundles a message info block with its parts.

type ModelRef

type ModelRef struct {
	ProviderID string `json:"providerID,omitempty"`
	ModelID    string `json:"modelID,omitempty"`
}

ModelRef identifies a provider/model pair.

type Part

type Part struct {
	ID          string          `json:"id"`
	SessionID   string          `json:"sessionID,omitempty"`
	MessageID   string          `json:"messageID,omitempty"`
	Type        string          `json:"type"`
	Text        string          `json:"text,omitempty"`
	Filename    string          `json:"filename,omitempty"`
	URL         string          `json:"url,omitempty"`
	Mime        string          `json:"mime,omitempty"`
	Name        string          `json:"name,omitempty"`
	Prompt      string          `json:"prompt,omitempty"`
	Description string          `json:"description,omitempty"`
	Agent       string          `json:"agent,omitempty"`
	Model       *ModelRef       `json:"model,omitempty"`
	Command     string          `json:"command,omitempty"`
	CallID      string          `json:"callID,omitempty"`
	Tool        string          `json:"tool,omitempty"`
	State       *ToolState      `json:"state,omitempty"`
	Snapshot    string          `json:"snapshot,omitempty"`
	Hash        string          `json:"hash,omitempty"`
	Files       []string        `json:"files,omitempty"`
	Reason      string          `json:"reason,omitempty"`
	Cost        float64         `json:"cost,omitempty"`
	Tokens      *TokenUsage     `json:"tokens,omitempty"`
	Attempt     int             `json:"attempt,omitempty"`
	Auto        bool            `json:"auto,omitempty"`
	Time        *PartTime       `json:"time,omitempty"`
	Metadata    map[string]any  `json:"metadata,omitempty"`
	Error       json.RawMessage `json:"error,omitempty"`
	Source      json.RawMessage `json:"source,omitempty"`
	Extra       map[string]any  `json:"extra,omitempty"`
}

Part represents a message part. Only a subset of fields is used by the bridge.

type PartInput

type PartInput struct {
	ID          string    `json:"id,omitempty"`
	Type        string    `json:"type"`
	Text        string    `json:"text,omitempty"`
	Mime        string    `json:"mime,omitempty"`
	Filename    string    `json:"filename,omitempty"`
	URL         string    `json:"url,omitempty"`
	Name        string    `json:"name,omitempty"`
	Prompt      string    `json:"prompt,omitempty"`
	Description string    `json:"description,omitempty"`
	Agent       string    `json:"agent,omitempty"`
	Model       *ModelRef `json:"model,omitempty"`
	Command     string    `json:"command,omitempty"`
}

PartInput is used to send parts to OpenCode.

type PartTime

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

PartTime represents part timing metadata.

type PermissionRequest

type PermissionRequest struct {
	ID         string          `json:"id"`
	SessionID  string          `json:"sessionID"`
	Permission string          `json:"permission"`
	Patterns   []string        `json:"patterns"`
	Metadata   map[string]any  `json:"metadata"`
	Always     []string        `json:"always"`
	Tool       *RequestToolRef `json:"tool,omitempty"`
}

type QuestionInfo

type QuestionInfo struct {
	Question string           `json:"question"`
	Header   string           `json:"header"`
	Options  []QuestionOption `json:"options"`
	Multiple bool             `json:"multiple,omitempty"`
	Custom   bool             `json:"custom,omitempty"`
}

type QuestionOption

type QuestionOption struct {
	Label       string `json:"label"`
	Description string `json:"description"`
}

type QuestionRequest

type QuestionRequest struct {
	ID        string          `json:"id"`
	SessionID string          `json:"sessionID"`
	Questions []QuestionInfo  `json:"questions"`
	Tool      *RequestToolRef `json:"tool,omitempty"`
}

type RequestToolRef

type RequestToolRef struct {
	MessageID string `json:"messageID,omitempty"`
	CallID    string `json:"callID,omitempty"`
}

type Session

type Session struct {
	ID        string      `json:"id"`
	Slug      string      `json:"slug"`
	ProjectID string      `json:"projectID"`
	Directory string      `json:"directory"`
	ParentID  string      `json:"parentID,omitempty"`
	Title     string      `json:"title"`
	Version   string      `json:"version"`
	Time      SessionTime `json:"time"`
}

Session represents an OpenCode session summary.

type SessionTime

type SessionTime struct {
	Created Timestamp `json:"created"`
	Updated Timestamp `json:"updated"`
}

SessionTime holds session timing metadata.

type Timestamp

type Timestamp int64

Timestamp represents millisecond timestamps returned by the OpenCode API.

func (*Timestamp) UnmarshalJSON

func (t *Timestamp) UnmarshalJSON(data []byte) error

UnmarshalJSON accepts either integer or floating-point JSON numbers.

type TokenCache

type TokenCache struct {
	Read  float64 `json:"read,omitempty"`
	Write float64 `json:"write,omitempty"`
}

TokenCache represents cached token usage.

type TokenUsage

type TokenUsage struct {
	Input     float64     `json:"input,omitempty"`
	Output    float64     `json:"output,omitempty"`
	Reasoning float64     `json:"reasoning,omitempty"`
	Cache     *TokenCache `json:"cache,omitempty"`
}

TokenUsage represents token usage.

type ToolState

type ToolState struct {
	Status      string         `json:"status"`
	Input       map[string]any `json:"input,omitempty"`
	Raw         string         `json:"raw,omitempty"`
	Title       string         `json:"title,omitempty"`
	Metadata    map[string]any `json:"metadata,omitempty"`
	Output      string         `json:"output,omitempty"`
	Error       string         `json:"error,omitempty"`
	Time        *ToolStateTime `json:"time,omitempty"`
	Attachments []Part         `json:"attachments,omitempty"`
}

ToolState captures tool execution state.

type ToolStateTime

type ToolStateTime struct {
	Start     Timestamp `json:"start,omitempty"`
	End       Timestamp `json:"end,omitempty"`
	Compacted Timestamp `json:"compacted,omitempty"`
}

ToolStateTime captures tool state timing.

Jump to

Keyboard shortcuts

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