Project Files
src / promptPreprocessor.ts
import { type ChatMessage, type PromptPreprocessorController } from "@lmstudio/sdk";
import { pluginConfigSchematics } from "./config";
import { getConfiguredAccounts } from "./accounts";
const SYSTEM_RULES = `\
[System: Email Plugin ā Multi-Account]
⢠email_accounts ā list configured accounts and their slot numbers/labels
⢠email_list(account) ā recent emails from a folder
⢠email_search(account, query/from/subject/since/...) ā search emails
⢠email_read(uid, account) ā full email body + attachments
⢠email_list_folders(account) ā all mailbox folders for an account
⢠email_delete(uids, account) ā move email(s) to Trash (recoverable, confirm first)
⢠email_send(account, to, subject, body) ā send/reply via SMTP
account param: slot number ("1","2","3"), label ("Work","Personal"), or blank for first account.
Always confirm with user before email_send ā it is irreversible.
On tool_error "not configured": tell the user to add credentials in plugin settings.`;
export async function promptPreprocessor(
ctl: PromptPreprocessorController,
userMessage: ChatMessage,
): Promise<string | ChatMessage> {
const history = await ctl.pullHistory();
if (history.length !== 0) return userMessage;
const cfg = ctl.getPluginConfig(pluginConfigSchematics);
const accounts = getConfiguredAccounts(cfg);
let accountInfo: string;
if (accounts.length === 0) {
accountInfo = `\n\n[Setup required: No email accounts configured. Add IMAP Host, Email Address, and Password for at least one account in plugin settings.]`;
} else {
const list = accounts.map(a => ` ⢠${a.slot} ā ${a.label} (${a.imap.user})${a.smtp ? " [send enabled]" : " [read-only]"}`).join("\n");
accountInfo = `\n\n[Configured accounts:\n${list}]`;
}
return `${SYSTEM_RULES}${accountInfo}\n\n${userMessage.getText()}`;
}