33import { Validator } from '@cfworker/json-schema' ;
44import {
55 ErrorType ,
6- LobeChatPlugin ,
6+ LobeChatPluginManifest ,
77 LobeChatPluginsMarketIndex ,
88 PluginRequestPayload ,
99 createErrorResponse ,
@@ -33,9 +33,9 @@ export const createLobeChatPluginGateway = (pluginsIndexUrl: string) => {
3333 if ( ! payloadParseResult . success )
3434 return createErrorResponse ( ErrorType . BadRequest , payloadParseResult . error ) ;
3535
36- const { name , arguments : args , indexUrl } = requestPayload ;
36+ const { identifier , arguments : args , indexUrl, apiName } = requestPayload ;
3737
38- console . info ( `plugin call: ${ name } ` ) ;
38+ console . info ( `[ ${ identifier } ] - ${ apiName } ` ) ;
3939
4040 const marketIndexUrl = indexUrl ?? pluginsIndexUrl ;
4141 // ========== 3. 获取插件市场索引 ========== //
@@ -71,7 +71,7 @@ export const createLobeChatPluginGateway = (pluginsIndexUrl: string) => {
7171
7272 // ========== 4. 校验插件 meta 完备性 ========== //
7373
74- const pluginMeta = marketIndex . plugins . find ( ( i ) => i . name === name ) ;
74+ const pluginMeta = marketIndex . plugins . find ( ( i ) => i . identifier === identifier ) ;
7575
7676 // 一个不规范的插件示例
7777 // const pluginMeta = {
@@ -89,8 +89,8 @@ export const createLobeChatPluginGateway = (pluginsIndexUrl: string) => {
8989 // 校验插件是否存在
9090 if ( ! pluginMeta )
9191 return createErrorResponse ( ErrorType . PluginMetaNotFound , {
92- message : `[gateway] plugin ' ${ name } ' is not found,please check the plugin list in ${ indexUrl } , or create an issue to [lobe-chat-plugins](https://github.com/lobehub/lobe-chat-plugins/issues)` ,
93- name ,
92+ identifier ,
93+ message : `[gateway] plugin ' ${ identifier } ' is not found,please check the plugin list in ${ indexUrl } , or create an issue to [lobe-chat-plugins](https://github.com/lobehub/lobe-chat-plugins/issues)` ,
9494 } ) ;
9595
9696 const metaParseResult = pluginMetaSchema . safeParse ( pluginMeta ) ;
@@ -105,10 +105,10 @@ export const createLobeChatPluginGateway = (pluginsIndexUrl: string) => {
105105 // ========== 5. 校验插件 manifest 完备性 ========== //
106106
107107 // 获取插件的 manifest
108- let manifest : LobeChatPlugin | undefined ;
108+ let manifest : LobeChatPluginManifest | undefined ;
109109 try {
110110 const pluginRes = await fetch ( pluginMeta . manifest ) ;
111- manifest = ( await pluginRes . json ( ) ) as LobeChatPlugin ;
111+ manifest = ( await pluginRes . json ( ) ) as LobeChatPluginManifest ;
112112 } catch ( error ) {
113113 console . error ( error ) ;
114114 manifest = undefined ;
@@ -129,12 +129,20 @@ export const createLobeChatPluginGateway = (pluginsIndexUrl: string) => {
129129 message : '[plugin] plugin manifest is invalid' ,
130130 } ) ;
131131
132- console . log ( `[${ name } ] plugin manifest:` , manifest ) ;
132+ console . log ( `[${ identifier } ] plugin manifest:` , manifest ) ;
133133
134134 // ========== 6. 校验请求入参与 manifest 要求一致性 ========== //
135+ const api = manifest . api . find ( ( i ) => i . name === apiName ) ;
136+
137+ if ( ! api )
138+ return createErrorResponse ( ErrorType . BadRequest , {
139+ apiName,
140+ identifier,
141+ message : '[plugin] api not found' ,
142+ } ) ;
135143
136144 if ( args ) {
137- const v = new Validator ( manifest . schema . parameters as any ) ;
145+ const v = new Validator ( api . parameters as any ) ;
138146 const validator = v . validate ( JSON . parse ( args ! ) ) ;
139147
140148 if ( ! validator . valid )
@@ -147,14 +155,14 @@ export const createLobeChatPluginGateway = (pluginsIndexUrl: string) => {
147155
148156 // ========== 7. 发送请求 ========== //
149157
150- const response = await fetch ( manifest . server . url , { body : args , method : 'post' } ) ;
158+ const response = await fetch ( api . url , { body : args , method : 'post' } ) ;
151159
152160 // 不正常的错误,直接返回请求
153161 if ( ! response . ok ) return response ;
154162
155163 const data = await response . text ( ) ;
156164
157- console . log ( `[${ name } ]` , args , `result:` , data . slice ( 0 , 1000 ) ) ;
165+ console . log ( `[${ identifier } ]` , args , `result:` , data . slice ( 0 , 1000 ) ) ;
158166
159167 return new Response ( data ) ;
160168 } ;
0 commit comments