| 1 | import type { User, Viewer } from "./identity"; |
| 2 | import type { RepoPath } from "./repos"; |
| 3 | import type { Result } from "./result"; |
| 4 | |
| 5 | /** Millionths of a US dollar in one dollar: the unit money is held in. */ |
| 6 | export const MICROS_PER_DOLLAR = 1_000_000; |
| 7 | |
| 8 | /** Whether workspaces are charged for agents at all, and with real money. */ |
| 9 | export type BillingStatus = { |
| 10 | /** |
| 11 | * False when no card processor is configured: nothing is charged, and who |
| 12 | * may run agents is decided some other way. |
| 13 | */ |
| 14 | enabled: boolean; |
| 15 | /** False while the card processor is in its test mode, where cards are not real. */ |
| 16 | live: boolean; |
| 17 | /** |
| 18 | * True while g1t is being built out: runs are recorded with what they |
| 19 | * cost, but nothing is charged and no credit is needed. Not forever. |
| 20 | */ |
| 21 | free?: boolean; |
| 22 | }; |
| 23 | |
| 24 | /** A workspace's standing. */ |
| 25 | export type BillingAccount = { |
| 26 | workspace: string; |
| 27 | /** |
| 28 | * Credit left, in millionths of a dollar. Can dip below zero by the cost |
| 29 | * of the runs that were under way when it ran out. |
| 30 | */ |
| 31 | balanceMicros: number; |
| 32 | status: BillingStatus; |
| 33 | /** What is added to a run's cost, in percent. */ |
| 34 | marginPercent: number; |
| 35 | /** What a run on the workspace's own model provider is charged instead. */ |
| 36 | orchestrationFeeMicros: number; |
| 37 | }; |
| 38 | |
| 39 | /** One line of a workspace's statement. */ |
| 40 | export type LedgerEntry = { |
| 41 | id: string; |
| 42 | /** Credit bought with a card, or an agent's run. */ |
| 43 | kind: "top_up" | "usage"; |
| 44 | /** Positive for credit added, negative for usage. */ |
| 45 | amountMicros: number; |
| 46 | description: string; |
| 47 | /** For usage: the repository and pull request the agent worked on. */ |
| 48 | repo: string | null; |
| 49 | number: number | null; |
| 50 | /** For usage: `implement`, `review` or `update`. */ |
| 51 | task: string | null; |
| 52 | /** For usage: the model, by its public name. */ |
| 53 | model: string | null; |
| 54 | /** For usage: who paid the model provider. */ |
| 55 | billedTo: "g1t" | "workspace"; |
| 56 | /** For a top-up: the username of whoever paid. */ |
| 57 | createdBy: string | null; |
| 58 | /** RFC 3339. */ |
| 59 | createdAt: string; |
| 60 | }; |
| 61 | |
| 62 | /** What lets a sandbox, and nothing else, report what its run cost. */ |
| 63 | export type RunTicket = { runId: string; token: string }; |
| 64 | |
| 65 | /** |
| 66 | * What agents cost, charged to the workspace they worked for. A workspace |
| 67 | * buys credit; each run deducts its cost plus g1t's margin; with no credit, |
| 68 | * no agent starts. |
| 69 | */ |
| 70 | /** |
| 71 | * The free allowance on g1t's hosted models for a workspace not otherwise |
| 72 | * open to them: a few dollars of model cost each, out of one pool, until a |
| 73 | * date. Mirrors `Trial` in `crates/contracts/src/billing.rs`. |
| 74 | */ |
| 75 | export type Trial = { |
| 76 | open: boolean; |
| 77 | usedMicros: number; |
| 78 | limitMicros: number; |
| 79 | endsAt: string | null; |
| 80 | /** Why it is closed: `off`, `ended`, `used` (this workspace's) or `pool` (everyone's). */ |
| 81 | reason: "off" | "ended" | "used" | "pool" | null; |
| 82 | }; |
| 83 | |
| 84 | /** |
| 85 | * A paid feature a workspace turns on with a monthly plan, as Cloudflare's |
| 86 | * Workers for Platforms or Vercel's Pro are bought. Never free: neither |
| 87 | * `free` nor the model allowance covers it. Mirrors `Feature` in |
| 88 | * `crates/contracts/src/billing.rs`. |
| 89 | */ |
| 90 | export type Feature = "deployments"; |
| 91 | |
| 92 | /** What the Deployments plan includes each month. Mirrors `deployments_allowance`. */ |
| 93 | export const DEPLOYMENTS_ALLOWANCE = { |
| 94 | apps: 10, |
| 95 | requests: 1_000_000, |
| 96 | cpuMs: 3_000_000, |
| 97 | /** What Cloudflare charges g1t past that, in millionths of a dollar. */ |
| 98 | microsPerAppMonth: 20_000, |
| 99 | microsPerMillionRequests: 300_000, |
| 100 | microsPerMillionCpuMs: 20_000, |
| 101 | /** One second of a build's sandbox; builds are charged, not included. */ |
| 102 | microsPerBuildSecond: 21, |
| 103 | } as const; |
| 104 | |
| 105 | export type FeaturePlan = { |
| 106 | feature: Feature; |
| 107 | title: string; |
| 108 | /** Charged every month while the plan is on, in cents. */ |
| 109 | monthlyCents: number; |
| 110 | /** What the price includes, one line each. */ |
| 111 | includes: string[]; |
| 112 | /** How usage past the allowance is charged. */ |
| 113 | overage: string; |
| 114 | }; |
| 115 | |
| 116 | export type SubscriptionStatus = "active" | "canceling" | "past_due" | "canceled"; |
| 117 | |
| 118 | export type Subscription = { |
| 119 | feature: Feature; |
| 120 | status: SubscriptionStatus; |
| 121 | /** RFC 3339: when the period paid for ends. */ |
| 122 | periodEnd: string | null; |
| 123 | startedBy: string; |
| 124 | startedAt: string; |
| 125 | }; |
| 126 | |
| 127 | /** A feature as a workspace sees it. */ |
| 128 | export type FeatureState = { |
| 129 | plan: FeaturePlan; |
| 130 | subscription: Subscription | null; |
| 131 | /** Whether the feature works for the workspace now. */ |
| 132 | on: boolean; |
| 133 | }; |
| 134 | |
| 135 | export interface BillingApi { |
| 136 | status(): Promise<BillingStatus>; |
| 137 | /** Members of the workspace only. */ |
| 138 | account(workspace: string, viewer: Viewer): Promise<Result<BillingAccount>>; |
| 139 | /** Newest first. Members of the workspace only. */ |
| 140 | ledger(workspace: string, viewer: Viewer): Promise<Result<LedgerEntry[]>>; |
| 141 | /** What the workspace's agents cost since `since`, broken down. Members only. */ |
| 142 | usage(workspace: string, viewer: Viewer, since: string): Promise<Result<Usage>>; |
| 143 | /** |
| 144 | * Starts a card payment for credit and returns the page to send the |
| 145 | * person to. Owners only. The payment's id comes back to `returnUrl` as |
| 146 | * `session`. |
| 147 | */ |
| 148 | checkout(actor: User, workspace: string, amountCents: number, returnUrl: string): Promise<Result<{ url: string }>>; |
| 149 | /** Credits a payment once the processor says it was made. Safe to repeat. */ |
| 150 | confirm(workspace: string, viewer: Viewer, session: string): Promise<Result<BillingAccount>>; |
| 151 | /** |
| 152 | * Whether a workspace may start an agent now, asked before anything is |
| 153 | * opened for it. A failure, with the reason to show, when it has no credit. |
| 154 | */ |
| 155 | canStart(workspace: string): Promise<Result<boolean>>; |
| 156 | /** A workspace's free allowance on g1t's hosted models; `exempt` are open to them anyway. */ |
| 157 | trial(workspace: string, exempt: string[]): Promise<Trial>; |
| 158 | /** |
| 159 | * Asks whether a workspace may start an agent and opens the run it will be |
| 160 | * charged for. Null when billing is off; a failure when there is no credit. |
| 161 | */ |
| 162 | /** Every paid feature and the workspace's plan for each. Members only. */ |
| 163 | features(workspace: string, viewer: Viewer): Promise<Result<FeatureState[]>>; |
| 164 | /** |
| 165 | * Starts the card page for a feature's monthly plan. Owners only. The |
| 166 | * page's id comes back to `returnUrl` as `session`. |
| 167 | */ |
| 168 | subscribe(actor: User, workspace: string, feature: Feature, returnUrl: string): Promise<Result<{ url: string }>>; |
| 169 | /** Turns the feature on once the plan is paid for. Safe to repeat. */ |
| 170 | confirmSubscription(workspace: string, viewer: Viewer, session: string): Promise<Result<FeatureState>>; |
| 171 | /** Ends a plan at the end of its period, or (`resume`) takes that back. Owners only. */ |
| 172 | cancelSubscription(actor: User, workspace: string, feature: Feature, resume?: boolean): Promise<Result<FeatureState>>; |
| 173 | /** Whether a feature works for a workspace now; a failure with the reason when not. */ |
| 174 | hasFeature(workspace: string, feature: Feature): Promise<Result<boolean>>; |
| 175 | /** |
| 176 | * Usage past a plan's allowance, charged from credit at cost plus the |
| 177 | * margin, once per `reference`. False if it was charged before. |
| 178 | */ |
| 179 | chargeFeature(charge: { |
| 180 | workspace: string; |
| 181 | feature: Feature; |
| 182 | costMicros: number; |
| 183 | description: string; |
| 184 | repo?: string | null; |
| 185 | reference: string; |
| 186 | }): Promise<Result<boolean>>; |
| 187 | startRun(run: { |
| 188 | workspace: string; |
| 189 | repo: RepoPath; |
| 190 | number: number; |
| 191 | task: string; |
| 192 | model: string; |
| 193 | /** `workspace` when the run uses the workspace's own model provider. */ |
| 194 | billedTo?: "g1t" | "workspace"; |
| 195 | }): Promise<Result<RunTicket | null>>; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | /** One slice of usage: what it was for, what it cost, how many runs. */ |
| 200 | export type UsageSlice = { key: string; micros: number; runs: number }; |
| 201 | |
| 202 | /** What a workspace's agents cost over a period. */ |
| 203 | export type Usage = { |
| 204 | since: string; |
| 205 | /** Charged, including g1t's margin. */ |
| 206 | spentMicros: number; |
| 207 | /** What g1t's model provider charged, before the margin. */ |
| 208 | costMicros: number; |
| 209 | /** What runs on the workspace's own provider cost there, estimated. Not charged by g1t. */ |
| 210 | providerMicros: number; |
| 211 | /** What the runs used, at cost: g1t's models and the workspace's own provider together. */ |
| 212 | usedMicros: number; |
| 213 | /** g1t charges nothing for now; the slices then measure usage at cost. */ |
| 214 | free: boolean; |
| 215 | runs: number; |
| 216 | /** Spend per day and task, keyed `YYYY-MM-DD/task`. */ |
| 217 | byDay: UsageSlice[]; |
| 218 | byTask: UsageSlice[]; |
| 219 | byRepo: UsageSlice[]; |
| 220 | /** Keyed `namespace/name#number`. */ |
| 221 | byPull: UsageSlice[]; |
| 222 | byModel: UsageSlice[]; |
| 223 | /** Credit bought in the period. */ |
| 224 | addedMicros: number; |
| 225 | }; |