diff --git a/src/agents/pi-embedded-runner/run/images.ts b/src/agents/pi-embedded-runner/run/images.ts index f7de8b734..dad5805bf 100644 --- a/src/agents/pi-embedded-runner/run/images.ts +++ b/src/agents/pi-embedded-runner/run/images.ts @@ -163,6 +163,8 @@ export async function loadImageFromRef( // For file paths, resolve relative to the appropriate root: // - When sandbox is enabled, resolve relative to sandboxRoot for security // - Otherwise, resolve relative to workspaceDir + // Note: ref.resolved may already be absolute (e.g., after ~ expansion in detectImageReferences), + // in which case we skip relative resolution. if (ref.type === "path" && !path.isAbsolute(targetPath)) { const resolveRoot = options?.sandboxRoot ?? workspaceDir; targetPath = path.resolve(resolveRoot, targetPath); diff --git a/src/web/media.ts b/src/web/media.ts index 50dce16d1..1d9bdceab 100644 --- a/src/web/media.ts +++ b/src/web/media.ts @@ -1,5 +1,6 @@ import fs from "node:fs/promises"; import path from "node:path"; +import { fileURLToPath } from "node:url"; import { logVerbose, shouldLogVerbose } from "../globals.js"; import { type MediaKind, maxBytesForKind, mediaKindFromMime } from "../media/constants.js"; @@ -24,8 +25,9 @@ async function loadWebMediaInternal( options: WebMediaOptions = {}, ): Promise { const { maxBytes, optimizeImages = true } = options; + // Use fileURLToPath for proper handling of file:// URLs (handles file://localhost/path, etc.) if (mediaUrl.startsWith("file://")) { - mediaUrl = mediaUrl.replace("file://", ""); + mediaUrl = fileURLToPath(mediaUrl); } const optimizeAndClampImage = async (buffer: Buffer, cap: number) => { @@ -57,7 +59,7 @@ async function loadWebMediaInternal( kind: MediaKind; fileName?: string; }): Promise => { - const cap = Math.min(maxBytes ?? maxBytesForKind(params.kind), maxBytesForKind(params.kind)); + const cap = maxBytes !== undefined ? Math.min(maxBytes, maxBytesForKind(params.kind)) : maxBytesForKind(params.kind); if (params.kind === "image") { const isGif = params.contentType === "image/gif"; if (isGif || !optimizeImages) {