-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Rule history #497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Rule history #497
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
apps/web/prisma/migrations/20250609204102_rule_history/migration.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| -- AlterTable | ||
| ALTER TABLE "Rule" ADD COLUMN "promptText" TEXT; | ||
|
|
||
| -- CreateTable | ||
| CREATE TABLE "RuleHistory" ( | ||
| "id" TEXT NOT NULL, | ||
| "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
elie222 marked this conversation as resolved.
|
||
| "ruleId" TEXT NOT NULL, | ||
| "version" INTEGER NOT NULL, | ||
| "triggerType" TEXT NOT NULL, | ||
| "promptText" TEXT, | ||
| "name" TEXT NOT NULL, | ||
| "instructions" TEXT, | ||
| "enabled" BOOLEAN NOT NULL, | ||
| "automate" BOOLEAN NOT NULL, | ||
| "runOnThreads" BOOLEAN NOT NULL, | ||
| "conditionalOperator" TEXT NOT NULL, | ||
| "from" TEXT, | ||
| "to" TEXT, | ||
| "subject" TEXT, | ||
| "body" TEXT, | ||
| "categoryFilterType" TEXT, | ||
| "systemType" TEXT, | ||
| "actions" JSONB NOT NULL, | ||
| "categoryFilters" JSONB, | ||
|
|
||
| CONSTRAINT "RuleHistory_pkey" PRIMARY KEY ("id") | ||
| ); | ||
|
|
||
| -- CreateIndex | ||
| CREATE INDEX "RuleHistory_ruleId_createdAt_idx" ON "RuleHistory"("ruleId", "createdAt"); | ||
|
|
||
| -- CreateIndex | ||
| CREATE UNIQUE INDEX "RuleHistory_ruleId_version_key" ON "RuleHistory"("ruleId", "version"); | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "RuleHistory" ADD CONSTRAINT "RuleHistory_ruleId_fkey" FOREIGN KEY ("ruleId") REFERENCES "Rule"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import prisma from "@/utils/prisma"; | ||
| import type { RuleWithRelations } from "@/utils/ai/rule/create-prompt-from-rule"; | ||
|
|
||
| export type RuleHistoryTrigger = | ||
| | "ai_update" // AI updates existing rule from prompt changes | ||
| | "manual_update" // User manually edits existing rule | ||
| | "ai_creation" // AI creates rule from parsing prompts | ||
| | "manual_creation" // User manually creates new rule | ||
| | "system_creation" // System automatically creates rule (e.g., reply tracker) | ||
| | "system_update"; // System automatically updates rule | ||
|
|
||
| /** | ||
| * Creates a complete snapshot of a rule in the RuleHistory table | ||
| */ | ||
| export async function createRuleHistory({ | ||
| rule, | ||
| triggerType, | ||
| }: { | ||
| rule: RuleWithRelations; | ||
| triggerType: RuleHistoryTrigger; | ||
| }) { | ||
| // Get the current version number for this rule | ||
| const lastHistory = await prisma.ruleHistory.findFirst({ | ||
| where: { ruleId: rule.id }, | ||
| orderBy: { version: "desc" }, | ||
| select: { version: true }, | ||
| }); | ||
|
|
||
| const nextVersion = (lastHistory?.version ?? 0) + 1; | ||
|
elie222 marked this conversation as resolved.
|
||
|
|
||
| // Serialize actions to JSON | ||
| const actionsSnapshot = rule.actions.map((action) => ({ | ||
| id: action.id, | ||
| type: action.type, | ||
| label: action.label, | ||
| subject: action.subject, | ||
| content: action.content, | ||
| to: action.to, | ||
| cc: action.cc, | ||
| bcc: action.bcc, | ||
| url: action.url, | ||
| })); | ||
|
|
||
| // Serialize category filters to JSON | ||
| const categoryFiltersSnapshot = rule.categoryFilters?.map((category) => ({ | ||
| id: category.id, | ||
| name: category.name, | ||
| description: category.description, | ||
| })); | ||
|
|
||
| return prisma.ruleHistory.create({ | ||
| data: { | ||
| ruleId: rule.id, | ||
| name: rule.name, | ||
| instructions: rule.instructions, | ||
| enabled: rule.enabled, | ||
| automate: rule.automate, | ||
| runOnThreads: rule.runOnThreads, | ||
| conditionalOperator: rule.conditionalOperator, | ||
| from: rule.from, | ||
| to: rule.to, | ||
| subject: rule.subject, | ||
| body: rule.body, | ||
| categoryFilterType: rule.categoryFilterType, | ||
| systemType: rule.systemType, | ||
| promptText: rule.promptText, | ||
| actions: actionsSnapshot, | ||
| categoryFilters: categoryFiltersSnapshot, | ||
| triggerType, | ||
| // Note: this is unique and can fail in race conditions. Not a big deal for now. | ||
| version: nextVersion, | ||
| }, | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| v1.3.29 | ||
| v1.4.0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.