| 1 | import type { ComputeKind, Reservation } from "./compute"; |
| 2 | import type { User, Viewer } from "./identity"; |
| 3 | import type { RepoPath } from "./repos"; |
| 4 | import type { Result } from "./result"; |
| 5 | |
| 6 | /** Millionths of a US dollar in one dollar: the unit money is held in. */ |
| 7 | export const MICROS_PER_DOLLAR = 1_000_000; |
| 8 | |
| 9 | /** Whether workspaces are charged for agents at all, and with real money. */ |
| 10 | export type BillingStatus = { |
| 11 | /** |
| 12 | * False when no card processor is configured: nothing is charged, and who |
| 13 | * may run agents is decided some other way. |
| 14 | */ |
| 15 | enabled: boolean; |
| 16 | /** False while the card processor is in its test mode, where cards are not real. */ |
| 17 | live: boolean; |
| 18 | /** |
| 19 | * True while g1t is being built out: runs are recorded with what they |
| 20 | * cost, but nothing is charged and no credit is needed. Not forever. |
| 21 | */ |
| 22 | free?: boolean; |
| 23 | }; |
| 24 | |
| 25 | /** A workspace's standing. */ |
| 26 | export type BillingAccount = { |
| 27 | workspace: string; |
| 28 | /** |
| 29 | * Credit left, in millionths of a dollar. Can dip below zero by the cost |
| 30 | * of the runs that were under way when it ran out. |
| 31 | */ |
| 32 | balanceMicros: number; |
| 33 | status: BillingStatus; |
| 34 | /** What is added to a run's cost, in percent. */ |
| 35 | marginPercent: number; |
| 36 | /** The card charged near the limit and when a month closes, if one is saved. */ |
| 37 | card?: { brand: string; last4: string; expMonth: number; expYear: number } | null; |
| 38 | }; |
| 39 | |
| 40 | /** One line of a workspace's statement. */ |
| 41 | export type LedgerEntry = { |
| 42 | id: string; |
| 43 | /** Credit bought with a card, or an agent's run. */ |
| 44 | kind: "top_up" | "usage"; |
| 45 | /** Positive for credit added, negative for usage. */ |
| 46 | amountMicros: number; |
| 47 | description: string; |
| 48 | /** For usage: the repository and pull request the agent worked on. */ |
| 49 | repo: string | null; |
| 50 | number: number | null; |
| 51 | /** For usage: `implement`, `review` or `update`. */ |
| 52 | task: string | null; |
| 53 | /** For usage: the model, by its public name. */ |
| 54 | model: string | null; |
| 55 | /** For usage: who paid the model provider. */ |
| 56 | billedTo: "g1t" | "workspace"; |
| 57 | /** For a top-up: the username of whoever paid. */ |
| 58 | createdBy: string | null; |
| 59 | /** RFC 3339. */ |
| 60 | createdAt: string; |
| 61 | /** The workspace the line belongs to, which tells an enterprise's lines apart. */ |
| 62 | workspace?: string | null; |
| 63 | /** For usage: what the plan's included usage paid of it. `amountMicros` is what is left to pay. */ |
| 64 | creditMicros?: number; |
| 65 | /** For usage: what the workspace's trial credit paid of it. */ |
| 66 | trialMicros?: number; |
| 67 | /** For usage: what g1t's open-source pool paid of it. */ |
| 68 | ossMicros?: number; |
| 69 | /** For usage: what g1t covered itself, such as a trial's last run past its credit. */ |
| 70 | givenMicros?: number; |
| 71 | /** For usage: what the account's discount took off its price. */ |
| 72 | discountMicros?: number; |
| 73 | /** For a credit from g1t, and for what of one expired or was revoked: its kind. */ |
| 74 | creditKind?: CreditKind; |
| 75 | }; |
| 76 | |
| 77 | /** |
| 78 | * Why g1t gave a workspace credit. Promotional (a welcome, a referral) and |
| 79 | * goodwill (an apology) are given away when spent; a refund gives back |
| 80 | * money already paid, and never expires. |
| 81 | */ |
| 82 | export type CreditKind = "promotional" | "goodwill" | "refund" | "purchased"; |
| 83 | |
| 84 | /** One credit g1t gave, with what of it was used: spent before anything paid in advance, the soonest-expiring first. */ |
| 85 | export type CreditGrant = { |
| 86 | /** `crd_…`, the grant's ledger reference. */ |
| 87 | id: string; |
| 88 | workspace: string; |
| 89 | kind: CreditKind; |
| 90 | amountMicros: number; |
| 91 | usedMicros: number; |
| 92 | /** What can still be spent: nothing once it expired or was revoked. */ |
| 93 | leftMicros: number; |
| 94 | note: string; |
| 95 | /** A refund: what it refunds, and the day of it. */ |
| 96 | refundFor?: string | null; |
| 97 | refundDay?: string | null; |
| 98 | /** RFC 3339; null never expires. */ |
| 99 | expiresAt: string | null; |
| 100 | createdBy: string; |
| 101 | createdAt: string; |
| 102 | state: "open" | "used" | "expired" | "revoked"; |
| 103 | closedAt?: string | null; |
| 104 | closedNote?: string | null; |
| 105 | closedBy?: string | null; |
| 106 | /** What expiring or revoking took off the balance. */ |
| 107 | closedMicros?: number; |
| 108 | /** What it pays for: all usage, or models only (spent first). */ |
| 109 | scope?: "all" | "models"; |
| 110 | /** Where it came from. */ |
| 111 | source?: "staff" | "purchase" | "promo_code" | "upgrade"; |
| 112 | }; |
| 113 | |
| 114 | /** A workspace's credits from g1t, newest first. */ |
| 115 | export type Credits = { grants: CreditGrant[]; leftMicros: number }; |
| 116 | |
| 117 | /** One month's credits of one kind: given, spent that month, and taken back unused. */ |
| 118 | export type CreditMonth = { |
| 119 | month: string; |
| 120 | kind: CreditKind; |
| 121 | givenMicros: number; |
| 122 | grants: number; |
| 123 | usedMicros: number; |
| 124 | expiredMicros: number; |
| 125 | revokedMicros: number; |
| 126 | }; |
| 127 | |
| 128 | /** Every credit g1t gave (at most 200, filtered), the last 12 months by kind, and who gave them. */ |
| 129 | export type AdminCredits = { grants: CreditGrant[]; months: CreditMonth[]; staff: string[] }; |
| 130 | |
| 131 | /** What a credit from sudo is, past its amount and note. */ |
| 132 | export type CreditOptions = { |
| 133 | kind: CreditKind; |
| 134 | /** RFC 3339; never for a refund. */ |
| 135 | expiresAt?: string | null; |
| 136 | /** A refund: what it is for, and the day refunded (`YYYY-MM-DD`). */ |
| 137 | refundFor?: string | null; |
| 138 | refundDay?: string | null; |
| 139 | }; |
| 140 | |
| 141 | /** What lets a sandbox, and nothing else, report what its run cost. */ |
| 142 | export type RunTicket = { runId: string; token: string }; |
| 143 | |
| 144 | /** |
| 145 | * What agents cost, charged to the workspace they worked for. A workspace |
| 146 | * buys credit; each run deducts its cost plus g1t's margin; with no credit, |
| 147 | * no agent starts. |
| 148 | */ |
| 149 | /** |
| 150 | * The free allowance on g1t's hosted models for a workspace not otherwise |
| 151 | * open to them: a few dollars of model cost each, out of one pool, until a |
| 152 | * date. Mirrors `Trial` in `crates/contracts/src/billing.rs`. |
| 153 | */ |
| 154 | /** |
| 155 | * How an account is charged. Standard unless g1t set otherwise in sudo: |
| 156 | * custom terms are a discount (0 to 100%), a ceiling, or both. A 100% |
| 157 | * discount charges nothing and shows the usage at its price. `comped` is |
| 158 | * from before discounts and reads as 100%; billing no longer writes it. |
| 159 | */ |
| 160 | export type Terms = { |
| 161 | kind: "standard" | "comped" | "custom"; |
| 162 | discountPercent: number; |
| 163 | ceilingMicros: number | null; |
| 164 | note: string; |
| 165 | until: string | null; |
| 166 | setBy: string | null; |
| 167 | setAt: string | null; |
| 168 | }; |
| 169 | |
| 170 | /** |
| 171 | * Who pays: a workspace's own account, or an enterprise's, which pays for |
| 172 | * several workspaces with one bill and one limit. |
| 173 | */ |
| 174 | export type PayingAccount = { |
| 175 | id: string; |
| 176 | kind: "workspace" | "enterprise"; |
| 177 | name: string; |
| 178 | terms: Terms; |
| 179 | workspaces: string[]; |
| 180 | /** Where an enterprise's invoices go. */ |
| 181 | billingEmail?: string | null; |
| 182 | /** An enterprise's invoices, newest first. */ |
| 183 | invoices?: EnterpriseInvoice[]; |
| 184 | createdAt: string; |
| 185 | /** What g1t staff set for the account beyond its terms. */ |
| 186 | allowances?: Allowances; |
| 187 | }; |
| 188 | |
| 189 | /** Set per account by g1t staff in sudo, on top of its terms. */ |
| 190 | export type Allowances = { |
| 191 | /** The g1t plan without its monthly price; usage is charged as usual. Comped accounts have it anyway. */ |
| 192 | plan: boolean; |
| 193 | /** Each public repository's monthly cap on g1t's open-source pool; null for the default. */ |
| 194 | ossRepoMicros: number | null; |
| 195 | /** Each workspace's trial credit, outside the monthly pool; null for the default. */ |
| 196 | trialMicros: number | null; |
| 197 | /** Agents at once, in place of the plan's (2 in the first month or on the trial, then 10); null for the default. */ |
| 198 | maxConcurrentAgents?: number | null; |
| 199 | /** One run's spend cap, in place of the owners' and the default $2; null for none. */ |
| 200 | runCapMicros?: number | null; |
| 201 | /** What one issue's agents may spend in all, in place of the owners' and the default $10; null for none. */ |
| 202 | issueCapMicros?: number | null; |
| 203 | /** Days of audit log its workspaces keep, in place of the plan's (7 free, 90 on the plan), longer or shorter; null for the plan's. */ |
| 204 | auditRetentionDays?: number | null; |
| 205 | /** A hold on new compute, with why; null for none. */ |
| 206 | hold?: string | null; |
| 207 | }; |
| 208 | |
| 209 | // --- Entitlements, and compute started under a reservation ------------------ |
| 210 | // |
| 211 | // Every service that starts compute asks billing first: |
| 212 | // 1. `entitlements(workspace)`: what it may do at all, its caps, and whether compute is paused. |
| 213 | // 2. `reserve(...)`: holds the estimate against what may pay for it, so starts at the same moment |
| 214 | // cannot overshoot together. Answers who pays first, or refuses with a stable code |
| 215 | // (`not_paid`, `trial_used`, `limit`, `paused`, `oss_pool_empty`) and a message for the owner. |
| 216 | // 3. `settle(reservationId, actualMicros)`: releases the hold. The charge goes on the ledger the usual way. |
| 217 | // A reservation never settled lapses after `RESERVATION_HOURS`. |
| 218 | |
| 219 | /** A reservation that is never settled stops holding after this long. */ |
| 220 | export const RESERVATION_HOURS = 3; |
| 221 | /** What a ceiling reads as when there is none (g1t's own workspaces). */ |
| 222 | export const UNLIMITED_MICROS = 1_000_000_000_000_000; |
| 223 | |
| 224 | /** What a workspace pays g1t on, as far as compute is concerned. Mirrors `PlanKind`. */ |
| 225 | export type PlanKind = "free" | "paid" | "internal" | "enterprise"; |
| 226 | |
| 227 | // `ComputeKind` (what compute is for), `PaidBy` (who pays first: credit, trial, oss, on_demand) and |
| 228 | // `Reservation` are in `./compute`, with the gate that calls `reserve` and `settle`. |
| 229 | |
| 230 | /** One level reached: 50, 75, 90 or 100 percent. */ |
| 231 | export type UsageAlert = { |
| 232 | /** `included` (the plan's included usage), `spend_limit` or `ceiling`. */ |
| 233 | meter: "included" | "spend_limit" | "ceiling" | string; |
| 234 | level: number; |
| 235 | usedMicros: number; |
| 236 | limitMicros: number; |
| 237 | message: string; |
| 238 | }; |
| 239 | |
| 240 | /** An hour's spend well above the workspace's usual: new compute waits for an owner. */ |
| 241 | export type Spike = { |
| 242 | id: string; |
| 243 | /** `open` (waiting), `continued` (keep going) or `stopped`. */ |
| 244 | status: "open" | "continued" | "stopped" | string; |
| 245 | hourMicros: number; |
| 246 | averageMicros: number; |
| 247 | detectedAt: string; |
| 248 | decidedBy?: string | null; |
| 249 | decidedAt?: string | null; |
| 250 | /** While continued: until when, unless spend doubles again first. */ |
| 251 | until?: string | null; |
| 252 | }; |
| 253 | |
| 254 | /** What a workspace may do now. Mirrors `Entitlements` in `crates/contracts/src/billing.rs`. */ |
| 255 | export type Entitlements = { |
| 256 | workspace: string; |
| 257 | plan: PlanKind; |
| 258 | /** May start sandboxes, models, deployments and semantic search at all: paid, internal, enterprise, or free with trial credit left. */ |
| 259 | compute: boolean; |
| 260 | /** The one-time trial credit left; 0 if none or used. */ |
| 261 | trialMicrosLeft: number; |
| 262 | /** A card check has been done; the trial and the open-source pool need it. */ |
| 263 | trialVerified: boolean; |
| 264 | /** A paid workspace still in its first billing cycle. */ |
| 265 | firstMonth: boolean; |
| 266 | /** 2 in the first month or on the trial, 10 after; staff can override it. */ |
| 267 | maxConcurrentAgents: number; |
| 268 | /** 60 in the first month or on the trial; otherwise the guardrails' own caps. */ |
| 269 | maxRunMinutes: number; |
| 270 | /** One run's spend cap, $2 by default; staff can override it. */ |
| 271 | runCapMicros: number; |
| 272 | /** Agent spend on one issue in all, $10 by default. */ |
| 273 | issueCapMicros: number; |
| 274 | /** g1t's ceiling on usage not yet paid for; `UNLIMITED_MICROS` for g1t's own; 0 for free. */ |
| 275 | ceilingMicros: number; |
| 276 | /** Usage not yet paid for this month, prepayment taken off. */ |
| 277 | exposureMicros: number; |
| 278 | /** Why new compute is paused, for the owner; null when it is not. */ |
| 279 | paused: string | null; |
| 280 | /** What open reservations hold now. */ |
| 281 | heldMicros?: number; |
| 282 | /** Paid in advance and not used yet. */ |
| 283 | prepaidMicros?: number; |
| 284 | /** The plan's included usage each month, and what of it is used. */ |
| 285 | includedMicros?: number; |
| 286 | includedUsedMicros?: number; |
| 287 | /** How far back the audit log can be read and exported, and what is kept: the plan's days, or what g1t staff set for the account. */ |
| 288 | auditRetentionDays: number; |
| 289 | /** Whether `auditRetentionDays` is what staff set for the account rather than the plan's. */ |
| 290 | auditRetentionCustom?: boolean; |
| 291 | /** Private repository storage free for every workspace: past it, the plan pays and a free workspace's pushes stop. */ |
| 292 | freePrivateStorageBytes: number; |
| 293 | /** The last daily measure of the workspace's private repositories (a lower bound). */ |
| 294 | privateStorageBytes: number; |
| 295 | /** On a paid plan: package storage past the free amounts is charged, never refused. */ |
| 296 | hasPlan?: boolean; |
| 297 | /** Package storage free for every workspace (public, and private): past it a free workspace's pushes are refused. */ |
| 298 | packagePublicFreeBytes?: number; |
| 299 | packagePrivateFreeBytes?: number; |
| 300 | /** What g1t's open-source pool paid for the workspace this month. */ |
| 301 | ossPaidMicros: number; |
| 302 | /** Deploy build time this month, every second of it metered. */ |
| 303 | buildSecondsUsed?: number; |
| 304 | /** Git operations this month, and how many are free for every workspace. */ |
| 305 | gitOperations?: number; |
| 306 | gitOperationsIncluded?: number; |
| 307 | /** The smallest amount a card is charged when a month closes. */ |
| 308 | minChargeMicros: number; |
| 309 | /** A spend spike waiting for an owner, or decided. */ |
| 310 | spike?: Spike | null; |
| 311 | /** Where usage stands against what is included and the limits, from 50%. */ |
| 312 | alerts?: UsageAlert[]; |
| 313 | }; |
| 314 | |
| 315 | /** A hold on a start's estimated cost, as billing answers it (`Reservation` in `./compute`, and more). */ |
| 316 | export type ReservationHeld = Reservation & { |
| 317 | /** What is held, at cost; may be less than the estimate for a free workspace's last bit of trial. */ |
| 318 | heldMicros?: number; |
| 319 | /** When the hold lapses if never settled. */ |
| 320 | expiresAt?: string; |
| 321 | }; |
| 322 | |
| 323 | /** A request to g1t: a higher limit, or help with usage past what was meant. */ |
| 324 | export type LimitRequest = { |
| 325 | id: string; |
| 326 | workspace: string; |
| 327 | kind: "limit" | "overage" | string; |
| 328 | amountMicros: number; |
| 329 | reason: string; |
| 330 | expectedMonthlyMicros: number; |
| 331 | status: "open" | "approved" | "declined" | string; |
| 332 | decidedMicros?: number | null; |
| 333 | decidedBy?: string | null; |
| 334 | /** The answer, as the owner sees it. */ |
| 335 | answer?: string | null; |
| 336 | createdBy: string; |
| 337 | createdAt: string; |
| 338 | decidedAt?: string | null; |
| 339 | }; |
| 340 | |
| 341 | /** What staff see beside a request. */ |
| 342 | export type WorkspaceHistory = { |
| 343 | plan: PlanKind | null; |
| 344 | months: MonthFigures[]; |
| 345 | paidClearedMicros: number; |
| 346 | payments: number; |
| 347 | disputes: number; |
| 348 | declines: number; |
| 349 | firstSeen: string | null; |
| 350 | ceilingMicros: number | null; |
| 351 | maxCeilingMicros: number | null; |
| 352 | spendLimitMicros: number | null; |
| 353 | lastHourMicros: number; |
| 354 | averageHourMicros: number; |
| 355 | lastDayMicros: number; |
| 356 | }; |
| 357 | |
| 358 | export type LimitRequestReview = { request: LimitRequest; history: WorkspaceHistory }; |
| 359 | |
| 360 | /** What a one-time goodwill credit comes to: the margin on the overage, always, plus its cost up to the cap. */ |
| 361 | export type Goodwill = { |
| 362 | overageMicros: number; |
| 363 | marginMicros: number; |
| 364 | costMicros: number; |
| 365 | creditMicros: number; |
| 366 | absorbedMicros: number; |
| 367 | }; |
| 368 | |
| 369 | /** A workspace whose month went well past its usual, or hit a spike. */ |
| 370 | export type Overage = { |
| 371 | workspace: string; |
| 372 | plan: PlanKind; |
| 373 | typicalMonthMicros: number; |
| 374 | thisMonthMicros: number; |
| 375 | costMicros: number; |
| 376 | marginMicros: number; |
| 377 | spike: Spike | null; |
| 378 | topEntries: LedgerEntry[]; |
| 379 | goodwill: Goodwill; |
| 380 | goodwillAvailable: boolean; |
| 381 | lastGoodwillAt: string | null; |
| 382 | request: LimitRequest | null; |
| 383 | }; |
| 384 | |
| 385 | /** One workspace's recent pace. */ |
| 386 | export type Velocity = { |
| 387 | workspace: string; |
| 388 | plan: PlanKind; |
| 389 | lastHourMicros: number; |
| 390 | averageHourMicros: number; |
| 391 | lastDayMicros: number; |
| 392 | thisMonthMicros: number; |
| 393 | ratio: number; |
| 394 | spike: Spike | null; |
| 395 | firstSeen: string | null; |
| 396 | }; |
| 397 | |
| 398 | /** g1t's capped budgets for free usage this month. */ |
| 399 | export type Pools = { |
| 400 | month: string; |
| 401 | trialGrantedMicros: number; |
| 402 | trialPoolMicros: number; |
| 403 | trialGrants: number; |
| 404 | ossUsedMicros: number; |
| 405 | ossPoolMicros: number; |
| 406 | ossRepoMicros: number; |
| 407 | }; |
| 408 | |
| 409 | export type AccountSummary = { |
| 410 | account: PayingAccount; |
| 411 | limit: Limit; |
| 412 | chargedMicros: number; |
| 413 | costMicros: number; |
| 414 | paidMicros: number; |
| 415 | /** The same figures for each of the account's workspaces that has any. */ |
| 416 | byWorkspace: WorkspaceFigures[]; |
| 417 | /** The last six months, oldest first. */ |
| 418 | months?: MonthFigures[]; |
| 419 | }; |
| 420 | |
| 421 | /** One workspace's share of an `AccountSummary`. */ |
| 422 | export type WorkspaceFigures = { workspace: string; chargedMicros: number; costMicros: number; paidMicros: number }; |
| 423 | |
| 424 | export type StripeStatus = { |
| 425 | /** `test` or `live`, from the key; `off` without one. */ |
| 426 | mode: "test" | "live" | "off" | string; |
| 427 | /** Whether `STRIPE_WEBHOOK_SECRET` is set, so events can be checked. */ |
| 428 | secretSet: boolean; |
| 429 | /** The destination at billing's address in Stripe, as Stripe has it. */ |
| 430 | webhook: { url: string; endpointId: string; status: "enabled" | "disabled" | string; events: string[]; createdAt: string } | null; |
| 431 | /** Events billing handles that the destination does not send. */ |
| 432 | missingEvents: string[]; |
| 433 | recentEvents: { id: string; kind: string; outcome: string; receivedAt: string }[]; |
| 434 | error: string | null; |
| 435 | }; |
| 436 | |
| 437 | /** An enterprise's invoice: one line per workspace, paid on Stripe's page. */ |
| 438 | export type EnterpriseInvoice = { |
| 439 | invoiceId: string; |
| 440 | hostedUrl: string | null; |
| 441 | amountMicros: number; |
| 442 | status: "open" | "paid" | "overdue" | "void" | string; |
| 443 | period: string; |
| 444 | lines: { workspace: string; amountMicros: number }[]; |
| 445 | createdAt: string; |
| 446 | }; |
| 447 | |
| 448 | /** A workspace's invoice: monthly, or when charged near its limit. Itemised, in Stripe's billing page. */ |
| 449 | export type WorkspaceInvoice = { |
| 450 | invoiceId: string; |
| 451 | workspace: string; |
| 452 | reason: "month" | "threshold" | string; |
| 453 | period: string; |
| 454 | amountMicros: number; |
| 455 | status: "paid" | "open" | "failed" | "void" | string; |
| 456 | hostedUrl: string | null; |
| 457 | pdfUrl: string | null; |
| 458 | lines: { description: string; amountMicros: number }[]; |
| 459 | createdAt: string; |
| 460 | /** The card processing fee on top of `amountMicros` when charged to a card, and the tax Stripe added once known. */ |
| 461 | feeMicros?: number; |
| 462 | taxMicros?: number; |
| 463 | }; |
| 464 | |
| 465 | /** A month of the ledger, grouped by day or project, a line per kind of charge. */ |
| 466 | export type Statement = { |
| 467 | month: string; |
| 468 | months: string[]; |
| 469 | groups: { |
| 470 | key: string; |
| 471 | label: string; |
| 472 | /** `coveredMicros`: what the plan's included usage, the trial, the open-source pool or g1t paid, not in `chargedMicros`. */ |
| 473 | /** `priceMicros`: usage at its price; `discountMicros`: what the account's discount took off it. */ |
| 474 | lines: { |
| 475 | kind: string; |
| 476 | count: number; |
| 477 | chargedMicros: number; |
| 478 | costMicros: number; |
| 479 | coveredMicros?: number; |
| 480 | priceMicros?: number; |
| 481 | discountMicros?: number; |
| 482 | /** On `Tax` and `Card processing fees` lines: what was paid with payments on top of what reached the balance. Never charged. */ |
| 483 | passedMicros?: number; |
| 484 | }[]; |
| 485 | chargedMicros: number; |
| 486 | priceMicros?: number; |
| 487 | discountMicros?: number; |
| 488 | }[]; |
| 489 | totals: { |
| 490 | chargedMicros: number; |
| 491 | paidMicros: number; |
| 492 | costMicros: number; |
| 493 | entries: number; |
| 494 | /** Usage at price, and what the discount took off: charged is the price less the discount and what paid for it. */ |
| 495 | priceMicros?: number; |
| 496 | discountMicros?: number; |
| 497 | /** The account's discount now, in percent; absent without one. */ |
| 498 | discountPercent?: number | null; |
| 499 | /** What paid for usage before it was charged, such as "Paid by g1t's open-source pool". */ |
| 500 | covered?: { source: "included" | "trial" | "oss_pool" | "given" | string; label: string; micros: number }[]; |
| 501 | /** Owed when the month closed but under the minimum charge: on the next invoice. */ |
| 502 | carriedMicros?: number; |
| 503 | /** Tax and card processing fees paid with the month's payments, on top of `paidMicros`. */ |
| 504 | taxMicros?: number; |
| 505 | cardFeeMicros?: number; |
| 506 | }; |
| 507 | }; |
| 508 | |
| 509 | export type MonthFigures = { |
| 510 | month: string; |
| 511 | /** Usage charged, after what paid for it first. */ |
| 512 | chargedMicros: number; |
| 513 | /** What usage cost g1t: never a workspace's own model provider. */ |
| 514 | costMicros: number; |
| 515 | paidMicros: number; |
| 516 | /** The plan's monthly price, paid. */ |
| 517 | plansMicros?: number; |
| 518 | /** What g1t gave at price (internal use, trials, the open-source pool, goodwill, covered). Not margin. */ |
| 519 | givenMicros?: number; |
| 520 | }; |
| 521 | |
| 522 | /** What g1t gave this month from one source. */ |
| 523 | export type GivenFigures = { source: "internal" | "trial" | "oss_pool" | "goodwill" | "covered" | string; label: string; micros: number; costMicros: number }; |
| 524 | |
| 525 | /** One internal workspace's use this month, and why it is not charged. */ |
| 526 | export type InternalUse = { workspace: string; reason: string; costMicros: number; entries: number }; |
| 527 | |
| 528 | export type SignalKind = "at_limit" | "near_ceiling" | "declined" | "growing" | "established" | "first_payment" | "high_spend" | "cost_over_revenue"; |
| 529 | |
| 530 | /** Why a workspace is worth reaching out to. */ |
| 531 | export type Signal = { |
| 532 | workspace: string; |
| 533 | kind: SignalKind; |
| 534 | detail: string; |
| 535 | valueMicros: number; |
| 536 | stage: string | null; |
| 537 | owner: string | null; |
| 538 | nextStep?: string | null; |
| 539 | /** When the next step is due, YYYY-MM-DD. */ |
| 540 | nextAt?: string | null; |
| 541 | }; |
| 542 | |
| 543 | /** One invoice g1t has sent, a workspace's or an enterprise's. */ |
| 544 | export type InvoiceSummary = { |
| 545 | invoiceId: string; |
| 546 | kind: "workspace" | "enterprise"; |
| 547 | account: string; |
| 548 | name: string; |
| 549 | reason: string; |
| 550 | period: string; |
| 551 | amountMicros: number; |
| 552 | status: string; |
| 553 | hostedUrl: string | null; |
| 554 | createdAt: string; |
| 555 | paidAt: string | null; |
| 556 | }; |
| 557 | |
| 558 | export type SalesStage = "none" | "lead" | "contacted" | "negotiating" | "won" | "lost" | "churn_risk"; |
| 559 | |
| 560 | export type SalesRecord = { |
| 561 | workspace: string; |
| 562 | stage: SalesStage | string; |
| 563 | owner: string | null; |
| 564 | nextStep: string | null; |
| 565 | nextAt: string | null; |
| 566 | notes: { id: string; text: string; by: string; createdAt: string }[]; |
| 567 | updatedAt: string | null; |
| 568 | }; |
| 569 | |
| 570 | export type Overview = { |
| 571 | month: string; |
| 572 | months: MonthFigures[]; |
| 573 | byKind: { kind: string; chargedMicros: number; costMicros: number }[]; |
| 574 | payingWorkspaces: number; |
| 575 | stopped: number; |
| 576 | nearCeiling: number; |
| 577 | declined: number; |
| 578 | openInvoicesMicros: number; |
| 579 | followUpsDue: number; |
| 580 | /** g1t's capped budgets for free usage, this month. */ |
| 581 | pools?: Pools | null; |
| 582 | /** Usage charged plus the plan's price paid, this month. */ |
| 583 | revenueMicros?: number; |
| 584 | activePlans?: number; |
| 585 | planMrrMicros?: number; |
| 586 | /** What g1t gave this month, by source, apart from its margin. */ |
| 587 | given?: GivenFigures[]; |
| 588 | /** g1t's own and Flagon's workspaces: what their use cost, and why they are not charged. */ |
| 589 | internal?: InternalUse[]; |
| 590 | openRequests?: number; |
| 591 | overages?: number; |
| 592 | openSpikes?: number; |
| 593 | }; |
| 594 | |
| 595 | /** A customer's Stripe billing page, for staff to send them. */ |
| 596 | export type BillingLink = { |
| 597 | /** One-time and short-lived, signed in already. */ |
| 598 | portalUrl: string; |
| 599 | /** The page's sign-in, which does not expire: the customer signs in by email. */ |
| 600 | loginUrl: string | null; |
| 601 | customerEmail: string | null; |
| 602 | expiresNote: string; |
| 603 | }; |
| 604 | |
| 605 | export type AdminAction = { id: string; account: string; action: string; detail: string; by: string; createdAt: string }; |
| 606 | |
| 607 | export type AccountDetail = { |
| 608 | summary: AccountSummary; |
| 609 | workspaces: Limit[]; |
| 610 | ledger: LedgerEntry[]; |
| 611 | audit: AdminAction[]; |
| 612 | }; |
| 613 | |
| 614 | /** Staff-only billing, for sudo.g1t.sh. Every change names who made it. */ |
| 615 | export interface BillingAdminApi { |
| 616 | accounts(query?: string): Promise<AccountSummary[]>; |
| 617 | account(id: string): Promise<Result<AccountDetail>>; |
| 618 | setTerms(id: string, terms: Terms, by: string): Promise<Result<PayingAccount>>; |
| 619 | /** Team on or off without charge, and the account's share of the pools. Needs a note. */ |
| 620 | setAllowances(id: string, allowances: Allowances, note: string, by: string): Promise<Result<PayingAccount>>; |
| 621 | createEnterprise(name: string, workspaces: string[], by: string): Promise<Result<PayingAccount>>; |
| 622 | attach(workspace: string, account: string | null, by: string): Promise<Result<PayingAccount>>; |
| 623 | /** Credit for a workspace: promotional, goodwill or a refund; the owners are emailed. */ |
| 624 | credit(workspace: string, amountMicros: number, note: string, by: string, options?: CreditOptions): Promise<Result<LedgerEntry>>; |
| 625 | /** Every credit g1t gave, filtered, with each month's totals by kind. */ |
| 626 | credits(filter?: { workspace?: string | null; kind?: CreditKind | null; month?: string | null; by?: string | null }): Promise<AdminCredits>; |
| 627 | /** What is left of a credit, taken back, with why. */ |
| 628 | revokeCredit(id: string, note: string, by: string): Promise<Result<CreditGrant>>; |
| 629 | /** A test workspace's billing wiped, to start again as a new customer. Only on Stripe's test key; never comped or enterprise. Logged. */ |
| 630 | resetBilling(workspace: string, confirm: string, note: string, by: string): Promise<Result<BillingReset>>; |
| 631 | /** The workspace's Stripe billing page, to send to the customer. Logged. */ |
| 632 | billingLink(workspace: string, by: string): Promise<Result<BillingLink>>; |
| 633 | /** Where billing stands with Stripe; with `setup`, registers the webhook first. */ |
| 634 | /** Stripe's state for billing; `fix` enables the destination and adds missing events first. */ |
| 635 | stripe(fix?: boolean, by?: string): Promise<StripeStatus>; |
| 636 | /** Where an enterprise's invoices go; makes its Stripe customer. */ |
| 637 | enterpriseBilling(id: string, email: string, by: string): Promise<Result<PayingAccount>>; |
| 638 | /** The enterprise's billing address and tax ID, on its Stripe customer: Stripe Tax works its invoices out from them. */ |
| 639 | enterpriseAddress(id: string, address: PostalAddress, taxIdType: string | null, taxId: string | null, by: string): Promise<Result<boolean>>; |
| 640 | /** Sends an enterprise its invoice now, for what its workspaces owe. */ |
| 641 | invoiceEnterprise(id: string, by: string): Promise<Result<EnterpriseInvoice>>; |
| 642 | /** Exactly these workspaces' accounts, such as one page of the list. */ |
| 643 | accountsFor(workspaces: string[]): Promise<AccountSummary[]>; |
| 644 | /** Every workspace worth reaching out to, most urgent first. */ |
| 645 | signals(): Promise<Signal[]>; |
| 646 | /** The business at a glance. */ |
| 647 | overview(): Promise<Overview>; |
| 648 | /** A workspace's sales record. */ |
| 649 | sales(workspace: string): Promise<SalesRecord>; |
| 650 | setSales(workspace: string, record: { stage: string; owner?: string | null; nextStep?: string | null; nextAt?: string | null }, by: string): Promise<Result<SalesRecord>>; |
| 651 | addNote(workspace: string, text: string, by: string): Promise<Result<SalesRecord>>; |
| 652 | /** A workspace's invoices from g1t, for staff. */ |
| 653 | workspaceInvoices(workspace: string): Promise<WorkspaceInvoice[]>; |
| 654 | /** Every invoice g1t has sent, newest first. */ |
| 655 | allInvoices(filter?: { status?: string; month?: string }): Promise<InvoiceSummary[]>; |
| 656 | /** Every change made in sudo and by Stripe, newest first, 100 at a time. */ |
| 657 | audit(filter?: { by?: string; action?: string; before?: string }): Promise<AdminAction[]>; |
| 658 | /** Limit and overage requests, with each workspace's history; `open` by default. */ |
| 659 | limitRequests(status?: "open" | "approved" | "declined" | "all"): Promise<LimitRequestReview[]>; |
| 660 | /** Approve (at the amount asked, or another) or decline; the owner is told in the app and by email. */ |
| 661 | decideLimitRequest(id: string, decision: "approve" | "decline", amountMicros: number | null, note: string, by: string): Promise<Result<LimitRequest>>; |
| 662 | /** The Overages queue. */ |
| 663 | overages(): Promise<Overage[]>; |
| 664 | /** A goodwill credit; no amount is the one-click credit. Larger, or a second in 12 months, needs a reason. */ |
| 665 | goodwill(workspace: string, amountMicros: number | null, reason: string, by: string, day?: string | null): Promise<Result<LedgerEntry>>; |
| 666 | /** Workspaces spending in the last day, fastest first. */ |
| 667 | velocity(): Promise<Velocity[]>; |
| 668 | /** Money that reached g1t outside the card pages, such as a bank transfer: entered as a payment. */ |
| 669 | recordPayment(workspace: string, amountMicros: number, reference: string, note: string, by: string): Promise<Result<LedgerEntry>>; |
| 670 | /** Costs & margin: Cloudflare's bill against what g1t charged, over the last `days` (7 to 90, 30 by default). */ |
| 671 | costs(days?: number): Promise<CostsReport>; |
| 672 | /** The open margin alerts, for the banner on every page. */ |
| 673 | costAlerts(): Promise<MarginAlert[]>; |
| 674 | /** g1t's own spend against its caps: the daily breaker and comped budgets. */ |
| 675 | spendCaps(): Promise<SpendCaps>; |
| 676 | /** Lets hosted-model runs start again for the rest of today (UTC); needs a note. */ |
| 677 | liftBreaker(note: string, by: string): Promise<Result<SpendCaps>>; |
| 678 | /** Approve or reject a price proposal; a rejection needs a note. An approved rise waits out the notice period. */ |
| 679 | decideProposal(id: string, decision: "approve" | "reject", note: string, by: string): Promise<Result<PriceProposal>>; |
| 680 | setCostSettings(settings: CostSettings, by: string): Promise<Result<CostSettings>>; |
| 681 | setCostMapping(mapping: CostMappingInput, by: string): Promise<Result<CostMapping>>; |
| 682 | /** Reads Cloudflare's bill and reconciles now, as the daily run does. */ |
| 683 | runCosts(by: string): Promise<Result<CostsRun>>; |
| 684 | } |
| 685 | |
| 686 | /** How much a workspace has earned g1t's trust with money. */ |
| 687 | export type Trust = "new" | "paid" | "established" | "reviewed" | "internal"; |
| 688 | |
| 689 | /** |
| 690 | * How far a workspace's unpaid usage has gone this month, and where its |
| 691 | * work stops: past `ceilingMicros`, no new sandboxes, builds or app |
| 692 | * requests. Usage counts at its cost to g1t or its charge, whichever is |
| 693 | * more, so it counts while g1t is free too. |
| 694 | */ |
| 695 | export type Limit = { |
| 696 | workspace: string; |
| 697 | /** The account that pays: the workspace's own (`ws_<slug>`), or its enterprise's. */ |
| 698 | account: string; |
| 699 | accountName: string; |
| 700 | trust: Trust; |
| 701 | exposureMicros: number; |
| 702 | /** The lower of g1t's ceiling and the owner's spend limit; null for g1t's own. */ |
| 703 | ceilingMicros: number | null; |
| 704 | trustCeilingMicros: number | null; |
| 705 | spendLimitMicros: number | null; |
| 706 | state: "ok" | "warning" | "stopped"; |
| 707 | message: string | null; |
| 708 | /** Charged this month: what the spend limit is measured against. */ |
| 709 | spentMicros?: number; |
| 710 | /** True while the owners have not chosen a limit, so the automatic one applies: $200, or twice last month's spend. */ |
| 711 | defaultSpendLimit?: boolean; |
| 712 | /** The most owners may set their own limit to without asking: the highest ceiling ever, plus what is prepaid. */ |
| 713 | availableMicros?: number | null; |
| 714 | /** How the ceiling grows from here, in a sentence. */ |
| 715 | growth?: string | null; |
| 716 | /** Paid in advance and not used yet; raises what can be used before work stops by as much. */ |
| 717 | prepaidMicros?: number; |
| 718 | /** The highest ceiling the workspace has had. */ |
| 719 | maxCeilingMicros?: number | null; |
| 720 | /** The most owners may raise the limit to themselves, once: twice the highest ceiling. Null once used. */ |
| 721 | raiseOnceMicros?: number | null; |
| 722 | /** When the one-time raise was used. */ |
| 723 | raisedAt?: string | null; |
| 724 | /** A paid workspace's first billing cycle, on the starting ceiling. */ |
| 725 | firstMonth?: boolean; |
| 726 | /** The budget's alerts, in percent of the spend limit: some of 50, 75, 90 and 100. */ |
| 727 | alertLevels?: number[]; |
| 728 | /** Whether usage pauses at the spend limit (the default); off, it only alerts. */ |
| 729 | pauseAtLimit?: boolean; |
| 730 | /** An HTTPS address told of each budget alert. */ |
| 731 | budgetWebhook?: string | null; |
| 732 | }; |
| 733 | |
| 734 | /** One metered unit: what it costs g1t and what it is sold at; the price follows the cost. */ |
| 735 | export type Price = { |
| 736 | meter: |
| 737 | | "sandbox_second" |
| 738 | | "build_second" |
| 739 | | "app_requests" |
| 740 | | "app_cpu" |
| 741 | | "app_month" |
| 742 | | "custom_domain_month" |
| 743 | | "private_storage" |
| 744 | | "embedding_tokens" |
| 745 | | "scan_cpu" |
| 746 | | "scan_rows" |
| 747 | | string; |
| 748 | title: string; |
| 749 | unit: string; |
| 750 | costMicros: number; |
| 751 | markupPercent: number; |
| 752 | priceMicros: number; |
| 753 | /** `list`: Cloudflare's published price. `cloudflare`: measured from Cloudflare's bill. */ |
| 754 | source: "list" | "cloudflare" | string; |
| 755 | checkedAt: string | null; |
| 756 | updatedAt: string; |
| 757 | }; |
| 758 | |
| 759 | export type PriceChange = { |
| 760 | meter: string; |
| 761 | oldCostMicros: number; |
| 762 | newCostMicros: number; |
| 763 | markupPercent: number; |
| 764 | /** The markup before, when the change was to the markup rather than the cost. */ |
| 765 | oldMarkupPercent?: number; |
| 766 | reason: string; |
| 767 | createdAt: string; |
| 768 | /** When a change still to come takes effect: a rise is announced before it is charged. */ |
| 769 | effectiveAt?: string; |
| 770 | }; |
| 771 | |
| 772 | export type PriceBook = { |
| 773 | prices: Price[]; |
| 774 | changes: PriceChange[]; |
| 775 | modelMarginPercent: number; |
| 776 | /** Every plan, as sold now. */ |
| 777 | plans?: FeaturePlan[]; |
| 778 | /** What is free, and the capped budgets that pay for it. */ |
| 779 | free?: FreeTier | null; |
| 780 | }; |
| 781 | |
| 782 | /** One kind of meter's usage this month, from `usage_meters`. */ |
| 783 | export type MeterUsage = { |
| 784 | /** `agents`, `builds`, `requests`, `domains`, `git_storage` or `search_scans`. */ |
| 785 | key: string; |
| 786 | label: string; |
| 787 | /** At price (cost plus 20%, on the account's terms), before what paid for it. */ |
| 788 | micros: number; |
| 789 | /** How much, when it is known: `12 runs`, `41 build minutes`. */ |
| 790 | quantity?: string | null; |
| 791 | }; |
| 792 | |
| 793 | /** What g1t gives without a plan; each is paid for by a capped budget. */ |
| 794 | export type FreeTier = { |
| 795 | /** Each new workspace's trial credit, once. */ |
| 796 | trialWorkspaceMicros: number; |
| 797 | /** Trial grants each month, in all; new trials wait when it is spent. */ |
| 798 | trialMonthlyPoolMicros: number; |
| 799 | /** g1t's open-source pool each month, and any one repository's share. */ |
| 800 | ossPoolMicros: number; |
| 801 | ossRepoMicros: number; |
| 802 | /** Private repository storage free for every workspace. Past it, the plan pays; a free workspace's pushes stop. */ |
| 803 | freePrivateStorageBytes: number; |
| 804 | /** Days of audit log a free workspace keeps. */ |
| 805 | auditRetentionDays: number; |
| 806 | /** Days of audit log the g1t plan keeps, and g1t's own and enterprise workspaces; longer by arrangement. */ |
| 807 | planAuditRetentionDays?: number; |
| 808 | /** The smallest amount a card is charged when a month closes; less carries over. */ |
| 809 | minChargeMicros: number; |
| 810 | /** Git operations free for every workspace each month. Past it, the plan pays; a free workspace is slowed down. */ |
| 811 | gitOperationsIncluded?: number; |
| 812 | /** A new paid workspace's ceiling in its first month. */ |
| 813 | paidStartCeilingMicros?: number; |
| 814 | /** The most a one-click goodwill credit can cost g1t. */ |
| 815 | overageForgiveCostMicros?: number; |
| 816 | }; |
| 817 | |
| 818 | /** |
| 819 | * A workspace's trial credit: one grant per workspace, made the first time |
| 820 | * it uses something, out of a pool that resets each calendar month. Mirrors |
| 821 | * `Trial` in `crates/contracts/src/billing.rs`. |
| 822 | */ |
| 823 | export type Trial = { |
| 824 | open: boolean; |
| 825 | usedMicros: number; |
| 826 | limitMicros: number; |
| 827 | /** No longer used: trials do not end on a date. */ |
| 828 | endsAt: string | null; |
| 829 | /** Why it is closed: `off`, `used` (this workspace's grant is spent) or `pool` (this month's are given out). */ |
| 830 | reason: "off" | "ended" | "used" | "pool" | null; |
| 831 | /** Whether the workspace has its grant already. */ |
| 832 | granted?: boolean; |
| 833 | /** With `pool`: when new trials start again, the first of next month. */ |
| 834 | waitsUntil?: string | null; |
| 835 | }; |
| 836 | |
| 837 | /** |
| 838 | * What a workspace pays a monthly price for: the g1t plan (`plan`), and the |
| 839 | * Security and quality activation (`security`), sold on its own. Deployments |
| 840 | * are part of the plan; `has_feature` for `deployments` answers whether the |
| 841 | * workspace has the plan. Mirrors `Feature` in `crates/contracts/src/billing.rs`. |
| 842 | */ |
| 843 | export type Feature = "plan" | "deployments" | "security"; |
| 844 | |
| 845 | /** |
| 846 | * What deployments cost g1t, in millionths of a dollar: fallbacks for when |
| 847 | * billing's price book cannot be read. Not an allowance: on the plan every |
| 848 | * unit is metered from the first, at cost plus 20%, and drawn from the |
| 849 | * plan's included usage first. Projects, previews and the apps behind them |
| 850 | * are not metered at all. Mirrors `deployment_costs`. |
| 851 | */ |
| 852 | export const DEPLOYMENT_COSTS = { |
| 853 | microsPerMillionRequests: 300_000, |
| 854 | microsPerMillionCpuMs: 20_000, |
| 855 | /** One second of a build's sandbox: a fallback; billing charges the price book's `build_second`. */ |
| 856 | microsPerBuildSecond: 15, |
| 857 | /** One custom domain for a month. */ |
| 858 | microsPerDomainMonth: 100_000, |
| 859 | } as const; |
| 860 | |
| 861 | export type FeaturePlan = { |
| 862 | feature: Feature; |
| 863 | title: string; |
| 864 | /** Charged every month while the plan is on, in cents, excluding tax. */ |
| 865 | monthlyCents: number; |
| 866 | /** The card processing fee on top each month, in cents (0 when off), excluding tax. */ |
| 867 | cardFeeCents?: number; |
| 868 | /** What the price includes, one line each. */ |
| 869 | includes: string[]; |
| 870 | /** How usage past the allowance is charged. */ |
| 871 | overage: string; |
| 872 | }; |
| 873 | |
| 874 | export type SubscriptionStatus = "active" | "canceling" | "past_due" | "canceled"; |
| 875 | |
| 876 | export type Subscription = { |
| 877 | feature: Feature; |
| 878 | status: SubscriptionStatus; |
| 879 | /** RFC 3339: when the period paid for ends. */ |
| 880 | periodEnd: string | null; |
| 881 | startedBy: string; |
| 882 | startedAt: string; |
| 883 | }; |
| 884 | |
| 885 | /** A feature as a workspace sees it. */ |
| 886 | export type FeatureState = { |
| 887 | plan: FeaturePlan; |
| 888 | subscription: Subscription | null; |
| 889 | /** Whether the feature works for the workspace now. */ |
| 890 | on: boolean; |
| 891 | /** On without a plan: comped terms, or given by g1t. Nothing to pay or turn off. */ |
| 892 | included?: boolean; |
| 893 | }; |
| 894 | |
| 895 | export interface BillingApi { |
| 896 | status(): Promise<BillingStatus>; |
| 897 | /** Members of the workspace only. */ |
| 898 | account(workspace: string, viewer: Viewer): Promise<Result<BillingAccount>>; |
| 899 | /** Newest first. Members of the workspace only. */ |
| 900 | ledger(workspace: string, viewer: Viewer): Promise<Result<LedgerEntry[]>>; |
| 901 | /** Credits from g1t, newest first, with what is left of each. Members only. */ |
| 902 | credits(workspace: string, viewer: Viewer): Promise<Result<Credits>>; |
| 903 | /** A month of the ledger, grouped by `day` (default) or `project`. Members only. */ |
| 904 | statement(workspace: string, viewer: Viewer, month?: string | null, group?: "day" | "project"): Promise<Result<Statement>>; |
| 905 | /** One statement line's entries, 50 at a time; `before` is the last id seen. */ |
| 906 | statementEntries( |
| 907 | workspace: string, |
| 908 | viewer: Viewer, |
| 909 | filter: { month: string; kind: string; day?: string | null; project?: string | null; before?: string | null }, |
| 910 | ): Promise<Result<LedgerEntry[]>>; |
| 911 | /** What the workspace's agents cost since `since`, broken down. Members only. */ |
| 912 | usage(workspace: string, viewer: Viewer, since: string): Promise<Result<Usage>>; |
| 913 | /** |
| 914 | * The model tokens the workspace's runs used, day by day over the last |
| 915 | * `days` (42, at most 366), for everyone or for one `person`. Members |
| 916 | * only; a member may ask only for themselves, an owner for anyone. |
| 917 | */ |
| 918 | tokenUsage(workspace: string, viewer: User, options?: { person?: string; days?: number }): Promise<Result<TokenUsage>>; |
| 919 | /** |
| 920 | * What one model answer used, added to its run's count for the day. The |
| 921 | * model proxy sends it; for usage views only, as runs are priced from AI |
| 922 | * Gateway. False when there was nothing to count. |
| 923 | */ |
| 924 | /** What the AI Gateway offers on g1t's key, with prices per million tokens. */ |
| 925 | gatewayModels(): Promise<GatewayModel[]>; |
| 926 | /** |
| 927 | * Whether a workspace's next AI Gateway request may go to g1t's models: |
| 928 | * fails with `payment_required` and what to do when it is over its spend |
| 929 | * limit, out of AI credit, or not on the plan. |
| 930 | */ |
| 931 | gatewayAdmit(workspace: string): Promise<Result<boolean>>; |
| 932 | /** Logs one AI Gateway request, and charges it when it used tokens on g1t's models. */ |
| 933 | recordGateway(record: GatewayRecord): Promise<Result<boolean>>; |
| 934 | /** A workspace's recent AI Gateway requests, newest first. Members only. */ |
| 935 | gatewayRequests(workspace: string, viewer: Viewer, options?: { limit?: number; before?: string | null }): Promise<Result<GatewayRequests>>; |
| 936 | recordTokens(usage: { |
| 937 | workspace: string; |
| 938 | /** The model session's id, one per run. */ |
| 939 | session: string; |
| 940 | /** The person the run is for, by username. */ |
| 941 | person?: string | null; |
| 942 | model: string; |
| 943 | tier?: "small" | "large" | "frontier" | null; |
| 944 | input: number; |
| 945 | output: number; |
| 946 | cacheRead: number; |
| 947 | cacheWrite: number; |
| 948 | }): Promise<Result<boolean>>; |
| 949 | /** |
| 950 | * Prepays usage ($25 at least) and returns the page to send the person to: |
| 951 | * by card with 3-D Secure, or by bank transfer from $1,000. Owners only. |
| 952 | * The payment's id comes back to `returnUrl` as `session`. |
| 953 | */ |
| 954 | checkout( |
| 955 | actor: User, |
| 956 | workspace: string, |
| 957 | amountCents: number, |
| 958 | returnUrl: string, |
| 959 | method?: "card" | "bank_transfer", |
| 960 | ): Promise<Result<{ url: string }>>; |
| 961 | /** |
| 962 | * Stripe's hosted billing page for the workspace: card, invoices, billing |
| 963 | * email and address. g1t never handles card numbers. Owners only. |
| 964 | */ |
| 965 | billingPortal(actor: User, workspace: string, returnUrl: string): Promise<Result<{ url: string }>>; |
| 966 | /** Credits a payment once the processor says it was made. Safe to repeat. */ |
| 967 | confirm(workspace: string, viewer: Viewer, session: string): Promise<Result<BillingAccount>>; |
| 968 | /** |
| 969 | * Whether a workspace may start an agent now, asked before anything is |
| 970 | * opened for it. A failure, with the reason to show, when it has no credit. |
| 971 | */ |
| 972 | canStart(workspace: string): Promise<Result<boolean>>; |
| 973 | /** A workspace's free allowance on g1t's hosted models; `exempt` are open to them anyway. */ |
| 974 | trial(workspace: string, exempt: string[]): Promise<Trial>; |
| 975 | /** |
| 976 | * Asks whether a workspace may start an agent and opens the run it will be |
| 977 | * charged for. Null when billing is off; a failure when there is no credit. |
| 978 | */ |
| 979 | /** Every paid feature and the workspace's plan for each. Members only. */ |
| 980 | features(workspace: string, viewer: Viewer): Promise<Result<FeatureState[]>>; |
| 981 | /** |
| 982 | * Starts the card page for a feature's monthly plan. Owners only. The |
| 983 | * page's id comes back to `returnUrl` as `session`. |
| 984 | */ |
| 985 | subscribe(actor: User, workspace: string, feature: Feature, returnUrl: string): Promise<Result<{ url: string }>>; |
| 986 | /** Turns the feature on once the plan is paid for. Safe to repeat. */ |
| 987 | confirmSubscription(workspace: string, viewer: Viewer, session: string): Promise<Result<FeatureState>>; |
| 988 | /** Ends a plan at the end of its period, or (`resume`) takes that back. Owners only. */ |
| 989 | cancelSubscription(actor: User, workspace: string, feature: Feature, resume?: boolean): Promise<Result<FeatureState>>; |
| 990 | /** Which of the workspaces are free (on no paid plan); none where payments are not set up. */ |
| 991 | freeWorkspaces(workspaces: string[]): Promise<string[]>; |
| 992 | /** Whether a feature works for a workspace now; a failure with the reason when not. */ |
| 993 | hasFeature(workspace: string, feature: Feature): Promise<Result<boolean>>; |
| 994 | /** |
| 995 | * Usage past a plan's allowance, charged from credit at cost plus the |
| 996 | * margin, once per `reference`. False if it was charged before. |
| 997 | */ |
| 998 | chargeFeature(charge: { |
| 999 | workspace: string; |
| 1000 | feature: Feature; |
| 1001 | costMicros: number; |
| 1002 | description: string; |
| 1003 | repo?: string | null; |
| 1004 | reference: string; |
| 1005 | /** For a build: how long it ran, so the plan's included build time pays for what it can. */ |
| 1006 | buildSeconds?: number | null; |
| 1007 | }): Promise<Result<boolean>>; |
| 1008 | /** |
| 1009 | * What a source cost g1t so far this month, so the workspace's limit |
| 1010 | * counts it now. Replaces the last report. Billing charges `context` |
| 1011 | * and `security` itself once the month is over; `deployments` charges |
| 1012 | * its own. |
| 1013 | */ |
| 1014 | notePending( |
| 1015 | workspace: string, |
| 1016 | source: "deployments" | "domains" | "context" | "security", |
| 1017 | costMicros: number, |
| 1018 | /** How much of it, for the Billing page: `1.2 million requests and 3.4 million CPU ms`. */ |
| 1019 | detail?: string | null, |
| 1020 | ): Promise<boolean>; |
| 1021 | /** |
| 1022 | * This month's usage, one line per kind of meter, at what it is charged |
| 1023 | * before the plan's included usage or a pool paid for it. Members only. |
| 1024 | */ |
| 1025 | usageMeters(workspace: string, viewer: Viewer): Promise<Result<MeterUsage[]>>; |
| 1026 | /** What the workspace may do now: its plan, caps, pause, trial, and what the plan gives it. */ |
| 1027 | entitlements(workspace: string): Promise<Entitlements>; |
| 1028 | /** |
| 1029 | * Holds a start's estimated cost before the work starts. A failure's code says why not: |
| 1030 | * `paused`, `limit`, `not_paid`, `trial_used` or `oss_pool_empty`, with a message for the owner. |
| 1031 | */ |
| 1032 | reserve(reservation: { |
| 1033 | workspace: string; |
| 1034 | repo: RepoPath; |
| 1035 | public: boolean; |
| 1036 | kind: ComputeKind; |
| 1037 | /** The most the work is expected to cost g1t, before the margin. */ |
| 1038 | estimateMicros: number; |
| 1039 | }): Promise<Result<ReservationHeld>>; |
| 1040 | /** Releases a reservation's hold with what the work cost g1t, before the margin. Safe to repeat. */ |
| 1041 | settle(reservationId: string, actualMicros: number): Promise<Result<boolean>>; |
| 1042 | /** Stripe's page to save and verify a card (3-D Secure, never charged). Owners only. */ |
| 1043 | cardCheck(actor: User, workspace: string, returnUrl: string): Promise<Result<{ url: string }>>; |
| 1044 | /** Records the card check once Stripe says it passed, and grants the trial if it can. Safe to repeat. */ |
| 1045 | confirmCardCheck(workspace: string, viewer: Viewer, session: string): Promise<Result<Entitlements>>; |
| 1046 | /** An owner asks for a higher limit, or for help with usage past what was meant. */ |
| 1047 | requestLimit( |
| 1048 | actor: User, |
| 1049 | workspace: string, |
| 1050 | request: { kind: "limit" | "overage"; amountMicros: number; reason: string; expectedMonthlyMicros: number }, |
| 1051 | ): Promise<Result<LimitRequest>>; |
| 1052 | /** The workspace's requests and their answers, newest first. Members only. */ |
| 1053 | limitRequests(workspace: string, viewer: Viewer): Promise<Result<LimitRequest[]>>; |
| 1054 | /** |
| 1055 | * The owners' own caps on agents: one run's spend ($0.10 to $100) and one issue's ($1 to $1,000). |
| 1056 | * Null goes back to the default ($2 and $10). A cap staff set wins. Owners only. |
| 1057 | */ |
| 1058 | setCaps(actor: User, workspace: string, caps: { runCapMicros: number | null; issueCapMicros: number | null }): Promise<Result<Entitlements>>; |
| 1059 | /** An owner's answer to a spend spike: keep going for 24 hours, or stop. */ |
| 1060 | confirmSpike(actor: User, workspace: string, keepGoing: boolean): Promise<Result<Entitlements>>; |
| 1061 | /** Every metered price and the recent changes. Public. */ |
| 1062 | prices(): Promise<PriceBook>; |
| 1063 | /** A workspace's limit, for its members. */ |
| 1064 | limit(workspace: string, viewer: Viewer): Promise<Result<Limit>>; |
| 1065 | /** The same, for the services that enforce it. */ |
| 1066 | checkLimit(workspace: string): Promise<Result<Limit>>; |
| 1067 | /** |
| 1068 | * The owners' own monthly limit, up to what is available; null goes back |
| 1069 | * to the default, and `useFullLimit` uses everything available. Owners only. |
| 1070 | */ |
| 1071 | setSpendLimit( |
| 1072 | actor: User, |
| 1073 | workspace: string, |
| 1074 | spendLimitMicros: number | null, |
| 1075 | useFullLimit?: boolean, |
| 1076 | /** Use the one-time raise: up to twice the highest ceiling, once per workspace. */ |
| 1077 | raiseOnce?: boolean, |
| 1078 | ): Promise<Result<Limit>>; |
| 1079 | /** The workspace's invoices from g1t, newest first. Members only. */ |
| 1080 | invoices(workspace: string, viewer: Viewer): Promise<Result<WorkspaceInvoice[]>>; |
| 1081 | /** |
| 1082 | * How long a sandbox ran for a workspace, reported when it stops. Its |
| 1083 | * cost is recorded and every second is charged, from the first. False if |
| 1084 | * `reference` was recorded before. |
| 1085 | */ |
| 1086 | recordSandbox(usage: { |
| 1087 | workspace: string; |
| 1088 | seconds: number; |
| 1089 | description: string; |
| 1090 | repo?: string | null; |
| 1091 | reference: string; |
| 1092 | /** What ran; checks, workflows and the merge queue on public repositories may use the open-source pool. */ |
| 1093 | kind?: ComputeKind | null; |
| 1094 | /** vCPU-seconds used, when the sandbox can tell: the run is priced on its own CPU. */ |
| 1095 | cpuSeconds?: number | null; |
| 1096 | /** The reservation it started under, settled with this cost. */ |
| 1097 | reservationId?: string | null; |
| 1098 | /** It ran on one of the workspace's self-hosted runners: its minutes go on usage at $0. */ |
| 1099 | selfHosted?: boolean; |
| 1100 | /** The machine it ran on, by label (`g1t-4core`); absent, the standard one. */ |
| 1101 | instance?: string | null; |
| 1102 | }): Promise<Result<boolean>>; |
| 1103 | startRun(run: { |
| 1104 | workspace: string; |
| 1105 | repo: RepoPath; |
| 1106 | number: number; |
| 1107 | task: string; |
| 1108 | model: string; |
| 1109 | /** `workspace` when the run uses the workspace's own model provider. */ |
| 1110 | billedTo?: "g1t" | "workspace"; |
| 1111 | /** |
| 1112 | * The model session's id: the run is settled at AI Gateway's price by |
| 1113 | * it, and on the workspace's own provider its tokens are counted under |
| 1114 | * it for the agent rate. |
| 1115 | */ |
| 1116 | session?: string | null; |
| 1117 | /** The tier g1t routed the run to: `small`, `large` or `frontier`. */ |
| 1118 | tier?: "small" | "large" | "frontier" | null; |
| 1119 | }): Promise<Result<RunTicket | null>>; |
| 1120 | /** Usage over a range of days (`YYYY-MM-DD`, both included), at price, by product, meter, project and day. Members only. */ |
| 1121 | usageReport( |
| 1122 | workspace: string, |
| 1123 | viewer: Viewer, |
| 1124 | range: { from: string; until: string; products?: string[]; projects?: string[] }, |
| 1125 | ): Promise<Result<UsageReport>>; |
| 1126 | /** The workspace's prepaid AI credit, auto-reload and prices. Members only. */ |
| 1127 | aiCredit(workspace: string, viewer: Viewer): Promise<Result<AiCredit>>; |
| 1128 | /** Stripe's page to buy AI credit ( to ,000, the card fee on its own line). Owners only. The page's id comes back to `returnUrl` as `ai_credit`. */ |
| 1129 | buyAiCredit(actor: User, workspace: string, amountCents: number, returnUrl: string): Promise<Result<{ url: string }>>; |
| 1130 | /** Credits a purchase once Stripe says it was paid, once. Safe to repeat. */ |
| 1131 | confirmAiCredit(workspace: string, viewer: Viewer, session: string): Promise<Result<AiCredit>>; |
| 1132 | /** Auto-reload's settings. Owners only. */ |
| 1133 | setAiReload( |
| 1134 | actor: User, |
| 1135 | workspace: string, |
| 1136 | reload: { enabled: boolean; thresholdMicros: number; targetMicros: number; monthlyMaxMicros: number }, |
| 1137 | ): Promise<Result<AiCredit>>; |
| 1138 | /** The monthly budget: the spend limit, its alerts, whether usage pauses at 100%, and a webhook. Owners only. */ |
| 1139 | setBudget( |
| 1140 | actor: User, |
| 1141 | workspace: string, |
| 1142 | budget: { amountMicros: number | null; alerts: number[]; pauseAtLimit: boolean; webhook: string | null; keepLimit?: boolean }, |
| 1143 | ): Promise<Result<Limit>>; |
| 1144 | /** Invoice details from the Stripe customer, the default payment method, invoices and the next invoice. Members only. */ |
| 1145 | billingDetails(workspace: string, viewer: Viewer): Promise<Result<BillingDetails>>; |
| 1146 | /** Saves invoice details on the Stripe customer; absent fields stay, empty clears. Owners only. */ |
| 1147 | setBillingDetails(actor: User, workspace: string, details: BillingDetailsInput): Promise<Result<BillingDetails>>; |
| 1148 | } |
| 1149 | |
| 1150 | |
| 1151 | /** One slice of usage: what it was for, what it cost, how many runs. */ |
| 1152 | export type UsageSlice = { key: string; micros: number; runs: number }; |
| 1153 | |
| 1154 | /** The model tokens runs used over a window of days. */ |
| 1155 | export type TokenUsage = { |
| 1156 | /** `YYYY-MM-DD`, the first day counted. */ |
| 1157 | since: string; |
| 1158 | /** The window's length: 42 unless asked, 366 at most. */ |
| 1159 | days: number; |
| 1160 | /** Null for the whole workspace. */ |
| 1161 | person: string | null; |
| 1162 | totalTokens: number; |
| 1163 | inputTokens: number; |
| 1164 | outputTokens: number; |
| 1165 | cacheReadTokens: number; |
| 1166 | cacheWriteTokens: number; |
| 1167 | /** What those runs were charged, as `usage` measures it. */ |
| 1168 | costMicros: number; |
| 1169 | /** Days in the window with any tokens. */ |
| 1170 | activeDays: number; |
| 1171 | /** Every day in the window, oldest first, zeros included. */ |
| 1172 | byDay: { day: string; tokens: number }[]; |
| 1173 | }; |
| 1174 | |
| 1175 | // --- AI Gateway ------------------------------------------------------------- |
| 1176 | |
| 1177 | /** A model the AI Gateway offers on g1t's key, with its prices per million tokens. */ |
| 1178 | export type GatewayModel = { |
| 1179 | /** The id a request names, such as `claude-sonnet-5-5`. */ |
| 1180 | model: string; |
| 1181 | /** For people: `Claude Sonnet 5.5`. */ |
| 1182 | name: string; |
| 1183 | provider: string; |
| 1184 | inputMicros: number; |
| 1185 | outputMicros: number; |
| 1186 | cacheReadMicros: number; |
| 1187 | cacheWriteMicros: number; |
| 1188 | }; |
| 1189 | |
| 1190 | /** One AI Gateway request, as the model proxy reports it to billing. */ |
| 1191 | export type GatewayRecord = { |
| 1192 | /** `gw_…`, chosen by the proxy; recording it twice records it once. */ |
| 1193 | id: string; |
| 1194 | workspace: string; |
| 1195 | tokenId: string; |
| 1196 | tokenName?: string | null; |
| 1197 | model: string; |
| 1198 | input: number; |
| 1199 | output: number; |
| 1200 | cacheRead: number; |
| 1201 | cacheWrite: number; |
| 1202 | /** The HTTP status the caller was answered with. */ |
| 1203 | status: number; |
| 1204 | /** On the workspace's own provider key: counted, never charged. */ |
| 1205 | ownKey: boolean; |
| 1206 | streamed: boolean; |
| 1207 | durationMs: number; |
| 1208 | error?: string | null; |
| 1209 | }; |
| 1210 | |
| 1211 | /** One AI Gateway request, as its log keeps it. */ |
| 1212 | export type GatewayRequest = { |
| 1213 | id: string; |
| 1214 | createdAt: string; |
| 1215 | model: string; |
| 1216 | tokenId: string; |
| 1217 | tokenName: string | null; |
| 1218 | input: number; |
| 1219 | output: number; |
| 1220 | cacheRead: number; |
| 1221 | cacheWrite: number; |
| 1222 | /** What the tokens cost at the model's price. */ |
| 1223 | costMicros: number; |
| 1224 | /** What the workspace was charged, before included usage and credit paid for it; 0 on its own key. */ |
| 1225 | chargedMicros: number; |
| 1226 | status: number; |
| 1227 | ownKey: boolean; |
| 1228 | streamed: boolean; |
| 1229 | durationMs: number; |
| 1230 | error: string | null; |
| 1231 | }; |
| 1232 | |
| 1233 | /** A page of AI Gateway requests, newest first. */ |
| 1234 | export type GatewayRequests = { |
| 1235 | requests: GatewayRequest[]; |
| 1236 | /** The `before` for the next page, when there is one. */ |
| 1237 | next: string | null; |
| 1238 | /** How many days requests are kept. */ |
| 1239 | retentionDays: number; |
| 1240 | }; |
| 1241 | |
| 1242 | /** What a workspace's agents cost over a period. */ |
| 1243 | export type Usage = { |
| 1244 | since: string; |
| 1245 | /** Charged, including g1t's margin. */ |
| 1246 | spentMicros: number; |
| 1247 | /** What g1t's usage came to at price, less what was charged: the plan's included usage, the trial, a pool or a free period paid it. Usage at price is `spentMicros` plus this. */ |
| 1248 | coveredMicros?: number; |
| 1249 | /** What the account's discount took off the price; usage at price is spent + covered + this. */ |
| 1250 | discountMicros?: number; |
| 1251 | /** The account's discount now, in percent; with one, the slices are at price. */ |
| 1252 | discountPercent?: number | null; |
| 1253 | /** Usage at price: spent + covered + discount, from the same ledger lines. The one usage figure every page shows. */ |
| 1254 | priceMicros?: number; |
| 1255 | /** What g1t's model provider charged, before the margin. */ |
| 1256 | costMicros: number; |
| 1257 | /** What runs on the workspace's own provider cost there, estimated. Not charged by g1t. */ |
| 1258 | providerMicros: number; |
| 1259 | /** What the runs used, at cost: g1t's models and the workspace's own provider together. */ |
| 1260 | usedMicros: number; |
| 1261 | /** g1t charges nothing for now; the slices then measure usage at cost. */ |
| 1262 | free: boolean; |
| 1263 | runs: number; |
| 1264 | /** Spend per day and task, keyed `YYYY-MM-DD/task`. */ |
| 1265 | byDay: UsageSlice[]; |
| 1266 | byTask: UsageSlice[]; |
| 1267 | byRepo: UsageSlice[]; |
| 1268 | /** Keyed `namespace/name#number`. */ |
| 1269 | byPull: UsageSlice[]; |
| 1270 | byModel: UsageSlice[]; |
| 1271 | /** Credit bought in the period. */ |
| 1272 | addedMicros: number; |
| 1273 | }; |
| 1274 | |
| 1275 | /** One of g1t's products (a "bucket") on one day. Money in micros. */ |
| 1276 | export type CostDay = { day: string; bucket: string; cfCostMicros: number; ownCostMicros: number; valueMicros: number; cashMicros: number }; |
| 1277 | |
| 1278 | /** One product over the range: what customers were charged at price against what it cost. */ |
| 1279 | export type ProductMargin = { |
| 1280 | bucket: string; |
| 1281 | title: string; |
| 1282 | /** Cloudflare's bill, or g1t's own figure where Cloudflare does not bill it (`costSource`). */ |
| 1283 | costMicros: number; |
| 1284 | cfCostMicros: number; |
| 1285 | ownCostMicros: number; |
| 1286 | valueMicros: number; |
| 1287 | marginMicros: number; |
| 1288 | marginPercent: number | null; |
| 1289 | costSource: "cloudflare" | "ledger" | string; |
| 1290 | /** Running g1t, paid for by the plan. */ |
| 1291 | overhead: boolean; |
| 1292 | }; |
| 1293 | |
| 1294 | /** All of g1t: money in (usage and the plan) against every cost. */ |
| 1295 | export type OverallMargin = { |
| 1296 | usageMicros: number; |
| 1297 | plansMicros: number; |
| 1298 | costMicros: number; |
| 1299 | marginMicros: number; |
| 1300 | marginPercent: number | null; |
| 1301 | /** Of costMicros, what went on usage g1t gave away on purpose: comped workspaces, free periods, the trial and the open-source pool. */ |
| 1302 | givenMicros?: number; |
| 1303 | /** Money in against costMicros - givenMicros. */ |
| 1304 | soldMarginMicros?: number; |
| 1305 | soldMarginPercent?: number | null; |
| 1306 | /** Usage sold: usageMicros against what that usage cost, less what was given. */ |
| 1307 | usageCostMicros?: number; |
| 1308 | usageMarginMicros?: number; |
| 1309 | usageMarginPercent?: number | null; |
| 1310 | /** Running g1t: plansMicros against the platform's cost, less its given share. */ |
| 1311 | runningCostMicros?: number; |
| 1312 | /** Cost no mapping names, less its given share. */ |
| 1313 | unmappedCostMicros?: number; |
| 1314 | /** givenMicros by why. Free is free periods, free allowances and overruns g1t covered. */ |
| 1315 | givenCompedMicros?: number; |
| 1316 | givenFreeMicros?: number; |
| 1317 | givenTrialMicros?: number; |
| 1318 | givenPoolMicros?: number; |
| 1319 | /** What discounts on an account's terms took below cost plus the margin: given, not margin lost. */ |
| 1320 | givenDiscountMicros?: number; |
| 1321 | /** Credits from g1t spent on usage, by kind: given, never money in. Refunds come off money in instead. */ |
| 1322 | givenCreditPromotionalMicros?: number; |
| 1323 | givenCreditGoodwillMicros?: number; |
| 1324 | /** Credits over the range: given (every kind), spent on usage, and refunds' money given back. */ |
| 1325 | creditsGivenMicros?: number; |
| 1326 | creditsUsedMicros?: number; |
| 1327 | creditsRefundedMicros?: number; |
| 1328 | /** costMicros by who g1t pays: Cloudflare's bill (billed, after included allowances) and model providers (tokens, not on Cloudflare's bill). */ |
| 1329 | /** What the plan's included usage paid for, at price: money in for usage, paid out of plansMicros. */ |
| 1330 | includedMicros?: number; |
| 1331 | cloudflareCostMicros?: number; |
| 1332 | modelsCostMicros?: number; |
| 1333 | /** Tax collected with payments over the range, net of refunds: owed to tax authorities, never cash or revenue. */ |
| 1334 | taxCollectedMicros?: number; |
| 1335 | /** Card processing fees passed on with card payments, net of refunds: they pay Stripe's fee, not revenue. */ |
| 1336 | cardFeesMicros?: number; |
| 1337 | }; |
| 1338 | |
| 1339 | /** A count, cost or leak that does not add up. */ |
| 1340 | export type CostDrift = { |
| 1341 | bucket: string; |
| 1342 | title: string; |
| 1343 | kind: "count" | "cost" | "leak" | string; |
| 1344 | ours: number; |
| 1345 | cloudflare: number; |
| 1346 | deltaPercent: number | null; |
| 1347 | detail: string; |
| 1348 | foundAt: string; |
| 1349 | }; |
| 1350 | |
| 1351 | export type MarginAlert = { |
| 1352 | id: string; |
| 1353 | kind: "margin" | "overall" | "leak" | "drift" | "workspace" | string; |
| 1354 | /** The product, or the workspace. */ |
| 1355 | subject: string; |
| 1356 | detail: string; |
| 1357 | since: string; |
| 1358 | openedAt: string; |
| 1359 | emailedAt: string | null; |
| 1360 | }; |
| 1361 | |
| 1362 | /** A change to a price, measured from what Cloudflare charged. */ |
| 1363 | export type PriceProposal = { |
| 1364 | id: string; |
| 1365 | meter: string; |
| 1366 | title: string; |
| 1367 | unit: string; |
| 1368 | currentCostMicros: number; |
| 1369 | proposedCostMicros: number; |
| 1370 | changePercent: number; |
| 1371 | markupPercent: number; |
| 1372 | reason: string; |
| 1373 | source: "keeper" | "reconciler" | string; |
| 1374 | /** Far off the current cost: look before approving. */ |
| 1375 | suspect: boolean; |
| 1376 | status: "open" | "applied" | "approved" | "rejected" | "superseded" | string; |
| 1377 | createdAt: string; |
| 1378 | decidedAt: string | null; |
| 1379 | decidedBy: string | null; |
| 1380 | note: string | null; |
| 1381 | effectiveAt: string | null; |
| 1382 | }; |
| 1383 | |
| 1384 | /** One version of one meter's price; never changed once written. */ |
| 1385 | export type PriceVersion = { |
| 1386 | id: string; |
| 1387 | meter: string; |
| 1388 | version: number; |
| 1389 | costMicros: number; |
| 1390 | markupPercent: number; |
| 1391 | priceMicros: number; |
| 1392 | effectiveAt: string; |
| 1393 | reason: string; |
| 1394 | createdBy: string; |
| 1395 | appliedAt: string | null; |
| 1396 | }; |
| 1397 | |
| 1398 | /** What `resetBilling` removed. */ |
| 1399 | export type BillingReset = { workspace: string; rows: number; refreshed?: boolean }; |
| 1400 | |
| 1401 | export type WorkspaceCost = { workspace: string; costMicros: number; revenueMicros: number; givenMicros?: number; internal: boolean }; |
| 1402 | |
| 1403 | export type CostLineSummary = { |
| 1404 | product: string; |
| 1405 | meter: string; |
| 1406 | rawName: string; |
| 1407 | unit: string; |
| 1408 | source: string; |
| 1409 | quantity: number; |
| 1410 | costMicros: number; |
| 1411 | /** Absent when no mapping claims it. */ |
| 1412 | bucket: string | null; |
| 1413 | }; |
| 1414 | |
| 1415 | export type CostMapping = { |
| 1416 | product: string; |
| 1417 | meter: string; |
| 1418 | bucket: string; |
| 1419 | priceMeter: string | null; |
| 1420 | ownMeter: string | null; |
| 1421 | scaleToOwn: boolean; |
| 1422 | driftPercent: number; |
| 1423 | note: string; |
| 1424 | updatedAt: string; |
| 1425 | updatedBy: string; |
| 1426 | }; |
| 1427 | |
| 1428 | export type CostMappingInput = { |
| 1429 | product: string; |
| 1430 | meter: string; |
| 1431 | bucket?: string; |
| 1432 | priceMeter?: string | null; |
| 1433 | ownMeter?: string | null; |
| 1434 | scaleToOwn?: boolean; |
| 1435 | driftPercent?: number | null; |
| 1436 | note?: string; |
| 1437 | remove?: boolean; |
| 1438 | }; |
| 1439 | |
| 1440 | export type CostSettings = { |
| 1441 | autoApply: boolean; |
| 1442 | autoApplyPercent: number; |
| 1443 | noticeDays: number; |
| 1444 | marginFloorPercent: number; |
| 1445 | alertDays: number; |
| 1446 | minDailyCostMicros: number; |
| 1447 | anomalyFactor: number; |
| 1448 | anomalyFloorMicros: number; |
| 1449 | /** Pass Stripe's card fee on as its own line when AI credit is bought by card. */ |
| 1450 | cardFee: boolean; |
| 1451 | }; |
| 1452 | |
| 1453 | export type CostsReport = { |
| 1454 | configured: boolean; |
| 1455 | fetchedAt: string | null; |
| 1456 | since: string; |
| 1457 | until: string; |
| 1458 | days: CostDay[]; |
| 1459 | products: ProductMargin[]; |
| 1460 | overall: OverallMargin; |
| 1461 | drift: CostDrift[]; |
| 1462 | alerts: MarginAlert[]; |
| 1463 | proposals: PriceProposal[]; |
| 1464 | versions: PriceVersion[]; |
| 1465 | topWorkspaces: WorkspaceCost[]; |
| 1466 | lines: CostLineSummary[]; |
| 1467 | mappings: CostMapping[]; |
| 1468 | settings: CostSettings; |
| 1469 | /** g1t's own spend against its two caps. */ |
| 1470 | caps: SpendCaps; |
| 1471 | }; |
| 1472 | |
| 1473 | /** What g1t pays for itself, at cost, against its caps (billing's `budget`). */ |
| 1474 | export type SpendCaps = { |
| 1475 | /** Today (UTC), YYYY-MM-DD, and this month, YYYY-MM. */ |
| 1476 | day: string; |
| 1477 | month: string; |
| 1478 | /** What g1t paid for itself today across every workspace. */ |
| 1479 | todayMicros: number; |
| 1480 | /** `PLATFORM_DAILY_SPEND_CAP_MICROS`; 0: no breaker. */ |
| 1481 | dailyCapMicros: number; |
| 1482 | /** New hosted-model agent runs g1t would pay for are paused. */ |
| 1483 | tripped: boolean; |
| 1484 | trippedAt: string | null; |
| 1485 | liftedBy: string | null; |
| 1486 | liftedAt: string | null; |
| 1487 | liftNote: string | null; |
| 1488 | /** This month so far, by what paid: comped, trial, oss, given, unpaid. */ |
| 1489 | monthBuckets: { bucket: string; title: string; micros: number }[]; |
| 1490 | comped: CompedBudget[]; |
| 1491 | /** Free workspaces' share of reconciled costs this month (git, storage, platform). */ |
| 1492 | freeTierMicros: number; |
| 1493 | /** Cloudflare's subscriptions a month: read from Cloudflare each day, else `CLOUDFLARE_FIXED_MONTHLY_MICROS`, an estimate. */ |
| 1494 | fixedMonthlyMicros: number; |
| 1495 | fixedSource?: "cloudflare" | "estimate" | string; |
| 1496 | fixedReadAt?: string | null; |
| 1497 | /** Each subscription, when read from Cloudflare. */ |
| 1498 | fixedItems?: { name: string; monthlyMicros: number }[]; |
| 1499 | /** Money in this month, through the last reconciled day. */ |
| 1500 | revenueMicros: number; |
| 1501 | }; |
| 1502 | |
| 1503 | /** A comped account's monthly budget, at cost. */ |
| 1504 | export type CompedBudget = { |
| 1505 | account: string; |
| 1506 | name: string; |
| 1507 | usedMicros: number; |
| 1508 | /** 0: no budget. */ |
| 1509 | ceilingMicros: number; |
| 1510 | /** `COMPED_MONTHLY_CEILING_MICROS`, not the account's own limit. */ |
| 1511 | defaultCeiling: boolean; |
| 1512 | /** 50, 75, 90, 100, or 0. */ |
| 1513 | level: number; |
| 1514 | }; |
| 1515 | |
| 1516 | export type CostsRun = { lines: number; days: number; proposals: number; alerts: number; problems: string[] }; |
| 1517 | |
| 1518 | /** The product families the Usage page groups meters into, in order. */ |
| 1519 | export const PRODUCTS = [ |
| 1520 | { key: "agent", label: "Agent" }, |
| 1521 | { key: "sandboxes", label: "Sandboxes" }, |
| 1522 | { key: "gateway", label: "AI Gateway" }, |
| 1523 | { key: "deployments", label: "Deployments" }, |
| 1524 | { key: "git_storage", label: "Git & storage" }, |
| 1525 | { key: "packages", label: "Packages" }, |
| 1526 | { key: "security", label: "Security & quality" }, |
| 1527 | { key: "search", label: "Search" }, |
| 1528 | ] as const; |
| 1529 | |
| 1530 | export type ProductKey = (typeof PRODUCTS)[number]["key"]; |
| 1531 | |
| 1532 | /** What usage came to over a range, and what paid for it: price − discount − included − credits = charged. */ |
| 1533 | export type UsageTotals = { |
| 1534 | /** Usage at price, pending usage included. */ |
| 1535 | priceMicros: number; |
| 1536 | discountMicros: number; |
| 1537 | /** Paid by the plan's included usage, the trial and g1t's pools. */ |
| 1538 | includedMicros: number; |
| 1539 | /** Paid by AI credit and credit from g1t. */ |
| 1540 | creditsMicros: number; |
| 1541 | /** Left for the workspace to pay. */ |
| 1542 | chargedMicros: number; |
| 1543 | /** Metered this month, charged when it closes. */ |
| 1544 | pendingMicros: number; |
| 1545 | costMicros: number; |
| 1546 | }; |
| 1547 | |
| 1548 | export type UsageDay = { day: string; product: string; micros: number }; |
| 1549 | |
| 1550 | export type UsageAllowance = { used: number; of: number; unit: string }; |
| 1551 | |
| 1552 | export type ProjectUsage = { project: string; micros: number; quantity: number }; |
| 1553 | |
| 1554 | /** One meter over a range. */ |
| 1555 | export type MeterLine = { |
| 1556 | key: string; |
| 1557 | label: string; |
| 1558 | product: string; |
| 1559 | /** `tokens`, `seconds`, `bytes`, `operations`, `requests` or `entries`. */ |
| 1560 | unit: string; |
| 1561 | quantity: number; |
| 1562 | micros: number; |
| 1563 | pendingMicros?: number; |
| 1564 | /** Every day of the range, oldest first, at price. */ |
| 1565 | daily: number[]; |
| 1566 | allowance?: UsageAllowance | null; |
| 1567 | byProject: ProjectUsage[]; |
| 1568 | /** How the quantity is counted, when that needs saying (the agent rate's token weights). */ |
| 1569 | note?: string | null; |
| 1570 | }; |
| 1571 | |
| 1572 | export type FeatureUsage = { key: string; label: string; micros: number; count: number }; |
| 1573 | |
| 1574 | export type ProductUsage = { key: string; label: string; micros: number; meters: MeterLine[]; features?: FeatureUsage[] }; |
| 1575 | |
| 1576 | /** The tokens one model used over a range, as the model proxy counted them. */ |
| 1577 | export type ModelTokens = { model: string; input: number; output: number; cacheRead: number; cacheWrite: number }; |
| 1578 | |
| 1579 | export type UsageReport = { |
| 1580 | from: string; |
| 1581 | until: string; |
| 1582 | totals: UsageTotals; |
| 1583 | days: UsageDay[]; |
| 1584 | products: ProductUsage[]; |
| 1585 | projects: string[]; |
| 1586 | /** Agent tokens by model over the range, most first. */ |
| 1587 | models?: ModelTokens[]; |
| 1588 | /** The plan's included usage this month, in micros. */ |
| 1589 | included?: UsageAllowance | null; |
| 1590 | discountPercent?: number | null; |
| 1591 | aiCreditMicros: number; |
| 1592 | creditMicros: number; |
| 1593 | trialMicros?: number | null; |
| 1594 | plan: PlanKind; |
| 1595 | free: boolean; |
| 1596 | }; |
| 1597 | |
| 1598 | export type AiReload = { |
| 1599 | enabled: boolean; |
| 1600 | thresholdMicros: number; |
| 1601 | targetMicros: number; |
| 1602 | monthlyMaxMicros: number; |
| 1603 | reloadedMicros?: number; |
| 1604 | failedAt?: string | null; |
| 1605 | error?: string | null; |
| 1606 | }; |
| 1607 | |
| 1608 | export type CardFee = { on: boolean; percentMicros: number; fixedCents: number }; |
| 1609 | |
| 1610 | /** Prepaid AI credit: what Agent and AI Gateway usage draws on. */ |
| 1611 | export type AiCredit = { |
| 1612 | balanceMicros: number; |
| 1613 | purchasedMicros: number; |
| 1614 | givenMicros: number; |
| 1615 | grants: CreditGrant[]; |
| 1616 | /** A 100% discount pays for AI usage: nothing to buy. */ |
| 1617 | freeViaDiscount: boolean; |
| 1618 | /** Invoiced after use (an enterprise). */ |
| 1619 | postpaid: boolean; |
| 1620 | /** New runs on g1t's models are refused for want of credit. */ |
| 1621 | blocked: boolean; |
| 1622 | canBuy: boolean; |
| 1623 | presetsCents: number[]; |
| 1624 | minCents: number; |
| 1625 | maxCents: number; |
| 1626 | cardFee: CardFee; |
| 1627 | reload: AiReload; |
| 1628 | /** The agent rate per million tokens, at price. */ |
| 1629 | agentRateMicros: number; |
| 1630 | modelMarkupPercent: number; |
| 1631 | gatewayMarkupPercent: number; |
| 1632 | upgradeCreditMicros: number; |
| 1633 | expiresDays: number; |
| 1634 | }; |
| 1635 | |
| 1636 | export type PostalAddress = { line1: string; line2: string; city: string; state: string; postalCode: string; country: string }; |
| 1637 | |
| 1638 | export type PaymentMethod = { |
| 1639 | kind: string; |
| 1640 | brand?: string | null; |
| 1641 | last4?: string | null; |
| 1642 | expMonth?: number | null; |
| 1643 | expYear?: number | null; |
| 1644 | }; |
| 1645 | |
| 1646 | export type StripeInvoice = { |
| 1647 | id: string; |
| 1648 | number?: string | null; |
| 1649 | status: string; |
| 1650 | totalCents: number; |
| 1651 | currency: string; |
| 1652 | createdAt: string; |
| 1653 | description?: string | null; |
| 1654 | hostedUrl?: string | null; |
| 1655 | pdfUrl?: string | null; |
| 1656 | }; |
| 1657 | |
| 1658 | export type UpcomingInvoice = { closesAt: string; subscriptionsMicros: number; usageMicros: number; totalMicros: number }; |
| 1659 | |
| 1660 | export type BillingDetails = { |
| 1661 | customer: boolean; |
| 1662 | email: string | null; |
| 1663 | name: string | null; |
| 1664 | address: PostalAddress | null; |
| 1665 | taxIdType: string | null; |
| 1666 | taxId: string | null; |
| 1667 | poNumber: string | null; |
| 1668 | language: string | null; |
| 1669 | paymentMethod: PaymentMethod | null; |
| 1670 | invoices: StripeInvoice[]; |
| 1671 | upcoming: UpcomingInvoice; |
| 1672 | unavailable?: string | null; |
| 1673 | /** Whether Stripe Tax can place the customer from the address; without it nothing is charged. */ |
| 1674 | taxLocation?: boolean; |
| 1675 | /** Set when g1t did not charge for want of a billing address. */ |
| 1676 | taxAddressNeededAt?: string | null; |
| 1677 | /** Stripe's check of the tax ID: pending, verified, unverified or unavailable. */ |
| 1678 | taxIdStatus?: string | null; |
| 1679 | /** none, exempt or reverse, as set at Stripe. */ |
| 1680 | taxExempt?: string | null; |
| 1681 | }; |
| 1682 | |
| 1683 | export type BillingDetailsInput = { |
| 1684 | email?: string; |
| 1685 | name?: string; |
| 1686 | address?: PostalAddress; |
| 1687 | taxIdType?: string; |
| 1688 | taxId?: string; |
| 1689 | poNumber?: string; |
| 1690 | language?: string; |
| 1691 | }; |