import { createHash } from "node:crypto"; import { mkdir, readFile, writeFile, rm } from "node:fs/promises"; import { createRequire } from "node:module"; import { homedir } from "node:os"; import { resolve } from "node:path"; import { createLogger } from "@poncho-ai/sdk"; import type { UploadsConfig } from "./config.js"; const uploadLog = createLogger("upload"); /** * Try to dynamically import a module, first from the harness's own * node_modules, then from the user's project directory. This handles * the case where the CLI is globally linked and optional deps are * installed in the user's project but not in the poncho-ai monorepo. */ // eslint-disable-next-line @typescript-eslint/no-explicit-any const tryImport = async (mod: string, workingDir?: string): Promise => { try { return await import(/* webpackIgnore: true */ mod); } catch { if (workingDir) { const require = createRequire(resolve(workingDir, "package.json")); const resolved = require.resolve(mod); return await import(/* webpackIgnore: true */ resolved); } throw new Error(`Cannot find module "${mod}"`); } }; export const PONCHO_UPLOAD_SCHEME = "poncho-upload://"; export const VFS_SCHEME = "vfs://"; export interface UploadStore { put(key: string, data: Buffer, mediaType: string): Promise; get(urlOrKey: string): Promise; delete(urlOrKey: string): Promise; } /** * Write-behind cache that wraps any UploadStore. `put()` caches the * data in memory and returns immediately with a `poncho-upload://` ref * while the actual cloud upload happens in the background. `get()` * serves from cache when available, eliminating the round-trip back * to the cloud store that would otherwise block the model request. */ class CachedUploadStore implements UploadStore { private readonly inner: UploadStore; private readonly cache = new Map(); private readonly maxEntries: number; private readonly ttlMs: number; constructor(inner: UploadStore, maxEntries = 64, ttlMs = 10 * 60 * 1000) { this.inner = inner; this.maxEntries = maxEntries; this.ttlMs = ttlMs; } async put(key: string, data: Buffer, mediaType: string): Promise { const ref = `${PONCHO_UPLOAD_SCHEME}${key}`; const now = Date.now(); this.cache.set(ref, { data, ts: now }); this.cache.set(key, { data, ts: now }); this.evict(); // Fire off the real upload in the background — don't block the caller. this.inner.put(key, data, mediaType).catch((err) => { uploadLog.error(`background upload failed: ${err instanceof Error ? err.message : err}`); }); return ref; } async get(urlOrKey: string): Promise { const cached = this.cache.get(urlOrKey); if (cached && Date.now() - cached.ts < this.ttlMs) { return cached.data; } return this.inner.get(urlOrKey); } async delete(urlOrKey: string): Promise { this.cache.delete(urlOrKey); return this.inner.delete(urlOrKey); } private evict(): void { if (this.cache.size <= this.maxEntries) return; let oldest: string | undefined; let oldestTs = Infinity; for (const [k, v] of this.cache) { if (v.ts < oldestTs) { oldestTs = v.ts; oldest = k; } } if (oldest) this.cache.delete(oldest); } } /** Derive a content-addressed key from file data. */ export const deriveUploadKey = ( data: Buffer, mediaType: string, ): string => { const hash = createHash("sha256").update(data).digest("hex").slice(0, 24); const ext = mimeToExt(mediaType); return `${hash}${ext}`; }; const DATA_URI_PREFIX = /^data:[^,]*?;base64,/; /** * Decode the `FileInput.data` field per its documented contract: it may be a * raw base64 string, a `data:;base64,<…>` URI, or an `https?://` URL. * Returns the decoded bytes. Older versions of the harness called * `Buffer.from(data, "base64")` unconditionally; that silently produced * garbage bytes for data URIs (the `:` / `;` / `,` in the prefix are not * valid base64 chars but Node's decoder ignores them rather than throwing). */ export const decodeFileInputData = async (data: string): Promise => { if (data.startsWith("http://") || data.startsWith("https://")) { const resp = await fetch(data); if (!resp.ok) { throw new Error( `uploads: failed to fetch file at ${data}: ${resp.status} ${resp.statusText}`, ); } return Buffer.from(await resp.arrayBuffer()); } const match = data.match(DATA_URI_PREFIX); if (match) { return Buffer.from(data.slice(match[0].length), "base64"); } return Buffer.from(data, "base64"); }; const MIME_EXT_MAP: Record = { "image/jpeg": ".jpg", "image/png": ".png", "image/gif": ".gif", "image/webp": ".webp", "image/svg+xml": ".svg", "application/pdf": ".pdf", "text/plain": ".txt", "text/csv": ".csv", "text/html": ".html", "application/json": ".json", "video/mp4": ".mp4", "video/webm": ".webm", "audio/mpeg": ".mp3", "audio/wav": ".wav", }; const mimeToExt = (mediaType: string): string => MIME_EXT_MAP[mediaType] ?? `.${mediaType.split("/").pop() ?? "bin"}`; // --------------------------------------------------------------------------- // Local filesystem implementation // --------------------------------------------------------------------------- const isServerlessEnv = (): boolean => { const cwd = process.cwd(); const home = homedir(); return ( process.env.VERCEL === "1" || process.env.VERCEL_ENV !== undefined || process.env.VERCEL_URL !== undefined || process.env.AWS_LAMBDA_FUNCTION_NAME !== undefined || process.env.AWS_EXECUTION_ENV?.includes("AWS_Lambda") === true || process.env.LAMBDA_TASK_ROOT !== undefined || process.env.NOW_REGION !== undefined || cwd.startsWith("/var/task") || home.startsWith("/var/task") || process.env.SERVERLESS === "1" ); }; export class LocalUploadStore implements UploadStore { private readonly uploadsDir: string; constructor(workingDir: string) { this.uploadsDir = isServerlessEnv() ? "/tmp/.poncho/uploads" : resolve(workingDir, ".poncho", "uploads"); } async put(_key: string, data: Buffer, mediaType: string): Promise { const key = deriveUploadKey(data, mediaType); const filePath = resolve(this.uploadsDir, key); await mkdir(this.uploadsDir, { recursive: true }); await writeFile(filePath, data); return `${PONCHO_UPLOAD_SCHEME}${key}`; } async get(urlOrKey: string): Promise { const key = urlOrKey.startsWith(PONCHO_UPLOAD_SCHEME) ? urlOrKey.slice(PONCHO_UPLOAD_SCHEME.length) : urlOrKey; return readFile(resolve(this.uploadsDir, key)); } async delete(urlOrKey: string): Promise { const key = urlOrKey.startsWith(PONCHO_UPLOAD_SCHEME) ? urlOrKey.slice(PONCHO_UPLOAD_SCHEME.length) : urlOrKey; await rm(resolve(this.uploadsDir, key), { force: true }); } } // --------------------------------------------------------------------------- // Vercel Blob implementation (optional dependency) // --------------------------------------------------------------------------- export class VercelBlobUploadStore implements UploadStore { // eslint-disable-next-line @typescript-eslint/no-explicit-any private sdk: any; private readonly workingDir?: string; private readonly access: "public" | "private"; constructor(workingDir?: string, access: "public" | "private" = "public") { this.workingDir = workingDir; this.access = access; } async loadSdk() { if (this.sdk) return this.sdk; try { this.sdk = await tryImport("@vercel/blob", this.workingDir); return this.sdk; } catch { throw new Error( 'uploads: vercel-blob provider requires the "@vercel/blob" package. Install it with: pnpm add @vercel/blob', ); } } async put(key: string, data: Buffer, mediaType: string): Promise { const sdk = await this.loadSdk(); await sdk.put(key, data, { access: this.access, contentType: mediaType, addRandomSuffix: false, allowOverwrite: true, }); return `${PONCHO_UPLOAD_SCHEME}${key}`; } async get(urlOrKey: string): Promise { let pathname = urlOrKey; if (urlOrKey.startsWith(PONCHO_UPLOAD_SCHEME)) { pathname = urlOrKey.slice(PONCHO_UPLOAD_SCHEME.length); } else if (urlOrKey.startsWith("https://") || urlOrKey.startsWith("http://")) { pathname = new URL(urlOrKey).pathname.slice(1); } if (this.access === "private") { const sdk = await this.loadSdk(); const result = await sdk.get(pathname, { access: "private" }); if (!result || result.statusCode !== 200) { throw new Error(`uploads: failed to fetch private blob "${pathname}": ${result?.statusCode ?? "not found"}`); } const chunks: Uint8Array[] = []; const reader = result.stream.getReader(); for (;;) { const { done, value } = await reader.read(); if (done) break; chunks.push(value); } return Buffer.concat(chunks); } const sdk = await this.loadSdk(); const blob = await sdk.head(pathname); const response = await fetch(blob.url); if (!response.ok) { throw new Error(`uploads: failed to fetch blob "${pathname}": ${response.status}`); } return Buffer.from(await response.arrayBuffer()); } async delete(urlOrKey: string): Promise { const sdk = await this.loadSdk(); await sdk.del(urlOrKey); } } // --------------------------------------------------------------------------- // S3-compatible implementation (optional dependency) // --------------------------------------------------------------------------- export class S3UploadStore implements UploadStore { // eslint-disable-next-line @typescript-eslint/no-explicit-any private s3Sdk: any; // eslint-disable-next-line @typescript-eslint/no-explicit-any private presignerSdk: any; // eslint-disable-next-line @typescript-eslint/no-explicit-any private client: any; private readonly bucket: string; private readonly region?: string; private readonly endpoint?: string; private readonly workingDir?: string; constructor(bucket: string, region?: string, endpoint?: string, workingDir?: string) { this.bucket = bucket; this.region = region; this.endpoint = endpoint; this.workingDir = workingDir; } async ensureClient() { if (this.client) return; try { this.s3Sdk = await tryImport("@aws-sdk/client-s3", this.workingDir); this.presignerSdk = await tryImport("@aws-sdk/s3-request-presigner", this.workingDir); } catch { throw new Error( 'uploads: s3 provider requires "@aws-sdk/client-s3" and "@aws-sdk/s3-request-presigner". ' + "Install with: pnpm add @aws-sdk/client-s3 @aws-sdk/s3-request-presigner", ); } this.client = new this.s3Sdk.S3Client({ region: this.region ?? process.env.AWS_REGION ?? "us-east-1", ...(this.endpoint ? { endpoint: this.endpoint, forcePathStyle: true } : {}), }); } async put(key: string, data: Buffer, mediaType: string): Promise { await this.ensureClient(); await this.client.send( new this.s3Sdk.PutObjectCommand({ Bucket: this.bucket, Key: key, Body: data, ContentType: mediaType, }), ); const url: string = await this.presignerSdk.getSignedUrl( this.client, new this.s3Sdk.GetObjectCommand({ Bucket: this.bucket, Key: key }), { expiresIn: 7 * 24 * 60 * 60 }, ); return url; } async get(urlOrKey: string): Promise { if (urlOrKey.startsWith("https://") || urlOrKey.startsWith("http://")) { const response = await fetch(urlOrKey); if (!response.ok) { throw new Error(`uploads: failed to fetch S3 object at ${urlOrKey}: ${response.status}`); } return Buffer.from(await response.arrayBuffer()); } // Strip the `poncho-upload://` scheme — LocalUploadStore does this for // both get/delete but S3 used to treat the full URI as a literal S3 // key, so resolving a persisted file part (data: "poncho-upload://…") // on a follow-up turn 404'd against R2 and the harness emitted a // "file is no longer available" placeholder. const key = urlOrKey.startsWith(PONCHO_UPLOAD_SCHEME) ? urlOrKey.slice(PONCHO_UPLOAD_SCHEME.length) : urlOrKey; await this.ensureClient(); const result = await this.client.send( new this.s3Sdk.GetObjectCommand({ Bucket: this.bucket, Key: key }), ); if (!result.Body) throw new Error(`uploads: empty body for S3 key ${key}`); return Buffer.from(await result.Body.transformToByteArray()); } async delete(urlOrKey: string): Promise { await this.ensureClient(); const key = urlOrKey.startsWith("https://") ? new URL(urlOrKey).pathname.slice(1) : urlOrKey.startsWith(PONCHO_UPLOAD_SCHEME) ? urlOrKey.slice(PONCHO_UPLOAD_SCHEME.length) : urlOrKey; await this.client.send( new this.s3Sdk.DeleteObjectCommand({ Bucket: this.bucket, Key: key }), ); } } // --------------------------------------------------------------------------- // Factory with graceful fallback // --------------------------------------------------------------------------- const warn = (msg: string) => { uploadLog.warn(msg); }; export const createUploadStore = async ( config: UploadsConfig | undefined, workingDir: string, ): Promise => { const provider = config?.provider ?? "local"; if (provider === "vercel-blob") { if (!process.env.BLOB_READ_WRITE_TOKEN) { warn( "uploads: vercel-blob configured but BLOB_READ_WRITE_TOKEN not found in environment. Falling back to local filesystem.\n" + " Make sure BLOB_READ_WRITE_TOKEN is set in your .env file or environment.", ); return new LocalUploadStore(workingDir); } const store = new VercelBlobUploadStore(workingDir, config?.access ?? "public"); try { await store.loadSdk(); uploadLog.item("using vercel-blob store"); return new CachedUploadStore(store); } catch { warn( 'uploads: vercel-blob configured but "@vercel/blob" package is not installed. Falling back to local filesystem.\n' + " Run `poncho build ` to auto-add it, or install manually: pnpm add @vercel/blob", ); return new LocalUploadStore(workingDir); } } if (provider === "s3") { const bucket = config?.bucket ?? process.env.PONCHO_UPLOADS_BUCKET; if (!process.env.AWS_ACCESS_KEY_ID || !bucket) { const missing = !process.env.AWS_ACCESS_KEY_ID ? "AWS_ACCESS_KEY_ID" : "bucket (config.uploads.bucket or PONCHO_UPLOADS_BUCKET)"; warn( `uploads: s3 configured but ${missing} not found in environment. Falling back to local filesystem.`, ); return new LocalUploadStore(workingDir); } const store = new S3UploadStore( bucket, config?.region ?? process.env.AWS_REGION, config?.endpoint ?? process.env.PONCHO_UPLOADS_ENDPOINT, workingDir, ); try { await store.ensureClient(); uploadLog.item(`using s3 store (bucket: ${bucket})`); return new CachedUploadStore(store); } catch { warn( "uploads: s3 configured but AWS SDK packages are not installed. Falling back to local filesystem.\n" + " Run `poncho build ` to auto-add them, or install manually: pnpm add @aws-sdk/client-s3 @aws-sdk/s3-request-presigner", ); return new LocalUploadStore(workingDir); } } return new LocalUploadStore(workingDir); };