Skip to content

Commit 0ca4ce8

Browse files
committed
fix: reference history format
1 parent 387b4d5 commit 0ca4ce8

File tree

3 files changed

+112
-7
lines changed
  • docSite/content/zh-cn/docs/development/upgrading
  • packages/service/core/workflow/dispatch
  • test/cases/function/packages/service/core/app/workflow/dispatch

3 files changed

+112
-7
lines changed

docSite/content/zh-cn/docs/development/upgrading/4101.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ weight: 784
1818
## 🐛 修复
1919

2020
1. 搜索类型系统工具无法正常显示
21-
2. 部分系统工具向下兼容问题
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
};
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)