@blueking/chat-helper 0.0.1-beta.10 → 0.0.1-beta.12

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.
@@ -537,7 +537,8 @@ export declare const useAgent: (mediator: IMediatorModule, protocol: ISSEProtoco
537
537
  chat: (userInput: IUserMessage["content"], sessionCode: string, url?: string, config?: IRequestConfig, property?: IMessageProperty) => Promise<void>;
538
538
  resendMessage: (messageId: string, sessionCode: string, newContent?: string, url?: string, config?: IRequestConfig) => Promise<void>;
539
539
  resumeStreamingChat: (sessionCode: string, url?: string, config?: IRequestConfig) => void;
540
- stopChat: () => Promise<void>;
540
+ abortChat: () => void;
541
+ stopChat: (sessionCode: string) => Promise<void>;
541
542
  getAgentInfo: () => Promise<void>;
542
543
  reset: (protocol: ISSEProtocol) => void;
543
544
  };
@@ -90,8 +90,8 @@ import { MessageRole, MessageStatus } from '../message/index.ts.js';
90
90
  const info = ref(null);
91
91
  const isInfoLoading = ref(false);
92
92
  const isChatting = ref(false);
93
- let chatController = null;
94
93
  let usedProtocol = protocol || new AGUIProtocol();
94
+ let abortController = null;
95
95
  const getAgentInfo = ()=>{
96
96
  var _mediator_http;
97
97
  isInfoLoading.value = true;
@@ -105,8 +105,6 @@ import { MessageRole, MessageStatus } from '../message/index.ts.js';
105
105
  var _ref = _async_to_generator(function*(sessionCode, url, config) {
106
106
  var // 发起聊天
107
107
  _mediator_http;
108
- // 创建一个 AbortController 实例
109
- chatController = new AbortController();
110
108
  // ag-ui 协议需要注入消息模块
111
109
  if (usedProtocol instanceof AGUIProtocol) {
112
110
  usedProtocol.injectMessageModule(mediator.message);
@@ -131,13 +129,15 @@ import { MessageRole, MessageStatus } from '../message/index.ts.js';
131
129
  isChatting.value = true;
132
130
  (_usedProtocol_onStart = usedProtocol.onStart) === null || _usedProtocol_onStart === void 0 ? void 0 : _usedProtocol_onStart.call(usedProtocol);
133
131
  };
132
+ // 创建 AbortController
133
+ abortController = new AbortController();
134
134
  (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.fetchClient.streamRequest(_object_spread({
135
135
  url: url || 'chat_completion/',
136
136
  method: 'POST',
137
137
  data: {
138
138
  session_code: sessionCode
139
139
  },
140
- controller: chatController,
140
+ controller: abortController,
141
141
  onDone,
142
142
  onError,
143
143
  onMessage,
@@ -176,22 +176,33 @@ import { MessageRole, MessageStatus } from '../message/index.ts.js';
176
176
  }();
177
177
  /**
178
178
  * 恢复流式聊天
179
- * 如果最后一条消息处于流式传输中,重新建立连接
179
+ * 如果最后一条消息处于流式传输中或是用户消息,重新建立连接
180
180
  * @param sessionCode - 会话代码
181
181
  * @param url - 请求 URL(可选)
182
182
  * @param config - 请求配置(可选)
183
183
  */ const resumeStreamingChat = (sessionCode, url, config)=>{
184
- var _mediator_message_list_value_at, _mediator_message;
185
- if (((_mediator_message = mediator.message) === null || _mediator_message === void 0 ? void 0 : (_mediator_message_list_value_at = _mediator_message.list.value.at(-1)) === null || _mediator_message_list_value_at === void 0 ? void 0 : _mediator_message_list_value_at.status) === MessageStatus.Streaming) {
184
+ var _mediator_message;
185
+ const lastMessage = (_mediator_message = mediator.message) === null || _mediator_message === void 0 ? void 0 : _mediator_message.list.value.at(-1);
186
+ if ((lastMessage === null || lastMessage === void 0 ? void 0 : lastMessage.status) === MessageStatus.Streaming || (lastMessage === null || lastMessage === void 0 ? void 0 : lastMessage.role) === MessageRole.User) {
186
187
  streamRequest(sessionCode, url, config);
187
188
  }
188
189
  };
189
- const stopChat = function() {
190
- var _ref = _async_to_generator(function*() {
191
- chatController === null || chatController === void 0 ? void 0 : chatController.abort();
192
- chatController = null;
190
+ /**
191
+ * 中止聊天(纯前端中止,后端继续处理)
192
+ */ const abortChat = ()=>{
193
+ var _abortController_abort;
194
+ abortController === null || abortController === void 0 ? void 0 : (_abortController_abort = abortController.abort) === null || _abortController_abort === void 0 ? void 0 : _abortController_abort.call(abortController);
195
+ abortController = null;
196
+ };
197
+ /**
198
+ * 停止会话,后端中止
199
+ * @param sessionCode - 会话代码
200
+ */ const stopChat = function() {
201
+ var _ref = _async_to_generator(function*(sessionCode) {
202
+ var _mediator_http_message;
203
+ return (_mediator_http_message = mediator.http.message) === null || _mediator_http_message === void 0 ? void 0 : _mediator_http_message.stopChat(sessionCode);
193
204
  });
194
- return function stopChat() {
205
+ return function stopChat(sessionCode) {
195
206
  return _ref.apply(this, arguments);
196
207
  };
197
208
  }();
@@ -250,8 +261,6 @@ import { MessageRole, MessageStatus } from '../message/index.ts.js';
250
261
  };
251
262
  }();
252
263
  const reset = (protocol)=>{
253
- // 停止正在进行的聊天
254
- stopChat();
255
264
  // 重置状态
256
265
  usedProtocol = protocol || new AGUIProtocol();
257
266
  info.value = null;
@@ -264,6 +273,7 @@ import { MessageRole, MessageStatus } from '../message/index.ts.js';
264
273
  chat,
265
274
  resendMessage,
266
275
  resumeStreamingChat,
276
+ abortChat,
267
277
  stopChat,
268
278
  getAgentInfo,
269
279
  reset
@@ -336,6 +336,12 @@ import { CustomEventName, EventType } from './type.ts.js';
336
336
  onError(error) {
337
337
  var // 回调给上层
338
338
  _this_onErrorCallback, _this;
339
+ // 创建一个错误消息
340
+ this.messageModule.plusMessage({
341
+ role: MessageRole.Assistant,
342
+ content: error.message,
343
+ status: MessageStatus.Error
344
+ });
339
345
  (_this_onErrorCallback = (_this = this).onErrorCallback) === null || _this_onErrorCallback === void 0 ? void 0 : _this_onErrorCallback.call(_this, error);
340
346
  }
341
347
  onMessage(message) {
@@ -415,6 +415,7 @@ export class FetchClient {
415
415
  var _config_onMessage;
416
416
  const json = JSON.parse(item);
417
417
  (_config_onMessage = config.onMessage) === null || _config_onMessage === void 0 ? void 0 : _config_onMessage.call(config, json);
418
+ temp = '';
418
419
  } else if (item) {
419
420
  temp = item;
420
421
  }
@@ -28,6 +28,9 @@ export declare const useHttp: (options: IUseChatHelperOptions) => {
28
28
  }>;
29
29
  getSessionFeedbackReasons: (rate: number, config?: import("./fetch").IRequestConfig) => Promise<string[]>;
30
30
  renameSession: (sessionCode: string, config?: import("./fetch").IRequestConfig) => Promise<import("..").ISession<unknown, unknown>>;
31
+ uploadFile: (sessionCode: string, file: File, config?: import("./fetch").IRequestConfig) => Promise<{
32
+ download_url?: string;
33
+ }>;
31
34
  };
32
35
  message: {
33
36
  getMessages: (sessionCode: string, limit?: number, config?: import("./fetch").IRequestConfig) => Promise<import("..").IMessage[]>;
@@ -36,6 +39,7 @@ export declare const useHttp: (options: IUseChatHelperOptions) => {
36
39
  deleteMessage: (id: string, config?: import("./fetch").IRequestConfig) => Promise<import("..").IMessage>;
37
40
  batchDeleteMessages: (ids: string[], config?: import("./fetch").IRequestConfig) => Promise<number>;
38
41
  shareMessages: (sessionCode: string, contentIds: string[], expiredAt?: number, config?: import("./fetch").IRequestConfig) => Promise<import("./module/message").IShareMessagesResponse>;
42
+ stopChat: (sessionCode: string, config?: import("./fetch").IRequestConfig) => Promise<void>;
39
43
  };
40
44
  fetchClient: import("./fetch").FetchClient;
41
45
  };
@@ -25,6 +25,9 @@ export declare const useModule: (fetchClient: FetchClient) => {
25
25
  }>;
26
26
  getSessionFeedbackReasons: (rate: number, config?: import("../fetch").IRequestConfig) => Promise<string[]>;
27
27
  renameSession: (sessionCode: string, config?: import("../fetch").IRequestConfig) => Promise<import("../..").ISession<unknown, unknown>>;
28
+ uploadFile: (sessionCode: string, file: File, config?: import("../fetch").IRequestConfig) => Promise<{
29
+ download_url?: string;
30
+ }>;
28
31
  };
29
32
  message: {
30
33
  getMessages: (sessionCode: string, limit?: number, config?: import("../fetch").IRequestConfig) => Promise<import("../..").IMessage[]>;
@@ -33,6 +36,7 @@ export declare const useModule: (fetchClient: FetchClient) => {
33
36
  deleteMessage: (id: string, config?: import("../fetch").IRequestConfig) => Promise<import("../..").IMessage>;
34
37
  batchDeleteMessages: (ids: string[], config?: import("../fetch").IRequestConfig) => Promise<number>;
35
38
  shareMessages: (sessionCode: string, contentIds: string[], expiredAt?: number, config?: import("../fetch").IRequestConfig) => Promise<import("./message").IShareMessagesResponse>;
39
+ stopChat: (sessionCode: string, config?: import("../fetch").IRequestConfig) => Promise<void>;
36
40
  };
37
41
  fetchClient: FetchClient;
38
42
  };
@@ -25,4 +25,5 @@ export declare const useMessage: (fetchClient: FetchClient) => {
25
25
  deleteMessage: (id: string, config?: IRequestConfig) => Promise<IMessage>;
26
26
  batchDeleteMessages: (ids: string[], config?: IRequestConfig) => Promise<number>;
27
27
  shareMessages: (sessionCode: string, contentIds: string[], expiredAt?: number, config?: IRequestConfig) => Promise<IShareMessagesResponse>;
28
+ stopChat: (sessionCode: string, config?: IRequestConfig) => Promise<void>;
28
29
  };
@@ -66,7 +66,7 @@ import { transferMessage2MessageApi, transferMessageApi2Message } from '../trans
66
66
  // 修改会话内容
67
67
  const modifyMessage = (data, config)=>fetchClient.put(`session_content/${data.id}/`, transferMessage2MessageApi(data), config).then(transferMessageApi2Message);
68
68
  // 删除会话内容
69
- const deleteMessage = (id, config)=>fetchClient.delete(`session_content/${id}/`, undefined, config);
69
+ const deleteMessage = (id, config)=>fetchClient['delete'](`session_content/${id}/`, undefined, config);
70
70
  // 批量删除聊天内容
71
71
  const batchDeleteMessages = (ids, config)=>fetchClient.post(`session_content/batch_delete/`, {
72
72
  ids
@@ -78,12 +78,17 @@ import { transferMessage2MessageApi, transferMessageApi2Message } from '../trans
78
78
  }, expiredAt ? {
79
79
  expired_at: expiredAt
80
80
  } : {}), config);
81
+ // 停止会话
82
+ const stopChat = (sessionCode, config)=>fetchClient.post(`session_content/stop/`, {
83
+ session_code: sessionCode
84
+ }, config);
81
85
  return {
82
86
  getMessages,
83
87
  plusMessage,
84
88
  modifyMessage,
85
89
  deleteMessage,
86
90
  batchDeleteMessages,
87
- shareMessages
91
+ shareMessages,
92
+ stopChat
88
93
  };
89
94
  };
@@ -22,4 +22,7 @@ export declare const useSession: (fetchClient: FetchClient) => {
22
22
  }>;
23
23
  getSessionFeedbackReasons: (rate: number, config?: IRequestConfig) => Promise<string[]>;
24
24
  renameSession: (sessionCode: string, config?: IRequestConfig) => Promise<ISession<unknown, unknown>>;
25
+ uploadFile: (sessionCode: string, file: File, config?: IRequestConfig) => Promise<{
26
+ download_url?: string;
27
+ }>;
25
28
  };
@@ -22,7 +22,59 @@
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 { transferSession2SessionApi, transferSessionApi2Session, transferSessionFeedback2SessionFeedbackApi, transferSessionFeedbackApi2SessionFeedback } from '../transform/session.ts.js';
25
+ */ function _define_property(obj, key, value) {
26
+ if (key in obj) {
27
+ Object.defineProperty(obj, key, {
28
+ value: value,
29
+ enumerable: true,
30
+ configurable: true,
31
+ writable: true
32
+ });
33
+ } else {
34
+ obj[key] = value;
35
+ }
36
+ return obj;
37
+ }
38
+ function _object_spread(target) {
39
+ for(var i = 1; i < arguments.length; i++){
40
+ var source = arguments[i] != null ? arguments[i] : {};
41
+ var ownKeys = Object.keys(source);
42
+ if (typeof Object.getOwnPropertySymbols === "function") {
43
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
44
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
45
+ }));
46
+ }
47
+ ownKeys.forEach(function(key) {
48
+ _define_property(target, key, source[key]);
49
+ });
50
+ }
51
+ return target;
52
+ }
53
+ function ownKeys(object, enumerableOnly) {
54
+ var keys = Object.keys(object);
55
+ if (Object.getOwnPropertySymbols) {
56
+ var symbols = Object.getOwnPropertySymbols(object);
57
+ if (enumerableOnly) {
58
+ symbols = symbols.filter(function(sym) {
59
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
60
+ });
61
+ }
62
+ keys.push.apply(keys, symbols);
63
+ }
64
+ return keys;
65
+ }
66
+ function _object_spread_props(target, source) {
67
+ source = source != null ? source : {};
68
+ if (Object.getOwnPropertyDescriptors) {
69
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
70
+ } else {
71
+ ownKeys(Object(source)).forEach(function(key) {
72
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
73
+ });
74
+ }
75
+ return target;
76
+ }
77
+ import { transferSession2SessionApi, transferSessionApi2Session, transferSessionFeedback2SessionFeedbackApi, transferSessionFeedbackApi2SessionFeedback } from '../transform/session.ts.js';
26
78
  /**
27
79
  * session 相关 http 接口
28
80
  * @param fetchClient - 请求客户端
@@ -37,7 +89,7 @@
37
89
  // 修改会话
38
90
  const modifySession = (data, config)=>fetchClient.put(`session/${data.sessionCode}/`, transferSession2SessionApi(data), config).then((res)=>transferSessionApi2Session(res));
39
91
  // 删除会话
40
- const deleteSession = (sessionCode, config)=>fetchClient['delete'](`session/${sessionCode}/`, undefined, config);
92
+ const deleteSession = (sessionCode, config)=>fetchClient.delete(`session/${sessionCode}/`, undefined, config);
41
93
  // 批量删除会话
42
94
  const batchDeleteSessions = (sessionCodes, config)=>fetchClient.post(`session/batch_delete/`, {
43
95
  session_codes: sessionCodes
@@ -52,6 +104,15 @@
52
104
  }, config);
53
105
  // 会话重命名
54
106
  const renameSession = (sessionCode, config)=>fetchClient.post(`session/${sessionCode}/ai_rename/`, undefined, config).then((res)=>transferSessionApi2Session(res));
107
+ // 上传文件到会话
108
+ const uploadFile = (sessionCode, file, config)=>{
109
+ const fileName = encodeURIComponent(file.name);
110
+ return file.arrayBuffer().then((content)=>fetchClient.post(`session/${sessionCode}/upload/${fileName}/`, content, _object_spread_props(_object_spread({}, config), {
111
+ headers: {
112
+ 'Content-Disposition': `attachment; filename="${file.name}"`
113
+ }
114
+ })));
115
+ };
55
116
  return {
56
117
  clearSession,
57
118
  getSessions,
@@ -62,6 +123,7 @@
62
123
  getSession,
63
124
  postSessionFeedback,
64
125
  getSessionFeedbackReasons,
65
- renameSession
126
+ renameSession,
127
+ uploadFile
66
128
  };
67
129
  };
package/dist/index.d.ts CHANGED
@@ -539,7 +539,8 @@ export declare const useChatHelper: (options: IUseChatHelperOptions) => {
539
539
  chat: (userInput: import("./message").IUserMessage["content"], sessionCode: string, url?: string, config?: import("./http").IRequestConfig, property?: import("./message").IMessageProperty) => Promise<void>;
540
540
  resendMessage: (messageId: string, sessionCode: string, newContent?: string, url?: string, config?: import("./http").IRequestConfig) => Promise<void>;
541
541
  resumeStreamingChat: (sessionCode: string, url?: string, config?: import("./http").IRequestConfig) => void;
542
- stopChat: () => Promise<void>;
542
+ abortChat: () => void;
543
+ stopChat: (sessionCode: string) => Promise<void>;
543
544
  getAgentInfo: () => Promise<void>;
544
545
  reset: (protocol: import("./http").ISSEProtocol) => void;
545
546
  };
@@ -2434,6 +2435,9 @@ export declare const useChatHelper: (options: IUseChatHelperOptions) => {
2434
2435
  }>;
2435
2436
  getSessionFeedbackReasons: (rate: number) => Promise<string[]>;
2436
2437
  renameSession: (sessionCode: string) => Promise<void>;
2438
+ uploadFile: (sessionCode: string, file: File) => Promise<{
2439
+ download_url?: string;
2440
+ }>;
2437
2441
  reset: () => void;
2438
2442
  };
2439
2443
  message: {
@@ -3352,6 +3356,9 @@ export declare const useChatHelper: (options: IUseChatHelperOptions) => {
3352
3356
  }>;
3353
3357
  getSessionFeedbackReasons: (rate: number, config?: import("./http").IRequestConfig) => Promise<string[]>;
3354
3358
  renameSession: (sessionCode: string, config?: import("./http").IRequestConfig) => Promise<import("./session").ISession<unknown, unknown>>;
3359
+ uploadFile: (sessionCode: string, file: File, config?: import("./http").IRequestConfig) => Promise<{
3360
+ download_url?: string;
3361
+ }>;
3355
3362
  };
3356
3363
  message: {
3357
3364
  getMessages: (sessionCode: string, limit?: number, config?: import("./http").IRequestConfig) => Promise<import("./message").IMessage[]>;
@@ -3360,6 +3367,7 @@ export declare const useChatHelper: (options: IUseChatHelperOptions) => {
3360
3367
  deleteMessage: (id: string, config?: import("./http").IRequestConfig) => Promise<import("./message").IMessage>;
3361
3368
  batchDeleteMessages: (ids: string[], config?: import("./http").IRequestConfig) => Promise<number>;
3362
3369
  shareMessages: (sessionCode: string, contentIds: string[], expiredAt?: number, config?: import("./http").IRequestConfig) => Promise<import("./http/module/message").IShareMessagesResponse>;
3370
+ stopChat: (sessionCode: string, config?: import("./http").IRequestConfig) => Promise<void>;
3363
3371
  };
3364
3372
  fetchClient: import("./http").FetchClient;
3365
3373
  };
@@ -1896,6 +1896,9 @@ export declare const useSession: (mediator: IMediatorModule) => {
1896
1896
  }>;
1897
1897
  getSessionFeedbackReasons: (rate: number) => Promise<string[]>;
1898
1898
  renameSession: (sessionCode: string) => Promise<void>;
1899
+ uploadFile: (sessionCode: string, file: File) => Promise<{
1900
+ download_url?: string;
1901
+ }>;
1899
1902
  reset: () => void;
1900
1903
  };
1901
1904
  export type ISessionModule = ReturnType<typeof useSession>;
@@ -122,8 +122,9 @@ import { ref } from 'vue';
122
122
  * @param options.loadMessages - 是否加载消息列表,默认 true。新创建的会话可设为 false 跳过加载
123
123
  */ const chooseSession = function() {
124
124
  var _ref = _async_to_generator(function*(sessionCode, options) {
125
- var _mediator_agent;
126
- yield (_mediator_agent = mediator.agent) === null || _mediator_agent === void 0 ? void 0 : _mediator_agent.stopChat();
125
+ var // 中止当前聊天
126
+ _mediator_agent;
127
+ (_mediator_agent = mediator.agent) === null || _mediator_agent === void 0 ? void 0 : _mediator_agent.abortChat();
127
128
  var _list_value_find;
128
129
  // 选择会话
129
130
  current.value = (_list_value_find = list.value.find((item)=>item.sessionCode === sessionCode)) !== null && _list_value_find !== void 0 ? _list_value_find : null;
@@ -234,6 +235,11 @@ import { ref } from 'vue';
234
235
  isRenameLoading.value = false;
235
236
  });
236
237
  };
238
+ // 上传文件到会话
239
+ const uploadFile = (sessionCode, file)=>{
240
+ var _mediator_http;
241
+ return (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.session.uploadFile(sessionCode, file);
242
+ };
237
243
  /**
238
244
  * 重置 session 模块状态
239
245
  */ const reset = ()=>{
@@ -263,6 +269,7 @@ import { ref } from 'vue';
263
269
  postSessionFeedback,
264
270
  getSessionFeedbackReasons,
265
271
  renameSession,
272
+ uploadFile,
266
273
  reset
267
274
  };
268
275
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blueking/chat-helper",
3
- "version": "0.0.1-beta.10",
3
+ "version": "0.0.1-beta.12",
4
4
  "description": "",
5
5
  "main": "./dist/index.ts.js",
6
6
  "types": "./dist/index.d.ts",