2023-12-23 18:39:06 +00:00
|
|
|
import {
|
|
|
|
FETCH_COMMIT_URL,
|
|
|
|
FETCH_TAG_URL,
|
|
|
|
ModelProvider,
|
|
|
|
StoreKey,
|
|
|
|
} from "../constant";
|
2023-06-13 16:37:42 +00:00
|
|
|
import { getClientConfig } from "../config/client";
|
2023-09-10 16:20:23 +00:00
|
|
|
import { createPersistStore } from "../utils/store";
|
2023-10-03 01:08:11 +00:00
|
|
|
import ChatGptIcon from "../icons/chatgpt.png";
|
2023-10-03 19:10:26 +00:00
|
|
|
import Locale from "../locales";
|
2023-12-23 18:39:06 +00:00
|
|
|
import { use } from "react";
|
|
|
|
import { useAppConfig } from ".";
|
|
|
|
import { ClientApi } from "../client/api";
|
2023-03-23 17:00:33 +00:00
|
|
|
|
2023-04-17 15:12:27 +00:00
|
|
|
const ONE_MINUTE = 60 * 1000;
|
2023-10-03 01:08:11 +00:00
|
|
|
const isApp = !!getClientConfig()?.isApp;
|
2023-04-17 15:12:27 +00:00
|
|
|
|
2023-06-29 16:26:03 +00:00
|
|
|
function formatVersionDate(t: string) {
|
|
|
|
const d = new Date(+t);
|
|
|
|
const year = d.getUTCFullYear();
|
|
|
|
const month = d.getUTCMonth() + 1;
|
|
|
|
const day = d.getUTCDate();
|
|
|
|
|
|
|
|
return [
|
|
|
|
year.toString(),
|
|
|
|
month.toString().padStart(2, "0"),
|
|
|
|
day.toString().padStart(2, "0"),
|
|
|
|
].join("");
|
|
|
|
}
|
|
|
|
|
2023-09-10 16:20:23 +00:00
|
|
|
type VersionType = "date" | "tag";
|
|
|
|
|
|
|
|
async function getVersion(type: VersionType) {
|
2023-06-29 16:26:03 +00:00
|
|
|
if (type === "date") {
|
|
|
|
const data = (await (await fetch(FETCH_COMMIT_URL)).json()) as {
|
|
|
|
commit: {
|
|
|
|
author: { name: string; date: string };
|
|
|
|
};
|
|
|
|
sha: string;
|
|
|
|
}[];
|
|
|
|
const remoteCommitTime = data[0].commit.author.date;
|
|
|
|
const remoteId = new Date(remoteCommitTime).getTime().toString();
|
|
|
|
return remoteId;
|
|
|
|
} else if (type === "tag") {
|
|
|
|
const data = (await (await fetch(FETCH_TAG_URL)).json()) as {
|
|
|
|
commit: { sha: string; url: string };
|
|
|
|
name: string;
|
|
|
|
}[];
|
|
|
|
return data.at(0)?.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-10 16:20:23 +00:00
|
|
|
export const useUpdateStore = createPersistStore(
|
|
|
|
{
|
|
|
|
versionType: "tag" as VersionType,
|
|
|
|
lastUpdate: 0,
|
|
|
|
version: "unknown",
|
|
|
|
remoteVersion: "",
|
|
|
|
used: 0,
|
|
|
|
subscription: 0,
|
|
|
|
|
|
|
|
lastUpdateUsage: 0,
|
|
|
|
},
|
|
|
|
(set, get) => ({
|
|
|
|
formatVersion(version: string) {
|
|
|
|
if (get().versionType === "date") {
|
|
|
|
version = formatVersionDate(version);
|
|
|
|
}
|
|
|
|
return version;
|
|
|
|
},
|
2023-04-17 15:12:27 +00:00
|
|
|
|
2023-09-10 16:20:23 +00:00
|
|
|
async getLatestVersion(force = false) {
|
|
|
|
const versionType = get().versionType;
|
|
|
|
let version =
|
|
|
|
versionType === "date"
|
|
|
|
? getClientConfig()?.commitDate
|
|
|
|
: getClientConfig()?.version;
|
2023-04-10 18:54:31 +00:00
|
|
|
|
2023-09-10 16:20:23 +00:00
|
|
|
set(() => ({ version }));
|
2023-06-29 16:26:03 +00:00
|
|
|
|
2023-09-10 16:20:23 +00:00
|
|
|
const shouldCheck = Date.now() - get().lastUpdate > 2 * 60 * ONE_MINUTE;
|
|
|
|
if (!force && !shouldCheck) return;
|
2023-03-23 17:00:33 +00:00
|
|
|
|
2023-09-10 16:20:23 +00:00
|
|
|
set(() => ({
|
|
|
|
lastUpdate: Date.now(),
|
|
|
|
}));
|
2023-03-23 17:00:33 +00:00
|
|
|
|
2023-09-10 16:20:23 +00:00
|
|
|
try {
|
|
|
|
const remoteId = await getVersion(versionType);
|
2023-04-17 17:34:12 +00:00
|
|
|
set(() => ({
|
2023-09-10 16:20:23 +00:00
|
|
|
remoteVersion: remoteId,
|
2023-04-17 17:34:12 +00:00
|
|
|
}));
|
2023-10-03 01:08:11 +00:00
|
|
|
if (window.__TAURI__?.notification && isApp) {
|
|
|
|
// Check if notification permission is granted
|
2023-12-23 18:15:30 +00:00
|
|
|
await window.__TAURI__?.notification
|
|
|
|
.isPermissionGranted()
|
|
|
|
.then((granted) => {
|
|
|
|
if (!granted) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// Request permission to show notifications
|
|
|
|
window.__TAURI__?.notification
|
|
|
|
.requestPermission()
|
|
|
|
.then((permission) => {
|
|
|
|
if (permission === "granted") {
|
|
|
|
if (version === remoteId) {
|
|
|
|
// Show a notification using Tauri
|
|
|
|
window.__TAURI__?.notification.sendNotification({
|
|
|
|
title: "NextChat",
|
|
|
|
body: `${Locale.Settings.Update.IsLatest}`,
|
|
|
|
icon: `${ChatGptIcon.src}`,
|
|
|
|
sound: "Default",
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const updateMessage =
|
|
|
|
Locale.Settings.Update.FoundUpdate(`${remoteId}`);
|
|
|
|
// Show a notification for the new version using Tauri
|
|
|
|
window.__TAURI__?.notification.sendNotification({
|
|
|
|
title: "NextChat",
|
|
|
|
body: updateMessage,
|
|
|
|
icon: `${ChatGptIcon.src}`,
|
|
|
|
sound: "Default",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2023-10-03 01:08:11 +00:00
|
|
|
}
|
2023-09-10 16:20:23 +00:00
|
|
|
console.log("[Got Upstream] ", remoteId);
|
|
|
|
} catch (error) {
|
|
|
|
console.error("[Fetch Upstream Commit Id]", error);
|
|
|
|
}
|
|
|
|
},
|
2023-04-17 17:34:12 +00:00
|
|
|
|
2023-09-10 16:20:23 +00:00
|
|
|
async updateUsage(force = false) {
|
2023-12-23 18:39:06 +00:00
|
|
|
// only support openai for now
|
2023-09-10 16:20:23 +00:00
|
|
|
const overOneMinute = Date.now() - get().lastUpdateUsage >= ONE_MINUTE;
|
|
|
|
if (!overOneMinute && !force) return;
|
2023-04-17 15:12:27 +00:00
|
|
|
|
2023-09-10 16:20:23 +00:00
|
|
|
set(() => ({
|
|
|
|
lastUpdateUsage: Date.now(),
|
|
|
|
}));
|
2023-04-17 15:12:27 +00:00
|
|
|
|
2023-09-10 16:20:23 +00:00
|
|
|
try {
|
2023-12-23 18:39:06 +00:00
|
|
|
const api = new ClientApi(ModelProvider.GPT);
|
2023-09-10 16:20:23 +00:00
|
|
|
const usage = await api.llm.usage();
|
2023-04-17 17:34:12 +00:00
|
|
|
|
2023-09-10 16:20:23 +00:00
|
|
|
if (usage) {
|
|
|
|
set(() => ({
|
|
|
|
used: usage.used,
|
|
|
|
subscription: usage.total,
|
|
|
|
}));
|
2023-03-23 17:00:33 +00:00
|
|
|
}
|
2023-09-10 16:20:23 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error((e as Error).message);
|
|
|
|
}
|
2023-03-30 16:46:17 +00:00
|
|
|
},
|
2023-09-10 16:20:23 +00:00
|
|
|
}),
|
|
|
|
{
|
|
|
|
name: StoreKey.Update,
|
|
|
|
version: 1,
|
|
|
|
},
|
2023-03-23 17:00:33 +00:00
|
|
|
);
|