perf: bill
This commit is contained in:
@@ -8,6 +8,8 @@ import { type ChatCompletionRequestMessage } from 'openai';
|
||||
import { AppModuleItemType } from '@/types/app';
|
||||
import { dispatchModules } from '../openapi/v1/chat/completions';
|
||||
import { gptMessage2ChatType } from '@/utils/adapt';
|
||||
import { createTaskBill, delTaskBill, finishTaskBill } from '@/service/events/pushBill';
|
||||
import { BillSourceEnum } from '@/constants/user';
|
||||
|
||||
export type MessageItemType = ChatCompletionRequestMessage & { _id?: string };
|
||||
export type Props = {
|
||||
@@ -15,10 +17,8 @@ export type Props = {
|
||||
prompt: string;
|
||||
modules: AppModuleItemType[];
|
||||
variables: Record<string, any>;
|
||||
};
|
||||
export type ChatResponseType = {
|
||||
newChatId: string;
|
||||
quoteLen?: number;
|
||||
appId: string;
|
||||
appName: string;
|
||||
};
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
@@ -30,8 +30,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
res.end();
|
||||
});
|
||||
|
||||
let { modules = [], history = [], prompt, variables = {} } = req.body as Props;
|
||||
|
||||
let { modules = [], history = [], prompt, variables = {}, appName, appId } = req.body as Props;
|
||||
let billId = '';
|
||||
try {
|
||||
if (!history || !modules || !prompt) {
|
||||
throw new Error('Prams Error');
|
||||
@@ -45,6 +45,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
/* user auth */
|
||||
const { userId } = await authUser({ req });
|
||||
|
||||
billId = await createTaskBill({
|
||||
userId,
|
||||
appName,
|
||||
appId,
|
||||
source: BillSourceEnum.fastgpt
|
||||
});
|
||||
|
||||
/* start process */
|
||||
const { responseData } = await dispatchModules({
|
||||
res,
|
||||
@@ -54,7 +61,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
history: gptMessage2ChatType(history),
|
||||
userChatInput: prompt
|
||||
},
|
||||
stream: true
|
||||
stream: true,
|
||||
billId
|
||||
});
|
||||
|
||||
sseResponse({
|
||||
@@ -70,7 +78,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
res.end();
|
||||
|
||||
// bill
|
||||
finishTaskBill({
|
||||
billId
|
||||
});
|
||||
} catch (err: any) {
|
||||
delTaskBill(billId);
|
||||
res.status(500);
|
||||
sseErrRes(res, err);
|
||||
res.end();
|
||||
|
||||
@@ -53,14 +53,6 @@ export async function saveChat({
|
||||
await connectToDatabase();
|
||||
const { app } = await authApp({ appId, userId, authOwner: false });
|
||||
|
||||
const content = prompts.map((item) => ({
|
||||
_id: item._id,
|
||||
obj: item.obj,
|
||||
value: item.value,
|
||||
systemPrompt: item.systemPrompt || '',
|
||||
quote: item.quote || []
|
||||
}));
|
||||
|
||||
if (String(app.userId) === userId) {
|
||||
await App.findByIdAndUpdate(appId, {
|
||||
updateTime: new Date()
|
||||
@@ -73,12 +65,11 @@ export async function saveChat({
|
||||
Chat.findByIdAndUpdate(historyId, {
|
||||
$push: {
|
||||
content: {
|
||||
$each: content
|
||||
$each: prompts
|
||||
}
|
||||
},
|
||||
variables,
|
||||
title: content[0].value.slice(0, 20),
|
||||
latestChat: content[1].value,
|
||||
title: prompts[0].value.slice(0, 20),
|
||||
updateTime: new Date()
|
||||
}).then(() => ({
|
||||
newHistoryId: ''
|
||||
@@ -90,9 +81,8 @@ export async function saveChat({
|
||||
userId,
|
||||
appId,
|
||||
variables,
|
||||
content,
|
||||
title: content[0].value.slice(0, 20),
|
||||
latestChat: content[1].value
|
||||
content: prompts,
|
||||
title: prompts[0].value.slice(0, 20)
|
||||
}).then((res) => ({
|
||||
newHistoryId: String(res._id)
|
||||
}))
|
||||
|
||||
Reference in New Issue
Block a user