mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-22 12:20:34 +00:00

* perf: collection created response * update openapi doc * remove default collection * perf: chat ui * fix: system prompt concat * perf: published check * perf: update app
78 lines
1.6 KiB
TypeScript
78 lines
1.6 KiB
TypeScript
import type { ChatItemType, ChatItemValueItemType } from '@fastgpt/global/core/chat/type';
|
|
import { MongoChatItem } from './chatItemSchema';
|
|
import { addLog } from '../../common/system/log';
|
|
import { ChatItemValueTypeEnum } from '@fastgpt/global/core/chat/constants';
|
|
|
|
export async function getChatItems({
|
|
appId,
|
|
chatId,
|
|
limit = 30,
|
|
field
|
|
}: {
|
|
appId: string;
|
|
chatId?: string;
|
|
limit?: number;
|
|
field: string;
|
|
}): Promise<{ histories: ChatItemType[] }> {
|
|
if (!chatId) {
|
|
return { histories: [] };
|
|
}
|
|
|
|
const histories = await MongoChatItem.find({ appId, chatId }, field)
|
|
.sort({ _id: -1 })
|
|
.limit(limit)
|
|
.lean();
|
|
|
|
histories.reverse();
|
|
|
|
histories.forEach((item) => {
|
|
// @ts-ignore
|
|
item.value = adaptStringValue(item.value);
|
|
});
|
|
|
|
return { histories };
|
|
}
|
|
/* 临时适配旧的对话记录 */
|
|
export const adaptStringValue = (value: any): ChatItemValueItemType[] => {
|
|
if (typeof value === 'string') {
|
|
return [
|
|
{
|
|
type: ChatItemValueTypeEnum.text,
|
|
text: {
|
|
content: value
|
|
}
|
|
}
|
|
];
|
|
}
|
|
return value;
|
|
};
|
|
|
|
export const addCustomFeedbacks = async ({
|
|
appId,
|
|
chatId,
|
|
chatItemId,
|
|
feedbacks
|
|
}: {
|
|
appId: string;
|
|
chatId?: string;
|
|
chatItemId?: string;
|
|
feedbacks: string[];
|
|
}) => {
|
|
if (!chatId || !chatItemId) return;
|
|
|
|
try {
|
|
await MongoChatItem.findOneAndUpdate(
|
|
{
|
|
appId,
|
|
chatId,
|
|
dataId: chatItemId
|
|
},
|
|
{
|
|
$push: { customFeedbacks: { $each: feedbacks } }
|
|
}
|
|
);
|
|
} catch (error) {
|
|
addLog.error('addCustomFeedbacks error', error);
|
|
}
|
|
};
|