Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package chat_completions
import (
"bytes"
"context"
"encoding/json"
"fmt"
"strings"
"sync/atomic"
Expand Down Expand Up @@ -181,14 +182,23 @@ func ConvertAntigravityResponseToOpenAI(_ context.Context, _ string, originalReq
mimeType = "image/png"
}
imageURL := fmt.Sprintf("data:%s;base64,%s", mimeType, data)
imagePayload := `{"image_url":{"url":""},"type":"image_url"}`
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
imagesResult := gjson.Get(template, "choices.0.delta.images")
if !imagesResult.Exists() || !imagesResult.IsArray() {
template, _ = sjson.SetRaw(template, "choices.0.delta.images", `[]`)
}
imageIndex := len(gjson.Get(template, "choices.0.delta.images").Array())
imagePayload, err := json.Marshal(map[string]any{
"index": imageIndex,
"type": "image_url",
"image_url": map[string]string{
"url": imageURL,
},
})
if err != nil {
continue
}
template, _ = sjson.Set(template, "choices.0.delta.role", "assistant")
template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", imagePayload)
template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", string(imagePayload))
Comment on lines +189 to +201

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This block of code for creating and appending an image payload is duplicated in other response translator files (gemini-cli_openai_response.go, gemini_openai_response.go). To improve maintainability and reduce redundancy, consider extracting this logic into a single, reusable helper function in a shared package.

For better type safety and readability, it's also recommended to use a struct for JSON marshaling instead of a map[string]any.

Finally, it would be beneficial to log the error from json.Marshal to aid in debugging, as it's currently being silently ignored.

				imageIndex := len(gjson.Get(template, "choices.0.delta.images").Array())

				type imagePayload struct {
					Index    int               `json:"index"`
					Type     string            `json:"type"`
					ImageURL map[string]string `json:"image_url"`
				}

				payload := imagePayload{
					Index: imageIndex,
					Type:  "image_url",
					ImageURL: map[string]string{
						"url": imageURL,
					},
				}

				imagePayloadBytes, err := json.Marshal(payload)
				if err != nil {
					log.Warnf("antigravity openai response: failed to marshal image payload: %v", err)
					continue
				}
				template, _ = sjson.Set(template, "choices.0.delta.role", "assistant")
				template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", string(imagePayloadBytes))

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package chat_completions
import (
"bytes"
"context"
"encoding/json"
"fmt"
"strings"
"sync/atomic"
Expand Down Expand Up @@ -170,14 +171,23 @@ func ConvertCliResponseToOpenAI(_ context.Context, _ string, originalRequestRawJ
mimeType = "image/png"
}
imageURL := fmt.Sprintf("data:%s;base64,%s", mimeType, data)
imagePayload := `{"image_url":{"url":""},"type":"image_url"}`
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
imagesResult := gjson.Get(template, "choices.0.delta.images")
if !imagesResult.Exists() || !imagesResult.IsArray() {
template, _ = sjson.SetRaw(template, "choices.0.delta.images", `[]`)
}
imageIndex := len(gjson.Get(template, "choices.0.delta.images").Array())
imagePayload, err := json.Marshal(map[string]any{
"index": imageIndex,
"type": "image_url",
"image_url": map[string]string{
"url": imageURL,
},
})
if err != nil {
continue
}
template, _ = sjson.Set(template, "choices.0.delta.role", "assistant")
template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", imagePayload)
template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", string(imagePayload))
Comment on lines +178 to +190

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This block of code for creating and appending an image payload is duplicated in other response translator files (antigravity_openai_response.go, gemini_openai_response.go). To improve maintainability and reduce redundancy, consider extracting this logic into a single, reusable helper function in a shared package.

Additionally, for better type safety and readability, it's recommended to use a struct for JSON marshaling instead of a map[string]any.

				imageIndex := len(gjson.Get(template, "choices.0.delta.images").Array())

				type imagePayload struct {
					Index    int               `json:"index"`
					Type     string            `json:"type"`
					ImageURL map[string]string `json:"image_url"`
				}

				payload := imagePayload{
					Index: imageIndex,
					Type:  "image_url",
					ImageURL: map[string]string{
						"url": imageURL,
					},
				}

				imagePayloadBytes, err := json.Marshal(payload)
				if err != nil {
					continue
				}
				template, _ = sjson.Set(template, "choices.0.delta.role", "assistant")
				template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", string(imagePayloadBytes))

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package chat_completions
import (
"bytes"
"context"
"encoding/json"
"fmt"
"strings"
"sync/atomic"
Expand Down Expand Up @@ -182,14 +183,23 @@ func ConvertGeminiResponseToOpenAI(_ context.Context, _ string, originalRequestR
mimeType = "image/png"
}
imageURL := fmt.Sprintf("data:%s;base64,%s", mimeType, data)
imagePayload := `{"image_url":{"url":""},"type":"image_url"}`
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
imagesResult := gjson.Get(template, "choices.0.delta.images")
if !imagesResult.Exists() || !imagesResult.IsArray() {
template, _ = sjson.SetRaw(template, "choices.0.delta.images", `[]`)
}
imageIndex := len(gjson.Get(template, "choices.0.delta.images").Array())
imagePayload, err := json.Marshal(map[string]any{
"index": imageIndex,
"type": "image_url",
"image_url": map[string]string{
"url": imageURL,
},
})
if err != nil {
continue
}
template, _ = sjson.Set(template, "choices.0.delta.role", "assistant")
template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", imagePayload)
template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", string(imagePayload))
Comment on lines +190 to +202

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This block of code for creating and appending an image payload is duplicated elsewhere in this file and in other response translator files. To improve maintainability and reduce redundancy, consider extracting this logic into a single, reusable helper function.

For better type safety and readability, it's also recommended to use a struct for JSON marshaling instead of a map[string]any.

Finally, it would be beneficial to log the error from json.Marshal to aid in debugging.

				imageIndex := len(gjson.Get(template, "choices.0.delta.images").Array())

				type imagePayload struct {
					Index    int               `json:"index"`
					Type     string            `json:"type"`
					ImageURL map[string]string `json:"image_url"`
				}

				payload := imagePayload{
					Index: imageIndex,
					Type:  "image_url",
					ImageURL: map[string]string{
						"url": imageURL,
					},
				}

				imagePayloadBytes, err := json.Marshal(payload)
				if err != nil {
					log.Warnf("gemini openai response: failed to marshal image payload in streaming: %v", err)
					continue
				}
				template, _ = sjson.Set(template, "choices.0.delta.role", "assistant")
				template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", string(imagePayloadBytes))

}
}
}
Expand Down Expand Up @@ -316,14 +326,23 @@ func ConvertGeminiResponseToOpenAINonStream(_ context.Context, _ string, origina
mimeType = "image/png"
}
imageURL := fmt.Sprintf("data:%s;base64,%s", mimeType, data)
imagePayload := `{"image_url":{"url":""},"type":"image_url"}`
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
imagesResult := gjson.Get(template, "choices.0.message.images")
if !imagesResult.Exists() || !imagesResult.IsArray() {
template, _ = sjson.SetRaw(template, "choices.0.message.images", `[]`)
}
imageIndex := len(gjson.Get(template, "choices.0.message.images").Array())
imagePayload, err := json.Marshal(map[string]any{
"index": imageIndex,
"type": "image_url",
"image_url": map[string]string{
"url": imageURL,
},
})
if err != nil {
continue
}
template, _ = sjson.Set(template, "choices.0.message.role", "assistant")
template, _ = sjson.SetRaw(template, "choices.0.message.images.-1", imagePayload)
template, _ = sjson.SetRaw(template, "choices.0.message.images.-1", string(imagePayload))
Comment on lines +333 to +345

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This block of code for creating and appending an image payload is duplicated elsewhere in this file and in other response translator files. To improve maintainability and reduce redundancy, consider extracting this logic into a single, reusable helper function.

For better type safety and readability, it's also recommended to use a struct for JSON marshaling instead of a map[string]any.

Finally, it would be beneficial to log the error from json.Marshal to aid in debugging.

				imageIndex := len(gjson.Get(template, "choices.0.message.images").Array())

				type imagePayload struct {
					Index    int               `json:"index"`
					Type     string            `json:"type"`
					ImageURL map[string]string `json:"image_url"`
				}

				payload := imagePayload{
					Index: imageIndex,
					Type:  "image_url",
					ImageURL: map[string]string{
						"url": imageURL,
					},
				}

				imagePayloadBytes, err := json.Marshal(payload)
				if err != nil {
					log.Warnf("gemini openai response: failed to marshal image payload in non-streaming: %v", err)
					continue
				}
				template, _ = sjson.Set(template, "choices.0.message.role", "assistant")
				template, _ = sjson.SetRaw(template, "choices.0.message.images.-1", string(imagePayloadBytes))

}
}
}
Expand Down
Loading