@telnyx/ai-agent-lib 0.1.4 → 0.1.6

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 CHANGED
@@ -46,7 +46,7 @@ createRoot(document.getElementById('root')!).render(
46
46
  ### 2. Use hooks in your components
47
47
 
48
48
  ```tsx
49
- import React, { useEffect, useRef } from 'react';
49
+ import React, { useEffect, useRef, useState } from 'react';
50
50
  import {
51
51
  useClient,
52
52
  useTranscript,
@@ -62,6 +62,7 @@ function VoiceChat() {
62
62
  const conversation = useConversation();
63
63
  const agentState = useAgentState();
64
64
  const audioRef = useRef<HTMLAudioElement>(null);
65
+ const [messageInput, setMessageInput] = useState('');
65
66
 
66
67
  // Setup audio playback
67
68
  useEffect(() => {
@@ -70,6 +71,15 @@ function VoiceChat() {
70
71
  }
71
72
  }, [conversation]);
72
73
 
74
+ const handleSendMessage = () => {
75
+ if (messageInput.trim()) {
76
+ client.sendConversationMessage(messageInput);
77
+ setMessageInput('');
78
+ }
79
+ };
80
+
81
+ const isCallActive = conversation?.call?.state === 'active';
82
+
73
83
  return (
74
84
  <div>
75
85
  <h2>Connection: {connectionState}</h2>
@@ -86,6 +96,26 @@ function VoiceChat() {
86
96
  End Conversation
87
97
  </button>
88
98
 
99
+ {/* Text message input - only available during active call */}
100
+ {isCallActive && (
101
+ <div style={{ margin: '20px 0' }}>
102
+ <h4>Send Text Message</h4>
103
+ <input
104
+ type="text"
105
+ value={messageInput}
106
+ onChange={(e) => setMessageInput(e.target.value)}
107
+ placeholder="Type a message..."
108
+ onKeyPress={(e) => e.key === 'Enter' && handleSendMessage()}
109
+ />
110
+ <button
111
+ onClick={handleSendMessage}
112
+ disabled={!messageInput.trim()}
113
+ >
114
+ Send Message
115
+ </button>
116
+ </div>
117
+ )}
118
+
89
119
  <audio ref={audioRef} autoPlay playsInline controls />
90
120
 
91
121
  <div>
@@ -100,6 +130,7 @@ function VoiceChat() {
100
130
  </div>
101
131
  );
102
132
  }
133
+ }
103
134
  ```
104
135
 
105
136
  ## API Reference
@@ -122,6 +153,7 @@ Returns the `TelnyxAIAgent` instance for direct API access.
122
153
  - `disconnect()` - Disconnect and cleanup
123
154
  - `startConversation()` - Start a new conversation
124
155
  - `endConversation()` - End the current conversation
156
+ - `sendConversationMessage(message: string)` - Send a text message during an active conversation
125
157
  - `transcript` - Get current transcript array
126
158
 
127
159
  **Events:**
@@ -183,9 +215,24 @@ agent.on('conversation.agent.state', (state) => {
183
215
  console.log(`Agent is now: ${state}`);
184
216
  });
185
217
 
218
+ agent.on('conversation.update', (notification) => {
219
+ if (notification.call?.state === 'active') {
220
+ console.log('Call is now active - you can send messages');
221
+
222
+ // Send a text message during the conversation
223
+ agent.sendConversationMessage('Hello, I have a question about your services.');
224
+ }
225
+ });
226
+
186
227
  // Start a conversation
187
228
  await agent.startConversation();
188
229
 
230
+ // Send messages during an active conversation
231
+ // Note: This will only work when there's an active call
232
+ setTimeout(() => {
233
+ agent.sendConversationMessage('Can you help me with my account?');
234
+ }, 5000);
235
+
189
236
  // Access transcript
190
237
  console.log(agent.transcript);
191
238
 
@@ -195,6 +242,57 @@ await agent.disconnect();
195
242
 
196
243
  ## Advanced Usage
197
244
 
245
+ ### Sending Text Messages During Conversations
246
+
247
+ You can send text messages to the AI agent during an active conversation using the `sendConversationMessage` method. This is useful for providing context, asking questions, or sending information that might be easier to type than speak.
248
+
249
+ ```tsx
250
+ // React example
251
+ function ChatInterface() {
252
+ const client = useClient();
253
+ const conversation = useConversation();
254
+ const [message, setMessage] = useState('');
255
+
256
+ const handleSendMessage = () => {
257
+ if (conversation?.call?.state === 'active' && message.trim()) {
258
+ client.sendConversationMessage(message);
259
+ setMessage('');
260
+ }
261
+ };
262
+
263
+ return (
264
+ <div>
265
+ {conversation?.call?.state === 'active' && (
266
+ <div>
267
+ <input
268
+ value={message}
269
+ onChange={(e) => setMessage(e.target.value)}
270
+ placeholder="Type a message to the agent..."
271
+ onKeyPress={(e) => e.key === 'Enter' && handleSendMessage()}
272
+ />
273
+ <button onClick={handleSendMessage}>Send</button>
274
+ </div>
275
+ )}
276
+ </div>
277
+ );
278
+ }
279
+ ```
280
+
281
+ ```typescript
282
+ // Direct usage example
283
+ agent.on('conversation.update', (notification) => {
284
+ if (notification.call?.state === 'active') {
285
+ // Now you can send text messages
286
+ agent.sendConversationMessage('I need help with order #12345');
287
+ }
288
+ });
289
+ ```
290
+
291
+ **Important Notes:**
292
+ - Messages can only be sent during an active conversation (when `call.state === 'active'`)
293
+ - The agent will receive and process text messages just like spoken input
294
+ - Text messages may appear in the transcript depending on the agent configuration
295
+
198
296
  ### Custom Audio Handling
199
297
 
200
298
  The library automatically handles audio stream monitoring and agent state detection based on audio levels. The audio stream is available through the conversation object:
package/dist/client.d.ts CHANGED
@@ -16,6 +16,7 @@ export declare class TelnyxAIAgent extends EventEmitter<AIAgentEvents> {
16
16
  constructor(params: TelnyxAIAgentConstructorParams);
17
17
  connect(): Promise<void>;
18
18
  disconnect(): Promise<void>;
19
+ sendConversationMessage(message: string): void;
19
20
  get transcript(): TranscriptItem[];
20
21
  startConversation(): Promise<void>;
21
22
  endConversation(): void | undefined;