domma-cms 1.49.2 → 1.51.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/admin/js/lib/chats-arrange.js +2 -2
- package/admin/js/lib/launchers-arrange.js +5 -2
- package/admin/js/lib/site-settings-arrange.js +3 -3
- package/admin/js/templates/chats.html +26 -2
- package/admin/js/templates/docs/usage-chat.html +51 -5
- package/admin/js/templates/docs/usage-launchers.html +2 -0
- package/admin/js/templates/launchers.html +4 -0
- package/admin/js/templates/settings.html +34 -0
- package/admin/js/views/chats.js +5 -5
- package/admin/js/views/launchers.js +107 -33
- package/docs/DommaFeatures.md +4 -0
- package/docs/screenshots/builder-character-site-settings.png +0 -0
- package/docs/screenshots/launcher-della-settings.png +0 -0
- package/docs/screenshots/site-settings-characters.png +0 -0
- package/package.json +2 -1
- package/public/css/characters.css +1 -0
- package/public/css/launcher-chat.css +1 -1
- package/public/css/launchers.css +1 -1
- package/public/images/characters/daisy-poses.png +0 -0
- package/public/images/characters/della-poses.png +0 -0
- package/public/images/characters/dot-poses.png +0 -0
- package/public/js/builder-character.mjs +2 -0
- package/public/js/character-view.mjs +1 -0
- package/public/js/characters.mjs +1 -0
- package/public/js/launcher-chat.mjs +20 -10
- package/public/js/launcher-settings.mjs +6 -2
- package/public/js/launchers.mjs +3 -3
- package/scripts/build.js +1 -0
- package/server/routes/api/chat.js +4 -2
- package/server/routes/api/chats.js +37 -0
- package/server/routes/api/settings.js +36 -2
- package/server/services/assistant.js +67 -0
- package/server/services/chat/ai.js +109 -0
- package/server/services/chat/conversation.js +37 -2
- package/server/services/chat/launcher-bridge.js +3 -1
- package/server/services/chat/saved.js +117 -0
- package/server/services/chat/settings.js +31 -4
- package/server/services/chat/store.js +1 -1
- package/server/services/collectionAccess.js +2 -1
- package/server/services/launchers.js +7 -2
- package/server/services/presetCollections.js +24 -2
- package/server/services/siteGitignore.js +1 -1
|
@@ -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
|
|
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. */
|
|
@@ -12,6 +12,8 @@
|
|
|
12
12
|
* @module services/chat/launcher-bridge
|
|
13
13
|
*/
|
|
14
14
|
import {normaliseLauncherChat} from './settings.js';
|
|
15
|
+
import {getConfig} from '../../config.js';
|
|
16
|
+
import {characterChat} from '../../../public/js/characters.mjs';
|
|
15
17
|
|
|
16
18
|
let loader = () => import('../launchers.js');
|
|
17
19
|
|
|
@@ -54,7 +56,7 @@ export async function chatLauncher(id) {
|
|
|
54
56
|
// Launchers ships with the engine now: no module means no chat, never "allow any id".
|
|
55
57
|
if (list === null) return null;
|
|
56
58
|
const found = list.find(l => l && String(l.id) === id);
|
|
57
|
-
return found ? {id, chat: normaliseLauncherChat(found.chat)} : null;
|
|
59
|
+
return found ? {id, chat: characterChat(normaliseLauncherChat(found.chat), getConfig('site').characters)} : null;
|
|
58
60
|
}
|
|
59
61
|
|
|
60
62
|
/**
|
|
@@ -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: '
|
|
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,20 @@ 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
|
+
character: storedChat(r).character,
|
|
100
|
+
aiMaxPerChat: Number.isFinite(Number(r.aiMaxPerChat)) && String(r.aiMaxPerChat ?? '').trim() !== ''
|
|
101
|
+
? Math.min(100, Math.max(0, Math.round(Number(r.aiMaxPerChat)))) : LAUNCHER_CHAT_DEFAULTS.aiMaxPerChat,
|
|
102
|
+
starters: storedChat(r).starters,
|
|
103
|
+
personButton: storedChat(r).personButton,
|
|
104
|
+
times: storedChat(r).times
|
|
78
105
|
};
|
|
79
106
|
}
|
|
80
107
|
|
|
@@ -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). */
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
* @module services/launchers
|
|
16
16
|
*/
|
|
17
17
|
import {getConfig, saveConfig} from '../config.js';
|
|
18
|
+
import {characterChat} from '../../public/js/characters.mjs';
|
|
18
19
|
import * as cache from './cache/index.js';
|
|
19
20
|
import {isToolEnabled} from './tools.js';
|
|
20
21
|
import {
|
|
@@ -25,7 +26,7 @@ import {
|
|
|
25
26
|
export const LAUNCHERS_CACHE_TAG = 'site';
|
|
26
27
|
|
|
27
28
|
/** Bumped by hand when launchers.mjs or launchers.css changes - the updater replaces public/ wholesale. */
|
|
28
|
-
export const LAUNCHERS_ASSET_V = '
|
|
29
|
+
export const LAUNCHERS_ASSET_V = '20261110-builder-characters';
|
|
29
30
|
|
|
30
31
|
/** @returns {object[]} the stored launchers, normalised, in order; none when the file is missing or broken */
|
|
31
32
|
export function getLaunchers() {
|
|
@@ -98,7 +99,11 @@ export function launchersForPage(urlPath, user) {
|
|
|
98
99
|
if (!launchersOn()) return [];
|
|
99
100
|
return getLaunchers()
|
|
100
101
|
.filter(l => launcherReady(l) && launcherShowsOn(l, urlPath, Boolean(user)))
|
|
101
|
-
.map(
|
|
102
|
+
.map(l => {
|
|
103
|
+
const out = publicLauncher(l);
|
|
104
|
+
if (out.chat) out.chat = characterChat(out.chat, getConfig('site').characters);
|
|
105
|
+
return out;
|
|
106
|
+
});
|
|
102
107
|
}
|
|
103
108
|
|
|
104
109
|
/**
|
|
@@ -500,6 +500,7 @@ const PRESETS = [
|
|
|
500
500
|
{name: 'count', label: 'Messages', type: 'number', default: 0},
|
|
501
501
|
{name: 'unread', label: 'Unread by staff', type: 'number', default: 0},
|
|
502
502
|
{name: 'misses', label: 'Unanswered in a row', type: 'number', default: 0},
|
|
503
|
+
{name: 'aiCount', label: 'AI answers', type: 'number', default: 0},
|
|
503
504
|
{name: 'askDetails', label: 'Details asked for', type: 'boolean', default: false},
|
|
504
505
|
{name: 'assignedTo', label: 'Answered by (id)', type: 'text'},
|
|
505
506
|
{name: 'assignedName', label: 'Answered by', type: 'text'},
|
|
@@ -514,7 +515,8 @@ const PRESETS = [
|
|
|
514
515
|
{name: 'text', label: 'Text', type: 'textarea'},
|
|
515
516
|
{name: 'answerId', label: 'Knowledge entry', type: 'text'},
|
|
516
517
|
{name: 'suggestions', label: 'Suggestions', type: 'json'},
|
|
517
|
-
{name: 'staffName', label: 'Staff name', type: 'text'}
|
|
518
|
+
{name: 'staffName', label: 'Staff name', type: 'text'},
|
|
519
|
+
{name: 'ai', label: 'Answered by AI', type: 'boolean', default: false}
|
|
518
520
|
]),
|
|
519
521
|
...chatPreset('chat-unanswered', 'Chat Unanswered', 'Questions the chat bot could not answer, for turning into Knowledge.', [
|
|
520
522
|
{name: 'text', label: 'Question', type: 'text', required: true},
|
|
@@ -524,6 +526,26 @@ const PRESETS = [
|
|
|
524
526
|
{name: 'firstAt', label: 'First asked', type: 'datetime'},
|
|
525
527
|
{name: 'lastAt', label: 'Last asked', type: 'datetime'},
|
|
526
528
|
{name: 'done', label: 'Dealt with', type: 'boolean', default: false}
|
|
529
|
+
]),
|
|
530
|
+
// A copy of a chat kept for a closer look (services/chat/saved.js): outlives the chat itself.
|
|
531
|
+
...chatPreset('chat-saved', 'Saved Chats', 'Chats kept for a closer look: a copy of the conversation, why it was saved, and what came of it.', [
|
|
532
|
+
{name: 'title', label: 'Title', type: 'text', required: true},
|
|
533
|
+
{name: 'note', label: 'Why it was saved', type: 'textarea'},
|
|
534
|
+
{name: 'outcome', label: 'Outcome', type: 'select', options: ['to look at', 'looking into it', 'done'], default: 'to look at'},
|
|
535
|
+
{name: 'visitorName', label: 'Name', type: 'text'},
|
|
536
|
+
{name: 'visitorEmail', label: 'Email', type: 'text'},
|
|
537
|
+
{name: 'page', label: 'Page', type: 'text'},
|
|
538
|
+
{name: 'startedAt', label: 'Chat started', type: 'datetime'},
|
|
539
|
+
{name: 'lastAt', label: 'Last message', type: 'datetime'},
|
|
540
|
+
{name: 'savedAt', label: 'Saved', type: 'datetime'},
|
|
541
|
+
{name: 'savedByName', label: 'Saved by', type: 'text'},
|
|
542
|
+
{name: 'savedBy', label: 'Saved by (id)', type: 'hidden'},
|
|
543
|
+
{name: 'chatId', label: 'Chat', type: 'hidden'},
|
|
544
|
+
{name: 'launcherId', label: 'Launcher', type: 'text'},
|
|
545
|
+
{name: 'chatStatus', label: 'Chat status when saved', type: 'text'},
|
|
546
|
+
{name: 'messageCount', label: 'Messages', type: 'number', default: 0},
|
|
547
|
+
{name: 'transcript', label: 'Conversation', type: 'textarea'},
|
|
548
|
+
{name: 'messages', label: 'Messages (data)', type: 'json'}
|
|
527
549
|
])
|
|
528
550
|
];
|
|
529
551
|
|
|
@@ -541,7 +563,7 @@ export const PRESET_COLLECTION_SLUGS = PRESETS.map(p => p.slug);
|
|
|
541
563
|
export const USER_DATA_PRESET_SLUGS = Object.freeze(['notes-notes', 'todo-items', 'contacts-contacts', 'contacts-groups']);
|
|
542
564
|
|
|
543
565
|
/** The chat presets (services/chat/store.js). */
|
|
544
|
-
export const CHAT_PRESET_SLUGS = Object.freeze(['chat-knowledge', 'chat-sessions', 'chat-messages', 'chat-unanswered']);
|
|
566
|
+
export const CHAT_PRESET_SLUGS = Object.freeze(['chat-knowledge', 'chat-sessions', 'chat-messages', 'chat-unanswered', 'chat-saved']);
|
|
545
567
|
|
|
546
568
|
/**
|
|
547
569
|
* The presets that may live in MongoDB (adapterRegistry.js resolveUserDataPreset):
|
|
@@ -91,7 +91,7 @@ export const PRIVATE_DATA = [
|
|
|
91
91
|
'jb-applications', 'jb-candidate-profiles', 'jb-signups',
|
|
92
92
|
// Visitor chat (Launchers, 1.28.0): conversations, names and emails, and the
|
|
93
93
|
// questions visitors asked. The Knowledge (chat-knowledge) is authored and stays.
|
|
94
|
-
'chat-sessions', 'chat-messages', 'chat-unanswered',
|
|
94
|
+
'chat-sessions', 'chat-messages', 'chat-unanswered', 'chat-saved',
|
|
95
95
|
// Games: each play carries the player's name and their resume token.
|
|
96
96
|
'quiz-plays', 'crossword-plays', 'daily-word-plays', 'word-search-plays',
|
|
97
97
|
// Outreach (Surveys, Campaigns): recipients, sends, claims, opens and clicks,
|