mirror of https://gitee.com/godoos/godoos.git
2 changed files with 230 additions and 192 deletions
@ -1,202 +1,237 @@ |
|||||
import { defineStore } from "pinia" |
import { defineStore } from 'pinia' |
||||
import { db } from "./db.js" |
import { db } from './db.js' |
||||
import { t } from "@/i18n/index.ts" |
import { t } from '@/i18n/index.ts' |
||||
import { useAssistantStore } from "./assistant.ts"; |
import { useAssistantStore } from './assistant.ts' |
||||
import { useModelStore } from "@/stores/model"; |
import { useModelStore } from '@/stores/model' |
||||
import { ref } from "vue"; |
import { ref } from 'vue' |
||||
export const useAiChatStore = defineStore('aichat', () => { |
export const useAiChatStore = defineStore( |
||||
const modelStore = useModelStore() |
'aichat', |
||||
const promptStore = useAssistantStore() |
() => { |
||||
const activeId: any = ref(0) |
const modelStore = useModelStore() |
||||
const currentMessage: any = ref({}) |
const promptStore = useAssistantStore() |
||||
// 聊天列表
|
const activeId: any = ref(0) |
||||
const chatList: any = ref([]) |
const currentMessage: any = ref({}) |
||||
const chatInfo: any = ref({}) |
// 聊天列表
|
||||
const messageList: any = ref([]) |
const chatList: any = ref([]) |
||||
const modelList:any = ref([]) |
const chatInfo: any = ref({}) |
||||
const promptList: any = ref([]) |
const messageList: any = ref([]) |
||||
const searchInput: any = ref('') |
const modelList: any = ref([]) |
||||
const showInfo = ref(false) |
const promptList: any = ref([]) |
||||
const editInfo: any = ref({}); //编辑聊天信息
|
const searchInput: any = ref('') |
||||
const isEditor = ref(true); |
const showInfo = ref(false) |
||||
|
const editInfo: any = ref({}) //编辑聊天信息
|
||||
|
const isEditor = ref(true) |
||||
|
|
||||
const newChat = async () => { |
const newChat = async (knowledgeId: any) => { |
||||
const currentModel = await modelStore.getModel('chat') |
const currentModel = await modelStore.getModel('chat') |
||||
if (!currentModel) { |
if (!currentModel) { |
||||
return false |
return false |
||||
|
} |
||||
|
const promptData = await promptStore.getPrompt('chat') |
||||
|
if (knowledgeId) { |
||||
|
return await addChat( |
||||
|
'知识库对话' + knowledgeId, |
||||
|
currentModel, |
||||
|
promptData, |
||||
|
knowledgeId |
||||
|
) |
||||
|
} |
||||
|
return await addChat( |
||||
|
t('aichat.newchat'), |
||||
|
currentModel, |
||||
|
promptData, |
||||
|
knowledgeId |
||||
|
) |
||||
} |
} |
||||
const promptData = await promptStore.getPrompt('chat') |
const getPrompt = async (type: string) => { |
||||
return await addChat(t('aichat.newchat'), currentModel, promptData, "") |
return await promptStore.getPrompt(type) |
||||
} |
} |
||||
const getPrompt = async (type: string) => { |
const initChat = async (knowledgeId: any) => { |
||||
return await promptStore.getPrompt(type) |
if (knowledgeId) { |
||||
} |
const chat = await db.getByField( |
||||
const initChat = async () => { |
'aichatlist', |
||||
if (activeId.value === 0) { |
'knowledgeId', |
||||
await newChat() |
knowledgeId |
||||
} |
) |
||||
modelList.value = await modelStore.getModelAction('chat') |
if (chat.length) { |
||||
const promptRes = await promptStore.getPrompts('chat') |
activeId.value = chat[0].id |
||||
promptList.value = promptRes.list |
} else { |
||||
chatList.value = await db.getAll('aichatlist') |
await newChat(knowledgeId) |
||||
if(activeId.value > 0){ |
} |
||||
messageList.value = await db.getByField('aichatmsg', 'chatId', activeId.value) |
} |
||||
|
if (activeId.value === 0) { |
||||
|
await newChat(knowledgeId) |
||||
|
} |
||||
|
modelList.value = await modelStore.getModelAction('chat') |
||||
|
const promptRes = await promptStore.getPrompts('chat') |
||||
|
promptList.value = promptRes.list |
||||
|
chatList.value = await db.getAll('aichatlist') |
||||
|
if (activeId.value > 0) { |
||||
|
messageList.value = await db.getByField( |
||||
|
'aichatmsg', |
||||
|
'chatId', |
||||
|
activeId.value |
||||
|
) |
||||
|
chatInfo.value = await db.getOne('aichatlist', activeId.value) |
||||
|
} |
||||
|
} |
||||
|
const getActiveChat = async () => { |
||||
chatInfo.value = await db.getOne('aichatlist', activeId.value) |
chatInfo.value = await db.getOne('aichatlist', activeId.value) |
||||
|
messageList.value = await db.getByField( |
||||
|
'aichatmsg', |
||||
|
'chatId', |
||||
|
activeId.value |
||||
|
) |
||||
|
chatList.value = await db.getAll('aichatlist') |
||||
|
return { chatInfo, messageList, chatList } |
||||
} |
} |
||||
} |
const getChatList = async () => { |
||||
const getActiveChat = async () => { |
|
||||
chatInfo.value = await db.getOne('aichatlist', activeId.value) |
|
||||
messageList.value = await db.getByField('aichatmsg', 'chatId', activeId.value) |
|
||||
chatList.value = await db.getAll('aichatlist') |
|
||||
return { chatInfo, messageList, chatList } |
|
||||
} |
|
||||
const getChatList = async () => { |
|
||||
const list = await db.getAll('aichatlist') |
|
||||
chatList.value = list |
|
||||
return list |
|
||||
} |
|
||||
// 添加聊天
|
|
||||
async function addChat(title: string, modelData: any, promptData: any, knowledgeId: string) { |
|
||||
const newChat = { |
|
||||
title, |
|
||||
prompt: promptData.prompt, |
|
||||
promptId: promptData.id, |
|
||||
model: modelData.model, |
|
||||
engine: modelData.info.engine, |
|
||||
createdAt: Date.now(), |
|
||||
knowledgeId |
|
||||
} |
|
||||
//console.log(newChat)
|
|
||||
activeId.value = await db.addOne('aichatlist', newChat) |
|
||||
return activeId.value |
|
||||
|
|
||||
} |
|
||||
async function setActiveId(newId: number) { |
|
||||
activeId.value = newId |
|
||||
chatInfo.value = await db.getOne('aichatlist', newId) |
|
||||
messageList.value = await db.getByField('aichatmsg', 'chatId', newId) |
|
||||
} |
|
||||
// 删除单个聊天
|
|
||||
async function deleteChat(chatId: number) { |
|
||||
await db.delete('aichatlist', chatId) |
|
||||
await db.deleteByField('aichatmsg', 'chatId', chatId) |
|
||||
//如果删除的id是当前id
|
|
||||
let id; |
|
||||
if (chatId == activeId.value) { |
|
||||
//
|
|
||||
const list = await db.getAll('aichatlist') |
const list = await db.getAll('aichatlist') |
||||
if (list.length > 0) { |
chatList.value = list |
||||
id = list[0]['id'] |
return list |
||||
|
} |
||||
} else { |
// 添加聊天
|
||||
id = await newChat() |
async function addChat( |
||||
|
title: string, |
||||
|
modelData: any, |
||||
|
promptData: any, |
||||
|
knowledgeId: string |
||||
|
) { |
||||
|
const newChat = { |
||||
|
title, |
||||
|
prompt: promptData.prompt, |
||||
|
promptId: promptData.id, |
||||
|
model: modelData.model, |
||||
|
engine: modelData.info.engine, |
||||
|
createdAt: Date.now(), |
||||
|
knowledgeId, |
||||
} |
} |
||||
setActiveId(id) |
//console.log(newChat)
|
||||
|
activeId.value = await db.addOne('aichatlist', newChat) |
||||
|
return activeId.value |
||||
|
} |
||||
|
async function setActiveId(newId: number) { |
||||
|
activeId.value = newId |
||||
|
chatInfo.value = await db.getOne('aichatlist', newId) |
||||
|
messageList.value = await db.getByField('aichatmsg', 'chatId', newId) |
||||
|
} |
||||
|
// 删除单个聊天
|
||||
|
async function deleteChat(chatId: number) { |
||||
|
await db.delete('aichatlist', chatId) |
||||
|
await db.deleteByField('aichatmsg', 'chatId', chatId) |
||||
|
//如果删除的id是当前id
|
||||
|
let id |
||||
|
if (chatId == activeId.value) { |
||||
|
//
|
||||
|
const list = await db.getAll('aichatlist') |
||||
|
if (list.length > 0) { |
||||
|
id = list[0]['id'] |
||||
|
} else { |
||||
|
id = await newChat(0) |
||||
|
} |
||||
|
setActiveId(id) |
||||
|
} |
||||
|
chatList.value = await db.getAll('aichatlist') |
||||
} |
} |
||||
chatList.value = await db.getAll('aichatlist') |
|
||||
} |
|
||||
|
|
||||
// 更新聊天菜单标题
|
|
||||
async function updateTitle(chatId: number, title: string) { |
|
||||
await db.update('aichatlist', chatId, { title }) |
|
||||
} |
|
||||
|
|
||||
// 清空所有Chat
|
|
||||
async function clearChat() { |
|
||||
await db.clear('aichatlist') |
|
||||
await db.clear('aichatmsg') |
|
||||
} |
|
||||
|
|
||||
|
// 更新聊天菜单标题
|
||||
|
async function updateTitle(chatId: number, title: string) { |
||||
|
await db.update('aichatlist', chatId, { title }) |
||||
|
} |
||||
|
|
||||
// 新增历史消息
|
// 清空所有Chat
|
||||
async function addMessages(chatId: number, message: any) { |
async function clearChat() { |
||||
const currentChat = await db.getOne('aichatlist', chatId) |
await db.clear('aichatlist') |
||||
//console.log(currentChat)
|
await db.clear('aichatmsg') |
||||
if (currentChat) { |
|
||||
return await db.addOne('aichatmsg', message) |
|
||||
} |
} |
||||
} |
|
||||
|
|
||||
async function getChat(chatId: number) { |
// 新增历史消息
|
||||
const chats = await db.getOne('aichatlist', chatId) |
async function addMessages(chatId: number, message: any) { |
||||
//console.log(chats)
|
const currentChat = await db.getOne('aichatlist', chatId) |
||||
const messages = await db.getByField('aichatmsg', 'chatId', chatId) |
//console.log(currentChat)
|
||||
return { chats, messages } |
if (currentChat) { |
||||
} |
return await db.addOne('aichatmsg', message) |
||||
|
} |
||||
|
} |
||||
|
|
||||
// 获取指定id的聊天的历史记录
|
async function getChat(chatId: number) { |
||||
|
const chats = await db.getOne('aichatlist', chatId) |
||||
|
//console.log(chats)
|
||||
|
const messages = await db.getByField('aichatmsg', 'chatId', chatId) |
||||
|
return { chats, messages } |
||||
|
} |
||||
|
|
||||
async function getChatHistory(chatId: number) { |
// 获取指定id的聊天的历史记录
|
||||
return await db.getByField('aichatmsg', 'chatId', chatId) |
|
||||
} |
|
||||
|
|
||||
// 删除指定id的聊天的历史记录
|
async function getChatHistory(chatId: number) { |
||||
async function clearChatHistory() { |
return await db.getByField('aichatmsg', 'chatId', chatId) |
||||
if(activeId.value > 0){ |
|
||||
await db.deleteByField('aichatmsg', 'chatId', activeId.value) |
|
||||
messageList.value = [] |
|
||||
} |
} |
||||
|
|
||||
} |
|
||||
|
|
||||
// 更新聊天配置
|
// 删除指定id的聊天的历史记录
|
||||
async function updateChat(config: any, chatId: number) { |
async function clearChatHistory() { |
||||
//console.log(config)
|
if (activeId.value > 0) { |
||||
return await db.update('aichatlist', chatId, config) |
await db.deleteByField('aichatmsg', 'chatId', activeId.value) |
||||
} |
messageList.value = [] |
||||
const showBox = (flag: any) => { |
} |
||||
isEditor.value = flag; |
} |
||||
if (flag === true) { |
|
||||
editInfo.value = toRaw(chatInfo.value); |
|
||||
} else { |
|
||||
editInfo.value = { |
|
||||
title: "", |
|
||||
model: "", |
|
||||
prompt: "", |
|
||||
promptId: "", |
|
||||
}; |
|
||||
} |
|
||||
showInfo.value = true; |
|
||||
}; |
|
||||
return { |
|
||||
activeId, |
|
||||
chatList, |
|
||||
messageList, |
|
||||
chatInfo, |
|
||||
currentMessage, |
|
||||
searchInput, |
|
||||
showInfo, |
|
||||
editInfo, |
|
||||
isEditor, |
|
||||
getPrompt, |
|
||||
modelList, |
|
||||
promptList, |
|
||||
initChat, |
|
||||
setActiveId, |
|
||||
getActiveChat, |
|
||||
getChatList, |
|
||||
addChat, |
|
||||
updateTitle, |
|
||||
deleteChat, |
|
||||
clearChat, |
|
||||
addMessages, |
|
||||
getChat, |
|
||||
getChatHistory, |
|
||||
clearChatHistory, |
|
||||
updateChat, |
|
||||
showBox |
|
||||
} |
|
||||
|
|
||||
}, { |
// 更新聊天配置
|
||||
persist: { |
async function updateChat(config: any, chatId: number) { |
||||
enabled: true, |
//console.log(config)
|
||||
strategies: [ |
return await db.update('aichatlist', chatId, config) |
||||
{ |
} |
||||
storage: localStorage, |
const showBox = (flag: any) => { |
||||
paths: [ |
isEditor.value = flag |
||||
"activeId" |
if (flag === true) { |
||||
] |
editInfo.value = toRaw(chatInfo.value) |
||||
}, // name 字段用localstorage存储
|
} else { |
||||
], |
editInfo.value = { |
||||
} |
title: '', |
||||
}) |
model: '', |
||||
|
prompt: '', |
||||
|
promptId: '', |
||||
|
} |
||||
|
} |
||||
|
showInfo.value = true |
||||
|
} |
||||
|
return { |
||||
|
activeId, |
||||
|
chatList, |
||||
|
messageList, |
||||
|
chatInfo, |
||||
|
currentMessage, |
||||
|
searchInput, |
||||
|
showInfo, |
||||
|
editInfo, |
||||
|
isEditor, |
||||
|
getPrompt, |
||||
|
modelList, |
||||
|
promptList, |
||||
|
initChat, |
||||
|
setActiveId, |
||||
|
getActiveChat, |
||||
|
getChatList, |
||||
|
addChat, |
||||
|
updateTitle, |
||||
|
deleteChat, |
||||
|
clearChat, |
||||
|
addMessages, |
||||
|
getChat, |
||||
|
getChatHistory, |
||||
|
clearChatHistory, |
||||
|
updateChat, |
||||
|
showBox, |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
persist: { |
||||
|
enabled: true, |
||||
|
strategies: [ |
||||
|
{ |
||||
|
storage: localStorage, |
||||
|
paths: ['activeId'], |
||||
|
}, // name 字段用localstorage存储
|
||||
|
], |
||||
|
}, |
||||
|
} |
||||
|
) |
||||
|
Loading…
Reference in new issue