@openlimiter/core 0.2.0 → 1.2.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.
@@ -1,4 +1,4 @@
1
- import { MAX_SNAPSHOT_AMOUNT, PROVIDER_CODES } from "./types.js";
1
+ import { ACCOUNT_ID_PATTERN, MAX_SNAPSHOT_AMOUNT, PROVIDER_CODES, SNAPSHOT_OBSERVED_VIA, SNAPSHOT_SOURCE_KINDS, UNKNOWN_PROVENANCE } from "./types.js";
2
2
  const units = new Set(["PERCENT", "CREDITS", "TOKENS", "REQUESTS"]);
3
3
  const sources = new Set([
4
4
  "native_payload",
@@ -10,6 +10,8 @@ const sources = new Set([
10
10
  const precisions = new Set(["exact", "estimated", "manual"]);
11
11
  const currencies = new Set(["USD"]);
12
12
  const providerCodes = new Set(PROVIDER_CODES);
13
+ const sourceKinds = new Set(SNAPSHOT_SOURCE_KINDS);
14
+ const observedVia = new Set(SNAPSHOT_OBSERVED_VIA);
13
15
  const safeMeter = /^[A-Z][A-Z0-9_]{0,31}$/u;
14
16
  function isRecord(value) {
15
17
  return typeof value === "object" && value !== null && !Array.isArray(value);
@@ -90,12 +92,50 @@ function normalizeAmounts(raw) {
90
92
  return null;
91
93
  if (used < 0 || limit < 0)
92
94
  return null;
93
- if (used > limit)
94
- return null;
95
95
  if (used > MAX_SNAPSHOT_AMOUNT || limit > MAX_SNAPSHOT_AMOUNT)
96
96
  return null;
97
97
  return { usedAmount: used, limitAmount: limit, currency: currency };
98
98
  }
99
+ /**
100
+ * Read the account this reading belongs to.
101
+ *
102
+ * Absent is the ordinary case and means one unnamed account. Present but
103
+ * malformed is the dangerous case, because an identity we cannot trust would
104
+ * quietly split or merge a user's meters in the cache, so that row is refused
105
+ * outright rather than stripped back to the unnamed account.
106
+ */
107
+ function readAccountId(value) {
108
+ if (value === undefined)
109
+ return { ok: true, accountId: null };
110
+ if (typeof value !== "string" || !ACCOUNT_ID_PATTERN.test(value))
111
+ return { ok: false };
112
+ return { ok: true, accountId: value };
113
+ }
114
+ /**
115
+ * Read how the observation arrived.
116
+ *
117
+ * The opposite call to the one above, and deliberately so. Provenance is a
118
+ * label on a reading, not part of its identity, so a provenance we cannot read
119
+ * costs the reading nothing: it becomes unknown and the number survives. Either
120
+ * field failing makes the whole thing unknown, because half a provenance would
121
+ * invite a surface to describe a source it cannot actually vouch for.
122
+ */
123
+ function readProvenance(value) {
124
+ if (value === undefined)
125
+ return null;
126
+ if (!isRecord(value))
127
+ return UNKNOWN_PROVENANCE;
128
+ const kind = value["sourceKind"];
129
+ const via = value["observedVia"];
130
+ if (typeof kind !== "string" || !sourceKinds.has(kind))
131
+ return UNKNOWN_PROVENANCE;
132
+ if (typeof via !== "string" || !observedVia.has(via))
133
+ return UNKNOWN_PROVENANCE;
134
+ return {
135
+ sourceKind: kind,
136
+ observedVia: via
137
+ };
138
+ }
99
139
  export function normalizeMeter(raw) {
100
140
  if (typeof raw.provider !== "string" || !providerCodes.has(raw.provider))
101
141
  return null;
@@ -126,6 +166,10 @@ export function normalizeMeter(raw) {
126
166
  return null;
127
167
  if (Date.parse(raw.expiresAt) < Date.parse(raw.observedAt))
128
168
  return null;
169
+ const account = readAccountId(raw.accountId);
170
+ if (!account.ok)
171
+ return null;
172
+ const provenance = readProvenance(raw.provenance);
129
173
  const amounts = normalizeAmounts(raw);
130
174
  return {
131
175
  provider: raw.provider,
@@ -139,7 +183,9 @@ export function normalizeMeter(raw) {
139
183
  observedAt: raw.observedAt,
140
184
  expiresAt: raw.expiresAt,
141
185
  labels,
142
- ...(amounts === null ? {} : amounts)
186
+ ...(amounts === null ? {} : amounts),
187
+ ...(account.accountId === null ? {} : { accountId: account.accountId }),
188
+ ...(provenance === null ? {} : { provenance })
143
189
  };
144
190
  }
145
191
  export function normalizeMeters(rawMeters) {
@@ -1 +1 @@
1
- {"version":3,"file":"normalizer.js","sourceRoot":"","sources":["../src/normalizer.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBA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AC,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC;AACzC,CAAC"}
@@ -0,0 +1,75 @@
1
+ /**
2
+ * When a connection may next ask its provider a question.
3
+ *
4
+ * Background collection is the difference between a quota monitor and a file
5
+ * viewer, and a collector that retries a failing provider on a fixed interval
6
+ * is how a monitor gets an account rate limited. The whole policy lives here as
7
+ * arithmetic over injected values: the caller supplies the clock and the random
8
+ * source, so a schedule is exactly reproducible in a test and identical on all
9
+ * three operating systems.
10
+ */
11
+ /** Longest our own backoff will ever grow to on its own. */
12
+ export declare const REFRESH_BACKOFF_CEILING_SECONDS = 3600;
13
+ /**
14
+ * Longest a provider may push us out with Retry-After.
15
+ *
16
+ * The header is honoured because the provider knows its own limits better than
17
+ * we do, but a header is still input from outside, and a day is already far
18
+ * past the point where a person would reconnect by hand.
19
+ */
20
+ export declare const REFRESH_RETRY_AFTER_CEILING_SECONDS = 86400;
21
+ /** How far either side of the computed delay the jitter may move it. */
22
+ export declare const REFRESH_JITTER_RATIO = 0.2;
23
+ /** The reader supplied sentinel for a connection that only refreshes by hand. */
24
+ export declare const SCHEDULE_EXEMPT_BASE_SECONDS = 0;
25
+ /** Missing wire cadence fails closed to the most conservative live default. */
26
+ export declare const REFRESH_FALLBACK_BASE_SECONDS = 300;
27
+ /** A stable scheduled instant for a connection exempt from background reads. */
28
+ export declare const SCHEDULE_EXEMPT_INSTANT = "9999-12-31T23:59:59.999Z";
29
+ export interface RefreshInput {
30
+ /**
31
+ * Consecutive failures behind this schedule. Zero means the last attempt
32
+ * succeeded, so the delay is the plain interval with jitter on it.
33
+ */
34
+ attempt: number;
35
+ /** The interval this connection uses when nothing is failing, in seconds. */
36
+ baseSeconds: number;
37
+ /** Retry-After in seconds when the provider sent one, otherwise null. */
38
+ retryAfterSeconds?: number | null;
39
+ /** Injected uniform random in the range zero to one. */
40
+ random: () => number;
41
+ }
42
+ /**
43
+ * The delay before the next attempt, in seconds.
44
+ *
45
+ * Doubling per consecutive failure, capped at an hour, then jittered by up to
46
+ * a fifth either way so a hundred installations that all failed at the same
47
+ * moment do not all come back at the same moment.
48
+ *
49
+ * The order matters and is the whole subtlety here. Jitter is applied to OUR
50
+ * backoff and to nothing else, and the provider's Retry-After is then taken as
51
+ * a floor underneath the jittered result. Jittering a Retry-After instead would
52
+ * subtract up to a fifth from an instruction the provider gave us: a header
53
+ * asking for a day back would come back as nineteen hours and knock on the door
54
+ * almost five hours early, which is precisely the behaviour that gets an account
55
+ * limited harder. Jitter exists to spread our own retries, so it may only ever
56
+ * move our own number. A Retry-After shorter than our backoff changes nothing,
57
+ * because a provider cannot talk us out of backing off.
58
+ */
59
+ export declare function refreshDelaySeconds(input: RefreshInput): number;
60
+ /**
61
+ * The instant of the next attempt, or null when the supplied clock is not a
62
+ * readable instant. Nothing is guessed from a clock we cannot read.
63
+ */
64
+ export declare function nextRefreshAt(now: string, input: RefreshInput): string | null;
65
+ /**
66
+ * Whether a scheduled instant has arrived.
67
+ *
68
+ * A schedule we cannot read counts as due. The alternative is a connection that
69
+ * never refreshes again because one unreadable value got written once, and the
70
+ * first successful attempt replaces it with a readable one. A clock we cannot
71
+ * read is the opposite case and counts as not due, because with no present
72
+ * moment there is nothing to compare against.
73
+ */
74
+ export declare function isDue(nextAt: string | null, now: string): boolean;
75
+ //# sourceMappingURL=schedule.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schedule.d.ts","sourceRoot":"","sources":["../src/schedule.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,4DAA4D;AAC5D,eAAO,MAAM,+BAA+B,OAAQ,CAAC;AAErD;;;;;;GAMG;AACH,eAAO,MAAM,mCAAmC,QAAS,CAAC;AAE1D,wEAAwE;AACxE,eAAO,MAAM,oBAAoB,MAAM,CAAC;AAExC,iFAAiF;AACjF,eAAO,MAAM,4BAA4B,IAAI,CAAC;AAE9C,+EAA+E;AAC/E,eAAO,MAAM,6BAA6B,MAAM,CAAC;AAEjD,gFAAgF;AAChF,eAAO,MAAM,uBAAuB,6BAA6B,CAAC;AAElE,MAAM,WAAW,YAAY;IAC3B;;;OAGG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB,6EAA6E;IAC7E,WAAW,EAAE,MAAM,CAAC;IACpB,yEAAyE;IACzE,iBAAiB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAClC,wDAAwD;IACxD,MAAM,EAAE,MAAM,MAAM,CAAC;CACtB;AAuCD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,YAAY,GAAG,MAAM,CAY/D;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,YAAY,GAAG,MAAM,GAAG,IAAI,CAU7E;AAED;;;;;;;;GAQG;AACH,wBAAgB,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAOjE"}
@@ -0,0 +1,131 @@
1
+ /**
2
+ * When a connection may next ask its provider a question.
3
+ *
4
+ * Background collection is the difference between a quota monitor and a file
5
+ * viewer, and a collector that retries a failing provider on a fixed interval
6
+ * is how a monitor gets an account rate limited. The whole policy lives here as
7
+ * arithmetic over injected values: the caller supplies the clock and the random
8
+ * source, so a schedule is exactly reproducible in a test and identical on all
9
+ * three operating systems.
10
+ */
11
+ /** Longest our own backoff will ever grow to on its own. */
12
+ export const REFRESH_BACKOFF_CEILING_SECONDS = 3_600;
13
+ /**
14
+ * Longest a provider may push us out with Retry-After.
15
+ *
16
+ * The header is honoured because the provider knows its own limits better than
17
+ * we do, but a header is still input from outside, and a day is already far
18
+ * past the point where a person would reconnect by hand.
19
+ */
20
+ export const REFRESH_RETRY_AFTER_CEILING_SECONDS = 86_400;
21
+ /** How far either side of the computed delay the jitter may move it. */
22
+ export const REFRESH_JITTER_RATIO = 0.2;
23
+ /** The reader supplied sentinel for a connection that only refreshes by hand. */
24
+ export const SCHEDULE_EXEMPT_BASE_SECONDS = 0;
25
+ /** Missing wire cadence fails closed to the most conservative live default. */
26
+ export const REFRESH_FALLBACK_BASE_SECONDS = 300;
27
+ /** A stable scheduled instant for a connection exempt from background reads. */
28
+ export const SCHEDULE_EXEMPT_INSTANT = "9999-12-31T23:59:59.999Z";
29
+ function usableRandom(random) {
30
+ let value;
31
+ try {
32
+ value = random();
33
+ }
34
+ catch {
35
+ return 0.5;
36
+ }
37
+ if (typeof value !== "number" || !Number.isFinite(value))
38
+ return 0.5;
39
+ return Math.min(Math.max(value, 0), 1);
40
+ }
41
+ function usableBase(baseSeconds) {
42
+ if (!Number.isFinite(baseSeconds) || baseSeconds < 0) {
43
+ return REFRESH_FALLBACK_BASE_SECONDS;
44
+ }
45
+ if (baseSeconds === SCHEDULE_EXEMPT_BASE_SECONDS) {
46
+ return Number.POSITIVE_INFINITY;
47
+ }
48
+ return Math.min(baseSeconds, REFRESH_BACKOFF_CEILING_SECONDS);
49
+ }
50
+ function usableAttempt(attempt) {
51
+ if (!Number.isFinite(attempt) || attempt <= 0)
52
+ return 0;
53
+ /*
54
+ * The exponent is clamped before it is used rather than after. Two to the
55
+ * power of a large attempt count overflows to Infinity long before the
56
+ * ceiling below could catch it, and Infinity times a jitter factor is not a
57
+ * date.
58
+ */
59
+ return Math.min(Math.floor(attempt), 32);
60
+ }
61
+ function usableRetryAfter(value) {
62
+ if (typeof value !== "number" || !Number.isFinite(value) || value <= 0)
63
+ return null;
64
+ return Math.min(value, REFRESH_RETRY_AFTER_CEILING_SECONDS);
65
+ }
66
+ /**
67
+ * The delay before the next attempt, in seconds.
68
+ *
69
+ * Doubling per consecutive failure, capped at an hour, then jittered by up to
70
+ * a fifth either way so a hundred installations that all failed at the same
71
+ * moment do not all come back at the same moment.
72
+ *
73
+ * The order matters and is the whole subtlety here. Jitter is applied to OUR
74
+ * backoff and to nothing else, and the provider's Retry-After is then taken as
75
+ * a floor underneath the jittered result. Jittering a Retry-After instead would
76
+ * subtract up to a fifth from an instruction the provider gave us: a header
77
+ * asking for a day back would come back as nineteen hours and knock on the door
78
+ * almost five hours early, which is precisely the behaviour that gets an account
79
+ * limited harder. Jitter exists to spread our own retries, so it may only ever
80
+ * move our own number. A Retry-After shorter than our backoff changes nothing,
81
+ * because a provider cannot talk us out of backing off.
82
+ */
83
+ export function refreshDelaySeconds(input) {
84
+ const base = usableBase(input.baseSeconds);
85
+ if (base === Number.POSITIVE_INFINITY)
86
+ return base;
87
+ const attempt = usableAttempt(input.attempt);
88
+ const backoff = Math.min(base * Math.pow(2, attempt), REFRESH_BACKOFF_CEILING_SECONDS);
89
+ const jitter = 1 + (usableRandom(input.random) * 2 - 1) * REFRESH_JITTER_RATIO;
90
+ const jittered = backoff * jitter;
91
+ const retryAfter = usableRetryAfter(input.retryAfterSeconds);
92
+ return retryAfter === null ? jittered : Math.max(jittered, retryAfter);
93
+ }
94
+ /**
95
+ * The instant of the next attempt, or null when the supplied clock is not a
96
+ * readable instant. Nothing is guessed from a clock we cannot read.
97
+ */
98
+ export function nextRefreshAt(now, input) {
99
+ const current = Date.parse(now);
100
+ if (!Number.isFinite(current))
101
+ return null;
102
+ if (input.baseSeconds === SCHEDULE_EXEMPT_BASE_SECONDS) {
103
+ return SCHEDULE_EXEMPT_INSTANT;
104
+ }
105
+ const milliseconds = current + Math.round(refreshDelaySeconds(input) * 1_000);
106
+ if (!Number.isFinite(milliseconds))
107
+ return null;
108
+ const next = new Date(milliseconds);
109
+ return Number.isFinite(next.getTime()) ? next.toISOString() : null;
110
+ }
111
+ /**
112
+ * Whether a scheduled instant has arrived.
113
+ *
114
+ * A schedule we cannot read counts as due. The alternative is a connection that
115
+ * never refreshes again because one unreadable value got written once, and the
116
+ * first successful attempt replaces it with a readable one. A clock we cannot
117
+ * read is the opposite case and counts as not due, because with no present
118
+ * moment there is nothing to compare against.
119
+ */
120
+ export function isDue(nextAt, now) {
121
+ const current = Date.parse(now);
122
+ if (!Number.isFinite(current))
123
+ return false;
124
+ if (nextAt === null)
125
+ return true;
126
+ const scheduled = Date.parse(nextAt);
127
+ if (!Number.isFinite(scheduled))
128
+ return true;
129
+ return current >= scheduled;
130
+ }
131
+ //# sourceMappingURL=schedule.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schedule.js","sourceRoot":"","sources":["../src/schedule.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,4DAA4D;AAC5D,MAAM,CAAC,MAAM,+BAA+B,GAAG,KAAK,CAAC;AAErD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,mCAAmC,GAAG,MAAM,CAAC;AAE1D,wEAAwE;AACxE,MAAM,CAAC,MAAM,oBAAoB,GAAG,GAAG,CAAC;AAExC,iFAAiF;AACjF,MAAM,CAAC,MAAM,4BAA4B,GAAG,CAAC,CAAC;AAE9C,+EAA+E;AAC/E,MAAM,CAAC,MAAM,6BAA6B,GAAG,GAAG,CAAC;AAEjD,gFAAgF;AAChF,MAAM,CAAC,MAAM,uBAAuB,GAAG,0BAA0B,CAAC;AAgBlE,SAAS,YAAY,CAAC,MAAoB;IACxC,IAAI,KAAa,CAAC;IAClB,IAAI,CAAC;QACH,KAAK,GAAG,MAAM,EAAE,CAAC;IACnB,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,GAAG,CAAC;IACb,CAAC;IACD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC;QAAE,OAAO,GAAG,CAAC;IACrE,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AACzC,CAAC;AAED,SAAS,UAAU,CAAC,WAAmB;IACrC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,WAAW,CAAC,IAAI,WAAW,GAAG,CAAC,EAAE,CAAC;QACrD,OAAO,6BAA6B,CAAC;IACvC,CAAC;IACD,IAAI,WAAW,KAAK,4BAA4B,EAAE,CA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AM,CAAC,CAAC;IACrC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAC;QAAE,OAAO,IAAI,CAAC;IAC7C,OAAO,OAAO,IAAI,SAAS,CAAC;AAC9B,CAAC"}
package/dist/types.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- export declare const PROVIDER_CODES: readonly ["CLAUDE", "OPENROUTER", "CODEX", "ANTIGRAVITY", "OPENCODE", "MANUAL"];
1
+ import type { ConnectionStatus } from "./connection-state.js";
2
+ export declare const PROVIDER_CODES: readonly ["CLAUDE", "OPENROUTER", "CODEX", "ANTIGRAVITY", "GEMINI_CLI", "OPENCODE", "GROK", "KIMI", "MANUAL"];
2
3
  export type ProviderCode = (typeof PROVIDER_CODES)[number];
3
4
  export type SnapshotState = "fresh" | "stale" | "unknown";
4
5
  export type SnapshotUnit = "PERCENT" | "CREDITS" | "TOKENS" | "REQUESTS";
@@ -31,6 +32,47 @@ export interface SnapshotAmounts {
31
32
  limitAmount: number;
32
33
  currency: SnapshotCurrency;
33
34
  }
35
+ /**
36
+ * How an observation reached us, stated in our own vocabulary.
37
+ *
38
+ * A provider tells us what its meter reads. It never tells us how we read it,
39
+ * so this field is written by OpenLimiter and only ever holds one of the values
40
+ * below. A human surface can then say "this came from your local Claude Code"
41
+ * or "this came from an official API" without a provider being able to write
42
+ * that sentence for us.
43
+ */
44
+ export declare const SNAPSHOT_SOURCE_KINDS: readonly ["statusline_payload", "explicit_ingest", "manual_document", "remote_api", "unknown"];
45
+ export type SnapshotSourceKind = (typeof SNAPSHOT_SOURCE_KINDS)[number];
46
+ /**
47
+ * Which reader carried the observation. Closed for the same reason as above.
48
+ *
49
+ * The first three name a concrete path this product actually has, because a
50
+ * source chip saying "your Claude Code statusline" and one saying "you imported
51
+ * this" are the difference between a live reading and a paste, and a generic
52
+ * reader kind cannot tell a person which they are looking at. The rest are the
53
+ * reader kinds the connection architecture defines, kept for paths that exist
54
+ * on paper before they exist in code.
55
+ */
56
+ export declare const SNAPSHOT_OBSERVED_VIA: readonly ["claude_code_statusline", "ingest_command", "manual_json", "local_event", "local_file", "local_command", "remote_http", "user_entry", "unknown"];
57
+ export type SnapshotObservedVia = (typeof SNAPSHOT_OBSERVED_VIA)[number];
58
+ export interface SnapshotProvenance {
59
+ sourceKind: SnapshotSourceKind;
60
+ observedVia: SnapshotObservedVia;
61
+ }
62
+ /**
63
+ * What a reading's provenance becomes when the stated provenance cannot be
64
+ * believed. The reading itself still stands: how a number arrived is a separate
65
+ * question from whether the number is in range.
66
+ */
67
+ export declare const UNKNOWN_PROVENANCE: SnapshotProvenance;
68
+ /**
69
+ * Shape of an account identifier, when a source names one.
70
+ *
71
+ * Lowercase, digits and hyphens, never a space, so it can be joined into a cache
72
+ * identity with a space separator and never collide. It is an opaque local alias
73
+ * and is not required to be an account name any provider would recognise.
74
+ */
75
+ export declare const ACCOUNT_ID_PATTERN: RegExp;
34
76
  export interface ConnectorLabels {
35
77
  credentialOrigin: "official-local-tool" | "user-key" | "browser-session" | "user-entered";
36
78
  dataInterfaceStatus: "native-statusline-payload" | "documented-api" | "internal-endpoint" | "authenticated-scrape" | "manual";
@@ -53,6 +95,16 @@ export interface Snapshot {
53
95
  usedAmount?: number;
54
96
  limitAmount?: number;
55
97
  currency?: SnapshotCurrency;
98
+ /**
99
+ * Which account this reading belongs to, when a source names one.
100
+ *
101
+ * Absent means one unnamed account, which is every reading written before
102
+ * multiple accounts existed. Absent is not the same as an account called
103
+ * "default": a row without this field keeps the identity it always had.
104
+ */
105
+ accountId?: string;
106
+ /** How the reading arrived. Absent means it was never recorded. */
107
+ provenance?: SnapshotProvenance;
56
108
  }
57
109
  export interface RawMeter {
58
110
  provider: unknown;
@@ -69,26 +121,66 @@ export interface RawMeter {
69
121
  usedAmount?: unknown;
70
122
  limitAmount?: unknown;
71
123
  currency?: unknown;
124
+ accountId?: unknown;
125
+ provenance?: unknown;
72
126
  }
73
127
  export interface ConnectorReadContext {
74
128
  payload?: unknown;
75
129
  now: string;
76
130
  environment: Readonly<Record<string, string | undefined>>;
77
131
  }
132
+ /**
133
+ * What a reader answers, and how its connection is doing while it answers.
134
+ *
135
+ * `connection` is optional so every existing caller keeps compiling, and it is
136
+ * the same value on both branches on purpose: a reader that returned meters can
137
+ * still be degraded, and a reader that returned nothing still owes a person one
138
+ * sentence about what to do. The status is written by OpenLimiter from the
139
+ * closed vocabulary in connection-state.ts, never by a provider payload.
140
+ */
78
141
  export type ConnectorResult = {
79
142
  ok: true;
80
143
  meters: readonly RawMeter[];
144
+ connection?: ConnectionStatus;
81
145
  } | {
82
146
  ok: false;
83
147
  reason: "unknown" | "unavailable" | "not_configured";
148
+ connection?: ConnectionStatus;
84
149
  };
150
+ /**
151
+ * What a connector's payload IS, before its parser sees it.
152
+ *
153
+ * Almost every provider answers JSON. OpenCode answers a logged in HTML page,
154
+ * because it publishes no usage interface at all. That difference cannot live
155
+ * only in the reader that happens to know about it: the desktop pipeline, the
156
+ * ingest command and the web engine all hand payloads to parsers, and any one
157
+ * of them that assumes JSON silently breaks the text reader. So the connector
158
+ * declares it, and every payload boundary asks.
159
+ */
160
+ export type ConnectorEncoding = "json" | "text";
161
+ /**
162
+ * How much the INTERFACE behind a reader can be relied on.
163
+ *
164
+ * Not how good the parser is. A reader pointed at a documented response and a
165
+ * reader pointed at a rendered page can both be flawless and still deserve
166
+ * different sentences on a surface, because only one of them has a contract
167
+ * behind it. Absent means stable, so a reader says nothing unless it has
168
+ * something to admit.
169
+ */
170
+ export type ConnectorMaturity = "stable" | "beta";
85
171
  export interface ConnectorContract {
86
172
  readonly id: Lowercase<ProviderCode>;
87
173
  readonly displayName: string;
88
174
  readonly labels: ConnectorLabels;
175
+ /** Whether this connector's parser wants parsed JSON or the raw text. */
176
+ readonly encoding: ConnectorEncoding;
177
+ /** Stated only when it is not stable, so silence is never a claim. */
178
+ readonly maturity?: ConnectorMaturity;
89
179
  detect(environment: Readonly<Record<string, string | undefined>>): boolean;
90
180
  read(context: ConnectorReadContext): Promise<ConnectorResult>;
91
181
  }
182
+ /** A reader's maturity, with the default spelled out rather than assumed. */
183
+ export declare function connectorMaturity(connector: ConnectorContract): ConnectorMaturity;
92
184
  export type AdviceReason = "HEALTHY" | "NEAR_CAP" | "AT_CAP" | "UNKNOWN";
93
185
  export type AdviceRecommendation = {
94
186
  code: "PREFER";
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,cAAc,iFAOjB,CAAC;AAEX,MAAM,MAAM,YAAY,GAAG,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC;AAC3D,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,OAAO,GAAG,SAAS,CAAC;AAC1D,MAAM,MAAM,YAAY,GAAG,SAAS,GAAG,SAAS,GAAG,QAAQ,GAAG,UAAU,CAAC;AACzE,MAAM,MAAM,iBAAiB,GAAG,OAAO,GAAG,WAAW,GAAG,QAAQ,CAAC;AACjE,MAAM,MAAM,cAAc,GACtB,gBAAgB,GAChB,gBAAgB,GAChB,kBAAkB,GAClB,oBAAoB,GACpB,cAAc,CAAC;AAEnB,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,SAAS,GAAG,OAAO,GAAG,UAAU,GAAG,SAAS,CAAC;IACnD,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED;;;;;;GAMG;AACH,MAAM,MAAM,gBAAgB,GAAG,KAAK,CAAC;AAErC,6EAA6E;AAC7E,eAAO,MAAM,mBAAmB,UAAY,CAAC;AAE7C;;;;;;;GAOG;AACH,MAAM,WAAW,eAAe;IAC9B,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,gBAAgB,CAAC;CAC5B;AAED,MAAM,WAAW,eAAe;IAC9B,gBAAgB,EACZ,qBAAqB,GACrB,UAAU,GACV,iBAAiB,GACjB,cAAc,CAAC;IACnB,mBAAmB,EACf,2BA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AC;CACxB;AAED,MAAM,WAAW,MAAM;IACrB,MAAM,EAAE,OAAO,CAAC;IAChB,MAAM,EAAE,YAAY,CAAC;IACrB,cAAc,EAAE,oBAAoB,CAAC;IACrC,SAAS,EAAE,SAAS,cAAc,EAAE,CAAC;IACrC,gBAAgB,EAAE,SAAS,YAAY,EAAE,CAAC;CAC3C;AAED,MAAM,WAAW,QAAQ;IACvB,eAAe,EAAE,MAAM,CAAC;IACxB,iBAAiB,EAAE,MAAM,GAAG,IAAI,CAAC;CAClC"}
package/dist/types.js CHANGED
@@ -3,9 +3,70 @@ export const PROVIDER_CODES = [
3
3
  "OPENROUTER",
4
4
  "CODEX",
5
5
  "ANTIGRAVITY",
6
+ "GEMINI_CLI",
6
7
  "OPENCODE",
8
+ "GROK",
9
+ "KIMI",
7
10
  "MANUAL"
8
11
  ];
9
12
  /** Largest money amount a reading may carry. Above this it is not a plan. */
10
13
  export const MAX_SNAPSHOT_AMOUNT = 1_000_000;
14
+ /**
15
+ * How an observation reached us, stated in our own vocabulary.
16
+ *
17
+ * A provider tells us what its meter reads. It never tells us how we read it,
18
+ * so this field is written by OpenLimiter and only ever holds one of the values
19
+ * below. A human surface can then say "this came from your local Claude Code"
20
+ * or "this came from an official API" without a provider being able to write
21
+ * that sentence for us.
22
+ */
23
+ export const SNAPSHOT_SOURCE_KINDS = [
24
+ "statusline_payload",
25
+ "explicit_ingest",
26
+ "manual_document",
27
+ "remote_api",
28
+ "unknown"
29
+ ];
30
+ /**
31
+ * Which reader carried the observation. Closed for the same reason as above.
32
+ *
33
+ * The first three name a concrete path this product actually has, because a
34
+ * source chip saying "your Claude Code statusline" and one saying "you imported
35
+ * this" are the difference between a live reading and a paste, and a generic
36
+ * reader kind cannot tell a person which they are looking at. The rest are the
37
+ * reader kinds the connection architecture defines, kept for paths that exist
38
+ * on paper before they exist in code.
39
+ */
40
+ export const SNAPSHOT_OBSERVED_VIA = [
41
+ "claude_code_statusline",
42
+ "ingest_command",
43
+ "manual_json",
44
+ "local_event",
45
+ "local_file",
46
+ "local_command",
47
+ "remote_http",
48
+ "user_entry",
49
+ "unknown"
50
+ ];
51
+ /**
52
+ * What a reading's provenance becomes when the stated provenance cannot be
53
+ * believed. The reading itself still stands: how a number arrived is a separate
54
+ * question from whether the number is in range.
55
+ */
56
+ export const UNKNOWN_PROVENANCE = {
57
+ sourceKind: "unknown",
58
+ observedVia: "unknown"
59
+ };
60
+ /**
61
+ * Shape of an account identifier, when a source names one.
62
+ *
63
+ * Lowercase, digits and hyphens, never a space, so it can be joined into a cache
64
+ * identity with a space separator and never collide. It is an opaque local alias
65
+ * and is not required to be an account name any provider would recognise.
66
+ */
67
+ export const ACCOUNT_ID_PATTERN = /^[a-z0-9][a-z0-9-]{0,63}$/u;
68
+ /** A reader's maturity, with the default spelled out rather than assumed. */
69
+ export function connectorMaturity(connector) {
70
+ return connector.maturity ?? "stable";
71
+ }
11
72
  //# sourceMappingURL=types.js.map
package/dist/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,cAAc,GAAG;IAC5B,QAAQ;IACR,YAAY;IACZ,OAAO;IACP,aAAa;IACb,UAAU;IACV,QAAQ;CACA,CAAC;AA2BX,6EAA6E;AAC7E,MAAM,CAAC,MAAM,mBAAmB,GAAG,SAAS,CAAC"}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,cAAc,GAAG;IAC5B,QAAQ;IACR,YAAY;IACZ,OAAO;IACP,aAAa;IACb,YAAY;IACZ,UAAU;IACV,MAAM;IACN,MAAM;IACN,QAAQ;CACA,CAAC;AA2BX,6EAA6E;AAC7E,MAAM,CAAC,MAAM,mBAAmB,GAAG,SAAS,CAAC;AAgB7C;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG;IACnC,oBAAoB;IACpB,iBAAiB;IACjB,iBAAiB;IACjB,YAAY;IACZ,SAAS;CACD,CAAC;AAIX;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG;IACnC,wBAAwB;IACxB,gBAAgB;IAChB,aAAa;IACb,aAAa;IACb,YAAY;IACZ,eAAe;IACf,aAAa;IACb,YAAY;IACZ,SAAS;CACD,CAAC;AASX;;;;GAIG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAuB;IACpD,UAAU,EAAE,SAAS;IACrB,WAAW,EAAE,SAAS;CACvB,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,4BAA4B,CAAC;AA2H/D,6EAA6E;AAC7E,MAAM,UAAU,iBAAiB,CAAC,SAA4B;IAC5D,OAAO,SAAS,CAAC,QAAQ,IAAI,QAAQ,CAAC;AACxC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openlimiter/core",
3
- "version": "0.2.0",
3
+ "version": "1.2.0",
4
4
  "description": "Shared types, quota models, cache handling, and forecasts for OpenLimiter.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -42,7 +42,8 @@
42
42
  "node": ">=24"
43
43
  },
44
44
  "publishConfig": {
45
- "access": "public"
45
+ "access": "public",
46
+ "provenance": true
46
47
  },
47
48
  "scripts": {
48
49
  "build": "tsc -p tsconfig.json",