@blueking/chat-helper 0.0.12-beta.2 → 0.0.12-beta.21

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.
@@ -79,7 +79,8 @@ function _object_spread(target) {
79
79
  }
80
80
  return target;
81
81
  }
82
- import { ref } from 'vue';
82
+ import { computed, ref } from 'vue';
83
+ /** 会话列表默认每页条数 */ const DEFAULT_PAGE_SIZE = 20;
83
84
  /**
84
85
  * 使用会话模块,主要做业务的组合
85
86
  * @param mediator - 中介者模块,用于获取其他模块的引用
@@ -87,13 +88,18 @@ import { ref } from 'vue';
87
88
  */ export const useSession = (mediator)=>{
88
89
  const list = ref([]);
89
90
  const current = ref(null);
91
+ const page = ref(0);
92
+ const numPages = ref(0);
93
+ const count = ref(0);
90
94
  const isCurrentLoading = ref(false);
91
95
  const isListLoading = ref(false);
96
+ const isLoadingMore = ref(false);
92
97
  const isCreateLoading = ref(false);
93
98
  const isUpdateLoading = ref(false);
94
99
  const isDeleteLoading = ref(false);
95
100
  const isBatchDeleteLoading = ref(false);
96
101
  const isRenameLoading = ref(false);
102
+ const hasMore = computed(()=>page.value > 0 && page.value < numPages.value);
97
103
  /**
98
104
  * 更新 list 中的 session,并同步更新 current
99
105
  * @param session - Partial session
@@ -101,22 +107,62 @@ import { ref } from 'vue';
101
107
  var _current_value;
102
108
  // 先更新 list 中对应的 session
103
109
  list.value = list.value.map((item)=>item.sessionCode === session.sessionCode ? _object_spread({}, item, session) : item);
104
- // 如果 current 是被更新的 session,则让 current 重新指向 list 中更新后的对象
110
+ // 同步 current:优先指向 list 中更新后的对象;若不在 list(分页外 / getSession 单独写入),直接合并字段
105
111
  if (((_current_value = current.value) === null || _current_value === void 0 ? void 0 : _current_value.sessionCode) === session.sessionCode) {
106
112
  var _list_value_find;
107
- current.value = (_list_value_find = list.value.find((item)=>item.sessionCode === session.sessionCode)) !== null && _list_value_find !== void 0 ? _list_value_find : current.value;
113
+ current.value = (_list_value_find = list.value.find((item)=>item.sessionCode === session.sessionCode)) !== null && _list_value_find !== void 0 ? _list_value_find : _object_spread({}, current.value, session);
108
114
  }
109
115
  };
110
- const getSessions = ()=>{
116
+ /**
117
+ * 拉取会话列表(默认第 1 页并替换本地 list)
118
+ */ const getSessions = (params)=>{
111
119
  var _mediator_http;
112
120
  isListLoading.value = true;
113
- return (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.session.getSessions().then((res)=>{
114
- list.value = res;
121
+ var _params_page, _params_page_size;
122
+ const requestParams = {
123
+ page: (_params_page = params === null || params === void 0 ? void 0 : params.page) !== null && _params_page !== void 0 ? _params_page : 1,
124
+ page_size: (_params_page_size = params === null || params === void 0 ? void 0 : params.page_size) !== null && _params_page_size !== void 0 ? _params_page_size : DEFAULT_PAGE_SIZE
125
+ };
126
+ return (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.session.getSessions(requestParams).then((res)=>{
127
+ list.value = res.results;
128
+ page.value = res.page;
129
+ numPages.value = res.numPages;
130
+ count.value = res.count;
115
131
  }).finally(()=>{
116
132
  isListLoading.value = false;
117
133
  });
118
134
  };
119
135
  /**
136
+ * 加载下一页会话并追加到 list(按 sessionCode 去重)
137
+ */ const loadMoreSessions = function() {
138
+ var _ref = _async_to_generator(function*(pageSize = DEFAULT_PAGE_SIZE) {
139
+ if (!hasMore.value || isListLoading.value || isLoadingMore.value) return;
140
+ isLoadingMore.value = true;
141
+ try {
142
+ var _mediator_http;
143
+ const res = yield (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.session.getSessions({
144
+ page: page.value + 1,
145
+ page_size: pageSize
146
+ });
147
+ if (!res) return;
148
+ const existing = new Set(list.value.map((s)=>s.sessionCode));
149
+ const appended = res.results.filter((s)=>!existing.has(s.sessionCode));
150
+ list.value = [
151
+ ...list.value,
152
+ ...appended
153
+ ];
154
+ page.value = res.page;
155
+ numPages.value = res.numPages;
156
+ count.value = res.count;
157
+ } finally{
158
+ isLoadingMore.value = false;
159
+ }
160
+ });
161
+ return function loadMoreSessions() {
162
+ return _ref.apply(this, arguments);
163
+ };
164
+ }();
165
+ /**
120
166
  * 切换会话
121
167
  * @param sessionCode - 会话编码
122
168
  * @param options - 选项
@@ -138,10 +184,12 @@ import { ref } from 'vue';
138
184
  // 默认加载消息,但允许跳过(新会话消息必定为空,无需加载)
139
185
  if (shouldLoadMessages) {
140
186
  var _mediator_message, // 继续聊天
141
- _mediator_agent1;
187
+ _mediator_agent1, // 轮询接口,判断是否可以继续聊天
188
+ _mediator_agent2;
142
189
  // 获取会话内容
143
190
  yield (_mediator_message = mediator.message) === null || _mediator_message === void 0 ? void 0 : _mediator_message.getMessages(sessionCode);
144
191
  (_mediator_agent1 = mediator.agent) === null || _mediator_agent1 === void 0 ? void 0 : _mediator_agent1.resumeStreamingChat(sessionCode);
192
+ (_mediator_agent2 = mediator.agent) === null || _mediator_agent2 === void 0 ? void 0 : _mediator_agent2.pollResumeSession(sessionCode);
145
193
  } else {
146
194
  // 新会话,清空消息列表
147
195
  if (mediator.message) {
@@ -175,6 +223,7 @@ import { ref } from 'vue';
175
223
  const res = yield (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.session.plusSession(session);
176
224
  if (res) {
177
225
  list.value.unshift(res); // 新会话插入到列表头部
226
+ count.value += 1;
178
227
  var _options_loadMessages;
179
228
  // 新会话默认不加载消息(消息必定为空),优化 UX
180
229
  yield chooseSession(res.sessionCode, {
@@ -205,6 +254,7 @@ import { ref } from 'vue';
205
254
  var _mediator_http, _current_value, _list_value_;
206
255
  yield (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.session.deleteSession(sessionCode);
207
256
  list.value = list.value.filter((item)=>item.sessionCode !== sessionCode);
257
+ count.value = Math.max(0, count.value - 1);
208
258
  // 如果当前会话被删除,选择第一个会话
209
259
  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)) {
210
260
  yield chooseSession(list.value[0].sessionCode);
@@ -228,7 +278,10 @@ import { ref } from 'vue';
228
278
  var _mediator_http;
229
279
  yield (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.session.batchDeleteSessions(sessionCodes);
230
280
  const deletedSet = new Set(sessionCodes);
281
+ const beforeLength = list.value.length;
231
282
  list.value = list.value.filter((item)=>!deletedSet.has(item.sessionCode));
283
+ const removedCount = beforeLength - list.value.length;
284
+ count.value = Math.max(0, count.value - removedCount);
232
285
  if (current.value && deletedSet.has(current.value.sessionCode)) {
233
286
  if (list.value.length > 0) {
234
287
  yield chooseSession(list.value[0].sessionCode);
@@ -259,7 +312,7 @@ import { ref } from 'vue';
259
312
  var _mediator_http;
260
313
  return (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.session.getSessionFeedbackReasons(rate);
261
314
  };
262
- // 会话重命名
315
+ // 会话重命名(返回接口最新 session,供上层事件直接使用新名称)
263
316
  const renameSession = (sessionCode)=>{
264
317
  var _mediator_http;
265
318
  isRenameLoading.value = true;
@@ -268,6 +321,7 @@ import { ref } from 'vue';
268
321
  sessionCode: res.sessionCode,
269
322
  sessionName: res.sessionName
270
323
  });
324
+ return res;
271
325
  }).finally(()=>{
272
326
  isRenameLoading.value = false;
273
327
  });
@@ -277,13 +331,21 @@ import { ref } from 'vue';
277
331
  var _mediator_http;
278
332
  return (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.session.uploadFile(sessionCode, file);
279
333
  };
334
+ const getPvFileDownloadUrl = (sessionCode, path, options)=>{
335
+ var _mediator_http;
336
+ return (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.session.getPvFileDownloadUrl(sessionCode, path, options);
337
+ };
280
338
  /**
281
339
  * 重置 session 模块状态
282
340
  */ const reset = ()=>{
283
341
  list.value = [];
284
342
  current.value = null;
343
+ page.value = 0;
344
+ numPages.value = 0;
345
+ count.value = 0;
285
346
  isCurrentLoading.value = false;
286
347
  isListLoading.value = false;
348
+ isLoadingMore.value = false;
287
349
  isCreateLoading.value = false;
288
350
  isUpdateLoading.value = false;
289
351
  isDeleteLoading.value = false;
@@ -293,13 +355,19 @@ import { ref } from 'vue';
293
355
  return {
294
356
  list,
295
357
  current,
358
+ page,
359
+ numPages,
360
+ count,
361
+ hasMore,
296
362
  isCurrentLoading,
297
363
  isListLoading,
364
+ isLoadingMore,
298
365
  isCreateLoading,
299
366
  isUpdateLoading,
300
367
  isDeleteLoading,
301
368
  isBatchDeleteLoading,
302
369
  getSessions,
370
+ loadMoreSessions,
303
371
  chooseSession,
304
372
  getSession,
305
373
  createSession,
@@ -310,6 +378,7 @@ import { ref } from 'vue';
310
378
  getSessionFeedbackReasons,
311
379
  renameSession,
312
380
  uploadFile,
381
+ getPvFileDownloadUrl,
313
382
  reset
314
383
  };
315
384
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blueking/chat-helper",
3
- "version": "0.0.12-beta.2",
3
+ "version": "0.0.12-beta.21",
4
4
  "description": "",
5
5
  "main": "./dist/index.ts.js",
6
6
  "types": "./dist/index.d.ts",
@@ -26,6 +26,9 @@
26
26
  "typescript": "^5.5.4",
27
27
  "vue-tsc": "^3.1.4"
28
28
  },
29
+ "dependencies": {
30
+ "@types/json-schema": "^7.0.15"
31
+ },
29
32
  "scripts": {
30
33
  "build": "bk-cli-service build && vue-tsc --emitDeclarationOnly"
31
34
  }