fix(kiro): preserve inline images in OpenAI MITM

Forward Kiro userInputMessage.images as OpenAI-compatible image_url content parts.
This commit is contained in:
Outis
2026-09-03 09:19:38 +07:00
committed by decolua
parent 70f15aa50b
commit 1f190bd00b
3 changed files with 227 additions and 1 deletions

View File

@@ -180,6 +180,14 @@ function withInitialFrame(state, frames) {
// ─── CodeWhisperer → OpenAI conversion ───────────────────────────────────────
const INLINE_IMAGE_MIME_BY_FORMAT = new Map([
["png", "image/png"],
["jpeg", "image/jpeg"],
["jpg", "image/jpeg"],
["gif", "image/gif"],
["webp", "image/webp"],
]);
/**
* Safely stringify a tool-call input value.
* OpenAI expects `function.arguments` to be a JSON string, never an object.
@@ -214,9 +222,32 @@ function convertUserInputMessage(uim) {
});
}
const images = Array.isArray(uim.images) ? uim.images : [];
const imageParts = images
.filter(image => image !== null
&& typeof image === "object"
&& !Array.isArray(image)
&& image.source !== null
&& typeof image.source === "object"
&& !Array.isArray(image.source)
&& INLINE_IMAGE_MIME_BY_FORMAT.has(image.format)
&& typeof image.source.bytes === "string"
&& image.source.bytes.length > 0)
.map(image => ({
type: "image_url",
image_url: {
url: `data:${INLINE_IMAGE_MIME_BY_FORMAT.get(image.format)};base64,${image.source.bytes}`,
},
}));
// Emit user text only if it exists alongside OR when there are no tool results
const text = (uim.content || "").trim();
if (text || toolResults.length === 0) {
if (imageParts.length > 0) {
const content = [];
if (text) content.push({ type: "text", text });
content.push(...imageParts);
out.push({ role: "user", content });
} else if (text || toolResults.length === 0) {
out.push({ role: "user", content: text });
}