Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { createHash } from 'node:crypto'; |
| 2 | |
| 3 | export const DEFAULT_SEMANTIC_TIMEOUT_MS = 3_000; |
| 4 | export const DEFAULT_SEMANTIC_REQUESTS_PER_MINUTE = 60; |
| 5 | export const DEFAULT_SEMANTIC_GLOBAL_REQUESTS_PER_MINUTE = 600; |
| 6 | export const DEFAULT_SEMANTIC_MAX_CONCURRENCY = 8; |
| 7 | |
| 8 | export interface SemanticSearchLease { |
| 9 | allowed: true; |
| 10 | release: () => void; |
| 11 | } |
| 12 | |
| 13 | export interface SemanticSearchDenied { |
| 14 | allowed: false; |
| 15 | reason: 'semantic-rate-limited' | 'semantic-capacity-limited'; |
| 16 | } |
| 17 | |
| 18 | export type SemanticSearchAdmission = SemanticSearchLease | SemanticSearchDenied; |
| 19 | |
| 20 | interface ClientWindow { |
| 21 | count: number; |
| 22 | startedAt: number; |
| 23 | } |
| 24 | |
| 25 | interface SemanticSearchBudgetOptions { |
| 26 | requestsPerWindow: number; |
| 27 | globalRequestsPerWindow?: number; |
| 28 | windowMs: number; |
| 29 | maxConcurrent: number; |
| 30 | now?: () => number; |
| 31 | } |
| 32 | |
| 33 | const MAX_TRACKED_CLIENTS = 5_000; |
| 34 | |
| 35 | export class SemanticSearchBudget { |
| 36 | private readonly clients = new Map<string, ClientWindow>(); |
| 37 | private readonly now: () => number; |
| 38 | private globalWindow: ClientWindow = { count: 0, startedAt: 0 }; |
| 39 | private active = 0; |
| 40 | |
| 41 | constructor(private readonly options: SemanticSearchBudgetOptions) { |
| 42 | this.now = options.now ?? Date.now; |
| 43 | } |
| 44 | |
| 45 | tryAcquire(clientKey?: string): SemanticSearchAdmission { |
| 46 | if (this.active >= this.options.maxConcurrent) { |
| 47 | return { allowed: false, reason: 'semantic-capacity-limited' }; |
| 48 | } |
| 49 | |
| 50 | const now = this.now(); |
| 51 | const globalLimit = this.options.globalRequestsPerWindow ?? Number.POSITIVE_INFINITY; |
| 52 | const globalWindow = now - this.globalWindow.startedAt >= this.options.windowMs |
| 53 | ? { count: 0, startedAt: now } |
| 54 | : this.globalWindow; |
| 55 | if (globalWindow.count >= globalLimit) { |
| 56 | return { allowed: false, reason: 'semantic-rate-limited' }; |
| 57 | } |
| 58 | |
| 59 | if (clientKey) { |
| 60 | const existing = this.clients.get(clientKey); |
| 61 | const client = !existing || now - existing.startedAt >= this.options.windowMs |
| 62 | ? { count: 0, startedAt: now } |
| 63 | : existing; |
| 64 | if (client.count >= this.options.requestsPerWindow) { |
| 65 | return { allowed: false, reason: 'semantic-rate-limited' }; |
| 66 | } |
| 67 | client.count += 1; |
| 68 | if (!existing && this.clients.size >= MAX_TRACKED_CLIENTS) { |
| 69 | const oldestKey = this.clients.keys().next().value; |
| 70 | if (oldestKey) this.clients.delete(oldestKey); |
| 71 | } |
| 72 | this.clients.set(clientKey, client); |
| 73 | } |
| 74 | |
| 75 | globalWindow.count += 1; |
| 76 | this.globalWindow = globalWindow; |
| 77 | this.active += 1; |
| 78 | let released = false; |
| 79 | return { |
| 80 | allowed: true, |
| 81 | release: () => { |
| 82 | if (released) return; |
| 83 | released = true; |
| 84 | this.active = Math.max(0, this.active - 1); |
| 85 | }, |
| 86 | }; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | function boundedInteger( |
| 91 | value: string | undefined, |
| 92 | fallback: number, |
| 93 | minimum: number, |
| 94 | maximum: number, |
| 95 | ): number { |
| 96 | const parsed = Number(value); |
| 97 | return Number.isInteger(parsed) && parsed >= minimum && parsed <= maximum |
| 98 | ? parsed |
| 99 | : fallback; |
| 100 | } |
| 101 | |
| 102 | export function semanticSearchClientKey(request: Request): string | undefined { |
| 103 | const forwarded = request.headers.get('x-vercel-forwarded-for') |
| 104 | ?? request.headers.get('x-forwarded-for'); |
| 105 | const clientAddress = forwarded?.split(',', 1)[0]?.trim(); |
| 106 | if (!clientAddress) return undefined; |
| 107 | return createHash('sha256').update(clientAddress, 'utf8').digest('hex').slice(0, 32); |
| 108 | } |
| 109 | |
| 110 | const requestsPerMinute = boundedInteger( |
| 111 | process.env.KB_SEMANTIC_REQUESTS_PER_MINUTE, |
| 112 | DEFAULT_SEMANTIC_REQUESTS_PER_MINUTE, |
| 113 | 1, |
| 114 | 1_000, |
| 115 | ); |
| 116 | const globalRequestsPerMinute = boundedInteger( |
| 117 | process.env.KB_SEMANTIC_GLOBAL_REQUESTS_PER_MINUTE, |
| 118 | DEFAULT_SEMANTIC_GLOBAL_REQUESTS_PER_MINUTE, |
| 119 | 1, |
| 120 | 10_000, |
| 121 | ); |
| 122 | const maxConcurrency = boundedInteger( |
| 123 | process.env.KB_SEMANTIC_MAX_CONCURRENCY, |
| 124 | DEFAULT_SEMANTIC_MAX_CONCURRENCY, |
| 125 | 1, |
| 126 | 100, |
| 127 | ); |
| 128 | const defaultBudget = new SemanticSearchBudget({ |
| 129 | requestsPerWindow: requestsPerMinute, |
| 130 | globalRequestsPerWindow: globalRequestsPerMinute, |
| 131 | windowMs: 60_000, |
| 132 | maxConcurrent: maxConcurrency, |
| 133 | }); |
| 134 | |
| 135 | export function acquireDefaultSemanticSearch(request: Request): SemanticSearchAdmission { |
| 136 | return defaultBudget.tryAcquire(semanticSearchClientKey(request)); |
| 137 | } |
| 138 | |
| 139 | export function defaultSemanticTimeoutMs(): number { |
| 140 | return boundedInteger( |
| 141 | process.env.KB_SEMANTIC_TIMEOUT_MS, |
| 142 | DEFAULT_SEMANTIC_TIMEOUT_MS, |
| 143 | 250, |
| 144 | 10_000, |
| 145 | ); |
| 146 | } |