@juspay/neurolink 7.24.1 → 7.26.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/CHANGELOG.md +12 -0
- package/dist/core/baseProvider.d.ts +13 -0
- package/dist/core/baseProvider.js +121 -1
- package/dist/core/evaluationProviders.d.ts +26 -0
- package/dist/core/evaluationProviders.js +160 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2 -0
- package/dist/lib/core/baseProvider.d.ts +13 -0
- package/dist/lib/core/baseProvider.js +121 -1
- package/dist/lib/core/evaluationProviders.d.ts +26 -0
- package/dist/lib/core/evaluationProviders.js +160 -0
- package/dist/lib/index.d.ts +3 -0
- package/dist/lib/index.js +2 -0
- package/dist/lib/middleware/builtin/analytics.d.ts +16 -0
- package/dist/lib/middleware/builtin/analytics.js +130 -0
- package/dist/lib/middleware/factory.d.ts +54 -0
- package/dist/lib/middleware/factory.js +289 -0
- package/dist/lib/middleware/index.d.ts +58 -0
- package/dist/lib/middleware/index.js +67 -0
- package/dist/lib/middleware/registry.d.ts +78 -0
- package/dist/lib/middleware/registry.js +283 -0
- package/dist/lib/middleware/types.d.ts +144 -0
- package/dist/lib/middleware/types.js +1 -0
- package/dist/middleware/builtin/analytics.d.ts +16 -0
- package/dist/middleware/builtin/analytics.js +130 -0
- package/dist/middleware/factory.d.ts +54 -0
- package/dist/middleware/factory.js +289 -0
- package/dist/middleware/index.d.ts +58 -0
- package/dist/middleware/index.js +67 -0
- package/dist/middleware/registry.d.ts +78 -0
- package/dist/middleware/registry.js +283 -0
- package/dist/middleware/types.d.ts +144 -0
- package/dist/middleware/types.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NeuroLink Middleware System
|
|
3
|
+
*
|
|
4
|
+
* This module provides a comprehensive middleware system for NeuroLink that integrates
|
|
5
|
+
* with the AI SDK's wrapLanguageModel functionality. It allows for modular enhancement
|
|
6
|
+
* of language models with features like analytics, guardrails, caching, and more.
|
|
7
|
+
*/
|
|
8
|
+
import type { NeuroLinkMiddleware, MiddlewareRegistrationOptions, MiddlewareConfig } from "./types.js";
|
|
9
|
+
export type { NeuroLinkMiddleware, MiddlewareConfig, MiddlewareContext, MiddlewareConditions, MiddlewareRegistrationOptions, MiddlewareExecutionResult, MiddlewareChainStats, MiddlewarePreset, MiddlewareFactoryOptions, BuiltInMiddlewareType, } from "./types.js";
|
|
10
|
+
export type { LanguageModelV1Middleware } from "ai";
|
|
11
|
+
export { MiddlewareRegistry, middlewareRegistry } from "./registry.js";
|
|
12
|
+
export { MiddlewareFactory } from "./factory.js";
|
|
13
|
+
/**
|
|
14
|
+
* Convenience function to register a middleware
|
|
15
|
+
*/
|
|
16
|
+
export declare function registerMiddleware(middleware: NeuroLinkMiddleware, options?: MiddlewareRegistrationOptions): void;
|
|
17
|
+
/**
|
|
18
|
+
* Convenience function to unregister a middleware
|
|
19
|
+
*/
|
|
20
|
+
export declare function unregisterMiddleware(middlewareId: string): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Convenience function to get all registered middleware
|
|
23
|
+
*/
|
|
24
|
+
export declare function listMiddleware(): NeuroLinkMiddleware[];
|
|
25
|
+
/**
|
|
26
|
+
* Convenience function to check if a middleware is registered
|
|
27
|
+
*/
|
|
28
|
+
export declare function hasMiddleware(middlewareId: string): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Convenience function to get middleware execution statistics
|
|
31
|
+
*/
|
|
32
|
+
export declare function getMiddlewareStats(): Record<string, {
|
|
33
|
+
totalExecutions: number;
|
|
34
|
+
successfulExecutions: number;
|
|
35
|
+
failedExecutions: number;
|
|
36
|
+
averageExecutionTime: number;
|
|
37
|
+
lastExecutionTime: number;
|
|
38
|
+
}>;
|
|
39
|
+
/**
|
|
40
|
+
* Convenience function to clear middleware execution statistics
|
|
41
|
+
*/
|
|
42
|
+
export declare function clearMiddlewareStats(middlewareId?: string): void;
|
|
43
|
+
/**
|
|
44
|
+
* Convenience function to get available middleware presets
|
|
45
|
+
*/
|
|
46
|
+
export declare function getAvailablePresets(): Array<{
|
|
47
|
+
name: string;
|
|
48
|
+
description: string;
|
|
49
|
+
middleware: string[];
|
|
50
|
+
}>;
|
|
51
|
+
/**
|
|
52
|
+
* Convenience function to validate middleware configuration
|
|
53
|
+
*/
|
|
54
|
+
export declare function validateMiddlewareConfig(config: Record<string, MiddlewareConfig>): {
|
|
55
|
+
isValid: boolean;
|
|
56
|
+
errors: string[];
|
|
57
|
+
warnings: string[];
|
|
58
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NeuroLink Middleware System
|
|
3
|
+
*
|
|
4
|
+
* This module provides a comprehensive middleware system for NeuroLink that integrates
|
|
5
|
+
* with the AI SDK's wrapLanguageModel functionality. It allows for modular enhancement
|
|
6
|
+
* of language models with features like analytics, guardrails, caching, and more.
|
|
7
|
+
*/
|
|
8
|
+
import { middlewareRegistry } from "./registry.js";
|
|
9
|
+
import { MiddlewareFactory } from "./factory.js";
|
|
10
|
+
// Registry for managing middleware
|
|
11
|
+
export { MiddlewareRegistry, middlewareRegistry } from "./registry.js";
|
|
12
|
+
// Factory for creating and applying middleware chains
|
|
13
|
+
export { MiddlewareFactory } from "./factory.js";
|
|
14
|
+
// Re-export built-in middleware when they're implemented
|
|
15
|
+
// export { analyticsMiddleware } from './built-in/analytics.js';
|
|
16
|
+
// export { guardrailsMiddleware } from './built-in/guardrails.js';
|
|
17
|
+
// export { loggingMiddleware } from './built-in/logging.js';
|
|
18
|
+
// export { cachingMiddleware } from './built-in/caching.js';
|
|
19
|
+
// export { rateLimitMiddleware } from './built-in/rateLimit.js';
|
|
20
|
+
/**
|
|
21
|
+
* Convenience function to register a middleware
|
|
22
|
+
*/
|
|
23
|
+
export function registerMiddleware(middleware, options) {
|
|
24
|
+
middlewareRegistry.register(middleware, options);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Convenience function to unregister a middleware
|
|
28
|
+
*/
|
|
29
|
+
export function unregisterMiddleware(middlewareId) {
|
|
30
|
+
return middlewareRegistry.unregister(middlewareId);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Convenience function to get all registered middleware
|
|
34
|
+
*/
|
|
35
|
+
export function listMiddleware() {
|
|
36
|
+
return middlewareRegistry.list();
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Convenience function to check if a middleware is registered
|
|
40
|
+
*/
|
|
41
|
+
export function hasMiddleware(middlewareId) {
|
|
42
|
+
return middlewareRegistry.has(middlewareId);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Convenience function to get middleware execution statistics
|
|
46
|
+
*/
|
|
47
|
+
export function getMiddlewareStats() {
|
|
48
|
+
return middlewareRegistry.getAggregatedStats();
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Convenience function to clear middleware execution statistics
|
|
52
|
+
*/
|
|
53
|
+
export function clearMiddlewareStats(middlewareId) {
|
|
54
|
+
middlewareRegistry.clearStats(middlewareId);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Convenience function to get available middleware presets
|
|
58
|
+
*/
|
|
59
|
+
export function getAvailablePresets() {
|
|
60
|
+
return MiddlewareFactory.getAvailablePresets();
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Convenience function to validate middleware configuration
|
|
64
|
+
*/
|
|
65
|
+
export function validateMiddlewareConfig(config) {
|
|
66
|
+
return MiddlewareFactory.validateConfig(config);
|
|
67
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { LanguageModelV1Middleware } from "ai";
|
|
2
|
+
import type { NeuroLinkMiddleware, MiddlewareConfig, MiddlewareContext, MiddlewareRegistrationOptions, MiddlewareExecutionResult } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Global middleware registry for NeuroLink
|
|
5
|
+
* Manages registration, configuration, and execution of middleware
|
|
6
|
+
*/
|
|
7
|
+
export declare class MiddlewareRegistry {
|
|
8
|
+
private middleware;
|
|
9
|
+
private globalConfigs;
|
|
10
|
+
private executionStats;
|
|
11
|
+
/**
|
|
12
|
+
* Register a middleware
|
|
13
|
+
*/
|
|
14
|
+
register(middleware: NeuroLinkMiddleware, options?: MiddlewareRegistrationOptions): void;
|
|
15
|
+
/**
|
|
16
|
+
* Unregister a middleware
|
|
17
|
+
*/
|
|
18
|
+
unregister(middlewareId: string): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Get a registered middleware
|
|
21
|
+
*/
|
|
22
|
+
get(middlewareId: string): NeuroLinkMiddleware | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* List all registered middleware
|
|
25
|
+
*/
|
|
26
|
+
list(): NeuroLinkMiddleware[];
|
|
27
|
+
/**
|
|
28
|
+
* Get middleware IDs sorted by priority
|
|
29
|
+
*/
|
|
30
|
+
getSortedIds(): string[];
|
|
31
|
+
/**
|
|
32
|
+
* Build middleware chain based on configuration
|
|
33
|
+
*/
|
|
34
|
+
buildChain(context: MiddlewareContext, config?: Record<string, MiddlewareConfig>): LanguageModelV1Middleware[];
|
|
35
|
+
/**
|
|
36
|
+
* Determine if middleware should be applied based on conditions
|
|
37
|
+
*/
|
|
38
|
+
private shouldApplyMiddleware;
|
|
39
|
+
/**
|
|
40
|
+
* Configure middleware with runtime configuration
|
|
41
|
+
*/
|
|
42
|
+
private configureMiddleware;
|
|
43
|
+
/**
|
|
44
|
+
* Record middleware execution statistics
|
|
45
|
+
*/
|
|
46
|
+
private recordExecution;
|
|
47
|
+
/**
|
|
48
|
+
* Get execution statistics for a middleware
|
|
49
|
+
*/
|
|
50
|
+
getExecutionStats(middlewareId: string): MiddlewareExecutionResult[];
|
|
51
|
+
/**
|
|
52
|
+
* Get aggregated statistics for all middleware
|
|
53
|
+
*/
|
|
54
|
+
getAggregatedStats(): Record<string, {
|
|
55
|
+
totalExecutions: number;
|
|
56
|
+
successfulExecutions: number;
|
|
57
|
+
failedExecutions: number;
|
|
58
|
+
averageExecutionTime: number;
|
|
59
|
+
lastExecutionTime: number;
|
|
60
|
+
}>;
|
|
61
|
+
/**
|
|
62
|
+
* Clear execution statistics
|
|
63
|
+
*/
|
|
64
|
+
clearStats(middlewareId?: string): void;
|
|
65
|
+
/**
|
|
66
|
+
* Check if a middleware is registered
|
|
67
|
+
*/
|
|
68
|
+
has(middlewareId: string): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Get the number of registered middleware
|
|
71
|
+
*/
|
|
72
|
+
size(): number;
|
|
73
|
+
/**
|
|
74
|
+
* Clear all registered middleware
|
|
75
|
+
*/
|
|
76
|
+
clear(): void;
|
|
77
|
+
}
|
|
78
|
+
export declare const middlewareRegistry: MiddlewareRegistry;
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import { logger } from "../utils/logger.js";
|
|
2
|
+
/**
|
|
3
|
+
* Global middleware registry for NeuroLink
|
|
4
|
+
* Manages registration, configuration, and execution of middleware
|
|
5
|
+
*/
|
|
6
|
+
export class MiddlewareRegistry {
|
|
7
|
+
middleware = new Map();
|
|
8
|
+
globalConfigs = new Map();
|
|
9
|
+
executionStats = new Map();
|
|
10
|
+
/**
|
|
11
|
+
* Register a middleware
|
|
12
|
+
*/
|
|
13
|
+
register(middleware, options = {}) {
|
|
14
|
+
const { replace = false, defaultEnabled = false, globalConfig } = options;
|
|
15
|
+
// Check if middleware already exists
|
|
16
|
+
if (this.middleware.has(middleware.metadata.id) && !replace) {
|
|
17
|
+
throw new Error(`Middleware with ID '${middleware.metadata.id}' already exists. Use replace: true to override.`);
|
|
18
|
+
}
|
|
19
|
+
// Set default enabled state
|
|
20
|
+
if (middleware.metadata.defaultEnabled === undefined) {
|
|
21
|
+
middleware.metadata.defaultEnabled = defaultEnabled;
|
|
22
|
+
}
|
|
23
|
+
// Store middleware
|
|
24
|
+
this.middleware.set(middleware.metadata.id, middleware);
|
|
25
|
+
// Store global configuration if provided
|
|
26
|
+
if (globalConfig) {
|
|
27
|
+
this.globalConfigs.set(middleware.metadata.id, globalConfig);
|
|
28
|
+
}
|
|
29
|
+
logger.debug(`Middleware registered: ${middleware.metadata.id}`, {
|
|
30
|
+
name: middleware.metadata.name,
|
|
31
|
+
priority: middleware.metadata.priority || 0,
|
|
32
|
+
defaultEnabled: middleware.metadata.defaultEnabled,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Unregister a middleware
|
|
37
|
+
*/
|
|
38
|
+
unregister(middlewareId) {
|
|
39
|
+
const removed = this.middleware.delete(middlewareId);
|
|
40
|
+
this.globalConfigs.delete(middlewareId);
|
|
41
|
+
this.executionStats.delete(middlewareId);
|
|
42
|
+
if (removed) {
|
|
43
|
+
logger.debug(`Middleware unregistered: ${middlewareId}`);
|
|
44
|
+
}
|
|
45
|
+
return removed;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Get a registered middleware
|
|
49
|
+
*/
|
|
50
|
+
get(middlewareId) {
|
|
51
|
+
return this.middleware.get(middlewareId);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* List all registered middleware
|
|
55
|
+
*/
|
|
56
|
+
list() {
|
|
57
|
+
return Array.from(this.middleware.values());
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Get middleware IDs sorted by priority
|
|
61
|
+
*/
|
|
62
|
+
getSortedIds() {
|
|
63
|
+
return Array.from(this.middleware.values())
|
|
64
|
+
.sort((a, b) => (b.metadata.priority || 0) - (a.metadata.priority || 0))
|
|
65
|
+
.map((m) => m.metadata.id);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Build middleware chain based on configuration
|
|
69
|
+
*/
|
|
70
|
+
buildChain(context, config = {}) {
|
|
71
|
+
const chain = [];
|
|
72
|
+
const sortedIds = this.getSortedIds();
|
|
73
|
+
for (const middlewareId of sortedIds) {
|
|
74
|
+
const middleware = this.middleware.get(middlewareId);
|
|
75
|
+
if (!middleware) {
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const middlewareConfig = config[middlewareId];
|
|
79
|
+
const globalConfig = this.globalConfigs.get(middlewareId);
|
|
80
|
+
// Determine if middleware should be applied
|
|
81
|
+
const shouldApply = this.shouldApplyMiddleware(middleware, middlewareConfig, context);
|
|
82
|
+
if (shouldApply) {
|
|
83
|
+
// Create configured middleware instance
|
|
84
|
+
const configuredMiddleware = this.configureMiddleware(middleware, middlewareConfig, globalConfig, context);
|
|
85
|
+
chain.push(configuredMiddleware);
|
|
86
|
+
logger.debug(`Added middleware to chain: ${middlewareId}`, {
|
|
87
|
+
priority: middleware.metadata.priority || 0,
|
|
88
|
+
chainLength: chain.length,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return chain;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Determine if middleware should be applied based on conditions
|
|
96
|
+
*/
|
|
97
|
+
shouldApplyMiddleware(middleware, config, context) {
|
|
98
|
+
// Check if explicitly disabled
|
|
99
|
+
if (config?.enabled === false) {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
// Check if explicitly enabled or default enabled
|
|
103
|
+
const isEnabled = config?.enabled === true ||
|
|
104
|
+
(config?.enabled === undefined && middleware.metadata.defaultEnabled);
|
|
105
|
+
if (!isEnabled) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
// Check conditions
|
|
109
|
+
const conditions = config?.conditions;
|
|
110
|
+
if (!conditions) {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
// Check provider conditions
|
|
114
|
+
if (conditions.providers &&
|
|
115
|
+
!conditions.providers.includes(context.provider)) {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
// Check model conditions
|
|
119
|
+
if (conditions.models && !conditions.models.includes(context.model)) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
// Check option conditions
|
|
123
|
+
if (conditions.options) {
|
|
124
|
+
for (const [key, value] of Object.entries(conditions.options)) {
|
|
125
|
+
if (context.options[key] !== value) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
// Check custom condition
|
|
131
|
+
if (conditions.custom && !conditions.custom(context)) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Configure middleware with runtime configuration
|
|
138
|
+
*/
|
|
139
|
+
configureMiddleware(middleware, config, globalConfig, context) {
|
|
140
|
+
// Merge configurations: global < middleware config < runtime config
|
|
141
|
+
const mergedConfig = {
|
|
142
|
+
...globalConfig,
|
|
143
|
+
...config?.config,
|
|
144
|
+
};
|
|
145
|
+
// Create wrapper that tracks execution
|
|
146
|
+
const wrappedMiddleware = {};
|
|
147
|
+
if (middleware.transformParams) {
|
|
148
|
+
wrappedMiddleware.transformParams = async (args) => {
|
|
149
|
+
const startTime = Date.now();
|
|
150
|
+
try {
|
|
151
|
+
const result = await middleware.transformParams(args);
|
|
152
|
+
this.recordExecution(middleware.metadata.id, startTime, true);
|
|
153
|
+
return result;
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
this.recordExecution(middleware.metadata.id, startTime, false, error);
|
|
157
|
+
throw error;
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
if (middleware.wrapGenerate) {
|
|
162
|
+
wrappedMiddleware.wrapGenerate = async (args) => {
|
|
163
|
+
const startTime = Date.now();
|
|
164
|
+
try {
|
|
165
|
+
const result = await middleware.wrapGenerate(args);
|
|
166
|
+
this.recordExecution(middleware.metadata.id, startTime, true);
|
|
167
|
+
return result;
|
|
168
|
+
}
|
|
169
|
+
catch (error) {
|
|
170
|
+
this.recordExecution(middleware.metadata.id, startTime, false, error);
|
|
171
|
+
throw error;
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
if (middleware.wrapStream) {
|
|
176
|
+
wrappedMiddleware.wrapStream = async (args) => {
|
|
177
|
+
const startTime = Date.now();
|
|
178
|
+
try {
|
|
179
|
+
const result = await middleware.wrapStream(args);
|
|
180
|
+
this.recordExecution(middleware.metadata.id, startTime, true);
|
|
181
|
+
return result;
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
this.recordExecution(middleware.metadata.id, startTime, false, error);
|
|
185
|
+
throw error;
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
return wrappedMiddleware;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Record middleware execution statistics
|
|
193
|
+
*/
|
|
194
|
+
recordExecution(middlewareId, startTime, success, error) {
|
|
195
|
+
const executionTime = Date.now() - startTime;
|
|
196
|
+
const result = {
|
|
197
|
+
applied: true,
|
|
198
|
+
executionTime,
|
|
199
|
+
error,
|
|
200
|
+
};
|
|
201
|
+
if (!this.executionStats.has(middlewareId)) {
|
|
202
|
+
this.executionStats.set(middlewareId, []);
|
|
203
|
+
}
|
|
204
|
+
const stats = this.executionStats.get(middlewareId) || [];
|
|
205
|
+
stats.push(result);
|
|
206
|
+
// Keep only last 100 executions per middleware
|
|
207
|
+
if (stats.length > 100) {
|
|
208
|
+
stats.shift();
|
|
209
|
+
}
|
|
210
|
+
if (error) {
|
|
211
|
+
logger.warn(`Middleware execution failed: ${middlewareId}`, {
|
|
212
|
+
executionTime,
|
|
213
|
+
error: error.message,
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
logger.debug(`Middleware executed: ${middlewareId}`, {
|
|
218
|
+
executionTime,
|
|
219
|
+
success,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Get execution statistics for a middleware
|
|
225
|
+
*/
|
|
226
|
+
getExecutionStats(middlewareId) {
|
|
227
|
+
return this.executionStats.get(middlewareId) || [];
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Get aggregated statistics for all middleware
|
|
231
|
+
*/
|
|
232
|
+
getAggregatedStats() {
|
|
233
|
+
const stats = {};
|
|
234
|
+
for (const [middlewareId, executions] of this.executionStats.entries()) {
|
|
235
|
+
const successful = executions.filter((e) => !e.error).length;
|
|
236
|
+
const failed = executions.filter((e) => e.error).length;
|
|
237
|
+
const totalTime = executions.reduce((sum, e) => sum + e.executionTime, 0);
|
|
238
|
+
const lastExecution = executions[executions.length - 1];
|
|
239
|
+
stats[middlewareId] = {
|
|
240
|
+
totalExecutions: executions.length,
|
|
241
|
+
successfulExecutions: successful,
|
|
242
|
+
failedExecutions: failed,
|
|
243
|
+
averageExecutionTime: executions.length > 0 ? totalTime / executions.length : 0,
|
|
244
|
+
lastExecutionTime: lastExecution?.executionTime || 0,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
return stats;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Clear execution statistics
|
|
251
|
+
*/
|
|
252
|
+
clearStats(middlewareId) {
|
|
253
|
+
if (middlewareId) {
|
|
254
|
+
this.executionStats.delete(middlewareId);
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
this.executionStats.clear();
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Check if a middleware is registered
|
|
262
|
+
*/
|
|
263
|
+
has(middlewareId) {
|
|
264
|
+
return this.middleware.has(middlewareId);
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Get the number of registered middleware
|
|
268
|
+
*/
|
|
269
|
+
size() {
|
|
270
|
+
return this.middleware.size;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Clear all registered middleware
|
|
274
|
+
*/
|
|
275
|
+
clear() {
|
|
276
|
+
this.middleware.clear();
|
|
277
|
+
this.globalConfigs.clear();
|
|
278
|
+
this.executionStats.clear();
|
|
279
|
+
logger.debug("All middleware cleared from registry");
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
// Global middleware registry instance
|
|
283
|
+
export const middlewareRegistry = new MiddlewareRegistry();
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import type { LanguageModelV1Middleware } from "ai";
|
|
2
|
+
import type { JsonValue } from "../types/common.js";
|
|
3
|
+
/**
|
|
4
|
+
* Metadata interface for NeuroLink middleware
|
|
5
|
+
* Provides additional information about middleware without affecting execution
|
|
6
|
+
*/
|
|
7
|
+
export interface NeuroLinkMiddlewareMetadata {
|
|
8
|
+
/** Unique identifier for the middleware */
|
|
9
|
+
id: string;
|
|
10
|
+
/** Human-readable name */
|
|
11
|
+
name: string;
|
|
12
|
+
/** Description of what the middleware does */
|
|
13
|
+
description?: string;
|
|
14
|
+
/** Priority for ordering (higher = earlier in chain) */
|
|
15
|
+
priority?: number;
|
|
16
|
+
/** Whether this middleware is enabled by default */
|
|
17
|
+
defaultEnabled?: boolean;
|
|
18
|
+
/** Configuration schema for the middleware */
|
|
19
|
+
configSchema?: Record<string, unknown>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* NeuroLink middleware with metadata
|
|
23
|
+
* Combines standard AI SDK middleware with NeuroLink-specific metadata
|
|
24
|
+
*/
|
|
25
|
+
export interface NeuroLinkMiddleware extends LanguageModelV1Middleware {
|
|
26
|
+
/** Middleware metadata */
|
|
27
|
+
readonly metadata: NeuroLinkMiddlewareMetadata;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Middleware configuration options
|
|
31
|
+
*/
|
|
32
|
+
export interface MiddlewareConfig {
|
|
33
|
+
/** Whether the middleware is enabled */
|
|
34
|
+
enabled?: boolean;
|
|
35
|
+
/** Middleware-specific configuration */
|
|
36
|
+
config?: Record<string, JsonValue>;
|
|
37
|
+
/** Conditions under which to apply this middleware */
|
|
38
|
+
conditions?: MiddlewareConditions;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Conditions for applying middleware
|
|
42
|
+
*/
|
|
43
|
+
export interface MiddlewareConditions {
|
|
44
|
+
/** Apply only to specific providers */
|
|
45
|
+
providers?: string[];
|
|
46
|
+
/** Apply only to specific models */
|
|
47
|
+
models?: string[];
|
|
48
|
+
/** Apply only when certain options are present */
|
|
49
|
+
options?: Record<string, unknown>;
|
|
50
|
+
/** Custom condition function */
|
|
51
|
+
custom?: (context: MiddlewareContext) => boolean;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Context passed to middleware for decision making
|
|
55
|
+
*/
|
|
56
|
+
export interface MiddlewareContext {
|
|
57
|
+
/** Provider name */
|
|
58
|
+
provider: string;
|
|
59
|
+
/** Model name */
|
|
60
|
+
model: string;
|
|
61
|
+
/** Request options */
|
|
62
|
+
options: Record<string, unknown>;
|
|
63
|
+
/** Session information */
|
|
64
|
+
session?: {
|
|
65
|
+
sessionId?: string;
|
|
66
|
+
userId?: string;
|
|
67
|
+
};
|
|
68
|
+
/** Additional metadata */
|
|
69
|
+
metadata?: Record<string, JsonValue>;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Middleware registration options
|
|
73
|
+
*/
|
|
74
|
+
export interface MiddlewareRegistrationOptions {
|
|
75
|
+
/** Whether to replace existing middleware with same ID */
|
|
76
|
+
replace?: boolean;
|
|
77
|
+
/** Whether to enable the middleware by default */
|
|
78
|
+
defaultEnabled?: boolean;
|
|
79
|
+
/** Global configuration for the middleware */
|
|
80
|
+
globalConfig?: Record<string, JsonValue>;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Middleware execution result
|
|
84
|
+
*/
|
|
85
|
+
export interface MiddlewareExecutionResult {
|
|
86
|
+
/** Whether the middleware was applied */
|
|
87
|
+
applied: boolean;
|
|
88
|
+
/** Execution time in milliseconds */
|
|
89
|
+
executionTime: number;
|
|
90
|
+
/** Any errors that occurred */
|
|
91
|
+
error?: Error;
|
|
92
|
+
/** Additional metadata from the middleware */
|
|
93
|
+
metadata?: Record<string, JsonValue>;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Middleware chain execution statistics
|
|
97
|
+
*/
|
|
98
|
+
export interface MiddlewareChainStats {
|
|
99
|
+
/** Total number of middleware in the chain */
|
|
100
|
+
totalMiddleware: number;
|
|
101
|
+
/** Number of middleware that were applied */
|
|
102
|
+
appliedMiddleware: number;
|
|
103
|
+
/** Total execution time for the chain */
|
|
104
|
+
totalExecutionTime: number;
|
|
105
|
+
/** Individual middleware execution results */
|
|
106
|
+
results: Record<string, MiddlewareExecutionResult>;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Built-in middleware types
|
|
110
|
+
*/
|
|
111
|
+
export type BuiltInMiddlewareType = "analytics" | "guardrails" | "logging" | "caching" | "rateLimit" | "retry" | "timeout";
|
|
112
|
+
/**
|
|
113
|
+
* Middleware preset configurations
|
|
114
|
+
*/
|
|
115
|
+
export interface MiddlewarePreset {
|
|
116
|
+
/** Preset name */
|
|
117
|
+
name: string;
|
|
118
|
+
/** Description of the preset */
|
|
119
|
+
description: string;
|
|
120
|
+
/** Middleware configurations in the preset */
|
|
121
|
+
middleware: Record<string, MiddlewareConfig>;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Factory options for middleware
|
|
125
|
+
*/
|
|
126
|
+
export interface MiddlewareFactoryOptions {
|
|
127
|
+
/** Enable specific middleware */
|
|
128
|
+
enabledMiddleware?: string[];
|
|
129
|
+
/** Disable specific middleware */
|
|
130
|
+
disabledMiddleware?: string[];
|
|
131
|
+
/** Middleware configurations */
|
|
132
|
+
middlewareConfig?: Record<string, MiddlewareConfig>;
|
|
133
|
+
/** Use a preset configuration */
|
|
134
|
+
preset?: string;
|
|
135
|
+
/** Global middleware settings */
|
|
136
|
+
global?: {
|
|
137
|
+
/** Maximum execution time for middleware chain */
|
|
138
|
+
maxExecutionTime?: number;
|
|
139
|
+
/** Whether to continue on middleware errors */
|
|
140
|
+
continueOnError?: boolean;
|
|
141
|
+
/** Whether to collect execution statistics */
|
|
142
|
+
collectStats?: boolean;
|
|
143
|
+
};
|
|
144
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { NeuroLinkMiddleware } from "../types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Create analytics middleware for tracking AI model usage
|
|
4
|
+
* Collects metrics on token usage, response times, and model performance
|
|
5
|
+
*/
|
|
6
|
+
export declare function createAnalyticsMiddleware(): NeuroLinkMiddleware;
|
|
7
|
+
/**
|
|
8
|
+
* Get collected metrics from analytics middleware
|
|
9
|
+
* Note: This is a utility function for accessing metrics
|
|
10
|
+
*/
|
|
11
|
+
export declare function getAnalyticsMetrics(): Map<string, Record<string, unknown>>;
|
|
12
|
+
/**
|
|
13
|
+
* Clear collected metrics from analytics middleware
|
|
14
|
+
* Note: This is a utility function for clearing metrics
|
|
15
|
+
*/
|
|
16
|
+
export declare function clearAnalyticsMetrics(): void;
|