@telnyx/ai-agent-lib 0.1.6 → 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 CHANGED
@@ -86,7 +86,12 @@ function VoiceChat() {
86
86
  <h3>Agent State: {agentState}</h3>
87
87
 
88
88
  <button
89
- onClick={() => client.startConversation()}
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
@@ -331,7 +352,7 @@ This library is built with TypeScript and provides full type definitions. All ho
331
352
 
332
353
  ## Examples
333
354
 
334
- Check out the `/src` directory for a complete example application demonstrating all features of the library.
355
+ Check out the [example](https://github.com/team-telnyx/telnyx-ai-agent-lib-example) repository for a complete example application demonstrating all features of the library.
335
356
 
336
357
  ## Dependencies
337
358
 
@@ -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>;
19
- sendConversationMessage(message: string): 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
+ */
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
- startConversation(): Promise<void>;
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
@@ -2558,34 +2558,67 @@ function di() {
2558
2558
  }
2559
2559
  var ui = di();
2560
2560
  const dt = /* @__PURE__ */ li(ui), Ee = new dt();
2561
- function hi(t) {
2561
+ function hi(t, e) {
2562
+ let n = 0;
2563
+ return (...i) => {
2564
+ const s = Date.now();
2565
+ s - n >= e && (n = s, t(...i));
2566
+ };
2567
+ }
2568
+ const pi = 10;
2569
+ class fi {
2570
+ animationFrameId = null;
2571
+ stream = null;
2572
+ audioContext = null;
2573
+ source = null;
2574
+ analyser = null;
2575
+ updateAgentState = hi((e) => {
2576
+ Ee.emit("conversation.agent.state", e);
2577
+ }, 100);
2578
+ setMonitoredAudioStream(e) {
2579
+ this.stream && this.stopAudioStreamMonitor(), this.stream = e, this.startAudioStreamMonitor();
2580
+ }
2581
+ stopAudioStreamMonitor() {
2582
+ this.animationFrameId && (cancelAnimationFrame(this.animationFrameId), this.animationFrameId = null), this.source?.disconnect();
2583
+ }
2584
+ startAudioStreamMonitor() {
2585
+ if (!this.stream) return;
2586
+ this.audioContext = new AudioContext(), this.source = this.audioContext.createMediaStreamSource(this.stream), this.analyser = this.audioContext.createAnalyser(), this.source.connect(this.analyser), this.analyser.fftSize = 512;
2587
+ const e = new Uint8Array(this.analyser.frequencyBinCount), n = () => {
2588
+ const i = e.reduce((s, c) => s + c, 0) / e.length;
2589
+ this.analyser?.getByteFrequencyData(e), i >= pi ? this.updateAgentState("speaking") : this.updateAgentState("listening"), this.animationFrameId = requestAnimationFrame(n);
2590
+ };
2591
+ this.animationFrameId = requestAnimationFrame(n);
2592
+ }
2593
+ }
2594
+ function gi(t) {
2562
2595
  if (!t || typeof t != "object")
2563
2596
  return !1;
2564
2597
  const e = t;
2565
2598
  return e.method === "ai_conversation" && typeof e.params == "object";
2566
2599
  }
2567
- function pi(t) {
2600
+ function vi(t) {
2568
2601
  if (!t || typeof t != "object")
2569
2602
  return !1;
2570
2603
  const e = t;
2571
2604
  return e.params.type !== "response.text.delta" ? !1 : !!e.params.delta;
2572
2605
  }
2573
- function fi(t) {
2606
+ function mi(t) {
2574
2607
  if (!t || typeof t != "object")
2575
2608
  return !1;
2576
2609
  const e = t;
2577
2610
  return e.params.type !== "conversation.item.created" ? !1 : !!e.params.item.content;
2578
2611
  }
2579
- function gi(t) {
2580
- return pi(t) ? {
2612
+ function bi(t) {
2613
+ return vi(t) ? {
2581
2614
  id: `${t.params.item_id}-${Date.now()}`,
2582
2615
  role: "assistant",
2583
2616
  content: t.params.delta,
2584
2617
  timestamp: /* @__PURE__ */ new Date()
2585
2618
  } : null;
2586
2619
  }
2587
- function vi(t) {
2588
- if (!fi(t) || t.params.item.role !== "user" || t.params.item.status !== "completed")
2620
+ function yi(t) {
2621
+ if (!mi(t) || t.params.item.role !== "user" || t.params.item.status !== "completed")
2589
2622
  return null;
2590
2623
  let e = "", n = [];
2591
2624
  return Array.isArray(t.params.item.content) ? (e = t.params.item.content.reduce((i, s) => (s.type === "text" && (i += s.text), i), ""), n = t.params.item.content.reduce((i, s) => (s.type === "image_url" && s.image_url.url && i.push({ type: "image", url: s.image_url.url }), i), [])) : e = t.params.item.content, {
@@ -2596,61 +2629,28 @@ function vi(t) {
2596
2629
  attachments: n
2597
2630
  };
2598
2631
  }
2599
- class mi extends dt {
2632
+ class _i extends dt {
2600
2633
  telnyxRTC;
2601
2634
  transcript = [];
2602
2635
  constructor(e) {
2603
2636
  super(), this.telnyxRTC = e, this.telnyxRTC.on(O.SocketMessage, this.onSocketMessage);
2604
2637
  }
2605
2638
  onSocketMessage = (e) => {
2606
- if (hi(e))
2639
+ if (gi(e))
2607
2640
  switch (e.params.type) {
2608
2641
  case "response.text.delta": {
2609
- const n = gi(e);
2642
+ const n = bi(e);
2610
2643
  n && (this.transcript.push(n), this.emit("transcript.item", n), Ee.emit("conversation.agent.state", "listening"));
2611
2644
  return;
2612
2645
  }
2613
2646
  case "conversation.item.created": {
2614
- const n = vi(e);
2647
+ const n = yi(e);
2615
2648
  n && (this.transcript.push(n), this.emit("transcript.item", n), Ee.emit("conversation.agent.state", "thinking"));
2616
2649
  return;
2617
2650
  }
2618
2651
  }
2619
2652
  };
2620
2653
  }
2621
- function bi(t, e) {
2622
- let n = 0;
2623
- return (...i) => {
2624
- const s = Date.now();
2625
- s - n >= e && (n = s, t(...i));
2626
- };
2627
- }
2628
- const yi = 10;
2629
- class _i {
2630
- animationFrameId = null;
2631
- stream = null;
2632
- audioContext = null;
2633
- source = null;
2634
- analyser = null;
2635
- updateAgentState = bi((e) => {
2636
- Ee.emit("conversation.agent.state", e);
2637
- }, 100);
2638
- setMonitoredAudioStream(e) {
2639
- this.stream && this.stopAudioStreamMonitor(), this.stream = e, this.startAudioStreamMonitor();
2640
- }
2641
- stopAudioStreamMonitor() {
2642
- this.animationFrameId && (cancelAnimationFrame(this.animationFrameId), this.animationFrameId = null), this.source?.disconnect();
2643
- }
2644
- startAudioStreamMonitor() {
2645
- if (!this.stream) return;
2646
- this.audioContext = new AudioContext(), this.source = this.audioContext.createMediaStreamSource(this.stream), this.analyser = this.audioContext.createAnalyser(), this.source.connect(this.analyser), this.analyser.fftSize = 512;
2647
- const e = new Uint8Array(this.analyser.frequencyBinCount), n = () => {
2648
- const i = e.reduce((s, c) => s + c, 0) / e.length;
2649
- this.analyser?.getByteFrequencyData(e), i >= yi ? this.updateAgentState("speaking") : this.updateAgentState("listening"), this.animationFrameId = requestAnimationFrame(n);
2650
- };
2651
- this.animationFrameId = requestAnimationFrame(n);
2652
- }
2653
- }
2654
2654
  class Ot extends dt {
2655
2655
  telnyxRTC;
2656
2656
  transcription;
@@ -2667,41 +2667,103 @@ class Ot extends dt {
2667
2667
  target_type: "ai_assistant",
2668
2668
  target_version_id: e.versionId
2669
2669
  }
2670
- }), this.telnyxRTC.on(O.Ready, this.onClientReady), this.telnyxRTC.on(O.Error, this.onClientOrSocketError), this.telnyxRTC.on(O.SocketError, this.onClientOrSocketError), this.telnyxRTC.on(O.Notification, this.onNotification), this.transcription = new mi(this.telnyxRTC), this.transcription.addListener("transcript.item", this.onTranscriptItem), Ee.addListener(
2670
+ }), this.telnyxRTC.on(O.Ready, this.onClientReady), this.telnyxRTC.on(O.Error, this.onClientOrSocketError), this.telnyxRTC.on(O.SocketError, this.onClientOrSocketError), this.telnyxRTC.on(O.Notification, this.onNotification), this.transcription = new _i(this.telnyxRTC), this.transcription.addListener("transcript.item", this.onTranscriptItem), Ee.addListener(
2671
2671
  "conversation.agent.state",
2672
2672
  this.onAgentStateChange
2673
- ), this.audioStreamMonitor = new _i();
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
  }
2681
- sendConversationMessage(e) {
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
+ */
2704
+ sendConversationMessage(e, n = []) {
2682
2705
  if (!this.activeCall) {
2683
2706
  console.error("No active call to send message.");
2684
2707
  return;
2685
2708
  }
2686
- this.activeCall.sendConversationMessage(e);
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
- async startConversation() {
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 n = RTCRtpReceiver.getCapabilities("audio")?.codecs?.find(
2697
- (c) => c.mimeType.toLowerCase().includes("opus")
2698
- ), i = this.versionId ? [{ name: "X-AI-Assistant-Version-ID", value: this.versionId }] : void 0, s = this.telnyxRTC.newCall({
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
- preferred_codecs: [n],
2701
- customHeaders: i
2756
+ ...c,
2757
+ preferred_codecs: [i],
2758
+ customHeaders: a.length > 0 ? a : void 0
2702
2759
  });
2703
- this.emit("conversation.update", { call: s, type: "callUpdate" });
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.6",
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
  }