@blueking/chat-helper 0.0.1-beta.2 → 0.0.1-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.
Files changed (42) hide show
  1. package/README.md +962 -63
  2. package/dist/agent/type.d.ts +20 -2
  3. package/dist/agent/use-agent.d.ts +904 -402
  4. package/dist/agent/use-agent.ts.js +218 -25
  5. package/dist/event/ag-ui.d.ts +22 -6
  6. package/dist/event/ag-ui.ts.js +167 -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 +58 -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 +24 -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 +22 -16
  17. package/dist/http/module/index.ts.js +2 -1
  18. package/dist/http/module/message.d.ts +22 -7
  19. package/dist/http/module/message.ts.js +49 -7
  20. package/dist/http/module/session.d.ts +14 -11
  21. package/dist/http/module/session.ts.js +66 -4
  22. package/dist/http/transform/agent.ts.js +6 -7
  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 +3151 -2641
  27. package/dist/index.ts.js +26 -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 +1741 -1733
  40. package/dist/session/use-session.ts.js +198 -33
  41. package/dist/type.d.ts +4 -4
  42. package/package.json +2 -1
@@ -22,55 +22,241 @@
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
124
  }).finally(()=>{
41
125
  isListLoading.value = false;
42
126
  });
43
127
  };
128
+ const modifyMessage = (message)=>{
129
+ list.value = list.value.map((item)=>item.messageId === message.messageId ? message : item);
130
+ };
44
131
  const plusMessage = (message)=>{
45
132
  list.value.push(message);
46
133
  };
47
- const modifyMessage = (message)=>{
48
- list.value = list.value.map((item)=>item.id === message.id ? message : item);
49
- };
50
134
  const getCurrentLoadingMessage = ()=>{
51
135
  return list.value.findLast((item)=>[
52
136
  MessageStatus.Pending,
53
137
  MessageStatus.Streaming
54
- ].includes(item.status) && item.role === MessageRole.Assistant);
138
+ ].includes(item.status));
55
139
  };
56
- const getMessageById = (id)=>{
57
- return list.value.find((item)=>item.id === id);
140
+ const getMessageByMessageId = (id)=>{
141
+ return list.value.find((item)=>item.messageId === id);
58
142
  };
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;
143
+ /**
144
+ * 创建并添加消息(乐观更新)
145
+ * 1. 立即在前端显示消息(使用临时 ID)
146
+ * 2. 同时调用后端 API
147
+ * 3. API 返回后更新为真实 ID
148
+ * 4. 失败时标记为错误状态
149
+ */ const createAndPlusMessage = function() {
150
+ var _ref = _async_to_generator(function*(message) {
151
+ // 1. 生成临时 ID 并立即添加到列表(乐观更新)
152
+ const tempId = generateTempId();
153
+ const optimisticMessage = _object_spread_props(_object_spread({}, message), {
154
+ id: tempId,
155
+ messageId: tempId
156
+ });
157
+ list.value.push(optimisticMessage);
158
+ try {
159
+ var _mediator_http;
160
+ // 2. 调用后端 API
161
+ const res = yield (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.message.plusMessage(message);
162
+ if (res) {
163
+ // 3. 更新为真实的消息数据
164
+ const index = list.value.findIndex((m)=>m.messageId === tempId);
165
+ if (index !== -1) {
166
+ const mergedMessage = _object_spread({}, res, 'property' in message && message.property ? {
167
+ property: message.property
168
+ } : {});
169
+ list.value[index] = mergedMessage;
170
+ }
171
+ }
172
+ } catch (error) {
173
+ // 4. 失败处理:标记消息为失败状态
174
+ const index = list.value.findIndex((m)=>m.messageId === tempId);
175
+ if (index !== -1) {
176
+ list.value[index] = _object_spread_props(_object_spread({}, list.value[index]), {
177
+ status: MessageStatus.Error
178
+ });
179
+ }
180
+ throw error;
181
+ }
65
182
  });
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;
183
+ return function createAndPlusMessage(message) {
184
+ return _ref.apply(this, arguments);
185
+ };
186
+ }();
187
+ /**
188
+ * 批量删除消息(乐观更新)
189
+ * 1. 立即从列表中移除消息
190
+ * 2. 调用后端 API
191
+ * 3. 失败时恢复消息
192
+ * 接口只需要传 user message id 列表
193
+ */ const deleteMessages = function() {
194
+ var _ref = _async_to_generator(function*(messages) {
195
+ // 获取 user message id 列表
196
+ const ids = messages.reduce((acc, item)=>{
197
+ if (item.role === MessageRole.User) {
198
+ acc.push(item.id);
199
+ }
200
+ return acc;
201
+ }, []);
202
+ // 如果 user message id 列表为空,则直接返回
203
+ if (ids.length <= 0) {
204
+ return;
205
+ }
206
+ // 1. 保存原始列表(用于失败时恢复)
207
+ const originalList = [
208
+ ...list.value
209
+ ];
210
+ // 2. 乐观更新:立即从列表中移除消息
211
+ list.value = list.value.filter((item)=>!messages.includes(item));
212
+ // 3. 调用后端 API
213
+ isDeleteLoading.value = true;
214
+ try {
215
+ var _mediator_http;
216
+ yield (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.message.batchDeleteMessages(ids);
217
+ } catch (error) {
218
+ // 4. 失败时恢复原始列表
219
+ list.value = originalList;
220
+ throw error;
221
+ } finally{
222
+ isDeleteLoading.value = false;
223
+ }
224
+ });
225
+ return function deleteMessages(messages) {
226
+ return _ref.apply(this, arguments);
227
+ };
228
+ }();
229
+ /**
230
+ * 分享消息
231
+ * 提取 user message id 列表,调用后端分享接口
232
+ * @param sessionCode 会话编码
233
+ * @param messages 要分享的消息列表
234
+ * @param expiredAt 过期时间(可选)
235
+ * @returns 分享结果(包含 share_url)
236
+ */ const shareMessages = function() {
237
+ var _ref = _async_to_generator(function*(sessionCode, messages, expiredAt) {
238
+ var _mediator_http;
239
+ // 获取 user message id 列表
240
+ const contentIds = messages.reduce((acc, item)=>{
241
+ if (item.role === MessageRole.User) {
242
+ acc.push(item.id);
243
+ }
244
+ return acc;
245
+ }, []);
246
+ // 如果 user message id 列表为空,则直接返回
247
+ if (contentIds.length <= 0) {
248
+ return;
249
+ }
250
+ return (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.message.shareMessages(sessionCode, contentIds, expiredAt);
73
251
  });
252
+ return function shareMessages(sessionCode, messages, expiredAt) {
253
+ return _ref.apply(this, arguments);
254
+ };
255
+ }();
256
+ const reset = ()=>{
257
+ list.value = [];
258
+ isListLoading.value = false;
259
+ isDeleteLoading.value = false;
74
260
  };
75
261
  return {
76
262
  list,
@@ -78,10 +264,12 @@ import { MessageRole, MessageStatus } from './type.ts.js';
78
264
  isDeleteLoading,
79
265
  getMessages,
80
266
  getCurrentLoadingMessage,
81
- getMessageById,
82
- plusMessage,
267
+ getMessageByMessageId,
83
268
  modifyMessage,
84
- deleteMessage,
85
- batchDeleteMessages
269
+ plusMessage,
270
+ createAndPlusMessage,
271
+ deleteMessages,
272
+ shareMessages,
273
+ reset
86
274
  };
87
275
  };
@@ -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;