@telnyx/ai-agent-lib 0.1.7 → 0.1.8
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 +25 -5
- package/dist/client.d.ts +65 -1
- package/dist/index.js +69 -7
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -86,7 +86,12 @@ function VoiceChat() {
|
|
|
86
86
|
<h3>Agent State: {agentState}</h3>
|
|
87
87
|
|
|
88
88
|
<button
|
|
89
|
-
onClick={() =>
|
|
89
|
+
onClick={() =>
|
|
90
|
+
client.startConversation({
|
|
91
|
+
callerName: 'Jane Doe',
|
|
92
|
+
customHeaders: [{ name: 'X-Account-Number', value: '123456' }]
|
|
93
|
+
})
|
|
94
|
+
}
|
|
90
95
|
disabled={connectionState !== 'connected'}
|
|
91
96
|
>
|
|
92
97
|
Start Conversation
|
|
@@ -151,11 +156,20 @@ Returns the `TelnyxAIAgent` instance for direct API access.
|
|
|
151
156
|
**Methods:**
|
|
152
157
|
- `connect()` - Connect to Telnyx platform
|
|
153
158
|
- `disconnect()` - Disconnect and cleanup
|
|
154
|
-
- `startConversation()` - Start a new conversation
|
|
159
|
+
- `startConversation(options?)` - Start a new conversation with optional caller metadata and headers
|
|
155
160
|
- `endConversation()` - End the current conversation
|
|
156
161
|
- `sendConversationMessage(message: string)` - Send a text message during an active conversation
|
|
157
162
|
- `transcript` - Get current transcript array
|
|
158
163
|
|
|
164
|
+
**`startConversation` Options:**
|
|
165
|
+
|
|
166
|
+
| Option | Type | Description |
|
|
167
|
+
|--------|------|-------------|
|
|
168
|
+
| `destinationNumber` | `string` | [docs](https://developers.telnyx.com/development/webrtc/js-sdk/interfaces/icalloptions#destinationnumber) |
|
|
169
|
+
| `callerNumber` | `string` | [docs](https://developers.telnyx.com/development/webrtc/js-sdk/interfaces/icalloptions#callernumber) |
|
|
170
|
+
| `callerName` | `string` | [docs](https://developers.telnyx.com/development/webrtc/js-sdk/interfaces/icalloptions#callername) |
|
|
171
|
+
| `customHeaders` | `{ name: string; value: string }[]` | [docs](https://developers.telnyx.com/development/webrtc/js-sdk/interfaces/icalloptions#customheaders) |
|
|
172
|
+
|
|
159
173
|
**Events:**
|
|
160
174
|
- `agent.connected` - Agent successfully connected
|
|
161
175
|
- `agent.disconnected` - Agent disconnected
|
|
@@ -224,8 +238,15 @@ agent.on('conversation.update', (notification) => {
|
|
|
224
238
|
}
|
|
225
239
|
});
|
|
226
240
|
|
|
227
|
-
// Start a conversation
|
|
228
|
-
await agent.startConversation(
|
|
241
|
+
// Start a conversation with optional caller metadata
|
|
242
|
+
await agent.startConversation({
|
|
243
|
+
callerNumber: '+15551234567',
|
|
244
|
+
callerName: 'John Doe',
|
|
245
|
+
customHeaders: [
|
|
246
|
+
{ name: 'X-User-Plan', value: 'gold' },
|
|
247
|
+
{ name: 'X-Session-Id', value: 'session-abc' }
|
|
248
|
+
]
|
|
249
|
+
});
|
|
229
250
|
|
|
230
251
|
// Send messages during an active conversation
|
|
231
252
|
// Note: This will only work when there's an active call
|
|
@@ -350,4 +371,3 @@ For support, please contact Telnyx support or check the [Telnyx documentation](h
|
|
|
350
371
|
## Contributing
|
|
351
372
|
|
|
352
373
|
This library is maintained by Telnyx. For bug reports or feature requests, please contact Telnyx support.elnyx AI Agent Library
|
|
353
|
-
|
package/dist/client.d.ts
CHANGED
|
@@ -14,11 +14,75 @@ export declare class TelnyxAIAgent extends EventEmitter<AIAgentEvents> {
|
|
|
14
14
|
private audioStreamMonitor;
|
|
15
15
|
activeCall: Call | null;
|
|
16
16
|
constructor(params: TelnyxAIAgentConstructorParams);
|
|
17
|
+
/**
|
|
18
|
+
* Connects to the Telnyx WebRTC service and establishes a session with the AI agent.
|
|
19
|
+
*
|
|
20
|
+
* @returns Promise that resolves when the connection is established
|
|
21
|
+
*/
|
|
17
22
|
connect(): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Disconnects from the Telnyx WebRTC service and cleans up all event listeners.
|
|
25
|
+
* Emits an 'agent.disconnected' event before completing cleanup.
|
|
26
|
+
*/
|
|
18
27
|
disconnect(): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Sends a text message to the AI agent during an active conversation.
|
|
30
|
+
* Requires an active conversation to be in progress.
|
|
31
|
+
*
|
|
32
|
+
* @param message - The text message to send to the AI agent
|
|
33
|
+
* @param attachments - Optional array of base64 encoded file attachments (defaults to empty array)
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* client.sendConversationMessage('Hello, I need help with my account');
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* // With attachments
|
|
40
|
+
* client.sendConversationMessage('Please review this document', ['base64EncodedData...']);
|
|
41
|
+
*/
|
|
19
42
|
sendConversationMessage(message: string, attachments?: string[]): void;
|
|
43
|
+
/**
|
|
44
|
+
* Gets the current conversation transcript.
|
|
45
|
+
*
|
|
46
|
+
* @returns Array of transcript items containing all messages exchanged during the conversation
|
|
47
|
+
*/
|
|
20
48
|
get transcript(): TranscriptItem[];
|
|
21
|
-
|
|
49
|
+
/**
|
|
50
|
+
* Initiates a conversation with the AI agent.
|
|
51
|
+
*
|
|
52
|
+
* @param options - Optional configuration for the call.
|
|
53
|
+
* Note: This method will ALWAYS call the AI agent, the optional parameters are primarily used for log referencing or passing parameters to the agent via the custom headers.
|
|
54
|
+
* @param options.destinationNumber - The destination phone number (defaults to "xxx"). Note, regardless of the destination number, the call will be made to the AI agent.
|
|
55
|
+
* @param options.callerNumber - The caller's phone number
|
|
56
|
+
* @param options.callerName - The caller's display name
|
|
57
|
+
* @param options.customHeaders - Custom SIP headers to pass to the AI agent. Headers with the `X-` prefix
|
|
58
|
+
* will be mapped to dynamic variables in the AI assistant (e.g., `X-Account-Number` becomes `{{account_number}}`).
|
|
59
|
+
* Note: Hyphens in header names are converted to underscores in variable names.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* // Start conversation with custom headers for dynamic variables
|
|
63
|
+
* client.startConversation({
|
|
64
|
+
* callerName: 'John Doe',
|
|
65
|
+
* customHeaders: [
|
|
66
|
+
* { name: 'X-User-Name', value: 'user-name' },
|
|
67
|
+
* { name: 'X-Agent-Session', value: 'session-abc' }
|
|
68
|
+
* ]
|
|
69
|
+
* });
|
|
70
|
+
* // These headers will be available in your AI assistant as {{user_name}} and {{agent_session}}
|
|
71
|
+
*/
|
|
72
|
+
startConversation(options?: {
|
|
73
|
+
destinationNumber?: string;
|
|
74
|
+
callerNumber?: string;
|
|
75
|
+
callerName?: string;
|
|
76
|
+
customHeaders?: {
|
|
77
|
+
name: string;
|
|
78
|
+
value: string;
|
|
79
|
+
}[];
|
|
80
|
+
}): Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* Ends the current active conversation with the AI agent.
|
|
83
|
+
*
|
|
84
|
+
* @returns Promise that resolves when the call is hung up, or undefined if there is no active call
|
|
85
|
+
*/
|
|
22
86
|
endConversation(): void | undefined;
|
|
23
87
|
private onClientReady;
|
|
24
88
|
private onClientOrSocketError;
|
package/dist/index.js
CHANGED
|
@@ -2672,12 +2672,35 @@ class Ot extends dt {
|
|
|
2672
2672
|
this.onAgentStateChange
|
|
2673
2673
|
), this.audioStreamMonitor = new fi();
|
|
2674
2674
|
}
|
|
2675
|
+
/**
|
|
2676
|
+
* Connects to the Telnyx WebRTC service and establishes a session with the AI agent.
|
|
2677
|
+
*
|
|
2678
|
+
* @returns Promise that resolves when the connection is established
|
|
2679
|
+
*/
|
|
2675
2680
|
async connect() {
|
|
2676
2681
|
return this.telnyxRTC.connect();
|
|
2677
2682
|
}
|
|
2683
|
+
/**
|
|
2684
|
+
* Disconnects from the Telnyx WebRTC service and cleans up all event listeners.
|
|
2685
|
+
* Emits an 'agent.disconnected' event before completing cleanup.
|
|
2686
|
+
*/
|
|
2678
2687
|
async disconnect() {
|
|
2679
2688
|
this.audioStreamMonitor.stopAudioStreamMonitor(), this.telnyxRTC.disconnect(), this.telnyxRTC.off(O.Ready, this.onClientReady), this.telnyxRTC.off(O.Error, this.onClientOrSocketError), this.telnyxRTC.off(O.SocketError, this.onClientOrSocketError), this.telnyxRTC.off(O.Notification, this.onNotification), this.emit("agent.disconnected"), this.transcription.removeAllListeners(), Ee.removeAllListeners(), this.removeAllListeners();
|
|
2680
2689
|
}
|
|
2690
|
+
/**
|
|
2691
|
+
* Sends a text message to the AI agent during an active conversation.
|
|
2692
|
+
* Requires an active conversation to be in progress.
|
|
2693
|
+
*
|
|
2694
|
+
* @param message - The text message to send to the AI agent
|
|
2695
|
+
* @param attachments - Optional array of base64 encoded file attachments (defaults to empty array)
|
|
2696
|
+
*
|
|
2697
|
+
* @example
|
|
2698
|
+
* client.sendConversationMessage('Hello, I need help with my account');
|
|
2699
|
+
*
|
|
2700
|
+
* @example
|
|
2701
|
+
* // With attachments
|
|
2702
|
+
* client.sendConversationMessage('Please review this document', ['base64EncodedData...']);
|
|
2703
|
+
*/
|
|
2681
2704
|
sendConversationMessage(e, n = []) {
|
|
2682
2705
|
if (!this.activeCall) {
|
|
2683
2706
|
console.error("No active call to send message.");
|
|
@@ -2685,23 +2708,62 @@ class Ot extends dt {
|
|
|
2685
2708
|
}
|
|
2686
2709
|
this.activeCall.sendConversationMessage(e, n);
|
|
2687
2710
|
}
|
|
2711
|
+
/**
|
|
2712
|
+
* Gets the current conversation transcript.
|
|
2713
|
+
*
|
|
2714
|
+
* @returns Array of transcript items containing all messages exchanged during the conversation
|
|
2715
|
+
*/
|
|
2688
2716
|
get transcript() {
|
|
2689
2717
|
return this.transcription.transcript;
|
|
2690
2718
|
}
|
|
2691
|
-
|
|
2719
|
+
/**
|
|
2720
|
+
* Initiates a conversation with the AI agent.
|
|
2721
|
+
*
|
|
2722
|
+
* @param options - Optional configuration for the call.
|
|
2723
|
+
* Note: This method will ALWAYS call the AI agent, the optional parameters are primarily used for log referencing or passing parameters to the agent via the custom headers.
|
|
2724
|
+
* @param options.destinationNumber - The destination phone number (defaults to "xxx"). Note, regardless of the destination number, the call will be made to the AI agent.
|
|
2725
|
+
* @param options.callerNumber - The caller's phone number
|
|
2726
|
+
* @param options.callerName - The caller's display name
|
|
2727
|
+
* @param options.customHeaders - Custom SIP headers to pass to the AI agent. Headers with the `X-` prefix
|
|
2728
|
+
* will be mapped to dynamic variables in the AI assistant (e.g., `X-Account-Number` becomes `{{account_number}}`).
|
|
2729
|
+
* Note: Hyphens in header names are converted to underscores in variable names.
|
|
2730
|
+
*
|
|
2731
|
+
* @example
|
|
2732
|
+
* // Start conversation with custom headers for dynamic variables
|
|
2733
|
+
* client.startConversation({
|
|
2734
|
+
* callerName: 'John Doe',
|
|
2735
|
+
* customHeaders: [
|
|
2736
|
+
* { name: 'X-User-Name', value: 'user-name' },
|
|
2737
|
+
* { name: 'X-Agent-Session', value: 'session-abc' }
|
|
2738
|
+
* ]
|
|
2739
|
+
* });
|
|
2740
|
+
* // These headers will be available in your AI assistant as {{user_name}} and {{agent_session}}
|
|
2741
|
+
*/
|
|
2742
|
+
async startConversation(e) {
|
|
2692
2743
|
if (!this.telnyxRTC) {
|
|
2693
2744
|
console.error("Client is not initialized.");
|
|
2694
2745
|
return;
|
|
2695
2746
|
}
|
|
2696
|
-
const
|
|
2697
|
-
(
|
|
2698
|
-
),
|
|
2747
|
+
const i = RTCRtpReceiver.getCapabilities("audio")?.codecs?.find(
|
|
2748
|
+
(h) => h.mimeType.toLowerCase().includes("opus")
|
|
2749
|
+
), { customHeaders: s, ...c } = e || {}, a = s ? [...s] : [];
|
|
2750
|
+
this.versionId && a.push({
|
|
2751
|
+
name: "X-AI-Assistant-Version-ID",
|
|
2752
|
+
value: this.versionId
|
|
2753
|
+
});
|
|
2754
|
+
const d = this.telnyxRTC.newCall({
|
|
2699
2755
|
destinationNumber: "xxx",
|
|
2700
|
-
|
|
2701
|
-
|
|
2756
|
+
...c,
|
|
2757
|
+
preferred_codecs: [i],
|
|
2758
|
+
customHeaders: a.length > 0 ? a : void 0
|
|
2702
2759
|
});
|
|
2703
|
-
this.emit("conversation.update", { call:
|
|
2760
|
+
this.emit("conversation.update", { call: d, type: "callUpdate" });
|
|
2704
2761
|
}
|
|
2762
|
+
/**
|
|
2763
|
+
* Ends the current active conversation with the AI agent.
|
|
2764
|
+
*
|
|
2765
|
+
* @returns Promise that resolves when the call is hung up, or undefined if there is no active call
|
|
2766
|
+
*/
|
|
2705
2767
|
endConversation() {
|
|
2706
2768
|
return this.activeCall?.hangup();
|
|
2707
2769
|
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@telnyx/ai-agent-lib",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.8",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
|
+
"license": "MIT",
|
|
8
9
|
"files": [
|
|
9
10
|
"dist"
|
|
10
11
|
],
|
|
@@ -40,5 +41,6 @@
|
|
|
40
41
|
"typescript": "~5.8.3",
|
|
41
42
|
"typescript-eslint": "^8.35.1",
|
|
42
43
|
"vite": "^7.0.4"
|
|
43
|
-
}
|
|
44
|
+
},
|
|
45
|
+
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
|
|
44
46
|
}
|