@trillet-ai/web-sdk 1.2.16 → 1.3.1
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 +134 -117
- package/dist/index.d.ts +22 -3
- package/dist/index.js +143 -75
- package/package.json +8 -3
package/README.md
CHANGED
|
@@ -1,167 +1,184 @@
|
|
|
1
|
-
# Trillet Web SDK
|
|
1
|
+
# Trillet AI Web Calls SDK
|
|
2
2
|
|
|
3
|
-
The Trillet Web SDK enables
|
|
3
|
+
The Trillet AI Web Calls SDK enables seamless integration of AI-powered voice and text interactions into web applications. This SDK provides a simple interface to connect with Trillet AI agents for real-time communication.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Features
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
- 🎙️ Real-time voice interactions with AI agents
|
|
8
|
+
- 💬 Text-based conversations
|
|
9
|
+
- 🔒 Secure authentication options
|
|
10
|
+
- 📊 Audio visualization capabilities
|
|
11
|
+
- 🎯 Event-driven architecture
|
|
12
|
+
- 🔄 Real-time transcription
|
|
13
|
+
- 🌐 Browser compatibility checks
|
|
10
14
|
|
|
11
|
-
##
|
|
15
|
+
## Installation
|
|
12
16
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
For production environments, we strongly recommend using server-side authentication to keep your API keys secure. Here's how to implement it:
|
|
18
|
-
|
|
19
|
-
1. First, generate a token on your backend server:
|
|
20
|
-
|
|
21
|
-
```javascript
|
|
22
|
-
// Backend (Node.js example)
|
|
23
|
-
app.post('/get-trillet-token', async (req, res) => {
|
|
24
|
-
try {
|
|
25
|
-
const response = await fetch('https://api.trillet.ai/v1/api/calls/sdk/auth', {
|
|
26
|
-
method: 'POST',
|
|
27
|
-
headers: {
|
|
28
|
-
'Content-Type': 'application/json',
|
|
29
|
-
},
|
|
30
|
-
body: JSON.stringify({
|
|
31
|
-
api_key: process.env.TRILLET_API_KEY,
|
|
32
|
-
workspace_id: process.env.TRILLET_WORKSPACE_ID
|
|
33
|
-
})
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
const { sdk_token } = await response.json();
|
|
37
|
-
res.json({ token: sdk_token });
|
|
38
|
-
} catch (error) {
|
|
39
|
-
res.status(500).json({ error: 'Failed to generate token' });
|
|
40
|
-
}
|
|
41
|
-
});
|
|
17
|
+
```bash
|
|
18
|
+
npm install @trillet/webcalls-sdk
|
|
19
|
+
# or
|
|
20
|
+
yarn add @trillet/webcalls-sdk
|
|
42
21
|
```
|
|
43
22
|
|
|
44
|
-
|
|
23
|
+
## Quick Start
|
|
45
24
|
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
const response = await fetch('/get-trillet-token');
|
|
49
|
-
const { token } = await response.json();
|
|
25
|
+
```typescript
|
|
26
|
+
import { TrilletAgent } from '@trillet/webcalls-sdk';
|
|
50
27
|
|
|
28
|
+
// Initialize the agent with API Key (Standard Method)
|
|
51
29
|
const agent = new TrilletAgent({
|
|
52
|
-
|
|
53
|
-
|
|
30
|
+
apiKey: 'your-api-key',
|
|
31
|
+
workspaceId: 'your-workspace-id',
|
|
32
|
+
agentId: 'your-agent-id',
|
|
33
|
+
|
|
34
|
+
// Optional parameters
|
|
35
|
+
variables: {
|
|
36
|
+
customVar1: 'value1',
|
|
37
|
+
customVar2: 'value2'
|
|
38
|
+
},
|
|
39
|
+
callbackUrl: 'https://your-callback-url.com'
|
|
54
40
|
});
|
|
55
41
|
|
|
42
|
+
// Start a regular call
|
|
56
43
|
await agent.startCall();
|
|
57
|
-
```
|
|
58
44
|
|
|
59
|
-
|
|
45
|
+
// Or start a public call
|
|
46
|
+
await agent.startPublicCall();
|
|
60
47
|
|
|
61
|
-
|
|
48
|
+
// Listen for events
|
|
49
|
+
agent.on('connected', (details) => {
|
|
50
|
+
console.log('Connected to call:', details);
|
|
51
|
+
});
|
|
62
52
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
apiKey: 'your-api-key', // Not recommended in production
|
|
66
|
-
workspaceId: 'your-workspace-id', // Not recommended in production
|
|
67
|
-
agentId: 'your-agent-id'
|
|
53
|
+
agent.on('transcriptionReceived', (transcript) => {
|
|
54
|
+
console.log('New transcript:', transcript);
|
|
68
55
|
});
|
|
69
56
|
|
|
70
|
-
|
|
57
|
+
// Control the call
|
|
58
|
+
agent.toggleMicrophone(true); // Enable/disable microphone
|
|
59
|
+
agent.endCall(); // End the call
|
|
71
60
|
```
|
|
72
61
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
## Basic Usage
|
|
62
|
+
## Authentication
|
|
76
63
|
|
|
77
|
-
|
|
78
|
-
|
|
64
|
+
The SDK supports two authentication methods:
|
|
65
|
+
|
|
66
|
+
1. **API Key Authentication (Standard Method)**
|
|
67
|
+
```typescript
|
|
68
|
+
const agent = new TrilletAgent({
|
|
69
|
+
apiKey: 'your-api-key',
|
|
70
|
+
workspaceId: 'your-workspace-id',
|
|
71
|
+
agentId: 'your-agent-id'
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
2. **Public Access**
|
|
76
|
+
- For public-facing applications
|
|
77
|
+
- Uses `startPublicCall()` method
|
|
78
|
+
```typescript
|
|
79
|
+
const agent = new TrilletAgent({
|
|
80
|
+
workspaceId: 'your-workspace-id',
|
|
81
|
+
agentId: 'your-agent-id'
|
|
82
|
+
});
|
|
83
|
+
await agent.startPublicCall();
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Call Types
|
|
87
|
+
|
|
88
|
+
The SDK supports two types of calls:
|
|
89
|
+
|
|
90
|
+
1. **Regular Call** - Using `startCall()`
|
|
91
|
+
- Requires full authentication with API Key
|
|
92
|
+
- Suitable for authenticated users
|
|
93
|
+
|
|
94
|
+
2. **Public Call** - Using `startPublicCall()`
|
|
95
|
+
- Requires workspace ID and agent ID
|
|
96
|
+
- Suitable for public-facing applications
|
|
97
|
+
- No API key needed in client-side code
|
|
98
|
+
- Example:
|
|
99
|
+
```typescript
|
|
100
|
+
const agent = new TrilletAgent({
|
|
101
|
+
workspaceId: 'your-workspace-id',
|
|
102
|
+
agentId: 'your-agent-id' // Required for both public and regular calls
|
|
103
|
+
});
|
|
104
|
+
await agent.startPublicCall();
|
|
105
|
+
```
|
|
79
106
|
|
|
80
|
-
|
|
81
|
-
const agent = new TrilletAgent({
|
|
82
|
-
connectionToken: 'token-from-your-backend',
|
|
83
|
-
agentId: 'your-agent-id',
|
|
84
|
-
variables: {
|
|
85
|
-
// Optional variables to customize agent behavior
|
|
86
|
-
customerName: 'John Doe',
|
|
87
|
-
context: 'support'
|
|
88
|
-
},
|
|
89
|
-
callbackUrl: 'https://your-server.com/webhooks/calls' // Optional webhook URL for call events
|
|
90
|
-
});
|
|
107
|
+
## Events
|
|
91
108
|
|
|
92
|
-
|
|
93
|
-
await agent.startCall();
|
|
109
|
+
The SDK emits various events that you can listen to:
|
|
94
110
|
|
|
95
|
-
|
|
96
|
-
agent.on('connected', () => {
|
|
97
|
-
|
|
111
|
+
```typescript
|
|
112
|
+
agent.on('connected', (details) => {
|
|
113
|
+
// Called when successfully connected to a call
|
|
98
114
|
});
|
|
99
115
|
|
|
100
116
|
agent.on('disconnected', () => {
|
|
101
|
-
|
|
117
|
+
// Called when the call ends
|
|
102
118
|
});
|
|
103
119
|
|
|
104
120
|
agent.on('error', (error) => {
|
|
105
|
-
|
|
121
|
+
// Called when an error occurs
|
|
106
122
|
});
|
|
107
123
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
## API Endpoints
|
|
113
|
-
|
|
114
|
-
The SDK uses the following endpoints:
|
|
115
|
-
|
|
116
|
-
- Authentication: `https://api.trillet.ai/v1/api/calls/sdk/auth`
|
|
117
|
-
- Connection: `https://api.trillet.ai/v1/api/calls/web/connect`
|
|
118
|
-
|
|
119
|
-
## Events
|
|
120
|
-
|
|
121
|
-
The SDK emits various events you can listen to:
|
|
122
|
-
|
|
123
|
-
- `connected`: Fired when successfully connected to the agent
|
|
124
|
-
- `disconnected`: Fired when disconnected from the agent
|
|
125
|
-
- `error`: Fired when an error occurs
|
|
126
|
-
- `status`: Provides status updates about the call
|
|
127
|
-
- `metadata`: Provides additional data about the call
|
|
128
|
-
- `assistantStartedSpeaking`: Fired when the AI agent starts speaking
|
|
129
|
-
- `assistantStoppedSpeaking`: Fired when the AI agent stops speaking
|
|
130
|
-
- `audioData`: Provides raw audio data (if enabled)
|
|
124
|
+
agent.on('transcriptionReceived', (segments, participant) => {
|
|
125
|
+
// Called when new transcription is available
|
|
126
|
+
});
|
|
131
127
|
|
|
132
|
-
|
|
128
|
+
agent.on('assistantStartedSpeaking', () => {
|
|
129
|
+
// Called when the AI agent starts speaking
|
|
130
|
+
});
|
|
133
131
|
|
|
134
|
-
|
|
132
|
+
agent.on('assistantStoppedSpeaking', () => {
|
|
133
|
+
// Called when the AI agent stops speaking
|
|
134
|
+
});
|
|
135
135
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
connectionToken: 'your-token',
|
|
139
|
-
agentId: 'your-agent-id',
|
|
140
|
-
variables: {
|
|
141
|
-
// Custom variables for your agent
|
|
142
|
-
},
|
|
143
|
-
callbackUrl: 'https://your-webhook-url.com',
|
|
136
|
+
agent.on('audioData', (data) => {
|
|
137
|
+
// Raw audio data for visualization
|
|
144
138
|
});
|
|
139
|
+
```
|
|
145
140
|
|
|
146
|
-
|
|
141
|
+
## Voice Mode Features
|
|
147
142
|
|
|
148
|
-
|
|
149
|
-
agent.toggleMicrophone(false); // Mute
|
|
150
|
-
agent.toggleMicrophone(true); // Unmute
|
|
143
|
+
When using voice mode, the SDK provides additional audio-related features:
|
|
151
144
|
|
|
152
|
-
|
|
145
|
+
```typescript
|
|
146
|
+
// Check if the assistant is currently speaking
|
|
153
147
|
const isSpeaking = agent.isAssistantSpeaking;
|
|
148
|
+
|
|
149
|
+
// Access transcripts
|
|
150
|
+
const allTranscripts = agent.getTranscripts();
|
|
151
|
+
const currentTranscript = agent.getCurrentTranscript();
|
|
154
152
|
```
|
|
155
153
|
|
|
154
|
+
## Requirements
|
|
155
|
+
|
|
156
|
+
- Modern web browser with secure context (HTTPS or localhost)
|
|
157
|
+
- WebRTC support
|
|
158
|
+
- Microphone access (for voice mode)
|
|
159
|
+
|
|
156
160
|
## Browser Support
|
|
157
161
|
|
|
158
|
-
The SDK
|
|
162
|
+
The SDK is compatible with modern browsers that support WebRTC:
|
|
159
163
|
- Chrome (recommended)
|
|
160
164
|
- Firefox
|
|
161
165
|
- Safari
|
|
162
166
|
- Edge
|
|
163
167
|
|
|
164
|
-
##
|
|
168
|
+
## Error Handling
|
|
169
|
+
|
|
170
|
+
The SDK includes comprehensive error handling:
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
agent.on('error', (error) => {
|
|
174
|
+
console.error('Trillet SDK Error:', error);
|
|
175
|
+
});
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## License
|
|
179
|
+
|
|
180
|
+
[License details here]
|
|
181
|
+
|
|
182
|
+
## Support
|
|
165
183
|
|
|
166
|
-
|
|
167
|
-
- Support: [support@trillet.ai](mailto:support@trillet.ai)
|
|
184
|
+
For support, please contact [support contact information].
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { EventEmitter } from "eventemitter3";
|
|
2
2
|
export interface TrilletAgentConfig {
|
|
3
|
-
connectionToken?: string;
|
|
4
3
|
apiKey?: string;
|
|
5
4
|
workspaceId?: string;
|
|
6
5
|
agentId: string;
|
|
7
6
|
variables?: Record<string, any>;
|
|
8
7
|
callbackUrl?: string;
|
|
9
|
-
mode?:
|
|
8
|
+
mode?: "voice" | "text";
|
|
10
9
|
}
|
|
11
10
|
export interface TrilletCallConfig {
|
|
12
11
|
token: string;
|
|
@@ -20,15 +19,35 @@ export interface TrilletCallConfig {
|
|
|
20
19
|
enableRawAudio?: boolean;
|
|
21
20
|
};
|
|
22
21
|
}
|
|
22
|
+
interface ToolUsed {
|
|
23
|
+
name: string;
|
|
24
|
+
args?: any;
|
|
25
|
+
status?: "pending" | "success" | "error";
|
|
26
|
+
}
|
|
27
|
+
interface Transcript {
|
|
28
|
+
role: "user" | "assistant";
|
|
29
|
+
text: string;
|
|
30
|
+
isFinal: boolean;
|
|
31
|
+
timestamp: Date;
|
|
32
|
+
participantId?: string;
|
|
33
|
+
toolUsed?: ToolUsed;
|
|
34
|
+
}
|
|
23
35
|
export declare class TrilletAgent extends EventEmitter {
|
|
24
36
|
private sdk;
|
|
25
37
|
private config;
|
|
26
38
|
private sdkToken;
|
|
39
|
+
private transcripts;
|
|
40
|
+
private currentTranscript;
|
|
27
41
|
constructor(config: TrilletAgentConfig);
|
|
28
42
|
private setupEventForwarding;
|
|
29
|
-
private authenticate;
|
|
30
43
|
startCall(): Promise<void>;
|
|
44
|
+
startPublicCall(): Promise<void>;
|
|
31
45
|
endCall(): void;
|
|
32
46
|
toggleMicrophone(enabled: boolean): void;
|
|
33
47
|
get isAssistantSpeaking(): boolean;
|
|
48
|
+
getTranscripts(): Transcript[];
|
|
49
|
+
getCurrentTranscript(): {
|
|
50
|
+
[key: string]: string;
|
|
51
|
+
};
|
|
34
52
|
}
|
|
53
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,109 +1,114 @@
|
|
|
1
1
|
import { EventEmitter } from "eventemitter3";
|
|
2
2
|
import { RemoteAudioTrack, Room, RoomEvent, Track, createAudioAnalyser, } from "livekit-client";
|
|
3
3
|
const TRILLET_API_URL = {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
CONNECT: "https://api.trillet.ai/v1/api/calls/web/connect",
|
|
5
|
+
PUBLIC_CONNECT: "https://api.trillet.ai/v1/api/web-call/public",
|
|
6
6
|
};
|
|
7
7
|
const decoder = new TextDecoder();
|
|
8
8
|
// Browser compatibility check
|
|
9
|
-
const isBrowser = typeof window !==
|
|
9
|
+
const isBrowser = typeof window !== "undefined";
|
|
10
10
|
const isSecureContext = isBrowser && window.isSecureContext;
|
|
11
11
|
export class TrilletAgent extends EventEmitter {
|
|
12
12
|
constructor(config) {
|
|
13
13
|
super();
|
|
14
14
|
this.sdkToken = null;
|
|
15
|
+
this.transcripts = [];
|
|
16
|
+
this.currentTranscript = {};
|
|
15
17
|
// Validate environment
|
|
16
18
|
if (!isBrowser) {
|
|
17
|
-
throw new Error(
|
|
19
|
+
throw new Error("Trillet SDK must be used in a browser environment");
|
|
18
20
|
}
|
|
19
21
|
if (!isSecureContext) {
|
|
20
|
-
throw new Error(
|
|
21
|
-
}
|
|
22
|
-
// Validate config
|
|
23
|
-
if (!config.connectionToken && (!config.apiKey || !config.workspaceId)) {
|
|
24
|
-
throw new Error('Either connectionToken OR (apiKey AND workspaceId) must be provided');
|
|
22
|
+
throw new Error("Trillet SDK requires a secure context (HTTPS or localhost)");
|
|
25
23
|
}
|
|
26
24
|
if (!config.agentId) {
|
|
27
|
-
throw new Error(
|
|
25
|
+
throw new Error("agentId is required");
|
|
28
26
|
}
|
|
29
27
|
this.config = {
|
|
30
28
|
...config,
|
|
31
|
-
mode: config.mode ||
|
|
29
|
+
mode: config.mode || "voice", // Default to voice mode
|
|
32
30
|
};
|
|
33
31
|
this.sdk = new TrilletWebSDK();
|
|
34
32
|
this.setupEventForwarding();
|
|
35
33
|
}
|
|
36
34
|
setupEventForwarding() {
|
|
35
|
+
var _a;
|
|
37
36
|
// Forward all SDK events to agent listeners
|
|
38
|
-
this.sdk.on(
|
|
39
|
-
this.sdk.on(
|
|
40
|
-
this.sdk.on(
|
|
41
|
-
this.sdk.on(
|
|
42
|
-
this.sdk.on(
|
|
43
|
-
this.sdk.on(
|
|
44
|
-
this.sdk.on(
|
|
45
|
-
this.sdk.on(
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
37
|
+
this.sdk.on("connected", () => this.emit("connected"));
|
|
38
|
+
this.sdk.on("disconnected", () => this.emit("disconnected"));
|
|
39
|
+
this.sdk.on("error", (error) => this.emit("error", error));
|
|
40
|
+
this.sdk.on("status", (status) => this.emit("status", status));
|
|
41
|
+
this.sdk.on("metadata", (metadata) => this.emit("metadata", metadata));
|
|
42
|
+
this.sdk.on("assistantStartedSpeaking", () => this.emit("assistantStartedSpeaking"));
|
|
43
|
+
this.sdk.on("assistantStoppedSpeaking", () => this.emit("assistantStoppedSpeaking"));
|
|
44
|
+
this.sdk.on("audioData", (data) => this.emit("audioData", data));
|
|
45
|
+
// Forward Room events to agent listeners
|
|
46
|
+
(_a = this.sdk
|
|
47
|
+
.getRoom()) === null || _a === void 0 ? void 0 : _a.on(RoomEvent.TranscriptionReceived, (segments, participant) => {
|
|
48
|
+
const room = this.sdk.getRoom();
|
|
49
|
+
if (!room)
|
|
50
|
+
return;
|
|
51
|
+
const participantId = (participant === null || participant === void 0 ? void 0 : participant.identity) || room.localParticipant.identity;
|
|
52
|
+
const isAgent = participantId.startsWith("agent-") ||
|
|
53
|
+
participantId.startsWith("web-agent");
|
|
54
|
+
segments.forEach((segment) => {
|
|
55
|
+
var _a;
|
|
56
|
+
if (segment.final) {
|
|
57
|
+
delete this.currentTranscript[participantId];
|
|
58
|
+
const text = segment.text.trim();
|
|
59
|
+
// Check for duplicates
|
|
60
|
+
const lastMessage = this.transcripts[this.transcripts.length - 1];
|
|
61
|
+
if (lastMessage &&
|
|
62
|
+
lastMessage.text === text &&
|
|
63
|
+
lastMessage.participantId === participantId &&
|
|
64
|
+
Date.now() - lastMessage.timestamp.getTime() < 1000) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
this.transcripts.push({
|
|
68
|
+
role: isAgent ? "assistant" : "user",
|
|
69
|
+
text,
|
|
70
|
+
isFinal: true,
|
|
71
|
+
timestamp: new Date(),
|
|
72
|
+
participantId,
|
|
73
|
+
toolUsed: text.includes("Using tool:")
|
|
74
|
+
? {
|
|
75
|
+
name: text.split("Using tool:")[1].split(" ")[1],
|
|
76
|
+
args: (_a = text.split("Arguments:")[1]) === null || _a === void 0 ? void 0 : _a.trim(),
|
|
77
|
+
status: "pending",
|
|
78
|
+
}
|
|
79
|
+
: undefined,
|
|
80
|
+
});
|
|
69
81
|
}
|
|
70
|
-
|
|
71
|
-
|
|
82
|
+
else {
|
|
83
|
+
this.currentTranscript[participantId] = segment.text;
|
|
72
84
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
return data.sdk_token;
|
|
77
|
-
}
|
|
78
|
-
catch (error) {
|
|
79
|
-
console.error('Trillet SDK Authentication Error:', error);
|
|
80
|
-
throw error;
|
|
81
|
-
}
|
|
85
|
+
});
|
|
86
|
+
this.emit(RoomEvent.TranscriptionReceived, segments, participant);
|
|
87
|
+
});
|
|
82
88
|
}
|
|
83
89
|
async startCall() {
|
|
84
90
|
try {
|
|
85
|
-
//
|
|
86
|
-
const token = await this.authenticate();
|
|
87
|
-
// 2. Initialize call with LiveKit token
|
|
91
|
+
// 2. Initialize call with API KEY
|
|
88
92
|
const response = await fetch(TRILLET_API_URL.CONNECT, {
|
|
89
|
-
method:
|
|
93
|
+
method: "POST",
|
|
90
94
|
headers: {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
95
|
+
"Content-Type": "application/json",
|
|
96
|
+
Accept: "application/json",
|
|
97
|
+
"X-API_KEY": this.config.apiKey,
|
|
94
98
|
},
|
|
95
99
|
body: JSON.stringify({
|
|
96
100
|
agentId: this.config.agentId,
|
|
97
101
|
mode: this.config.mode,
|
|
98
102
|
dynamic_variables: this.config.variables || {},
|
|
99
|
-
callback_url: this.config.callbackUrl
|
|
100
|
-
})
|
|
103
|
+
callback_url: this.config.callbackUrl,
|
|
104
|
+
}),
|
|
101
105
|
});
|
|
102
106
|
if (!response.ok) {
|
|
103
107
|
let errorMessage;
|
|
104
108
|
try {
|
|
105
109
|
const errorData = await response.json();
|
|
106
|
-
errorMessage =
|
|
110
|
+
errorMessage =
|
|
111
|
+
errorData.message || errorData.error || "Unknown error";
|
|
107
112
|
}
|
|
108
113
|
catch (_a) {
|
|
109
114
|
errorMessage = await response.text();
|
|
@@ -112,36 +117,90 @@ export class TrilletAgent extends EventEmitter {
|
|
|
112
117
|
}
|
|
113
118
|
const data = await response.json();
|
|
114
119
|
if (!data.token || !data.roomName) {
|
|
115
|
-
throw new Error(
|
|
120
|
+
throw new Error("Invalid response from server: missing token or room name");
|
|
116
121
|
}
|
|
117
122
|
// Initialize call with LiveKit token
|
|
118
123
|
await this.sdk.initializeCall({
|
|
119
124
|
token: data.token,
|
|
120
|
-
wsUrl: process.env.LIVEKIT_WS_URL ||
|
|
125
|
+
wsUrl: process.env.LIVEKIT_WS_URL ||
|
|
126
|
+
"wss://trillet-ai-xdx0dw5r.livekit.cloud",
|
|
121
127
|
audioSettings: {
|
|
122
|
-
sampleRate: 48000
|
|
128
|
+
sampleRate: 48000,
|
|
123
129
|
},
|
|
124
130
|
features: {
|
|
125
|
-
enableRawAudio: this.config.mode ===
|
|
126
|
-
}
|
|
131
|
+
enableRawAudio: this.config.mode === "voice",
|
|
132
|
+
},
|
|
127
133
|
});
|
|
128
134
|
// Enable audio playback for voice mode
|
|
129
|
-
if (this.config.mode ===
|
|
135
|
+
if (this.config.mode === "voice") {
|
|
130
136
|
await this.sdk.enableAudioPlayback();
|
|
131
137
|
}
|
|
132
138
|
// Emit additional connection details
|
|
133
|
-
this.emit(
|
|
139
|
+
this.emit("connected", {
|
|
134
140
|
callId: data.callId,
|
|
135
141
|
roomName: data.roomName,
|
|
136
|
-
agent: data.agent
|
|
142
|
+
agent: data.agent,
|
|
137
143
|
});
|
|
138
144
|
}
|
|
139
145
|
catch (error) {
|
|
140
|
-
console.error(
|
|
141
|
-
this.emit(
|
|
146
|
+
console.error("Trillet SDK Error:", error);
|
|
147
|
+
this.emit("error", error instanceof Error ? error : new Error(String(error)));
|
|
142
148
|
throw error;
|
|
143
149
|
}
|
|
144
150
|
}
|
|
151
|
+
async startPublicCall() {
|
|
152
|
+
// 2. Initialize public call
|
|
153
|
+
const response = await fetch(TRILLET_API_URL.PUBLIC_CONNECT, {
|
|
154
|
+
method: "POST",
|
|
155
|
+
headers: {
|
|
156
|
+
"Content-Type": "application/json",
|
|
157
|
+
Accept: "application/json",
|
|
158
|
+
'x-workspace-id': this.config.workspaceId
|
|
159
|
+
},
|
|
160
|
+
body: JSON.stringify({
|
|
161
|
+
call_agent_id: this.config.agentId,
|
|
162
|
+
mode: this.config.mode,
|
|
163
|
+
dynamic_variables: this.config.variables || {},
|
|
164
|
+
callback_url: this.config.callbackUrl,
|
|
165
|
+
}),
|
|
166
|
+
});
|
|
167
|
+
if (!response.ok) {
|
|
168
|
+
let errorMessage;
|
|
169
|
+
try {
|
|
170
|
+
const errorData = await response.json();
|
|
171
|
+
errorMessage = errorData.message || errorData.error || "Unknown error";
|
|
172
|
+
}
|
|
173
|
+
catch (_a) {
|
|
174
|
+
errorMessage = await response.text();
|
|
175
|
+
}
|
|
176
|
+
throw new Error(`API Error: ${errorMessage}`);
|
|
177
|
+
}
|
|
178
|
+
const data = await response.json();
|
|
179
|
+
if (!data.token || !data.roomName) {
|
|
180
|
+
throw new Error("Invalid response from server: missing token or room name");
|
|
181
|
+
}
|
|
182
|
+
// Initialize call with LiveKit token
|
|
183
|
+
await this.sdk.initializeCall({
|
|
184
|
+
token: data.token,
|
|
185
|
+
wsUrl: process.env.LIVEKIT_WS_URL || "wss://trillet-ai-xdx0dw5r.livekit.cloud",
|
|
186
|
+
audioSettings: {
|
|
187
|
+
sampleRate: 48000,
|
|
188
|
+
},
|
|
189
|
+
features: {
|
|
190
|
+
enableRawAudio: this.config.mode === "voice",
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
// Enable audio playback for voice mode
|
|
194
|
+
if (this.config.mode === "voice") {
|
|
195
|
+
await this.sdk.enableAudioPlayback();
|
|
196
|
+
}
|
|
197
|
+
// Emit additional connection details
|
|
198
|
+
this.emit("connected", {
|
|
199
|
+
callId: data.callId,
|
|
200
|
+
roomName: data.roomName,
|
|
201
|
+
agent: data.agent,
|
|
202
|
+
});
|
|
203
|
+
}
|
|
145
204
|
endCall() {
|
|
146
205
|
this.sdk.endCall();
|
|
147
206
|
}
|
|
@@ -151,6 +210,12 @@ export class TrilletAgent extends EventEmitter {
|
|
|
151
210
|
get isAssistantSpeaking() {
|
|
152
211
|
return this.sdk.isAssistantSpeaking;
|
|
153
212
|
}
|
|
213
|
+
getTranscripts() {
|
|
214
|
+
return this.transcripts;
|
|
215
|
+
}
|
|
216
|
+
getCurrentTranscript() {
|
|
217
|
+
return this.currentTranscript;
|
|
218
|
+
}
|
|
154
219
|
}
|
|
155
220
|
// Internal SDK class
|
|
156
221
|
class TrilletWebSDK extends EventEmitter {
|
|
@@ -184,7 +249,7 @@ class TrilletWebSDK extends EventEmitter {
|
|
|
184
249
|
this.emit("connected");
|
|
185
250
|
}
|
|
186
251
|
catch (error) {
|
|
187
|
-
const errorMessage = error instanceof Error ? error.message :
|
|
252
|
+
const errorMessage = error instanceof Error ? error.message : "Failed to initialize call";
|
|
188
253
|
this.emit("error", errorMessage);
|
|
189
254
|
console.error("Call initialization failed:", error);
|
|
190
255
|
this.endCall();
|
|
@@ -251,7 +316,7 @@ class TrilletWebSDK extends EventEmitter {
|
|
|
251
316
|
this.audioAnalyzer = {
|
|
252
317
|
calculateVolume: analyzer.calculateVolume,
|
|
253
318
|
analyser: analyzer.analyser,
|
|
254
|
-
cleanup: analyzer.cleanup
|
|
319
|
+
cleanup: analyzer.cleanup,
|
|
255
320
|
};
|
|
256
321
|
this.audioFrameCapture = requestAnimationFrame(() => this.captureAudio());
|
|
257
322
|
}
|
|
@@ -289,4 +354,7 @@ class TrilletWebSDK extends EventEmitter {
|
|
|
289
354
|
break;
|
|
290
355
|
}
|
|
291
356
|
}
|
|
357
|
+
getRoom() {
|
|
358
|
+
return this.room;
|
|
359
|
+
}
|
|
292
360
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trillet-ai/web-sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "Trillet Web SDK for real-time audio communication with AI agents",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -13,7 +13,8 @@
|
|
|
13
13
|
"build": "npm run clean && tsc",
|
|
14
14
|
"dev": "tsc --watch",
|
|
15
15
|
"lint": "eslint src/**/*.{ts,tsx}",
|
|
16
|
-
"prepare": "npm run build"
|
|
16
|
+
"prepare": "npm run build",
|
|
17
|
+
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
|
|
17
18
|
},
|
|
18
19
|
"peerDependencies": {
|
|
19
20
|
"react": ">=16.8.0",
|
|
@@ -31,17 +32,21 @@
|
|
|
31
32
|
"author": "Trillet AI",
|
|
32
33
|
"license": "MIT",
|
|
33
34
|
"dependencies": {
|
|
35
|
+
"@trillet-ai/web-sdk": "file:",
|
|
34
36
|
"eventemitter3": "^5.0.1",
|
|
35
|
-
"livekit-client": "^
|
|
37
|
+
"livekit-client": "^2.8.1"
|
|
36
38
|
},
|
|
37
39
|
"devDependencies": {
|
|
40
|
+
"@types/jest": "^29.5.12",
|
|
38
41
|
"@types/node": "^20.11.0",
|
|
39
42
|
"@types/react": "^18.2.0",
|
|
40
43
|
"@types/react-dom": "^18.2.0",
|
|
41
44
|
"@typescript-eslint/eslint-plugin": "^6.18.1",
|
|
42
45
|
"@typescript-eslint/parser": "^6.18.1",
|
|
43
46
|
"eslint": "^8.56.0",
|
|
47
|
+
"jest": "^29.7.0",
|
|
44
48
|
"rimraf": "^5.0.10",
|
|
49
|
+
"ts-jest": "^29.1.2",
|
|
45
50
|
"typescript": "^5.3.3"
|
|
46
51
|
},
|
|
47
52
|
"files": [
|