@origonai/web-chat-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +423 -0
- package/dist/origon-chat-sdk.js +584 -0
- package/dist/origon-chat-sdk.js.map +1 -0
- package/package.json +51 -0
- package/src/call.js +622 -0
- package/src/chat.js +376 -0
- package/src/constants.js +10 -0
- package/src/http.js +122 -0
- package/src/index.js +58 -0
- package/src/utils.js +84 -0
package/README.md
ADDED
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
# @origonai/web-chat-sdk
|
|
2
|
+
|
|
3
|
+
A lightweight, UI-agnostic Chat and Voice SDK for integrating Origon/Samespace chat and real-time voice call capabilities into your web applications.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🗨️ **Real-time Chat** — Send and receive messages with streaming support
|
|
8
|
+
- 🎙️ **Voice Calls** — WebRTC-based audio calls with full control
|
|
9
|
+
- 🔄 **Live Agent Support** — Seamless handoff to human agents
|
|
10
|
+
- 📜 **Chat History** — Retrieve and resume previous sessions
|
|
11
|
+
- 🎯 **Framework Agnostic** — Works with React, Vue, Svelte, vanilla JS, or any framework
|
|
12
|
+
- 📦 **Zero UI Dependencies** — Bring your own UI components
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @origonai/web-chat-sdk
|
|
18
|
+
# or
|
|
19
|
+
yarn add @origonai/web-chat-sdk
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Quick Start
|
|
23
|
+
|
|
24
|
+
### Basic Chat Example
|
|
25
|
+
|
|
26
|
+
```javascript
|
|
27
|
+
import {
|
|
28
|
+
initialize,
|
|
29
|
+
startChat,
|
|
30
|
+
sendMessage,
|
|
31
|
+
disconnect,
|
|
32
|
+
setCallbacks
|
|
33
|
+
} from '@origonai/web-chat-sdk'
|
|
34
|
+
|
|
35
|
+
// 1. Set up callbacks for state updates
|
|
36
|
+
setCallbacks({
|
|
37
|
+
onMessagesUpdate: (messages) => {
|
|
38
|
+
console.log('Messages updated:', messages)
|
|
39
|
+
},
|
|
40
|
+
onTyping: (isTyping) => {
|
|
41
|
+
console.log('Agent is typing:', isTyping)
|
|
42
|
+
},
|
|
43
|
+
onSessionUpdate: (sessionId) => {
|
|
44
|
+
console.log('Session ID:', sessionId)
|
|
45
|
+
},
|
|
46
|
+
onLiveAgentMode: (isLiveAgent) => {
|
|
47
|
+
console.log('Live agent mode:', isLiveAgent)
|
|
48
|
+
}
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
// 2. Initialize with your endpoint
|
|
52
|
+
initialize({
|
|
53
|
+
endpoint: 'https://your-origon-endpoint.com/api/chat'
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
// 3. Start a chat session
|
|
57
|
+
const { sessionId, messages, configData } = await startChat()
|
|
58
|
+
|
|
59
|
+
// 4. Send messages
|
|
60
|
+
await sendMessage({ text: 'Hello!' })
|
|
61
|
+
|
|
62
|
+
// 5. Disconnect when done
|
|
63
|
+
disconnect()
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Voice Call Example
|
|
67
|
+
|
|
68
|
+
```javascript
|
|
69
|
+
import {
|
|
70
|
+
initialize,
|
|
71
|
+
startCall,
|
|
72
|
+
disconnectCall,
|
|
73
|
+
toggleMute,
|
|
74
|
+
setCallCallbacks
|
|
75
|
+
} from '@origonai/web-chat-sdk'
|
|
76
|
+
|
|
77
|
+
// 1. Set up call callbacks
|
|
78
|
+
setCallCallbacks({
|
|
79
|
+
onCallStatus: (status) => {
|
|
80
|
+
console.log('Call status:', status) // 'connecting' | 'connected' | 'disconnected' | 'error'
|
|
81
|
+
},
|
|
82
|
+
onCallError: (error) => {
|
|
83
|
+
console.error('Call error:', error)
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
// 2. Initialize (same as chat)
|
|
88
|
+
initialize({
|
|
89
|
+
endpoint: 'https://your-origon-endpoint.com/api/chat'
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
// 3. Start a voice call
|
|
93
|
+
await startCall()
|
|
94
|
+
|
|
95
|
+
// 4. Toggle mute
|
|
96
|
+
const isMuted = toggleMute()
|
|
97
|
+
|
|
98
|
+
// 5. End the call
|
|
99
|
+
disconnectCall()
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## API Reference
|
|
103
|
+
|
|
104
|
+
### Configuration & Authentication
|
|
105
|
+
|
|
106
|
+
#### `initialize(credentials)`
|
|
107
|
+
|
|
108
|
+
Initializes the SDK with your credentials.
|
|
109
|
+
|
|
110
|
+
```javascript
|
|
111
|
+
initialize({
|
|
112
|
+
endpoint: string, // Required: Your Origon API endpoint
|
|
113
|
+
token?: string, // Optional: JWT token for authenticated users
|
|
114
|
+
externalId?: string // Optional: Custom user identifier
|
|
115
|
+
})
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
#### `authenticate(credentials)`
|
|
119
|
+
|
|
120
|
+
Authenticates and retrieves configuration from the server.
|
|
121
|
+
|
|
122
|
+
```javascript
|
|
123
|
+
const config = await authenticate({
|
|
124
|
+
endpoint: 'https://your-origon-endpoint.com/api/chat'
|
|
125
|
+
})
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
#### `configure(credentials)`
|
|
129
|
+
|
|
130
|
+
Configures the API service with an endpoint (called automatically by `authenticate`).
|
|
131
|
+
|
|
132
|
+
```javascript
|
|
133
|
+
configure({ endpoint: 'https://your-endpoint.com/api/chat' })
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
### Chat Functions
|
|
139
|
+
|
|
140
|
+
#### `setCallbacks(callbacks)`
|
|
141
|
+
|
|
142
|
+
Sets callback functions for chat events.
|
|
143
|
+
|
|
144
|
+
```javascript
|
|
145
|
+
setCallbacks({
|
|
146
|
+
onMessagesUpdate: (messages: Message[]) => void,
|
|
147
|
+
onTyping: (isTyping: boolean) => void,
|
|
148
|
+
onLiveAgentMode: (isLiveAgent: boolean) => void,
|
|
149
|
+
onSessionUpdate: (sessionId: string) => void
|
|
150
|
+
})
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
#### `startChat(payload?)`
|
|
154
|
+
|
|
155
|
+
Starts a new chat session or resumes an existing one.
|
|
156
|
+
|
|
157
|
+
```javascript
|
|
158
|
+
const result = await startChat({
|
|
159
|
+
sessionId?: string // Optional: Resume an existing session
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
// Returns:
|
|
163
|
+
// {
|
|
164
|
+
// sessionId: string,
|
|
165
|
+
// messages: Message[],
|
|
166
|
+
// configData: object
|
|
167
|
+
// }
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
#### `sendMessage(message)`
|
|
171
|
+
|
|
172
|
+
Sends a message in the current chat session. Returns a Promise that resolves with the session ID when the bot response is complete.
|
|
173
|
+
|
|
174
|
+
```javascript
|
|
175
|
+
const sessionId = await sendMessage({
|
|
176
|
+
text: string, // Required: Message text
|
|
177
|
+
html?: string // Optional: HTML content
|
|
178
|
+
})
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
#### `disconnect()`
|
|
182
|
+
|
|
183
|
+
Disconnects from the current chat session and cleans up resources.
|
|
184
|
+
|
|
185
|
+
```javascript
|
|
186
|
+
disconnect()
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
#### `getHistory()`
|
|
190
|
+
|
|
191
|
+
Retrieves chat history for the current device/user.
|
|
192
|
+
|
|
193
|
+
```javascript
|
|
194
|
+
const { sessions } = await getHistory()
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
#### `getMessages(sessionId)`
|
|
198
|
+
|
|
199
|
+
Retrieves messages for a specific session.
|
|
200
|
+
|
|
201
|
+
```javascript
|
|
202
|
+
const { sessionHistory } = await getMessages('session-id')
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
---
|
|
206
|
+
|
|
207
|
+
### Call Functions
|
|
208
|
+
|
|
209
|
+
#### `setCallCallbacks(callbacks)`
|
|
210
|
+
|
|
211
|
+
Sets callback functions for call events.
|
|
212
|
+
|
|
213
|
+
```javascript
|
|
214
|
+
setCallCallbacks({
|
|
215
|
+
onCallStatus: (status: string) => void,
|
|
216
|
+
onCallError: (error: string | null) => void
|
|
217
|
+
})
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
#### `startCall(payload?)`
|
|
221
|
+
|
|
222
|
+
Initiates a WebRTC voice call.
|
|
223
|
+
|
|
224
|
+
```javascript
|
|
225
|
+
await startCall({
|
|
226
|
+
sessionId?: string // Optional: Associate with existing chat session
|
|
227
|
+
})
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
#### `disconnectCall()`
|
|
231
|
+
|
|
232
|
+
Ends the current voice call and cleans up resources.
|
|
233
|
+
|
|
234
|
+
```javascript
|
|
235
|
+
disconnectCall()
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
#### `toggleMute()`
|
|
239
|
+
|
|
240
|
+
Toggles microphone mute state.
|
|
241
|
+
|
|
242
|
+
```javascript
|
|
243
|
+
const isMuted = toggleMute() // Returns: boolean
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
#### `getLocalStream()`
|
|
247
|
+
|
|
248
|
+
Returns the local MediaStream for the current call.
|
|
249
|
+
|
|
250
|
+
```javascript
|
|
251
|
+
const stream = getLocalStream() // Returns: MediaStream | null
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
#### `getInboundAudioEnergy()`
|
|
255
|
+
|
|
256
|
+
Gets the total audio energy of the inbound audio stream (useful for visualizations).
|
|
257
|
+
|
|
258
|
+
```javascript
|
|
259
|
+
const energy = await getInboundAudioEnergy() // Returns: number
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
#### `getOutboundAudioEnergy()`
|
|
263
|
+
|
|
264
|
+
Gets the total audio energy of the outbound audio stream.
|
|
265
|
+
|
|
266
|
+
```javascript
|
|
267
|
+
const energy = await getOutboundAudioEnergy() // Returns: number
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
---
|
|
271
|
+
|
|
272
|
+
### Constants
|
|
273
|
+
|
|
274
|
+
#### `MESSAGE_ROLES`
|
|
275
|
+
|
|
276
|
+
Enum for message roles in the chat.
|
|
277
|
+
|
|
278
|
+
```javascript
|
|
279
|
+
import { MESSAGE_ROLES } from '@origonai/web-chat-sdk'
|
|
280
|
+
|
|
281
|
+
MESSAGE_ROLES.BOT // 'assistant' - AI/bot responses
|
|
282
|
+
MESSAGE_ROLES.USER // 'user' - End user messages
|
|
283
|
+
MESSAGE_ROLES.AGENT // 'agent' - Human agent messages
|
|
284
|
+
MESSAGE_ROLES.SYSTEM // 'system' - System notifications
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
---
|
|
288
|
+
|
|
289
|
+
## Types
|
|
290
|
+
|
|
291
|
+
### Message
|
|
292
|
+
|
|
293
|
+
```typescript
|
|
294
|
+
interface Message {
|
|
295
|
+
id?: string
|
|
296
|
+
text: string
|
|
297
|
+
html?: string
|
|
298
|
+
role: 'assistant' | 'user' | 'agent' | 'system'
|
|
299
|
+
timestamp?: string
|
|
300
|
+
loading?: boolean
|
|
301
|
+
done?: boolean
|
|
302
|
+
errorText?: string
|
|
303
|
+
video?: object // YouTube video data
|
|
304
|
+
channel?: string
|
|
305
|
+
}
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
### ChatCallbacks
|
|
309
|
+
|
|
310
|
+
```typescript
|
|
311
|
+
interface ChatCallbacks {
|
|
312
|
+
onMessagesUpdate?: (messages: Message[]) => void
|
|
313
|
+
onTyping?: (isTyping: boolean) => void
|
|
314
|
+
onLiveAgentMode?: (isLiveAgent: boolean) => void
|
|
315
|
+
onSessionUpdate?: (sessionId: string) => void
|
|
316
|
+
}
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
### CallCallbacks
|
|
320
|
+
|
|
321
|
+
```typescript
|
|
322
|
+
interface CallCallbacks {
|
|
323
|
+
onCallStatus?: (status: 'connecting' | 'connected' | 'disconnected' | 'error') => void
|
|
324
|
+
onCallError?: (error: string | null) => void
|
|
325
|
+
}
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
---
|
|
329
|
+
|
|
330
|
+
## Advanced Usage
|
|
331
|
+
|
|
332
|
+
### Resuming a Previous Session
|
|
333
|
+
|
|
334
|
+
```javascript
|
|
335
|
+
// Get session from history
|
|
336
|
+
const { sessions } = await getHistory()
|
|
337
|
+
const previousSession = sessions[0]
|
|
338
|
+
|
|
339
|
+
// Resume the session
|
|
340
|
+
const { messages } = await startChat({
|
|
341
|
+
sessionId: previousSession.sessionId
|
|
342
|
+
})
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
### Authenticated Users
|
|
346
|
+
|
|
347
|
+
```javascript
|
|
348
|
+
initialize({
|
|
349
|
+
endpoint: 'https://your-endpoint.com/api/chat',
|
|
350
|
+
token: 'your-jwt-token',
|
|
351
|
+
externalId: 'user-123'
|
|
352
|
+
})
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
### Combining Chat and Voice
|
|
356
|
+
|
|
357
|
+
```javascript
|
|
358
|
+
import {
|
|
359
|
+
initialize,
|
|
360
|
+
startChat,
|
|
361
|
+
sendMessage,
|
|
362
|
+
startCall,
|
|
363
|
+
disconnectCall,
|
|
364
|
+
setCallbacks,
|
|
365
|
+
setCallCallbacks
|
|
366
|
+
} from '@origonai/web-chat-sdk'
|
|
367
|
+
|
|
368
|
+
// Set up both chat and call callbacks
|
|
369
|
+
setCallbacks({
|
|
370
|
+
onMessagesUpdate: (messages) => updateChatUI(messages),
|
|
371
|
+
onSessionUpdate: (sessionId) => saveSession(sessionId)
|
|
372
|
+
})
|
|
373
|
+
|
|
374
|
+
setCallCallbacks({
|
|
375
|
+
onCallStatus: (status) => updateCallUI(status)
|
|
376
|
+
})
|
|
377
|
+
|
|
378
|
+
initialize({ endpoint: 'https://your-endpoint.com/api/chat' })
|
|
379
|
+
|
|
380
|
+
// Start chat
|
|
381
|
+
const { sessionId } = await startChat()
|
|
382
|
+
|
|
383
|
+
// Later, start a voice call in the same session
|
|
384
|
+
await startCall({ sessionId })
|
|
385
|
+
|
|
386
|
+
// Messages and voice share the same session
|
|
387
|
+
await sendMessage({ text: 'Can you hear me?' })
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
### Audio Visualization
|
|
391
|
+
|
|
392
|
+
```javascript
|
|
393
|
+
// Create an audio level meter
|
|
394
|
+
async function updateAudioLevel() {
|
|
395
|
+
try {
|
|
396
|
+
const inbound = await getInboundAudioEnergy()
|
|
397
|
+
const outbound = await getOutboundAudioEnergy()
|
|
398
|
+
|
|
399
|
+
updateVisualization({ inbound, outbound })
|
|
400
|
+
} catch (e) {
|
|
401
|
+
// Handle no active call
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
setInterval(updateAudioLevel, 100)
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
---
|
|
409
|
+
|
|
410
|
+
## Browser Support
|
|
411
|
+
|
|
412
|
+
- Chrome 80+
|
|
413
|
+
- Firefox 78+
|
|
414
|
+
- Safari 14+
|
|
415
|
+
- Edge 80+
|
|
416
|
+
|
|
417
|
+
Voice calls require WebRTC support and microphone permissions.
|
|
418
|
+
|
|
419
|
+
---
|
|
420
|
+
|
|
421
|
+
## License
|
|
422
|
+
|
|
423
|
+
MIT © Origon
|