Skip to content

Commit fea957e

Browse files
committed
✨ feat: support defaultPluginSettings options on Gateway
1 parent 1fb770f commit fea957e

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

src/gateway.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type IValidator = (schema: Schema, value: any) => { errors?: any; valid: boolean
2424

2525
export interface GatewayOptions {
2626
Validator?: IValidator;
27+
defaultPluginSettings?: Record<string, Record<string, any>>;
2728
/**
2829
* @default https://chat-plugins.lobehub.com
2930
*/
@@ -43,11 +44,17 @@ export interface GatewayErrorResponse {
4344
export class Gateway {
4445
private pluginIndexUrl = DEFAULT_PLUGINS_INDEX_URL;
4546
private _validator: IValidator | undefined;
47+
private defaultPluginSettings: Record<string, Record<string, any>> = {};
4648

4749
constructor(options?: GatewayOptions) {
4850
if (options?.pluginsIndexUrl) {
4951
this.pluginIndexUrl = options.pluginsIndexUrl;
5052
}
53+
54+
if (options?.defaultPluginSettings) {
55+
this.defaultPluginSettings = options.defaultPluginSettings;
56+
}
57+
5158
if (options?.Validator) {
5259
this._validator = options.Validator;
5360
}
@@ -184,10 +191,12 @@ export class Gateway {
184191

185192
// console.log(`[${identifier}] plugin manifest:`, manifest);
186193

194+
const defaultSettings = this.defaultPluginSettings[identifier] || {};
195+
const finalSettings = { ...defaultSettings, ...settings };
187196
// ========== 6. 校验是否按照 manifest 包含了 settings 配置 ========== //
188197

189198
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);
191200

192201
if (!valid)
193202
return this.createErrorResponse(PluginErrorType.PluginSettingsInvalid, {
@@ -225,10 +234,10 @@ export class Gateway {
225234

226235
// ========== 8. 兼容 OpenAPI 请求模式 ========== //
227236
if (manifest.openapi) {
228-
return await this.callOpenAPI(payload, settings, manifest);
237+
return await this.callOpenAPI(payload, finalSettings, manifest);
229238
}
230239

231-
return await this.callApi(api, args, settings);
240+
return await this.callApi(api, args, finalSettings);
232241
};
233242

234243
private async callApi(
@@ -263,7 +272,7 @@ export class Gateway {
263272

264273
private async callOpenAPI(
265274
payload: PluginRequestPayload,
266-
settings: any,
275+
settings: any = {},
267276
manifest: LobeChatPluginManifest,
268277
): Promise<GatewaySuccessResponse> {
269278
const { arguments: args, apiName } = payload;
@@ -282,7 +291,7 @@ export class Gateway {
282291
};
283292

284293
// 根据 settings 中的每个属性来构建 authorizations 对象
285-
for (const [key, value] of Object.entries(settings || {})) {
294+
for (const [key, value] of Object.entries(settings)) {
286295
// 处理 API Key 和 Bearer Token
287296
authorizations[key] = value as string;
288297

tests/__snapshots__/edge.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ exports[`createGatewayOnEdgeRuntime > Error > should return PluginMetaInvalid er
200200
"expected": "string",
201201
"message": "Required",
202202
"path": [
203-
"createAt",
203+
"createdAt",
204204
],
205205
"received": "undefined",
206206
},

tests/edge.test.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
PluginRequestPayload,
55
createHeadersWithPluginSettings,
66
} from '@lobehub/chat-plugin-sdk';
7+
import { LOBE_PLUGIN_SETTINGS } from '@lobehub/chat-plugin-sdk/lib/request';
78
import { createGatewayOnEdgeRuntime } from '@lobehub/chat-plugins-gateway';
89
// @ts-ignore
910
import SwaggerClient from 'swagger-client';
@@ -107,6 +108,36 @@ describe('createGatewayOnEdgeRuntime', () => {
107108
expect(data).toEqual({ success: true });
108109
});
109110

111+
describe('with defaultPluginSettings', () => {
112+
it('should execute successfully when provided with defaultPluginSettings payload', async () => {
113+
(fetch as Mock).mockImplementation(async (url, { headers }) => {
114+
if (url === 'https://test-market-index-url.com')
115+
return {
116+
json: async () => mockMarketIndex,
117+
ok: true,
118+
};
119+
120+
return new Response(JSON.stringify({ headers }), { status: 200 });
121+
});
122+
123+
const config = { abc: '123' };
124+
125+
gateway = createGatewayOnEdgeRuntime({ defaultPluginSettings: { 'test-plugin': config } });
126+
127+
const request: Request = new Request('https://test-url.com', {
128+
body: JSON.stringify(mockPluginRequestPayload),
129+
method: 'POST',
130+
});
131+
132+
const response = await gateway(request);
133+
const data = await response.json();
134+
expect(response.status).toBe(200);
135+
expect(data).toEqual({
136+
headers: { [LOBE_PLUGIN_SETTINGS]: JSON.stringify(config) },
137+
});
138+
});
139+
});
140+
110141
describe('Error', () => {
111142
it('only allow post method', async () => {
112143
const invalidPayload = { ...mockPluginRequestPayload, identifier: null }; // Invalid payload
@@ -210,7 +241,7 @@ describe('createGatewayOnEdgeRuntime', () => {
210241
plugins: [
211242
{
212243
author: 'test-plugin',
213-
createAt: '2023-08-12',
244+
createdAt: '2023-08-12',
214245
homepage: 'https://github.com/lobehub/chat-plugin-real-time-weather',
215246
identifier: 'test-plugin',
216247
manifest: 'https://test-plugin-url.com/manifest.json',
@@ -256,7 +287,7 @@ describe('createGatewayOnEdgeRuntime', () => {
256287
plugins: [
257288
{
258289
author: 'test-plugin',
259-
createAt: '2023-08-12',
290+
createdAt: '2023-08-12',
260291
homepage: 'https://github.com/lobehub/chat-plugin-real-time-weather',
261292
identifier: 'test-plugin',
262293
manifest: 'https://test-plugin-url.com/manifest.json',

0 commit comments

Comments
 (0)