Skip to content

Commit d72487a

Browse files
committed
fix: support error handler
1 parent 0e642e5 commit d72487a

File tree

6 files changed

+66
-14
lines changed

6 files changed

+66
-14
lines changed

apps/miniprogram-agent-ui/miniprogram/components/agent-ui/chatFile/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Component({
2222
}
2323

2424
// 上传云存储获取 fileId
25-
console.log('rawFileName tempFileName tempPath', rawFileName, tempFileName, tempPath)
25+
// console.log('rawFileName tempFileName tempPath', rawFileName, tempFileName, tempPath)
2626
wx.cloud.uploadFile({
2727
cloudPath: this.generateCosUploadPath(botId, rawFileName ? (rawFileName.split('.')[0] + '-' + tempFileName) : tempFileName), // 云上文件路径
2828
filePath: tempPath,
Lines changed: 2 additions & 0 deletions
Loading

apps/miniprogram-agent-ui/miniprogram/components/agent-ui/index.js

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ Component({
124124
feedbackType: "",
125125
textareaHeight: 50,
126126
curLineCount: 1,
127+
defaultErrorMsg: "网络繁忙,请稍后重试!",
127128
},
128129
attached: async function () {
129130
const chatMode = this.data.chatMode;
@@ -151,7 +152,7 @@ Component({
151152

152153
// 初始化第一条记录为welcomeMessage
153154
const record = {
154-
content: bot.welcomeMessage,
155+
content: bot.welcomeMessage || "您好,有什么需要帮助您的?",
155156
record_id: "record_id" + String(+new Date() + 10),
156157
role: "assistant",
157158
hiddenBtnGround: true,
@@ -183,6 +184,14 @@ Component({
183184
});
184185
},
185186
methods: {
187+
showErrorMsg: function (e) {
188+
const { content } = e.currentTarget.dataset;
189+
console.log("content", content);
190+
wx.showModal({
191+
title: "错误原因",
192+
content: typeof content === "string" ? content : JSON.stringify({ content }),
193+
});
194+
},
186195
handleLineChange: function (e) {
187196
console.log("linechange", e.detail.lineCount);
188197
// 查foot-function height
@@ -389,6 +398,9 @@ Component({
389398
fileSize: item.bytes,
390399
}));
391400
}
401+
if (item.role === "assistant" && item.content === "") {
402+
transformItem.content = this.data.defaultErrorMsg;
403+
}
392404
return transformItem;
393405
});
394406
this.setData({
@@ -420,7 +432,7 @@ Component({
420432
return;
421433
}
422434
const record = {
423-
content: bot.welcomeMessage,
435+
content: bot.welcomeMessage || "您好,有什么需要帮助您的?",
424436
record_id: "record_id" + String(+new Date() + 10),
425437
role: "assistant",
426438
hiddenBtnGround: true,
@@ -495,6 +507,13 @@ Component({
495507
});
496508
return;
497509
}
510+
if (this.data.useWebSearch) {
511+
wx.showModal({
512+
title: "提示",
513+
content: "联网搜索不支持上传文件/图片",
514+
});
515+
return;
516+
}
498517
const self = this;
499518
const isCurSendFile = this.data.sendFileList.find((item) => item.rawType === "file");
500519
if (isCurSendFile) {
@@ -608,7 +627,7 @@ Component({
608627
if (this.data.useWebSearch) {
609628
wx.showModal({
610629
title: "提示",
611-
content: "联网搜索不支持上传附件",
630+
content: "联网搜索不支持上传文件/图片",
612631
});
613632
return;
614633
}
@@ -760,6 +779,9 @@ Component({
760779
this.toBottom(40);
761780
}
762781
const { data } = event;
782+
if (data === "[DONE]") {
783+
break;
784+
}
763785
try {
764786
const dataJson = JSON.parse(data);
765787
const {
@@ -773,6 +795,7 @@ Component({
773795
knowledge_base,
774796
finish_reason,
775797
search_results,
798+
error,
776799
} = dataJson;
777800
const newValue = [...this.data.chatRecords];
778801
// 取最后一条消息更新
@@ -785,7 +808,13 @@ Component({
785808
lastValue.search_info = null;
786809
lastValue.reasoning_content = "";
787810
lastValue.knowledge_meta = [];
788-
lastValue.content = "网络繁忙,请稍后重试!";
811+
lastValue.content = this.data.defaultErrorMsg;
812+
if (error && error.message) {
813+
lastValue.error = error.message;
814+
this.setData({
815+
[`chatRecords[${lastValueIndex}].error`]: lastValue.error,
816+
});
817+
}
789818
this.setData({
790819
[`chatRecords[${lastValueIndex}].search_info`]: lastValue.search_info,
791820
[`chatRecords[${lastValueIndex}].reasoning_content`]: lastValue.reasoning_content,
@@ -851,7 +880,7 @@ Component({
851880
});
852881
}
853882
} catch (e) {
854-
// console.log('err', event, e)
883+
console.log("err", event, e);
855884
break;
856885
}
857886
index++;
@@ -862,6 +891,12 @@ Component({
862891
// 取最后一条消息更新
863892
const lastValue = newValue[lastValueIndex];
864893
lastValue.hiddenBtnGround = isManuallyPaused;
894+
if (lastValue.content === "") {
895+
lastValue.content = this.data.defaultErrorMsg;
896+
this.setData({
897+
[`chatRecords[${lastValueIndex}].content`]: lastValue.content,
898+
});
899+
}
865900
this.setData({
866901
chatStatus: 0,
867902
[`chatRecords[${lastValueIndex}].hiddenBtnGround`]: isManuallyPaused,

apps/miniprogram-agent-ui/miniprogram/components/agent-ui/index.wxml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,16 @@
6868
<markdownPreview markdown="{{item.content||''}}"></markdownPreview>
6969
<!-- 下面的按钮 -->
7070
<view style="display: flex; gap: 10px;justify-content: flex;" wx:if="{{!item.hiddenBtnGround}}">
71+
<image wx:if="{{item.error}}" mode="widthFix" bind:tap="showErrorMsg" src='./imgs/error-circle.svg' style="width: 36rpx; height: 36rpx;" data-content="{{item.error}}" />
7172
<image mode="widthFix" bind:tap="copyChatRecord" src='./imgs/copy.svg' style="width: 36rpx; height: 36rpx;" data-content="{{item.content}}" />
72-
<button class="share_btn" open-type="share">
73-
<image mode="widthFix" src='./imgs/share.svg' style="width: 36rpx; height: 36rpx;vertical-align: top;" bind:tap="share" />
74-
</button>
75-
<block wx:if="{{chatMode=== 'bot'}}">
76-
<image mode="widthFix" bind:tap="openFeedback" data-feedbackType="upvote" data-feedbackRecordId="{{item.record_id}}" src='./imgs/thumb-up.svg' style="width: 36rpx; height: 36rpx;" />
77-
<image mode="widthFix" bind:tap="openFeedback" data-feedbackType="downvote" data-feedbackRecordId="{{item.record_id}}" src='./imgs/thumb-down.svg' style="width: 36rpx; height: 36rpx;" />
73+
<block wx:if="{{!item.error}}">
74+
<button class="share_btn" open-type="share">
75+
<image mode="widthFix" src='./imgs/share.svg' style="width: 36rpx; height: 36rpx;vertical-align: top;" bind:tap="share" />
76+
</button>
77+
<block wx:if="{{chatMode=== 'bot'}}">
78+
<image mode="widthFix" bind:tap="openFeedback" data-feedbackType="upvote" data-feedbackRecordId="{{item.record_id}}" src='./imgs/thumb-up.svg' style="width: 36rpx; height: 36rpx;" />
79+
<image mode="widthFix" bind:tap="openFeedback" data-feedbackType="downvote" data-feedbackRecordId="{{item.record_id}}" src='./imgs/thumb-down.svg' style="width: 36rpx; height: 36rpx;" />
80+
</block>
7881
</block>
7982
</view>
8083
</block>

apps/miniprogram-agent-ui/project.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@
6363
"ignore": [],
6464
"include": []
6565
},
66-
"appid": "wx401b4f0b10b9f661",
66+
"appid": "wx5ceb4e4809aa1d28",
6767
"libVersion": "3.7.8"
6868
}

apps/miniprogram-agent-ui/project.private.config.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,19 @@
33
"compileHotReLoad": true,
44
"urlCheck": true
55
},
6-
"condition": {},
6+
"condition": {
7+
"miniprogram": {
8+
"list": [
9+
{
10+
"name": "pages/chatBot/chatBot",
11+
"pathName": "pages/chatBot/chatBot",
12+
"query": "",
13+
"scene": null,
14+
"launchMode": "default"
15+
}
16+
]
17+
}
18+
},
719
"description": "项目私有配置文件。此文件中的内容将覆盖 project.config.json 中的相同字段。项目的改动优先同步到此文件中。详见文档:https://developers.weixin.qq.com/miniprogram/dev/devtools/projectconfig.html",
820
"projectname": "cloudbase-agent-ui",
921
"libVersion": "3.7.8"

0 commit comments

Comments
 (0)