@boldare/askdare 0.2.0-rc.1 → 0.2.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/dist/ui/panel.tsx CHANGED
@@ -1,4 +1,4 @@
1
- // Ejected from @boldare/askdare@0.2.0-rc.1 with `npx @boldare/askdare add ui`. This copy is yours to change.
1
+ // Ejected from @boldare/askdare@0.2.0 with `npx @boldare/askdare add ui`. This copy is yours to change.
2
2
  import {
3
3
  ErrorMessage,
4
4
  MessageList,
@@ -53,6 +53,8 @@ export function DefaultPanel({
53
53
  }): ReactElement {
54
54
  const { state, actions, bot, t } = useChat();
55
55
  const [confirming, setConfirming] = useState(false);
56
+ /** Accepted, and the server is deleting the conversation: the question stays, busy, until then. */
57
+ const [restarting, setRestarting] = useState(false);
56
58
  const restartRef = useRef<HTMLButtonElement>(null);
57
59
  const askStartOver = useCallback(() => setConfirming(true), []);
58
60
  const skin = useMemo<Skin>(
@@ -61,6 +63,7 @@ export function DefaultPanel({
61
63
  );
62
64
 
63
65
  const cancelStartOver = (): void => {
66
+ if (restarting) return;
64
67
  setConfirming(false);
65
68
  requestAnimationFrame(() => restartRef.current?.focus());
66
69
  };
@@ -101,10 +104,13 @@ export function DefaultPanel({
101
104
  {confirming && (
102
105
  <StartOverQuestion
103
106
  modal={ownsDocument}
107
+ busy={restarting}
104
108
  onCancel={cancelStartOver}
105
- onAccept={() => {
109
+ onAccept={async () => {
110
+ setRestarting(true);
111
+ await actions.newConversation();
112
+ setRestarting(false);
106
113
  setConfirming(false);
107
- void actions.newConversation();
108
114
  }}
109
115
  />
110
116
  )}
@@ -1,4 +1,4 @@
1
- // Ejected from @boldare/askdare@0.2.0-rc.1 with `npx @boldare/askdare add ui`. This copy is yours to change.
1
+ // Ejected from @boldare/askdare@0.2.0 with `npx @boldare/askdare add ui`. This copy is yours to change.
2
2
  import { useChat } from '@boldare/askdare/react';
3
3
  import type { ReactElement, ReactNode } from 'react';
4
4
  import { Avatar } from './avatar';
package/docs/cli.md CHANGED
@@ -38,7 +38,7 @@ The folder needs a `package.json` with `next` among its dependencies, and an `ap
38
38
 
39
39
  | File | What `init` does | When it leaves the file alone |
40
40
  | --- | --- | --- |
41
- | `askdare.config.ts` | Creates one bot: the ID, `model: process.env.ASKDARE_MODEL ?? ''`, short instructions, an empty `security.allowedOrigins`, `locale: 'auto'`, a greeting and the `aura` theme. It is `askdare.config.js` when the project has no `tsconfig.json`. | An `askdare.config` file already exists (`.ts`, `.tsx`, `.js`, `.jsx` or `.mjs`). |
41
+ | `askdare.config.ts` | Creates one bot: the ID, `model: process.env.ASKDARE_MODEL ?? ''`, short instructions and a greeting (replaced by `ASKDARE_INSTRUCTIONS` and `ASKDARE_GREETING` when they are set), `security.allowedOrigins` from `ASKDARE_ALLOWED_ORIGINS` (comma-separated, empty by default), `locale: 'auto'` and the `aura` theme. It is `askdare.config.js` when the project has no `tsconfig.json`. | An `askdare.config` file already exists (`.ts`, `.tsx`, `.js`, `.jsx` or `.mjs`). |
42
42
  | `app/api/chat/[...path]/route.ts` | Creates the catch-all route: `export const maxDuration = 300;` and `createChatHandler({ bots })`. Under `src/app/` if that's where your app is. | A route file is already there. |
43
43
  | `.gitignore` | Appends `.env*.local`, so secrets never land in git. | Git already ignores `.env.local` and `.env.production.local`. |
44
44
  | `.env.local` | Appends, under `# askdare`, each with a comment: `OPENAI_API_KEY` (empty), `CHAT_SECRET` (32 random bytes, base64url) and `ASKDARE_MODEL` (the `--model` value, or empty). A new file is readable only by you. | Skips each variable that `.env.local` or `.env` already defines. It never changes existing lines. |
@@ -59,7 +59,7 @@ askdare init
59
59
  Next:
60
60
  1. In .env.local, set OPENAI_API_KEY, ASKDARE_MODEL.
61
61
  2. Start the app (npm run dev), then check it: npx @boldare/askdare doctor
62
- 3. To embed the chat on another site, add that site to allowedOrigins and paste:
62
+ 3. To embed the chat on another site, add it to ASKDARE_ALLOWED_ORIGINS in .env.local (comma-separated) and paste:
63
63
  <script src="https://<your app>/api/chat/widget.js?bot=support" async></script>
64
64
  ```
65
65
 
@@ -397,6 +397,8 @@ The library doesn't read these. `npx @boldare/askdare init`, the starter and the
397
397
  | Variable | Used for | Notes |
398
398
  | --- | --- | --- |
399
399
  | `ASKDARE_MODEL` | `model: process.env.ASKDARE_MODEL ?? ''` | `init` writes it to `.env.local` (from `--model`). Set it in every environment that builds or runs the app; on Vercel, in Production and Preview. |
400
+ | `ASKDARE_INSTRUCTIONS` | `instructions` | Optional. The `init` template uses it in place of its default instructions and keeps the sentence about the reply language. |
401
+ | `ASKDARE_GREETING` | `widget.greeting` | Optional. |
400
402
  | `ASKDARE_TRANSCRIBE_MODEL` | `voice.model` | |
401
403
  | `ASKDARE_ALLOWED_ORIGINS` | `security.allowedOrigins` | Comma-separated, as in the [example](#example). |
402
404
  | `KB_VECTOR_STORE_ID` | `knowledge.vectorStoreId` | The first `npx @boldare/askdare kb sync` creates the store and prints its ID. |
@@ -14,6 +14,33 @@ askdare runs wherever Next.js runs on Node.js. It needs no database: conversatio
14
14
 
15
15
  Set them in your host's settings, never in files you commit.
16
16
 
17
+ ## One-click deploy
18
+
19
+ To see askdare running on a host before you add it to your own app, deploy the [starter](https://github.com/boldare/askdare/tree/main/examples/starter): the app that `npx @boldare/askdare init` makes of a fresh `create-next-app`, with one bot. It takes about five minutes, and [askdare.vercel.app/deploy](https://askdare.vercel.app/deploy) walks you through it.
20
+
21
+ [![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fboldare%2Faskdare%2Ftree%2Fmain%2Fexamples%2Fstarter&project-name=askdare-chat&repository-name=askdare-chat&env=OPENAI_API_KEY%2CCHAT_SECRET%2CASKDARE_MODEL%2CASKDARE_INSTRUCTIONS%2CASKDARE_GREETING&envDefaults=%7B%22ASKDARE_MODEL%22%3A%22gpt-6.1-terra%22%2C%22ASKDARE_INSTRUCTIONS%22%3A%22You%20are%20the%20assistant%20on%20our%20website.%20Answer%20briefly%20and%20politely.%20If%20you%20do%20not%20know%20something%2C%20say%20so.%22%2C%22ASKDARE_GREETING%22%3A%22Hi%21%20How%20can%20I%20help%3F%22%7D&envDescription=OPENAI_API_KEY%3A%20a%20key%20from%20a%20dedicated%20OpenAI%20project%20with%20a%20hard%20spend%20limit.%20CHAT_SECRET%3A%20at%20least%2032%20random%20characters%3B%20askdare.vercel.app%2Fdeploy%20makes%20one%20for%20you.%20ASKDARE_MODEL%3A%20a%20current%20OpenAI%20model%20your%20project%20may%20use.%20ASKDARE_INSTRUCTIONS%3A%20what%20the%20bot%20does%20and%20how%20it%20answers.%20ASKDARE_GREETING%3A%20the%20first%20message%20in%20the%20chat.&envLink=https%3A%2F%2Faskdare.vercel.app%2Fdeploy)
22
+
23
+ - **Vercel:** the button copies the starter into a new repository on your Git account, then asks for the variables. Enter your `OPENAI_API_KEY` and a new `CHAT_SECRET` of at least 32 characters: copy one from [askdare.vercel.app/deploy](https://askdare.vercel.app/deploy), which makes it in your browser, or run `node -e "console.log(require('node:crypto').randomBytes(32).toString('base64url'))"`. `ASKDARE_MODEL` comes filled in with the starter's model; change it to one your project may use. `ASKDARE_INSTRUCTIONS` (what the bot does) and `ASKDARE_GREETING` (its first message) come filled in with the starter's defaults; change them to give the bot its job. To embed the chat on other sites later, add `ASKDARE_ALLOWED_ORIGINS` under **Settings → Environment Variables** and redeploy.
24
+ - **Heroku:** a Heroku button only deploys an app at the root of a repository, so it works from your own copy of the starter. Create one with `npx create-next-app --example https://github.com/boldare/askdare/tree/main/examples/starter my-chat`, push it to GitHub, then open `https://www.heroku.com/deploy?template=https://github.com/<you>/<repo>`. The starter's `app.json` asks for `OPENAI_API_KEY`, generates `CHAT_SECRET`, fills in `ASKDARE_MODEL`, offers the optional `ASKDARE_INSTRUCTIONS`, `ASKDARE_GREETING` and `ASKDARE_ALLOWED_ORIGINS`, and runs one web dyno with the `Procfile` from [Heroku](#heroku).
25
+
26
+ These settings live in your host's environment variables, which win over the defaults in `askdare.config.ts`. Change them there and redeploy, or delete them and edit the config in your repository.
27
+
28
+ Then check the live app from a local copy of that repository, with the production values in `.env.production.local`:
29
+
30
+ ```bash
31
+ vercel env pull .env.production.local --environment=production
32
+ ```
33
+
34
+ ```bash
35
+ heroku config --shell --app <your app> > .env.production.local
36
+ ```
37
+
38
+ ```bash
39
+ npx @boldare/askdare doctor --url https://<your app> --env-file .env.production.local
40
+ ```
41
+
42
+ A variable marked **Sensitive** on Vercel comes back empty from `vercel env pull`; paste its value into the file yourself. The starter keeps limits per instance, so before the chat is public, share them with Upstash (step 3 under [Vercel](#vercel)) and work through the [going-public checklist](going-public.md).
43
+
17
44
  ## Vercel
18
45
 
19
46
  1. Import the repository in Vercel. The defaults for Next.js are right.
package/docs/embedding.md CHANGED
@@ -257,4 +257,4 @@ ASKDARE_ALLOWED_ORIGINS=http://127.0.0.1:4000
257
257
 
258
258
  ## Page JavaScript
259
259
 
260
- The page can open and close the chat, sign the visitor in, and set a greeting, suggestions and page context per page: `askdare('open')`, `askdare('close')`, `askdare('toggle')`, `askdare('identify', token)` and `askdare('configure', { … })`. See [Styling](styling.md#3-page-javascript) and [Signed-in visitors](identity.md).
260
+ The page can open and close the chat, sign the visitor in, and set a greeting, suggestions and page context per page: `askdare('open')`, `askdare('close')`, `askdare('toggle')`, `askdare('identify', token)` and `askdare('configure', { … })`. See [Styling](styling.md#3-page-javascript) and [Signed-in visitors](identity.md). To hear what happens in the chat, for analytics, use `askdare('on', type, listener)`: see [Chat events](events.md).
package/docs/events.md ADDED
@@ -0,0 +1,183 @@
1
+ # Chat events
2
+
3
+ Your page can hear what happens in the chat: when it opens, when a visitor sends a message, when an answer arrives, when an answer is rated. Use these events for analytics such as GA4 or a GTM `dataLayer`, or to make your page react, for example by hiding your own "Contact us" button while the chat is open ([ADR-0013](https://github.com/boldare/askdare/blob/main/docs/adr/0013-browser-events.md)).
4
+
5
+ Events carry metadata only: counts, durations, outcomes and ratings. They never carry what anyone wrote. To work with the conversations themselves, use the server hooks in [Observability](observability.md) and [Answer ratings](feedback.md).
6
+
7
+ ## With the script tag
8
+
9
+ Register a listener with `askdare('on', type, listener)`. Pass one event type, or `'*'` for all of them:
10
+
11
+ ```html
12
+ <script>
13
+ // Safe before the script has loaded: calls queue up and run once it is ready.
14
+ window.askdare = window.askdare || function () {
15
+ (window.askdare.q = window.askdare.q || []).push(arguments);
16
+ };
17
+ askdare('on', '*', function (event) {
18
+ console.log('askdare', event.type, event);
19
+ });
20
+ </script>
21
+ <script src="https://app.example.com/api/chat/widget.js?bot=support" async></script>
22
+ ```
23
+
24
+ Register before the script loads, as above, and you hear `ready` too. A listener added later still hears everything after that.
25
+
26
+ `askdare('on')` returns a function that removes the listener. Through the queue, before the script has loaded, it returns nothing, so keep the listener and remove it with `askdare('off')`:
27
+
28
+ ```js
29
+ function onSent(event) { /* … */ }
30
+ askdare('on', 'message.sent', onSent);
31
+ // later
32
+ askdare('off', 'message.sent', onSent);
33
+ ```
34
+
35
+ ## In React
36
+
37
+ In a chat inside your page ([React](react.md)), pass `onEvent` to `Root`:
38
+
39
+ ```tsx
40
+ 'use client';
41
+ import * as Chat from '@boldare/askdare/react';
42
+
43
+ export function SupportChat({ config }: { config: Chat.ChatConfig }) {
44
+ return (
45
+ <Chat.Root config={config} onEvent={(event) => track(`chat_${event.type}`, event)}>
46
+ <Chat.DefaultChat />
47
+ </Chat.Root>
48
+ );
49
+ }
50
+ ```
51
+
52
+ - **`Root` always calls the latest function.** An inline arrow function is fine: a new one on each render doesn't restart the chat.
53
+ - **The types are exported:** `ChatEvent`, a union you can narrow by `event.type`, along with `ChatEventType` and `ChatEventListener`.
54
+ - **There are no `open` or `close` events in React.** The chat is part of your page, so you already know when you show it.
55
+
56
+ ## The events
57
+
58
+ Every event has `type` and `bot`, the bot's ID. The other fields depend on the type:
59
+
60
+ | `type` | Other fields | When |
61
+ | --- | --- | --- |
62
+ | `ready` | | Once per chat, when it can take messages: the visitor is known and the stored conversation, if any, has loaded. Not if loading it fails. |
63
+ | `open` | | The panel opened. Script tag only, and not for an inline chat, which is always open. |
64
+ | `close` | | The panel closed. Script tag only. |
65
+ | `message.sent` | `length`, `attachments`, `source` | The visitor sent a message. |
66
+ | `message.received` | `outcome`, `durationMs`, `citations` | An answer ended. |
67
+ | `feedback` | `rating`, `commented` | The server accepted a rating. |
68
+ | `error` | `code` | A turn failed. |
69
+ | `conversation.reset` | | The visitor started a new conversation. |
70
+
71
+ | Field | Type | Meaning |
72
+ | --- | --- | --- |
73
+ | `length` | number | The message's length in characters. |
74
+ | `attachments` | number | How many images and PDFs it carried. |
75
+ | `source` | `'typed'` or `'starter'` | Typed or dictated into the field, or picked from the suggested questions. |
76
+ | `outcome` | `'completed'`, `'failed'` or `'stopped'` | `stopped` when the chat was taken down mid-answer, as when a React `Root` unmounts. |
77
+ | `durationMs` | number | From sending the message to the end of the answer. |
78
+ | `citations` | number | How many sources the answer cites. |
79
+ | `rating` | `'up'`, `'down'` or `null` | `null` when the visitor took their rating back. |
80
+ | `commented` | boolean | A 👎 came with a comment. |
81
+ | `code` | string | One of the [error codes](troubleshooting.md#error-codes), such as `rate_limited` or `timeout`. |
82
+
83
+ ### Good to know
84
+
85
+ - **Events come in order for each message.** A `message.sent` is followed by exactly one `message.received`, with an `error` just before it when the turn failed.
86
+ - **Closing the panel doesn't stop an answer.** The answer finishes in the background, so `message.received` still arrives after `close`.
87
+ - **A 👎 with a comment fires `feedback` twice:** first for the 👎 (`commented: false`), then once the comment is sent (`commented: true`).
88
+ - **A rating the server refuses fires nothing,** because the chat takes it back.
89
+ - **New types and fields may come in a minor release.** Ignore the ones you don't know, and handle the ones you do by `event.type`.
90
+
91
+ ## What events never contain
92
+
93
+ Analytics scripts on your page are often third parties, so events leave out:
94
+ - the text of messages, answers and comments;
95
+ - page context, file names, source titles and links;
96
+ - conversation, message and OpenAI IDs, feedback handles and tokens;
97
+ - anything about who the visitor is.
98
+
99
+ With the script tag the loader checks this again on your page. It passes on only the known types and fields, each a number, a boolean, `null` or a short code, and it sets `bot` itself. Each event is frozen, so one listener can't change what the next one gets.
100
+
101
+ A listener that throws doesn't break the chat. askdare logs one line naming the event type, without the error, and the other listeners still run.
102
+
103
+ ## Examples
104
+
105
+ ### Google Analytics 4
106
+
107
+ ```js
108
+ askdare('on', '*', function (event) {
109
+ const { type, bot, ...params } = event;
110
+ gtag('event', 'chat_' + type.replace('.', '_'), { chat_bot: bot, ...params });
111
+ });
112
+ ```
113
+
114
+ GA4 event names allow only letters, digits and underscores, hence `chat_message_sent` rather than `chat_message.sent`.
115
+
116
+ ### Google Tag Manager
117
+
118
+ ```js
119
+ askdare('on', '*', function (event) {
120
+ window.dataLayer = window.dataLayer || [];
121
+ window.dataLayer.push({ event: 'askdare.' + event.type, askdare: event });
122
+ });
123
+ ```
124
+
125
+ In GTM, add a Custom Event trigger on `askdare.message.sent` (or a regex such as `^askdare\.`) and read the fields with Data Layer Variables, such as `askdare.length`.
126
+
127
+ ### Hide your own button while the chat is open
128
+
129
+ ```js
130
+ const contact = document.getElementById('contact-us');
131
+ askdare('on', 'open', () => { contact.hidden = true; });
132
+ askdare('on', 'close', () => { contact.hidden = false; });
133
+ ```
134
+
135
+ ### Count questions until a conversion
136
+
137
+ ```js
138
+ let asked = 0;
139
+ askdare('on', 'message.sent', () => { asked += 1; });
140
+ document.querySelector('#signup').addEventListener('submit', () => {
141
+ gtag('event', 'sign_up', { chat_questions: asked });
142
+ });
143
+ ```
144
+
145
+ ## Types for the script tag
146
+
147
+ The script tag adds `window.askdare` at runtime and ships no types. In a TypeScript page, declare what you use:
148
+
149
+ ```ts
150
+ type AskdareEvent = { type: string; bot: string } & Record<string, string | number | boolean | null>;
151
+
152
+ declare global {
153
+ interface Window {
154
+ askdare: {
155
+ (command: 'on', type: string, listener: (event: AskdareEvent) => void): (() => void) | undefined;
156
+ (command: 'off', type: string, listener: (event: AskdareEvent) => void): void;
157
+ (command: 'open' | 'close' | 'toggle'): void;
158
+ };
159
+ }
160
+ }
161
+ export {};
162
+ ```
163
+
164
+ In React, use `ChatEvent` from `@boldare/askdare/react` instead.
165
+
166
+ ## Try it in the browser
167
+
168
+ Open a page with the chat and its DevTools console. Choose **top** in the console's context menu rather than the chat's frame: `askdare` lives on your page, not in the chat. Then paste:
169
+
170
+ ```js
171
+ const off = askdare('on', '*', (event) => console.log('[askdare]', event.type, event));
172
+ ```
173
+
174
+ Open the chat, ask something, rate the answer, start over and close it. The console shows each event as it happens. `off()` stops it. You only see `ready` if you add the listener before the first open, because the chat loads when it first opens.
175
+
176
+ To see them without a console, open [the chat inside a page](https://askdare.vercel.app/inline) on the askdare website: a panel beside it lists every event `onEvent` receives, with all of its fields.
177
+
178
+ ## Troubleshooting
179
+
180
+ - **`askdare is not defined`.** The console is in the chat's frame, or the page hasn't defined the queue and the script hasn't loaded yet. Switch the console to **top**, or define the queue as in [the first example](#with-the-script-tag).
181
+ - **No `ready`.** The listener came after the chat had loaded, or loading the stored conversation failed. The chat then shows why.
182
+ - **Nothing at all.** Check that the page runs a version of askdare with events: the loader comes from your app (`/api/chat/widget.js`), so redeploy the app after upgrading.
183
+ - **`[askdare] A listener for "…" threw.`** One of your listeners threw. The chat carries on, but that listener missed the event.
@@ -4,6 +4,12 @@ From an empty Next.js app to a working chat in about 15 minutes. You need Node 2
4
4
 
5
5
  No Next.js app yet? Create one with the [Next.js installation guide](https://nextjs.org/docs/app/getting-started/installation), then come back.
6
6
 
7
+ Just want to see it running first? Deploy the starter app in one click, in about five minutes ([askdare.vercel.app/deploy](https://askdare.vercel.app/deploy) walks you through it), then come back to add askdare to your own app:
8
+
9
+ [![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fboldare%2Faskdare%2Ftree%2Fmain%2Fexamples%2Fstarter&project-name=askdare-chat&repository-name=askdare-chat&env=OPENAI_API_KEY%2CCHAT_SECRET%2CASKDARE_MODEL%2CASKDARE_INSTRUCTIONS%2CASKDARE_GREETING&envDefaults=%7B%22ASKDARE_MODEL%22%3A%22gpt-6.1-terra%22%2C%22ASKDARE_INSTRUCTIONS%22%3A%22You%20are%20the%20assistant%20on%20our%20website.%20Answer%20briefly%20and%20politely.%20If%20you%20do%20not%20know%20something%2C%20say%20so.%22%2C%22ASKDARE_GREETING%22%3A%22Hi%21%20How%20can%20I%20help%3F%22%7D&envDescription=OPENAI_API_KEY%3A%20a%20key%20from%20a%20dedicated%20OpenAI%20project%20with%20a%20hard%20spend%20limit.%20CHAT_SECRET%3A%20at%20least%2032%20random%20characters%3B%20askdare.vercel.app%2Fdeploy%20makes%20one%20for%20you.%20ASKDARE_MODEL%3A%20a%20current%20OpenAI%20model%20your%20project%20may%20use.%20ASKDARE_INSTRUCTIONS%3A%20what%20the%20bot%20does%20and%20how%20it%20answers.%20ASKDARE_GREETING%3A%20the%20first%20message%20in%20the%20chat.&envLink=https%3A%2F%2Faskdare.vercel.app%2Fdeploy)
10
+
11
+ [Deployment](deployment.md#one-click-deploy) covers what to fill in, Heroku, and how to check the live app with `doctor`.
12
+
7
13
  ## 1. Get an OpenAI key
8
14
 
9
15
  1. In the [OpenAI dashboard](https://platform.openai.com/), create a **new project** just for this chat.
@@ -87,7 +93,7 @@ Every check and option is in [Command line](cli.md#doctor).
87
93
 
88
94
  The chat can live on any site, not only on your Next.js app:
89
95
 
90
- 1. Add the site to `security.allowedOrigins` in `askdare.config.ts`, for example `['https://www.example.com']`.
96
+ 1. Add the site to `ASKDARE_ALLOWED_ORIGINS` in `.env.local`, for example `ASKDARE_ALLOWED_ORIGINS=https://www.example.com`. The `init` config reads it into `security.allowedOrigins`; separate several sites with commas. Restart `npm run dev`. A config from the `init` of askdare 0.1 sets `security.allowedOrigins` directly; add the site to that list instead.
91
97
  2. Paste one tag into that site's HTML:
92
98
 
93
99
  ```html
@@ -96,7 +102,7 @@ The chat can live on any site, not only on your Next.js app:
96
102
 
97
103
  Browsers refuse to show the chat on sites that aren't on the list. Check a site with `npx @boldare/askdare doctor --origin https://www.example.com`.
98
104
 
99
- Staging and production usually embed the chat on different sites. Read the list from an environment variable, such as `ASKDARE_ALLOWED_ORIGINS`. [Embedding](embedding.md#staging-and-production) shows how, along with the script's options and your site's Content Security Policy.
105
+ Staging and production usually embed the chat on different sites, so set `ASKDARE_ALLOWED_ORIGINS` in each environment. [Embedding](embedding.md#staging-and-production) has more, along with the script's options and your site's Content Security Policy.
100
106
 
101
107
  ## Working with an AI coding agent
102
108
 
@@ -11,6 +11,8 @@ Both may be async. askdare calls them after the answer has streamed, keeps the f
11
11
 
12
12
  Visitors' ratings of answers reach a third hook, `onFeedback`: see [Answer ratings](feedback.md).
13
13
 
14
+ For analytics on the page itself, such as GA4 or a GTM `dataLayer`, listen to the chat's [events in the browser](events.md).
15
+
14
16
  ## Costs per turn
15
17
 
16
18
  ```ts
@@ -74,3 +76,7 @@ The full list is in [Configuration](configuration.md#onusage). In short:
74
76
  - **Never included:** message text, tool arguments or results, file names, IP addresses, raw conversation or visitor IDs.
75
77
 
76
78
  A turn that breaks off mid-answer reports the usage askdare received, which can be a little less than OpenAI bills.
79
+
80
+ ## Events in the browser
81
+
82
+ The page itself can hear what happens in the chat (opened, message sent, answer received, answer rated) for analytics such as GA4 or a GTM `dataLayer`: `askdare('on', type, listener)` with the script tag, or `onEvent` on `Root` in React. The events carry metadata only. See [Chat events](events.md).
package/docs/privacy.md CHANGED
@@ -18,6 +18,7 @@ This page lists what data the chat handles, where it goes and how long it stays,
18
18
  | Error reports | Your server log, or your `onError` function | Your hosting's log retention |
19
19
  | Turn reports: token counts, timings, tool names, pseudonyms | Your `onUsage` function, if you set one | Wherever you keep them |
20
20
  | Ratings of answers and the comments visitors write | Your `onFeedback` function, if you set one | Wherever you keep them |
21
+ | Chat events: counts, durations, outcomes, ratings (no text) | Your page's `askdare('on')` or `onEvent` listeners ([Chat events](events.md)), if you add any | Wherever your analytics keeps them |
21
22
 
22
23
  ## In the visitor's browser
23
24
 
package/docs/react.md CHANGED
@@ -63,6 +63,7 @@ That is all: the components are client components already, and the chat loads it
63
63
  | `identity` | The signed-in visitor: a token from your backend, as for `askdare('identify')`. See [below](#signed-in-visitors). |
64
64
  | `greeting`, `starters` | This page's greeting and up to 4 suggested questions, instead of the bot's. |
65
65
  | `context` | A short description of the page for the assistant, up to 1,000 characters. Only for a bot with `pageContext: true`. |
66
+ | `onEvent` | Called with what happens in the chat, such as a message sent or an answer rated, for analytics. Metadata only. A new function doesn't restart the chat. See [Chat events](events.md). |
66
67
 
67
68
  `greeting`, `starters` and `context` are the same as in `askdare('configure')` and are checked the same way ([Styling](styling.md#3-page-javascript)).
68
69
 
package/docs/styling.md CHANGED
@@ -151,6 +151,7 @@ Every preset sets both font tokens, so set both. You can also set them as the `f
151
151
  | `askdare('open')`, `askdare('close')`, `askdare('toggle')` | Shows or hides the chat. Closing returns focus to whatever opened it. |
152
152
  | `askdare('identify', token)` | Signs the visitor in with a token from your backend, or out with `null`; see [Signed-in visitors](identity.md). |
153
153
  | `askdare('configure', { greeting, starters, context })` | Per-page greeting and suggestions, and `context`: a short description of the page (up to 1000 characters) for the assistant. |
154
+ | `askdare('on', type, listener)`, `askdare('off', type, listener)` | Listens to what happens in the chat, such as a message sent or an answer rated, for analytics. Metadata only; see [Chat events](events.md). |
154
155
 
155
156
  `configure` accepts only those three fields: never the model, instructions or tools. The context is off unless the bot opts in with `pageContext: true` in `defineBot`. It then travels with the visitor's message, as a marked part of their own turn: anyone can set it from their browser, so it gets no more trust than what the visitor types, and it never becomes part of the bot's instructions. It is sent again only when it changes, and the visitor's history doesn't show it. Define the small queue first, then call it any time:
156
157
 
@@ -67,7 +67,7 @@ The same check reports every invalid setting as `Invalid bot "<id>": <setting> <
67
67
 
68
68
  | Message | Fix |
69
69
  | --- | --- |
70
- | `security.allowedOrigins.0 must be an https origin like https://www.example.com (no path or wildcard; http only for localhost)` | Use the scheme and host only, such as `https://www.example.com`, with no trailing slash |
70
+ | `security.allowedOrigins.0 must be an https origin like https://www.example.com (no path or wildcard; http only for localhost)` | Use the scheme and host only, such as `https://www.example.com`, with no trailing slash. With the `init` config, check each entry in `ASKDARE_ALLOWED_ORIGINS` |
71
71
  | `id use 1-48 lowercase letters, digits or dashes` | Rename the bot, and update `?bot=` in the script tag |
72
72
  | `knowledge.vectorStoreId must be a vector store ID like vs_…` | Set `KB_VECTOR_STORE_ID` to the ID that `kb sync` printed |
73
73
  | `voice.model is required` | Set a transcription model in `voice` |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@boldare/askdare",
3
- "version": "0.2.0-rc.1",
3
+ "version": "0.2.0",
4
4
  "description": "Embeddable AI chatbot for Next.js, as a script-tag widget or React components, built on the official OpenAI SDK.",
5
5
  "license": "MIT",
6
6
  "author": {