Documentation
¶
Index ¶
- type Client
- type Event
- type EventType
- type HostTool
- type HostToolCall
- type HostToolPermission
- type HostToolResult
- type InputMessage
- type MessageSnapshot
- type Options
- type PermissionDecision
- type PermissionHandler
- type PermissionRequest
- type Role
- type RunRequest
- type RunResult
- type Session
- type StreamHandler
- type ToolCall
- type ToolResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func (*Client) CreateSession ¶
func (*Client) SendMessageStream ¶
func (c *Client) SendMessageStream(ctx context.Context, req RunRequest, handler StreamHandler) (*RunResult, error)
type EventType ¶
type EventType string
const ( EventSessionCreated EventType = "session_created" EventMessageCreated EventType = "message_created" EventMessageUpdated EventType = "message_updated" EventAssistantTextDelta EventType = "assistant_text_delta" EventAssistantThinkDelta EventType = "assistant_thinking_delta" EventAgentCompleted EventType = "agent_completed" EventAgentError EventType = "agent_error" EventAgentCanceled EventType = "agent_canceled" EventAgentSummaryProgress EventType = "agent_summary_progress" )
type HostTool ¶
type HostTool struct {
Name string
Description string
Parameters map[string]any
Required []string
Handler func(ctx context.Context, call HostToolCall) (HostToolResult, error)
// Permission 可选:声明该宿主工具的授权动作。为空则不触发权限门禁。
Permission *HostToolPermission
}
HostTool 是宿主注入的自定义工具:暴露给模型,并在 opencode 的内置 tool-call 循环中执行。
Parameters / Required 与 opencode 内置工具(tools.ToolInfo)保持同一种组织方式: Parameters 是 JSON Schema 的 properties 映射,Required 是必填字段名列表。
type HostToolCall ¶
HostToolCall 是传给宿主工具 Handler 的一次调用。
type HostToolPermission ¶
type HostToolPermission struct {
Action string // execute / write / read / fetch / delete ...
PathFrom string // 可选:从入参 JSON 中取文件路径的字段名(如 "file_path")
Description string // 可选:权限描述;为空时回退为 "Run tool: <name>"
}
HostToolPermission 声明宿主工具的授权动作;配置后该工具在执行前会走 permission.Service.Request(与 opencode 内置工具同一套权限门禁)。
未配置(nil)表示内部只读工具,不触发权限请求。
type HostToolResult ¶
type HostToolResult struct {
Content string // 回传给模型的文本
IsError bool // 是否为失败结果(模型据此纠正行为)
Metadata any // 可选的结构化元信息
}
HostToolResult 是宿主工具 Handler 返回的结果。
type InputMessage ¶
InputMessage 是种子历史中的一条对话消息。 注意:opencode 的 provider 仅渲染 user / assistant / tool 三种历史角色, system 请通过 RunRequest.System 传递。
type MessageSnapshot ¶
type Options ¶
type Options struct {
WorkingDir string
Debug bool
// Provider 是厂商类型,决定走哪条 SDK 客户端(openai / anthropic / gemini / deepseek …)。
Provider string
// BaseURL 是厂商 API 基地址(OpenAI 兼容协议生效),为空则用内置默认地址。
BaseURL string
// APIKey 是本次请求使用的凭据。
APIKey string
// APIModel 是发给厂商的真实模型名;为空时回退为 ModelID。
APIModel string
// ModelID 是模型标识:类库内部用它作为模型 key 与日志/回显。
ModelID string
// Name 是模型展示名;为空时回退 ModelID。
Name string
// ContextWindow 模型上下文窗口(token 数);0 表示未知。
ContextWindow int64
// DefaultMaxTokens 模型默认最大输出 token;0 表示未知。
DefaultMaxTokens int64
// MaxTokens 本次请求的最大输出 token,覆盖 DefaultMaxTokens。
MaxTokens int64
// CanReason 表示模型支持推理 / 思考过程(决定是否下发 reasoning effort)。
CanReason bool
// SupportsAttachments 表示模型支持图片等附件。
SupportsAttachments bool
// ReasoningEffort 推理强度(openai 系 low / medium / high)。
ReasoningEffort string
// CostPer1MIn / CostPer1MOut 每百万输入 / 输出 token 价格(美元)。
// 仅用于会话费用统计(Session.Cost 与 TUI 展示),不参与请求。
CostPer1MIn float64
CostPer1MOut float64
// CostPer1MInCached / CostPer1MOutCached 命中缓存的输入 / 输出价格。
CostPer1MInCached float64
CostPer1MOutCached float64
AutoApprove bool
// PermissionHandler 可选:注入后,工具权限请求逐次委托给宿主决策。
PermissionHandler PermissionHandler
// Tools 可选:宿主注入的自定义工具,追加到 opencode 内置工具之后,与内置工具
// 共用同一套 tool-call 循环(模型可调用,opencode 负责回填结果)。
Tools []HostTool
}
Options 描述「一次请求」所需的全部模型参数。
作为类库使用时,调用方只需给出本次请求用到的模型与凭据, 不再需要传入整张 provider / agent 配置表(已移除 Providers / Agents 字段)。
type PermissionDecision ¶
type PermissionDecision int
PermissionDecision 是权限委托返回的决策结果。
const ( // PermissionDeny 拒绝(默认值,安全兜底)。 PermissionDeny PermissionDecision = iota // PermissionAllow 允许执行。 PermissionAllow )
type PermissionHandler ¶
type PermissionHandler func(ctx context.Context, req PermissionRequest) PermissionDecision
PermissionHandler 由宿主注入,用于把工具权限请求委托给外部策略 / UI。
clientOpts.PermissionHandler = func(ctx context.Context, req sdk.PermissionRequest) sdk.PermissionDecision {
op := toOperation(req) // 转成上层的中立 Operation
decision, err := rt.RequestPermission(ctx, userID, op)
if err != nil || decision != agent.DecisionAllow {
return sdk.PermissionDeny
}
return sdk.PermissionAllow
}
配置后,SDK 不再阻塞等待命令行交互,而是逐次调用该委托决策。
type PermissionRequest ¶
type PermissionRequest struct {
SessionID string
ToolName string
Action string // execute / write / read / fetch ...
Path string
Description string
Params any
}
PermissionRequest 是一次待决策的权限请求(面向宿主的公开视图)。
type RunRequest ¶
type RunResult ¶
type RunResult struct {
SessionID string
Message MessageSnapshot
}
type StreamHandler ¶
type StreamHandler func(event Event)