ai.matey.utils 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/conversation-history.js +139 -0
- package/dist/cjs/conversation-history.js.map +1 -0
- package/dist/cjs/index.js +42 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/model-cache.js +163 -0
- package/dist/cjs/model-cache.js.map +1 -0
- package/dist/cjs/parameter-normalizer.js +451 -0
- package/dist/cjs/parameter-normalizer.js.map +1 -0
- package/dist/cjs/streaming-modes.js +277 -0
- package/dist/cjs/streaming-modes.js.map +1 -0
- package/dist/cjs/streaming.js +892 -0
- package/dist/cjs/streaming.js.map +1 -0
- package/dist/cjs/structured-output.js +398 -0
- package/dist/cjs/structured-output.js.map +1 -0
- package/dist/cjs/system-message.js +222 -0
- package/dist/cjs/system-message.js.map +1 -0
- package/dist/cjs/validation.js +534 -0
- package/dist/cjs/validation.js.map +1 -0
- package/dist/cjs/warnings.js +301 -0
- package/dist/cjs/warnings.js.map +1 -0
- package/dist/esm/conversation-history.js +134 -0
- package/dist/esm/conversation-history.js.map +1 -0
- package/dist/esm/index.js +26 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/model-cache.js +158 -0
- package/dist/esm/model-cache.js.map +1 -0
- package/dist/esm/parameter-normalizer.js +434 -0
- package/dist/esm/parameter-normalizer.js.map +1 -0
- package/dist/esm/streaming-modes.js +265 -0
- package/dist/esm/streaming-modes.js.map +1 -0
- package/dist/esm/streaming.js +860 -0
- package/dist/esm/streaming.js.map +1 -0
- package/dist/esm/structured-output.js +387 -0
- package/dist/esm/structured-output.js.map +1 -0
- package/dist/esm/system-message.js +213 -0
- package/dist/esm/system-message.js.map +1 -0
- package/dist/esm/validation.js +523 -0
- package/dist/esm/validation.js.map +1 -0
- package/dist/esm/warnings.js +284 -0
- package/dist/esm/warnings.js.map +1 -0
- package/dist/types/conversation-history.d.ts +70 -0
- package/dist/types/conversation-history.d.ts.map +1 -0
- package/dist/types/index.d.ts +17 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/model-cache.d.ts +88 -0
- package/dist/types/model-cache.d.ts.map +1 -0
- package/dist/types/parameter-normalizer.d.ts +154 -0
- package/dist/types/parameter-normalizer.d.ts.map +1 -0
- package/dist/types/streaming-modes.d.ts +139 -0
- package/dist/types/streaming-modes.d.ts.map +1 -0
- package/dist/types/streaming.d.ts +384 -0
- package/dist/types/streaming.d.ts.map +1 -0
- package/dist/types/structured-output.d.ts +157 -0
- package/dist/types/structured-output.d.ts.map +1 -0
- package/dist/types/system-message.d.ts +78 -0
- package/dist/types/system-message.d.ts.map +1 -0
- package/dist/types/validation.d.ts +46 -0
- package/dist/types/validation.d.ts.map +1 -0
- package/dist/types/warnings.d.ts +149 -0
- package/dist/types/warnings.d.ts.map +1 -0
- package/package.json +75 -0
- package/readme.md +280 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 AI Matey Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Conversation History Management Utilities
|
|
4
|
+
*
|
|
5
|
+
* Utilities for managing conversation history with configurable trimming strategies.
|
|
6
|
+
* Used by both middleware and wrappers for consistent history management.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.trimHistory = trimHistory;
|
|
12
|
+
exports.countMessagePairs = countMessagePairs;
|
|
13
|
+
exports.shouldTrimHistory = shouldTrimHistory;
|
|
14
|
+
/**
|
|
15
|
+
* Trim conversation history to keep only the most recent messages.
|
|
16
|
+
*
|
|
17
|
+
* @param history - Current conversation history
|
|
18
|
+
* @param maxHistorySize - Maximum number of message pairs to keep
|
|
19
|
+
* - 0: Return empty array (no history)
|
|
20
|
+
* - -1: Return full history (no trimming)
|
|
21
|
+
* - N > 0: Keep last N user/assistant pairs
|
|
22
|
+
* @param strategy - Trimming strategy
|
|
23
|
+
* - 'fifo': First-in-first-out, remove oldest messages
|
|
24
|
+
* - 'smart': Preserve system messages, trim user/assistant pairs
|
|
25
|
+
* @returns Trimmed history
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* const history = [
|
|
30
|
+
* { role: 'system', content: 'You are helpful' },
|
|
31
|
+
* { role: 'user', content: 'Hi' },
|
|
32
|
+
* { role: 'assistant', content: 'Hello!' },
|
|
33
|
+
* { role: 'user', content: 'How are you?' },
|
|
34
|
+
* { role: 'assistant', content: 'I am well!' },
|
|
35
|
+
* ];
|
|
36
|
+
*
|
|
37
|
+
* // Keep last 1 pair + system message
|
|
38
|
+
* const trimmed = trimHistory(history, 1, 'smart');
|
|
39
|
+
* // Result: system message + last user/assistant pair
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
function trimHistory(history, maxHistorySize, strategy = 'fifo') {
|
|
43
|
+
// No history
|
|
44
|
+
if (maxHistorySize === 0) {
|
|
45
|
+
return [];
|
|
46
|
+
}
|
|
47
|
+
// Unlimited history
|
|
48
|
+
if (maxHistorySize < 0) {
|
|
49
|
+
return Array.from(history);
|
|
50
|
+
}
|
|
51
|
+
// No trimming needed
|
|
52
|
+
if (history.length === 0) {
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
if (strategy === 'smart') {
|
|
56
|
+
return trimHistorySmart(history, maxHistorySize);
|
|
57
|
+
}
|
|
58
|
+
// FIFO strategy: keep last N pairs (2N messages)
|
|
59
|
+
return trimHistoryFifo(history, maxHistorySize);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* FIFO trimming: Keep last N message pairs
|
|
63
|
+
*/
|
|
64
|
+
function trimHistoryFifo(history, maxPairs) {
|
|
65
|
+
const maxMessages = maxPairs * 2;
|
|
66
|
+
if (history.length <= maxMessages) {
|
|
67
|
+
return Array.from(history);
|
|
68
|
+
}
|
|
69
|
+
// Keep last 2N messages
|
|
70
|
+
return Array.from(history.slice(-maxMessages));
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Smart trimming: Preserve system messages, trim user/assistant pairs
|
|
74
|
+
*/
|
|
75
|
+
function trimHistorySmart(history, maxPairs) {
|
|
76
|
+
// Separate system messages from conversation
|
|
77
|
+
const systemMessages = [];
|
|
78
|
+
const conversationMessages = [];
|
|
79
|
+
for (const message of history) {
|
|
80
|
+
if (message.role === 'system') {
|
|
81
|
+
systemMessages.push(message);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
conversationMessages.push(message);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// Trim conversation to last N pairs
|
|
88
|
+
const maxConversationMessages = maxPairs * 2;
|
|
89
|
+
const trimmedConversation = conversationMessages.length <= maxConversationMessages
|
|
90
|
+
? conversationMessages
|
|
91
|
+
: conversationMessages.slice(-maxConversationMessages);
|
|
92
|
+
// Combine: system messages first, then conversation
|
|
93
|
+
return [...systemMessages, ...trimmedConversation];
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Count message pairs in history (excluding system messages)
|
|
97
|
+
*
|
|
98
|
+
* @param history - Conversation history
|
|
99
|
+
* @returns Number of user/assistant pairs
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* const history = [
|
|
104
|
+
* { role: 'system', content: 'You are helpful' },
|
|
105
|
+
* { role: 'user', content: 'Hi' },
|
|
106
|
+
* { role: 'assistant', content: 'Hello!' },
|
|
107
|
+
* ];
|
|
108
|
+
*
|
|
109
|
+
* countMessagePairs(history); // 1
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
function countMessagePairs(history) {
|
|
113
|
+
const nonSystemMessages = history.filter((m) => m.role !== 'system');
|
|
114
|
+
return Math.floor(nonSystemMessages.length / 2);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Check if history needs trimming
|
|
118
|
+
*
|
|
119
|
+
* @param history - Current history
|
|
120
|
+
* @param maxHistorySize - Maximum pairs to keep
|
|
121
|
+
* @param strategy - Trimming strategy
|
|
122
|
+
* @returns True if history exceeds limit
|
|
123
|
+
*/
|
|
124
|
+
function shouldTrimHistory(history, maxHistorySize, strategy = 'fifo') {
|
|
125
|
+
if (maxHistorySize === 0) {
|
|
126
|
+
return history.length > 0;
|
|
127
|
+
}
|
|
128
|
+
if (maxHistorySize < 0) {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
if (strategy === 'smart') {
|
|
132
|
+
const pairs = countMessagePairs(history);
|
|
133
|
+
return pairs > maxHistorySize;
|
|
134
|
+
}
|
|
135
|
+
// FIFO: check total message count
|
|
136
|
+
const maxMessages = maxHistorySize * 2;
|
|
137
|
+
return history.length > maxMessages;
|
|
138
|
+
}
|
|
139
|
+
//# sourceMappingURL=conversation-history.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conversation-history.js","sourceRoot":"","sources":["../../src/conversation-history.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;AAqCH,kCA0BC;AA4DD,8CAGC;AAUD,8CAqBC;AApJD;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,SAAgB,WAAW,CACzB,OAA6B,EAC7B,cAAsB,EACtB,WAAyB,MAAM;IAE/B,aAAa;IACb,IAAI,cAAc,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,oBAAoB;IACpB,IAAI,cAAc,GAAG,CAAC,EAAE,CAAC;QACvB,OAAO,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC7B,CAAC;IAED,qBAAqB;IACrB,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,IAAI,QAAQ,KAAK,OAAO,EAAE,CAAC;QACzB,OAAO,gBAAgB,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC;IACnD,CAAC;IAED,iDAAiD;IACjD,OAAO,eAAe,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC;AAClD,CAAC;AAED;;GAEG;AACH,SAAS,eAAe,CAAC,OAA6B,EAAE,QAAgB;IACtE,MAAM,WAAW,GAAG,QAAQ,GAAG,CAAC,CAAC;IAEjC,IAAI,OAAO,CAAC,MAAM,IAAI,WAAW,EAAE,CAAC;QAClC,OAAO,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC7B,CAAC;IAED,wBAAwB;IACxB,OAAO,KAAK,C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AAC,MAAM,GAAG,CAAC,CAAC;IAC5B,CAAC;IAED,IAAI,cAAc,GAAG,CAAC,EAAE,CAAC;QACvB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,IAAI,QAAQ,KAAK,OAAO,EAAE,CAAC;QACzB,MAAM,KAAK,GAAG,iBAAiB,CAAC,OAAO,CAAC,CAAC;QACzC,OAAO,KAAK,GAAG,cAAc,CAAC;IAChC,CAAC;IAED,kCAAkC;IAClC,MAAM,WAAW,GAAG,cAAc,GAAG,CAAC,CAAC;IACvC,OAAO,OAAO,CAAC,MAAM,GAAG,WAAW,CAAC;AACtC,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Utility Functions
|
|
4
|
+
*
|
|
5
|
+
* Collection of utility functions for validation, normalization, and streaming.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
12
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
13
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
14
|
+
}
|
|
15
|
+
Object.defineProperty(o, k2, desc);
|
|
16
|
+
}) : (function(o, m, k, k2) {
|
|
17
|
+
if (k2 === undefined) k2 = k;
|
|
18
|
+
o[k2] = m[k];
|
|
19
|
+
}));
|
|
20
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
21
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
22
|
+
};
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
// Validation utilities
|
|
25
|
+
__exportStar(require("./validation.js"), exports);
|
|
26
|
+
// System message utilities
|
|
27
|
+
__exportStar(require("./system-message.js"), exports);
|
|
28
|
+
// Parameter normalization utilities
|
|
29
|
+
__exportStar(require("./parameter-normalizer.js"), exports);
|
|
30
|
+
// Streaming utilities
|
|
31
|
+
__exportStar(require("./streaming.js"), exports);
|
|
32
|
+
// Streaming mode conversion utilities
|
|
33
|
+
__exportStar(require("./streaming-modes.js"), exports);
|
|
34
|
+
// Warning utilities
|
|
35
|
+
__exportStar(require("./warnings.js"), exports);
|
|
36
|
+
// Conversation history utilities
|
|
37
|
+
__exportStar(require("./conversation-history.js"), exports);
|
|
38
|
+
// Model cache utilities
|
|
39
|
+
__exportStar(require("./model-cache.js"), exports);
|
|
40
|
+
// Structured output utilities (Zod integration)
|
|
41
|
+
__exportStar(require("./structured-output.js"), exports);
|
|
42
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;;;;;;;;;;;;;;AAEH,uBAAuB;AACvB,kDAAgC;AAEhC,2BAA2B;AAC3B,sDAAoC;AAEpC,oCAAoC;AACpC,4DAA0C;AAE1C,sBAAsB;AACtB,iDAA+B;AAE/B,sCAAsC;AACtC,uDAAqC;AAErC,oBAAoB;AACpB,gDAA8B;AAE9B,iCAAiC;AACjC,4DAA0C;AAE1C,wBAAwB;AACxB,mDAAiC;AAEjC,gDAAgD;AAChD,yDAAuC"}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Model Cache
|
|
4
|
+
*
|
|
5
|
+
* In-memory caching for model lists to reduce API calls.
|
|
6
|
+
* Supports both global (shared) and instance-scoped caches.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.globalModelCache = exports.ModelCache = void 0;
|
|
12
|
+
exports.getModelCache = getModelCache;
|
|
13
|
+
// ============================================================================
|
|
14
|
+
// Model Cache Implementation
|
|
15
|
+
// ============================================================================
|
|
16
|
+
/**
|
|
17
|
+
* In-memory cache for model lists with TTL-based expiration.
|
|
18
|
+
*
|
|
19
|
+
* Usage:
|
|
20
|
+
* ```typescript
|
|
21
|
+
* const cache = new ModelCache();
|
|
22
|
+
*
|
|
23
|
+
* // Set with 1 hour TTL
|
|
24
|
+
* cache.set('openai-backend', modelResult, 3600000);
|
|
25
|
+
*
|
|
26
|
+
* // Get (returns null if expired or not found)
|
|
27
|
+
* const cached = cache.get('openai-backend');
|
|
28
|
+
*
|
|
29
|
+
* // Invalidate specific backend
|
|
30
|
+
* cache.invalidate('openai-backend');
|
|
31
|
+
*
|
|
32
|
+
* // Clear all
|
|
33
|
+
* cache.clear();
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
class ModelCache {
|
|
37
|
+
cache;
|
|
38
|
+
constructor() {
|
|
39
|
+
this.cache = new Map();
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Get cached model list for a backend.
|
|
43
|
+
*
|
|
44
|
+
* @param backendName Unique backend identifier
|
|
45
|
+
* @returns Cached result or null if not found/expired
|
|
46
|
+
*/
|
|
47
|
+
get(backendName) {
|
|
48
|
+
const entry = this.cache.get(backendName);
|
|
49
|
+
if (!entry) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
// Check if expired
|
|
53
|
+
if (Date.now() > entry.expiresAt) {
|
|
54
|
+
this.cache.delete(backendName);
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
// Return cached result with updated source
|
|
58
|
+
return {
|
|
59
|
+
...entry.result,
|
|
60
|
+
source: 'cache',
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Cache a model list result.
|
|
65
|
+
*
|
|
66
|
+
* @param backendName Unique backend identifier
|
|
67
|
+
* @param result Model list result to cache
|
|
68
|
+
* @param ttl Time to live in milliseconds
|
|
69
|
+
*/
|
|
70
|
+
set(backendName, result, ttl) {
|
|
71
|
+
const expiresAt = Date.now() + ttl;
|
|
72
|
+
this.cache.set(backendName, {
|
|
73
|
+
result,
|
|
74
|
+
expiresAt,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Check if backend has cached models.
|
|
79
|
+
*
|
|
80
|
+
* @param backendName Unique backend identifier
|
|
81
|
+
* @returns true if cache exists and not expired
|
|
82
|
+
*/
|
|
83
|
+
has(backendName) {
|
|
84
|
+
return this.get(backendName) !== null;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Invalidate (remove) cached models for a backend.
|
|
88
|
+
*
|
|
89
|
+
* @param backendName Unique backend identifier
|
|
90
|
+
*/
|
|
91
|
+
invalidate(backendName) {
|
|
92
|
+
this.cache.delete(backendName);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Clear all cached model lists.
|
|
96
|
+
*/
|
|
97
|
+
clear() {
|
|
98
|
+
this.cache.clear();
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Get number of cached backends.
|
|
102
|
+
*/
|
|
103
|
+
size() {
|
|
104
|
+
// Count only non-expired entries
|
|
105
|
+
let count = 0;
|
|
106
|
+
const now = Date.now();
|
|
107
|
+
for (const [key, entry] of this.cache.entries()) {
|
|
108
|
+
if (now <= entry.expiresAt) {
|
|
109
|
+
count++;
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
// Clean up expired entry
|
|
113
|
+
this.cache.delete(key);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return count;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Get all cached backend names (excluding expired).
|
|
120
|
+
*/
|
|
121
|
+
keys() {
|
|
122
|
+
const keys = [];
|
|
123
|
+
const now = Date.now();
|
|
124
|
+
for (const [key, entry] of this.cache.entries()) {
|
|
125
|
+
if (now <= entry.expiresAt) {
|
|
126
|
+
keys.push(key);
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
// Clean up expired entry
|
|
130
|
+
this.cache.delete(key);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return keys;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
exports.ModelCache = ModelCache;
|
|
137
|
+
// ============================================================================
|
|
138
|
+
// Global Cache Instance
|
|
139
|
+
// ============================================================================
|
|
140
|
+
/**
|
|
141
|
+
* Global model cache shared across all adapter instances.
|
|
142
|
+
*
|
|
143
|
+
* This is the default cache used when `modelsCacheScope: 'global'`.
|
|
144
|
+
* Reduces redundant API calls when multiple adapters share the same backend.
|
|
145
|
+
*/
|
|
146
|
+
exports.globalModelCache = new ModelCache();
|
|
147
|
+
// ============================================================================
|
|
148
|
+
// Cache Factory
|
|
149
|
+
// ============================================================================
|
|
150
|
+
/**
|
|
151
|
+
* Create or get model cache based on scope strategy.
|
|
152
|
+
*
|
|
153
|
+
* @param scope Cache scope strategy
|
|
154
|
+
* @returns Model cache instance
|
|
155
|
+
*/
|
|
156
|
+
function getModelCache(scope = 'global') {
|
|
157
|
+
if (scope === 'global') {
|
|
158
|
+
return exports.globalModelCache;
|
|
159
|
+
}
|
|
160
|
+
// Create new instance-scoped cache
|
|
161
|
+
return new ModelCache();
|
|
162
|
+
}
|
|
163
|
+
//# sourceMappingURL=model-cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model-cache.js","sourceRoot":"","sources":["../../src/model-cache.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;AAwLH,sCAOC;AAxKD,+EAA+E;AAC/E,6BAA6B;AAC7B,+EAA+E;AAE/E;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAa,UAAU;IACb,KAAK,CAA0B;IAEvC;QACE,IAAI,CAAC,KAAK,GAAG,IAAI,GAAG,EAAE,CAAC;IACzB,CAAC;IAED;;;;;OAKG;IACH,GAAG,CAAC,WAAmB;QACrB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