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

217 lines12,123 bytesCodeBlame
1import type { BillingApi } from "./billing";
2import type { EventsApi } from "./events";
3import type { IdentityApi } from "./identity";
4import type { IntegrationsApi } from "./integrations";
5import type { ReposApi } from "./repos";
6import type { WorkApi } from "./work";
7
8/** A service binding, as far as these clients need it. */
9export type ServiceBinding = {
10 fetch(input: string, init?: RequestInit): Promise<Response>;
11};
12
13/**
14 * Calls a method on a service that speaks the JSON protocol used by the
15 * Rust services: `POST /rpc/<method>` with the arguments as the body.
16 */
17async function rpc<T>(
18 service: ServiceBinding,
19 method: string,
20 args: object,
21): Promise<T> {
22 // The hostname is ignored; a service binding always reaches its service.
23 const response = await service.fetch(`https://service/rpc/${method}`, {
24 method: "POST",
25 headers: { "content-type": "application/json" },
26 body: JSON.stringify(args),
27 });
28 if (!response.ok) {
29 throw new Error(`${method} failed with status ${response.status}`);
30 }
31 return (await response.json()) as T;
32}
33
34export function identityClient(service: ServiceBinding): IdentityApi {
35 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
36 return {
37 register: (username, email, password) =>
38 call("register", { username, email, password }),
39 signIn: (username, password) => call("sign_in", { username, password }),
40 signOut: (sessionToken) => call("sign_out", { sessionToken }),
41 resendVerification: (user) => call("resend_verification", { user }),
42 verifyEmail: (token) => call("verify_email", { token }),
43 requestPasswordReset: (email) => call("request_password_reset", { email }),
44 resetPassword: (token, password) =>
45 call("reset_password", { token, password }),
46 deviceStart: (clientName) => call("device_start", { clientName }),
47 deviceLookup: (userCode) => call("device_lookup", { userCode }),
48 deviceResolve: (userCode, user, approve) =>
49 call("device_resolve", { userCode, user, approve }),
50 deviceClaim: (deviceCode) => call("device_claim", { deviceCode }),
51 oauthAuthorize: (user, approval) => call("oauth_authorize", { user, ...approval }),
52 oauthExchange: (code, codeVerifier, clientId, redirectUri) =>
53 call("oauth_exchange", { code, codeVerifier, clientId, redirectUri }),
54 oauthRefresh: (refreshToken, clientId) =>
55 call("oauth_refresh", { refreshToken, clientId }),
56 listOAuthGrants: (user) => call("list_oauth_grants", { user }),
57 revokeOAuthGrant: (user, id) => call("revoke_oauth_grant", { user, id }),
58 createWorkspace: (user, slug, name) => call("create_workspace", { user, slug, name }),
59 getWorkspace: (slug) => call("get_workspace", { slug }),
60 listMembers: (slug, viewer) => call("list_members", { slug, viewer }),
61 addMember: (actor, slug, username) => call("add_member", { actor, slug, username }),
62 removeMember: (actor, slug, username) =>
63 call("remove_member", { actor, slug, username }),
64 updateWorkspace: (actor, slug, details) =>
65 call("update_workspace", { actor, slug, ...details }),
66 listWorkspaceTokens: (slug, viewer) => call("list_workspace_tokens", { slug, viewer }),
67 createWorkspaceToken: (actor, slug, name) =>
68 call("create_workspace_token", { actor, slug, name }),
69 removeWorkspaceToken: (actor, slug, id) =>
70 call("remove_workspace_token", { actor, slug, id }),
71 userForSession: (sessionToken) => call("user_for_session", { sessionToken }),
72 userForGitCredentials: (username, secret) =>
73 call("user_for_git_credentials", { username, secret }),
74 userForAccessToken: (token) => call("user_for_access_token", { token }),
75 userForSshKey: (fingerprint) => call("user_for_ssh_key", { fingerprint }),
76 userByUsername: (username) => call("user_by_username", { username }),
77 usernames: (ids) => call("usernames", { ids }),
78 listSshKeys: (user) => call("list_ssh_keys", { user }),
79 addSshKey: (user, title, publicKey) =>
80 call("add_ssh_key", { user, title, publicKey }),
81 removeSshKey: (user, id) => call("remove_ssh_key", { user, id }),
82 listAccessTokens: (user) => call("list_access_tokens", { user }),
83 createAccessToken: (user, name, ttlSeconds) =>
84 call("create_access_token", { user, name, ttlSeconds }),
85 createAgentToken: (onBehalfOf, scope, ttlSeconds) =>
86 call("create_agent_token", { onBehalfOf, scope, ttlSeconds }),
87 removeAccessToken: (user, id) => call("remove_access_token", { user, id }),
88 };
89}
90
91export function reposClient(service: ServiceBinding): ReposApi {
92 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
93 return {
94 get: (path, viewer) => call("get", { path, viewer }),
95 getById: (id, viewer) => call("get_by_id", { id, viewer }),
96 list: (viewer, options = {}) => call("list", { viewer, ...options }),
97 create: (owner, input) => call("create", { owner, ...input }),
98 update: (actor, path, changes) => call("update", { actor, path, ...changes }),
99 tree: (path, viewer, ref, treePath) =>
100 call("tree", { path, viewer, ref, treePath }),
101 blob: (path, viewer, ref, filePath) =>
102 call("blob", { path, viewer, ref, filePath }),
103 log: (path, viewer, ref, limit) => call("log", { path, viewer, ref, limit }),
104 blame: (path, viewer, ref, filePath) => call("blame", { path, viewer, ref, filePath }),
105 forkForPull: (sourceId, pullId, actor) =>
106 call("fork_for_pull", { sourceId, pullId, actor }),
107 gitAccess: (path, viewer, service) =>
108 call("git_access", { path, viewer, service }),
109 branches: (path, viewer) => call("branches", { path, viewer }),
110 land: (sourceId, actor, branch) => call("land", { sourceId, actor, branch }),
111 compare: (repoId, viewer, base, head) => call("compare", { repoId, viewer, base, head }),
112 };
113}
114
115export function workClient(service: ServiceBinding): WorkApi {
116 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
117 return {
118 openIssue: (actor, repo, input) => call("open_issue", { actor, repo, ...input }),
119 listIssues: (repo, viewer, filter = {}) => call("list_issues", { repo, viewer, ...filter }),
120 getIssue: (repo, number, viewer) => call("get_issue", { repo, number, viewer }),
121 updateIssue: (actor, repo, number, input) =>
122 call("update_issue", { actor, repo, number, ...input }),
123 closeIssue: (actor, repo, number, reason) =>
124 call("close_issue", { actor, repo, number, reason }),
125 reopenIssue: (actor, repo, number) => call("reopen_issue", { actor, repo, number }),
126 listLabels: (repo, viewer) => call("list_labels", { repo, viewer }),
127 counts: (repo, viewer) => call("counts", { repo, viewer }),
128 addComment: (actor, repo, number, comment) =>
129 call("add_comment", { actor, repo, number, ...comment }),
130 startChecks: (pullId) => call("start_checks", { pullId }),
131 reportChecks: (runId, token, report) =>
132 call("report_checks", { runId, token, ...report }),
133 startReview: (pullId) => call("start_review", { pullId }),
134 failReview: (runId, token, error) => call("report_review", { runId, token, error }),
135 advance: (pullId) => call("advance", { pullId }),
136 stall: (pullId, reason) => call("stall", { pullId, reason }),
137 managedPulls: (repoId) => call("managed_pulls", { repoId }),
138 queue: (repo, viewer) => call("queue", { repo, viewer }),
139 queueBuild: (repoId) => call("queue_build", { repoId }),
140 failQueue: (entryId, token, error) => call("report_queue", { entryId, token, error }),
141 removeFromQueue: (actor, repo, number) => call("remove_from_queue", { actor, repo, number }),
142 messageAgent: (actor, repo, number, body) => call("message_agent", { actor, repo, number, body }),
143 catchUpJob: (pullId) => call("catch_up_job", { pullId }),
144 getSettings: (repo, viewer) => call("get_settings", { repo, viewer }),
145 updateSettings: (actor, repo, settings) =>
146 call("update_settings", { actor, repo, settings }),
147 openPull: (actor, repo, input) => call("open_pull", { actor, repo, ...input }),
148 listPulls: (repo, viewer, state) => call("list_pulls", { repo, viewer, state }),
149 getPull: (repo, number, viewer) => call("get_pull", { repo, number, viewer }),
150 updatePull: (actor, repo, number, changes) =>
151 call("update_pull", { actor, repo, number, ...changes }),
152 readyPull: (actor, repo, number, summary) =>
153 call("ready_pull", { actor, repo, number, summary }),
154 closePull: (actor, repo, number) => call("close_pull", { actor, repo, number }),
155 mergePull: (actor, repo, number, options = {}) =>
156 call("merge_pull", { actor, repo, number, ...options }),
157 listActivePulls: (viewer) => call("list_active_pulls", { viewer }),
158 startPlan: (actor, repo, brief) => call("start_plan", { actor, repo, brief }),
159 failPlan: (planId, token, error) => call("report_plan", { planId, token, error }),
160 getPlan: (repo, viewer, id) => call("get_plan", { repo, viewer, id }),
161 listPlans: (repo, viewer) => call("list_plans", { repo, viewer }),
162 applyPlan: (actor, repo, id, options = {}) =>
163 call("apply_plan", { actor, repo, id, ...options }),
164 queueIssue: (actor, repo, number, queued) =>
165 call("queue_issue", { actor, repo, number, queued }),
166 readyIssues: (repoId) => call("ready_issues", { repoId }),
167 listAssignedIssues: (viewer) => call("list_assigned_issues", { viewer }),
168 appendSession: (actor, repo, number, entries) =>
169 call("append_session", { actor, repo, number, entries }),
170 readSession: (repo, number, viewer, afterSeq = 0) =>
171 call("read_session", { repo, number, viewer, afterSeq }),
172 };
173}
174
175export function billingClient(service: ServiceBinding): BillingApi {
176 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
177 return {
178 status: () => call("status", {}),
179 account: (workspace, viewer) => call("account", { workspace, viewer }),
180 ledger: (workspace, viewer) => call("ledger", { workspace, viewer }),
181 usage: (workspace, viewer, since) => call("usage", { workspace, viewer, since }),
182 checkout: (actor, workspace, amountCents, returnUrl) =>
183 call("checkout", { actor, workspace, amountCents, returnUrl }),
184 confirm: (workspace, viewer, session) => call("confirm", { workspace, viewer, session }),
185 canStart: (workspace) => call("can_start", { workspace }),
186 startRun: (run) => call("start_run", run),
187 };
188}
189
190export function eventsClient(service: ServiceBinding): EventsApi {
191 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
192 return {
193 publish: (events) => call("publish", { events }),
194 list: (query) => call("list", query),
195 };
196}
197
198export function integrationsClient(service: ServiceBinding): IntegrationsApi {
199 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
200 return {
201 list: (workspace, viewer) => call("list", { workspace, viewer }),
202 connect: (actor, workspace, input) => call("connect", { actor, workspace, ...input }),
203 update: (actor, workspace, id, input) => call("update", { actor, workspace, id, ...input }),
204 disconnect: (actor, workspace, id) => call("disconnect", { actor, workspace, id }),
205 test: (actor, workspace, id) => call("test", { actor, workspace, id }),
206 deliveries: (workspace, viewer, id) => call("deliveries", { workspace, viewer, id }),
207 resolve: (workspace, viewer, reference) => call("resolve", { workspace, viewer, reference }),
208 references: (workspace, text, limit) => call("references", { workspace, text, limit }),
209 import: (actor, repo, reference, assign) => call("import", { actor, repo, reference, assign }),
210 links: (repo, number) => call("links", { repo, number }),
211 modelProvider: (workspace) => call("model_provider", { workspace }),
212 openModelSession: (run) => call("open_model_session", run),
213 modelUpstream: (token) => call("model_upstream", { token }),
214 routes: (workspace, viewer) => call("routes", { workspace, viewer }),
215 setRoutes: (actor, workspace, routes) => call("set_routes", { actor, workspace, routes }),
216 };
217}