forked from lobehub/lobe-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.ts
182 lines (152 loc) · 4.86 KB
/
plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import {
LobeChatPluginManifest,
LobeChatPluginsMarketIndex,
pluginManifestSchema,
} from '@lobehub/chat-plugin-sdk';
import { PluginModel } from '@/database/models/plugin';
import { PLUGINS_URLS, URLS } from '@/services/_url';
import { globalHelpers } from '@/store/global/helpers';
import { OpenAIPluginManifest } from '@/types/openai/plugin';
import { LobeTool } from '@/types/tool';
import { LobeToolCustomPlugin } from '@/types/tool/plugin';
export interface InstallPluginParams {
identifier: string;
manifest: LobeChatPluginManifest;
type: 'plugin' | 'customPlugin';
}
class PluginService {
private _fetchJSON = async <T = any>(url: string, proxy = false): Promise<T> => {
// 2. 发送请求
let res: Response;
try {
res = await (proxy ? fetch(URLS.proxy, { body: url, method: 'POST' }) : fetch(url));
} catch {
throw new TypeError('fetchError');
}
if (!res.ok) {
throw new TypeError('fetchError');
}
let data;
const contentType = res.headers.get('Content-Type');
try {
if (contentType === 'application/json') {
data = await res.json();
} else {
const { default: YAML } = await import('yaml');
const yaml = await res.text();
data = YAML.parse(yaml);
}
} catch {
throw new TypeError('urlError');
}
return data;
};
/**
* get plugin list from store
*/
getPluginList = async (): Promise<LobeChatPluginsMarketIndex> => {
const locale = globalHelpers.getCurrentLanguage();
const res = await fetch(`${PLUGINS_URLS.store}?locale=${locale}`);
return res.json();
};
getPluginManifest = async (
url?: string,
useProxy: boolean = false,
): Promise<LobeChatPluginManifest> => {
// 1. valid plugin
if (!url) {
throw new TypeError('noManifest');
}
// 2. 发送请求
let data = await this._fetchJSON<LobeChatPluginManifest>(url, useProxy);
// @ts-ignore
// if there is a description_for_model, it is an OpenAI plugin
// we need convert to lobe plugin
if (data['description_for_model']) {
data = this.convertOpenAIManifestToLobeManifest(data as any);
}
// 3. 校验插件文件格式规范
const parser = pluginManifestSchema.safeParse(data);
if (!parser.success) {
throw new TypeError('manifestInvalid', { cause: parser.error });
}
// 4. if exist OpenAPI api, merge the OpenAPIs to api
if (parser.data.openapi) {
const openapiJson = await this._fetchJSON(parser.data.openapi, useProxy);
try {
const { OpenAPIConvertor } = await import('@lobehub/chat-plugin-sdk/openapi');
const convertor = new OpenAPIConvertor(openapiJson);
const openAPIs = await convertor.convertOpenAPIToPluginSchema();
data.api = [...data.api, ...openAPIs];
data.settings = await convertor.convertAuthToSettingsSchema(data.settings);
} catch (error) {
throw new TypeError('openAPIInvalid', { cause: error });
}
}
return data;
};
installPlugin = async (plugin: InstallPluginParams) => {
return PluginModel.create(plugin);
};
getInstalledPlugins = () => {
return PluginModel.getList() as Promise<LobeTool[]>;
};
uninstallPlugin(identifier: string) {
return PluginModel.delete(identifier);
}
async createCustomPlugin(customPlugin: LobeToolCustomPlugin) {
return PluginModel.create({ ...customPlugin, type: 'customPlugin' });
}
async updatePlugin(id: string, value: LobeToolCustomPlugin) {
return PluginModel.update(id, value);
}
async updatePluginManifest(id: string, manifest: LobeChatPluginManifest) {
return PluginModel.update(id, { manifest });
}
async removeAllPlugins() {
return PluginModel.clear();
}
async updatePluginSettings(id: string, settings: any) {
return PluginModel.update(id, { settings });
}
private convertOpenAIManifestToLobeManifest = (
data: OpenAIPluginManifest,
): LobeChatPluginManifest => {
const manifest: LobeChatPluginManifest = {
api: [],
homepage: data.legal_info_url,
identifier: data.name_for_model,
meta: {
avatar: data.logo_url,
description: data.description_for_human,
title: data.name_for_human,
},
openapi: data.api.url,
systemRole: data.description_for_model,
type: 'default',
version: '1',
};
switch (data.auth.type) {
case 'none': {
break;
}
case 'service_http': {
manifest.settings = {
properties: {
apiAuthKey: {
default: data.auth.verification_tokens['openai'],
description: 'API Key',
format: 'password',
title: 'API Key',
type: 'string',
},
},
type: 'object',
};
break;
}
}
return manifest;
};
}
export const pluginService = new PluginService();