sarvam-conv-ai-sdk 0.0.1

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.
Files changed (84) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +208 -0
  3. package/dist/conversation.d.ts +92 -0
  4. package/dist/conversation.d.ts.map +1 -0
  5. package/dist/conversation.js +154 -0
  6. package/dist/conversation.js.map +1 -0
  7. package/dist/index.d.ts +6 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +7 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/interfaces/browser.d.ts +76 -0
  12. package/dist/interfaces/browser.d.ts.map +1 -0
  13. package/dist/interfaces/browser.js +318 -0
  14. package/dist/interfaces/browser.js.map +1 -0
  15. package/dist/modules/socket-manager.d.ts +183 -0
  16. package/dist/modules/socket-manager.d.ts.map +1 -0
  17. package/dist/modules/socket-manager.js +534 -0
  18. package/dist/modules/socket-manager.js.map +1 -0
  19. package/dist/modules/text-agent.d.ts +30 -0
  20. package/dist/modules/text-agent.d.ts.map +1 -0
  21. package/dist/modules/text-agent.js +43 -0
  22. package/dist/modules/text-agent.js.map +1 -0
  23. package/dist/modules/voice-agent.d.ts +68 -0
  24. package/dist/modules/voice-agent.d.ts.map +1 -0
  25. package/dist/modules/voice-agent.js +158 -0
  26. package/dist/modules/voice-agent.js.map +1 -0
  27. package/dist/package.json +33 -0
  28. package/dist/types/actions.d.ts +17 -0
  29. package/dist/types/actions.d.ts.map +1 -0
  30. package/dist/types/actions.js +2 -0
  31. package/dist/types/actions.js.map +1 -0
  32. package/dist/types/audio.d.ts +78 -0
  33. package/dist/types/audio.d.ts.map +1 -0
  34. package/dist/types/audio.js +2 -0
  35. package/dist/types/audio.js.map +1 -0
  36. package/dist/types/base.d.ts +22 -0
  37. package/dist/types/base.d.ts.map +1 -0
  38. package/dist/types/base.js +2 -0
  39. package/dist/types/base.js.map +1 -0
  40. package/dist/types/config.d.ts +26 -0
  41. package/dist/types/config.d.ts.map +1 -0
  42. package/dist/types/config.js +2 -0
  43. package/dist/types/config.js.map +1 -0
  44. package/dist/types/events.d.ts +29 -0
  45. package/dist/types/events.d.ts.map +1 -0
  46. package/dist/types/events.js +2 -0
  47. package/dist/types/events.js.map +1 -0
  48. package/dist/types/index.d.ts +24 -0
  49. package/dist/types/index.d.ts.map +1 -0
  50. package/dist/types/index.js +12 -0
  51. package/dist/types/index.js.map +1 -0
  52. package/dist/types/language.d.ts +14 -0
  53. package/dist/types/language.d.ts.map +1 -0
  54. package/dist/types/language.js +15 -0
  55. package/dist/types/language.js.map +1 -0
  56. package/dist/types/system.d.ts +17 -0
  57. package/dist/types/system.d.ts.map +1 -0
  58. package/dist/types/system.js +2 -0
  59. package/dist/types/system.js.map +1 -0
  60. package/dist/types/text.d.ts +41 -0
  61. package/dist/types/text.d.ts.map +1 -0
  62. package/dist/types/text.js +2 -0
  63. package/dist/types/text.js.map +1 -0
  64. package/dist/types/types.d.ts +101 -0
  65. package/dist/types/types.d.ts.map +1 -0
  66. package/dist/types/types.js +120 -0
  67. package/dist/types/types.js.map +1 -0
  68. package/dist/utils/audio-queue.d.ts +39 -0
  69. package/dist/utils/audio-queue.d.ts.map +1 -0
  70. package/dist/utils/audio-queue.js +71 -0
  71. package/dist/utils/audio-queue.js.map +1 -0
  72. package/dist/utils/buffer.d.ts +11 -0
  73. package/dist/utils/buffer.d.ts.map +1 -0
  74. package/dist/utils/buffer.js +56 -0
  75. package/dist/utils/buffer.js.map +1 -0
  76. package/dist/utils/message.d.ts +14 -0
  77. package/dist/utils/message.d.ts.map +1 -0
  78. package/dist/utils/message.js +52 -0
  79. package/dist/utils/message.js.map +1 -0
  80. package/dist/utils/socket.d.ts +26 -0
  81. package/dist/utils/socket.d.ts.map +1 -0
  82. package/dist/utils/socket.js +27 -0
  83. package/dist/utils/socket.js.map +1 -0
  84. package/package.json +46 -0
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Sarvam AI
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
package/README.md ADDED
@@ -0,0 +1,208 @@
1
+ # Sarvam Conversational AI SDK - TypeScript
2
+
3
+ TypeScript SDK for building real-time voice-to-voice conversational AI applications in the browser.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @sarvam/conv-ai-sdk
9
+ ```
10
+
11
+ For React applications:
12
+ ```bash
13
+ npm install react react-dom
14
+ npm install --save-dev @types/react @types/react-dom
15
+ ```
16
+
17
+ ## Quick Start
18
+
19
+ Here's a complete React component example for voice-to-voice interaction:
20
+
21
+ ```tsx
22
+ import React, { useEffect, useRef, useState } from 'react';
23
+ import {
24
+ AsyncSamvaadAgent,
25
+ BrowserAudioInterface,
26
+ InteractionConfig,
27
+ InteractionType,
28
+ ServerTextChunkMsg,
29
+ UserIdentifierType,
30
+ } from '@sarvam/conv-ai-sdk';
31
+
32
+ interface VoiceChatProps {
33
+ apiKey: string;
34
+ appId: string;
35
+ orgId: string;
36
+ workspaceId: string;
37
+ userId?: string;
38
+ }
39
+
40
+ export const VoiceChat: React.FC<VoiceChatProps> = ({
41
+ apiKey,
42
+ appId,
43
+ orgId,
44
+ workspaceId,
45
+ userId = 'react_user',
46
+ }) => {
47
+ const [isConnected, setIsConnected] = useState(false);
48
+ const [isConnecting, setIsConnecting] = useState(false);
49
+ const [transcript, setTranscript] = useState<string>('');
50
+ const agentRef = useRef<AsyncSamvaadAgent | null>(null);
51
+
52
+ const handleText = async (msg: ServerTextChunkMsg) => {
53
+ setTranscript(prev => prev + msg.text);
54
+ };
55
+
56
+ const startConversation = async () => {
57
+ if (isConnecting || isConnected) return;
58
+
59
+ setIsConnecting(true);
60
+
61
+ try {
62
+ const config: InteractionConfig = {
63
+ user_identifier_type: UserIdentifierType.CUSTOM,
64
+ user_identifier: userId,
65
+ org_id: orgId,
66
+ workspace_id: workspaceId,
67
+ app_id: appId,
68
+ interaction_type: InteractionType.CALL,
69
+ sample_rate: 16000,
70
+ };
71
+
72
+ const audioInterface = new BrowserAudioInterface();
73
+
74
+ const agent = new AsyncSamvaadAgent({
75
+ apiKey,
76
+ config,
77
+ textCallback: handleText,
78
+ audioInterface,
79
+ });
80
+
81
+ agentRef.current = agent;
82
+
83
+ await agent.start();
84
+ const connected = await agent.waitForConnect(5.0);
85
+
86
+ if (connected) {
87
+ setIsConnected(true);
88
+ setIsConnecting(false);
89
+
90
+ agent.waitForDisconnect().then(() => {
91
+ setIsConnected(false);
92
+ });
93
+ } else {
94
+ setIsConnecting(false);
95
+ alert('Failed to connect. Please check your credentials.');
96
+ }
97
+ } catch (error) {
98
+ setIsConnecting(false);
99
+ console.error('Error starting conversation:', error);
100
+ alert(`Error: ${error instanceof Error ? error.message : String(error)}`);
101
+ }
102
+ };
103
+
104
+ const stopConversation = async () => {
105
+ if (agentRef.current) {
106
+ try {
107
+ await agentRef.current.stop();
108
+ agentRef.current = null;
109
+ setIsConnected(false);
110
+ setTranscript('');
111
+ } catch (error) {
112
+ console.error('Error stopping conversation:', error);
113
+ }
114
+ }
115
+ };
116
+
117
+ useEffect(() => {
118
+ return () => {
119
+ if (agentRef.current) {
120
+ agentRef.current.stop().catch(console.error);
121
+ }
122
+ };
123
+ }, []);
124
+
125
+ return (
126
+ <div style={{ padding: '20px', maxWidth: '600px', margin: '0 auto' }}>
127
+ <h2>Voice Chat</h2>
128
+
129
+ <div style={{ marginBottom: '20px' }}>
130
+ {!isConnected && !isConnecting && (
131
+ <button onClick={startConversation}>
132
+ Start Voice Chat
133
+ </button>
134
+ )}
135
+
136
+ {isConnecting && <div>Connecting...</div>}
137
+
138
+ {isConnected && (
139
+ <button onClick={stopConversation}>
140
+ Stop Voice Chat
141
+ </button>
142
+ )}
143
+ </div>
144
+
145
+ {isConnected && (
146
+ <div>
147
+ <strong>Agent Response:</strong>
148
+ <div>{transcript || 'Listening...'}</div>
149
+ </div>
150
+ )}
151
+ </div>
152
+ );
153
+ };
154
+ ```
155
+
156
+ ## Configuration
157
+
158
+ ### AsyncSamvaadAgent Parameters
159
+
160
+ | Parameter | Type | Required | Description |
161
+ | --- | --- | --- | --- |
162
+ | apiKey | string | Yes | API key for authentication |
163
+ | config | InteractionConfig | Yes | Interaction configuration |
164
+ | audioInterface | AsyncAudioInterface | No | Audio interface for mic/speaker (required for voice) |
165
+ | textCallback | (msg: ServerTextChunkMsg) => Promise<void> | No | Receives streaming text chunks |
166
+ | eventCallback | (event: ServerEventBase) => Promise<void> | No | Receives events |
167
+ | audioCallback | (msg: ServerAudioChunkMsg) => Promise<void> | No | Receives audio chunks (if not using audioInterface) |
168
+
169
+ ### InteractionConfig
170
+
171
+ Required fields:
172
+ - `user_identifier_type`: `CUSTOM`, `EMAIL`, `PHONE_NUMBER`, or `UNKNOWN`
173
+ - `user_identifier`: User identifier value
174
+ - `org_id`: Your organization ID
175
+ - `workspace_id`: Your workspace ID
176
+ - `app_id`: Application ID
177
+ - `interaction_type`: `InteractionType.CALL`
178
+ - `sample_rate`: `8000` or `16000`
179
+
180
+ Optional:
181
+ - `agent_variables`: Key-value pairs for agent context
182
+ - `initial_language_name`: Starting language (e.g., "English", "Hindi")
183
+ - `initial_state_name`: Starting state name
184
+ - `initial_bot_message`: First message from agent
185
+
186
+ ### Methods
187
+
188
+ - `await agent.start()` — Start and connect
189
+ - `await agent.stop()` — Stop and cleanup
190
+ - `await agent.waitForConnect(timeout?)` — Wait until connected
191
+ - `await agent.waitForDisconnect()` — Wait until disconnected
192
+ - `agent.isConnected()` — Check connection status
193
+ - `agent.getInteractionId()` — Get current interaction ID
194
+
195
+ ## Examples
196
+
197
+ Check out the [`examples/web`](./examples/web) directory for a complete, production-ready React + TypeScript web application demonstrating:
198
+ - Voice-to-voice AI conversation
199
+ - Modern, responsive UI with dark/light mode
200
+ - Real-time transcript display
201
+ - Audio level visualization
202
+ - Comprehensive error handling
203
+
204
+ See the [example README](./examples/web/README.md) for setup instructions.
205
+
206
+ ## License
207
+
208
+ MIT
@@ -0,0 +1,92 @@
1
+ /**
2
+ * Unified conversational AI agent using factory pattern.
3
+ *
4
+ * This module provides a single ConversationAgent class that internally
5
+ * instantiates the appropriate agent type (VoiceAgent or TextAgent) based
6
+ * on the interaction_type in the configuration.
7
+ */
8
+ import { VoiceAgent } from "./modules/voice-agent";
9
+ import { TextAgent } from "./modules/text-agent";
10
+ import { AsyncAudioInterface, Platform } from "./types";
11
+ import { InteractionConfig, ServerAudioChunkMsg, ServerEventBase, ServerTextChunkMsg, ServerTextMsgType } from "./types";
12
+ export declare class ConversationAgent {
13
+ private agent;
14
+ constructor(options: {
15
+ apiKey: string;
16
+ config: InteractionConfig;
17
+ audioInterface?: AsyncAudioInterface;
18
+ audioCallback?: (msg: ServerAudioChunkMsg) => Promise<void>;
19
+ eventCallback?: (event: ServerEventBase) => Promise<void>;
20
+ startCallback?: () => Promise<void>;
21
+ endCallback?: () => Promise<void>;
22
+ textCallback?: (msg: ServerTextChunkMsg | ServerTextMsgType) => Promise<void>;
23
+ baseUrl?: string;
24
+ proxy?: boolean;
25
+ platform?: Platform;
26
+ });
27
+ /**
28
+ * Get the reference ID (call SID)
29
+ */
30
+ get reference_id(): string;
31
+ set reference_id(referenceId: string);
32
+ /**
33
+ * Start the conversation session
34
+ */
35
+ start(): Promise<void>;
36
+ /**
37
+ * Stop the conversation session
38
+ */
39
+ stop(): Promise<void>;
40
+ /**
41
+ * Wait until the WebSocket disconnects
42
+ */
43
+ waitForDisconnect(): Promise<void>;
44
+ /**
45
+ * Wait until the WebSocket connection is established
46
+ */
47
+ waitForConnect(timeout?: number): Promise<boolean>;
48
+ /**
49
+ * Check if the WebSocket is currently connected
50
+ */
51
+ isConnected(): boolean;
52
+ /**
53
+ * Get the current interaction identifier
54
+ */
55
+ getInteractionId(): string | undefined;
56
+ /**
57
+ * Send audio data (only available for voice/call interactions)
58
+ */
59
+ sendAudio(audioData: Uint8Array): Promise<void>;
60
+ /**
61
+ * Send text message (only available for text/chat interactions)
62
+ */
63
+ sendText(text: string): Promise<void>;
64
+ /**
65
+ * Get the underlying agent instance type
66
+ */
67
+ getAgentType(): "voice" | "text";
68
+ /**
69
+ * Check if this is a voice agent
70
+ */
71
+ isVoiceAgent(): this is {
72
+ agent: VoiceAgent;
73
+ };
74
+ /**
75
+ * Check if this is a text agent
76
+ */
77
+ isTextAgent(): this is {
78
+ agent: TextAgent;
79
+ };
80
+ }
81
+ export declare function createConversation(config: InteractionConfig, apiKey: string, options?: {
82
+ audioInterface?: AsyncAudioInterface;
83
+ audioCallback?: (msg: ServerAudioChunkMsg) => Promise<void>;
84
+ eventCallback?: (event: ServerEventBase) => Promise<void>;
85
+ textCallback?: (msg: ServerTextChunkMsg | ServerTextMsgType) => Promise<void>;
86
+ startCallback?: () => Promise<void>;
87
+ endCallback?: () => Promise<void>;
88
+ baseUrl?: string;
89
+ proxy?: boolean;
90
+ platform?: Platform;
91
+ }): Promise<ConversationAgent>;
92
+ //# sourceMappingURL=conversation.d.ts.map
@@ -0,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IAChB,QAAQ,CAAC,EAAE,QAAQ,CAAC;CACrB,GACA,OAAO,CAAC,iBAAiB,CAAC,CAiB5B"}
@@ -0,0 +1,154 @@
1
+ /**
2
+ * Unified conversational AI agent using factory pattern.
3
+ *
4
+ * This module provides a single ConversationAgent class that internally
5
+ * instantiates the appropriate agent type (VoiceAgent or TextAgent) based
6
+ * on the interaction_type in the configuration.
7
+ */
8
+ import { VoiceAgent } from "./modules/voice-agent";
9
+ import { TextAgent } from "./modules/text-agent";
10
+ import { InteractionType } from "./types";
11
+ export class ConversationAgent {
12
+ agent;
13
+ constructor(options) {
14
+ const interactionType = options.config.interaction_type;
15
+ // Factory logic: decide which agent to instantiate based on interaction_type
16
+ if (interactionType === InteractionType.CALL) {
17
+ // Create VoiceAgent for voice/call interactions
18
+ this.agent = new VoiceAgent({
19
+ apiKey: options.apiKey,
20
+ config: options.config,
21
+ audioInterface: options.audioInterface,
22
+ audioCallback: options.audioCallback,
23
+ eventCallback: options.eventCallback,
24
+ startCallback: options.startCallback,
25
+ endCallback: options.endCallback,
26
+ textCallback: options.textCallback,
27
+ baseUrl: options.baseUrl,
28
+ proxy: options.proxy,
29
+ platform: options.platform,
30
+ });
31
+ }
32
+ else if (interactionType === InteractionType.TEXT) {
33
+ // Create TextAgent for text/chat interactions
34
+ this.agent = new TextAgent({
35
+ apiKey: options.apiKey,
36
+ config: options.config,
37
+ eventCallback: options.eventCallback,
38
+ startCallback: options.startCallback,
39
+ endCallback: options.endCallback,
40
+ textCallback: options.textCallback,
41
+ baseUrl: options.baseUrl || "https://f7dae5ca1fb6.ngrok-free.app/",
42
+ proxy: options.proxy,
43
+ platform: options.platform,
44
+ });
45
+ }
46
+ else {
47
+ throw new Error(`Unsupported interaction_type: ${interactionType}. ` +
48
+ `Supported types are: ${InteractionType.CALL}, ${InteractionType.TEXT}`);
49
+ }
50
+ }
51
+ /**
52
+ * Get the reference ID (call SID)
53
+ */
54
+ get reference_id() {
55
+ return this.agent.reference_id;
56
+ }
57
+ set reference_id(referenceId) {
58
+ this.agent.reference_id = referenceId;
59
+ }
60
+ /**
61
+ * Start the conversation session
62
+ */
63
+ async start() {
64
+ await this.agent.start();
65
+ }
66
+ /**
67
+ * Stop the conversation session
68
+ */
69
+ async stop() {
70
+ await this.agent.stop();
71
+ }
72
+ /**
73
+ * Wait until the WebSocket disconnects
74
+ */
75
+ async waitForDisconnect() {
76
+ await this.agent.waitForDisconnect();
77
+ }
78
+ /**
79
+ * Wait until the WebSocket connection is established
80
+ */
81
+ async waitForConnect(timeout) {
82
+ return await this.agent.waitForConnect(timeout);
83
+ }
84
+ /**
85
+ * Check if the WebSocket is currently connected
86
+ */
87
+ isConnected() {
88
+ return this.agent.isConnected();
89
+ }
90
+ /**
91
+ * Get the current interaction identifier
92
+ */
93
+ getInteractionId() {
94
+ return this.agent.getInteractionId();
95
+ }
96
+ /**
97
+ * Send audio data (only available for voice/call interactions)
98
+ */
99
+ async sendAudio(audioData) {
100
+ if (this.agent instanceof VoiceAgent) {
101
+ await this.agent.sendAudio(audioData);
102
+ }
103
+ else {
104
+ throw new Error("sendAudio() is only available for voice/call interactions");
105
+ }
106
+ }
107
+ /**
108
+ * Send text message (only available for text/chat interactions)
109
+ */
110
+ async sendText(text) {
111
+ if (this.agent instanceof TextAgent) {
112
+ await this.agent.sendText(text);
113
+ }
114
+ else {
115
+ throw new Error("sendText() is only available for text/chat interactions");
116
+ }
117
+ }
118
+ /**
119
+ * Get the underlying agent instance type
120
+ */
121
+ getAgentType() {
122
+ return this.agent instanceof VoiceAgent ? "voice" : "text";
123
+ }
124
+ /**
125
+ * Check if this is a voice agent
126
+ */
127
+ isVoiceAgent() {
128
+ return this.agent instanceof VoiceAgent;
129
+ }
130
+ /**
131
+ * Check if this is a text agent
132
+ */
133
+ isTextAgent() {
134
+ return this.agent instanceof TextAgent;
135
+ }
136
+ }
137
+ export async function createConversation(config, apiKey, options) {
138
+ const agent = new ConversationAgent({
139
+ apiKey,
140
+ config,
141
+ audioInterface: options?.audioInterface,
142
+ audioCallback: options?.audioCallback,
143
+ eventCallback: options?.eventCallback,
144
+ textCallback: options?.textCallback,
145
+ startCallback: options?.startCallback,
146
+ endCallback: options?.endCallback,
147
+ baseUrl: options?.baseUrl,
148
+ proxy: options?.proxy,
149
+ platform: options?.platform,
150
+ });
151
+ await agent.start();
152
+ return agent;
153
+ }
154
+ //# sourceMappingURL=conversation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"conversation.js","sourceRoot":"","sources":["../src/conversation.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAuB,eAAe,EAAY,MAAM,SAAS,CAAC;AAWzE,MAAM,OAAO,iBAAiB;IACpB,KAAK,CAAgB;IAE7B,YAAY,OAcX;QACC,MAAM,eAAe,GAAG,OAAO,CAAC,MAAM,CAAC,gBAAgB,CAAC;QAExD,6EAA6E;QAC7E,IAAI,eAAe,KAAK,eAAe,CAAC,IAAI,EAAE,CAAC;YAC7C,gDAAgD;YAChD,IAAI,CAAC,KAAK,GAAG,IAAI,UAAU,CAAC;gBAC1B,MAAM,EAAE,OAAO,CAAC,MAAM;gBACtB,MAAM,EAAE,OAAO,CAAC,MAAM;gBACtB,cAAc,EAAE,OAAO,CAAC,cAAc;gBACtC,aAAa,EAAE,OAAO,CAAC,aAAa;gBACpC,aAAa,EAAE,OAAO,CAAC,aAAa;gBACpC,aAAa,EAAE,OAAO,CAAC,aAAa;gBACpC,WAAW,EAAE,OAAO,CAAC,WAAW;gBAChC,YAAY,EAAE,OAAO,CAAC,YAAY;gBAClC,OAAO,EAAE,OAAO,CAAC,OAAO;gBACxB,KAAK,EAAE,OAAO,CAAC,KAAK;gBACpB,QAAQ,EAAE,OAAO,CAAC,QAAQ;aAC3B,CAAC,CAAC;QACL,CAAC;aAAM,IAAI,eAAe,KAAK,e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OAAO,EAAE,aAAa;QACrC,aAAa,EAAE,OAAO,EAAE,aAAa;QACrC,YAAY,EAAE,OAAO,EAAE,YAAY;QACnC,aAAa,EAAE,OAAO,EAAE,aAAa;QACrC,WAAW,EAAE,OAAO,EAAE,WAAW;QACjC,OAAO,EAAE,OAAO,EAAE,OAAO;QACzB,KAAK,EAAE,OAAO,EAAE,KAAK;QACrB,QAAQ,EAAE,OAAO,EAAE,QAAQ;KAC5B,CAAC,CAAC;IAEH,MAAM,KAAK,CAAC,KAAK,EAAE,CAAC;IACpB,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -0,0 +1,6 @@
1
+ export { ConversationAgent, createConversation } from "./conversation";
2
+ export type { Platform } from "./types";
3
+ export type { AsyncAudioInterface, AudioData } from "./types";
4
+ export { BrowserAudioInterface } from "./interfaces/browser";
5
+ export * from "./types";
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACvE,YAAY,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAGxC,YAAY,EAAE,mBAAmB,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAG9D,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AAG7D,cAAc,SAAS,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ // Unified conversation agent
2
+ export { ConversationAgent, createConversation } from "./conversation";
3
+ // Browser audio interfaces
4
+ export { BrowserAudioInterface } from "./interfaces/browser";
5
+ // Messages and types
6
+ export * from "./types";
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6BAA6B;AAC7B,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAMvE,2BAA2B;AAC3B,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AAE7D,qBAAqB;AACrB,cAAc,SAAS,CAAC"}
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Browser-based audio interface implementations using Web Audio API.
3
+ *
4
+ * These interfaces provide audio input/output capabilities for web applications
5
+ * using the conversational AI SDK in browser environments.
6
+ */
7
+ import { AsyncAudioInterface, AudioData } from "../types";
8
+ /**
9
+ * Browser audio interface for capturing microphone input and playing audio output.
10
+ * Uses Web Audio API for both recording and playback.
11
+ *
12
+ * Features:
13
+ * - Microphone capture with configurable sample rate
14
+ * - Audio resampling to match server requirements
15
+ * - Playback with automatic context management
16
+ * - Interrupt support for stopping ongoing playback
17
+ */
18
+ export declare class BrowserAudioInterface implements AsyncAudioInterface {
19
+ private audioContext?;
20
+ private mediaStream?;
21
+ private audioWorkletNode?;
22
+ private scriptProcessorNode?;
23
+ private sourceNode?;
24
+ private inputCallback?;
25
+ private sampleRate;
26
+ private isRecording;
27
+ private playbackQueue;
28
+ private nextPlaybackTime;
29
+ private readonly BUFFER_THRESHOLD;
30
+ /**
31
+ * Create a new browser audio interface
32
+ * @param sampleRate - Target sample rate for audio capture (default: 16000 Hz)
33
+ */
34
+ constructor(sampleRate?: number);
35
+ /**
36
+ * Start capturing audio from the microphone
37
+ * @param inputCallback - Callback to receive audio chunks
38
+ */
39
+ start(inputCallback: (audioData: AudioData, frameCount: number) => Promise<void>): Promise<void>;
40
+ /**
41
+ * Stop capturing audio and clean up resources
42
+ */
43
+ stop(): Promise<void>;
44
+ /**
45
+ * Output audio to the speakers
46
+ * @param audio - Audio data in 16-bit PCM mono format
47
+ * @param sampleRate - Sample rate of the audio (default: 16000 Hz)
48
+ */
49
+ output(audio: AudioData, sampleRate?: number): Promise<void>;
50
+ /**
51
+ * Interrupt and stop all ongoing audio playback
52
+ */
53
+ interrupt(): void;
54
+ /**
55
+ * Setup audio processing using AudioWorklet (modern approach)
56
+ */
57
+ private setupAudioWorklet;
58
+ /**
59
+ * Setup audio processing using ScriptProcessor (fallback for older browsers)
60
+ */
61
+ private setupScriptProcessor;
62
+ /**
63
+ * Convert Float32 audio data to Int16 PCM format
64
+ */
65
+ private float32ToInt16;
66
+ /**
67
+ * Convert Int16 PCM audio data to Float32 format
68
+ */
69
+ private int16ToFloat32;
70
+ }
71
+ /**
72
+ * Buffered audio interface for browsers with unreliable network connections.
73
+ * Buffers audio chunks before playback to ensure smooth playback.
74
+ * Enable if needed for slower networks.
75
+ */
76
+ //# sourceMappingURL=browser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../../src/interfaces/browser.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,mBAAmB,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAE1D;;;;;;;;;GASG;AACH,qBAAa,qBAAsB,YAAW,mBAAmB;IAC/D,OAAO,CAAC,YAAY,CAAC,CAAe;IACpC,OAAO,CAAC,WAAW,CAAC,CAAc;IAClC,OAAO,CAAC,gBAAgB,CAAC,CAAmB;IAC5C,OAAO,CAAC,mBAAmB,CAAC,CAAsB;IAClD,OAAO,CAAC,UAAU,CAAC,CAA6B;IAChD,OAAO,CAAC,aAAa,CAAC,CAGH;IACnB,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,aAAa,CAA+B;IACpD,OAAO,CAAC,gBAAgB,CAAa;IACrC,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAe;IAEhD;;;OAGG;gBACS,UAAU,GAAE,MAAc;IAItC;;;OAGG;IACG,KAAK,CACT,aAAa,EAAE,CAAC,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,GACzE,OAAO,CAAC,IAAI,CAAC;IAyChB;;OAEG;IACG,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAwC3B;;;;OAIG;IACG,MAAM,CAAC,KAAK,EAAE,SAAS,EAAE,UAAU,GAAE,MAAc,GAAG,OAAO,CAAC,IAAI,CAAC;IAuEzE;;OAEG;IACH,SAAS,IAAI,IAAI;IAcjB;;OAEG;YACW,iBAAiB;IAyC/B;;OAEG;IACH,OAAO,CAAC,oBAAoB;IAwB5B;;OAEG;IACH,OAAO,CAAC,cAAc;IAStB;;OAEG;IACH,OAAO,CAAC,cAAc;CAQvB;AAED;;;;GAIG"}