@constructor-io/constructorio-ui-asa 1.8.0 → 1.10.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 (43) hide show
  1. package/README.md +8 -210
  2. package/dist/constructorio-ui-asa-bundled.js +79 -21
  3. package/lib/cjs/components/Button/Button.js +22 -3
  4. package/lib/cjs/components/Chat/AiMessage.css +52 -0
  5. package/lib/cjs/components/Chat/AiMessage.js +6 -2
  6. package/lib/cjs/components/Chat/Chat.js +18 -4
  7. package/lib/cjs/components/Chat/ChatInput.js +3 -3
  8. package/lib/cjs/hooks/asaStreamHandlers.js +1 -1
  9. package/lib/cjs/hooks/persistence/backgroundTurns.js +35 -0
  10. package/lib/cjs/hooks/persistence/useChatPersistence.js +35 -10
  11. package/lib/cjs/hooks/useAsaResults.js +54 -26
  12. package/lib/cjs/hooks/useAsaTracking.js +31 -16
  13. package/lib/cjs/index.js +3 -1
  14. package/lib/cjs/utils/chatThreads.js +15 -1
  15. package/lib/cjs/utils/localStoragePersistence.js +110 -19
  16. package/lib/cjs/utils/markdown.js +60 -0
  17. package/lib/cjs/version.js +1 -1
  18. package/lib/mjs/components/Button/Button.js +26 -3
  19. package/lib/mjs/components/Chat/AiMessage.css +52 -0
  20. package/lib/mjs/components/Chat/AiMessage.js +6 -2
  21. package/lib/mjs/components/Chat/Chat.js +18 -4
  22. package/lib/mjs/components/Chat/ChatInput.js +3 -3
  23. package/lib/mjs/hooks/asaStreamHandlers.js +1 -1
  24. package/lib/mjs/hooks/persistence/backgroundTurns.js +35 -1
  25. package/lib/mjs/hooks/persistence/useChatPersistence.js +35 -11
  26. package/lib/mjs/hooks/useAsaResults.js +53 -24
  27. package/lib/mjs/hooks/useAsaTracking.js +40 -16
  28. package/lib/mjs/index.js +1 -0
  29. package/lib/mjs/utils/chatThreads.js +12 -0
  30. package/lib/mjs/utils/localStoragePersistence.js +106 -14
  31. package/lib/mjs/utils/markdown.js +53 -0
  32. package/lib/mjs/version.js +1 -1
  33. package/lib/types/components/Button/Button.d.ts +3 -2
  34. package/lib/types/components/Chat/Chat.d.ts +13 -1
  35. package/lib/types/hooks/persistence/backgroundTurns.d.ts +6 -0
  36. package/lib/types/hooks/persistence/useChatPersistence.d.ts +4 -0
  37. package/lib/types/hooks/useAsaTracking.d.ts +4 -2
  38. package/lib/types/index.d.ts +3 -2
  39. package/lib/types/types.d.ts +57 -8
  40. package/lib/types/utils/chatThreads.d.ts +4 -0
  41. package/lib/types/utils/markdown.d.ts +8 -0
  42. package/lib/types/version.d.ts +1 -1
  43. package/package.json +6 -2
package/README.md CHANGED
@@ -3,225 +3,28 @@
3
3
  [![npm](https://img.shields.io/npm/v/@constructor-io/constructorio-ui-asa)](https://www.npmjs.com/package/@constructor-io/constructorio-ui-asa)
4
4
  [![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/Constructor-io/constructorio-ui-asa/blob/main/LICENSE)
5
5
 
6
- A UI library that provides React components to manage the fetching and rendering logic for [Constructor.io's AI Shopping Agent](https://constructor.com/solutions/ai-shopping-agent/). TypeScript support is available.
6
+ React components for [Constructor.io's AI Shopping Agent](https://constructor.com/solutions/ai-shopping-agent/): a conversational shopping assistant.
7
7
 
8
- A UI Library that provides React components to manage the fetching and rendering logic for [AI Shopping Agent](https://constructor.com/solutions/ai-shopping-agent/) powered by Constructor.io. Typescript support is available.
8
+ **[Documentation →](https://constructor-io.github.io/constructorio-ui-asa/)**
9
9
 
10
- - [Overview](#overview)
11
- - [Installation](#installation)
12
- - [Usage](#usage)
13
- - [Customization](#customization)
14
- - [Troubleshooting](#troubleshooting)
15
- - [Local Development](#local-development)
16
- - [Publishing New Versions](#publishing-new-versions)
17
- - [Supporting Docs](#supporting-docs)
18
- - [Related Libraries](#related-libraries)
19
- - [License](#license)
20
-
21
- ## Overview
22
-
23
- [Constructor.io's AI Shopping Agent](https://constructor.com/solutions/ai-shopping-agent/) is a conversational shopping experience that helps shoppers discover products through natural-language chat. This UI library simplifies the integration process by providing React components that handle the fetching and rendering logic for the AI Shopping Agent.
24
-
25
- [Our Storybook Docs](https://constructor-io.github.io/constructorio-ui-asa/?path=/docs/general-introduction--variants) are the best place to explore the behavior and the available configuration options for this UI library.
26
-
27
- ## Installation
10
+ ## Install
28
11
 
29
12
  ```bash
30
13
  npm i @constructor-io/constructorio-ui-asa
31
14
  ```
32
15
 
33
- ### Prerequisites
34
-
35
- This library declares the following as peer dependencies, so they must be present in your project:
36
-
37
- | Package | Version |
38
- | ------------------------------------------------- | ----------- |
39
- | `@constructor-io/constructorio-client-javascript` | `^2.93.1` |
40
- | `@constructor-io/constructorio-ui-components` | `^1.6.0` |
41
- | `react` | `>=16.12.0` |
42
- | `react-dom` | `>=16.12.0` |
43
- | `tslib` | `^2.4.0` |
44
-
45
- On npm 7+ these are installed automatically. If your package manager doesn't install peer dependencies automatically (yarn, pnpm, npm 6 and below), install any that are missing:
46
-
47
- ```bash
48
- npm install \
49
- @constructor-io/constructorio-client-javascript \
50
- @constructor-io/constructorio-ui-components \
51
- react react-dom tslib
52
- ```
53
-
54
16
  ## Usage
55
17
 
56
- ### Using the React Components
57
-
58
- Wrap your application (or the relevant subtree) with `CioAsaProvider` to supply the Constructor client and configuration, then render the `Chat` component for a full conversational UI.
59
-
60
18
  ```jsx
61
19
  import { CioAsaProvider, Chat } from '@constructor-io/constructorio-ui-asa';
62
20
  import '@constructor-io/constructorio-ui-asa/styles.css';
63
21
 
64
- function ShoppingAgent() {
65
- return (
66
- <CioAsaProvider apiKey='YOUR_API_KEY'>
67
- <Chat
68
- onClose={() => {}}
69
- onProductClick={(product) => (window.location.href = product.url)}
70
- currency='$'
71
- initialSuggestions={['Show me summer dresses', 'Best running shoes under $100']}
72
- />
73
- </CioAsaProvider>
74
- );
75
- }
76
- ```
77
-
78
- `CioAsaProvider` accepts:
79
-
80
- | Prop | Type | Description |
81
- | ---------------------- | ------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
82
- | `apiKey` | `string` | Your Constructor index key. Required unless you pass your own `cioClient`. |
83
- | `cioClient` | `ConstructorIOClient` | A pre-configured Constructor client. Provide this instead of `apiKey` when you need to customize client options (e.g. `serviceUrl`, `segments`, `userId`). |
84
- | `testCells` | `Record<string, string>` | A/B test cells to attach to tracking events, as `{ [testName]: cellName }`. See [A/B Testing](#ab-testing). |
85
- | `staticRequestConfigs` | `RequestConfigs` | Request-level config passed to the ASA agent. Defaults to `{ domain: 'chatbot' }`. |
86
- | `formatters` | `Formatters` | Override built-in formatters (e.g. `formatPrice`). Merged over the defaults. |
87
- | `urlHelpers` | `UrlHelpers` | Override built-in URL read/write helpers. Merged over the defaults. |
88
- | `persistConversation` | `{ enabled: boolean }` | Off by default; pass `{ enabled: true }` to keep the conversation across page loads: in `localStorage` for a signed-in shopper (keyed by api key, domain and user id), in `sessionStorage` for a guest, so it ends with the tab. Off when omitted, and off when no api key can be read from a custom `cioClient`. See [Persistent Chat](https://constructor-io.github.io/constructorio-ui-asa/?path=/docs/components-chat-persistent-chat--variants). |
89
- | `userId` | `string` or `null` | Whose conversation this is, required with `persistConversation`. Pass the signed-in shopper's id on login and `null` on logout or for a guest, so each shopper only ever sees their own history. Use the same stable, non-personal id you give Constructor for personalization. With `apiKey` it is also set on the client; with your own `cioClient`, set it there too. |
90
-
91
- > **Login and logout.** History is stored per `userId`. Signing in moves the guest history into the shopper's history, and the conversation on screen continues; changing to another id or logging out switches the chat to that history in every open tab. Nothing is deleted on logout by default: the old history stays on the device for 7 days and returns when that shopper signs in again. To delete it on logout, call `clearPersistedConversations({ apiKey, domain, userId })`. See [Persistent Chat](https://constructor-io.github.io/constructorio-ui-asa/?path=/docs/components-chat-persistent-chat--variants).
92
-
93
- > **Where do I get `apiKey`?** This is your Constructor index key (the same key used by other Constructor client integrations), available in your Constructor dashboard. The AI Shopping Agent must be enabled for your account — contact your Constructor representative if agent requests return errors.
94
-
95
- > **Server-side rendering.** The Constructor client is only instantiated in the browser, so `Chat` and `useAsaResults` must run client-side only. In Next.js App Router, add `'use client'` and defer rendering until mounted (or load with `next/dynamic` and `{ ssr: false }`). See the [Integration Guide](https://constructor-io.github.io/constructorio-ui-asa/?path=/docs/general-integration-guide--variants) for a full example.
96
-
97
- ### A/B Testing
98
-
99
- Attach test cells so ASA's tracking events can be attributed to a cell. Each entry is sent as its
100
- own `ef-<testName>` parameter, so a shopper in several concurrent tests carries every cell:
101
-
102
- ```jsx
103
- <CioAsa
104
- apiKey='YOUR_API_KEY'
105
- testCells={{ constructorio: 'variant_a', your_other_test: 'variant_b' }}
106
- />
107
- ```
108
-
109
- The keys are your own test names, not values Constructor defines. Constructor's docs have the page
110
- set `window.cnstrc.testCell` to a bare cell name, which carries no test name of its own, so label
111
- it with yours:
112
-
113
- ```jsx
114
- <CioAsa apiKey='YOUR_API_KEY' testCells={{ constructorio: window.cnstrc.testCell }} />
115
- ```
116
-
117
- Some integrations expose a `window.cnstrc.testCells` map instead, which you can pass straight
118
- through. Empty and non-string values are dropped, so a cell read from a global that resolves to
119
- `undefined` is simply not sent.
120
-
121
- If you supply your own `cioClient`, that client owns its own options: set `testCells` there
122
- instead, as a `ConstructorIOClient` constructor option. `testCells` is ignored in that case, and
123
- passing both logs a warning rather than dropping the value silently.
124
-
125
- ### Using the Hook Directly
126
-
127
- For custom UI implementations, use the `useAsaResults` hook. All configuration comes from the surrounding `CioAsaProvider`; its options are `{ initialThreadId }` to resume a specific thread and `{ normalizeItem }` to control how raw result items are stored as product cards with `persistConversation` on.
128
-
129
- ```jsx
130
- import { useAsaResults } from '@constructor-io/constructorio-ui-asa';
131
-
132
- function CustomChat() {
133
- const { messages, sendMessage, isStreaming, clearHistory } = useAsaResults();
134
-
135
- return (
136
- <div>
137
- {messages.map((msg) => (
138
- <div key={msg.id}>
139
- <strong>{msg.role}:</strong> {msg.text}
140
- </div>
141
- ))}
142
- <button onClick={() => sendMessage('Show me running shoes')} disabled={isStreaming}>
143
- Send
144
- </button>
145
- <button onClick={clearHistory}>Reset</button>
146
- </div>
147
- );
148
- }
149
- ```
150
-
151
- The hook returns `messages` (the full conversation), `sendMessage(text)`, `isStreaming`, and `clearHistory()`. With `persistConversation` on it also returns `isHydrating`, `threads`, `activeThreadId`, `newThread()` and `switchThread(threadId)` for building a chat history UI. Product results arrive on assistant messages in `msg.groups` and can be rendered with the exported `ResultsBlock` component.
152
-
153
- ## Customization
154
-
155
- ### Styling
156
-
157
- By default, importing React components from this library does not pull any CSS into your project.
158
-
159
- If you wish to use the starter styles from this library, add an import statement similar to the example below:
160
-
161
- ```js
162
- import '@constructor-io/constructorio-ui-asa/styles.css';
163
- ```
164
-
165
- - These starter styles can be used as a foundation to build on top of, or just as a reference for you to replace completely.
166
- - To opt out of all default styling, do not import the `styles.css` stylesheet.
167
- - All components use BEM-style class names prefixed with `cio-asa-` for easy style overrides.
168
-
169
- ### Translations
170
-
171
- All user-facing text can be customized via the `translations` prop for internationalization. Pass only the keys you want to override — all others fall back to English defaults.
172
-
173
- ```jsx
174
- <Chat
175
- translations={{
176
- 'CioAsa.welcome.title': 'Style Advisor',
177
- 'CioAsa.welcome.placeholder': 'What are you looking for?',
178
- 'CioAsa.results.addToCart': 'Add to bag',
179
- }}
180
- />
181
- ```
182
-
183
- ### Component Overrides
184
-
185
- You can override individual sub-components using the `componentOverrides` prop with render props. See our [Storybook Docs](https://constructor-io.github.io/constructorio-ui-asa/?path=/docs/general-introduction--variants) for available override slots.
186
-
187
- ```jsx
188
- <Chat
189
- componentOverrides={{
190
- header: {
191
- reactNode: ({ title, onClose }) => (
192
- <div className='my-header'>
193
- <h1>{title}</h1>
194
- <button onClick={onClose}>Close</button>
195
- </div>
196
- ),
197
- },
198
- }}
199
- />
22
+ <CioAsaProvider apiKey='YOUR_API_KEY'>
23
+ <Chat onClose={() => setIsOpen(false)} />
24
+ </CioAsaProvider>;
200
25
  ```
201
26
 
202
- ## Troubleshooting
203
-
204
- ### Known Issues
205
-
206
- **Older JavaScript environments**
207
-
208
- The library provides two builds: CommonJS (cjs) and ECMAScript Modules (mjs).
209
-
210
- The ECMAScript Modules (mjs) build targets ESNext, which might not be supported by your environment. If your environment uses an older JavaScript version like ES6 (ES2015), you may get an error such as:
211
-
212
- `Module parse failed: Unexpected token`
213
-
214
- To solve this you can import the CommonJS (cjs) build, which supports ES6 (ES2015) syntax:
215
-
216
- `import { Chat } from '@constructor-io/constructorio-ui-asa/cjs'`
217
-
218
- **ESLint**
219
-
220
- There is a known issue with ESLint where it fails to resolve the paths exposed in the `exports` statement of NPM packages. If you receive an error like the one below, you can safely disable ESLint using `// eslint-disable-line` for that line.
221
-
222
- `Unable to resolve path to module '@constructor-io/constructorio-ui-asa/styles.css'`
223
-
224
- Relevant open issues: [Issue 1868](https://github.com/import-js/eslint-plugin-import/issues/1868), [Issue 1810](https://github.com/import-js/eslint-plugin-import/issues/1810)
27
+ Peer dependencies, layout, product results, tracking, customization and every prop are in the [docs](https://constructor-io.github.io/constructorio-ui-asa/).
225
28
 
226
29
  ## Local Development
227
30
 
@@ -261,11 +64,6 @@ This workflow will automatically:
261
64
 
262
65
  #### Note: Please don't manually increase the package.json version or create new git tags.
263
66
 
264
- ## Supporting Docs
265
-
266
- - [Storybook Docs](https://constructor-io.github.io/constructorio-ui-asa/?path=/docs/general-introduction--variants)
267
- - [Constructor.io API Documentation](https://docs.constructor.io/)
268
-
269
67
  ## Related Libraries
270
68
 
271
69
  - [@constructor-io/constructorio-client-javascript](https://github.com/Constructor-io/constructorio-client-javascript) - JavaScript client for Constructor.io API
@@ -278,7 +76,7 @@ This workflow will automatically:
278
76
  1. Fork the repo and create a new branch.
279
77
  2. Run `npm ci` to install dependencies.
280
78
  3. Make your changes.
281
- 4. Run `npm run lint` and `npm run test` to verify.
79
+ 4. Run `npm run lint` and `npm run test` to verify. For docs changes, follow [`.claude/docs.md`](.claude/docs.md).
282
80
  5. Submit a PR for review.
283
81
 
284
82
  ## License