update.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import {
  2. FETCH_COMMIT_URL,
  3. FETCH_TAG_URL,
  4. ModelProvider,
  5. StoreKey,
  6. } from "../constant";
  7. import { getClientConfig } from "../config/client";
  8. import { createPersistStore } from "../utils/store";
  9. import { clientUpdate } from "../utils";
  10. import ChatGptIcon from "../icons/chatgpt.png";
  11. import Locale from "../locales";
  12. import { ClientApi } from "../client/api";
  13. const ONE_MINUTE = 60 * 1000;
  14. const isApp = !!getClientConfig()?.isApp;
  15. function formatVersionDate(t: string) {
  16. const d = new Date(+t);
  17. const year = d.getUTCFullYear();
  18. const month = d.getUTCMonth() + 1;
  19. const day = d.getUTCDate();
  20. return [
  21. year.toString(),
  22. month.toString().padStart(2, "0"),
  23. day.toString().padStart(2, "0"),
  24. ].join("");
  25. }
  26. type VersionType = "date" | "tag";
  27. async function getVersion(type: VersionType) {
  28. if (type === "date") {
  29. const data = (await (await fetch(FETCH_COMMIT_URL)).json()) as {
  30. commit: {
  31. author: { name: string; date: string };
  32. };
  33. sha: string;
  34. }[];
  35. const remoteCommitTime = data[0].commit.author.date;
  36. const remoteId = new Date(remoteCommitTime).getTime().toString();
  37. return remoteId;
  38. } else if (type === "tag") {
  39. const data = (await (await fetch(FETCH_TAG_URL)).json()) as {
  40. commit: { sha: string; url: string };
  41. name: string;
  42. }[];
  43. return data.at(0)?.name;
  44. }
  45. }
  46. export const useUpdateStore = createPersistStore(
  47. {
  48. versionType: "tag" as VersionType,
  49. lastUpdate: 0,
  50. version: "unknown",
  51. remoteVersion: "",
  52. used: 0,
  53. subscription: 0,
  54. lastUpdateUsage: 0,
  55. },
  56. (set, get) => ({
  57. formatVersion(version: string) {
  58. if (get().versionType === "date") {
  59. version = formatVersionDate(version);
  60. }
  61. return version;
  62. },
  63. async getLatestVersion(force = false) {
  64. const versionType = get().versionType;
  65. let version =
  66. versionType === "date"
  67. ? getClientConfig()?.commitDate
  68. : getClientConfig()?.version;
  69. set(() => ({ version }));
  70. const shouldCheck = Date.now() - get().lastUpdate > 2 * 60 * ONE_MINUTE;
  71. if (!force && !shouldCheck) return;
  72. set(() => ({
  73. lastUpdate: Date.now(),
  74. }));
  75. try {
  76. const remoteId = await getVersion(versionType);
  77. set(() => ({
  78. remoteVersion: remoteId,
  79. }));
  80. if (window.__TAURI__?.notification && isApp) {
  81. // Check if notification permission is granted
  82. await window.__TAURI__?.notification
  83. .isPermissionGranted()
  84. .then((granted) => {
  85. if (!granted) {
  86. return;
  87. } else {
  88. // Request permission to show notifications
  89. window.__TAURI__?.notification
  90. .requestPermission()
  91. .then((permission) => {
  92. if (permission === "granted") {
  93. if (version === remoteId) {
  94. // Show a notification using Tauri
  95. window.__TAURI__?.notification.sendNotification({
  96. title: "NextChat",
  97. body: `${Locale.Settings.Update.IsLatest}`,
  98. icon: `${ChatGptIcon.src}`,
  99. sound: "Default",
  100. });
  101. } else {
  102. const updateMessage =
  103. Locale.Settings.Update.FoundUpdate(`${remoteId}`);
  104. // Show a notification for the new version using Tauri
  105. window.__TAURI__?.notification.sendNotification({
  106. title: "NextChat",
  107. body: updateMessage,
  108. icon: `${ChatGptIcon.src}`,
  109. sound: "Default",
  110. });
  111. clientUpdate();
  112. }
  113. }
  114. });
  115. }
  116. });
  117. }
  118. console.log("[Got Upstream] ", remoteId);
  119. } catch (error) {
  120. console.error("[Fetch Upstream Commit Id]", error);
  121. }
  122. },
  123. async updateUsage(force = false) {
  124. // only support openai for now
  125. const overOneMinute = Date.now() - get().lastUpdateUsage >= ONE_MINUTE;
  126. if (!overOneMinute && !force) return;
  127. set(() => ({
  128. lastUpdateUsage: Date.now(),
  129. }));
  130. try {
  131. const api = new ClientApi(ModelProvider.GPT);
  132. const usage = await api.llm.usage();
  133. if (usage) {
  134. set(() => ({
  135. used: usage.used,
  136. subscription: usage.total,
  137. }));
  138. }
  139. } catch (e) {
  140. console.error((e as Error).message);
  141. }
  142. },
  143. }),
  144. {
  145. name: StoreKey.Update,
  146. version: 1,
  147. },
  148. );