@blueking/chat-helper 0.0.11 → 0.0.12-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.
@@ -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
@@ -107,16 +113,56 @@ import { ref } from 'vue';
107
113
  current.value = (_list_value_find = list.value.find((item)=>item.sessionCode === session.sessionCode)) !== null && _list_value_find !== void 0 ? _list_value_find : current.value;
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 - 选项
@@ -126,15 +172,24 @@ import { ref } from 'vue';
126
172
  var // 中止当前聊天
127
173
  _mediator_agent;
128
174
  (_mediator_agent = mediator.agent) === null || _mediator_agent === void 0 ? void 0 : _mediator_agent.abortChat();
129
- // 从接口更新当前会话信息
130
- yield getSession(sessionCode);
175
+ const shouldLoadMessages = (options === null || options === void 0 ? void 0 : options.loadMessages) !== false;
176
+ const localSession = list.value.find((item)=>item.sessionCode === sessionCode);
177
+ // 新会话或明确空会话已在本地列表中,无需额外 GET 会话详情。
178
+ if (!shouldLoadMessages && localSession) {
179
+ current.value = localSession;
180
+ } else {
181
+ // 从接口更新当前会话信息
182
+ yield getSession(sessionCode);
183
+ }
131
184
  // 默认加载消息,但允许跳过(新会话消息必定为空,无需加载)
132
- if ((options === null || options === void 0 ? void 0 : options.loadMessages) !== false) {
185
+ if (shouldLoadMessages) {
133
186
  var _mediator_message, // 继续聊天
134
- _mediator_agent1;
187
+ _mediator_agent1, // 轮询接口,判断是否可以继续聊天
188
+ _mediator_agent2;
135
189
  // 获取会话内容
136
190
  yield (_mediator_message = mediator.message) === null || _mediator_message === void 0 ? void 0 : _mediator_message.getMessages(sessionCode);
137
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);
138
193
  } else {
139
194
  // 新会话,清空消息列表
140
195
  if (mediator.message) {
@@ -168,6 +223,7 @@ import { ref } from 'vue';
168
223
  const res = yield (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.session.plusSession(session);
169
224
  if (res) {
170
225
  list.value.unshift(res); // 新会话插入到列表头部
226
+ count.value += 1;
171
227
  var _options_loadMessages;
172
228
  // 新会话默认不加载消息(消息必定为空),优化 UX
173
229
  yield chooseSession(res.sessionCode, {
@@ -198,6 +254,7 @@ import { ref } from 'vue';
198
254
  var _mediator_http, _current_value, _list_value_;
199
255
  yield (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.session.deleteSession(sessionCode);
200
256
  list.value = list.value.filter((item)=>item.sessionCode !== sessionCode);
257
+ count.value = Math.max(0, count.value - 1);
201
258
  // 如果当前会话被删除,选择第一个会话
202
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)) {
203
260
  yield chooseSession(list.value[0].sessionCode);
@@ -221,7 +278,10 @@ import { ref } from 'vue';
221
278
  var _mediator_http;
222
279
  yield (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.session.batchDeleteSessions(sessionCodes);
223
280
  const deletedSet = new Set(sessionCodes);
281
+ const beforeLength = list.value.length;
224
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);
225
285
  if (current.value && deletedSet.has(current.value.sessionCode)) {
226
286
  if (list.value.length > 0) {
227
287
  yield chooseSession(list.value[0].sessionCode);
@@ -275,8 +335,12 @@ import { ref } from 'vue';
275
335
  */ const reset = ()=>{
276
336
  list.value = [];
277
337
  current.value = null;
338
+ page.value = 0;
339
+ numPages.value = 0;
340
+ count.value = 0;
278
341
  isCurrentLoading.value = false;
279
342
  isListLoading.value = false;
343
+ isLoadingMore.value = false;
280
344
  isCreateLoading.value = false;
281
345
  isUpdateLoading.value = false;
282
346
  isDeleteLoading.value = false;
@@ -286,13 +350,19 @@ import { ref } from 'vue';
286
350
  return {
287
351
  list,
288
352
  current,
353
+ page,
354
+ numPages,
355
+ count,
356
+ hasMore,
289
357
  isCurrentLoading,
290
358
  isListLoading,
359
+ isLoadingMore,
291
360
  isCreateLoading,
292
361
  isUpdateLoading,
293
362
  isDeleteLoading,
294
363
  isBatchDeleteLoading,
295
364
  getSessions,
365
+ loadMoreSessions,
296
366
  chooseSession,
297
367
  getSession,
298
368
  createSession,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blueking/chat-helper",
3
- "version": "0.0.11",
3
+ "version": "0.0.12-beta.10",
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
  }