mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 13:38:31 +00:00
refactor(translator): P4 concern #2 encodeDataUri — dedupe base64 data-uri building
- imageHelper.encodeDataUri(mime, base64) replaces 5 inline `data:${m};base64,${d}` templates
- Applied to gemini/claude/antigravity request + gemini response translators
- Golden tests pass, identical output
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,3 +1,8 @@
|
|||||||
|
// Build a base64 data URI from mime + base64 payload
|
||||||
|
export function encodeDataUri(mimeType, base64) {
|
||||||
|
return `data:${mimeType};base64,${base64}`;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch a remote image URL and return it as a base64 data URI.
|
* Fetch a remote image URL and return it as a base64 data URI.
|
||||||
* Used when upstream providers (Codex, etc.) require inline base64 images
|
* Used when upstream providers (Codex, etc.) require inline base64 images
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { register } from "../index.js";
|
import { register } from "../index.js";
|
||||||
import { FORMATS } from "../formats.js";
|
import { FORMATS } from "../formats.js";
|
||||||
import { adjustMaxTokens } from "../helpers/maxTokensHelper.js";
|
import { adjustMaxTokens } from "../helpers/maxTokensHelper.js";
|
||||||
|
import { encodeDataUri } from "../helpers/imageHelper.js";
|
||||||
|
|
||||||
// Convert Antigravity request to OpenAI format
|
// Convert Antigravity request to OpenAI format
|
||||||
// Antigravity body: { project, model, userAgent, requestType, requestId, request: { contents, systemInstruction, tools, toolConfig, generationConfig, sessionId } }
|
// Antigravity body: { project, model, userAgent, requestType, requestId, request: { contents, systemInstruction, tools, toolConfig, generationConfig, sessionId } }
|
||||||
@@ -156,7 +157,7 @@ function convertContent(content) {
|
|||||||
textParts.push({
|
textParts.push({
|
||||||
type: "image_url",
|
type: "image_url",
|
||||||
image_url: {
|
image_url: {
|
||||||
url: `data:${part.inlineData.mimeType};base64,${part.inlineData.data}`
|
url: encodeDataUri(part.inlineData.mimeType, part.inlineData.data)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { register } from "../index.js";
|
import { register } from "../index.js";
|
||||||
import { FORMATS } from "../formats.js";
|
import { FORMATS } from "../formats.js";
|
||||||
import { adjustMaxTokens } from "../helpers/maxTokensHelper.js";
|
import { adjustMaxTokens } from "../helpers/maxTokensHelper.js";
|
||||||
|
import { encodeDataUri } from "../helpers/imageHelper.js";
|
||||||
|
|
||||||
function stripAnthropicBillingHeader(text) {
|
function stripAnthropicBillingHeader(text) {
|
||||||
if (typeof text !== "string") return "";
|
if (typeof text !== "string") return "";
|
||||||
@@ -140,7 +141,7 @@ function convertClaudeMessage(msg) {
|
|||||||
parts.push({
|
parts.push({
|
||||||
type: "image_url",
|
type: "image_url",
|
||||||
image_url: {
|
image_url: {
|
||||||
url: `data:${block.source.media_type};base64,${block.source.data}`
|
url: encodeDataUri(block.source.media_type, block.source.data)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { register } from "../index.js";
|
import { register } from "../index.js";
|
||||||
import { FORMATS } from "../formats.js";
|
import { FORMATS } from "../formats.js";
|
||||||
import { adjustMaxTokens } from "../helpers/maxTokensHelper.js";
|
import { adjustMaxTokens } from "../helpers/maxTokensHelper.js";
|
||||||
|
import { encodeDataUri } from "../helpers/imageHelper.js";
|
||||||
|
|
||||||
// Convert Gemini request to OpenAI format
|
// Convert Gemini request to OpenAI format
|
||||||
export function geminiToOpenAIRequest(model, body, stream) {
|
export function geminiToOpenAIRequest(model, body, stream) {
|
||||||
@@ -88,7 +89,7 @@ function convertGeminiContent(content) {
|
|||||||
parts.push({
|
parts.push({
|
||||||
type: "image_url",
|
type: "image_url",
|
||||||
image_url: {
|
image_url: {
|
||||||
url: `data:${part.inlineData.mimeType};base64,${part.inlineData.data}`
|
url: encodeDataUri(part.inlineData.mimeType, part.inlineData.data)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { FORMATS } from "../formats.js";
|
|||||||
import { buildChunk } from "../helpers/chunkBuilder.js";
|
import { buildChunk } from "../helpers/chunkBuilder.js";
|
||||||
import { buildUsage } from "../helpers/usageHelper.js";
|
import { buildUsage } from "../helpers/usageHelper.js";
|
||||||
import { reasoningDelta } from "../helpers/reasoningHelper.js";
|
import { reasoningDelta } from "../helpers/reasoningHelper.js";
|
||||||
|
import { encodeDataUri } from "../helpers/imageHelper.js";
|
||||||
|
|
||||||
// Build chunk meta for current gemini state
|
// Build chunk meta for current gemini state
|
||||||
function chunkMeta(state) {
|
function chunkMeta(state) {
|
||||||
@@ -116,7 +117,7 @@ export function geminiToOpenAIResponse(chunk, state) {
|
|||||||
{
|
{
|
||||||
images: [{
|
images: [{
|
||||||
type: "image_url",
|
type: "image_url",
|
||||||
image_url: { url: `data:${mimeType};base64,${inlineData.data}` }
|
image_url: { url: encodeDataUri(mimeType, inlineData.data) }
|
||||||
}]
|
}]
|
||||||
},
|
},
|
||||||
null
|
null
|
||||||
|
|||||||
Reference in New Issue
Block a user