@ibiz-template/vue3-components 0.7.41-alpha.165 → 0.7.41-alpha.166
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.
- package/dist/{index-li-EqXGB.js → index-CCoBeNIV.js} +6 -6
- package/dist/{index.es-CA0rGKdK.js → index.es-D1vng-7i.js} +1 -1
- package/dist/index.min.css +1 -1
- package/dist/index.system.min.js +1 -1
- package/dist/{wang-editor-BPGsMZgX.js → wang-editor-B0ZpE6o-.js} +1 -1
- package/dist/{xlsx-util-Bv9fDQB8.js → xlsx-util-Djaaq9Po.js} +1 -1
- package/es/editor/index.mjs +8 -0
- package/es/editor/work-space/embed-work-space/embed-work-space.css +1 -0
- package/es/editor/work-space/embed-work-space/embed-work-space.mjs +75 -0
- package/es/editor/work-space/embed-work-space.controller.mjs +62 -0
- package/es/editor/work-space/embed-work-space.provider.mjs +22 -0
- package/es/editor/work-space/index.mjs +5 -0
- package/es/util/ai-chat-util/ai-chat-util.mjs +217 -240
- package/lib/editor/index.cjs +8 -0
- package/lib/editor/work-space/embed-work-space/embed-work-space.cjs +77 -0
- package/lib/editor/work-space/embed-work-space/embed-work-space.css +1 -0
- package/lib/editor/work-space/embed-work-space.controller.cjs +64 -0
- package/lib/editor/work-space/embed-work-space.provider.cjs +24 -0
- package/lib/editor/work-space/index.cjs +11 -0
- package/lib/util/ai-chat-util/ai-chat-util.cjs +217 -240
- package/package.json +5 -5
- package/src/editor/index.ts +7 -1
- package/src/editor/work-space/embed-work-space/embed-work-space.scss +8 -0
- package/src/editor/work-space/embed-work-space/embed-work-space.tsx +78 -0
- package/src/editor/work-space/embed-work-space.controller.ts +70 -0
- package/src/editor/work-space/embed-work-space.provider.ts +28 -0
- package/src/editor/work-space/index.ts +3 -0
- package/src/util/ai-chat-util/ai-chat-util.ts +256 -276
|
@@ -56,6 +56,174 @@ export class AIChatUtil implements IApiAIChatUtil {
|
|
|
56
56
|
private NONE_ERROR_MESSAGE: string =
|
|
57
57
|
'Expected content-type to be text/event-stream, Actual: null';
|
|
58
58
|
|
|
59
|
+
// 网络中断自动续传的最大重试次数
|
|
60
|
+
private AUTO_RESUME_MAX_RETRY: number = 3;
|
|
61
|
+
|
|
62
|
+
// 网络中断自动续传的退避基数(毫秒),按 1s/2s/4s 指数退避
|
|
63
|
+
private AUTO_RESUME_BACKOFF_BASE: number = 1000;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @description 创建 AI 聊天 SSE 流式消息处理回调(提问与恢复会话共用)
|
|
67
|
+
* @param {any} aiChat 单实例聊天总控
|
|
68
|
+
* @param {string} id 本次问答的流式消息 id
|
|
69
|
+
* @param {string} localMsgID 本地用户消息 id(用于回填真实消息 id;为空串时为恢复会话场景,状态 20 走 isLoading 加载分支)
|
|
70
|
+
* @return {*} {(msg: IPortalAsyncAction) => void}
|
|
71
|
+
* @memberof AIChatUtil
|
|
72
|
+
*/
|
|
73
|
+
private createChatSseHandler(
|
|
74
|
+
aiChat: any,
|
|
75
|
+
id: string,
|
|
76
|
+
localMsgID: string,
|
|
77
|
+
): (msg: IPortalAsyncAction) => void {
|
|
78
|
+
// 更新标记,主要用于更新用户消息真实消息ID(每条消息都会回asextparam,只需要设置一次即可)
|
|
79
|
+
let updateFlag = true;
|
|
80
|
+
return (msg: IPortalAsyncAction) => {
|
|
81
|
+
// 20: 持续回答中,消息会持续推送。同一个消息 id 会显示在同一个框内
|
|
82
|
+
if (msg.actionstate === 20 && msg.actionresult) {
|
|
83
|
+
if (localMsgID) {
|
|
84
|
+
// 提问场景:移除等待回复消息
|
|
85
|
+
aiChat.removeWaitingMessage(id);
|
|
86
|
+
// 更新用户消息真实id
|
|
87
|
+
if (msg.asextparam && updateFlag) {
|
|
88
|
+
aiChat.updateMsgRealMessageID(localMsgID, msg.asextparam);
|
|
89
|
+
updateFlag = false;
|
|
90
|
+
}
|
|
91
|
+
} else {
|
|
92
|
+
// 恢复会话场景:设置当前聊天在加载中
|
|
93
|
+
aiChat.isLoading.value = true;
|
|
94
|
+
}
|
|
95
|
+
// 不存在才设置,存在则不设置(下方设置的时候需要去存储话题数据中)
|
|
96
|
+
if (!aiChat.getActiveAsyncacitonId()) {
|
|
97
|
+
aiChat.setActiveAsyncacitonId(msg.asyncacitonid);
|
|
98
|
+
}
|
|
99
|
+
aiChat.addMessage({
|
|
100
|
+
messageid: id,
|
|
101
|
+
state: msg.actionstate,
|
|
102
|
+
type: 'DEFAULT',
|
|
103
|
+
role: 'ASSISTANT',
|
|
104
|
+
content: msg.actionresult as string,
|
|
105
|
+
status: 'pending',
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
// 30: 回答完成,包含具体所有消息内容。直接覆盖之前的临时拼接消息
|
|
109
|
+
else if (msg.actionstate === 30 && msg.actionresult) {
|
|
110
|
+
const result = JSON.parse(msg.actionresult as string);
|
|
111
|
+
if (result.usage) {
|
|
112
|
+
aiChat.updateMsgUsage(id, result.usage);
|
|
113
|
+
}
|
|
114
|
+
if (result.files) {
|
|
115
|
+
aiChat.updateMsgOutputFiles(id, result.files);
|
|
116
|
+
}
|
|
117
|
+
const choices = result.choices;
|
|
118
|
+
if (choices && choices.length > 0) {
|
|
119
|
+
aiChat.replaceMessage({
|
|
120
|
+
messageid: id,
|
|
121
|
+
state: msg.actionstate,
|
|
122
|
+
type: 'DEFAULT',
|
|
123
|
+
role: 'ASSISTANT',
|
|
124
|
+
content: choices[0].content || '',
|
|
125
|
+
realmessageid: choices[0].messageid,
|
|
126
|
+
status: 'sent',
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
// 清空异步消息任务
|
|
130
|
+
aiChat.setActiveAsyncacitonId('');
|
|
131
|
+
}
|
|
132
|
+
// 40: 回答报错,展示错误信息
|
|
133
|
+
else if (msg.actionstate === 40) {
|
|
134
|
+
aiChat.replaceMessage({
|
|
135
|
+
messageid: id,
|
|
136
|
+
state: msg.actionstate,
|
|
137
|
+
type: 'ERROR',
|
|
138
|
+
role: 'ASSISTANT',
|
|
139
|
+
errorText: msg.actionresult as string,
|
|
140
|
+
status: 'failed',
|
|
141
|
+
});
|
|
142
|
+
// 清空异步消息任务
|
|
143
|
+
aiChat.setActiveAsyncacitonId('');
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* @description 网络中断(network error)后按 asyncacitonid 自动续传,指数退避重试;成功返回 true,不可恢复/重试耗尽返回 false 交回调用方走既有错误处理
|
|
150
|
+
* @param {any} aiChat 单实例聊天总控
|
|
151
|
+
* @param {IContext} ctx 上下文
|
|
152
|
+
* @param {IParams} param 视图参数
|
|
153
|
+
* @param {*} deService 实体服务
|
|
154
|
+
* @param {{ id: string; abortController: AbortController }} state 聊天实例提问状态(WeakMap 持有的同一对象引用)
|
|
155
|
+
* @param {{ asyncacitonid: string; sessionid?: string; srfaiagent?: string; srfproject?: string }} resumeInfo 续传参数
|
|
156
|
+
* @param {(msg: IPortalAsyncAction) => void} onMsg SSE 流式消息处理回调
|
|
157
|
+
* @return {*} {Promise<boolean>} 是否续传成功
|
|
158
|
+
* @memberof AIChatUtil
|
|
159
|
+
*/
|
|
160
|
+
private async autoResumeChat(
|
|
161
|
+
aiChat: any,
|
|
162
|
+
ctx: IContext,
|
|
163
|
+
param: IParams,
|
|
164
|
+
deService: any,
|
|
165
|
+
state: { id: string; abortController: AbortController },
|
|
166
|
+
resumeInfo: {
|
|
167
|
+
asyncacitonid: string;
|
|
168
|
+
sessionid?: string;
|
|
169
|
+
srfaiagent?: string;
|
|
170
|
+
srfproject?: string;
|
|
171
|
+
},
|
|
172
|
+
onMsg: (msg: IPortalAsyncAction) => void,
|
|
173
|
+
): Promise<boolean> {
|
|
174
|
+
// 续传前先释放旧 SSE 连接,避免旧连接未关闭影响新连接建立
|
|
175
|
+
state.abortController?.abort();
|
|
176
|
+
// 全程复用同一个新 AbortController 并存回 state:退避等待期间用户点停止时,
|
|
177
|
+
// abortQuestion abort 的即是该实例,下一轮请求携带已中止信号会立即抛 AbortError 退出循环
|
|
178
|
+
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
|
+
// 其余(网络仍断等)继续下一轮退避重试
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
|
|
59
227
|
/**
|
|
60
228
|
* 获取AI聊天对象
|
|
61
229
|
*/
|
|
@@ -330,6 +498,7 @@ export class AIChatUtil implements IApiAIChatUtil {
|
|
|
330
498
|
projectSelectPlaceholder:
|
|
331
499
|
editorParams.srfprojectselectplaceholder ||
|
|
332
500
|
ibiz.config.common.aiProjectSelectPlaceholder,
|
|
501
|
+
selectAIProjectId: editorParams.srfselectaiprojectid,
|
|
333
502
|
};
|
|
334
503
|
// 编辑器参数srfaiappendcurdata,是否传入对象参数,用于历史查询传参
|
|
335
504
|
if (editorParams.srfaiappendcurdata) {
|
|
@@ -621,8 +790,6 @@ export class AIChatUtil implements IApiAIChatUtil {
|
|
|
621
790
|
srfproject: string | undefined,
|
|
622
791
|
localMsgID: string,
|
|
623
792
|
) => {
|
|
624
|
-
// 更新标记,主要用于更新用户消息真实消息ID(每条消息都会回asextparam,只需要设置一次即可)
|
|
625
|
-
let updateFlag = true;
|
|
626
793
|
const state = getChatState(aiChat);
|
|
627
794
|
state.id = createUUID();
|
|
628
795
|
state.abortController = new AbortController();
|
|
@@ -630,27 +797,28 @@ export class AIChatUtil implements IApiAIChatUtil {
|
|
|
630
797
|
const deService = await ibiz.hub
|
|
631
798
|
.getApp(ctx.srfappid)
|
|
632
799
|
.deService.getService(ctx, other.appDataEntityId);
|
|
800
|
+
// SSE 流式消息处理回调(question 与网络中断自动续传共用)
|
|
801
|
+
const onMsg = this.createChatSseHandler(aiChat, id, localMsgID);
|
|
633
802
|
try {
|
|
634
|
-
const
|
|
803
|
+
const requestData: IData = {
|
|
635
804
|
messages: arr,
|
|
636
805
|
sessionid,
|
|
637
806
|
};
|
|
638
|
-
if (srfaiagent)
|
|
639
|
-
if (srfmode)
|
|
640
|
-
if (srfscope)
|
|
641
|
-
if (srfproject)
|
|
642
|
-
if (srfknowledgebases)
|
|
643
|
-
questionRequestData.knowledgebases = srfknowledgebases;
|
|
807
|
+
if (srfaiagent) requestData.srfaiagent = srfaiagent;
|
|
808
|
+
if (srfmode) requestData.mode = srfmode;
|
|
809
|
+
if (srfscope) requestData.srfscope = srfscope;
|
|
810
|
+
if (srfproject) requestData.srfproject = srfproject;
|
|
811
|
+
if (srfknowledgebases) requestData.knowledgebases = srfknowledgebases;
|
|
644
812
|
if (srfaiagentconfig) {
|
|
645
|
-
Object.assign(
|
|
813
|
+
Object.assign(requestData, srfaiagentconfig);
|
|
646
814
|
}
|
|
647
815
|
if (mcpservers) {
|
|
648
|
-
Object.assign(
|
|
816
|
+
Object.assign(requestData, {
|
|
649
817
|
mcpservers,
|
|
650
818
|
});
|
|
651
819
|
}
|
|
652
820
|
if (sessionmeta) {
|
|
653
|
-
Object.assign(
|
|
821
|
+
Object.assign(requestData, {
|
|
654
822
|
srfuimeta: JSON.stringify(sessionmeta),
|
|
655
823
|
});
|
|
656
824
|
}
|
|
@@ -663,79 +831,34 @@ export class AIChatUtil implements IApiAIChatUtil {
|
|
|
663
831
|
Object.assign(extParams, { ...appendcurdata });
|
|
664
832
|
}
|
|
665
833
|
if (extParams && Object.keys(extParams).length > 0) {
|
|
666
|
-
Object.assign(
|
|
834
|
+
Object.assign(requestData, { srfextparams: extParams });
|
|
667
835
|
}
|
|
668
836
|
// 新增等待回复消息(AI首条内容到达前显示loading气泡)
|
|
669
837
|
aiChat.addWaitingMessage(id);
|
|
670
838
|
await deService.aiChatSse(
|
|
671
|
-
|
|
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
|
-
},
|
|
839
|
+
onMsg,
|
|
732
840
|
abortController,
|
|
733
841
|
ctx,
|
|
734
842
|
{ srfactag: param.srfactag },
|
|
735
|
-
{ ...
|
|
843
|
+
{ ...requestData },
|
|
736
844
|
);
|
|
737
845
|
} catch (error) {
|
|
738
|
-
|
|
846
|
+
const errMsg = (error as Error)?.message || '';
|
|
847
|
+
const asyncacitonid = aiChat.getActiveAsyncacitonId();
|
|
848
|
+
// 仅网络中断且后端异步动作仍在运行(asyncid 未清)时自动续传
|
|
849
|
+
if (errMsg === 'network error' && asyncacitonid) {
|
|
850
|
+
const resumed = await this.autoResumeChat(
|
|
851
|
+
aiChat,
|
|
852
|
+
ctx,
|
|
853
|
+
param,
|
|
854
|
+
deService,
|
|
855
|
+
state,
|
|
856
|
+
{ asyncacitonid, sessionid, srfaiagent, srfproject },
|
|
857
|
+
onMsg,
|
|
858
|
+
);
|
|
859
|
+
if (resumed) return true;
|
|
860
|
+
}
|
|
861
|
+
if (error && errMsg !== this.PRE_ERROR_MESSAGE) {
|
|
739
862
|
aiChat.replaceMessage({
|
|
740
863
|
messageid: id,
|
|
741
864
|
state: 40,
|
|
@@ -750,6 +873,7 @@ export class AIChatUtil implements IApiAIChatUtil {
|
|
|
750
873
|
{
|
|
751
874
|
...lastMessage,
|
|
752
875
|
state: 40,
|
|
876
|
+
errorText: ibiz.i18n.t('app.aiError'),
|
|
753
877
|
},
|
|
754
878
|
false,
|
|
755
879
|
);
|
|
@@ -822,87 +946,35 @@ export class AIChatUtil implements IApiAIChatUtil {
|
|
|
822
946
|
const deService = await ibiz.hub
|
|
823
947
|
.getApp(ctx.srfappid)
|
|
824
948
|
.deService.getService(ctx, other.appDataEntityId);
|
|
825
|
-
const
|
|
949
|
+
const requestData: IData = { asyncacitonid: other.asyncacitonid };
|
|
826
950
|
if (other.sessionid) {
|
|
827
|
-
Object.assign(
|
|
951
|
+
Object.assign(requestData, {
|
|
828
952
|
sessionid: other.sessionid,
|
|
829
953
|
});
|
|
830
954
|
}
|
|
831
955
|
if (other.srfaiagent) {
|
|
832
|
-
Object.assign(
|
|
956
|
+
Object.assign(requestData, {
|
|
833
957
|
srfaiagent: other.srfaiagent,
|
|
834
958
|
});
|
|
835
959
|
}
|
|
836
960
|
if (other.srfproject)
|
|
837
|
-
Object.assign(
|
|
961
|
+
Object.assign(requestData, {
|
|
838
962
|
srfproject: other.srfproject,
|
|
839
963
|
});
|
|
964
|
+
// 恢复会话流式消息处理回调(与提问共用,localMsgID 传空串走 isLoading 分支)
|
|
965
|
+
const onMsg = this.createChatSseHandler(aiChat, id, '');
|
|
840
966
|
try {
|
|
841
967
|
await deService.aiResumeSse(
|
|
842
|
-
|
|
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
|
-
},
|
|
968
|
+
onMsg,
|
|
898
969
|
abortController,
|
|
899
970
|
ctx,
|
|
900
971
|
{ srfactag: param.srfactag },
|
|
901
|
-
{ ...
|
|
972
|
+
{ ...requestData },
|
|
902
973
|
);
|
|
903
974
|
} catch (error) {
|
|
975
|
+
const errMsg = (error as Error)?.message || '';
|
|
904
976
|
// 等于NONE_ERROR_MESSAGE,代码该异步行为已经完成,不需要做处理
|
|
905
|
-
if (error &&
|
|
977
|
+
if (error && errMsg !== this.NONE_ERROR_MESSAGE) {
|
|
906
978
|
if (error && (error as Error).message !== this.PRE_ERROR_MESSAGE) {
|
|
907
979
|
aiChat.replaceMessage({
|
|
908
980
|
messageid: id,
|
|
@@ -919,6 +991,7 @@ export class AIChatUtil implements IApiAIChatUtil {
|
|
|
919
991
|
{
|
|
920
992
|
...lastMessage,
|
|
921
993
|
state: 40,
|
|
994
|
+
errorText: ibiz.i18n.t('app.aiError'),
|
|
922
995
|
},
|
|
923
996
|
false,
|
|
924
997
|
);
|
|
@@ -1378,7 +1451,10 @@ export class AIChatUtil implements IApiAIChatUtil {
|
|
|
1378
1451
|
projectSelectPlaceholder:
|
|
1379
1452
|
params.srfprojectselectplaceholder ||
|
|
1380
1453
|
ibiz.config.common.aiProjectSelectPlaceholder,
|
|
1454
|
+
selectAIProjectId: params.srfselectaiprojectid,
|
|
1381
1455
|
};
|
|
1456
|
+
if (params.hasOwnProperty('srfselectaiprojectid'))
|
|
1457
|
+
delete params.srfselectaiprojectid;
|
|
1382
1458
|
if (params.hasOwnProperty('srfenableaiprojectselect'))
|
|
1383
1459
|
delete params.srfenableaiprojectselect;
|
|
1384
1460
|
if (params.hasOwnProperty('srfprojectselectplaceholder'))
|
|
@@ -1694,8 +1770,6 @@ export class AIChatUtil implements IApiAIChatUtil {
|
|
|
1694
1770
|
srfproject: string | undefined,
|
|
1695
1771
|
localMsgID: string,
|
|
1696
1772
|
) => {
|
|
1697
|
-
// 更新标记,主要用于更新用户消息真实消息ID(每条消息都会回asextparam,只需要设置一次即可)
|
|
1698
|
-
let updateFlag = true;
|
|
1699
1773
|
const state = getChatState(aiChat);
|
|
1700
1774
|
state.id = createUUID();
|
|
1701
1775
|
state.abortController = new AbortController();
|
|
@@ -1703,25 +1777,26 @@ export class AIChatUtil implements IApiAIChatUtil {
|
|
|
1703
1777
|
const deService = await ibiz.hub
|
|
1704
1778
|
.getApp(ctx.srfappid)
|
|
1705
1779
|
.deService.getService(ctx, other.appDataEntityId);
|
|
1780
|
+
// SSE 流式消息处理回调(question 与网络中断自动续传共用)
|
|
1781
|
+
const onMsg = this.createChatSseHandler(aiChat, id, localMsgID);
|
|
1706
1782
|
try {
|
|
1707
|
-
const
|
|
1783
|
+
const requestData: IData = {
|
|
1708
1784
|
messages: arr,
|
|
1709
1785
|
sessionid,
|
|
1710
1786
|
};
|
|
1711
|
-
if (srfaiagent)
|
|
1712
|
-
if (srfmode)
|
|
1713
|
-
if (srfscope)
|
|
1714
|
-
if (srfproject)
|
|
1715
|
-
if (srfknowledgebases)
|
|
1716
|
-
questionRequestData.knowledgebases = srfknowledgebases;
|
|
1787
|
+
if (srfaiagent) requestData.srfaiagent = srfaiagent;
|
|
1788
|
+
if (srfmode) requestData.mode = srfmode;
|
|
1789
|
+
if (srfscope) requestData.srfscope = srfscope;
|
|
1790
|
+
if (srfproject) requestData.srfproject = srfproject;
|
|
1791
|
+
if (srfknowledgebases) requestData.knowledgebases = srfknowledgebases;
|
|
1717
1792
|
if (srfaiagentconfig) {
|
|
1718
|
-
Object.assign(
|
|
1793
|
+
Object.assign(requestData, srfaiagentconfig);
|
|
1719
1794
|
}
|
|
1720
1795
|
if (mcpservers) {
|
|
1721
|
-
Object.assign(
|
|
1796
|
+
Object.assign(requestData, { mcpservers });
|
|
1722
1797
|
}
|
|
1723
1798
|
if (sessionmeta) {
|
|
1724
|
-
Object.assign(
|
|
1799
|
+
Object.assign(requestData, {
|
|
1725
1800
|
srfuimeta: JSON.stringify(sessionmeta),
|
|
1726
1801
|
});
|
|
1727
1802
|
}
|
|
@@ -1734,79 +1809,34 @@ export class AIChatUtil implements IApiAIChatUtil {
|
|
|
1734
1809
|
Object.assign(extParams, { ...appendcurdata });
|
|
1735
1810
|
}
|
|
1736
1811
|
if (extParams && Object.keys(extParams).length > 0) {
|
|
1737
|
-
Object.assign(
|
|
1812
|
+
Object.assign(requestData, { srfextparams: extParams });
|
|
1738
1813
|
}
|
|
1739
1814
|
// 新增等待回复消息(AI首条内容到达前显示loading气泡)
|
|
1740
1815
|
aiChat.addWaitingMessage(id);
|
|
1741
1816
|
await deService.aiChatSse(
|
|
1742
|
-
|
|
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
|
-
},
|
|
1817
|
+
onMsg,
|
|
1803
1818
|
abortController,
|
|
1804
1819
|
ctx,
|
|
1805
1820
|
{ srfactag: param.srfactag },
|
|
1806
|
-
{ ...
|
|
1821
|
+
{ ...requestData },
|
|
1807
1822
|
);
|
|
1808
1823
|
} catch (error) {
|
|
1809
|
-
|
|
1824
|
+
const errMsg = (error as Error)?.message || '';
|
|
1825
|
+
const asyncacitonid = aiChat.getActiveAsyncacitonId();
|
|
1826
|
+
// 仅网络中断且后端异步动作仍在运行(asyncid 未清)时自动续传
|
|
1827
|
+
if (errMsg === 'network error' && asyncacitonid) {
|
|
1828
|
+
const resumed = await this.autoResumeChat(
|
|
1829
|
+
aiChat,
|
|
1830
|
+
ctx,
|
|
1831
|
+
param,
|
|
1832
|
+
deService,
|
|
1833
|
+
state,
|
|
1834
|
+
{ asyncacitonid, sessionid, srfaiagent, srfproject },
|
|
1835
|
+
onMsg,
|
|
1836
|
+
);
|
|
1837
|
+
if (resumed) return true;
|
|
1838
|
+
}
|
|
1839
|
+
if (error && errMsg !== this.PRE_ERROR_MESSAGE) {
|
|
1810
1840
|
aiChat.replaceMessage({
|
|
1811
1841
|
messageid: id,
|
|
1812
1842
|
state: 40,
|
|
@@ -1821,6 +1851,7 @@ export class AIChatUtil implements IApiAIChatUtil {
|
|
|
1821
1851
|
{
|
|
1822
1852
|
...lastMessage,
|
|
1823
1853
|
state: 40,
|
|
1854
|
+
errorText: ibiz.i18n.t('app.aiError'),
|
|
1824
1855
|
},
|
|
1825
1856
|
false,
|
|
1826
1857
|
);
|
|
@@ -1893,87 +1924,35 @@ export class AIChatUtil implements IApiAIChatUtil {
|
|
|
1893
1924
|
const deService = await ibiz.hub
|
|
1894
1925
|
.getApp(ctx.srfappid)
|
|
1895
1926
|
.deService.getService(ctx, other.appDataEntityId);
|
|
1896
|
-
const
|
|
1927
|
+
const requestData: IData = { asyncacitonid: other.asyncacitonid };
|
|
1897
1928
|
if (other.sessionid) {
|
|
1898
|
-
Object.assign(
|
|
1929
|
+
Object.assign(requestData, {
|
|
1899
1930
|
sessionid: other.sessionid,
|
|
1900
1931
|
});
|
|
1901
1932
|
}
|
|
1902
1933
|
if (other.srfaiagent) {
|
|
1903
|
-
Object.assign(
|
|
1934
|
+
Object.assign(requestData, {
|
|
1904
1935
|
srfaiagent: other.srfaiagent,
|
|
1905
1936
|
});
|
|
1906
1937
|
}
|
|
1907
1938
|
if (other.srfproject)
|
|
1908
|
-
Object.assign(
|
|
1939
|
+
Object.assign(requestData, {
|
|
1909
1940
|
srfproject: other.srfproject,
|
|
1910
1941
|
});
|
|
1942
|
+
// 恢复会话流式消息处理回调(与提问共用,localMsgID 传空串走 isLoading 分支)
|
|
1943
|
+
const onMsg = this.createChatSseHandler(aiChat, id, '');
|
|
1911
1944
|
try {
|
|
1912
1945
|
await deService.aiResumeSse(
|
|
1913
|
-
|
|
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
|
-
},
|
|
1946
|
+
onMsg,
|
|
1969
1947
|
abortController,
|
|
1970
1948
|
ctx,
|
|
1971
1949
|
{ srfactag: param.srfactag },
|
|
1972
|
-
{ ...
|
|
1950
|
+
{ ...requestData },
|
|
1973
1951
|
);
|
|
1974
1952
|
} catch (error) {
|
|
1953
|
+
const errMsg = (error as Error)?.message || '';
|
|
1975
1954
|
// 等于NONE_ERROR_MESSAGE,代码该异步行为已经完成,不需要做处理
|
|
1976
|
-
if (error &&
|
|
1955
|
+
if (error && errMsg !== this.NONE_ERROR_MESSAGE) {
|
|
1977
1956
|
if (error && (error as Error).message !== this.PRE_ERROR_MESSAGE) {
|
|
1978
1957
|
aiChat.replaceMessage({
|
|
1979
1958
|
messageid: id,
|
|
@@ -1990,6 +1969,7 @@ export class AIChatUtil implements IApiAIChatUtil {
|
|
|
1990
1969
|
{
|
|
1991
1970
|
...lastMessage,
|
|
1992
1971
|
state: 40,
|
|
1972
|
+
errorText: ibiz.i18n.t('app.aiError'),
|
|
1993
1973
|
},
|
|
1994
1974
|
false,
|
|
1995
1975
|
);
|