@postalsys/email-ai-tools 1.9.2 → 1.11.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/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +55 -0
- package/examples/summary.js +2 -3
- package/lib/embeddings-query.js +4 -4
- package/lib/generate-summary.js +159 -72
- package/lib/risk-analysis.js +22 -7
- package/package.json +4 -4
- package/release-please-config.json +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,60 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.11.0](https://github.com/postalsys/email-ai-tools/compare/v1.10.0...v1.11.0) (2025-11-18)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* **chat:** Added question sorting method ([6b5ce50](https://github.com/postalsys/email-ai-tools/commit/6b5ce506f7d43e03b1dd9f6ae0142c976a4eb88e))
|
|
9
|
+
* **embeddings:** Support for embeddings query ([5c40f15](https://github.com/postalsys/email-ai-tools/commit/5c40f1504f969cb39270edf28e06e2ea65681fc1))
|
|
10
|
+
* **list-models:** new method to list available models ([7ddb033](https://github.com/postalsys/email-ai-tools/commit/7ddb0334d1cc7b702533aec004b249ef5b1654b7))
|
|
11
|
+
* **models:** Support GTP-3 Instruct ([6a15fc4](https://github.com/postalsys/email-ai-tools/commit/6a15fc4fdda175485fc22b2bc5fa68a281ee65f5))
|
|
12
|
+
* **openai:** Allow to specify base API url ([04ebd3d](https://github.com/postalsys/email-ai-tools/commit/04ebd3d5adfd88ec7a681bbffed4fb302c982846))
|
|
13
|
+
* upgrade to larger context models and improve prompt engineering ([27564ce](https://github.com/postalsys/email-ai-tools/commit/27564ce9fdebb79fcb2803a2c47a895881835874))
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
### Bug Fixes
|
|
17
|
+
|
|
18
|
+
* better model listing ([05fff58](https://github.com/postalsys/email-ai-tools/commit/05fff589726c39f3b82691d4383e8d9242fc172f))
|
|
19
|
+
* bumped deps ([45be822](https://github.com/postalsys/email-ai-tools/commit/45be822bb45e5f74455f4415aa61b8cf77fa2352))
|
|
20
|
+
* bumped deps ([89c148b](https://github.com/postalsys/email-ai-tools/commit/89c148ba37ee869bfc7c06350efefa7a50fbd630))
|
|
21
|
+
* bumped deps ([436db20](https://github.com/postalsys/email-ai-tools/commit/436db20f6c9800a20e10a90622b3a5c94dd611ad))
|
|
22
|
+
* bumped deps ([c253ed0](https://github.com/postalsys/email-ai-tools/commit/c253ed026313672adf9432412ab45d0e14870954))
|
|
23
|
+
* Bumped deps ([59e3b08](https://github.com/postalsys/email-ai-tools/commit/59e3b0844c0cdb8a40206c19ed86528e4d7269c5))
|
|
24
|
+
* Bumped deps ([95056e4](https://github.com/postalsys/email-ai-tools/commit/95056e471721571bc4ed19a7330745a1540d4632))
|
|
25
|
+
* Bumped deps ([008ae6c](https://github.com/postalsys/email-ai-tools/commit/008ae6c6fa95b4aff85fd8229d97837ea068e0bd))
|
|
26
|
+
* Bumped deps ([33b87cd](https://github.com/postalsys/email-ai-tools/commit/33b87cdd3f3f26e8ceade75ff99d12219c7c2eb6))
|
|
27
|
+
* bumped email-text-tools ([1d487df](https://github.com/postalsys/email-ai-tools/commit/1d487df5e411bb2ccf4d84592e5c138634cc1181))
|
|
28
|
+
* **chat:** fixed response parsing for markdown responses ([34c4fdd](https://github.com/postalsys/email-ai-tools/commit/34c4fdda3ee7eca3a66d5f469cb4ee7a1f777083))
|
|
29
|
+
* **chat:** generate and return chat topic ([47755fb](https://github.com/postalsys/email-ai-tools/commit/47755fb0a8625df3bc43c2dc5b410d419e90bc03))
|
|
30
|
+
* **chat:** Include the name and email of the person asking the question in the prompt for context ([79bf33c](https://github.com/postalsys/email-ai-tools/commit/79bf33ca1ccd47e3680e912aebe8bcd243578507))
|
|
31
|
+
* **deloy:** Set up automatic publishing ([18e2dc0](https://github.com/postalsys/email-ai-tools/commit/18e2dc00a2c123995f2e67c81c132e4380021dd7))
|
|
32
|
+
* **deps:** bumped dependencies ([b53b9c8](https://github.com/postalsys/email-ai-tools/commit/b53b9c86f7fc963ccf261e27b6ece37b1424281a))
|
|
33
|
+
* **deps:** bumped deps ([234a0f3](https://github.com/postalsys/email-ai-tools/commit/234a0f36130500efa72afc194b4e73dc86eb4d24))
|
|
34
|
+
* **deps:** Bumped deps ([e2eef79](https://github.com/postalsys/email-ai-tools/commit/e2eef79c36da0a500bd11ab9ffe4bb86fd791b7d))
|
|
35
|
+
* **deps:** Bumped deps ([1097be6](https://github.com/postalsys/email-ai-tools/commit/1097be6a6241e2b9bfcece2e8bb07fe078889602))
|
|
36
|
+
* **deps:** Bumped deps ([7f41de8](https://github.com/postalsys/email-ai-tools/commit/7f41de8776311c89803fd02ee73423e572981f9e))
|
|
37
|
+
* **deps:** Bumped deps ([948331a](https://github.com/postalsys/email-ai-tools/commit/948331abdc924392d0a5654b1683e92131ad6154))
|
|
38
|
+
* **deps:** Bumped deps ([52bc6d5](https://github.com/postalsys/email-ai-tools/commit/52bc6d56aaa904a0f640086626a927766e316c14))
|
|
39
|
+
* **deps:** Bumped deps ([d1ba73e](https://github.com/postalsys/email-ai-tools/commit/d1ba73ee14c3d1cd680a64fba637ae658460dcf3))
|
|
40
|
+
* **deps:** Bumped email-text-tools ([05cd642](https://github.com/postalsys/email-ai-tools/commit/05cd642b31778f8bcddeccf2513d99524db5cc84))
|
|
41
|
+
* **deps:** bumped nodemailer ([375c5fd](https://github.com/postalsys/email-ai-tools/commit/375c5fd25162880b32205bee155cf694a91bad23))
|
|
42
|
+
* **Deps:** fixed deps ([b6a2dea](https://github.com/postalsys/email-ai-tools/commit/b6a2dea7305bbf064e2d0a24e6c31c87180629c3))
|
|
43
|
+
* **deps:** Updated undici ([64ec2e7](https://github.com/postalsys/email-ai-tools/commit/64ec2e7148be9d83a255a08f9ab50a934eeacf99))
|
|
44
|
+
* **embeddings:** Changed embeddings output structure ([368467c](https://github.com/postalsys/email-ai-tools/commit/368467cabcb21dab9d4819933f6d3027f3cd3f26))
|
|
45
|
+
* **embeddings:** remove quoted lines ([fb5dd92](https://github.com/postalsys/email-ai-tools/commit/fb5dd92b834249a0290113df4c42b2f01eb22e84))
|
|
46
|
+
* **logging:** Log API url if verbose ([2e2b9d9](https://github.com/postalsys/email-ai-tools/commit/2e2b9d9bc605b3261f420855d5ae2d0ca5878153))
|
|
47
|
+
* **package:** updated package-lock ([a892178](https://github.com/postalsys/email-ai-tools/commit/a89217801c7d05a281c031a9ac72593e29febc97))
|
|
48
|
+
* **questions:** Tuned sort ordering ([6f6b3f4](https://github.com/postalsys/email-ai-tools/commit/6f6b3f467e70cca1dab82410c07029ca98932d7d))
|
|
49
|
+
* Replaced punycode with punycode.js ([5c54d20](https://github.com/postalsys/email-ai-tools/commit/5c54d202c700230fac4ae87899d32bcafa56dfca))
|
|
50
|
+
|
|
51
|
+
## [1.10.0](https://github.com/postalsys/email-ai-tools/compare/email-ai-tools-v1.9.2...email-ai-tools-v1.10.0) (2025-11-18)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
### Features
|
|
55
|
+
|
|
56
|
+
* upgrade to larger context models and improve prompt engineering ([27564ce](https://github.com/postalsys/email-ai-tools/commit/27564ce9fdebb79fcb2803a2c47a895881835874))
|
|
57
|
+
|
|
3
58
|
## [1.9.2](https://github.com/postalsys/email-ai-tools/compare/email-ai-tools-v1.9.1...email-ai-tools-v1.9.2) (2025-10-29)
|
|
4
59
|
|
|
5
60
|
|
package/examples/summary.js
CHANGED
package/lib/embeddings-query.js
CHANGED
|
@@ -9,8 +9,8 @@ const fetchAgent = new Agent({ connect: { timeout: 90 * 1000 } });
|
|
|
9
9
|
|
|
10
10
|
const tokenizer = new GPT3Tokenizer({ type: 'gpt3' });
|
|
11
11
|
|
|
12
|
-
const MAX_ALLOWED_TEXT_LENGTH =
|
|
13
|
-
const
|
|
12
|
+
const MAX_ALLOWED_TEXT_LENGTH = 64 * 1024;
|
|
13
|
+
const DEFAULT_MAX_ALLOWED_TOKENS = 30000;
|
|
14
14
|
|
|
15
15
|
const OPENAI_API_BASE_URL = 'https://api.openai.com';
|
|
16
16
|
|
|
@@ -137,8 +137,8 @@ async function embeddingsQuery(apiToken, opts) {
|
|
|
137
137
|
let question = (opts.question || '').toString().trim();
|
|
138
138
|
let contextChunks = (opts.contextChunks || '').toString().trim();
|
|
139
139
|
|
|
140
|
-
let maxAllowedTokens = opts.maxTokens ||
|
|
141
|
-
let gptModel = opts.gptModel || 'gpt-
|
|
140
|
+
let maxAllowedTokens = opts.maxTokens || DEFAULT_MAX_ALLOWED_TOKENS;
|
|
141
|
+
let gptModel = opts.gptModel || 'gpt-5-mini';
|
|
142
142
|
|
|
143
143
|
let prompt;
|
|
144
144
|
|
package/lib/generate-summary.js
CHANGED
|
@@ -10,81 +10,107 @@ const fetchAgent = new Agent({ connect: { timeout: 90 * 1000 } });
|
|
|
10
10
|
|
|
11
11
|
const tokenizer = new GPT3Tokenizer({ type: 'gpt3' });
|
|
12
12
|
|
|
13
|
-
const MAX_ALLOWED_TEXT_LENGTH =
|
|
14
|
-
const
|
|
13
|
+
const MAX_ALLOWED_TEXT_LENGTH = 64 * 1024;
|
|
14
|
+
const DEFAULT_MAX_ALLOWED_TOKENS = 30000;
|
|
15
|
+
const MAX_INSTRUCT_TOKENS = 4000;
|
|
16
|
+
const MAX_RETRIES = 5;
|
|
17
|
+
const RETRY_DELAY_MS = 1000;
|
|
15
18
|
|
|
16
19
|
const OPENAI_API_BASE_URL = 'https://api.openai.com';
|
|
17
20
|
|
|
18
21
|
const OPENAI_API_URL_CHAT = '/v1/chat/completions';
|
|
19
22
|
const OPENAI_API_URL_INSTRUCT = '/v1/completions';
|
|
20
23
|
|
|
21
|
-
const DEFAULT_ALLOWED_HEADERS = [
|
|
24
|
+
const DEFAULT_ALLOWED_HEADERS = [
|
|
25
|
+
'from',
|
|
26
|
+
'to',
|
|
27
|
+
'cc',
|
|
28
|
+
'bcc',
|
|
29
|
+
'subject',
|
|
30
|
+
'mime-version',
|
|
31
|
+
'authentication-results',
|
|
32
|
+
'arc-authentication-results',
|
|
33
|
+
'arc-message-signature',
|
|
34
|
+
'arc-seal',
|
|
35
|
+
'date',
|
|
36
|
+
'content-type',
|
|
37
|
+
'list-id'
|
|
38
|
+
];
|
|
22
39
|
|
|
23
40
|
const DEFAULT_SYSTEM_PROMPT = `
|
|
24
|
-
|
|
41
|
+
You are an executive assistant that processes emails for reporting. Your role is to analyze emails for important information and identify potential security threats.
|
|
25
42
|
`.trim();
|
|
26
43
|
|
|
27
44
|
const DEFAULT_USER_PROMPT = `
|
|
28
45
|
Instructions:
|
|
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
|
-
|
|
46
|
+
|
|
47
|
+
RETURN A JSON OBJECT with the following structure:
|
|
48
|
+
|
|
49
|
+
REQUIRED FIELDS:
|
|
50
|
+
- "sentiment": Must be exactly one of: "positive", "neutral", or "negative"
|
|
51
|
+
- "summary": A concise one-sentence summary (maximum 150 characters)
|
|
52
|
+
- "shouldReply": Boolean - true if the sender expects a reply, false otherwise
|
|
53
|
+
- "riskAssessment": Object containing:
|
|
54
|
+
- "risk": Integer from 1 to 5 (1=low risk, 5=high risk)
|
|
55
|
+
- "assessment": Single sentence describing only the risk factors (if risk > 1)
|
|
56
|
+
|
|
57
|
+
OPTIONAL FIELDS (only include if applicable):
|
|
58
|
+
- "replyText": If this is a reply or forward, extract only the new content written by the current sender (exclude quoted text, signatures, and reply markers)
|
|
59
|
+
- "events": Array of event objects (only include if confidence is medium or high)
|
|
60
|
+
- "description": Event description in English
|
|
61
|
+
- "location": Event location (if mentioned)
|
|
62
|
+
- "startTime": ISO 8601 format (YYYY-MM-DDTHH:mm:ss or YYYY-MM-DD)
|
|
63
|
+
- "endTime": ISO 8601 format (only if clearly stated)
|
|
64
|
+
- "type": One of "meeting", "appointment", "task", "event", or "general"
|
|
65
|
+
- "confidence": One of "high", "medium", "low"
|
|
66
|
+
- "actions": Array of action objects (only if recipient must take action)
|
|
67
|
+
- "description": Action description in English
|
|
68
|
+
- "dueDate": ISO 8601 format (YYYY-MM-DDTHH:mm:ss or YYYY-MM-DD)
|
|
69
|
+
- "confidence": One of "high", "medium", "low"
|
|
70
|
+
|
|
71
|
+
SECURITY ANALYSIS - Assess risk based on these factors:
|
|
72
|
+
HIGH RISK INDICATORS (risk 4-5):
|
|
73
|
+
- Links with typosquatted or homoglyph domains (e.g., paypa1.com, g00gle.com)
|
|
74
|
+
- Sender address mismatches claimed identity (e.g., from "support@random.com" claiming to be "Microsoft")
|
|
75
|
+
- Promises of large unclaimed money, inheritances, or lottery winnings
|
|
76
|
+
- Threats of severe penalties, account suspension, or legal action
|
|
77
|
+
- Requests to verify account credentials or update payment information urgently
|
|
78
|
+
- Attachments with double extensions (e.g., invoice.pdf.exe) or executable extensions (.exe, .scr, .bat, .vbs, .js)
|
|
79
|
+
|
|
80
|
+
MEDIUM RISK INDICATORS (risk 2-3):
|
|
81
|
+
- Vague business opportunities with no specific details
|
|
82
|
+
- Claims of technical issues requiring immediate action
|
|
83
|
+
- Sender uses suspicious email patterns (long random strings, excessive numbers in username)
|
|
84
|
+
- Missing or failed email authentication (no SPF/DKIM/DMARC pass)
|
|
85
|
+
- Offers of adult content or age-restricted services
|
|
86
|
+
|
|
87
|
+
LOW RISK INDICATORS (risk 1):
|
|
88
|
+
- Valid SPF, DKIM, and DMARC authentication (all pass)
|
|
89
|
+
- Professional email from recognized organization
|
|
90
|
+
- No urgency pressure or suspicious requests
|
|
91
|
+
|
|
92
|
+
EMAIL AUTHENTICATION FACTS:
|
|
93
|
+
- "authentication-results" header shows SPF, DKIM, DMARC, and ARC validation
|
|
94
|
+
- Look for: "spf=pass", "dkim=pass", "dmarc=pass", "arc=pass"
|
|
95
|
+
- Passing all checks significantly reduces spoofing risk
|
|
96
|
+
- Failed or missing authentication increases risk
|
|
97
|
+
|
|
98
|
+
TIMESTAMP GUIDELINES:
|
|
99
|
+
- Use ISO 8601 format: YYYY-MM-DDTHH:mm:ss (24-hour time)
|
|
100
|
+
- For date-only: YYYY-MM-DD
|
|
101
|
+
- If email uses relative dates (e.g., "tomorrow", "next week"), calculate from the "date" header
|
|
102
|
+
- Do not include timezone information
|
|
103
|
+
|
|
104
|
+
REPLY/FORWARD DETECTION:
|
|
105
|
+
- Email is a reply if it has an "in-reply-to" header
|
|
106
|
+
- Email is a forward if it has "references" header but no "in-reply-to" header
|
|
107
|
+
- For replies/forwards, extract only the new content, excluding:
|
|
108
|
+
- Lines starting with ">" (quoted text)
|
|
109
|
+
- Email signatures
|
|
110
|
+
- Previous email headers (From:, Sent:, To:, etc.)
|
|
111
|
+
- Forwarding markers (------Original Message------)
|
|
112
|
+
|
|
113
|
+
Always return response in English, regardless of input language.
|
|
88
114
|
`.trim();
|
|
89
115
|
|
|
90
116
|
const SCHEMA_PROMPT = `
|
|
@@ -122,6 +148,57 @@ function getApiUrl(baseApiUrl, path) {
|
|
|
122
148
|
return url.href;
|
|
123
149
|
}
|
|
124
150
|
|
|
151
|
+
/**
|
|
152
|
+
* Generates a comprehensive summary and analysis of an email message using OpenAI's API
|
|
153
|
+
*
|
|
154
|
+
* @param {Object} message - The email message object to analyze
|
|
155
|
+
* @param {Array} message.headers - Array of header objects with {key, value} structure
|
|
156
|
+
* @param {Array} [message.attachments] - Array of attachment objects with {filename, contentType}
|
|
157
|
+
* @param {string} [message.text] - Plain text content of the email
|
|
158
|
+
* @param {string} [message.html] - HTML content of the email (will be converted to text)
|
|
159
|
+
* @param {string} apiToken - OpenAI API authentication token
|
|
160
|
+
* @param {Object} [opts] - Configuration options
|
|
161
|
+
* @param {string} [opts.baseApiUrl] - Custom API base URL (default: OpenAI API)
|
|
162
|
+
* @param {string} [opts.gptModel='gpt-5-mini'] - Model to use (gpt-3.5-turbo, gpt-4, etc.)
|
|
163
|
+
* @param {string} [opts.systemPrompt] - Override default system prompt
|
|
164
|
+
* @param {string} [opts.userPrompt] - Override default user prompt
|
|
165
|
+
* @param {number} [opts.maxTokens=4000] - Maximum tokens for the prompt
|
|
166
|
+
* @param {number} [opts.temperature] - Sampling temperature (0-2)
|
|
167
|
+
* @param {number} [opts.topP] - Nucleus sampling parameter (0-1)
|
|
168
|
+
* @param {string} [opts.user] - OpenAI user identifier for tracking
|
|
169
|
+
* @param {Array<string>} [opts.allowedHeaders] - Additional headers to include (merged with defaults)
|
|
170
|
+
* @param {boolean} [opts.verbose=false] - Enable debug logging
|
|
171
|
+
*
|
|
172
|
+
* @returns {Promise<Object>} Analysis result object
|
|
173
|
+
* @returns {string} return.sentiment - Email sentiment: 'positive', 'neutral', or 'negative'
|
|
174
|
+
* @returns {string} return.summary - One-sentence summary of the email
|
|
175
|
+
* @returns {boolean} return.shouldReply - Whether sender expects a reply
|
|
176
|
+
* @returns {Object} return.riskAssessment - Security risk assessment
|
|
177
|
+
* @returns {number} return.riskAssessment.risk - Risk score from 1 (low) to 5 (high)
|
|
178
|
+
* @returns {string} [return.riskAssessment.assessment] - Description of risk factors (if risk > 1)
|
|
179
|
+
* @returns {string} [return.replyText] - Extracted new content from reply/forward
|
|
180
|
+
* @returns {Array<Object>} [return.events] - Extracted calendar events
|
|
181
|
+
* @returns {Array<Object>} [return.actions] - Actions required from recipient
|
|
182
|
+
* @returns {string} return.id - OpenAI request ID
|
|
183
|
+
* @returns {number} return.tokens - Total tokens used
|
|
184
|
+
* @returns {string} return.model - Model used for generation
|
|
185
|
+
*
|
|
186
|
+
* @throws {Error} If prompt is too long even after truncation
|
|
187
|
+
* @throws {Error} If API request fails
|
|
188
|
+
* @throws {Error} If response is not valid JSON
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* const result = await generateSummary(
|
|
192
|
+
* {
|
|
193
|
+
* headers: [{key: 'from', value: 'user@example.com'}],
|
|
194
|
+
* text: 'Meeting tomorrow at 2pm'
|
|
195
|
+
* },
|
|
196
|
+
* 'sk-...',
|
|
197
|
+
* { gptModel: 'gpt-5-mini' }
|
|
198
|
+
* );
|
|
199
|
+
* console.log(result.summary);
|
|
200
|
+
* console.log(result.events);
|
|
201
|
+
*/
|
|
125
202
|
async function generateSummary(message, apiToken, opts) {
|
|
126
203
|
opts = opts || {};
|
|
127
204
|
|
|
@@ -130,8 +207,8 @@ async function generateSummary(message, apiToken, opts) {
|
|
|
130
207
|
let systemPrompt = (opts.systemPrompt || DEFAULT_SYSTEM_PROMPT).toString().trim();
|
|
131
208
|
let userPrompt = (opts.userPrompt || DEFAULT_USER_PROMPT).toString().trim();
|
|
132
209
|
|
|
133
|
-
let maxAllowedTokens = opts.maxTokens ||
|
|
134
|
-
let gptModel = opts.gptModel || 'gpt-
|
|
210
|
+
let maxAllowedTokens = opts.maxTokens || DEFAULT_MAX_ALLOWED_TOKENS;
|
|
211
|
+
let gptModel = opts.gptModel || 'gpt-5-mini';
|
|
135
212
|
|
|
136
213
|
let text = message.text || '';
|
|
137
214
|
if (message.html && (!text || message.html.length >= text.length)) {
|
|
@@ -161,7 +238,7 @@ async function generateSummary(message, apiToken, opts) {
|
|
|
161
238
|
header => allowedHeaders.includes(header.key)
|
|
162
239
|
)
|
|
163
240
|
.filter(header => {
|
|
164
|
-
// only
|
|
241
|
+
// only keep the latest authentication header
|
|
165
242
|
if (['authentication-results', 'arc-authentication-results', 'arc-message-signature', 'arc-seal'].includes(header.key)) {
|
|
166
243
|
if (headerSeen.has(header.key)) {
|
|
167
244
|
return false;
|
|
@@ -181,7 +258,7 @@ async function generateSummary(message, apiToken, opts) {
|
|
|
181
258
|
|
|
182
259
|
if (promptText.length > MAX_ALLOWED_TEXT_LENGTH) {
|
|
183
260
|
charactersRemoved += promptText.length - MAX_ALLOWED_TEXT_LENGTH;
|
|
184
|
-
promptText = promptText.
|
|
261
|
+
promptText = promptText.substring(0, MAX_ALLOWED_TEXT_LENGTH);
|
|
185
262
|
}
|
|
186
263
|
|
|
187
264
|
while (true) {
|
|
@@ -203,7 +280,7 @@ ${JSON.stringify(content)}`;
|
|
|
203
280
|
charactersRemoved += 1024;
|
|
204
281
|
} else if (promptText.length > 2 * 256) {
|
|
205
282
|
promptText = promptText.substring(0, promptText.length - 256);
|
|
206
|
-
charactersRemoved +=
|
|
283
|
+
charactersRemoved += 256;
|
|
207
284
|
} else if (promptText.length > 2 * 100) {
|
|
208
285
|
promptText = promptText.substring(0, promptText.length - 100);
|
|
209
286
|
charactersRemoved += 100;
|
|
@@ -214,7 +291,17 @@ ${JSON.stringify(content)}`;
|
|
|
214
291
|
promptText = promptText.substring(0, promptText.length - 1);
|
|
215
292
|
charactersRemoved += 1;
|
|
216
293
|
} else {
|
|
217
|
-
|
|
294
|
+
const error = new Error(
|
|
295
|
+
`Unable to fit email content within token limit of ${maxAllowedTokens}. ` +
|
|
296
|
+
`Original text length: ${text.length} characters. ` +
|
|
297
|
+
`Removed ${charactersRemoved} characters but still exceeds limit. ` +
|
|
298
|
+
`Consider using a model with larger context window.`
|
|
299
|
+
);
|
|
300
|
+
error.code = 'PROMPT_TOO_LONG';
|
|
301
|
+
error.originalLength = text.length;
|
|
302
|
+
error.charactersRemoved = charactersRemoved;
|
|
303
|
+
error.maxAllowedTokens = maxAllowedTokens;
|
|
304
|
+
throw error;
|
|
218
305
|
}
|
|
219
306
|
}
|
|
220
307
|
|
|
@@ -253,7 +340,7 @@ ${JSON.stringify(content)}`;
|
|
|
253
340
|
openAiAPIURL = getApiUrl(baseApiUrl, OPENAI_API_URL_INSTRUCT);
|
|
254
341
|
payload.prompt = `${systemPrompt}\n${prompt}`;
|
|
255
342
|
let tokens = tokenizer.encode(payload.prompt);
|
|
256
|
-
payload.max_tokens =
|
|
343
|
+
payload.max_tokens = MAX_INSTRUCT_TOKENS - tokens.bpe.length;
|
|
257
344
|
}
|
|
258
345
|
break;
|
|
259
346
|
|
|
@@ -289,9 +376,9 @@ ${JSON.stringify(content)}`;
|
|
|
289
376
|
data = await res.json();
|
|
290
377
|
|
|
291
378
|
if (!res.ok) {
|
|
292
|
-
if (res.status === 429 && ++retries <
|
|
379
|
+
if (res.status === 429 && ++retries < MAX_RETRIES) {
|
|
293
380
|
// try again
|
|
294
|
-
await new Promise(r => setTimeout(r,
|
|
381
|
+
await new Promise(r => setTimeout(r, RETRY_DELAY_MS));
|
|
295
382
|
return await run();
|
|
296
383
|
}
|
|
297
384
|
|
package/lib/risk-analysis.js
CHANGED
|
@@ -9,13 +9,29 @@ const fetchAgent = new Agent({ connect: { timeout: 90 * 1000 } });
|
|
|
9
9
|
|
|
10
10
|
const tokenizer = new GPT3Tokenizer({ type: 'gpt3' });
|
|
11
11
|
|
|
12
|
-
const MAX_ALLOWED_TEXT_LENGTH =
|
|
13
|
-
const
|
|
12
|
+
const MAX_ALLOWED_TEXT_LENGTH = 64 * 1024;
|
|
13
|
+
const DEFAULT_MAX_ALLOWED_TOKENS = 30000;
|
|
14
14
|
|
|
15
15
|
const OPENAI_API_BASE_URL = 'https://api.openai.com';
|
|
16
16
|
|
|
17
17
|
const OPENAI_API_URL = '/v1/chat/completions';
|
|
18
18
|
|
|
19
|
+
const DEFAULT_ALLOWED_HEADERS = [
|
|
20
|
+
'from',
|
|
21
|
+
'to',
|
|
22
|
+
'cc',
|
|
23
|
+
'bcc',
|
|
24
|
+
'subject',
|
|
25
|
+
'mime-version',
|
|
26
|
+
'authentication-results',
|
|
27
|
+
'arc-authentication-results',
|
|
28
|
+
'arc-message-signature',
|
|
29
|
+
'arc-seal',
|
|
30
|
+
'date',
|
|
31
|
+
'content-type',
|
|
32
|
+
'list-id'
|
|
33
|
+
];
|
|
34
|
+
|
|
19
35
|
const DEFAULT_SYSTEM_PROMPT = `
|
|
20
36
|
I want you to act as are an IT security expert that monitors emails for suspicious and risky activity.
|
|
21
37
|
`
|
|
@@ -79,8 +95,8 @@ async function riskAnalysis(message, apiToken, opts) {
|
|
|
79
95
|
let systemPrompt = opts.systemPrompt || DEFAULT_SYSTEM_PROMPT;
|
|
80
96
|
let userPrompt = opts.userPrompt || DEFAULT_USER_PROMPT;
|
|
81
97
|
|
|
82
|
-
let maxAllowedTokens = opts.maxTokens ||
|
|
83
|
-
let gptModel = opts.gptModel || 'gpt-
|
|
98
|
+
let maxAllowedTokens = opts.maxTokens || DEFAULT_MAX_ALLOWED_TOKENS;
|
|
99
|
+
let gptModel = opts.gptModel || 'gpt-5-mini';
|
|
84
100
|
|
|
85
101
|
let text = message.text || '';
|
|
86
102
|
if (message.html && (!text || message.html.length > text.length * 2)) {
|
|
@@ -95,11 +111,10 @@ async function riskAnalysis(message, apiToken, opts) {
|
|
|
95
111
|
.concat(message.headers || [])
|
|
96
112
|
.filter(
|
|
97
113
|
// use a whitelist
|
|
98
|
-
header =>
|
|
99
|
-
['from', 'to', 'cc', 'bcc', 'subject', 'mime-version', 'authentication-results', 'date', 'content-type', 'list-id'].includes(header.key)
|
|
114
|
+
header => DEFAULT_ALLOWED_HEADERS.includes(header.key)
|
|
100
115
|
)
|
|
101
116
|
.filter(header => {
|
|
102
|
-
// only
|
|
117
|
+
// only keep the latest
|
|
103
118
|
if (['authentication-results'].includes(header.key)) {
|
|
104
119
|
if (headerSeen.has(header.key)) {
|
|
105
120
|
return false;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@postalsys/email-ai-tools",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.0",
|
|
4
4
|
"description": "Email AI tools",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -22,11 +22,11 @@
|
|
|
22
22
|
"homepage": "https://github.com/postalsys/email-ai-tools#readme",
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@eslint/eslintrc": "3.3.1",
|
|
25
|
-
"@eslint/js": "9.
|
|
26
|
-
"eslint": "9.
|
|
25
|
+
"@eslint/js": "9.39.1",
|
|
26
|
+
"eslint": "9.39.1",
|
|
27
27
|
"eslint-config-nodemailer": "1.2.0",
|
|
28
28
|
"eslint-config-prettier": "10.1.8",
|
|
29
|
-
"mailparser": "3.
|
|
29
|
+
"mailparser": "3.9.0",
|
|
30
30
|
"prettier": "3.6.2"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|