@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 215 lines 7.59 KB
Raw
1 import { Credentials } from "@distilled.cloud/gcp/Credentials";
2 import type * as storage from "@distilled.cloud/gcp/storage_v1";
3 import * as Context from "effect/Context";
4 import * as Data from "effect/Data";
5 import * as Effect from "effect/Effect";
6 import * as HttpClient from "effect/http/HttpClient";
7 import * as HttpClientRequest from "effect/http/HttpClientRequest";
8 import type * as HttpClientResponse from "effect/http/HttpClientResponse";
9 import * as Schedule from "effect/Schedule";
10 import * as GCPBindings from "../GCPBindings.ts";
11
12 /**
13 * Object content transfer over the Cloud Storage JSON API media endpoints
14 * (`alt=media` download, `uploadType=multipart` upload). The generated
15 * storage SDK only covers the metadata surface — `objects.get` returns
16 * metadata and `objects.insert` without the `/upload` path is rejected —
17 * so content bindings speak these two endpoints directly.
18 *
19 * NOT exported from `index.ts`.
20 */
21
22 const API = "https://storage.googleapis.com";
23
24 /** The object (or bucket) does not exist. */
25 export class ObjectNotFound extends Data.TaggedError("GCP.Storage.ObjectNotFound")<{
26 bucket: string;
27 object: string;
28 }> {}
29
30 /** Cloud Storage rejected the request. */
31 export class ObjectRequestFailed extends Data.TaggedError("GCP.Storage.ObjectRequestFailed")<{
32 bucket: string;
33 object: string;
34 status: number;
35 message: string;
36 }> {}
37
38 export interface ObjectContent {
39 /** Raw object bytes. */
40 body: Uint8Array;
41 /** `Content-Type` the object was stored with. */
42 contentType: string | undefined;
43 /** Object generation served. */
44 generation: string | undefined;
45 }
46
47 export interface PutObjectContent {
48 /** Object name (key). */
49 name: string;
50 /** Object content. Strings are encoded as UTF-8. */
51 body: string | Uint8Array;
52 /** @default "application/octet-stream" (or "text/plain; charset=utf-8" for strings) */
53 contentType?: string;
54 /** Custom metadata stored on the object. */
55 metadata?: Record<string, string>;
56 /** `Cache-Control` served with the object. */
57 cacheControl?: string;
58 /** Only write when the live generation matches (`0` = only if absent). */
59 ifGenerationMatch?: string;
60 }
61
62 const isRetryable = (error: ObjectNotFound | ObjectRequestFailed) =>
63 error._tag === "GCP.Storage.ObjectRequestFailed" && (error.status === 429 || error.status >= 500);
64
65 const failureOf = (
66 bucket: string,
67 object: string,
68 response: HttpClientResponse.HttpClientResponse,
69 ) =>
70 Effect.gen(function* () {
71 if (response.status === 404) {
72 return yield* new ObjectNotFound({ bucket, object });
73 }
74 const text = yield* response.text.pipe(Effect.orElseSucceed(() => ""));
75 return yield* new ObjectRequestFailed({
76 bucket,
77 object,
78 status: response.status,
79 message: text.slice(0, 500),
80 });
81 });
82
83 const transportFailure = (bucket: string, object: string) => (cause: { message?: string }) =>
84 new ObjectRequestFailed({
85 bucket,
86 object,
87 status: 0,
88 message: cause.message ?? "HTTP transport failure",
89 });
90
91 /**
92 * Build the download / upload callables once, closing over the ambient
93 * `Credentials` and `HttpClient` (resolved at Layer construction, like a
94 * distilled operation).
95 */
96 export const makeObjectMedia = Effect.gen(function* () {
97 const context = yield* GCPBindings.withCredentials(
98 Effect.context<Credentials | HttpClient.HttpClient>(),
99 );
100 const credentials = Context.get(context, Credentials);
101 const http = Context.get(context, HttpClient.HttpClient);
102
103 // A failed token exchange (e.g. workload identity) fails the object request
104 // with status 401 rather than leaking `GCPCredentialsError` into the
105 // binding's typed errors.
106 const authorized = (
107 bucket: string,
108 object: string,
109 request: HttpClientRequest.HttpClientRequest,
110 ) =>
111 credentials.pipe(
112 Effect.mapError(
113 (cause) => new ObjectRequestFailed({ bucket, object, status: 401, message: cause.message }),
114 ),
115 Effect.map((config) => request.pipe(HttpClientRequest.bearerToken(config.accessToken))),
116 );
117
118 const download = (options: { bucket: string; object: string; generation?: string }) =>
119 Effect.gen(function* () {
120 const request = yield* authorized(
121 options.bucket,
122 options.object,
123 HttpClientRequest.get(
124 `${API}/storage/v1/b/${encodeURIComponent(options.bucket)}/o/${encodeURIComponent(options.object)}`,
125 ).pipe(
126 HttpClientRequest.setUrlParams({
127 alt: "media",
128 ...(options.generation === undefined ? {} : { generation: options.generation }),
129 }),
130 ),
131 );
132 const response = yield* http
133 .execute(request)
134 .pipe(Effect.mapError(transportFailure(options.bucket, options.object)));
135 if (response.status !== 200) {
136 return yield* failureOf(options.bucket, options.object, response);
137 }
138 const body = yield* response.arrayBuffer.pipe(
139 Effect.mapError(transportFailure(options.bucket, options.object)),
140 );
141 return {
142 body: new Uint8Array(body),
143 contentType: response.headers["content-type"],
144 generation: response.headers["x-goog-generation"],
145 } satisfies ObjectContent;
146 }).pipe(
147 Effect.retry({
148 while: isRetryable,
149 times: 4,
150 schedule: Schedule.exponential("200 millis"),
151 }),
152 );
153
154 const upload = (bucket: string, options: PutObjectContent) =>
155 Effect.gen(function* () {
156 const bytes =
157 typeof options.body === "string" ? new TextEncoder().encode(options.body) : options.body;
158 const contentType =
159 options.contentType ??
160 (typeof options.body === "string"
161 ? "text/plain; charset=utf-8"
162 : "application/octet-stream");
163 const boundary = yield* Effect.sync(() => `alchemy-${crypto.randomUUID()}`);
164 const metadata = JSON.stringify({
165 name: options.name,
166 contentType,
167 metadata: options.metadata,
168 cacheControl: options.cacheControl,
169 });
170 const encoder = new TextEncoder();
171 const head = encoder.encode(
172 `--${boundary}\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n${metadata}\r\n` +
173 `--${boundary}\r\nContent-Type: ${contentType}\r\n\r\n`,
174 );
175 const tail = encoder.encode(`\r\n--${boundary}--\r\n`);
176 const payload = new Uint8Array(head.length + bytes.length + tail.length);
177 payload.set(head, 0);
178 payload.set(bytes, head.length);
179 payload.set(tail, head.length + bytes.length);
180
181 const request = yield* authorized(
182 bucket,
183 options.name,
184 HttpClientRequest.post(`${API}/upload/storage/v1/b/${encodeURIComponent(bucket)}/o`).pipe(
185 HttpClientRequest.setUrlParams({
186 uploadType: "multipart",
187 ...(options.ifGenerationMatch === undefined
188 ? {}
189 : { ifGenerationMatch: options.ifGenerationMatch }),
190 }),
191 HttpClientRequest.bodyUint8Array(payload, `multipart/related; boundary=${boundary}`),
192 ),
193 );
194 const response = yield* http
195 .execute(request)
196 .pipe(Effect.mapError(transportFailure(bucket, options.name)));
197 if (response.status !== 200) {
198 return yield* failureOf(bucket, options.name, response);
199 }
200 const json = yield* response.json.pipe(
201 Effect.mapError(transportFailure(bucket, options.name)),
202 );
203 // The upload endpoint answers with the same `Object` resource
204 // `objects.insert` documents.
205 return json as storage.Storage_Object;
206 }).pipe(
207 Effect.retry({
208 while: isRetryable,
209 times: 4,
210 schedule: Schedule.exponential("200 millis"),
211 }),
212 );
213
214 return { download, upload };
215 });