ai.matey.middleware 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.
- package/LICENSE +21 -0
- package/dist/cjs/caching.js +226 -0
- package/dist/cjs/caching.js.map +1 -0
- package/dist/cjs/conversation-history.js +213 -0
- package/dist/cjs/conversation-history.js.map +1 -0
- package/dist/cjs/cost-tracking.js +355 -0
- package/dist/cjs/cost-tracking.js.map +1 -0
- package/dist/cjs/index.js +37 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/logging.js +174 -0
- package/dist/cjs/logging.js.map +1 -0
- package/dist/cjs/opentelemetry.js +499 -0
- package/dist/cjs/opentelemetry.js.map +1 -0
- package/dist/cjs/retry.js +205 -0
- package/dist/cjs/retry.js.map +1 -0
- package/dist/cjs/security.js +175 -0
- package/dist/cjs/security.js.map +1 -0
- package/dist/cjs/telemetry.js +216 -0
- package/dist/cjs/telemetry.js.map +1 -0
- package/dist/cjs/transform.js +284 -0
- package/dist/cjs/transform.js.map +1 -0
- package/dist/cjs/validation.js +506 -0
- package/dist/cjs/validation.js.map +1 -0
- package/dist/esm/caching.js +221 -0
- package/dist/esm/caching.js.map +1 -0
- package/dist/esm/conversation-history.js +207 -0
- package/dist/esm/conversation-history.js.map +1 -0
- package/dist/esm/cost-tracking.js +347 -0
- package/dist/esm/cost-tracking.js.map +1 -0
- package/dist/esm/index.js +21 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/logging.js +171 -0
- package/dist/esm/logging.js.map +1 -0
- package/dist/esm/opentelemetry.js +458 -0
- package/dist/esm/opentelemetry.js.map +1 -0
- package/dist/esm/retry.js +198 -0
- package/dist/esm/retry.js.map +1 -0
- package/dist/esm/security.js +169 -0
- package/dist/esm/security.js.map +1 -0
- package/dist/esm/telemetry.js +210 -0
- package/dist/esm/telemetry.js.map +1 -0
- package/dist/esm/transform.js +272 -0
- package/dist/esm/transform.js.map +1 -0
- package/dist/esm/validation.js +494 -0
- package/dist/esm/validation.js.map +1 -0
- package/dist/types/caching.d.ts +98 -0
- package/dist/types/caching.d.ts.map +1 -0
- package/dist/types/conversation-history.d.ts +188 -0
- package/dist/types/conversation-history.d.ts.map +1 -0
- package/dist/types/cost-tracking.d.ts +262 -0
- package/dist/types/cost-tracking.d.ts.map +1 -0
- package/dist/types/index.d.ts +20 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/logging.d.ts +82 -0
- package/dist/types/logging.d.ts.map +1 -0
- package/dist/types/opentelemetry.d.ts +219 -0
- package/dist/types/opentelemetry.d.ts.map +1 -0
- package/dist/types/retry.d.ts +86 -0
- package/dist/types/retry.d.ts.map +1 -0
- package/dist/types/security.d.ts +120 -0
- package/dist/types/security.d.ts.map +1 -0
- package/dist/types/telemetry.d.ts +120 -0
- package/dist/types/telemetry.d.ts.map +1 -0
- package/dist/types/transform.d.ts +184 -0
- package/dist/types/transform.d.ts.map +1 -0
- package/dist/types/validation.d.ts +356 -0
- package/dist/types/validation.d.ts.map +1 -0
- package/package.json +203 -0
- package/readme.md +103 -0
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenTelemetry Middleware
|
|
3
|
+
*
|
|
4
|
+
* Provides distributed tracing and metrics export using OpenTelemetry.
|
|
5
|
+
* OpenTelemetry packages are optional peer dependencies - this middleware
|
|
6
|
+
* will check for their availability at runtime.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
import type { Middleware, TelemetrySink } from 'ai.matey.types';
|
|
11
|
+
/**
|
|
12
|
+
* Batch span processor configuration.
|
|
13
|
+
*/
|
|
14
|
+
export interface BatchSpanProcessorConfig {
|
|
15
|
+
/**
|
|
16
|
+
* Maximum queue size for buffering spans.
|
|
17
|
+
* @default 2048
|
|
18
|
+
*/
|
|
19
|
+
maxQueueSize?: number;
|
|
20
|
+
/**
|
|
21
|
+
* Maximum batch size per export.
|
|
22
|
+
* @default 512
|
|
23
|
+
*/
|
|
24
|
+
maxExportBatchSize?: number;
|
|
25
|
+
/**
|
|
26
|
+
* Scheduled delay (in milliseconds) for batching.
|
|
27
|
+
* @default 5000
|
|
28
|
+
*/
|
|
29
|
+
scheduledDelayMillis?: number;
|
|
30
|
+
/**
|
|
31
|
+
* Export timeout (in milliseconds).
|
|
32
|
+
* @default 30000
|
|
33
|
+
*/
|
|
34
|
+
exportTimeoutMillis?: number;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Configuration for OpenTelemetry middleware.
|
|
38
|
+
*/
|
|
39
|
+
export interface OpenTelemetryConfig {
|
|
40
|
+
/**
|
|
41
|
+
* Service name for OpenTelemetry.
|
|
42
|
+
* @default 'ai-matey'
|
|
43
|
+
*/
|
|
44
|
+
serviceName?: string;
|
|
45
|
+
/**
|
|
46
|
+
* Service version for OpenTelemetry.
|
|
47
|
+
* @default '0.1.0'
|
|
48
|
+
*/
|
|
49
|
+
serviceVersion?: string;
|
|
50
|
+
/**
|
|
51
|
+
* OTLP endpoint URL.
|
|
52
|
+
* @default 'http://localhost:4318/v1/traces'
|
|
53
|
+
*/
|
|
54
|
+
endpoint?: string;
|
|
55
|
+
/**
|
|
56
|
+
* Custom headers for OTLP export.
|
|
57
|
+
*/
|
|
58
|
+
headers?: Record<string, string>;
|
|
59
|
+
/**
|
|
60
|
+
* Sampling rate (0.0 to 1.0).
|
|
61
|
+
* @default 1.0 (100%)
|
|
62
|
+
*/
|
|
63
|
+
samplingRate?: number;
|
|
64
|
+
/**
|
|
65
|
+
* Custom resource attributes.
|
|
66
|
+
*/
|
|
67
|
+
resourceAttributes?: Record<string, string>;
|
|
68
|
+
/**
|
|
69
|
+
* Whether to export spans.
|
|
70
|
+
* @default true
|
|
71
|
+
*/
|
|
72
|
+
exportSpans?: boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Custom tracer name.
|
|
75
|
+
* @default 'ai-matey-tracer'
|
|
76
|
+
*/
|
|
77
|
+
tracerName?: string;
|
|
78
|
+
/**
|
|
79
|
+
* Batch span processor configuration.
|
|
80
|
+
*/
|
|
81
|
+
batchSpanProcessorConfig?: BatchSpanProcessorConfig;
|
|
82
|
+
/**
|
|
83
|
+
* OTLP exporter timeout (in milliseconds).
|
|
84
|
+
* @default 10000
|
|
85
|
+
*/
|
|
86
|
+
exporterTimeoutMillis?: number;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* OpenTelemetry span attribute names.
|
|
90
|
+
*/
|
|
91
|
+
export declare const OpenTelemetryAttributes: {
|
|
92
|
+
readonly REQUEST_ID: "ai.request.id";
|
|
93
|
+
readonly REQUEST_MODEL: "ai.request.model";
|
|
94
|
+
readonly REQUEST_STREAM: "ai.request.stream";
|
|
95
|
+
readonly REQUEST_MESSAGE_COUNT: "ai.request.message_count";
|
|
96
|
+
readonly REQUEST_MAX_TOKENS: "ai.request.max_tokens";
|
|
97
|
+
readonly RESPONSE_BACKEND: "ai.response.backend";
|
|
98
|
+
readonly RESPONSE_FINISH_REASON: "ai.response.finish_reason";
|
|
99
|
+
readonly RESPONSE_MODEL: "ai.response.model";
|
|
100
|
+
readonly TOKENS_PROMPT: "ai.tokens.prompt";
|
|
101
|
+
readonly TOKENS_COMPLETION: "ai.tokens.completion";
|
|
102
|
+
readonly TOKENS_TOTAL: "ai.tokens.total";
|
|
103
|
+
readonly FRONTEND: "ai.frontend";
|
|
104
|
+
readonly BACKEND: "ai.backend";
|
|
105
|
+
readonly DURATION_MS: "ai.duration.ms";
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Create OpenTelemetry middleware.
|
|
109
|
+
*
|
|
110
|
+
* Provides distributed tracing with span creation, context propagation,
|
|
111
|
+
* and metrics export via OpenTelemetry.
|
|
112
|
+
*
|
|
113
|
+
* **Note:** This middleware requires optional OpenTelemetry packages to be installed:
|
|
114
|
+
* - `@opentelemetry/api`
|
|
115
|
+
* - `@opentelemetry/sdk-trace-base`
|
|
116
|
+
* - `@opentelemetry/exporter-trace-otlp-http`
|
|
117
|
+
* - `@opentelemetry/resources`
|
|
118
|
+
* - `@opentelemetry/semantic-conventions`
|
|
119
|
+
*
|
|
120
|
+
* Install with:
|
|
121
|
+
* ```bash
|
|
122
|
+
* npm install @opentelemetry/api @opentelemetry/sdk-trace-base \
|
|
123
|
+
* @opentelemetry/exporter-trace-otlp-http @opentelemetry/resources \
|
|
124
|
+
* @opentelemetry/semantic-conventions
|
|
125
|
+
* ```
|
|
126
|
+
*
|
|
127
|
+
* @param config OpenTelemetry configuration
|
|
128
|
+
* @returns Promise that resolves to OpenTelemetry middleware
|
|
129
|
+
* @throws Error if OpenTelemetry packages are not installed
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```typescript
|
|
133
|
+
* import { createOpenTelemetryMiddleware } from 'ai.matey.middleware';
|
|
134
|
+
*
|
|
135
|
+
* const otel = await createOpenTelemetryMiddleware({
|
|
136
|
+
* serviceName: 'my-ai-service',
|
|
137
|
+
* endpoint: 'http://localhost:4318/v1/traces',
|
|
138
|
+
* samplingRate: 1.0
|
|
139
|
+
* });
|
|
140
|
+
*
|
|
141
|
+
* bridge.use(otel);
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
export declare function createOpenTelemetryMiddleware(config?: OpenTelemetryConfig): Promise<Middleware>;
|
|
145
|
+
/**
|
|
146
|
+
* OpenTelemetry-based telemetry sink.
|
|
147
|
+
*
|
|
148
|
+
* Implements the TelemetrySink interface using OpenTelemetry metrics.
|
|
149
|
+
* This allows you to use the existing telemetry middleware with OpenTelemetry.
|
|
150
|
+
*
|
|
151
|
+
* **Note:** Currently uses spans for metrics as the OpenTelemetry metrics API
|
|
152
|
+
* is still evolving. Consider using createOpenTelemetryMiddleware for full
|
|
153
|
+
* tracing support.
|
|
154
|
+
*/
|
|
155
|
+
export declare class OpenTelemetryTelemetrySink implements TelemetrySink {
|
|
156
|
+
private tracer;
|
|
157
|
+
private constructor();
|
|
158
|
+
/**
|
|
159
|
+
* Create a new OpenTelemetryTelemetrySink instance.
|
|
160
|
+
*
|
|
161
|
+
* @param config OpenTelemetry configuration
|
|
162
|
+
* @returns Promise that resolves to a new sink instance
|
|
163
|
+
* @throws Error if OpenTelemetry packages are not installed
|
|
164
|
+
*/
|
|
165
|
+
static create(config?: OpenTelemetryConfig): Promise<OpenTelemetryTelemetrySink>;
|
|
166
|
+
recordMetric(name: string, value: number, tags?: Record<string, string>): void;
|
|
167
|
+
recordEvent(name: string, data?: Record<string, unknown>): void;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Check if OpenTelemetry packages are installed and available.
|
|
171
|
+
*
|
|
172
|
+
* @returns Promise that resolves to true if OpenTelemetry is available
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* ```typescript
|
|
176
|
+
* import { isOpenTelemetryAvailable } from 'ai.matey.middleware';
|
|
177
|
+
*
|
|
178
|
+
* if (await isOpenTelemetryAvailable()) {
|
|
179
|
+
* console.log('OpenTelemetry is available!');
|
|
180
|
+
* } else {
|
|
181
|
+
* console.log('OpenTelemetry packages not installed.');
|
|
182
|
+
* }
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
export declare function isOpenTelemetryAvailable(): Promise<boolean>;
|
|
186
|
+
/**
|
|
187
|
+
* Synchronously check if OpenTelemetry is already loaded.
|
|
188
|
+
* Use isOpenTelemetryAvailable() for async check with dynamic import.
|
|
189
|
+
*
|
|
190
|
+
* @returns True if OpenTelemetry is already loaded
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```typescript
|
|
194
|
+
* import { isOpenTelemetryLoaded } from 'ai.matey.middleware';
|
|
195
|
+
*
|
|
196
|
+
* if (isOpenTelemetryLoaded()) {
|
|
197
|
+
* console.log('OpenTelemetry is already loaded!');
|
|
198
|
+
* }
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
export declare function isOpenTelemetryLoaded(): boolean;
|
|
202
|
+
/**
|
|
203
|
+
* Force shutdown of the global tracer provider.
|
|
204
|
+
* Useful for graceful shutdown in server applications.
|
|
205
|
+
*
|
|
206
|
+
* @returns Promise that resolves when shutdown is complete
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* ```typescript
|
|
210
|
+
* import { shutdownOpenTelemetry } from 'ai.matey.middleware';
|
|
211
|
+
*
|
|
212
|
+
* process.on('SIGTERM', async () => {
|
|
213
|
+
* await shutdownOpenTelemetry();
|
|
214
|
+
* process.exit(0);
|
|
215
|
+
* });
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
218
|
+
export declare function shutdownOpenTelemetry(): Promise<void>;
|
|
219
|
+
//# sourceMappingURL=opentelemetry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"opentelemetry.d.ts","sourceRoot":"","sources":["../../src/opentelemetry.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAqC,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAkEnG;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB;;;OAGG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAE5B;;;OAGG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAE9B;;;OAGG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC9B;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAEjC;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB;;OAEG;IACH,kBAAkB,CAAC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
+
*
|
|
4
|
+
* Retries failed requests with exponential backoff.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import type { Middleware } from 'ai.matey.types';
|
|
9
|
+
/**
|
|
10
|
+
* Configuration for retry middleware.
|
|
11
|
+
*/
|
|
12
|
+
export interface RetryConfig {
|
|
13
|
+
/**
|
|
14
|
+
* Maximum number of retry attempts.
|
|
15
|
+
* @default 3
|
|
16
|
+
*/
|
|
17
|
+
maxAttempts?: number;
|
|
18
|
+
/**
|
|
19
|
+
* Initial delay before first retry (milliseconds).
|
|
20
|
+
* @default 1000
|
|
21
|
+
*/
|
|
22
|
+
initialDelay?: number;
|
|
23
|
+
/**
|
|
24
|
+
* Backoff multiplier for exponential backoff.
|
|
25
|
+
* @default 2
|
|
26
|
+
*/
|
|
27
|
+
backoffMultiplier?: number;
|
|
28
|
+
/**
|
|
29
|
+
* Maximum delay between retries (milliseconds).
|
|
30
|
+
* @default 30000 (30 seconds)
|
|
31
|
+
*/
|
|
32
|
+
maxDelay?: number;
|
|
33
|
+
/**
|
|
34
|
+
* Whether to add jitter to retry delays.
|
|
35
|
+
* @default true
|
|
36
|
+
*/
|
|
37
|
+
useJitter?: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Custom function to determine if error is retryable.
|
|
40
|
+
* @default Check error.isRetryable property
|
|
41
|
+
*/
|
|
42
|
+
shouldRetry?: (error: unknown, attempt: number) => boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Callback invoked before each retry.
|
|
45
|
+
*/
|
|
46
|
+
onRetry?: (error: unknown, attempt: number, delay: number) => void;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Create retry middleware.
|
|
50
|
+
*
|
|
51
|
+
* Retries failed requests with exponential backoff.
|
|
52
|
+
*
|
|
53
|
+
* @param config Retry configuration
|
|
54
|
+
* @returns Retry middleware
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* const retry = createRetryMiddleware({
|
|
59
|
+
* maxAttempts: 3,
|
|
60
|
+
* initialDelay: 1000,
|
|
61
|
+
* backoffMultiplier: 2,
|
|
62
|
+
* maxDelay: 30000
|
|
63
|
+
* });
|
|
64
|
+
*
|
|
65
|
+
* bridge.use(retry);
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export declare function createRetryMiddleware(config?: RetryConfig): Middleware;
|
|
69
|
+
/**
|
|
70
|
+
* Check if an error is a rate limit error.
|
|
71
|
+
*/
|
|
72
|
+
export declare function isRateLimitError(error: unknown): boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Check if an error is a network error.
|
|
75
|
+
*/
|
|
76
|
+
export declare function isNetworkError(error: unknown): boolean;
|
|
77
|
+
/**
|
|
78
|
+
* Check if an error is a server error (5xx).
|
|
79
|
+
*/
|
|
80
|
+
export declare function isServerError(error: unknown): boolean;
|
|
81
|
+
/**
|
|
82
|
+
* Create a retry predicate that only retries specific error types.
|
|
83
|
+
* Note: maxAttempts is enforced by the middleware loop, not this function.
|
|
84
|
+
*/
|
|
85
|
+
export declare function createRetryPredicate(errorTypes: Array<'rate_limit' | 'network' | 'server'>): (error: unknown, attempt: number) => boolean;
|
|
86
|
+
//# sourceMappingURL=retry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retry.d.ts","sourceRoot":"","sources":["../../src/retry.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAqC,MAAM,gBAAgB,CAAC;AAQpF;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB;;;OAGG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAE3B;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;;OAGG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IAEpB;;;OAGG;IACH,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC;IAE3D;;OAEG;IACH,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CACpE;AA8DD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,GAAE,WAAgB,GAAG,UAAU,CA2F1E;AAMD;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAMxD;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAMtD;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAMrD;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAClC,UAAU,EAAE,KAAK,CAAC,YAAY,GAAG,SAAS,GAAG,QAAQ,CAAC,GACrD,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,KAAK,OAAO,CAwB9C"}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Security Middleware
|
|
3
|
+
*
|
|
4
|
+
* Adds security headers and implements security best practices for HTTP responses.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import type { Middleware } from 'ai.matey.types';
|
|
9
|
+
/**
|
|
10
|
+
* Security headers configuration
|
|
11
|
+
*/
|
|
12
|
+
export interface SecurityConfig {
|
|
13
|
+
/**
|
|
14
|
+
* Content Security Policy
|
|
15
|
+
* @default "default-src 'self'"
|
|
16
|
+
*/
|
|
17
|
+
contentSecurityPolicy?: string | false;
|
|
18
|
+
/**
|
|
19
|
+
* X-Content-Type-Options header
|
|
20
|
+
* @default "nosniff"
|
|
21
|
+
*/
|
|
22
|
+
contentTypeOptions?: string | false;
|
|
23
|
+
/**
|
|
24
|
+
* X-Frame-Options header
|
|
25
|
+
* @default "DENY"
|
|
26
|
+
*/
|
|
27
|
+
frameOptions?: 'DENY' | 'SAMEORIGIN' | false;
|
|
28
|
+
/**
|
|
29
|
+
* X-XSS-Protection header
|
|
30
|
+
* @default "1; mode=block"
|
|
31
|
+
*/
|
|
32
|
+
xssProtection?: string | false;
|
|
33
|
+
/**
|
|
34
|
+
* Strict-Transport-Security header
|
|
35
|
+
* @default "max-age=31536000; includeSubDomains"
|
|
36
|
+
*/
|
|
37
|
+
hsts?: string | false;
|
|
38
|
+
/**
|
|
39
|
+
* Referrer-Policy header
|
|
40
|
+
* @default "strict-origin-when-cross-origin"
|
|
41
|
+
*/
|
|
42
|
+
referrerPolicy?: 'no-referrer' | 'no-referrer-when-downgrade' | 'origin' | 'origin-when-cross-origin' | 'same-origin' | 'strict-origin' | 'strict-origin-when-cross-origin' | 'unsafe-url' | false;
|
|
43
|
+
/**
|
|
44
|
+
* Permissions-Policy header
|
|
45
|
+
* @default "geolocation=(), microphone=(), camera=()"
|
|
46
|
+
*/
|
|
47
|
+
permissionsPolicy?: string | false;
|
|
48
|
+
/**
|
|
49
|
+
* X-Powered-By header (should be removed for security)
|
|
50
|
+
* @default false (header removed)
|
|
51
|
+
*/
|
|
52
|
+
poweredBy?: string | false;
|
|
53
|
+
/**
|
|
54
|
+
* Additional custom headers
|
|
55
|
+
*/
|
|
56
|
+
customHeaders?: Record<string, string>;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Default security configuration
|
|
60
|
+
*/
|
|
61
|
+
export declare const DEFAULT_SECURITY_CONFIG: Required<Omit<SecurityConfig, 'customHeaders'>>;
|
|
62
|
+
/**
|
|
63
|
+
* Create security headers middleware
|
|
64
|
+
*
|
|
65
|
+
* Adds security headers to responses to protect against common vulnerabilities.
|
|
66
|
+
*
|
|
67
|
+
* @param config - Security configuration
|
|
68
|
+
* @returns Middleware function
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```typescript
|
|
72
|
+
* import { createSecurityMiddleware } from 'ai.matey';
|
|
73
|
+
*
|
|
74
|
+
* const security = createSecurityMiddleware({
|
|
75
|
+
* contentSecurityPolicy: "default-src 'self'",
|
|
76
|
+
* hsts: 'max-age=31536000',
|
|
77
|
+
* });
|
|
78
|
+
*
|
|
79
|
+
* bridge.use(security);
|
|
80
|
+
* ```
|
|
81
|
+
*
|
|
82
|
+
* @example Production Configuration
|
|
83
|
+
* ```typescript
|
|
84
|
+
* const productionSecurity = createSecurityMiddleware({
|
|
85
|
+
* contentSecurityPolicy: "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'",
|
|
86
|
+
* frameOptions: 'DENY',
|
|
87
|
+
* hsts: 'max-age=31536000; includeSubDomains; preload',
|
|
88
|
+
* referrerPolicy: 'strict-origin-when-cross-origin',
|
|
89
|
+
* permissionsPolicy: 'geolocation=(), microphone=(), camera=(), payment=()',
|
|
90
|
+
* });
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export declare function createSecurityMiddleware(config?: SecurityConfig): Middleware;
|
|
94
|
+
/**
|
|
95
|
+
* Create production-ready security middleware with strict settings
|
|
96
|
+
*
|
|
97
|
+
* @returns Middleware with production security settings
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* import { createProductionSecurityMiddleware } from 'ai.matey';
|
|
102
|
+
*
|
|
103
|
+
* bridge.use(createProductionSecurityMiddleware());
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
export declare function createProductionSecurityMiddleware(): Middleware;
|
|
107
|
+
/**
|
|
108
|
+
* Create development-friendly security middleware with relaxed settings
|
|
109
|
+
*
|
|
110
|
+
* @returns Middleware with development security settings
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* import { createDevelopmentSecurityMiddleware } from 'ai.matey';
|
|
115
|
+
*
|
|
116
|
+
* bridge.use(createDevelopmentSecurityMiddleware());
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
export declare function createDevelopmentSecurityMiddleware(): Middleware;
|
|
120
|
+
//# sourceMappingURL=security.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"security.d.ts","sourceRoot":"","sources":["../../src/security.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAEjD;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,qBAAqB,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;IAEvC;;;OAGG;IACH,kBAAkB,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;IAEpC;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,GAAG,YAAY,GAAG,KAAK,CAAC;IAE7C;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;IAE/B;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;IAEtB;;;OAGG;IACH,cAAc,CAAC,EACX,aAAa,GACb,4BAA4B,GAC5B,QAAQ,GACR,0BAA0B,GAC1B,aAAa,GACb,eAAe,GACf,iCAAiC,GACjC,YAAY,GACZ,KAAK,CAAC;IAEV;;;OAGG;IACH,iBAAiB,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;IAEnC;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;IAE3B;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACxC;AAED;;GAEG;AACH,eAAO,MAAM,uBAAuB,EAAE,QAAQ,CAAC,IAAI,CAAC,cAAc,EAAE,eAAe,CAAC,CASnF,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,wBAAwB,CAAC,MAAM,GAAE,cAAmB,GAAG,UAAU,CAgFhF;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,kCAAkC,IAAI,UAAU,CAY/D;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,mCAAmC,IAAI,UAAU,CAWhE"}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Telemetry Middleware
|
|
3
|
+
*
|
|
4
|
+
* Tracks metrics, events, and performance data for requests and responses.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import type { Middleware, TelemetrySink } from 'ai.matey.types';
|
|
9
|
+
/**
|
|
10
|
+
* Configuration for telemetry middleware.
|
|
11
|
+
*/
|
|
12
|
+
export interface TelemetryConfig {
|
|
13
|
+
/**
|
|
14
|
+
* Telemetry sink for sending metrics.
|
|
15
|
+
*/
|
|
16
|
+
sink: TelemetrySink;
|
|
17
|
+
/**
|
|
18
|
+
* Whether to track request counts.
|
|
19
|
+
* @default true
|
|
20
|
+
*/
|
|
21
|
+
trackCounts?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Whether to track latencies.
|
|
24
|
+
* @default true
|
|
25
|
+
*/
|
|
26
|
+
trackLatencies?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Whether to track errors.
|
|
29
|
+
* @default true
|
|
30
|
+
*/
|
|
31
|
+
trackErrors?: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Whether to track token usage.
|
|
34
|
+
* @default true
|
|
35
|
+
*/
|
|
36
|
+
trackTokens?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Custom tags to add to all metrics.
|
|
39
|
+
*/
|
|
40
|
+
tags?: Record<string, string>;
|
|
41
|
+
/**
|
|
42
|
+
* Sample rate (0.0 to 1.0).
|
|
43
|
+
* @default 1.0 (100%)
|
|
44
|
+
*/
|
|
45
|
+
sampleRate?: number;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Metric names.
|
|
49
|
+
*/
|
|
50
|
+
export declare const MetricNames: {
|
|
51
|
+
readonly REQUEST_COUNT: "ai.adapter.request.count";
|
|
52
|
+
readonly REQUEST_DURATION: "ai.adapter.request.duration";
|
|
53
|
+
readonly REQUEST_ERROR: "ai.adapter.request.error";
|
|
54
|
+
readonly TOKEN_PROMPT: "ai.adapter.tokens.prompt";
|
|
55
|
+
readonly TOKEN_COMPLETION: "ai.adapter.tokens.completion";
|
|
56
|
+
readonly TOKEN_TOTAL: "ai.adapter.tokens.total";
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Event names.
|
|
60
|
+
*/
|
|
61
|
+
export declare const EventNames: {
|
|
62
|
+
readonly REQUEST_START: "ai.adapter.request.start";
|
|
63
|
+
readonly REQUEST_COMPLETE: "ai.adapter.request.complete";
|
|
64
|
+
readonly REQUEST_ERROR: "ai.adapter.request.error";
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Create telemetry middleware.
|
|
68
|
+
*
|
|
69
|
+
* Tracks metrics and events for monitoring and observability.
|
|
70
|
+
*
|
|
71
|
+
* @param config Telemetry configuration
|
|
72
|
+
* @returns Telemetry middleware
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* const telemetry = createTelemetryMiddleware({
|
|
77
|
+
* sink: {
|
|
78
|
+
* recordMetric: (name, value, tags) => {
|
|
79
|
+
* console.log(`Metric: ${name} = ${value}`, tags);
|
|
80
|
+
* },
|
|
81
|
+
* recordEvent: (name, data) => {
|
|
82
|
+
* console.log(`Event: ${name}`, data);
|
|
83
|
+
* }
|
|
84
|
+
* },
|
|
85
|
+
* trackCounts: true,
|
|
86
|
+
* trackLatencies: true,
|
|
87
|
+
* trackTokens: true
|
|
88
|
+
* });
|
|
89
|
+
*
|
|
90
|
+
* bridge.use(telemetry);
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export declare function createTelemetryMiddleware(config: TelemetryConfig): Middleware;
|
|
94
|
+
/**
|
|
95
|
+
* Console telemetry sink for development/debugging.
|
|
96
|
+
*/
|
|
97
|
+
export declare class ConsoleTelemetrySink implements TelemetrySink {
|
|
98
|
+
recordMetric(name: string, value: number, tags?: Record<string, string>): void;
|
|
99
|
+
recordEvent(name: string, data?: Record<string, unknown>): void;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* In-memory telemetry sink for testing.
|
|
103
|
+
*/
|
|
104
|
+
export declare class InMemoryTelemetrySink implements TelemetrySink {
|
|
105
|
+
private metrics;
|
|
106
|
+
private events;
|
|
107
|
+
recordMetric(name: string, value: number, tags?: Record<string, string>): void;
|
|
108
|
+
recordEvent(name: string, data?: Record<string, unknown>): void;
|
|
109
|
+
getMetrics(): Array<{
|
|
110
|
+
name: string;
|
|
111
|
+
value: number;
|
|
112
|
+
tags?: Record<string, string>;
|
|
113
|
+
}>;
|
|
114
|
+
getEvents(): Array<{
|
|
115
|
+
name: string;
|
|
116
|
+
data?: Record<string, unknown>;
|
|
117
|
+
}>;
|
|
118
|
+
clear(): void;
|
|
119
|
+
}
|
|
120
|
+
//# sourceMappingURL=telemetry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"telemetry.d.ts","sourceRoot":"","sources":["../../src/telemetry.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAqC,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAOnG;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B;;OAEG;IACH,IAAI,EAAE,aAAa,CAAC;IAEpB;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;IAEtB;;;OAGG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;IAEzB;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;IAEtB;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,CA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