@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 84 lines 2.8 KB
Raw
1 import type { Config } from "@distilled.cloud/gcp/Credentials";
2 import { Credentials } from "@distilled.cloud/gcp/Credentials";
3 import * as Region from "@distilled.cloud/gcp/Region";
4 import { orDieCredentialsUnavailable } from "alchemy/Auth/Resolve";
5 import * as Context from "effect/Context";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as Layer from "effect/Layer";
9 import * as Option from "effect/Option";
10 import * as Redacted from "effect/Redacted";
11 import { DEFAULT_GCP_REGION, GCP_AUTH_PROVIDER_NAME } from "./AuthProvider.ts";
12
13 export class GcpProjectMissing extends Data.TaggedError("GCP.ProjectMissing")<{
14 message: string;
15 }> {}
16
17 /**
18 * Fully-resolved GCP environment for a stack.
19 *
20 * Distilled GCP authenticates with a bearer access token; Alchemy always
21 * pairs it with a project id so lifecycle operations can fill
22 * `projects/{project}/...` resource names.
23 */
24 export interface GcpEnvironmentShape {
25 accessToken: Redacted.Redacted<string>;
26 project: string;
27 /**
28 * Default region for regional resources created without an explicit
29 * location: a `GCP.Region` override, else the profile / environment
30 * region, else `us-central1`.
31 */
32 region: string;
33 }
34
35 export class GcpEnvironment extends Context.Service<
36 GcpEnvironment,
37 Effect.Effect<GcpEnvironmentShape, GcpProjectMissing>
38 >()("GCP::Environment") {
39 static current = GcpEnvironment.use((env) => env);
40 readonly kind = "Environment" as const;
41 }
42
43 const requireProject = (
44 config: Config,
45 region: string,
46 ): Effect.Effect<GcpEnvironmentShape, GcpProjectMissing> => {
47 if (!config.project) {
48 return Effect.fail(
49 new GcpProjectMissing({
50 message:
51 "GCP project id is required. Set GOOGLE_PROJECT_ID or configure the Alchemy GCP profile.",
52 }),
53 );
54 }
55 return Effect.succeed({
56 accessToken: config.accessToken,
57 project: config.project,
58 region,
59 });
60 };
61
62 /**
63 * Build a `GcpEnvironment` layer from the distilled `Credentials`
64 * service. Provide this after `Credentials.fromAuthProvider()`.
65 */
66 export const fromCredentials = () =>
67 Layer.effect(
68 GcpEnvironment,
69 Effect.gen(function* () {
70 const credentials = yield* Credentials;
71 // Like AWS, the region override is part of the providers layer:
72 // capture it when the layer is built, since provider lifecycles run
73 // in the providers' context, not the caller's.
74 const override = yield* Effect.serviceOption(Region.Region);
75 return Effect.gen(function* () {
76 const config = yield* credentials.pipe(orDieCredentialsUnavailable(GCP_AUTH_PROVIDER_NAME));
77 const region =
78 (Option.isSome(override) ? yield* override.value : undefined) ??
79 config.region ??
80 DEFAULT_GCP_REGION;
81 return yield* requireProject(config, region);
82 });
83 }),
84 );