Skip to content

Commit ecf1c4c

Browse files
authored
✨ feat: 初步剥离天气插件 (#1)
1 parent 6cf3f3d commit ecf1c4c

File tree

6 files changed

+24
-100
lines changed

6 files changed

+24
-100
lines changed

api/v1/runner.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,40 @@
1+
// TODO:后续替换为 sdk
2+
import { LobeChatPlugins } from '@lobehub/lobe-chat-plugins';
3+
14
import { PluginsMap } from '../../plugins';
25
import { OpenAIPluginPayload } from '../../types/plugins';
36

47
export const config = {
58
runtime: 'edge',
69
};
710

11+
const INDEX_PKG = `@lobehub/lobe-chat-plugins`;
12+
13+
const INDEX_URL = `https://registry.npmmirror.com/${INDEX_PKG}/latest/files`;
14+
815
export default async (req: Request) => {
916
if (req.method !== 'POST') return new Response('Method Not Allowed', { status: 405 });
1017

1118
const { name, arguments: args } = (await req.json()) as OpenAIPluginPayload;
1219

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

22+
console.timeLog('请求 index');
23+
const res = await fetch(INDEX_URL);
24+
const manifest: LobeChatPlugins = await res.json();
25+
console.timeEnd('请求 index');
26+
27+
const item = manifest.plugins.find((i) => i.name === name);
28+
29+
// 先通过插件资产路径查询
30+
if (item) {
31+
const res = await fetch(item.runtime.endpoint, { body: args, method: 'post' });
32+
const data = await res.text();
33+
console.log(`[${name}]`, args, `result:`, data.slice(0, 3600));
34+
return new Response(data);
35+
}
36+
37+
// TODO:兼容性代码,全量完成迁移后移除
1538
const func = PluginsMap[name];
1639

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

2346
return new Response(JSON.stringify(result));
2447
}
25-
2648
return;
2749
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"not ie <= 10"
3232
],
3333
"dependencies": {
34+
"@lobehub/lobe-chat-plugins": "^1.1.1",
3435
"query-string": "^8"
3536
},
3637
"devDependencies": {

plugins/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { PluginItem } from '../types/pluginItem';
22
import searchEngine from './searchEngine';
3-
import getWeather from './weather';
43
import webCrawler from './webCrawler';
54

65
export const PluginsMap: Record<string, PluginItem> = {
7-
[getWeather.name]: getWeather,
86
[searchEngine.name]: searchEngine,
97
[webCrawler.name]: webCrawler,
108
};

plugins/weather/index.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

plugins/weather/runner.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

plugins/weather/type.ts

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)