Browse Source

回车问题

CzRger 3 days ago
parent
commit
e2d86dc0d1

+ 83 - 67
src/views/smart-ask-answer/assistant/chat.vue

@@ -24,7 +24,7 @@
           <div class="chat-input-block-main-auto" ref="ref_auto">
             <div class="chat-input-block-main-auto-list">
               <template v-for="item in state.autoList">
-                <div class="chat-input-block-main-auto-item __hover" @click="setText(item)">{{ item }}</div>
+                <div class="chat-input-block-main-auto-item __hover" @click="setText(item)" v-html="item.replace(new RegExp(state.text, 'g'), `<span style='color: #d32520;'>$&</span>`)"/>
               </template>
             </div>
           </div>
@@ -32,7 +32,8 @@
             ref="ref_text"
             placeholder="请输入您的问题"
             :rows="1"
-            v-model="state.text"/>
+            v-model="state.text"
+          />
         </div>
         <div class="chat-input-block-operations">
           <div class="cibo-audio">
@@ -40,7 +41,7 @@
           </div>
           <div class="cibo-split"/>
           <el-tooltip content="发送" placement="top">
-            <div class="cibo-send __hover" @click="$czrUtil.isValue(state.text.trim()) ? onSend() : undefined">
+            <div class="cibo-send __hover" @click="onSend()">
               <img src="@/views/smart-ask-answer/assistant/imgs/send.png"/>
             </div>
           </el-tooltip>
@@ -57,7 +58,7 @@ import answerCom from './component/answer/index.vue';
 import audioCom from './component/audio/index.vue'
 import {get, post, ssePost} from './dify/base'
 import {useRouter} from "vue-router";
-import {YMDHms} from "@/utils/czr-util";
+import {isValue, YMDHms} from "@/utils/czr-util";
 import {updateFeedback} from "@/views/smart-ask-answer/assistant/dify/share";
 import {staticConQueryQuestionslist} from "@/views/smart-ask-answer/assistant/cms/api";
 
@@ -82,70 +83,72 @@ const state: any = reactive({
 const ref_text = ref()
 const ref_chatMsg = ref()
 const ref_auto = ref()
-const onSend = (text) => {
-  state.loading = true
-  if (text) {
-    state.params.query = text
-  } else {
-    state.params.query = state.text + ''
-    state.text = ''
-  }
-  emit('getText', state.params.query)
-  const ask = {
-    type: 'ask',
-    content: state.params.query + ''
+const onSend = (text = '') => {
+  if (isValue(state.text.trim())) {
+    state.loading = true
+    if (text) {
+      state.params.query = text
+    } else {
+      state.params.query = state.text + ''
+      state.text = ''
+    }
+    emit('getText', state.params.query)
+    const ask = {
+      type: 'ask',
+      content: state.params.query + ''
+    }
+    state.chats.push(ask)
+    const answer = reactive({
+      type: 'answer',
+      content: '',
+      messageId: ''
+    })
+    state.chats.push(answer)
+    ssePost(`/installed-apps/${window.czrConfig.dify.appId}/chat-messages`, {
+      body: state.params,
+    }, {
+      onData: (message: string, isFirstMessage: boolean, { conversationId: newConversationId, messageId, taskId }: any) => {
+        answer.content += message
+        ref_chatMsg.value.scrollTo({
+          top: ref_chatMsg.value.scrollHeight,
+          behavior: 'smooth'
+        });
+      },
+      onFile(file) {
+      },
+      onThought(thought) {
+      },
+      onMessageEnd: (messageEnd) => {
+        if (!state.params.conversation_id) {
+          state.params.conversation_id = messageEnd.conversation_id
+          post(`/installed-apps/${window.czrConfig.dify.appId}/conversations/${state.params.conversation_id}/name`, {body: {auto_generate: false, name: YMDHms(new Date())}})
+        }
+        answer.messageId = messageEnd.message_id
+        state.params.parent_message_id = messageEnd.message_id
+        state.loading = false
+      },
+      onMessageReplace: (messageReplace) => {
+      },
+      onError() {
+      },
+      onWorkflowStarted: ({ workflow_run_id, task_id }) => {
+      },
+      onWorkflowFinished: ({ data: workflowFinishedData }) => {
+      },
+      onIterationStart: ({ data: iterationStartedData }) => {
+      },
+      onIterationFinish: ({ data: iterationFinishedData }) => {
+      },
+      onNodeStarted: ({ data: nodeStartedData }) => {
+      },
+      onNodeFinished: ({ data: nodeFinishedData }) => {
+      },
+      onTTSChunk: (messageId: string, audio: string) => {
+      },
+      onTTSEnd: (messageId: string, audio: string) => {
+      },
+    })
   }
-  state.chats.push(ask)
-  const answer = reactive({
-    type: 'answer',
-    content: '',
-    messageId: ''
-  })
-  state.chats.push(answer)
-  ssePost(`/installed-apps/${window.czrConfig.dify.appId}/chat-messages`, {
-    body: state.params,
-  }, {
-    onData: (message: string, isFirstMessage: boolean, { conversationId: newConversationId, messageId, taskId }: any) => {
-      answer.content += message
-      ref_chatMsg.value.scrollTo({
-        top: ref_chatMsg.value.scrollHeight,
-        behavior: 'smooth'
-      });
-    },
-    onFile(file) {
-    },
-    onThought(thought) {
-    },
-    onMessageEnd: (messageEnd) => {
-      if (!state.params.conversation_id) {
-        state.params.conversation_id = messageEnd.conversation_id
-        post(`/installed-apps/${window.czrConfig.dify.appId}/conversations/${state.params.conversation_id}/name`, {body: {auto_generate: false, name: YMDHms(new Date())}})
-      }
-      answer.messageId = messageEnd.message_id
-      state.params.parent_message_id = messageEnd.message_id
-      state.loading = false
-    },
-    onMessageReplace: (messageReplace) => {
-    },
-    onError() {
-    },
-    onWorkflowStarted: ({ workflow_run_id, task_id }) => {
-    },
-    onWorkflowFinished: ({ data: workflowFinishedData }) => {
-    },
-    onIterationStart: ({ data: iterationStartedData }) => {
-    },
-    onIterationFinish: ({ data: iterationFinishedData }) => {
-    },
-    onNodeStarted: ({ data: nodeStartedData }) => {
-    },
-    onNodeFinished: ({ data: nodeFinishedData }) => {
-    },
-    onTTSChunk: (messageId: string, audio: string) => {
-    },
-    onTTSEnd: (messageId: string, audio: string) => {
-    },
-  })
 }
 const onLoading = () => {
   state.loading = true
@@ -212,6 +215,19 @@ const initTextHandle = () => {
   const textarea = ref_text.value
   const floatingDiv = ref_auto.value
 
+  textarea.addEventListener('keydown', (e) => {
+    e.preventDefault()
+    if (e.ctrlKey && e.key === 'Enter') {
+      state.text += '\n'
+      textarea.style.height = 'auto';
+      textarea.style.height = Math.min(textarea.scrollHeight + 2, 200) + 'px';
+    } else if (e.key === 'Enter') {
+      onSend()
+    } else {
+      textarea.style.height = 'auto';
+      textarea.style.height = Math.min(textarea.scrollHeight + 2, 200) + 'px';
+    }
+  });
   textarea.addEventListener('input', (e) => {
     textarea.style.height = 'auto';
     textarea.style.height = Math.min(textarea.scrollHeight + 2, 200) + 'px';

BIN
src/views/smart-ask-answer/assistant/imgs/none.jpg


File diff suppressed because it is too large
+ 24 - 15
src/views/smart-ask-answer/assistant/index.vue