domma-cms 1.69.2 → 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.
Files changed (43) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/admin/js/lib/wordpress-import-arrange.js +1 -1
  3. package/admin/js/templates/docs/usage-tools.html +11 -0
  4. package/admin/js/templates/wordpress-import.html +25 -3
  5. package/admin/js/views/wordpress-import.js +10 -10
  6. package/docs/DommaFeatures.md +3 -1
  7. package/package.json +1 -1
  8. package/plugins/feedback/CHANGELOG.md +5 -0
  9. package/plugins/feedback/CLAUDE.md +26 -0
  10. package/plugins/feedback/admin/css/index.css +11 -0
  11. package/plugins/feedback/admin/lib/ai.js +41 -0
  12. package/plugins/feedback/admin/lib/kit.js +3 -1
  13. package/plugins/feedback/admin/views/feedback.js +1 -1
  14. package/plugins/feedback/admin/views/inbox.js +58 -2
  15. package/plugins/feedback/docs/guide.md +2 -0
  16. package/plugins/feedback/lib/ai-text.js +217 -0
  17. package/plugins/feedback/lib/receiver.js +10 -0
  18. package/plugins/feedback/lib/themes.js +75 -0
  19. package/plugins/feedback/plugin.js +39 -2
  20. package/plugins/feedback/plugin.json +2 -2
  21. package/plugins/feedback/tests/ai.test.js +248 -0
  22. package/plugins/free-tier.lock.json +31 -21
  23. package/plugins/support/CHANGELOG.md +5 -0
  24. package/plugins/support/CLAUDE.md +33 -0
  25. package/plugins/support/admin/css/index.css +14 -0
  26. package/plugins/support/admin/lib/ai.js +68 -0
  27. package/plugins/support/admin/lib/kit.js +5 -1
  28. package/plugins/support/admin/views/inbox.js +121 -4
  29. package/plugins/support/admin/views/support.js +1 -1
  30. package/plugins/support/docs/guide.md +5 -0
  31. package/plugins/support/lib/ai-text.js +217 -0
  32. package/plugins/support/lib/assist.js +74 -0
  33. package/plugins/support/lib/receiver.js +2 -1
  34. package/plugins/support/plugin.js +73 -3
  35. package/plugins/support/plugin.json +2 -2
  36. package/plugins/support/tests/ai.test.js +245 -0
  37. package/server/routes/api/wordpress-import.js +60 -3
  38. package/server/services/wpImport/crawl.js +12 -3
  39. package/server/services/wpImport/custom.js +1 -0
  40. package/server/services/wpImport/index.js +24 -4
  41. package/server/services/wpImport/plan.js +82 -5
  42. package/server/services/wpImport/redirects.js +436 -0
  43. package/server/services/wpImport/wxr.js +5 -1
@@ -27,6 +27,11 @@
27
27
  * PUT /tickets/:id status, priority, internal notes, archived
28
28
  * GET /tickets/:id/attachments/:att a file the customer attached
29
29
  * DELETE /tickets/:id
30
+ * POST /tickets/:id/summary AI Assist: the conversation summarised (kept, shown atop the thread)
31
+ * POST /tickets/:id/suggest AI Assist: a suggested reply, for the reply box - never sent by itself
32
+ * POST /themes AI Assist: the requests waiting on Domma grouped into themes (kept)
33
+ * All three only with AI Assist on, through the Manager's own service in
34
+ * this process (lib/ai-text.js); only the conversation's words are sent.
30
35
  *
31
36
  * A standalone site (not started by a manager) still gets the contact card -
32
37
  * Support is for everyone - but has nowhere to send a conversation.
@@ -46,6 +51,8 @@ import {
46
51
  import {createStore, readUpload, sendFile} from './lib/attachments.js';
47
52
  import {getPluginSettings, getPluginStates, savePluginState} from '../../server/services/plugins.js';
48
53
  import {applyShelf, createOutbox, flush, managerApi, managerOf, mergeTickets, newRef} from './lib/sender.js';
54
+ import {aiFailure, aiTextAvailable, cleanSummary, cleanThemes, createKept, requestAiText} from './lib/ai-text.js';
55
+ import {AI_LIMITS, SUMMARY_MIN, THEMES_QUESTION, THEMES_SURVEY, conversationText, replyThread, subjectOf, themesAnswers, waitingOnDomma} from './lib/assist.js';
49
56
 
50
57
  const HERE = path.dirname(fileURLToPath(import.meta.url));
51
58
  const NAME = 'support';
@@ -342,6 +349,16 @@ async function receiverRoutes(fastify, {can, notify, mail, receiverMod, liveSett
342
349
  };
343
350
  // A request's files live in data/attachments/<request entry id>/.
344
351
  const files = createStore(path.join(options.dataDir || path.join(HERE, 'data'), 'attachments'));
352
+ // AI Assist results kept on the Manager: `summary:<id>` per request, `themes` for the waiting ones.
353
+ const kept = createKept(path.join(options.dataDir || path.join(HERE, 'data'), 'ai.json'));
354
+ const keptSummary = (id) => { const v = kept.get(`summary:${id}`); return v ? cleanSummary(v) : null; };
355
+ const keep = (key, value) => { try { kept.set(key, value); } catch (err) { fastify.log.warn(`[support] AI result not kept: ${err.message}`); } };
356
+ /** What the screen may offer: each AI Assist task this server can do now. */
357
+ const aiOffer = () => ({summary: aiTextAvailable('summary'), reply: aiTextAvailable('reply'), themes: aiTextAvailable('themes')});
358
+ const NO_AI = 'This comes with AI Assist, in the Pro plan.';
359
+ /** A request's site as it may appear in the words (taken out before they go): its name and domain. Not the
360
+ * slug - often a plain word ("jobs") the words need. Its domains (and links on them) go too. */
361
+ const sitesOf = (slug) => { const s = siteOf(slug); return [s.name, s.host, ...(Array.isArray(s.hosts) ? s.hosts : [])].filter(Boolean); };
345
362
  // A site sends a file's bytes raw. Only inside this plugin's scope; the engine has no parser for it.
346
363
  if (!fastify.hasContentTypeParser('application/octet-stream')) {
347
364
  fastify.addContentTypeParser('application/octet-stream', {parseAs: 'buffer', bodyLimit: ATTACHMENTS.maxBytes + 1024}, (req, body, done) => done(null, body));
@@ -446,11 +463,13 @@ async function receiverRoutes(fastify, {can, notify, mail, receiverMod, liveSett
446
463
  const row = (e, dir = directory()) => ({id: e.id, ...e.data, siteName: dir[e.data.site]?.name || e.data.site,
447
464
  siteHost: dir[e.data.site]?.host || '', createdAt: e.meta?.createdAt, updatedAt: e.meta?.updatedAt});
448
465
  /** One request for the panel: plus where a reply will be emailed, and why. */
449
- const detail = async (e) => ({...row(e), replyTo: await replyTarget(e.data)});
466
+ const detail = async (e) => ({...row(e), replyTo: await replyTarget(e.data), aiSummary: keptSummary(e.id),
467
+ // How much of the conversation Summarise would send: the screen holds the button back under SUMMARY_MIN.
468
+ ...(aiTextAvailable('summary') && {aiChars: conversationText(e.data, {sites: sitesOf(e.data.site)}).length})});
450
469
 
451
470
  fastify.get('/mode', can('manage'), async () => {
452
471
  const s = await liveSettings();
453
- return {mode: 'receiver', connected: true, contact: contactCard(s), notifyEmail: s.notifyEmail || ''};
472
+ return {mode: 'receiver', connected: true, contact: contactCard(s), notifyEmail: s.notifyEmail || '', ai: aiOffer()};
454
473
  });
455
474
 
456
475
  fastify.get('/count', can('manage'), async () => {
@@ -476,7 +495,54 @@ async function receiverRoutes(fastify, {can, notify, mail, receiverMod, liveSett
476
495
 
477
496
  fastify.get('/tickets', can('manage'), async () => {
478
497
  const dir = directory();
479
- return {tickets: (await rx.allTickets()).map(e => row(e, dir))};
498
+ const themes = kept.get('themes');
499
+ return {tickets: (await rx.allTickets()).map(e => row(e, dir)), themes: themes ? cleanThemes(themes) : null};
500
+ });
501
+
502
+ // ---- AI Assist (the Manager's own service; only the conversation's words are sent) ----
503
+
504
+ fastify.post('/tickets/:id/summary', can('manage'), async (request, reply) => {
505
+ if (!aiTextAvailable('summary')) return reply.code(403).send({error: NO_AI});
506
+ const e = await rx.getTicket(request.params.id);
507
+ if (!e) return reply.code(404).send({error: 'No such request.'});
508
+ const sites = {sites: sitesOf(e.data.site)};
509
+ const text = conversationText(e.data, sites);
510
+ if (text.length < SUMMARY_MIN) return reply.code(400).send({error: 'There is too little to summarise yet.'});
511
+ const res = await requestAiText({task: 'summary', kind: 'ticket', title: subjectOf(e.data, sites), text});
512
+ const failed = aiFailure(res, 'Claude would not summarise this request.');
513
+ if (failed) return reply.code(failed.status).send(failed.body);
514
+ const result = cleanSummary({at: new Date().toISOString(), messages: (e.data.messages || []).length, summary: res.body.summary, points: res.body.points});
515
+ keep(`summary:${e.id}`, result);
516
+ return {...result, used: res.body.used, limit: res.body.limit};
517
+ });
518
+
519
+ /** A reply to edit: it goes back to the reply box and nowhere else. */
520
+ fastify.post('/tickets/:id/suggest', can('manage'), async (request, reply) => {
521
+ if (!aiTextAvailable('reply')) return reply.code(403).send({error: NO_AI});
522
+ const e = await rx.getTicket(request.params.id);
523
+ if (!e) return reply.code(404).send({error: 'No such request.'});
524
+ const sites = {sites: sitesOf(e.data.site)};
525
+ const res = await requestAiText({task: 'reply', title: subjectOf(e.data, sites), thread: replyThread(e.data, sites)});
526
+ const failed = aiFailure(res, 'Claude would not suggest a reply to this one.');
527
+ if (failed) return reply.code(failed.status).send(failed.body);
528
+ const text = String(res.body.text || '').trim().slice(0, 8000);
529
+ if (!text) return reply.code(502).send({error: 'No reply came back. Try again.', used: res.body.used, limit: res.body.limit});
530
+ return {text, used: res.body.used, limit: res.body.limit};
531
+ });
532
+
533
+ fastify.post('/themes', can('manage'), async (request, reply) => {
534
+ if (!aiTextAvailable('themes')) return reply.code(403).send({error: NO_AI});
535
+ const waiting = waitingOnDomma(await rx.allTickets()).slice(0, AI_LIMITS.answers);
536
+ const answers = themesAnswers(waiting, sitesOf);
537
+ if (answers.length < 2) return reply.code(400).send({error: 'There need to be at least two requests waiting to summarise.'});
538
+ const res = await requestAiText({task: 'themes', kind: 'tickets', survey: THEMES_SURVEY, question: THEMES_QUESTION, answers});
539
+ const failed = aiFailure(res, 'Claude would not summarise these requests.');
540
+ if (failed) return reply.code(failed.status).send(failed.body);
541
+ const result = cleanThemes({at: new Date().toISOString(), filter: THEMES_QUESTION, answers: res.body.answers ?? answers.length,
542
+ summary: res.body.summary, themes: res.body.themes});
543
+ // Which requests it read (never shown): deleting one of them drops the summary, which may quote it.
544
+ keep('themes', {...result, ids: waiting.map(e => e.id)});
545
+ return {...result, used: res.body.used, limit: res.body.limit};
480
546
  });
481
547
 
482
548
  fastify.get('/tickets/:id', can('manage'), async (request, reply) => {
@@ -527,6 +593,10 @@ async function receiverRoutes(fastify, {can, notify, mail, receiverMod, liveSett
527
593
  if (!e) return reply.code(404).send({error: 'No such request.'});
528
594
  await rx.removeTicket(e.id);
529
595
  files.removeAll(e.id);
596
+ try {
597
+ kept.remove(`summary:${e.id}`);
598
+ if ((kept.get('themes')?.ids || []).includes(e.id)) kept.remove('themes');
599
+ } catch { /* tidying only */ }
530
600
  return {ok: true};
531
601
  });
532
602
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "support",
3
3
  "displayName": "Support",
4
- "version": "1.2.1",
4
+ "version": "1.3.0",
5
5
  "tier": "free",
6
6
  "description": "Get help from Domma straight from your admin: our email, phone and hours always to hand, and a conversation with the Domma team for anything you need - urgent requests first. Right-click a request to archive, resolve or remove it.",
7
7
  "author": "Domma CMS",
@@ -61,7 +61,7 @@
61
61
  ],
62
62
  "views": {
63
63
  "plugin-support": {
64
- "entry": "support/admin/views/support.js?v=1.2.1",
64
+ "entry": "support/admin/views/support.js?v=1.3.0",
65
65
  "exportName": "supportView"
66
66
  }
67
67
  }
@@ -0,0 +1,245 @@
1
+ /**
2
+ * AI Assist in the Manager's Support inbox: a request summarised, a reply suggested, the waiting requests'
3
+ * themes (lib/ai-text.js, lib/assist.js, the routes and the cards). The Manager's AI Assist service is a stub on
4
+ * globalThis[Symbol.for('domma.ai-text')]: nothing reaches Claude.
5
+ */
6
+ import {test, before, after} from 'node:test';
7
+ import assert from 'node:assert/strict';
8
+ import fs from 'node:fs/promises';
9
+ import os from 'node:os';
10
+ import path from 'node:path';
11
+ import Fastify from 'fastify';
12
+ import support from '../plugin.js';
13
+ import {aiTextAvailable, useAiAssistLookup} from '../lib/ai-text.js';
14
+ import {conversationText, replyThread, themesAnswers, waitingOnDomma} from '../lib/assist.js';
15
+ import {AI_QUESTION, summaryCard, themesCard, usageText} from '../admin/lib/ai.js';
16
+ import {buildTicketPayload, cleanRequest} from '../lib/shape.js';
17
+ import {customerTurn, dommaTurn, forget, open, patch} from '../lib/receiver.js';
18
+
19
+ const LOCAL = Symbol.for('domma.ai-text');
20
+ const COLL = path.resolve('content/collections/support-tickets');
21
+ let dir;
22
+ let app;
23
+ let sent = [];
24
+ const ids = {};
25
+
26
+ const RESULTS = {
27
+ summary: () => ({summary: 'The customer cannot save.', points: ['Check the save route']}),
28
+ reply: () => ({text: 'Thanks for telling us.\n\nThe Domma team'}),
29
+ themes: (body) => ({summary: 'Saving, mostly.', answers: body.answers.length, themes: [{title: 'Saving', detail: 'd', count: 2, quotes: ['gone']}]})
30
+ };
31
+ function stubRegistry(answer = (body) => ({status: 200, body: {...RESULTS[body.task](body), used: 11, limit: 500}})) {
32
+ globalThis[LOCAL] = {version: 1, tasks: ['themes', 'subjects', 'draft', 'rewrite', 'summary', 'reply'],
33
+ run: async (body) => { sent.push(body); return answer(body); }};
34
+ }
35
+
36
+ const call = async (method, url, body) => {
37
+ const res = await app.inject({method, url, payload: body ?? {}});
38
+ return {status: res.statusCode, body: res.json()};
39
+ };
40
+
41
+ async function request(slug, req, user) {
42
+ const {request: r, error} = cleanRequest(req);
43
+ assert.equal(error, undefined);
44
+ const ref = `ref-${Math.random().toString(36).slice(2)}`;
45
+ const out = await open(slug, buildTicketPayload({request: r, user, site: slug, ref}));
46
+ assert.ok(out.entry, out.error);
47
+ return {id: out.entry.id, ref};
48
+ }
49
+
50
+ before(async () => {
51
+ await fs.rm(COLL, {recursive: true, force: true});
52
+ dir = await fs.mkdtemp(path.join(os.tmpdir(), 'support-ai-'));
53
+ app = Fastify();
54
+ await app.register(support, {
55
+ auth: {authenticate: async (req) => { req.user = {id: 'v1', name: 'Darryl'}; }, requirePermission: () => async () => {}},
56
+ mode: 'receiver', mail: {send: async () => true, siteUrl: () => 'https://dcm.example.test'}, ownerEmail: async () => null,
57
+ dataDir: dir, readSettings: async () => ({}), slugFromToken: async () => null,
58
+ siteDirectory: () => ({acme: {name: 'Acme Ltd', host: 'acme.example.test'}, beta: {name: 'Beta Co', host: 'beta.example.test'}}),
59
+ hooks: {}
60
+ });
61
+ const ann = {id: 'u1', name: 'Ann Jones', email: 'ann.jones@acme.test'};
62
+ const a = await request('acme', {subject: 'Pages will not save', body: 'Hi, Ann here. Every save on Acme Ltd loses my text, see https://acme.example.test/contact. It happens whether I type a little or a lot, and the words are simply gone once the page reloads. Call 07700 900123.\n\nThanks,\nAnn Jones\nAcme Ltd'}, ann);
63
+ ids.saving = a.id;
64
+ await dommaTurn(a.id, {body: 'Hi Ann, which page? Darryl', by: 'Darryl', msgId: 'd1'});
65
+ await customerTurn('acme', {ticketRef: a.ref, ref: 'c2', body: 'The contact page.\n> Hi Ann, which page?', user: ann});
66
+ const b = await request('beta', {subject: 'Save button greyed', body: 'It never comes back after the first save.'}, {id: 'u2', name: 'Bo', email: 'bo@beta.test'});
67
+ ids.greyed = b.id;
68
+ const c = await request('beta', {subject: 'Old one', body: 'Archived away.'}, {id: 'u2', name: 'Bo'});
69
+ await patch(c.id, {archived: true}, 'Darryl');
70
+ ids.old = c.id;
71
+ forget();
72
+ });
73
+
74
+ after(async () => {
75
+ delete globalThis[LOCAL];
76
+ useAiAssistLookup(null);
77
+ await app?.close();
78
+ await fs.rm(COLL, {recursive: true, force: true});
79
+ await fs.rm(dir, {recursive: true, force: true});
80
+ });
81
+
82
+ test('the conversation as Customer:/Domma: lines and as a thread - no names, addresses, numbers or quoted mail', () => {
83
+ const data = {subject: 'S', userName: 'Ann Jones', messages: [
84
+ {from: 'customer', name: 'Ann Jones', body: 'Hi, Ann here. Mail ann@x.io.\n-- \nAnn Jones'},
85
+ {from: 'domma', name: 'Darryl', body: 'Hello Ann. Darryl'},
86
+ {from: 'customer', name: 'Ann Jones', body: '> Hello Ann\nThanks!'},
87
+ {from: 'customer', name: 'Ann Jones', body: ''}]};
88
+ // "Hi," and a lone "Thanks!" go with the quoted mail and the signature.
89
+ assert.equal(conversationText(data), 'Customer: [name] here. Mail [email].\n\nDomma: Hello [name]. [name]');
90
+ assert.deepEqual(replyThread(data), [{from: 'customer', text: '[name] here. Mail [email].'}, {from: 'us', text: 'Hello [name]. [name]'}]);
91
+ // No name on the request ("A site admin" stands in): the customer's email address still names them, in any case.
92
+ const unnamed = {userName: 'A site admin', userEmail: 'john.smith@acme.test', messages: [
93
+ {from: 'customer', name: 'A site admin', body: 'The site admin menu fails. JOHN\n\nKind regards\njohn smith\nDirector'},
94
+ {from: 'domma', name: 'Domma Support', body: 'thanks, john - Domma Support will check.'}]};
95
+ assert.equal(conversationText(unnamed), 'Customer: The site admin menu fails. [name]\n\nDomma: thanks, [name] - Domma Support will check.');
96
+ assert.equal(conversationText({messages: [{from: 'customer', body: 'Broken on https://www.acme.example.test/shop, Acme Ltd.'}]}, {sites: ['Acme Ltd', 'acme.example.test']}),
97
+ 'Customer: Broken on [site], [site].');
98
+ // Long: the latest kept.
99
+ const long = {messages: Array.from({length: 50}, (_, i) => ({from: 'customer', body: `${i} ${'x'.repeat(7000)}`}))};
100
+ const text = conversationText(long);
101
+ assert.ok(text.length <= 60_000);
102
+ assert.ok(text.endsWith('x') && text.includes('Customer: 49 '));
103
+ assert.equal(replyThread(long).length, 40);
104
+ const waiting = waitingOnDomma([{data: {status: 'open'}}, {data: {status: 'open', archived: true}}, {data: {status: 'answered'}}]);
105
+ assert.equal(waiting.length, 1);
106
+ assert.deepEqual(themesAnswers([{data: {subject: 'Hi', messages: [{from: 'customer', body: 'one'}, {from: 'domma', body: 'ours'}, {from: 'customer', body: 'two'}]}}]), ['Hi. one two']);
107
+ });
108
+
109
+ test('the screen is told which tasks it may offer', async () => {
110
+ useAiAssistLookup(() => false);
111
+ stubRegistry();
112
+ assert.deepEqual((await call('GET', '/mode')).body.ai, {summary: false, reply: false, themes: false});
113
+ useAiAssistLookup(() => true);
114
+ assert.deepEqual((await call('GET', '/mode')).body.ai, {summary: true, reply: true, themes: true});
115
+ globalThis[LOCAL] = {version: 1, tasks: ['themes'], run: async () => ({status: 200, body: {}})};
116
+ assert.deepEqual((await call('GET', '/mode')).body.ai, {summary: false, reply: false, themes: true});
117
+ delete globalThis[LOCAL];
118
+ assert.equal(aiTextAvailable('summary'), false);
119
+ assert.equal((await call('POST', `/tickets/${ids.saving}/summary`)).status, 403);
120
+ assert.equal((await call('POST', `/tickets/${ids.saving}/suggest`)).status, 403);
121
+ assert.equal((await call('POST', '/themes')).status, 403);
122
+ });
123
+
124
+ test('Summarise: the conversation goes as text, the summary is kept and shown with the request', async () => {
125
+ useAiAssistLookup(() => true);
126
+ stubRegistry();
127
+ sent = [];
128
+ const r = await call('POST', `/tickets/${ids.saving}/summary`);
129
+ assert.equal(r.status, 200, JSON.stringify(r.body));
130
+ assert.deepEqual(r.body.points, ['Check the save route']);
131
+ assert.equal(r.body.messages, 3);
132
+ assert.equal(r.body.used, 11);
133
+ const body = sent[0];
134
+ assert.equal(body.task, 'summary');
135
+ assert.equal(body.kind, 'ticket');
136
+ assert.equal(body.title, 'Pages will not save');
137
+ assert.match(body.text, /^Customer: \[name\] here\. Every save on \[site\] loses my text, see \[site\]\. It happens .* reloads\. Call \[phone\]\.\n\nDomma: which page\? \[name\]\n\nCustomer: The contact page\.$/);
138
+ const wire = JSON.stringify(body);
139
+ for (const secret of ['Ann', 'Jones', 'Darryl', 'ann.jones@acme.test', '07700', 'acme.example.test', 'Acme', '/contact']) assert.ok(!wire.includes(secret), secret);
140
+ const detail = await call('GET', `/tickets/${ids.saving}`);
141
+ assert.equal(detail.body.aiSummary.summary, 'The customer cannot save.');
142
+ assert.equal(detail.body.aiSummary.messages, 3);
143
+ });
144
+
145
+ test('Suggest a reply: the thread from customer|us, the text back for the box, nothing sent or saved', async () => {
146
+ useAiAssistLookup(() => true);
147
+ stubRegistry();
148
+ sent = [];
149
+ const r = await call('POST', `/tickets/${ids.saving}/suggest`);
150
+ assert.equal(r.status, 200, JSON.stringify(r.body));
151
+ assert.equal(r.body.text, 'Thanks for telling us.\n\nThe Domma team');
152
+ assert.deepEqual(sent[0].thread.map(m => m.from), ['customer', 'us', 'customer']);
153
+ assert.equal(sent[0].task, 'reply');
154
+ assert.equal(sent[0].title, 'Pages will not save');
155
+ // The request is as it was: no new message, no status change.
156
+ const detail = await call('GET', `/tickets/${ids.saving}`);
157
+ assert.equal(detail.body.messages.length, 3);
158
+ assert.equal(detail.body.status, 'open');
159
+ });
160
+
161
+ test('the Manager\'s answers pass through: too little, a decline, the allowance, busy', async () => {
162
+ useAiAssistLookup(() => true);
163
+ // Too short for the Manager (under 200 characters once cleaned): the screen is told, and nothing is asked.
164
+ stubRegistry();
165
+ sent = [];
166
+ assert.ok((await call('GET', `/tickets/${ids.greyed}`)).body.aiChars < 200);
167
+ assert.ok((await call('GET', `/tickets/${ids.saving}`)).body.aiChars >= 200);
168
+ const short = await call('POST', `/tickets/${ids.greyed}/summary`);
169
+ assert.equal(short.status, 400);
170
+ assert.match(short.body.error, /too little/);
171
+ assert.equal(sent.length, 0);
172
+ stubRegistry(() => ({status: 200, body: {declined: true, used: 12, limit: 500}}));
173
+ const declined = await call('POST', `/tickets/${ids.greyed}/suggest`);
174
+ assert.equal(declined.status, 422);
175
+ assert.equal(declined.body.used, 12);
176
+ stubRegistry(() => ({status: 429, body: {error: 'All used this month.', used: 500, limit: 500}}));
177
+ assert.equal((await call('POST', `/tickets/${ids.greyed}/suggest`)).body.error, 'All used this month.');
178
+ stubRegistry(() => ({status: 503, body: {error: 'Busy just now. Try again shortly.'}}));
179
+ assert.equal((await call('POST', '/themes')).status, 503);
180
+ stubRegistry(() => ({status: 200, body: {text: ' ', used: 13, limit: 500}}));
181
+ assert.equal((await call('POST', `/tickets/${ids.greyed}/suggest`)).status, 502);
182
+ assert.equal((await call('POST', '/tickets/nope/summary')).status, 404);
183
+ // The kept summary is untouched by the failures.
184
+ assert.equal((await call('GET', `/tickets/${ids.saving}`)).body.aiSummary.summary, 'The customer cannot save.');
185
+ });
186
+
187
+ test('Summarise waiting requests: open and not archived only, kept for the list', async () => {
188
+ useAiAssistLookup(() => true);
189
+ stubRegistry();
190
+ sent = [];
191
+ const r = await call('POST', '/themes');
192
+ assert.equal(r.status, 200, JSON.stringify(r.body));
193
+ const body = sent[0];
194
+ assert.equal(body.kind, 'tickets');
195
+ assert.equal(body.survey, 'Support requests from Domma sites');
196
+ assert.equal(body.question, 'Requests waiting on Domma');
197
+ assert.equal(body.answers.length, 2);
198
+ assert.ok(!JSON.stringify(body).includes('Archived away'));
199
+ assert.ok(!JSON.stringify(body).includes('which page'), 'Domma\'s own words are not a customer theme');
200
+ assert.ok(body.answers.includes('Save button greyed. It never comes back after the first save.'));
201
+ const list = await call('GET', '/tickets');
202
+ assert.equal(list.body.themes.summary, 'Saving, mostly.');
203
+ assert.equal(list.body.themes.answers, 2);
204
+ });
205
+
206
+ test('deleting a request drops its kept summary, and the waiting requests\' themes when it was one of them', async () => {
207
+ useAiAssistLookup(() => true);
208
+ stubRegistry();
209
+ const read = async () => JSON.parse(await fs.readFile(path.join(dir, 'ai.json'), 'utf8'));
210
+ assert.ok((await read())[`summary:${ids.saving}`]);
211
+ assert.equal((await call('GET', '/tickets')).body.themes.ids, undefined, 'which requests it read stays on the server');
212
+ // The archived one was not summarised: the themes stay.
213
+ assert.equal((await call('DELETE', `/tickets/${ids.old}`)).status, 200);
214
+ assert.ok((await read()).themes);
215
+ assert.equal((await call('DELETE', `/tickets/${ids.saving}`)).status, 200);
216
+ const after = await read();
217
+ assert.equal(after[`summary:${ids.saving}`], undefined);
218
+ assert.equal(after.themes, undefined);
219
+ assert.equal((await call('GET', '/tickets')).body.themes, null);
220
+ });
221
+
222
+ test('the cards and the question', () => {
223
+ const tooShort = summaryCard(null, {messages: 1, chars: 120});
224
+ assert.equal(tooShort.canSummarise, false);
225
+ assert.match(tooShort.note, /Too short to summarise/);
226
+ assert.equal(summaryCard(null, {chars: 200}).canSummarise, true);
227
+ assert.equal(summaryCard(null, {chars: 500, busy: 'reply'}).canSummarise, false);
228
+ const none = summaryCard(null, {messages: 2});
229
+ assert.equal(none.label, 'Summarise');
230
+ assert.equal(none.suggestLabel, 'Suggest a reply');
231
+ assert.equal(summaryCard(null, {busy: 'summary'}).label, 'Summarising...');
232
+ assert.equal(summaryCard(null, {busy: 'reply'}).suggestLabel, 'Suggesting...');
233
+ assert.equal(summaryCard(null, {busy: 'reply'}).busy, true);
234
+ const kept = summaryCard({at: '2026-11-16T12:00:00Z', messages: 2, summary: 'S', points: ['a', 'b']}, {messages: 3});
235
+ assert.equal(kept.label, 'Summarise again');
236
+ assert.deepEqual(kept.points, [{key: '0', text: 'a'}, {key: '1', text: 'b'}]);
237
+ assert.match(kept.note, /^AI summary of 2 messages, .*\. The conversation has moved on since\.$/);
238
+ assert.ok(!summaryCard({messages: 3, summary: 'S'}, {messages: 3}).note.includes('moved on'));
239
+ assert.equal(themesCard(null, {waiting: 1}).canSummarise, false);
240
+ assert.equal(themesCard(null, {waiting: 2}).canSummarise, true);
241
+ assert.equal(themesCard({answers: 2, themes: [{title: 't', count: 1, quotes: ['q']}]}, {waiting: 2}).themes[0].countText, '1 request');
242
+ assert.match(AI_QUESTION, /no site names, people's names, email addresses, phone numbers or files/);
243
+ assert.match(AI_QUESTION, /nothing is sent until you send it/);
244
+ assert.equal(usageText({used: 4, limit: 500}, 'Summarised.'), 'Summarised. 4 of 500 AI Assist requests used this month.');
245
+ });
@@ -14,6 +14,9 @@
14
14
  * DELETE /staged/:id/uploads drop that zip
15
15
  * POST /staged/:id/widgets the widgets, as a .wie file (the "Widget Importer & Exporter" plugin's) - multipart, field "file"
16
16
  * DELETE /staged/:id/widgets drop that file
17
+ * POST /staged/:id/redirects the old site's own redirects, as a list: a file (multipart, field "file" - .csv, .tsv,
18
+ * .txt, .json, .htaccess) or pasted ({text}); services/wpImport/redirects.js reads it
19
+ * DELETE /staged/:id/redirects drop that list
17
20
  * POST /staged/:id/preview {options} -> the plan; nothing is written
18
21
  * POST /staged/:id/run {options} -> a background job
19
22
  * DELETE /staged/:id discard an upload
@@ -39,9 +42,10 @@ import {adminOrigin} from '../../services/passwordReset.js';
39
42
  import {getConfig} from '../../config.js';
40
43
  import {
41
44
  blogTarget, shopTarget, cleanOptions, createStaging, discardStaged, getJob, inspectStaged, previewImport, runningJob, stagingMeta,
42
- stagingPath, startRun, updateStagingMeta, UPLOADS_ZIP_MAX_BYTES, WIE_MAX_BYTES, widgetsMeta, WXR_MAX_BYTES, zipRel
45
+ redirectsMeta, REDIRECT_LIST_MAX_BYTES, stagingPath, startRun, updateStagingMeta, UPLOADS_ZIP_MAX_BYTES, WIE_MAX_BYTES, widgetsMeta, WXR_MAX_BYTES, zipRel
43
46
  } from '../../services/wpImport/index.js';
44
47
  import {readWie} from '../../services/wpImport/widgets.js';
48
+ import {parseRedirectList} from '../../services/wpImport/redirects.js';
45
49
  import {crawlSite, CrawlError, startUrl} from '../../services/wpImport/crawl.js';
46
50
  import {getReport, isId, listHistory, listStaging, removeHistory} from '../../services/wpImport/store.js';
47
51
  import {openZip} from '../../services/wpImport/zip.js';
@@ -185,7 +189,7 @@ export function planForScreen(plan, blog, shop = {}) {
185
189
  ...(r.front && {front: true}), ...(r.moveFrom && {moveFrom: r.moveFrom}),
186
190
  ...(r.homeLatest && {homeLatest: true}), ...(r.postsPage && {postsPage: true}), ...(r.postsIndex && {postsIndex: true})
187
191
  })),
188
- redirects: plan.redirects.map(r => ({from: r.from, to: r.to, action: r.action, reason: r.reason || ''}))
192
+ redirects: plan.redirects.map(r => ({from: r.from, to: r.to, action: r.action, reason: r.reason || '', ...(r.old && {old: true, code: r.code || 301}), ...(r.unread && {unread: true})}))
189
193
  };
190
194
  }
191
195
 
@@ -209,7 +213,7 @@ export async function wordpressImportRoutes(fastify, opts = {}) {
209
213
  blog: {available: blog.available, basePath: blog.basePath, comments: Boolean(blog.comments), reason: blog.reason || ''},
210
214
  redirects: Boolean(seoEngine()),
211
215
  people: await peopleContext(request.user),
212
- limits: {wxr: WXR_MAX_BYTES, uploads: UPLOADS_ZIP_MAX_BYTES, widgets: WIE_MAX_BYTES}
216
+ limits: {wxr: WXR_MAX_BYTES, uploads: UPLOADS_ZIP_MAX_BYTES, widgets: WIE_MAX_BYTES, redirects: REDIRECT_LIST_MAX_BYTES}
213
217
  };
214
218
  });
215
219
 
@@ -345,6 +349,59 @@ export async function wordpressImportRoutes(fastify, opts = {}) {
345
349
  return updateStagingMeta(id, {widgets: null});
346
350
  });
347
351
 
352
+ fastify.post('/staged/:id/redirects', {...may, bodyLimit: REDIRECT_LIST_MAX_BYTES + 65536}, async (request, reply) => {
353
+ const {id} = request.params;
354
+ const meta = isId(id) ? await stagingMeta(id) : null;
355
+ if (!meta) return reply.code(404).send({error: 'That upload has gone - upload the export again.'});
356
+ if (runningJob()?.staged === id) return reply.code(409).send({error: 'That import is running.'});
357
+ const dest = stagingPath(id, 'redirects.txt');
358
+ const tooBig = () => reply.code(413).send({error: `The list is over ${mb(REDIRECT_LIST_MAX_BYTES)} - that is far bigger than any real one.`});
359
+ try {
360
+ let name = 'Pasted list';
361
+ let size = 0;
362
+ let text;
363
+ if (request.isMultipart?.()) {
364
+ const file = await request.file({limits: {fileSize: REDIRECT_LIST_MAX_BYTES + 1, files: 1}}).catch(() => null);
365
+ if (!file) return reply.code(400).send({error: 'Choose the redirects file.'});
366
+ name = String(file.filename || '');
367
+ if (!/(\.(csv|tsv|txt|json|conf)|htaccess)$/i.test(name)) {
368
+ file.file.resume();
369
+ return reply.code(400).send({error: 'Upload the redirects as a .csv, .txt, .json or .htaccess file - or paste the list.'});
370
+ }
371
+ const tmp = `${dest}.new`;
372
+ try {
373
+ size = await saveUpload(file.file, tmp, REDIRECT_LIST_MAX_BYTES);
374
+ text = await fs.promises.readFile(tmp, 'utf8');
375
+ } finally {
376
+ fs.rmSync(tmp, {force: true});
377
+ }
378
+ } else {
379
+ text = typeof request.body?.text === 'string' ? request.body.text : '';
380
+ size = Buffer.byteLength(text);
381
+ if (size > REDIRECT_LIST_MAX_BYTES) return tooBig();
382
+ }
383
+ if (text.includes('\u0000')) return reply.code(400).send({error: 'That is not a text file.'});
384
+ const read = parseRedirectList(text);
385
+ if (!read.rows.length) {
386
+ const why = read.skipped[0]?.reason;
387
+ return reply.code(400).send({error: `No redirects could be read from that${why ? ` - the first line it stopped at: ${why}` : '. Each line needs the old address and the new one: /old-page -> /new-page 301, or a sheet with the two in columns.'}`});
388
+ }
389
+ await fs.promises.writeFile(dest, text, {mode: 0o600});
390
+ return await updateStagingMeta(id, {redirects: redirectsMeta(name, size, read)});
391
+ } catch (err) {
392
+ if (err.code === 'ETOOBIG') return tooBig();
393
+ return fail(reply, err);
394
+ }
395
+ });
396
+
397
+ fastify.delete('/staged/:id/redirects', may, async (request, reply) => {
398
+ const {id} = request.params;
399
+ if (!isId(id) || !(await stagingMeta(id))) return reply.code(404).send({error: 'That upload has gone.'});
400
+ if (runningJob()?.staged === id) return reply.code(409).send({error: 'That import is running.'});
401
+ fs.rmSync(stagingPath(id, 'redirects.txt'), {force: true});
402
+ return updateStagingMeta(id, {redirects: null});
403
+ });
404
+
348
405
  fastify.post('/staged/:id/preview', may, async (request, reply) => {
349
406
  const {id} = request.params;
350
407
  if (!isId(id)) return reply.code(404).send({error: 'That upload has gone.'});
@@ -453,6 +453,7 @@ export async function crawlSite(raw, opts = {}) {
453
453
 
454
454
  const read = new Map();
455
455
  read.set([...pages.keys()][0], homePage);
456
+ const moves = [];
456
457
  const failed = [];
457
458
  const queue = [...pages.keys()].slice(1);
458
459
  let capped = false;
@@ -470,6 +471,8 @@ export async function crawlSite(raw, opts = {}) {
470
471
  if (!res.ok) throw new Error(`answered ${res.status}`);
471
472
  if (!/html/i.test(String(res.headers['content-type'] || 'text/html'))) throw new Error('not a web page');
472
473
  const finalUrl = pageAddress(res.url, res.url, site) || url;
474
+ // The old site sent this address on to another of its own: one of its redirects.
475
+ if (finalUrl !== url && moves.length < 500) moves.push({from: url, to: finalUrl});
473
476
  if (read.has(finalUrl)) continue;
474
477
  const page = readPage(res.text(), finalUrl, site, pages.get(url) || {});
475
478
  read.set(finalUrl, page);
@@ -489,7 +492,7 @@ export async function crawlSite(raw, opts = {}) {
489
492
 
490
493
  progress.phase = 'Writing the export';
491
494
  tell();
492
- const xml = toWxr({site, homeUrl, homePage, pages: [...read.values()], builder});
495
+ const xml = toWxr({site, homeUrl, homePage, pages: [...read.values()], builder, moves});
493
496
  const all = [...read.values()];
494
497
  return {
495
498
  xml,
@@ -497,7 +500,7 @@ export async function crawlSite(raw, opts = {}) {
497
500
  url: homeUrl, builder: builder || 'website', siteName: homePage.siteName || homePage.title,
498
501
  pages: all.filter(p => p.kind === 'page').length, posts: all.filter(p => p.kind === 'post').length,
499
502
  files: new Set(all.flatMap(p => p.files)).size, menu: homePage.nav?.length || 0,
500
- sitemap: usedSitemap, capped, failed: failed.slice(0, 100), failedCount: failed.length,
503
+ redirects: moves.length, sitemap: usedSitemap, capped, failed: failed.slice(0, 100), failedCount: failed.length,
501
504
  thin: all.filter(p => p.html.replace(/<[^>]+>/g, '').trim().length < 40).map(p => p.url).slice(0, 100)
502
505
  }
503
506
  };
@@ -522,7 +525,7 @@ const slugOf = (url, site) => {
522
525
  };
523
526
 
524
527
  /** The pages read, as a WXR 1.2 document ./wxr.js reads like any other. */
525
- export function toWxr({site, homeUrl, homePage, pages, builder}) {
528
+ export function toWxr({site, homeUrl, homePage, pages, builder, moves = []}) {
526
529
  const now = wpDate(new Date().toISOString());
527
530
  const items = [];
528
531
  const ids = new Set();
@@ -556,6 +559,12 @@ export function toWxr({site, homeUrl, homePage, pages, builder}) {
556
559
  const name = decodeURIComponent(new URL(url).pathname.split('/').pop() || 'file');
557
560
  items.push(itemXml({id: idFor(`file:${url}`), type: 'attachment', title: f.alt || name.replace(/\.[a-z0-9]+$/i, ''), metas: f.alt ? {_wp_attachment_image_alt: f.alt} : {}, slug: '', link: url, date: f.date, creator: '', parent: f.parent, attachmentUrl: url, status: 'inherit'}));
558
561
  }
562
+ // The redirects met on the way, kept the way ./redirects.js reads them from an export (as redirect_rule items).
563
+ for (const m of moves) {
564
+ const from = new URL(m.from).pathname;
565
+ items.push(itemXml({id: idFor(`redirect:${from}`), type: 'redirect_rule', title: from, slug: '', link: '', date: now, creator: '',
566
+ metas: {_redirect_rule_from: from, _redirect_rule_to: m.to, _redirect_rule_status_code: '301'}}));
567
+ }
559
568
  // The menu.
560
569
  let menuXml = '';
561
570
  if (homePage.nav?.length) {
@@ -38,6 +38,7 @@ export const INTERNAL_TYPES = [
38
38
  {match: 'oembed_cache', reason: 'WordPress\'s cache of embedded videos and posts.'},
39
39
  {match: 'user_request', reason: 'Privacy requests - they hold people\'s personal details.'},
40
40
  {match: /^acf-(field-group|field|post-type|taxonomy|ui-options-page)$/, reason: 'Advanced Custom Fields settings - the field values come across with the content that uses them.'},
41
+ {match: 'redirect_rule', reason: 'The old site\'s own redirects - they come across as redirects, in SEO › Redirects.'},
41
42
  {match: 'wpcf7_contact_form', reason: 'Contact Form 7 forms - rebuild them with Forms.'},
42
43
  {match: /^(wpforms|wpforms_log|nf_sub|frm_form_actions|frm_styles|mc4wp-form|flamingo_\w+|feedback)$/, reason: 'A forms plugin\'s forms or submissions - submissions hold people\'s details; rebuild forms with Forms.'},
43
44
  {match: /^(elementor_library|elementor_\w+|e-landing-page|et_pb_layout|fl-builder-template|vc_grid_item|templatera|ct_template|oxy_user_library|brizy_template)$/, reason: 'A page builder\'s saved templates - layout, not content.'},
@@ -88,6 +88,7 @@ import {getConfig, saveConfig} from '../../config.js';
88
88
  import * as cache from '../cache/index.js';
89
89
  import {getSearchSettings} from '../search.js';
90
90
  import {readWie, widgetAreaHtml, WIE_MAX_BYTES} from './widgets.js';
91
+ import {parseRedirectList, REDIRECT_LIST_MAX_BYTES} from './redirects.js';
91
92
  import {FOOTER_BLOCKS_MAX} from '../renderer.js';
92
93
  import {allowInGitignore} from '../siteGitignore.js';
93
94
  import fsSync from 'node:fs';
@@ -95,7 +96,7 @@ import {answeredElsewhere} from '../hostClaims.js';
95
96
 
96
97
  export const WXR_MAX_BYTES = 50 * 1024 * 1024;
97
98
  export const UPLOADS_ZIP_MAX_BYTES = 1024 * 1024 * 1024;
98
- export {WIE_MAX_BYTES};
99
+ export {WIE_MAX_BYTES, REDIRECT_LIST_MAX_BYTES};
99
100
  const DOWNLOAD_CONCURRENCY = 4;
100
101
 
101
102
  const BLOG = {posts: 'blog-posts', categories: 'blog-categories', comments: 'blog-comments'};
@@ -387,6 +388,23 @@ export async function loadWidgets(id) {
387
388
  return readWie(await fs.readFile(stagingPath(id, 'widgets.wie')));
388
389
  }
389
390
 
391
+ /** What the staging meta says about a redirects list. */
392
+ export function redirectsMeta(fileName, size, read) {
393
+ return {
394
+ fileName: String(fileName || '').slice(0, 200).replace(/[^\w.() -]/g, '_'), size,
395
+ format: read.format, count: read.rows.length, unread: read.skipped.length, more: read.more || 0
396
+ };
397
+ }
398
+
399
+ /** The staged redirects list (the old site's own redirects), read, or null when there is none. */
400
+ export async function loadRedirectList(id) {
401
+ const meta = await stagingMeta(id);
402
+ if (!meta?.redirects) return null;
403
+ try {
404
+ return parseRedirectList(await fs.readFile(stagingPath(id, 'redirects.txt'), 'utf8'));
405
+ } catch { return null; }
406
+ }
407
+
390
408
  export async function discardStaged(id) {
391
409
  parsed.delete(id);
392
410
  await dropStaging(id);
@@ -435,8 +453,9 @@ export async function previewImport(id, options = {}) {
435
453
  const state = await siteState(mapping, sourceKey(wxr.site));
436
454
  const zipPaths = options.media === 'zip' ? await uploadsIndex(id) : null;
437
455
  const wie = await loadWidgets(id);
456
+ const oldRedirects = await loadRedirectList(id);
438
457
  // `people` (what the person may do with users) comes from the route, never from the request body.
439
- const plan = planImport(wxr, state, mapping, {...cleanOptions(options), people: options.people || null, zipPaths, wie, anyPort: testHostsAllowed()});
458
+ const plan = planImport(wxr, state, mapping, {...cleanOptions(options), people: options.people || null, zipPaths, wie, oldRedirects, anyPort: testHostsAllowed()});
440
459
  plan.notes.unshift(...platformNotes(wxr, meta));
441
460
  return {plan, state, wxr, mapping, meta, wie};
442
461
  }
@@ -1126,8 +1145,9 @@ async function runPhases(job, {plan, wxr, oldMapping, meta, inviteOrigin, wie},
1126
1145
  const own = mapping.redirects[r.key];
1127
1146
  const rule = own && seo.store.rules().find(x => x.id === own.id);
1128
1147
  let out;
1129
- if (rule) out = seo.updateRule(rule.id, {from: r.from, to: r.to, code: 301});
1130
- else out = seo.addRule({from: r.from, to: r.to, code: 301, note: `${sourceName(wxr, true)} import`});
1148
+ const code = r.old && r.code ? r.code : 301;
1149
+ if (rule) out = seo.updateRule(rule.id, {from: r.from, to: r.to, code});
1150
+ else out = seo.addRule({from: r.from, to: r.to, code, note: r.old ? `${sourceName(wxr, true)} import - a redirect the old site had` : `${sourceName(wxr, true)} import`});
1131
1151
  if (out.errors) throw new Error(Object.values(out.errors).join(' '));
1132
1152
  mapping.redirects[r.key] = {id: out.rule.id, from: out.rule.from, to: out.rule.to};
1133
1153
  redirectResults.push({...r, result: rule ? 'updated' : 'created'});