History

refactor(image): avoid using json.Marshal

TinyCoder committed 10 months ago

Commit a7fc2ee1 parent 671558a3 files · +16 −47Browse files

Unified · Split

3 files
  1. internal/translator/antigravity/openai/chat-completions/antigravity_openai_response.go +4 −12
  2. internal/translator/gemini-cli/openai/chat-completions/gemini-cli_openai_response.go +4 −12
  3. internal/translator/gemini/openai/chat-completions/gemini_openai_response.go +8 −23
internal/translator/antigravity/openai/chat-completions/antigravity_openai_response.go+4−12
@@ -8,7 +8,6 @@ package chat_completions · 7 unmodified lines
8import (8import (
9 "bytes"9 "bytes"
10 "context"10 "context"
11 "encoding/json"
12 "fmt"11 "fmt"
13 "strings"12 "strings"
14 "sync/atomic"13 "sync/atomic"
@@ -187,18 +186,11 @@ func ConvertAntigravityResponseToOpenAI(_ context.Context, _ string, originalReq · 172 unmodified lines
187 template, _ = sjson.SetRaw(template, "choices.0.delta.images", `[]`)186 template, _ = sjson.SetRaw(template, "choices.0.delta.images", `[]`)
188 }187 }
189 imageIndex := len(gjson.Get(template, "choices.0.delta.images").Array())188 imageIndex := len(gjson.Get(template, "choices.0.delta.images").Array())
190 imagePayload, err := json.Marshal(map[string]any{189 imagePayload := `{"type":"image_url","image_url":{"url":""}}`
191 "index": imageIndex,190 imagePayload, _ = sjson.Set(imagePayload, "index", imageIndex)
192 "type": "image_url",191 imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
193 "image_url": map[string]string{
194 "url": imageURL,
195 },
196 })
197 if err != nil {
198 continue
199 }
200 template, _ = sjson.Set(template, "choices.0.delta.role", "assistant")192 template, _ = sjson.Set(template, "choices.0.delta.role", "assistant")
201 template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", string(imagePayload))193 template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", imagePayload)
202 }194 }
203 }195 }
204 }196 }
internal/translator/gemini-cli/openai/chat-completions/gemini-cli_openai_response.go+4−12
@@ -8,7 +8,6 @@ package chat_completions · 7 unmodified lines
8import (8import (
9 "bytes"9 "bytes"
10 "context"10 "context"
11 "encoding/json"
12 "fmt"11 "fmt"
13 "strings"12 "strings"
14 "sync/atomic"13 "sync/atomic"
@@ -176,18 +175,11 @@ func ConvertCliResponseToOpenAI(_ context.Context, _ string, originalRequestRawJ · 161 unmodified lines
176 template, _ = sjson.SetRaw(template, "choices.0.delta.images", `[]`)175 template, _ = sjson.SetRaw(template, "choices.0.delta.images", `[]`)
177 }176 }
178 imageIndex := len(gjson.Get(template, "choices.0.delta.images").Array())177 imageIndex := len(gjson.Get(template, "choices.0.delta.images").Array())
179 imagePayload, err := json.Marshal(map[string]any{178 imagePayload := `{"type":"image_url","image_url":{"url":""}}`
180 "index": imageIndex,179 imagePayload, _ = sjson.Set(imagePayload, "index", imageIndex)
181 "type": "image_url",180 imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
182 "image_url": map[string]string{
183 "url": imageURL,
184 },
185 })
186 if err != nil {
187 continue
188 }
189 template, _ = sjson.Set(template, "choices.0.delta.role", "assistant")181 template, _ = sjson.Set(template, "choices.0.delta.role", "assistant")
190 template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", string(imagePayload))182 template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", imagePayload)
191 }183 }
192 }184 }
193 }185 }
internal/translator/gemini/openai/chat-completions/gemini_openai_response.go+8−23
@@ -8,7 +8,6 @@ package chat_completions · 7 unmodified lines
8import (8import (
9 "bytes"9 "bytes"
10 "context"10 "context"
11 "encoding/json"
12 "fmt"11 "fmt"
13 "strings"12 "strings"
14 "sync/atomic"13 "sync/atomic"
@@ -188,18 +187,11 @@ func ConvertGeminiResponseToOpenAI(_ context.Context, _ string, originalRequestR · 173 unmodified lines
188 template, _ = sjson.SetRaw(template, "choices.0.delta.images", `[]`)187 template, _ = sjson.SetRaw(template, "choices.0.delta.images", `[]`)
189 }188 }
190 imageIndex := len(gjson.Get(template, "choices.0.delta.images").Array())189 imageIndex := len(gjson.Get(template, "choices.0.delta.images").Array())
191 imagePayload, err := json.Marshal(map[string]any{190 imagePayload := `{"type":"image_url","image_url":{"url":""}}`
192 "index": imageIndex,191 imagePayload, _ = sjson.Set(imagePayload, "index", imageIndex)
193 "type": "image_url",192 imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
194 "image_url": map[string]string{
195 "url": imageURL,
196 },
197 })
198 if err != nil {
199 continue
200 }
201 template, _ = sjson.Set(template, "choices.0.delta.role", "assistant")193 template, _ = sjson.Set(template, "choices.0.delta.role", "assistant")
202 template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", string(imagePayload))194 template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", imagePayload)
203 }195 }
204 }196 }
205 }197 }
@@ -331,18 +323,11 @@ func ConvertGeminiResponseToOpenAINonStream(_ context.Context, _ string, origina · 125 unmodified lines
331 template, _ = sjson.SetRaw(template, "choices.0.message.images", `[]`)323 template, _ = sjson.SetRaw(template, "choices.0.message.images", `[]`)
332 }324 }
333 imageIndex := len(gjson.Get(template, "choices.0.message.images").Array())325 imageIndex := len(gjson.Get(template, "choices.0.message.images").Array())
334 imagePayload, err := json.Marshal(map[string]any{326 imagePayload := `{"type":"image_url","image_url":{"url":""}}`
335 "index": imageIndex,327 imagePayload, _ = sjson.Set(imagePayload, "index", imageIndex)
336 "type": "image_url",328 imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
337 "image_url": map[string]string{
338 "url": imageURL,
339 },
340 })
341 if err != nil {
342 continue
343 }
344 template, _ = sjson.Set(template, "choices.0.message.role", "assistant")329 template, _ = sjson.Set(template, "choices.0.message.role", "assistant")
345 template, _ = sjson.SetRaw(template, "choices.0.message.images.-1", string(imagePayload))330 template, _ = sjson.SetRaw(template, "choices.0.message.images.-1", imagePayload)
346 }331 }
347 }332 }
348 }333 }