Skip to content

Commit

Permalink
✨ feat: 支持入参包含 manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Aug 28, 2023
1 parent 0c05171 commit b209fba
Showing 1 changed file with 77 additions and 73 deletions.
150 changes: 77 additions & 73 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export const createLobeChatPluginGateway = (pluginsIndexUrl: string) => {
});

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

const payloadParseResult = pluginRequestPayloadSchema.safeParse(requestPayload);

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

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

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

const marketIndexUrl = indexUrl ?? pluginsIndexUrl;
// ========== 3. 获取插件市场索引 ========== //
// 入参中如果没有 manifest,则从插件市场索引中获取
if (!manifest) {
const marketIndexUrl = indexUrl ?? pluginsIndexUrl;
// ========== 3. 获取插件市场索引 ========== //

let marketIndex: LobeChatPluginsMarketIndex | undefined;
try {
const indexRes = await fetch(marketIndexUrl);
marketIndex = await indexRes.json();
} catch (error) {
console.error(error);
marketIndex = undefined;
}

// 插件市场索引不存在
if (!marketIndex)
return createErrorResponse(PluginErrorType.PluginMarketIndexNotFound, {
indexUrl: marketIndexUrl,
message: '[gateway] plugin market index not found',
});

let marketIndex: LobeChatPluginsMarketIndex | undefined;
try {
const indexRes = await fetch(marketIndexUrl);
marketIndex = await indexRes.json();
} catch (error) {
console.error(error);
marketIndex = undefined;
}
// 插件市场索引解析失败
const indexParseResult = marketIndexSchema.safeParse(marketIndex);

// 插件市场索引不存在
if (!marketIndex)
return createErrorResponse(PluginErrorType.PluginMarketIndexNotFound, {
indexUrl: marketIndexUrl,
message: '[gateway] plugin market index not found',
});
if (!indexParseResult.success)
return createErrorResponse(PluginErrorType.PluginMarketIndexInvalid, {
error: indexParseResult.error,
indexUrl: marketIndexUrl,
marketIndex,
message: '[gateway] plugin market index is invalid',
});

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

if (!indexParseResult.success)
return createErrorResponse(PluginErrorType.PluginMarketIndexInvalid, {
error: indexParseResult.error,
indexUrl: marketIndexUrl,
marketIndex,
message: '[gateway] plugin market index is invalid',
});
// ========== 4. 校验插件 meta 完备性 ========== //

console.info('marketIndex:', marketIndex);

// ========== 4. 校验插件 meta 完备性 ========== //

const pluginMeta = marketIndex.plugins.find((i) => i.identifier === identifier);

// 一个不规范的插件示例
// const pluginMeta = {
// createAt: '2023-08-12',
// homepage: 'https://github.com/lobehub/chat-plugin-real-time-weather',
// manifest: 'https://registry.npmmirror.com/@lobehub/lobe-chat-plugins/latest/files',
// meta: {
// avatar: '☂️',
// tags: ['weather', 'realtime'],
// },
// name: 'realtimeWeather',
// schemaVersion: 'v1',
// };

// 校验插件是否存在
if (!pluginMeta)
return createErrorResponse(PluginErrorType.PluginMetaNotFound, {
identifier,
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)`,
});
const pluginMeta = marketIndex.plugins.find((i) => i.identifier === identifier);

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

if (!metaParseResult.success)
return createErrorResponse(PluginErrorType.PluginMetaInvalid, {
error: metaParseResult.error,
message: '[plugin] plugin meta is invalid',
pluginMeta,
});
// 校验插件是否存在
if (!pluginMeta)
return createErrorResponse(PluginErrorType.PluginMetaNotFound, {
identifier,
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)`,
});

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

// 获取插件的 manifest
let manifest: LobeChatPluginManifest | undefined;
try {
const pluginRes = await fetch(pluginMeta.manifest);
manifest = (await pluginRes.json()) as LobeChatPluginManifest;
} catch (error) {
console.error(error);
manifest = undefined;
if (!metaParseResult.success)
return createErrorResponse(PluginErrorType.PluginMetaInvalid, {
error: metaParseResult.error,
message: '[plugin] plugin meta is invalid',
pluginMeta,
});
// ========== 5. 校验插件 manifest 完备性 ========== //

// 获取插件的 manifest
try {
const pluginRes = await fetch(pluginMeta.manifest);
manifest = (await pluginRes.json()) as LobeChatPluginManifest;
} catch (error) {
console.error(error);
manifest = undefined;
}

if (!manifest)
return createErrorResponse(PluginErrorType.PluginManifestNotFound, {
manifestUrl: pluginMeta.manifest,
message: '[plugin] plugin manifest not found',
});
}

if (!manifest)
return createErrorResponse(PluginErrorType.PluginManifestNotFound, {
manifestUrl: pluginMeta.manifest,
message: '[plugin] plugin manifest not found',
});

const manifestParseResult = pluginManifestSchema.safeParse(manifest);

if (!manifestParseResult.success)
Expand Down

0 comments on commit b209fba

Please sign in to comment.