src / promptPreprocessor.ts
import { type ChatMessage, type PromptPreprocessorController } from "@lmstudio/sdk";
import { parseIcsFile, filterByRange, formatEvent } from "./ical";
import { fetchMacOSEvents } from "./macos";
// PromptPreprocessorController has no getPluginConfig — use defaults.
// injectTodayIntoPrompt config only affects tool calls; preprocessor always injects.
// To disable injection, users can toggle it off by removing the preprocessor (not supported without config access).
export async function promptPreprocessor(
ctl: PromptPreprocessorController,
userMessage: ChatMessage,
): Promise<string | ChatMessage> {
// Only inject on first message of a session to avoid repetition
const history = await ctl.pullHistory();
if (history.length !== 0) return userMessage;
try {
const today = new Date();
today.setHours(0, 0, 0, 0);
const endOfDay = new Date(today);
endOfDay.setHours(23, 59, 59, 999);
// Default to macOS; ICS path unknown without config access
const events = fetchMacOSEvents(today, endOfDay);
const summary = events.length === 0
? "No events scheduled today."
: `Today's events (${today.toDateString()}):\n${events.map(e => " • " + formatEvent(e)).join("\n")}`;
const injection = `[Calendar context: ${summary}]`;
return `${injection}\n\n${userMessage.getText()}`;
} catch {
// Silently skip if Calendar.app unavailable or on non-macOS
return userMessage;
}
}