-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
route.ts
82 lines (59 loc) 路 2.67 KB
/
route.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
import { PluginRequestPayload } from '@lobehub/chat-plugin-sdk';
import { createGatewayOnEdgeRuntime } from '@lobehub/chat-plugins-gateway';
import { createErrorResponse } from '@/app/api/errorResponse';
import { getJWTPayload } from '@/app/api/middleware/auth/utils';
import { getAppConfig } from '@/config/app';
import { LOBE_CHAT_AUTH_HEADER, OAUTH_AUTHORIZED, enableNextAuth } from '@/const/auth';
import { LOBE_CHAT_TRACE_ID, TraceNameMap } from '@/const/trace';
import { AgentRuntimeError } from '@/libs/agent-runtime';
import { TraceClient } from '@/libs/traces';
import { ChatErrorType, ErrorType } from '@/types/fetch';
import { getTracePayload } from '@/utils/trace';
import { parserPluginSettings } from './settings';
const checkAuth = (accessCode: string | null, oauthAuthorized: boolean | null) => {
const { ACCESS_CODES, PLUGIN_SETTINGS } = getAppConfig();
// if there is no plugin settings, just skip the auth
if (!PLUGIN_SETTINGS) return { auth: true };
// If authorized by oauth
if (oauthAuthorized && enableNextAuth) return { auth: true };
// if accessCode doesn't exist
if (!ACCESS_CODES.length) return { auth: true };
if (!accessCode || !ACCESS_CODES.includes(accessCode)) {
return { auth: false, error: ChatErrorType.InvalidAccessCode };
}
return { auth: true };
};
const { PLUGINS_INDEX_URL: pluginsIndexUrl, PLUGIN_SETTINGS } = getAppConfig();
const defaultPluginSettings = parserPluginSettings(PLUGIN_SETTINGS);
const handler = createGatewayOnEdgeRuntime({ defaultPluginSettings, pluginsIndexUrl });
export const POST = async (req: Request) => {
// get Authorization from header
const authorization = req.headers.get(LOBE_CHAT_AUTH_HEADER);
if (!authorization) throw AgentRuntimeError.createError(ChatErrorType.Unauthorized);
const oauthAuthorized = !!req.headers.get(OAUTH_AUTHORIZED);
const payload = await getJWTPayload(authorization);
const result = checkAuth(payload.accessCode!, oauthAuthorized);
if (!result.auth) {
return createErrorResponse(result.error as ErrorType);
}
// add trace
const tracePayload = getTracePayload(req);
const traceClient = new TraceClient();
const trace = traceClient.createTrace({
id: tracePayload?.traceId,
...tracePayload,
});
const { manifest, indexUrl, ...input } = (await req.clone().json()) as PluginRequestPayload;
const span = trace?.span({
input,
metadata: { indexUrl, manifest },
name: TraceNameMap.FetchPluginAPI,
});
span?.update({ parentObservationId: tracePayload?.observationId });
const res = await handler(req);
span?.end({ output: await res.clone().text() });
if (trace?.id) {
res.headers.set(LOBE_CHAT_TRACE_ID, trace.id);
}
return res;
};