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

main
ts 144 lines 4.63 KB
Raw
1 import { Credentials } from "@distilled.cloud/gcp/Credentials";
2 import type { GcpOpContext } from "@distilled.cloud/gcp/pubsublite_v1";
3 import * as Effect from "effect/Effect";
4 import * as HttpClient from "effect/http/HttpClient";
5 import * as GCPBindings from "../GCPBindings.ts";
6 import { grantFor, type BindingIam } from "../HttpBinding.ts";
7 import type { AdminReservation } from "./AdminReservation.ts";
8 import type { AdminSubscription } from "./AdminSubscription.ts";
9 import type { AdminTopic } from "./AdminTopic.ts";
10
11 type Op<I, A, E> = Effect.Effect<(input: I) => Effect.Effect<A, E>, never, GcpOpContext>;
12
13 /**
14 * Shared HTTP scaffolding for Pub/Sub Lite bindings.
15 * NOT exported from index.ts.
16 *
17 * Distilled ops are `OperationMethod`s: yield them once at Layer
18 * construction (after providing Credentials + HttpClient) so the inner
19 * runtime Effect is `Effect<A, E>` and does not leak `GcpOpContext`.
20 */
21 export const makeReservationHttpBinding = <I extends { name?: string }, A, E>(options: {
22 tag: string;
23 iam: BindingIam;
24 operation: Op<I, A, E>;
25 }) =>
26 Effect.gen(function* () {
27 const bind = yield* GCPBindings.Bind;
28 const run = yield* options.operation as Effect.Effect<
29 (input: I) => Effect.Effect<A, E>,
30 never,
31 Credentials | HttpClient.HttpClient
32 >;
33 return Effect.fn(function* (reservation: AdminReservation) {
34 if (!globalThis.__ALCHEMY_RUNTIME__) {
35 yield* bind`${options.tag}(${reservation})`({
36 iam: [grantFor(options.iam, reservation.name)],
37 });
38 }
39 const name = yield* reservation.name;
40 return Effect.fn(`${options.tag}(${reservation.LogicalId})`)(function* (
41 request?: Omit<I, "name">,
42 ) {
43 return yield* run({
44 ...request,
45 name: yield* name,
46 } as I);
47 });
48 });
49 });
50
51 export const makeTopicNameHttpBinding = <I extends { name?: string }, A, E>(options: {
52 tag: string;
53 iam: BindingIam;
54 operation: Op<I, A, E>;
55 }) =>
56 Effect.gen(function* () {
57 const bind = yield* GCPBindings.Bind;
58 const run = yield* options.operation as Effect.Effect<
59 (input: I) => Effect.Effect<A, E>,
60 never,
61 Credentials | HttpClient.HttpClient
62 >;
63 return Effect.fn(function* (topic: AdminTopic) {
64 if (!globalThis.__ALCHEMY_RUNTIME__) {
65 yield* bind`${options.tag}(${topic})`({
66 iam: [grantFor(options.iam, topic.name)],
67 });
68 }
69 const name = yield* topic.name;
70 return Effect.fn(`${options.tag}(${topic.LogicalId})`)(function* (request?: Omit<I, "name">) {
71 return yield* run({
72 ...request,
73 name: yield* name,
74 } as I);
75 });
76 });
77 });
78
79 export const makeTopicStatsHttpBinding = <I extends { topic: string }, A, E>(options: {
80 tag: string;
81 iam: BindingIam;
82 operation: Op<I, A, E>;
83 }) =>
84 Effect.gen(function* () {
85 const bind = yield* GCPBindings.Bind;
86 const run = yield* options.operation as Effect.Effect<
87 (input: I) => Effect.Effect<A, E>,
88 never,
89 Credentials | HttpClient.HttpClient
90 >;
91 return Effect.fn(function* (topic: AdminTopic) {
92 if (!globalThis.__ALCHEMY_RUNTIME__) {
93 yield* bind`${options.tag}(${topic})`({
94 iam: [grantFor(options.iam, topic.name)],
95 });
96 }
97 const name = yield* topic.name;
98 return Effect.fn(`${options.tag}(${topic.LogicalId})`)(function* (
99 request?: Omit<I, "topic">,
100 ) {
101 return yield* run({
102 ...request,
103 topic: yield* name,
104 } as I);
105 });
106 });
107 });
108
109 export const makeSubscriptionHttpBinding = <
110 I extends { name?: string; subscription?: string },
111 A,
112 E,
113 >(options: {
114 tag: string;
115 iam: BindingIam;
116 field: "name" | "subscription";
117 operation: Op<I, A, E>;
118 }) =>
119 Effect.gen(function* () {
120 const bind = yield* GCPBindings.Bind;
121 const run = yield* options.operation as Effect.Effect<
122 (input: I) => Effect.Effect<A, E>,
123 never,
124 Credentials | HttpClient.HttpClient
125 >;
126 return Effect.fn(function* (subscription: AdminSubscription) {
127 if (!globalThis.__ALCHEMY_RUNTIME__) {
128 yield* bind`${options.tag}(${subscription})`({
129 iam: [grantFor(options.iam, subscription.name)],
130 });
131 }
132 const name = yield* subscription.name;
133 return Effect.fn(`${options.tag}(${subscription.LogicalId})`)(function* (
134 request?: Omit<I, "name" | "subscription">,
135 ) {
136 const resolved = yield* name;
137 const input =
138 options.field === "subscription"
139 ? ({ ...request, subscription: resolved } as I)
140 : ({ ...request, name: resolved } as I);
141 return yield* run(input);
142 });
143 });
144 });