mirror of
https://github.com/githubhjs/CLIProxyAPIPlus.git
synced 2026-07-12 01:25:13 +00:00
fix(kiro): 修复 base64 图片格式转换问题
This commit is contained in:
@@ -4,6 +4,7 @@ package chat_completions
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
"github.com/tidwall/sjson"
|
"github.com/tidwall/sjson"
|
||||||
@@ -182,13 +183,43 @@ func ConvertOpenAIRequestToKiro(modelName string, inputRawJSON []byte, stream bo
|
|||||||
"text": part.Get("text").String(),
|
"text": part.Get("text").String(),
|
||||||
})
|
})
|
||||||
} else if partType == "image_url" {
|
} else if partType == "image_url" {
|
||||||
contentParts = append(contentParts, map[string]interface{}{
|
imageURL := part.Get("image_url.url").String()
|
||||||
"type": "image",
|
|
||||||
"source": map[string]interface{}{
|
// 检查是否是base64格式 (data:image/png;base64,xxxxx)
|
||||||
"type": "url",
|
if strings.HasPrefix(imageURL, "data:") {
|
||||||
"url": part.Get("image_url.url").String(),
|
// 解析 data URL 格式
|
||||||
},
|
// 格式: data:image/png;base64,xxxxx
|
||||||
})
|
commaIdx := strings.Index(imageURL, ",")
|
||||||
|
if commaIdx != -1 {
|
||||||
|
// 提取 media_type (例如 "image/png")
|
||||||
|
header := imageURL[5:commaIdx] // 去掉 "data:" 前缀
|
||||||
|
mediaType := header
|
||||||
|
if semiIdx := strings.Index(header, ";"); semiIdx != -1 {
|
||||||
|
mediaType = header[:semiIdx]
|
||||||
|
}
|
||||||
|
|
||||||
|
// 提取 base64 数据
|
||||||
|
base64Data := imageURL[commaIdx+1:]
|
||||||
|
|
||||||
|
contentParts = append(contentParts, map[string]interface{}{
|
||||||
|
"type": "image",
|
||||||
|
"source": map[string]interface{}{
|
||||||
|
"type": "base64",
|
||||||
|
"media_type": mediaType,
|
||||||
|
"data": base64Data,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 普通URL格式 - 保持原有逻辑
|
||||||
|
contentParts = append(contentParts, map[string]interface{}{
|
||||||
|
"type": "image",
|
||||||
|
"source": map[string]interface{}{
|
||||||
|
"type": "url",
|
||||||
|
"url": imageURL,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if content.String() != "" {
|
} else if content.String() != "" {
|
||||||
@@ -214,13 +245,43 @@ func ConvertOpenAIRequestToKiro(modelName string, inputRawJSON []byte, stream bo
|
|||||||
"text": part.Get("text").String(),
|
"text": part.Get("text").String(),
|
||||||
})
|
})
|
||||||
} else if partType == "image_url" {
|
} else if partType == "image_url" {
|
||||||
contentParts = append(contentParts, map[string]interface{}{
|
imageURL := part.Get("image_url.url").String()
|
||||||
"type": "image",
|
|
||||||
"source": map[string]interface{}{
|
// 检查是否是base64格式 (data:image/png;base64,xxxxx)
|
||||||
"type": "url",
|
if strings.HasPrefix(imageURL, "data:") {
|
||||||
"url": part.Get("image_url.url").String(),
|
// 解析 data URL 格式
|
||||||
},
|
// 格式: data:image/png;base64,xxxxx
|
||||||
})
|
commaIdx := strings.Index(imageURL, ",")
|
||||||
|
if commaIdx != -1 {
|
||||||
|
// 提取 media_type (例如 "image/png")
|
||||||
|
header := imageURL[5:commaIdx] // 去掉 "data:" 前缀
|
||||||
|
mediaType := header
|
||||||
|
if semiIdx := strings.Index(header, ";"); semiIdx != -1 {
|
||||||
|
mediaType = header[:semiIdx]
|
||||||
|
}
|
||||||
|
|
||||||
|
// 提取 base64 数据
|
||||||
|
base64Data := imageURL[commaIdx+1:]
|
||||||
|
|
||||||
|
contentParts = append(contentParts, map[string]interface{}{
|
||||||
|
"type": "image",
|
||||||
|
"source": map[string]interface{}{
|
||||||
|
"type": "base64",
|
||||||
|
"media_type": mediaType,
|
||||||
|
"data": base64Data,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 普通URL格式 - 保持原有逻辑
|
||||||
|
contentParts = append(contentParts, map[string]interface{}{
|
||||||
|
"type": "image",
|
||||||
|
"source": map[string]interface{}{
|
||||||
|
"type": "url",
|
||||||
|
"url": imageURL,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
claudeMsg["content"] = contentParts
|
claudeMsg["content"] = contentParts
|
||||||
|
|||||||
Reference in New Issue
Block a user