in src/lib/server/textGeneration/assistant.ts [7:53]
export async function processPreprompt(preprompt: string, user_message: string | undefined) {
// Replace {{today}} with formatted date
const today = new Intl.DateTimeFormat("en-US", {
weekday: "long",
day: "numeric",
month: "long",
year: "numeric",
}).format(new Date());
preprompt = preprompt.replaceAll("{{today}}", today);
const requestRegex = /{{\s?(get|post|url)=(.*?)\s?}}/g;
for (const match of preprompt.matchAll(requestRegex)) {
const method = match[1].toUpperCase();
const urlString = match[2];
try {
const url = new URL(urlString);
if ((await isURLLocal(url)) && config.ENABLE_LOCAL_FETCH !== "true") {
throw new Error("URL couldn't be fetched, it resolved to a local address.");
}
let res;
if (method == "POST") {
res = await fetch(url.href, {
method: "POST",
body: user_message,
headers: {
"Content-Type": "text/plain",
},
});
} else if (method == "GET" || method == "URL") {
res = await fetch(url.href);
} else {
throw new Error("Invalid method " + method);
}
if (!res.ok) {
throw new Error("URL couldn't be fetched, error " + res.status);
}
const text = await res.text();
preprompt = preprompt.replaceAll(match[0], text);
} catch (e) {
preprompt = preprompt.replaceAll(match[0], (e as Error).message);
}
}
return preprompt;
}