Skip to content

Commit

Permalink
♻️ refactor: refactor the backend code for better organization (#4287)
Browse files Browse the repository at this point in the history
* ♻️ refactor: move AgentRuntime

* ♻️ refactor: move errorResponse

* ♻️ refactor: move auth middleware

* ♻️ refactor: move createBizOpenAI

* 🐛 fix: fix rewriteQuery

* fix
  • Loading branch information
arvinxx authored Oct 11, 2024
1 parent 04af49a commit 9a369ac
Show file tree
Hide file tree
Showing 32 changed files with 30 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import OpenAI from 'openai';

import { getOpenAIAuthFromRequest } from '@/const/fetch';
import { ChatErrorType, ErrorType } from '@/types/fetch';
import { createErrorResponse } from '@/utils/errorResponse';

import { createErrorResponse } from '../../errorResponse';
import { checkAuth } from './auth';
import { createOpenai } from './createOpenai';

Expand Down
4 changes: 2 additions & 2 deletions src/app/(backend)/api/chat/[provider]/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { getAuth } from '@clerk/nextjs/server';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { checkAuthMethod, getJWTPayload } from '@/app/(backend)/api/middleware/auth/utils';
import { checkAuthMethod, getJWTPayload } from '@/app/(backend)/middleware/auth/utils';
import { LOBE_CHAT_AUTH_HEADER, OAUTH_AUTHORIZED } from '@/const/auth';
import { AgentRuntime, LobeRuntimeAI } from '@/libs/agent-runtime';
import { ChatErrorType } from '@/types/fetch';
Expand All @@ -13,7 +13,7 @@ vi.mock('@clerk/nextjs/server', () => ({
getAuth: vi.fn(),
}));

vi.mock('../../middleware/auth/utils', () => ({
vi.mock('@/app/(backend)/middleware/auth/utils', () => ({
getJWTPayload: vi.fn(),
checkAuthMethod: vi.fn(),
}));
Expand Down
7 changes: 3 additions & 4 deletions src/app/(backend)/api/chat/[provider]/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { createErrorResponse } from '@/app/(backend)/api/errorResponse';
import { checkAuth } from '@/app/(backend)/middleware/auth';
import { AgentRuntime, ChatCompletionErrorPayload } from '@/libs/agent-runtime';
import { createTraceOptions, initAgentRuntimeWithUserPayload } from '@/server/modules/AgentRuntime';
import { ChatErrorType } from '@/types/fetch';
import { ChatStreamPayload } from '@/types/openai/chat';
import { createErrorResponse } from '@/utils/errorResponse';
import { getTracePayload } from '@/utils/trace';

import { checkAuth } from '../../middleware/auth';
import { createTraceOptions, initAgentRuntimeWithUserPayload } from '../agentRuntime';

export const runtime = 'edge';

export const POST = checkAuth(async (req: Request, { params, jwtPayload, createRuntime }) => {
Expand Down
7 changes: 3 additions & 4 deletions src/app/(backend)/api/chat/models/[provider]/route.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { NextResponse } from 'next/server';

import { createErrorResponse } from '@/app/(backend)/api/errorResponse';
import { checkAuth } from '@/app/(backend)/middleware/auth';
import { ChatCompletionErrorPayload, ModelProvider } from '@/libs/agent-runtime';
import { initAgentRuntimeWithUserPayload } from '@/server/modules/AgentRuntime';
import { ChatErrorType } from '@/types/fetch';

import { checkAuth } from '../../../middleware/auth';
import { initAgentRuntimeWithUserPayload } from '../../agentRuntime';
import { createErrorResponse } from '@/utils/errorResponse';

export const runtime = 'edge';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getAuth } from '@clerk/nextjs/server';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { createErrorResponse } from '@/app/(backend)/api/errorResponse';
import { AgentRuntimeError } from '@/libs/agent-runtime';
import { ChatErrorType } from '@/types/fetch';
import { createErrorResponse } from '@/utils/errorResponse';

import { RequestHandler, checkAuth } from './index';
import { checkAuthMethod, getJWTPayload } from './utils';
Expand All @@ -12,7 +12,7 @@ vi.mock('@clerk/nextjs/server', () => ({
getAuth: vi.fn(),
}));

vi.mock('@/app/(backend)/api/errorResponse', () => ({
vi.mock('@/utils/errorResponse', () => ({
createErrorResponse: vi.fn(),
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { AuthObject } from '@clerk/backend';
import { getAuth } from '@clerk/nextjs/server';
import { NextRequest } from 'next/server';

import { createErrorResponse } from '@/app/(backend)/api/errorResponse';
import { JWTPayload, LOBE_CHAT_AUTH_HEADER, OAUTH_AUTHORIZED, enableClerk } from '@/const/auth';
import { AgentRuntime, AgentRuntimeError, ChatCompletionErrorPayload } from '@/libs/agent-runtime';
import { ChatErrorType } from '@/types/fetch';
import { createErrorResponse } from '@/utils/errorResponse';

import { checkAuthMethod, getJWTPayload } from './utils';

Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/app/(backend)/webapi/plugin/gateway/route.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { PluginRequestPayload } from '@lobehub/chat-plugin-sdk';
import { createGatewayOnEdgeRuntime } from '@lobehub/chat-plugins-gateway';

import { createErrorResponse } from '@/app/(backend)/api/errorResponse';
import { getJWTPayload } from '@/app/(backend)/api/middleware/auth/utils';
import { getJWTPayload } from '@/app/(backend)/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 { createErrorResponse } from '@/utils/errorResponse';
import { getTracePayload } from '@/utils/trace';

import { parserPluginSettings } from './settings';
Expand Down
2 changes: 1 addition & 1 deletion src/app/(backend)/webapi/stt/openai/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OpenAISTTPayload } from '@lobehub/tts';
import { createOpenaiAudioTranscriptions } from '@lobehub/tts/server';

import { createBizOpenAI } from '@/app/(backend)/api/openai/createBizOpenAI';
import { createBizOpenAI } from '@/app/(backend)/_deprecated/createBizOpenAI';

export const runtime = 'edge';

Expand Down
6 changes: 3 additions & 3 deletions src/app/(backend)/webapi/text-to-image/[provider]/route.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { NextResponse } from 'next/server';

import { initAgentRuntimeWithUserPayload } from '@/app/(backend)/api/chat/agentRuntime';
import { createErrorResponse } from '@/app/(backend)/api/errorResponse';
import { checkAuth } from '@/app/(backend)/api/middleware/auth';
import { checkAuth } from '@/app/(backend)/middleware/auth';
import { ChatCompletionErrorPayload } from '@/libs/agent-runtime';
import { TextToImagePayload } from '@/libs/agent-runtime/types';
import { initAgentRuntimeWithUserPayload } from '@/server/modules/AgentRuntime';
import { ChatErrorType } from '@/types/fetch';
import { createErrorResponse } from '@/utils/errorResponse';

export const runtime = 'edge';

Expand Down
2 changes: 1 addition & 1 deletion src/app/(backend)/webapi/tts/openai/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OpenAITTSPayload } from '@lobehub/tts';
import { createOpenaiAudioSpeech } from '@lobehub/tts/server';

import { createBizOpenAI } from '@/app/(backend)/api/openai/createBizOpenAI';
import { createBizOpenAI } from '@/app/(backend)/_deprecated/createBizOpenAI';

export const runtime = 'edge';

Expand Down
2 changes: 1 addition & 1 deletion src/libs/agent-runtime/AgentRuntime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { LangfuseGenerationClient, LangfuseTraceClient } from 'langfuse-core';
import { ClientOptions } from 'openai';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { createTraceOptions } from '@/app/(backend)/api/chat/agentRuntime';
import * as langfuseCfg from '@/config/langfuse';
import { JWTPayload } from '@/const/auth';
import { TraceNameMap } from '@/const/trace';
Expand All @@ -28,6 +27,7 @@ import {
} from '@/libs/agent-runtime';
import { LobeStepfunAI } from '@/libs/agent-runtime/stepfun';
import LobeWenxinAI from '@/libs/agent-runtime/wenxin';
import { createTraceOptions } from '@/server/modules/AgentRuntime';

import { AgentChatOptions } from './AgentRuntime';
import { LobeBedrockAIParams } from './bedrock';
Expand Down
2 changes: 1 addition & 1 deletion src/libs/trpc/middleware/jwtPayload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { TRPCError } from '@trpc/server';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import * as utils from '@/app/(backend)/api/middleware/auth/utils';
import * as utils from '@/app/(backend)/middleware/auth/utils';
import { createCallerFactory } from '@/libs/trpc';
import { trpc } from '@/libs/trpc/init';
import { AuthContext, createContextInner } from '@/server/context';
Expand Down
2 changes: 1 addition & 1 deletion src/libs/trpc/middleware/jwtPayload.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TRPCError } from '@trpc/server';

import { getJWTPayload } from '@/app/(backend)/api/middleware/auth/utils';
import { getJWTPayload } from '@/app/(backend)/middleware/auth/utils';
import { trpc } from '@/libs/trpc/init';

export const jwtPayloadChecker = trpc.middleware(async (opts) => {
Expand Down
2 changes: 1 addition & 1 deletion src/libs/trpc/middleware/keyVaults.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TRPCError } from '@trpc/server';

import { getJWTPayload } from '@/app/(backend)/api/middleware/auth/utils';
import { getJWTPayload } from '@/app/(backend)/middleware/auth/utils';
import { trpc } from '@/libs/trpc/init';

export const keyVaults = trpc.middleware(async (opts) => {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { AgentRuntime } from '@/libs/agent-runtime';
import { LobeStepfunAI } from '@/libs/agent-runtime/stepfun';
import LobeWenxinAI from '@/libs/agent-runtime/wenxin';

import { initAgentRuntimeWithUserPayload } from './agentRuntime';
import { initAgentRuntimeWithUserPayload } from './index';

// 模拟依赖项
vi.mock('@/config/llm', () => ({
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/server/routers/async/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { chunk } from 'lodash-es';
import pMap from 'p-map';
import { z } from 'zod';

import { initAgentRuntimeWithUserPayload } from '@/app/(backend)/api/chat/agentRuntime';
import { fileEnv } from '@/config/file';
import { DEFAULT_EMBEDDING_MODEL } from '@/const/settings';
import { ASYNC_TASK_TIMEOUT, AsyncTaskModel } from '@/database/server/models/asyncTask';
Expand All @@ -13,6 +12,7 @@ import { FileModel } from '@/database/server/models/file';
import { NewChunkItem, NewEmbeddingsItem } from '@/database/server/schemas/lobechat';
import { ModelProvider } from '@/libs/agent-runtime';
import { asyncAuthedProcedure, asyncRouter as router } from '@/libs/trpc/async';
import { initAgentRuntimeWithUserPayload } from '@/server/modules/AgentRuntime';
import { S3 } from '@/server/modules/S3';
import { ChunkService } from '@/server/services/chunk';
import {
Expand Down
2 changes: 1 addition & 1 deletion src/server/routers/async/ragEval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { TRPCError } from '@trpc/server';
import OpenAI from 'openai';
import { z } from 'zod';

import { initAgentRuntimeWithUserPayload } from '@/app/(backend)/api/chat/agentRuntime';
import { chainAnswerWithContext } from '@/chains/answerWithContext';
import { DEFAULT_EMBEDDING_MODEL, DEFAULT_MODEL } from '@/const/settings';
import { ChunkModel } from '@/database/server/models/chunk';
Expand All @@ -15,6 +14,7 @@ import {
} from '@/database/server/models/ragEval';
import { ModelProvider } from '@/libs/agent-runtime';
import { asyncAuthedProcedure, asyncRouter as router } from '@/libs/trpc/async';
import { initAgentRuntimeWithUserPayload } from '@/server/modules/AgentRuntime';
import { ChunkService } from '@/server/services/chunk';
import { AsyncTaskError } from '@/types/asyncTask';
import { EvalEvaluationStatus } from '@/types/eval';
Expand Down
2 changes: 1 addition & 1 deletion src/server/routers/lambda/chunk.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { inArray } from 'drizzle-orm';
import { z } from 'zod';

import { initAgentRuntimeWithUserPayload } from '@/app/(backend)/api/chat/agentRuntime';
import { DEFAULT_EMBEDDING_MODEL } from '@/const/settings';
import { serverDB } from '@/database/server';
import { AsyncTaskModel } from '@/database/server/models/asyncTask';
Expand All @@ -13,6 +12,7 @@ import { knowledgeBaseFiles } from '@/database/server/schemas/lobechat';
import { ModelProvider } from '@/libs/agent-runtime';
import { authedProcedure, router } from '@/libs/trpc';
import { keyVaults } from '@/libs/trpc/middleware/keyVaults';
import { initAgentRuntimeWithUserPayload } from '@/server/modules/AgentRuntime';
import { ChunkService } from '@/server/services/chunk';
import { SemanticSearchSchema } from '@/types/rag';

Expand Down
2 changes: 1 addition & 1 deletion src/services/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { PluginRequestPayload, createHeadersWithPluginSettings } from '@lobehub/
import { produce } from 'immer';
import { merge } from 'lodash-es';

import { createErrorResponse } from '@/app/(backend)/api/errorResponse';
import { DEFAULT_MODEL_PROVIDER_LIST } from '@/config/modelProviders';
import { INBOX_GUIDE_SYSTEMROLE } from '@/const/guide';
import { INBOX_SESSION_ID } from '@/const/session';
Expand All @@ -24,6 +23,7 @@ import { ChatErrorType } from '@/types/fetch';
import { ChatMessage, MessageToolCall } from '@/types/message';
import type { ChatStreamPayload, OpenAIChatMessage } from '@/types/openai/chat';
import { UserMessageContentPart } from '@/types/openai/chat';
import { createErrorResponse } from '@/utils/errorResponse';
import { FetchSSEOptions, fetchSSE, getMessageError } from '@/utils/fetch';
import { genToolCallingName } from '@/utils/toolCall';
import { createTraceHeader, getTraceId } from '@/utils/trace';
Expand Down
2 changes: 1 addition & 1 deletion src/services/ollama.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ListResponse, Ollama as OllamaBrowser, ProgressResponse } from 'ollama/browser';

import { createErrorResponse } from '@/app/(backend)/api/errorResponse';
import { ModelProvider } from '@/libs/agent-runtime';
import { useUserStore } from '@/store/user';
import { keyVaultsConfigSelectors } from '@/store/user/selectors';
import { ChatErrorType } from '@/types/fetch';
import { createErrorResponse } from '@/utils/errorResponse';
import { getMessageError } from '@/utils/fetch';

const DEFAULT_BASE_URL = 'http://127.0.0.1:11434';
Expand Down
2 changes: 1 addition & 1 deletion src/store/chat/slices/message/actions/rag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const chatRag: StateCreator<ChatStore, [['zustand/devtools', never]], [],
// 1. get the rewrite query
let rewriteQuery = message?.ragQuery || userQuery;

// only rewrite query length is less than 10 characters
// only rewrite query length is less than 10 characters, refs: https:/lobehub/lobe-chat/pull/4288
// if there is no ragQuery and there is a chat history
// we need to rewrite the user message to get better results
if (rewriteQuery.length < 10 && !message?.ragQuery && messages.length > 0) {
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 9a369ac

Please sign in to comment.