index.vue 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866
  1. <template>
  2. <!-- agent ui 组件根容器 -->
  3. <view class="agent-ui">
  4. <view class="navBar" v-if="chatMode === 'bot'">
  5. <view class="nav-content">
  6. <!-- <image src="{{bot.avatar}}" mode="aspectFill" class="bot-avatar"/> -->
  7. <text class="bot-name">{{ bot.name }}</text>
  8. </view>
  9. </view>
  10. <!-- 聊天对话区 -->
  11. <scroll-view
  12. @wheel="onWheel"
  13. :enhanced="true"
  14. @scroll="onScroll"
  15. @dragstart="handleScrollStart"
  16. class="main"
  17. :style="'height: ' + (windowInfo.windowHeight - footerHeight - (chatMode === 'bot' ? 45 : 0)) + 'px;'"
  18. :scroll-y="true"
  19. :scroll-top="viewTop"
  20. :scroll-into-view="scrollTo"
  21. lower-threshold="1"
  22. @scrolltolower="handleScrollToLower"
  23. :show-scrollbar="false"
  24. :refresher-enabled="showPullRefresh"
  25. :refresher-threshold="80"
  26. @refresherrefresh="handelRefresh"
  27. :refresher-triggered="triggered"
  28. :bounces="false"
  29. >
  30. <view v-if="chatMode === 'bot' && showPullRefresh" class="tips">
  31. {{ refreshText }}
  32. </view>
  33. <view v-if="chatMode === 'model'" class="nav">
  34. <image :src="bot.avatar || modelConfig.logo" mode="aspectFill" class="avatar" />
  35. <view style="line-height: 47px; font-size: 20px; font-weight: 500">{{ chatMode === 'bot' ? bot.name : modelConfig.modelProvider }}</view>
  36. <view style="line-height: 26px; padding: 0px 16px; font-size: 32rpx">{{ chatMode === 'bot' ? '' : modelConfig.welcomeMsg }}</view>
  37. </view>
  38. <block v-for="(item, index) in chatRecords" :key="index">
  39. <!-- 系统聊天 -->
  40. <view class="system" :style="'padding-left: ' + (showBotAvatar ? 100 : 0) + 'rpx;'" v-if="item.role === 'assistant'">
  41. <view class="avatar-left" v-if="showBotAvatar">
  42. <image :src="chatMode === 'bot' ? bot.avatar : modelConfig.logo" mode="aspectFill" style="width: 90rpx; height: 90rpx; border-radius: 28rpx" />
  43. </view>
  44. <view>
  45. <!-- 最后一条消息,并且是发送状态显示发送中 -->
  46. <block v-if="chatRecords.length - 1 === index && chatStatus === 1">
  47. <view style="display: flex; align-items: center; gap: 4px; font-size: 32rpx; line-height: 1.8">
  48. <image src="/static/components/agent-ui/imgs/loading.svg" mode="aspectFill" style="width: 14px; height: 14px" />
  49. 请稍等,正在卖力思考中 🤔
  50. </view>
  51. </block>
  52. <block v-else>
  53. <!-- 联网搜索 -->
  54. <FoldedCard v-if="item.search_info" :initStatus="false" :showBgColor="true">
  55. <view slot="title" style="opacity: 0.7; font-size: 14px; display: flex; align-items: center; gap: 8px">
  56. <image src="/static/components/agent-ui/imgs/search.svg" mode="aspectFill" style="width: 36rpx; height: 36rpx" />
  57. <text>已参考 {{ item.search_info.search_results.length }} 个网页</text>
  58. </view>
  59. <view slot="content" class="link-box">
  60. <block v-for="(itemz, index1) in item.search_info.search_results" :key="index1">
  61. <view
  62. @tap="copyUrl"
  63. :data-url="itemz.url"
  64. style="
  65. margin-bottom: 3px;
  66. font-size: 14px;
  67. color: rgb(0, 82, 217);
  68. line-height: 24px;
  69. white-space: nowrap;
  70. overflow: hidden;
  71. text-overflow: ellipsis;
  72. "
  73. >
  74. {{ index1 + 1 }}.{{ itemz.title }}
  75. </view>
  76. </block>
  77. </view>
  78. </FoldedCard>
  79. <!-- 知识库 -->
  80. <view
  81. v-if="item.knowledge_base && item.knowledge_base.length"
  82. style="
  83. border-radius: 8px;
  84. margin-bottom: 12px;
  85. background-color: #f5f5f5;
  86. padding: 18rpx 26rpx;
  87. display: inline-block;
  88. opacity: 0.7;
  89. font-size: 14px;
  90. "
  91. >
  92. 已参考 {{ item.knowledge_base.length }} 个知识库
  93. </view>
  94. <!-- 推理过程 -->
  95. <FoldedCard v-if="!!item.reasoning_content" :initStatus="true" :showBgColor="false">
  96. <view slot="title" style="opacity: 0.7; font-size: 14px; display: flex; align-items: center; gap: 8px">
  97. <image src="/static/components/agent-ui/imgs/system-sum.svg" mode="aspectFill" style="width: 36rpx; height: 36rpx" />
  98. <block v-if="item.pauseThinking">已停止思考</block>
  99. <block v-else>
  100. <text>{{ item.reasoning_content && !item.content ? '思考中...' : '已深度思考(用时' + item.thinkingTime + '秒)' }}</text>
  101. </block>
  102. </view>
  103. <view style="padding-left: 25rpx; margin-top: 28rpx; border-left: rgb(165, 164, 164) solid 2px; opacity: 0.7" slot="content">
  104. <markdownPreview :markdown="item.reasoning_content || ''" :fontSize="28"></markdownPreview>
  105. </view>
  106. </FoldedCard>
  107. <markdownPreview :markdown="item.content || ''"></markdownPreview>
  108. <!-- 下面的按钮 -->
  109. <view style="display: flex; gap: 10px; justify-content: flex" v-if="!item.hiddenBtnGround">
  110. <image
  111. mode="widthFix"
  112. @tap="copyChatRecord"
  113. src="/static/components/agent-ui/imgs/copy.svg"
  114. style="width: 36rpx; height: 36rpx"
  115. :data-content="item.content"
  116. />
  117. <button class="share_btn" open-type="share">
  118. <image mode="widthFix" src="/static/components/agent-ui/imgs/share.svg" style="width: 36rpx; height: 36rpx; vertical-align: top" @tap="share" />
  119. </button>
  120. <block v-if="chatMode === 'bot'">
  121. <image
  122. mode="widthFix"
  123. @tap="openFeedback"
  124. data-feedbackType="upvote"
  125. :data-feedbackRecordId="item.record_id"
  126. src="/static/components/agent-ui/imgs/thumb-up.svg"
  127. style="width: 36rpx; height: 36rpx"
  128. />
  129. <image
  130. mode="widthFix"
  131. @tap="openFeedback"
  132. data-feedbackType="downvote"
  133. :data-feedbackRecordId="item.record_id"
  134. src="/static/components/agent-ui/imgs/thumb-down.svg"
  135. style="width: 36rpx; height: 36rpx"
  136. />
  137. </block>
  138. </view>
  139. </block>
  140. </view>
  141. </view>
  142. <!-- 用户输入 -->
  143. <view class="userContent" v-if="item.role === 'user'">
  144. <view class="user" :style="'padding-left: ' + (showBotAvatar ? 80 : 0) + 'rpx;'">
  145. <view class="user_content">
  146. {{ item.content }}
  147. </view>
  148. </view>
  149. <view class="fileBar">
  150. <chatFile
  151. :enableDel="false"
  152. :fileData="innerItem"
  153. @removeChild="handleRemoveChild"
  154. @changeChild="handleChangeChild"
  155. v-for="(innerItem, index1) in item.fileList"
  156. :key="index1"
  157. ></chatFile>
  158. </view>
  159. </view>
  160. </block>
  161. <!-- 推荐问题 -->
  162. <block v-for="(item, index) in questions" :key="index">
  163. <view class="questions" :style="'padding-left: ' + (showBotAvatar ? 80 : 0) + 'rpx;'">
  164. <view class="question_content" @tap="handleSendMessage" :data-message="item">{{ item }}</view>
  165. </view>
  166. </block>
  167. <view id="scroll-bottom" style="width: 100%; height: 20px"></view>
  168. </scroll-view>
  169. <!-- 底部输入区 -->
  170. <view class="footer">
  171. <view class="feature_list" v-if="showFeatureList">
  172. <view @tap="handleClickWebSearch" :class="'webSearchSwitch ' + (useWebSearch ? 'feature_enable' : '')">
  173. <image :src="useWebSearch ? '/static/components/agent-ui/imgs/internetUse.svg' : '/static/components/agent-ui/imgs/internet.svg'" mode="" style="width: 40rpx; height: 30px; margin-right: 10rpx" />
  174. <text>联网搜索</text>
  175. </view>
  176. </view>
  177. <view class="file_list" v-if="showFileList">
  178. <chatFile
  179. :enableDel="true"
  180. :fileData="item"
  181. @removeChild="handleRemoveChild"
  182. @changeChild="handleChangeChild"
  183. v-for="(item, index) in sendFileList"
  184. :key="index"
  185. ></chatFile>
  186. </view>
  187. <view class="foot_function">
  188. <!-- <scroll-view class="img-box" scroll-x="true" wx:if="{{!!imageList.length}}">
  189. <block wx:for="{{imageList}}" wx:key="tempFilePath">
  190. <view class="img-preview">
  191. <image src="{{item.tempFilePath}}" alt="" model='aspectFill' class="img-preview-image" />
  192. <view class="img-preview-loading" wx:if="{{!!!item.base64Url}}"></view>
  193. <image src="./imgs/close.svg" mode="aspectFill" class="img-preview-close" bind:tap="deleteImg" data-index="{{index}}" />
  194. </view>
  195. </block>
  196. </scroll-view> -->
  197. <view class="input_box">
  198. <view class="input_inner_box">
  199. <textarea
  200. class="input"
  201. :value="inputValue"
  202. maxlength="1024"
  203. @focus="bindInputFocus"
  204. @input="bindKeyInput"
  205. placeholder="说点什么吧"
  206. @confirm="sendMessage"
  207. confirm-type="send"
  208. adjust-position
  209. cursor-spacing="40"
  210. :auto-height="true"
  211. :show-confirm-bar="false"
  212. @linechange="handleLineChange"
  213. />
  214. </view>
  215. <view class="right_btns">
  216. <!-- 加号 -->
  217. <image src="/static/components/agent-ui/imgs/set.svg" class="set" mode="widthFix" @tap="handleClickTools" />
  218. <!-- 发送按钮 -->
  219. <image
  220. src="/static/components/agent-ui/imgs/send.svg"
  221. class="set"
  222. mode="widthFix"
  223. v-if="!!inputValue && chatStatus === 0"
  224. @tap="sendMessage"
  225. style="transform: rotate(-40deg); transform-origin: 8px 8px"
  226. />
  227. <!-- 暂停按钮 -->
  228. <image src="/static/components/agent-ui/imgs/stop.svg" class="set" mode="widthFix" v-if="!(chatStatus === 0)" @tap="stop" />
  229. </view>
  230. </view>
  231. </view>
  232. <!-- 底部工具栏 -->
  233. <view class="tool_box" v-if="showTools">
  234. <view class="function" @tap="clearChatRecords">
  235. <image src="/static/components/agent-ui/imgs/clear.svg" alt="widthFix" class="icon" />
  236. <text class="text_desc">清除</text>
  237. </view>
  238. <view v-if="showUploadImg && chatMode === 'bot'" class="function" @tap="handleAlbum">
  239. <image src="/static/components/agent-ui/imgs/uploadImg.svg" alt="widthFix" class="icon" />
  240. <text class="text_desc">图片</text>
  241. </view>
  242. <view v-if="showUploadFile && chatMode === 'bot'" class="function" @tap="handleUploadMessageFile">
  243. <image src="/static/components/agent-ui/imgs/wechat.svg" alt="widthFix" class="icon" />
  244. <text class="text_desc">微信文件</text>
  245. </view>
  246. <view v-if="showUploadImg && chatMode === 'bot'" class="function" @tap="handleCamera">
  247. <image src="/static/components/agent-ui/imgs/camera.svg" alt="widthFix" class="icon" />
  248. <text class="text_desc">相机</text>
  249. </view>
  250. </view>
  251. </view>
  252. <image
  253. @tap="autoToBottom"
  254. v-if="manualScroll"
  255. style="width: 35px; height: 35px; border-radius: 50px; position: absolute; bottom: 150px; right: 20px; padding: 5px; background-color: white"
  256. src="/static/components/agent-ui/imgs/toBottom.svg"
  257. mode="aspectFit"
  258. @error=""
  259. @load=""
  260. />
  261. <feedback
  262. :input="input"
  263. :aiAnswer="aiAnswer"
  264. :isShowFeedback="isShowFeedback"
  265. @close="closefeedback"
  266. :feedbackRecordId="feedbackRecordId"
  267. :feedbackType="feedbackType"
  268. :botId="bot.botId"
  269. ></feedback>
  270. </view>
  271. </template>
  272. <script>
  273. import markdownPreview from '@/components/agent-ui/wd-markdown/index';
  274. import FoldedCard from '@/components/agent-ui/collapse/index';
  275. import chatFile from '@/components/agent-ui/chatFile/index';
  276. import feedback from '@/components/agent-ui/feedback/index';
  277. // components/agent-ui/index.js
  278. import { checkConfig, randomSelectInitquestion } from './tools';
  279. import md5 from './md5.js';
  280. export default {
  281. components: {
  282. markdownPreview,
  283. FoldedCard,
  284. chatFile,
  285. feedback
  286. },
  287. data() {
  288. return {
  289. isLoading: true,
  290. // 判断是否尚在加载中
  291. article: {},
  292. windowInfo: uni.getWindowInfo(),
  293. bot: {
  294. name: '',
  295. avatar: '',
  296. botId: ''
  297. },
  298. inputValue: '',
  299. output: '',
  300. chatRecords: [],
  301. scrollTop: 0,
  302. setPanelVisibility: false,
  303. questions: [],
  304. scrollTop: 0,
  305. scrollTop: 0,
  306. // 文字撑起来后能滚动的最大高度
  307. viewTop: 0,
  308. // 根据实际情况,可能用户手动滚动,需要记录当前滚动的位置
  309. scrollTo: '',
  310. // 快速定位到指定元素,置底用
  311. scrollTimer: null,
  312. //
  313. manualScroll: false,
  314. // 当前为手动滚动/自动滚动
  315. showTools: false,
  316. // 展示底部工具栏
  317. showFileList: false,
  318. // 展示输入框顶部文件行
  319. showTopBar: false,
  320. // 展示顶部bar
  321. sendFileList: [],
  322. footerHeight: 73,
  323. lastScrollTop: 0,
  324. showUploadFile: true,
  325. showUploadImg: false,
  326. showWebSearchSwitch: false,
  327. showPullRefresh: true,
  328. useWebSearch: false,
  329. showFeatureList: false,
  330. chatStatus: 0,
  331. // 页面状态: 0-正常状态,可输入,可发送, 1-发送中 2-思考中 3-输出content中
  332. triggered: false,
  333. page: 1,
  334. size: 10,
  335. total: 0,
  336. refreshText: '下拉加载历史记录',
  337. contentHeightInScrollViewTop: 0,
  338. // scroll区域顶部固定区域高度
  339. shouldAddScrollTop: false,
  340. isShowFeedback: false,
  341. feedbackRecordId: '',
  342. feedbackType: '',
  343. textareaHeight: 50,
  344. curLineCount: 1,
  345. aiAnswer: '',
  346. input: '',
  347. innerItem: ''
  348. };
  349. },
  350. props: {
  351. chatMode: {
  352. type: String,
  353. default: ''
  354. },
  355. showBotAvatar: {
  356. type: Boolean,
  357. default: false
  358. },
  359. presentationMode: {
  360. type: String,
  361. default: ''
  362. },
  363. agentConfig: {
  364. type: Object,
  365. default: () => ({
  366. botId: String,
  367. allowUploadFile: Boolean,
  368. allowWebSearch: Boolean,
  369. // allowUploadImage: Boolean,
  370. allowPullRefresh: Boolean
  371. })
  372. },
  373. modelConfig: {
  374. type: Object,
  375. default: () => ({
  376. modelProvider: String,
  377. quickResponseModel: String,
  378. // deepReasoningModel: String, // 待支持
  379. logo: String,
  380. welcomeMsg: String
  381. })
  382. }
  383. },
  384. watch: {
  385. showWebSearchSwitch: function (showWebSearchSwitch) {
  386. this.setData({
  387. showFeatureList: showWebSearchSwitch
  388. });
  389. },
  390. showTools: function (isShow) {
  391. // console.log('showTools', isShow)
  392. if (isShow) {
  393. this.setData({
  394. footerHeight: this.footerHeight + 80
  395. });
  396. } else {
  397. this.setData({
  398. footerHeight: this.footerHeight - 80
  399. });
  400. }
  401. },
  402. showFileList: function (isShow) {
  403. console.log('showFileList', isShow);
  404. if (isShow) {
  405. this.setData({
  406. footerHeight: this.footerHeight + 80
  407. });
  408. } else {
  409. this.setData({
  410. footerHeight: this.footerHeight - 80
  411. });
  412. }
  413. },
  414. showFeatureList: function (isShow) {
  415. console.log('showFeatureList', isShow);
  416. if (isShow) {
  417. this.setData({
  418. footerHeight: this.footerHeight + 30
  419. });
  420. } else {
  421. const subHeight = this.footerHeight - 30;
  422. this.setData({
  423. footerHeight: subHeight >= 80 ? subHeight : 80
  424. });
  425. }
  426. }
  427. },
  428. mounted() {
  429. // 处理小程序 attached 生命周期
  430. this.attached();
  431. },
  432. methods: {
  433. attached: async function () {
  434. const chatMode = this.chatMode;
  435. // 检查配置
  436. const [check, message] = checkConfig(chatMode, this.agentConfig, this.modelConfig);
  437. if (!check) {
  438. uni.showModal({
  439. title: '提示',
  440. content: message
  441. });
  442. return;
  443. }
  444. if (chatMode === 'bot') {
  445. const { botId } = this.agentConfig;
  446. const ai = wx.cloud.extend.AI;
  447. const bot = await ai.bot.get({
  448. botId
  449. });
  450. // 新增错误提示
  451. if (bot.code) {
  452. uni.showModal({
  453. title: '提示',
  454. content: bot.message
  455. });
  456. return;
  457. }
  458. // 初始化第一条记录为welcomeMessage
  459. const record = {
  460. content: bot.welcomeMessage,
  461. record_id: 'record_id' + String(+new Date() + 10),
  462. role: 'assistant',
  463. hiddenBtnGround: true
  464. };
  465. const { chatRecords } = this;
  466. // 随机选取三个初始化问题
  467. const questions = randomSelectInitquestion(bot.initQuestions, 3);
  468. let { allowWebSearch, allowUploadFile, allowPullRefresh } = this.agentConfig;
  469. console.log('allowWebSearch', allowWebSearch);
  470. allowWebSearch = allowWebSearch === undefined ? true : allowWebSearch;
  471. allowUploadFile = allowUploadFile === undefined ? true : allowUploadFile;
  472. allowPullRefresh = allowPullRefresh === undefined ? true : allowPullRefresh;
  473. console.log('allowUploadFile', allowUploadFile);
  474. console.error(chatRecords);
  475. this.setData({
  476. bot,
  477. questions,
  478. chatRecords: [...chatRecords, record],
  479. showWebSearchSwitch: !!(bot.searchEnable && allowWebSearch),
  480. showUploadFile: allowUploadFile,
  481. showPullRefresh: allowPullRefresh
  482. });
  483. }
  484. const topHeight = await this.calculateContentInTop();
  485. // console.log('topHeight', topHeight)
  486. this.setData({
  487. contentHeightInScrollViewTop: topHeight
  488. });
  489. },
  490. handleLineChange: function (e) {
  491. console.log('linechange', e.detail.lineCount);
  492. // 查foot-function height
  493. const that = this;
  494. const query = uni.createSelectorQuery().in(this);
  495. query
  496. .select('.foot_function')
  497. .boundingClientRect(function (res) {
  498. if (res) {
  499. if (res.height < that.textareaHeight) {
  500. that.setData({
  501. footerHeight: that.footerHeight - (that.textareaHeight - res.height)
  502. });
  503. }
  504. if (res.height > that.textareaHeight) {
  505. that.setData({
  506. footerHeight: that.footerHeight + (res.height - that.textareaHeight)
  507. });
  508. }
  509. that.setData({
  510. textareaHeight: res.height
  511. });
  512. } else {
  513. console.log('未找到指定元素');
  514. }
  515. })
  516. .exec();
  517. },
  518. openFeedback: function (e) {
  519. const { feedbackrecordid, feedbacktype } = e.currentTarget.dataset;
  520. let index = null;
  521. this.chatRecords.forEach((item, _index) => {
  522. if (item.record_id === feedbackrecordid) {
  523. index = _index;
  524. }
  525. });
  526. const inputRecord = this.chatRecords[index - 1];
  527. const answerRecord = this.chatRecords[index];
  528. // console.log(record)
  529. this.setData({
  530. isShowFeedback: true,
  531. feedbackRecordId: feedbackrecordid,
  532. feedbackType: feedbacktype,
  533. aiAnswer: answerRecord.content,
  534. input: inputRecord.content
  535. });
  536. },
  537. closefeedback: function () {
  538. this.setData({
  539. isShowFeedback: false,
  540. feedbackRecordId: '',
  541. feedbackType: ''
  542. });
  543. },
  544. // 滚动相关处理
  545. calculateContentHeight() {
  546. return new Promise((resolve) => {
  547. const query = uni.createSelectorQuery().in(this);
  548. query
  549. .selectAll('.main >>> .system, .main >>> .userContent')
  550. .boundingClientRect((rects) => {
  551. let totalHeight = 0;
  552. rects.forEach((rect) => {
  553. totalHeight += rect.height;
  554. });
  555. resolve(totalHeight);
  556. })
  557. .exec();
  558. });
  559. },
  560. calculateContentInTop() {
  561. // console.log('执行top 部分计算')
  562. return new Promise((resolve) => {
  563. const query = uni.createSelectorQuery().in(this);
  564. query
  565. .selectAll('.main >>> .nav, .main >>> .tips')
  566. .boundingClientRect((rects) => {
  567. let totalHeight = 0;
  568. rects.forEach((rect) => {
  569. totalHeight += rect.height;
  570. });
  571. // console.log('top height', totalHeight);
  572. resolve(totalHeight);
  573. })
  574. .exec();
  575. });
  576. },
  577. onWheel: function (e) {
  578. // 解决小程序开发工具中滑动
  579. if (!this.manualScroll && e.detail.deltaY < 0) {
  580. this.setData({
  581. manualScroll: true
  582. });
  583. }
  584. },
  585. onScroll: function (e) {
  586. if (e.detail.scrollTop < this.lastScrollTop) {
  587. // 鸿蒙系统上可能滚动事件,拖动事件失效,兜底处理
  588. this.setData({
  589. manualScroll: true
  590. });
  591. }
  592. this.setData({
  593. lastScrollTop: e.detail.scrollTop
  594. });
  595. // 针对连续滚动的最后一次进行处理,scroll-view的 scroll end事件不好判定
  596. if (this.scrollTimer) {
  597. clearTimeout(this.scrollTimer);
  598. }
  599. this.setData({
  600. scrollTimer: setTimeout(() => {
  601. const newTop = Math.max(this.scrollTop, e.detail.scrollTop);
  602. if (this.manualScroll) {
  603. this.setData({
  604. scrollTop: newTop
  605. });
  606. } else {
  607. this.setData({
  608. scrollTop: newTop,
  609. viewTop: newTop
  610. });
  611. }
  612. }, 100)
  613. });
  614. },
  615. handleScrollStart: function (e) {
  616. // console.log("drag start", e);
  617. if (e.detail.scrollTop > 0 && !this.manualScroll) {
  618. // 手动开始滚
  619. this.setData({
  620. manualScroll: true
  621. });
  622. }
  623. },
  624. handleScrollToLower: function (e) {
  625. // console.log("scroll to lower", e);
  626. // 到底转自动
  627. this.setData({
  628. manualScroll: false
  629. });
  630. },
  631. autoToBottom: function () {
  632. // console.log("autoToBottom");
  633. this.setData({
  634. manualScroll: false,
  635. scrollTo: 'scroll-bottom'
  636. });
  637. // console.log('scrollTop', this.data.scrollTop);
  638. },
  639. bindInputFocus: function (e) {
  640. this.setData({
  641. manualScroll: false
  642. });
  643. this.autoToBottom();
  644. },
  645. bindKeyInput: function (e) {
  646. this.setData({
  647. inputValue: e.detail.value
  648. });
  649. },
  650. handelRefresh: function (e) {
  651. this.setData(
  652. {
  653. triggered: true,
  654. refreshText: '刷新中'
  655. },
  656. async () => {
  657. // 模拟请求回数据后 停止加载
  658. // console.log('this.data.agentConfig.type', this.data.agentConfig.type)
  659. if (this.chatMode === 'bot') {
  660. // 判断当前是否大于一条 (一条则为系统默认提示,直接从库里拉出最近的一页)
  661. if (this.chatRecords.length > 1) {
  662. const newPage = Math.floor(this.chatRecords.length / this.size) + 1;
  663. this.setData({
  664. page: newPage
  665. });
  666. }
  667. const res = await wx.cloud.extend.AI.bot.getChatRecords({
  668. botId: this.agentConfig.botId,
  669. pageNumber: this.page,
  670. pageSize: this.size,
  671. sort: 'desc'
  672. });
  673. if (res.recordList) {
  674. this.setData({
  675. total: res.total
  676. });
  677. // 找出新获取的一页中,不在内存中的数据
  678. const freshNum = this.size - ((this.chatRecords.length - 1) % this.size);
  679. const freshChatRecords = res.recordList
  680. .reverse()
  681. .slice(0, freshNum)
  682. .map((item) => ({
  683. ...item,
  684. record_id: item.recordId
  685. }));
  686. this.setData({
  687. chatRecords: [...freshChatRecords, ...this.chatRecords]
  688. });
  689. console.log('totalChatRecords', this.chatRecords);
  690. }
  691. this.setData({
  692. triggered: false,
  693. refreshText: '下拉加载历史记录'
  694. });
  695. }
  696. }
  697. );
  698. },
  699. clearChatRecords: function () {
  700. const chatMode = this.chatMode;
  701. const { bot } = this;
  702. this.setData({
  703. showTools: false
  704. });
  705. if (chatMode === 'model') {
  706. this.setData({
  707. chatRecords: [],
  708. chatStatus: 0
  709. });
  710. return;
  711. }
  712. // 只有一条不需要清
  713. if (this.chatRecords.length === 1) {
  714. return;
  715. }
  716. const record = {
  717. content: bot.welcomeMessage,
  718. record_id: 'record_id' + String(+new Date() + 10),
  719. role: 'assistant',
  720. hiddenBtnGround: true
  721. };
  722. const questions = randomSelectInitquestion(bot.initQuestions, 3);
  723. this.setData({
  724. chatRecords: [record],
  725. chatStatus: 0,
  726. questions,
  727. page: 1 // 重置分页页码
  728. });
  729. },
  730. chooseMedia: function (sourceType) {
  731. const that = this;
  732. uni.chooseMedia({
  733. count: 1,
  734. mediaType: ['image'],
  735. sourceType: [sourceType],
  736. maxDuration: 30,
  737. camera: 'back',
  738. success(res) {
  739. console.log('res', res);
  740. console.log('tempFiles', res.tempFiles);
  741. const isImageSizeValid = res.tempFiles.every((item) => item.size <= 31457280);
  742. if (!isImageSizeValid) {
  743. uni.showToast({
  744. title: '图片大小30M限制',
  745. icon: 'error'
  746. });
  747. return;
  748. }
  749. const tempFiles = res.tempFiles.map((item) => {
  750. const tempFileInfos = item.tempFilePath.split('.');
  751. const tempFileName = md5(tempFileInfos[0]) + '.' + tempFileInfos[1];
  752. return {
  753. tempId: tempFileName,
  754. rawType: item.fileType,
  755. // 微信选择默认的文件类型 image/video/file
  756. tempFileName: tempFileName,
  757. // 文件名
  758. rawFileName: '',
  759. // 图片类不带源文件名
  760. tempPath: item.tempFilePath,
  761. fileSize: item.size,
  762. fileUrl: '',
  763. fileId: '',
  764. botId: that.agentConfig.botId,
  765. parsed: false
  766. };
  767. });
  768. const finalFileList = [...tempFiles];
  769. console.log('final', finalFileList);
  770. that.setData({
  771. sendFileList: finalFileList //
  772. });
  773. if (finalFileList.length) {
  774. that.setData({
  775. showTools: false
  776. });
  777. if (!that.showFileList) {
  778. that.setData({
  779. showFileList: true
  780. });
  781. }
  782. }
  783. }
  784. });
  785. },
  786. handleUploadImg: function (sourceType) {
  787. const that = this;
  788. const isCurSendFile = this.sendFileList.find((item) => item.rawType === 'file');
  789. if (isCurSendFile) {
  790. uni.showModal({
  791. title: '确认替换吗',
  792. content: '上传图片将替换当前文件内容',
  793. showCancel: 'true',
  794. cancelText: '取消',
  795. confirmText: '确认',
  796. success(res) {
  797. console.log('res', res);
  798. that.chooseMedia(sourceType);
  799. },
  800. fail(error) {
  801. console.log('choose file e', error);
  802. }
  803. });
  804. } else {
  805. that.chooseMedia(sourceType);
  806. }
  807. },
  808. chooseMessageFile: function () {
  809. console.log('触发choose');
  810. const that = this;
  811. const oldFileLen = this.sendFileList.filter((item) => item.rawType === 'file').length;
  812. console.log('oldFileLen', oldFileLen);
  813. const subFileCount = oldFileLen <= 5 ? 5 - oldFileLen : 0;
  814. if (subFileCount === 0) {
  815. uni.showToast({
  816. title: '文件数量限制5个',
  817. icon: 'error'
  818. });
  819. return;
  820. }
  821. uni.chooseMessageFile({
  822. count: subFileCount,
  823. type: 'file',
  824. success(res) {
  825. // tempFilePath可以作为img标签的src属性显示图片
  826. // const tempFilePaths = res.tempFiles;
  827. console.log('res', res);
  828. // 检验文件后缀
  829. const isFileExtValid = res.tempFiles.every((item) => that.checkFileExt(item.name.split('.')[1]));
  830. if (!isFileExtValid) {
  831. uni.showModal({
  832. content: '当前支持文件类型为 pdf、txt、doc、docx、ppt、pptx、xls、xlsx、csv',
  833. showCancel: false,
  834. confirmText: '确定'
  835. });
  836. return;
  837. }
  838. // 校验各文件大小是否小于10M
  839. const isFileSizeValid = res.tempFiles.every((item) => item.size <= 10485760);
  840. if (!isFileSizeValid) {
  841. uni.showToast({
  842. title: '单文件10M限制',
  843. icon: 'error'
  844. });
  845. return;
  846. }
  847. const tempFiles = res.tempFiles.map((item) => {
  848. const tempFileInfos = item.path.split('.');
  849. const tempFileName = md5(tempFileInfos[0]) + '.' + tempFileInfos[1];
  850. return {
  851. tempId: tempFileName,
  852. rawType: item.type,
  853. // 微信选择默认的文件类型 image/video/file
  854. tempFileName: tempFileName,
  855. // 文件名
  856. rawFileName: item.name,
  857. tempPath: item.path,
  858. fileSize: item.size,
  859. fileUrl: '',
  860. fileId: '',
  861. botId: that.agentConfig.botId,
  862. parsed: false
  863. };
  864. });
  865. // 过滤掉已选择中的 image 文件(保留file)
  866. const filterFileList = that.sendFileList.filter((item) => item.rawType !== 'image');
  867. const finalFileList = [...filterFileList, ...tempFiles];
  868. console.log('final', finalFileList);
  869. that.setData({
  870. sendFileList: finalFileList //
  871. });
  872. if (finalFileList.length) {
  873. that.setData({
  874. showTools: false
  875. });
  876. if (!that.showFileList) {
  877. that.setData({
  878. showFileList: true
  879. });
  880. }
  881. }
  882. },
  883. fail(e) {
  884. console.log('choose e', e);
  885. }
  886. });
  887. },
  888. handleUploadMessageFile: function () {
  889. if (this.useWebSearch) {
  890. uni.showModal({
  891. title: '提示',
  892. content: '联网搜索不支持上传附件'
  893. });
  894. return;
  895. }
  896. const that = this;
  897. const isCurSendImage = this.sendFileList.find((item) => item.rawType === 'image');
  898. if (isCurSendImage) {
  899. uni.showModal({
  900. title: '确认替换吗',
  901. content: '上传文件将替换当前图片内容',
  902. showCancel: 'true',
  903. cancelText: '取消',
  904. confirmText: '确认',
  905. success(res) {
  906. console.log('res', res);
  907. that.chooseMessageFile();
  908. },
  909. fail(error) {
  910. console.log('choose file e', error);
  911. }
  912. });
  913. } else {
  914. that.chooseMessageFile();
  915. }
  916. },
  917. handleAlbum: function () {
  918. this.handleUploadImg('album');
  919. },
  920. handleCamera: function () {
  921. this.handleUploadImg('camera');
  922. },
  923. checkFileExt: function (ext) {
  924. return ['pdf', 'txt', 'doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx', 'csv'].includes(ext);
  925. },
  926. stop: function () {
  927. this.autoToBottom();
  928. const { chatRecords, chatStatus } = this;
  929. const newChatRecords = [...chatRecords];
  930. const record = newChatRecords[newChatRecords.length - 1];
  931. if (chatStatus === 1) {
  932. record.content = '已暂停生成';
  933. }
  934. // 暂停思考
  935. if (chatStatus === 2) {
  936. record.pauseThinking = true;
  937. }
  938. this.setData({
  939. chatRecords: newChatRecords,
  940. manualScroll: false,
  941. chatStatus: 0 // 暂停之后切回正常状态
  942. });
  943. },
  944. openSetPanel: function () {
  945. this.setData({
  946. setPanelVisibility: true
  947. });
  948. },
  949. closeSetPanel: function () {
  950. this.setData({
  951. setPanelVisibility: false
  952. });
  953. },
  954. handleSendMessage: async function (event) {
  955. // 发送消息前校验所有文件上传状态
  956. if (this.sendFileList.some((item) => !item.fileId || !item.parsed)) {
  957. uni.showToast({
  958. title: '文件上传及解析中',
  959. icon: 'error'
  960. });
  961. return;
  962. }
  963. await this.sendMessage(event);
  964. },
  965. sendMessage: async function (event) {
  966. if (this.showFileList) {
  967. this.setData({
  968. showFileList: !this.showFileList
  969. });
  970. }
  971. if (this.showTools) {
  972. this.setData({
  973. showTools: !this.showTools
  974. });
  975. }
  976. const { message } = event.currentTarget.dataset;
  977. let { inputValue, bot, agentConfig, chatRecords, chatStatus, modelConfig } = this;
  978. // 如果正在进行对话,不让发送消息
  979. if (chatStatus !== 0) {
  980. return;
  981. }
  982. // 将传进来的消息给到inputValue
  983. if (message) {
  984. inputValue = message;
  985. }
  986. // 空消息返回
  987. if (!inputValue) {
  988. return;
  989. }
  990. const { modelProvider, quickResponseModel } = modelConfig;
  991. const chatMode = this.chatMode;
  992. // console.log(inputValue,bot.botId)
  993. const userRecord = {
  994. content: inputValue,
  995. record_id: 'record_id' + String(+new Date() - 10),
  996. role: 'user',
  997. fileList: this.sendFileList
  998. };
  999. if (this.sendFileList.length) {
  1000. this.setData({
  1001. sendFileList: []
  1002. });
  1003. }
  1004. const record = {
  1005. content: '',
  1006. record_id: 'record_id' + String(+new Date() + 10),
  1007. role: 'assistant',
  1008. hiddenBtnGround: true
  1009. };
  1010. this.setData({
  1011. inputValue: '',
  1012. questions: [],
  1013. chatRecords: [...chatRecords, userRecord, record],
  1014. chatStatus: 1 // 聊天状态切换为1发送中
  1015. });
  1016. // 新增一轮对话记录时 自动往下滚底
  1017. this.autoToBottom();
  1018. if (chatMode === 'bot') {
  1019. const ai = wx.cloud.extend.AI;
  1020. const res = await ai.bot.sendMessage({
  1021. data: {
  1022. botId: bot.botId,
  1023. msg: inputValue,
  1024. files: this.showUploadFile ? userRecord.fileList.map((item) => item.fileId) : undefined,
  1025. searchEnable: this.useWebSearch
  1026. }
  1027. });
  1028. let contentText = '';
  1029. let reasoningContentText = '';
  1030. let isManuallyPaused = false; //这个标记是为了处理手动暂停时,不要请求推荐问题,不显示下面的按钮
  1031. let startTime = null; //记录开始思考时间
  1032. let endTime = null; // 记录结束思考时间
  1033. let index = 0;
  1034. for await (let event of res.eventStream) {
  1035. const { chatStatus } = this;
  1036. if (chatStatus === 0) {
  1037. isManuallyPaused = true;
  1038. break;
  1039. }
  1040. if (index % 10 === 0) {
  1041. // 更新频率降为1/10
  1042. this.toBottom(40);
  1043. }
  1044. const { data } = event;
  1045. try {
  1046. const dataJson = JSON.parse(data);
  1047. const { type, content, reasoning_content, record_id, search_info, role, knowledge_meta, knowledge_base, finish_reason } = dataJson;
  1048. const newValue = [...this.chatRecords];
  1049. // 取最后一条消息更新
  1050. const lastValueIndex = newValue.length - 1;
  1051. const lastValue = newValue[lastValueIndex];
  1052. lastValue.role = role || 'assistant';
  1053. lastValue.record_id = record_id;
  1054. // 优先处理错误,直接中断
  1055. if (finish_reason === 'error' || finish_reason === 'content_filter') {
  1056. lastValue.search_info = null;
  1057. lastValue.reasoning_content = '';
  1058. lastValue.knowledge_meta = [];
  1059. lastValue.content = '网络繁忙,请稍后重试!';
  1060. this.setData({
  1061. [`chatRecords[${lastValueIndex}].search_info`]: lastValue.search_info,
  1062. [`chatRecords[${lastValueIndex}].reasoning_content`]: lastValue.reasoning_content,
  1063. [`chatRecords[${lastValueIndex}].knowledge_meta`]: lastValue.knowledge_meta,
  1064. [`chatRecords[${lastValueIndex}].content`]: lastValue.content,
  1065. [`chatRecords[${lastValueIndex}].record_id`]: lastValue.record_id
  1066. });
  1067. break;
  1068. }
  1069. // 下面根据type来确定输出的内容
  1070. // 只更新一次参考文献,后续再收到这样的消息丢弃
  1071. if (type === 'search' && !lastValue.search_info) {
  1072. lastValue.search_info = search_info;
  1073. this.setData({
  1074. chatStatus: 2,
  1075. [`chatRecords[${lastValueIndex}].search_info`]: lastValue.search_info,
  1076. [`chatRecords[${lastValueIndex}].record_id`]: lastValue.record_id
  1077. }); // 聊天状态切换为思考中,展示联网的信息
  1078. }
  1079. // 思考过程
  1080. if (type === 'thinking') {
  1081. if (!startTime) {
  1082. startTime = +new Date();
  1083. endTime = +new Date();
  1084. } else {
  1085. endTime = +new Date();
  1086. }
  1087. reasoningContentText += reasoning_content;
  1088. lastValue.reasoning_content = reasoningContentText;
  1089. lastValue.thinkingTime = Math.floor((endTime - startTime) / 1000);
  1090. this.setData({
  1091. [`chatRecords[${lastValueIndex}].reasoning_content`]: lastValue.reasoning_content,
  1092. [`chatRecords[${lastValueIndex}].thinkingTime`]: lastValue.thinkingTime,
  1093. [`chatRecords[${lastValueIndex}].record_id`]: lastValue.record_id,
  1094. chatStatus: 2
  1095. }); // 聊天状态切换为思考中
  1096. }
  1097. // 内容
  1098. if (type === 'text') {
  1099. contentText += content;
  1100. lastValue.content = contentText;
  1101. this.setData({
  1102. [`chatRecords[${lastValueIndex}].content`]: lastValue.content,
  1103. [`chatRecords[${lastValueIndex}].record_id`]: lastValue.record_id,
  1104. chatStatus: 3
  1105. }); // 聊天状态切换为输出content中
  1106. }
  1107. // 知识库,只更新一次
  1108. if (type === 'knowledge' && !lastValue.knowledge_meta) {
  1109. // console.log('ryan',knowledge_base)
  1110. lastValue.knowledge_base = knowledge_base;
  1111. this.setData({
  1112. [`chatRecords[${lastValueIndex}].knowledge_base`]: lastValue.knowledge_base,
  1113. chatStatus: 2
  1114. });
  1115. }
  1116. } catch (e) {
  1117. console.log('CatchClause', e);
  1118. console.log('CatchClause', e);
  1119. // console.log('err', event, e)
  1120. break;
  1121. }
  1122. index++;
  1123. }
  1124. this.toBottom(40);
  1125. const newValue = [...this.chatRecords];
  1126. const lastValueIndex = newValue.length - 1;
  1127. // 取最后一条消息更新
  1128. const lastValue = newValue[lastValueIndex];
  1129. lastValue.hiddenBtnGround = isManuallyPaused;
  1130. this.setData({
  1131. chatStatus: 0,
  1132. [`chatRecords[${lastValueIndex}].hiddenBtnGround`]: isManuallyPaused
  1133. }); // 对话完成,切回0 ,并且修改最后一条消息的状态,让下面的按钮展示
  1134. if (bot.isNeedRecommend && !isManuallyPaused) {
  1135. const ai = wx.cloud.extend.AI;
  1136. const chatRecords = this.chatRecords;
  1137. const lastPairChatRecord = chatRecords.length >= 2 ? chatRecords.slice(chatRecords.length - 2) : [];
  1138. const recommendRes = await ai.bot.getRecommendQuestions({
  1139. data: {
  1140. botId: bot.botId,
  1141. history: lastPairChatRecord.map((item) => ({
  1142. role: item.role,
  1143. content: item.content
  1144. })),
  1145. msg: inputValue,
  1146. agentSetting: '',
  1147. introduction: '',
  1148. name: ''
  1149. }
  1150. });
  1151. let result = '';
  1152. for await (let str of recommendRes.textStream) {
  1153. // this.toBottom();
  1154. this.toBottom();
  1155. result += str;
  1156. this.setData({
  1157. questions: result.split('\n').filter((item) => !!item)
  1158. });
  1159. }
  1160. }
  1161. }
  1162. if (chatMode === 'model') {
  1163. const aiModel = wx.cloud.extend.AI.createModel(modelProvider);
  1164. const res = await aiModel.streamText({
  1165. data: {
  1166. model: quickResponseModel,
  1167. messages: [
  1168. ...chatRecords.map((item) => ({
  1169. role: item.role,
  1170. content: item.content
  1171. })),
  1172. {
  1173. role: 'user',
  1174. content: inputValue
  1175. }
  1176. ]
  1177. }
  1178. });
  1179. let contentText = '';
  1180. let reasoningText = '';
  1181. let chatStatus = 2;
  1182. let isManuallyPaused = false;
  1183. let startTime = null; //记录开始思考时间
  1184. let endTime = null; // 记录结束思考时间
  1185. for await (let event of res.eventStream) {
  1186. if (this.chatStatus === 0) {
  1187. isManuallyPaused = true;
  1188. break;
  1189. }
  1190. this.toBottom();
  1191. const { data } = event;
  1192. try {
  1193. const dataJson = JSON.parse(data);
  1194. const { id, choices = [] } = dataJson || {};
  1195. const { delta, finish_reason } = choices[0] || {};
  1196. if (finish_reason === 'stop') {
  1197. break;
  1198. }
  1199. const { content, reasoning_content, role } = delta;
  1200. reasoningText += reasoning_content || '';
  1201. contentText += content || '';
  1202. const newValue = [...this.chatRecords];
  1203. const lastValue = newValue[newValue.length - 1];
  1204. lastValue.content = contentText;
  1205. lastValue.reasoning_content = reasoningText;
  1206. lastValue.record_id = 'record_id' + String(id);
  1207. if (!!reasoningText && !contentText) {
  1208. // 推理中
  1209. chatStatus = 2;
  1210. if (!startTime) {
  1211. startTime = +new Date();
  1212. endTime = +new Date();
  1213. } else {
  1214. endTime = +new Date();
  1215. }
  1216. } else {
  1217. chatStatus = 3;
  1218. }
  1219. lastValue.thinkingTime = endTime ? Math.floor((endTime - startTime) / 1000) : 0;
  1220. this.setData({
  1221. chatRecords: newValue,
  1222. chatStatus
  1223. });
  1224. } catch (e) {
  1225. console.log('CatchClause', e);
  1226. console.log('CatchClause', e);
  1227. // console.log(e, event)
  1228. break;
  1229. }
  1230. }
  1231. const newValue = [...this.chatRecords];
  1232. const lastValue = newValue[newValue.length - 1];
  1233. lastValue.hiddenBtnGround = isManuallyPaused; // 用户手动暂停,不显示下面的按钮
  1234. this.setData({
  1235. chatRecords: newValue,
  1236. chatStatus: 0
  1237. }); // 回正
  1238. }
  1239. },
  1240. toBottom: async function (unit) {
  1241. const addUnit = unit === undefined ? 4 : unit;
  1242. if (this.shouldAddScrollTop) {
  1243. const newTop = this.scrollTop + addUnit;
  1244. if (this.manualScroll) {
  1245. this.setData({
  1246. scrollTop: newTop
  1247. });
  1248. } else {
  1249. this.setData({
  1250. scrollTop: newTop,
  1251. viewTop: newTop
  1252. });
  1253. }
  1254. return;
  1255. }
  1256. // 只有当内容高度接近scroll 区域视口高度时才开始增加 scrollTop
  1257. const clientHeight = this.windowInfo.windowHeight - this.footerHeight - (this.chatMode === 'bot' ? 40 : 0); // 视口高度
  1258. const contentHeight = (await this.calculateContentHeight()) + (this.contentHeightInScrollViewTop || (await this.calculateContentInTop())); // 内容总高度
  1259. // console.log(
  1260. // 'contentHeight clientHeight newTop',
  1261. // contentHeight,
  1262. // clientHeight,
  1263. // this.data.scrollTop + 4
  1264. // );
  1265. if (clientHeight - contentHeight < 10) {
  1266. this.setData({
  1267. shouldAddScrollTop: true
  1268. });
  1269. }
  1270. },
  1271. copyChatRecord: function (e) {
  1272. // console.log(e)
  1273. const { content } = e.currentTarget.dataset;
  1274. uni.setClipboardData({
  1275. data: content,
  1276. success: function (res) {
  1277. uni.showToast({
  1278. title: '复制成功',
  1279. icon: 'success'
  1280. });
  1281. }
  1282. });
  1283. },
  1284. addFileList: function () {
  1285. // 顶部文件行展现时,隐藏底部工具栏
  1286. this.setData({});
  1287. },
  1288. subFileList: function () {},
  1289. copyUrl: function (e) {
  1290. const { url } = e.currentTarget.dataset;
  1291. console.log(url);
  1292. uni.setClipboardData({
  1293. data: url,
  1294. success: function (res) {
  1295. uni.showToast({
  1296. title: '网页链接复制成功',
  1297. icon: 'success'
  1298. });
  1299. }
  1300. });
  1301. },
  1302. handleRemoveChild: function (e) {
  1303. // console.log("remove", e.detail.tempId);
  1304. if (e.detail.tempId) {
  1305. const newSendFileList = this.sendFileList.filter((item) => item.tempId !== e.detail.tempId);
  1306. console.log('newSendFileList', newSendFileList);
  1307. this.setData({
  1308. sendFileList: newSendFileList
  1309. });
  1310. if (newSendFileList.length === 0 && this.showFileList) {
  1311. this.setData({
  1312. showFileList: false
  1313. });
  1314. }
  1315. }
  1316. },
  1317. handleChangeChild: function (e) {
  1318. console.log('change', e.detail);
  1319. const { fileId, tempId, parsed } = e.detail;
  1320. // const curFile = this.data.sendFileList.find(item => item.tempId === tempId)
  1321. // curFile.fileId = fileId
  1322. const newSendFileList = this.sendFileList.map((item) => {
  1323. if (item.tempId === tempId) {
  1324. return {
  1325. ...item,
  1326. fileId,
  1327. parsed
  1328. };
  1329. }
  1330. return item;
  1331. });
  1332. this.setData({
  1333. sendFileList: newSendFileList
  1334. });
  1335. },
  1336. handleClickTools: function () {
  1337. this.setData({
  1338. showTools: !this.showTools
  1339. });
  1340. },
  1341. handleClickWebSearch: function () {
  1342. if (this.sendFileList.length) {
  1343. uni.showModal({
  1344. title: '提示',
  1345. content: '上传附件后不支持联网搜索'
  1346. });
  1347. return;
  1348. }
  1349. this.setData({
  1350. useWebSearch: !this.useWebSearch
  1351. });
  1352. },
  1353. share() {
  1354. console.log('占位:函数 share 未声明');
  1355. }
  1356. },
  1357. created: function () {}
  1358. };
  1359. </script>
  1360. <style>
  1361. /* components/agent-ui/index.wxss */
  1362. .agent-ui {
  1363. width: 750rpx;
  1364. height: 100vh;
  1365. position: relative;
  1366. }
  1367. .nav {
  1368. width: 750rpx;
  1369. padding: 20px 0px 0px 0px;
  1370. display: flex;
  1371. flex-direction: column;
  1372. align-items: center;
  1373. }
  1374. .navBar {
  1375. height: 45px;
  1376. width: 100%;
  1377. box-shadow: 0 16px 16px #fff;
  1378. /* background: linear-gradient(to bottom,
  1379. rgba(245, 245, 245, 0.98) 60%,
  1380. rgba(235, 235, 235, 0.92)
  1381. ); */
  1382. /* border-bottom: 1rpx solid rgba(0, 0, 0, 0.08); */
  1383. /* margin-bottom: 10px */
  1384. }
  1385. /* .tips {
  1386. width: 100%;
  1387. font-size: 12px;
  1388. display: flex;
  1389. align-items: center;
  1390. justify-content: center;
  1391. height: 30px;
  1392. color: rgb(95, 92, 92)
  1393. } */
  1394. .tips {
  1395. display: flex;
  1396. align-items: center;
  1397. gap: 12px;
  1398. width: 100%;
  1399. color: rgb(128, 128, 128);
  1400. font-size: 12px;
  1401. height: 52px;
  1402. }
  1403. .tips::before,
  1404. .tips::after {
  1405. content: '';
  1406. display: inline-block;
  1407. height: 1px;
  1408. transform: scaleY(0.5);
  1409. flex-grow: 1;
  1410. border-radius: 2px;
  1411. }
  1412. .tips::before {
  1413. background-image: linear-gradient(to right, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.5));
  1414. }
  1415. .tips::after {
  1416. background-image: linear-gradient(to left, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.5));
  1417. }
  1418. .nav-content {
  1419. height: 40px;
  1420. display: flex;
  1421. align-items: center;
  1422. justify-content: center;
  1423. gap: 12px;
  1424. }
  1425. .bot-avatar {
  1426. width: 36px;
  1427. height: 36px;
  1428. border-radius: 50%;
  1429. border: 2px solid rgba(255, 255, 255, 0.8);
  1430. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  1431. }
  1432. .bot-name {
  1433. font-size: 18px;
  1434. font-weight: 500;
  1435. color: #333;
  1436. }
  1437. .main {
  1438. background-color: #fff;
  1439. /* padding-top:10px; */
  1440. }
  1441. .share_btn {
  1442. background-color: #fff;
  1443. margin: 0px !important;
  1444. padding: 0px !important;
  1445. width: 36rpx !important;
  1446. height: 36rpx;
  1447. }
  1448. .avatar {
  1449. width: 160rpx;
  1450. height: 160rpx;
  1451. border-radius: 75rpx;
  1452. }
  1453. .questions {
  1454. margin: 0px 16px 10px 16px;
  1455. /* background-color: blueviolet; */
  1456. }
  1457. .question_content {
  1458. background-color: #f5f5f5;
  1459. padding: 16rpx 24rpx;
  1460. border-radius: 12px;
  1461. display: inline-block;
  1462. font-size: 14px;
  1463. font-weight: 300;
  1464. }
  1465. .footer {
  1466. width: 100%;
  1467. min-height: 80px;
  1468. max-height: 380px;
  1469. /* background-color: aquamarine; */
  1470. position: absolute;
  1471. bottom: 0;
  1472. }
  1473. .foot_function {
  1474. margin: 8px 8px 15px;
  1475. }
  1476. .footer .file_list {
  1477. display: flex;
  1478. flex-direction: row;
  1479. flex-wrap: nowrap;
  1480. gap: 10px;
  1481. padding: 0px 16px;
  1482. overflow-x: scroll;
  1483. height: 80px;
  1484. }
  1485. .img-box {
  1486. position: absolute;
  1487. top: -100px;
  1488. left: 0px;
  1489. white-space: nowrap;
  1490. /* 防止内部元素换行 */
  1491. width: 100%;
  1492. /* 设置容器宽度 */
  1493. background-color: #fff;
  1494. }
  1495. .img-preview {
  1496. display: inline-block;
  1497. width: 80px;
  1498. height: 80px;
  1499. margin-right: 8px;
  1500. position: relative;
  1501. margin-top: 10px;
  1502. }
  1503. .img-preview-image {
  1504. width: 80px;
  1505. height: 80px;
  1506. border-radius: 10px;
  1507. }
  1508. .img-preview-loading {
  1509. width: 100%;
  1510. height: 100%;
  1511. position: absolute;
  1512. top: 0px;
  1513. left: 0px;
  1514. background-color: #eee;
  1515. border-radius: 10px;
  1516. }
  1517. .img-preview-close {
  1518. width: 16px;
  1519. height: 16px;
  1520. position: absolute;
  1521. right: -8px;
  1522. top: -8px;
  1523. /* background-color: blueviolet; */
  1524. }
  1525. .input_box {
  1526. /* display: flex; */
  1527. display: flex;
  1528. align-items: flex-end;
  1529. /* padding: 0 16px; */
  1530. flex-direction: row;
  1531. /* align-items: center; */
  1532. gap: 10rpx;
  1533. position: relative;
  1534. min-height: 40px;
  1535. /* max-height: 120px; */
  1536. /* margin: 8px 0px 15px; */
  1537. padding: 0px 8px;
  1538. /* position: relative; */
  1539. background-color: white;
  1540. transition: all 0.3s;
  1541. }
  1542. .set_panel_modal {
  1543. position: fixed;
  1544. width: 750rpx;
  1545. height: 100vh;
  1546. left: 0px;
  1547. top: 0px;
  1548. background-color: rgba(0, 0, 0, 0.7);
  1549. z-index: 1000;
  1550. }
  1551. .set_panel {
  1552. background-color: #f3f3f3;
  1553. position: absolute;
  1554. left: 0px;
  1555. bottom: 0px;
  1556. width: 750rpx;
  1557. }
  1558. .set_panel_funtion {
  1559. display: flex;
  1560. flex-direction: row;
  1561. padding: 10px 16px;
  1562. box-sizing: border-box;
  1563. gap: 10px;
  1564. }
  1565. .set_panel_cancel {
  1566. height: 60px;
  1567. text-align: center;
  1568. line-height: 40px;
  1569. color: black;
  1570. border-top: #cfcdcd solid 1px;
  1571. }
  1572. .function {
  1573. width: 50px;
  1574. display: flex;
  1575. flex-direction: column;
  1576. justify-content: center;
  1577. align-items: center;
  1578. gap: 8px;
  1579. background-color: #fff;
  1580. color: black;
  1581. padding: 0px 6px;
  1582. border-radius: 16px;
  1583. box-shadow: 0 0px 2px rgba(0, 0, 0, 0.253);
  1584. }
  1585. .icon {
  1586. width: 48rpx;
  1587. height: 48rpx;
  1588. }
  1589. .text_desc {
  1590. font-weight: 300;
  1591. font-size: 24rpx;
  1592. }
  1593. .input_inner_box {
  1594. width: 100%;
  1595. min-height: 50px;
  1596. background-color: #fff;
  1597. padding: 12px 12px;
  1598. color: black;
  1599. border: #f3f3f3 solid 1px;
  1600. box-sizing: border-box;
  1601. border-radius: 16px;
  1602. box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.2);
  1603. }
  1604. .input {
  1605. width: 100%;
  1606. flex: 1;
  1607. /* height: 40px; */
  1608. max-height: 160px;
  1609. /* overflow-y: hidden; */
  1610. /* line-height: 22px; */
  1611. font-size: 16px;
  1612. /* display: flex;
  1613. align-items: center; */
  1614. height: 20px;
  1615. /* padding-top: 10px; */
  1616. /* align-items: center; */
  1617. }
  1618. .right_btns {
  1619. height: 50px;
  1620. display: flex;
  1621. align-items: center;
  1622. /* flex: 1; */
  1623. gap: 10rpx;
  1624. /* padding: 0 8px; */
  1625. }
  1626. .set {
  1627. width: 58rpx;
  1628. height: 58rpx;
  1629. }
  1630. .system {
  1631. margin-left: 32rpx;
  1632. margin-right: 32rpx;
  1633. border-radius: 12rpx;
  1634. padding-bottom: 16px;
  1635. box-sizing: border-box;
  1636. position: relative;
  1637. }
  1638. .avatar-left {
  1639. position: absolute;
  1640. top: 0px;
  1641. left: 0px;
  1642. }
  1643. .guide_system {
  1644. padding-left: 32rpx;
  1645. padding-right: 32rpx;
  1646. border-radius: 12rpx;
  1647. padding-bottom: 16px;
  1648. box-sizing: border-box;
  1649. }
  1650. .bot_intro_system {
  1651. padding-left: 32rpx;
  1652. padding-right: 32rpx;
  1653. border-radius: 12rpx;
  1654. padding-bottom: 16px;
  1655. box-sizing: border-box;
  1656. }
  1657. .user {
  1658. display: flex;
  1659. justify-content: flex-end;
  1660. margin-bottom: 16px;
  1661. }
  1662. .userContent .fileBar {
  1663. /* display: flex;
  1664. overflow-x: scroll;
  1665. justify-content: flex-end; */
  1666. display: flex;
  1667. flex-direction: row;
  1668. flex-direction: row-reverse;
  1669. flex-wrap: nowrap;
  1670. padding: 0px 16px;
  1671. overflow-x: scroll;
  1672. max-height: 80px;
  1673. gap: 10px;
  1674. }
  1675. .user .user_content {
  1676. background-color: #f3f5fb;
  1677. border-radius: 12rpx 0rpx 12rpx 12rpx;
  1678. margin-left: 32rpx;
  1679. margin-right: 32rpx;
  1680. padding: 24rpx;
  1681. word-wrap: break-word;
  1682. word-break: break-all;
  1683. font-size: 32rpx;
  1684. }
  1685. .feedback_modal {
  1686. position: fixed;
  1687. top: 0px;
  1688. left: 0px;
  1689. width: 750rpx;
  1690. height: 100vh;
  1691. background-color: rgba(0, 0, 0, 0.7);
  1692. display: flex;
  1693. justify-content: center;
  1694. align-items: center;
  1695. z-index: 1000;
  1696. }
  1697. .modal {
  1698. background-color: #fff;
  1699. width: 700rpx;
  1700. border-radius: 16rpx;
  1701. overflow: hidden;
  1702. }
  1703. .modal_head {
  1704. height: 40px;
  1705. line-height: 40px;
  1706. padding: 0px 10px;
  1707. }
  1708. .modal_body {
  1709. padding: 10px;
  1710. }
  1711. .modal_footer {
  1712. display: flex;
  1713. }
  1714. .link-box {
  1715. padding: 0px 16px 6px 16px;
  1716. }
  1717. .tool_box {
  1718. height: 80px;
  1719. display: flex;
  1720. flex-direction: row;
  1721. padding: 5px 16px;
  1722. box-sizing: border-box;
  1723. gap: 10px;
  1724. justify-content: flex-start;
  1725. flex-wrap: nowrap;
  1726. overflow-x: scroll;
  1727. }
  1728. .tool_box .function {
  1729. flex: 0 0 calc(25% - 20px);
  1730. }
  1731. .webSearchSwitch {
  1732. width: 200rpx;
  1733. height: 30px;
  1734. display: flex;
  1735. justify-content: center;
  1736. align-items: center;
  1737. border-radius: 25px;
  1738. border: 1px solid rgba(0, 0, 0, 0.08);
  1739. font-size: 14px;
  1740. }
  1741. .feature_enable {
  1742. background-color: rgb(219, 234, 254);
  1743. color: rgb(77, 107, 254);
  1744. border-color: rgba(0, 122, 255, 0.15);
  1745. }
  1746. .feature_list {
  1747. margin: 0 16px;
  1748. }
  1749. </style>