@strand-js/core 0.1.4 → 0.1.6
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/dist/index.d.mts +16 -1
- package/dist/index.d.ts +16 -1
- package/dist/index.js +29 -0
- package/dist/index.mjs +28 -0
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -181,4 +181,19 @@ type ValidationResult = {
|
|
|
181
181
|
};
|
|
182
182
|
declare function validateMessages(messages: unknown, config?: RequestValidationConfig): ValidationResult;
|
|
183
183
|
|
|
184
|
-
|
|
184
|
+
interface RateLimitConfig {
|
|
185
|
+
windowMs: number;
|
|
186
|
+
maxRequests: number;
|
|
187
|
+
keyExtractor?: (identifier: string) => string;
|
|
188
|
+
}
|
|
189
|
+
interface RateLimitResult {
|
|
190
|
+
retryAfter: number;
|
|
191
|
+
}
|
|
192
|
+
declare class RateLimiter {
|
|
193
|
+
private readonly config;
|
|
194
|
+
private readonly store;
|
|
195
|
+
constructor(config: RateLimitConfig);
|
|
196
|
+
check(identifier: string): RateLimitResult | null;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export { type AgentStatus, type AgentStep, type ContextWindowConfig, type ContextWindowStrategy, type JsonSchema, type Message, type RateLimitConfig, type RateLimitResult, RateLimiter, type RequestValidationConfig, type RetryConfig, type SendOptions, type Session, SessionStateMachine, type StrandClient, type StrandClientConfig, StrandError, type StreamingStatus, type TokenUsage, type ToolCall, type ToolCallState, type ToolCallStatus, ToolCallStore, type ToolDefinition, type ValidationResult, type WireEvent, applyContextWindow, createStrandClient, generateId, parseSSEStream, processWireEvent, tool, toolToJsonSchema, validateMessages, withRetry };
|
package/dist/index.d.ts
CHANGED
|
@@ -181,4 +181,19 @@ type ValidationResult = {
|
|
|
181
181
|
};
|
|
182
182
|
declare function validateMessages(messages: unknown, config?: RequestValidationConfig): ValidationResult;
|
|
183
183
|
|
|
184
|
-
|
|
184
|
+
interface RateLimitConfig {
|
|
185
|
+
windowMs: number;
|
|
186
|
+
maxRequests: number;
|
|
187
|
+
keyExtractor?: (identifier: string) => string;
|
|
188
|
+
}
|
|
189
|
+
interface RateLimitResult {
|
|
190
|
+
retryAfter: number;
|
|
191
|
+
}
|
|
192
|
+
declare class RateLimiter {
|
|
193
|
+
private readonly config;
|
|
194
|
+
private readonly store;
|
|
195
|
+
constructor(config: RateLimitConfig);
|
|
196
|
+
check(identifier: string): RateLimitResult | null;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export { type AgentStatus, type AgentStep, type ContextWindowConfig, type ContextWindowStrategy, type JsonSchema, type Message, type RateLimitConfig, type RateLimitResult, RateLimiter, type RequestValidationConfig, type RetryConfig, type SendOptions, type Session, SessionStateMachine, type StrandClient, type StrandClientConfig, StrandError, type StreamingStatus, type TokenUsage, type ToolCall, type ToolCallState, type ToolCallStatus, ToolCallStore, type ToolDefinition, type ValidationResult, type WireEvent, applyContextWindow, createStrandClient, generateId, parseSSEStream, processWireEvent, tool, toolToJsonSchema, validateMessages, withRetry };
|
package/dist/index.js
CHANGED
|
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
RateLimiter: () => RateLimiter,
|
|
23
24
|
SessionStateMachine: () => SessionStateMachine,
|
|
24
25
|
StrandError: () => StrandError,
|
|
25
26
|
ToolCallStore: () => ToolCallStore,
|
|
@@ -498,8 +499,36 @@ function validateMessages(messages, config = {}) {
|
|
|
498
499
|
}
|
|
499
500
|
return { ok: true };
|
|
500
501
|
}
|
|
502
|
+
|
|
503
|
+
// src/rate-limiter.ts
|
|
504
|
+
var RateLimiter = class {
|
|
505
|
+
constructor(config) {
|
|
506
|
+
this.store = /* @__PURE__ */ new Map();
|
|
507
|
+
this.config = {
|
|
508
|
+
...config,
|
|
509
|
+
keyExtractor: config.keyExtractor ?? ((id) => id)
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
// Returns null if allowed, or { retryAfter } if blocked
|
|
513
|
+
check(identifier) {
|
|
514
|
+
const key = this.config.keyExtractor(identifier);
|
|
515
|
+
const now = Date.now();
|
|
516
|
+
const entry = this.store.get(key);
|
|
517
|
+
if (!entry || now - entry.windowStart >= this.config.windowMs) {
|
|
518
|
+
this.store.set(key, { count: 1, windowStart: now });
|
|
519
|
+
return null;
|
|
520
|
+
}
|
|
521
|
+
if (entry.count >= this.config.maxRequests) {
|
|
522
|
+
const retryAfter = Math.ceil((this.config.windowMs - (now - entry.windowStart)) / 1e3);
|
|
523
|
+
return { retryAfter };
|
|
524
|
+
}
|
|
525
|
+
entry.count++;
|
|
526
|
+
return null;
|
|
527
|
+
}
|
|
528
|
+
};
|
|
501
529
|
// Annotate the CommonJS export names for ESM import in node:
|
|
502
530
|
0 && (module.exports = {
|
|
531
|
+
RateLimiter,
|
|
503
532
|
SessionStateMachine,
|
|
504
533
|
StrandError,
|
|
505
534
|
ToolCallStore,
|
package/dist/index.mjs
CHANGED
|
@@ -461,7 +461,35 @@ function validateMessages(messages, config = {}) {
|
|
|
461
461
|
}
|
|
462
462
|
return { ok: true };
|
|
463
463
|
}
|
|
464
|
+
|
|
465
|
+
// src/rate-limiter.ts
|
|
466
|
+
var RateLimiter = class {
|
|
467
|
+
constructor(config) {
|
|
468
|
+
this.store = /* @__PURE__ */ new Map();
|
|
469
|
+
this.config = {
|
|
470
|
+
...config,
|
|
471
|
+
keyExtractor: config.keyExtractor ?? ((id) => id)
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
// Returns null if allowed, or { retryAfter } if blocked
|
|
475
|
+
check(identifier) {
|
|
476
|
+
const key = this.config.keyExtractor(identifier);
|
|
477
|
+
const now = Date.now();
|
|
478
|
+
const entry = this.store.get(key);
|
|
479
|
+
if (!entry || now - entry.windowStart >= this.config.windowMs) {
|
|
480
|
+
this.store.set(key, { count: 1, windowStart: now });
|
|
481
|
+
return null;
|
|
482
|
+
}
|
|
483
|
+
if (entry.count >= this.config.maxRequests) {
|
|
484
|
+
const retryAfter = Math.ceil((this.config.windowMs - (now - entry.windowStart)) / 1e3);
|
|
485
|
+
return { retryAfter };
|
|
486
|
+
}
|
|
487
|
+
entry.count++;
|
|
488
|
+
return null;
|
|
489
|
+
}
|
|
490
|
+
};
|
|
464
491
|
export {
|
|
492
|
+
RateLimiter,
|
|
465
493
|
SessionStateMachine,
|
|
466
494
|
StrandError,
|
|
467
495
|
ToolCallStore,
|