The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Schema from "effect/Schema"; |
| 3 | import * as SchemaAST from "effect/SchemaAST"; |
| 4 | |
| 5 | import { TrimmedString } from "./baseSchemas.ts"; |
| 6 | import { SecretReference } from "./secretReference.ts"; |
| 7 | |
| 8 | /** |
| 9 | * A credential setting: the literal, which is kept in the secret store and redacted for |
| 10 | * clients, or the secret reference to read it from, which is not itself a secret and is |
| 11 | * stored and shown as written. Typing a settings field with it is the whole declaration: |
| 12 | * forms offer a secret provider for it and the server reads the secret for its consumer. |
| 13 | */ |
| 14 | export const SettingsSecretValue = Schema.Union([TrimmedString, SecretReference]).pipe( |
| 15 | Schema.annotateKey({ settingsSecret: true, settingsSecretReference: true }), |
| 16 | ); |
| 17 | export type SettingsSecretValue = typeof SettingsSecretValue.Type; |
| 18 | |
| 19 | export const isSecretReference = Schema.is(SecretReference); |
| 20 | |
| 21 | /** Whether a reference appears anywhere in `value`, such as a settings patch or an RPC payload. */ |
| 22 | export function containsSecretReference(value: unknown): boolean { |
| 23 | if (value === null || typeof value !== "object") return false; |
| 24 | if (isSecretReference(value)) return true; |
| 25 | return Object.values(value).some(containsSecretReference); |
| 26 | } |
| 27 | |
| 28 | /** The literal a credential setting holds, or `""` when it names a secret instead. */ |
| 29 | export function settingsSecretLiteral(value: SettingsSecretValue | undefined): string { |
| 30 | return typeof value === "string" ? value : ""; |
| 31 | } |
| 32 | |
| 33 | export class SettingsSecretUnresolvedError extends Schema.TaggedError<SettingsSecretUnresolvedError>()( |
| 34 | "SettingsSecretUnresolvedError", |
| 35 | { provider: Schema.String }, |
| 36 | ) { |
| 37 | override get message(): string { |
| 38 | return `Could not read the credential from the secret provider "${this.provider}". Unlock or fix it in Settings, then try again.`; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * The literal a credential setting holds once its secret is resolved. A reference still in place |
| 44 | * could not be read, and fails instead of passing for a missing token, so callers that would fall |
| 45 | * back to another credential cannot silently switch accounts. |
| 46 | */ |
| 47 | export const requireSettingsSecretLiteral = ( |
| 48 | value: SettingsSecretValue | undefined, |
| 49 | secretProviders: Readonly<Record<string, { readonly label: string } | undefined>>, |
| 50 | ): Effect.Effect<string, SettingsSecretUnresolvedError> => |
| 51 | isSecretReference(value) |
| 52 | ? Effect.fail( |
| 53 | new SettingsSecretUnresolvedError({ |
| 54 | provider: |
| 55 | (Object.hasOwn(secretProviders, value.provider) |
| 56 | ? secretProviders[value.provider]?.label.trim() |
| 57 | : undefined) || value.provider, |
| 58 | }), |
| 59 | ) |
| 60 | : Effect.succeed(value ?? ""); |
| 61 | |
| 62 | export function acceptsSecretReference(schema: Schema.Top): boolean { |
| 63 | return Schema.resolveAnnotationsKey(schema)?.settingsSecretReference === true; |
| 64 | } |
| 65 | |
| 66 | const declaresSecretReference = (ast: SchemaAST.AST) => |
| 67 | ast.context?.annotations?.settingsSecretReference === true; |
| 68 | |
| 69 | interface SecretReferenceField { |
| 70 | readonly field: string; |
| 71 | readonly keyed: boolean; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Fields of a settings struct typed `SettingsSecretValue`, or a record of it such as one value |
| 76 | * per host; any other schema has none. |
| 77 | */ |
| 78 | function secretReferenceSettingsFields(schema: Schema.Top): ReadonlyArray<SecretReferenceField> { |
| 79 | if (!SchemaAST.isObjects(schema.ast)) return []; |
| 80 | return schema.ast.propertySignatures.flatMap<SecretReferenceField>(({ name, type }) => { |
| 81 | if (typeof name !== "string") return []; |
| 82 | if (declaresSecretReference(type)) return [{ field: name, keyed: false }]; |
| 83 | return SchemaAST.isObjects(type) && |
| 84 | type.indexSignatures.some((signature) => declaresSecretReference(signature.type)) |
| 85 | ? [{ field: name, keyed: true }] |
| 86 | : []; |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | /** Where a reference sits: a field, or one key of a record field. */ |
| 91 | export interface SettingsSecretSlot { |
| 92 | readonly field: string; |
| 93 | readonly key: string | undefined; |
| 94 | } |
| 95 | |
| 96 | interface SettingsSecretReference extends SettingsSecretSlot { |
| 97 | readonly reference: SecretReference; |
| 98 | } |
| 99 | |
| 100 | /** Each declared reference slot of `settings` that holds a reference, in field order. */ |
| 101 | export function secretReferencesInSettings( |
| 102 | schema: Schema.Top, |
| 103 | settings: unknown, |
| 104 | ): ReadonlyArray<SettingsSecretReference> { |
| 105 | if (settings === null || typeof settings !== "object") return []; |
| 106 | const values = settings as Readonly<Record<string, unknown>>; |
| 107 | return secretReferenceSettingsFields(schema).flatMap<SettingsSecretReference>( |
| 108 | ({ field, keyed }) => { |
| 109 | const value = values[field]; |
| 110 | if (!keyed) |
| 111 | return isSecretReference(value) ? [{ field, key: undefined, reference: value }] : []; |
| 112 | if (value === null || typeof value !== "object") return []; |
| 113 | return Object.entries(value).flatMap(([key, entry]) => |
| 114 | isSecretReference(entry) ? [{ field, key, reference: entry }] : [], |
| 115 | ); |
| 116 | }, |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | /** `settings` with each slot set to its value, keeping the other keys of a record field. */ |
| 121 | export function withSettingsSecrets<Settings extends Readonly<Record<string, unknown>>>( |
| 122 | settings: Settings, |
| 123 | slots: ReadonlyArray<SettingsSecretSlot & { readonly value: string }>, |
| 124 | ): Settings { |
| 125 | const next: Record<string, unknown> = { ...settings }; |
| 126 | for (const { field, key, value } of slots) { |
| 127 | next[field] = key === undefined ? value : { ...(next[field] as object), [key]: value }; |
| 128 | } |
| 129 | return next as Settings; |
| 130 | } |
| 131 | |
| 132 | export const SecretSourceCheckResult = Schema.Union([ |
| 133 | Schema.TaggedStruct("Resolved", {}), |
| 134 | Schema.TaggedStruct("Failed", { detail: Schema.String }), |
| 135 | ]); |
| 136 | export type SecretSourceCheckResult = typeof SecretSourceCheckResult.Type; |