@blueking/chat-helper 0.0.1-beta.1 → 0.0.1-beta.10

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 (43) hide show
  1. package/README.md +969 -63
  2. package/dist/agent/type.d.ts +2 -2
  3. package/dist/agent/use-agent.d.ts +439 -435
  4. package/dist/agent/use-agent.ts.js +189 -20
  5. package/dist/event/ag-ui.d.ts +23 -7
  6. package/dist/event/ag-ui.ts.js +161 -150
  7. package/dist/event/type.d.ts +30 -107
  8. package/dist/event/type.ts.js +8 -11
  9. package/dist/http/fetch/fetch.d.ts +40 -36
  10. package/dist/http/fetch/fetch.ts.js +46 -34
  11. package/dist/http/fetch/index.d.ts +1 -0
  12. package/dist/http/fetch/index.ts.js +17 -1
  13. package/dist/http/index.d.ts +20 -16
  14. package/dist/http/index.ts.js +60 -3
  15. package/dist/http/module/agent.d.ts +2 -2
  16. package/dist/http/module/index.d.ts +18 -16
  17. package/dist/http/module/index.ts.js +2 -1
  18. package/dist/http/module/message.d.ts +21 -7
  19. package/dist/http/module/message.ts.js +43 -6
  20. package/dist/http/module/session.d.ts +11 -11
  21. package/dist/http/module/session.ts.js +3 -3
  22. package/dist/http/transform/agent.ts.js +3 -6
  23. package/dist/http/transform/message.d.ts +6 -6
  24. package/dist/http/transform/message.ts.js +540 -118
  25. package/dist/http/transform/session.ts.js +9 -1
  26. package/dist/index.d.ts +2655 -2651
  27. package/dist/index.ts.js +31 -5
  28. package/dist/mediator/index.d.ts +2 -0
  29. package/dist/mediator/index.ts.js +26 -0
  30. package/dist/mediator/type.d.ts +50 -0
  31. package/dist/mediator/type.ts.js +28 -0
  32. package/dist/mediator/use-mediator.d.ts +7 -0
  33. package/dist/mediator/use-mediator.ts.js +47 -0
  34. package/dist/message/type.d.ts +235 -142
  35. package/dist/message/type.ts.js +15 -15
  36. package/dist/message/use-message.d.ts +748 -758
  37. package/dist/message/use-message.ts.js +215 -27
  38. package/dist/session/type.d.ts +10 -0
  39. package/dist/session/use-session.d.ts +1736 -1733
  40. package/dist/session/use-session.ts.js +161 -37
  41. package/dist/type.d.ts +4 -4
  42. package/dist/type.ts.js +25 -0
  43. package/package.json +2 -1
@@ -51,13 +51,40 @@ function _async_to_generator(fn) {
51
51
  });
52
52
  };
53
53
  }
54
+ function _define_property(obj, key, value) {
55
+ if (key in obj) {
56
+ Object.defineProperty(obj, key, {
57
+ value: value,
58
+ enumerable: true,
59
+ configurable: true,
60
+ writable: true
61
+ });
62
+ } else {
63
+ obj[key] = value;
64
+ }
65
+ return obj;
66
+ }
67
+ function _object_spread(target) {
68
+ for(var i = 1; i < arguments.length; i++){
69
+ var source = arguments[i] != null ? arguments[i] : {};
70
+ var ownKeys = Object.keys(source);
71
+ if (typeof Object.getOwnPropertySymbols === "function") {
72
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
73
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
74
+ }));
75
+ }
76
+ ownKeys.forEach(function(key) {
77
+ _define_property(target, key, source[key]);
78
+ });
79
+ }
80
+ return target;
81
+ }
54
82
  import { ref } from 'vue';
55
83
  /**
56
84
  * 使用会话模块,主要做业务的组合
57
- * @param http - HTTP 模块
58
- * @param message - 消息模块
85
+ * @param mediator - 中介者模块,用于获取其他模块的引用
59
86
  * @returns 会话模块
60
- */ export const useSession = (http, message, agent)=>{
87
+ */ export const useSession = (mediator)=>{
61
88
  const list = ref([]);
62
89
  const current = ref(null);
63
90
  const isCurrentLoading = ref(false);
@@ -65,66 +92,159 @@ import { ref } from 'vue';
65
92
  const isCreateLoading = ref(false);
66
93
  const isUpdateLoading = ref(false);
67
94
  const isDeleteLoading = ref(false);
95
+ const isRenameLoading = ref(false);
96
+ /**
97
+ * 更新 list 中的 session,并同步更新 current
98
+ * @param session - Partial session
99
+ */ const updateSessionInList = (session)=>{
100
+ var _current_value;
101
+ // 先更新 list 中对应的 session
102
+ list.value = list.value.map((item)=>item.sessionCode === session.sessionCode ? _object_spread({}, item, session) : item);
103
+ // 如果 current 是被更新的 session,则让 current 重新指向 list 中更新后的对象
104
+ if (((_current_value = current.value) === null || _current_value === void 0 ? void 0 : _current_value.sessionCode) === session.sessionCode) {
105
+ var _list_value_find;
106
+ current.value = (_list_value_find = list.value.find((item)=>item.sessionCode === session.sessionCode)) !== null && _list_value_find !== void 0 ? _list_value_find : current.value;
107
+ }
108
+ };
68
109
  const getSessions = ()=>{
110
+ var _mediator_http;
69
111
  isListLoading.value = true;
70
- return http.session.getSessions().then((res)=>{
112
+ return (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.session.getSessions().then((res)=>{
71
113
  list.value = res;
72
- }).finally(()=>{
114
+ })['finally'](()=>{
73
115
  isListLoading.value = false;
74
116
  });
75
117
  };
76
- const chooseSession = function() {
77
- var _ref = _async_to_generator(function*(sessionCode) {
78
- yield agent.stopChat();
118
+ /**
119
+ * 切换会话
120
+ * @param sessionCode - 会话编码
121
+ * @param options - 选项
122
+ * @param options.loadMessages - 是否加载消息列表,默认 true。新创建的会话可设为 false 跳过加载
123
+ */ const chooseSession = function() {
124
+ var _ref = _async_to_generator(function*(sessionCode, options) {
125
+ var _mediator_agent;
126
+ yield (_mediator_agent = mediator.agent) === null || _mediator_agent === void 0 ? void 0 : _mediator_agent.stopChat();
79
127
  var _list_value_find;
80
128
  // 选择会话
81
129
  current.value = (_list_value_find = list.value.find((item)=>item.sessionCode === sessionCode)) !== null && _list_value_find !== void 0 ? _list_value_find : null;
82
- // 获取会话内容
83
- message.getMessages(sessionCode);
130
+ // 默认加载消息,但允许跳过(新会话消息必定为空,无需加载)
131
+ if ((options === null || options === void 0 ? void 0 : options.loadMessages) !== false) {
132
+ var _mediator_message, // 继续聊天
133
+ _mediator_agent1;
134
+ // 获取会话内容
135
+ yield (_mediator_message = mediator.message) === null || _mediator_message === void 0 ? void 0 : _mediator_message.getMessages(sessionCode);
136
+ (_mediator_agent1 = mediator.agent) === null || _mediator_agent1 === void 0 ? void 0 : _mediator_agent1.resumeStreamingChat(sessionCode);
137
+ } else {
138
+ // 新会话,清空消息列表
139
+ if (mediator.message) {
140
+ mediator.message.list.value = [];
141
+ }
142
+ }
84
143
  });
85
- return function chooseSession(sessionCode) {
144
+ return function chooseSession(sessionCode, options) {
86
145
  return _ref.apply(this, arguments);
87
146
  };
88
147
  }();
89
148
  const getSession = (sessionCode)=>{
149
+ var _mediator_http;
90
150
  isCurrentLoading.value = true;
91
- return http.session.getSession(sessionCode).then((res)=>{
151
+ return (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.session.getSession(sessionCode).then((res)=>{
92
152
  current.value = res;
93
- }).finally(()=>{
153
+ })['finally'](()=>{
94
154
  isCurrentLoading.value = false;
95
155
  });
96
156
  };
97
- const createSession = (session)=>{
98
- isCreateLoading.value = true;
99
- return http.session.plusSession(session).then((res)=>{
100
- list.value.push(current.value);
101
- chooseSession(res.sessionCode);
102
- }).finally(()=>{
103
- isCreateLoading.value = false;
157
+ /**
158
+ * 创建新会话
159
+ * @param session - 会话数据
160
+ * @param options - 选项
161
+ * @param options.loadMessages - 创建后是否加载消息列表,默认 false(新会话消息必定为空)
162
+ */ const createSession = function() {
163
+ var _ref = _async_to_generator(function*(session, options) {
164
+ isCreateLoading.value = true;
165
+ try {
166
+ var _mediator_http;
167
+ const res = yield (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.session.plusSession(session);
168
+ if (res) {
169
+ list.value.unshift(res); // 新会话插入到列表头部
170
+ var _options_loadMessages;
171
+ // 新会话默认不加载消息(消息必定为空),优化 UX
172
+ yield chooseSession(res.sessionCode, {
173
+ loadMessages: (_options_loadMessages = options === null || options === void 0 ? void 0 : options.loadMessages) !== null && _options_loadMessages !== void 0 ? _options_loadMessages : false
174
+ });
175
+ }
176
+ } finally{
177
+ isCreateLoading.value = false;
178
+ }
104
179
  });
105
- };
180
+ return function createSession(session, options) {
181
+ return _ref.apply(this, arguments);
182
+ };
183
+ }();
106
184
  const updateSession = (session)=>{
185
+ var _mediator_http;
107
186
  isUpdateLoading.value = true;
108
- return http.session.modifySession(session).then((res)=>{
109
- current.value = res;
110
- list.value = list.value.map((item)=>item.sessionCode === session.sessionCode ? current.value : item);
111
- }).finally(()=>{
187
+ return (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.session.modifySession(session).then((res)=>{
188
+ updateSessionInList(res);
189
+ })['finally'](()=>{
112
190
  isUpdateLoading.value = false;
113
191
  });
114
192
  };
115
- const deleteSession = (sessionCode)=>{
116
- isDeleteLoading.value = true;
117
- return http.session.deleteSession(sessionCode).then(()=>{
118
- var _current_value;
119
- list.value = list.value.filter((item)=>item.sessionCode !== sessionCode);
120
- // 如果当前会话被删除,选择第一个会话
121
- if (((_current_value = current.value) === null || _current_value === void 0 ? void 0 : _current_value.sessionCode) === sessionCode) {
122
- var _list_value_;
123
- chooseSession((_list_value_ = list.value[0]) === null || _list_value_ === void 0 ? void 0 : _list_value_.sessionCode);
193
+ const deleteSession = function() {
194
+ var _ref = _async_to_generator(function*(sessionCode) {
195
+ isDeleteLoading.value = true;
196
+ try {
197
+ var _mediator_http, _current_value, _list_value_;
198
+ yield (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.session.deleteSession(sessionCode);
199
+ list.value = list.value.filter((item)=>item.sessionCode !== sessionCode);
200
+ // 如果当前会话被删除,选择第一个会话
201
+ if (((_current_value = current.value) === null || _current_value === void 0 ? void 0 : _current_value.sessionCode) === sessionCode && ((_list_value_ = list.value[0]) === null || _list_value_ === void 0 ? void 0 : _list_value_.sessionCode)) {
202
+ yield chooseSession(list.value[0].sessionCode);
203
+ }
204
+ } finally{
205
+ isDeleteLoading.value = false;
124
206
  }
125
- }).finally(()=>{
126
- isDeleteLoading.value = false;
127
207
  });
208
+ return function deleteSession(sessionCode) {
209
+ return _ref.apply(this, arguments);
210
+ };
211
+ }();
212
+ /**
213
+ * 提交会话反馈,sessionContentIds 只需要传 user 对应的 sessionContentId
214
+ * @param data - ISessionFeedback
215
+ */ const postSessionFeedback = (data)=>{
216
+ var _mediator_http;
217
+ return (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.session.postSessionFeedback(data);
218
+ };
219
+ // 获取会话反馈原因
220
+ const getSessionFeedbackReasons = (rate)=>{
221
+ var _mediator_http;
222
+ return (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.session.getSessionFeedbackReasons(rate);
223
+ };
224
+ // 会话重命名
225
+ const renameSession = (sessionCode)=>{
226
+ var _mediator_http;
227
+ isRenameLoading.value = true;
228
+ return (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.session.renameSession(sessionCode).then((res)=>{
229
+ updateSessionInList({
230
+ sessionCode: res.sessionCode,
231
+ sessionName: res.sessionName
232
+ });
233
+ })['finally'](()=>{
234
+ isRenameLoading.value = false;
235
+ });
236
+ };
237
+ /**
238
+ * 重置 session 模块状态
239
+ */ const reset = ()=>{
240
+ list.value = [];
241
+ current.value = null;
242
+ isCurrentLoading.value = false;
243
+ isListLoading.value = false;
244
+ isCreateLoading.value = false;
245
+ isUpdateLoading.value = false;
246
+ isDeleteLoading.value = false;
247
+ isRenameLoading.value = false;
128
248
  };
129
249
  return {
130
250
  list,
@@ -139,6 +259,10 @@ import { ref } from 'vue';
139
259
  getSession,
140
260
  createSession,
141
261
  updateSession,
142
- deleteSession
262
+ deleteSession,
263
+ postSessionFeedback,
264
+ getSessionFeedbackReasons,
265
+ renameSession,
266
+ reset
143
267
  };
144
268
  };
package/dist/type.d.ts CHANGED
@@ -1,10 +1,10 @@
1
- import type { ISSEProtocol, RequestConfig } from './http/fetch';
2
- import type { Response } from './http/fetch';
1
+ import type { IRequestConfig, ISSEProtocol } from './http/fetch';
2
+ import type { IResponse } from './http/fetch';
3
3
  export interface IUseChatHelperOptions {
4
4
  protocol?: ISSEProtocol;
5
5
  interceptors?: {
6
- request?: (config: RequestConfig) => RequestConfig;
7
- response?: (response: Response) => Response;
6
+ request?: (config: IRequestConfig) => IRequestConfig;
7
+ response?: (response: IResponse) => IResponse;
8
8
  };
9
9
  requestData: {
10
10
  data?: (() => Record<string, unknown>) | Record<string, unknown>;
@@ -0,0 +1,25 @@
1
+ /*
2
+ * Tencent is pleased to support the open source community by making
3
+ * 蓝鲸智云PaaS平台 (BlueKing PaaS) available.
4
+ *
5
+ * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
6
+ *
7
+ * 蓝鲸智云PaaS平台 (BlueKing PaaS) is licensed under the MIT License.
8
+ *
9
+ * License for 蓝鲸智云PaaS平台 (BlueKing PaaS):
10
+ *
11
+ * ---------------------------------------------------
12
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13
+ * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
14
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15
+ * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16
+ *
17
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of
18
+ * the Software.
19
+ *
20
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
21
+ * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
23
+ * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24
+ * IN THE SOFTWARE.
25
+ */ export { };
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@blueking/chat-helper",
3
- "version": "0.0.1-beta.1",
3
+ "version": "0.0.1-beta.10",
4
4
  "description": "",
5
5
  "main": "./dist/index.ts.js",
6
+ "types": "./dist/index.d.ts",
6
7
  "scripts": {
7
8
  "build": "bk-cli-service build && vue-tsc --emitDeclarationOnly"
8
9
  },