| 1 | import { Telemetry } from "./posthog.js"; |
| 2 | |
| 3 | interface TokenBatch { |
| 4 | model: string; |
| 5 | provider: string; |
| 6 | count: number; |
| 7 | totalPromptTokens: number; |
| 8 | totalGeneratedTokens: number; |
| 9 | lastEventTime: number; |
| 10 | } |
| 11 | |
| 12 | export class TokensBatchingService { |
| 13 | private static instance: TokensBatchingService; |
| 14 | private batches = new Map<string, TokenBatch>(); |
| 15 | private flushTimer: NodeJS.Timeout | null = null; |
| 16 | |
| 17 | private readonly BATCH_SIZE_LIMIT = 25; |
| 18 | private readonly FLUSH_INTERVAL_MS = 10 * 60 * 1000; // 10 minutes |
| 19 | |
| 20 | static getInstance(): TokensBatchingService { |
| 21 | if (!TokensBatchingService.instance) { |
| 22 | TokensBatchingService.instance = new TokensBatchingService(); |
| 23 | } |
| 24 | return TokensBatchingService.instance; |
| 25 | } |
| 26 | |
| 27 | private constructor() { |
| 28 | this.startFlushTimer(); |
| 29 | } |
| 30 | |
| 31 | addTokens( |
| 32 | model: string, |
| 33 | provider: string, |
| 34 | promptTokens: number, |
| 35 | generatedTokens: number, |
| 36 | ): void { |
| 37 | const key = `${provider}:${model}`; |
| 38 | |
| 39 | if (!this.batches.has(key)) { |
| 40 | this.batches.set(key, { |
| 41 | model, |
| 42 | provider, |
| 43 | count: 0, |
| 44 | totalPromptTokens: 0, |
| 45 | totalGeneratedTokens: 0, |
| 46 | lastEventTime: Date.now(), |
| 47 | }); |
| 48 | } |
| 49 | |
| 50 | const batch = this.batches.get(key)!; |
| 51 | batch.count++; |
| 52 | batch.totalPromptTokens += promptTokens; |
| 53 | batch.totalGeneratedTokens += generatedTokens; |
| 54 | batch.lastEventTime = Date.now(); |
| 55 | |
| 56 | // Flush if batch is full |
| 57 | if (batch.count >= this.BATCH_SIZE_LIMIT) { |
| 58 | this.flushBatch(key, batch); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | private flushBatch(key: string, batch: TokenBatch): void { |
| 63 | if (batch.count === 0) return; |
| 64 | |
| 65 | void Telemetry.capture( |
| 66 | "tokens_generated_batch", |
| 67 | { |
| 68 | model: batch.model, |
| 69 | provider: batch.provider, |
| 70 | eventCount: batch.count, |
| 71 | totalPromptTokens: batch.totalPromptTokens, |
| 72 | totalGeneratedTokens: batch.totalGeneratedTokens, |
| 73 | avgPromptTokens: Math.round(batch.totalPromptTokens / batch.count), |
| 74 | avgGeneratedTokens: Math.round( |
| 75 | batch.totalGeneratedTokens / batch.count, |
| 76 | ), |
| 77 | }, |
| 78 | true, |
| 79 | ); |
| 80 | |
| 81 | this.batches.delete(key); |
| 82 | } |
| 83 | |
| 84 | private startFlushTimer(): void { |
| 85 | this.flushTimer = setInterval(() => { |
| 86 | this.flushAllBatches(); |
| 87 | }, this.FLUSH_INTERVAL_MS); |
| 88 | // Allow the process to exit if this timer is the only thing keeping it alive |
| 89 | // This prevents test hangs and allows graceful shutdown |
| 90 | this.flushTimer.unref(); |
| 91 | } |
| 92 | |
| 93 | private flushAllBatches(): void { |
| 94 | for (const [key, batch] of this.batches.entries()) { |
| 95 | this.flushBatch(key, batch); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | shutdown(): void { |
| 100 | if (this.flushTimer) { |
| 101 | clearInterval(this.flushTimer); |
| 102 | this.flushTimer = null; |
| 103 | } |
| 104 | this.flushAllBatches(); |
| 105 | } |
| 106 | } |