refactor(mcp) (#1042)

* improve(interface): replace with interface
* feat(mcp): 添加构建器模式支持
新增功能:
- RequestBuilder 构建器,支持流式 API
- 多轮对话支持(AddAssistantMessage)
- Function Calling / Tools 支持
- 精细参数控制(temperature, top_p, penalties 等)
- 3个预设场景(Chat, CodeGen, CreativeWriting)
- 完整的测试套件(19个新测试)
修复问题:
- Config 字段未使用(MaxRetries、Temperature 等)
- DeepSeek/Qwen SetAPIKey 的冗余 nil 检查
向后兼容:
- 保留 CallWithMessages API
- 新增 CallWithRequest API
测试:
- 81 个测试全部通过
- 覆盖率 80.6%
Co-Authored-By: tinkle-community <tinklefund@gmail.com>
---------
Co-authored-by: zbhan <zbhan@freewheel.tv>
Co-authored-by: tinkle-community <tinklefund@gmail.com>
This commit is contained in:
Shui
2025-11-15 23:04:53 -05:00
committed by GitHub
parent 36fcad03c5
commit b60383f22b
22 changed files with 6144 additions and 142 deletions
+69
View File
@@ -0,0 +1,69 @@
package mcp
import (
"net/http"
"os"
"strconv"
"time"
)
// Config 客户端配置(集中管理所有配置)
type Config struct {
// Provider 配置
Provider string
APIKey string
BaseURL string
Model string
// 行为配置
MaxTokens int
Temperature float64
UseFullURL bool
// 重试配置
MaxRetries int
RetryWaitBase time.Duration
RetryableErrors []string
// 超时配置
Timeout time.Duration
// 依赖注入
Logger Logger
HTTPClient *http.Client
}
// DefaultConfig 返回默认配置
func DefaultConfig() *Config {
return &Config{
// 默认值
MaxTokens: getEnvInt("AI_MAX_TOKENS", 2000),
Temperature: MCPClientTemperature,
MaxRetries: MaxRetryTimes,
RetryWaitBase: 2 * time.Second,
Timeout: DefaultTimeout,
RetryableErrors: retryableErrors,
// 默认依赖
Logger: &defaultLogger{},
HTTPClient: &http.Client{Timeout: DefaultTimeout},
}
}
// getEnvInt 从环境变量读取整数,失败则返回默认值
func getEnvInt(key string, defaultValue int) int {
if val := os.Getenv(key); val != "" {
if parsed, err := strconv.Atoi(val); err == nil && parsed > 0 {
return parsed
}
}
return defaultValue
}
// getEnvString 从环境变量读取字符串,为空则返回默认值
func getEnvString(key string, defaultValue string) string {
if val := os.Getenv(key); val != "" {
return val
}
return defaultValue
}