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 +589 -0
- package/dist/chunk-NZO6ZQRY.mjs +2092 -0
- package/dist/fileFromPath-4L47H5QG.mjs +127 -0
- package/dist/index.d.mts +806 -0
- package/dist/index.d.ts +806 -0
- package/dist/index.js +16179 -0
- package/dist/index.mjs +13879 -0
- package/package.json +31 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,806 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
declare const ChatMessageSchema: z.ZodObject<{
|
|
4
|
+
role: z.ZodEnum<["user", "assistant", "system", "tool"]>;
|
|
5
|
+
content: z.ZodString;
|
|
6
|
+
tool_calls: z.ZodOptional<z.ZodArray<z.ZodRecord<z.ZodString, z.ZodAny>, "many">>;
|
|
7
|
+
tool_call_id: z.ZodOptional<z.ZodString>;
|
|
8
|
+
}, "strip", z.ZodTypeAny, {
|
|
9
|
+
role: "user" | "assistant" | "system" | "tool";
|
|
10
|
+
content: string;
|
|
11
|
+
tool_calls?: Record<string, any>[] | undefined;
|
|
12
|
+
tool_call_id?: string | undefined;
|
|
13
|
+
}, {
|
|
14
|
+
role: "user" | "assistant" | "system" | "tool";
|
|
15
|
+
content: string;
|
|
16
|
+
tool_calls?: Record<string, any>[] | undefined;
|
|
17
|
+
tool_call_id?: string | undefined;
|
|
18
|
+
}>;
|
|
19
|
+
type ChatMessage = z.infer<typeof ChatMessageSchema>;
|
|
20
|
+
declare const ChatRequestSchema: z.ZodObject<{
|
|
21
|
+
messages: z.ZodArray<z.ZodObject<{
|
|
22
|
+
role: z.ZodEnum<["user", "assistant", "system", "tool"]>;
|
|
23
|
+
content: z.ZodString;
|
|
24
|
+
tool_calls: z.ZodOptional<z.ZodArray<z.ZodRecord<z.ZodString, z.ZodAny>, "many">>;
|
|
25
|
+
tool_call_id: z.ZodOptional<z.ZodString>;
|
|
26
|
+
}, "strip", z.ZodTypeAny, {
|
|
27
|
+
role: "user" | "assistant" | "system" | "tool";
|
|
28
|
+
content: string;
|
|
29
|
+
tool_calls?: Record<string, any>[] | undefined;
|
|
30
|
+
tool_call_id?: string | undefined;
|
|
31
|
+
}, {
|
|
32
|
+
role: "user" | "assistant" | "system" | "tool";
|
|
33
|
+
content: string;
|
|
34
|
+
tool_calls?: Record<string, any>[] | undefined;
|
|
35
|
+
tool_call_id?: string | undefined;
|
|
36
|
+
}>, "many">;
|
|
37
|
+
tools: z.ZodOptional<z.ZodArray<z.ZodRecord<z.ZodString, z.ZodAny>, "many">>;
|
|
38
|
+
model_family: z.ZodDefault<z.ZodEnum<["fast", "smart", "capable"]>>;
|
|
39
|
+
temperature: z.ZodDefault<z.ZodNumber>;
|
|
40
|
+
max_tokens: z.ZodOptional<z.ZodNumber>;
|
|
41
|
+
stream: z.ZodDefault<z.ZodBoolean>;
|
|
42
|
+
}, "strip", z.ZodTypeAny, {
|
|
43
|
+
messages: {
|
|
44
|
+
role: "user" | "assistant" | "system" | "tool";
|
|
45
|
+
content: string;
|
|
46
|
+
tool_calls?: Record<string, any>[] | undefined;
|
|
47
|
+
tool_call_id?: string | undefined;
|
|
48
|
+
}[];
|
|
49
|
+
model_family: "fast" | "smart" | "capable";
|
|
50
|
+
temperature: number;
|
|
51
|
+
stream: boolean;
|
|
52
|
+
tools?: Record<string, any>[] | undefined;
|
|
53
|
+
max_tokens?: number | undefined;
|
|
54
|
+
}, {
|
|
55
|
+
messages: {
|
|
56
|
+
role: "user" | "assistant" | "system" | "tool";
|
|
57
|
+
content: string;
|
|
58
|
+
tool_calls?: Record<string, any>[] | undefined;
|
|
59
|
+
tool_call_id?: string | undefined;
|
|
60
|
+
}[];
|
|
61
|
+
tools?: Record<string, any>[] | undefined;
|
|
62
|
+
model_family?: "fast" | "smart" | "capable" | undefined;
|
|
63
|
+
temperature?: number | undefined;
|
|
64
|
+
max_tokens?: number | undefined;
|
|
65
|
+
stream?: boolean | undefined;
|
|
66
|
+
}>;
|
|
67
|
+
type ChatRequest = z.infer<typeof ChatRequestSchema>;
|
|
68
|
+
declare const UsageMetricsSchema: z.ZodObject<{
|
|
69
|
+
prompt_tokens: z.ZodDefault<z.ZodNumber>;
|
|
70
|
+
completion_tokens: z.ZodDefault<z.ZodNumber>;
|
|
71
|
+
total_tokens: z.ZodDefault<z.ZodNumber>;
|
|
72
|
+
}, "strip", z.ZodTypeAny, {
|
|
73
|
+
prompt_tokens: number;
|
|
74
|
+
completion_tokens: number;
|
|
75
|
+
total_tokens: number;
|
|
76
|
+
}, {
|
|
77
|
+
prompt_tokens?: number | undefined;
|
|
78
|
+
completion_tokens?: number | undefined;
|
|
79
|
+
total_tokens?: number | undefined;
|
|
80
|
+
}>;
|
|
81
|
+
type UsageMetrics = z.infer<typeof UsageMetricsSchema>;
|
|
82
|
+
declare const ChatResponseSchema: z.ZodObject<{
|
|
83
|
+
content: z.ZodNullable<z.ZodString>;
|
|
84
|
+
tool_calls: z.ZodOptional<z.ZodNullable<z.ZodArray<z.ZodRecord<z.ZodString, z.ZodAny>, "many">>>;
|
|
85
|
+
usage: z.ZodObject<{
|
|
86
|
+
prompt_tokens: z.ZodDefault<z.ZodNumber>;
|
|
87
|
+
completion_tokens: z.ZodDefault<z.ZodNumber>;
|
|
88
|
+
total_tokens: z.ZodDefault<z.ZodNumber>;
|
|
89
|
+
}, "strip", z.ZodTypeAny, {
|
|
90
|
+
prompt_tokens: number;
|
|
91
|
+
completion_tokens: number;
|
|
92
|
+
total_tokens: number;
|
|
93
|
+
}, {
|
|
94
|
+
prompt_tokens?: number | undefined;
|
|
95
|
+
completion_tokens?: number | undefined;
|
|
96
|
+
total_tokens?: number | undefined;
|
|
97
|
+
}>;
|
|
98
|
+
provider_name: z.ZodString;
|
|
99
|
+
model_name: z.ZodString;
|
|
100
|
+
}, "strip", z.ZodTypeAny, {
|
|
101
|
+
content: string | null;
|
|
102
|
+
usage: {
|
|
103
|
+
prompt_tokens: number;
|
|
104
|
+
completion_tokens: number;
|
|
105
|
+
total_tokens: number;
|
|
106
|
+
};
|
|
107
|
+
provider_name: string;
|
|
108
|
+
model_name: string;
|
|
109
|
+
tool_calls?: Record<string, any>[] | null | undefined;
|
|
110
|
+
}, {
|
|
111
|
+
content: string | null;
|
|
112
|
+
usage: {
|
|
113
|
+
prompt_tokens?: number | undefined;
|
|
114
|
+
completion_tokens?: number | undefined;
|
|
115
|
+
total_tokens?: number | undefined;
|
|
116
|
+
};
|
|
117
|
+
provider_name: string;
|
|
118
|
+
model_name: string;
|
|
119
|
+
tool_calls?: Record<string, any>[] | null | undefined;
|
|
120
|
+
}>;
|
|
121
|
+
type ChatResponse = z.infer<typeof ChatResponseSchema>;
|
|
122
|
+
|
|
123
|
+
declare abstract class BaseProvider {
|
|
124
|
+
/** The unique identifier of the provider (e.g., 'openai', 'anthropic'). */
|
|
125
|
+
abstract get providerName(): string;
|
|
126
|
+
/**
|
|
127
|
+
* Translates a universal ChatRequest into the provider-specific payload,
|
|
128
|
+
* executes the request, and formats it back to a standard ChatResponse.
|
|
129
|
+
*/
|
|
130
|
+
abstract chatComplete(request: ChatRequest): Promise<ChatResponse>;
|
|
131
|
+
/**
|
|
132
|
+
* Same as chatComplete but yields text chunks asynchronously.
|
|
133
|
+
*/
|
|
134
|
+
abstract chatStream(request: ChatRequest): AsyncGenerator<string, void, unknown>;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Middleware interface for the AI Model Gateway.
|
|
139
|
+
* Intercept, transform, or log requests/responses flowing through the gateway.
|
|
140
|
+
*/
|
|
141
|
+
interface GatewayMiddleware {
|
|
142
|
+
/** Called before a request is sent to any provider. Can modify the request. */
|
|
143
|
+
onRequest?: (request: ChatRequest) => ChatRequest | Promise<ChatRequest>;
|
|
144
|
+
/** Called after a successful response is received. Can modify the response. */
|
|
145
|
+
onResponse?: (response: ChatResponse, request: ChatRequest) => ChatResponse | Promise<ChatResponse>;
|
|
146
|
+
/** Called when a provider throws an error. */
|
|
147
|
+
onError?: (error: Error, providerName: string) => void;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Applies a middleware pipeline to requests and responses.
|
|
151
|
+
*/
|
|
152
|
+
declare function applyRequestMiddleware(middlewares: GatewayMiddleware[], request: ChatRequest): Promise<ChatRequest>;
|
|
153
|
+
declare function applyResponseMiddleware(middlewares: GatewayMiddleware[], response: ChatResponse, request: ChatRequest): Promise<ChatResponse>;
|
|
154
|
+
|
|
155
|
+
declare class AIModelGateway {
|
|
156
|
+
providers: BaseProvider[];
|
|
157
|
+
private middlewares;
|
|
158
|
+
constructor(providers: BaseProvider[]);
|
|
159
|
+
/**
|
|
160
|
+
* Register middleware to intercept, transform, or log requests/responses.
|
|
161
|
+
* Middleware is executed in registration order.
|
|
162
|
+
*/
|
|
163
|
+
use(middleware: GatewayMiddleware): this;
|
|
164
|
+
chatComplete(request: ChatRequest): Promise<ChatResponse>;
|
|
165
|
+
chatStream(request: ChatRequest): AsyncGenerator<string, void, unknown>;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
interface BaseMemoryStore {
|
|
169
|
+
getMessages(sessionId: string): Promise<ChatMessage[]>;
|
|
170
|
+
addMessage(sessionId: string, message: ChatMessage): Promise<void>;
|
|
171
|
+
clearSession(sessionId: string): Promise<void>;
|
|
172
|
+
}
|
|
173
|
+
declare class InMemoryStore implements BaseMemoryStore {
|
|
174
|
+
maxHistory: number;
|
|
175
|
+
private memory;
|
|
176
|
+
constructor(maxHistory?: number);
|
|
177
|
+
getMessages(sessionId: string): Promise<ChatMessage[]>;
|
|
178
|
+
addMessage(sessionId: string, message: ChatMessage): Promise<void>;
|
|
179
|
+
clearSession(sessionId: string): Promise<void>;
|
|
180
|
+
/** Returns the number of active sessions in memory. */
|
|
181
|
+
get sessionCount(): number;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
interface ToolContext<T = any> {
|
|
185
|
+
name: string;
|
|
186
|
+
description: string;
|
|
187
|
+
schema: z.ZodType<T>;
|
|
188
|
+
execute: (args: T) => Promise<string | any>;
|
|
189
|
+
getMcpSchema: () => any;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Factory function to create strongly-typed tools utilizing Zod.
|
|
193
|
+
* Automatically handles conversion to JSONSchema.
|
|
194
|
+
*/
|
|
195
|
+
declare function createTool<T>(options: {
|
|
196
|
+
name: string;
|
|
197
|
+
description: string;
|
|
198
|
+
schema: z.ZodType<T>;
|
|
199
|
+
execute: (args: T) => Promise<string | any>;
|
|
200
|
+
}): ToolContext<T>;
|
|
201
|
+
|
|
202
|
+
declare class ChatAgent {
|
|
203
|
+
private _executor;
|
|
204
|
+
constructor(gateway: AIModelGateway, memory?: BaseMemoryStore);
|
|
205
|
+
chat(sessionId: string, userInput: string): Promise<string>;
|
|
206
|
+
}
|
|
207
|
+
declare class ToolAgent {
|
|
208
|
+
private _executor;
|
|
209
|
+
constructor(gateway: AIModelGateway, tools: ToolContext[], memory?: BaseMemoryStore);
|
|
210
|
+
run(sessionId: string, userInput: string, maxIterations?: number): Promise<string>;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
declare class EchoAI {
|
|
214
|
+
gateway: AIModelGateway;
|
|
215
|
+
constructor(options?: {
|
|
216
|
+
providers?: BaseProvider[];
|
|
217
|
+
});
|
|
218
|
+
createChatAgent(): ChatAgent;
|
|
219
|
+
createToolAgent(tools: ToolContext[]): ToolAgent;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
interface AgentTelemetry {
|
|
223
|
+
onAgentStart?: (sessionId: string, input: string) => void;
|
|
224
|
+
onAgentIteration?: (sessionId: string, iteration: number) => void;
|
|
225
|
+
onToolStart?: (sessionId: string, toolName: string, args: any) => void;
|
|
226
|
+
onToolEnd?: (sessionId: string, toolName: string, result: string) => void;
|
|
227
|
+
onTokenUsage?: (sessionId: string, provider: string, model: string, usage: {
|
|
228
|
+
prompt_tokens: number;
|
|
229
|
+
completion_tokens: number;
|
|
230
|
+
total_tokens: number;
|
|
231
|
+
}) => void;
|
|
232
|
+
onAgentEnd?: (sessionId: string, output: string) => void;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
declare class AgentExecutor {
|
|
236
|
+
private gateway;
|
|
237
|
+
private memory;
|
|
238
|
+
private tools;
|
|
239
|
+
private systemPrompt?;
|
|
240
|
+
private telemetry?;
|
|
241
|
+
constructor(options: {
|
|
242
|
+
gateway: AIModelGateway | BaseProvider;
|
|
243
|
+
memory: BaseMemoryStore;
|
|
244
|
+
tools?: ToolContext[];
|
|
245
|
+
systemPrompt?: string;
|
|
246
|
+
telemetry?: AgentTelemetry;
|
|
247
|
+
});
|
|
248
|
+
execute(sessionId: string, userInput: string, maxIterations?: number): Promise<string>;
|
|
249
|
+
/**
|
|
250
|
+
* Forces the LLM to output strictly structured JSON validated against the provided Zod schema.
|
|
251
|
+
* Throws StructuredOutputError if the output cannot be parsed.
|
|
252
|
+
*/
|
|
253
|
+
executeStructured<T>(sessionId: string, userInput: string, schema: z.ZodType<T>, maxIterations?: number): Promise<T>;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Chains multiple agents sequentially.
|
|
258
|
+
* The output of one agent becomes the input of the next.
|
|
259
|
+
* Perfect for pipelines like: Summarize → Translate → Format.
|
|
260
|
+
*/
|
|
261
|
+
declare class AgentPipeline {
|
|
262
|
+
private agents;
|
|
263
|
+
addStage(label: string, executor: AgentExecutor): this;
|
|
264
|
+
run(sessionId: string, initialInput: string): Promise<string>;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Routes user input to the most appropriate agent based on intent classification.
|
|
268
|
+
* Useful for building multi-skill assistants (e.g., support bot vs. sales bot).
|
|
269
|
+
*/
|
|
270
|
+
declare class AgentRouter {
|
|
271
|
+
private routes;
|
|
272
|
+
private fallback?;
|
|
273
|
+
addRoute(label: string, pattern: RegExp, executor: AgentExecutor): this;
|
|
274
|
+
setFallback(executor: AgentExecutor): this;
|
|
275
|
+
route(sessionId: string, userInput: string): Promise<string>;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
declare const calculatorTool: ToolContext<{
|
|
279
|
+
expression: string;
|
|
280
|
+
}>;
|
|
281
|
+
declare const dateTimeTool: ToolContext<{}>;
|
|
282
|
+
declare const webSearchTool: ToolContext<{
|
|
283
|
+
query: string;
|
|
284
|
+
}>;
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Wraps an AIModelGateway with a TTL-based in-memory response cache.
|
|
288
|
+
* Identical requests within the TTL window are served instantly from cache,
|
|
289
|
+
* eliminating redundant API calls and saving tokens.
|
|
290
|
+
*/
|
|
291
|
+
declare class CachedGateway {
|
|
292
|
+
private gateway;
|
|
293
|
+
private ttlMs;
|
|
294
|
+
private cache;
|
|
295
|
+
constructor(gateway: AIModelGateway, ttlMs?: number);
|
|
296
|
+
private getCacheKey;
|
|
297
|
+
chatComplete(request: ChatRequest): Promise<ChatResponse>;
|
|
298
|
+
/** Streaming is not cacheable, passthrough directly. */
|
|
299
|
+
chatStream(request: ChatRequest): AsyncGenerator<string, void, unknown>;
|
|
300
|
+
clearCache(): void;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
declare class PromptVersionError extends Error {
|
|
304
|
+
constructor(message: string);
|
|
305
|
+
}
|
|
306
|
+
declare class PromptTemplate {
|
|
307
|
+
name: string;
|
|
308
|
+
version: string;
|
|
309
|
+
template: string;
|
|
310
|
+
requiredVars: Set<string>;
|
|
311
|
+
major: number;
|
|
312
|
+
minor: number;
|
|
313
|
+
patch: number;
|
|
314
|
+
constructor(options: {
|
|
315
|
+
name: string;
|
|
316
|
+
version: string;
|
|
317
|
+
template: string;
|
|
318
|
+
requiredVars?: string[];
|
|
319
|
+
});
|
|
320
|
+
/**
|
|
321
|
+
* Injects variables into the template string.
|
|
322
|
+
* Uses `{{variableName}}` mustache-style placeholders.
|
|
323
|
+
*/
|
|
324
|
+
render(variables: Record<string, any>): string;
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Central repository for managing prompt templates and their versions.
|
|
328
|
+
* Allows decoupling prompt engineering from application code releases.
|
|
329
|
+
* Supports instant rollbacks and A/B testing.
|
|
330
|
+
*/
|
|
331
|
+
declare class PromptRegistry {
|
|
332
|
+
private registry;
|
|
333
|
+
private activeVersions;
|
|
334
|
+
register(template: PromptTemplate, isActive?: boolean): void;
|
|
335
|
+
getTemplate(name: string, version?: string): PromptTemplate;
|
|
336
|
+
setActiveVersion(name: string, version: string): void;
|
|
337
|
+
listVersions(name: string): string[];
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Thrown when all providers in the Gateway exhaust their retry attempts.
|
|
342
|
+
*/
|
|
343
|
+
declare class GatewayRoutingError extends Error {
|
|
344
|
+
errors: string[];
|
|
345
|
+
constructor(errors: string[]);
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Thrown when a required peer dependency (e.g., openai or @anthropic-ai/sdk) is not installed.
|
|
349
|
+
*/
|
|
350
|
+
declare class ProviderDependencyError extends Error {
|
|
351
|
+
constructor(providerName: string);
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Thrown when a tool fails during execution within the Agent loop.
|
|
355
|
+
*/
|
|
356
|
+
declare class ToolExecutionError extends Error {
|
|
357
|
+
toolName: string;
|
|
358
|
+
constructor(toolName: string, message: string);
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Thrown when no providers are configured in the EchoAI client.
|
|
362
|
+
*/
|
|
363
|
+
declare class ConfigurationError extends Error {
|
|
364
|
+
constructor(message: string);
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Thrown when input validation fails on any public API method.
|
|
368
|
+
*/
|
|
369
|
+
declare class ValidationError extends Error {
|
|
370
|
+
constructor(method: string, message: string);
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Thrown when the AgentExecutor exceeds its maximum iteration limit.
|
|
374
|
+
*/
|
|
375
|
+
declare class AgentIterationLimitError extends Error {
|
|
376
|
+
iterations: number;
|
|
377
|
+
constructor(iterations: number);
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Thrown when structured output parsing fails.
|
|
381
|
+
*/
|
|
382
|
+
declare class StructuredOutputError extends Error {
|
|
383
|
+
rawOutput: string;
|
|
384
|
+
constructor(rawOutput: string, parseError: string);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
interface TranscriptionResult {
|
|
388
|
+
text: string;
|
|
389
|
+
language?: string;
|
|
390
|
+
duration?: number;
|
|
391
|
+
segments?: TranscriptionSegment[];
|
|
392
|
+
}
|
|
393
|
+
interface TranscriptionSegment {
|
|
394
|
+
start: number;
|
|
395
|
+
end: number;
|
|
396
|
+
text: string;
|
|
397
|
+
confidence?: number;
|
|
398
|
+
}
|
|
399
|
+
interface STTOptions {
|
|
400
|
+
language?: string;
|
|
401
|
+
prompt?: string;
|
|
402
|
+
temperature?: number;
|
|
403
|
+
timestamps?: boolean;
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* Abstract base class for Speech-to-Text providers.
|
|
407
|
+
* Extend this to integrate any STT engine (Whisper, Deepgram, AssemblyAI, etc.)
|
|
408
|
+
*/
|
|
409
|
+
declare abstract class BaseSTTProvider {
|
|
410
|
+
abstract get providerName(): string;
|
|
411
|
+
abstract transcribe(audio: Buffer, options?: STTOptions): Promise<TranscriptionResult>;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
type TTSVoice = "alloy" | "echo" | "fable" | "onyx" | "nova" | "shimmer" | string;
|
|
415
|
+
type TTSFormat = "mp3" | "opus" | "aac" | "flac" | "wav" | "pcm";
|
|
416
|
+
interface TTSOptions {
|
|
417
|
+
voice?: TTSVoice;
|
|
418
|
+
speed?: number;
|
|
419
|
+
format?: TTSFormat;
|
|
420
|
+
model?: string;
|
|
421
|
+
}
|
|
422
|
+
interface TTSResult {
|
|
423
|
+
audio: Buffer;
|
|
424
|
+
format: TTSFormat;
|
|
425
|
+
durationMs?: number;
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Abstract base class for Text-to-Speech providers.
|
|
429
|
+
* Extend this to integrate any TTS engine (OpenAI, ElevenLabs, Google, etc.)
|
|
430
|
+
*/
|
|
431
|
+
declare abstract class BaseTTSProvider {
|
|
432
|
+
abstract get providerName(): string;
|
|
433
|
+
abstract synthesize(text: string, options?: TTSOptions): Promise<TTSResult>;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
interface SpeakerProfile {
|
|
437
|
+
id: string;
|
|
438
|
+
name: string;
|
|
439
|
+
embedding: number[];
|
|
440
|
+
enrolledAt: Date;
|
|
441
|
+
}
|
|
442
|
+
interface IdentificationResult {
|
|
443
|
+
speakerId: string;
|
|
444
|
+
speakerName: string;
|
|
445
|
+
confidence: number;
|
|
446
|
+
matched: boolean;
|
|
447
|
+
}
|
|
448
|
+
interface VerificationResult {
|
|
449
|
+
verified: boolean;
|
|
450
|
+
confidence: number;
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Abstract base class for Speaker Recognition providers.
|
|
454
|
+
* Extend this to integrate speaker verification engines (Azure, custom models, etc.)
|
|
455
|
+
*/
|
|
456
|
+
declare abstract class BaseSpeakerRecognizer {
|
|
457
|
+
abstract get providerName(): string;
|
|
458
|
+
abstract extractEmbedding(audio: Buffer): Promise<number[]>;
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* In-memory voiceprint store for speaker enrollment and identification.
|
|
462
|
+
* Uses cosine similarity for matching voice embeddings.
|
|
463
|
+
*/
|
|
464
|
+
declare class VoiceprintStore {
|
|
465
|
+
private profiles;
|
|
466
|
+
threshold: number;
|
|
467
|
+
constructor(threshold?: number);
|
|
468
|
+
enroll(id: string, name: string, embedding: number[]): SpeakerProfile;
|
|
469
|
+
identify(embedding: number[]): IdentificationResult;
|
|
470
|
+
verify(speakerId: string, embedding: number[]): VerificationResult;
|
|
471
|
+
getProfile(id: string): SpeakerProfile | undefined;
|
|
472
|
+
listSpeakers(): SpeakerProfile[];
|
|
473
|
+
removeSpeaker(id: string): boolean;
|
|
474
|
+
private cosineSimilarity;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
declare class OpenAIWhisperSTT extends BaseSTTProvider {
|
|
478
|
+
private client;
|
|
479
|
+
constructor(apiKey: string);
|
|
480
|
+
get providerName(): string;
|
|
481
|
+
transcribe(audio: Buffer, options?: STTOptions): Promise<TranscriptionResult>;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
declare class OpenAITTS extends BaseTTSProvider {
|
|
485
|
+
private client;
|
|
486
|
+
constructor(apiKey: string);
|
|
487
|
+
get providerName(): string;
|
|
488
|
+
synthesize(text: string, options?: TTSOptions): Promise<TTSResult>;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Unified voice client providing one-liner access to STT, TTS, and Speaker Recognition.
|
|
493
|
+
* Auto-configures OpenAI providers from environment variables.
|
|
494
|
+
*/
|
|
495
|
+
declare class EchoVoice {
|
|
496
|
+
stt: BaseSTTProvider;
|
|
497
|
+
tts: BaseTTSProvider;
|
|
498
|
+
speakers: VoiceprintStore;
|
|
499
|
+
private recognizer?;
|
|
500
|
+
constructor(options?: {
|
|
501
|
+
stt?: BaseSTTProvider;
|
|
502
|
+
tts?: BaseTTSProvider;
|
|
503
|
+
recognizer?: BaseSpeakerRecognizer;
|
|
504
|
+
speakerThreshold?: number;
|
|
505
|
+
});
|
|
506
|
+
/** Transcribe audio to text. */
|
|
507
|
+
transcribe(audio: Buffer, options?: STTOptions): Promise<TranscriptionResult>;
|
|
508
|
+
/** Convert text to audio. Returns a raw Buffer of the audio file. */
|
|
509
|
+
speak(text: string, options?: TTSOptions): Promise<TTSResult>;
|
|
510
|
+
/**
|
|
511
|
+
* Enroll a speaker by providing their audio sample.
|
|
512
|
+
* Requires a BaseSpeakerRecognizer to extract embeddings.
|
|
513
|
+
*/
|
|
514
|
+
enrollSpeaker(id: string, name: string, audio: Buffer): Promise<void>;
|
|
515
|
+
/**
|
|
516
|
+
* Identify who is speaking from an audio sample.
|
|
517
|
+
* Returns the best matching speaker profile.
|
|
518
|
+
*/
|
|
519
|
+
identifySpeaker(audio: Buffer): Promise<IdentificationResult>;
|
|
520
|
+
/**
|
|
521
|
+
* Verify if an audio sample belongs to a specific enrolled speaker.
|
|
522
|
+
*/
|
|
523
|
+
verifySpeaker(speakerId: string, audio: Buffer): Promise<VerificationResult>;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
interface APIConnectorConfig {
|
|
527
|
+
baseUrl: string;
|
|
528
|
+
headers?: Record<string, string>;
|
|
529
|
+
authToken?: string;
|
|
530
|
+
timeout?: number;
|
|
531
|
+
}
|
|
532
|
+
interface FetchResult {
|
|
533
|
+
status: number;
|
|
534
|
+
data: any;
|
|
535
|
+
ok: boolean;
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Connects the chatbot to your website's API to fetch live data.
|
|
539
|
+
* The bot can query your endpoints to answer customer questions with real data.
|
|
540
|
+
*/
|
|
541
|
+
declare class APIConnector {
|
|
542
|
+
private baseUrl;
|
|
543
|
+
private headers;
|
|
544
|
+
private timeout;
|
|
545
|
+
constructor(config: APIConnectorConfig);
|
|
546
|
+
get(endpoint: string): Promise<FetchResult>;
|
|
547
|
+
post(endpoint: string, body: any): Promise<FetchResult>;
|
|
548
|
+
private request;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
interface VectorEntry {
|
|
552
|
+
id: string;
|
|
553
|
+
content: string;
|
|
554
|
+
vector: number[];
|
|
555
|
+
metadata?: Record<string, any>;
|
|
556
|
+
}
|
|
557
|
+
interface SearchResult {
|
|
558
|
+
content: string;
|
|
559
|
+
score: number;
|
|
560
|
+
metadata?: Record<string, any>;
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Enhanced in-memory vector store with persistence and similarity thresholding.
|
|
564
|
+
*/
|
|
565
|
+
declare class MemoryVectorStore {
|
|
566
|
+
private entries;
|
|
567
|
+
/** Add a single vector entry. */
|
|
568
|
+
add(entry: VectorEntry): Promise<void>;
|
|
569
|
+
/** Add multiple vector entries at once. */
|
|
570
|
+
addBatch(entries: VectorEntry[]): Promise<void>;
|
|
571
|
+
/** Retrieve top K results above a minimum similarity score. */
|
|
572
|
+
search(queryVector: number[], k?: number, minScore?: number): Promise<SearchResult[]>;
|
|
573
|
+
/** Save the entire store to a JSON file. */
|
|
574
|
+
saveToFile(path: string): Promise<void>;
|
|
575
|
+
/** Load store entries from a JSON file. */
|
|
576
|
+
loadFromFile(path: string): Promise<void>;
|
|
577
|
+
/** Clear all stored vectors. */
|
|
578
|
+
clear(): void;
|
|
579
|
+
get size(): number;
|
|
580
|
+
}
|
|
581
|
+
interface ChunkOptions {
|
|
582
|
+
chunkSize?: number;
|
|
583
|
+
chunkOverlap?: number;
|
|
584
|
+
}
|
|
585
|
+
declare function chunkText(text: string, options?: ChunkOptions): string[];
|
|
586
|
+
interface KnowledgeBaseConfig {
|
|
587
|
+
openaiApiKey: string;
|
|
588
|
+
embeddingModel?: string;
|
|
589
|
+
chunkSize?: number;
|
|
590
|
+
chunkOverlap?: number;
|
|
591
|
+
topK?: number;
|
|
592
|
+
minScore?: number;
|
|
593
|
+
}
|
|
594
|
+
declare class KnowledgeBase {
|
|
595
|
+
private store;
|
|
596
|
+
private openai;
|
|
597
|
+
private embeddingModel;
|
|
598
|
+
private chunkOpts;
|
|
599
|
+
private topK;
|
|
600
|
+
private minScore;
|
|
601
|
+
private docCounter;
|
|
602
|
+
constructor(config: KnowledgeBaseConfig);
|
|
603
|
+
ingestText(text: string, source?: string): Promise<number>;
|
|
604
|
+
ingestURL(url: string): Promise<number>;
|
|
605
|
+
query(question: string, topK?: number): Promise<string>;
|
|
606
|
+
save(path: string): Promise<void>;
|
|
607
|
+
load(path: string): Promise<void>;
|
|
608
|
+
private embed;
|
|
609
|
+
private embedBatch;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* Enterprise Conversation Analytics Engine.
|
|
614
|
+
* Tracks resolution rates, CSAT, response latency, token costs, and top queries.
|
|
615
|
+
*/
|
|
616
|
+
interface ConversationRecord {
|
|
617
|
+
sessionId: string;
|
|
618
|
+
startTime: number;
|
|
619
|
+
endTime?: number;
|
|
620
|
+
messageCount: number;
|
|
621
|
+
resolved: boolean;
|
|
622
|
+
handedOff: boolean;
|
|
623
|
+
csatScore?: number;
|
|
624
|
+
totalTokens: number;
|
|
625
|
+
totalCostUsd: number;
|
|
626
|
+
topQueries: string[];
|
|
627
|
+
responseTimes: number[];
|
|
628
|
+
model?: string;
|
|
629
|
+
}
|
|
630
|
+
interface AnalyticsSnapshot {
|
|
631
|
+
totalConversations: number;
|
|
632
|
+
resolvedCount: number;
|
|
633
|
+
resolutionRate: number;
|
|
634
|
+
handoffRate: number;
|
|
635
|
+
avgCsatScore: number;
|
|
636
|
+
avgResponseTimeMs: number;
|
|
637
|
+
totalTokensUsed: number;
|
|
638
|
+
totalCostUsd: number;
|
|
639
|
+
avgTokensPerConversation: number;
|
|
640
|
+
avgCostPerConversation: number;
|
|
641
|
+
topQueries: {
|
|
642
|
+
query: string;
|
|
643
|
+
count: number;
|
|
644
|
+
}[];
|
|
645
|
+
conversationsPerHour: number;
|
|
646
|
+
}
|
|
647
|
+
declare const OPENAI_PRICING: Record<string, {
|
|
648
|
+
input: number;
|
|
649
|
+
output: number;
|
|
650
|
+
}>;
|
|
651
|
+
declare class ConversationAnalytics {
|
|
652
|
+
private records;
|
|
653
|
+
private queryFrequency;
|
|
654
|
+
estimateTokens(text: string): number;
|
|
655
|
+
calculateCost(model: string, tokens: number, type?: "input" | "output"): number;
|
|
656
|
+
startConversation(sessionId: string, model?: string): void;
|
|
657
|
+
recordQuery(sessionId: string, query: string): void;
|
|
658
|
+
recordResponse(sessionId: string, reply: string, latencyMs: number): void;
|
|
659
|
+
markResolved(sessionId: string): void;
|
|
660
|
+
markHandedOff(sessionId: string): void;
|
|
661
|
+
recordCSAT(sessionId: string, score: number): void;
|
|
662
|
+
getSnapshot(): AnalyticsSnapshot;
|
|
663
|
+
private emptySnapshot;
|
|
664
|
+
exportJSON(): string;
|
|
665
|
+
importJSON(json: string): void;
|
|
666
|
+
reset(): void;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
/**
|
|
670
|
+
* Advanced Human Handoff Manager.
|
|
671
|
+
* Detects escalation triggers (frustration, sentiment, max turns)
|
|
672
|
+
* and dispatches to external helpdesks via webhooks with HMAC security.
|
|
673
|
+
*/
|
|
674
|
+
type EscalationTrigger = "explicit_request" | "low_confidence" | "frustration" | "sentiment" | "max_turns" | "keyword";
|
|
675
|
+
interface HandoffEvent {
|
|
676
|
+
sessionId: string;
|
|
677
|
+
trigger: EscalationTrigger;
|
|
678
|
+
conversationHistory: {
|
|
679
|
+
role: string;
|
|
680
|
+
content: string;
|
|
681
|
+
}[];
|
|
682
|
+
customerMessage: string;
|
|
683
|
+
summary?: string;
|
|
684
|
+
timestamp: number;
|
|
685
|
+
metadata?: Record<string, any>;
|
|
686
|
+
}
|
|
687
|
+
interface HandoffConfig {
|
|
688
|
+
webhookUrl?: string;
|
|
689
|
+
webhookSecret?: string;
|
|
690
|
+
maxTurnsBeforeHandoff?: number;
|
|
691
|
+
negativeSentimentThreshold?: number;
|
|
692
|
+
frustrationKeywords?: string[];
|
|
693
|
+
explicitKeywords?: string[];
|
|
694
|
+
onHandoff?: (event: HandoffEvent) => void | Promise<void>;
|
|
695
|
+
offlineMessage?: string;
|
|
696
|
+
}
|
|
697
|
+
declare class HandoffManager {
|
|
698
|
+
private webhookUrl?;
|
|
699
|
+
private webhookSecret?;
|
|
700
|
+
private maxTurns;
|
|
701
|
+
private sentimentThreshold;
|
|
702
|
+
private frustrationKeywords;
|
|
703
|
+
private explicitKeywords;
|
|
704
|
+
private onHandoff?;
|
|
705
|
+
offlineMessage: string;
|
|
706
|
+
private turnCounts;
|
|
707
|
+
private negativeStrikes;
|
|
708
|
+
constructor(config: HandoffConfig);
|
|
709
|
+
/** Detect negative sentiment in a message (simple rule-based). */
|
|
710
|
+
private isNegative;
|
|
711
|
+
/** Check if a message should trigger handoff. Returns the trigger type or null. */
|
|
712
|
+
shouldEscalate(sessionId: string, message: string): EscalationTrigger | null;
|
|
713
|
+
/** Execute the handoff: fire webhook, invoke callback, return user message. */
|
|
714
|
+
escalate(sessionId: string, trigger: EscalationTrigger, conversationHistory: {
|
|
715
|
+
role: string;
|
|
716
|
+
content: string;
|
|
717
|
+
}[], customerMessage: string, summary?: string, metadata?: Record<string, any>): Promise<string>;
|
|
718
|
+
private dispatchWebhook;
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
interface SupportBotConfig {
|
|
722
|
+
gateway: AIModelGateway;
|
|
723
|
+
companyName: string;
|
|
724
|
+
companyDescription?: string;
|
|
725
|
+
apiConnector?: APIConnectorConfig;
|
|
726
|
+
knowledgeBase?: KnowledgeBaseConfig;
|
|
727
|
+
handoff?: HandoffConfig;
|
|
728
|
+
memory?: BaseMemoryStore;
|
|
729
|
+
telemetry?: AgentTelemetry;
|
|
730
|
+
customTools?: ToolContext[];
|
|
731
|
+
greeting?: string;
|
|
732
|
+
systemPrompt?: string;
|
|
733
|
+
maxIterations?: number;
|
|
734
|
+
}
|
|
735
|
+
type BotMiddleware = (ctx: {
|
|
736
|
+
sessionId: string;
|
|
737
|
+
message: string;
|
|
738
|
+
bot: CustomerSupportBot;
|
|
739
|
+
}) => Promise<void | string>;
|
|
740
|
+
/**
|
|
741
|
+
* Pre-configured customer support chatbot agent.
|
|
742
|
+
* Connects to your website API, answers questions, uses a RAG knowledge base,
|
|
743
|
+
* tracks analytics, and handles human handoffs.
|
|
744
|
+
*/
|
|
745
|
+
declare class CustomerSupportBot {
|
|
746
|
+
private executor;
|
|
747
|
+
private connector?;
|
|
748
|
+
knowledgeBase?: KnowledgeBase;
|
|
749
|
+
analytics: ConversationAnalytics;
|
|
750
|
+
handoff?: HandoffManager;
|
|
751
|
+
greeting: string;
|
|
752
|
+
private middlewares;
|
|
753
|
+
constructor(config: SupportBotConfig);
|
|
754
|
+
/** Register a middleware function to run before every chat turn. */
|
|
755
|
+
use(middleware: BotMiddleware): void;
|
|
756
|
+
/** Process a customer message and return the bot's response. */
|
|
757
|
+
chat(sessionId: string, message: string): Promise<string>;
|
|
758
|
+
/** Start tracking analytics for a new session. */
|
|
759
|
+
initSession(sessionId: string): void;
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
interface ChatWidgetTheme {
|
|
763
|
+
primaryColor?: string;
|
|
764
|
+
headerBg?: string;
|
|
765
|
+
headerText?: string;
|
|
766
|
+
bubbleBg?: string;
|
|
767
|
+
userBubbleBg?: string;
|
|
768
|
+
fontFamily?: string;
|
|
769
|
+
borderRadius?: string;
|
|
770
|
+
position?: "bottom-right" | "bottom-left";
|
|
771
|
+
width?: string;
|
|
772
|
+
height?: string;
|
|
773
|
+
}
|
|
774
|
+
interface ChatWidgetConfig {
|
|
775
|
+
serverEndpoint: string;
|
|
776
|
+
title?: string;
|
|
777
|
+
subtitle?: string;
|
|
778
|
+
greeting?: string;
|
|
779
|
+
placeholder?: string;
|
|
780
|
+
theme?: ChatWidgetTheme;
|
|
781
|
+
enableVoice?: boolean;
|
|
782
|
+
}
|
|
783
|
+
/**
|
|
784
|
+
* Generates a self-contained, embeddable HTML/CSS/JS chatbot widget.
|
|
785
|
+
* Paste the output into any website — zero dependencies required client-side.
|
|
786
|
+
*/
|
|
787
|
+
declare class ChatWidget {
|
|
788
|
+
static generate(config: ChatWidgetConfig): string;
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
interface ServerConfig extends SupportBotConfig {
|
|
792
|
+
port?: number;
|
|
793
|
+
corsOrigins?: string | string[];
|
|
794
|
+
}
|
|
795
|
+
/**
|
|
796
|
+
* Creates a complete Express-compatible request handler for the chatbot.
|
|
797
|
+
* Handles /chat, /analytics, /handoff, and /knowledge ingestion.
|
|
798
|
+
*/
|
|
799
|
+
declare function createChatHandler(bot: CustomerSupportBot): (req: any, res: any) => Promise<any>;
|
|
800
|
+
/**
|
|
801
|
+
* Quick-start: Spins up a minimal Express server with the chatbot endpoints.
|
|
802
|
+
* Requires `express` and `cors` to be installed in the consumer's project.
|
|
803
|
+
*/
|
|
804
|
+
declare function startChatServer(config: ServerConfig): Promise<void>;
|
|
805
|
+
|
|
806
|
+
export { AIModelGateway, APIConnector, type APIConnectorConfig, AgentExecutor, AgentIterationLimitError, AgentPipeline, AgentRouter, type AgentTelemetry, type AnalyticsSnapshot, type BaseMemoryStore, BaseProvider, BaseSTTProvider, BaseSpeakerRecognizer, BaseTTSProvider, type BotMiddleware, CachedGateway, ChatAgent, type ChatMessage, ChatMessageSchema, type ChatRequest, ChatRequestSchema, type ChatResponse, ChatResponseSchema, ChatWidget, type ChatWidgetConfig, type ChatWidgetTheme, type ChunkOptions, ConfigurationError, ConversationAnalytics, type ConversationRecord, CustomerSupportBot, EchoAI, EchoVoice, type EscalationTrigger, type FetchResult, type GatewayMiddleware, GatewayRoutingError, type HandoffConfig, type HandoffEvent, HandoffManager, type IdentificationResult, InMemoryStore, KnowledgeBase, type KnowledgeBaseConfig, MemoryVectorStore, OPENAI_PRICING, OpenAITTS, OpenAIWhisperSTT, PromptRegistry, PromptTemplate, PromptVersionError, ProviderDependencyError, type STTOptions, type SearchResult, type ServerConfig, type SpeakerProfile, StructuredOutputError, type SupportBotConfig, type TTSFormat, type TTSOptions, type TTSResult, type TTSVoice, ToolAgent, type ToolContext, ToolExecutionError, type TranscriptionResult, type TranscriptionSegment, type UsageMetrics, UsageMetricsSchema, ValidationError, type VectorEntry, type VerificationResult, VoiceprintStore, applyRequestMiddleware, applyResponseMiddleware, calculatorTool, chunkText, createChatHandler, createTool, dateTimeTool, startChatServer, webSearchTool };
|