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, 03:37 UTC
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Schema from "effect/Schema"; |
| 3 | import * as SchemaTransformation from "effect/SchemaTransformation"; |
| 4 | import { TrimmedNonEmptyString } from "./baseSchemas.ts"; |
| 5 | import { SecretReference } from "./secretReference.ts"; |
| 6 | |
| 7 | const PROVIDER_SLUG_MAX_CHARS = 64; |
| 8 | const PROVIDER_SLUG_PATTERN = /^[a-zA-Z][a-zA-Z0-9_-]*$/; |
| 9 | const ENVIRONMENT_VARIABLE_NAME_MAX_CHARS = 128; |
| 10 | const ENVIRONMENT_VARIABLE_NAME_PATTERN = /^[a-zA-Z_][a-zA-Z0-9_]*$/; |
| 11 | |
| 12 | const slugSchema = TrimmedNonEmptyString.check( |
| 13 | Schema.isMaxLength(PROVIDER_SLUG_MAX_CHARS), |
| 14 | Schema.isPattern(PROVIDER_SLUG_PATTERN), |
| 15 | ); |
| 16 | |
| 17 | export const ProviderDriverKind = slugSchema.pipe(Schema.brand("ProviderDriverKind")); |
| 18 | export type ProviderDriverKind = typeof ProviderDriverKind.Type; |
| 19 | |
| 20 | export const isProviderDriverKind = Schema.is(ProviderDriverKind); |
| 21 | |
| 22 | export const ProviderInstanceId = slugSchema.pipe(Schema.brand("ProviderInstanceId")); |
| 23 | export type ProviderInstanceId = typeof ProviderInstanceId.Type; |
| 24 | |
| 25 | export const ProviderInstanceEnvironmentVariableName = TrimmedNonEmptyString.check( |
| 26 | Schema.isMaxLength(ENVIRONMENT_VARIABLE_NAME_MAX_CHARS), |
| 27 | Schema.isPattern(ENVIRONMENT_VARIABLE_NAME_PATTERN), |
| 28 | ); |
| 29 | export type ProviderInstanceEnvironmentVariableName = |
| 30 | typeof ProviderInstanceEnvironmentVariableName.Type; |
| 31 | |
| 32 | const ProviderInstanceEnvironmentVariableValue = Schema.Struct({ |
| 33 | name: ProviderInstanceEnvironmentVariableName, |
| 34 | value: Schema.Union([Schema.String, SecretReference]), |
| 35 | sensitive: Schema.Boolean, |
| 36 | valueRedacted: Schema.optionalKey(Schema.Boolean), |
| 37 | }); |
| 38 | |
| 39 | // A reference is encoded beside an empty value so older servers and clients still decode it. |
| 40 | const StoredProviderInstanceEnvironmentVariable = Schema.Struct({ |
| 41 | name: ProviderInstanceEnvironmentVariableName, |
| 42 | value: Schema.String.pipe(Schema.withDecodingDefault(Effect.succeed(""))), |
| 43 | sensitive: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))), |
| 44 | valueRedacted: Schema.optionalKey(Schema.Boolean), |
| 45 | secret: Schema.optionalKey(SecretReference), |
| 46 | }); |
| 47 | |
| 48 | export const ProviderInstanceEnvironmentVariable = StoredProviderInstanceEnvironmentVariable.pipe( |
| 49 | Schema.decodeTo( |
| 50 | Schema.toType(ProviderInstanceEnvironmentVariableValue), |
| 51 | SchemaTransformation.transform< |
| 52 | typeof ProviderInstanceEnvironmentVariableValue.Type, |
| 53 | typeof StoredProviderInstanceEnvironmentVariable.Type |
| 54 | >({ |
| 55 | decode: ({ secret, ...variable }) => |
| 56 | secret === undefined ? variable : { ...variable, value: secret, sensitive: true }, |
| 57 | encode: ({ value, ...variable }) => |
| 58 | typeof value === "string" |
| 59 | ? { ...variable, value } |
| 60 | : { ...variable, value: "", secret: value }, |
| 61 | }), |
| 62 | ), |
| 63 | ); |
| 64 | export type ProviderInstanceEnvironmentVariable = typeof ProviderInstanceEnvironmentVariable.Type; |
| 65 | |
| 66 | export const ProviderInstanceEnvironment = Schema.Array(ProviderInstanceEnvironmentVariable); |
| 67 | export type ProviderInstanceEnvironment = typeof ProviderInstanceEnvironment.Type; |
| 68 | |
| 69 | /** What drivers receive: every secret reference has been read into a plain value. */ |
| 70 | export type ResolvedProviderInstanceEnvironment = ReadonlyArray< |
| 71 | Omit<ProviderInstanceEnvironmentVariable, "value"> & { readonly value: string } |
| 72 | >; |
| 73 | |
| 74 | /** Drivers decode their own config; unknown-driver envelopes round-trip verbatim across versions. */ |
| 75 | export const ProviderInstanceConfig = Schema.Struct({ |
| 76 | driver: ProviderDriverKind, |
| 77 | displayName: Schema.optional(TrimmedNonEmptyString), |
| 78 | accentColor: Schema.optional(TrimmedNonEmptyString), |
| 79 | environment: Schema.optionalKey(ProviderInstanceEnvironment), |
| 80 | enabled: Schema.optionalKey(Schema.Boolean), |
| 81 | config: Schema.optionalKey(Schema.Unknown), |
| 82 | }); |
| 83 | export type ProviderInstanceConfig = typeof ProviderInstanceConfig.Type; |
| 84 | |
| 85 | export const ProviderInstanceMutation = Schema.Union([ |
| 86 | Schema.Struct({ |
| 87 | operation: Schema.Literal("create"), |
| 88 | instanceId: ProviderInstanceId, |
| 89 | instance: ProviderInstanceConfig, |
| 90 | }), |
| 91 | Schema.Struct({ |
| 92 | operation: Schema.Literal("upsert"), |
| 93 | instanceId: ProviderInstanceId, |
| 94 | instance: ProviderInstanceConfig, |
| 95 | }), |
| 96 | Schema.Struct({ |
| 97 | operation: Schema.Literal("remove"), |
| 98 | instanceId: ProviderInstanceId, |
| 99 | }), |
| 100 | ]); |
| 101 | export type ProviderInstanceMutation = typeof ProviderInstanceMutation.Type; |
| 102 | |
| 103 | export const ProviderInstanceConfigMap = Schema.Record(ProviderInstanceId, ProviderInstanceConfig); |
| 104 | export type ProviderInstanceConfigMap = typeof ProviderInstanceConfigMap.Type; |
| 105 | |
| 106 | /** Driver-kind ids keep stored selections routable without migration. */ |
| 107 | export const defaultInstanceIdForDriver = (driver: ProviderDriverKind): ProviderInstanceId => |
| 108 | ProviderInstanceId.make(driver); |