flagon-io/g1t

public

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t/packages/contracts/src/clients.ts

347 lines20,078 bytesCodeBlame
1import type { ActionsApi } from "./actions";
2import type { BillingAdminApi, BillingApi } from "./billing";
3import type { DeploymentsApi } from "./deployments";
4import type { ProjectsApi } from "./projects";
5import type { EventsApi } from "./events";
6import type { IdentityAdminApi, IdentityApi } from "./identity";
7import type { IntegrationsApi } from "./integrations";
8import type { WebhooksApi } from "./webhooks";
9import type { ReposApi } from "./repos";
10import type { WorkApi } from "./work";
11
12/** A service binding, as far as these clients need it. */
13export type ServiceBinding = {
14 fetch(input: string, init?: RequestInit): Promise<Response>;
15};
16
17/**
18 * Calls a method on a service that speaks the JSON protocol used by the
19 * Rust services: `POST /rpc/<method>` with the arguments as the body.
20 */
21async function rpc<T>(
22 service: ServiceBinding,
23 method: string,
24 args: object,
25): Promise<T> {
26 // The hostname is ignored; a service binding always reaches its service.
27 const response = await service.fetch(`https://service/rpc/${method}`, {
28 method: "POST",
29 headers: { "content-type": "application/json" },
30 body: JSON.stringify(args),
31 });
32 if (!response.ok) {
33 throw new Error(`${method} failed with status ${response.status}`);
34 }
35 return (await response.json()) as T;
36}
37
38export function identityClient(service: ServiceBinding): IdentityApi {
39 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
40 return {
41 register: (username, email, password) =>
42 call("register", { username, email, password }),
43 signIn: (username, password) => call("sign_in", { username, password }),
44 signOut: (sessionToken) => call("sign_out", { sessionToken }),
45 resendVerification: (user) => call("resend_verification", { user }),
46 verifyEmail: (token) => call("verify_email", { token }),
47 requestPasswordReset: (email) => call("request_password_reset", { email }),
48 resetPassword: (token, password) =>
49 call("reset_password", { token, password }),
50 deviceStart: (clientName) => call("device_start", { clientName }),
51 deviceLookup: (userCode) => call("device_lookup", { userCode }),
52 deviceResolve: (userCode, user, approve) =>
53 call("device_resolve", { userCode, user, approve }),
54 deviceClaim: (deviceCode) => call("device_claim", { deviceCode }),
55 oauthAuthorize: (user, approval) => call("oauth_authorize", { user, ...approval }),
56 oauthExchange: (code, codeVerifier, clientId, redirectUri) =>
57 call("oauth_exchange", { code, codeVerifier, clientId, redirectUri }),
58 oauthRefresh: (refreshToken, clientId) =>
59 call("oauth_refresh", { refreshToken, clientId }),
60 listOAuthGrants: (user) => call("list_oauth_grants", { user }),
61 revokeOAuthGrant: (user, id) => call("revoke_oauth_grant", { user, id }),
62 createWorkspace: (user, slug, name) => call("create_workspace", { user, slug, name }),
63 getWorkspace: (slug) => call("get_workspace", { slug }),
64 listMembers: (slug, viewer) => call("list_members", { slug, viewer }),
65 addMember: (actor, slug, username) => call("add_member", { actor, slug, username }),
66 removeMember: (actor, slug, username) =>
67 call("remove_member", { actor, slug, username }),
68 updateWorkspace: (actor, slug, details) =>
69 call("update_workspace", { actor, slug, ...details }),
70 listWorkspaceTokens: (slug, viewer) => call("list_workspace_tokens", { slug, viewer }),
71 createWorkspaceToken: (actor, slug, name) =>
72 call("create_workspace_token", { actor, slug, name }),
73 removeWorkspaceToken: (actor, slug, id) =>
74 call("remove_workspace_token", { actor, slug, id }),
75 userForSession: (sessionToken) => call("user_for_session", { sessionToken }),
76 userForGitCredentials: (username, secret) =>
77 call("user_for_git_credentials", { username, secret }),
78 userForAccessToken: (token) => call("user_for_access_token", { token }),
79 userForSshKey: (fingerprint) => call("user_for_ssh_key", { fingerprint }),
80 userByUsername: (username) => call("user_by_username", { username }),
81 usernames: (ids) => call("usernames", { ids }),
82 listSshKeys: (user) => call("list_ssh_keys", { user }),
83 addSshKey: (user, title, publicKey) =>
84 call("add_ssh_key", { user, title, publicKey }),
85 removeSshKey: (user, id) => call("remove_ssh_key", { user, id }),
86 listAccessTokens: (user) => call("list_access_tokens", { user }),
87 createAccessToken: (user, name, ttlSeconds) =>
88 call("create_access_token", { user, name, ttlSeconds }),
89 createAgentToken: (onBehalfOf, scope, ttlSeconds) =>
90 call("create_agent_token", { onBehalfOf, scope, ttlSeconds }),
91 removeAccessToken: (user, id) => call("remove_access_token", { user, id }),
92 };
93}
94
95/** Staff-only identity. Only sudo binds to it; see `IdentityAdminApi`. */
96export function identityAdminClient(service: ServiceBinding): IdentityAdminApi {
97 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
98 return {
99 workspaces: (query) => call("admin_workspaces", { query: query ?? null }),
100 workspace: (slug) => call("admin_workspace", { slug }),
101 };
102}
103
104export function reposClient(service: ServiceBinding): ReposApi {
105 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
106 return {
107 get: (path, viewer) => call("get", { path, viewer }),
108 getById: (id, viewer) => call("get_by_id", { id, viewer }),
109 list: (viewer, options = {}) => call("list", { viewer, ...options }),
110 create: (owner, input) => call("create", { owner, ...input }),
111 update: (actor, path, changes) => call("update", { actor, path, ...changes }),
112 tree: (path, viewer, ref, treePath) =>
113 call("tree", { path, viewer, ref, treePath }),
114 blob: (path, viewer, ref, filePath) =>
115 call("blob", { path, viewer, ref, filePath }),
116 log: (path, viewer, ref, limit) => call("log", { path, viewer, ref, limit }),
117 blame: (path, viewer, ref, filePath) => call("blame", { path, viewer, ref, filePath }),
118 forkForPull: (sourceId, pullId, actor) =>
119 call("fork_for_pull", { sourceId, pullId, actor }),
120 gitAccess: (path, viewer, service) =>
121 call("git_access", { path, viewer, service }),
122 branches: (path, viewer) => call("branches", { path, viewer }),
123 land: (sourceId, actor, branch) => call("land", { sourceId, actor, branch }),
124 compare: (repoId, viewer, base, head) => call("compare", { repoId, viewer, base, head }),
125 };
126}
127
128export function workClient(service: ServiceBinding): WorkApi {
129 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
130 return {
131 openIssue: (actor, repo, input) => call("open_issue", { actor, repo, ...input }),
132 listIssues: (repo, viewer, filter = {}) => call("list_issues", { repo, viewer, ...filter }),
133 getIssue: (repo, number, viewer) => call("get_issue", { repo, number, viewer }),
134 updateIssue: (actor, repo, number, input) =>
135 call("update_issue", { actor, repo, number, ...input }),
136 closeIssue: (actor, repo, number, reason) =>
137 call("close_issue", { actor, repo, number, reason }),
138 reopenIssue: (actor, repo, number) => call("reopen_issue", { actor, repo, number }),
139 listLabels: (repo, viewer) => call("list_labels", { repo, viewer }),
140 counts: (repo, viewer) => call("counts", { repo, viewer }),
141 addComment: (actor, repo, number, comment) =>
142 call("add_comment", { actor, repo, number, ...comment }),
143 startChecks: (pullId) => call("start_checks", { pullId }),
144 reportChecks: (runId, token, report) =>
145 call("report_checks", { runId, token, ...report }),
146 startReview: (pullId) => call("start_review", { pullId }),
147 failReview: (runId, token, error) => call("report_review", { runId, token, error }),
148 advance: (pullId) => call("advance", { pullId }),
149 stall: (pullId, reason) => call("stall", { pullId, reason }),
150 managedPulls: (repoId) => call("managed_pulls", { repoId }),
151 queue: (repo, viewer) => call("queue", { repo, viewer }),
152 queueBuild: (repoId) => call("queue_build", { repoId }),
153 failQueue: (entryId, token, error) => call("report_queue", { entryId, token, error }),
154 removeFromQueue: (actor, repo, number) => call("remove_from_queue", { actor, repo, number }),
155 messageAgent: (actor, repo, number, body) => call("message_agent", { actor, repo, number, body }),
156 catchUpJob: (pullId) => call("catch_up_job", { pullId }),
157 wakeForMessages: (pullId) => call("wake_for_messages", { pullId }),
158 getSettings: (repo, viewer) => call("get_settings", { repo, viewer }),
159 updateSettings: (actor, repo, settings) =>
160 call("update_settings", { actor, repo, settings }),
161 openPull: (actor, repo, input) => call("open_pull", { actor, repo, ...input }),
162 listPulls: (repo, viewer, state) => call("list_pulls", { repo, viewer, state }),
163 getPull: (repo, number, viewer) => call("get_pull", { repo, number, viewer }),
164 updatePull: (actor, repo, number, changes) =>
165 call("update_pull", { actor, repo, number, ...changes }),
166 readyPull: (actor, repo, number, summary) =>
167 call("ready_pull", { actor, repo, number, summary }),
168 closePull: (actor, repo, number) => call("close_pull", { actor, repo, number }),
169 mergePull: (actor, repo, number, options = {}) =>
170 call("merge_pull", { actor, repo, number, ...options }),
171 listActivePulls: (viewer) => call("list_active_pulls", { viewer }),
172 startPlan: (actor, repo, brief) => call("start_plan", { actor, repo, brief }),
173 failPlan: (planId, token, error) => call("report_plan", { planId, token, error }),
174 getPlan: (repo, viewer, id) => call("get_plan", { repo, viewer, id }),
175 listPlans: (repo, viewer) => call("list_plans", { repo, viewer }),
176 applyPlan: (actor, repo, id, options = {}) =>
177 call("apply_plan", { actor, repo, id, ...options }),
178 queueIssue: (actor, repo, number, queued) =>
179 call("queue_issue", { actor, repo, number, queued }),
180 readyIssues: (repoId) => call("ready_issues", { repoId }),
181 listAssignedIssues: (viewer) => call("list_assigned_issues", { viewer }),
182 appendSession: (actor, repo, number, entries) =>
183 call("append_session", { actor, repo, number, entries }),
184 readSession: (repo, number, viewer, afterSeq = 0) =>
185 call("read_session", { repo, number, viewer, afterSeq }),
186 };
187}
188
189export function billingClient(service: ServiceBinding): BillingApi {
190 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
191 return {
192 status: () => call("status", {}),
193 account: (workspace, viewer) => call("account", { workspace, viewer }),
194 ledger: (workspace, viewer) => call("ledger", { workspace, viewer }),
195 usage: (workspace, viewer, since) => call("usage", { workspace, viewer, since }),
196 checkout: (actor, workspace, amountCents, returnUrl) =>
197 call("checkout", { actor, workspace, amountCents, returnUrl }),
198 confirm: (workspace, viewer, session) => call("confirm", { workspace, viewer, session }),
199 canStart: (workspace) => call("can_start", { workspace }),
200 trial: (workspace, exempt) => call("trial", { workspace, exempt }),
201 startRun: (run) => call("start_run", run),
202 features: (workspace, viewer) => call("features", { workspace, viewer }),
203 subscribe: (actor, workspace, feature, returnUrl) =>
204 call("subscribe", { actor, workspace, feature, returnUrl }),
205 confirmSubscription: (workspace, viewer, session) =>
206 call("confirm_subscription", { workspace, viewer, session }),
207 cancelSubscription: (actor, workspace, feature, resume = false) =>
208 call("cancel_subscription", { actor, workspace, feature, resume }),
209 hasFeature: (workspace, feature) => call("has_feature", { workspace, feature }),
210 chargeFeature: (charge) => call("charge_feature", charge),
211 billingPortal: (actor, workspace, returnUrl) => call("billing_portal", { actor, workspace, return_url: returnUrl }),
212 recordSandbox: (usage) => call("record_sandbox", usage),
213 limit: (workspace, viewer) => call("limit", { workspace, viewer }),
214 checkLimit: (workspace) => call("check_limit", { workspace }),
215 prices: () => call("prices", {}),
216 notePending: (workspace, source, costMicros) => call("note_pending", { workspace, source, costMicros }),
217 setSpendLimit: (actor, workspace, spendLimitMicros, useFullLimit = false) =>
218 call("set_spend_limit", { actor, workspace, spendLimitMicros, use_full_limit: useFullLimit }),
219 invoices: (workspace, viewer) => call("invoices", { workspace, viewer }),
220 };
221}
222
223export function billingAdminClient(service: ServiceBinding): BillingAdminApi {
224 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
225 return {
226 accounts: (query) => call("admin_accounts", { query: query ?? null }),
227 account: (id) => call("admin_account", { id }),
228 setTerms: (id, terms, by) => call("admin_set_terms", { id, terms, by }),
229 createEnterprise: (name, workspaces, by) => call("admin_create_enterprise", { name, workspaces, by }),
230 attach: (workspace, account, by) => call("admin_attach", { workspace, account, by }),
231 credit: (workspace, amountMicros, note, by) => call("admin_credit", { workspace, amount_micros: amountMicros, note, by }),
232 billingLink: (workspace, by) => call("admin_billing_link", { workspace, by }),
233 stripe: (setup = false, by) => call("admin_stripe", { setup, by: by ?? null }),
234 enterpriseBilling: (id, email, by) => call("admin_enterprise_billing", { id, email, by }),
235 invoiceEnterprise: (id, by) => call("admin_invoice_enterprise", { id, by }),
236 accountsFor: (workspaces) => call("admin_accounts", { query: null, workspaces }),
237 signals: () => call("admin_signals", {}),
238 overview: () => call("admin_overview", {}),
239 sales: (workspace) => call("admin_sales", { workspace }),
240 setSales: (workspace, record, by) =>
241 call("admin_set_sales", {
242 workspace,
243 stage: record.stage,
244 owner: record.owner ?? null,
245 next_step: record.nextStep ?? null,
246 next_at: record.nextAt ?? null,
247 by,
248 }),
249 addNote: (workspace, text, by) => call("admin_add_note", { workspace, text, by }),
250 workspaceInvoices: (workspace) => call("admin_workspace_invoices", { workspace }),
251 allInvoices: (filter = {}) => call("admin_invoices", { status: filter.status ?? null, month: filter.month ?? null }),
252 audit: (filter = {}) =>
253 call("admin_audit", { by: filter.by ?? null, action: filter.action ?? null, before: filter.before ?? null }),
254 };
255}
256
257export function eventsClient(service: ServiceBinding): EventsApi {
258 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
259 return {
260 publish: (events) => call("publish", { events }),
261 list: (query) => call("list", query),
262 };
263}
264
265export function integrationsClient(service: ServiceBinding): IntegrationsApi {
266 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
267 return {
268 list: (workspace, viewer) => call("list", { workspace, viewer }),
269 connect: (actor, workspace, input) => call("connect", { actor, workspace, ...input }),
270 update: (actor, workspace, id, input) => call("update", { actor, workspace, id, ...input }),
271 disconnect: (actor, workspace, id) => call("disconnect", { actor, workspace, id }),
272 test: (actor, workspace, id) => call("test", { actor, workspace, id }),
273 deliveries: (workspace, viewer, id) => call("deliveries", { workspace, viewer, id }),
274 resolve: (workspace, viewer, reference) => call("resolve", { workspace, viewer, reference }),
275 references: (workspace, text, limit) => call("references", { workspace, text, limit }),
276 import: (actor, repo, reference, assign) => call("import", { actor, repo, reference, assign }),
277 links: (repo, number) => call("links", { repo, number }),
278 modelProvider: (workspace) => call("model_provider", { workspace }),
279 openModelSession: (run) => call("open_model_session", run),
280 modelUpstream: (token) => call("model_upstream", { token }),
281 routes: (workspace, viewer) => call("routes", { workspace, viewer }),
282 setRoutes: (actor, workspace, routes) => call("set_routes", { actor, workspace, routes }),
283 };
284}
285
286export function webhooksClient(service: ServiceBinding): WebhooksApi {
287 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
288 return {
289 list: (viewer, owner) => call("list", { viewer, ...owner }),
290 create: (actor, owner, input) => call("create", { actor, ...owner, ...input }),
291 update: (actor, owner, id, input) => call("update", { actor, ...owner, id, ...input }),
292 delete: (actor, owner, id) => call("delete", { actor, ...owner, id }),
293 ping: (actor, owner, id) => call("ping", { actor, ...owner, id }),
294 deliveries: (viewer, owner, id) => call("deliveries", { viewer, ...owner, id }),
295 redeliver: (actor, owner, deliveryId) => call("redeliver", { actor, ...owner, deliveryId }),
296 };
297}
298
299export function actionsClient(service: ServiceBinding): ActionsApi {
300 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
301 return {
302 workflows: (repo, viewer) => call("workflows", { repo, viewer }),
303 runs: (repo, viewer, filter = {}) => call("runs", { repo, viewer, ...filter }),
304 run: (repo, viewer, id) => call("run", { repo, viewer, id }),
305 logs: (repo, viewer, job, after = 0) => call("logs", { repo, viewer, job, after }),
306 dispatch: (actor, repo, workflow, ref, inputs) => call("dispatch", { actor, repo, workflow, ref, inputs }),
307 cancel: (actor, repo, id) => call("cancel", { actor, repo, id }),
308 rerun: (actor, repo, id, failedOnly = false) => call("rerun", { actor, repo, id, failed_only: failedOnly }),
309 setWorkflowEnabled: (actor, repo, workflow, enabled) =>
310 call("set_workflow_enabled", { actor, repo, workflow, enabled }),
311 settings: (actor, owner, kind) => call("settings", { actor, ...owner, kind }),
312 setSetting: (actor, owner, kind, name, value, options = {}) =>
313 call("set_setting", { actor, ...owner, kind, name, value, ...options }),
314 deleteSetting: (actor, owner, kind, name, id) => call("delete_setting", { actor, ...owner, kind, name, id }),
315 };
316}
317
318
319export function deploymentsClient(service: ServiceBinding): DeploymentsApi {
320 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
321 return {
322 settings: (project, viewer) => call("settings", { project, viewer }),
323 updateSettings: (actor, project, changes) => call("update_settings", { actor, project, changes }),
324 list: (project, viewer) => call("list", { project, viewer }),
325 get: (project, id, viewer) => call("get", { project, id, viewer }),
326 redeploy: (actor, project, branch) => call("redeploy", { actor, project, branch }),
327 takeDown: (actor, project, branch) => call("take_down", { actor, project, branch }),
328 stack: (actor, project, branch) => call("stack", { actor, project, branch }),
329 overview: (workspace, viewer) => call("overview", { workspace, viewer }),
330 usage: (workspace, viewer) => call("usage", { workspace, viewer }),
331 };
332}
333
334export function projectsClient(service: ServiceBinding): ProjectsApi {
335 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
336 return {
337 list: (workspace, viewer) => call("list", { workspace, viewer }),
338 get: (workspace, slug, viewer) => call("get", { workspace, slug, viewer }),
339 byRepo: (repoId) => call("by_repo", { repoId }),
340 create: (actor, workspace, input) => call("create", { actor, workspace, input }),
341 update: (actor, workspace, slug, changes) => call("update", { actor, workspace, slug, changes }),
342 dependencies: (workspace, slug, viewer) => call("dependencies", { workspace, slug, viewer }),
343 addDependency: (actor, workspace, slug, on, as) => call("add_dependency", { actor, workspace, slug, on, as }),
344 removeDependency: (actor, workspace, slug, on) => call("remove_dependency", { actor, workspace, slug, on }),
345 graph: (projectId) => call("graph", { projectId }),
346 };
347}