(filePath: string, basePath: string)
| 62 | } |
| 63 | |
| 64 | async chunkMdxFile(filePath: string, basePath: string): Promise<DocChunk[]> { |
| 65 | const content = await fs.readFile(filePath, 'utf-8') |
| 66 | const relativePath = path.relative(basePath, filePath) |
| 67 | |
| 68 | const { data: frontmatter, content: markdownContent } = this.parseFrontmatter(content) |
| 69 | |
| 70 | const documentUrl = this.generateDocumentUrl(relativePath) |
| 71 | |
| 72 | const { chunks: textChunks, cleanedContent } = await this.splitContent(markdownContent) |
| 73 | |
| 74 | const headers = this.extractHeaders(cleanedContent) |
| 75 | |
| 76 | logger.info(`Generating embeddings for ${textChunks.length} chunks in ${relativePath}`) |
| 77 | const embeddingModel = getConfiguredEmbeddingModel() |
| 78 | const embeddings: number[][] = |
| 79 | textChunks.length > 0 ? (await generateEmbeddings(textChunks, embeddingModel)).embeddings : [] |
| 80 | |
| 81 | const chunks: DocChunk[] = [] |
| 82 | let currentPosition = 0 |
| 83 | |
| 84 | for (let i = 0; i < textChunks.length; i++) { |
| 85 | const chunkText = textChunks[i] |
| 86 | const chunkStart = currentPosition |
| 87 | const chunkEnd = currentPosition + chunkText.length |
| 88 | |
| 89 | const relevantHeader = this.findRelevantHeader(headers, chunkStart) |
| 90 | |
| 91 | const chunk: DocChunk = { |
| 92 | text: chunkText, |
| 93 | tokenCount: estimateTokens(chunkText), |
| 94 | sourceDocument: relativePath, |
| 95 | headerLink: relevantHeader ? `${documentUrl}#${relevantHeader.anchor}` : documentUrl, |
| 96 | headerText: relevantHeader?.text || frontmatter.title || 'Document Root', |
| 97 | headerLevel: relevantHeader?.level || 1, |
| 98 | embedding: embeddings[i] || [], |
| 99 | embeddingModel, |
| 100 | metadata: { |
| 101 | startIndex: chunkStart, |
| 102 | endIndex: chunkEnd, |
| 103 | title: frontmatter.title, |
| 104 | }, |
| 105 | } |
| 106 | |
| 107 | chunks.push(chunk) |
| 108 | currentPosition = chunkEnd |
| 109 | } |
| 110 | |
| 111 | return chunks |
| 112 | } |
| 113 | |
| 114 | private async findMdxFiles(dirPath: string): Promise<string[]> { |
| 115 | const files: string[] = [] |
no test coverage detected