domma-cms 1.70.0 → 1.71.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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.71.0] - 2026-11-16
4
+
5
+ ### Added
6
+
7
+ - **Feedback 1.4.0 and Support 1.3.0: AI Assist on the receiving site.** With AI Assist (Pro plan), staff can see themes across feedback reports, a summary of a support ticket and a suggested reply, each run only when clicked.
8
+
3
9
  ## [1.70.0] - 2026-11-16
4
10
 
5
11
  ### Added
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "domma-cms",
3
- "version": "1.70.0",
3
+ "version": "1.71.0",
4
4
  "description": "File-based CMS powered by Domma and Fastify. Run npx domma-cms my-site to create a new project.",
5
5
  "type": "module",
6
6
  "main": "server/server.js",
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## [1.4.0] - 2026-11-16
4
+
5
+ - **Summarise** in the Domma server's inbox (with AI Assist): groups the reports in view, following the status, type, site and search filters, into themes with AI, with counts and a few quotes. Only titles and descriptions are sent, never site names, people's names, email addresses or files. The latest summary is kept on the screen. Sites see no change.
@@ -214,3 +214,29 @@ at the next prune (older than a day, no longer named). Found on the dcm click-th
214
214
  - **Waypoint:** `issueDescription(d, {rich: wp.richDescriptions === true})` - rich text plus the facts as an escaped
215
215
  paragraph for a Waypoint that says it takes rich text, otherwise the words and the `---` block as before.
216
216
  - Do not edit the copy of this Tool in domma-cms: it is synced from here (`make sync-free-tier`).
217
+
218
+ ## Unreleased: Summarise (AI Assist, the receiver only)
219
+
220
+ - **Where.** The inbox's **Summarise** card (`admin/views/inbox.js`, model in `admin/lib/ai.js`, PURE and tested). Shown only
221
+ when `GET /mode` answers `ai: true`: `aiTextAvailable('themes')` in `lib/ai-text.js` = AI Assist on (`isAiAssist()`,
222
+ test seam `useAiAssistLookup`) AND the Manager's own service in this process, `globalThis[Symbol.for('domma.ai-text')]`
223
+ (site-manager/services/aiTextRoutes.js). No fleet route: the receiver IS the Manager; a site never offers it.
224
+ - **Route.** `POST /themes` (`manage`) `{status, type, site, q}` - the inbox's filter as it stands. The server picks the
225
+ reports itself (`lib/themes.js reportsInView`, the inbox's rules, newest first, at most 1000) and sends task `themes`,
226
+ kind `feedback`, survey "Feedback from Domma sites", question `filterText()` (never a site's name or the search's words),
227
+ answers "Title. Description" as plain text through `scrubText()` (emails, phone numbers, quoted mail, signatures,
228
+ the sender's name and the site's name out).
229
+ Never the site, the sender, their email, Where, the CMS version or files. Status and error text pass through (403, 429,
230
+ 503; a decline is 422). The latest result is kept in `data/ai.json` (`createKept`, 0600) and comes back on `GET /reports`
231
+ as `themes`.
232
+ - **UI.** Confirm once per visit (per mount) before anything goes, "Summarising..." while it runs, a toast with
233
+ "n of 500 AI Assist requests used this month". Tests: `tests/ai.test.js` (the registry is a stub; nothing reaches Claude).
234
+ - **`scrubText()`** (lib/ai-text.js): a greeting at the start ("Hi Ann,") and a sign-off with up to three short lines
235
+ under it ("Thanks,\nAnn Jones\nAcme Ltd") go (`letter: false` for titles/subjects); known names match in any case as
236
+ whole words, with parts learnt from the email's local part (john.smith@ -> john, smith); stand-ins ("A site admin",
237
+ "An admin", "Domma Support") and mailbox words (info, admin, support...) are never names. A common word that is also a
238
+ known name ("will" for Will) goes too - the price of matching any case. Sites: names and domains, and links on them.
239
+ - **Site domains** come from `rx.siteHosts()` (sites.json primary, fallback and other domains; `options.siteHosts` in tests).
240
+ - **Retention:** the kept result records which report ids it read (`ids`, never sent to the browser); deleting one of
241
+ them drops the kept summary, since it may quote that report.
242
+ - `lib/ai-text.js` is the same file as Support's: change both.
@@ -100,3 +100,14 @@
100
100
  .fb-loading { display: block; margin: .5rem 0; color: var(--dm-text-secondary); font-size: var(--dm-font-size-sm); }
101
101
  /* A panel saving before it closes is locked (inert): show it. */
102
102
  .fb-form.is-saving, .fb-detail.is-saving { opacity: .65; cursor: progress; }
103
+
104
+ /* "Summarise" (AI Assist, the Manager only) */
105
+ .fb-themes { display: grid; gap: .5rem; margin: 0 0 .8rem; padding: .65rem .75rem; border: 1px dashed var(--dm-border); border-radius: var(--dm-radius-md); }
106
+ .fb-themes-bar { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
107
+ .fb-themes-bar .fb-hint { margin: 0; }
108
+ .fb-themes-summary { margin: 0; font-weight: 500; }
109
+ .fb-theme-list { display: grid; gap: .5rem; }
110
+ .fb-theme { border-left: 3px solid var(--dm-primary); padding: .15rem 0 .15rem .6rem; }
111
+ .fb-theme-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: .5rem; }
112
+ .fb-theme p { margin: .15rem 0 0; }
113
+ .fb-theme-quotes { font-style: italic; color: var(--dm-text-secondary); font-size: var(--dm-font-size-sm); }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Feedback inbox - the "Summarise" card (AI Assist, the Manager only). PURE, so it is tested in node: what
3
+ * the card shows for the kept result, the button's label, and the question asked before anything goes.
4
+ *
5
+ * @module feedback/admin/lib/ai
6
+ */
7
+
8
+ /** 16 Nov, 14:05 - or '' for no date. */
9
+ export function shortStamp(iso) {
10
+ const d = iso ? new Date(iso) : null;
11
+ return d && !Number.isNaN(d.getTime()) ? d.toLocaleString('en-GB', {day: 'numeric', month: 'short', hour: '2-digit', minute: '2-digit'}) : '';
12
+ }
13
+
14
+ /**
15
+ * The card's model.
16
+ * @param {object|null} kept the latest result: {at, filter, answers, summary, themes}
17
+ * @param {{busy?: boolean, shown?: number}} o busy: a summary is on its way; shown: reports in the view now
18
+ */
19
+ export function themesCard(kept, {busy = false, shown = 0} = {}) {
20
+ const themes = (kept?.themes || []).map((t, k) => ({
21
+ key: String(k), title: t.title || '', detail: t.detail || '',
22
+ countText: `${t.count} report${t.count === 1 ? '' : 's'}`,
23
+ quotesText: (t.quotes || []).filter(Boolean).map(q => `"${q}"`).join(' · ')
24
+ }));
25
+ const when = shortStamp(kept?.at);
26
+ return {
27
+ canSummarise: !busy && shown >= 2,
28
+ label: busy ? 'Summarising...' : kept ? 'Summarise again' : 'Summarise',
29
+ summary: kept?.summary || '',
30
+ themes,
31
+ note: kept ? `AI summary of ${kept.answers} report${kept.answers === 1 ? '' : 's'}${kept.filter ? ` (${kept.filter})` : ''}${when ? `, ${when}` : ''}.`
32
+ : shown >= 2 ? `Group the ${shown} reports shown into themes.` : 'Summarising needs at least two reports in view.'
33
+ };
34
+ }
35
+
36
+ /** Asked once per visit, before any report leaves for Claude. */
37
+ export const themesQuestion = (n) => `Summarise the ${n} reports shown with AI? Their titles and descriptions go to Claude through AI Assist - `
38
+ + 'no site names, people\'s names, email addresses or files. Each summary counts towards this server\'s monthly AI Assist allowance.';
39
+
40
+ /** The toast after a summary. */
41
+ export const usageText = (res, what = 'Summarised') => (res?.limit ? `${what}. ${res.used} of ${res.limit} AI Assist requests used this month.` : `${what}.`);
@@ -54,7 +54,9 @@ export const api = {
54
54
  /** Where a file is read from: this site's (or the manager's own) copy, or a report's in the inbox. */
55
55
  ownFileUrl: (id) => `${BASE}/attachments/${seg(id)}`,
56
56
  reportFileUrl: (id, att) => `${BASE}/reports/${seg(id)}/attachments/${seg(att)}`,
57
- promote: (id, d) => H.post(`${BASE}/reports/${seg(id)}/promote`, d)
57
+ promote: (id, d) => H.post(`${BASE}/reports/${seg(id)}/promote`, d),
58
+ /** "Summarise" (AI Assist, the Manager only): the inbox's filter as it stands -> the themes, kept. */
59
+ themes: (filter) => H.post(`${BASE}/themes`, filter)
58
60
  };
59
61
 
60
62
  // ---------------------------------------------------------------------------------
@@ -17,7 +17,7 @@ import {api, loadKit, store} from '../lib/kit.js';
17
17
  import {addSettingsCog} from '../lib/settings.js';
18
18
 
19
19
  const ROUTE = /^#\/plugins\/feedback(\/|$)/;
20
- const V = '1.1.8';
20
+ const V = '1.4.0';
21
21
  const TABS = [
22
22
  {key: 'inbox', label: 'Inbox', icon: 'inbox'},
23
23
  {key: 'mine', label: 'My feedback', icon: 'send'}
@@ -6,11 +6,15 @@
6
6
  * will see, internal notes it will not, and - with Waypoint Pro here - a
7
7
  * Waypoint issue made from it, whose progress the report then follows.
8
8
  * Right-click (or the row's kebab, or Shift+F10) for the quick moves.
9
+ *
10
+ * With AI Assist on the Manager, "Summarise" groups the reports in view (the
11
+ * filters as they stand) into themes with Claude; the latest result is kept.
9
12
  */
10
13
 
11
14
  import {FILES, api, copyText, esc, guardPanel, offerDraft, openPanel, panelSet, refreshBadge, rowKeys, showFiles, stamp, store, when} from '../lib/kit.js';
12
15
  import {DRAFT_KEYS, draftQuestion, draftText, draftValues, reportForm, reportPrint} from '../lib/drafts.js';
13
16
  import {richTextToPlain, showRichText} from '/admin/js/lib/rich-field.js';
17
+ import {themesCard, themesQuestion, usageText} from '../lib/ai.js';
14
18
 
15
19
  const ROW = `
16
20
  <div class="fb-row" data-id="{{id}}" tabindex="0" role="button" aria-haspopup="menu">
@@ -23,10 +27,17 @@ const ROW = `
23
27
 
24
28
  const TONE = {new: 'info', acknowledged: 'muted', planned: 'info', 'in-progress': 'warning', done: 'success', declined: 'muted'};
25
29
  const ISSUE_TYPES = [{key: 'bug', label: 'Bug'}, {key: 'story', label: 'Story'}, {key: 'task', label: 'Task'}];
30
+ /** One theme in the "Summarise" card. Text bindings only: nothing from Claude is parsed as HTML. */
31
+ const THEME = `
32
+ <div class="fb-theme">
33
+ <div class="fb-theme-head"><strong data-bind-text="title"></strong><span class="fb-chip is-muted" data-bind-text="countText"></span></div>
34
+ <p data-bind-hidden="!detail" data-bind-text="detail"></p>
35
+ <p class="fb-theme-quotes" data-bind-hidden="!quotesText" data-bind-text="quotesText"></p>
36
+ </div>`;
26
37
  const PRIORITIES = [{key: 'highest', label: 'Highest'}, {key: 'high', label: 'High'}, {key: 'medium', label: 'Medium'}, {key: 'low', label: 'Low'}, {key: 'lowest', label: 'Lowest'}];
27
38
 
28
39
  export async function mount(root, ctx) {
29
- const {kit, scope, here, meta, id} = ctx;
40
+ const {kit, scope, here, meta, id, mode} = ctx;
30
41
  const types = meta.types || [];
31
42
  const statuses = meta.statuses || [];
32
43
  const iconOf = (t) => types.find(x => x.key === t)?.icon || 'message-square';
@@ -80,6 +91,14 @@ export async function mount(root, ctx) {
80
91
  }));
81
92
  const emptyText = M.computed(() => (reports.value.length ? 'Nothing matches.' : 'No feedback yet. Reports from every site the manager runs arrive here.'));
82
93
 
94
+ // "Summarise" (AI Assist): offered only when the server says it can be asked for here.
95
+ const ai = M.observable(mode?.ai === true);
96
+ const kept = M.observable(null);
97
+ const summarising = M.observable(false);
98
+ const themes = M.computed(() => themesCard(kept.value, {busy: summarising.value, shown: rows.value.length}));
99
+ const themeRows = M.computed(() => themes.value.themes);
100
+ scope(() => { themes.dispose?.(); themeRows.dispose?.(); });
101
+
83
102
  root.innerHTML = '';
84
103
  const view = document.createElement('div');
85
104
  view.className = 'fb-card';
@@ -101,6 +120,15 @@ export async function mount(root, ctx) {
101
120
  <input type="search" class="form-input fb-search" placeholder="Search title, text, person, site..." aria-label="Search feedback" data-model="query.value">
102
121
  <span data-help-title="Narrowing the list" data-help="The chips above pick a status (Open is everything not Done or Not planned); these narrow it further to one site or one kind of report. The search looks in the title, the text, who sent it, the site and the Waypoint key. The chip counts are for every site and type."></span>
103
122
  </div>
123
+ <div class="fb-themes" data-bind-hidden="!ai.value">
124
+ <div class="fb-themes-bar">
125
+ <button type="button" class="btn btn-sm btn-outline" data-summarise data-bind-disabled="!themes.value.canSummarise"><span data-icon="sparkles" data-icon-size="13"></span> <span data-bind-text="themes.value.label"></span></button>
126
+ <small class="fb-hint" data-bind-text="themes.value.note"></small>
127
+ <span data-help-title="Summarise" data-help="Groups the reports in view - the status chip, type, site and search as they are set now - into themes with AI, each with how many reports it covers and a few of their words. Only the reports' titles and descriptions go to Claude, through AI Assist: no site names, people's names, email addresses or files. The latest summary stays here until you summarise again. Each one counts towards this server's monthly AI Assist allowance."></span>
128
+ </div>
129
+ <p class="fb-themes-summary" data-bind-hidden="!themes.value.summary" data-bind-text="themes.value.summary"></p>
130
+ <div class="fb-theme-list" data-each="themeRows.value key=key"></div>
131
+ </div>
104
132
  <div class="fb-loading dm-loading-slot" data-bind-hidden="!loading.value">Loading...</div>
105
133
  <p class="fb-note is-bad" data-bind-hidden="!error.value" data-bind-text="error.value"></p>
106
134
  <p class="fb-note" data-bind-hidden="loading.value || error.value || rows.value.length" data-bind-text="emptyText.value"></p>
@@ -110,6 +138,7 @@ export async function mount(root, ctx) {
110
138
  // {{icon}} is marked processed - every row cloned from it would then stay blank.
111
139
  scope(kit.helpPopovers(view, {position: 'bottom'}));
112
140
  view.querySelector('.fb-list').innerHTML = ROW;
141
+ view.querySelector('.fb-theme-list').innerHTML = THEME;
113
142
  // The list re-renders whenever the rows change; its icons need scanning each time,
114
143
  // after the render has landed (flush first, or the spans are scanned empty).
115
144
  const listEl = view.querySelector('.fb-list');
@@ -138,6 +167,7 @@ export async function mount(root, ctx) {
138
167
  if (!here()) return;
139
168
  sites.value = res.sites || {};
140
169
  reports.value = Array.isArray(res.reports) ? res.reports : [];
170
+ kept.value = res.themes && typeof res.themes === 'object' ? res.themes : null;
141
171
  error.value = '';
142
172
  } catch (err) {
143
173
  if (here()) error.value = `Could not load the feedback: ${err.message || err}`;
@@ -366,8 +396,34 @@ export async function mount(root, ctx) {
366
396
  return true;
367
397
  }
368
398
 
369
- const bindings = M.applyBindings({loading, error, filter, site, type, query, rows, counts, emptyText}, view);
399
+ const bindings = M.applyBindings({loading, error, filter, site, type, query, rows, counts, emptyText, ai, themes, themeRows}, view);
370
400
  scope(() => bindings.dispose());
401
+
402
+ // Summarise (AI Assist): asked once per visit before any report leaves for Claude.
403
+ let toldAboutAi = false;
404
+ async function summarise() {
405
+ if (summarising.peek() || !ai.peek()) return;
406
+ const n = rows.peek().length;
407
+ if (n < 2) return;
408
+ if (!toldAboutAi && !await E.confirm(themesQuestion(n))) return;
409
+ if (!here()) return;
410
+ toldAboutAi = true;
411
+ summarising.value = true;
412
+ try {
413
+ const res = await api.themes({status: filter.peek(), type: type.peek(), site: site.peek(), q: query.peek()});
414
+ if (!here()) return;
415
+ kept.value = res;
416
+ E.toast(usageText(res), {type: 'success'});
417
+ } catch (err) {
418
+ if (here()) E.toast(`No summary: ${err.message || err}`, {type: 'error', duration: 8000});
419
+ } finally {
420
+ summarising.value = false;
421
+ }
422
+ }
423
+ const summariseBtn = view.querySelector('[data-summarise]');
424
+ const onSummarise = () => { summarise(); };
425
+ summariseBtn.addEventListener('click', onSummarise);
426
+ scope(() => summariseBtn.removeEventListener('click', onSummarise));
371
427
  const seg = view.querySelector('.fb-seg');
372
428
  const onFilter = (e) => { const b = e.target.closest('[data-filter]'); if (b) filter.value = b.dataset.filter; };
373
429
  seg.addEventListener('click', onFilter);
@@ -84,6 +84,7 @@ On the Domma server itself (the Manager), the same screen is the **Inbox** of ev
84
84
  - With Waypoint Pro installed, **Make a Waypoint issue** files the report in a project with a type and priority. The report then follows the issue: planned, in progress, done. "Not planned" set by hand stays until you change it.
85
85
  - Right-click a row to open it, change its status quickly, or **Delete** it. Deleting removes it for the site too.
86
86
  - Holders of the most senior role also get **My feedback**, to file their own reports. These appear in the Inbox as "Manager (this server)".
87
+ - With **AI Assist** on the Domma server, **Summarise** above the list groups the reports in view into themes with AI: a short summary, then each theme with how many reports it covers and a few of their words. It follows the filters as they are set (status, type, site and search), so set them first - for example **New** and **Bug** for the new bug reports. Only the reports' titles and descriptions go to Claude: no site names or web addresses, people's names, email addresses or files, and greetings and sign-offs are left out. You are asked once per visit before anything is sent. The latest summary stays on the screen, with when it was made and for which reports, until you summarise again or delete one of the reports it read. Each summary counts towards the server's monthly AI Assist allowance.
87
88
  - The **Feedback settings** cog sets how many reports one site may send in a window (default 10 in 10 minutes); more are turned away for now and the site sends them later. Changes apply to the next report.
88
89
 
89
90
  ## Limitations
@@ -94,3 +95,4 @@ On the Domma server itself (the Manager), the same screen is the **Inbox** of ev
94
95
  - A report keeps its formatting in the inbox, in your own list and in a Waypoint issue made from it. Notices and copied text are plain. If the other end runs an older version of Feedback it shows the same words without the formatting.
95
96
  - The Domma server accepts up to 10 reports from one site in 10 minutes. Any more show as **Waiting to send** and go later.
96
97
  - You cannot edit or withdraw a report once sent. Send a follow-up instead.
98
+ - **Summarise** is only on the Domma server, with AI Assist. It needs at least two reports in view and reads up to 1,000 of them, newest first.
@@ -0,0 +1,217 @@
1
+ /**
2
+ * AI Assist on the Manager - the receiver side only. The Domma team's inbox asks Claude for summaries and
3
+ * suggestions through the Manager's own AI Assist service, reached in this process:
4
+ * globalThis[Symbol.for('domma.ai-text')] = {version: 1, tasks: [...], run(body) -> Promise<{status, body}>}
5
+ * (site-manager/services/aiTextRoutes.js). There is no fleet route here: the receiver IS the Manager, and a
6
+ * customer site never offers these features.
7
+ *
8
+ * What is sent is text only, cleaned by `scrubText()`: no names, email addresses, phone numbers, ids or files.
9
+ * A suggestion is never sent or saved by itself: the person sees it and keeps, edits or drops it.
10
+ *
11
+ * Feedback and Support each carry their own copy of this file (either can be installed alone).
12
+ *
13
+ * @module lib/ai-text
14
+ */
15
+ import fs from 'node:fs';
16
+ import path from 'node:path';
17
+
18
+ let aiAssistLookup = null;
19
+ try {
20
+ ({isAiAssist: aiAssistLookup} = await import('../../../server/services/aiAssist.js'));
21
+ } catch { /* an engine before AI Assist: no AI features */ }
22
+
23
+ /** Whether this server has AI Assist (the Tool is on and licensed). Tests set their own. */
24
+ export function aiAssistOn() {
25
+ try { return typeof aiAssistLookup === 'function' && aiAssistLookup() === true; } catch { return false; }
26
+ }
27
+ export function useAiAssistLookup(fn) { aiAssistLookup = fn; }
28
+
29
+ const LOCAL = Symbol.for('domma.ai-text');
30
+ const registry = () => {
31
+ const r = globalThis[LOCAL];
32
+ return r && typeof r === 'object' && typeof r.run === 'function' ? r : null;
33
+ };
34
+
35
+ /** Whether a task can be asked for here: AI Assist on, and the Manager's service in this process. */
36
+ export function aiTextAvailable(task) {
37
+ if (!aiAssistOn()) return false;
38
+ const r = registry();
39
+ if (!r) return false;
40
+ return !task || !Array.isArray(r.tasks) || r.tasks.includes(task);
41
+ }
42
+
43
+ /**
44
+ * Ask the Manager's AI Assist service. Never throws: {status, body} as it answered, or a 403/503 of its own.
45
+ * @param {object} body {task, ...} as aiTextRoutes.js takes it
46
+ */
47
+ export async function requestAiText(body) {
48
+ if (!aiAssistOn()) return {status: 403, body: {error: 'This comes with AI Assist, in the Pro plan.'}};
49
+ const r = registry();
50
+ if (!r) return {status: 503, body: {error: 'AI Assist is not available on this server.'}};
51
+ try {
52
+ const out = await r.run(body);
53
+ if (!out || !Number.isInteger(out.status)) return {status: 503, body: {error: 'AI Assist did not answer. Try again shortly.'}};
54
+ return {status: out.status, body: out.body && typeof out.body === 'object' ? out.body : {}};
55
+ } catch {
56
+ return {status: 503, body: {error: 'AI Assist is not available just now. Try again shortly.'}};
57
+ }
58
+ }
59
+
60
+ /**
61
+ * The answer an admin route gives when the service did not produce a result: its status and error text
62
+ * passed through, a decline as 422. Null when there is a result to use.
63
+ */
64
+ export function aiFailure({status, body}, declined = 'Claude would not do that one.') {
65
+ if (status === 200 && !body?.declined) return null;
66
+ return {status: status === 200 ? 422 : status,
67
+ body: {error: body?.declined ? declined : body?.error || 'AI Assist did not answer.', used: body?.used, limit: body?.limit}};
68
+ }
69
+
70
+ const escapeRe = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
71
+ /** Whole words (not inside another word), longest first, any case. */
72
+ const wordsOf = (list) => {
73
+ const alts = [...new Set(list.map(w => w.toLowerCase()))].filter(w => w.length >= 2).sort((a, b) => b.length - a.length).map(escapeRe);
74
+ return alts.length ? new RegExp(`(?<![\\p{L}\\p{N}_])(?:${alts.join('|')})(?![\\p{L}\\p{N}_])`, 'giu') : null;
75
+ };
76
+
77
+ /**
78
+ * Names that stand in for a missing one, and words a mailbox is often called: never treated as anyone's name
79
+ * (taking "site", "admin" or "support" out of every message would lose its sense, and protect no one).
80
+ */
81
+ const NOT_NAMES = new Set(['a', 'an', 'the', 'site', 'admin', 'administrator', 'an admin', 'a site admin', 'domma', 'domma support', 'support',
82
+ 'team', 'customer', 'manager', 'user', 'info', 'hello', 'contact', 'sales', 'office', 'mail', 'email', 'enquiries', 'help', 'noreply',
83
+ 'no-reply', 'webmaster', 'accounts', 'billing']);
84
+
85
+ /** The parts of the names and of the email addresses' local parts (john.smith@ -> john, smith). */
86
+ export function nameParts({names = [], emails = []} = {}) {
87
+ const out = new Set();
88
+ const add = (w) => { const v = String(w || '').trim().replace(/^[^\p{L}]+|[^\p{L}]+$/gu, ''); if (v.length >= 2 && !NOT_NAMES.has(v.toLowerCase())) out.add(v); };
89
+ for (const n of names || []) {
90
+ const full = String(n || '').trim();
91
+ if (!full || NOT_NAMES.has(full.toLowerCase())) continue;
92
+ add(full);
93
+ for (const w of full.split(/\s+/)) add(w);
94
+ }
95
+ for (const e of emails || []) {
96
+ const local = String(e || '').split('@')[0] || '';
97
+ if (!String(e || '').includes('@')) continue;
98
+ for (const w of local.split(/[._+\-\d]+/)) if (/^\p{L}+$/u.test(w)) add(w);
99
+ }
100
+ return [...out];
101
+ }
102
+
103
+ const GREETING_LINE = /^\s*(?:hi|hello|hey|dear|good (?:morning|afternoon|evening)|morning|afternoon)\b(?:[ \t]+[\p{L}'-]+){0,3}[ \t]*[,!.]?[ \t]*$/iu;
104
+ const GREETING_START = /^\s*(?:hi|hello|hey|dear|good (?:morning|afternoon|evening))\b[^,\n]{0,40},[ \t]*/iu;
105
+ const WORDS = "[\\p{L}'.-]+(?:[ \\t]+[\\p{L}'.-]+){0,2}";
106
+ /** "Thanks," / "Kind regards" / "Cheers, Jo" / "Thanks Darryl": a line of its own. Bare "Best" needs its comma or the line to end. */
107
+ const SIGN_OFF = new RegExp(`^\\s*(?:(?:many thanks|thanks(?: again| so much| very much)?|thank you(?: so much| very much| again)?|kind regards|warm regards|best regards|best wishes|all the best|regards)[ \\t]*(?:[,.!][ \\t]*(?:${WORDS})?|[ \\t]+${WORDS})?|(?:best|cheers)[ \\t]*(?:[,.!][ \\t]*(?:${WORDS})?)?)[ \\t]*[,.!]?[ \\t]*$`, 'iu');
108
+
109
+ /**
110
+ * Text fit to send. Taken out: a greeting at the start ("Hi Ann,"), a sign-off and the short lines under it
111
+ * ("Thanks,\nAnn Jones\nAcme Ltd"), earlier messages quoted in an email (lines starting ">", "On ... wrote:" and
112
+ * what follows), a signature after "-- ", email addresses and phone numbers; then the people and sites known to be
113
+ * in it: `names` and the local parts of `emails` (whole names or any part, any case, as whole words) become [name],
114
+ * `sites` (names and domains, any case; a link on a domain too) become [site]. Spaces tidied. `letter: false` (a title
115
+ * or subject) keeps the first line and any "Thanks" as they are.
116
+ */
117
+ export function scrubText(raw, max = 2000, {names = [], emails = [], sites = [], letter = true} = {}) {
118
+ const people = wordsOf(nameParts({names, emails}));
119
+ const siteList = (sites || []).map(n => String(n || '').trim().toLowerCase()).filter(Boolean);
120
+ const hosts = siteList.filter(s => /^[a-z0-9-]+(?:\.[a-z0-9-]+)+$/.test(s));
121
+ const links = hosts.length
122
+ ? new RegExp(`(?:https?://)?(?:[a-z0-9-]+\\.)*(?:${hosts.map(escapeRe).join('|')})(?::\\d+)?(?:[/?#](?:[^\\s<>"']*[^\\s<>"'.,;:!?)\\]])?)?(?![\\w.-]*[a-z0-9])`, 'gi') : null;
123
+ const places = wordsOf(siteList.filter(s => !hosts.includes(s)));
124
+ const lines = String(raw ?? '').replace(/\r\n?/g, '\n').replace(/ /g, ' ').split('\n');
125
+ const kept = [];
126
+ let started = false;
127
+ for (let i = 0; i < lines.length; i++) {
128
+ let line = lines[i];
129
+ if (/^--\s?$/.test(line)) break;
130
+ if (/^\s*On\b.{0,200}\bwrote:\s*$/i.test(line)) break;
131
+ if (/^\s*>/.test(line)) continue;
132
+ if (letter && !started && line.trim()) {
133
+ started = true;
134
+ if (GREETING_LINE.test(line)) continue;
135
+ line = line.replace(GREETING_START, '');
136
+ }
137
+ if (letter && SIGN_OFF.test(line)) {
138
+ // The name, role and company lines under it: short ones only, at most three.
139
+ let j = i + 1;
140
+ while (j < lines.length && j <= i + 3 && lines[j].trim() && lines[j].trim().length <= 40) j++;
141
+ i = j - 1;
142
+ continue;
143
+ }
144
+ kept.push(line);
145
+ }
146
+ let out = kept.join('\n')
147
+ .replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]+/g, ' ')
148
+ // Bounded parts, so a long run without spaces costs little.
149
+ .replace(/(?:mailto:)?[^\s@<>()"',;:]{1,64}@[^\s@<>()"',;:]{1,253}\.[A-Za-z]{2,24}/g, '[email]')
150
+ // A run of digits, spaces, brackets and hyphens holding 9 to 15 digits is a phone number. Dates and
151
+ // versions (2026-11-16, 1.58.0) have fewer digits, or dots.
152
+ .replace(/(?<![\w.])\+?\(?\d[\d \t()-]{6,24}\d(?!\w|\.\d)/g, (m) => {
153
+ const digits = m.replace(/\D/g, '').length;
154
+ return digits >= 9 && digits <= 15 ? '[phone]' : m;
155
+ });
156
+ if (links) out = out.replace(links, '[site]');
157
+ if (places) out = out.replace(places, '[site]');
158
+ if (people) out = out.replace(people, '[name]');
159
+ return out
160
+ .replace(/[ \t]+/g, ' ')
161
+ .replace(/\n{3,}/g, '\n\n')
162
+ .trim()
163
+ .slice(0, max);
164
+ }
165
+
166
+ const text = (v, max) => String(v ?? '').slice(0, max);
167
+ const whole = (v) => Math.max(0, Math.floor(Number(v) || 0));
168
+
169
+ /** A kept themes result, cleaned (it is shown again later). */
170
+ export function cleanThemes(raw) {
171
+ const r = raw && typeof raw === 'object' ? raw : {};
172
+ return {
173
+ at: text(r.at, 40),
174
+ filter: text(r.filter, 300),
175
+ answers: whole(r.answers),
176
+ summary: text(r.summary, 1500),
177
+ themes: (Array.isArray(r.themes) ? r.themes : []).slice(0, 8).map(t => ({
178
+ title: text(t?.title, 120), detail: text(t?.detail, 600), count: whole(t?.count),
179
+ quotes: (Array.isArray(t?.quotes) ? t.quotes : []).slice(0, 3).map(q => text(q, 300))
180
+ }))
181
+ };
182
+ }
183
+
184
+ /** A kept summary of one conversation, cleaned. */
185
+ export function cleanSummary(raw) {
186
+ const r = raw && typeof raw === 'object' ? raw : {};
187
+ return {
188
+ at: text(r.at, 40),
189
+ messages: whole(r.messages),
190
+ summary: text(r.summary, 1500),
191
+ points: (Array.isArray(r.points) ? r.points : []).slice(0, 5).map(p => text(p, 300)).filter(Boolean)
192
+ };
193
+ }
194
+
195
+ /**
196
+ * Results kept on the Manager, one file: {<key>: value}. Read on each use; written whole, through a temporary
197
+ * file, private (0600). Nothing here is sent anywhere.
198
+ */
199
+ export function createKept(file) {
200
+ const read = () => {
201
+ try {
202
+ const v = JSON.parse(fs.readFileSync(file, 'utf8'));
203
+ return v && typeof v === 'object' && !Array.isArray(v) ? v : {};
204
+ } catch { return {}; }
205
+ };
206
+ const write = (all) => {
207
+ fs.mkdirSync(path.dirname(file), {recursive: true, mode: 0o700});
208
+ const tmp = `${file}.${process.pid}.tmp`;
209
+ fs.writeFileSync(tmp, JSON.stringify(all, null, 2), {mode: 0o600});
210
+ fs.renameSync(tmp, file);
211
+ };
212
+ return {
213
+ get: (key) => read()[key] ?? null,
214
+ set(key, value) { const all = read(); all[key] = value; write(all); return value; },
215
+ remove(key) { const all = read(); if (key in all) { delete all[key]; write(all); } }
216
+ };
217
+ }
@@ -52,6 +52,16 @@ export function siteNames() {
52
52
  } catch { return {}; }
53
53
  }
54
54
 
55
+ /** slug -> every domain the site answers on (primary, fallback, the rest), from Site Manager's registry. */
56
+ export function siteHosts() {
57
+ try {
58
+ const j = JSON.parse(fs.readFileSync(path.join(SITE_MANAGER, 'data', 'sites.json'), 'utf8'));
59
+ const list = Array.isArray(j) ? j : (j.sites || Object.values(j));
60
+ return Object.fromEntries(list.filter(s => s?.slug).map(s => [s.slug,
61
+ [...new Set([s.primaryDomain, s.fallbackDomain, ...(Array.isArray(s.domains) ? s.domains : [])].filter(d => typeof d === 'string' && d))]]));
62
+ } catch { return {}; }
63
+ }
64
+
55
65
  /** Make the collection when it is missing. Every boot: a plugin switched on by copy never runs onEnable. */
56
66
  export async function ensureCollection() {
57
67
  if (await getCollection(COLLECTION)) return false;
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Feedback inbox - "Summarise" (AI Assist, the Manager only). PURE: which reports the inbox shows for a filter
3
+ * (the same rules as admin/views/inbox.js), what of them is sent, and how the filter is described to Claude.
4
+ *
5
+ * What is sent of a report: its title and its description as plain text, through scrubText() - never the
6
+ * site, who sent it, their email, where it happened, the CMS version or its files.
7
+ *
8
+ * @module feedback/lib/themes
9
+ */
10
+ import {richTextToPlain} from '../../../server/services/richText.js';
11
+ import {STATUS_KEYS, TYPE_KEYS, statusLabel, typeLabel} from './shape.js';
12
+ import {scrubText} from './ai-text.js';
13
+
14
+ /** The Manager's caps for a themes task (aiTextRoutes.js LIMITS): answers, characters per answer. */
15
+ export const THEMES_MAX = {answers: 1000, answer: 2000};
16
+ export const THEMES_SURVEY = 'Feedback from Domma sites';
17
+
18
+ const plain = (v) => richTextToPlain(String(v || '')).replace(/\u00a0/g, ' ');
19
+
20
+ /** The inbox's filter, from the browser, cleaned: {status: open|all|<status>, type, site, q}. */
21
+ export function cleanFilter(raw = {}) {
22
+ const b = raw && typeof raw === 'object' ? raw : {};
23
+ const status = ['open', 'all', ...STATUS_KEYS].includes(b.status) ? b.status : 'open';
24
+ return {
25
+ status,
26
+ type: TYPE_KEYS.includes(b.type) ? b.type : '',
27
+ site: typeof b.site === 'string' ? b.site.slice(0, 100) : '',
28
+ q: typeof b.q === 'string' ? b.q.trim().slice(0, 200) : ''
29
+ };
30
+ }
31
+
32
+ const statusMatch = (f, s) => (f === 'all' ? true : f === 'open' ? !['done', 'declined'].includes(s) : s === f);
33
+
34
+ /**
35
+ * The reports the inbox shows for a filter, newest first.
36
+ * @param {Array<{id, data, meta}>} entries
37
+ * @param {object} filter cleanFilter()
38
+ * @param {object} names site slug -> name (the search looks in it, as the inbox's does)
39
+ */
40
+ export function reportsInView(entries = [], filter = cleanFilter(), names = {}) {
41
+ const q = filter.q.toLowerCase();
42
+ return entries
43
+ .filter(e => statusMatch(filter.status, e.data?.status))
44
+ .filter(e => !filter.site || e.data?.site === filter.site)
45
+ .filter(e => !filter.type || e.data?.type === filter.type)
46
+ .filter(e => !q || [e.data?.title, plain(e.data?.description), e.data?.userName, names[e.data?.site] || e.data?.site, e.data?.issueKey]
47
+ .join(' ').toLowerCase().includes(q))
48
+ .sort((a, b) => String(b.meta?.createdAt || '').localeCompare(String(a.meta?.createdAt || '')));
49
+ }
50
+
51
+ /** What is sent of each report: "Title. Description", cleaned (the sender's name and email, the site's name and domains too). Blanks dropped; at most THEMES_MAX.answers. */
52
+ export function themesAnswers(entries = [], siteNames = {}, siteHosts = {}) {
53
+ return entries
54
+ .map(e => {
55
+ // The sender (name, and the parts of their email address) and their site (name, domains, links on them).
56
+ const hosts = Array.isArray(siteHosts[e.data?.site]) ? siteHosts[e.data.site] : [];
57
+ const names = {names: [e.data?.userName], emails: [e.data?.userEmail], sites: [siteNames[e.data?.site], ...hosts]};
58
+ const title = scrubText(e.data?.title, 200, {...names, letter: false});
59
+ const body = scrubText(plain(e.data?.description), THEMES_MAX.answer, names);
60
+ return scrubText([title && !/[.!?]$/.test(title) ? `${title}.` : title, body].filter(Boolean).join(' '), THEMES_MAX.answer);
61
+ })
62
+ .filter(Boolean)
63
+ .slice(0, THEMES_MAX.answers);
64
+ }
65
+
66
+ /** The filter in a few words, for Claude and for the card: never a site's name, nor the search's words. */
67
+ export function filterText(filter = cleanFilter()) {
68
+ const what = filter.status === 'all' ? 'Every report'
69
+ : filter.status === 'open' ? 'Open reports (not done or not planned)'
70
+ : `Reports marked ${statusLabel(filter.status)}`;
71
+ return [what,
72
+ filter.type && `${typeLabel(filter.type)} only`,
73
+ filter.site && 'from one site',
74
+ filter.q && 'matching a search'].filter(Boolean).join(', ');
75
+ }