@imenam/mcp-organizer 1.0.21 → 1.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/README.md +17 -12
  2. package/dist/shared/logic/documents.d.ts +2 -4
  3. package/dist/shared/logic/documents.js +6 -11
  4. package/dist/shared/schemas/agent-tools.schema.d.ts +133 -7
  5. package/dist/shared/schemas/agent-tools.schema.js +52 -7
  6. package/dist/shared/schemas/documents-api.schema.d.ts +0 -2
  7. package/dist/shared/schemas/documents-api.schema.js +0 -2
  8. package/dist/src/agent-api/attachments.d.ts +2 -0
  9. package/dist/src/agent-api/attachments.js +96 -0
  10. package/dist/src/agent-api/documents.d.ts +1 -1
  11. package/dist/src/agent-api/documents.js +9 -33
  12. package/dist/src/agent-api/files.d.ts +35 -0
  13. package/dist/src/agent-api/files.js +86 -0
  14. package/dist/src/agent-api/router.d.ts +2 -2
  15. package/dist/src/agent-api/router.js +5 -4
  16. package/dist/src/agent-api/tasks.js +5 -8
  17. package/dist/src/agent-api/types.d.ts +4 -2
  18. package/dist/src/core/agent-files.d.ts +41 -0
  19. package/dist/src/core/agent-files.js +102 -0
  20. package/dist/src/core/agent.d.ts +3 -1
  21. package/dist/src/core/agent.js +14 -3
  22. package/dist/src/core/document-text.d.ts +3 -2
  23. package/dist/src/core/document-text.js +3 -2
  24. package/dist/src/core/documents.js +0 -1
  25. package/dist/src/core/history.d.ts +3 -1
  26. package/dist/src/core/history.js +28 -11
  27. package/dist/src/core/mail-render.d.ts +98 -0
  28. package/dist/src/core/mail-render.js +447 -0
  29. package/dist/src/core/mail.d.ts +6 -1
  30. package/dist/src/core/mail.js +1 -1
  31. package/dist/src/core/notifications.d.ts +2 -0
  32. package/dist/src/core/notifications.js +70 -32
  33. package/dist/src/core/password-reset.js +30 -9
  34. package/dist/src/core/requests.d.ts +9 -4
  35. package/dist/src/core/requests.js +21 -10
  36. package/dist/src/core/snapshots.d.ts +3 -3
  37. package/dist/src/core/snapshots.js +3 -1
  38. package/dist/src/core/spaces.js +12 -5
  39. package/dist/src/gui-api.js +2 -2
  40. package/dist/src/mcp/client.d.ts +7 -4
  41. package/dist/src/mcp/client.js +31 -4
  42. package/dist/src/mcp/start.d.ts +1 -1
  43. package/dist/src/mcp/start.js +6 -6
  44. package/dist/src/mcp/tools.d.ts +11 -0
  45. package/dist/src/mcp/tools.js +55 -4
  46. package/dist/src/mcp/upload.d.ts +22 -0
  47. package/dist/src/mcp/upload.js +79 -0
  48. package/dist/src/server/app.js +38 -4
  49. package/gui/dist/assets/{index-DbzcjyaW.js → index-C1f7hOcM.js} +2 -2
  50. package/gui/dist/assets/{index-Dvf5Yo0e.css → index-CfMiwPSL.css} +1 -1
  51. package/gui/dist/index.html +2 -2
  52. package/package.json +4 -1
@@ -0,0 +1,447 @@
1
+ import { Marked } from "marked";
2
+ /**
3
+ * Rendu des e-mails du site : gabarit HTML (mise en page en tableau, styles en
4
+ * ligne, fond clair, sans image externe) et partie texte équivalente, sans
5
+ * syntaxe markdown. Le markdown des utilisateurs et de l'agent (GFM, retours à
6
+ * la ligne simples conservés) est lu par le lexer de marked, puis écrit ici
7
+ * balise par balise : seules les balises de ce module sortent, tout texte est
8
+ * échappé, le HTML brut du markdown s'affiche comme du texte et seuls les
9
+ * liens http, https et mailto restent cliquables.
10
+ */
11
+ /** Nom du site dans l'en-tête et le pied des e-mails. */
12
+ export const SITE_NAME = "mcp-organizer";
13
+ const COLORS = {
14
+ page: "#f4f5fb",
15
+ card: "#ffffff",
16
+ border: "#e3e5f0",
17
+ text: "#1f2330",
18
+ muted: "#5d6275",
19
+ accent: "#6d7cff",
20
+ link: "#4554d9",
21
+ button: "#5563e8",
22
+ code: "#f3f4f8",
23
+ alert: "#b42318",
24
+ };
25
+ const FONT = "-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif";
26
+ const MONO = "SFMono-Regular,Consolas,'Liberation Mono',Menlo,monospace";
27
+ const ESCAPES = { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" };
28
+ /** Texte échappé pour le HTML, contenu ou valeur d'attribut. */
29
+ export function escapeHtml(text) {
30
+ return text.replace(/[&<>"']/g, (char) => ESCAPES[char]);
31
+ }
32
+ /** URL d'une clé de tâche (`ORG-41`), résolue dans l'espace `spaceId`. */
33
+ export function taskKeyUrl(baseUrl, key, spaceId) {
34
+ return `${baseUrl}/#/cle/${encodeURIComponent(key)}${spaceId ? `/${encodeURIComponent(spaceId)}` : ""}`;
35
+ }
36
+ /** Adresse cliquable d'un lien du markdown : http, https, mailto, ou `#/…` résolu sur la GUI ; null sinon. */
37
+ function safeHref(href, links) {
38
+ const url = href.trim();
39
+ if (url.startsWith("#/"))
40
+ return links ? `${links.baseUrl}/${url}` : null;
41
+ return /^(https?:\/\/|mailto:)/i.test(url) ? url : null;
42
+ }
43
+ const ATTACHMENT = "attachment:";
44
+ /** Extension marked : une clé de tâche des préfixes donnés, hors du texte d'un lien, devient un jeton `taskKey`. */
45
+ function taskKeyExtension(prefixes) {
46
+ const alternatives = [...new Set(prefixes)].join("|");
47
+ const anywhere = new RegExp(`\\b(?:${alternatives})-\\d+\\b`);
48
+ const atStart = new RegExp(`^(?:${alternatives})-\\d+\\b`);
49
+ return {
50
+ name: "taskKey",
51
+ level: "inline",
52
+ start: (src) => src.match(anywhere)?.index,
53
+ tokenizer(src) {
54
+ if (this.lexer.state.inLink)
55
+ return undefined;
56
+ const match = atStart.exec(src);
57
+ return match ? { type: "taskKey", raw: match[0], key: match[0] } : undefined;
58
+ },
59
+ };
60
+ }
61
+ /** Jetons du markdown : GFM, retour à la ligne simple conservé, clés de tâche reconnues. */
62
+ function lex(markdown, links) {
63
+ const prefixes = links?.keyPrefixes?.filter((prefix) => /^[A-Z][A-Z0-9]*$/.test(prefix)) ?? [];
64
+ const marked = new Marked({ gfm: true, breaks: true });
65
+ if (prefixes.length > 0)
66
+ marked.use({ extensions: [taskKeyExtension(prefixes)] });
67
+ return marked.lexer(markdown.replace(/\r\n?/g, "\n"));
68
+ }
69
+ /** Clé de tâche entière d'un code en ligne (`` `ORG-41` ``), ou null. */
70
+ function wholeKey(text, links) {
71
+ const prefixes = links?.keyPrefixes ?? [];
72
+ const match = /^([A-Z][A-Z0-9]*)-\d+$/.exec(text);
73
+ return match && prefixes.includes(match[1]) ? text : null;
74
+ }
75
+ // --- Markdown vers HTML -------------------------------------------------------------
76
+ const S = {
77
+ p: `margin:0 0 12px;`,
78
+ link: `color:${COLORS.link};text-decoration:underline;`,
79
+ key: `color:${COLORS.link};text-decoration:none;font-weight:600;`,
80
+ codeInline: `font-family:${MONO};font-size:13px;background:${COLORS.code};border:1px solid ${COLORS.border};border-radius:4px;padding:1px 4px;`,
81
+ pre: `margin:0 0 12px;padding:12px 14px;background:${COLORS.code};border:1px solid ${COLORS.border};border-radius:6px;font-family:${MONO};font-size:13px;line-height:1.5;color:${COLORS.text};white-space:pre-wrap;word-wrap:break-word;word-break:break-word;`,
82
+ list: `margin:0 0 12px;padding:0 0 0 22px;`,
83
+ nestedList: `margin:4px 0 0;padding:0 0 0 22px;`,
84
+ li: `margin:0 0 4px;`,
85
+ quote: `margin:0 0 12px;padding:2px 0 2px 12px;border-left:3px solid ${COLORS.border};color:${COLORS.muted};`,
86
+ table: `border-collapse:collapse;margin:0 0 12px;font-size:14px;`,
87
+ cell: `border:1px solid ${COLORS.border};padding:6px 10px;text-align:left;vertical-align:top;`,
88
+ hr: `border:0;border-top:1px solid ${COLORS.border};margin:16px 0;`,
89
+ attachment: `color:${COLORS.muted};font-style:italic;`,
90
+ attachmentLink: `color:${COLORS.muted};font-style:italic;text-decoration:underline;`,
91
+ };
92
+ const HEADING_SIZES = [20, 18, 16, 15, 15, 15];
93
+ function htmlInline(tokens, links) {
94
+ return (tokens ?? []).map((token) => htmlInlineToken(token, links)).join("");
95
+ }
96
+ function htmlInlineToken(token, links) {
97
+ switch (token.type) {
98
+ case "text":
99
+ return "tokens" in token && token.tokens ? htmlInline(token.tokens, links) : escapeHtml(token.text);
100
+ case "escape":
101
+ case "html":
102
+ return escapeHtml(token.text);
103
+ case "strong":
104
+ return `<strong>${htmlInline(token.tokens, links)}</strong>`;
105
+ case "em":
106
+ return `<em>${htmlInline(token.tokens, links)}</em>`;
107
+ case "del":
108
+ return `<del>${htmlInline(token.tokens, links)}</del>`;
109
+ case "br":
110
+ return "<br>";
111
+ case "codespan": {
112
+ const key = wholeKey(token.text, links);
113
+ if (key && links)
114
+ return keyLink(key, links);
115
+ return `<code style="${S.codeInline}">${escapeHtml(token.text)}</code>`;
116
+ }
117
+ case "taskKey":
118
+ return links ? keyLink(token.key, links) : escapeHtml(token.key);
119
+ case "link": {
120
+ const label = htmlInline(token.tokens, links);
121
+ const href = safeHref(token.href, links);
122
+ return href ? `<a href="${escapeHtml(href)}" style="${S.link}">${label}</a>` : label;
123
+ }
124
+ case "image":
125
+ return htmlImage(token, links);
126
+ default:
127
+ return "raw" in token ? escapeHtml(token.raw) : "";
128
+ }
129
+ }
130
+ function keyLink(key, links) {
131
+ return `<a href="${escapeHtml(taskKeyUrl(links.baseUrl, key, links.spaceId))}" style="${S.key}">${escapeHtml(key)}</a>`;
132
+ }
133
+ /** Image : jamais chargée ; une pièce jointe devient « Image jointe : nom », une image externe un lien. */
134
+ function htmlImage(token, links) {
135
+ if (token.href.startsWith(ATTACHMENT)) {
136
+ const label = escapeHtml(attachmentLabel(token.text));
137
+ const url = links?.attachmentUrl;
138
+ return url ? `<a href="${escapeHtml(url)}" style="${S.attachmentLink}">${label}</a>` : `<span style="${S.attachment}">${label}</span>`;
139
+ }
140
+ const label = escapeHtml(token.text ? `Image : ${token.text}` : "Image");
141
+ const href = safeHref(token.href, links);
142
+ return href ? `<a href="${escapeHtml(href)}" style="${S.link}">${label}</a>` : label;
143
+ }
144
+ function attachmentLabel(name) {
145
+ return name ? `Image jointe : ${name}` : "Image jointe";
146
+ }
147
+ function htmlBlocks(tokens, links) {
148
+ return tokens.map((token) => htmlBlock(token, links)).join("");
149
+ }
150
+ function htmlBlock(token, links) {
151
+ switch (token.type) {
152
+ case "space":
153
+ case "def":
154
+ return "";
155
+ case "paragraph":
156
+ return `<p style="${S.p}">${htmlInline(token.tokens, links)}</p>`;
157
+ case "text":
158
+ return `<p style="${S.p}">${htmlInlineToken(token, links)}</p>`;
159
+ case "heading": {
160
+ const size = HEADING_SIZES[Math.min(token.depth, 6) - 1];
161
+ return `<p style="margin:16px 0 8px;font-size:${size}px;font-weight:700;line-height:1.3;">${htmlInline(token.tokens, links)}</p>`;
162
+ }
163
+ case "code":
164
+ return `<pre style="${S.pre}">${escapeHtml(token.text)}</pre>`;
165
+ case "blockquote":
166
+ return `<div style="${S.quote}">${htmlBlocks(token.tokens ?? [], links)}</div>`;
167
+ case "hr":
168
+ return `<hr style="${S.hr}">`;
169
+ case "list":
170
+ return htmlList(token, links);
171
+ case "table":
172
+ return htmlTable(token, links);
173
+ case "html":
174
+ return `<p style="${S.p}">${escapeHtml(token.text)}</p>`;
175
+ default:
176
+ return "raw" in token ? `<p style="${S.p}">${escapeHtml(token.raw)}</p>` : "";
177
+ }
178
+ }
179
+ /** Liste ; une liste imbriquée dans un élément suit son texte sans marge basse. */
180
+ function htmlList(list, links, nested = false) {
181
+ const tag = list.ordered ? "ol" : "ul";
182
+ const start = list.ordered && typeof list.start === "number" && list.start !== 1 ? ` start="${list.start}"` : "";
183
+ const items = list.items.map((item) => `<li style="${S.li}">${htmlListItem(item, links)}</li>`).join("");
184
+ return `<${tag}${start} style="${nested ? S.nestedList : S.list}">${items}</${tag}>`;
185
+ }
186
+ /** Contenu d'un élément de liste ; ses paragraphes perdent leur marge, sauf dans une liste aérée. */
187
+ function htmlListItem(item, links) {
188
+ const box = item.task ? `${item.checked ? "&#9745;" : "&#9744;"} ` : "";
189
+ const parts = item.tokens
190
+ .filter((token) => token.type !== "checkbox")
191
+ .map((token) => {
192
+ if (token.type === "text")
193
+ return htmlInlineToken(token, links);
194
+ if (token.type === "paragraph" && !item.loose)
195
+ return htmlInline(token.tokens, links);
196
+ if (token.type === "list")
197
+ return htmlList(token, links, true);
198
+ return htmlBlock(token, links);
199
+ });
200
+ return box + parts.join("");
201
+ }
202
+ function htmlTable(table, links) {
203
+ const align = (value) => (value ? `text-align:${value};` : "");
204
+ const head = table.header.map((cell) => `<th style="${S.cell}${align(cell.align)}background:${COLORS.code};">${htmlInline(cell.tokens, links)}</th>`).join("");
205
+ const rows = table.rows.map((row) => `<tr>${row.map((cell) => `<td style="${S.cell}${align(cell.align)}">${htmlInline(cell.tokens, links)}</td>`).join("")}</tr>`).join("");
206
+ return `<table role="presentation" cellpadding="0" cellspacing="0" style="${S.table}"><tr>${head}</tr>${rows}</table>`;
207
+ }
208
+ /** HTML assaini d'un texte markdown, avec les styles en ligne du gabarit. */
209
+ export function markdownToHtml(markdown, links) {
210
+ return htmlBlocks(lex(markdown, links), links);
211
+ }
212
+ /** HTML d'un élément de liste : un seul paragraphe est rendu sans balise de paragraphe. */
213
+ function markdownItemHtml(markdown, links) {
214
+ const tokens = lex(markdown, links).filter((token) => token.type !== "space");
215
+ return tokens.length === 1 && tokens[0].type === "paragraph" ? htmlInline(tokens[0].tokens, links) : htmlBlocks(tokens, links);
216
+ }
217
+ // --- Markdown vers texte --------------------------------------------------------------
218
+ function textInline(tokens, links) {
219
+ return (tokens ?? []).map((token) => textInlineToken(token, links)).join("");
220
+ }
221
+ function textInlineToken(token, links) {
222
+ switch (token.type) {
223
+ case "text":
224
+ return "tokens" in token && token.tokens ? textInline(token.tokens, links) : token.text;
225
+ case "escape":
226
+ case "html":
227
+ case "codespan":
228
+ return token.text;
229
+ case "strong":
230
+ case "em":
231
+ case "del":
232
+ return textInline(token.tokens, links);
233
+ case "br":
234
+ return "\n";
235
+ case "taskKey":
236
+ return token.key;
237
+ case "link": {
238
+ const label = textInline(token.tokens, links);
239
+ const href = safeHref(token.href, links);
240
+ if (!href || [label, `mailto:${label}`, `http://${label}`, `https://${label}`].includes(href))
241
+ return label;
242
+ return `${label} (${href})`;
243
+ }
244
+ case "image": {
245
+ const image = token;
246
+ if (image.href.startsWith(ATTACHMENT))
247
+ return `(${attachmentLabel(image.text).toLowerCase()})`;
248
+ const href = safeHref(image.href, links);
249
+ const label = image.text ? `Image : ${image.text}` : "Image";
250
+ return href ? `${label} (${href})` : label;
251
+ }
252
+ default:
253
+ return "raw" in token ? token.raw : "";
254
+ }
255
+ }
256
+ const indent = (text, prefix) => text
257
+ .split("\n")
258
+ .map((line) => (line ? prefix + line : line))
259
+ .join("\n");
260
+ function textBlocks(tokens, links, separator = "\n\n") {
261
+ return tokens
262
+ .map((token) => textBlock(token, links))
263
+ .filter((part) => part !== "")
264
+ .join(separator);
265
+ }
266
+ function textBlock(token, links) {
267
+ switch (token.type) {
268
+ case "space":
269
+ case "def":
270
+ return "";
271
+ case "paragraph":
272
+ case "heading":
273
+ return textInline(token.tokens, links);
274
+ case "text":
275
+ return textInlineToken(token, links);
276
+ case "code":
277
+ return indent(token.text, " ");
278
+ case "blockquote":
279
+ return textBlocks(token.tokens ?? [], links)
280
+ .split("\n")
281
+ .map((line) => (line ? `> ${line}` : ">"))
282
+ .join("\n");
283
+ case "hr":
284
+ return "——————";
285
+ case "list":
286
+ return textList(token, links);
287
+ case "table": {
288
+ const table = token;
289
+ const row = (cells) => cells.map((cell) => textInline(cell.tokens, links)).join(" | ");
290
+ return [row(table.header), ...table.rows.map(row)].join("\n");
291
+ }
292
+ default:
293
+ return "raw" in token ? token.raw.trim() : "";
294
+ }
295
+ }
296
+ function textList(list, links) {
297
+ const first = typeof list.start === "number" ? list.start : 1;
298
+ return list.items
299
+ .map((item, index) => {
300
+ const bullet = list.ordered ? `${first + index}. ` : "- ";
301
+ const box = item.task ? (item.checked ? "[x] " : "[ ] ") : "";
302
+ const body = textBlocks(item.tokens.filter((token) => token.type !== "checkbox"), links, item.loose ? "\n\n" : "\n");
303
+ const [head, ...rest] = (box + body).split("\n");
304
+ return [bullet + head, ...rest.map((line) => (line ? " ".repeat(bullet.length) + line : line))].join("\n");
305
+ })
306
+ .join(list.loose ? "\n\n" : "\n");
307
+ }
308
+ /** Texte brut lisible d'un texte markdown : sans marques de mise en forme, liens écrits « libellé (adresse) ». */
309
+ export function markdownToText(markdown, links) {
310
+ return textBlocks(lex(markdown, links), links).trim();
311
+ }
312
+ function htmlInlines(content) {
313
+ return content
314
+ .map((part) => {
315
+ if (typeof part === "string")
316
+ return escapeHtml(part);
317
+ if ("strong" in part)
318
+ return `<strong>${escapeHtml(part.strong)}</strong>`;
319
+ return `<a href="${escapeHtml(part.url)}" style="${S.link}">${escapeHtml(part.text)}</a>`;
320
+ })
321
+ .join("");
322
+ }
323
+ function textInlines(content) {
324
+ return content
325
+ .map((part) => {
326
+ if (typeof part === "string")
327
+ return part;
328
+ if ("strong" in part)
329
+ return part.strong;
330
+ return part.plain || part.text === part.url ? part.text : `${part.text} (${part.url})`;
331
+ })
332
+ .join("");
333
+ }
334
+ const merge = (base, extra) => base ? { ...base, ...extra } : extra?.baseUrl ? extra : undefined;
335
+ function htmlEmailBlock(block, links) {
336
+ switch (block.type) {
337
+ case "paragraph": {
338
+ const color = block.tone === "muted" ? COLORS.muted : block.tone === "alert" ? COLORS.alert : COLORS.text;
339
+ return `<p style="margin:0 0 14px;color:${color};">${htmlInlines(block.content)}</p>`;
340
+ }
341
+ case "quote":
342
+ return `<div style="margin:0 0 16px;padding:4px 0 0 14px;border-left:3px solid ${COLORS.accent};">${markdownToHtml(block.markdown, merge(links, block.links))}</div>`;
343
+ case "section": {
344
+ const sectionLinks = merge(links, block.links);
345
+ // Des éléments sans libellé forment une liste à puces ; sinon chacun a son libellé et sa citation.
346
+ const bullets = block.items.every((item) => !item.label && item.markdown);
347
+ const bulletList = () => {
348
+ const entries = block.items.map((item) => `<li style="${S.li}">${markdownItemHtml(item.markdown, sectionLinks)}</li>`).join("");
349
+ return `<tr><td style="padding:0 0 6px;"><ul style="margin:0;padding:0 0 0 22px;">${entries}</ul></td></tr>`;
350
+ };
351
+ const labelledRows = () => block.items
352
+ .map((item) => {
353
+ const label = item.label ? `<p style="margin:0 0 6px;color:${COLORS.muted};font-size:14px;">${escapeHtml(item.label)}</p>` : "";
354
+ const body = item.markdown
355
+ ? `<div style="padding:2px 0 0 12px;border-left:3px solid ${COLORS.accent};">${markdownToHtml(item.markdown, sectionLinks)}</div>`
356
+ : "";
357
+ return `<tr><td style="padding:0 0 6px;">${label}${body}</td></tr>`;
358
+ })
359
+ .join("");
360
+ const items = bullets ? bulletList() : labelledRows();
361
+ const link = block.link
362
+ ? `<tr><td style="padding:2px 0 0;"><a href="${escapeHtml(block.link.url)}" style="${S.link}font-size:14px;font-weight:600;">${escapeHtml(block.link.label)}&nbsp;&rarr;</a></td></tr>`
363
+ : "";
364
+ return (`<table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="margin:0 0 16px;border:1px solid ${COLORS.border};border-radius:6px;">` +
365
+ `<tr><td style="padding:14px 16px;">` +
366
+ `<p style="margin:0 0 10px;font-size:16px;font-weight:600;color:${COLORS.text};">${htmlInlines(block.title)}</p>` +
367
+ `<table role="presentation" width="100%" cellpadding="0" cellspacing="0">${items}${link}</table>` +
368
+ `</td></tr></table>`);
369
+ }
370
+ }
371
+ }
372
+ function textEmailBlock(block, links) {
373
+ switch (block.type) {
374
+ case "paragraph":
375
+ return textInlines(block.content);
376
+ case "quote":
377
+ return markdownToText(block.markdown, merge(links, block.links));
378
+ case "section": {
379
+ const sectionLinks = merge(links, block.links);
380
+ const lines = [textInlines(block.title)];
381
+ for (const item of block.items) {
382
+ const body = item.markdown ? markdownToText(item.markdown, sectionLinks) : "";
383
+ if (item.label && body)
384
+ lines.push(`- ${item.label} :\n${indent(body, " ")}`);
385
+ else if (item.label)
386
+ lines.push(`- ${item.label}`);
387
+ else if (body)
388
+ lines.push(`- ${indent(body, " ").slice(2)}`);
389
+ }
390
+ if (block.link)
391
+ lines.push(`${block.link.label} : ${block.link.url}`);
392
+ return lines.join("\n");
393
+ }
394
+ }
395
+ }
396
+ /** Bouton de l'action principale, rendu aussi par Outlook (cellule colorée autour du lien). */
397
+ function htmlButton(action) {
398
+ return (`<table role="presentation" cellpadding="0" cellspacing="0" style="margin:8px 0 4px;"><tr>` +
399
+ `<td bgcolor="${COLORS.button}" style="background:${COLORS.button};border-radius:6px;">` +
400
+ `<a href="${escapeHtml(action.url)}" style="display:inline-block;padding:12px 22px;font-family:${FONT};font-size:15px;font-weight:600;color:#ffffff;text-decoration:none;border-radius:6px;">${escapeHtml(action.label)}</a>` +
401
+ `</td></tr></table>`);
402
+ }
403
+ /** Rend un e-mail : partie HTML dans le gabarit du site, et partie texte équivalente. */
404
+ export function renderEmail(content) {
405
+ const { links } = content;
406
+ const header = `<span style="font-size:13px;font-weight:700;letter-spacing:0.04em;color:${COLORS.accent};">${escapeHtml(SITE_NAME)}</span>` +
407
+ (content.space ? `<span style="font-size:13px;color:${COLORS.muted};">&nbsp;&middot;&nbsp;${escapeHtml(content.space)}</span>` : "");
408
+ const body = content.blocks.map((block) => htmlEmailBlock(block, links)).join("");
409
+ const footer = [
410
+ content.footnote ? escapeHtml(content.footnote) : "",
411
+ content.preferencesUrl
412
+ ? `<a href="${escapeHtml(content.preferencesUrl)}" style="color:${COLORS.muted};text-decoration:underline;">Gérer mes préférences de notification</a>`
413
+ : "",
414
+ ]
415
+ .filter(Boolean)
416
+ .join("<br>");
417
+ const html = `<!DOCTYPE html><html lang="fr"><head><meta charset="utf-8">` +
418
+ `<meta name="viewport" content="width=device-width,initial-scale=1">` +
419
+ `<meta name="color-scheme" content="light"><meta name="supported-color-schemes" content="light">` +
420
+ `<title>${escapeHtml(content.subject)}</title></head>` +
421
+ `<body style="margin:0;padding:0;background:${COLORS.page};">` +
422
+ `<table role="presentation" width="100%" cellpadding="0" cellspacing="0" bgcolor="${COLORS.page}" style="background:${COLORS.page};">` +
423
+ `<tr><td align="center" style="padding:24px 12px;">` +
424
+ `<table role="presentation" width="600" cellpadding="0" cellspacing="0" bgcolor="${COLORS.card}" style="width:100%;max-width:600px;background:${COLORS.card};border:1px solid ${COLORS.border};border-top:4px solid ${COLORS.accent};border-radius:8px;">` +
425
+ `<tr><td style="padding:18px 28px 0;font-family:${FONT};">${header}</td></tr>` +
426
+ `<tr><td style="padding:16px 28px 20px;font-family:${FONT};font-size:15px;line-height:1.55;color:${COLORS.text};word-wrap:break-word;">` +
427
+ body +
428
+ (content.action ? htmlButton(content.action) : "") +
429
+ (content.action && content.showActionUrl
430
+ ? `<p style="margin:12px 0 0;font-size:13px;color:${COLORS.muted};">Si le bouton ne s'ouvre pas, copiez ce lien dans votre navigateur :<br><a href="${escapeHtml(content.action.url)}" style="${S.link}word-break:break-all;">${escapeHtml(content.action.url)}</a></p>`
431
+ : "") +
432
+ `</td></tr></table>` +
433
+ (footer
434
+ ? `<table role="presentation" width="600" cellpadding="0" cellspacing="0" style="width:100%;max-width:600px;">` +
435
+ `<tr><td style="padding:16px 28px;font-family:${FONT};font-size:12px;line-height:1.5;color:${COLORS.muted};text-align:center;">${footer}</td></tr></table>`
436
+ : "") +
437
+ `</td></tr></table></body></html>`;
438
+ const footerLines = [content.footnote ?? "", content.preferencesUrl ? `Préférences de notification : ${content.preferencesUrl}` : ""].filter(Boolean);
439
+ const text = [
440
+ ...content.blocks.map((block) => textEmailBlock(block, links)),
441
+ content.action ? `${content.action.label} : ${content.action.url}` : "",
442
+ footerLines.length > 0 ? ["—", ...footerLines].join("\n") : "",
443
+ ]
444
+ .filter((part) => part !== "")
445
+ .join("\n\n");
446
+ return { subject: content.subject, html, text: `${text}\n` };
447
+ }
@@ -1,8 +1,12 @@
1
- /** Message envoyé par e-mail, en texte brut. */
1
+ /**
2
+ * Message envoyé par e-mail, en `multipart/alternative` : une partie texte
3
+ * brut et une partie HTML, produites par `renderEmail` (`mail-render.ts`).
4
+ */
2
5
  export interface MailMessage {
3
6
  to: string;
4
7
  subject: string;
5
8
  text: string;
9
+ html: string;
6
10
  }
7
11
  /** Issue d'un envoi : `reason` dit pourquoi l'e-mail n'est pas parti. */
8
12
  export type MailOutcome = {
@@ -18,6 +22,7 @@ export interface MailTransport {
18
22
  to: string;
19
23
  subject: string;
20
24
  text: string;
25
+ html: string;
21
26
  }): Promise<unknown>;
22
27
  }
23
28
  /**
@@ -29,7 +29,7 @@ export function createMailer(config) {
29
29
  disabledReason: null,
30
30
  send: async (message) => {
31
31
  try {
32
- await transport.sendMail({ from, to: message.to, subject: message.subject, text: message.text });
32
+ await transport.sendMail({ from, to: message.to, subject: message.subject, text: message.text, html: message.html });
33
33
  return { sent: true };
34
34
  }
35
35
  catch (err) {
@@ -70,6 +70,8 @@ export declare class NotificationMailer {
70
70
  * les points à traiter laissés à l'auteur de la demande.
71
71
  */
72
72
  private digest;
73
+ /** E-mail de notification rendu dans le gabarit, avec les liens de l'espace et celui des préférences. */
74
+ private mail;
73
75
  /** Textes des points encore ouverts d'une demande. */
74
76
  private openPoints;
75
77
  private taskUrl;
@@ -1,4 +1,5 @@
1
1
  import { NotFoundError } from "./errors.js";
2
+ import { markdownToText, renderEmail } from "./mail-render.js";
2
3
  import { requireMember } from "./spaces.js";
3
4
  /**
4
5
  * Notifications (assignation, commentaire, demande traitée avec des points à
@@ -15,6 +16,8 @@ const AGENT_NAME = "L'agent";
15
16
  export const AGENT_EMAIL_MAX_DELAY_MS = 15 * 60_000;
16
17
  /** Pied des e-mails de notification. */
17
18
  const UNSUBSCRIBE = "Ces e-mails se désactivent dans les Paramètres de mcp-organizer.";
19
+ /** Titre de la liste des points laissés à l'auteur d'une demande. */
20
+ const POINTS_TITLE = "Il reste à traiter de ton côté";
18
21
  function publicBase(publicUrl) {
19
22
  return publicUrl?.trim().replace(/\/+$/, "") || null;
20
23
  }
@@ -27,6 +30,10 @@ export function notificationEmailBlocker(deps) {
27
30
  }
28
31
  return null;
29
32
  }
33
+ /** Extrait du texte markdown d'une demande, sans marques de mise en forme. */
34
+ function requestExcerpt(text) {
35
+ return excerpt(markdownToText(text));
36
+ }
30
37
  /** Première ligne d'un texte, abrégée. */
31
38
  function excerpt(text) {
32
39
  const line = text.trim().split(/\r?\n/)[0].trim();
@@ -201,32 +208,39 @@ export class NotificationMailer {
201
208
  const request = this.store.getAgentRequest(notification.requestId ?? "");
202
209
  const space = this.store.getSpace(request.targetSpaceId);
203
210
  const points = this.openPoints(request.id);
204
- return {
205
- to,
211
+ const requestUrl = this.requestUrl(request.id);
212
+ return this.mail(to, space, {
206
213
  subject: `${AGENT_NAME} a traité ta demande : ${pointsLabel(points.length)} à traiter de ton côté`,
207
- text: `${AGENT_NAME} a traité ta demande « ${excerpt(request.text)} » dans l'espace « ${space.name} ». Il reste à traiter de ton côté :\n\n${points
208
- .map((point) => `- ${point}`)
209
- .join("\n")}\n\nOuvrir la demande : ${this.requestUrl(request.id)}\n\n${UNSUBSCRIBE}\n`,
210
- };
214
+ blocks: [
215
+ { type: "paragraph", content: [`${AGENT_NAME} a traité ta demande « ${requestExcerpt(request.text)} » dans l'espace « ${space.name} ».`] },
216
+ { type: "section", title: [POINTS_TITLE], items: points.map((point) => ({ markdown: point })), links: { attachmentUrl: requestUrl } },
217
+ ],
218
+ action: { label: "Voir la demande", url: requestUrl },
219
+ });
211
220
  }
212
221
  const task = this.store.getTask(notification.taskId);
213
222
  const space = this.store.getSpace(task.spaceId);
214
223
  const key = `${space.keyPrefix}-${task.number}`;
215
224
  const who = actorName(this.store, notification) ?? AGENT_NAME;
216
- const footer = `\n\nOuvrir la tâche : ${this.taskUrl(task.id)}\n\n${UNSUBSCRIBE}\n`;
225
+ const taskUrl = this.taskUrl(task.id);
226
+ const action = { label: "Ouvrir la tâche", url: taskUrl };
227
+ const named = [{ text: key, url: taskUrl, plain: true }, " « ", { strong: task.title }, ` » dans l'espace « ${space.name} »`];
217
228
  if (notification.type === "assigned") {
218
- return {
219
- to,
229
+ return this.mail(to, space, {
220
230
  subject: `${who} vous a assigné « ${task.title} »`,
221
- text: `${who} vous a assigné la tâche ${key} « ${task.title} » dans l'espace « ${space.name} ».${footer}`,
222
- };
231
+ blocks: [{ type: "paragraph", content: [`${who} vous a assigné la tâche `, ...named, "."] }],
232
+ action,
233
+ });
223
234
  }
224
235
  const comment = notification.commentId === null ? null : this.store.findComment(notification.commentId);
225
- return {
226
- to,
236
+ return this.mail(to, space, {
227
237
  subject: `${who} a commenté « ${task.title} »`,
228
- text: `${who} a commenté la tâche ${key} « ${task.title} » dans l'espace « ${space.name} » :\n\n${comment?.body ?? ""}${footer}`,
229
- };
238
+ blocks: [
239
+ { type: "paragraph", content: [`${who} a commenté la tâche `, ...named, " :"] },
240
+ { type: "quote", markdown: comment?.body ?? "", links: { attachmentUrl: taskUrl } },
241
+ ],
242
+ action,
243
+ });
230
244
  }
231
245
  /**
232
246
  * E-mail récapitulatif des notifications d'un lot de l'agent : la demande
@@ -253,33 +267,57 @@ export class NotificationMailer {
253
267
  ? `${AGENT_NAME} a agi dans l'espace « ${space.name} ».`
254
268
  : request.kind === "direct"
255
269
  ? `${AGENT_NAME} a agi dans l'espace « ${space.name} » à la demande directe de ${userName(this.store, request.authorId)}.`
256
- : `${AGENT_NAME} a agi dans l'espace « ${space.name} » pour la demande « ${excerpt(request.text)} » de ${userName(this.store, request.authorId)}.`;
257
- const status = request?.status === "processing"
258
- ? `\n${AGENT_NAME} n'a pas encore terminé cette demande.`
259
- : request?.status === "failed"
260
- ? `\nLa demande a échoué${request.failureReason ? ` : ${request.failureReason}` : "."}`
261
- : "";
262
- const sections = [];
270
+ : `${AGENT_NAME} a agi dans l'espace « ${space.name} » pour la demande « ${requestExcerpt(request.text)} » de ${userName(this.store, request.authorId)}.`;
271
+ const blocks = [{ type: "paragraph", content: [intro] }];
272
+ if (request?.status === "processing")
273
+ blocks.push({ type: "paragraph", content: [`${AGENT_NAME} n'a pas encore terminé cette demande.`], tone: "muted" });
274
+ if (request?.status === "failed") {
275
+ blocks.push({ type: "paragraph", content: [`La demande a échoué${request.failureReason ? ` : ${request.failureReason}` : "."}`], tone: "alert" });
276
+ }
263
277
  for (const [taskId, list] of byTask) {
264
278
  const task = this.store.getTask(taskId);
265
- const lines = list.map((notification) => {
279
+ const taskUrl = this.taskUrl(task.id);
280
+ const items = list.map((notification) => {
266
281
  if (notification.type === "assigned")
267
- return "- vous a assigné cette tâche";
282
+ return { label: "vous a assigné cette tâche" };
268
283
  const comment = notification.commentId === null ? null : this.store.findComment(notification.commentId);
269
- const body = (comment?.body ?? "").trim().replace(/\r?\n/g, "\n ");
270
- return body ? `- a commenté :\n ${body}` : "- a commenté";
284
+ const body = (comment?.body ?? "").trim();
285
+ return body ? { label: "a commenté", markdown: body } : { label: "a commenté" };
286
+ });
287
+ blocks.push({
288
+ type: "section",
289
+ title: [{ strong: `${space.keyPrefix}-${task.number} « ${task.title} »` }],
290
+ items,
291
+ link: { label: "Ouvrir la tâche", url: taskUrl },
292
+ links: { attachmentUrl: taskUrl },
271
293
  });
272
- sections.push(`${space.keyPrefix}-${task.number} « ${task.title} »\n${lines.join("\n")}\nOuvrir la tâche : ${this.taskUrl(task.id)}`);
273
294
  }
274
295
  if (followUps) {
275
296
  const points = this.openPoints(followUps.requestId);
276
- sections.push(`Il reste à traiter de ton côté :\n${points.map((point) => `- ${point}`).join("\n")}\nOuvrir la demande : ${this.requestUrl(followUps.requestId)}`);
297
+ blocks.push({
298
+ type: "section",
299
+ title: [POINTS_TITLE],
300
+ items: points.map((point) => ({ markdown: point })),
301
+ links: { attachmentUrl: this.requestUrl(followUps.requestId) },
302
+ });
277
303
  }
278
- return {
279
- to,
304
+ return this.mail(to, space, {
280
305
  subject: `${AGENT_NAME} : ${notificationsLabel(notifications.length)} dans l'espace « ${space.name} »`,
281
- text: `${intro}${status}\n\n${sections.join("\n\n")}\n\n${UNSUBSCRIBE}\n`,
282
- };
306
+ blocks,
307
+ action: request ? { label: "Voir la demande", url: this.requestUrl(request.id) } : undefined,
308
+ });
309
+ }
310
+ /** E-mail de notification rendu dans le gabarit, avec les liens de l'espace et celui des préférences. */
311
+ mail(to, space, content) {
312
+ const baseUrl = publicBase(this.deps.publicUrl);
313
+ const rendered = renderEmail({
314
+ ...content,
315
+ space: space.name,
316
+ footnote: UNSUBSCRIBE,
317
+ preferencesUrl: `${baseUrl}/#/parametres`,
318
+ links: { baseUrl, keyPrefixes: [space.keyPrefix], spaceId: space.id },
319
+ });
320
+ return { to, ...rendered };
283
321
  }
284
322
  /** Textes des points encore ouverts d'une demande. */
285
323
  openPoints(requestId) {