opencode

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package opencode registers MCP tools that delegate work to an opencode server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(s *mcp.Server, client *Client, mgr *Manager)

Register adds opencode handoff tools to an MCP server.

Types

type Agent

type Agent struct {
	Name        string `json:"name"`
	Description string `json:"description,omitempty"`
	Mode        string `json:"mode,omitempty"`
}

type AgentsResult

type AgentsResult struct {
	Agents []Agent `json:"agents"`
}

type Client

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

Client calls opencode HTTP API endpoints via the generated client.

func NewClient

func NewClient(cfg Config, timeout time.Duration) (*Client, error)

NewClient creates an opencode API client.

func (*Client) Agents

func (c *Client) Agents(ctx context.Context, loc Location) ([]Agent, error)

func (*Client) BaseURL

func (c *Client) BaseURL() string

func (*Client) Context

func (c *Client) Context(ctx context.Context, _ Location, sessionID string) (json.RawMessage, error)

func (*Client) CreateSession

func (c *Client) CreateSession(ctx context.Context, loc Location, req CreateSessionRequest) (Session, error)

func (*Client) Health

func (c *Client) Health(ctx context.Context) (json.RawMessage, error)

func (*Client) Messages

func (c *Client) Messages(ctx context.Context, _ Location, sessionID string) (json.RawMessage, error)

Messages returns the raw message array for a session using the v1 GET /session/{id}/message endpoint. The v2 /api/session/{id}/message endpoint returns only event stream items (e.g. agent-switched) and does not include user/assistant message text.

func (*Client) PermissionReply

func (c *Client) PermissionReply(ctx context.Context, _ Location, sessionID, requestID, reply, message string) (json.RawMessage, error)

func (*Client) Permissions

func (c *Client) Permissions(ctx context.Context, loc Location, _ string) (json.RawMessage, error)

func (*Client) Prompt

func (c *Client) Prompt(ctx context.Context, loc Location, sessionID string, req PromptRequest) (json.RawMessage, error)

func (*Client) ProvidersAndModels

func (c *Client) ProvidersAndModels(ctx context.Context, loc Location) (ModelsResult, error)

func (*Client) QuestionReply

func (c *Client) QuestionReply(ctx context.Context, _ Location, sessionID, requestID string, reject bool, answers [][]string) (json.RawMessage, error)

func (*Client) Questions

func (c *Client) Questions(ctx context.Context, loc Location, _ string) (json.RawMessage, error)

func (*Client) SessionPermissionRequests

func (c *Client) SessionPermissionRequests(ctx context.Context, _ Location, sessionID string) ([]map[string]any, error)

func (*Client) SessionQuestionRequests

func (c *Client) SessionQuestionRequests(ctx context.Context, loc Location, sessionID string) ([]map[string]any, error)

func (*Client) Sessions

func (c *Client) Sessions(ctx context.Context, req SessionsRequest) (SessionsResult, error)

func (*Client) SyncTimeout

func (c *Client) SyncTimeout() time.Duration

func (*Client) Wait

func (c *Client) Wait(ctx context.Context, _ Location, sessionID string) error

type Config

type Config struct {
	BaseURL          string
	Username         string
	Password         string
	DefaultDirectory string
	// SyncTimeout is the timeout for blocking prompt calls (session message POST).
	// If zero, the general request timeout is used.
	SyncTimeout time.Duration
	// APILogger, when non-nil, logs every HTTP request and response body at
	// debug level. Useful for debugging opencode API calls.
	APILogger *slog.Logger
}

Config contains connection settings for an opencode HTTP server.

type CreateSessionRequest

type CreateSessionRequest struct {
	Title      string `json:"title,omitempty"`
	ParentID   string `json:"parentID,omitempty"`
	ProviderID string `json:"providerID,omitempty"`
	ModelID    string `json:"modelID,omitempty"`
}

type CreateSessionResult

type CreateSessionResult struct {
	SessionID string `json:"session_id"`
	Title     string `json:"title,omitempty"`
}

type HandoffCheckResult

type HandoffCheckResult struct {
	SessionID          string           `json:"session_id"`
	Status             string           `json:"status,omitempty"`
	FinalText          string           `json:"final_text,omitempty"`
	PendingPermissions []RequestSummary `json:"pending_permissions,omitempty"`
	PendingQuestions   []RequestSummary `json:"pending_questions,omitempty"`
	JobError           string           `json:"job_error,omitempty"`
	Errors             []string         `json:"errors,omitempty"`
	Messages           []MessageSummary `json:"messages,omitempty"`
}

type HandoffFireResult

type HandoffFireResult struct {
	SessionID          string           `json:"session_id"`
	PromptMessageID    string           `json:"prompt_message_id,omitempty"`
	Message            string           `json:"message,omitempty"`
	PendingPermissions []RequestSummary `json:"pending_permissions,omitempty"`
	PendingQuestions   []RequestSummary `json:"pending_questions,omitempty"`
	Errors             []string         `json:"errors,omitempty"`
}

type HealthResult

type HealthResult struct {
	OK      bool            `json:"ok"`
	BaseURL string          `json:"base_url"`
	Data    json.RawMessage `json:"-"`
	Message string          `json:"message,omitempty"`
}

type Job

type Job struct {
	SessionID       string    `json:"session_id"`
	Status          JobStatus `json:"status"`
	PromptMessageID string    `json:"prompt_message_id,omitempty"`
	Err             error     `json:"-"`
	CreatedAt       time.Time `json:"created_at"`
	UpdatedAt       time.Time `json:"updated_at"`
	// contains filtered or unexported fields
}

type JobStatus

type JobStatus string
const (
	JobRunning JobStatus = "running"
	JobDone    JobStatus = "done"
	JobError   JobStatus = "error"
	// JobUnknown means the server restarted while the job was running;
	// the actual outcome must be determined by querying opencode.
	JobUnknown JobStatus = "unknown"
)

type Location

type Location struct {
	Directory string `json:"directory,omitempty" jsonschema:"Project directory to run opencode in. Overrides the server default directory."`
	Workspace string `json:"workspace,omitempty" jsonschema:"Optional opencode workspace identifier."`
}

type Manager

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

func NewManager

func NewManager(ctx context.Context, client *Client, opts ManagerOptions) *Manager

func (*Manager) Job

func (m *Manager) Job(sessionID string) (*Job, bool)

func (*Manager) Jobs

func (m *Manager) Jobs() []Job

func (*Manager) Submit

func (m *Manager) Submit(_ context.Context, loc Location, sessionID string, req PromptRequest) (string, error)

type ManagerOptions

type ManagerOptions struct {
	Logger   *slog.Logger
	StateDir string
}

type MessageSummary

type MessageSummary struct {
	ID   string `json:"id,omitempty"`
	Role string `json:"role,omitempty"`
	Text string `json:"text,omitempty"`
}

type ModelSummary

type ModelSummary struct {
	ProviderID string `json:"provider_id,omitempty"`
	ID         string `json:"id"`
	Name       string `json:"name,omitempty"`
}

type ModelsResult

type ModelsResult struct {
	Providers []ProviderSummary `json:"providers,omitempty"`
	Models    []ModelSummary    `json:"models,omitempty"`
}

type PermissionReplyResult

type PermissionReplyResult struct {
	OK                 bool             `json:"ok"`
	Data               json.RawMessage  `json:"-"`
	PendingPermissions []RequestSummary `json:"pending_permissions,omitempty"`
	PendingQuestions   []RequestSummary `json:"pending_questions,omitempty"`
	Errors             []string         `json:"errors,omitempty"`
}

type PromptPayload

type PromptPayload struct {
	Text string `json:"text"`
}

type PromptRequest

type PromptRequest struct {
	Prompt PromptPayload `json:"prompt"`
	Agent  string        `json:"agent,omitempty"`
}

type ProviderSummary

type ProviderSummary struct {
	ID     string `json:"id"`
	Name   string `json:"name,omitempty"`
	Models int    `json:"models,omitempty"`
}

type QuestionReplyResult

type QuestionReplyResult struct {
	OK                 bool             `json:"ok"`
	Data               json.RawMessage  `json:"-"`
	PendingPermissions []RequestSummary `json:"pending_permissions,omitempty"`
	PendingQuestions   []RequestSummary `json:"pending_questions,omitempty"`
	Errors             []string         `json:"errors,omitempty"`
}

type RequestSummary

type RequestSummary struct {
	Kind    string `json:"kind,omitempty"`
	Title   string `json:"title,omitempty"`
	Text    string `json:"text,omitempty"`
	Preview string `json:"preview,omitempty"`
}

type RequestsResult

type RequestsResult struct {
	Requests []RequestSummary `json:"requests"`
	Count    int              `json:"count"`
}

type Session

type Session struct {
	ID        string          `json:"id"`
	Title     string          `json:"title,omitempty"`
	ParentID  string          `json:"parent_id,omitempty"`
	CreatedAt int64           `json:"created_at,omitempty"`
	UpdatedAt int64           `json:"updated_at,omitempty"`
	Raw       json.RawMessage `json:"-"`
}

type SessionsRequest

type SessionsRequest struct {
	Location
}

type SessionsResult

type SessionsResult struct {
	Sessions []Session `json:"sessions"`
}

Directories

Path Synopsis
Code generated by ogen, DO NOT EDIT.
Code generated by ogen, DO NOT EDIT.

Jump to

Keyboard shortcuts

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