Documentation
¶
Overview ¶
MockMemoryManager 的测试模拟实现。
用于消息存储、检索与状态验证相关测试。
MockProvider 的 LLM 提供商测试模拟实现。
支持固定响应、流式输出与错误注入场景。
MockToolManager 的工具管理测试模拟实现。
支持工具注册、调用与错误场景测试。
Index ¶
- type GenerateRequest
- type GenerateResponse
- type GenerateStreamChunk
- type MockMemoryManager
- func (m *MockMemoryManager) Add(ctx context.Context, msg types.Message) error
- func (m *MockMemoryManager) AddBatch(ctx context.Context, msgs []types.Message) error
- func (m *MockMemoryManager) Clear(ctx context.Context) error
- func (m *MockMemoryManager) Count() int
- func (m *MockMemoryManager) GetAddCalls() int
- func (m *MockMemoryManager) GetAll(ctx context.Context) ([]types.Message, error)
- func (m *MockMemoryManager) GetClearCalls() int
- func (m *MockMemoryManager) GetGetCalls() int
- func (m *MockMemoryManager) GetRecent(ctx context.Context, n int) ([]types.Message, error)
- func (m *MockMemoryManager) GetSearchCalls() int
- func (m *MockMemoryManager) Reset()
- func (m *MockMemoryManager) Search(ctx context.Context, query string, topK int) ([]types.Message, error)
- func (m *MockMemoryManager) WithAddError(err error) *MockMemoryManager
- func (m *MockMemoryManager) WithClearError(err error) *MockMemoryManager
- func (m *MockMemoryManager) WithGetError(err error) *MockMemoryManager
- func (m *MockMemoryManager) WithMaxMessages(max int) *MockMemoryManager
- func (m *MockMemoryManager) WithMessages(messages []types.Message) *MockMemoryManager
- func (m *MockMemoryManager) WithSearchError(err error) *MockMemoryManager
- func (m *MockMemoryManager) WithSearchResults(results []types.Message) *MockMemoryManager
- func (m *MockMemoryManager) WithTokenLimit(limit int) *MockMemoryManager
- type MockProvider
- func NewErrorProvider(err error) *MockProvider
- func NewFlakeyProvider(failAfter int, response string) *MockProvider
- func NewMockProvider() *MockProvider
- func NewStreamProvider(chunks []string) *MockProvider
- func NewSuccessProvider(response string) *MockProvider
- func NewToolCallProvider(toolCalls []types.ToolCall) *MockProvider
- func (m *MockProvider) Completion(ctx context.Context, req *llm.ChatRequest) (*llm.ChatResponse, error)
- func (m *MockProvider) Endpoints() llm.ProviderEndpoints
- func (m *MockProvider) Generate(ctx context.Context, req *GenerateRequest) (*GenerateResponse, error)
- func (m *MockProvider) GetCallCount() int
- func (m *MockProvider) GetCalls() []MockProviderCall
- func (m *MockProvider) GetLastCall() *MockProviderCall
- func (m *MockProvider) HealthCheck(ctx context.Context) (*llm.HealthStatus, error)
- func (m *MockProvider) ListModels(ctx context.Context) ([]llm.Model, error)
- func (m *MockProvider) Name() string
- func (m *MockProvider) Reset()
- func (m *MockProvider) Stream(ctx context.Context, req *llm.ChatRequest) (<-chan llm.StreamChunk, error)
- func (m *MockProvider) StreamGenerate(ctx context.Context, req *GenerateRequest) (<-chan GenerateStreamChunk, error)
- func (m *MockProvider) SupportsNativeFunctionCalling() bool
- func (m *MockProvider) WithCompletionFunc(fn func(ctx context.Context, req *llm.ChatRequest) (*llm.ChatResponse, error)) *MockProvider
- func (m *MockProvider) WithDelay(ms int) *MockProvider
- func (m *MockProvider) WithError(err error) *MockProvider
- func (m *MockProvider) WithFailAfter(n int) *MockProvider
- func (m *MockProvider) WithGenerateFunc(fn func(ctx context.Context, req *GenerateRequest) (*GenerateResponse, error)) *MockProvider
- func (m *MockProvider) WithResponse(response string) *MockProvider
- func (m *MockProvider) WithStreamChunks(chunks []string) *MockProvider
- func (m *MockProvider) WithStreamFunc(...) *MockProvider
- func (m *MockProvider) WithTokenUsage(prompt, completion int) *MockProvider
- func (m *MockProvider) WithToolCalls(toolCalls []types.ToolCall) *MockProvider
- type MockProviderCall
- type MockToolManager
- func (m *MockToolManager) Clear()
- func (m *MockToolManager) Execute(ctx context.Context, name string, args map[string]any) (any, error)
- func (m *MockToolManager) ExecuteToolCall(ctx context.Context, tc types.ToolCall) (any, error)
- func (m *MockToolManager) Get(name string) (types.ToolSchema, bool)
- func (m *MockToolManager) GetCallCount() int
- func (m *MockToolManager) GetCalls() []ToolCall
- func (m *MockToolManager) GetCallsForTool(name string) []ToolCall
- func (m *MockToolManager) GetLastCall() *ToolCall
- func (m *MockToolManager) HasTool(name string) bool
- func (m *MockToolManager) List() []types.ToolSchema
- func (m *MockToolManager) Register(tool types.ToolSchema) error
- func (m *MockToolManager) Reset()
- func (m *MockToolManager) Unregister(name string) error
- func (m *MockToolManager) WithDefaultError(err error) *MockToolManager
- func (m *MockToolManager) WithDefaultResult(result any) *MockToolManager
- func (m *MockToolManager) WithTool(name string, fn ToolFunc) *MockToolManager
- func (m *MockToolManager) WithToolDefinition(tool types.ToolSchema) *MockToolManager
- func (m *MockToolManager) WithToolError(name string, err error) *MockToolManager
- func (m *MockToolManager) WithToolResult(name string, result any) *MockToolManager
- type ToolCall
- type ToolFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type GenerateRequest ¶ added in v1.0.0
type GenerateRequest struct {
Messages []types.Message `json:"messages"`
Model string `json:"model,omitempty"`
Tools []types.ToolSchema `json:"tools,omitempty"`
Stream bool `json:"stream,omitempty"`
}
GenerateRequest 是 E2E 测试使用的简化请求类型。 它将被内部转换为 llm.ChatRequest。
type GenerateResponse ¶ added in v1.0.0
type GenerateResponse struct {
Content string `json:"content"`
ToolCalls []types.ToolCall `json:"tool_calls,omitempty"`
}
GenerateResponse 是 E2E 测试使用的简化响应类型。 它从 llm.ChatResponse 中提取关键字段。
type GenerateStreamChunk ¶ added in v1.0.0
type GenerateStreamChunk struct {
Content string `json:"content"`
FinishReason string `json:"finish_reason,omitempty"`
}
GenerateStreamChunk 是 E2E 测试使用的简化流式块类型。
type MockMemoryManager ¶
type MockMemoryManager struct {
// contains filtered or unexported fields
}
MockMemoryManager 是记忆管理器的模拟实现
func NewErrorMemory ¶
func NewErrorMemory(err error) *MockMemoryManager
NewErrorMemory 创建总是返回错误的记忆管理器
func NewLimitedMemory ¶
func NewLimitedMemory(maxMessages int) *MockMemoryManager
NewLimitedMemory 创建有限制的记忆管理器
func NewMockMemoryManager ¶
func NewMockMemoryManager() *MockMemoryManager
NewMockMemoryManager 创建新的 MockMemoryManager
func NewPrefilledMemory ¶
func NewPrefilledMemory(messages []types.Message) *MockMemoryManager
NewPrefilledMemory 创建预填充消息的记忆管理器
func (*MockMemoryManager) Clear ¶
func (m *MockMemoryManager) Clear(ctx context.Context) error
Clear 清空记忆
func (*MockMemoryManager) GetAddCalls ¶
func (m *MockMemoryManager) GetAddCalls() int
GetAddCalls 获取 Add 调用次数
func (*MockMemoryManager) GetClearCalls ¶
func (m *MockMemoryManager) GetClearCalls() int
GetClearCalls 获取 Clear 调用次数
func (*MockMemoryManager) GetGetCalls ¶
func (m *MockMemoryManager) GetGetCalls() int
GetGetCalls 获取 Get 调用次数
func (*MockMemoryManager) GetSearchCalls ¶
func (m *MockMemoryManager) GetSearchCalls() int
GetSearchCalls 获取 Search 调用次数
func (*MockMemoryManager) Search ¶
func (m *MockMemoryManager) Search(ctx context.Context, query string, topK int) ([]types.Message, error)
Search 搜索相关消息(向量搜索模拟)
func (*MockMemoryManager) WithAddError ¶
func (m *MockMemoryManager) WithAddError(err error) *MockMemoryManager
WithAddError 设置 Add 方法的错误
func (*MockMemoryManager) WithClearError ¶
func (m *MockMemoryManager) WithClearError(err error) *MockMemoryManager
WithClearError 设置 Clear 方法的错误
func (*MockMemoryManager) WithGetError ¶
func (m *MockMemoryManager) WithGetError(err error) *MockMemoryManager
WithGetError 设置 Get 方法的错误
func (*MockMemoryManager) WithMaxMessages ¶
func (m *MockMemoryManager) WithMaxMessages(max int) *MockMemoryManager
WithMaxMessages 设置最大消息数
func (*MockMemoryManager) WithMessages ¶
func (m *MockMemoryManager) WithMessages(messages []types.Message) *MockMemoryManager
WithMessages 预设消息
func (*MockMemoryManager) WithSearchError ¶
func (m *MockMemoryManager) WithSearchError(err error) *MockMemoryManager
WithSearchError 设置 Search 方法的错误
func (*MockMemoryManager) WithSearchResults ¶
func (m *MockMemoryManager) WithSearchResults(results []types.Message) *MockMemoryManager
WithSearchResults 设置搜索结果
func (*MockMemoryManager) WithTokenLimit ¶
func (m *MockMemoryManager) WithTokenLimit(limit int) *MockMemoryManager
WithTokenLimit 设置 Token 限制
type MockProvider ¶
type MockProvider struct {
// contains filtered or unexported fields
}
MockProvider 是 LLM Provider 的模拟实现
func NewErrorProvider ¶
func NewErrorProvider(err error) *MockProvider
NewErrorProvider 创建总是失败的 Provider
func NewFlakeyProvider ¶
func NewFlakeyProvider(failAfter int, response string) *MockProvider
NewFlakeyProvider 创建不稳定的 Provider(间歇性失败)
func NewStreamProvider ¶
func NewStreamProvider(chunks []string) *MockProvider
NewStreamProvider 创建流式响应的 Provider
func NewSuccessProvider ¶
func NewSuccessProvider(response string) *MockProvider
NewSuccessProvider 创建总是成功的 Provider
func NewToolCallProvider ¶
func NewToolCallProvider(toolCalls []types.ToolCall) *MockProvider
NewToolCallProvider 创建返回工具调用的 Provider
func (*MockProvider) Completion ¶
func (m *MockProvider) Completion(ctx context.Context, req *llm.ChatRequest) (*llm.ChatResponse, error)
Completion 生成响应
func (*MockProvider) Endpoints ¶ added in v1.0.0
func (m *MockProvider) Endpoints() llm.ProviderEndpoints
Endpoints 返回 Provider 使用的 API 端点信息
func (*MockProvider) Generate ¶ added in v1.0.0
func (m *MockProvider) Generate(ctx context.Context, req *GenerateRequest) (*GenerateResponse, error)
Generate 是 E2E 测试使用的简化生成接口。 内部将 GenerateRequest 转换为 llm.ChatRequest 并调用 Completion。
func (*MockProvider) GetCalls ¶
func (m *MockProvider) GetCalls() []MockProviderCall
GetCalls 获取所有调用记录
func (*MockProvider) GetLastCall ¶
func (m *MockProvider) GetLastCall() *MockProviderCall
GetLastCall 获取最后一次调用
func (*MockProvider) HealthCheck ¶
func (m *MockProvider) HealthCheck(ctx context.Context) (*llm.HealthStatus, error)
HealthCheck 执行健康检查
func (*MockProvider) ListModels ¶
ListModels 返回可用模型列表
func (*MockProvider) Stream ¶
func (m *MockProvider) Stream(ctx context.Context, req *llm.ChatRequest) (<-chan llm.StreamChunk, error)
Stream 流式生成响应
func (*MockProvider) StreamGenerate ¶ added in v1.0.0
func (m *MockProvider) StreamGenerate(ctx context.Context, req *GenerateRequest) (<-chan GenerateStreamChunk, error)
StreamGenerate 是 E2E 测试使用的简化流式接口。 内部将 GenerateRequest 转换为 llm.ChatRequest 并调用 Stream, 返回简化的 GenerateStreamChunk channel。
func (*MockProvider) SupportsNativeFunctionCalling ¶
func (m *MockProvider) SupportsNativeFunctionCalling() bool
SupportsNativeFunctionCalling 返回是否支持原生函数调用
func (*MockProvider) WithCompletionFunc ¶
func (m *MockProvider) WithCompletionFunc(fn func(ctx context.Context, req *llm.ChatRequest) (*llm.ChatResponse, error)) *MockProvider
WithCompletionFunc 设置自定义 Completion 函数
func (*MockProvider) WithDelay ¶
func (m *MockProvider) WithDelay(ms int) *MockProvider
WithDelay 设置响应延迟(毫秒)
func (*MockProvider) WithError ¶
func (m *MockProvider) WithError(err error) *MockProvider
WithError 设置返回错误
func (*MockProvider) WithFailAfter ¶
func (m *MockProvider) WithFailAfter(n int) *MockProvider
WithFailAfter 设置在第 N 次调用后失败
func (*MockProvider) WithGenerateFunc ¶ added in v1.0.0
func (m *MockProvider) WithGenerateFunc(fn func(ctx context.Context, req *GenerateRequest) (*GenerateResponse, error)) *MockProvider
WithGenerateFunc 设置自定义 Generate 函数(E2E 测试用简化接口)
func (*MockProvider) WithResponse ¶
func (m *MockProvider) WithResponse(response string) *MockProvider
WithResponse 设置固定响应内容
func (*MockProvider) WithStreamChunks ¶
func (m *MockProvider) WithStreamChunks(chunks []string) *MockProvider
WithStreamChunks 设置流式响应块
func (*MockProvider) WithStreamFunc ¶
func (m *MockProvider) WithStreamFunc(fn func(ctx context.Context, req *llm.ChatRequest) (<-chan llm.StreamChunk, error)) *MockProvider
WithStreamFunc 设置自定义 Stream 函数
func (*MockProvider) WithTokenUsage ¶
func (m *MockProvider) WithTokenUsage(prompt, completion int) *MockProvider
WithTokenUsage 设置 Token 使用量
func (*MockProvider) WithToolCalls ¶
func (m *MockProvider) WithToolCalls(toolCalls []types.ToolCall) *MockProvider
WithToolCalls 设置工具调用响应
type MockProviderCall ¶
type MockProviderCall struct {
Request *llm.ChatRequest
Response *llm.ChatResponse
Error error
}
MockProviderCall 记录单次调用
type MockToolManager ¶
type MockToolManager struct {
// contains filtered or unexported fields
}
MockToolManager 是工具管理器的模拟实现
func NewCalculatorToolManager ¶
func NewCalculatorToolManager() *MockToolManager
NewCalculatorToolManager 创建带计算器工具的管理器
func NewEmptyToolManager ¶
func NewEmptyToolManager() *MockToolManager
NewEmptyToolManager 创建空的工具管理器
func NewErrorToolManager ¶
func NewErrorToolManager(err error) *MockToolManager
NewErrorToolManager 创建总是返回错误的工具管理器
func NewMockToolManager ¶
func NewMockToolManager() *MockToolManager
NewMockToolManager 创建新的 MockToolManager
func NewSearchToolManager ¶
func NewSearchToolManager(results []string) *MockToolManager
NewSearchToolManager 创建带搜索工具的管理器
func (*MockToolManager) Execute ¶
func (m *MockToolManager) Execute(ctx context.Context, name string, args map[string]any) (any, error)
Execute 执行工具
func (*MockToolManager) ExecuteToolCall ¶
ExecuteToolCall 执行 ToolCall 结构
func (*MockToolManager) Get ¶
func (m *MockToolManager) Get(name string) (types.ToolSchema, bool)
Get 获取工具定义
func (*MockToolManager) GetCallCount ¶
func (m *MockToolManager) GetCallCount() int
GetCallCount 获取调用次数
func (*MockToolManager) GetCalls ¶
func (m *MockToolManager) GetCalls() []ToolCall
GetCalls 获取所有调用记录
func (*MockToolManager) GetCallsForTool ¶
func (m *MockToolManager) GetCallsForTool(name string) []ToolCall
GetCallsForTool 获取特定工具的调用记录
func (*MockToolManager) GetLastCall ¶
func (m *MockToolManager) GetLastCall() *ToolCall
GetLastCall 获取最后一次调用
func (*MockToolManager) HasTool ¶
func (m *MockToolManager) HasTool(name string) bool
HasTool 检查工具是否存在
func (*MockToolManager) Register ¶
func (m *MockToolManager) Register(tool types.ToolSchema) error
Register 注册工具
func (*MockToolManager) Unregister ¶
func (m *MockToolManager) Unregister(name string) error
Unregister 注销工具
func (*MockToolManager) WithDefaultError ¶
func (m *MockToolManager) WithDefaultError(err error) *MockToolManager
WithDefaultError 设置默认返回错误
func (*MockToolManager) WithDefaultResult ¶
func (m *MockToolManager) WithDefaultResult(result any) *MockToolManager
WithDefaultResult 设置默认返回结果
func (*MockToolManager) WithTool ¶
func (m *MockToolManager) WithTool(name string, fn ToolFunc) *MockToolManager
WithTool 注册工具及其执行函数
func (*MockToolManager) WithToolDefinition ¶
func (m *MockToolManager) WithToolDefinition(tool types.ToolSchema) *MockToolManager
WithToolDefinition 注册工具定义
func (*MockToolManager) WithToolError ¶
func (m *MockToolManager) WithToolError(name string, err error) *MockToolManager
WithToolError 设置工具的固定返回错误
func (*MockToolManager) WithToolResult ¶
func (m *MockToolManager) WithToolResult(name string, result any) *MockToolManager
WithToolResult 设置工具的固定返回结果