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,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Conversation History Middleware
|
|
3
|
+
*
|
|
4
|
+
* Manages conversation history across requests with configurable trimming strategies.
|
|
5
|
+
* Maintains a global conversation history that can be accessed and modified by requests.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import type { Middleware } from 'ai.matey.types';
|
|
10
|
+
import type { IRMessage } from 'ai.matey.types';
|
|
11
|
+
import { type TrimStrategy } from 'ai.matey.utils';
|
|
12
|
+
/**
|
|
13
|
+
* Configuration for conversation history middleware.
|
|
14
|
+
*/
|
|
15
|
+
export interface ConversationHistoryConfig {
|
|
16
|
+
/**
|
|
17
|
+
* Maximum number of message pairs to keep.
|
|
18
|
+
* - 0: No history (stateless)
|
|
19
|
+
* - -1: Unlimited history
|
|
20
|
+
* - N > 0: Keep last N user/assistant pairs
|
|
21
|
+
* @default -1 (unlimited)
|
|
22
|
+
*/
|
|
23
|
+
maxHistorySize?: number;
|
|
24
|
+
/**
|
|
25
|
+
* Trimming strategy.
|
|
26
|
+
* - 'fifo': First-in-first-out, remove oldest messages
|
|
27
|
+
* - 'smart': Preserve system messages, trim user/assistant pairs
|
|
28
|
+
* @default 'smart'
|
|
29
|
+
*/
|
|
30
|
+
strategy?: TrimStrategy;
|
|
31
|
+
/**
|
|
32
|
+
* Whether to prepend history to each request.
|
|
33
|
+
* If false, history is maintained but not automatically added to requests.
|
|
34
|
+
* @default true
|
|
35
|
+
*/
|
|
36
|
+
prependHistory?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Whether to append responses to history after each request.
|
|
39
|
+
* @default true
|
|
40
|
+
*/
|
|
41
|
+
trackResponses?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Initial conversation history.
|
|
44
|
+
* Useful for seeding with system messages or previous conversation.
|
|
45
|
+
* @default []
|
|
46
|
+
*/
|
|
47
|
+
initialHistory?: IRMessage[];
|
|
48
|
+
/**
|
|
49
|
+
* Custom filter to determine which messages should be added to history.
|
|
50
|
+
* Return true to include message in history, false to exclude.
|
|
51
|
+
* @default (msg) => true (include all messages)
|
|
52
|
+
*/
|
|
53
|
+
messageFilter?: (message: IRMessage) => boolean;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Conversation history manager for accessing and manipulating history.
|
|
57
|
+
*/
|
|
58
|
+
export interface ConversationHistoryManager {
|
|
59
|
+
/**
|
|
60
|
+
* Get the current conversation history.
|
|
61
|
+
*/
|
|
62
|
+
getHistory(): readonly IRMessage[];
|
|
63
|
+
/**
|
|
64
|
+
* Add a message to the history.
|
|
65
|
+
* @param message Message to add
|
|
66
|
+
*/
|
|
67
|
+
addMessage(message: IRMessage): void;
|
|
68
|
+
/**
|
|
69
|
+
* Add multiple messages to the history.
|
|
70
|
+
* @param messages Messages to add
|
|
71
|
+
*/
|
|
72
|
+
addMessages(messages: IRMessage[]): void;
|
|
73
|
+
/**
|
|
74
|
+
* Clear the conversation history.
|
|
75
|
+
*/
|
|
76
|
+
clear(): void;
|
|
77
|
+
/**
|
|
78
|
+
* Set the conversation history to a specific state.
|
|
79
|
+
* @param history New history state
|
|
80
|
+
*/
|
|
81
|
+
setHistory(history: IRMessage[]): void;
|
|
82
|
+
/**
|
|
83
|
+
* Get the number of message pairs in history (excluding system messages).
|
|
84
|
+
*/
|
|
85
|
+
getPairCount(): number;
|
|
86
|
+
/**
|
|
87
|
+
* Manually trigger history trimming based on config.
|
|
88
|
+
*/
|
|
89
|
+
trim(): void;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Create conversation history middleware.
|
|
93
|
+
*
|
|
94
|
+
* Maintains a global conversation history across requests. Automatically prepends
|
|
95
|
+
* history to requests and appends responses to history after completion.
|
|
96
|
+
*
|
|
97
|
+
* @param config Conversation history configuration
|
|
98
|
+
* @returns Conversation history middleware and manager
|
|
99
|
+
*
|
|
100
|
+
* @example Basic Usage
|
|
101
|
+
* ```typescript
|
|
102
|
+
* const { middleware, manager } = createConversationHistoryMiddleware({
|
|
103
|
+
* maxHistorySize: 10,
|
|
104
|
+
* strategy: 'smart'
|
|
105
|
+
* });
|
|
106
|
+
*
|
|
107
|
+
* bridge.use(middleware);
|
|
108
|
+
*
|
|
109
|
+
* // Access history
|
|
110
|
+
* console.log('Pairs:', manager.getPairCount());
|
|
111
|
+
* console.log('History:', manager.getHistory());
|
|
112
|
+
*
|
|
113
|
+
* // Clear history when needed
|
|
114
|
+
* manager.clear();
|
|
115
|
+
* ```
|
|
116
|
+
*
|
|
117
|
+
* @example With Initial System Message
|
|
118
|
+
* ```typescript
|
|
119
|
+
* const { middleware } = createConversationHistoryMiddleware({
|
|
120
|
+
* initialHistory: [
|
|
121
|
+
* { role: 'system', content: 'You are a helpful assistant.' }
|
|
122
|
+
* ],
|
|
123
|
+
* maxHistorySize: 5,
|
|
124
|
+
* strategy: 'smart' // Preserves system message
|
|
125
|
+
* });
|
|
126
|
+
* ```
|
|
127
|
+
*
|
|
128
|
+
* @example Stateless Mode (No History)
|
|
129
|
+
* ```typescript
|
|
130
|
+
* const { middleware } = createConversationHistoryMiddleware({
|
|
131
|
+
* maxHistorySize: 0 // No history tracking
|
|
132
|
+
* });
|
|
133
|
+
* ```
|
|
134
|
+
*
|
|
135
|
+
* @example Custom Message Filtering
|
|
136
|
+
* ```typescript
|
|
137
|
+
* const { middleware } = createConversationHistoryMiddleware({
|
|
138
|
+
* maxHistorySize: 10,
|
|
139
|
+
* // Only track user and assistant messages, ignore system
|
|
140
|
+
* messageFilter: (msg) => msg.role !== 'system'
|
|
141
|
+
* });
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
export declare function createConversationHistoryMiddleware(config?: ConversationHistoryConfig): {
|
|
145
|
+
middleware: Middleware;
|
|
146
|
+
manager: ConversationHistoryManager;
|
|
147
|
+
};
|
|
148
|
+
/**
|
|
149
|
+
* Create a simple conversation history middleware with minimal config.
|
|
150
|
+
*
|
|
151
|
+
* @param maxHistorySize Maximum number of message pairs to keep
|
|
152
|
+
* @returns Conversation history middleware
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```typescript
|
|
156
|
+
* // Keep last 5 message pairs
|
|
157
|
+
* bridge.use(simpleConversationHistory(5));
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
export declare function simpleConversationHistory(maxHistorySize?: number): Middleware;
|
|
161
|
+
/**
|
|
162
|
+
* Create a stateless conversation history middleware.
|
|
163
|
+
* Useful for explicitly disabling history in a pipeline.
|
|
164
|
+
*
|
|
165
|
+
* @returns Stateless middleware
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* ```typescript
|
|
169
|
+
* // Explicitly disable history
|
|
170
|
+
* bridge.use(statelessConversation());
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
export declare function statelessConversation(): Middleware;
|
|
174
|
+
/**
|
|
175
|
+
* Create a conversation history middleware that only tracks user/assistant pairs.
|
|
176
|
+
* System messages from requests are passed through but not tracked.
|
|
177
|
+
*
|
|
178
|
+
* @param maxHistorySize Maximum number of message pairs
|
|
179
|
+
* @returns Conversation history middleware
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```typescript
|
|
183
|
+
* // Track conversation but not system messages
|
|
184
|
+
* bridge.use(conversationOnlyHistory(10));
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
export declare function conversationOnlyHistory(maxHistorySize?: number): Middleware;
|
|
188
|
+
//# sourceMappingURL=conversation-history.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conversation-history.d.ts","sourceRoot":"","sources":["../../src/conversation-history.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAqC,MAAM,gBAAgB,CAAC;AACpF,OAAO,KAAK,EAAkB,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAChE,OAAO,EAAe,KAAK,YAAY,EAAqB,MAAM,gBAAgB,CAAC;AAMnF;;GAEG;AACH,MAAM,WAAW,yBAAyB;IACxC;;;;;;OAMG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,YAAY,CAAC;IAExB;;;;OAIG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;IAEzB;;;OAGG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;IAEzB;;;;OAIG;IACH,cAAc,CAAC,EAAE,SAAS,EAAE,CAAC;IAE7B;;;;OAIG;IACH,aAAa,CAAC,EAAE,CAAC,OAAO,EAAE,SAAS,KAAK,OAAO,CAAC;CACjD;AAED;;GAEG;AACH,MAAM,WAAW,0BAA0B;IACzC;;OAEG;IACH,UAAU,IAAI,SAAS,SAAS,EAAE,CAAC;IAEnC;;;OAGG;IACH,UAAU,CAAC,OAAO,EAAE,SAAS,GAAG,IAAI,CAAC;IAErC;;;OAGG;IACH,WAAW,CAAC,QAAQ,EAAE,SAAS,EAAE,GAAG,IAAI,CAAC;IAEzC;;OAEG;IACH,KAAK,IAAI,IAAI,CAAC;IAEd;;;OAGG;IACH,UAAU,CAAC,OAAO,EAAE,SAAS,EAAE,GAAG,IAAI,CAAC;IAEvC;;OAEG;IACH,YAAY,IAAI,MAAM,CAAC;IAEvB;;OAEG;IACH,IAAI,IAAI,IAAI,CAAC;CACd;AAMD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoDG;AACH,wBAAgB,mCAAmC,CAAC,MAAM,GAAE,yBAA8B,GAAG;IAC3F,UAAU,EAAE,UAAU,CAAC;IACvB,OAAO,EAAE,0BAA0B,CAAC;CACrC,CA0HA;AAMD;;;;;;;;;;;GAWG;AACH,wBAAgB,yBAAyB,CAAC,cAAc,GAAE,MAAW,GAAG,UAAU,CAGjF;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,qBAAqB,IAAI,UAAU,CAGlD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,uBAAuB,CAAC,cAAc,GAAE,MAAW,GAAG,UAAU,CAO/E"}
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cost Tracking Middleware
|
|
3
|
+
*
|
|
4
|
+
* Track and monitor API costs across different providers.
|
|
5
|
+
* Essential for production deployments to control spending.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import type { Middleware, StreamingMiddleware } from 'ai.matey.types';
|
|
10
|
+
import type { IRUsage } from 'ai.matey.types';
|
|
11
|
+
/**
|
|
12
|
+
* Provider pricing configuration
|
|
13
|
+
*/
|
|
14
|
+
export interface ProviderPricing {
|
|
15
|
+
/**
|
|
16
|
+
* Cost per 1M input tokens (in USD)
|
|
17
|
+
*/
|
|
18
|
+
inputCostPer1M: number;
|
|
19
|
+
/**
|
|
20
|
+
* Cost per 1M output tokens (in USD)
|
|
21
|
+
*/
|
|
22
|
+
outputCostPer1M: number;
|
|
23
|
+
/**
|
|
24
|
+
* Optional: Different pricing for cached tokens
|
|
25
|
+
*/
|
|
26
|
+
cachedInputCostPer1M?: number;
|
|
27
|
+
/**
|
|
28
|
+
* Optional: Different pricing based on request type
|
|
29
|
+
*/
|
|
30
|
+
imageInputCostPer1M?: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Model-specific pricing
|
|
34
|
+
*/
|
|
35
|
+
export interface ModelPricing {
|
|
36
|
+
/**
|
|
37
|
+
* Model identifier or pattern (supports wildcards)
|
|
38
|
+
*/
|
|
39
|
+
model: string | RegExp;
|
|
40
|
+
/**
|
|
41
|
+
* Pricing for this model
|
|
42
|
+
*/
|
|
43
|
+
pricing: ProviderPricing;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Cost calculation result
|
|
47
|
+
*/
|
|
48
|
+
export interface CostCalculation {
|
|
49
|
+
/**
|
|
50
|
+
* Provider name
|
|
51
|
+
*/
|
|
52
|
+
provider: string;
|
|
53
|
+
/**
|
|
54
|
+
* Model used
|
|
55
|
+
*/
|
|
56
|
+
model: string;
|
|
57
|
+
/**
|
|
58
|
+
* Input tokens
|
|
59
|
+
*/
|
|
60
|
+
inputTokens: number;
|
|
61
|
+
/**
|
|
62
|
+
* Output tokens
|
|
63
|
+
*/
|
|
64
|
+
outputTokens: number;
|
|
65
|
+
/**
|
|
66
|
+
* Total tokens
|
|
67
|
+
*/
|
|
68
|
+
totalTokens: number;
|
|
69
|
+
/**
|
|
70
|
+
* Input cost in USD
|
|
71
|
+
*/
|
|
72
|
+
inputCost: number;
|
|
73
|
+
/**
|
|
74
|
+
* Output cost in USD
|
|
75
|
+
*/
|
|
76
|
+
outputCost: number;
|
|
77
|
+
/**
|
|
78
|
+
* Total cost in USD
|
|
79
|
+
*/
|
|
80
|
+
totalCost: number;
|
|
81
|
+
/**
|
|
82
|
+
* Request timestamp
|
|
83
|
+
*/
|
|
84
|
+
timestamp: number;
|
|
85
|
+
/**
|
|
86
|
+
* Request ID
|
|
87
|
+
*/
|
|
88
|
+
requestId: string;
|
|
89
|
+
/**
|
|
90
|
+
* Additional metadata
|
|
91
|
+
*/
|
|
92
|
+
metadata?: Record<string, unknown>;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Cost tracking storage interface
|
|
96
|
+
*/
|
|
97
|
+
export interface CostStorage {
|
|
98
|
+
/**
|
|
99
|
+
* Record a cost calculation
|
|
100
|
+
*/
|
|
101
|
+
record(cost: CostCalculation): Promise<void>;
|
|
102
|
+
/**
|
|
103
|
+
* Get total cost for a time period
|
|
104
|
+
*/
|
|
105
|
+
getTotal(startTime?: number, endTime?: number): Promise<number>;
|
|
106
|
+
/**
|
|
107
|
+
* Get costs grouped by provider
|
|
108
|
+
*/
|
|
109
|
+
getByProvider(startTime?: number, endTime?: number): Promise<Map<string, number>>;
|
|
110
|
+
/**
|
|
111
|
+
* Get costs grouped by model
|
|
112
|
+
*/
|
|
113
|
+
getByModel(startTime?: number, endTime?: number): Promise<Map<string, number>>;
|
|
114
|
+
/**
|
|
115
|
+
* Clear all cost data
|
|
116
|
+
*/
|
|
117
|
+
clear(): Promise<void>;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* In-memory cost storage
|
|
121
|
+
*/
|
|
122
|
+
export declare class InMemoryCostStorage implements CostStorage {
|
|
123
|
+
private costs;
|
|
124
|
+
record(cost: CostCalculation): Promise<void>;
|
|
125
|
+
getTotal(startTime?: number, endTime?: number): Promise<number>;
|
|
126
|
+
getByProvider(startTime?: number, endTime?: number): Promise<Map<string, number>>;
|
|
127
|
+
getByModel(startTime?: number, endTime?: number): Promise<Map<string, number>>;
|
|
128
|
+
clear(): Promise<void>;
|
|
129
|
+
private filterCosts;
|
|
130
|
+
/**
|
|
131
|
+
* Get all cost records (for debugging/export)
|
|
132
|
+
*/
|
|
133
|
+
getAllCosts(): CostCalculation[];
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Cost tracking configuration
|
|
137
|
+
*/
|
|
138
|
+
export interface CostTrackingConfig {
|
|
139
|
+
/**
|
|
140
|
+
* Provider-specific pricing
|
|
141
|
+
*/
|
|
142
|
+
providers?: Record<string, ProviderPricing>;
|
|
143
|
+
/**
|
|
144
|
+
* Model-specific pricing (overrides provider pricing)
|
|
145
|
+
*/
|
|
146
|
+
models?: ModelPricing[];
|
|
147
|
+
/**
|
|
148
|
+
* Cost storage implementation
|
|
149
|
+
*/
|
|
150
|
+
storage?: CostStorage;
|
|
151
|
+
/**
|
|
152
|
+
* Callback when cost is calculated
|
|
153
|
+
*/
|
|
154
|
+
onCost?: (cost: CostCalculation) => void | Promise<void>;
|
|
155
|
+
/**
|
|
156
|
+
* Callback when cost threshold is exceeded
|
|
157
|
+
*/
|
|
158
|
+
onThresholdExceeded?: (cost: CostCalculation, threshold: number) => void | Promise<void>;
|
|
159
|
+
/**
|
|
160
|
+
* Cost threshold per request (in USD)
|
|
161
|
+
*/
|
|
162
|
+
requestThreshold?: number;
|
|
163
|
+
/**
|
|
164
|
+
* Cost threshold per hour (in USD)
|
|
165
|
+
*/
|
|
166
|
+
hourlyThreshold?: number;
|
|
167
|
+
/**
|
|
168
|
+
* Cost threshold per day (in USD)
|
|
169
|
+
*/
|
|
170
|
+
dailyThreshold?: number;
|
|
171
|
+
/**
|
|
172
|
+
* Whether to log costs to console
|
|
173
|
+
*/
|
|
174
|
+
logCosts?: boolean;
|
|
175
|
+
/**
|
|
176
|
+
* Whether to include cost in response metadata
|
|
177
|
+
*/
|
|
178
|
+
includeInMetadata?: boolean;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Default pricing for common providers (as of 2024)
|
|
182
|
+
* Prices in USD per 1M tokens
|
|
183
|
+
*/
|
|
184
|
+
export declare const DEFAULT_PRICING: Record<string, ProviderPricing>;
|
|
185
|
+
/**
|
|
186
|
+
* Calculate cost for a request/response
|
|
187
|
+
*/
|
|
188
|
+
export declare function calculateCost(usage: IRUsage, provider: string, model: string, config: CostTrackingConfig): CostCalculation;
|
|
189
|
+
/**
|
|
190
|
+
* Create cost tracking middleware
|
|
191
|
+
*
|
|
192
|
+
* @param config - Cost tracking configuration
|
|
193
|
+
* @returns Middleware function
|
|
194
|
+
*
|
|
195
|
+
* @example Basic Usage
|
|
196
|
+
* ```typescript
|
|
197
|
+
* import { createCostTrackingMiddleware } from 'ai.matey';
|
|
198
|
+
*
|
|
199
|
+
* const costTracking = createCostTrackingMiddleware({
|
|
200
|
+
* logCosts: true,
|
|
201
|
+
* onCost: (cost) => {
|
|
202
|
+
* console.log(`Request cost: $${cost.totalCost.toFixed(6)}`);
|
|
203
|
+
* }
|
|
204
|
+
* });
|
|
205
|
+
*
|
|
206
|
+
* bridge.use(costTracking);
|
|
207
|
+
* ```
|
|
208
|
+
*
|
|
209
|
+
* @example With Custom Pricing
|
|
210
|
+
* ```typescript
|
|
211
|
+
* const costTracking = createCostTrackingMiddleware({
|
|
212
|
+
* providers: {
|
|
213
|
+
* 'anthropic': {
|
|
214
|
+
* inputCostPer1M: 3.0,
|
|
215
|
+
* outputCostPer1M: 15.0
|
|
216
|
+
* }
|
|
217
|
+
* },
|
|
218
|
+
* models: [
|
|
219
|
+
* {
|
|
220
|
+
* model: /gpt-4-turbo/,
|
|
221
|
+
* pricing: {
|
|
222
|
+
* inputCostPer1M: 10.0,
|
|
223
|
+
* outputCostPer1M: 30.0
|
|
224
|
+
* }
|
|
225
|
+
* }
|
|
226
|
+
* ]
|
|
227
|
+
* });
|
|
228
|
+
* ```
|
|
229
|
+
*
|
|
230
|
+
* @example With Thresholds
|
|
231
|
+
* ```typescript
|
|
232
|
+
* const costTracking = createCostTrackingMiddleware({
|
|
233
|
+
* requestThreshold: 0.10, // Warn if request costs > $0.10
|
|
234
|
+
* hourlyThreshold: 10.00, // Warn if hourly cost > $10
|
|
235
|
+
* dailyThreshold: 100.00, // Warn if daily cost > $100
|
|
236
|
+
* onThresholdExceeded: (cost, threshold) => {
|
|
237
|
+
* console.warn(`Cost threshold exceeded: $${cost.totalCost} > $${threshold}`);
|
|
238
|
+
* }
|
|
239
|
+
* });
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
export declare function createCostTrackingMiddleware(config?: CostTrackingConfig): Middleware;
|
|
243
|
+
/**
|
|
244
|
+
* Create streaming cost tracking middleware
|
|
245
|
+
*
|
|
246
|
+
* Note: For streaming, cost is calculated at the end when usage is available
|
|
247
|
+
*/
|
|
248
|
+
export declare function createStreamingCostTrackingMiddleware(config?: CostTrackingConfig): StreamingMiddleware;
|
|
249
|
+
/**
|
|
250
|
+
* Get cost statistics from storage
|
|
251
|
+
*/
|
|
252
|
+
export declare function getCostStats(storage: CostStorage, hours?: number): Promise<{
|
|
253
|
+
period: string;
|
|
254
|
+
total: number;
|
|
255
|
+
byProvider: {
|
|
256
|
+
[k: string]: number;
|
|
257
|
+
};
|
|
258
|
+
byModel: {
|
|
259
|
+
[k: string]: number;
|
|
260
|
+
};
|
|
261
|
+
}>;
|
|
262
|
+
//# sourceMappingURL=cost-tracking.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cost-tracking.d.ts","sourceRoot":"","sources":["../../src/cost-tracking.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AACtE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAE9C;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B;;OAEG;IACH,cAAc,EAAE,MAAM,CAAC;IAEvB;;OAEG;IACH,eAAe,EAAE,MAAM,CAAC;IAExB;;OAEG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAE9B;;OAEG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC9B;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B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B;;OAEG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB;;OAEG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB;;OAEG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B;AAED;;;GAGG;AACH,eAAO,MAAM,eAAe,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CA0E3D,CAAC;AAEF;;GAEG;AACH,wBAAgB,aAAa,CAC3B,KAAK,EAAE,OAAO,EACd,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,kBAAkB,GACzB,eAAe,CAuDjB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoDG;AACH,wBAAgB,4BAA4B,CAAC,MAAM,GAAE,kBAAuB,GAAG,UAAU,CAoExF;AAED;;;;GAIG;AACH,wBAAgB,qCAAqC,CACnD,MAAM,GAAE,kBAAuB,GAC9B,mBAAmB,CA2CrB;AAED;;GAEG;AACH,wBAAsB,YAAY,CAAC,OAAO,EAAE,WAAW,EAAE,KAAK,GAAE,MAAW;;;;;;;;;GAa1E"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AI Matey Middleware
|
|
3
|
+
*
|
|
4
|
+
* Consolidated package containing all middleware components.
|
|
5
|
+
* Middleware provides cross-cutting concerns like caching, retry,
|
|
6
|
+
* logging, and security for the AI adapter system.
|
|
7
|
+
*
|
|
8
|
+
* @module ai.matey.middleware
|
|
9
|
+
*/
|
|
10
|
+
export * from './caching.js';
|
|
11
|
+
export * from './conversation-history.js';
|
|
12
|
+
export * from './cost-tracking.js';
|
|
13
|
+
export * from './logging.js';
|
|
14
|
+
export * from './opentelemetry.js';
|
|
15
|
+
export * from './retry.js';
|
|
16
|
+
export * from './security.js';
|
|
17
|
+
export * from './telemetry.js';
|
|
18
|
+
export * from './transform.js';
|
|
19
|
+
export * from './validation.js';
|
|
20
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,cAAc,cAAc,CAAC;AAC7B,cAAc,2BAA2B,CAAC;AAC1C,cAAc,oBAAoB,CAAC;AACnC,cAAc,cAAc,CAAC;AAC7B,cAAc,oBAAoB,CAAC;AACnC,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Logging Middleware
|
|
3
|
+
*
|
|
4
|
+
* Logs requests, responses, and errors with configurable levels and sanitization.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import type { Middleware } from 'ai.matey.types';
|
|
9
|
+
/**
|
|
10
|
+
* Log level.
|
|
11
|
+
*/
|
|
12
|
+
export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
13
|
+
/**
|
|
14
|
+
* Logger interface.
|
|
15
|
+
*/
|
|
16
|
+
export interface Logger {
|
|
17
|
+
debug(message: string, data?: unknown): void;
|
|
18
|
+
info(message: string, data?: unknown): void;
|
|
19
|
+
warn(message: string, data?: unknown): void;
|
|
20
|
+
error(message: string, data?: unknown): void;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Configuration for logging middleware.
|
|
24
|
+
*/
|
|
25
|
+
export interface LoggingConfig {
|
|
26
|
+
/**
|
|
27
|
+
* Minimum log level.
|
|
28
|
+
* @default 'info'
|
|
29
|
+
*/
|
|
30
|
+
level?: LogLevel;
|
|
31
|
+
/**
|
|
32
|
+
* Whether to log request bodies.
|
|
33
|
+
* @default true
|
|
34
|
+
*/
|
|
35
|
+
logRequests?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Whether to log response bodies.
|
|
38
|
+
* @default true
|
|
39
|
+
*/
|
|
40
|
+
logResponses?: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Whether to log errors.
|
|
43
|
+
* @default true
|
|
44
|
+
*/
|
|
45
|
+
logErrors?: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Whether to sanitize sensitive data (API keys, tokens).
|
|
48
|
+
* @default true
|
|
49
|
+
*/
|
|
50
|
+
sanitize?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Custom logger implementation.
|
|
53
|
+
* @default console
|
|
54
|
+
*/
|
|
55
|
+
logger?: Logger;
|
|
56
|
+
/**
|
|
57
|
+
* Custom log prefix.
|
|
58
|
+
*/
|
|
59
|
+
prefix?: string;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Create logging middleware.
|
|
63
|
+
*
|
|
64
|
+
* Logs requests, responses, and errors at configurable levels.
|
|
65
|
+
*
|
|
66
|
+
* @param config Logging configuration
|
|
67
|
+
* @returns Logging middleware
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* const logging = createLoggingMiddleware({
|
|
72
|
+
* level: 'info',
|
|
73
|
+
* logRequests: true,
|
|
74
|
+
* logResponses: true,
|
|
75
|
+
* sanitize: true
|
|
76
|
+
* });
|
|
77
|
+
*
|
|
78
|
+
* bridge.use(logging);
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export declare function createLoggingMiddleware(config?: LoggingConfig): Middleware;
|
|
82
|
+
//# sourceMappingURL=logging.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logging.d.ts","sourceRoot":"","sources":["../../src/logging.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAqC,MAAM,gBAAgB,CAAC;AAOpF;;GAEG;AACH,MAAM,MAAM,QAAQ,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;AAE3D;;GAEG;AACH,MAAM,WAAW,MAAM;IACrB,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;IAC7C,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;IAC5C,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;IAC5C,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;CAC9C;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B;;;OAGG;IACH,KAAK,CAAC,EAAE,QAAQ,CAAC;IAEjB;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;IAEtB;;;OAGG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IAEvB;;;OAGG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IAEpB;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAwGD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,uBAAuB,CAAC,MAAM,GAAE,aAAkB,GAAG,UAAU,CAwE9E"}
|