| 1 | import { Chunk, ChunkWithoutID } from "../../index.js"; |
| 2 | import { countTokensAsync } from "../../llm/countTokens.js"; |
| 3 | import { supportedLanguages } from "../../util/treeSitter.js"; |
| 4 | import { getUriFileExtension, getUriPathBasename } from "../../util/uri.js"; |
| 5 | |
| 6 | import { basicChunker } from "./basic.js"; |
| 7 | import { codeChunker } from "./code.js"; |
| 8 | |
| 9 | export type ChunkDocumentParam = { |
| 10 | filepath: string; |
| 11 | contents: string; |
| 12 | maxChunkSize: number; |
| 13 | digest: string; |
| 14 | }; |
| 15 | |
| 16 | export async function* chunkDocumentWithoutId( |
| 17 | fileUri: string, |
| 18 | contents: string, |
| 19 | maxChunkSize: number, |
| 20 | ): AsyncGenerator<ChunkWithoutID> { |
| 21 | if (contents.trim() === "") { |
| 22 | return; |
| 23 | } |
| 24 | const extension = getUriFileExtension(fileUri); |
| 25 | // Files that should use basicChunker despite having tree-sitter support |
| 26 | // These files don't have code structure (classes/functions) that codeChunker expects |
| 27 | const NON_CODE_EXTENSIONS = [ |
| 28 | "css", |
| 29 | "html", |
| 30 | "htm", |
| 31 | "json", |
| 32 | "toml", |
| 33 | "yaml", |
| 34 | "yml", |
| 35 | ]; |
| 36 | |
| 37 | if ( |
| 38 | extension in supportedLanguages && |
| 39 | !NON_CODE_EXTENSIONS.includes(extension) |
| 40 | ) { |
| 41 | try { |
| 42 | for await (const chunk of codeChunker(fileUri, contents, maxChunkSize)) { |
| 43 | yield chunk; |
| 44 | } |
| 45 | return; |
| 46 | } catch (e: any) { |
| 47 | // falls back to basicChunker |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | yield* basicChunker(contents, maxChunkSize); |
| 52 | } |
| 53 | |
| 54 | export async function* chunkDocument({ |
| 55 | filepath, |
| 56 | contents, |
| 57 | maxChunkSize, |
| 58 | digest, |
| 59 | }: ChunkDocumentParam): AsyncGenerator<Chunk> { |
| 60 | let index = 0; |
| 61 | const chunkPromises: Promise<Chunk | undefined>[] = []; |
| 62 | for await (const chunkWithoutId of chunkDocumentWithoutId( |
| 63 | filepath, |
| 64 | contents, |
| 65 | maxChunkSize, |
| 66 | )) { |
| 67 | chunkPromises.push( |
| 68 | new Promise((resolve) => { |
| 69 | void (async () => { |
| 70 | if ((await countTokensAsync(chunkWithoutId.content)) > maxChunkSize) { |
| 71 | // console.debug( |
| 72 | // `Chunk with more than ${maxChunkSize} tokens constructed: `, |
| 73 | // filepath, |
| 74 | // countTokens(chunkWithoutId.content), |
| 75 | // ); |
| 76 | return resolve(undefined); |
| 77 | } |
| 78 | resolve({ |
| 79 | ...chunkWithoutId, |
| 80 | digest, |
| 81 | index: index++, |
| 82 | filepath, |
| 83 | }); |
| 84 | })(); |
| 85 | }), |
| 86 | ); |
| 87 | } |
| 88 | for await (const chunk of chunkPromises) { |
| 89 | if (!chunk) { |
| 90 | continue; |
| 91 | } |
| 92 | yield chunk; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | export function shouldChunk(fileUri: string, contents: string): boolean { |
| 97 | if (contents.length > 1000000) { |
| 98 | // if a file has more than 1m characters then skip it |
| 99 | return false; |
| 100 | } |
| 101 | if (contents.length === 0) { |
| 102 | return false; |
| 103 | } |
| 104 | const baseName = getUriPathBasename(fileUri); |
| 105 | return baseName.includes("."); |
| 106 | } |