Skip to content

Commit

Permalink
♻️ refactor: refactor the core chatStream and plugin gateway auth (lo…
Browse files Browse the repository at this point in the history
…behub#1426)

* ♻️ refactor: refactor the core chatStream

* ♻️ refactor: refactor the plugin api auth

* ✅ test: add google tests

* ✅ test: add google tests

* ✅ test: add google tests

* ♻️ refactor: refactor the plugin api auth
  • Loading branch information
arvinxx authored and 573dave committed Mar 5, 2024
1 parent 51b5a63 commit 56c0427
Show file tree
Hide file tree
Showing 13 changed files with 585 additions and 174 deletions.
2 changes: 1 addition & 1 deletion src/app/api/chat/[provider]/agentRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class AgentRuntime {
const { GOOGLE_API_KEY } = getServerConfig();
const apiKey = apiKeyManager.pick(payload?.apiKey || GOOGLE_API_KEY);

return new LobeGoogleAI(apiKey);
return new LobeGoogleAI({ apiKey });
}

private static initBedrock(payload: JWTPayload) {
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/chat/google/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { POST as UniverseRoute } from '../[provider]/route';
//
// setGlobalDispatcher(new ProxyAgent({ uri: process.env.HTTP_PROXY_URL }));
// }
// undici only can be used in NodeJS
// export const runtime = 'nodejs';

// but undici only can be used in NodeJS
// so if you want to use with proxy, you need comment the code below
export const runtime = 'edge';

export const preferredRegion = [
Expand Down
13 changes: 10 additions & 3 deletions src/app/api/plugin/gateway/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createGatewayOnEdgeRuntime } from '@lobehub/chat-plugins-gateway';

import { getJWTPayload } from '@/app/api/chat/auth';
import { createErrorResponse } from '@/app/api/errorResponse';
import { getServerConfig } from '@/config/server';
import { getOpenAIAuthFromRequest } from '@/const/fetch';
import { LOBE_CHAT_AUTH_HEADER, OAUTH_AUTHORIZED } from '@/const/auth';
import { AgentRuntimeError } from '@/libs/agent-runtime';
import { ChatErrorType, ErrorType } from '@/types/fetch';

import { parserPluginSettings } from './settings';
Expand Down Expand Up @@ -33,9 +35,14 @@ const defaultPluginSettings = parserPluginSettings(PLUGIN_SETTINGS);
const handler = createGatewayOnEdgeRuntime({ defaultPluginSettings, pluginsIndexUrl });

export const POST = async (req: Request) => {
const { accessCode, oauthAuthorized } = getOpenAIAuthFromRequest(req);
// get Authorization from header
const authorization = req.headers.get(LOBE_CHAT_AUTH_HEADER);
if (!authorization) throw AgentRuntimeError.createError(ChatErrorType.Unauthorized);

const result = checkAuth(accessCode, oauthAuthorized);
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);
Expand Down
4 changes: 0 additions & 4 deletions src/config/server/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ declare global {

// Ollama Provider;
OLLAMA_PROXY_URL?: string;

DEBUG_CHAT_COMPLETION?: string;
}
}
}
Expand Down Expand Up @@ -100,7 +98,5 @@ export const getProviderConfig = () => {

ENABLE_OLLAMA: !!process.env.OLLAMA_PROXY_URL,
OLLAMA_PROXY_URL: process.env.OLLAMA_PROXY_URL || '',

DEBUG_CHAT_COMPLETION: process.env.DEBUG_CHAT_COMPLETION === '1',
};
};
Loading

0 comments on commit 56c0427

Please sign in to comment.