mirror of
https://github.com/labring/FastGPT.git
synced 2025-10-14 15:11:13 +00:00
fix: reference history format;perf: add error to chat log (#5153)
* perf: add error to chat log * fix: reference history format
This commit is contained in:
22
docSite/content/zh-cn/docs/development/upgrading/4101.md
Normal file
22
docSite/content/zh-cn/docs/development/upgrading/4101.md
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
---
|
||||||
|
title: 'V4.10.0'
|
||||||
|
description: 'FastGPT V4.10.0 更新说明'
|
||||||
|
icon: 'upgrade'
|
||||||
|
draft: false
|
||||||
|
toc: true
|
||||||
|
weight: 784
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
## 🚀 新增内容
|
||||||
|
|
||||||
|
|
||||||
|
## ⚙️ 优化
|
||||||
|
|
||||||
|
1. 定时任务报错日志记录到对话日志
|
||||||
|
|
||||||
|
## 🐛 修复
|
||||||
|
|
||||||
|
1. 搜索类型系统工具无法正常显示
|
||||||
|
2. 部分系统工具向下兼容问题
|
||||||
|
3. AI 节点,手动选择历史记录时,会导致 system 记录重复。
|
@@ -83,16 +83,14 @@ export const filterToolNodeIdByEdges = ({
|
|||||||
|
|
||||||
export const getHistories = (history?: ChatItemType[] | number, histories: ChatItemType[] = []) => {
|
export const getHistories = (history?: ChatItemType[] | number, histories: ChatItemType[] = []) => {
|
||||||
if (!history) return [];
|
if (!history) return [];
|
||||||
|
// Select reference history
|
||||||
|
if (Array.isArray(history)) return history;
|
||||||
|
|
||||||
|
// history is number
|
||||||
const systemHistoryIndex = histories.findIndex((item) => item.obj !== ChatRoleEnum.System);
|
const systemHistoryIndex = histories.findIndex((item) => item.obj !== ChatRoleEnum.System);
|
||||||
const systemHistories = histories.slice(0, systemHistoryIndex);
|
const systemHistories = histories.slice(0, systemHistoryIndex);
|
||||||
const chatHistories = histories.slice(systemHistoryIndex);
|
const chatHistories = histories.slice(systemHistoryIndex);
|
||||||
|
const filterHistories = chatHistories.slice(-(history * 2));
|
||||||
const filterHistories = (() => {
|
|
||||||
if (typeof history === 'number') return chatHistories.slice(-(history * 2));
|
|
||||||
if (Array.isArray(history)) return history;
|
|
||||||
return [];
|
|
||||||
})();
|
|
||||||
|
|
||||||
return [...systemHistories, ...filterHistories];
|
return [...systemHistories, ...filterHistories];
|
||||||
};
|
};
|
||||||
|
@@ -120,7 +120,7 @@ const Logs = () => {
|
|||||||
refreshDeps: [chatSources, logTitle]
|
refreshDeps: [chatSources, logTitle]
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
console.log(dateRange, 111);
|
|
||||||
return (
|
return (
|
||||||
<Flex
|
<Flex
|
||||||
flexDirection={'column'}
|
flexDirection={'column'}
|
||||||
|
@@ -170,7 +170,7 @@ async function handler(
|
|||||||
),
|
),
|
||||||
MongoChat.countDocuments(where, { ...readFromSecondary })
|
MongoChat.countDocuments(where, { ...readFromSecondary })
|
||||||
]);
|
]);
|
||||||
console.log(list);
|
|
||||||
const listWithSourceMember = await addSourceMember({
|
const listWithSourceMember = await addSourceMember({
|
||||||
list
|
list
|
||||||
});
|
});
|
||||||
|
@@ -78,6 +78,7 @@ const scheduleTriggerAppCron = () => {
|
|||||||
getScheduleTriggerApp();
|
getScheduleTriggerApp();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
getScheduleTriggerApp();
|
||||||
};
|
};
|
||||||
|
|
||||||
export const startCron = () => {
|
export const startCron = () => {
|
||||||
|
@@ -22,8 +22,11 @@ import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runti
|
|||||||
import { type UserChatItemValueItemType } from '@fastgpt/global/core/chat/type';
|
import { type UserChatItemValueItemType } from '@fastgpt/global/core/chat/type';
|
||||||
import { saveChat } from '@fastgpt/service/core/chat/saveChat';
|
import { saveChat } from '@fastgpt/service/core/chat/saveChat';
|
||||||
import { getAppLatestVersion } from '@fastgpt/service/core/app/version/controller';
|
import { getAppLatestVersion } from '@fastgpt/service/core/app/version/controller';
|
||||||
|
import { getErrText } from '@fastgpt/global/common/error/utils';
|
||||||
|
|
||||||
export const getScheduleTriggerApp = async () => {
|
export const getScheduleTriggerApp = async () => {
|
||||||
|
addLog.info('Schedule trigger app');
|
||||||
|
|
||||||
// 1. Find all the app
|
// 1. Find all the app
|
||||||
const apps = await retryFn(() => {
|
const apps = await retryFn(() => {
|
||||||
return MongoApp.find({
|
return MongoApp.find({
|
||||||
@@ -35,25 +38,26 @@ export const getScheduleTriggerApp = async () => {
|
|||||||
// 2. Run apps
|
// 2. Run apps
|
||||||
await Promise.allSettled(
|
await Promise.allSettled(
|
||||||
apps.map(async (app) => {
|
apps.map(async (app) => {
|
||||||
try {
|
if (!app.scheduledTriggerConfig) return;
|
||||||
if (!app.scheduledTriggerConfig) return;
|
const chatId = getNanoid();
|
||||||
// random delay 0 ~ 60s
|
// random delay 0 ~ 60s
|
||||||
await delay(Math.floor(Math.random() * 60 * 1000));
|
await delay(Math.floor(Math.random() * 60 * 1000));
|
||||||
const { timezone, externalProvider } = await getUserChatInfoAndAuthTeamPoints(app.tmbId);
|
const { timezone, externalProvider } = await retryFn(() =>
|
||||||
|
getUserChatInfoAndAuthTeamPoints(app.tmbId)
|
||||||
|
);
|
||||||
|
|
||||||
// Get app latest version
|
// Get app latest version
|
||||||
const { nodes, edges, chatConfig } = await getAppLatestVersion(app._id, app);
|
const { nodes, edges, chatConfig } = await retryFn(() => getAppLatestVersion(app._id, app));
|
||||||
|
const userQuery: UserChatItemValueItemType[] = [
|
||||||
const chatId = getNanoid();
|
{
|
||||||
const userQuery: UserChatItemValueItemType[] = [
|
type: ChatItemValueTypeEnum.text,
|
||||||
{
|
text: {
|
||||||
type: ChatItemValueTypeEnum.text,
|
content: app.scheduledTriggerConfig?.defaultPrompt
|
||||||
text: {
|
|
||||||
content: app.scheduledTriggerConfig?.defaultPrompt
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
];
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
try {
|
||||||
const { flowUsages, assistantResponses, flowResponses, durationSeconds, system_memories } =
|
const { flowUsages, assistantResponses, flowResponses, durationSeconds, system_memories } =
|
||||||
await retryFn(() => {
|
await retryFn(() => {
|
||||||
return dispatchWorkFlow({
|
return dispatchWorkFlow({
|
||||||
@@ -82,6 +86,8 @@ export const getScheduleTriggerApp = async () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const error = flowResponses[flowResponses.length - 1]?.error;
|
||||||
|
|
||||||
// Save chat
|
// Save chat
|
||||||
await saveChat({
|
await saveChat({
|
||||||
chatId,
|
chatId,
|
||||||
@@ -106,7 +112,8 @@ export const getScheduleTriggerApp = async () => {
|
|||||||
memories: system_memories
|
memories: system_memories
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
durationSeconds
|
durationSeconds,
|
||||||
|
errorMsg: getErrText(error)
|
||||||
});
|
});
|
||||||
createChatUsage({
|
createChatUsage({
|
||||||
appName: app.name,
|
appName: app.name,
|
||||||
@@ -116,15 +123,39 @@ export const getScheduleTriggerApp = async () => {
|
|||||||
source: UsageSourceEnum.cronJob,
|
source: UsageSourceEnum.cronJob,
|
||||||
flowUsages
|
flowUsages
|
||||||
});
|
});
|
||||||
|
|
||||||
// update next time
|
|
||||||
app.scheduledTriggerNextTime = getNextTimeByCronStringAndTimezone(
|
|
||||||
app.scheduledTriggerConfig
|
|
||||||
);
|
|
||||||
await app.save();
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
addLog.warn('Schedule trigger error', { error });
|
addLog.error('Schedule trigger error', error);
|
||||||
|
|
||||||
|
await saveChat({
|
||||||
|
chatId,
|
||||||
|
appId: app._id,
|
||||||
|
teamId: String(app.teamId),
|
||||||
|
tmbId: String(app.tmbId),
|
||||||
|
nodes,
|
||||||
|
appChatConfig: chatConfig,
|
||||||
|
variables: {},
|
||||||
|
isUpdateUseTime: false, // owner update use time
|
||||||
|
newTitle: 'Cron Job',
|
||||||
|
source: ChatSourceEnum.cronJob,
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
obj: ChatRoleEnum.Human,
|
||||||
|
value: userQuery
|
||||||
|
},
|
||||||
|
{
|
||||||
|
obj: ChatRoleEnum.AI,
|
||||||
|
value: [],
|
||||||
|
[DispatchNodeResponseKeyEnum.nodeResponse]: []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
durationSeconds: 0,
|
||||||
|
errorMsg: getErrText(error)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// update next time
|
||||||
|
app.scheduledTriggerNextTime = getNextTimeByCronStringAndTimezone(app.scheduledTriggerConfig);
|
||||||
|
await app.save().catch();
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@@ -0,0 +1,106 @@
|
|||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import { getHistories } from '@fastgpt/service/core/workflow/dispatch/utils';
|
||||||
|
import { ChatItemValueTypeEnum, ChatRoleEnum } from '@fastgpt/global/core/chat/constants';
|
||||||
|
import type { ChatItemType } from '@fastgpt/global/core/chat/type';
|
||||||
|
|
||||||
|
describe('getHistories test', async () => {
|
||||||
|
const MockHistories: ChatItemType[] = [
|
||||||
|
{
|
||||||
|
obj: ChatRoleEnum.System,
|
||||||
|
value: [
|
||||||
|
{
|
||||||
|
type: ChatItemValueTypeEnum.text,
|
||||||
|
text: {
|
||||||
|
content: '你好'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
obj: ChatRoleEnum.Human,
|
||||||
|
value: [
|
||||||
|
{
|
||||||
|
type: ChatItemValueTypeEnum.text,
|
||||||
|
text: {
|
||||||
|
content: '你好'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
obj: ChatRoleEnum.AI,
|
||||||
|
value: [
|
||||||
|
{
|
||||||
|
type: ChatItemValueTypeEnum.text,
|
||||||
|
text: {
|
||||||
|
content: '你好2'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
obj: ChatRoleEnum.Human,
|
||||||
|
value: [
|
||||||
|
{
|
||||||
|
type: ChatItemValueTypeEnum.text,
|
||||||
|
text: {
|
||||||
|
content: '你好3'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
obj: ChatRoleEnum.AI,
|
||||||
|
value: [
|
||||||
|
{
|
||||||
|
type: ChatItemValueTypeEnum.text,
|
||||||
|
text: {
|
||||||
|
content: '你好4'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
it('getHistories', async () => {
|
||||||
|
// Number
|
||||||
|
expect(getHistories(1, MockHistories)).toEqual([
|
||||||
|
...MockHistories.slice(0, 1),
|
||||||
|
...MockHistories.slice(-2)
|
||||||
|
]);
|
||||||
|
expect(getHistories(2, MockHistories)).toEqual([...MockHistories.slice(0)]);
|
||||||
|
expect(getHistories(4, MockHistories)).toEqual([...MockHistories.slice(0)]);
|
||||||
|
|
||||||
|
// Array
|
||||||
|
expect(
|
||||||
|
getHistories(
|
||||||
|
[
|
||||||
|
{
|
||||||
|
obj: ChatRoleEnum.Human,
|
||||||
|
value: [
|
||||||
|
{
|
||||||
|
type: ChatItemValueTypeEnum.text,
|
||||||
|
text: {
|
||||||
|
content: '你好'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
MockHistories
|
||||||
|
)
|
||||||
|
).toEqual([
|
||||||
|
{
|
||||||
|
obj: ChatRoleEnum.Human,
|
||||||
|
value: [
|
||||||
|
{
|
||||||
|
type: ChatItemValueTypeEnum.text,
|
||||||
|
text: {
|
||||||
|
content: '你好'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
Reference in New Issue
Block a user