UNPKG

@juspay/neurolink

Version:

The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002

92 lines (91 loc) • 4.52 kB
/** * Proxy usage statistics with restart- and handoff-safe persistence. * * Request handlers update memory synchronously. Small deltas are merged into a * shared snapshot asynchronously, under a cross-process lock, so overlapping * rolling workers cannot overwrite each other's counters. */ import type { AccountStats, ProxyTerminalErrorDetails, ProxyTerminalErrorJournal, ProxyStats, ProxyStatsPersistenceStatus, ProxyUsageStatsSnapshot, ProxyUsageStatsStoreOptions } from "../types/index.js"; export declare class ProxyUsageStatsStore { private readonly now; private readonly flushIntervalMs; private readonly lockTimeoutMs; private readonly staleLockMs; private filePath?; private terminalErrorsFilePath?; private stats; private pending; private terminalErrors; private pendingTerminalErrors; private pendingMutations; private inFlightMutations; private pendingTerminalErrorMutations; private inFlightTerminalErrorMutations; private revision; private terminalErrorsRevision; private flushTimer; private readonly flushMutex; private lastFlushedAt?; private lastReconciledAt?; private lastRecoveryAt?; private lastError?; private terminalErrorsLastFlushedAt?; private terminalErrorsLastRecoveryAt?; private terminalErrorsLastError?; private snapshotVersion; constructor(options?: ProxyUsageStatsStoreOptions); initialize(filePath?: string): Promise<void>; recordAttempt(accountLabel: string, accountType: string): void; recordFinalSuccess(accountLabel?: string, accountType?: string): void; recordAttemptError(accountLabel: string, accountType: string, status: number, rateLimitKind?: "transient" | "quota"): void; recordFinalError(status: number, accountLabel?: string, accountType?: string, details?: ProxyTerminalErrorDetails): void; getStats(): ProxyStats; getAccountStats(label: string, type?: string): AccountStats | undefined; getTerminalErrors(): ProxyTerminalErrorJournal; getUsageSnapshot(): ProxyUsageStatsSnapshot; getPersistenceStatus(): ProxyStatsPersistenceStatus; flush(): Promise<void>; private persistStatsDelta; private persistTerminalErrorDelta; reconcileUsageSnapshot(): Promise<ProxyUsageStatsSnapshot>; reconcile(): Promise<ProxyStats>; resetMemory(): void; resetForTests(): Promise<void>; private markMutation; private applyAttempt; private applyFinalSuccess; private applyAttemptError; private applyFinalError; private applyTerminalError; private ensureAccount; private scheduleFlush; private cancelFlushTimer; private readSnapshot; private readTerminalErrorSnapshot; private applySnapshot; private applyTerminalErrorSnapshot; private recoverCorruptSnapshot; private recoverCorruptTerminalErrorSnapshot; private quarantineCorruptSnapshot; private quarantineCorruptTerminalErrorSnapshot; private pruneCorruptSnapshots; private describeError; } export declare function initUsageStats(filePath: string): Promise<void>; export declare function recordAttempt(accountLabel: string, accountType: string): void; export declare function recordFinalSuccess(accountLabel?: string, accountType?: string): void; export declare function recordAttemptError(accountLabel: string, accountType: string, status: number, rateLimitKind?: "transient" | "quota"): void; export declare function recordFinalError(status: number, accountLabel?: string, accountType?: string, details?: ProxyTerminalErrorDetails): void; export declare function getStats(): ProxyStats; /** Return the process-local coherent snapshot without filesystem reconciliation. */ export declare function getUsageSnapshot(): ProxyUsageStatsSnapshot; export declare function getReconciledStats(): Promise<ProxyStats>; export declare function getReconciledUsageSnapshot(): Promise<ProxyUsageStatsSnapshot>; export declare function getAccountStats(label: string, type?: string): AccountStats | undefined; export declare function getTerminalErrors(): ProxyTerminalErrorJournal; export declare function getUsageStatsPersistenceStatus(): ProxyStatsPersistenceStatus; export declare function flushUsageStats(): Promise<void>; /** Reset process-local counters while intentionally preserving durable state. */ export declare function resetStats(): void; /** Disconnect persistence and clear singleton state between isolated tests. */ export declare function resetUsageStatsForTests(): Promise<void>;