@postalsys/email-ai-tools 1.12.1 → 1.13.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.
@@ -0,0 +1,34 @@
1
+ name: Tests
2
+
3
+ on:
4
+ push:
5
+ branches: [master]
6
+ pull_request:
7
+ branches: [master]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ matrix:
14
+ node-version: [22, 24]
15
+ steps:
16
+ - uses: actions/checkout@v4
17
+ - uses: actions/setup-node@v4
18
+ with:
19
+ node-version: ${{ matrix.node-version }}
20
+ cache: 'npm'
21
+ - run: npm ci
22
+ - run: npm test
23
+
24
+ lint:
25
+ runs-on: ubuntu-latest
26
+ steps:
27
+ - uses: actions/checkout@v4
28
+ - uses: actions/setup-node@v4
29
+ with:
30
+ node-version: 24
31
+ cache: 'npm'
32
+ - run: npm ci
33
+ - run: npm run lint
34
+ - run: npx prettier --check "**/*.{js,json,md}" --ignore-path .prettierignore
@@ -1,3 +1,3 @@
1
1
  {
2
- ".": "1.12.1"
2
+ ".": "1.13.0"
3
3
  }
package/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.13.0](https://github.com/postalsys/email-ai-tools/compare/v1.12.1...v1.13.0) (2026-03-25)
4
+
5
+
6
+ ### Features
7
+
8
+ * add test suite and CI workflow ([2fa43d6](https://github.com/postalsys/email-ai-tools/commit/2fa43d61f29c50a7ae172c41fb7d288f781ccd31))
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * Bumped deps ([6a121e6](https://github.com/postalsys/email-ai-tools/commit/6a121e6cb948da5f8bf84e8a4f65f69baf888942))
14
+ * require Node 22+ for test CI matrix ([3e689ef](https://github.com/postalsys/email-ai-tools/commit/3e689efe7a50df58d85071bb587a0cbba8ed9bb4))
15
+
3
16
  ## [1.12.1](https://github.com/postalsys/email-ai-tools/compare/v1.12.0...v1.12.1) (2026-03-23)
4
17
 
5
18
 
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@postalsys/email-ai-tools",
3
- "version": "1.12.1",
3
+ "version": "1.13.0",
4
4
  "description": "Email AI tools",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1",
7
+ "test": "node --test test/*.test.js",
8
8
  "update": "rm -rf node_modules package-lock.json && ncu -u && npm install",
9
9
  "format": "prettier --write \"**/*.{js,json,md}\" --ignore-path .prettierignore",
10
10
  "lint": "eslint ."
@@ -26,7 +26,7 @@
26
26
  "eslint": "10.1.0",
27
27
  "eslint-config-nodemailer": "1.2.0",
28
28
  "eslint-config-prettier": "10.1.8",
29
- "mailparser": "3.9.4",
29
+ "mailparser": "3.9.5",
30
30
  "prettier": "3.8.1"
31
31
  },
32
32
  "dependencies": {
@@ -34,7 +34,7 @@
34
34
  "gpt3-tokenizer": "1.1.5",
35
35
  "libmime": "5.3.7",
36
36
  "linkify-it": "5.0.0",
37
- "nodemailer": "8.0.3",
37
+ "nodemailer": "8.0.4",
38
38
  "punycode.js": "2.3.1",
39
39
  "tlds": "1.261.0",
40
40
  "undici": "7.24.5"
@@ -0,0 +1,304 @@
1
+ 'use strict';
2
+
3
+ const { describe, it, before, after, beforeEach } = require('node:test');
4
+ const assert = require('node:assert/strict');
5
+ const { createMockServer, chatResponse, instructResponse, errorResponse } = require('./helpers/mock-server');
6
+ const { embeddingsQuery, questionQuery } = require('../lib/embeddings-query');
7
+
8
+ describe('embeddings-query', () => {
9
+ let mock;
10
+
11
+ before(async () => {
12
+ mock = await createMockServer();
13
+ });
14
+
15
+ after(async () => {
16
+ await mock.close();
17
+ });
18
+
19
+ beforeEach(() => {
20
+ mock.clearRequests();
21
+ });
22
+
23
+ describe('embeddingsQuery', () => {
24
+ it('returns answer and message IDs from response', async () => {
25
+ mock.setHandler(() => chatResponse('Answer: The meeting is on Friday.\nMessage-ID: <msg001@example.com>'));
26
+
27
+ const result = await embeddingsQuery('test-token', {
28
+ baseApiUrl: mock.url,
29
+ question: 'When is the meeting?',
30
+ contextChunks: '- EMAIL #1:\nFrom: test@example.com\nSubject: Meeting\nMessage-ID: <msg001@example.com>\n\nMeeting on Friday.'
31
+ });
32
+
33
+ assert.equal(result.answer, 'The meeting is on Friday.');
34
+ assert.deepEqual(result.messageId, ['<msg001@example.com>']);
35
+ assert.equal(result.id, 'chatcmpl-test123');
36
+ assert.equal(result.tokens, 150);
37
+ assert.equal(result.model, 'gpt-5-mini');
38
+ });
39
+
40
+ it('deduplicates message IDs', async () => {
41
+ mock.setHandler(() =>
42
+ chatResponse('Answer: Found in multiple emails.\nMessage-ID: <msg001@example.com>, <msg001@example.com>, <msg002@example.com>')
43
+ );
44
+
45
+ const result = await embeddingsQuery('test-token', {
46
+ baseApiUrl: mock.url,
47
+ question: 'Test?',
48
+ contextChunks: 'context'
49
+ });
50
+
51
+ assert.deepEqual(result.messageId, ['<msg001@example.com>', '<msg002@example.com>']);
52
+ });
53
+
54
+ it('sends correct request payload with chat model', async () => {
55
+ mock.setHandler(() => chatResponse('Answer: Test\nMessage-ID: <test@example.com>'));
56
+
57
+ await embeddingsQuery('my-api-key', {
58
+ baseApiUrl: mock.url,
59
+ question: 'What happened?',
60
+ contextChunks: 'email context here',
61
+ userData: { name: 'John', email: 'john@example.com' },
62
+ user: 'user123',
63
+ temperature: 0.5
64
+ });
65
+
66
+ const req = mock.requests[0];
67
+ assert.equal(req.method, 'POST');
68
+ assert.equal(req.url, '/v1/chat/completions');
69
+ assert.equal(req.body.model, 'gpt-5-mini');
70
+ assert.equal(req.body.user, 'user123');
71
+ assert.equal(req.body.temperature, 0.5);
72
+ assert.equal(req.body.messages.length, 2);
73
+ assert.ok(req.body.messages[1].content.includes('What happened?'));
74
+ assert.ok(req.body.messages[1].content.includes('email context here'));
75
+ assert.ok(req.body.messages[1].content.includes('John'));
76
+ assert.ok(req.body.messages[1].content.includes('john@example.com'));
77
+ });
78
+
79
+ it('uses instruct endpoint for gpt-3.5-turbo-instruct', async () => {
80
+ mock.setHandler(() => instructResponse('Answer: Instruct answer\nMessage-ID: <test@example.com>'));
81
+
82
+ const result = await embeddingsQuery('test-token', {
83
+ baseApiUrl: mock.url,
84
+ question: 'Test?',
85
+ contextChunks: 'context',
86
+ gptModel: 'gpt-3.5-turbo-instruct'
87
+ });
88
+
89
+ assert.equal(result.answer, 'Instruct answer');
90
+ assert.equal(result.model, 'gpt-3.5-turbo-instruct');
91
+ const req = mock.requests[0];
92
+ assert.equal(req.url, '/v1/completions');
93
+ assert.ok(typeof req.body.prompt === 'string');
94
+ assert.ok(req.body.max_tokens > 0);
95
+ });
96
+
97
+ it('handles response with no answer section', async () => {
98
+ mock.setHandler(() => chatResponse('I could not find relevant information.'));
99
+
100
+ const result = await embeddingsQuery('test-token', {
101
+ baseApiUrl: mock.url,
102
+ question: 'Unknown topic?',
103
+ contextChunks: 'irrelevant context'
104
+ });
105
+
106
+ assert.equal(result.answer, '');
107
+ });
108
+
109
+ it('retries on 429 rate limit', { timeout: 10000 }, async () => {
110
+ let callCount = 0;
111
+ mock.setHandler(() => {
112
+ callCount++;
113
+ if (callCount === 1) {
114
+ return errorResponse(429, 'Rate limited');
115
+ }
116
+ return chatResponse('Answer: Success after retry\nMessage-ID: <test@example.com>');
117
+ });
118
+
119
+ const result = await embeddingsQuery('test-token', {
120
+ baseApiUrl: mock.url,
121
+ question: 'Test?',
122
+ contextChunks: 'context'
123
+ });
124
+
125
+ assert.equal(result.answer, 'Success after retry');
126
+ assert.equal(callCount, 2);
127
+ });
128
+
129
+ it('throws on API errors', async () => {
130
+ mock.setHandler(() => errorResponse(500, 'Server error', 'internal_error'));
131
+
132
+ await assert.rejects(
133
+ () =>
134
+ embeddingsQuery('test-token', {
135
+ baseApiUrl: mock.url,
136
+ question: 'Test?',
137
+ contextChunks: 'context'
138
+ }),
139
+ err => {
140
+ assert.equal(err.message, 'Server error');
141
+ assert.equal(err.statusCode, 500);
142
+ return true;
143
+ }
144
+ );
145
+ });
146
+ });
147
+
148
+ describe('questionQuery', () => {
149
+ it('parses ordering and topic from JSON response', async () => {
150
+ mock.setHandler(() => instructResponse({ ordering: 'newer_first', topic: 'Conference event' }));
151
+
152
+ const result = await questionQuery('When is the next conference?', 'test-token', { baseApiUrl: mock.url });
153
+
154
+ assert.equal(result.ordering, 'newer_first');
155
+ assert.equal(result.topic, 'Conference event');
156
+ assert.equal(result.id, 'cmpl-test123');
157
+ assert.equal(result.tokens, 100);
158
+ assert.equal(result.model, 'gpt-3.5-turbo-instruct');
159
+ });
160
+
161
+ it('parses time constraints from response', async () => {
162
+ mock.setHandler(() =>
163
+ instructResponse({
164
+ ordering: 'best_match',
165
+ start_time: '2024-01-01',
166
+ end_time: '2024-01-31',
167
+ topic: 'January emails'
168
+ })
169
+ );
170
+
171
+ const result = await questionQuery('Show me emails from January 2024', 'test-token', {
172
+ baseApiUrl: mock.url
173
+ });
174
+
175
+ assert.equal(result.ordering, 'best_match');
176
+ assert.equal(result.start_time, '2024-01-01');
177
+ assert.equal(result.end_time, '2024-01-31');
178
+ });
179
+
180
+ it('removes null values from response', async () => {
181
+ mock.setHandler(() =>
182
+ instructResponse({
183
+ ordering: 'newer_first',
184
+ start_time: null,
185
+ end_time: null,
186
+ topic: 'Test topic'
187
+ })
188
+ );
189
+
190
+ const result = await questionQuery('Show me recent emails', 'test-token', { baseApiUrl: mock.url });
191
+
192
+ assert.ok(!('start_time' in result));
193
+ assert.ok(!('end_time' in result));
194
+ assert.equal(result.ordering, 'newer_first');
195
+ });
196
+
197
+ it('extracts JSON from response with surrounding text', async () => {
198
+ mock.setHandler(() => instructResponse('Based on the question:\n{"ordering":"older_first","topic":"First Amazon invoice"}\nDone.'));
199
+
200
+ const result = await questionQuery('When did I get my first Amazon invoice?', 'test-token', {
201
+ baseApiUrl: mock.url
202
+ });
203
+
204
+ assert.equal(result.ordering, 'older_first');
205
+ assert.equal(result.topic, 'First Amazon invoice');
206
+ });
207
+
208
+ it('throws on empty question', async () => {
209
+ await assert.rejects(
210
+ () => questionQuery('', 'test-token', { baseApiUrl: mock.url }),
211
+ err => {
212
+ assert.equal(err.message, 'Question not provided');
213
+ assert.equal(err.code, 'EmptyInput');
214
+ return true;
215
+ }
216
+ );
217
+ });
218
+
219
+ it('throws on null question', async () => {
220
+ await assert.rejects(
221
+ () => questionQuery(null, 'test-token', { baseApiUrl: mock.url }),
222
+ err => {
223
+ assert.equal(err.code, 'EmptyInput');
224
+ return true;
225
+ }
226
+ );
227
+ });
228
+
229
+ it('defaults to gpt-3.5-turbo-instruct model', async () => {
230
+ mock.setHandler(() => instructResponse({ ordering: 'best_match', topic: 'Test' }));
231
+
232
+ await questionQuery('Test question', 'test-token', { baseApiUrl: mock.url });
233
+
234
+ const req = mock.requests[0];
235
+ assert.equal(req.url, '/v1/completions');
236
+ assert.equal(req.body.model, 'gpt-3.5-turbo-instruct');
237
+ });
238
+
239
+ it('uses chat endpoint for chat models', async () => {
240
+ mock.setHandler(() => chatResponse({ ordering: 'best_match', topic: 'Test' }));
241
+
242
+ await questionQuery('Test question', 'test-token', {
243
+ baseApiUrl: mock.url,
244
+ gptModel: 'gpt-4'
245
+ });
246
+
247
+ const req = mock.requests[0];
248
+ assert.equal(req.url, '/v1/chat/completions');
249
+ assert.equal(req.body.model, 'gpt-4');
250
+ assert.ok(Array.isArray(req.body.messages));
251
+ });
252
+
253
+ it('sets default temperature to 0.2', async () => {
254
+ mock.setHandler(() => instructResponse({ ordering: 'best_match', topic: 'Test' }));
255
+
256
+ await questionQuery('Test question', 'test-token', { baseApiUrl: mock.url });
257
+
258
+ assert.equal(mock.requests[0].body.temperature, 0.2);
259
+ });
260
+
261
+ it('throws on invalid JSON response', async () => {
262
+ mock.setHandler(() => instructResponse('I cannot understand the question'));
263
+
264
+ await assert.rejects(
265
+ () => questionQuery('Test?', 'test-token', { baseApiUrl: mock.url }),
266
+ err => {
267
+ assert.ok(err.message.includes('Failed to parse'));
268
+ assert.equal(err.code, 'OutputParseFailed');
269
+ return true;
270
+ }
271
+ );
272
+ });
273
+
274
+ it('retries on 429 rate limit', { timeout: 10000 }, async () => {
275
+ let callCount = 0;
276
+ mock.setHandler(() => {
277
+ callCount++;
278
+ if (callCount === 1) {
279
+ return errorResponse(429, 'Rate limited');
280
+ }
281
+ return instructResponse({ ordering: 'best_match', topic: 'Retry success' });
282
+ });
283
+
284
+ const result = await questionQuery('Test?', 'test-token', { baseApiUrl: mock.url });
285
+
286
+ assert.equal(result.topic, 'Retry success');
287
+ assert.equal(callCount, 2);
288
+ });
289
+
290
+ it('throws on API errors', async () => {
291
+ mock.setHandler(() => errorResponse(403, 'Forbidden', 'access_denied'));
292
+
293
+ await assert.rejects(
294
+ () => questionQuery('Test?', 'test-token', { baseApiUrl: mock.url }),
295
+ err => {
296
+ assert.equal(err.message, 'Forbidden');
297
+ assert.equal(err.code, 'access_denied');
298
+ assert.equal(err.statusCode, 403);
299
+ return true;
300
+ }
301
+ );
302
+ });
303
+ });
304
+ });
@@ -0,0 +1,103 @@
1
+ 'use strict';
2
+
3
+ const { describe, it, before, after, beforeEach } = require('node:test');
4
+ const assert = require('node:assert/strict');
5
+ const { createMockServer, embeddingResponse, errorResponse } = require('./helpers/mock-server');
6
+ const { getChunkEmbeddings } = require('../lib/generate-embeddings');
7
+
8
+ describe('getChunkEmbeddings', () => {
9
+ let mock;
10
+
11
+ before(async () => {
12
+ mock = await createMockServer();
13
+ });
14
+
15
+ after(async () => {
16
+ await mock.close();
17
+ });
18
+
19
+ beforeEach(() => {
20
+ mock.clearRequests();
21
+ });
22
+
23
+ it('returns embedding data for a text chunk', async () => {
24
+ const embedding = [0.1, 0.2, 0.3, 0.4, 0.5];
25
+ mock.setHandler(() => embeddingResponse(embedding));
26
+
27
+ const result = await getChunkEmbeddings('Hello world', 'test-token', { baseApiUrl: mock.url });
28
+
29
+ assert.equal(result.chunk, 'Hello world');
30
+ assert.deepEqual(result.embedding, embedding);
31
+ assert.equal(typeof result._time, 'number');
32
+ assert.ok(result._time >= 0);
33
+ });
34
+
35
+ it('sends correct request to embeddings endpoint', async () => {
36
+ mock.setHandler(() => embeddingResponse());
37
+
38
+ await getChunkEmbeddings('Test chunk', 'my-api-key', {
39
+ baseApiUrl: mock.url,
40
+ gptModel: 'text-embedding-3-small',
41
+ user: 'user123'
42
+ });
43
+
44
+ const req = mock.requests[0];
45
+ assert.equal(req.method, 'POST');
46
+ assert.equal(req.url, '/v1/embeddings');
47
+ assert.ok(req.headers.authorization.includes('Bearer my-api-key'));
48
+ assert.equal(req.body.model, 'text-embedding-3-small');
49
+ assert.equal(req.body.input, 'Test chunk');
50
+ assert.equal(req.body.user, 'user123');
51
+ });
52
+
53
+ it('defaults to text-embedding-ada-002 model', async () => {
54
+ mock.setHandler(() => embeddingResponse());
55
+
56
+ await getChunkEmbeddings('Test', 'test-token', { baseApiUrl: mock.url });
57
+
58
+ assert.equal(mock.requests[0].body.model, 'text-embedding-ada-002');
59
+ });
60
+
61
+ it('retries on 429 rate limit', { timeout: 10000 }, async () => {
62
+ let callCount = 0;
63
+ mock.setHandler(() => {
64
+ callCount++;
65
+ if (callCount === 1) {
66
+ return errorResponse(429, 'Rate limited');
67
+ }
68
+ return embeddingResponse([0.1, 0.2]);
69
+ });
70
+
71
+ const result = await getChunkEmbeddings('Test', 'test-token', { baseApiUrl: mock.url });
72
+
73
+ assert.deepEqual(result.embedding, [0.1, 0.2]);
74
+ assert.equal(callCount, 2);
75
+ });
76
+
77
+ it('throws on API errors', async () => {
78
+ mock.setHandler(() => errorResponse(401, 'Unauthorized', 'invalid_api_key'));
79
+
80
+ await assert.rejects(
81
+ () => getChunkEmbeddings('Test', 'test-token', { baseApiUrl: mock.url }),
82
+ err => {
83
+ assert.equal(err.message, 'Unauthorized');
84
+ assert.equal(err.code, 'invalid_api_key');
85
+ assert.equal(err.statusCode, 401);
86
+ return true;
87
+ }
88
+ );
89
+ });
90
+
91
+ it('throws generic error on failure without error details', async () => {
92
+ mock.setHandler(() => ({ status: 500, body: {} }));
93
+
94
+ await assert.rejects(
95
+ () => getChunkEmbeddings('Test', 'test-token', { baseApiUrl: mock.url }),
96
+ err => {
97
+ assert.equal(err.message, 'Failed to run API request');
98
+ assert.equal(err.statusCode, 500);
99
+ return true;
100
+ }
101
+ );
102
+ });
103
+ });