@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.
- package/CHANGELOG.md +12 -0
- package/README.md +1 -1
- package/dist/cjs/index.d.ts +28 -0
- package/dist/cjs/index.js +67 -0
- package/dist/cjs/lib/api-request.d.ts +42 -0
- package/dist/cjs/lib/api-request.js +183 -0
- package/dist/cjs/lib/authentication.d.ts +46 -0
- package/{lib → dist/cjs/lib}/authentication.js +33 -44
- package/dist/cjs/lib/generate-summary.d.ts +143 -0
- package/dist/cjs/lib/generate-summary.js +562 -0
- package/dist/cjs/lib/get-api-url.d.ts +19 -0
- package/dist/cjs/lib/get-api-url.js +37 -0
- package/dist/cjs/lib/list-models.d.ts +46 -0
- package/dist/cjs/lib/list-models.js +125 -0
- package/dist/cjs/lib/signals.d.ts +72 -0
- package/dist/cjs/lib/signals.js +293 -0
- package/dist/cjs/lib/token-estimate.d.ts +46 -0
- package/{lib → dist/cjs/lib}/token-estimate.js +21 -50
- package/dist/cjs/lib/visible-text.d.ts +33 -0
- package/dist/cjs/lib/visible-text.js +182 -0
- package/dist/cjs/package-info.d.ts +2 -0
- package/dist/cjs/package-info.js +6 -0
- package/dist/cjs/package.json +3 -0
- package/dist/esm/index.d.ts +28 -0
- package/dist/esm/index.js +30 -0
- package/dist/esm/lib/api-request.d.ts +42 -0
- package/{lib → dist/esm/lib}/api-request.js +20 -53
- package/dist/esm/lib/authentication.d.ts +46 -0
- package/dist/esm/lib/authentication.js +149 -0
- package/dist/esm/lib/generate-summary.d.ts +143 -0
- package/{lib → dist/esm/lib}/generate-summary.js +59 -155
- package/dist/esm/lib/get-api-url.d.ts +19 -0
- package/{lib → dist/esm/lib}/get-api-url.js +4 -13
- package/dist/esm/lib/list-models.d.ts +46 -0
- package/{lib → dist/esm/lib}/list-models.js +19 -50
- package/dist/esm/lib/signals.d.ts +72 -0
- package/{lib → dist/esm/lib}/signals.js +33 -79
- package/dist/esm/lib/token-estimate.d.ts +46 -0
- package/dist/esm/lib/token-estimate.js +108 -0
- package/dist/esm/lib/visible-text.d.ts +33 -0
- package/{lib → dist/esm/lib}/visible-text.js +14 -40
- package/dist/esm/package-info.d.ts +2 -0
- package/dist/esm/package-info.js +3 -0
- package/dist/esm/package.json +3 -0
- package/package.json +63 -10
- package/.github/VULNERABILITY_REPORT.yml +0 -75
- package/.github/workflows/release.yaml +0 -37
- package/.github/workflows/test.yaml +0 -35
- package/.ncurc.js +0 -23
- package/.prettierignore +0 -5
- package/.prettierrc.js +0 -10
- package/.release-please-manifest.json +0 -3
- package/CLAUDE.md +0 -185
- package/eslint.config.js +0 -70
- package/examples/fixtures/message-1.json +0 -74
- package/examples/list-models.js +0 -14
- package/examples/summary.js +0 -34
- package/index.js +0 -26
- package/release-please-config.json +0 -10
- package/test/api-request.test.js +0 -169
- package/test/authentication.test.js +0 -110
- package/test/commonjs-graph.test.js +0 -21
- package/test/dispatcher.test.js +0 -94
- package/test/generate-summary.test.js +0 -795
- package/test/get-api-url.test.js +0 -52
- package/test/helpers/mock-server.js +0 -120
- package/test/index.test.js +0 -57
- package/test/list-models.test.js +0 -302
- package/test/signals.test.js +0 -264
- package/test/token-estimate.test.js +0 -164
- package/test/visible-text.test.js +0 -144
|
@@ -1,21 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
3
2
|
// Token counts are estimated from characters instead of running a BPE tokenizer. The GPT-3
|
|
4
3
|
// tokenizer this library used to load does not match current model vocabularies anyway, costs
|
|
5
4
|
// ~15 MB of heap per instance, and is super-linear on long unbroken runs (a 64 KB CJK body took
|
|
6
5
|
// 25 s per encode). The estimate errs on the high side; the model's own context length error
|
|
7
6
|
// stays the backstop for the rare input that tokenizes worse than this.
|
|
8
|
-
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.DEFAULT_MAX_ALLOWED_TOKENS = void 0;
|
|
9
|
+
exports.estimateTokens = estimateTokens;
|
|
10
|
+
exports.fitLength = fitLength;
|
|
11
|
+
exports.fitPrompt = fitPrompt;
|
|
12
|
+
exports.truncate = truncate;
|
|
9
13
|
// ASCII at 3 characters per token is below what English, code and base64-ish runs actually
|
|
10
14
|
// reach, every non-ASCII UTF-16 code unit counts as a whole token, and the margin goes on top
|
|
11
15
|
const CHARS_PER_TOKEN = 3;
|
|
12
16
|
const MARGIN = 1.1;
|
|
13
|
-
|
|
14
17
|
// Input text beyond this many characters is never sent, whatever the token budget
|
|
15
18
|
const MAX_ALLOWED_TEXT_LENGTH = 64 * 1024;
|
|
16
19
|
// Default prompt budget when the caller does not set maxTokens
|
|
17
20
|
const DEFAULT_MAX_ALLOWED_TOKENS = 30000;
|
|
18
|
-
|
|
21
|
+
exports.DEFAULT_MAX_ALLOWED_TOKENS = DEFAULT_MAX_ALLOWED_TOKENS;
|
|
19
22
|
// Cost of one UTF-16 code unit in "ASCII character" units. The text travels JSON encoded, so the
|
|
20
23
|
// characters that encoding escapes count double
|
|
21
24
|
function charUnits(code) {
|
|
@@ -27,42 +30,32 @@ function charUnits(code) {
|
|
|
27
30
|
}
|
|
28
31
|
return CHARS_PER_TOKEN;
|
|
29
32
|
}
|
|
30
|
-
|
|
31
33
|
/**
|
|
32
34
|
* Estimates how many tokens a string takes
|
|
33
35
|
*
|
|
34
|
-
* @
|
|
35
|
-
* @returns {number} Estimated token count
|
|
36
|
+
* @returns Estimated token count
|
|
36
37
|
*/
|
|
37
|
-
function estimateTokens(
|
|
38
|
-
str = (
|
|
39
|
-
|
|
38
|
+
function estimateTokens(input) {
|
|
39
|
+
const str = (input || '').toString();
|
|
40
40
|
let units = 0;
|
|
41
41
|
for (let i = 0; i < str.length; i++) {
|
|
42
42
|
units += charUnits(str.charCodeAt(i));
|
|
43
43
|
}
|
|
44
|
-
|
|
45
44
|
// the epsilon keeps float noise (100 * 1.1 = 110.00000000000001) from adding a token
|
|
46
45
|
return Math.ceil((units / CHARS_PER_TOKEN) * MARGIN - 1e-9);
|
|
47
46
|
}
|
|
48
|
-
|
|
49
47
|
/**
|
|
50
48
|
* Returns the length of the longest prefix of `str` that fits into `maxTokens` by the same
|
|
51
49
|
* estimate as estimateTokens(). Never ends between the two halves of a surrogate pair.
|
|
52
50
|
*
|
|
53
|
-
* @
|
|
54
|
-
* @param {number} maxTokens
|
|
55
|
-
* @returns {number} Prefix length in UTF-16 code units
|
|
51
|
+
* @returns Prefix length in UTF-16 code units
|
|
56
52
|
*/
|
|
57
|
-
function fitLength(
|
|
58
|
-
str = (
|
|
59
|
-
|
|
53
|
+
function fitLength(input, maxTokens) {
|
|
54
|
+
const str = (input || '').toString();
|
|
60
55
|
if (!(maxTokens > 0)) {
|
|
61
56
|
return 0;
|
|
62
57
|
}
|
|
63
|
-
|
|
64
58
|
const maxUnits = Math.floor((maxTokens * CHARS_PER_TOKEN) / MARGIN);
|
|
65
|
-
|
|
66
59
|
let units = 0;
|
|
67
60
|
let i = 0;
|
|
68
61
|
for (; i < str.length; i++) {
|
|
@@ -72,10 +65,8 @@ function fitLength(str, maxTokens) {
|
|
|
72
65
|
}
|
|
73
66
|
units += cost;
|
|
74
67
|
}
|
|
75
|
-
|
|
76
68
|
return keepSurrogatePair(str, i);
|
|
77
69
|
}
|
|
78
|
-
|
|
79
70
|
// Moves a cut position back by one if it would separate a surrogate pair
|
|
80
71
|
function keepSurrogatePair(str, pos) {
|
|
81
72
|
if (pos > 0 && pos < str.length) {
|
|
@@ -86,59 +77,39 @@ function keepSurrogatePair(str, pos) {
|
|
|
86
77
|
}
|
|
87
78
|
return pos;
|
|
88
79
|
}
|
|
89
|
-
|
|
90
80
|
/**
|
|
91
81
|
* Cuts a string to at most `maxLength` UTF-16 code units, never between the halves of a
|
|
92
82
|
* surrogate pair
|
|
93
|
-
*
|
|
94
|
-
* @param {string} str
|
|
95
|
-
* @param {number} maxLength
|
|
96
|
-
* @returns {string}
|
|
97
83
|
*/
|
|
98
|
-
function truncate(
|
|
99
|
-
str = (
|
|
84
|
+
function truncate(input, maxLength) {
|
|
85
|
+
const str = (input || '').toString();
|
|
100
86
|
return str.length > maxLength ? str.substring(0, keepSurrogatePair(str, maxLength)) : str;
|
|
101
87
|
}
|
|
102
|
-
|
|
103
88
|
/**
|
|
104
89
|
* Cuts `text` so that it fits into `maxTokens` estimated tokens next to the fixed parts of the
|
|
105
90
|
* prompt
|
|
106
91
|
*
|
|
107
|
-
* @
|
|
108
|
-
* @param {string} opts.text - Variable part of the prompt, the only part that gets shortened
|
|
109
|
-
* @param {number} [opts.maxLength=MAX_ALLOWED_TEXT_LENGTH] - Hard cap on the text length
|
|
110
|
-
* @param {number} [opts.maxTokens=DEFAULT_MAX_ALLOWED_TOKENS] - Token budget for the whole prompt
|
|
111
|
-
* @param {Array<string|number>|string|number} [opts.overhead] - The fixed parts, as strings to estimate or as token counts already estimated
|
|
112
|
-
* @returns {{text: string, charactersRemoved: number}}
|
|
113
|
-
* @throws {Error} PROMPT_TOO_LONG if the fixed parts alone do not fit
|
|
92
|
+
* @throws Error with code PROMPT_TOO_LONG if the fixed parts alone do not fit
|
|
114
93
|
*/
|
|
115
94
|
function fitPrompt({ text, maxLength, maxTokens, overhead }) {
|
|
116
95
|
text = (text || '').toString();
|
|
117
96
|
maxLength = maxLength || MAX_ALLOWED_TEXT_LENGTH;
|
|
118
97
|
maxTokens = maxTokens || DEFAULT_MAX_ALLOWED_TOKENS;
|
|
119
|
-
|
|
120
98
|
const overheadTokens = []
|
|
121
99
|
.concat(overhead === undefined ? [] : overhead)
|
|
122
100
|
.reduce((sum, part) => sum + (typeof part === 'number' ? part : estimateTokens(part)), 0);
|
|
123
|
-
|
|
124
101
|
if (overheadTokens > maxTokens) {
|
|
125
|
-
const error = new Error(
|
|
126
|
-
`
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
`Consider using a model with larger context window.`
|
|
130
|
-
);
|
|
102
|
+
const error = new Error(`Unable to fit email content within token limit of ${maxTokens}. ` +
|
|
103
|
+
`Original text length: ${text.length} characters. ` +
|
|
104
|
+
`The prompt without the email text is already estimated at ${overheadTokens} tokens. ` +
|
|
105
|
+
`Consider using a model with larger context window.`);
|
|
131
106
|
error.code = 'PROMPT_TOO_LONG';
|
|
132
107
|
error.originalLength = text.length;
|
|
133
108
|
error.charactersRemoved = text.length;
|
|
134
109
|
error.maxAllowedTokens = maxTokens;
|
|
135
110
|
throw error;
|
|
136
111
|
}
|
|
137
|
-
|
|
138
112
|
let fitted = truncate(text, maxLength);
|
|
139
113
|
fitted = fitted.substring(0, fitLength(fitted, maxTokens - overheadTokens));
|
|
140
|
-
|
|
141
114
|
return { text: fitted, charactersRemoved: text.length - fitted.length };
|
|
142
115
|
}
|
|
143
|
-
|
|
144
|
-
module.exports = { estimateTokens, fitLength, fitPrompt, truncate, DEFAULT_MAX_ALLOWED_TOKENS };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** A link of the message: its target and the text a reader takes as its destination */
|
|
2
|
+
export interface MessageLink {
|
|
3
|
+
href: string;
|
|
4
|
+
text: string;
|
|
5
|
+
}
|
|
6
|
+
/** What visibleHtml() found */
|
|
7
|
+
export interface VisibleHtml {
|
|
8
|
+
/** Every link the HTML carries, hidden or not */
|
|
9
|
+
links: MessageLink[];
|
|
10
|
+
/** How many styled-away elements carrying text were dropped */
|
|
11
|
+
hiddenElements: number;
|
|
12
|
+
/** How many invisible characters were dropped */
|
|
13
|
+
invisibleCharacters: number;
|
|
14
|
+
/** The visible HTML, serialised only when asked for */
|
|
15
|
+
html: () => string;
|
|
16
|
+
}
|
|
17
|
+
declare const MAX_HTML_LENGTH: number;
|
|
18
|
+
declare const MAX_LINK_TEXT_LENGTH = 1024;
|
|
19
|
+
declare const INVISIBLE_CHARS: RegExp;
|
|
20
|
+
/**
|
|
21
|
+
* Parses an HTML message once: every link it carries, for the checks, and the document as the
|
|
22
|
+
* reader sees it, for the model
|
|
23
|
+
*/
|
|
24
|
+
declare function visibleHtml(html: string): VisibleHtml;
|
|
25
|
+
/**
|
|
26
|
+
* Removes the characters that take no space on screen
|
|
27
|
+
*
|
|
28
|
+
*/
|
|
29
|
+
declare function visibleText(input: unknown): {
|
|
30
|
+
text: string;
|
|
31
|
+
invisibleCharacters: number;
|
|
32
|
+
};
|
|
33
|
+
export { visibleHtml, visibleText, INVISIBLE_CHARS, MAX_HTML_LENGTH, MAX_LINK_TEXT_LENGTH };
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.MAX_LINK_TEXT_LENGTH = exports.MAX_HTML_LENGTH = exports.INVISIBLE_CHARS = void 0;
|
|
7
|
+
exports.visibleHtml = visibleHtml;
|
|
8
|
+
exports.visibleText = visibleText;
|
|
9
|
+
const htmlparser2_1 = require("htmlparser2");
|
|
10
|
+
const dom_serializer_1 = __importDefault(require("dom-serializer"));
|
|
11
|
+
const domutils_1 = require("domutils");
|
|
12
|
+
const token_estimate_js_1 = require("./token-estimate.js");
|
|
13
|
+
// What the reader never sees must not reach the model either: an email can carry instructions
|
|
14
|
+
// for an AI in text a mail client does not render, and the summary has to describe the message
|
|
15
|
+
// as it looks to the person it was sent to. The links are collected before anything is removed,
|
|
16
|
+
// because the checks run on them must see every link the message carries, hidden or not
|
|
17
|
+
// Elements whose content is never rendered as text
|
|
18
|
+
const HIDDEN_TAGS = new Set(['script', 'style', 'template', 'title', 'iframe', 'object', 'embed']);
|
|
19
|
+
// Comments and quoted strings inside a style attribute, which carry no declarations
|
|
20
|
+
const STYLE_NOISE = /\/\*[\s\S]*?\*\/|"[^"]*"|'[^']*'/g;
|
|
21
|
+
// Everything here runs synchronously in the process that called it, so how much of a message is
|
|
22
|
+
// read has to be this library's decision rather than the sender's. The HTML is cut before it is
|
|
23
|
+
// parsed; the text of a link is cut to what a reader could take in as its destination
|
|
24
|
+
const MAX_HTML_LENGTH = 1024 * 1024;
|
|
25
|
+
exports.MAX_HTML_LENGTH = MAX_HTML_LENGTH;
|
|
26
|
+
const MAX_LINK_TEXT_LENGTH = 1024;
|
|
27
|
+
exports.MAX_LINK_TEXT_LENGTH = MAX_LINK_TEXT_LENGTH;
|
|
28
|
+
// Outlook for Windows shows what a conditional comment wraps; the markers go, the content stays.
|
|
29
|
+
// The condition is bounded: an opening marker that never closes used to be scanned to the end
|
|
30
|
+
// of the document, once for every marker, which is quadratic
|
|
31
|
+
const CONDITIONAL_COMMENT = /<!--\[if [^\]]{0,200}\]>|<!\[endif\]-->/gi;
|
|
32
|
+
// Characters that take no space on screen: zero-width joiners and spaces, soft hyphens, the
|
|
33
|
+
// bidirectional controls and the word joiner. Removing them changes nothing the reader sees and
|
|
34
|
+
// stops them splitting words the model should recognise or reordering text it should read
|
|
35
|
+
const INVISIBLE_CHARS = /[\u00ad\u200b-\u200f\u2028-\u202e\u2060-\u2064\u2066-\u206f\ufeff]/g;
|
|
36
|
+
exports.INVISIBLE_CHARS = INVISIBLE_CHARS;
|
|
37
|
+
// Only what reliably hides text. Zero sizes of boxes and negative positions depend on other
|
|
38
|
+
// properties to hide anything, so they are left alone rather than dropping text the reader sees
|
|
39
|
+
function isHiddenStyle(input) {
|
|
40
|
+
const style = String(input ?? '').replace(STYLE_NOISE, ' ');
|
|
41
|
+
if (!style.trim()) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
for (const declaration of style.split(';')) {
|
|
45
|
+
const match = /^\s*([a-z-]+)\s*:\s*([^!]*)/i.exec(declaration);
|
|
46
|
+
if (!match) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
const property = match[1].toLowerCase();
|
|
50
|
+
const value = match[2].trim().toLowerCase();
|
|
51
|
+
const number = parseFloat(value);
|
|
52
|
+
switch (property) {
|
|
53
|
+
case 'display':
|
|
54
|
+
if (value === 'none') {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
break;
|
|
58
|
+
case 'visibility':
|
|
59
|
+
if (value === 'hidden' || value === 'collapse') {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
break;
|
|
63
|
+
case 'opacity':
|
|
64
|
+
if (Number.isFinite(number) && number < 0.1) {
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
break;
|
|
68
|
+
case 'font-size':
|
|
69
|
+
// zero in any unit, or below one pixel
|
|
70
|
+
if (Number.isFinite(number) && (number === 0 || (number < 1 && /^[\d.]+(?:px|pt)?$/.test(value)))) {
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
break;
|
|
74
|
+
case 'text-indent':
|
|
75
|
+
if (Number.isFinite(number) && number <= -1000) {
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
function isHiddenElement(node) {
|
|
84
|
+
const attribs = node.attribs || {};
|
|
85
|
+
const style = attribs.style || '';
|
|
86
|
+
// the hidden attribute is a user agent rule, which an author display value overrides
|
|
87
|
+
if ('hidden' in attribs && !/display\s*:\s*(?!none)/i.test(style.replace(STYLE_NOISE, ' '))) {
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
return isHiddenStyle(style);
|
|
91
|
+
}
|
|
92
|
+
function hasText(node) {
|
|
93
|
+
if ((0, domutils_1.isText)(node)) {
|
|
94
|
+
return /\S/.test(node.data || '');
|
|
95
|
+
}
|
|
96
|
+
return (0, domutils_1.hasChildren)(node) && node.children.some(hasText);
|
|
97
|
+
}
|
|
98
|
+
function stripHidden(node, stats) {
|
|
99
|
+
if (!(0, domutils_1.hasChildren)(node)) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
node.children = node.children.filter(child => {
|
|
103
|
+
if ((0, domutils_1.isComment)(child)) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
if ((0, domutils_1.isText)(child)) {
|
|
107
|
+
// before the conversion, which would turn a zero-width space into a plain one
|
|
108
|
+
const cleaned = child.data.replace(INVISIBLE_CHARS, '');
|
|
109
|
+
stats.invisibleCharacters += child.data.length - cleaned.length;
|
|
110
|
+
child.data = cleaned;
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
if (!(0, domutils_1.isTag)(child)) {
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
if (HIDDEN_TAGS.has(child.name)) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
if (isHiddenElement(child)) {
|
|
120
|
+
// a layout element with no text in it is not hidden content, only a spacer
|
|
121
|
+
if (hasText(child)) {
|
|
122
|
+
stats.hiddenElements++;
|
|
123
|
+
}
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
return true;
|
|
127
|
+
});
|
|
128
|
+
for (const child of node.children) {
|
|
129
|
+
stripHidden(child, stats);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
// What the reader takes as the link: its text, and the alt and title of an image standing in
|
|
133
|
+
// for it
|
|
134
|
+
function linkText(node) {
|
|
135
|
+
const parts = [(0, domutils_1.textContent)(node), node.attribs.title || ''];
|
|
136
|
+
for (const image of (0, domutils_1.findAll)(child => child.name === 'img', node.children)) {
|
|
137
|
+
parts.push(image.attribs.alt || '', image.attribs.title || '');
|
|
138
|
+
}
|
|
139
|
+
return (0, token_estimate_js_1.truncate)(parts.join(' ').replace(/\s+/g, ' ').trim(), MAX_LINK_TEXT_LENGTH);
|
|
140
|
+
}
|
|
141
|
+
function collectLinks(document) {
|
|
142
|
+
const targets = (0, domutils_1.findAll)(node => ((node.name === 'a' || node.name === 'area') && !!node.attribs.href) || (node.name === 'form' && !!node.attribs.action), document.children);
|
|
143
|
+
return targets.map(node => ({ href: node.attribs.href || node.attribs.action, text: linkText(node) }));
|
|
144
|
+
}
|
|
145
|
+
// A browser shows flow content a message put inside <head>, the converter reads only <body>:
|
|
146
|
+
// what is left of the head after the metadata went moves to the front of the body
|
|
147
|
+
function surfaceHead(document) {
|
|
148
|
+
const head = (0, domutils_1.findOne)(node => node.name === 'head', document.children);
|
|
149
|
+
if (!head) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
const body = (0, domutils_1.findOne)(node => node.name === 'body', document.children);
|
|
153
|
+
if (!body) {
|
|
154
|
+
head.name = 'div';
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
for (const child of head.children.slice().reverse()) {
|
|
158
|
+
(0, domutils_1.prependChild)(body, child);
|
|
159
|
+
}
|
|
160
|
+
(0, domutils_1.removeElement)(head);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Parses an HTML message once: every link it carries, for the checks, and the document as the
|
|
164
|
+
* reader sees it, for the model
|
|
165
|
+
*/
|
|
166
|
+
function visibleHtml(html) {
|
|
167
|
+
const document = (0, htmlparser2_1.parseDocument)((0, token_estimate_js_1.truncate)(html, MAX_HTML_LENGTH).replace(CONDITIONAL_COMMENT, ''));
|
|
168
|
+
const links = collectLinks(document);
|
|
169
|
+
const stats = { hiddenElements: 0, invisibleCharacters: 0 };
|
|
170
|
+
stripHidden(document, stats);
|
|
171
|
+
surfaceHead(document);
|
|
172
|
+
return { links, hiddenElements: stats.hiddenElements, invisibleCharacters: stats.invisibleCharacters, html: () => (0, dom_serializer_1.default)(document) };
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Removes the characters that take no space on screen
|
|
176
|
+
*
|
|
177
|
+
*/
|
|
178
|
+
function visibleText(input) {
|
|
179
|
+
const text = String(input ?? '');
|
|
180
|
+
const matches = text.match(INVISIBLE_CHARS);
|
|
181
|
+
return { text: matches ? text.replace(INVISIBLE_CHARS, '') : text, invisibleCharacters: matches ? matches.length : 0 };
|
|
182
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Generated by scripts/build.js from package.json. Do not edit by hand.
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.version = exports.name = void 0;
|
|
5
|
+
exports.name = "@postalsys/email-ai-tools";
|
|
6
|
+
exports.version = "2.3.0";
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import * as summary from './lib/generate-summary.js';
|
|
2
|
+
import * as models from './lib/list-models.js';
|
|
3
|
+
export type { GenerateSummaryOptions, SummaryAddress, SummaryError, SummaryMessage, SummaryResult, SummaryUsage } from './lib/generate-summary.js';
|
|
4
|
+
export type { ListModelsOptions, ModelDescription, ModelEntry } from './lib/list-models.js';
|
|
5
|
+
export type { Signal } from './lib/signals.js';
|
|
6
|
+
export declare const generateSummary: typeof summary.generateSummary;
|
|
7
|
+
export declare const listModels: typeof models.listModels;
|
|
8
|
+
export declare const describeModel: typeof models.describeModel;
|
|
9
|
+
export declare const DEFAULT_MODEL = "gpt-6-luna";
|
|
10
|
+
export declare const DEFAULT_REASONING_EFFORT = "low";
|
|
11
|
+
export declare const DEFAULT_MAX_TOKENS = 30000;
|
|
12
|
+
export declare const DEFAULT_SYSTEM_PROMPT: string;
|
|
13
|
+
export declare const DEFAULT_INSTRUCTIONS: string;
|
|
14
|
+
export declare const REASONING_EFFORTS: string[];
|
|
15
|
+
export declare const ALLOWED_HEADERS: readonly string[];
|
|
16
|
+
declare const emailAiTools: {
|
|
17
|
+
generateSummary: typeof summary.generateSummary;
|
|
18
|
+
listModels: typeof models.listModels;
|
|
19
|
+
describeModel: typeof models.describeModel;
|
|
20
|
+
DEFAULT_MODEL: string;
|
|
21
|
+
DEFAULT_REASONING_EFFORT: string;
|
|
22
|
+
DEFAULT_MAX_TOKENS: number;
|
|
23
|
+
DEFAULT_SYSTEM_PROMPT: string;
|
|
24
|
+
DEFAULT_INSTRUCTIONS: string;
|
|
25
|
+
REASONING_EFFORTS: string[];
|
|
26
|
+
ALLOWED_HEADERS: readonly string[];
|
|
27
|
+
};
|
|
28
|
+
export default emailAiTools;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// The public entry point. Every member is declared here as its own constant rather than
|
|
2
|
+
// re-exported (`export { x } from`), because the CommonJS output of a re-export is a getter and
|
|
3
|
+
// `require('@postalsys/email-ai-tools')` has always returned plain writable properties.
|
|
4
|
+
import * as summary from './lib/generate-summary.js';
|
|
5
|
+
import * as models from './lib/list-models.js';
|
|
6
|
+
export const generateSummary = summary.generateSummary;
|
|
7
|
+
export const listModels = models.listModels;
|
|
8
|
+
export const describeModel = models.describeModel;
|
|
9
|
+
export const DEFAULT_MODEL = summary.DEFAULT_MODEL;
|
|
10
|
+
export const DEFAULT_REASONING_EFFORT = summary.DEFAULT_REASONING_EFFORT;
|
|
11
|
+
export const DEFAULT_MAX_TOKENS = summary.DEFAULT_MAX_TOKENS;
|
|
12
|
+
export const DEFAULT_SYSTEM_PROMPT = summary.DEFAULT_SYSTEM_PROMPT;
|
|
13
|
+
export const DEFAULT_INSTRUCTIONS = summary.DEFAULT_INSTRUCTIONS;
|
|
14
|
+
export const REASONING_EFFORTS = summary.REASONING_EFFORTS;
|
|
15
|
+
export const ALLOWED_HEADERS = summary.ALLOWED_HEADERS;
|
|
16
|
+
// For `import emailAiTools from '@postalsys/email-ai-tools'`. In the CommonJS build the default
|
|
17
|
+
// export is replaced by the exports object itself, non-enumerable (scripts/build.js)
|
|
18
|
+
const emailAiTools = {
|
|
19
|
+
generateSummary,
|
|
20
|
+
listModels,
|
|
21
|
+
describeModel,
|
|
22
|
+
DEFAULT_MODEL,
|
|
23
|
+
DEFAULT_REASONING_EFFORT,
|
|
24
|
+
DEFAULT_MAX_TOKENS,
|
|
25
|
+
DEFAULT_SYSTEM_PROMPT,
|
|
26
|
+
DEFAULT_INSTRUCTIONS,
|
|
27
|
+
REASONING_EFFORTS,
|
|
28
|
+
ALLOWED_HEADERS
|
|
29
|
+
};
|
|
30
|
+
export default emailAiTools;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Dispatcher } from 'undici';
|
|
2
|
+
/** An error from apiRequest(), with what the API said about it */
|
|
3
|
+
export interface ApiRequestError extends Error {
|
|
4
|
+
code?: string;
|
|
5
|
+
statusCode?: number;
|
|
6
|
+
/** Which request parameter a 400 is about, when the API says */
|
|
7
|
+
param?: string;
|
|
8
|
+
type?: string;
|
|
9
|
+
/** Seconds the API asked to wait, from a 429 */
|
|
10
|
+
retryAfter?: number;
|
|
11
|
+
/** The start of a response that was not JSON */
|
|
12
|
+
responseText?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface ApiRequestOptions {
|
|
15
|
+
/** Default 'post' */
|
|
16
|
+
method?: string | undefined;
|
|
17
|
+
/** Sent as a Bearer token */
|
|
18
|
+
apiToken: string;
|
|
19
|
+
/** JSON request body */
|
|
20
|
+
payload?: unknown;
|
|
21
|
+
/** undici Dispatcher, the module's own agent when omitted */
|
|
22
|
+
dispatcher?: Dispatcher | undefined;
|
|
23
|
+
/** Aborts the request and any retry wait; without one the call is given `timeout` in all */
|
|
24
|
+
signal?: AbortSignal | undefined;
|
|
25
|
+
/** Deadline in milliseconds for the whole call when no signal is given (default DEFAULT_TIMEOUT) */
|
|
26
|
+
timeout?: number | undefined;
|
|
27
|
+
/** Logs the request and the response to stderr */
|
|
28
|
+
verbose?: boolean | undefined;
|
|
29
|
+
}
|
|
30
|
+
declare const DEFAULT_TIMEOUT: number;
|
|
31
|
+
declare const MAX_RESPONSE_SIZE: number;
|
|
32
|
+
/**
|
|
33
|
+
* Sends a request to an OpenAI-compatible API and returns the parsed JSON response body
|
|
34
|
+
*
|
|
35
|
+
* @param url - Request URL
|
|
36
|
+
* @returns Parsed response body and the time the request took in milliseconds
|
|
37
|
+
*/
|
|
38
|
+
declare function apiRequest(url: string, opts: ApiRequestOptions): Promise<{
|
|
39
|
+
data: any;
|
|
40
|
+
time: number;
|
|
41
|
+
}>;
|
|
42
|
+
export { apiRequest, MAX_RESPONSE_SIZE, DEFAULT_TIMEOUT };
|