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

main
ts 153 lines 5.64 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import * as Effect from "effect/Effect";
3 import * as Schedule from "effect/Schedule";
4 import { serveProbes } from "../../bindingHost.ts";
5
6 /** Topic the `Publish` binding writes to; `PublishTap` observes it. */
7 export const PublishTopic = GCP.PubSub.Topic("PublishTopic", {});
8 export const PublishTap = Effect.gen(function* () {
9 const topic = yield* PublishTopic;
10 return yield* GCP.PubSub.Subscription("PublishTap", { topic: topic.name });
11 });
12
13 /** Topic the `WriteTopic` client writes to; `WriteTap` observes it. */
14 export const WriteTopicTarget = GCP.PubSub.Topic("WriteTopicTarget", {});
15 export const WriteTap = Effect.gen(function* () {
16 const topic = yield* WriteTopicTarget;
17 return yield* GCP.PubSub.Subscription("WriteTap", { topic: topic.name });
18 });
19
20 /** Subscription the `Pull` / `Acknowledge` bindings consume. */
21 export const PullTopic = GCP.PubSub.Topic("PullTopic", {});
22 export const PullInbox = Effect.gen(function* () {
23 const topic = yield* PullTopic;
24 return yield* GCP.PubSub.Subscription("PullInbox", {
25 topic: topic.name,
26 ackDeadlineSeconds: 10,
27 });
28 });
29
30 /** Subscription the `ReadSubscription` client consumes. */
31 export const ReadTopic = GCP.PubSub.Topic("ReadTopic", {});
32 export const ReadInbox = Effect.gen(function* () {
33 const topic = yield* ReadTopic;
34 return yield* GCP.PubSub.Subscription("ReadInbox", {
35 topic: topic.name,
36 ackDeadlineSeconds: 10,
37 });
38 });
39
40 /** Avro schema for `GetSchema` / `ValidateMessage`. */
41 export const EventSchema = GCP.PubSub.Schema("EventSchema", {
42 type: "AVRO",
43 definition: JSON.stringify({
44 type: "record",
45 name: "Event",
46 fields: [{ name: "id", type: "string" }],
47 }),
48 });
49
50 export const PUBLISH_PAYLOAD = "publish-binding";
51 export const WRITE_PAYLOAD = "write-topic-binding";
52 export const WRITE_BATCH_TEXT = "batch-text";
53 /** Binary payload: must round-trip byte for byte. */
54 export const WRITE_BATCH_BYTES = [0, 1, 255];
55
56 const untilSome = <A>(page: ReadonlyArray<A> | undefined) => (page ?? []).length > 0;
57
58 /**
59 * Effect-native Cloud Run service exercising every Pub/Sub binding as its
60 * own runtime service account. Deployed from {@link ../Bindings.test.ts}.
61 */
62 export default class PubSubBindingsHost extends GCP.Function<PubSubBindingsHost>()(
63 "PubSubBindingsHost",
64 { main: import.meta.url, invokerIamDisabled: true },
65 Effect.gen(function* () {
66 const publish = yield* GCP.PubSub.Publish(PublishTopic);
67 const pull = yield* GCP.PubSub.Pull(PullInbox);
68 const acknowledge = yield* GCP.PubSub.Acknowledge(PullInbox);
69 const writeTopic = yield* GCP.PubSub.WriteTopic(WriteTopicTarget);
70 const readSubscription = yield* GCP.PubSub.ReadSubscription(ReadInbox);
71 const getSchema = yield* GCP.PubSub.GetSchema(EventSchema);
72 const validate = yield* GCP.PubSub.ValidateMessage(EventSchema);
73
74 return {
75 fetch: serveProbes({
76 publish: publish({
77 body: {
78 messages: [
79 {
80 data: Buffer.from(PUBLISH_PAYLOAD).toString("base64"),
81 attributes: { via: "Publish" },
82 },
83 ],
84 },
85 }),
86 pullAndAcknowledge: Effect.gen(function* () {
87 const page = yield* pull({
88 body: { maxMessages: 10, returnImmediately: false },
89 }).pipe(
90 Effect.repeat({
91 schedule: Schedule.spaced("1 second"),
92 until: (page) => untilSome(page.receivedMessages),
93 times: 20,
94 }),
95 );
96 const received = page.receivedMessages ?? [];
97 yield* acknowledge({
98 body: { ackIds: received.map((message) => message.ackId ?? "") },
99 });
100 return received.map((message) =>
101 Buffer.from(message.message?.data ?? "", "base64").toString("utf8"),
102 );
103 }),
104 writeTopic: Effect.gen(function* () {
105 const id = yield* writeTopic.publish({
106 data: WRITE_PAYLOAD,
107 attributes: { via: "WriteTopic" },
108 });
109 const batch = yield* writeTopic.publishBatch([
110 { data: WRITE_BATCH_TEXT },
111 { data: new Uint8Array(WRITE_BATCH_BYTES) },
112 ]);
113 const empty = yield* writeTopic.publishBatch([]);
114 return { id, batch, empty };
115 }),
116 readSubscription: Effect.gen(function* () {
117 const messages = yield* readSubscription.pull({ maxMessages: 10 }).pipe(
118 Effect.repeat({
119 schedule: Schedule.spaced("1 second"),
120 until: untilSome,
121 times: 20,
122 }),
123 );
124 // Extend, then acknowledge: exercises every client method.
125 const ackIds = messages.map((message) => message.ackId);
126 yield* readSubscription.modifyAckDeadline(ackIds, 30);
127 yield* readSubscription.acknowledge(ackIds);
128 return messages.map((message) => ({
129 text: message.text,
130 attributes: message.attributes,
131 }));
132 }),
133 getSchema: getSchema({ view: "FULL" }),
134 validateValid: validate({
135 encoding: "JSON",
136 message: Buffer.from(JSON.stringify({ id: "abc" })).toString("base64"),
137 }),
138 validateInvalid: validate({
139 encoding: "JSON",
140 message: Buffer.from(JSON.stringify({ wrong: 1 })).toString("base64"),
141 }),
142 }),
143 };
144 }).pipe(
145 Effect.provide(GCP.PubSub.PublishHttp),
146 Effect.provide(GCP.PubSub.PullHttp),
147 Effect.provide(GCP.PubSub.AcknowledgeHttp),
148 Effect.provide(GCP.PubSub.WriteTopicHttp),
149 Effect.provide(GCP.PubSub.ReadSubscriptionHttp),
150 Effect.provide(GCP.PubSub.GetSchemaHttp),
151 Effect.provide(GCP.PubSub.ValidateMessageHttp),
152 ),
153 ) {}