Skip to content

Commit

Permalink
✨ feat: can replace index url
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Aug 20, 2023
1 parent 2e78c39 commit dc3d0cc
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# relpace url with your internal plugins index url
PLUGINS_INDEX_URL=https://example.com/index.json
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
[coverage]: https://codecov.io/gh/lobehub/chat-plugins-gateway/branch/master/graph/badge.svg
[codecov-url]: https://codecov.io/gh/lobehub/chat-plugins-gateway/branch/master

Lobe Chat 插件市场列表
Lobe Chat 插件网关
8 changes: 0 additions & 8 deletions api/v1/_validator.ts

This file was deleted.

23 changes: 13 additions & 10 deletions api/v1/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ import {
ErrorType,
LobeChatPlugin,
LobeChatPluginsMarketIndex,
PluginRequestPayload,
createErrorResponse,
marketIndexSchema,
pluginManifestSchema,
pluginMetaSchema,
pluginRequestPayloadSchema,
} from '@lobehub/chat-plugin-sdk';

import { PluginPayload, payloadSchema } from './_validator';

export const config = {
runtime: 'edge',
};

const INDEX_URL = `https://registry.npmmirror.com/@lobehub/lobe-chat-plugins/latest/files`;
const DEFAULT_INDEX_URL =
process.env.PLUGINS_INDEX_URL ??
'https://registry.npmmirror.com/@lobehub/lobe-chat-plugins/latest/files';

export default async (req: Request) => {
// ========== 1. 校验请求方法 ========== //
Expand All @@ -27,22 +29,23 @@ export default async (req: Request) => {
});

// ========== 2. 校验请求入参基础格式 ========== //
const requestPayload = (await req.json()) as PluginPayload;
const requestPayload = (await req.json()) as PluginRequestPayload;

const payloadParseResult = payloadSchema.safeParse(requestPayload);
const payloadParseResult = pluginRequestPayloadSchema.safeParse(requestPayload);

if (!payloadParseResult.success)
return createErrorResponse(ErrorType.BadRequest, payloadParseResult.error);

const { name, arguments: args } = requestPayload;
const { name, arguments: args, indexUrl } = requestPayload;

console.info(`plugin call: ${name}`);

const marketIndexUrl = indexUrl ?? DEFAULT_INDEX_URL;
// ========== 3. 获取插件市场索引 ========== //

let marketIndex: LobeChatPluginsMarketIndex | undefined;
try {
const indexRes = await fetch(INDEX_URL);
const indexRes = await fetch(marketIndexUrl);
marketIndex = await indexRes.json();
} catch (error) {
console.error(error);
Expand All @@ -52,7 +55,7 @@ export default async (req: Request) => {
// 插件市场索引不存在
if (!marketIndex)
return createErrorResponse(ErrorType.PluginMarketIndexNotFound, {
indexUrl: INDEX_URL,
indexUrl,
message: '[gateway] plugin market index not found',
});

Expand All @@ -62,7 +65,7 @@ export default async (req: Request) => {
if (!indexParseResult.success)
return createErrorResponse(ErrorType.PluginMarketIndexInvalid, {
error: indexParseResult.error,
indexUrl: INDEX_URL,
indexUrl,
marketIndex,
message: '[gateway] plugin market index is invalid',
});
Expand All @@ -89,7 +92,7 @@ export default async (req: Request) => {
// 校验插件是否存在
if (!pluginMeta)
return createErrorResponse(ErrorType.PluginMetaNotFound, {
message: `[gateway] plugin '${name}' is not found,please check the plugin list in ${INDEX_URL}, or create an issue to [lobe-chat-plugins](https://github.com/lobehub/lobe-chat-plugins/issues)`,
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)`,
name,
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"prettier": "^2",
"semantic-release": "^21",
"typescript": "^5",
"vercel": "^29",
"vercel": "^28.20.0",
"vitest": "latest"
}
}

0 comments on commit dc3d0cc

Please sign in to comment.