-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle33.min.js
More file actions
289 lines (243 loc) Β· 8.65 KB
/
bundle33.min.js
File metadata and controls
289 lines (243 loc) Β· 8.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
(() => {
// Utility functions
function extractDomain(url) {
try {
const urlObject = new URL(url)
return urlObject.hostname.replace('www.', '')
} catch {
return url
}
}
function createMarkdownElement(text, role, onImageClick) {
const container = document.createElement('div')
const lines = text.split('\n')
const collectedLinks = []
// ... (rest of the markdown parsing logic, same as our current implementation)
// Note: Keeping the same markdown parsing logic we developed
return container
}
// Custom Web Component
class AutomatioChat extends HTMLElement {
constructor() {
super()
this.attachShadow({ mode: 'open' })
this.messages = []
this.isTyping = false
this.isDark = false
}
connectedCallback() {
const hostUrl = this.getAttribute('host_url')
const apiKey = this.getAttribute('api_key')
const flowId = this.getAttribute('flow_id')
const agentName = this.getAttribute('agent_name') || 'AI Assistant'
const agentDescription = this.getAttribute('agent_description') || 'Powered by Automatio'
this.createStyles()
this.createUI(agentName, agentDescription)
this.setupEventListeners()
// Initialize with a welcome message
this.addMessage({
id: '1',
role: 'assistant',
content: 'Hello! How can I help you today?',
timestamp: new Date()
})
}
createStyles() {
const style = document.createElement('style')
style.textContent = `
:host {
--chat-width: 400px;
--chat-border-radius: 8px;
--chat-bg: #ffffff;
--chat-dark-bg: #1f2937;
--chat-border: #e5e7eb;
--chat-dark-border: #374151;
}
.chat-container {
position: fixed;
bottom: 20px;
right: 20px;
width: var(--chat-width);
background: var(--chat-bg);
border-radius: var(--chat-border-radius);
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
font-family: system-ui, -apple-system, sans-serif;
border: 1px solid var(--chat-border);
overflow: hidden;
}
:host(.dark) .chat-container {
background: var(--chat-dark-bg);
border-color: var(--chat-dark-border);
}
/* ... rest of the styles from our current implementation ... */
`
this.shadowRoot.appendChild(style)
}
createUI(agentName, agentDescription) {
const container = document.createElement('div')
container.className = 'chat-container'
container.innerHTML = `
<div class="chat-header">
<div class="agent-info">
<div class="agent-avatar">
<img src="https://hebbkx1anhila5yf.public.blob.vercel-storage.com/879-cQrx9a2kw2d0VO4ZsOtv1rMMJpo912.png" alt="${agentName}">
</div>
<div class="agent-details">
<div class="agent-name">${agentName}</div>
<div class="agent-description">${agentDescription}</div>
</div>
</div>
<div class="chat-controls">
<button class="sound-toggle">π</button>
<button class="theme-toggle">π</button>
<button class="close-button">Γ</button>
</div>
</div>
<div class="messages-container"></div>
<form class="input-form">
<input type="text" class="message-input" placeholder="Type a message...">
<button type="submit" class="send-button">Send</button>
</form>
`
this.shadowRoot.appendChild(container)
}
setupEventListeners() {
const form = this.shadowRoot.querySelector('.input-form')
const input = this.shadowRoot.querySelector('.message-input')
const soundToggle = this.shadowRoot.querySelector('.sound-toggle')
const themeToggle = this.shadowRoot.querySelector('.theme-toggle')
const closeButton = this.shadowRoot.querySelector('.close-button')
form.addEventListener('submit', (e) => {
e.preventDefault()
this.handleSubmit(input.value)
input.value = ''
})
soundToggle.addEventListener('click', () => {
this.isMuted = !this.isMuted
soundToggle.textContent = this.isMuted ? 'π' : 'π'
})
themeToggle.addEventListener('click', () => {
this.isDark = !this.isDark
themeToggle.textContent = this.isDark ? 'π' : 'π'
this.toggleTheme()
})
closeButton.addEventListener('click', () => this.remove())
}
toggleTheme() {
if (this.isDark) {
this.classList.add('dark')
} else {
this.classList.remove('dark')
}
}
async handleSubmit(message) {
if (!message.trim()) return
const userMessage = {
id: Date.now().toString(),
role: 'user',
content: message,
timestamp: new Date()
}
this.addMessage(userMessage)
this.showTypingIndicator()
try {
const response = await this.sendMessage(message)
this.hideTypingIndicator()
const assistantMessage = {
id: (Date.now() + 1).toString(),
role: 'assistant',
content: response,
timestamp: new Date()
}
this.addMessage(assistantMessage)
} catch (error) {
console.error('Error sending message:', error)
this.hideTypingIndicator()
const errorMessage = {
id: (Date.now() + 1).toString(),
role: 'assistant',
content: 'I apologize, but I encountered an error processing your message. Please try again.',
timestamp: new Date()
}
this.addMessage(errorMessage)
}
}
async sendMessage(message) {
// This should be replaced with actual API call logic
await new Promise(resolve => setTimeout(resolve, 1000))
return `I received your message: "${message}"\n\nThis is a default response. Configure the API endpoint to get real responses.`
}
addMessage(message) {
const container = this.shadowRoot.querySelector('.messages-container')
const messageElement = document.createElement('div')
messageElement.className = `message-wrapper ${message.role}`
if (message.role === 'assistant') {
messageElement.innerHTML = `
<div class="avatar">
<img src="https://hebbkx1anhila5yf.public.blob.vercel-storage.com/879-cQrx9a2kw2d0VO4ZsOtv1rMMJpo912.png" alt="Assistant">
</div>
`
}
const contentElement = document.createElement('div')
contentElement.className = 'message'
const markdownElement = createMarkdownElement(
message.content,
message.role,
(src, alt) => this.showImageModal(src, alt)
)
contentElement.appendChild(markdownElement)
messageElement.appendChild(contentElement)
container.appendChild(messageElement)
container.scrollTop = container.scrollHeight
}
showTypingIndicator() {
if (this.isTyping) return
this.isTyping = true
const container = this.shadowRoot.querySelector('.messages-container')
const typingElement = document.createElement('div')
typingElement.className = 'message-wrapper assistant typing'
typingElement.innerHTML = `
<div class="avatar">
<img src="https://hebbkx1anhila5yf.public.blob.vercel-storage.com/879-cQrx9a2kw2d0VO4ZsOtv1rMMJpo912.png" alt="Assistant">
</div>
<div class="typing-indicator">
<span></span>
<span></span>
<span></span>
</div>
`
container.appendChild(typingElement)
container.scrollTop = container.scrollHeight
}
hideTypingIndicator() {
if (!this.isTyping) return
this.isTyping = false
const typingElement = this.shadowRoot.querySelector('.typing')
if (typingElement) {
typingElement.remove()
}
}
showImageModal(src, alt) {
const modal = document.createElement('div')
modal.className = 'image-modal'
modal.innerHTML = `
<div class="modal-content">
<img src="${src}" alt="${alt}">
<button class="close-modal">Γ</button>
</div>
`
modal.addEventListener('click', (e) => {
if (e.target === modal) {
modal.remove()
}
})
modal.querySelector('.close-modal').addEventListener('click', () => {
modal.remove()
})
this.shadowRoot.appendChild(modal)
}
}
// Register the web component
customElements.define('automatio-chat', AutomatioChat)
console.log('[Bundle] Automatio Chat Widget loaded and registered')
})()