Bläddra i källkod

using tauri http api run plugin to fixed cors in App

lloydzhou 1 år sedan
förälder
incheckning
f9a047aad4
9 ändrade filer med 75 tillägg och 20 borttagningar
  1. 18 15
      app/components/plugin.tsx
  2. 7 1
      app/global.d.ts
  3. 1 1
      app/store/chat.ts
  4. 2 0
      app/store/plugin.ts
  5. 34 1
      app/utils.ts
  6. 2 1
      package.json
  7. 1 1
      src-tauri/Cargo.toml
  8. 5 0
      src-tauri/tauri.conf.json
  9. 5 0
      yarn.lock

+ 18 - 15
app/components/plugin.tsx

@@ -27,6 +27,7 @@ import {
 import Locale from "../locales";
 import { useNavigate } from "react-router-dom";
 import { useEffect, useState } from "react";
+import { getClientConfig } from "../config/client";
 
 export function PluginPage() {
   const navigate = useNavigate();
@@ -293,21 +294,23 @@ export function PluginPage() {
                   ></PasswordInput>
                 </ListItem>
               )}
-              <ListItem
-                title={Locale.Plugin.Auth.Proxy}
-                subTitle={Locale.Plugin.Auth.ProxyDescription}
-              >
-                <input
-                  type="checkbox"
-                  checked={editingPlugin?.usingProxy}
-                  style={{ minWidth: 16 }}
-                  onChange={(e) => {
-                    pluginStore.updatePlugin(editingPlugin.id, (plugin) => {
-                      plugin.usingProxy = e.currentTarget.checked;
-                    });
-                  }}
-                ></input>
-              </ListItem>
+              {!getClientConfig()?.isApp && (
+                <ListItem
+                  title={Locale.Plugin.Auth.Proxy}
+                  subTitle={Locale.Plugin.Auth.ProxyDescription}
+                >
+                  <input
+                    type="checkbox"
+                    checked={editingPlugin?.usingProxy}
+                    style={{ minWidth: 16 }}
+                    onChange={(e) => {
+                      pluginStore.updatePlugin(editingPlugin.id, (plugin) => {
+                        plugin.usingProxy = e.currentTarget.checked;
+                      });
+                    }}
+                  ></input>
+                </ListItem>
+              )}
             </List>
             <List>
               <ListItem title={Locale.Plugin.EditModal.Content}>

+ 7 - 1
app/global.d.ts

@@ -21,10 +21,16 @@ declare interface Window {
       writeBinaryFile(path: string, data: Uint8Array): Promise<void>;
       writeTextFile(path: string, data: string): Promise<void>;
     };
-    notification:{
+    notification: {
       requestPermission(): Promise<Permission>;
       isPermissionGranted(): Promise<boolean>;
       sendNotification(options: string | Options): void;
     };
+    http: {
+      fetch<T>(
+        url: string,
+        options?: Record<string, unknown>,
+      ): Promise<Response<T>>;
+    };
   };
 }

+ 1 - 1
app/store/chat.ts

@@ -420,7 +420,7 @@ export const useChatStore = createPersistStore(
             });
           },
           onError(error) {
-            const isAborted = error.message.includes("aborted");
+            const isAborted = error.message?.includes?.("aborted");
             botMessage.content +=
               "\n\n" +
               prettyObject({

+ 2 - 0
app/store/plugin.ts

@@ -4,6 +4,7 @@ import { StoreKey } from "../constant";
 import { nanoid } from "nanoid";
 import { createPersistStore } from "../utils/store";
 import yaml from "js-yaml";
+import { adapter } from "../utils";
 
 export type Plugin = {
   id: string;
@@ -61,6 +62,7 @@ export const FunctionToolService = {
     const api = new OpenAPIClientAxios({
       definition: yaml.load(plugin.content) as any,
       axiosConfigDefaults: {
+        adapter: adapter as any,
         baseURL,
         headers,
       },

+ 34 - 1
app/utils.ts

@@ -2,7 +2,9 @@ import { useEffect, useState } from "react";
 import { showToast } from "./components/ui-lib";
 import Locale from "./locales";
 import { RequestMessage } from "./client/api";
-import { ServiceProvider } from "./constant";
+import { ServiceProvider, REQUEST_TIMEOUT_MS } from "./constant";
+import isObject from "lodash-es/isObject";
+import { fetch as tauriFetch, Body, ResponseType } from "@tauri-apps/api/http";
 
 export function trimTopic(topic: string) {
   // Fix an issue where double quotes still show in the Indonesian language
@@ -285,3 +287,34 @@ export function showPlugins(provider: ServiceProvider, model: string) {
   }
   return false;
 }
+
+export function fetch(
+  url: string,
+  options?: Record<string, unknown>,
+): Promise<any> {
+  if (window.__TAURI__) {
+    const payload = options?.body || options?.data;
+    return tauriFetch(url, {
+      ...options,
+      body:
+        payload &&
+        ({
+          type: "Text",
+          payload,
+        } as any),
+      timeout: ((options?.timeout as number) || REQUEST_TIMEOUT_MS) / 1000,
+      responseType:
+        options?.responseType == "text" ? ResponseType.Text : ResponseType.JSON,
+    } as any);
+  }
+  return window.fetch(url, options);
+}
+
+export function adapter(config: Record<string, unknown>) {
+  const { baseURL, url, params, ...rest } = config;
+  const path = baseURL ? `${baseURL}${url}` : url;
+  const fetchUrl = params
+    ? `${path}?${new URLSearchParams(params as any).toString()}`
+    : path;
+  return fetch(fetchUrl as string, { ...rest, responseType: "text" });
+}

+ 2 - 1
package.json

@@ -51,14 +51,15 @@
     "zustand": "^4.3.8"
   },
   "devDependencies": {
+    "@tauri-apps/api": "^1.6.0",
     "@tauri-apps/cli": "1.5.11",
+    "@types/js-yaml": "4.0.9",
     "@types/lodash-es": "^4.17.12",
     "@types/node": "^20.11.30",
     "@types/react": "^18.2.70",
     "@types/react-dom": "^18.2.7",
     "@types/react-katex": "^3.0.0",
     "@types/spark-md5": "^3.0.4",
-    "@types/js-yaml": "4.0.9",
     "concurrently": "^8.2.2",
     "cross-env": "^7.0.3",
     "eslint": "^8.49.0",

+ 1 - 1
src-tauri/Cargo.toml

@@ -17,7 +17,7 @@ tauri-build = { version = "1.5.1", features = [] }
 [dependencies]
 serde_json = "1.0"
 serde = { version = "1.0", features = ["derive"] }
-tauri = { version = "1.5.4", features = [
+tauri = { version = "1.5.4", features = [ "http-all",
     "notification-all",
     "fs-all",
     "clipboard-all",

+ 5 - 0
src-tauri/tauri.conf.json

@@ -50,6 +50,11 @@
       },
       "notification": {
         "all": true
+      },
+      "http": {
+        "all": true,
+        "request": true,
+        "scope": ["https://*", "http://*"]
       }
     },
     "bundle": {

+ 5 - 0
yarn.lock

@@ -1553,6 +1553,11 @@
   dependencies:
     tslib "^2.4.0"
 
+"@tauri-apps/api@^1.6.0":
+  version "1.6.0"
+  resolved "https://registry.npmjs.org/@tauri-apps/api/-/api-1.6.0.tgz#745b7e4e26782c3b2ad9510d558fa5bb2cf29186"
+  integrity sha512-rqI++FWClU5I2UBp4HXFvl+sBWkdigBkxnpJDQUWttNyG7IZP4FwQGhTNL5EOw0vI8i6eSAJ5frLqO7n7jbJdg==
+
 "@tauri-apps/cli-darwin-arm64@1.5.11":
   version "1.5.11"
   resolved "https://registry.yarnpkg.com/@tauri-apps/cli-darwin-arm64/-/cli-darwin-arm64-1.5.11.tgz#a831f98f685148e46e8050dbdddbf4bcdda9ddc6"