Skip to content

Commit b209fba

Browse files
committed
✨ feat: 支持入参包含 manifest
1 parent 0c05171 commit b209fba

1 file changed

Lines changed: 77 additions & 73 deletions

File tree

src/index.ts

Lines changed: 77 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ export const createLobeChatPluginGateway = (pluginsIndexUrl: string) => {
2828
});
2929

3030
// ========== 2. 校验请求入参基础格式 ========== //
31-
const requestPayload = (await req.json()) as PluginRequestPayload;
31+
const requestPayload = (await req.json()) as PluginRequestPayload & {
32+
manifest?: LobeChatPluginManifest;
33+
};
3234

3335
const payloadParseResult = pluginRequestPayloadSchema.safeParse(requestPayload);
3436

@@ -37,91 +39,93 @@ export const createLobeChatPluginGateway = (pluginsIndexUrl: string) => {
3739

3840
const { identifier, arguments: args, indexUrl, apiName } = requestPayload;
3941

42+
let manifest: LobeChatPluginManifest | undefined = requestPayload.manifest;
4043
console.info(`[${identifier}] - ${apiName} `);
4144

42-
const marketIndexUrl = indexUrl ?? pluginsIndexUrl;
43-
// ========== 3. 获取插件市场索引 ========== //
45+
// 入参中如果没有 manifest,则从插件市场索引中获取
46+
if (!manifest) {
47+
const marketIndexUrl = indexUrl ?? pluginsIndexUrl;
48+
// ========== 3. 获取插件市场索引 ========== //
49+
50+
let marketIndex: LobeChatPluginsMarketIndex | undefined;
51+
try {
52+
const indexRes = await fetch(marketIndexUrl);
53+
marketIndex = await indexRes.json();
54+
} catch (error) {
55+
console.error(error);
56+
marketIndex = undefined;
57+
}
58+
59+
// 插件市场索引不存在
60+
if (!marketIndex)
61+
return createErrorResponse(PluginErrorType.PluginMarketIndexNotFound, {
62+
indexUrl: marketIndexUrl,
63+
message: '[gateway] plugin market index not found',
64+
});
4465

45-
let marketIndex: LobeChatPluginsMarketIndex | undefined;
46-
try {
47-
const indexRes = await fetch(marketIndexUrl);
48-
marketIndex = await indexRes.json();
49-
} catch (error) {
50-
console.error(error);
51-
marketIndex = undefined;
52-
}
66+
// 插件市场索引解析失败
67+
const indexParseResult = marketIndexSchema.safeParse(marketIndex);
5368

54-
// 插件市场索引不存在
55-
if (!marketIndex)
56-
return createErrorResponse(PluginErrorType.PluginMarketIndexNotFound, {
57-
indexUrl: marketIndexUrl,
58-
message: '[gateway] plugin market index not found',
59-
});
69+
if (!indexParseResult.success)
70+
return createErrorResponse(PluginErrorType.PluginMarketIndexInvalid, {
71+
error: indexParseResult.error,
72+
indexUrl: marketIndexUrl,
73+
marketIndex,
74+
message: '[gateway] plugin market index is invalid',
75+
});
6076

61-
// 插件市场索引解析失败
62-
const indexParseResult = marketIndexSchema.safeParse(marketIndex);
77+
console.info('marketIndex:', marketIndex);
6378

64-
if (!indexParseResult.success)
65-
return createErrorResponse(PluginErrorType.PluginMarketIndexInvalid, {
66-
error: indexParseResult.error,
67-
indexUrl: marketIndexUrl,
68-
marketIndex,
69-
message: '[gateway] plugin market index is invalid',
70-
});
79+
// ========== 4. 校验插件 meta 完备性 ========== //
7180

72-
console.info('marketIndex:', marketIndex);
73-
74-
// ========== 4. 校验插件 meta 完备性 ========== //
75-
76-
const pluginMeta = marketIndex.plugins.find((i) => i.identifier === identifier);
77-
78-
// 一个不规范的插件示例
79-
// const pluginMeta = {
80-
// createAt: '2023-08-12',
81-
// homepage: 'https://github.com/lobehub/chat-plugin-real-time-weather',
82-
// manifest: 'https://registry.npmmirror.com/@lobehub/lobe-chat-plugins/latest/files',
83-
// meta: {
84-
// avatar: '☂️',
85-
// tags: ['weather', 'realtime'],
86-
// },
87-
// name: 'realtimeWeather',
88-
// schemaVersion: 'v1',
89-
// };
90-
91-
// 校验插件是否存在
92-
if (!pluginMeta)
93-
return createErrorResponse(PluginErrorType.PluginMetaNotFound, {
94-
identifier,
95-
message: `[gateway] plugin '${identifier}' is not found,please check the plugin list in ${marketIndexUrl}, or create an issue to [lobe-chat-plugins](https://github.com/lobehub/lobe-chat-plugins/issues)`,
96-
});
81+
const pluginMeta = marketIndex.plugins.find((i) => i.identifier === identifier);
9782

98-
const metaParseResult = pluginMetaSchema.safeParse(pluginMeta);
83+
// 一个不规范的插件示例
84+
// const pluginMeta = {
85+
// createAt: '2023-08-12',
86+
// homepage: 'https://github.com/lobehub/chat-plugin-real-time-weather',
87+
// manifest: 'https://registry.npmmirror.com/@lobehub/lobe-chat-plugins/latest/files',
88+
// meta: {
89+
// avatar: '☂️',
90+
// tags: ['weather', 'realtime'],
91+
// },
92+
// name: 'realtimeWeather',
93+
// schemaVersion: 'v1',
94+
// };
9995

100-
if (!metaParseResult.success)
101-
return createErrorResponse(PluginErrorType.PluginMetaInvalid, {
102-
error: metaParseResult.error,
103-
message: '[plugin] plugin meta is invalid',
104-
pluginMeta,
105-
});
96+
// 校验插件是否存在
97+
if (!pluginMeta)
98+
return createErrorResponse(PluginErrorType.PluginMetaNotFound, {
99+
identifier,
100+
message: `[gateway] plugin '${identifier}' is not found,please check the plugin list in ${marketIndexUrl}, or create an issue to [lobe-chat-plugins](https://github.com/lobehub/lobe-chat-plugins/issues)`,
101+
});
106102

107-
// ========== 5. 校验插件 manifest 完备性 ========== //
103+
const metaParseResult = pluginMetaSchema.safeParse(pluginMeta);
108104

109-
// 获取插件的 manifest
110-
let manifest: LobeChatPluginManifest | undefined;
111-
try {
112-
const pluginRes = await fetch(pluginMeta.manifest);
113-
manifest = (await pluginRes.json()) as LobeChatPluginManifest;
114-
} catch (error) {
115-
console.error(error);
116-
manifest = undefined;
105+
if (!metaParseResult.success)
106+
return createErrorResponse(PluginErrorType.PluginMetaInvalid, {
107+
error: metaParseResult.error,
108+
message: '[plugin] plugin meta is invalid',
109+
pluginMeta,
110+
});
111+
// ========== 5. 校验插件 manifest 完备性 ========== //
112+
113+
// 获取插件的 manifest
114+
try {
115+
const pluginRes = await fetch(pluginMeta.manifest);
116+
manifest = (await pluginRes.json()) as LobeChatPluginManifest;
117+
} catch (error) {
118+
console.error(error);
119+
manifest = undefined;
120+
}
121+
122+
if (!manifest)
123+
return createErrorResponse(PluginErrorType.PluginManifestNotFound, {
124+
manifestUrl: pluginMeta.manifest,
125+
message: '[plugin] plugin manifest not found',
126+
});
117127
}
118128

119-
if (!manifest)
120-
return createErrorResponse(PluginErrorType.PluginManifestNotFound, {
121-
manifestUrl: pluginMeta.manifest,
122-
message: '[plugin] plugin manifest not found',
123-
});
124-
125129
const manifestParseResult = pluginManifestSchema.safeParse(manifest);
126130

127131
if (!manifestParseResult.success)

0 commit comments

Comments
 (0)