| 43 | * Loader for loading plain text files. |
| 44 | */ |
| 45 | export class TextLoader implements FileLoaderInterface { |
| 46 | async loadPages(filePath: string): Promise<DocumentPage[]> { |
| 47 | log('Loading text file:', filePath); |
| 48 | try { |
| 49 | const fileContent = await readTextFile(filePath); |
| 50 | log('Text file loaded successfully, size:', fileContent.length, 'bytes'); |
| 51 | const lines = fileContent.split('\n'); |
| 52 | const lineCount = lines.length; |
| 53 | const charCount = fileContent.length; |
| 54 | log('Text file stats:', { charCount, lineCount }); |
| 55 | |
| 56 | const page: DocumentPage = { |
| 57 | charCount, |
| 58 | lineCount, |
| 59 | metadata: { |
| 60 | lineNumberEnd: lineCount, |
| 61 | lineNumberStart: 1, |
| 62 | }, |
| 63 | pageContent: fileContent, |
| 64 | }; |
| 65 | |
| 66 | log('Text page created successfully'); |
| 67 | return [page]; |
| 68 | } catch (e) { |
| 69 | const error = e as Error; |
| 70 | log('Error encountered while loading text file'); |
| 71 | console.error(`Error loading text file ${filePath}: ${error.message}`); |
| 72 | // If reading fails, return a Page containing error information |
| 73 | const errorPage: DocumentPage = { |
| 74 | charCount: 0, |
| 75 | lineCount: 0, |
| 76 | metadata: { |
| 77 | error: `Failed to load text file: ${error.message}`, |
| 78 | }, |
| 79 | pageContent: '', |
| 80 | }; |
| 81 | log('Created error page for failed text file loading'); |
| 82 | return [errorPage]; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * For plain text, simply concatenate the content of all pages. |
| 88 | * (Although TextLoader typically has only one page, this maintains interface consistency) |
| 89 | * @param pages Array of pages |
| 90 | * @returns Aggregated content |
| 91 | */ |
| 92 | async aggregateContent(pages: DocumentPage[]): Promise<string> { |
| 93 | log('Aggregating content from', pages.length, 'text pages'); |
| 94 | // By default, join with newline separator, can be adjusted or made configurable as needed |
| 95 | const result = pages.map((page) => page.pageContent).join('\n'); |
| 96 | log('Content aggregated successfully, length:', result.length); |
| 97 | return result; |
| 98 | } |
| 99 | } |
nothing calls this directly
no outgoing calls
no test coverage detected