generated from arvinxx/vercel-serverless-api-template
-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
gateway.ts
360 lines (300 loc) · 12 KB
/
gateway.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// reason to use cfworker json schema:
// https://github.com/vercel/next.js/discussions/47063#discussioncomment-5303951
import { Schema } from '@cfworker/json-schema';
import {
IPluginErrorType,
LobeChatPluginApi,
LobeChatPluginManifest,
LobeChatPluginsMarketIndex,
PluginErrorType,
PluginRequestPayload,
createHeadersWithPluginSettings,
marketIndexSchema,
pluginManifestSchema,
pluginMetaSchema,
pluginRequestPayloadSchema,
} from '@lobehub/chat-plugin-sdk';
import { OPENAPI_REQUEST_BODY_KEY } from '@lobehub/chat-plugin-sdk/openapi';
// @ts-ignore
import SwaggerClient from 'swagger-client';
export const DEFAULT_PLUGINS_INDEX_URL = 'https://chat-plugins.lobehub.com';
type IValidator = (schema: Schema, value: any) => { errors?: any; valid: boolean };
export interface GatewayOptions {
Validator?: IValidator;
defaultPluginSettings?: Record<string, Record<string, any>>;
/**
* @default https://chat-plugins.lobehub.com
*/
pluginsIndexUrl?: string;
}
export interface GatewaySuccessResponse {
data: string;
success: true;
}
export interface GatewayErrorResponse {
body: string | object;
errorType: IPluginErrorType;
success: false;
}
export class Gateway {
private pluginIndexUrl = DEFAULT_PLUGINS_INDEX_URL;
private _validator: IValidator | undefined;
private defaultPluginSettings: Record<string, Record<string, any>> = {};
constructor(options?: GatewayOptions) {
if (options?.pluginsIndexUrl) {
this.pluginIndexUrl = options.pluginsIndexUrl;
}
if (options?.defaultPluginSettings) {
this.defaultPluginSettings = options.defaultPluginSettings;
}
if (options?.Validator) {
this._validator = options.Validator;
}
}
private createSuccessResponse = (data: string) => {
return { data, success: true } as const;
};
private createErrorResponse = (errorType: IPluginErrorType | string, body?: string | object) => {
throw { body, errorType, success: false };
};
private async validate(schema: Schema, value: any) {
if (this._validator) return this._validator(schema, value);
// reason to use cfworker json schema:
// https://github.com/vercel/next.js/discussions/47063#discussioncomment-5303951
const { Validator } = await import('@cfworker/json-schema');
const v = new Validator(schema);
const validator = v.validate(value);
if (!validator.valid) return { errors: validator.errors, valid: false };
return { valid: true };
}
execute = async (
payload: PluginRequestPayload,
settings?: any,
): Promise<GatewaySuccessResponse> => {
// ========== 2. 校验请求入参基础格式 ========== //
const payloadParseResult = pluginRequestPayloadSchema.safeParse(payload);
if (!payloadParseResult.success)
return this.createErrorResponse(PluginErrorType.BadRequest, payloadParseResult.error);
const { identifier, arguments: args, indexUrl, apiName } = payload;
let manifest = payload.manifest as LobeChatPluginManifest | undefined;
console.info(`[${identifier}] - ${apiName} `);
// 入参中如果没有 manifest,则从插件市场索引中获取
if (!manifest) {
const marketIndexUrl = indexUrl ?? this.pluginIndexUrl;
// ========== 3. 获取插件市场索引 ========== //
let marketIndex: LobeChatPluginsMarketIndex | undefined;
try {
const indexRes = await fetch(marketIndexUrl);
marketIndex = await indexRes.json();
} catch (error) {
console.error(error);
marketIndex = undefined;
}
// 插件市场索引不存在
if (!marketIndex)
return this.createErrorResponse(PluginErrorType.PluginMarketIndexNotFound, {
indexUrl: marketIndexUrl,
message: '[gateway] plugin market index not found',
});
// 插件市场索引解析失败
const indexParseResult = marketIndexSchema.safeParse(marketIndex);
if (!indexParseResult.success)
return this.createErrorResponse(PluginErrorType.PluginMarketIndexInvalid, {
error: indexParseResult.error,
indexUrl: marketIndexUrl,
marketIndex,
message: '[gateway] plugin market index is invalid',
});
console.info(
`[marketIndex V${marketIndex.schemaVersion}] total ${marketIndex.plugins.length} plugins`,
);
// ========== 4. 校验插件 meta 完备性 ========== //
const pluginMeta = marketIndex.plugins.find((i) => i.identifier === identifier);
// 一个不规范的插件示例
// const pluginMeta = {
// createAt: '2023-08-12',
// homepage: 'https://github.com/lobehub/chat-plugin-real-time-weather',
// manifest: 'https://registry.npmmirror.com/@lobehub/lobe-chat-plugins/latest/files',
// meta: {
// avatar: '☂️',
// tags: ['weather', 'realtime'],
// },
// name: 'realtimeWeather',
// schemaVersion: 'v1',
// };
// 校验插件是否存在
if (!pluginMeta)
return this.createErrorResponse(PluginErrorType.PluginMetaNotFound, {
identifier,
message: `[gateway] plugin '${identifier}' is not found,please check the plugin list in ${marketIndexUrl}, or create an issue to [lobe-chat-plugins](https://github.com/lobehub/lobe-chat-plugins/issues)`,
});
const metaParseResult = pluginMetaSchema.safeParse(pluginMeta);
if (!metaParseResult.success)
return this.createErrorResponse(PluginErrorType.PluginMetaInvalid, {
error: metaParseResult.error,
message: '[plugin] plugin meta is invalid',
pluginMeta,
});
// ========== 5. 校验插件 manifest 完备性 ========== //
// 获取插件的 manifest
try {
const pluginRes = await fetch(pluginMeta.manifest);
manifest = (await pluginRes.json()) as LobeChatPluginManifest;
} catch (error) {
console.error(error);
manifest = undefined;
}
if (!manifest)
return this.createErrorResponse(PluginErrorType.PluginManifestNotFound, {
manifestUrl: pluginMeta.manifest,
message: '[plugin] plugin manifest not found',
});
}
const manifestParseResult = pluginManifestSchema.safeParse(manifest);
if (!manifestParseResult.success)
return this.createErrorResponse(PluginErrorType.PluginManifestInvalid, {
error: manifestParseResult.error,
manifest: manifest,
message: '[plugin] plugin manifest is invalid',
});
// console.log(`[${identifier}] plugin manifest:`, manifest);
const defaultSettings = this.defaultPluginSettings[identifier] || {};
const finalSettings = { ...defaultSettings, ...settings };
// ========== 6. 校验是否按照 manifest 包含了 settings 配置 ========== //
if (manifest.settings) {
const { valid, errors } = await this.validate(manifest.settings as any, finalSettings);
if (!valid)
return this.createErrorResponse(PluginErrorType.PluginSettingsInvalid, {
error: errors,
message: '[plugin] your settings is invalid with plugin manifest setting schema',
settings,
});
}
// ========== 7. 校验请求入参与 manifest 要求一致性 ========== //
const api = manifest.api.find((i) => i.name === apiName);
if (!api)
return this.createErrorResponse(PluginErrorType.PluginApiNotFound, {
manifest,
message: '[plugin] api not found',
request: {
apiName,
identifier,
},
});
if (args) {
const params = JSON.parse(args!);
const { valid, errors } = await this.validate(api.parameters as any, params);
if (!valid)
return this.createErrorResponse(PluginErrorType.PluginApiParamsError, {
api,
error: errors,
message: '[plugin] args is invalid with plugin manifest api schema',
request: params,
});
}
// ========== 8. 兼容 OpenAPI 请求模式 ========== //
if (manifest.openapi) {
return await this.callOpenAPI(payload, finalSettings, manifest);
}
return await this.callApi(api, args, finalSettings);
};
private async callApi(
api: LobeChatPluginApi,
args: string | undefined,
settings: any,
): Promise<GatewaySuccessResponse> {
if (!api.url)
return this.createErrorResponse(PluginErrorType.PluginApiParamsError, {
api,
message: '[plugin] missing api url',
});
const response = await fetch(api.url, {
body: args,
headers: createHeadersWithPluginSettings(settings),
method: 'POST',
});
// ========== 9. 发送请求 ========== //
// 不正常的错误,直接返回请求
if (!response.ok)
return this.createErrorResponse(response.status as IPluginErrorType, await response.text());
const data = await response.text();
// console.log(`[${identifier}]`, args, `result:`, JSON.stringify(data).slice(0, 1000));
return this.createSuccessResponse(data);
}
private async callOpenAPI(
payload: PluginRequestPayload,
settings: any = {},
manifest: LobeChatPluginManifest,
): Promise<GatewaySuccessResponse> {
const { arguments: args, apiName } = payload;
// @ts-ignore
// const { default: SwaggerClient } = await import('swagger-client');
const authorizations = {} as {
[key: string]: any;
basicAuth?: any;
oauth2?: {
accessToken: string;
clientId: string;
clientSecret: string;
};
};
// 根据 settings 中的每个属性来构建 authorizations 对象
for (const [key, value] of Object.entries(settings)) {
// 处理 API Key 和 Bearer Token
authorizations[key] = value as string;
// TODO: Basic Auth and OAuth2
// if (key.endsWith('_username') && key.endsWith('_password')) {
// // 处理 HTTP Basic Authentication
// const username = settings[key];
// const password = settings[key.replace('_username', '_password')];
// authorizations.basicAuth = new SwaggerClient.PasswordAuthorization(username, password);
// console.log(authorizations.basicAuth);
// } else if (
// key.endsWith('_clientId') &&
// key.endsWith('_clientSecret') &&
// key.endsWith('_accessToken')
// ) {
// // 处理 OAuth2
// const clientId = settings[key];
// const clientSecret = settings[key.replace('_clientId', '_clientSecret')];
// const accessToken = settings[key.replace('_clientId', '_accessToken')];
// authorizations.oauth2 = { accessToken, clientId, clientSecret };
// }
}
let client;
try {
client = await SwaggerClient({ authorizations, url: manifest.openapi });
} catch (error) {
return this.createErrorResponse(PluginErrorType.PluginOpenApiInitError, {
error: error,
message: '[plugin] openapi client init error',
openapi: manifest.openapi,
});
}
const requestParams = JSON.parse(args || '{}');
const { [OPENAPI_REQUEST_BODY_KEY]: requestBody, ...parameters } = requestParams;
try {
const res = await client.execute({ operationId: apiName, parameters, requestBody });
return this.createSuccessResponse(res.text);
} catch (error) {
// 如果没有 status,说明没有发送请求,可能是 openapi 相关调用实现的问题
if (!(error as any).status) {
console.error(error);
return this.createErrorResponse(PluginErrorType.PluginGatewayError, {
apiName,
authorizations,
error: (error as Error).message,
message:
'[plugin] there are problem with sending openapi request, please contact with LobeHub Team',
openapi: manifest.openapi,
parameters,
requestBody,
});
}
// 如果是 401 则说明是鉴权问题
if ((error as Response).status === 401)
return this.createErrorResponse(PluginErrorType.PluginSettingsInvalid);
return this.createErrorResponse(PluginErrorType.PluginServerError, { error });
}
}
}