src / preprocessor.ts

import { type PreprocessorController, type ChatMessage } from "@lmstudio/sdk";

// This is the preprocessor for your plugin. The preprocessor takes in the user message, transforms
// it, and returns the transformed message. The returned message is used instead of the original
// message.
//
// If multiple plugins with preprocessors are enabled, they will be chained together.
//
// A preprocessor can either:
// 1. Return a string (which will only replace the text of the message, i.e. not affecting files or
//    images in the message, if any)
// 2. Return a ChatMessage object (which will replace the entire message)
export async function preprocess(ctl: PreprocessorController, userMessage: ChatMessage) {
  // Get the text part of the userMessage;
  const textContent = userMessage.getText();

  // Print the user message we received. `toString()` is different from `getText()` in that
  // `toString()` will produce a human readable version of the message (which might include
  // placeholders for images/files), whereas `getText()` will only return the text part of the
  // message.
  console.info("Received a preprocess request for user message:", userMessage.toString());

  // Our preprocessor will just prepend the current date and time to the message.
  const transformed = `Current Date/Time: ${new Date().toISOString()}\n${textContent}`;

  // Return the transformed message as a string. This will only replace the text of the message.
  // If there are any files or images in the message, they will not be affected.
  return transformed;
}