systemprompt-mcp-reddit
Version:
A specialized Model Context Protocol (MCP) server that enables you to search, read, and interact with Reddit content, leveraging an AI Agent to help with each operation.
62 lines • 2.25 kB
JavaScript
import { XML_TAGS } from "../constants/message-handler.js";
/**
* Updates a user message with existing page content
* @param messages Array of messages to update
* @param blocks The page blocks to include
*/
export function updateUserMessageWithContent(messages, blocks) {
const userMessageIndex = messages.findIndex((msg) => msg.role === "user");
if (userMessageIndex === -1)
return;
const userMessage = messages[userMessageIndex];
if (userMessage.content.type !== "text")
return;
messages[userMessageIndex] = {
role: "user",
content: {
type: "text",
text: userMessage.content.text.replace(XML_TAGS.REQUEST_PARAMS_CLOSE, XML_TAGS.EXISTING_CONTENT_TEMPLATE(JSON.stringify(blocks, null, 2))),
},
};
}
/**
* Injects variables into text
* @param text The text to inject variables into
* @param variables The variables to inject
* @returns The text with variables injected
*/
export function injectVariablesIntoText(text, variables) {
// First handle conditional blocks
text = text.replace(/{{#([^}]+)}}(.*?){{\/\1}}/gs, (_, key, content) => {
return key in variables && variables[key] ? content : "";
});
// Then handle direct variable replacements
const directMatches = text.match(/{{([^#/][^}]*)}}/g);
if (!directMatches)
return text;
const missingVariables = directMatches
.map((match) => match.slice(2, -2))
.filter((key) => !(key in variables));
if (missingVariables.length > 0) {
throw new Error("Missing required variables: " + missingVariables.join(", "));
}
return text.replace(/{{([^#/][^}]*)}}/g, (_, key) => String(variables[key] ?? ""));
}
/**
* Injects variables into a message
* @param message The message to inject variables into
* @param variables The variables to inject
* @returns The message with variables injected
*/
export function injectVariables(message, variables) {
if (message.content.type !== "text")
return message;
return {
...message,
content: {
type: "text",
text: injectVariablesIntoText(message.content.text, variables),
},
};
}
//# sourceMappingURL=message-handlers.js.map