diff --git a/api/v1/runner.ts b/api/v1/runner.ts index 1af3d56..1cda0b3 100644 --- a/api/v1/runner.ts +++ b/api/v1/runner.ts @@ -1,3 +1,6 @@ +// TODO:后续替换为 sdk +import { LobeChatPlugins } from '@lobehub/lobe-chat-plugins'; + import { PluginsMap } from '../../plugins'; import { OpenAIPluginPayload } from '../../types/plugins'; @@ -5,6 +8,10 @@ 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 }); @@ -12,6 +19,22 @@ export default async (req: Request) => { 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) { @@ -22,6 +45,5 @@ export default async (req: Request) => { return new Response(JSON.stringify(result)); } - return; }; diff --git a/package.json b/package.json index c4bdeaf..6fe3503 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "not ie <= 10" ], "dependencies": { + "@lobehub/lobe-chat-plugins": "^1.1.1", "query-string": "^8" }, "devDependencies": { diff --git a/plugins/index.ts b/plugins/index.ts index f4ffe6c..aab679e 100644 --- a/plugins/index.ts +++ b/plugins/index.ts @@ -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 = { - [getWeather.name]: getWeather, [searchEngine.name]: searchEngine, [webCrawler.name]: webCrawler, }; diff --git a/plugins/weather/index.ts b/plugins/weather/index.ts deleted file mode 100644 index 9f5b25d..0000000 --- a/plugins/weather/index.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { PluginItem } from '../../types/pluginItem'; -import runner from './runner'; -import { WeatherResult } from './type'; - -const schema: PluginItem['schema'] = { - description: '获取当前天气情况', - name: 'realtimeWeather', - parameters: { - properties: { - city: { - description: '城市名称', - type: 'string', - }, - }, - required: ['city'], - type: 'object', - }, -}; - -const getWeather: PluginItem = { - avatar: '☂️', - name: 'realtimeWeather', - runner, - schema, -}; - -export default getWeather; diff --git a/plugins/weather/runner.ts b/plugins/weather/runner.ts deleted file mode 100644 index a0736e4..0000000 --- a/plugins/weather/runner.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { PluginRunner } from '../../types/pluginItem'; -import { Response, WeatherParams, WeatherResult } from './type'; - -const weatherBaseURL = 'https://restapi.amap.com/v3/weather/weatherInfo'; - -const citySearchURL = 'https://restapi.amap.com/v3/config/district'; - -const KEY = process.env.GAODE_WEATHER_KEY; - -const fetchCityCode = async (keywords: string): Promise => { - const URL = `${citySearchURL}?keywords=${keywords}&subdistrict=0&extensions=base&key=${KEY}`; - const res = await fetch(URL); - - const data = await res.json(); - console.log(data); - - return data.districts[0].adcode; -}; - -const fetchWeather: PluginRunner = async ({ - city, - extensions = 'all', -}) => { - const cityCode = await fetchCityCode(city); - - const URL = `${weatherBaseURL}?city=${cityCode}&extensions=${extensions}&key=${KEY}`; - const res = await fetch(URL); - - const data: Response = await res.json(); - - return data.forecasts; -}; - -export default fetchWeather; diff --git a/plugins/weather/type.ts b/plugins/weather/type.ts deleted file mode 100644 index 2384bdd..0000000 --- a/plugins/weather/type.ts +++ /dev/null @@ -1,36 +0,0 @@ -export interface WeatherParams { - city: string; - extensions?: 'base' | 'all'; -} -export type WeatherResult = Forecast[]; - -export interface Response { - count: string; - forecasts: Forecast[]; - info: string; - infocode: string; - status: string; -} - -export interface Forecast { - adcode: string; - casts: Cast[]; - city: string; - province: string; - reporttime: string; -} - -export interface Cast { - date: string; - daypower: string; - daytemp: string; - daytemp_float: string; - dayweather: string; - daywind: string; - nightpower: string; - nighttemp: string; - nighttemp_float: string; - nightweather: string; - nightwind: string; - week: string; -}