forked from xiangsx/gpt4free-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.ts
105 lines (96 loc) · 2.62 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { existsSync, readFileSync, watch } from 'fs';
import { ModelType, Site } from '../model/base';
export type SiteCfg = {
site: Site;
priority: number;
base_url?: string;
api_key?: string;
label?: string;
proxy?: boolean;
};
// 首先定义配置的数据类型
interface ConfigData {
perplexity: {
cf_debug: boolean;
};
site_map: Partial<Record<ModelType, SiteCfg[]>>;
one_api: {
base_url: string;
api_key: string;
proxy: boolean;
};
cursor: {
primary_model: ModelType;
};
// 当添加新字段时,需要在此处更新类型定义
}
class BaseConfig {
private filePath: string = './run/config.json';
private defaultConfig: ConfigData = {
perplexity: {
cf_debug: false,
},
site_map: {},
one_api: {
base_url: '',
api_key: '',
proxy: false,
}, // Add new fields here, with their default values
cursor: {
primary_model: ModelType.GPT4,
},
};
public config: ConfigData;
constructor() {
// Initialize config as a deep copy of defaultConfig
this.config = JSON.parse(JSON.stringify(this.defaultConfig));
}
load() {
if (!existsSync(this.filePath)) {
// console.log(
// `Configuration file ${this.filePath} not found. Retrying in 5 seconds...`,
// );
setTimeout(() => this.load(), 5000);
return;
}
try {
const rawData = readFileSync(this.filePath, 'utf8');
const fileConfig: Partial<ConfigData> = JSON.parse(rawData);
// Merge defaultConfig and fileConfig
this.config = Object.assign(this.config, this.defaultConfig, fileConfig);
console.log(
'Loaded config from run/config.json successfully!',
JSON.stringify(this.config),
);
} catch (error) {
// console.error(`Error reading or parsing the configuration file ${this.filePath}.`, error);
}
}
watchFile() {
if (!existsSync(this.filePath)) {
// console.log(`Configuration file ${this.filePath} not found. Retrying in 5 seconds...`);
setTimeout(() => this.watchFile(), 5000);
return;
}
let timeoutId: NodeJS.Timeout | null = null;
const debounceDelay = 300;
try {
watch(this.filePath, (event) => {
if (event === 'change') {
if (timeoutId) {
clearTimeout(timeoutId);
}
timeoutId = setTimeout(() => {
console.log(
`Configuration file ${this.filePath} has been changed! Reloading...`,
);
this.load();
}, debounceDelay);
}
});
} catch (e) {
console.error(e);
}
}
}
export const Config = new BaseConfig();