@elliotding/ai-agent-mcp 0.1.25 → 0.1.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (237) hide show
  1. package/package.json +4 -1
  2. package/.prompt-cache/cmd-cmd-client-sdk-ai-hub-generate-testcase.md +0 -101
  3. package/.prompt-cache/cmd-cmd-client-sdk-ai-hub-submit_zct_job.md +0 -158
  4. package/.prompt-cache/skill-skill-client-sdk-ai-hub-analyze-conf-status.md +0 -311
  5. package/.prompt-cache/skill-skill-client-sdk-ai-hub-analyze-sdk-log.md +0 -64
  6. package/.prompt-cache/skill-skill-client-sdk-ai-hub-analyze-zmb-log-errors.md +0 -84
  7. package/ai-resource-telemetry.json +0 -40
  8. package/dist/api/cached-client.d.ts +0 -48
  9. package/dist/api/cached-client.d.ts.map +0 -1
  10. package/dist/api/cached-client.js +0 -126
  11. package/dist/api/cached-client.js.map +0 -1
  12. package/dist/api/client.d.ts +0 -281
  13. package/dist/api/client.d.ts.map +0 -1
  14. package/dist/api/client.js +0 -371
  15. package/dist/api/client.js.map +0 -1
  16. package/dist/auth/index.d.ts +0 -8
  17. package/dist/auth/index.d.ts.map +0 -1
  18. package/dist/auth/index.js +0 -26
  19. package/dist/auth/index.js.map +0 -1
  20. package/dist/auth/middleware.d.ts +0 -36
  21. package/dist/auth/middleware.d.ts.map +0 -1
  22. package/dist/auth/middleware.js +0 -194
  23. package/dist/auth/middleware.js.map +0 -1
  24. package/dist/auth/permissions.d.ts +0 -60
  25. package/dist/auth/permissions.d.ts.map +0 -1
  26. package/dist/auth/permissions.js +0 -262
  27. package/dist/auth/permissions.js.map +0 -1
  28. package/dist/auth/token-validator.d.ts +0 -52
  29. package/dist/auth/token-validator.d.ts.map +0 -1
  30. package/dist/auth/token-validator.js +0 -215
  31. package/dist/auth/token-validator.js.map +0 -1
  32. package/dist/cache/cache-manager.d.ts +0 -49
  33. package/dist/cache/cache-manager.d.ts.map +0 -1
  34. package/dist/cache/cache-manager.js +0 -191
  35. package/dist/cache/cache-manager.js.map +0 -1
  36. package/dist/cache/index.d.ts +0 -6
  37. package/dist/cache/index.d.ts.map +0 -1
  38. package/dist/cache/index.js +0 -12
  39. package/dist/cache/index.js.map +0 -1
  40. package/dist/cache/redis-client.d.ts +0 -45
  41. package/dist/cache/redis-client.d.ts.map +0 -1
  42. package/dist/cache/redis-client.js +0 -210
  43. package/dist/cache/redis-client.js.map +0 -1
  44. package/dist/config/constants.d.ts +0 -28
  45. package/dist/config/constants.d.ts.map +0 -1
  46. package/dist/config/constants.js +0 -31
  47. package/dist/config/constants.js.map +0 -1
  48. package/dist/config/index.d.ts +0 -71
  49. package/dist/config/index.d.ts.map +0 -1
  50. package/dist/config/index.js +0 -190
  51. package/dist/config/index.js.map +0 -1
  52. package/dist/filesystem/manager.d.ts +0 -45
  53. package/dist/filesystem/manager.d.ts.map +0 -1
  54. package/dist/filesystem/manager.js +0 -246
  55. package/dist/filesystem/manager.js.map +0 -1
  56. package/dist/git/multi-source-manager.d.ts +0 -78
  57. package/dist/git/multi-source-manager.d.ts.map +0 -1
  58. package/dist/git/multi-source-manager.js +0 -577
  59. package/dist/git/multi-source-manager.js.map +0 -1
  60. package/dist/git/operations.d.ts +0 -27
  61. package/dist/git/operations.d.ts.map +0 -1
  62. package/dist/git/operations.js +0 -83
  63. package/dist/git/operations.js.map +0 -1
  64. package/dist/index.d.ts +0 -6
  65. package/dist/index.d.ts.map +0 -1
  66. package/dist/index.js +0 -122
  67. package/dist/index.js.map +0 -1
  68. package/dist/monitoring/health.d.ts +0 -35
  69. package/dist/monitoring/health.d.ts.map +0 -1
  70. package/dist/monitoring/health.js +0 -105
  71. package/dist/monitoring/health.js.map +0 -1
  72. package/dist/prompts/cache.d.ts +0 -69
  73. package/dist/prompts/cache.d.ts.map +0 -1
  74. package/dist/prompts/cache.js +0 -163
  75. package/dist/prompts/cache.js.map +0 -1
  76. package/dist/prompts/generator.d.ts +0 -49
  77. package/dist/prompts/generator.d.ts.map +0 -1
  78. package/dist/prompts/generator.js +0 -160
  79. package/dist/prompts/generator.js.map +0 -1
  80. package/dist/prompts/index.d.ts +0 -13
  81. package/dist/prompts/index.d.ts.map +0 -1
  82. package/dist/prompts/index.js +0 -24
  83. package/dist/prompts/index.js.map +0 -1
  84. package/dist/prompts/manager.d.ts +0 -207
  85. package/dist/prompts/manager.d.ts.map +0 -1
  86. package/dist/prompts/manager.js +0 -566
  87. package/dist/prompts/manager.js.map +0 -1
  88. package/dist/resources/index.d.ts +0 -6
  89. package/dist/resources/index.d.ts.map +0 -1
  90. package/dist/resources/index.js +0 -10
  91. package/dist/resources/index.js.map +0 -1
  92. package/dist/resources/loader.d.ts +0 -88
  93. package/dist/resources/loader.d.ts.map +0 -1
  94. package/dist/resources/loader.js +0 -492
  95. package/dist/resources/loader.js.map +0 -1
  96. package/dist/server/http.d.ts +0 -57
  97. package/dist/server/http.d.ts.map +0 -1
  98. package/dist/server/http.js +0 -435
  99. package/dist/server/http.js.map +0 -1
  100. package/dist/server.d.ts +0 -13
  101. package/dist/server.d.ts.map +0 -1
  102. package/dist/server.js +0 -201
  103. package/dist/server.js.map +0 -1
  104. package/dist/session/manager.d.ts +0 -91
  105. package/dist/session/manager.d.ts.map +0 -1
  106. package/dist/session/manager.js +0 -251
  107. package/dist/session/manager.js.map +0 -1
  108. package/dist/telemetry/index.d.ts +0 -3
  109. package/dist/telemetry/index.d.ts.map +0 -1
  110. package/dist/telemetry/index.js +0 -7
  111. package/dist/telemetry/index.js.map +0 -1
  112. package/dist/telemetry/manager.d.ts +0 -151
  113. package/dist/telemetry/manager.d.ts.map +0 -1
  114. package/dist/telemetry/manager.js +0 -367
  115. package/dist/telemetry/manager.js.map +0 -1
  116. package/dist/tools/index.d.ts +0 -13
  117. package/dist/tools/index.d.ts.map +0 -1
  118. package/dist/tools/index.js +0 -29
  119. package/dist/tools/index.js.map +0 -1
  120. package/dist/tools/manage-subscription.d.ts +0 -47
  121. package/dist/tools/manage-subscription.d.ts.map +0 -1
  122. package/dist/tools/manage-subscription.js +0 -317
  123. package/dist/tools/manage-subscription.js.map +0 -1
  124. package/dist/tools/registry.d.ts +0 -40
  125. package/dist/tools/registry.d.ts.map +0 -1
  126. package/dist/tools/registry.js +0 -85
  127. package/dist/tools/registry.js.map +0 -1
  128. package/dist/tools/resolve-prompt-content.d.ts +0 -35
  129. package/dist/tools/resolve-prompt-content.d.ts.map +0 -1
  130. package/dist/tools/resolve-prompt-content.js +0 -99
  131. package/dist/tools/resolve-prompt-content.js.map +0 -1
  132. package/dist/tools/search-resources.d.ts +0 -35
  133. package/dist/tools/search-resources.d.ts.map +0 -1
  134. package/dist/tools/search-resources.js +0 -159
  135. package/dist/tools/search-resources.js.map +0 -1
  136. package/dist/tools/sync-resources.d.ts +0 -54
  137. package/dist/tools/sync-resources.d.ts.map +0 -1
  138. package/dist/tools/sync-resources.js +0 -735
  139. package/dist/tools/sync-resources.js.map +0 -1
  140. package/dist/tools/track-usage.d.ts +0 -63
  141. package/dist/tools/track-usage.d.ts.map +0 -1
  142. package/dist/tools/track-usage.js +0 -90
  143. package/dist/tools/track-usage.js.map +0 -1
  144. package/dist/tools/uninstall-resource.d.ts +0 -30
  145. package/dist/tools/uninstall-resource.d.ts.map +0 -1
  146. package/dist/tools/uninstall-resource.js +0 -174
  147. package/dist/tools/uninstall-resource.js.map +0 -1
  148. package/dist/tools/upload-resource.d.ts +0 -81
  149. package/dist/tools/upload-resource.d.ts.map +0 -1
  150. package/dist/tools/upload-resource.js +0 -393
  151. package/dist/tools/upload-resource.js.map +0 -1
  152. package/dist/transport/sse.d.ts +0 -29
  153. package/dist/transport/sse.d.ts.map +0 -1
  154. package/dist/transport/sse.js +0 -271
  155. package/dist/transport/sse.js.map +0 -1
  156. package/dist/types/errors.d.ts +0 -60
  157. package/dist/types/errors.d.ts.map +0 -1
  158. package/dist/types/errors.js +0 -112
  159. package/dist/types/errors.js.map +0 -1
  160. package/dist/types/index.d.ts +0 -7
  161. package/dist/types/index.d.ts.map +0 -1
  162. package/dist/types/index.js +0 -23
  163. package/dist/types/index.js.map +0 -1
  164. package/dist/types/mcp.d.ts +0 -50
  165. package/dist/types/mcp.d.ts.map +0 -1
  166. package/dist/types/mcp.js +0 -6
  167. package/dist/types/mcp.js.map +0 -1
  168. package/dist/types/resources.d.ts +0 -109
  169. package/dist/types/resources.d.ts.map +0 -1
  170. package/dist/types/resources.js +0 -7
  171. package/dist/types/resources.js.map +0 -1
  172. package/dist/types/tools.d.ts +0 -253
  173. package/dist/types/tools.d.ts.map +0 -1
  174. package/dist/types/tools.js +0 -6
  175. package/dist/types/tools.js.map +0 -1
  176. package/dist/utils/cursor-paths.d.ts +0 -84
  177. package/dist/utils/cursor-paths.d.ts.map +0 -1
  178. package/dist/utils/cursor-paths.js +0 -166
  179. package/dist/utils/cursor-paths.js.map +0 -1
  180. package/dist/utils/log-cleaner.d.ts +0 -18
  181. package/dist/utils/log-cleaner.d.ts.map +0 -1
  182. package/dist/utils/log-cleaner.js +0 -112
  183. package/dist/utils/log-cleaner.js.map +0 -1
  184. package/dist/utils/logger.d.ts +0 -59
  185. package/dist/utils/logger.d.ts.map +0 -1
  186. package/dist/utils/logger.js +0 -292
  187. package/dist/utils/logger.js.map +0 -1
  188. package/dist/utils/validation.d.ts +0 -58
  189. package/dist/utils/validation.d.ts.map +0 -1
  190. package/dist/utils/validation.js +0 -214
  191. package/dist/utils/validation.js.map +0 -1
  192. package/src/api/cached-client.ts +0 -144
  193. package/src/api/client.ts +0 -697
  194. package/src/auth/index.ts +0 -11
  195. package/src/auth/middleware.ts +0 -244
  196. package/src/auth/permissions.ts +0 -323
  197. package/src/auth/token-validator.ts +0 -292
  198. package/src/cache/cache-manager.ts +0 -243
  199. package/src/cache/index.ts +0 -6
  200. package/src/cache/redis-client.ts +0 -249
  201. package/src/config/constants.ts +0 -33
  202. package/src/config/index.ts +0 -269
  203. package/src/filesystem/manager.ts +0 -235
  204. package/src/git/multi-source-manager.ts +0 -654
  205. package/src/git/operations.ts +0 -93
  206. package/src/index.ts +0 -157
  207. package/src/monitoring/health.ts +0 -132
  208. package/src/prompts/cache.ts +0 -140
  209. package/src/prompts/generator.ts +0 -143
  210. package/src/prompts/index.ts +0 -20
  211. package/src/prompts/manager.ts +0 -718
  212. package/src/resources/index.ts +0 -13
  213. package/src/resources/loader.ts +0 -563
  214. package/src/server/http.ts +0 -549
  215. package/src/server.ts +0 -206
  216. package/src/session/manager.ts +0 -296
  217. package/src/telemetry/index.ts +0 -10
  218. package/src/telemetry/manager.ts +0 -419
  219. package/src/tools/index.ts +0 -13
  220. package/src/tools/manage-subscription.ts +0 -388
  221. package/src/tools/registry.ts +0 -97
  222. package/src/tools/resolve-prompt-content.ts +0 -113
  223. package/src/tools/search-resources.ts +0 -185
  224. package/src/tools/sync-resources.ts +0 -829
  225. package/src/tools/track-usage.ts +0 -113
  226. package/src/tools/uninstall-resource.ts +0 -199
  227. package/src/tools/upload-resource.ts +0 -431
  228. package/src/transport/sse.ts +0 -308
  229. package/src/types/errors.ts +0 -146
  230. package/src/types/index.ts +0 -7
  231. package/src/types/mcp.ts +0 -61
  232. package/src/types/resources.ts +0 -141
  233. package/src/types/tools.ts +0 -305
  234. package/src/utils/cursor-paths.ts +0 -135
  235. package/src/utils/log-cleaner.ts +0 -92
  236. package/src/utils/logger.ts +0 -333
  237. package/src/utils/validation.ts +0 -262
@@ -1,292 +0,0 @@
1
- /**
2
- * Token Validation via CSP API
3
- * Validates tokens by calling CSP /user/permissions endpoint
4
- */
5
-
6
- import { apiClient } from '../api/client';
7
- import { logger, logError, logAuthAttempt } from '../utils/logger';
8
-
9
- /**
10
- * Token validation payload structure
11
- */
12
- export interface TokenPayload {
13
- userId: string;
14
- email: string;
15
- groups: string[]; // Changed from 'roles' to 'groups' to match API response
16
- // Additional fields for compatibility
17
- roles?: string[]; // Alias for groups
18
- [key: string]: unknown;
19
- }
20
-
21
- /**
22
- * CSP API response for /user/permissions
23
- */
24
- interface PermissionsResponse {
25
- code: number;
26
- data?: {
27
- user_id: string;
28
- email: string;
29
- groups: string[];
30
- };
31
- message?: string;
32
- }
33
-
34
- /**
35
- * Token validation cache (in-memory, 5 minute TTL)
36
- */
37
- const tokenCache = new Map<string, { payload: TokenPayload; expireAt: number }>();
38
-
39
- /**
40
- * Cache cleanup interval reference (for cleanup on shutdown)
41
- */
42
- let cacheCleanupInterval: NodeJS.Timeout | null = null;
43
-
44
- /**
45
- * Clean expired cache entries
46
- */
47
- function cleanExpiredCache(): void {
48
- const now = Date.now();
49
- let cleaned = 0;
50
-
51
- for (const [token, entry] of tokenCache.entries()) {
52
- if (entry.expireAt < now) {
53
- tokenCache.delete(token);
54
- cleaned++;
55
- }
56
- }
57
-
58
- if (cleaned > 0) {
59
- logger.debug(
60
- { type: 'cache_cleanup', cleaned, remaining: tokenCache.size },
61
- `Cleaned ${cleaned} expired token(s) from cache`
62
- );
63
- }
64
- }
65
-
66
- /**
67
- * Start cache cleanup interval
68
- */
69
- export function startCacheCleanup(): void {
70
- if (cacheCleanupInterval) {
71
- logger.warn('Cache cleanup interval already running');
72
- return;
73
- }
74
-
75
- // Clean cache every minute
76
- cacheCleanupInterval = setInterval(cleanExpiredCache, 60000);
77
- logger.info('Token cache cleanup interval started (60s)');
78
- }
79
-
80
- /**
81
- * Stop cache cleanup interval
82
- */
83
- export function stopCacheCleanup(): void {
84
- if (cacheCleanupInterval) {
85
- clearInterval(cacheCleanupInterval);
86
- cacheCleanupInterval = null;
87
- logger.info('Token cache cleanup interval stopped');
88
- }
89
- }
90
-
91
- // Start cleanup on module load
92
- startCacheCleanup();
93
-
94
- /**
95
- * Verify token by calling CSP API /user/permissions
96
- * @param token - The JWT token to verify
97
- * @returns Token payload if valid, null otherwise
98
- */
99
- export async function verifyTokenViaAPI(token: string): Promise<TokenPayload | null> {
100
- const tokenPreview = token.substring(0, 10) + '...' + token.substring(token.length - 10);
101
- const startTime = Date.now();
102
-
103
- try {
104
- logger.debug(
105
- {
106
- type: 'auth',
107
- operation: 'verify_token_api',
108
- tokenPreview,
109
- timestamp: new Date().toISOString()
110
- },
111
- 'Calling CSP API /user/permissions to validate token'
112
- );
113
-
114
- // Call CSP API to validate the token presented in the SSE Authorization header.
115
- const response = await apiClient.get<PermissionsResponse>(
116
- '/csp/api/user/permissions',
117
- {
118
- headers: {
119
- 'Authorization': `Bearer ${token}`,
120
- },
121
- timeout: 5000, // 5 second timeout for auth check
122
- }
123
- );
124
-
125
- const duration = Date.now() - startTime;
126
-
127
- // Check response code (2000 means success)
128
- if (response.code === 2000 && response.data) {
129
- const payload: TokenPayload = {
130
- userId: response.data.user_id,
131
- email: response.data.email,
132
- groups: response.data.groups || [],
133
- roles: response.data.groups || [], // Alias for backward compatibility
134
- };
135
-
136
- logger.info(
137
- {
138
- type: 'auth',
139
- operation: 'verify_token_api',
140
- userId: payload.userId,
141
- email: payload.email,
142
- groups: payload.groups,
143
- duration,
144
- timestamp: new Date().toISOString()
145
- },
146
- `Token validated successfully for user ${payload.userId}`
147
- );
148
-
149
- logAuthAttempt('token_validation', true, {
150
- userId: payload.userId,
151
- email: payload.email,
152
- groups: payload.groups,
153
- duration
154
- });
155
-
156
- return payload;
157
- }
158
-
159
- logger.warn(
160
- {
161
- type: 'auth',
162
- operation: 'verify_token_api',
163
- code: response.code,
164
- message: response.message,
165
- tokenPreview,
166
- duration,
167
- timestamp: new Date().toISOString()
168
- },
169
- 'Token validation failed - invalid or expired token'
170
- );
171
-
172
- logAuthAttempt('token_validation', false, {
173
- code: response.code,
174
- message: response.message,
175
- duration
176
- });
177
-
178
- return null;
179
- } catch (error) {
180
- const duration = Date.now() - startTime;
181
-
182
- logError(error as Error, {
183
- type: 'auth',
184
- operation: 'verify_token_api',
185
- tokenPreview,
186
- duration,
187
- timestamp: new Date().toISOString()
188
- });
189
-
190
- logAuthAttempt('token_validation', false, {
191
- error: error instanceof Error ? error.message : String(error),
192
- duration
193
- });
194
-
195
- return null;
196
- }
197
- }
198
-
199
- /**
200
- * Verify token with caching
201
- * Uses cached result if available to reduce API calls
202
- * @param token - The token to verify
203
- * @returns Token payload if valid, null otherwise
204
- */
205
- export async function verifyToken(token: string): Promise<TokenPayload | null> {
206
- const tokenPreview = token.substring(0, 10) + '...' + token.substring(token.length - 10);
207
-
208
- // Check cache first
209
- const cached = tokenCache.get(token);
210
- if (cached && cached.expireAt > Date.now()) {
211
- logger.debug(
212
- {
213
- type: 'auth',
214
- operation: 'verify_token',
215
- userId: cached.payload.userId,
216
- email: cached.payload.email,
217
- cacheHit: true,
218
- tokenPreview,
219
- timestamp: new Date().toISOString()
220
- },
221
- 'Token validation cache hit'
222
- );
223
- return cached.payload;
224
- }
225
-
226
- logger.debug(
227
- {
228
- type: 'auth',
229
- operation: 'verify_token',
230
- cacheHit: false,
231
- tokenPreview,
232
- timestamp: new Date().toISOString()
233
- },
234
- 'Token validation cache miss, calling API'
235
- );
236
-
237
- // Validate via API
238
- const payload = await verifyTokenViaAPI(token);
239
-
240
- // Cache the result if valid (5 minute TTL)
241
- if (payload) {
242
- const expireAt = Date.now() + 5 * 60 * 1000; // 5 minutes
243
- tokenCache.set(token, { payload, expireAt });
244
- logger.debug(
245
- {
246
- type: 'auth',
247
- operation: 'verify_token',
248
- userId: payload.userId,
249
- email: payload.email,
250
- cacheTTL: '5min',
251
- timestamp: new Date().toISOString()
252
- },
253
- 'Token validation result cached (5 min TTL)'
254
- );
255
- }
256
-
257
- return payload;
258
- }
259
-
260
- /**
261
- * Clear token from cache (e.g., after logout)
262
- * @param token - The token to invalidate
263
- */
264
- export function invalidateToken(token: string): void {
265
- tokenCache.delete(token);
266
- logger.debug(
267
- { type: 'auth', operation: 'invalidate_token' },
268
- 'Token removed from cache'
269
- );
270
- }
271
-
272
- /**
273
- * Clear all cached tokens
274
- */
275
- export function clearTokenCache(): void {
276
- tokenCache.clear();
277
- logger.info(
278
- { type: 'auth', operation: 'clear_cache' },
279
- 'All cached tokens cleared'
280
- );
281
- }
282
-
283
- /**
284
- * Get cache statistics
285
- */
286
- export function getTokenCacheStats() {
287
- cleanExpiredCache();
288
- return {
289
- size: tokenCache.size,
290
- tokens: Array.from(tokenCache.keys()).map(t => t.substring(0, 10) + '...'),
291
- };
292
- }
@@ -1,243 +0,0 @@
1
- /**
2
- * Multi-Layer Cache Manager
3
- * L1: In-memory LRU cache
4
- * L2: Redis persistent cache
5
- */
6
-
7
- import { LRUCache } from 'lru-cache';
8
- import { config } from '../config';
9
- import { logger } from '../utils/logger';
10
- import { redisClient } from './redis-client';
11
-
12
- const CACHE_KEY_PREFIX = 'csp:cache';
13
-
14
- /** JSON-serializable cache value type (satisfies LRUCache V extends {}) */
15
- type CacheValue = object | string | number | boolean;
16
-
17
- export interface CacheStats {
18
- l1Hits: number;
19
- l2Hits: number;
20
- misses: number;
21
- hitRate: number;
22
- }
23
-
24
- export class CacheManager {
25
- private static instance: CacheManager | null = null;
26
- private readonly l1: LRUCache<string, CacheValue>;
27
- private readonly l2: typeof redisClient;
28
- private readonly defaultTtlSeconds: number;
29
- private readonly defaultNamespace: string;
30
-
31
- private l1Hits = 0;
32
- private l2Hits = 0;
33
- private misses = 0;
34
-
35
- private constructor(options?: { namespace?: string }) {
36
- const ttlMs = (config.cache.redis?.ttl ?? 900) * 1000;
37
- this.defaultTtlSeconds = config.cache.redis?.ttl ?? 900;
38
- this.defaultNamespace = options?.namespace ?? 'default';
39
-
40
- this.l1 = new LRUCache<string, CacheValue>({
41
- max: 100,
42
- ttl: ttlMs,
43
- ttlAutopurge: true,
44
- });
45
-
46
- this.l2 = redisClient;
47
-
48
- logger.info(
49
- {
50
- type: 'cache',
51
- message: 'CacheManager initialized',
52
- l1Max: 100,
53
- ttlSeconds: this.defaultTtlSeconds,
54
- namespace: this.defaultNamespace,
55
- },
56
- 'Multi-layer cache initialized'
57
- );
58
- }
59
-
60
- static getInstance(options?: { namespace?: string }): CacheManager {
61
- if (CacheManager.instance === null) {
62
- CacheManager.instance = new CacheManager(options);
63
- }
64
- return CacheManager.instance;
65
- }
66
-
67
- static async resetInstance(): Promise<void> {
68
- if (CacheManager.instance) {
69
- await CacheManager.instance.clear();
70
- await CacheManager.instance.l2.disconnect();
71
- CacheManager.instance = null;
72
- }
73
- }
74
-
75
- private buildKey(key: string, namespace?: string): string {
76
- const ns = namespace ?? this.defaultNamespace;
77
- return `${CACHE_KEY_PREFIX}:${ns}:${key}`;
78
- }
79
-
80
- private getRedisPattern(namespace?: string): string {
81
- const ns = namespace ?? this.defaultNamespace;
82
- return `${CACHE_KEY_PREFIX}:${ns}:*`;
83
- }
84
-
85
- async connect(): Promise<void> {
86
- await this.l2.connect();
87
- }
88
-
89
- /**
90
- * Get value from cache. Checks L1 first, then L2.
91
- * On L2 hit, promotes value to L1.
92
- */
93
- async get(key: string, namespace?: string): Promise<unknown | null> {
94
- const fullKey = this.buildKey(key, namespace);
95
-
96
- const l1Value = this.l1.get(fullKey);
97
- if (l1Value !== undefined) {
98
- this.l1Hits++;
99
- logger.debug({ type: 'cache', key: fullKey, layer: 'L1' }, 'Cache L1 hit');
100
- return l1Value;
101
- }
102
-
103
- let l2Value: string | null = null;
104
- try {
105
- l2Value = await this.l2.get(fullKey);
106
- } catch (error) {
107
- logger.debug(
108
- { type: 'cache', key: fullKey, error: (error as Error).message },
109
- 'L2 get failed, Redis may be unavailable'
110
- );
111
- }
112
-
113
- if (l2Value !== null) {
114
- this.l2Hits++;
115
- try {
116
- const parsed = JSON.parse(l2Value) as unknown;
117
- if (parsed !== null && parsed !== undefined) {
118
- this.l1.set(fullKey, parsed as CacheValue);
119
- }
120
- logger.debug({ type: 'cache', key: fullKey, layer: 'L2' }, 'Cache L2 hit, promoted to L1');
121
- return parsed;
122
- } catch (error) {
123
- logger.warn(
124
- { type: 'cache', key: fullKey, error: (error as Error).message },
125
- 'Failed to parse L2 cache value'
126
- );
127
- try {
128
- await this.l2.del(fullKey);
129
- } catch {
130
- /* Redis may be unavailable */
131
- }
132
- this.misses++;
133
- return null;
134
- }
135
- }
136
-
137
- this.misses++;
138
- logger.debug({ type: 'cache', key: fullKey }, 'Cache miss');
139
- return null;
140
- }
141
-
142
- /**
143
- * Set value in both L1 and L2 caches.
144
- */
145
- async set(
146
- key: string,
147
- value: unknown,
148
- ttl?: number,
149
- namespace?: string
150
- ): Promise<void> {
151
- const fullKey = this.buildKey(key, namespace);
152
- const ttlSeconds = ttl ?? this.defaultTtlSeconds;
153
- const ttlMs = ttlSeconds * 1000;
154
-
155
- if (value !== null && value !== undefined) {
156
- this.l1.set(fullKey, value as CacheValue, { ttl: ttlMs });
157
- }
158
-
159
- const serialized = JSON.stringify(value);
160
- try {
161
- await this.l2.set(fullKey, serialized, ttlSeconds);
162
- } catch (error) {
163
- logger.debug(
164
- { type: 'cache', key: fullKey, error: (error as Error).message },
165
- 'L2 set failed, Redis may be unavailable'
166
- );
167
- }
168
-
169
- logger.debug(
170
- { type: 'cache', key: fullKey, ttlSeconds },
171
- 'Cache set'
172
- );
173
- }
174
-
175
- /**
176
- * Delete key from both caches.
177
- */
178
- async del(key: string, namespace?: string): Promise<void> {
179
- const fullKey = this.buildKey(key, namespace);
180
- this.l1.delete(fullKey);
181
- try {
182
- await this.l2.del(fullKey);
183
- } catch (error) {
184
- logger.debug(
185
- { type: 'cache', key: fullKey, error: (error as Error).message },
186
- 'L2 del failed, Redis may be unavailable'
187
- );
188
- }
189
- logger.debug({ type: 'cache', key: fullKey }, 'Cache del');
190
- }
191
-
192
- /**
193
- * Clear all cache layers. If namespace provided, clear only that namespace.
194
- */
195
- async clear(namespace?: string): Promise<void> {
196
- if (namespace !== undefined) {
197
- const pattern = this.getRedisPattern(namespace);
198
- const prefix = `${CACHE_KEY_PREFIX}:${namespace}:`;
199
- for (const k of this.l1.keys()) {
200
- if (k.startsWith(prefix)) {
201
- this.l1.delete(k);
202
- }
203
- }
204
- try {
205
- await this.l2.clear(pattern);
206
- } catch (error) {
207
- logger.debug(
208
- { type: 'cache', namespace, error: (error as Error).message },
209
- 'L2 clear failed, Redis may be unavailable'
210
- );
211
- }
212
- logger.info({ type: 'cache', namespace }, 'Cache cleared for namespace');
213
- } else {
214
- this.l1.clear();
215
- try {
216
- await this.l2.clear(`${CACHE_KEY_PREFIX}:*`);
217
- } catch (error) {
218
- logger.debug(
219
- { type: 'cache', error: (error as Error).message },
220
- 'L2 clear failed, Redis may be unavailable'
221
- );
222
- }
223
- logger.info({ type: 'cache' }, 'Cache cleared');
224
- }
225
- }
226
-
227
- getStats(): CacheStats {
228
- const total = this.l1Hits + this.l2Hits + this.misses;
229
- const hitRate = total > 0 ? (this.l1Hits + this.l2Hits) / total : 0;
230
- return {
231
- l1Hits: this.l1Hits,
232
- l2Hits: this.l2Hits,
233
- misses: this.misses,
234
- hitRate,
235
- };
236
- }
237
-
238
- resetStats(): void {
239
- this.l1Hits = 0;
240
- this.l2Hits = 0;
241
- this.misses = 0;
242
- }
243
- }
@@ -1,6 +0,0 @@
1
- /**
2
- * Cache module exports
3
- */
4
-
5
- export { CacheManager, type CacheStats } from './cache-manager';
6
- export { redisClient, RedisClient } from './redis-client';