echo-ai-sdk-ts 2.6.3 → 3.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.
Files changed (4) hide show
  1. package/README.md +842 -624
  2. package/dist/index.js +28423 -11046
  3. package/dist/index.mjs +28292 -10957
  4. package/package.json +72 -43
package/README.md CHANGED
@@ -1,624 +1,842 @@
1
- # Echo AI SDK
2
-
3
- The all-in-one AI platform for chat, voice, agents, and **embeddable customer support chatbots**.
4
-
5
- ![npm](https://img.shields.io/npm/v/echo-ai-sdk) ![license](https://img.shields.io/npm/l/echo-ai-sdk) ![typescript](https://img.shields.io/badge/Language-TypeScript-blue)
6
-
7
- ## 🚀 Deploy an AI Chatbot on Your Website in 5 Minutes
8
-
9
- ```bash
10
- npm install echo-ai-sdk zod openai express cors
11
- ```
12
-
13
- ### Step 1: Create Your Server (3 lines)
14
-
15
- ```typescript
16
- import { startChatServer, AIModelGateway, OpenAIProvider } from "echo-ai-sdk";
17
-
18
- startChatServer({
19
- gateway: new AIModelGateway([new OpenAIProvider(process.env.OPENAI_API_KEY!)]),
20
- companyName: "Acme Inc",
21
- companyDescription: "We sell premium widgets and gadgets.",
22
- apiConnector: { // Optional: connect to your website API
23
- baseUrl: "https://api.acme.com/v1",
24
- authToken: process.env.API_TOKEN
25
- },
26
- port: 3456
27
- });
28
- ```
29
-
30
- ### Step 2: Embed the Widget on Your Website (1 line)
31
-
32
- ```typescript
33
- import { ChatWidget } from "echo-ai-sdk";
34
-
35
- const embedCode = ChatWidget.generate({
36
- serverEndpoint: "https://your-server.com",
37
- title: "Acme Support",
38
- subtitle: "We reply instantly ⚡",
39
- enableVoice: true, // Enable microphone input
40
- theme: {
41
- primaryColor: "#6366f1",
42
- position: "bottom-right"
43
- }
44
- });
45
-
46
- // Paste `embedCode` into your HTML, or serve it dynamically
47
- console.log(embedCode);
48
- ```
49
-
50
- ### Step 3: Done! 🎉
51
-
52
- Your website now has a fully functional AI chatbot with **Tier 1 Enterprise Features**:
53
-
54
- #### 🧠 RAG Knowledge Base (Eliminate Hallucinations)
55
- Ground your AI in your own data (PDFs, URLs, text).
56
- ```typescript
57
- await bot.knowledgeBase.ingestURL("https://acme.com/shipping-policy");
58
- await bot.knowledgeBase.ingestText("Our warranty lasts 2 years...");
59
- ```
60
-
61
- #### 📊 Conversation Analytics
62
- Track resolution rates, CSAT, token costs, and response times.
63
- ```typescript
64
- const stats = bot.analytics.getSnapshot();
65
- console.log(`Resolution Rate: ${stats.resolutionRate * 100}%`);
66
- ```
67
-
68
- #### 🤝 Human Handoff
69
- Auto-escalate to human agents when the bot detects frustration or explicit requests.
70
- ```typescript
71
- const bot = new CustomerSupportBot({
72
- handoff: {
73
- webhookUrl: "https://your-helpdesk.com/handoff",
74
- webhookSecret: "secure_token_123"
75
- }
76
- });
77
- ```
78
-
79
- ### 🌟 Tier 1 Perfection Features
80
-
81
- #### 🧠 RAG Persistence
82
- Ground your bot in your data and keep it there across restarts.
83
- ```typescript
84
- await bot.knowledgeBase.ingestText("Your knowledge text here...");
85
- await bot.knowledgeBase.save("./knowledge-base.json"); // Save to disk
86
- await bot.knowledgeBase.load("./knowledge-base.json"); // Load back
87
- ```
88
-
89
- #### 📊 High-Precision Analytics & Cost Control
90
- Track every penny with accurate token estimation and pricing maps.
91
- ```typescript
92
- const stats = bot.analytics.getSnapshot();
93
- console.log(`Total Cost: $${stats.totalCostUsd}`);
94
- console.log(`Handoff Rate: ${stats.handoffRate * 100}%`);
95
- ```
96
-
97
- #### 🤝 Sentiment-Aware Human Handoff
98
- Detect frustration before it's too late. The bot tracks "negative strikes" and auto-summarizes problems for human agents.
99
- ```typescript
100
- const bot = new CustomerSupportBot({
101
- handoff: {
102
- webhookUrl: "https://your-helpdesk.com/webhook",
103
- negativeSentimentThreshold: 3, // Escalates after 3 angry turns
104
- }
105
- });
106
- ```
107
-
108
- #### 🔌 Middleware API
109
- Hook into every chat turn to add custom logic, logging, or transformations.
110
- ```typescript
111
- bot.use(async ({ sessionId, message }) => {
112
- console.log(`[Turn] ${sessionId}: ${message}`);
113
- });
114
- ```
115
-
116
-
117
- ### 🚀 Tier 2 Pro-Grade Features
118
-
119
- #### 🌐 Omnichannel Sync
120
- Connect your bot to Slack and Telegram while maintaining a single user context.
121
- ```typescript
122
- import { TelegramAdapter, SlackAdapter } from "echo-ai-sdk";
123
-
124
- const tg = new TelegramAdapter({ bot, token: "TG_TOKEN" });
125
- await tg.start();
126
-
127
- const slack = new SlackAdapter({ bot, signingSecret: "...", token: "..." });
128
- await slack.start();
129
- ```
130
-
131
- #### 💾 Persistent Session Store
132
- Move beyond memory. Use `FileSessionStore` for local persistence or implement `SessionStore` for Redis.
133
- ```typescript
134
- import { FileSessionStore } from "echo-ai-sdk";
135
- const bot = new CustomerSupportBot({
136
- sessionStore: new FileSessionStore("./sessions"),
137
- });
138
- ```
139
-
140
- #### 📈 Outcome-Based Billing & ROI
141
- Track the *real value* of your AI by recording business outcomes.
142
- ```typescript
143
- // Inside a tool or middleware
144
- bot.trackOutcome(sessionId, "lead_captured", 50.0);
145
-
146
- const stats = bot.analytics.getSnapshot();
147
- console.log(`ROI: ${stats.roi * 100}%`);
148
- console.log(`Value Generated: $${stats.totalValueGeneratedUsd}`);
149
- ```
150
-
151
- ---
152
-
153
- ## Installation
154
-
155
- ```sh
156
- npm install echo-ai-sdk zod
157
- ```
158
-
159
- You'll need the respective provider SDKs if you wish to utilize them:
160
-
161
- ```sh
162
- npm install openai @anthropic-ai/sdk
163
- ```
164
-
165
- ## Quick Start (The Facade)
166
-
167
- The quickest way to get started is by instantiating the `EchoAI` client. It automatically sniffs your `process.env` for `OPENAI_API_KEY` or `ANTHROPIC_API_KEY` and constructs the highly available gateway.
168
-
169
- ```typescript
170
- import { EchoAI, calculatorTool } from "echo-ai-sdk";
171
-
172
- // Assumes process.env.OPENAI_API_KEY is populated.
173
- const ai = new EchoAI();
174
-
175
- const agent = ai.createToolAgent([calculatorTool]);
176
-
177
- async function run() {
178
- const response = await agent.run("session_xyz", "What is 750 multiplied by 14?");
179
- console.log(response); // AI answers via calculating transparently!
180
- }
181
- run();
182
- ```
183
-
184
- ## Advanced Usage
185
-
186
- ### Building Your Own Type-Safe Tools
187
-
188
- Using standard Zod schemas, we expose `createTool` to automatically synthesize MCP-compliant JSON schemas out-of-the-box.
189
-
190
- ```typescript
191
- import { z } from "zod";
192
- import { createTool } from "echo-ai-sdk";
193
-
194
- export const databaseTool = createTool({
195
- name: "query_db",
196
- description: "Execute read-only queries against the replica DB.",
197
- schema: z.object({
198
- sql: z.string().describe("The SQL Query syntax.")
199
- }),
200
- execute: async ({ sql }) => {
201
- // Implement your database logic
202
- return await db.query(sql);
203
- }
204
- });
205
- ```
206
-
207
- ### Direct Gateway Access (With Retry Backoff!)
208
-
209
- If you don't need the Agent Loop and simply want resilient generation or streaming, the Gateway natively features 3-tier exponential backoff under the hood!
210
-
211
- ```typescript
212
- import { AIModelGateway, OpenAIProvider, ChatRequest } from "echo-ai-sdk";
213
-
214
- const gateway = new AIModelGateway([
215
- new OpenAIProvider(process.env.OPENAI_API_KEY),
216
- // Add AnthropicProvider to handle OpenAI 500 API errors sequentially
217
- ]);
218
-
219
- const req: ChatRequest = {
220
- messages: [{ role: "user", content: "Tell me a story." }],
221
- model_family: "fast"
222
- };
223
-
224
- const stream = gateway.chatStream(req);
225
-
226
- for await(const token of stream) {
227
- process.stdout.write(token);
228
- }
229
- ```
230
-
231
- ### Structured Outputs (Enforcing Zod Schemas)
232
- Need the AI to respond in a strict format?
233
- ```typescript
234
- import { z } from 'zod';
235
-
236
- const myCustomSchema = z.object({
237
- analysis: z.string(),
238
- riskScore: z.number().min(1).max(10)
239
- });
240
-
241
- // Forces the agent to output the exact JSON object you requested!
242
- const result = await agent.executeStructured("session123", "Analyze these financial docs...", myCustomSchema);
243
- console.log(result.riskScore); // 100% Type-Safe Numbers!
244
- ```
245
-
246
- ### Telemetry & Tracking
247
- Expose hooks to pipe your LLM metrics safely straight to platforms like Datadog, Grafana, or LangSmith.
248
-
249
- ```typescript
250
- import { AgentExecutor } from "echo-ai-sdk";
251
-
252
- const executor = new AgentExecutor({
253
- gateway,
254
- memory,
255
- tools,
256
- telemetry: {
257
- onTokenUsage: (sessionId, provider, model, usage) => console.log(`Burned ${usage.total_tokens} tokens on ${model}!`),
258
- onToolStart: (sessionId, toolName, args) => console.log(`Agent executing ${toolName}!`)
259
- }
260
- });
261
- ```
262
-
263
- ## Error Handling
264
-
265
- Echo AI SDK embraces explicit semantic exceptions overriding generic javascript errors.
266
-
267
- ```typescript
268
- import { GatewayRoutingError } from "echo-ai-sdk";
269
-
270
- try {
271
- await gateway.chatComplete(req);
272
- } catch (e) {
273
- if (e instanceof GatewayRoutingError) {
274
- console.error("All providers went down. Terminal panic.");
275
- }
276
- }
277
- ```
278
-
279
- ### Prompt Templates & Registry
280
-
281
- Version your prompts like code. Supports immutable registrations, semantic versioning, and instant A/B rollbacks.
282
-
283
- ```typescript
284
- import { PromptTemplate, PromptRegistry } from "echo-ai-sdk";
285
-
286
- const registry = new PromptRegistry();
287
-
288
- registry.register(new PromptTemplate({
289
- name: "greet",
290
- version: "1.0.0",
291
- template: "Hello {{name}}, welcome to {{product}}!",
292
- requiredVars: ["name", "product"]
293
- }));
294
-
295
- const prompt = registry.getTemplate("greet");
296
- console.log(prompt.render({ name: "Alex", product: "EchoMind" }));
297
- // → "Hello Alex, welcome to EchoMind!"
298
- ```
299
-
300
- ### Gateway Middleware
301
-
302
- Intercept, transform, or log every request and response flowing through the gateway.
303
-
304
- ```typescript
305
- import { AIModelGateway } from "echo-ai-sdk";
306
-
307
- const gateway = new AIModelGateway([...providers]);
308
-
309
- gateway.use({
310
- onRequest: (req) => {
311
- console.log(`[Audit] Sending ${req.messages.length} messages`);
312
- return req;
313
- },
314
- onResponse: (res, req) => {
315
- console.log(`[Audit] Used ${res.usage?.total_tokens} tokens`);
316
- return res;
317
- },
318
- onError: (err, provider) => {
319
- console.error(`[Alert] ${provider} failed: ${err.message}`);
320
- }
321
- });
322
- ```
323
-
324
- ### Multi-Agent Orchestration
325
-
326
- #### AgentPipeline — Sequential Chaining
327
- Chain agents where each stage's output feeds into the next:
328
-
329
- ```typescript
330
- import { AgentPipeline } from "echo-ai-sdk";
331
-
332
- const pipeline = new AgentPipeline()
333
- .addStage("summarize", summaryAgent)
334
- .addStage("translate", translatorAgent)
335
- .addStage("format", formatterAgent);
336
-
337
- const result = await pipeline.run("session1", "Raw meeting transcript...");
338
- ```
339
-
340
- #### AgentRouter — Intent-Based Dispatch
341
- Route user input to specialized agents based on regex patterns:
342
-
343
- ```typescript
344
- import { AgentRouter } from "echo-ai-sdk";
345
-
346
- const router = new AgentRouter()
347
- .addRoute("support", /help|issue|bug/i, supportAgent)
348
- .addRoute("sales", /pricing|demo|buy/i, salesAgent)
349
- .setFallback(generalAgent);
350
-
351
- const answer = await router.route("session1", "I need help with billing");
352
- ```
353
-
354
- ### Response Caching
355
-
356
- Eliminate redundant API calls with TTL-based caching:
357
-
358
- ```typescript
359
- import { CachedGateway } from "echo-ai-sdk";
360
-
361
- const cached = new CachedGateway(gateway, 120_000); // 2-minute TTL
362
- const response = await cached.chatComplete(req); // Cache MISS → calls API
363
- const again = await cached.chatComplete(req); // Cache HIT → instant!
364
- ```
365
-
366
- ## 🎤 Voice Features (v1.2.0)
367
-
368
- Echo AI SDK provides plug-and-play **Speech-to-Text**, **Text-to-Speech**, and **Speaker Recognition** — all through a single unified client.
369
-
370
- ### Quick Start — EchoVoice
371
-
372
- ```typescript
373
- import { EchoVoice } from "echo-ai-sdk";
374
- import fs from "fs";
375
-
376
- // Auto-detects OPENAI_API_KEY for Whisper STT & TTS
377
- const voice = new EchoVoice();
378
- ```
379
-
380
- ---
381
-
382
- ### Speech-to-Text (STT)
383
-
384
- Convert audio files to text using OpenAI Whisper:
385
-
386
- ```typescript
387
- const audioBuffer = fs.readFileSync("meeting.wav");
388
-
389
- // Basic transcription
390
- const result = await voice.transcribe(audioBuffer);
391
- console.log(result.text); // → "Welcome to today's meeting..."
392
-
393
- // With language hint and timestamps
394
- const detailed = await voice.transcribe(audioBuffer, {
395
- language: "en",
396
- timestamps: true,
397
- temperature: 0.0
398
- });
399
-
400
- for (const segment of detailed.segments!) {
401
- console.log(`[${segment.start}s → ${segment.end}s] ${segment.text}`);
402
- }
403
- ```
404
-
405
- #### STT Options Reference
406
-
407
- | Option | Type | Description |
408
- |---|---|---|
409
- | `language` | `string` | ISO language code (e.g., `"en"`, `"es"`, `"ja"`) |
410
- | `prompt` | `string` | Guide the model's style or vocabulary |
411
- | `temperature` | `number` | 0.0 (deterministic) to 1.0 (creative) |
412
- | `timestamps` | `boolean` | Enable word-level timing segments |
413
-
414
- #### TranscriptionResult Type
415
-
416
- ```typescript
417
- interface TranscriptionResult {
418
- text: string; // Full transcribed text
419
- language?: string; // Detected language
420
- duration?: number; // Audio duration in seconds
421
- segments?: { // Time-aligned segments
422
- start: number;
423
- end: number;
424
- text: string;
425
- confidence?: number;
426
- }[];
427
- }
428
- ```
429
-
430
- ---
431
-
432
- ### Text-to-Speech (TTS)
433
-
434
- Convert text to lifelike audio:
435
-
436
- ```typescript
437
- // Basic synthesis
438
- const speech = await voice.speak("Hello, welcome to EchoMind!");
439
- fs.writeFileSync("greeting.mp3", speech.audio);
440
-
441
- // With voice selection and options
442
- const custom = await voice.speak("Breaking news: AI is everywhere.", {
443
- voice: "nova", // alloy | echo | fable | onyx | nova | shimmer
444
- speed: 1.2, // 0.25x to 4.0x
445
- format: "opus", // mp3 | opus | aac | flac | wav | pcm
446
- model: "tts-1-hd" // tts-1 (fast) or tts-1-hd (quality)
447
- });
448
- fs.writeFileSync("news.opus", custom.audio);
449
- ```
450
-
451
- #### TTS Options Reference
452
-
453
- | Option | Type | Default | Description |
454
- |---|---|---|---|
455
- | `voice` | `TTSVoice` | `"alloy"` | Voice persona |
456
- | `speed` | `number` | `1.0` | Playback speed (0.25–4.0) |
457
- | `format` | `TTSFormat` | `"mp3"` | Output audio format |
458
- | `model` | `string` | `"tts-1"` | `tts-1` (fast) or `tts-1-hd` (quality) |
459
-
460
- #### Available Voices
461
-
462
- | Voice | Tone |
463
- |---|---|
464
- | `alloy` | Balanced, neutral |
465
- | `echo` | Warm, conversational |
466
- | `fable` | British, narrative |
467
- | `onyx` | Deep, authoritative |
468
- | `nova` | Energetic, young |
469
- | `shimmer` | Soft, gentle |
470
-
471
- ---
472
-
473
- ### Speaker Recognition
474
-
475
- Enroll speakers, then identify or verify them from new audio samples. Powered by cosine similarity over voice embeddings.
476
-
477
- #### Direct VoiceprintStore Usage (No API Required)
478
-
479
- ```typescript
480
- import { VoiceprintStore } from "echo-ai-sdk";
481
-
482
- const store = new VoiceprintStore(0.80); // 80% match threshold
483
-
484
- // Enroll speakers with pre-computed embeddings
485
- store.enroll("spk_001", "Alice", [0.12, 0.84, 0.33, ...]);
486
- store.enroll("spk_002", "Bob", [0.91, 0.15, 0.67, ...]);
487
-
488
- // Identify an unknown speaker
489
- const match = store.identify(unknownEmbedding);
490
- if (match.matched) {
491
- console.log(`Identified: ${match.speakerName} (${(match.confidence * 100).toFixed(1)}%)`);
492
- } else {
493
- console.log("Unknown speaker");
494
- }
495
-
496
- // Verify a specific speaker
497
- const verification = store.verify("spk_001", audioEmbedding);
498
- console.log(`Verified: ${verification.verified}, Confidence: ${verification.confidence}`);
499
-
500
- // Manage speakers
501
- store.listSpeakers(); // → [SpeakerProfile, ...]
502
- store.removeSpeaker("spk_001"); // Remove enrollment
503
- ```
504
-
505
- #### With EchoVoice Facade (Automatic Embedding Extraction)
506
-
507
- ```typescript
508
- import { EchoVoice, BaseSpeakerRecognizer } from "echo-ai-sdk";
509
-
510
- // Implement your own embedding extractor
511
- class MySpeakerModel extends BaseSpeakerRecognizer {
512
- get providerName() { return "my-model"; }
513
- async extractEmbedding(audio: Buffer): Promise<number[]> {
514
- // Call your ML model, Azure Speaker API, or custom service
515
- return [0.12, 0.84, 0.33, /* ... */];
516
- }
517
- }
518
-
519
- const voice = new EchoVoice({ recognizer: new MySpeakerModel() });
520
-
521
- // Enroll from raw audio
522
- await voice.enrollSpeaker("spk_001", "Alice", aliceAudioBuffer);
523
-
524
- // Identify from new audio
525
- const who = await voice.identifySpeaker(unknownAudioBuffer);
526
- console.log(who.speakerName); // → "Alice"
527
-
528
- // Verify a specific person
529
- const check = await voice.verifySpeaker("spk_001", testAudioBuffer);
530
- console.log(check.verified); // → true
531
- ```
532
-
533
- ---
534
-
535
- ### Bring Your Own Provider
536
-
537
- All voice components are fully pluggable. Implement the abstract base classes to use any provider:
538
-
539
- ```typescript
540
- import { BaseSTTProvider, BaseTTSProvider, EchoVoice } from "echo-ai-sdk";
541
-
542
- // Custom Deepgram STT
543
- class DeepgramSTT extends BaseSTTProvider {
544
- get providerName() { return "deepgram"; }
545
- async transcribe(audio: Buffer, options?) {
546
- // Your Deepgram integration
547
- return { text: "transcribed text" };
548
- }
549
- }
550
-
551
- // Custom ElevenLabs TTS
552
- class ElevenLabsTTS extends BaseTTSProvider {
553
- get providerName() { return "elevenlabs"; }
554
- async synthesize(text: string, options?) {
555
- // Your ElevenLabs integration
556
- return { audio: Buffer.from([]), format: "mp3" as const };
557
- }
558
- }
559
-
560
- // Plug them in
561
- const voice = new EchoVoice({
562
- stt: new DeepgramSTT(),
563
- tts: new ElevenLabsTTS()
564
- });
565
- ```
566
-
567
- ---
568
-
569
- ## Complete API Reference
570
-
571
- ### Core Classes
572
-
573
- | Class | Description |
574
- |---|---|
575
- | `EchoAI` | Main client facade for LLM chat & agents |
576
- | `EchoVoice` | Unified voice client (STT + TTS + Speaker Recognition) |
577
- | `AIModelGateway` | Multi-provider gateway with failover & backoff |
578
- | `AgentExecutor` | ReAct autonomous agent loop with tool execution |
579
- | `CachedGateway` | TTL-based response caching wrapper |
580
-
581
- ### Agent Classes
582
-
583
- | Class | Description |
584
- |---|---|
585
- | `ChatAgent` | Simple conversational agent |
586
- | `ToolAgent` | Agent with tool-calling capabilities |
587
- | `AgentPipeline` | Sequential multi-agent chaining |
588
- | `AgentRouter` | Intent-based agent routing |
589
-
590
- ### Voice Classes
591
-
592
- | Class | Description |
593
- |---|---|
594
- | `OpenAIWhisperSTT` | Whisper-based speech-to-text |
595
- | `OpenAITTS` | OpenAI text-to-speech (6 voices) |
596
- | `VoiceprintStore` | Speaker enrollment & cosine similarity matching |
597
- | `BaseSTTProvider` | Abstract STT provider (extend for custom) |
598
- | `BaseTTSProvider` | Abstract TTS provider (extend for custom) |
599
- | `BaseSpeakerRecognizer` | Abstract speaker embedding extractor |
600
-
601
- ### Error Classes
602
-
603
- | Exception | When It Fires |
604
- |---|---|
605
- | `GatewayRoutingError` | All providers exhausted |
606
- | `ConfigurationError` | Missing API keys or invalid setup |
607
- | `ValidationError` | Invalid inputs to any public API |
608
- | `ToolExecutionError` | Tool fails during agent loop |
609
- | `AgentIterationLimitError` | Agent exceeds max iterations |
610
- | `StructuredOutputError` | LLM output fails JSON parsing |
611
- | `PromptVersionError` | Invalid semver or duplicate prompt version |
612
-
613
- ### Utilities
614
-
615
- | Class/Function | Description |
616
- |---|---|
617
- | `createTool` | Zod-to-MCP tool factory |
618
- | `PromptTemplate` | Versioned prompt with mustache rendering |
619
- | `PromptRegistry` | Prompt version management with A/B rollback |
620
- | `InMemoryStore` | Session-based conversation memory |
621
- | `AgentTelemetry` | Lifecycle hooks interface for APM integration |
622
-
623
- ## License
624
- MIT © EchoMind Team
1
+ # Echo AI SDK
2
+
3
+ The all-in-one AI platform for chat, voice, agents, and **embeddable customer support chatbots**.
4
+
5
+ ![npm](https://img.shields.io/npm/v/echo-ai-sdk)
6
+ ![license](https://img.shields.io/npm/l/echo-ai-sdk)
7
+ ![typescript](https://img.shields.io/badge/Language-TypeScript-blue)
8
+
9
+ ## CI/CD Status
10
+
11
+ ### Build & Test
12
+ ![CI](https://github.com/Endsett/Echo-ai-sdk/workflows/CI/badge.svg)
13
+ ![Tests](https://github.com/Endsett/Echo-ai-sdk/workflows/Test/badge.svg)
14
+ ![Coverage](https://codecov.io/gh/Endsett/Echo-ai-sdk/branch/main/graph/badge.svg)
15
+
16
+ ### Quality & Security
17
+ ![CodeQL](https://github.com/Endsett/Echo-ai-sdk/workflows/CodeQL/badge.svg)
18
+ ![Security Scan](https://github.com/Endsett/Echo-ai-sdk/workflows/Security/badge.svg)
19
+ ![Dependency Review](https://github.com/Endsett/Echo-ai-sdk/workflows/Dependency%20Review/badge.svg)
20
+
21
+ ### Release & Publishing
22
+ ![Release](https://github.com/Endsett/Echo-ai-sdk/workflows/Release/badge.svg)
23
+ ![npm](https://img.shields.io/npm/dt/echo-ai-sdk)
24
+ ![GitHub release](https://img.shields.io/github/release/Endsett/Echo-ai-sdk)
25
+
26
+ ### Code Quality
27
+ ![ESLint](https://img.shields.io/badge/ESLint-passing-brightgreen)
28
+ ![TypeScript](https://img.shields.io/badge/TypeScript-5.3-blue)
29
+ ![Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4)
30
+
31
+ ### Performance
32
+ ![Bundle Size](https://img.shields.io/bundlephobia/minzip/echo-ai-sdk)
33
+ ![Dependencies](https://img.shields.io/librariesio/dependent-repositories/npm/echo-ai-sdk)
34
+
35
+ ## 🚀 Deploy an AI Chatbot on Your Website in 5 Minutes
36
+
37
+ ```bash
38
+ npm install echo-ai-sdk zod openai express cors
39
+ ```
40
+
41
+ ### Step 1: Create Your Server (3 lines)
42
+
43
+ ```typescript
44
+ import { startChatServer, AIModelGateway, OpenAIProvider } from "echo-ai-sdk";
45
+
46
+ startChatServer({
47
+ gateway: new AIModelGateway([new OpenAIProvider(process.env.OPENAI_API_KEY!)]),
48
+ companyName: "Acme Inc",
49
+ companyDescription: "We sell premium widgets and gadgets.",
50
+ apiConnector: { // Optional: connect to your website API
51
+ baseUrl: "https://api.acme.com/v1",
52
+ authToken: process.env.API_TOKEN
53
+ },
54
+ port: 3456
55
+ });
56
+ ```
57
+
58
+ ### Step 2: Embed the Widget on Your Website (1 line)
59
+
60
+ ```typescript
61
+ import { ChatWidget } from "echo-ai-sdk";
62
+
63
+ const embedCode = ChatWidget.generate({
64
+ serverEndpoint: "https://your-server.com",
65
+ title: "Acme Support",
66
+ subtitle: "We reply instantly ⚡",
67
+ enableVoice: true, // Enable microphone input
68
+ theme: {
69
+ primaryColor: "#6366f1",
70
+ position: "bottom-right"
71
+ }
72
+ });
73
+
74
+ // Paste `embedCode` into your HTML, or serve it dynamically
75
+ console.log(embedCode);
76
+ ```
77
+
78
+ ### Step 3: Done! 🎉
79
+
80
+ Your website now has a fully functional AI chatbot with **Tier 1 Enterprise Features**:
81
+
82
+ #### 🧠 RAG Knowledge Base (Eliminate Hallucinations)
83
+ Ground your AI in your own data (PDFs, URLs, text).
84
+ ```typescript
85
+ await bot.knowledgeBase.ingestURL("https://acme.com/shipping-policy");
86
+ await bot.knowledgeBase.ingestText("Our warranty lasts 2 years...");
87
+ ```
88
+
89
+ #### 📊 Conversation Analytics
90
+ Track resolution rates, CSAT, token costs, and response times.
91
+ ```typescript
92
+ const stats = bot.analytics.getSnapshot();
93
+ console.log(`Resolution Rate: ${stats.resolutionRate * 100}%`);
94
+ ```
95
+
96
+ #### 🤝 Human Handoff
97
+ Auto-escalate to human agents when the bot detects frustration or explicit requests.
98
+ ```typescript
99
+ const bot = new CustomerSupportBot({
100
+ handoff: {
101
+ webhookUrl: "https://your-helpdesk.com/handoff",
102
+ webhookSecret: "secure_token_123"
103
+ }
104
+ });
105
+ ```
106
+
107
+ ### 🌟 Tier 1 Perfection Features
108
+
109
+ #### 🧠 RAG Persistence
110
+ Ground your bot in your data and keep it there across restarts.
111
+ ```typescript
112
+ await bot.knowledgeBase.ingestText("Your knowledge text here...");
113
+ await bot.knowledgeBase.save("./knowledge-base.json"); // Save to disk
114
+ await bot.knowledgeBase.load("./knowledge-base.json"); // Load back
115
+ ```
116
+
117
+ #### 📊 High-Precision Analytics & Cost Control
118
+ Track every penny with accurate token estimation and pricing maps.
119
+ ```typescript
120
+ const stats = bot.analytics.getSnapshot();
121
+ console.log(`Total Cost: $${stats.totalCostUsd}`);
122
+ console.log(`Handoff Rate: ${stats.handoffRate * 100}%`);
123
+ ```
124
+
125
+ #### 🤝 Sentiment-Aware Human Handoff
126
+ Detect frustration before it's too late. The bot tracks "negative strikes" and auto-summarizes problems for human agents.
127
+ ```typescript
128
+ const bot = new CustomerSupportBot({
129
+ handoff: {
130
+ webhookUrl: "https://your-helpdesk.com/webhook",
131
+ negativeSentimentThreshold: 3, // Escalates after 3 angry turns
132
+ }
133
+ });
134
+ ```
135
+
136
+ #### 🔌 Middleware API
137
+ Hook into every chat turn to add custom logic, logging, or transformations.
138
+ ```typescript
139
+ bot.use(async ({ sessionId, message }) => {
140
+ console.log(`[Turn] ${sessionId}: ${message}`);
141
+ });
142
+ ```
143
+
144
+
145
+ ### 🚀 Tier 2 Pro-Grade Features
146
+
147
+ #### 🌐 Omnichannel Sync
148
+ Connect your bot to Slack and Telegram while maintaining a single user context.
149
+ ```typescript
150
+ import { TelegramAdapter, SlackAdapter } from "echo-ai-sdk";
151
+
152
+ const tg = new TelegramAdapter({ bot, token: "TG_TOKEN" });
153
+ await tg.start();
154
+
155
+ const slack = new SlackAdapter({ bot, signingSecret: "...", token: "..." });
156
+ await slack.start();
157
+ ```
158
+
159
+ #### 💾 Persistent Session Store
160
+ Move beyond memory. Use `FileSessionStore` for local persistence or implement `SessionStore` for Redis.
161
+ ```typescript
162
+ import { FileSessionStore } from "echo-ai-sdk";
163
+ const bot = new CustomerSupportBot({
164
+ sessionStore: new FileSessionStore("./sessions"),
165
+ });
166
+ ```
167
+
168
+ #### 🧠 Honcho Memory — Semantic, Reasoning-Based Agent Memory
169
+ Replace simple in-memory chat history with [Honcho's](https://honcho.dev) production-grade memory system. Your agents gain **cross-session context**, **semantic search**, **entity understanding**, and **continual learning** — all as a drop-in `BaseMemoryStore`.
170
+
171
+ ```typescript
172
+ import { HonchoMemoryStore, AgentExecutor } from "echo-ai-sdk";
173
+
174
+ // Drop-in replacement for InMemoryStore
175
+ const memory = new HonchoMemoryStore({
176
+ apiKey: process.env.HONCHO_API_KEY,
177
+ workspaceId: "my-app",
178
+ });
179
+
180
+ const agent = new AgentExecutor({ gateway, memory, tools });
181
+
182
+ // Agents now automatically persist and recall context across sessions!
183
+ ```
184
+
185
+ **Semantic Search** — Find memories by meaning, not keywords:
186
+ ```typescript
187
+ const results = await memory.searchMemory("user", "coding preferences", {
188
+ topK: 5,
189
+ maxDistance: 0.7,
190
+ });
191
+ // Synthesized conclusions like "User prefers TypeScript with functional patterns"
192
+ ```
193
+
194
+ **Peer Insights** Ask Honcho what it knows about a user:
195
+ ```typescript
196
+ const insight = await memory.getInsights("user", "What motivates this user?");
197
+ // → "The user is motivated by building AI products that help people..."
198
+ ```
199
+
200
+ **Unified Semantic Search** Combine Honcho reasoning with local vector stores:
201
+ ```typescript
202
+ import { SemanticMemorySearch, HonchoMemoryStore } from "echo-ai-sdk";
203
+
204
+ const search = new SemanticMemorySearch(honchoStore, optionalVectorStore);
205
+ const results = await search.search("user", "project architecture");
206
+ ```
207
+
208
+ #### 📈 Outcome-Based Billing & ROI
209
+ Track the *real value* of your AI by recording business outcomes.
210
+ ```typescript
211
+ // Inside a tool or middleware
212
+ bot.trackOutcome(sessionId, "lead_captured", 50.0);
213
+
214
+ const stats = bot.analytics.getSnapshot();
215
+ console.log(`ROI: ${stats.roi * 100}%`);
216
+ console.log(`Value Generated: $${stats.totalValueGeneratedUsd}`);
217
+ ```
218
+
219
+ ---
220
+
221
+ ### 🚀 Tier 3 Advanced Agent Capabilities
222
+
223
+ #### 🤖 Advanced Reasoning Agents
224
+ Go beyond simple tool execution with sophisticated reasoning patterns:
225
+
226
+ ```typescript
227
+ import { ReActAgent, CoTAgent, ToTAgent } from "echo-ai-sdk";
228
+
229
+ // ReAct with Reflection - Thinks before acting
230
+ const reactAgent = new ReActAgent({
231
+ gateway,
232
+ memory,
233
+ tools: [searchTool, calculatorTool],
234
+ reactOptions: {
235
+ enableReflection: true,
236
+ confidenceThreshold: 0.8
237
+ }
238
+ });
239
+
240
+ // Chain of Thought - Step-by-step reasoning
241
+ const cotAgent = new CoTAgent({
242
+ gateway,
243
+ memory,
244
+ tools: [analysisTool],
245
+ cotOptions: {
246
+ maxSteps: 10,
247
+ requireConclusions: true
248
+ }
249
+ });
250
+
251
+ // Tree of Thoughts - Explores multiple solution paths
252
+ const totAgent = new ToTAgent({
253
+ gateway,
254
+ memory,
255
+ tools: [creativeTool],
256
+ totOptions: {
257
+ maxDepth: 4,
258
+ breadth: 3,
259
+ evaluationMethod: "score"
260
+ }
261
+ });
262
+
263
+ // Stream reasoning process
264
+ for await (const event of reactAgent.executeStream(sessionId, task)) {
265
+ console.log(`${event.type}: ${event.content}`);
266
+ }
267
+ ```
268
+
269
+ #### ⚡ Enhanced Parallel Execution
270
+ Execute tools simultaneously with dependency resolution:
271
+
272
+ ```typescript
273
+ import { EnhancedAgentExecutor, ToolDependency } from "echo-ai-sdk";
274
+
275
+ const executor = new EnhancedAgentExecutor({
276
+ gateway,
277
+ memory,
278
+ tools: [searchTool, weatherTool, calculatorTool],
279
+ toolDependencies: [
280
+ { toolName: "weather", dependsOn: ["search"] } // Weather needs search results first
281
+ ],
282
+ executionOptions: {
283
+ enableParallel: true,
284
+ maxParallelTools: 5,
285
+ toolTimeout: 10000,
286
+ streamToolResults: true
287
+ }
288
+ });
289
+
290
+ // Tools execute in parallel when possible
291
+ const result = await executor.execute(sessionId, complexTask);
292
+ ```
293
+
294
+ #### 🔄 Multi-Agent Collaboration
295
+ Coordinate multiple agents for complex workflows:
296
+
297
+ ```typescript
298
+ import { AgentTeam, AgentOrchestrator, DynamicAgentSelector } from "echo-ai-sdk";
299
+
300
+ // Create an agent team
301
+ const team = new AgentTeam({
302
+ name: "ResearchTeam",
303
+ loadBalancingStrategy: "capability_based",
304
+ communicationProtocol: "direct"
305
+ }, memory);
306
+
307
+ // Register specialized agents
308
+ team.registerAgent({
309
+ id: "researcher",
310
+ name: "Research Specialist",
311
+ capabilities: [{
312
+ name: "research",
313
+ tools: ["search"],
314
+ reasoningPatterns: ["react"],
315
+ specialties: ["academic_research"]
316
+ }],
317
+ status: "active",
318
+ currentLoad: 0,
319
+ maxConcurrentTasks: 3
320
+ });
321
+
322
+ // Delegate tasks to best agent
323
+ const taskId = await team.delegateTask("coordinator", {
324
+ query: "Latest AI research papers",
325
+ analysis: "summarize"
326
+ }, {
327
+ capabilities: ["research"],
328
+ maxResponseTime: 5000
329
+ });
330
+
331
+ // Orchestrate complex workflows
332
+ const orchestrator = new AgentOrchestrator({}, memory);
333
+ const workflow = {
334
+ id: "research_pipeline",
335
+ steps: [
336
+ { id: "collect", requiredCapabilities: ["research"] },
337
+ { id: "analyze", requiredCapabilities: ["analysis"] },
338
+ { id: "summarize", requiredCapabilities: ["summarization"] }
339
+ ],
340
+ orchestrationPattern: "sequential"
341
+ };
342
+
343
+ const executionId = await orchestrator.executeWorkflow(
344
+ workflow.id,
345
+ { topic: "quantum computing" }
346
+ );
347
+ ```
348
+
349
+ #### 🎯 Dynamic Agent Selection
350
+ Automatically select the best agent based on capabilities, performance, and cost:
351
+
352
+ ```typescript
353
+ const selector = new DynamicAgentSelector();
354
+
355
+ // Select best agent for task
356
+ const selection = selector.selectAgent(agents, {
357
+ requiredCapabilities: ["analysis", "computation"],
358
+ maxResponseTime: 3000,
359
+ costLimit: 0.10,
360
+ priority: "high"
361
+ }, "performance_first");
362
+
363
+ console.log(`Selected: ${selection.agentId} (score: ${selection.score})`);
364
+ console.log(`Reasoning: ${selection.reasoning}`);
365
+ ```
366
+
367
+ ---
368
+
369
+ ## Installation
370
+
371
+ ```sh
372
+ npm install echo-ai-sdk zod
373
+ ```
374
+
375
+ You'll need the respective provider SDKs if you wish to utilize them:
376
+
377
+ ```sh
378
+ npm install openai @anthropic-ai/sdk
379
+ ```
380
+
381
+ ## Quick Start (The Facade)
382
+
383
+ The quickest way to get started is by instantiating the `EchoAI` client. It automatically sniffs your `process.env` for `OPENAI_API_KEY` or `ANTHROPIC_API_KEY` and constructs the highly available gateway.
384
+
385
+ ```typescript
386
+ import { EchoAI, calculatorTool } from "echo-ai-sdk";
387
+
388
+ // Assumes process.env.OPENAI_API_KEY is populated.
389
+ const ai = new EchoAI();
390
+
391
+ const agent = ai.createToolAgent([calculatorTool]);
392
+
393
+ async function run() {
394
+ const response = await agent.run("session_xyz", "What is 750 multiplied by 14?");
395
+ console.log(response); // AI answers via calculating transparently!
396
+ }
397
+ run();
398
+ ```
399
+
400
+ ## Advanced Usage
401
+
402
+ ### Building Your Own Type-Safe Tools
403
+
404
+ Using standard Zod schemas, we expose `createTool` to automatically synthesize MCP-compliant JSON schemas out-of-the-box.
405
+
406
+ ```typescript
407
+ import { z } from "zod";
408
+ import { createTool } from "echo-ai-sdk";
409
+
410
+ export const databaseTool = createTool({
411
+ name: "query_db",
412
+ description: "Execute read-only queries against the replica DB.",
413
+ schema: z.object({
414
+ sql: z.string().describe("The SQL Query syntax.")
415
+ }),
416
+ execute: async ({ sql }) => {
417
+ // Implement your database logic
418
+ return await db.query(sql);
419
+ }
420
+ });
421
+ ```
422
+
423
+ ### Direct Gateway Access (With Retry Backoff!)
424
+
425
+ If you don't need the Agent Loop and simply want resilient generation or streaming, the Gateway natively features 3-tier exponential backoff under the hood!
426
+
427
+ ```typescript
428
+ import { AIModelGateway, OpenAIProvider, ChatRequest } from "echo-ai-sdk";
429
+
430
+ const gateway = new AIModelGateway([
431
+ new OpenAIProvider(process.env.OPENAI_API_KEY),
432
+ // Add AnthropicProvider to handle OpenAI 500 API errors sequentially
433
+ ]);
434
+
435
+ const req: ChatRequest = {
436
+ messages: [{ role: "user", content: "Tell me a story." }],
437
+ model_family: "fast"
438
+ };
439
+
440
+ const stream = gateway.chatStream(req);
441
+
442
+ for await(const token of stream) {
443
+ process.stdout.write(token);
444
+ }
445
+ ```
446
+
447
+ ### Structured Outputs (Enforcing Zod Schemas)
448
+ Need the AI to respond in a strict format?
449
+ ```typescript
450
+ import { z } from 'zod';
451
+
452
+ const myCustomSchema = z.object({
453
+ analysis: z.string(),
454
+ riskScore: z.number().min(1).max(10)
455
+ });
456
+
457
+ // Forces the agent to output the exact JSON object you requested!
458
+ const result = await agent.executeStructured("session123", "Analyze these financial docs...", myCustomSchema);
459
+ console.log(result.riskScore); // 100% Type-Safe Numbers!
460
+ ```
461
+
462
+ ### Telemetry & Tracking
463
+ Expose hooks to pipe your LLM metrics safely straight to platforms like Datadog, Grafana, or LangSmith.
464
+
465
+ ```typescript
466
+ import { AgentExecutor } from "echo-ai-sdk";
467
+
468
+ const executor = new AgentExecutor({
469
+ gateway,
470
+ memory,
471
+ tools,
472
+ telemetry: {
473
+ onTokenUsage: (sessionId, provider, model, usage) => console.log(`Burned ${usage.total_tokens} tokens on ${model}!`),
474
+ onToolStart: (sessionId, toolName, args) => console.log(`Agent executing ${toolName}!`)
475
+ }
476
+ });
477
+ ```
478
+
479
+ ## Error Handling
480
+
481
+ Echo AI SDK embraces explicit semantic exceptions overriding generic javascript errors.
482
+
483
+ ```typescript
484
+ import { GatewayRoutingError } from "echo-ai-sdk";
485
+
486
+ try {
487
+ await gateway.chatComplete(req);
488
+ } catch (e) {
489
+ if (e instanceof GatewayRoutingError) {
490
+ console.error("All providers went down. Terminal panic.");
491
+ }
492
+ }
493
+ ```
494
+
495
+ ### Prompt Templates & Registry
496
+
497
+ Version your prompts like code. Supports immutable registrations, semantic versioning, and instant A/B rollbacks.
498
+
499
+ ```typescript
500
+ import { PromptTemplate, PromptRegistry } from "echo-ai-sdk";
501
+
502
+ const registry = new PromptRegistry();
503
+
504
+ registry.register(new PromptTemplate({
505
+ name: "greet",
506
+ version: "1.0.0",
507
+ template: "Hello {{name}}, welcome to {{product}}!",
508
+ requiredVars: ["name", "product"]
509
+ }));
510
+
511
+ const prompt = registry.getTemplate("greet");
512
+ console.log(prompt.render({ name: "Alex", product: "EchoMind" }));
513
+ // "Hello Alex, welcome to EchoMind!"
514
+ ```
515
+
516
+ ### Gateway Middleware
517
+
518
+ Intercept, transform, or log every request and response flowing through the gateway.
519
+
520
+ ```typescript
521
+ import { AIModelGateway } from "echo-ai-sdk";
522
+
523
+ const gateway = new AIModelGateway([...providers]);
524
+
525
+ gateway.use({
526
+ onRequest: (req) => {
527
+ console.log(`[Audit] Sending ${req.messages.length} messages`);
528
+ return req;
529
+ },
530
+ onResponse: (res, req) => {
531
+ console.log(`[Audit] Used ${res.usage?.total_tokens} tokens`);
532
+ return res;
533
+ },
534
+ onError: (err, provider) => {
535
+ console.error(`[Alert] ${provider} failed: ${err.message}`);
536
+ }
537
+ });
538
+ ```
539
+
540
+ ### Multi-Agent Orchestration
541
+
542
+ #### AgentPipeline Sequential Chaining
543
+ Chain agents where each stage's output feeds into the next:
544
+
545
+ ```typescript
546
+ import { AgentPipeline } from "echo-ai-sdk";
547
+
548
+ const pipeline = new AgentPipeline()
549
+ .addStage("summarize", summaryAgent)
550
+ .addStage("translate", translatorAgent)
551
+ .addStage("format", formatterAgent);
552
+
553
+ const result = await pipeline.run("session1", "Raw meeting transcript...");
554
+ ```
555
+
556
+ #### AgentRouter Intent-Based Dispatch
557
+ Route user input to specialized agents based on regex patterns:
558
+
559
+ ```typescript
560
+ import { AgentRouter } from "echo-ai-sdk";
561
+
562
+ const router = new AgentRouter()
563
+ .addRoute("support", /help|issue|bug/i, supportAgent)
564
+ .addRoute("sales", /pricing|demo|buy/i, salesAgent)
565
+ .setFallback(generalAgent);
566
+
567
+ const answer = await router.route("session1", "I need help with billing");
568
+ ```
569
+
570
+ ### Response Caching
571
+
572
+ Eliminate redundant API calls with TTL-based caching:
573
+
574
+ ```typescript
575
+ import { CachedGateway } from "echo-ai-sdk";
576
+
577
+ const cached = new CachedGateway(gateway, 120_000); // 2-minute TTL
578
+ const response = await cached.chatComplete(req); // Cache MISS calls API
579
+ const again = await cached.chatComplete(req); // Cache HIT → instant!
580
+ ```
581
+
582
+ ## 🎤 Voice Features (v1.2.0)
583
+
584
+ Echo AI SDK provides plug-and-play **Speech-to-Text**, **Text-to-Speech**, and **Speaker Recognition** — all through a single unified client.
585
+
586
+ ### Quick Start EchoVoice
587
+
588
+ ```typescript
589
+ import { EchoVoice } from "echo-ai-sdk";
590
+ import fs from "fs";
591
+
592
+ // Auto-detects OPENAI_API_KEY for Whisper STT & TTS
593
+ const voice = new EchoVoice();
594
+ ```
595
+
596
+ ---
597
+
598
+ ### Speech-to-Text (STT)
599
+
600
+ Convert audio files to text using OpenAI Whisper:
601
+
602
+ ```typescript
603
+ const audioBuffer = fs.readFileSync("meeting.wav");
604
+
605
+ // Basic transcription
606
+ const result = await voice.transcribe(audioBuffer);
607
+ console.log(result.text); // "Welcome to today's meeting..."
608
+
609
+ // With language hint and timestamps
610
+ const detailed = await voice.transcribe(audioBuffer, {
611
+ language: "en",
612
+ timestamps: true,
613
+ temperature: 0.0
614
+ });
615
+
616
+ for (const segment of detailed.segments!) {
617
+ console.log(`[${segment.start}s ${segment.end}s] ${segment.text}`);
618
+ }
619
+ ```
620
+
621
+ #### STT Options Reference
622
+
623
+ | Option | Type | Description |
624
+ |---|---|---|
625
+ | `language` | `string` | ISO language code (e.g., `"en"`, `"es"`, `"ja"`) |
626
+ | `prompt` | `string` | Guide the model's style or vocabulary |
627
+ | `temperature` | `number` | 0.0 (deterministic) to 1.0 (creative) |
628
+ | `timestamps` | `boolean` | Enable word-level timing segments |
629
+
630
+ #### TranscriptionResult Type
631
+
632
+ ```typescript
633
+ interface TranscriptionResult {
634
+ text: string; // Full transcribed text
635
+ language?: string; // Detected language
636
+ duration?: number; // Audio duration in seconds
637
+ segments?: { // Time-aligned segments
638
+ start: number;
639
+ end: number;
640
+ text: string;
641
+ confidence?: number;
642
+ }[];
643
+ }
644
+ ```
645
+
646
+ ---
647
+
648
+ ### Text-to-Speech (TTS)
649
+
650
+ Convert text to lifelike audio:
651
+
652
+ ```typescript
653
+ // Basic synthesis
654
+ const speech = await voice.speak("Hello, welcome to EchoMind!");
655
+ fs.writeFileSync("greeting.mp3", speech.audio);
656
+
657
+ // With voice selection and options
658
+ const custom = await voice.speak("Breaking news: AI is everywhere.", {
659
+ voice: "nova", // alloy | echo | fable | onyx | nova | shimmer
660
+ speed: 1.2, // 0.25x to 4.0x
661
+ format: "opus", // mp3 | opus | aac | flac | wav | pcm
662
+ model: "tts-1-hd" // tts-1 (fast) or tts-1-hd (quality)
663
+ });
664
+ fs.writeFileSync("news.opus", custom.audio);
665
+ ```
666
+
667
+ #### TTS Options Reference
668
+
669
+ | Option | Type | Default | Description |
670
+ |---|---|---|---|
671
+ | `voice` | `TTSVoice` | `"alloy"` | Voice persona |
672
+ | `speed` | `number` | `1.0` | Playback speed (0.25–4.0) |
673
+ | `format` | `TTSFormat` | `"mp3"` | Output audio format |
674
+ | `model` | `string` | `"tts-1"` | `tts-1` (fast) or `tts-1-hd` (quality) |
675
+
676
+ #### Available Voices
677
+
678
+ | Voice | Tone |
679
+ |---|---|
680
+ | `alloy` | Balanced, neutral |
681
+ | `echo` | Warm, conversational |
682
+ | `fable` | British, narrative |
683
+ | `onyx` | Deep, authoritative |
684
+ | `nova` | Energetic, young |
685
+ | `shimmer` | Soft, gentle |
686
+
687
+ ---
688
+
689
+ ### Speaker Recognition
690
+
691
+ Enroll speakers, then identify or verify them from new audio samples. Powered by cosine similarity over voice embeddings.
692
+
693
+ #### Direct VoiceprintStore Usage (No API Required)
694
+
695
+ ```typescript
696
+ import { VoiceprintStore } from "echo-ai-sdk";
697
+
698
+ const store = new VoiceprintStore(0.80); // 80% match threshold
699
+
700
+ // Enroll speakers with pre-computed embeddings
701
+ store.enroll("spk_001", "Alice", [0.12, 0.84, 0.33, ...]);
702
+ store.enroll("spk_002", "Bob", [0.91, 0.15, 0.67, ...]);
703
+
704
+ // Identify an unknown speaker
705
+ const match = store.identify(unknownEmbedding);
706
+ if (match.matched) {
707
+ console.log(`Identified: ${match.speakerName} (${(match.confidence * 100).toFixed(1)}%)`);
708
+ } else {
709
+ console.log("Unknown speaker");
710
+ }
711
+
712
+ // Verify a specific speaker
713
+ const verification = store.verify("spk_001", audioEmbedding);
714
+ console.log(`Verified: ${verification.verified}, Confidence: ${verification.confidence}`);
715
+
716
+ // Manage speakers
717
+ store.listSpeakers(); // → [SpeakerProfile, ...]
718
+ store.removeSpeaker("spk_001"); // Remove enrollment
719
+ ```
720
+
721
+ #### With EchoVoice Facade (Automatic Embedding Extraction)
722
+
723
+ ```typescript
724
+ import { EchoVoice, BaseSpeakerRecognizer } from "echo-ai-sdk";
725
+
726
+ // Implement your own embedding extractor
727
+ class MySpeakerModel extends BaseSpeakerRecognizer {
728
+ get providerName() { return "my-model"; }
729
+ async extractEmbedding(audio: Buffer): Promise<number[]> {
730
+ // Call your ML model, Azure Speaker API, or custom service
731
+ return [0.12, 0.84, 0.33, /* ... */];
732
+ }
733
+ }
734
+
735
+ const voice = new EchoVoice({ recognizer: new MySpeakerModel() });
736
+
737
+ // Enroll from raw audio
738
+ await voice.enrollSpeaker("spk_001", "Alice", aliceAudioBuffer);
739
+
740
+ // Identify from new audio
741
+ const who = await voice.identifySpeaker(unknownAudioBuffer);
742
+ console.log(who.speakerName); // → "Alice"
743
+
744
+ // Verify a specific person
745
+ const check = await voice.verifySpeaker("spk_001", testAudioBuffer);
746
+ console.log(check.verified); // → true
747
+ ```
748
+
749
+ ---
750
+
751
+ ### Bring Your Own Provider
752
+
753
+ All voice components are fully pluggable. Implement the abstract base classes to use any provider:
754
+
755
+ ```typescript
756
+ import { BaseSTTProvider, BaseTTSProvider, EchoVoice } from "echo-ai-sdk";
757
+
758
+ // Custom Deepgram STT
759
+ class DeepgramSTT extends BaseSTTProvider {
760
+ get providerName() { return "deepgram"; }
761
+ async transcribe(audio: Buffer, options?) {
762
+ // Your Deepgram integration
763
+ return { text: "transcribed text" };
764
+ }
765
+ }
766
+
767
+ // Custom ElevenLabs TTS
768
+ class ElevenLabsTTS extends BaseTTSProvider {
769
+ get providerName() { return "elevenlabs"; }
770
+ async synthesize(text: string, options?) {
771
+ // Your ElevenLabs integration
772
+ return { audio: Buffer.from([]), format: "mp3" as const };
773
+ }
774
+ }
775
+
776
+ // Plug them in
777
+ const voice = new EchoVoice({
778
+ stt: new DeepgramSTT(),
779
+ tts: new ElevenLabsTTS()
780
+ });
781
+ ```
782
+
783
+ ---
784
+
785
+ ## Complete API Reference
786
+
787
+ ### Core Classes
788
+
789
+ | Class | Description |
790
+ |---|---|
791
+ | `EchoAI` | Main client facade for LLM chat & agents |
792
+ | `EchoVoice` | Unified voice client (STT + TTS + Speaker Recognition) |
793
+ | `AIModelGateway` | Multi-provider gateway with failover & backoff |
794
+ | `AgentExecutor` | ReAct autonomous agent loop with tool execution |
795
+ | `CachedGateway` | TTL-based response caching wrapper |
796
+
797
+ ### Agent Classes
798
+
799
+ | Class | Description |
800
+ |---|---|
801
+ | `ChatAgent` | Simple conversational agent |
802
+ | `ToolAgent` | Agent with tool-calling capabilities |
803
+ | `AgentPipeline` | Sequential multi-agent chaining |
804
+ | `AgentRouter` | Intent-based agent routing |
805
+
806
+ ### Voice Classes
807
+
808
+ | Class | Description |
809
+ |---|---|
810
+ | `OpenAIWhisperSTT` | Whisper-based speech-to-text |
811
+ | `OpenAITTS` | OpenAI text-to-speech (6 voices) |
812
+ | `VoiceprintStore` | Speaker enrollment & cosine similarity matching |
813
+ | `BaseSTTProvider` | Abstract STT provider (extend for custom) |
814
+ | `BaseTTSProvider` | Abstract TTS provider (extend for custom) |
815
+ | `BaseSpeakerRecognizer` | Abstract speaker embedding extractor |
816
+
817
+ ### Error Classes
818
+
819
+ | Exception | When It Fires |
820
+ |---|---|
821
+ | `GatewayRoutingError` | All providers exhausted |
822
+ | `ConfigurationError` | Missing API keys or invalid setup |
823
+ | `ValidationError` | Invalid inputs to any public API |
824
+ | `ToolExecutionError` | Tool fails during agent loop |
825
+ | `AgentIterationLimitError` | Agent exceeds max iterations |
826
+ | `StructuredOutputError` | LLM output fails JSON parsing |
827
+ | `PromptVersionError` | Invalid semver or duplicate prompt version |
828
+
829
+ ### Utilities
830
+
831
+ | Class/Function | Description |
832
+ |---|---|
833
+ | `createTool` | Zod-to-MCP tool factory |
834
+ | `PromptTemplate` | Versioned prompt with mustache rendering |
835
+ | `PromptRegistry` | Prompt version management with A/B rollback |
836
+ | `InMemoryStore` | Session-based conversation memory |
837
+ | `HonchoMemoryStore` | Production-grade Honcho-powered semantic memory store |
838
+ | `SemanticMemorySearch` | Unified search across Honcho reasoning + local vectors |
839
+ | `AgentTelemetry` | Lifecycle hooks interface for APM integration |
840
+
841
+ ## License
842
+ MIT © EchoMind Team