continue-custom/core/indexing/chunk/basic.ts
Rob Leidle 78a4025414 Offload all token counting to worker processes as well as some optimizations to do more token counting in parallel
Adding the workerpool logic broke gui as vite does not support __dirname
or path.join(...). To work around this, moved the gui dependency of
stripImages out of countTokens.ts
2024-07-26 13:10:36 -07:00

45 lines
1,017 B
TypeScript

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