domma-cms 1.49.1 → 1.50.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 (38) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/admin/js/lib/chats-arrange.js +2 -2
  3. package/admin/js/lib/launchers-arrange.js +5 -2
  4. package/admin/js/lib/sidebar-renderer.js +1 -1
  5. package/admin/js/lib/site-settings-arrange.js +2 -2
  6. package/admin/js/templates/chats.html +26 -2
  7. package/admin/js/templates/docs/usage-chat.html +51 -5
  8. package/admin/js/templates/launchers.html +4 -0
  9. package/admin/js/templates/settings.html +18 -0
  10. package/admin/js/views/chats.js +5 -5
  11. package/admin/js/views/launchers.js +105 -33
  12. package/admin/js/views/layouts.js +1 -1
  13. package/admin/js/views/menu-editor.js +1 -1
  14. package/docs/DommaFeatures.md +4 -0
  15. package/package.json +1 -1
  16. package/public/css/launcher-chat.css +1 -1
  17. package/public/css/launchers.css +1 -1
  18. package/public/js/launcher-chat.mjs +16 -10
  19. package/public/js/launcher-settings.mjs +6 -2
  20. package/public/js/launchers.mjs +3 -3
  21. package/public/js/menu-cards.mjs +1 -1
  22. package/public/js/menu-decor.mjs +1 -1
  23. package/public/js/site.js +1 -1
  24. package/server/routes/api/chat.js +4 -2
  25. package/server/routes/api/chats.js +37 -0
  26. package/server/routes/api/settings.js +11 -1
  27. package/server/services/assistant.js +67 -0
  28. package/server/services/chat/ai.js +109 -0
  29. package/server/services/chat/conversation.js +37 -2
  30. package/server/services/chat/saved.js +117 -0
  31. package/server/services/chat/settings.js +30 -4
  32. package/server/services/chat/store.js +1 -1
  33. package/server/services/collectionAccess.js +2 -1
  34. package/server/services/launchers.js +1 -1
  35. package/server/services/menuRender.js +6 -5
  36. package/server/services/presetCollections.js +24 -2
  37. package/server/services/siteGitignore.js +1 -1
  38. package/server/templates/page.html +1 -1
@@ -22,6 +22,7 @@
22
22
  */
23
23
  import {chatLauncher} from '../../services/chat/launcher-bridge.js';
24
24
  import {readChatSettings} from '../../services/chat/settings.js';
25
+ import {aiAvailable} from '../../services/chat/ai.js';
25
26
  import {
26
27
  createSession, getSession, knowledgeIndex, messagesAfter, sessionByToken, staffAvailable
27
28
  } from '../../services/chat/store.js';
@@ -117,8 +118,9 @@ export async function chatPublicRoutes(fastify) {
117
118
  fastify.get('/config', limit(60), async (request, reply) => {
118
119
  const launcher = await launcherOf(request, reply);
119
120
  if (!launcher) return;
120
- const {greeting, botName, bot, staff, askDetails} = launcher.chat;
121
- return {greeting, botName, bot, staff, askDetails, staffOnline: staff && staffAvailable().length > 0};
121
+ const {greeting, botName, bot, staff, askDetails, accent, accentText, avatar, starters, personButton, times} = launcher.chat;
122
+ return {greeting, botName, bot, staff, askDetails, accent, accentText, avatar, starters, personButton, times, ai: bot && launcher.chat.ai && aiAvailable(),
123
+ staffOnline: staff && staffAvailable().length > 0};
122
124
  });
123
125
 
124
126
  fastify.get('/state', limit(90), async (request, reply) => {
@@ -10,16 +10,21 @@
10
10
  * POST /api/chats/knowledge/test - {text}: what the bot would answer, with scores
11
11
  * GET /api/chats/unanswered - questions the bot could not answer
12
12
  * GET /api/chats/settings - site-wide chat settings
13
+ * GET /api/chats/saved - chats saved for a closer look
14
+ * GET /api/chats/saved/:sid - one, with its messages
13
15
  * chats.reply:
14
16
  * POST /api/chats/presence - {available}: "I can take chats" (sent every 30s by the inbox)
15
17
  * POST /api/chats/:id/reply - {text}
16
18
  * POST /api/chats/:id/close
19
+ * POST /api/chats/:id/save - {note}: keep a copy in Saved Chats (the chat-saved collection)
20
+ * PUT /api/chats/saved/:sid - {note?, outcome?}
17
21
  * chats.manage:
18
22
  * DELETE /api/chats/:id
19
23
  * POST /api/chats/knowledge, PUT/DELETE /api/chats/knowledge/:id
20
24
  * POST /api/chats/knowledge/remove-examples
21
25
  * POST /api/chats/unanswered/:id/done {done}, DELETE /api/chats/unanswered/:id
22
26
  * PUT /api/chats/settings
27
+ * DELETE /api/chats/saved/:sid
23
28
  */
24
29
  import {authenticate as defaultAuthenticate, requirePermission as defaultRequirePermission} from '../../middleware/auth.js';
25
30
  import {ago, clip, plural} from '../../services/sidebarBadges.js';
@@ -34,6 +39,7 @@ import {
34
39
  } from '../../services/chat/conversation.js';
35
40
  import {CHAT_DEFAULTS, readChatSettings, saveChatSettings} from '../../services/chat/settings.js';
36
41
  import {chatLauncherSummary} from '../../services/chat/launcher-bridge.js';
42
+ import {getSaved, listSaved, removeSaved, saveChat, updateSaved} from '../../services/chat/saved.js';
37
43
  import {CONFIDENCE} from '../../services/chat/matcher.js';
38
44
 
39
45
  const ID = /^[A-Za-z0-9_-]{1,64}$/;
@@ -187,6 +193,28 @@ export async function chatsRoutes(fastify, opts = {}) {
187
193
 
188
194
  fastify.put('/settings', can('manage'), async (request) => ({settings: saveChatSettings(request.body || {})}));
189
195
 
196
+ // ---- Saved chats -----------------------------------------------------------
197
+
198
+ fastify.get('/saved', can('read'), async () => ({items: await listSaved()}));
199
+
200
+ fastify.get('/saved/:sid', can('read'), async (request, reply) => {
201
+ const item = ID.test(request.params.sid) ? await getSaved(request.params.sid) : null;
202
+ return item ? {item} : reply.code(404).send({error: 'Saved chat not found'});
203
+ });
204
+
205
+ fastify.put('/saved/:sid', can('reply'), async (request, reply) => {
206
+ if (!ID.test(request.params.sid)) return reply.code(404).send({error: 'Saved chat not found'});
207
+ const {item, error, missing} = await updateSaved(request.params.sid, request.body || {});
208
+ if (missing) return reply.code(404).send({error: 'Saved chat not found'});
209
+ if (error) return reply.code(400).send({error});
210
+ return {item};
211
+ });
212
+
213
+ fastify.delete('/saved/:sid', can('manage'), async (request, reply) => {
214
+ if (!ID.test(request.params.sid) || !await removeSaved(request.params.sid)) return reply.code(404).send({error: 'Saved chat not found'});
215
+ return {ok: true};
216
+ });
217
+
190
218
  // ---- One chat (after the static paths) -----------------------------------
191
219
 
192
220
  fastify.get('/:id', can('read'), async (request, reply) => {
@@ -224,6 +252,15 @@ export async function chatsRoutes(fastify, opts = {}) {
224
252
  return {chat: staffSummary(result.session)};
225
253
  });
226
254
 
255
+ fastify.post('/:id/save', can('reply'), async (request, reply) => {
256
+ const {id} = request.params;
257
+ if (!ID.test(id)) return reply.code(404).send({error: 'Chat not found'});
258
+ const user = {id: userId(request), name: request.user?.name || request.user?.username || ''};
259
+ const result = await saveChat(id, request.body || {}, user);
260
+ if (result.error) return reply.code(result.code || 400).send({error: result.error});
261
+ return reply.code(201).send({item: result.item});
262
+ });
263
+
227
264
  fastify.delete('/:id', can('manage'), async (request, reply) => {
228
265
  const {id} = request.params;
229
266
  if (!ID.test(id)) return reply.code(404).send({error: 'Chat not found'});
@@ -22,6 +22,7 @@ import {normaliseAdminPath} from '../../services/roles.js';
22
22
  import {cssClassProblems} from '../../../public/js/css-classes.mjs';
23
23
  import {motionFooterMirror, normaliseMotionSettings} from '../../services/motionSettings.js';
24
24
  import {invalidateMeasurementId, measurementIdOf, normaliseMeasurementId} from '../../services/googleAnalytics.js';
25
+ import {assistantProblem, normaliseAssistant} from '../../services/assistant.js';
25
26
 
26
27
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
27
28
  const CUSTOM_CSS_FILE = path.resolve(__dirname, '../../../content/custom.css');
@@ -127,7 +128,9 @@ export async function settingsRoutes(fastify) {
127
128
  // Google Analytics (Site Settings > Analytics) - services/googleAnalytics.js.
128
129
  'googleAnalytics',
129
130
  // The "Reduce motion" control (Site Settings > Reduce motion) - services/motionSettings.js.
130
- 'motion'
131
+ 'motion',
132
+ // What every AI answer knows about the site and the rules it keeps (services/assistant.js).
133
+ 'assistant'
131
134
  ]);
132
135
  const unknownKeys = Object.keys(data).filter(k => !ALLOWED_KEYS.has(k));
133
136
  if (unknownKeys.length > 0) {
@@ -167,6 +170,13 @@ export async function settingsRoutes(fastify) {
167
170
  if (raw && !id) return reply.status(400).send({error: 'Google Analytics: a measurement ID looks like G-XXXXXXXXXX.'});
168
171
  data.googleAnalytics = {measurementId: id};
169
172
  }
173
+ if ('assistant' in data) {
174
+ const a = data.assistant;
175
+ if (!a || typeof a !== 'object' || Array.isArray(a)) return reply.status(400).send({error: 'AI assistant: send {context, rules}.'});
176
+ const problem = assistantProblem(a);
177
+ if (problem) return reply.status(400).send({error: `AI assistant: ${problem}`});
178
+ data.assistant = normaliseAssistant(a);
179
+ }
170
180
  if ('adminHome' in data) {
171
181
  // An admin route, or blank for the built-in dashboard.
172
182
  const home = data.adminHome == null || data.adminHome === '' ? '/' : normaliseAdminPath(data.adminHome);
@@ -0,0 +1,67 @@
1
+ /**
2
+ * What every AI answer on a site knows and keeps to - one place for the Launcher chat bot (Della) and any
3
+ * AI feature that talks to visitors after it.
4
+ *
5
+ * Site-wide (site.json `assistant`, Site Settings > General > AI assistant):
6
+ * context facts about the business in the owner's words - what you do, where, hours, prices, tone
7
+ * rules what an answer must never do or say - "never quote prices", "do not discuss competitors"
8
+ * Per use (a chat launcher's `chat.aiContext` / `chat.aiRules`): the same two, for that launcher only,
9
+ * added after the site's own. A launcher's rules add to the site's; they never lift one. A launcher may leave
10
+ * out the site's CONTEXT (`chat.aiUseSite: false`, for a chat about something else), but never its rules:
11
+ * the site's rules apply to every AI answer on the site, and nothing in a launcher or a chat can override them.
12
+ *
13
+ * Both are the owner's instructions, so they go to Claude as instructions (the system prompt), while what a
14
+ * visitor types goes as the visitor's message and is never obeyed as an instruction.
15
+ *
16
+ * @module services/assistant
17
+ */
18
+ import {getConfig} from '../config.js';
19
+
20
+ export const CONTEXT_MAX = 4000;
21
+ export const RULES_MAX = 2000;
22
+
23
+ const clean = (v, max) => String(v ?? '').replace(/\r\n?/g, '\n')
24
+ // eslint-disable-next-line no-control-regex
25
+ .replace(/[\u0000-\u0008\u000b-\u001f\u007f]/g, '').replace(/\n{3,}/g, '\n\n').trim().slice(0, max);
26
+
27
+ /** What is wrong with an `assistant` block as sent, or ''. */
28
+ export function assistantProblem(a = {}) {
29
+ for (const [key, max, label] of [['context', CONTEXT_MAX, 'About the business'], ['rules', RULES_MAX, 'Rules']]) {
30
+ if (a[key] !== undefined && a[key] !== null && typeof a[key] !== 'string') return `${label} must be text.`;
31
+ if (String(a[key] ?? '').length > max) return `${label} is limited to ${max} characters.`;
32
+ }
33
+ return '';
34
+ }
35
+
36
+ /** The block as stored. */
37
+ export function normaliseAssistant(a = {}) {
38
+ const r = a && typeof a === 'object' && !Array.isArray(a) ? a : {};
39
+ return {context: clean(r.context, CONTEXT_MAX), rules: clean(r.rules, RULES_MAX)};
40
+ }
41
+
42
+ /** The site's own block, read now (never cached: Site Settings saves take effect on the next answer). */
43
+ export function siteAssistant() {
44
+ try {
45
+ const site = getConfig('site') || {};
46
+ return {...normaliseAssistant(site.assistant), name: clean(site.title, 120), tagline: clean(site.tagline, 200)};
47
+ } catch {
48
+ return {context: '', rules: '', name: '', tagline: ''};
49
+ }
50
+ }
51
+
52
+ /**
53
+ * The context and rules for one use: the site's, then the launcher's.
54
+ *
55
+ * @param {{aiContext?: string, aiRules?: string, aiUseSite?: boolean}} [extra]
56
+ * @returns {{site: {name: string, tagline: string}, context: string, rules: string}}
57
+ */
58
+ export function assistantBrief(extra = {}) {
59
+ const all = siteAssistant();
60
+ const s = extra.aiUseSite === false ? {...all, context: ''} : all;
61
+ const more = normaliseAssistant({context: extra.aiContext, rules: extra.aiRules});
62
+ return {
63
+ site: {name: s.name, tagline: s.tagline},
64
+ context: [s.context, more.context].filter(Boolean).join('\n\n'),
65
+ rules: [s.rules, more.rules].filter(Boolean).join('\n')
66
+ };
67
+ }
@@ -0,0 +1,109 @@
1
+ /**
2
+ * Della's AI answers - what the chat bot says when the Knowledge has no answer.
3
+ *
4
+ * Included in the Pro plan on hosted sites, so a site holds no Claude key: the question goes to the Manager
5
+ * that runs the site (MANAGER_URL + FLEET_TOKEN, POST /api/fleet/launcher-chat), which checks the licence and
6
+ * the month's allowance and asks Claude from a short-lived job that alone reads the key. On dcm, which is the
7
+ * Manager, the same code is reached in this process (globalThis[Symbol.for('domma.launcher-chat')]). A site
8
+ * with neither has no AI answers, and the bot answers from the Knowledge alone, as before.
9
+ *
10
+ * What Claude is given: the site's name, Site Settings > AI assistant and the launcher's own context and rules
11
+ * (services/assistant.js), the enabled Knowledge, the last few messages of this chat and the question. It
12
+ * answers only from those, or says it does not know - then the chat goes on as before ("Sorry, I do not
13
+ * know", the offer of a person).
14
+ *
15
+ * @module services/chat/ai
16
+ */
17
+ import {assistantBrief} from '../assistant.js';
18
+ import {knowledgeIndex, messagesAfter} from './store.js';
19
+
20
+ const REGISTRY = Symbol.for('domma.launcher-chat');
21
+ /** What a visitor will wait for an answer. */
22
+ const ASK_MS = 25_000;
23
+ /** Knowledge and chat sent at most: a reply is a few sentences, the prompt need not be a book. */
24
+ export const KNOWLEDGE_CHARS = 16_000;
25
+ export const HISTORY_MESSAGES = 10;
26
+ /** After the Manager says no (no licence, the month's answers used), ask again only after this. */
27
+ const BACKOFF_MS = {403: 10 * 60_000, 429: 30 * 60_000, other: 60_000};
28
+
29
+ let restUntil = 0;
30
+ export function _resetAi() { restUntil = 0; }
31
+
32
+ /** Whether this site has someone to ask. */
33
+ export function aiAvailable(env = process.env, at = Date.now()) {
34
+ if (at < restUntil) return false;
35
+ return Boolean(globalThis[REGISTRY] || (env.MANAGER_URL && env.FLEET_TOKEN));
36
+ }
37
+
38
+ const plain = (md) => String(md || '').replace(/!?\[([^\]]*)\]\([^)]*\)/g, '$1').replace(/[*_`#>]/g, '').replace(/\n{3,}/g, '\n\n').trim();
39
+
40
+ /** The enabled Knowledge as question and answer pairs, most used first, within KNOWLEDGE_CHARS. */
41
+ export function knowledgeForAi(entries) {
42
+ const out = [];
43
+ let used = 0;
44
+ const list = [...entries].filter(k => k.enabled !== false && !k.sample).sort((a, b) => (b.hits || 0) - (a.hits || 0));
45
+ for (const k of list) {
46
+ const pair = {q: [k.question, ...String(k.alternatives || '').split('\n')].map(s => s.trim()).filter(Boolean).join(' / '), a: plain(k.answer)};
47
+ const size = pair.q.length + pair.a.length;
48
+ if (used + size > KNOWLEDGE_CHARS) break;
49
+ used += size;
50
+ out.push(pair);
51
+ }
52
+ return out;
53
+ }
54
+
55
+ /** The chat before the question, as {from, text}: visitor and bot only (staff never answer while the bot does). */
56
+ export function historyForAi(messages, question) {
57
+ const rows = messages.filter(m => m.data?.from === 'visitor' || m.data?.from === 'bot')
58
+ .map(m => ({from: m.data.from, text: String(m.data.text || '').slice(0, 600)}));
59
+ // The question itself was written just before this asks: it goes separately.
60
+ if (rows.length && rows[rows.length - 1].from === 'visitor' && rows[rows.length - 1].text === question) rows.pop();
61
+ return rows.slice(-HISTORY_MESSAGES);
62
+ }
63
+
64
+ async function ask(payload, {env, fetchFn}) {
65
+ const local = globalThis[REGISTRY];
66
+ if (local) return local.answer(payload);
67
+ const res = await fetchFn(`${String(env.MANAGER_URL).replace(/\/+$/, '')}/api/fleet/launcher-chat`, {
68
+ method: 'POST',
69
+ headers: {authorization: `Bearer ${env.FLEET_TOKEN}`, 'content-type': 'application/json'},
70
+ body: JSON.stringify(payload),
71
+ signal: AbortSignal.timeout(ASK_MS)
72
+ });
73
+ return {status: res.status, body: await res.json().catch(() => ({}))};
74
+ }
75
+
76
+ /**
77
+ * Ask for an answer to `question` in chat `id`.
78
+ *
79
+ * @param {string} id - the chat
80
+ * @param {string} question - what the visitor just wrote
81
+ * @param {{botName: string, aiContext?: string, aiRules?: string}} chat - the launcher's chat block
82
+ * @returns {Promise<string>} the answer, or '' (no answer: carry on without)
83
+ */
84
+ export async function aiAnswer(id, question, chat, {env = process.env, fetchFn = fetch, log = console} = {}) {
85
+ if (!aiAvailable(env)) return '';
86
+ try {
87
+ const brief = assistantBrief(chat);
88
+ const {byId} = await knowledgeIndex();
89
+ const count = (await messagesAfter(id, 0)).slice(-(HISTORY_MESSAGES + 1));
90
+ const payload = {
91
+ botName: chat.botName,
92
+ site: {name: brief.site.name, description: brief.site.tagline},
93
+ context: brief.context,
94
+ rules: brief.rules,
95
+ knowledge: knowledgeForAi([...byId.values()]),
96
+ history: historyForAi(count, question),
97
+ question
98
+ };
99
+ const {status, body} = await ask(payload, {env, fetchFn});
100
+ if (status === 200) return typeof body?.answer === 'string' ? body.answer.trim() : '';
101
+ restUntil = Date.now() + (BACKOFF_MS[status] || BACKOFF_MS.other);
102
+ if (status !== 429) log.warn?.(`[chat] AI answers unavailable (${status}): ${String(body?.error || '').slice(0, 120)}`);
103
+ return '';
104
+ } catch (err) {
105
+ restUntil = Date.now() + BACKOFF_MS.other;
106
+ log.warn?.(`[chat] AI answer failed: ${err.message}`);
107
+ return '';
108
+ }
109
+ }
@@ -27,6 +27,7 @@ import {
27
27
  patchSession, removeSession, removeSessionsAndOrphans, renderAnswer, staffAvailable, withChat
28
28
  } from './store.js';
29
29
  import {readChatSettings, staffDisplayName} from './settings.js';
30
+ import {aiAnswer} from './ai.js';
30
31
 
31
32
  export const SOURCE = 'core:chat';
32
33
  const int = (v) => (Number.isFinite(Number(v)) ? Math.trunc(Number(v)) : 0);
@@ -100,6 +101,7 @@ export function publicMessage(m, chat, knowledge = new Map()) {
100
101
  out.text = String(d.text || '');
101
102
  }
102
103
  if (d.from === 'bot') out.name = chat.botName;
104
+ if (d.from === 'bot' && (d.ai === true || d.ai === 'true')) out.ai = true;
103
105
  if (d.from === 'staff') out.name = d.staffName || 'The team';
104
106
  if (d.from === 'bot' && Array.isArray(d.suggestions) && d.suggestions.length) {
105
107
  out.suggestions = d.suggestions.map((s, i) => ({label: String(s?.label || ''), i}));
@@ -139,6 +141,7 @@ export function staffMessage(m, knowledge = new Map()) {
139
141
  return {
140
142
  n: int(d.n), from: d.from, text: String(d.text || ''), at: m.meta?.createdAt || null,
141
143
  name: d.staffName || '', answer: d.answerId ? (knowledge.get(d.answerId)?.question || '(an entry since removed)') : '',
144
+ ai: d.ai === true || d.ai === 'true',
142
145
  suggestions: Array.isArray(d.suggestions) ? d.suggestions.map(s => String(s?.label || '')) : []
143
146
  };
144
147
  }
@@ -157,6 +160,18 @@ export function needsStaff(data) {
157
160
  // The bot
158
161
  // ---------------------------------------------------------------------------
159
162
 
163
+ /**
164
+ * An AI answer written to the chat, or null: AI off for this launcher, this chat's allowance used
165
+ * (`aiMaxPerChat`), or nothing to say. Inside withChat.
166
+ */
167
+ async function aiTurn(id, text, chat, session) {
168
+ if (!chat.ai) return null;
169
+ const used = int(session?.data.aiCount);
170
+ if (used >= int(chat.aiMaxPerChat ?? 20)) return null;
171
+ const said = await aiAnswer(id, text, chat);
172
+ return said ? addMessage(id, {from: 'bot', text: said, ai: true}, {misses: 0, aiCount: used + 1}) : null;
173
+ }
174
+
160
175
  /** The bot's reply to a visitor's words, written to the chat. Inside withChat. */
161
176
  async function botReply(id, text, chat, settings) {
162
177
  const {index, byId} = await knowledgeIndex();
@@ -170,10 +185,16 @@ async function botReply(id, text, chat, settings) {
170
185
  return addMessage(id, {from: 'bot', text: byId.get(r.best.id)?.answer || '', answerId: r.best.id}, {misses: 0});
171
186
  }
172
187
  if (r.kind === 'suggest') {
188
+ // A near miss: the AI (which sees the whole Knowledge) answers it if it can; the choices are the fallback.
189
+ const said = await aiTurn(id, text, chat, session);
190
+ if (said) return said;
173
191
  return addMessage(id, {from: 'bot', text: SAY.suggest,
174
192
  suggestions: r.suggestions.map(s => ({id: s.id, label: s.question}))});
175
193
  }
194
+ // Still logged when the AI answers: it is a gap in the Knowledge worth filling either way.
176
195
  await logUnanswered(text, r.best?.question || '');
196
+ const said = await aiTurn(id, text, chat, session);
197
+ if (said) return said;
177
198
  const misses = int(session?.data.misses) + 1;
178
199
  const out = await addMessage(id, {from: 'bot', text: chat.staff ? SAY.noneStaff : SAY.noneAlone}, {misses});
179
200
  if (chat.staff && misses >= 2) return addMessage(id, {from: 'bot', text: SAY.offerPerson});
@@ -416,8 +437,10 @@ export function staffDeletes(id) {
416
437
  */
417
438
  export const BOT_ONLY_DAYS = 7;
418
439
 
419
- export async function sweepChats({at = Date.now(), retentionDays = readChatSettings().retentionDays} = {}) {
440
+ export async function sweepChats({at = Date.now(), retentionDays = readChatSettings().retentionDays, keepFor = null} = {}) {
420
441
  const HOUR = 3_600_000;
442
+ // A chat launcher may keep its own chats for longer or shorter than the site (chat.keepDays; 0 = the site's).
443
+ const perLauncher = keepFor || await launcherKeepDays();
421
444
  let closed = 0;
422
445
  const doomed = [];
423
446
  for (const entry of await listSessions()) {
@@ -435,7 +458,9 @@ export async function sweepChats({at = Date.now(), retentionDays = readChatSetti
435
458
  forgetSession(entry.id);
436
459
  } else if (d.status === 'closed') {
437
460
  const botOnly = !d.assignedTo && !d.visitorEmail;
438
- const keepDays = botOnly ? Math.min(BOT_ONLY_DAYS, retentionDays) : retentionDays;
461
+ const own = perLauncher.get(String(d.launcherId || '')) || 0;
462
+ const days = own > 0 ? own : retentionDays;
463
+ const keepDays = botOnly ? Math.min(BOT_ONLY_DAYS, days) : days;
439
464
  if (quiet >= keepDays * 24 * HOUR) doomed.push(entry.id);
440
465
  else forgetSession(entry.id);
441
466
  }
@@ -444,6 +469,16 @@ export async function sweepChats({at = Date.now(), retentionDays = readChatSetti
444
469
  return {closed, deleted: removed.chats, messages: removed.messages};
445
470
  }
446
471
 
472
+ /** launcherId -> keepDays (only the launchers that set their own). */
473
+ async function launcherKeepDays() {
474
+ try {
475
+ const {chatLaunchers} = await import('../launchers.js');
476
+ return new Map((await chatLaunchers()).filter(l => Number(l.chat?.keepDays) > 0).map(l => [String(l.id), Number(l.chat.keepDays)]));
477
+ } catch {
478
+ return new Map();
479
+ }
480
+ }
481
+
447
482
  let sweepTimer = null;
448
483
 
449
484
  /** Run the tidy-up an hour after start, then hourly. */
@@ -0,0 +1,117 @@
1
+ /**
2
+ * Saved chats - a copy of a visitor chat kept for a closer look (the chat-saved
3
+ * preset). Staff save one from the Chats inbox with a note on why; the copy
4
+ * is the whole conversation as it stood, so it outlives the chat's own
5
+ * retention and its deletion, and carries an outcome staff move along
6
+ * (to look at -> looking into it -> done).
7
+ *
8
+ * @module services/chat/saved
9
+ */
10
+ import {createEntry, deleteEntry, getEntry, listEntries, updateEntry} from '../collections.js';
11
+ import {cleanText, getSession, knowledgeIndex, messagesAfter} from './store.js';
12
+ import {staffMessage, staffSummary} from './conversation.js';
13
+
14
+ export const SAVED_SLUG = 'chat-saved';
15
+ export const OUTCOMES = Object.freeze(['to look at', 'looking into it', 'done']);
16
+ export const NOTE_MAX = 2000;
17
+ /** Saved chats a site keeps at most: past it, the oldest that are done go first, then saving is refused. */
18
+ export const SAVED_MAX = 2000;
19
+
20
+ const WHO = {visitor: 'Visitor', bot: 'Bot', staff: 'Staff', system: 'Note'};
21
+
22
+ /** The conversation as plain text, one message a paragraph - what the Collections screen shows. */
23
+ export function transcriptOf(messages, chat = {}) {
24
+ return messages.map((m) => {
25
+ const at = m.at ? new Date(m.at).toISOString().slice(0, 16).replace('T', ' ') : '';
26
+ const who = m.from === 'visitor' ? (chat.name || WHO.visitor) : m.from === 'staff' ? (m.name || WHO.staff) : WHO[m.from] || m.from;
27
+ const answer = m.from === 'bot' && m.answer ? ` [Knowledge: ${m.answer}]` : m.from === 'bot' && m.ai ? ' [AI]' : '';
28
+ return `[${at}] ${who}: ${m.text}${answer}`;
29
+ }).join('\n\n');
30
+ }
31
+
32
+ /** A short title: who, and what they first asked. */
33
+ export function titleOf(chat) {
34
+ const who = chat.name || 'A visitor';
35
+ const about = chat.summary ? ` - ${chat.summary}` : '';
36
+ return `${who}${about}`.slice(0, 140);
37
+ }
38
+
39
+ /** One saved chat for the inbox list (no messages). */
40
+ export function savedSummary(entry) {
41
+ const d = entry.data || {};
42
+ return {
43
+ id: entry.id, title: d.title || '', note: d.note || '', outcome: OUTCOMES.includes(d.outcome) ? d.outcome : OUTCOMES[0],
44
+ name: d.visitorName || '', email: d.visitorEmail || '', page: d.page || '', chatId: d.chatId || '',
45
+ count: Number(d.messageCount) || 0, startedAt: d.startedAt || null, savedAt: d.savedAt || entry.meta?.createdAt || null,
46
+ savedBy: d.savedByName || '', updatedAt: entry.meta?.updatedAt || null
47
+ };
48
+ }
49
+
50
+ /** One saved chat with its messages. */
51
+ export function savedDetail(entry) {
52
+ const d = entry.data || {};
53
+ return {...savedSummary(entry), messages: Array.isArray(d.messages) ? d.messages : []};
54
+ }
55
+
56
+ /**
57
+ * Save a copy of chat `id`.
58
+ *
59
+ * @param {string} id
60
+ * @param {{note?: string}} body
61
+ * @param {{id?: string, name?: string}} user
62
+ * @returns {Promise<{item?: object, error?: string, code?: number}>}
63
+ */
64
+ export async function saveChat(id, body = {}, user = {}) {
65
+ const session = await getSession(id, {fresh: true});
66
+ if (!session) return {error: 'Chat not found', code: 404};
67
+ const {total} = await listEntries(SAVED_SLUG, {limit: 1});
68
+ if (total >= SAVED_MAX) return {error: `There are ${SAVED_MAX} saved chats already. Delete some you have finished with first.`, code: 409};
69
+ const {byId} = await knowledgeIndex();
70
+ const messages = (await messagesAfter(id, 0)).map(m => staffMessage(m, byId))
71
+ .map(({n, from, text, at, name, answer, ai}) => ({n, from, text, at, ...(name && {name}), ...(answer && {answer}), ...(ai && {ai: true})}));
72
+ const chat = staffSummary(session);
73
+ const at = new Date().toISOString();
74
+ const entry = await createEntry(SAVED_SLUG, {
75
+ title: titleOf(chat),
76
+ note: cleanText(body.note, NOTE_MAX),
77
+ outcome: OUTCOMES[0],
78
+ chatId: chat.id, launcherId: chat.launcherId, chatStatus: chat.status,
79
+ visitorName: chat.name, visitorEmail: chat.email, page: chat.page,
80
+ startedAt: chat.startedAt, lastAt: chat.lastAt, savedAt: at,
81
+ savedBy: String(user.id || ''), savedByName: String(user.name || '').slice(0, 80),
82
+ messageCount: messages.length,
83
+ transcript: transcriptOf(messages, chat),
84
+ messages
85
+ }, {createdBy: user.id || null, source: 'chat'});
86
+ return {item: savedSummary(entry)};
87
+ }
88
+
89
+ export async function listSaved() {
90
+ const {entries} = await listEntries(SAVED_SLUG, {limit: 0, sort: 'savedAt', order: 'desc'});
91
+ return entries.map(savedSummary);
92
+ }
93
+
94
+ export async function getSaved(id) {
95
+ const entry = await getEntry(SAVED_SLUG, id);
96
+ return entry ? savedDetail(entry) : null;
97
+ }
98
+
99
+ /** Staff change the note or move the outcome along. */
100
+ export async function updateSaved(id, body = {}) {
101
+ const entry = await getEntry(SAVED_SLUG, id);
102
+ if (!entry) return {missing: true};
103
+ const patch = {};
104
+ if (body.note !== undefined) patch.note = cleanText(body.note, NOTE_MAX);
105
+ if (body.outcome !== undefined) {
106
+ if (!OUTCOMES.includes(body.outcome)) return {error: 'Pick to look at, looking into it or done.'};
107
+ patch.outcome = body.outcome;
108
+ }
109
+ const updated = await updateEntry(SAVED_SLUG, id, {...entry.data, ...patch});
110
+ return {item: savedSummary(updated)};
111
+ }
112
+
113
+ export async function removeSaved(id) {
114
+ if (!await getEntry(SAVED_SLUG, id)) return false;
115
+ await deleteEntry(SAVED_SLUG, id);
116
+ return true;
117
+ }
@@ -15,6 +15,7 @@
15
15
  */
16
16
  import {getConfig, saveConfig} from '../../config.js';
17
17
  import {CONFIDENCE} from './matcher.js';
18
+ import {launcherAvatar, launcherColour, normaliseLauncherChat as storedChat} from '../../../public/js/launcher-settings.mjs';
18
19
 
19
20
  export const CHAT_DEFAULTS = Object.freeze({
20
21
  confidence: 'balanced',
@@ -32,19 +33,32 @@ export const RETENTION_MAX = 730;
32
33
  /** The per-launcher chat block. */
33
34
  export const LAUNCHER_CHAT_DEFAULTS = Object.freeze({
34
35
  greeting: 'Hi! Ask me anything about us - or ask to talk to a person.',
35
- botName: 'Assistant',
36
+ botName: 'Della',
36
37
  bot: true,
37
38
  staff: true,
38
- askDetails: 'offline'
39
+ askDetails: 'offline',
40
+ ai: true,
41
+ aiUseSite: true,
42
+ aiContext: '',
43
+ aiRules: '',
44
+ accent: '',
45
+ accentText: '',
46
+ avatar: '',
47
+ aiMaxPerChat: 20
39
48
  });
40
49
  export const ASK_DETAILS = ['offline', 'always', 'never'];
41
50
  export const GREETING_MAX = 300;
42
51
  export const BOT_NAME_MAX = 40;
52
+ export const AI_CONTEXT_MAX = 4000;
53
+ export const AI_RULES_MAX = 2000;
43
54
 
44
55
  const clip = (v, max, fallback) => {
45
56
  const s = typeof v === 'string' ? v.trim().replace(/\s+/g, ' ').slice(0, max) : '';
46
57
  return s || fallback;
47
58
  };
59
+ const block = (v, max) => (typeof v === 'string'
60
+ // eslint-disable-next-line no-control-regex
61
+ ? v.replace(/\r\n?/g, '\n').replace(/[\u0000-\u0009\u000b-\u001f\u007f]/g, ' ').replace(/\n{3,}/g, '\n\n').trim().slice(0, max) : '');
48
62
  const flag = (v, fallback) => (v === undefined || v === null ? fallback : !(v === false || v === 'false' || v === 0 || v === '0' || v === 'off'));
49
63
 
50
64
  /**
@@ -65,7 +79,7 @@ export function normaliseChatSettings(raw) {
65
79
 
66
80
  /**
67
81
  * @param {object} [raw] - a launcher's `chat` block
68
- * @returns {{greeting: string, botName: string, bot: boolean, staff: boolean, askDetails: string}}
82
+ * @returns {{greeting: string, botName: string, bot: boolean, staff: boolean, askDetails: string, ai: boolean, aiContext: string, aiRules: string}}
69
83
  */
70
84
  export function normaliseLauncherChat(raw) {
71
85
  const r = raw && typeof raw === 'object' && !Array.isArray(raw) ? raw : {};
@@ -74,7 +88,19 @@ export function normaliseLauncherChat(raw) {
74
88
  botName: clip(r.botName, BOT_NAME_MAX, LAUNCHER_CHAT_DEFAULTS.botName),
75
89
  bot: flag(r.bot, true),
76
90
  staff: flag(r.staff, true),
77
- askDetails: ASK_DETAILS.includes(r.askDetails) ? r.askDetails : LAUNCHER_CHAT_DEFAULTS.askDetails
91
+ askDetails: ASK_DETAILS.includes(r.askDetails) ? r.askDetails : LAUNCHER_CHAT_DEFAULTS.askDetails,
92
+ ai: flag(r.ai, true),
93
+ aiUseSite: flag(r.aiUseSite, true),
94
+ aiContext: block(r.aiContext, AI_CONTEXT_MAX),
95
+ aiRules: block(r.aiRules, AI_RULES_MAX),
96
+ accent: launcherColour(r.accent),
97
+ accentText: launcherColour(r.accentText),
98
+ avatar: launcherAvatar(r.avatar),
99
+ aiMaxPerChat: Number.isFinite(Number(r.aiMaxPerChat)) && String(r.aiMaxPerChat ?? '').trim() !== ''
100
+ ? Math.min(100, Math.max(0, Math.round(Number(r.aiMaxPerChat)))) : LAUNCHER_CHAT_DEFAULTS.aiMaxPerChat,
101
+ starters: storedChat(r).starters,
102
+ personButton: storedChat(r).personButton,
103
+ times: storedChat(r).times
78
104
  };
79
105
  }
80
106
 
@@ -441,7 +441,7 @@ export async function addMessage(id, msg, patch = {}) {
441
441
  const message = await createEntry(SLUGS.messages, {
442
442
  sessionId: id, n, from: msg.from, text: cleanText(msg.text, msg.answerId ? ANSWER_SNAPSHOT : TEXT_MAX),
443
443
  answerId: msg.answerId || '', suggestions: Array.isArray(msg.suggestions) ? msg.suggestions : [],
444
- staffName: msg.staffName || ''
444
+ staffName: msg.staffName || '', ...(msg.ai && {ai: true})
445
445
  }, {source: 'chat'});
446
446
  const at = message.meta.createdAt;
447
447
  const data = {...session.data, ...patch, count: n, lastAt: at};
@@ -54,7 +54,8 @@ const CORE_TOOL_COLLECTIONS = Object.freeze({
54
54
  'chat-knowledge': ['chats', 'manage', 'Chats'],
55
55
  'chat-sessions': ['chats', 'manage', 'Chats'],
56
56
  'chat-messages': ['chats', 'manage', 'Chats'],
57
- 'chat-unanswered': ['chats', 'manage', 'Chats']
57
+ 'chat-unanswered': ['chats', 'manage', 'Chats'],
58
+ 'chat-saved': ['chats', 'manage', 'Chats']
58
59
  });
59
60
 
60
61
  /** A row anyone with the Tool may read, whoever made it (ownership gates writes only). */
@@ -25,7 +25,7 @@ import {
25
25
  export const LAUNCHERS_CACHE_TAG = 'site';
26
26
 
27
27
  /** Bumped by hand when launchers.mjs or launchers.css changes - the updater replaces public/ wholesale. */
28
- export const LAUNCHERS_ASSET_V = '20261101-launchers-covering';
28
+ export const LAUNCHERS_ASSET_V = '20261110-chat-times';
29
29
 
30
30
  /** @returns {object[]} the stored launchers, normalised, in order; none when the file is missing or broken */
31
31
  export function getLaunchers() {