liudong
2024-08-24 2e37bf48f87688804e3e32ba3c8402be8bce5934
src/views/sessionManager/components/agentSession.vue
@@ -9,7 +9,13 @@
          trigger="click"
        >
          <a-button border
          >{{agentTitle}}
          >
            <span style="
                       width: 100px;
                      overflow: hidden;
                      text-overflow: ellipsis;
                      white-space: nowrap;
                    ">{{ from.name }}</span>
            <icon-down style="margin-left: 4px" />
          </a-button>
          <template #content>
@@ -75,7 +81,7 @@
            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>
@@ -141,8 +147,8 @@
                  maxRows: 5,
                }"
      />
      <div class="btn-send">
        <!-- <icon-send size="32" /> -->
      <div style="width: 100%;display: flex;justify-content: space-between">
        <span></span>
        <a-button
          :disabled="chatDis"
          @click="sentClick"
@@ -154,11 +160,24 @@
        </a-button
        >
      </div>
<!--      <div class="btn-send">-->
<!--        &lt;!&ndash; <icon-send size="32" /> &ndash;&gt;-->
<!--        <a-button-->
<!--          :disabled="chatDis"-->
<!--          @click="sentClick"-->
<!--          type="text"-->
<!--          style="border-radius: 24px"-->
<!--          :loading="loading"-->
<!--        >-->
<!--          <icon-send size="32" style="color: #0960bd;"/>-->
<!--        </a-button-->
<!--        >-->
<!--      </div>-->
    </div>
    <a-modal v-model:visible="visible" title="修改名称" @before-open="handleOpened" @cancel="handleCancel" :footer="false" title-align="start">
      <a-form ref="formRef" :rules="rules" :model="agentObj" @submit="handleSubmit" >
      <a-form ref="formRef" :rules="rules" :model="from" @submit="handleSubmit" >
        <a-form-item field="name" label="名称">
          <a-input v-model="agentTitle" placeholder="请输入名称"/>
          <a-input v-model="from.name" placeholder="请输入名称"/>
        </a-form-item>
        <a-form-item>
          <div style="width: 100%;text-align: right">
@@ -180,7 +199,7 @@
import { agentResetApi, agentSetApi, getAgentSessionDetailsApi } from "@/api/agentSession";
import EventBus from '@/utils/EventBus';
import useClipboard from "vue-clipboard3";
import { addSessionApi } from "@/api/session";
import { addSessionApi, getSessionDetailsApi } from "@/api/session";
const props = defineProps({
  modalObj: Object,
});
@@ -202,6 +221,7 @@
const streamStr = ref('');
const inputMsg = ref('');
const activeSessionId = ref('');
const conversation_id = ref('');
const fieldNames = { value: 'id', label: 'name' };
const agentObj = reactive({});
const agentList = ref([]);
@@ -213,9 +233,14 @@
const theme = computed(() => {
  return appStore.theme;
});
let from = reactive({
  name:'未命名会话',
});
const visible = ref(false);
let isHistory = ref(false);//是否是历史记录
let dsl = reactive({});
const chatDataMeg = reactive({});
const rules = {
@@ -229,12 +254,16 @@
const handleSubmit = async({values, errors}) => {
  if(errors) return;
  agentObj.name = agentTitle.value;
  const { code, data } = await addSessionApi(agentObj);
  console.log(agentObj, 'agentObj');
  let chatData = {
    id:conversation_id.value,
    conversation_id:conversation_id.value,
    name: from.name
  }
  const { code, data } = await addSessionApi(chatData);
  if (data) {
    Message.success("修改成功");
    handleCancel()
    queryAgentSessionDetail(agentObj.id);
  }
}
@@ -259,14 +288,11 @@
};
const createNewAgent = async (session) => {
  console.log(session,'新建agent会话对象');
  Object.assign(agentObj, session);
  isHistory.value = false;
  initPage();
}
// 调用set方法
const agentSet = async () => {
  const res = await agentSetApi({
@@ -274,10 +300,9 @@
    title: agentObj.title,
    dsl: agentObj.dsl,
  });
  console.log(res,'agentSet');
  // console.log(res,'agentSet');
  if (res.code == 0) {
    // sessionDetailList.value = res.data.dsl.messages;
    // Message.success('修改成功');
    conversation_id.value = res.data.conversation_id;
    agentReset();
  }
}
@@ -341,14 +366,33 @@
  inputMsg.value = '';
}
// 调用get方法
const queryAgentSessionDetail = async (id) => {
  const { code, data } = await getAgentSessionDetailsApi(id);
  if (code == 0) {
    console.log(data,'会话详情');
    console.log(data,'agent会话详情');
    Object.assign(chatObj, data);
    sessionDetailList.value = data.dsl.messages;
    agentTitle.value = `和${data.title}的会话` || '未命名会话';
    agentTitle.value = `${data.title}` || '未命名会话';
    from.name = `${data.title}` || '未命名会话';
    refreshScroll(); //刷新滚动条位置
  }
};
// 历史记录跳转获取agent会话详情
const querySessionDetail = async (session) => {
  conversation_id.value = session.id;
  from.name = session.name;
  const { code, data } = await getSessionDetailsApi(session.id);
  if (code == 200) {
    sessionDetailList.value = data.message.messages;
    const res = await getAgentSessionDetailsApi(session.dialog_id);
    if (res.code == 0) {
      console.log(res.data,'agent会话详情');
      Object.assign(agentObj, res.data);
      Object.assign(chatObj, res.data);
      Object.assign(dsl, res.data.dsl);
      refreshScroll(); //刷新滚动条位置
    }
  }
};
@@ -404,13 +448,19 @@
const startChat = async(valMsg)=>{
  sessionDetailList.value.push({
    content: valMsg,
    role: 'user',
  });
  sessionDetailList.value.push({ role: 'last' });
  refreshScroll();
  let chatStr = {
    id: agentObj.id,
    message: valMsg,
  }
  if(isHistory.value){
    chatStr.dsl = dsl;
  }
  const response = await fetch(
    '/api/v1/canvas/completion',
    {
@@ -419,10 +469,7 @@
        'Authorization': getAuthorization(),
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        id: agentObj.id,
        message: valMsg,
      }),
      body: JSON.stringify(chatStr),
    }
  );
@@ -450,8 +497,13 @@
      if (done) {
        console.info('done');
        displayedText.value = '';
        queryAgentSessionDetail(agentObj.id);
        EventBus.emit('queryAppUsageList');
        if (isStopChat.value) {
          isStopChat.value = false;
          setChatDataMeg(chatDataMeg);
        } else {
          queryAgentSessionDetail(agentObj.id);
          EventBus.emit('queryAppUsageList');
        }
        break;
      }
    }
@@ -461,10 +513,9 @@
}
const stopChat=async()=>{
  // const { code, data } = await stopChatApi(activeSessionId.value);
  // const { code, data } = await stopChatApi(agentObj.id);
  // if (code === 200) {
  //   Message.success("已停止");
  //   queryNewSessionDetail(activeSessionId.value);
  // }
  loading.value = false;
  chatDis.value = false;
@@ -484,20 +535,26 @@
  sessionDetailList.value = sessionDetailList.value.splice(0, sessionDetailList.value.length-2).concat(lastArr);
  console.log(sessionDetailList.value, 'sessionDetailList2');
  console.log(chatObj, 'chatObj对象');
  chatObj.message = chatObj.message.concat(lastArr);
  // clearTimeout(timer!);
  // timer = null;
  // const { code, data } = await addSessionApi(chatObj);
  // if (data) {
  //   //停止定时器
  //   clearTimeout(timer!);
  //   timer = null;
  //   displayedText.value = "";
  //   queryNewSessionDetail(activeSessionId.value);
  // }
  chatObj.dsl.messages = chatObj.dsl.messages.concat(lastArr);
  Object.assign(chatDataMeg, {
    id: chatObj.id,
    conversation_id: chatObj.id,
    message: sessionDetailList.value,
  });
}
const setChatDataMeg = async (chatData) => {
  const { code, data } = await addSessionApi(chatData);
  if (data) {
    //停止定时器
    clearTimeout(timer!);
    timer = null;
    // displayedText.value = "";
    queryAgentSessionDetail(chatObj.id);
  }
};
const handleShiftEnter = (event) => {
  event.preventDefault();
@@ -538,8 +595,8 @@
    createNewAgent(data);
  });
  EventBus.on('queryAgentSessionDetail', (data) => {
    Object.assign(agentObj, data);
    queryAgentSessionDetail(data.id);
    isHistory.value = true;
    querySessionDetail(data);
  });
});
onBeforeUnmount(() => {