@theonexai/blockspark-chat-widget 1.0.17

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 (69) hide show
  1. package/README.md +270 -0
  2. package/dist/ChatWidget-BM2lXfeE.cjs +2 -0
  3. package/dist/ChatWidget-BM2lXfeE.cjs.map +1 -0
  4. package/dist/ChatWidget-Cljd-Tfm.js +1356 -0
  5. package/dist/ChatWidget-Cljd-Tfm.js.map +1 -0
  6. package/dist/adapters/vue/index.d.ts +7 -0
  7. package/dist/adapters/vue/index.d.ts.map +1 -0
  8. package/dist/adapters/vue/useChatMode.d.ts +21 -0
  9. package/dist/adapters/vue/useChatMode.d.ts.map +1 -0
  10. package/dist/components/ChatWidget.d.ts +39 -0
  11. package/dist/components/ChatWidget.d.ts.map +1 -0
  12. package/dist/composables/useChatWidget.d.ts +35 -0
  13. package/dist/composables/useChatWidget.d.ts.map +1 -0
  14. package/dist/core/stateManager.d.ts +136 -0
  15. package/dist/core/stateManager.d.ts.map +1 -0
  16. package/dist/core/types.d.ts +64 -0
  17. package/dist/core/types.d.ts.map +1 -0
  18. package/dist/entry/next.d.ts +9 -0
  19. package/dist/entry/next.d.ts.map +1 -0
  20. package/dist/entry/nuxt.d.ts +12 -0
  21. package/dist/entry/nuxt.d.ts.map +1 -0
  22. package/dist/entry/react.d.ts +10 -0
  23. package/dist/entry/react.d.ts.map +1 -0
  24. package/dist/entry/vanilla.d.ts +33 -0
  25. package/dist/entry/vanilla.d.ts.map +1 -0
  26. package/dist/entry/vite.d.ts +11 -0
  27. package/dist/entry/vite.d.ts.map +1 -0
  28. package/dist/entry/vue.d.ts +12 -0
  29. package/dist/entry/vue.d.ts.map +1 -0
  30. package/dist/hooks/useChatMode.d.ts +17 -0
  31. package/dist/hooks/useChatMode.d.ts.map +1 -0
  32. package/dist/index.cjs.js +2 -0
  33. package/dist/index.cjs.js.map +1 -0
  34. package/dist/index.d.ts +21 -0
  35. package/dist/index.d.ts.map +1 -0
  36. package/dist/index.esm.js +1383 -0
  37. package/dist/index.esm.js.map +1 -0
  38. package/dist/nuxt.cjs.js +2 -0
  39. package/dist/nuxt.cjs.js.map +1 -0
  40. package/dist/nuxt.esm.js +9 -0
  41. package/dist/nuxt.esm.js.map +1 -0
  42. package/dist/sanitize-C8MB41vY.cjs +4 -0
  43. package/dist/sanitize-C8MB41vY.cjs.map +1 -0
  44. package/dist/sanitize-Cm1kskSD.js +1842 -0
  45. package/dist/sanitize-Cm1kskSD.js.map +1 -0
  46. package/dist/services/chatService.d.ts +144 -0
  47. package/dist/services/chatService.d.ts.map +1 -0
  48. package/dist/services/dialogflowBackendService.d.ts +36 -0
  49. package/dist/services/dialogflowBackendService.d.ts.map +1 -0
  50. package/dist/services/dialogflowClient.d.ts +36 -0
  51. package/dist/services/dialogflowClient.d.ts.map +1 -0
  52. package/dist/services/sessionManager.d.ts +13 -0
  53. package/dist/services/sessionManager.d.ts.map +1 -0
  54. package/dist/styles.css +1 -0
  55. package/dist/types.d.ts +5 -0
  56. package/dist/types.d.ts.map +1 -0
  57. package/dist/utils/dialogflowHandler.d.ts +31 -0
  58. package/dist/utils/dialogflowHandler.d.ts.map +1 -0
  59. package/dist/utils/frameworkDetector.d.ts +17 -0
  60. package/dist/utils/frameworkDetector.d.ts.map +1 -0
  61. package/dist/utils/sanitize.d.ts +25 -0
  62. package/dist/utils/sanitize.d.ts.map +1 -0
  63. package/dist/utils/ssr.d.ts +35 -0
  64. package/dist/utils/ssr.d.ts.map +1 -0
  65. package/dist/vue.cjs.js +2 -0
  66. package/dist/vue.cjs.js.map +1 -0
  67. package/dist/vue.esm.js +9 -0
  68. package/dist/vue.esm.js.map +1 -0
  69. package/package.json +114 -0
package/README.md ADDED
@@ -0,0 +1,270 @@
1
+ # BlockSpark Chat Widget
2
+
3
+ A universal JavaScript chat widget library that connects directly with Dialogflow CX - no backend API required!
4
+
5
+ **Supports:** React, Next.js, Vue.js, Nuxt 3, Vite, and Vanilla JavaScript
6
+
7
+ ## Installation
8
+
9
+ ### Option 1: Install from Local Path (Recommended for Development)
10
+
11
+ ```bash
12
+ # In your website project directory
13
+ npm install /path/to/blockspark-chat-widget
14
+ # or
15
+ npm install ../blockspark-chat-widget
16
+ ```
17
+
18
+ ### Option 2: Use npm link (For Development)
19
+
20
+ ```bash
21
+ # In the blockspark-chat-widget directory
22
+ npm link
23
+
24
+ # In your website project directory
25
+ npm link @blockspark/chat-widget
26
+ ```
27
+
28
+ ### Option 3: Publish to npm (For Production)
29
+
30
+ ```bash
31
+ # Build the library first
32
+ npm run build
33
+
34
+ # Publish to npm (make sure you're logged in)
35
+ npm publish
36
+ ```
37
+
38
+ Then install in your project:
39
+ ```bash
40
+ npm install @blockspark/chat-widget
41
+ ```
42
+
43
+ ## Usage
44
+
45
+ ### Vue.js 3
46
+
47
+ ```vue
48
+ <template>
49
+ <ChatWidget
50
+ df-project-id="your-project-id"
51
+ df-agent-id="your-agent-id"
52
+ :service-account-key="serviceAccountKey"
53
+ backendBaseUrl="https://chartconnect.blockspark.in"
54
+ backendWsUrl="wss://chartconnect.blockspark.in"
55
+ />
56
+ </template>
57
+
58
+ <script setup>
59
+ import ChatWidget from '@blockspark/chat-widget/vue';
60
+ // ⚠️ REQUIRED: Import CSS separately
61
+ import '@blockspark/chat-widget/styles';
62
+ import serviceAccountKey from './credentials/dialogflow.json';
63
+ </script>
64
+ ```
65
+
66
+ **CSS Import Options:**
67
+ - `import '@blockspark/chat-widget/styles'` (recommended)
68
+ - `import '@blockspark/chat-widget/dist/styles.css'` (alternative)
69
+
70
+ ### Nuxt 3 / Vue.js 3 (Important: Use ClientOnly!)
71
+
72
+ ```vue
73
+ <template>
74
+ <ClientOnly>
75
+ <ChatWidget
76
+ df-project-id="your-project-id"
77
+ df-agent-id="your-agent-id"
78
+ :service-account-key="serviceAccountKey"
79
+ />
80
+ </ClientOnly>
81
+ </template>
82
+
83
+ <script setup>
84
+ import ChatWidget from '@blockspark/chat-widget/nuxt';
85
+ import '@blockspark/chat-widget/styles';
86
+ import serviceAccountKey from './credentials/dialogflow.json';
87
+ </script>
88
+ ```
89
+
90
+ **In `nuxt.config.ts`:**
91
+ ```typescript
92
+ export default defineNuxtConfig({
93
+ css: ['@blockspark/chat-widget/dist/styles.css'],
94
+
95
+ vite: {
96
+ optimizeDeps: {
97
+ exclude: ['@blockspark/chat-widget'],
98
+ },
99
+ },
100
+ });
101
+ ```
102
+
103
+ **⚠️ CRITICAL for Nuxt:** The component MUST be wrapped in `<ClientOnly>` because it uses browser-only APIs (`localStorage`, `window`, `WebSocket`) that don't exist during SSR.
104
+
105
+ **CSS Import Options:**
106
+ - `import '@blockspark/chat-widget/styles'` (recommended - shorter path)
107
+ - `import '@blockspark/chat-widget/dist/styles.css'` (alternative)
108
+
109
+ ### React/Next.js
110
+
111
+ ```tsx
112
+ import ChatWidget from '@blockspark/chat-widget';
113
+ import '@blockspark/chat-widget/dist/styles.css';
114
+ import serviceAccountKey from './service-account-key.json';
115
+
116
+ function App() {
117
+ return (
118
+ <ChatWidget
119
+ dfProjectId="your-project-id"
120
+ dfLocation="us-central1"
121
+ dfAgentId="your-agent-id"
122
+ serviceAccountKey={serviceAccountKey}
123
+ languageCode="en"
124
+ />
125
+ );
126
+ }
127
+ ```
128
+
129
+ ## Props
130
+
131
+ | Prop | Type | Required | Default | Description |
132
+ |------|------|----------|---------|-------------|
133
+ | `dfProjectId` | `string` | Yes | - | Dialogflow project ID |
134
+ | `dfLocation` | `string` | No | `"us-central1"` | Dialogflow location |
135
+ | `dfAgentId` | `string` | Yes | - | Dialogflow agent ID |
136
+ | `serviceAccountKey` | `object` | Yes* | - | Google Cloud service account key JSON object |
137
+ | `accessToken` | `string` | Yes* | - | Access token (alternative to serviceAccountKey) |
138
+ | `languageCode` | `string` | No | `"en"` | Language code for Dialogflow |
139
+ | `title` | `string` | No | `"💬 BlockSpark AI Assistant"` | Chat widget title |
140
+ | `subtitle` | `string` | No | `"We're here to help"` | Chat widget subtitle |
141
+ | `welcomeTitle` | `string` | No | `"👋 Welcome to Blockspark"` | Welcome popup title |
142
+ | `welcomeMessage` | `string` | No | `"My name is BlockSpark..."` | Welcome popup message |
143
+ | `welcomeCta` | `string` | No | `"💬 Click here to start chatting!"` | Welcome popup CTA text |
144
+ | `showWelcomePopup` | `boolean` | No | `true` | Whether to show welcome popup |
145
+ | `welcomePopupDelay` | `number` | No | `1500` | Delay before showing welcome popup (ms) |
146
+ | `fallbackWelcomeMessage` | `string` | No | `"Hello! I'm BlockSpark..."` | Fallback message if API fails |
147
+ | `inputPlaceholder` | `string` | No | `"Type your message..."` | Input field placeholder |
148
+ | `emptyStateMessage` | `string` | No | `"Hi! I'm BlockSpark..."` | Empty state message |
149
+ | `debug` | `boolean` | No | `false` | Enable debug logging |
150
+ | `backendBaseUrl` | `string` | No | `"http://localhost:8012"` | Backend REST API base URL for Human Support Mode |
151
+ | `backendWsUrl` | `string` | No | `"ws://localhost:8012"` | Backend WebSocket URL for Human Support Mode |
152
+
153
+ \* Either `serviceAccountKey` or `accessToken` must be provided.
154
+
155
+ ## Features
156
+
157
+ - ✅ **Direct Dialogflow CX Integration** - No backend required
158
+ - ✅ **Human Handoff Support** - Automatic transition from bot to human agents
159
+ - ✅ **Rich Content** - Supports Dialogflow chips, cards, and more
160
+ - ✅ **Real-time Messaging** - WebSocket support for human agents
161
+ - ✅ **SSR Compatible** - Works with Next.js and Nuxt 3 (with ClientOnly wrapper)
162
+ - ✅ **Framework Agnostic** - Same UI across React and Vue
163
+ - ✅ **TypeScript Support** - Full type definitions included
164
+ - ✅ **Tree-shakeable** - Only import what you need
165
+
166
+ ## How It Works
167
+
168
+ The widget connects **directly** to Dialogflow CX using the REST API - no backend required!
169
+
170
+ 1. **Authentication**: Uses Google Cloud service account key to generate OAuth2 access tokens
171
+ 2. **Session Management**: Creates and manages Dialogflow sessions automatically
172
+ 3. **Message Handling**: Sends messages directly to Dialogflow and displays responses
173
+ 4. **Rich Content**: Supports Dialogflow rich content (chips, cards, etc.)
174
+ 5. **Human Handoff**: Automatically switches to human support mode when Dialogflow returns handoff signal
175
+
176
+ ## Requirements
177
+
178
+ ### For React/Next.js
179
+ - React 16.8.0 or higher (peer dependency)
180
+ - React DOM 16.8.0 or higher (peer dependency)
181
+
182
+ ### For Vue.js/Nuxt
183
+ - Vue 3.0.0 or higher (peer dependency)
184
+
185
+ **Note:** Peer dependencies are optional - install only what you need for your framework.
186
+
187
+ ## Getting Your Service Account Key
188
+
189
+ 1. Go to [Google Cloud Console](https://console.cloud.google.com/)
190
+ 2. Select your project
191
+ 3. Navigate to **IAM & Admin** > **Service Accounts**
192
+ 4. Create a new service account or select an existing one
193
+ 5. Create a JSON key and download it
194
+ 6. Enable **Dialogflow API** for your project
195
+ 7. Grant the service account **Dialogflow API User** role
196
+
197
+ ## Security Warning ⚠️
198
+
199
+ **Important**: Service account keys contain sensitive credentials.
200
+
201
+ - **For Development**: You can import the key directly (as shown in examples)
202
+ - **For Production**:
203
+ - **DO NOT** expose service account keys in client-side code
204
+ - Use a backend proxy to handle authentication
205
+ - Or use OAuth2 flow to get access tokens securely
206
+ - Consider using restricted service account keys with minimal permissions
207
+
208
+ ## Development
209
+
210
+ ```bash
211
+ # Install dependencies
212
+ npm install
213
+
214
+ # Build for production
215
+ npm run build
216
+
217
+ # Watch mode for development
218
+ npm run dev
219
+ ```
220
+
221
+ ## Troubleshooting
222
+
223
+ ### Vue.js/Nuxt Issues
224
+
225
+ **Widget not displaying?**
226
+ - ✅ Make sure you imported the CSS: `import '@blockspark/chat-widget/styles'`
227
+ - ✅ For Nuxt: Wrap in `<ClientOnly>` component
228
+ - ✅ For Nuxt: Add CSS to `nuxt.config.ts` and exclude from optimization
229
+
230
+ **Vue warnings about attributes?**
231
+ - ✅ This is fixed in the latest version
232
+ - ✅ Ensure you're using the latest package version
233
+
234
+ **Nuxt vite-node errors?**
235
+ - ✅ Wrap component in `<ClientOnly>`
236
+ - ✅ Add to `nuxt.config.ts`:
237
+ ```ts
238
+ vite: {
239
+ optimizeDeps: {
240
+ exclude: ['@blockspark/chat-widget'],
241
+ },
242
+ }
243
+ ```
244
+ - ✅ Clear cache: `rm -rf .nuxt node_modules/.vite`
245
+
246
+ **504 Outdated Optimize Dep error?**
247
+ - ✅ Clear Vite cache: `rm -rf node_modules/.vite && npm run dev`
248
+ - ✅ Or add to `vite.config.ts`:
249
+ ```ts
250
+ optimizeDeps: {
251
+ exclude: ['@blockspark/chat-widget'],
252
+ }
253
+ ```
254
+
255
+ ### General Issues
256
+
257
+ **Chat not opening?**
258
+ - ✅ Check CSS is imported
259
+ - ✅ Verify Dialogflow config is provided
260
+ - ✅ Enable debug mode: `:debug="true"` or `debug={true}` to see console logs
261
+
262
+ **Dialogflow errors?**
263
+ - ✅ Verify service account key is valid
264
+ - ✅ Check Dialogflow API is enabled
265
+ - ✅ Ensure service account has Dialogflow API User role
266
+ - ✅ Verify project ID, location, and agent ID are correct
267
+
268
+ ## License
269
+
270
+ MIT
@@ -0,0 +1,2 @@
1
+ "use strict";const e=require("vue"),t=require("./sanitize-C8MB41vY.cjs");class s{constructor(e){if(this.listeners=new Set,this.chatMode="ai",this.chatId=null,this.supportSessionId=null,this.chatService=null,this.collectingUserInfo=!1,this.userInfoStep=null,this.collectedUserName="",this.collectedUserEmail="",this.collectedUserMobile="",this.wsConnected=!1,this.agentTyping=!1,this.currentAgent={name:"Agent"},this.isConnectingToAgent=!1,this.agentAccepted=!1,this.chatResolved=!1,this.historyLoaded=null,this.typingTimeout=null,this.agentTypingTimeout=null,this.config=e,this.state={isOpen:!1,showWelcomePopup:!1,messages:[],inputValue:"",isLoading:!1,error:null,sessionId:null,chatMode:"ai"},this.chatService=t.createChatService({baseUrl:this.config.backendBaseUrl||"http://localhost:8012",wsUrl:this.config.backendWsUrl||"ws://localhost:8012",debug:this.config.debug||!1}),"undefined"!=typeof window&&window.localStorage){const e=localStorage.getItem("blockspark_chat_mode");this.chatMode="HUMAN"===e?"human":"ai",this.state.chatMode=this.chatMode,this.chatId=localStorage.getItem("blockspark_chat_id"),this.supportSessionId=localStorage.getItem("blockspark_session_id")}}subscribe(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}getState(){return{...this.state}}setState(e){this.state={...this.state,...e},this.listeners.forEach(e=>e(this.getState()))}updateConfig(e){this.config={...this.config,...e}}async openChat(){if(this.setState({isOpen:!0}),this.state.showWelcomePopup&&this.setState({showWelcomePopup:!1}),"ai"===this.state.chatMode&&!this.state.sessionId&&this.config.dfProjectId&&this.config.dfAgentId)try{this.setState({isLoading:!0});const e={dfProjectId:this.config.dfProjectId,dfLocation:this.config.dfLocation||"us-central1",dfAgentId:this.config.dfAgentId,languageCode:this.config.languageCode||"en",backendBaseUrl:this.config.backendBaseUrl||"http://localhost:8012"},s=await t.createDialogflowSession(e);if(this.setState({sessionId:s.session_id,isLoading:!1}),s.message){const e={id:`welcome-${Date.now()}`,text:s.message,sender:"bot",timestamp:new Date,richContent:s.richContent};this.setState({messages:[e]})}}catch(e){this.setState({isLoading:!1,error:e.message||"Failed to initialize chat"});const t={id:`fallback-${Date.now()}`,text:this.config.fallbackWelcomeMessage||"Hello! How can I help you today?",sender:"bot",timestamp:new Date};this.setState({messages:[t]})}}closeChat(){this.setState({isOpen:!1})}closeWelcomePopup(){this.setState({showWelcomePopup:!1})}toggleChat(){const e=!this.state.isOpen;this.setState({isOpen:e}),e&&this.state.showWelcomePopup&&this.setState({showWelcomePopup:!1})}setInputValue(e){this.setState({inputValue:e})}clearError(){this.setState({error:null})}async sendMessage(e,s=!1){if(e.trim()&&!this.state.isLoading){if(this.collectingUserInfo){const t={id:`user-${Date.now()}`,text:e.trim(),sender:"user",timestamp:new Date};return this.setState({messages:[...this.state.messages,t],inputValue:"",isLoading:!1,error:null}),void(await this.handleUserInfoCollection(e))}if(s)this.setState({inputValue:"",isLoading:!0,error:null});else{const t={id:`user-${Date.now()}`,text:e.trim(),sender:"user",timestamp:new Date};this.setState({messages:[...this.state.messages,t],inputValue:"",isLoading:!0,error:null})}try{"human"===this.state.chatMode?await this.sendHumanMessage(e):await this.sendAIMessage(e)}catch(a){if(a instanceof t.ChatResolvedError||"ChatResolvedError"===a?.name||"chat_resolved"===a?.message)return void this.enterResolvedState(this.chatId);const e={id:`error-${Date.now()}`,text:this.config.debug?`Error: ${a.message||"Failed to send message"}`:a.message?.includes("CORS")||a.message?.includes("Failed to fetch")?"Unable to connect to Dialogflow. Please check your configuration and network.":"Sorry, I'm having trouble processing your message. Please try again.",sender:"bot",timestamp:new Date};this.setState({messages:[...this.state.messages,e],error:a.message||"Failed to send message",isLoading:!1})}}}async sendAIMessage(e){if(!this.config.dfProjectId||!this.config.dfAgentId)throw new Error("Dialogflow configuration is missing");const s={dfProjectId:this.config.dfProjectId,dfLocation:this.config.dfLocation||"us-central1",dfAgentId:this.config.dfAgentId,languageCode:this.config.languageCode||"en",backendBaseUrl:this.config.backendBaseUrl||"http://localhost:8012"};if(!this.state.sessionId){const e=await t.createDialogflowSession(s);this.setState({sessionId:e.session_id})}this.config.debug;const a=await t.sendDialogflowMessage(e,this.state.sessionId,s);if(this.config.debug,!0===a.handoff){const e={id:`bot-${Date.now()}`,text:a.response||this.config.fallbackWelcomeMessage||"No response",sender:"bot",timestamp:new Date,richContent:a.richContent};return this.setState({messages:[...this.state.messages,e],isLoading:!1}),void this.startUserInfoCollection()}const o={id:`bot-${Date.now()}`,text:a.response||this.config.fallbackWelcomeMessage||"No response",sender:"bot",timestamp:new Date,richContent:a.richContent};this.setState({messages:[...this.state.messages,o],isLoading:!1})}async sendHumanMessage(e){if(!this.chatId||!this.supportSessionId){const e={id:`error-${Date.now()}`,text:"Chat session not initialized. Please try again.",sender:"bot",timestamp:new Date};return void this.setState({messages:[...this.state.messages,e],isLoading:!1})}if(!this.chatService)throw new Error("Chat service not initialized");this.chatService.sendTypingIndicator("typing_stop"),this.typingTimeout&&(clearTimeout(this.typingTimeout),this.typingTimeout=null);try{this.chatService.sendMessageViaWebSocket(e.trim())||await this.chatService.sendMessageToAgent(this.chatId,this.supportSessionId,e.trim()),this.setState({isLoading:!1})}catch(s){if(s instanceof t.ChatResolvedError||"ChatResolvedError"===s?.name||"chat_resolved"===s?.message)return void this.enterResolvedState(this.chatId);if(!(s.message?.includes("Chat not found")||s.message?.includes("unauthorized")||s.message?.includes("401")||s.message?.includes("404")))throw s;this.config.debug,this.chatId=null,this.supportSessionId=null,"undefined"!=typeof window&&window.localStorage&&(localStorage.removeItem("blockspark_chat_id"),localStorage.removeItem("blockspark_session_id"));try{const t=await this.chatService.startSupportChat(this.state.sessionId||null,null,null,null);return this.chatId=t.chat_id,this.supportSessionId=t.session_id,"undefined"!=typeof window&&window.localStorage&&(localStorage.setItem("blockspark_chat_id",this.chatId),localStorage.setItem("blockspark_session_id",this.supportSessionId)),this.chatId&&this.supportSessionId&&await this.chatService.sendMessageToAgent(this.chatId,this.supportSessionId,e.trim()),void this.setState({isLoading:!1})}catch(a){if(a instanceof t.ChatResolvedError||"chat_resolved"===a?.message)return void this.enterResolvedState(this.chatId);throw a}}}switchToHumanMode(){this.chatMode="human",this.setState({chatMode:"human"}),"undefined"!=typeof window&&window.localStorage&&localStorage.setItem("blockspark_chat_mode","HUMAN")}switchToBotMode(){this.chatMode="ai",this.setState({chatMode:"ai"}),"undefined"!=typeof window&&window.localStorage&&localStorage.setItem("blockspark_chat_mode","BOT")}initializeWelcomePopup(){if(!1!==this.config.showWelcomePopup){const e=this.config.welcomePopupDelay||1500;setTimeout(()=>{this.state.isOpen||this.setState({showWelcomePopup:!0})},e)}}startUserInfoCollection(){this.collectingUserInfo=!0,this.userInfoStep="name",this.collectedUserName="",this.collectedUserEmail="",this.collectedUserMobile="";const e={id:`prompt-${Date.now()}`,text:"To connect you with a human agent, I'll need some information. Please provide your name:",sender:"bot",timestamp:new Date};this.setState({messages:[...this.state.messages,e]})}async handleUserInfoCollection(e){if("name"===this.userInfoStep){const t=e.trim();this.collectedUserName=t,this.userInfoStep="email";const s={id:`prompt-${Date.now()}`,text:"Thank you! Now please provide your email address:",sender:"bot",timestamp:new Date};return void this.setState({messages:[...this.state.messages,s],inputValue:""})}if("email"===this.userInfoStep){const t=e.trim();if(!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(t)){const e={id:`prompt-${Date.now()}`,text:"Please provide a valid email address:",sender:"bot",timestamp:new Date};return void this.setState({messages:[...this.state.messages,e],inputValue:""})}this.collectedUserEmail=t,this.userInfoStep="mobile";const s={id:`prompt-${Date.now()}`,text:"Thank you! Now please provide your mobile number:",sender:"bot",timestamp:new Date};return void this.setState({messages:[...this.state.messages,s],inputValue:""})}if("mobile"===this.userInfoStep){const t=e.trim();if(!/^[\+]?[(]?[0-9]{1,4}[)]?[-\s\.]?[(]?[0-9]{1,4}[)]?[-\s\.]?[0-9]{1,9}$/.test(t)||t.length<10){const e={id:`prompt-${Date.now()}`,text:"Please provide a valid mobile number (e.g., +1234567890):",sender:"bot",timestamp:new Date};return void this.setState({messages:[...this.state.messages,e],inputValue:""})}this.collectedUserMobile=t,this.collectingUserInfo=!1,this.userInfoStep=null,await this.handleHandoff(this.collectedUserName,this.collectedUserEmail,t),this.setState({inputValue:""})}}async handleHandoff(e,t,s){if(!this.chatService)throw new Error("Chat service not initialized");try{this.isConnectingToAgent=!0;const o=this.state.sessionId,n=await this.chatService.ensureChatInitialized(this.chatId,this.supportSessionId,o||null,e||null,t||null,s||null);if(!n||!n.chat_id)throw new Error("Failed to initialize chat session");const i=n.chat_id,c=n.session_id;i!==this.chatId&&(this.chatId=i,this.supportSessionId=c,"undefined"!=typeof window&&window.localStorage&&(localStorage.setItem("blockspark_chat_id",this.chatId),localStorage.setItem("blockspark_session_id",this.supportSessionId)),this.config.debug);try{await this.chatService.requestHandoff(i,c,"Customer requested human agent",o||null,e||null,t||null,s||null),this.config.debug}catch(a){if(!(a.message?.includes("Invalid chat_id")||a.message?.includes("Chat not found")||a.message?.includes("unauthorized")||a.message?.includes("400")||a.message?.includes("401")||a.message?.includes("404")||a.message?.includes("expired")))throw a;{this.config.debug,this.chatId=null,this.supportSessionId=null,"undefined"!=typeof window&&window.localStorage&&(localStorage.removeItem("blockspark_chat_id"),localStorage.removeItem("blockspark_session_id"));const a=await this.chatService.startSupportChat(o||null,e||null,t||null,s||null);if(!a||!a.chat_id)throw new Error("Failed to re-initialize chat session");this.chatId=a.chat_id,this.supportSessionId=a.session_id,"undefined"!=typeof window&&window.localStorage&&(localStorage.setItem("blockspark_chat_id",this.chatId),localStorage.setItem("blockspark_session_id",this.supportSessionId)),await this.chatService.requestHandoff(this.chatId,this.supportSessionId,"Customer requested human agent",o||null,e||null,t||null,s||null),this.config.debug}}this.switchToHumanMode(),this.chatResolved=!1,this.agentAccepted=!1;const l={id:`connecting-${Date.now()}`,text:"Connecting you to a human agent...",sender:"bot",timestamp:new Date};this.setState({messages:[...this.state.messages,l]}),i&&c&&this.chatService.connectWebSocket(i,c,e=>{this.handleWebSocketMessage(e)},e=>{this.wsConnected=e}),this.isConnectingToAgent=!1}catch(o){const e={id:`error-${Date.now()}`,text:this.config.debug?`Handoff error: ${o.message}`:"Failed to connect to agent. Please try again.",sender:"bot",timestamp:new Date};this.setState({messages:[...this.state.messages,e]}),this.isConnectingToAgent=!1}}handleWebSocketMessage(e){switch(e.type){case"message":if(e.content&&("agent"===e.sender_type||!e.sender_type)){const t={id:e.id||`agent-${Date.now()}`,text:e.content,sender:"agent",timestamp:new Date(e.timestamp||Date.now())};new Set(this.state.messages.map(e=>e.id)).has(t.id)||this.setState({messages:[...this.state.messages,t]}),this.agentTyping=!1,this.agentTypingTimeout&&(clearTimeout(this.agentTypingTimeout),this.agentTypingTimeout=null)}break;case"typing_start":"agent"===e.sender_type&&(this.agentTyping=!0,this.agentTypingTimeout&&clearTimeout(this.agentTypingTimeout),this.agentTypingTimeout=setTimeout(()=>{this.agentTyping=!1},3e3));break;case"typing_stop":"agent"===e.sender_type&&(this.agentTyping=!1,this.agentTypingTimeout&&(clearTimeout(this.agentTypingTimeout),this.agentTypingTimeout=null));break;case"agent_changed":if(e.to_agent){this.currentAgent={id:e.to_agent_id,name:e.to_agent};const t={id:`system-${Date.now()}`,text:e.from_agent?`Chat has been transferred from ${e.from_agent} to ${e.to_agent}`:`Chat has been transferred to ${e.to_agent}`,sender:"bot",timestamp:new Date};this.setState({messages:[...this.state.messages,t]})}break;case"agent_accepted":this.agentAccepted=!0,this.isConnectingToAgent=!1;const t={id:e.id||`agent-accepted-${Date.now()}`,text:"You can chat now, the agent has accepted your request.",sender:"bot",timestamp:e.timestamp?new Date(e.timestamp):new Date};new Set(this.state.messages.map(e=>e.id)).has(t.id)||this.setState({messages:[...this.state.messages,t]}),e.to_agent&&(this.currentAgent={name:e.to_agent,id:e.to_agent_id});break;case"chat_resolved":case"chat_ended":this.enterResolvedState(e.chat_id||null);break;case"chat_info":"active"===e.status?(this.isConnectingToAgent=!1,this.agentAccepted=!0):"resolved"!==e.status&&"ended"!==e.status||this.enterResolvedState(e.chat_id||null),e.agent_id&&(this.currentAgent={...this.currentAgent,id:e.agent_id});break;case"error":const s={id:`error-${Date.now()}`,text:e.error||"An error occurred. Please try again.",sender:"bot",timestamp:new Date};this.setState({messages:[...this.state.messages,s]})}}enterResolvedState(e){this.chatResolved=!0,this.isConnectingToAgent=!1,this.agentAccepted=!1,this.agentTyping=!1,this.agentTypingTimeout&&(clearTimeout(this.agentTypingTimeout),this.agentTypingTimeout=null),this.chatService&&this.chatService.disconnectWebSocket(),this.chatId=null,this.supportSessionId=null,"undefined"!=typeof window&&window.localStorage&&(localStorage.removeItem("blockspark_chat_id"),localStorage.removeItem("blockspark_session_id"));const s={id:`resolved-${Date.now()}`,text:"Thank you for contacting us!",sender:"bot",timestamp:new Date};this.setState({messages:[...this.state.messages,s]}),setTimeout(async()=>{if(this.switchToBotMode(),this.chatResolved=!1,this.setState({messages:[],sessionId:null}),this.collectingUserInfo=!1,this.userInfoStep=null,this.collectedUserName="",this.collectedUserEmail="",this.collectedUserMobile="",this.config.dfProjectId&&this.config.dfAgentId&&this.state.isOpen)try{this.setState({isLoading:!0});const e={dfProjectId:this.config.dfProjectId,dfLocation:this.config.dfLocation||"us-central1",dfAgentId:this.config.dfAgentId,languageCode:this.config.languageCode||"en",backendBaseUrl:this.config.backendBaseUrl||"http://localhost:8012"},s=await t.createDialogflowSession(e);if(this.setState({sessionId:s.session_id,isLoading:!1}),s.message){const e={id:`welcome-${Date.now()}`,text:s.message,sender:"bot",timestamp:new Date,richContent:s.richContent};this.setState({messages:[e]})}}catch(e){this.setState({isLoading:!1,error:e.message||"Failed to initialize chat"});const t={id:`fallback-${Date.now()}`,text:this.config.fallbackWelcomeMessage||"Hello! How can I help you today?",sender:"bot",timestamp:new Date};this.setState({messages:[t]})}},2e3)}async loadMessageHistory(e=!1){if(this.chatService&&this.chatId&&this.supportSessionId)try{if(this.chatId&&this.supportSessionId){const t=(await this.chatService.loadMessageHistory(this.chatId,this.supportSessionId)).map(e=>({id:e.id||`history-${Date.now()}-${Math.random()}`,text:e.content,sender:"agent"===e.sender_type?"agent":"user",timestamp:new Date(e.timestamp)}));if(e){const e=new Set(this.state.messages.map(e=>e.id)),s=t.filter(t=>!e.has(t.id)),a=[...this.state.messages,...s].sort((e,t)=>e.timestamp.getTime()-t.timestamp.getTime());this.setState({messages:a})}else this.setState({messages:t})}}catch(t){if(this.config.debug){const e={id:`error-${Date.now()}`,text:`Failed to load chat history: ${t.message}`,sender:"bot",timestamp:new Date};this.setState({messages:[...this.state.messages,e]})}}}getAdditionalState(){return{wsConnected:this.wsConnected,agentTyping:this.agentTyping,currentAgent:this.currentAgent,isConnectingToAgent:this.isConnectingToAgent,agentAccepted:this.agentAccepted,chatResolved:this.chatResolved}}destroy(){this.typingTimeout&&clearTimeout(this.typingTimeout),this.agentTypingTimeout&&clearTimeout(this.agentTypingTimeout),this.chatService&&this.chatService.disconnectWebSocket(),this.listeners.clear()}}function a(t){const a=new s(t),o=e.ref(a.getState()),n=e.ref(!1),i=e.ref(!1),c=e.ref({name:"Agent"}),l=e.ref(!1),r=a.subscribe(e=>{o.value={...e};const t=a.getAdditionalState();n.value=t.wsConnected,i.value=t.agentTyping,c.value=t.currentAgent,l.value=t.isConnectingToAgent}),d=e.computed(()=>o.value.isOpen),h=e.computed(()=>o.value.messages),m=e.computed(()=>o.value.isLoading),g=e.computed(()=>o.value.error),u=e.computed(()=>o.value.chatMode);return e.onUnmounted(()=>{r(),a.destroy()}),e.watch(()=>t,e=>{a.updateConfig(e)},{deep:!0}),{state:o,isOpen:d,messages:h,isLoading:m,error:g,chatMode:u,wsConnected:n,agentTyping:i,currentAgent:c,isConnectingToAgent:l,openChat:async()=>{await a.openChat()},closeChat:()=>{a.closeChat()},sendMessage:async e=>{await a.sendMessage(e)},toggleChat:async()=>{await a.toggleChat()},setInputValue:e=>{a.setInputValue(e)},clearError:()=>{a.clearError()},manager:a}}const o={class:"custom-welcome-header"},n={class:"custom-welcome-title"},i={class:"custom-welcome-message"},c={class:"custom-welcome-cta"},l={key:2,class:"custom-chat-window"},r={class:"custom-chat-header"},d={class:"custom-chat-header-content"},h={class:"custom-chat-title"},m={class:"custom-chat-subtitle"},g={key:0,class:"custom-mode-indicator"},u={key:0,class:"custom-mode-badge"},p={key:1,class:"custom-agent-info"},f={class:"custom-agent-name"},w={key:2,class:"custom-mode-badge"},y={key:0,class:"custom-chat-empty"},k={key:1,class:"custom-chat-empty"},S=["innerHTML"],v={key:0,class:"custom-chips-container"},I={key:0,class:"custom-chips-group"},b=["onClick"],C={key:0,class:"custom-chips-group"},E=["onClick"],T={class:"custom-message-time"},_={key:2,class:"custom-message custom-message-bot"},V={key:3,class:"custom-message custom-message-bot"},B={key:4,class:"custom-agent-typing-indicator"},N=["value","placeholder","disabled"],D=["disabled"],A=((e,t)=>{const s=e.__vccOpts||e;for(const[a,o]of t)s[a]=o;return s})(e.defineComponent({inheritAttrs:!1,__name:"ChatWidget",props:{dfProjectId:{},dfLocation:{},dfAgentId:{},languageCode:{},backendBaseUrl:{},backendWsUrl:{},title:{default:"💬 BlockSpark AI Assistant"},subtitle:{default:"We're here to help"},welcomeTitle:{default:"👋 Welcome to BlockSpark"},welcomeMessage:{default:"My name is BlockSpark AI Assistant and I'll guide you."},welcomeCta:{default:"💬 Click here to start chatting!"},showWelcomePopup:{type:Boolean,default:!0},welcomePopupDelay:{default:1500},fallbackWelcomeMessage:{default:"Hello! I'm BlockSpark AI Assistant. How can I help you today?"},inputPlaceholder:{default:"Type your message..."},emptyStateMessage:{default:"Hi! I'm BlockSpark AI Assistant. How can I help you today?"},debug:{type:Boolean,default:!1}},setup(s){const A=s,{state:M,isOpen:x,messages:L,isLoading:P,error:U,chatMode:$,wsConnected:W,agentTyping:H,currentAgent:O,isConnectingToAgent:R,openChat:z,closeChat:F,sendMessage:j,toggleChat:q,setInputValue:Y,clearError:G,manager:J}=a(A),K=e.ref(null),Q=e.ref(null),X=e.computed(()=>A),Z=e.computed(()=>M.value.isLoading&&0===M.value.messages.length&&!M.value.sessionId),ee=e.computed(()=>!1),te=R;e.watch(()=>L.value.length,()=>{e.nextTick(()=>{K.value?.scrollIntoView({behavior:"smooth"})})});const se=async()=>{M.value.isOpen||(M.value.isOpen=!0,M.value.showWelcomePopup=!1),await z()},ae=()=>{F()},oe=()=>{J.closeWelcomePopup()},ne=e=>{const t=e.target;Y(t.value)},ie=async e=>{e.preventDefault(),M.value.inputValue.trim()&&await j(M.value.inputValue)},ce=async(e,t)=>{const s=e||t||"";await j(s)},le=e=>e.includes("👤")||e.includes("being connected")||e.includes("live support agent")||e.includes("transfer your conversation");return e.onMounted(()=>{X.value.showWelcomePopup&&"undefined"!=typeof window&&J.initializeWelcomePopup()}),(s,a)=>(e.openBlock(),e.createElementBlock("div",e.mergeProps({class:"custom-chat-widget"},s.$attrs),[e.unref(M).showWelcomePopup&&!e.unref(M).isOpen?(e.openBlock(),e.createElementBlock("div",{key:0,class:"custom-welcome-popup",onClick:se},[e.createElementVNode("div",o,[e.createElementVNode("div",n,e.toDisplayString(X.value.welcomeTitle),1),e.createElementVNode("button",{class:"custom-close-popup",onClick:e.withModifiers(oe,["stop"]),"aria-label":"Close welcome popup"}," × ")]),e.createElementVNode("div",i,e.toDisplayString(X.value.welcomeMessage),1),e.createElementVNode("div",c,e.toDisplayString(X.value.welcomeCta),1)])):e.createCommentVNode("",!0),e.unref(M).isOpen?e.createCommentVNode("",!0):(e.openBlock(),e.createElementBlock("button",{key:1,class:"custom-chat-toggle-btn",onClick:se,"aria-label":"Open chat"},[...a[0]||(a[0]=[e.createElementVNode("svg",{width:"24",height:"24",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor","stroke-width":"2","stroke-linecap":"round","stroke-linejoin":"round"},[e.createElementVNode("path",{d:"M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"})],-1)])])),e.unref(M).isOpen?(e.openBlock(),e.createElementBlock("div",l,[e.createElementVNode("div",r,[e.createElementVNode("div",d,[e.createElementVNode("div",h,e.toDisplayString(X.value.title),1),e.createElementVNode("div",m,[e.createTextVNode(e.toDisplayString(X.value.subtitle)+" ",1),"human"===e.unref(M).chatMode?(e.openBlock(),e.createElementBlock("span",g," • "+e.toDisplayString(e.unref(W)?"🟢 Connected":"🟡 Connecting..."),1)):e.createCommentVNode("",!0)]),"human"===e.unref(M).chatMode?(e.openBlock(),e.createElementBlock("div",u," Human Support Mode ")):e.createCommentVNode("",!0),"human"===e.unref(M).chatMode?(e.openBlock(),e.createElementBlock("div",p,[a[1]||(a[1]=e.createElementVNode("span",{class:"custom-agent-label"},"Agent:",-1)),e.createElementVNode("span",f,e.toDisplayString(e.unref(O).name),1)])):e.createCommentVNode("",!0),"ai"===e.unref(M).chatMode?(e.openBlock(),e.createElementBlock("div",w," Bot Mode ")):e.createCommentVNode("",!0)]),e.createElementVNode("button",{class:"custom-chat-close-btn",onClick:ae,"aria-label":"Close chat"}," × ")]),e.createElementVNode("div",{class:"custom-chat-messages",ref_key:"messagesContainer",ref:Q},[Z.value&&0===e.unref(M).messages.length?(e.openBlock(),e.createElementBlock("div",y,[...a[2]||(a[2]=[e.createElementVNode("div",{class:"custom-typing-indicator"},[e.createElementVNode("span"),e.createElementVNode("span"),e.createElementVNode("span")],-1),e.createElementVNode("p",null,"Initializing chat...",-1)])])):Z.value||0!==e.unref(M).messages.length?e.createCommentVNode("",!0):(e.openBlock(),e.createElementBlock("div",k,[a[3]||(a[3]=e.createElementVNode("div",{class:"custom-chat-empty-icon"},"👋",-1)),e.createElementVNode("p",null,e.toDisplayString(X.value.emptyStateMessage),1)])),(e.openBlock(!0),e.createElementBlock(e.Fragment,null,e.renderList(e.unref(M).messages,s=>{return e.openBlock(),e.createElementBlock("div",{key:s.id,class:e.normalizeClass(["custom-message",`custom-message-${s.sender}`,{"custom-handoff-message":le(s.text)}])},[e.createElementVNode("div",{class:e.normalizeClass(["custom-message-content",{"custom-handoff-content":le(s.text)}]),innerHTML:e.unref(t.safeLinkifyText)(s.text).replace(/\n/g,"<br>")},null,10,S),s.richContent&&Array.isArray(s.richContent)&&s.richContent.length>0?(e.openBlock(),e.createElementBlock("div",v,[(e.openBlock(!0),e.createElementBlock(e.Fragment,null,e.renderList(s.richContent,(t,s)=>(e.openBlock(),e.createElementBlock(e.Fragment,{key:s},[Array.isArray(t)?(e.openBlock(!0),e.createElementBlock(e.Fragment,{key:1},e.renderList(t,(t,a)=>(e.openBlock(),e.createElementBlock(e.Fragment,{key:`${s}-${a}`},["chips"===t.type&&t.options?(e.openBlock(),e.createElementBlock("div",C,[(e.openBlock(!0),e.createElementBlock(e.Fragment,null,e.renderList(t.options,(t,s)=>(e.openBlock(),e.createElementBlock("button",{key:s,class:"custom-chip-button",type:"button",onClick:e=>ce(t.text,t.payload)},e.toDisplayString(t.text),9,E))),128))])):e.createCommentVNode("",!0)],64))),128)):(e.openBlock(),e.createElementBlock(e.Fragment,{key:0},["chips"===t.type&&t.options?(e.openBlock(),e.createElementBlock("div",I,[(e.openBlock(!0),e.createElementBlock(e.Fragment,null,e.renderList(t.options,(t,s)=>(e.openBlock(),e.createElementBlock("button",{key:s,class:"custom-chip-button",type:"button",onClick:e=>ce(t.text,t.payload)},e.toDisplayString(t.text),9,b))),128))])):e.createCommentVNode("",!0)],64))],64))),128))])):e.createCommentVNode("",!0),e.createElementVNode("div",T,e.toDisplayString((a=s.timestamp,a.toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"}))),1)],2);var a}),128)),e.unref(M).isLoading?(e.openBlock(),e.createElementBlock("div",_,[...a[4]||(a[4]=[e.createElementVNode("div",{class:"custom-typing-indicator"},[e.createElementVNode("span"),e.createElementVNode("span"),e.createElementVNode("span")],-1)])])):e.createCommentVNode("",!0),e.unref(te)?(e.openBlock(),e.createElementBlock("div",V,[...a[5]||(a[5]=[e.createElementVNode("div",{class:"custom-typing-indicator"},[e.createElementVNode("span"),e.createElementVNode("span"),e.createElementVNode("span")],-1),e.createElementVNode("div",{class:"custom-message-content"}," Connecting to agent... ",-1)])])):e.createCommentVNode("",!0),"human"===e.unref(M).chatMode&&e.unref(H)?(e.openBlock(),e.createElementBlock("div",B,[...a[6]||(a[6]=[e.createElementVNode("span",{class:"custom-typing-dots"},[e.createElementVNode("span"),e.createElementVNode("span"),e.createElementVNode("span")],-1),e.createElementVNode("span",{class:"custom-typing-text"},"Agent is typing...",-1)])])):e.createCommentVNode("",!0),e.createElementVNode("div",{ref_key:"messagesEndRef",ref:K},null,512)],512),e.createElementVNode("form",{class:"custom-chat-input-form",onSubmit:e.withModifiers(ie,["prevent"])},[e.createElementVNode("input",{type:"text",class:"custom-chat-input",value:e.unref(M).inputValue,onInput:ne,placeholder:X.value.inputPlaceholder,disabled:e.unref(M).isLoading||Z.value||ee.value},null,40,N),e.createElementVNode("button",{type:"submit",class:"custom-chat-send-btn",disabled:!e.unref(M).inputValue.trim()||e.unref(M).isLoading||Z.value||ee.value},[...a[7]||(a[7]=[e.createElementVNode("svg",{width:"20",height:"20",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor","stroke-width":"2","stroke-linecap":"round","stroke-linejoin":"round"},[e.createElementVNode("line",{x1:"22",y1:"2",x2:"11",y2:"13"}),e.createElementVNode("polygon",{points:"22 2 15 22 11 13 2 9 22 2"})],-1)])],8,D)],32)])):e.createCommentVNode("",!0)],16))}}),[["__scopeId","data-v-0d32fcf4"]]);exports.ChatWidgetComponent=A,exports.WidgetStateManager=s,exports.useChatWidget=a;
2
+ //# sourceMappingURL=ChatWidget-BM2lXfeE.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ChatWidget-BM2lXfeE.cjs","sources":["../src/core/stateManager.ts","../src/composables/useChatWidget.ts","../src/components/ChatWidget.vue"],"sourcesContent":["/**\n * State Manager - Framework Agnostic\n * Manages widget state independently of framework\n */\n\nimport type { WidgetState, WidgetConfig, ChatMessage } from './types';\nimport {\n createDialogflowSession,\n sendDialogflowMessage,\n type DialogflowBackendConfig,\n} from '../services/dialogflowBackendService';\nimport { \n createChatService, \n ChatResolvedError,\n type WebSocketMessage\n} from '../services/chatService';\n\nexport class WidgetStateManager {\n private state: WidgetState;\n private config: WidgetConfig;\n private listeners: Set<(state: WidgetState) => void> = new Set();\n private chatMode: 'ai' | 'human' = 'ai';\n private chatId: string | null = null;\n private supportSessionId: string | null = null;\n private chatService: ReturnType<typeof createChatService> | null = null;\n \n // User info collection for handoff\n private collectingUserInfo: boolean = false;\n private userInfoStep: 'name' | 'email' | 'mobile' | null = null;\n private collectedUserName: string = '';\n private collectedUserEmail: string = '';\n private collectedUserMobile: string = '';\n \n // Additional state for human support\n private wsConnected: boolean = false;\n private agentTyping: boolean = false;\n private currentAgent: { name: string; id?: string } = { name: 'Agent' };\n private isConnectingToAgent: boolean = false;\n private agentAccepted: boolean = false;\n private chatResolved: boolean = false;\n private historyLoaded: string | null = null;\n \n // Typing timeout refs (stored as class properties since we can't use refs)\n private typingTimeout: NodeJS.Timeout | null = null;\n private agentTypingTimeout: NodeJS.Timeout | null = null;\n\n constructor(config: WidgetConfig) {\n this.config = config;\n this.state = {\n isOpen: false,\n showWelcomePopup: false,\n messages: [],\n inputValue: '',\n isLoading: false,\n error: null,\n sessionId: null,\n chatMode: 'ai',\n };\n \n // Initialize ChatService\n this.chatService = createChatService({\n baseUrl: this.config.backendBaseUrl || 'http://localhost:8012',\n wsUrl: this.config.backendWsUrl || 'ws://localhost:8012',\n debug: this.config.debug || false,\n });\n \n // Load chat mode and chat IDs from localStorage if available\n if (typeof window !== 'undefined' && window.localStorage) {\n const savedMode = localStorage.getItem('blockspark_chat_mode');\n this.chatMode = savedMode === 'HUMAN' ? 'human' : 'ai';\n this.state.chatMode = this.chatMode;\n \n this.chatId = localStorage.getItem('blockspark_chat_id');\n this.supportSessionId = localStorage.getItem('blockspark_session_id');\n }\n }\n\n /**\n * Subscribe to state changes\n */\n subscribe(listener: (state: WidgetState) => void): () => void {\n this.listeners.add(listener);\n return () => {\n this.listeners.delete(listener);\n };\n }\n\n /**\n * Get current state\n */\n getState(): WidgetState {\n return { ...this.state };\n }\n\n /**\n * Update state and notify listeners\n */\n private setState(updates: Partial<WidgetState>): void {\n this.state = { ...this.state, ...updates };\n this.listeners.forEach(listener => listener(this.getState()));\n }\n\n /**\n * Update configuration\n */\n updateConfig(config: Partial<WidgetConfig>): void {\n this.config = { ...this.config, ...config };\n }\n\n /**\n * Open chat\n */\n async openChat(): Promise<void> {\n this.setState({ isOpen: true });\n if (this.state.showWelcomePopup) {\n this.setState({ showWelcomePopup: false });\n }\n \n // Initialize Dialogflow session when chat opens (if in AI mode and no session exists)\n if (this.state.chatMode === 'ai' && !this.state.sessionId && this.config.dfProjectId && this.config.dfAgentId) {\n try {\n this.setState({ isLoading: true });\n const dialogflowConfig: DialogflowBackendConfig = {\n dfProjectId: this.config.dfProjectId,\n dfLocation: this.config.dfLocation || 'us-central1',\n dfAgentId: this.config.dfAgentId,\n languageCode: this.config.languageCode || 'en',\n backendBaseUrl: this.config.backendBaseUrl || 'http://localhost:8012',\n };\n \n const session = await createDialogflowSession(dialogflowConfig);\n this.setState({ \n sessionId: session.session_id,\n isLoading: false,\n });\n \n // Add welcome message from Dialogflow if available\n if (session.message) {\n const welcomeMessage: ChatMessage = {\n id: `welcome-${Date.now()}`,\n text: session.message,\n sender: 'bot',\n timestamp: new Date(),\n richContent: session.richContent,\n };\n this.setState({\n messages: [welcomeMessage],\n });\n }\n } catch (error: any) {\n console.error('Error initializing Dialogflow session:', error);\n this.setState({ \n isLoading: false,\n error: error.message || 'Failed to initialize chat',\n });\n \n // Show fallback welcome message\n const fallbackMessage: ChatMessage = {\n id: `fallback-${Date.now()}`,\n text: this.config.fallbackWelcomeMessage || 'Hello! How can I help you today?',\n sender: 'bot',\n timestamp: new Date(),\n };\n this.setState({\n messages: [fallbackMessage],\n });\n }\n }\n }\n\n /**\n * Close chat\n */\n closeChat(): void {\n this.setState({ isOpen: false });\n }\n\n /**\n * Close welcome popup\n */\n closeWelcomePopup(): void {\n this.setState({ showWelcomePopup: false });\n }\n\n /**\n * Toggle chat\n */\n toggleChat(): void {\n const willBeOpen = !this.state.isOpen;\n this.setState({ isOpen: willBeOpen });\n if (willBeOpen && this.state.showWelcomePopup) {\n this.setState({ showWelcomePopup: false });\n }\n }\n\n /**\n * Set input value\n */\n setInputValue(value: string): void {\n this.setState({ inputValue: value });\n }\n\n /**\n * Clear error\n */\n clearError(): void {\n this.setState({ error: null });\n }\n\n /**\n * Send message\n */\n async sendMessage(text: string, skipUserMessage: boolean = false): Promise<void> {\n if (!text.trim() || this.state.isLoading) {\n return;\n }\n\n // Handle user info collection for handoff\n if (this.collectingUserInfo) {\n // Add user message first so it appears in chat\n const userMessage: ChatMessage = {\n id: `user-${Date.now()}`,\n text: text.trim(),\n sender: 'user',\n timestamp: new Date(),\n };\n this.setState({\n messages: [...this.state.messages, userMessage],\n inputValue: '',\n isLoading: false,\n error: null,\n });\n \n // Then handle the info collection\n await this.handleUserInfoCollection(text);\n return;\n }\n\n // Add user message unless skipped (e.g., when chip button already added it)\n if (!skipUserMessage) {\n const userMessage: ChatMessage = {\n id: `user-${Date.now()}`,\n text: text.trim(),\n sender: 'user',\n timestamp: new Date(),\n };\n\n this.setState({\n messages: [...this.state.messages, userMessage],\n inputValue: '',\n isLoading: true,\n error: null,\n });\n } else {\n this.setState({\n inputValue: '',\n isLoading: true,\n error: null,\n });\n }\n\n try {\n if (this.state.chatMode === 'human') {\n // Human support mode\n await this.sendHumanMessage(text);\n } else {\n // AI mode (Dialogflow)\n await this.sendAIMessage(text);\n }\n } catch (error: any) {\n console.error('Error sending message:', error);\n \n // chat_resolved is a normal terminal condition, not an error\n if (error instanceof ChatResolvedError || error?.name === 'ChatResolvedError' || error?.message === 'chat_resolved') {\n this.enterResolvedState(this.chatId);\n return;\n }\n \n // Add error message to chat\n const errorMessage: ChatMessage = {\n id: `error-${Date.now()}`,\n text: this.config.debug \n ? `Error: ${error.message || 'Failed to send message'}`\n : error.message?.includes('CORS') || error.message?.includes('Failed to fetch')\n ? 'Unable to connect to Dialogflow. Please check your configuration and network.'\n : 'Sorry, I\\'m having trouble processing your message. Please try again.',\n sender: 'bot',\n timestamp: new Date(),\n };\n \n this.setState({\n messages: [...this.state.messages, errorMessage],\n error: error.message || 'Failed to send message',\n isLoading: false,\n });\n }\n }\n\n /**\n * Send message to Dialogflow\n */\n private async sendAIMessage(text: string): Promise<void> {\n if (!this.config.dfProjectId || !this.config.dfAgentId) {\n throw new Error('Dialogflow configuration is missing');\n }\n\n const dialogflowConfig: DialogflowBackendConfig = {\n dfProjectId: this.config.dfProjectId!,\n dfLocation: this.config.dfLocation || 'us-central1',\n dfAgentId: this.config.dfAgentId!,\n languageCode: this.config.languageCode || 'en',\n backendBaseUrl: this.config.backendBaseUrl || 'http://localhost:8012',\n };\n\n // Create session if needed\n if (!this.state.sessionId) {\n const session = await createDialogflowSession(dialogflowConfig);\n this.setState({ sessionId: session.session_id });\n }\n\n // Send message (correct parameter order: message, sessionId, config)\n if (this.config.debug) {\n console.log('Sending message to Dialogflow:', {\n message: text,\n sessionId: this.state.sessionId,\n hasConfig: !!dialogflowConfig,\n });\n }\n \n const response = await sendDialogflowMessage(\n text,\n this.state.sessionId!,\n dialogflowConfig\n );\n\n if (this.config.debug) {\n console.log('Dialogflow response:', {\n response: response.response,\n hasRichContent: !!response.richContent,\n richContent: response.richContent,\n });\n }\n\n // Check for handoff\n if (response.handoff === true) {\n // Add bot response first\n const botMessage: ChatMessage = {\n id: `bot-${Date.now()}`,\n text: response.response || this.config.fallbackWelcomeMessage || 'No response',\n sender: 'bot',\n timestamp: new Date(),\n richContent: response.richContent,\n };\n this.setState({\n messages: [...this.state.messages, botMessage],\n isLoading: false,\n });\n \n // Start collecting user info for handoff\n this.startUserInfoCollection();\n return;\n }\n \n const botMessage: ChatMessage = {\n id: `bot-${Date.now()}`,\n text: response.response || this.config.fallbackWelcomeMessage || 'No response',\n sender: 'bot',\n timestamp: new Date(),\n richContent: response.richContent,\n };\n\n this.setState({\n messages: [...this.state.messages, botMessage],\n isLoading: false,\n });\n }\n\n /**\n * Send message to human support\n */\n private async sendHumanMessage(text: string): Promise<void> {\n if (!this.chatId || !this.supportSessionId) {\n const errorMessage: ChatMessage = {\n id: `error-${Date.now()}`,\n text: 'Chat session not initialized. Please try again.',\n sender: 'bot',\n timestamp: new Date(),\n };\n this.setState({\n messages: [...this.state.messages, errorMessage],\n isLoading: false,\n });\n return;\n }\n\n if (!this.chatService) {\n throw new Error('Chat service not initialized');\n }\n\n // Send typing_stop before sending message\n this.chatService.sendTypingIndicator('typing_stop');\n if (this.typingTimeout) {\n clearTimeout(this.typingTimeout);\n this.typingTimeout = null;\n }\n\n try {\n // Try WebSocket first, fallback to REST API\n const sentViaWs = this.chatService.sendMessageViaWebSocket(text.trim());\n \n if (!sentViaWs) {\n // Fallback to REST API\n await this.chatService.sendMessageToAgent(this.chatId, this.supportSessionId, text.trim());\n }\n \n this.setState({ isLoading: false });\n } catch (error: any) {\n // chat_resolved is a normal terminal condition, not an error\n if (error instanceof ChatResolvedError || error?.name === 'ChatResolvedError' || error?.message === 'chat_resolved') {\n this.enterResolvedState(this.chatId);\n return;\n }\n\n // Handle 401/404 - clear chat_id and reinitialize\n if (error.message?.includes('Chat not found') ||\n error.message?.includes('unauthorized') ||\n error.message?.includes('401') ||\n error.message?.includes('404')) {\n if (this.config.debug) {\n console.log('⚠️ Chat expired. Re-initializing...');\n }\n \n // Clear chat_id\n this.chatId = null;\n this.supportSessionId = null;\n if (typeof window !== 'undefined' && window.localStorage) {\n localStorage.removeItem('blockspark_chat_id');\n localStorage.removeItem('blockspark_session_id');\n }\n \n // Try to reinitialize chat\n try {\n const newSession = await this.chatService.startSupportChat(\n this.state.sessionId || null,\n null,\n null,\n null\n );\n this.chatId = newSession.chat_id;\n this.supportSessionId = newSession.session_id;\n if (typeof window !== 'undefined' && window.localStorage) {\n localStorage.setItem('blockspark_chat_id', this.chatId);\n localStorage.setItem('blockspark_session_id', this.supportSessionId);\n }\n \n // Retry sending message\n if (this.chatId && this.supportSessionId) {\n await this.chatService.sendMessageToAgent(\n this.chatId,\n this.supportSessionId,\n text.trim()\n );\n }\n this.setState({ isLoading: false });\n return;\n } catch (retryError: any) {\n if (retryError instanceof ChatResolvedError || retryError?.message === 'chat_resolved') {\n this.enterResolvedState(this.chatId);\n return;\n }\n throw retryError;\n }\n } else {\n throw error;\n }\n }\n }\n\n /**\n * Switch to human mode\n */\n switchToHumanMode(): void {\n this.chatMode = 'human';\n this.setState({ chatMode: 'human' });\n if (typeof window !== 'undefined' && window.localStorage) {\n localStorage.setItem('blockspark_chat_mode', 'HUMAN');\n }\n }\n\n /**\n * Switch to AI mode\n */\n switchToBotMode(): void {\n this.chatMode = 'ai';\n this.setState({ chatMode: 'ai' });\n if (typeof window !== 'undefined' && window.localStorage) {\n localStorage.setItem('blockspark_chat_mode', 'BOT');\n }\n }\n\n /**\n * Initialize welcome popup\n */\n initializeWelcomePopup(): void {\n if (this.config.showWelcomePopup !== false) {\n const delay = this.config.welcomePopupDelay || 1500;\n setTimeout(() => {\n if (!this.state.isOpen) {\n this.setState({ showWelcomePopup: true });\n }\n }, delay);\n }\n }\n\n /**\n * Start user info collection for handoff\n */\n private startUserInfoCollection(): void {\n this.collectingUserInfo = true;\n this.userInfoStep = 'name';\n this.collectedUserName = '';\n this.collectedUserEmail = '';\n this.collectedUserMobile = '';\n \n // Ask for name\n const namePrompt: ChatMessage = {\n id: `prompt-${Date.now()}`,\n text: 'To connect you with a human agent, I\\'ll need some information. Please provide your name:',\n sender: 'bot',\n timestamp: new Date(),\n };\n this.setState({\n messages: [...this.state.messages, namePrompt],\n });\n }\n\n /**\n * Handle user info collection\n */\n private async handleUserInfoCollection(text: string): Promise<void> {\n if (this.userInfoStep === 'name') {\n // Save name and ask for email\n const name = text.trim();\n this.collectedUserName = name;\n this.userInfoStep = 'email';\n \n // Ask for email\n const emailPrompt: ChatMessage = {\n id: `prompt-${Date.now()}`,\n text: 'Thank you! Now please provide your email address:',\n sender: 'bot',\n timestamp: new Date(),\n };\n this.setState({\n messages: [...this.state.messages, emailPrompt],\n inputValue: '',\n });\n return;\n } else if (this.userInfoStep === 'email') {\n // Validate email format\n const email = text.trim();\n const emailRegex = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/;\n \n if (!emailRegex.test(email)) {\n // Invalid email, ask again\n const invalidEmailMessage: ChatMessage = {\n id: `prompt-${Date.now()}`,\n text: 'Please provide a valid email address:',\n sender: 'bot',\n timestamp: new Date(),\n };\n this.setState({\n messages: [...this.state.messages, invalidEmailMessage],\n inputValue: '',\n });\n return;\n }\n \n // Save email and ask for mobile\n this.collectedUserEmail = email;\n this.userInfoStep = 'mobile';\n \n // Ask for mobile\n const mobilePrompt: ChatMessage = {\n id: `prompt-${Date.now()}`,\n text: 'Thank you! Now please provide your mobile number:',\n sender: 'bot',\n timestamp: new Date(),\n };\n this.setState({\n messages: [...this.state.messages, mobilePrompt],\n inputValue: '',\n });\n return;\n } else if (this.userInfoStep === 'mobile') {\n // Validate mobile format\n const mobile = text.trim();\n const mobileRegex = /^[\\+]?[(]?[0-9]{1,4}[)]?[-\\s\\.]?[(]?[0-9]{1,4}[)]?[-\\s\\.]?[0-9]{1,9}$/;\n \n if (!mobileRegex.test(mobile) || mobile.length < 10) {\n // Invalid mobile, ask again\n const invalidMobileMessage: ChatMessage = {\n id: `prompt-${Date.now()}`,\n text: 'Please provide a valid mobile number (e.g., +1234567890):',\n sender: 'bot',\n timestamp: new Date(),\n };\n this.setState({\n messages: [...this.state.messages, invalidMobileMessage],\n inputValue: '',\n });\n return;\n }\n \n // Save mobile and proceed with handoff\n this.collectedUserMobile = mobile;\n \n // Reset collection state\n this.collectingUserInfo = false;\n this.userInfoStep = null;\n \n // Proceed with handoff using collected info\n await this.handleHandoff(this.collectedUserName, this.collectedUserEmail, mobile);\n this.setState({ inputValue: '' });\n }\n }\n\n /**\n * Handle handoff from Dialogflow to human support\n */\n async handleHandoff(customerName?: string, customerEmail?: string, customerMobile?: string): Promise<void> {\n if (!this.chatService) {\n throw new Error('Chat service not initialized');\n }\n\n try {\n this.isConnectingToAgent = true;\n \n // Get Dialogflow session ID if available\n const dialogflowSessionId = this.state.sessionId;\n \n // STEP 1: Ensure chat is initialized\n const session = await this.chatService.ensureChatInitialized(\n this.chatId,\n this.supportSessionId,\n dialogflowSessionId || null,\n customerName || null,\n customerEmail || null,\n customerMobile || null\n );\n \n if (!session || !session.chat_id) {\n throw new Error('Failed to initialize chat session');\n }\n \n const currentChatId = session.chat_id;\n const currentSupportSessionId = session.session_id;\n \n // Update state if chat was newly initialized\n if (currentChatId !== this.chatId) {\n this.chatId = currentChatId;\n this.supportSessionId = currentSupportSessionId;\n if (typeof window !== 'undefined' && window.localStorage) {\n localStorage.setItem('blockspark_chat_id', this.chatId);\n localStorage.setItem('blockspark_session_id', this.supportSessionId);\n }\n if (this.config.debug) {\n console.log('✅ Chat initialized:', { chatId: currentChatId, sessionId: currentSupportSessionId });\n }\n }\n\n // STEP 2: Request handoff\n try {\n await this.chatService.requestHandoff(\n currentChatId,\n currentSupportSessionId,\n 'Customer requested human agent',\n dialogflowSessionId || null,\n customerName || null,\n customerEmail || null,\n customerMobile || null\n );\n \n if (this.config.debug) {\n console.log('✅ Handoff requested successfully');\n }\n } catch (handoffError: any) {\n // Handle 401/404 or \"chat not found\" - clear chat_id and retry\n if (handoffError.message?.includes('Invalid chat_id') || \n handoffError.message?.includes('Chat not found') ||\n handoffError.message?.includes('unauthorized') ||\n handoffError.message?.includes('400') ||\n handoffError.message?.includes('401') ||\n handoffError.message?.includes('404') ||\n handoffError.message?.includes('expired')) {\n if (this.config.debug) {\n console.log('⚠️ Chat expired or not found. Re-initializing chat...');\n }\n \n // Clear old chat_id\n this.chatId = null;\n this.supportSessionId = null;\n if (typeof window !== 'undefined' && window.localStorage) {\n localStorage.removeItem('blockspark_chat_id');\n localStorage.removeItem('blockspark_session_id');\n }\n \n // Create new chat session\n const newSession = await this.chatService.startSupportChat(\n dialogflowSessionId || null,\n customerName || null,\n customerEmail || null,\n customerMobile || null\n );\n \n if (!newSession || !newSession.chat_id) {\n throw new Error('Failed to re-initialize chat session');\n }\n \n this.chatId = newSession.chat_id;\n this.supportSessionId = newSession.session_id;\n if (typeof window !== 'undefined' && window.localStorage) {\n localStorage.setItem('blockspark_chat_id', this.chatId);\n localStorage.setItem('blockspark_session_id', this.supportSessionId);\n }\n \n // Retry handoff with new chat_id\n await this.chatService.requestHandoff(\n this.chatId,\n this.supportSessionId,\n 'Customer requested human agent',\n dialogflowSessionId || null,\n customerName || null,\n customerEmail || null,\n customerMobile || null\n );\n \n if (this.config.debug) {\n console.log('✅ Handoff requested successfully after retry');\n }\n } else {\n throw handoffError;\n }\n }\n\n // Switch to human mode\n this.switchToHumanMode();\n this.chatResolved = false;\n this.agentAccepted = false;\n\n // Add connecting message\n const connectingMessage: ChatMessage = {\n id: `connecting-${Date.now()}`,\n text: 'Connecting you to a human agent...',\n sender: 'bot',\n timestamp: new Date(),\n };\n this.setState({\n messages: [...this.state.messages, connectingMessage],\n });\n\n // Connect WebSocket with handlers\n if (currentChatId && currentSupportSessionId) {\n this.chatService.connectWebSocket(\n currentChatId,\n currentSupportSessionId,\n (message: WebSocketMessage) => {\n this.handleWebSocketMessage(message);\n },\n (connected: boolean) => {\n this.wsConnected = connected;\n }\n );\n }\n \n this.isConnectingToAgent = false;\n } catch (error: any) {\n console.error('Error handling handoff:', error);\n const errorMessage: ChatMessage = {\n id: `error-${Date.now()}`,\n text: this.config.debug\n ? `Handoff error: ${error.message}`\n : 'Failed to connect to agent. Please try again.',\n sender: 'bot',\n timestamp: new Date(),\n };\n this.setState({\n messages: [...this.state.messages, errorMessage],\n });\n this.isConnectingToAgent = false;\n }\n }\n\n /**\n * Handle WebSocket messages\n */\n private handleWebSocketMessage(message: WebSocketMessage): void {\n switch (message.type) {\n case 'message':\n if (message.content) {\n // Only display messages from agent\n if (message.sender_type === 'agent' || !message.sender_type) {\n const agentMessage: ChatMessage = {\n id: message.id || `agent-${Date.now()}`,\n text: message.content,\n sender: 'agent',\n timestamp: new Date(message.timestamp || Date.now()),\n };\n \n // Avoid duplicate messages\n const existingIds = new Set(this.state.messages.map(m => m.id));\n if (!existingIds.has(agentMessage.id)) {\n this.setState({\n messages: [...this.state.messages, agentMessage],\n });\n }\n \n // Hide typing indicator when message received\n this.agentTyping = false;\n if (this.agentTypingTimeout) {\n clearTimeout(this.agentTypingTimeout);\n this.agentTypingTimeout = null;\n }\n }\n }\n break;\n\n case 'typing_start':\n if (message.sender_type === 'agent') {\n this.agentTyping = true;\n // Auto-hide after 3 seconds if no message received\n if (this.agentTypingTimeout) {\n clearTimeout(this.agentTypingTimeout);\n }\n this.agentTypingTimeout = setTimeout(() => {\n this.agentTyping = false;\n }, 3000);\n }\n break;\n\n case 'typing_stop':\n if (message.sender_type === 'agent') {\n this.agentTyping = false;\n if (this.agentTypingTimeout) {\n clearTimeout(this.agentTypingTimeout);\n this.agentTypingTimeout = null;\n }\n }\n break;\n\n case 'agent_changed':\n if (message.to_agent) {\n this.currentAgent = {\n id: message.to_agent_id,\n name: message.to_agent,\n };\n\n // Add system message to chat\n const systemMessage: ChatMessage = {\n id: `system-${Date.now()}`,\n text: message.from_agent\n ? `Chat has been transferred from ${message.from_agent} to ${message.to_agent}`\n : `Chat has been transferred to ${message.to_agent}`,\n sender: 'bot',\n timestamp: new Date(),\n };\n this.setState({\n messages: [...this.state.messages, systemMessage],\n });\n }\n break;\n\n case 'agent_accepted':\n this.agentAccepted = true;\n this.isConnectingToAgent = false;\n const acceptedMessage: ChatMessage = {\n id: message.id || `agent-accepted-${Date.now()}`,\n text: 'You can chat now, the agent has accepted your request.',\n sender: 'bot',\n timestamp: message.timestamp ? new Date(message.timestamp) : new Date(),\n };\n const existingIds = new Set(this.state.messages.map(m => m.id));\n if (!existingIds.has(acceptedMessage.id)) {\n this.setState({\n messages: [...this.state.messages, acceptedMessage],\n });\n }\n if (message.to_agent) {\n this.currentAgent = {\n name: message.to_agent,\n id: message.to_agent_id,\n };\n }\n break;\n\n case 'chat_resolved':\n case 'chat_ended':\n this.enterResolvedState(message.chat_id || null);\n break;\n\n case 'chat_info':\n if (message.status === 'active') {\n this.isConnectingToAgent = false;\n this.agentAccepted = true;\n } else if (message.status === 'resolved' || message.status === 'ended') {\n this.enterResolvedState(message.chat_id || null);\n }\n if (message.agent_id) {\n this.currentAgent = {\n ...this.currentAgent,\n id: message.agent_id,\n };\n }\n break;\n\n case 'error':\n console.error('WebSocket error:', message.error);\n const errorMessage: ChatMessage = {\n id: `error-${Date.now()}`,\n text: message.error || 'An error occurred. Please try again.',\n sender: 'bot',\n timestamp: new Date(),\n };\n this.setState({\n messages: [...this.state.messages, errorMessage],\n });\n break;\n }\n }\n\n /**\n * Enter resolved state\n */\n private enterResolvedState(resolvedChatId?: string | null): void {\n this.chatResolved = true;\n this.isConnectingToAgent = false;\n this.agentAccepted = false;\n this.agentTyping = false;\n if (this.agentTypingTimeout) {\n clearTimeout(this.agentTypingTimeout);\n this.agentTypingTimeout = null;\n }\n\n // Stop WS + prevent any reuse of the old chat_id\n if (this.chatService) {\n this.chatService.disconnectWebSocket();\n }\n this.chatId = null;\n this.supportSessionId = null;\n if (typeof window !== 'undefined' && window.localStorage) {\n localStorage.removeItem('blockspark_chat_id');\n localStorage.removeItem('blockspark_session_id');\n }\n \n // Add thank you message before reset\n const thankYouMessage: ChatMessage = {\n id: `resolved-${Date.now()}`,\n text: 'Thank you for contacting us!',\n sender: 'bot',\n timestamp: new Date(),\n };\n this.setState({\n messages: [...this.state.messages, thankYouMessage],\n });\n \n // Automatically reset to BOT mode after showing thank you message\n setTimeout(async () => {\n this.switchToBotMode();\n this.chatResolved = false;\n this.setState({\n messages: [],\n sessionId: null, // Clear session ID to force recreation\n });\n this.collectingUserInfo = false;\n this.userInfoStep = null;\n this.collectedUserName = '';\n this.collectedUserEmail = '';\n this.collectedUserMobile = '';\n \n // Recreate Dialogflow session to start fresh\n if (this.config.dfProjectId && this.config.dfAgentId && this.state.isOpen) {\n try {\n this.setState({ isLoading: true });\n const dialogflowConfig: DialogflowBackendConfig = {\n dfProjectId: this.config.dfProjectId,\n dfLocation: this.config.dfLocation || 'us-central1',\n dfAgentId: this.config.dfAgentId,\n languageCode: this.config.languageCode || 'en',\n backendBaseUrl: this.config.backendBaseUrl || 'http://localhost:8012',\n };\n \n const session = await createDialogflowSession(dialogflowConfig);\n this.setState({ \n sessionId: session.session_id,\n isLoading: false,\n });\n \n // Add welcome message from Dialogflow if available\n if (session.message) {\n const welcomeMessage: ChatMessage = {\n id: `welcome-${Date.now()}`,\n text: session.message,\n sender: 'bot',\n timestamp: new Date(),\n richContent: session.richContent,\n };\n this.setState({\n messages: [welcomeMessage],\n });\n }\n } catch (error: any) {\n console.error('Error recreating Dialogflow session after handoff:', error);\n this.setState({ \n isLoading: false,\n error: error.message || 'Failed to initialize chat',\n });\n \n // Show fallback welcome message\n const fallbackMessage: ChatMessage = {\n id: `fallback-${Date.now()}`,\n text: this.config.fallbackWelcomeMessage || 'Hello! How can I help you today?',\n sender: 'bot',\n timestamp: new Date(),\n };\n this.setState({\n messages: [fallbackMessage],\n });\n }\n }\n }, 2000);\n }\n\n /**\n * Load message history\n */\n async loadMessageHistory(preserveExisting: boolean = false): Promise<void> {\n if (!this.chatService || !this.chatId || !this.supportSessionId) {\n return;\n }\n\n try {\n if (this.chatId && this.supportSessionId) {\n const history = await this.chatService.loadMessageHistory(this.chatId, this.supportSessionId);\n \n const historyMessages: ChatMessage[] = history.map((msg: any) => ({\n id: msg.id || `history-${Date.now()}-${Math.random()}`,\n text: msg.content,\n sender: msg.sender_type === 'agent' ? 'agent' : 'user',\n timestamp: new Date(msg.timestamp),\n }));\n\n if (preserveExisting) {\n // Merge with existing messages, avoiding duplicates\n const existingIds = new Set(this.state.messages.map(m => m.id));\n const newMessages = historyMessages.filter(m => !existingIds.has(m.id));\n \n // Combine existing messages with new history messages\n // Sort by timestamp to maintain chronological order\n const combined = [...this.state.messages, ...newMessages].sort((a, b) => \n a.timestamp.getTime() - b.timestamp.getTime()\n );\n \n this.setState({\n messages: combined,\n });\n } else {\n // Replace all messages\n this.setState({\n messages: historyMessages,\n });\n }\n }\n } catch (error: any) {\n console.error('Error loading message history:', error);\n if (this.config.debug) {\n const errorMessage: ChatMessage = {\n id: `error-${Date.now()}`,\n text: `Failed to load chat history: ${error.message}`,\n sender: 'bot',\n timestamp: new Date(),\n };\n this.setState({\n messages: [...this.state.messages, errorMessage],\n });\n }\n }\n }\n\n /**\n * Get additional state for UI (not in WidgetState but needed by components)\n */\n getAdditionalState() {\n return {\n wsConnected: this.wsConnected,\n agentTyping: this.agentTyping,\n currentAgent: this.currentAgent,\n isConnectingToAgent: this.isConnectingToAgent,\n agentAccepted: this.agentAccepted,\n chatResolved: this.chatResolved,\n };\n }\n\n /**\n * Cleanup\n */\n destroy(): void {\n if (this.typingTimeout) {\n clearTimeout(this.typingTimeout);\n }\n if (this.agentTypingTimeout) {\n clearTimeout(this.agentTypingTimeout);\n }\n if (this.chatService) {\n this.chatService.disconnectWebSocket();\n }\n this.listeners.clear();\n }\n}\n","/**\n * useChatWidget Composable\n * Headless mode for Vue 3 - use logic without UI\n */\n\nimport { ref, computed, onUnmounted, watch, readonly, type Ref } from 'vue';\nimport { WidgetStateManager } from '../core/stateManager';\nimport type { WidgetConfig, WidgetState, ChatMessage } from '../core/types';\n\nexport interface UseChatWidgetReturn {\n // State\n state: Readonly<Ref<WidgetState>>;\n isOpen: Readonly<Ref<boolean>>;\n messages: Readonly<Ref<ChatMessage[]>>;\n isLoading: Readonly<Ref<boolean>>;\n error: Readonly<Ref<string | null>>;\n chatMode: Readonly<Ref<'ai' | 'human'>>;\n\n // Additional state\n wsConnected: Readonly<Ref<boolean>>;\n agentTyping: Readonly<Ref<boolean>>;\n currentAgent: Readonly<Ref<{ name: string; id?: string }>>;\n isConnectingToAgent: Readonly<Ref<boolean>>;\n\n // Actions\n openChat: () => Promise<void>;\n closeChat: () => void;\n sendMessage: (text: string) => Promise<void>;\n toggleChat: () => Promise<void>;\n setInputValue: (value: string) => void;\n clearError: () => void;\n\n // Manager instance (for advanced usage)\n manager: WidgetStateManager;\n}\n\n/**\n * Headless chat widget composable\n * Use this for custom UI implementations\n */\nexport function useChatWidget(config: WidgetConfig): UseChatWidgetReturn {\n // Create state manager\n const manager = new WidgetStateManager(config);\n\n // Reactive state\n const state = ref<WidgetState>(manager.getState());\n const wsConnected = ref(false);\n const agentTyping = ref(false);\n const currentAgent = ref<{ name: string; id?: string }>({ name: 'Agent' });\n const isConnectingToAgent = ref(false);\n\n // Subscribe to state changes\n const unsubscribe = manager.subscribe((newState) => {\n state.value = { ...newState };\n \n // Update additional state\n const additional = manager.getAdditionalState();\n wsConnected.value = additional.wsConnected;\n agentTyping.value = additional.agentTyping;\n currentAgent.value = additional.currentAgent;\n isConnectingToAgent.value = additional.isConnectingToAgent;\n });\n\n // Computed properties\n const isOpen = computed(() => state.value.isOpen);\n const messages = computed(() => state.value.messages);\n const isLoading = computed(() => state.value.isLoading);\n const error = computed(() => state.value.error);\n const chatMode = computed(() => state.value.chatMode);\n\n // Actions\n const openChat = async () => {\n await manager.openChat();\n };\n\n const closeChat = () => {\n manager.closeChat();\n };\n\n const sendMessage = async (text: string) => {\n await manager.sendMessage(text);\n };\n\n const toggleChat = async () => {\n await manager.toggleChat();\n };\n\n const setInputValue = (value: string) => {\n manager.setInputValue(value);\n };\n\n const clearError = () => {\n manager.clearError();\n };\n\n // Cleanup on unmount\n onUnmounted(() => {\n unsubscribe();\n manager.destroy();\n });\n\n // Watch config changes\n watch(\n () => config,\n (newConfig) => {\n manager.updateConfig(newConfig);\n },\n { deep: true }\n );\n\n return {\n state: state as Readonly<Ref<WidgetState>>,\n isOpen: isOpen as Readonly<Ref<boolean>>,\n messages: messages as Readonly<Ref<ChatMessage[]>>,\n isLoading: isLoading as Readonly<Ref<boolean>>,\n error: error as Readonly<Ref<string | null>>,\n chatMode: chatMode as Readonly<Ref<'ai' | 'human'>>,\n wsConnected: wsConnected as Readonly<Ref<boolean>>,\n agentTyping: agentTyping as Readonly<Ref<boolean>>,\n currentAgent: currentAgent as Readonly<Ref<{ name: string; id?: string }>>,\n isConnectingToAgent: isConnectingToAgent as Readonly<Ref<boolean>>,\n openChat,\n closeChat,\n sendMessage,\n toggleChat,\n setInputValue,\n clearError,\n manager,\n };\n}\n","<template>\n <div class=\"custom-chat-widget\" v-bind=\"$attrs\">\n <!-- Welcome Popup -->\n <div\n v-if=\"state.showWelcomePopup && !state.isOpen\"\n class=\"custom-welcome-popup\"\n @click=\"handleOpenChat\"\n >\n <div class=\"custom-welcome-header\">\n <div class=\"custom-welcome-title\">{{ config.welcomeTitle }}</div>\n <button\n class=\"custom-close-popup\"\n @click.stop=\"handleCloseWelcomePopup\"\n aria-label=\"Close welcome popup\"\n >\n ×\n </button>\n </div>\n <div class=\"custom-welcome-message\">{{ config.welcomeMessage }}</div>\n <div class=\"custom-welcome-cta\">{{ config.welcomeCta }}</div>\n </div>\n\n <!-- Chat Toggle Button -->\n <button\n v-if=\"!state.isOpen\"\n class=\"custom-chat-toggle-btn\"\n @click=\"handleOpenChat\"\n aria-label=\"Open chat\"\n >\n <svg\n width=\"24\"\n height=\"24\"\n viewBox=\"0 0 24 24\"\n fill=\"none\"\n stroke=\"currentColor\"\n stroke-width=\"2\"\n stroke-linecap=\"round\"\n stroke-linejoin=\"round\"\n >\n <path d=\"M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z\"></path>\n </svg>\n </button>\n\n <!-- Chat Window -->\n <div v-if=\"state.isOpen\" class=\"custom-chat-window\">\n <div class=\"custom-chat-header\">\n <div class=\"custom-chat-header-content\">\n <div class=\"custom-chat-title\">{{ config.title }}</div>\n <div class=\"custom-chat-subtitle\">\n {{ config.subtitle }}\n <span v-if=\"state.chatMode === 'human'\" class=\"custom-mode-indicator\">\n • {{ wsConnected ? '🟢 Connected' : '🟡 Connecting...' }}\n </span>\n </div>\n <div v-if=\"state.chatMode === 'human'\" class=\"custom-mode-badge\">\n Human Support Mode\n </div>\n <div v-if=\"state.chatMode === 'human'\" class=\"custom-agent-info\">\n <span class=\"custom-agent-label\">Agent:</span>\n <span class=\"custom-agent-name\">{{ currentAgent.name }}</span>\n </div>\n <div v-if=\"state.chatMode === 'ai'\" class=\"custom-mode-badge\">\n Bot Mode\n </div>\n </div>\n <button\n class=\"custom-chat-close-btn\"\n @click=\"handleCloseChat\"\n aria-label=\"Close chat\"\n >\n ×\n </button>\n </div>\n\n <div class=\"custom-chat-messages\" ref=\"messagesContainer\">\n <!-- Empty State -->\n <div v-if=\"isInitializing && state.messages.length === 0\" class=\"custom-chat-empty\">\n <div class=\"custom-typing-indicator\">\n <span></span>\n <span></span>\n <span></span>\n </div>\n <p>Initializing chat...</p>\n </div>\n <div v-else-if=\"!isInitializing && state.messages.length === 0\" class=\"custom-chat-empty\">\n <div class=\"custom-chat-empty-icon\">👋</div>\n <p>{{ config.emptyStateMessage }}</p>\n </div>\n\n <!-- Messages -->\n <div\n v-for=\"message in state.messages\"\n :key=\"message.id\"\n :class=\"[\n 'custom-message', \n `custom-message-${message.sender}`,\n { 'custom-handoff-message': isHandoffMessage(message.text) }\n ]\"\n >\n <div \n class=\"custom-message-content\"\n :class=\"{ 'custom-handoff-content': isHandoffMessage(message.text) }\"\n v-html=\"safeLinkifyText(message.text).replace(/\\n/g, '<br>')\"\n ></div>\n \n <!-- Rich Content (Chips) -->\n <div\n v-if=\"message.richContent && Array.isArray(message.richContent) && message.richContent.length > 0\"\n class=\"custom-chips-container\"\n >\n <template v-for=\"(contentGroup, groupIndex) in message.richContent\" :key=\"groupIndex\">\n <template v-if=\"!Array.isArray(contentGroup)\">\n <div\n v-if=\"contentGroup.type === 'chips' && contentGroup.options\"\n class=\"custom-chips-group\"\n >\n <button\n v-for=\"(chip, chipIndex) in contentGroup.options\"\n :key=\"chipIndex\"\n class=\"custom-chip-button\"\n type=\"button\"\n @click=\"handleChipClick(chip.text, chip.payload)\"\n >\n {{ chip.text }}\n </button>\n </div>\n </template>\n <template v-else>\n <template\n v-for=\"(content, contentIndex) in contentGroup\"\n :key=\"`${groupIndex}-${contentIndex}`\"\n >\n <div\n v-if=\"content.type === 'chips' && content.options\"\n class=\"custom-chips-group\"\n >\n <button\n v-for=\"(chip, chipIndex) in content.options\"\n :key=\"chipIndex\"\n class=\"custom-chip-button\"\n type=\"button\"\n @click=\"handleChipClick(chip.text, chip.payload)\"\n >\n {{ chip.text }}\n </button>\n </div>\n </template>\n </template>\n </template>\n </div>\n\n <div class=\"custom-message-time\">\n {{ formatTime(message.timestamp) }}\n </div>\n </div>\n\n <!-- Loading Indicator -->\n <div v-if=\"state.isLoading\" class=\"custom-message custom-message-bot\">\n <div class=\"custom-typing-indicator\">\n <span></span>\n <span></span>\n <span></span>\n </div>\n </div>\n\n <!-- Connecting to Agent Indicator -->\n <div v-if=\"isConnectingToAgent\" class=\"custom-message custom-message-bot\">\n <div class=\"custom-typing-indicator\">\n <span></span>\n <span></span>\n <span></span>\n </div>\n <div class=\"custom-message-content\">\n Connecting to agent...\n </div>\n </div>\n\n <!-- Agent Typing Indicator -->\n <div v-if=\"state.chatMode === 'human' && agentTyping\" class=\"custom-agent-typing-indicator\">\n <span class=\"custom-typing-dots\">\n <span></span>\n <span></span>\n <span></span>\n </span>\n <span class=\"custom-typing-text\">Agent is typing...</span>\n </div>\n\n <div ref=\"messagesEndRef\"></div>\n </div>\n\n <!-- Input Form -->\n <form class=\"custom-chat-input-form\" @submit.prevent=\"handleSubmit\">\n <input\n type=\"text\"\n class=\"custom-chat-input\"\n :value=\"state.inputValue\"\n @input=\"handleInput\"\n :placeholder=\"config.inputPlaceholder\"\n :disabled=\"state.isLoading || isInitializing || isStartingNewChat\"\n />\n <button\n type=\"submit\"\n class=\"custom-chat-send-btn\"\n :disabled=\"!state.inputValue.trim() || state.isLoading || isInitializing || isStartingNewChat\"\n >\n <svg\n width=\"20\"\n height=\"20\"\n viewBox=\"0 0 24 24\"\n fill=\"none\"\n stroke=\"currentColor\"\n stroke-width=\"2\"\n stroke-linecap=\"round\"\n stroke-linejoin=\"round\"\n >\n <line x1=\"22\" y1=\"2\" x2=\"11\" y2=\"13\"></line>\n <polygon points=\"22 2 15 22 11 13 2 9 22 2\"></polygon>\n </svg>\n </button>\n </form>\n </div>\n </div>\n</template>\n\n<script setup lang=\"ts\">\nimport { ref, computed, onMounted, nextTick, watch } from 'vue';\nimport { useChatWidget } from '../composables/useChatWidget';\nimport { safeLinkifyText } from '../utils/sanitize';\nimport type { WidgetConfig } from '../core/types';\n\n// Handle attributes properly to avoid Vue warnings\n// For Vue 3.3+, defineOptions is available as a compiler macro\n// @ts-ignore - defineOptions is a compiler macro\ndefineOptions({\n inheritAttrs: false, // We handle attrs manually via v-bind=\"$attrs\"\n});\n\n// Props\nconst props = withDefaults(defineProps<WidgetConfig>(), {\n title: '💬 BlockSpark AI Assistant',\n subtitle: \"We're here to help\",\n welcomeTitle: '👋 Welcome to BlockSpark',\n welcomeMessage: \"My name is BlockSpark AI Assistant and I'll guide you.\",\n welcomeCta: '💬 Click here to start chatting!',\n showWelcomePopup: true,\n welcomePopupDelay: 1500,\n fallbackWelcomeMessage: \"Hello! I'm BlockSpark AI Assistant. How can I help you today?\",\n inputPlaceholder: 'Type your message...',\n emptyStateMessage: \"Hi! I'm BlockSpark AI Assistant. How can I help you today?\",\n debug: false,\n});\n\n// Use headless composable\nconst {\n state,\n isOpen,\n messages,\n isLoading,\n error,\n chatMode,\n wsConnected,\n agentTyping,\n currentAgent,\n isConnectingToAgent: isConnectingToAgentFromComposable,\n openChat,\n closeChat,\n sendMessage,\n toggleChat,\n setInputValue,\n clearError,\n manager,\n} = useChatWidget(props);\n\n// Refs\nconst messagesEndRef = ref<HTMLDivElement | null>(null);\nconst messagesContainer = ref<HTMLDivElement | null>(null);\n\n// Computed config (for reactivity)\nconst config = computed(() => props);\n\n// Computed state for initialization and connection (matching React component)\nconst isInitializing = computed(() => {\n return state.value.isLoading && state.value.messages.length === 0 && !state.value.sessionId;\n});\n\nconst isStartingNewChat = computed(() => {\n // This would be tracked in manager if needed, for now return false\n return false;\n});\n\n// Use isConnectingToAgent from composable (already computed in manager)\nconst isConnectingToAgent = isConnectingToAgentFromComposable;\n\n// Auto-scroll to bottom when messages change\nwatch(\n () => messages.value.length,\n () => {\n nextTick(() => {\n messagesEndRef.value?.scrollIntoView({ behavior: 'smooth' });\n });\n }\n);\n\n// Handlers\nconst handleOpenChat = async () => {\n // Ensure immediate UI response\n if (!state.value.isOpen) {\n state.value.isOpen = true;\n state.value.showWelcomePopup = false;\n }\n await openChat();\n};\n\nconst handleCloseChat = () => {\n closeChat();\n};\n\nconst handleCloseWelcomePopup = () => {\n manager.closeWelcomePopup();\n};\n\nconst handleInput = (event: Event) => {\n const target = event.target as HTMLInputElement;\n setInputValue(target.value);\n};\n\nconst handleSubmit = async (event: Event) => {\n event.preventDefault();\n if (state.value.inputValue.trim()) {\n await sendMessage(state.value.inputValue);\n }\n};\n\nconst handleChipClick = async (chipText: string, payload?: string) => {\n const messageToSend = chipText || payload || '';\n await sendMessage(messageToSend);\n};\n\n// Utility functions\nconst formatTime = (date: Date): string => {\n return date.toLocaleTimeString([], {\n hour: '2-digit',\n minute: '2-digit',\n });\n};\n\n// Check if message is a handoff message\nconst isHandoffMessage = (text: string): boolean => {\n return text.includes('👤') || \n text.includes('being connected') || \n text.includes('live support agent') ||\n text.includes('transfer your conversation');\n};\n\n// Initialize welcome popup on mount (SSR-safe)\nonMounted(() => {\n if (config.value.showWelcomePopup && typeof window !== 'undefined') {\n manager.initializeWelcomePopup();\n }\n});\n</script>\n\n<style scoped>\n/* Styles are imported separately by users */\n/* This ensures SSR compatibility */\n</style>\n"],"names":["WidgetStateManager","constructor","config","this","listeners","Set","chatMode","chatId","supportSessionId","chatService","collectingUserInfo","userInfoStep","collectedUserName","collectedUserEmail","collectedUserMobile","wsConnected","agentTyping","currentAgent","name","isConnectingToAgent","agentAccepted","chatResolved","historyLoaded","typingTimeout","agentTypingTimeout","state","isOpen","showWelcomePopup","messages","inputValue","isLoading","error","sessionId","createChatService","baseUrl","backendBaseUrl","wsUrl","backendWsUrl","debug","window","localStorage","savedMode","getItem","subscribe","listener","add","delete","getState","setState","updates","forEach","updateConfig","openChat","dfProjectId","dfAgentId","dialogflowConfig","dfLocation","languageCode","session","createDialogflowSession","session_id","message","welcomeMessage","id","Date","now","text","sender","timestamp","richContent","fallbackMessage","fallbackWelcomeMessage","closeChat","closeWelcomePopup","toggleChat","willBeOpen","setInputValue","value","clearError","sendMessage","skipUserMessage","trim","userMessage","handleUserInfoCollection","sendHumanMessage","sendAIMessage","ChatResolvedError","enterResolvedState","errorMessage","includes","Error","response","sendDialogflowMessage","handoff","botMessage","startUserInfoCollection","sendTypingIndicator","clearTimeout","sendMessageViaWebSocket","sendMessageToAgent","removeItem","newSession","startSupportChat","chat_id","setItem","retryError","switchToHumanMode","switchToBotMode","initializeWelcomePopup","delay","welcomePopupDelay","setTimeout","namePrompt","emailPrompt","email","test","invalidEmailMessage","mobilePrompt","mobile","length","invalidMobileMessage","handleHandoff","customerName","customerEmail","customerMobile","dialogflowSessionId","ensureChatInitialized","currentChatId","currentSupportSessionId","requestHandoff","handoffError","connectingMessage","connectWebSocket","handleWebSocketMessage","connected","type","content","sender_type","agentMessage","map","m","has","to_agent","to_agent_id","systemMessage","from_agent","acceptedMessage","status","agent_id","resolvedChatId","disconnectWebSocket","thankYouMessage","async","loadMessageHistory","preserveExisting","historyMessages","msg","Math","random","existingIds","newMessages","filter","combined","sort","a","b","getTime","getAdditionalState","destroy","clear","useChatWidget","manager","ref","unsubscribe","newState","additional","computed","onUnmounted","watch","newConfig","deep","props","__props","isConnectingToAgentFromComposable","messagesEndRef","messagesContainer","isInitializing","isStartingNewChat","nextTick","scrollIntoView","behavior","handleOpenChat","handleCloseChat","handleCloseWelcomePopup","handleInput","event","target","handleSubmit","preventDefault","handleChipClick","chipText","payload","messageToSend","isHandoffMessage","onMounted","_openBlock","_createElementBlock","_mergeProps","class","$attrs","_unref","onClick","_createElementVNode","_hoisted_1","_hoisted_2","_toDisplayString","welcomeTitle","_hoisted_3","_hoisted_4","welcomeCta","width","height","viewBox","fill","stroke","d","_hoisted_5","_hoisted_6","_hoisted_7","_hoisted_8","title","_hoisted_9","subtitle","_hoisted_10","_hoisted_11","_hoisted_12","_cache","_hoisted_13","_hoisted_14","_hoisted_15","_hoisted_16","emptyStateMessage","_Fragment","_renderList","key","_normalizeClass","innerHTML","safeLinkifyText","replace","Array","isArray","_hoisted_18","contentGroup","groupIndex","contentIndex","options","_hoisted_21","chip","chipIndex","$event","_hoisted_22","_hoisted_19","_hoisted_20","_hoisted_23","date","toLocaleTimeString","hour","minute","_hoisted_24","_hoisted_25","_hoisted_26","onSubmit","onInput","placeholder","inputPlaceholder","disabled","x1","y1","x2","y2","points"],"mappings":"yEAiBO,MAAMA,EA6BX,WAAAC,CAAYC,GAqBV,GA/CFC,KAAQC,cAAmDC,IAC3DF,KAAQG,SAA2B,KACnCH,KAAQI,OAAwB,KAChC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