fix(codex): await image URL fetches before sending to upstream (closes #575)
Remote HTTP(S) image URLs are fetched and inlined as base64 data URIs in a new prefetchImages() step run before super.execute(), so the body sent to Codex contains resolved image bytes instead of URLs the backend cannot access. Scope is limited to the Codex executor — base executor and other providers are untouched. Co-authored-by: anuragg-saxenaa <anuragg.saxenaa@gmail.com> Made-with: Cursor
This commit is contained in:
34
open-sse/translator/helpers/imageHelper.js
Normal file
34
open-sse/translator/helpers/imageHelper.js
Normal file
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Fetch a remote image URL and return it as a base64 data URI.
|
||||
* Used when upstream providers (Codex, etc.) require inline base64 images
|
||||
* instead of remote URLs they cannot fetch.
|
||||
* Returns null if fetch fails.
|
||||
*
|
||||
* @param {string} imageUrl - HTTP(S) URL of the image
|
||||
* @param {object} options - { signal, timeoutMs }
|
||||
* @returns {Promise<{url: string, mimeType: string}|null>}
|
||||
*/
|
||||
export async function fetchImageAsBase64(imageUrl, options = {}) {
|
||||
const { signal, timeoutMs = 10000 } = options;
|
||||
if (!imageUrl || (!imageUrl.startsWith("http://") && !imageUrl.startsWith("https://"))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const controller = new AbortController();
|
||||
const timeout = signal ? null : setTimeout(() => controller.abort(), timeoutMs);
|
||||
const fetchSignal = signal || controller.signal;
|
||||
|
||||
try {
|
||||
const response = await fetch(imageUrl, { signal: fetchSignal });
|
||||
if (!response.ok) return null;
|
||||
|
||||
const mimeType = response.headers.get("Content-Type") || "image/jpeg";
|
||||
const arrayBuffer = await response.arrayBuffer();
|
||||
const base64 = Buffer.from(arrayBuffer).toString("base64");
|
||||
return { url: `data:${mimeType};base64,${base64}`, mimeType };
|
||||
} catch {
|
||||
return null;
|
||||
} finally {
|
||||
if (timeout) clearTimeout(timeout);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user