@oh-my-pi/pi-ai 6.9.0 → 7.0.0

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/src/usage.ts ADDED
@@ -0,0 +1,133 @@
1
+ /**
2
+ * Usage reporting types for provider quota/limit endpoints.
3
+ *
4
+ * Provides a normalized schema to represent multiple limit windows, model tiers,
5
+ * and shared quotas across providers.
6
+ */
7
+
8
+ import type { Provider } from "./types";
9
+
10
+ export type UsageUnit = "percent" | "tokens" | "requests" | "usd" | "minutes" | "bytes" | "unknown";
11
+
12
+ export type UsageStatus = "ok" | "warning" | "exhausted" | "unknown";
13
+
14
+ /** Time window for a limit (e.g. 5h, 7d, monthly). */
15
+ export interface UsageWindow {
16
+ /** Stable identifier (e.g. "5h", "7d", "monthly"). */
17
+ id: string;
18
+ /** Human label (e.g. "5 Hour", "7 Day"). */
19
+ label: string;
20
+ /** Window duration in milliseconds, when known. */
21
+ durationMs?: number;
22
+ /** Absolute reset timestamp in milliseconds since epoch. */
23
+ resetsAt?: number;
24
+ /** Relative reset time in milliseconds, computed at fetch time. */
25
+ resetInMs?: number;
26
+ }
27
+
28
+ /** Quantitative usage data. */
29
+ export interface UsageAmount {
30
+ /** Amount used in the given unit. */
31
+ used?: number;
32
+ /** Maximum limit in the given unit. */
33
+ limit?: number;
34
+ /** Remaining amount in the given unit. */
35
+ remaining?: number;
36
+ /** Fraction used (0..1). */
37
+ usedFraction?: number;
38
+ /** Fraction remaining (0..1). */
39
+ remainingFraction?: number;
40
+ /** Unit for the amounts (percent, tokens, etc.). */
41
+ unit: UsageUnit;
42
+ }
43
+
44
+ /** Scope metadata describing what the limit applies to. */
45
+ export interface UsageScope {
46
+ provider: Provider;
47
+ accountId?: string;
48
+ projectId?: string;
49
+ orgId?: string;
50
+ modelId?: string;
51
+ tier?: string;
52
+ windowId?: string;
53
+ shared?: boolean;
54
+ }
55
+
56
+ /** Normalized limit entry for a single window or quota bucket. */
57
+ export interface UsageLimit {
58
+ /** Stable identifier for this limit entry. */
59
+ id: string;
60
+ /** Human label for display. */
61
+ label: string;
62
+ scope: UsageScope;
63
+ window?: UsageWindow;
64
+ amount: UsageAmount;
65
+ status?: UsageStatus;
66
+ notes?: string[];
67
+ }
68
+
69
+ /** Aggregated usage report for a provider. */
70
+ export interface UsageReport {
71
+ provider: Provider;
72
+ fetchedAt: number;
73
+ limits: UsageLimit[];
74
+ metadata?: Record<string, unknown>;
75
+ raw?: unknown;
76
+ }
77
+
78
+ /** Cache entry for usage reports with absolute expiry. */
79
+ export interface UsageCacheEntry {
80
+ value: UsageReport | null;
81
+ expiresAt: number;
82
+ }
83
+
84
+ /** Dependency-injected cache store for usage responses. */
85
+ export interface UsageCache {
86
+ get(key: string): UsageCacheEntry | undefined | Promise<UsageCacheEntry | undefined>;
87
+ set(key: string, entry: UsageCacheEntry): void | Promise<void>;
88
+ delete?(key: string): void | Promise<void>;
89
+ cleanup?(): void | Promise<void>;
90
+ }
91
+
92
+ /** Optional logger for usage fetchers. */
93
+ export interface UsageLogger {
94
+ debug(message: string, meta?: Record<string, unknown>): void;
95
+ warn(message: string, meta?: Record<string, unknown>): void;
96
+ }
97
+
98
+ /** Credential bundle for usage endpoints. */
99
+ export interface UsageCredential {
100
+ type: "api_key" | "oauth";
101
+ apiKey?: string;
102
+ accessToken?: string;
103
+ refreshToken?: string;
104
+ expiresAt?: number;
105
+ accountId?: string;
106
+ projectId?: string;
107
+ email?: string;
108
+ enterpriseUrl?: string;
109
+ metadata?: Record<string, unknown>;
110
+ }
111
+
112
+ /** Parameters provided to a usage fetcher. */
113
+ export interface UsageFetchParams {
114
+ provider: Provider;
115
+ credential: UsageCredential;
116
+ baseUrl?: string;
117
+ signal?: AbortSignal;
118
+ }
119
+
120
+ /** Shared runtime utilities for fetchers. */
121
+ export interface UsageFetchContext {
122
+ cache: UsageCache;
123
+ fetch: typeof fetch;
124
+ now: () => number;
125
+ logger?: UsageLogger;
126
+ }
127
+
128
+ /** Provider implementation for fetching usage information. */
129
+ export interface UsageProvider {
130
+ id: Provider;
131
+ fetchUsage(params: UsageFetchParams, ctx: UsageFetchContext): Promise<UsageReport | null>;
132
+ supports?(params: UsageFetchParams): boolean;
133
+ }