From 7ccfc4a10cc3c5118ec635a44a3bff3c146d310d Mon Sep 17 00:00:00 2001 From: Json_Lee <2622336659@qq.com> Date: Mon, 12 May 2025 20:13:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=8E=A5=E5=8F=A3=E5=AF=B9=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 1 + src/api/chat/index.ts | 8 ++ src/api/chat/types.ts | 221 ++++++++++++++++++++++++++++++++++++++ src/api/session/index.ts | 3 +- src/api/session/types.ts | 4 + src/pages/chat/index.vue | 90 +++++++++++++--- src/store/modules/chat.ts | 33 ++++++ src/store/modules/user.ts | 5 +- src/utils/request.ts | 9 +- vite.config.ts | 1 + 10 files changed, 355 insertions(+), 20 deletions(-) create mode 100644 .env create mode 100644 src/api/chat/index.ts create mode 100644 src/api/chat/types.ts create mode 100644 src/store/modules/chat.ts diff --git a/.env b/.env new file mode 100644 index 0000000..0c801b6 --- /dev/null +++ b/.env @@ -0,0 +1 @@ +VITE_API_URL = https://web.pandarobot.chat/api diff --git a/src/api/chat/index.ts b/src/api/chat/index.ts new file mode 100644 index 0000000..94474ba --- /dev/null +++ b/src/api/chat/index.ts @@ -0,0 +1,8 @@ +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 getChatList(params: GetChatListParams) { + return get('/system/message/list', params); +} diff --git a/src/api/chat/types.ts b/src/api/chat/types.ts new file mode 100644 index 0000000..b822a2f --- /dev/null +++ b/src/api/chat/types.ts @@ -0,0 +1,221 @@ +/** + * ChatRequest,描述:对话请求对象 + */ +export interface SendDTO { + /** + * 应用ID + */ + appId?: string; + /** + * 上下文的条数 + */ + contentNumber?: number; + /** + * 是否开启mcp + */ + isMcp?: boolean; + /** + * 知识库id + */ + kid?: string; + messages: Message[]; + model: string; + /** + * 提示词 + */ + prompt?: string; + /** + * 是否开启联网搜索(0关闭 1开启) + */ + search?: boolean; + /** + * 会话id + */ + sessionId?: number; + /** + * 是否开启流式对话 + */ + stream?: boolean; + /** + * 系统提示词 + */ + sysPrompt?: string; + /** + * 用户id + */ + userId?: number; + /** + * 是否携带上下文 + */ + usingContext?: boolean; +} + +/** + * Message,描述: + */ +export interface Message { + content?: string; + name?: string; + reasoning_content?: string; + /** + * 目前支持四个中角色参考官网,进行情景输入: + * https://platform.openai.com/docs/guides/chat/introduction + */ + role?: 'system' | 'user' | 'assistant' | 'function' | 'tool'; + tool_call_id?: string; + /** + * The tool calls generated by the model, such as function calls. + */ + tool_calls?: ToolCalls[]; +} + +/** + * ToolCalls,The tool calls generated by the model, such as function calls. + */ +export interface ToolCalls { + function?: ToolCallFunction; + /** + * The ID of the tool call. + */ + id?: string; + /** + * The type of the tool. Currently, only function is supported. + */ + type?: string; +} + +/** + * ToolCallFunction,ToolCall 的 Function参数 + * The function that the model called. + */ +export interface ToolCallFunction { + /** + * 方法参数 + */ + arguments?: string; + /** + * 方法名 + */ + name?: string; +} + +export interface GetChatListParams { + /** + * 消息内容 + */ + content?: string; + /** + * 创建者 + */ + createBy?: number; + /** + * 创建部门 + */ + createDept?: number; + /** + * 创建时间 + */ + createTime?: Date; + /** + * 扣除金额 + */ + deductCost?: number; + /** + * 主键 + */ + id?: number; + /** + * 排序的方向desc或者asc + */ + isAsc?: string; + /** + * 模型名称 + */ + modelName?: string; + /** + * 排序列 + */ + orderByColumn?: string; + /** + * 当前页数 + */ + pageNum?: number; + /** + * 分页大小 + */ + pageSize?: number; + /** + * 请求参数 + */ + params?: { [key: string]: { [key: string]: any } }; + /** + * 备注 + */ + remark?: string; + /** + * 对话角色 + */ + role?: string; + /** + * 会话id + */ + sessionId?: number; + /** + * 累计 Tokens + */ + totalTokens?: number; + /** + * 更新者 + */ + updateBy?: number; + /** + * 更新时间 + */ + updateTime?: Date; + /** + * 用户id + */ + userId?: number; +} + +/** + * ChatMessageVo,聊天消息视图对象 chat_message + */ +export interface ChatMessageVo { + /** + * 消息内容 + */ + content?: string; + /** + * 扣除金额 + */ + deductCost?: number; + /** + * 主键 + */ + id?: number; + /** + * 模型名称 + */ + modelName?: string; + /** + * 备注 + */ + remark?: string; + /** + * 对话角色 + */ + role?: string; + /** + * 会话id + */ + sessionId?: number; + /** + * 累计 Tokens + */ + totalTokens?: number; + /** + * 用户id + */ + userId?: number; +} diff --git a/src/api/session/index.ts b/src/api/session/index.ts index bac8dc1..8373ba0 100644 --- a/src/api/session/index.ts +++ b/src/api/session/index.ts @@ -1,6 +1,7 @@ import type { ChatSessionVo, CreateSessionDTO, + CreateSessionVO, GetSessionListParams, } from './types'; import { get, post } from '@/utils/request'; @@ -10,5 +11,5 @@ export function getSessionList(params: GetSessionListParams) { } export function createSession(data: CreateSessionDTO) { - return post('/system/session', data); + return post('/system/session', data); } diff --git a/src/api/session/types.ts b/src/api/session/types.ts index 92e8b25..e175be4 100644 --- a/src/api/session/types.ts +++ b/src/api/session/types.ts @@ -136,3 +136,7 @@ export interface CreateSessionDTO { */ userId: number; } + +export interface CreateSessionVO { + id: number; +} diff --git a/src/pages/chat/index.vue b/src/pages/chat/index.vue index c10bfbc..378674b 100644 --- a/src/pages/chat/index.vue +++ b/src/pages/chat/index.vue @@ -1,33 +1,93 @@ diff --git a/src/store/modules/chat.ts b/src/store/modules/chat.ts new file mode 100644 index 0000000..bf26ca8 --- /dev/null +++ b/src/store/modules/chat.ts @@ -0,0 +1,33 @@ +import type { ChatMessageVo } from '@/api/chat/types'; +import { getChatList } from '@/api/chat'; +import { defineStore } from 'pinia'; +import { useUserStore } from './user'; + +export const useChatStore = defineStore('chat', () => { + const userStore = useUserStore(); + const chatMap = ref>({}); + + const setChatMap = (id: number, data: ChatMessageVo[]) => { + chatMap.value[id] = data; + }; + + const requestChatList = async (sessionId: number) => { + try { + const res = await getChatList({ + sessionId, + userId: userStore.userInfo?.userId as number, + }); + if (res.rows) { + setChatMap(sessionId, res.rows); + } + } + catch (error) { + console.error('getChatList:', error); + } + }; + + return { + chatMap, + requestChatList, + }; +}); diff --git a/src/store/modules/user.ts b/src/store/modules/user.ts index 39ea451..3a9b777 100644 --- a/src/store/modules/user.ts +++ b/src/store/modules/user.ts @@ -1,10 +1,12 @@ import type { LoginUser } from '@/api/auth/types'; import { defineStore } from 'pinia'; +import { useRouter } from 'vue-router'; export const useUserStore = defineStore( 'user', () => { const token = ref(); + const router = useRouter(); const setToken = (value: string) => { token.value = value; }; @@ -20,9 +22,10 @@ export const useUserStore = defineStore( userInfo.value = void 0; }; - const logout = () => { + const logout = async () => { // 如果需要调用接口,可以在这里调用 clearToken(); + router.replace({ name: 'login' }); clearUserInfo(); }; diff --git a/src/utils/request.ts b/src/utils/request.ts index 397cdd2..2d5aa23 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -12,11 +12,11 @@ interface BaseResponse { } export const request = hookFetch.create({ - baseURL: 'https://web.pandarobot.chat/api', + baseURL: import.meta.env.VITE_API_URL, headers: { 'Content-Type': 'application/json', }, - plugins: [sseTextDecoderPlugin()], + plugins: [sseTextDecoderPlugin({ json: true, prefix: 'data:' })], }); function jwtPlugin(): HookFetchPlugin { @@ -29,10 +29,13 @@ function jwtPlugin(): HookFetchPlugin { return config; }, afterResponse: async (response) => { - console.log(response); + // console.log(response); if (response.result?.code === 200) { return response; } + if (response.result?.code === 401) { + userStore.logout(); + } ElMessage.error(response.result?.msg); return Promise.reject(response); }, diff --git a/vite.config.ts b/vite.config.ts index 8517444..356eee9 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -6,6 +6,7 @@ import { ElementPlusResolver } from "unplugin-vue-components/resolvers"; import UnoCSS from "unocss/vite"; import path from "path"; +// TODO: 开发一个环境变量ts类型处理的插件 // https://vite.dev/config/ export default defineConfig({ plugins: [