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
| 1 | import * as Output from "alchemy/Output"; |
| 2 | import type { ProviderMode } from "alchemy/ProviderMode"; |
| 3 | import { isResource, type ResourceLike } from "alchemy/Resource"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import type { AIGatewayProps } from "./AIGateway.ts"; |
| 6 | |
| 7 | type CredentialScope = Pick<AIGatewayProps, "branch" | "project">; |
| 8 | |
| 9 | export const scopeIdentity = (scope: CredentialScope) => { |
| 10 | const source = scope.branch ?? scope.project; |
| 11 | if (isResource(source)) return `${source.Type}:${source.FQN}`; |
| 12 | if (Effect.isEffect(source) || Output.isOutput(source)) return undefined; |
| 13 | if (typeof scope.branch?.projectId === "string" && typeof scope.branch.branchId === "string") { |
| 14 | return `branch:${scope.branch.projectId}:${scope.branch.branchId}`; |
| 15 | } |
| 16 | if (typeof scope.project?.projectId === "string") return `project:${scope.project.projectId}`; |
| 17 | return undefined; |
| 18 | }; |
| 19 | |
| 20 | /** Unknown scope identity falls back to a managed credential. */ |
| 21 | export const usesInjectedCredentials = ( |
| 22 | host: Pick<ResourceLike, "Type" | "Props" | "Mode"> | undefined, |
| 23 | target: CredentialScope, |
| 24 | defaultMode: ProviderMode, |
| 25 | ) => { |
| 26 | if (host?.Type !== "Neon.Function" || (host.Mode ?? defaultMode) !== "live") return false; |
| 27 | const current: CredentialScope | undefined = host.Props; |
| 28 | if (!current || Effect.isEffect(current) || Output.isOutput(current)) return false; |
| 29 | return ( |
| 30 | (current.branch !== undefined && current.branch === target.branch) || |
| 31 | (current.project !== undefined && current.project === target.project) || |
| 32 | (scopeIdentity(current) !== undefined && scopeIdentity(current) === scopeIdentity(target)) |
| 33 | ); |
| 34 | }; |