@ottimis/jack-provider-sdk 0.1.0 → 0.3.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/dist/backend.d.ts +38 -12
- package/dist/backend.d.ts.map +1 -1
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/usage.js +34 -0
- package/dist/cjs/usage.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/provider.d.ts +151 -55
- package/dist/provider.d.ts.map +1 -1
- package/dist/usage.d.ts +219 -0
- package/dist/usage.d.ts.map +1 -0
- package/dist/usage.js +33 -0
- package/dist/usage.js.map +1 -0
- package/package.json +5 -3
- package/src/backend.ts +28 -12
- package/src/index.ts +1 -0
- package/src/provider.ts +161 -44
- package/src/usage.ts +228 -0
package/src/usage.ts
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Usage / billing capability — provider-owned data flow.
|
|
3
|
+
*
|
|
4
|
+
* Each provider knows how to talk to its own billing surface (Claude
|
|
5
|
+
* cookie API for Pro/Max, OpenAI usage endpoints for Codex, Gemini's
|
|
6
|
+
* Google Cloud quotas, …) and how to map its SDK's per-message token
|
|
7
|
+
* counts into a canonical shape. The host runs a generic poll loop and
|
|
8
|
+
* a generic chip — it never special-cases any provider.
|
|
9
|
+
*
|
|
10
|
+
* Two surfaces:
|
|
11
|
+
*
|
|
12
|
+
* - `fetch()` for **account-level** snapshots. Pulled by a host poller
|
|
13
|
+
* on `recommendedPollIntervalSec` cadence (clamped to host bounds).
|
|
14
|
+
* What "account" means is provider-defined: Claude → org, Codex →
|
|
15
|
+
* OpenAI project, Gemini → Cloud Billing project.
|
|
16
|
+
*
|
|
17
|
+
* - `formatSessionMetrics()` for **per-session** translation. The
|
|
18
|
+
* manager already calls `backend.getContextUsage()` after every
|
|
19
|
+
* `assistant` message; that returns a loose `AgentContextUsage`
|
|
20
|
+
* bag. This hook lets the provider lift it into canonical
|
|
21
|
+
* {@link UsageMetric}[] without the host trying to interpret
|
|
22
|
+
* provider-specific fields.
|
|
23
|
+
*
|
|
24
|
+
* Single source of truth: the provider. Host plumbs, never decodes.
|
|
25
|
+
*
|
|
26
|
+
* Optional everywhere — providers without billing visibility (Codex
|
|
27
|
+
* without admin keys, Gemini without OAuth) leave `fetch()` returning
|
|
28
|
+
* an empty `metrics: []`. The capability flag stays `true` if the
|
|
29
|
+
* provider can format per-session metrics, `false` if it has nothing
|
|
30
|
+
* to say at all.
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
import type { AgentContextUsage } from './backend'
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Canonical metric kinds. New kinds extend this union additively when a
|
|
37
|
+
* provider has a meaningfully different shape (e.g. a future
|
|
38
|
+
* `quarterly_burn` for enterprise billing). Adding a kind is a minor
|
|
39
|
+
* SDK bump; the renderer's dispatch table grows alongside.
|
|
40
|
+
*/
|
|
41
|
+
export type UsageMetric = TimeWindowMetric | TokenUtilizationMetric | MonthlySpendMetric
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Rolling-window utilization. The classic Claude Pro/Max bucket
|
|
45
|
+
* (5-hour, 7-day) — utilization 0..1 with a hard reset boundary.
|
|
46
|
+
*
|
|
47
|
+
* Optional `used` / `limit` / `unit` carry raw counts when the
|
|
48
|
+
* provider exposes them (Gemini free-tier daily quota: 12 of 50
|
|
49
|
+
* requests). Claude's cookie API gives only `utilization`, so those
|
|
50
|
+
* stay undefined and the chip falls back to "X% of N-hour usage".
|
|
51
|
+
*/
|
|
52
|
+
export type TimeWindowMetric = {
|
|
53
|
+
kind: 'time_window'
|
|
54
|
+
/** Stable, provider-defined key. e.g. `'five_hour'`, `'seven_day_opus'`, `'daily'`. */
|
|
55
|
+
id: string
|
|
56
|
+
/** Human-readable label for UI. Provider supplies (i18n is its concern). */
|
|
57
|
+
label: string
|
|
58
|
+
/** Window utilization in [0, 1]. */
|
|
59
|
+
utilization: number
|
|
60
|
+
/** Optional: raw count consumed in the window (when known). */
|
|
61
|
+
used?: number
|
|
62
|
+
/** Optional: raw cap of the window (when known). */
|
|
63
|
+
limit?: number
|
|
64
|
+
/** Optional: unit of `used`/`limit`. Default `'tokens'`. Chip uses for formatting. */
|
|
65
|
+
unit?: 'tokens' | 'requests' | (string & {})
|
|
66
|
+
/** ISO 8601 timestamp when the window resets. */
|
|
67
|
+
resetsAt: string
|
|
68
|
+
/** Window length in seconds. Used for elapsed-progress UI. */
|
|
69
|
+
windowSeconds: number
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Count-based utilization without a time boundary. Used for context
|
|
74
|
+
* window pressure ("X tokens of Y max") and any cumulative provider
|
|
75
|
+
* metric that doesn't reset on a clock (lifetime / billing-period
|
|
76
|
+
* tokens, etc).
|
|
77
|
+
*
|
|
78
|
+
* `max` is optional — a provider tracking total token consumption
|
|
79
|
+
* without a hard cap (analytics, not gating) leaves it undefined and
|
|
80
|
+
* the chip shows just the count.
|
|
81
|
+
*/
|
|
82
|
+
export type TokenUtilizationMetric = {
|
|
83
|
+
kind: 'token_utilization'
|
|
84
|
+
/** Stable, provider-defined key. e.g. `'context'`, `'session_total'`. */
|
|
85
|
+
id: string
|
|
86
|
+
/** Human-readable label for UI. */
|
|
87
|
+
label: string
|
|
88
|
+
/** Tokens consumed. */
|
|
89
|
+
used: number
|
|
90
|
+
/** Optional cap. Undefined = no cap, chip hides the ratio. */
|
|
91
|
+
max?: number
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Spend on a billing cycle (typically monthly). Only meaningful for
|
|
96
|
+
* providers using API-key auth — subscription users have rolling-window
|
|
97
|
+
* quotas, not $-spend.
|
|
98
|
+
*
|
|
99
|
+
* `budgetUsd` is optional: most users don't set a budget, so the chip
|
|
100
|
+
* shows raw spend without a denominator. When present, chip can render
|
|
101
|
+
* a budget bar.
|
|
102
|
+
*/
|
|
103
|
+
export type MonthlySpendMetric = {
|
|
104
|
+
kind: 'monthly_spend'
|
|
105
|
+
id: string
|
|
106
|
+
/** Cycle label, e.g. "May 2026" or "Current cycle". */
|
|
107
|
+
label: string
|
|
108
|
+
spentUsd: number
|
|
109
|
+
budgetUsd?: number
|
|
110
|
+
/** ISO 8601 — start of the billing cycle. */
|
|
111
|
+
cycleStart: string
|
|
112
|
+
/** ISO 8601 — end of the billing cycle. */
|
|
113
|
+
cycleEnd: string
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* One snapshot of provider-side usage data, as returned by
|
|
118
|
+
* {@link UsageApi.fetch}. `metrics` may be empty when the provider has
|
|
119
|
+
* no account-level data to report (Codex without billing key, etc.).
|
|
120
|
+
*/
|
|
121
|
+
export type UsageSnapshot = {
|
|
122
|
+
metrics: UsageMetric[]
|
|
123
|
+
/** ISO 8601 — when this snapshot was observed. */
|
|
124
|
+
observedAt: string
|
|
125
|
+
/** Verbatim provider payload, kept for debug / future analytics.
|
|
126
|
+
* Never consumed by host or chip directly. */
|
|
127
|
+
raw?: unknown
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Connection / authorization state of the provider's usage surface.
|
|
132
|
+
* Surface in the chip's pop-over so the user knows whether they're
|
|
133
|
+
* tracking real numbers or seeing a cached / disconnected snapshot.
|
|
134
|
+
*/
|
|
135
|
+
export type UsageStatus = {
|
|
136
|
+
connected: boolean
|
|
137
|
+
/** Optional human-readable identity (org name, email, project id). */
|
|
138
|
+
identity?: string
|
|
139
|
+
/**
|
|
140
|
+
* Provider-defined auth-mode hint. Lets the renderer adapt copy
|
|
141
|
+
* (e.g. "Connected as API user" vs "Pro subscription"). Stable
|
|
142
|
+
* provider-supplied strings; host doesn't interpret beyond
|
|
143
|
+
* passthrough.
|
|
144
|
+
*/
|
|
145
|
+
authMode?: 'subscription' | 'api_key' | (string & {})
|
|
146
|
+
/** Last error message, when not connected. */
|
|
147
|
+
error?: string
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Result of {@link UsageApi.connect}. The `'choose'` branch covers
|
|
152
|
+
* multi-org / multi-project accounts where the user must pick one
|
|
153
|
+
* (Claude's multi-org case). Generalising to a generic option list
|
|
154
|
+
* means the chip's UI doesn't special-case any provider.
|
|
155
|
+
*/
|
|
156
|
+
export type UsageConnectResult =
|
|
157
|
+
| { kind: 'ready'; identity: string }
|
|
158
|
+
| { kind: 'choose'; options: UsageConnectOption[] }
|
|
159
|
+
| { kind: 'error'; error: string }
|
|
160
|
+
| { kind: 'cancelled' }
|
|
161
|
+
|
|
162
|
+
export type UsageConnectOption = {
|
|
163
|
+
id: string
|
|
164
|
+
label: string
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Context handed to {@link UsageApi.connect}. Currently just a parent
|
|
169
|
+
* window for Electron-coupled flows (Claude opens a child BrowserWindow
|
|
170
|
+
* on `claude.ai/login`). Typed as `unknown` here so the SDK doesn't
|
|
171
|
+
* pull in `electron` as a peer dep — the host narrows to
|
|
172
|
+
* `BrowserWindow` at the call site.
|
|
173
|
+
*/
|
|
174
|
+
export type UsageConnectContext = {
|
|
175
|
+
/** Parent window for any modal flow the provider needs to open. */
|
|
176
|
+
parentWindow?: unknown
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Provider-owned usage capability. Optional on {@link JackProvider};
|
|
181
|
+
* absent = host hides the chip's "Connect" affordance and the
|
|
182
|
+
* capability flag is `false`.
|
|
183
|
+
*/
|
|
184
|
+
export type UsageApi = {
|
|
185
|
+
/** Current connection state — used for chip display + gating. */
|
|
186
|
+
status(): Promise<UsageStatus>
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Open the provider's connect flow. Whatever modality the provider
|
|
190
|
+
* needs (login window, API-key picker, OAuth redirect) lives here.
|
|
191
|
+
*/
|
|
192
|
+
connect(ctx: UsageConnectContext): Promise<UsageConnectResult>
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* When `connect()` returned `'choose'`, host calls this with the
|
|
196
|
+
* user's pick. Optional — providers that never choose omit it.
|
|
197
|
+
*/
|
|
198
|
+
selectOption?(optionId: string): Promise<UsageConnectResult>
|
|
199
|
+
|
|
200
|
+
/** Drop credentials and stop any provider-side polling. */
|
|
201
|
+
disconnect(): Promise<void>
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Fetch one fresh account-level snapshot. Empty `metrics: []` is
|
|
205
|
+
* fine when the provider has no billing surface yet — the capability
|
|
206
|
+
* stays `true` for the per-session bridge.
|
|
207
|
+
*/
|
|
208
|
+
fetch(): Promise<UsageSnapshot>
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Recommended poll cadence (seconds). Host clamps to its bounds.
|
|
212
|
+
* Optional — host falls back to its own default if unset.
|
|
213
|
+
*/
|
|
214
|
+
recommendedPollIntervalSec?: number
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Translate the loose {@link AgentContextUsage} the manager pulls from
|
|
218
|
+
* `backend.getContextUsage()` into canonical {@link UsageMetric}[].
|
|
219
|
+
*
|
|
220
|
+
* Pure function (no side effects) — provider knows its own SDK's
|
|
221
|
+
* shape and lifts it. Empty array OK when the raw bag has nothing
|
|
222
|
+
* useful (e.g. fresh session before any message lands).
|
|
223
|
+
*
|
|
224
|
+
* Optional. Providers without per-session token visibility omit it
|
|
225
|
+
* and the chip skips the per-session row.
|
|
226
|
+
*/
|
|
227
|
+
formatSessionMetrics?(raw: AgentContextUsage): UsageMetric[]
|
|
228
|
+
}
|