@touchcastllc/napster-companion-api-dev 1.0.0-alpha.43
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/LICENSE +21 -0
- package/README.md +830 -0
- package/lib/components/Avatar/index.d.ts +35 -0
- package/lib/components/Embed/index.d.ts +25 -0
- package/lib/components/InactiveOverlay/index.d.ts +15 -0
- package/lib/components/Preview/index.d.ts +18 -0
- package/lib/components/StyledTooltip/index.d.ts +16 -0
- package/lib/components/VolumeControl/index.d.ts +18 -0
- package/lib/components/WaveForm/index.d.ts +19 -0
- package/lib/components/index.d.ts +2 -0
- package/lib/constants/events.d.ts +52 -0
- package/lib/constants/greenscreen.d.ts +28 -0
- package/lib/constants/index.d.ts +6 -0
- package/lib/constants/waveform.d.ts +34 -0
- package/lib/index.css +1 -0
- package/lib/index.d.ts +34 -0
- package/lib/index.esm.js +1 -0
- package/lib/index.js +1 -0
- package/lib/index.standalone.js +1 -0
- package/lib/services/analytics.d.ts +148 -0
- package/lib/services/faceTracking.d.ts +13 -0
- package/lib/services/screenShare.d.ts +15 -0
- package/lib/services/webrtc.d.ts +29 -0
- package/lib/setupTests.d.ts +1 -0
- package/lib/stores/index.d.ts +8 -0
- package/lib/stores/middleware/featuresUpdateMiddleware.d.ts +4 -0
- package/lib/stores/selectors.d.ts +30 -0
- package/lib/stores/slices/appSlice.d.ts +12 -0
- package/lib/stores/slices/avatarSlice.d.ts +26 -0
- package/lib/stores/store.d.ts +11 -0
- package/lib/types/abstract-typing.d.ts +35 -0
- package/lib/types/analytics.d.ts +95 -0
- package/lib/types/errors.d.ts +143 -0
- package/lib/types/index.d.ts +354 -0
- package/lib/umd.d.ts +1 -0
- package/lib/utils/InactiveOverlayManager.d.ts +77 -0
- package/lib/utils/MediaCapture.d.ts +13 -0
- package/lib/utils/classnames.d.ts +50 -0
- package/lib/utils/debug.d.ts +84 -0
- package/lib/utils/domFactory.d.ts +56 -0
- package/lib/utils/greenscreen/GreenScreenProcessor.d.ts +25 -0
- package/lib/utils/greenscreen/index.d.ts +1 -0
- package/lib/utils/index.d.ts +25 -0
- package/lib/utils/mouthDetection.d.ts +33 -0
- package/lib/utils/sendCommand.d.ts +2 -0
- package/lib/utils/svg.d.ts +34 -0
- package/package.json +56 -0
package/README.md
ADDED
|
@@ -0,0 +1,830 @@
|
|
|
1
|
+
# Napster Companion API SDK
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@touchcastllc/napster-companion-api)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
|
|
6
|
+
> Real-time conversational video AI for web applications. Embed AI companions that users can talk to face-to-face via WebRTC. Attaches to any DOM element, handles video streaming and avatar rendering, connects to Azure OpenAI for conversation.
|
|
7
|
+
|
|
8
|
+
**Supported Environments:**
|
|
9
|
+
|
|
10
|
+
- Modern browsers (Chrome, Firefox, Safari, Edge)
|
|
11
|
+
- React 16.x through 19.x
|
|
12
|
+
- Vue 2.x and 3.x
|
|
13
|
+
- Angular 12+
|
|
14
|
+
- Vanilla JavaScript (no build tools required)
|
|
15
|
+
|
|
16
|
+
**Key Capabilities:**
|
|
17
|
+
|
|
18
|
+
- **WebRTC Video Streaming:** Real-time video with AI avatars
|
|
19
|
+
- **Framework Agnostic:** React, Vue, Angular, or vanilla JS
|
|
20
|
+
- **Zero External Dependencies:** Drop in a script tag and go
|
|
21
|
+
- **TypeScript Support:** Fully typed APIs
|
|
22
|
+
- **Tree-Shakeable:** Import only what you need
|
|
23
|
+
- **Production Ready:** Minified builds with type definitions
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## 1. Quick Start
|
|
28
|
+
|
|
29
|
+
Get up and running in minutes. Follow these four steps to integrate the SDK into your project.
|
|
30
|
+
|
|
31
|
+
### 1.1 Choose Your Setup
|
|
32
|
+
|
|
33
|
+
Pick the integration method that matches your project:
|
|
34
|
+
|
|
35
|
+
| Build Type | Best For | Import Path | Dependencies |
|
|
36
|
+
| -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------- | -------------------- |
|
|
37
|
+
| **ESM** | ✅ **Recommended for most projects**<br>✅ You use a bundler (Vite, Webpack, Rollup, etc.)<br>✅ You want optimal bundle size (dependencies not bundled)<br>✅ You're using TypeScript or modern JavaScript | `@touchcastllc/napster-companion-api` | Redux Toolkit (peer) |
|
|
38
|
+
| **Standalone** | ✅ You want a `<script>` tag (no bundler, no external dependencies)<br>✅ You want zero external dependencies<br>✅ You don't want to manage dependencies separately<br>✅ You're okay with a slightly larger file (all dependencies included) | `lib/index.standalone.js` | All bundled |
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
### 1.2 Installation & Setup
|
|
43
|
+
|
|
44
|
+
#### 1.2.1 For ESM Build Type (Recommended)
|
|
45
|
+
|
|
46
|
+
**Step 1: Install the SDK**
|
|
47
|
+
|
|
48
|
+
Install the package:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
npm install @touchcastllc/napster-companion-api
|
|
52
|
+
# or
|
|
53
|
+
yarn add @touchcastllc/napster-companion-api
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Install peer dependencies:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
npm install @reduxjs/toolkit
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
**Step 2: Import the SDK**
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import { NapsterCompanionApiSdk } from "@touchcastllc/napster-companion-api";
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
**Also, import CSS Stylesheet (required):**
|
|
69
|
+
|
|
70
|
+
The SDK comes with a complete stylesheet for all components. Import it in your application:
|
|
71
|
+
|
|
72
|
+
**ESM/TypeScript:**
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
import "@touchcastllc/napster-companion-api/styles";
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
**CommonJS:**
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
require("@touchcastllc/napster-companion-api/styles");
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
**HTML `<link>` Tag:**
|
|
85
|
+
|
|
86
|
+
```html
|
|
87
|
+
<link
|
|
88
|
+
rel="stylesheet"
|
|
89
|
+
href="https://cdn.jsdelivr.net/npm/@touchcastllc/napster-companion-api@latest/lib/index.css"
|
|
90
|
+
/>
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
**CSS `@import`:**
|
|
94
|
+
|
|
95
|
+
```css
|
|
96
|
+
@import url("https://cdn.jsdelivr.net/npm/@touchcastllc/napster-companion-api@latest/lib/index.css");
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
The CSS file (`index.css`) is minified and contains all styles needed for the avatar widget, controls, tooltips, and more.
|
|
100
|
+
|
|
101
|
+
#### 1.2.2 For Standalone Build Type
|
|
102
|
+
|
|
103
|
+
**Step 1: Import the SDK**
|
|
104
|
+
|
|
105
|
+
```html
|
|
106
|
+
<!-- All dependencies bundled (easier, larger file) -->
|
|
107
|
+
<script src="https://cdn.jsdelivr.net/npm/@touchcastllc/napster-companion-api@latest/lib/index.standalone.js"></script>
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
### 1.3 Initialization
|
|
113
|
+
|
|
114
|
+
#### 1.3.1 For ESM Build Type
|
|
115
|
+
|
|
116
|
+
Initialize the SDK by calling `NapsterCompanionApiSdk.init()` with your auth token and configuration options.
|
|
117
|
+
|
|
118
|
+
See the [1.4 Authentication](#14-authentication) section for details on generating auth tokens.
|
|
119
|
+
|
|
120
|
+
**Basic Example:**
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
const widget = await NapsterCompanionApiSdk.init("YOUR_AUTH_TOKEN", {
|
|
124
|
+
mountContainer: "#avatar-container",
|
|
125
|
+
});
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
#### 1.3.2 For Standalone Build Type
|
|
129
|
+
|
|
130
|
+
Initialize the SDK by calling `window.napsterCompanionApiSDK.init()` with your auth token and configuration options.
|
|
131
|
+
|
|
132
|
+
See the [1.4 Authentication](#14-authentication) section for details on generating auth tokens.
|
|
133
|
+
|
|
134
|
+
**Step 1: Add container to your HTML**
|
|
135
|
+
|
|
136
|
+
```html
|
|
137
|
+
<div id="sdk-container"></div>
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
**Step 2: Initialize globally**
|
|
141
|
+
|
|
142
|
+
```html
|
|
143
|
+
<script>
|
|
144
|
+
window.napsterCompanionApiSDK.init("AUTH_TOKEN", {
|
|
145
|
+
mountContainer: "#sdk-container",
|
|
146
|
+
});
|
|
147
|
+
</script>
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
---
|
|
151
|
+
|
|
152
|
+
### 1.4 Authentication
|
|
153
|
+
|
|
154
|
+
The SDK requires an **auth token** to connect to the Napster Companion API.
|
|
155
|
+
|
|
156
|
+
**Important:** **Do not generate tokens in client-side code** — use a backend service to keep your API key secure.
|
|
157
|
+
|
|
158
|
+
#### Step 1: Generate Napster Companion API Key
|
|
159
|
+
|
|
160
|
+
1. Visit the [Napster Companion API Keys page](https://companion-api.napster.com/admin/organization/keys)
|
|
161
|
+
2. Click **Generate API key** and enter your details
|
|
162
|
+
3. Store your API key securely on your backend server
|
|
163
|
+
|
|
164
|
+
#### Step 2: Create Connection & Get Token (Backend)
|
|
165
|
+
|
|
166
|
+
Create a backend API call to the Napster Companion API to generate auth tokens:
|
|
167
|
+
|
|
168
|
+
**Request URL:**
|
|
169
|
+
|
|
170
|
+
```
|
|
171
|
+
POST https://companion-api.napster.com/public/connections
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
**Request Headers:**
|
|
175
|
+
|
|
176
|
+
| Header | Value |
|
|
177
|
+
| -------------- | ------------------- |
|
|
178
|
+
| `content-type` | `application/json` |
|
|
179
|
+
| `x-api-key` | `YOUR_API_KEY_HERE` |
|
|
180
|
+
|
|
181
|
+
**Request Body:**
|
|
182
|
+
|
|
183
|
+
| Field | Type | Description |
|
|
184
|
+
| ----------------------------------------------------------- | ------- | ------------------------------------ |
|
|
185
|
+
| `companionId` | string | Your Napster Companion ID |
|
|
186
|
+
| `providerConfig.type` | string | Provider type (e.g., `azureOpenAI`) |
|
|
187
|
+
| `providerConfig.voiceId` | string | Voice identifier (e.g., `alloy`) |
|
|
188
|
+
| `providerConfig.settings.instructions` | string | Custom instructions for the avatar |
|
|
189
|
+
| `providerConfig.settings.turnDetection.threshold` | number | Voice detection threshold |
|
|
190
|
+
| `providerConfig.settings.turnDetection.prefix_padding_ms` | number | Padding before speech detection (ms) |
|
|
191
|
+
| `providerConfig.settings.turnDetection.silence_duration_ms` | number | Silence duration threshold (ms) |
|
|
192
|
+
| `providerConfig.settings.temperature` | number | Response temperature (0-1) |
|
|
193
|
+
| `providerConfig.useGreenVideo` | boolean | Enable green screen background |
|
|
194
|
+
| `disableIdleTimeout` | boolean | Disable idle timeout |
|
|
195
|
+
|
|
196
|
+
**Example Request:**
|
|
197
|
+
|
|
198
|
+
```bash
|
|
199
|
+
curl 'https://companion-api.napster.com/public/connections' \
|
|
200
|
+
-H 'content-type: application/json' \
|
|
201
|
+
-H 'x-api-key: YOUR_API_KEY_HERE' \
|
|
202
|
+
--data-raw '{
|
|
203
|
+
"companionId": "YOUR_COMPANION_ID",
|
|
204
|
+
"providerConfig": {
|
|
205
|
+
"type": "azureOpenAI",
|
|
206
|
+
"voiceId": "alloy",
|
|
207
|
+
"settings": {
|
|
208
|
+
"instructions": "Your custom instructions here...",
|
|
209
|
+
"turnDetection": {
|
|
210
|
+
"threshold": 0,
|
|
211
|
+
"prefix_padding_ms": 0,
|
|
212
|
+
"silence_duration_ms": 0
|
|
213
|
+
},
|
|
214
|
+
"temperature": 0
|
|
215
|
+
},
|
|
216
|
+
"useGreenVideo": true
|
|
217
|
+
},
|
|
218
|
+
"disableIdleTimeout": false
|
|
219
|
+
}'
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
#### Step 3: Use Token in Client
|
|
223
|
+
|
|
224
|
+
Fetch the token from your backend and pass it to the SDK:
|
|
225
|
+
|
|
226
|
+
```typescript
|
|
227
|
+
// Possible client-side code for token retrieval
|
|
228
|
+
async function initializeAvatar() {
|
|
229
|
+
// Fetch token from your backend
|
|
230
|
+
const response = await fetch("/your-backend-endpoint", { method: "POST" });
|
|
231
|
+
const { token } = await response.json();
|
|
232
|
+
}
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
---
|
|
236
|
+
|
|
237
|
+
## 2. Examples
|
|
238
|
+
|
|
239
|
+
We provide example implementations for popular frameworks and vanilla JavaScript:
|
|
240
|
+
|
|
241
|
+
### 2.1 React
|
|
242
|
+
|
|
243
|
+
```tsx
|
|
244
|
+
import React, { useEffect, useRef, useState } from "react";
|
|
245
|
+
import { NapsterCompanionApiSdk } from "@touchcastllc/napster-companion-api";
|
|
246
|
+
import type { NapsterCompanionApiInstance } from "@touchcastllc/napster-companion-api";
|
|
247
|
+
|
|
248
|
+
export function CompanionWidget() {
|
|
249
|
+
const containerRef = useRef<HTMLDivElement>(null);
|
|
250
|
+
const [instance, setInstance] = useState<NapsterCompanionApiInstance | null>(
|
|
251
|
+
null
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
useEffect(() => {
|
|
255
|
+
const initSDK = async () => {
|
|
256
|
+
if (!containerRef.current) return;
|
|
257
|
+
|
|
258
|
+
const result = await NapsterCompanionApiSdk.init("YOUR_TOKEN", {
|
|
259
|
+
mountContainer: containerRef.current,
|
|
260
|
+
position: "bottom-right",
|
|
261
|
+
});
|
|
262
|
+
setInstance(result);
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
initSDK();
|
|
266
|
+
|
|
267
|
+
return () => {
|
|
268
|
+
instance?.destroy();
|
|
269
|
+
};
|
|
270
|
+
}, []);
|
|
271
|
+
|
|
272
|
+
return <div ref={containerRef} style={{ width: "100%", height: "100%" }} />;
|
|
273
|
+
}
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
### 2.2 Vue 3
|
|
277
|
+
|
|
278
|
+
```html
|
|
279
|
+
<template>
|
|
280
|
+
<div ref="containerRef"></div>
|
|
281
|
+
</template>
|
|
282
|
+
|
|
283
|
+
<script setup lang="ts">
|
|
284
|
+
import { onMounted, onUnmounted, ref } from "vue";
|
|
285
|
+
import { NapsterCompanionApiSdk } from "@touchcastllc/napster-companion-api";
|
|
286
|
+
import type { NapsterCompanionApiInstance } from "@touchcastllc/napster-companion-api";
|
|
287
|
+
|
|
288
|
+
const containerRef = ref<HTMLElement>();
|
|
289
|
+
let instance: NapsterCompanionApiInstance | null = null;
|
|
290
|
+
|
|
291
|
+
onMounted(async () => {
|
|
292
|
+
if (!containerRef.value) return;
|
|
293
|
+
|
|
294
|
+
instance = await NapsterCompanionApiSdk.init("YOUR_TOKEN", {
|
|
295
|
+
mountContainer: containerRef.value,
|
|
296
|
+
position: "bottom-right",
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
onUnmounted(() => {
|
|
301
|
+
instance?.destroy();
|
|
302
|
+
});
|
|
303
|
+
</script>
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
### 2.3 Angular
|
|
307
|
+
|
|
308
|
+
```typescript
|
|
309
|
+
import {
|
|
310
|
+
Component,
|
|
311
|
+
OnInit,
|
|
312
|
+
OnDestroy,
|
|
313
|
+
ElementRef,
|
|
314
|
+
ViewChild,
|
|
315
|
+
} from "@angular/core";
|
|
316
|
+
import {
|
|
317
|
+
NapsterCompanionApiSdk,
|
|
318
|
+
NapsterCompanionApiInstance,
|
|
319
|
+
} from "@touchcastllc/napster-companion-api";
|
|
320
|
+
|
|
321
|
+
@Component({
|
|
322
|
+
selector: "app-companion",
|
|
323
|
+
template: "<div #containerRef></div>",
|
|
324
|
+
})
|
|
325
|
+
export class CompanionComponent implements OnInit, OnDestroy {
|
|
326
|
+
@ViewChild("containerRef") containerRef!: ElementRef<HTMLDivElement>;
|
|
327
|
+
private instance: NapsterCompanionApiInstance | null = null;
|
|
328
|
+
|
|
329
|
+
async ngOnInit() {
|
|
330
|
+
this.instance = await NapsterCompanionApiSdk.init("YOUR_TOKEN", {
|
|
331
|
+
mountContainer: this.containerRef.nativeElement,
|
|
332
|
+
position: "bottom-right",
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
ngOnDestroy() {
|
|
337
|
+
this.instance?.destroy();
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
### 2.4 Vanilla JavaScript/TypeScript
|
|
343
|
+
|
|
344
|
+
```html
|
|
345
|
+
<div id="sdk-container"></div>
|
|
346
|
+
<script src="https://cdn.jsdelivr.net/npm/@touchcastllc/napster-companion-api@latest/lib/index.standalone.js"></script>
|
|
347
|
+
<script>
|
|
348
|
+
const sdk = window.napsterCompanionApiSDK;
|
|
349
|
+
|
|
350
|
+
sdk
|
|
351
|
+
.init("YOUR_TOKEN", {
|
|
352
|
+
mountContainer: "#sdk-container",
|
|
353
|
+
position: "bottom-right",
|
|
354
|
+
})
|
|
355
|
+
.then(instance => {
|
|
356
|
+
// Control the avatar
|
|
357
|
+
instance.showAvatar();
|
|
358
|
+
|
|
359
|
+
// Hide after 10 seconds
|
|
360
|
+
setTimeout(() => instance.hideAvatar(), 10000);
|
|
361
|
+
});
|
|
362
|
+
</script>
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
### 2.5 Advanced Configuration
|
|
366
|
+
|
|
367
|
+
```typescript
|
|
368
|
+
async function initAdvancedAvatar() {
|
|
369
|
+
const instance = await NapsterCompanionApiSdk.init("YOUR_TOKEN", {
|
|
370
|
+
mountContainer: "#avatar-container",
|
|
371
|
+
position: "bottom-right",
|
|
372
|
+
|
|
373
|
+
avatarStyle: {
|
|
374
|
+
view: "round", // Options: "round" | "rectangle" | "silhouette"
|
|
375
|
+
},
|
|
376
|
+
|
|
377
|
+
features: {
|
|
378
|
+
inactiveTimeout: { enabled: true, duration: 120000, countdown: 15 },
|
|
379
|
+
showSDKLoader: { enabled: true, bgColor: "#f0f0f0" },
|
|
380
|
+
},
|
|
381
|
+
|
|
382
|
+
style: {
|
|
383
|
+
borderRadius: "12px",
|
|
384
|
+
boxShadow: "0 4px 20px rgba(0,0,0,0.3)",
|
|
385
|
+
},
|
|
386
|
+
|
|
387
|
+
onReady: () => console.log("Avatar ready!"),
|
|
388
|
+
onError: error => console.error("Error:", error),
|
|
389
|
+
onAvatarReady: ready => console.log("Avatar loaded:", ready),
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
return instance;
|
|
393
|
+
}
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
### 2.6 Dynamic Feature Control
|
|
397
|
+
|
|
398
|
+
```typescript
|
|
399
|
+
async function controlFeatures() {
|
|
400
|
+
const instance = await NapsterCompanionApiSdk.init("YOUR_TOKEN", {
|
|
401
|
+
mountContainer: "#avatar-container",
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
// Change position dynamically
|
|
405
|
+
document.getElementById("move-avatar")?.addEventListener("click", () => {
|
|
406
|
+
instance.setPosition("top-left");
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
return instance;
|
|
410
|
+
}
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
### 2.7 Custom Styling
|
|
414
|
+
|
|
415
|
+
#### Inline Styles
|
|
416
|
+
|
|
417
|
+
```typescript
|
|
418
|
+
const instance = await NapsterCompanionApiSdk.init(token, {
|
|
419
|
+
style: {
|
|
420
|
+
border: "2px solid #00ff00",
|
|
421
|
+
borderRadius: "8px",
|
|
422
|
+
boxShadow: "0 2px 10px rgba(0,0,0,0.2)",
|
|
423
|
+
background: "rgba(255,255,255,0.95)",
|
|
424
|
+
},
|
|
425
|
+
});
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
#### CSS Classes
|
|
429
|
+
|
|
430
|
+
```typescript
|
|
431
|
+
const instance = await NapsterCompanionApiSdk.init(token, {
|
|
432
|
+
className: "my-custom-avatar",
|
|
433
|
+
});
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
```css
|
|
437
|
+
.my-custom-avatar {
|
|
438
|
+
border: 2px solid #007acc;
|
|
439
|
+
border-radius: 12px;
|
|
440
|
+
backdrop-filter: blur(10px);
|
|
441
|
+
}
|
|
442
|
+
```
|
|
443
|
+
|
|
444
|
+
#### Dynamic Style Updates
|
|
445
|
+
|
|
446
|
+
```typescript
|
|
447
|
+
// Update styles after initialization
|
|
448
|
+
instance.updateStyles({
|
|
449
|
+
opacity: "0.9",
|
|
450
|
+
transform: "scale(1.1)",
|
|
451
|
+
transition: "all 0.3s ease",
|
|
452
|
+
});
|
|
453
|
+
```
|
|
454
|
+
|
|
455
|
+
### 2.8 Avatar Style Configuration
|
|
456
|
+
|
|
457
|
+
Customize the avatar's visual appearance using the `avatarStyle` configuration. Control the avatar's view mode, and other visual properties.
|
|
458
|
+
|
|
459
|
+
#### Avatar View Modes
|
|
460
|
+
|
|
461
|
+
```typescript
|
|
462
|
+
const instance = await NapsterCompanionApiSdk.init(token, {
|
|
463
|
+
avatarStyle: {
|
|
464
|
+
view: "round", // Options: "round" | "rectangle" | "silhouette"
|
|
465
|
+
},
|
|
466
|
+
});
|
|
467
|
+
```
|
|
468
|
+
|
|
469
|
+
**Available View Modes:**
|
|
470
|
+
|
|
471
|
+
- `"round"` (default) - Circular avatar with rounded appearance
|
|
472
|
+
- `"rectangle"` - Rectangular container, ideal for custom styling
|
|
473
|
+
- `"silhouette"` - Full view without background styling. (**Note: works only with green screen feature enabled in [Create Connection](#step-2-create-connection--get-token-backend) API**)
|
|
474
|
+
|
|
475
|
+
#### Avatar Style Example
|
|
476
|
+
|
|
477
|
+
```typescript
|
|
478
|
+
const instance = await NapsterCompanionApiSdk.init(token, {
|
|
479
|
+
avatarStyle: {
|
|
480
|
+
view: "rectangle",
|
|
481
|
+
},
|
|
482
|
+
style: {
|
|
483
|
+
borderRadius: "16px", // Works well with rectangle
|
|
484
|
+
boxShadow: "0 8px 32px rgba(0,0,0,0.2)",
|
|
485
|
+
},
|
|
486
|
+
});
|
|
487
|
+
```
|
|
488
|
+
|
|
489
|
+
---
|
|
490
|
+
|
|
491
|
+
## 3. Feature Configuration
|
|
492
|
+
|
|
493
|
+
### 3.1 Core Features
|
|
494
|
+
|
|
495
|
+
#### Inactivity Timeout
|
|
496
|
+
|
|
497
|
+
Automatically disconnect the avatar after a period of user inactivity. Users receive a countdown notification before disconnection.
|
|
498
|
+
|
|
499
|
+
```typescript
|
|
500
|
+
const instance = await NapsterCompanionApiSdk.init(token, {
|
|
501
|
+
features: {
|
|
502
|
+
inactiveTimeout: {
|
|
503
|
+
enabled: true,
|
|
504
|
+
duration: 60000, // 60 seconds (max: 180000ms)
|
|
505
|
+
countdown: 10, // 10 second countdown (max: 60s)
|
|
506
|
+
},
|
|
507
|
+
},
|
|
508
|
+
});
|
|
509
|
+
```
|
|
510
|
+
|
|
511
|
+
#### SDK Loading Screen
|
|
512
|
+
|
|
513
|
+
Display a loading screen while the avatar assets are being loaded. Customize the background color to match your application's design.
|
|
514
|
+
|
|
515
|
+
```typescript
|
|
516
|
+
const instance = await NapsterCompanionApiSdk.init(token, {
|
|
517
|
+
features: {
|
|
518
|
+
showSDKLoader: {
|
|
519
|
+
enabled: true,
|
|
520
|
+
bgColor: "#ffffff", // Optional background color
|
|
521
|
+
},
|
|
522
|
+
},
|
|
523
|
+
});
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
### 3.2 Position & Layout
|
|
527
|
+
|
|
528
|
+
Customize the avatar's position on the screen using predefined positions or custom styles. Also you can override the position using style properties.
|
|
529
|
+
|
|
530
|
+
```typescript
|
|
531
|
+
const instance = await NapsterCompanionApiSdk.init(token, {
|
|
532
|
+
mountContainer: "#my-container", // CSS selector or HTMLElement
|
|
533
|
+
position: "bottom-right", // Predefined positions
|
|
534
|
+
// Apply custom inline styles to override default SDK styles, will be added to root container
|
|
535
|
+
style: {
|
|
536
|
+
top: "20px",
|
|
537
|
+
left: "20px",
|
|
538
|
+
zIndex: "1000",
|
|
539
|
+
},
|
|
540
|
+
// Apply CSS class name to override default SDK styles, will be added to root container
|
|
541
|
+
className: "my-custom-class",
|
|
542
|
+
});
|
|
543
|
+
```
|
|
544
|
+
|
|
545
|
+
**Available Positions:**
|
|
546
|
+
|
|
547
|
+
- `"bottom-right"` (default)
|
|
548
|
+
- `"bottom-left"`
|
|
549
|
+
- `"bottom-center"`
|
|
550
|
+
- `"top-right"`
|
|
551
|
+
- `"top-left"`
|
|
552
|
+
- `"top-center"`
|
|
553
|
+
- `"center"`
|
|
554
|
+
|
|
555
|
+
## 4. Event Handling
|
|
556
|
+
|
|
557
|
+
The SDK provides several event callbacks for monitoring status and handling errors.
|
|
558
|
+
|
|
559
|
+
```typescript
|
|
560
|
+
const instance = await NapsterCompanionApiSdk.init(token, {
|
|
561
|
+
// Fires when the SDK is initialized and ready to use
|
|
562
|
+
onReady: () => console.log("SDK ready!"),
|
|
563
|
+
// Fires on any SDK error
|
|
564
|
+
onError: error => console.error("SDK error:", error),
|
|
565
|
+
// Fires when the avatar is fully loaded and ready
|
|
566
|
+
onAvatarReady: isReady => console.log("Avatar ready:", isReady),
|
|
567
|
+
// Fires when user inactivity status changes
|
|
568
|
+
onInactivityStatusChange: isInactive => console.log("Inactive:", isInactive),
|
|
569
|
+
// Fires when the SDK is destroyed
|
|
570
|
+
onDestroy: () => console.log("SDK destroyed"),
|
|
571
|
+
// Fires when data is received from the WEBRTC data channel
|
|
572
|
+
onData: data => console.log("Data received:", data),
|
|
573
|
+
});
|
|
574
|
+
```
|
|
575
|
+
|
|
576
|
+
---
|
|
577
|
+
|
|
578
|
+
## 5. API Reference
|
|
579
|
+
|
|
580
|
+
### 5.1 Core Methods
|
|
581
|
+
|
|
582
|
+
#### `init(token: string, config?: NapsterCompanionApiConfig): Promise<NapsterCompanionApiInstance>`
|
|
583
|
+
|
|
584
|
+
Initialize the SDK with authentication token and configuration.
|
|
585
|
+
|
|
586
|
+
```typescript
|
|
587
|
+
const instance = await NapsterCompanionApiSdk.init("YOUR_TOKEN", {
|
|
588
|
+
mountContainer: "#avatar-container",
|
|
589
|
+
position: "bottom-right",
|
|
590
|
+
});
|
|
591
|
+
```
|
|
592
|
+
|
|
593
|
+
#### `showAvatar(): void`
|
|
594
|
+
|
|
595
|
+
Make the avatar visible on screen.
|
|
596
|
+
|
|
597
|
+
```typescript
|
|
598
|
+
instance.showAvatar();
|
|
599
|
+
```
|
|
600
|
+
|
|
601
|
+
#### `hideAvatar(): void`
|
|
602
|
+
|
|
603
|
+
Hide the avatar from screen.
|
|
604
|
+
|
|
605
|
+
```typescript
|
|
606
|
+
instance.hideAvatar();
|
|
607
|
+
```
|
|
608
|
+
|
|
609
|
+
#### `avatarIsVisible(): boolean`
|
|
610
|
+
|
|
611
|
+
Check if the avatar is currently visible.
|
|
612
|
+
|
|
613
|
+
```typescript
|
|
614
|
+
if (instance.avatarIsVisible()) {
|
|
615
|
+
console.log("Avatar is visible");
|
|
616
|
+
}
|
|
617
|
+
```
|
|
618
|
+
|
|
619
|
+
#### `destroy(): void`
|
|
620
|
+
|
|
621
|
+
Cleanup and remove the SDK completely.
|
|
622
|
+
|
|
623
|
+
```typescript
|
|
624
|
+
instance.destroy();
|
|
625
|
+
```
|
|
626
|
+
|
|
627
|
+
### 5.2 Styling & Position Methods
|
|
628
|
+
|
|
629
|
+
#### `updateStyles(styles: StyleObject): void`
|
|
630
|
+
|
|
631
|
+
Update container styles dynamically.
|
|
632
|
+
|
|
633
|
+
```typescript
|
|
634
|
+
instance.updateStyles({
|
|
635
|
+
top: "50px",
|
|
636
|
+
left: "50px",
|
|
637
|
+
opacity: "0.8",
|
|
638
|
+
});
|
|
639
|
+
```
|
|
640
|
+
|
|
641
|
+
#### `setPosition(position: Position): void`
|
|
642
|
+
|
|
643
|
+
Change avatar position on screen.
|
|
644
|
+
|
|
645
|
+
```typescript
|
|
646
|
+
instance.setPosition("top-left");
|
|
647
|
+
// or using enum
|
|
648
|
+
import { Position } from "@touchcastllc/napster-companion-api";
|
|
649
|
+
instance.setPosition(Position.TOP_LEFT);
|
|
650
|
+
```
|
|
651
|
+
|
|
652
|
+
#### `clearPosition(): void`
|
|
653
|
+
|
|
654
|
+
Reset position to default/configured value.
|
|
655
|
+
|
|
656
|
+
```typescript
|
|
657
|
+
instance.clearPosition();
|
|
658
|
+
```
|
|
659
|
+
|
|
660
|
+
### 5.3 Feature Control Methods
|
|
661
|
+
|
|
662
|
+
#### `enableFeature(feature: string): void`
|
|
663
|
+
|
|
664
|
+
Enable a specific feature.
|
|
665
|
+
|
|
666
|
+
```typescript
|
|
667
|
+
instance.enableFeature("disclaimer");
|
|
668
|
+
```
|
|
669
|
+
|
|
670
|
+
#### `disableFeature(feature: string): void`
|
|
671
|
+
|
|
672
|
+
Disable a specific feature.
|
|
673
|
+
|
|
674
|
+
```typescript
|
|
675
|
+
instance.disableFeature("disclaimer");
|
|
676
|
+
```
|
|
677
|
+
|
|
678
|
+
#### `isFeatureEnabled(feature: string): boolean`
|
|
679
|
+
|
|
680
|
+
Check if a feature is currently enabled.
|
|
681
|
+
|
|
682
|
+
```typescript
|
|
683
|
+
if (instance.isFeatureEnabled("disclaimer")) {
|
|
684
|
+
console.log("Disclaimer is enabled");
|
|
685
|
+
}
|
|
686
|
+
```
|
|
687
|
+
|
|
688
|
+
### 5.4 Communication Methods
|
|
689
|
+
|
|
690
|
+
#### `sendCommand(command: { type: string; data?: object }): void`
|
|
691
|
+
|
|
692
|
+
Send a command to the avatar via the data channel.
|
|
693
|
+
|
|
694
|
+
```typescript
|
|
695
|
+
// Send a message to the avatar
|
|
696
|
+
instance.sendCommand({ type: "send_message", data: { text: "Hello, how are you?" } });
|
|
697
|
+
|
|
698
|
+
// Cancel the current response
|
|
699
|
+
instance.sendCommand({ type: "cancel" });
|
|
700
|
+
```
|
|
701
|
+
|
|
702
|
+
## 6. Configuration Interface
|
|
703
|
+
|
|
704
|
+
```typescript
|
|
705
|
+
interface NapsterCompanionApiConfig {
|
|
706
|
+
// Container mounting
|
|
707
|
+
mountContainer?: HTMLElement | string | null;
|
|
708
|
+
|
|
709
|
+
// Positioning
|
|
710
|
+
position?:
|
|
711
|
+
| "bottom-right"
|
|
712
|
+
| "bottom-left"
|
|
713
|
+
| "bottom-center"
|
|
714
|
+
| "top-right"
|
|
715
|
+
| "top-left"
|
|
716
|
+
| "top-center"
|
|
717
|
+
| "center";
|
|
718
|
+
|
|
719
|
+
// Styling
|
|
720
|
+
style?: StyleObject;
|
|
721
|
+
className?: string;
|
|
722
|
+
avatarStyle?: {
|
|
723
|
+
view: "round" | "rectangle" | "silhouette";
|
|
724
|
+
borderWidth?: string;
|
|
725
|
+
borderColor?: string;
|
|
726
|
+
borderStyle?: string;
|
|
727
|
+
};
|
|
728
|
+
|
|
729
|
+
// Features
|
|
730
|
+
features?: {
|
|
731
|
+
backgroundRemoval?: { enabled: boolean };
|
|
732
|
+
inactiveTimeout?: {
|
|
733
|
+
enabled: boolean;
|
|
734
|
+
duration?: number; // max: 180000ms
|
|
735
|
+
countdown?: number; // max: 60s
|
|
736
|
+
};
|
|
737
|
+
disclaimer?: { enabled: boolean; text?: string };
|
|
738
|
+
showSDKLoader?: { enabled: boolean; bgColor?: string };
|
|
739
|
+
};
|
|
740
|
+
|
|
741
|
+
// Configuration
|
|
742
|
+
debug?: boolean;
|
|
743
|
+
|
|
744
|
+
// Event callbacks
|
|
745
|
+
onReady?: () => void;
|
|
746
|
+
onError?: (error: Error) => void;
|
|
747
|
+
onData?: (data: EventMessage) => void;
|
|
748
|
+
onAvatarReady?: (isReady?: boolean) => void;
|
|
749
|
+
onInactivityStatusChange?: (isInactive: boolean) => void;
|
|
750
|
+
onDestroy?: () => void;
|
|
751
|
+
onFeaturesUpdate?: (features: FeatureConfig) => void;
|
|
752
|
+
}
|
|
753
|
+
```
|
|
754
|
+
|
|
755
|
+
---
|
|
756
|
+
|
|
757
|
+
## 7. Troubleshooting
|
|
758
|
+
|
|
759
|
+
### 7.1 Common Issues
|
|
760
|
+
|
|
761
|
+
**Problem: "Module not found" Error**
|
|
762
|
+
**Solution:**
|
|
763
|
+
|
|
764
|
+
```bash
|
|
765
|
+
# Ensure package is installed
|
|
766
|
+
npm install @touchcastllc/napster-companion-api
|
|
767
|
+
|
|
768
|
+
# Install peer dependencies
|
|
769
|
+
npm install @reduxjs/toolkit
|
|
770
|
+
```
|
|
771
|
+
|
|
772
|
+
**Problem: Invalid or expired token**
|
|
773
|
+
**Solution:**
|
|
774
|
+
|
|
775
|
+
- Generate new token from backend API
|
|
776
|
+
- Ensure token is passed correctly to `init()`
|
|
777
|
+
|
|
778
|
+
**Problem: Avatar appears unstyled**
|
|
779
|
+
**Solution:**
|
|
780
|
+
|
|
781
|
+
See the style import guide from [1.2.1 Installation & Setup](#121-for-esm-build-type-recommended)
|
|
782
|
+
|
|
783
|
+
**Problem:** `mountContainer` element doesn't exist
|
|
784
|
+
**Solution:**
|
|
785
|
+
|
|
786
|
+
```typescript
|
|
787
|
+
// Wait for DOM to load
|
|
788
|
+
document.addEventListener("DOMContentLoaded", async () => {
|
|
789
|
+
const instance = await NapsterCompanionApiSdk.init(token, {
|
|
790
|
+
mountContainer: "#avatar-container", // Ensure element exists
|
|
791
|
+
});
|
|
792
|
+
});
|
|
793
|
+
```
|
|
794
|
+
|
|
795
|
+
### 7.2 Debug Mode
|
|
796
|
+
|
|
797
|
+
Enable debug logging:
|
|
798
|
+
|
|
799
|
+
```typescript
|
|
800
|
+
const instance = await NapsterCompanionApiSdk.init(token, {
|
|
801
|
+
debug: true, // Enable debug logs
|
|
802
|
+
onError: error => console.error("SDK Error:", error),
|
|
803
|
+
onData: data => console.log("SDK Data:", data),
|
|
804
|
+
});
|
|
805
|
+
```
|
|
806
|
+
|
|
807
|
+
### 7.3 Performance Issues
|
|
808
|
+
|
|
809
|
+
#### Large Bundle Size
|
|
810
|
+
|
|
811
|
+
- Use ESM build instead of standalone
|
|
812
|
+
- Import only needed features
|
|
813
|
+
- Enable tree-shaking in bundler
|
|
814
|
+
|
|
815
|
+
#### Memory Leaks
|
|
816
|
+
|
|
817
|
+
```typescript
|
|
818
|
+
// Always cleanup when done
|
|
819
|
+
useEffect(() => {
|
|
820
|
+
return () => {
|
|
821
|
+
instance?.destroy();
|
|
822
|
+
};
|
|
823
|
+
}, [instance]);
|
|
824
|
+
```
|
|
825
|
+
|
|
826
|
+
---
|
|
827
|
+
|
|
828
|
+
## License
|
|
829
|
+
|
|
830
|
+
This project is licensed under the MIT License - see the [LICENSE](/LICENSE) file for details.
|