pr_01m47d15m3e54sn21z27rpy5n9/packages/contracts/src/billing.ts

91 lines3,296 bytesCodeBlame
1import type { User, Viewer } from "./identity";
2import type { RepoPath } from "./repos";
3import type { Result } from "./result";
4
5/** Millionths of a US dollar in one dollar: the unit money is held in. */
6export const MICROS_PER_DOLLAR = 1_000_000;
7
8/** Whether workspaces are charged for agents at all, and with real money. */
9export 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
19/** A workspace's standing. */
20export type BillingAccount = {
21 workspace: string;
22 /**
23 * Credit left, in millionths of a dollar. Can dip below zero by the cost
24 * of the runs that were under way when it ran out.
25 */
26 balanceMicros: number;
27 status: BillingStatus;
28 /** What is added to a run's cost, in percent. */
29 marginPercent: number;
30};
31
32/** One line of a workspace's statement. */
33export type LedgerEntry = {
34 id: string;
35 /** Credit bought with a card, or an agent's run. */
36 kind: "top_up" | "usage";
37 /** Positive for credit added, negative for usage. */
38 amountMicros: number;
39 description: string;
40 /** For usage: the repository and pull request the agent worked on. */
41 repo: string | null;
42 number: number | null;
43 /** For usage: `implement`, `review` or `update`. */
44 task: string | null;
45 /** For usage: the model, by its public name. */
46 model: string | null;
47 /** For a top-up: the username of whoever paid. */
48 createdBy: string | null;
49 /** RFC 3339. */
50 createdAt: string;
51};
52
53/** What lets a sandbox, and nothing else, report what its run cost. */
54export type RunTicket = { runId: string; token: string };
55
56/**
57 * What agents cost, charged to the workspace they worked for. A workspace
58 * buys credit; each run deducts its cost plus g1t's margin; with no credit,
59 * no agent starts.
60 */
61export interface BillingApi {
62 status(): Promise<BillingStatus>;
63 /** Members of the workspace only. */
64 account(workspace: string, viewer: Viewer): Promise<Result<BillingAccount>>;
65 /** Newest first. Members of the workspace only. */
66 ledger(workspace: string, viewer: Viewer): Promise<Result<LedgerEntry[]>>;
67 /**
68 * Starts a card payment for credit and returns the page to send the
69 * person to. Owners only. The payment's id comes back to `returnUrl` as
70 * `session`.
71 */
72 checkout(actor: User, workspace: string, amountCents: number, returnUrl: string): Promise<Result<{ url: string }>>;
73 /** Credits a payment once the processor says it was made. Safe to repeat. */
74 confirm(workspace: string, viewer: Viewer, session: string): Promise<Result<BillingAccount>>;
75 /**
76 * Whether a workspace may start an agent now, asked before anything is
77 * opened for it. A failure, with the reason to show, when it has no credit.
78 */
79 canStart(workspace: string): Promise<Result<boolean>>;
80 /**
81 * Asks whether a workspace may start an agent and opens the run it will be
82 * charged for. Null when billing is off; a failure when there is no credit.
83 */
84 startRun(run: {
85 workspace: string;
86 repo: RepoPath;
87 number: number;
88 task: string;
89 model: string;
90 }): Promise<Result<RunTicket | null>>;
91}