From 136f126296af65f50da598d084d1485c0e40437a Mon Sep 17 00:00:00 2001 From: Mohamed Bassem Date: Sun, 27 Apr 2025 00:02:20 +0100 Subject: feat: Implement generic rule engine (#1318) * Add schema for the new rule engine * Add rule engine backend logic * Implement the worker logic and event firing * Implement the UI changesfor the rule engine * Ensure that when a referenced list or tag are deleted, the corresponding event/action is * Dont show smart lists in rule engine events * Add privacy validations for attached tag and list ids * Move the rules logic into a models --- apps/workers/openaiWorker.ts | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'apps/workers/openaiWorker.ts') diff --git a/apps/workers/openaiWorker.ts b/apps/workers/openaiWorker.ts index 7b0ae095..c8b2770e 100644 --- a/apps/workers/openaiWorker.ts +++ b/apps/workers/openaiWorker.ts @@ -19,6 +19,7 @@ import logger from "@karakeep/shared/logger"; import { buildImagePrompt, buildTextPrompt } from "@karakeep/shared/prompts"; import { OpenAIQueue, + triggerRuleEngineOnEvent, triggerSearchReindex, triggerWebhook, zOpenAIRequestSchema, @@ -377,19 +378,20 @@ async function connectTags( } // Delete old AI tags - await tx + const detachedTags = await tx .delete(tagsOnBookmarks) .where( and( eq(tagsOnBookmarks.attachedBy, "ai"), eq(tagsOnBookmarks.bookmarkId, bookmarkId), ), - ); + ) + .returning(); const allTagIds = new Set([...matchedTagIds, ...newTagIds]); // Attach new ones - await tx + const attachedTags = await tx .insert(tagsOnBookmarks) .values( [...allTagIds].map((tagId) => ({ @@ -398,7 +400,19 @@ async function connectTags( attachedBy: "ai" as const, })), ) - .onConflictDoNothing(); + .onConflictDoNothing() + .returning(); + + await triggerRuleEngineOnEvent(bookmarkId, [ + ...detachedTags.map((t) => ({ + type: "tagRemoved" as const, + tagId: t.tagId, + })), + ...attachedTags.map((t) => ({ + type: "tagAdded" as const, + tagId: t.tagId, + })), + ]); }); } -- cgit v1.2.3-70-g09d2