Skip to content

Commit

Permalink
send rule as part of call webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
elie222 committed Jan 1, 2025
1 parent 6724fbe commit 30eb1a6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 24 deletions.
48 changes: 32 additions & 16 deletions apps/web/utils/ai/actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type { gmail_v1 } from "@googleapis/gmail";
import { draftEmail, forwardEmail, sendEmail } from "@/utils/gmail/mail";
import { ActionType, type ExecutedAction } from "@prisma/client";
import {
ActionType,
type ExecutedRule,
type ExecutedAction,
} from "@prisma/client";
import type { PartialRecord } from "@/utils/types";
import {
archiveThread,
Expand Down Expand Up @@ -43,6 +47,7 @@ type ActionFunction<T extends Omit<ActionItem, "type" | "id">> = (
email: EmailForAction,
args: T,
userEmail: string,
executedRule: ExecutedRule,
) => Promise<any>;

export type Properties = PartialRecord<
Expand Down Expand Up @@ -393,15 +398,25 @@ const call_webhook: ActionFunction<any> = async (
email: EmailForAction,
args: { url: string },
userEmail: string,
executedRule: ExecutedRule,
) => {
await callWebhook(userEmail, args.url, {
threadId: email.threadId,
messageId: email.messageId,
subject: email.subject,
from: email.from,
cc: email.cc,
bcc: email.bcc,
headerMessageId: email.headerMessageId,
email: {
threadId: email.threadId,
messageId: email.messageId,
subject: email.subject,
from: email.from,
cc: email.cc,
bcc: email.bcc,
headerMessageId: email.headerMessageId,
},
executedRule: {
id: executedRule.id,
ruleId: executedRule.ruleId,
reason: executedRule.reason,
automated: executedRule.automated,
createdAt: executedRule.createdAt,
},
});
};

Expand All @@ -410,6 +425,7 @@ export const runActionFunction = async (
email: EmailForAction,
action: ActionItem,
userEmail: string,
executedRule: ExecutedRule,
) => {
logger.info("Running action", {
actionType: action.type,
Expand All @@ -421,21 +437,21 @@ export const runActionFunction = async (
const { type, ...args } = action;
switch (type) {
case ActionType.ARCHIVE:
return archive(gmail, email, args, userEmail);
return archive(gmail, email, args, userEmail, executedRule);
case ActionType.LABEL:
return label(gmail, email, args, userEmail);
return label(gmail, email, args, userEmail, executedRule);
case ActionType.DRAFT_EMAIL:
return draft(gmail, email, args, userEmail);
return draft(gmail, email, args, userEmail, executedRule);
case ActionType.REPLY:
return reply(gmail, email, args, userEmail);
return reply(gmail, email, args, userEmail, executedRule);
case ActionType.SEND_EMAIL:
return send_email(gmail, email, args, userEmail);
return send_email(gmail, email, args, userEmail, executedRule);
case ActionType.FORWARD:
return forward(gmail, email, args, userEmail);
return forward(gmail, email, args, userEmail, executedRule);
case ActionType.MARK_SPAM:
return mark_spam(gmail, email, args, userEmail);
return mark_spam(gmail, email, args, userEmail, executedRule);
case ActionType.CALL_WEBHOOK:
return call_webhook(gmail, email, args, userEmail);
return call_webhook(gmail, email, args, userEmail, executedRule);
default:
throw new Error(`Unknown action: ${action}`);
}
Expand Down
2 changes: 1 addition & 1 deletion apps/web/utils/ai/choose-rule/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export async function executeAct({

for (const action of executedRule.actionItems) {
try {
await runActionFunction(gmail, email, action, userEmail);
await runActionFunction(gmail, email, action, userEmail, executedRule);
} catch (error) {
await prisma.executedRule.update({
where: { id: executedRule.id },
Expand Down
21 changes: 14 additions & 7 deletions apps/web/utils/webhook.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { createScopedLogger } from "@/utils/logger";
import prisma from "@/utils/prisma";
import { sleep } from "@/utils/sleep";
import type { ExecutedRule } from "@prisma/client";

const logger = createScopedLogger("webhook");

type WebhookPayload = {
threadId: string;
messageId: string;
subject: string;
from: string;
cc?: string;
bcc?: string;
headerMessageId: string;
email: {
threadId: string;
messageId: string;
subject: string;
from: string;
cc?: string;
bcc?: string;
headerMessageId: string;
};
executedRule: Pick<
ExecutedRule,
"id" | "ruleId" | "reason" | "automated" | "createdAt"
>;
};

export const callWebhook = async (
Expand Down

0 comments on commit 30eb1a6

Please sign in to comment.