Project Files
src / utils / docs / parser / parseFile.ts
import type { FileHandle, PromptPreprocessorController } from "@lmstudio/sdk";
import type { ParsedFile } from "../../../types/types";
import { createDefaultChain } from "./parserIndex";
import { type PluginCapableCtl } from "../../shared/pluginCtl";
export const globalCache = new Map<string, ParsedFile>();
const chain = createDefaultChain();
export async function parseFile(
ctl: PluginCapableCtl,
file: FileHandle,
): Promise<ParsedFile> {
let filePath: string;
try {
filePath = await file.getFilePath();
} catch {
filePath = file.name;
}
const cached = globalCache.get(filePath);
if (cached !== undefined) return cached;
const result = await chain.run(file, { ctl, filePath });
const parsed: ParsedFile = result.success
? {
content: result.content,
ocrApplied: result.parserName === "ocr-pdf",
customParsed: result.isCustomExtraction,
}
: { content: "", ocrApplied: false, customParsed: false };
globalCache.set(filePath, parsed)
return parsed;
}