Skip to content
991 linesCodeBlameRaw
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 { PackagesApi } from "./packages";
9import type { WebhooksApi } from "./webhooks";
10import type { ReposApi } from "./repos";
11import type { PullDetail, WorkApi } from "./work";
12import type { Result } from "./result";
13import type { RunnerComputerApi } from "./runner";
14import type { RunnersApi } from "./runners";
15
16/** A service binding, as far as these clients need it. */
17export type ServiceBinding = {
18 fetch(input: string, init?: RequestInit): Promise<Response>;
19};
20
21/**
22 * Calls a method on a service that speaks the JSON protocol used by the
23 * Rust services: `POST /rpc/<method>` with the arguments as the body.
24 */
25async function rpc<T>(
26 service: ServiceBinding,
27 method: string,
28 args: object,
29): Promise<T> {
30 // The hostname is ignored; a service binding always reaches its service.
31 const response = await service.fetch(`https://service/rpc/${method}`, {
32 method: "POST",
33 headers: { "content-type": "application/json" },
34 body: JSON.stringify(args),
35 });
36 if (!response.ok) {
37 throw new Error(`${method} failed with status ${response.status}`);
38 }
39 return (await response.json()) as T;
40}
41
42export function identityClient(service: ServiceBinding): IdentityApi {
43 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
44 return {
45 register: (username, email, password, inviteCode, client, emailProof) =>
46 call("register", {
47 username,
48 email,
49 password,
50 invite_code: inviteCode ?? null,
51 email_proof: emailProof ?? null,
52 client: client ?? null,
53 }),
54 signIn: (username, password, client) => call("sign_in", { username, password, client: client ?? null }),
55 twoFactorSignIn: (challenge, code, client) => call("two_factor_sign_in", { challenge, code, client: client ?? null }),
56 signOut: (sessionToken) => call("sign_out", { sessionToken }),
57 resendVerification: (user) => call("resend_verification", { user }),
58 verifyEmail: (token) => call("verify_email", { token }),
59 requestPasswordReset: (email, client) => call("request_password_reset", { email, client: client ?? null }),
60 resetPassword: (token, password) =>
61 call("reset_password", { token, password }),
62 deviceStart: (clientName) => call("device_start", { clientName }),
63 deviceLookup: (userCode) => call("device_lookup", { userCode }),
64 deviceResolve: (userCode, user, approve) =>
65 call("device_resolve", { userCode, user, approve }),
66 deviceClaim: (deviceCode) => call("device_claim", { deviceCode }),
67 oauthAuthorize: (user, approval) => call("oauth_authorize", { user, ...approval }),
68 oauthExchange: (code, codeVerifier, clientId, redirectUri) =>
69 call("oauth_exchange", { code, codeVerifier, clientId, redirectUri }),
70 oauthRefresh: (refreshToken, clientId) =>
71 call("oauth_refresh", { refreshToken, clientId }),
72 listOAuthGrants: (user) => call("list_oauth_grants", { user }),
73 revokeOAuthGrant: (user, id) => call("revoke_oauth_grant", { user, id }),
74 updateOAuthGrant: (user, id, grant) =>
75 call("update_oauth_grant", { user, id, scopes: grant.scopes }),
76 createWorkspace: (user, slug, name) => call("create_workspace", { user, slug, name }),
77 getWorkspace: (slug) => call("get_workspace", { slug }),
78 workspaceResidency: (slug) => call("workspace_residency", { slug }),
79 setWorkspaceResidency: (actor, slug, residency) => call("set_workspace_residency", { actor, slug, residency }),
80 listMembers: (slug, viewer) => call("list_members", { slug, viewer }),
81 addMember: (actor, slug, username) => call("add_member", { actor, slug, username }),
82 removeMember: (actor, slug, username) =>
83 call("remove_member", { actor, slug, username, surface: "web" }),
84 updateMember: (actor, slug, username, change) =>
85 call("update_member", { actor, slug, username, role: change.role ?? null, org_roles: change.org_roles ?? null, surface: "web" }),
86 transferOwnership: (actor, slug, username) => call("transfer_ownership", { actor, slug, username, surface: "web" }),
87 leaveWorkspace: (user, slug) => call("leave_workspace", { user, slug, surface: "web" }),
88 setMemberPrivileges: (actor, slug, change) => call("set_member_privileges", { actor, slug, privileges: change, surface: "web" }),
89 setTwoFactorRequirement: (actor, slug, required) =>
90 call("set_two_factor_requirement", { actor, slug, required, surface: "web" }),
91 updateWorkspace: (actor, slug, details) =>
92 call("update_workspace", { actor, slug, ...details }),
93 renameWorkspace: (actor, slug, newSlug) => call("rename_workspace", { actor, slug, newSlug }),
94 checkWorkspaceRename: (actor, slug, newSlug) =>
95 call("check_workspace_rename", { actor, slug, newSlug }),
96 resolveSlug: (slug) => call("resolve_slug", { slug }),
97 deleteWorkspace: (actor, slug, confirm) => call("delete_workspace", { actor, slug, confirm }),
98 checkWorkspaceDeletion: (actor, slug) => call("check_workspace_deletion", { actor, slug }),
99 setWorkspaceAvatar: (actor, slug, image) => call("set_workspace_avatar", { actor, slug, image }),
100 setUserAvatar: (user, image) => call("set_user_avatar", { user, image }),
101 listWorkspaceTokens: (slug, viewer) => call("list_workspace_tokens", { slug, viewer }),
102 removeWorkspaceToken: (actor, slug, id) =>
103 call("remove_workspace_token", { actor, slug, id }),
104 createToken: (actor, input) =>
105 call("create_token", {
106 actor,
107 owner: input.owner ?? null,
108 name: input.name,
109 description: input.description ?? null,
110 ttl_seconds: input.ttlSeconds,
111 workspace: input.workspace,
112 repository_selection: input.repositorySelection,
113 repositories: input.repositories,
114 permissions: input.permissions,
115 website: input.website ?? false,
116 }),
117 updateToken: (actor, id, change, owner) =>
118 call("update_token", {
119 actor,
120 id,
121 owner: owner ?? null,
122 name: change.name ?? null,
123 description: change.description ?? null,
124 repository_selection: change.repositorySelection ?? null,
125 repositories: change.repositories ?? null,
126 permissions: change.permissions ?? null,
127 website: change.website ?? null,
128 }),
129 getTokenPolicy: (slug, viewer) => call("get_token_policy", { slug, viewer }),
130 setTokenPolicy: (actor, slug, change) =>
131 call("set_token_policy", {
132 actor,
133 slug,
134 allow_tokens_for_all_workspaces: change.allowTokensForAllWorkspaces ?? null,
135 allow_tokens_for_this_workspace: change.allowTokensForThisWorkspace ?? null,
136 require_approval: change.requireApproval ?? null,
137 max_lifetime_days: change.maxLifetimeDays ?? null,
138 forbid_no_expiry: change.forbidNoExpiry ?? null,
139 surface: "web",
140 }),
141 listMemberTokens: (actor, slug, filter = {}) =>
142 call("list_member_tokens", { actor, slug, status: filter.status ?? null }),
143 reviewTokenRequest: (actor, slug, id, approve, reason) =>
144 call("review_token_request", { actor, slug, id, approve, reason: reason ?? null, surface: "web" }),
145 revokeMemberToken: (actor, slug, id, reason) =>
146 call("revoke_member_token", { actor, slug, id, reason: reason ?? null, surface: "web" }),
147 userForSession: (sessionToken) => call("user_for_session", { sessionToken }),
148 registration: () => call("registration", {}),
149 listInvites: (user) => call("list_invites", { user }),
150 createInvite: (user, options = {}) =>
151 call("create_invite", { user, email: options.email ?? null, workspace: options.workspace ?? null, join: options.join ?? null, join_role: options.join ? (options.joinRole ?? null) : null }),
152 revokeInvite: (user, id) => call("revoke_invite", { user, id }),
153 checkInvite: (code, client, options = {}) =>
154 call("check_invite", {
155 code,
156 client: client ?? null,
157 viewer: options.viewer ?? null,
158 any_status: options.anyStatus ?? false,
159 email_proof: options.emailProof ?? null,
160 }),
161 acceptInvite: (user, code) => call("accept_invite", { user, code }),
162 inviteMember: (actor, slug, who) =>
163 call("invite_member", { actor, slug, email: who.email ?? "", username: who.username ?? null, role: who.role ?? null, message: who.message ?? null }),
164 listInvitations: (user) => call("list_invitations", { user }),
165 acceptInvitation: (user, id) => call("accept_invitation", { user, id, surface: "web" }),
166 declineInvitation: (user, id) => call("decline_invitation", { user, id, surface: "web" }),
167 findPeople: (query, limit) => call("find_people", { query, limit: limit ?? null }),
168 workspaceInvites: (slug, viewer) => call("workspace_invites", { slug, viewer }),
169 revokeWorkspaceInvite: (actor, slug, id) => call("revoke_workspace_invite", { actor, slug, id }),
170 resendWorkspaceInvite: (actor, slug, id) => call("resend_workspace_invite", { actor, slug, id }),
171 requestAccess: (email, about, client) => call("request_access", { email, about, client: client ?? null }),
172 userForGitCredentials: (username, secret) =>
173 call("user_for_git_credentials", { username, secret }),
174 userForAccessToken: (token) => call("user_for_access_token", { token }),
175 userForSshKey: (fingerprint) => call("user_for_ssh_key", { fingerprint }),
176 userByUsername: (username) => call("user_by_username", { username }),
177 usernames: (ids) => call("usernames", { ids }),
178 usersForAudience: (ids) => call("users_for_audience", { ids }),
179 profile: (username) => call("profile", { username }),
180 updateProfile: (actor, fields) => call("update_profile", { actor, ...fields }),
181 profileWorkspaces: (username, viewer, publicIn) =>
182 call("profile_workspaces", { username, viewer, public: publicIn }),
183 dockPins: (user, workspace) => call("dock_pins", { user, workspace }),
184 setDockPins: (user, workspace, apps) => call("set_dock_pins", { user, workspace, apps }),
185 soundSettings: (user) => call("sound_settings", { user }),
186 setSoundSettings: (user, change) => call("set_sound_settings", { user, change }),
187 listSshKeys: (user) => call("list_ssh_keys", { user }),
188 addSshKey: (user, title, publicKey) =>
189 call("add_ssh_key", { user, title, publicKey }),
190 removeSshKey: (user, id) => call("remove_ssh_key", { user, id }),
191 listAccessTokens: (user) => call("list_access_tokens", { user }),
192 createAccessToken: (user, name, ttlSeconds, grant) =>
193 call("create_access_token", {
194 user,
195 name,
196 ttlSeconds,
197 scopes: grant?.scopes ?? null,
198 listed: grant?.listed ?? false,
199 }),
200 createAgentToken: (onBehalfOf, scope, ttlSeconds) =>
201 call("create_agent_token", { onBehalfOf, scope, ttlSeconds }),
202 removeAccessToken: (user, id) => call("remove_access_token", { user, id }),
203 createRunCredential: (input) => call("create_run_credential", input),
204 bindRunCredentials: (tokenHashes, runId) => call("bind_run_credentials", { tokenHashes, runId }),
205 revokeRunCredentials: (target) => call("revoke_run_credentials", target),
206 // Who has access to a repository; see access.ts.
207 repoAccess: (owner, name, viewer) => call("repo_access", { viewer, path: { namespace: owner, name } }),
208 addCollaborator: (actor, owner, name, invitee, role) =>
209 call("add_collaborator", { actor, path: { namespace: owner, name }, invitee, role }),
210 setCollaboratorRole: (actor, owner, name, username, role) =>
211 call("set_collaborator_role", { actor, path: { namespace: owner, name }, username, role }),
212 removeCollaborator: (actor, owner, name, username) =>
213 call("remove_collaborator", { actor, path: { namespace: owner, name }, username }),
214 collaboratorPermission: (viewer, owner, name, username) =>
215 call("collaborator_permission", { viewer, path: { namespace: owner, name }, username }),
216 myRepoInvitations: (user) => call("my_repo_invitations", { user }),
217 respondRepoInvitation: (user, id, accept) => call("respond_repo_invitation", { user, id, accept }),
218 revokeRepoInvitation: (actor, owner, name, id) =>
219 call("revoke_repo_invitation", { actor, path: { namespace: owner, name }, id }),
220 setBasePermission: (actor, slug, base) => call("set_base_permission", { actor, slug, base_permission: base }),
221 outsideCollaborators: (viewer, slug) => call("outside_collaborators", { viewer, slug }),
222 // A repository's deploy keys; see deploy-keys.ts.
223 listDeployKeys: (viewer, owner, name) => call("list_deploy_keys", { viewer, path: { namespace: owner, name } }),
224 addDeployKey: (actor, owner, name, key) =>
225 call("add_deploy_key", { actor, path: { namespace: owner, name }, title: key.title, key: key.key, read_only: key.readOnly }),
226 removeDeployKey: (actor, owner, name, id) => call("remove_deploy_key", { actor, path: { namespace: owner, name }, id }),
227 // Teams; see teams.ts.
228 listTeams: (viewer, workspace, query) => call("list_teams", { viewer, workspace, query: query ?? null }),
229 getTeam: (viewer, workspace, team) => call("get_team", { viewer, workspace, team }),
230 createTeam: (actor, workspace, team) => call("create_team", { actor, workspace, ...team }),
231 setTeamCreation: (actor, slug, setting) => call("set_team_creation", { actor, slug, team_creation: setting }),
232 updateTeam: (actor, workspace, team, changes) => call("update_team", { actor, workspace, team, ...changes }),
233 deleteTeam: (actor, workspace, team) => call("delete_team", { actor, workspace, team }),
234 teamMembers: (viewer, workspace, team, includeChildTeams) =>
235 call("team_members", { viewer, workspace, team, include_child_teams: includeChildTeams ?? false }),
236 setTeamMember: (actor, workspace, team, username, role) =>
237 call("set_team_member", { actor, workspace, team, username, role }),
238 removeTeamMember: (actor, workspace, team, username) =>
239 call("remove_team_member", { actor, workspace, team, username }),
240 childTeams: (viewer, workspace, team) => call("child_teams", { viewer, workspace, team }),
241 teamRepos: (viewer, workspace, team) => call("team_repos", { viewer, workspace, team }),
242 setTeamRepo: (actor, workspace, team, owner, name, role) =>
243 call("set_team_repo", { actor, workspace, team, repo: { namespace: owner, name }, role }),
244 removeTeamRepo: (actor, workspace, team, owner, name) =>
245 call("remove_team_repo", { actor, workspace, team, repo: { namespace: owner, name } }),
246 userTeams: (viewer, workspace, username) => call("user_teams", { viewer, workspace, username }),
247 teamMemberships: (viewer, workspace) => call("team_memberships", { viewer, workspace }),
248 teamAgents: (viewer, workspace, team) => call("team_agents", { viewer, workspace, team }),
249 setTeamAgent: (actor, workspace, team, agentId) => call("set_team_agent", { actor, workspace, team, agent_id: agentId }),
250 removeTeamAgent: (actor, workspace, team, agentId) => call("remove_team_agent", { actor, workspace, team, agent_id: agentId }),
251 agentTeams: (workspace, agentId) => call("agent_teams", { workspace, agent_id: agentId }),
252 teamAgentIndex: (workspace) => call("team_agent_index", { workspace }),
253 adoptAgentTeams: (workspaceId, agents) => call("adopt_agent_teams", { workspace_id: workspaceId, agents }),
254 // People; see people.ts.
255 peopleDirectory: (viewer, workspace) => call("people_directory", { viewer, workspace }),
256 setMemberProfile: (actor, workspace, username, changes) => call("set_member_profile", { actor, workspace, username, ...changes }),
257 };
258}
259
260/**
261 * The slug a renamed workspace has now, for a `workspace.renamed` handler:
262 * asked of identity by the workspace's id, so that renames delivered twice
263 * or out of order converge. Falls back to the event's `to`.
264 */
265export async function currentWorkspaceSlug(
266 identity: ServiceBinding,
267 renamed: { workspaceId: string; to: string },
268): Promise<string> {
269 const names = await identityClient(identity).usernames([renamed.workspaceId]);
270 return names[renamed.workspaceId] ?? renamed.to;
271}
272
273/** The slugs whose rows move to `current`: the two a rename names, less `current`. */
274export function staleSlugs(renamed: { from: string; to: string }, current: string): string[] {
275 return [...new Set([renamed.from, renamed.to])].filter((slug) => slug !== current);
276}
277
278/**
279 * Where a transferred repository is now, as `namespace/name`, for a
280 * `repo.transferred` handler: asked of repos by id, so transfers delivered
281 * twice or out of order converge. Falls back to the event's destination.
282 */
283export async function currentRepoPath(
284 repos: ServiceBinding,
285 transferred: { repoId: string; name: string; to: string },
286): Promise<string> {
287 const path = await rpc<{ namespace: string; name: string } | null>(repos, "path_by_id", { id: transferred.repoId });
288 return path ? `${path.namespace}/${path.name}` : `${transferred.to}/${transferred.name}`;
289}
290
291/** The paths whose rows move to `current`: the two a transfer names, less `current`. */
292export function stalePaths(transferred: { name: string; from: string; to: string }, current: string): string[] {
293 return [...new Set([transferred.from, transferred.to].map((ns) => `${ns}/${transferred.name}`))].filter(
294 (path) => path !== current,
295 );
296}
297
298/**
299 * A repository's path change, from `repo.transferred` or `repo.renamed`,
300 * read the same way: the two paths (`namespace/name`) the event names, old
301 * then new. Null for any other event.
302 */
303export type RepoMove = { repoId: string; paths: [string, string] };
304
305export function repoMove(event: { type: string; data: unknown }): RepoMove | null {
306 const data = event.data as Record<string, string>;
307 if (event.type === "repo.transferred") {
308 return { repoId: data.repoId!, paths: [`${data.from}/${data.name}`, `${data.to}/${data.name}`] };
309 }
310 if (event.type === "repo.renamed") {
311 return { repoId: data.repoId!, paths: [`${data.namespace}/${data.from}`, `${data.namespace}/${data.to}`] };
312 }
313 return null;
314}
315
316/**
317 * Where a moved repository is now, as `namespace/name`: asked of repos by
318 * id, so moves delivered twice or out of order converge. Falls back to the
319 * event's new path.
320 */
321export async function currentMovedPath(repos: ServiceBinding, move: RepoMove): Promise<string> {
322 const path = await rpc<{ namespace: string; name: string } | null>(repos, "path_by_id", { id: move.repoId });
323 return path ? `${path.namespace}/${path.name}` : move.paths[1];
324}
325
326/** The paths whose rows move to `current`: the two a move names, less `current`. */
327export function staleMovedPaths(move: RepoMove, current: string): string[] {
328 return [...new Set(move.paths)].filter((path) => path !== current);
329}
330
331/** Staff-only identity. Only sudo binds to it; see `IdentityAdminApi`. */
332export function identityAdminClient(service: ServiceBinding): IdentityAdminApi {
333 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
334 return {
335 workspaces: (query) => call("admin_workspaces", { query: query ?? null }),
336 workspace: (slug) => call("admin_workspace", { slug }),
337 waitlist: (query, status) => call("admin_waitlist", { query: query ?? null, status: status ?? null }),
338 waitlistPending: () => call("admin_waitlist_pending", {}),
339 decideWaitlist: (id, approve, staff, note) => call("admin_decide_waitlist", { id, approve, staff, note: note ?? null }),
340 invites: (query) => call("admin_invites", { query: query ?? null }),
341 revokeInvite: (id, staff) => call("admin_revoke_invite", { id, staff }),
342 mintInvite: (email, staff) => call("admin_mint_invite", { email, staff }),
343 grantInvites: (target, name, amount, note, staff) =>
344 call("admin_grant_invites", { target, name, amount, note, staff }),
345 inviteTree: (username) => call("admin_invite_tree", { username }),
346 workspaceInvites: (slug) => call("admin_workspace_invites", { slug }),
347 sharedInvites: () => call("admin_shared_invites", {}),
348 createSharedInvite: (link, staff) =>
349 call("admin_create_shared_invite", {
350 label: link.label,
351 max_uses: link.maxUses,
352 expires_on: link.expiresOn,
353 domains: link.domains,
354 staff,
355 }),
356 revokeSharedInvite: (id, staff) => call("admin_revoke_shared_invite", { id, staff }),
357 deletedWorkspaces: () => call("admin_deleted_workspaces", {}),
358 restoreWorkspace: (workspaceId, staff) => call("admin_restore_workspace", { workspaceId, staff }),
359 purgeWorkspace: (workspaceId, staff, confirm) => call("admin_purge_workspace", { workspaceId, staff, confirm }),
360 aliases: () => call("admin_aliases", {}),
361 setAlias: (alias, workspace, note, staff) => call("admin_set_alias", { alias, workspace, note, staff }),
362 removeAlias: (alias, reason, staff) => call("admin_remove_alias", { alias, reason, staff }),
363 };
364}
365
366export function reposClient(service: ServiceBinding): ReposApi {
367 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
368 return {
369 get: (path, viewer) => call("get", { path, viewer }),
370 getById: (id, viewer) => call("get_by_id", { id, viewer }),
371 readable: (ids, viewer) => call("readable", { ids, viewer }),
372 publicNamespaces: (ownerId) => call("public_namespaces", { ownerId }),
373 list: (viewer, options = {}) => call("list", { viewer, ...options }),
374 create: (owner, input) => call("create", { owner, ...input }),
375 update: (actor, path, changes) => call("update", { actor, path, ...changes }),
376 transfer: (actor, path, to) => call("transfer", { actor, path, to }),
377 delete: (actor, path, confirm) => call("delete", { actor, path, confirm }),
378 deleted: (viewer, namespace) => call("deleted", { viewer, namespace }),
379 restore: (actor, path) => call("restore", { actor, path }),
380 purge: (actor, path, confirm) => call("purge", { actor, path, confirm }),
381 rename: (actor, path, name) => call("rename", { actor, path, name }),
382 archive: (actor, path, archived) => call("archive", { actor, path, archived }),
383 setVisibility: (actor, path, isPrivate, confirm) => call("set_visibility", { actor, path, isPrivate, confirm }),
384 setDefaultBranch: (actor, path, branch) => call("set_default_branch", { actor, path, branch }),
385 renameBranch: (actor, path, from, to) => call("rename_branch", { actor, path, from, to }),
386 resolveBranch: (repoId, branch) => call("resolve_branch", { repoId, branch }),
387 statusById: (id) => call("status_by_id", { id }),
388 storageOptions: () => call("storage_options", {}),
389 resolvePath: (path) => call("resolve_path", { path }),
390 tree: (path, viewer, ref, treePath) =>
391 call("tree", { path, viewer, ref, treePath }),
392 blob: (path, viewer, ref, filePath) =>
393 call("blob", { path, viewer, ref, filePath }),
394 log: (path, viewer, ref, limit) => call("log", { path, viewer, ref, limit }),
395 blame: (path, viewer, ref, filePath) => call("blame", { path, viewer, ref, filePath }),
396 forkForPull: (sourceId, pullId, actor) =>
397 call("fork_for_pull", { sourceId, pullId, actor }),
398 gitAccess: (path, viewer, service) =>
399 call("git_access", { path, viewer, service }),
400 branches: (path, viewer) => call("branches", { path, viewer }),
401 lastCommits: (path, viewer, ref, treePath) => call("last_commits", { path, viewer, ref, treePath }),
402 branchDrift: (path, viewer, base, heads) => call("branch_drift", { path, viewer, base, heads }),
403 tags: (path, viewer) => call("tags", { path, viewer }),
404 about: (path, viewer) => call("about", { path, viewer }),
405 languages: (path, viewer) => call("languages", { path, viewer }),
406 contributors: (path, viewer) => call("contributors", { path, viewer }),
407 license: (path, viewer) => call("license", { path, viewer }),
408 stars: (path, viewer) => call("stars", { path, viewer }),
409 star: (actor, path, starred) => call("star", { path, actor, starred }),
410 stargazers: (path, viewer, page) => call("stargazers", { path, viewer, page: page ?? null }),
411 starred: (username, viewer) => call("starred", { username, viewer }),
412 releases: (path, viewer) => call("releases", { path, viewer }),
413 release: (path, viewer, which) => call("release", { path, viewer, id: which.id ?? null, tag: which.tag ?? null, latest: which.latest ?? false }),
414 createRelease: (actor, path, release) => call("create_release", { path, actor, ...release }),
415 updateRelease: (actor, path, id, change) => call("update_release", { path, actor, id, ...change }),
416 deleteRelease: (actor, path, id) => call("delete_release", { path, actor, id }),
417 listFiles: (repoId, ref, limit) => call("list_files", { repoId, ref, skipDirs: [], limit }),
418 rawBlobs: (repoId, hashes, maxBytes) => call("raw_blobs", { repoId, hashes, maxBytes }),
419 rawFile: (repoId, ref, path, maxBytes) => call("raw_file", { repoId, ref, path, maxBytes }),
420 commitFile: (repo, actor, file) => call("commit_file", { repo, actor, ...file }),
421 land: (sourceId, actor, branch) => call("land", { sourceId, actor, branch }),
422 compare: (repoId, viewer, base, head, baseBranch) => call("compare", { repoId, viewer, base, head, baseBranch }),
423 claimBackups: (limit, maxRunning) => call("claim_backups", { limit, maxRunning }),
424 // The sandbox's own calls are snake_case (they come through the API).
425 failBackup: (jobId, token, error) => call("backup_fail", { job_id: jobId, token, error }),
426 };
427}
428
429/**
430 * `PullDetail`'s own fields that the work service writes in snake_case:
431 * `g1t_contracts::work::PullDetail` has no camelCase renaming, though what
432 * it holds (`Pull`, `CheckRun` and the rest) does. Each with its name here.
433 */
434const PULL_DETAIL_FIELDS = [
435 ["review_pending", "reviewPending"],
436 ["earlier_checks", "earlierChecks"],
437 ["required_checks", "requiredChecks"],
438 ["code_owners", "codeOwners"],
439] as const;
440
441/**
442 * A pull request's detail as the work service sent it, with its own fields
443 * in the camelCase this package uses: read at the edge, by `workClient`'s
444 * `getPull`. Either spelling is taken, so a service that already sends
445 * camelCase reads the same.
446 */
447export function pullDetailFromWire(raw: Record<string, unknown>): PullDetail {
448 const detail: Record<string, unknown> = { ...raw };
449 for (const [snake, camel] of PULL_DETAIL_FIELDS) {
450 if (snake in detail) {
451 if (!(camel in detail)) detail[camel] = detail[snake];
452 delete detail[snake];
453 }
454 }
455 if (typeof detail.reviewPending !== "boolean") detail.reviewPending = false;
456 return detail as PullDetail;
457}
458
459export function workClient(service: ServiceBinding): WorkApi {
460 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
461 return {
462 openIssue: (actor, repo, input) => call("open_issue", { actor, repo, ...input }),
463 delegateIssue: (actor, repo, input) => call("delegate_issue", { actor, repo, ...input }),
464 listIssues: (repo, viewer, filter = {}) => call("list_issues", { repo, viewer, ...filter }),
465 getIssue: (repo, number, viewer) => call("get_issue", { repo, number, viewer }),
466 updateIssue: (actor, repo, number, input) =>
467 call("update_issue", { actor, repo, number, ...input }),
468 closeIssue: (actor, repo, number, reason) =>
469 call("close_issue", { actor, repo, number, reason }),
470 reopenIssue: (actor, repo, number) => call("reopen_issue", { actor, repo, number }),
471 listLabels: (repo, viewer) => call("list_labels", { repo, viewer }),
472 saveLabel: (actor, repo, label) => call("save_label", { actor, repo, ...label }),
473 deleteLabel: (actor, repo, name) => call("delete_label", { actor, repo, name }),
474 addDefaultLabels: (actor, repo) => call("add_default_labels", { actor, repo }),
475 setLabels: (actor, repo, number, labels, change = "set") =>
476 call("set_labels", { actor, repo, number, labels, change }),
477 listMilestones: (repo, viewer, state) => call("list_milestones", { repo, viewer, state }),
478 getMilestone: (repo, number, viewer) => call("get_milestone", { repo, number, viewer }),
479 saveMilestone: (actor, repo, milestone) => call("save_milestone", { actor, repo, ...milestone }),
480 deleteMilestone: (actor, repo, number) => call("delete_milestone", { actor, repo, number }),
481 counts: (repo, viewer) => call("counts", { repo, viewer }),
482 addComment: (actor, repo, number, comment) =>
483 call("add_comment", { actor, repo, number, ...comment }),
484 editComment: (actor, repo, commentId, body) => call("edit_comment", { actor, repo, commentId, body }),
485 deleteComment: (actor, repo, commentId) => call("delete_comment", { actor, repo, commentId }),
486 workspaceAgentComment: (repo, number, agent, actingFor, body) =>
487 call("workspace_agent_comment", { repo, number, agent, acting_for: actingFor, body }),
488 workspaceAgentReview: (repo, number, agent, actingFor, verdict, body) =>
489 call("workspace_agent_review", { repo, number, agent, acting_for: actingFor, verdict, body }),
490 startChecks: (pullId) => call("start_checks", { pullId }),
491 reportChecks: (runId, token, report) =>
492 call("report_checks", { runId, token, ...report }),
493 startReview: (pullId) => call("start_review", { pullId }),
494 failReview: (runId, token, error) => call("report_review", { runId, token, error }),
495 startMergecheck: (pullId) => call("start_mergecheck", { pullId }),
496 failMergecheck: (pullId, token, error) => call("report_mergecheck", { pullId, token, error }),
497 advance: (pullId) => call("advance", { pullId }),
498 stall: (pullId, reason) => call("stall", { pullId, reason }),
499 managedPulls: (repoId) => call("managed_pulls", { repoId }),
500 queue: (repo, viewer) => call("queue", { repo, viewer }),
501 queueBuild: (repoId) => call("queue_build", { repoId }),
502 failQueue: (entryId, token, error) => call("report_queue", { entryId, token, error }),
503 removeFromQueue: (actor, repo, number) => call("remove_from_queue", { actor, repo, number }),
504 messageAgent: (actor, repo, number, body) => call("message_agent", { actor, repo, number, body }),
505 catchUpJob: (pullId) => call("catch_up_job", { pullId }),
506 wakeForMessages: (pullId) => call("wake_for_messages", { pullId }),
507 getSettings: (repo, viewer) => call("get_settings", { repo, viewer }),
508 // Rulesets travel in snake_case (rules.ts).
509 listRulesets: (owner, viewer, includeParents = false) =>
510 call("list_rulesets", { viewer, ...owner, include_parents: includeParents }),
511 getRuleset: (owner, id, viewer) => call("get_ruleset", { viewer, ...owner, id }),
512 saveRuleset: (actor, owner, ruleset, id) => call("save_ruleset", { actor, ...owner, id: id ?? null, ruleset }),
513 deleteRuleset: (actor, owner, id) => call("delete_ruleset", { actor, ...owner, id }),
514 effectiveRules: (repo, name, viewer, target = "branch") => call("effective_rules", { viewer, repo, name, target }),
515 ruleEvaluations: (owner, viewer, filter = {}) => call("rule_evaluations", { viewer, ...owner, ...filter }),
516 seenChecks: (repo, viewer) => call("seen_checks", { repo, viewer }),
517 commitChecks: (repo, viewer, shas) => call("commit_checks", { repo, viewer, shas }),
518 getCheckRun: (repo, id, viewer) => call("get_check_run", { repo, id, viewer }),
519 checkRunAnnotations: (repo, id, viewer) => call("check_run_annotations", { repo, id, viewer }),
520 requestCheckAction: (actor, repo, id, identifier) => call("request_check_action", { actor, repo, id, identifier }),
521 rerequestCheckRun: (actor, repo, id) => call("rerequest_check_run", { actor, repo, id }),
522 codeownersErrors: (repo, viewer, ref) => call("codeowners_errors", { repo, viewer, ref: ref ?? null }),
523 updateSettings: (actor, repo, settings) =>
524 call("update_settings", { actor, repo, settings }),
525 openPull: (actor, repo, input) => call("open_pull", { actor, repo, ...input }),
526 listPulls: (repo, viewer, state, filter = {}) => call("list_pulls", { repo, viewer, state, ...filter }),
527 pullsForRepos: (repoIds, viewer, limit) => call("pulls_for_repos", { repoIds, viewer, limit }),
528 // Its own fields arrive in snake_case: read into camelCase here.
529 getPull: (repo, number, viewer) =>
530 call<Result<Record<string, unknown>>>("get_pull", { repo, number, viewer }).then(
531 (found): Result<PullDetail> => (found.ok ? { ok: true, value: pullDetailFromWire(found.value) } : found),
532 ),
533 updatePull: (actor, repo, number, changes) =>
534 call("update_pull", { actor, repo, number, ...changes }),
535 catchUpPull: (actor, repo, number) => call("catch_up_pull", { actor, repo, number }),
536 readyPull: (actor, repo, number, summary) =>
537 call("ready_pull", { actor, repo, number, summary }),
538 closePull: (actor, repo, number) => call("close_pull", { actor, repo, number }),
539 reopenPull: (actor, repo, number) => call("reopen_pull", { actor, repo, number }),
540 convertPullToDraft: (actor, repo, number) => call("convert_pull_to_draft", { actor, repo, number }),
541 mergePull: (actor, repo, number, options = {}) =>
542 call("merge_pull", { actor, repo, number, ...options }),
543 listActivePulls: (viewer) => call("list_active_pulls", { viewer }),
544 byAuthor: (username, viewer, filter = {}) => call("by_author", { username, viewer, ...filter }),
545 contributions: (username, viewer) => call("contributions", { username, viewer }),
546 startPlan: (actor, repo, brief) => call("start_plan", { actor, repo, brief }),
547 failPlan: (planId, token, error) => call("report_plan", { planId, token, error }),
548 getPlan: (repo, viewer, id) => call("get_plan", { repo, viewer, id }),
549 listPlans: (repo, viewer) => call("list_plans", { repo, viewer }),
550 applyPlan: (actor, repo, id, options = {}) =>
551 call("apply_plan", { actor, repo, id, ...options }),
552 queueIssue: (actor, repo, number, queued) =>
553 call("queue_issue", { actor, repo, number, queued }),
554 readyIssues: (repoId) => call("ready_issues", { repoId }),
555 listAssignedIssues: (viewer) => call("list_assigned_issues", { viewer }),
556 appendSession: (actor, repo, number, entries) =>
557 call("append_session", { actor, repo, number, entries }),
558 readSession: (repo, number, viewer, afterSeq = 0) =>
559 call("read_session", { repo, number, viewer, afterSeq }),
560 };
561}
562
563export function billingClient(service: ServiceBinding): BillingApi {
564 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
565 return {
566 status: () => call("status", {}),
567 account: (workspace, viewer) => call("account", { workspace, viewer }),
568 ledger: (workspace, viewer) => call("ledger", { workspace, viewer }),
569 credits: (workspace, viewer) => call("credits", { workspace, viewer }),
570 statement: (workspace, viewer, month = null, group = "day") => call("statement", { workspace, viewer, month, group }),
571 statementEntries: (workspace, viewer, filter) =>
572 call("statement_entries", {
573 workspace,
574 viewer,
575 month: filter.month,
576 kind: filter.kind,
577 day: filter.day ?? null,
578 project: filter.project ?? null,
579 before: filter.before ?? null,
580 }),
581 usage: (workspace, viewer, since) => call("usage", { workspace, viewer, since }),
582 tokenUsage: (workspace, viewer, options = {}) =>
583 call("token_usage", { workspace, viewer, person: options.person ?? null, days: options.days ?? null }),
584 recordTokens: (usage) => call("record_tokens", usage),
585 gatewayModels: () => call("gateway_models", {}),
586 modelDefaults: () => call("model_defaults", {}),
587 recordDiscovery: (provider, models, by, error = null) => call("record_discovery", { provider, models, by, error }),
588 gatewayAdmit: (workspace) => call("gateway_admit", { workspace }),
589 recordGateway: (record) => call("record_gateway", record),
590 gatewayRequests: (workspace, viewer, options = {}) =>
591 call("gateway_requests", { workspace, viewer, limit: options.limit ?? null, before: options.before ?? null }),
592 checkout: (actor, workspace, amountCents, returnUrl, method = "card") =>
593 call("checkout", { actor, workspace, amountCents, returnUrl, method }),
594 confirm: (workspace, viewer, session) => call("confirm", { workspace, viewer, session }),
595 canStart: (workspace) => call("can_start", { workspace }),
596 trial: (workspace, exempt) => call("trial", { workspace, exempt }),
597 startRun: (run) => call("start_run", run),
598 features: (workspace, viewer) => call("features", { workspace, viewer }),
599 subscribe: (actor, workspace, feature, returnUrl) =>
600 call("subscribe", { actor, workspace, feature, returnUrl }),
601 confirmSubscription: (workspace, viewer, session) =>
602 call("confirm_subscription", { workspace, viewer, session }),
603 cancelSubscription: (actor, workspace, feature, resume = false) =>
604 call("cancel_subscription", { actor, workspace, feature, resume }),
605 hasFeature: (workspace, feature) => call("has_feature", { workspace, feature }),
606 freeWorkspaces: (workspaces) => call("free_workspaces", { workspaces }),
607 chargeFeature: (charge) => call("charge_feature", charge),
608 billingPortal: (actor, workspace, returnUrl) => call("billing_portal", { actor, workspace, return_url: returnUrl }),
609 recordSandbox: (usage) => call("record_sandbox", usage),
610 limit: (workspace, viewer) => call("limit", { workspace, viewer }),
611 checkLimit: (workspace) => call("check_limit", { workspace }),
612 prices: () => call("prices", {}),
613 notePending: (workspace, source, costMicros, detail = null) => call("note_pending", { workspace, source, costMicros, detail }),
614 usageMeters: (workspace, viewer) => call("usage_meters", { workspace, viewer }),
615 setSpendLimit: (actor, workspace, spendLimitMicros, useFullLimit = false, raiseOnce = false) =>
616 call("set_spend_limit", { actor, workspace, spendLimitMicros, use_full_limit: useFullLimit, raise_once: raiseOnce }),
617 invoices: (workspace, viewer) => call("invoices", { workspace, viewer }),
618 entitlements: (workspace) => call("entitlements", { workspace }),
619 reserve: (reservation) => call("reserve", reservation),
620 settle: (reservationId, actualMicros) => call("settle", { reservationId, actualMicros }),
621 cardCheck: (actor, workspace, returnUrl) => call("card_check", { actor, workspace, returnUrl }),
622 confirmCardCheck: (workspace, viewer, session) => call("confirm_card_check", { workspace, viewer, session }),
623 requestLimit: (actor, workspace, request) => call("request_limit", { actor, workspace, ...request }),
624 limitRequests: (workspace, viewer) => call("limit_requests", { workspace, viewer }),
625 confirmSpike: (actor, workspace, keepGoing) => call("confirm_spike", { actor, workspace, keepGoing }),
626 setCaps: (actor, workspace, caps) => call("set_caps", { actor, workspace, ...caps }),
627 usageReport: (workspace, viewer, range) =>
628 call("usage_report", { workspace, viewer, from: range.from, until: range.until, products: range.products ?? [], projects: range.projects ?? [] }),
629 aiCredit: (workspace, viewer) => call("ai_credit", { workspace, viewer }),
630 buyAiCredit: (actor, workspace, amountCents, returnUrl) => call("buy_ai_credit", { actor, workspace, amountCents, returnUrl }),
631 confirmAiCredit: (workspace, viewer, session) => call("confirm_ai_credit", { workspace, viewer, session }),
632 setAiReload: (actor, workspace, reload) => call("set_ai_reload", { actor, workspace, ...reload }),
633 setBudget: (actor, workspace, budget) => call("set_budget", { actor, workspace, ...budget }),
634 billingDetails: (workspace, viewer) => call("billing_details", { workspace, viewer }),
635 setBillingDetails: (actor, workspace, details) => call("set_billing_details", { actor, workspace, ...details }),
636 };
637}
638
639export function billingAdminClient(service: ServiceBinding): BillingAdminApi {
640 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
641 return {
642 accounts: (query) => call("admin_accounts", { query: query ?? null }),
643 account: (id) => call("admin_account", { id }),
644 setTerms: (id, terms, by) => call("admin_set_terms", { id, terms, by }),
645 setAllowances: (id, allowances, note, by) => call("admin_set_allowances", { id, allowances, note, by }),
646 createEnterprise: (name, workspaces, by) => call("admin_create_enterprise", { name, workspaces, by }),
647 attach: (workspace, account, by) => call("admin_attach", { workspace, account, by }),
648 credit: (workspace, amountMicros, note, by, options = { kind: "goodwill" }) =>
649 call("admin_credit", {
650 workspace,
651 amount_micros: amountMicros,
652 note,
653 by,
654 kind: options.kind,
655 expires_at: options.expiresAt ?? null,
656 refund_for: options.refundFor ?? null,
657 refund_day: options.refundDay ?? null,
658 }),
659 credits: (filter = {}) =>
660 call("admin_credits", {
661 workspace: filter.workspace ?? null,
662 kind: filter.kind ?? null,
663 month: filter.month ?? null,
664 by: filter.by ?? null,
665 }),
666 revokeCredit: (id, note, by) => call("admin_revoke_credit", { id, note, by }),
667 resetBilling: (workspace, confirm, note, by) => call("admin_reset_billing", { workspace, confirm, note, by }),
668 billingLink: (workspace, by) => call("admin_billing_link", { workspace, by }),
669 stripe: (fix = false, by) => call("admin_stripe", { fix, by: by ?? null }),
670 enterpriseBilling: (id, email, by) => call("admin_enterprise_billing", { id, email, by }),
671 enterpriseAddress: (id, address, taxIdType, taxId, by) => call("admin_enterprise_address", { id, address, taxIdType, taxId, by }),
672 invoiceEnterprise: (id, by) => call("admin_invoice_enterprise", { id, by }),
673 accountsFor: (workspaces) => call("admin_accounts", { query: null, workspaces }),
674 signals: () => call("admin_signals", {}),
675 overview: () => call("admin_overview", {}),
676 sales: (workspace) => call("admin_sales", { workspace }),
677 setSales: (workspace, record, by) =>
678 call("admin_set_sales", {
679 workspace,
680 stage: record.stage,
681 owner: record.owner ?? null,
682 next_step: record.nextStep ?? null,
683 next_at: record.nextAt ?? null,
684 by,
685 }),
686 addNote: (workspace, text, by) => call("admin_add_note", { workspace, text, by }),
687 workspaceInvoices: (workspace) => call("admin_workspace_invoices", { workspace }),
688 allInvoices: (filter = {}) => call("admin_invoices", { status: filter.status ?? null, month: filter.month ?? null }),
689 audit: (filter = {}) =>
690 call("admin_audit", { by: filter.by ?? null, action: filter.action ?? null, before: filter.before ?? null }),
691 limitRequests: (status = "open") => call("admin_limit_requests", { status }),
692 decideLimitRequest: (id, decision, amountMicros, note, by) =>
693 call("admin_decide_limit_request", { id, decision, amount_micros: amountMicros, note, by }),
694 overages: () => call("admin_overages", {}),
695 goodwill: (workspace, amountMicros, reason, by, day = null) =>
696 call("admin_goodwill", { workspace, amount_micros: amountMicros, reason, by, day }),
697 velocity: () => call("admin_velocity", {}),
698 recordPayment: (workspace, amountMicros, reference, note, by) =>
699 call("admin_record_payment", { workspace, amount_micros: amountMicros, reference, note, by }),
700 costs: (days) => call("admin_costs", { days: days ?? null }),
701 costAlerts: () => call("admin_cost_alerts", {}),
702 spendCaps: () => call("admin_spend_caps", {}),
703 liftBreaker: (note, by) => call("admin_lift_breaker", { note, by }),
704 platformGuard: () => call("admin_platform_guard", {}),
705 setPause: (level, paused, note, by) => call("admin_set_pause", { level, paused, note, by }),
706 decideProposal: (id, decision, note, by) => call("admin_decide_proposal", { id, decision, note, by }),
707 setCostSettings: (settings, by) => call("admin_set_cost_settings", { settings, by }),
708 setCostMapping: (mapping, by) =>
709 call("admin_set_cost_mapping", {
710 product: mapping.product,
711 meter: mapping.meter,
712 bucket: mapping.bucket ?? "",
713 price_meter: mapping.priceMeter ?? null,
714 own_meter: mapping.ownMeter ?? null,
715 scale_to_own: mapping.scaleToOwn ?? false,
716 drift_percent: mapping.driftPercent ?? null,
717 note: mapping.note ?? "",
718 remove: mapping.remove ?? false,
719 by,
720 }),
721 runCosts: (by) => call("admin_run_costs", { by }),
722 models: () => call("admin_models", {}),
723 decideModel: (model, decision, details, reason, by) =>
724 call("admin_decide_model", {
725 model,
726 decision,
727 name: details.name ?? null,
728 tier_hint: details.tierHint ?? null,
729 prices: details.prices
730 ? {
731 input_micros: details.prices.inputMicros,
732 output_micros: details.prices.outputMicros,
733 cache_read_micros: details.prices.cacheReadMicros,
734 cache_write_micros: details.prices.cacheWriteMicros,
735 cache_write_1h_micros: details.prices.cacheWrite1hMicros,
736 threshold: details.prices.threshold,
737 over_input_micros: details.prices.overInputMicros,
738 over_output_micros: details.prices.overOutputMicros,
739 over_cache_read_micros: details.prices.overCacheReadMicros,
740 over_cache_write_micros: details.prices.overCacheWriteMicros,
741 over_cache_write_1h_micros: details.prices.overCacheWrite1hMicros,
742 }
743 : null,
744 reason,
745 by,
746 }),
747 setModelDefault: (purpose, value, reason, by) =>
748 call("admin_set_model_default", {
749 purpose,
750 model: value.model ?? null,
751 tier: value.tier ?? null,
752 effort: value.effort ?? null,
753 reason,
754 by,
755 }),
756 };
757}
758
759export function eventsClient(service: ServiceBinding): EventsApi {
760 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
761 return {
762 publish: (events) => call("publish", { events }),
763 list: (query) => call("list", query),
764 activityDigest: (args) => call("activity_digest", args),
765 };
766}
767
768export function integrationsClient(service: ServiceBinding): IntegrationsApi {
769 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
770 return {
771 list: (workspace, viewer) => call("list", { workspace, viewer }),
772 connect: (actor, workspace, input) => call("connect", { actor, workspace, ...input }),
773 update: (actor, workspace, id, input) => call("update", { actor, workspace, id, ...input }),
774 disconnect: (actor, workspace, id) => call("disconnect", { actor, workspace, id }),
775 test: (actor, workspace, id) => call("test", { actor, workspace, id }),
776 deliveries: (workspace, viewer, id) => call("deliveries", { workspace, viewer, id }),
777 resolve: (workspace, viewer, reference) => call("resolve", { workspace, viewer, reference }),
778 references: (workspace, text, limit) => call("references", { workspace, text, limit }),
779 import: (actor, repo, reference, assign) => call("import", { actor, repo, reference, assign }),
780 comment: (actor, workspace, reference, text, link) => call("comment", { actor, workspace, reference, text, link }),
781 close: (actor, workspace, reference, text, link) => call("close", { actor, workspace, reference, text, link }),
782 links: (repo, number) => call("links", { repo, number }),
783 modelProvider: (workspace) => call("model_provider", { workspace }),
784 openModelSession: (run) => call("open_model_session", run),
785 modelUpstream: (token) => call("model_upstream", { token }),
786 gatewayUpstream: (workspace) => call("gateway_upstream", { workspace }),
787 gatewayProviders: (workspace) => call("gateway_providers", { workspace }),
788 closeModelSessions: (tokenHashes) => call("close_model_sessions", { token_hashes: tokenHashes }),
789 capModelSessions: (tokenHashes, capMicros) => call("cap_model_sessions", { token_hashes: tokenHashes, cap_micros: capMicros }),
790 routes: (workspace, viewer) => call("routes", { workspace, viewer }),
791 setRoutes: (actor, workspace, routes) => call("set_routes", { actor, workspace, routes }),
792 };
793}
794
795export function webhooksClient(service: ServiceBinding): WebhooksApi {
796 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
797 return {
798 list: (viewer, owner) => call("list", { viewer, ...owner }),
799 create: (actor, owner, input) => call("create", { actor, ...owner, ...input }),
800 update: (actor, owner, id, input) => call("update", { actor, ...owner, id, ...input }),
801 delete: (actor, owner, id) => call("delete", { actor, ...owner, id }),
802 ping: (actor, owner, id) => call("ping", { actor, ...owner, id }),
803 deliveries: (viewer, owner, id) => call("deliveries", { viewer, ...owner, id }),
804 redeliver: (actor, owner, deliveryId) => call("redeliver", { actor, ...owner, deliveryId }),
805 };
806}
807
808export function actionsClient(service: ServiceBinding): ActionsApi {
809 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
810 return {
811 workflows: (repo, viewer) => call("workflows", { repo, viewer }),
812 runs: (repo, viewer, filter = {}) => call("runs", { repo, viewer, ...filter }),
813 run: (repo, viewer, id, attempt) => call("run", { repo, viewer, id, attempt: attempt ?? null }),
814 logs: (repo, viewer, job, after = 0) => call("logs", { repo, viewer, job, after }),
815 summaries: (repo, viewer, id, attempt) => call("summaries", { repo, viewer, id, attempt: attempt ?? null }),
816 jobLogText: (repo, viewer, job) => call("job_log_text", { repo, viewer, job }),
817 runLogs: (repo, viewer, id, attempt) => call("run_logs", { repo, viewer, id, attempt: attempt ?? null }),
818 dispatch: (actor, repo, workflow, ref, inputs) => call("dispatch", { actor, repo, workflow, ref, inputs }),
819 cancel: (actor, repo, id, force = false) => call("cancel", { actor, repo, id, force }),
820 rerun: (actor, repo, id, failedOnly = false, options = {}) =>
821 call("rerun", { actor, repo, id, failed_only: failedOnly, job: options.job ?? null, debug: options.debug ?? false }),
822 setWorkflowEnabled: (actor, repo, workflow, enabled) =>
823 call("set_workflow_enabled", { actor, repo, workflow, enabled }),
824 settings: (actor, owner, kind) => call("settings", { actor, ...owner, kind }),
825 setSetting: (actor, owner, kind, name, value, options = {}) =>
826 call("set_setting", { actor, ...owner, kind, name, value, ...options }),
827 deleteSetting: (actor, owner, kind, name, id) => call("delete_setting", { actor, ...owner, kind, name, id }),
828 approveRun: (actor, repo, id) => call("approve_run", { actor, repo, id }),
829 pendingDeployments: (repo, viewer, id) => call("pending_deployments", { repo, viewer, id }),
830 reviewDeployments: (actor, repo, id, state, environments = [], comment) =>
831 call("review_deployments", { actor, repo, id, state, environments, comment: comment ?? null }),
832 actionsSettings: (repo, viewer) => call("actions_settings", { repo, viewer }),
833 setActionsSettings: (actor, repo, change) => call("set_actions_settings", { actor, repo, ...change }),
834 workspaceActionsSettings: (workspace, viewer) => call("workspace_actions_settings", { workspace, viewer }),
835 setWorkspaceActionsSettings: (actor, workspace, change) =>
836 call("set_workspace_actions_settings", { actor, workspace, ...change }),
837 environments: (repo, viewer) => call("environments", { repo, viewer }),
838 setEnvironment: (actor, repo, name, change) => call("set_environment", { actor, repo, name, ...change }),
839 deleteEnvironment: (actor, repo, name) => call("delete_environment", { actor, repo, name }),
840 artifacts: (repo, viewer, filter = {}) => call("artifacts", { repo, viewer, ...filter }),
841 artifactDownload: (repo, viewer, by) => call("artifact_download", { repo, viewer, ...by }),
842 deleteArtifact: (actor, repo, id) => call("delete_artifact", { actor, repo, id }),
843 artifactRetention: (repo, viewer, days) => call("artifact_retention", { repo, viewer, days }),
844 };
845}
846
847
848/** Self-hosted runners, kept by the actions service. */
849export function runnersClient(service: ServiceBinding): RunnersApi {
850 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
851 return {
852 stuck: (viewer) => call("stuck_jobs", { viewer }),
853 activity: (actor, workspace) => call("runner_activity", { actor, workspace }),
854 list: (actor, owner) => call("runners", { actor, ...owner }),
855 createToken: (actor, owner, group) => call("create_registration_token", { actor, ...owner, group }),
856 remove: (actor, owner, id) => call("remove_runner", { actor, ...owner, id }),
857 groups: (actor, workspace) => call("runner_groups", { actor, workspace }),
858 setGroup: (actor, workspace, group) => call("set_runner_group", { actor, workspace, ...group }),
859 deleteGroup: (actor, workspace, id) => call("delete_runner_group", { actor, workspace, id }),
860 settings: (actor, owner) => call("runner_settings", { actor, ...owner }),
861 setSettings: (actor, owner, change) => call("set_runner_settings", { actor, ...owner, ...change }),
862 };
863}
864
865export function deploymentsClient(service: ServiceBinding): DeploymentsApi {
866 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
867 return {
868 settings: (project, viewer) => call("settings", { project, viewer }),
869 updateSettings: (actor, project, changes) => call("update_settings", { actor, project, changes }),
870 list: (project, viewer) => call("list", { project, viewer }),
871 get: (project, id, viewer) => call("get", { project, id, viewer }),
872 redeploy: (actor, project, branch) => call("redeploy", { actor, project, branch }),
873 takeDown: (actor, project, branch) => call("take_down", { actor, project, branch }),
874 overview: (workspace, viewer) => call("overview", { workspace, viewer }),
875 usage: (workspace, viewer) => call("usage", { workspace, viewer }),
876 domains: (project, viewer) => call("domains", { project, viewer }),
877 addDomain: (actor, project, hostname, options = {}) =>
878 call("add_domain", { actor, project, hostname, twin: !!options.twin }),
879 removeDomain: (actor, project, id) => call("remove_domain", { actor, project, id }),
880 refreshDomain: (actor, project, id) => call("refresh_domain", { actor, project, id }),
881 repoDeployments: (repo, viewer, filter = {}) => call("list_deployments", { repo, viewer, ...filter }),
882 repoDeployment: (repo, id, viewer) => call("get_deployment", { repo, id, viewer }),
883 environments: (repo, viewer) => call("list_environments", { repo, viewer }),
884 createDeployment: (actor, repo, input) => call("create_deployment", { repo, actor, ...input }),
885 createDeploymentStatus: (actor, repo, id, input) => call("create_deployment_status", { repo, actor, id, ...input }),
886 };
887}
888
889/** Packages: the registries beside the code (services/packages). */
890export function packagesClient(service: ServiceBinding): PackagesApi {
891 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
892 return {
893 list: (workspace, viewer, filter = {}) =>
894 call("list_packages", {
895 workspace,
896 viewer,
897 ecosystem: filter.ecosystem ?? null,
898 repo_id: filter.repoId ?? null,
899 query: filter.query ?? null,
900 }),
901 get: (workspace, ecosystem, name, viewer) => call("get_package", { workspace, ecosystem, name, viewer }),
902 deleteVersion: (actor, workspace, ecosystem, name, version, surface) =>
903 call("delete_version", { actor, workspace, ecosystem, name, version, surface: surface ?? null }),
904 deletePackage: (actor, workspace, ecosystem, name, surface) =>
905 call("delete_package", { actor, workspace, ecosystem, name, surface: surface ?? null }),
906 set: (actor, workspace, ecosystem, name, change, surface) =>
907 call("set_package", {
908 actor,
909 workspace,
910 ecosystem,
911 name,
912 visibility: change.visibility ?? null,
913 link: change.link ?? null,
914 unlink: change.unlink ?? false,
915 inherit_access: change.inheritAccess ?? null,
916 surface: surface ?? null,
917 }),
918 settings: (workspace, ecosystem, name, viewer) => call("package_settings", { workspace, ecosystem, name, viewer }),
919 deleted: (workspace, viewer) => call("deleted_packages", { workspace, viewer }),
920 restorePackage: (actor, workspace, ecosystem, name, surface) =>
921 call("restore_package", { actor, workspace, ecosystem, name, surface: surface ?? null }),
922 restoreVersion: (actor, workspace, ecosystem, name, version, surface) =>
923 call("restore_version", { actor, workspace, ecosystem, name, version, surface: surface ?? null }),
924 setAccess: (actor, workspace, ecosystem, name, who, role, surface) =>
925 call("set_package_access", {
926 actor,
927 workspace,
928 ecosystem,
929 name,
930 user: "user" in who ? who.user : null,
931 team: "team" in who ? who.team : null,
932 role,
933 surface: surface ?? null,
934 }),
935 removeAccess: (actor, workspace, ecosystem, name, who, surface) =>
936 call("remove_package_access", {
937 actor,
938 workspace,
939 ecosystem,
940 name,
941 user: "user" in who ? who.user : null,
942 team: "team" in who ? who.team : null,
943 surface: surface ?? null,
944 }),
945 setActionsAccess: (actor, workspace, ecosystem, name, repo, role, surface) =>
946 call("set_actions_access", { actor, workspace, ecosystem, name, repo, role, surface: surface ?? null }),
947 removeActionsAccess: (actor, workspace, ecosystem, name, repo, surface) =>
948 call("remove_actions_access", { actor, workspace, ecosystem, name, repo, surface: surface ?? null }),
949 storage: (workspace) => call("storage", { workspace }),
950 storageAll: () => call("storage_all", {}),
951 syncComposer: (repoId) => call("sync_composer", { repo_id: repoId }),
952 };
953}
954
955export function projectsClient(service: ServiceBinding): ProjectsApi {
956 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
957 return {
958 list: (workspace, viewer) => call("list", { workspace, viewer }),
959 get: (workspace, slug, viewer) => call("get", { workspace, slug, viewer }),
960 byRepo: (repoId) => call("by_repo", { repoId }),
961 create: (actor, workspace, input) => call("create", { actor, workspace, input }),
962 update: (actor, workspace, slug, changes) => call("update", { actor, workspace, slug, changes }),
963 deploymentsChanged: (projectId, enabled) => call("deployments_changed", { projectId, enabled }),
964 shortcuts: (workspace, viewer) => call("shortcuts", { workspace, viewer }),
965 pin: (actor, workspace, slug, position) => call("pin", { actor, workspace, slug, position: position ?? null }),
966 unpin: (actor, workspace, slug) => call("unpin", { actor, workspace, slug }),
967 reorderPins: (actor, workspace, slugs) => call("reorder_pins", { actor, workspace, slugs }),
968 visited: (actor, projectId) => call("visited", { actor, projectId }),
969 publicLinks: (repos) => call("public_links", { repos }),
970 };
971}
972
973/**
974 * An agent's own computer, on the runner (services/runner `AgentComputer`):
975 * what the agents service calls for a session's `run_command` and file
976 * tools, and for the Computer tab. Arguments and answers are snake_case.
977 */
978export function runnerComputerClient(service: ServiceBinding): RunnerComputerApi {
979 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
980 return {
981 computerStatus: (agentId) => call("computer_status", { agent_id: agentId }),
982 computerWake: (args) => call("computer_wake", args),
983 computerExec: (args) => call("computer_exec", args),
984 computerReadFile: (args) => call("computer_read_file", args),
985 computerWriteFile: (args) => call("computer_write_file", args),
986 computerSleep: (agentId) => call("computer_sleep", { agent_id: agentId }),
987 computerReset: (agentId) => call("computer_reset", { agent_id: agentId }),
988 computerForget: (agentId) => call("computer_forget", { agent_id: agentId }),
989 computerCommands: (agentId, sessionId) => call("computer_commands", { agent_id: agentId, session_id: sessionId ?? null }),
990 };
991}