@workover/cli 0.26.0 → 0.29.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/bin/workover.mjs +2312 -5
- package/contacts.mjs +1561 -0
- package/domains.mjs +417 -0
- package/email-studio.mjs +829 -0
- package/feedback-board.mjs +321 -0
- package/journeys.mjs +409 -0
- package/lib.mjs +56 -0
- package/package.json +20 -1
- package/support-agent-tests.mjs +424 -0
- package/support-config.mjs +302 -0
- package/support-email.mjs +458 -0
- package/support-fields.mjs +294 -0
- package/support-freshdesk.mjs +176 -0
- package/support-inbox.mjs +313 -0
- package/support-label-rules.mjs +136 -0
- package/support-labels.mjs +244 -0
- package/support-outcomes.mjs +276 -0
- package/support-outside-bot.mjs +145 -0
- package/support-setup.mjs +660 -0
- package/support-signatures.mjs +156 -0
- package/support-sources.mjs +233 -0
- package/support-walter.mjs +310 -0
package/email-studio.mjs
ADDED
|
@@ -0,0 +1,829 @@
|
|
|
1
|
+
// Emails designed in blocks from a terminal (email studio step 3, WO-1752): `workover email ...`
|
|
2
|
+
// and the MCP server's `workover_email_*` tools.
|
|
3
|
+
//
|
|
4
|
+
// THE SAME FILE ships in @workover/cli (apps/cli/email-studio.mjs) and @workover/mcp
|
|
5
|
+
// (apps/mcp/email-studio.mjs), because the two packages are published separately and neither may
|
|
6
|
+
// depend on the other. `apps/mcp/email-studio.test.mjs` fails when the copies differ.
|
|
7
|
+
//
|
|
8
|
+
// An email designed in blocks is JSON: {"version": 1, "base"?: {...}, "tokens"?: {...}, "blocks":
|
|
9
|
+
// [...]}. `blocks` lists every block with its properties, defaults and an example, and the JSON
|
|
10
|
+
// Schema of the whole document; `render` compiles it to the HTML and text that are stored for
|
|
11
|
+
// sending, Liquid intact; `preview` fills it for one person (a contact, sample data, or the sample
|
|
12
|
+
// contact), strictly: a merge tag nothing fills is refused with the field and the JSON pointer.
|
|
13
|
+
// Nothing here saves or sends. A journey's email step takes the same document as `blocks` in place
|
|
14
|
+
// of `body` (`workover contacts journeys create|update --file`).
|
|
15
|
+
//
|
|
16
|
+
// `import` (step 4) brings a Customer.io email across as a NEW draft, never over one that exists:
|
|
17
|
+
// a Design Studio source with the workspace's global styles and the node's envelope, or a
|
|
18
|
+
// rich-text body in its layout. Its report lists everything that did not map one to one; nothing
|
|
19
|
+
// is dropped. `templates` lists and reads the drafts.
|
|
20
|
+
//
|
|
21
|
+
// Emails kept on their own (step B) have a draft and the versions it was published as: `new`
|
|
22
|
+
// makes one, `save <id>` saves the draft (naming the revision read, so no edit is lost),
|
|
23
|
+
// `publish <id>` snapshots it once its check passes, `versions <id>` lists them and `restore`
|
|
24
|
+
// copies one back into the draft. `templates <id> --out` writes the draft with its revision.
|
|
25
|
+
//
|
|
26
|
+
// Layouts (step 6): `layouts [<id>]` lists them or shows one (--out writes its HTML); `layout new
|
|
27
|
+
// --name --file layout.html`, `layout save <id> --file layout.html --revision <n>`, `layout delete
|
|
28
|
+
// <id>`. A note is HTML blocks in a layout, as Customer.io's rich-text emails are.
|
|
29
|
+
//
|
|
30
|
+
// `check` (step A) answers the QA checks and a readiness score; `test` sends the email to you,
|
|
31
|
+
// filled for a contact; `preview --images-blocked` shows it as a client that blocks images.
|
|
32
|
+
//
|
|
33
|
+
// `brand-kit` (step 5) reads and saves the workspace's brand kit: tokens, text styles and buttons
|
|
34
|
+
// every email is drawn with. An email references a kit token as {"token": "colors.<name>_<id>"};
|
|
35
|
+
// the id decides. A save names the version it read, and a token it removes is frozen into each
|
|
36
|
+
// email that used it. An import links its email to the kit unless told not to (`--no-link`).
|
|
37
|
+
//
|
|
38
|
+
// Every call goes to a route opened to tokens holding the `contacts` scope (`workover login
|
|
39
|
+
// --contacts`), and any teammate may use them, except saving an import or a brand kit: owners and
|
|
40
|
+
// admins.
|
|
41
|
+
|
|
42
|
+
import { readFileSync } from 'node:fs';
|
|
43
|
+
|
|
44
|
+
import { apiError, resolveWorkspace } from './site-setup.mjs';
|
|
45
|
+
|
|
46
|
+
export const EMAIL_HELP =
|
|
47
|
+
'An email in blocks is JSON: {"version": 1, "base": {"fontFamily", "color", "background", ...}, "tokens": {"colors": {"brand": "#3E63DD"}}, "blocks": [{"type": "section", "children": [{"type": "heading", "level": 1, "text": "Hi {{ customer.first_name }}"}, {"type": "button", "text": "Open", "href": "https://..."}]}]}. `workover email blocks` lists every block.';
|
|
48
|
+
|
|
49
|
+
const base = (ws) => `/workspace/${encodeURIComponent(ws.id)}/email`;
|
|
50
|
+
|
|
51
|
+
/** An email file: the blocks document, or {"blocks": {...}, "subject"?, "preheader"?}. */
|
|
52
|
+
export function emailFromFile(path) {
|
|
53
|
+
let raw;
|
|
54
|
+
try {
|
|
55
|
+
raw = readFileSync(String(path), 'utf8');
|
|
56
|
+
} catch {
|
|
57
|
+
throw apiError(400, `Cannot read ${path}.`);
|
|
58
|
+
}
|
|
59
|
+
let v;
|
|
60
|
+
try {
|
|
61
|
+
v = JSON.parse(raw);
|
|
62
|
+
} catch {
|
|
63
|
+
throw apiError(400, `${path} is not JSON. ${EMAIL_HELP}`);
|
|
64
|
+
}
|
|
65
|
+
return emailOf(v);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** {blocks, subject?, preheader?} from either a bare document or a wrapper holding one. */
|
|
69
|
+
export function emailOf(v) {
|
|
70
|
+
if (!v || typeof v !== 'object' || Array.isArray(v)) throw apiError(400, `An email is a JSON object. ${EMAIL_HELP}`);
|
|
71
|
+
if (v.version !== undefined && Array.isArray(v.blocks)) return { blocks: v };
|
|
72
|
+
const out = { blocks: v.blocks };
|
|
73
|
+
if (v.subject !== undefined) out.subject = v.subject;
|
|
74
|
+
if (v.preheader !== undefined) out.preheader = v.preheader;
|
|
75
|
+
if (v.name !== undefined) out.name = v.name;
|
|
76
|
+
if (v.revision !== undefined) out.revision = v.revision;
|
|
77
|
+
if (out.blocks === undefined) throw apiError(400, `Give the email's blocks. ${EMAIL_HELP}`);
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ── Calls ─────────────────────────────────────────────────────────────────────────────────────────
|
|
82
|
+
|
|
83
|
+
export async function listBlocks(call, { workspace } = {}) {
|
|
84
|
+
const ws = await resolveWorkspace(call, workspace);
|
|
85
|
+
return { workspace: ws, ...(await call(`${base(ws)}/blocks`)) };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export async function renderEmail(call, { workspace, email } = {}) {
|
|
89
|
+
const ws = await resolveWorkspace(call, workspace);
|
|
90
|
+
const res = await call(`${base(ws)}/render`, { method: 'POST', body: { blocks: email?.blocks } });
|
|
91
|
+
return { workspace: ws, ...res };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The email for one person, as preview, check and test take it. */
|
|
95
|
+
function fillBody({ email, subject, preheader, contact, data }) {
|
|
96
|
+
const body = { blocks: email?.blocks, subject: subject ?? email?.subject ?? '' };
|
|
97
|
+
const pre = preheader ?? email?.preheader;
|
|
98
|
+
if (pre !== undefined) body.preheader = pre;
|
|
99
|
+
if (contact !== undefined && contact !== null && contact !== '') body.contact = contact;
|
|
100
|
+
if (data !== undefined) body.data = data;
|
|
101
|
+
return body;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** `contact`: an id, an email or x:<external id>; `data`: {name, email, externalId, attributes}. */
|
|
105
|
+
export async function previewEmail(call, { workspace, imagesBlocked, ...rest } = {}) {
|
|
106
|
+
const ws = await resolveWorkspace(call, workspace);
|
|
107
|
+
const body = { ...fillBody(rest), ...(imagesBlocked ? { imagesBlocked: true } : {}) };
|
|
108
|
+
const res = await call(`${base(ws)}/preview`, { method: 'POST', body });
|
|
109
|
+
return { workspace: ws, ...res };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** The QA checks and the readiness score, for the email filled as preview fills it. */
|
|
113
|
+
export async function checkEmail(call, { workspace, ...rest } = {}) {
|
|
114
|
+
const ws = await resolveWorkspace(call, workspace);
|
|
115
|
+
const res = await call(`${base(ws)}/check`, { method: 'POST', body: fillBody(rest) });
|
|
116
|
+
return { workspace: ws, ...res };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** A test send to the person asking; `fromAddressId` picks the sending address. */
|
|
120
|
+
export async function testEmail(call, { workspace, fromAddressId, ...rest } = {}) {
|
|
121
|
+
const ws = await resolveWorkspace(call, workspace);
|
|
122
|
+
const body = fillBody(rest);
|
|
123
|
+
if (fromAddressId !== undefined && fromAddressId !== null && fromAddressId !== '') {
|
|
124
|
+
if (!/^\d+$/.test(String(fromAddressId))) throw apiError(400, 'A sending address id is a number.');
|
|
125
|
+
body.fromAddressId = Number(fromAddressId);
|
|
126
|
+
}
|
|
127
|
+
const res = await call(`${base(ws)}/test`, { method: 'POST', body });
|
|
128
|
+
return { workspace: ws, ...res };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** A file's text, or its JSON when `json`; a clear 400 when it cannot be read. */
|
|
132
|
+
function fileOf(path, json) {
|
|
133
|
+
let raw;
|
|
134
|
+
try {
|
|
135
|
+
raw = readFileSync(String(path), 'utf8');
|
|
136
|
+
} catch {
|
|
137
|
+
throw apiError(400, `Cannot read ${path}.`);
|
|
138
|
+
}
|
|
139
|
+
if (!json) return raw;
|
|
140
|
+
try {
|
|
141
|
+
return JSON.parse(raw);
|
|
142
|
+
} catch {
|
|
143
|
+
throw apiError(400, `${path} is not JSON.`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* An import's body from files: `source` (a Design Studio .ds-source.html) or `body` (a rich-text
|
|
149
|
+
* email's HTML) with its `layout`; `globalStyles` and `node` as JSON files, as Customer.io answers
|
|
150
|
+
* them (`/ds/global-styles`, the node of the template).
|
|
151
|
+
*/
|
|
152
|
+
export function importFromFiles({ source, body, layout, layoutName, layoutId, text, globalStyles, node, name, templateId, dryRun, noLink } = {}) {
|
|
153
|
+
if (!source && !body) throw apiError(400, 'Give --source <file.ds-source.html> (Design Studio) or --body <file.html> (rich text).');
|
|
154
|
+
const out = {};
|
|
155
|
+
if (source) out.source = fileOf(source, false);
|
|
156
|
+
if (body) out.body = fileOf(body, false);
|
|
157
|
+
if (layout) out.layout = fileOf(layout, false);
|
|
158
|
+
if (layoutName) out.layoutName = String(layoutName);
|
|
159
|
+
if (text) out.text = fileOf(text, false);
|
|
160
|
+
if (globalStyles) out.globalStyles = fileOf(globalStyles, true);
|
|
161
|
+
if (node) out.node = fileOf(node, true);
|
|
162
|
+
if (name) out.name = String(name);
|
|
163
|
+
const given = (v) => v !== undefined && v !== null && v !== '';
|
|
164
|
+
if (given(templateId) || given(layoutId))
|
|
165
|
+
out.customerio = { ...(given(templateId) ? { templateId: String(templateId) } : {}), ...(given(layoutId) ? { layoutId: String(layoutId) } : {}) };
|
|
166
|
+
if (dryRun) out.dryRun = true;
|
|
167
|
+
if (noLink) out.link = false;
|
|
168
|
+
return out;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export async function importEmail(call, { workspace, ...body } = {}) {
|
|
172
|
+
const ws = await resolveWorkspace(call, workspace);
|
|
173
|
+
const res = await call(`${base(ws)}/import`, { method: 'POST', body });
|
|
174
|
+
return { workspace: ws, ...res };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export async function listTemplates(call, { workspace } = {}) {
|
|
178
|
+
const ws = await resolveWorkspace(call, workspace);
|
|
179
|
+
return { workspace: ws, ...(await call(`${base(ws)}/templates`)) };
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export async function getTemplate(call, { workspace, id } = {}) {
|
|
183
|
+
const ws = await resolveWorkspace(call, workspace);
|
|
184
|
+
if (!/^\d+$/.test(String(id ?? ''))) throw apiError(400, 'An email id is a number; `workover email templates` lists them.');
|
|
185
|
+
return { workspace: ws, ...(await call(`${base(ws)}/templates/${encodeURIComponent(String(id))}`)) };
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const idOf = (id, what = 'An email id') => {
|
|
189
|
+
if (!/^\d+$/.test(String(id ?? ''))) throw apiError(400, `${what} is a number; \`workover email templates\` lists them.`);
|
|
190
|
+
return encodeURIComponent(String(id));
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
/** A new email: {name, subject?, preheader?, blocks?}. */
|
|
194
|
+
export async function createTemplate(call, { workspace, ...body } = {}) {
|
|
195
|
+
const ws = await resolveWorkspace(call, workspace);
|
|
196
|
+
return { workspace: ws, ...(await call(`${base(ws)}/templates`, { method: 'POST', body })) };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** A save of the draft: any of name, subject, preheader, blocks, with the revision read. */
|
|
200
|
+
export async function saveTemplate(call, { workspace, id, ...body } = {}) {
|
|
201
|
+
const ws = await resolveWorkspace(call, workspace);
|
|
202
|
+
if (!Number.isInteger(body.revision))
|
|
203
|
+
throw apiError(400, 'Say which revision this edits: save the file `workover email templates <id> --out` writes, or add --revision <n>.');
|
|
204
|
+
return { workspace: ws, ...(await call(`${base(ws)}/templates/${idOf(id)}`, { method: 'PUT', body })) };
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export async function publishTemplate(call, { workspace, id, revision, note, ignoreChecks } = {}) {
|
|
208
|
+
const ws = await resolveWorkspace(call, workspace);
|
|
209
|
+
const body = { revision, ...(note ? { note } : {}), ...(ignoreChecks ? { ignoreChecks: true } : {}) };
|
|
210
|
+
return { workspace: ws, ...(await call(`${base(ws)}/templates/${idOf(id)}/publish`, { method: 'POST', body })) };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export async function listVersions(call, { workspace, id } = {}) {
|
|
214
|
+
const ws = await resolveWorkspace(call, workspace);
|
|
215
|
+
return { workspace: ws, ...(await call(`${base(ws)}/templates/${idOf(id)}/versions`)) };
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export async function getVersion(call, { workspace, id, version } = {}) {
|
|
219
|
+
const ws = await resolveWorkspace(call, workspace);
|
|
220
|
+
return { workspace: ws, ...(await call(`${base(ws)}/templates/${idOf(id)}/versions/${idOf(version, 'A version')}`)) };
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export async function restoreVersion(call, { workspace, id, version, revision } = {}) {
|
|
224
|
+
const ws = await resolveWorkspace(call, workspace);
|
|
225
|
+
const path = `${base(ws)}/templates/${idOf(id)}/versions/${idOf(version, 'A version')}/restore`;
|
|
226
|
+
return { workspace: ws, ...(await call(path, { method: 'POST', body: { revision } })) };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export async function listLayouts(call, { workspace } = {}) {
|
|
230
|
+
const ws = await resolveWorkspace(call, workspace);
|
|
231
|
+
return { workspace: ws, ...(await call(`${base(ws)}/layouts`)) };
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export async function getLayout(call, { workspace, id } = {}) {
|
|
235
|
+
const ws = await resolveWorkspace(call, workspace);
|
|
236
|
+
return { workspace: ws, ...(await call(`${base(ws)}/layouts/${idOf(id, 'A layout id')}`)) };
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export async function createLayout(call, { workspace, name, html } = {}) {
|
|
240
|
+
const ws = await resolveWorkspace(call, workspace);
|
|
241
|
+
return { workspace: ws, ...(await call(`${base(ws)}/layouts`, { method: 'POST', body: { name, html } })) };
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export async function saveLayout(call, { workspace, id, revision, name, html } = {}) {
|
|
245
|
+
const ws = await resolveWorkspace(call, workspace);
|
|
246
|
+
if (!Number.isInteger(revision)) throw apiError(400, 'Say which revision this edits (`workover email layouts <id>` shows it): --revision <n>.');
|
|
247
|
+
const body = { revision, ...(name !== undefined ? { name } : {}), ...(html !== undefined ? { html } : {}) };
|
|
248
|
+
return { workspace: ws, ...(await call(`${base(ws)}/layouts/${idOf(id, 'A layout id')}`, { method: 'PUT', body })) };
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export async function deleteLayout(call, { workspace, id } = {}) {
|
|
252
|
+
const ws = await resolveWorkspace(call, workspace);
|
|
253
|
+
return { workspace: ws, ...(await call(`${base(ws)}/layouts/${idOf(id, 'A layout id')}`, { method: 'DELETE' })) };
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const usedByLine = (u) => {
|
|
257
|
+
if (!u) return null;
|
|
258
|
+
const parts = [
|
|
259
|
+
u.templates?.length && `${u.templates.length} email${u.templates.length === 1 ? '' : 's'} (${u.templates.map((t) => `#${t.id}`).join(', ')})`,
|
|
260
|
+
u.versions && `${u.versions} published version${u.versions === 1 ? '' : 's'}`,
|
|
261
|
+
u.journeySteps?.length && `${u.journeySteps.length} journey step${u.journeySteps.length === 1 ? '' : 's'}`,
|
|
262
|
+
].filter(Boolean);
|
|
263
|
+
return parts.length ? `Used by ${parts.join(', ')}.` : 'Not used by any email yet.';
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
export function renderLayouts(res) {
|
|
267
|
+
const rows = res.layouts ?? [];
|
|
268
|
+
const out = ['', ` ${rows.length} layout${rows.length === 1 ? '' : 's'}.`, ''];
|
|
269
|
+
for (const l of rows) out.push(` #${l.id} ${l.name} (revision ${l.revision})`);
|
|
270
|
+
if (rows.length) out.push('');
|
|
271
|
+
out.push(
|
|
272
|
+
' A note is {"version": 1, "layout": {"id": <id>}, "blocks": [{"type": "html", "html": "..."}]}: its HTML in place of the layout\'s {{ content }}.',
|
|
273
|
+
'',
|
|
274
|
+
);
|
|
275
|
+
return out;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export function renderLayout(res, verb) {
|
|
279
|
+
const l = res.layout;
|
|
280
|
+
const head = verb ? ` ✓ ${verb} layout #${l.id} "${l.name}", revision ${l.revision}.` : ` #${l.id} "${l.name}", revision ${l.revision}.`;
|
|
281
|
+
const used = usedByLine(res.usedBy);
|
|
282
|
+
return ['', head, ...(used ? [` ${used}`] : []), ''];
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** The draft as a file to edit and save back: what `save` reads its revision from. */
|
|
286
|
+
export const draftFile = (t) => ({ name: t.name, subject: t.subject, preheader: t.preheader, revision: t.revision, blocks: t.blocks });
|
|
287
|
+
|
|
288
|
+
export async function getBrandKit(call, { workspace } = {}) {
|
|
289
|
+
const ws = await resolveWorkspace(call, workspace);
|
|
290
|
+
return { workspace: ws, ...(await call(`${base(ws)}/brand-kit`)) };
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* A brand kit file: what `brand-kit --out` writes ({"kit": {...}, "version": n}), or a bare kit
|
|
295
|
+
* ({"version": 1, "tokens", ...}) with the version read given apart.
|
|
296
|
+
*/
|
|
297
|
+
export function brandKitFromFile(path, version) {
|
|
298
|
+
const raw = fileOf(path, true);
|
|
299
|
+
const wrapped = raw && typeof raw === 'object' && raw.kit && typeof raw.kit === 'object';
|
|
300
|
+
const kit = wrapped ? raw.kit : raw;
|
|
301
|
+
const v = version !== undefined && version !== null && version !== '' ? Number(version) : wrapped ? raw.version : undefined;
|
|
302
|
+
if (!Number.isInteger(v))
|
|
303
|
+
throw apiError(400, 'Say which version of the kit this edits: save the file `workover email brand-kit --out kit.json` writes, or add --kit-version <n>.');
|
|
304
|
+
return { kit, version: v };
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
export async function saveBrandKit(call, { workspace, kit, version, dryRun } = {}) {
|
|
308
|
+
const ws = await resolveWorkspace(call, workspace);
|
|
309
|
+
const body = { kit, version, ...(dryRun ? { dryRun: true } : {}) };
|
|
310
|
+
return { workspace: ws, ...(await call(`${base(ws)}/brand-kit`, { method: 'PUT', body })) };
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// ── Rendering ─────────────────────────────────────────────────────────────────────────────────────
|
|
314
|
+
|
|
315
|
+
const pairs = (p) =>
|
|
316
|
+
[p.lightDark ? 'light/dark' : '', p.phoneDesktop ? 'phone/desktop' : '', p.token ? `token ${p.token}` : '']
|
|
317
|
+
.filter(Boolean)
|
|
318
|
+
.join(', ');
|
|
319
|
+
|
|
320
|
+
export function renderBlocks(res, { type } = {}) {
|
|
321
|
+
const blocks = (res.blocks ?? []).filter((b) => !type || b.type === type);
|
|
322
|
+
if (type && !blocks.length) throw apiError(404, `There is no ${type} block; \`workover email blocks\` lists them.`);
|
|
323
|
+
const out = [''];
|
|
324
|
+
for (const b of blocks) {
|
|
325
|
+
out.push(` ${b.type}${b.children ? ` (holds ${b.children.join(', ')})` : ''}`, ` ${b.describe}`);
|
|
326
|
+
if (type) {
|
|
327
|
+
for (const p of b.props) {
|
|
328
|
+
const extra = [
|
|
329
|
+
p.required ? 'required' : '',
|
|
330
|
+
p.default !== undefined ? `default ${JSON.stringify(p.default)}` : '',
|
|
331
|
+
p.values ? `one of ${p.values.join(' | ')}` : '',
|
|
332
|
+
pairs(p),
|
|
333
|
+
]
|
|
334
|
+
.filter(Boolean)
|
|
335
|
+
.join('; ');
|
|
336
|
+
out.push(` ${p.name} (${p.kind})${extra ? ` [${extra}]` : ''}: ${p.describe}`);
|
|
337
|
+
}
|
|
338
|
+
out.push(' Example:', ...JSON.stringify(b.example, null, 2).split('\n').map((l) => ` ${l}`));
|
|
339
|
+
}
|
|
340
|
+
out.push('');
|
|
341
|
+
}
|
|
342
|
+
if (!type) out.push(' `workover email blocks <type>` shows a block\'s properties and an example; --json gives the schema.', '');
|
|
343
|
+
return out;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
export function renderRendered(res) {
|
|
347
|
+
const out = ['', ` ✓ Compiled (${Math.round((res.html?.length ?? 0) / 1024)} KB of HTML, version ${String(res.hash ?? '').slice(0, 12)}).`];
|
|
348
|
+
for (const w of res.warnings ?? []) out.push(` ! ${w}`);
|
|
349
|
+
out.push(' --out email.html writes the HTML; --json gives the HTML and the text.', '');
|
|
350
|
+
return out;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
export function renderPreview(res) {
|
|
354
|
+
const who = res.contact ? `${res.contact.name || res.contact.email || `contact ${res.contact.id}`}` : res.sample ? 'the sample contact' : 'the data given';
|
|
355
|
+
return ['', ` Subject: ${res.subject}`, ` For: ${who}`, '', ...String(res.text ?? '').split('\n').map((l) => ` ${l}`), ''];
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
const MARK = { pass: '✓', warn: '!', fail: '✗' };
|
|
359
|
+
|
|
360
|
+
/** A QA report: the score, then what failed and what to look at, then how much passed. */
|
|
361
|
+
export function renderChecked(res) {
|
|
362
|
+
const checks = res.checks ?? [];
|
|
363
|
+
const out = ['', ` ${res.ready ? '✓ Ready to send' : '✗ Not ready to send'}. Score ${res.score}/100.`, ''];
|
|
364
|
+
for (const status of ['fail', 'warn'])
|
|
365
|
+
for (const c of checks.filter((x) => x.status === status)) {
|
|
366
|
+
const where = c.pointer ?? c.field;
|
|
367
|
+
out.push(` ${MARK[status]} ${c.id}: ${c.message}${where ? ` (${where})` : ''}`);
|
|
368
|
+
}
|
|
369
|
+
const passed = checks.filter((c) => c.status === 'pass');
|
|
370
|
+
if (passed.length) out.push(` ✓ ${passed.map((c) => c.id).join(', ')}`);
|
|
371
|
+
out.push('');
|
|
372
|
+
return out;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
export function renderTested(res) {
|
|
376
|
+
const who = res.contact ? res.contact.name || res.contact.email || `contact ${res.contact.id}` : res.sample ? 'the sample contact' : 'the data given';
|
|
377
|
+
return [
|
|
378
|
+
'',
|
|
379
|
+
` ✓ ${res.captured ? 'Captured' : 'Sent'} a test to ${res.to}, filled for ${who}.`,
|
|
380
|
+
` From: ${res.from}`,
|
|
381
|
+
` Subject: ${res.subject}`,
|
|
382
|
+
...(res.capturePath ? [` Written to ${res.capturePath}`] : []),
|
|
383
|
+
'',
|
|
384
|
+
];
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const KIND_LABEL = {
|
|
388
|
+
html: 'kept as raw HTML',
|
|
389
|
+
ignored: 'left out',
|
|
390
|
+
approximated: 'approximated',
|
|
391
|
+
default: 'global-style defaults applied',
|
|
392
|
+
info: 'notes',
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
/** An import's outcome: the draft, the counts, then every note but the defaults, by place. */
|
|
396
|
+
export function renderImported(res) {
|
|
397
|
+
const t = res.template;
|
|
398
|
+
const out = [''];
|
|
399
|
+
out.push(
|
|
400
|
+
t
|
|
401
|
+
? ` ✓ Imported as a new draft: #${t.id} "${t.name}".`
|
|
402
|
+
: ` ✓ Dry run: "${res.name}" would be imported. Nothing was saved.`,
|
|
403
|
+
);
|
|
404
|
+
out.push(` Subject: ${res.subject || '(none)'}`);
|
|
405
|
+
if (res.preheader) out.push(` Preheader: ${res.preheader}`);
|
|
406
|
+
const counts = Object.entries(res.counts ?? {})
|
|
407
|
+
.filter(([, n]) => n)
|
|
408
|
+
.map(([k, n]) => `${n} ${KIND_LABEL[k] ?? k}`);
|
|
409
|
+
out.push(` Report: ${counts.length ? counts.join(', ') : 'everything mapped one to one'}.`, '');
|
|
410
|
+
for (const n of res.report ?? []) {
|
|
411
|
+
if (n.kind === 'default') continue;
|
|
412
|
+
const where = [n.pointer, n.line ? `line ${n.line}` : ''].filter(Boolean).join(', ');
|
|
413
|
+
out.push(` ${n.kind === 'html' || n.kind === 'ignored' ? '!' : '·'} ${n.message}${where ? ` (${where})` : ''}`);
|
|
414
|
+
}
|
|
415
|
+
if ((res.report ?? []).some((n) => n.kind !== 'default')) out.push('');
|
|
416
|
+
if (res.layout)
|
|
417
|
+
out.push(
|
|
418
|
+
res.layout.id === null
|
|
419
|
+
? ` In the layout "${res.layout.name}", which saving would add to the workspace.`
|
|
420
|
+
: ` In the layout #${res.layout.id} "${res.layout.name}"${res.layout.created ? ' (added to the workspace)' : ''}.`,
|
|
421
|
+
'',
|
|
422
|
+
);
|
|
423
|
+
if (res.linked) {
|
|
424
|
+
const added = res.brandKitChanges?.added ?? [];
|
|
425
|
+
out.push(
|
|
426
|
+
` Linked to the brand kit${added.length ? `; ${t ? 'added' : 'would add'} ${added.join(', ')}` : ''}.`,
|
|
427
|
+
);
|
|
428
|
+
for (const d of res.brandKitChanges?.differs ?? []) out.push(` · ${d}`);
|
|
429
|
+
out.push('');
|
|
430
|
+
}
|
|
431
|
+
out.push(' --json gives the blocks, the brand kit and the whole report.', '');
|
|
432
|
+
return out;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
const stateOf = (t) =>
|
|
436
|
+
t.publishedVersion ? `published v${t.publishedVersion}${t.unpublishedChanges ? ', unpublished changes' : ''}` : 'draft';
|
|
437
|
+
|
|
438
|
+
export function renderSavedTemplate(res, verb = 'Saved') {
|
|
439
|
+
const t = res.template;
|
|
440
|
+
return ['', ` ✓ ${verb} #${t.id} "${t.name}": revision ${t.revision}, ${stateOf(t)}.`, ''];
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
export function renderPublished(res) {
|
|
444
|
+
const v = res.version;
|
|
445
|
+
return ['', ` ✓ Published #${res.template.id} "${res.template.name}" as version ${v.version}${v.note ? ` (${v.note})` : ''}. Score ${res.score}/100.`, ''];
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
export function renderVersions(res) {
|
|
449
|
+
const rows = res.versions ?? [];
|
|
450
|
+
const out = ['', ` #${res.template.id} "${res.template.name}": ${stateOf(res.template)}.`, ''];
|
|
451
|
+
if (!rows.length) out.push(' Not published yet. `workover email publish <id>` publishes the draft.');
|
|
452
|
+
for (const v of rows)
|
|
453
|
+
out.push(` v${v.version}${v.published ? ' (published)' : ''} ${new Date(v.publishedAt).toISOString().slice(0, 16).replace('T', ' ')} ${v.subject}${v.note ? ` · ${v.note}` : ''}`);
|
|
454
|
+
out.push('', ' `workover email restore <id> <version>` copies one back into the draft.', '');
|
|
455
|
+
return out;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
export function renderTemplates(res) {
|
|
459
|
+
const rows = res.templates ?? [];
|
|
460
|
+
if (!rows.length) return ['', ' No emails yet. `workover email import --source ...` brings one from Customer.io.', ''];
|
|
461
|
+
const out = [''];
|
|
462
|
+
for (const t of rows) {
|
|
463
|
+
const from = t.source?.kind === 'customerio' ? ` from Customer.io${t.source.templateId ? ` #${t.source.templateId}` : ''}` : '';
|
|
464
|
+
out.push(` #${t.id} ${stateOf(t)} ${t.name}${from}`, ` ${t.subject || '(no subject)'}`);
|
|
465
|
+
}
|
|
466
|
+
out.push('', ' `workover email templates <id>` shows one; --out email.json writes its draft to edit and save.', '');
|
|
467
|
+
return out;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
export function renderTemplate(res) {
|
|
471
|
+
const t = res.template;
|
|
472
|
+
return [
|
|
473
|
+
'',
|
|
474
|
+
` #${t.id} ${stateOf(t)}, revision ${t.revision} ${t.name}`,
|
|
475
|
+
` Subject: ${t.subject || '(none)'}`,
|
|
476
|
+
...(t.preheader ? [` Preheader: ${t.preheader}`] : []),
|
|
477
|
+
` ${(t.blocks?.blocks ?? []).length} top-level blocks; import report: ${(t.importReport ?? []).length} notes.`,
|
|
478
|
+
'',
|
|
479
|
+
];
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
const swatch = (t) => `${t.name} ${t.value}${t.dark ? ` / dark ${t.dark}` : ''} (${t.id})`;
|
|
483
|
+
|
|
484
|
+
export function renderBrandKit(res) {
|
|
485
|
+
const k = res.kit ?? {};
|
|
486
|
+
const out = [''];
|
|
487
|
+
if (!res.version) out.push(' No brand kit yet. Import an email from Customer.io, or save one with --file.');
|
|
488
|
+
else out.push(` Brand kit, version ${res.version}.`);
|
|
489
|
+
for (const g of ['colors', 'spacing', 'fonts', 'radius']) {
|
|
490
|
+
const list = k.tokens?.[g] ?? [];
|
|
491
|
+
if (!list.length) continue;
|
|
492
|
+
out.push('', ` ${g}`);
|
|
493
|
+
for (const t of list) out.push(` ${swatch(t)}`);
|
|
494
|
+
}
|
|
495
|
+
const styles = Object.keys(k.textStyles ?? {});
|
|
496
|
+
if (styles.length) out.push('', ` text styles: ${styles.join(', ')}`);
|
|
497
|
+
for (const b of k.buttons ?? []) out.push(` button: ${b.name}${b.default ? ' (default)' : ''} (${b.id})`);
|
|
498
|
+
out.push('', ' An email uses a token as {"token": "colors.<name>_<id>"}. --out kit.json writes it to edit.', '');
|
|
499
|
+
return out;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
export function renderBrandKitSaved(res) {
|
|
503
|
+
const frozen = res.frozen ?? [];
|
|
504
|
+
const out = [''];
|
|
505
|
+
out.push(res.dryRun ? ' Dry run: nothing was saved.' : ` ✓ Saved the brand kit, version ${res.version}.`);
|
|
506
|
+
if (frozen.length) {
|
|
507
|
+
out.push(
|
|
508
|
+
'',
|
|
509
|
+
` ${res.dryRun ? 'Saving would freeze' : 'Froze'} what was removed into ${frozen.length} ${frozen.length === 1 ? 'email' : 'emails'}, so none changes:`,
|
|
510
|
+
);
|
|
511
|
+
for (const e of frozen)
|
|
512
|
+
out.push(` ${e.kind === 'template' ? `email #${e.id}` : `journey #${e.journeyId}`} ${e.name}: ${e.frozen.join(', ')}`);
|
|
513
|
+
}
|
|
514
|
+
out.push('');
|
|
515
|
+
return out;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// ── MCP tools ─────────────────────────────────────────────────────────────────────────────────────
|
|
519
|
+
|
|
520
|
+
const WORKSPACE = {
|
|
521
|
+
type: 'string',
|
|
522
|
+
description: 'The workspace, by slug or id. Needed only when the account has more than one.',
|
|
523
|
+
};
|
|
524
|
+
const DOC = {
|
|
525
|
+
type: 'object',
|
|
526
|
+
description: `The email: {"version": 1, "base"?, "tokens"?, "blocks": [...]}. ${EMAIL_HELP}`,
|
|
527
|
+
};
|
|
528
|
+
const SCOPE =
|
|
529
|
+
' Needs a token from `workover login --contacts`; without it this returns a permission error naming that command.';
|
|
530
|
+
|
|
531
|
+
export const EMAIL_STUDIO_TOOL_DEFS = [
|
|
532
|
+
{
|
|
533
|
+
name: 'workover_email_blocks',
|
|
534
|
+
description:
|
|
535
|
+
'The blocks an email is designed in: each with its properties, defaults, which take {"light","dark"} or {"phone","desktop"} pairs or a {"token"}, and a working example. `type` shows one block in full; `schema: true` answers the JSON Schema of a whole email. Read this before writing an email in blocks.' +
|
|
536
|
+
SCOPE,
|
|
537
|
+
inputSchema: {
|
|
538
|
+
type: 'object',
|
|
539
|
+
properties: {
|
|
540
|
+
workspace: WORKSPACE,
|
|
541
|
+
type: { type: 'string', description: 'One block, like "row" or "button".' },
|
|
542
|
+
schema: { type: 'boolean', description: 'Answer the JSON Schema instead.' },
|
|
543
|
+
},
|
|
544
|
+
},
|
|
545
|
+
},
|
|
546
|
+
{
|
|
547
|
+
name: 'workover_email_render',
|
|
548
|
+
description:
|
|
549
|
+
'Compile an email designed in blocks to the HTML and plain text stored for sending, Liquid intact. A mistake comes back with a JSON pointer to the block and property; fix it and render again. Saves and sends nothing.' +
|
|
550
|
+
SCOPE,
|
|
551
|
+
inputSchema: { type: 'object', properties: { workspace: WORKSPACE, blocks: DOC }, required: ['blocks'] },
|
|
552
|
+
},
|
|
553
|
+
{
|
|
554
|
+
name: 'workover_email_preview',
|
|
555
|
+
description:
|
|
556
|
+
'An email designed in blocks filled for one person: a contact (id, email or x:<external id>), sample `data`, or the sample contact. Strict: a merge tag nothing fills is refused with its field and JSON pointer. Answers the subject and the plain text; `html: true` adds the HTML. Sends nothing.' +
|
|
557
|
+
SCOPE,
|
|
558
|
+
inputSchema: {
|
|
559
|
+
type: 'object',
|
|
560
|
+
properties: {
|
|
561
|
+
workspace: WORKSPACE,
|
|
562
|
+
blocks: DOC,
|
|
563
|
+
subject: { type: 'string' },
|
|
564
|
+
preheader: { type: 'string' },
|
|
565
|
+
contact: { type: ['string', 'number'], description: 'An id, an email or x:<external id>.' },
|
|
566
|
+
data: {
|
|
567
|
+
type: 'object',
|
|
568
|
+
description: 'Sample values in place of a contact: {"name", "email", "externalId", "attributes": {...}}.',
|
|
569
|
+
},
|
|
570
|
+
html: { type: 'boolean', description: 'Include the HTML.' },
|
|
571
|
+
imagesBlocked: { type: 'boolean', description: 'The HTML as a client that blocks images shows it.' },
|
|
572
|
+
},
|
|
573
|
+
required: ['blocks', 'subject'],
|
|
574
|
+
},
|
|
575
|
+
},
|
|
576
|
+
{
|
|
577
|
+
name: 'workover_email_check',
|
|
578
|
+
description:
|
|
579
|
+
"Is an email ready to send: the QA checks on it filled for one person (as workover_email_preview fills it). Merge tags nothing fills, the subject and preheader, links and images written in text or raw HTML, alt text, the unsubscribe link, the HTML size against Gmail's 102 KB clipping and the plain-text version, each pass, warn or fail with a JSON pointer; a readiness score (100, less 20 per failure and 5 per warning) and `ready`. Run it before a send and fix every failure. Saves and sends nothing." +
|
|
580
|
+
SCOPE,
|
|
581
|
+
inputSchema: {
|
|
582
|
+
type: 'object',
|
|
583
|
+
properties: {
|
|
584
|
+
workspace: WORKSPACE,
|
|
585
|
+
blocks: DOC,
|
|
586
|
+
subject: { type: 'string' },
|
|
587
|
+
preheader: { type: 'string' },
|
|
588
|
+
contact: { type: ['string', 'number'], description: 'An id, an email or x:<external id>.' },
|
|
589
|
+
data: { type: 'object', description: 'Sample values in place of a contact.' },
|
|
590
|
+
},
|
|
591
|
+
required: ['blocks', 'subject'],
|
|
592
|
+
},
|
|
593
|
+
},
|
|
594
|
+
{
|
|
595
|
+
name: 'workover_email_test',
|
|
596
|
+
description:
|
|
597
|
+
'Send a test of an email to the person this token belongs to (never to the contact), filled for a contact, sample `data` or the sample contact, with "[Test]" before the subject, from the workspace\'s default sending address or `fromAddressId`. Owners and admins; refused until sending is live.' +
|
|
598
|
+
SCOPE,
|
|
599
|
+
inputSchema: {
|
|
600
|
+
type: 'object',
|
|
601
|
+
properties: {
|
|
602
|
+
workspace: WORKSPACE,
|
|
603
|
+
blocks: DOC,
|
|
604
|
+
subject: { type: 'string' },
|
|
605
|
+
preheader: { type: 'string' },
|
|
606
|
+
contact: { type: ['string', 'number'], description: 'An id, an email or x:<external id>.' },
|
|
607
|
+
data: { type: 'object', description: 'Sample values in place of a contact.' },
|
|
608
|
+
fromAddressId: { type: 'number', description: 'A sending address of the workspace.' },
|
|
609
|
+
},
|
|
610
|
+
required: ['blocks', 'subject'],
|
|
611
|
+
},
|
|
612
|
+
},
|
|
613
|
+
{
|
|
614
|
+
name: 'workover_email_import',
|
|
615
|
+
description:
|
|
616
|
+
'Bring a Customer.io email across as a NEW draft (never over an email that exists): a Design Studio email as `source` (its <x-base>...</x-base>), with the workspace `globalStyles` and the template `node` as Customer.io answers them; or a rich-text email as `body` with its `layout`. Liquid is kept as written. Answers the blocks, the subject and preheader, and an import report: everything that did not map one to one, by JSON pointer and source line; content that does not map becomes a raw HTML block, never dropped. `dryRun: true` answers the same without saving (any member); saving needs an owner or admin.' +
|
|
617
|
+
SCOPE,
|
|
618
|
+
inputSchema: {
|
|
619
|
+
type: 'object',
|
|
620
|
+
properties: {
|
|
621
|
+
workspace: WORKSPACE,
|
|
622
|
+
source: { type: 'string', description: 'The Design Studio source, <x-base>...</x-base>.' },
|
|
623
|
+
body: { type: 'string', description: "A rich-text email's HTML, in place of source." },
|
|
624
|
+
layout: {
|
|
625
|
+
type: 'string',
|
|
626
|
+
description:
|
|
627
|
+
"The rich-text email's layout, holding {{ content }}: the email links to the workspace layout with the same HTML, or a new one.",
|
|
628
|
+
},
|
|
629
|
+
layoutName: { type: 'string', description: 'The name a new layout gets (Customer.io\'s layout name).' },
|
|
630
|
+
text: { type: 'string', description: "A rich-text email's own plain-text version (Customer.io's body_plain)." },
|
|
631
|
+
globalStyles: { type: 'object', description: "Customer.io's global styles (/ds/global-styles)." },
|
|
632
|
+
node: { type: 'object', description: "The template's Design Studio node: envelope_details, settings, name." },
|
|
633
|
+
name: { type: 'string' },
|
|
634
|
+
customerio: { type: 'object', description: '{"templateId", "nodeId"}: where it came from.' },
|
|
635
|
+
dryRun: { type: 'boolean', description: 'Answer the import without saving it.' },
|
|
636
|
+
link: {
|
|
637
|
+
type: 'boolean',
|
|
638
|
+
description:
|
|
639
|
+
"Link the email to the workspace's brand kit, adding the global styles it lacks (default true). The kit is never overwritten; where it would draw the email differently, the email stands alone.",
|
|
640
|
+
},
|
|
641
|
+
},
|
|
642
|
+
},
|
|
643
|
+
},
|
|
644
|
+
{
|
|
645
|
+
name: 'workover_email_templates',
|
|
646
|
+
description:
|
|
647
|
+
"The workspace's emails kept on their own (imports included), most recently changed first; with `id`, one email's blocks, subject, preheader and import report." +
|
|
648
|
+
SCOPE,
|
|
649
|
+
inputSchema: {
|
|
650
|
+
type: 'object',
|
|
651
|
+
properties: { workspace: WORKSPACE, id: { type: ['string', 'number'], description: 'One email.' } },
|
|
652
|
+
},
|
|
653
|
+
},
|
|
654
|
+
{
|
|
655
|
+
name: 'workover_email_save',
|
|
656
|
+
description:
|
|
657
|
+
"Create an email (no `id`: needs `name`) or save its draft (`id` and the `revision` read from workover_email_templates): any of name, subject, preheader, blocks. The published version is untouched until workover_email_publish. A stale revision is refused; read it again. Owners and admins." +
|
|
658
|
+
SCOPE,
|
|
659
|
+
inputSchema: {
|
|
660
|
+
type: 'object',
|
|
661
|
+
properties: {
|
|
662
|
+
workspace: WORKSPACE,
|
|
663
|
+
id: { type: 'number', description: 'The email to save; leave out to create one.' },
|
|
664
|
+
revision: { type: 'number', description: 'The revision you read.' },
|
|
665
|
+
name: { type: 'string' },
|
|
666
|
+
subject: { type: 'string' },
|
|
667
|
+
preheader: { type: 'string' },
|
|
668
|
+
blocks: DOC,
|
|
669
|
+
},
|
|
670
|
+
},
|
|
671
|
+
},
|
|
672
|
+
{
|
|
673
|
+
name: 'workover_email_publish',
|
|
674
|
+
description:
|
|
675
|
+
'Publish an email\'s draft as its next version, with the `revision` read. Refused with the failing checks (as workover_email_check finds them) unless `ignoreChecks: true`; refused when nothing changed since the published version. Owners and admins.' +
|
|
676
|
+
SCOPE,
|
|
677
|
+
inputSchema: {
|
|
678
|
+
type: 'object',
|
|
679
|
+
properties: {
|
|
680
|
+
workspace: WORKSPACE,
|
|
681
|
+
id: { type: 'number' },
|
|
682
|
+
revision: { type: 'number' },
|
|
683
|
+
note: { type: 'string', description: 'What changed, for the version history.' },
|
|
684
|
+
ignoreChecks: { type: 'boolean' },
|
|
685
|
+
},
|
|
686
|
+
required: ['id', 'revision'],
|
|
687
|
+
},
|
|
688
|
+
},
|
|
689
|
+
{
|
|
690
|
+
name: 'workover_email_versions',
|
|
691
|
+
description:
|
|
692
|
+
"An email's published versions, newest first; with `version`, that version's blocks; with `version` and `restore: true` (and the `revision` read), copy it back into the draft without publishing it (owners and admins)." +
|
|
693
|
+
SCOPE,
|
|
694
|
+
inputSchema: {
|
|
695
|
+
type: 'object',
|
|
696
|
+
properties: {
|
|
697
|
+
workspace: WORKSPACE,
|
|
698
|
+
id: { type: 'number' },
|
|
699
|
+
version: { type: 'number' },
|
|
700
|
+
restore: { type: 'boolean' },
|
|
701
|
+
revision: { type: 'number' },
|
|
702
|
+
},
|
|
703
|
+
required: ['id'],
|
|
704
|
+
},
|
|
705
|
+
},
|
|
706
|
+
{
|
|
707
|
+
name: 'workover_email_layouts',
|
|
708
|
+
description:
|
|
709
|
+
"The workspace's email layouts: HTML with {{ content }} once, where a note's body goes (as Customer.io's layouts). No `id`: the list; `id`: one, with its HTML and what uses it. `action: \"create\"` with name and html; `\"save\"` with id, the `revision` read and name and/or html (the change reaches every email using it); `\"delete\"` with id (refused while in use). A note is {\"version\": 1, \"layout\": {\"id\": <id>}, \"text\"?: \"...\", \"blocks\": [{\"type\": \"html\", \"html\": \"...\"}]}. Changes are owners' and admins'." +
|
|
710
|
+
SCOPE,
|
|
711
|
+
inputSchema: {
|
|
712
|
+
type: 'object',
|
|
713
|
+
properties: {
|
|
714
|
+
workspace: WORKSPACE,
|
|
715
|
+
id: { type: 'number' },
|
|
716
|
+
action: { type: 'string', enum: ['create', 'save', 'delete'] },
|
|
717
|
+
name: { type: 'string' },
|
|
718
|
+
html: { type: 'string' },
|
|
719
|
+
revision: { type: 'number' },
|
|
720
|
+
},
|
|
721
|
+
},
|
|
722
|
+
},
|
|
723
|
+
{
|
|
724
|
+
name: 'workover_email_brand_kit',
|
|
725
|
+
description:
|
|
726
|
+
"The workspace's brand kit for emails: tokens (colors, spacing, fonts, radius, each with a 12-character id), text styles (h1 to h6, paragraph, subtitle) and buttons. An email uses a kit token as {\"token\": \"colors.<name>_<id>\"}; the id decides, the name is a label. Without `kit`, answers the kit and its `version`. With `kit` and the `version` you read, saves the whole kit: an edit reaches every email, and a token or button it removes is frozen into each email that used it (listed); a stale version is refused. `dryRun: true` lists what a save would freeze (any member); saving needs an owner or admin." +
|
|
727
|
+
SCOPE,
|
|
728
|
+
inputSchema: {
|
|
729
|
+
type: 'object',
|
|
730
|
+
properties: {
|
|
731
|
+
workspace: WORKSPACE,
|
|
732
|
+
kit: { type: 'object', description: 'The whole kit: {"version": 1, "tokens", "textStyles", "buttons"}.' },
|
|
733
|
+
version: { type: 'number', description: 'The version you read; 0 when the workspace has no kit.' },
|
|
734
|
+
dryRun: { type: 'boolean' },
|
|
735
|
+
},
|
|
736
|
+
},
|
|
737
|
+
},
|
|
738
|
+
];
|
|
739
|
+
|
|
740
|
+
/** The email tools' names, for the MCP dispatcher. */
|
|
741
|
+
export const EMAIL_STUDIO_TOOLS = new Set(EMAIL_STUDIO_TOOL_DEFS.map((t) => t.name));
|
|
742
|
+
|
|
743
|
+
/** One email tool, run: the lines to answer with. Throws as `call` does. */
|
|
744
|
+
export async function runEmailStudioTool(call, name, args = {}) {
|
|
745
|
+
const workspace = args.workspace === undefined || args.workspace === null ? undefined : String(args.workspace);
|
|
746
|
+
if (name === 'workover_email_blocks') {
|
|
747
|
+
const res = await listBlocks(call, { workspace });
|
|
748
|
+
if (args.schema === true) return [JSON.stringify(res.schema, null, 2)];
|
|
749
|
+
return renderBlocks(res, { type: args.type ? String(args.type) : undefined });
|
|
750
|
+
}
|
|
751
|
+
if (name === 'workover_email_import') {
|
|
752
|
+
const body = {};
|
|
753
|
+
for (const k of ['source', 'body', 'layout', 'layoutName', 'text', 'globalStyles', 'node', 'name', 'customerio'])
|
|
754
|
+
if (args[k] !== undefined && args[k] !== null) body[k] = args[k];
|
|
755
|
+
if (args.dryRun === true) body.dryRun = true;
|
|
756
|
+
if (args.link === false) body.link = false;
|
|
757
|
+
const res = await importEmail(call, { workspace, ...body });
|
|
758
|
+
return [...renderImported(res), JSON.stringify(res.blocks, null, 2)];
|
|
759
|
+
}
|
|
760
|
+
if (name === 'workover_email_layouts') {
|
|
761
|
+
const id = args.id === undefined || args.id === null ? undefined : args.id;
|
|
762
|
+
if (args.action === 'create') return renderLayout(await createLayout(call, { workspace, name: args.name, html: args.html }), 'Created');
|
|
763
|
+
if (args.action === 'save')
|
|
764
|
+
return renderLayout(await saveLayout(call, { workspace, id, revision: args.revision, name: args.name ?? undefined, html: args.html ?? undefined }), 'Saved');
|
|
765
|
+
if (args.action === 'delete') {
|
|
766
|
+
const res = await deleteLayout(call, { workspace, id });
|
|
767
|
+
return ['', ` ✓ Deleted layout #${res.deleted}.`, ''];
|
|
768
|
+
}
|
|
769
|
+
if (id === undefined) return renderLayouts(await listLayouts(call, { workspace }));
|
|
770
|
+
const res = await getLayout(call, { workspace, id });
|
|
771
|
+
return [...renderLayout(res), res.layout.html];
|
|
772
|
+
}
|
|
773
|
+
if (name === 'workover_email_brand_kit') {
|
|
774
|
+
if (args.kit === undefined || args.kit === null) {
|
|
775
|
+
const res = await getBrandKit(call, { workspace });
|
|
776
|
+
return [...renderBrandKit(res), JSON.stringify({ kit: res.kit, version: res.version }, null, 2)];
|
|
777
|
+
}
|
|
778
|
+
if (!Number.isInteger(args.version))
|
|
779
|
+
throw apiError(400, 'Give the `version` you read (0 when the workspace has no kit), so a save never overwrites an edit made since.');
|
|
780
|
+
const res = await saveBrandKit(call, { workspace, kit: args.kit, version: args.version, dryRun: args.dryRun === true });
|
|
781
|
+
return renderBrandKitSaved(res);
|
|
782
|
+
}
|
|
783
|
+
if (name === 'workover_email_save') {
|
|
784
|
+
const body = {};
|
|
785
|
+
for (const k of ['name', 'subject', 'preheader', 'blocks', 'revision']) if (args[k] !== undefined && args[k] !== null) body[k] = args[k];
|
|
786
|
+
if (args.id === undefined || args.id === null) {
|
|
787
|
+
const res = await createTemplate(call, { workspace, ...body });
|
|
788
|
+
return renderSavedTemplate(res, 'Created');
|
|
789
|
+
}
|
|
790
|
+
return renderSavedTemplate(await saveTemplate(call, { workspace, id: args.id, ...body }));
|
|
791
|
+
}
|
|
792
|
+
if (name === 'workover_email_publish')
|
|
793
|
+
return renderPublished(
|
|
794
|
+
await publishTemplate(call, { workspace, id: args.id, revision: args.revision, note: args.note, ignoreChecks: args.ignoreChecks === true }),
|
|
795
|
+
);
|
|
796
|
+
if (name === 'workover_email_versions') {
|
|
797
|
+
if (args.version === undefined || args.version === null) return renderVersions(await listVersions(call, { workspace, id: args.id }));
|
|
798
|
+
if (args.restore === true) {
|
|
799
|
+
const res = await restoreVersion(call, { workspace, id: args.id, version: args.version, revision: args.revision });
|
|
800
|
+
return renderSavedTemplate(res, `Restored version ${res.restored} into`);
|
|
801
|
+
}
|
|
802
|
+
const res = await getVersion(call, { workspace, id: args.id, version: args.version });
|
|
803
|
+
return [JSON.stringify(res.version, null, 2)];
|
|
804
|
+
}
|
|
805
|
+
if (name === 'workover_email_templates') {
|
|
806
|
+
if (args.id !== undefined && args.id !== null && args.id !== '') {
|
|
807
|
+
const res = await getTemplate(call, { workspace, id: args.id });
|
|
808
|
+
return [...renderTemplate(res), JSON.stringify(res.template, null, 2)];
|
|
809
|
+
}
|
|
810
|
+
return renderTemplates(await listTemplates(call, { workspace }));
|
|
811
|
+
}
|
|
812
|
+
// A refusal's message already names the field and the JSON pointer of each mistake.
|
|
813
|
+
const email = emailOf({ blocks: args.blocks });
|
|
814
|
+
if (name === 'workover_email_render') {
|
|
815
|
+
const res = await renderEmail(call, { workspace, email });
|
|
816
|
+
return [...renderRendered(res), res.html];
|
|
817
|
+
}
|
|
818
|
+
const person = { workspace, email, subject: args.subject, preheader: args.preheader, contact: args.contact, data: args.data };
|
|
819
|
+
if (name === 'workover_email_preview') {
|
|
820
|
+
const res = await previewEmail(call, { ...person, imagesBlocked: args.imagesBlocked === true });
|
|
821
|
+
return args.html === true ? [...renderPreview(res), res.html] : renderPreview(res);
|
|
822
|
+
}
|
|
823
|
+
if (name === 'workover_email_check') {
|
|
824
|
+
const res = await checkEmail(call, person);
|
|
825
|
+
return [...renderChecked(res), JSON.stringify({ score: res.score, ready: res.ready, checks: res.checks }, null, 2)];
|
|
826
|
+
}
|
|
827
|
+
if (name === 'workover_email_test') return renderTested(await testEmail(call, { ...person, fromAddressId: args.fromAddressId }));
|
|
828
|
+
throw apiError(400, `Unknown tool: ${name}`);
|
|
829
|
+
}
|