@aglyn/plugins-ai 1.31.0 → 1.32.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/README.md +2 -1
- package/package.json +17 -17
- package/src/lib/components/site-chat-api.d.ts +38 -0
- package/src/lib/components/site-chat-api.js +65 -0
- package/src/lib/components/site-chat-api.js.map +1 -0
- package/src/lib/components/site-chat-card.component.d.ts +31 -0
- package/src/lib/components/site-chat-card.component.js +474 -0
- package/src/lib/components/site-chat-card.component.js.map +1 -0
- package/src/lib/components/site-chat-panel.d.ts +38 -0
- package/src/lib/components/site-chat-panel.js +749 -0
- package/src/lib/components/site-chat-panel.js.map +1 -0
- package/src/lib/components/site-chat-runtime.d.ts +29 -0
- package/src/lib/components/site-chat-runtime.js +166 -0
- package/src/lib/components/site-chat-runtime.js.map +1 -0
- package/src/lib/declarations.js +5 -0
- package/src/lib/declarations.js.map +1 -1
- package/src/lib/model/site-chat.d.ts +219 -0
- package/src/lib/model/site-chat.js +200 -0
- package/src/lib/model/site-chat.js.map +1 -0
- package/src/lib/plugin.js +10 -0
- package/src/lib/plugin.js.map +1 -1
- package/src/lib/runtime/ai-palette.generated.js +124 -0
- package/src/lib/runtime/ai-palette.generated.js.map +1 -1
- package/src/lib/server/site-chat-enricher.d.ts +29 -0
- package/src/lib/server/site-chat-enricher.js +41 -0
- package/src/lib/server/site-chat-enricher.js.map +1 -0
- package/src/lib/server/site-chat-owner-routes.d.ts +56 -0
- package/src/lib/server/site-chat-owner-routes.js +209 -0
- package/src/lib/server/site-chat-owner-routes.js.map +1 -0
- package/src/lib/server/site-chat-page-reads.d.ts +23 -0
- package/src/lib/server/site-chat-page-reads.js +32 -0
- package/src/lib/server/site-chat-page-reads.js.map +1 -0
- package/src/lib/server/site-chat-platform.d.ts +25 -0
- package/src/lib/server/site-chat-platform.js +405 -0
- package/src/lib/server/site-chat-platform.js.map +1 -0
- package/src/lib/server/site-chat-prompt.d.ts +83 -0
- package/src/lib/server/site-chat-prompt.js +166 -0
- package/src/lib/server/site-chat-prompt.js.map +1 -0
- package/src/lib/server/site-chat-session.d.ts +33 -0
- package/src/lib/server/site-chat-session.js +105 -0
- package/src/lib/server/site-chat-session.js.map +1 -0
- package/src/lib/server/site-chat-visitor-routes.d.ts +46 -0
- package/src/lib/server/site-chat-visitor-routes.js +105 -0
- package/src/lib/server/site-chat-visitor-routes.js.map +1 -0
- package/src/lib/server/site-chat.d.ts +119 -0
- package/src/lib/server/site-chat.js +500 -0
- package/src/lib/server/site-chat.js.map +1 -0
- package/src/lib/server.js +4 -0
- package/src/lib/server.js.map +1 -1
- package/src/lib/site-server.d.ts +23 -0
- package/src/lib/site-server.js +41 -0
- package/src/lib/site-server.js.map +1 -0
- package/src/lib/site.d.ts +23 -0
- package/src/lib/site.js +33 -0
- package/src/lib/site.js.map +1 -0
- package/src/lib/usage/site-chat-meter.d.ts +68 -0
- package/src/lib/usage/site-chat-meter.js +125 -0
- package/src/lib/usage/site-chat-meter.js.map +1 -0
|
@@ -0,0 +1,749 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Aglyn LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/ import { _ as _extends } from "@swc/helpers/_/_extends";
|
|
17
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
18
|
+
import Box from "@mui/material/Box";
|
|
19
|
+
import Button from "@mui/material/Button";
|
|
20
|
+
import Chip from "@mui/material/Chip";
|
|
21
|
+
import IconButton from "@mui/material/IconButton";
|
|
22
|
+
import Link from "@mui/material/Link";
|
|
23
|
+
import Paper from "@mui/material/Paper";
|
|
24
|
+
import Stack from "@mui/material/Stack";
|
|
25
|
+
import SvgIcon from "@mui/material/SvgIcon";
|
|
26
|
+
import TextField from "@mui/material/TextField";
|
|
27
|
+
import Typography from "@mui/material/Typography";
|
|
28
|
+
import { Fragment, useCallback, useEffect, useRef, useState } from "react";
|
|
29
|
+
import { SITE_CHAT_HISTORY_ROUTE, SITE_CHAT_MESSAGE_MAX, SITE_CHAT_ROUTE } from "../model/site-chat.js";
|
|
30
|
+
const CLOSE_ICON = 'M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z';
|
|
31
|
+
const SEND_ICON = 'M2,21L23,12L2,3V10L17,12L2,14V21Z';
|
|
32
|
+
let counter = 0;
|
|
33
|
+
const nextId = ()=>`m${counter += 1}`;
|
|
34
|
+
/** A site path, and nothing else, is a link the panel will draw. */ export function isSitePath(href) {
|
|
35
|
+
return /^\/(?!\/)[^\s]*$/.test(href);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Answer text as the visitor sees it: paragraphs, and Markdown links drawn
|
|
39
|
+
* only when they point at a path of this site. Anything else is its text.
|
|
40
|
+
*/ export function renderSiteChatText(text) {
|
|
41
|
+
const paragraphs = text.split(/\n{2,}/);
|
|
42
|
+
return paragraphs.map((paragraph, index)=>{
|
|
43
|
+
const parts = [];
|
|
44
|
+
const pattern = /\[([^\]]+)\]\(([^)\s]+)\)|\*\*([^*]+)\*\*/g;
|
|
45
|
+
let last = 0;
|
|
46
|
+
let match;
|
|
47
|
+
while(match = pattern.exec(paragraph)){
|
|
48
|
+
if (match.index > last) parts.push(paragraph.slice(last, match.index));
|
|
49
|
+
if (match[3]) parts.push(/*#__PURE__*/ _jsx("strong", {
|
|
50
|
+
children: match[3]
|
|
51
|
+
}, `b${match.index}`));
|
|
52
|
+
else if (isSitePath(match[2])) {
|
|
53
|
+
parts.push(/*#__PURE__*/ _jsx(Link, {
|
|
54
|
+
href: match[2],
|
|
55
|
+
underline: "always",
|
|
56
|
+
children: match[1]
|
|
57
|
+
}, `l${match.index}`));
|
|
58
|
+
} else parts.push(match[1]);
|
|
59
|
+
last = match.index + match[0].length;
|
|
60
|
+
}
|
|
61
|
+
if (last < paragraph.length) parts.push(paragraph.slice(last));
|
|
62
|
+
return /*#__PURE__*/ _jsx(Typography, {
|
|
63
|
+
variant: "body2",
|
|
64
|
+
component: "p",
|
|
65
|
+
sx: {
|
|
66
|
+
m: 0,
|
|
67
|
+
mt: index ? 1 : 0,
|
|
68
|
+
whiteSpace: 'pre-line'
|
|
69
|
+
},
|
|
70
|
+
children: parts.map((part, at)=>/*#__PURE__*/ _jsx(Fragment, {
|
|
71
|
+
children: part
|
|
72
|
+
}, at))
|
|
73
|
+
}, index);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
/** The events of one streamed answer, as they arrive. */ export async function readSiteChatStream(response, onEvent) {
|
|
77
|
+
var _response_body;
|
|
78
|
+
const reader = (_response_body = response.body) == null ? void 0 : _response_body.getReader();
|
|
79
|
+
if (!reader) return;
|
|
80
|
+
const decoder = new TextDecoder();
|
|
81
|
+
let buffer = '';
|
|
82
|
+
for(;;){
|
|
83
|
+
const { value, done } = await reader.read();
|
|
84
|
+
if (done) break;
|
|
85
|
+
buffer += decoder.decode(value, {
|
|
86
|
+
stream: true
|
|
87
|
+
});
|
|
88
|
+
let end;
|
|
89
|
+
while((end = buffer.indexOf('\n\n')) >= 0){
|
|
90
|
+
const frame = buffer.slice(0, end);
|
|
91
|
+
buffer = buffer.slice(end + 2);
|
|
92
|
+
const line = frame.split('\n').find((entry)=>entry.startsWith('data: '));
|
|
93
|
+
if (!line) continue;
|
|
94
|
+
try {
|
|
95
|
+
onEvent(JSON.parse(line.slice(6)));
|
|
96
|
+
} catch (unused) {
|
|
97
|
+
// A frame that is not an event is skipped.
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
function CardView({ card }) {
|
|
103
|
+
return /*#__PURE__*/ _jsxs(Paper, _extends({
|
|
104
|
+
variant: "outlined"
|
|
105
|
+
}, card.url && isSitePath(card.url) ? {
|
|
106
|
+
component: 'a',
|
|
107
|
+
href: card.url
|
|
108
|
+
} : {}, {
|
|
109
|
+
sx: {
|
|
110
|
+
display: 'flex',
|
|
111
|
+
gap: 1.5,
|
|
112
|
+
p: 1,
|
|
113
|
+
textDecoration: 'none',
|
|
114
|
+
color: 'inherit',
|
|
115
|
+
borderRadius: 2
|
|
116
|
+
},
|
|
117
|
+
children: [
|
|
118
|
+
card.imageUrl ? /*#__PURE__*/ _jsx(Box, {
|
|
119
|
+
component: "img",
|
|
120
|
+
src: card.imageUrl,
|
|
121
|
+
alt: "",
|
|
122
|
+
loading: "lazy",
|
|
123
|
+
sx: {
|
|
124
|
+
width: 56,
|
|
125
|
+
height: 56,
|
|
126
|
+
objectFit: 'cover',
|
|
127
|
+
borderRadius: 1.5,
|
|
128
|
+
flexShrink: 0
|
|
129
|
+
}
|
|
130
|
+
}) : null,
|
|
131
|
+
/*#__PURE__*/ _jsxs(Box, {
|
|
132
|
+
sx: {
|
|
133
|
+
minWidth: 0
|
|
134
|
+
},
|
|
135
|
+
children: [
|
|
136
|
+
/*#__PURE__*/ _jsx(Typography, {
|
|
137
|
+
variant: "subtitle2",
|
|
138
|
+
noWrap: true,
|
|
139
|
+
children: card.title
|
|
140
|
+
}),
|
|
141
|
+
card.price ? /*#__PURE__*/ _jsxs(Typography, {
|
|
142
|
+
variant: "body2",
|
|
143
|
+
children: [
|
|
144
|
+
card.price,
|
|
145
|
+
' ',
|
|
146
|
+
card.compareAtPrice ? /*#__PURE__*/ _jsx(Box, {
|
|
147
|
+
component: "s",
|
|
148
|
+
sx: {
|
|
149
|
+
color: 'text.secondary'
|
|
150
|
+
},
|
|
151
|
+
children: card.compareAtPrice
|
|
152
|
+
}) : null
|
|
153
|
+
]
|
|
154
|
+
}) : null,
|
|
155
|
+
card.caption ? /*#__PURE__*/ _jsx(Typography, {
|
|
156
|
+
variant: "caption",
|
|
157
|
+
color: "text.secondary",
|
|
158
|
+
children: card.caption
|
|
159
|
+
}) : null
|
|
160
|
+
]
|
|
161
|
+
})
|
|
162
|
+
]
|
|
163
|
+
}));
|
|
164
|
+
}
|
|
165
|
+
/** A fresh key for one press, for a door that refuses a repeated attempt. */ function attemptKey() {
|
|
166
|
+
try {
|
|
167
|
+
return window.crypto.randomUUID();
|
|
168
|
+
} catch (unused) {
|
|
169
|
+
return `${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/** Where a door's answer may send the visitor: this site, or a checkout it named. */ function nextUrl(answer, fallback) {
|
|
173
|
+
const url = typeof answer.url === 'string' ? answer.url : '';
|
|
174
|
+
if (url && (isSitePath(url) || /^https:\/\//.test(url))) return url;
|
|
175
|
+
return fallback != null ? fallback : null;
|
|
176
|
+
}
|
|
177
|
+
function ProposalView({ proposal }) {
|
|
178
|
+
var _proposal_fields;
|
|
179
|
+
const [state, setState] = useState('ready');
|
|
180
|
+
const [error, setError] = useState('');
|
|
181
|
+
const [values, setValues] = useState({});
|
|
182
|
+
const fields = (_proposal_fields = proposal.fields) != null ? _proposal_fields : [];
|
|
183
|
+
const missing = fields.some((field)=>{
|
|
184
|
+
var _values_field_name;
|
|
185
|
+
return field.required && !String((_values_field_name = values[field.name]) != null ? _values_field_name : '').trim();
|
|
186
|
+
});
|
|
187
|
+
const confirm = async ()=>{
|
|
188
|
+
setState('sending');
|
|
189
|
+
setError('');
|
|
190
|
+
try {
|
|
191
|
+
const path = proposal.request.path.replace(/^\/?api\//, '');
|
|
192
|
+
// The VISITOR's request, from their browser, with their own cookies:
|
|
193
|
+
// the chat proposed it, and only this press sends it.
|
|
194
|
+
const response = await fetch(`/api/${path}`, {
|
|
195
|
+
method: 'POST',
|
|
196
|
+
credentials: 'same-origin',
|
|
197
|
+
headers: _extends({
|
|
198
|
+
'Content-Type': 'application/json'
|
|
199
|
+
}, proposal.idempotent ? {
|
|
200
|
+
'Idempotency-Key': attemptKey()
|
|
201
|
+
} : {}),
|
|
202
|
+
body: JSON.stringify(_extends({}, proposal.request.body, values))
|
|
203
|
+
});
|
|
204
|
+
const answer = await response.json().catch(()=>({}));
|
|
205
|
+
if (!response.ok) {
|
|
206
|
+
var _answer_error;
|
|
207
|
+
setState('failed');
|
|
208
|
+
setError((_answer_error = answer.error) != null ? _answer_error : 'That did not go through. Try again, or use the page.');
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
setState('done');
|
|
212
|
+
if (proposal.afterEvent && /^[a-z][a-z0-9:-]{2,60}$/.test(proposal.afterEvent)) {
|
|
213
|
+
window.dispatchEvent(new Event(proposal.afterEvent));
|
|
214
|
+
}
|
|
215
|
+
// A booking or a stay that takes payment answers its checkout: the
|
|
216
|
+
// visitor pays there, as they would from the page.
|
|
217
|
+
const next = nextUrl(answer);
|
|
218
|
+
if (next && proposal.kind !== 'add-to-cart') window.location.assign(next);
|
|
219
|
+
} catch (unused) {
|
|
220
|
+
setState('failed');
|
|
221
|
+
setError('That did not go through. Try again, or use the page.');
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
return /*#__PURE__*/ _jsxs(Paper, {
|
|
225
|
+
variant: "outlined",
|
|
226
|
+
sx: {
|
|
227
|
+
p: 1.5,
|
|
228
|
+
borderRadius: 2
|
|
229
|
+
},
|
|
230
|
+
"data-site-chat-proposal": proposal.kind,
|
|
231
|
+
children: [
|
|
232
|
+
proposal.card ? /*#__PURE__*/ _jsx(CardView, {
|
|
233
|
+
card: proposal.card
|
|
234
|
+
}) : null,
|
|
235
|
+
/*#__PURE__*/ _jsx(Typography, {
|
|
236
|
+
variant: "subtitle2",
|
|
237
|
+
sx: {
|
|
238
|
+
mt: proposal.card ? 1 : 0
|
|
239
|
+
},
|
|
240
|
+
children: proposal.title
|
|
241
|
+
}),
|
|
242
|
+
/*#__PURE__*/ _jsx(Typography, {
|
|
243
|
+
variant: "body2",
|
|
244
|
+
color: "text.secondary",
|
|
245
|
+
children: proposal.summary
|
|
246
|
+
}),
|
|
247
|
+
state === 'done' ? /*#__PURE__*/ _jsxs(Stack, {
|
|
248
|
+
direction: "row",
|
|
249
|
+
spacing: 1,
|
|
250
|
+
sx: {
|
|
251
|
+
mt: 1,
|
|
252
|
+
alignItems: 'center'
|
|
253
|
+
},
|
|
254
|
+
children: [
|
|
255
|
+
/*#__PURE__*/ _jsx(Typography, {
|
|
256
|
+
variant: "body2",
|
|
257
|
+
color: "success.main",
|
|
258
|
+
children: "Done."
|
|
259
|
+
}),
|
|
260
|
+
proposal.thenUrl ? /*#__PURE__*/ _jsx(Button, {
|
|
261
|
+
size: "small",
|
|
262
|
+
href: proposal.thenUrl,
|
|
263
|
+
children: proposal.kind === 'add-to-cart' ? 'View cart' : 'Continue'
|
|
264
|
+
}) : null
|
|
265
|
+
]
|
|
266
|
+
}) : /*#__PURE__*/ _jsxs(Stack, {
|
|
267
|
+
spacing: 1,
|
|
268
|
+
sx: {
|
|
269
|
+
mt: 1
|
|
270
|
+
},
|
|
271
|
+
children: [
|
|
272
|
+
fields.map((field)=>{
|
|
273
|
+
var _values_field_name;
|
|
274
|
+
return /*#__PURE__*/ _jsx(TextField, {
|
|
275
|
+
size: "small",
|
|
276
|
+
type: field.type,
|
|
277
|
+
label: field.label,
|
|
278
|
+
required: field.required,
|
|
279
|
+
value: (_values_field_name = values[field.name]) != null ? _values_field_name : '',
|
|
280
|
+
onChange: (event)=>setValues((current)=>_extends({}, current, {
|
|
281
|
+
[field.name]: event.target.value
|
|
282
|
+
}))
|
|
283
|
+
}, field.name);
|
|
284
|
+
}),
|
|
285
|
+
/*#__PURE__*/ _jsx(Button, {
|
|
286
|
+
variant: "contained",
|
|
287
|
+
size: "small",
|
|
288
|
+
disabled: state === 'sending' || missing,
|
|
289
|
+
onClick: confirm,
|
|
290
|
+
children: proposal.confirmLabel
|
|
291
|
+
})
|
|
292
|
+
]
|
|
293
|
+
}),
|
|
294
|
+
error ? /*#__PURE__*/ _jsx(Typography, {
|
|
295
|
+
variant: "caption",
|
|
296
|
+
color: "error",
|
|
297
|
+
component: "p",
|
|
298
|
+
sx: {
|
|
299
|
+
mt: 0.5
|
|
300
|
+
},
|
|
301
|
+
children: error
|
|
302
|
+
}) : null
|
|
303
|
+
]
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
function HandOffForm({ hostId, prompt }) {
|
|
307
|
+
const [email, setEmail] = useState('');
|
|
308
|
+
const [question, setQuestion] = useState('');
|
|
309
|
+
const [state, setState] = useState('ready');
|
|
310
|
+
if (state === 'sent') {
|
|
311
|
+
return /*#__PURE__*/ _jsx(Typography, {
|
|
312
|
+
variant: "body2",
|
|
313
|
+
color: "success.main",
|
|
314
|
+
children: "Thanks. The team will reply by email."
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
const send = async ()=>{
|
|
318
|
+
setState('sending');
|
|
319
|
+
const response = await fetch(`/api/${SITE_CHAT_ROUTE}/handoff`, {
|
|
320
|
+
method: 'POST',
|
|
321
|
+
credentials: 'same-origin',
|
|
322
|
+
headers: {
|
|
323
|
+
'Content-Type': 'application/json'
|
|
324
|
+
},
|
|
325
|
+
body: JSON.stringify({
|
|
326
|
+
hostId,
|
|
327
|
+
email,
|
|
328
|
+
question
|
|
329
|
+
})
|
|
330
|
+
}).catch(()=>null);
|
|
331
|
+
setState((response == null ? void 0 : response.ok) ? 'sent' : 'failed');
|
|
332
|
+
};
|
|
333
|
+
return /*#__PURE__*/ _jsxs(Stack, {
|
|
334
|
+
spacing: 1,
|
|
335
|
+
children: [
|
|
336
|
+
/*#__PURE__*/ _jsx(Typography, {
|
|
337
|
+
variant: "body2",
|
|
338
|
+
children: prompt
|
|
339
|
+
}),
|
|
340
|
+
/*#__PURE__*/ _jsx(TextField, {
|
|
341
|
+
size: "small",
|
|
342
|
+
type: "email",
|
|
343
|
+
label: "Your email",
|
|
344
|
+
value: email,
|
|
345
|
+
onChange: (e)=>setEmail(e.target.value)
|
|
346
|
+
}),
|
|
347
|
+
/*#__PURE__*/ _jsx(TextField, {
|
|
348
|
+
size: "small",
|
|
349
|
+
label: "Your question",
|
|
350
|
+
multiline: true,
|
|
351
|
+
minRows: 2,
|
|
352
|
+
value: question,
|
|
353
|
+
onChange: (e)=>setQuestion(e.target.value)
|
|
354
|
+
}),
|
|
355
|
+
/*#__PURE__*/ _jsx(Button, {
|
|
356
|
+
variant: "outlined",
|
|
357
|
+
size: "small",
|
|
358
|
+
disabled: state === 'sending' || !/^\S+@\S+\.\S+$/.test(email) || !question.trim(),
|
|
359
|
+
onClick: send,
|
|
360
|
+
children: "Send to the team"
|
|
361
|
+
}),
|
|
362
|
+
state === 'failed' ? /*#__PURE__*/ _jsx(Typography, {
|
|
363
|
+
variant: "caption",
|
|
364
|
+
color: "error",
|
|
365
|
+
children: "That did not go through. Please try again."
|
|
366
|
+
}) : null
|
|
367
|
+
]
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
function Sources({ sources }) {
|
|
371
|
+
if (!sources.length) return null;
|
|
372
|
+
return /*#__PURE__*/ _jsxs(Typography, {
|
|
373
|
+
variant: "caption",
|
|
374
|
+
color: "text.secondary",
|
|
375
|
+
component: "p",
|
|
376
|
+
sx: {
|
|
377
|
+
mt: 0.5
|
|
378
|
+
},
|
|
379
|
+
children: [
|
|
380
|
+
"From:",
|
|
381
|
+
' ',
|
|
382
|
+
sources.map((source, index)=>/*#__PURE__*/ _jsxs(Fragment, {
|
|
383
|
+
children: [
|
|
384
|
+
index ? ', ' : '',
|
|
385
|
+
/*#__PURE__*/ _jsx(Link, {
|
|
386
|
+
href: source.url,
|
|
387
|
+
color: "inherit",
|
|
388
|
+
children: source.title
|
|
389
|
+
})
|
|
390
|
+
]
|
|
391
|
+
}, source.url))
|
|
392
|
+
]
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* The chat panel (AGL-4250), loaded on the launcher's press. A conversation
|
|
397
|
+
* is the visitor's own, kept by the site under a signed session cookie, so a
|
|
398
|
+
* visitor who comes back sees where they left off.
|
|
399
|
+
*/ export function SiteChatPanel({ hostId, slice, onClose }) {
|
|
400
|
+
const [messages, setMessages] = useState([]);
|
|
401
|
+
const [memberName, setMemberName] = useState('');
|
|
402
|
+
const [draft, setDraft] = useState('');
|
|
403
|
+
const [busy, setBusy] = useState(false);
|
|
404
|
+
const listRef = useRef(null);
|
|
405
|
+
useEffect(()=>{
|
|
406
|
+
let cancelled = false;
|
|
407
|
+
fetch(`/api/${SITE_CHAT_HISTORY_ROUTE}?hostId=${encodeURIComponent(hostId)}`, {
|
|
408
|
+
credentials: 'same-origin'
|
|
409
|
+
}).then((response)=>response.ok ? response.json() : null).then((answer)=>{
|
|
410
|
+
var _answer_member;
|
|
411
|
+
if (cancelled || !answer) return;
|
|
412
|
+
if ((_answer_member = answer.member) == null ? void 0 : _answer_member.name) setMemberName(answer.member.name);
|
|
413
|
+
if (Array.isArray(answer.turns)) {
|
|
414
|
+
setMessages(answer.turns.map((turn)=>_extends({}, turn, {
|
|
415
|
+
id: nextId()
|
|
416
|
+
})));
|
|
417
|
+
}
|
|
418
|
+
}).catch(()=>undefined);
|
|
419
|
+
return ()=>{
|
|
420
|
+
cancelled = true;
|
|
421
|
+
};
|
|
422
|
+
}, [
|
|
423
|
+
hostId
|
|
424
|
+
]);
|
|
425
|
+
useEffect(()=>{
|
|
426
|
+
var _listRef_current;
|
|
427
|
+
(_listRef_current = listRef.current) == null ? void 0 : _listRef_current.scrollTo({
|
|
428
|
+
top: listRef.current.scrollHeight
|
|
429
|
+
});
|
|
430
|
+
}, [
|
|
431
|
+
messages
|
|
432
|
+
]);
|
|
433
|
+
const ask = useCallback(async (question)=>{
|
|
434
|
+
const message = question.trim().slice(0, SITE_CHAT_MESSAGE_MAX);
|
|
435
|
+
if (!message || busy) return;
|
|
436
|
+
setBusy(true);
|
|
437
|
+
setDraft('');
|
|
438
|
+
const answerId = nextId();
|
|
439
|
+
setMessages((list)=>[
|
|
440
|
+
...list,
|
|
441
|
+
{
|
|
442
|
+
id: nextId(),
|
|
443
|
+
role: 'visitor',
|
|
444
|
+
text: message
|
|
445
|
+
},
|
|
446
|
+
{
|
|
447
|
+
id: answerId,
|
|
448
|
+
role: 'assistant',
|
|
449
|
+
text: ''
|
|
450
|
+
}
|
|
451
|
+
]);
|
|
452
|
+
const update = (change)=>setMessages((list)=>list.map((entry)=>entry.id === answerId ? change(entry) : entry));
|
|
453
|
+
try {
|
|
454
|
+
const response = await fetch(`/api/${SITE_CHAT_ROUTE}`, {
|
|
455
|
+
method: 'POST',
|
|
456
|
+
credentials: 'same-origin',
|
|
457
|
+
headers: {
|
|
458
|
+
'Content-Type': 'application/json'
|
|
459
|
+
},
|
|
460
|
+
body: JSON.stringify({
|
|
461
|
+
hostId,
|
|
462
|
+
message,
|
|
463
|
+
path: window.location.pathname
|
|
464
|
+
})
|
|
465
|
+
});
|
|
466
|
+
if (!response.ok) {
|
|
467
|
+
const answer = await response.json().catch(()=>({}));
|
|
468
|
+
update((entry)=>{
|
|
469
|
+
var _answer_error;
|
|
470
|
+
return _extends({}, entry, {
|
|
471
|
+
text: (_answer_error = answer.error) != null ? _answer_error : 'The chat is not available right now.',
|
|
472
|
+
failed: true
|
|
473
|
+
});
|
|
474
|
+
});
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
await readSiteChatStream(response, (event)=>{
|
|
478
|
+
if (event.type === 'delta') update((entry)=>_extends({}, entry, {
|
|
479
|
+
text: entry.text + event.text
|
|
480
|
+
}));
|
|
481
|
+
else if (event.type === 'cards') update((entry)=>_extends({}, entry, {
|
|
482
|
+
cards: event.cards
|
|
483
|
+
}));
|
|
484
|
+
else if (event.type === 'proposal') update((entry)=>_extends({}, entry, {
|
|
485
|
+
proposal: event.proposal
|
|
486
|
+
}));
|
|
487
|
+
else if (event.type === 'handoff') update((entry)=>_extends({}, entry, {
|
|
488
|
+
handOff: event.message
|
|
489
|
+
}));
|
|
490
|
+
else if (event.type === 'done') update((entry)=>_extends({}, entry, {
|
|
491
|
+
sources: event.sources
|
|
492
|
+
}));
|
|
493
|
+
else if (event.type === 'error') update((entry)=>_extends({}, entry, {
|
|
494
|
+
text: event.error,
|
|
495
|
+
failed: true
|
|
496
|
+
}));
|
|
497
|
+
});
|
|
498
|
+
} catch (unused) {
|
|
499
|
+
update((entry)=>_extends({}, entry, {
|
|
500
|
+
text: 'The chat is not available right now.',
|
|
501
|
+
failed: true
|
|
502
|
+
}));
|
|
503
|
+
} finally{
|
|
504
|
+
setBusy(false);
|
|
505
|
+
}
|
|
506
|
+
}, [
|
|
507
|
+
busy,
|
|
508
|
+
hostId
|
|
509
|
+
]);
|
|
510
|
+
const greeting = memberName ? `Hi ${memberName}! ${slice.greeting.replace(/^(hi|hello|hey)[!,.]?\s*/i, '')}` : slice.greeting;
|
|
511
|
+
const side = slice.position === 'left' ? {
|
|
512
|
+
left: {
|
|
513
|
+
xs: 0,
|
|
514
|
+
sm: 24
|
|
515
|
+
}
|
|
516
|
+
} : {
|
|
517
|
+
right: {
|
|
518
|
+
xs: 0,
|
|
519
|
+
sm: 24
|
|
520
|
+
}
|
|
521
|
+
};
|
|
522
|
+
return /*#__PURE__*/ _jsxs(Paper, {
|
|
523
|
+
elevation: 8,
|
|
524
|
+
role: "dialog",
|
|
525
|
+
"aria-label": "Chat",
|
|
526
|
+
"data-site-chat-panel": "",
|
|
527
|
+
sx: (theme)=>_extends({
|
|
528
|
+
position: 'fixed',
|
|
529
|
+
zIndex: theme.zIndex.modal,
|
|
530
|
+
bottom: {
|
|
531
|
+
xs: 0,
|
|
532
|
+
sm: 24
|
|
533
|
+
}
|
|
534
|
+
}, side, {
|
|
535
|
+
width: {
|
|
536
|
+
xs: '100vw',
|
|
537
|
+
sm: 380
|
|
538
|
+
},
|
|
539
|
+
height: {
|
|
540
|
+
xs: '85dvh',
|
|
541
|
+
sm: 560
|
|
542
|
+
},
|
|
543
|
+
maxHeight: '100dvh',
|
|
544
|
+
display: 'flex',
|
|
545
|
+
flexDirection: 'column',
|
|
546
|
+
borderRadius: {
|
|
547
|
+
xs: '16px 16px 0 0',
|
|
548
|
+
sm: 3
|
|
549
|
+
},
|
|
550
|
+
overflow: 'hidden'
|
|
551
|
+
}),
|
|
552
|
+
children: [
|
|
553
|
+
/*#__PURE__*/ _jsxs(Stack, {
|
|
554
|
+
direction: "row",
|
|
555
|
+
sx: {
|
|
556
|
+
alignItems: 'center',
|
|
557
|
+
px: 2,
|
|
558
|
+
py: 1.25,
|
|
559
|
+
bgcolor: 'primary.main',
|
|
560
|
+
color: 'primary.contrastText'
|
|
561
|
+
},
|
|
562
|
+
children: [
|
|
563
|
+
/*#__PURE__*/ _jsx(Typography, {
|
|
564
|
+
variant: "subtitle1",
|
|
565
|
+
sx: {
|
|
566
|
+
flex: 1
|
|
567
|
+
},
|
|
568
|
+
children: "Chat"
|
|
569
|
+
}),
|
|
570
|
+
/*#__PURE__*/ _jsx(IconButton, {
|
|
571
|
+
"aria-label": "Close chat",
|
|
572
|
+
onClick: onClose,
|
|
573
|
+
sx: {
|
|
574
|
+
color: 'inherit'
|
|
575
|
+
},
|
|
576
|
+
children: /*#__PURE__*/ _jsx(SvgIcon, {
|
|
577
|
+
children: /*#__PURE__*/ _jsx("path", {
|
|
578
|
+
d: CLOSE_ICON
|
|
579
|
+
})
|
|
580
|
+
})
|
|
581
|
+
})
|
|
582
|
+
]
|
|
583
|
+
}),
|
|
584
|
+
/*#__PURE__*/ _jsxs(Box, {
|
|
585
|
+
ref: listRef,
|
|
586
|
+
sx: {
|
|
587
|
+
flex: 1,
|
|
588
|
+
overflowY: 'auto',
|
|
589
|
+
p: 2,
|
|
590
|
+
display: 'flex',
|
|
591
|
+
flexDirection: 'column',
|
|
592
|
+
gap: 1.5
|
|
593
|
+
},
|
|
594
|
+
children: [
|
|
595
|
+
/*#__PURE__*/ _jsx(Box, {
|
|
596
|
+
sx: {
|
|
597
|
+
alignSelf: 'flex-start',
|
|
598
|
+
maxWidth: '88%',
|
|
599
|
+
bgcolor: 'action.hover',
|
|
600
|
+
borderRadius: 2,
|
|
601
|
+
px: 1.5,
|
|
602
|
+
py: 1
|
|
603
|
+
},
|
|
604
|
+
children: /*#__PURE__*/ _jsx(Typography, {
|
|
605
|
+
variant: "body2",
|
|
606
|
+
children: greeting
|
|
607
|
+
})
|
|
608
|
+
}),
|
|
609
|
+
slice.openingMessage && !messages.length ? /*#__PURE__*/ _jsx(Box, {
|
|
610
|
+
sx: {
|
|
611
|
+
alignSelf: 'flex-start',
|
|
612
|
+
maxWidth: '88%',
|
|
613
|
+
bgcolor: 'action.hover',
|
|
614
|
+
borderRadius: 2,
|
|
615
|
+
px: 1.5,
|
|
616
|
+
py: 1
|
|
617
|
+
},
|
|
618
|
+
children: /*#__PURE__*/ _jsx(Typography, {
|
|
619
|
+
variant: "body2",
|
|
620
|
+
children: slice.openingMessage
|
|
621
|
+
})
|
|
622
|
+
}) : null,
|
|
623
|
+
messages.map((message)=>{
|
|
624
|
+
var _message_sources, _message_cards;
|
|
625
|
+
return message.role === 'visitor' ? /*#__PURE__*/ _jsx(Box, {
|
|
626
|
+
sx: {
|
|
627
|
+
alignSelf: 'flex-end',
|
|
628
|
+
maxWidth: '88%',
|
|
629
|
+
bgcolor: 'primary.main',
|
|
630
|
+
color: 'primary.contrastText',
|
|
631
|
+
borderRadius: 2,
|
|
632
|
+
px: 1.5,
|
|
633
|
+
py: 1
|
|
634
|
+
},
|
|
635
|
+
children: /*#__PURE__*/ _jsx(Typography, {
|
|
636
|
+
variant: "body2",
|
|
637
|
+
children: message.text
|
|
638
|
+
})
|
|
639
|
+
}, message.id) : /*#__PURE__*/ _jsxs(Stack, {
|
|
640
|
+
spacing: 1,
|
|
641
|
+
sx: {
|
|
642
|
+
alignSelf: 'flex-start',
|
|
643
|
+
maxWidth: '92%'
|
|
644
|
+
},
|
|
645
|
+
children: [
|
|
646
|
+
/*#__PURE__*/ _jsxs(Box, {
|
|
647
|
+
"aria-live": "polite",
|
|
648
|
+
sx: {
|
|
649
|
+
bgcolor: 'action.hover',
|
|
650
|
+
borderRadius: 2,
|
|
651
|
+
px: 1.5,
|
|
652
|
+
py: 1,
|
|
653
|
+
color: message.failed ? 'error.main' : undefined
|
|
654
|
+
},
|
|
655
|
+
children: [
|
|
656
|
+
message.text ? renderSiteChatText(message.text) : /*#__PURE__*/ _jsx(Typography, {
|
|
657
|
+
variant: "body2",
|
|
658
|
+
children: "…"
|
|
659
|
+
}),
|
|
660
|
+
/*#__PURE__*/ _jsx(Sources, {
|
|
661
|
+
sources: (_message_sources = message.sources) != null ? _message_sources : []
|
|
662
|
+
})
|
|
663
|
+
]
|
|
664
|
+
}),
|
|
665
|
+
((_message_cards = message.cards) != null ? _message_cards : []).map((card)=>{
|
|
666
|
+
var _card_url;
|
|
667
|
+
return /*#__PURE__*/ _jsx(CardView, {
|
|
668
|
+
card: card
|
|
669
|
+
}, (_card_url = card.url) != null ? _card_url : card.title);
|
|
670
|
+
}),
|
|
671
|
+
message.proposal ? /*#__PURE__*/ _jsx(ProposalView, {
|
|
672
|
+
proposal: message.proposal
|
|
673
|
+
}) : null,
|
|
674
|
+
message.handOff && slice.handOff ? /*#__PURE__*/ _jsx(HandOffForm, {
|
|
675
|
+
hostId: hostId,
|
|
676
|
+
prompt: message.handOff
|
|
677
|
+
}) : null
|
|
678
|
+
]
|
|
679
|
+
}, message.id);
|
|
680
|
+
}),
|
|
681
|
+
!messages.length && slice.chips.length ? /*#__PURE__*/ _jsx(Stack, {
|
|
682
|
+
direction: "row",
|
|
683
|
+
sx: {
|
|
684
|
+
flexWrap: 'wrap',
|
|
685
|
+
gap: 1
|
|
686
|
+
},
|
|
687
|
+
children: slice.chips.map((chip)=>/*#__PURE__*/ _jsx(Chip, {
|
|
688
|
+
label: chip,
|
|
689
|
+
variant: "outlined",
|
|
690
|
+
onClick: ()=>ask(chip)
|
|
691
|
+
}, chip))
|
|
692
|
+
}) : null
|
|
693
|
+
]
|
|
694
|
+
}),
|
|
695
|
+
/*#__PURE__*/ _jsxs(Box, {
|
|
696
|
+
component: "form",
|
|
697
|
+
onSubmit: (event)=>{
|
|
698
|
+
event.preventDefault();
|
|
699
|
+
void ask(draft);
|
|
700
|
+
},
|
|
701
|
+
sx: {
|
|
702
|
+
display: 'flex',
|
|
703
|
+
gap: 1,
|
|
704
|
+
p: 1.5,
|
|
705
|
+
borderTop: 1,
|
|
706
|
+
borderColor: 'divider'
|
|
707
|
+
},
|
|
708
|
+
children: [
|
|
709
|
+
/*#__PURE__*/ _jsx(TextField, {
|
|
710
|
+
size: "small",
|
|
711
|
+
fullWidth: true,
|
|
712
|
+
placeholder: "Ask a question",
|
|
713
|
+
value: draft,
|
|
714
|
+
onChange: (event)=>setDraft(event.target.value),
|
|
715
|
+
slotProps: {
|
|
716
|
+
htmlInput: {
|
|
717
|
+
maxLength: SITE_CHAT_MESSAGE_MAX,
|
|
718
|
+
'aria-label': 'Your question'
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
}),
|
|
722
|
+
/*#__PURE__*/ _jsx(IconButton, {
|
|
723
|
+
type: "submit",
|
|
724
|
+
color: "primary",
|
|
725
|
+
"aria-label": "Send",
|
|
726
|
+
disabled: busy || !draft.trim(),
|
|
727
|
+
children: /*#__PURE__*/ _jsx(SvgIcon, {
|
|
728
|
+
children: /*#__PURE__*/ _jsx("path", {
|
|
729
|
+
d: SEND_ICON
|
|
730
|
+
})
|
|
731
|
+
})
|
|
732
|
+
})
|
|
733
|
+
]
|
|
734
|
+
}),
|
|
735
|
+
/*#__PURE__*/ _jsx(Typography, {
|
|
736
|
+
variant: "caption",
|
|
737
|
+
color: "text.secondary",
|
|
738
|
+
sx: {
|
|
739
|
+
px: 2,
|
|
740
|
+
pb: 1
|
|
741
|
+
},
|
|
742
|
+
children: "Answers are written by AI from this site's own pages."
|
|
743
|
+
})
|
|
744
|
+
]
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
export default SiteChatPanel;
|
|
748
|
+
|
|
749
|
+
//# sourceMappingURL=site-chat-panel.js.map
|