types

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LoopState

type LoopState struct {
	Input    string
	Messages []Message

	Tools *ToolRegistry

	Iteration     int
	MaxIterations int

	// PendingToolCalls 是本轮模型响应中待执行的调用,由引擎填充。
	PendingToolCalls []ToolCall

	LastResponse *ModelResponse
	Usage        Usage

	// Metadata 供 hook 之间共享任意数据。
	// 并发契约:除 OnToolStart / OnToolEnd(跨调用并发)外,引擎串行执行
	// hook 回调;这两个回调内写 Metadata 须自行加锁。
	Metadata map[string]any

	// Emitter 由引擎注入:hook 通过它发送自定义事件到 OnEvent 回调。
	// hook 回调由引擎串行调用,此路径无并发;OnToolStart / OnToolEnd
	// 跨调用并发,其中发事件须遵守 OnEvent 的并发契约(快速返回、并发安全)。
	Emitter event.OnEvent

	// Stop 置 true 后,当前节点收尾完毕即终止 loop。
	Stop       bool
	StopReason StopReason

	StartedAt time.Time
	EndedAt   time.Time
}

LoopState 是贯穿整个 loop 的统一结构体,hook 可读写任意字段。 context.Context 按 Go 惯例作为独立参数传递,不放入 State。

func (*LoopState) AppendMessage

func (s *LoopState) AppendMessage(m Message)

func (*LoopState) EmitEvent

func (s *LoopState) EmitEvent(typ event.EventType, data any)

EmitEvent 发送自定义事件(自动补全时间戳与迭代号)。 事件类型建议加命名空间前缀,如 "approve.denied",避免与引擎事件冲突。 Emitter 未注入时为空操作。

type Message

type Message struct {
	Role       Role       `json:"role"`
	Content    string     `json:"content"`
	ToolCalls  []ToolCall `json:"tool_calls,omitempty"`
	ToolCallID string     `json:"tool_call_id,omitempty"` // RoleTool 消息关联的调用 ID
	Err        string     `json:"err,omitempty"`          // 工具执行失败时的错误文本
}

type ModelChunk

type ModelChunk struct {
	ContentDelta string
}

ModelChunk 是流式输出的增量文本。

type ModelRequest

type ModelRequest struct {
	Messages []Message
	Tools    []Tool
}

ModelRequest 是引擎调用 Provider 时的完整输入。

type ModelResponse

type ModelResponse struct {
	Content   string
	ToolCalls []ToolCall
	Usage     Usage
}

ModelResponse 是模型单次响应:纯文本,或附带 tool calls 触发下一轮循环。

type Role

type Role string
const (
	RoleSystem    Role = "system"
	RoleUser      Role = "user"
	RoleAssistant Role = "assistant"
	RoleTool      Role = "tool"
)

type StopReason

type StopReason string
const (
	StopCompleted    StopReason = "completed"
	StopMaxIteration StopReason = "max_iterations"
	StopAborted      StopReason = "aborted"
	StopError        StopReason = "error"
	StopCancelled    StopReason = "cancelled"
)

type Tool

type Tool interface {
	Name() string
	Description() string
	ArgsSchema() json.RawMessage
	Invoke(ctx context.Context, args json.RawMessage) (string, error)
}

type ToolCall

type ToolCall struct {
	ID   string
	Name string
	Args json.RawMessage
}

type ToolRegistry

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

func NewToolRegistry

func NewToolRegistry(tools ...Tool) *ToolRegistry

func (*ToolRegistry) List

func (r *ToolRegistry) List() []Tool

func (*ToolRegistry) Lookup

func (r *ToolRegistry) Lookup(name string) (Tool, error)

func (*ToolRegistry) Register

func (r *ToolRegistry) Register(t Tool)

func (*ToolRegistry) SetWarp

func (r *ToolRegistry) SetWarp(warp func(Tool) Tool)

SetWarp 设置注册拦截:之后所有 Register 的工具都会被包装 (包括 hook 在运行时注入的工具,如 mcp router)。已注册的不受影响。

type ToolResult

type ToolResult struct {
	CallID  string
	Name    string
	Content string
	Err     error
}

type Usage

type Usage struct {
	PromptTokens     int
	CompletionTokens int
}

func (*Usage) Add

func (u *Usage) Add(other Usage)

Jump to

Keyboard shortcuts

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