Compare commits
2 Commits
af568bc9cc
...
c4a5de31f3
Author | SHA1 | Date | |
---|---|---|---|
c4a5de31f3 | |||
396ef9d2be |
36
src/main.ts
36
src/main.ts
@ -31,7 +31,7 @@ const getNotifications = async () => {
|
|||||||
|
|
||||||
const notifications = await getNotifications();
|
const notifications = await getNotifications();
|
||||||
|
|
||||||
const storeUserData = async (notification: Notification) => {
|
const storeUserData = async (notification: Notification): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const user = await prisma.user.upsert({
|
const user = await prisma.user.upsert({
|
||||||
where: { userFqn: notification.status.account.fqn },
|
where: { userFqn: notification.status.account.fqn },
|
||||||
@ -99,16 +99,13 @@ const generateOllamaRequest = async (
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (await alreadyRespondedTo(notification)) {
|
if (await alreadyRespondedTo(notification)) {
|
||||||
// console.log(
|
|
||||||
// `Already responded to notification ID ${notification.status.id}. Canceling.`
|
|
||||||
// );
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await storeUserData(notification);
|
await storeUserData(notification);
|
||||||
const ollamaRequestBody: OllamaRequest = {
|
const ollamaRequestBody: OllamaRequest = {
|
||||||
model: process.env.OLLAMA_MODEL as string,
|
model: process.env.OLLAMA_MODEL as string,
|
||||||
system: process.env.OLLAMA_SYSTEM_PROMPT as string,
|
system: process.env.OLLAMA_SYSTEM_PROMPT as string,
|
||||||
prompt: `@${notification.status.account.fqn} asks: ${striptags(
|
prompt: `@${notification.status.account.fqn} says: ${striptags(
|
||||||
notification.status.content
|
notification.status.content
|
||||||
)}`,
|
)}`,
|
||||||
stream: false,
|
stream: false,
|
||||||
@ -119,10 +116,8 @@ const generateOllamaRequest = async (
|
|||||||
});
|
});
|
||||||
const ollamaResponse: OllamaResponse = await response.json();
|
const ollamaResponse: OllamaResponse = await response.json();
|
||||||
await storePromptData(notification, ollamaResponse);
|
await storePromptData(notification, ollamaResponse);
|
||||||
// console.log(
|
// await postReplyToStatus(notification, ollamaResponse);
|
||||||
// `${notification.status.account.fqn} asked:\n${notification.status.content}\nResponse:\n${ollamaResponse.response}`
|
return ollamaResponse;
|
||||||
// );
|
|
||||||
await postReplyToStatus(notification, ollamaResponse);
|
|
||||||
}
|
}
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
throw new Error(error.message);
|
throw new Error(error.message);
|
||||||
@ -134,15 +129,21 @@ const postReplyToStatus = async (
|
|||||||
ollamaResponseBody: OllamaResponse
|
ollamaResponseBody: OllamaResponse
|
||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
const mentions = notification.status.mentions?.map((mention) => {
|
let mentions: string[];
|
||||||
return mention.acct;
|
const statusBody: NewStatusBody = {
|
||||||
});
|
|
||||||
let statusBody: NewStatusBody = {
|
|
||||||
content_type: "text/markdown",
|
content_type: "text/markdown",
|
||||||
status: ollamaResponseBody.response,
|
status: ollamaResponseBody.response,
|
||||||
in_reply_to_id: notification.status.id,
|
in_reply_to_id: notification.status.id,
|
||||||
to: mentions,
|
|
||||||
};
|
};
|
||||||
|
if (
|
||||||
|
notification.status.mentions &&
|
||||||
|
notification.status.mentions.length > 0
|
||||||
|
) {
|
||||||
|
mentions = notification.status.mentions.map((mention) => {
|
||||||
|
return mention.acct;
|
||||||
|
});
|
||||||
|
statusBody.to = mentions;
|
||||||
|
}
|
||||||
|
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`${process.env.PLEROMA_INSTANCE_URL}/api/v1/statuses`,
|
`${process.env.PLEROMA_INSTANCE_URL}/api/v1/statuses`,
|
||||||
@ -166,8 +167,11 @@ const postReplyToStatus = async (
|
|||||||
|
|
||||||
if (notifications) {
|
if (notifications) {
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
notifications.map((notification) => {
|
notifications.map(async (notification) => {
|
||||||
generateOllamaRequest(notification);
|
const ollamaResponse = await generateOllamaRequest(notification);
|
||||||
|
if (ollamaResponse) {
|
||||||
|
postReplyToStatus(notification, ollamaResponse);
|
||||||
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user