This commit is contained in:
josc146
2023-05-17 21:20:41 +08:00
parent 11813454de
commit df8eef5f64
14 changed files with 438 additions and 121 deletions

View File

@@ -1,18 +1,14 @@
import commonStore, {defaultModelConfigs, ModelSourceItem} from './stores/commonStore';
import {ListDirFiles, ReadJson, SaveJson} from '../wailsjs/go/backend_golang/App';
import manifest from '../../manifest.json';
import commonStore, {defaultModelConfigs} from './stores/commonStore';
import {ReadJson} from '../wailsjs/go/backend_golang/App';
import {LocalConfig, refreshModels} from './utils';
export async function startup() {
initCache();
await initConfig();
}
type Cache = {
models: ModelSourceItem[]
}
async function initConfig() {
await ReadJson('config.json').then((configData) => {
await ReadJson('config.json').then((configData: LocalConfig) => {
if (configData.modelSourceManifestList)
commonStore.setModelSourceManifestList(configData.modelSourceManifestList);
if (configData.modelConfigs && Array.isArray(configData.modelConfigs))
@@ -27,80 +23,5 @@ async function initConfig() {
}
async function initCache() {
let cache: Cache = {models: []};
await ReadJson('cache.json').then((cacheData: Cache) => {
cache = cacheData;
}).catch(
async () => {
cache = {models: manifest.models};
await SaveJson('cache.json', cache).catch(() => {
});
}
);
// built-in
commonStore.setModelSourceList(cache.models);
await ListDirFiles(manifest.localModelPath).then((data) => {
cache.models.push(...data.flatMap(d => {
if (!d.isDir && d.name.endsWith('.pth'))
return [{
name: d.name,
size: d.size,
lastUpdated: d.modTime,
isLocal: true
}];
return [];
}));
}).catch(() => {
});
for (let i = 0; i < cache.models.length; i++) {
if (!cache.models[i].lastUpdatedMs)
cache.models[i].lastUpdatedMs = Date.parse(cache.models[i].lastUpdated);
for (let j = i + 1; j < cache.models.length; j++) {
if (!cache.models[j].lastUpdatedMs)
cache.models[j].lastUpdatedMs = Date.parse(cache.models[j].lastUpdated);
if (cache.models[i].name === cache.models[j].name) {
if (cache.models[i].size === cache.models[j].size) {
if (cache.models[i].lastUpdatedMs! < cache.models[j].lastUpdatedMs!) {
cache.models[i] = Object.assign({}, cache.models[i], cache.models[j]);
} else {
cache.models[i] = Object.assign({}, cache.models[j], cache.models[i]);
}
} // else is bad local file
cache.models.splice(j, 1);
j--;
}
}
}
// local files
commonStore.setModelSourceList(cache.models);
await SaveJson('cache.json', cache).catch(() => {
});
const manifestUrls = commonStore.modelSourceManifestList.split(/[,;\n]/);
const requests = manifestUrls.filter(url => url.endsWith('.json')).map(
url => fetch(url, {cache: 'no-cache'}).then(r => r.json()));
await Promise.allSettled(requests)
.then((data: PromiseSettledResult<Cache>[]) => {
cache.models.push(...data.flatMap(d => {
if (d.status === 'fulfilled')
return d.value.models;
return [];
}));
})
.catch(() => {
});
cache.models = cache.models.filter((model, index, self) => {
return model.name.endsWith('.pth')
&& index === self.findIndex(
m => m.name === model.name || (m.SHA256 === model.SHA256 && m.size === model.size));
});
// remote files
commonStore.setModelSourceList(cache.models);
await SaveJson('cache.json', cache).catch(() => {
});
await refreshModels(true);
}