cnhis-design-vue 3.4.0-beta.72 → 3.4.0-beta.74

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.
Files changed (46) hide show
  1. package/README.md +87 -87
  2. package/es/components/iho-chat/index.d.ts +2732 -332
  3. package/es/components/iho-chat/src/Index.vue.d.ts +2732 -332
  4. package/es/components/iho-chat/src/Index.vue2.js +5 -3
  5. package/es/components/iho-chat/src/api/index.d.ts +89 -16
  6. package/es/components/iho-chat/src/api/index.js +8 -1
  7. package/es/components/iho-chat/src/components/ChatAdd.vue.d.ts +104 -22
  8. package/es/components/iho-chat/src/components/ChatFile.vue.d.ts +191 -29
  9. package/es/components/iho-chat/src/components/ChatFooter.vue.d.ts +191 -29
  10. package/es/components/iho-chat/src/components/ChatHeader.vue.d.ts +755 -101
  11. package/es/components/iho-chat/src/components/ChatMain.vue.d.ts +610 -51
  12. package/es/components/iho-chat/src/components/ChatMain.vue2.js +28 -29
  13. package/es/components/iho-chat/src/components/ChatRecord.vue.d.ts +176 -14
  14. package/es/components/iho-chat/src/components/ChatSearch.vue.d.ts +175 -10
  15. package/es/components/iho-chat/src/components/ChatSearch.vue2.js +2 -1
  16. package/es/components/iho-chat/src/components/ChatSet.vue.d.ts +197 -29
  17. package/es/components/iho-chat/src/components/ChatSet.vue2.js +48 -11
  18. package/es/components/iho-chat/src/components/ContextMenu.js +7 -10
  19. package/es/components/iho-chat/src/components/MessageEvent.vue.d.ts +88 -7
  20. package/es/components/iho-chat/src/components/MessageMarkdown.vue.d.ts +157 -0
  21. package/es/components/iho-chat/src/components/MessageMarkdown.vue.js +6 -0
  22. package/es/components/iho-chat/src/components/MessageMarkdown.vue2.js +41 -0
  23. package/es/components/iho-chat/src/components/MessageMergeForward.vue.d.ts +176 -14
  24. package/es/components/iho-chat/src/components/MultipleVideo.vue.d.ts +192 -29
  25. package/es/components/iho-chat/src/components/PersonProfile.vue.d.ts +91 -8
  26. package/es/components/iho-chat/src/components/PersonProfile.vue2.js +1 -1
  27. package/es/components/iho-chat/src/components/SiderList.vue.d.ts +93 -11
  28. package/es/components/iho-chat/src/components/Video.vue.d.ts +89 -8
  29. package/es/components/iho-chat/src/components/userItemRender.js +2 -1
  30. package/es/components/iho-chat/src/constants/index.d.ts +2 -1
  31. package/es/components/iho-chat/src/constants/index.js +1 -0
  32. package/es/components/iho-chat/src/hooks/useData.d.ts +374 -27
  33. package/es/components/iho-chat/src/hooks/useData.js +30 -9
  34. package/es/components/iho-chat/src/hooks/useStreamOutput.d.ts +6 -0
  35. package/es/components/iho-chat/src/hooks/useStreamOutput.js +100 -0
  36. package/es/components/iho-chat/src/hooks/useVideo.d.ts +89 -7
  37. package/es/components/iho-chat/src/hooks/useWebSocket.js +14 -4
  38. package/es/components/iho-chat/src/types/index.d.ts +39 -7
  39. package/es/components/iho-chat/src/utils/index.d.ts +2 -1
  40. package/es/components/iho-chat/src/utils/sseClient.d.ts +22 -0
  41. package/es/components/iho-chat/src/utils/sseClient.js +97 -0
  42. package/es/components/iho-chat/style/index.css +1 -1
  43. package/es/components/index.css +1 -1
  44. package/es/env.d.ts +25 -25
  45. package/es/shared/package.json.js +1 -1
  46. package/package.json +3 -3
@@ -1,4 +1,4 @@
1
- import { ref, reactive, computed, watch, toRef, inject, toRaw } from 'vue';
1
+ import { reactive, ref, computed, watch, toRef, inject, toRaw } from 'vue';
2
2
  import { openSessionApi, updateStatusApi, groupUserApi } from '../api/index.js';
3
3
  import { promiseTimeout, useIntervalFn } from '@vueuse/core';
4
4
  import { isGroup, listSort, formatTime, transformMessage, $message, simplifyMessage } from '../utils/index.js';
@@ -23,7 +23,13 @@ function getLastMessageContent(session) {
23
23
  return content;
24
24
  }
25
25
  function initData(props, emit) {
26
- const stompClient = ref();
26
+ const stompClient = {
27
+ value: null
28
+ };
29
+ const tempState = {
30
+ currentMsg: {},
31
+ updateSessionItem: {}
32
+ };
27
33
  const state = reactive({
28
34
  orgId: props.orgId,
29
35
  currentSessionItem: {},
@@ -32,11 +38,8 @@ function initData(props, emit) {
32
38
  id: props.userId
33
39
  },
34
40
  msgList: [],
35
- currentMsg: {},
36
- isAppendMsg: false,
37
41
  sessionList: [],
38
42
  isChangeSession: false,
39
- updateSessionItem: {},
40
43
  isUpdateSession: false,
41
44
  currentReferenceMsg: null,
42
45
  currentReEditMsg: null,
@@ -50,8 +53,10 @@ function initData(props, emit) {
50
53
  userId: "",
51
54
  info: null
52
55
  },
53
- isForward: false
56
+ isForward: false,
57
+ loading: false
54
58
  });
59
+ const chatMainRef = ref();
55
60
  const cssVars = useTheme();
56
61
  const unreadTotal = computed(() => {
57
62
  return state.sessionList.reduce((num, item) => num + +item.unreadNum, 0);
@@ -96,7 +101,7 @@ function initData(props, emit) {
96
101
  state.isChangeSession = false;
97
102
  }
98
103
  if (isUpdateSession) {
99
- updateSessionList(state.updateSessionItem);
104
+ updateSessionList(tempState.updateSessionItem);
100
105
  state.isUpdateSession = false;
101
106
  }
102
107
  },
@@ -145,6 +150,17 @@ function initData(props, emit) {
145
150
  resume();
146
151
  }
147
152
  }
153
+ function appendMsg() {
154
+ setMsgList([tempState.currentMsg, ...state.msgList]);
155
+ scrollToBottom();
156
+ }
157
+ function scrollToBottom() {
158
+ var _a;
159
+ (_a = chatMainRef.value) == null ? void 0 : _a.scrollTo({
160
+ top: 0,
161
+ behavior: "auto"
162
+ });
163
+ }
148
164
  async function sendMessage(message) {
149
165
  var _a;
150
166
  try {
@@ -239,7 +255,7 @@ function initData(props, emit) {
239
255
  ...item,
240
256
  lastMessageContent: getLastMessageContent({ ...item, chatType: item.chatType || (matchSessionItem == null ? void 0 : matchSessionItem.chatType) })
241
257
  };
242
- state.updateSessionItem = matchSessionItem ? {
258
+ tempState.updateSessionItem = matchSessionItem ? {
243
259
  ...cloneDeep(matchSessionItem),
244
260
  ...commonItem
245
261
  } : commonItem;
@@ -332,9 +348,12 @@ function initData(props, emit) {
332
348
  return {
333
349
  filterable: toRef(props, "filterable"),
334
350
  trtcProxy: toRef(props, "trtcProxy"),
351
+ token: toRef(props, "token"),
335
352
  cssVars,
336
353
  stompClient,
337
354
  state,
355
+ chatMainRef,
356
+ tempState,
338
357
  unreadTotal,
339
358
  isGroupChat,
340
359
  isGroupLeader,
@@ -350,7 +369,9 @@ function initData(props, emit) {
350
369
  setUpdateSessionItem,
351
370
  setSessionList,
352
371
  emit,
353
- setUserDetail
372
+ setUserDetail,
373
+ appendMsg,
374
+ scrollToBottom
354
375
  };
355
376
  }
356
377
  const useData = () => inject(InjectionIChatData);
@@ -0,0 +1,6 @@
1
+ export declare function useStreamOutput(): {
2
+ handleStreamOutput: () => Promise<{
3
+ msgId: string;
4
+ content: string;
5
+ } | undefined>;
6
+ };
@@ -0,0 +1,100 @@
1
+ import { useData } from './useData.js';
2
+ import 'vue';
3
+ import 'stompjs';
4
+ import 'sockjs-client/dist/sockjs.min.js';
5
+ import { MESSAGE_TYPE } from '../constants/index.js';
6
+ import '../api/index.js';
7
+ import '../utils/index.js';
8
+ import 'lodash-es';
9
+ import 'trtc-sdk-v5';
10
+ import { SseClient } from '../utils/sseClient.js';
11
+ import { uuidGenerator } from '../../../../shared/utils/index.js';
12
+ import { format } from 'date-fns';
13
+ import { useThrottleFn } from '@vueuse/core';
14
+
15
+ function useStreamOutput() {
16
+ const { state, token, appendMsg, tempState, scrollToBottom } = useData();
17
+ function handleRender(id, content) {
18
+ const msgItem = state.msgList.find((item) => item.id === id);
19
+ if (msgItem) {
20
+ const oldContent = msgItem.content.msg;
21
+ if (content && oldContent !== content) {
22
+ msgItem.content.msg = content;
23
+ scrollToBottom();
24
+ }
25
+ }
26
+ }
27
+ const _handleRender = useThrottleFn(handleRender, 250, true, true);
28
+ let sseClient = null;
29
+ async function handleStreamOutput() {
30
+ var _a, _b, _c, _d;
31
+ if (sseClient == null ? void 0 : sseClient.active)
32
+ return;
33
+ const flagObj = {
34
+ id: uuidGenerator(),
35
+ status: "START",
36
+ content: ""
37
+ };
38
+ state.loading = true;
39
+ sseClient == null ? void 0 : sseClient.disconnect();
40
+ sseClient = new SseClient();
41
+ await sseClient.connect(
42
+ "/flow/ai/mcpApi/conversation",
43
+ {
44
+ agentCode: ((_b = (_a = state.currentSessionItem.businessInfo) == null ? void 0 : _a.topicInfo) == null ? void 0 : _b.agentCode) || "A17696-012",
45
+ sessionId: state.currentSessionItem.receiver,
46
+ conversationId: flagObj.id,
47
+ content: "\u804A\u5929\u603B\u7ED3",
48
+ configs: {
49
+ ...(_d = (_c = state.currentSessionItem.businessInfo) == null ? void 0 : _c.topicInfo) == null ? void 0 : _d.param,
50
+ groupId: state.currentSessionItem.receiver
51
+ }
52
+ },
53
+ (data, msg) => {
54
+ const content = data != null ? data : "";
55
+ if (flagObj.status === "START") {
56
+ tempState.currentMsg = {
57
+ id: flagObj.id,
58
+ chatType: state.currentSessionItem.chatType,
59
+ receiver: state.currentSessionItem.receiver,
60
+ content: { chatMessageType: MESSAGE_TYPE.AI_SUMMARY, msg: "" },
61
+ sender: state.userInfo.id,
62
+ senderName: state.userInfo.name,
63
+ senderAvatar: state.userInfo.avatar,
64
+ sendTime: format(new Date(Date.now() + 30 * 1e3), "yyyy-MM-dd HH:mm:ss")
65
+ };
66
+ appendMsg();
67
+ flagObj.status = "STOP";
68
+ } else {
69
+ flagObj.content += content;
70
+ const { id, content: snapshotContent } = flagObj;
71
+ _handleRender(id, snapshotContent);
72
+ }
73
+ },
74
+ {
75
+ headers: {
76
+ Authorization: "bearer " + token.value
77
+ },
78
+ method: "POST",
79
+ onClose: () => {
80
+ state.loading = false;
81
+ sseClient = null;
82
+ },
83
+ onError: () => {
84
+ state.loading = false;
85
+ sseClient == null ? void 0 : sseClient.disconnect();
86
+ sseClient = null;
87
+ }
88
+ }
89
+ );
90
+ return {
91
+ msgId: flagObj.id,
92
+ content: flagObj.content
93
+ };
94
+ }
95
+ return {
96
+ handleStreamOutput
97
+ };
98
+ }
99
+
100
+ export { useStreamOutput };
@@ -1,4 +1,5 @@
1
1
  import { Ref } from 'vue';
2
+ import { MESSAGE_TYPE } from '../constants';
2
3
  import TRTC from 'trtc-sdk-v5';
3
4
  import { AnyFn } from '../../../../shared/types';
4
5
  export interface RemoteUser {
@@ -28,8 +29,50 @@ export interface VideoConfig {
28
29
  }
29
30
  export declare function useVideo(config?: VideoConfig): {
30
31
  state: {
31
- orgId: string | number;
32
- currentSessionItem: import("../../../../shared/types").AnyObject;
32
+ [x: string]: any;
33
+ orgId: string;
34
+ currentSessionItem: {
35
+ id: string;
36
+ sessionKey: string;
37
+ receiver: string;
38
+ createdTime: string;
39
+ avatar: string;
40
+ name: string;
41
+ defaultName: string;
42
+ chatType: import("../constants").CHAT_TYPE;
43
+ roleInfo: import("../constants").ROLE_TYPE;
44
+ status: import("../constants").STATUS;
45
+ sortTime: string;
46
+ topping: boolean;
47
+ unreadNum: number;
48
+ updatedTime: string;
49
+ lastMessageContent: string;
50
+ sender?: string | undefined;
51
+ lastMessageId?: string | undefined;
52
+ lastMessage?: {
53
+ [x: string]: any;
54
+ chatMessageType: MESSAGE_TYPE;
55
+ msg: string;
56
+ referenceContent?: any;
57
+ forwardMessageList?: import("../../../../shared/types").AnyObject[] | undefined;
58
+ messageTemplate?: any;
59
+ messageTemplateData?: any;
60
+ fileUrl?: string | undefined;
61
+ avStatus?: string | undefined;
62
+ } | undefined;
63
+ lastMessageSendTime?: string | undefined;
64
+ lastMessageStatus?: string | undefined;
65
+ lastSenderName?: string | undefined;
66
+ businessInfo?: {
67
+ uiSetting?: import("../../../../shared/types").AnyObject | undefined;
68
+ topicInfo?: {
69
+ [x: string]: any;
70
+ param?: import("../../../../shared/types").AnyObject | undefined;
71
+ } | undefined;
72
+ } | undefined;
73
+ labelList?: import("../../../../shared/types").AnyObject[] | undefined;
74
+ notDisturb: boolean;
75
+ };
33
76
  id: string;
34
77
  userInfo: import("../../../../shared/types").AnyObject;
35
78
  msgList: {
@@ -38,7 +81,7 @@ export declare function useVideo(config?: VideoConfig): {
38
81
  sessionKey: string;
39
82
  content: {
40
83
  [x: string]: any;
41
- chatMessageType: string;
84
+ chatMessageType: MESSAGE_TYPE;
42
85
  msg: string;
43
86
  referenceContent?: any;
44
87
  forwardMessageList?: import("../../../../shared/types").AnyObject[] | undefined;
@@ -56,11 +99,49 @@ export declare function useVideo(config?: VideoConfig): {
56
99
  __time?: string | undefined;
57
100
  __sendTime?: string | undefined;
58
101
  }[];
59
- currentMsg: import("../../../../shared/types").AnyObject;
60
- isAppendMsg: boolean;
61
- sessionList: import("../../../../shared/types").AnyObject[];
102
+ sessionList: {
103
+ id: string;
104
+ sessionKey: string;
105
+ receiver: string;
106
+ createdTime: string;
107
+ avatar: string;
108
+ name: string;
109
+ defaultName: string;
110
+ chatType: import("../constants").CHAT_TYPE;
111
+ roleInfo: import("../constants").ROLE_TYPE;
112
+ status: import("../constants").STATUS;
113
+ sortTime: string;
114
+ topping: boolean;
115
+ unreadNum: number;
116
+ updatedTime: string;
117
+ lastMessageContent: string;
118
+ sender?: string | undefined;
119
+ lastMessageId?: string | undefined;
120
+ lastMessage?: {
121
+ [x: string]: any;
122
+ chatMessageType: MESSAGE_TYPE;
123
+ msg: string;
124
+ referenceContent?: any;
125
+ forwardMessageList?: import("../../../../shared/types").AnyObject[] | undefined;
126
+ messageTemplate?: any;
127
+ messageTemplateData?: any;
128
+ fileUrl?: string | undefined;
129
+ avStatus?: string | undefined;
130
+ } | undefined;
131
+ lastMessageSendTime?: string | undefined;
132
+ lastMessageStatus?: string | undefined;
133
+ lastSenderName?: string | undefined;
134
+ businessInfo?: {
135
+ uiSetting?: import("../../../../shared/types").AnyObject | undefined;
136
+ topicInfo?: {
137
+ [x: string]: any;
138
+ param?: import("../../../../shared/types").AnyObject | undefined;
139
+ } | undefined;
140
+ } | undefined;
141
+ labelList?: import("../../../../shared/types").AnyObject[] | undefined;
142
+ notDisturb: boolean;
143
+ }[];
62
144
  isChangeSession: boolean;
63
- updateSessionItem: import("../../../../shared/types").AnyObject;
64
145
  isUpdateSession: boolean;
65
146
  currentReferenceMsg: import("../../../../shared/types").AnyObject | null;
66
147
  currentReEditMsg: import("../../../../shared/types").AnyObject | null;
@@ -78,6 +159,7 @@ export declare function useVideo(config?: VideoConfig): {
78
159
  info: import("../../../../shared/types").AnyObject | null;
79
160
  };
80
161
  isForward: boolean;
162
+ loading: boolean;
81
163
  };
82
164
  trtcProxy: Ref<any, any>;
83
165
  sendMessage: (message: {
@@ -6,7 +6,17 @@ import { isAudioOrVideoMessage } from '../utils/index.js';
6
6
  import { remove } from 'lodash-es';
7
7
 
8
8
  function useWebSocket(props, data) {
9
- const { stompClient, state, openSession, closeSession, setSessionList, setUpdateSessionItem, setCurrentSessionItem } = data;
9
+ const {
10
+ stompClient,
11
+ state,
12
+ tempState,
13
+ openSession,
14
+ closeSession,
15
+ setSessionList,
16
+ setUpdateSessionItem,
17
+ setCurrentSessionItem,
18
+ appendMsg
19
+ } = data;
10
20
  const groupSessionList = [];
11
21
  initWebSocket();
12
22
  function initWebSocket() {
@@ -77,9 +87,9 @@ function useWebSocket(props, data) {
77
87
  );
78
88
  }
79
89
  async function handleMessage(messageVo, chatType) {
80
- state.currentMsg = messageVo;
90
+ tempState.currentMsg = messageVo;
81
91
  if (messageVo.sessionKey === state.currentSessionItem.sessionKey) {
82
- state.isAppendMsg = true;
92
+ appendMsg();
83
93
  if (messageVo.sender !== state.userInfo.id) {
84
94
  await readMessageApi({
85
95
  chatType,
@@ -115,7 +125,7 @@ function useWebSocket(props, data) {
115
125
  case SUBSCRIBE_MESSAGE_TYPE.MESSAGE: {
116
126
  if (isAudioOrVideoMessage(messageVo.content)) {
117
127
  if (!state.showVideo && !state.showMultipleVideo || messageVo.content.avStatus !== AV_STATUS.IN_CALL) {
118
- state.currentMsg = messageVo;
128
+ tempState.currentMsg = messageVo;
119
129
  Object.assign(state.currentAVMsg, {
120
130
  strRoomId: messageVo.sessionKey,
121
131
  chatMessageType: messageVo.content.chatMessageType,
@@ -1,7 +1,8 @@
1
1
  import { AnyObject } from '../../../../shared/types';
2
2
  import { UploadFileInfo } from 'naive-ui';
3
+ import { CHAT_TYPE, MESSAGE_TYPE, ROLE_TYPE, STATUS } from '../constants';
3
4
  type ItemContent = {
4
- chatMessageType: string;
5
+ chatMessageType: MESSAGE_TYPE;
5
6
  msg: string;
6
7
  referenceContent?: any;
7
8
  forwardMessageList?: AnyObject[];
@@ -25,17 +26,46 @@ export type MsgListItem = {
25
26
  __sendTime?: string;
26
27
  [key: string]: any;
27
28
  };
29
+ export type SessionItem = {
30
+ id: string;
31
+ sessionKey: string;
32
+ receiver: string;
33
+ createdTime: string;
34
+ avatar: string;
35
+ name: string;
36
+ defaultName: string;
37
+ chatType: CHAT_TYPE;
38
+ roleInfo: ROLE_TYPE;
39
+ status: STATUS;
40
+ sortTime: string;
41
+ topping: boolean;
42
+ unreadNum: number;
43
+ updatedTime: string;
44
+ lastMessageContent: string;
45
+ sender?: string;
46
+ lastMessageId?: string;
47
+ lastMessage?: ItemContent;
48
+ lastMessageSendTime?: string;
49
+ lastMessageStatus?: string;
50
+ lastSenderName?: string;
51
+ businessInfo?: {
52
+ uiSetting?: AnyObject;
53
+ topicInfo?: {
54
+ param?: AnyObject;
55
+ [key: string]: any;
56
+ };
57
+ };
58
+ labelList?: AnyObject[];
59
+ notDisturb: boolean;
60
+ };
28
61
  export type IState = {
29
- orgId: number | string;
30
- currentSessionItem: AnyObject;
62
+ orgId: string;
63
+ currentSessionItem: SessionItem;
31
64
  id: string;
32
65
  userInfo: AnyObject;
33
66
  msgList: MsgListItem[];
34
- currentMsg: AnyObject;
35
- isAppendMsg: boolean;
36
- sessionList: AnyObject[];
67
+ sessionList: SessionItem[];
37
68
  isChangeSession: boolean;
38
- updateSessionItem: AnyObject;
39
69
  isUpdateSession: boolean;
40
70
  currentReferenceMsg: null | AnyObject;
41
71
  currentReEditMsg: null | AnyObject;
@@ -53,6 +83,8 @@ export type IState = {
53
83
  info: AnyObject | null;
54
84
  };
55
85
  isForward: boolean;
86
+ loading: boolean;
87
+ [key: string]: any;
56
88
  };
57
89
  export type FileOptions = {
58
90
  file: UploadFileInfo;
@@ -1,11 +1,12 @@
1
1
  import { AnyObject } from '../../../../shared/types';
2
+ import { SessionItem } from '../types';
2
3
  export declare const $message: import("naive-ui/es/message").MessageApi;
3
4
  export declare function formatTime(dateStr: string): {
4
5
  siderMsgTime: any;
5
6
  msgTime: any;
6
7
  recordTime: string;
7
8
  };
8
- export declare function listSort(list: AnyObject[]): AnyObject[];
9
+ export declare function listSort(list: SessionItem[]): SessionItem[];
9
10
  export declare function transformMessage(content: AnyObject): any;
10
11
  export declare function getAVTime(msg: string): string;
11
12
  export declare function simplifyMessage(lastMessage: AnyObject): any;
@@ -0,0 +1,22 @@
1
+ import { type EventSourceMessage } from '@microsoft/fetch-event-source';
2
+ import { AnyObject, AnyFn } from '../../../../shared/types';
3
+ type SSEClientOptions = {
4
+ headers?: Record<string, string>;
5
+ method?: 'GET' | 'POST';
6
+ withCredentials?: boolean;
7
+ parseJSON?: boolean;
8
+ abortOn?: (msg: EventSourceMessage) => boolean;
9
+ onOpen?: (response: Response) => void;
10
+ onError?: (error: unknown) => void;
11
+ onClose?: () => void;
12
+ };
13
+ export declare class SseClient {
14
+ private controller?;
15
+ private connecting;
16
+ private baseOptions;
17
+ constructor(options?: SSEClientOptions);
18
+ get active(): boolean;
19
+ disconnect(): void;
20
+ connect(url: string, params: AnyObject | undefined, onMessage: AnyFn<[unknown, EventSourceMessage], void>, options?: SSEClientOptions): Promise<void>;
21
+ }
22
+ export default SseClient;
@@ -0,0 +1,97 @@
1
+ import { fetchEventSource } from '@microsoft/fetch-event-source';
2
+
3
+ function toQuery(url, params) {
4
+ if (!params || Object.keys(params).length === 0)
5
+ return url;
6
+ const usp = new URLSearchParams();
7
+ Object.entries(params).forEach(([k, v]) => {
8
+ if (v === void 0 || v === null)
9
+ return;
10
+ usp.append(k, typeof v === "string" ? v : JSON.stringify(v));
11
+ });
12
+ return url + (url.includes("?") ? "&" : "?") + usp.toString();
13
+ }
14
+ function safeParse(data, enable) {
15
+ if (!enable)
16
+ return data;
17
+ try {
18
+ return JSON.parse(data);
19
+ } catch (e) {
20
+ return data;
21
+ }
22
+ }
23
+ class SseClient {
24
+ constructor(options = { method: "POST", parseJSON: true }) {
25
+ this.connecting = false;
26
+ this.baseOptions = options;
27
+ }
28
+ get active() {
29
+ return this.connecting && !!this.controller && !this.controller.signal.aborted;
30
+ }
31
+ disconnect() {
32
+ if (this.controller && !this.controller.signal.aborted) {
33
+ this.controller.abort();
34
+ }
35
+ this.connecting = false;
36
+ }
37
+ async connect(url, params = {}, onMessage, options = {}) {
38
+ var _a, _b, _c, _d;
39
+ const opts = {
40
+ parseJSON: true,
41
+ abortOn: (msg) => msg.data === "[DONE]",
42
+ ...this.baseOptions,
43
+ ...options
44
+ };
45
+ if (this.controller)
46
+ this.controller.abort();
47
+ this.controller = new AbortController();
48
+ this.connecting = true;
49
+ const method = (_a = opts.method) != null ? _a : "POST";
50
+ const headers = { "Content-Type": "application/json", ...(_b = opts.headers) != null ? _b : {} };
51
+ const credentials = opts.withCredentials ? "include" : "same-origin";
52
+ const finalUrl = method === "GET" ? toQuery(url, params) : url;
53
+ const body = method === "POST" ? JSON.stringify(params != null ? params : {}) : void 0;
54
+ try {
55
+ await fetchEventSource(finalUrl, {
56
+ method,
57
+ headers,
58
+ body,
59
+ signal: this.controller.signal,
60
+ credentials,
61
+ openWhenHidden: true,
62
+ onopen: async (r) => {
63
+ var _a2;
64
+ return (_a2 = opts.onOpen) == null ? void 0 : _a2.call(opts, r);
65
+ },
66
+ onmessage: (msg) => {
67
+ var _a2;
68
+ try {
69
+ const data = safeParse(msg.data, opts.parseJSON !== false);
70
+ onMessage(data, msg);
71
+ if (opts.abortOn && opts.abortOn(msg))
72
+ this.disconnect();
73
+ } catch (e) {
74
+ (_a2 = opts.onError) == null ? void 0 : _a2.call(opts, e);
75
+ }
76
+ },
77
+ onclose: () => {
78
+ this.disconnect();
79
+ },
80
+ onerror: (err) => {
81
+ var _a2;
82
+ (_a2 = opts.onError) == null ? void 0 : _a2.call(opts, err);
83
+ this.disconnect();
84
+ throw err;
85
+ }
86
+ });
87
+ } catch (e) {
88
+ (_c = opts.onError) == null ? void 0 : _c.call(opts, e);
89
+ this.disconnect();
90
+ } finally {
91
+ this.connecting = false;
92
+ (_d = opts.onClose) == null ? void 0 : _d.call(opts);
93
+ }
94
+ }
95
+ }
96
+
97
+ export { SseClient, SseClient as default };