@@ -24,6 +24,7 @@ type IValidator = (schema: Schema, value: any) => { errors?: any; valid: boolean
24
24
25
25
export interface GatewayOptions {
26
26
Validator ?: IValidator ;
27
+ defaultPluginSettings ?: Record < string , Record < string , any > > ;
27
28
/**
28
29
* @default https://chat-plugins.lobehub.com
29
30
*/
@@ -43,11 +44,17 @@ export interface GatewayErrorResponse {
43
44
export class Gateway {
44
45
private pluginIndexUrl = DEFAULT_PLUGINS_INDEX_URL ;
45
46
private _validator : IValidator | undefined ;
47
+ private defaultPluginSettings : Record < string , Record < string , any > > = { } ;
46
48
47
49
constructor ( options ?: GatewayOptions ) {
48
50
if ( options ?. pluginsIndexUrl ) {
49
51
this . pluginIndexUrl = options . pluginsIndexUrl ;
50
52
}
53
+
54
+ if ( options ?. defaultPluginSettings ) {
55
+ this . defaultPluginSettings = options . defaultPluginSettings ;
56
+ }
57
+
51
58
if ( options ?. Validator ) {
52
59
this . _validator = options . Validator ;
53
60
}
@@ -184,10 +191,12 @@ export class Gateway {
184
191
185
192
// console.log(`[${identifier}] plugin manifest:`, manifest);
186
193
194
+ const defaultSettings = this . defaultPluginSettings [ identifier ] || { } ;
195
+ const finalSettings = { ...defaultSettings , ...settings } ;
187
196
// ========== 6. 校验是否按照 manifest 包含了 settings 配置 ========== //
188
197
189
198
if ( manifest . settings ) {
190
- const { valid, errors } = await this . validate ( manifest . settings as any , settings || { } ) ;
199
+ const { valid, errors } = await this . validate ( manifest . settings as any , finalSettings ) ;
191
200
192
201
if ( ! valid )
193
202
return this . createErrorResponse ( PluginErrorType . PluginSettingsInvalid , {
@@ -225,10 +234,10 @@ export class Gateway {
225
234
226
235
// ========== 8. 兼容 OpenAPI 请求模式 ========== //
227
236
if ( manifest . openapi ) {
228
- return await this . callOpenAPI ( payload , settings , manifest ) ;
237
+ return await this . callOpenAPI ( payload , finalSettings , manifest ) ;
229
238
}
230
239
231
- return await this . callApi ( api , args , settings ) ;
240
+ return await this . callApi ( api , args , finalSettings ) ;
232
241
} ;
233
242
234
243
private async callApi (
@@ -263,7 +272,7 @@ export class Gateway {
263
272
264
273
private async callOpenAPI (
265
274
payload : PluginRequestPayload ,
266
- settings : any ,
275
+ settings : any = { } ,
267
276
manifest : LobeChatPluginManifest ,
268
277
) : Promise < GatewaySuccessResponse > {
269
278
const { arguments : args , apiName } = payload ;
@@ -282,7 +291,7 @@ export class Gateway {
282
291
} ;
283
292
284
293
// 根据 settings 中的每个属性来构建 authorizations 对象
285
- for ( const [ key , value ] of Object . entries ( settings || { } ) ) {
294
+ for ( const [ key , value ] of Object . entries ( settings ) ) {
286
295
// 处理 API Key 和 Bearer Token
287
296
authorizations [ key ] = value as string ;
288
297
0 commit comments