some abstraction and I'm gonna kill myself
This commit is contained in:
		
							
								
								
									
										24
									
								
								src/main.ts
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								src/main.ts
									
									
									
									
									
								
							| @ -21,6 +21,7 @@ import { | |||||||
|   recordPendingResponse, |   recordPendingResponse, | ||||||
|   trimInputData, |   trimInputData, | ||||||
|   selectRandomEmoji, |   selectRandomEmoji, | ||||||
|  |   shouldContinue, | ||||||
| } from "./util.js"; | } from "./util.js"; | ||||||
|  |  | ||||||
| export const prisma = new PrismaClient(); | export const prisma = new PrismaClient(); | ||||||
| @ -59,25 +60,10 @@ const ollamaConfig: OllamaConfigOptions = { | |||||||
| const generateOllamaRequest = async ( | const generateOllamaRequest = async ( | ||||||
|   notification: Notification |   notification: Notification | ||||||
| ): Promise<OllamaResponse | undefined> => { | ): Promise<OllamaResponse | undefined> => { | ||||||
|   const { |   const { whitelistOnly, ollamaModel, ollamaSystemPrompt, ollamaUrl } = | ||||||
|     whitelistOnly, |     envConfig; | ||||||
|     ollamaModel, |  | ||||||
|     ollamaSystemPrompt, |  | ||||||
|     ollamaUrl, |  | ||||||
|     botAccountId, |  | ||||||
|   } = envConfig; |  | ||||||
|   try { |   try { | ||||||
|     console.log(trimInputData(notification.status.content)); |     if (shouldContinue(notification)) { | ||||||
|     if ( |  | ||||||
|       // striptags(notification.status.content).includes("!prompt") && |  | ||||||
|       !notification.status.account.bot && // sanity check, sort of |  | ||||||
|       notification.type === "mention" && |  | ||||||
|       (notification.status.in_reply_to_account_id === botAccountId || |  | ||||||
|         notification.status.in_reply_to_account_id === null) && |  | ||||||
|       trimInputData(notification.status.content).split(" ").includes("Lexi") |  | ||||||
|       // only reply to mentions when the bot is the direct recipient or when an @ is at the top level of a conversation chain, or when the AI is @ directly |  | ||||||
|       // notification.status.visibility !== "private" // for safety, let's only respond to public messages |  | ||||||
|     ) { |  | ||||||
|       if (whitelistOnly && !isFromWhitelistedDomain(notification)) { |       if (whitelistOnly && !isFromWhitelistedDomain(notification)) { | ||||||
|         await deleteNotification(notification); |         await deleteNotification(notification); | ||||||
|         return; |         return; | ||||||
| @ -87,10 +73,8 @@ const generateOllamaRequest = async ( | |||||||
|       } |       } | ||||||
|       await recordPendingResponse(notification); |       await recordPendingResponse(notification); | ||||||
|       await storeUserData(notification); |       await storeUserData(notification); | ||||||
|       // console.log(trimInputData(notification.status.content)); |  | ||||||
|       const ollamaRequestBody: OllamaRequest = { |       const ollamaRequestBody: OllamaRequest = { | ||||||
|         model: ollamaModel, |         model: ollamaModel, | ||||||
|         // prompt: trimInputData(notification.status.content), |  | ||||||
|         prompt: `${notification.status.account.fqn} says: ${trimInputData( |         prompt: `${notification.status.account.fqn} says: ${trimInputData( | ||||||
|           notification.status.content |           notification.status.content | ||||||
|         )}`, |         )}`, | ||||||
|  | |||||||
							
								
								
									
										29
									
								
								src/util.ts
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								src/util.ts
									
									
									
									
									
								
							| @ -34,6 +34,34 @@ const recordPendingResponse = async (notification: Notification) => { | |||||||
|   } |   } | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | const shouldContinue = (notification: Notification) => { | ||||||
|  |   // wow this is bad | ||||||
|  |   try { | ||||||
|  |     const { botAccountId } = envConfig; | ||||||
|  |     const statusContent = trimInputData(notification.status.content); | ||||||
|  |     if ( | ||||||
|  |       notification.status.visibility !== "private" && | ||||||
|  |       !notification.account.bot && | ||||||
|  |       notification.type === "mention" | ||||||
|  |     ) { | ||||||
|  |       if (notification.status.in_reply_to_account_id === botAccountId) { | ||||||
|  |         return true; | ||||||
|  |       } else if ( | ||||||
|  |         notification.status.in_reply_to_account_id !== botAccountId && | ||||||
|  |         statusContent.includes("Lexi") | ||||||
|  |       ) { | ||||||
|  |         return true; | ||||||
|  |       } else { | ||||||
|  |         return false; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   } catch (error: unknown) { | ||||||
|  |     if (error instanceof Error) { | ||||||
|  |       throw new Error(error.message); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | }; | ||||||
|  |  | ||||||
| const isFromWhitelistedDomain = (notification: Notification): boolean => { | const isFromWhitelistedDomain = (notification: Notification): boolean => { | ||||||
|   try { |   try { | ||||||
|     const domain = notification.status.account.fqn.split("@")[1]; |     const domain = notification.status.account.fqn.split("@")[1]; | ||||||
| @ -76,4 +104,5 @@ export { | |||||||
|   trimInputData, |   trimInputData, | ||||||
|   recordPendingResponse, |   recordPendingResponse, | ||||||
|   isFromWhitelistedDomain, |   isFromWhitelistedDomain, | ||||||
|  |   shouldContinue, | ||||||
| }; | }; | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user