@ibiz-template/vue3-components 0.7.41-alpha.165 → 0.7.41-alpha.167

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 (34) hide show
  1. package/dist/{index-li-EqXGB.js → index-DXoEi5m-.js} +28 -28
  2. package/dist/{index.es-CA0rGKdK.js → index.es-nZgG-TGm.js} +1 -1
  3. package/dist/index.min.css +1 -1
  4. package/dist/index.system.min.js +1 -1
  5. package/dist/{wang-editor-BPGsMZgX.js → wang-editor-CK60NH54.js} +1 -1
  6. package/dist/{xlsx-util-Bv9fDQB8.js → xlsx-util-5Xgq-UOe.js} +1 -1
  7. package/es/editor/index.mjs +8 -0
  8. package/es/editor/work-space/embed-work-space/embed-work-space.css +1 -0
  9. package/es/editor/work-space/embed-work-space/embed-work-space.mjs +75 -0
  10. package/es/editor/work-space/embed-work-space.controller.mjs +62 -0
  11. package/es/editor/work-space/embed-work-space.provider.mjs +22 -0
  12. package/es/editor/work-space/index.mjs +5 -0
  13. package/es/panel-component/user-message/async-action/async-action/async-action.provider.mjs +44 -41
  14. package/es/util/ai-chat-util/ai-chat-util.mjs +326 -244
  15. package/es/web-app/main.mjs +1 -1
  16. package/lib/editor/index.cjs +8 -0
  17. package/lib/editor/work-space/embed-work-space/embed-work-space.cjs +77 -0
  18. package/lib/editor/work-space/embed-work-space/embed-work-space.css +1 -0
  19. package/lib/editor/work-space/embed-work-space.controller.cjs +64 -0
  20. package/lib/editor/work-space/embed-work-space.provider.cjs +24 -0
  21. package/lib/editor/work-space/index.cjs +11 -0
  22. package/lib/panel-component/user-message/async-action/async-action/async-action.provider.cjs +44 -40
  23. package/lib/util/ai-chat-util/ai-chat-util.cjs +324 -242
  24. package/lib/web-app/main.cjs +1 -1
  25. package/package.json +5 -5
  26. package/src/editor/index.ts +7 -1
  27. package/src/editor/work-space/embed-work-space/embed-work-space.scss +8 -0
  28. package/src/editor/work-space/embed-work-space/embed-work-space.tsx +78 -0
  29. package/src/editor/work-space/embed-work-space.controller.ts +70 -0
  30. package/src/editor/work-space/embed-work-space.provider.ts +28 -0
  31. package/src/editor/work-space/index.ts +3 -0
  32. package/src/panel-component/user-message/async-action/async-action/async-action.provider.ts +58 -42
  33. package/src/util/ai-chat-util/ai-chat-util.ts +418 -279
  34. package/src/web-app/main.ts +1 -1
@@ -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,6 +61,181 @@ 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
 
64
+ // 各 asyncActionID 对应的回调集合(同一 ID 可能被多处注册不同回调),回调入参为最新异步作业对象
65
+ private asyncActionCallBackMap: Map<
66
+ string,
67
+ Set<(action: IPortalAsyncAction) => void>
68
+ > = new Map();
69
+
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
+ }
88
+
89
+ /**
90
+ * @description 创建 AI 聊天 SSE 流式消息处理回调(提问与恢复会话共用)
91
+ * @param {any} aiChat 单实例聊天总控
92
+ * @param {string} id 本次问答的流式消息 id
93
+ * @param {string} localMsgID 本地用户消息 id(用于回填真实消息 id;为空串时为恢复会话场景,状态 20 走 isLoading 加载分支)
94
+ * @return {*} {(msg: IPortalAsyncAction) => void}
95
+ * @memberof AIChatUtil
96
+ */
97
+ private createChatSseHandler(
98
+ aiChat: any,
99
+ id: string,
100
+ localMsgID: string,
101
+ ): (msg: IPortalAsyncAction) => void {
102
+ // 更新标记,主要用于更新用户消息真实消息ID(每条消息都会回asextparam,只需要设置一次即可)
103
+ let updateFlag = true;
104
+ return (msg: IPortalAsyncAction) => {
105
+ // 20: 持续回答中,消息会持续推送。同一个消息 id 会显示在同一个框内
106
+ if (msg.actionstate === 20 && msg.actionresult) {
107
+ if (localMsgID) {
108
+ // 提问场景:移除等待回复消息
109
+ aiChat.removeWaitingMessage(id);
110
+ // 更新用户消息真实id
111
+ if (msg.asextparam && updateFlag) {
112
+ aiChat.updateMsgRealMessageID(localMsgID, msg.asextparam);
113
+ updateFlag = false;
114
+ }
115
+ } else {
116
+ // 恢复会话场景:设置当前聊天在加载中
117
+ aiChat.isLoading.value = true;
118
+ }
119
+ // 不存在才设置,存在则不设置(下方设置的时候需要去存储话题数据中)
120
+ if (!aiChat.getActiveAsyncacitonId()) {
121
+ aiChat.setActiveAsyncacitonId(msg.asyncacitonid);
122
+ }
123
+ aiChat.addMessage({
124
+ messageid: id,
125
+ state: msg.actionstate,
126
+ type: 'DEFAULT',
127
+ role: 'ASSISTANT',
128
+ content: msg.actionresult as string,
129
+ status: 'pending',
130
+ });
131
+ }
132
+ // 30: 回答完成,包含具体所有消息内容。直接覆盖之前的临时拼接消息
133
+ else if (msg.actionstate === 30 && msg.actionresult) {
134
+ const result = JSON.parse(msg.actionresult as string);
135
+ if (result.usage) {
136
+ aiChat.updateMsgUsage(id, result.usage);
137
+ }
138
+ if (result.files) {
139
+ aiChat.updateMsgOutputFiles(id, result.files);
140
+ }
141
+ const choices = result.choices;
142
+ if (choices && choices.length > 0) {
143
+ aiChat.replaceMessage({
144
+ messageid: id,
145
+ state: msg.actionstate,
146
+ type: 'DEFAULT',
147
+ role: 'ASSISTANT',
148
+ content: choices[0].content || '',
149
+ realmessageid: choices[0].messageid,
150
+ status: 'sent',
151
+ });
152
+ }
153
+ // 清空异步消息任务
154
+ aiChat.setActiveAsyncacitonId('');
155
+ }
156
+ // 40: 回答报错,展示错误信息
157
+ else if (msg.actionstate === 40) {
158
+ aiChat.replaceMessage({
159
+ messageid: id,
160
+ state: msg.actionstate,
161
+ type: 'ERROR',
162
+ role: 'ASSISTANT',
163
+ errorText: msg.actionresult as string,
164
+ status: 'failed',
165
+ });
166
+ // 清空异步消息任务
167
+ aiChat.setActiveAsyncacitonId('');
168
+ }
169
+ };
170
+ }
171
+
172
+ /**
173
+ * @description 网络中断(network error)后按 asyncacitonid 自动续传一次;成功返回 true,失败返回 false 交回调用方走既有错误处理
174
+ * @param {any} aiChat 单实例聊天总控
175
+ * @param {IContext} ctx 上下文
176
+ * @param {IParams} param 视图参数
177
+ * @param {*} deService 实体服务
178
+ * @param {{ id: string; abortController: AbortController }} state 聊天实例提问状态(WeakMap 持有的同一对象引用)
179
+ * @param {{ asyncacitonid: string; sessionid?: string; srfaiagent?: string; srfproject?: string }} resumeInfo 续传参数
180
+ * @param {(msg: IPortalAsyncAction) => void} onMsg SSE 流式消息处理回调
181
+ * @return {*} {Promise<boolean>} 是否续传成功
182
+ * @memberof AIChatUtil
183
+ */
184
+ private async autoResumeChat(
185
+ aiChat: any,
186
+ ctx: IContext,
187
+ param: IParams,
188
+ deService: any,
189
+ state: { id: string; abortController: AbortController },
190
+ resumeInfo: {
191
+ asyncacitonid: string;
192
+ sessionid?: string;
193
+ srfaiagent?: string;
194
+ srfproject?: string;
195
+ },
196
+ onMsg: (msg: IPortalAsyncAction) => void,
197
+ ): Promise<boolean> {
198
+ // 续传前先释放旧 SSE 连接,避免旧连接未关闭影响新连接建立
199
+ state.abortController?.abort();
200
+ // 新建 AbortController 并存回 state:用户点停止时,abortQuestion abort 的即是该实例,
201
+ // 请求携带已中止信号会立即抛 AbortError 结束续传
202
+ state.abortController = new AbortController();
203
+ try {
204
+ const resumeRequestData: IData = {
205
+ asyncacitonid: resumeInfo.asyncacitonid,
206
+ };
207
+ if (resumeInfo.sessionid) {
208
+ resumeRequestData.sessionid = resumeInfo.sessionid;
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;
236
+ }
237
+ }
238
+
59
239
  /**
60
240
  * 获取AI聊天对象
61
241
  */
@@ -330,6 +510,10 @@ export class AIChatUtil implements IApiAIChatUtil {
330
510
  projectSelectPlaceholder:
331
511
  editorParams.srfprojectselectplaceholder ||
332
512
  ibiz.config.common.aiProjectSelectPlaceholder,
513
+ knowledgeBaseView:
514
+ editorParams.srfaiknowledgebaseview ||
515
+ ibiz.config.common.aiKnowledgeBaseView,
516
+ selectAIProjectId: editorParams.srfselectaiprojectid,
333
517
  };
334
518
  // 编辑器参数srfaiappendcurdata,是否传入对象参数,用于历史查询传参
335
519
  if (editorParams.srfaiappendcurdata) {
@@ -490,6 +674,24 @@ export class AIChatUtil implements IApiAIChatUtil {
490
674
  editorParams,
491
675
  );
492
676
  };
677
+ // 打开视图
678
+ chatOptions.openEmbedView = async (
679
+ ctx: IContext,
680
+ param: IParams,
681
+ viewId: string,
682
+ container: HTMLElement,
683
+ selectedData: IData[],
684
+ callback?: (result: IModalData) => void,
685
+ ) => {
686
+ await this.openEmbedView(
687
+ ctx,
688
+ param,
689
+ viewId,
690
+ container,
691
+ selectedData,
692
+ callback,
693
+ );
694
+ };
493
695
  // 知识库
494
696
  chatOptions.fetchKnowledgeBaseList = (args: IParams = {}) => {
495
697
  return this.getKnowledgeBaseList(context, params, editorParams, args);
@@ -536,6 +738,9 @@ export class AIChatUtil implements IApiAIChatUtil {
536
738
  await this.getAIFileSystemOptions(filePreviewMode);
537
739
  chatOptions.fileSystemOptions = fileSystemOptions;
538
740
 
741
+ // 异步任务参数
742
+ chatOptions.asyncActionOptions = await this.getAIAsyncActionOptions();
743
+
539
744
  // 请求历史
540
745
  chatOptions.history = async (
541
746
  ctx: IContext,
@@ -621,8 +826,6 @@ export class AIChatUtil implements IApiAIChatUtil {
621
826
  srfproject: string | undefined,
622
827
  localMsgID: string,
623
828
  ) => {
624
- // 更新标记,主要用于更新用户消息真实消息ID(每条消息都会回asextparam,只需要设置一次即可)
625
- let updateFlag = true;
626
829
  const state = getChatState(aiChat);
627
830
  state.id = createUUID();
628
831
  state.abortController = new AbortController();
@@ -630,27 +833,28 @@ export class AIChatUtil implements IApiAIChatUtil {
630
833
  const deService = await ibiz.hub
631
834
  .getApp(ctx.srfappid)
632
835
  .deService.getService(ctx, other.appDataEntityId);
836
+ // SSE 流式消息处理回调(question 与网络中断自动续传共用)
837
+ const onMsg = this.createChatSseHandler(aiChat, id, localMsgID);
633
838
  try {
634
- const questionRequestData: IData = {
839
+ const requestData: IData = {
635
840
  messages: arr,
636
841
  sessionid,
637
842
  };
638
- if (srfaiagent) questionRequestData.srfaiagent = srfaiagent;
639
- if (srfmode) questionRequestData.mode = srfmode;
640
- if (srfscope) questionRequestData.srfscope = srfscope;
641
- if (srfproject) questionRequestData.srfproject = srfproject;
642
- if (srfknowledgebases)
643
- questionRequestData.knowledgebases = srfknowledgebases;
843
+ if (srfaiagent) requestData.srfaiagent = srfaiagent;
844
+ if (srfmode) requestData.mode = srfmode;
845
+ if (srfscope) requestData.srfscope = srfscope;
846
+ if (srfproject) requestData.srfproject = srfproject;
847
+ if (srfknowledgebases) requestData.knowledgebases = srfknowledgebases;
644
848
  if (srfaiagentconfig) {
645
- Object.assign(questionRequestData, srfaiagentconfig);
849
+ Object.assign(requestData, srfaiagentconfig);
646
850
  }
647
851
  if (mcpservers) {
648
- Object.assign(questionRequestData, {
852
+ Object.assign(requestData, {
649
853
  mcpservers,
650
854
  });
651
855
  }
652
856
  if (sessionmeta) {
653
- Object.assign(questionRequestData, {
857
+ Object.assign(requestData, {
654
858
  srfuimeta: JSON.stringify(sessionmeta),
655
859
  });
656
860
  }
@@ -663,79 +867,34 @@ export class AIChatUtil implements IApiAIChatUtil {
663
867
  Object.assign(extParams, { ...appendcurdata });
664
868
  }
665
869
  if (extParams && Object.keys(extParams).length > 0) {
666
- Object.assign(questionRequestData, { srfextparams: extParams });
870
+ Object.assign(requestData, { srfextparams: extParams });
667
871
  }
668
872
  // 新增等待回复消息(AI首条内容到达前显示loading气泡)
669
873
  aiChat.addWaitingMessage(id);
670
874
  await deService.aiChatSse(
671
- (msg: IPortalAsyncAction) => {
672
- // 20: 持续回答中,消息会持续推送。同一个消息 id 会显示在同一个框内
673
- if (msg.actionstate === 20 && msg.actionresult) {
674
- // 移除等待回复消息
675
- aiChat.removeWaitingMessage(id);
676
- // 更新用户消息真实id
677
- if (msg.asextparam && localMsgID && updateFlag) {
678
- aiChat.updateMsgRealMessageID(localMsgID, msg.asextparam);
679
- updateFlag = false;
680
- }
681
- // 不存在才设置,存在则不设置(下方设置的时候需要去存储话题数据中)
682
- if (!aiChat.getActiveAsyncacitonId()) {
683
- aiChat.setActiveAsyncacitonId(msg.asyncacitonid);
684
- }
685
- aiChat.addMessage({
686
- messageid: id,
687
- state: msg.actionstate,
688
- type: 'DEFAULT',
689
- role: 'ASSISTANT',
690
- content: msg.actionresult as string,
691
- status: 'pending',
692
- });
693
- }
694
- // 30: 回答完成,包含具体所有消息内容。直接覆盖之前的临时拼接消息
695
- else if (msg.actionstate === 30 && msg.actionresult) {
696
- const result = JSON.parse(msg.actionresult as string);
697
- if (result.usage) {
698
- aiChat.updateMsgUsage(id, result.usage);
699
- }
700
- if (result.files) {
701
- aiChat.updateMsgOutputFiles(id, result.files);
702
- }
703
- const choices = result.choices;
704
- if (choices && choices.length > 0) {
705
- aiChat.replaceMessage({
706
- messageid: id,
707
- state: msg.actionstate,
708
- type: 'DEFAULT',
709
- role: 'ASSISTANT',
710
- content: choices[0].content || '',
711
- realmessageid: choices[0].messageid,
712
- status: 'sent',
713
- });
714
- }
715
- // 清空异步消息任务
716
- aiChat.setActiveAsyncacitonId('');
717
- }
718
- // 40: 回答报错,展示错误信息
719
- else if (msg.actionstate === 40) {
720
- aiChat.replaceMessage({
721
- messageid: id,
722
- state: msg.actionstate,
723
- type: 'ERROR',
724
- role: 'ASSISTANT',
725
- errorText: msg.actionresult as string,
726
- status: 'failed',
727
- });
728
- // 清空异步消息任务
729
- aiChat.setActiveAsyncacitonId('');
730
- }
731
- },
875
+ onMsg,
732
876
  abortController,
733
877
  ctx,
734
878
  { srfactag: param.srfactag },
735
- { ...questionRequestData },
879
+ { ...requestData },
736
880
  );
737
881
  } catch (error) {
738
- if (error && (error as Error).message !== this.PRE_ERROR_MESSAGE) {
882
+ const errMsg = (error as Error)?.message || '';
883
+ const asyncacitonid = aiChat.getActiveAsyncacitonId();
884
+ // 仅网络中断且后端异步动作仍在运行(asyncid 未清)时自动续传
885
+ if (errMsg === 'network error' && asyncacitonid) {
886
+ const resumed = await this.autoResumeChat(
887
+ aiChat,
888
+ ctx,
889
+ param,
890
+ deService,
891
+ state,
892
+ { asyncacitonid, sessionid, srfaiagent, srfproject },
893
+ onMsg,
894
+ );
895
+ if (resumed) return true;
896
+ }
897
+ if (error && errMsg !== this.PRE_ERROR_MESSAGE) {
739
898
  aiChat.replaceMessage({
740
899
  messageid: id,
741
900
  state: 40,
@@ -750,6 +909,7 @@ export class AIChatUtil implements IApiAIChatUtil {
750
909
  {
751
910
  ...lastMessage,
752
911
  state: 40,
912
+ errorText: ibiz.i18n.t('app.aiError'),
753
913
  },
754
914
  false,
755
915
  );
@@ -822,87 +982,35 @@ export class AIChatUtil implements IApiAIChatUtil {
822
982
  const deService = await ibiz.hub
823
983
  .getApp(ctx.srfappid)
824
984
  .deService.getService(ctx, other.appDataEntityId);
825
- const resumeRequestData: IData = { asyncacitonid: other.asyncacitonid };
985
+ const requestData: IData = { asyncacitonid: other.asyncacitonid };
826
986
  if (other.sessionid) {
827
- Object.assign(resumeRequestData, {
987
+ Object.assign(requestData, {
828
988
  sessionid: other.sessionid,
829
989
  });
830
990
  }
831
991
  if (other.srfaiagent) {
832
- Object.assign(resumeRequestData, {
992
+ Object.assign(requestData, {
833
993
  srfaiagent: other.srfaiagent,
834
994
  });
835
995
  }
836
996
  if (other.srfproject)
837
- Object.assign(resumeRequestData, {
997
+ Object.assign(requestData, {
838
998
  srfproject: other.srfproject,
839
999
  });
1000
+ // 恢复会话流式消息处理回调(与提问共用,localMsgID 传空串走 isLoading 分支)
1001
+ const onMsg = this.createChatSseHandler(aiChat, id, '');
840
1002
  try {
841
1003
  await deService.aiResumeSse(
842
- (msg: IPortalAsyncAction) => {
843
- // 20: 持续回答中,消息会持续推送。同一个消息 id 会显示在同一个框内
844
- if (msg.actionstate === 20 && msg.actionresult) {
845
- // 设置当前聊天在加载中
846
- aiChat.isLoading.value = true;
847
- // 不存在才设置,存在则不设置(下方设置的时候需要去存储话题数据中)
848
- if (!aiChat.getActiveAsyncacitonId()) {
849
- aiChat.setActiveAsyncacitonId(msg.asyncacitonid);
850
- }
851
- aiChat.addMessage({
852
- messageid: id,
853
- state: msg.actionstate,
854
- type: 'DEFAULT',
855
- role: 'ASSISTANT',
856
- content: msg.actionresult as string,
857
- status: 'pending',
858
- });
859
- }
860
- // 30: 回答完成,包含具体所有消息内容。直接覆盖之前的临时拼接消息
861
- else if (msg.actionstate === 30 && msg.actionresult) {
862
- const result = JSON.parse(msg.actionresult as string);
863
- if (result.usage) {
864
- aiChat.updateMsgUsage(id, result.usage);
865
- }
866
- if (result.files) {
867
- aiChat.updateMsgOutputFiles(id, result.files);
868
- }
869
- const choices = result.choices;
870
- if (choices && choices.length > 0) {
871
- aiChat.replaceMessage({
872
- messageid: id,
873
- state: msg.actionstate,
874
- type: 'DEFAULT',
875
- role: 'ASSISTANT',
876
- content: choices[0].content || '',
877
- realmessageid: choices[0].messageid,
878
- status: 'sent',
879
- });
880
- }
881
- // 清空异步消息任务
882
- aiChat.setActiveAsyncacitonId('');
883
- }
884
- // 40: 回答报错,展示错误信息
885
- else if (msg.actionstate === 40) {
886
- aiChat.replaceMessage({
887
- messageid: id,
888
- state: msg.actionstate,
889
- type: 'ERROR',
890
- role: 'ASSISTANT',
891
- errorText: msg.actionresult as string,
892
- status: 'failed',
893
- });
894
- // 清空异步消息任务
895
- aiChat.setActiveAsyncacitonId('');
896
- }
897
- },
1004
+ onMsg,
898
1005
  abortController,
899
1006
  ctx,
900
1007
  { srfactag: param.srfactag },
901
- { ...resumeRequestData },
1008
+ { ...requestData },
902
1009
  );
903
1010
  } catch (error) {
1011
+ const errMsg = (error as Error)?.message || '';
904
1012
  // 等于NONE_ERROR_MESSAGE,代码该异步行为已经完成,不需要做处理
905
- if (error && (error as Error).message !== this.NONE_ERROR_MESSAGE) {
1013
+ if (error && errMsg !== this.NONE_ERROR_MESSAGE) {
906
1014
  if (error && (error as Error).message !== this.PRE_ERROR_MESSAGE) {
907
1015
  aiChat.replaceMessage({
908
1016
  messageid: id,
@@ -919,6 +1027,7 @@ export class AIChatUtil implements IApiAIChatUtil {
919
1027
  {
920
1028
  ...lastMessage,
921
1029
  state: 40,
1030
+ errorText: ibiz.i18n.t('app.aiError'),
922
1031
  },
923
1032
  false,
924
1033
  );
@@ -1378,7 +1487,14 @@ export class AIChatUtil implements IApiAIChatUtil {
1378
1487
  projectSelectPlaceholder:
1379
1488
  params.srfprojectselectplaceholder ||
1380
1489
  ibiz.config.common.aiProjectSelectPlaceholder,
1381
- };
1490
+ knowledgeBaseView:
1491
+ params.srfaiknowledgebaseview || ibiz.config.common.aiKnowledgeBaseView,
1492
+ selectAIProjectId: params.srfselectaiprojectid,
1493
+ };
1494
+ if (params.hasOwnProperty('srfaiknowledgebaseview'))
1495
+ delete params.srfaiknowledgebaseview;
1496
+ if (params.hasOwnProperty('srfselectaiprojectid'))
1497
+ delete params.srfselectaiprojectid;
1382
1498
  if (params.hasOwnProperty('srfenableaiprojectselect'))
1383
1499
  delete params.srfenableaiprojectselect;
1384
1500
  if (params.hasOwnProperty('srfprojectselectplaceholder'))
@@ -1555,7 +1671,24 @@ export class AIChatUtil implements IApiAIChatUtil {
1555
1671
  } else {
1556
1672
  chatOptions.enableWorkSpace = ibiz.config.common.aiEnableWorkSpace;
1557
1673
  }
1558
-
1674
+ // 打开视图
1675
+ chatOptions.openEmbedView = async (
1676
+ ctx: IContext,
1677
+ param: IParams,
1678
+ viewId: string,
1679
+ container: HTMLElement,
1680
+ selectedData: IData[],
1681
+ callback?: (result: IModalData) => void,
1682
+ ) => {
1683
+ await this.openEmbedView(
1684
+ ctx,
1685
+ param,
1686
+ viewId,
1687
+ container,
1688
+ selectedData,
1689
+ callback,
1690
+ );
1691
+ };
1559
1692
  // 智能体选项(由内部获取,优化打开速度)
1560
1693
  chatOptions.fetchAgentList = (args: IParams = {}) => {
1561
1694
  const { query, agentid } = args;
@@ -1609,6 +1742,9 @@ export class AIChatUtil implements IApiAIChatUtil {
1609
1742
  await this.getAIFileSystemOptions(filePreviewMode);
1610
1743
  chatOptions.fileSystemOptions = fileSystemOptions;
1611
1744
 
1745
+ // 异步任务参数
1746
+ chatOptions.asyncActionOptions = await this.getAIAsyncActionOptions();
1747
+
1612
1748
  // 查询历史
1613
1749
  chatOptions.history = async (
1614
1750
  ctx: IContext,
@@ -1694,8 +1830,6 @@ export class AIChatUtil implements IApiAIChatUtil {
1694
1830
  srfproject: string | undefined,
1695
1831
  localMsgID: string,
1696
1832
  ) => {
1697
- // 更新标记,主要用于更新用户消息真实消息ID(每条消息都会回asextparam,只需要设置一次即可)
1698
- let updateFlag = true;
1699
1833
  const state = getChatState(aiChat);
1700
1834
  state.id = createUUID();
1701
1835
  state.abortController = new AbortController();
@@ -1703,25 +1837,26 @@ export class AIChatUtil implements IApiAIChatUtil {
1703
1837
  const deService = await ibiz.hub
1704
1838
  .getApp(ctx.srfappid)
1705
1839
  .deService.getService(ctx, other.appDataEntityId);
1840
+ // SSE 流式消息处理回调(question 与网络中断自动续传共用)
1841
+ const onMsg = this.createChatSseHandler(aiChat, id, localMsgID);
1706
1842
  try {
1707
- const questionRequestData: IData = {
1843
+ const requestData: IData = {
1708
1844
  messages: arr,
1709
1845
  sessionid,
1710
1846
  };
1711
- if (srfaiagent) questionRequestData.srfaiagent = srfaiagent;
1712
- if (srfmode) questionRequestData.mode = srfmode;
1713
- if (srfscope) questionRequestData.srfscope = srfscope;
1714
- if (srfproject) questionRequestData.srfproject = srfproject;
1715
- if (srfknowledgebases)
1716
- questionRequestData.knowledgebases = srfknowledgebases;
1847
+ if (srfaiagent) requestData.srfaiagent = srfaiagent;
1848
+ if (srfmode) requestData.mode = srfmode;
1849
+ if (srfscope) requestData.srfscope = srfscope;
1850
+ if (srfproject) requestData.srfproject = srfproject;
1851
+ if (srfknowledgebases) requestData.knowledgebases = srfknowledgebases;
1717
1852
  if (srfaiagentconfig) {
1718
- Object.assign(questionRequestData, srfaiagentconfig);
1853
+ Object.assign(requestData, srfaiagentconfig);
1719
1854
  }
1720
1855
  if (mcpservers) {
1721
- Object.assign(questionRequestData, { mcpservers });
1856
+ Object.assign(requestData, { mcpservers });
1722
1857
  }
1723
1858
  if (sessionmeta) {
1724
- Object.assign(questionRequestData, {
1859
+ Object.assign(requestData, {
1725
1860
  srfuimeta: JSON.stringify(sessionmeta),
1726
1861
  });
1727
1862
  }
@@ -1734,79 +1869,34 @@ export class AIChatUtil implements IApiAIChatUtil {
1734
1869
  Object.assign(extParams, { ...appendcurdata });
1735
1870
  }
1736
1871
  if (extParams && Object.keys(extParams).length > 0) {
1737
- Object.assign(questionRequestData, { srfextparams: extParams });
1872
+ Object.assign(requestData, { srfextparams: extParams });
1738
1873
  }
1739
1874
  // 新增等待回复消息(AI首条内容到达前显示loading气泡)
1740
1875
  aiChat.addWaitingMessage(id);
1741
1876
  await deService.aiChatSse(
1742
- (msg: IPortalAsyncAction) => {
1743
- // 20: 持续回答中,消息会持续推送。同一个消息 id 会显示在同一个框内
1744
- if (msg.actionstate === 20 && msg.actionresult) {
1745
- // 移除等待回复消息
1746
- aiChat.removeWaitingMessage(id);
1747
- // 更新用户消息真实id
1748
- if (msg.asextparam && localMsgID && updateFlag) {
1749
- aiChat.updateMsgRealMessageID(localMsgID, msg.asextparam);
1750
- updateFlag = false;
1751
- }
1752
- // 不存在才设置,存在则不设置(下方设置的时候需要去存储话题数据中)
1753
- if (!aiChat.getActiveAsyncacitonId()) {
1754
- aiChat.setActiveAsyncacitonId(msg.asyncacitonid);
1755
- }
1756
- aiChat.addMessage({
1757
- messageid: id,
1758
- state: msg.actionstate,
1759
- type: 'DEFAULT',
1760
- role: 'ASSISTANT',
1761
- content: msg.actionresult as string,
1762
- status: 'pending',
1763
- });
1764
- }
1765
- // 30: 回答完成,包含具体所有消息内容。直接覆盖之前的临时拼接消息
1766
- else if (msg.actionstate === 30 && msg.actionresult) {
1767
- const result = JSON.parse(msg.actionresult as string);
1768
- if (result.usage) {
1769
- aiChat.updateMsgUsage(id, result.usage);
1770
- }
1771
- if (result.files) {
1772
- aiChat.updateMsgOutputFiles(id, result.files);
1773
- }
1774
- const choices = result.choices;
1775
- if (choices && choices.length > 0) {
1776
- aiChat.replaceMessage({
1777
- messageid: id,
1778
- state: msg.actionstate,
1779
- type: 'DEFAULT',
1780
- role: 'ASSISTANT',
1781
- content: choices[0].content || '',
1782
- realmessageid: choices[0].messageid,
1783
- status: 'sent',
1784
- });
1785
- }
1786
- // 清空异步消息任务
1787
- aiChat.setActiveAsyncacitonId('');
1788
- }
1789
- // 40: 回答报错,展示错误信息
1790
- else if (msg.actionstate === 40) {
1791
- aiChat.replaceMessage({
1792
- messageid: id,
1793
- state: msg.actionstate,
1794
- type: 'ERROR',
1795
- role: 'ASSISTANT',
1796
- errorText: msg.actionresult as string,
1797
- status: 'failed',
1798
- });
1799
- // 清空异步消息任务
1800
- aiChat.setActiveAsyncacitonId('');
1801
- }
1802
- },
1877
+ onMsg,
1803
1878
  abortController,
1804
1879
  ctx,
1805
1880
  { srfactag: param.srfactag },
1806
- { ...questionRequestData },
1881
+ { ...requestData },
1807
1882
  );
1808
1883
  } catch (error) {
1809
- if (error && (error as Error).message !== this.PRE_ERROR_MESSAGE) {
1884
+ const errMsg = (error as Error)?.message || '';
1885
+ const asyncacitonid = aiChat.getActiveAsyncacitonId();
1886
+ // 仅网络中断且后端异步动作仍在运行(asyncid 未清)时自动续传
1887
+ if (errMsg === 'network error' && asyncacitonid) {
1888
+ const resumed = await this.autoResumeChat(
1889
+ aiChat,
1890
+ ctx,
1891
+ param,
1892
+ deService,
1893
+ state,
1894
+ { asyncacitonid, sessionid, srfaiagent, srfproject },
1895
+ onMsg,
1896
+ );
1897
+ if (resumed) return true;
1898
+ }
1899
+ if (error && errMsg !== this.PRE_ERROR_MESSAGE) {
1810
1900
  aiChat.replaceMessage({
1811
1901
  messageid: id,
1812
1902
  state: 40,
@@ -1821,6 +1911,7 @@ export class AIChatUtil implements IApiAIChatUtil {
1821
1911
  {
1822
1912
  ...lastMessage,
1823
1913
  state: 40,
1914
+ errorText: ibiz.i18n.t('app.aiError'),
1824
1915
  },
1825
1916
  false,
1826
1917
  );
@@ -1893,87 +1984,35 @@ export class AIChatUtil implements IApiAIChatUtil {
1893
1984
  const deService = await ibiz.hub
1894
1985
  .getApp(ctx.srfappid)
1895
1986
  .deService.getService(ctx, other.appDataEntityId);
1896
- const resumeRequestData: IData = { asyncacitonid: other.asyncacitonid };
1987
+ const requestData: IData = { asyncacitonid: other.asyncacitonid };
1897
1988
  if (other.sessionid) {
1898
- Object.assign(resumeRequestData, {
1989
+ Object.assign(requestData, {
1899
1990
  sessionid: other.sessionid,
1900
1991
  });
1901
1992
  }
1902
1993
  if (other.srfaiagent) {
1903
- Object.assign(resumeRequestData, {
1994
+ Object.assign(requestData, {
1904
1995
  srfaiagent: other.srfaiagent,
1905
1996
  });
1906
1997
  }
1907
1998
  if (other.srfproject)
1908
- Object.assign(resumeRequestData, {
1999
+ Object.assign(requestData, {
1909
2000
  srfproject: other.srfproject,
1910
2001
  });
2002
+ // 恢复会话流式消息处理回调(与提问共用,localMsgID 传空串走 isLoading 分支)
2003
+ const onMsg = this.createChatSseHandler(aiChat, id, '');
1911
2004
  try {
1912
2005
  await deService.aiResumeSse(
1913
- (msg: IPortalAsyncAction) => {
1914
- // 20: 持续回答中,消息会持续推送。同一个消息 id 会显示在同一个框内
1915
- if (msg.actionstate === 20 && msg.actionresult) {
1916
- // 设置当前聊天在加载中
1917
- aiChat.isLoading.value = true;
1918
- // 不存在才设置,存在则不设置(下方设置的时候需要去存储话题数据中)
1919
- if (!aiChat.getActiveAsyncacitonId()) {
1920
- aiChat.setActiveAsyncacitonId(msg.asyncacitonid);
1921
- }
1922
- aiChat.addMessage({
1923
- messageid: id,
1924
- state: msg.actionstate,
1925
- type: 'DEFAULT',
1926
- role: 'ASSISTANT',
1927
- content: msg.actionresult as string,
1928
- status: 'pending',
1929
- });
1930
- }
1931
- // 30: 回答完成,包含具体所有消息内容。直接覆盖之前的临时拼接消息
1932
- else if (msg.actionstate === 30 && msg.actionresult) {
1933
- const result = JSON.parse(msg.actionresult as string);
1934
- if (result.usage) {
1935
- aiChat.updateMsgUsage(id, result.usage);
1936
- }
1937
- if (result.files) {
1938
- aiChat.updateMsgOutputFiles(id, result.files);
1939
- }
1940
- const choices = result.choices;
1941
- if (choices && choices.length > 0) {
1942
- aiChat.replaceMessage({
1943
- messageid: id,
1944
- state: msg.actionstate,
1945
- type: 'DEFAULT',
1946
- role: 'ASSISTANT',
1947
- content: choices[0].content || '',
1948
- realmessageid: choices[0].messageid,
1949
- status: 'sent',
1950
- });
1951
- }
1952
- // 清空异步消息任务
1953
- aiChat.setActiveAsyncacitonId('');
1954
- }
1955
- // 40: 回答报错,展示错误信息
1956
- else if (msg.actionstate === 40) {
1957
- aiChat.replaceMessage({
1958
- messageid: id,
1959
- state: msg.actionstate,
1960
- type: 'ERROR',
1961
- role: 'ASSISTANT',
1962
- errorText: msg.actionresult as string,
1963
- status: 'failed',
1964
- });
1965
- // 清空异步消息任务
1966
- aiChat.setActiveAsyncacitonId('');
1967
- }
1968
- },
2006
+ onMsg,
1969
2007
  abortController,
1970
2008
  ctx,
1971
2009
  { srfactag: param.srfactag },
1972
- { ...resumeRequestData },
2010
+ { ...requestData },
1973
2011
  );
1974
2012
  } catch (error) {
2013
+ const errMsg = (error as Error)?.message || '';
1975
2014
  // 等于NONE_ERROR_MESSAGE,代码该异步行为已经完成,不需要做处理
1976
- if (error && (error as Error).message !== this.NONE_ERROR_MESSAGE) {
2015
+ if (error && errMsg !== this.NONE_ERROR_MESSAGE) {
1977
2016
  if (error && (error as Error).message !== this.PRE_ERROR_MESSAGE) {
1978
2017
  aiChat.replaceMessage({
1979
2018
  messageid: id,
@@ -1990,6 +2029,7 @@ export class AIChatUtil implements IApiAIChatUtil {
1990
2029
  {
1991
2030
  ...lastMessage,
1992
2031
  state: 40,
2032
+ errorText: ibiz.i18n.t('app.aiError'),
1993
2033
  },
1994
2034
  false,
1995
2035
  );
@@ -2921,4 +2961,103 @@ export class AIChatUtil implements IApiAIChatUtil {
2921
2961
  };
2922
2962
  return fileSystemOptions;
2923
2963
  }
2964
+
2965
+ /**
2966
+ * @description 打开嵌入视图
2967
+ * @private
2968
+ * @param {IContext} context 上下文
2969
+ * @param {IParams} params 视图参数
2970
+ * @param {string} viewId 视图标识
2971
+ * @param {HTMLElement} container 视图挂载容器
2972
+ * @param {IData[]} [selectedData] 选中数据
2973
+ * @param {(result: IModalData) => void} [callback] 回调
2974
+ * @returns {*} {Promise<void>}
2975
+ * @memberof AIChatUtil
2976
+ */
2977
+ private async openEmbedView(
2978
+ context: IContext,
2979
+ params: IParams,
2980
+ viewId: string,
2981
+ container: HTMLElement,
2982
+ selectedData: IData[],
2983
+ callback?: (result: IModalData) => void,
2984
+ ): Promise<void> {
2985
+ // 1. 先安全卸载之前的视图
2986
+ render(null, container);
2987
+ // 2. 绘制视图
2988
+ const modal = new Modal({
2989
+ mode: ViewMode.EMBED,
2990
+ viewUsage: 2,
2991
+ dismiss: (result: IModalData) => {
2992
+ callback?.(result);
2993
+ // 视图关闭时卸载
2994
+ render(null, container);
2995
+ },
2996
+ });
2997
+ const modelData = await ibiz.hub.getAppView(viewId);
2998
+ const vNode = createVNode(IBizViewShell, {
2999
+ modal,
3000
+ modelData,
3001
+ context,
3002
+ params: { ...params, checkstrictly: true },
3003
+ style: {
3004
+ height: `${modelData.height || 400}px`,
3005
+ },
3006
+ state: {
3007
+ selectedData,
3008
+ },
3009
+ });
3010
+ // 3. 设置应用上下文
3011
+ vNode.appContext = this.App._context;
3012
+ // 4. 挂载视图
3013
+ render(vNode, container);
3014
+ }
3015
+
3016
+ /**
3017
+ * @description 获取异步行为配置
3018
+ * @returns {*} {Promise<IData>}
3019
+ * @memberof AIChatUtil
3020
+ */
3021
+ async getAIAsyncActionOptions(): Promise<IData> {
3022
+ const asyncActionOptions: IData = {};
3023
+ // 获取最后一次执行异步行为对象
3024
+ asyncActionOptions.getLastAsyncAction = async (
3025
+ asyncActionID: string,
3026
+ ): Promise<IPortalAsyncAction | null> => {
3027
+ const result =
3028
+ await ibiz.hub.notice.asyncAction.service.get(asyncActionID);
3029
+ if (result.ok && result.data) {
3030
+ return result.data;
3031
+ }
3032
+ return null;
3033
+ };
3034
+ // 注册回调,返回销毁函数
3035
+ asyncActionOptions.registerCallBack = (
3036
+ asyncActionID: string,
3037
+ callback: (action: IPortalAsyncAction) => void,
3038
+ ) => {
3039
+ let callbacks = this.asyncActionCallBackMap.get(asyncActionID);
3040
+ if (!callbacks) {
3041
+ callbacks = new Set();
3042
+ this.asyncActionCallBackMap.set(asyncActionID, callbacks);
3043
+ }
3044
+ // 同一回调重复注册时幂等跳过
3045
+ callbacks.add(callback);
3046
+ // 返回取消订阅函数,供外部在不再需要时清理
3047
+ return () => {
3048
+ callbacks!.delete(callback);
3049
+ if (!callbacks!.size) {
3050
+ this.asyncActionCallBackMap.delete(asyncActionID);
3051
+ }
3052
+ };
3053
+ };
3054
+ // 预览异步行为
3055
+ asyncActionOptions.onPreview = async (
3056
+ asyncAction: IPortalAsyncAction,
3057
+ _event: MouseEvent,
3058
+ ): Promise<boolean> => {
3059
+ return openAsyncActionModal(asyncAction);
3060
+ };
3061
+ return asyncActionOptions;
3062
+ }
2924
3063
  }