Skip to content

Commit 8f26a6c

Browse files
committed
✨ feat: 支持 V1 manifest 版本的方案
1 parent d0fc888 commit 8f26a6c

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

api/v1/runner.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// TODO:后续替换为 sdk
2-
import { LobeChatPlugins } from '@lobehub/lobe-chat-plugins';
1+
import { LobeChatPlugin, LobeChatPluginsMarketIndex } from '@lobehub/chat-plugin-sdk';
32

43
import { OpenAIPluginPayload } from '../../types/plugins';
54

@@ -14,8 +13,8 @@ const INDEX_URL = `https://registry.npmmirror.com/${INDEX_PKG}/latest/files`;
1413
export default async (req: Request) => {
1514
if (req.method !== 'POST') return new Response('Method Not Allowed', { status: 405 });
1615

17-
const res = await fetch(INDEX_URL);
18-
const manifest: LobeChatPlugins = await res.json();
16+
const indexRes = await fetch(INDEX_URL);
17+
const manifest: LobeChatPluginsMarketIndex = await indexRes.json();
1918
console.log('manifest:', manifest);
2019

2120
const { name, arguments: args } = (await req.json()) as OpenAIPluginPayload;
@@ -24,13 +23,35 @@ export default async (req: Request) => {
2423

2524
const item = manifest.plugins.find((i) => i.name === name);
2625

27-
// 先通过插件资产 endpoint 路径查询
28-
if (!!item?.runtime.endpoint) {
29-
const res = await fetch(item.runtime.endpoint, { body: args, method: 'post' });
26+
if (!item) return;
27+
28+
// 兼容 V0 版本的代码
29+
if ((manifest.version as number) === 0) {
30+
// 先通过插件资产 endpoint 路径查询
31+
const res = await fetch((item as any).runtime.endpoint, { body: args, method: 'post' });
3032
const data = await res.text();
3133
console.log(`[${name}]`, args, `result:`, data.slice(0, 3600));
3234
return new Response(data);
3335
}
3436

37+
// 新版 V1 的代码
38+
else if (manifest.version === 1) {
39+
// 先通过插件资产 endpoint 路径查询
40+
41+
if (!item.manifest) return;
42+
43+
// 获取插件的 manifest
44+
const pluginRes = await fetch(item.manifest);
45+
const chatPlugin = (await pluginRes.json()) as LobeChatPlugin;
46+
47+
const response = await fetch(chatPlugin.server.url, { body: args, method: 'post' });
48+
49+
const data = await response.text();
50+
51+
console.log(`[${name}]`, args, `result:`, data.slice(0, 3600));
52+
53+
return new Response(data);
54+
}
55+
3556
return;
3657
};

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
"not ie <= 10"
3232
],
3333
"dependencies": {
34-
"@lobehub/lobe-chat-plugins": "^1.1.1",
35-
"query-string": "^8"
34+
"@lobehub/chat-plugin-sdk": "^1.0.1"
3635
},
3736
"devDependencies": {
3837
"@lobehub/lint": "latest",

0 commit comments

Comments
 (0)