fall back to basic chunker when parser fails to load

This commit is contained in:
Nate 2024-08-29 12:40:20 -07:00
parent cdcfe1b6f1
commit 38cd597431
2 changed files with 15 additions and 7 deletions

View file

@ -1,5 +1,7 @@
import { Chunk, ChunkWithoutID, IDE } from "../../index.js";
import { Chunk, ChunkWithoutID } from "../../index.js";
import { countTokens, countTokensAsync } from "../../llm/countTokens.js";
import { extractMinimalStackTraceInfo } from "../../util/extractMinimalStackTraceInfo.js";
import { Telemetry } from "../../util/posthog.js";
import { supportedLanguages } from "../../util/treeSitter.js";
import { basicChunker } from "./basic.js";
import { codeChunker } from "./code.js";
@ -28,8 +30,11 @@ async function* chunkDocumentWithoutId(
yield chunk;
}
return;
} catch (e) {
// console.error(`Failed to parse ${filepath}: `, e);
} catch (e: any) {
Telemetry.capture("code_chunker_error", {
fileExtension: ext,
stack: extractMinimalStackTraceInfo(e.stack),
});
// falls back to basicChunker
}
}
@ -78,7 +83,11 @@ export async function* chunkDocument({
}
}
export function shouldChunk(pathSep: string, filepath: string, contents: string): boolean {
export function shouldChunk(
pathSep: string,
filepath: string,
contents: string,
): boolean {
if (contents.length > 1000000) {
// if a file has more than 1m characters then skip it
return false;
@ -89,4 +98,4 @@ export function shouldChunk(pathSep: string, filepath: string, contents: string)
const basename = filepath.split(pathSep).pop();
// files without extensions are often binary files, skip it if so
return basename?.includes(".") ?? false;
}
}

View file

@ -232,8 +232,7 @@ export async function* codeChunker(
const parser = await getParserForFile(filepath);
if (parser === undefined) {
console.debug(`Failed to load parser for file ${filepath}: `);
return;
throw new Error(`Failed to load parser for file ${filepath}: `);
}
const tree = parser.parse(contents);