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, 04:45 UTC
| 1 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 2 | import type { ResourceBinding } from "alchemy/Resource"; |
| 3 | import * as Data from "effect/Data"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Redacted from "effect/Redacted"; |
| 6 | import type { FunctionBinding, FunctionProps } from "./Function.ts"; |
| 7 | |
| 8 | export class FunctionConfigurationError extends Data.TaggedError("FunctionConfigurationError")<{ |
| 9 | message: string; |
| 10 | cause?: unknown; |
| 11 | }> {} |
| 12 | |
| 13 | const injected = new Set([ |
| 14 | "NEON_API_KEY", |
| 15 | "DATABASE_URL", |
| 16 | "DATABASE_URL_UNPOOLED", |
| 17 | "PGHOST", |
| 18 | "PGHOST_UNPOOLED", |
| 19 | "PGUSER", |
| 20 | "PGPASSWORD", |
| 21 | "PGDATABASE", |
| 22 | "AWS_ACCESS_KEY_ID", |
| 23 | "AWS_SECRET_ACCESS_KEY", |
| 24 | "AWS_ENDPOINT_URL_S3", |
| 25 | "AWS_REGION", |
| 26 | "NEON_AUTH_BASE_URL", |
| 27 | "NEON_AUTH_URL", |
| 28 | "NEON_AUTH_JWKS_URL", |
| 29 | "NEON_DATA_API_URL", |
| 30 | "NEON_AI_GATEWAY_TOKEN", |
| 31 | "NEON_AI_GATEWAY_BASE_URL", |
| 32 | "NEON_BRANCH", |
| 33 | "AI_GATEWAY_API_KEY", |
| 34 | "AI_GATEWAY_BASE_URL", |
| 35 | "NEON_PROJECT_ID", |
| 36 | "NEON_BRANCH_ID", |
| 37 | ]); |
| 38 | |
| 39 | const checkedSlug = Effect.fn(function* (id: string, slug?: string) { |
| 40 | const value = |
| 41 | slug ?? |
| 42 | (yield* createPhysicalName({ id, maxLength: 20, delimiter: "", lowercase: true })).replaceAll( |
| 43 | /[^a-z0-9]/g, |
| 44 | "", |
| 45 | ); |
| 46 | if (!/^[a-z0-9]{1,20}$/.test(value)) |
| 47 | return yield* new FunctionConfigurationError({ |
| 48 | message: "Function slug must match ^[a-z0-9]{1,20}$", |
| 49 | }); |
| 50 | return value; |
| 51 | }); |
| 52 | |
| 53 | export function functionSlug( |
| 54 | id: string, |
| 55 | slug: string, |
| 56 | ): Effect.Effect<string, FunctionConfigurationError>; |
| 57 | export function functionSlug(id: string, slug?: string): ReturnType<typeof checkedSlug>; |
| 58 | export function functionSlug(id: string, slug?: string) { |
| 59 | return checkedSlug(id, slug); |
| 60 | } |
| 61 | |
| 62 | export const functionEnvironment = Effect.fn(function* ( |
| 63 | props: FunctionProps, |
| 64 | bindings: ResourceBinding<FunctionBinding>[], |
| 65 | ) { |
| 66 | const env: Record<string, string> = {}; |
| 67 | for (const source of [props.env ?? {}, ...bindings.map((binding) => binding.data.env ?? {})]) { |
| 68 | for (const [key, value] of Object.entries(source)) { |
| 69 | if (value === undefined) continue; |
| 70 | if (injected.has(key) || key.startsWith("ALCHEMY_")) |
| 71 | return yield* new FunctionConfigurationError({ |
| 72 | message: `Cannot override injected Function environment key ${key}`, |
| 73 | }); |
| 74 | if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) |
| 75 | return yield* new FunctionConfigurationError({ |
| 76 | message: `Invalid Function environment key ${key}`, |
| 77 | }); |
| 78 | const plain = Redacted.isRedacted(value) ? Redacted.value(value) : value; |
| 79 | if (key in env && env[key] !== plain) |
| 80 | return yield* new FunctionConfigurationError({ |
| 81 | message: `Conflicting Function environment key ${key}`, |
| 82 | }); |
| 83 | env[key] = plain; |
| 84 | } |
| 85 | } |
| 86 | return env; |
| 87 | }); |