|
| 1 | +/** |
| 2 | + * Billing Cycle Manager |
| 3 | + * |
| 4 | + * Manages billing cycles to ensure only the first request in a cycle is billed. |
| 5 | + * |
| 6 | + * Rules: |
| 7 | + * - First request in cycle: X-Initiator = user (billed) |
| 8 | + * - Subsequent requests: X-Initiator = agent (not billed) |
| 9 | + * - Cycle resets after 5 minutes of inactivity (no requests after last response) |
| 10 | + * - Concurrent first requests: only ONE is billed |
| 11 | + * - Failed requests: do not enter cycle, no billing |
| 12 | + */ |
| 13 | + |
| 14 | +const CYCLE_TIMEOUT_MS = 5 * 60 * 1000 // 5 minutes |
| 15 | + |
| 16 | +class BillingCycleManager { |
| 17 | + private inCycle: boolean = false |
| 18 | + private lastResponseTime: number = 0 |
| 19 | + private pendingResponses: number = 0 |
| 20 | + private lock: Promise<void> = Promise.resolve() |
| 21 | + |
| 22 | + /** |
| 23 | + * Determines whether the current request should be billed. |
| 24 | + * Thread-safe: handles concurrent requests correctly. |
| 25 | + * |
| 26 | + * @returns 'user' if request should be billed, 'agent' if not |
| 27 | + */ |
| 28 | + async determineInitiator(): Promise<"user" | "agent"> { |
| 29 | + // Acquire lock for thread-safety |
| 30 | + await this.acquireLock() |
| 31 | + |
| 32 | + try { |
| 33 | + const now = Date.now() |
| 34 | + |
| 35 | + // Check if cycle has timed out (5 minutes since last response) |
| 36 | + if ( |
| 37 | + this.inCycle |
| 38 | + && this.pendingResponses === 0 |
| 39 | + && now - this.lastResponseTime > CYCLE_TIMEOUT_MS |
| 40 | + ) { |
| 41 | + this.inCycle = false |
| 42 | + } |
| 43 | + |
| 44 | + // Determine billing |
| 45 | + if (!this.inCycle) { |
| 46 | + // First request in new cycle - bill it |
| 47 | + this.inCycle = true |
| 48 | + this.pendingResponses++ |
| 49 | + return "user" |
| 50 | + } |
| 51 | + |
| 52 | + // Already in cycle - don't bill |
| 53 | + this.pendingResponses++ |
| 54 | + return "agent" |
| 55 | + } finally { |
| 56 | + this.releaseLock() |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Mark a response as complete (for both streaming and non-streaming). |
| 62 | + * Updates the last response timestamp. |
| 63 | + */ |
| 64 | + markResponseComplete(): void { |
| 65 | + this.lastResponseTime = Date.now() |
| 66 | + this.pendingResponses = Math.max(0, this.pendingResponses - 1) |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Mark a request as failed. |
| 71 | + * Failed requests do not contribute to the billing cycle. |
| 72 | + */ |
| 73 | + markRequestFailed(): void { |
| 74 | + this.pendingResponses = Math.max(0, this.pendingResponses - 1) |
| 75 | + |
| 76 | + // If this was the first request and it failed, exit the cycle |
| 77 | + if (this.pendingResponses === 0 && this.lastResponseTime === 0) { |
| 78 | + this.inCycle = false |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Get current cycle status (for debugging/monitoring) |
| 84 | + */ |
| 85 | + getStatus(): { |
| 86 | + inCycle: boolean |
| 87 | + lastResponseTime: number |
| 88 | + pendingResponses: number |
| 89 | + } { |
| 90 | + return { |
| 91 | + inCycle: this.inCycle, |
| 92 | + lastResponseTime: this.lastResponseTime, |
| 93 | + pendingResponses: this.pendingResponses, |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Reset the billing cycle (for testing purposes) |
| 99 | + */ |
| 100 | + reset(): void { |
| 101 | + this.inCycle = false |
| 102 | + this.lastResponseTime = 0 |
| 103 | + this.pendingResponses = 0 |
| 104 | + } |
| 105 | + |
| 106 | + // Simple async lock implementation |
| 107 | + private async acquireLock(): Promise<void> { |
| 108 | + const currentLock = this.lock |
| 109 | + let releaseLock!: () => void |
| 110 | + this.lock = new Promise((resolve) => { |
| 111 | + releaseLock = resolve |
| 112 | + }) |
| 113 | + await currentLock |
| 114 | + this.releaseLock = releaseLock |
| 115 | + } |
| 116 | + |
| 117 | + private releaseLock: () => void = () => {} |
| 118 | +} |
| 119 | + |
| 120 | +// Singleton instance |
| 121 | +export const billingCycleManager = new BillingCycleManager() |
0 commit comments