ttp-agent-sdk 2.1.7 → 2.1.9

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.
@@ -1,307 +0,0 @@
1
- /**
2
- * React Example - VoiceSDK Integration
3
- *
4
- * This example shows how to use the VoiceSDK with React components
5
- * for a modern web application.
6
- */
7
-
8
- import React, { useState, useRef, useEffect } from 'react';
9
- import { VoiceSDK, VoiceButton } from 'ttp-agent-sdk';
10
-
11
- function VoiceChatApp() {
12
- const [isConnected, setIsConnected] = useState(false);
13
- const [isRecording, setIsRecording] = useState(false);
14
- const [isPlaying, setIsPlaying] = useState(false);
15
- const [messages, setMessages] = useState([]);
16
- const [connectionStatus, setConnectionStatus] = useState('Disconnected');
17
-
18
- const voiceSDKRef = useRef(null);
19
-
20
- // Initialize VoiceSDK
21
- useEffect(() => {
22
- const voiceSDK = new VoiceSDK({
23
- websocketUrl: 'wss://speech.talktopc.com/ws/conv',
24
- agentId: 'demo_agent_123',
25
- appId: 'demo_app_456',
26
- voice: 'default',
27
- language: 'en',
28
- autoReconnect: true
29
- });
30
-
31
- // Set up event handlers
32
- voiceSDK.on('connected', () => {
33
- setIsConnected(true);
34
- setConnectionStatus('Connected');
35
- addMessage('system', 'Connected to voice agent');
36
- });
37
-
38
- voiceSDK.on('disconnected', () => {
39
- setIsConnected(false);
40
- setIsRecording(false);
41
- setIsPlaying(false);
42
- setConnectionStatus('Disconnected');
43
- addMessage('system', 'Disconnected from voice agent');
44
- });
45
-
46
- voiceSDK.on('recordingStarted', () => {
47
- setIsRecording(true);
48
- addMessage('user', '🎤 Recording...');
49
- });
50
-
51
- voiceSDK.on('recordingStopped', () => {
52
- setIsRecording(false);
53
- addMessage('user', '⏹️ Recording stopped');
54
- });
55
-
56
- voiceSDK.on('playbackStarted', () => {
57
- setIsPlaying(true);
58
- addMessage('agent', '🔊 Agent is speaking...');
59
- });
60
-
61
- voiceSDK.on('playbackStopped', () => {
62
- setIsPlaying(false);
63
- });
64
-
65
- voiceSDK.on('message', (message) => {
66
- if (message.type === 'agent_response') {
67
- addMessage('agent', message.agent_response);
68
- } else if (message.type === 'user_transcript') {
69
- addMessage('user', message.user_transcription);
70
- }
71
- });
72
-
73
- voiceSDK.on('error', (error) => {
74
- console.error('VoiceSDK Error:', error);
75
- addMessage('error', `Error: ${error.message}`);
76
- });
77
-
78
- voiceSDKRef.current = voiceSDK;
79
-
80
- // Cleanup on unmount
81
- return () => {
82
- if (voiceSDKRef.current) {
83
- voiceSDKRef.current.destroy();
84
- }
85
- };
86
- }, []);
87
-
88
- const addMessage = (type, text) => {
89
- setMessages(prev => [...prev, { type, text, timestamp: new Date() }]);
90
- };
91
-
92
- const handleConnect = async () => {
93
- if (voiceSDKRef.current) {
94
- try {
95
- setConnectionStatus('Connecting...');
96
- await voiceSDKRef.current.connect();
97
- } catch (error) {
98
- console.error('Connection failed:', error);
99
- setConnectionStatus('Connection failed');
100
- }
101
- }
102
- };
103
-
104
- const handleDisconnect = () => {
105
- if (voiceSDKRef.current) {
106
- voiceSDKRef.current.disconnect();
107
- }
108
- };
109
-
110
- const handleToggleRecording = async () => {
111
- if (voiceSDKRef.current) {
112
- try {
113
- if (isRecording) {
114
- await voiceSDKRef.current.stopRecording();
115
- } else {
116
- await voiceSDKRef.current.startRecording();
117
- }
118
- } catch (error) {
119
- console.error('Recording toggle failed:', error);
120
- }
121
- }
122
- };
123
-
124
- return (
125
- <div style={{
126
- maxWidth: '800px',
127
- margin: '0 auto',
128
- padding: '20px',
129
- fontFamily: 'system-ui, -apple-system, sans-serif'
130
- }}>
131
- <h1>🎤 Voice Chat App</h1>
132
-
133
- {/* Connection Status */}
134
- <div style={{
135
- padding: '12px',
136
- borderRadius: '8px',
137
- marginBottom: '20px',
138
- backgroundColor: isConnected ? '#D1FAE5' : '#FEE2E2',
139
- color: isConnected ? '#065F46' : '#991B1B',
140
- border: `1px solid ${isConnected ? '#10B981' : '#EF4444'}`
141
- }}>
142
- <strong>Status:</strong> {connectionStatus}
143
- </div>
144
-
145
- {/* Controls */}
146
- <div style={{
147
- display: 'flex',
148
- gap: '12px',
149
- marginBottom: '20px',
150
- flexWrap: 'wrap'
151
- }}>
152
- <button
153
- onClick={handleConnect}
154
- disabled={isConnected}
155
- style={{
156
- padding: '12px 24px',
157
- backgroundColor: isConnected ? '#9CA3AF' : '#4F46E5',
158
- color: 'white',
159
- border: 'none',
160
- borderRadius: '6px',
161
- cursor: isConnected ? 'not-allowed' : 'pointer'
162
- }}
163
- >
164
- Connect
165
- </button>
166
-
167
- <button
168
- onClick={handleDisconnect}
169
- disabled={!isConnected}
170
- style={{
171
- padding: '12px 24px',
172
- backgroundColor: !isConnected ? '#9CA3AF' : '#EF4444',
173
- color: 'white',
174
- border: 'none',
175
- borderRadius: '6px',
176
- cursor: !isConnected ? 'not-allowed' : 'pointer'
177
- }}
178
- >
179
- Disconnect
180
- </button>
181
-
182
- <button
183
- onClick={handleToggleRecording}
184
- disabled={!isConnected}
185
- style={{
186
- padding: '12px 24px',
187
- backgroundColor: !isConnected ? '#9CA3AF' : (isRecording ? '#EF4444' : '#10B981'),
188
- color: 'white',
189
- border: 'none',
190
- borderRadius: '6px',
191
- cursor: !isConnected ? 'not-allowed' : 'pointer'
192
- }}
193
- >
194
- {isRecording ? 'Stop Recording' : 'Start Recording'}
195
- </button>
196
- </div>
197
-
198
- {/* Voice Button Component */}
199
- <div style={{ marginBottom: '20px' }}>
200
- <h3>Voice Button Component:</h3>
201
- <VoiceButton
202
- websocketUrl="wss://speech.talktopc.com/ws/conv"
203
- agentId="demo_agent_123"
204
- appId="demo_app_456"
205
- onConnected={() => console.log('VoiceButton connected')}
206
- onRecordingStarted={() => console.log('VoiceButton recording started')}
207
- onPlaybackStarted={() => console.log('VoiceButton playback started')}
208
- />
209
- </div>
210
-
211
- {/* Messages */}
212
- <div style={{
213
- border: '1px solid #E5E7EB',
214
- borderRadius: '8px',
215
- height: '400px',
216
- overflowY: 'auto',
217
- padding: '16px',
218
- backgroundColor: '#F9FAFB'
219
- }}>
220
- <h3>Messages:</h3>
221
- {messages.length === 0 ? (
222
- <p style={{ color: '#6B7280', fontStyle: 'italic' }}>
223
- No messages yet. Connect and start recording to begin the conversation.
224
- </p>
225
- ) : (
226
- messages.map((message, index) => (
227
- <div
228
- key={index}
229
- style={{
230
- marginBottom: '12px',
231
- padding: '8px 12px',
232
- borderRadius: '6px',
233
- backgroundColor: message.type === 'user' ? '#E5E7EB' :
234
- message.type === 'agent' ? '#F3F4F6' :
235
- message.type === 'error' ? '#FEE2E2' : '#EFF6FF',
236
- color: message.type === 'error' ? '#991B1B' : '#111827',
237
- alignSelf: message.type === 'user' ? 'flex-end' : 'flex-start',
238
- maxWidth: '80%',
239
- marginLeft: message.type === 'user' ? 'auto' : '0',
240
- marginRight: message.type === 'user' ? '0' : 'auto'
241
- }}
242
- >
243
- <div style={{ fontWeight: 'bold', marginBottom: '4px' }}>
244
- {message.type === 'user' ? '👤 You' :
245
- message.type === 'agent' ? '🤖 Agent' :
246
- message.type === 'error' ? '❌ Error' : 'ℹ️ System'}
247
- </div>
248
- <div>{message.text}</div>
249
- <div style={{
250
- fontSize: '12px',
251
- color: '#6B7280',
252
- marginTop: '4px'
253
- }}>
254
- {message.timestamp.toLocaleTimeString()}
255
- </div>
256
- </div>
257
- ))
258
- )}
259
- </div>
260
-
261
- {/* Status Indicators */}
262
- <div style={{
263
- display: 'flex',
264
- gap: '20px',
265
- marginTop: '20px',
266
- fontSize: '14px',
267
- color: '#6B7280'
268
- }}>
269
- <div>
270
- <span style={{
271
- display: 'inline-block',
272
- width: '8px',
273
- height: '8px',
274
- borderRadius: '50%',
275
- backgroundColor: isConnected ? '#10B981' : '#EF4444',
276
- marginRight: '8px'
277
- }}></span>
278
- Connection
279
- </div>
280
- <div>
281
- <span style={{
282
- display: 'inline-block',
283
- width: '8px',
284
- height: '8px',
285
- borderRadius: '50%',
286
- backgroundColor: isRecording ? '#EF4444' : '#9CA3AF',
287
- marginRight: '8px'
288
- }}></span>
289
- Recording
290
- </div>
291
- <div>
292
- <span style={{
293
- display: 'inline-block',
294
- width: '8px',
295
- height: '8px',
296
- borderRadius: '50%',
297
- backgroundColor: isPlaying ? '#10B981' : '#9CA3AF',
298
- marginRight: '8px'
299
- }}></span>
300
- Playing
301
- </div>
302
- </div>
303
- </div>
304
- );
305
- }
306
-
307
- export default VoiceChatApp;