improve refreshRemoteModels

This commit is contained in:
josc146 2023-12-22 14:44:27 +08:00
parent 2e0a542f33
commit 280194647c
4 changed files with 31 additions and 8 deletions

View File

@ -18,6 +18,7 @@
"file-saver": "^2.0.5", "file-saver": "^2.0.5",
"html-midi-player": "^1.5.0", "html-midi-player": "^1.5.0",
"i18next": "^22.4.15", "i18next": "^22.4.15",
"lodash-es": "^4.17.21",
"mobx": "^6.9.0", "mobx": "^6.9.0",
"mobx-react-lite": "^3.4.3", "mobx-react-lite": "^3.4.3",
"pdfjs-dist": "^4.0.189", "pdfjs-dist": "^4.0.189",
@ -40,6 +41,7 @@
}, },
"devDependencies": { "devDependencies": {
"@types/file-saver": "^2.0.7", "@types/file-saver": "^2.0.7",
"@types/lodash-es": "^4.17.12",
"@types/react": "^18.2.6", "@types/react": "^18.2.6",
"@types/react-beautiful-dnd": "^13.1.4", "@types/react-beautiful-dnd": "^13.1.4",
"@types/react-dom": "^18.2.4", "@types/react-dom": "^18.2.4",
@ -2533,6 +2535,21 @@
"hoist-non-react-statics": "^3.3.0" "hoist-non-react-statics": "^3.3.0"
} }
}, },
"node_modules/@types/lodash": {
"version": "4.14.202",
"resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.202.tgz",
"integrity": "sha512-OvlIYQK9tNneDlS0VN54LLd5uiPCBOp7gS5Z0f1mjoJYBrtStzgmJBxONW3U6OZqdtNzZPmn9BS/7WI7BFFcFQ==",
"dev": true
},
"node_modules/@types/lodash-es": {
"version": "4.17.12",
"resolved": "https://registry.npmjs.org/@types/lodash-es/-/lodash-es-4.17.12.tgz",
"integrity": "sha512-0NgftHUcV4v34VhXm8QBSftKVXtbkBG3ViCjs6+eJ5a6y6Mi/jiFGPc1sC7QK+9BFhWrURE3EOggmWaSxL9OzQ==",
"dev": true,
"dependencies": {
"@types/lodash": "*"
}
},
"node_modules/@types/long": { "node_modules/@types/long": {
"version": "4.0.2", "version": "4.0.2",
"resolved": "https://registry.npmjs.org/@types/long/-/long-4.0.2.tgz", "resolved": "https://registry.npmjs.org/@types/long/-/long-4.0.2.tgz",
@ -4210,6 +4227,11 @@
"integrity": "sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==", "integrity": "sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==",
"dev": true "dev": true
}, },
"node_modules/lodash-es": {
"version": "4.17.21",
"resolved": "https://registry.npmjs.org/lodash-es/-/lodash-es-4.17.21.tgz",
"integrity": "sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw=="
},
"node_modules/long": { "node_modules/long": {
"version": "4.0.0", "version": "4.0.0",
"resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz", "resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz",

View File

@ -19,6 +19,7 @@
"file-saver": "^2.0.5", "file-saver": "^2.0.5",
"html-midi-player": "^1.5.0", "html-midi-player": "^1.5.0",
"i18next": "^22.4.15", "i18next": "^22.4.15",
"lodash-es": "^4.17.21",
"mobx": "^6.9.0", "mobx": "^6.9.0",
"mobx-react-lite": "^3.4.3", "mobx-react-lite": "^3.4.3",
"pdfjs-dist": "^4.0.189", "pdfjs-dist": "^4.0.189",
@ -41,6 +42,7 @@
}, },
"devDependencies": { "devDependencies": {
"@types/file-saver": "^2.0.7", "@types/file-saver": "^2.0.7",
"@types/lodash-es": "^4.17.12",
"@types/react": "^18.2.6", "@types/react": "^18.2.6",
"@types/react-beautiful-dnd": "^13.1.4", "@types/react-beautiful-dnd": "^13.1.4",
"@types/react-dom": "^18.2.4", "@types/react-dom": "^18.2.4",

View File

@ -27,6 +27,7 @@ import logo from '../assets/images/logo.png';
import { Preset } from '../types/presets'; import { Preset } from '../types/presets';
import { botName, Conversation, MessageType, userName } from '../types/chat'; import { botName, Conversation, MessageType, userName } from '../types/chat';
import { v4 as uuid } from 'uuid'; import { v4 as uuid } from 'uuid';
import { findLastIndex } from 'lodash-es';
export type Cache = { export type Cache = {
version: string version: string
@ -145,7 +146,7 @@ function initLastUnfinishedModelDownloads() {
export async function refreshRemoteModels(cache: { export async function refreshRemoteModels(cache: {
models: ModelSourceItem[] models: ModelSourceItem[]
}) { }, filter: boolean = true, initUnfinishedModels: boolean = false) {
const manifestUrls = commonStore.modelSourceManifestList.split(/[,;\n]/); const manifestUrls = commonStore.modelSourceManifestList.split(/[,;\n]/);
const requests = manifestUrls.filter(url => url.endsWith('.json')).map( const requests = manifestUrls.filter(url => url.endsWith('.json')).map(
url => fetch(url, { cache: 'no-cache' }).then(r => r.json())); url => fetch(url, { cache: 'no-cache' }).then(r => r.json()));
@ -162,18 +163,16 @@ export async function refreshRemoteModels(cache: {
}); });
cache.models = cache.models.filter((model, index, self) => { cache.models = cache.models.filter((model, index, self) => {
return modelSuffix.some((ext => model.name.endsWith(ext))) return modelSuffix.some((ext => model.name.endsWith(ext)))
&& index === self.findIndex( && index === findLastIndex(self,
m => m.name === model.name || (m.SHA256 && m.SHA256 === model.SHA256 && m.size === model.size)); m => m.name === model.name || (!!m.SHA256 && m.SHA256 === model.SHA256 && m.size === model.size));
});
commonStore.setModelSourceList(cache.models);
await saveCache().catch(() => {
}); });
await refreshLocalModels(cache, filter, initUnfinishedModels);
} }
export const refreshModels = async (readCache: boolean = false, initUnfinishedModels: boolean = false) => { export const refreshModels = async (readCache: boolean = false, initUnfinishedModels: boolean = false) => {
const cache = await refreshBuiltInModels(readCache); const cache = await refreshBuiltInModels(readCache);
await refreshLocalModels(cache, false, initUnfinishedModels); await refreshLocalModels(cache, false, initUnfinishedModels);
await refreshRemoteModels(cache); await refreshRemoteModels(cache, false, initUnfinishedModels);
}; };
export const getStrategy = (modelConfig: ModelConfig | undefined = undefined) => { export const getStrategy = (modelConfig: ModelConfig | undefined = undefined) => {

View File

@ -12,7 +12,7 @@ const vendor = [
'mobx', 'mobx-react-lite', 'mobx', 'mobx-react-lite',
'i18next', 'react-i18next', 'i18next', 'react-i18next',
'usehooks-ts', 'react-toastify', 'usehooks-ts', 'react-toastify',
'classnames' 'classnames', 'lodash-es'
]; ];
const embedded = [ const embedded = [