Skip to content

Commit e9d32f4

Browse files
authored
fix: add draft generation check (#1855)
# Prevent duplicate draft generation by checking existing drafts ## Description This PR enhances the `shouldGenerateDraft` function to prevent generating duplicate drafts for the same email thread. The function now: 1. Checks if a draft already exists for the thread by calling the Gmail API 2. Skips draft generation if a draft is already present for that thread 3. Logs the decision for better debugging <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved draft generation logic to prevent creating duplicate drafts for the same thread. * Enhanced workflow reliability by ensuring eligibility checks are properly awaited. * **Chores** * Added detailed logging for draft generation, including thread message counts and sender information. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 4c86d59 commit e9d32f4

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

apps/server/src/thread-workflow-utils/index.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import type { IGetThreadResponse } from '../lib/driver/types';
22
import { composeEmail } from '../trpc/routes/ai/compose';
3+
import { getZeroAgent } from '../lib/server-utils';
34
import { type ParsedMessage } from '../types';
45
import { connection } from '../db/schema';
56

6-
const shouldGenerateDraft = (
7+
const shouldGenerateDraft = async (
78
thread: IGetThreadResponse,
89
foundConnection: typeof connection.$inferSelect,
9-
): boolean => {
10+
): Promise<boolean> => {
1011
if (!thread.messages || thread.messages.length === 0) return false;
1112

1213
const latestMessage = thread.messages[thread.messages.length - 1];
@@ -37,6 +38,29 @@ const shouldGenerateDraft = (
3738
}
3839
}
3940

41+
try {
42+
const agent = await getZeroAgent(foundConnection.id);
43+
44+
const threadId = thread.messages[0]?.threadId;
45+
if (!threadId) {
46+
console.log('[SHOULD_GENERATE_DRAFT] No thread ID found, skipping draft check');
47+
return true;
48+
}
49+
50+
const draftsResponse: any = await agent.listDrafts({ maxResults: 100 });
51+
52+
const hasDraftForThread = draftsResponse.threads.some((draft: any) => {
53+
return draft.id === threadId;
54+
});
55+
56+
if (hasDraftForThread) {
57+
console.log(`[SHOULD_GENERATE_DRAFT] Draft already exists for thread ${threadId}, skipping`);
58+
return false;
59+
}
60+
} catch (error) {
61+
console.error('[SHOULD_GENERATE_DRAFT] Error checking for existing drafts:', error);
62+
}
63+
4064
return true;
4165
};
4266

apps/server/src/thread-workflow-utils/workflow-engine.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export const createDefaultWorkflows = (): WorkflowEngine => {
115115
if (executionCheck?.alreadyExecuted) {
116116
return false;
117117
}
118-
return shouldGenerateDraft(context.thread, context.foundConnection);
118+
return await shouldGenerateDraft(context.thread, context.foundConnection);
119119
},
120120
action: async (context) => {
121121
console.log('[WORKFLOW_ENGINE] Thread eligible for draft generation', {

apps/server/src/thread-workflow-utils/workflow-functions.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export type WorkflowFunction = (context: WorkflowContext) => Promise<any>;
1818

1919
export const workflowFunctions: Record<string, WorkflowFunction> = {
2020
shouldGenerateDraft: async (context) => {
21-
return shouldGenerateDraft(context.thread, context.foundConnection);
21+
return await shouldGenerateDraft(context.thread, context.foundConnection);
2222
},
2323

2424
checkWorkflowExecution: async (context) => {
@@ -80,6 +80,11 @@ export const workflowFunctions: Record<string, WorkflowFunction> = {
8080

8181
generateAutomaticDraft: async (context) => {
8282
console.log('[WORKFLOW_FUNCTIONS] Generating automatic draft for thread:', context.threadId);
83+
console.log('[WORKFLOW_FUNCTIONS] Thread has', context.thread.messages.length, 'messages');
84+
console.log(
85+
'[WORKFLOW_FUNCTIONS] Latest message from:',
86+
context.thread.messages[context.thread.messages.length - 1]?.sender?.email,
87+
);
8388

8489
const draftContent = await generateAutomaticDraft(
8590
context.connectionId,

0 commit comments

Comments
 (0)