Skip to content

Commit ea8b14c

Browse files
authored
fix: reference history format;perf: add error to chat log (#5153)
* perf: add error to chat log * fix: reference history format
1 parent cd1d804 commit ea8b14c

File tree

7 files changed

+190
-32
lines changed

7 files changed

+190
-32
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: 'V4.10.0'
3+
description: 'FastGPT V4.10.0 更新说明'
4+
icon: 'upgrade'
5+
draft: false
6+
toc: true
7+
weight: 784
8+
---
9+
10+
11+
## 🚀 新增内容
12+
13+
14+
## ⚙️ 优化
15+
16+
1. 定时任务报错日志记录到对话日志
17+
18+
## 🐛 修复
19+
20+
1. 搜索类型系统工具无法正常显示
21+
2. 部分系统工具向下兼容问题
22+
3. AI 节点,手动选择历史记录时,会导致 system 记录重复。

packages/service/core/workflow/dispatch/utils.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,14 @@ export const filterToolNodeIdByEdges = ({
8383

8484
export const getHistories = (history?: ChatItemType[] | number, histories: ChatItemType[] = []) => {
8585
if (!history) return [];
86+
// Select reference history
87+
if (Array.isArray(history)) return history;
8688

89+
// history is number
8790
const systemHistoryIndex = histories.findIndex((item) => item.obj !== ChatRoleEnum.System);
8891
const systemHistories = histories.slice(0, systemHistoryIndex);
8992
const chatHistories = histories.slice(systemHistoryIndex);
90-
91-
const filterHistories = (() => {
92-
if (typeof history === 'number') return chatHistories.slice(-(history * 2));
93-
if (Array.isArray(history)) return history;
94-
return [];
95-
})();
93+
const filterHistories = chatHistories.slice(-(history * 2));
9694

9795
return [...systemHistories, ...filterHistories];
9896
};

projects/app/src/pageComponents/app/detail/Logs/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ const Logs = () => {
120120
refreshDeps: [chatSources, logTitle]
121121
}
122122
);
123-
console.log(dateRange, 111);
123+
124124
return (
125125
<Flex
126126
flexDirection={'column'}

projects/app/src/pages/api/core/app/getChatLogs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ async function handler(
170170
),
171171
MongoChat.countDocuments(where, { ...readFromSecondary })
172172
]);
173-
console.log(list);
173+
174174
const listWithSourceMember = await addSourceMember({
175175
list
176176
});

projects/app/src/service/common/system/cron.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ const scheduleTriggerAppCron = () => {
7878
getScheduleTriggerApp();
7979
}
8080
});
81+
getScheduleTriggerApp();
8182
};
8283

8384
export const startCron = () => {

projects/app/src/service/core/app/utils.ts

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runti
2222
import { type UserChatItemValueItemType } from '@fastgpt/global/core/chat/type';
2323
import { saveChat } from '@fastgpt/service/core/chat/saveChat';
2424
import { getAppLatestVersion } from '@fastgpt/service/core/app/version/controller';
25+
import { getErrText } from '@fastgpt/global/common/error/utils';
2526

2627
export const getScheduleTriggerApp = async () => {
28+
addLog.info('Schedule trigger app');
29+
2730
// 1. Find all the app
2831
const apps = await retryFn(() => {
2932
return MongoApp.find({
@@ -35,25 +38,26 @@ export const getScheduleTriggerApp = async () => {
3538
// 2. Run apps
3639
await Promise.allSettled(
3740
apps.map(async (app) => {
38-
try {
39-
if (!app.scheduledTriggerConfig) return;
40-
// random delay 0 ~ 60s
41-
await delay(Math.floor(Math.random() * 60 * 1000));
42-
const { timezone, externalProvider } = await getUserChatInfoAndAuthTeamPoints(app.tmbId);
41+
if (!app.scheduledTriggerConfig) return;
42+
const chatId = getNanoid();
43+
// random delay 0 ~ 60s
44+
await delay(Math.floor(Math.random() * 60 * 1000));
45+
const { timezone, externalProvider } = await retryFn(() =>
46+
getUserChatInfoAndAuthTeamPoints(app.tmbId)
47+
);
4348

44-
// Get app latest version
45-
const { nodes, edges, chatConfig } = await getAppLatestVersion(app._id, app);
46-
47-
const chatId = getNanoid();
48-
const userQuery: UserChatItemValueItemType[] = [
49-
{
50-
type: ChatItemValueTypeEnum.text,
51-
text: {
52-
content: app.scheduledTriggerConfig?.defaultPrompt
53-
}
49+
// Get app latest version
50+
const { nodes, edges, chatConfig } = await retryFn(() => getAppLatestVersion(app._id, app));
51+
const userQuery: UserChatItemValueItemType[] = [
52+
{
53+
type: ChatItemValueTypeEnum.text,
54+
text: {
55+
content: app.scheduledTriggerConfig?.defaultPrompt
5456
}
55-
];
57+
}
58+
];
5659

60+
try {
5761
const { flowUsages, assistantResponses, flowResponses, durationSeconds, system_memories } =
5862
await retryFn(() => {
5963
return dispatchWorkFlow({
@@ -82,6 +86,8 @@ export const getScheduleTriggerApp = async () => {
8286
});
8387
});
8488

89+
const error = flowResponses[flowResponses.length - 1]?.error;
90+
8591
// Save chat
8692
await saveChat({
8793
chatId,
@@ -106,7 +112,8 @@ export const getScheduleTriggerApp = async () => {
106112
memories: system_memories
107113
}
108114
],
109-
durationSeconds
115+
durationSeconds,
116+
errorMsg: getErrText(error)
110117
});
111118
createChatUsage({
112119
appName: app.name,
@@ -116,15 +123,39 @@ export const getScheduleTriggerApp = async () => {
116123
source: UsageSourceEnum.cronJob,
117124
flowUsages
118125
});
119-
120-
// update next time
121-
app.scheduledTriggerNextTime = getNextTimeByCronStringAndTimezone(
122-
app.scheduledTriggerConfig
123-
);
124-
await app.save();
125126
} catch (error) {
126-
addLog.warn('Schedule trigger error', { error });
127+
addLog.error('Schedule trigger error', error);
128+
129+
await saveChat({
130+
chatId,
131+
appId: app._id,
132+
teamId: String(app.teamId),
133+
tmbId: String(app.tmbId),
134+
nodes,
135+
appChatConfig: chatConfig,
136+
variables: {},
137+
isUpdateUseTime: false, // owner update use time
138+
newTitle: 'Cron Job',
139+
source: ChatSourceEnum.cronJob,
140+
content: [
141+
{
142+
obj: ChatRoleEnum.Human,
143+
value: userQuery
144+
},
145+
{
146+
obj: ChatRoleEnum.AI,
147+
value: [],
148+
[DispatchNodeResponseKeyEnum.nodeResponse]: []
149+
}
150+
],
151+
durationSeconds: 0,
152+
errorMsg: getErrText(error)
153+
});
127154
}
155+
156+
// update next time
157+
app.scheduledTriggerNextTime = getNextTimeByCronStringAndTimezone(app.scheduledTriggerConfig);
158+
await app.save().catch();
128159
})
129160
);
130161
};
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { getHistories } from '@fastgpt/service/core/workflow/dispatch/utils';
3+
import { ChatItemValueTypeEnum, ChatRoleEnum } from '@fastgpt/global/core/chat/constants';
4+
import type { ChatItemType } from '@fastgpt/global/core/chat/type';
5+
6+
describe('getHistories test', async () => {
7+
const MockHistories: ChatItemType[] = [
8+
{
9+
obj: ChatRoleEnum.System,
10+
value: [
11+
{
12+
type: ChatItemValueTypeEnum.text,
13+
text: {
14+
content: '你好'
15+
}
16+
}
17+
]
18+
},
19+
{
20+
obj: ChatRoleEnum.Human,
21+
value: [
22+
{
23+
type: ChatItemValueTypeEnum.text,
24+
text: {
25+
content: '你好'
26+
}
27+
}
28+
]
29+
},
30+
{
31+
obj: ChatRoleEnum.AI,
32+
value: [
33+
{
34+
type: ChatItemValueTypeEnum.text,
35+
text: {
36+
content: '你好2'
37+
}
38+
}
39+
]
40+
},
41+
{
42+
obj: ChatRoleEnum.Human,
43+
value: [
44+
{
45+
type: ChatItemValueTypeEnum.text,
46+
text: {
47+
content: '你好3'
48+
}
49+
}
50+
]
51+
},
52+
{
53+
obj: ChatRoleEnum.AI,
54+
value: [
55+
{
56+
type: ChatItemValueTypeEnum.text,
57+
text: {
58+
content: '你好4'
59+
}
60+
}
61+
]
62+
}
63+
];
64+
65+
it('getHistories', async () => {
66+
// Number
67+
expect(getHistories(1, MockHistories)).toEqual([
68+
...MockHistories.slice(0, 1),
69+
...MockHistories.slice(-2)
70+
]);
71+
expect(getHistories(2, MockHistories)).toEqual([...MockHistories.slice(0)]);
72+
expect(getHistories(4, MockHistories)).toEqual([...MockHistories.slice(0)]);
73+
74+
// Array
75+
expect(
76+
getHistories(
77+
[
78+
{
79+
obj: ChatRoleEnum.Human,
80+
value: [
81+
{
82+
type: ChatItemValueTypeEnum.text,
83+
text: {
84+
content: '你好'
85+
}
86+
}
87+
]
88+
}
89+
],
90+
MockHistories
91+
)
92+
).toEqual([
93+
{
94+
obj: ChatRoleEnum.Human,
95+
value: [
96+
{
97+
type: ChatItemValueTypeEnum.text,
98+
text: {
99+
content: '你好'
100+
}
101+
}
102+
]
103+
}
104+
]);
105+
});
106+
});

0 commit comments

Comments
 (0)