The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { GithubOpContext } from "@distilled.cloud/github"; |
| 2 | import { Credentials } from "@distilled.cloud/github/Credentials"; |
| 3 | import { Self } from "alchemy/Self"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import type * as Redacted from "effect/Redacted"; |
| 8 | import { normalizeGitHubBaseUrl } from "./BaseUrl.ts"; |
| 9 | import { config } from "./Credentials.ts"; |
| 10 | import { PersonalAccessToken } from "./PersonalAccessToken.ts"; |
| 11 | import type { Repository } from "./Repository.ts"; |
| 12 | |
| 13 | /** |
| 14 | * Shared scaffolding for the GitHub `*Http` and `*Local` binding layers. |
| 15 | * |
| 16 | * Each binding declares its distilled call once with {@link operation}. |
| 17 | * `make` runs it with a {@link PersonalAccessToken} bound into the host (one |
| 18 | * token resource per host, shared by every GitHub binding on it); |
| 19 | * `BindingLocal.make` runs it with the provider's current credentials. |
| 20 | * |
| 21 | * NOT exported from `index.ts`. |
| 22 | */ |
| 23 | |
| 24 | /** The bound repository, as an operation sees it. */ |
| 25 | export interface BoundRepository { |
| 26 | readonly owner: string; |
| 27 | readonly repository: string; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * One distilled call, implemented by both the `*Http` and `*Local` layers. |
| 32 | * `name` names the span (`GitHub.issues.get`). |
| 33 | */ |
| 34 | export interface Operation<Request extends ReadonlyArray<any>, Out, E> { |
| 35 | readonly name: string; |
| 36 | readonly call: ( |
| 37 | repo: BoundRepository, |
| 38 | ) => (...request: Request) => Effect.Effect<Out, E, GithubOpContext>; |
| 39 | } |
| 40 | |
| 41 | export const operation = <Request extends ReadonlyArray<any>, Out, E>( |
| 42 | name: string, |
| 43 | call: Operation<Request, Out, E>["call"], |
| 44 | ): Operation<Request, Out, E> => ({ name, call }); |
| 45 | |
| 46 | /** Where a client's calls authenticate: a token and the REST API base URL. */ |
| 47 | export interface TokenSource { |
| 48 | readonly token: Effect.Effect<Redacted.Redacted<string>>; |
| 49 | readonly apiUrl: Effect.Effect<string | undefined>; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Bind a repository and return the runtime client for `op`. The repository's |
| 54 | * `fullName` is bound into the host, so the client resolves it at runtime. |
| 55 | */ |
| 56 | export const makeOperationClient = <Request extends ReadonlyArray<any>, Out, E>( |
| 57 | op: Operation<Request, Out, E>, |
| 58 | source: TokenSource, |
| 59 | ) => |
| 60 | Effect.fn(function* (repo: Repository) { |
| 61 | const fullName = yield* repo.fullName; |
| 62 | // A per-repository `baseUrl` prop wins over the token's host. |
| 63 | const repoBaseUrl = |
| 64 | typeof repo.Props?.baseUrl === "string" |
| 65 | ? yield* normalizeGitHubBaseUrl(repo.Props.baseUrl).pipe(Effect.orDie) |
| 66 | : undefined; |
| 67 | // Credentials resolve per request, so the bound token is read at runtime. |
| 68 | const github = Layer.mergeAll( |
| 69 | Layer.succeed( |
| 70 | Credentials, |
| 71 | Effect.all([source.token, source.apiUrl]).pipe( |
| 72 | Effect.map(([token, apiUrl]) => config(token, repoBaseUrl ?? apiUrl)), |
| 73 | ), |
| 74 | ), |
| 75 | FetchHttpClient.layer, |
| 76 | ); |
| 77 | return Effect.fn(`GitHub.${op.name}`)(function* (...request: Request) { |
| 78 | const [owner, repository] = (yield* fullName).split("/") as [string, string]; |
| 79 | return yield* op |
| 80 | .call({ owner, repository })(...request) |
| 81 | .pipe(Effect.provide(github)); |
| 82 | }); |
| 83 | }); |
| 84 | |
| 85 | /** |
| 86 | * The token-backed implementation of a GitHub binding. Pass it to |
| 87 | * `Layer.effect(Tag, BindingHttp.make(op))`. |
| 88 | */ |
| 89 | export const make = <Request extends ReadonlyArray<any>, Out, E>(op: Operation<Request, Out, E>) => |
| 90 | Effect.gen(function* () { |
| 91 | const Token = yield* PersonalAccessToken; |
| 92 | const self = yield* Self; |
| 93 | const token = yield* Token(`${self.LogicalId}GitHubToken`); |
| 94 | return makeOperationClient(op, { |
| 95 | token: yield* token.value, |
| 96 | apiUrl: yield* token.apiUrl, |
| 97 | }); |
| 98 | }); |