Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ refactor: refactor the core chatStream #1426

Merged
merged 6 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
♻️ refactor: refactor the plugin api auth
  • Loading branch information
arvinxx committed Feb 29, 2024
commit bbc7d79bc97b259bcc9a6d0e18bafc0d2acce807
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);
const oauthAuthorized = !!req.headers.get(OAUTH_AUTHORIZED);
if (!authorization) throw AgentRuntimeError.createError(ChatErrorType.Unauthorized);

const result = checkAuth(accessCode, oauthAuthorized);
const payload = await getJWTPayload(authorization);

const result = checkAuth(payload.accessCode!, oauthAuthorized);

if (!result.auth) {
return createErrorResponse(result.error as ErrorType);
Expand Down
4 changes: 2 additions & 2 deletions src/services/__tests__/chat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,10 +574,10 @@ Get data from users`,

global.fetch = vi.fn(() => Promise.resolve(mockResponse));

const text = await chatService.runPluginApi(params, options);
const result = await chatService.runPluginApi(params, options);

expect(global.fetch).toHaveBeenCalledWith(expect.any(String), expect.any(Object));
expect(text).toBe('Plugin Result');
expect(result).toBe('Plugin Result');
});

// Add more test cases to cover different scenarios and edge cases
Expand Down
11 changes: 7 additions & 4 deletions src/services/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,15 @@
const settings = pluginSelectors.getPluginSettingsById(params.identifier)(s);
const manifest = pluginSelectors.getPluginManifestById(params.identifier)(s);

const gatewayURL = manifest?.gateway;
const headers = await createHeaderWithAuth({
headers: { ...createHeadersWithPluginSettings(settings) },
});

const res = await fetch(gatewayURL ?? API_ENDPOINTS.gateway, {
const gatewayURL = manifest?.gateway ?? API_ENDPOINTS.gateway;

const res = await fetch(gatewayURL, {
body: JSON.stringify({ ...params, manifest }),
// TODO: we can have a better auth way
headers: createHeadersWithPluginSettings(settings, createHeaderWithOpenAI()),
headers,
method: 'POST',
signal: options?.signal,
});
Expand All @@ -172,8 +175,8 @@
}: FetchAITaskResultParams) => {
const errorHandle = (error: Error, errorContent?: any) => {
onLoadingChange?.(false);
if (abortController?.signal.aborted) {
return;

Check warning on line 179 in src/services/chat.ts

View check run for this annotation

Codecov / codecov/patch

src/services/chat.ts#L178-L179

Added lines #L178 - L179 were not covered by tests
}
onError?.(error, errorContent);
};
Expand Down
Loading