|
@@ -2,8 +2,12 @@ import OpenAPIClientAxios from "openapi-client-axios";
|
|
|
import { StoreKey } from "../constant";
|
|
import { StoreKey } from "../constant";
|
|
|
import { nanoid } from "nanoid";
|
|
import { nanoid } from "nanoid";
|
|
|
import { createPersistStore } from "../utils/store";
|
|
import { createPersistStore } from "../utils/store";
|
|
|
|
|
+import { getClientConfig } from "../config/client";
|
|
|
import yaml from "js-yaml";
|
|
import yaml from "js-yaml";
|
|
|
import { adapter } from "../utils";
|
|
import { adapter } from "../utils";
|
|
|
|
|
+import { useAccessStore } from "./access";
|
|
|
|
|
+
|
|
|
|
|
+const isApp = getClientConfig()?.buildMode === "export";
|
|
|
|
|
|
|
|
export type Plugin = {
|
|
export type Plugin = {
|
|
|
id: string;
|
|
id: string;
|
|
@@ -16,7 +20,6 @@ export type Plugin = {
|
|
|
authLocation?: string;
|
|
authLocation?: string;
|
|
|
authHeader?: string;
|
|
authHeader?: string;
|
|
|
authToken?: string;
|
|
authToken?: string;
|
|
|
- usingProxy?: boolean;
|
|
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
export type FunctionToolItem = {
|
|
export type FunctionToolItem = {
|
|
@@ -46,17 +49,24 @@ export const FunctionToolService = {
|
|
|
plugin?.authType == "basic"
|
|
plugin?.authType == "basic"
|
|
|
? `Basic ${plugin?.authToken}`
|
|
? `Basic ${plugin?.authToken}`
|
|
|
: plugin?.authType == "bearer"
|
|
: plugin?.authType == "bearer"
|
|
|
- ? ` Bearer ${plugin?.authToken}`
|
|
|
|
|
|
|
+ ? `Bearer ${plugin?.authToken}`
|
|
|
: plugin?.authToken;
|
|
: plugin?.authToken;
|
|
|
const authLocation = plugin?.authLocation || "header";
|
|
const authLocation = plugin?.authLocation || "header";
|
|
|
const definition = yaml.load(plugin.content) as any;
|
|
const definition = yaml.load(plugin.content) as any;
|
|
|
const serverURL = definition?.servers?.[0]?.url;
|
|
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> = {
|
|
const headers: Record<string, string | undefined> = {
|
|
|
- "X-Base-URL": !!plugin?.usingProxy ? serverURL : undefined,
|
|
|
|
|
|
|
+ "X-Base-URL": !isApp ? serverURL : undefined,
|
|
|
};
|
|
};
|
|
|
if (authLocation == "header") {
|
|
if (authLocation == "header") {
|
|
|
headers[headerName] = tokenValue;
|
|
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({
|
|
const api = new OpenAPIClientAxios({
|
|
|
definition: yaml.load(plugin.content) as any,
|
|
definition: yaml.load(plugin.content) as any,
|
|
@@ -165,7 +175,7 @@ export const usePluginStore = createPersistStore(
|
|
|
(set, get) => ({
|
|
(set, get) => ({
|
|
|
create(plugin?: Partial<Plugin>) {
|
|
create(plugin?: Partial<Plugin>) {
|
|
|
const plugins = get().plugins;
|
|
const plugins = get().plugins;
|
|
|
- const id = nanoid();
|
|
|
|
|
|
|
+ const id = plugin?.id || nanoid();
|
|
|
plugins[id] = {
|
|
plugins[id] = {
|
|
|
...createEmptyPlugin(),
|
|
...createEmptyPlugin(),
|
|
|
...plugin,
|
|
...plugin,
|
|
@@ -220,5 +230,37 @@ export const usePluginStore = createPersistStore(
|
|
|
{
|
|
{
|
|
|
name: StoreKey.Plugin,
|
|
name: StoreKey.Plugin,
|
|
|
version: 1,
|
|
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;
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+ },
|
|
|
},
|
|
},
|
|
|
);
|
|
);
|