瀏覽代碼

1. add buildin plugin; 2. remove `usingProxy`

lloydzhou 1 年之前
父節點
當前提交
ed20fd2962
共有 3 個文件被更改,包括 69 次插入37 次删除
  1. 5 32
      app/components/plugin.tsx
  2. 47 5
      app/store/plugin.ts
  3. 17 0
      public/plugins.json

+ 5 - 32
app/components/plugin.tsx

@@ -12,7 +12,6 @@ import EditIcon from "../icons/edit.svg";
 import AddIcon from "../icons/add.svg";
 import CloseIcon from "../icons/close.svg";
 import DeleteIcon from "../icons/delete.svg";
-import EyeIcon from "../icons/eye.svg";
 import ConfirmIcon from "../icons/confirm.svg";
 import ReloadIcon from "../icons/reload.svg";
 import GithubIcon from "../icons/github.svg";
@@ -29,7 +28,6 @@ import {
 import Locale from "../locales";
 import { useNavigate } from "react-router-dom";
 import { useState } from "react";
-import { getClientConfig } from "../config/client";
 
 export function PluginPage() {
   const navigate = useNavigate();
@@ -209,19 +207,11 @@ export function PluginPage() {
                   </div>
                 </div>
                 <div className={styles["mask-actions"]}>
-                  {m.builtin ? (
-                    <IconButton
-                      icon={<EyeIcon />}
-                      text={Locale.Plugin.Item.View}
-                      onClick={() => setEditingPluginId(m.id)}
-                    />
-                  ) : (
-                    <IconButton
-                      icon={<EditIcon />}
-                      text={Locale.Plugin.Item.Edit}
-                      onClick={() => setEditingPluginId(m.id)}
-                    />
-                  )}
+                  <IconButton
+                    icon={<EditIcon />}
+                    text={Locale.Plugin.Item.Edit}
+                    onClick={() => setEditingPluginId(m.id)}
+                  />
                   {!m.builtin && (
                     <IconButton
                       icon={<DeleteIcon />}
@@ -325,23 +315,6 @@ export function PluginPage() {
                   ></PasswordInput>
                 </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}>

+ 47 - 5
app/store/plugin.ts

@@ -2,8 +2,12 @@ import OpenAPIClientAxios from "openapi-client-axios";
 import { StoreKey } from "../constant";
 import { nanoid } from "nanoid";
 import { createPersistStore } from "../utils/store";
+import { getClientConfig } from "../config/client";
 import yaml from "js-yaml";
 import { adapter } from "../utils";
+import { useAccessStore } from "./access";
+
+const isApp = getClientConfig()?.buildMode === "export";
 
 export type Plugin = {
   id: string;
@@ -16,7 +20,6 @@ export type Plugin = {
   authLocation?: string;
   authHeader?: string;
   authToken?: string;
-  usingProxy?: boolean;
 };
 
 export type FunctionToolItem = {
@@ -46,17 +49,24 @@ export const FunctionToolService = {
       plugin?.authType == "basic"
         ? `Basic ${plugin?.authToken}`
         : plugin?.authType == "bearer"
-        ? ` Bearer ${plugin?.authToken}`
+        ? `Bearer ${plugin?.authToken}`
         : plugin?.authToken;
     const authLocation = plugin?.authLocation || "header";
     const definition = yaml.load(plugin.content) as any;
     const serverURL = definition?.servers?.[0]?.url;
-    const baseURL = !!plugin?.usingProxy ? "/api/proxy" : serverURL;
+    const baseURL = !isApp ? "/api/proxy" : serverURL;
     const headers: Record<string, string | undefined> = {
-      "X-Base-URL": !!plugin?.usingProxy ? serverURL : undefined,
+      "X-Base-URL": !isApp ? serverURL : undefined,
     };
     if (authLocation == "header") {
       headers[headerName] = tokenValue;
+      // try using openaiApiKey for Dalle3 Plugin.
+      if (!tokenValue && plugin.id === "dalle3") {
+        const openaiApiKey = useAccessStore.getState().openaiApiKey;
+        if (openaiApiKey) {
+          headers[headerName] = `Bearer ${openaiApiKey}`;
+        }
+      }
     }
     const api = new OpenAPIClientAxios({
       definition: yaml.load(plugin.content) as any,
@@ -165,7 +175,7 @@ export const usePluginStore = createPersistStore(
   (set, get) => ({
     create(plugin?: Partial<Plugin>) {
       const plugins = get().plugins;
-      const id = nanoid();
+      const id = plugin?.id || nanoid();
       plugins[id] = {
         ...createEmptyPlugin(),
         ...plugin,
@@ -220,5 +230,37 @@ export const usePluginStore = createPersistStore(
   {
     name: StoreKey.Plugin,
     version: 1,
+    onRehydrateStorage(state) {
+      console.log("onRehydrateStorage", state);
+      // Skip store rehydration on server side
+      if (typeof window === "undefined") {
+        return;
+      }
+
+      fetch("./plugins.json")
+        .then((res) => res.json())
+        .then((res) => {
+          Promise.all(
+            res.map((item: any) =>
+              fetch(item.schema)
+                .then((res) => res.text())
+                .then((content) => ({
+                  ...item,
+                  content,
+                })),
+            ),
+          ).then((builtinPlugins: any) => {
+            builtinPlugins.forEach((item: any) => {
+              const plugin = state.create(item);
+              state.updatePlugin(plugin.id, (plugin) => {
+                const tool = FunctionToolService.add(plugin, true);
+                plugin.title = tool.api.definition.info.title;
+                plugin.version = tool.api.definition.info.version;
+                plugin.builtin = true;
+              });
+            });
+          });
+        });
+    },
   },
 );

+ 17 - 0
public/plugins.json

@@ -0,0 +1,17 @@
+[
+  {
+    "id": "dalle3",
+    "name": "Dalle3",
+    "schema": "https://ghp.ci/https://raw.githubusercontent.com/ChatGPTNextWeb/NextChat-Awesome-Plugins/main/plugins/dalle/openapi.json"
+  },
+  {
+    "id": "arxivsearch",
+    "name": "ArxivSearch",
+    "schema": "https://ghp.ci/https://raw.githubusercontent.com/ChatGPTNextWeb/NextChat-Awesome-Plugins/main/plugins/arxivsearch/openapi.json"
+  },
+  {
+    "id": "duckduckgolite",
+    "name": "DuckDuckGoLiteSearch",
+    "schema": "https://ghp.ci/https://raw.githubusercontent.com/ChatGPTNextWeb/NextChat-Awesome-Plugins/main/plugins/duckduckgolite/openapi.json"
+  }
+]