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

main
ts 89 lines 3.21 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import * as Effect from "effect/Effect";
3 import * as HttpServerResponse from "effect/http/HttpServerResponse";
4 import * as Stream from "effect/Stream";
5 import { documentFor, Files, UPLOADS_PREFIX, Uploads, type FileRecord } from "./resources.ts";
6
7 const sha256Hex = (bytes: Uint8Array) =>
8 Effect.promise(() => crypto.subtle.digest("SHA-256", bytes as Uint8Array<ArrayBuffer>)).pipe(
9 Effect.map((digest) =>
10 Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0")).join(""),
11 ),
12 );
13
14 /**
15 * The background half of the pipeline: index every finished upload.
16 *
17 * Cloud Storage publishes an `OBJECT_FINALIZE` notification for each
18 * object written under `uploads/`; Pub/Sub pushes it to this service,
19 * which reads the object back, hashes it, and records the result in
20 * Firestore. Nothing here runs on the upload's request path, so a slow
21 * hash of a large file never delays the client.
22 *
23 * The service stays private: Pub/Sub authenticates each push with an
24 * OIDC token for the service's own identity.
25 */
26 export default class Indexer extends GCP.Function<Indexer>()(
27 "Indexer",
28 {
29 main: import.meta.url,
30 },
31 Effect.gen(function* () {
32 const bucket = yield* Uploads;
33 const database = yield* Files;
34
35 const uploads = yield* GCP.Storage.ReadBucket(bucket);
36 const files = yield* GCP.Firestore.WriteDatabase(database);
37
38 yield* GCP.Storage.consumeBucketEvents(
39 bucket,
40 { eventTypes: ["OBJECT_FINALIZE"], prefix: UPLOADS_PREFIX },
41 (events) =>
42 events.pipe(
43 Stream.runForEach((event) =>
44 Effect.gen(function* () {
45 // Read the exact generation the event describes. If it has
46 // since been overwritten or deleted, a later event (or the
47 // delete) owns the record, so there is nothing to do.
48 const object = yield* uploads.get(event.object, {
49 generation: event.generation,
50 });
51 if (object === undefined) {
52 yield* Effect.log(`indexer: ${event.object} is gone, skipping`);
53 return;
54 }
55
56 const name = event.object.slice(UPLOADS_PREFIX.length);
57 const record: FileRecord = {
58 name,
59 object: event.object,
60 generation: event.generation,
61 size: object.body.byteLength,
62 contentType:
63 object.contentType ?? event.metadata.contentType ?? "application/octet-stream",
64 sha256: yield* sha256Hex(object.body),
65 indexedAt: new Date(),
66 };
67
68 yield* files.set(documentFor(name), { ...record });
69 yield* Effect.log(`indexer: ${name} (${record.size} bytes)`);
70 }).pipe(
71 // A failure fails the push, so Pub/Sub redelivers the event.
72 Effect.orDie,
73 ),
74 ),
75 ),
76 );
77
78 return {
79 fetch: Effect.succeed(HttpServerResponse.text("ok")),
80 };
81 }).pipe(
82 Effect.provide([
83 GCP.Storage.BucketEventSourceLive,
84 GCP.Storage.ReadBucketHttp,
85 GCP.Firestore.WriteDatabaseHttp,
86 ]),
87 Effect.provide(GCP.Run.TopicEventSource),
88 ),
89 ) {}