fix(lint): 修复golangci-lint检查发现的问题
修复了4个lint问题: 1. errcheck (3处): 在测试中添加类型断言的ok检查 2. govet copylocks (1处): 将mutex从ToolCorrectionStats移到CodexToolCorrector **详细修改:** 1. **openai_tool_corrector_test.go** - 添加了类型断言的ok检查,避免panic - 在解析JSON后检查payload结构的有效性 - 改进错误处理和测试可靠性 2. **openai_tool_corrector.go** - 将sync.RWMutex从ToolCorrectionStats移到CodexToolCorrector - 避免在GetStats()返回时复制mutex - 保持线程安全的同时符合Go最佳实践 **测试验证:** - 所有单元测试通过 ✅ - go vet 检查通过 ✅ - 代码编译正常 ✅
This commit is contained in:
@@ -29,16 +29,16 @@ var codexToolNameMapping = map[string]string{
|
||||
"execBash": "bash",
|
||||
}
|
||||
|
||||
// ToolCorrectionStats 记录工具修正的统计信息
|
||||
// ToolCorrectionStats 记录工具修正的统计信息(导出用于 JSON 序列化)
|
||||
type ToolCorrectionStats struct {
|
||||
TotalCorrected int `json:"total_corrected"`
|
||||
CorrectionsByTool map[string]int `json:"corrections_by_tool"`
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// CodexToolCorrector 处理 Codex 工具调用的自动修正
|
||||
type CodexToolCorrector struct {
|
||||
stats ToolCorrectionStats
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// NewCodexToolCorrector 创建新的工具修正器
|
||||
@@ -251,8 +251,8 @@ func (c *CodexToolCorrector) correctToolParameters(toolName string, functionCall
|
||||
|
||||
// recordCorrection 记录一次工具名称修正
|
||||
func (c *CodexToolCorrector) recordCorrection(from, to string) {
|
||||
c.stats.mu.Lock()
|
||||
defer c.stats.mu.Unlock()
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
c.stats.TotalCorrected++
|
||||
key := fmt.Sprintf("%s->%s", from, to)
|
||||
@@ -264,8 +264,8 @@ func (c *CodexToolCorrector) recordCorrection(from, to string) {
|
||||
|
||||
// GetStats 获取工具修正统计信息
|
||||
func (c *CodexToolCorrector) GetStats() ToolCorrectionStats {
|
||||
c.stats.mu.RLock()
|
||||
defer c.stats.mu.RUnlock()
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
|
||||
// 返回副本以避免并发问题
|
||||
statsCopy := ToolCorrectionStats{
|
||||
@@ -281,8 +281,8 @@ func (c *CodexToolCorrector) GetStats() ToolCorrectionStats {
|
||||
|
||||
// ResetStats 重置统计信息
|
||||
func (c *CodexToolCorrector) ResetStats() {
|
||||
c.stats.mu.Lock()
|
||||
defer c.stats.mu.Unlock()
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
c.stats.TotalCorrected = 0
|
||||
c.stats.CorrectionsByTool = make(map[string]int)
|
||||
|
||||
@@ -38,8 +38,18 @@ func TestCorrectToolCallsInSSEData(t *testing.T) {
|
||||
if err := json.Unmarshal([]byte(result), &payload); err != nil {
|
||||
t.Fatalf("Failed to parse result: %v", err)
|
||||
}
|
||||
toolCalls := payload["tool_calls"].([]any)
|
||||
functionCall := toolCalls[0].(map[string]any)["function"].(map[string]any)
|
||||
toolCalls, ok := payload["tool_calls"].([]any)
|
||||
if !ok || len(toolCalls) == 0 {
|
||||
t.Fatal("No tool_calls found in result")
|
||||
}
|
||||
toolCall, ok := toolCalls[0].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatal("Invalid tool_call format")
|
||||
}
|
||||
functionCall, ok := toolCall["function"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatal("Invalid function format")
|
||||
}
|
||||
if functionCall["name"] != "edit" {
|
||||
t.Errorf("Expected tool name 'edit', got '%v'", functionCall["name"])
|
||||
}
|
||||
@@ -54,7 +64,10 @@ func TestCorrectToolCallsInSSEData(t *testing.T) {
|
||||
if err := json.Unmarshal([]byte(result), &payload); err != nil {
|
||||
t.Fatalf("Failed to parse result: %v", err)
|
||||
}
|
||||
functionCall := payload["function_call"].(map[string]any)
|
||||
functionCall, ok := payload["function_call"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatal("Invalid function_call format")
|
||||
}
|
||||
if functionCall["name"] != "todowrite" {
|
||||
t.Errorf("Expected tool name 'todowrite', got '%v'", functionCall["name"])
|
||||
}
|
||||
@@ -131,8 +144,18 @@ func TestCorrectToolCallsInSSEData(t *testing.T) {
|
||||
if err := json.Unmarshal([]byte(result), &payload); err != nil {
|
||||
t.Fatalf("Failed to parse result: %v", err)
|
||||
}
|
||||
toolCalls := payload["tool_calls"].([]any)
|
||||
functionCall := toolCalls[0].(map[string]any)["function"].(map[string]any)
|
||||
toolCalls, ok := payload["tool_calls"].([]any)
|
||||
if !ok || len(toolCalls) == 0 {
|
||||
t.Fatal("No tool_calls found in result")
|
||||
}
|
||||
toolCall, ok := toolCalls[0].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatal("Invalid tool_call format")
|
||||
}
|
||||
functionCall, ok := toolCall["function"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatal("Invalid function format")
|
||||
}
|
||||
if functionCall["name"] != "edit" {
|
||||
t.Errorf("Expected tool name 'edit', got '%v'", functionCall["name"])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user