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
- type Adapter
- type OpenCodeServer
- func (s *OpenCodeServer) Abort(ctx context.Context, sessionID string) error
- func (s *OpenCodeServer) ConnectSSE(ctx context.Context, workDir string) (<-chan sseEvent, error)
- func (s *OpenCodeServer) CreateSession(ctx context.Context, title, providerID, modelID, systemPrompt string) (*sessionResponse, error)
- func (s *OpenCodeServer) GetSession(ctx context.Context, sessionID string) (*sessionResponse, error)
- func (s *OpenCodeServer) PID() int
- func (s *OpenCodeServer) SendMessage(ctx context.Context, sessionID string, req messageRequest) (*messageResponse, error)
- func (s *OpenCodeServer) SendPermissionDecision(ctx context.Context, permissionID, decision string) error
- func (s *OpenCodeServer) SendQuestionAnswer(ctx context.Context, questionID string, answers [][]string) error
- func (s *OpenCodeServer) Stop() error
- type Runtime
- type ServerInvoker
Constants ¶
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 ¶
NewAdapter 创建 OpenCode Runtime 适配器(serve 模式)。
func (*Adapter) GetSkillDir ¶
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.
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) 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 子进程。
关闭流程:
- 取消管道读取 goroutine(pipeCancel)
- 关闭 done channel(通知外部观察者)
- 发送 SIGTERM 请求子进程自行清理
- 等待 gracefulShutdownTimeout
- 超时则强制 SIGKILL
- 从唯一的 waitCh 等待进程回收
Stop 是幂等的,多次调用安全。
type Runtime ¶
type Runtime struct {
// contains filtered or unexported fields
}
Runtime executes requests through OpenCode.
func NewWithInvoker ¶
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)
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 ¶
func (inv *ServerInvoker) Invoke(ctx context.Context, req cli.InvocationRequest) (*cli.Invocation, error)
Run 启动 opcode serve,创建会话并执行提示。