@minded-ai/mindedjs 1.0.53-patch6 → 1.0.53

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 (53) hide show
  1. package/dist/agent.d.ts +9 -17
  2. package/dist/agent.d.ts.map +1 -1
  3. package/dist/agent.js +5 -76
  4. package/dist/agent.js.map +1 -1
  5. package/dist/index.d.ts +1 -3
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js.map +1 -1
  8. package/dist/nodes/addPromptNode.d.ts.map +1 -1
  9. package/dist/nodes/addPromptNode.js +0 -1
  10. package/dist/nodes/addPromptNode.js.map +1 -1
  11. package/dist/platform/config.d.ts +0 -3
  12. package/dist/platform/config.d.ts.map +1 -1
  13. package/dist/platform/config.js +1 -18
  14. package/dist/platform/config.js.map +1 -1
  15. package/dist/platform/mindedConnection.d.ts +4 -4
  16. package/dist/platform/mindedConnection.d.ts.map +1 -1
  17. package/dist/platform/mindedConnection.js +5 -8
  18. package/dist/platform/mindedConnection.js.map +1 -1
  19. package/dist/platform/mindedConnectionTypes.d.ts +2 -55
  20. package/dist/platform/mindedConnectionTypes.d.ts.map +1 -1
  21. package/dist/platform/mindedConnectionTypes.js +2 -11
  22. package/dist/platform/mindedConnectionTypes.js.map +1 -1
  23. package/dist/types/Agent.types.d.ts +0 -13
  24. package/dist/types/Agent.types.d.ts.map +1 -1
  25. package/dist/types/Agent.types.js.map +1 -1
  26. package/dist/types/Flows.types.d.ts +2 -15
  27. package/dist/types/Flows.types.d.ts.map +1 -1
  28. package/dist/types/Flows.types.js +0 -1
  29. package/dist/types/Flows.types.js.map +1 -1
  30. package/package.json +1 -4
  31. package/src/agent.ts +16 -93
  32. package/src/index.ts +0 -4
  33. package/src/nodes/addPromptNode.ts +0 -1
  34. package/src/platform/config.ts +1 -21
  35. package/src/platform/mindedConnection.ts +9 -15
  36. package/src/platform/mindedConnectionTypes.ts +2 -66
  37. package/src/types/Agent.types.ts +0 -14
  38. package/src/types/Flows.types.ts +1 -15
  39. package/dist/types/Voice.types.d.ts +0 -5
  40. package/dist/types/Voice.types.d.ts.map +0 -1
  41. package/dist/types/Voice.types.js +0 -3
  42. package/dist/types/Voice.types.js.map +0 -1
  43. package/dist/voice/elevenLabsUtils.d.ts +0 -70
  44. package/dist/voice/elevenLabsUtils.d.ts.map +0 -1
  45. package/dist/voice/elevenLabsUtils.js +0 -20
  46. package/dist/voice/elevenLabsUtils.js.map +0 -1
  47. package/dist/voice/voiceSession.d.ts +0 -47
  48. package/dist/voice/voiceSession.d.ts.map +0 -1
  49. package/dist/voice/voiceSession.js +0 -214
  50. package/dist/voice/voiceSession.js.map +0 -1
  51. package/src/types/Voice.types.ts +0 -4
  52. package/src/voice/elevenLabsUtils.ts +0 -101
  53. package/src/voice/voiceSession.ts +0 -257
@@ -1,257 +0,0 @@
1
- import { WebSocket } from 'ws';
2
- import { Agent } from '../agent';
3
- import {
4
- MindedConnectionSocketMessageType,
5
- } from '../platform/mindedConnectionTypes';
6
- import { getConfig } from '../platform/config';
7
- import {
8
- ElevenLabsWebSocketEvent,
9
- getSignedUrl,
10
- } from './elevenLabsUtils';
11
- import { AIMessage, BaseMessage, HumanMessage } from '@langchain/core/messages';
12
-
13
- /**
14
- * Voice Conversation class for managing individual ElevenLabs voice conversations
15
- */
16
- export class VoiceSession {
17
- private agent: Agent;
18
- private sessionId: string;
19
- private firstMessage: string;
20
- private voiceId?: string;
21
- private elevenLabsSocket?: WebSocket;
22
-
23
- private onAudioCallback?: (data: string) => void;
24
- private onInterruptionCallback?: () => void;
25
- private onMessageCallback?: (text: string, message: BaseMessage) => void;
26
- private onDisconnectCallback?: () => void;
27
-
28
- constructor({ agent, sessionId, firstMessage, voiceId }: { agent: Agent; sessionId: string; firstMessage: string; voiceId?: string }) {
29
- console.debug('Starting voice session', { sessionId, firstMessage });
30
- this.agent = agent;
31
- this.sessionId = sessionId;
32
- this.firstMessage = firstMessage;
33
- this.voiceId = voiceId;
34
- }
35
-
36
- /**
37
- * Initialize the voice conversation connection
38
- */
39
- public async init(): Promise<void> {
40
- const { elevenLabsKey, elevenLabsAgentId } = getConfig();
41
-
42
- if (!elevenLabsKey) {
43
- throw new Error('Missing ElevenLabs key - set ELEVEN_LABS_KEY env var');
44
- }
45
-
46
- if (!elevenLabsAgentId) {
47
- throw new Error('Missing ElevenLabs agent id - set ELEVEN_LABS_AGENT_ID env var');
48
- }
49
-
50
- const signedUrl = await getSignedUrl({
51
- agentId: elevenLabsAgentId,
52
- apiKey: elevenLabsKey
53
- });
54
-
55
- this.elevenLabsSocket = new WebSocket(signedUrl);
56
- this.setupSocketHandlers();
57
- }
58
-
59
- private async sendToElevenLabs(message: string): Promise<void> {
60
- if (!this.elevenLabsSocket) {
61
- throw new Error('Socket not initialized');
62
- }
63
- await this.waitForSocketOpen(this.elevenLabsSocket);
64
- this.elevenLabsSocket.send(message);
65
- }
66
-
67
- private async waitForSocketOpen(socket: WebSocket): Promise<void> {
68
- if (socket.readyState === WebSocket.OPEN) {
69
- return;
70
- }
71
- return new Promise((resolve, reject) => {
72
- const timeoutMs = 10000;
73
- const intervalMs = 100;
74
- let tries = 0;
75
- const interval = setInterval(() => {
76
- if (socket.readyState === WebSocket.OPEN) {
77
- clearInterval(interval);
78
- resolve();
79
- }
80
- tries++;
81
- if (tries >= timeoutMs) {
82
- clearInterval(interval);
83
- reject(new Error('Socket not open'));
84
- }
85
- }, intervalMs);
86
- });
87
- }
88
-
89
- private setupSocketHandlers(): void {
90
- const socket = this.elevenLabsSocket!; // non-null assertion once, we ensured it's assigned in init()
91
-
92
- socket.onopen = () => {
93
- console.debug('Connected to voice provider');
94
- const initiationData: ConversationInitiationClientData = {
95
- type: 'conversation_initiation_client_data',
96
- conversation_config_override: {
97
- agent: {
98
- first_message: this.firstMessage,
99
- language: 'en',
100
- },
101
- ...(this.voiceId
102
- ? {
103
- tts: {
104
- voice_id: this.voiceId,
105
- },
106
- }
107
- : {}),
108
- } as ConversationInitiationClientData['conversation_config_override'],
109
- };
110
-
111
- this.sendToElevenLabs(JSON.stringify(initiationData));
112
- this.sendToElevenLabs(JSON.stringify({ type: 'contextual_update', text: JSON.stringify({ sessionId: this.sessionId, mindedToken: getConfig().token! }) }));
113
- };
114
-
115
- socket.onclose = () => {
116
- console.debug('Disconnected from voice provider');
117
- this.onDisconnectCallback?.();
118
- this.agent.voiceSessions.delete(this.sessionId);
119
- };
120
-
121
- socket.onerror = (err: unknown) => {
122
- console.error('[ElevenLabsVoice] WebSocket error', err);
123
- };
124
-
125
- socket.onmessage = async (event: any) => {
126
- const data: ElevenLabsWebSocketEvent = JSON.parse(event.data.toString());
127
-
128
- switch (data.type) {
129
- case 'ping':
130
- setTimeout(() => {
131
- this.sendToElevenLabs(JSON.stringify({ type: 'pong', event_id: data.ping_event.event_id }));
132
- }, data.ping_event.ping_ms);
133
- break;
134
- case 'user_transcript':
135
- console.debug('User transcript received', data.user_transcription_event.user_transcript);
136
- if (this.onMessageCallback) {
137
- this.onMessageCallback(data.user_transcription_event.user_transcript, new HumanMessage(data.user_transcription_event.user_transcript));
138
- }
139
- break;
140
- case 'agent_response':
141
- if (this.onMessageCallback) {
142
- this.onMessageCallback(data.agent_response_event.agent_response, new AIMessage(data.agent_response_event.agent_response));
143
- }
144
- break;
145
- case 'interruption':
146
- console.debug('Interruption received');
147
- this.onInterruptionCallback?.();
148
- // Send interruption event to dashboard if connected
149
- if (getConfig().dashboardConnected) {
150
- try {
151
- this.agent.mindedConnection?.emit(MindedConnectionSocketMessageType.DASHBOARD_VOICE_INTERRUPTION, {
152
- type: MindedConnectionSocketMessageType.DASHBOARD_VOICE_INTERRUPTION,
153
- sessionId: this.sessionId,
154
- timestamp: Date.now(),
155
- });
156
- } catch (error) {
157
- console.error('[ElevenLabsVoice] Error sending interruption to dashboard', error);
158
- }
159
- }
160
- break;
161
- case 'audio':
162
- if (this.onAudioCallback) {
163
- this.onAudioCallback(data.audio_event.audio_base_64);
164
- }
165
- if (getConfig().dashboardConnected) {
166
- try {
167
- await this.agent.mindedConnection?.awaitEmit(MindedConnectionSocketMessageType.DASHBOARD_VOICE_AGENT_AUDIO, {
168
- sessionId: this.sessionId,
169
- audioData: data.audio_event.audio_base_64,
170
- });
171
- } catch (error) {
172
- console.error('[ElevenLabsVoice] Error sending audio to dashboard', error);
173
- }
174
- }
175
- break;
176
- case 'conversation_initiation_metadata':
177
- console.debug('ElevenLabs conversation initiation metadata', data);
178
- break;
179
- case 'agent_response_correction':
180
- try {
181
- console.debug('Agent response correction received', data.agent_response_correction_event);
182
- await this.updateAgentResponse(data.agent_response_correction_event.original_agent_response, data.agent_response_correction_event.corrected_agent_response);
183
- } catch (error) {
184
- console.error('[ElevenLabsVoice] Error updating agent response', error);
185
- }
186
- break;
187
- default:
188
- console.debug('Received unknown message from ElevenLabs', data);
189
- break;
190
- }
191
- };
192
- }
193
-
194
- private async updateAgentResponse(originalAgentResponse: string, correctedAgentResponse: string): Promise<void> {
195
- const graphState = await this.agent.compiledGraph.getState(this.agent.getLangraphConfig(this.sessionId));
196
- const agentMessage = graphState.values.messages.find((message: BaseMessage) => message.content === originalAgentResponse && message instanceof AIMessage);
197
- if (agentMessage) {
198
- agentMessage.content = correctedAgentResponse;
199
- const currentNodeId = graphState.tasks[graphState.tasks.length - 1].name;
200
- await this.agent.compiledGraph.updateState(this.agent.getLangraphConfig(this.sessionId), {
201
- messages: graphState.values.messages,
202
- }, currentNodeId);
203
- }
204
- else {
205
- console.warn('Agent message not found for correction', originalAgentResponse);
206
- }
207
- }
208
-
209
- public hangup(): void {
210
- this.onDisconnectCallback?.();
211
- this.elevenLabsSocket?.close();
212
- }
213
-
214
- /**
215
- * Set callback for audio data in base64 format
216
- */
217
- onAudio(callback: (data: string) => void): void {
218
- this.onAudioCallback = callback;
219
- }
220
-
221
- /**
222
- * Set callback for interruption events
223
- */
224
- onInterruption(callback: () => void): void {
225
- this.onInterruptionCallback = callback;
226
- }
227
-
228
- /**
229
- * Set callback for disconnect events
230
- */
231
- onDisconnect(callback: () => void): void {
232
- this.onDisconnectCallback = callback;
233
- }
234
-
235
- /**
236
- * User audio in base64 format
237
- */
238
- sendAudio(audioData: string): void {
239
- this.sendToElevenLabs(JSON.stringify({ user_audio_chunk: audioData }));
240
- }
241
- }
242
-
243
- // ----------------- Helper Types -----------------
244
-
245
- /** Shape of the message sent to ElevenLabs when starting a conversation */
246
- interface ConversationInitiationClientData {
247
- type: 'conversation_initiation_client_data';
248
- conversation_config_override: {
249
- agent: {
250
- first_message: string;
251
- language: string;
252
- };
253
- tts?: {
254
- voice_id: string;
255
- };
256
- };
257
- }