continue-custom/core/indexing/chunk/chunk.ts
Nate Sesti 0419da7568
✨ Tab Autocomplete (#758)
* ✨ tab autocomplete attempt 1

* 🚧 WIP autocomplete

* 🚧 wip

* 🔧 config for tab autocomplete

* ⚡️ caching for autocomplete

* 🚧 external snippet formatting

* 🎨 stuff

* stuff

* ⚡️ debouncing and other improvements

* ⚡️ use LSP to get definition during function call

* 💄 status bar for Continue tab-autocomplete

* 🐛 small status bar fix

* 📝 basic docs for tab-autocomplete

* 📝 h2 in docs

* 🚸 Explain how to download Ollama and Deepseek Coder

* 📝 autocomplete troubleshooting
2024-01-31 18:59:18 -08:00

62 lines
1.5 KiB
TypeScript

import { Chunk, ChunkWithoutID } from "../..";
import { MAX_CHUNK_SIZE } from "../../llm/constants";
import { countTokens } from "../../llm/countTokens";
import { supportedLanguages } from "../../util/treeSitter";
import { basicChunker } from "./basic";
import { codeChunker } from "./code";
async function* chunkDocumentWithoutId(
filepath: string,
contents: string,
maxChunkSize: number
): AsyncGenerator<ChunkWithoutID> {
if (contents.trim() === "") {
return;
}
const segs = filepath.split(".");
const ext = segs[segs.length - 1];
if (ext in supportedLanguages) {
try {
for await (const chunk of codeChunker(filepath, contents, maxChunkSize)) {
yield chunk;
}
return;
} catch (e) {
console.error(`Failed to parse ${filepath}: `, e);
// falls back to basicChunker
}
}
yield* basicChunker(contents, maxChunkSize);
}
export async function* chunkDocument(
filepath: string,
contents: string,
maxChunkSize: number,
digest: string
): AsyncGenerator<Chunk> {
let index = 0;
for await (let chunkWithoutId of chunkDocumentWithoutId(
filepath,
contents,
maxChunkSize
)) {
if (countTokens(chunkWithoutId.content, "gpt-4") > MAX_CHUNK_SIZE) {
console.warn(
`Chunk with more than ${maxChunkSize} tokens constructed: `,
filepath,
countTokens(chunkWithoutId.content, "gpt-4")
);
continue;
}
yield {
...chunkWithoutId,
digest,
index,
filepath,
};
index++;
}
}