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