@hamsa-ai/voice-agents-sdk 0.2.0 → 0.2.2

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,170 +0,0 @@
1
- import AudioPlayer from './audio_player'
2
- import AudioRecorder from './audio_recorder'
3
-
4
- export default class WebSocketManager {
5
- constructor(
6
- url,
7
- conversationId,
8
- onError,
9
- onStart,
10
- onTransciprtionRecieved,
11
- onAnswerRecieved,
12
- onSpeaking,
13
- onListening,
14
- onClosed,
15
- voiceEnablement,
16
- tools,
17
- apiKey
18
- ) {
19
- this.url = `${url}/${conversationId}?api_key=${apiKey}`;
20
- this.ws = null;
21
- this.isConnected = false;
22
- this.audioPlayer = null;
23
- this.audioRecorder = null;
24
- this.last_transcription_date = new Date();
25
- this.last_voice_byte_date = new Date();
26
- this.is_media = false;
27
- this.onErrorCB = onError;
28
- this.onStartCB = onStart;
29
- this.onTransciprtionRecievedCB = onTransciprtionRecieved;
30
- this.onAnswerRecievedCB = onAnswerRecieved;
31
- this.onSpeakingCB = onSpeaking;
32
- this.onListeningCB = onListening;
33
- this.onClosedCB = onClosed;
34
- this.voiceEnablement = voiceEnablement;
35
- this.tools = tools;
36
- this.apiKey = apiKey;
37
- this.setVolume = this.setVolume.bind(this);
38
- }
39
-
40
- setVolume(volume) {
41
- this.audioPlayer.setVolume(volume);
42
- }
43
-
44
- startCall() {
45
- try {
46
- if (!this.ws) {
47
- this.ws = new WebSocket(this.url);
48
- this.ws.onopen = this.onOpen.bind(this);
49
- this.ws.onmessage = this.onMessage.bind(this);
50
- this.ws.onclose = this.onClose.bind(this);
51
- this.ws.onerror = this.onError.bind(this);
52
- this.audioPlayer = new AudioPlayer(this.ws, this.onSpeakingCB, this.onListeningCB)
53
- this.audioRecorder = new AudioRecorder()
54
- }
55
- }catch(e) {
56
- console.log(e)
57
- }
58
- }
59
-
60
- onOpen() {
61
- this.ws.send(JSON.stringify({ event: 'start', streamSid: 'WEBSDK' }));
62
- this.isConnected = true;
63
- this.audioRecorder.startStreaming(this.ws);
64
- if (this.onStartCB) this.onStartCB()
65
- }
66
-
67
- onMessage(event) {
68
- const message = JSON.parse(event.data);
69
- switch (message.event) {
70
- case 'media':
71
- if (message.media) {
72
- this.audioPlayer.enqueueAudio(message.media.payload);
73
- }
74
- break;
75
- case 'clear':
76
- this.audioPlayer.stopAndClear();
77
- break;
78
- case 'mark':
79
- this.audioPlayer.addMark(message.mark.name);
80
- break;
81
- case 'transcription':
82
- if (this.onTransciprtionRecievedCB) this.onTransciprtionRecievedCB(message.content)
83
- break;
84
- case 'answer':
85
- if (this.onAnswerRecievedCB) this.onAnswerRecievedCB(message.content)
86
- break;
87
- case 'tools':
88
- const tools_response = this.run_tools(message.content)
89
- this.ws.send(JSON.stringify({ event: 'tools_response', tools_response: tools_response, streamSid: 'WEBSDK' }));
90
- break;
91
- default:
92
- break;
93
- }
94
- }
95
-
96
- onClose(event) {
97
- if (this.onClosedCB) this.onClosedCB()
98
- this.audioPlayer.stopAndClear();
99
- this.audioRecorder.stop();
100
- this.isConnected = false;
101
- this.ws = null
102
- }
103
-
104
- onError(error) {
105
- if (this.onErrorCB) this.onErrorCB(error)
106
-
107
- }
108
-
109
- endCall() {
110
- if (this.ws) {
111
- this.audioPlayer.stopAndClear();
112
- this.ws.send(JSON.stringify({ event: 'stop' }));
113
- this.audioRecorder.stop();
114
- this.#closeWebSocket()
115
- if (this.onClosedCB) this.onClosedCB()
116
- }
117
- }
118
-
119
- pauseCall() {
120
- this.audioPlayer.pause()
121
- this.audioRecorder.pause()
122
- }
123
-
124
- resumeCall() {
125
- this.audioPlayer.resume()
126
- this.audioRecorder.resume()
127
- }
128
-
129
- run_tools(tools_array) {
130
- const results = [];
131
- tools_array.forEach(item => {
132
- if (item.type === 'function') {
133
- const selected_function = this.#findFunctionByName(item.function.name)
134
- const functionName = item.function.name;
135
- const functionArgs = JSON.parse(item.function.arguments);
136
- if (selected_function && typeof selected_function["fn"] === 'function') {
137
- const response = selected_function["fn"](...Object.values(functionArgs));
138
- results.push({
139
- id: item.id,
140
- function: {
141
- name: functionName,
142
- response: response
143
- }
144
- });
145
- } else {
146
- results.push({
147
- id: item.id,
148
- function: {
149
- name: functionName,
150
- response: "Error could not find the function"
151
- }
152
- });
153
- console.log(`Function ${functionName} is not defined`);
154
- }
155
- }
156
- });
157
-
158
- return results;
159
- }
160
-
161
- #findFunctionByName(functionName) {
162
- return this.tools.find(item => item.function_name === functionName) || null;
163
- }
164
-
165
- #closeWebSocket() {
166
- if (this.ws.readyState === WebSocket.OPEN) {
167
- this.ws.close(1000, 'Normal Closure');
168
- }
169
- }
170
- }
package/src/main.js DELETED
@@ -1,178 +0,0 @@
1
- import WebSocketManager from './classes/websocket_manager';
2
- import { EventEmitter } from 'events';
3
-
4
- export class HamsaVoiceAgent extends EventEmitter {
5
- constructor(apiKey) {
6
- super();
7
- this.webSocketManager = null;
8
- this.apiKey = apiKey;
9
- this.API_URL = "https://api.tryhamsa.com"
10
- this.WS_URL = "wss://bots.tryhamsa.com/stream"
11
- }
12
-
13
- setVolume(volume) {
14
- this.webSocketManager.setVolume(volume);
15
- }
16
-
17
- async start({
18
- agentId = null,
19
- params = {},
20
- voiceEnablement = false,
21
- tools = []
22
- }) {
23
- try {
24
- const conversationId = await this.#init_conversation(agentId, params, voiceEnablement, tools);
25
- if (!conversationId) {
26
- throw new Error("Failed to initialize conversation.");
27
- }
28
- this.jobId = conversationId; // Store the jobId
29
- this.webSocketManager = new WebSocketManager(
30
- this.WS_URL,
31
- conversationId,
32
- (error) => this.emit('error', error),
33
- () => this.emit('start'),
34
- (transcription) => this.emit('transcriptionReceived', transcription),
35
- (answer) => this.emit('answerReceived', answer),
36
- () => this.emit('speaking'),
37
- () => this.emit('listening'),
38
- () => this.emit('closed'),
39
- voiceEnablement,
40
- tools,
41
- this.apiKey
42
- );
43
- this.webSocketManager.startCall();
44
- this.emit('callStarted');
45
- } catch (e) {
46
- this.emit('error', new Error("Error in starting the call! Make sure you initialized the client with init()."));
47
- }
48
- }
49
-
50
- end() {
51
- try {
52
- this.webSocketManager.endCall();
53
- this.emit('callEnded');
54
- } catch (e) {
55
- this.emit('error', new Error("Error in ending the call! Make sure you initialized the client with init()."));
56
- }
57
- }
58
-
59
- pause() {
60
- this.webSocketManager.pauseCall();
61
- this.emit('callPaused');
62
- }
63
-
64
- resume() {
65
- this.webSocketManager.resumeCall();
66
- this.emit('callResumed');
67
- }
68
-
69
- /**
70
- * Retrieves job details from the Hamsa API using the stored jobId.
71
- * Implements retry logic with exponential backoff.
72
- * @param {number} [maxRetries=5] - Maximum number of retry attempts.
73
- * @param {number} [initialRetryInterval=1000] - Initial delay between retries in milliseconds.
74
- * @param {number} [backoffFactor=2] - Factor by which the retry interval increases each attempt.
75
- * @returns {Promise<Object>} Job details object.
76
- */
77
- async getJobDetails(maxRetries = 5, initialRetryInterval = 1000, backoffFactor = 2) {
78
- if (!this.jobId) {
79
- throw new Error("Cannot fetch job details: jobId is not set. Start a conversation first.");
80
- }
81
-
82
- const url = `${this.API_URL}/v1/job`;
83
- const headers = {
84
- "Authorization": `Token ${this.apiKey}`,
85
- "Content-Type": "application/json"
86
- };
87
- const params = new URLSearchParams({ jobId: this.jobId });
88
-
89
- let currentInterval = initialRetryInterval;
90
-
91
- const fetchJobDetails = async (attempt = 1) => {
92
- try {
93
- const response = await fetch(`${url}?${params.toString()}`, { method: 'GET', headers });
94
- if (!response.ok) {
95
- const errorText = await response.text();
96
- throw new Error(`API Error: ${response.status} ${response.statusText} - ${errorText}`);
97
- }
98
- const data = await response.json();
99
- // check if the message is COMPLETED in the data to decide if we should retry
100
- if (data.message === "COMPLETED") {
101
- return data;
102
- } else {
103
- throw new Error(`Job status is not COMPLETED: ${data.message}`);
104
- }
105
- } catch (error) {
106
- if (attempt < maxRetries) {
107
- console.warn(`Attempt ${attempt} failed: ${error.message}. Retrying in ${currentInterval / 1000} seconds...`);
108
- await this.#delay(currentInterval); // Wait before retrying
109
- currentInterval *= backoffFactor; // Increase the interval
110
- return fetchJobDetails(attempt + 1);
111
- } else {
112
- throw new Error(`Failed to fetch job details after ${maxRetries} attempts: ${error.message}`);
113
- }
114
- }
115
- };
116
-
117
- return fetchJobDetails();
118
- }
119
-
120
- async #init_conversation(voiceAgentId, params, voiceEnablement, tools) {
121
- const headers = {
122
- "Authorization": `Token ${this.apiKey}`,
123
- "Content-Type": "application/json"
124
- }
125
- const llmtools = (voiceEnablement && tools) ? this.#convertToolsToLLMTools(tools) : []
126
- const body = {
127
- voiceAgentId,
128
- params,
129
- voiceEnablement,
130
- tools: llmtools
131
- }
132
-
133
- const requestOptions = {
134
- method: "POST",
135
- headers: headers,
136
- body: JSON.stringify(body),
137
- redirect: "follow"
138
- };
139
-
140
- try {
141
- const response = await fetch(`${this.API_URL}/v1/voice-agents/conversation-init`, requestOptions);
142
- const result = await response.json();
143
- return result["data"]["jobId"]
144
- } catch (error) {
145
- this.emit('error', new Error("Error in initializing the call. Please double-check your API_KEY and ensure you have sufficient funds in your balance."));
146
- };
147
- }
148
-
149
- #convertToolsToLLMTools(tools) {
150
- return tools.map(item => ({
151
- type: "function",
152
- function: {
153
- name: item.function_name,
154
- description: item.description,
155
- parameters: {
156
- type: "object",
157
- properties: item.parameters?.reduce((acc, param) => {
158
- acc[param.name] = {
159
- type: param.type,
160
- description: param.description
161
- };
162
- return acc;
163
- }, {}) || {},
164
- required: item.required || []
165
- }
166
- }
167
- }));
168
- }
169
- /**
170
- * Delays execution for a specified amount of time.
171
- * @param {number} ms - Milliseconds to delay.
172
- * @returns {Promise} Promise that resolves after the delay.
173
- */
174
- #delay(ms) {
175
- return new Promise(resolve => setTimeout(resolve, ms));
176
- }
177
- }
178
-
package/tsconfig.json DELETED
@@ -1,14 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "declaration": true,
4
- "emitDeclarationOnly": true,
5
- "outDir": "./types",
6
- "target": "ES6",
7
- "module": "ES6",
8
- "allowJs": true,
9
- "moduleResolution": "node", // Try "nodenext" if "node" doesn't work
10
- "esModuleInterop": true
11
- },
12
- "include": ["src/**/*.js"],
13
- "exclude": ["types"]
14
- }
@@ -1 +0,0 @@
1
- declare const audioProcessorURL: "\nclass AudioPlayerProcessor extends AudioWorkletProcessor {\n constructor() {\n super();\n this.audioData = [];\n this.isPaused = false;\n this.marks = [];\n this.isDone = false;\n\n this.port.onmessage = (event) => {\n if (event.data.type === 'enqueue') {\n this.audioData.push(...event.data.audioSamples);\n this.isPaused = false;\n this.isDone = false;\n } else if (event.data.type === 'pause') {\n this.isPaused = true;\n } else if (event.data.type === 'resume') {\n this.isPaused = false;\n } else if (event.data.type === 'addMark') {\n this.marks.push(event.data.markName);\n } else if (event.data.type === 'clear') {\n this.clearAllData();\n }\n };\n }\n\n clearAllData() {\n this.audioData = []; // Clear the audio data buffer\n this.marks = []; // Clear any pending marks\n this.isPaused = true; // Optionally, pause processing to ensure no data is played\n }\n\n process(inputs, outputs) {\n const output = outputs[0];\n\n if (this.isPaused) {\n for (let channel = 0; channel < output.length; channel++) {\n output[channel].fill(0); // Output silence if paused or cleared\n }\n return true;\n }\n\n for (let channel = 0; channel < output.length; channel++) {\n const outputData = output[channel];\n const inputData = this.audioData.splice(0, outputData.length);\n\n if (inputData.length > 0) {\n outputData.set(inputData);\n } else {\n outputData.fill(0); // Output silence when no data is available\n }\n }\n if (this.audioData.length === 0 && !this.isDone) {\n this.isDone = true; \n this.port.postMessage({ type: 'finished' });\n }\n\n // Process marks if all audio data has been played\n if (this.marks.length > 0 && this.audioData.length === 0) {\n const mark_name = this.marks.shift();\n this.port.postMessage({ type: 'mark', markName: mark_name });\n }\n\n\n\n return true; // Keep the processor active\n }\n}\n\nregisterProcessor('audio-player-processor', AudioPlayerProcessor);\n";
@@ -1 +0,0 @@
1
- declare const audioRecorderProcessor: "\nclass AudioProcessor extends AudioWorkletProcessor {\n encodeBase64(bytes) {\n const base64abc = [\n 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',\n 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',\n 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',\n 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'\n ];\n\n let result = '';\n let i;\n const l = bytes.length;\n for (i = 2; i < l; i += 3) {\n result += base64abc[bytes[i - 2] >> 2];\n result += base64abc[((bytes[i - 2] & 0x03) << 4) | (bytes[i - 1] >> 4)];\n result += base64abc[((bytes[i - 1] & 0x0f) << 2) | (bytes[i] >> 6)];\n result += base64abc[bytes[i] & 0x3f];\n }\n if (i === l + 1) { // 1 octet yet to write\n result += base64abc[bytes[i - 2] >> 2];\n result += base64abc[(bytes[i - 2] & 0x03) << 4];\n result += '==';\n }\n if (i === l) { // 2 octets yet to write\n result += base64abc[bytes[i - 2] >> 2];\n result += base64abc[((bytes[i - 2] & 0x03) << 4) | (bytes[i - 1] >> 4)];\n result += base64abc[(bytes[i - 1] & 0x0f) << 2];\n result += '=';\n }\n return result;\n }\n\n process(inputs, outputs, parameters) {\n const input = inputs[0];\n if (input && input[0]) {\n const inputData = input[0];\n const rawAudioData = new Float32Array(inputData.length);\n rawAudioData.set(inputData);\n\n // Convert the audio data to a Uint8Array for base64 encoding\n const uint8Array = new Uint8Array(rawAudioData.buffer);\n\n // Use the custom base64 encoding function\n const base64String = this.encodeBase64(uint8Array);\n\n // Send the base64 string to the main thread via the port\n this.port.postMessage({ event: 'media', streamSid: 'WEBSDK', media: { payload: base64String } });\n }\n\n return true;\n }\n}\n\nregisterProcessor('audio-processor', AudioProcessor);\n";
@@ -1,3 +0,0 @@
1
- export class EvenetsListener {
2
- initListeners(): void;
3
- }
@@ -1,17 +0,0 @@
1
- export default class FancyButton {
2
- constructor(voiceEnablement: any);
3
- isCallStarted: boolean;
4
- iframe: HTMLIFrameElement;
5
- isIframeLoaded: boolean;
6
- voiceEnablement: any;
7
- createFloatingButton(WSManager: any): void;
8
- WSManager: any;
9
- toggleCall(): void;
10
- startCall(): void;
11
- endCall(): void;
12
- updateButtonAppearance(): void;
13
- startWaveAnimation(): void;
14
- stopWaveAnimation(): void;
15
- createIframe(callback: any): void;
16
- removeIframe(): void;
17
- }
package/webpack.config.js DELETED
@@ -1,44 +0,0 @@
1
- const path = require('path');
2
-
3
- module.exports = {
4
- entry: './src/main.js',
5
- output: {
6
- path: path.resolve(__dirname, 'dist'),
7
- filename: 'index.js',
8
- library: {
9
- name: 'Hamsa Voice-Agents SDK',
10
- type: 'umd', // UMD ensures compatibility across environments
11
- },
12
- globalObject: 'this',
13
- umdNamedDefine: true
14
- },
15
- target: 'web', // 'web' should cover most browser-based environments
16
- module: {
17
- rules: [
18
- {
19
- test: /\.worklet\.js/,
20
- loader: "audio-worklet-loader",
21
- options: {
22
- inline: "no-fallback",
23
- }
24
- },
25
- {
26
- test: /\.js$/,
27
- exclude: /node_modules/,
28
- use: 'babel-loader',
29
- },
30
- {
31
- test: /\.css$/,
32
- use: ['style-loader', 'css-loader'],
33
- },
34
- ]
35
- },
36
- resolve: {
37
- extensions: ['.js'],
38
- fallback: {
39
- "events": require.resolve("events/")
40
- }
41
- },
42
- mode: 'production',
43
- externals: [], // Define any external dependencies that should not be bundled
44
- };