@aparte/n8n-chat 0.1.0 → 0.3.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 +230 -16
- package/dist/cards.d.ts +3 -0
- package/dist/cdn/chunks/chunk-E55C7UF2.js +2 -0
- package/dist/cdn/chunks/chunk-E55C7UF2.js.map +7 -0
- package/dist/cdn/chunks/chunk-F2QIT4NR.js +206 -0
- package/dist/cdn/chunks/chunk-F2QIT4NR.js.map +7 -0
- package/dist/cdn/chunks/chunk-F2TNPBN3.js +2 -0
- package/dist/cdn/chunks/chunk-F2TNPBN3.js.map +7 -0
- package/dist/cdn/chunks/chunk-KY2H3HIJ.js +2 -0
- package/dist/cdn/chunks/chunk-KY2H3HIJ.js.map +7 -0
- package/dist/cdn/chunks/chunk-VLHCX6EH.js +356 -0
- package/dist/cdn/chunks/chunk-VLHCX6EH.js.map +7 -0
- package/dist/cdn/chunks/chunk-XC2SVYFR.js +2 -0
- package/dist/cdn/chunks/chunk-XC2SVYFR.js.map +7 -0
- package/dist/cdn/chunks/de-M5OVUL5F.js +2 -0
- package/dist/cdn/chunks/de-M5OVUL5F.js.map +7 -0
- package/dist/cdn/chunks/es-JUSLSPF7.js +2 -0
- package/dist/cdn/chunks/es-JUSLSPF7.js.map +7 -0
- package/dist/cdn/chunks/fr-Z2Y3375Z.js +2 -0
- package/dist/cdn/chunks/fr-Z2Y3375Z.js.map +7 -0
- package/dist/cdn/chunks/{highlight-HDJ3ROJ4.js → highlight-ADUWBKTT.js} +2 -2
- package/dist/cdn/chunks/{highlight-HDJ3ROJ4.js.map → highlight-ADUWBKTT.js.map} +1 -1
- package/dist/cdn/chunks/it-N53LIIX5.js +2 -0
- package/dist/cdn/chunks/it-N53LIIX5.js.map +7 -0
- package/dist/cdn/chunks/items-GTJKLFZT.js +467 -0
- package/dist/cdn/chunks/items-GTJKLFZT.js.map +7 -0
- package/dist/cdn/chunks/ja-LJHMBKJ4.js +2 -0
- package/dist/cdn/chunks/ja-LJHMBKJ4.js.map +7 -0
- package/dist/cdn/chunks/ko-XAQOC75B.js +2 -0
- package/dist/cdn/chunks/ko-XAQOC75B.js.map +7 -0
- package/dist/cdn/chunks/link-L4C4K5GE.js +493 -0
- package/dist/cdn/chunks/link-L4C4K5GE.js.map +7 -0
- package/dist/cdn/chunks/media-EOQEOMXK.js +797 -0
- package/dist/cdn/chunks/media-EOQEOMXK.js.map +7 -0
- package/dist/cdn/chunks/order-T5L422AO.js +839 -0
- package/dist/cdn/chunks/order-T5L422AO.js.map +7 -0
- package/dist/cdn/chunks/place-UD2C3U5F.js +689 -0
- package/dist/cdn/chunks/place-UD2C3U5F.js.map +7 -0
- package/dist/cdn/chunks/plan-JJ3Z64NU.js +542 -0
- package/dist/cdn/chunks/plan-JJ3Z64NU.js.map +7 -0
- package/dist/cdn/chunks/product-FNGKWEQP.js +676 -0
- package/dist/cdn/chunks/product-FNGKWEQP.js.map +7 -0
- package/dist/cdn/chunks/pt-BR-KMZM3WTU.js +2 -0
- package/dist/cdn/chunks/pt-BR-KMZM3WTU.js.map +7 -0
- package/dist/cdn/chunks/ru-3AG5HYY2.js +2 -0
- package/dist/cdn/chunks/ru-3AG5HYY2.js.map +7 -0
- package/dist/cdn/chunks/score-3CGZEPWP.js +882 -0
- package/dist/cdn/chunks/score-3CGZEPWP.js.map +7 -0
- package/dist/cdn/chunks/stats-FGZCRFFK.js +491 -0
- package/dist/cdn/chunks/stats-FGZCRFFK.js.map +7 -0
- package/dist/cdn/chunks/stock-E72GAUMB.js +707 -0
- package/dist/cdn/chunks/stock-E72GAUMB.js.map +7 -0
- package/dist/cdn/chunks/weather-5ZNWC2WM.js +615 -0
- package/dist/cdn/chunks/weather-5ZNWC2WM.js.map +7 -0
- package/dist/cdn/chunks/zh-CN-DL6ZWLGA.js +2 -0
- package/dist/cdn/chunks/zh-CN-DL6ZWLGA.js.map +7 -0
- package/dist/cdn/n8n-chat.js +57 -51
- package/dist/cdn/n8n-chat.js.map +4 -4
- package/dist/create-chat.d.ts +43 -8
- package/dist/index.d.ts +2 -0
- package/dist/index.js +87 -41
- package/dist/index.js.map +1 -1
- package/dist/strings.d.ts +1 -1
- package/dist/style.css +9 -1
- package/dist/theme.css +8 -0
- package/package.json +7 -6
- package/dist/cdn/chunks/chunk-MU6PIUJC.js +0 -206
- package/dist/cdn/chunks/chunk-MU6PIUJC.js.map +0 -7
- package/dist/cdn/chunks/de-WJNL5YNK.js +0 -2
- package/dist/cdn/chunks/de-WJNL5YNK.js.map +0 -7
- package/dist/cdn/chunks/es-DV5N3CXP.js +0 -2
- package/dist/cdn/chunks/es-DV5N3CXP.js.map +0 -7
- package/dist/cdn/chunks/fr-YU75PBOR.js +0 -2
- package/dist/cdn/chunks/fr-YU75PBOR.js.map +0 -7
- package/dist/cdn/chunks/it-DZTHWNO3.js +0 -2
- package/dist/cdn/chunks/it-DZTHWNO3.js.map +0 -7
- package/dist/cdn/chunks/ja-QO442LBN.js +0 -2
- package/dist/cdn/chunks/ja-QO442LBN.js.map +0 -7
- package/dist/cdn/chunks/ko-EGFZJCXQ.js +0 -2
- package/dist/cdn/chunks/ko-EGFZJCXQ.js.map +0 -7
- package/dist/cdn/chunks/pt-BR-526LHI67.js +0 -2
- package/dist/cdn/chunks/pt-BR-526LHI67.js.map +0 -7
- package/dist/cdn/chunks/ru-5WNEMKNP.js +0 -2
- package/dist/cdn/chunks/ru-5WNEMKNP.js.map +0 -7
- package/dist/cdn/chunks/zh-CN-ABSHNLG6.js +0 -2
- package/dist/cdn/chunks/zh-CN-ABSHNLG6.js.map +0 -7
package/README.md
CHANGED
|
@@ -2,18 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
**n8n's chat widget, drawn by [aparté](https://apartejs.dev).** `createChat()` takes
|
|
4
4
|
[`@n8n/chat`](https://www.npmjs.com/package/@n8n/chat)'s options and mounts the same widget on
|
|
5
|
-
your n8n Chat Trigger — a button in the corner, or a
|
|
6
|
-
|
|
5
|
+
your n8n Chat Trigger — a button in the corner, or a block in your page. Change the import, keep
|
|
6
|
+
your call. And your workflow can answer with **rich cards**: an order, a booking, a product, the
|
|
7
|
+
weather. [See it live](https://n8n-chat.apartejs.dev).
|
|
7
8
|
|
|
8
9
|
<p>
|
|
9
|
-
<img src="docs/widget-
|
|
10
|
-
<img src="docs/widget-dark.png" alt="The same widget in its dark scheme" width="300">
|
|
10
|
+
<img src="docs/widget-card-order.png" alt="The widget open in a corner of a shop's page: the visitor asks where their order is, and the answer comes with the order's card, its items and total" width="300">
|
|
11
|
+
<img src="docs/widget-card-dark.png" alt="The same widget in its dark scheme, answering a booking with the booking's card" width="300">
|
|
11
12
|
</p>
|
|
12
13
|
|
|
14
|
+
- Rich cards your workflow writes as text: weather, order and booking, product, place, list, key
|
|
15
|
+
figures, stock, plan, score, media, link. n8n's own chat shows the same answer as a code block.
|
|
16
|
+
- Extensible: your own cards, and aparté's plugins (maths, diagrams, charts) through `setup`.
|
|
13
17
|
- Every option of `@n8n/chat` 1.43, the same defaults, the same session in the browser — and the
|
|
14
18
|
Chat nodes' messages and buttons of a workflow that answers through response nodes.
|
|
15
19
|
- Ten languages, keyboard and screen-reader access, readable contrast, a dark scheme.
|
|
16
|
-
- About
|
|
20
|
+
- About 143 KB compressed on the first load, against about 471 KB (measured 2026-10-31, `@n8n/chat` 1.44.7).
|
|
17
21
|
- A handle to drive it from your page: `open`, `close`, `toggle`, `send`, `reset`, `setDisabled`,
|
|
18
22
|
`destroy`.
|
|
19
23
|
- MIT.
|
|
@@ -23,9 +27,9 @@ keep your call.
|
|
|
23
27
|
### From a CDN — no build step
|
|
24
28
|
|
|
25
29
|
```html
|
|
26
|
-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@aparte/n8n-chat@0.
|
|
30
|
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@aparte/n8n-chat@0.3.0/dist/style.css">
|
|
27
31
|
<script type="module">
|
|
28
|
-
import { createChat } from 'https://cdn.jsdelivr.net/npm/@aparte/n8n-chat@0.
|
|
32
|
+
import { createChat } from 'https://cdn.jsdelivr.net/npm/@aparte/n8n-chat@0.3.0/dist/cdn/n8n-chat.js';
|
|
29
33
|
|
|
30
34
|
createChat({ webhookUrl: 'https://your-n8n.example.com/webhook/<id>/chat' });
|
|
31
35
|
</script>
|
|
@@ -55,10 +59,10 @@ Replace the stylesheet and the import; the call stays as it is:
|
|
|
55
59
|
|
|
56
60
|
```diff
|
|
57
61
|
- <link href="https://cdn.jsdelivr.net/npm/@n8n/chat/dist/style.css" rel="stylesheet" />
|
|
58
|
-
+ <link href="https://cdn.jsdelivr.net/npm/@aparte/n8n-chat@0.
|
|
62
|
+
+ <link href="https://cdn.jsdelivr.net/npm/@aparte/n8n-chat@0.3.0/dist/style.css" rel="stylesheet" />
|
|
59
63
|
<script type="module">
|
|
60
64
|
- import { createChat } from 'https://cdn.jsdelivr.net/npm/@n8n/chat/dist/chat.bundle.es.js';
|
|
61
|
-
+ import { createChat } from 'https://cdn.jsdelivr.net/npm/@aparte/n8n-chat@0.
|
|
65
|
+
+ import { createChat } from 'https://cdn.jsdelivr.net/npm/@aparte/n8n-chat@0.3.0/dist/cdn/n8n-chat.js';
|
|
62
66
|
createChat({ webhookUrl: 'https://your-n8n.example.com/webhook/<id>/chat' });
|
|
63
67
|
</script>
|
|
64
68
|
```
|
|
@@ -92,7 +96,7 @@ memory on the agent only, n8n answers that request with an error.
|
|
|
92
96
|
| `webhookUrl` | the Chat Trigger's production URL, ending in `/chat` | required |
|
|
93
97
|
| `webhookConfig` | `headers` sent with every request, read each time (a refreshed token is sent); `method` is accepted, every request is a POST | `{}` |
|
|
94
98
|
| `target` | where it mounts: an element, or a selector | `'#n8n-chat'` |
|
|
95
|
-
| `mode` | `'window'` (a button in the corner) or `'
|
|
99
|
+
| `mode` | `'window'` (a button in the corner) or `'embedded'` (a block in the page, filling its target; `'fullscreen'`, `@n8n/chat`'s name for it, works too) | `'window'` |
|
|
96
100
|
| `showWindowCloseButton` | a ✕ in the header, in window mode | `false` |
|
|
97
101
|
| `showWelcomeScreen` | a screen with a "New Conversation" button first | `false` |
|
|
98
102
|
| `loadPreviousSession` | keep the session in the browser and show its earlier messages | `true` |
|
|
@@ -105,7 +109,9 @@ memory on the agent only, n8n answers that request with an error.
|
|
|
105
109
|
| `messageHistory` | a conversation to show when n8n has none (text, `type: 'component'`, `transparent`) | — |
|
|
106
110
|
| `messageComponents` | functions that draw `type: 'component'` history messages | — |
|
|
107
111
|
| `metadata` | sent with every message, as `metadata` in the trigger's output | — |
|
|
108
|
-
| `allowFileUploads`, `allowedFilesMimeTypes` | let the person attach files, and which | `false`, any |
|
|
112
|
+
| `allowFileUploads`, `allowedFilesMimeTypes` | let the person attach files, and which (`'image/*,application/pdf'`, extensions too) | `false`, any |
|
|
113
|
+
| `cards` | draw the ` ```card-<name> ` blocks of an answer as [cards](#rich-cards) | `true` |
|
|
114
|
+
| `setup` | `(config) => stop?`: aparté's plugins and your own cards, on this chat ([below](#your-own-cards-and-apartés-plugins)) | — |
|
|
109
115
|
| `enableMessageActions` | "Repost" and "Reuse" on the person's own messages | `false` |
|
|
110
116
|
| `disabled` | block the input (`true`, or n8n's `{ value }`) | `false` |
|
|
111
117
|
| `beforeMessageSent`, `afterMessageSent` | hooks around each message | — |
|
|
@@ -143,9 +149,146 @@ and the workflow carries on.
|
|
|
143
149
|
on, the answer has to be a button; off, the words reach n8n as a message and the node goes on as
|
|
144
150
|
not approved — as with n8n's own widget.
|
|
145
151
|
|
|
146
|
-
|
|
147
|
-
one stops with "Embedded chat is not supported".
|
|
148
|
-
|
|
152
|
+
Up to n8n 2.45, Chat nodes run only when the Chat Trigger's **Mode** is **Hosted Chat**; an
|
|
153
|
+
embedded one stops with "Embedded chat is not supported". n8n 2.46 and 3.0 lift that limit and run
|
|
154
|
+
them in **Embedded** mode too; the widget has been tested with Hosted Chat. A button whose link
|
|
155
|
+
leads off your n8n is not shown.
|
|
156
|
+
|
|
157
|
+
## Rich cards
|
|
158
|
+
|
|
159
|
+
n8n's chat carries only text, so a card travels as text: a code block whose language names the
|
|
160
|
+
card, holding its fields as JSON, after the sentence that answers.
|
|
161
|
+
|
|
162
|
+
````md
|
|
163
|
+
Order 1001 left our warehouse two days ago.
|
|
164
|
+
|
|
165
|
+
```card-order
|
|
166
|
+
{ "number": "1001", "status": "shipped", "total": "68.00", "currency": "EUR",
|
|
167
|
+
"line_items": [{ "name": "Linen shirt", "quantity": 1, "total": "49.00" },
|
|
168
|
+
{ "name": "Canvas tote bag", "quantity": 1, "total": "19.00" }] }
|
|
169
|
+
```
|
|
170
|
+
````
|
|
171
|
+
|
|
172
|
+
The widget draws the block as the card; n8n's own chat, an email or a model reading the history back
|
|
173
|
+
shows the sentence and the code. The cards: `weather`, `order` (a booking too, with
|
|
174
|
+
`"kind": "booking"`), `product`, `place`, `items`, `stats`, `stock`, `plan`, `score`, `media`,
|
|
175
|
+
`link`. Their fields are those of [`@aparte/plugin-cards`](https://apartejs.dev/guides/cards/), whose
|
|
176
|
+
readers also take the shapes shops already return (WooCommerce and Shopify orders, schema.org
|
|
177
|
+
products). Each card's code is fetched the first time an answer uses it.
|
|
178
|
+
|
|
179
|
+
Build the block in a **Code** node from your tool's real result rather than asking the model to
|
|
180
|
+
write JSON: it is exact every time, and a setting can turn it off for a chat that does not draw
|
|
181
|
+
cards. The [shop and booking workflows](https://n8n-chat.apartejs.dev/#workflows) do it that way.
|
|
182
|
+
`cards: false` shows the blocks as code.
|
|
183
|
+
|
|
184
|
+
## Your own cards, and aparté's plugins
|
|
185
|
+
|
|
186
|
+
The widget is built with [aparté](https://apartejs.dev), and `setup` hands you its config: anything
|
|
187
|
+
aparté does, this chat can do. A card of your own is a `defineCard`, and your workflow sends it as
|
|
188
|
+
` ```card-<its name> `:
|
|
189
|
+
|
|
190
|
+
```js
|
|
191
|
+
import { createChat, defineCard, el } from '@aparte/n8n-chat';
|
|
192
|
+
|
|
193
|
+
const ticket = defineCard({
|
|
194
|
+
name: 'ticket',
|
|
195
|
+
variants: ['card'],
|
|
196
|
+
// A card draws its own padding, from the kit's variable, as the shipped cards do.
|
|
197
|
+
styles: '.aparte-ticket-card { padding: var(--aparte-rich-card-padding) } .aparte-ticket-card .id { font-weight: 600 }',
|
|
198
|
+
icon: '<svg viewBox="0 0 24 24"><path d="M4 6h16v12H4z" fill="none" stroke="currentColor"/></svg>',
|
|
199
|
+
normalize: (raw) => (raw?.id ? { id: String(raw.id), subject: String(raw.subject ?? ''), status: raw.status } : null),
|
|
200
|
+
render: (t) => [el('span', { class: 'id', text: t.id }), el('p', { text: t.subject }), el('small', { text: t.status })],
|
|
201
|
+
skeleton: () => [],
|
|
202
|
+
label: (t) => `Ticket ${t?.id ?? ''}`,
|
|
203
|
+
fallback: (t) => `Ticket ${t.id}: ${t.subject}`,
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
createChat({
|
|
207
|
+
webhookUrl: '…',
|
|
208
|
+
setup: (config) => ticket.setup({ config }),
|
|
209
|
+
});
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
Take `defineCard` from the widget's own module, as above, and from the same URL when you load
|
|
213
|
+
it from a CDN (`…/@aparte/n8n-chat@0.3.0/dist/cdn/n8n-chat.js`): the card and the chat then share
|
|
214
|
+
one copy of aparté. A kit loaded from another URL keeps its cards where the chat never looks.
|
|
215
|
+
|
|
216
|
+
````md
|
|
217
|
+
Your ticket is open.
|
|
218
|
+
|
|
219
|
+
```card-ticket
|
|
220
|
+
{ "id": "T-42", "subject": "Refund for order 1001", "status": "Waiting for us" }
|
|
221
|
+
```
|
|
222
|
+
````
|
|
223
|
+
|
|
224
|
+
The same `setup` adds aparté's plugins — maths, diagrams, charts — each in one line:
|
|
225
|
+
|
|
226
|
+
```js
|
|
227
|
+
import { setupKatex } from '@aparte/plugin-katex';
|
|
228
|
+
import 'katex/dist/katex.min.css'; // KaTeX's own sheet: without it a formula shows twice
|
|
229
|
+
import { setupMermaid } from '@aparte/plugin-mermaid';
|
|
230
|
+
|
|
231
|
+
createChat({
|
|
232
|
+
webhookUrl: '…',
|
|
233
|
+
setup: (config) => {
|
|
234
|
+
const stops = [setupKatex({}, config), setupMermaid({}, config)];
|
|
235
|
+
return () => stops.forEach((stop) => stop());
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
What `setup` returns, if a function, runs on `destroy()`.
|
|
241
|
+
|
|
242
|
+
### All of aparté, at three levels
|
|
243
|
+
|
|
244
|
+
The widget's options keep `@n8n/chat`'s names so a page switches without rewriting, but everything
|
|
245
|
+
underneath is aparté, and you can reach it:
|
|
246
|
+
|
|
247
|
+
- **The config** — `setup(config)` hands you the chat's own `AparteConfig`: plugins, cards, segment
|
|
248
|
+
and tool renderers, icons (`config.registerIcons`), the locale (`config.setLocale`), the actions on a
|
|
249
|
+
message (`config.registerAction`). See the [configuration guide](https://apartejs.dev/guides/customization/).
|
|
250
|
+
- **The elements** — `chat.element` is the widget's root; the chat inside it is aparté's
|
|
251
|
+
`<aparte-chat>`, its field `<aparte-composer>`, and their attributes are theirs to set:
|
|
252
|
+
|
|
253
|
+
```js
|
|
254
|
+
const chat = createChat({ webhookUrl: '…' });
|
|
255
|
+
chat.element.querySelector('aparte-composer')?.setAttribute('placeholder', 'Ask about your order…');
|
|
256
|
+
```
|
|
257
|
+
- **The look** — aparté's `--aparte-*` variables, beside n8n's `--chat--*` ones:
|
|
258
|
+
|
|
259
|
+
```css
|
|
260
|
+
/* n8n's variables for what n8n's widget already styles; aparté's for the rest. */
|
|
261
|
+
.aparte-n8n { --chat--message--border-radius: 14px; --aparte-font-family: 'Inter', sans-serif; }
|
|
262
|
+
/* The theme maps some aparté tokens from n8n's on the chat itself: set those there. */
|
|
263
|
+
.aparte-n8n aparte-chat { --aparte-radius-input: 20px; }
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
What does not apply here is what needs the agent's loop in the page — a tool run in the browser, an
|
|
267
|
+
approval of a tool call: an n8n workflow runs its tools itself, on n8n.
|
|
268
|
+
|
|
269
|
+
## The page the visitor is on
|
|
270
|
+
|
|
271
|
+
`metadata` is sent with every message and reaches the workflow as `$json.metadata`. Send the page,
|
|
272
|
+
and the workflow knows which product "this one" is:
|
|
273
|
+
|
|
274
|
+
```js
|
|
275
|
+
createChat({
|
|
276
|
+
webhookUrl: '…',
|
|
277
|
+
metadata: { page: location.href, title: document.title },
|
|
278
|
+
});
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
## In the page, not in a corner
|
|
282
|
+
|
|
283
|
+
`mode: 'embedded'` puts the chat in your page as a block, filling its `target`, which needs a
|
|
284
|
+
height. `'fullscreen'`, `@n8n/chat`'s name for the same mode, works too.
|
|
285
|
+
|
|
286
|
+
```html
|
|
287
|
+
<div id="support-chat" style="height: 600px"></div>
|
|
288
|
+
<script type="module">
|
|
289
|
+
createChat({ webhookUrl: '…', target: '#support-chat', mode: 'embedded' });
|
|
290
|
+
</script>
|
|
291
|
+
```
|
|
149
292
|
|
|
150
293
|
## Files
|
|
151
294
|
|
|
@@ -153,6 +296,75 @@ With `allowFileUploads`, the visitor picks, drops or pastes files, and n8n recei
|
|
|
153
296
|
`files`, as from n8n's widget. A file can be removed with the keyboard; a picture or a file the
|
|
154
297
|
visitor sent opens in a new tab.
|
|
155
298
|
|
|
299
|
+
## Keeping bots out
|
|
300
|
+
|
|
301
|
+
A public Chat Trigger answers anyone who finds its URL, and every answer can cost you a call to
|
|
302
|
+
your AI model. [Cloudflare Turnstile](https://www.cloudflare.com/application-services/products/turnstile/)
|
|
303
|
+
— free, and usually invisible to people — lets the workflow stop a bot before the model runs. The
|
|
304
|
+
widget needs no option for it: `metadata` is read at each send and `beforeMessageSent` is awaited,
|
|
305
|
+
so the page puts a fresh token in `metadata` before every message.
|
|
306
|
+
|
|
307
|
+
1. In Cloudflare, add a Turnstile widget for your site's hostname. You get a **site key** (for the
|
|
308
|
+
page) and a **secret key** (for n8n).
|
|
309
|
+
2. In the page:
|
|
310
|
+
|
|
311
|
+
```html
|
|
312
|
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@aparte/n8n-chat@0.3.0/dist/style.css">
|
|
313
|
+
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit"></script>
|
|
314
|
+
<div id="turnstile"></div>
|
|
315
|
+
<script type="module">
|
|
316
|
+
import { createChat } from 'https://cdn.jsdelivr.net/npm/@aparte/n8n-chat@0.3.0/dist/cdn/n8n-chat.js';
|
|
317
|
+
|
|
318
|
+
// A Turnstile token is single-use and lasts five minutes: ask for a new one before each message.
|
|
319
|
+
let widgetId;
|
|
320
|
+
let pending;
|
|
321
|
+
function turnstileToken() {
|
|
322
|
+
return new Promise((resolve, reject) => {
|
|
323
|
+
pending = { resolve, reject };
|
|
324
|
+
if (widgetId === undefined) {
|
|
325
|
+
widgetId = turnstile.render('#turnstile', {
|
|
326
|
+
sitekey: '<your site key>',
|
|
327
|
+
execution: 'execute',
|
|
328
|
+
appearance: 'interaction-only', // shown only when Cloudflare needs the visitor to click
|
|
329
|
+
callback: (token) => pending.resolve(token),
|
|
330
|
+
'error-callback': () => pending.reject(new Error('Turnstile could not check this visitor')),
|
|
331
|
+
});
|
|
332
|
+
} else {
|
|
333
|
+
turnstile.reset(widgetId);
|
|
334
|
+
}
|
|
335
|
+
turnstile.execute('#turnstile');
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const metadata = {};
|
|
340
|
+
createChat({
|
|
341
|
+
webhookUrl: 'https://your-n8n.example.com/webhook/<id>/chat',
|
|
342
|
+
metadata,
|
|
343
|
+
beforeMessageSent: async () => {
|
|
344
|
+
metadata.turnstileToken = await turnstileToken();
|
|
345
|
+
},
|
|
346
|
+
});
|
|
347
|
+
</script>
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
3. In the workflow, between the Chat Trigger and your agent:
|
|
351
|
+
- an **HTTP Request** node: `POST https://challenges.cloudflare.com/turnstile/v0/siteverify`,
|
|
352
|
+
body as JSON:
|
|
353
|
+
`{{ JSON.stringify({ secret: '<your secret key>', response: $json.metadata?.turnstileToken ?? '' }) }}`
|
|
354
|
+
(anyone who can open the workflow can read the secret there);
|
|
355
|
+
- an **If** node on `{{ $json.success }}` is true;
|
|
356
|
+
- on its true branch, an **Edit Fields (Set)** node in JSON mode that gives the trigger's message
|
|
357
|
+
back, so the agent and its memory read `chatInput` and `sessionId` as before:
|
|
358
|
+
`{{ JSON.stringify($('When chat message received').item.json) }}`;
|
|
359
|
+
- then your agent. The false branch ends there: nothing reaches the model.
|
|
360
|
+
|
|
361
|
+
When Cloudflare refuses a visitor, the widget shows its usual "couldn't answer" message and nothing
|
|
362
|
+
is sent. A visitor's reply to a waiting Chat node goes over the execution's WebSocket, which was
|
|
363
|
+
opened by a checked message, so it needs no token. A bot that calls the webhook directly still
|
|
364
|
+
starts an execution, a short one (on n8n Cloud, executions count against your plan); only a proxy in
|
|
365
|
+
front of n8n stops it earlier. Checked on n8n 2.45.7 with Cloudflare's test keys, text messages
|
|
366
|
+
only: with file uploads, check that your agent still receives the files after the Set node.
|
|
367
|
+
|
|
156
368
|
## Languages
|
|
157
369
|
|
|
158
370
|
English, French, Spanish, Portuguese (Brazil), German, Italian, Russian, Japanese, Korean and
|
|
@@ -180,7 +392,7 @@ or Angular components, inside an `.aparte-n8n` element.
|
|
|
180
392
|
|
|
181
393
|
## Compared with `@n8n/chat`
|
|
182
394
|
|
|
183
|
-
| | `@n8n/chat` 1.43 | `@aparte/n8n-chat` |
|
|
395
|
+
| | `@n8n/chat` (options checked on 1.43.3, weight on 1.44.7) | `@aparte/n8n-chat` |
|
|
184
396
|
| --- | --- | --- |
|
|
185
397
|
| Drive it from the page | — | `open`, `close`, `toggle`, `send`, `reset`, `setDisabled`, `destroy` |
|
|
186
398
|
| A new conversation | not until the browser's storage is cleared | `reset()` |
|
|
@@ -190,7 +402,8 @@ or Angular components, inside an `.aparte-n8n` element.
|
|
|
190
402
|
| Keyboard and screen reader | buttons without names | named controls, a labelled window, a modal sheet on a phone |
|
|
191
403
|
| Contrast of the visitor's bubble | 2.55:1 | 4.7:1 (WCAG AA) |
|
|
192
404
|
| Dark scheme | — | ✓ |
|
|
193
|
-
|
|
|
405
|
+
| Rich cards | the JSON as text | drawn: order, booking, product, weather… |
|
|
406
|
+
| Weight, first load (compressed) | ~471 KB | ~143 KB |
|
|
194
407
|
| License | Sustainable Use License | MIT |
|
|
195
408
|
|
|
196
409
|
## Versions
|
|
@@ -201,6 +414,7 @@ tested is what your visitors run.
|
|
|
201
414
|
|
|
202
415
|
## Links
|
|
203
416
|
|
|
417
|
+
- [n8n-chat.apartejs.dev](https://n8n-chat.apartejs.dev) — the live demo, and workflows to start from.
|
|
204
418
|
- [aparté](https://apartejs.dev) — the library it is built with.
|
|
205
419
|
- [`@aparte/runner-n8n`](https://apartejs.dev/providers/agent/n8n/) — the n8n protocol alone, to put
|
|
206
420
|
an n8n workflow behind a chat you compose yourself.
|
package/dist/cards.d.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{a as n}from"./chunk-VLHCX6EH.js";function p(a,{name:s,options:i,value:c,label:l}){if(!a.live||i.length===0)return null;let r=n("div",{class:"aparte-rich-card__segmented",attrs:{role:"group"}});l&&a.bind(r,l,"aria-label");for(let e of i){let d=e.value===c,t=n("button",{class:"aparte-rich-card__segment",attrs:{type:"button","aria-pressed":d?"true":"false","data-card-key":`${s}:${e.value}`}});typeof e.label=="function"?a.bind(t,e.label):t.textContent=e.label,t.addEventListener("click",()=>{if(t.getAttribute("aria-pressed")!=="true"){for(let u of r.querySelectorAll(".aparte-rich-card__segment"))u.setAttribute("aria-pressed",u===t?"true":"false");a.change(s,e.value)}}),r.append(t)}return r}export{p as a};
|
|
2
|
+
//# sourceMappingURL=chunk-E55C7UF2.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../node_modules/.pnpm/@aparte+plugin-cards@0.29.3_@aparte+core@0.29.3/node_modules/@aparte/plugin-cards/src/kit/controls.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * Controls a card can carry: a row of choices, a switch, a slider. Each reports through\n * `ctx.change(name, value)`, so the card fires `aparte-card-change` and the app's\n * `onChange` answers with new data, drawn in place — a stock chart's range, a light's\n * brightness.\n *\n * A control is drawn only when something will answer it (`ctx.live`): without an\n * `onChange`, a button that does nothing would be a promise the card cannot keep\n * (ratified decision #8). Each control carries a `data-card-key`, so a redraw puts the\n * focus back on the control that was pressed.\n */\nimport type { CardContext } from './define.js';\nimport { el } from './dom.js';\n\nexport interface SegmentedOptions {\n /** The control's name in the change (`range`). */\n name: string;\n /** The choices; a label is a string or a function read again on a language switch. */\n options: Array<{ value: string; label: string | (() => string) }>;\n /** The choice in force. */\n value?: string;\n /** The group's accessible name. */\n label?: () => string;\n}\n\n/** A row of choices, one pressed (`aria-pressed`). `null` when nothing would answer it. */\nexport function segmented(ctx: CardContext, { name, options, value, label }: SegmentedOptions): HTMLElement | null {\n if (!ctx.live || options.length === 0) return null;\n const group = el('div', { class: 'aparte-rich-card__segmented', attrs: { role: 'group' } });\n if (label) ctx.bind(group, label, 'aria-label');\n for (const option of options) {\n const pressed = option.value === value;\n const button = el('button', {\n class: 'aparte-rich-card__segment',\n attrs: { type: 'button', 'aria-pressed': pressed ? 'true' : 'false', 'data-card-key': `${name}:${option.value}` },\n });\n if (typeof option.label === 'function') ctx.bind(button, option.label);\n else button.textContent = option.label;\n button.addEventListener('click', () => {\n if (button.getAttribute('aria-pressed') === 'true') return;\n // Pressed at once, before the app answers: a second press reads what was chosen.\n for (const other of group.querySelectorAll('.aparte-rich-card__segment')) other.setAttribute('aria-pressed', other === button ? 'true' : 'false');\n ctx.change(name, option.value);\n });\n group.append(button);\n }\n return group;\n}\n\nexport interface ToggleOptions {\n name: string;\n checked: boolean;\n /** What it switches (\"Living-room light\"). */\n label: () => string;\n}\n\n/** A switch (`role=\"switch\"`). `null` when nothing would answer it. */\nexport function toggle(ctx: CardContext, { name, checked, label }: ToggleOptions): HTMLElement | null {\n if (!ctx.live) return null;\n const button = el('button', {\n class: 'aparte-rich-card__toggle',\n attrs: { type: 'button', role: 'switch', 'aria-checked': checked ? 'true' : 'false', 'data-card-key': name },\n }, el('span', { class: 'aparte-rich-card__toggle-thumb', attrs: { 'aria-hidden': 'true' } }));\n ctx.bind(button, label, 'aria-label');\n button.addEventListener('click', () => {\n // Flipped at once, before the app answers: a second click before the answer flips it\n // back, rather than sending the same state twice.\n const next = button.getAttribute('aria-checked') !== 'true';\n button.setAttribute('aria-checked', next ? 'true' : 'false');\n ctx.change(name, next);\n });\n return button;\n}\n\nexport interface SliderOptions {\n name: string;\n value: number;\n min?: number;\n max?: number;\n step?: number;\n label: () => string;\n /** How the value reads beside it, and to a screen reader (\"60 %\"). */\n format?: (value: number) => string;\n}\n\n/**\n * A slider: the value beside it follows while it moves, the change is reported once it is\n * let go (`change`, not `input`) — one request for a drag, not one per pixel.\n */\nexport function slider(ctx: CardContext, { name, value, min = 0, max = 100, step = 1, label, format = String }: SliderOptions): HTMLElement | null {\n if (!ctx.live) return null;\n const input = el('input', {\n class: 'aparte-rich-card__slider-input',\n attrs: { type: 'range', min, max, step, value, 'data-card-key': name },\n });\n const shown = el('output', { class: 'aparte-rich-card__slider-value' });\n const name_ = el('span', { class: 'aparte-rich-card__slider-label' });\n ctx.bind(name_, label);\n ctx.bind(input, label, 'aria-label');\n const paint = (v: number) => {\n shown.textContent = format(v);\n input.setAttribute('aria-valuetext', format(v));\n };\n ctx.bind(shown, () => format(Number(input.value)));\n paint(value);\n input.addEventListener('input', () => paint(Number(input.value)));\n input.addEventListener('change', () => ctx.change(name, Number(input.value)));\n return el('label', { class: 'aparte-rich-card__slider' }, name_, input, shown);\n}\n"],
|
|
5
|
+
"mappings": "wCA0BO,SAASA,EAAUC,EAAkB,CAAE,KAAAC,EAAM,QAAAC,EAAS,MAAAC,EAAO,MAAAC,CAAA,EAA+C,CAC/G,GAAI,CAACJ,EAAI,MAAQE,EAAQ,SAAW,EAAG,OAAO,KAC9C,IAAMG,EAAQC,EAAG,MAAO,CAAE,MAAO,8BAA+B,MAAO,CAAE,KAAM,OAAA,CAAA,CAAW,EACtFF,GAAOJ,EAAI,KAAKK,EAAOD,EAAO,YAAY,EAC9C,QAAWG,KAAUL,EAAS,CAC1B,IAAMM,EAAUD,EAAO,QAAUJ,EAC3BM,EAASH,EAAG,SAAU,CACxB,MAAO,4BACP,MAAO,CAAE,KAAM,SAAU,eAAgBE,EAAU,OAAS,QAAS,gBAAiB,GAAGP,CAAI,IAAIM,EAAO,KAAK,EAAA,CAAG,CACnH,EACG,OAAOA,EAAO,OAAU,WAAAP,EAAgB,KAAKS,EAAQF,EAAO,KAAK,EAChEE,EAAO,YAAcF,EAAO,MACjCE,EAAO,iBAAiB,QAAS,IAAM,CACnC,GAAIA,EAAO,aAAa,cAAc,IAAM,OAE5C,SAAWC,KAASL,EAAM,iBAAiB,4BAA4B,EAAGK,EAAM,aAAa,eAAgBA,IAAUD,EAAS,OAAS,OAAO,EAChJT,EAAI,OAAOC,EAAMM,EAAO,KAAK,EACjC,CAAC,EACDF,EAAM,OAAOI,CAAM,CACvB,CACA,OAAOJ,CACX",
|
|
6
|
+
"names": ["segmented", "ctx", "name", "options", "value", "label", "group", "el", "option", "pressed", "button", "other"]
|
|
7
|
+
}
|