ai.matey.core 0.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.
Files changed (51) hide show
  1. package/LICENSE +21 -0
  2. package/dist/cjs/bridge.js +657 -0
  3. package/dist/cjs/bridge.js.map +1 -0
  4. package/dist/cjs/capability-inference.js +349 -0
  5. package/dist/cjs/capability-inference.js.map +1 -0
  6. package/dist/cjs/capability-matcher.js +216 -0
  7. package/dist/cjs/capability-matcher.js.map +1 -0
  8. package/dist/cjs/index.js +31 -0
  9. package/dist/cjs/index.js.map +1 -0
  10. package/dist/cjs/middleware-stack.js +256 -0
  11. package/dist/cjs/middleware-stack.js.map +1 -0
  12. package/dist/cjs/model-pricing.js +350 -0
  13. package/dist/cjs/model-pricing.js.map +1 -0
  14. package/dist/cjs/model-translation.js +171 -0
  15. package/dist/cjs/model-translation.js.map +1 -0
  16. package/dist/cjs/router.js +1388 -0
  17. package/dist/cjs/router.js.map +1 -0
  18. package/dist/esm/bridge.js +652 -0
  19. package/dist/esm/bridge.js.map +1 -0
  20. package/dist/esm/capability-inference.js +343 -0
  21. package/dist/esm/capability-inference.js.map +1 -0
  22. package/dist/esm/capability-matcher.js +210 -0
  23. package/dist/esm/capability-matcher.js.map +1 -0
  24. package/dist/esm/index.js +15 -0
  25. package/dist/esm/index.js.map +1 -0
  26. package/dist/esm/middleware-stack.js +250 -0
  27. package/dist/esm/middleware-stack.js.map +1 -0
  28. package/dist/esm/model-pricing.js +340 -0
  29. package/dist/esm/model-pricing.js.map +1 -0
  30. package/dist/esm/model-translation.js +163 -0
  31. package/dist/esm/model-translation.js.map +1 -0
  32. package/dist/esm/router.js +1383 -0
  33. package/dist/esm/router.js.map +1 -0
  34. package/dist/types/bridge.d.ts +254 -0
  35. package/dist/types/bridge.d.ts.map +1 -0
  36. package/dist/types/capability-inference.d.ts +35 -0
  37. package/dist/types/capability-inference.d.ts.map +1 -0
  38. package/dist/types/capability-matcher.d.ts +104 -0
  39. package/dist/types/capability-matcher.d.ts.map +1 -0
  40. package/dist/types/index.d.ts +15 -0
  41. package/dist/types/index.d.ts.map +1 -0
  42. package/dist/types/middleware-stack.d.ts +102 -0
  43. package/dist/types/middleware-stack.d.ts.map +1 -0
  44. package/dist/types/model-pricing.d.ts +81 -0
  45. package/dist/types/model-pricing.d.ts.map +1 -0
  46. package/dist/types/model-translation.d.ts +171 -0
  47. package/dist/types/model-translation.d.ts.map +1 -0
  48. package/dist/types/router.d.ts +287 -0
  49. package/dist/types/router.d.ts.map +1 -0
  50. package/package.json +70 -0
  51. package/readme.md +34 -0
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Model Pricing Registry
3
+ *
4
+ * Centralized pricing database for AI models across providers.
5
+ * Prices are in USD per 1,000 tokens.
6
+ *
7
+ * @module
8
+ */
9
+ import type { ModelCapabilities } from 'ai.matey.types';
10
+ /**
11
+ * Pricing data for a specific model.
12
+ */
13
+ export interface ModelPricing {
14
+ readonly input: number;
15
+ readonly output: number;
16
+ }
17
+ /**
18
+ * Extended capabilities including estimated quality and latency.
19
+ */
20
+ export interface ExtendedModelCapabilities extends ModelCapabilities {
21
+ readonly pricing: ModelPricing;
22
+ readonly latency?: {
23
+ readonly p50?: number;
24
+ readonly p95?: number;
25
+ };
26
+ readonly qualityScore?: number;
27
+ readonly modelFamily?: string;
28
+ readonly releaseDate?: string;
29
+ }
30
+ /**
31
+ * Get pricing information for a specific model.
32
+ *
33
+ * @param modelId - The model identifier (e.g., "gpt-4", "claude-3-opus-20240229")
34
+ * @returns Pricing data or null if not found
35
+ */
36
+ export declare function getModelPricing(modelId: string): ModelPricing | null;
37
+ /**
38
+ * Get extended capabilities for a specific model.
39
+ *
40
+ * @param modelId - The model identifier
41
+ * @returns Extended capabilities or null if not found
42
+ */
43
+ export declare function getModelCapabilities(modelId: string): Partial<ExtendedModelCapabilities> | null;
44
+ /**
45
+ * Get all models with pricing data.
46
+ *
47
+ * @returns Array of model IDs with pricing information
48
+ */
49
+ export declare function getAllPricedModels(): string[];
50
+ /**
51
+ * Find models by model family.
52
+ *
53
+ * @param family - The model family (e.g., "gpt-4", "claude-3")
54
+ * @returns Array of model IDs in the family
55
+ */
56
+ export declare function getModelsByFamily(family: string): string[];
57
+ /**
58
+ * Set custom pricing for a model (overrides static database).
59
+ *
60
+ * @param modelId - The model identifier
61
+ * @param pricing - Custom pricing data
62
+ */
63
+ export declare function setPricingOverride(modelId: string, pricing: ModelPricing): void;
64
+ /**
65
+ * Clear pricing override for a model.
66
+ *
67
+ * @param modelId - The model identifier
68
+ */
69
+ export declare function clearPricingOverride(modelId: string): void;
70
+ /**
71
+ * Clear all pricing overrides.
72
+ */
73
+ export declare function clearAllPricingOverrides(): void;
74
+ /**
75
+ * Get pricing with user overrides applied.
76
+ *
77
+ * @param modelId - The model identifier
78
+ * @returns Pricing data (override if set, else from database)
79
+ */
80
+ export declare function getPricingWithOverrides(modelId: string): ModelPricing | null;
81
+ //# sourceMappingURL=model-pricing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"model-pricing.d.ts","sourceRoot":"","sources":["../../src/model-pricing.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAExD;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,yBAA0B,SAAQ,iBAAiB;IAClE,QAAQ,CAAC,OAAO,EAAE,YAAY,CAAC;IAC/B,QAAQ,CAAC,OAAO,CAAC,EAAE;QACjB,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC;QACtB,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC;KACvB,CAAC;IACF,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;CAC/B;AAyQD;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,YAAY,GAAG,IAAI,CAGpE;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,yBAAyB,CAAC,GAAG,IAAI,CAE/F;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,IAAI,MAAM,EAAE,CAE7C;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAI1D;AAQD;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,GAAG,IAAI,CAE/E;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAE1D;AAED;;GAEG;AACH,wBAAgB,wBAAwB,IAAI,IAAI,CAE/C;AAED;;;;;GAKG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,MAAM,GAAG,YAAY,GAAG,IAAI,CAE5E"}
@@ -0,0 +1,171 @@
1
+ /**
2
+ * Core Model Translation
3
+ *
4
+ * Backend-agnostic model translation utilities for converting model names
5
+ * between different AI providers. Works in any environment (Node.js, Browser, Deno, etc.).
6
+ *
7
+ * @module core/model-translation
8
+ */
9
+ /**
10
+ * Model name mapping (source model → target model).
11
+ */
12
+ export type ModelMapping = Record<string, string>;
13
+ /**
14
+ * Translation strategy for Router fallback.
15
+ */
16
+ export type ModelTranslationStrategy = 'exact' | 'pattern' | 'hybrid' | 'none';
17
+ /**
18
+ * Configuration for model translation behavior.
19
+ */
20
+ export interface ModelTranslationConfig {
21
+ /**
22
+ * Translation strategy to use.
23
+ * @default 'hybrid'
24
+ */
25
+ readonly strategy: ModelTranslationStrategy;
26
+ /**
27
+ * Emit warning event when using backend default model.
28
+ * @default true
29
+ */
30
+ readonly warnOnDefault?: boolean;
31
+ /**
32
+ * Throw error if no translation found (instead of returning original).
33
+ * @default false
34
+ */
35
+ readonly strictMode?: boolean;
36
+ }
37
+ /**
38
+ * Options for translateModel function.
39
+ */
40
+ export interface ModelTranslationOptions {
41
+ /**
42
+ * Exact model mappings.
43
+ */
44
+ readonly mapping?: ModelMapping;
45
+ /**
46
+ * Backend default model (used in hybrid strategy).
47
+ */
48
+ readonly defaultModel?: string;
49
+ /**
50
+ * Translation strategy.
51
+ * @default 'exact'
52
+ */
53
+ readonly strategy?: ModelTranslationStrategy;
54
+ /**
55
+ * Throw error if no translation found.
56
+ * @default false
57
+ */
58
+ readonly strictMode?: boolean;
59
+ }
60
+ /**
61
+ * Result of model translation.
62
+ */
63
+ export interface TranslationResult {
64
+ /**
65
+ * Translated model name.
66
+ */
67
+ readonly translated: string;
68
+ /**
69
+ * Source of translation.
70
+ */
71
+ readonly source: 'exact' | 'pattern' | 'default' | 'none';
72
+ /**
73
+ * Whether a translation was applied.
74
+ */
75
+ readonly wasTranslated: boolean;
76
+ }
77
+ /**
78
+ * Translate a model name using exact mapping.
79
+ *
80
+ * @param modelName - Original model name
81
+ * @param options - Translation options
82
+ * @returns Translation result
83
+ *
84
+ * @example
85
+ * ```typescript
86
+ * const result = translateModel('gpt-4', {
87
+ * mapping: { 'gpt-4': 'claude-3-5-sonnet-20241022' }
88
+ * });
89
+ * // → { translated: 'claude-3-5-sonnet-20241022', source: 'exact', wasTranslated: true }
90
+ * ```
91
+ */
92
+ export declare function translateModel(modelName: string, options?: ModelTranslationOptions): TranslationResult;
93
+ /**
94
+ * Create a model translator with pre-configured options.
95
+ *
96
+ * @param options - Default translation options
97
+ * @returns Translator function
98
+ *
99
+ * @example
100
+ * ```typescript
101
+ * const translator = createModelTranslator({
102
+ * mapping: { 'gpt-4': 'claude-3-5-sonnet-20241022' },
103
+ * strategy: 'hybrid',
104
+ * defaultModel: 'claude-3-5-haiku-20241022'
105
+ * });
106
+ *
107
+ * const result = translator('gpt-4');
108
+ * // → { translated: 'claude-3-5-sonnet-20241022', source: 'exact', wasTranslated: true }
109
+ * ```
110
+ */
111
+ export declare function createModelTranslator(defaultOptions: ModelTranslationOptions): (modelName: string, overrides?: Partial<ModelTranslationOptions>) => TranslationResult;
112
+ /**
113
+ * Get reverse mapping (target model → source model).
114
+ *
115
+ * @param mapping - Forward mapping
116
+ * @returns Reverse mapping
117
+ *
118
+ * @example
119
+ * ```typescript
120
+ * const forward = { 'gpt-4': 'claude-3-5-sonnet' };
121
+ * const reverse = reverseMapping(forward);
122
+ * // → { 'claude-3-5-sonnet': 'gpt-4' }
123
+ * ```
124
+ */
125
+ export declare function reverseMapping(mapping: ModelMapping): ModelMapping;
126
+ /**
127
+ * Check if a model name has an exact translation.
128
+ *
129
+ * @param modelName - Model name to check
130
+ * @param mapping - Model mapping
131
+ * @returns True if exact translation exists
132
+ *
133
+ * @example
134
+ * ```typescript
135
+ * const mapping = { 'gpt-4': 'claude-3-5-sonnet' };
136
+ * hasTranslation('gpt-4', mapping); // → true
137
+ * hasTranslation('gpt-3.5', mapping); // → false
138
+ * ```
139
+ */
140
+ export declare function hasTranslation(modelName: string, mapping: ModelMapping): boolean;
141
+ /**
142
+ * Merge multiple model mappings into one.
143
+ *
144
+ * Later mappings take precedence over earlier ones.
145
+ *
146
+ * @param mappings - Array of mappings to merge
147
+ * @returns Merged mapping
148
+ *
149
+ * @example
150
+ * ```typescript
151
+ * const mapping1 = { 'gpt-4': 'claude-3-opus' };
152
+ * const mapping2 = { 'gpt-4': 'claude-3-5-sonnet' }; // Override
153
+ * const merged = mergeMappings(mapping1, mapping2);
154
+ * // → { 'gpt-4': 'claude-3-5-sonnet' }
155
+ * ```
156
+ */
157
+ export declare function mergeMappings(...mappings: ModelMapping[]): ModelMapping;
158
+ /**
159
+ * Validate that a model mapping is well-formed.
160
+ *
161
+ * @param mapping - Mapping to validate
162
+ * @throws Error if mapping is invalid
163
+ *
164
+ * @example
165
+ * ```typescript
166
+ * validateMapping({ 'gpt-4': 'claude-3-5-sonnet' }); // OK
167
+ * validateMapping({ 'gpt-4': '' }); // Throws: empty target model
168
+ * ```
169
+ */
170
+ export declare function validateMapping(mapping: ModelMapping): void;
171
+ //# sourceMappingURL=model-translation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"model-translation.d.ts","sourceRoot":"","sources":["../../src/model-translation.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAMH;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAElD;;GAEG;AACH,MAAM,MAAM,wBAAwB,GAChC,OAAO,GACP,SAAS,GACT,QAAQ,GACR,MAAM,CAAC;AAEX;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC;;;OAGG;IACH,QAAQ,CAAC,QAAQ,EAAE,wBAAwB,CAAC;IAE5C;;;OAGG;IACH,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAC;IAEjC;;;OAGG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,OAAO,CAAC;CAC/B;AAED;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC;;OAEG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,YAAY,CAAC;IAEhC;;OAEG;IACH,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;IAE/B;;;OAGG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,wBAAwB,CAAC;IAE7C;;;OAGG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,OAAO,CAAC;CAC/B;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC;;OAEG;IACH,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAE5B;;OAEG;IACH,QAAQ,CAAC,MAAM,EAAE,OAAO,GAAG,SAAS,GAAG,SAAS,GAAG,MAAM,CAAC;IAE1D;;OAEG;IACH,QAAQ,CAAC,aAAa,EAAE,OAAO,CAAC;CACjC;AAMD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,cAAc,CAC5B,SAAS,EAAE,MAAM,EACjB,OAAO,GAAE,uBAA4B,GACpC,iBAAiB,CA+BnB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,qBAAqB,CACnC,cAAc,EAAE,uBAAuB,GACtC,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,OAAO,CAAC,uBAAuB,CAAC,KAAK,iBAAiB,CAIxF;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,YAAY,GAAG,YAAY,CAMlE;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,GAAG,OAAO,CAEhF;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,aAAa,CAAC,GAAG,QAAQ,EAAE,YAAY,EAAE,GAAG,YAAY,CAMvE;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,YAAY,GAAG,IAAI,CAa3D"}
@@ -0,0 +1,287 @@
1
+ /**
2
+ * Router Implementation
3
+ *
4
+ * The Router manages multiple backend adapters with intelligent routing,
5
+ * fallback strategies, circuit breaker pattern, and health checking.
6
+ * It implements the BackendAdapter interface so it can be used as a backend.
7
+ *
8
+ * @module
9
+ */
10
+ import type { BackendAdapter, AdapterMetadata } from 'ai.matey.types';
11
+ import type { IRChatRequest, IRChatResponse, IRChatStream } from 'ai.matey.types';
12
+ import type { Router as IRouter, RouterConfig, BackendInfo, BackendStats, RouterStats, ModelMapping, ModelPatternMapping, ParallelDispatchOptions, ParallelDispatchResult } from 'ai.matey.types';
13
+ /**
14
+ * Router manages multiple backend adapters with intelligent routing.
15
+ */
16
+ export declare class Router implements IRouter {
17
+ readonly metadata: AdapterMetadata;
18
+ readonly config: RouterConfig;
19
+ private backends;
20
+ private modelMapping;
21
+ private modelTranslationMapping;
22
+ private backendTranslationMappings;
23
+ private modelPatterns;
24
+ private fallbackChain;
25
+ private roundRobinIndex;
26
+ private healthCheckInterval?;
27
+ private stats;
28
+ constructor(config?: Partial<RouterConfig>);
29
+ /**
30
+ * Convert IR request to provider format.
31
+ * Not applicable for Router - use the specific backend adapter instead.
32
+ */
33
+ fromIR(_request: IRChatRequest): unknown;
34
+ /**
35
+ * Convert provider response to IR format.
36
+ * Not applicable for Router - use the specific backend adapter instead.
37
+ */
38
+ toIR(_response: unknown, _originalRequest: IRChatRequest, _latencyMs: number): IRChatResponse;
39
+ /**
40
+ * Register a backend adapter.
41
+ */
42
+ register(name: string, adapter: BackendAdapter): Router;
43
+ /**
44
+ * Unregister a backend adapter.
45
+ */
46
+ unregister(name: string): Router;
47
+ /**
48
+ * Get a registered backend adapter.
49
+ */
50
+ get(name: string): BackendAdapter | undefined;
51
+ /**
52
+ * Check if backend is registered.
53
+ */
54
+ has(name: string): boolean;
55
+ /**
56
+ * List all registered backend names.
57
+ */
58
+ listBackends(): readonly string[];
59
+ /**
60
+ * Get information about all or specific backend.
61
+ */
62
+ getBackendInfo(): BackendInfo[];
63
+ getBackendInfo(name: string): BackendInfo | undefined;
64
+ /**
65
+ * Set fallback chain for sequential failover.
66
+ */
67
+ setFallbackChain(chain: readonly string[]): Router;
68
+ /**
69
+ * Get current fallback chain.
70
+ */
71
+ getFallbackChain(): readonly string[];
72
+ /**
73
+ * Set model to backend mapping.
74
+ */
75
+ setModelMapping(mapping: ModelMapping): Router;
76
+ /**
77
+ * Get current model mapping.
78
+ */
79
+ getModelMapping(): ModelMapping;
80
+ /**
81
+ * Set model name translation mapping (for fallback scenarios).
82
+ * Maps source model names to target model names.
83
+ *
84
+ * @example
85
+ * ```typescript
86
+ * router.setModelTranslationMapping({
87
+ * 'gpt-4': 'claude-3-5-sonnet-20241022',
88
+ * 'gpt-3.5-turbo': 'claude-3-5-haiku-20241022'
89
+ * });
90
+ * ```
91
+ */
92
+ setModelTranslationMapping(mapping: ModelMapping): Router;
93
+ /**
94
+ * Get current model translation mapping.
95
+ */
96
+ getModelTranslationMapping(): ModelMapping;
97
+ /**
98
+ * Set backend-specific model translation mapping.
99
+ * This takes priority over global model translation mapping.
100
+ *
101
+ * @example
102
+ * ```typescript
103
+ * router.setBackendTranslationMapping('anthropic', {
104
+ * 'gpt-4': 'claude-3-5-sonnet-20241022',
105
+ * 'gpt-3.5-turbo': 'claude-3-5-haiku-20241022'
106
+ * });
107
+ * ```
108
+ */
109
+ setBackendTranslationMapping(backendName: string, mapping: ModelMapping): Router;
110
+ /**
111
+ * Get backend-specific model translation mapping.
112
+ */
113
+ getBackendTranslationMapping(backendName: string): ModelMapping;
114
+ /**
115
+ * Clear all model translation mappings.
116
+ *
117
+ * @returns This router for chaining
118
+ */
119
+ clearModelTranslationMapping(): Router;
120
+ /**
121
+ * Clear backend-specific model translation mappings.
122
+ *
123
+ * @param backendName Optional backend name to clear. If not provided, clears all.
124
+ * @returns This router for chaining
125
+ */
126
+ clearBackendTranslationMapping(backendName?: string): Router;
127
+ /**
128
+ * Set model pattern mappings.
129
+ */
130
+ setModelPatterns(patterns: readonly ModelPatternMapping[]): Router;
131
+ /**
132
+ * Get current model patterns.
133
+ */
134
+ getModelPatterns(): readonly ModelPatternMapping[];
135
+ /**
136
+ * Select backend for a request.
137
+ */
138
+ selectBackend(request: IRChatRequest, preferredBackend?: string): Promise<string>;
139
+ /**
140
+ * Execute request with automatic backend selection and fallback.
141
+ */
142
+ execute(request: IRChatRequest, signal?: AbortSignal): Promise<IRChatResponse>;
143
+ /**
144
+ * Execute streaming request with automatic backend selection and fallback.
145
+ */
146
+ executeStream(request: IRChatRequest, signal?: AbortSignal): IRChatStream;
147
+ /**
148
+ * Dispatch request to multiple backends in parallel.
149
+ */
150
+ dispatchParallel(request: IRChatRequest, options?: ParallelDispatchOptions, signal?: AbortSignal): Promise<ParallelDispatchResult>;
151
+ /**
152
+ * Check health of all or specific backend.
153
+ */
154
+ checkHealth(): Promise<Record<string, boolean>>;
155
+ checkHealth(name: string): Promise<boolean>;
156
+ /**
157
+ * Manually open circuit breaker for a backend.
158
+ */
159
+ openCircuitBreaker(name: string, timeoutMs?: number): void;
160
+ /**
161
+ * Manually close circuit breaker for a backend.
162
+ */
163
+ closeCircuitBreaker(name: string): void;
164
+ /**
165
+ * Reset circuit breaker statistics.
166
+ */
167
+ resetCircuitBreaker(name?: string): void;
168
+ /**
169
+ * Check if circuit breaker is open for a backend.
170
+ *
171
+ * @param name Backend name
172
+ * @returns true if circuit breaker is open, false otherwise
173
+ */
174
+ isCircuitBreakerOpen(name: string): boolean;
175
+ /**
176
+ * Get router statistics.
177
+ */
178
+ getStats(): RouterStats;
179
+ /**
180
+ * Reset router statistics.
181
+ */
182
+ resetStats(): void;
183
+ /**
184
+ * Get statistics for specific backend.
185
+ */
186
+ getBackendStats(name: string): BackendStats | undefined;
187
+ /**
188
+ * Clone router with new configuration.
189
+ */
190
+ clone(config: Partial<RouterConfig>): Router;
191
+ /**
192
+ * Clean up resources.
193
+ */
194
+ dispose(): void;
195
+ /**
196
+ * Execute request on specific backend.
197
+ */
198
+ private executeOnBackend;
199
+ /**
200
+ * Execute streaming request on specific backend.
201
+ */
202
+ private executeStreamOnBackend;
203
+ /**
204
+ * Execute fallback strategy.
205
+ */
206
+ private executeFallback;
207
+ /**
208
+ * Translate model name for a specific backend.
209
+ *
210
+ * Applies translation strategy: backend-specific exact → global exact → pattern → default → passthrough
211
+ */
212
+ private translateModelForBackend;
213
+ /**
214
+ * Sequential fallback: try backends one by one.
215
+ */
216
+ private fallbackSequential;
217
+ /**
218
+ * Parallel fallback: try all remaining backends at once.
219
+ */
220
+ private fallbackParallel;
221
+ /**
222
+ * Check circuit breaker state.
223
+ */
224
+ private checkCircuitBreaker;
225
+ /**
226
+ * Get list of available backends.
227
+ */
228
+ private getAvailableBackends;
229
+ /**
230
+ * Check if backend is available.
231
+ */
232
+ private isBackendAvailable;
233
+ /**
234
+ * Routing: explicit backend selection.
235
+ */
236
+ private routeExplicit;
237
+ /**
238
+ * Routing: model-based selection.
239
+ */
240
+ private routeByModel;
241
+ /**
242
+ * Routing: cost-optimized selection.
243
+ */
244
+ private routeByCost;
245
+ /**
246
+ * Routing: latency-optimized selection.
247
+ */
248
+ private routeByLatency;
249
+ /**
250
+ * Routing: round-robin selection.
251
+ */
252
+ private routeRoundRobin;
253
+ /**
254
+ * Routing: random selection.
255
+ */
256
+ private routeRandom;
257
+ /**
258
+ * Select backend based on capability requirements.
259
+ * Returns the backend name with the best matching model, or null if none match.
260
+ */
261
+ private selectBackendByCapabilities;
262
+ /**
263
+ * Calculate backend statistics.
264
+ */
265
+ private calculateBackendStats;
266
+ /**
267
+ * Create backend info from state.
268
+ */
269
+ private createBackendInfo;
270
+ /**
271
+ * Start periodic health checking.
272
+ */
273
+ private startHealthChecking;
274
+ /**
275
+ * Combine multiple abort signals.
276
+ */
277
+ private combineSignals;
278
+ /**
279
+ * Wrap unknown error as AdapterError.
280
+ */
281
+ private wrapError;
282
+ }
283
+ /**
284
+ * Create a new Router instance.
285
+ */
286
+ export declare function createRouter(config?: Partial<RouterConfig>): Router;
287
+ //# sourceMappingURL=router.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../../src/router.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACtE,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,YAAY,EAAiB,MAAM,gBAAgB,CAAC;AACjG,OAAO,KAAK,EACV,MAAM,IAAI,OAAO,EACjB,YAAY,EACZ,WAAW,EACX,YAAY,EACZ,WAAW,EAEX,YAAY,EACZ,mBAAmB,EACnB,uBAAuB,EACvB,sBAAsB,EACvB,MAAM,gBAAgB,CAAC;AAiCxB;;GAEG;AACH,qBAAa,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B;IAiB3B;;OAEG;IACH,OAAO,CAAC,cAAc;IActB;;OAEG;IACH,OAAO,CAAC,SAAS;CAalB;AAMD;;GAEG;AACH,wBAAgB,YAAY,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,YAAY,CAAC,GAAG,MAAM,CAEnE"}
package/package.json ADDED
@@ -0,0 +1,70 @@
1
+ {
2
+ "name": "ai.matey.core",
3
+ "version": "0.2.0",
4
+ "description": "Core components for AI Matey - Universal AI Adapter System",
5
+ "type": "module",
6
+ "main": "./dist/cjs/index.js",
7
+ "module": "./dist/esm/index.js",
8
+ "types": "./dist/types/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": {
12
+ "types": "./dist/types/index.d.ts",
13
+ "default": "./dist/esm/index.js"
14
+ },
15
+ "require": {
16
+ "types": "./dist/types/index.d.ts",
17
+ "default": "./dist/cjs/index.js"
18
+ }
19
+ }
20
+ },
21
+ "files": [
22
+ "dist",
23
+ "readme.md",
24
+ "CHANGELOG.md",
25
+ "LICENSE"
26
+ ],
27
+ "scripts": {
28
+ "build": "npm run build:esm && npm run build:cjs && npm run build:types",
29
+ "build:esm": "tsc -p tsconfig.esm.json",
30
+ "build:cjs": "tsc -p tsconfig.cjs.json",
31
+ "build:types": "tsc -p tsconfig.types.json",
32
+ "clean": "rm -rf dist",
33
+ "typecheck": "tsc --noEmit",
34
+ "lint": "eslint src --ext .ts",
35
+ "lint:fix": "eslint src --ext .ts --fix",
36
+ "test": "vitest run",
37
+ "test:watch": "vitest"
38
+ },
39
+ "dependencies": {
40
+ "ai.matey.types": "*",
41
+ "ai.matey.errors": "*",
42
+ "ai.matey.utils": "*"
43
+ },
44
+ "devDependencies": {
45
+ "typescript": "^5.9.3",
46
+ "vitest": "^3.2.4"
47
+ },
48
+ "keywords": [
49
+ "ai",
50
+ "llm",
51
+ "core",
52
+ "router",
53
+ "bridge",
54
+ "ai-matey"
55
+ ],
56
+ "author": "AI Matey",
57
+ "license": "MIT",
58
+ "homepage": "https://github.com/johnhenry/ai.matey#readme",
59
+ "bugs": {
60
+ "url": "https://github.com/johnhenry/ai.matey/issues"
61
+ },
62
+ "repository": {
63
+ "type": "git",
64
+ "url": "git+https://github.com/johnhenry/ai.matey.git",
65
+ "directory": "packages/ai.matey.core"
66
+ },
67
+ "engines": {
68
+ "node": ">=18.0.0"
69
+ }
70
+ }
package/readme.md ADDED
@@ -0,0 +1,34 @@
1
+ # ai.matey.core
2
+
3
+ Core Bridge, Router, and MiddlewareStack implementations
4
+
5
+ Part of the [ai.matey](https://github.com/johnhenry/ai.matey) monorepo.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install ai.matey.core
11
+ ```
12
+
13
+ ## Exports
14
+
15
+ - `Bridge`
16
+ - `createBridge`
17
+ - `Router`
18
+ - `createRouter`
19
+ - `MiddlewareStack`
20
+ - `createMiddlewareContext`
21
+
22
+ ## Usage
23
+
24
+ ```typescript
25
+ import { Bridge, createBridge, Router, createRouter, MiddlewareStack, createMiddlewareContext } from 'ai.matey.core';
26
+ ```
27
+
28
+ ## API Reference
29
+
30
+ See the TypeScript definitions for detailed API documentation.
31
+
32
+ ## License
33
+
34
+ MIT - see [LICENSE](./LICENSE) for details.