@@ -150,9 +150,41 @@ def __init__(
150
150
self ._init_config ()
151
151
152
152
def _init_config_schema (self ):
153
- if not os .path .exists (self .schema_path ):
154
- os .makedirs (os .path .dirname (self .schema_path ), exist_ok = True )
155
- shutil .copy (OUR_SCHEMA_PATH , self .schema_path )
153
+ """
154
+ Initializes `config_schema.json` in the user's data dir whenever the
155
+ server extension starts. Users may add custom fields to their config
156
+ schema to insert new keys into the Jupyter AI config.
157
+
158
+ New in v2.31.1: Jupyter AI now merges the user's existing config schema
159
+ with Jupyter AI's config schema on init. This prevents validation errors
160
+ on missing keys when users upgrade Jupyter AI from an older version.
161
+
162
+ TODO v3: Remove the ability for users to provide a custom config schema.
163
+ This feature is entirely unused as far as I am aware, and we need to
164
+ simplify how Jupyter AI handles user configuration in v3 anyways.
165
+ """
166
+
167
+ # ensure the parent directory has been created
168
+ os .makedirs (os .path .dirname (self .schema_path ), exist_ok = True )
169
+
170
+ # read existing_schema
171
+ if os .path .exists (self .schema_path ):
172
+ with open (self .schema_path , encoding = "utf-8" ) as f :
173
+ existing_schema = json .load (f )
174
+ else :
175
+ existing_schema = {}
176
+
177
+ # read default_schema
178
+ with open (OUR_SCHEMA_PATH , encoding = "utf-8" ) as f :
179
+ default_schema = json .load (f )
180
+
181
+ # merge existing_schema into default_schema
182
+ # specifying existing_schema as the second argument ensures that
183
+ # existing_schema always overrides existing keys in default_schema, i.e.
184
+ # this call only adds new keys in default_schema.
185
+ schema = Merger .merge (default_schema , existing_schema )
186
+ with open (self .schema_path , encoding = "utf-8" , mode = "w" ) as f :
187
+ json .dump (schema , f , indent = self .indentation_depth )
156
188
157
189
def _init_validator (self ) -> None :
158
190
with open (OUR_SCHEMA_PATH , encoding = "utf-8" ) as f :
0 commit comments