opencode

package
v0.1.25 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2026 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Overview

Package opencode adapts the OpenCode CLI to the agent Runtime contract. 使用 opencode serve 模式,通过 HTTP REST API + SSE 进行通信。

Package opencode provides the OpenCode Runtime type.

Package opencode adapts the OpenCode CLI to the agent Runtime contract. 使用 opencode serve 模式,通过 HTTP REST API + SSE 进行通信。

Index

Constants

View Source
const (
	// Kind is the canonical runtime kind for OpenCode.
	Kind = agent.RuntimeKindOpenCode
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Adapter

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

Adapter 通过 OpenCode CLI serve 模式执行提示。

func NewAdapter

func NewAdapter(binary string, extraEnv map[string]string, dataDir string) *Adapter

NewAdapter 创建 OpenCode Runtime 适配器(serve 模式)。

func (*Adapter) GetSkillDir

func (a *Adapter) GetSkillDir() string

GetSkillDir 返回 OpenCode CLI 的技能目录路径。

func (*Adapter) Invoke

func (a *Adapter) Invoke(ctx context.Context, req cli.InvocationRequest) (*cli.Invocation, error)

Invoke starts OpenCode serve and returns its process activity stream.

func (*Adapter) Prepare

func (a *Adapter) Prepare(_ context.Context, _ string) error

Prepare performs provider-specific workspace setup.

type OpenCodeServer

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

OpenCodeServer 管理 opcode serve 子进程并通过 HTTP 与之通信。

func (*OpenCodeServer) Abort

func (s *OpenCodeServer) Abort(ctx context.Context, sessionID string) error

Abort 中断正在运行的会话。

func (*OpenCodeServer) ConnectSSE

func (s *OpenCodeServer) ConnectSSE(ctx context.Context, workDir string) (<-chan sseEvent, error)

ConnectSSE 连接到 /event SSE 端点,返回解析后的事件通道。 事件按 directory 过滤以确保只接收当前工作区的事件。

func (*OpenCodeServer) CreateSession

func (s *OpenCodeServer) CreateSession(ctx context.Context, title, providerID, modelID, systemPrompt string) (*sessionResponse, error)

CreateSession 创建新的 OpenCode 会话。

func (*OpenCodeServer) GetSession added in v0.1.18

func (s *OpenCodeServer) GetSession(ctx context.Context, sessionID string) (*sessionResponse, error)

GetSession retrieves session metadata required to resume plan handoff handling.

func (*OpenCodeServer) PID

func (s *OpenCodeServer) PID() int

PID 返回子进程的 PID。

func (*OpenCodeServer) SendMessage

func (s *OpenCodeServer) SendMessage(ctx context.Context, sessionID string, req messageRequest) (*messageResponse, error)

SendMessage 向指定会话发送消息并同步等待完整响应。 注意:openCode 的 /session/:id/message 是同步端点,会等待模型完整生成, 可能耗时数分钟甚至更长,因此不使用带超时的 httpClient,而是完全依赖 context 控制生命周期。

func (*OpenCodeServer) SendPermissionDecision

func (s *OpenCodeServer) SendPermissionDecision(ctx context.Context, permissionID, decision string) error

SendPermissionDecision 响应权限请求。

func (*OpenCodeServer) SendQuestionAnswer

func (s *OpenCodeServer) SendQuestionAnswer(ctx context.Context, questionID string, answers [][]string) error

SendQuestionAnswer 响应 question 请求,发送用户选择的答案。

func (*OpenCodeServer) Stop

func (s *OpenCodeServer) Stop() error

Stop 优雅地终止 opencode 子进程。

关闭流程:

  1. 取消管道读取 goroutine(pipeCancel)
  2. 关闭 done channel(通知外部观察者)
  3. 发送 SIGTERM 请求子进程自行清理
  4. 等待 gracefulShutdownTimeout
  5. 超时则强制 SIGKILL
  6. 从唯一的 waitCh 等待进程回收

Stop 是幂等的,多次调用安全。

type Runtime

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

Runtime executes requests through OpenCode.

func New

func New(
	binary string,
	options agent.RuntimeAdapterOptions,
	dataDir string,
) (*Runtime, error)

New creates an OpenCode Runtime backed by the configured CLI binary.

func NewWithInvoker

func NewWithInvoker(invoker cli.Invoker, options agent.RuntimeAdapterOptions) (*Runtime, error)

NewWithInvoker creates an OpenCode Runtime with an injected process invoker.

func (*Runtime) Execute

func (r *Runtime) Execute(
	ctx context.Context,
	request agent.ExecutionRequest,
	observer agent.NodeObserver,
) (agent.ExecutionResult, error)

func (*Runtime) Name

func (r *Runtime) Name() string

type ServerInvoker

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

============================================================================ ServerInvoker — opencode serve 模式的调用器 ============================================================================ ServerInvoker 通过 opcode serve HTTP API 执行提示。

func NewServerInvoker

func NewServerInvoker(binary string, extraEnv map[string]string, dataDir string) *ServerInvoker

NewServerInvoker 创建新的 ServerInvoker。

func (*ServerInvoker) Invoke

Run 启动 opcode serve,创建会话并执行提示。

Jump to

Keyboard shortcuts

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