universal-agent-memory 0.7.3 → 0.7.4

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 (34) hide show
  1. package/dist/cli/memory.js +27 -0
  2. package/dist/cli/memory.js.map +1 -1
  3. package/dist/cli/update.d.ts.map +1 -1
  4. package/dist/cli/update.js +2 -2
  5. package/dist/cli/update.js.map +1 -1
  6. package/dist/utils/calculate-average.d.ts +15 -0
  7. package/dist/utils/calculate-average.d.ts.map +1 -0
  8. package/dist/utils/calculate-average.js +21 -0
  9. package/dist/utils/calculate-average.js.map +1 -0
  10. package/dist/utils/config-manager.d.ts +30 -0
  11. package/dist/utils/config-manager.d.ts.map +1 -0
  12. package/dist/utils/config-manager.js +41 -0
  13. package/dist/utils/config-manager.js.map +1 -0
  14. package/dist/utils/fetch-with-retry.d.ts +5 -0
  15. package/dist/utils/fetch-with-retry.d.ts.map +1 -0
  16. package/dist/utils/fetch-with-retry.js +61 -0
  17. package/dist/utils/fetch-with-retry.js.map +1 -0
  18. package/dist/utils/order-processor-refactored.d.ts +126 -0
  19. package/dist/utils/order-processor-refactored.d.ts.map +1 -0
  20. package/dist/utils/order-processor-refactored.js +165 -0
  21. package/dist/utils/order-processor-refactored.js.map +1 -0
  22. package/dist/utils/order-processor-strategy.d.ts +72 -0
  23. package/dist/utils/order-processor-strategy.d.ts.map +1 -0
  24. package/dist/utils/order-processor-strategy.js +158 -0
  25. package/dist/utils/order-processor-strategy.js.map +1 -0
  26. package/dist/utils/order-processor.d.ts +242 -0
  27. package/dist/utils/order-processor.d.ts.map +1 -0
  28. package/dist/utils/order-processor.js +370 -0
  29. package/dist/utils/order-processor.js.map +1 -0
  30. package/dist/utils/rate-limiter-simple.d.ts +58 -0
  31. package/dist/utils/rate-limiter-simple.d.ts.map +1 -0
  32. package/dist/utils/rate-limiter-simple.js +100 -0
  33. package/dist/utils/rate-limiter-simple.js.map +1 -0
  34. package/package.json +1 -1
@@ -0,0 +1,370 @@
1
+ /**
2
+ * Order Processing System - SOLID Principles Implementation
3
+ *
4
+ * This module demonstrates a refactored order processing system using:
5
+ * - Single Responsibility Principle: Each strategy handles one order type
6
+ * - Open/Closed Principle: New order types can be added without modifying existing code
7
+ * - Liskov Substitution Principle: All strategies can be used interchangeably
8
+ * - Interface Segregation Principle: Focused interfaces for specific needs
9
+ * - Dependency Inversion Principle: Depends on abstractions (interfaces) not concretions
10
+ */
11
+ import { z } from 'zod';
12
+ /**
13
+ * Order type enum
14
+ */
15
+ export var OrderType;
16
+ (function (OrderType) {
17
+ OrderType["DIGITAL"] = "digital";
18
+ OrderType["PHYSICAL"] = "physical";
19
+ OrderType["SUBSCRIPTION"] = "subscription";
20
+ })(OrderType || (OrderType = {}));
21
+ /**
22
+ * Order status enum
23
+ */
24
+ export var OrderStatus;
25
+ (function (OrderStatus) {
26
+ OrderStatus["PENDING"] = "pending";
27
+ OrderStatus["DELIVERED"] = "delivered";
28
+ OrderStatus["SHIPPED"] = "shipped";
29
+ OrderStatus["ACTIVE"] = "active";
30
+ })(OrderStatus || (OrderStatus = {}));
31
+ /**
32
+ * Order validation schema using Zod
33
+ */
34
+ export const OrderSchema = z.object({
35
+ id: z.string().min(1, 'Order ID is required'),
36
+ type: z.nativeEnum(OrderType),
37
+ status: z.nativeEnum(OrderStatus).default(OrderStatus.PENDING),
38
+ customerId: z.string().min(1, 'Customer ID is required'),
39
+ items: z.array(z.object({
40
+ productId: z.string().min(1, 'Product ID is required'),
41
+ quantity: z.number().int().positive('Quantity must be positive'),
42
+ price: z.number().positive('Price must be positive'),
43
+ })).min(1, 'At least one item is required'),
44
+ createdAt: z.date(),
45
+ updatedAt: z.date(),
46
+ });
47
+ /**
48
+ * Digital order processor strategy
49
+ * Handles delivery of digital products via email
50
+ */
51
+ export class DigitalOrderProcessor {
52
+ /**
53
+ * Process a digital order
54
+ * @param order - The digital order to process
55
+ * @returns Processing result with delivered status
56
+ * @throws {Error} If email delivery fails
57
+ */
58
+ async process(order) {
59
+ try {
60
+ // Send email with download link
61
+ await this.sendDownloadLinkEmail(order);
62
+ const processedOrder = {
63
+ ...order,
64
+ status: OrderStatus.DELIVERED,
65
+ updatedAt: new Date(),
66
+ };
67
+ console.log(`Digital order ${order.id} delivered successfully`);
68
+ return {
69
+ success: true,
70
+ order: processedOrder,
71
+ message: `Digital order ${order.id} has been delivered via email`,
72
+ };
73
+ }
74
+ catch (error) {
75
+ console.error(`Failed to process digital order ${order.id}:`, error);
76
+ throw new Error(`Failed to process digital order: ${error instanceof Error ? error.message : 'Unknown error'}`);
77
+ }
78
+ }
79
+ /**
80
+ * Validate digital order requirements
81
+ * @param order - The order to validate
82
+ * @returns true if valid for digital processing
83
+ */
84
+ validate(order) {
85
+ return order.type === OrderType.DIGITAL && order.status === OrderStatus.PENDING;
86
+ }
87
+ /**
88
+ * Send download link email to customer
89
+ * @param order - The order containing customer information
90
+ * @throws {Error} If email cannot be sent
91
+ */
92
+ async sendDownloadLinkEmail(order) {
93
+ // Simulate email sending
94
+ console.log(`Sending email with download link for order ${order.id} to customer ${order.customerId}`);
95
+ // In production: await emailService.sendDownloadLink(order.customerId, order.items);
96
+ }
97
+ }
98
+ /**
99
+ * Physical order processor strategy
100
+ * Handles shipping of physical products
101
+ */
102
+ export class PhysicalOrderProcessor {
103
+ /**
104
+ * Process a physical order
105
+ * @param order - The physical order to process
106
+ * @returns Processing result with shipped status
107
+ * @throws {Error} If shipping label creation fails
108
+ */
109
+ async process(order) {
110
+ try {
111
+ // Create shipping label
112
+ await this.createShippingLabel(order);
113
+ const processedOrder = {
114
+ ...order,
115
+ status: OrderStatus.SHIPPED,
116
+ updatedAt: new Date(),
117
+ };
118
+ console.log(`Physical order ${order.id} shipped successfully`);
119
+ return {
120
+ success: true,
121
+ order: processedOrder,
122
+ message: `Physical order ${order.id} has been shipped`,
123
+ };
124
+ }
125
+ catch (error) {
126
+ console.error(`Failed to process physical order ${order.id}:`, error);
127
+ throw new Error(`Failed to process physical order: ${error instanceof Error ? error.message : 'Unknown error'}`);
128
+ }
129
+ }
130
+ /**
131
+ * Validate physical order requirements
132
+ * @param order - The order to validate
133
+ * @returns true if valid for physical processing
134
+ */
135
+ validate(order) {
136
+ return order.type === OrderType.PHYSICAL && order.status === OrderStatus.PENDING;
137
+ }
138
+ /**
139
+ * Create shipping label for the order
140
+ * @param order - The order to create label for
141
+ * @throws {Error} If shipping label cannot be created
142
+ */
143
+ async createShippingLabel(_order) {
144
+ // Intentionally unused in this stub implementation
145
+ console.log('Creating shipping label');
146
+ // In production: await shippingService.createLabel(order);
147
+ }
148
+ }
149
+ /**
150
+ * Subscription order processor strategy
151
+ * Handles activation of subscription services
152
+ */
153
+ export class SubscriptionOrderProcessor {
154
+ /**
155
+ * Process a subscription order
156
+ * @param order - The subscription order to process
157
+ * @returns Processing result with active status
158
+ * @throws {Error} If subscription activation fails
159
+ */
160
+ async process(order) {
161
+ try {
162
+ // Activate subscription
163
+ await this.activateSubscription(order);
164
+ const processedOrder = {
165
+ ...order,
166
+ status: OrderStatus.ACTIVE,
167
+ updatedAt: new Date(),
168
+ };
169
+ console.log(`Subscription order ${order.id} activated successfully`);
170
+ return {
171
+ success: true,
172
+ order: processedOrder,
173
+ message: `Subscription order ${order.id} has been activated`,
174
+ };
175
+ }
176
+ catch (error) {
177
+ console.error(`Failed to process subscription order ${order.id}:`, error);
178
+ throw new Error(`Failed to process subscription order: ${error instanceof Error ? error.message : 'Unknown error'}`);
179
+ }
180
+ }
181
+ /**
182
+ * Validate subscription order requirements
183
+ * @param order - The order to validate
184
+ * @returns true if valid for subscription processing
185
+ */
186
+ validate(order) {
187
+ return order.type === OrderType.SUBSCRIPTION && order.status === OrderStatus.PENDING;
188
+ }
189
+ /**
190
+ * Activate subscription service
191
+ * @param order - The order containing subscription details
192
+ * @throws {Error} If subscription cannot be activated
193
+ */
194
+ async activateSubscription(_order) {
195
+ // Intentionally unused in this stub implementation
196
+ console.log('Activating subscription');
197
+ // In production: await subscriptionService.activate(order.customerId, order.items);
198
+ }
199
+ }
200
+ /**
201
+ * OrderProcessor factory
202
+ * Responsible for selecting the appropriate processor based on order type
203
+ * Follows Factory pattern for dependency injection
204
+ */
205
+ export class OrderProcessorFactory {
206
+ processors;
207
+ constructor() {
208
+ // Register all available processors
209
+ this.processors = newMap([
210
+ [OrderType.DIGITAL, new DigitalOrderProcessor()],
211
+ [OrderType.PHYSICAL, new PhysicalOrderProcessor()],
212
+ [OrderType.SUBSCRIPTION, new SubscriptionOrderProcessor()],
213
+ ]);
214
+ }
215
+ /**
216
+ * Get the appropriate processor for the given order type
217
+ * @param orderType - The type of order
218
+ * @returns The appropriate order processor
219
+ * @throws {Error} If no processor is found for the order type
220
+ */
221
+ getProcessor(orderType) {
222
+ const processor = this.processors.get(orderType);
223
+ if (!processor) {
224
+ throw new Error(`No processor found for order type: ${orderType}`);
225
+ }
226
+ return processor;
227
+ }
228
+ /**
229
+ * Register a new processor for a specific order type
230
+ * @param orderType - The order type to handle
231
+ * @param processor - The processor implementation
232
+ */
233
+ registerProcessor(orderType, processor) {
234
+ this.processors.set(orderType, processor);
235
+ }
236
+ /**
237
+ * Check if a processor is registered for the given order type
238
+ * @param orderType - The order type to check
239
+ * @returns true if a processor exists
240
+ */
241
+ hasProcessor(orderType) {
242
+ return this.processors.has(orderType);
243
+ }
244
+ }
245
+ /**
246
+ * Helper function to create a typed Map
247
+ * @param entries - Initial entries for the Map
248
+ * @returns A new Map with the specified entries
249
+ */
250
+ function newMap(entries) {
251
+ return new Map(entries);
252
+ }
253
+ /**
254
+ * OrderProcessingService
255
+ * Main service that coordinates order processing using the appropriate strategy
256
+ */
257
+ export class OrderProcessingService {
258
+ processorFactory;
259
+ constructor(processorFactory) {
260
+ this.processorFactory = processorFactory || new OrderProcessorFactory();
261
+ }
262
+ /**
263
+ * Process an order using the appropriate strategy
264
+ * @param orderInput - The order data to process (may be unvalidated)
265
+ * @returns Processing result with updated order
266
+ * @throws {Error} If validation fails or processing encounters an error
267
+ */
268
+ async processOrder(orderInput) {
269
+ try {
270
+ // Validate input using Zod schema
271
+ const order = OrderSchema.parse(orderInput);
272
+ console.log(`Processing order ${order.id} of type ${order.type}`);
273
+ // Get the appropriate processor
274
+ const processor = this.processorFactory.getProcessor(order.type);
275
+ // Validate that the order can be processed
276
+ if (!processor.validate(order)) {
277
+ throw new Error(`Order ${order.id} with status ${order.status} cannot be processed`);
278
+ }
279
+ // Process the order using the selected strategy
280
+ const result = await processor.process(order);
281
+ console.log(result.message);
282
+ return result;
283
+ }
284
+ catch (error) {
285
+ if (error instanceof z.ZodError) {
286
+ console.error('Validation error:', error.errors);
287
+ throw new Error(`Order validation failed: ${error.errors.map(e => e.message).join(', ')}`);
288
+ }
289
+ console.error('Order processing error:', error);
290
+ throw error;
291
+ }
292
+ }
293
+ }
294
+ /**
295
+ * Example usage demonstrating the refactored code
296
+ */
297
+ export async function exampleUsage() {
298
+ // Initialize the service
299
+ const orderService = new OrderProcessingService();
300
+ // Example 1: Digital order
301
+ const digitalOrder = {
302
+ id: 'ORD-001',
303
+ type: OrderType.DIGITAL,
304
+ status: OrderStatus.PENDING,
305
+ customerId: 'CUST-123',
306
+ items: [
307
+ {
308
+ productId: 'PROD-DIG-001',
309
+ quantity: 1,
310
+ price: 29.99,
311
+ },
312
+ ],
313
+ createdAt: new Date(),
314
+ updatedAt: new Date(),
315
+ };
316
+ try {
317
+ const result1 = await orderService.processOrder(digitalOrder);
318
+ console.log('Result:', result1);
319
+ }
320
+ catch (error) {
321
+ console.error('Error:', error);
322
+ }
323
+ // Example 2: Physical order
324
+ const physicalOrder = {
325
+ id: 'ORD-002',
326
+ type: OrderType.PHYSICAL,
327
+ status: OrderStatus.PENDING,
328
+ customerId: 'CUST-456',
329
+ items: [
330
+ {
331
+ productId: 'PROD-PHY-001',
332
+ quantity: 2,
333
+ price: 49.99,
334
+ },
335
+ ],
336
+ createdAt: new Date(),
337
+ updatedAt: new Date(),
338
+ };
339
+ try {
340
+ const result2 = await orderService.processOrder(physicalOrder);
341
+ console.log('Result:', result2);
342
+ }
343
+ catch (error) {
344
+ console.error('Error:', error);
345
+ }
346
+ // Example 3: Subscription order
347
+ const subscriptionOrder = {
348
+ id: 'ORD-003',
349
+ type: OrderType.SUBSCRIPTION,
350
+ status: OrderStatus.PENDING,
351
+ customerId: 'CUST-789',
352
+ items: [
353
+ {
354
+ productId: 'PROD-SUB-001',
355
+ quantity: 1,
356
+ price: 19.99,
357
+ },
358
+ ],
359
+ createdAt: new Date(),
360
+ updatedAt: new Date(),
361
+ };
362
+ try {
363
+ const result3 = await orderService.processOrder(subscriptionOrder);
364
+ console.log('Result:', result3);
365
+ }
366
+ catch (error) {
367
+ console.error('Error:', error);
368
+ }
369
+ }
370
+ //# sourceMappingURL=order-processor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"order-processor.js","sourceRoot":"","sources":["../../src/utils/order-processor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;GAEG;AACH,MAAM,CAAN,IAAY,SAIX;AAJD,WAAY,SAAS;IACnB,gCAAmB,CAAA;IACnB,kCAAqB,CAAA;IACrB,0CAA6B,CAAA;AAC/B,CAAC,EAJW,SAAS,KAAT,SAAS,QAIpB;AAED;;GAEG;AACH,MAAM,CAAN,IAAY,WAKX;AALD,WAAY,WAAW;IACrB,kCAAmB,CAAA;IACnB,sCAAuB,CAAA;IACvB,kCAAmB,CAAA;IACnB,gCAAiB,CAAA;AACnB,CAAC,EALW,WAAW,KAAX,WAAW,QAKtB;AA4BD;;GAEG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,CAAC,MAAM,CAAC;IAClC,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,EAAE,sBAAsB,CAAC;IAC7C,IAAI,EAAE,CAAC,CAAC,UAAU,CAAC,SAAS,CAAC;IAC7B,MAAM,EAAE,CAAC,CAAC,UAAU,C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AAE,IAAI,IAAI,EAAE;KACtB,CAAC;IAEF,IAAI,CAAC;QACH,MAAM,OAAO,GAAG,MAAM,YAAY,CAAC,YAAY,CAAC,aAAa,CAAC,CAAC;QAC/D,OAAO,CAAC,GAAG,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IAClC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,KAAK,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;IACjC,CAAC;IAED,gCAAgC;IAChC,MAAM,iBAAiB,GAAG;QACxB,EAAE,EAAE,SAAS;QACb,IAAI,EAAE,SAAS,CAAC,YAAY;QAC5B,MAAM,EAAE,WAAW,CAAC,OAAO;QAC3B,UAAU,EAAE,UAAU;QACtB,KAAK,EAAE;YACL;gBACE,SAAS,EAAE,cAAc;gBACzB,QAAQ,EAAE,CAAC;gBACX,KAAK,EAAE,KAAK;aACb;SACF;QACD,SAAS,EAAE,IAAI,IAAI,EAAE;QACrB,SAAS,EAAE,IAAI,IAAI,EAAE;KACtB,CAAC;IAEF,IAAI,CAAC;QACH,MAAM,OAAO,GAAG,MAAM,YAAY,CAAC,YAAY,CAAC,iBAAiB,CAAC,CAAC;QACnE,OAAO,CAAC,GAAG,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IAClC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,KAAK,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;IACjC,CAAC;AACH,CAAC"}
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Configuration options for the RateLimiter.
3
+ */
4
+ export interface RateLimiterConfig {
5
+ /** Maximum number of requests allowed within the time window */
6
+ maxRequests: number;
7
+ /** Time window in milliseconds */
8
+ windowMs: number;
9
+ }
10
+ /**
11
+ * A sliding-window rate limiter that tracks requests per client.
12
+ * Uses a Map to store request timestamps for each client and
13
+ * automatically cleans up expired entries.
14
+ */
15
+ export declare class RateLimiter {
16
+ private readonly maxRequests;
17
+ private readonly windowMs;
18
+ private readonly clients;
19
+ /**
20
+ * Creates a new RateLimiter instance.
21
+ *
22
+ * @param config - Configuration for request limits and time window
23
+ * @throws Error if maxRequests or windowMs are not positive numbers
24
+ */
25
+ constructor(config: RateLimiterConfig);
26
+ /**
27
+ * Checks if a request from the given client is allowed.
28
+ * If allowed, records the request timestamp.
29
+ *
30
+ * @param clientId - Unique identifier for the client
31
+ * @returns True if the request is allowed, false if rate limited
32
+ */
33
+ isAllowed(clientId: string): boolean;
34
+ /**
35
+ * Returns the number of remaining requests for a client within the current window.
36
+ *
37
+ * @param clientId - Unique identifier for the client
38
+ * @returns Number of remaining allowed requests
39
+ */
40
+ getRemainingRequests(clientId: string): number;
41
+ /**
42
+ * Resets rate limiting state.
43
+ * If clientId is provided, resets only that client.
44
+ * Otherwise, resets all clients.
45
+ *
46
+ * @param clientId - Optional client identifier to reset
47
+ */
48
+ reset(clientId?: string): void;
49
+ /**
50
+ * Removes expired timestamps from a client's entry.
51
+ * Deletes the entry entirely if no valid timestamps remain.
52
+ *
53
+ * @param clientId - Client identifier to clean up
54
+ * @param now - Current timestamp for expiration calculation
55
+ */
56
+ private cleanupExpiredEntries;
57
+ }
58
+ //# sourceMappingURL=rate-limiter-simple.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rate-limiter-simple.d.ts","sourceRoot":"","sources":["../../src/utils/rate-limiter-simple.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,gEAAgE;IAChE,WAAW,EAAE,MAAM,CAAC;IACpB,kCAAkC;IAClC,QAAQ,EAAE,MAAM,CAAC;CAClB;AASD;;;;GAIG;AACH,qBAAa,WAAW;IACtB,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;IACrC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAuC;IAE/D;;;;;OAKG;gBACS,MAAM,EAAE,iBAAiB;IAqBrC;;;;;;OAMG;IACH,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO;IAmBpC;;;;;OAKG;IACH,oBAAoB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM;IAa9C;;;;;;OAMG;IACH,KAAK,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI;IAQ9B;;;;;;OAMG;IACH,OAAO,CAAC,qBAAqB;CAgB9B"}
@@ -0,0 +1,100 @@
1
+ /**
2
+ * A sliding-window rate limiter that tracks requests per client.
3
+ * Uses a Map to store request timestamps for each client and
4
+ * automatically cleans up expired entries.
5
+ */
6
+ export class RateLimiter {
7
+ maxRequests;
8
+ windowMs;
9
+ clients = new Map();
10
+ /**
11
+ * Creates a new RateLimiter instance.
12
+ *
13
+ * @param config - Configuration for request limits and time window
14
+ * @throws Error if maxRequests or windowMs are not positive numbers
15
+ */
16
+ constructor(config) {
17
+ if (typeof config.maxRequests !== 'number' ||
18
+ config.maxRequests <= 0 ||
19
+ !Number.isInteger(config.maxRequests)) {
20
+ throw new Error('maxRequests must be a positive integer');
21
+ }
22
+ if (typeof config.windowMs !== 'number' ||
23
+ config.windowMs <= 0 ||
24
+ !Number.isInteger(config.windowMs)) {
25
+ throw new Error('windowMs must be a positive integer');
26
+ }
27
+ this.maxRequests = config.maxRequests;
28
+ this.windowMs = config.windowMs;
29
+ }
30
+ /**
31
+ * Checks if a request from the given client is allowed.
32
+ * If allowed, records the request timestamp.
33
+ *
34
+ * @param clientId - Unique identifier for the client
35
+ * @returns True if the request is allowed, false if rate limited
36
+ */
37
+ isAllowed(clientId) {
38
+ const now = Date.now();
39
+ this.cleanupExpiredEntries(clientId, now);
40
+ const entry = this.clients.get(clientId);
41
+ if (!entry) {
42
+ this.clients.set(clientId, { timestamps: [now] });
43
+ return true;
44
+ }
45
+ if (entry.timestamps.length < this.maxRequests) {
46
+ entry.timestamps.push(now);
47
+ return true;
48
+ }
49
+ return false;
50
+ }
51
+ /**
52
+ * Returns the number of remaining requests for a client within the current window.
53
+ *
54
+ * @param clientId - Unique identifier for the client
55
+ * @returns Number of remaining allowed requests
56
+ */
57
+ getRemainingRequests(clientId) {
58
+ const now = Date.now();
59
+ this.cleanupExpiredEntries(clientId, now);
60
+ const entry = this.clients.get(clientId);
61
+ if (!entry) {
62
+ return this.maxRequests;
63
+ }
64
+ return Math.max(0, this.maxRequests - entry.timestamps.length);
65
+ }
66
+ /**
67
+ * Resets rate limiting state.
68
+ * If clientId is provided, resets only that client.
69
+ * Otherwise, resets all clients.
70
+ *
71
+ * @param clientId - Optional client identifier to reset
72
+ */
73
+ reset(clientId) {
74
+ if (clientId !== undefined) {
75
+ this.clients.delete(clientId);
76
+ }
77
+ else {
78
+ this.clients.clear();
79
+ }
80
+ }
81
+ /**
82
+ * Removes expired timestamps from a client's entry.
83
+ * Deletes the entry entirely if no valid timestamps remain.
84
+ *
85
+ * @param clientId - Client identifier to clean up
86
+ * @param now - Current timestamp for expiration calculation
87
+ */
88
+ cleanupExpiredEntries(clientId, now) {
89
+ const entry = this.clients.get(clientId);
90
+ if (!entry) {
91
+ return;
92
+ }
93
+ const cutoff = now - this.windowMs;
94
+ entry.timestamps = entry.timestamps.filter((timestamp) => timestamp > cutoff);
95
+ if (entry.timestamps.length === 0) {
96
+ this.clients.delete(clientId);
97
+ }
98
+ }
99
+ }
100
+ //# sourceMappingURL=rate-limiter-simple.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rate-limiter-simple.js","sourceRoot":"","sources":["../../src/utils/rate-limiter-simple.ts"],"names":[],"mappings":"AAiBA;;;;GAIG;AACH,MAAM,OAAO,WAAW;IACL,WAAW,CAAS;IACpB,QAAQ,CAAS;IACjB,OAAO,GAA6B,IAAI,GAAG,EAAE,CAAC;IAE/D;;;;;OAKG;IACH,YAAY,MAAyB;QACnC,IACE,OAAO,MAAM,CAAC,WAAW,KAAK,QAAQ;YACtC,MAAM,CAAC,WAAW,IAAI,CAAC;YACvB,CAAC,MAAM,CAAC,SAAS,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C;QAE1C,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAEzC,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,OAAO,IAAI,CAAC,WAAW,CAAC;QAC1B,CAAC;QAED,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,WAAW,GAAG,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;IACjE,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,QAAiB;QACrB,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;YAC3B,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAChC,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QACvB,CAAC;IACH,CAAC;IAED;;;;;;OAMG;IACK,qBAAqB,CAAC,QAAgB,EAAE,GAAW;QACzD,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAEzC,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,OAAO;QACT,CAAC;QAED,MAAM,MAAM,GAAG,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC;QACnC,KAAK,CAAC,UAAU,GAAG,KAAK,CAAC,UAAU,CAAC,MAAM,CACxC,CAAC,SAAS,EAAE,EAAE,CAAC,SAAS,GAAG,MAAM,CAClC,CAAC;QAEF,IAAI,KAAK,CAAC,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAClC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAChC,CAAC;IACH,CAAC;CACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "universal-agent-memory",
3
- "version": "0.7.3",
3
+ "version": "0.7.4",
4
4
  "description": "Universal AI agent memory system - CLAUDE.md templates, memory, worktrees for Claude Code, Factory.AI, VSCode, OpenCode",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",