@postalsys/email-ai-tools 1.0.3 → 1.1.1

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,7 +22,7 @@ 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
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.
@@ -30,26 +30,65 @@ Instructions:
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
32
  - If the email text mentions events, return these events as separate event objects in an "events" array
33
- - In the event object, include "description" property that describes the event
34
- - In the event object, include "location" property that defines the expected location of the event
35
- - In the event object, include "startTime" property that includes a timestamp without a timezone for the start of the event
36
- - In the event object, include "endTime" property that includes a timestamp without timezone for the expected end of the event if there is high confidence for the value
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
+ - "type" property that includes a keyword that describes the type of the event.
39
+ - "event" is a regular calendar event
40
+ - "meeting" is a scheduled meeting
41
+ - "appointment" is a scheduled appointment
42
+ - "task" if the event describes a task that needs to be completed by the due date
43
+ - "general" describes a generic event, like a fair or a competition
44
+ - If the email text mentions actions that the recipient must take, return these actions as separate action objects in an "actions" array
45
+ - In the action object, include the following properties
46
+ - "description" property that summarises the action
47
+ - "dueDate" property that includes a timestamp without a timezone for the due date of the action
37
48
  - Do not include the event in the "events" array if the confidence for it being an event is low
49
+ - Generate a security analysis of the email and store the security analysis in "riskAssessment" property as an object value.
50
+ - The "riskAssessment" object should include the following properties
51
+ - "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.
52
+ - "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.
53
+ - Your security analysis should contain (but is not limited to) the following risk factors:
54
+ - 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
55
+ - Does the sender address of the email not seem to match the persona or organization the sender email claims to be
56
+ - Does the email promise the user an extremely good financial outcome
57
+ - Does the email promise unclaimed money or goods
58
+ - Does the email offer a vague business opportunity with no specific details
59
+ - Does the email suggest there is a severe penalty if the user does not act as requested
60
+ - Does the email claim that there are technical issues with the user's email account
61
+ - Does the email offer services or activities that are not suitable for underage persons
62
+ - Does the sender's email look like a throwaway address
63
+ - Does the sender's email look like it might be spoofed
64
+ - Does the email contain attachments where the name of the attachment might be misleading and suggest a different file format than is actually used
65
+ - Does the email contain attachments that might be executable files
38
66
  - Respond with a JSON formatted structure. Do not write any other explanations.
39
67
  - Always return the response in English
40
68
 
41
69
  Facts:
42
70
  - An email consists of message headers, an attachments list, and text content
43
71
  - The email to analyze is formatted in a JSON format
72
+ - An email might contain an "authentication-results" result header that indicates if the message passed SPF, DKIM, ARC, and DMARC checks
73
+ - Having a valid SPF, DKIM, and DMARC increases the chances that the sender domain name is not spoofed
74
+ - For valid SPF, the "authentication-results" header must include "spf=pass"
75
+ - For valid DKIM, the "authentication-results" header must include "dkim=pass"
76
+ - For valid DMARC, the "authentication-results" header must include "dmarc=pass"
77
+ - For valid ARC, the "authentication-results" header must include "arc=pass"
78
+ - Throwaway email addresses might contain a word or a name and more than one number in the username of the email address
79
+ - Throwaway email addresses might use a randomly generated or hex text string as the username of the email address
80
+ - Throwaway email addresses might use free email services like gmail.com, outlook.com, hotmail.com, yahoo.com, aol.com, etc
44
81
  - An email is a reply to a previous email only if it includes an "in-reply-to" header
45
- - An email is a forwarded email only if it includes the "references" header, but not the "in-reply-to" header
82
+ - An email is a forwarded email only if it includes the "references" header but not the "in-reply-to" header
46
83
  - The email structure includes a property "headers" that contains an array of header values.
47
- - For timestamps without timezone, use the "YYYY-MM-DD hh:mm:ss" format
84
+ - For timestamps without a timezone, use the "YYYY-MM-DD hh:mm:ss" format
85
+ - For timestamps without known time, use the "YYYY-MM-DD" format
48
86
  - If the email uses relative dates, then use the date from the "date" header as the base value to calculate actual dates
49
87
  - Each header contains two properties, "key" as the header field key name and "value" as the header value without the key prefix
50
88
  - The email structure includes a property "attachments" that contains an array of attachments.
51
89
  - Each attachment includes a "filename" property that describes the file name, a "contentType" property that describes the Content-Type value of the attachment
52
90
  - The email includes a "text" property for the text content
91
+ - The email includes a "subject" property for the email subject
53
92
 
54
93
  Analyze the following email:
55
94
  `.trim();
@@ -61,17 +100,31 @@ async function generateSummary(message, apiToken, opts) {
61
100
  let gptModel = opts.gptModel || 'gpt-3.5-turbo';
62
101
 
63
102
  let text = message.text || '';
64
- if (message.html && (!text || message.html.length > text.length * 2)) {
103
+ if (message.html && (!text || message.html.length >= text.length)) {
65
104
  text = htmlToText(message.html);
66
105
  }
67
106
 
68
107
  let prompt;
108
+ const headerSeen = new Set();
69
109
 
70
110
  const content = {
71
- headers: [].concat(message.headers || []).filter(
72
- // use a whitelist
73
- header => ['from', 'to', 'cc', 'bcc', 'subject', 'mime-version', 'date', 'content-type', 'list-id'].includes(header.key)
74
- ),
111
+ headers: []
112
+ .concat(message.headers || [])
113
+ .filter(
114
+ // use a whitelist
115
+ header =>
116
+ ['from', 'to', 'cc', 'bcc', 'subject', 'mime-version', 'authentication-results', 'date', 'content-type', 'list-id'].includes(header.key)
117
+ )
118
+ .filter(header => {
119
+ // only keept the latest
120
+ if (['authentication-results'].includes(header.key)) {
121
+ if (headerSeen.has(header.key)) {
122
+ return false;
123
+ }
124
+ headerSeen.add(header.key);
125
+ }
126
+ return true;
127
+ }),
75
128
  attachments: [].concat(message.attachments || []).map(attachment => ({ filename: attachment.filename, contentType: attachment.contentType }))
76
129
  };
77
130
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@postalsys/email-ai-tools",
3
- "version": "1.0.3",
3
+ "version": "1.1.1",
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
  }