1+ // TODO:后续替换为 sdk
2+ import { LobeChatPlugins } from '@lobehub/lobe-chat-plugins' ;
3+
14import { PluginsMap } from '../../plugins' ;
25import { OpenAIPluginPayload } from '../../types/plugins' ;
36
47export 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+
815export 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} ;
0 commit comments