@postalsys/email-ai-tools 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/examples/risk.js CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const fs = require('fs').promises;
4
- const { riskAnalysis } = require('..');
4
+ const { generateSummary } = require('..');
5
5
  const simpleParser = require('mailparser').simpleParser;
6
6
  const libmime = require('libmime');
7
7
  const Path = require('path');
@@ -14,12 +14,13 @@ async function analyzeEmail(path) {
14
14
 
15
15
  const parsed = await simpleParser(eml);
16
16
 
17
- const result = await riskAnalysis(
17
+ const result = await generateSummary(
18
18
  {
19
19
  headers: parsed.headerLines.map(header => libmime.decodeHeader(header.line)),
20
20
  attachments: parsed.attachments,
21
21
  html: parsed.html,
22
- text: parsed.text
22
+ text: parsed.text,
23
+ subject: parsed.subject
23
24
  },
24
25
  process.env.OPENAI_API_KEY,
25
26
  {
@@ -22,25 +22,63 @@ You do not comment or explain anything.
22
22
 
23
23
  const USER_PROMPT = `
24
24
  Instructions:
25
- - You are an executive assistant scanning incoming emails to inform your superiors about what is important and what is not.
25
+ - You are an executive assistant scanning incoming emails to inform your superiors about what is important and what is not, and also to inform about obvious fraud attempts.
26
26
  - Your task is to monitor and analyze incoming emails, which consist of the message headers, a list of attachments, and text content
27
- - Describe the sentiment of the email using one word. Use either "positive", "neutral" or "negative". Include this value in the response as a "sentiment" property.
27
+ - Describe the sentiment of the email using one word. Use either "positive", "neutral", or "negative". Include this value in the response as a "sentiment" property.
28
28
  - Generate a one-sentence summary of the email. Include this value in the response as a "summary" property.
29
29
  - Does it seem like the sender of the email would expect a reply to this email? Include this information in the response as a "shouldReply" property with the value "true" if they expect it and "false" if not.
30
30
  - If this email is a reply to a previous email or a forwarded email, then extract the text content that only the email's sender wrote, and include this as a "replyText" property in the response.
31
31
  - Do not include message signatures in the extracted reply text
32
+ - If the email text mentions events, return these events as separate event objects in an "events" array
33
+ - In the event object include the following properties
34
+ - "description" property that describes the event
35
+ - "location" property that defines the expected location of the event
36
+ - "startTime" property that includes a timestamp without a timezone for the start of the event
37
+ - "endTime" property that includes a timestamp without timezone for the expected end of the event if there is high confidence for the value
38
+ - Do not include the event in the "events" array if the confidence for it being an event is low
39
+ - Generate a security analysis of the email and store the security analysis in "riskAssessment" property as an object value.
40
+ - The "riskAssessment" object should include the following properties
41
+ - "risk" property that contains a risk score for the email using the following scale: 1 - 5 (where 1 is low risk, and 5 is high risk), taking into account what may happen if a user acts by the instructions given in the email.
42
+ - "assessment" property, a single-sentence assessment text that includes details about issues that increase the risk score, do not disclose details that decrease the risk score or do not affect it.
43
+ - Your security analysis should contain (but is not limited to) the following risk factors:
44
+ - Does the email include links with domain names that contain typos or homoglyphs that might mislead the user about the actual target of the link
45
+ - Does the sender address of the email not seem to match the persona or organization the sender email claims to be
46
+ - Does the email promise the user an extremely good financial outcome
47
+ - Does the email promise unclaimed money or goods
48
+ - Does the email offer a vague business opportunity with no specific details
49
+ - Does the email suggest there is a severe penalty if the user does not act as requested
50
+ - Does the email claim that there are technical issues with the user's email account
51
+ - Does the email offer services or activities that are not suitable for underage persons
52
+ - Does the sender's email look like a throwaway address
53
+ - Does the sender's email look like it might be spoofed
54
+ - Does the email contain attachments where the name of the attachment might be misleading and suggest a different file format than is actually used
55
+ - Does the email contain attachments that might be executable files
32
56
  - Respond with a JSON formatted structure. Do not write any other explanations.
57
+ - Always return the response in English
33
58
 
34
59
  Facts:
35
60
  - An email consists of message headers, an attachments list, and text content
36
61
  - The email to analyze is formatted in a JSON format
37
- - An email is a reply to a previous email only if it includes a "in-reply-to" header
38
- - An email is a forwarded email only if it includes the "references" header , but not the "in-reply-to" header
62
+ - An email might contain an "authentication-results" result header that indicates if the message passed SPF, DKIM, ARC, and DMARC checks
63
+ - Having a valid SPF, DKIM, and DMARC increases the chances that the sender domain name is not spoofed
64
+ - For valid SPF, the "authentication-results" header must include "spf=pass"
65
+ - For valid DKIM, the "authentication-results" header must include "dkim=pass"
66
+ - For valid DMARC, the "authentication-results" header must include "dmarc=pass"
67
+ - For valid ARC, the "authentication-results" header must include "arc=pass"
68
+ - Throwaway email addresses might contain a word or a name and more than one number in the username of the email address
69
+ - Throwaway email addresses might use a randomly generated or hex text string as the username of the email address
70
+ - Throwaway email addresses might use free email services like gmail.com, outlook.com, hotmail.com, yahoo.com, aol.com, etc
71
+ - An email is a reply to a previous email only if it includes an "in-reply-to" header
72
+ - An email is a forwarded email only if it includes the "references" header, but not the "in-reply-to" header
39
73
  - The email structure includes a property "headers" that contains an array of header values.
74
+ - For timestamps without timezone, use the "YYYY-MM-DD hh:mm:ss" format
75
+ - For timestamps without known time, use the "YYYY-MM-DD" format
76
+ - If the email uses relative dates, then use the date from the "date" header as the base value to calculate actual dates
40
77
  - Each header contains two properties, "key" as the header field key name and "value" as the header value without the key prefix
41
78
  - The email structure includes a property "attachments" that contains an array of attachments.
42
79
  - Each attachment includes a "filename" property that describes the file name, a "contentType" property that describes the Content-Type value of the attachment
43
80
  - The email includes a "text" property for the text content
81
+ - The email includes a "subject" property for the email subject
44
82
 
45
83
  Analyze the following email:
46
84
  `.trim();
@@ -52,17 +90,31 @@ async function generateSummary(message, apiToken, opts) {
52
90
  let gptModel = opts.gptModel || 'gpt-3.5-turbo';
53
91
 
54
92
  let text = message.text || '';
55
- if (message.html && (!text || message.html.length > text.length * 2)) {
93
+ if (message.html && (!text || message.html.length >= text.length)) {
56
94
  text = htmlToText(message.html);
57
95
  }
58
96
 
59
97
  let prompt;
98
+ const headerSeen = new Set();
60
99
 
61
100
  const content = {
62
- headers: [].concat(message.headers || []).filter(
63
- // use a whitelist
64
- header => ['from', 'to', 'cc', 'bcc', 'subject', 'mime-version', 'date', 'content-type', 'list-id'].includes(header.key)
65
- ),
101
+ headers: []
102
+ .concat(message.headers || [])
103
+ .filter(
104
+ // use a whitelist
105
+ header =>
106
+ ['from', 'to', 'cc', 'bcc', 'subject', 'mime-version', 'authentication-results', 'date', 'content-type', 'list-id'].includes(header.key)
107
+ )
108
+ .filter(header => {
109
+ // only keept the latest
110
+ if (['authentication-results'].includes(header.key)) {
111
+ if (headerSeen.has(header.key)) {
112
+ return false;
113
+ }
114
+ headerSeen.add(header.key);
115
+ }
116
+ return true;
117
+ }),
66
118
  attachments: [].concat(message.attachments || []).map(attachment => ({ filename: attachment.filename, contentType: attachment.contentType }))
67
119
  };
68
120
 
@@ -193,6 +245,20 @@ ${JSON.stringify(content)}`;
193
245
  if (typeof values.shouldReply !== 'undefined' && typeof values.shouldReply !== 'boolean') {
194
246
  delete values.shouldReply;
195
247
  }
248
+ if (values && values.events) {
249
+ values.events = []
250
+ .concat(values.events || [])
251
+ .filter(event => event.startTime)
252
+ .map(event => {
253
+ let result = {};
254
+ for (let key of Object.keys(event)) {
255
+ if (event[key] !== '' && event[key] !== null) {
256
+ result[key] = event[key];
257
+ }
258
+ }
259
+ return result;
260
+ });
261
+ }
196
262
  } catch (err) {
197
263
  let error = new Error('Failed to parse output from OpenAI API');
198
264
  error.textContent = output;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@postalsys/email-ai-tools",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "Email AI tools",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -23,9 +23,9 @@
23
23
  "mailparser": "3.6.4"
24
24
  },
25
25
  "dependencies": {
26
- "@postalsys/email-text-tools": "2.0.0",
26
+ "@postalsys/email-text-tools": "2.0.1",
27
27
  "gpt3-tokenizer": "1.1.5",
28
28
  "libmime": "5.2.1",
29
- "undici": "5.22.0"
29
+ "undici": "5.22.1"
30
30
  }
31
31
  }