Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

usage.ts

164 lines5.9 KB

1import * as Schema from "effect/Schema";
2​
3import { ForwardCompatibleArray, NonNegativeInt, TrimmedNonEmptyString } from "./baseSchemas.ts";
4​
5/** Bump for incompatible UsageSummary changes; skipped unknown array variants need no bump. */
6export const USAGE_CONTRACT_VERSION = 6 as const;
7​
8export const USAGE_MERGE_COMPATIBLE_SINCE = 4 as const;
9​
10export const UsageProviderKind = Schema.Literals([
11 "claude",
12 "codex",
13 "grok",
14 "cursor",
15 "opencode",
16 "antigravity",
17 "pi",
18 "muse",
19]);
20export type UsageProviderKind = typeof UsageProviderKind.Type;
21​
22const USAGE_DAY_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
23​
24export const UsageDay = TrimmedNonEmptyString.check(Schema.isPattern(USAGE_DAY_PATTERN)).pipe(
25 Schema.brand("UsageDay"),
26);
27export type UsageDay = typeof UsageDay.Type;
28​
29export const UsageResolution = Schema.Literals(["day", "hour"]);
30export type UsageResolution = typeof UsageResolution.Type;
31​
32export const UsageCostSource = Schema.Literals(["providerReported", "modelPriced", "unpriced"]);
33export type UsageCostSource = typeof UsageCostSource.Type;
34​
35/** Sum uncached input, cache reads, and cache writes for input totals; reasoning is already included in output. */
36export const UsageTokenTotals = Schema.Struct({
37 uncachedInputTokens: NonNegativeInt,
38 cachedInputTokens: NonNegativeInt,
39 cacheCreationTokens: NonNegativeInt,
40 outputTokens: NonNegativeInt,
41 reasoningTokens: NonNegativeInt,
42});
43export type UsageTokenTotals = typeof UsageTokenTotals.Type;
44​
45export const UsageCategoryCost = Schema.Struct({
46 input: Schema.Number,
47 cacheRead: Schema.Number,
48 cacheWrite: Schema.Number,
49 output: Schema.Number,
50});
51export type UsageCategoryCost = typeof UsageCategoryCost.Type;
52​
53export const UsageBucket = Schema.Struct({
54 day: UsageDay,
55 hourStart: Schema.optional(TrimmedNonEmptyString),
56 provider: UsageProviderKind,
57 model: TrimmedNonEmptyString,
58 sourcePath: Schema.optional(TrimmedNonEmptyString),
59 totals: UsageTokenTotals,
60 costUsd: Schema.Number,
61 cacheSavingsUsd: Schema.Number,
62 categoryCostUsd: Schema.optional(UsageCategoryCost),
63 fastCostUsd: Schema.optional(Schema.Number),
64 ultrafastCostUsd: Schema.optional(Schema.Number),
65 speedPremiumUsd: Schema.optional(Schema.Number),
66 costSource: UsageCostSource,
67 records: NonNegativeInt,
68 unpricedRecords: NonNegativeInt,
69 sessions: NonNegativeInt,
70});
71export type UsageBucket = typeof UsageBucket.Type;
72​
73export const UsageSourceFingerprint = Schema.Struct({
74 hostId: TrimmedNonEmptyString,
75 provider: UsageProviderKind,
76 resolvedHomePath: TrimmedNonEmptyString,
77 /** Transcript directory device:inode distinguishes machines sharing a hostname and home path. */
78 volumeId: Schema.String,
79});
80export type UsageSourceFingerprint = typeof UsageSourceFingerprint.Type;
81​
82export const UsageSourceStatus = Schema.Literals(["ok", "missing", "partial", "failed"]);
83export type UsageSourceStatus = typeof UsageSourceStatus.Type;
84​
85export const UsageSource = Schema.Struct({
86 fingerprint: UsageSourceFingerprint,
87 status: UsageSourceStatus,
88 scannedFiles: NonNegativeInt,
89 skippedFiles: NonNegativeInt,
90 malformedRecords: NonNegativeInt,
91 /** Total this count; bucket counts double-count sessions spanning days or models. */
92 distinctSessions: NonNegativeInt,
93 message: Schema.NullOr(TrimmedNonEmptyString),
94 action: Schema.optionalKey(Schema.Literal("enableCursorKeychain")),
95 /** Cached response during refresh; repeat with awaitRefresh for fresh data. */
96 refreshing: Schema.optionalKey(Schema.Literal(true)),
97});
98export type UsageSource = typeof UsageSource.Type;
99​
100export const UsagePricingStatus = Schema.Literals(["fresh", "cached", "unavailable"]);
101export type UsagePricingStatus = typeof UsagePricingStatus.Type;
102​
103export const UsagePricing = Schema.Struct({
104 status: UsagePricingStatus,
105 source: TrimmedNonEmptyString,
106 fetchedAt: Schema.NullOr(Schema.String),
107 knownModels: NonNegativeInt,
108});
109export type UsagePricing = typeof UsagePricing.Type;
110​
111/**
112 * Matches a provider's original model ID, before usage model aliases apply. Omit provider to
113 * match any; a string provider lets older clients carry selectors for providers they lack.
114 */
115export const UsageModelSelector = Schema.Struct({
116 provider: Schema.optional(TrimmedNonEmptyString),
117 model: TrimmedNonEmptyString,
118});
119export type UsageModelSelector = typeof UsageModelSelector.Type;
120​
121/** Keeps records matching `include` (all when omitted) and not matching `exclude`. */
122export const UsageModelFilter = Schema.Struct({
123 include: Schema.optional(Schema.Array(UsageModelSelector)),
124 exclude: Schema.optional(Schema.Array(UsageModelSelector)),
125});
126export type UsageModelFilter = typeof UsageModelFilter.Type;
127​
128export const UsageSummaryInput = Schema.Struct({
129 sinceDay: UsageDay,
130 untilDay: UsageDay,
131 /** IANA bucketing zone; offsets fail across DST boundaries. */
132 timeZone: TrimmedNonEmptyString,
133 resolution: Schema.optional(UsageResolution),
134 sinceTime: Schema.optional(TrimmedNonEmptyString),
135 untilTime: Schema.optional(TrimmedNonEmptyString),
136 awaitRefresh: Schema.optional(Schema.Boolean),
137 modelFilter: Schema.optional(UsageModelFilter),
138});
139export type UsageSummaryInput = typeof UsageSummaryInput.Type;
140​
141export const UsageSummary = Schema.Struct({
142 contractVersion: Schema.Number,
143 readAt: Schema.String,
144 timeZone: TrimmedNonEmptyString,
145 sinceDay: UsageDay,
146 untilDay: UsageDay,
147 buckets: ForwardCompatibleArray(UsageBucket),
148 sources: ForwardCompatibleArray(UsageSource),
149 pricing: UsagePricing,
150 scanDurationMs: NonNegativeInt,
151 /** The filter the server applied; absent means unfiltered, including servers that predate filters. */
152 modelFilter: Schema.optional(UsageModelFilter),
153});
154export type UsageSummary = typeof UsageSummary.Type;
155​
156export class UsageReadError extends Schema.TaggedError<UsageReadError>()("UsageReadError", {
157 reason: Schema.Literals(["scanFailed", "invalidWindow"]),
158 detail: TrimmedNonEmptyString,
159 cause: Schema.optional(Schema.Defect()),
160}) {
161 override get message(): string {
162 return `Usage read failed (${this.reason}): ${this.detail}`;
163 }