@ibiz-template/vue3-components 0.7.41-alpha.166 → 0.7.41-alpha.168

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.
@@ -4,7 +4,7 @@
4
4
  /* eslint-disable no-prototype-builtins */
5
5
  /* eslint-disable no-unused-vars */
6
6
  /* eslint-disable @typescript-eslint/no-unused-vars */
7
- import { h } from 'vue';
7
+ import { App, createVNode, h, render } from 'vue';
8
8
  import { AxiosProgressEvent } from 'axios';
9
9
  import { createUUID } from 'qx-util';
10
10
  import {
@@ -13,7 +13,9 @@ import {
13
13
  IAppDEUIActionGroupDetail,
14
14
  } from '@ibiz/model-core';
15
15
  import {
16
+ Modal,
16
17
  IModal,
18
+ ViewMode,
17
19
  IModalData,
18
20
  UtilService,
19
21
  UIActionUtil,
@@ -36,9 +38,12 @@ import {
36
38
  IBizContext,
37
39
  IChatMessage,
38
40
  IPortalAsyncAction,
41
+ IPortalMessage,
39
42
  } from '@ibiz-template/core';
43
+ import { IBizViewShell } from '@ibiz-template/vue3-util';
40
44
  import { isNotNil } from 'ramda';
41
45
  import { AIFeedback } from './ai-feedback/ai-feedback';
46
+ import { openAsyncActionModal } from '../../panel-component/user-message/async-action/async-action/async-action.provider';
42
47
 
43
48
  export class AIChatUtil implements IApiAIChatUtil {
44
49
  private TOPIC_CHAT_PREFIX: string = 'topic';
@@ -56,11 +61,30 @@ export class AIChatUtil implements IApiAIChatUtil {
56
61
  private NONE_ERROR_MESSAGE: string =
57
62
  'Expected content-type to be text/event-stream, Actual: null';
58
63
 
59
- // 网络中断自动续传的最大重试次数
60
- private AUTO_RESUME_MAX_RETRY: number = 3;
64
+ // 各 asyncActionID 对应的回调集合(同一 ID 可能被多处注册不同回调),回调入参为最新异步作业对象
65
+ private asyncActionCallBackMap: Map<
66
+ string,
67
+ Set<(action: IPortalAsyncAction) => void>
68
+ > = new Map();
61
69
 
62
- // 网络中断自动续传的退避基数(毫秒),按 1s/2s/4s 指数退避
63
- private AUTO_RESUME_BACKOFF_BASE: number = 1000;
70
+ /**
71
+ * Creates an instance of AIChatUtil.
72
+ * @param {App} App vue App
73
+ * @memberof AIChatUtil
74
+ */
75
+ constructor(private App: App) {
76
+ // 全局监听器仅注册一次,按 asyncacitonid 分发到对应回调集合
77
+ ibiz.mc.command.asyncAction.on((msg: IPortalMessage) => {
78
+ if (!msg.data || msg.subtype !== 'ASYNCACTION') return;
79
+ const action = msg.data as IPortalAsyncAction;
80
+ // 异步交谈补全操作拦截,不做分发(与全局异步作业监听的处理保持一致)
81
+ if (action.actiontype === 'ASYNCCHATCOMPLETION') return;
82
+ const callbacks = this.asyncActionCallBackMap.get(action.asyncacitonid);
83
+ if (callbacks) {
84
+ callbacks.forEach(cb => cb(action));
85
+ }
86
+ });
87
+ }
64
88
 
65
89
  /**
66
90
  * @description 创建 AI 聊天 SSE 流式消息处理回调(提问与恢复会话共用)
@@ -146,7 +170,7 @@ export class AIChatUtil implements IApiAIChatUtil {
146
170
  }
147
171
 
148
172
  /**
149
- * @description 网络中断(network error)后按 asyncacitonid 自动续传,指数退避重试;成功返回 true,不可恢复/重试耗尽返回 false 交回调用方走既有错误处理
173
+ * @description 网络中断(network error)后按 asyncacitonid 自动续传一次;成功返回 true,失败返回 false 交回调用方走既有错误处理
150
174
  * @param {any} aiChat 单实例聊天总控
151
175
  * @param {IContext} ctx 上下文
152
176
  * @param {IParams} param 视图参数
@@ -173,55 +197,43 @@ export class AIChatUtil implements IApiAIChatUtil {
173
197
  ): Promise<boolean> {
174
198
  // 续传前先释放旧 SSE 连接,避免旧连接未关闭影响新连接建立
175
199
  state.abortController?.abort();
176
- // 全程复用同一个新 AbortController 并存回 state:退避等待期间用户点停止时,
177
- // abortQuestion abort 的即是该实例,下一轮请求携带已中止信号会立即抛 AbortError 退出循环
200
+ // 新建 AbortController 并存回 state:用户点停止时,abortQuestion abort 的即是该实例,
201
+ // 请求携带已中止信号会立即抛 AbortError 结束续传
178
202
  state.abortController = new AbortController();
179
- for (let attempt = 1; attempt <= this.AUTO_RESUME_MAX_RETRY; attempt++) {
180
- try {
181
- // 指数退避后重试
182
- // eslint-disable-next-line no-await-in-loop
183
- await new Promise<void>(resolve => {
184
- setTimeout(
185
- resolve,
186
- this.AUTO_RESUME_BACKOFF_BASE * 2 ** (attempt - 1),
187
- );
188
- });
189
- const resumeRequestData: IData = {
190
- asyncacitonid: resumeInfo.asyncacitonid,
191
- };
192
- if (resumeInfo.sessionid) {
193
- resumeRequestData.sessionid = resumeInfo.sessionid;
194
- }
195
- if (resumeInfo.srfaiagent) {
196
- resumeRequestData.srfaiagent = resumeInfo.srfaiagent;
197
- }
198
- if (resumeInfo.srfproject) {
199
- resumeRequestData.srfproject = resumeInfo.srfproject;
200
- }
201
- // eslint-disable-next-line no-await-in-loop
202
- await deService.aiResumeSse(
203
- onMsg,
204
- state.abortController,
205
- ctx,
206
- { srfactag: param.srfactag },
207
- { ...resumeRequestData },
208
- );
209
- return true;
210
- } catch (resumeError) {
211
- const resumeMsg = (resumeError as Error)?.message || '';
212
- // 用户已点停止,不再续传
213
- if ((resumeError as Error)?.name === 'AbortError') {
214
- return false;
215
- }
216
- // 后端异步动作已完成,无可恢复内容
217
- if (resumeMsg === this.NONE_ERROR_MESSAGE) {
218
- aiChat.setActiveAsyncacitonId('');
219
- return false;
220
- }
221
- // 其余(网络仍断等)继续下一轮退避重试
203
+ try {
204
+ const resumeRequestData: IData = {
205
+ asyncacitonid: resumeInfo.asyncacitonid,
206
+ };
207
+ if (resumeInfo.sessionid) {
208
+ resumeRequestData.sessionid = resumeInfo.sessionid;
222
209
  }
210
+ if (resumeInfo.srfaiagent) {
211
+ resumeRequestData.srfaiagent = resumeInfo.srfaiagent;
212
+ }
213
+ if (resumeInfo.srfproject) {
214
+ resumeRequestData.srfproject = resumeInfo.srfproject;
215
+ }
216
+ await deService.aiResumeSse(
217
+ onMsg,
218
+ state.abortController,
219
+ ctx,
220
+ { srfactag: param.srfactag },
221
+ { ...resumeRequestData },
222
+ );
223
+ return true;
224
+ } catch (resumeError) {
225
+ // 用户已点停止,不再续传
226
+ if ((resumeError as Error)?.name === 'AbortError') {
227
+ return false;
228
+ }
229
+ // 续传失败(网络仍断、后端异步动作已完成等),中止续传连接后交回调用方走既有错误处理
230
+ state.abortController?.abort();
231
+ // 后端异步动作已完成,无可恢复内容,清空异步消息任务
232
+ if ((resumeError as Error)?.message === this.NONE_ERROR_MESSAGE) {
233
+ aiChat.setActiveAsyncacitonId('');
234
+ }
235
+ return false;
223
236
  }
224
- return false;
225
237
  }
226
238
 
227
239
  /**
@@ -498,6 +510,9 @@ export class AIChatUtil implements IApiAIChatUtil {
498
510
  projectSelectPlaceholder:
499
511
  editorParams.srfprojectselectplaceholder ||
500
512
  ibiz.config.common.aiProjectSelectPlaceholder,
513
+ knowledgeBaseView:
514
+ editorParams.srfaiknowledgebaseview ||
515
+ ibiz.config.common.aiKnowledgeBaseView,
501
516
  selectAIProjectId: editorParams.srfselectaiprojectid,
502
517
  };
503
518
  // 编辑器参数srfaiappendcurdata,是否传入对象参数,用于历史查询传参
@@ -659,6 +674,8 @@ export class AIChatUtil implements IApiAIChatUtil {
659
674
  editorParams,
660
675
  );
661
676
  };
677
+ // 打开视图
678
+ chatOptions.openEmbedView = this.openEmbedView.bind(this);
662
679
  // 知识库
663
680
  chatOptions.fetchKnowledgeBaseList = (args: IParams = {}) => {
664
681
  return this.getKnowledgeBaseList(context, params, editorParams, args);
@@ -705,6 +722,9 @@ export class AIChatUtil implements IApiAIChatUtil {
705
722
  await this.getAIFileSystemOptions(filePreviewMode);
706
723
  chatOptions.fileSystemOptions = fileSystemOptions;
707
724
 
725
+ // 异步任务参数
726
+ chatOptions.asyncActionOptions = await this.getAIAsyncActionOptions();
727
+
708
728
  // 请求历史
709
729
  chatOptions.history = async (
710
730
  ctx: IContext,
@@ -1451,8 +1471,12 @@ export class AIChatUtil implements IApiAIChatUtil {
1451
1471
  projectSelectPlaceholder:
1452
1472
  params.srfprojectselectplaceholder ||
1453
1473
  ibiz.config.common.aiProjectSelectPlaceholder,
1474
+ knowledgeBaseView:
1475
+ params.srfaiknowledgebaseview || ibiz.config.common.aiKnowledgeBaseView,
1454
1476
  selectAIProjectId: params.srfselectaiprojectid,
1455
1477
  };
1478
+ if (params.hasOwnProperty('srfaiknowledgebaseview'))
1479
+ delete params.srfaiknowledgebaseview;
1456
1480
  if (params.hasOwnProperty('srfselectaiprojectid'))
1457
1481
  delete params.srfselectaiprojectid;
1458
1482
  if (params.hasOwnProperty('srfenableaiprojectselect'))
@@ -1631,7 +1655,8 @@ export class AIChatUtil implements IApiAIChatUtil {
1631
1655
  } else {
1632
1656
  chatOptions.enableWorkSpace = ibiz.config.common.aiEnableWorkSpace;
1633
1657
  }
1634
-
1658
+ // 打开视图
1659
+ chatOptions.openEmbedView = this.openEmbedView.bind(this);
1635
1660
  // 智能体选项(由内部获取,优化打开速度)
1636
1661
  chatOptions.fetchAgentList = (args: IParams = {}) => {
1637
1662
  const { query, agentid } = args;
@@ -1685,6 +1710,9 @@ export class AIChatUtil implements IApiAIChatUtil {
1685
1710
  await this.getAIFileSystemOptions(filePreviewMode);
1686
1711
  chatOptions.fileSystemOptions = fileSystemOptions;
1687
1712
 
1713
+ // 异步任务参数
1714
+ chatOptions.asyncActionOptions = await this.getAIAsyncActionOptions();
1715
+
1688
1716
  // 查询历史
1689
1717
  chatOptions.history = async (
1690
1718
  ctx: IContext,
@@ -2901,4 +2929,107 @@ export class AIChatUtil implements IApiAIChatUtil {
2901
2929
  };
2902
2930
  return fileSystemOptions;
2903
2931
  }
2932
+
2933
+ /**
2934
+ * @description 打开嵌入视图
2935
+ * @private
2936
+ * @param {IContext} context 上下文
2937
+ * @param {IParams} params 视图参数
2938
+ * @param {string} viewId 视图标识
2939
+ * @param {HTMLElement} container 视图挂载容器
2940
+ * @param {IData[]} [selectedData] 选中数据
2941
+ * @param {(result: IModalData) => void} [callback] 回调
2942
+ * @returns {*} {Promise<void>}
2943
+ * @memberof AIChatUtil
2944
+ */
2945
+ private async openEmbedView(
2946
+ context: IContext,
2947
+ params: IParams,
2948
+ viewId: string,
2949
+ container: HTMLElement,
2950
+ selectedData: IData[],
2951
+ callback?: (result: IModalData) => void,
2952
+ ): Promise<{
2953
+ width: number | undefined;
2954
+ height: number | undefined;
2955
+ unmount: (result?: IModalData) => void;
2956
+ }> {
2957
+ // 卸载:只卸载内容,不负责移除 container
2958
+ const unmount = (result: IModalData = { ok: false }) => {
2959
+ callback?.(result);
2960
+ render(null, container);
2961
+ };
2962
+ // 1. 绘制视图
2963
+ const modal = new Modal({
2964
+ mode: ViewMode.EMBED,
2965
+ viewUsage: 2,
2966
+ dismiss: result => unmount(result),
2967
+ });
2968
+ const modelData = await ibiz.hub.getAppView(viewId);
2969
+ const vNode = createVNode(IBizViewShell, {
2970
+ modal,
2971
+ modelData,
2972
+ context,
2973
+ params: { ...params, checkstrictly: true },
2974
+ state: { selectedData },
2975
+ });
2976
+ // 2. 设置应用上下文
2977
+ vNode.appContext = this.App._context;
2978
+ // 3. 挂载视图
2979
+ render(vNode, container);
2980
+ // 4. 返回卸载方式和视图大小,AI内部关闭视图时必须调用卸载方法
2981
+ return {
2982
+ width: modelData.width,
2983
+ height: modelData.height,
2984
+ unmount,
2985
+ };
2986
+ }
2987
+
2988
+ /**
2989
+ * @description 获取异步行为配置
2990
+ * @returns {*} {Promise<IData>}
2991
+ * @memberof AIChatUtil
2992
+ */
2993
+ async getAIAsyncActionOptions(): Promise<IData> {
2994
+ const asyncActionOptions: IData = {};
2995
+ // 获取最后一次执行异步行为对象
2996
+ asyncActionOptions.getLastAsyncAction = async (
2997
+ asyncActionID: string,
2998
+ ): Promise<IPortalAsyncAction | null> => {
2999
+ const result =
3000
+ await ibiz.hub.notice.asyncAction.service.get(asyncActionID);
3001
+ if (result.ok && result.data) {
3002
+ return result.data;
3003
+ }
3004
+ return null;
3005
+ };
3006
+ // 注册回调,返回销毁函数
3007
+ asyncActionOptions.registerCallBack = (
3008
+ asyncActionID: string,
3009
+ callback: (action: IPortalAsyncAction) => void,
3010
+ ) => {
3011
+ let callbacks = this.asyncActionCallBackMap.get(asyncActionID);
3012
+ if (!callbacks) {
3013
+ callbacks = new Set();
3014
+ this.asyncActionCallBackMap.set(asyncActionID, callbacks);
3015
+ }
3016
+ // 同一回调重复注册时幂等跳过
3017
+ callbacks.add(callback);
3018
+ // 返回取消订阅函数,供外部在不再需要时清理
3019
+ return () => {
3020
+ callbacks!.delete(callback);
3021
+ if (!callbacks!.size) {
3022
+ this.asyncActionCallBackMap.delete(asyncActionID);
3023
+ }
3024
+ };
3025
+ };
3026
+ // 预览异步行为
3027
+ asyncActionOptions.onPreview = async (
3028
+ asyncAction: IPortalAsyncAction,
3029
+ _event: MouseEvent,
3030
+ ): Promise<boolean> => {
3031
+ return openAsyncActionModal(asyncAction);
3032
+ };
3033
+ return asyncActionOptions;
3034
+ }
2904
3035
  }
@@ -133,7 +133,7 @@ export async function runApp(
133
133
  ibiz.overlay = new OverlayController();
134
134
  ibiz.inLineAIUtil = new InLineAIUtil();
135
135
  ibiz.screenShotUtil = new ScreenShotUtil();
136
- ibiz.aiChatUtil = new AIChatUtil();
136
+ ibiz.aiChatUtil = new AIChatUtil(app);
137
137
  ibiz.util.text.format = (value, code): string => {
138
138
  return app.config.globalProperties.$textFormat(value, code);
139
139
  };