fix(openai): 修复生图服务 lint 问题
- 移除不安全的类型断言用法 - 补齐响应体关闭与字符串拼接的 lint 问题 - 按 staticcheck 建议简化选择器与条件表达式
This commit is contained in:
@@ -831,8 +831,8 @@ func (s *OpenAIGatewayService) forwardOpenAIImagesOAuth(
|
|||||||
return nil, fmt.Errorf("openai image conversation request failed: %w", err)
|
return nil, fmt.Errorf("openai image conversation request failed: %w", err)
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
if resp.Response != nil && resp.Response.Body != nil {
|
if resp != nil && resp.Body != nil {
|
||||||
_ = resp.Response.Body.Close()
|
_ = resp.Body.Close()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
if resp.StatusCode >= 400 {
|
if resp.StatusCode >= 400 {
|
||||||
@@ -969,9 +969,9 @@ func bootstrapOpenAIBackendAPI(ctx context.Context, client *req.Client, headers
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if resp.Response != nil && resp.Response.Body != nil {
|
if resp != nil && resp.Body != nil {
|
||||||
_, _ = io.Copy(io.Discard, resp.Response.Body)
|
_, _ = io.Copy(io.Discard, resp.Body)
|
||||||
_ = resp.Response.Body.Close()
|
_ = resp.Body.Close()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -1118,8 +1118,9 @@ func uploadOpenAIImageFiles(ctx context.Context, client *req.Client, headers htt
|
|||||||
results := make([]openAIUploadedImage, 0, len(uploads))
|
results := make([]openAIUploadedImage, 0, len(uploads))
|
||||||
for i := range uploads {
|
for i := range uploads {
|
||||||
item := uploads[i]
|
item := uploads[i]
|
||||||
|
fileName := coalesceOpenAIFileName(item.FileName, "image.png")
|
||||||
payload := map[string]any{
|
payload := map[string]any{
|
||||||
"file_name": coalesceOpenAIFileName(item.FileName, "image.png"),
|
"file_name": fileName,
|
||||||
"file_size": len(item.Data),
|
"file_size": len(item.Data),
|
||||||
"use_case": "multimodal",
|
"use_case": "multimodal",
|
||||||
}
|
}
|
||||||
@@ -1178,7 +1179,7 @@ func uploadOpenAIImageFiles(ctx context.Context, client *req.Client, headers htt
|
|||||||
|
|
||||||
results = append(results, openAIUploadedImage{
|
results = append(results, openAIUploadedImage{
|
||||||
FileID: created.FileID,
|
FileID: created.FileID,
|
||||||
FileName: payload["file_name"].(string),
|
FileName: fileName,
|
||||||
FileSize: len(item.Data),
|
FileSize: len(item.Data),
|
||||||
MimeType: coalesceOpenAIFileName(item.ContentType, "application/octet-stream"),
|
MimeType: coalesceOpenAIFileName(item.ContentType, "application/octet-stream"),
|
||||||
Width: item.Width,
|
Width: item.Width,
|
||||||
@@ -1230,6 +1231,15 @@ func buildOpenAIImageConversationRequest(parsed *OpenAIImagesRequest, parentMess
|
|||||||
if len(uploads) > 0 {
|
if len(uploads) > 0 {
|
||||||
contentType = "multimodal_text"
|
contentType = "multimodal_text"
|
||||||
}
|
}
|
||||||
|
metadata := map[string]any{
|
||||||
|
"developer_mode_connector_ids": []any{},
|
||||||
|
"selected_github_repos": []any{},
|
||||||
|
"selected_all_github_repos": false,
|
||||||
|
"system_hints": []string{"picture_v2"},
|
||||||
|
"serialization_metadata": map[string]any{
|
||||||
|
"custom_symbol_offsets": []any{},
|
||||||
|
},
|
||||||
|
}
|
||||||
message := map[string]any{
|
message := map[string]any{
|
||||||
"id": uuid.NewString(),
|
"id": uuid.NewString(),
|
||||||
"author": map[string]any{"role": "user"},
|
"author": map[string]any{"role": "user"},
|
||||||
@@ -1237,20 +1247,11 @@ func buildOpenAIImageConversationRequest(parsed *OpenAIImagesRequest, parentMess
|
|||||||
"content_type": contentType,
|
"content_type": contentType,
|
||||||
"parts": parts,
|
"parts": parts,
|
||||||
},
|
},
|
||||||
"metadata": map[string]any{
|
"metadata": metadata,
|
||||||
"developer_mode_connector_ids": []any{},
|
|
||||||
"selected_github_repos": []any{},
|
|
||||||
"selected_all_github_repos": false,
|
|
||||||
"system_hints": []string{"picture_v2"},
|
|
||||||
"serialization_metadata": map[string]any{
|
|
||||||
"custom_symbol_offsets": []any{},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"create_time": float64(time.Now().UnixMilli()) / 1000,
|
"create_time": float64(time.Now().UnixMilli()) / 1000,
|
||||||
}
|
}
|
||||||
if len(attachments) > 0 {
|
if len(attachments) > 0 {
|
||||||
messageMetadata := message["metadata"].(map[string]any)
|
metadata["attachments"] = attachments
|
||||||
messageMetadata["attachments"] = attachments
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return map[string]any{
|
return map[string]any{
|
||||||
@@ -1371,7 +1372,10 @@ func openAIImagePointerMatches(body []byte) []string {
|
|||||||
end := idx + len(prefix)
|
end := idx + len(prefix)
|
||||||
for end < len(raw) {
|
for end < len(raw) {
|
||||||
ch := raw[end]
|
ch := raw[end]
|
||||||
if !(ch == '-' || ch == '_' || ch >= '0' && ch <= '9' || ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') {
|
if ch != '-' && ch != '_' &&
|
||||||
|
(ch < '0' || ch > '9') &&
|
||||||
|
(ch < 'a' || ch > 'z') &&
|
||||||
|
(ch < 'A' || ch > 'Z') {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
end++
|
end++
|
||||||
@@ -1695,14 +1699,14 @@ func downloadOpenAIImageBytes(ctx context.Context, client *req.Client, headers h
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
if resp.Response != nil && resp.Response.Body != nil {
|
if resp != nil && resp.Body != nil {
|
||||||
_ = resp.Response.Body.Close()
|
_ = resp.Body.Close()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
return nil, newOpenAIImageStatusError(resp, "download image bytes failed")
|
return nil, newOpenAIImageStatusError(resp, "download image bytes failed")
|
||||||
}
|
}
|
||||||
return io.ReadAll(resp.Response.Body)
|
return io.ReadAll(resp.Body)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleOpenAIImageBackendError(resp *req.Response) error {
|
func handleOpenAIImageBackendError(resp *req.Response) error {
|
||||||
@@ -1936,16 +1940,8 @@ func generateOpenAIChallengeAnswer(seed string, difficulty string, config []any)
|
|||||||
seedBytes := []byte(seed)
|
seedBytes := []byte(seed)
|
||||||
|
|
||||||
for i := 0; i < 100000; i++ {
|
for i := 0; i < 100000; i++ {
|
||||||
var builder bytes.Buffer
|
payload := fmt.Sprintf("%s%d,%s,%d,%s", p1, i, p2, i>>1, p3)
|
||||||
builder.Write(p1)
|
encoded := base64.StdEncoding.EncodeToString([]byte(payload))
|
||||||
builder.WriteString(strconv.Itoa(i))
|
|
||||||
builder.WriteString(",")
|
|
||||||
builder.Write(p2)
|
|
||||||
builder.WriteString(",")
|
|
||||||
builder.WriteString(strconv.Itoa(i >> 1))
|
|
||||||
builder.WriteString(",")
|
|
||||||
builder.Write(p3)
|
|
||||||
encoded := base64.StdEncoding.EncodeToString(builder.Bytes())
|
|
||||||
sum := sha3.Sum512(append(seedBytes, []byte(encoded)...))
|
sum := sha3.Sum512(append(seedBytes, []byte(encoded)...))
|
||||||
if bytes.Compare(sum[:len(diffBytes)], diffBytes) <= 0 {
|
if bytes.Compare(sum[:len(diffBytes)], diffBytes) <= 0 {
|
||||||
return encoded, true
|
return encoded, true
|
||||||
|
|||||||
Reference in New Issue
Block a user