(file = '')
| 26 | } |
| 27 | |
| 28 | export function useCacheModel(file = '') { |
| 29 | const [modelCacheJson, setModelCacheJson] = useState<Record<string, any>[]>([]); |
| 30 | |
| 31 | useEffect(() => { |
| 32 | if (!file) return; |
| 33 | (async () => { |
| 34 | const data = await readJSON(file, { isRoot: true, isList: true }); |
| 35 | setModelCacheJson(data); |
| 36 | })(); |
| 37 | }, [file]); |
| 38 | |
| 39 | const modelCacheSet = async (data: Record<string, any>[], newFile = '') => { |
| 40 | await writeJSON(newFile ? newFile : file, data, { isRoot: true }); |
| 41 | setModelCacheJson(data); |
| 42 | await modelCacheCmd(); |
| 43 | }; |
| 44 | |
| 45 | const modelCacheCmd = async () => { |
| 46 | // Generate the `chat.model.cmd.json` file and refresh the page for the slash command to take effect. |
| 47 | const list = await invoke('cmd_list'); |
| 48 | await writeJSON(CHAT_MODEL_CMD_JSON, { |
| 49 | name: 'ChatGPT CMD', |
| 50 | last_updated: Date.now(), |
| 51 | data: list, |
| 52 | }); |
| 53 | await invoke('window_reload', { label: 'core' }); |
| 54 | await invoke('window_reload', { label: 'tray' }); |
| 55 | }; |
| 56 | |
| 57 | return { modelCacheJson, modelCacheSet, modelCacheCmd }; |
| 58 | } |
no test coverage detected