The best way to control Claude Code, Codex and more from any computer or phone

main
ts 88 lines 3.2 KB
Raw
1 import * as Users from "@distilled.cloud/github/users";
2 import * as Provider from "alchemy/Provider";
3 import { Resource } from "alchemy/Resource";
4 import * as Effect from "effect/Effect";
5 import type * as Redacted from "effect/Redacted";
6 import * as Result from "effect/Result";
7 import { Credentials, config } from "./Credentials.ts";
8 import type * as GitHub from "./Providers.ts";
9
10 export interface PersonalAccessTokenProps {
11 /**
12 * The token to capture. Defaults to the provider's credential (the token
13 * `alchemy login` or `GITHUB_TOKEN` resolves).
14 */
15 token?: Redacted.Redacted<string>;
16 }
17
18 export interface PersonalAccessToken extends Resource<
19 "GitHub.PersonalAccessToken",
20 PersonalAccessTokenProps,
21 {
22 /** The token value. Yield it inside a host to bind it into the host's environment. */
23 value: Redacted.Redacted<string>;
24 /** REST API base URL the token authenticates against. */
25 apiUrl: string;
26 /** The login the token authenticates as. */
27 login: string;
28 },
29 never,
30 GitHub.Providers
31 > {}
32
33 /**
34 * A GitHub token captured as a resource, so a Worker or Function can
35 * authenticate with it at runtime.
36 *
37 * GitHub has no API to mint personal access tokens, so this resource does not
38 * create one. It captures a token (the `token` prop, or the provider's
39 * credential by default), validates it, and stores it in state. The `GitHub.*Http`
40 * binding layers capture one per host and bind its `value` into the host.
41 * Deleting the resource only forgets the token; revoking it stays a manual step.
42 *
43 * ### Capturing a Token
44 * **Example:** The provider's credential
45 * ```typescript
46 * const token = yield* GitHub.PersonalAccessToken("BotToken");
47 * ```
48 *
49 * **Example:** An explicit token
50 * ```typescript
51 * const token = yield* GitHub.PersonalAccessToken("BotToken", {
52 * token: Redacted.make(process.env.BOT_GITHUB_TOKEN!),
53 * });
54 * ```
55 *
56 * @resource
57 * @product Repository
58 */
59 export const PersonalAccessToken = Resource<PersonalAccessToken>("GitHub.PersonalAccessToken");
60
61 /** Read the login the token authenticates as. */
62 const validate = (token: Redacted.Redacted<string>, apiUrl: string) =>
63 Users.getAuthenticated({}).pipe(
64 Effect.map((user) => ({ login: user.login })),
65 Effect.provideService(Credentials, Effect.succeed(config(token, apiUrl))),
66 );
67
68 export const PersonalAccessTokenProvider = () =>
69 Provider.succeed(PersonalAccessToken, {
70 stables: ["value", "apiUrl", "login"],
71 // GitHub cannot enumerate personal access tokens.
72 list: () => Effect.succeed([]),
73 read: Effect.fn(function* ({ output }) {
74 if (output === undefined) return undefined;
75 // A token revoked out-of-band reads as gone, so the next deploy
76 // captures a working one.
77 const probe = yield* validate(output.value, output.apiUrl).pipe(Effect.result);
78 return Result.isSuccess(probe) ? { ...output, ...probe.success } : undefined;
79 }),
80 reconcile: Effect.fn(function* ({ news }) {
81 const credentials = yield* yield* Credentials;
82 const token = news?.token ?? credentials.token;
83 const apiUrl = credentials.apiBaseUrl;
84 const { login } = yield* validate(token, apiUrl);
85 return { value: token, apiUrl, login };
86 }),
87 delete: () => Effect.void,
88 });