forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemailAutomationService.ts
430 lines (370 loc) · 16.4 KB
/
emailAutomationService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import { Service, ServiceType, IAgentRuntime, elizaLogger, Memory, ModelClass, generateText, composeContext } from "@elizaos/core";
import { EmailService } from "./emailService";
import { EmailContext, EmailOptions, GeneratedEmailContent } from "../types";
import { shouldEmailTemplate } from "../templates/shouldEmail";
import { emailFormatTemplate } from "../templates/emailFormat";
export class EmailAutomationService extends Service {
static get serviceType(): ServiceType {
return ServiceType.EMAIL_AUTOMATION;
}
get serviceType(): ServiceType {
return ServiceType.EMAIL_AUTOMATION;
}
private emailService!: EmailService;
private runtime!: IAgentRuntime;
constructor() {
super();
}
async initialize(runtime: IAgentRuntime): Promise<void> {
this.runtime = runtime;
elizaLogger.info("🔄 Initializing Email Automation Service...");
// Check if enabled
const isEnabled = runtime.getSetting('EMAIL_AUTOMATION_ENABLED')?.toLowerCase() === 'true' || false;
elizaLogger.info(`📋 Email Automation Enabled: ${isEnabled}`);
if (!isEnabled) {
elizaLogger.info("❌ Email automation is disabled");
return;
}
try {
// Required settings
const resendApiKey = runtime.getSetting('RESEND_API_KEY');
const defaultToEmail = runtime.getSetting('DEFAULT_TO_EMAIL');
const defaultFromEmail = runtime.getSetting('DEFAULT_FROM_EMAIL');
elizaLogger.debug("🔑 Checking configuration:", {
hasApiKey: !!resendApiKey,
hasToEmail: !!defaultToEmail,
hasFromEmail: !!defaultFromEmail
});
if (!resendApiKey || !defaultToEmail || !defaultFromEmail) {
throw new Error('Missing required email configuration: RESEND_API_KEY, DEFAULT_TO_EMAIL, DEFAULT_FROM_EMAIL');
}
this.emailService = new EmailService({
RESEND_API_KEY: resendApiKey,
OWNER_EMAIL: defaultToEmail
});
elizaLogger.success(`✅ Service ${this.serviceType} initialized successfully`);
elizaLogger.info("📧 Email service ready to process messages");
} catch (error) {
elizaLogger.error("❌ Failed to initialize email service:", error);
// Don't rethrow - let the service gracefully handle missing config
}
}
private async buildContext(memory: Memory): Promise<EmailContext> {
elizaLogger.debug("🔄 Building email context for message:", {
userId: memory.userId,
messageId: memory.id,
contentLength: memory.content.text.length
});
const state = await this.runtime.composeState(memory);
// Include message content in state for template access
if (state) {
state.message = {
content: memory.content,
userId: memory.userId,
id: memory.id
};
}
return {
memory,
state,
metadata: state?.metadata || {},
timestamp: new Date(),
conversationId: memory.id || ''
};
}
async evaluateMessage(memory: Memory): Promise<boolean> {
if (!this.emailService) {
elizaLogger.error("❌ Email service not initialized");
throw new Error('Missing required email configuration');
}
try {
// Build context first
const context = await this.buildContext(memory);
elizaLogger.info("🔍 Evaluating accumulated conversation for email automation:", {
text: memory.content.text,
userId: memory.userId,
roomId: memory.roomId
});
// Check if we should send an email
const shouldEmail = await this.shouldSendEmail(context);
if (shouldEmail) {
elizaLogger.info("✨ Accumulated context triggered email automation, preparing to send...");
await this.handleEmailTrigger(context);
elizaLogger.success("✅ Email processed and sent successfully");
return true;
}
elizaLogger.info("⏭️ Current context does not warrant email automation");
return false;
} catch (error) {
elizaLogger.error("❌ Error evaluating message for email:", error);
return false;
}
}
private async shouldSendEmail(context: EmailContext): Promise<boolean> {
elizaLogger.info("🤔 Evaluating if message should trigger email...");
// Now TypeScript knows we're using the full State type
elizaLogger.info("🔍 Full state debug:", {
message: context.state.message?.content?.text || 'No message text',
recentMessages: context.state.recentMessages || [],
// agentName: context.state.agentName || 'Unknown',
// bio: context.state.bio || '',
// topics: context.state.topics || [],
// rawState: context.state
});
const customPrompt = this.runtime.getSetting('EMAIL_EVALUATION_PROMPT');
const template = customPrompt || shouldEmailTemplate;
const composedContext = composeContext({
state: context.state, // Now properly typed as EmailState
template
});
// Log the actual composed context
elizaLogger.debug("📝 Template variables:", {
messageText: context.memory?.content?.text || 'No message text',
composedContextStart: composedContext.substring(0, 200)
});
const decision = await generateText({
runtime: this.runtime,
context: composedContext,
modelClass: ModelClass.SMALL
});
elizaLogger.info("📝 Final composed prompt:", {
prompt: composedContext
});
const shouldEmail = decision.includes("[EMAIL]");
elizaLogger.info(`📊 Email decision: ${shouldEmail ? "✅ Should send" : "❌ Should not send"}`, {
decision: decision.trim(),
trigger: shouldEmail
});
return shouldEmail;
}
private async handleEmailTrigger(context: EmailContext) {
try {
// Extract name and contact info from the message text
const messageLines = context.memory.content.text.split('\n');
const nameMatch = messageLines.find(line => line.includes('Cooper Ribb'));
const emailMatch = messageLines.find(line => line.includes('@'));
const phoneMatch = messageLines.find(line => line.includes('512-'));
const linkedinMatch = messageLines.find(line => line.includes('linkedin.com'));
const githubMatch = messageLines.find(line => line.includes('github.com'));
const userInfo = {
id: context.memory.userId,
displayName: nameMatch ? nameMatch.trim() : this.formatUserIdentifier(context.memory.userId),
email: emailMatch ? emailMatch.match(/[\w.-]+@[\w.-]+\.\w+/)?.[0] : '',
phone: phoneMatch ? phoneMatch.match(/\d{3}-\d{3}-\d{4}/)?.[0] : '',
linkedin: linkedinMatch ? linkedinMatch.match(/linkedin\.com\/[\w-]+/)?.[0] : '',
github: githubMatch ? githubMatch.match(/github\.com\/[\w-]+/)?.[0] : '',
platform: this.detectPlatform(context.memory.userId),
metadata: context.metadata || {}
};
// Format contact info nicely
const contactLines = [
userInfo.displayName,
[userInfo.email, userInfo.phone].filter(Boolean).join(' | '),
[
userInfo.linkedin ? `LinkedIn: ${userInfo.linkedin}` : '',
userInfo.github ? `GitHub: ${userInfo.github}` : ''
].filter(Boolean).join(' | ')
].filter(Boolean);
const messageText = context.memory.content.text;
const enhancedContext = {
...context.state,
message: messageText,
userInfo,
platform: userInfo.platform,
userId: userInfo.id,
senderName: userInfo.displayName,
contactInfo: contactLines.join('\n'),
previousMessages: messageText,
bio: '',
lore: ''
};
elizaLogger.info("🔍 Enhanced Context:", {
enhancedContext,
messageLength: messageText.length,
userDetails: userInfo
});
const emailPrompt = composeContext({
state: enhancedContext,
template: emailFormatTemplate
});
elizaLogger.info("📧 Generated Email Prompt:", {
fullPrompt: emailPrompt,
template: emailFormatTemplate
});
// Generate content with enhanced context
const formattedEmail = await generateText({
runtime: this.runtime,
context: emailPrompt,
modelClass: ModelClass.SMALL
});
elizaLogger.info("📧 LLM Generated Email:", {
formattedEmail: formattedEmail
});
// Parse and validate sections
const sections = this.parseFormattedEmail(formattedEmail);
// Add explicit validation with helpful errors
if (!sections.background) {
elizaLogger.error("Missing background section in generated email");
throw new Error("Email generation failed: Missing background section");
}
if (!sections.keyPoints || sections.keyPoints.length === 0) {
elizaLogger.error("Missing or empty key points in generated email");
throw new Error("Email generation failed: No key points generated");
}
// Create email content with ALL sections
const emailContent: GeneratedEmailContent = {
subject: sections.subject,
blocks: [
// Replace the old contact block with our formatted version
{
type: 'paragraph',
content: enhancedContext.contactInfo, // This uses our nicely formatted contactLines
metadata: {
style: 'margin-bottom: 1.5em; font-family: monospace; white-space: pre;'
}
},
{
type: 'paragraph',
content: sections.background,
metadata: {
style: 'margin-bottom: 1.5em;'
}
},
{
type: 'heading',
content: 'Key Points'
},
{
type: 'bulletList',
content: sections.keyPoints
},
// Add Technical Details section
{
type: 'heading',
content: 'Technical Details'
},
{
type: 'bulletList',
content: sections.technicalDetails || []
},
// Add Next Steps section
{
type: 'heading',
content: 'Next Steps'
},
{
type: 'bulletList',
content: sections.nextSteps || []
}
],
metadata: {
tone: 'professional',
intent: 'connection_request',
priority: 'high'
}
};
elizaLogger.info("📋 Email content prepared:", {
subject: emailContent.subject,
blocksCount: emailContent.blocks.length,
sections: {
hasBackground: !!sections.background,
keyPointsCount: sections.keyPoints.length,
technicalDetailsCount: sections.technicalDetails?.length,
nextStepsCount: sections.nextSteps?.length
}
});
const emailOptions = {
to: this.runtime.getSetting('DEFAULT_TO_EMAIL') || '',
from: this.runtime.getSetting('DEFAULT_FROM_EMAIL') || '',
headers: {
'X-Conversation-ID': context.conversationId,
'X-User-ID': userInfo.id,
'X-Platform': userInfo.platform,
'X-Display-Name': userInfo.displayName
}
};
elizaLogger.info("📤 Composing email...", {
to: emailOptions.to,
from: emailOptions.from,
conversationId: context.conversationId
});
await this.emailService.sendEmail(emailContent, emailOptions);
} catch (error) {
elizaLogger.error("❌ Email generation failed:", { error, context });
throw error;
}
}
private parseFormattedEmail(formattedEmail: string): {
subject: string;
applicant?: string;
contact?: string;
platform?: string;
background: string;
keyPoints: string[];
technicalDetails?: string[];
nextSteps: string[];
} {
const sections: any = {};
// Extract subject
const subjectMatch = formattedEmail.match(/Subject:\s*(.+?)(?=\n|$)/i);
sections.subject = subjectMatch?.[1]?.trim() || '';
// Extract applicant info
const applicantMatch = formattedEmail.match(/Applicant:\s*(.+?)(?=\n|Contact:|$)/i);
sections.applicant = applicantMatch?.[1]?.trim();
// Extract contact info
const contactMatch = formattedEmail.match(/Contact:\s*(.+?)(?=\n|Platform:|Background:|$)/i);
sections.contact = contactMatch?.[1]?.trim();
// Extract platform info
const platformMatch = formattedEmail.match(/Platform:\s*(.+?)(?=\n|Background:|$)/i);
sections.platform = platformMatch?.[1]?.trim();
// Extract background
const backgroundMatch = formattedEmail.match(/Background:\s*([\s\S]*?)(?=\n\n|Key Points:|$)/i);
sections.background = backgroundMatch?.[1]?.trim() || '';
// Extract key points
const keyPointsMatch = formattedEmail.match(/Key Points:\n([\s\S]*?)(?=\n\n|Technical Details:|Next Steps:|$)/);
sections.keyPoints = keyPointsMatch?.[1]
?.split('\n')
.map(point => point.trim())
.filter(point => point.startsWith('•'))
.map(point => point.replace('•', '').trim()) || [];
// Extract technical details
const technicalDetailsMatch = formattedEmail.match(/Technical Details:\n([\s\S]*?)(?=\n\n|Next Steps:|$)/);
sections.technicalDetails = technicalDetailsMatch?.[1]
?.split('\n')
.map(point => point.trim())
.filter(point => point.startsWith('•'))
.map(point => point.replace('•', '').trim()) || [];
// Extract next steps
const nextStepsMatch = formattedEmail.match(/Next Steps:\n([\s\S]*?)(?=\n\n|$)/);
sections.nextSteps = nextStepsMatch?.[1]
?.split('\n')
.map(point => point.trim())
.filter(point => point.startsWith('•') || /^\d+\./.test(point)) // Check for bullets or numbers
.map(point => point.replace(/^(\d+\.|•)/, '').trim()) || [];
return sections;
}
private formatUserIdentifier(userId: string): string {
// If userId is a Discord ID (typically a large number)
if (/^\d{17,19}$/.test(userId)) {
return `Discord User ${userId}`;
}
// For email addresses
if (userId.includes('@')) {
return userId;
}
// Default format
return `User ${userId}`;
}
private detectPlatform(userId: string): string {
// Discord IDs are typically 17-19 digit numbers
if (/^\d{17,19}$/.test(userId)) {
return 'discord';
}
// Email format
if (userId.includes('@')) {
return 'email';
}
// Default platform
return 'unknown';
}
}