@lobehub/chat 1.0.11 → 1.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +50 -0
- package/locales/ar/setting.json +5 -0
- package/locales/bg-BG/setting.json +5 -0
- package/locales/de-DE/setting.json +5 -0
- package/locales/en-US/setting.json +5 -0
- package/locales/es-ES/setting.json +5 -0
- package/locales/fr-FR/setting.json +5 -0
- package/locales/it-IT/setting.json +5 -0
- package/locales/ja-JP/setting.json +5 -0
- package/locales/ko-KR/setting.json +5 -0
- package/locales/nl-NL/setting.json +5 -0
- package/locales/pl-PL/setting.json +5 -0
- package/locales/pt-BR/setting.json +5 -0
- package/locales/ru-RU/setting.json +5 -0
- package/locales/tr-TR/setting.json +5 -0
- package/locales/vi-VN/setting.json +5 -0
- package/locales/zh-CN/setting.json +5 -0
- package/locales/zh-TW/setting.json +5 -0
- package/package.json +4 -3
- package/src/app/(main)/chat/(workspace)/@conversation/default.tsx +1 -1
- package/src/app/(main)/chat/settings/features/EditPage.tsx +2 -2
- package/src/app/(main)/settings/agent/index.tsx +1 -1
- package/src/app/(main)/settings/system-agent/features/AgentMeta.tsx +58 -0
- package/src/app/(main)/settings/system-agent/features/Topic.tsx +1 -0
- package/src/app/(main)/settings/system-agent/features/Translation.tsx +1 -0
- package/src/app/(main)/settings/system-agent/index.tsx +2 -0
- package/src/app/(main)/settings/system-agent/page.tsx +1 -1
- package/src/const/settings/systemAgent.ts +1 -0
- package/src/features/AgentSetting/AgentMeta/AutoGenerateAvatar.tsx +59 -0
- package/src/features/AgentSetting/AgentMeta/index.tsx +6 -9
- package/src/features/AgentSetting/AgentSettings.tsx +24 -0
- package/src/features/AgentSetting/AgentSettingsStore.tsx +14 -0
- package/src/features/AgentSetting/StoreUpdater.tsx +10 -5
- package/src/features/AgentSetting/hooks/useAgentSettings.ts +31 -0
- package/src/features/AgentSetting/index.tsx +3 -26
- package/src/features/AgentSetting/store/action.ts +36 -18
- package/src/features/ChatInput/useSend.ts +8 -0
- package/src/features/Conversation/Error/ErrorJsonViewer.tsx +1 -1
- package/src/features/Conversation/Messages/Assistant/ToolCalls/index.tsx +3 -2
- package/src/features/Conversation/Messages/Tool/index.tsx +3 -5
- package/src/features/Conversation/Messages/components/Arguments.tsx +20 -0
- package/src/features/Conversation/Messages/hooks/useYamlArguments.ts +11 -0
- package/src/features/ModelSelect/index.tsx +6 -4
- package/src/libs/agent-runtime/anthropic/index.test.ts +1 -31
- package/src/libs/agent-runtime/anthropic/index.ts +9 -63
- package/src/libs/agent-runtime/openai/index.test.ts +7 -7
- package/src/libs/agent-runtime/openai/index.ts +0 -6
- package/src/libs/agent-runtime/utils/anthropicHelpers.ts +2 -2
- package/src/libs/agent-runtime/utils/streams/anthropic.test.ts +102 -28
- package/src/libs/agent-runtime/utils/streams/anthropic.ts +29 -12
- package/src/libs/agent-runtime/utils/streams/protocol.ts +6 -1
- package/src/locales/default/setting.ts +5 -0
- package/src/services/message/server.ts +4 -0
- package/src/services/message/type.ts +1 -1
- package/src/store/agent/slices/chat/initialState.ts +2 -0
- package/src/store/chat/slices/message/action.ts +48 -48
- package/src/store/chat/slices/message/initialState.ts +2 -0
- package/src/store/chat/slices/message/selectors.ts +3 -0
- package/src/store/chat/slices/plugin/action.test.ts +15 -15
- package/src/store/chat/slices/plugin/action.ts +128 -115
- package/src/store/chat/utils/index.ts +19 -0
- package/src/store/user/slices/settings/action.ts +8 -2
- package/src/store/user/slices/settings/selectors/systemAgent.ts +2 -0
- package/src/types/message/index.ts +1 -1
- package/src/types/user/settings/systemAgent.ts +3 -0
- /package/src/{components/StoreHydration → app/(main)/chat/(workspace)/@conversation/features}/ChatHydration/index.tsx +0 -0
|
@@ -99,27 +99,67 @@ describe('AnthropicStream', () => {
|
|
|
99
99
|
});
|
|
100
100
|
|
|
101
101
|
it('should handle tool use event and ReadableStream input', async () => {
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
102
|
+
const streams = [
|
|
103
|
+
{
|
|
104
|
+
type: 'message_start',
|
|
105
|
+
message: {
|
|
106
|
+
id: 'msg_017aTuY86wNxth5TE544yqJq',
|
|
107
|
+
type: 'message',
|
|
108
|
+
role: 'assistant',
|
|
109
|
+
model: 'claude-3-sonnet-20240229',
|
|
110
|
+
content: [],
|
|
111
|
+
stop_reason: null,
|
|
112
|
+
stop_sequence: null,
|
|
113
|
+
usage: { input_tokens: 457, output_tokens: 1 },
|
|
110
114
|
},
|
|
111
115
|
},
|
|
112
|
-
|
|
116
|
+
{ type: 'content_block_start', index: 0, content_block: { type: 'text', text: '' } },
|
|
117
|
+
{ type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: '好' } },
|
|
118
|
+
{ type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: '的:' } },
|
|
119
|
+
|
|
120
|
+
{ type: 'content_block_stop', index: 0 },
|
|
121
|
+
{
|
|
122
|
+
type: 'content_block_start',
|
|
123
|
+
index: 1,
|
|
124
|
+
content_block: {
|
|
125
|
+
type: 'tool_use',
|
|
126
|
+
id: 'toolu_01WdYWxYFQ8iu5iZq1Dy9Saf',
|
|
127
|
+
name: 'realtime-weather____fetchCurrentWeather',
|
|
128
|
+
input: {},
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
type: 'content_block_delta',
|
|
133
|
+
index: 1,
|
|
134
|
+
delta: { type: 'input_json_delta', partial_json: '' },
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
type: 'content_block_delta',
|
|
138
|
+
index: 1,
|
|
139
|
+
delta: { type: 'input_json_delta', partial_json: '{"city": "' },
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
type: 'content_block_delta',
|
|
143
|
+
index: 1,
|
|
144
|
+
delta: { type: 'input_json_delta', partial_json: '杭' },
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
type: 'content_block_delta',
|
|
148
|
+
index: 1,
|
|
149
|
+
delta: { type: 'input_json_delta', partial_json: '州"}' },
|
|
150
|
+
},
|
|
151
|
+
{ type: 'content_block_stop', index: 1 },
|
|
152
|
+
{
|
|
153
|
+
type: 'message_delta',
|
|
154
|
+
delta: { stop_reason: 'tool_use', stop_sequence: null },
|
|
155
|
+
usage: { output_tokens: 83 },
|
|
156
|
+
},
|
|
157
|
+
];
|
|
113
158
|
|
|
114
159
|
const mockReadableStream = new ReadableStream({
|
|
115
160
|
start(controller) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
message: { id: 'message_1', metadata: {} },
|
|
119
|
-
});
|
|
120
|
-
controller.enqueue(toolUseEvent);
|
|
121
|
-
controller.enqueue({
|
|
122
|
-
type: 'message_stop',
|
|
161
|
+
streams.forEach((chunk) => {
|
|
162
|
+
controller.enqueue(chunk);
|
|
123
163
|
});
|
|
124
164
|
controller.close();
|
|
125
165
|
},
|
|
@@ -139,19 +179,53 @@ describe('AnthropicStream', () => {
|
|
|
139
179
|
chunks.push(decoder.decode(chunk, { stream: true }));
|
|
140
180
|
}
|
|
141
181
|
|
|
142
|
-
expect(chunks).toEqual(
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
182
|
+
expect(chunks).toEqual(
|
|
183
|
+
[
|
|
184
|
+
'id: msg_017aTuY86wNxth5TE544yqJq',
|
|
185
|
+
'event: data',
|
|
186
|
+
'data: {"id":"msg_017aTuY86wNxth5TE544yqJq","type":"message","role":"assistant","model":"claude-3-sonnet-20240229","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":457,"output_tokens":1}}\n',
|
|
187
|
+
'id: msg_017aTuY86wNxth5TE544yqJq',
|
|
188
|
+
'event: data',
|
|
189
|
+
'data: ""\n',
|
|
190
|
+
'id: msg_017aTuY86wNxth5TE544yqJq',
|
|
191
|
+
'event: text',
|
|
192
|
+
'data: "好"\n',
|
|
193
|
+
'id: msg_017aTuY86wNxth5TE544yqJq',
|
|
194
|
+
'event: text',
|
|
195
|
+
'data: "的:"\n',
|
|
196
|
+
'id: msg_017aTuY86wNxth5TE544yqJq',
|
|
197
|
+
'event: data',
|
|
198
|
+
'data: {"type":"content_block_stop","index":0}\n',
|
|
199
|
+
// Tool calls
|
|
200
|
+
'id: msg_017aTuY86wNxth5TE544yqJq',
|
|
201
|
+
'event: tool_calls',
|
|
202
|
+
`data: [{"function":{"arguments":"","name":"realtime-weather____fetchCurrentWeather"},"id":"toolu_01WdYWxYFQ8iu5iZq1Dy9Saf","index":0,"type":"function"}]\n`,
|
|
203
|
+
'id: msg_017aTuY86wNxth5TE544yqJq',
|
|
204
|
+
'event: tool_calls',
|
|
205
|
+
`data: [{"function":{"arguments":""},"index":0,"type":"function"}]\n`,
|
|
206
|
+
'id: msg_017aTuY86wNxth5TE544yqJq',
|
|
207
|
+
'event: tool_calls',
|
|
208
|
+
`data: [{"function":{"arguments":"{\\"city\\": \\""},"index":0,"type":"function"}]\n`,
|
|
209
|
+
'id: msg_017aTuY86wNxth5TE544yqJq',
|
|
210
|
+
'event: tool_calls',
|
|
211
|
+
|
|
212
|
+
`data: [{"function":{"arguments":"杭"},"index":0,"type":"function"}]\n`,
|
|
213
|
+
'id: msg_017aTuY86wNxth5TE544yqJq',
|
|
214
|
+
'event: tool_calls',
|
|
215
|
+
|
|
216
|
+
`data: [{"function":{"arguments":"州\\"}"},"index":0,"type":"function"}]\n`,
|
|
217
|
+
|
|
218
|
+
'id: msg_017aTuY86wNxth5TE544yqJq',
|
|
219
|
+
'event: data',
|
|
220
|
+
'data: {"type":"content_block_stop","index":1}\n',
|
|
221
|
+
|
|
222
|
+
'id: msg_017aTuY86wNxth5TE544yqJq',
|
|
223
|
+
'event: stop',
|
|
224
|
+
'data: "tool_use"\n',
|
|
225
|
+
].map((item) => `${item}\n`),
|
|
226
|
+
);
|
|
153
227
|
|
|
154
|
-
expect(onToolCallMock).toHaveBeenCalledTimes(
|
|
228
|
+
expect(onToolCallMock).toHaveBeenCalledTimes(5);
|
|
155
229
|
});
|
|
156
230
|
|
|
157
231
|
it('should handle ReadableStream input', async () => {
|
|
@@ -22,27 +22,39 @@ export const transformAnthropicStream = (
|
|
|
22
22
|
stack.id = chunk.message.id;
|
|
23
23
|
return { data: chunk.message, id: chunk.message.id, type: 'data' };
|
|
24
24
|
}
|
|
25
|
+
case 'content_block_start': {
|
|
26
|
+
if (chunk.content_block.type === 'tool_use') {
|
|
27
|
+
const toolChunk = chunk.content_block;
|
|
25
28
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
+
const toolCall: StreamToolCallChunkData = {
|
|
30
|
+
function: {
|
|
31
|
+
arguments: '',
|
|
32
|
+
name: toolChunk.name,
|
|
33
|
+
},
|
|
34
|
+
id: toolChunk.id,
|
|
35
|
+
index: 0,
|
|
36
|
+
type: 'function',
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
stack.tool = { id: toolChunk.id, index: 0, name: toolChunk.name };
|
|
40
|
+
|
|
41
|
+
return { data: [toolCall], id: stack.id, type: 'tool_calls' };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return { data: chunk.content_block.text, id: stack.id, type: 'data' };
|
|
45
|
+
}
|
|
29
46
|
|
|
30
47
|
case 'content_block_delta': {
|
|
31
|
-
switch (chunk.delta.type
|
|
32
|
-
default:
|
|
48
|
+
switch (chunk.delta.type) {
|
|
33
49
|
case 'text_delta': {
|
|
34
50
|
return { data: chunk.delta.text, id: stack.id, type: 'text' };
|
|
35
51
|
}
|
|
36
52
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
// and maybe we need to update it when the feature is available
|
|
40
|
-
case 'tool_use': {
|
|
41
|
-
const delta = (chunk.delta as any).tool_use as Anthropic.Beta.Tools.ToolUseBlock;
|
|
53
|
+
case 'input_json_delta': {
|
|
54
|
+
const delta = chunk.delta.partial_json;
|
|
42
55
|
|
|
43
56
|
const toolCall: StreamToolCallChunkData = {
|
|
44
|
-
function: { arguments:
|
|
45
|
-
id: delta.id,
|
|
57
|
+
function: { arguments: delta },
|
|
46
58
|
index: 0,
|
|
47
59
|
type: 'function',
|
|
48
60
|
};
|
|
@@ -53,7 +65,12 @@ export const transformAnthropicStream = (
|
|
|
53
65
|
type: 'tool_calls',
|
|
54
66
|
} as StreamProtocolToolCallChunk;
|
|
55
67
|
}
|
|
68
|
+
|
|
69
|
+
default: {
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
56
72
|
}
|
|
73
|
+
return { data: chunk, id: stack.id, type: 'data' };
|
|
57
74
|
}
|
|
58
75
|
|
|
59
76
|
case 'message_delta': {
|
|
@@ -2,6 +2,11 @@ import { ChatStreamCallbacks } from '@/libs/agent-runtime';
|
|
|
2
2
|
|
|
3
3
|
export interface StreamStack {
|
|
4
4
|
id: string;
|
|
5
|
+
tool?: {
|
|
6
|
+
id: string;
|
|
7
|
+
index: number;
|
|
8
|
+
name: string;
|
|
9
|
+
};
|
|
5
10
|
}
|
|
6
11
|
|
|
7
12
|
export interface StreamProtocolChunk {
|
|
@@ -15,7 +20,7 @@ export interface StreamToolCallChunkData {
|
|
|
15
20
|
arguments?: string;
|
|
16
21
|
name?: string | null;
|
|
17
22
|
};
|
|
18
|
-
id
|
|
23
|
+
id?: string;
|
|
19
24
|
index: number;
|
|
20
25
|
type: 'function' | string;
|
|
21
26
|
}
|
|
@@ -44,6 +44,10 @@ export class ServerService implements IMessageService {
|
|
|
44
44
|
return lambdaClient.message.update.mutate({ id, value: { error } });
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
+
async updateMessagePluginError(id: string, error: ChatMessageError): Promise<any> {
|
|
48
|
+
return lambdaClient.message.update.mutate({ id, value: { pluginError: error } });
|
|
49
|
+
}
|
|
50
|
+
|
|
47
51
|
updateMessage(id: string, message: Partial<ChatMessage>): Promise<any> {
|
|
48
52
|
return lambdaClient.message.update.mutate({ id, value: message });
|
|
49
53
|
}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { DeepPartial } from 'utility-types';
|
|
2
2
|
|
|
3
3
|
import { DEFAULT_AGENT_CONFIG } from '@/const/settings';
|
|
4
|
+
import { AgentSettingsInstance } from '@/features/AgentSetting';
|
|
4
5
|
import { LobeAgentConfig } from '@/types/agent';
|
|
5
6
|
|
|
6
7
|
export interface AgentState {
|
|
7
8
|
activeId: string;
|
|
8
9
|
agentMap: Record<string, DeepPartial<LobeAgentConfig>>;
|
|
10
|
+
agentSettingInstance?: AgentSettingsInstance | null;
|
|
9
11
|
defaultAgentConfig: LobeAgentConfig;
|
|
10
12
|
isInboxAgentConfigInit: boolean;
|
|
11
13
|
updateAgentChatConfigSignal?: AbortController;
|
|
@@ -19,12 +19,14 @@ import { agentSelectors } from '@/store/agent/selectors';
|
|
|
19
19
|
import { chatHelpers } from '@/store/chat/helpers';
|
|
20
20
|
import { messageMapKey } from '@/store/chat/slices/message/utils';
|
|
21
21
|
import { ChatStore } from '@/store/chat/store';
|
|
22
|
-
import { ChatMessage, MessageToolCall } from '@/types/message';
|
|
22
|
+
import { ChatMessage, ChatMessageError, MessageToolCall } from '@/types/message';
|
|
23
23
|
import { TraceEventPayloads } from '@/types/trace';
|
|
24
24
|
import { setNamespace } from '@/utils/storeDebug';
|
|
25
25
|
import { nanoid } from '@/utils/uuid';
|
|
26
26
|
|
|
27
|
+
import type { ChatStoreState } from '../../initialState';
|
|
27
28
|
import { chatSelectors, topicSelectors } from '../../selectors';
|
|
29
|
+
import { preventLeavingFn, toggleBooleanList } from '../../utils';
|
|
28
30
|
import { MessageDispatch, messagesReducer } from './reducer';
|
|
29
31
|
|
|
30
32
|
const n = setNamespace('m');
|
|
@@ -84,6 +86,12 @@ export interface ChatMessageAction {
|
|
|
84
86
|
id?: string,
|
|
85
87
|
action?: string,
|
|
86
88
|
) => AbortController | undefined;
|
|
89
|
+
internal_toggleLoadingArrays: (
|
|
90
|
+
key: keyof ChatStoreState,
|
|
91
|
+
loading: boolean,
|
|
92
|
+
id?: string,
|
|
93
|
+
action?: string,
|
|
94
|
+
) => AbortController | undefined;
|
|
87
95
|
internal_toggleToolCallingStreaming: (id: string, streaming: boolean[] | undefined) => void;
|
|
88
96
|
internal_toggleMessageLoading: (loading: boolean, id: string) => void;
|
|
89
97
|
/**
|
|
@@ -123,6 +131,7 @@ export interface ChatMessageAction {
|
|
|
123
131
|
content: string,
|
|
124
132
|
toolCalls?: MessageToolCall[],
|
|
125
133
|
) => Promise<void>;
|
|
134
|
+
internal_updateMessageError: (id: string, error: ChatMessageError | null) => Promise<void>;
|
|
126
135
|
internal_createMessage: (
|
|
127
136
|
params: CreateMessageParams,
|
|
128
137
|
context?: { tempMessageId?: string; skipRefresh?: boolean },
|
|
@@ -136,24 +145,6 @@ export interface ChatMessageAction {
|
|
|
136
145
|
const getAgentConfig = () => agentSelectors.currentAgentConfig(useAgentStore.getState());
|
|
137
146
|
const getAgentChatConfig = () => agentSelectors.currentAgentChatConfig(useAgentStore.getState());
|
|
138
147
|
|
|
139
|
-
const preventLeavingFn = (e: BeforeUnloadEvent) => {
|
|
140
|
-
// set returnValue to trigger alert modal
|
|
141
|
-
// Note: No matter what value is set, the browser will display the standard text
|
|
142
|
-
e.returnValue = '你有正在生成中的请求,确定要离开吗?';
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
const toggleBooleanList = (ids: string[], id: string, loading: boolean) => {
|
|
146
|
-
return produce(ids, (draft) => {
|
|
147
|
-
if (loading) {
|
|
148
|
-
draft.push(id);
|
|
149
|
-
} else {
|
|
150
|
-
const index = draft.indexOf(id);
|
|
151
|
-
|
|
152
|
-
if (index >= 0) draft.splice(index, 1);
|
|
153
|
-
}
|
|
154
|
-
});
|
|
155
|
-
};
|
|
156
|
-
|
|
157
148
|
export const chatMessage: StateCreator<
|
|
158
149
|
ChatStore,
|
|
159
150
|
[['zustand/devtools', never]],
|
|
@@ -591,35 +582,7 @@ export const chatMessage: StateCreator<
|
|
|
591
582
|
};
|
|
592
583
|
},
|
|
593
584
|
internal_toggleChatLoading: (loading, id, action) => {
|
|
594
|
-
|
|
595
|
-
window.addEventListener('beforeunload', preventLeavingFn);
|
|
596
|
-
|
|
597
|
-
const abortController = new AbortController();
|
|
598
|
-
set(
|
|
599
|
-
{
|
|
600
|
-
abortController,
|
|
601
|
-
chatLoadingIds: toggleBooleanList(get().messageLoadingIds, id!, loading),
|
|
602
|
-
},
|
|
603
|
-
false,
|
|
604
|
-
action,
|
|
605
|
-
);
|
|
606
|
-
|
|
607
|
-
return abortController;
|
|
608
|
-
} else {
|
|
609
|
-
if (!id) {
|
|
610
|
-
set({ abortController: undefined, chatLoadingIds: [] }, false, action);
|
|
611
|
-
} else
|
|
612
|
-
set(
|
|
613
|
-
{
|
|
614
|
-
abortController: undefined,
|
|
615
|
-
chatLoadingIds: toggleBooleanList(get().messageLoadingIds, id, loading),
|
|
616
|
-
},
|
|
617
|
-
false,
|
|
618
|
-
action,
|
|
619
|
-
);
|
|
620
|
-
|
|
621
|
-
window.removeEventListener('beforeunload', preventLeavingFn);
|
|
622
|
-
}
|
|
585
|
+
return get().internal_toggleLoadingArrays('chatLoadingIds', loading, id, action);
|
|
623
586
|
},
|
|
624
587
|
internal_toggleMessageLoading: (loading, id) => {
|
|
625
588
|
set(
|
|
@@ -684,6 +647,11 @@ export const chatMessage: StateCreator<
|
|
|
684
647
|
await internal_coreProcessMessage(contextMessages, latestMsg.id, { traceId });
|
|
685
648
|
},
|
|
686
649
|
|
|
650
|
+
internal_updateMessageError: async (id, error) => {
|
|
651
|
+
get().internal_dispatchMessage({ id, type: 'updateMessages', value: { error } });
|
|
652
|
+
await messageService.updateMessage(id, { error });
|
|
653
|
+
await get().refreshMessages();
|
|
654
|
+
},
|
|
687
655
|
internal_updateMessageContent: async (id, content, toolCalls) => {
|
|
688
656
|
const { internal_dispatchMessage, refreshMessages, internal_transformToolCalls } = get();
|
|
689
657
|
|
|
@@ -762,4 +730,36 @@ export const chatMessage: StateCreator<
|
|
|
762
730
|
.catch();
|
|
763
731
|
}
|
|
764
732
|
},
|
|
733
|
+
|
|
734
|
+
internal_toggleLoadingArrays: (key, loading, id, action) => {
|
|
735
|
+
if (loading) {
|
|
736
|
+
window.addEventListener('beforeunload', preventLeavingFn);
|
|
737
|
+
|
|
738
|
+
const abortController = new AbortController();
|
|
739
|
+
set(
|
|
740
|
+
{
|
|
741
|
+
abortController,
|
|
742
|
+
[key]: toggleBooleanList(get()[key] as string[], id!, loading),
|
|
743
|
+
},
|
|
744
|
+
false,
|
|
745
|
+
action,
|
|
746
|
+
);
|
|
747
|
+
|
|
748
|
+
return abortController;
|
|
749
|
+
} else {
|
|
750
|
+
if (!id) {
|
|
751
|
+
set({ abortController: undefined, [key]: [] }, false, action);
|
|
752
|
+
} else
|
|
753
|
+
set(
|
|
754
|
+
{
|
|
755
|
+
abortController: undefined,
|
|
756
|
+
[key]: toggleBooleanList(get()[key] as string[], id, loading),
|
|
757
|
+
},
|
|
758
|
+
false,
|
|
759
|
+
action,
|
|
760
|
+
);
|
|
761
|
+
|
|
762
|
+
window.removeEventListener('beforeunload', preventLeavingFn);
|
|
763
|
+
}
|
|
764
|
+
},
|
|
765
765
|
});
|
|
@@ -26,6 +26,7 @@ export interface ChatMessageState {
|
|
|
26
26
|
*/
|
|
27
27
|
messagesInit: boolean;
|
|
28
28
|
messagesMap: Record<string, ChatMessage[]>;
|
|
29
|
+
pluginApiLoadingIds: string[];
|
|
29
30
|
/**
|
|
30
31
|
* the tool calling stream ids
|
|
31
32
|
*/
|
|
@@ -41,5 +42,6 @@ export const initialMessageState: ChatMessageState = {
|
|
|
41
42
|
messageLoadingIds: [],
|
|
42
43
|
messagesInit: false,
|
|
43
44
|
messagesMap: {},
|
|
45
|
+
pluginApiLoadingIds: [],
|
|
44
46
|
toolCallingStreamIds: {},
|
|
45
47
|
};
|
|
@@ -129,6 +129,8 @@ const isHasMessageLoading = (s: ChatStore) => s.messageLoadingIds.length > 0;
|
|
|
129
129
|
const isCreatingMessage = (s: ChatStore) => s.isCreatingMessage;
|
|
130
130
|
|
|
131
131
|
const isMessageGenerating = (id: string) => (s: ChatStore) => s.chatLoadingIds.includes(id);
|
|
132
|
+
const isPluginApiInvoking = (id: string) => (s: ChatStore) => s.pluginApiLoadingIds.includes(id);
|
|
133
|
+
|
|
132
134
|
const isToolCallStreaming = (id: string, index: number) => (s: ChatStore) => {
|
|
133
135
|
const isLoading = s.toolCallingStreamIds[id];
|
|
134
136
|
|
|
@@ -155,6 +157,7 @@ export const chatSelectors = {
|
|
|
155
157
|
isMessageEditing,
|
|
156
158
|
isMessageGenerating,
|
|
157
159
|
isMessageLoading,
|
|
160
|
+
isPluginApiInvoking,
|
|
158
161
|
isToolCallStreaming,
|
|
159
162
|
latestMessage,
|
|
160
163
|
showInboxWelcome,
|
|
@@ -112,7 +112,7 @@ describe('ChatPluginAction', () => {
|
|
|
112
112
|
|
|
113
113
|
vi.spyOn(storeState, 'refreshMessages');
|
|
114
114
|
vi.spyOn(storeState, 'triggerAIMessage').mockResolvedValue(undefined);
|
|
115
|
-
vi.spyOn(storeState, '
|
|
115
|
+
vi.spyOn(storeState, 'internal_togglePluginApiCalling').mockReturnValue(undefined);
|
|
116
116
|
|
|
117
117
|
const runSpy = vi.spyOn(chatService, 'runPluginApi').mockResolvedValue({
|
|
118
118
|
text: pluginApiResponse,
|
|
@@ -125,7 +125,7 @@ describe('ChatPluginAction', () => {
|
|
|
125
125
|
await result.current.invokeDefaultTypePlugin(messageId, pluginPayload);
|
|
126
126
|
});
|
|
127
127
|
|
|
128
|
-
expect(storeState.
|
|
128
|
+
expect(storeState.internal_togglePluginApiCalling).toHaveBeenCalledWith(
|
|
129
129
|
true,
|
|
130
130
|
messageId,
|
|
131
131
|
expect.any(String),
|
|
@@ -135,7 +135,7 @@ describe('ChatPluginAction', () => {
|
|
|
135
135
|
content: pluginApiResponse,
|
|
136
136
|
});
|
|
137
137
|
expect(storeState.refreshMessages).toHaveBeenCalled();
|
|
138
|
-
expect(storeState.
|
|
138
|
+
expect(storeState.internal_togglePluginApiCalling).toHaveBeenCalledWith(
|
|
139
139
|
false,
|
|
140
140
|
'message-id',
|
|
141
141
|
'plugin/fetchPlugin/end',
|
|
@@ -150,7 +150,7 @@ describe('ChatPluginAction', () => {
|
|
|
150
150
|
const storeState = useChatStore.getState();
|
|
151
151
|
vi.spyOn(storeState, 'refreshMessages');
|
|
152
152
|
vi.spyOn(storeState, 'triggerAIMessage').mockResolvedValue(undefined);
|
|
153
|
-
vi.spyOn(storeState, '
|
|
153
|
+
vi.spyOn(storeState, 'internal_togglePluginApiCalling').mockReturnValue(undefined);
|
|
154
154
|
|
|
155
155
|
vi.spyOn(chatService, 'runPluginApi').mockRejectedValue(error);
|
|
156
156
|
|
|
@@ -159,7 +159,7 @@ describe('ChatPluginAction', () => {
|
|
|
159
159
|
await result.current.invokeDefaultTypePlugin(messageId, pluginPayload);
|
|
160
160
|
});
|
|
161
161
|
|
|
162
|
-
expect(storeState.
|
|
162
|
+
expect(storeState.internal_togglePluginApiCalling).toHaveBeenCalledWith(
|
|
163
163
|
true,
|
|
164
164
|
messageId,
|
|
165
165
|
expect.any(String),
|
|
@@ -167,7 +167,7 @@ describe('ChatPluginAction', () => {
|
|
|
167
167
|
expect(chatService.runPluginApi).toHaveBeenCalledWith(pluginPayload, { trace: {} });
|
|
168
168
|
expect(messageService.updateMessageError).toHaveBeenCalledWith(messageId, error);
|
|
169
169
|
expect(storeState.refreshMessages).toHaveBeenCalled();
|
|
170
|
-
expect(storeState.
|
|
170
|
+
expect(storeState.internal_togglePluginApiCalling).toHaveBeenCalledWith(
|
|
171
171
|
false,
|
|
172
172
|
'message-id',
|
|
173
173
|
'plugin/fetchPlugin/end',
|
|
@@ -467,7 +467,7 @@ describe('ChatPluginAction', () => {
|
|
|
467
467
|
});
|
|
468
468
|
|
|
469
469
|
useChatStore.setState({
|
|
470
|
-
|
|
470
|
+
internal_togglePluginApiCalling: vi.fn(),
|
|
471
471
|
internal_updateMessageContent: vi.fn(),
|
|
472
472
|
text2image: vi.fn(),
|
|
473
473
|
});
|
|
@@ -491,12 +491,12 @@ describe('ChatPluginAction', () => {
|
|
|
491
491
|
);
|
|
492
492
|
|
|
493
493
|
// Verify that loading was toggled correctly
|
|
494
|
-
expect(result.current.
|
|
494
|
+
expect(result.current.internal_togglePluginApiCalling).toHaveBeenCalledWith(
|
|
495
495
|
true,
|
|
496
496
|
messageId,
|
|
497
497
|
expect.any(String),
|
|
498
498
|
);
|
|
499
|
-
expect(result.current.
|
|
499
|
+
expect(result.current.internal_togglePluginApiCalling).toHaveBeenCalledWith(false);
|
|
500
500
|
expect(useChatStore.getState().text2image).toHaveBeenCalled();
|
|
501
501
|
});
|
|
502
502
|
|
|
@@ -516,7 +516,7 @@ describe('ChatPluginAction', () => {
|
|
|
516
516
|
});
|
|
517
517
|
|
|
518
518
|
useChatStore.setState({
|
|
519
|
-
|
|
519
|
+
internal_togglePluginApiCalling: vi.fn(),
|
|
520
520
|
text2image: vi.fn(),
|
|
521
521
|
internal_updateMessageContent: vi.fn(),
|
|
522
522
|
});
|
|
@@ -540,12 +540,12 @@ describe('ChatPluginAction', () => {
|
|
|
540
540
|
);
|
|
541
541
|
|
|
542
542
|
// Verify that loading was toggled correctly
|
|
543
|
-
expect(result.current.
|
|
543
|
+
expect(result.current.internal_togglePluginApiCalling).toHaveBeenCalledWith(
|
|
544
544
|
true,
|
|
545
545
|
messageId,
|
|
546
546
|
expect.any(String),
|
|
547
547
|
);
|
|
548
|
-
expect(result.current.
|
|
548
|
+
expect(result.current.internal_togglePluginApiCalling).toHaveBeenCalledWith(false);
|
|
549
549
|
expect(useChatStore.getState().text2image).not.toHaveBeenCalled();
|
|
550
550
|
});
|
|
551
551
|
|
|
@@ -563,7 +563,7 @@ describe('ChatPluginAction', () => {
|
|
|
563
563
|
});
|
|
564
564
|
|
|
565
565
|
useChatStore.setState({
|
|
566
|
-
|
|
566
|
+
internal_togglePluginApiCalling: vi.fn(),
|
|
567
567
|
internal_updateMessageContent: vi.fn(),
|
|
568
568
|
text2image: vi.fn(),
|
|
569
569
|
refreshMessages: vi.fn(),
|
|
@@ -576,12 +576,12 @@ describe('ChatPluginAction', () => {
|
|
|
576
576
|
});
|
|
577
577
|
|
|
578
578
|
// Verify that loading was toggled correctly
|
|
579
|
-
expect(result.current.
|
|
579
|
+
expect(result.current.internal_togglePluginApiCalling).toHaveBeenCalledWith(
|
|
580
580
|
true,
|
|
581
581
|
messageId,
|
|
582
582
|
expect.any(String),
|
|
583
583
|
);
|
|
584
|
-
expect(result.current.
|
|
584
|
+
expect(result.current.internal_togglePluginApiCalling).toHaveBeenCalledWith(false);
|
|
585
585
|
|
|
586
586
|
// Verify that the message content was not updated
|
|
587
587
|
expect(result.current.internal_updateMessageContent).not.toHaveBeenCalled();
|