MCPcopy
hub / github.com/CopyTranslator/CopyTranslator / loadFromConfig

Method loadFromConfig

src/common/configParser.ts:153–203  ·  view source on GitHub ↗
(values: Config)

Source from the content-addressed store, hash-verified

151 }
152
153 loadFromConfig(values: Config) {
154 const config: Config = {};
155 for (const key of this.rules.keys()) {
156 let val = values[key];
157 let isValid: boolean = true;
158 const rule = this.getRule(key);
159 if (
160 rule.minimalVersion != undefined &&
161 isLower(values["version"], rule.minimalVersion as string)
162 ) {
163 //config的版本小于这项rule的最低版本,则需要更新为最新的预定义值
164 isValid = false;
165 }
166 isValid = isValid && this.checkValid(key, val);
167 if (!isValid) {
168 //无效的话,就置为默认值
169 val = this.getRule(key).predefined;
170 }
171 config[key] = val;
172 }
173 const providerConfigs = (values["translatorProviders"] || []) as any[];
174 const customIdsFromProviders = providerConfigs.flatMap((provider) => {
175 const providerId = provider && provider.id;
176 const enabledModels =
177 (provider && provider.enabledModels) || [];
178 if (!providerId || !Array.isArray(enabledModels)) {
179 return [];
180 }
181 return enabledModels.map((model: string) => `${providerId}-${model}`);
182 });
183 const customIdsFromConfig = (values["customTranslators"] || []) as string[];
184 const customIdSet = new Set([
185 ...customIdsFromProviders,
186 ...customIdsFromConfig,
187 ]);
188 const filterExistingWithCustom = (engines: any[]) =>
189 Array.from(new Set(engines || [])).filter(
190 (id) => hasTranslator(String(id)) || customIdSet.has(String(id))
191 );
192 const enabled = filterExistingWithCustom(config["translator-enabled"] || []);
193 const activeSet = new Set(
194 getEnabledWithCustomIds(enabled, [...customIdSet])
195 );
196 const cleanGroup = (group: any[]) =>
197 filterExistingWithCustom(group || []).filter((id) => activeSet.has(id));
198 config["translator-enabled"] = enabled;
199 config["translator-cache"] = cleanGroup(config["translator-cache"]);
200 config["translator-compare"] = cleanGroup(config["translator-compare"]);
201 config["translator-double"] = cleanGroup(config["translator-double"]);
202 store.dispatch("setConfig", config);
203 }
204
205 restoreDefault() {
206 for (const [key, rule] of this.rules) {

Callers 1

loadMethod · 0.95

Calls 6

getRuleMethod · 0.95
checkValidMethod · 0.95
isLowerFunction · 0.90
getEnabledWithCustomIdsFunction · 0.90
keysMethod · 0.80
dispatchMethod · 0.80

Tested by

no test coverage detected