@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 102 lines 3.56 KB
Raw
1 import type { ConfigError } from "@distilled.cloud/core/errors";
2 import {
3 Credentials,
4 DEFAULT_API_BASE_URL,
5 type Config,
6 } from "@distilled.cloud/github/Credentials";
7 import { lazyCredentials } from "alchemy/Auth/Resolve";
8 import * as Effect from "effect/Effect";
9 import * as Layer from "effect/Layer";
10 import * as Redacted from "effect/Redacted";
11 import {
12 GITHUB_AUTH_PROVIDER_NAME,
13 type GitHubResolvedCredentials,
14 readEnvCredentials,
15 } from "./AuthProvider.ts";
16 import { normalizeGitHubBaseUrl } from "./BaseUrl.ts";
17
18 export { Credentials } from "@distilled.cloud/github/Credentials";
19
20 declare module "@distilled.cloud/github/Credentials" {
21 // Lets stack programs require it, e.g. an Action using a `GitHub.*Local` binding.
22 interface Credentials {
23 readonly kind: "Credentials";
24 }
25 }
26
27 /** The User-Agent alchemy sends; GitHub rejects requests without one. */
28 export const USER_AGENT = "alchemy";
29
30 /**
31 * Distilled credentials for a token on a REST API base URL (`undefined`, from
32 * {@link normalizeGitHubBaseUrl}, is github.com).
33 */
34 export const config = (
35 token: Redacted.Redacted<string>,
36 apiBaseUrl: string | undefined,
37 ): Config => ({ token, apiBaseUrl: apiBaseUrl ?? DEFAULT_API_BASE_URL, userAgent: USER_AGENT });
38
39 /**
40 * Build a `Credentials` layer from a literal token. Useful for
41 * tests or when callers already have a PAT in hand.
42 *
43 * Pass `baseUrl` to target a GitHub Enterprise instance. It accepts a
44 * hostname or URL and is normalized into the REST API base URL — a GitHub
45 * Enterprise Server host gets `/api/v3` appended, a `*.ghe.com`
46 * data-residency host gets the `api.` prefix.
47 */
48 export const fromToken = (
49 token: string | Redacted.Redacted<string>,
50 options?: { readonly baseUrl?: string },
51 ) =>
52 Layer.succeed(
53 Credentials,
54 Effect.gen(function* () {
55 const baseUrl =
56 options?.baseUrl !== undefined ? yield* normalizeGitHubBaseUrl(options.baseUrl) : undefined;
57 return config(typeof token === "string" ? Redacted.make(token) : token, baseUrl);
58 }).pipe(Effect.orDie),
59 );
60
61 /**
62 * Build a `Credentials` layer that reads the token from
63 * `GITHUB_ACCESS_TOKEN` or `GITHUB_TOKEN` at layer build time.
64 *
65 * GitHub Enterprise is resolved from the environment too: `GITHUB_BASE_URL`,
66 * `GITHUB_API_URL` (set by GitHub Actions runners), or `GH_HOST` select the
67 * host, and on an enterprise host `GH_ENTERPRISE_TOKEN` /
68 * `GITHUB_ENTERPRISE_TOKEN` are checked before the standard token variables.
69 */
70 export const fromEnv = () =>
71 Layer.succeed(
72 Credentials,
73 readEnvCredentials().pipe(
74 Effect.map((creds) => config(creds.token, creds.baseUrl)),
75 Effect.orDie,
76 ),
77 );
78
79 /**
80 * Build a `Credentials` layer that resolves a token via the
81 * Alchemy AuthProvider for the configured profile (defaults to
82 * the current Alchemy profile).
83 *
84 * Pass `baseUrl` to hard-code the GitHub host — it takes precedence over
85 * whatever host the auth provider resolved from the profile config or
86 * environment. `GitHub.providers({ baseUrl })` threads its option here.
87 */
88 export const fromAuthProvider = (options?: { readonly baseUrl?: string }) =>
89 Layer.effect(
90 Credentials,
91 Effect.gen(function* () {
92 const fixedBaseUrl =
93 options?.baseUrl !== undefined
94 ? { baseUrl: yield* normalizeGitHubBaseUrl(options.baseUrl) }
95 : undefined;
96 return yield* lazyCredentials<GitHubResolvedCredentials, ConfigError>()(
97 GITHUB_AUTH_PROVIDER_NAME,
98 (creds) =>
99 config(creds.token, fixedBaseUrl !== undefined ? fixedBaseUrl.baseUrl : creds.baseUrl),
100 );
101 }).pipe(Effect.orDie),
102 );