Add files via upload

This commit is contained in:
公明
2025-11-09 13:30:08 +08:00
committed by GitHub
parent ffd417fe93
commit 1f51c64084
10 changed files with 214 additions and 46 deletions
+18 -12
View File
@@ -17,19 +17,25 @@ import (
// Agent AI代理
type Agent struct {
openAIClient *http.Client
config *config.OpenAIConfig
mcpServer *mcp.Server
logger *zap.Logger
openAIClient *http.Client
config *config.OpenAIConfig
mcpServer *mcp.Server
logger *zap.Logger
maxIterations int
}
// NewAgent 创建新的Agent
func NewAgent(cfg *config.OpenAIConfig, mcpServer *mcp.Server, logger *zap.Logger) *Agent {
func NewAgent(cfg *config.OpenAIConfig, mcpServer *mcp.Server, logger *zap.Logger, maxIterations int) *Agent {
// 如果 maxIterations 为 0 或负数,使用默认值 30
if maxIterations <= 0 {
maxIterations = 30
}
return &Agent{
openAIClient: &http.Client{Timeout: 5 * time.Minute},
config: cfg,
mcpServer: mcpServer,
logger: logger,
openAIClient: &http.Client{Timeout: 5 * time.Minute},
config: cfg,
mcpServer: mcpServer,
logger: logger,
maxIterations: maxIterations,
}
}
@@ -271,7 +277,7 @@ func (a *Agent) AgentLoopWithProgress(ctx context.Context, userInput string, his
MCPExecutionIDs: make([]string, 0),
}
maxIterations := 30
maxIterations := a.maxIterations
for i := 0; i < maxIterations; i++ {
// 检查是否是最后一次迭代
isLastIteration := (i == maxIterations-1)
@@ -527,7 +533,7 @@ func (a *Agent) AgentLoopWithProgress(ctx context.Context, userInput string, his
sendProgress("progress", "达到最大迭代次数,正在生成总结...", nil)
finalSummaryPrompt := ChatMessage{
Role: "user",
Content: "已达到最大迭代次数(30轮)。请总结到目前为止的所有测试结果、发现的问题和已完成的工作。如果需要继续测试,请提供详细的下一步执行计划。请直接回复,不要调用工具。",
Content: fmt.Sprintf("已达到最大迭代次数(%d轮)。请总结到目前为止的所有测试结果、发现的问题和已完成的工作。如果需要继续测试,请提供详细的下一步执行计划。请直接回复,不要调用工具。", a.maxIterations),
}
messages = append(messages, finalSummaryPrompt)
@@ -542,7 +548,7 @@ func (a *Agent) AgentLoopWithProgress(ctx context.Context, userInput string, his
}
// 如果无法生成总结,返回友好的提示
result.Response = "已达到最大迭代次数(30轮)。系统已执行了多轮测试,但由于达到迭代上限,无法继续自动执行。建议您查看已执行的工具结果,或提出新的测试请求以继续测试。"
result.Response = fmt.Sprintf("已达到最大迭代次数(%d轮)。系统已执行了多轮测试,但由于达到迭代上限,无法继续自动执行。建议您查看已执行的工具结果,或提出新的测试请求以继续测试。", a.maxIterations)
return result, nil
}
+5 -1
View File
@@ -63,7 +63,11 @@ func New(cfg *config.Config, log *logger.Logger) (*App, error) {
executor.RegisterTools(mcpServer)
// 创建Agent
agent := agent.NewAgent(&cfg.OpenAI, mcpServer, log.Logger)
maxIterations := cfg.Agent.MaxIterations
if maxIterations <= 0 {
maxIterations = 30 // 默认值
}
agent := agent.NewAgent(&cfg.OpenAI, mcpServer, log.Logger, maxIterations)
// 创建处理器
agentHandler := handler.NewAgentHandler(agent, db, log.Logger)
+8
View File
@@ -14,6 +14,7 @@ type Config struct {
Log LogConfig `yaml:"log"`
MCP MCPConfig `yaml:"mcp"`
OpenAI OpenAIConfig `yaml:"openai"`
Agent AgentConfig `yaml:"agent"`
Security SecurityConfig `yaml:"security"`
Database DatabaseConfig `yaml:"database"`
}
@@ -49,6 +50,10 @@ type DatabaseConfig struct {
Path string `yaml:"path"`
}
type AgentConfig struct {
MaxIterations int `yaml:"max_iterations"`
}
type ToolConfig struct {
Name string `yaml:"name"`
Command string `yaml:"command"`
@@ -200,6 +205,9 @@ func Default() *Config {
BaseURL: "https://api.openai.com/v1",
Model: "gpt-4",
},
Agent: AgentConfig{
MaxIterations: 30, // 默认最大迭代次数
},
Security: SecurityConfig{
Tools: []ToolConfig{}, // 工具配置应该从 config.yaml 或 tools/ 目录加载
ToolsDir: "tools", // 默认工具目录
+21 -1
View File
@@ -256,7 +256,27 @@ func (e *Executor) buildCommandArgs(toolName string, toolConfig *config.ToolConf
// 布尔值特殊处理:如果为 false,跳过;如果为 true,只添加标志
if param.Type == "bool" {
if boolVal, ok := value.(bool); ok {
var boolVal bool
var ok bool
// 尝试多种类型转换
if boolVal, ok = value.(bool); ok {
// 已经是布尔值
} else if numVal, ok := value.(float64); ok {
// JSON 数字类型(float64
boolVal = numVal != 0
ok = true
} else if numVal, ok := value.(int); ok {
// int 类型
boolVal = numVal != 0
ok = true
} else if strVal, ok := value.(string); ok {
// 字符串类型
boolVal = strVal == "true" || strVal == "1" || strVal == "yes"
ok = true
}
if ok {
if !boolVal {
continue // false 时不添加任何参数
}