Skip to content

Commit

Permalink
🔥 Refactor: 剥离插件服务端路由,为实现独立的插件调用做准备 (lobehub#82)
Browse files Browse the repository at this point in the history
* 🔥 refactor: remove /api/plugins function

* ♻️ refactor: refactor the plugin implement to separate the server runner
  • Loading branch information
arvinxx authored Aug 16, 2023
1 parent 0aa38fd commit 4f034dd
Show file tree
Hide file tree
Showing 11 changed files with 13 additions and 179 deletions.
9 changes: 7 additions & 2 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import nextPWA from 'next-pwa';
const isProd = process.env.NODE_ENV === 'production';
const API_END_PORT_URL = process.env.API_END_PORT_URL || '';

// chat plugin market
const PLUGIN_RUNNER_BASE_URL =
process.env.PLUGIN_RUNNER_BASE_URL || 'https://lobe-chat-plugin-market.vercel.app';

const withPWA = nextPWA({
dest: 'public',
register: true,
Expand Down Expand Up @@ -31,8 +35,9 @@ const nextConfig = {
destination: `${API_END_PORT_URL}/api/openai`,
},
{
source: '/api/plugins-dev',
destination: `${API_END_PORT_URL}/api/plugins`,
source: '/api/plugins',
// refs to: https://github.com/lobehub/chat-plugin-market
destination: `${PLUGIN_RUNNER_BASE_URL}/api/v1/runner`,
},
];
},
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
"dependencies": {
"@ant-design/colors": "^7",
"@ant-design/icons": "^5",
"@commitlint/cli": "^17",
"@emoji-mart/data": "^1",
"@emoji-mart/react": "^1",
"@icons-pack/react-simple-icons": "^8",
Expand All @@ -86,7 +85,6 @@
"next": "13.4.7",
"openai-edge": "^1",
"polished": "^4",
"query-string": "^8",
"react": "^18",
"react-dom": "^18",
"react-hotkeys-hook": "^4",
Expand Down
22 changes: 0 additions & 22 deletions src/pages/api/plugins.api.ts

This file was deleted.

6 changes: 1 addition & 5 deletions src/plugins/searchEngine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import { ChatCompletionFunctions } from 'openai-edge/types/api';

import { PluginItem } from '@/plugins/type';

import runner from './runner';
import { Result } from './type';

const schema: ChatCompletionFunctions = {
description: '查询搜索引擎获取信息',
name: 'searchEngine',
Expand All @@ -20,10 +17,9 @@ const schema: ChatCompletionFunctions = {
},
};

const searchEngine: PluginItem<Result> = {
const searchEngine: PluginItem = {
avatar: '🔍',
name: 'searchEngine',
runner,
schema,
};

Expand Down
41 changes: 0 additions & 41 deletions src/plugins/searchEngine/runner.ts

This file was deleted.

18 changes: 2 additions & 16 deletions src/plugins/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ReactNode } from 'react';
* @template Result - 结果类型,默认为 any
* @template RunnerParams - 运行参数类型,默认为 any
*/
export interface PluginItem<Result = any, RunnerParams = any> {
export interface PluginItem {
/**
* 头像
*/
Expand All @@ -15,12 +15,7 @@ export interface PluginItem<Result = any, RunnerParams = any> {
* 名称
*/
name: string;
/**
* 运行器
* @param params - 运行参数
* @returns 运行结果的 Promise
*/
runner: PluginRunner<RunnerParams, Result>;
render?: PluginRender;
/**
* 聊天完成函数的模式
*/
Expand All @@ -34,15 +29,6 @@ export interface PluginItem<Result = any, RunnerParams = any> {
*/
export type PluginRender = (props: PluginRenderProps) => ReactNode;

/**
* 插件运行器
* @template Params - 参数类型,默认为 object
* @template Result - 结果类型,默认为 any
* @param params - 运行参数
* @returns 运行结果的 Promise
*/
export type PluginRunner<Params = object, Result = any> = (params: Params) => Promise<Result>;

/**
* 插件渲染属性
* @template Result - 结果类型,默认为 any
Expand Down
6 changes: 1 addition & 5 deletions src/plugins/weather/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { PluginItem } from '@/plugins/type';
import { WeatherResult } from '@/plugins/weather/type';

import runner from './runner';

const schema = {
description: '获取当前天气情况',
Expand All @@ -18,10 +15,9 @@ const schema = {
},
};

const getWeather: PluginItem<WeatherResult> = {
const getWeather: PluginItem = {
avatar: '☂️',
name: 'realtimeWeather',
runner,
schema,
};

Expand Down
35 changes: 0 additions & 35 deletions src/plugins/weather/runner.ts

This file was deleted.

5 changes: 1 addition & 4 deletions src/plugins/webCrawler/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { PluginItem } from '@/plugins/type';

import runner from './runner';
import { Result } from './type';

const schema = {
description: '提取网页内容并总结',
name: 'websiteCrawler',
Expand All @@ -18,6 +15,6 @@ const schema = {
},
};

const getWeather: PluginItem<Result> = { avatar: '🕸', name: 'websiteCrawler', runner, schema };
const getWeather: PluginItem = { avatar: '🕸', name: 'websiteCrawler', schema };

export default getWeather;
46 changes: 0 additions & 46 deletions src/plugins/webCrawler/runner.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/services/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ const prefix = isDev ? '-dev' : '';

export const URLS = {
openai: '/api/openai' + prefix,
plugins: '/api/plugins' + prefix,
plugins: '/api/plugins',
};

0 comments on commit 4f034dd

Please sign in to comment.