@blueking/chat-helper 0.0.1-beta.3 → 0.0.1-beta.30

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 +962 -63
  2. package/dist/agent/type.d.ts +27 -2
  3. package/dist/agent/type.ts.js +1 -1
  4. package/dist/agent/use-agent.d.ts +506 -436
  5. package/dist/agent/use-agent.ts.js +224 -25
  6. package/dist/event/ag-ui.d.ts +27 -7
  7. package/dist/event/ag-ui.ts.js +180 -150
  8. package/dist/event/type.d.ts +36 -108
  9. package/dist/event/type.ts.js +9 -11
  10. package/dist/http/fetch/fetch.d.ts +40 -36
  11. package/dist/http/fetch/fetch.ts.js +59 -32
  12. package/dist/http/fetch/index.d.ts +1 -0
  13. package/dist/http/fetch/index.ts.js +17 -1
  14. package/dist/http/index.d.ts +24 -16
  15. package/dist/http/index.ts.js +60 -3
  16. package/dist/http/module/agent.d.ts +2 -2
  17. package/dist/http/module/index.d.ts +22 -16
  18. package/dist/http/module/index.ts.js +2 -1
  19. package/dist/http/module/message.d.ts +22 -7
  20. package/dist/http/module/message.ts.js +49 -7
  21. package/dist/http/module/session.d.ts +14 -11
  22. package/dist/http/module/session.ts.js +66 -4
  23. package/dist/http/transform/agent.ts.js +11 -8
  24. package/dist/http/transform/message.d.ts +6 -6
  25. package/dist/http/transform/message.ts.js +542 -118
  26. package/dist/http/transform/session.ts.js +9 -1
  27. package/dist/index.d.ts +2926 -2631
  28. package/dist/index.ts.js +26 -5
  29. package/dist/mediator/index.d.ts +2 -0
  30. package/dist/mediator/index.ts.js +26 -0
  31. package/dist/mediator/type.d.ts +50 -0
  32. package/dist/mediator/type.ts.js +28 -0
  33. package/dist/mediator/use-mediator.d.ts +7 -0
  34. package/dist/mediator/use-mediator.ts.js +47 -0
  35. package/dist/message/type.d.ts +239 -142
  36. package/dist/message/type.ts.js +15 -15
  37. package/dist/message/use-message.d.ts +817 -754
  38. package/dist/message/use-message.ts.js +226 -28
  39. package/dist/session/type.d.ts +10 -0
  40. package/dist/session/use-session.d.ts +1881 -1729
  41. package/dist/session/use-session.ts.js +198 -33
  42. package/dist/type.d.ts +4 -4
  43. package/package.json +2 -1
@@ -22,66 +22,264 @@
22
22
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
23
23
  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24
24
  * IN THE SOFTWARE.
25
- */ import { ref } from 'vue';
25
+ */ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
26
+ try {
27
+ var info = gen[key](arg);
28
+ var value = info.value;
29
+ } catch (error) {
30
+ reject(error);
31
+ return;
32
+ }
33
+ if (info.done) {
34
+ resolve(value);
35
+ } else {
36
+ Promise.resolve(value).then(_next, _throw);
37
+ }
38
+ }
39
+ function _async_to_generator(fn) {
40
+ return function() {
41
+ var self = this, args = arguments;
42
+ return new Promise(function(resolve, reject) {
43
+ var gen = fn.apply(self, args);
44
+ function _next(value) {
45
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
46
+ }
47
+ function _throw(err) {
48
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
49
+ }
50
+ _next(undefined);
51
+ });
52
+ };
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
+ }
82
+ function ownKeys(object, enumerableOnly) {
83
+ var keys = Object.keys(object);
84
+ if (Object.getOwnPropertySymbols) {
85
+ var symbols = Object.getOwnPropertySymbols(object);
86
+ if (enumerableOnly) {
87
+ symbols = symbols.filter(function(sym) {
88
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
89
+ });
90
+ }
91
+ keys.push.apply(keys, symbols);
92
+ }
93
+ return keys;
94
+ }
95
+ function _object_spread_props(target, source) {
96
+ source = source != null ? source : {};
97
+ if (Object.getOwnPropertyDescriptors) {
98
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
99
+ } else {
100
+ ownKeys(Object(source)).forEach(function(key) {
101
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
102
+ });
103
+ }
104
+ return target;
105
+ }
106
+ import { ref } from 'vue';
26
107
  import { MessageRole, MessageStatus } from './type.ts.js';
108
+ /**
109
+ * 生成临时消息 ID
110
+ */ const generateTempId = ()=>`temp_${Date.now()}_${Math.random().toString(36).slice(2, 9)}`;
27
111
  /**
28
112
  * 使用消息模块,主要做业务的组合
29
113
  * @param http - HTTP 模块
30
114
  * @returns 消息模块
31
- */ export const useMessage = (http)=>{
115
+ */ export const useMessage = (mediator)=>{
32
116
  const list = ref([]);
33
117
  const isListLoading = ref(false);
34
118
  const isDeleteLoading = ref(false);
35
- const isBatchDeleteLoading = ref(false);
36
119
  const getMessages = (sessionCode)=>{
120
+ var _mediator_http;
37
121
  isListLoading.value = true;
38
- return http.message.getMessages(sessionCode).then((res)=>{
122
+ return (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.message.getMessages(sessionCode).then((res)=>{
39
123
  list.value = res;
40
- }).finally(()=>{
124
+ })['finally'](()=>{
41
125
  isListLoading.value = false;
42
126
  });
43
127
  };
44
- const plusMessage = (message)=>{
45
- list.value.push(message);
128
+ /**
129
+ * 清空消息列表 & 调用接口删除所有数据
130
+ */ const clearMessages = ()=>{
131
+ var _mediator_http;
132
+ const messageIds = list.value.map((item)=>item.id);
133
+ return (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.message.batchDeleteMessages(messageIds).then(()=>{
134
+ list.value = [];
135
+ });
46
136
  };
47
137
  const modifyMessage = (message)=>{
48
- list.value = list.value.map((item)=>item.id === message.id ? message : item);
138
+ list.value = list.value.map((item)=>item.messageId === message.messageId ? message : item);
139
+ };
140
+ const plusMessage = (message)=>{
141
+ list.value.push(message);
49
142
  };
50
143
  const getCurrentLoadingMessage = ()=>{
51
144
  return list.value.findLast((item)=>[
52
145
  MessageStatus.Pending,
53
146
  MessageStatus.Streaming
54
- ].includes(item.status) && item.role === MessageRole.Assistant);
147
+ ].includes(item.status));
55
148
  };
56
- const getMessageById = (id)=>{
57
- return list.value.find((item)=>item.id === id);
149
+ const getMessageByMessageId = (id)=>{
150
+ return list.value.find((item)=>item.messageId === id);
58
151
  };
59
- const deleteMessage = (id)=>{
60
- isDeleteLoading.value = true;
61
- return http.message.deleteMessage(id).then(()=>{
62
- list.value = list.value.filter((item)=>item.id !== id);
63
- }).finally(()=>{
64
- isDeleteLoading.value = false;
152
+ /**
153
+ * 创建并添加消息(乐观更新)
154
+ * 1. 立即在前端显示消息(使用临时 ID)
155
+ * 2. 同时调用后端 API
156
+ * 3. API 返回后更新为真实 ID
157
+ * 4. 失败时标记为错误状态
158
+ */ const createAndPlusMessage = function() {
159
+ var _ref = _async_to_generator(function*(message) {
160
+ // 1. 生成临时 ID 并立即添加到列表(乐观更新)
161
+ const tempId = generateTempId();
162
+ const optimisticMessage = _object_spread_props(_object_spread({}, message), {
163
+ id: tempId,
164
+ messageId: tempId
165
+ });
166
+ list.value.push(optimisticMessage);
167
+ try {
168
+ var _mediator_http;
169
+ // 2. 调用后端 API
170
+ const res = yield (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.message.plusMessage(message);
171
+ if (res) {
172
+ // 3. 更新为真实的消息数据
173
+ const index = list.value.findIndex((m)=>m.messageId === tempId);
174
+ if (index !== -1) {
175
+ const mergedMessage = _object_spread({}, res, 'property' in message && message.property ? {
176
+ property: message.property
177
+ } : {});
178
+ list.value[index] = mergedMessage;
179
+ }
180
+ }
181
+ } catch (error) {
182
+ // 4. 失败处理:标记消息为失败状态
183
+ const index = list.value.findIndex((m)=>m.messageId === tempId);
184
+ if (index !== -1) {
185
+ list.value[index] = _object_spread_props(_object_spread({}, list.value[index]), {
186
+ status: MessageStatus.Error
187
+ });
188
+ }
189
+ throw error;
190
+ }
65
191
  });
66
- };
67
- const batchDeleteMessages = (ids)=>{
68
- isBatchDeleteLoading.value = true;
69
- return http.message.batchDeleteMessages(ids).then(()=>{
70
- list.value = list.value.filter((item)=>!ids.includes(item.id));
71
- }).finally(()=>{
72
- isBatchDeleteLoading.value = false;
192
+ return function createAndPlusMessage(message) {
193
+ return _ref.apply(this, arguments);
194
+ };
195
+ }();
196
+ /**
197
+ * 批量删除消息(乐观更新)
198
+ * 1. 立即从列表中移除消息
199
+ * 2. 调用后端 API
200
+ * 3. 失败时恢复消息
201
+ * 接口只需要传 user message id 列表
202
+ */ const deleteMessages = function() {
203
+ var _ref = _async_to_generator(function*(messages) {
204
+ // 获取 user message id 列表
205
+ const ids = messages.reduce((acc, item)=>{
206
+ if (item.role === MessageRole.User) {
207
+ acc.push(item.id);
208
+ }
209
+ return acc;
210
+ }, []);
211
+ // 如果 user message id 列表为空,则直接返回
212
+ if (ids.length <= 0) {
213
+ return;
214
+ }
215
+ // 1. 保存原始列表(用于失败时恢复)
216
+ const originalList = [
217
+ ...list.value
218
+ ];
219
+ // 2. 乐观更新:立即从列表中移除消息
220
+ list.value = list.value.filter((item)=>!messages.includes(item));
221
+ // 3. 调用后端 API
222
+ isDeleteLoading.value = true;
223
+ try {
224
+ var _mediator_http;
225
+ yield (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.message.batchDeleteMessages(ids);
226
+ } catch (error) {
227
+ // 4. 失败时恢复原始列表
228
+ list.value = originalList;
229
+ throw error;
230
+ } finally{
231
+ isDeleteLoading.value = false;
232
+ }
233
+ });
234
+ return function deleteMessages(messages) {
235
+ return _ref.apply(this, arguments);
236
+ };
237
+ }();
238
+ /**
239
+ * 分享消息
240
+ * 提取 user message id 列表,调用后端分享接口
241
+ * @param sessionCode 会话编码
242
+ * @param messages 要分享的消息列表
243
+ * @param expiredAt 过期时间(可选)
244
+ * @returns 分享结果(包含 share_url)
245
+ */ const shareMessages = function() {
246
+ var _ref = _async_to_generator(function*(sessionCode, messages, expiredAt) {
247
+ var _mediator_http;
248
+ // 获取 user message id 列表
249
+ const contentIds = messages.reduce((acc, item)=>{
250
+ if (item.role === MessageRole.User) {
251
+ acc.push(item.id);
252
+ }
253
+ return acc;
254
+ }, []);
255
+ // 如果 user message id 列表为空,则直接返回
256
+ if (contentIds.length <= 0) {
257
+ return;
258
+ }
259
+ return (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.message.shareMessages(sessionCode, contentIds, expiredAt);
73
260
  });
261
+ return function shareMessages(sessionCode, messages, expiredAt) {
262
+ return _ref.apply(this, arguments);
263
+ };
264
+ }();
265
+ const reset = ()=>{
266
+ list.value = [];
267
+ isListLoading.value = false;
268
+ isDeleteLoading.value = false;
74
269
  };
75
270
  return {
76
271
  list,
77
272
  isListLoading,
78
273
  isDeleteLoading,
79
274
  getMessages,
275
+ clearMessages,
80
276
  getCurrentLoadingMessage,
81
- getMessageById,
82
- plusMessage,
277
+ getMessageByMessageId,
83
278
  modifyMessage,
84
- deleteMessage,
85
- batchDeleteMessages
279
+ plusMessage,
280
+ createAndPlusMessage,
281
+ deleteMessages,
282
+ shareMessages,
283
+ reset
86
284
  };
87
285
  };
@@ -1,10 +1,15 @@
1
1
  import type { IMessage } from '../message';
2
2
  export interface ISession<ITool = unknown, IAnchorPathResources = unknown> {
3
3
  anchorPathResources?: IAnchorPathResources;
4
+ comment?: string;
4
5
  createdAt?: string;
5
6
  isTemporary?: boolean;
7
+ labels?: string[];
6
8
  model?: string;
9
+ rate?: number;
7
10
  sessionCode: string;
11
+ /** 会话消息数量,用于判断会话是否有内容 */
12
+ sessionContentCount?: number;
8
13
  sessionName: string;
9
14
  tools?: ITool[];
10
15
  updatedAt?: string;
@@ -24,10 +29,15 @@ export interface ISession<ITool = unknown, IAnchorPathResources = unknown> {
24
29
  }
25
30
  export interface ISessionApi<IToolApi = unknown, IAnchorPathResourcesApi = unknown> {
26
31
  anchor_path_resources?: IAnchorPathResourcesApi;
32
+ comment?: string;
27
33
  created_at?: string;
28
34
  is_temporary?: boolean;
35
+ labels?: string[];
29
36
  model?: string;
37
+ rate?: number;
30
38
  session_code: string;
39
+ /** 会话消息数量 */
40
+ session_content_count?: number;
31
41
  session_name: string;
32
42
  tools?: IToolApi[];
33
43
  updated_at?: string;