g1t/packages/contracts/src/clients.ts

112 lines5,649 bytesCodeBlame

Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.

API and MCP server, Rust identity service, registration, site redesign1import type { IdentityApi } from "./identity";
Rust repos service with shipping; pull requests kept in the model2import type { ReposApi } from "./repos";
Work service in Rust, with RFC 3339 timestamps3import type { WorkApi } from "./work";
API and MCP server, Rust identity service, registration, site redesign4
5/** A service binding, as far as these clients need it. */
6export type ServiceBinding = {
7 fetch(input: string, init?: RequestInit): Promise<Response>;
8};
9
10/**
11 * Calls a method on a service that speaks the JSON protocol used by the
12 * Rust services: `POST /rpc/<method>` with the arguments as the body.
13 */
14async function rpc<T>(
15 service: ServiceBinding,
16 method: string,
17 args: object,
18): Promise<T> {
19 // The hostname is ignored; a service binding always reaches its service.
20 const response = await service.fetch(`https://service/rpc/${method}`, {
21 method: "POST",
22 headers: { "content-type": "application/json" },
23 body: JSON.stringify(args),
24 });
25 if (!response.ok) {
26 throw new Error(`${method} failed with status ${response.status}`);
27 }
28 return response.json();
29}
30
31export function identityClient(service: ServiceBinding): IdentityApi {
32 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
33 return {
34 register: (username, email, password) =>
35 call("register", { username, email, password }),
36 signIn: (username, password) => call("sign_in", { username, password }),
37 signOut: (sessionToken) => call("sign_out", { sessionToken }),
Email verification, password reset, and Git for AI scale positioning38 resendVerification: (user) => call("resend_verification", { user }),
39 verifyEmail: (token) => call("verify_email", { token }),
40 requestPasswordReset: (email) => call("request_password_reset", { email }),
41 resetPassword: (token, password) =>
42 call("reset_password", { token, password }),
Device sign-in replaces registering and minting tokens over the API43 deviceStart: (clientName) => call("device_start", { clientName }),
44 deviceLookup: (userCode) => call("device_lookup", { userCode }),
45 deviceResolve: (userCode, user, approve) =>
46 call("device_resolve", { userCode, user, approve }),
47 deviceClaim: (deviceCode) => call("device_claim", { deviceCode }),
Workspaces own repositories48 createWorkspace: (user, slug, name) => call("create_workspace", { user, slug, name }),
49 getWorkspace: (slug) => call("get_workspace", { slug }),
50 listMembers: (slug, viewer) => call("list_members", { slug, viewer }),
51 addMember: (actor, slug, username) => call("add_member", { actor, slug, username }),
52 removeMember: (actor, slug, username) =>
53 call("remove_member", { actor, slug, username }),
API and MCP server, Rust identity service, registration, site redesign54 userForSession: (sessionToken) => call("user_for_session", { sessionToken }),
55 userForGitCredentials: (username, secret) =>
56 call("user_for_git_credentials", { username, secret }),
57 userForAccessToken: (token) => call("user_for_access_token", { token }),
58 userForSshKey: (fingerprint) => call("user_for_ssh_key", { fingerprint }),
59 userByUsername: (username) => call("user_by_username", { username }),
60 listSshKeys: (user) => call("list_ssh_keys", { user }),
61 addSshKey: (user, title, publicKey) =>
62 call("add_ssh_key", { user, title, publicKey }),
63 removeSshKey: (user, id) => call("remove_ssh_key", { user, id }),
64 listAccessTokens: (user) => call("list_access_tokens", { user }),
Account dropdown, llms.txt onboarding, hosted agent runner (not yet deployed)65 createAccessToken: (user, name, ttlSeconds) =>
66 call("create_access_token", { user, name, ttlSeconds }),
API and MCP server, Rust identity service, registration, site redesign67 removeAccessToken: (user, id) => call("remove_access_token", { user, id }),
68 };
69}
Rust repos service with shipping; pull requests kept in the model70
71export function reposClient(service: ServiceBinding): ReposApi {
72 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
73 return {
74 get: (path, viewer) => call("get", { path, viewer }),
75 getById: (id, viewer) => call("get_by_id", { id, viewer }),
76 list: (viewer, options = {}) => call("list", { viewer, ...options }),
77 create: (owner, input) => call("create", { owner, ...input }),
78 tree: (path, viewer, ref, treePath) =>
79 call("tree", { path, viewer, ref, treePath }),
80 blob: (path, viewer, ref, filePath) =>
81 call("blob", { path, viewer, ref, filePath }),
82 log: (path, viewer, ref, limit) => call("log", { path, viewer, ref, limit }),
83 forkForAttempt: (sourceId, attemptId, actor) =>
84 call("fork_for_attempt", { sourceId, attemptId, actor }),
85 gitAccess: (path, viewer, service) =>
86 call("git_access", { path, viewer, service }),
87 land: (forkId, actor) => call("land", { forkId, actor }),
Diffs on attempts; hosted agent presented as the g1t agent88 compare: (repoId, viewer, base) => call("compare", { repoId, viewer, base }),
Rust repos service with shipping; pull requests kept in the model89 };
90}
Work service in Rust, with RFC 3339 timestamps91
92export function workClient(service: ServiceBinding): WorkApi {
93 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
94 return {
95 openIntent: (actor, repo, input) => call("open_intent", { actor, repo, ...input }),
96 listIntents: (repo, viewer, status) => call("list_intents", { repo, viewer, status }),
97 getIntent: (repo, number, viewer) => call("get_intent", { repo, number, viewer }),
98 withdrawIntent: (actor, intentId) => call("withdraw_intent", { actor, intentId }),
99 startAttempt: (actor, intentId, input) =>
100 call("start_attempt", { actor, intentId, ...input }),
101 getAttempt: (attemptId, viewer) => call("get_attempt", { attemptId, viewer }),
102 submitAttempt: (actor, attemptId, summary) =>
103 call("submit_attempt", { actor, attemptId, summary }),
104 abandonAttempt: (actor, attemptId) => call("abandon_attempt", { actor, attemptId }),
105 shipAttempt: (actor, attemptId) => call("ship_attempt", { actor, attemptId }),
106 listActiveAttempts: (viewer) => call("list_active_attempts", { viewer }),
107 appendSession: (actor, attemptId, entries) =>
108 call("append_session", { actor, attemptId, entries }),
109 readSession: (attemptId, viewer, afterSeq = 0) =>
110 call("read_session", { attemptId, viewer, afterSeq }),
111 };
112}