Skip to content

Commit

Permalink
✨ feat: 初步剥离天气插件 (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx authored Aug 17, 2023
1 parent 6cf3f3d commit ecf1c4c
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 100 deletions.
24 changes: 23 additions & 1 deletion api/v1/runner.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
// TODO:后续替换为 sdk
import { LobeChatPlugins } from '@lobehub/lobe-chat-plugins';

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

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

const INDEX_PKG = `@lobehub/lobe-chat-plugins`;

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 { name, arguments: args } = (await req.json()) as OpenAIPluginPayload;

console.log(`检测到 functionCall: ${name}`);

console.timeLog('请求 index');
const res = await fetch(INDEX_URL);
const manifest: LobeChatPlugins = await res.json();
console.timeEnd('请求 index');

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

// 先通过插件资产路径查询
if (item) {
const res = await fetch(item.runtime.endpoint, { body: args, method: 'post' });
const data = await res.text();
console.log(`[${name}]`, args, `result:`, data.slice(0, 3600));
return new Response(data);
}

// TODO:兼容性代码,全量完成迁移后移除
const func = PluginsMap[name];

if (func) {
Expand All @@ -22,6 +45,5 @@ export default async (req: Request) => {

return new Response(JSON.stringify(result));
}

return;
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"not ie <= 10"
],
"dependencies": {
"@lobehub/lobe-chat-plugins": "^1.1.1",
"query-string": "^8"
},
"devDependencies": {
Expand Down
2 changes: 0 additions & 2 deletions plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { PluginItem } from '../types/pluginItem';
import searchEngine from './searchEngine';
import getWeather from './weather';
import webCrawler from './webCrawler';

export const PluginsMap: Record<string, PluginItem> = {
[getWeather.name]: getWeather,
[searchEngine.name]: searchEngine,
[webCrawler.name]: webCrawler,
};
27 changes: 0 additions & 27 deletions plugins/weather/index.ts

This file was deleted.

34 changes: 0 additions & 34 deletions plugins/weather/runner.ts

This file was deleted.

36 changes: 0 additions & 36 deletions plugins/weather/type.ts

This file was deleted.

0 comments on commit ecf1c4c

Please sign in to comment.