Skip to content

Commit

Permalink
✨ feat: 支持 V1 manifest 版本的方案
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Aug 18, 2023
1 parent d0fc888 commit 8f26a6c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
35 changes: 28 additions & 7 deletions api/v1/runner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// TODO:后续替换为 sdk
import { LobeChatPlugins } from '@lobehub/lobe-chat-plugins';
import { LobeChatPlugin, LobeChatPluginsMarketIndex } from '@lobehub/chat-plugin-sdk';

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

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

const res = await fetch(INDEX_URL);
const manifest: LobeChatPlugins = await res.json();
const indexRes = await fetch(INDEX_URL);
const manifest: LobeChatPluginsMarketIndex = await indexRes.json();
console.log('manifest:', manifest);

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

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

// 先通过插件资产 endpoint 路径查询
if (!!item?.runtime.endpoint) {
const res = await fetch(item.runtime.endpoint, { body: args, method: 'post' });
if (!item) return;

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

// 新版 V1 的代码
else if (manifest.version === 1) {
// 先通过插件资产 endpoint 路径查询

if (!item.manifest) return;

// 获取插件的 manifest
const pluginRes = await fetch(item.manifest);
const chatPlugin = (await pluginRes.json()) as LobeChatPlugin;

const response = await fetch(chatPlugin.server.url, { body: args, method: 'post' });

const data = await response.text();

console.log(`[${name}]`, args, `result:`, data.slice(0, 3600));

return new Response(data);
}

return;
};
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
"not ie <= 10"
],
"dependencies": {
"@lobehub/lobe-chat-plugins": "^1.1.1",
"query-string": "^8"
"@lobehub/chat-plugin-sdk": "^1.0.1"
},
"devDependencies": {
"@lobehub/lint": "latest",
Expand Down

0 comments on commit 8f26a6c

Please sign in to comment.