11// components/agent-ui/index.js
2- import { checkConfig , randomSelectInitquestion } from "./tools" ;
2+ import { checkConfig , randomSelectInitquestion , getCloudInstance } from "./tools" ;
33import md5 from "./md5.js" ;
44Component ( {
55 properties : {
66 chatMode : {
77 type : String ,
88 value : "" ,
99 } ,
10- aiInstance :{
10+ envShareConfig :{
1111 type :Object ,
12- value :null
12+ value :{
13+ resourceAppid :String ,
14+ resourceEnv :String
15+ }
1316 } ,
1417 showBotAvatar : {
1518 type : Boolean ,
@@ -103,10 +106,11 @@ Component({
103106 } ) ;
104107 return ;
105108 }
109+ // 初始化一次cloudInstance,它是单例的,后面不传参数也可以获取到
110+ const cloudInstance = await getCloudInstance ( this . data . envShareConfig )
106111 if ( chatMode === "bot" ) {
107112 const { botId } = this . data . agentConfig ;
108- console . log ( this . data . aiInstance )
109- const ai = this . data . aiInstance || wx . cloud . extend . AI ;
113+ const ai = cloudInstance . extend . AI ;
110114 const bot = await ai . bot . get ( { botId } ) ;
111115 // 新增错误提示
112116 if ( bot . code ) {
@@ -128,13 +132,13 @@ Component({
128132 // 随机选取三个初始化问题
129133 const questions = randomSelectInitquestion ( bot . initQuestions , 3 ) ;
130134 let { allowWebSearch, allowUploadFile, allowPullRefresh, allowUploadImage, showToolCallDetail } = this . data . agentConfig ;
131- console . log ( "allowWebSearch" , allowWebSearch ) ;
135+ // console.log("allowWebSearch", allowWebSearch);
132136 allowWebSearch = allowWebSearch === undefined ? true : allowWebSearch ;
133137 allowUploadFile = allowUploadFile === undefined ? true : allowUploadFile ;
134138 allowPullRefresh = allowPullRefresh === undefined ? true : allowPullRefresh ;
135139 allowUploadImage = allowUploadImage === undefined ? true : allowUploadImage ;
136140 showToolCallDetail = showToolCallDetail === undefined ? true : showToolCallDetail
137- console . log ( "allowUploadFile" , allowUploadFile ) ;
141+ // console.log("allowUploadFile", allowUploadFile);
138142 this . setData ( {
139143 bot,
140144 questions,
@@ -155,7 +159,7 @@ Component({
155159 methods : {
156160 showErrorMsg : function ( e ) {
157161 const { content, reqid } = e . currentTarget . dataset ;
158- console . log ( "content" , content ) ;
162+ // console.log("content", content);
159163 const transformContent =
160164 typeof content === "string"
161165 ? reqid
@@ -211,7 +215,7 @@ Component({
211215 return transformToolCallList ;
212216 } ,
213217 handleLineChange : function ( e ) {
214- console . log ( "linechange" , e . detail . lineCount ) ;
218+ // console.log("linechange", e.detail.lineCount);
215219 // 查foot-function height
216220 const self = this ;
217221 const query = wx . createSelectorQuery ( ) . in ( this ) ;
@@ -223,7 +227,7 @@ Component({
223227 textareaHeight : res . height ,
224228 } ) ;
225229 } else {
226- console . log ( "未找到指定元素" ) ;
230+ // console.log("未找到指定元素");
227231 }
228232 } )
229233 . exec ( ) ;
@@ -374,7 +378,8 @@ Component({
374378 page : newPage ,
375379 } ) ;
376380 }
377- const ai = this . data . aiInstance . extend . AI || wx . cloud . extend . AI
381+ const cloudInstance = await getCloudInstance ( this . data . envShareConfig ) ;
382+ const ai = cloudInstance . extend . AI ;
378383 const res = await ai . bot . getChatRecords ( {
379384 botId : this . data . agentConfig . botId ,
380385 pageNumber : this . data . page ,
@@ -409,13 +414,13 @@ Component({
409414 transformItem . content = this . data . defaultErrorMsg ;
410415 }
411416 if ( item . role === "assistant" && item . origin_msg ) {
412- console . log ( "toolcall origin_msg" , JSON . parse ( item . origin_msg ) ) ;
417+ // console.log("toolcall origin_msg", JSON.parse(item.origin_msg));
413418 const origin_msg_obj = JSON . parse ( item . origin_msg ) ;
414419 if ( origin_msg_obj . aiResHistory ) {
415420 const transformToolCallList = this . transformToolCallHistoryList ( origin_msg_obj . aiResHistory ) ;
416421 transformItem . toolCallList = transformToolCallList ;
417422 const toolCallErr = transformToolCallList . find ( ( item ) => item . error ) ?. error ;
418- console . log ( "toolCallErr" , toolCallErr ) ;
423+ // console.log("toolCallErr", toolCallErr);
419424 if ( toolCallErr ?. error ?. message ) {
420425 transformItem . error = toolCallErr . error . message ;
421426 transformItem . reqId = item . trace_id || "" ;
@@ -431,7 +436,7 @@ Component({
431436 this . setData ( {
432437 chatRecords : [ ...freshChatRecords , ...this . data . chatRecords ] ,
433438 } ) ;
434- console . log ( "totalChatRecords" , this . data . chatRecords ) ;
439+ // console.log("totalChatRecords", this.data.chatRecords);
435440 }
436441 this . setData ( {
437442 triggered : false ,
@@ -479,8 +484,8 @@ Component({
479484 maxDuration : 30 ,
480485 camera : "back" ,
481486 success ( res ) {
482- console . log ( "res" , res ) ;
483- console . log ( "tempFiles" , res . tempFiles ) ;
487+ // console.log("res", res);
488+ // console.log("tempFiles", res.tempFiles);
484489 const isImageSizeValid = res . tempFiles . every ( ( item ) => item . size <= 30 * 1024 * 1024 ) ;
485490 if ( ! isImageSizeValid ) {
486491 wx . showToast ( {
@@ -507,7 +512,7 @@ Component({
507512 } ) ;
508513
509514 const finalFileList = [ ...tempFiles ] ;
510- console . log ( "final" , finalFileList ) ;
515+ // console.log("final", finalFileList);
511516 self . setData ( {
512517 sendFileList : finalFileList , //
513518 } ) ;
@@ -549,22 +554,22 @@ Component({
549554 cancelText : "取消" ,
550555 confirmText : "确认" ,
551556 success ( res ) {
552- console . log ( "res" , res ) ;
557+ // console.log("res", res);
553558 self . chooseMedia ( sourceType ) ;
554559 } ,
555560 fail ( error ) {
556- console . log ( "choose file e" , error ) ;
561+ // console.log("choose file e", error);
557562 } ,
558563 } ) ;
559564 } else {
560565 self . chooseMedia ( sourceType ) ;
561566 }
562567 } ,
563568 chooseMessageFile : function ( ) {
564- console . log ( "触发choose" ) ;
569+ // console.log("触发choose");
565570 const self = this ;
566571 const oldFileLen = this . data . sendFileList . filter ( ( item ) => item . rawType === "file" ) . length ;
567- console . log ( "oldFileLen" , oldFileLen ) ;
572+ // console.log("oldFileLen", oldFileLen);
568573 const subFileCount = oldFileLen <= 5 ? 5 - oldFileLen : 0 ;
569574 if ( subFileCount === 0 ) {
570575 wx . showToast ( {
@@ -579,7 +584,7 @@ Component({
579584 success ( res ) {
580585 // tempFilePath可以作为img标签的src属性显示图片
581586 // const tempFilePaths = res.tempFiles;
582- console . log ( "res" , res ) ;
587+ // console.log("res", res);
583588 // 检验文件后缀
584589 const isFileExtValid = res . tempFiles . every ( ( item ) => self . checkFileExt ( item . name . split ( "." ) [ 1 ] ) ) ;
585590 if ( ! isFileExtValid ) {
@@ -777,7 +782,8 @@ Component({
777782 // 新增一轮对话记录时 自动往下滚底
778783 this . autoToBottom ( ) ;
779784 if ( chatMode === "bot" ) {
780- const ai = this . data . aiInstance || wx . cloud . extend . AI ;
785+ const cloudInstance = await getCloudInstance ( this . data . envShareConfig ) ;
786+ const ai = cloudInstance . extend . AI ;
781787 const res = await ai . bot . sendMessage ( {
782788 data : {
783789 botId : bot . botId ,
@@ -1000,7 +1006,8 @@ Component({
10001006 [ `chatRecords[${ lastValueIndex } ].hiddenBtnGround` ] : isManuallyPaused ,
10011007 } ) ; // 对话完成,切回0 ,并且修改最后一条消息的状态,让下面的按钮展示
10021008 if ( bot . isNeedRecommend && ! isManuallyPaused ) {
1003- const ai = this . data . aiInstance || wx . cloud . extend . AI ;
1009+ const cloudInstance = await getCloudInstance ( this . data . envShareConfig )
1010+ const ai = cloudInstance . extend . AI ;
10041011 const chatRecords = this . data . chatRecords ;
10051012 const lastPairChatRecord = chatRecords . length >= 2 ? chatRecords . slice ( chatRecords . length - 2 ) : [ ] ;
10061013 const recommendRes = await ai . bot . getRecommendQuestions ( {
@@ -1029,8 +1036,8 @@ Component({
10291036 }
10301037 if ( chatMode === "model" ) {
10311038 const { modelProvider, quickResponseModel } = modelConfig ;
1032- console . log ( 'ryan' , this . data . aiInstance )
1033- const ai = this . data . aiInstance || wx . cloud . extend . AI
1039+ const cloudInstance = await getCloudInstance ( this . data . envShareConfig )
1040+ const ai = cloudInstance . extend . AI ;
10341041 const aiModel = ai . createModel ( modelProvider ) ;
10351042 const res = await aiModel . streamText ( {
10361043 data : {
0 commit comments