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

main
ts 164 lines 5.49 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as Effect from "effect/Effect";
3 import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder";
4 import * as Etag from "effect/http/Etag";
5 import * as HttpPlatform from "effect/http/HttpPlatform";
6 import * as HttpRouter from "effect/http/HttpRouter";
7 import * as Layer from "effect/Layer";
8 import * as Path from "effect/Path";
9 import {
10 CloneToken,
11 CreateRepoResponse,
12 Metadata,
13 RepoApi,
14 RepoConflict,
15 RepoInfo,
16 RepoNotFound,
17 } from "./Api.ts";
18 import Repo from "./Repo.ts";
19 import { Repos } from "./Repos.ts";
20
21 // Workers don't have a FileSystem, so HttpPlatform's file-response surface
22 // is stubbed. The repo API never serves files.
23 const HttpPlatformStub = Layer.succeed(HttpPlatform.HttpPlatform, {
24 fileResponse: () => Effect.die("HttpPlatform.fileResponse not supported"),
25 fileWebResponse: () => Effect.die("HttpPlatform.fileWebResponse not supported"),
26 });
27
28 export default class Worker extends Cloudflare.Worker<Worker>()(
29 "Api",
30 {
31 main: import.meta.url,
32 observability: { enabled: true },
33 compatibility: {
34 date: "2026-08-31",
35 },
36 },
37 Effect.gen(function* () {
38 const artifacts = yield* Repos;
39 const repos = yield* Repo;
40
41 const findRepo = (name: string) =>
42 artifacts.list({ limit: 100 }).pipe(
43 Effect.flatMap((res) => {
44 const found = res.repos.find((r: { name: string }) => r.name === name);
45 return found ? Effect.succeed(found) : Effect.fail(new RepoNotFound({ name }));
46 }),
47 Effect.catchTag("ArtifactsError", () => Effect.fail(new RepoNotFound({ name }))),
48 );
49
50 const handlers = HttpApiBuilder.group(RepoApi, "repos", (h) =>
51 h
52 .handle("createRepo", ({ payload }) =>
53 artifacts
54 .create(payload.name, {
55 description: payload.description,
56 setDefaultBranch: "main",
57 })
58 .pipe(
59 Effect.tap(() =>
60 repos
61 .getByName(payload.name)
62 .init(payload.description ?? "")
63 .pipe(Effect.orDie),
64 ),
65 Effect.map(
66 (c) =>
67 new CreateRepoResponse({
68 name: c.name,
69 remote: c.remote,
70 token: c.token,
71 defaultBranch: c.defaultBranch,
72 }),
73 ),
74 Effect.catchTag("ArtifactsError", (err) =>
75 Effect.fail(new RepoConflict({ message: err.message })),
76 ),
77 ),
78 )
79 .handle("getRepo", ({ params }) =>
80 findRepo(params.name).pipe(
81 Effect.flatMap((found) =>
82 repos
83 .getByName(params.name)
84 .get()
85 .pipe(
86 Effect.catch(() => Effect.succeed(null)),
87 Effect.map(
88 (meta) =>
89 new RepoInfo({
90 id: found.id,
91 name: found.name,
92 description: found.description ?? null,
93 defaultBranch: found.defaultBranch,
94 remote: found.remote,
95 status: found.status,
96 readOnly: found.readOnly,
97 createdAt: found.createdAt,
98 updatedAt: found.updatedAt,
99 lastPushAt: found.lastPushAt ?? null,
100 metadata: meta ? new Metadata(meta) : null,
101 }),
102 ),
103 ),
104 ),
105 ),
106 )
107 .handle("updateRepo", ({ params, payload }) =>
108 findRepo(params.name).pipe(
109 Effect.flatMap(() =>
110 repos
111 .getByName(params.name)
112 .update({
113 description: payload.description,
114 topics: payload.topics ? [...payload.topics] : undefined,
115 })
116 .pipe(Effect.orDie),
117 ),
118 Effect.map((m) => new Metadata(m)),
119 ),
120 )
121 .handle("deleteRepo", ({ params }) =>
122 artifacts.delete(params.name).pipe(
123 Effect.asVoid,
124 Effect.catchTag("ArtifactsError", () =>
125 Effect.fail(new RepoNotFound({ name: params.name })),
126 ),
127 ),
128 )
129 .handle("starRepo", ({ params }) =>
130 findRepo(params.name).pipe(
131 Effect.flatMap(() => repos.getByName(params.name).star().pipe(Effect.orDie)),
132 Effect.map((m) => new Metadata(m)),
133 ),
134 )
135 .handle("cloneToken", ({ params, payload }) =>
136 artifacts.get(params.name).pipe(
137 Effect.flatMap((handle) =>
138 handle.createToken(payload.scope ?? "read", payload.ttl ?? 3600),
139 ),
140 Effect.map(
141 (t) =>
142 new CloneToken({
143 id: t.id,
144 plaintext: t.plaintext,
145 scope: t.scope as "read" | "write",
146 expiresAt: t.expiresAt,
147 }),
148 ),
149 Effect.catchTag("ArtifactsError", () =>
150 Effect.fail(new RepoNotFound({ name: params.name })),
151 ),
152 ),
153 ),
154 );
155
156 return {
157 fetch: HttpApiBuilder.layer(RepoApi).pipe(
158 Layer.provide(handlers),
159 Layer.provide([Etag.layer, HttpPlatformStub, Path.layer]),
160 HttpRouter.toHttpEffect,
161 ),
162 };
163 }).pipe(Effect.provide(Layer.mergeAll(Binding))),
164 ) {}