@@ -22,6 +22,7 @@ export interface ProviderSettingsFieldModel {
2222 readonly description ?: string | undefined ;
2323 readonly placeholder ?: string | undefined ;
2424 readonly clearWhenEmpty : "omit" | "persist" ;
25+ readonly defaultBooleanValue ?: boolean | undefined ;
2526}
2627
2728function titleizeFieldKey ( key : string ) : string {
@@ -59,6 +60,17 @@ function readProviderSettingsFormSchemaAnnotation(
5960 return Schema . resolveAnnotations ( definition . settingsSchema ) ?. providerSettingsFormSchema ?? { } ;
6061}
6162
63+ function readFieldBooleanDefault (
64+ fieldSchema : ProviderClientDefinition [ "settingsSchema" ] [ "fields" ] [ string ] ,
65+ ) : boolean | undefined {
66+ try {
67+ const decoded = Schema . decodeUnknownSync ( fieldSchema as Schema . Decoder < unknown > ) ( undefined ) ;
68+ return typeof decoded === "boolean" ? decoded : undefined ;
69+ } catch {
70+ return undefined ;
71+ }
72+ }
73+
6274export function deriveProviderSettingsFields (
6375 definition : ProviderClientDefinition ,
6476) : ReadonlyArray < ProviderSettingsFieldModel > {
@@ -93,6 +105,9 @@ export function deriveProviderSettingsFields(
93105 ? { placeholder : formAnnotation . placeholder }
94106 : { } ) ,
95107 clearWhenEmpty : formAnnotation . clearWhenEmpty ?? "omit" ,
108+ ...( formAnnotation . control === "switch"
109+ ? { defaultBooleanValue : readFieldBooleanDefault ( fieldSchema ) }
110+ : { } ) ,
96111 } satisfies ProviderSettingsFieldModel ,
97112 ] ;
98113 } ) ;
@@ -104,10 +119,14 @@ export function readProviderConfigString(config: unknown, key: string): string {
104119 return typeof value === "string" ? value : "" ;
105120}
106121
107- export function readProviderConfigBoolean ( config : unknown , key : string ) : boolean {
108- if ( config === null || typeof config !== "object" ) return false ;
122+ export function readProviderConfigBoolean (
123+ config : unknown ,
124+ key : string ,
125+ defaultValue = false ,
126+ ) : boolean {
127+ if ( config === null || typeof config !== "object" ) return defaultValue ;
109128 const value = ( config as Record < string , unknown > ) [ key ] ;
110- return typeof value === "boolean" ? value : false ;
129+ return typeof value === "boolean" ? value : defaultValue ;
111130}
112131
113132export function nextProviderConfigWithFieldValue (
@@ -119,7 +138,8 @@ export function nextProviderConfigWithFieldValue(
119138 config !== null && typeof config === "object" ? { ...( config as Record < string , unknown > ) } : { } ;
120139
121140 if ( typeof value === "boolean" ) {
122- if ( field . clearWhenEmpty === "omit" && value === false ) {
141+ const emptyBooleanValue = field . defaultBooleanValue ?? false ;
142+ if ( field . clearWhenEmpty === "omit" && value === emptyBooleanValue ) {
123143 delete base [ field . key ] ;
124144 } else {
125145 base [ field . key ] = value ;
@@ -188,7 +208,7 @@ function ProviderSettingsFieldRow({
188208 { description }
189209 </ div >
190210 < Switch
191- checked = { readProviderConfigBoolean ( value , field . key ) }
211+ checked = { readProviderConfigBoolean ( value , field . key , field . defaultBooleanValue ) }
192212 onCheckedChange = { ( checked ) =>
193213 onChange ( nextProviderConfigWithFieldValue ( value , field , Boolean ( checked ) ) )
194214 }
0 commit comments