@postalsys/email-ai-tools 2.2.8 → 2.3.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.
Files changed (71) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +1 -1
  3. package/dist/cjs/index.d.ts +28 -0
  4. package/dist/cjs/index.js +67 -0
  5. package/dist/cjs/lib/api-request.d.ts +42 -0
  6. package/dist/cjs/lib/api-request.js +183 -0
  7. package/dist/cjs/lib/authentication.d.ts +46 -0
  8. package/{lib → dist/cjs/lib}/authentication.js +33 -44
  9. package/dist/cjs/lib/generate-summary.d.ts +143 -0
  10. package/dist/cjs/lib/generate-summary.js +562 -0
  11. package/dist/cjs/lib/get-api-url.d.ts +19 -0
  12. package/dist/cjs/lib/get-api-url.js +37 -0
  13. package/dist/cjs/lib/list-models.d.ts +46 -0
  14. package/dist/cjs/lib/list-models.js +125 -0
  15. package/dist/cjs/lib/signals.d.ts +72 -0
  16. package/dist/cjs/lib/signals.js +293 -0
  17. package/dist/cjs/lib/token-estimate.d.ts +46 -0
  18. package/{lib → dist/cjs/lib}/token-estimate.js +21 -50
  19. package/dist/cjs/lib/visible-text.d.ts +33 -0
  20. package/dist/cjs/lib/visible-text.js +182 -0
  21. package/dist/cjs/package-info.d.ts +2 -0
  22. package/dist/cjs/package-info.js +6 -0
  23. package/dist/cjs/package.json +3 -0
  24. package/dist/esm/index.d.ts +28 -0
  25. package/dist/esm/index.js +30 -0
  26. package/dist/esm/lib/api-request.d.ts +42 -0
  27. package/{lib → dist/esm/lib}/api-request.js +20 -53
  28. package/dist/esm/lib/authentication.d.ts +46 -0
  29. package/dist/esm/lib/authentication.js +149 -0
  30. package/dist/esm/lib/generate-summary.d.ts +143 -0
  31. package/{lib → dist/esm/lib}/generate-summary.js +59 -155
  32. package/dist/esm/lib/get-api-url.d.ts +19 -0
  33. package/{lib → dist/esm/lib}/get-api-url.js +4 -13
  34. package/dist/esm/lib/list-models.d.ts +46 -0
  35. package/{lib → dist/esm/lib}/list-models.js +19 -50
  36. package/dist/esm/lib/signals.d.ts +72 -0
  37. package/{lib → dist/esm/lib}/signals.js +33 -79
  38. package/dist/esm/lib/token-estimate.d.ts +46 -0
  39. package/dist/esm/lib/token-estimate.js +108 -0
  40. package/dist/esm/lib/visible-text.d.ts +33 -0
  41. package/{lib → dist/esm/lib}/visible-text.js +14 -40
  42. package/dist/esm/package-info.d.ts +2 -0
  43. package/dist/esm/package-info.js +3 -0
  44. package/dist/esm/package.json +3 -0
  45. package/package.json +63 -10
  46. package/.github/VULNERABILITY_REPORT.yml +0 -75
  47. package/.github/workflows/release.yaml +0 -37
  48. package/.github/workflows/test.yaml +0 -35
  49. package/.ncurc.js +0 -23
  50. package/.prettierignore +0 -5
  51. package/.prettierrc.js +0 -10
  52. package/.release-please-manifest.json +0 -3
  53. package/CLAUDE.md +0 -185
  54. package/eslint.config.js +0 -70
  55. package/examples/fixtures/message-1.json +0 -74
  56. package/examples/list-models.js +0 -14
  57. package/examples/summary.js +0 -34
  58. package/index.js +0 -26
  59. package/release-please-config.json +0 -10
  60. package/test/api-request.test.js +0 -169
  61. package/test/authentication.test.js +0 -110
  62. package/test/commonjs-graph.test.js +0 -21
  63. package/test/dispatcher.test.js +0 -94
  64. package/test/generate-summary.test.js +0 -795
  65. package/test/get-api-url.test.js +0 -52
  66. package/test/helpers/mock-server.js +0 -120
  67. package/test/index.test.js +0 -57
  68. package/test/list-models.test.js +0 -302
  69. package/test/signals.test.js +0 -264
  70. package/test/token-estimate.test.js +0 -164
  71. package/test/visible-text.test.js +0 -144
@@ -1,110 +0,0 @@
1
- 'use strict';
2
-
3
- const { describe, it } = require('node:test');
4
- const assert = require('node:assert/strict');
5
- const { parseAuthenticationResults, isTrustedAuthservId, authenticationBlock, authenticationFailed } = require('../lib/authentication');
6
-
7
- describe('parseAuthenticationResults', () => {
8
- it('reads the authserv-id and the first result of each method', () => {
9
- const parsed = parseAuthenticationResults(
10
- 'mx.google.com; dkim=pass header.i=@example.com header.s=sel header.b=abc; spf=pass (google.com: domain of a@example.com designates 1.2.3.4 as permitted sender) smtp.mailfrom=a@example.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=example.com; dkim=fail header.i=@other.example'
11
- );
12
-
13
- assert.deepEqual(parsed, { authservId: 'mx.google.com', results: { dkim: 'pass', spf: 'pass', dmarc: 'pass' } });
14
- });
15
-
16
- it('reads a header with a version and comments carrying semicolons', () => {
17
- const parsed = parseAuthenticationResults('mail.example.org 1; spf=softfail (reason; with semicolon) smtp.mailfrom=x@y.example; arc=none');
18
-
19
- assert.equal(parsed.authservId, 'mail.example.org');
20
- assert.deepEqual(parsed.results, { spf: 'softfail', arc: 'none' });
21
- });
22
-
23
- it('reads the Microsoft form, which names no server', () => {
24
- const parsed = parseAuthenticationResults(
25
- 'spf=pass (sender IP is 40.107.1.2) smtp.mailfrom=example.com; dkim=pass (signature was verified) header.d=example.com;dmarc=pass action=none header.from=example.com;compauth=pass reason=100'
26
- );
27
-
28
- assert.equal(parsed.authservId, null);
29
- assert.deepEqual(parsed.results, { spf: 'pass', dkim: 'pass', dmarc: 'pass' });
30
- });
31
-
32
- it('keeps a method=result inside a comment or a quoted string from counting', () => {
33
- const parsed = parseAuthenticationResults(
34
- 'mx.google.com; spf=fail (google.com: domain of "x);dmarc=pass;("@evil.example designates) smtp.mailfrom=x; dkim=pass (a (b) ; dmarc=pass ; (c) d); dmarc=fail'
35
- );
36
-
37
- assert.deepEqual(parsed, { authservId: 'mx.google.com', results: { spf: 'fail', dkim: 'pass', dmarc: 'fail' } });
38
- });
39
-
40
- it('returns null for an empty value', () => {
41
- assert.equal(parseAuthenticationResults(''), null);
42
- assert.equal(parseAuthenticationResults(undefined), null);
43
- });
44
- });
45
-
46
- describe('isTrustedAuthservId', () => {
47
- it('matches the name itself and hosts under it, whatever the case of the id', () => {
48
- assert.equal(isTrustedAuthservId('MX.google.com', ['mx.google.com']), true);
49
- assert.equal(isTrustedAuthservId('mx.google.com.evil.example', ['mx.google.com']), false);
50
- assert.equal(isTrustedAuthservId('evilmx.google.com', ['mx.google.com']), false);
51
- assert.equal(isTrustedAuthservId('mx1.example.com', ['example.com']), true);
52
- assert.equal(isTrustedAuthservId('notexample.com', ['example.com']), false);
53
- assert.equal(isTrustedAuthservId('example.com', ['mx1.example.com']), false);
54
- assert.equal(isTrustedAuthservId('mx.google.com', []), false);
55
- });
56
-
57
- it('accepts a missing name only when told to', () => {
58
- assert.equal(isTrustedAuthservId(null, ['mx.google.com']), false);
59
- assert.equal(isTrustedAuthservId(null, ['mx.google.com'], true), true);
60
- });
61
- });
62
-
63
- describe('authenticationBlock', () => {
64
- it('marks a verdict from a trusted server as verified', () => {
65
- assert.deepEqual(
66
- authenticationBlock('mx.google.com; spf=pass; dkim=pass; dmarc=pass', {
67
- trustedAuthservIds: ['mx.google.com'],
68
- acceptUnnamedAuthentication: false
69
- }),
70
- {
71
- verified: true,
72
- authservId: 'mx.google.com',
73
- spf: 'pass',
74
- dkim: 'pass',
75
- dmarc: 'pass'
76
- }
77
- );
78
- });
79
-
80
- it('keeps an unverified verdict, marked as such', () => {
81
- const block = authenticationBlock('attacker.example; spf=pass; dkim=pass; dmarc=pass', {
82
- trustedAuthservIds: ['mx.google.com'],
83
- acceptUnnamedAuthentication: false
84
- });
85
-
86
- assert.equal(block.verified, false);
87
- assert.equal(block.authservId, 'attacker.example');
88
- assert.equal(block.dmarc, 'pass');
89
- });
90
-
91
- it('returns null without a header', () => {
92
- assert.equal(authenticationBlock('', { trustedAuthservIds: [], acceptUnnamedAuthentication: false }), null);
93
- });
94
- });
95
-
96
- describe('authenticationFailed', () => {
97
- it('is true for a verified DMARC failure, or SPF and DKIM both failing without DMARC', () => {
98
- assert.equal(authenticationFailed({ verified: true, dmarc: 'fail', spf: 'pass' }), true);
99
- assert.equal(authenticationFailed({ verified: true, spf: 'fail', dkim: 'fail' }), true);
100
- assert.equal(authenticationFailed({ verified: true, spf: 'softfail', dkim: 'permerror' }), true);
101
- });
102
-
103
- it('is false for a pass, a partial failure, an unverified verdict or no verdict', () => {
104
- assert.equal(authenticationFailed({ verified: true, spf: 'pass', dkim: 'pass', dmarc: 'pass' }), false);
105
- assert.equal(authenticationFailed({ verified: true, spf: 'fail', dkim: 'pass' }), false);
106
- assert.equal(authenticationFailed({ verified: true, spf: 'fail', dkim: 'fail', dmarc: 'pass' }), false);
107
- assert.equal(authenticationFailed({ verified: false, dmarc: 'fail' }), false);
108
- assert.equal(authenticationFailed(null), false);
109
- });
110
- });
@@ -1,21 +0,0 @@
1
- 'use strict';
2
-
3
- // Loads every runtime dependency through require(), including the jsdom-backed mimeHtml getter of
4
- // email-text-tools that the summary code never reaches. Under `npm run test:cjs` (require(esm)
5
- // disabled) this fails as soon as anything in the runtime graph is ESM-only.
6
-
7
- const { describe, it } = require('node:test');
8
- const assert = require('node:assert/strict');
9
- const { dependencies } = require('../package.json');
10
-
11
- describe('CommonJS dependency graph', () => {
12
- for (const name of Object.keys(dependencies)) {
13
- it(`require('${name}') loads`, () => {
14
- assert.ok(require(name));
15
- });
16
- }
17
-
18
- it('loads the jsdom-backed mimeHtml from email-text-tools', () => {
19
- assert.equal(typeof require('@postalsys/email-text-tools').mimeHtml, 'function');
20
- });
21
- });
@@ -1,94 +0,0 @@
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, modelsResponse } = require('./helpers/mock-server');
7
- const { generateSummary, 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
- // The request behaviour itself is covered in api-request.test.js; these only check that every
33
- // entry point hands its dispatcher and signal down to it
34
- describe('request option pass-through', () => {
35
- let mock;
36
- let dispatcher;
37
-
38
- before(async () => {
39
- mock = await createMockServer();
40
- });
41
-
42
- after(async () => {
43
- await mock.close();
44
- });
45
-
46
- beforeEach(() => {
47
- mock.clearRequests();
48
- dispatcher = new CountingAgent();
49
- });
50
-
51
- const cases = [
52
- {
53
- name: 'generateSummary',
54
- response: () => chatResponse({ sentiment: 'neutral', summary: 'ok', shouldReply: false, riskAssessment: { risk: 1 } }),
55
- run: opts => generateSummary(message, 'token', opts)
56
- },
57
- {
58
- name: 'listModels',
59
- response: () => modelsResponse(),
60
- run: opts => listModels('token', opts)
61
- }
62
- ];
63
-
64
- for (const entry of cases) {
65
- it(`${entry.name} passes the dispatcher and the signal to the request`, { timeout: 5000 }, async () => {
66
- mock.setHandler(entry.response);
67
-
68
- await entry.run({ baseApiUrl: mock.url, dispatcher });
69
-
70
- assert.ok(mock.requests.length >= 1, 'the mock server should have been reached');
71
- assert.equal(dispatcher.dispatched, mock.requests.length);
72
-
73
- // aborted while the request is in flight, and the 429 would otherwise be retried
74
- mock.clearRequests();
75
- const controller = new AbortController();
76
- mock.setHandler(() => {
77
- controller.abort(new Error('stopped'));
78
- return { status: 429, raw: '', headers: { 'Retry-After': '10' } };
79
- });
80
-
81
- await assert.rejects(() => entry.run({ baseApiUrl: mock.url, signal: controller.signal }), { message: 'stopped' });
82
- assert.equal(mock.requests.length, 1);
83
- });
84
- }
85
-
86
- it('falls back to the built-in agent when no dispatcher is given', async () => {
87
- mock.setHandler(() => modelsResponse());
88
-
89
- await listModels('token', { baseApiUrl: mock.url });
90
-
91
- assert.equal(mock.requests.length, 1);
92
- assert.equal(dispatcher.dispatched, 0);
93
- });
94
- });