ttp-agent-sdk 2.0.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.
@@ -0,0 +1,190 @@
1
+ /**
2
+ * VoiceButton - Vanilla JavaScript voice button
3
+ */
4
+ import VoiceSDK from '../core/VoiceSDK.js';
5
+
6
+ export default class VoiceButton {
7
+ constructor(options = {}) {
8
+ this.options = {
9
+ websocketUrl: options.websocketUrl || 'wss://speech.bidme.co.il/ws/conv',
10
+ agentId: options.agentId, // Optional - for direct agent access (unsecured method)
11
+ voice: options.voice || 'default',
12
+ language: options.language || 'en',
13
+ container: options.container || document.body,
14
+ buttonText: options.buttonText || 'Start Listening',
15
+ buttonClass: options.buttonClass || 'voice-button',
16
+ ...options
17
+ };
18
+
19
+ this.isConnected = false;
20
+ this.isRecording = false;
21
+ this.isPlaying = false;
22
+
23
+ this.voiceSDK = new VoiceSDK({
24
+ websocketUrl: this.options.websocketUrl,
25
+ agentId: this.options.agentId, // Pass through agentId if provided
26
+ voice: this.options.voice,
27
+ language: this.options.language
28
+ });
29
+
30
+ this.setupEventListeners();
31
+ this.createButton();
32
+ this.connect();
33
+ }
34
+
35
+ /**
36
+ * Setup event listeners
37
+ */
38
+ setupEventListeners() {
39
+ this.voiceSDK.on('connected', () => {
40
+ this.isConnected = true;
41
+ this.updateButton();
42
+ this.options.onConnected?.();
43
+ });
44
+
45
+ this.voiceSDK.on('disconnected', () => {
46
+ this.isConnected = false;
47
+ this.updateButton();
48
+ this.options.onDisconnected?.();
49
+ });
50
+
51
+ this.voiceSDK.on('recordingStarted', () => {
52
+ this.isRecording = true;
53
+ this.updateButton();
54
+ this.options.onRecordingStarted?.();
55
+ });
56
+
57
+ this.voiceSDK.on('recordingStopped', () => {
58
+ this.isRecording = false;
59
+ this.updateButton();
60
+ this.options.onRecordingStopped?.();
61
+ });
62
+
63
+ this.voiceSDK.on('playbackStarted', () => {
64
+ this.isPlaying = true;
65
+ this.options.onPlaybackStarted?.();
66
+ });
67
+
68
+ this.voiceSDK.on('playbackStopped', () => {
69
+ this.isPlaying = false;
70
+ this.options.onPlaybackStopped?.();
71
+ });
72
+
73
+ this.voiceSDK.on('error', (error) => {
74
+ this.options.onError?.(error);
75
+ });
76
+
77
+ this.voiceSDK.on('message', (message) => {
78
+ this.options.onMessage?.(message);
79
+ });
80
+
81
+ this.voiceSDK.on('bargeIn', (message) => {
82
+ this.options.onBargeIn?.(message);
83
+ });
84
+
85
+ this.voiceSDK.on('stopPlaying', (message) => {
86
+ this.options.onStopPlaying?.(message);
87
+ });
88
+ }
89
+
90
+ /**
91
+ * Create the button element
92
+ */
93
+ createButton() {
94
+ this.button = document.createElement('button');
95
+ this.button.className = this.options.buttonClass;
96
+ this.button.style.cssText = `
97
+ padding: 12px 24px;
98
+ border: none;
99
+ border-radius: 8px;
100
+ background-color: #6c757d;
101
+ color: white;
102
+ cursor: pointer;
103
+ font-size: 16px;
104
+ font-weight: 500;
105
+ transition: all 0.2s ease;
106
+ display: flex;
107
+ align-items: center;
108
+ gap: 8px;
109
+ `;
110
+
111
+ this.button.addEventListener('click', () => this.toggleRecording());
112
+ this.options.container.appendChild(this.button);
113
+
114
+ this.updateButton();
115
+ }
116
+
117
+ /**
118
+ * Update button appearance and state
119
+ */
120
+ updateButton() {
121
+ if (!this.button) return;
122
+
123
+ const icon = this.isRecording ? '🔴' : '🎤';
124
+ const text = this.isRecording ? 'Stop Listening' : 'Start Listening';
125
+
126
+ this.button.innerHTML = `
127
+ <span style="font-size: 20px;">${icon}</span>
128
+ <span>${text}</span>
129
+ `;
130
+
131
+ this.button.disabled = !this.isConnected;
132
+ this.button.style.backgroundColor = this.isRecording ? '#dc3545' :
133
+ this.isConnected ? '#007bff' : '#6c757d';
134
+ }
135
+
136
+ /**
137
+ * Connect to voice server
138
+ */
139
+ async connect() {
140
+ try {
141
+ await this.voiceSDK.connect();
142
+ } catch (error) {
143
+ console.error('Failed to connect:', error);
144
+ }
145
+ }
146
+
147
+ /**
148
+ * Toggle recording
149
+ */
150
+ async toggleRecording() {
151
+ if (!this.voiceSDK) return;
152
+
153
+ try {
154
+ await this.voiceSDK.toggleRecording();
155
+ } catch (error) {
156
+ console.error('Error toggling recording:', error);
157
+ }
158
+ }
159
+
160
+ /**
161
+ * Get current status
162
+ */
163
+ getStatus() {
164
+ return {
165
+ isConnected: this.isConnected,
166
+ isRecording: this.isRecording,
167
+ isPlaying: this.isPlaying
168
+ };
169
+ }
170
+
171
+ /**
172
+ * Update configuration
173
+ */
174
+ updateConfig(newConfig) {
175
+ this.voiceSDK.updateConfig(newConfig);
176
+ }
177
+
178
+ /**
179
+ * Destroy the button and cleanup
180
+ */
181
+ destroy() {
182
+ if (this.button && this.button.parentNode) {
183
+ this.button.parentNode.removeChild(this.button);
184
+ }
185
+
186
+ if (this.voiceSDK) {
187
+ this.voiceSDK.destroy();
188
+ }
189
+ }
190
+ }