@lokutor/sdk 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Lokutor 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.
package/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # Lokutor JavaScript SDK
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@lokutor/sdk.svg)](https://www.npmjs.com/package/@lokutor/sdk)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+
6
+ Official JavaScript/TypeScript SDK for [Lokutor AI](https://lokutor.com), providing high-performance, real-time Voice-to-Voice AI.
7
+
8
+ ## Features
9
+
10
+ - ⚡ **Ultra-low latency**: Optimized WebSocket streaming for real-time interactions.
11
+ - 🗣️ **Conversational Intelligence**: Integrated STT, LLM, and TTS with VAD and barge-in support.
12
+ - 🌍 **Multi-lingual**: Support for English, Spanish, French, Portuguese, and Korean.
13
+ - 🎨 **Natural Voices**: Multiple high-quality male and female voice styles.
14
+ - 💻 **TypeScript First**: Full type definitions included.
15
+
16
+ ## Installation
17
+
18
+ ```bash
19
+ npm install @lokutor/sdk
20
+ # or
21
+ yarn add @lokutor/sdk
22
+ ```
23
+
24
+ ## Quick Start
25
+
26
+ ### 1. Voice Agent (Conversational AI)
27
+
28
+ The `VoiceAgentClient` handles everything from audio streaming to conversational logic.
29
+
30
+ ```typescript
31
+ import { VoiceAgentClient, VoiceStyle, Language } from '@lokutor/sdk';
32
+
33
+ const client = new VoiceAgentClient({
34
+ apiKey: 'your-api-key',
35
+ prompt: 'You are a helpful and friendly AI assistant.',
36
+ onTranscription: (text) => console.log('User:', text),
37
+ onResponse: (text) => console.log('AI:', text)
38
+ });
39
+
40
+ await client.connect();
41
+ // Feed PCM audio data to the client:
42
+ // client.sendAudio(pcmData);
43
+ ```
44
+
45
+ ### 2. Standalone Streaming TTS
46
+
47
+ Use `TTSClient` to convert text into high-quality audio streams without the conversational overhead.
48
+
49
+ ```typescript
50
+ import { TTSClient, VoiceStyle } from '@lokutor/sdk';
51
+
52
+ const client = new TTSClient({ apiKey: 'your-api-key' });
53
+
54
+ await client.synthesize({
55
+ text: 'Hello world, this is a test of Lokutor streaming TTS.',
56
+ voice: VoiceStyle.F1,
57
+ onAudio: (buffer) => {
58
+ // Play the audio buffer (Node.js or Browser)
59
+ console.log(`Received ${buffer.length} bytes of audio`);
60
+ }
61
+ });
62
+ ```
63
+
64
+ ## Hardware Compatibility
65
+
66
+ This SDK is platform-agnostic and works in both **Node.js** and **Browser** environments.
67
+
68
+ ### For Node.js
69
+ We recommend using `node-record-lpcm16` for recording and `speaker` for playback. See the [examples](./examples) folder for a complete Node.js CLI implementation.
70
+
71
+ ### For Browser
72
+ Use the [Web Audio API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API) to capture microphone input and play back the received buffers.
73
+
74
+ ## Documentation
75
+
76
+ Full documentation is available at [docs.lokutor.com](https://docs.lokutor.com).
77
+
78
+ ## License
79
+
80
+ MIT © [Lokutor AI](https://lokutor.com)
@@ -0,0 +1,163 @@
1
+ /**
2
+ * Available voice styles for the Lokutor AI Agent
3
+ */
4
+ declare enum VoiceStyle {
5
+ F1 = "F1",
6
+ F2 = "F2",
7
+ F3 = "F3",
8
+ F4 = "F4",
9
+ F5 = "F5",
10
+ M1 = "M1",
11
+ M2 = "M2",
12
+ M3 = "M3",
13
+ M4 = "M4",
14
+ M5 = "M5"
15
+ }
16
+ /**
17
+ * Supported languages for speech and text
18
+ */
19
+ declare enum Language {
20
+ ENGLISH = "en",
21
+ SPANISH = "es",
22
+ FRENCH = "fr",
23
+ PORTUGUESE = "pt",
24
+ KOREAN = "ko"
25
+ }
26
+ /**
27
+ * Audio configuration constants
28
+ */
29
+ declare const AUDIO_CONFIG: {
30
+ SAMPLE_RATE: number;
31
+ CHANNELS: number;
32
+ CHUNK_DURATION_MS: number;
33
+ readonly CHUNK_SIZE: number;
34
+ };
35
+ /**
36
+ * Default WebSocket URLs
37
+ */
38
+ declare const DEFAULT_URLS: {
39
+ VOICE_AGENT: string;
40
+ TTS: string;
41
+ };
42
+ /**
43
+ * SDK Configuration interface
44
+ */
45
+ interface LokutorConfig {
46
+ apiKey: string;
47
+ serverUrl?: string;
48
+ onTranscription?: (text: string) => void;
49
+ onResponse?: (text: string) => void;
50
+ onAudio?: (data: Buffer) => void;
51
+ onStatus?: (status: string) => void;
52
+ onError?: (error: any) => void;
53
+ }
54
+ /**
55
+ * Text-to-Speech synthesis request options
56
+ */
57
+ interface SynthesizeOptions {
58
+ text: string;
59
+ voice?: VoiceStyle;
60
+ language?: Language;
61
+ speed?: number;
62
+ steps?: number;
63
+ visemes?: boolean;
64
+ }
65
+
66
+ /**
67
+ * Main client for Lokutor Voice Agent SDK
68
+ *
69
+ * Provides a high-level interface for real-time voice conversations.
70
+ */
71
+ declare class VoiceAgentClient {
72
+ private ws;
73
+ private apiKey;
74
+ private serverUrl;
75
+ prompt: string;
76
+ voice: VoiceStyle;
77
+ language: Language;
78
+ private onTranscription?;
79
+ private onResponse?;
80
+ private onAudioCallback?;
81
+ private onStatus?;
82
+ private onError?;
83
+ private isConnected;
84
+ constructor(config: LokutorConfig & {
85
+ prompt: string;
86
+ voice?: VoiceStyle;
87
+ language?: Language;
88
+ });
89
+ /**
90
+ * Connect to the Lokutor Voice Agent server
91
+ */
92
+ connect(): Promise<boolean>;
93
+ /**
94
+ * Send initial configuration to the server
95
+ */
96
+ private sendConfig;
97
+ /**
98
+ * Send raw PCM audio data to the server
99
+ * @param audioData Int16 PCM audio buffer
100
+ */
101
+ sendAudio(audioData: Buffer | Uint8Array): void;
102
+ /**
103
+ * Handle incoming binary data (audio response)
104
+ */
105
+ private handleBinaryMessage;
106
+ /**
107
+ * Handle incoming text messages (metadata/transcriptions)
108
+ */
109
+ private handleTextMessage;
110
+ private audioListeners;
111
+ private emit;
112
+ onAudio(callback: (data: Buffer) => void): void;
113
+ /**
114
+ * Disconnect from the server
115
+ */
116
+ disconnect(): void;
117
+ }
118
+ /**
119
+ * Client for standalone Text-to-Speech synthesis
120
+ */
121
+ declare class TTSClient {
122
+ private apiKey;
123
+ private serverUrl;
124
+ private onAudioCallback?;
125
+ private onVisemesCallback?;
126
+ private onErrorCallback?;
127
+ constructor(config: {
128
+ apiKey: string;
129
+ serverUrl?: string;
130
+ });
131
+ /**
132
+ * Synthesize text to speech
133
+ *
134
+ * This opens a temporary WebSocket connection, sends the request,
135
+ * and streams back the audio.
136
+ */
137
+ synthesize(options: {
138
+ text: string;
139
+ voice?: VoiceStyle;
140
+ language?: Language;
141
+ speed?: number;
142
+ steps?: number;
143
+ visemes?: boolean;
144
+ onAudio?: (data: Buffer) => void;
145
+ onVisemes?: (visemes: any[]) => void;
146
+ onError?: (error: any) => void;
147
+ }): Promise<void>;
148
+ }
149
+ /**
150
+ * Quick function to start a conversation (requires manual audio piping in JS)
151
+ */
152
+ declare function simpleConversation(config: LokutorConfig & {
153
+ prompt: string;
154
+ }): Promise<VoiceAgentClient>;
155
+ /**
156
+ * Quick function for standalone TTS synthesis
157
+ */
158
+ declare function simpleTTS(options: SynthesizeOptions & {
159
+ apiKey: string;
160
+ onAudio: (buf: Buffer) => void;
161
+ }): Promise<void>;
162
+
163
+ export { AUDIO_CONFIG, DEFAULT_URLS, Language, type LokutorConfig, type SynthesizeOptions, TTSClient, VoiceAgentClient, VoiceStyle, simpleConversation, simpleTTS };
@@ -0,0 +1,163 @@
1
+ /**
2
+ * Available voice styles for the Lokutor AI Agent
3
+ */
4
+ declare enum VoiceStyle {
5
+ F1 = "F1",
6
+ F2 = "F2",
7
+ F3 = "F3",
8
+ F4 = "F4",
9
+ F5 = "F5",
10
+ M1 = "M1",
11
+ M2 = "M2",
12
+ M3 = "M3",
13
+ M4 = "M4",
14
+ M5 = "M5"
15
+ }
16
+ /**
17
+ * Supported languages for speech and text
18
+ */
19
+ declare enum Language {
20
+ ENGLISH = "en",
21
+ SPANISH = "es",
22
+ FRENCH = "fr",
23
+ PORTUGUESE = "pt",
24
+ KOREAN = "ko"
25
+ }
26
+ /**
27
+ * Audio configuration constants
28
+ */
29
+ declare const AUDIO_CONFIG: {
30
+ SAMPLE_RATE: number;
31
+ CHANNELS: number;
32
+ CHUNK_DURATION_MS: number;
33
+ readonly CHUNK_SIZE: number;
34
+ };
35
+ /**
36
+ * Default WebSocket URLs
37
+ */
38
+ declare const DEFAULT_URLS: {
39
+ VOICE_AGENT: string;
40
+ TTS: string;
41
+ };
42
+ /**
43
+ * SDK Configuration interface
44
+ */
45
+ interface LokutorConfig {
46
+ apiKey: string;
47
+ serverUrl?: string;
48
+ onTranscription?: (text: string) => void;
49
+ onResponse?: (text: string) => void;
50
+ onAudio?: (data: Buffer) => void;
51
+ onStatus?: (status: string) => void;
52
+ onError?: (error: any) => void;
53
+ }
54
+ /**
55
+ * Text-to-Speech synthesis request options
56
+ */
57
+ interface SynthesizeOptions {
58
+ text: string;
59
+ voice?: VoiceStyle;
60
+ language?: Language;
61
+ speed?: number;
62
+ steps?: number;
63
+ visemes?: boolean;
64
+ }
65
+
66
+ /**
67
+ * Main client for Lokutor Voice Agent SDK
68
+ *
69
+ * Provides a high-level interface for real-time voice conversations.
70
+ */
71
+ declare class VoiceAgentClient {
72
+ private ws;
73
+ private apiKey;
74
+ private serverUrl;
75
+ prompt: string;
76
+ voice: VoiceStyle;
77
+ language: Language;
78
+ private onTranscription?;
79
+ private onResponse?;
80
+ private onAudioCallback?;
81
+ private onStatus?;
82
+ private onError?;
83
+ private isConnected;
84
+ constructor(config: LokutorConfig & {
85
+ prompt: string;
86
+ voice?: VoiceStyle;
87
+ language?: Language;
88
+ });
89
+ /**
90
+ * Connect to the Lokutor Voice Agent server
91
+ */
92
+ connect(): Promise<boolean>;
93
+ /**
94
+ * Send initial configuration to the server
95
+ */
96
+ private sendConfig;
97
+ /**
98
+ * Send raw PCM audio data to the server
99
+ * @param audioData Int16 PCM audio buffer
100
+ */
101
+ sendAudio(audioData: Buffer | Uint8Array): void;
102
+ /**
103
+ * Handle incoming binary data (audio response)
104
+ */
105
+ private handleBinaryMessage;
106
+ /**
107
+ * Handle incoming text messages (metadata/transcriptions)
108
+ */
109
+ private handleTextMessage;
110
+ private audioListeners;
111
+ private emit;
112
+ onAudio(callback: (data: Buffer) => void): void;
113
+ /**
114
+ * Disconnect from the server
115
+ */
116
+ disconnect(): void;
117
+ }
118
+ /**
119
+ * Client for standalone Text-to-Speech synthesis
120
+ */
121
+ declare class TTSClient {
122
+ private apiKey;
123
+ private serverUrl;
124
+ private onAudioCallback?;
125
+ private onVisemesCallback?;
126
+ private onErrorCallback?;
127
+ constructor(config: {
128
+ apiKey: string;
129
+ serverUrl?: string;
130
+ });
131
+ /**
132
+ * Synthesize text to speech
133
+ *
134
+ * This opens a temporary WebSocket connection, sends the request,
135
+ * and streams back the audio.
136
+ */
137
+ synthesize(options: {
138
+ text: string;
139
+ voice?: VoiceStyle;
140
+ language?: Language;
141
+ speed?: number;
142
+ steps?: number;
143
+ visemes?: boolean;
144
+ onAudio?: (data: Buffer) => void;
145
+ onVisemes?: (visemes: any[]) => void;
146
+ onError?: (error: any) => void;
147
+ }): Promise<void>;
148
+ }
149
+ /**
150
+ * Quick function to start a conversation (requires manual audio piping in JS)
151
+ */
152
+ declare function simpleConversation(config: LokutorConfig & {
153
+ prompt: string;
154
+ }): Promise<VoiceAgentClient>;
155
+ /**
156
+ * Quick function for standalone TTS synthesis
157
+ */
158
+ declare function simpleTTS(options: SynthesizeOptions & {
159
+ apiKey: string;
160
+ onAudio: (buf: Buffer) => void;
161
+ }): Promise<void>;
162
+
163
+ export { AUDIO_CONFIG, DEFAULT_URLS, Language, type LokutorConfig, type SynthesizeOptions, TTSClient, VoiceAgentClient, VoiceStyle, simpleConversation, simpleTTS };
package/dist/index.js ADDED
@@ -0,0 +1,305 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ AUDIO_CONFIG: () => AUDIO_CONFIG,
24
+ DEFAULT_URLS: () => DEFAULT_URLS,
25
+ Language: () => Language,
26
+ TTSClient: () => TTSClient,
27
+ VoiceAgentClient: () => VoiceAgentClient,
28
+ VoiceStyle: () => VoiceStyle,
29
+ simpleConversation: () => simpleConversation,
30
+ simpleTTS: () => simpleTTS
31
+ });
32
+ module.exports = __toCommonJS(index_exports);
33
+
34
+ // src/types.ts
35
+ var VoiceStyle = /* @__PURE__ */ ((VoiceStyle2) => {
36
+ VoiceStyle2["F1"] = "F1";
37
+ VoiceStyle2["F2"] = "F2";
38
+ VoiceStyle2["F3"] = "F3";
39
+ VoiceStyle2["F4"] = "F4";
40
+ VoiceStyle2["F5"] = "F5";
41
+ VoiceStyle2["M1"] = "M1";
42
+ VoiceStyle2["M2"] = "M2";
43
+ VoiceStyle2["M3"] = "M3";
44
+ VoiceStyle2["M4"] = "M4";
45
+ VoiceStyle2["M5"] = "M5";
46
+ return VoiceStyle2;
47
+ })(VoiceStyle || {});
48
+ var Language = /* @__PURE__ */ ((Language2) => {
49
+ Language2["ENGLISH"] = "en";
50
+ Language2["SPANISH"] = "es";
51
+ Language2["FRENCH"] = "fr";
52
+ Language2["PORTUGUESE"] = "pt";
53
+ Language2["KOREAN"] = "ko";
54
+ return Language2;
55
+ })(Language || {});
56
+ var AUDIO_CONFIG = {
57
+ SAMPLE_RATE: 44100,
58
+ CHANNELS: 1,
59
+ CHUNK_DURATION_MS: 20,
60
+ get CHUNK_SIZE() {
61
+ return Math.floor(this.SAMPLE_RATE * this.CHUNK_DURATION_MS / 1e3);
62
+ }
63
+ };
64
+ var DEFAULT_URLS = {
65
+ VOICE_AGENT: "wss://api.lokutor.com/ws/agent",
66
+ TTS: "wss://api.lokutor.com/ws/tts"
67
+ };
68
+
69
+ // src/client.ts
70
+ var import_ws = require("ws");
71
+ var VoiceAgentClient = class {
72
+ ws = null;
73
+ apiKey;
74
+ serverUrl;
75
+ prompt;
76
+ voice;
77
+ language;
78
+ // Callbacks
79
+ onTranscription;
80
+ onResponse;
81
+ onAudioCallback;
82
+ onStatus;
83
+ onError;
84
+ isConnected = false;
85
+ constructor(config) {
86
+ this.apiKey = config.apiKey;
87
+ this.serverUrl = config.serverUrl || DEFAULT_URLS.VOICE_AGENT;
88
+ this.prompt = config.prompt;
89
+ this.voice = config.voice || "F1" /* F1 */;
90
+ this.language = config.language || "en" /* ENGLISH */;
91
+ this.onTranscription = config.onTranscription;
92
+ this.onResponse = config.onResponse;
93
+ this.onAudioCallback = config.onAudio;
94
+ this.onStatus = config.onStatus;
95
+ this.onError = config.onError;
96
+ }
97
+ /**
98
+ * Connect to the Lokutor Voice Agent server
99
+ */
100
+ async connect() {
101
+ return new Promise((resolve, reject) => {
102
+ try {
103
+ console.log(`\u{1F517} Connecting to ${this.serverUrl}...`);
104
+ const headers = {};
105
+ if (this.apiKey) {
106
+ headers["X-API-Key"] = this.apiKey;
107
+ }
108
+ this.ws = new import_ws.WebSocket(this.serverUrl, {
109
+ headers
110
+ });
111
+ this.ws.on("open", () => {
112
+ this.isConnected = true;
113
+ console.log("\u2705 Connected to voice agent!");
114
+ this.sendConfig();
115
+ resolve(true);
116
+ });
117
+ this.ws.on("message", (data, isBinary) => {
118
+ if (isBinary) {
119
+ this.handleBinaryMessage(data);
120
+ } else {
121
+ this.handleTextMessage(data.toString());
122
+ }
123
+ });
124
+ this.ws.on("error", (err) => {
125
+ console.error("\u274C WebSocket error:", err);
126
+ if (this.onError) this.onError(err);
127
+ if (!this.isConnected) reject(err);
128
+ });
129
+ this.ws.on("close", () => {
130
+ this.isConnected = false;
131
+ console.log("Disconnected");
132
+ });
133
+ } catch (err) {
134
+ if (this.onError) this.onError(err);
135
+ reject(err);
136
+ }
137
+ });
138
+ }
139
+ /**
140
+ * Send initial configuration to the server
141
+ */
142
+ sendConfig() {
143
+ if (!this.ws || !this.isConnected) return;
144
+ this.ws.send(JSON.stringify({ type: "prompt", data: this.prompt }));
145
+ this.ws.send(JSON.stringify({ type: "voice", data: this.voice }));
146
+ this.ws.send(JSON.stringify({ type: "language", data: this.language }));
147
+ console.log(`\u2699\uFE0F Configured: voice=${this.voice}, language=${this.language}`);
148
+ }
149
+ /**
150
+ * Send raw PCM audio data to the server
151
+ * @param audioData Int16 PCM audio buffer
152
+ */
153
+ sendAudio(audioData) {
154
+ if (this.ws && this.isConnected) {
155
+ this.ws.send(audioData, { binary: true });
156
+ }
157
+ }
158
+ /**
159
+ * Handle incoming binary data (audio response)
160
+ */
161
+ handleBinaryMessage(data) {
162
+ this.emit("audio", data);
163
+ }
164
+ /**
165
+ * Handle incoming text messages (metadata/transcriptions)
166
+ */
167
+ handleTextMessage(text) {
168
+ try {
169
+ const msg = JSON.parse(text);
170
+ switch (msg.type) {
171
+ case "audio":
172
+ if (msg.data) {
173
+ const buffer = Buffer.from(msg.data, "base64");
174
+ this.handleBinaryMessage(buffer);
175
+ }
176
+ break;
177
+ case "transcript":
178
+ if (msg.role === "user") {
179
+ if (this.onTranscription) this.onTranscription(msg.data);
180
+ console.log(`\u{1F4AC} You: ${msg.data}`);
181
+ } else {
182
+ if (this.onResponse) this.onResponse(msg.data);
183
+ console.log(`\u{1F916} Agent: ${msg.data}`);
184
+ }
185
+ break;
186
+ case "status":
187
+ if (this.onStatus) this.onStatus(msg.data);
188
+ const icons = {
189
+ "interrupted": "\u26A1",
190
+ "thinking": "\u{1F9E0}",
191
+ "speaking": "\u{1F50A}",
192
+ "listening": "\u{1F442}"
193
+ };
194
+ console.log(`${icons[msg.data] || ""} Status: ${msg.data}`);
195
+ break;
196
+ case "error":
197
+ if (this.onError) this.onError(msg.data);
198
+ console.error(`\u274C Server error: ${msg.data}`);
199
+ break;
200
+ }
201
+ } catch (e) {
202
+ }
203
+ }
204
+ audioListeners = [];
205
+ emit(event, data) {
206
+ if (event === "audio") {
207
+ if (this.onAudioCallback) this.onAudioCallback(data);
208
+ this.audioListeners.forEach((l) => l(data));
209
+ }
210
+ }
211
+ onAudio(callback) {
212
+ this.audioListeners.push(callback);
213
+ }
214
+ /**
215
+ * Disconnect from the server
216
+ */
217
+ disconnect() {
218
+ if (this.ws) {
219
+ this.ws.close();
220
+ this.ws = null;
221
+ }
222
+ }
223
+ };
224
+ var TTSClient = class {
225
+ apiKey;
226
+ serverUrl;
227
+ onAudioCallback;
228
+ onVisemesCallback;
229
+ onErrorCallback;
230
+ constructor(config) {
231
+ this.apiKey = config.apiKey;
232
+ this.serverUrl = config.serverUrl || DEFAULT_URLS.TTS;
233
+ }
234
+ /**
235
+ * Synthesize text to speech
236
+ *
237
+ * This opens a temporary WebSocket connection, sends the request,
238
+ * and streams back the audio.
239
+ */
240
+ synthesize(options) {
241
+ return new Promise((resolve, reject) => {
242
+ try {
243
+ const headers = {};
244
+ if (this.apiKey) {
245
+ headers["X-API-Key"] = this.apiKey;
246
+ }
247
+ const ws = new import_ws.WebSocket(this.serverUrl, { headers });
248
+ ws.on("open", () => {
249
+ const req = {
250
+ text: options.text,
251
+ voice: options.voice || "F1" /* F1 */,
252
+ lang: options.language || "en" /* ENGLISH */,
253
+ speed: options.speed || 1.05,
254
+ steps: options.steps || 24,
255
+ visemes: options.visemes || false
256
+ };
257
+ ws.send(JSON.stringify(req));
258
+ });
259
+ ws.on("message", (data, isBinary) => {
260
+ if (isBinary) {
261
+ if (options.onAudio) options.onAudio(data);
262
+ } else {
263
+ try {
264
+ const msg = JSON.parse(data.toString());
265
+ if (Array.isArray(msg) && options.onVisemes) {
266
+ options.onVisemes(msg);
267
+ }
268
+ } catch (e) {
269
+ }
270
+ }
271
+ });
272
+ ws.on("error", (err) => {
273
+ if (options.onError) options.onError(err);
274
+ reject(err);
275
+ });
276
+ ws.on("close", () => {
277
+ resolve();
278
+ });
279
+ } catch (err) {
280
+ if (options.onError) options.onError(err);
281
+ reject(err);
282
+ }
283
+ });
284
+ }
285
+ };
286
+ async function simpleConversation(config) {
287
+ const client = new VoiceAgentClient(config);
288
+ await client.connect();
289
+ return client;
290
+ }
291
+ async function simpleTTS(options) {
292
+ const client = new TTSClient({ apiKey: options.apiKey });
293
+ return client.synthesize(options);
294
+ }
295
+ // Annotate the CommonJS export names for ESM import in node:
296
+ 0 && (module.exports = {
297
+ AUDIO_CONFIG,
298
+ DEFAULT_URLS,
299
+ Language,
300
+ TTSClient,
301
+ VoiceAgentClient,
302
+ VoiceStyle,
303
+ simpleConversation,
304
+ simpleTTS
305
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,271 @@
1
+ // src/types.ts
2
+ var VoiceStyle = /* @__PURE__ */ ((VoiceStyle2) => {
3
+ VoiceStyle2["F1"] = "F1";
4
+ VoiceStyle2["F2"] = "F2";
5
+ VoiceStyle2["F3"] = "F3";
6
+ VoiceStyle2["F4"] = "F4";
7
+ VoiceStyle2["F5"] = "F5";
8
+ VoiceStyle2["M1"] = "M1";
9
+ VoiceStyle2["M2"] = "M2";
10
+ VoiceStyle2["M3"] = "M3";
11
+ VoiceStyle2["M4"] = "M4";
12
+ VoiceStyle2["M5"] = "M5";
13
+ return VoiceStyle2;
14
+ })(VoiceStyle || {});
15
+ var Language = /* @__PURE__ */ ((Language2) => {
16
+ Language2["ENGLISH"] = "en";
17
+ Language2["SPANISH"] = "es";
18
+ Language2["FRENCH"] = "fr";
19
+ Language2["PORTUGUESE"] = "pt";
20
+ Language2["KOREAN"] = "ko";
21
+ return Language2;
22
+ })(Language || {});
23
+ var AUDIO_CONFIG = {
24
+ SAMPLE_RATE: 44100,
25
+ CHANNELS: 1,
26
+ CHUNK_DURATION_MS: 20,
27
+ get CHUNK_SIZE() {
28
+ return Math.floor(this.SAMPLE_RATE * this.CHUNK_DURATION_MS / 1e3);
29
+ }
30
+ };
31
+ var DEFAULT_URLS = {
32
+ VOICE_AGENT: "wss://api.lokutor.com/ws/agent",
33
+ TTS: "wss://api.lokutor.com/ws/tts"
34
+ };
35
+
36
+ // src/client.ts
37
+ import { WebSocket } from "ws";
38
+ var VoiceAgentClient = class {
39
+ ws = null;
40
+ apiKey;
41
+ serverUrl;
42
+ prompt;
43
+ voice;
44
+ language;
45
+ // Callbacks
46
+ onTranscription;
47
+ onResponse;
48
+ onAudioCallback;
49
+ onStatus;
50
+ onError;
51
+ isConnected = false;
52
+ constructor(config) {
53
+ this.apiKey = config.apiKey;
54
+ this.serverUrl = config.serverUrl || DEFAULT_URLS.VOICE_AGENT;
55
+ this.prompt = config.prompt;
56
+ this.voice = config.voice || "F1" /* F1 */;
57
+ this.language = config.language || "en" /* ENGLISH */;
58
+ this.onTranscription = config.onTranscription;
59
+ this.onResponse = config.onResponse;
60
+ this.onAudioCallback = config.onAudio;
61
+ this.onStatus = config.onStatus;
62
+ this.onError = config.onError;
63
+ }
64
+ /**
65
+ * Connect to the Lokutor Voice Agent server
66
+ */
67
+ async connect() {
68
+ return new Promise((resolve, reject) => {
69
+ try {
70
+ console.log(`\u{1F517} Connecting to ${this.serverUrl}...`);
71
+ const headers = {};
72
+ if (this.apiKey) {
73
+ headers["X-API-Key"] = this.apiKey;
74
+ }
75
+ this.ws = new WebSocket(this.serverUrl, {
76
+ headers
77
+ });
78
+ this.ws.on("open", () => {
79
+ this.isConnected = true;
80
+ console.log("\u2705 Connected to voice agent!");
81
+ this.sendConfig();
82
+ resolve(true);
83
+ });
84
+ this.ws.on("message", (data, isBinary) => {
85
+ if (isBinary) {
86
+ this.handleBinaryMessage(data);
87
+ } else {
88
+ this.handleTextMessage(data.toString());
89
+ }
90
+ });
91
+ this.ws.on("error", (err) => {
92
+ console.error("\u274C WebSocket error:", err);
93
+ if (this.onError) this.onError(err);
94
+ if (!this.isConnected) reject(err);
95
+ });
96
+ this.ws.on("close", () => {
97
+ this.isConnected = false;
98
+ console.log("Disconnected");
99
+ });
100
+ } catch (err) {
101
+ if (this.onError) this.onError(err);
102
+ reject(err);
103
+ }
104
+ });
105
+ }
106
+ /**
107
+ * Send initial configuration to the server
108
+ */
109
+ sendConfig() {
110
+ if (!this.ws || !this.isConnected) return;
111
+ this.ws.send(JSON.stringify({ type: "prompt", data: this.prompt }));
112
+ this.ws.send(JSON.stringify({ type: "voice", data: this.voice }));
113
+ this.ws.send(JSON.stringify({ type: "language", data: this.language }));
114
+ console.log(`\u2699\uFE0F Configured: voice=${this.voice}, language=${this.language}`);
115
+ }
116
+ /**
117
+ * Send raw PCM audio data to the server
118
+ * @param audioData Int16 PCM audio buffer
119
+ */
120
+ sendAudio(audioData) {
121
+ if (this.ws && this.isConnected) {
122
+ this.ws.send(audioData, { binary: true });
123
+ }
124
+ }
125
+ /**
126
+ * Handle incoming binary data (audio response)
127
+ */
128
+ handleBinaryMessage(data) {
129
+ this.emit("audio", data);
130
+ }
131
+ /**
132
+ * Handle incoming text messages (metadata/transcriptions)
133
+ */
134
+ handleTextMessage(text) {
135
+ try {
136
+ const msg = JSON.parse(text);
137
+ switch (msg.type) {
138
+ case "audio":
139
+ if (msg.data) {
140
+ const buffer = Buffer.from(msg.data, "base64");
141
+ this.handleBinaryMessage(buffer);
142
+ }
143
+ break;
144
+ case "transcript":
145
+ if (msg.role === "user") {
146
+ if (this.onTranscription) this.onTranscription(msg.data);
147
+ console.log(`\u{1F4AC} You: ${msg.data}`);
148
+ } else {
149
+ if (this.onResponse) this.onResponse(msg.data);
150
+ console.log(`\u{1F916} Agent: ${msg.data}`);
151
+ }
152
+ break;
153
+ case "status":
154
+ if (this.onStatus) this.onStatus(msg.data);
155
+ const icons = {
156
+ "interrupted": "\u26A1",
157
+ "thinking": "\u{1F9E0}",
158
+ "speaking": "\u{1F50A}",
159
+ "listening": "\u{1F442}"
160
+ };
161
+ console.log(`${icons[msg.data] || ""} Status: ${msg.data}`);
162
+ break;
163
+ case "error":
164
+ if (this.onError) this.onError(msg.data);
165
+ console.error(`\u274C Server error: ${msg.data}`);
166
+ break;
167
+ }
168
+ } catch (e) {
169
+ }
170
+ }
171
+ audioListeners = [];
172
+ emit(event, data) {
173
+ if (event === "audio") {
174
+ if (this.onAudioCallback) this.onAudioCallback(data);
175
+ this.audioListeners.forEach((l) => l(data));
176
+ }
177
+ }
178
+ onAudio(callback) {
179
+ this.audioListeners.push(callback);
180
+ }
181
+ /**
182
+ * Disconnect from the server
183
+ */
184
+ disconnect() {
185
+ if (this.ws) {
186
+ this.ws.close();
187
+ this.ws = null;
188
+ }
189
+ }
190
+ };
191
+ var TTSClient = class {
192
+ apiKey;
193
+ serverUrl;
194
+ onAudioCallback;
195
+ onVisemesCallback;
196
+ onErrorCallback;
197
+ constructor(config) {
198
+ this.apiKey = config.apiKey;
199
+ this.serverUrl = config.serverUrl || DEFAULT_URLS.TTS;
200
+ }
201
+ /**
202
+ * Synthesize text to speech
203
+ *
204
+ * This opens a temporary WebSocket connection, sends the request,
205
+ * and streams back the audio.
206
+ */
207
+ synthesize(options) {
208
+ return new Promise((resolve, reject) => {
209
+ try {
210
+ const headers = {};
211
+ if (this.apiKey) {
212
+ headers["X-API-Key"] = this.apiKey;
213
+ }
214
+ const ws = new WebSocket(this.serverUrl, { headers });
215
+ ws.on("open", () => {
216
+ const req = {
217
+ text: options.text,
218
+ voice: options.voice || "F1" /* F1 */,
219
+ lang: options.language || "en" /* ENGLISH */,
220
+ speed: options.speed || 1.05,
221
+ steps: options.steps || 24,
222
+ visemes: options.visemes || false
223
+ };
224
+ ws.send(JSON.stringify(req));
225
+ });
226
+ ws.on("message", (data, isBinary) => {
227
+ if (isBinary) {
228
+ if (options.onAudio) options.onAudio(data);
229
+ } else {
230
+ try {
231
+ const msg = JSON.parse(data.toString());
232
+ if (Array.isArray(msg) && options.onVisemes) {
233
+ options.onVisemes(msg);
234
+ }
235
+ } catch (e) {
236
+ }
237
+ }
238
+ });
239
+ ws.on("error", (err) => {
240
+ if (options.onError) options.onError(err);
241
+ reject(err);
242
+ });
243
+ ws.on("close", () => {
244
+ resolve();
245
+ });
246
+ } catch (err) {
247
+ if (options.onError) options.onError(err);
248
+ reject(err);
249
+ }
250
+ });
251
+ }
252
+ };
253
+ async function simpleConversation(config) {
254
+ const client = new VoiceAgentClient(config);
255
+ await client.connect();
256
+ return client;
257
+ }
258
+ async function simpleTTS(options) {
259
+ const client = new TTSClient({ apiKey: options.apiKey });
260
+ return client.synthesize(options);
261
+ }
262
+ export {
263
+ AUDIO_CONFIG,
264
+ DEFAULT_URLS,
265
+ Language,
266
+ TTSClient,
267
+ VoiceAgentClient,
268
+ VoiceStyle,
269
+ simpleConversation,
270
+ simpleTTS
271
+ };
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@lokutor/sdk",
3
+ "version": "1.0.0",
4
+ "description": "JavaScript/TypeScript SDK for Lokutor Real-time Voice AI",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "build": "tsup src/index.ts --format cjs,esm --dts",
13
+ "dev": "tsup src/index.ts --format cjs,esm --watch --dts",
14
+ "test": "vitest run",
15
+ "test:watch": "vitest",
16
+ "lint": "eslint src --ext .ts",
17
+ "docs": "typedoc src/index.ts"
18
+ },
19
+ "keywords": [
20
+ "voice",
21
+ "ai",
22
+ "tts",
23
+ "stt",
24
+ "streaming",
25
+ "websocket",
26
+ "real-time"
27
+ ],
28
+ "author": "Lokutor AI",
29
+ "license": "MIT",
30
+ "dependencies": {
31
+ "ws": "^8.16.0"
32
+ },
33
+ "devDependencies": {
34
+ "@types/node": "^20.10.0",
35
+ "@types/ws": "^8.5.10",
36
+ "tsup": "^8.0.1",
37
+ "typescript": "^5.3.2",
38
+ "vitest": "^1.0.1"
39
+ }
40
+ }