sanity checking, do not duplicate responses
This commit is contained in:
		
							
								
								
									
										33
									
								
								src/main.ts
									
									
									
									
									
								
							
							
						
						
									
										33
									
								
								src/main.ts
									
									
									
									
									
								
							| @ -3,6 +3,7 @@ import { | ||||
|   OllamaResponse, | ||||
|   NewStatusBody, | ||||
|   Notification, | ||||
|   OllamaConfigOptions, | ||||
| } from "../types.js"; | ||||
| import striptags from "striptags"; | ||||
| import { PrismaClient } from "../generated/prisma/client.js"; | ||||
| @ -51,7 +52,7 @@ const alreadyRespondedTo = async ( | ||||
| ): Promise<boolean> => { | ||||
|   try { | ||||
|     const duplicate = await prisma.response.findFirst({ | ||||
|       where: { pleromaNotificationId: notification.status.id }, | ||||
|       where: { pleromaNotificationId: notification.id, isProcessing: true }, | ||||
|     }); | ||||
|     if (duplicate) { | ||||
|       return true; | ||||
| @ -67,12 +68,13 @@ const storePromptData = async ( | ||||
|   ollamaResponseBody: OllamaResponse | ||||
| ) => { | ||||
|   try { | ||||
|     await prisma.response.create({ | ||||
|     await prisma.response.updateMany({ | ||||
|       where: { pleromaNotificationId: notification.id }, | ||||
|       data: { | ||||
|         response: ollamaResponseBody.response, | ||||
|         request: striptags(notification.status.content), | ||||
|         request: trimInputData(notification.status.content), | ||||
|         to: notification.account.fqn, | ||||
|         pleromaNotificationId: notification.status.id, | ||||
|         isProcessing: false, | ||||
|       }, | ||||
|     }); | ||||
|   } catch (error: any) { | ||||
| @ -87,6 +89,18 @@ const trimInputData = (input: string) => { | ||||
|   return split.slice(promptStringIndex + 1).join(" "); // returns everything after the !prompt | ||||
| }; | ||||
|  | ||||
| const recordPendingResponse = async (notification: Notification) => { | ||||
|   try { | ||||
|     await prisma.response.create({ | ||||
|       data: { | ||||
|         pleromaNotificationId: notification.id, | ||||
|       }, | ||||
|     }); | ||||
|   } catch (error: any) { | ||||
|     throw new Error(error.message); | ||||
|   } | ||||
| }; | ||||
|  | ||||
| const generateOllamaRequest = async ( | ||||
|   notification: Notification | ||||
| ): Promise<OllamaResponse | undefined> => { | ||||
| @ -107,7 +121,12 @@ const generateOllamaRequest = async ( | ||||
|       if (await alreadyRespondedTo(notification)) { | ||||
|         return; | ||||
|       } | ||||
|       await recordPendingResponse(notification); | ||||
|       await storeUserData(notification); | ||||
|       const ollamaConfig: OllamaConfigOptions = { | ||||
|         temperature: 1.2, | ||||
|         num_predict: 400, | ||||
|       }; | ||||
|       const ollamaRequestBody: OllamaRequest = { | ||||
|         model: process.env.OLLAMA_MODEL as string, | ||||
|         system: process.env.OLLAMA_SYSTEM_PROMPT as string, | ||||
| @ -115,6 +134,7 @@ const generateOllamaRequest = async ( | ||||
|           notification.status.content | ||||
|         )}`, | ||||
|         stream: false, | ||||
|         options: ollamaConfig, | ||||
|       }; | ||||
|       const response = await fetch(`${process.env.OLLAMA_URL}/api/generate`, { | ||||
|         method: "POST", | ||||
| @ -177,6 +197,11 @@ const deleteNotification = async (notification: Notification) => { | ||||
|     if (!notification.id) { | ||||
|       return; | ||||
|     } | ||||
|     await prisma.response.updateMany({ | ||||
|       // this is probably not the best way to do this, but since we may have duplicate notifications, we have to update all of them - probably won't scale (lmao) | ||||
|       where: { pleromaNotificationId: notification.id }, | ||||
|       data: { isProcessing: false }, | ||||
|     }); | ||||
|     const response = await fetch( | ||||
|       `${process.env.PLEROMA_INSTANCE_URL}/api/v1/notifications/${notification.id}/dismiss`, | ||||
|       { | ||||
|  | ||||
		Reference in New Issue
	
	Block a user