@lobehub/chat 0.55.0 → 0.55.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,31 @@
2
2
 
3
3
  # Changelog
4
4
 
5
+ ### [Version 0.55.1](https://github.com/lobehub/lobe-chat/compare/v0.55.0...v0.55.1)
6
+
7
+ <sup>Released on **2023-08-22**</sup>
8
+
9
+ #### ♻ Code Refactoring
10
+
11
+ - **misc**: Refactor plugin api with @lobehub/chat-plugins-gateway.
12
+
13
+ <br/>
14
+
15
+ <details>
16
+ <summary><kbd>Improvements and Fixes</kbd></summary>
17
+
18
+ #### Code refactoring
19
+
20
+ - **misc**: Refactor plugin api with @lobehub/chat-plugins-gateway, closes [#100](https://github.com/lobehub/lobe-chat/issues/100) ([b88d0db](https://github.com/lobehub/lobe-chat/commit/b88d0db))
21
+
22
+ </details>
23
+
24
+ <div align="right">
25
+
26
+ [![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)
27
+
28
+ </div>
29
+
5
30
  ## [Version 0.55.0](https://github.com/lobehub/lobe-chat/compare/v0.54.4...v0.55.0)
6
31
 
7
32
  <sup>Released on **2023-08-22**</sup>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobehub/chat",
3
- "version": "0.55.0",
3
+ "version": "0.55.1",
4
4
  "description": "Lobe Chat is an open-source chatbot client using LangChain, Typescript and Next.js",
5
5
  "keywords": [
6
6
  "chatbot",
@@ -68,6 +68,7 @@
68
68
  "@emoji-mart/react": "^1",
69
69
  "@icons-pack/react-simple-icons": "^8",
70
70
  "@lobehub/chat-plugin-sdk": "latest",
71
+ "@lobehub/chat-plugins-gateway": "^1.2.2",
71
72
  "@lobehub/ui": "latest",
72
73
  "@vercel/analytics": "^1",
73
74
  "ahooks": "^3",
@@ -1,17 +1,4 @@
1
- // reason to use cfworker json schema:
2
- // https://github.com/vercel/next.js/discussions/47063#discussioncomment-5303951
3
- import { Validator } from '@cfworker/json-schema';
4
- import {
5
- ErrorType,
6
- LobeChatPlugin,
7
- LobeChatPluginsMarketIndex,
8
- PluginRequestPayload,
9
- createErrorResponse,
10
- marketIndexSchema,
11
- pluginManifestSchema,
12
- pluginMetaSchema,
13
- pluginRequestPayloadSchema,
14
- } from '@lobehub/chat-plugin-sdk';
1
+ import { createLobeChatPluginGateway } from '@lobehub/chat-plugins-gateway';
15
2
 
16
3
  import { PLUGINS_INDEX_URL } from '@/const/url';
17
4
 
@@ -19,145 +6,4 @@ export const config = {
19
6
  runtime: 'edge',
20
7
  };
21
8
 
22
- const DEFAULT_INDEX_URL = PLUGINS_INDEX_URL;
23
-
24
- export default async (req: Request) => {
25
- // ========== 1. 校验请求方法 ========== //
26
- if (req.method !== 'POST')
27
- return createErrorResponse(ErrorType.MethodNotAllowed, {
28
- message: '[gateway] only allow POST method',
29
- });
30
-
31
- // ========== 2. 校验请求入参基础格式 ========== //
32
- const requestPayload = (await req.json()) as PluginRequestPayload;
33
-
34
- const payloadParseResult = pluginRequestPayloadSchema.safeParse(requestPayload);
35
-
36
- if (!payloadParseResult.success)
37
- return createErrorResponse(ErrorType.BadRequest, payloadParseResult.error);
38
-
39
- const { name, arguments: args, indexUrl } = requestPayload;
40
-
41
- console.info(`plugin call: ${name}`);
42
-
43
- const marketIndexUrl = indexUrl ?? DEFAULT_INDEX_URL;
44
- // ========== 3. 获取插件市场索引 ========== //
45
-
46
- let marketIndex: LobeChatPluginsMarketIndex | undefined;
47
- try {
48
- const indexRes = await fetch(marketIndexUrl);
49
- marketIndex = await indexRes.json();
50
- } catch (error) {
51
- console.error(error);
52
- marketIndex = undefined;
53
- }
54
-
55
- // 插件市场索引不存在
56
- if (!marketIndex)
57
- return createErrorResponse(ErrorType.PluginMarketIndexNotFound, {
58
- indexUrl,
59
- message: '[gateway] plugin market index not found',
60
- });
61
-
62
- // 插件市场索引解析失败
63
- const indexParseResult = marketIndexSchema.safeParse(marketIndex);
64
-
65
- if (!indexParseResult.success)
66
- return createErrorResponse(ErrorType.PluginMarketIndexInvalid, {
67
- error: indexParseResult.error,
68
- indexUrl,
69
- marketIndex,
70
- message: '[gateway] plugin market index is invalid',
71
- });
72
-
73
- console.info('marketIndex:', marketIndex);
74
-
75
- // ========== 4. 校验插件 meta 完备性 ========== //
76
-
77
- const pluginMeta = marketIndex.plugins.find((i) => i.name === name);
78
-
79
- // 一个不规范的插件示例
80
- // const pluginMeta = {
81
- // createAt: '2023-08-12',
82
- // homepage: 'https://github.com/lobehub/chat-plugin-real-time-weather',
83
- // manifest: 'https://registry.npmmirror.com/@lobehub/lobe-chat-plugins/latest/files',
84
- // meta: {
85
- // avatar: '☂️',
86
- // tags: ['weather', 'realtime'],
87
- // },
88
- // name: 'realtimeWeather',
89
- // schemaVersion: 'v1',
90
- // };
91
-
92
- // 校验插件是否存在
93
- if (!pluginMeta)
94
- return createErrorResponse(ErrorType.PluginMetaNotFound, {
95
- message: `[gateway] plugin '${name}' is not found,please check the plugin list in ${indexUrl}, or create an issue to [lobe-chat-plugins](https://github.com/lobehub/lobe-chat-plugins/issues)`,
96
- name,
97
- });
98
-
99
- const metaParseResult = pluginMetaSchema.safeParse(pluginMeta);
100
-
101
- if (!metaParseResult.success)
102
- return createErrorResponse(ErrorType.PluginMetaInvalid, {
103
- error: metaParseResult.error,
104
- message: '[plugin] plugin meta is invalid',
105
- pluginMeta,
106
- });
107
-
108
- // ========== 5. 校验插件 manifest 完备性 ========== //
109
-
110
- // 获取插件的 manifest
111
- let manifest: LobeChatPlugin | undefined;
112
- try {
113
- const pluginRes = await fetch(pluginMeta.manifest);
114
- manifest = (await pluginRes.json()) as LobeChatPlugin;
115
- } catch (error) {
116
- console.error(error);
117
- manifest = undefined;
118
- }
119
-
120
- if (!manifest)
121
- return createErrorResponse(ErrorType.PluginManifestNotFound, {
122
- manifestUrl: pluginMeta.manifest,
123
- message: '[plugin] plugin manifest not found',
124
- });
125
-
126
- const manifestParseResult = pluginManifestSchema.safeParse(manifest);
127
-
128
- if (!manifestParseResult.success)
129
- return createErrorResponse(ErrorType.PluginManifestInvalid, {
130
- error: manifestParseResult.error,
131
- manifest: manifest,
132
- message: '[plugin] plugin manifest is invalid',
133
- });
134
-
135
- console.log(`[${name}] plugin manifest:`, manifest);
136
-
137
- // ========== 6. 校验请求入参与 manifest 要求一致性 ========== //
138
-
139
- if (args) {
140
- const v = new Validator(manifest.schema.parameters as any);
141
- const validator = v.validate(JSON.parse(args!));
142
-
143
- if (!validator.valid)
144
- return createErrorResponse(ErrorType.BadRequest, {
145
- error: validator.errors,
146
- manifest,
147
- message: '[plugin] args is invalid with plugin manifest schema',
148
- });
149
- }
150
-
151
- // ========== 7. 发送请求 ========== //
152
-
153
- const response = await fetch(manifest.server.url, { body: args, method: 'post' });
154
-
155
- // 不正常的错误,直接返回请求
156
- if (!response.ok) return response;
157
-
158
- const data = await response.text();
159
-
160
- console.log(`[${name}]`, args, `result:`, data.slice(0, 1000));
161
-
162
- return new Response(data);
163
- };
9
+ export default createLobeChatPluginGateway(PLUGINS_INDEX_URL);