opencode

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const CarrierKey = "acp-go-opencode"
View Source
const EventCapacity = 256
View Source
const HistoryPageBytes = 8 << 20

HistoryPageBytes bounds the event data one history read selects, so a long conversation reads as several bounded pages.

View Source
const InternalEnvPrefix = "ACP_GO_OPENCODE_INTERNAL_"
View Source
const MaxBodyBytes = 64 << 20
View Source
const OutputFormatJSONSchema = "json_schema"
View Source
const ServerUsername = "opencode"

ServerUsername is the basic-auth user the adapter configures the native server with and sends on every request.

Variables

This section is empty.

Functions

func AdvancePluginGeneration added in v0.2.0

func AdvancePluginGeneration(root string) error

AdvancePluginGeneration makes the environment plugin forget the session carriers it read, so the next shell in each session reads its carrier again.

func CheckPlugin added in v0.2.0

func CheckPlugin(root, directory string) error

func ConfigDir added in v0.2.0

func ConfigDir(lookup func(string) (string, bool)) string

func DataDir added in v0.2.0

func DataDir(lookup func(string) (string, bool)) string

func DecodeHistory added in v0.2.0

func DecodeHistory(reader io.Reader) ([][]byte, int64, error)

DecodeHistory decodes the native database command's JSON rows into mirror rows one row at a time and returns the bytes of event data they held. The database keeps each event's data as JSON text.

func HomeEnvironment added in v0.2.0

func HomeEnvironment(home string) map[string]string

func IsMissing added in v0.2.0

func IsMissing(err error) bool

func ModelSelectionShapeError added in v0.2.0

func ModelSelectionShapeError(value string) error

func NewID added in v0.2.0

func NewID(prefix string) string

func NewMessageID added in v0.2.0

func NewMessageID() string

NewMessageID uses the native timestamp layout so later CLI messages sort after it.

func ReadHistory added in v0.2.0

func ReadHistory(ctx context.Context, executable, scratchDir string, environment []string, id string, cursors map[string]int64, pageBytes int64) (rows [][]byte, more bool, err error)

ReadHistory reads one page of the sync events of a conversation graph through the native database command: the session id and every descendant, each from after its cursor, or from its start when the cursor map has no entry for it. Events are ordered by sequence and id, so each conversation's events in a page continue its cursor without a gap. A page holds events until their data reaches pageBytes, and always at least one; more reports that the page reached the bound, so later events may remain. It returns each event as its encoded mirror row. The HTTP history route exports every session in the account in one response.

func SessionPath added in v0.2.0

func SessionPath(id string) string

func WritePlugin added in v0.2.0

func WritePlugin(root, config string, ownsHome bool) (string, error)

WritePlugin registers the environment hook alongside the caller's native config.

Types

type Catalog added in v0.2.0

type Catalog struct {
	Providers []ProviderInfo    `json:"providers"`
	Default   map[string]string `json:"default"`
}

type Client

type Client struct {
	URL      string
	Password string
	// contains filtered or unexported fields
}

Client is an authenticated native HTTP endpoint shared by every session.

func NewClient added in v0.2.0

func NewClient() *Client

func (*Client) Args added in v0.2.0

func (c *Client) Args() []string

func (*Client) Do added in v0.2.0

func (c *Client) Do(ctx context.Context, directory, method, path string, body, out any) error

func (*Client) Interrupt added in v0.2.0

func (c *Client) Interrupt(ctx context.Context, dir, id string) error

func (*Client) ReadAddress added in v0.2.0

func (c *Client) ReadAddress(stdout io.Reader) error

ReadAddress reads the owned server's bound loopback address before any HTTP request.

func (*Client) Replay added in v0.2.0

func (c *Client) Replay(ctx context.Context, dir string, events []SyncEvent) error

Replay imports sync events through the native replay route.

func (*Client) Session added in v0.2.0

func (c *Client) Session(ctx context.Context, dir, id string) (NativeSession, error)

func (*Client) Subscribe added in v0.2.0

func (c *Client) Subscribe(ctx context.Context) (*Stream, error)

func (*Client) TurnMessages added in v0.2.0

func (c *Client) TurnMessages(ctx context.Context, dir, id, messageID string) ([]NativeMessage, error)

TurnMessages reads the session's messages from the user message messageID onward, in native order. It pages back from the newest message until a page holds messageID, so the read costs the turn rather than the whole conversation; when the session has no such message it reads every message.

func (*Client) UsageAccess added in v0.2.0

func (c *Client) UsageAccess(ctx context.Context, directory, providerID, modelID string) (usage.Access, error)

UsageAccess resolves the addressed directory's effective credentials and model route.

func (*Client) UsageGateways added in v0.2.0

func (c *Client) UsageGateways(ctx context.Context, directory string, lookup func(string) (string, bool)) ([]gateway.Route, error)

UsageGateways lists the catalog providers configured with their own base URL: the routes opencode sends requests through, each with the key the catalog holds for it, an {env:NAME} key resolved through lookup.

type CommandRequest

type CommandRequest struct {
	MessageID string           `json:"messageID,omitempty"`
	Agent     string           `json:"agent,omitempty"`
	Model     string           `json:"model,omitempty"`
	Variant   string           `json:"variant,omitempty"`
	Command   string           `json:"command"`
	Arguments string           `json:"arguments"`
	Parts     []map[string]any `json:"parts,omitempty"`
}

type Event

type Event struct {
	ID         string          `json:"id"`
	Type       string          `json:"type"`
	Properties json.RawMessage `json:"properties"`
	// SessionID is the native session the event addresses, or empty.
	SessionID string `json:"-"`
}

Event is one native bus event from the global stream.

type HTTPError

type HTTPError struct {
	Status int
	Native NativeError
}

func (*HTTPError) Error

func (e *HTTPError) Error() string

type MessageRequest

type MessageRequest struct {
	MessageID string           `json:"messageID,omitempty"`
	Model     *ModelSelector   `json:"model,omitempty"`
	Agent     string           `json:"agent,omitempty"`
	Variant   string           `json:"variant,omitempty"`
	Parts     []map[string]any `json:"parts"`
	Format    *OutputFormat    `json:"format,omitempty"`
}

type ModelSelector

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

type NativeAgent

type NativeAgent struct {
	Name string `json:"name"`
	Mode string `json:"mode"`
}

type NativeAttachment

type NativeAttachment struct {
	ID       string `json:"id"`
	Type     string `json:"type"`
	Mime     string `json:"mime"`
	Filename string `json:"filename"`
	URL      string `json:"url"`
}

NativeAttachment is a native file part carried inside a completed tool state's attachments array. The URL is a data URL, a file URL/path, or a remote location.

type NativeCommand

type NativeCommand struct {
	Name        string   `json:"name"`
	Description string   `json:"description,omitempty"`
	Hints       []string `json:"hints"`
}

type NativeError

type NativeError struct {
	Type    string `json:"type"`
	Name    string `json:"name"`
	Message string `json:"message"`
	Data    struct {
		Message string `json:"message"`
	} `json:"data"`
}

type NativeMessage

type NativeMessage struct {
	Info  NativeMessageInfo `json:"info"`
	Parts []NativePart      `json:"parts"`
}

type NativeMessageInfo

type NativeMessageInfo struct {
	ID         string          `json:"id"`
	SessionID  string          `json:"sessionID"`
	Role       string          `json:"role"`
	ParentID   string          `json:"parentID"`
	ModelID    string          `json:"modelID"`
	ProviderID string          `json:"providerID"`
	Finish     string          `json:"finish"`
	Summary    json.RawMessage `json:"summary,omitempty"`
	Structured json.RawMessage `json:"structured,omitempty"`
	Error      *NativeError    `json:"error,omitempty"`
	Time       struct {
		Created   int64 `json:"created"`
		Completed int64 `json:"completed"`
	} `json:"time"`
}

func (NativeMessageInfo) CompactionSummary added in v0.2.0

func (i NativeMessageInfo) CompactionSummary() bool

CompactionSummary reports whether an assistant message is a compaction summary. The member is true there; on a user message it holds the message's change summary instead.

type NativePart

type NativePart struct {
	Auto      *bool           `json:"auto,omitempty"`
	ID        string          `json:"id"`
	SessionID string          `json:"sessionID"`
	MessageID string          `json:"messageID"`
	Type      string          `json:"type"`
	Text      string          `json:"text"`
	CallID    string          `json:"callID"`
	Tool      string          `json:"tool"`
	State     json.RawMessage `json:"state"`
	Mime      string          `json:"mime"`
	Filename  string          `json:"filename"`
	URL       string          `json:"url"`
	Tokens    NativeTokens    `json:"tokens"`
}

type NativeSession

type NativeSession struct {
	ParentID  string         `json:"parentID"`
	ID        string         `json:"id"`
	Title     string         `json:"title"`
	Directory string         `json:"directory"`
	Agent     string         `json:"agent"`
	Metadata  map[string]any `json:"metadata"`
	Model     struct {
		ID         string `json:"id"`
		ProviderID string `json:"providerID"`
	} `json:"model"`
	Time struct {
		Created int64 `json:"created"`
		Updated int64 `json:"updated"`
	} `json:"time"`
}

type NativeSessionStatus

type NativeSessionStatus struct {
	Type string `json:"type"`
}

type NativeTodo

type NativeTodo struct {
	Content  string `json:"content"`
	Status   string `json:"status"`
	Priority string `json:"priority"`
}

type NativeTokens

type NativeTokens struct {
	Input     float64 `json:"input"`
	Output    float64 `json:"output"`
	Reasoning float64 `json:"reasoning"`
	Cache     struct {
		Read  float64 `json:"read"`
		Write float64 `json:"write"`
	} `json:"cache"`
}

NativeTokens is one model call's usage. Input excludes the tokens read from and written to the prompt cache, which Cache counts, and Output excludes Reasoning.

type OutputFormat

type OutputFormat struct {
	Type   string         `json:"type"`
	Schema map[string]any `json:"schema"`
}

type PermissionRequest

type PermissionRequest struct {
	ID         string         `json:"id"`
	SessionID  string         `json:"sessionID"`
	Permission string         `json:"permission"`
	Patterns   []string       `json:"patterns"`
	Tool       PermissionTool `json:"tool"`
}

type PermissionTool

type PermissionTool struct {
	MessageID string `json:"messageID"`
	CallID    string `json:"callID"`
}

type ProviderInfo

type ProviderInfo struct {
	ID     string                   `json:"id"`
	Name   string                   `json:"name"`
	Models map[string]ProviderModel `json:"models"`
}

type ProviderModel

type ProviderModel struct {
	ID           string                     `json:"id"`
	Name         string                     `json:"name"`
	Limit        map[string]any             `json:"limit"`
	Capabilities *ProviderModelCapabilities `json:"capabilities"`
	// Variants are the model's named request presets, keyed by the name a
	// prompt selects one with. On a reasoning model the catalog names them by
	// effort level, so the keys are the effort levels the model supports.
	Variants map[string]map[string]any `json:"variants"`
}

type ProviderModelCapabilities

type ProviderModelCapabilities struct {
	Input ProviderModelInputCapabilities `json:"input"`
}

ProviderModelCapabilities is the authenticated provider catalog's exhaustive per-model capability record.

type ProviderModelInputCapabilities

type ProviderModelInputCapabilities struct {
	Image *bool `json:"image"`
}

ProviderModelInputCapabilities reports which input kinds the model accepts. A nil field means the catalog did not state the fact either way.

type QuestionInfo

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

func (QuestionInfo) AllowsCustom added in v0.2.0

func (q QuestionInfo) AllowsCustom() bool

AllowsCustom applies the native question default.

type QuestionOption

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

type QuestionRequest

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

type QuestionTool

type QuestionTool struct {
	MessageID string `json:"messageID"`
	CallID    string `json:"callID"`
}

type Stream added in v0.2.0

type Stream struct {
	Events chan Event
	// contains filtered or unexported fields
}

Stream owns one ordered global SSE connection. Closing it joins its reader.

func (*Stream) Close added in v0.2.0

func (s *Stream) Close()

func (*Stream) Err added in v0.2.0

func (s *Stream) Err() error

type SyncEvent

type SyncEvent struct {
	AggregateID string          `json:"aggregate_id"`
	Data        json.RawMessage `json:"data"`
	ID          string          `json:"id"`
	Sequence    int64           `json:"seq"`
	Type        string          `json:"type"`
}

SyncEvent is one native sync event as the mirror stores it: Data is the event's JSON object. Its encoding is the mirror row: members in name order, data compact.

Jump to

Keyboard shortcuts

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