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

main
ts 147 lines 5.12 KB
Raw
1 import * as HttpApi from "effect/http-api/HttpApi";
2 import * as HttpApiEndpoint from "effect/http-api/HttpApiEndpoint";
3 import * as HttpApiGroup from "effect/http-api/HttpApiGroup";
4 import * as HttpApiSchema from "effect/http-api/HttpApiSchema";
5 import * as Schema from "effect/Schema";
6 import { PackageName } from "./Manifest.ts";
7
8 /**
9 * The HTTP contract between the `pkg` CLI and the registry Worker, as an
10 * `HttpApi`. The Worker implements it with `HttpApiBuilder`; the CLI calls
11 * it through `HttpApiClient`. Importing this file pulls in no runtime code.
12 */
13
14 /**
15 * The GitHub Actions run a request comes from. It is a lookup hint, not a
16 * credential: the registry resolves the run through the GitHub API and
17 * trusts only what GitHub says about it.
18 */
19 export const RunRef = Schema.Struct({
20 repo: Schema.String,
21 runId: Schema.Number,
22 attempt: Schema.Number,
23 });
24 export type RunRef = typeof RunRef.Type;
25
26 /**
27 * Name of the artifact a job uploads to its own run to vouch for a manifest.
28 * Only the job holds the runtime token that can add artifacts to the run, so
29 * an artifact carrying the manifest's hash is GitHub's record that this run
30 * approved exactly these package hashes.
31 */
32 export const manifestArtifactName = (sha256: string) => `pkg-manifest-${sha256}`;
33
34 /** Lowercase hex SHA-256. */
35 export const Sha256 = Schema.String.pipe(Schema.check(Schema.isPattern(/^[a-f0-9]{64}$/)));
36
37 export const TarballRef = Schema.Struct({
38 name: PackageName,
39 sha256: Sha256,
40 });
41 export type TarballRef = typeof TarballRef.Type;
42
43 /** The request names no usable run, or carries an invalid body. */
44 export class BadRequest extends Schema.TaggedError<BadRequest>()(
45 "BadRequest",
46 { message: Schema.String },
47 { httpApiStatus: 400 },
48 ) {}
49
50 /** The repository may not publish, or the run has not vouched for the manifest. */
51 export class Forbidden extends Schema.TaggedError<Forbidden>()(
52 "Forbidden",
53 { message: Schema.String },
54 { httpApiStatus: 403 },
55 ) {}
56
57 /** Requests come from inside a run, so a finished run cannot publish. */
58 export class RunNotInProgress extends Schema.TaggedError<RunNotInProgress>()(
59 "RunNotInProgress",
60 { message: Schema.String },
61 { httpApiStatus: 409 },
62 ) {}
63
64 /** Upload these, then publish again. */
65 export class MissingTarballs extends Schema.TaggedError<MissingTarballs>()(
66 "MissingTarballs",
67 { missing: Schema.Array(TarballRef) },
68 { httpApiStatus: 409 },
69 ) {}
70
71 export class PackageTooLarge extends Schema.TaggedError<PackageTooLarge>()(
72 "PackageTooLarge",
73 { message: Schema.String },
74 { httpApiStatus: 413 },
75 ) {}
76
77 /** GitHub could not be reached or answered with an error. */
78 export class Upstream extends Schema.TaggedError<Upstream>()(
79 "Upstream",
80 { message: Schema.String },
81 { httpApiStatus: 502 },
82 ) {}
83
84 export const PublishedPackage = Schema.Struct({
85 name: Schema.String,
86 group: Schema.String,
87 /** Install URL pinned to the commit. */
88 url: Schema.String,
89 tags: Schema.Array(Schema.String),
90 });
91 export type PublishedPackage = typeof PublishedPackage.Type;
92
93 export const PublishResponse = Schema.Struct({
94 packages: Schema.Array(PublishedPackage),
95 });
96 export type PublishResponse = typeof PublishResponse.Type;
97
98 /**
99 * `manifest` is the exact `pkg-manifest.json` text the job vouched for; its
100 * SHA-256 must match an artifact on the run. Idempotent: the registry
101 * either fails with {@link MissingTarballs} or writes the tags.
102 */
103 export const publish = HttpApiEndpoint.post("publish", "/api/publish", {
104 payload: Schema.Struct({ run: RunRef, manifest: Schema.String }),
105 success: PublishResponse,
106 error: [BadRequest, Forbidden, RunNotInProgress, MissingTarballs, PackageTooLarge, Upstream],
107 });
108
109 export const TarballResponse = Schema.Struct({
110 name: Schema.String,
111 sha256: Sha256,
112 size: Schema.Number,
113 uploaded: Schema.Boolean,
114 });
115 export type TarballResponse = typeof TarballResponse.Type;
116
117 /**
118 * Content-addressed upload. Any in-progress run of an allowed repository
119 * may upload; bytes only become reachable once a vouched manifest tags
120 * them. The run travels in the query because the body is the tarball.
121 */
122 export const uploadTarball = HttpApiEndpoint.put("uploadTarball", "/api/tarballs/:name/:sha256", {
123 params: TarballRef,
124 query: RunRef,
125 payload: Schema.Uint8Array.pipe(HttpApiSchema.asUint8Array()),
126 success: TarballResponse,
127 error: [BadRequest, Forbidden, RunNotInProgress, PackageTooLarge, Upstream],
128 });
129
130 export const health = HttpApiEndpoint.get("health", "/api/health", {
131 success: Schema.Struct({ ok: Schema.Boolean }),
132 });
133
134 export class Registry extends HttpApiGroup.make("Registry").add(publish).add(uploadTarball) {}
135
136 export class Health extends HttpApiGroup.make("Health").add(health) {}
137
138 export class PkgApi extends HttpApi.make("Pkg").add(Registry).add(Health) {}
139
140 /**
141 * Immutable tarball URL on the registry. Dependencies between packed
142 * packages link to these, so a tarball's bytes depend only on its own
143 * source and its dependencies' bytes, never on a commit, and identical
144 * builds deduplicate across commits, pull requests, and repositories.
145 */
146 export const tarballUrl = (registry: string, name: string, sha256: string) =>
147 `${registry.replace(/\/+$/, "")}/${name}/-/${sha256}.tgz`;