@@ -112,7 +112,7 @@ Component({
112112
113113 // 初始化第一条记录为welcomeMessage
114114 const record = {
115- content : bot . welcomeMessage || "您好,有什么需要帮助您的 ?" ,
115+ content : bot . welcomeMessage || "你好,有什么我可以帮到你 ?" ,
116116 record_id : "record_id" + String ( + new Date ( ) + 10 ) ,
117117 role : "assistant" ,
118118 hiddenBtnGround : true ,
@@ -145,13 +145,62 @@ Component({
145145 } ,
146146 methods : {
147147 showErrorMsg : function ( e ) {
148- const { content } = e . currentTarget . dataset ;
148+ const { content, reqid } = e . currentTarget . dataset ;
149149 console . log ( "content" , content ) ;
150+ const transformContent =
151+ typeof content === "string"
152+ ? reqid
153+ ? `${ content } |reqId:${ reqid } `
154+ : content
155+ : JSON . stringify ( { content, reqid } ) ;
150156 wx . showModal ( {
151157 title : "错误原因" ,
152- content : typeof content === "string" ? content : JSON . stringify ( { content } ) ,
158+ content : transformContent ,
159+ success ( ) {
160+ wx . setClipboardData ( {
161+ data : transformContent ,
162+ success : function ( res ) {
163+ wx . showToast ( {
164+ title : "复制错误完成" ,
165+ icon : "success" ,
166+ } ) ;
167+ } ,
168+ } ) ;
169+ } ,
153170 } ) ;
154171 } ,
172+ transformToolCallHistoryList : function ( toolCallList ) {
173+ const callParamsList = toolCallList . filter ( ( item ) => item . type === "tool-call" ) ;
174+ // const callResultList = toolCallList.filter(item => item.type === 'tool-result')
175+ const callContentList = toolCallList . filter ( ( item ) => item . type === "text" ) ;
176+ const transformToolCallList = [ ] ;
177+ for ( let i = 0 ; i < callParamsList . length ; i ++ ) {
178+ const curParam = callParamsList [ i ] ;
179+ const curResult = toolCallList . find (
180+ ( item ) => item . type === "tool-result" && item . toolCallId === curParam . tool_call . id
181+ ) ;
182+ const curContent = callContentList [ i ] ;
183+ const curError = toolCallList . find (
184+ // (item) => item.finish_reason === "error" && item.error.message.toolCallId === curParam.tool_call.id
185+ ( item ) => item . finish_reason === "error"
186+ ) ;
187+ const transformToolCallObj = {
188+ id : curParam . tool_call . id ,
189+ name : curParam . tool_call . function . name ,
190+ callParams : "```json\n\n" + JSON . stringify ( curParam . tool_call . function . arguments , null , 2 ) + "\n```" ,
191+ content : ( ( curContent && curContent . content ) || "" ) . replaceAll ( "\t" , "" ) . replaceAll ( "\n" , "\n\n" ) ,
192+ } ;
193+ if ( curResult ) {
194+ transformToolCallObj . callResult = "```json\n\n" + JSON . stringify ( curResult . result , null , 2 ) + "\n```" ;
195+ }
196+ if ( curError ) {
197+ transformToolCallObj . error = curError ;
198+ }
199+
200+ transformToolCallList . push ( transformToolCallObj ) ;
201+ }
202+ return transformToolCallList ;
203+ } ,
155204 handleLineChange : function ( e ) {
156205 console . log ( "linechange" , e . detail . lineCount ) ;
157206 // 查foot-function height
@@ -349,8 +398,26 @@ Component({
349398 if ( item . role === "assistant" && item . content === "" ) {
350399 transformItem . content = this . data . defaultErrorMsg ;
351400 }
401+ if ( item . role === "assistant" && item . origin_msg ) {
402+ console . log ( "toolcall origin_msg" , JSON . parse ( item . origin_msg ) ) ;
403+ const origin_msg_obj = JSON . parse ( item . origin_msg ) ;
404+ if ( origin_msg_obj . aiResHistory ) {
405+ const transformToolCallList = this . transformToolCallHistoryList ( origin_msg_obj . aiResHistory ) ;
406+ transformItem . toolCallList = transformToolCallList ;
407+ const toolCallErr = transformToolCallList . find ( ( item ) => item . error ) ?. error ;
408+ console . log ( "toolCallErr" , toolCallErr ) ;
409+ if ( toolCallErr ?. error ?. message ) {
410+ transformItem . error = toolCallErr . error . message ;
411+ transformItem . reqId = item . trace_id || "" ;
412+ }
413+ } else {
414+ // 之前异常的返回
415+ // return null
416+ }
417+ }
352418 return transformItem ;
353- } ) ;
419+ } )
420+ . filter ( ( item ) => item ) ;
354421 this . setData ( {
355422 chatRecords : [ ...freshChatRecords , ...this . data . chatRecords ] ,
356423 } ) ;
@@ -380,7 +447,7 @@ Component({
380447 return ;
381448 }
382449 const record = {
383- content : bot . welcomeMessage || "您好,有什么需要帮助您的 ?" ,
450+ content : bot . welcomeMessage || "你好,有什么我可以帮到你 ?" ,
384451 record_id : "record_id" + String ( + new Date ( ) + 10 ) ,
385452 role : "assistant" ,
386453 hiddenBtnGround : true ,
@@ -699,7 +766,6 @@ Component({
699766
700767 // 新增一轮对话记录时 自动往下滚底
701768 this . autoToBottom ( ) ;
702-
703769 if ( chatMode === "bot" ) {
704770 const ai = wx . cloud . extend . AI ;
705771 const res = await ai . bot . sendMessage ( {
@@ -762,6 +828,23 @@ Component({
762828 this . setData ( {
763829 [ `chatRecords[${ lastValueIndex } ].error` ] : lastValue . error ,
764830 } ) ;
831+ if ( lastValue . toolCallList && lastValue . toolCallList . length ) {
832+ let errToolCallObj = null ;
833+ if ( typeof error . message === "string" ) {
834+ errToolCallObj = lastValue . toolCallList [ lastValue . toolCallList . length - 1 ] ;
835+ } else {
836+ if ( error . message ?. toolCallId ) {
837+ errToolCallObj = lastValue . toolCallList . find ( ( item ) => item . id === error . message . toolCallId ) ;
838+ }
839+ }
840+ if ( errToolCallObj && ! errToolCallObj . callResult ) {
841+ errToolCallObj . error = error ;
842+ this . setData ( {
843+ [ `chatRecords[${ lastValueIndex } ].toolCallList` ] : lastValue . toolCallList ,
844+ } ) ;
845+ this . autoToBottom ( ) ;
846+ }
847+ }
765848 }
766849 this . setData ( {
767850 [ `chatRecords[${ lastValueIndex } ].search_info` ] : lastValue . search_info ,
@@ -770,6 +853,12 @@ Component({
770853 [ `chatRecords[${ lastValueIndex } ].content` ] : lastValue . content ,
771854 [ `chatRecords[${ lastValueIndex } ].record_id` ] : lastValue . record_id ,
772855 } ) ;
856+ // if (error) {
857+ // lastValue.error = error;
858+ // this.setData({
859+ // [`chatRecords[${lastValueIndex}].error`]: lastValue.error,
860+ // });
861+ // }
773862 break ;
774863 }
775864 // 下面根据type来确定输出的内容
@@ -802,13 +891,31 @@ Component({
802891 }
803892 // 内容
804893 if ( type === "text" ) {
805- contentText += content ;
806- lastValue . content = contentText ;
807- this . setData ( {
808- [ `chatRecords[${ lastValueIndex } ].content` ] : lastValue . content ,
809- [ `chatRecords[${ lastValueIndex } ].record_id` ] : lastValue . record_id ,
810- chatStatus : 3 ,
811- } ) ; // 聊天状态切换为输出content中
894+ // 区分是 toolCall 的content 还是普通的 content
895+ let isToolCallContent = false ;
896+ const toolCallList = lastValue . toolCallList ;
897+ if ( toolCallList && toolCallList . length ) {
898+ const lastToolCallObj = toolCallList [ toolCallList . length - 1 ] ;
899+ if ( lastToolCallObj . callParams && ! lastToolCallObj . callResult ) {
900+ isToolCallContent = true ;
901+ lastToolCallObj . content += content . replaceAll ( "\t" , "" ) ;
902+ this . setData ( {
903+ [ `chatRecords[${ lastValueIndex } ].toolCallList` ] : lastValue . toolCallList ,
904+ chatStatus : 3 ,
905+ } ) ;
906+ this . autoToBottom ( ) ;
907+ }
908+ }
909+
910+ if ( ! isToolCallContent ) {
911+ contentText += content ;
912+ lastValue . content = contentText ;
913+ this . setData ( {
914+ [ `chatRecords[${ lastValueIndex } ].content` ] : lastValue . content ,
915+ [ `chatRecords[${ lastValueIndex } ].record_id` ] : lastValue . record_id ,
916+ chatStatus : 3 ,
917+ } ) ; // 聊天状态切换为输出content中
918+ }
812919 }
813920 // 知识库,只更新一次
814921 if ( type === "knowledge" && ! lastValue . knowledge_meta ) {
@@ -827,6 +934,39 @@ Component({
827934 chatStatus : 2 ,
828935 } ) ;
829936 }
937+ // tool_call 场景,调用请求
938+ if ( type === "tool-call" ) {
939+ const { tool_call } = dataJson ;
940+ const callBody = {
941+ id : tool_call . id ,
942+ name : tool_call . function . name ,
943+ callParams : "```json\n" + JSON . stringify ( tool_call . function . arguments , null , 2 ) + "\n```" ,
944+ content : "" ,
945+ } ;
946+ if ( ! lastValue . toolCallList ) {
947+ lastValue . toolCallList = [ callBody ] ;
948+ } else {
949+ lastValue . toolCallList . push ( callBody ) ;
950+ }
951+ this . setData ( {
952+ [ `chatRecords[${ lastValueIndex } ].toolCallList` ] : lastValue . toolCallList ,
953+ } ) ;
954+ this . autoToBottom ( ) ;
955+ }
956+ // tool_call 场景,调用响应
957+ if ( type === "tool-result" ) {
958+ const { toolCallId, result } = dataJson ;
959+ if ( lastValue . toolCallList && lastValue . toolCallList . length ) {
960+ const lastToolCallObj = lastValue . toolCallList . find ( ( item ) => item . id === toolCallId ) ;
961+ if ( lastToolCallObj && ! lastToolCallObj . callResult ) {
962+ lastToolCallObj . callResult = "```json\n" + JSON . stringify ( result , null , 2 ) + "\n```" ;
963+ this . setData ( {
964+ [ `chatRecords[${ lastValueIndex } ].toolCallList` ] : lastValue . toolCallList ,
965+ } ) ;
966+ this . autoToBottom ( ) ;
967+ }
968+ }
969+ }
830970 } catch ( e ) {
831971 console . log ( "err" , event , e ) ;
832972 break ;
0 commit comments