Skip to content

Commit 1d4b923

Browse files
committed
Change the settings script to check the content
1 parent 4b5b107 commit 1d4b923

File tree

6 files changed

+55
-44
lines changed

6 files changed

+55
-44
lines changed

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
"url": "https://github.com/jupyterlite/ai.git"
2828
},
2929
"scripts": {
30-
"build": "node ./scripts/settings-generator.js && jlpm build:lib && jlpm build:labextension:dev",
30+
"build": "jlpm settings:build && jlpm build:lib && jlpm build:labextension:dev",
3131
"build:dev": "jlpm build:lib && jlpm build:labextension:dev",
32-
"build:prod": "node ./scripts/settings-generator.js && jlpm clean && jlpm build:lib:prod && jlpm build:labextension",
32+
"build:prod": "jlpm settings:build && jlpm clean && jlpm build:lib:prod && jlpm build:labextension",
3333
"build:labextension": "jupyter labextension build .",
3434
"build:labextension:dev": "jupyter labextension build --development True .",
3535
"build:lib": "tsc --sourceMap",
@@ -47,6 +47,8 @@
4747
"prettier": "jlpm prettier:base --write --list-different",
4848
"prettier:base": "prettier \"**/*{.ts,.tsx,.js,.jsx,.css,.json,.md}\"",
4949
"prettier:check": "jlpm prettier:base --check",
50+
"settings:build": "node ./scripts/settings-checker.js --generate",
51+
"settings:check": "node ./scripts/settings-checker.js",
5052
"stylelint": "jlpm stylelint:check --fix",
5153
"stylelint:check": "stylelint --cache \"style/**/*.css\"",
5254
"watch": "run-p watch:src watch:labextension",

scripts/settings-generator.js scripts/settings-checker.js

+47-38
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,24 @@ const fs = require('fs');
22
const tsj = require('ts-json-schema-generator');
33
const path = require('path');
44

5-
console.log('Building settings schemas\n');
6-
75
const providersDir = 'src/default_providers';
86

7+
const error = [];
8+
let generate = false;
9+
if (process.argv.length >= 3) {
10+
if (process.argv[2] === '--generate') {
11+
generate = true;
12+
} else {
13+
throw Error(`Argument '${process.argv[2]}' is not valid.`)
14+
}
15+
}
16+
17+
if (generate) {
18+
console.log('Building settings schemas\n');
19+
} else {
20+
console.log('Checking settings schemas\n');
21+
}
22+
923
// Build the langchain BaseLanguageModelParams object
1024
const configBase = {
1125
path: 'node_modules/@langchain/core/dist/language_models/base.d.ts',
@@ -50,9 +64,11 @@ const providers = {
5064

5165
Object.entries(providers).forEach(([name, desc], index) => {
5266
const outputDir = path.join(providersDir, name);
53-
if (!fs.existsSync(outputDir)) {
54-
fs.mkdirSync(outputDir);
67+
const outputPath = path.join(outputDir, 'settings-schema.json');
68+
if (!generate && !fs.existsSync(outputPath)) {
69+
throw Error(`${outputPath} does not exist`);
5570
}
71+
5672
// The configuration doesn't include functions, which may probably not be filled
5773
// from the settings panel.
5874
const config = {
@@ -63,8 +79,6 @@ Object.entries(providers).forEach(([name, desc], index) => {
6379
topRef: false
6480
};
6581

66-
const outputPath = path.join(outputDir, 'settings-schema.json');
67-
6882
const generator = tsj.createGenerator(config);
6983
let schema;
7084

@@ -130,38 +144,33 @@ Object.entries(providers).forEach(([name, desc], index) => {
130144
}
131145
});
132146

133-
// Write JSON file.
134-
const schemaString = JSON.stringify(schema, null, 2);
135-
fs.writeFile(outputPath, schemaString, err => {
136-
if (err) {
137-
throw err;
147+
let schemaString = JSON.stringify(schema, null, 2);
148+
schemaString += '\n';
149+
if (generate) {
150+
if (!fs.existsSync(outputDir)) {
151+
fs.mkdirSync(outputDir);
138152
}
139-
});
153+
// Write JSON file.
154+
fs.writeFile(outputPath, schemaString, err => {
155+
if (err) {
156+
throw err;
157+
}
158+
});
159+
} else {
160+
const currentContent = fs.readFileSync(outputPath, {encoding: 'utf-8'});
161+
if (currentContent !== schemaString) {
162+
error.push(`The content of ${name} settings does not match with the generated one.`)
163+
}
164+
}
140165
});
141166

142-
// // Build the index.ts file
143-
// const indexContent = ["import { IDict } from '../../tokens';", ''];
144-
// Object.keys(providers).forEach(name => {
145-
// indexContent.push(`import ${name} from './_generated/${name}.json';`);
146-
// });
147-
148-
// indexContent.push('', 'const ProviderSettings: IDict<any> = {');
149-
150-
// Object.keys(providers).forEach((name, index) => {
151-
// indexContent.push(
152-
// ` ${name}` + (index < Object.keys(providers).length - 1 ? ',' : '')
153-
// );
154-
// });
155-
// indexContent.push('};', '', 'export { ProviderSettings };', '');
156-
// fs.writeFile(
157-
// path.join(providersDir, 'index.ts'),
158-
// indexContent.join('\n'),
159-
// err => {
160-
// if (err) {
161-
// throw err;
162-
// }
163-
// }
164-
// );
165-
166-
console.log('Settings schemas built\n');
167-
console.log('=====================\n');
167+
if (generate) {
168+
console.log('Settings schemas built\n');
169+
console.log('=====================\n');
170+
} else if (error.length) {
171+
console.error(error.join('\n'));
172+
console.error('Please run "jlpm settings:build" to fix it')
173+
throw Error('Errors in settings schemas');
174+
} else {
175+
console.log('Settings schemas checked successfully\n');
176+
}

src/default_providers/Anthropic/settings-schema.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@
6767
"additionalProperties": false,
6868
"description": "Input to AnthropicChat class.",
6969
"definitions": {}
70-
}
70+
}

src/default_providers/ChromeAI/settings-schema.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818
},
1919
"additionalProperties": false,
2020
"definitions": {}
21-
}
21+
}

src/default_providers/MistralAI/settings-schema.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,4 @@
7272
"additionalProperties": false,
7373
"description": "Input to chat model class.",
7474
"definitions": {}
75-
}
75+
}

src/default_providers/OpenAI/settings-schema.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -665,4 +665,4 @@
665665
}
666666
},
667667
"definitions": {}
668-
}
668+
}

0 commit comments

Comments
 (0)