Skip to content

Commit 794995a

Browse files
committed
refactor: 现代化类型声明,将 interface{} 替换为 any
- 全面替换 384 处 interface{} 为 Go 1.18+ 的 any 类型 - 涵盖所有核心包:parser、converter、types、server、logger - 保持完全向后兼容,无功能变更 - 通过编译验证和静态代码检查 - 提升代码可读性和现代化程度 影响范围: - 结构体字段类型定义 - 函数参数和返回值类型 - 局部变量声明 - 类型断言更新 - 注释文档同步更新
1 parent 2a8ddf0 commit 794995a

20 files changed

+205
-710
lines changed

converter/codewhisperer.go

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -105,29 +105,29 @@ func extractToolResultsFromMessage(content any) []types.ToolResult {
105105

106106
// 提取 content - 转换为数组格式
107107
if content, exists := block["content"]; exists {
108-
// 将 content 转换为 []map[string]interface{} 格式
109-
var contentArray []map[string]interface{}
108+
// 将 content 转换为 []map[string]any 格式
109+
var contentArray []map[string]any
110110

111111
// 处理不同的 content 格式
112112
switch c := content.(type) {
113113
case string:
114114
// 如果是字符串,包装成标准格式
115-
contentArray = []map[string]interface{}{
115+
contentArray = []map[string]any{
116116
{"text": c},
117117
}
118-
case []interface{}:
118+
case []any:
119119
// 如果已经是数组,保持原样
120120
for _, item := range c {
121-
if m, ok := item.(map[string]interface{}); ok {
121+
if m, ok := item.(map[string]any); ok {
122122
contentArray = append(contentArray, m)
123123
}
124124
}
125-
case map[string]interface{}:
125+
case map[string]any:
126126
// 如果是单个对象,包装成数组
127-
contentArray = []map[string]interface{}{c}
127+
contentArray = []map[string]any{c}
128128
default:
129129
// 其他格式,尝试转换为字符串
130-
contentArray = []map[string]interface{}{
130+
contentArray = []map[string]any{
131131
{"text": fmt.Sprintf("%v", c)},
132132
}
133133
}
@@ -163,23 +163,23 @@ func extractToolResultsFromMessage(content any) []types.ToolResult {
163163

164164
// 处理 content
165165
if block.Content != nil {
166-
var contentArray []map[string]interface{}
166+
var contentArray []map[string]any
167167

168168
switch c := block.Content.(type) {
169169
case string:
170-
contentArray = []map[string]interface{}{
170+
contentArray = []map[string]any{
171171
{"text": c},
172172
}
173-
case []interface{}:
173+
case []any:
174174
for _, item := range c {
175-
if m, ok := item.(map[string]interface{}); ok {
175+
if m, ok := item.(map[string]any); ok {
176176
contentArray = append(contentArray, m)
177177
}
178178
}
179-
case map[string]interface{}:
180-
contentArray = []map[string]interface{}{c}
179+
case map[string]any:
180+
contentArray = []map[string]any{c}
181181
default:
182-
contentArray = []map[string]interface{}{
182+
contentArray = []map[string]any{
183183
{"text": fmt.Sprintf("%v", c)},
184184
}
185185
}
@@ -360,21 +360,6 @@ func BuildCodeWhispererRequest(anthropicReq types.AnthropicRequest, profileArn s
360360
}
361361
}
362362

363-
// 如果有工具,添加简化的工具使用系统提示
364-
if len(anthropicReq.Tools) > 0 {
365-
// 使用系统提示生成器创建工具系统提示
366-
spg := NewSystemPromptGenerator()
367-
toolSystemPrompt := spg.GenerateToolSystemPrompt(anthropicReq.ToolChoice, anthropicReq.Tools)
368-
369-
// 只有在有其他系统内容时才添加分隔符
370-
if systemContentBuilder.Len() > 0 && toolSystemPrompt != "" {
371-
systemContentBuilder.WriteString("\n\n")
372-
}
373-
if toolSystemPrompt != "" {
374-
systemContentBuilder.WriteString(toolSystemPrompt)
375-
}
376-
}
377-
378363
// 如果有系统内容,添加到历史记录 (恢复v0.4结构化类型)
379364
if systemContentBuilder.Len() > 0 {
380365
userMsg := types.HistoryUserMessage{}
@@ -492,11 +477,11 @@ func extractToolUsesFromMessage(content any) []types.ToolUseEntry {
492477
}
493478

494479
// 提取 input
495-
if input, ok := block["input"].(map[string]interface{}); ok {
480+
if input, ok := block["input"].(map[string]any); ok {
496481
toolUse.Input = input
497482
} else {
498483
// 如果 input 不是 map 或不存在,设置为空对象
499-
toolUse.Input = map[string]interface{}{}
484+
toolUse.Input = map[string]any{}
500485
}
501486

502487
toolUses = append(toolUses, toolUse)
@@ -523,15 +508,15 @@ func extractToolUsesFromMessage(content any) []types.ToolUseEntry {
523508

524509
if block.Input != nil {
525510
switch inp := (*block.Input).(type) {
526-
case map[string]interface{}:
511+
case map[string]any:
527512
toolUse.Input = inp
528513
default:
529-
toolUse.Input = map[string]interface{}{
514+
toolUse.Input = map[string]any{
530515
"value": inp,
531516
}
532517
}
533518
} else {
534-
toolUse.Input = map[string]interface{}{}
519+
toolUse.Input = map[string]any{}
535520
}
536521

537522
toolUses = append(toolUses, toolUse)

converter/system_prompt.go

Lines changed: 0 additions & 98 deletions
This file was deleted.

logger/logger.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,9 @@ func (l *Logger) log(level Level, msg string, fields []Field) {
188188
for _, field := range fields {
189189
// 跳过重复的系统字段
190190
if field.Key == "level" || field.Key == "log_level" ||
191-
field.Key == "timestamp" || field.Key == "message" ||
192-
field.Key == "file" || field.Key == "log_file" ||
193-
field.Key == "func" {
191+
field.Key == "timestamp" || field.Key == "message" ||
192+
field.Key == "file" || field.Key == "log_file" ||
193+
field.Key == "func" {
194194
continue
195195
}
196196
entry.Fields[field.Key] = field.Value
@@ -235,7 +235,7 @@ func (l *Logger) marshalLogEntry(entry *LogEntry) []byte {
235235
escapedMsg, _ := sonic.MarshalString(entry.Message)
236236
// 移除外层引号
237237
if len(escapedMsg) >= 2 {
238-
b.WriteString(escapedMsg[1:len(escapedMsg)-1])
238+
b.WriteString(escapedMsg[1 : len(escapedMsg)-1])
239239
}
240240
b.WriteString(`"`)
241241

parser/compliant_event_stream_parser.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ func (cesp *CompliantEventStreamParser) generateSummary(messages []*EventStreamM
182182
if eventType == "content_block_start" || eventType == "content_block_stop" ||
183183
eventType == "content_block_delta" {
184184
// 检查是否是工具相关的内容块
185-
if data, ok := event.Data.(map[string]interface{}); ok {
185+
if data, ok := event.Data.(map[string]any); ok {
186186
if contentBlock, exists := data["content_block"]; exists {
187-
if block, ok := contentBlock.(map[string]interface{}); ok {
187+
if block, ok := contentBlock.(map[string]any); ok {
188188
if blockType, ok := block["type"].(string); ok && blockType == "tool_use" {
189189
summary.HasToolCalls = true
190190
}
@@ -229,15 +229,15 @@ type ParseResult struct {
229229

230230
// ParseSummary 解析摘要
231231
type ParseSummary struct {
232-
TotalMessages int `json:"total_messages"`
233-
TotalEvents int `json:"total_events"`
234-
MessageTypes map[string]int `json:"message_types"`
235-
EventTypes map[string]int `json:"event_types"`
236-
HasToolCalls bool `json:"has_tool_calls"`
237-
HasCompletions bool `json:"has_completions"`
238-
HasErrors bool `json:"has_errors"`
239-
HasSessionEvents bool `json:"has_session_events"`
240-
ToolSummary map[string]interface{} `json:"tool_summary"`
232+
TotalMessages int `json:"total_messages"`
233+
TotalEvents int `json:"total_events"`
234+
MessageTypes map[string]int `json:"message_types"`
235+
EventTypes map[string]int `json:"event_types"`
236+
HasToolCalls bool `json:"has_tool_calls"`
237+
HasCompletions bool `json:"has_completions"`
238+
HasErrors bool `json:"has_errors"`
239+
HasSessionEvents bool `json:"has_session_events"`
240+
ToolSummary map[string]any `json:"tool_summary"`
241241
}
242242

243243
// IsComplete 检查解析是否完整
@@ -251,8 +251,8 @@ func (pr *ParseResult) GetCompletionText() string {
251251

252252
for _, event := range pr.Events {
253253
if event.Event == "content_block_delta" {
254-
if data, ok := event.Data.(map[string]interface{}); ok {
255-
if delta, ok := data["delta"].(map[string]interface{}); ok {
254+
if data, ok := event.Data.(map[string]any); ok {
255+
if delta, ok := data["delta"].(map[string]any); ok {
256256
if deltaText, ok := delta["text"].(string); ok {
257257
text += deltaText
258258
}

parser/compliant_message_processor.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ func (cmp *CompliantMessageProcessor) processEventMessage(message *EventStreamMe
169169

170170
// processErrorMessage 处理错误消息
171171
func (cmp *CompliantMessageProcessor) processErrorMessage(message *EventStreamMessage) ([]SSEEvent, error) {
172-
var errorData map[string]interface{}
172+
var errorData map[string]any
173173
if len(message.Payload) > 0 {
174174
if err := utils.FastUnmarshal(message.Payload, &errorData); err != nil {
175175
logger.Warn("解析错误消息载荷失败", logger.Err(err))
176-
errorData = map[string]interface{}{
176+
errorData = map[string]any{
177177
"message": string(message.Payload),
178178
}
179179
}
@@ -194,7 +194,7 @@ func (cmp *CompliantMessageProcessor) processErrorMessage(message *EventStreamMe
194194
return []SSEEvent{
195195
{
196196
Event: "error",
197-
Data: map[string]interface{}{
197+
Data: map[string]any{
198198
"type": "error",
199199
"error_code": errorCode,
200200
"error_message": errorMessage,
@@ -206,11 +206,11 @@ func (cmp *CompliantMessageProcessor) processErrorMessage(message *EventStreamMe
206206

207207
// processExceptionMessage 处理异常消息
208208
func (cmp *CompliantMessageProcessor) processExceptionMessage(message *EventStreamMessage) ([]SSEEvent, error) {
209-
var exceptionData map[string]interface{}
209+
var exceptionData map[string]any
210210
if len(message.Payload) > 0 {
211211
if err := utils.FastUnmarshal(message.Payload, &exceptionData); err != nil {
212212
logger.Warn("解析异常消息载荷失败", logger.Err(err))
213-
exceptionData = map[string]interface{}{
213+
exceptionData = map[string]any{
214214
"message": string(message.Payload),
215215
}
216216
}
@@ -231,7 +231,7 @@ func (cmp *CompliantMessageProcessor) processExceptionMessage(message *EventStre
231231
return []SSEEvent{
232232
{
233233
Event: "exception",
234-
Data: map[string]interface{}{
234+
Data: map[string]any{
235235
"type": "exception",
236236
"exception_type": exceptionType,
237237
"exception_message": exceptionMessage,

0 commit comments

Comments
 (0)