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.
@@ -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
+ });