@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, 06:00 UTC

main
ts 102 lines 3.8 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as GitHub from "@alchemy.run/github";
3 import type { RuntimeContext } from "alchemy/RuntimeContext";
4 import * as Effect from "effect/Effect";
5 import { HttpServerRequest } from "effect/http/HttpServerRequest";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import * as Layer from "effect/Layer";
8
9 export const owner = "alchemy-run-test";
10 export const repository = "alchemy-pr-2529-bindings";
11
12 // Public and retained across runs: the gh token lacks delete_repo.
13 export const Repo = GitHub.Repository("Repo", {
14 owner,
15 name: repository,
16 visibility: "public",
17 description: "Retained deterministic fixture for alchemy PR #2529 (GitHub runtime bindings)",
18 autoInit: true,
19 });
20
21 /** Every GitHub runtime binding, bound to `repo`, keyed by name. */
22 export const bindAll = Effect.fn(function* (repo: GitHub.Repository) {
23 const bindings: Record<
24 string,
25 (request: any) => Effect.Effect<unknown, { _tag: string }, RuntimeContext>
26 > = {
27 CreateIssue: yield* GitHub.CreateIssue(repo),
28 GetIssue: yield* GitHub.GetIssue(repo),
29 UpdateIssue: yield* GitHub.UpdateIssue(repo),
30 ListIssues: yield* GitHub.ListIssues(repo),
31 SearchIssues: yield* GitHub.SearchIssues(repo),
32 CreateIssueComment: yield* GitHub.CreateIssueComment(repo),
33 ListIssueComments: yield* GitHub.ListIssueComments(repo),
34 GetPullRequest: yield* GitHub.GetPullRequest(repo),
35 ListPullRequests: yield* GitHub.ListPullRequests(repo),
36 CreatePullRequest: yield* GitHub.CreatePullRequest(repo),
37 MergePullRequest: yield* GitHub.MergePullRequest(repo),
38 CreatePullRequestReview: yield* GitHub.CreatePullRequestReview(repo),
39 ListPullRequestReviews: yield* GitHub.ListPullRequestReviews(repo),
40 ListPullRequestReviewComments: yield* GitHub.ListPullRequestReviewComments(repo),
41 ListPullRequestFiles: yield* GitHub.ListPullRequestFiles(repo),
42 };
43 return bindings;
44 });
45
46 /** Call one binding; failures become `{ error }` so tests can assert typed tags. */
47 export const callBinding = (
48 bindings: Effect.Success<ReturnType<typeof bindAll>>,
49 binding: string,
50 request: unknown,
51 ) =>
52 bindings[binding]!(request).pipe(
53 Effect.map((data) => ({ data })),
54 Effect.catch((error) => Effect.succeed({ error })),
55 );
56
57 // `POST /<Binding>` calls the binding with the JSON body as its request.
58 // Failures answer 200 with `{ error }` so the test can assert typed tags.
59 export default class GitHubBindingsWorker extends Cloudflare.Worker<GitHubBindingsWorker>()(
60 "GitHubBindingsWorker",
61 {
62 main: import.meta.url,
63 },
64 Effect.gen(function* () {
65 const repo = yield* Repo;
66 const bindings = yield* bindAll(repo);
67
68 return {
69 fetch: Effect.gen(function* () {
70 const request = yield* HttpServerRequest;
71 const name = new URL(request.url, "http://worker").pathname.slice(1);
72 if (bindings[name] === undefined || request.method !== "POST") {
73 return HttpServerResponse.text("ok");
74 }
75 const body = yield* request.json;
76 return yield* callBinding(bindings, name, body).pipe(
77 Effect.flatMap((result) => HttpServerResponse.json(result)),
78 );
79 }).pipe(Effect.orDie),
80 };
81 }).pipe(
82 Effect.provide(
83 Layer.mergeAll(
84 GitHub.CreateIssueHttp,
85 GitHub.GetIssueHttp,
86 GitHub.UpdateIssueHttp,
87 GitHub.ListIssuesHttp,
88 GitHub.SearchIssuesHttp,
89 GitHub.CreateIssueCommentHttp,
90 GitHub.ListIssueCommentsHttp,
91 GitHub.GetPullRequestHttp,
92 GitHub.ListPullRequestsHttp,
93 GitHub.CreatePullRequestHttp,
94 GitHub.MergePullRequestHttp,
95 GitHub.CreatePullRequestReviewHttp,
96 GitHub.ListPullRequestReviewsHttp,
97 GitHub.ListPullRequestReviewCommentsHttp,
98 GitHub.ListPullRequestFilesHttp,
99 ),
100 ),
101 ),
102 ) {}