@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 50 lines 1.87 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
6 /** Bucket the function writes to from `fetch` and from topic deliveries. */
7 export const Marker = GCP.Storage.Bucket("FnMarker", { forceDestroy: true });
8 /** Topic pushed to the function through a push subscription. */
9 export const Pings = GCP.PubSub.Topic("FnPings", {});
10
11 /**
12 * Effect-native Cloud Function: an HTTP `fetch` using Storage bindings,
13 * plus a Pub/Sub push event source that records each message.
14 * Deployed from {@link ../EffectNative.test.ts}.
15 */
16 export default class EffectFunction extends GCP.CloudFunctions.Function<EffectFunction>()(
17 "EffectFunction",
18 { main: import.meta.url, location: "us-central1" },
19 Effect.gen(function* () {
20 const bucket = yield* Marker;
21 const putObject = yield* GCP.Storage.PutObject(bucket);
22 const getObject = yield* GCP.Storage.GetObject(bucket);
23
24 yield* GCP.PubSub.consumeTopicMessages(yield* Pings, (messages) =>
25 messages.pipe(
26 Stream.runForEach(({ message }) =>
27 putObject({
28 name: `ping-${message.messageId}.txt`,
29 body: atob(message.data ?? ""),
30 }).pipe(Effect.orDie),
31 ),
32 ),
33 );
34
35 return {
36 fetch: Effect.gen(function* () {
37 yield* putObject({ name: "hello.txt", body: "from-function" }).pipe(Effect.orDie);
38 const { body } = yield* getObject({ object: "hello.txt" }).pipe(Effect.orDie);
39 return yield* HttpServerResponse.json({
40 read: new TextDecoder().decode(body),
41 node: yield* Effect.sync(() => process.versions.node),
42 });
43 }),
44 };
45 }).pipe(
46 Effect.provide(GCP.Storage.PutObjectHttp),
47 Effect.provide(GCP.Storage.GetObjectHttp),
48 Effect.provide(GCP.CloudFunctions.TopicEventSource),
49 ),
50 ) {}