continue-custom/core/indexing/chunk/basic.ts
Nate Sesti 74020b1052
Tests for indexing + follow all .gitignore syntax (#1661)
* cleaner indexing progress updates messages

* chunking tests

* first round of testing for walkDir in .ts

* few more tests

* swap fs with ide

* clean up dead code

* replace traverseDirectory

* fix listFolders

* smoother indexing updates for chunking

* ide pathSetp

* absolute paths test

* fix path sep error with abs paths on windows

* clean up tests
2024-07-03 23:41:43 -07:00

39 lines
878 B
TypeScript

import { ChunkWithoutID } from "../../index.js";
import { countTokens } from "../../llm/countTokens.js";
export function* basicChunker(
contents: string,
maxChunkSize: number,
): Generator<ChunkWithoutID> {
if (contents.trim().length === 0) {
return;
}
let chunkContent = "";
let chunkTokens = 0;
let startLine = 0;
let currLine = 0;
for (const line of contents.split("\n")) {
const lineTokens = countTokens(line);
if (chunkTokens + lineTokens > maxChunkSize - 5) {
yield { content: chunkContent, startLine, endLine: currLine - 1 };
chunkContent = "";
chunkTokens = 0;
startLine = currLine;
}
if (lineTokens < maxChunkSize) {
chunkContent += `${line}\n`;
chunkTokens += lineTokens + 1;
}
currLine++;
}
yield {
content: chunkContent,
startLine,
endLine: currLine - 1,
};
}