mirror of
https://github.com/decolua/9router.git
synced 2026-05-08 12:01:28 +00:00
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
35 lines
1.2 KiB
JavaScript
35 lines
1.2 KiB
JavaScript
/**
|
|
* 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);
|
|
}
|
|
}
|