diff --git a/packages/service/core/chat/controller.ts b/packages/service/core/chat/controller.ts index 60c54755d..4acfbb683 100644 --- a/packages/service/core/chat/controller.ts +++ b/packages/service/core/chat/controller.ts @@ -5,6 +5,7 @@ import { ChatItemValueTypeEnum, ChatRoleEnum } from '@fastgpt/global/core/chat/c import { delFileByFileIdList, getGFSCollection } from '../../common/file/gridfs/controller'; import { BucketNameEnum } from '@fastgpt/global/common/file/constants'; import { MongoChat } from './chatSchema'; +import { ChatSchema as ChatType } from '@fastgpt/global/core/chat/type.d'; export async function getChatItems({ appId, @@ -35,7 +36,26 @@ export async function getChatItems({ return { histories }; } -/* 临时适配旧的对话记录 */ + +export async function getChat({ + appId, + chatId, + field +}: { + appId: string; + chatId?: string; + field: string; +}): Promise<{ chat: ChatType | null }> { + if (!chatId) { + return { chat: null }; + } + + const chat = await MongoChat.findOne({ appId, chatId }, field).lean(); + + return { chat }; +} + +/* Temporary adaptation for old conversation records */ export const adaptStringValue = (value: any): ChatItemValueItemType[] => { if (typeof value === 'string') { return [ diff --git a/projects/app/src/pages/api/v1/chat/completions.ts b/projects/app/src/pages/api/v1/chat/completions.ts index f698bed55..a23e6b79b 100644 --- a/projects/app/src/pages/api/v1/chat/completions.ts +++ b/projects/app/src/pages/api/v1/chat/completions.ts @@ -20,7 +20,7 @@ import { textAdaptGptResponse } from '@fastgpt/global/core/workflow/runtime/utils'; import { GPTMessages2Chats, chatValue2RuntimePrompt } from '@fastgpt/global/core/chat/adapt'; -import { getChatItems } from '@fastgpt/service/core/chat/controller'; +import { getChat, getChatItems } from '@fastgpt/service/core/chat/controller'; import { saveChat } from '@fastgpt/service/core/chat/saveChat'; import { responseWrite } from '@fastgpt/service/common/response'; import { pushChatUsage } from '@/service/support/wallet/usage/push'; @@ -220,17 +220,31 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { // Get and concat history; const limit = getMaxHistoryLimitFromNodes(app.modules); - const [{ histories }, { nodes, edges, chatConfig }] = await Promise.all([ + const [{ histories }, { nodes, edges, chatConfig }, { chat }] = await Promise.all([ getChatItems({ appId: app._id, chatId, limit, field: `dataId obj value nodeOutputs` }), - getAppLatestVersion(app._id, app) + getAppLatestVersion(app._id, app), + getChat({ + appId: app._id, + chatId, + field: 'source variableList variables' + }) ]); + // get chat histories const newHistories = concatHistories(histories, chatMessages); + // get global variables + if (chat && chat.variables) { + variables = { + ...chat.variables, + ...variables + }; + } + // Get runtimeNodes let runtimeNodes = storeNodes2RuntimeNodes(nodes, getWorkflowEntryNodeIds(nodes, newHistories));