From f5e8dcfbbf0b790248dc31287def6e4c254d4d29 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 12 Mar 2024 23:51:31 -0700 Subject: [PATCH] refac --- src/App.vue | 3 --- src/main.ts | 47 +++++++++++++++++++++++++++++++++++++++------- src/utils/index.ts | 6 ++++++ 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/src/App.vue b/src/App.vue index 549b975..3d9e598 100644 --- a/src/App.vue +++ b/src/App.vue @@ -24,15 +24,12 @@ const submitHandler = () => { onBeforeMount(async () => { - console.log('hi') const res = await window.electron.loadConfig() if (res) { url.value = res.url token.value = res.token } - - }) diff --git a/src/main.ts b/src/main.ts index 74f0cda..4558f9c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -8,13 +8,16 @@ import { import path from "path"; import { keyboard, Key } from "@nut-tree/nut-js"; -import { splitStream } from "./utils"; +import { splitStream, sleep } from "./utils"; keyboard.config.autoDelayMs = 0; +let WEBUI_VERSION: string | null = null; + let config = { url: "", token: "", + model: "", }; // Handle creating/removing shortcuts on Windows when installing/uninstalling. @@ -125,12 +128,6 @@ const generateResponse = async (prompt: string) => { } }; -function sleep(ms) { - return new Promise((resolve) => { - setTimeout(resolve, ms); - }); -} - const shortcutHandler = async () => { console.log("shortcutHandler"); keyboard.config.autoDelayMs = 10; @@ -162,12 +159,48 @@ const shortcutHandler = async () => { } }; +const getVersion = async () => { + if (config.url) { + const res = await fetch(`${config.url}/api/version`, { + method: "GET", + headers: { + "Content-Type": "application/json", + }, + }) + .then(async (res) => { + if (!res.ok) throw await res.json(); + return res.json(); + }) + .catch((err) => { + console.log(err); + return null; + }); + + console.log(res); + + if (res) { + WEBUI_VERSION = res.version; + } else { + WEBUI_VERSION = null; + } + } else { + WEBUI_VERSION = null; + } +}; + // This method will be called when Electron has finished // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs. app .whenReady() .then(() => { + ipcMain.handle("check-connection", async (event, arg) => { + await getVersion(); + return WEBUI_VERSION !== null; + }); + + ipcMain.handle("get-models", (event, arg) => {}); + ipcMain.handle("load-config", (event, arg) => { return config; }); diff --git a/src/utils/index.ts b/src/utils/index.ts index 404fa84..e836efa 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -12,3 +12,9 @@ export const splitStream = (splitOn) => { }, }); }; + +export const sleep = (ms) => { + return new Promise((resolve) => { + setTimeout(resolve, ms); + }); +};