( filename: string, )
| 2991 | * (more than PDF_AT_MENTION_INLINE_THRESHOLD pages), or null otherwise. |
| 2992 | */ |
| 2993 | export async function tryGetPDFReference( |
| 2994 | filename: string, |
| 2995 | ): Promise<PDFReferenceAttachment | null> { |
| 2996 | const ext = parse(filename).ext.toLowerCase() |
| 2997 | if (!isPDFExtension(ext)) { |
| 2998 | return null |
| 2999 | } |
| 3000 | try { |
| 3001 | const [stats, pageCount] = await Promise.all([ |
| 3002 | getFsImplementation().stat(filename), |
| 3003 | getPDFPageCount(filename), |
| 3004 | ]) |
| 3005 | // Use page count if available, otherwise fall back to size heuristic (~100KB per page) |
| 3006 | const effectivePageCount = pageCount ?? Math.ceil(stats.size / (100 * 1024)) |
| 3007 | if (effectivePageCount > PDF_AT_MENTION_INLINE_THRESHOLD) { |
| 3008 | logEvent('ncode_pdf_reference_attachment', { |
| 3009 | pageCount: effectivePageCount, |
| 3010 | fileSize: stats.size, |
| 3011 | hadPdfinfo: pageCount !== null, |
| 3012 | } as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS) |
| 3013 | return { |
| 3014 | type: 'pdf_reference', |
| 3015 | filename, |
| 3016 | pageCount: effectivePageCount, |
| 3017 | fileSize: stats.size, |
| 3018 | displayPath: relative(getCwd(), filename), |
| 3019 | } |
| 3020 | } |
| 3021 | } catch { |
| 3022 | // If we can't stat the file, return null to proceed with normal reading |
| 3023 | } |
| 3024 | return null |
| 3025 | } |
| 3026 | |
| 3027 | export async function generateFileAttachment( |
| 3028 | filename: string, |
no test coverage detected