@digilogiclabs/platform-core 1.13.0 → 1.15.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/dist/{ConsoleEmail-CqXhZmFN.d.mts → ConsoleEmail-CO0OM4UT.d.ts} +2 -718
- package/dist/{ConsoleEmail-CqXhZmFN.d.ts → ConsoleEmail-DDB28vyS.d.mts} +2 -718
- package/dist/IAI-D8wA_i8N.d.mts +718 -0
- package/dist/IAI-D8wA_i8N.d.ts +718 -0
- package/dist/agents-Cd2eEX5M.d.mts +864 -0
- package/dist/agents-CntmA45w.d.ts +864 -0
- package/dist/agents.d.mts +2 -0
- package/dist/agents.d.ts +2 -0
- package/dist/agents.js +479 -0
- package/dist/agents.js.map +1 -0
- package/dist/agents.mjs +448 -0
- package/dist/agents.mjs.map +1 -0
- package/dist/auth.d.mts +186 -1
- package/dist/auth.d.ts +186 -1
- package/dist/auth.js +205 -0
- package/dist/auth.js.map +1 -1
- package/dist/auth.mjs +204 -0
- package/dist/auth.mjs.map +1 -1
- package/dist/index.d.mts +7 -563
- package/dist/index.d.ts +7 -563
- package/dist/index.js +436 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +431 -0
- package/dist/index.mjs.map +1 -1
- package/dist/testing.d.mts +3 -2
- package/dist/testing.d.ts +3 -2
- package/package.json +6 -1
|
@@ -0,0 +1,718 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cache abstraction interface
|
|
3
|
+
* Provides a vendor-agnostic way to interact with key-value caches
|
|
4
|
+
*/
|
|
5
|
+
interface ICacheOptions {
|
|
6
|
+
/** Time to live in seconds */
|
|
7
|
+
ttl?: number;
|
|
8
|
+
/** Cache key prefix */
|
|
9
|
+
prefix?: string;
|
|
10
|
+
}
|
|
11
|
+
interface ICache {
|
|
12
|
+
/**
|
|
13
|
+
* Get a value from cache
|
|
14
|
+
*/
|
|
15
|
+
get<T = unknown>(key: string): Promise<T | null>;
|
|
16
|
+
/**
|
|
17
|
+
* Set a value in cache
|
|
18
|
+
*/
|
|
19
|
+
set<T = unknown>(key: string, value: T, ttl?: number): Promise<void>;
|
|
20
|
+
/**
|
|
21
|
+
* Delete a value from cache
|
|
22
|
+
*/
|
|
23
|
+
delete(key: string): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Check if key exists
|
|
26
|
+
*/
|
|
27
|
+
exists(key: string): Promise<boolean>;
|
|
28
|
+
/**
|
|
29
|
+
* Delete all keys matching pattern
|
|
30
|
+
*/
|
|
31
|
+
deletePattern(pattern: string): Promise<number>;
|
|
32
|
+
/**
|
|
33
|
+
* Get multiple values
|
|
34
|
+
*/
|
|
35
|
+
mget<T = unknown>(keys: string[]): Promise<(T | null)[]>;
|
|
36
|
+
/**
|
|
37
|
+
* Set multiple values
|
|
38
|
+
*/
|
|
39
|
+
mset<T = unknown>(entries: Array<{
|
|
40
|
+
key: string;
|
|
41
|
+
value: T;
|
|
42
|
+
ttl?: number;
|
|
43
|
+
}>): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* Increment a numeric value
|
|
46
|
+
*/
|
|
47
|
+
incr(key: string, by?: number): Promise<number>;
|
|
48
|
+
/**
|
|
49
|
+
* Publish a message to a channel
|
|
50
|
+
*/
|
|
51
|
+
publish(channel: string, message: string): Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Subscribe to a channel
|
|
54
|
+
*/
|
|
55
|
+
subscribe(channel: string, callback: (message: string) => void): Promise<() => void>;
|
|
56
|
+
/**
|
|
57
|
+
* Check cache connectivity
|
|
58
|
+
*/
|
|
59
|
+
healthCheck(): Promise<boolean>;
|
|
60
|
+
/**
|
|
61
|
+
* Close cache connection
|
|
62
|
+
*/
|
|
63
|
+
close(): Promise<void>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Logger Interface
|
|
68
|
+
* Standardizes logging across the platform
|
|
69
|
+
*/
|
|
70
|
+
/**
|
|
71
|
+
* Log levels in order of severity
|
|
72
|
+
*/
|
|
73
|
+
type LogLevel = "debug" | "info" | "warn" | "error";
|
|
74
|
+
/**
|
|
75
|
+
* Metadata that can be attached to log entries
|
|
76
|
+
*/
|
|
77
|
+
interface LogMeta {
|
|
78
|
+
/** Error object if logging an error */
|
|
79
|
+
error?: Error;
|
|
80
|
+
/** Duration in milliseconds (for timing operations) */
|
|
81
|
+
duration?: number;
|
|
82
|
+
/** Request ID for tracing */
|
|
83
|
+
requestId?: string;
|
|
84
|
+
/** User ID if applicable */
|
|
85
|
+
userId?: string;
|
|
86
|
+
/** Any additional key-value pairs */
|
|
87
|
+
[key: string]: unknown;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* A structured log entry
|
|
91
|
+
*/
|
|
92
|
+
interface LogEntry {
|
|
93
|
+
/** Log level */
|
|
94
|
+
level: LogLevel;
|
|
95
|
+
/** Log message */
|
|
96
|
+
message: string;
|
|
97
|
+
/** When the log was created */
|
|
98
|
+
timestamp: Date;
|
|
99
|
+
/** Optional metadata */
|
|
100
|
+
meta?: LogMeta;
|
|
101
|
+
/** Logger context (e.g., service name) */
|
|
102
|
+
context?: Record<string, unknown>;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Logger interface for structured logging
|
|
106
|
+
*/
|
|
107
|
+
interface ILogger {
|
|
108
|
+
/**
|
|
109
|
+
* Log a debug message (for development/troubleshooting)
|
|
110
|
+
*/
|
|
111
|
+
debug(message: string, meta?: LogMeta): void;
|
|
112
|
+
/**
|
|
113
|
+
* Log an informational message
|
|
114
|
+
*/
|
|
115
|
+
info(message: string, meta?: LogMeta): void;
|
|
116
|
+
/**
|
|
117
|
+
* Log a warning message
|
|
118
|
+
*/
|
|
119
|
+
warn(message: string, meta?: LogMeta): void;
|
|
120
|
+
/**
|
|
121
|
+
* Log an error message
|
|
122
|
+
*/
|
|
123
|
+
error(message: string, meta?: LogMeta): void;
|
|
124
|
+
/**
|
|
125
|
+
* Create a child logger with additional context
|
|
126
|
+
* @param context Additional context to include in all logs from this child
|
|
127
|
+
*/
|
|
128
|
+
child(context: Record<string, unknown>): ILogger;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Configuration options for loggers
|
|
132
|
+
*/
|
|
133
|
+
interface LoggerConfig {
|
|
134
|
+
/** Minimum level to log */
|
|
135
|
+
level?: LogLevel;
|
|
136
|
+
/** Whether to pretty print (for development) */
|
|
137
|
+
pretty?: boolean;
|
|
138
|
+
/** Service name to include in logs */
|
|
139
|
+
service?: string;
|
|
140
|
+
/** Environment name */
|
|
141
|
+
environment?: string;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Console logger implementation (for development)
|
|
145
|
+
*/
|
|
146
|
+
declare class ConsoleLogger implements ILogger {
|
|
147
|
+
private context;
|
|
148
|
+
private level;
|
|
149
|
+
private static levelPriority;
|
|
150
|
+
constructor(config?: LoggerConfig);
|
|
151
|
+
private shouldLog;
|
|
152
|
+
private log;
|
|
153
|
+
debug(message: string, meta?: LogMeta): void;
|
|
154
|
+
info(message: string, meta?: LogMeta): void;
|
|
155
|
+
warn(message: string, meta?: LogMeta): void;
|
|
156
|
+
error(message: string, meta?: LogMeta): void;
|
|
157
|
+
child(context: Record<string, unknown>): ILogger;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* No-op logger for testing or when logging is disabled
|
|
161
|
+
*/
|
|
162
|
+
declare class NoopLogger implements ILogger {
|
|
163
|
+
debug(): void;
|
|
164
|
+
info(): void;
|
|
165
|
+
warn(): void;
|
|
166
|
+
error(): void;
|
|
167
|
+
child(): ILogger;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Distributed Tracing Interface
|
|
172
|
+
*
|
|
173
|
+
* Provides vendor-agnostic distributed tracing for observability.
|
|
174
|
+
* Supports OpenTelemetry, Jaeger, Zipkin, and custom implementations.
|
|
175
|
+
*/
|
|
176
|
+
type SpanKind = "internal" | "server" | "client" | "producer" | "consumer";
|
|
177
|
+
type SpanStatusCode = "unset" | "ok" | "error";
|
|
178
|
+
interface SpanStatus {
|
|
179
|
+
code: SpanStatusCode;
|
|
180
|
+
message?: string;
|
|
181
|
+
}
|
|
182
|
+
interface SpanContext {
|
|
183
|
+
traceId: string;
|
|
184
|
+
spanId: string;
|
|
185
|
+
traceFlags: number;
|
|
186
|
+
traceState?: string;
|
|
187
|
+
}
|
|
188
|
+
interface SpanOptions {
|
|
189
|
+
/** Type of span */
|
|
190
|
+
kind?: SpanKind;
|
|
191
|
+
/** Initial attributes */
|
|
192
|
+
attributes?: Record<string, string | number | boolean>;
|
|
193
|
+
/** Parent span (for manual context propagation) */
|
|
194
|
+
parent?: ISpan;
|
|
195
|
+
/** Links to other spans */
|
|
196
|
+
links?: SpanContext[];
|
|
197
|
+
/** Start time (defaults to now) */
|
|
198
|
+
startTime?: number;
|
|
199
|
+
}
|
|
200
|
+
interface SpanEvent {
|
|
201
|
+
name: string;
|
|
202
|
+
timestamp: number;
|
|
203
|
+
attributes?: Record<string, string | number | boolean>;
|
|
204
|
+
}
|
|
205
|
+
interface ISpan {
|
|
206
|
+
/** Span name */
|
|
207
|
+
readonly name: string;
|
|
208
|
+
/** Span context (trace ID, span ID) */
|
|
209
|
+
readonly context: SpanContext;
|
|
210
|
+
/** Whether the span is recording */
|
|
211
|
+
readonly isRecording: boolean;
|
|
212
|
+
/**
|
|
213
|
+
* Set a single attribute
|
|
214
|
+
*/
|
|
215
|
+
setAttribute(key: string, value: string | number | boolean): this;
|
|
216
|
+
/**
|
|
217
|
+
* Set multiple attributes
|
|
218
|
+
*/
|
|
219
|
+
setAttributes(attributes: Record<string, string | number | boolean>): this;
|
|
220
|
+
/**
|
|
221
|
+
* Add an event to the span
|
|
222
|
+
*/
|
|
223
|
+
addEvent(name: string, attributes?: Record<string, string | number | boolean>): this;
|
|
224
|
+
/**
|
|
225
|
+
* Set the span status
|
|
226
|
+
*/
|
|
227
|
+
setStatus(status: SpanStatus): this;
|
|
228
|
+
/**
|
|
229
|
+
* Record an exception
|
|
230
|
+
*/
|
|
231
|
+
recordException(exception: Error, attributes?: Record<string, string | number | boolean>): this;
|
|
232
|
+
/**
|
|
233
|
+
* Update the span name
|
|
234
|
+
*/
|
|
235
|
+
updateName(name: string): this;
|
|
236
|
+
/**
|
|
237
|
+
* End the span (required to export)
|
|
238
|
+
*/
|
|
239
|
+
end(endTime?: number): void;
|
|
240
|
+
}
|
|
241
|
+
interface ITracing {
|
|
242
|
+
/**
|
|
243
|
+
* Create and start a new span
|
|
244
|
+
*/
|
|
245
|
+
startSpan(name: string, options?: SpanOptions): ISpan;
|
|
246
|
+
/**
|
|
247
|
+
* Get the currently active span
|
|
248
|
+
*/
|
|
249
|
+
getCurrentSpan(): ISpan | undefined;
|
|
250
|
+
/**
|
|
251
|
+
* Execute a function within a span context
|
|
252
|
+
*/
|
|
253
|
+
withSpan<T>(name: string, fn: (span: ISpan) => T, options?: SpanOptions): T;
|
|
254
|
+
/**
|
|
255
|
+
* Execute an async function within a span context
|
|
256
|
+
*/
|
|
257
|
+
withSpanAsync<T>(name: string, fn: (span: ISpan) => Promise<T>, options?: SpanOptions): Promise<T>;
|
|
258
|
+
/**
|
|
259
|
+
* Instrument a function with automatic span creation
|
|
260
|
+
*/
|
|
261
|
+
instrument<TArgs extends unknown[], TReturn>(name: string, fn: (...args: TArgs) => TReturn, options?: SpanOptions): (...args: TArgs) => TReturn;
|
|
262
|
+
/**
|
|
263
|
+
* Instrument an async function with automatic span creation
|
|
264
|
+
*/
|
|
265
|
+
instrumentAsync<TArgs extends unknown[], TReturn>(name: string, fn: (...args: TArgs) => Promise<TReturn>, options?: SpanOptions): (...args: TArgs) => Promise<TReturn>;
|
|
266
|
+
/**
|
|
267
|
+
* Extract trace context from headers (for incoming requests)
|
|
268
|
+
*/
|
|
269
|
+
extractContext(headers: Record<string, string | string[] | undefined>): SpanContext | undefined;
|
|
270
|
+
/**
|
|
271
|
+
* Inject trace context into headers (for outgoing requests)
|
|
272
|
+
*/
|
|
273
|
+
injectContext(headers: Record<string, string>): void;
|
|
274
|
+
/**
|
|
275
|
+
* Check if tracing is enabled and healthy
|
|
276
|
+
*/
|
|
277
|
+
healthCheck(): Promise<boolean>;
|
|
278
|
+
/**
|
|
279
|
+
* Flush pending spans to the exporter
|
|
280
|
+
*/
|
|
281
|
+
flush(): Promise<void>;
|
|
282
|
+
/**
|
|
283
|
+
* Shutdown the tracer
|
|
284
|
+
*/
|
|
285
|
+
close(): Promise<void>;
|
|
286
|
+
}
|
|
287
|
+
interface TracingConfig {
|
|
288
|
+
/** Enable tracing */
|
|
289
|
+
enabled: boolean;
|
|
290
|
+
/** Service name for traces */
|
|
291
|
+
serviceName: string;
|
|
292
|
+
/** Service version */
|
|
293
|
+
serviceVersion?: string;
|
|
294
|
+
/** Environment (dev, staging, production) */
|
|
295
|
+
environment?: string;
|
|
296
|
+
/** Sampling rate (0.0 to 1.0) */
|
|
297
|
+
sampleRate?: number;
|
|
298
|
+
/** Exporter type */
|
|
299
|
+
exporter?: "console" | "otlp" | "jaeger" | "zipkin" | "none";
|
|
300
|
+
/** Exporter endpoint */
|
|
301
|
+
endpoint?: string;
|
|
302
|
+
/** Additional resource attributes */
|
|
303
|
+
resourceAttributes?: Record<string, string>;
|
|
304
|
+
/** Propagation format */
|
|
305
|
+
propagation?: "w3c" | "b3" | "jaeger";
|
|
306
|
+
}
|
|
307
|
+
declare class MemorySpan implements ISpan {
|
|
308
|
+
readonly name: string;
|
|
309
|
+
readonly context: SpanContext;
|
|
310
|
+
readonly isRecording: boolean;
|
|
311
|
+
private _attributes;
|
|
312
|
+
private _events;
|
|
313
|
+
private _status;
|
|
314
|
+
private _endTime?;
|
|
315
|
+
private _startTime;
|
|
316
|
+
constructor(name: string, traceId: string, parentSpanId?: string);
|
|
317
|
+
private generateSpanId;
|
|
318
|
+
setAttribute(key: string, value: string | number | boolean): this;
|
|
319
|
+
setAttributes(attributes: Record<string, string | number | boolean>): this;
|
|
320
|
+
addEvent(name: string, attributes?: Record<string, string | number | boolean>): this;
|
|
321
|
+
setStatus(status: SpanStatus): this;
|
|
322
|
+
recordException(exception: Error, attributes?: Record<string, string | number | boolean>): this;
|
|
323
|
+
updateName(name: string): this;
|
|
324
|
+
end(endTime?: number): void;
|
|
325
|
+
getAttributes(): Record<string, string | number | boolean>;
|
|
326
|
+
getEvents(): SpanEvent[];
|
|
327
|
+
getStatus(): SpanStatus;
|
|
328
|
+
getDuration(): number | undefined;
|
|
329
|
+
isEnded(): boolean;
|
|
330
|
+
}
|
|
331
|
+
declare class MemoryTracing implements ITracing {
|
|
332
|
+
private spans;
|
|
333
|
+
private currentSpan;
|
|
334
|
+
private traceId;
|
|
335
|
+
constructor();
|
|
336
|
+
private generateTraceId;
|
|
337
|
+
startSpan(name: string, options?: SpanOptions): ISpan;
|
|
338
|
+
getCurrentSpan(): ISpan | undefined;
|
|
339
|
+
withSpan<T>(name: string, fn: (span: ISpan) => T, options?: SpanOptions): T;
|
|
340
|
+
withSpanAsync<T>(name: string, fn: (span: ISpan) => Promise<T>, options?: SpanOptions): Promise<T>;
|
|
341
|
+
instrument<TArgs extends unknown[], TReturn>(name: string, fn: (...args: TArgs) => TReturn, options?: SpanOptions): (...args: TArgs) => TReturn;
|
|
342
|
+
instrumentAsync<TArgs extends unknown[], TReturn>(name: string, fn: (...args: TArgs) => Promise<TReturn>, options?: SpanOptions): (...args: TArgs) => Promise<TReturn>;
|
|
343
|
+
extractContext(headers: Record<string, string | string[] | undefined>): SpanContext | undefined;
|
|
344
|
+
injectContext(headers: Record<string, string>): void;
|
|
345
|
+
healthCheck(): Promise<boolean>;
|
|
346
|
+
flush(): Promise<void>;
|
|
347
|
+
close(): Promise<void>;
|
|
348
|
+
getSpans(): MemorySpan[];
|
|
349
|
+
getCompletedSpans(): MemorySpan[];
|
|
350
|
+
clear(): void;
|
|
351
|
+
}
|
|
352
|
+
declare class NoopTracing implements ITracing {
|
|
353
|
+
private noopSpan;
|
|
354
|
+
startSpan(): ISpan;
|
|
355
|
+
getCurrentSpan(): ISpan | undefined;
|
|
356
|
+
withSpan<T>(_name: string, fn: (span: ISpan) => T): T;
|
|
357
|
+
withSpanAsync<T>(_name: string, fn: (span: ISpan) => Promise<T>): Promise<T>;
|
|
358
|
+
instrument<TArgs extends unknown[], TReturn>(_name: string, fn: (...args: TArgs) => TReturn): (...args: TArgs) => TReturn;
|
|
359
|
+
instrumentAsync<TArgs extends unknown[], TReturn>(_name: string, fn: (...args: TArgs) => Promise<TReturn>): (...args: TArgs) => Promise<TReturn>;
|
|
360
|
+
extractContext(): SpanContext | undefined;
|
|
361
|
+
injectContext(): void;
|
|
362
|
+
healthCheck(): Promise<boolean>;
|
|
363
|
+
flush(): Promise<void>;
|
|
364
|
+
close(): Promise<void>;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* IAI - Core AI abstraction interface for platform-core
|
|
369
|
+
*
|
|
370
|
+
* Provides a vendor-agnostic interface for AI operations including:
|
|
371
|
+
* - Text generation (chat, completion)
|
|
372
|
+
* - Embeddings generation
|
|
373
|
+
* - Model routing and fallback
|
|
374
|
+
* - Streaming support
|
|
375
|
+
* - Token usage tracking
|
|
376
|
+
*/
|
|
377
|
+
type AIProvider = "openai" | "anthropic" | "google" | "azure" | "bedrock" | "custom";
|
|
378
|
+
type AIModelType = "chat" | "completion" | "embedding" | "image" | "audio" | "video";
|
|
379
|
+
type AIRole = "system" | "user" | "assistant" | "function" | "tool";
|
|
380
|
+
interface AIMessage {
|
|
381
|
+
role: AIRole;
|
|
382
|
+
content: string;
|
|
383
|
+
name?: string;
|
|
384
|
+
toolCallId?: string;
|
|
385
|
+
toolCalls?: AIToolCall[];
|
|
386
|
+
}
|
|
387
|
+
interface AIToolCall {
|
|
388
|
+
id: string;
|
|
389
|
+
type: "function";
|
|
390
|
+
function: {
|
|
391
|
+
name: string;
|
|
392
|
+
arguments: string;
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
interface AITool {
|
|
396
|
+
type: "function";
|
|
397
|
+
function: {
|
|
398
|
+
name: string;
|
|
399
|
+
description: string;
|
|
400
|
+
parameters: Record<string, unknown>;
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
interface AIModelConfig {
|
|
404
|
+
/** Model identifier (e.g., 'gpt-4', 'claude-3-opus') */
|
|
405
|
+
modelId: string;
|
|
406
|
+
/** Provider for this model */
|
|
407
|
+
provider: AIProvider;
|
|
408
|
+
/** Model capabilities */
|
|
409
|
+
capabilities: AIModelType[];
|
|
410
|
+
/** Maximum context window in tokens */
|
|
411
|
+
maxContextTokens: number;
|
|
412
|
+
/** Maximum output tokens */
|
|
413
|
+
maxOutputTokens: number;
|
|
414
|
+
/** Cost per 1K input tokens in USD */
|
|
415
|
+
inputCostPer1K: number;
|
|
416
|
+
/** Cost per 1K output tokens in USD */
|
|
417
|
+
outputCostPer1K: number;
|
|
418
|
+
/** Supports streaming */
|
|
419
|
+
supportsStreaming: boolean;
|
|
420
|
+
/** Supports function/tool calling */
|
|
421
|
+
supportsTools: boolean;
|
|
422
|
+
/** Supports vision (image input) */
|
|
423
|
+
supportsVision: boolean;
|
|
424
|
+
/** Rate limits per minute */
|
|
425
|
+
rateLimits?: {
|
|
426
|
+
requestsPerMinute: number;
|
|
427
|
+
tokensPerMinute: number;
|
|
428
|
+
};
|
|
429
|
+
/** Custom endpoint for self-hosted models */
|
|
430
|
+
endpoint?: string;
|
|
431
|
+
/** Additional provider-specific config */
|
|
432
|
+
providerConfig?: Record<string, unknown>;
|
|
433
|
+
}
|
|
434
|
+
interface AIChatRequest {
|
|
435
|
+
messages: AIMessage[];
|
|
436
|
+
model?: string;
|
|
437
|
+
temperature?: number;
|
|
438
|
+
maxTokens?: number;
|
|
439
|
+
topP?: number;
|
|
440
|
+
frequencyPenalty?: number;
|
|
441
|
+
presencePenalty?: number;
|
|
442
|
+
stop?: string | string[];
|
|
443
|
+
tools?: AITool[];
|
|
444
|
+
toolChoice?: "auto" | "none" | "required" | {
|
|
445
|
+
type: "function";
|
|
446
|
+
function: {
|
|
447
|
+
name: string;
|
|
448
|
+
};
|
|
449
|
+
};
|
|
450
|
+
stream?: boolean;
|
|
451
|
+
user?: string;
|
|
452
|
+
metadata?: Record<string, unknown>;
|
|
453
|
+
}
|
|
454
|
+
interface AICompletionRequest {
|
|
455
|
+
prompt: string;
|
|
456
|
+
model?: string;
|
|
457
|
+
temperature?: number;
|
|
458
|
+
maxTokens?: number;
|
|
459
|
+
topP?: number;
|
|
460
|
+
frequencyPenalty?: number;
|
|
461
|
+
presencePenalty?: number;
|
|
462
|
+
stop?: string | string[];
|
|
463
|
+
stream?: boolean;
|
|
464
|
+
user?: string;
|
|
465
|
+
metadata?: Record<string, unknown>;
|
|
466
|
+
}
|
|
467
|
+
interface AIEmbeddingRequest {
|
|
468
|
+
input: string | string[];
|
|
469
|
+
model?: string;
|
|
470
|
+
dimensions?: number;
|
|
471
|
+
user?: string;
|
|
472
|
+
metadata?: Record<string, unknown>;
|
|
473
|
+
}
|
|
474
|
+
interface AIChatResponse {
|
|
475
|
+
id: string;
|
|
476
|
+
model: string;
|
|
477
|
+
provider: AIProvider;
|
|
478
|
+
choices: AIChatChoice[];
|
|
479
|
+
usage: AIUsageInfo;
|
|
480
|
+
created: Date;
|
|
481
|
+
finishReason: AIFinishReason;
|
|
482
|
+
}
|
|
483
|
+
interface AIChatChoice {
|
|
484
|
+
index: number;
|
|
485
|
+
message: AIMessage;
|
|
486
|
+
finishReason: AIFinishReason;
|
|
487
|
+
}
|
|
488
|
+
type AIFinishReason = "stop" | "length" | "tool_calls" | "content_filter" | "error";
|
|
489
|
+
interface AICompletionResponse {
|
|
490
|
+
id: string;
|
|
491
|
+
model: string;
|
|
492
|
+
provider: AIProvider;
|
|
493
|
+
text: string;
|
|
494
|
+
usage: AIUsageInfo;
|
|
495
|
+
created: Date;
|
|
496
|
+
finishReason: AIFinishReason;
|
|
497
|
+
}
|
|
498
|
+
interface AIEmbeddingResponse {
|
|
499
|
+
id: string;
|
|
500
|
+
model: string;
|
|
501
|
+
provider: AIProvider;
|
|
502
|
+
embeddings: number[][];
|
|
503
|
+
usage: AIUsageInfo;
|
|
504
|
+
created: Date;
|
|
505
|
+
}
|
|
506
|
+
interface AIUsageInfo {
|
|
507
|
+
promptTokens: number;
|
|
508
|
+
completionTokens: number;
|
|
509
|
+
totalTokens: number;
|
|
510
|
+
estimatedCostUsd: number;
|
|
511
|
+
}
|
|
512
|
+
interface AIStreamChunk {
|
|
513
|
+
id: string;
|
|
514
|
+
model: string;
|
|
515
|
+
provider: AIProvider;
|
|
516
|
+
delta: {
|
|
517
|
+
content?: string;
|
|
518
|
+
toolCalls?: AIToolCall[];
|
|
519
|
+
role?: AIRole;
|
|
520
|
+
};
|
|
521
|
+
finishReason?: AIFinishReason;
|
|
522
|
+
usage?: AIUsageInfo;
|
|
523
|
+
}
|
|
524
|
+
type AIStreamCallback = (chunk: AIStreamChunk) => void | Promise<void>;
|
|
525
|
+
type RoutingStrategy = "priority" | "round-robin" | "least-latency" | "cost-optimized" | "random";
|
|
526
|
+
interface AIRouterConfig {
|
|
527
|
+
/** Primary model to use */
|
|
528
|
+
primaryModel: string;
|
|
529
|
+
/** Fallback models in order of preference */
|
|
530
|
+
fallbackModels?: string[];
|
|
531
|
+
/** Routing strategy for load balancing */
|
|
532
|
+
strategy?: RoutingStrategy;
|
|
533
|
+
/** Maximum retries before failing */
|
|
534
|
+
maxRetries?: number;
|
|
535
|
+
/** Timeout in milliseconds */
|
|
536
|
+
timeoutMs?: number;
|
|
537
|
+
/** Enable automatic fallback on errors */
|
|
538
|
+
autoFallback?: boolean;
|
|
539
|
+
/** Cost budget per request in USD (for cost-optimized routing) */
|
|
540
|
+
costBudget?: number;
|
|
541
|
+
/** Custom routing function */
|
|
542
|
+
customRouter?: (request: AIChatRequest | AICompletionRequest) => string;
|
|
543
|
+
}
|
|
544
|
+
type AIErrorCode = "invalid_request" | "authentication_error" | "rate_limit_exceeded" | "quota_exceeded" | "model_not_found" | "context_length_exceeded" | "content_filter" | "server_error" | "timeout" | "network_error" | "provider_unavailable" | "unknown";
|
|
545
|
+
interface AIError extends Error {
|
|
546
|
+
code: AIErrorCode;
|
|
547
|
+
provider?: AIProvider;
|
|
548
|
+
model?: string;
|
|
549
|
+
statusCode?: number;
|
|
550
|
+
retryable: boolean;
|
|
551
|
+
retryAfterMs?: number;
|
|
552
|
+
}
|
|
553
|
+
declare const AIErrorMessages: Record<AIErrorCode, string>;
|
|
554
|
+
declare function createAIError(code: AIErrorCode, message?: string, options?: {
|
|
555
|
+
provider?: AIProvider;
|
|
556
|
+
model?: string;
|
|
557
|
+
statusCode?: number;
|
|
558
|
+
retryable?: boolean;
|
|
559
|
+
retryAfterMs?: number;
|
|
560
|
+
cause?: Error;
|
|
561
|
+
}): AIError;
|
|
562
|
+
declare function isAIError(error: unknown): error is AIError;
|
|
563
|
+
interface AIConfig {
|
|
564
|
+
/** Default model for chat requests */
|
|
565
|
+
defaultChatModel?: string;
|
|
566
|
+
/** Default model for completion requests */
|
|
567
|
+
defaultCompletionModel?: string;
|
|
568
|
+
/** Default model for embedding requests */
|
|
569
|
+
defaultEmbeddingModel?: string;
|
|
570
|
+
/** Router configuration */
|
|
571
|
+
router?: AIRouterConfig;
|
|
572
|
+
/** Available model configurations */
|
|
573
|
+
models?: AIModelConfig[];
|
|
574
|
+
/** API keys for providers */
|
|
575
|
+
apiKeys?: Partial<Record<AIProvider, string>>;
|
|
576
|
+
/** Default timeout in milliseconds */
|
|
577
|
+
defaultTimeoutMs?: number;
|
|
578
|
+
/** Enable request/response logging */
|
|
579
|
+
enableLogging?: boolean;
|
|
580
|
+
/** Cache embeddings */
|
|
581
|
+
cacheEmbeddings?: boolean;
|
|
582
|
+
/** Embedding cache TTL in seconds */
|
|
583
|
+
embeddingCacheTtlSeconds?: number;
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* IAI - Core AI interface
|
|
587
|
+
*
|
|
588
|
+
* @example
|
|
589
|
+
* ```typescript
|
|
590
|
+
* const ai = platform.ai;
|
|
591
|
+
*
|
|
592
|
+
* // Chat completion
|
|
593
|
+
* const response = await ai.chat({
|
|
594
|
+
* messages: [
|
|
595
|
+
* { role: 'system', content: 'You are a helpful assistant.' },
|
|
596
|
+
* { role: 'user', content: 'Hello!' }
|
|
597
|
+
* ]
|
|
598
|
+
* });
|
|
599
|
+
*
|
|
600
|
+
* // Streaming chat
|
|
601
|
+
* for await (const chunk of ai.chatStream({
|
|
602
|
+
* messages: [{ role: 'user', content: 'Tell me a story' }]
|
|
603
|
+
* })) {
|
|
604
|
+
* process.stdout.write(chunk.delta.content || '');
|
|
605
|
+
* }
|
|
606
|
+
*
|
|
607
|
+
* // Embeddings
|
|
608
|
+
* const embeddings = await ai.embed({
|
|
609
|
+
* input: ['Hello world', 'Goodbye world']
|
|
610
|
+
* });
|
|
611
|
+
* ```
|
|
612
|
+
*/
|
|
613
|
+
interface IAI {
|
|
614
|
+
/**
|
|
615
|
+
* Send a chat completion request
|
|
616
|
+
*/
|
|
617
|
+
chat(request: AIChatRequest): Promise<AIChatResponse>;
|
|
618
|
+
/**
|
|
619
|
+
* Send a streaming chat completion request
|
|
620
|
+
*/
|
|
621
|
+
chatStream(request: AIChatRequest): AsyncIterable<AIStreamChunk>;
|
|
622
|
+
/**
|
|
623
|
+
* Send a chat completion request with callback-based streaming
|
|
624
|
+
*/
|
|
625
|
+
chatWithCallback(request: AIChatRequest, callback: AIStreamCallback): Promise<AIChatResponse>;
|
|
626
|
+
/**
|
|
627
|
+
* Send a text completion request
|
|
628
|
+
*/
|
|
629
|
+
complete(request: AICompletionRequest): Promise<AICompletionResponse>;
|
|
630
|
+
/**
|
|
631
|
+
* Send a streaming completion request
|
|
632
|
+
*/
|
|
633
|
+
completeStream(request: AICompletionRequest): AsyncIterable<AIStreamChunk>;
|
|
634
|
+
/**
|
|
635
|
+
* Generate embeddings for text
|
|
636
|
+
*/
|
|
637
|
+
embed(request: AIEmbeddingRequest): Promise<AIEmbeddingResponse>;
|
|
638
|
+
/**
|
|
639
|
+
* Calculate similarity between two texts
|
|
640
|
+
*/
|
|
641
|
+
similarity(text1: string, text2: string, model?: string): Promise<number>;
|
|
642
|
+
/**
|
|
643
|
+
* List available models
|
|
644
|
+
*/
|
|
645
|
+
listModels(): Promise<AIModelConfig[]>;
|
|
646
|
+
/**
|
|
647
|
+
* Get model configuration
|
|
648
|
+
*/
|
|
649
|
+
getModel(modelId: string): Promise<AIModelConfig | null>;
|
|
650
|
+
/**
|
|
651
|
+
* Check if a model supports a capability
|
|
652
|
+
*/
|
|
653
|
+
supportsCapability(modelId: string, capability: AIModelType): Promise<boolean>;
|
|
654
|
+
/**
|
|
655
|
+
* Estimate tokens for text
|
|
656
|
+
*/
|
|
657
|
+
estimateTokens(text: string, model?: string): Promise<number>;
|
|
658
|
+
/**
|
|
659
|
+
* Estimate cost for a request
|
|
660
|
+
*/
|
|
661
|
+
estimateCost(request: AIChatRequest | AICompletionRequest | AIEmbeddingRequest): Promise<number>;
|
|
662
|
+
/**
|
|
663
|
+
* Check if AI service is healthy
|
|
664
|
+
*/
|
|
665
|
+
healthCheck(): Promise<{
|
|
666
|
+
healthy: boolean;
|
|
667
|
+
providers: Record<AIProvider, {
|
|
668
|
+
available: boolean;
|
|
669
|
+
latencyMs?: number;
|
|
670
|
+
error?: string;
|
|
671
|
+
}>;
|
|
672
|
+
}>;
|
|
673
|
+
}
|
|
674
|
+
/**
|
|
675
|
+
* MemoryAI - In-memory implementation for testing
|
|
676
|
+
*/
|
|
677
|
+
declare class MemoryAI implements IAI {
|
|
678
|
+
private config;
|
|
679
|
+
private models;
|
|
680
|
+
private responses;
|
|
681
|
+
private embeddings;
|
|
682
|
+
private requestLog;
|
|
683
|
+
constructor(config?: AIConfig);
|
|
684
|
+
setResponse(key: string, response: AIChatResponse | AICompletionResponse): void;
|
|
685
|
+
setEmbedding(text: string, embedding: number[]): void;
|
|
686
|
+
getRequestLog(): Array<{
|
|
687
|
+
type: string;
|
|
688
|
+
request: unknown;
|
|
689
|
+
timestamp: Date;
|
|
690
|
+
}>;
|
|
691
|
+
clearRequestLog(): void;
|
|
692
|
+
chat(request: AIChatRequest): Promise<AIChatResponse>;
|
|
693
|
+
chatStream(request: AIChatRequest): AsyncIterable<AIStreamChunk>;
|
|
694
|
+
chatWithCallback(request: AIChatRequest, callback: AIStreamCallback): Promise<AIChatResponse>;
|
|
695
|
+
complete(request: AICompletionRequest): Promise<AICompletionResponse>;
|
|
696
|
+
completeStream(request: AICompletionRequest): AsyncIterable<AIStreamChunk>;
|
|
697
|
+
embed(request: AIEmbeddingRequest): Promise<AIEmbeddingResponse>;
|
|
698
|
+
similarity(text1: string, text2: string, model?: string): Promise<number>;
|
|
699
|
+
listModels(): Promise<AIModelConfig[]>;
|
|
700
|
+
getModel(modelId: string): Promise<AIModelConfig | null>;
|
|
701
|
+
supportsCapability(modelId: string, capability: AIModelType): Promise<boolean>;
|
|
702
|
+
estimateTokens(text: string, _model?: string): Promise<number>;
|
|
703
|
+
estimateCost(request: AIChatRequest | AICompletionRequest | AIEmbeddingRequest): Promise<number>;
|
|
704
|
+
healthCheck(): Promise<{
|
|
705
|
+
healthy: boolean;
|
|
706
|
+
providers: Record<AIProvider, {
|
|
707
|
+
available: boolean;
|
|
708
|
+
latencyMs?: number;
|
|
709
|
+
error?: string;
|
|
710
|
+
}>;
|
|
711
|
+
}>;
|
|
712
|
+
private estimateTokensSync;
|
|
713
|
+
private calculateCost;
|
|
714
|
+
private generateMockEmbedding;
|
|
715
|
+
private cosineSimilarity;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
export { type AIConfig as A, type AIFinishReason as B, ConsoleLogger as C, type AIUsageInfo as D, type AIRouterConfig as E, type AIErrorCode as F, type AIError as G, MemoryTracing as H, type ICache as I, NoopTracing as J, type ISpan as K, type LogLevel as L, MemoryAI as M, NoopLogger as N, type SpanOptions as O, type SpanStatus as P, type SpanKind as Q, type RoutingStrategy as R, type SpanContext as S, type TracingConfig as T, type SpanStatusCode as U, type SpanEvent as V, type ILogger as a, type ITracing as b, type IAI as c, type AIChatRequest as d, type AIChatResponse as e, type AIStreamChunk as f, type AIStreamCallback as g, type AICompletionRequest as h, type AICompletionResponse as i, type AIEmbeddingRequest as j, type AIEmbeddingResponse as k, type AIModelConfig as l, type AIModelType as m, type AIProvider as n, type ICacheOptions as o, type LogMeta as p, type LogEntry as q, type LoggerConfig as r, createAIError as s, isAIError as t, AIErrorMessages as u, type AIRole as v, type AIMessage as w, type AIToolCall as x, type AITool as y, type AIChatChoice as z };
|