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

main
ts 111 lines 4.04 KB
Raw
1 import * as Drizzle from "@alchemy.run/drizzle/Postgres";
2 import * as GCP from "@alchemy.run/gcp";
3 import { sql } from "drizzle-orm";
4 import * as Effect from "effect/Effect";
5 import * as Redacted from "effect/Redacted";
6 import { serveProbes } from "../../bindingHost.ts";
7
8 /** Test-only password; the test writes it into {@link Password}. */
9 export const USER_PASSWORD = "Alchemy-bindings-test-1";
10
11 export const Db = Effect.gen(function* () {
12 // Shared-core, zonal, no backups: the cheapest Cloud SQL there is.
13 const instance = yield* GCP.SQL.Instance("BindingsDb", {
14 databaseVersion: "POSTGRES_17",
15 edition: "ENTERPRISE",
16 tier: "db-f1-micro",
17 availabilityType: "ZONAL",
18 backup: { enabled: false },
19 deletionProtectionEnabled: false,
20 dataApiAccess: true,
21 });
22 const database = yield* GCP.SQL.Database("App", {
23 instance: instance.instanceName,
24 });
25 const user = yield* GCP.SQL.User("AppUser", {
26 instance: instance.instanceName,
27 password: USER_PASSWORD,
28 });
29 // The Data API only accepts regional secrets in the instance's region.
30 const password = yield* GCP.SecretManager.LocationsSecret("AppPassword", {
31 location: instance.region,
32 });
33 return { instance, database, user, password };
34 });
35
36 /**
37 * Effect-native Cloud Run service exercising every Cloud SQL binding as
38 * its own runtime service account. Deployed from
39 * {@link ../Bindings.test.ts}.
40 */
41 export default class SqlBindingsHost extends GCP.Function<SqlBindingsHost>()(
42 "SqlBindingsHost",
43 { main: import.meta.url, invokerIamDisabled: true },
44 Effect.gen(function* () {
45 const { instance, database, user, password } = yield* Db;
46 const connect = yield* GCP.SQL.Connect(instance, {
47 database,
48 user,
49 passwordSecret: password,
50 });
51 const executeSql = yield* GCP.SQL.ExecuteSql(instance);
52 const getInstance = yield* GCP.SQL.GetInstance(instance);
53 const getUser = yield* GCP.SQL.GetUser(user);
54 const db = yield* Drizzle.Postgres(connect.pipe(Effect.map((info) => info.url)));
55 const databaseName = yield* database.databaseName;
56 const userName = yield* user.userName;
57 const secretName = yield* password.name;
58
59 return {
60 fetch: serveProbes({
61 connect: Effect.gen(function* () {
62 const info = yield* connect;
63 // A real query over the `/cloudsql` socket: the Cloud SQL
64 // connector authorizes it with the host's cloudsql.client grant.
65 const rows = yield* db.execute<{ user: string; database: string }>(
66 sql`SELECT current_user AS "user", current_database() AS "database"`,
67 "objects",
68 );
69 return {
70 connectionName: info.connectionName,
71 socketPath: info.socketPath,
72 username: info.username,
73 database: info.database,
74 passwordMatches: Redacted.value(info.password) === USER_PASSWORD,
75 rows,
76 };
77 }),
78 executeSql: Effect.gen(function* () {
79 const response = yield* executeSql({
80 body: {
81 database: yield* databaseName,
82 user: yield* userName,
83 passwordSecretVersion: `${yield* secretName}/versions/latest`,
84 sqlStatement: "SELECT current_user AS who, 41 + 1 AS answer",
85 },
86 });
87 return {
88 status: response.status,
89 columns: (response.results?.[0]?.columns ?? []).map((column) => column.name),
90 values: (response.results?.[0]?.rows?.[0]?.values ?? []).map((cell) => cell.value),
91 };
92 }),
93 getInstance: getInstance().pipe(
94 Effect.map((live) => ({
95 name: live.name,
96 state: live.state,
97 connectionName: live.connectionName,
98 })),
99 ),
100 getUser: getUser().pipe(
101 Effect.map((live) => ({ name: live.name, instance: live.instance })),
102 ),
103 }),
104 };
105 }).pipe(
106 Effect.provide(GCP.SQL.ConnectHttp),
107 Effect.provide(GCP.SQL.ExecuteSqlHttp),
108 Effect.provide(GCP.SQL.GetInstanceHttp),
109 Effect.provide(GCP.SQL.GetUserHttp),
110 ),
111 ) {}