@squadico/chatbot 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/README.md +502 -0
  2. package/dist/chatbot.global.js +8 -0
  3. package/dist/chatbot.global.js.map +1 -0
  4. package/dist/components/Approval.d.ts +37 -0
  5. package/dist/components/Approval.d.ts.map +1 -0
  6. package/dist/components/ChatbotWidget.d.ts +19 -0
  7. package/dist/components/ChatbotWidget.d.ts.map +1 -0
  8. package/dist/components/Choices.d.ts +27 -0
  9. package/dist/components/Choices.d.ts.map +1 -0
  10. package/dist/components/Composer.d.ts +35 -0
  11. package/dist/components/Composer.d.ts.map +1 -0
  12. package/dist/components/Confirm.d.ts +16 -0
  13. package/dist/components/Confirm.d.ts.map +1 -0
  14. package/dist/components/MessageList.d.ts +32 -0
  15. package/dist/components/MessageList.d.ts.map +1 -0
  16. package/dist/components/RichText.d.ts +19 -0
  17. package/dist/components/RichText.d.ts.map +1 -0
  18. package/dist/element.d.ts +9 -0
  19. package/dist/element.d.ts.map +1 -0
  20. package/dist/index.cjs +1 -0
  21. package/dist/index.d.ts +15 -0
  22. package/dist/index.d.ts.map +1 -0
  23. package/dist/index.js +2 -0
  24. package/dist/jsx.d.ts +57 -0
  25. package/dist/jsx.d.ts.map +1 -0
  26. package/dist/lib/client.d.ts +217 -0
  27. package/dist/lib/client.d.ts.map +1 -0
  28. package/dist/lib/confirm.d.ts +17 -0
  29. package/dist/lib/confirm.d.ts.map +1 -0
  30. package/dist/lib/context.d.ts +40 -0
  31. package/dist/lib/context.d.ts.map +1 -0
  32. package/dist/lib/conversation.d.ts +49 -0
  33. package/dist/lib/conversation.d.ts.map +1 -0
  34. package/dist/lib/i18n.d.ts +152 -0
  35. package/dist/lib/i18n.d.ts.map +1 -0
  36. package/dist/lib/openState.d.ts +26 -0
  37. package/dist/lib/openState.d.ts.map +1 -0
  38. package/dist/lib/uid.d.ts +8 -0
  39. package/dist/lib/uid.d.ts.map +1 -0
  40. package/dist/lib/useChat.d.ts +71 -0
  41. package/dist/lib/useChat.d.ts.map +1 -0
  42. package/dist/react.cjs +2 -0
  43. package/dist/react.cjs.map +1 -0
  44. package/dist/react.d.ts +30 -0
  45. package/dist/react.d.ts.map +1 -0
  46. package/dist/react.js +70 -0
  47. package/dist/react.js.map +1 -0
  48. package/dist/src-CN1vDssu.js +2492 -0
  49. package/dist/src-CN1vDssu.js.map +1 -0
  50. package/dist/src-DvRdkFTu.cjs +8 -0
  51. package/dist/src-DvRdkFTu.cjs.map +1 -0
  52. package/dist/types.d.ts +301 -0
  53. package/dist/types.d.ts.map +1 -0
  54. package/package.json +76 -0
package/README.md ADDED
@@ -0,0 +1,502 @@
1
+ # @squadico/chatbot
2
+
3
+ Standalone web component for the Squadico assistant: a floating button that
4
+ opens the chatbot in a sidebar on the right of the screen. Installable on any
5
+ front end — plain HTML, Next.js, Vue 2, Vue 3 — without imposing a framework.
6
+
7
+ ```bash
8
+ npm install @squadico/chatbot
9
+ ```
10
+
11
+ ```html
12
+ <squadico-chatbot
13
+ embed-key="sq_pub_paste_yours_here"
14
+ context='{"workspace": {"id": "WS-1", "name": "Acme"}}'
15
+ ></squadico-chatbot>
16
+ ```
17
+
18
+ ## What the package contains
19
+
20
+ | Entry point | Contents | For whom |
21
+ |--------------------------------|------------------------------------------------|-------------------------------|
22
+ | `@squadico/chatbot` | Registers `<squadico-chatbot>` | Every host |
23
+ | `@squadico/chatbot/react` | `<SquadicoChatbot />`, typed props + callbacks | Next.js / React |
24
+ | `@squadico/chatbot/standalone` | All-in-one IIFE bundle | `<script>` tag, CDN |
25
+ | `@squadico/chatbot/jsx` | JSX types for the tag | React, using the tag directly |
26
+
27
+ Weight of the standalone bundle: **~22 kB gzip**, everything included (Preact
28
+ runtime + CSS). The budget is checked at build time by `npm run size`.
29
+
30
+ ## Implementation choices
31
+
32
+ - **Preact, not React.** The API is React's (hooks, JSX) but the runtime weighs
33
+ ~4 kB instead of ~45. It is *bundled*, never a peer dependency: the widget has
34
+ to work on a Vue 2 host with no React runtime at all. The `/react` subpath is
35
+ the only one importing `react`, as an **optional** peer dependency — it is a
36
+ plain adapter around the custom element, not a second widget.
37
+ - **Shadow DOM.** The widget styles do not leak into the page, and the page CSS
38
+ cannot break the widget. The only extension points are the `--sqc-*` variables
39
+ (see below).
40
+ - **No authentication at all.** The widget is the *public* surface: it carries
41
+ the application's `embed-key`, and the conversation it opens hands back a
42
+ visitor token it presents on every later request. There is no bearer token to
43
+ provide and no auth SDK to embed — which also means the key is safe in client
44
+ HTML: it identifies the surface and authorizes nothing. Which pages may open a
45
+ conversation with it is decided by the origin allowlist configured beside it in
46
+ Squadico, and the browser is the one that tells the API where the page is.
47
+ - **The server names the conversation.** Its id comes back from the open call: a
48
+ visitor allowed to name their own conversation is a visitor choosing which one
49
+ to read.
50
+ - **Who the visitor is, when the page knows.** `user` — `{ id, name?, digest? }` —
51
+ is sent once, when the panel opens, and labels every message that person writes.
52
+ Without a `digest` it is attribution and never authorization: anyone can open
53
+ devtools and send another id, so it groups conversations and names turns in a
54
+ transcript and decides nothing. Changing the id opens a new conversation rather
55
+ than relabelling the old one. Optional by design: a marketing site has anonymous
56
+ visitors, and their conversations are anonymous. See *Verified visitors* below
57
+ for the application that asks for proof.
58
+ - **The conversation survives a reload.** Its id and token are remembered for the
59
+ tab, and a panel that finds one reads the transcript back rather than opening a
60
+ second conversation and orphaning the first. Remembered per application and per
61
+ claimed user: switch either and the panel starts a new conversation. Closing the
62
+ tab starts one too — that token is the whole of the authorization on somebody's
63
+ conversation, and it does not belong in a shared machine's storage for weeks.
64
+ - **Several agents, addressed by name.** An application can hold more than one
65
+ agent, each with a handle. A message *leading* with one goes to it —
66
+ `@sales what does the pro plan cost?` — and anything else goes to the agent the
67
+ organization set as the one answering plain text. Typing `@` offers the handles
68
+ that application chose to show; a name nobody answers to still gets an answer,
69
+ with the names that would have worked shown under it. `agent` opens the panel on
70
+ one of them, for a page that already knows who should answer.
71
+ - **The name is read from the message, server-side.** The picker writes text and
72
+ nothing else, so a handle chosen from the list is indistinguishable from one
73
+ somebody spelled out — and a page cannot pick an agent the application does not
74
+ offer.
75
+ - **No backdrop.** The sidebar is a docked panel, not a modal: the page stays
76
+ readable and clickable during the conversation.
77
+ - **Markdown rendered, never HTML injected.** The agent's answers (headings,
78
+ lists, listing tables, code, links) are converted into Preact nodes — hence
79
+ escaped by construction — and `href`s are limited to `http`/`https`/`mailto`.
80
+ No Markdown library: the subset the agent produces is enough.
81
+ - **Checkboxes = clickable options.** When the agent offers candidates
82
+ (`- [ ] Acme workspace`), the widget turns them into buttons: clicking sends
83
+ the exact label as the turn's answer, since the agent matches the answer
84
+ against the candidates by name. Single choice, and only the last answer's
85
+ options are live — those of earlier turns stay displayed, locked on what was
86
+ picked.
87
+ - **Closed questions = Yes / No buttons.** When the last line of an answer is a
88
+ confirmation request ("Do you confirm the creation?"), the widget adds two
89
+ buttons sending `yes` or `no` — the agent recognizes those bare answers without
90
+ an LLM call. The detection is deliberately narrow (`src/lib/confirm.ts`): an
91
+ open question must not show buttons.
92
+ - **Pending tool call = two buttons, and nothing written.** A tool the
93
+ organization marked as needing approval stops the turn before it runs: the
94
+ answer is the agent's recap, and the frame beside it says which call is
95
+ waiting. The widget shows "Yes, apply" and "No, cancel" and sends the decision
96
+ to `POST /public/conversations/{id}/approvals`. Nothing has left for the tool until
97
+ then — and typing something else instead is a decision too: the call was never
98
+ approved, so it never runs, and the backend closes it as the new question goes
99
+ out.
100
+ - **Several agents, one panel.** An application can hold more than one agent, and
101
+ a `done` frame says which one answered — so the transcript signs each answer
102
+ with its own name and emoji, and `chatbot-message` carries the handle for a
103
+ host page that wants to react per agent. A single-agent integration renders
104
+ exactly as it did before.
105
+
106
+ ## Attributes
107
+
108
+ All optional. An attribute is a string; object or function values are set as
109
+ **properties** (see the next section).
110
+
111
+ | Attribute | Default | Role |
112
+ |-------------------|----------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
113
+ | `embed-key` | — (**required**) | Public key of the Squadico application this page embeds. Safe in client HTML: it identifies the surface and authorizes nothing |
114
+ | `context` | — | Page context, as a serialised JSON object. An entry is written `"workspace": {"id": "…", "name": "…"}` or, without its name, `"workspaceId": "…"` |
115
+ | `user` | — | Who the page has signed in, as JSON: `'{"id":"42","name":"Ada"}'`. A label for the transcript, never an authorization — unless the application verifies visitors, where it also carries `digest` (see *Verified visitors*) |
116
+ | `agent` | the application's default agent | Handle the panel opens on — `agent="sales"` on a pricing page. A name nobody answers to opens on the default agent rather than refusing; the visitor can address somebody else afterwards |
117
+ | `heading` | translated (`Assistant` in `en`) | Header title. Empty hands it back to the translation |
118
+ | `subheading` | `Squadico` | Subtitle |
119
+ | `assistant-name` | translated (`Assistant` in `en`) | Name the assistant signs its answers with |
120
+ | `greeting` | translated greeting | First message from the assistant |
121
+ | `placeholder` | translated | Input placeholder |
122
+ | `suggestions` | — | `'["a","b"]'` or `a\|b` |
123
+ | `width` | `min(392px, 100vw)` | Sidebar width |
124
+ | `accent` | black (white in `dark`) | Accent colour: the launcher, the visitor's bubbles and the main buttons. Text on it turns white or dark by itself |
125
+ | `theme` | `light` | `light` \| `dark` \| `auto` |
126
+ | `position` | `bottom-right` | Anchor of the floating button (the sidebar stays on the right) |
127
+ | `open` | absent | Presence = open at mount, for a first visit — a panel the user opened or closed themselves wins |
128
+ | `persist-open` | `true` | The panel stays open (or closed) across a refresh or a page change, for the lifetime of the tab. `persist-open="false"` → the state starts from `open` on every mount |
129
+ | `locale` | browser languages, then `en` | Interface language and date locale, in BCP-47. When absent, `navigator.languages` is read in the user's order of preference. Three languages ship — `en`, `fr`, `es`; anything else falls back to English. The region is only read by `Intl`: `fr-CA` = French copy, Canadian dates |
130
+
131
+ ### Icon of the floating button
132
+
133
+ The assistant spark by default. It is replaced by a child of the custom element
134
+ carrying `slot="launcher-icon"` — not by an attribute: the widget renders inside
135
+ a shadow root, where a host component cannot be mounted. Projected through the
136
+ slot, the element stays in the page's tree (its CSS applies) and inherits the
137
+ button color, so a `currentColor` is enough. The frame is 26 px.
138
+
139
+ ```html
140
+ <squadico-chatbot>
141
+ <svg slot="launcher-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor">
142
+ <path d="M3 12h13l-2-4h4l3 4v4H3z" />
143
+ </svg>
144
+ </squadico-chatbot>
145
+ ```
146
+
147
+ With the React wrapper the same thing is said as a prop — any React node, which
148
+ the adapter projects into that slot:
149
+
150
+ ```tsx
151
+ <SquadicoChatbot launcherIcon={<BotIcon className="size-6" />} />
152
+ ```
153
+
154
+ A child with no `slot` is projected nowhere, and therefore not displayed.
155
+
156
+ ### Properties and imperative API
157
+
158
+ ```ts
159
+ const chatbot = document.querySelector('squadico-chatbot')
160
+
161
+ chatbot.context = {
162
+ workspace: { id: 'WS-1', name: 'Acme' },
163
+ anyOtherKey: 'forwarded as-is',
164
+ }
165
+ chatbot.embedKey = 'sq_pub_…'
166
+ chatbot.user = { id: 'host-42', name: 'Ada' } // or leave it out: anonymous
167
+
168
+ chatbot.openChat()
169
+ chatbot.closeChat()
170
+ chatbot.toggleChat()
171
+ chatbot.reset() // clears the history; the next message opens a new conversation
172
+ chatbot.sendMessage('List my agents') // opens the sidebar and sends
173
+ chatbot.open // boolean, read/write
174
+ chatbot.sessionId // string — the server's id, empty until the panel has opened
175
+ ```
176
+
177
+ ### Events
178
+
179
+ Emitted on the element, `bubbles` + `composed` (they cross the shadow root).
180
+
181
+ | Event | `detail` |
182
+ |----------------------------------|-----------------------------------|
183
+ | `chatbot-open` / `chatbot-close` | `{ sessionId }` |
184
+ | `chatbot-message` | `{ role, content, sessionId, agent? }` — `agent` is the handle that answered, on an application holding several |
185
+ | `chatbot-error` | `{ message, status?, sessionId }` |
186
+ | `chatbot-reset` | `{ sessionId }` |
187
+
188
+ ### CSS customisation
189
+
190
+ The variables are set on the host element, from the page:
191
+
192
+ ```css
193
+ squadico-chatbot {
194
+ --sqc-accent: #2563eb; /* default: black, white in the dark theme */
195
+ --sqc-width: 480px;
196
+ --sqc-radius: 12px; /* message bubbles; default 16px */
197
+ --sqc-z: 9999;
198
+ --sqc-font-family: 'Inter', sans-serif; /* default: Plus Jakarta Sans */
199
+ --sqc-font-heading: 'Inter', sans-serif; /* header, Markdown headings; default: the body family */
200
+ }
201
+ ```
202
+
203
+ The widget applies the Squadico design system (v2, near-monochrome): black for
204
+ action (slate-800 on hover), slate for surfaces, rules and secondary text,
205
+ blue-600 for links, a neutral focus ring, and its radius hierarchy — 16px for
206
+ the bubbles, 12px for buttons and inputs, full for the launcher and badges. The
207
+ values are hard-copied into `src/styles.css`, and the package depends on no CSS
208
+ variable of the host application. The typeface is **referenced, never
209
+ downloaded** — the widget issues no font request. If the page loads Plus Jakarta
210
+ Sans itself, `font-family` resolution crosses the shadow root and the widget
211
+ benefits; otherwise the system fallbacks apply.
212
+
213
+ ## Integration
214
+
215
+ ### HTML / CDN
216
+
217
+ ```html
218
+ <script src="…/chatbot.global.js"></script>
219
+ <squadico-chatbot
220
+ embed-key="sq_pub_paste_yours_here"
221
+ context='{"workspaceId":"WS-1"}'
222
+ ></squadico-chatbot>
223
+ ```
224
+
225
+ ### Next.js
226
+
227
+ From a client component (the wrapper already carries `'use client'`):
228
+
229
+ ```tsx
230
+ 'use client'
231
+ import { SquadicoChatbot } from '@squadico/chatbot/react'
232
+
233
+ export function Assistant({ workspace, me }: { workspace: Workspace; me?: User }) {
234
+ return (
235
+ <SquadicoChatbot
236
+ embedKey={process.env.NEXT_PUBLIC_SQUADICO_EMBED_KEY}
237
+ context={{ workspace }}
238
+ // Optional: names this visitor's turns in the transcript. A label, not a
239
+ // login — leave it out and the conversation is anonymous.
240
+ user={me ? { id: me.id, name: me.name } : undefined}
241
+ onMessage={(detail) => analytics.track('chatbot_message', detail)}
242
+ />
243
+ )
244
+ }
245
+ ```
246
+
247
+ Importing the main entry point is safe server-side too: registering the custom
248
+ element is guarded by a `typeof window` check, so no
249
+ `ReferenceError: HTMLElement is not defined` during SSR.
250
+
251
+ ### Vue 3
252
+
253
+ Declare the tag as a custom element so the compiler does not look for a Vue
254
+ component:
255
+
256
+ ```ts
257
+ // vite.config.ts
258
+ vue({
259
+ template: {
260
+ compilerOptions: { isCustomElement: (tag) => tag === 'squadico-chatbot' },
261
+ },
262
+ })
263
+ ```
264
+
265
+ ```vue
266
+ <script setup lang="ts">
267
+ import '@squadico/chatbot'
268
+ import { ref, watchEffect } from 'vue'
269
+ import type { SquadicoChatbotElement } from '@squadico/chatbot'
270
+
271
+ const chatbot = ref<SquadicoChatbotElement | null>(null)
272
+ const props = defineProps<{ workspace: { id: string; name: string } }>()
273
+
274
+ watchEffect(() => {
275
+ if (chatbot.value) chatbot.value.context = { workspace: props.workspace }
276
+ })
277
+ </script>
278
+
279
+ <template>
280
+ <squadico-chatbot
281
+ ref="chatbot"
282
+ embed-key="sq_pub_paste_yours_here"
283
+ @chatbot-message="(e) => console.log(e.detail)"
284
+ />
285
+ </template>
286
+ ```
287
+
288
+ ### Vue 2
289
+
290
+ ```js
291
+ // main.js
292
+ Vue.config.ignoredElements = ['squadico-chatbot']
293
+ ```
294
+
295
+ The package exposes a CJS build, resolved by webpack 4, which ignores the
296
+ `exports.import` field. The rest is identical to Vue 3: `$refs` to set `context`
297
+ and `user`, `@chatbot-message` for the events.
298
+
299
+ ## Contract with the API
300
+
301
+ The widget talks directly to the Squadico API (FastAPI), at
302
+ `https://api.squadico.com` — written `{api}` below — on the seven routes the
303
+ public surface serves.
304
+
305
+ The API is not an option. There is no `api-url` attribute and no `apiUrl` prop: a
306
+ page that could set one could point a Squadico panel at a server that is not
307
+ Squadico's, and everybody embedding the widget talks to the same API anyway. The
308
+ URL is written into the bundle at build time (`config/api-url.ts`); `API_URL`
309
+ exports it for a page that wants to read it.
310
+
311
+ - `POST {api}/public/applications/{embed-key}/conversations` — opens the
312
+ conversation. Body: `{ "user": { "id": "…", "name": "…" }, "agent": "sales" }`,
313
+ or `{}` for an anonymous visitor on the default agent. Answers the
314
+ conversation's id, the visitor token, the answering agent's name and welcome
315
+ message, the handle it opened on, the transcript as it stands, and the handles
316
+ this application offers (empty until an organization ticks one). The browser's
317
+ `Origin` decides whether the page may do this at all
318
+ - `POST {api}/public/conversations/{id}/messages` — one turn, streamed. Body:
319
+ `{ "content": "…", "context": { … }, "attachment_ids": ["…"] }`. Header:
320
+ `X-Visitor-Token`. A turn carries the files it *names*: anything else the
321
+ conversation is still holding stays where it is. `content` may be empty when
322
+ the turn names a file — a file on its own is a question about the file — and a
323
+ turn carrying neither words nor a file is refused
324
+ - `GET {api}/public/conversations/{id}` — the transcript, read back after a reload.
325
+ Header: `X-Visitor-Token`. The open response minus the token, which the caller
326
+ already holds
327
+ - `POST {api}/public/conversations/{id}/approvals` — the decision on a paused tool
328
+ call: `{ "tool_call_id": "…", "approved": true }`, same header, same frames
329
+ - `POST {api}/public/conversations/{id}/attachments` — one file, `multipart/form-data`
330
+ under the field `file`, same header. Answers what was *accepted*: the id the
331
+ message will name, and the kind and type read off the bytes on the server — never
332
+ what the browser announced. Refusals are distinct on purpose: `415` the format,
333
+ `413` the size, `409` this message already carries as many files as it may,
334
+ `422` the file would not read
335
+ - `DELETE {api}/public/conversations/{id}/attachments/{attachment-id}` — takes a file
336
+ back out of the draft, bytes and all. `409` once it has been sent: a file that
337
+ travelled with a turn is part of the transcript
338
+ - `GET {api}/public/conversations/{id}/attachments/{attachment-id}/content` — the
339
+ bytes, same header
340
+
341
+ The download route is why an attachment carries no URL. It is opened by the
342
+ visitor token, which travels in a header — and a header is exactly what an
343
+ `<img src>` cannot send, so the panel fetches the file itself and draws an object
344
+ URL it revokes when the bubble goes. An integration reading the transcript has to
345
+ do the same.
346
+
347
+ The `done` frame of a message that addressed a name nobody answers to also
348
+ carries `unknown_handle` and the `handles` that do — beside the answer the default
349
+ agent gave, never instead of it.
350
+
351
+ Both streamed routes answer `text/event-stream` with four named events:
352
+
353
+ ```
354
+ event: token
355
+ data: {"text":"Le catalogue "}
356
+
357
+ event: done
358
+ data: {"id":"…","role":"assistant","content":"…","agent":{"handle":"support",…}}
359
+
360
+ event: approval_required
361
+ data: {"tool_call_id":"call-1","tool":"create_ticket","arguments":{…}}
362
+
363
+ event: error
364
+ data: {"detail":"The agent could not finish answering"}
365
+ ```
366
+
367
+ `done` carries the stored turn — the one a reload would show — and `agent` when
368
+ the application holds several. An `error` frame is a failure that happened after
369
+ the 200 was on the wire: there is no status code left to fail with, so it travels
370
+ in the stream.
371
+
372
+ An agent that leads a team adds three more:
373
+
374
+ ```
375
+ event: handoff
376
+ data: {"to_agent_name":"Technique","reason":"Une panne technique."}
377
+
378
+ event: escalated
379
+ data: {}
380
+
381
+ event: held
382
+ data: {}
383
+ ```
384
+
385
+ `handoff` comes before the answer of the agent the conversation was passed to,
386
+ which has no handle on the application: the widget signs that answer with
387
+ `to_agent_name`. `escalated` follows the answer of an agent that handed the
388
+ conversation to a human of the organization; from then on a message is answered
389
+ with `held` alone — it was kept for that human and no agent ran. The replay route
390
+ says the same with `"handling": "awaiting_human"` or `"human"`, and the widget
391
+ reads the conversation again every five seconds until the agents have it back,
392
+ showing the colleague's replies signed with their name (`"kind": "human"`,
393
+ `"author_name"`).
394
+
395
+ Refusals before the stream starts are JSON: `{ "detail": …, "code": … }`. Three
396
+ codes a page reacts to rather than just logging:
397
+
398
+ | `code` | What happened | What to do |
399
+ |----------------------------|------------------------------------------------------------|-----------------------------------|
400
+ | `origin_not_allowed` | This page's origin is not on the application's allowlist | Ask for the domain to be added |
401
+ | `application_has_no_agent` | The application is there and nothing answers on it yet | Nothing: it is being set up |
402
+ | `conversation_retired` | The embed key was rotated after this conversation opened | Open a new conversation (the widget does this by itself, and asks the question again) |
403
+ | `identity_not_verified` | The application verifies visitors and the claim had no valid `digest` | Fix the digest on your server (see *Verified visitors*) |
404
+ | `public_rate_limited` | A budget is spent — 429, with a `Retry-After` header | Wait what the header says |
405
+
406
+ Three things to watch:
407
+
408
+ - **Auth**: there is none to arrange. The `embed-key` is public and the visitor
409
+ token is handed out by the API; nothing here relays a bearer token.
410
+ - **Origins**: the page's origin has to be listed on the application in Squadico
411
+ — the panel is a custom element rendered *inline*, so the browser sends the
412
+ host page's own origin. Moving it into an iframe would break that check, and
413
+ the open call then has to move back into the host-page script.
414
+ - **CORS**: the host page's origin also has to be in the API's `CORS_ORIGINS`.
415
+ - **Context keys**: the widget filters nothing and forwards the context as-is.
416
+ It reaches the agent as *data* — a labelled block beside the question, never
417
+ folded into its instructions — and the API bounds it rather than believing it.
418
+ The only shaping: a named object is split before sending
419
+ (`workspace: { id, name }` → `workspaceId` + `workspaceName`), the form the
420
+ agent reads.
421
+
422
+ ### Verified visitors
423
+
424
+ An application can be switched to **verified attribution** in the Squadico back
425
+ office. From then on a claim has to arrive with an HMAC of the id, computed under
426
+ the identity secret that screen hands over once — and a claim without one, or with
427
+ a wrong one, is refused with `identity_not_verified` rather than quietly recorded
428
+ as a label.
429
+
430
+ Compute it **on your server**. A digest a browser can produce is a digest anybody
431
+ can produce, and the secret would be sitting in the page's source:
432
+
433
+ ```js
434
+ // Node, wherever the page is rendered — never in the browser
435
+ import { createHmac } from 'node:crypto'
436
+
437
+ const digest = createHmac('sha256', process.env.SQUADICO_IDENTITY_SECRET)
438
+ .update(user.id)
439
+ .digest('hex')
440
+ ```
441
+
442
+ ```html
443
+ <squadico-chatbot
444
+ embed-key="sq_pub_…"
445
+ user='{"id":"host-42","name":"Ada","digest":"a3f1…"}'
446
+ ></squadico-chatbot>
447
+ ```
448
+
449
+ The digest is over the id alone, so it can be computed once when the page is
450
+ rendered. Nothing else changes: an anonymous visitor still opens a conversation on
451
+ a verifying application, because requiring *proof* of an identity is not the same
452
+ as requiring one.
453
+
454
+ ### Context: named object or bare id
455
+
456
+ A host object can be named in two ways:
457
+
458
+ ```ts
459
+ chatbot.context = { workspace: { id: 'WS-1', name: 'Acme' } } // named
460
+ chatbot.context = { workspaceId: 'WS-1' } // the id alone
461
+ ```
462
+
463
+ The widget knows no domain entity: any key carrying a `{ id, name }` object is
464
+ displayed as a chip and split on the wire, and any key carrying a plain string is
465
+ forwarded untouched. The named form should be preferred as soon as the page knows
466
+ the name, which is the case as soon as it displays it: the panel chip then shows
467
+ "Acme" instead of a UUID, and the agent can name the object without reading it
468
+ back.
469
+
470
+ ## Development
471
+
472
+ The development server is the one place the API can be moved: it talks to
473
+ `http://localhost:8000` unless `SQUADICO_API_URL` says otherwise. A build ignores
474
+ the variable — everything `npm run build` produces talks to
475
+ `https://api.squadico.com`.
476
+
477
+ ```bash
478
+ npm run dev # minimal sandbox on :3010
479
+ SQUADICO_API_URL=https://staging.example npm run dev # sandbox on another API
480
+ npm run build # ESM + CJS + IIFE + .d.ts declarations
481
+ npm run size # real gzip weight, 30 kB budget on the standalone bundle
482
+ npm run type-check # tsc --noEmit
483
+ npm run test # vitest run
484
+ npm run lint # eslint
485
+ ```
486
+
487
+ ## Publishing
488
+
489
+ `.github/workflows/publish-chatbot.yml` publishes the package to npm. Bump
490
+ `version` in `package.json`, then run it by hand — *Actions → Publish chatbot →
491
+ Run workflow*, on the branch to release from. *Dry run* packs without
492
+ publishing. No tag is needed; pushing `chatbot-v<version>` publishes too.
493
+
494
+ The job runs the same checks as above — the `chatbot` job of `ci.yml` runs them
495
+ on every pull request touching `libs/chatbot` — refuses a version already on the
496
+ registry (and a tag that does not match `package.json`), and checks the bundle
497
+ talks to the production API before publishing. It needs an `NPM_TOKEN` secret: an npm automation token allowed to
498
+ publish `@squadico/chatbot`.
499
+
500
+ The suite runs on vitest + jsdom against the real frame shapes: the HTTP client,
501
+ the conversation's state machine, the composer's picker. `type-check`, `lint` and
502
+ the size budget are guardrails beside it, not instead of it.