From d2275d5932caeddfab967d9da47f8c425c9e433f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E5=98=89=E6=82=A6?= Date: Thu, 29 May 2025 01:27:35 +0800 Subject: [PATCH] =?UTF-8?q?test(api):=20:white=5Fcheck=5Fmark:=20=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E6=8E=A5=E5=8F=A3=EF=BC=8C=E6=A2=B3=E7=90=86=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/chat/index.ts | 7 ++ src/api/chat/types.ts | 7 +- src/pages/chat/layouts/chatWithId/index.vue | 91 +++++++++++++++------ src/stores/modules/chat.ts | 22 ++--- src/stores/modules/model.ts | 1 - 5 files changed, 86 insertions(+), 42 deletions(-) diff --git a/src/api/chat/index.ts b/src/api/chat/index.ts index 94474ba..8b8a6fe 100644 --- a/src/api/chat/index.ts +++ b/src/api/chat/index.ts @@ -1,8 +1,15 @@ import type { ChatMessageVo, GetChatListParams, SendDTO } from './types'; import { get, post } from '@/utils/request'; +// 发送消息 export const send = (data: SendDTO) => post('/chat/send', data).stream(); +// 新增对应会话聊天记录 +export function addChat(data: ChatMessageVo) { + return post('/system/message', data); +} + +// 获取当前会话的聊天记录 export function getChatList(params: GetChatListParams) { return get('/system/message/list', params); } diff --git a/src/api/chat/types.ts b/src/api/chat/types.ts index 4d5d6f1..b438a7b 100644 --- a/src/api/chat/types.ts +++ b/src/api/chat/types.ts @@ -1,5 +1,3 @@ -import type { ModelType } from '@/constants/enums'; - /** * ChatRequest,描述:对话请求对象 */ @@ -21,7 +19,8 @@ export interface SendDTO { */ kid?: string; messages: Message[]; - model: ModelType; + // model: ModelType; + model?: string; /** * 提示词 */ @@ -161,7 +160,7 @@ export interface GetChatListParams { /** * 会话id */ - sessionId?: number; + sessionId?: string; /** * 累计 Tokens */ diff --git a/src/pages/chat/layouts/chatWithId/index.vue b/src/pages/chat/layouts/chatWithId/index.vue index 9c0ec34..2ffdd6d 100644 --- a/src/pages/chat/layouts/chatWithId/index.vue +++ b/src/pages/chat/layouts/chatWithId/index.vue @@ -6,7 +6,9 @@ import type { ThinkingStatus } from 'vue-element-plus-x/types/Thinking'; import { Loading, Position } from '@element-plus/icons-vue'; import { useXStream } from 'vue-element-plus-x'; import { useRoute } from 'vue-router'; +import { send } from '@/api/chat/index'; import { useChatStore } from '@/stores/modules/chat'; +import { useModelStore } from '@/stores/modules/model'; type MessageItem = BubbleProps & { key: number; @@ -16,15 +18,16 @@ type MessageItem = BubbleProps & { expanded?: boolean; }; -const { startStream, cancel, data, error, isLoading } = useXStream(); +const { startStream: _, cancel, data, error, isLoading } = useXStream(); -const BASE_URL = 'https://api.siliconflow.cn/v1/chat/completions'; +// const BASE_URL = 'https://api.siliconflow.cn/v1/chat/completions'; // 仅供测试,请勿拿去测试其他付费模型 -const API_KEY = 'sk-vfjyscildobjnrijtcllnkhtcouidcxdgjxtldzqzeowrbga'; -const MODEL = 'THUDM/GLM-Z1-9B-0414'; +// const API_KEY = 'sk-vfjyscildobjnrijtcllnkhtcouidcxdgjxtldzqzeowrbga'; +// const MODEL = 'THUDM/GLM-Z1-9B-0414'; const route = useRoute(); const chatStore = useChatStore(); +const modelStore = useModelStore(); const isDeepThinking = computed(() => chatStore.isDeepThinking); const inputValue = ref('帮我写一篇小米手机介绍'); const senderRef = ref(null); @@ -34,9 +37,29 @@ const processedIndex = ref(0); watch( () => route.params?.id, - (_id_) => { + async (_id_) => { if (_id_) { - chatStore.requestChatList(Number(_id_)); + await chatStore.requestChatList(`${_id_}`); + // 判断的当前会话id是否有聊天记录 + if (chatStore.chatMap[`${_id_}`] && chatStore.chatMap[`${_id_}`].length) { + bubbleItems.value = chatStore.chatMap[`${_id_}`].map((item: any) => ({ + key: item.id, + avatar: + item.role === 'user' + ? 'https://avatars.githubusercontent.com/u/76239030?v=4' + : 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png', + content: item.content, + avatarSize: '32px', + role: item.role, + typing: false, + })); + + // 滚动到底部 + setTimeout(() => { + bubbleListRef.value!.scrollToBottom(); + }, 350); + } + const v = localStorage.getItem('chatContent'); if (v) { inputValue.value = v; @@ -122,28 +145,42 @@ async function startSSE() { // 这里有必要调用一下 BubbleList 组件的滚动到底部 手动触发 自动滚动 bubbleListRef.value!.scrollToBottom(); - const response = await fetch(BASE_URL, { - method: 'POST', - headers: { - 'Authorization': `Bearer ${API_KEY}`, - 'Content-Type': 'application/json', - 'Accept': 'text/event-stream', - }, - body: JSON.stringify({ - model: MODEL, - messages: bubbleItems.value - .filter((item: any) => item.role === 'user') - .map((item: any) => ({ - role: item.role, - content: item.content, - })), - stream: true, - }), + const res = await send({ + messages: bubbleItems.value + .filter((item: any) => item.role === 'user') + .map((item: any) => ({ + role: item.role, + content: item.content, + })), + sessionId: Number(route.params?.id), + userId: 1, + model: modelStore.currentModelInfo.modelName ?? '', }); - const readableStream = response.body!; - // 重置状态 - processedIndex.value = 0; - await startStream({ readableStream }); + + console.log('res', res); + + // const response = await fetch(BASE_URL, { + // method: 'POST', + // headers: { + // 'Authorization': `Bearer ${API_KEY}`, + // 'Content-Type': 'application/json', + // 'Accept': 'text/event-stream', + // }, + // body: JSON.stringify({ + // model: MODEL, + // messages: bubbleItems.value + // .filter((item: any) => item.role === 'user') + // .map((item: any) => ({ + // role: item.role, + // content: item.content, + // })), + // stream: true, + // }), + // }); + // const readableStream = response.body!; + // // 重置状态 + // processedIndex.value = 0; + // await startStream({ readableStream }); } catch (err) { handleError(err); diff --git a/src/stores/modules/chat.ts b/src/stores/modules/chat.ts index 55436b8..6774db9 100644 --- a/src/stores/modules/chat.ts +++ b/src/stores/modules/chat.ts @@ -6,13 +6,22 @@ import { useUserStore } from './user'; export const useChatStore = defineStore('chat', () => { const userStore = useUserStore(); - const chatMap = ref>({}); + // 是否开启深度思考 + const isDeepThinking = ref(false); - const setChatMap = (id: number, data: ChatMessageVo[]) => { + const setDeepThinking = (value: boolean) => { + isDeepThinking.value = value; + }; + + // 会议ID对应-聊天记录 map对象 + const chatMap = ref>({}); + + const setChatMap = (id: string, data: ChatMessageVo[]) => { chatMap.value[id] = data; }; - const requestChatList = async (sessionId: number) => { + // 获取当前会话的聊天记录 + const requestChatList = async (sessionId: string) => { // 如果没有 token 则不查询聊天记录 if (!userStore.token) return; @@ -30,13 +39,6 @@ export const useChatStore = defineStore('chat', () => { } }; - // 是否开启深度思考 - const isDeepThinking = ref(false); - - const setDeepThinking = (value: boolean) => { - isDeepThinking.value = value; - }; - return { chatMap, requestChatList, diff --git a/src/stores/modules/model.ts b/src/stores/modules/model.ts index 079c6d6..30c5433 100644 --- a/src/stores/modules/model.ts +++ b/src/stores/modules/model.ts @@ -19,7 +19,6 @@ export const useModelStore = defineStore('model', () => { try { const res = await getModelList(); modelList.value = res.data; - console.log('模型列表', res.data); } catch (error) { console.error('requestModelList错误', error);