aboutsummaryrefslogtreecommitdiffstats
path: root/apps/workers/openaiWorker.ts
diff options
context:
space:
mode:
authorMohamed Bassem <me@mbassem.com>2025-04-27 00:02:20 +0100
committerGitHub <noreply@github.com>2025-04-27 00:02:20 +0100
commit136f126296af65f50da598d084d1485c0e40437a (patch)
tree2725c7932ebbcb9b48b5af98eb9b72329a400260 /apps/workers/openaiWorker.ts
parentca47be7fe7be128f459c37614a04902a873fe289 (diff)
downloadkarakeep-136f126296af65f50da598d084d1485c0e40437a.tar.zst
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
Diffstat (limited to 'apps/workers/openaiWorker.ts')
-rw-r--r--apps/workers/openaiWorker.ts22
1 files changed, 18 insertions, 4 deletions
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,
+ })),
+ ]);
});
}