balibabu commited on
Commit
09502ac
·
1 Parent(s): 9cbbfbf

feat: catch errors when sending messages #918 (#1113)

Browse files

### What problem does this PR solve?

feat: catch errors when sending messages #918

### Type of change

- [x] New Feature (non-breaking change which adds functionality)

web/src/hooks/logicHooks.ts CHANGED
@@ -1,6 +1,7 @@
1
  import { Authorization } from '@/constants/authorization';
2
  import { LanguageTranslationMap } from '@/constants/common';
3
  import { Pagination } from '@/interfaces/common';
 
4
  import { IAnswer } from '@/interfaces/database/chat';
5
  import { IKnowledgeFile } from '@/interfaces/database/knowledge';
6
  import { IChangeParserConfigRequestBody } from '@/interfaces/request/document';
@@ -153,7 +154,9 @@ export const useSendMessageWithSse = (
153
  const [done, setDone] = useState(true);
154
 
155
  const send = useCallback(
156
- async (body: any) => {
 
 
157
  try {
158
  setDone(false);
159
  const response = await fetch(url, {
@@ -165,6 +168,8 @@ export const useSendMessageWithSse = (
165
  body: JSON.stringify(body),
166
  });
167
 
 
 
168
  const reader = response?.body
169
  ?.pipeThrough(new TextDecoderStream())
170
  .pipeThrough(new EventSourceParserStream())
@@ -192,7 +197,7 @@ export const useSendMessageWithSse = (
192
  }
193
  console.info('done?');
194
  setDone(true);
195
- return response;
196
  } catch (e) {
197
  setDone(true);
198
  console.warn(e);
 
1
  import { Authorization } from '@/constants/authorization';
2
  import { LanguageTranslationMap } from '@/constants/common';
3
  import { Pagination } from '@/interfaces/common';
4
+ import { ResponseType } from '@/interfaces/database/base';
5
  import { IAnswer } from '@/interfaces/database/chat';
6
  import { IKnowledgeFile } from '@/interfaces/database/knowledge';
7
  import { IChangeParserConfigRequestBody } from '@/interfaces/request/document';
 
154
  const [done, setDone] = useState(true);
155
 
156
  const send = useCallback(
157
+ async (
158
+ body: any,
159
+ ): Promise<{ response: Response; data: ResponseType } | undefined> => {
160
  try {
161
  setDone(false);
162
  const response = await fetch(url, {
 
168
  body: JSON.stringify(body),
169
  });
170
 
171
+ const res = response.clone().json();
172
+
173
  const reader = response?.body
174
  ?.pipeThrough(new TextDecoderStream())
175
  .pipeThrough(new EventSourceParserStream())
 
197
  }
198
  console.info('done?');
199
  setDone(true);
200
+ return { data: await res, response };
201
  } catch (e) {
202
  setDone(true);
203
  console.warn(e);
web/src/interfaces/database/base.ts ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ export interface ResponseType {
2
+ retcode: number;
3
+ data: any;
4
+ retmsg: string;
5
+ status: number;
6
+ }
web/src/pages/flow/canvas/index.tsx CHANGED
@@ -19,6 +19,7 @@ import {
19
  } from '../hooks';
20
  import { TextUpdaterNode } from './node';
21
 
 
22
  import styles from './index.less';
23
 
24
  const nodeTypes = { textUpdater: TextUpdaterNode };
@@ -29,9 +30,11 @@ const edgeTypes = {
29
 
30
  interface IProps {
31
  sideWidth: number;
 
 
32
  }
33
 
34
- function FlowCanvas({ sideWidth }: IProps) {
35
  const {
36
  nodes,
37
  edges,
@@ -99,6 +102,10 @@ function FlowCanvas({ sideWidth }: IProps) {
99
  visible={drawerVisible}
100
  hideModal={hideDrawer}
101
  ></FlowDrawer>
 
 
 
 
102
  </div>
103
  );
104
  }
 
19
  } from '../hooks';
20
  import { TextUpdaterNode } from './node';
21
 
22
+ import ChatDrawer from '../chat/drawer';
23
  import styles from './index.less';
24
 
25
  const nodeTypes = { textUpdater: TextUpdaterNode };
 
30
 
31
  interface IProps {
32
  sideWidth: number;
33
+ chatDrawerVisible: boolean;
34
+ hideChatDrawer(): void;
35
  }
36
 
37
+ function FlowCanvas({ sideWidth, chatDrawerVisible, hideChatDrawer }: IProps) {
38
  const {
39
  nodes,
40
  edges,
 
102
  visible={drawerVisible}
103
  hideModal={hideDrawer}
104
  ></FlowDrawer>
105
+ <ChatDrawer
106
+ visible={chatDrawerVisible}
107
+ hideModal={hideChatDrawer}
108
+ ></ChatDrawer>
109
  </div>
110
  );
111
  }
web/src/pages/flow/chat/box.tsx CHANGED
@@ -82,6 +82,7 @@ const FlowChatBox = () => {
82
  onClose={hideModal}
83
  open={visible}
84
  width={'50vw'}
 
85
  >
86
  <DocumentPreviewer
87
  documentId={documentId}
 
82
  onClose={hideModal}
83
  open={visible}
84
  width={'50vw'}
85
+ mask={false}
86
  >
87
  <DocumentPreviewer
88
  documentId={documentId}
web/src/pages/flow/chat/drawer.tsx CHANGED
@@ -11,6 +11,7 @@ const ChatDrawer = ({ visible, hideModal }: IModalProps<any>) => {
11
  open={visible}
12
  getContainer={false}
13
  width={470}
 
14
  // zIndex={10000}
15
  >
16
  <FlowChatBox></FlowChatBox>
 
11
  open={visible}
12
  getContainer={false}
13
  width={470}
14
+ mask={false}
15
  // zIndex={10000}
16
  >
17
  <FlowChatBox></FlowChatBox>
web/src/pages/flow/chat/hooks.ts CHANGED
@@ -8,10 +8,13 @@ import {
8
  import { IAnswer } from '@/interfaces/database/chat';
9
  import { IMessage } from '@/pages/chat/interface';
10
  import api from '@/utils/api';
 
11
  import { useCallback, useEffect, useState } from 'react';
12
  import { useParams } from 'umi';
13
  import { v4 as uuid } from 'uuid';
14
 
 
 
15
  export const useSelectCurrentMessages = () => {
16
  const { id: id } = useParams();
17
  const [currentMessages, setCurrentMessages] = useState<IMessage[]>([]);
@@ -107,9 +110,11 @@ export const useSendMessage = (
107
  if (message) {
108
  params.message = message;
109
  }
110
- const res: Response | undefined = await send(params);
 
 
 
111
 
112
- if (res?.status !== 200) {
113
  // cancel loading
114
  setValue(message);
115
  removeLatestMessage();
 
8
  import { IAnswer } from '@/interfaces/database/chat';
9
  import { IMessage } from '@/pages/chat/interface';
10
  import api from '@/utils/api';
11
+ import { message } from 'antd';
12
  import { useCallback, useEffect, useState } from 'react';
13
  import { useParams } from 'umi';
14
  import { v4 as uuid } from 'uuid';
15
 
16
+ const antMessage = message;
17
+
18
  export const useSelectCurrentMessages = () => {
19
  const { id: id } = useParams();
20
  const [currentMessages, setCurrentMessages] = useState<IMessage[]>([]);
 
110
  if (message) {
111
  params.message = message;
112
  }
113
+ const res = await send(params);
114
+
115
+ if (res?.response.status !== 200 || res?.data?.retcode !== 0) {
116
+ antMessage.error(res?.data?.retmsg);
117
 
 
118
  // cancel loading
119
  setValue(message);
120
  removeLatestMessage();
web/src/pages/flow/header/index.tsx CHANGED
@@ -1,21 +1,19 @@
1
  import { Button, Flex, Space } from 'antd';
2
 
3
- import { useSetModalState } from '@/hooks/commonHooks';
4
  import { useFetchFlow } from '@/hooks/flow-hooks';
5
  import { ArrowLeftOutlined } from '@ant-design/icons';
6
  import { Link } from 'umi';
7
- import ChatDrawer from '../chat/drawer';
8
- import { useRunGraph, useSaveGraph } from '../hooks';
9
  import styles from './index.less';
10
 
11
- const FlowHeader = () => {
 
 
 
 
12
  const { saveGraph } = useSaveGraph();
13
- const { runGraph } = useRunGraph();
14
- const {
15
- visible: chatDrawerVisible,
16
- hideModal: hideChatDrawer,
17
- showModal: showChatDrawer,
18
- } = useSetModalState();
19
  const { data } = useFetchFlow();
20
 
21
  return (
@@ -41,10 +39,6 @@ const FlowHeader = () => {
41
  </Button>
42
  </Space>
43
  </Flex>
44
- <ChatDrawer
45
- visible={chatDrawerVisible}
46
- hideModal={hideChatDrawer}
47
- ></ChatDrawer>
48
  </>
49
  );
50
  };
 
1
  import { Button, Flex, Space } from 'antd';
2
 
 
3
  import { useFetchFlow } from '@/hooks/flow-hooks';
4
  import { ArrowLeftOutlined } from '@ant-design/icons';
5
  import { Link } from 'umi';
6
+ import { useSaveGraph } from '../hooks';
7
+
8
  import styles from './index.less';
9
 
10
+ interface IProps {
11
+ showChatDrawer(): void;
12
+ }
13
+
14
+ const FlowHeader = ({ showChatDrawer }: IProps) => {
15
  const { saveGraph } = useSaveGraph();
16
+
 
 
 
 
 
17
  const { data } = useFetchFlow();
18
 
19
  return (
 
39
  </Button>
40
  </Space>
41
  </Flex>
 
 
 
 
42
  </>
43
  );
44
  };
web/src/pages/flow/hooks.ts CHANGED
@@ -2,7 +2,6 @@ import { useSetModalState } from '@/hooks/commonHooks';
2
  import {
3
  useFetchFlow,
4
  useFetchFlowTemplates,
5
- useRunFlow,
6
  useSetFlow,
7
  } from '@/hooks/flow-hooks';
8
  import { useFetchLlmList } from '@/hooks/llmHooks';
@@ -216,19 +215,3 @@ export const useFetchDataOnMount = () => {
216
  export const useFlowIsFetching = () => {
217
  return useIsFetching({ queryKey: ['flowDetail'] }) > 0;
218
  };
219
-
220
- export const useRunGraph = () => {
221
- const { data } = useFetchFlow();
222
- const { runFlow } = useRunFlow();
223
- const { id } = useParams();
224
- const { nodes, edges } = useGraphStore((state) => state);
225
- const runGraph = useCallback(() => {
226
- const dslComponents = buildDslComponentsByGraph(nodes, edges);
227
- runFlow({
228
- id: id!!,
229
- dsl: { ...data.dsl, components: dslComponents },
230
- });
231
- }, [nodes, edges, runFlow, id, data]);
232
-
233
- return { runGraph };
234
- };
 
2
  import {
3
  useFetchFlow,
4
  useFetchFlowTemplates,
 
5
  useSetFlow,
6
  } from '@/hooks/flow-hooks';
7
  import { useFetchLlmList } from '@/hooks/llmHooks';
 
215
  export const useFlowIsFetching = () => {
216
  return useIsFetching({ queryKey: ['flowDetail'] }) > 0;
217
  };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
web/src/pages/flow/index.tsx CHANGED
@@ -1,3 +1,4 @@
 
1
  import { Layout } from 'antd';
2
  import { useState } from 'react';
3
  import { ReactFlowProvider } from 'reactflow';
@@ -10,6 +11,11 @@ const { Content } = Layout;
10
 
11
  function RagFlow() {
12
  const [collapsed, setCollapsed] = useState(false);
 
 
 
 
 
13
 
14
  useFetchDataOnMount();
15
 
@@ -18,9 +24,13 @@ function RagFlow() {
18
  <ReactFlowProvider>
19
  <Sider setCollapsed={setCollapsed} collapsed={collapsed}></Sider>
20
  <Layout>
21
- <FlowHeader></FlowHeader>
22
  <Content style={{ margin: 0 }}>
23
- <FlowCanvas sideWidth={collapsed ? 0 : 200}></FlowCanvas>
 
 
 
 
24
  </Content>
25
  </Layout>
26
  </ReactFlowProvider>
 
1
+ import { useSetModalState } from '@/hooks/commonHooks';
2
  import { Layout } from 'antd';
3
  import { useState } from 'react';
4
  import { ReactFlowProvider } from 'reactflow';
 
11
 
12
  function RagFlow() {
13
  const [collapsed, setCollapsed] = useState(false);
14
+ const {
15
+ visible: chatDrawerVisible,
16
+ hideModal: hideChatDrawer,
17
+ showModal: showChatDrawer,
18
+ } = useSetModalState();
19
 
20
  useFetchDataOnMount();
21
 
 
24
  <ReactFlowProvider>
25
  <Sider setCollapsed={setCollapsed} collapsed={collapsed}></Sider>
26
  <Layout>
27
+ <FlowHeader showChatDrawer={showChatDrawer}></FlowHeader>
28
  <Content style={{ margin: 0 }}>
29
+ <FlowCanvas
30
+ sideWidth={collapsed ? 0 : 200}
31
+ chatDrawerVisible={chatDrawerVisible}
32
+ hideChatDrawer={hideChatDrawer}
33
+ ></FlowCanvas>
34
  </Content>
35
  </Layout>
36
  </ReactFlowProvider>
web/src/utils/request.ts CHANGED
@@ -1,4 +1,5 @@
1
  import { Authorization } from '@/constants/authorization';
 
2
  import i18n from '@/locales/config';
3
  import authorizationUtil, { getAuthorization } from '@/utils/authorizationUtil';
4
  import { message, notification } from 'antd';
@@ -42,12 +43,6 @@ type ResultCode =
42
  | 503
43
  | 504;
44
 
45
- interface ResponseType {
46
- retcode: number;
47
- data: any;
48
- retmsg: string;
49
- status: number;
50
- }
51
  const errorHandler = (error: {
52
  response: Response;
53
  message: string;
 
1
  import { Authorization } from '@/constants/authorization';
2
+ import { ResponseType } from '@/interfaces/database/base';
3
  import i18n from '@/locales/config';
4
  import authorizationUtil, { getAuthorization } from '@/utils/authorizationUtil';
5
  import { message, notification } from 'antd';
 
43
  | 503
44
  | 504;
45
 
 
 
 
 
 
 
46
  const errorHandler = (error: {
47
  response: Response;
48
  message: string;