src / promptPreprocessor.ts
import { type ChatMessage, type PromptPreprocessorController } from "@lmstudio/sdk";
import { ragPeerStatus } from "./peers";
const SYSTEM_RULES = `\
[System: Document Parser Plugin]
⢠parse_document ā extract full text from PDF, DOCX, spreadsheet, or plain text files.
⢠search_document ā keyword/regex search inside a document with surrounding context lines.
⢠Output tool results directly; do not paraphrase or truncate document content unless asked.
⢠On tool_error, read the error field and correct the parameter before retrying.`;
export async function promptPreprocessor(
ctl: PromptPreprocessorController,
userMessage: ChatMessage,
): Promise<string | ChatMessage> {
const history = await ctl.pullHistory();
if (history.length !== 0) return userMessage;
const ragTip = ragPeerStatus() === false
? `\n\n[Tip: Install the "altra/rag" plugin to semantically index documents with embeddings. rag_ingest + rag_query enables meaning-based retrieval across all your documents ā search_document is keyword/regex only and requires re-reading the file each time.]`
: "";
return `${SYSTEM_RULES}${ragTip}\n\n${userMessage.getText()}`;
}