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 { |
| 2 | SecretProviderId, |
| 3 | SecretProviderKind, |
| 4 | SecretProviderLabel, |
| 5 | type SecretProviderConfig, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import { |
| 8 | OnePasswordSecretProviderConfig, |
| 9 | type OnePasswordAccount, |
| 10 | } from "@t3tools/secret-provider-onepassword/shared/definition"; |
| 11 | import * as Option from "effect/Option"; |
| 12 | import * as Result from "effect/Result"; |
| 13 | import * as Schema from "effect/Schema"; |
| 14 | import { msg } from "gt-react"; |
| 15 | |
| 16 | import { m, type MessageResolver } from "../../i18n/messages"; |
| 17 | |
| 18 | const decodeSecretProviderId = Schema.decodeOption(SecretProviderId); |
| 19 | const decodeOnePasswordConfig = Schema.decodeUnknownOption(OnePasswordSecretProviderConfig); |
| 20 | |
| 21 | export type ClassifiedSecretProvider = |
| 22 | | { readonly kind: "one_password"; readonly account: OnePasswordAccount } |
| 23 | | { readonly kind: "unsupported" }; |
| 24 | |
| 25 | /** Unknown kinds and configs that no longer decode are shown read-only; removal stays available. */ |
| 26 | export function classifySecretProvider(provider: SecretProviderConfig): ClassifiedSecretProvider { |
| 27 | if (provider.kind !== "one_password") return { kind: "unsupported" }; |
| 28 | const decoded = decodeOnePasswordConfig(provider.config); |
| 29 | return decoded._tag === "Some" |
| 30 | ? { kind: "one_password", account: decoded.value.account } |
| 31 | : { kind: "unsupported" }; |
| 32 | } |
| 33 | |
| 34 | /** Stable and readable in settings.json, so re-adding a provider under the same name restores its variables. */ |
| 35 | function secretProviderIdFromName(name: string): SecretProviderId | null { |
| 36 | const slug = name |
| 37 | .toLowerCase() |
| 38 | .replace(/[^a-z0-9]+/g, "-") |
| 39 | .replace(/^-|-$/g, "") |
| 40 | .slice(0, 64) |
| 41 | .replace(/-$/, ""); |
| 42 | return Option.getOrNull(decodeSecretProviderId(slug)); |
| 43 | } |
| 44 | |
| 45 | export interface SecretProviderDraft { |
| 46 | readonly name: string; |
| 47 | readonly account: string; |
| 48 | } |
| 49 | |
| 50 | export function draftFromSecretProvider(provider: SecretProviderConfig): SecretProviderDraft { |
| 51 | const classified = classifySecretProvider(provider); |
| 52 | return { |
| 53 | name: provider.label, |
| 54 | account: classified.kind === "one_password" ? classified.account : "", |
| 55 | }; |
| 56 | } |
| 57 | |
| 58 | export type SecretProviderTarget = |
| 59 | | { readonly mode: "add" } |
| 60 | | { readonly mode: "edit"; readonly id: SecretProviderId }; |
| 61 | |
| 62 | export type SecretProviderDraftIssues = { |
| 63 | readonly [Field in keyof SecretProviderDraft]?: string; |
| 64 | }; |
| 65 | |
| 66 | function decodeField<S extends Schema.ConstraintDecoder<unknown>>( |
| 67 | schema: S, |
| 68 | value: string, |
| 69 | emptyMessage: string, |
| 70 | ): Result.Result<S["Type"], string> { |
| 71 | const trimmed = value.trim(); |
| 72 | if (trimmed.length === 0) return Result.fail(emptyMessage); |
| 73 | return Result.mapError(Schema.decodeResult(schema)(trimmed), (error) => error.message); |
| 74 | } |
| 75 | |
| 76 | /** The settings entry a draft adds or replaces, or every field that blocks it, one message each. */ |
| 77 | export function secretProviderFromDraft( |
| 78 | draft: SecretProviderDraft, |
| 79 | existingIds: ReadonlySet<string>, |
| 80 | target: SecretProviderTarget, |
| 81 | resolve: MessageResolver = m, |
| 82 | ): Result.Result< |
| 83 | { readonly id: SecretProviderId; readonly config: SecretProviderConfig }, |
| 84 | SecretProviderDraftIssues |
| 85 | > { |
| 86 | const label = decodeField(SecretProviderLabel, draft.name, resolve(msg("Enter a name."))); |
| 87 | const account = decodeField( |
| 88 | OnePasswordSecretProviderConfig.fields.account, |
| 89 | draft.account, |
| 90 | resolve(msg("Enter the 1Password account.")), |
| 91 | ); |
| 92 | |
| 93 | let id: SecretProviderId | null = null; |
| 94 | let nameIssue: string | undefined; |
| 95 | if (Result.isFailure(label)) { |
| 96 | nameIssue = label.failure; |
| 97 | } else { |
| 98 | id = target.mode === "edit" ? target.id : secretProviderIdFromName(label.success); |
| 99 | if (id === null) nameIssue = resolve(msg("The name needs at least one letter or digit.")); |
| 100 | else if (target.mode === "add" && existingIds.has(id)) |
| 101 | nameIssue = resolve(msg("A secret provider with this name exists.")); |
| 102 | } |
| 103 | |
| 104 | if ( |
| 105 | id === null || |
| 106 | nameIssue !== undefined || |
| 107 | Result.isFailure(label) || |
| 108 | Result.isFailure(account) |
| 109 | ) { |
| 110 | return Result.fail({ |
| 111 | ...(nameIssue === undefined ? {} : { name: nameIssue }), |
| 112 | ...(Result.isFailure(account) ? { account: account.failure } : {}), |
| 113 | }); |
| 114 | } |
| 115 | return Result.succeed({ |
| 116 | id, |
| 117 | config: { |
| 118 | kind: SecretProviderKind.make("one_password"), |
| 119 | label: label.success, |
| 120 | config: { account: account.success }, |
| 121 | }, |
| 122 | }); |
| 123 | } |