@solo3li/backend-node 1.0.0 → 1.0.3
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 +69 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +30 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# @solo3li/backend-node
|
|
2
|
+
|
|
3
|
+
The official Node.js Server SDK for the **Voice AI CPaaS**.
|
|
4
|
+
This library allows SaaS backends to securely authenticate with the CPaaS API and generate short-lived LiveKit access tokens for their front-end users.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm install @solo3li/backend-node
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
**Security Warning:** This SDK must **ONLY** be used on your secure backend server. Never expose your `apiKey` in a client-side application (React, Angular, iOS, etc.).
|
|
15
|
+
|
|
16
|
+
### 1. Initialize the Client
|
|
17
|
+
|
|
18
|
+
```typescript
|
|
19
|
+
import { CPaaSClient } from '@solo3li/backend-node';
|
|
20
|
+
|
|
21
|
+
// Initialize the client with your secret API Key from the Dashboard
|
|
22
|
+
const cpaas = new CPaaSClient({
|
|
23
|
+
apiKey: 'sk_1234567890abcdef...', // Your Secret API Key
|
|
24
|
+
baseUrl: 'https://api.yourcpaas.com' // Optional: Custom CPaaS URL
|
|
25
|
+
});
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### 2. Generate a Connection Token
|
|
29
|
+
|
|
30
|
+
When a user on your frontend clicks "Call AI", your frontend should make a REST request to your backend. Your backend then uses this SDK to generate a secure token.
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import express from 'express';
|
|
34
|
+
const app = express();
|
|
35
|
+
|
|
36
|
+
app.post('/api/get-voice-token', async (req, res) => {
|
|
37
|
+
try {
|
|
38
|
+
// Generate a token for the AI Agent
|
|
39
|
+
const connection = await cpaas.createConnectionToken({
|
|
40
|
+
agentId: '550e8400-e29b-41d4-a716-446655440000', // The ID of the Agent from your Dashboard
|
|
41
|
+
participantName: 'Ahmed (Premium User)', // Name of the end-user
|
|
42
|
+
metadata: {
|
|
43
|
+
'StoreName': 'Noon E-commerce', // Dynamic context to inject into the AI's prompt
|
|
44
|
+
'Language': 'Arabic'
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// Send the token and URL back to your Frontend (React/iOS)
|
|
49
|
+
res.json({
|
|
50
|
+
token: connection.token,
|
|
51
|
+
livekitUrl: connection.livekitUrl
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
} catch (error) {
|
|
55
|
+
console.error('Failed to generate CPaaS token:', error);
|
|
56
|
+
res.status(500).json({ error: 'Failed to connect to AI' });
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Response Object
|
|
62
|
+
|
|
63
|
+
The `createConnectionToken` method returns an object containing:
|
|
64
|
+
- `token`: A secure JWT access token valid for this specific session.
|
|
65
|
+
- `roomName`: The unique ID of the generated voice room.
|
|
66
|
+
- `livekitUrl`: The WebSocket URL that the Client SDK should connect to.
|
|
67
|
+
|
|
68
|
+
## License
|
|
69
|
+
MIT
|
package/dist/index.d.ts
CHANGED
|
@@ -16,4 +16,11 @@ export declare class CPaaSClient {
|
|
|
16
16
|
private client;
|
|
17
17
|
constructor(config: CPaaSConfig);
|
|
18
18
|
createConnectionToken(request: CreateTokenRequest): Promise<CreateTokenResponse>;
|
|
19
|
+
createTransferToken(roomId: string, agentName: string): Promise<{
|
|
20
|
+
token: string;
|
|
21
|
+
livekitUrl: string;
|
|
22
|
+
}>;
|
|
23
|
+
initiateSipTransfer(roomId: string, sipUri: string): Promise<{
|
|
24
|
+
success: boolean;
|
|
25
|
+
}>;
|
|
19
26
|
}
|
package/dist/index.js
CHANGED
|
@@ -32,5 +32,35 @@ class CPaaSClient {
|
|
|
32
32
|
throw error;
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
|
+
async createTransferToken(roomId, agentName) {
|
|
36
|
+
try {
|
|
37
|
+
const response = await this.client.post('/api/Connection/transfer-token', {
|
|
38
|
+
roomId,
|
|
39
|
+
participantName: agentName
|
|
40
|
+
});
|
|
41
|
+
return response.data;
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
if (error.response) {
|
|
45
|
+
throw new Error(`CPaaS API Error: ${error.response.status} - ${JSON.stringify(error.response.data)}`);
|
|
46
|
+
}
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
async initiateSipTransfer(roomId, sipUri) {
|
|
51
|
+
try {
|
|
52
|
+
const response = await this.client.post('/api/Connection/sip-transfer', {
|
|
53
|
+
roomId,
|
|
54
|
+
sipUri
|
|
55
|
+
});
|
|
56
|
+
return response.data;
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
if (error.response) {
|
|
60
|
+
throw new Error(`CPaaS API Error: ${error.response.status} - ${JSON.stringify(error.response.data)}`);
|
|
61
|
+
}
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
35
65
|
}
|
|
36
66
|
exports.CPaaSClient = CPaaSClient;
|