chatbox ui

This commit is contained in:
archer
2023-07-11 23:22:01 +08:00
parent eb768d9c04
commit b2e2f60e0d
46 changed files with 1123 additions and 2817 deletions

View File

@@ -3,6 +3,7 @@ import { getErrText } from '@/utils/tools';
import { parseStreamChunk } from '@/utils/adapt';
import { NextApiResponse } from 'next';
import { sseResponse } from '../utils/tools';
import { SpecificInputEnum } from '@/constants/app';
interface Props {
res: NextApiResponse; // 用于流转发
@@ -13,7 +14,7 @@ export const moduleFetch = ({ url, data, res }: Props) =>
new Promise<Record<string, any>>(async (resolve, reject) => {
try {
const abortSignal = new AbortController();
const baseUrl = `http://localhost:3000/api`;
const baseUrl = `http://localhost:${process.env.PORT || 3000}/api`;
const requestUrl = url.startsWith('/') ? `${baseUrl}${url}` : url;
const response = await fetch(requestUrl, {
method: 'POST',
@@ -41,7 +42,9 @@ export const moduleFetch = ({ url, data, res }: Props) =>
const reader = response.body?.getReader();
let chatResponse: Record<string, any> = {};
let chatResponse: Record<string, any> = {
[SpecificInputEnum.answerText]: ''
};
const read = async () => {
try {
@@ -80,7 +83,8 @@ export const moduleFetch = ({ url, data, res }: Props) =>
if (answer) {
chatResponse = {
...chatResponse,
answer: chatResponse.answer ? chatResponse.answer + answer : answer
[SpecificInputEnum.answerText]:
chatResponse[SpecificInputEnum.answerText] + answer
};
}

View File

@@ -8,21 +8,11 @@ const ChatSchema = new Schema({
ref: 'user',
required: true
},
modelId: {
appId: {
type: Schema.Types.ObjectId,
ref: 'model',
required: true
},
expiredTime: {
// 过期时间
type: Number,
default: () => new Date()
},
loadAmount: {
// 剩余加载次数
type: Number,
default: -1
},
updateTime: {
type: Date,
default: () => new Date()
@@ -35,13 +25,13 @@ const ChatSchema = new Schema({
type: String,
default: ''
},
latestChat: {
type: String,
default: ''
},
top: {
type: Boolean
},
variables: {
type: Object,
default: {}
},
content: {
type: [
{

View File

@@ -7,7 +7,7 @@ const CollectionSchema = new Schema({
ref: 'user',
required: true
},
modelId: {
appId: {
type: Schema.Types.ObjectId,
ref: 'model',
required: true

View File

@@ -1,18 +0,0 @@
import { Schema, model, models, Model } from 'mongoose';
import { ChatSchema as ChatType } from '@/types/mongoSchema';
import { ChatRoleMap } from '@/constants/chat';
const InstallAppSchema = new Schema({
userId: {
type: Schema.Types.ObjectId,
ref: 'user',
required: true
},
modelId: {
type: Schema.Types.ObjectId,
ref: 'model',
required: true
}
});
export const InstallApp: Model<ChatType> = models['installApp'] || model('chat', InstallAppSchema);

View File

@@ -258,65 +258,6 @@ export const authKb = async ({ kbId, userId }: { kbId: string; userId: string })
return Promise.reject(ERROR_ENUM.unAuthKb);
};
// 获取对话校验
export const authChat = async ({
modelId,
chatId,
req
}: {
modelId: string;
chatId?: string;
req: NextApiRequest;
}) => {
const { userId } = await authUser({ req, authToken: true });
// 获取 app 数据
const { app, showModelDetail } = await authApp({
appId: modelId,
userId,
authOwner: false,
reserveDetail: true
});
// 聊天内容
let content: ChatItemType[] = [];
if (chatId) {
// 获取 chat 数据
content = await Chat.aggregate([
{ $match: { _id: new mongoose.Types.ObjectId(chatId) } },
{
$project: {
content: {
$slice: ['$content', -50] // 返回 content 数组的最后50个元素
}
}
},
{ $unwind: '$content' },
{
$project: {
obj: '$content.obj',
value: '$content.value',
quote: '$content.quote'
}
}
]);
}
// 获取 user 的 apiKey
const { userOpenAiKey, systemAuthKey } = await getApiKey({
model: app.chat.chatModel,
userId
});
return {
userOpenAiKey,
systemAuthKey,
content,
userId,
model: app,
showModelDetail
};
};
export const authShareChat = async ({ shareId }: { shareId: string }) => {
// get shareChat
const shareChat = await ShareChat.findOne({ shareId });