chore: add image URL validation

This commit is contained in:
zaidmukaddam 2024-12-23 11:34:55 +05:30
parent dc866d8384
commit 64af65ac61

View File

@ -114,6 +114,24 @@ function sanitizeUrl(url: string): string {
return url.replace(/\s+/g, '%20') return url.replace(/\s+/g, '%20')
} }
async function isValidImageUrl(url: string): Promise<boolean> {
try {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 5000);
const response = await fetch(url, {
method: 'HEAD',
signal: controller.signal
});
clearTimeout(timeout);
return response.ok && (response.headers.get('content-type')?.startsWith('image/') ?? false);
} catch {
return false;
}
}
const defaultsystemPrompt = ` const defaultsystemPrompt = `
You are an expert AI web search engine called MiniPerplx, that helps users find information on the internet with no bullshit talks. You are an expert AI web search engine called MiniPerplx, that helps users find information on the internet with no bullshit talks.
Always start with running the tool(s) and then and then only write your response AT ALL COSTS!! Always start with running the tool(s) and then and then only write your response AT ALL COSTS!!
@ -285,18 +303,32 @@ export async function POST(req: Request) {
published_date: topics[index] === "news" ? obj.published_date : undefined, published_date: topics[index] === "news" ? obj.published_date : undefined,
})), })),
images: includeImageDescriptions images: includeImageDescriptions
? data.images ? await Promise.all(
.map(({ url, description }: { url: string; description?: string }) => ({ data.images
url: sanitizeUrl(url), .map(async ({ url, description }: { url: string; description?: string }) => {
description: description ?? '' const sanitizedUrl = sanitizeUrl(url);
})) const isValid = await isValidImageUrl(sanitizedUrl);
.filter(
(image: { url: string; description: string }): image is { url: string; description: string } => return isValid ? {
typeof image === 'object' && url: sanitizedUrl,
image.description !== undefined && description: description ?? ''
image.description !== '' } : null;
})
).then(results =>
results.filter((image): image is { url: string; description: string } =>
image !== null &&
typeof image === 'object' &&
typeof image.description === 'string' &&
image.description !== ''
) )
: data.images.map(({ url }: { url: string }) => sanitizeUrl(url)) )
: await Promise.all(
data.images
.map(async ({ url }: { url: string }) => {
const sanitizedUrl = sanitizeUrl(url);
return await isValidImageUrl(sanitizedUrl) ? sanitizedUrl : null;
})
).then(results => results.filter((url): url is string => url !== null))
}; };
}); });