1- // TODO:后续替换为 sdk
2- import { LobeChatPlugins } from '@lobehub/lobe-chat-plugins' ;
1+ import { LobeChatPlugin , LobeChatPluginsMarketIndex } from '@lobehub/chat-plugin-sdk' ;
32
43import { OpenAIPluginPayload } from '../../types/plugins' ;
54
@@ -14,8 +13,8 @@ const INDEX_URL = `https://registry.npmmirror.com/${INDEX_PKG}/latest/files`;
1413export default async ( req : Request ) => {
1514 if ( req . method !== 'POST' ) return new Response ( 'Method Not Allowed' , { status : 405 } ) ;
1615
17- const res = await fetch ( INDEX_URL ) ;
18- const manifest : LobeChatPlugins = await res . json ( ) ;
16+ const indexRes = await fetch ( INDEX_URL ) ;
17+ const manifest : LobeChatPluginsMarketIndex = await indexRes . json ( ) ;
1918 console . log ( 'manifest:' , manifest ) ;
2019
2120 const { name, arguments : args } = ( await req . json ( ) ) as OpenAIPluginPayload ;
@@ -24,13 +23,35 @@ export default async (req: Request) => {
2423
2524 const item = manifest . plugins . find ( ( i ) => i . name === name ) ;
2625
27- // 先通过插件资产 endpoint 路径查询
28- if ( ! ! item ?. runtime . endpoint ) {
29- const res = await fetch ( item . runtime . endpoint , { body : args , method : 'post' } ) ;
26+ if ( ! item ) return ;
27+
28+ // 兼容 V0 版本的代码
29+ if ( ( manifest . version as number ) === 0 ) {
30+ // 先通过插件资产 endpoint 路径查询
31+ const res = await fetch ( ( item as any ) . runtime . endpoint , { body : args , method : 'post' } ) ;
3032 const data = await res . text ( ) ;
3133 console . log ( `[${ name } ]` , args , `result:` , data . slice ( 0 , 3600 ) ) ;
3234 return new Response ( data ) ;
3335 }
3436
37+ // 新版 V1 的代码
38+ else if ( manifest . version === 1 ) {
39+ // 先通过插件资产 endpoint 路径查询
40+
41+ if ( ! item . manifest ) return ;
42+
43+ // 获取插件的 manifest
44+ const pluginRes = await fetch ( item . manifest ) ;
45+ const chatPlugin = ( await pluginRes . json ( ) ) as LobeChatPlugin ;
46+
47+ const response = await fetch ( chatPlugin . server . url , { body : args , method : 'post' } ) ;
48+
49+ const data = await response . text ( ) ;
50+
51+ console . log ( `[${ name } ]` , args , `result:` , data . slice ( 0 , 3600 ) ) ;
52+
53+ return new Response ( data ) ;
54+ }
55+
3556 return ;
3657} ;
0 commit comments