obi-sdk 0.19.51 → 0.19.52
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 +206 -0
- package/dist/index.js +43 -43
- package/dist/index2.js +1 -1
- package/dist/obi-sdk.standalone.iife.js +43 -43
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -78,6 +78,212 @@ Key features of session persistence:
|
|
|
78
78
|
|
|
79
79
|
No additional configuration is required to enable this feature.
|
|
80
80
|
|
|
81
|
+
## Command API
|
|
82
|
+
|
|
83
|
+
The Obi SDK provides a programmatic command API for controlling sessions and interactions. While most users should use the [obi-loader](https://www.npmjs.com/package/obi-loader) package which provides a convenient wrapper, advanced users can access the SDK instance directly via `window.obi`.
|
|
84
|
+
|
|
85
|
+
### Using window.ObiSDK() (Recommended)
|
|
86
|
+
|
|
87
|
+
For most use cases, use the global `window.ObiSDK()` function provided by the obi-loader:
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
// Start a session
|
|
91
|
+
window.ObiSDK("startSession", { planUuid: "plan-uuid", withMicrophone: true })
|
|
92
|
+
|
|
93
|
+
// Send a message
|
|
94
|
+
window.ObiSDK("say", "Help me with this feature")
|
|
95
|
+
|
|
96
|
+
// Stop a session
|
|
97
|
+
window.ObiSDK("stopSession")
|
|
98
|
+
|
|
99
|
+
// Update configuration
|
|
100
|
+
window.ObiSDK("update", { showMenu: true })
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
For detailed documentation and examples of all commands, see the [obi-loader README](https://www.npmjs.com/package/obi-loader#programmatic-commands).
|
|
104
|
+
|
|
105
|
+
### Advanced: Direct SDK Instance Usage
|
|
106
|
+
|
|
107
|
+
Advanced users can access the SDK instance directly for more control:
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
// Access the SDK instance
|
|
111
|
+
const sdk = window.obi
|
|
112
|
+
|
|
113
|
+
// Start a session
|
|
114
|
+
await sdk.startSession({ withMicrophone: true })
|
|
115
|
+
|
|
116
|
+
// Send a message
|
|
117
|
+
await sdk.say("I need help with this feature")
|
|
118
|
+
|
|
119
|
+
// Stop the session
|
|
120
|
+
sdk.client?.stopSession()
|
|
121
|
+
|
|
122
|
+
// Update configuration
|
|
123
|
+
sdk.updateConfig({ showMenu: true, isActive: true })
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Available Commands
|
|
127
|
+
|
|
128
|
+
| Command | Description | Parameters |
|
|
129
|
+
| -------------- | ---------------------------------------------- | ------------------------------------------------------- |
|
|
130
|
+
| `update` | Update widget configuration | `config: Partial<Config>` - Configuration object |
|
|
131
|
+
| `startSession` | Start an onboarding session | `options?: { planUuid?: string, withMicrophone?: boolean }` |
|
|
132
|
+
| `say` | Send a text message to the agent | `message: string` - Message to send |
|
|
133
|
+
| `stopSession` | Stop the current session | None |
|
|
134
|
+
|
|
135
|
+
**Note**: For comprehensive documentation, usage examples, and best practices, refer to the [obi-loader package documentation](https://www.npmjs.com/package/obi-loader).
|
|
136
|
+
|
|
137
|
+
## Event API
|
|
138
|
+
|
|
139
|
+
The Obi SDK extends `EventEmitter` and provides events for monitoring SDK state and responding to changes. Access the SDK instance via `window.obi` to subscribe to events.
|
|
140
|
+
|
|
141
|
+
### Available Events
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
// Listen for SDK initialization
|
|
145
|
+
window.obi.on("initialised", () => {
|
|
146
|
+
console.log("Obi SDK is ready")
|
|
147
|
+
// Perform actions after SDK is initialized
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
// Handle errors
|
|
151
|
+
window.obi.on("error", (error: Error) => {
|
|
152
|
+
console.error("Obi SDK error:", error)
|
|
153
|
+
// Report to error tracking service
|
|
154
|
+
errorTracker.captureException(error)
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
// Monitor menu visibility changes
|
|
158
|
+
window.obi.on("showMenu", (isVisible: boolean) => {
|
|
159
|
+
console.log(`Menu is now ${isVisible ? "visible" : "hidden"}`)
|
|
160
|
+
// Track analytics
|
|
161
|
+
analytics.track("obi_menu_toggled", { visible: isVisible })
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
// Track configuration updates
|
|
165
|
+
window.obi.on("configUpdated", () => {
|
|
166
|
+
console.log("Obi configuration has been updated")
|
|
167
|
+
// Sync with your application state
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
// Handle transient messages
|
|
171
|
+
window.obi.on("showMessage", ({ text, autoCloseMs }) => {
|
|
172
|
+
console.log(`Message: ${text} (auto-close in ${autoCloseMs}ms)`)
|
|
173
|
+
// Display in custom UI if needed
|
|
174
|
+
})
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Event Reference
|
|
178
|
+
|
|
179
|
+
| Event | Parameters | Description |
|
|
180
|
+
| ---------------- | ----------------------------------------- | -------------------------------------------------------------- |
|
|
181
|
+
| `initialised` | None | Fired when SDK initialization is complete |
|
|
182
|
+
| `error` | `error: Error` | Fired when an error occurs in the SDK |
|
|
183
|
+
| `showMenu` | `isVisible: boolean` | Fired when the widget menu visibility changes |
|
|
184
|
+
| `configUpdated` | None | Fired when the SDK configuration is updated |
|
|
185
|
+
| `showMessage` | `{ text: string, autoCloseMs?: number }` | Fired when a transient message should be displayed to the user |
|
|
186
|
+
|
|
187
|
+
### Use Cases
|
|
188
|
+
|
|
189
|
+
**Error Tracking Integration:**
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
window.obi.on("error", (error) => {
|
|
193
|
+
// Send to Sentry, Bugsnag, or other error tracking service
|
|
194
|
+
Sentry.captureException(error, {
|
|
195
|
+
tags: { source: "obi-sdk" },
|
|
196
|
+
})
|
|
197
|
+
})
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
**Analytics Integration:**
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
window.obi.on("initialised", () => {
|
|
204
|
+
analytics.track("obi_sdk_loaded")
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
window.obi.on("showMenu", (isVisible) => {
|
|
208
|
+
analytics.track("obi_menu_interaction", {
|
|
209
|
+
action: isVisible ? "opened" : "closed",
|
|
210
|
+
timestamp: new Date().toISOString(),
|
|
211
|
+
})
|
|
212
|
+
})
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
**UI Synchronization:**
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
// Sync menu state with your application UI
|
|
219
|
+
window.obi.on("showMenu", (isVisible) => {
|
|
220
|
+
document.getElementById("obi-menu-indicator")?.classList.toggle("active", isVisible)
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
// Display custom notifications
|
|
224
|
+
window.obi.on("showMessage", ({ text, autoCloseMs }) => {
|
|
225
|
+
showCustomToast(text, autoCloseMs || 5000)
|
|
226
|
+
})
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
**State Management Integration:**
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
// Redux example
|
|
233
|
+
window.obi.on("configUpdated", () => {
|
|
234
|
+
store.dispatch(obiConfigUpdated())
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
window.obi.on("error", (error) => {
|
|
238
|
+
store.dispatch(obiError(error.message))
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
// React state example
|
|
242
|
+
function useObiEvents() {
|
|
243
|
+
const [isMenuVisible, setIsMenuVisible] = useState(false)
|
|
244
|
+
const [isInitialized, setIsInitialized] = useState(false)
|
|
245
|
+
|
|
246
|
+
useEffect(() => {
|
|
247
|
+
const sdk = window.obi
|
|
248
|
+
|
|
249
|
+
sdk.on("initialised", () => setIsInitialized(true))
|
|
250
|
+
sdk.on("showMenu", setIsMenuVisible)
|
|
251
|
+
|
|
252
|
+
return () => {
|
|
253
|
+
sdk.off("initialised")
|
|
254
|
+
sdk.off("showMenu")
|
|
255
|
+
}
|
|
256
|
+
}, [])
|
|
257
|
+
|
|
258
|
+
return { isMenuVisible, isInitialized }
|
|
259
|
+
}
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
### TypeScript Support
|
|
263
|
+
|
|
264
|
+
The SDK provides full TypeScript support for events:
|
|
265
|
+
|
|
266
|
+
```typescript
|
|
267
|
+
import type { ObiSDK, ObiSDKEvents } from "@obi/obi-sdk"
|
|
268
|
+
|
|
269
|
+
// Event handlers are fully typed
|
|
270
|
+
window.obi.on("initialised", () => {
|
|
271
|
+
// No parameters
|
|
272
|
+
})
|
|
273
|
+
|
|
274
|
+
window.obi.on("error", (error: Error) => {
|
|
275
|
+
// error is typed as Error
|
|
276
|
+
})
|
|
277
|
+
|
|
278
|
+
window.obi.on("showMenu", (isVisible: boolean) => {
|
|
279
|
+
// isVisible is typed as boolean
|
|
280
|
+
})
|
|
281
|
+
|
|
282
|
+
window.obi.on("showMessage", ({ text, autoCloseMs }) => {
|
|
283
|
+
// Parameters are typed correctly
|
|
284
|
+
})
|
|
285
|
+
```
|
|
286
|
+
|
|
81
287
|
## Engagement Sessions
|
|
82
288
|
|
|
83
289
|
The SDK also tracks lightweight engagement sessions per product + onboardee for analytics. A new engagement session starts after 30 minutes of inactivity and is stored in `localStorage`. You can subscribe to the `engagementSessionStarted` event and read current values via `getEngagementInfo()` on the SDK instance (available as `window.obi` when using the loader).
|