Project Files
src / promptPreprocessor.ts
import {
type ChatMessage,
type PromptPreprocessorController,
} from "@lmstudio/sdk";
import { registerExcelFile, getRegisteredExcelFiles } from "./tools/excel/excelFileRegistry";
import { registerDocFile, getRegisteredDocFiles } from "./tools/docs/docFileRegistry";
import { adaptFromPreprocessor } from "./utils/shared/pluginCtl";
import { configSchematics } from "./config";
import { makeDocPreview } from "./utils/docs/retrieval/filePreview";
import { makeExcelPreview } from "./utils/excel/excelPreview";
const EXCEL_EXTENSIONS = [".xlsx", ".xls", ".xlsm"];
const isExcelFile = (f: { name: string }) =>
EXCEL_EXTENSIONS.some(ext => f.name.toLowerCase().endsWith(ext));
export async function preprocess(ctl: PromptPreprocessorController, userMessage: ChatMessage) {
const originalUserText = userMessage.getText();
const workingDir = ctl.getWorkingDirectory();
const newFiles = userMessage.getFiles(ctl.client).filter(f => f.type !== "image");
for (const f of newFiles) {
if (isExcelFile(f)) registerExcelFile(workingDir, f);
else registerDocFile(workingDir, f);
}
const excelFiles = getRegisteredExcelFiles(workingDir);
const docFiles = getRegisteredDocFiles(workingDir);
if (excelFiles.length === 0 && docFiles.length === 0) return userMessage;
const adapted = adaptFromPreprocessor(ctl, configSchematics);
const tags: string[] = [];
if (docFiles.length > 0) {
const docLines = await Promise.all(
docFiles.map(async f => `- ${f.name}: ${await makeDocPreview(f, adapted)}`)
);
tags.push(
`Document(s) attached (previews below — do NOT guess a file's topic from its filename alone, ` +
`use these previews instead):\n${docLines.join("\n")}\n` +
`Use query_document_data with the exact fileName once you know which file is relevant.`
);
}
if (excelFiles.length > 0) {
const excelLines = await Promise.all(
excelFiles.map(async f => `- ${f.name}: ${await makeExcelPreview(f)}`)
);
tags.push(
`Spreadsheet(s) attached (previews below):\n${excelLines.join("\n")}\n` +
`Use query_excel_data with the exact fileName once you know which file is relevant.`
);
}
userMessage.replaceText(tags.join("\n\n") + `\n\n${originalUserText}`);
return userMessage;
}