@blueking/chat-helper 0.0.1-beta.9 → 0.0.3

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 +631 -141
  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 +464 -484
  5. package/dist/agent/use-agent.ts.js +191 -42
  6. package/dist/event/ag-ui.d.ts +37 -7
  7. package/dist/event/ag-ui.ts.js +225 -173
  8. package/dist/event/type.d.ts +99 -107
  9. package/dist/event/type.ts.js +34 -11
  10. package/dist/http/fetch/fetch.d.ts +5 -1
  11. package/dist/http/fetch/fetch.ts.js +40 -51
  12. package/dist/http/fetch/index.d.ts +1 -0
  13. package/dist/http/fetch/index.ts.js +133 -3
  14. package/dist/http/index.d.ts +14 -5
  15. package/dist/http/index.ts.js +59 -3
  16. package/dist/http/module/index.d.ts +13 -5
  17. package/dist/http/module/index.ts.js +2 -1
  18. package/dist/http/module/message.d.ts +22 -5
  19. package/dist/http/module/message.ts.js +51 -4
  20. package/dist/http/module/session.d.ts +4 -1
  21. package/dist/http/module/session.ts.js +66 -4
  22. package/dist/http/transform/agent.ts.js +11 -8
  23. package/dist/http/transform/message.d.ts +6 -6
  24. package/dist/http/transform/message.ts.js +566 -118
  25. package/dist/http/transform/session.ts.js +9 -1
  26. package/dist/index.d.ts +2983 -3314
  27. package/dist/index.ts.js +27 -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 +280 -146
  35. package/dist/message/type.ts.js +16 -15
  36. package/dist/message/use-message.d.ts +847 -963
  37. package/dist/message/use-message.ts.js +230 -37
  38. package/dist/session/type.d.ts +10 -0
  39. package/dist/session/use-session.d.ts +2006 -2214
  40. package/dist/session/use-session.ts.js +198 -33
  41. package/dist/type.d.ts +2 -0
  42. package/package.json +11 -6
@@ -22,77 +22,270 @@
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
- // 从接口获取最新的消息塞到前面
45
- const plusLatestMessage = (sessionCode)=>{
46
- return http.message.getMessages(sessionCode, 1).then((res)=>{
47
- list.value.unshift(res[0]);
48
- }).catch((err)=>{
49
- console.error(err);
50
- });
128
+ /**
129
+ * 清空消息列表 & 调用接口删除所有数据
130
+ */ const clearMessages = ()=>{
131
+ return deleteMessages(list.value);
51
132
  };
52
133
  const modifyMessage = (message)=>{
53
- list.value = list.value.map((item)=>item.id === message.id ? message : item);
134
+ list.value = list.value.map((item)=>item.messageId === message.messageId ? message : item);
135
+ };
136
+ const plusMessage = (message)=>{
137
+ list.value.push(message);
54
138
  };
55
139
  const getCurrentLoadingMessage = ()=>{
56
140
  return list.value.findLast((item)=>[
57
141
  MessageStatus.Pending,
58
142
  MessageStatus.Streaming
59
- ].includes(item.status) && item.role === MessageRole.Assistant);
143
+ ].includes(item.status));
60
144
  };
61
- const getMessageById = (id)=>{
62
- return list.value.find((item)=>item.id === id);
145
+ const getMessageByMessageId = (id)=>{
146
+ return list.value.find((item)=>item.messageId === id);
63
147
  };
64
- const plusMessage = (message)=>{
65
- return http.message.plusMessage(message).then((res)=>{
66
- list.value.unshift(res);
148
+ /**
149
+ * 创建并添加消息(乐观更新)
150
+ * 1. 立即在前端显示消息(使用临时 ID)
151
+ * 2. 同时调用后端 API
152
+ * 3. API 返回后更新为真实 ID
153
+ * 4. 失败时标记为错误状态
154
+ */ const createAndPlusMessage = function() {
155
+ var _ref = _async_to_generator(function*(message) {
156
+ // 1. 生成临时 ID 并立即添加到列表(乐观更新)
157
+ const tempId = generateTempId();
158
+ const optimisticMessage = _object_spread_props(_object_spread({}, message), {
159
+ id: tempId,
160
+ messageId: tempId
161
+ });
162
+ list.value.push(optimisticMessage);
163
+ try {
164
+ var _mediator_http;
165
+ // 2. 调用后端 API
166
+ const res = yield (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.message.plusMessage(message);
167
+ if (res) {
168
+ // 3. 更新为真实的消息数据
169
+ const index = list.value.findIndex((m)=>m.messageId === tempId);
170
+ if (index !== -1) {
171
+ const mergedMessage = _object_spread({}, res, 'property' in message && message.property ? {
172
+ property: message.property
173
+ } : {});
174
+ list.value[index] = mergedMessage;
175
+ }
176
+ }
177
+ } catch (error) {
178
+ // 4. 失败处理:标记消息为失败状态
179
+ const index = list.value.findIndex((m)=>m.messageId === tempId);
180
+ if (index !== -1) {
181
+ list.value[index] = _object_spread_props(_object_spread({}, list.value[index]), {
182
+ status: MessageStatus.Error
183
+ });
184
+ }
185
+ throw error;
186
+ }
67
187
  });
68
- };
69
- const deleteMessage = (id)=>{
70
- isDeleteLoading.value = true;
71
- return http.message.deleteMessage(id).then(()=>{
72
- list.value = list.value.filter((item)=>item.id !== id);
73
- }).finally(()=>{
74
- isDeleteLoading.value = false;
188
+ return function createAndPlusMessage(message) {
189
+ return _ref.apply(this, arguments);
190
+ };
191
+ }();
192
+ /**
193
+ * 批量删除消息(乐观更新)
194
+ * 1. 立即从列表中移除消息
195
+ * 2. 调用后端 API
196
+ * 3. 失败时恢复消息
197
+ * 接口只需要传 user message id 列表
198
+ */ const deleteMessages = function() {
199
+ var _ref = _async_to_generator(function*(messages) {
200
+ // 获取 user message id 列表
201
+ const ids = messages.reduce((acc, item)=>{
202
+ if (item.role === MessageRole.User) {
203
+ acc.push(item.id);
204
+ }
205
+ return acc;
206
+ }, []);
207
+ // 如果 user message id 列表为空,则直接返回
208
+ if (ids.length <= 0) {
209
+ return;
210
+ }
211
+ // 1. 保存原始列表(用于失败时恢复)
212
+ const originalList = [
213
+ ...list.value
214
+ ];
215
+ // 2. 乐观更新:立即从列表中移除消息
216
+ list.value = list.value.filter((item)=>!messages.includes(item));
217
+ // 3. 调用后端 API
218
+ isDeleteLoading.value = true;
219
+ try {
220
+ var _mediator_http;
221
+ yield (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.message.batchDeleteMessages(ids);
222
+ } catch (error) {
223
+ // 4. 失败时恢复原始列表
224
+ list.value = originalList;
225
+ throw error;
226
+ } finally{
227
+ isDeleteLoading.value = false;
228
+ }
75
229
  });
76
- };
77
- const batchDeleteMessages = (ids)=>{
78
- isBatchDeleteLoading.value = true;
79
- return http.message.batchDeleteMessages(ids).then(()=>{
80
- list.value = list.value.filter((item)=>!ids.includes(item.id));
81
- }).finally(()=>{
82
- isBatchDeleteLoading.value = false;
230
+ return function deleteMessages(messages) {
231
+ return _ref.apply(this, arguments);
232
+ };
233
+ }();
234
+ /**
235
+ * 分享消息
236
+ * 提取 user message id 列表,调用后端分享接口
237
+ * @param sessionCode 会话编码
238
+ * @param messages 要分享的消息列表
239
+ * @param expiredAt 过期时间(可选)
240
+ * @returns 分享结果(包含 share_url)
241
+ */ const shareMessages = function() {
242
+ var _ref = _async_to_generator(function*(sessionCode, messages, expiredAt) {
243
+ var _mediator_http;
244
+ // 获取 user message id 列表
245
+ const contentIds = messages.reduce((acc, item)=>{
246
+ if (item.role === MessageRole.User) {
247
+ acc.push(item.id);
248
+ }
249
+ return acc;
250
+ }, []);
251
+ // 如果 user message id 列表为空,则直接返回
252
+ if (contentIds.length <= 0) {
253
+ return;
254
+ }
255
+ return (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.message.shareMessages(sessionCode, contentIds, expiredAt);
83
256
  });
257
+ return function shareMessages(sessionCode, messages, expiredAt) {
258
+ return _ref.apply(this, arguments);
259
+ };
260
+ }();
261
+ const getFlowAgentTaskInfo = (taskId)=>{
262
+ var _mediator_http;
263
+ return (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.message.getFlowAgentTaskInfo(taskId);
264
+ };
265
+ const getFlowAgentTaskNodeInfo = (taskId, nodeId)=>{
266
+ var _mediator_http;
267
+ return (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.message.getFlowAgentTaskNodeInfo(taskId, nodeId);
268
+ };
269
+ const reset = ()=>{
270
+ list.value = [];
271
+ isListLoading.value = false;
272
+ isDeleteLoading.value = false;
84
273
  };
85
274
  return {
86
275
  list,
87
276
  isListLoading,
88
277
  isDeleteLoading,
89
278
  getMessages,
279
+ clearMessages,
90
280
  getCurrentLoadingMessage,
91
- getMessageById,
92
- plusLatestMessage,
281
+ getMessageByMessageId,
93
282
  modifyMessage,
94
283
  plusMessage,
95
- deleteMessage,
96
- batchDeleteMessages
284
+ createAndPlusMessage,
285
+ deleteMessages,
286
+ shareMessages,
287
+ getFlowAgentTaskInfo,
288
+ getFlowAgentTaskNodeInfo,
289
+ reset
97
290
  };
98
291
  };
@@ -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;