@postalsys/email-ai-tools 1.13.23 → 1.14.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.
@@ -1,3 +1,3 @@
1
1
  {
2
- ".": "1.13.23"
2
+ ".": "1.14.0"
3
3
  }
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.14.0](https://github.com/postalsys/email-ai-tools/compare/v1.13.24...v1.14.0) (2026-09-05)
4
+
5
+
6
+ ### Features
7
+
8
+ * send API requests through a caller-supplied undici dispatcher ([63e710b](https://github.com/postalsys/email-ai-tools/commit/63e710b6898e89134cd0b99862aaf61dd020ae59))
9
+
10
+ ## [1.13.24](https://github.com/postalsys/email-ai-tools/compare/v1.13.23...v1.13.24) (2026-09-01)
11
+
12
+
13
+ ### Bug Fixes
14
+
15
+ * **deps:** update nodemailer to 9.1.1 and mailparser to 3.9.20 ([6e5a75a](https://github.com/postalsys/email-ai-tools/commit/6e5a75a0b74d740493e8a812b45d118481087094))
16
+
3
17
  ## [1.13.23](https://github.com/postalsys/email-ai-tools/compare/v1.13.22...v1.13.23) (2026-08-31)
4
18
 
5
19
 
@@ -248,7 +248,7 @@ ${promptText}
248
248
  method: 'post',
249
249
  headers,
250
250
  body: JSON.stringify(payload),
251
- dispatcher: fetchAgent
251
+ dispatcher: opts.dispatcher || fetchAgent
252
252
  });
253
253
 
254
254
  data = await res.json();
@@ -438,7 +438,7 @@ ${question}
438
438
  method: 'post',
439
439
  headers,
440
440
  body: JSON.stringify(payload),
441
- dispatcher: fetchAgent
441
+ dispatcher: opts.dispatcher || fetchAgent
442
442
  });
443
443
 
444
444
  data = await res.json();
@@ -26,7 +26,8 @@ const { getApiUrl, OPENAI_API_BASE_URL } = require('./get-api-url');
26
26
  const OPENAI_API_URL = '/v1/embeddings';
27
27
 
28
28
  async function getChunkEmbeddings(chunk, apiToken, opts) {
29
- let { gptModel, verbose, baseApiUrl } = opts || {};
29
+ opts = opts || {};
30
+ let { gptModel, verbose, baseApiUrl, dispatcher } = opts;
30
31
 
31
32
  baseApiUrl = baseApiUrl || OPENAI_API_BASE_URL;
32
33
 
@@ -64,7 +65,7 @@ async function getChunkEmbeddings(chunk, apiToken, opts) {
64
65
  method: 'post',
65
66
  headers,
66
67
  body: JSON.stringify(payload),
67
- dispatcher: fetchAgent
68
+ dispatcher: dispatcher || fetchAgent
68
69
  });
69
70
 
70
71
  data = await res.json();
@@ -120,6 +121,15 @@ class Embedder {
120
121
  this.chunkSize = opts.chunkSize || 400;
121
122
  this.gptModel = opts.gptModel || 'text-embedding-ada-002';
122
123
 
124
+ // Forwarded to every per-chunk embeddings request
125
+ this.requestOpts = {
126
+ gptModel: this.gptModel,
127
+ baseApiUrl: opts.baseApiUrl,
128
+ user: opts.user,
129
+ dispatcher: opts.dispatcher,
130
+ verbose: opts.verbose
131
+ };
132
+
123
133
  this.addressHeaders = new Map();
124
134
 
125
135
  let subject;
@@ -345,12 +355,7 @@ class Embedder {
345
355
  let embeddings = [];
346
356
 
347
357
  for (let chunk of chunks) {
348
- embeddings.push(
349
- await getChunkEmbeddings(chunk, this.apiToken, {
350
- gptModel: this.gptModel,
351
- verbose: this.verbose
352
- })
353
- );
358
+ embeddings.push(await getChunkEmbeddings(chunk, this.apiToken, this.requestOpts));
354
359
  }
355
360
 
356
361
  return { model: this.gptModel, embeddings };
@@ -358,7 +363,7 @@ class Embedder {
358
363
  }
359
364
 
360
365
  module.exports.getChunkEmbeddings = getChunkEmbeddings;
361
- module.exports.generateEmbeddings = async (message, apiToken) => {
362
- let embedder = new Embedder(message, apiToken);
366
+ module.exports.generateEmbeddings = async (message, apiToken, opts) => {
367
+ let embedder = new Embedder(message, apiToken, opts);
363
368
  return await embedder.getEmbeddings();
364
369
  };
@@ -163,6 +163,7 @@ Analyze the following email:`.trim();
163
163
  * @param {string} [opts.user] - OpenAI user identifier for tracking
164
164
  * @param {Array<string>} [opts.allowedHeaders] - Additional headers to include (merged with defaults)
165
165
  * @param {boolean} [opts.verbose=false] - Enable debug logging
166
+ * @param {Object} [opts.dispatcher] - undici Dispatcher (for example a ProxyAgent) the API request is sent through; the module's own keep-alive agent when omitted
166
167
  *
167
168
  * @returns {Promise<Object>} Analysis result object
168
169
  * @returns {string} return.sentiment - Email sentiment: 'positive', 'neutral', or 'negative'
@@ -365,7 +366,7 @@ ${JSON.stringify(content)}`;
365
366
  method: 'post',
366
367
  headers,
367
368
  body: JSON.stringify(payload),
368
- dispatcher: fetchAgent
369
+ dispatcher: opts.dispatcher || fetchAgent
369
370
  });
370
371
 
371
372
  data = await res.json();
@@ -37,7 +37,7 @@ async function listModels(apiToken, opts) {
37
37
  res = await fetchCmd(openAiAPIURL, {
38
38
  method: 'get',
39
39
  headers,
40
- dispatcher: fetchAgent
40
+ dispatcher: opts.dispatcher || fetchAgent
41
41
  });
42
42
 
43
43
  data = await res.json();
@@ -203,7 +203,7 @@ ${JSON.stringify(content)}`;
203
203
  method: 'post',
204
204
  headers,
205
205
  body: JSON.stringify(payload),
206
- dispatcher: fetchAgent
206
+ dispatcher: opts.dispatcher || fetchAgent
207
207
  });
208
208
 
209
209
  data = await res.json();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@postalsys/email-ai-tools",
3
- "version": "1.13.23",
3
+ "version": "1.14.0",
4
4
  "description": "Email AI tools",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -21,12 +21,12 @@
21
21
  },
22
22
  "homepage": "https://github.com/postalsys/email-ai-tools#readme",
23
23
  "devDependencies": {
24
- "@eslint/eslintrc": "3.3.6",
24
+ "@eslint/eslintrc": "3.3.7",
25
25
  "@eslint/js": "10.0.1",
26
26
  "eslint": "10.9.1",
27
27
  "eslint-config-nodemailer": "1.2.0",
28
28
  "eslint-config-prettier": "10.1.8",
29
- "mailparser": "3.9.19",
29
+ "mailparser": "3.9.20",
30
30
  "prettier": "3.9.6"
31
31
  },
32
32
  "dependencies": {
@@ -34,7 +34,7 @@
34
34
  "gpt3-tokenizer": "1.1.5",
35
35
  "libmime": "5.4.3",
36
36
  "linkify-it": "5.0.2",
37
- "nodemailer": "9.1.0",
37
+ "nodemailer": "9.1.1",
38
38
  "punycode.js": "2.3.1",
39
39
  "tlds": "1.261.0",
40
40
  "undici": "7.29.0"
@@ -0,0 +1,125 @@
1
+ 'use strict';
2
+
3
+ const { describe, it, before, after, beforeEach } = require('node:test');
4
+ const assert = require('node:assert/strict');
5
+ const { Agent } = require('undici');
6
+ const { createMockServer, chatResponse, embeddingResponse, modelsResponse } = require('./helpers/mock-server');
7
+ const { generateSummary, generateEmbeddings, getChunkEmbeddings, embeddingsQuery, questionQuery, riskAnalysis, listModels } = require('..');
8
+
9
+ // A real undici Agent that counts what was dispatched through it, so a test can tell the request
10
+ // used the dispatcher it was given and not the module's own agent.
11
+ class CountingAgent extends Agent {
12
+ constructor() {
13
+ super();
14
+ this.dispatched = 0;
15
+ }
16
+
17
+ dispatch(opts, handler) {
18
+ this.dispatched++;
19
+ return super.dispatch(opts, handler);
20
+ }
21
+ }
22
+
23
+ const message = {
24
+ headers: [
25
+ { key: 'from', value: 'sender@example.com' },
26
+ { key: 'to', value: 'recipient@example.com' },
27
+ { key: 'subject', value: 'Test Subject' }
28
+ ],
29
+ text: 'Hello, this is a test email.'
30
+ };
31
+
32
+ describe('dispatcher option', () => {
33
+ let mock;
34
+ let dispatcher;
35
+
36
+ before(async () => {
37
+ mock = await createMockServer();
38
+ });
39
+
40
+ after(async () => {
41
+ await mock.close();
42
+ });
43
+
44
+ beforeEach(() => {
45
+ mock.clearRequests();
46
+ dispatcher = new CountingAgent();
47
+ });
48
+
49
+ const cases = [
50
+ {
51
+ name: 'generateSummary',
52
+ response: () => chatResponse({ sentiment: 'neutral', summary: 'ok', shouldReply: false, riskAssessment: { risk: 1 } }),
53
+ run: opts => generateSummary(message, 'token', opts)
54
+ },
55
+ {
56
+ name: 'riskAnalysis',
57
+ response: () => chatResponse({ risk: 1, assessment: 'fine' }),
58
+ run: opts => riskAnalysis(message, 'token', opts)
59
+ },
60
+ {
61
+ name: 'getChunkEmbeddings',
62
+ response: () => embeddingResponse(),
63
+ run: opts => getChunkEmbeddings('chunk', 'token', opts)
64
+ },
65
+ {
66
+ name: 'generateEmbeddings',
67
+ response: () => embeddingResponse(),
68
+ run: opts => generateEmbeddings(message, 'token', opts)
69
+ },
70
+ {
71
+ name: 'embeddingsQuery',
72
+ response: () => chatResponse('Answer.\n\nMessage IDs: id1'),
73
+ run: opts => embeddingsQuery('token', Object.assign({ question: 'What?', contextChunks: 'id1: hello' }, opts))
74
+ },
75
+ {
76
+ name: 'questionQuery',
77
+ response: () => chatResponse({ ordering: 'best_match', topic: 'conference' }),
78
+ run: opts => questionQuery('When is the next conference?', 'token', opts)
79
+ },
80
+ {
81
+ name: 'listModels',
82
+ response: () => modelsResponse(),
83
+ run: opts => listModels('token', opts)
84
+ }
85
+ ];
86
+
87
+ for (const entry of cases) {
88
+ it(`${entry.name} sends its request through the given dispatcher`, async () => {
89
+ mock.setHandler(entry.response);
90
+
91
+ await entry.run({ baseApiUrl: mock.url, dispatcher });
92
+
93
+ assert.ok(mock.requests.length >= 1, 'the mock server should have been reached');
94
+ assert.equal(dispatcher.dispatched, mock.requests.length);
95
+ });
96
+ }
97
+
98
+ it('generateEmbeddings forwards the request options to every chunk request', async () => {
99
+ mock.setHandler(() => embeddingResponse());
100
+
101
+ const result = await generateEmbeddings(message, 'token', {
102
+ baseApiUrl: mock.url,
103
+ dispatcher,
104
+ gptModel: 'text-embedding-3-small',
105
+ user: 'user-1'
106
+ });
107
+
108
+ assert.equal(result.model, 'text-embedding-3-small');
109
+ assert.ok(mock.requests.length >= 1);
110
+ for (const req of mock.requests) {
111
+ assert.equal(req.url, '/v1/embeddings');
112
+ assert.equal(req.body.model, 'text-embedding-3-small');
113
+ assert.equal(req.body.user, 'user-1');
114
+ }
115
+ });
116
+
117
+ it('falls back to the built-in agent when no dispatcher is given', async () => {
118
+ mock.setHandler(() => modelsResponse());
119
+
120
+ await listModels('token', { baseApiUrl: mock.url });
121
+
122
+ assert.equal(mock.requests.length, 1);
123
+ assert.equal(dispatcher.dispatched, 0);
124
+ });
125
+ });