best-email-builder 0.14.0 → 0.16.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 +189 -0
- package/README.md +13 -6
- package/THIRD-PARTY-NOTICES.md +34 -0
- package/dist/ai-server.js +7 -3
- package/dist/ai-server.js.map +1 -1
- package/dist/ai.js +114 -108
- package/dist/ai.js.map +1 -1
- package/dist/builder.js +8 -8
- package/dist/{chunk-XXUYQ5BX.js → chunk-6OR6W4KJ.js} +268 -55
- package/dist/chunk-6OR6W4KJ.js.map +1 -0
- package/dist/{chunk-O2A2DPFJ.js → chunk-7H7TLQBL.js} +159 -122
- package/dist/chunk-7H7TLQBL.js.map +1 -0
- package/dist/{chunk-R3DWMSSR.js → chunk-AGA3BLRI.js} +2 -2
- package/dist/{chunk-SYEUC7I2.js → chunk-EFE245KW.js} +399 -19
- package/dist/chunk-EFE245KW.js.map +1 -0
- package/dist/{chunk-NEEOZR5C.js → chunk-F5H7ITED.js} +9 -9
- package/dist/chunk-F5H7ITED.js.map +1 -0
- package/dist/{chunk-2ZRGQFX6.js → chunk-HAHHRMQG.js} +2576 -885
- package/dist/chunk-HAHHRMQG.js.map +1 -0
- package/dist/{chunk-OTE7BRKU.js → chunk-MC7JUFHI.js} +4 -4
- package/dist/{chunk-PCSKVVSZ.js → chunk-MSHGCLCA.js} +2 -2
- package/dist/{chunk-VHTV32EW.js → chunk-QLUCTFJ2.js} +311 -3
- package/dist/chunk-QLUCTFJ2.js.map +1 -0
- package/dist/{chunk-7JJ4EZAZ.js → chunk-RPDQABWZ.js} +2 -2
- package/dist/{chunk-SAYIBNH5.js → chunk-W5AJWWHH.js} +159 -20
- package/dist/chunk-W5AJWWHH.js.map +1 -0
- package/dist/de-BNFO6VLR.js +1875 -0
- package/dist/de-BNFO6VLR.js.map +1 -0
- package/dist/en-PBUZLCBA.js +1875 -0
- package/dist/en-PBUZLCBA.js.map +1 -0
- package/dist/es-M7A73ED7.js +1875 -0
- package/dist/es-M7A73ED7.js.map +1 -0
- package/dist/fr-SUFB35ZZ.js +1875 -0
- package/dist/fr-SUFB35ZZ.js.map +1 -0
- package/dist/index.js +10 -10
- package/dist/library.js +5 -5
- package/dist/pt-OEAULFDA.js +1875 -0
- package/dist/pt-OEAULFDA.js.map +1 -0
- package/dist/ru-ZCORYHGA.js +1875 -0
- package/dist/ru-ZCORYHGA.js.map +1 -0
- package/dist/server.js +80 -19
- package/dist/server.js.map +1 -1
- package/dist/styles.css +1 -1
- package/dist/types/ai/errors.d.ts +19 -7
- package/dist/types/ai/index.d.ts +1 -0
- package/dist/types/ai/languagetool.d.ts +46 -0
- package/dist/types/ai/rewrite.d.ts +9 -7
- package/dist/types/ai/server.d.ts +1 -0
- package/dist/types/ai/subjects.d.ts +1 -0
- package/dist/types/blocks/shared.d.ts +12 -9
- package/dist/types/core/EmailEditor.d.ts +85 -12
- package/dist/types/core/blockBox.d.ts +12 -0
- package/dist/types/core/constants.d.ts +1 -1
- package/dist/types/core/mjmlAttributes.d.ts +37 -0
- package/dist/types/core/textFormat.d.ts +4 -0
- package/dist/types/defaultEditor.d.ts +3 -1
- package/dist/types/engine/NativeAdapter.d.ts +5 -2
- package/dist/types/engine/canvas/CanvasView.d.ts +30 -3
- package/dist/types/engine/canvas/canvasStyles.d.ts +1 -1
- package/dist/types/engine/canvas/icons.d.ts +5 -0
- package/dist/types/engine/canvasMjml.d.ts +4 -2
- package/dist/types/engine/mergeTags.d.ts +47 -0
- package/dist/types/engine/mjml.d.ts +7 -0
- package/dist/types/features/brand/brand.d.ts +4 -0
- package/dist/types/features/emoji/EmojiPanel.d.ts +3 -0
- package/dist/types/features/emoji/data/de.d.ts +2 -0
- package/dist/types/features/emoji/data/en.d.ts +2 -0
- package/dist/types/features/emoji/data/es.d.ts +2 -0
- package/dist/types/features/emoji/data/fr.d.ts +2 -0
- package/dist/types/features/emoji/data/pt.d.ts +2 -0
- package/dist/types/features/emoji/data/ru.d.ts +2 -0
- package/dist/types/features/emoji/data/zh.d.ts +2 -0
- package/dist/types/features/emoji/emojis.d.ts +33 -0
- package/dist/types/features/emoji/index.d.ts +2 -0
- package/dist/types/features/variables/VariableDialog.d.ts +16 -4
- package/dist/types/features/variables/VariablesPanel.d.ts +23 -0
- package/dist/types/features/variables/catalog.d.ts +45 -3
- package/dist/types/renderer/columnAlignment.d.ts +10 -0
- package/dist/types/renderer/compileEditorMjml.d.ts +2 -0
- package/dist/types/server/compileMjml.d.ts +5 -0
- package/dist/types/server/handler.d.ts +1 -1
- package/dist/types/server/render.d.ts +15 -4
- package/dist/types/server/sendTest.d.ts +2 -2
- package/dist/types/server/variables.d.ts +7 -2
- package/dist/types/storage/client.d.ts +8 -1
- package/dist/types/ui/BoxModel.d.ts +39 -0
- package/dist/types/ui/CanvasPanel.d.ts +19 -0
- package/dist/types/ui/PropertiesPanel.d.ts +1 -2
- package/dist/types/ui/SidePanel.d.ts +72 -0
- package/dist/types/ui/TextRibbon.d.ts +25 -0
- package/dist/types/ui/fields.d.ts +16 -1
- package/dist/types/ui/primitives/button.d.ts +1 -1
- package/dist/types/ui/primitives/color-picker.d.ts +19 -3
- package/dist/types/ui/primitives/fields.d.ts +5 -0
- package/dist/types/ui/primitives/overlays.d.ts +3 -1
- package/dist/types/ui/useEmailEditorState.d.ts +1 -0
- package/dist/zh-KPGU7EQG.js +1875 -0
- package/dist/zh-KPGU7EQG.js.map +1 -0
- package/package.json +2 -1
- package/dist/chunk-2ZRGQFX6.js.map +0 -1
- package/dist/chunk-NEEOZR5C.js.map +0 -1
- package/dist/chunk-O2A2DPFJ.js.map +0 -1
- package/dist/chunk-SAYIBNH5.js.map +0 -1
- package/dist/chunk-SYEUC7I2.js.map +0 -1
- package/dist/chunk-VHTV32EW.js.map +0 -1
- package/dist/chunk-XXUYQ5BX.js.map +0 -1
- package/dist/types/features/variables/VariablesBar.d.ts +0 -21
- package/dist/types/ui/ElementActions.d.ts +0 -13
- /package/dist/{chunk-R3DWMSSR.js.map → chunk-AGA3BLRI.js.map} +0 -0
- /package/dist/{chunk-OTE7BRKU.js.map → chunk-MC7JUFHI.js.map} +0 -0
- /package/dist/{chunk-PCSKVVSZ.js.map → chunk-MSHGCLCA.js.map} +0 -0
- /package/dist/{chunk-7JJ4EZAZ.js.map → chunk-RPDQABWZ.js.map} +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,195 @@ All notable changes are documented here. The format follows
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [0.16.0] — 2026-10-26
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
- A ribbon above the canvas, always open, as in a word processor: bold,
|
|
13
|
+
italic, underline, strikethrough, link, remove the link, clear formatting,
|
|
14
|
+
font size (typed or picked), line height and alignment, each tool
|
|
15
|
+
named in a tooltip. The word tools act on the selection of the text being
|
|
16
|
+
edited, or on the whole of a selected text, and show what is pressed at the
|
|
17
|
+
caret. `editor.formatText(command, value)` drives them; the editor's
|
|
18
|
+
`format` event reports the format at the caret.
|
|
19
|
+
- An emoji panel, opened from the end of the ribbon: every emoji up to
|
|
20
|
+
Unicode 14 by group, searchable by name or keyword in the interface's
|
|
21
|
+
language (accents optional), the recently used first. Like a variable, an
|
|
22
|
+
emoji under the pointer shows in the text, a click inserts it. The names
|
|
23
|
+
are loaded on first use, one language at a time (emojibase-data, MIT — see
|
|
24
|
+
THIRD-PARTY-NOTICES.md), and only the rows near the scroll are drawn.
|
|
25
|
+
- A feature may declare `drawer: {icon, label, title, panel}`: its button at
|
|
26
|
+
the end of the ribbon opens its panel beside the canvas, one at a time.
|
|
27
|
+
- The Spacing section of the properties is a box diagram, as in a browser's
|
|
28
|
+
developer tools: the margin outside, the border, the padding inside, the
|
|
29
|
+
content in the middle, each zone with its four sides typed in place and a
|
|
30
|
+
line saying what it sets. Every built-in block has the three zones.
|
|
31
|
+
- Borders: a width per side, a style (solid, dashed, dotted, double, groove,
|
|
32
|
+
ridge) and a color. On sections, columns, images and buttons, equal widths
|
|
33
|
+
write one `border` attribute; different ones write `border-top`,
|
|
34
|
+
`border-right`… A double, groove or ridge line starts at 3 px, the least
|
|
35
|
+
that draws it. A block property may take `type: 'border'`, and `box:
|
|
36
|
+
'margin' | 'border' | 'padding'` to place it in the diagram.
|
|
37
|
+
- Spacing MJML has no attribute for (a section's margin, a text's border or
|
|
38
|
+
margin…) is kept in valid MJML: a section's margin as an `mj-wrapper`
|
|
39
|
+
(`css-class="eb-section-margin"`) around it, the others in an encoded
|
|
40
|
+
`eb-box-…` CSS class. The package's compiler (`compileMjml`, the server
|
|
41
|
+
handler) turns them into email tables; another MJML compiler ignores them.
|
|
42
|
+
- Vertical alignment on every block: on a content block it aligns its
|
|
43
|
+
column, on a layout all its columns. Columns of one desktop row now have
|
|
44
|
+
equal heights in the compiled email, their content where it is aligned;
|
|
45
|
+
stacked on mobile, they keep their own height. Outlook keeps MJML's table.
|
|
46
|
+
- The corner radius takes pixels or percentages, with no upper limit (a size
|
|
47
|
+
property may give `units: ['px', '%']`).
|
|
48
|
+
- Variable groups: the *Variables* panel lists the variables under their
|
|
49
|
+
group; the **+** beside *Group* creates an empty group, a group's **+** a
|
|
50
|
+
variable in it. A storage may give `listVariableGroups()` and
|
|
51
|
+
`saveVariableGroup(name)` (HTTP: `GET` and `POST /variable-groups`); a
|
|
52
|
+
server repository `listGroups()` and `insertGroup(name)`. New error codes:
|
|
53
|
+
`INVALID_GROUP`, `DUPLICATE_GROUP`, `NO_GROUP_STORE`.
|
|
54
|
+
- `--eb-accent-color`, set on `:root`, gives the accent of every screen (the
|
|
55
|
+
primary color, the focus ring and the light accent come from it).
|
|
56
|
+
- The storage may give `imageUrl(path)`: where the canvas, and the *Brand*
|
|
57
|
+
dialog for a logo, show an uploaded image when the page cannot load its
|
|
58
|
+
path itself (images on another origin, no server). The template and the
|
|
59
|
+
brand keep the path.
|
|
60
|
+
|
|
61
|
+
- The unsubscribe link is edited like the other variables, since each email
|
|
62
|
+
provider has its own: its *value if empty* is used when the sending gives
|
|
63
|
+
no `unsubscribeUrl` (an address, or the provider's tag such as `*|UNSUB|*`),
|
|
64
|
+
its *sample value* in the preview and test sends (`#` otherwise). Both must
|
|
65
|
+
be an address or a tag, never `javascript:`.
|
|
66
|
+
- Colors keep their transparency: the picker's alpha slider is written
|
|
67
|
+
`rgba()` in the email (MJML refuses `#RRGGBBAA`) and shown as `#RRGGBBAA` in
|
|
68
|
+
the field. Classic Outlook, which reads no `rgba()`, gets in `bgcolor` the
|
|
69
|
+
opaque color it looks like over white.
|
|
70
|
+
- *Background under the content* (Email → Colors): a color painted under the
|
|
71
|
+
sections, at the email's width. The rounded corners of a section show it
|
|
72
|
+
instead of the page background. MJML has no such attribute: it is written
|
|
73
|
+
as an `mj-wrapper` (`css-class="eb-content-layer"`) around the sections and
|
|
74
|
+
read back as `content-background-color` on `mj-body`. A full-width band or
|
|
75
|
+
an `mj-wrapper` of your own interrupts it. Left empty, the MJML is exactly
|
|
76
|
+
as before.
|
|
77
|
+
- *Proofread* without AI: `createLanguageToolProofreader({ url })` (from
|
|
78
|
+
`best-email-builder/ai` and `/ai/server`) is a `proofread` function that
|
|
79
|
+
asks a LanguageTool server. Its corrections show in the same dialog, as the
|
|
80
|
+
smallest change of their words: markup, links and merge tags stay. Style
|
|
81
|
+
suggestions, brands and acronyms, and a capital asked of a subject or a
|
|
82
|
+
button are left out; `ignoreWords` adds your own. Alone, it is the only
|
|
83
|
+
check, with no key; beside a provider, LanguageTool proofreads and the AI
|
|
84
|
+
writes. `LANGUAGETOOL_PUBLIC_API` is LanguageTool's free API, to try it
|
|
85
|
+
out (limited, and the texts go to LanguageTool). New error codes:
|
|
86
|
+
`CHECKER_UNAVAILABLE`, `CHECKER_BUSY`.
|
|
87
|
+
|
|
88
|
+
### Fixed
|
|
89
|
+
- A color dragged in the picker (the area, the hue or the transparency
|
|
90
|
+
slider) paints the block as it moves, rather than when the mouse is
|
|
91
|
+
released. The whole drag is one undo step.
|
|
92
|
+
- Bold, italic… on a text only selected (not being edited) formatted it but
|
|
93
|
+
left it open with everything selected: the next emoji or variable replaced
|
|
94
|
+
the whole text. The format is applied to the whole text and the text is
|
|
95
|
+
closed at once; the ribbon shows the format of its words.
|
|
96
|
+
- An emoji clicked in the panel showed twice for a moment, and the grid moved
|
|
97
|
+
under the pointer as *Recently used* appeared.
|
|
98
|
+
- The live demo showed no uploaded image: it had no server to serve
|
|
99
|
+
`/email-assets/`. It now keeps uploads in the browser (Cache Storage, so
|
|
100
|
+
they outlive a reload) and shows them in the canvas, the preview and the
|
|
101
|
+
exported HTML.
|
|
102
|
+
- Removing the last column of a section left an empty section behind, shown
|
|
103
|
+
as a phantom block: the section goes with it.
|
|
104
|
+
- Dialogs, menus and tooltips could open under the host page's layers or the
|
|
105
|
+
canvas: they stay above.
|
|
106
|
+
|
|
107
|
+
### Changed
|
|
108
|
+
- The space around a button, an image, a divider or the social icons is
|
|
109
|
+
called its margin (MJML's `padding` of these blocks); the button's inner
|
|
110
|
+
padding is its padding.
|
|
111
|
+
- The *Email provider variables* bar above the canvas is now the *Variables*
|
|
112
|
+
panel beside it, opened by the *Variables* button at the end of the ribbon.
|
|
113
|
+
It stays open while you insert, rather than growing under the pointer.
|
|
114
|
+
- *Write with AI* and *Rewrite with AI* are one section with ONE field: say
|
|
115
|
+
what to change in the text, or give a topic to write about — the model
|
|
116
|
+
tells which (`action: 'custom'` now does both; on a text with no words, the
|
|
117
|
+
words are a topic). The one-click rewrites stay under it.
|
|
118
|
+
- While the AI works on a text, it shimmers on the canvas; the answer comes
|
|
119
|
+
in word by word (at once with reduced motion). `editor.setContent` takes
|
|
120
|
+
`{ reveal: true }` for that, and `editor.showWorking(id)` shows the shimmer.
|
|
121
|
+
- The **×** of a variable cut over two lines sits at the variable's end, top
|
|
122
|
+
right of its last line, rather than after its first piece.
|
|
123
|
+
- The *Variables* panel shows the unsubscribe link among the others, with
|
|
124
|
+
its pencil, without a line between them. The panels beside the canvas show
|
|
125
|
+
no heading (their button names them), only their **×**; no line before the
|
|
126
|
+
ribbon's *Emojis* and *Variables* buttons.
|
|
127
|
+
- The *Brand* dialog: its fields are as high as the properties panel's; the
|
|
128
|
+
logo's square, larger and square, opens the file explorer (the *Upload*
|
|
129
|
+
button is gone); the preview is as tall as the fields beside it; the color
|
|
130
|
+
pickers have no transparency slider (brand colors stay opaque).
|
|
131
|
+
- `unsubscribeUrl` is optional: without it, `personalize` and `renderEmail`
|
|
132
|
+
use the link's value if empty, `sendTestEmail` its sample; the handler's
|
|
133
|
+
`previewUnsubscribeUrl` defaults to `null` (the sample, otherwise `#`).
|
|
134
|
+
- The *Proofread* dialog no longer says the AI reads the email: "Every text
|
|
135
|
+
is checked for spelling, grammar and punctuation mistakes", whoever checks.
|
|
136
|
+
|
|
137
|
+
## [0.15.0] — 2026-10-22
|
|
138
|
+
|
|
139
|
+
### Added
|
|
140
|
+
- A variable in the email, under the pointer, swaps its colors (line by line
|
|
141
|
+
when it runs over two, never the words beside it) and shows a **×** in its
|
|
142
|
+
top-right corner. The × takes the tag out, with the space before it, in a
|
|
143
|
+
text (being edited or not) or a button's label. Ctrl+Z brings it back.
|
|
144
|
+
- The side panels are resized by dragging their border towards the canvas or
|
|
145
|
+
away from it (or with the arrow keys once it is focused; a double-click
|
|
146
|
+
restores the default width), and fold to a narrow rail with the round button
|
|
147
|
+
on that border; the same button, on the rail's border, unfolds them. The
|
|
148
|
+
editor remembers both in the browser.
|
|
149
|
+
- Every variable of the bar can be edited, not only the user's own: the
|
|
150
|
+
pencil of an application's variable changes its name, group, sample value
|
|
151
|
+
and value if empty — never its tag. The storage keeps the edit under the
|
|
152
|
+
variable's key (`PUT /variables/:key`, `saveVariable`) and *Restore the
|
|
153
|
+
default* deletes it. The unsubscribe link stays as it is.
|
|
154
|
+
- *Font size* in the *Brand* dialog, next to the font: paragraphs take it
|
|
155
|
+
(13 to 18 px, line height scaled to match), while headings, buttons and
|
|
156
|
+
footers keep their sizes. `cleanBrand` keeps `fontSize`, `applyBrand` applies
|
|
157
|
+
it.
|
|
158
|
+
- System variables' keys may keep the capitals of an email provider's tags,
|
|
159
|
+
as Brevo's `contact.FIRSTNAME` (custom variables keep lowercase keys).
|
|
160
|
+
- The live demo offers Brevo's contact attributes (first name, last name,
|
|
161
|
+
email address, mobile number) in the variables bar, inserted as Brevo's own
|
|
162
|
+
tags (`{{ contact.FIRSTNAME }}`) and shown with their samples in the
|
|
163
|
+
preview.
|
|
164
|
+
|
|
165
|
+
### Changed
|
|
166
|
+
- The side panels fold and unfold smoothly, in the time the canvas takes to
|
|
167
|
+
switch to the mobile view (350 ms): their content keeps its width and slides
|
|
168
|
+
out under the border, fading, instead of disappearing at once. With reduced
|
|
169
|
+
motion, they still fold at once. The panel keeps the border with the canvas
|
|
170
|
+
in both states, so no line doubles while it moves.
|
|
171
|
+
- The *Email provider variables* bar no longer covers the email when it
|
|
172
|
+
opens: the email moves down to make room, and back up when the bar closes.
|
|
173
|
+
Under the pointer, it opens after a short pause, so crossing it on the way
|
|
174
|
+
to the email does not move the email.
|
|
175
|
+
- Wider side panels by default: three blocks in a row on the left, and the
|
|
176
|
+
properties about 40% wider.
|
|
177
|
+
- The properties panel no longer has the *Move up* / *Move down* /
|
|
178
|
+
*Duplicate* / *Delete* row: the toolbar of the selection frame on the canvas
|
|
179
|
+
does it. Columns, social icons and menu links, which are not dragged, get
|
|
180
|
+
arrows in that toolbar to move them among their siblings. With every block
|
|
181
|
+
selected (Ctrl+A), the panel keeps a *Delete* button. Its title no longer
|
|
182
|
+
has a line under it.
|
|
183
|
+
- Denser properties: fields, lists and color pickers are 26 px high (all the
|
|
184
|
+
same), the AI action chips 22 px, with less space between the fields.
|
|
185
|
+
- *Write with AI* and *Rewrite with AI* take the instruction in a box of
|
|
186
|
+
several lines: Enter sends it, Shift+Enter starts a new line.
|
|
187
|
+
- A text box (the preview text, a variable's description…) is resized in
|
|
188
|
+
height only: its width stays the panel's, like the other fields.
|
|
189
|
+
|
|
190
|
+
### Fixed
|
|
191
|
+
- The email preview (the *Preview* dialog and the template library) had two
|
|
192
|
+
scrollbars, the frame's and its box's. The frame now fills the box, so only
|
|
193
|
+
the email scrolls. On a narrow screen, it takes the height the points to
|
|
194
|
+
check leave.
|
|
195
|
+
- The live demo's preview listed its points to check in English whatever the
|
|
196
|
+
page's language; they now follow it, after a switch too.
|
|
197
|
+
|
|
9
198
|
## [0.14.0] — 2026-10-20
|
|
10
199
|
|
|
11
200
|
### Changed
|
package/README.md
CHANGED
|
@@ -52,6 +52,10 @@ once and given away.
|
|
|
52
52
|
- Columns from 1/4 to 3/4 of the row, background images on sections,
|
|
53
53
|
full-width images on phones.
|
|
54
54
|
- Drag with a drop line, type in place, a toolbar and shortcuts on every element.
|
|
55
|
+
- A ribbon above the canvas, as in a word processor: bold, italic, underline,
|
|
56
|
+
strikethrough, link, size, line height and alignment, each tool
|
|
57
|
+
named in a tooltip. At its end, panels beside the canvas for the emojis
|
|
58
|
+
(searchable, named in seven languages) and the variables.
|
|
55
59
|
- Desktop and mobile canvas, showing the real compiled email.
|
|
56
60
|
- Undo and redo, autosave, local drafts, versions, and conflict detection
|
|
57
61
|
between two editors.
|
|
@@ -66,10 +70,12 @@ once and given away.
|
|
|
66
70
|
|
|
67
71
|
**AI, optional**
|
|
68
72
|
- *Proofread* the whole email: spelling, grammar and punctuation in every
|
|
69
|
-
text, each correction shown word by word, applied in one step.
|
|
70
|
-
|
|
71
|
-
- *
|
|
72
|
-
|
|
73
|
+
text, each correction shown word by word, applied in one step. By the AI,
|
|
74
|
+
or without AI by LanguageTool, on your own server.
|
|
75
|
+
- *Write with AI*, in one field: a text on a topic, or a change in your own
|
|
76
|
+
words, to fit the rest of the email; or in one click: shorter, longer,
|
|
77
|
+
fixed, friendlier, more formal, translated into 13 languages. The answer
|
|
78
|
+
comes in word by word.
|
|
73
79
|
- *Suggest* three subject lines drawn from the email.
|
|
74
80
|
- OpenRouter, Gemini, Groq, Mistral, OpenAI, DeepSeek, Cerebras, Ollama, any
|
|
75
81
|
OpenAI-compatible API, Claude or your own agent. The model runs on your
|
|
@@ -145,7 +151,7 @@ the layout, Next.js, Express and image hosting.
|
|
|
145
151
|
| [Editor and library](docs/EDITOR.md) | The three ways to mount them, every prop, theming. |
|
|
146
152
|
| [Server](docs/SERVER.md) | Routes, handler options, the storage contract, your database. |
|
|
147
153
|
| [Variables and sending](docs/VARIABLES.md) | Merge tags, custom variables, personalizing at send time. |
|
|
148
|
-
| [AI](docs/AI.md) | Proofread, Write
|
|
154
|
+
| [AI](docs/AI.md) | Proofread, Write with AI, Subjects. Where the model runs, and the providers. |
|
|
149
155
|
| [Your own AI agent](docs/AGENT.md) | The `agent@1` protocol. |
|
|
150
156
|
| [Brand](docs/BRAND.md) | Logo, colors and font, read from a website. |
|
|
151
157
|
| [Languages](docs/LANGUAGES.md) | Seven built in, your own strings, adding one. |
|
|
@@ -178,7 +184,8 @@ channel described in [SECURITY.md](SECURITY.md).
|
|
|
178
184
|
|
|
179
185
|
## License
|
|
180
186
|
|
|
181
|
-
[MIT](LICENSE). MJML and mjml-browser are MIT too.
|
|
187
|
+
[MIT](LICENSE). MJML and mjml-browser are MIT too. The emoji names come from
|
|
188
|
+
emojibase-data (MIT), see [THIRD-PARTY-NOTICES.md](THIRD-PARTY-NOTICES.md).
|
|
182
189
|
|
|
183
190
|
## Author
|
|
184
191
|
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# Third-party notices
|
|
2
|
+
|
|
3
|
+
best-email-builder is MIT licensed (see [LICENSE](LICENSE)). It ships data
|
|
4
|
+
derived from the following project, under its own license.
|
|
5
|
+
|
|
6
|
+
## emojibase-data
|
|
7
|
+
|
|
8
|
+
The names and keywords of the emoji panel (`src/features/emoji/data/`, built
|
|
9
|
+
by `scripts/build-emoji-data.mjs`) come from
|
|
10
|
+
[emojibase-data](https://github.com/milesj/emojibase) 18.0.0.
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
MIT License
|
|
14
|
+
|
|
15
|
+
Copyright (c) 2017-2019 Miles Johnson
|
|
16
|
+
|
|
17
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
18
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
19
|
+
in the Software without restriction, including without limitation the rights
|
|
20
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
21
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
22
|
+
furnished to do so, subject to the following conditions:
|
|
23
|
+
|
|
24
|
+
The above copyright notice and this permission notice shall be included in all
|
|
25
|
+
copies or substantial portions of the Software.
|
|
26
|
+
|
|
27
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
28
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
29
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
30
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
31
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
32
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
33
|
+
SOFTWARE.
|
|
34
|
+
```
|
package/dist/ai-server.js
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
DEFAULT_CLAUDE_MODEL,
|
|
7
7
|
EmailGenerationError,
|
|
8
8
|
GENERATION_ERRORS,
|
|
9
|
+
LANGUAGETOOL_PUBLIC_API,
|
|
9
10
|
PROOFREAD_SYSTEM_PROMPT,
|
|
10
11
|
REWRITE_ACTIONS,
|
|
11
12
|
REWRITE_LANGUAGES,
|
|
@@ -14,6 +15,7 @@ import {
|
|
|
14
15
|
agentEmailModel,
|
|
15
16
|
claudeEmailModel,
|
|
16
17
|
createEmailGenerator,
|
|
18
|
+
createLanguageToolProofreader,
|
|
17
19
|
createProofreader,
|
|
18
20
|
createRewriter,
|
|
19
21
|
createSubjectSuggester,
|
|
@@ -26,9 +28,9 @@ import {
|
|
|
26
28
|
reviewFix,
|
|
27
29
|
reviewRewrite,
|
|
28
30
|
reviewSubjects
|
|
29
|
-
} from "./chunk-
|
|
30
|
-
import "./chunk-
|
|
31
|
-
import "./chunk-
|
|
31
|
+
} from "./chunk-6OR6W4KJ.js";
|
|
32
|
+
import "./chunk-AGA3BLRI.js";
|
|
33
|
+
import "./chunk-EFE245KW.js";
|
|
32
34
|
|
|
33
35
|
// src/ai/agentEndpoint.js
|
|
34
36
|
var TASKS = ["rewrite", "subjects"];
|
|
@@ -146,6 +148,7 @@ export {
|
|
|
146
148
|
DEFAULT_CLAUDE_MODEL,
|
|
147
149
|
EmailGenerationError,
|
|
148
150
|
GENERATION_ERRORS,
|
|
151
|
+
LANGUAGETOOL_PUBLIC_API,
|
|
149
152
|
PROOFREAD_SYSTEM_PROMPT,
|
|
150
153
|
REWRITE_ACTIONS,
|
|
151
154
|
REWRITE_LANGUAGES,
|
|
@@ -156,6 +159,7 @@ export {
|
|
|
156
159
|
claudeEmailModel,
|
|
157
160
|
createAgentEndpoint,
|
|
158
161
|
createEmailGenerator,
|
|
162
|
+
createLanguageToolProofreader,
|
|
159
163
|
createProofreader,
|
|
160
164
|
createRewriter,
|
|
161
165
|
createSubjectSuggester,
|
package/dist/ai-server.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/ai/agentEndpoint.js"],"sourcesContent":["// @ts-check\n/**\n * THE AGENT'S SIDE of `best-email-builder/agent@1` (see agent.js), for an\n * agent written in JavaScript — and `checkAgent`, to try any agent with the\n * builder's own checks before plugging it in.\n *\n * const endpoint = createAgentEndpoint({\n * complete: async ({ task, system, messages, schema, request }) => {\n * const who = await sessionOf(request); // your rights\n * return myAgent.answer({ system, messages, who }); // text, or the answer as an object\n * },\n * });\n * // Next.js: export const POST = endpoint; Express: app.post('/email-builder', toNodeListener(endpoint))\n *\n * const report = await checkAgent({ url: 'http://localhost:8800/email-builder', headers });\n * // { ok, results: [{ task: 'proofread', ok, ms, attempts, sample }, …] }\n */\n\nimport { AGENT_ERROR_CODES, AGENT_PROTOCOL, agentEmailModel } from './agent.js';\nimport { GENERATION_ERRORS } from './errors.js';\nimport { createEmailGenerator } from './generator.js';\n\nconst TASKS = ['rewrite', 'subjects'];\n/** A repair turn carries the last answer: the texts of an email at most. */\nconst MAX_BODY_BYTES = 2 * 1024 * 1024;\nconst MAX_MESSAGES = 20;\n\n/**\n * @typedef {{\n * task: 'rewrite'|'subjects',\n * system: string,\n * messages: {role: 'user'|'assistant', content: string}[],\n * schema: object,\n * request: Request,\n * signal: AbortSignal,\n * }} AgentTask\n */\n\n/** @param {unknown} body @param {number} status */\nconst json = (body, status = 200) =>\n new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json; charset=utf-8', 'Cache-Control': 'no-store' } });\n\n/** @param {string} error @param {string} code @param {number} status */\nconst refuse = (error, code, status) => json({ error, code }, status);\n\n/**\n * What the builder sent, checked; or why not.\n * @param {any} body\n * @returns {string|null} the problem\n */\nfunction problemOf(body) {\n if (!body || typeof body !== 'object') return 'The body is not a JSON object';\n if (body.protocol !== AGENT_PROTOCOL) return `Unsupported protocol ${JSON.stringify(body.protocol)} — this endpoint speaks ${AGENT_PROTOCOL}`;\n if (!TASKS.includes(body.task)) return `Unknown task ${JSON.stringify(body.task)}`;\n if (typeof body.system !== 'string') return '`system` must be a string';\n if (!body.schema || typeof body.schema !== 'object') return '`schema` must be an object';\n const { messages } = body;\n if (!Array.isArray(messages) || !messages.length || messages.length > MAX_MESSAGES) return `\\`messages\\` must hold 1 to ${MAX_MESSAGES} messages`;\n for (const message of messages) {\n if (!message || (message.role !== 'user' && message.role !== 'assistant') || typeof message.content !== 'string') {\n return 'Each message is { role: \"user\"|\"assistant\", content: string }';\n }\n }\n return null;\n}\n\n/**\n * @param {{\n * complete: (task: AgentTask) => Promise<string | object> | string | object,\n * onError?: (error: unknown, request: Request) => void,\n * }} options\n * `complete`: your agent. It answers with the text of its model, or with\n * the answer as an object. To fail with a reason the builder explains, throw\n * an error whose `code` is one of AGENT_ERROR_CODES ('REFUSED',\n * 'MODEL_BUSY'…); any other error is logged with `onError` and answered as\n * MODEL_ERROR, its message kept out of the response.\n * @returns {(request: Request) => Promise<Response>}\n */\nexport function createAgentEndpoint({ complete, onError = (error) => console.error('[agent]', error) } = /** @type {any} */ ({})) {\n if (typeof complete !== 'function') throw new TypeError('createAgentEndpoint: `complete` must be a function — your agent');\n\n return async function agentEndpoint(request) {\n if (request.method.toUpperCase() !== 'POST') return refuse('POST only', 'METHOD_NOT_ALLOWED', 405);\n const length = Number(request.headers.get('content-length') || 0);\n if (length > MAX_BODY_BYTES) return refuse('Request too large', 'TOO_LARGE', 413);\n const raw = await request.text();\n if (raw.length > MAX_BODY_BYTES) return refuse('Request too large', 'TOO_LARGE', 413);\n /** @type {any} */\n let body;\n try {\n body = JSON.parse(raw);\n } catch {\n return refuse('Invalid JSON', 'INVALID_JSON', 400);\n }\n const problem = problemOf(body);\n if (problem) return refuse(problem, 'INVALID_REQUEST', 400);\n\n try {\n const answer = await complete({\n task: body.task,\n system: body.system,\n messages: body.messages.map((/** @type {any} */ m) => ({ role: m.role, content: m.content })),\n schema: body.schema,\n request,\n signal: request.signal,\n });\n if (typeof answer === 'string') return json({ text: answer });\n if (answer && typeof answer === 'object') return json({ output: answer });\n return refuse('The agent gave no answer', 'MODEL_ERROR', 502);\n } catch (error) {\n const code = /** @type {any} */ (error)?.code;\n if (AGENT_ERROR_CODES.includes(code)) {\n const known = GENERATION_ERRORS[/** @type {keyof typeof GENERATION_ERRORS} */ (code)];\n return refuse(String(/** @type {any} */ (error)?.detail || /** @type {any} */ (error)?.message || known.message), code, known.status);\n }\n onError(error, request);\n return refuse('The agent could not answer', 'MODEL_ERROR', 502);\n }\n };\n}\n\nconst SAMPLE_TEXT =\n '<p>Our summer sale starts on Monday: 30% off everything in store, for three days only. ' +\n 'It ends on Wednesday at midnight. <a href=\"https://example.com/sale\">See the offers</a>.</p>';\n\n/** An email with three mistakes to find, and one text without. @type {import('./proofread.js').ProofreadText[]} */\nconst SAMPLE_EMAIL = [\n { id: 'subject', kind: 'subject', format: 'plain', text: 'Our sumer sale starts on Monday' },\n { id: 'text-1', kind: 'text', format: 'html', text: SAMPLE_TEXT },\n { id: 'text-2', kind: 'text', format: 'html', text: '<p>We hope to see you their. <b>Every</b> product are on sale.</p>' },\n { id: 'button-1', kind: 'button', format: 'plain', text: 'See the offers' },\n];\n\n/**\n * Tries an agent with the builder's own checks: an email proofread, one text\n * rewritten, subject ideas — in turn, each as the builder would ask it (with\n * its repair turn). Three to six calls to the agent.\n * @param {{\n * url: string,\n * headers?: import('./agent.js').AgentHeaders,\n * user?: any,\n * fetch?: typeof fetch,\n * timeout?: number,\n * language?: string,\n * }} options\n * `user`: what `headers` gets, as the handler's `authorize` would give it.\n * @returns {Promise<{ok: boolean, results: {task: string, ok: boolean, ms: number, attempts?: number, sample?: unknown, code?: string, error?: string}[]}>}\n */\nexport async function checkAgent({ url, headers, user, fetch: fetchImpl, timeout, language = 'en' }) {\n const model = agentEmailModel({ url, headers, fetch: fetchImpl, timeout });\n const generator = createEmailGenerator({ model });\n /** @type {[string, () => Promise<any>, (result: any) => unknown][]} */\n const steps = [\n ['proofread', () => generator.proofread({ texts: SAMPLE_EMAIL, language }, { user }), (r) => r.fixes],\n ['rewrite', () => generator.rewrite({ action: 'shorter', text: SAMPLE_TEXT, format: 'html' }, { user }), (r) => r.text],\n [\n 'subjects',\n () => generator.suggestSubjects({ subject: 'Summer sale: 30% off', text: 'Our summer sale starts on Monday: 30% off everything in store, for three days only.', language }, { user }),\n (r) => r.subjects,\n ],\n ];\n const results = [];\n for (const [task, run, sampleOf] of steps) {\n const started = Date.now();\n try {\n const result = await run();\n results.push({ task, ok: true, ms: Date.now() - started, attempts: result.attempts, sample: sampleOf(result) });\n } catch (error) {\n const failure = /** @type {any} */ (error);\n results.push({ task, ok: false, ms: Date.now() - started, code: failure?.code || 'ERROR', error: String(failure?.detail || failure?.message || error) });\n }\n }\n return { ok: results.every((result) => result.ok), results };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsBA,IAAM,QAAQ,CAAC,WAAW,UAAU;AAEpC,IAAM,iBAAiB,IAAI,OAAO;AAClC,IAAM,eAAe;AAcrB,IAAM,OAAO,CAAC,MAAM,SAAS,QAC3B,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG,EAAE,QAAQ,SAAS,EAAE,gBAAgB,mCAAmC,iBAAiB,WAAW,EAAE,CAAC;AAG5I,IAAM,SAAS,CAAC,OAAO,MAAM,WAAW,KAAK,EAAE,OAAO,KAAK,GAAG,MAAM;AAOpE,SAAS,UAAU,MAAM;AACvB,MAAI,CAAC,QAAQ,OAAO,SAAS,SAAU,QAAO;AAC9C,MAAI,KAAK,aAAa,eAAgB,QAAO,wBAAwB,KAAK,UAAU,KAAK,QAAQ,CAAC,gCAA2B,cAAc;AAC3I,MAAI,CAAC,MAAM,SAAS,KAAK,IAAI,EAAG,QAAO,gBAAgB,KAAK,UAAU,KAAK,IAAI,CAAC;AAChF,MAAI,OAAO,KAAK,WAAW,SAAU,QAAO;AAC5C,MAAI,CAAC,KAAK,UAAU,OAAO,KAAK,WAAW,SAAU,QAAO;AAC5D,QAAM,EAAE,SAAS,IAAI;AACrB,MAAI,CAAC,MAAM,QAAQ,QAAQ,KAAK,CAAC,SAAS,UAAU,SAAS,SAAS,aAAc,QAAO,+BAA+B,YAAY;AACtI,aAAW,WAAW,UAAU;AAC9B,QAAI,CAAC,WAAY,QAAQ,SAAS,UAAU,QAAQ,SAAS,eAAgB,OAAO,QAAQ,YAAY,UAAU;AAChH,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;AAcO,SAAS,oBAAoB,EAAE,UAAU,UAAU,CAAC,UAAU,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A,QAAM,UAAU,CAAC;AACjB,aAAW,CAAC,MAAM,KAAK,QAAQ,KAAK,OAAO;AACzC,UAAM,UAAU,KAAK,IAAI;AACzB,QAAI;AACF,YAAM,SAAS,MAAM,IAAI;AACzB,cAAQ,KAAK,EAAE,MAAM,IAAI,MAAM,IAAI,KAAK,IAAI,IAAI,SAAS,UAAU,OAAO,UAAU,QAAQ,SAAS,MAAM,EAAE,CAAC;AAAA,IAChH,SAAS,OAAO;AACd,YAAM;AAAA;AAAA,QAA8B;AAAA;AACpC,cAAQ,KAAK,EAAE,MAAM,IAAI,OAAO,IAAI,KAAK,IAAI,IAAI,SAAS,MAAM,SAAS,QAAQ,SAAS,OAAO,OAAO,SAAS,UAAU,SAAS,WAAW,KAAK,EAAE,CAAC;AAAA,IACzJ;AAAA,EACF;AACA,SAAO,EAAE,IAAI,QAAQ,MAAM,CAAC,WAAW,OAAO,EAAE,GAAG,QAAQ;AAC7D;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/ai/agentEndpoint.js"],"sourcesContent":["// @ts-check\n/**\n * THE AGENT'S SIDE of `best-email-builder/agent@1` (see agent.js), for an\n * agent written in JavaScript — and `checkAgent`, to try any agent with the\n * builder's own checks before plugging it in.\n *\n * const endpoint = createAgentEndpoint({\n * complete: async ({ task, system, messages, schema, request }) => {\n * const who = await sessionOf(request); // your rights\n * return myAgent.answer({ system, messages, who }); // text, or the answer as an object\n * },\n * });\n * // Next.js: export const POST = endpoint; Express: app.post('/email-builder', toNodeListener(endpoint))\n *\n * const report = await checkAgent({ url: 'http://localhost:8800/email-builder', headers });\n * // { ok, results: [{ task: 'proofread', ok, ms, attempts, sample }, …] }\n */\n\nimport { AGENT_ERROR_CODES, AGENT_PROTOCOL, agentEmailModel } from './agent.js';\nimport { GENERATION_ERRORS } from './errors.js';\nimport { createEmailGenerator } from './generator.js';\n\nconst TASKS = ['rewrite', 'subjects'];\n/** A repair turn carries the last answer: the texts of an email at most. */\nconst MAX_BODY_BYTES = 2 * 1024 * 1024;\nconst MAX_MESSAGES = 20;\n\n/**\n * @typedef {{\n * task: 'rewrite'|'subjects',\n * system: string,\n * messages: {role: 'user'|'assistant', content: string}[],\n * schema: object,\n * request: Request,\n * signal: AbortSignal,\n * }} AgentTask\n */\n\n/** @param {unknown} body @param {number} status */\nconst json = (body, status = 200) =>\n new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json; charset=utf-8', 'Cache-Control': 'no-store' } });\n\n/** @param {string} error @param {string} code @param {number} status */\nconst refuse = (error, code, status) => json({ error, code }, status);\n\n/**\n * What the builder sent, checked; or why not.\n * @param {any} body\n * @returns {string|null} the problem\n */\nfunction problemOf(body) {\n if (!body || typeof body !== 'object') return 'The body is not a JSON object';\n if (body.protocol !== AGENT_PROTOCOL) return `Unsupported protocol ${JSON.stringify(body.protocol)} — this endpoint speaks ${AGENT_PROTOCOL}`;\n if (!TASKS.includes(body.task)) return `Unknown task ${JSON.stringify(body.task)}`;\n if (typeof body.system !== 'string') return '`system` must be a string';\n if (!body.schema || typeof body.schema !== 'object') return '`schema` must be an object';\n const { messages } = body;\n if (!Array.isArray(messages) || !messages.length || messages.length > MAX_MESSAGES) return `\\`messages\\` must hold 1 to ${MAX_MESSAGES} messages`;\n for (const message of messages) {\n if (!message || (message.role !== 'user' && message.role !== 'assistant') || typeof message.content !== 'string') {\n return 'Each message is { role: \"user\"|\"assistant\", content: string }';\n }\n }\n return null;\n}\n\n/**\n * @param {{\n * complete: (task: AgentTask) => Promise<string | object> | string | object,\n * onError?: (error: unknown, request: Request) => void,\n * }} options\n * `complete`: your agent. It answers with the text of its model, or with\n * the answer as an object. To fail with a reason the builder explains, throw\n * an error whose `code` is one of AGENT_ERROR_CODES ('REFUSED',\n * 'MODEL_BUSY'…); any other error is logged with `onError` and answered as\n * MODEL_ERROR, its message kept out of the response.\n * @returns {(request: Request) => Promise<Response>}\n */\nexport function createAgentEndpoint({ complete, onError = (error) => console.error('[agent]', error) } = /** @type {any} */ ({})) {\n if (typeof complete !== 'function') throw new TypeError('createAgentEndpoint: `complete` must be a function — your agent');\n\n return async function agentEndpoint(request) {\n if (request.method.toUpperCase() !== 'POST') return refuse('POST only', 'METHOD_NOT_ALLOWED', 405);\n const length = Number(request.headers.get('content-length') || 0);\n if (length > MAX_BODY_BYTES) return refuse('Request too large', 'TOO_LARGE', 413);\n const raw = await request.text();\n if (raw.length > MAX_BODY_BYTES) return refuse('Request too large', 'TOO_LARGE', 413);\n /** @type {any} */\n let body;\n try {\n body = JSON.parse(raw);\n } catch {\n return refuse('Invalid JSON', 'INVALID_JSON', 400);\n }\n const problem = problemOf(body);\n if (problem) return refuse(problem, 'INVALID_REQUEST', 400);\n\n try {\n const answer = await complete({\n task: body.task,\n system: body.system,\n messages: body.messages.map((/** @type {any} */ m) => ({ role: m.role, content: m.content })),\n schema: body.schema,\n request,\n signal: request.signal,\n });\n if (typeof answer === 'string') return json({ text: answer });\n if (answer && typeof answer === 'object') return json({ output: answer });\n return refuse('The agent gave no answer', 'MODEL_ERROR', 502);\n } catch (error) {\n const code = /** @type {any} */ (error)?.code;\n if (AGENT_ERROR_CODES.includes(code)) {\n const known = GENERATION_ERRORS[/** @type {keyof typeof GENERATION_ERRORS} */ (code)];\n return refuse(String(/** @type {any} */ (error)?.detail || /** @type {any} */ (error)?.message || known.message), code, known.status);\n }\n onError(error, request);\n return refuse('The agent could not answer', 'MODEL_ERROR', 502);\n }\n };\n}\n\nconst SAMPLE_TEXT =\n '<p>Our summer sale starts on Monday: 30% off everything in store, for three days only. ' +\n 'It ends on Wednesday at midnight. <a href=\"https://example.com/sale\">See the offers</a>.</p>';\n\n/** An email with three mistakes to find, and one text without. @type {import('./proofread.js').ProofreadText[]} */\nconst SAMPLE_EMAIL = [\n { id: 'subject', kind: 'subject', format: 'plain', text: 'Our sumer sale starts on Monday' },\n { id: 'text-1', kind: 'text', format: 'html', text: SAMPLE_TEXT },\n { id: 'text-2', kind: 'text', format: 'html', text: '<p>We hope to see you their. <b>Every</b> product are on sale.</p>' },\n { id: 'button-1', kind: 'button', format: 'plain', text: 'See the offers' },\n];\n\n/**\n * Tries an agent with the builder's own checks: an email proofread, one text\n * rewritten, subject ideas — in turn, each as the builder would ask it (with\n * its repair turn). Three to six calls to the agent.\n * @param {{\n * url: string,\n * headers?: import('./agent.js').AgentHeaders,\n * user?: any,\n * fetch?: typeof fetch,\n * timeout?: number,\n * language?: string,\n * }} options\n * `user`: what `headers` gets, as the handler's `authorize` would give it.\n * @returns {Promise<{ok: boolean, results: {task: string, ok: boolean, ms: number, attempts?: number, sample?: unknown, code?: string, error?: string}[]}>}\n */\nexport async function checkAgent({ url, headers, user, fetch: fetchImpl, timeout, language = 'en' }) {\n const model = agentEmailModel({ url, headers, fetch: fetchImpl, timeout });\n const generator = createEmailGenerator({ model });\n /** @type {[string, () => Promise<any>, (result: any) => unknown][]} */\n const steps = [\n ['proofread', () => generator.proofread({ texts: SAMPLE_EMAIL, language }, { user }), (r) => r.fixes],\n ['rewrite', () => generator.rewrite({ action: 'shorter', text: SAMPLE_TEXT, format: 'html' }, { user }), (r) => r.text],\n [\n 'subjects',\n () => generator.suggestSubjects({ subject: 'Summer sale: 30% off', text: 'Our summer sale starts on Monday: 30% off everything in store, for three days only.', language }, { user }),\n (r) => r.subjects,\n ],\n ];\n const results = [];\n for (const [task, run, sampleOf] of steps) {\n const started = Date.now();\n try {\n const result = await run();\n results.push({ task, ok: true, ms: Date.now() - started, attempts: result.attempts, sample: sampleOf(result) });\n } catch (error) {\n const failure = /** @type {any} */ (error);\n results.push({ task, ok: false, ms: Date.now() - started, code: failure?.code || 'ERROR', error: String(failure?.detail || failure?.message || error) });\n }\n }\n return { ok: results.every((result) => result.ok), results };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsBA,IAAM,QAAQ,CAAC,WAAW,UAAU;AAEpC,IAAM,iBAAiB,IAAI,OAAO;AAClC,IAAM,eAAe;AAcrB,IAAM,OAAO,CAAC,MAAM,SA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