echo-ai-sdk-ts 2.2.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/README.md ADDED
@@ -0,0 +1,589 @@
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
+
118
+ ## Installation
119
+
120
+ ```sh
121
+ npm install echo-ai-sdk zod
122
+ ```
123
+
124
+ You'll need the respective provider SDKs if you wish to utilize them:
125
+
126
+ ```sh
127
+ npm install openai @anthropic-ai/sdk
128
+ ```
129
+
130
+ ## Quick Start (The Facade)
131
+
132
+ 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.
133
+
134
+ ```typescript
135
+ import { EchoAI, calculatorTool } from "echo-ai-sdk";
136
+
137
+ // Assumes process.env.OPENAI_API_KEY is populated.
138
+ const ai = new EchoAI();
139
+
140
+ const agent = ai.createToolAgent([calculatorTool]);
141
+
142
+ async function run() {
143
+ const response = await agent.run("session_xyz", "What is 750 multiplied by 14?");
144
+ console.log(response); // AI answers via calculating transparently!
145
+ }
146
+ run();
147
+ ```
148
+
149
+ ## Advanced Usage
150
+
151
+ ### Building Your Own Type-Safe Tools
152
+
153
+ Using standard Zod schemas, we expose `createTool` to automatically synthesize MCP-compliant JSON schemas out-of-the-box.
154
+
155
+ ```typescript
156
+ import { z } from "zod";
157
+ import { createTool } from "echo-ai-sdk";
158
+
159
+ export const databaseTool = createTool({
160
+ name: "query_db",
161
+ description: "Execute read-only queries against the replica DB.",
162
+ schema: z.object({
163
+ sql: z.string().describe("The SQL Query syntax.")
164
+ }),
165
+ execute: async ({ sql }) => {
166
+ // Implement your database logic
167
+ return await db.query(sql);
168
+ }
169
+ });
170
+ ```
171
+
172
+ ### Direct Gateway Access (With Retry Backoff!)
173
+
174
+ 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!
175
+
176
+ ```typescript
177
+ import { AIModelGateway, OpenAIProvider, ChatRequest } from "echo-ai-sdk";
178
+
179
+ const gateway = new AIModelGateway([
180
+ new OpenAIProvider(process.env.OPENAI_API_KEY),
181
+ // Add AnthropicProvider to handle OpenAI 500 API errors sequentially
182
+ ]);
183
+
184
+ const req: ChatRequest = {
185
+ messages: [{ role: "user", content: "Tell me a story." }],
186
+ model_family: "fast"
187
+ };
188
+
189
+ const stream = gateway.chatStream(req);
190
+
191
+ for await(const token of stream) {
192
+ process.stdout.write(token);
193
+ }
194
+ ```
195
+
196
+ ### Structured Outputs (Enforcing Zod Schemas)
197
+ Need the AI to respond in a strict format?
198
+ ```typescript
199
+ import { z } from 'zod';
200
+
201
+ const myCustomSchema = z.object({
202
+ analysis: z.string(),
203
+ riskScore: z.number().min(1).max(10)
204
+ });
205
+
206
+ // Forces the agent to output the exact JSON object you requested!
207
+ const result = await agent.executeStructured("session123", "Analyze these financial docs...", myCustomSchema);
208
+ console.log(result.riskScore); // 100% Type-Safe Numbers!
209
+ ```
210
+
211
+ ### Telemetry & Tracking
212
+ Expose hooks to pipe your LLM metrics safely straight to platforms like Datadog, Grafana, or LangSmith.
213
+
214
+ ```typescript
215
+ import { AgentExecutor } from "echo-ai-sdk";
216
+
217
+ const executor = new AgentExecutor({
218
+ gateway,
219
+ memory,
220
+ tools,
221
+ telemetry: {
222
+ onTokenUsage: (sessionId, provider, model, usage) => console.log(`Burned ${usage.total_tokens} tokens on ${model}!`),
223
+ onToolStart: (sessionId, toolName, args) => console.log(`Agent executing ${toolName}!`)
224
+ }
225
+ });
226
+ ```
227
+
228
+ ## Error Handling
229
+
230
+ Echo AI SDK embraces explicit semantic exceptions overriding generic javascript errors.
231
+
232
+ ```typescript
233
+ import { GatewayRoutingError } from "echo-ai-sdk";
234
+
235
+ try {
236
+ await gateway.chatComplete(req);
237
+ } catch (e) {
238
+ if (e instanceof GatewayRoutingError) {
239
+ console.error("All providers went down. Terminal panic.");
240
+ }
241
+ }
242
+ ```
243
+
244
+ ### Prompt Templates & Registry
245
+
246
+ Version your prompts like code. Supports immutable registrations, semantic versioning, and instant A/B rollbacks.
247
+
248
+ ```typescript
249
+ import { PromptTemplate, PromptRegistry } from "echo-ai-sdk";
250
+
251
+ const registry = new PromptRegistry();
252
+
253
+ registry.register(new PromptTemplate({
254
+ name: "greet",
255
+ version: "1.0.0",
256
+ template: "Hello {{name}}, welcome to {{product}}!",
257
+ requiredVars: ["name", "product"]
258
+ }));
259
+
260
+ const prompt = registry.getTemplate("greet");
261
+ console.log(prompt.render({ name: "Alex", product: "EchoMind" }));
262
+ // → "Hello Alex, welcome to EchoMind!"
263
+ ```
264
+
265
+ ### Gateway Middleware
266
+
267
+ Intercept, transform, or log every request and response flowing through the gateway.
268
+
269
+ ```typescript
270
+ import { AIModelGateway } from "echo-ai-sdk";
271
+
272
+ const gateway = new AIModelGateway([...providers]);
273
+
274
+ gateway.use({
275
+ onRequest: (req) => {
276
+ console.log(`[Audit] Sending ${req.messages.length} messages`);
277
+ return req;
278
+ },
279
+ onResponse: (res, req) => {
280
+ console.log(`[Audit] Used ${res.usage?.total_tokens} tokens`);
281
+ return res;
282
+ },
283
+ onError: (err, provider) => {
284
+ console.error(`[Alert] ${provider} failed: ${err.message}`);
285
+ }
286
+ });
287
+ ```
288
+
289
+ ### Multi-Agent Orchestration
290
+
291
+ #### AgentPipeline — Sequential Chaining
292
+ Chain agents where each stage's output feeds into the next:
293
+
294
+ ```typescript
295
+ import { AgentPipeline } from "echo-ai-sdk";
296
+
297
+ const pipeline = new AgentPipeline()
298
+ .addStage("summarize", summaryAgent)
299
+ .addStage("translate", translatorAgent)
300
+ .addStage("format", formatterAgent);
301
+
302
+ const result = await pipeline.run("session1", "Raw meeting transcript...");
303
+ ```
304
+
305
+ #### AgentRouter — Intent-Based Dispatch
306
+ Route user input to specialized agents based on regex patterns:
307
+
308
+ ```typescript
309
+ import { AgentRouter } from "echo-ai-sdk";
310
+
311
+ const router = new AgentRouter()
312
+ .addRoute("support", /help|issue|bug/i, supportAgent)
313
+ .addRoute("sales", /pricing|demo|buy/i, salesAgent)
314
+ .setFallback(generalAgent);
315
+
316
+ const answer = await router.route("session1", "I need help with billing");
317
+ ```
318
+
319
+ ### Response Caching
320
+
321
+ Eliminate redundant API calls with TTL-based caching:
322
+
323
+ ```typescript
324
+ import { CachedGateway } from "echo-ai-sdk";
325
+
326
+ const cached = new CachedGateway(gateway, 120_000); // 2-minute TTL
327
+ const response = await cached.chatComplete(req); // Cache MISS → calls API
328
+ const again = await cached.chatComplete(req); // Cache HIT → instant!
329
+ ```
330
+
331
+ ## 🎤 Voice Features (v1.2.0)
332
+
333
+ Echo AI SDK provides plug-and-play **Speech-to-Text**, **Text-to-Speech**, and **Speaker Recognition** — all through a single unified client.
334
+
335
+ ### Quick Start — EchoVoice
336
+
337
+ ```typescript
338
+ import { EchoVoice } from "echo-ai-sdk";
339
+ import fs from "fs";
340
+
341
+ // Auto-detects OPENAI_API_KEY for Whisper STT & TTS
342
+ const voice = new EchoVoice();
343
+ ```
344
+
345
+ ---
346
+
347
+ ### Speech-to-Text (STT)
348
+
349
+ Convert audio files to text using OpenAI Whisper:
350
+
351
+ ```typescript
352
+ const audioBuffer = fs.readFileSync("meeting.wav");
353
+
354
+ // Basic transcription
355
+ const result = await voice.transcribe(audioBuffer);
356
+ console.log(result.text); // → "Welcome to today's meeting..."
357
+
358
+ // With language hint and timestamps
359
+ const detailed = await voice.transcribe(audioBuffer, {
360
+ language: "en",
361
+ timestamps: true,
362
+ temperature: 0.0
363
+ });
364
+
365
+ for (const segment of detailed.segments!) {
366
+ console.log(`[${segment.start}s → ${segment.end}s] ${segment.text}`);
367
+ }
368
+ ```
369
+
370
+ #### STT Options Reference
371
+
372
+ | Option | Type | Description |
373
+ |---|---|---|
374
+ | `language` | `string` | ISO language code (e.g., `"en"`, `"es"`, `"ja"`) |
375
+ | `prompt` | `string` | Guide the model's style or vocabulary |
376
+ | `temperature` | `number` | 0.0 (deterministic) to 1.0 (creative) |
377
+ | `timestamps` | `boolean` | Enable word-level timing segments |
378
+
379
+ #### TranscriptionResult Type
380
+
381
+ ```typescript
382
+ interface TranscriptionResult {
383
+ text: string; // Full transcribed text
384
+ language?: string; // Detected language
385
+ duration?: number; // Audio duration in seconds
386
+ segments?: { // Time-aligned segments
387
+ start: number;
388
+ end: number;
389
+ text: string;
390
+ confidence?: number;
391
+ }[];
392
+ }
393
+ ```
394
+
395
+ ---
396
+
397
+ ### Text-to-Speech (TTS)
398
+
399
+ Convert text to lifelike audio:
400
+
401
+ ```typescript
402
+ // Basic synthesis
403
+ const speech = await voice.speak("Hello, welcome to EchoMind!");
404
+ fs.writeFileSync("greeting.mp3", speech.audio);
405
+
406
+ // With voice selection and options
407
+ const custom = await voice.speak("Breaking news: AI is everywhere.", {
408
+ voice: "nova", // alloy | echo | fable | onyx | nova | shimmer
409
+ speed: 1.2, // 0.25x to 4.0x
410
+ format: "opus", // mp3 | opus | aac | flac | wav | pcm
411
+ model: "tts-1-hd" // tts-1 (fast) or tts-1-hd (quality)
412
+ });
413
+ fs.writeFileSync("news.opus", custom.audio);
414
+ ```
415
+
416
+ #### TTS Options Reference
417
+
418
+ | Option | Type | Default | Description |
419
+ |---|---|---|---|
420
+ | `voice` | `TTSVoice` | `"alloy"` | Voice persona |
421
+ | `speed` | `number` | `1.0` | Playback speed (0.25–4.0) |
422
+ | `format` | `TTSFormat` | `"mp3"` | Output audio format |
423
+ | `model` | `string` | `"tts-1"` | `tts-1` (fast) or `tts-1-hd` (quality) |
424
+
425
+ #### Available Voices
426
+
427
+ | Voice | Tone |
428
+ |---|---|
429
+ | `alloy` | Balanced, neutral |
430
+ | `echo` | Warm, conversational |
431
+ | `fable` | British, narrative |
432
+ | `onyx` | Deep, authoritative |
433
+ | `nova` | Energetic, young |
434
+ | `shimmer` | Soft, gentle |
435
+
436
+ ---
437
+
438
+ ### Speaker Recognition
439
+
440
+ Enroll speakers, then identify or verify them from new audio samples. Powered by cosine similarity over voice embeddings.
441
+
442
+ #### Direct VoiceprintStore Usage (No API Required)
443
+
444
+ ```typescript
445
+ import { VoiceprintStore } from "echo-ai-sdk";
446
+
447
+ const store = new VoiceprintStore(0.80); // 80% match threshold
448
+
449
+ // Enroll speakers with pre-computed embeddings
450
+ store.enroll("spk_001", "Alice", [0.12, 0.84, 0.33, ...]);
451
+ store.enroll("spk_002", "Bob", [0.91, 0.15, 0.67, ...]);
452
+
453
+ // Identify an unknown speaker
454
+ const match = store.identify(unknownEmbedding);
455
+ if (match.matched) {
456
+ console.log(`Identified: ${match.speakerName} (${(match.confidence * 100).toFixed(1)}%)`);
457
+ } else {
458
+ console.log("Unknown speaker");
459
+ }
460
+
461
+ // Verify a specific speaker
462
+ const verification = store.verify("spk_001", audioEmbedding);
463
+ console.log(`Verified: ${verification.verified}, Confidence: ${verification.confidence}`);
464
+
465
+ // Manage speakers
466
+ store.listSpeakers(); // → [SpeakerProfile, ...]
467
+ store.removeSpeaker("spk_001"); // Remove enrollment
468
+ ```
469
+
470
+ #### With EchoVoice Facade (Automatic Embedding Extraction)
471
+
472
+ ```typescript
473
+ import { EchoVoice, BaseSpeakerRecognizer } from "echo-ai-sdk";
474
+
475
+ // Implement your own embedding extractor
476
+ class MySpeakerModel extends BaseSpeakerRecognizer {
477
+ get providerName() { return "my-model"; }
478
+ async extractEmbedding(audio: Buffer): Promise<number[]> {
479
+ // Call your ML model, Azure Speaker API, or custom service
480
+ return [0.12, 0.84, 0.33, /* ... */];
481
+ }
482
+ }
483
+
484
+ const voice = new EchoVoice({ recognizer: new MySpeakerModel() });
485
+
486
+ // Enroll from raw audio
487
+ await voice.enrollSpeaker("spk_001", "Alice", aliceAudioBuffer);
488
+
489
+ // Identify from new audio
490
+ const who = await voice.identifySpeaker(unknownAudioBuffer);
491
+ console.log(who.speakerName); // → "Alice"
492
+
493
+ // Verify a specific person
494
+ const check = await voice.verifySpeaker("spk_001", testAudioBuffer);
495
+ console.log(check.verified); // → true
496
+ ```
497
+
498
+ ---
499
+
500
+ ### Bring Your Own Provider
501
+
502
+ All voice components are fully pluggable. Implement the abstract base classes to use any provider:
503
+
504
+ ```typescript
505
+ import { BaseSTTProvider, BaseTTSProvider, EchoVoice } from "echo-ai-sdk";
506
+
507
+ // Custom Deepgram STT
508
+ class DeepgramSTT extends BaseSTTProvider {
509
+ get providerName() { return "deepgram"; }
510
+ async transcribe(audio: Buffer, options?) {
511
+ // Your Deepgram integration
512
+ return { text: "transcribed text" };
513
+ }
514
+ }
515
+
516
+ // Custom ElevenLabs TTS
517
+ class ElevenLabsTTS extends BaseTTSProvider {
518
+ get providerName() { return "elevenlabs"; }
519
+ async synthesize(text: string, options?) {
520
+ // Your ElevenLabs integration
521
+ return { audio: Buffer.from([]), format: "mp3" as const };
522
+ }
523
+ }
524
+
525
+ // Plug them in
526
+ const voice = new EchoVoice({
527
+ stt: new DeepgramSTT(),
528
+ tts: new ElevenLabsTTS()
529
+ });
530
+ ```
531
+
532
+ ---
533
+
534
+ ## Complete API Reference
535
+
536
+ ### Core Classes
537
+
538
+ | Class | Description |
539
+ |---|---|
540
+ | `EchoAI` | Main client facade for LLM chat & agents |
541
+ | `EchoVoice` | Unified voice client (STT + TTS + Speaker Recognition) |
542
+ | `AIModelGateway` | Multi-provider gateway with failover & backoff |
543
+ | `AgentExecutor` | ReAct autonomous agent loop with tool execution |
544
+ | `CachedGateway` | TTL-based response caching wrapper |
545
+
546
+ ### Agent Classes
547
+
548
+ | Class | Description |
549
+ |---|---|
550
+ | `ChatAgent` | Simple conversational agent |
551
+ | `ToolAgent` | Agent with tool-calling capabilities |
552
+ | `AgentPipeline` | Sequential multi-agent chaining |
553
+ | `AgentRouter` | Intent-based agent routing |
554
+
555
+ ### Voice Classes
556
+
557
+ | Class | Description |
558
+ |---|---|
559
+ | `OpenAIWhisperSTT` | Whisper-based speech-to-text |
560
+ | `OpenAITTS` | OpenAI text-to-speech (6 voices) |
561
+ | `VoiceprintStore` | Speaker enrollment & cosine similarity matching |
562
+ | `BaseSTTProvider` | Abstract STT provider (extend for custom) |
563
+ | `BaseTTSProvider` | Abstract TTS provider (extend for custom) |
564
+ | `BaseSpeakerRecognizer` | Abstract speaker embedding extractor |
565
+
566
+ ### Error Classes
567
+
568
+ | Exception | When It Fires |
569
+ |---|---|
570
+ | `GatewayRoutingError` | All providers exhausted |
571
+ | `ConfigurationError` | Missing API keys or invalid setup |
572
+ | `ValidationError` | Invalid inputs to any public API |
573
+ | `ToolExecutionError` | Tool fails during agent loop |
574
+ | `AgentIterationLimitError` | Agent exceeds max iterations |
575
+ | `StructuredOutputError` | LLM output fails JSON parsing |
576
+ | `PromptVersionError` | Invalid semver or duplicate prompt version |
577
+
578
+ ### Utilities
579
+
580
+ | Class/Function | Description |
581
+ |---|---|
582
+ | `createTool` | Zod-to-MCP tool factory |
583
+ | `PromptTemplate` | Versioned prompt with mustache rendering |
584
+ | `PromptRegistry` | Prompt version management with A/B rollback |
585
+ | `InMemoryStore` | Session-based conversation memory |
586
+ | `AgentTelemetry` | Lifecycle hooks interface for APM integration |
587
+
588
+ ## License
589
+ MIT © EchoMind Team