From 68e210d0d509dc42c6f449867bb00ce923d7ff87 Mon Sep 17 00:00:00 2001 From: Luo Tim Date: Fri, 7 Jun 2024 01:52:29 +0800 Subject: [PATCH 1/9] Add APIUtils --- src/util/APIUtil.ts | 54 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/util/APIUtil.ts diff --git a/src/util/APIUtil.ts b/src/util/APIUtil.ts new file mode 100644 index 0000000..9f78888 --- /dev/null +++ b/src/util/APIUtil.ts @@ -0,0 +1,54 @@ +import axios from "axios"; +import { v4 as uuidv4 } from 'uuid'; + +class APIUtil { + private static instance: APIUtil; + private currentMessageId: string | undefined; + + + constructor() { + console.log("APIUtil"); + } + + public static getInstance(): APIUtil { + if (!APIUtil.instance) { + APIUtil.instance = new APIUtil(); + } + return APIUtil.instance; + } + + async createMessage(baseUrl: string, accessKey: string, message: object) { + this.currentMessageId = `msg-${uuidv4()}`; + try { + const res = await axios.post( + `${baseUrl}/api/v1/messages`, + {...message, message_id: this.currentMessageId}, + { headers: { + Authorization: `Bearer ${accessKey}`, + 'Content-Type': 'application/json', + }} + ) + console.log("Message created: ", res?.data); + } catch(err) { + console.error(err); + } + } + + async createEvent(baseUrl: string, accessKey: string, event: object) { + try { + const res = await axios.post( + `${baseUrl}/api/v1/messages/${this.currentMessageId}/events`, + event, + {headers: { + Authorization: `Bearer ${accessKey}`, + 'Content-Type': 'application/json', + }} + ) + console.log("Event created: ", res?.data); + } catch(err) { + console.error(err); + } + } +} + +export default APIUtil.getInstance(); From e23f343d0f8110b482fb718c090d4294e004862b Mon Sep 17 00:00:00 2001 From: Luo Tim Date: Fri, 7 Jun 2024 01:53:52 +0800 Subject: [PATCH 2/9] Add config methods to choose API addresses --- src/views/stores/ConfigStore.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/views/stores/ConfigStore.ts b/src/views/stores/ConfigStore.ts index f58af43..2989b62 100644 --- a/src/views/stores/ConfigStore.ts +++ b/src/views/stores/ConfigStore.ts @@ -113,6 +113,23 @@ export const ConfigStore = types } return ""; }, + getAppURL: function() { + return "http://localhost:8001" + const apiBase = this.getAPIBase(); + if (apiBase.includes("api-test.devchat.ai")) { + return "https://apptest.devchat.ai"; + } else { + return "https://app.devchat.ai"; + } + }, + getWebURL: function() { + const apiBase = this.getAPIBase(); + if (apiBase.includes("api-test.devchat.ai")) { + return "https://webtest.devchat.ai"; + } else { + return "https://web.devchat.ai"; + } + }, setConfig: function (data) { this.updateSettle(false); let needUpdate = false; From 05bf0a7f3df21decf3f6e65e7cba60017af15478 Mon Sep 17 00:00:00 2001 From: Luo Tim Date: Fri, 7 Jun 2024 01:55:09 +0800 Subject: [PATCH 3/9] Trigger message event on generating --- src/views/stores/ChatStore.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/views/stores/ChatStore.ts b/src/views/stores/ChatStore.ts index 325644a..ed35aaf 100644 --- a/src/views/stores/ChatStore.ts +++ b/src/views/stores/ChatStore.ts @@ -5,6 +5,7 @@ import yaml from "js-yaml"; import { RootInstance } from "./RootStore"; import { useTranslation } from "react-i18next"; import i18next from "i18next"; +import APIUtil from "@/util/APIUtil"; interface Context { content: string; @@ -171,7 +172,8 @@ export const ChatStore = types self.hasDone = false; self.errorMessage = ""; self.currentMessage = ""; - const chatModel = getParent(self).config.getDefaultModel(); + const config = getParent(self).config + const chatModel = config.getDefaultModel(); messageUtil.sendMessage({ command: "sendMessage", text: text, @@ -179,6 +181,7 @@ export const ChatStore = types parent_hash: lastNonEmptyHash(), model: chatModel, }); + APIUtil.createMessage(config.getAppURL(), config.getUserKey(), {content: text}); }; const helpMessage = (originalMessage = false) => { @@ -390,7 +393,7 @@ Thinking... self.messages.push(...messages); }, updateLastMessage: (message: string) => { - console.log("message: ", message); + // console.log("message: ", message); if (self.messages.length > 0) { self.messages[self.messages.length - 1].message = message; // if (message === "") { From 6df56742b416c7131fd8fb2960444d5479252a76 Mon Sep 17 00:00:00 2001 From: Luo Tim Date: Fri, 7 Jun 2024 01:55:49 +0800 Subject: [PATCH 4/9] Handle code buttons events --- .../MessageMarkdown/CodeButtons.tsx | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/views/components/MessageMarkdown/CodeButtons.tsx b/src/views/components/MessageMarkdown/CodeButtons.tsx index d03f80d..428ec92 100644 --- a/src/views/components/MessageMarkdown/CodeButtons.tsx +++ b/src/views/components/MessageMarkdown/CodeButtons.tsx @@ -1,9 +1,12 @@ import { Tooltip, ActionIcon, CopyButton, Flex } from "@mantine/core"; import { IconCheck, IconGitCommit, IconFileDiff, IconColumnInsertRight, IconReplace, IconCopy,IconFile } from "@tabler/icons-react"; import React, { useState } from "react"; +import { useMst } from "@/views/stores/RootStore"; import messageUtil from '@/util/MessageUtil'; import language from "react-syntax-highlighter/dist/esm/languages/hljs/1c"; +import { use } from "chai"; +import APIUtil from "@/util/APIUtil"; const IconButton = ({ label, color = 'gray', onClick, children }) => ( @@ -31,10 +34,14 @@ const CommitButton = ({ code }) => { }; const CodeCopyButton = ({ code }) => { + const {config} = useMst(); return ( {({ copied, copy }) => ( - + { + copy(); + APIUtil.createEvent(config.getAppURL(), config.getUserKey(), {name: 'copy', value: 'copy'}) + }}> {copied ? : } )} @@ -43,11 +50,14 @@ const CodeCopyButton = ({ code }) => { }; const DiffButton = ({ code }) => { + const {config} = useMst(); const handleClick = () => { + const e = 'show_diff'; messageUtil.sendMessage({ - command: 'show_diff', + command: e, content: code }); + APIUtil.createEvent(config.getAppURL(), config.getUserKey(), {name: e, value: e}) }; return ( @@ -57,11 +67,14 @@ const DiffButton = ({ code }) => { }; const CodeApplyButton = ({ code }) => { + const {config} = useMst(); const handleClick = () => { + const e = 'code_apply'; messageUtil.sendMessage({ - command: 'code_apply', + command: e, content: code }); + APIUtil.createEvent(config.getAppURL(), config.getUserKey(), {name: e, value: e}) }; return ( @@ -71,11 +84,14 @@ const CodeApplyButton = ({ code }) => { }; const FileApplyButton = ({ code }) => { + const {config} = useMst(); const handleClick = () => { + const e = 'code_file_apply'; messageUtil.sendMessage({ - command: 'code_file_apply', + command: e, content: code }); + APIUtil.createEvent(config.getAppURL(), config.getUserKey(), {name: e, value: e}) }; return ( @@ -86,12 +102,15 @@ const FileApplyButton = ({ code }) => { // Add a new button to create new file const NewFileButton = ({ language,code }) => { + const {config} = useMst(); const handleClick = () => { + const e = 'code_new_file'; messageUtil.sendMessage({ - command: 'code_new_file', + command: e, language: language, content: code }); + APIUtil.createEvent(config.getAppURL(), config.getUserKey(), {name: e, value: e}) }; return ( From 2395c6ad8258b203da819a87ae201a5bfb1f1ba4 Mon Sep 17 00:00:00 2001 From: Luo Tim Date: Fri, 7 Jun 2024 01:56:08 +0800 Subject: [PATCH 5/9] Handle code copy events --- src/views/components/MessageMarkdown/index.tsx | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/views/components/MessageMarkdown/index.tsx b/src/views/components/MessageMarkdown/index.tsx index e76d460..01cd193 100644 --- a/src/views/components/MessageMarkdown/index.tsx +++ b/src/views/components/MessageMarkdown/index.tsx @@ -17,6 +17,7 @@ import { useSetState } from "@mantine/hooks"; import { useTranslation } from "react-i18next"; import { useRouter } from "@/views/router"; import remarkGfm from "remark-gfm"; +import APIUtil from "@/util/APIUtil"; (typeof global !== "undefined" ? global : window).Prism = Prism; require("prismjs/components/prism-java"); @@ -70,7 +71,7 @@ function parseMetaData(string) { const MessageMarkdown = observer((props: MessageMarkdownProps) => { const { children, activeStep = false, messageDone } = props; - const { chat } = useMst(); + const { config, chat } = useMst(); const router = useRouter(); const [steps, setSteps] = useState([]); const tree = fromMarkdown(children); @@ -107,6 +108,13 @@ const MessageMarkdown = observer((props: MessageMarkdownProps) => { }); }; + const handleCodeCopy = (event) => { + const selection = window.getSelection()?.toString(); + console.log("Copied: ", selection); + const e = 'manual_copy'; + APIUtil.createEvent(config.getAppURL(), config.getUserKey(), {name: e, value: selection}) + } + useEffect(() => { let previousNode: any = null; let chatmarkCount = 0; @@ -328,6 +336,7 @@ const MessageMarkdown = observer((props: MessageMarkdownProps) => { whiteSpace: "pre", ...props.style, }} + onCopy={handleCodeCopy} {...props} > {tokens.map((line, i) => ( From 2c01f5c5ba0b9ce70ee281431436bf231e704e74 Mon Sep 17 00:00:00 2001 From: Luo Tim Date: Fri, 7 Jun 2024 15:36:05 +0800 Subject: [PATCH 6/9] Manage webapp credentials by APIUtil and add onCodeDiffApply listener --- src/util/APIUtil.ts | 25 +++++++++++++------ src/views/App.tsx | 2 ++ .../MessageMarkdown/CodeButtons.tsx | 12 ++++----- .../components/MessageMarkdown/index.tsx | 2 +- src/views/stores/ChatStore.ts | 2 +- 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/util/APIUtil.ts b/src/util/APIUtil.ts index 9f78888..08fe5a3 100644 --- a/src/util/APIUtil.ts +++ b/src/util/APIUtil.ts @@ -3,11 +3,17 @@ import { v4 as uuidv4 } from 'uuid'; class APIUtil { private static instance: APIUtil; + private baseUrl: string | undefined; + private accessKey: string | undefined; private currentMessageId: string | undefined; constructor() { - console.log("APIUtil"); + window.onCodeDiffApply = () => { + const e = 'code_diff_apply' + this.createEvent({name: e, value: e}) + }; + console.log("Registered onCodeDiffApply"); } public static getInstance(): APIUtil { @@ -17,14 +23,19 @@ class APIUtil { return APIUtil.instance; } - async createMessage(baseUrl: string, accessKey: string, message: object) { + config(baseUrl: string, accessKey: string) { + this.baseUrl = baseUrl; + this.accessKey = accessKey; + } + + async createMessage(message: object) { this.currentMessageId = `msg-${uuidv4()}`; try { const res = await axios.post( - `${baseUrl}/api/v1/messages`, + `${this.baseUrl}/api/v1/messages`, {...message, message_id: this.currentMessageId}, { headers: { - Authorization: `Bearer ${accessKey}`, + Authorization: `Bearer ${this.accessKey}`, 'Content-Type': 'application/json', }} ) @@ -34,13 +45,13 @@ class APIUtil { } } - async createEvent(baseUrl: string, accessKey: string, event: object) { + async createEvent(event: object) { try { const res = await axios.post( - `${baseUrl}/api/v1/messages/${this.currentMessageId}/events`, + `${this.baseUrl}/api/v1/messages/${this.currentMessageId}/events`, event, {headers: { - Authorization: `Bearer ${accessKey}`, + Authorization: `Bearer ${this.accessKey}`, 'Content-Type': 'application/json', }} ) diff --git a/src/views/App.tsx b/src/views/App.tsx index 812bb4f..71f0acb 100644 --- a/src/views/App.tsx +++ b/src/views/App.tsx @@ -8,6 +8,7 @@ import { useMst } from "./stores/RootStore"; import MessageUtil from "@/util/MessageUtil"; import "./App.css"; import "./i18n"; +import APIUtil from "@/util/APIUtil"; export default function App() { const [ready, setReady] = useState(false); @@ -28,6 +29,7 @@ export default function App() { MessageUtil.registerHandler("readConfig", (data: { value: any }) => { console.log("readConfig registerHandler: ", data); config.setConfig(data.value); + APIUtil.config(config.getAppURL(), config.getUserKey()) config.refreshModelList(); setReady(true); }); diff --git a/src/views/components/MessageMarkdown/CodeButtons.tsx b/src/views/components/MessageMarkdown/CodeButtons.tsx index 428ec92..40a44c4 100644 --- a/src/views/components/MessageMarkdown/CodeButtons.tsx +++ b/src/views/components/MessageMarkdown/CodeButtons.tsx @@ -4,8 +4,6 @@ import React, { useState } from "react"; import { useMst } from "@/views/stores/RootStore"; import messageUtil from '@/util/MessageUtil'; -import language from "react-syntax-highlighter/dist/esm/languages/hljs/1c"; -import { use } from "chai"; import APIUtil from "@/util/APIUtil"; const IconButton = ({ label, color = 'gray', onClick, children }) => ( @@ -40,7 +38,7 @@ const CodeCopyButton = ({ code }) => { {({ copied, copy }) => ( { copy(); - APIUtil.createEvent(config.getAppURL(), config.getUserKey(), {name: 'copy', value: 'copy'}) + APIUtil.createEvent({name: 'copy', value: 'copy'}) }}> {copied ? : } @@ -57,7 +55,7 @@ const DiffButton = ({ code }) => { command: e, content: code }); - APIUtil.createEvent(config.getAppURL(), config.getUserKey(), {name: e, value: e}) + APIUtil.createEvent({name: e, value: e}) }; return ( @@ -74,7 +72,7 @@ const CodeApplyButton = ({ code }) => { command: e, content: code }); - APIUtil.createEvent(config.getAppURL(), config.getUserKey(), {name: e, value: e}) + APIUtil.createEvent({name: e, value: e}) }; return ( @@ -91,7 +89,7 @@ const FileApplyButton = ({ code }) => { command: e, content: code }); - APIUtil.createEvent(config.getAppURL(), config.getUserKey(), {name: e, value: e}) + APIUtil.createEvent({name: e, value: e}) }; return ( @@ -110,7 +108,7 @@ const NewFileButton = ({ language,code }) => { language: language, content: code }); - APIUtil.createEvent(config.getAppURL(), config.getUserKey(), {name: e, value: e}) + APIUtil.createEvent({name: e, value: e}) }; return ( diff --git a/src/views/components/MessageMarkdown/index.tsx b/src/views/components/MessageMarkdown/index.tsx index 01cd193..bdfae4e 100644 --- a/src/views/components/MessageMarkdown/index.tsx +++ b/src/views/components/MessageMarkdown/index.tsx @@ -112,7 +112,7 @@ const MessageMarkdown = observer((props: MessageMarkdownProps) => { const selection = window.getSelection()?.toString(); console.log("Copied: ", selection); const e = 'manual_copy'; - APIUtil.createEvent(config.getAppURL(), config.getUserKey(), {name: e, value: selection}) + APIUtil.createEvent({name: e, value: selection}) } useEffect(() => { diff --git a/src/views/stores/ChatStore.ts b/src/views/stores/ChatStore.ts index ed35aaf..f65c81d 100644 --- a/src/views/stores/ChatStore.ts +++ b/src/views/stores/ChatStore.ts @@ -181,7 +181,7 @@ export const ChatStore = types parent_hash: lastNonEmptyHash(), model: chatModel, }); - APIUtil.createMessage(config.getAppURL(), config.getUserKey(), {content: text}); + APIUtil.createMessage({content: text}); }; const helpMessage = (originalMessage = false) => { From 85c98607194de9b0ce519d01a04baefe7c2c325f Mon Sep 17 00:00:00 2001 From: Luo Tim Date: Fri, 7 Jun 2024 16:36:00 +0800 Subject: [PATCH 7/9] Refactor code diff apply message flow --- src/util/APIUtil.ts | 6 +----- src/util/ideaBridge.ts | 7 +++++++ src/views/stores/ChatStore.ts | 4 ++++ 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/util/APIUtil.ts b/src/util/APIUtil.ts index 08fe5a3..9e29004 100644 --- a/src/util/APIUtil.ts +++ b/src/util/APIUtil.ts @@ -9,11 +9,7 @@ class APIUtil { constructor() { - window.onCodeDiffApply = () => { - const e = 'code_diff_apply' - this.createEvent({name: e, value: e}) - }; - console.log("Registered onCodeDiffApply"); + console.log("APIUtil ready"); } public static getInstance(): APIUtil { diff --git a/src/util/ideaBridge.ts b/src/util/ideaBridge.ts index 715a9b9..63d6225 100644 --- a/src/util/ideaBridge.ts +++ b/src/util/ideaBridge.ts @@ -329,6 +329,9 @@ class IdeaBridge { case "updateSetting/response": this.resviceUpdateSetting(res); break; + case "codeDiffApply/response": + this.resviceCodeDiffApply(res); + break; case "sendUserMessage/response": this.resviceSendUserMessage(res); break; @@ -377,6 +380,10 @@ class IdeaBridge { this.executeHandlers("updateSetting", res.payload); } + resviceCodeDiffApply(res) { + this.executeHandlers("codeDiffApply", res.payload); + } + resviceSendUserMessage(res) { this.executeHandlers("chatWithDevChat", { command: "chatWithDevChat", diff --git a/src/views/stores/ChatStore.ts b/src/views/stores/ChatStore.ts index f65c81d..f046747 100644 --- a/src/views/stores/ChatStore.ts +++ b/src/views/stores/ChatStore.ts @@ -174,6 +174,10 @@ export const ChatStore = types self.currentMessage = ""; const config = getParent(self).config const chatModel = config.getDefaultModel(); + messageUtil.registerHandler("codeDiffApply", (_: any) => { + const e = 'code_diff_apply' + APIUtil.createEvent({name: e, value: e}) + }) messageUtil.sendMessage({ command: "sendMessage", text: text, From b3a18fe429951d0b193b3a7c0846f8a665d1ad2a Mon Sep 17 00:00:00 2001 From: Luo Tim Date: Fri, 7 Jun 2024 17:07:15 +0800 Subject: [PATCH 8/9] Create message with model info --- src/views/stores/ChatStore.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/views/stores/ChatStore.ts b/src/views/stores/ChatStore.ts index f046747..43c8f36 100644 --- a/src/views/stores/ChatStore.ts +++ b/src/views/stores/ChatStore.ts @@ -185,7 +185,7 @@ export const ChatStore = types parent_hash: lastNonEmptyHash(), model: chatModel, }); - APIUtil.createMessage({content: text}); + APIUtil.createMessage({content: text, model: chatModel}); }; const helpMessage = (originalMessage = false) => { From 43e5ceade3464cc14451e89070a1211fa835b8ba Mon Sep 17 00:00:00 2001 From: Luo Tim Date: Sat, 8 Jun 2024 03:19:05 +0800 Subject: [PATCH 9/9] webapp api address management --- src/util/APIUtil.ts | 54 ++++++++++++++++++++++- src/views/App.tsx | 2 +- src/views/components/BalanceTip/index.tsx | 42 ++++++------------ src/views/stores/ConfigStore.ts | 18 -------- 4 files changed, 66 insertions(+), 50 deletions(-) diff --git a/src/util/APIUtil.ts b/src/util/APIUtil.ts index 9e29004..a0efc3e 100644 --- a/src/util/APIUtil.ts +++ b/src/util/APIUtil.ts @@ -5,6 +5,7 @@ class APIUtil { private static instance: APIUtil; private baseUrl: string | undefined; private accessKey: string | undefined; + private webappUrl: string | undefined; private currentMessageId: string | undefined; @@ -18,17 +19,51 @@ class APIUtil { } return APIUtil.instance; } + async fetchWebappUrl() { + try { + const res = await axios.get( + `${this.baseUrl}/addresses/webapp`, + { headers: { 'Authorization': `Bearer ${this.accessKey}` }} + ) + const urlOrPath = res?.data; + if (!urlOrPath) { + throw new Error("No webapp url found"); + } + let href = ""; + if (urlOrPath.startsWith("http://") || urlOrPath.startsWith("https://")) { + href = urlOrPath; + } else { + href = new URL(urlOrPath, this.baseUrl).href + } + if (href.endsWith('/')) { + href = href.slice(0, -1); + } + if (href.endsWith('/api')) { + href = href.slice(0, -4); + } + console.log('Webapp url: ', href) + return href; + } catch (err) { + console.error("Error fetch webapp url:", err); + return "https://app.devchat.ai"; + } + } config(baseUrl: string, accessKey: string) { this.baseUrl = baseUrl; this.accessKey = accessKey; + if (!this.webappUrl) { + this.fetchWebappUrl().then(url => { + this.webappUrl = url; + }) + } } async createMessage(message: object) { this.currentMessageId = `msg-${uuidv4()}`; try { const res = await axios.post( - `${this.baseUrl}/api/v1/messages`, + `${this.webappUrl}/api/v1/messages`, {...message, message_id: this.currentMessageId}, { headers: { Authorization: `Bearer ${this.accessKey}`, @@ -44,7 +79,7 @@ class APIUtil { async createEvent(event: object) { try { const res = await axios.post( - `${this.baseUrl}/api/v1/messages/${this.currentMessageId}/events`, + `${this.webappUrl}/api/v1/messages/${this.currentMessageId}/events`, event, {headers: { Authorization: `Bearer ${this.accessKey}`, @@ -56,6 +91,21 @@ class APIUtil { console.error(err); } } + + async getBalance() { + try { + if (!this.webappUrl) this.webappUrl = await this.fetchWebappUrl(); + const res = await axios.get( + `${this.webappUrl}/api/v1/users/profile`, + {headers: { Authorization: `Bearer ${this.accessKey}` }} + ) + return res?.data?.organization + } catch(err) { + console.error(err); + return null; + } + } } + export default APIUtil.getInstance(); diff --git a/src/views/App.tsx b/src/views/App.tsx index 71f0acb..315cd37 100644 --- a/src/views/App.tsx +++ b/src/views/App.tsx @@ -29,7 +29,7 @@ export default function App() { MessageUtil.registerHandler("readConfig", (data: { value: any }) => { console.log("readConfig registerHandler: ", data); config.setConfig(data.value); - APIUtil.config(config.getAppURL(), config.getUserKey()) + APIUtil.config(config.getAPIBase(), config.getUserKey()) config.refreshModelList(); setReady(true); }); diff --git a/src/views/components/BalanceTip/index.tsx b/src/views/components/BalanceTip/index.tsx index cfea49b..43fec7d 100644 --- a/src/views/components/BalanceTip/index.tsx +++ b/src/views/components/BalanceTip/index.tsx @@ -1,5 +1,4 @@ import React, { useEffect, useState } from "react"; -import axios from "axios"; import messageUtil from "@/util/MessageUtil"; import { IconWallet } from "@tabler/icons-react"; import { @@ -11,6 +10,7 @@ import { } from "@mantine/core"; import { Trans } from "react-i18next"; import { useMst } from "@/views/stores/RootStore"; +import APIUtil from "@/util/APIUtil"; const currencyMap = { USD: "$", @@ -28,15 +28,9 @@ function formatCurrency(balance: number | null, currency: string) { return `${currencyMap[currency] || currency}${balance}`; } -const envMap = { - dev: { - requestUrl: "https://apptest.devchat.ai", - link: "https://webtest.devchat.ai", - }, - prod: { - requestUrl: "https://app.devchat.ai", - link: "https://web.devchat.ai", - }, +const links = { + dev: "https://webtest.devchat.ai", + prod: "https://web.devchat.ai", }; export default function WechatTip() { @@ -50,23 +44,13 @@ export default function WechatTip() { const platform = process.env.platform; const getBalance = () => { - if (!envMap[env].requestUrl || !accessKey) { - return; - } - setLoading(true); - axios - .get(`${envMap[env].requestUrl}/api/v1/users/profile`, { - headers: { Authorization: `Bearer ${accessKey}` }, - }) - .then((res) => { - if (res?.data?.organization?.balance) { - setBalance(formatBalance(res?.data?.organization?.balance)); - setCurrency(res?.data?.organization?.currency); - } - }) - .finally(() => { - setLoading(false); - }); + APIUtil.getBalance().then(org => { + setLoading(true); + setBalance(formatBalance(org?.balance)); + setCurrency(org?.currency); + }).finally(() => { + setLoading(false); + }) }; useEffect(() => { @@ -93,7 +77,7 @@ export default function WechatTip() { e.stopPropagation(); messageUtil.sendMessage({ command: "openLink", - url: envMap[env].link, + url: links[env], }); }; @@ -139,7 +123,7 @@ export default function WechatTip() { web.devchat.ai{" "} ) : ( - + web.devchat.ai{" "} )} diff --git a/src/views/stores/ConfigStore.ts b/src/views/stores/ConfigStore.ts index 2989b62..63106c8 100644 --- a/src/views/stores/ConfigStore.ts +++ b/src/views/stores/ConfigStore.ts @@ -2,7 +2,6 @@ import MessageUtil from "@/util/MessageUtil"; import { types, Instance, flow } from "mobx-state-tree"; import modelsTemplate from "@/models"; import cloneDeep from "lodash.clonedeep"; -import { set } from "mobx"; import axios from "axios"; const defaultAPIBase = [ @@ -113,23 +112,6 @@ export const ConfigStore = types } return ""; }, - getAppURL: function() { - return "http://localhost:8001" - const apiBase = this.getAPIBase(); - if (apiBase.includes("api-test.devchat.ai")) { - return "https://apptest.devchat.ai"; - } else { - return "https://app.devchat.ai"; - } - }, - getWebURL: function() { - const apiBase = this.getAPIBase(); - if (apiBase.includes("api-test.devchat.ai")) { - return "https://webtest.devchat.ai"; - } else { - return "https://web.devchat.ai"; - } - }, setConfig: function (data) { this.updateSettle(false); let needUpdate = false;