zhangxiao
2024-08-26 53fd55bdf6544ae9c2697e460766ce2d53044dbe
src/views/sessionManager/components/agentSession.vue
@@ -4,18 +4,17 @@
    <div class="chatHeader">
      <div class="chatHeaderBox">
<!--        <span class="title">{{agentTitle}}</span>-->
        <a-popover
          position="bottom"
          trigger="click"
        >
          <a-button border
          >
            <span style="
        <a-popover position="bottom" trigger="click">
          <a-button border>
            <span
              style="
                       width: 100px;
                      overflow: hidden;
                      text-overflow: ellipsis;
                      white-space: nowrap;
                    ">{{ from.name }}</span>
              "
              >{{ from.name }}</span
            >
            <icon-down style="margin-left: 4px" />
          </a-button>
          <template #content>
@@ -66,7 +65,7 @@
        <template #avatar>
          <img
            class="icon-user-jpg"
            src="../../../assets/images/icon-chart.png"
            src="../../../assets/images/icon-picture.png"
            alt="本地图片"
          />
        </template>
@@ -81,16 +80,26 @@
            auto-size
            v-model="sessionDetail.content"
            :class="{ chatItemAnswer: theme === 'light' }"
            :style="{ backgroundColor: theme === 'light' ? '#ffffff' : '#000000'}"
            :style="{
              backgroundColor: theme === 'light' ? '#ffffff' : '#000000',
            }"
            style="border: none"
          >
          </a-textarea>
        </template>
        <template #actions>
                  <span class="action" v-if="index != 0" @click="copy(sessionDetail.content)">
          <span
            class="action"
            v-if="index != 0"
            @click="copy(sessionDetail.content)"
          >
                     <icon-copy /> 复制
                  </span>
          <span class="action"  v-if="index != 0 && index == sessionDetailList.length - 1" @click="reGenerate()">
          <span
            class="action"
            v-if="index != 0 && index == sessionDetailList.length - 1"
            @click="reGenerate()"
          >
                     <icon-refresh /> 重新生成
                  </span>
        </template>
@@ -109,15 +118,23 @@
            auto-size
            v-model="displayedText"
            :class="{ chatItemAnswer: theme === 'light' }"
            :style="{backgroundColor: theme === 'light' ? '#ffffff' : '#000000'}"
            :style="{
              backgroundColor: theme === 'light' ? '#ffffff' : '#000000',
            }"
            style="border: none"
          >
          </a-textarea>
        </template>
        <template #actions>
          <div class="action"
          <div
            class="action"
               @click="stopChat"
               style="background: var(--color-bg-2);color: var(--color-primary-light-4);" v-if="displayedText != ''">
            style="
              background: var(--color-bg-2);
              color: var(--color-primary-light-4);
            "
            v-if="displayedText != ''"
          >
            <icon-record-stop />
            停止生成
          </div>
@@ -156,9 +173,8 @@
          style="border-radius: 24px"
          :loading="loading"
        >
          <icon-send size="32" style="color: #0960bd;"/>
        </a-button
        >
          <icon-send size="32" style="color: #0960bd" />
        </a-button>
      </div>
<!--      <div class="btn-send">-->
<!--        &lt;!&ndash; <icon-send size="32" /> &ndash;&gt;-->
@@ -174,7 +190,14 @@
<!--        >-->
<!--      </div>-->
    </div>
    <a-modal v-model:visible="visible" title="修改名称" @before-open="handleOpened" @cancel="handleCancel" :footer="false" title-align="start">
    <a-modal
      v-model:visible="visible"
      title="修改名称"
      @before-open="handleOpened"
      @cancel="handleCancel"
      :footer="false"
      title-align="start"
    >
      <a-form ref="formRef" :rules="rules" :model="from" @submit="handleSubmit" >
        <a-form-item field="name" label="名称">
          <a-input v-model="from.name" placeholder="请输入名称"/>
@@ -182,7 +205,12 @@
        <a-form-item>
          <div style="width: 100%;text-align: right">
            <a-button @click="visible = false">取消</a-button>
            <a-button style="margin-left: 10px" type="primary" html-type="submit">确定</a-button>
            <a-button
              style="margin-left: 10px"
              type="primary"
              html-type="submit"
              >确定</a-button
            >
          </div>
        </a-form-item>
      </a-form>
@@ -191,15 +219,29 @@
</template>
<script setup lang="ts">
import { defineProps, ref, watch, defineEmits, onMounted, reactive, computed, nextTick, onBeforeUnmount } from "vue";
  import {
    defineProps,
    ref,
    watch,
    defineEmits,
    onMounted,
    reactive,
    computed,
    nextTick,
    onBeforeUnmount,
  } from 'vue';
import { Message } from '@arco-design/web-vue';
import { useAppStore } from "@/store";
import { getAuthorization } from "@/utils/auth";
  import { useAppStore } from '@/store';
  import { getAuthorization } from '@/utils/auth';
import { EventSourceParserStream } from 'eventsource-parser/stream';
import { agentResetApi, agentSetApi, getAgentSessionDetailsApi } from "@/api/agentSession";
  import {
    agentResetApi,
    agentSetApi,
    getAgentSessionDetailsApi,
  } from '@/api/agentSession';
import EventBus from '@/utils/EventBus';
import useClipboard from "vue-clipboard3";
import { addSessionApi, getSessionDetailsApi } from "@/api/session";
  import useClipboard from 'vue-clipboard3';
  import { addSessionApi, getSessionDetailsApi } from '@/api/session';
const props = defineProps({
  modalObj: Object,
});
@@ -212,8 +254,7 @@
const dialogId = ref('');
const chatDis = ref(false);
const loading = ref(false);
const agentTitle = ref("未命名会话");
  const agentTitle = ref('未命名会话');
const currIndex = ref(0);
const displayedText = ref(''); // 正在显示的文字
@@ -228,7 +269,7 @@
const selectValue = ref('');
const sectionList = ref({});
let chatObj = reactive({});
const isStopChat = ref(false)
  const isStopChat = ref(false);
const appStore = useAppStore();
const theme = computed(() => {
  return appStore.theme;
@@ -242,7 +283,6 @@
let dsl = reactive({});
const chatDataMeg = reactive({});
const rules = {
  name: [
    {
@@ -250,7 +290,7 @@
      message:'名称不允许为空',
    },
  ],
}
  };
const handleSubmit = async({values, errors}) => {
  if(errors) return;
@@ -258,29 +298,28 @@
  let chatData = {
    id:conversation_id.value,
    conversation_id:conversation_id.value,
    name: from.name
  }
      name: from.name,
    };
  const { code, data } = await addSessionApi(chatData);
  if (data) {
    Message.success("修改成功");
    handleCancel()
      Message.success('修改成功');
      handleCancel();
  }
}
  };
const handleClick = () => {
  visible.value = true;
};
const handleCancel = () => {
  visible.value = false;
}
  };
const handleOpened =(el) => {
  // Object.assign(form,{
  //   name: '',// 用户名
  // });
  // formRef.value.resetFields();
}
  };
// 初始化页面
const initPage = async () => {
@@ -292,7 +331,7 @@
  Object.assign(agentObj, session);
  isHistory.value = false;
  initPage();
}
  };
// 调用set方法
const agentSet = async () => {
  const res = await agentSetApi({
@@ -305,7 +344,7 @@
    conversation_id.value = res.data.conversation_id;
    agentReset();
  }
}
  };
// 调用reset方法
const agentReset = async () => {
  const res = await agentResetApi({
@@ -315,22 +354,19 @@
    // Message.success('修改成功');
    agentCompletion();
  }
}
  };
// 调用completion方法
const agentCompletion = async () => {
  const response = await fetch(
    '/api/v1/canvas/completion',
    {
    const response = await fetch('/api/v1/canvas/completion', {
      method: 'POST',
      headers: {
        'Authorization': getAuthorization(),
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        id: agentObj.id
        id: agentObj.id,
      }),
    }
  );
    });
  const reader = response?.body
    ?.pipeThrough(new TextDecoderStream())
@@ -364,7 +400,7 @@
  chatDis.value = false;
  loading.value = false;
  inputMsg.value = '';
}
  };
// 调用get方法
const queryAgentSessionDetail = async (id) => {
  const { code, data } = await getAgentSessionDetailsApi(id);
@@ -396,12 +432,10 @@
  }
};
const { toClipboard } = useClipboard();
const copy = async (text) => {
  await toClipboard(text);    //参数为要复制的文本
}
  };
const sentClick = () => {
  sendMessage('click');
@@ -409,8 +443,9 @@
// 重新生成
const reGenerate = () => {
  let inputContent = sessionDetailList.value[sessionDetailList.value.length-2].content
  startChat(inputContent)
    let inputContent =
      sessionDetailList.value[sessionDetailList.value.length - 2].content;
    startChat(inputContent);
};
const sendMessage = async (event) => {
@@ -435,7 +470,7 @@
      // }
      if (inputMsg.value) {
        startChat(inputMsg.value)
          startChat(inputMsg.value);
        inputMsg.value = '';
      } else {
        Message.warning('消息不能为空');
@@ -445,7 +480,6 @@
    }
  }
};
const startChat = async(valMsg)=>{
  sessionDetailList.value.push({
@@ -457,21 +491,18 @@
  let chatStr = {
    id: agentObj.id,
    message: valMsg,
  }
    };
  if(isHistory.value){
    chatStr.dsl = dsl;
  }
  const response = await fetch(
    '/api/v1/canvas/completion',
    {
    const response = await fetch('/api/v1/canvas/completion', {
      method: 'POST',
      headers: {
        'Authorization': getAuthorization(),
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(chatStr),
    }
  );
    });
  const reader = response?.body
    ?.pipeThrough(new TextDecoderStream())
@@ -510,7 +541,7 @@
  }
  chatDis.value = false;
  loading.value = false;
}
  };
const stopChat=async()=>{
  // const { code, data } = await stopChatApi(agentObj.id);
@@ -523,16 +554,22 @@
  console.log('stopChat');
  console.log(displayedText.value, 'displayedText');
  console.log(sessionDetailList.value, 'sessionDetailList');
  let inputText = sessionDetailList.value[sessionDetailList.value.length-2].content
    let inputText =
      sessionDetailList.value[sessionDetailList.value.length - 2].content;
  // 数组合并
  let lastArr = [{
    let lastArr = [
      {
    content: inputText,
    role: "user"
  },{
        role: 'user',
      },
      {
    content: displayedText.value,
    role: "assistant"
  }]
  sessionDetailList.value = sessionDetailList.value.splice(0, sessionDetailList.value.length-2).concat(lastArr);
        role: 'assistant',
      },
    ];
    sessionDetailList.value = sessionDetailList.value
      .splice(0, sessionDetailList.value.length - 2)
      .concat(lastArr);
  console.log(sessionDetailList.value, 'sessionDetailList2');
  console.log(chatObj, 'chatObj对象');
  chatObj.dsl.messages = chatObj.dsl.messages.concat(lastArr);
@@ -541,9 +578,7 @@
    conversation_id: chatObj.id,
    message: sessionDetailList.value,
  });
}
  };
const setChatDataMeg = async (chatData) => {
  const { code, data } = await addSessionApi(chatData);
@@ -560,8 +595,6 @@
  event.preventDefault();
  inputMsg.value += '\n';
};
//文字动态输出
const startDisplayStr = () => {
@@ -589,7 +622,6 @@
  });
};
onMounted(() => {
  EventBus.on('createAgent', (data) => {
    createNewAgent(data);
@@ -612,15 +644,15 @@
    if(JSON.stringify(newVal) != '{}'){
      // initPage();
    }
  },{
    },
    {
    immediate: true,
    deep: true
      deep: true,
  }
);
</script>
<style scoped lang="scss">
.dark {
  color: gray !important;
}