From 0d8876137889482e79426702e35311ca871a3817 Mon Sep 17 00:00:00 2001 From: Archer <545436317@qq.com> Date: Thu, 18 Dec 2025 13:49:45 +0800 Subject: [PATCH] perf: redis del (#6115) * fix: log * fix: redirect api --- .../introduction/development/openapi/chat.mdx | 10 +++++----- document/data/doc-last-modified.json | 2 +- packages/global/openapi/core/app/log/api.ts | 7 +++++-- packages/service/common/response/index.ts | 19 +++++++++++++++---- packages/service/common/system/log.ts | 3 ++- packages/service/core/chat/saveChat.ts | 1 + .../src/pages/api/core/chat/clearHistories.ts | 4 ++++ .../app/src/pages/api/core/chat/delHistory.ts | 4 ++++ .../src/pages/api/core/chat/getHistories.ts | 4 ++++ .../api/core/chat/history/batchDelete.ts | 1 - .../api/core/chat/history/clearHistories.ts | 2 +- .../pages/api/core/chat/history/delHistory.ts | 2 +- .../api/core/chat/history/getHistories.ts | 2 +- .../api/core/chat/history/updateHistory.ts | 2 +- .../src/pages/api/core/chat/updateHistory.ts | 4 ++++ 15 files changed, 49 insertions(+), 18 deletions(-) create mode 100644 projects/app/src/pages/api/core/chat/clearHistories.ts create mode 100644 projects/app/src/pages/api/core/chat/delHistory.ts create mode 100644 projects/app/src/pages/api/core/chat/getHistories.ts create mode 100644 projects/app/src/pages/api/core/chat/updateHistory.ts diff --git a/document/content/docs/introduction/development/openapi/chat.mdx b/document/content/docs/introduction/development/openapi/chat.mdx index e349f0725..c7a5b838d 100644 --- a/document/content/docs/introduction/development/openapi/chat.mdx +++ b/document/content/docs/introduction/development/openapi/chat.mdx @@ -616,7 +616,7 @@ event取值: ```bash -curl --location --request POST 'http://localhost:3000/api/core/chat/getHistories' \ +curl --location --request POST 'http://localhost:3000/api/core/chat/history/getHistories' \ --header 'Authorization: Bearer [apikey]' \ --header 'Content-Type: application/json' \ --data-raw '{ @@ -679,7 +679,7 @@ curl --location --request POST 'http://localhost:3000/api/core/chat/getHistories ```bash -curl --location --request POST 'http://localhost:3000/api/core/chat/updateHistory' \ +curl --location --request POST 'http://localhost:3000/api/core/chat/history/updateHistory' \ --header 'Authorization: Bearer [apikey]' \ --header 'Content-Type: application/json' \ --data-raw '{ @@ -721,7 +721,7 @@ curl --location --request POST 'http://localhost:3000/api/core/chat/updateHistor ```bash -curl --location --request POST 'http://localhost:3000/api/core/chat/updateHistory' \ +curl --location --request POST 'http://localhost:3000/api/core/chat/history/updateHistory' \ --header 'Authorization: Bearer [apikey]' \ --header 'Content-Type: application/json' \ --data-raw '{ @@ -763,7 +763,7 @@ curl --location --request POST 'http://localhost:3000/api/core/chat/updateHistor ```bash -curl --location --request DELETE 'http://localhost:3000/api/core/chat/delHistory?chatId=[chatId]&appId=[appId]' \ +curl --location --request DELETE 'http://localhost:3000/api/core/chat/history/delHistory?chatId=[chatId]&appId=[appId]' \ --header 'Authorization: Bearer [apikey]' ``` @@ -800,7 +800,7 @@ curl --location --request DELETE 'http://localhost:3000/api/core/chat/delHistory ```bash -curl --location --request DELETE 'http://localhost:3000/api/core/chat/clearHistories?appId=[appId]' \ +curl --location --request DELETE 'http://localhost:3000/api/core/chat/history/clearHistories?appId=[appId]' \ --header 'Authorization: Bearer [apikey]' ``` diff --git a/document/data/doc-last-modified.json b/document/data/doc-last-modified.json index 4225a2bf8..c7ab6a0f5 100644 --- a/document/data/doc-last-modified.json +++ b/document/data/doc-last-modified.json @@ -120,7 +120,7 @@ "document/content/docs/upgrading/4-14/4142.mdx": "2025-11-18T19:27:14+08:00", "document/content/docs/upgrading/4-14/4143.mdx": "2025-11-26T20:52:05+08:00", "document/content/docs/upgrading/4-14/4144.mdx": "2025-12-16T14:56:04+08:00", - "document/content/docs/upgrading/4-14/4145.mdx": "2025-12-18T10:17:10+08:00", + "document/content/docs/upgrading/4-14/4145.mdx": "2025-12-18T11:09:13+08:00", "document/content/docs/upgrading/4-8/40.mdx": "2025-08-02T19:38:37+08:00", "document/content/docs/upgrading/4-8/41.mdx": "2025-08-02T19:38:37+08:00", "document/content/docs/upgrading/4-8/42.mdx": "2025-08-02T19:38:37+08:00", diff --git a/packages/global/openapi/core/app/log/api.ts b/packages/global/openapi/core/app/log/api.ts index 5360a98c5..2e9a338a8 100644 --- a/packages/global/openapi/core/app/log/api.ts +++ b/packages/global/openapi/core/app/log/api.ts @@ -31,12 +31,15 @@ export type updateLogKeysBody = z.infer; export const ChatLogItemSchema = z.object({ _id: z.string().meta({ example: '68ad85a7463006c963799a05', description: '对话日志 ID' }), chatId: z.string().meta({ example: 'chat123', description: '对话 ID' }), - title: z.string().optional().meta({ example: '用户对话', description: '对话标题' }), + title: z.string().nullish().meta({ example: '用户对话', description: '对话标题' }), customTitle: z.string().nullish().meta({ example: '自定义标题', description: '自定义对话标题' }), source: z.enum(ChatSourceEnum).meta({ example: ChatSourceEnum.api, description: '对话来源' }), sourceName: z.string().nullish().meta({ example: 'API调用', description: '来源名称' }), updateTime: z.date().meta({ example: '2024-01-01T00:30:00.000Z', description: '更新时间' }), - createTime: z.date().meta({ example: '2024-01-01T00:00:00.000Z', description: '创建时间' }), + createTime: z + .date() + .nullish() + .meta({ example: '2024-01-01T00:00:00.000Z', description: '创建时间' }), messageCount: z.int().nullish().meta({ example: 10, description: '消息数量' }), userGoodFeedbackCount: z.int().nullish().meta({ example: 3, description: '好评反馈数量' }), userBadFeedbackCount: z.int().nullish().meta({ example: 1, description: '差评反馈数量' }), diff --git a/packages/service/common/response/index.ts b/packages/service/common/response/index.ts index 12b01800d..42865ea9a 100644 --- a/packages/service/common/response/index.ts +++ b/packages/service/common/response/index.ts @@ -17,8 +17,9 @@ export interface ProcessedError { code: number; statusText: string; message: string; - data?: any; shouldClearCookie: boolean; + data?: any; + zodError?: any; } /** @@ -32,6 +33,7 @@ export function processError(params: { defaultCode?: number; }): ProcessedError { const { error, url, defaultCode = 500 } = params; + let zodError; const errResponseKey = typeof error === 'string' ? error : error?.message; @@ -67,7 +69,14 @@ export function processError(params: { if (error instanceof UserError) { addLog.info(`Request error: ${url}, ${msg}`); } else if (error instanceof ZodError) { - addLog.error(`[Zod] Error in ${url}`, error.message); + zodError = (() => { + try { + return JSON.parse(error.message); + } catch (error) {} + })(); + addLog.error(`[Zod] Error in ${url}`, { + data: zodError + }); msg = error.message; } else { addLog.error(`System unexpected error: ${url}, ${msg}`, error); @@ -78,7 +87,8 @@ export function processError(params: { code: defaultCode, statusText: 'error', message: replaceSensitiveText(msg), - shouldClearCookie: false + shouldClearCookie: false, + zodError }; } @@ -107,7 +117,8 @@ export const jsonRes = ( code: processedError.code, statusText: processedError.statusText, message: message || processedError.message, - data: processedError.data !== undefined ? processedError.data : null + data: processedError.data !== undefined ? processedError.data : null, + zodError: processedError.zodError }); return; diff --git a/packages/service/common/system/log.ts b/packages/service/common/system/log.ts index ef3db91ef..676faee14 100644 --- a/packages/service/common/system/log.ts +++ b/packages/service/common/system/log.ts @@ -4,6 +4,7 @@ import { LogLevelEnum } from './log/constant'; import { connectionMongo } from '../mongo/index'; import { getMongoLog } from './log/schema'; import { getLogger } from '../otel/log'; +import { getErrText } from '@fastgpt/global/common/error/utils'; export enum EventTypeEnum { outLinkBot = '[Outlink bot]', @@ -151,7 +152,7 @@ export const addLog = { error(msg: string, error?: any) { this.log(LogLevelEnum.error, msg, { ...(error?.data && { data: error?.data }), - message: error?.message || error, + message: getErrText(error), stack: error?.stack, ...(error?.config && { config: { diff --git a/packages/service/core/chat/saveChat.ts b/packages/service/core/chat/saveChat.ts index 38fff3ac4..9c1321924 100644 --- a/packages/service/core/chat/saveChat.ts +++ b/packages/service/core/chat/saveChat.ts @@ -313,6 +313,7 @@ export async function saveChat(props: Props) { shareId, outLinkUid, metadata: metadataUpdate, + createTime: new Date(), updateTime: new Date() } }, diff --git a/projects/app/src/pages/api/core/chat/clearHistories.ts b/projects/app/src/pages/api/core/chat/clearHistories.ts new file mode 100644 index 000000000..dc94b600d --- /dev/null +++ b/projects/app/src/pages/api/core/chat/clearHistories.ts @@ -0,0 +1,4 @@ +import { NextAPI } from '@/service/middleware/entry'; +import { handler } from './history/clearHistories'; + +export default NextAPI(handler); diff --git a/projects/app/src/pages/api/core/chat/delHistory.ts b/projects/app/src/pages/api/core/chat/delHistory.ts new file mode 100644 index 000000000..40888ad9a --- /dev/null +++ b/projects/app/src/pages/api/core/chat/delHistory.ts @@ -0,0 +1,4 @@ +import { NextAPI } from '@/service/middleware/entry'; +import { handler } from './history/delHistory'; + +export default NextAPI(handler); diff --git a/projects/app/src/pages/api/core/chat/getHistories.ts b/projects/app/src/pages/api/core/chat/getHistories.ts new file mode 100644 index 000000000..493dfa63c --- /dev/null +++ b/projects/app/src/pages/api/core/chat/getHistories.ts @@ -0,0 +1,4 @@ +import { NextAPI } from '@/service/middleware/entry'; +import { handler } from './history/getHistories'; + +export default NextAPI(handler); diff --git a/projects/app/src/pages/api/core/chat/history/batchDelete.ts b/projects/app/src/pages/api/core/chat/history/batchDelete.ts index 0f02912b0..8ef1a86d0 100644 --- a/projects/app/src/pages/api/core/chat/history/batchDelete.ts +++ b/projects/app/src/pages/api/core/chat/history/batchDelete.ts @@ -9,7 +9,6 @@ import { getS3ChatSource } from '@fastgpt/service/common/s3/sources/chat'; import { authApp } from '@fastgpt/service/support/permission/app/auth'; import { AppReadChatLogPerVal } from '@fastgpt/global/support/permission/app/constant'; import { ChatBatchDeleteBodySchema } from '@fastgpt/global/openapi/core/chat/history/api'; -import { UserError } from '@fastgpt/global/common/error/utils'; async function handler(req: ApiRequestProps, res: NextApiResponse) { const { appId, chatIds } = ChatBatchDeleteBodySchema.parse(req.body); diff --git a/projects/app/src/pages/api/core/chat/history/clearHistories.ts b/projects/app/src/pages/api/core/chat/history/clearHistories.ts index 3aaca63ab..1da531de7 100644 --- a/projects/app/src/pages/api/core/chat/history/clearHistories.ts +++ b/projects/app/src/pages/api/core/chat/history/clearHistories.ts @@ -7,7 +7,7 @@ import { type ApiRequestProps } from '@fastgpt/service/type/next'; import { authChatCrud } from '@/service/support/permission/auth/chat'; /* clear all chat histories of an app */ -async function handler(req: ApiRequestProps, res: NextApiResponse) { +export async function handler(req: ApiRequestProps, res: NextApiResponse) { const { appId, shareId, outLinkUid, teamId, teamToken } = ClearChatHistoriesSchema.parse( req.query ); diff --git a/projects/app/src/pages/api/core/chat/history/delHistory.ts b/projects/app/src/pages/api/core/chat/history/delHistory.ts index 83b1009de..9bad0c6e8 100644 --- a/projects/app/src/pages/api/core/chat/history/delHistory.ts +++ b/projects/app/src/pages/api/core/chat/history/delHistory.ts @@ -6,7 +6,7 @@ import { NextAPI } from '@/service/middleware/entry'; import { type ApiRequestProps } from '@fastgpt/service/type/next'; /* delete single chat history (soft delete) */ -async function handler(req: ApiRequestProps, res: NextApiResponse) { +export async function handler(req: ApiRequestProps, res: NextApiResponse) { const { appId, chatId } = DelChatHistorySchema.parse(req.query); await authChatCrud({ diff --git a/projects/app/src/pages/api/core/chat/history/getHistories.ts b/projects/app/src/pages/api/core/chat/history/getHistories.ts index 48dac80bb..435afc3ac 100644 --- a/projects/app/src/pages/api/core/chat/history/getHistories.ts +++ b/projects/app/src/pages/api/core/chat/history/getHistories.ts @@ -14,7 +14,7 @@ import { parsePaginationRequest } from '@fastgpt/service/common/api/pagination'; import { addMonths } from 'date-fns'; /* get chat histories list */ -async function handler( +export async function handler( req: ApiRequestProps, _res: ApiResponseType ): Promise { diff --git a/projects/app/src/pages/api/core/chat/history/updateHistory.ts b/projects/app/src/pages/api/core/chat/history/updateHistory.ts index 0c472b227..ddc8c3f3d 100644 --- a/projects/app/src/pages/api/core/chat/history/updateHistory.ts +++ b/projects/app/src/pages/api/core/chat/history/updateHistory.ts @@ -7,7 +7,7 @@ import { type ApiRequestProps } from '@fastgpt/service/type/next'; import { WritePermissionVal } from '@fastgpt/global/support/permission/constant'; /* update chat history: title, customTitle, top */ -async function handler(req: ApiRequestProps, res: NextApiResponse) { +export async function handler(req: ApiRequestProps, res: NextApiResponse) { const { appId, chatId, title, customTitle, top } = UpdateHistoryBodySchema.parse(req.body); await authChatCrud({ req, diff --git a/projects/app/src/pages/api/core/chat/updateHistory.ts b/projects/app/src/pages/api/core/chat/updateHistory.ts new file mode 100644 index 000000000..23f88328b --- /dev/null +++ b/projects/app/src/pages/api/core/chat/updateHistory.ts @@ -0,0 +1,4 @@ +import { NextAPI } from '@/service/middleware/entry'; +import { handler } from './history/updateHistory'; + +export default NextAPI(handler);