fix: improve error handling for file URL processing

- Enhanced error handling in image reference detection to skip malformed file URLs without crashing.
- Updated media loading logic to throw an error for invalid file URLs, ensuring better feedback for users.
This commit is contained in:
Tyler Yust
2026-01-17 03:29:22 -08:00
committed by Peter Steinberger
parent 8c0e290db1
commit ddcc05f5f4
2 changed files with 11 additions and 3 deletions

View File

@@ -128,8 +128,12 @@ export function detectImageReferences(prompt: string): DetectedImageRef[] {
if (seen.has(raw.toLowerCase())) continue;
seen.add(raw.toLowerCase());
// Use fileURLToPath for proper handling (e.g., file://localhost/path)
const resolved = fileURLToPath(raw);
refs.push({ raw, type: "path", resolved });
try {
const resolved = fileURLToPath(raw);
refs.push({ raw, type: "path", resolved });
} catch {
// Skip malformed file:// URLs
}
}
// Pattern for file paths (absolute, relative, or home)

View File

@@ -27,7 +27,11 @@ async function loadWebMediaInternal(
const { maxBytes, optimizeImages = true } = options;
// Use fileURLToPath for proper handling of file:// URLs (handles file://localhost/path, etc.)
if (mediaUrl.startsWith("file://")) {
mediaUrl = fileURLToPath(mediaUrl);
try {
mediaUrl = fileURLToPath(mediaUrl);
} catch {
throw new Error(`Invalid file:// URL: ${mediaUrl}`);
}
}
const optimizeAndClampImage = async (buffer: Buffer, cap: number) => {