zhangxiao
2024-08-06 23f8f2717620e9c4b8184275e785df32501c53b5
src/views/session/sessionManager/index.vue
@@ -1,357 +1,421 @@
<script setup lang="ts">
    import { IconMoreVertical,IconQuestionCircleFill,IconPoweroff,IconCommon ,IconSend} from '@arco-design/web-vue/es/icon';
    import img1 from '@/assets/images/u64.png'
    import img2 from '@/assets/images/u69.png'
    import img3 from '@/assets/images/u74.png';
    import AddSession from './components/addSession.vue'
    import { ref, onMounted, computed, reactive, nextTick,watch } from 'vue';
    import { useUserStore,useAppStore } from '@/store';
    import { sessionListApi, deleteSessionApi,getSessionDetailsApi,chatApi }from '@/api/session';
    import { Message } from '@arco-design/web-vue';
    const userStore = useUserStore();
    const appStore = useAppStore();
    const theme = computed(() => {
        return appStore.theme;
    });
    const sessionList=ref([]);//会话列表
    const sessionDetailList=ref([]);//根据会话id出来的会话详情
    const activeSessionId=ref('');
    const inputMsg=ref('');
    const scrollbar = ref(null);
  import {
    IconMoreVertical,
    IconQuestionCircleFill,
    IconPoweroff,
    IconCommon,
    IconSend,
  } from '@arco-design/web-vue/es/icon';
  import img1 from '@/assets/images/u64.png';
  import img2 from '@/assets/images/u69.png';
  import img3 from '@/assets/images/u74.png';
  import AddSession from './components/addSession.vue';
  import { ref, onMounted, computed, reactive, nextTick, watch } from 'vue';
  import { useUserStore, useAppStore } from '@/store';
  import {
    sessionListApi,
    deleteSessionApi,
    getSessionDetailsApi,
    chatApi,
  } from '@/api/session';
  import { Message } from '@arco-design/web-vue';
  const userStore = useUserStore();
  const appStore = useAppStore();
  const theme = computed(() => {
    return appStore.theme;
  });
  const sessionList = ref([]); //会话列表
  const sessionDetailList = ref([]); //根据会话id出来的会话详情
  const activeSessionId = ref('');
  const inputMsg = ref('');
  const scrollbar = ref(null);
    const currIndex = ref(0)
    const displayedText = ref('');// 正在显示的文字
    let timer: number|null = null;
    const streamStr=ref('');
    const modalObj=reactive({ add:false });
    //查询会话列表
    const querySessionList = async () => {
        const { code, data } =await sessionListApi();
        if (code === 200) {
          sessionList.value = data;
          if(Array.isArray(data)&&data.length>0){
                    activeSessionId.value=data[0].id;
                    const res= await getSessionDetailsApi(data[0].id);
                    if(res.code===200){
                        sessionDetailList.value=res.data.message;
                        refreshScroll();
                    }
                }
        }else{
          Message.warning('查询失败');
  const currIndex = ref(0);
  const displayedText = ref(''); // 正在显示的文字
  let timer: number | null = null;
  const streamStr = ref('');
  const modalObj = reactive({ add: false });
  //查询会话列表
  const querySessionList = async () => {
    const { code, data } = await sessionListApi();
    if (code === 200) {
      sessionList.value = data;
      if (Array.isArray(data) && data.length > 0) {
        activeSessionId.value = data[0].id;
        const res = await getSessionDetailsApi(data[0].id);
        if (res.code === 200) {
          sessionDetailList.value = res.data.message;
          refreshScroll();
        }
    };
    //根据会话id删除会话
    const deleteSession=async (session)=>{
        const {code}=await deleteSessionApi([session.id]);
        if(code===200){
            Message.success('删除成功');
            querySessionList();
        }
    };
    // eslint-disable-next-line prettier/prettier
    // 新增会话之后刷新会话列表
    const addSession=()=>{
      querySessionList();
    };
    // 初始化数据
    const initData =()=>{
      querySessionList();
    };
    // 获取登录信息
    const userName=computed(()=>{
        return userStore.name;
    });
    const avatar = computed(() => {
        return userStore.avatar;
    });
    const refreshScroll=()=>{
        nextTick(()=>{
            const container = document.getElementById('home');
            scrollbar.value.scrollTop(container.scrollHeight);
        });
    };
    // 根据会话id 查询会话详情
    const querySessionDetail=async (session)=>{
        activeSessionId.value=session.id;
        const {code,data}= await getSessionDetailsApi(session.id);
        if(code===200){
            sessionDetailList.value=data.message;
            refreshScroll();//刷新滚动条位置
        }
    };
    const sendMessage= async ()=>{
        if(inputMsg.value){
            const {code,data} =await chatApi({conversation_id:activeSessionId.value,messages:inputMsg.value});
            const res= await getSessionDetailsApi(activeSessionId.value);
            if(res.code===200){
                sessionDetailList.value=res.data.message.map((item,index)=>{
                    if(index===res.data.message.length-1){
                        item.role='last';
                        displayedText.value='';
                        currIndex.value=0;
                        streamStr.value=item.content;
                        startDisplayStr();
                    }
                    return item;
                });
                refreshScroll();
            }
            inputMsg.value='';
        }else{
            Message.warning('消息不能为空');
        }
    };
    onMounted(()=>{
       initData();
    });
    //文字动态输出
    const startDisplayStr = () => {
        if (timer) {
            clearTimeout(timer!);
        }
        const res = streamStr.value;
        // 将数组中的字符串拼接起来
        if (currIndex.value < res.length) {
            displayedText.value += res[currIndex.value];
            currIndex.value++;
            setTimeout(startDisplayStr, 100);
        } else {
            clearTimeout(timer!);
            timer = null
        }
      }
    } else {
      Message.warning('查询失败');
    }
    watch(() => scrollbar.value, (newScroll, oldScroll) => {
  };
  //根据会话id删除会话
  const deleteSession = async (session) => {
    const { code } = await deleteSessionApi([session.id]);
    if (code === 200) {
      Message.success('删除成功');
      querySessionList();
    }
  };
  // eslint-disable-next-line prettier/prettier
  // 新增会话之后刷新会话列表
  const addSession = () => {
    querySessionList();
  };
  // 初始化数据
  const initData = () => {
    querySessionList();
  };
  // 获取登录信息
  const userName = computed(() => {
    return userStore.name;
  });
  const avatar = computed(() => {
    return userStore.avatar;
  });
  const refreshScroll = () => {
    nextTick(() => {
      const container = document.getElementById('home');
      scrollbar.value.scrollTop(container.scrollHeight);
    });
  };
  // 根据会话id 查询会话详情
  const querySessionDetail = async (session) => {
    activeSessionId.value = session.id;
    const { code, data } = await getSessionDetailsApi(session.id);
    if (code === 200) {
      sessionDetailList.value = data.message;
      refreshScroll(); //刷新滚动条位置
    }
  };
  const sendMessage = async () => {
    if (inputMsg.value) {
      const { code, data } = await chatApi({
        conversation_id: activeSessionId.value,
        messages: inputMsg.value,
      });
      const res = await getSessionDetailsApi(activeSessionId.value);
      if (res.code === 200) {
        sessionDetailList.value = res.data.message.map((item, index) => {
          if (index === res.data.message.length - 1) {
            item.role = 'last';
            displayedText.value = '';
            currIndex.value = 0;
            streamStr.value = item.content;
            startDisplayStr();
          }
          return item;
        });
        refreshScroll();
      }
      inputMsg.value = '';
    } else {
      Message.warning('消息不能为空');
    }
  };
  onMounted(() => {
    initData();
  });
  //文字动态输出
  const startDisplayStr = () => {
    if (timer) {
      clearTimeout(timer!);
    }
    const res = streamStr.value;
    // 将数组中的字符串拼接起来
    if (currIndex.value < res.length) {
      displayedText.value += res[currIndex.value];
      currIndex.value++;
      setTimeout(startDisplayStr, 100);
    } else {
      clearTimeout(timer!);
      timer = null;
    }
  };
  watch(
    () => scrollbar.value,
    (newScroll, oldScroll) => {
      if (newScroll) {
        // 获取a-scroll的高度
        const height = newScroll.$el.offsetHeight;
        console.log('a-scroll height changed to:', height);
      }
    },{deep:true})
    },
    { deep: true }
  );
</script>
<template>
    <div class="container">
        <AddSession :modalObj="modalObj" @addSession="addSession"></AddSession>
        <a-row :gutter="[0,0]">
            <a-col :span="5">
                <a-card class="left">
                    <template #cover>
                        <a-button type="outline" class="card-btn" @click="modalObj.add=true">
                            +新建会话
                        </a-button>
                    </template>
                    <a-scrollbar class="left-list" style="height: 60vh;overflow-y: auto;">
                        <div class="item" :class="{ isLeftActive:activeSessionId===session.id }" v-for="session in sessionList" :key="session.id" @click="querySessionDetail(session)">
                            <div class="item-left">
                                <IconQuestionCircleFill/> 
                                {{session.name}}
                            </div>
                            <div class="item-right">
                                <a-popover position="bottom">
                                    <icon-more-vertical></icon-more-vertical>
                                    <template #content>
                                        <!--<div>
  <div class="container">
    <AddSession :modalObj="modalObj" @addSession="addSession"></AddSession>
    <a-row :gutter="[0, 0]">
      <a-col :span="5">
        <a-card class="left">
          <template #cover>
            <a-button
              type="outline"
              class="card-btn"
              @click="modalObj.add = true"
            >
              +新建会话
            </a-button>
          </template>
          <a-scrollbar class="left-list" style="height: 60vh; overflow-y: auto">
            <div
              class="item"
              :class="{ isLeftActive: activeSessionId === session.id }"
              v-for="session in sessionList"
              :key="session.id"
              @click="querySessionDetail(session)"
            >
              <div class="item-left">
                <IconQuestionCircleFill /> 
                {{ session.name }}
              </div>
              <div class="item-right">
                <a-popover position="bottom">
                  <icon-more-vertical></icon-more-vertical>
                  <template #content>
                    <!--<div>
                                            <a-button type="text" size="mini">编辑</a-button>
                                        </div>-->
                                        <div>
                                            <a-button type="text" size="mini" @click="deleteSession(session)">删除</a-button>
                                        </div>
                                    </template>
                                </a-popover>
                            </div>
                        </div>
                    </a-scrollbar>
                    <div class="left-bottom">
                        <div class="item"><IconCommon/> <span>向朋友推荐</span></div>
                        <div class="item"><IconCommon/> <span>最新消息</span></div>
                        <div class="item"><IconCommon/> <span>问题反馈</span></div>
                        <div class="item"><IconCommon/> <span>常见问题</span></div>
                        <div class="item"><IconPoweroff/> <span>退出</span></div>
                    <div>
                      <a-button
                        type="text"
                        size="mini"
                        @click="deleteSession(session)"
                        >删除</a-button
                      >
                    </div>
                </a-card>
            </a-col>
            <a-col :span="19">
                <a-card class="right">
                    <div v-if="sessionDetailList.length===0">
                        <div class="right-title">ChatAI</div>
                        <a-row justify="center" class="right-middle">
                            <a-col :span="5" class="item">
                                <p><a-image :src="img1" width="50px"></a-image></p>
                                <p>示例</p>
                            </a-col>
                            <a-col :span="5" class="item">
                                <p><a-image :src="img2" width="50px"></a-image></p>
                                <p>核心功能</p>
                            </a-col>
                            <a-col :span="5" class="item">
                                <p><a-image :src="img3" width="50px"></a-image></p>
                                <p>局限性</p>
                            </a-col>
                        </a-row>
                        <a-row justify="center" class="right-middle-list">
                            <a-col :span="6" class="item">“请用简单的术语解释量子计算”</a-col>
                            <a-col :span="6" class="item">可以对历史对话进行记忆</a-col>
                            <a-col :span="6" class="item">可能会出现错误的内容</a-col>
                        </a-row>
                        <a-row justify="center" class="right-middle-list">
                            <a-col :span="6" class="item">“提供一些庆祝10岁孩子生日的创意?”</a-col>
                            <a-col :span="6" class="item">允许用户对答案进行修正</a-col>
                            <a-col :span="6" class="item">可能会产生有害或虚假的内容</a-col>
                        </a-row>
                        <a-row justify="center" class="right-middle-list">
                            <a-col :span="6" class="item">“如何在Javascript中发出http请求?”</a-col>
                            <a-col :span="6" class="item">可以接受或拒绝不恰当的训练</a-col>
                            <a-col :span="6" class="item">对2023年以后得世界和事件了解有限</a-col>
                        </a-row>
                    </div>
                    <a-scrollbar ref="scrollbar" id="home" v-else class="chat-list" style="width:90%;overflow:auto;height: 70vh;margin: 0px auto">
                        <div class="chat-item" v-for="sessionDetail in sessionDetailList">
                            <a-comment
                                v-if="sessionDetail.role==='user'"
                                avatar="https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/3ee5f13fb09879ecb5185e440cef6eb9.png~tplv-uwbnlip3yd-webp.webp"
                            >
                                <template #content>
                                    <div :class="{light:theme==='light'}">{{sessionDetail.content}}</div>
                                </template>
                            </a-comment>
                            <a-comment
                                    v-else-if="sessionDetail.role==='assistant'"
                                    avatar="https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/9eeb1800d9b78349b24682c3518ac4a3.png~tplv-uwbnlip3yd-webp.webp"
                            >
                                <template #content>
                                    <a-card class="chat-item-answer"  style="background-color: rgba(63, 64, 79, 1);">
                                        <div :class="{light:theme==='light'}">{{sessionDetail.content}}</div>
                                    </a-card>
                                </template>
                            </a-comment>
                            <a-comment
                                v-else-if="sessionDetail.role==='last'"
                                avatar="https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/9eeb1800d9b78349b24682c3518ac4a3.png~tplv-uwbnlip3yd-webp.webp"
                            >
                                <template #content>
                                    <a-textarea readonly auto-size v-model="displayedText" class="chat-item-answer"  style="background-color: rgba(63, 64, 79, 1);">
                                    </a-textarea>
                                </template>
                            </a-comment>
                        </div>
                    </a-scrollbar>
                    <div class="bottom">
                        <div class="input">
                            <a-input v-model="inputMsg" @keydown.enter="sendMessage" placeholder="输入您想了解的内容,按Enter发送">
                            <template #suffix>
                                <icon-send  style="cursor: pointer" @click="sendMessage"/>
                            </template>
                        </a-input></div>
                        <div class="text">内容由AI生成,仅供参考,请遵守《用户协议》、《个人信息保护规则》,基于ChatGPT大模型,版本:V3.3.0</div>
                    </div>
                </a-card>
            </a-col>
        </a-row>
    </div>
                  </template>
                </a-popover>
              </div>
            </div>
          </a-scrollbar>
          <div class="left-bottom">
            <div class="item"><IconCommon /> <span>向朋友推荐</span></div>
            <div class="item"><IconCommon /> <span>最新消息</span></div>
            <div class="item"><IconCommon /> <span>问题反馈</span></div>
            <div class="item"><IconCommon /> <span>常见问题</span></div>
            <div class="item"><IconPoweroff /> <span>退出</span></div>
          </div>
        </a-card>
      </a-col>
      <a-col :span="19">
        <a-card class="right">
          <div v-if="sessionDetailList.length === 0">
            <div class="right-title">ChatAI</div>
            <a-row justify="center" class="right-middle">
              <a-col :span="5" class="item">
                <p><a-image :src="img1" width="50px"></a-image></p>
                <p>示例</p>
              </a-col>
              <a-col :span="5" class="item">
                <p><a-image :src="img2" width="50px"></a-image></p>
                <p>核心功能</p>
              </a-col>
              <a-col :span="5" class="item">
                <p><a-image :src="img3" width="50px"></a-image></p>
                <p>局限性</p>
              </a-col>
            </a-row>
            <a-row justify="center" class="right-middle-list">
              <a-col :span="6" class="item">“请用简单的术语解释量子计算”</a-col>
              <a-col :span="6" class="item">可以对历史对话进行记忆</a-col>
              <a-col :span="6" class="item">可能会出现错误的内容</a-col>
            </a-row>
            <a-row justify="center" class="right-middle-list">
              <a-col :span="6" class="item"
                >“提供一些庆祝10岁孩子生日的创意?”</a-col
              >
              <a-col :span="6" class="item">允许用户对答案进行修正</a-col>
              <a-col :span="6" class="item">可能会产生有害或虚假的内容</a-col>
            </a-row>
            <a-row justify="center" class="right-middle-list">
              <a-col :span="6" class="item"
                >“如何在Javascript中发出http请求?”</a-col
              >
              <a-col :span="6" class="item">可以接受或拒绝不恰当的训练</a-col>
              <a-col :span="6" class="item"
                >对2023年以后得世界和事件了解有限</a-col
              >
            </a-row>
          </div>
          <a-scrollbar
            ref="scrollbar"
            id="home"
            v-else
            class="chat-list"
            style="width: 90%; overflow: auto; height: 70vh; margin: 0px auto"
          >
            <div class="chat-item" v-for="sessionDetail in sessionDetailList">
              <a-comment
                v-if="sessionDetail.role === 'user'"
                avatar="https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/3ee5f13fb09879ecb5185e440cef6eb9.png~tplv-uwbnlip3yd-webp.webp"
              >
                <template #content>
                  <div :class="{ light: theme === 'light' }">{{
                    sessionDetail.content
                  }}</div>
                </template>
              </a-comment>
              <a-comment
                v-else-if="sessionDetail.role === 'assistant'"
                avatar="https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/9eeb1800d9b78349b24682c3518ac4a3.png~tplv-uwbnlip3yd-webp.webp"
              >
                <template #content>
                  <a-card
                    class="chat-item-answer"
                    style="background-color: rgba(63, 64, 79, 1)"
                  >
                    <div :class="{ light: theme === 'light' }">{{
                      sessionDetail.content
                    }}</div>
                  </a-card>
                </template>
              </a-comment>
              <a-comment
                v-else-if="sessionDetail.role === 'last'"
                avatar="https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/9eeb1800d9b78349b24682c3518ac4a3.png~tplv-uwbnlip3yd-webp.webp"
              >
                <template #content>
                  <a-textarea
                    readonly
                    auto-size
                    v-model="displayedText"
                    class="chat-item-answer"
                    style="background-color: rgba(63, 64, 79, 1)"
                  >
                  </a-textarea>
                </template>
              </a-comment>
            </div>
          </a-scrollbar>
          <div class="bottom">
            <div class="input">
              <a-input
                v-model="inputMsg"
                @keydown.enter="sendMessage"
                placeholder="输入您想了解的内容,按Enter发送"
              >
                <template #suffix>
                  <icon-send style="cursor: pointer" @click="sendMessage" />
                </template> </a-input
            ></div>
            <div class="text"
              >内容由AI生成,仅供参考,请遵守《用户协议》、《个人信息保护规则》,基于ChatGPT大模型,版本:V3.3.0</div
            >
          </div>
        </a-card>
      </a-col>
    </a-row>
  </div>
</template>
<style scoped lang="scss">
    .isLeftActive{
        background-color:rgba(52, 53, 66, 1) ;
  .isLeftActive {
    background-color: rgba(52, 53, 66, 1);
  }
  .light {
    color: white !important;
  }
  .container {
    background-color: #1d2129;
    .left,
    .right {
      color: white;
      height: calc(100vh - 60px);
      border: 0px;
    }
    .light{
        color: white !important;
    }
    .container{
        background-color: #1d2129;
        .left,.right{
            color: white;
            height: calc(100vh - 60px);
            border: 0px;
        }
        .left{
            position: relative;
            background-color: rgba(30, 33, 34, 1);
            .card-btn {
                width: 90%;
                margin: 15px auto;
                border: 1px solid white;
                color: white;
            }
            .left-list{
                .item{
                    display: flex;
                    justify-content: space-between;
                    padding-left: 30px;
                    color: white;
                    cursor: pointer;
                    line-height: 40px;
                    .item-right{
                        margin-right: 10px;
                    }
                    &:hover{
                        background-color:rgba(52, 53, 66, 1) ;
                    }
                }
            }
            .left-bottom{
                position: absolute;
                bottom: 30px;
                left:40px;
                .item{
                    text-align: left;
                    color: white;
                    font-size: 12px;
                    line-height: 30px;
                }
            }
        }
        .right{
            position: relative;
    .left {
      position: relative;
      background-color: rgba(30, 33, 34, 1);
      .card-btn {
        width: 90%;
        margin: 15px auto;
        border: 1px solid white;
        color: white;
      }
      .left-list {
        .item {
          display: flex;
          justify-content: space-between;
          padding-left: 30px;
          color: white;
          cursor: pointer;
          line-height: 40px;
          .item-right {
            margin-right: 10px;
          }
          &:hover {
            background-color: rgba(52, 53, 66, 1);
            .right-title{
                font-size: 30px;
                color: white;
                text-align: center;
                margin-top: 100px;
            }
            .right-middle{
                margin-top: 40px;
                text-align: center;
                color: white;
            }
            .right-middle-list{
                color: white;
                .item{
                    padding-top:15px;
                    text-align: center;
                    height: 50px;
                    background-color: rgba(63, 64, 79, 1);
                    margin:8px 20px;
                    border-radius: 10px;
                }
            }
            .bottom{
                width: 100%;
                position: absolute;
                bottom: 40px;
                left:20%;
                .input{
                    width: 60%;
                }
                .text{
                    font-size: 12px;
                    color: lightgrey;
                    line-height: 40px;
                }
            }
            .chat-list{
                width: 90%;
                margin: 0px auto;
                .chat-item{
                    margin-top: 20px;
                    .chat-item-answer{
                        color: white;
                    }
                }
            }
          }
        }
      }
      .left-bottom {
        position: absolute;
        bottom: 30px;
        left: 40px;
        .item {
          text-align: left;
          color: white;
          font-size: 12px;
          line-height: 30px;
        }
      }
    }
    .right {
      position: relative;
      background-color: rgba(52, 53, 66, 1);
      .right-title {
        font-size: 30px;
        color: white;
        text-align: center;
        margin-top: 100px;
      }
      .right-middle {
        margin-top: 40px;
        text-align: center;
        color: white;
      }
      .right-middle-list {
        color: white;
        .item {
          padding-top: 15px;
          text-align: center;
          height: 50px;
          background-color: rgba(63, 64, 79, 1);
          margin: 8px 20px;
          border-radius: 10px;
        }
      }
      .bottom {
        width: 100%;
        position: absolute;
        bottom: 40px;
        left: 20%;
        .input {
          width: 60%;
        }
        .text {
          font-size: 12px;
          color: lightgrey;
          line-height: 40px;
        }
      }
      .chat-list {
        width: 90%;
        margin: 0px auto;
        .chat-item {
          margin-top: 20px;
          .chat-item-answer {
            color: white;
          }
        }
      }
    }
  }
</style>