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