@@ -8,16 +8,15 @@ import validator from '@rjsf/validator-ajv8';
8
8
import { JSONSchema7 } from 'json-schema' ;
9
9
import React from 'react' ;
10
10
11
- import { instructions } from './instructions' ;
12
11
import baseSettings from './schemas/base.json' ;
13
- import { ProviderSettings } from './schemas' ;
14
- import { IDict } from '../token' ;
12
+ import { IAIProviderRegistry , IDict } from '../token' ;
15
13
16
14
const MD_MIME_TYPE = 'text/markdown' ;
17
15
const STORAGE_NAME = '@jupyterlite/ai:settings' ;
18
16
const INSTRUCTION_CLASS = 'jp-AISettingsInstructions' ;
19
17
20
18
export const aiSettingsRenderer = ( options : {
19
+ providerRegistry : IAIProviderRegistry ;
21
20
rmRegistry ?: IRenderMimeRegistry ;
22
21
} ) : IFormRenderer => {
23
22
return {
@@ -43,6 +42,12 @@ export class AiSettings extends React.Component<
43
42
> {
44
43
constructor ( props : FieldProps ) {
45
44
super ( props ) ;
45
+ if ( ! props . formContext . providerRegistry ) {
46
+ throw new Error (
47
+ 'The provider registry is needed to enable the jupyterlite-ai settings panel'
48
+ ) ;
49
+ }
50
+ this . _providerRegistry = props . formContext . providerRegistry ;
46
51
this . _rmRegistry = props . formContext . rmRegistry ?? null ;
47
52
this . _settings = props . formContext . settings ;
48
53
@@ -53,7 +58,7 @@ export class AiSettings extends React.Component<
53
58
title : 'Provider' ,
54
59
description : 'The AI provider to use for chat and completion' ,
55
60
default : 'None' ,
56
- enum : [ 'None' ] . concat ( Object . keys ( ProviderSettings ) )
61
+ enum : [ 'None' ] . concat ( this . _providerRegistry . providers )
57
62
} ;
58
63
this . _providerSchema = providerSchema as JSONSchema7 ;
59
64
@@ -142,8 +147,10 @@ export class AiSettings extends React.Component<
142
147
private _buildSchema ( ) : JSONSchema7 {
143
148
const schema = JSONExt . deepCopy ( baseSettings ) as any ;
144
149
this . _uiSchema = { } ;
145
- const settingsSchema =
146
- ( ProviderSettings [ this . _provider ] ?. properties as JSONSchema7 ) ?? null ;
150
+ const settingsSchema = this . _providerRegistry . getSettingsSchema (
151
+ this . _provider
152
+ ) ;
153
+
147
154
if ( settingsSchema ) {
148
155
Object . entries ( settingsSchema ) . forEach ( ( [ key , value ] ) => {
149
156
schema . properties [ key ] = value ;
@@ -166,15 +173,15 @@ export class AiSettings extends React.Component<
166
173
* Render the markdown instructions for the current provider.
167
174
*/
168
175
private async _renderInstruction ( ) : Promise < void > {
169
- if ( ! this . _rmRegistry || ! instructions [ this . _provider ] ) {
176
+ let instructions = this . _providerRegistry . getInstructions ( this . _provider ) ;
177
+ if ( ! this . _rmRegistry || ! instructions ) {
170
178
this . setState ( { instruction : null } ) ;
171
179
return ;
172
180
}
173
- let mdStr = instructions [ this . _provider ] ;
174
- mdStr = `---\n\n${ mdStr } \n\n---` ;
181
+ instructions = `---\n\n${ instructions } \n\n---` ;
175
182
const renderer = this . _rmRegistry . createRenderer ( MD_MIME_TYPE ) ;
176
183
const model = this . _rmRegistry . createModel ( {
177
- data : { [ MD_MIME_TYPE ] : mdStr }
184
+ data : { [ MD_MIME_TYPE ] : instructions }
178
185
} ) ;
179
186
await renderer . renderModel ( model ) ;
180
187
this . setState ( { instruction : renderer . node } ) ;
@@ -240,6 +247,7 @@ export class AiSettings extends React.Component<
240
247
) ;
241
248
}
242
249
250
+ private _providerRegistry : IAIProviderRegistry ;
243
251
private _provider : string ;
244
252
private _providerSchema : JSONSchema7 ;
245
253
private _rmRegistry : IRenderMimeRegistry | null ;
0 commit comments